Refactor the exception handling code

This commit is contained in:
TuxSH 2016-06-08 21:44:04 +02:00
parent e478908dca
commit ef60c8ea4c
6 changed files with 168 additions and 100 deletions

View File

@ -7,6 +7,9 @@
#pragma once #pragma once
#include "types.h"
#define NULL 0
void __attribute__((noreturn)) mcuReboot(void); void __attribute__((noreturn)) mcuReboot(void);
void clearDCacheAndDMB(void); void clearDCacheAndDMB(void);
@ -14,3 +17,18 @@ void FIQHandler(void);
void undefinedInstructionHandler(void); void undefinedInstructionHandler(void);
void dataAbortHandler(void); void dataAbortHandler(void);
void prefetchAbortHandler(void); 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;

View File

@ -5,9 +5,7 @@
* This is part of Luma3DS, see LICENSE.txt for details * This is part of Luma3DS, see LICENSE.txt for details
*/ */
#include "types.h"
#include "handlers.h" #include "handlers.h"
#define NULL 0
#define FINAL_BUFFER 0xE5000000 //0x25000000 #define FINAL_BUFFER 0xE5000000 //0x25000000
@ -18,51 +16,66 @@
void __attribute__((noreturn)) mainHandler(u32 regs[REG_DUMP_SIZE / 4], u32 type, u32 cpuId) 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; 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 dumpHeader.magic[0] = 0xDEADC0DE;
dump[1] = 0xDEADCAFE; //Magic dumpHeader.magic[1] = 0xDEADCAFE;
dump[2] = (1 << 16) | 1; //Dump format version number dumpHeader.versionMajor = 1;
dump[3] = ((cpuId & 0xf) << 16) | 11; //Processor dumpHeader.versionMinor = 1;
dump[4] = type; //Exception type
dump[6] = REG_DUMP_SIZE; //Register dump size (r0-r12, sp, lr, pc, cpsr, fpexc) dumpHeader.processor = 11;
dump[7] = CODE_DUMP_SIZE; //Code dump size (10 ARM instructions, up to 20 Thumb instructions). dumpHeader.core = cpuId & 0xF;
dumpHeader.type = type;
dumpHeader.registerDumpSize = REG_DUMP_SIZE;
dumpHeader.codeDumpSize = CODE_DUMP_SIZE;
dumpHeader.additionalDataSize = 0;
//Dump registers //Dump registers
//Current order of saved regs: dfsr, ifsr, far, fpexc, fpinst, fpinst2, 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[6]; u32 cpsr = regs[6];
u32 pc = regs[7] - ((type < 3) ? (((cpsr & 0x20) != 0 && type == 1) ? 2 : 4) : 8); u32 pc = regs[7] - ((type < 3) ? (((cpsr & 0x20) != 0 && type == 1) ? 2 : 4) : 8);
regdump[15] = pc; registerDump[15] = pc;
regdump[16] = cpsr; registerDump[16] = cpsr;
for(u32 i = 0; i < 6; i++) regdump[17 + i] = regs[i]; for(u32 i = 0; i < 6; i++) registerDump[17 + i] = regs[i];
for(u32 i = 0; i < 7; i++) regdump[8 + i] = regs[8 + i]; for(u32 i = 0; i < 7; i++) registerDump[8 + i] = regs[8 + i];
for(u32 i = 0; i < 8; i++) regdump[i] = regs[15 + i]; for(u32 i = 0; i < 8; i++) registerDump[i] = regs[15 + i];
dumpHeader.stackDumpSize = 0x1000 - (registerDump[13] & 0xfff);
//Dump code //Dump code
u8 *codedump = (u8 *)regdump + dump[6]; 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
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 < dumpHeader.codeDumpSize; i++)
for(u32 i = 0; i < dump[7]; i++) codeDump[i] = instr[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 stack in place
dump[8] = 0x1000 - (regdump[13] & 0xfff); //Stack dump size (max. 0x1000 bytes) vu32 *sp = (vu32 *)registerDump[13];
vu32 *sp = (vu32 *)regdump[13]; for(u32 i = 0; i < dumpHeader.stackDumpSize / 4; i++)
vu32 *stackdump = (vu32 *)((vu8 *)FINAL_BUFFER + 40 + REG_DUMP_SIZE + CODE_DUMP_SIZE); *final++ = sp[i];
for(u32 i = 0; i < dump[8] / 4; i++)
stackdump[i] = sp[i];
vu8 *currentKProcess = *(vu8 **)0xFFFF9004; vu8 *currentKProcess = *(vu8 **)0xFFFF9004;
vu8 *currentKCodeSet = (currentKProcess != NULL) ? *(vu8 **)(currentKProcess + CODESET_OFFSET) : NULL; vu8 *currentKCodeSet = (currentKProcess != NULL) ? *(vu8 **)(currentKProcess + CODESET_OFFSET) : NULL;
if(currentKCodeSet != NULL) if(currentKCodeSet != NULL)
{ {
dump[9] = 16; //Additional data size vu32 *additionalData = final;
vu32 *additionalData = (vu32 *)((vu8 *)FINAL_BUFFER + 40 + REG_DUMP_SIZE + CODE_DUMP_SIZE + dump[8]); dumpHeader.additionalDataSize = 16;
additionalData[0] = *(vu32 *)(currentKCodeSet + 0x50); //Process name additionalData[0] = *(vu32 *)(currentKCodeSet + 0x50); //Process name
additionalData[1] = *(vu32 *)(currentKCodeSet + 0x54); 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); additionalData[3] = *(vu32 *)(currentKCodeSet + 0x60);
} }
else else
dump[9] = 0; 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;
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];
clearDCacheAndDMB(); clearDCacheAndDMB();
mcuReboot(); mcuReboot();

