From e478908dca353dbd3e89a6bec8b69aa6392af8d8 Mon Sep 17 00:00:00 2001 From: TuxSH Date: Tue, 7 Jun 2016 19:25:45 +0200 Subject: [PATCH] Make the exception handlers dump more information You'll need to the latest version of exception_dump_parser.py --- exceptions/arm11/source/handlers.s | 18 ++++++++-- exceptions/arm11/source/mainHandler.c | 48 +++++++++++++++++---------- exceptions/arm9/source/handlers.s | 11 ++++-- exceptions/arm9/source/mainHandler.c | 22 +++++------- exceptions/exception_dump_parser.py | 26 ++++++++++----- source/exceptions.c | 29 +++++++++++----- source/exceptions.h | 2 +- source/firm.c | 8 ++--- source/patches.c | 8 ++++- source/patches.h | 2 +- 10 files changed, 115 insertions(+), 59 deletions(-) diff --git a/exceptions/arm11/source/handlers.s b/exceptions/arm11/source/handlers.s index 61e9e23..20fadf8 100644 --- a/exceptions/arm11/source/handlers.s +++ b/exceptions/arm11/source/handlers.s @@ -38,7 +38,7 @@ _commonHandler: stmfd r6!, {r8-lr} msr cpsr_c, r3 @ restore processor mode mov sp, r6 - vmrs r3, fpexc + fmrx r3, fpexc cmp r1, #1 bne noFPUInit @@ -66,9 +66,21 @@ _commonHandler: rfefd sp! noFPUInit: - stmfd sp!, {r2,lr} @ it's a bit of a mess, but we will fix that later - @ order of saved regs now: cpsr, pc + (2/4/8), r8-r14, r0-r7 + stmfd sp!, {r2,lr} + mrc p15,0,r4,c5,c0,0 @ dfsr + mrc p15,0,r5,c5,c0,1 @ ifsr + mrc p15,0,r6,c6,c0,0 @ far + mov r7, r3 + fmrx r8, fpinst + fmrx r9, fpinst2 + + stmfd sp!, {r4-r9} @ it's a bit of a mess, but we will fix that later + @ order of saved regs now: dfsr, ifsr, far, fpexc, fpinst, fpinst2, cpsr, pc + (2/4/8), r8-r14, r0-r7 + + bic r3, #(1<<31) + fmxr fpexc, r3 @ clear the VFP11 exception flag (if it's set) + mov r0, sp mrc p15,0,r2,c0,c0,5 @ CPU ID register diff --git a/exceptions/arm11/source/mainHandler.c b/exceptions/arm11/source/mainHandler.c index 71ca073..c2d753b 100644 --- a/exceptions/arm11/source/mainHandler.c +++ b/exceptions/arm11/source/mainHandler.c @@ -7,14 +7,16 @@ #include "types.h" #include "handlers.h" +#define NULL 0 #define FINAL_BUFFER 0xE5000000 //0x25000000 -#define REG_DUMP_SIZE (4*18) +#define REG_DUMP_SIZE (4*23) #define CODE_DUMP_SIZE 48 -#define OTHER_DATA_SIZE 0 -void __attribute__((noreturn)) mainHandler(u32 regs[18], u32 type, u32 cpuId, u32 fpexc) +#define CODESET_OFFSET 0xBEEFBEEF + +void __attribute__((noreturn)) mainHandler(u32 regs[REG_DUMP_SIZE / 4], u32 type, u32 cpuId) { u32 dump[(40 + REG_DUMP_SIZE + CODE_DUMP_SIZE) / 4]; vu32 *final = (vu32 *)FINAL_BUFFER; @@ -23,32 +25,24 @@ void __attribute__((noreturn)) mainHandler(u32 regs[18], u32 type, u32 cpuId, u3 dump[0] = 0xDEADC0DE; //Magic dump[1] = 0xDEADCAFE; //Magic - dump[2] = (1 << 16) | 0; //Dump format version number + dump[2] = (1 << 16) | 1; //Dump format version number dump[3] = ((cpuId & 0xf) << 16) | 11; //Processor dump[4] = type; //Exception type dump[6] = REG_DUMP_SIZE; //Register dump size (r0-r12, sp, lr, pc, cpsr, fpexc) dump[7] = CODE_DUMP_SIZE; //Code dump size (10 ARM instructions, up to 20 Thumb instructions). - dump[9] = OTHER_DATA_SIZE; //Other data size //Dump registers - //Current order of saved regs: cpsr, pc, r8-r12, sp, lr, r0-r7 + //Current order of saved regs: dfsr, ifsr, far, fpexc, fpinst, fpinst2, cpsr, pc, r8-r12, sp, lr, r0-r7 u32 *regdump = dump + 10; - u32 cpsr = regs[0]; - u32 pc = regs[1] - ((type < 3) ? (((cpsr & 0x20) != 0 && type == 1) ? 2 : 4) : 8); + u32 cpsr = regs[6]; + u32 pc = regs[7] - ((type < 3) ? (((cpsr & 0x20) != 0 && type == 1) ? 2 : 4) : 8); regdump[15] = pc; regdump[16] = cpsr; - regdump[17] = fpexc; - - for(u32 i = 0; i < 7; i++) - regdump[8 + i] = regs[2 + i]; - - for(u32 i = 0; i < 8; i++) - regdump[i] = regs[9 + i]; - - dump[8] = 0x1000 - (regdump[13] & 0xfff); //Stack dump size (max. 0x1000 bytes) - dump[5] = 40 + REG_DUMP_SIZE + CODE_DUMP_SIZE + dump[8] + OTHER_DATA_SIZE; //Total size + for(u32 i = 0; i < 6; i++) regdump[17 + i] = regs[i]; + for(u32 i = 0; i < 7; i++) regdump[8 + i] = regs[8 + i]; + for(u32 i = 0; i < 8; i++) regdump[i] = regs[15 + i]; //Dump code u8 *codedump = (u8 *)regdump + dump[6]; @@ -57,11 +51,29 @@ void __attribute__((noreturn)) mainHandler(u32 regs[18], u32 type, u32 cpuId, u3 codedump[i] = instr[i]; //Dump stack in place + dump[8] = 0x1000 - (regdump[13] & 0xfff); //Stack dump size (max. 0x1000 bytes) vu32 *sp = (vu32 *)regdump[13]; vu32 *stackdump = (vu32 *)((vu8 *)FINAL_BUFFER + 40 + REG_DUMP_SIZE + CODE_DUMP_SIZE); for(u32 i = 0; i < dump[8] / 4; i++) stackdump[i] = sp[i]; + vu8 *currentKProcess = *(vu8 **)0xFFFF9004; + vu8 *currentKCodeSet = (currentKProcess != NULL) ? *(vu8 **)(currentKProcess + CODESET_OFFSET) : NULL; + if(currentKCodeSet != NULL) + { + dump[9] = 16; //Additional data size + vu32 *additionalData = (vu32 *)((vu8 *)FINAL_BUFFER + 40 + REG_DUMP_SIZE + CODE_DUMP_SIZE + dump[8]); + + additionalData[0] = *(vu32 *)(currentKCodeSet + 0x50); //Process name + additionalData[1] = *(vu32 *)(currentKCodeSet + 0x54); + + additionalData[2] = *(vu32 *)(currentKCodeSet + 0x5C); //Title ID + additionalData[3] = *(vu32 *)(currentKCodeSet + 0x60); + } + else + dump[9] = 0; + + dump[5] = 40 + REG_DUMP_SIZE + CODE_DUMP_SIZE + dump[8] + dump[9]; //Total size for(u32 i = 0; i < (40 + REG_DUMP_SIZE + CODE_DUMP_SIZE) / 4; i++) final[i] = dump[i]; diff --git a/exceptions/arm9/source/handlers.s b/exceptions/arm9/source/handlers.s index a59086b..a33fe1b 100644 --- a/exceptions/arm9/source/handlers.s +++ b/exceptions/arm9/source/handlers.s @@ -39,8 +39,15 @@ _commonHandler: msr cpsr_c, r3 @ restore processor mode mov sp, r6 - stmfd sp!, {r2,lr} @ it's a bit of a mess, but we will fix that later - @ order of saved regs now: cpsr, pc + (2/4/8), r8-r14, r0-r7 + stmfd sp!, {r2,lr} + + mrc p15,0,r3,c5,c0,0 @ dfsr + mrc p15,0,r4,c5,c0,1 @ ifsr + mrc p15,0,r5,c6,c0,0 @ far + + stmfd sp!, {r3-r5} @ it's a bit of a mess, but we will fix that later + @ order of saved regs now: dfsr, ifsr, far, cpsr, pc + (2/4/8), r8-r14, r0-r7 + mov r0, sp b mainHandler diff --git a/exceptions/arm9/source/mainHandler.c b/exceptions/arm9/source/mainHandler.c index d6e35ad..d216f31 100644 --- a/exceptions/arm9/source/mainHandler.c +++ b/exceptions/arm9/source/mainHandler.c @@ -11,18 +11,17 @@ #define FINAL_BUFFER 0x25000000 -#define REG_DUMP_SIZE (4*17) +#define REG_DUMP_SIZE (4*20) #define CODE_DUMP_SIZE 48 #define OTHER_DATA_SIZE 0 -void __attribute__((noreturn)) mainHandler(u32 regs[17], u32 type) +void __attribute__((noreturn)) mainHandler(u32 regs[REG_DUMP_SIZE / 4], u32 type) { - //vu32 *dump = (u32 *)TEMP_BUFFER; u32 dump[(40 + REG_DUMP_SIZE + CODE_DUMP_SIZE) / 4]; dump[0] = 0xDEADC0DE; //Magic dump[1] = 0xDEADCAFE; //Magic - dump[2] = (1 << 16) | 0; //Dump format version number + dump[2] = (1 << 16) | 1; //Dump format version number dump[3] = 9; //Processor dump[4] = type; //Exception type dump[6] = REG_DUMP_SIZE; //Register dump size (r0-r12, sp, lr, pc, cpsr) @@ -30,20 +29,17 @@ void __attribute__((noreturn)) mainHandler(u32 regs[17], u32 type) dump[9] = OTHER_DATA_SIZE; //Other data size //Dump registers - //Current order of saved regs: cpsr, pc, r8-r12, sp, lr, r0-r7 + //Current order of saved regs: dfsr, ifsr, far, cpsr, pc, r8-r14, r0-r7 vu32 *regdump = dump + 10; - u32 cpsr = regs[0]; - u32 pc = regs[1] - ((type < 3) ? (((cpsr & 0x20) != 0 && type == 1) ? 2 : 4) : 8); + u32 cpsr = regs[3]; + u32 pc = regs[4] - ((type < 3) ? (((cpsr & 0x20) != 0 && type == 1) ? 2 : 4) : 8); regdump[15] = pc; regdump[16] = cpsr; - - for(u32 i = 0; i < 7; i++) - regdump[8 + i] = regs[2 + i]; - - for(u32 i = 0; i < 8; i++) - regdump[i] = regs[9 + i]; + for(u32 i = 0; i < 3; i++) regdump[17 + i] = regs[i]; + for(u32 i = 0; i < 7; i++) regdump[8 + i] = regs[5 + i]; + for(u32 i = 0; i < 8; i++) regdump[i] = regs[12 + i]; dump[8] = 0x1000 - (regdump[13] & 0xfff); //Stack dump size (max. 0x1000 bytes) dump[5] = 40 + REG_DUMP_SIZE + CODE_DUMP_SIZE + dump[8] + OTHER_DATA_SIZE; //Total size diff --git a/exceptions/exception_dump_parser.py b/exceptions/exception_dump_parser.py index 1fb91b2..e5a42bc 100644 --- a/exceptions/exception_dump_parser.py +++ b/exceptions/exception_dump_parser.py @@ -67,7 +67,7 @@ def makeRegisterLine(A, rA, B, rB): return "{0:<15}{1:<20}{2:<15}{3:<20}".format(A, "{0:08x}".format(rA), B, "{0:08x}".format(rB)) handledExceptionNames = ("FIQ", "undefined instruction", "prefetch abort", "data abort") -registerNames = tuple("r{0}".format(i) for i in range(13)) + ("sp", "lr", "pc", "cpsr", "fpexc") +registerNames = tuple("r{0}".format(i) for i in range(13)) + ("sp", "lr", "pc", "cpsr") + ("dfsr", "ifsr", "far") + ("fpexc", "fpinst", "fpinst2") svcBreakReasons = ("(svcBreak: panic)", "(svcBreak: assertion failed)", "(svcBreak: user-related)") if __name__ == "__main__": @@ -79,27 +79,37 @@ if __name__ == "__main__": if unpack_from("<2I", data) != (0xdeadc0de, 0xdeadcafe): raise SystemExit("Invalid file format") - processor, exceptionType, _, nbRegisters, codeDumpSize, stackDumpSize = unpack_from("<6I", data, 12) + version, processor, exceptionType, _, nbRegisters, codeDumpSize, stackDumpSize, additionalDataSize = unpack_from("<8I", data, 8) nbRegisters //= 4 + if version < (1 << 16) | 1: + raise SystemExit("Incompatible format version, please use the appropriate parser.") + registers = unpack_from("<{0}I".format(nbRegisters), data, 40) codeDump = data[40 + 4 * nbRegisters : 40 + 4 * nbRegisters + codeDumpSize] stackOffset = 40 + 4 * nbRegisters + codeDumpSize stackDump = data[stackOffset : stackOffset + stackDumpSize] + addtionalDataOffset = stackOffset + stackDumpSize + additionalData = data[addtionalDataOffset : addtionalDataOffset + additionalDataSize] if processor == 9: print("Processor: ARM9") else: print("Processor: ARM11 (core {0})".format(processor >> 16)) - svcBreakStr = "" + typeDetailsStr = "" if exceptionType == 2 and (registers[16] & 0x20) == 0 and unpack_from("= len(handledExceptionNames) else handledExceptionNames[exceptionType], svcBreakStr)) + typeDetailsStr = " " + (svcBreakReasons[registers[0]] if registers[0] < 3 else "(svcBreak)") + elif processor != 9 and (registers[20] & 0x80000000) != 0: + typeDetailsStr = " (VFP exception)" + + print("Exception type: {0}{1}".format("unknown" if exceptionType >= len(handledExceptionNames) else handledExceptionNames[exceptionType], typeDetailsStr)) + if additionalDataSize != 0: + print("Current process: {0} ({1:016x})".format(additionalData[:8].decode("ascii"), unpack_from("> 2) - 2) & 0xFFFFFF)) #define MAKE_BRANCH_LINK(src,dst) (0xEB000000 | ((u32)((((u8 *)(dst) - (u8 *)(src)) >> 2) - 2) & 0xFFFFFF)) -void installArm11Handlers(u32 *exceptionsPage, u32 stackAddr) +void installArm11Handlers(u32 *exceptionsPage, u32 stackAddr, u32 codeSetOffset) { u32 *initFPU; for(initFPU = exceptionsPage; initFPU < (exceptionsPage + 0x400) && (initFPU[0] != 0xE59F0008 || initFPU[1] != 0xE5900000); initFPU += 1); @@ -59,6 +59,7 @@ void installArm11Handlers(u32 *exceptionsPage, u32 stackAddr) case 0xEBFFFFFE: *pos = MAKE_BRANCH_LINK(pos, initFPU); break; case 0xEAFFFFFE: *pos = MAKE_BRANCH(pos, mcuReboot); break; case 0xE12FFF1C: pos[1] = 0xFFFF0000 + 4 * (u32)(freeSpace - exceptionsPage) + pos[1] - 32; break; // bx r12 (mainHandler) + case 0xBEEFBEEF: *pos = codeSetOffset; default: break; } } @@ -117,17 +118,26 @@ void detectAndProcessExceptionDumps(void) } - char arm11Str[] = "Processor: ARM11 (core X)"; - if((dump[3] & 0xFFFF) == 11) arm11Str[28] = '0' + (char)(dump[3] >> 16); + char arm11Str[] = "Processor: ARM11 (core X)"; + if((dump[3] & 0xFFFF) == 11) arm11Str[29] = '0' + (char)(dump[3] >> 16); initScreens(); drawString("An exception occurred", 10, 10, COLOR_RED); - int posY = drawString(((dump[3] & 0xFFFF) == 11) ? arm11Str : "Processor: ARM9", 10, 30, COLOR_WHITE) + SPACING_Y; - posY = drawString("Exception type: ", 10, posY, COLOR_WHITE); - posY = drawString(handledExceptionNames[dump[4]], 10 + 16 * SPACING_X, posY, COLOR_WHITE); + int posY = drawString(((dump[3] & 0xFFFF) == 11) ? arm11Str : "Processor: ARM9", 10, 30, COLOR_WHITE) + SPACING_Y; + + posY = drawString("Exception type: ", 10, posY, COLOR_WHITE); + posY = drawString(handledExceptionNames[dump[4]], 10 + 17 * SPACING_X, posY, COLOR_WHITE); if(dump[4] == 2 && dump[7] >= 4 && (dump[10 + 16] & 0x20) == 0 && *(vu32 *)((vu8 *)dump + 40 + dump[6] + dump[7] - 4) == 0xE12FFF7F) - posY = drawString("(svcBreak)", 10 + 31 * SPACING_X, posY, COLOR_WHITE); + posY = drawString("(svcBreak)", 10 + 32 * SPACING_X, posY, COLOR_WHITE); + + if((dump[3] & 0xFFFF) == 11 && dump[9] != 0) + { + posY += SPACING_Y; + char processNameStr[] = "Current process: --------"; + memcpy(processNameStr + 17, (char *)(dump + ((dump[5] - dump[9]) / 4)), 8); + posY = drawString(processNameStr, 10, posY, COLOR_WHITE); + } posY += 3 * SPACING_Y; for(u32 i = 0; i < 17; i += 2) @@ -139,7 +149,7 @@ void detectAndProcessExceptionDumps(void) if(dump[3] != 9 || i != 16) { posY = drawString(registerNames[i + 1], 10 + 22 * SPACING_X, posY, COLOR_WHITE); - hexItoa(dump[10 + i + 1], hexstring); + hexItoa((i == 16) ? dump[10 + 20] : dump[10 + i + 1], hexstring); posY = drawString(hexstring, 10 + 29 * SPACING_X, posY, COLOR_WHITE); } @@ -150,7 +160,10 @@ void detectAndProcessExceptionDumps(void) u32 mode = dump[10 + 16] & 0xF; if(dump[4] == 3 && (mode == 7 || mode == 11)) + { posY = drawString("Incorrect dump: failed to dump code and/or stack", 10, posY, 0x00FFFF) + 2 * SPACING_Y; //in yellow + if(dump[3] != 9) posY -= SPACING_Y; + } posY = drawString("You can find a dump in the following file:", 10, posY, COLOR_WHITE) + SPACING_Y; posY = drawString((dump[3] == 9) ? path9 : path11, 10, posY, COLOR_WHITE) + 2 * SPACING_Y; diff --git a/source/exceptions.h b/source/exceptions.h index 577696c..d48408a 100644 --- a/source/exceptions.h +++ b/source/exceptions.h @@ -8,5 +8,5 @@ #include "types.h" void installArm9Handlers(void); -void installArm11Handlers(u32 *exceptionsPage, u32 stackAddr); +void installArm11Handlers(u32 *exceptionsPage, u32 stackAddr, u32 codeSetOffset); void detectAndProcessExceptionDumps(void); \ No newline at end of file diff --git a/source/firm.c b/source/firm.c index aadc7c3..f078a61 100755 --- a/source/firm.c +++ b/source/firm.c @@ -326,15 +326,15 @@ static inline void patchNativeFirm(u32 nandType, u32 emuHeader, u32 a9lhMode) if(DEVMODE == 2) patchUnitInfoValueSet(arm9Section, section[2].size); //Install arm11 exception handlers - u32 stackAddress; - u32 *exceptionsPage = getInfoForArm11ExceptionHandlers(arm11Section1, section[1].size, &stackAddress); - installArm11Handlers(exceptionsPage, stackAddress); + u32 stackAddress, codeSetOffset; + u32 *exceptionsPage = getInfoForArm11ExceptionHandlers(arm11Section1, section[1].size, &stackAddress, &codeSetOffset); + installArm11Handlers(exceptionsPage, stackAddress, codeSetOffset); //Kernel9/Process9 debugging patchExceptionHandlersInstall(arm9Section, section[2].size); patchSvcBreak9(arm9Section, section[2].size, (u32)(section[2].address)); - //Stub svcBreak11 with "bkpt 255" + //Stub svcBreak11 with "bkpt 65535" patchSvcBreak11(arm11Section1, section[1].size); //Make FCRAM (and VRAM as a side effect) globally executable from arm11 kernel diff --git a/source/patches.c b/source/patches.c index f5059bd..381755c 100644 --- a/source/patches.c +++ b/source/patches.c @@ -34,14 +34,20 @@ u8 *getProcess9(u8 *pos, u32 size, u32 *process9Size, u32 *process9MemAddr) return off - 0x204 + (*(u32 *)(off - 0x64) * 0x200) + 0x200; } -u32* getInfoForArm11ExceptionHandlers(u8 *pos, u32 size, u32 *stackAddr) +u32* getInfoForArm11ExceptionHandlers(u8 *pos, u32 size, u32 *stackAddr, u32 *codeSetOffset) { //This function has to succeed. Crash if it doesn't (we'll get an exception dump of it anyways) const u8 callExceptionDispatcherPattern[] = {0x0F, 0x00, 0xBD, 0xE8, 0x13, 0x00, 0x02, 0xF1}; + const u8 getTitleIDFromCodeSetPattern[] = {0xDC, 0x05, 0xC0, 0xE1, 0x20, 0x04, 0xA0, 0xE1}; *stackAddr = *((u32 *)memsearch(pos, callExceptionDispatcherPattern, size, 8) + 3); + u32 *loadCodeSet = (u32 *)memsearch(pos, getTitleIDFromCodeSetPattern, size, 8); + while((*loadCodeSet >> 20) != 0xE59 || ((*loadCodeSet >> 12) & 0xF) != 0) //ldr r0, [rX, #offset] + loadCodeSet--; + *codeSetOffset = *loadCodeSet & 0xFFF; + findArm11ExceptionsPageAndSvcTable(pos, size); return arm11ExceptionsPage; } diff --git a/source/patches.h b/source/patches.h index 3b57645..3b53364 100644 --- a/source/patches.h +++ b/source/patches.h @@ -16,7 +16,7 @@ typedef struct patchData { } patchData; u8 *getProcess9(u8 *pos, u32 size, u32 *process9Size, u32 *process9MemAddr); -u32* getInfoForArm11ExceptionHandlers(u8 *pos, u32 size, u32 *stackAddr); +u32* getInfoForArm11ExceptionHandlers(u8 *pos, u32 size, u32 *stackAddr, u32 *codeSetOffset); void patchSignatureChecks(u8 *pos, u32 size); void patchTitleInstallMinVersionCheck(u8 *pos, u32 size); void patchFirmlaunches(u8 *pos, u32 size, u32 process9MemAddr);