1
0
Fork 0
mirror of https://github.com/Atmosphere-NX/Atmosphere.git synced 2024-11-16 00:47:00 +00:00
Atmosphere/libraries/libmesosphere/source/kern_panic.cpp

165 lines
5.8 KiB
C++
Raw Normal View History

/*
* Copyright (c) Atmosphère-NX
*
* 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/>.
*/
#include <mesosphere.hpp>
2020-02-07 14:26:01 +00:00
extern "C" void _start();
namespace ams::result::impl {
NORETURN void OnResultAssertion(Result result) {
MESOSPHERE_PANIC("OnResultAssertion(2%03d-%04d)", result.GetModule(), result.GetDescription());
}
}
namespace ams::kern {
2020-09-17 00:44:31 +01:00
/* NOTE: This is not exposed via a header, but is referenced via assembly. */
/* NOTE: Nintendo does not save register contents on panic; we use this */
/* to generate an atmosphere fatal report on panic. */
constinit KExceptionContext g_panic_exception_contexts[cpu::NumCores];
namespace {
2020-02-14 12:01:09 +00:00
constexpr std::array<s32, cpu::NumCores> NegativeArray = [] {
std::array<s32, cpu::NumCores> arr = {};
for (size_t i = 0; i < arr.size(); i++) {
arr[i] = -1;
}
return arr;
}();
2020-02-07 14:26:01 +00:00
constinit util::Atomic<s32> g_next_ticket = 0;
constinit util::Atomic<s32> g_current_ticket = 0;
2020-02-14 12:01:09 +00:00
constinit std::array<s32, cpu::NumCores> g_core_tickets = NegativeArray;
2020-02-14 12:01:09 +00:00
s32 GetCoreTicket() {
const s32 core_id = GetCurrentCoreId();
if (g_core_tickets[core_id] == -1) {
g_core_tickets[core_id] = 2 * (g_next_ticket++);
2020-02-07 14:26:01 +00:00
}
2020-02-14 12:01:09 +00:00
return g_core_tickets[core_id];
}
void WaitCoreTicket() {
const s32 expected = GetCoreTicket();
const s32 desired = expected + 1;
s32 compare = g_current_ticket.Load<std::memory_order_relaxed>();
2020-02-14 12:01:09 +00:00
do {
if (compare == desired) {
break;
}
compare = expected;
} while (!g_current_ticket.CompareExchangeWeak(compare, desired));
2020-02-14 12:01:09 +00:00
}
void ReleaseCoreTicket() {
const s32 expected = GetCoreTicket() + 1;
const s32 desired = expected + 1;
s32 compare = expected;
g_current_ticket.CompareExchangeStrong(compare, desired);
2020-02-14 12:01:09 +00:00
}
2020-09-17 00:44:31 +01:00
ALWAYS_INLINE KExceptionContext *GetPanicExceptionContext(int core_id) {
#if defined(MESOSPHERE_ENABLE_PANIC_REGISTER_DUMP)
return std::addressof(g_panic_exception_contexts[core_id]);
#else
return nullptr;
#endif
}
2020-02-14 12:01:09 +00:00
[[gnu::unused]] void PrintCurrentState() {
/* Wait for it to be our turn to print. */
WaitCoreTicket();
2020-09-17 00:44:31 +01:00
/* Get the current exception context. */
const s32 core_id = GetCurrentCoreId();
const auto *core_ctx = GetPanicExceptionContext(core_id);
2020-02-14 12:01:09 +00:00
2020-09-17 00:44:31 +01:00
/* Print the state. */
MESOSPHERE_RELEASE_LOG("Core[%d] Current State:\n", core_id);
2020-02-14 12:01:09 +00:00
2022-03-22 21:11:35 +00:00
/* Print kernel state. */
#ifdef ATMOSPHERE_ARCH_ARM64
2022-03-22 21:11:35 +00:00
MESOSPHERE_RELEASE_LOG("Kernel Registers:\n");
for (size_t i = 0; i < 31; i++) {
MESOSPHERE_RELEASE_LOG(" X[%02zu] = %016lx\n", i, core_ctx->x[i]);
}
MESOSPHERE_RELEASE_LOG(" SP = %016lx\n", core_ctx->sp);
/* Print kernel backtrace. */
2022-03-22 21:11:35 +00:00
MESOSPHERE_RELEASE_LOG("Kernel Backtrace:\n");
2020-09-17 00:44:31 +01:00
uintptr_t fp = core_ctx != nullptr ? core_ctx->x[29] : reinterpret_cast<uintptr_t>(__builtin_frame_address(0));
2020-02-14 12:01:09 +00:00
for (size_t i = 0; i < 32 && fp && util::IsAligned(fp, 0x10) && cpu::GetPhysicalAddressWritable(nullptr, fp, true); i++) {
struct {
uintptr_t fp;
uintptr_t lr;
} *stack_frame = reinterpret_cast<decltype(stack_frame)>(fp);
MESOSPHERE_RELEASE_LOG(" [%02zx]: %p\n", i, reinterpret_cast<void *>(stack_frame->lr));
2020-02-14 12:01:09 +00:00
fp = stack_frame->fp;
}
#endif
2022-03-22 21:11:35 +00:00
/* Print registers and user backtrace. */
KDebug::PrintRegister();
KDebug::PrintBacktrace();
2020-02-14 12:01:09 +00:00
MESOSPHERE_RELEASE_LOG("\n");
/* Allow the next core to print. */
ReleaseCoreTicket();
2020-02-07 14:26:01 +00:00
}
NORETURN void StopSystem() {
2020-02-07 14:26:01 +00:00
#ifdef MESOSPHERE_BUILD_FOR_DEBUGGING
2020-02-14 12:01:09 +00:00
/* Print the current core. */
2020-02-07 14:26:01 +00:00
PrintCurrentState();
#endif
2020-09-17 00:44:31 +01:00
KSystemControl::StopSystem(GetPanicExceptionContext(GetCurrentCoreId()));
}
}
NORETURN WEAK_SYMBOL void PanicImpl(const char *file, int line, const char *format, ...) {
2020-02-07 14:26:01 +00:00
#ifdef MESOSPHERE_BUILD_FOR_DEBUGGING
2020-02-14 12:01:09 +00:00
/* Wait for it to be our turn to print. */
WaitCoreTicket();
::std::va_list vl;
va_start(vl, format);
MESOSPHERE_RELEASE_LOG("Core[%d]: Kernel Panic at %s:%d\n", GetCurrentCoreId(), file, line);
if (KProcess *cur_process = GetCurrentProcessPointer(); cur_process != nullptr) {
MESOSPHERE_RELEASE_LOG("Core[%d]: Current Process: %s\n", GetCurrentCoreId(), cur_process->GetName());
}
2020-02-14 12:01:09 +00:00
MESOSPHERE_RELEASE_VLOG(format, vl);
MESOSPHERE_RELEASE_LOG("\n");
va_end(vl);
2020-08-17 22:20:24 +01:00
#else
MESOSPHERE_UNUSED(file, line, format);
2020-02-07 14:26:01 +00:00
#endif
StopSystem();
}
NORETURN WEAK_SYMBOL void PanicImpl() {
StopSystem();
}
}