View File

@ -6,8 +6,24 @@
*/ */
#pragma once #pragma once
#include "types.h"
void FIQHandler(void); void FIQHandler(void);
void undefinedInstructionHandler(void); void undefinedInstructionHandler(void);
void dataAbortHandler(void); void dataAbortHandler(void);
void prefetchAbortHandler(void); 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;

View File

@ -39,14 +39,8 @@ _commonHandler:
msr cpsr_c, r3 @ restore processor mode msr cpsr_c, r3 @ restore processor mode
mov sp, r6 mov sp, r6
stmfd sp!, {r2,lr} 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
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 mov r0, sp
b mainHandler b mainHandler

View File

@ -5,60 +5,67 @@
* This is part of Luma3DS, see LICENSE.txt for details * This is part of Luma3DS, see LICENSE.txt for details
*/ */
#include "types.h"
#include "i2c.h" #include "i2c.h"
#include "handlers.h" #include "handlers.h"
#define FINAL_BUFFER 0x25000000 #define FINAL_BUFFER 0x25000000
#define REG_DUMP_SIZE (4*20) #define REG_DUMP_SIZE (4*17)
#define CODE_DUMP_SIZE 48 #define CODE_DUMP_SIZE 48
#define OTHER_DATA_SIZE 0
void __attribute__((noreturn)) mainHandler(u32 regs[REG_DUMP_SIZE / 4], u32 type) 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 u32 registerDump[REG_DUMP_SIZE / 4];
dump[1] = 0xDEADCAFE; //Magic u8 codeDump[CODE_DUMP_SIZE];
dump[2] = (1 << 16) | 1; //Dump format version number
dump[3] = 9; //Processor dumpHeader.magic[0] = 0xDEADC0DE;
dump[4] = type; //Exception type dumpHeader.magic[1] = 0xDEADCAFE;
dump[6] = REG_DUMP_SIZE; //Register dump size (r0-r12, sp, lr, pc, cpsr) dumpHeader.versionMajor = 1;
dump[7] = CODE_DUMP_SIZE; //Code dump size (10 ARM instructions, up to 20 Thumb instructions). dumpHeader.versionMinor = 1;
dump[9] = OTHER_DATA_SIZE; //Other data size
dumpHeader.processor = 9;
dumpHeader.core = 0;
dumpHeader.type = type;
dumpHeader.registerDumpSize = REG_DUMP_SIZE;
dumpHeader.codeDumpSize = CODE_DUMP_SIZE;
dumpHeader.additionalDataSize = 0;
//Dump registers //Dump registers
//Current order of saved regs: dfsr, ifsr, far, cpsr, pc, r8-r14, r0-r7 //Current order of saved regs: 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]; registerDump[15] = pc;
u32 pc = regs[4] - ((type < 3) ? (((cpsr & 0x20) != 0 && type == 1) ? 2 : 4) : 8); 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; dumpHeader.stackDumpSize = 0x1000 - (registerDump[13] & 0xfff);
regdump[16] = cpsr; dumpHeader.totalSize = sizeof(ExceptionDumpHeader) + dumpHeader.registerDumpSize + dumpHeader.codeDumpSize + dumpHeader.stackDumpSize;
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
//Dump code //Dump code
u8 *codedump = (u8 *)regdump + dump[6]; 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
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 < dumpHeader.codeDumpSize; i++)
for(u32 i = 0; i < dump[7]; i++) codeDump[i] = instr[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];
//Copy header (actually optimized by the compiler), register dump and code dump
vu32 *final = (vu32 *)FINAL_BUFFER; vu32 *final = (vu32 *)FINAL_BUFFER;
for(u32 i = 0; i < (40 + REG_DUMP_SIZE + CODE_DUMP_SIZE) / 4; i++) *(ExceptionDumpHeader *)final = dumpHeader;
final[i] = dump[i]; 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 i2cWriteRegister(I2C_DEV_MCU, 0x20, 1 << 2); //Reboot
while(1); while(1);

