2016-06-02 22:33:44 +02:00
|
|
|
/*
|
2016-07-05 16:24:00 +02:00
|
|
|
* This file is part of Luma3DS
|
|
|
|
* Copyright (C) 2016 Aurora Wright, TuxSH
|
2016-06-02 22:33:44 +02:00
|
|
|
*
|
2016-07-05 16:24:00 +02:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* Additional Terms 7.b of GPLv3 applies to this file: Requiring preservation of specified
|
|
|
|
* reasonable legal notices or author attributions in that material or in the Appropriate Legal
|
|
|
|
* Notices displayed by works containing it.
|
2016-06-02 22:33:44 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "handlers.h"
|
|
|
|
|
|
|
|
#define FINAL_BUFFER 0xE5000000 //0x25000000
|
|
|
|
|
2016-06-07 19:25:45 +02:00
|
|
|
#define REG_DUMP_SIZE (4*23)
|
2016-06-02 22:33:44 +02:00
|
|
|
#define CODE_DUMP_SIZE 48
|
|
|
|
|
2016-06-07 19:25:45 +02:00
|
|
|
#define CODESET_OFFSET 0xBEEFBEEF
|
|
|
|
|
|
|
|
void __attribute__((noreturn)) mainHandler(u32 regs[REG_DUMP_SIZE / 4], u32 type, u32 cpuId)
|
2016-06-02 22:33:44 +02:00
|
|
|
{
|
2016-06-08 21:44:04 +02:00
|
|
|
ExceptionDumpHeader dumpHeader;
|
|
|
|
|
|
|
|
u32 registerDump[REG_DUMP_SIZE / 4];
|
|
|
|
u8 codeDump[CODE_DUMP_SIZE];
|
2016-06-02 22:33:44 +02:00
|
|
|
vu32 *final = (vu32 *)FINAL_BUFFER;
|
|
|
|
|
2016-06-08 21:44:04 +02:00
|
|
|
while(final[0] == 0xDEADC0DE && final[1] == 0xDEADCAFE && (final[3] == 9 || (final[3] & 0xFFFF) == 11));
|
|
|
|
|
|
|
|
dumpHeader.magic[0] = 0xDEADC0DE;
|
|
|
|
dumpHeader.magic[1] = 0xDEADCAFE;
|
|
|
|
dumpHeader.versionMajor = 1;
|
|
|
|
dumpHeader.versionMinor = 1;
|
2016-06-02 22:33:44 +02:00
|
|
|
|
2016-06-08 21:44:04 +02:00
|
|
|
dumpHeader.processor = 11;
|
|
|
|
dumpHeader.core = cpuId & 0xF;
|
|
|
|
dumpHeader.type = type;
|
|
|
|
|
|
|
|
dumpHeader.registerDumpSize = REG_DUMP_SIZE;
|
|
|
|
dumpHeader.codeDumpSize = CODE_DUMP_SIZE;
|
|
|
|
dumpHeader.additionalDataSize = 0;
|
2016-06-02 22:33:44 +02:00
|
|
|
|
|
|
|
//Dump registers
|
2016-06-07 19:25:45 +02:00
|
|
|
//Current order of saved regs: dfsr, ifsr, far, fpexc, fpinst, fpinst2, cpsr, pc, r8-r12, sp, lr, r0-r7
|
|
|
|
u32 cpsr = regs[6];
|
|
|
|
u32 pc = regs[7] - ((type < 3) ? (((cpsr & 0x20) != 0 && type == 1) ? 2 : 4) : 8);
|
2016-06-02 22:33:44 +02:00
|
|
|
|
2016-06-08 21:44:04 +02:00
|
|
|
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];
|
|
|
|
|
2016-07-14 20:08:31 +02:00
|
|
|
dumpHeader.stackDumpSize = 0x1000 - (registerDump[13] & 0xFFF);
|
2016-06-03 21:38:35 +02:00
|
|
|
|
2016-06-02 22:33:44 +02:00
|
|
|
//Dump code
|
2016-06-08 21:44:04 +02:00
|
|
|
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
|
2016-07-14 20:08:31 +02:00
|
|
|
if(cannotAccessVA((u8 *)instr) || cannotAccessVA((u8 *)instr + dumpHeader.codeDumpSize))
|
|
|
|
dumpHeader.codeDumpSize = 0;
|
2016-06-08 21:44:04 +02:00
|
|
|
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);
|
|
|
|
|
2016-06-02 22:33:44 +02:00
|
|
|
//Dump stack in place
|
2016-06-08 21:44:04 +02:00
|
|
|
vu32 *sp = (vu32 *)registerDump[13];
|
2016-07-14 20:08:31 +02:00
|
|
|
if(cannotAccessVA((u8 *)sp))
|
|
|
|
dumpHeader.stackDumpSize = 0;
|
2016-06-08 21:44:04 +02:00
|
|
|
for(u32 i = 0; i < dumpHeader.stackDumpSize / 4; i++)
|
|
|
|
*final++ = sp[i];
|
|
|
|
|
2016-06-02 22:33:44 +02:00
|
|
|
|
2016-07-14 20:08:31 +02:00
|
|
|
vu8 *currentKProcess = (cannotAccessVA((u8 *)0xFFFF9004)) ? NULL : *(vu8 **)0xFFFF9004;
|
|
|
|
vu8 *currentKCodeSet = (currentKProcess != NULL && ((u32)currentKProcess & 3) == 0 && !cannotAccessVA((u8 *)currentKProcess + CODESET_OFFSET))
|
|
|
|
? *(vu8 **)(currentKProcess + CODESET_OFFSET) : NULL;
|
|
|
|
|
|
|
|
if(currentKCodeSet != NULL && ((u32)currentKCodeSet & 3) == 0 && !cannotAccessVA((u8 *)currentKCodeSet))
|
2016-06-07 19:25:45 +02:00
|
|
|
{
|
2016-06-08 21:44:04 +02:00
|
|
|
vu32 *additionalData = final;
|
|
|
|
dumpHeader.additionalDataSize = 16;
|
2016-06-07 19:25:45 +02:00
|
|
|
|
|
|
|
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
|
2016-07-14 20:08:31 +02:00
|
|
|
dumpHeader.additionalDataSize = 0;
|
2016-06-08 21:44:04 +02:00
|
|
|
|
|
|
|
//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;
|
|
|
|
|
2016-06-11 00:00:53 +02:00
|
|
|
|
2016-06-18 13:10:07 +02:00
|
|
|
cleanInvalidateDCacheAndDMB();
|
2016-06-11 00:00:53 +02:00
|
|
|
mcuReboot(); //Also contains DCache-cleaning code
|
2016-06-02 22:33:44 +02:00
|
|
|
}
|