From 46d914ff456ab167beb776e8adf57af471fa6c24 Mon Sep 17 00:00:00 2001 From: Sachin Parekh Date: Fri, 17 Apr 2020 19:06:26 +0530 Subject: [PATCH 1/2] gdb: Modify PC in case of invalid PC Signed-off-by: Sachin Parekh --- components/esp32/ld/esp32.project.ld.in | 1 + components/esp32s2/ld/esp32s2.project.ld.in | 1 + components/esp_gdbstub/xtensa/gdbstub_xtensa.c | 18 ++++++++++++++++-- components/esp_system/port/panic_handler.c | 11 ++++++++++- 4 files changed, 28 insertions(+), 3 deletions(-) diff --git a/components/esp32/ld/esp32.project.ld.in b/components/esp32/ld/esp32.project.ld.in index ec6cee01b..720cf1b97 100644 --- a/components/esp32/ld/esp32.project.ld.in +++ b/components/esp32/ld/esp32.project.ld.in @@ -142,6 +142,7 @@ SECTIONS . = 0x3C0; KEEP(*(.DoubleExceptionVector.text)); . = 0x400; + _invalid_pc_placeholder = ABSOLUTE(.); *(.*Vector.literal) *(.UserEnter.literal); diff --git a/components/esp32s2/ld/esp32s2.project.ld.in b/components/esp32s2/ld/esp32s2.project.ld.in index a619b1add..ba76cef9c 100644 --- a/components/esp32s2/ld/esp32s2.project.ld.in +++ b/components/esp32s2/ld/esp32s2.project.ld.in @@ -142,6 +142,7 @@ SECTIONS . = 0x3C0; KEEP(*(.DoubleExceptionVector.text)); . = 0x400; + _invalid_pc_placeholder = ABSOLUTE(.); *(.*Vector.literal) *(.UserEnter.literal); diff --git a/components/esp_gdbstub/xtensa/gdbstub_xtensa.c b/components/esp_gdbstub/xtensa/gdbstub_xtensa.c index a0da9e8cc..5187435a3 100644 --- a/components/esp_gdbstub/xtensa/gdbstub_xtensa.c +++ b/components/esp_gdbstub/xtensa/gdbstub_xtensa.c @@ -24,6 +24,8 @@ #warning "gdbstub_xtensa: revisit the implementation for Call0 ABI" #endif +extern int _invalid_pc_placeholder; + static void init_regfile(esp_gdbstub_gdb_regfile_t *dst) { memset(dst, 0, sizeof(*dst)); @@ -47,7 +49,15 @@ void esp_gdbstub_frame_to_regfile(const esp_gdbstub_frame_t *frame, esp_gdbstub_ { init_regfile(dst); const uint32_t *a_regs = (const uint32_t *) &frame->a0; - dst->pc = (frame->pc & 0x3fffffffU) | 0x40000000U; + if (!(esp_ptr_executable((frame->pc & 0x3fffffffU) | 0x40000000U) && (frame->pc & 0xC0000000U))) { + /* Xtensa ABI sets the 2 MSBs of the PC according to the windowed call size + * Incase the PC is invalid, GDB will fail to translate addresses to function names + * Hence replacing the PC to a placeholder address in case of invalid PC + */ + dst->pc = (uint32_t)&_invalid_pc_placeholder; + } else { + dst->pc = (frame->pc & 0x3fffffffU) | 0x40000000U; + } for (int i = 0; i < 16; i++) { dst->a[i] = a_regs[i]; @@ -73,7 +83,11 @@ static void solicited_frame_to_regfile(const XtSolFrame *frame, esp_gdbstub_gdb_ { init_regfile(dst); const uint32_t *a_regs = (const uint32_t *) &frame->a0; - dst->pc = (frame->pc & 0x3fffffffU) | 0x40000000U; + if (!(esp_ptr_executable((frame->pc & 0x3fffffffU) | 0x40000000U) && (frame->pc & 0xC0000000U))) { + dst->pc = (uint32_t)&_invalid_pc_placeholder; + } else { + dst->pc = (frame->pc & 0x3fffffffU) | 0x40000000U; + } /* only 4 registers saved in the solicited frame */ for (int i = 0; i < 4; i++) { diff --git a/components/esp_system/port/panic_handler.c b/components/esp_system/port/panic_handler.c index 312776f97..4e28a92e0 100644 --- a/components/esp_system/port/panic_handler.c +++ b/components/esp_system/port/panic_handler.c @@ -51,7 +51,9 @@ #include "panic_internal.h" -extern void esp_panic_handler(panic_info_t *); +extern int _invalid_pc_placeholder; + +extern void esp_panic_handler(panic_info_t*); static wdt_hal_context_t wdt0_context = {.inst = WDT_MWDT0, .mwdt_dev = &TIMERG0}; @@ -512,6 +514,13 @@ static void panic_handler(XtExcFrame *frame, bool pseudo_excause) #endif if (esp_cpu_in_ocd_debug_mode()) { + if (!(esp_ptr_executable(cpu_ll_pc_to_ptr(frame->pc)) && (frame->pc & 0xC0000000U))) { + /* Xtensa ABI sets the 2 MSBs of the PC according to the windowed call size + * Incase the PC is invalid, GDB will fail to translate addresses to function names + * Hence replacing the PC to a placeholder address in case of invalid PC + */ + frame->pc = (uint32_t)&_invalid_pc_placeholder; + } if (frame->exccause == PANIC_RSN_INTWDT_CPU0 || frame->exccause == PANIC_RSN_INTWDT_CPU1) { wdt_hal_write_protect_disable(&wdt0_context); From c0a33487b1002ba2e21fdae8b70dcccdb5d1ce57 Mon Sep 17 00:00:00 2001 From: Sachin Parekh Date: Mon, 20 Apr 2020 13:52:21 +0530 Subject: [PATCH 2/2] gdbstub_xtensa.c: Replace with cpu_ll_pc_to_ptr macro Signed-off-by: Sachin Parekh --- components/esp_gdbstub/xtensa/gdbstub_xtensa.c | 10 +++++----- components/soc/src/esp32/include/hal/cpu_ll.h | 2 +- components/soc/src/esp32s2/include/hal/cpu_ll.h | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/components/esp_gdbstub/xtensa/gdbstub_xtensa.c b/components/esp_gdbstub/xtensa/gdbstub_xtensa.c index 5187435a3..cf8614ac5 100644 --- a/components/esp_gdbstub/xtensa/gdbstub_xtensa.c +++ b/components/esp_gdbstub/xtensa/gdbstub_xtensa.c @@ -34,7 +34,7 @@ static void init_regfile(esp_gdbstub_gdb_regfile_t *dst) static void update_regfile_common(esp_gdbstub_gdb_regfile_t *dst) { if (dst->a[0] & 0x8000000U) { - dst->a[0] = (dst->a[0] & 0x3fffffffU) | 0x40000000U; + dst->a[0] = (uint32_t)cpu_ll_pc_to_ptr(dst->a[0]); } if (!esp_stack_ptr_is_sane(dst->a[1])) { dst->a[1] = 0xDEADBEEF; @@ -49,14 +49,14 @@ void esp_gdbstub_frame_to_regfile(const esp_gdbstub_frame_t *frame, esp_gdbstub_ { init_regfile(dst); const uint32_t *a_regs = (const uint32_t *) &frame->a0; - if (!(esp_ptr_executable((frame->pc & 0x3fffffffU) | 0x40000000U) && (frame->pc & 0xC0000000U))) { + if (!(esp_ptr_executable(cpu_ll_pc_to_ptr(frame->pc)) && (frame->pc & 0xC0000000U))) { /* Xtensa ABI sets the 2 MSBs of the PC according to the windowed call size * Incase the PC is invalid, GDB will fail to translate addresses to function names * Hence replacing the PC to a placeholder address in case of invalid PC */ dst->pc = (uint32_t)&_invalid_pc_placeholder; } else { - dst->pc = (frame->pc & 0x3fffffffU) | 0x40000000U; + dst->pc = (uint32_t)cpu_ll_pc_to_ptr(frame->pc); } for (int i = 0; i < 16; i++) { @@ -83,10 +83,10 @@ static void solicited_frame_to_regfile(const XtSolFrame *frame, esp_gdbstub_gdb_ { init_regfile(dst); const uint32_t *a_regs = (const uint32_t *) &frame->a0; - if (!(esp_ptr_executable((frame->pc & 0x3fffffffU) | 0x40000000U) && (frame->pc & 0xC0000000U))) { + if (!(esp_ptr_executable(cpu_ll_pc_to_ptr(frame->pc)) && (frame->pc & 0xC0000000U))) { dst->pc = (uint32_t)&_invalid_pc_placeholder; } else { - dst->pc = (frame->pc & 0x3fffffffU) | 0x40000000U; + dst->pc = (uint32_t)cpu_ll_pc_to_ptr(frame->pc); } /* only 4 registers saved in the solicited frame */ diff --git a/components/soc/src/esp32/include/hal/cpu_ll.h b/components/soc/src/esp32/include/hal/cpu_ll.h index f063576fb..c33d5d182 100644 --- a/components/soc/src/esp32/include/hal/cpu_ll.h +++ b/components/soc/src/esp32/include/hal/cpu_ll.h @@ -103,7 +103,7 @@ static inline uint32_t cpu_ll_ptr_to_pc(const void* addr) static inline void* cpu_ll_pc_to_ptr(uint32_t pc) { - return (void*) ((pc & 0x3fffffff) | 0x40000000); + return (void*) ((pc & 0x3fffffffU) | 0x40000000U); } static inline void cpu_ll_set_watchpoint(int id, diff --git a/components/soc/src/esp32s2/include/hal/cpu_ll.h b/components/soc/src/esp32s2/include/hal/cpu_ll.h index cfcefc3de..ffb4271fb 100644 --- a/components/soc/src/esp32s2/include/hal/cpu_ll.h +++ b/components/soc/src/esp32s2/include/hal/cpu_ll.h @@ -97,7 +97,7 @@ static inline uint32_t cpu_ll_ptr_to_pc(const void* addr) static inline void* cpu_ll_pc_to_ptr(uint32_t pc) { - return (void*) ((pc & 0x3fffffff) | 0x40000000); + return (void*) ((pc & 0x3fffffffU) | 0x40000000U); } static inline void cpu_ll_set_watchpoint(int id,