View File

@ -13,6 +13,21 @@
#include "../build/arm9_exceptions.h" #include "../build/arm9_exceptions.h"
#include "../build/arm11_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 installArm9Handlers(void)
{ {
void *payloadAddress = (void *)0x01FF8000; void *payloadAddress = (void *)0x01FF8000;
@ -92,16 +107,19 @@ void detectAndProcessExceptionDumps(void)
char hexstring[] = "00000000"; 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 path9[41] = "/luma/dumps/arm9";
char path11[42] = "/luma/dumps/arm11"; char path11[42] = "/luma/dumps/arm11";
char fileName[] = "crash_dump_00000000.dmp"; 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); findDumpFile(path9, fileName);
path9[16] = '/'; path9[16] = '/';
@ -117,25 +135,25 @@ void detectAndProcessExceptionDumps(void)
fileWrite((void *)dump, path11, dump[5]); fileWrite((void *)dump, path11, dump[5]);
} }
char arm11Str[] = "Processor: ARM11 (core X)"; 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(); initScreens();
drawString("An exception occurred", 10, 10, COLOR_RED); 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("Exception type: ", 10, posY, COLOR_WHITE);
posY = drawString(handledExceptionNames[dump[4]], 10 + 17 * SPACING_X, posY, COLOR_WHITE); posY = drawString(handledExceptionNames[dumpHeader->type], 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) 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); 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; posY += SPACING_Y;
char processNameStr[] = "Current process: --------"; 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); posY = drawString(processNameStr, 10, posY, COLOR_WHITE);
} }
@ -143,13 +161,13 @@ void detectAndProcessExceptionDumps(void)
for(u32 i = 0; i < 17; i += 2) for(u32 i = 0; i < 17; i += 2)
{ {
posY = drawString(registerNames[i], 10, posY, COLOR_WHITE); 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); 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); 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); posY = drawString(hexstring, 10 + 29 * SPACING_X, posY, COLOR_WHITE);
} }
@ -158,15 +176,15 @@ void detectAndProcessExceptionDumps(void)
posY += 2 * SPACING_Y; posY += 2 * SPACING_Y;
u32 mode = dump[10 + 16] & 0xF; u32 mode = regs[16] & 0xF;
if(dump[4] == 3 && (mode == 7 || mode == 11)) 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 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("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); drawString("Press any button to shutdown", 10, posY, COLOR_WHITE);
waitInput(); waitInput();