From cfa26c2a2da3f672cada8b533ffefa6cb516266e Mon Sep 17 00:00:00 2001 From: Wolfvak Date: Wed, 16 Aug 2017 19:43:08 -0300 Subject: [PATCH] Misc exception handlers fixes Biggest one is they actually work now --- Makefile | 4 +- source/start.s | 4 +- source/{common => system}/bootfirm.h | 0 source/{common => system}/bootfirm.s | 0 source/{common => system}/cache.h | 0 source/{common => system}/cache.s | 0 source/{common => system}/xrq.c | 51 +++++++++++++------------ source/{common => system}/xrq_handler.s | 18 ++++----- 8 files changed, 38 insertions(+), 39 deletions(-) rename source/{common => system}/bootfirm.h (100%) rename source/{common => system}/bootfirm.s (100%) rename source/{common => system}/cache.h (100%) rename source/{common => system}/cache.s (100%) rename source/{common => system}/xrq.c (64%) rename source/{common => system}/xrq_handler.s (79%) diff --git a/Makefile b/Makefile index 2327d5b38..c5035a853 100644 --- a/Makefile +++ b/Makefile @@ -21,9 +21,9 @@ ifeq ($(SAFEMODE),1) export TARGET := SafeMode9 endif BUILD := build -SOURCES := source source/common source/filesys source/crypto source/fatfs source/nand source/virtual source/game source/gamecart source/quicklz +SOURCES := source source/common source/filesys source/crypto source/fatfs source/nand source/virtual source/game source/gamecart source/quicklz source/system DATA := data -INCLUDES := common source source/common source/font source/filesys source/crypto source/fatfs source/nand source/virtual source/game source/gamecart source/quicklz +INCLUDES := common source source/common source/font source/filesys source/crypto source/fatfs source/nand source/virtual source/game source/gamecart source/quicklz source/system #--------------------------------------------------------------------------------- # options for code generation diff --git a/source/start.s b/source/start.s index 05137e34f..381869ec0 100644 --- a/source/start.s +++ b/source/start.s @@ -9,7 +9,7 @@ .global _start _start: @ Switch to supervisor mode and disable interrupts - msr cpsr_c, #(SR_SVC_MODE | SR_IRQ | SR_FIQ) + msr cpsr_c, #(SR_SYS_MODE | SR_IRQ | SR_FIQ) @ Short delay (not always necessary, just in case) mov r3, #0x40000 @@ -136,7 +136,7 @@ _start_gm: bl main __mpu_regions: - .word 0xFFFF001F @ ffff0000 64k | bootrom (unprotected / protected) + .word 0xFFFF001F @ FFFF0000 64k | bootrom (unprotected / protected) .word 0x3000801B @ 30008000 16k | dtcm .word 0x00000035 @ 00000000 128M | itcm .word 0x08000029 @ 08000000 2M | arm9 mem (O3DS / N3DS) diff --git a/source/common/bootfirm.h b/source/system/bootfirm.h similarity index 100% rename from source/common/bootfirm.h rename to source/system/bootfirm.h diff --git a/source/common/bootfirm.s b/source/system/bootfirm.s similarity index 100% rename from source/common/bootfirm.s rename to source/system/bootfirm.s diff --git a/source/common/cache.h b/source/system/cache.h similarity index 100% rename from source/common/cache.h rename to source/system/cache.h diff --git a/source/common/cache.s b/source/system/cache.s similarity index 100% rename from source/common/cache.s rename to source/system/cache.s diff --git a/source/common/xrq.c b/source/system/xrq.c similarity index 64% rename from source/common/xrq.c rename to source/system/xrq.c index d768c6376..d32e336cb 100644 --- a/source/common/xrq.c +++ b/source/system/xrq.c @@ -10,8 +10,8 @@ #include -/* Code will be dumped from PC-PC_DUMPRAD to PC+PC_DUMPRAD */ -#define PC_DUMPRAD (0x20) +#define PC_DUMPRAD (0x30) +#define SP_DUMPLEN (0x60) #define XRQ_DUMPDATAFUNC(type, size) \ int XRQ_DumpData_##type(char *b, u32 s, u32 e) \ @@ -33,10 +33,9 @@ XRQ_DUMPDATAFUNC(u16, 4) XRQ_DUMPDATAFUNC(u32, 8) -// Last 3 should never happen const char *XRQ_Name[] = { "Reset", "Undefined", "SWI", "Prefetch Abort", - "Data Abort", "", "", "" + "Data Abort", "Reserved", "IRQ", "FIQ" }; extern char __stack_top; @@ -45,43 +44,44 @@ void XRQ_DumpRegisters(u32 xrq, u32 *regs) { int y; u32 sp, st, pc; - char *wstr = (char*)TEMP_BUFFER, *dumpstr = wstr; + char dumpstr[2048], *wstr = dumpstr; ClearScreen(MAIN_SCREEN, COLOR_BLACK); - wstr += sprintf(wstr, "Exception: %s (%lu)\n", XRQ_Name[xrq&7], xrq); + /* Print registers on screen */ + wstr += sprintf(wstr, "Exception: %s (%lu)\n", XRQ_Name[xrq&7], xrq); for (int i = 0; i < 8; i++) { int i_ = i*2; wstr += sprintf(wstr, "R%02d: %08lX | R%02d: %08lX\n", i_, regs[i_], i_+1, regs[i_+1]); } - wstr += sprintf(wstr, "CPSR: %08lX\n", regs[16]); - DrawStringF(MAIN_SCREEN, 10, 0, COLOR_WHITE, COLOR_BLACK, - dumpstr); + DrawStringF(MAIN_SCREEN, 10, 0, COLOR_WHITE, COLOR_BLACK, dumpstr); + + /* Reinitialize SD */ y = GetDrawStringHeight(dumpstr); - DrawStringF(MAIN_SCREEN, 10, y, COLOR_WHITE, COLOR_BLACK, - "Reinitializing SD subsystem..."); + DrawString(MAIN_SCREEN, "Reinitializing SD subsystem...", 10, y, + COLOR_WHITE, COLOR_BLACK); y+=FONT_HEIGHT_EXT; while(!InitSDCardFS()) { DeinitSDCardFS(); } - - DrawStringF(MAIN_SCREEN, 10, y, COLOR_WHITE, COLOR_BLACK, - "Done"); + DrawString(MAIN_SCREEN, "Done", 10, y, COLOR_WHITE, COLOR_BLACK); y+=FONT_HEIGHT_EXT; + + /* Dump STACK */ sp = regs[13] & ~0xF; st = (u32)&__stack_top; - if (sp >= 0x20000000 && sp <= st) { - wstr += sprintf(wstr, "\nStack dump:\n\n"); - wstr += XRQ_DumpData_u8(wstr, sp, st); - } + wstr += sprintf(wstr, "\nStack dump:\n\n"); + wstr += XRQ_DumpData_u8(wstr, sp, min(sp+SP_DUMPLEN, st)); + - pc = regs[15]; + /* Dump TEXT */ + pc = regs[15] & ~0xF; wstr += sprintf(wstr, "\nCode dump:\n\n"); if (regs[16] & SR_THUMB) { wstr += XRQ_DumpData_u16(wstr, pc-PC_DUMPRAD, pc+PC_DUMPRAD); @@ -89,12 +89,15 @@ void XRQ_DumpRegisters(u32 xrq, u32 *regs) wstr += XRQ_DumpData_u32(wstr, pc-PC_DUMPRAD, pc+PC_DUMPRAD); } - DrawStringF(MAIN_SCREEN, 10, y, COLOR_WHITE, COLOR_BLACK, - "Dumping state to SD..."); + + /* Dump to SD */ + DrawString(MAIN_SCREEN, "Dumping state to SD...", 10, y, + COLOR_WHITE, COLOR_BLACK); y+=FONT_HEIGHT_EXT; - FileSetData(OUTPUT_PATH"/dump.txt", dumpstr, wstr - dumpstr, 0, true); - DrawStringF(MAIN_SCREEN, 10, y, COLOR_WHITE, COLOR_BLACK, - "Done"); + FileSetData(OUTPUT_PATH"/exception_dump.txt", dumpstr, wstr - dumpstr, 0, true); + DrawString(MAIN_SCREEN, "Done", 10, y, COLOR_WHITE, COLOR_BLACK); + + DeinitSDCardFS(); return; } diff --git a/source/common/xrq_handler.s b/source/system/xrq_handler.s similarity index 79% rename from source/common/xrq_handler.s rename to source/system/xrq_handler.s index b6c9b5640..9c33c2772 100644 --- a/source/common/xrq_handler.s +++ b/source/system/xrq_handler.s @@ -32,7 +32,6 @@ XRQ_Registers: .space (17*4) XRQ_Reset: - msr cpsr_c, #(SR_ABT_MODE | SR_IRQ | SR_FIQ) XRQ_FATAL 0 XRQ_Undefined: @@ -52,10 +51,6 @@ XRQ_MainHandler: mrs r9, spsr mov r8, lr - @ Disable interrupts - orr r0, r0, #(SR_IRQ | SR_FIQ) - msr cpsr, r0 - @ Disable mpu / caches ldr r4, =BRF_WB_INV_DCACHE ldr r5, =BRF_INVALIDATE_ICACHE @@ -67,23 +62,23 @@ XRQ_MainHandler: @ Retrieve banked registers and r0, r9, #(SR_PMODE_MASK) cmp r0, #(SR_USR_MODE) - orreq r0, r9, #(SR_SYS_MODE) + orreq r0, #(SR_SYS_MODE) orr r0, #(SR_IRQ | SR_FIQ) - msr cpsr_c, r0 @ Switch to previous mode + msr cpsr_c, r0 @ Switch to previous mode mov r0, sp mov r1, lr - msr cpsr, r10 @ Return to abort + msr cpsr_c, r10 @ Return to abort stmia sp!, {r0,r1,r8,r9} - ldr sp, =__stack_abt - ldr r2, =XRQ_DumpRegisters + ldr sp, =0x8000 + ldr r2, =XRQ_DumpRegisters @ void XRQ_DumpRegisters(u32 xrq_id, u32 *regs) adr r1, XRQ_Registers mov r0, r11 blx r2 - msr cpsr_c, #(SR_SVC_MODE | SR_IRQ | SR_FIQ) + msr cpsr, #(SR_SVC_MODE | SR_IRQ | SR_FIQ) mov r0, #0 .LXRQ_WFI: mcr p15, 0, r0, c7, c0, 4 @@ -93,3 +88,4 @@ XRQ_MainHandler: .global XRQ_End XRQ_End: + .word 0