From 46d914ff456ab167beb776e8adf57af471fa6c24 Mon Sep 17 00:00:00 2001 From: Sachin Parekh Date: Fri, 17 Apr 2020 19:06:26 +0530 Subject: [PATCH] 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);