2016-04-26 22:06:19 +02:00
|
|
|
/*
|
2016-07-05 16:24:00 +02:00
|
|
|
* This file is part of Luma3DS
|
2020-04-25 14:26:21 +02:00
|
|
|
* Copyright (C) 2016-2020 Aurora Wright, TuxSH
|
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/>.
|
|
|
|
*
|
2017-06-05 02:02:04 +02:00
|
|
|
* Additional Terms 7.b and 7.c of GPLv3 apply 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.
|
|
|
|
* * Prohibiting misrepresentation of the origin of that material,
|
|
|
|
* or requiring that modified versions of such material be marked in
|
|
|
|
* reasonable ways as different from the original version.
|
2016-04-26 22:06:19 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "exceptions.h"
|
|
|
|
#include "fs.h"
|
|
|
|
#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"
|
2016-04-26 22:06:19 +02:00
|
|
|
#include "utils.h"
|
2017-05-07 17:58:44 +02:00
|
|
|
#include "fmt.h"
|
2017-08-16 16:55:07 +02:00
|
|
|
#include "buttons.h"
|
2018-05-22 20:26:14 +02:00
|
|
|
#include "arm9_exception_handlers.h"
|
2016-06-02 22:33:44 +02:00
|
|
|
|
2016-04-26 22:06:19 +02:00
|
|
|
void installArm9Handlers(void)
|
|
|
|
{
|
2018-05-22 20:26:14 +02:00
|
|
|
vu32 *dstVeneers = (vu32 *)0x08000000;
|
2016-06-03 21:38:35 +02:00
|
|
|
|
2018-05-22 20:26:14 +02:00
|
|
|
for(u32 i = 0; i < 6; i++)
|
2016-05-28 22:05:07 +02:00
|
|
|
{
|
2018-05-22 20:26:14 +02:00
|
|
|
if(arm9ExceptionHandlerAddressTable[i] != 0)
|
|
|
|
{
|
|
|
|
dstVeneers[2 * i] = 0xE51FF004;
|
|
|
|
dstVeneers[2 * i + 1] = arm9ExceptionHandlerAddressTable[i];
|
|
|
|
}
|
2016-05-28 22:05:07 +02:00
|
|
|
}
|
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-11-15 20:18:28 +01:00
|
|
|
if(dumpHeader->magic[0] != 0xDEADC0DE || dumpHeader->magic[1] != 0xDEADCAFE || (dumpHeader->processor != 9 && dumpHeader->processor != 11)) return;
|
2016-08-30 02:03:56 +02:00
|
|
|
|
2016-11-15 19:29:48 +01:00
|
|
|
const vu32 *regs = (vu32 *)((vu8 *)dumpHeader + sizeof(ExceptionDumpHeader));
|
|
|
|
const vu8 *stackDump = (vu8 *)regs + dumpHeader->registerDumpSize + dumpHeader->codeDumpSize;
|
|
|
|
const vu8 *additionalData = stackDump + dumpHeader->stackDumpSize;
|
2016-09-03 02:02:03 +02:00
|
|
|
|
2017-06-09 17:29:26 +02:00
|
|
|
static const char *handledExceptionNames[] = {
|
2016-11-15 19:29:48 +01:00
|
|
|
"FIQ", "undefined instruction", "prefetch abort", "data abort"
|
2017-06-09 17:29:26 +02:00
|
|
|
},
|
|
|
|
*specialExceptions[] = {
|
2017-05-07 17:58:44 +02:00
|
|
|
"kernel panic", "svcBreak"
|
2017-06-09 17:29:26 +02:00
|
|
|
},
|
|
|
|
*registerNames[] = {
|
2016-11-15 19:29:48 +01:00
|
|
|
"R0", "R1", "R2", "R3", "R4", "R5", "R6", "R7", "R8", "R9", "R10", "R11", "R12",
|
|
|
|
"SP", "LR", "PC", "CPSR", "FPEXC"
|
2017-08-15 14:05:32 +02:00
|
|
|
},
|
|
|
|
*faultStatusNames[] = {
|
2017-10-05 19:53:09 +02:00
|
|
|
"Alignment", "Instr.cache maintenance op.",
|
|
|
|
"Ext.Abort on translation - Lv1", "Ext.Abort on translation - Lv2",
|
2017-08-15 14:05:32 +02:00
|
|
|
"Translation - Section", "Translation - Page", "Access bit - Section", "Access bit - Page",
|
|
|
|
"Domain - Section", "Domain - Page", "Permission - Section", "Permission - Page",
|
|
|
|
"Precise External Abort", "Imprecise External Abort", "Debug event"
|
|
|
|
};
|
|
|
|
|
2017-08-16 18:02:35 +02:00
|
|
|
static const u32 faultStatusValues[] = {
|
2017-08-15 14:05:32 +02:00
|
|
|
0b1, 0b100, 0b1100, 0b1110, 0b101, 0b111, 0b11, 0b110, 0b1001, 0b1011, 0b1101,
|
|
|
|
0b1111, 0b1000, 0b10110, 0b10
|
2016-11-15 19:29:48 +01:00
|
|
|
};
|
2016-08-30 02:03:56 +02:00
|
|
|
|
2016-11-15 19:29:48 +01:00
|
|
|
initScreens();
|
2016-09-03 02:02:03 +02:00
|
|
|
|
2017-05-07 17:58:44 +02:00
|
|
|
drawString(true, 10, 10, COLOR_RED, "An exception occurred");
|
|
|
|
u32 posY;
|
2020-04-25 14:17:23 +02:00
|
|
|
if(dumpHeader->processor == 11) posY = drawFormattedString(true, 10, 30, COLOR_WHITE, "Processor: Arm11 (core %u)", dumpHeader->core);
|
|
|
|
else posY = drawString(true, 10, 30, COLOR_WHITE, "Processor: Arm9");
|
2017-08-16 18:02:35 +02:00
|
|
|
|
2016-11-15 19:29:48 +01:00
|
|
|
if(dumpHeader->type == 2)
|
|
|
|
{
|
|
|
|
if((regs[16] & 0x20) == 0 && dumpHeader->codeDumpSize >= 4)
|
2016-06-07 19:25:45 +02:00
|
|
|
{
|
2016-11-17 15:38:28 +01:00
|
|
|
u32 instr = *(vu32 *)(stackDump - 4);
|
2017-05-07 17:58:44 +02:00
|
|
|
if(instr == 0xE12FFF7E)
|
|
|
|
posY = drawFormattedString(true, 10, posY + SPACING_Y, COLOR_WHITE, "Exception type: %s (%s)", handledExceptionNames[dumpHeader->type], specialExceptions[0]);
|
|
|
|
else if(instr == 0xEF00003C)
|
|
|
|
posY = drawFormattedString(true, 10, posY + SPACING_Y, COLOR_WHITE, "Exception type: %s (%s)", handledExceptionNames[dumpHeader->type], specialExceptions[1]);
|
|
|
|
else
|
|
|
|
posY = drawFormattedString(true, 10, posY + SPACING_Y, COLOR_WHITE, "Exception type: %s", handledExceptionNames[dumpHeader->type]);
|
2016-06-07 19:25:45 +02:00
|
|
|
}
|
2016-11-26 14:07:48 +01:00
|
|
|
else if((regs[16] & 0x20) != 0 && dumpHeader->codeDumpSize >= 2)
|
2016-05-06 22:50:01 +02:00
|
|
|
{
|
2016-11-17 15:38:28 +01:00
|
|
|
u16 instr = *(vu16 *)(stackDump - 2);
|
2017-05-07 17:58:44 +02:00
|
|
|
if(instr == 0xDF3C)
|
|
|
|
posY = drawFormattedString(true, 10, posY + SPACING_Y, COLOR_WHITE, "Exception type: %s (%s)", handledExceptionNames[dumpHeader->type], specialExceptions[0]);
|
|
|
|
else
|
|
|
|
posY = drawFormattedString(true, 10, posY + SPACING_Y, COLOR_WHITE, "Exception type: %s", handledExceptionNames[dumpHeader->type]);
|
2016-05-06 22:50:01 +02:00
|
|
|
}
|
2017-08-21 19:26:17 +02:00
|
|
|
else
|
|
|
|
posY = drawFormattedString(true, 10, posY + SPACING_Y, COLOR_WHITE, "Exception type: %s", handledExceptionNames[dumpHeader->type]);
|
2016-11-15 19:29:48 +01:00
|
|
|
}
|
2017-05-07 17:58:44 +02:00
|
|
|
else
|
|
|
|
posY = drawFormattedString(true, 10, posY + SPACING_Y, COLOR_WHITE, "Exception type: %s", handledExceptionNames[dumpHeader->type]);
|
2017-08-16 18:02:35 +02:00
|
|
|
|
2017-10-07 20:57:42 +02:00
|
|
|
if(dumpHeader->processor == 11 && dumpHeader->type >= 2)
|
2017-08-21 19:32:39 +02:00
|
|
|
{
|
|
|
|
u32 xfsr = (dumpHeader->type == 2 ? regs[18] : regs[17]) & 0xF;
|
|
|
|
|
|
|
|
for(u32 i = 0; i < 15; i++)
|
|
|
|
if(xfsr == faultStatusValues[i])
|
|
|
|
{
|
|
|
|
posY = drawFormattedString(true, 10, posY + SPACING_Y, COLOR_WHITE, "Fault status: %s", faultStatusNames[i]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-05-27 15:15:25 +02:00
|
|
|
|
2017-10-07 23:04:55 +02:00
|
|
|
if(dumpHeader->additionalDataSize != 0)
|
2017-05-07 17:58:44 +02:00
|
|
|
posY = drawFormattedString(true, 10, posY + SPACING_Y, COLOR_WHITE,
|
|
|
|
"Current process: %.8s (%016llX)", (const char *)additionalData, *(vu64 *)(additionalData + 8));
|
2016-11-15 19:29:48 +01:00
|
|
|
posY += SPACING_Y;
|
2016-09-03 02:02:03 +02:00
|
|
|
|
2016-11-15 19:29:48 +01:00
|
|
|
for(u32 i = 0; i < 17; i += 2)
|
|
|
|
{
|
2019-03-15 19:22:17 +01:00
|
|
|
posY = drawFormattedString(true, 10, posY + SPACING_Y, COLOR_WHITE, "%-7s%08lX", registerNames[i], regs[i]);
|
2016-09-13 01:43:44 +02:00
|
|
|
|
2017-06-13 02:53:53 +02:00
|
|
|
if(i != 16)
|
2019-03-15 19:22:17 +01:00
|
|
|
posY = drawFormattedString(true, 10 + 22 * SPACING_X, posY, COLOR_WHITE, "%-7s%08lX", registerNames[i + 1], regs[i + 1]);
|
2017-06-13 02:53:53 +02:00
|
|
|
else if(dumpHeader->processor == 11)
|
2019-03-15 19:22:17 +01:00
|
|
|
posY = drawFormattedString(true, 10 + 22 * SPACING_X, posY, COLOR_WHITE, "%-7s%08lX", registerNames[i + 1], regs[20]);
|
2016-11-15 19:29:48 +01:00
|
|
|
}
|
2017-08-16 18:02:35 +02:00
|
|
|
|
2017-10-07 20:57:42 +02:00
|
|
|
if(dumpHeader->processor == 11 && dumpHeader->type == 3)
|
2019-03-15 19:22:17 +01:00
|
|
|
posY = drawFormattedString(true, 10, posY + SPACING_Y, COLOR_WHITE, "%-7s%08lX Access type: %s", "FAR", regs[19], regs[17] & (1u << 11) ? "Write" : "Read");
|
2016-09-13 00:52:15 +02:00
|
|
|
|
2016-11-15 19:29:48 +01:00
|
|
|
posY += SPACING_Y;
|
2016-05-27 15:15:25 +02:00
|
|
|
|
2016-11-15 19:29:48 +01:00
|
|
|
u32 mode = regs[16] & 0xF;
|
|
|
|
if(dumpHeader->type == 3 && (mode == 7 || mode == 11))
|
2017-05-07 17:58:44 +02:00
|
|
|
posY = drawString(true, 10, posY + SPACING_Y, COLOR_YELLOW, "Incorrect dump: failed to dump code and/or stack") + SPACING_Y;
|
2016-11-15 19:29:48 +01:00
|
|
|
|
2017-05-07 17:58:44 +02:00
|
|
|
u32 posYBottom = drawString(false, 10, 10, COLOR_WHITE, "Stack dump:") + SPACING_Y;
|
2016-11-15 19:29:48 +01:00
|
|
|
|
|
|
|
for(u32 line = 0; line < 19 && stackDump < additionalData; line++)
|
|
|
|
{
|
2019-03-15 19:22:17 +01:00
|
|
|
posYBottom = drawFormattedString(false, 10, posYBottom + SPACING_Y, COLOR_WHITE, "%08lX:", regs[13] + 8 * line);
|
2016-08-30 02:03:56 +02:00
|
|
|
|
2016-11-15 19:29:48 +01:00
|
|
|
for(u32 i = 0; i < 8 && stackDump < additionalData; i++, stackDump++)
|
2017-05-07 17:58:44 +02:00
|
|
|
drawFormattedString(false, 10 + 10 * SPACING_X + 3 * i * SPACING_X, posYBottom, COLOR_WHITE, "%02X", *stackDump);
|
2016-11-15 19:29:48 +01:00
|
|
|
}
|
2016-08-30 02:03:56 +02:00
|
|
|
|
2017-08-16 16:55:07 +02:00
|
|
|
static const char *choiceMessage[] = {"Press A to save the crash dump", "Press any other button to shutdown"};
|
|
|
|
|
|
|
|
drawString(true, 10, posY + SPACING_Y, COLOR_WHITE, choiceMessage[0]);
|
|
|
|
drawString(true, 10, posY + SPACING_Y + SPACING_Y , COLOR_WHITE, choiceMessage[1]);
|
|
|
|
|
|
|
|
if(waitInput(false) != BUTTON_A) goto exit;
|
|
|
|
|
|
|
|
drawString(true, 10, posY + SPACING_Y, COLOR_BLACK, choiceMessage[0]);
|
|
|
|
drawString(true, 10, posY + SPACING_Y + SPACING_Y , COLOR_BLACK, choiceMessage[1]);
|
|
|
|
|
2018-05-24 00:55:38 +02:00
|
|
|
char folderPath[32],
|
|
|
|
path[128],
|
|
|
|
fileName[32];
|
2017-05-07 23:18:26 +02:00
|
|
|
|
2017-05-07 17:58:44 +02:00
|
|
|
sprintf(folderPath, "dumps/arm%u", dumpHeader->processor);
|
|
|
|
findDumpFile(folderPath, fileName);
|
|
|
|
sprintf(path, "%s/%s", folderPath, fileName);
|
2016-09-06 19:17:45 +02:00
|
|
|
|
2016-11-15 19:29:48 +01:00
|
|
|
if(fileWrite((void *)dumpHeader, path, dumpHeader->totalSize))
|
|
|
|
{
|
2017-08-16 16:55:07 +02:00
|
|
|
posY = drawString(true, 10, posY + SPACING_Y, COLOR_WHITE, "You can find the dump in the following file:");
|
|
|
|
posY = drawFormattedString(true, 10, posY + SPACING_Y, COLOR_WHITE, "%s:/luma/%s", isSdMode ? "SD" : "CTRNAND", path) + SPACING_Y;
|
2016-04-26 22:06:19 +02:00
|
|
|
}
|
2017-05-07 17:58:44 +02:00
|
|
|
else posY = drawString(true, 10, posY + SPACING_Y, COLOR_RED, "Error writing the dump file");
|
2016-11-15 19:29:48 +01:00
|
|
|
|
2017-05-07 17:58:44 +02:00
|
|
|
drawString(true, 10, posY + SPACING_Y, COLOR_WHITE, "Press any button to shutdown");
|
2016-11-15 19:29:48 +01:00
|
|
|
|
|
|
|
waitInput(false);
|
2017-08-17 17:33:18 +02:00
|
|
|
|
|
|
|
exit:
|
2018-05-24 00:55:38 +02:00
|
|
|
memset((void *)dumpHeader, 0, dumpHeader->totalSize);
|
2016-11-15 19:29:48 +01:00
|
|
|
mcuPowerOff();
|
2016-12-22 23:11:15 +01:00
|
|
|
}
|