2016-04-26 22:06:19 +02:00
|
|
|
/*
|
2016-07-05 16:24:00 +02:00
|
|
|
* This file is part of Luma3DS
|
|
|
|
* Copyright (C) 2016 Aurora Wright, TuxSH
|
|
|
|
*
|
|
|
|
* 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-04-26 22:06:19 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "exceptions.h"
|
|
|
|
#include "fs.h"
|
2016-08-30 21:02:45 +02:00
|
|
|
#include "strings.h"
|
2016-04-26 22:06:19 +02:00
|
|
|
#include "memory.h"
|
2016-06-11 00:00:53 +02:00
|
|
|
#include "screen.h"
|
2016-05-03 17:05:19 +02:00
|
|
|
#include "draw.h"
|
|
|
|
#include "i2c.h"
|
2016-04-26 22:06:19 +02:00
|
|
|
#include "utils.h"
|
|
|
|
#include "../build/arm9_exceptions.h"
|
2016-06-02 22:33:44 +02:00
|
|
|
#include "../build/arm11_exceptions.h"
|
|
|
|
|
2016-04-26 22:06:19 +02:00
|
|
|
void installArm9Handlers(void)
|
|
|
|
{
|
2016-05-28 22:05:07 +02:00
|
|
|
const u32 offsets[] = {0x08, 0x18, 0x20, 0x28};
|
|
|
|
|
2016-08-30 02:03:56 +02:00
|
|
|
memcpy((void *)0x01FF8000, arm9_exceptions + 32, arm9_exceptions_size - 32);
|
2016-06-03 21:38:35 +02:00
|
|
|
|
2016-05-28 22:05:07 +02:00
|
|
|
//IRQHandler is at 0x08000000, but we won't handle it for some reasons
|
|
|
|
//svcHandler is at 0x08000010, but we won't handle svc either
|
|
|
|
|
|
|
|
for(u32 i = 0; i < 4; i++)
|
|
|
|
{
|
|
|
|
*(vu32 *)(0x08000000 + offsets[i]) = 0xE51FF004;
|
2016-06-03 21:38:35 +02:00
|
|
|
*(vu32 *)(0x08000000 + offsets[i] + 4) = *((const u32 *)arm9_exceptions + 1 + i);
|
2016-05-28 22:05:07 +02:00
|
|
|
}
|
2016-04-26 22:06:19 +02:00
|
|
|
}
|
|
|
|
|
2016-08-30 21:50:04 +02:00
|
|
|
void installArm11Handlers(u32 *exceptionsPage, u32 stackAddress, u32 codeSetOffset)
|
2016-06-02 22:33:44 +02:00
|
|
|
{
|
|
|
|
u32 *initFPU;
|
2016-08-30 02:03:56 +02:00
|
|
|
for(initFPU = exceptionsPage; initFPU < (exceptionsPage + 0x400) && (initFPU[0] != 0xE59F0008 || initFPU[1] != 0xE5900000); initFPU++);
|
2016-06-02 22:33:44 +02:00
|
|
|
|
|
|
|
u32 *mcuReboot;
|
2016-08-30 02:03:56 +02:00
|
|
|
for(mcuReboot = exceptionsPage; mcuReboot < (exceptionsPage + 0x400) && (mcuReboot[0] != 0xE59F4104 || mcuReboot[1] != 0xE3A0A0C2); mcuReboot++);
|
2016-06-03 21:38:35 +02:00
|
|
|
mcuReboot--;
|
2016-06-02 22:33:44 +02:00
|
|
|
|
|
|
|
u32 *freeSpace;
|
2016-08-30 02:03:56 +02:00
|
|
|
for(freeSpace = initFPU; freeSpace < (exceptionsPage + 0x400) && (freeSpace[0] != 0xFFFFFFFF || freeSpace[1] != 0xFFFFFFFF); freeSpace++);
|
|
|
|
|
2016-06-03 21:38:35 +02:00
|
|
|
memcpy(freeSpace, arm11_exceptions + 32, arm11_exceptions_size - 32);
|
2016-06-02 22:33:44 +02:00
|
|
|
|
2016-06-03 21:38:35 +02:00
|
|
|
exceptionsPage[1] = MAKE_BRANCH(exceptionsPage + 1, (u8 *)freeSpace + *(u32 *)(arm11_exceptions + 8) - 32); //Undefined Instruction
|
|
|
|
exceptionsPage[3] = MAKE_BRANCH(exceptionsPage + 3, (u8 *)freeSpace + *(u32 *)(arm11_exceptions + 12) - 32); //Prefetch Abort
|
|
|
|
exceptionsPage[4] = MAKE_BRANCH(exceptionsPage + 4, (u8 *)freeSpace + *(u32 *)(arm11_exceptions + 16) - 32); //Data Abort
|
|
|
|
exceptionsPage[7] = MAKE_BRANCH(exceptionsPage + 7, (u8 *)freeSpace + *(u32 *)(arm11_exceptions + 4) - 32); //FIQ
|
2016-06-02 22:33:44 +02:00
|
|
|
|
2016-06-03 21:38:35 +02:00
|
|
|
for(u32 *pos = freeSpace; pos < (u32 *)((u8 *)freeSpace + arm11_exceptions_size - 32); pos++)
|
2016-06-02 22:33:44 +02:00
|
|
|
{
|
2016-06-03 21:38:35 +02:00
|
|
|
switch(*pos) //Perform relocations
|
2016-06-02 22:33:44 +02:00
|
|
|
{
|
2016-08-30 21:50:04 +02:00
|
|
|
case 0xFFFF3000: *pos = stackAddress; break;
|
2016-06-02 22:33:44 +02:00
|
|
|
case 0xEBFFFFFE: *pos = MAKE_BRANCH_LINK(pos, initFPU); break;
|
|
|
|
case 0xEAFFFFFE: *pos = MAKE_BRANCH(pos, mcuReboot); break;
|
2016-08-30 02:03:56 +02:00
|
|
|
case 0xE12FFF1C: pos[1] = 0xFFFF0000 + 4 * (u32)(freeSpace - exceptionsPage) + pos[1] - 32; break; //bx r12 (mainHandler)
|
|
|
|
case 0xBEEFBEEF: *pos = codeSetOffset; break;
|
2016-06-02 22:33:44 +02:00
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-26 22:06:19 +02:00
|
|
|
void detectAndProcessExceptionDumps(void)
|
|
|
|
{
|
2016-06-08 21:44:04 +02:00
|
|
|
volatile ExceptionDumpHeader *dumpHeader = (volatile ExceptionDumpHeader *)0x25000000;
|
2016-04-26 22:06:19 +02:00
|
|
|
|
2016-06-08 21:44:04 +02:00
|
|
|
if(dumpHeader->magic[0] == 0xDEADC0DE && dumpHeader->magic[1] == 0xDEADCAFE && (dumpHeader->processor == 9 || dumpHeader->processor == 11))
|
2016-04-26 22:06:19 +02:00
|
|
|
{
|
2016-08-30 02:03:56 +02:00
|
|
|
char path[42];
|
2016-04-26 22:06:19 +02:00
|
|
|
char fileName[] = "crash_dump_00000000.dmp";
|
2016-06-08 21:44:04 +02:00
|
|
|
u32 size = dumpHeader->totalSize;
|
2016-04-26 22:06:19 +02:00
|
|
|
|
2016-08-30 21:02:45 +02:00
|
|
|
char *pathFolder = dumpHeader->processor == 9 ? "/luma/dumps/arm9" : "/luma/dumps/arm11";
|
2016-04-26 22:06:19 +02:00
|
|
|
|
2016-08-30 02:03:56 +02:00
|
|
|
findDumpFile(pathFolder, fileName);
|
2016-08-30 21:11:11 +02:00
|
|
|
memcpy(path, pathFolder, strlen(pathFolder) + 1);
|
2016-08-30 21:02:45 +02:00
|
|
|
concatenateStrings(path, "/");
|
|
|
|
concatenateStrings(path, fileName);
|
2016-08-30 02:03:56 +02:00
|
|
|
|
2016-08-30 17:09:04 +02:00
|
|
|
fileWrite((void *)dumpHeader, path, size);
|
2016-08-30 02:03:56 +02:00
|
|
|
|
|
|
|
vu32 *regs = (vu32 *)((vu8 *)dumpHeader + sizeof(ExceptionDumpHeader));
|
|
|
|
vu8 *additionalData = (vu8 *)dumpHeader + dumpHeader->totalSize - dumpHeader->additionalDataSize;
|
|
|
|
|
|
|
|
const char *handledExceptionNames[] = {
|
|
|
|
"FIQ", "undefined instruction", "prefetch abort", "data abort"
|
|
|
|
};
|
|
|
|
|
|
|
|
const char *registerNames[] = {
|
|
|
|
"R0", "R1", "R2", "R3", "R4", "R5", "R6", "R7", "R8", "R9", "R10", "R11", "R12",
|
|
|
|
"SP", "LR", "PC", "CPSR", "FPEXC"
|
|
|
|
};
|
|
|
|
|
|
|
|
char hexstring[] = "00000000";
|
|
|
|
|
2016-06-07 19:25:45 +02:00
|
|
|
char arm11Str[] = "Processor: ARM11 (core X)";
|
2016-06-08 21:44:04 +02:00
|
|
|
if(dumpHeader->processor == 11) arm11Str[29] = '0' + (char)(dumpHeader->core);
|
2016-08-30 02:03:56 +02:00
|
|
|
|
2016-05-03 17:05:19 +02:00
|
|
|
initScreens();
|
|
|
|
|
2016-05-06 22:50:01 +02:00
|
|
|
drawString("An exception occurred", 10, 10, COLOR_RED);
|
2016-08-30 02:03:56 +02:00
|
|
|
int posY = drawString(dumpHeader->processor == 11 ? arm11Str : "Processor: ARM9", 10, 30, COLOR_WHITE) + SPACING_Y;
|
2016-06-07 19:25:45 +02:00
|
|
|
|
|
|
|
posY = drawString("Exception type: ", 10, posY, COLOR_WHITE);
|
2016-06-08 21:44:04 +02:00
|
|
|
posY = drawString(handledExceptionNames[dumpHeader->type], 10 + 17 * SPACING_X, posY, COLOR_WHITE);
|
2016-06-07 19:25:45 +02:00
|
|
|
|
2016-08-12 15:17:19 +02:00
|
|
|
if(dumpHeader->type == 2)
|
|
|
|
{
|
|
|
|
if((regs[16] & 0x20) == 0 && dumpHeader->codeDumpSize >= 4)
|
|
|
|
{
|
|
|
|
u32 instr = *(vu32 *)((vu8 *)dumpHeader + sizeof(ExceptionDumpHeader) + dumpHeader->registerDumpSize + dumpHeader->codeDumpSize - 4);
|
|
|
|
if(instr == 0xE12FFF7E)
|
|
|
|
posY = drawString("(kernel panic)", 10 + 32 * SPACING_X, posY, COLOR_WHITE);
|
|
|
|
else if(instr == 0xEF00003C)
|
|
|
|
posY = drawString("(svcBreak)", 10 + 32 * SPACING_X, posY, COLOR_WHITE);
|
|
|
|
}
|
|
|
|
else if((regs[16] & 0x20) == 0 && dumpHeader->codeDumpSize >= 2)
|
|
|
|
{
|
|
|
|
u16 instr = *(vu16 *)((vu8 *)dumpHeader + sizeof(ExceptionDumpHeader) + dumpHeader->registerDumpSize + dumpHeader->codeDumpSize - 2);
|
|
|
|
if(instr == 0xDF3C)
|
|
|
|
posY = drawString("(svcBreak)", 10 + 32 * SPACING_X, posY, COLOR_WHITE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-08 21:44:04 +02:00
|
|
|
if(dumpHeader->processor == 11 && dumpHeader->additionalDataSize != 0)
|
2016-06-07 19:25:45 +02:00
|
|
|
{
|
|
|
|
posY += SPACING_Y;
|
|
|
|
char processNameStr[] = "Current process: --------";
|
2016-06-08 21:44:04 +02:00
|
|
|
memcpy(processNameStr + 17, (char *)additionalData, 8);
|
2016-06-07 19:25:45 +02:00
|
|
|
posY = drawString(processNameStr, 10, posY, COLOR_WHITE);
|
|
|
|
}
|
2016-06-04 21:11:15 +02:00
|
|
|
|
2016-05-06 22:50:01 +02:00
|
|
|
posY += 3 * SPACING_Y;
|
2016-08-30 02:03:56 +02:00
|
|
|
|
2016-05-06 22:50:01 +02:00
|
|
|
for(u32 i = 0; i < 17; i += 2)
|
|
|
|
{
|
|
|
|
posY = drawString(registerNames[i], 10, posY, COLOR_WHITE);
|
2016-06-08 21:44:04 +02:00
|
|
|
hexItoa(regs[i], hexstring);
|
2016-05-06 22:50:01 +02:00
|
|
|
posY = drawString(hexstring, 10 + 7 * SPACING_X, posY, COLOR_WHITE);
|
2016-05-27 15:15:25 +02:00
|
|
|
|
2016-06-08 21:44:04 +02:00
|
|
|
if(dumpHeader->processor != 9 || i != 16)
|
2016-05-06 22:50:01 +02:00
|
|
|
{
|
|
|
|
posY = drawString(registerNames[i + 1], 10 + 22 * SPACING_X, posY, COLOR_WHITE);
|
2016-08-30 02:03:56 +02:00
|
|
|
hexItoa(i == 16 ? regs[20] : regs[i + 1], hexstring);
|
2016-05-06 22:50:01 +02:00
|
|
|
posY = drawString(hexstring, 10 + 29 * SPACING_X, posY, COLOR_WHITE);
|
|
|
|
}
|
2016-05-27 15:15:25 +02:00
|
|
|
|
2016-05-06 22:50:01 +02:00
|
|
|
posY += SPACING_Y;
|
|
|
|
}
|
2016-05-27 15:15:25 +02:00
|
|
|
|
2016-05-06 22:50:01 +02:00
|
|
|
posY += 2 * SPACING_Y;
|
2016-06-03 21:38:35 +02:00
|
|
|
|
2016-06-08 21:44:04 +02:00
|
|
|
u32 mode = regs[16] & 0xF;
|
|
|
|
if(dumpHeader->type == 3 && (mode == 7 || mode == 11))
|
2016-06-07 19:25:45 +02:00
|
|
|
{
|
2016-08-30 02:03:56 +02:00
|
|
|
posY = drawString("Incorrect dump: failed to dump code and/or stack", 10, posY, 0x00FFFF) + 2 * SPACING_Y; //In yellow
|
2016-06-08 21:44:04 +02:00
|
|
|
if(dumpHeader->processor != 9) posY -= SPACING_Y;
|
2016-06-07 19:25:45 +02:00
|
|
|
}
|
2016-06-03 21:38:35 +02:00
|
|
|
|
2016-05-06 22:50:01 +02:00
|
|
|
posY = drawString("You can find a dump in the following file:", 10, posY, COLOR_WHITE) + SPACING_Y;
|
2016-08-30 02:03:56 +02:00
|
|
|
posY = drawString(path, 10, posY, COLOR_WHITE) + 2 * SPACING_Y;
|
2016-05-06 22:50:01 +02:00
|
|
|
drawString("Press any button to shutdown", 10, posY, COLOR_WHITE);
|
2016-05-27 15:15:25 +02:00
|
|
|
|
2016-05-03 17:05:19 +02:00
|
|
|
waitInput();
|
2016-08-30 02:03:56 +02:00
|
|
|
|
|
|
|
memset32((void *)dumpHeader, 0, size);
|
|
|
|
|
2016-06-11 00:00:53 +02:00
|
|
|
mcuPowerOff();
|
2016-04-26 22:06:19 +02:00
|
|
|
}
|
|
|
|
}
|