error_handling: save SP and some values from stack

This commit is contained in:
Andrey Gusakov 2025-01-22 18:09:02 +03:00 committed by rusefillc
parent e830ac8015
commit 71e2bc9f74
2 changed files with 30 additions and 0 deletions

View File

@ -197,6 +197,14 @@ do { \
} \
} while(0)
#define printErrorStack() \
do { \
PRINT("SP 0x%08lx", err->sp); \
for (size_t i = 0; i < ERROR_STACK_DEPTH; i++) { \
PRINT(" 0x%08lx: 0x%08lx", err->sp - i * 4, err->stack[i]); \
} \
} while(0)
// TODO: reuse this code for writing crash report file
void errorHandlerShowBootReasonAndErrors() {
//this is console print
@ -209,6 +217,7 @@ void errorHandlerShowBootReasonAndErrors() {
ErrorCookie cookie = err->Cookie;
printErrorState();
printErrorStack();
#endif // EFI_BACKUP_SRAM
#undef PRINT
}
@ -276,6 +285,7 @@ void errorHandlerWriteReportFile(FIL *fd, int index) {
printResetReason();
#if EFI_BACKUP_SRAM
printErrorState();
printErrorStack();
#endif // EFI_BACKUP_SRAM
// additional board-specific data
onBoardWriteErrorFile(fd);
@ -295,6 +305,15 @@ backupErrorState *errorHandlerGetLastErrorDescriptor(void)
#endif
}
static void errorHandlerSaveStack(backupErrorState *err, uint32_t *sp)
{
err->sp = (uint32_t)sp;
for (size_t i = 0; i < ERROR_STACK_DEPTH; i++) {
err->stack[i] = *sp;
sp++;
}
}
void logHardFault(uint32_t type, uintptr_t faultAddress, port_extctx* ctx, uint32_t csfr) {
criticalShutdown();
#if EFI_BACKUP_SRAM
@ -315,6 +334,9 @@ void logHardFault(uint32_t type, uintptr_t faultAddress, port_extctx* ctx, uint3
#if EFI_SIMULATOR || EFI_PROD_CODE
void chDbgPanic3(const char *msg, const char * file, int line) {
// following is allocated on stack
// add some marker
uint32_t tmp = 0xfffffa11;
#if EFI_PROD_CODE
// Attempt to break in to the debugger, if attached
if (CoreDebug->DHCSR & CoreDebug_DHCSR_C_DEBUGEN_Msk)
@ -331,6 +353,9 @@ void chDbgPanic3(const char *msg, const char * file, int line) {
err->line = line;
strlncpy(err->msg, msg, efi::size(err->msg));
err->Cookie = ErrorCookie::ChibiOsPanic;
// copy stack last as it can be corrupted and cause another exeption
uint32_t *sp = &tmp;
errorHandlerSaveStack(err, sp);
}
#endif // EFI_BACKUP_SRAM

View File

@ -89,6 +89,9 @@ enum class ErrorCookie : uint32_t {
const char *errorCookieToName(ErrorCookie cookie);
// Error handling/recovery/reporting information
#define ERROR_STACK_DEPTH 32
typedef struct {
ErrorCookie Cookie;
@ -99,6 +102,8 @@ typedef struct {
uint32_t FaultType;
uint32_t FaultAddress;
uint32_t Csfr;
uint32_t sp;
uint32_t stack[ERROR_STACK_DEPTH];
} backupErrorState;
// reads backup ram and checks for any error report