2016-04-26 22:06:19 +02:00
|
|
|
/*
|
2016-05-28 22:05:07 +02:00
|
|
|
* mainHandler.c
|
2016-04-26 22:06:19 +02:00
|
|
|
* by TuxSH
|
|
|
|
*
|
|
|
|
* This is part of Luma3DS, see LICENSE.txt for details
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "types.h"
|
|
|
|
#include "i2c.h"
|
|
|
|
#include "handlers.h"
|
|
|
|
|
|
|
|
#define FINAL_BUFFER 0x25000000
|
2016-05-06 15:45:25 +02:00
|
|
|
|
|
|
|
#define REG_DUMP_SIZE (4*17)
|
|
|
|
#define CODE_DUMP_SIZE 48
|
|
|
|
#define OTHER_DATA_SIZE 0
|
2016-04-26 22:06:19 +02:00
|
|
|
|
|
|
|
void __attribute__((noreturn)) mainHandler(u32 regs[17], u32 type)
|
|
|
|
{
|
2016-05-06 15:45:25 +02:00
|
|
|
//vu32 *dump = (u32 *)TEMP_BUFFER;
|
2016-06-03 21:38:35 +02:00
|
|
|
u32 dump[(40 + REG_DUMP_SIZE + CODE_DUMP_SIZE) / 4];
|
2016-05-06 15:45:25 +02:00
|
|
|
|
2016-04-26 22:06:19 +02:00
|
|
|
dump[0] = 0xDEADC0DE; //Magic
|
|
|
|
dump[1] = 0xDEADCAFE; //Magic
|
|
|
|
dump[2] = (1 << 16) | 0; //Dump format version number
|
|
|
|
dump[3] = 9; //Processor
|
|
|
|
dump[4] = type; //Exception type
|
2016-05-06 15:45:25 +02:00
|
|
|
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
|
2016-04-26 22:06:19 +02:00
|
|
|
|
|
|
|
//Dump registers
|
2016-05-06 15:45:25 +02:00
|
|
|
//Current order of saved regs: cpsr, pc, r8-r12, sp, lr, r0-r7
|
2016-04-26 22:06:19 +02:00
|
|
|
vu32 *regdump = dump + 10;
|
|
|
|
|
2016-05-06 15:45:25 +02:00
|
|
|
u32 cpsr = regs[0];
|
|
|
|
u32 pc = regs[1] - ((type < 3) ? (((cpsr & 0x20) != 0 && type == 1) ? 2 : 4) : 8);
|
2016-04-26 22:06:19 +02:00
|
|
|
|
|
|
|
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];
|
|
|
|
|
2016-06-03 21:38:35 +02:00
|
|
|
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
|
|
|
|
|
2016-04-26 22:06:19 +02:00
|
|
|
//Dump code
|
2016-06-04 21:11:15 +02:00
|
|
|
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++)
|
2016-04-26 22:06:19 +02:00
|
|
|
codedump[i] = instr[i];
|
|
|
|
|
|
|
|
//Dump stack
|
|
|
|
vu32 *sp = (vu32 *)regdump[13];
|
2016-06-03 21:38:35 +02:00
|
|
|
vu32 *stackdump = (vu32 *)((vu8 *)FINAL_BUFFER + 40 + REG_DUMP_SIZE + CODE_DUMP_SIZE);
|
2016-04-26 22:06:19 +02:00
|
|
|
for(u32 i = 0; i < dump[8] / 4; i++)
|
|
|
|
stackdump[i] = sp[i];
|
|
|
|
|
2016-06-03 21:38:35 +02:00
|
|
|
vu32 *final = (vu32 *)FINAL_BUFFER;
|
|
|
|
for(u32 i = 0; i < (40 + REG_DUMP_SIZE + CODE_DUMP_SIZE) / 4; i++)
|
2016-04-26 22:06:19 +02:00
|
|
|
final[i] = dump[i];
|
|
|
|
|
|
|
|
i2cWriteRegister(I2C_DEV_MCU, 0x20, 1 << 2); //Reboot
|
|
|
|
while(1);
|
|
|
|
}
|