diff --git a/libraries/libmesosphere/include/mesosphere/arch/arm64/kern_cpu.hpp b/libraries/libmesosphere/include/mesosphere/arch/arm64/kern_cpu.hpp index ace16769f..89dec0318 100644 --- a/libraries/libmesosphere/include/mesosphere/arch/arm64/kern_cpu.hpp +++ b/libraries/libmesosphere/include/mesosphere/arch/arm64/kern_cpu.hpp @@ -59,6 +59,51 @@ namespace ams::kern::arm64::cpu { EnsureInstructionConsistency(); } + /* Helper for address access. */ + ALWAYS_INLINE bool GetPhysicalAddressWritable(KPhysicalAddress *out, KVirtualAddress addr, bool privileged = false) { + const uintptr_t va = GetInteger(addr); + + if (privileged) { + __asm__ __volatile__("at s1e1w, %[va]" :: [va]"r"(va) : "memory"); + } else { + __asm__ __volatile__("at s1e0w, %[va]" :: [va]"r"(va) : "memory"); + } + InstructionMemoryBarrier(); + + u64 par = GetParEl1(); + + if (par & 0x1) { + return false; + } + + if (out) { + *out = KPhysicalAddress((par & 0xFFFFFFFFF000ull) | (va & 0xFFFull)); + } + return true; + } + + ALWAYS_INLINE bool GetPhysicalAddressReadable(KPhysicalAddress *out, KVirtualAddress addr, bool privileged = false) { + const uintptr_t va = GetInteger(addr); + + if (privileged) { + __asm__ __volatile__("at s1e1r, %[va]" :: [va]"r"(va) : "memory"); + } else { + __asm__ __volatile__("at s1e0r, %[va]" :: [va]"r"(va) : "memory"); + } + InstructionMemoryBarrier(); + + u64 par = GetParEl1(); + + if (par & 0x1) { + return false; + } + + if (out) { + *out = KPhysicalAddress((par & 0xFFFFFFFFF000ull) | (va & 0xFFFull)); + } + return true; + } + /* Synchronization helpers. */ NOINLINE void SynchronizeAllCores(); diff --git a/libraries/libmesosphere/include/mesosphere/arch/arm64/kern_cpu_system_registers.hpp b/libraries/libmesosphere/include/mesosphere/arch/arm64/kern_cpu_system_registers.hpp index 3d20e786a..8cca7ec86 100644 --- a/libraries/libmesosphere/include/mesosphere/arch/arm64/kern_cpu_system_registers.hpp +++ b/libraries/libmesosphere/include/mesosphere/arch/arm64/kern_cpu_system_registers.hpp @@ -43,6 +43,8 @@ namespace ams::kern::arm64::cpu { MESOSPHERE_CPU_DEFINE_SYSREG_ACCESSORS(VbarEl1, vbar_el1) + MESOSPHERE_CPU_DEFINE_SYSREG_ACCESSORS(ParEl1, par_el1) + MESOSPHERE_CPU_DEFINE_SYSREG_ACCESSORS(SctlrEl1, sctlr_el1) MESOSPHERE_CPU_DEFINE_SYSREG_ACCESSORS(CpuActlrEl1, s3_1_c15_c2_0) diff --git a/libraries/libmesosphere/include/mesosphere/kern_debug_log.hpp b/libraries/libmesosphere/include/mesosphere/kern_debug_log.hpp index 53352dcf7..1611e68a3 100644 --- a/libraries/libmesosphere/include/mesosphere/kern_debug_log.hpp +++ b/libraries/libmesosphere/include/mesosphere/kern_debug_log.hpp @@ -24,7 +24,7 @@ namespace ams::kern { public: static NOINLINE void Initialize(); - static NOINLINE void Printf(const char *format, ...); + static NOINLINE void Printf(const char *format, ...) __attribute__((format(printf, 1, 2))); static NOINLINE void VPrintf(const char *format, ::std::va_list vl); }; diff --git a/libraries/libmesosphere/include/mesosphere/kern_panic.hpp b/libraries/libmesosphere/include/mesosphere/kern_panic.hpp index af10aef9b..8f5937cae 100644 --- a/libraries/libmesosphere/include/mesosphere/kern_panic.hpp +++ b/libraries/libmesosphere/include/mesosphere/kern_panic.hpp @@ -19,7 +19,7 @@ namespace ams::kern { - NORETURN void Panic(const char *file, int line, const char *format, ...); + NORETURN void Panic(const char *file, int line, const char *format, ...) __attribute__((format(printf, 3, 4))); NORETURN void Panic(); } @@ -31,11 +31,12 @@ namespace ams::kern { #endif #ifdef MESOSPHERE_ENABLE_ASSERTIONS -#define MESOSPHERE_ASSERT_IMPL(expr, ...) \ - ({ \ - if (AMS_UNLIKELY(!(expr))) { \ - MESOSPHERE_PANIC(__VA_ARGS__); \ - } \ +#define MESOSPHERE_ASSERT_IMPL(expr, ...) \ + ({ \ + const bool __tmp_meso_assert_val = (expr); \ + if (AMS_UNLIKELY(!__tmp_meso_assert_val)) { \ + MESOSPHERE_PANIC(__VA_ARGS__); \ + } \ }) #else #define MESOSPHERE_ASSERT_IMPL(expr, ...) do { static_cast(expr); } while (0) @@ -56,14 +57,16 @@ namespace ams::kern { #define MESOSPHERE_ABORT_UNLESS(expr) \ ({ \ - if (AMS_UNLIKELY(!(expr))) { \ + const bool _tmp_meso_assert_val = (expr); \ + if (AMS_UNLIKELY(!_tmp_meso_assert_val)) { \ MESOSPHERE_PANIC("Abort(): %s", #expr); \ } \ }) #define MESOSPHERE_INIT_ABORT_UNLESS(expr) \ ({ \ - if (AMS_UNLIKELY(!(expr))) { \ + const bool __tmp_meso_assert_val = (expr); \ + if (AMS_UNLIKELY(!__tmp_meso_assert_val)) { \ MESOSPHERE_INIT_ABORT(); \ } \ }) diff --git a/libraries/libmesosphere/source/init/kern_init_slab_setup.cpp b/libraries/libmesosphere/source/init/kern_init_slab_setup.cpp index 108ab9205..c04409f06 100644 --- a/libraries/libmesosphere/source/init/kern_init_slab_setup.cpp +++ b/libraries/libmesosphere/source/init/kern_init_slab_setup.cpp @@ -201,8 +201,7 @@ namespace ams::kern::init { case KSlabType_KThread: address = InitializeSlabHeap(address, SLAB_COUNT(KThread)); break; - default: - MESOSPHERE_ABORT(); + MESOSPHERE_UNREACHABLE_DEFAULT_CASE(); } } } diff --git a/libraries/libmesosphere/source/kern_k_thread.cpp b/libraries/libmesosphere/source/kern_k_thread.cpp index c4e63abca..ab7413e7a 100644 --- a/libraries/libmesosphere/source/kern_k_thread.cpp +++ b/libraries/libmesosphere/source/kern_k_thread.cpp @@ -50,8 +50,8 @@ namespace ams::kern { [[fallthrough]]; case ThreadType_User: { - MESOSPHERE_ASSERT((owner == nullptr) || (owner->GetCoreMask() | (1ul << core)) == owner->GetCoreMask()); - MESOSPHERE_ASSERT((owner == nullptr) || (owner->GetPriorityMask() | (1ul << prio)) == owner->GetPriorityMask()); + MESOSPHERE_ASSERT(((owner == nullptr) || (owner->GetCoreMask() | (1ul << core)) == owner->GetCoreMask())); + MESOSPHERE_ASSERT(((owner == nullptr) || (owner->GetPriorityMask() | (1ul << prio)) == owner->GetPriorityMask())); } break; default: diff --git a/libraries/libmesosphere/source/kern_panic.cpp b/libraries/libmesosphere/source/kern_panic.cpp index 0e7c952da..eb58d853c 100644 --- a/libraries/libmesosphere/source/kern_panic.cpp +++ b/libraries/libmesosphere/source/kern_panic.cpp @@ -15,6 +15,8 @@ */ #include +extern "C" void _start(); + namespace ams::result::impl { NORETURN void OnResultAssertion(Result result) { @@ -27,14 +29,55 @@ namespace ams::kern { namespace { + size_t g_panic_count = 0; + + [[gnu::unused]] void PrintCurrentState() { + if (g_panic_count == 1) { + g_panic_count++; + + MESOSPHERE_RELEASE_LOG("Base Address: %p\n", _start); + + /* TODO: Dump register state. */ + + #ifdef ATMOSPHERE_ARCH_ARM64 + MESOSPHERE_RELEASE_LOG("Backtrace:\n"); + uintptr_t fp = reinterpret_cast(__builtin_frame_address(0)); + 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(fp); + MESOSPHERE_RELEASE_LOG(" [%02zx]: %p\n", i, reinterpret_cast(stack_frame->lr)); + fp = stack_frame->fp; + } + #endif + } + } + NORETURN void StopSystem() { + #ifdef MESOSPHERE_BUILD_FOR_DEBUGGING + PrintCurrentState(); + #endif + KSystemControl::StopSystem(); } } NORETURN WEAK_SYMBOL void Panic(const char *file, int line, const char *format, ...) { - /* TODO: Implement printing, log this information. */ + #ifdef MESOSPHERE_BUILD_FOR_DEBUGGING + if (g_panic_count == 0) { + g_panic_count++; + + ::std::va_list vl; + va_start(vl, format); + MESOSPHERE_RELEASE_LOG("KernelPanic (Core %d): %s:%d\n", GetCurrentCoreId(), file, line); + MESOSPHERE_RELEASE_VLOG(format, vl); + MESOSPHERE_RELEASE_LOG("\n"); + va_end(vl); + } + #endif + StopSystem(); }