[Cryptech-Commits] [sw/stm32] 08/08: Rebase branch 'profiling' from master
git at cryptech.is
git at cryptech.is
Thu Sep 7 22:42:08 UTC 2017
This is an automated email from the git hooks/post-receive script.
paul at psgd.org pushed a commit to branch profiling
in repository sw/stm32.
commit 72227852729ed3125af58cff3f593340b3247fed
Merge: 2e1f880 5ff8c95
Author: Paul Selkirk <paul at psgd.org>
AuthorDate: Thu Sep 7 18:17:12 2017 -0400
Rebase branch 'profiling' from master
Makefile | 10 +++++++---
libraries/libprof/README.txt | 2 +-
libraries/libprof/profil.c | 17 ++++++++++-------
libraries/libtfm/Makefile | 5 +++++
projects/hsm/Makefile | 6 ++++--
projects/hsm/hsm.c | 5 +++++
projects/hsm/mgmt-task.c | 9 ++++++---
task.c | 8 ++++----
task.h | 2 +-
9 files changed, 43 insertions(+), 21 deletions(-)
diff --cc Makefile
index 12e7df8,68f4da3..e349ab1
--- a/Makefile
+++ b/Makefile
@@@ -117,6 -116,6 +117,9 @@@ CFLAGS += -I$(MBED_DIR)/targets/cmsis/T
CFLAGS += -I$(MBED_DIR)/targets/cmsis/TARGET_STM/TARGET_STM32F4/$(BOARD)
CFLAGS += -I$(MBED_DIR)/targets/hal/TARGET_STM/TARGET_STM32F4
CFLAGS += -I$(MBED_DIR)/targets/hal/TARGET_STM/TARGET_STM32F4/$(BOARD)
++ifdef DO_TASK_METRICS
++CFLAGS += -DDO_TASK_METRICS
++endif
%.o : %.c
$(CC) $(CFLAGS) -c -o $@ $<
@@@ -124,7 -123,7 +127,11 @@@
%.o : %.S
$(CC) $(CFLAGS) -c -o $@ $<
++ifdef DO_PROFILING
++all: hsm
++else
all: board-test cli-test libhal-test hsm bootloader
++endif
$(MBED_DIR)/libstmf4.a: .FORCE
$(MAKE) -C $(MBED_DIR)
@@@ -152,9 -151,9 +159,6 @@@ libhal-test: $(BOARD_OBJS) $(LIBS) $(LI
ifdef DO_PROFILING
CFLAGS += -pg -DDO_PROFILING
--endif
--ifdef DO_TASK_METRICS
--CFLAGS += -DDO_TASK_METRICS
hsm: $(BOARD_OBJS) $(LIBS) $(LIBHAL_BLD)/libhal.a $(LIBCLI_BLD)/libcli.a $(LIBPROF_BLD)/libprof.a .FORCE
$(MAKE) -C projects/hsm
else
diff --cc libraries/libprof/README.txt
index 1fe378c,1fe378c..f0b8ee8
--- a/libraries/libprof/README.txt
+++ b/libraries/libprof/README.txt
@@@ -52,7 -52,7 +52,7 @@@ In the OpenOCD console, enable semihost
In another window, start the debugger:
-- $ sw/stm32/bin/debug projects/hsm/hsm
++ $ ../../bin/debug hsm
In the CLI, type `profile start`, then start the unit test or whatever
will be exercising the hsm. Afterwards, in the CLI, type `profile stop`.
diff --cc libraries/libprof/profil.c
index 0654879,0654879..b0d8d55
--- a/libraries/libprof/profil.c
+++ b/libraries/libprof/profil.c
@@@ -25,27 -25,27 +25,30 @@@ static struct profinfo prof =
PROFILE_NOT_INIT, 0, 0, 0, 0
};
--extern void set_SysTick_hook(void (*hook)(void));
--
/* sample the current program counter */
--static void SysTick_hook(void) {
-- size_t pc = (size_t)((uint32_t *)__get_MSP())[8];
-- if (pc >= prof.lowpc && pc < prof.highpc) {
++void profil_callback(void) {
++ if (prof.state == PROFILE_ON) {
++ /* The interrupt mechanism pushes xPSR, PC, LR, R12, and R3-R0 onto the
++ * stack, so PC is the 6th word from the top at that point. However, the
++ * normal function entry code pushes registers as well, so the stack
++ * offset right now depends on the call tree that got us here.
++ */
++ size_t pc = (size_t)((uint32_t *)__get_MSP())[6 + 6];
++ if (pc >= prof.lowpc && pc < prof.highpc) {
size_t idx = PROFIDX (pc, prof.lowpc, prof.scale);
prof.counter[idx]++;
++ }
}
}
/* Stop profiling to the profiling buffer pointed to by p. */
static int profile_off (struct profinfo *p) {
-- set_SysTick_hook(NULL);
p->state = PROFILE_OFF;
return 0;
}
/* Create a timer thread and pass it a pointer P to the profiling buffer. */
static int profile_on (struct profinfo *p) {
-- set_SysTick_hook(SysTick_hook);
p->state = PROFILE_ON;
return 0; /* ok */
}
diff --cc libraries/libtfm/Makefile
index ceb8541,6da552d..5be45f4
--- a/libraries/libtfm/Makefile
+++ b/libraries/libtfm/Makefile
@@@ -10,33 -10,10 +10,38 @@@ BITS := 819
HDR := ${LIBTFM_SRC}/tomsfastmath/src/headers/tfm.h
LIB := tomsfastmath/libtfm.a
-#CFLAGS += -DTFM_X86
-#CFLAGS += -DTFM_NO_ASM
+# See sw/thirdparty/libtfm/Makefile for compilation options. Note
+# that libtfm platform-specific assembly code has opinions on the
+# optimization level (and appears to be best tested with -O3).
-CFLAGS += -fPIC -Wall -W -Wshadow -I${LIBTFM_SRC}/tomsfastmath/src/headers -g3 -DFP_MAX_SIZE="(${BITS}*2+(8*DIGIT_BIT))"
+# Using $(subst...) here is a kludge. A cleaner approach might be for
+# sw/stm32/Makefile to build up the non-variant parts of CFLAGS in a
+# different variable before merging the variant and non-variant parts
+# into CFLAGS, which would give us a clean copy of the non-variant
+# parts to use when constructing our own CFLAGS. Later.
+
+# The ARM assembly code in libtfm still generates a lot of warnings of the form:
+#
+# warning: matching constraint does not allow a register [enabled by default]
+#
+# This is just a warning, the resulting library appears to work
+# correctly, and the fix appears to require a nasty intervention in
+# the guts of the libtfm assembly code, so we live with the warning
+# for now, at least until we confirm that it hasn't already been fixed
+# in a newer version of libtfm.
+
++ifdef DO_PROFILING
++# arm-none-eabi-gcc: error: -pg and -fomit-frame-pointer are incompatible
++STM32_LIBTFM_CFLAGS_OPTIMIZATION := -O3 -funroll-loops
++else
+STM32_LIBTFM_CFLAGS_OPTIMIZATION := -O3 -funroll-loops -fomit-frame-pointer
++endif
+
+CFLAGS := $(subst ${STM32_CFLAGS_OPTIMIZATION},${STM32_LIBTFM_CFLAGS_OPTIMIZATION},${CFLAGS})
+CFLAGS += -DTFM_ARM -Dasm=__asm__ -Wa,-mimplicit-it=thumb
+CFLAGS += -I${LIBTFM_SRC}/tomsfastmath/src/headers
+CFLAGS += -DFP_MAX_SIZE="(${BITS}*2+(8*DIGIT_BIT))"
+CFLAGS += -Wall -W -Wshadow
TARGETS := $(notdir ${HDR} ${LIB})
diff --cc projects/hsm/hsm.c
index b6b8820,33342c0..5ef2ccc
--- a/projects/hsm/hsm.c
+++ b/projects/hsm/hsm.c
@@@ -244,38 -244,25 +244,43 @@@ static void RxCallback(uint8_t c
}
}
-static uint8_t uart_rx[2]; /* current character received from UART */
-static uint32_t uart_rx_idx = 0;
-
-/* UART DMA half-complete and complete callbacks. With a 2-character DMA
- * buffer, one or the other of these will fire on each incoming character.
- * Under heavy load, these will sometimes fire in the wrong order, but the
- * data are in the right order in the DMA buffer, so we have a flip-flop
- * buffer index that doesn't depend on the order of the callbacks.
+/* A ring buffer for the UART DMA receiver. In theory, it should get at most
+ * 92 characters per 1ms tick, but we're going to up-size it for safety.
*/
-void HAL_UART2_RxHalfCpltCallback(UART_HandleTypeDef *huart)
-{
- RxCallback(uart_rx[uart_rx_idx]);
- uart_rx_idx ^= 1;
-}
+#ifndef RPC_UART_RECVBUF_SIZE
+#define RPC_UART_RECVBUF_SIZE 1024 /* must be a power of 2 */
+#endif
+#define RPC_UART_RECVBUF_MASK (RPC_UART_RECVBUF_SIZE - 1)
+
+typedef struct {
+ uint32_t ridx;
+ uint8_t buf[RPC_UART_RECVBUF_SIZE];
+} uart_ringbuf_t;
+
+volatile uart_ringbuf_t uart_ringbuf = {0, {0}};
-void HAL_UART2_RxCpltCallback(UART_HandleTypeDef *huart)
+#define RINGBUF_RIDX(rb) (rb.ridx & RPC_UART_RECVBUF_MASK)
+#define RINGBUF_WIDX(rb) (sizeof(rb.buf) - __HAL_DMA_GET_COUNTER(huart_user.hdmarx))
+#define RINGBUF_COUNT(rb) ((RINGBUF_WIDX(rb) - RINGBUF_RIDX(rb)) & RPC_UART_RECVBUF_MASK)
+#define RINGBUF_READ(rb, dst) {dst = rb.buf[RINGBUF_RIDX(rb)]; rb.ridx++;}
+
+size_t uart_rx_max = 0;
+
+void HAL_SYSTICK_Callback(void)
{
- RxCallback(uart_rx[uart_rx_idx]);
- uart_rx_idx ^= 1;
++#ifdef DO_PROFILING
++ extern void profil_callback(void);
++ profil_callback();
++#endif
++
+ size_t count = RINGBUF_COUNT(uart_ringbuf);
+ if (uart_rx_max < count) uart_rx_max = count;
+
+ while (RINGBUF_COUNT(uart_ringbuf)) {
+ uint8_t c;
+ RINGBUF_READ(uart_ringbuf, c);
+ RxCallback(c);
+ }
}
/* Send one character over the UART. This is called from
diff --cc projects/hsm/mgmt-task.c
index 9f6a908,1658a80..4668585
--- a/projects/hsm/mgmt-task.c
+++ b/projects/hsm/mgmt-task.c
@@@ -99,9 -95,9 +99,12 @@@ void configure_cli_task(struct cli_def
struct cli_command *c = cli_register_command(cli, NULL, "task", NULL, 0, 0, NULL);
/* task show */
-- struct cli_command *c_show = cli_register_command(cli, c, "show", cmd_task_show, 0, 0, "Show the active tasks");
++#ifdef DO_TASK_METRICS
++ struct cli_command *c_show =
++#endif
++ cli_register_command(cli, c, "show", cmd_task_show, 0, 0, "Show the active tasks");
- #ifdef TASK_METRICS
+ #ifdef DO_TASK_METRICS
/* task show metrics */
cli_register_command(cli, c_show, "metrics", cmd_task_show_metrics, 0, 0, "Show task metrics");
More information about the Commits
mailing list