diff --git a/exceptions/arm11/source/handlers.h b/exceptions/arm11/source/handlers.h index f0875f5..cc21966 100644 --- a/exceptions/arm11/source/handlers.h +++ b/exceptions/arm11/source/handlers.h @@ -7,10 +7,28 @@ #pragma once +#include "types.h" +#define NULL 0 + void __attribute__((noreturn)) mcuReboot(void); void clearDCacheAndDMB(void); void FIQHandler(void); void undefinedInstructionHandler(void); void dataAbortHandler(void); -void prefetchAbortHandler(void); \ No newline at end of file +void prefetchAbortHandler(void); + +typedef struct __attribute__((packed)) +{ + u32 magic[2]; + u16 versionMinor, versionMajor; + + u16 processor, core; + u32 type; + + u32 totalSize; + u32 registerDumpSize; + u32 codeDumpSize; + u32 stackDumpSize; + u32 additionalDataSize; +} ExceptionDumpHeader; \ No newline at end of file diff --git a/exceptions/arm11/source/mainHandler.c b/exceptions/arm11/source/mainHandler.c index c2d753b..6c2f35e 100644 --- a/exceptions/arm11/source/mainHandler.c +++ b/exceptions/arm11/source/mainHandler.c @@ -5,9 +5,7 @@ * This is part of Luma3DS, see LICENSE.txt for details */ -#include "types.h" #include "handlers.h" -#define NULL 0 #define FINAL_BUFFER 0xE5000000 //0x25000000 @@ -18,51 +16,66 @@ void __attribute__((noreturn)) mainHandler(u32 regs[REG_DUMP_SIZE / 4], u32 type, u32 cpuId) { - u32 dump[(40 + REG_DUMP_SIZE + CODE_DUMP_SIZE) / 4]; + ExceptionDumpHeader dumpHeader; + + u32 registerDump[REG_DUMP_SIZE / 4]; + u8 codeDump[CODE_DUMP_SIZE]; vu32 *final = (vu32 *)FINAL_BUFFER; - while(final[0] == 0xDEADC0DE && final[1] == 0xDEADCAFE && ((final[3] & 0xFFFF) == 9 || (final[3] & 0xFFFF) == 11)); + while(final[0] == 0xDEADC0DE && final[1] == 0xDEADCAFE && (final[3] == 9 || (final[3] & 0xFFFF) == 11)); - dump[0] = 0xDEADC0DE; //Magic - dump[1] = 0xDEADCAFE; //Magic - 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). + dumpHeader.magic[0] = 0xDEADC0DE; + dumpHeader.magic[1] = 0xDEADCAFE; + dumpHeader.versionMajor = 1; + dumpHeader.versionMinor = 1; + + dumpHeader.processor = 11; + dumpHeader.core = cpuId & 0xF; + dumpHeader.type = type; + + dumpHeader.registerDumpSize = REG_DUMP_SIZE; + dumpHeader.codeDumpSize = CODE_DUMP_SIZE; + dumpHeader.additionalDataSize = 0; //Dump registers //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[6]; u32 pc = regs[7] - ((type < 3) ? (((cpsr & 0x20) != 0 && type == 1) ? 2 : 4) : 8); - regdump[15] = pc; - regdump[16] = cpsr; - 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]; + registerDump[15] = pc; + registerDump[16] = cpsr; + for(u32 i = 0; i < 6; i++) registerDump[17 + i] = regs[i]; + for(u32 i = 0; i < 7; i++) registerDump[8 + i] = regs[8 + i]; + for(u32 i = 0; i < 8; i++) registerDump[i] = regs[15 + i]; + + dumpHeader.stackDumpSize = 0x1000 - (registerDump[13] & 0xfff); //Dump code - u8 *codedump = (u8 *)regdump + dump[6]; - vu8 *instr = (vu8 *)pc + ((cpsr & 0x20) ? 2 : 4) - dump[7]; //Doesn't work well on 32-bit Thumb instructions, but it isn't much of a problem - for(u32 i = 0; i < dump[7]; i++) - codedump[i] = instr[i]; - + vu8 *instr = (vu8 *)pc + ((cpsr & 0x20) ? 2 : 4) - dumpHeader.codeDumpSize; //Doesn't work well on 32-bit Thumb instructions, but it isn't much of a problem + for(u32 i = 0; i < dumpHeader.codeDumpSize; i++) + codeDump[i] = instr[i]; + + //Copy register dump and code dump + final = (vu32 *)(FINAL_BUFFER + sizeof(ExceptionDumpHeader)); + + for(u32 i = 0; i < dumpHeader.registerDumpSize / 4; i++) + *final++ = registerDump[i]; + + for(u32 i = 0; i < dumpHeader.codeDumpSize / 4; i++) + *final++ = *((u32 *)codeDump + 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]; + vu32 *sp = (vu32 *)registerDump[13]; + for(u32 i = 0; i < dumpHeader.stackDumpSize / 4; i++) + *final++ = 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]); + vu32 *additionalData = final; + dumpHeader.additionalDataSize = 16; additionalData[0] = *(vu32 *)(currentKCodeSet + 0x50); //Process name additionalData[1] = *(vu32 *)(currentKCodeSet + 0x54); @@ -71,11 +84,13 @@ void __attribute__((noreturn)) mainHandler(u32 regs[REG_DUMP_SIZE / 4], u32 type 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]; + dumpHeader.additionalDataSize = 16; + + //Copy header (actually optimized by the compiler) + final = (vu32 *)FINAL_BUFFER; + dumpHeader.totalSize = sizeof(ExceptionDumpHeader) + dumpHeader.registerDumpSize + dumpHeader.codeDumpSize + dumpHeader.stackDumpSize + dumpHeader.additionalDataSize; + *(ExceptionDumpHeader *)final = dumpHeader; + clearDCacheAndDMB(); mcuReboot(); diff --git a/exceptions/arm9/source/handlers.h b/exceptions/arm9/source/handlers.h index 0e4a49f..f014c3c 100644 --- a/exceptions/arm9/source/handlers.h +++ b/exceptions/arm9/source/handlers.h @@ -6,8 +6,24 @@ */ #pragma once +#include "types.h" void FIQHandler(void); void undefinedInstructionHandler(void); void dataAbortHandler(void); -void prefetchAbortHandler(void); \ No newline at end of file +void prefetchAbortHandler(void); + +typedef struct __attribute__((packed)) +{ + u32 magic[2]; + u16 versionMinor, versionMajor; + + u16 processor, core; + u32 type; + + u32 totalSize; + u32 registerDumpSize; + u32 codeDumpSize; + u32 stackDumpSize; + u32 additionalDataSize; +} ExceptionDumpHeader; \ No newline at end of file diff --git a/exceptions/arm9/source/handlers.s b/exceptions/arm9/source/handlers.s index a33fe1b..3578524 100644 --- a/exceptions/arm9/source/handlers.s +++ b/exceptions/arm9/source/handlers.s @@ -39,14 +39,8 @@ _commonHandler: msr cpsr_c, r3 @ restore processor mode mov sp, r6 - 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 + 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 mov r0, sp b mainHandler diff --git a/exceptions/arm9/source/mainHandler.c b/exceptions/arm9/source/mainHandler.c index d216f31..bb37aeb 100644 --- a/exceptions/arm9/source/mainHandler.c +++ b/exceptions/arm9/source/mainHandler.c @@ -5,60 +5,67 @@ * This is part of Luma3DS, see LICENSE.txt for details */ -#include "types.h" #include "i2c.h" #include "handlers.h" #define FINAL_BUFFER 0x25000000 -#define REG_DUMP_SIZE (4*20) +#define REG_DUMP_SIZE (4*17) #define CODE_DUMP_SIZE 48 -#define OTHER_DATA_SIZE 0 void __attribute__((noreturn)) mainHandler(u32 regs[REG_DUMP_SIZE / 4], u32 type) { - u32 dump[(40 + REG_DUMP_SIZE + CODE_DUMP_SIZE) / 4]; + ExceptionDumpHeader dumpHeader; - dump[0] = 0xDEADC0DE; //Magic - dump[1] = 0xDEADCAFE; //Magic - 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) - dump[7] = CODE_DUMP_SIZE; //Code dump size (10 ARM instructions, up to 20 Thumb instructions). - dump[9] = OTHER_DATA_SIZE; //Other data size + u32 registerDump[REG_DUMP_SIZE / 4]; + u8 codeDump[CODE_DUMP_SIZE]; + + dumpHeader.magic[0] = 0xDEADC0DE; + dumpHeader.magic[1] = 0xDEADCAFE; + dumpHeader.versionMajor = 1; + dumpHeader.versionMinor = 1; + + dumpHeader.processor = 9; + dumpHeader.core = 0; + dumpHeader.type = type; + + dumpHeader.registerDumpSize = REG_DUMP_SIZE; + dumpHeader.codeDumpSize = CODE_DUMP_SIZE; + dumpHeader.additionalDataSize = 0; //Dump registers - //Current order of saved regs: dfsr, ifsr, far, cpsr, pc, r8-r14, r0-r7 - vu32 *regdump = dump + 10; + //Current order of saved regs: cpsr, pc, r8-r14, r0-r7 + 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); + registerDump[15] = pc; + registerDump[16] = cpsr; + for(u32 i = 0; i < 7; i++) registerDump[8 + i] = regs[2 + i]; + for(u32 i = 0; i < 8; i++) registerDump[i] = regs[9 + i]; - regdump[15] = pc; - regdump[16] = cpsr; - 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 + dumpHeader.stackDumpSize = 0x1000 - (registerDump[13] & 0xfff); + dumpHeader.totalSize = sizeof(ExceptionDumpHeader) + dumpHeader.registerDumpSize + dumpHeader.codeDumpSize + dumpHeader.stackDumpSize; //Dump code - u8 *codedump = (u8 *)regdump + dump[6]; - vu8 *instr = (vu8 *)pc + ((cpsr & 0x20) ? 2 : 4) - dump[7]; //Doesn't work well on 32-bit Thumb instructions, but it isn't much of a problem - for(u32 i = 0; i < dump[7]; i++) - codedump[i] = instr[i]; - - //Dump stack - 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 *instr = (vu8 *)pc + ((cpsr & 0x20) ? 2 : 4) - dumpHeader.codeDumpSize; //Doesn't work well on 32-bit Thumb instructions, but it isn't much of a problem + for(u32 i = 0; i < dumpHeader.codeDumpSize; i++) + codeDump[i] = instr[i]; + + //Copy header (actually optimized by the compiler), register dump and code dump vu32 *final = (vu32 *)FINAL_BUFFER; - for(u32 i = 0; i < (40 + REG_DUMP_SIZE + CODE_DUMP_SIZE) / 4; i++) - final[i] = dump[i]; + *(ExceptionDumpHeader *)final = dumpHeader; + final += sizeof(ExceptionDumpHeader) / 4; + + for(u32 i = 0; i < dumpHeader.registerDumpSize / 4; i++) + *final++ = registerDump[i]; + + for(u32 i = 0; i < dumpHeader.codeDumpSize / 4; i++) + *final++ = *((u32 *)codeDump + i); + + //Dump stack in place + vu32 *sp = (vu32 *)registerDump[13]; + for(u32 i = 0; i < dumpHeader.stackDumpSize / 4; i++) + *final++ = sp[i]; i2cWriteRegister(I2C_DEV_MCU, 0x20, 1 << 2); //Reboot while(1); diff --git a/source/exceptions.c b/source/exceptions.c index 4398295..b0c0d02 100644 --- a/source/exceptions.c +++ b/source/exceptions.c @@ -13,6 +13,21 @@ #include "../build/arm9_exceptions.h" #include "../build/arm11_exceptions.h" +typedef struct __attribute__((packed)) +{ + u32 magic[2]; + u16 versionMinor, versionMajor; + + u16 processor, core; + u32 type; + + u32 totalSize; + u32 registerDumpSize; + u32 codeDumpSize; + u32 stackDumpSize; + u32 additionalDataSize; +} ExceptionDumpHeader; + void installArm9Handlers(void) { void *payloadAddress = (void *)0x01FF8000; @@ -92,16 +107,19 @@ void detectAndProcessExceptionDumps(void) char hexstring[] = "00000000"; - vu32 *dump = (vu32 *)0x25000000; + volatile ExceptionDumpHeader *dumpHeader = (volatile ExceptionDumpHeader *)0x25000000; + vu32 *dump = (vu32 *)dumpHeader; + vu32 *regs = (vu32 *)((vu8 *)dumpHeader + sizeof(ExceptionDumpHeader)); + vu8 *additionalData = (vu8 *)dumpHeader + dumpHeader->totalSize - dumpHeader->additionalDataSize; - if(dump[0] == 0xDEADC0DE && dump[1] == 0xDEADCAFE && (dump[3] == 9 || (dump[3] & 0xFFFF) == 11)) + if(dumpHeader->magic[0] == 0xDEADC0DE && dumpHeader->magic[1] == 0xDEADCAFE && (dumpHeader->processor == 9 || dumpHeader->processor == 11)) { char path9[41] = "/luma/dumps/arm9"; char path11[42] = "/luma/dumps/arm11"; char fileName[] = "crash_dump_00000000.dmp"; - u32 size = dump[5]; + u32 size = dumpHeader->totalSize; - if(dump[3] == 9) + if(dumpHeader->processor == 9) { findDumpFile(path9, fileName); path9[16] = '/'; @@ -116,26 +134,26 @@ void detectAndProcessExceptionDumps(void) memcpy(&path11[18], fileName, sizeof(fileName)); fileWrite((void *)dump, path11, dump[5]); } - char arm11Str[] = "Processor: ARM11 (core X)"; - if((dump[3] & 0xFFFF) == 11) arm11Str[29] = '0' + (char)(dump[3] >> 16); + if(dumpHeader->processor == 11) arm11Str[29] = '0' + (char)(dumpHeader->core); 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; + int posY = drawString((dumpHeader->processor == 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(handledExceptionNames[dumpHeader->type], 10 + 17 * SPACING_X, posY, COLOR_WHITE); + if(dumpHeader->type == 2 && dumpHeader->registerDumpSize >= 4 && (regs[16] & 0x20) == 0 + && *(vu32 *)((vu8 *)dumpHeader + sizeof(ExceptionDumpHeader) + dumpHeader->registerDumpSize + dumpHeader->codeDumpSize - 4) == 0xE12FFF7F) posY = drawString("(svcBreak)", 10 + 32 * SPACING_X, posY, COLOR_WHITE); - if((dump[3] & 0xFFFF) == 11 && dump[9] != 0) + if(dumpHeader->processor == 11 && dumpHeader->additionalDataSize != 0) { posY += SPACING_Y; char processNameStr[] = "Current process: --------"; - memcpy(processNameStr + 17, (char *)(dump + ((dump[5] - dump[9]) / 4)), 8); + memcpy(processNameStr + 17, (char *)additionalData, 8); posY = drawString(processNameStr, 10, posY, COLOR_WHITE); } @@ -143,13 +161,13 @@ void detectAndProcessExceptionDumps(void) for(u32 i = 0; i < 17; i += 2) { posY = drawString(registerNames[i], 10, posY, COLOR_WHITE); - hexItoa(dump[10 + i], hexstring); + hexItoa(regs[i], hexstring); posY = drawString(hexstring, 10 + 7 * SPACING_X, posY, COLOR_WHITE); - if(dump[3] != 9 || i != 16) + if(dumpHeader->processor != 9 || i != 16) { posY = drawString(registerNames[i + 1], 10 + 22 * SPACING_X, posY, COLOR_WHITE); - hexItoa((i == 16) ? dump[10 + 20] : dump[10 + i + 1], hexstring); + hexItoa((i == 16) ? regs[20] : regs[i + 1], hexstring); posY = drawString(hexstring, 10 + 29 * SPACING_X, posY, COLOR_WHITE); } @@ -158,15 +176,15 @@ void detectAndProcessExceptionDumps(void) posY += 2 * SPACING_Y; - u32 mode = dump[10 + 16] & 0xF; - if(dump[4] == 3 && (mode == 7 || mode == 11)) + u32 mode = regs[16] & 0xF; + if(dumpHeader->type == 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; + if(dumpHeader->processor != 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; + posY = drawString((dumpHeader->processor == 9) ? path9 : path11, 10, posY, COLOR_WHITE) + 2 * SPACING_Y; drawString("Press any button to shutdown", 10, posY, COLOR_WHITE); waitInput();