2018-11-14 11:23:28 +00:00
|
|
|
/*
|
2020-01-24 10:10:40 +00:00
|
|
|
* Copyright (c) 2018-2020 Atmosphère-NX
|
2018-11-14 11:23:28 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms and conditions of the GNU General Public License,
|
|
|
|
* version 2, as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope 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/>.
|
|
|
|
*/
|
2020-05-11 22:02:10 +00:00
|
|
|
#include <stratosphere.hpp>
|
2018-11-14 11:23:28 +00:00
|
|
|
#include "fatal_debug.hpp"
|
|
|
|
#include "fatal_config.hpp"
|
|
|
|
|
2019-10-24 09:30:10 +00:00
|
|
|
namespace ams::fatal::srv {
|
2019-06-17 23:41:03 +00:00
|
|
|
|
2019-07-19 02:09:35 +00:00
|
|
|
namespace {
|
2019-06-17 23:41:03 +00:00
|
|
|
|
2019-07-19 02:09:35 +00:00
|
|
|
constexpr u32 SvcSendSyncRequestInstruction = 0xD4000421;
|
2019-06-17 23:41:03 +00:00
|
|
|
|
2019-07-19 02:09:35 +00:00
|
|
|
struct StackFrame {
|
|
|
|
u64 fp;
|
|
|
|
u64 lr;
|
|
|
|
};
|
2019-06-17 23:41:03 +00:00
|
|
|
|
2021-01-18 16:48:47 +00:00
|
|
|
constexpr inline size_t MaxThreads = 0x60;
|
|
|
|
|
|
|
|
constinit std::pair<u64, u64> g_thread_id_to_tls_map[MaxThreads];
|
|
|
|
constinit size_t g_tls_map_index = 0;
|
|
|
|
|
|
|
|
void ResetThreadTlsMap() {
|
|
|
|
g_tls_map_index = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetThreadTls(u64 thread_id, u64 tls) {
|
|
|
|
if (g_tls_map_index < MaxThreads) {
|
|
|
|
g_thread_id_to_tls_map[g_tls_map_index++] = std::make_pair(thread_id, tls);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GetThreadTls(u64 *out, u64 thread_id) {
|
|
|
|
for (size_t i = 0; i < g_tls_map_index; ++i) {
|
|
|
|
if (g_thread_id_to_tls_map[i].first == thread_id) {
|
|
|
|
*out = g_thread_id_to_tls_map[i].second;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-10-24 08:40:44 +00:00
|
|
|
bool IsThreadFatalCaller(Result result, u32 debug_handle, u64 thread_id, u64 thread_tls_addr, ThreadContext *thread_ctx) {
|
2019-07-19 02:09:35 +00:00
|
|
|
/* Verify that the thread is running or waiting. */
|
|
|
|
{
|
|
|
|
u64 _;
|
|
|
|
u32 _thread_state;
|
|
|
|
if (R_FAILED(svcGetDebugThreadParam(&_, &_thread_state, debug_handle, thread_id, DebugThreadParam_State))) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-11-14 11:23:28 +00:00
|
|
|
|
2019-07-19 02:09:35 +00:00
|
|
|
const svc::ThreadState thread_state = static_cast<svc::ThreadState>(_thread_state);
|
2020-01-18 04:11:03 +00:00
|
|
|
if (thread_state != svc::ThreadState_Waiting && thread_state != svc::ThreadState_Running) {
|
2019-07-19 02:09:35 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2018-11-14 11:23:28 +00:00
|
|
|
|
2019-07-19 02:09:35 +00:00
|
|
|
/* Get the thread context. */
|
|
|
|
if (R_FAILED(svcGetDebugThreadContext(thread_ctx, debug_handle, thread_id, svc::ThreadContextFlag_All))) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-06-17 23:41:03 +00:00
|
|
|
|
2019-07-19 02:09:35 +00:00
|
|
|
/* Try to read the current instruction. */
|
|
|
|
u32 insn;
|
|
|
|
if (R_FAILED(svcReadDebugProcessMemory(&insn, debug_handle, thread_ctx->pc.x, sizeof(insn)))) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-06-17 23:41:03 +00:00
|
|
|
|
2019-07-19 02:09:35 +00:00
|
|
|
/* If the instruction isn't svcSendSyncRequest, it's not the fatal caller. */
|
|
|
|
if (insn != SvcSendSyncRequestInstruction) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-06-17 23:41:03 +00:00
|
|
|
|
2019-07-19 02:09:35 +00:00
|
|
|
/* Read in the fatal caller's TLS. */
|
|
|
|
u8 thread_tls[0x100];
|
|
|
|
if (R_FAILED(svcReadDebugProcessMemory(thread_tls, debug_handle, thread_tls_addr, sizeof(thread_tls)))) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-06-17 23:41:03 +00:00
|
|
|
|
2019-10-18 02:39:22 +00:00
|
|
|
/* We want to parse the command the fatal caller sent. */
|
2019-07-19 02:09:35 +00:00
|
|
|
{
|
2019-10-18 02:39:22 +00:00
|
|
|
const auto request = hipcParseRequest(thread_tls);
|
|
|
|
|
|
|
|
const struct {
|
|
|
|
CmifInHeader header;
|
2019-10-24 08:40:44 +00:00
|
|
|
Result result;
|
2019-10-18 02:39:22 +00:00
|
|
|
} *in_data = decltype(in_data)(request.data.data_words);
|
|
|
|
static_assert(sizeof(*in_data) == 0x14, "InData!");
|
2019-06-17 23:41:03 +00:00
|
|
|
|
2019-07-19 02:09:35 +00:00
|
|
|
/* Fatal command takes in a PID, only one buffer max. */
|
2019-10-18 02:39:22 +00:00
|
|
|
if ((request.meta.type != CmifCommandType_Request && request.meta.type != CmifCommandType_RequestWithContext) ||
|
|
|
|
!request.meta.send_pid ||
|
|
|
|
request.meta.num_send_statics ||
|
|
|
|
request.meta.num_recv_statics ||
|
|
|
|
request.meta.num_recv_buffers ||
|
|
|
|
request.meta.num_exch_buffers ||
|
|
|
|
request.meta.num_copy_handles ||
|
|
|
|
request.meta.num_move_handles ||
|
|
|
|
request.meta.num_data_words < ((sizeof(*in_data) + 0x10) / sizeof(u32)))
|
|
|
|
{
|
2019-07-19 02:09:35 +00:00
|
|
|
return false;
|
|
|
|
}
|
2019-06-17 23:41:03 +00:00
|
|
|
|
2019-10-18 02:39:22 +00:00
|
|
|
if (in_data->header.magic != CMIF_IN_HEADER_MAGIC) {
|
2019-07-19 02:09:35 +00:00
|
|
|
return false;
|
|
|
|
}
|
2019-06-17 23:41:03 +00:00
|
|
|
|
2019-10-18 02:39:22 +00:00
|
|
|
if (in_data->header.version > 1) {
|
2019-07-19 02:09:35 +00:00
|
|
|
return false;
|
|
|
|
}
|
2019-06-17 23:41:03 +00:00
|
|
|
|
2019-10-18 02:39:22 +00:00
|
|
|
switch (in_data->header.command_id) {
|
|
|
|
case 0:
|
|
|
|
case 1:
|
|
|
|
if (request.meta.num_send_buffers != 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
if (request.meta.num_send_buffers != 1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return false;
|
2019-07-19 02:09:35 +00:00
|
|
|
}
|
2019-06-17 23:41:03 +00:00
|
|
|
|
2019-10-24 08:40:44 +00:00
|
|
|
if (in_data->result.GetValue() != result.GetValue()) {
|
2019-07-19 02:09:35 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We found our caller. */
|
|
|
|
return true;
|
2018-11-14 11:23:28 +00:00
|
|
|
}
|
2019-06-17 23:41:03 +00:00
|
|
|
|
2019-07-19 02:09:35 +00:00
|
|
|
bool TryGuessBaseAddress(u64 *out_base_address, u32 debug_handle, u64 guess) {
|
|
|
|
MemoryInfo mi;
|
|
|
|
u32 pi;
|
|
|
|
if (R_FAILED(svcQueryDebugProcessMemory(&mi, &pi, debug_handle, guess)) || mi.perm != Perm_Rx) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-06-17 23:41:03 +00:00
|
|
|
|
2019-07-19 02:09:35 +00:00
|
|
|
/* Iterate backwards until we find the memory before the code region. */
|
|
|
|
while (mi.addr > 0) {
|
|
|
|
if (R_FAILED(svcQueryDebugProcessMemory(&mi, &pi, debug_handle, guess))) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-06-17 23:41:03 +00:00
|
|
|
|
2019-07-19 02:09:35 +00:00
|
|
|
if (mi.type == MemType_Unmapped) {
|
|
|
|
/* Code region will be at the end of the unmapped region preceding it. */
|
|
|
|
*out_base_address = mi.addr + mi.size;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
guess = mi.addr - 4;
|
|
|
|
}
|
2019-06-17 23:41:03 +00:00
|
|
|
|
2018-11-14 11:23:28 +00:00
|
|
|
return false;
|
|
|
|
}
|
2019-06-17 23:41:03 +00:00
|
|
|
|
2019-07-19 02:09:35 +00:00
|
|
|
u64 GetBaseAddress(const ThrowContext *throw_ctx, const ThreadContext *thread_ctx, u32 debug_handle) {
|
|
|
|
u64 base_address = 0;
|
|
|
|
|
|
|
|
if (TryGuessBaseAddress(&base_address, debug_handle, thread_ctx->pc.x)) {
|
|
|
|
return base_address;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (TryGuessBaseAddress(&base_address, debug_handle, thread_ctx->lr)) {
|
|
|
|
return base_address;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t i = 0; i < throw_ctx->cpu_ctx.aarch64_ctx.stack_trace_size; i++) {
|
|
|
|
if (TryGuessBaseAddress(&base_address, debug_handle, throw_ctx->cpu_ctx.aarch64_ctx.stack_trace[i])) {
|
|
|
|
return base_address;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return base_address;
|
2018-11-14 11:23:28 +00:00
|
|
|
}
|
2019-06-17 23:41:03 +00:00
|
|
|
|
2019-07-19 02:09:35 +00:00
|
|
|
}
|
2018-11-14 11:23:28 +00:00
|
|
|
|
2019-10-18 02:39:22 +00:00
|
|
|
void TryCollectDebugInformation(ThrowContext *ctx, os::ProcessId process_id) {
|
2019-07-19 02:09:35 +00:00
|
|
|
/* Try to debug the process. This may fail, if we called into ourself. */
|
2020-12-09 14:45:19 +00:00
|
|
|
Handle debug_handle;
|
|
|
|
if (R_FAILED(svcDebugActiveProcess(std::addressof(debug_handle), static_cast<u64>(process_id)))) {
|
2019-07-19 02:09:35 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-12-09 14:45:19 +00:00
|
|
|
ON_SCOPE_EXIT { R_ABORT_UNLESS(svc::CloseHandle(debug_handle)); };
|
2019-06-17 23:41:03 +00:00
|
|
|
|
2018-11-14 11:23:28 +00:00
|
|
|
/* First things first, check if process is 64 bits, and get list of thread infos. */
|
2021-01-18 16:48:47 +00:00
|
|
|
ResetThreadTlsMap();
|
2018-11-14 11:23:28 +00:00
|
|
|
{
|
2020-07-31 08:27:09 +00:00
|
|
|
bool got_create_process = false;
|
2019-07-19 02:09:35 +00:00
|
|
|
svc::DebugEventInfo d;
|
2020-12-09 14:45:19 +00:00
|
|
|
while (R_SUCCEEDED(svcGetDebugEvent(reinterpret_cast<u8 *>(&d), debug_handle))) {
|
2019-07-19 02:09:35 +00:00
|
|
|
switch (d.type) {
|
2020-07-31 08:27:09 +00:00
|
|
|
case svc::DebugEvent_CreateProcess:
|
|
|
|
ctx->cpu_ctx.architecture = (d.info.create_process.flags & 1) ? CpuContext::Architecture_Aarch64 : CpuContext::Architecture_Aarch32;
|
|
|
|
std::memcpy(ctx->proc_name, d.info.create_process.name, sizeof(d.info.create_process.name));
|
|
|
|
got_create_process = true;
|
2019-07-19 02:09:35 +00:00
|
|
|
break;
|
2020-07-31 08:27:09 +00:00
|
|
|
case svc::DebugEvent_CreateThread:
|
2021-01-18 16:48:47 +00:00
|
|
|
SetThreadTls(d.info.create_thread.thread_id, d.info.create_thread.tls_address);
|
2019-07-19 02:09:35 +00:00
|
|
|
break;
|
2020-01-18 04:11:03 +00:00
|
|
|
case svc::DebugEvent_Exception:
|
|
|
|
case svc::DebugEvent_ExitProcess:
|
|
|
|
case svc::DebugEvent_ExitThread:
|
2019-07-19 02:09:35 +00:00
|
|
|
break;
|
2018-11-14 11:23:28 +00:00
|
|
|
}
|
|
|
|
}
|
2019-06-17 23:41:03 +00:00
|
|
|
|
2020-07-31 08:27:09 +00:00
|
|
|
if (!got_create_process) {
|
2018-11-14 11:23:28 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2019-06-17 23:41:03 +00:00
|
|
|
|
2018-11-14 11:23:28 +00:00
|
|
|
/* TODO: Try to collect information on 32-bit fatals. This shouldn't really matter for any real use case. */
|
2019-07-19 02:09:35 +00:00
|
|
|
if (ctx->cpu_ctx.architecture == CpuContext::Architecture_Aarch32) {
|
2018-11-14 11:23:28 +00:00
|
|
|
return;
|
|
|
|
}
|
2019-06-17 23:41:03 +00:00
|
|
|
|
2019-07-19 02:09:35 +00:00
|
|
|
/* Welcome to hell. Here, we try to identify which thread called into fatal. */
|
2018-11-14 11:23:28 +00:00
|
|
|
bool found_fatal_caller = false;
|
|
|
|
u64 thread_id = 0;
|
2019-12-31 23:23:25 +00:00
|
|
|
u64 thread_tls = 0;
|
2018-11-14 11:23:28 +00:00
|
|
|
ThreadContext thread_ctx;
|
|
|
|
{
|
|
|
|
/* We start by trying to get a list of threads. */
|
2020-03-29 22:24:40 +00:00
|
|
|
s32 thread_count;
|
2018-11-14 11:23:28 +00:00
|
|
|
u64 thread_ids[0x60];
|
2020-12-09 14:45:19 +00:00
|
|
|
if (R_FAILED(svc::GetThreadList(&thread_count, thread_ids, 0x60, debug_handle))) {
|
2018-11-14 11:23:28 +00:00
|
|
|
return;
|
|
|
|
}
|
2019-06-17 23:41:03 +00:00
|
|
|
|
2018-11-14 11:23:28 +00:00
|
|
|
/* We need to locate the thread that's called fatal. */
|
2020-03-29 22:24:40 +00:00
|
|
|
for (s32 i = 0; i < thread_count; i++) {
|
2018-11-14 11:23:28 +00:00
|
|
|
const u64 cur_thread_id = thread_ids[i];
|
2021-01-18 16:48:47 +00:00
|
|
|
u64 cur_thread_tls;
|
|
|
|
if (!GetThreadTls(std::addressof(cur_thread_tls), cur_thread_id)) {
|
2018-11-14 11:23:28 +00:00
|
|
|
continue;
|
|
|
|
}
|
2019-06-17 23:41:03 +00:00
|
|
|
|
2021-01-18 16:48:47 +00:00
|
|
|
if (IsThreadFatalCaller(ctx->result, debug_handle, cur_thread_id, cur_thread_tls, &thread_ctx)) {
|
|
|
|
thread_id = cur_thread_id;
|
|
|
|
thread_tls = cur_thread_tls;
|
2018-11-14 11:23:28 +00:00
|
|
|
found_fatal_caller = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!found_fatal_caller) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2020-12-09 14:45:19 +00:00
|
|
|
if (R_FAILED(svcGetDebugThreadContext(&thread_ctx, debug_handle, thread_id, svc::ThreadContextFlag_All))) {
|
2018-11-14 11:23:28 +00:00
|
|
|
return;
|
|
|
|
}
|
2019-06-17 23:41:03 +00:00
|
|
|
|
2019-07-19 02:09:35 +00:00
|
|
|
/* Set register states. */
|
|
|
|
ctx->cpu_ctx.aarch64_ctx.SetRegisterValue(aarch64::RegisterName_FP, thread_ctx.fp);
|
|
|
|
ctx->cpu_ctx.aarch64_ctx.SetRegisterValue(aarch64::RegisterName_LR, thread_ctx.lr);
|
|
|
|
ctx->cpu_ctx.aarch64_ctx.SetRegisterValue(aarch64::RegisterName_SP, thread_ctx.sp);
|
|
|
|
ctx->cpu_ctx.aarch64_ctx.SetRegisterValue(aarch64::RegisterName_PC, thread_ctx.pc.x);
|
2018-11-14 11:23:28 +00:00
|
|
|
|
|
|
|
/* Parse a stack trace. */
|
|
|
|
u64 cur_fp = thread_ctx.fp;
|
2019-07-19 02:09:35 +00:00
|
|
|
ctx->cpu_ctx.aarch64_ctx.stack_trace_size = 0;
|
|
|
|
for (unsigned int i = 0; i < aarch64::CpuContext::MaxStackTraceDepth; i++) {
|
2018-11-14 11:23:28 +00:00
|
|
|
/* Validate the current frame. */
|
|
|
|
if (cur_fp == 0 || (cur_fp & 0xF)) {
|
|
|
|
break;
|
|
|
|
}
|
2019-06-17 23:41:03 +00:00
|
|
|
|
2018-11-14 11:23:28 +00:00
|
|
|
/* Read a new frame. */
|
2019-07-19 02:09:35 +00:00
|
|
|
StackFrame cur_frame = {};
|
2020-12-09 14:45:19 +00:00
|
|
|
if (R_FAILED(svcReadDebugProcessMemory(&cur_frame, debug_handle, cur_fp, sizeof(StackFrame)))) {
|
2018-11-14 11:23:28 +00:00
|
|
|
break;
|
|
|
|
}
|
2019-06-17 23:41:03 +00:00
|
|
|
|
2018-11-14 11:23:28 +00:00
|
|
|
/* Advance to the next frame. */
|
|
|
|
ctx->cpu_ctx.aarch64_ctx.stack_trace[ctx->cpu_ctx.aarch64_ctx.stack_trace_size++] = cur_frame.lr;
|
|
|
|
cur_fp = cur_frame.fp;
|
|
|
|
}
|
2019-06-17 23:41:03 +00:00
|
|
|
|
2018-11-14 22:13:31 +00:00
|
|
|
/* Try to read up to 0x100 of stack. */
|
2019-12-31 23:23:25 +00:00
|
|
|
ctx->stack_dump_base = 0;
|
2018-11-14 22:13:31 +00:00
|
|
|
for (size_t sz = 0x100; sz > 0; sz -= 0x10) {
|
2020-12-09 14:45:19 +00:00
|
|
|
if (R_SUCCEEDED(svcReadDebugProcessMemory(ctx->stack_dump, debug_handle, thread_ctx.sp, sz))) {
|
2019-12-31 23:23:25 +00:00
|
|
|
ctx->stack_dump_base = thread_ctx.sp;
|
2019-07-19 02:09:35 +00:00
|
|
|
ctx->stack_dump_size = sz;
|
2018-11-14 22:13:31 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2019-03-26 16:04:32 +00:00
|
|
|
|
2019-12-31 23:23:25 +00:00
|
|
|
/* Try to read the first 0x100 of TLS. */
|
2020-12-09 14:45:19 +00:00
|
|
|
if (R_SUCCEEDED(svcReadDebugProcessMemory(ctx->tls_dump, debug_handle, thread_tls, sizeof(ctx->tls_dump)))) {
|
2019-12-31 23:23:25 +00:00
|
|
|
ctx->tls_address = thread_tls;
|
|
|
|
} else {
|
|
|
|
ctx->tls_address = 0;
|
|
|
|
std::memset(ctx->tls_dump, 0xCC, sizeof(ctx->tls_dump));
|
|
|
|
}
|
|
|
|
|
2019-07-19 02:09:35 +00:00
|
|
|
/* Parse the base address. */
|
2020-12-09 14:45:19 +00:00
|
|
|
ctx->cpu_ctx.aarch64_ctx.SetBaseAddress(GetBaseAddress(ctx, &thread_ctx, debug_handle));
|
2018-11-14 11:23:28 +00:00
|
|
|
}
|
2019-07-19 02:09:35 +00:00
|
|
|
|
|
|
|
}
|