From 53df3e8b57b1543ed88bd17159e822eed6a88bf1 Mon Sep 17 00:00:00 2001 From: MohmadAyman Date: Wed, 1 Jun 2016 20:35:00 +0200 Subject: [PATCH] core: cpu: provide function to acquire ISR stack usage --- core/include/arch/thread_arch.h | 5 +++++ cpu/arm7_common/arm_cpu.c | 7 +++++++ cpu/atmega_common/thread_arch.c | 7 +++++++ cpu/cortexm_common/include/cpu.h | 9 +++++++++ cpu/cortexm_common/thread_arch.c | 12 ++++++++++++ cpu/cortexm_common/vectors_cortexm.c | 8 -------- cpu/msp430-common/cpu.c | 7 +++++++ cpu/native/native_cpu.c | 7 +++++++ cpu/x86/x86_thread_arch.c | 25 +++++++++++++++++++++++++ 9 files changed, 79 insertions(+), 8 deletions(-) create mode 100644 cpu/x86/x86_thread_arch.c diff --git a/core/include/arch/thread_arch.h b/core/include/arch/thread_arch.h index 264d9f5ba..48ce4e12e 100644 --- a/core/include/arch/thread_arch.h +++ b/core/include/arch/thread_arch.h @@ -58,6 +58,11 @@ typedef void *(*thread_task_func_t)(void *arg); */ char *thread_arch_stack_init(thread_task_func_t task_func, void *arg, void *stack_start, int stack_size); +/** + * @brief Get the number of bytes used on the ISR stack + */ +int thread_arch_isr_stack_usage(void); + /** * @brief Print the current stack to stdout */ diff --git a/cpu/arm7_common/arm_cpu.c b/cpu/arm7_common/arm_cpu.c index a579b4c5a..2e1c1592a 100644 --- a/cpu/arm7_common/arm_cpu.c +++ b/cpu/arm7_common/arm_cpu.c @@ -28,6 +28,13 @@ void thread_yield_higher(void) __asm__("svc 0\n"); } +/* This function calculates the ISR_usage */ +int thread_arch_isr_stack_usage(void) +{ + /* TODO */ + return -1; +} + /*---------------------------------------------------------------------------- * Processor specific routine - here for ARM7 * sizeof(void*) = sizeof(int) diff --git a/cpu/atmega_common/thread_arch.c b/cpu/atmega_common/thread_arch.c index 9c3aff8ca..452fbd4bc 100644 --- a/cpu/atmega_common/thread_arch.c +++ b/cpu/atmega_common/thread_arch.c @@ -198,6 +198,13 @@ void thread_arch_stack_print(void) printf("stack size: %u bytes\n", size); } +/* This function calculates the ISR_usage */ +int thread_arch_isr_stack_usage(void) +{ + /* TODO */ + return -1; +} + void thread_arch_start_threading(void) __attribute__((naked)); void thread_arch_start_threading(void) { diff --git a/cpu/cortexm_common/include/cpu.h b/cpu/cortexm_common/include/cpu.h index b0628db9e..5ab5f3dde 100644 --- a/cpu/cortexm_common/include/cpu.h +++ b/cpu/cortexm_common/include/cpu.h @@ -48,6 +48,15 @@ extern "C" { #define ARCH_HAS_ATOMIC_COMPARE_AND_SWAP 1 #endif +/** + * @brief Interrupt stack canary value + * + * @note 0xe7fe is the ARM Thumb machine code equivalent of asm("bl #-2\n") or + * 'while (1);', i.e. an infinite loop. + * @internal + */ +#define STACK_CANARY_WORD (0xE7FEE7FEu) + /** * @brief Initialization of the CPU */ diff --git a/cpu/cortexm_common/thread_arch.c b/cpu/cortexm_common/thread_arch.c index 591864afe..97def4ea9 100644 --- a/cpu/cortexm_common/thread_arch.c +++ b/cpu/cortexm_common/thread_arch.c @@ -100,6 +100,9 @@ #include "irq.h" #include "cpu.h" +extern uint32_t _estack; +extern uint32_t _sstack; + /** * @brief Noticeable marker marking the beginning of a stack segment * @@ -251,6 +254,15 @@ void thread_arch_stack_print(void) printf("current stack size: %i byte\n", count); } +/* This function returns the number of bytes used on the ISR stack */ +int thread_arch_isr_stack_usage(void) +{ + uint32_t *ptr = &_sstack; + + while (*(ptr++) == STACK_CANARY_WORD) {} + return (ISR_STACKSIZE - (ptr - &_sstack)); +} + __attribute__((naked)) void NORETURN thread_arch_start_threading(void) { __asm__ volatile ( diff --git a/cpu/cortexm_common/vectors_cortexm.c b/cpu/cortexm_common/vectors_cortexm.c index 0ada8c72d..e29024be2 100644 --- a/cpu/cortexm_common/vectors_cortexm.c +++ b/cpu/cortexm_common/vectors_cortexm.c @@ -31,14 +31,6 @@ #include "panic.h" #include "vectors_cortexm.h" -/** - * @brief Interrupt stack canary value - * - * @note 0xe7fe is the ARM Thumb machine code equivalent of __asm__("bl #-2\n") or - * 'while (1);', i.e. an infinite loop. - */ -#define STACK_CANARY_WORD 0xE7FEE7FEu - /** * @brief Memory markers, defined in the linker script * @{ diff --git a/cpu/msp430-common/cpu.c b/cpu/msp430-common/cpu.c index f39ac458f..f8c4426b9 100644 --- a/cpu/msp430-common/cpu.c +++ b/cpu/msp430-common/cpu.c @@ -33,6 +33,13 @@ __attribute__((naked)) void thread_yield_higher(void) UNREACHABLE(); } +/* This function calculates the ISR_usage */ +int thread_arch_isr_stack_usage(void) +{ + /* TODO */ + return -1; +} + NORETURN void cpu_switch_context_exit(void) { sched_active_thread = sched_threads[0]; diff --git a/cpu/native/native_cpu.c b/cpu/native/native_cpu.c index 90d4f85e8..0a1cfe133 100644 --- a/cpu/native/native_cpu.c +++ b/cpu/native/native_cpu.c @@ -74,6 +74,13 @@ void thread_print_stack(void) return; } +/* This function calculates the ISR_usage */ +int thread_arch_isr_stack_usage(void) +{ + /* TODO */ + return -1; +} + char *thread_stack_init(thread_task_func_t task_func, void *arg, void *stack_start, int stacksize) { char *stk; diff --git a/cpu/x86/x86_thread_arch.c b/cpu/x86/x86_thread_arch.c new file mode 100644 index 000000000..a06c8d68e --- /dev/null +++ b/cpu/x86/x86_thread_arch.c @@ -0,0 +1,25 @@ +/* + * Copyright (C) 2016 Freie Universität Berlin + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License v2.1. See the file LICENSE in the top level + * directory for more details. + */ + +/** + * @{ + * + * @file + * @author Martine Lenders + */ + +#include "thread_arch.h" + +/* This function calculates the ISR_usage */ +int thread_arch_isr_stack_usage(void) +{ + /* TODO */ + return -1; +} + +/** @} */