From 34e11bf2bc2b8956de518b6c14a695c81fd81758 Mon Sep 17 00:00:00 2001 From: Benjamin Valentin Date: Wed, 13 Jul 2022 23:19:14 +0200 Subject: [PATCH 1/2] sys/architecture: introduce uinttxtptr_t --- cpu/avr8_common/include/architecture_arch.h | 4 ++++ sys/include/architecture.h | 13 +++++++++++++ 2 files changed, 17 insertions(+) diff --git a/cpu/avr8_common/include/architecture_arch.h b/cpu/avr8_common/include/architecture_arch.h index a4e7733fb5..9cca5f3362 100644 --- a/cpu/avr8_common/include/architecture_arch.h +++ b/cpu/avr8_common/include/architecture_arch.h @@ -29,6 +29,10 @@ extern "C" { /* Doc is provided centrally in platform.h, hide this from Doxygen */ #ifndef DOXYGEN #define ARCHITECTURE_WORD_BITS (8U) + +#define ARCHITECTURE_LARGE_TXT_PTR 1 +typedef uint32_t uinttxtptr_t; +#define PRIxTXTPTR PRIx32 #endif /* DOXYGEN */ #ifdef __cplusplus diff --git a/sys/include/architecture.h b/sys/include/architecture.h index 906b192eff..ebd7ce1c31 100644 --- a/sys/include/architecture.h +++ b/sys/include/architecture.h @@ -25,6 +25,7 @@ #define ARCHITECTURE_H #include +#include #include "architecture_arch.h" @@ -79,6 +80,18 @@ typedef int32_t sword_t; #error "Unsupported word size (check ARCHITECTURE_WORD_BITS in architecture_arch.h)" #endif +#if !defined(ARCHITECTURE_LARGE_TXT_PTR) || DOXYGEN +/** + * @brief Pointer type to point anywhere in the .text section + */ +typedef uintptr_t uinttxtptr_t; + +/** + * @brief Format string macro for text section pointer + */ +#define PRIxTXTPTR PRIxPTR +#endif + /** * @brief Type qualifier to use to align data on word boundaries * From ea917f4b07d1283335bdcb60ee7bc63ef31ad767 Mon Sep 17 00:00:00 2001 From: Benjamin Valentin Date: Fri, 17 Jun 2022 17:19:49 +0200 Subject: [PATCH 2/2] cpu: cpu_print_last_instruction() -> cpu_get_caller_pc() --- core/lib/assert.c | 3 ++- cpu/avr8_common/include/cpu.h | 9 +++++---- cpu/cortexm_common/include/cpu.h | 10 ++++++---- cpu/esp_common/include/cpu.h | 5 +++-- cpu/lpc23xx/include/cpu.h | 8 ++++---- cpu/mips32r2_common/include/cpu.h | 5 +++-- cpu/msp430_common/include/cpu.h | 6 +++--- cpu/native/include/cpu.h | 9 +++++---- cpu/riscv_common/include/cpu.h | 5 +++-- 9 files changed, 34 insertions(+), 26 deletions(-) diff --git a/core/lib/assert.c b/core/lib/assert.c index e1e4bccfe4..bf5607bdaa 100644 --- a/core/lib/assert.c +++ b/core/lib/assert.c @@ -16,6 +16,7 @@ #include #include "assert.h" +#include "architecture.h" #include "cpu.h" #include "panic.h" @@ -27,7 +28,7 @@ __NORETURN void _assert_failure(const char *file, unsigned line) __NORETURN void _assert_panic(void) { - cpu_print_last_instruction(); + printf("%" PRIxTXTPTR "\n", cpu_get_caller_pc()); core_panic(PANIC_ASSERT_FAIL, "FAILED ASSERTION."); } diff --git a/cpu/avr8_common/include/cpu.h b/cpu/avr8_common/include/cpu.h index 0c28ad2f6c..18c4dcee71 100644 --- a/cpu/avr8_common/include/cpu.h +++ b/cpu/avr8_common/include/cpu.h @@ -36,6 +36,7 @@ #include #include +#include "architecture.h" #include "cpu_conf.h" #include "cpu_clock.h" #include "sched.h" @@ -167,14 +168,14 @@ void avr8_exit_isr(void); void avr8_clk_init(void); /** - * @brief Print the last instruction's address + * @brief Get the last instruction's address * * @details This works only if called in a function as first statement, as * it relies on the return address to be the topmost item on the stack. */ -static inline void __attribute__((always_inline)) cpu_print_last_instruction(void) +static inline uinttxtptr_t __attribute__((always_inline)) cpu_get_caller_pc(void) { - uint32_t addr; + uinttxtptr_t addr; __asm__ volatile( "ldi %D[dest], 0" "\n\t" #if __AVR_3_BYTE_PC__ @@ -198,7 +199,7 @@ static inline void __attribute__((always_inline)) cpu_print_last_instruction(voi * the instruction that called this function. Also multiply by two to get * the byte position, rather than the (16 bit) instruction position */ addr = (addr - 1 ) * 2; - printf("0x%" PRIx32 "\n", addr); + return addr; } /** diff --git a/cpu/cortexm_common/include/cpu.h b/cpu/cortexm_common/include/cpu.h index dd36710dc4..c99de0492e 100644 --- a/cpu/cortexm_common/include/cpu.h +++ b/cpu/cortexm_common/include/cpu.h @@ -127,13 +127,15 @@ void cortexm_init_misc(void); #endif /* defined(CPU_CORTEXM_INIT_SUBFUNCTIONS) || defined(DOXYGEN) */ /** - * @brief Prints the current content of the link register (lr) + * @brief Returns the current content of the link register (lr) + * + * @return content of the link register (lr) */ -static inline void cpu_print_last_instruction(void) +static inline uintptr_t cpu_get_caller_pc(void) { - uint32_t *lr_ptr; + uintptr_t lr_ptr; __asm__ __volatile__("mov %0, lr" : "=r"(lr_ptr)); - printf("%p\n", (void*) lr_ptr); + return lr_ptr; } /** diff --git a/cpu/esp_common/include/cpu.h b/cpu/esp_common/include/cpu.h index e28ef2139d..4b385bda6c 100644 --- a/cpu/esp_common/include/cpu.h +++ b/cpu/esp_common/include/cpu.h @@ -30,13 +30,14 @@ extern "C" { #define PROVIDES_PM_SET_LOWEST /** - * @brief Print the last instruction's address + * @brief Gets the last instruction's address * * @todo: Not supported */ -static inline void cpu_print_last_instruction(void) +static inline uintptr_t cpu_get_caller_pc(void) { /* This function must exist else RIOT won't compile */ + return 0; } #ifdef __cplusplus diff --git a/cpu/lpc23xx/include/cpu.h b/cpu/lpc23xx/include/cpu.h index 81dedb20ee..506b3ec7a6 100644 --- a/cpu/lpc23xx/include/cpu.h +++ b/cpu/lpc23xx/include/cpu.h @@ -58,13 +58,13 @@ void gpio_init_ports(void); #endif /** - * @brief Prints the current content of the link register (lr) + * @brief Returns the current content of the link register (lr) */ -static inline void cpu_print_last_instruction(void) +static inline uintptr_t cpu_get_caller_pc(void) { - register uint32_t *lr_ptr; + register uintptr_t lr_ptr; __asm__ __volatile__("mov %0, lr" : "=r"(lr_ptr)); - printf("%p\n", (void*) lr_ptr); + return lr_ptr; } /** diff --git a/cpu/mips32r2_common/include/cpu.h b/cpu/mips32r2_common/include/cpu.h index e39aef634a..17f6874a24 100644 --- a/cpu/mips32r2_common/include/cpu.h +++ b/cpu/mips32r2_common/include/cpu.h @@ -41,13 +41,14 @@ extern "C" { /** @} */ /** - * @brief Print the last instruction's address + * @brief Gets the last instruction's address * * @todo: Not supported */ -static inline void cpu_print_last_instruction(void) +static inline uintptr_t cpu_get_caller_pc(void) { /* This function must exist else RIOT won't compile */ + return 0; } /** diff --git a/cpu/msp430_common/include/cpu.h b/cpu/msp430_common/include/cpu.h index 701554ccb3..28a61943aa 100644 --- a/cpu/msp430_common/include/cpu.h +++ b/cpu/msp430_common/include/cpu.h @@ -118,13 +118,13 @@ static inline void __attribute__((always_inline)) __exit_isr(void) } /** - * @brief Print the last instruction's address + * @brief Returns the last instruction's address * * @todo: Not supported */ -static inline void cpu_print_last_instruction(void) +static inline uintptr_t cpu_get_caller_pc(void) { - puts("n/a"); + return 0; } #ifdef __cplusplus diff --git a/cpu/native/include/cpu.h b/cpu/native/include/cpu.h index 48740ff0c7..20a8f6310b 100644 --- a/cpu/native/include/cpu.h +++ b/cpu/native/include/cpu.h @@ -20,6 +20,7 @@ #ifndef CPU_H #define CPU_H +#include #include #include "cpu_conf.h" @@ -34,14 +35,14 @@ extern "C" { #define CPU_HAS_UNALIGNED_ACCESS /** - * @brief Prints the address the callee will return to + * @brief Gets the address the callee will return to */ -__attribute__((always_inline)) static inline void cpu_print_last_instruction(void) +__attribute__((always_inline)) static inline uintptr_t cpu_get_caller_pc(void) { /* __builtin_return_address will return the address the calling function - * will return to - since cpu_print_last_instruction is forced inline, + * will return to - since cpu_get_caller_pc is forced inline, * it is the return address of the user of this function */ - printf("%p\n", __builtin_return_address(0)); + return (uintptr_t)__builtin_return_address(0); } #ifdef __cplusplus diff --git a/cpu/riscv_common/include/cpu.h b/cpu/riscv_common/include/cpu.h index 2bf814519b..25ce04b66c 100644 --- a/cpu/riscv_common/include/cpu.h +++ b/cpu/riscv_common/include/cpu.h @@ -47,13 +47,14 @@ void riscv_fpu_init(void); void riscv_irq_init(void); /** - * @brief Print the last instruction's address + * @brief Gets the last instruction's address * * @todo: Not supported */ -static inline void cpu_print_last_instruction(void) +static inline uintptr_t cpu_get_caller_pc(void) { /* This function must exist else RIOT won't compile */ + return 0; } /**