1
0
Fork 0
mirror of https://github.com/Atmosphere-NX/Atmosphere.git synced 2024-12-25 19:56:03 +00:00

kern: improve panic output

This commit is contained in:
Michael Scire 2020-02-14 04:01:09 -08:00
parent 5b98426171
commit b3e6571586

View file

@ -29,13 +29,56 @@ namespace ams::kern {
namespace { namespace {
size_t g_panic_count = 0; 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;
}();
std::atomic<s32> g_next_ticket = 0;
std::atomic<s32> g_current_ticket = 0;
std::array<s32, cpu::NumCores> g_core_tickets = NegativeArray;
s32 GetCoreTicket() {
const s32 core_id = GetCurrentCoreId();
if (g_core_tickets[core_id] == -1) {
g_core_tickets[core_id] = 2 * g_next_ticket.fetch_add(1);
}
return g_core_tickets[core_id];
}
void WaitCoreTicket() {
const s32 expected = GetCoreTicket();
const s32 desired = expected + 1;
s32 compare = g_current_ticket;
do {
if (compare == desired) {
break;
}
compare = expected;
} while (!g_current_ticket.compare_exchange_weak(compare, desired));
}
void ReleaseCoreTicket() {
const s32 expected = GetCoreTicket() + 1;
const s32 desired = expected + 1;
s32 compare = g_current_ticket;
do {
if (compare != expected) {
break;
}
} while (!g_current_ticket.compare_exchange_weak(compare, desired));
}
[[gnu::unused]] void PrintCurrentState() { [[gnu::unused]] void PrintCurrentState() {
if (g_panic_count == 1) { /* Wait for it to be our turn to print. */
g_panic_count++; WaitCoreTicket();
MESOSPHERE_RELEASE_LOG("Base Address: %p\n", _start); const s32 core_id = GetCurrentCoreId();
MESOSPHERE_RELEASE_LOG("Core[%d] Current State:\n", core_id);
/* TODO: Dump register state. */ /* TODO: Dump register state. */
@ -51,11 +94,16 @@ namespace ams::kern {
fp = stack_frame->fp; fp = stack_frame->fp;
} }
#endif #endif
}
MESOSPHERE_RELEASE_LOG("\n");
/* Allow the next core to print. */
ReleaseCoreTicket();
} }
NORETURN void StopSystem() { NORETURN void StopSystem() {
#ifdef MESOSPHERE_BUILD_FOR_DEBUGGING #ifdef MESOSPHERE_BUILD_FOR_DEBUGGING
/* Print the current core. */
PrintCurrentState(); PrintCurrentState();
#endif #endif
@ -66,16 +114,15 @@ namespace ams::kern {
NORETURN WEAK_SYMBOL void Panic(const char *file, int line, const char *format, ...) { NORETURN WEAK_SYMBOL void Panic(const char *file, int line, const char *format, ...) {
#ifdef MESOSPHERE_BUILD_FOR_DEBUGGING #ifdef MESOSPHERE_BUILD_FOR_DEBUGGING
if (g_panic_count == 0) { /* Wait for it to be our turn to print. */
g_panic_count++; WaitCoreTicket();
::std::va_list vl; ::std::va_list vl;
va_start(vl, format); va_start(vl, format);
MESOSPHERE_RELEASE_LOG("KernelPanic (Core %d): %s:%d\n", GetCurrentCoreId(), file, line); MESOSPHERE_RELEASE_LOG("Core[%d]: Kernel Panic at %s:%d\n", GetCurrentCoreId(), file, line);
MESOSPHERE_RELEASE_VLOG(format, vl); MESOSPHERE_RELEASE_VLOG(format, vl);
MESOSPHERE_RELEASE_LOG("\n"); MESOSPHERE_RELEASE_LOG("\n");
va_end(vl); va_end(vl);
}
#endif #endif
StopSystem(); StopSystem();