From 3cfd412e6a2d7659d3636ff2fce5328017ca34d8 Mon Sep 17 00:00:00 2001 From: Michael Scire Date: Sat, 14 Nov 2020 12:17:38 -0800 Subject: [PATCH] libexo/sc7fw: support release/debug/audit --- config/templates/exosphere.mk | 2 +- libexosphere/Makefile | 12 +-- .../diag/diag_detailed_assertion_impl.inc | 93 ++++++++++++++++ libexosphere/include/exosphere/log.hpp | 22 +++- .../include/exosphere/secmon/secmon_log.hpp | 20 ++++ .../source/libc/libgcc_division.arch.arm.c | 100 ++++++++++++++++++ .../libc/libgcc_division_asm.arch.arm.s | 37 +++++++ .../source/libc/libgcc_thumb_case.arch.arm.s | 19 ++++ libexosphere/source/log/log_api.cpp | 43 ++++++-- .../source/diag/diag_assertion_impl.cpp | 14 +-- libvapours/include/vapours/assert.hpp | 2 +- libvapours/include/vapours/common.hpp | 24 ++++- 12 files changed, 361 insertions(+), 27 deletions(-) create mode 100644 libexosphere/include/exosphere/diag/diag_detailed_assertion_impl.inc create mode 100644 libexosphere/include/exosphere/secmon/secmon_log.hpp diff --git a/config/templates/exosphere.mk b/config/templates/exosphere.mk index e2629d9f..6387d898 100644 --- a/config/templates/exosphere.mk +++ b/config/templates/exosphere.mk @@ -37,7 +37,7 @@ export CXXWRAPS := -Wl,--wrap,__cxa_pure_virtual \ -Wl,--wrap,_ZSt20__throw_length_errorPKc \ -Wl,--wrap,_ZNSt11logic_errorC2EPKc -export LIBS := -lexosphere +export LIBS := -l$(LIBEXOSPHERE_NAME) #--------------------------------------------------------------------------------- # list of directories containing libraries, this must be the top level containing diff --git a/libexosphere/Makefile b/libexosphere/Makefile index 7cc93f20..de8a0a44 100644 --- a/libexosphere/Makefile +++ b/libexosphere/Makefile @@ -6,16 +6,16 @@ define ATMOSPHERE_ADD_TARGET ATMOSPHERE_BUILD_CONFIGS += $(strip $1) arm64-$(strip $1): - $$(MAKE) -f arm64.mk $(strip $1) + @$$(MAKE) -f arm64.mk $(strip $1) arm-$(strip $1): - $$(MAKE) -f arm.mk $(strip $1) + @$$(MAKE) -f arm.mk $(strip $1) clean-arm64-$(strip $1): - $$(MAKE) -f arm64.mk clean-$(strip $1) + @$$(MAKE) -f arm64.mk clean-$(strip $1) clean-arm-$(strip $1): - $$(MAKE) -f arm.mk clean-$(strip $1) + @$$(MAKE) -f arm.mk clean-$(strip $1) endef @@ -24,10 +24,10 @@ $(eval $(call ATMOSPHERE_ADD_TARGET, debug)) $(eval $(call ATMOSPHERE_ADD_TARGET, audit)) clean-arm64: - $(MAKE) -f arm64.mk clean + @$(MAKE) -f arm64.mk clean clean-arm: - $(MAKE) -f arm.mk clean + @$(MAKE) -f arm.mk clean clean: clean-arm64 clean-arm diff --git a/libexosphere/include/exosphere/diag/diag_detailed_assertion_impl.inc b/libexosphere/include/exosphere/diag/diag_detailed_assertion_impl.inc new file mode 100644 index 00000000..52eab968 --- /dev/null +++ b/libexosphere/include/exosphere/diag/diag_detailed_assertion_impl.inc @@ -0,0 +1,93 @@ +/* + * Copyright (c) 2018-2020 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 . + */ + +void AbortImpl(const char *file, int line, const char *func, const char *expr, u64 value) { + #if defined(AMS_ENABLE_DETAILED_ASSERTIONS) + { + AMS_LOG("Abort Called\n"); + AMS_LOG(" Location: %s:%d\n", file, line); + AMS_LOG(" Function: %s\n", func); + AMS_LOG(" Expression: %s\n", expr); + AMS_LOG(" Value: %016" PRIx64 "\n", value); + AMS_LOG("\n"); + } + #else + AMS_UNUSED(file, line, func, expr, value); + #endif + AbortImpl(); +} + +void AbortImpl(const char *file, int line, const char *func, const char *expr, u64 value, const char *format, ...) { + #if defined(AMS_ENABLE_DETAILED_ASSERTIONS) + { + AMS_LOG("Abort Called\n"); + AMS_LOG(" Location: %s:%d\n", file, line); + AMS_LOG(" Function: %s\n", func); + AMS_LOG(" Expression: %s\n", expr); + AMS_LOG(" Value: %016" PRIx64 "\n", value); + AMS_LOG("\n"); + { + ::std::va_list vl; + va_start(vl, format); + AMS_VLOG(format, vl); + va_end(vl); + AMS_LOG("\n"); + } + } + #else + AMS_UNUSED(file, line, func, expr, value, format); + #endif + AbortImpl(); +} + +void AssertionFailureImpl(const char *file, int line, const char *func, const char *expr, u64 value) { + #if defined(AMS_ENABLE_DETAILED_ASSERTIONS) + { + AMS_LOG("Assertion Failure\n"); + AMS_LOG(" Location: %s:%d\n", file, line); + AMS_LOG(" Function: %s\n", func); + AMS_LOG(" Expression: %s\n", expr); + AMS_LOG(" Value: %016" PRIx64 "\n", value); + AMS_LOG("\n"); + } + #else + AMS_UNUSED(file, line, func, expr, value); + #endif + AbortImpl(); +} + +void AssertionFailureImpl(const char *file, int line, const char *func, const char *expr, u64 value, const char *format, ...) { + #if defined(AMS_ENABLE_DETAILED_ASSERTIONS) + { + AMS_LOG("Assertion Failure\n"); + AMS_LOG(" Location: %s:%d\n", file, line); + AMS_LOG(" Function: %s\n", func); + AMS_LOG(" Expression: %s\n", expr); + AMS_LOG(" Value: %016" PRIx64 "\n", value); + AMS_LOG("\n"); + { + ::std::va_list vl; + va_start(vl, format); + AMS_VLOG(format, vl); + va_end(vl); + AMS_LOG("\n"); + } + } + #else + AMS_UNUSED(file, line, func, expr, value, format); + #endif + AbortImpl(); +} diff --git a/libexosphere/include/exosphere/log.hpp b/libexosphere/include/exosphere/log.hpp index 3c54fbed..7ddde611 100644 --- a/libexosphere/include/exosphere/log.hpp +++ b/libexosphere/include/exosphere/log.hpp @@ -18,10 +18,30 @@ namespace ams::log { + #if defined(AMS_BUILD_FOR_AUDITING) || defined(AMS_BUILD_FOR_DEBUGGING) + #define AMS_IMPL_ENABLE_LOG + #endif + + #if defined(AMS_IMPL_ENABLE_LOG) + #define AMS_LOG(...) ::ams::log::Printf(__VA_ARGS__) + #define AMS_VLOG(...) ::ams::log::VPrintf(__VA_ARGS__) + #define AMS_DUMP(...) ::ams::log::Dump(__VA_ARGS__) + #define AMS_LOG_FLUSH() ::ams::log::Flush() + #else + #define AMS_LOG(...) static_cast(0) + #define AMS_VLOG(...) static_cast(0) + #define AMS_DUMP(...) static_cast(0) + #define AMS_LOG_FLUSH() static_cast(0) + #endif + void Initialize(); void Finalize(); + void Printf(const char *fmt, ...) __attribute__((format(printf, 1, 2))); + void VPrintf(const char *fmt, ::std::va_list vl); + void Dump(const void *src, size_t size); + void SendText(const void *text, size_t size); void Flush(); -} \ No newline at end of file +} diff --git a/libexosphere/include/exosphere/secmon/secmon_log.hpp b/libexosphere/include/exosphere/secmon/secmon_log.hpp new file mode 100644 index 00000000..61e298d5 --- /dev/null +++ b/libexosphere/include/exosphere/secmon/secmon_log.hpp @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2018-2020 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 . + */ +#pragma once +#include +#include + +#define AMS_SECMON_LOG(...) AMS_LOG(" [secmon] " __VA_ARGS__) diff --git a/libexosphere/source/libc/libgcc_division.arch.arm.c b/libexosphere/source/libc/libgcc_division.arch.arm.c index 22bc673a..d9e86177 100644 --- a/libexosphere/source/libc/libgcc_division.arch.arm.c +++ b/libexosphere/source/libc/libgcc_division.arch.arm.c @@ -155,6 +155,106 @@ signed __aeabi_idivmod(signed numerator, signed denominator) return ret_idivmod_values(qr.q, qr.r); } +/* struct lqr - stores qutient/remainder to handle divmod EABI interfaces. */ +struct lqr { + unsigned long long q; /* computed quotient */ + unsigned long long r; /* computed remainder */ + unsigned q_n; /* specficies if quotient shall be negative */ + unsigned r_n; /* specficies if remainder shall be negative */ +}; + +static void ul_div_qr(unsigned long long numerator, + unsigned long long denominator, struct lqr *qr); + + +static void division_lqr(unsigned long long n, unsigned long long p, + struct lqr *qr) +{ + unsigned long long i = 1, q = 0; + if (p == 0) { + qr->r = 0xFFFFFFFFFFFFFFFFULL; /* division by 0 */ + return; + } + + while ((p >> 63) == 0) { + i = i << 1; /* count the max division steps */ + p = p << 1; /* increase p until it has maximum size*/ + } + + while (i > 0) { + q = q << 1; /* write bit in q at index (size-1) */ + if (n >= p) { + n -= p; + q++; + } + p = p >> 1; /* decrease p */ + i = i >> 1; /* decrease remaining size in q */ + } + qr->r = n; + qr->q = q; +} + +static void ul_div_qr(unsigned long long numerator, + unsigned long long denominator, struct lqr *qr) +{ + + division_lqr(numerator, denominator, qr); + + /* negate quotient and/or remainder according to requester */ + if (qr->q_n) + qr->q = -qr->q; + if (qr->r_n) + qr->r = -qr->r; +} + +struct asm_ulqr { + unsigned long long v0; + unsigned long long v1; +}; + +/* called from assembly function __aeabi_uldivmod */ +void __ul_divmod(struct asm_ulqr *asm_ulqr); +void __ul_divmod(struct asm_ulqr *asm_ulqr) +{ + unsigned long long numerator = asm_ulqr->v0; + unsigned long long denominator = asm_ulqr->v1; + struct lqr qr = { .q_n = 0, .r_n = 0 }; + + ul_div_qr(numerator, denominator, &qr); + + asm_ulqr->v0 = qr.q; + asm_ulqr->v1 = qr.r; +} + +struct asm_lqr { + long long v0; + long long v1; +}; + +/* called from assembly function __aeabi_ldivmod */ +void __l_divmod(struct asm_lqr *asm_lqr); +void __l_divmod(struct asm_lqr *asm_lqr) +{ + long long numerator = asm_lqr->v0; + long long denominator = asm_lqr->v1; + struct lqr qr = { .q_n = 0, .r_n = 0 }; + + if (((numerator < 0) && (denominator > 0)) || + ((numerator > 0) && (denominator < 0))) + qr.q_n = 1; /* quotient shall be negate */ + if (numerator < 0) { + numerator = -numerator; + qr.r_n = 1; /* remainder shall be negate */ + } + if (denominator < 0) + denominator = -denominator; + + ul_div_qr(numerator, denominator, &qr); + + asm_lqr->v0 = qr.q; + asm_lqr->v1 = qr.r; +} + #ifdef __cplusplus } /* extern "C" */ #endif \ No newline at end of file diff --git a/libexosphere/source/libc/libgcc_division_asm.arch.arm.s b/libexosphere/source/libc/libgcc_division_asm.arch.arm.s index a0acbace..d5217e0a 100644 --- a/libexosphere/source/libc/libgcc_division_asm.arch.arm.s +++ b/libexosphere/source/libc/libgcc_division_asm.arch.arm.s @@ -28,3 +28,40 @@ ret_uidivmod_values: bx lr .type ret_uidivmod_values, %function .size ret_uidivmod_values, .-ret_uidivmod_values + +/* + * __value_in_regs lldiv_t __aeabi_ldivmod( long long n, long long d) + */ +.section .text.__aeabi_ldivmod, "ax", %progbits +.globl __aeabi_ldivmod +.align 0 +.syntax unified +__aeabi_ldivmod: + push {ip, lr} + push {r0-r3} + mov r0, sp + bl __l_divmod + pop {r0-r3} + pop {ip, pc} + +.type __aeabi_ldivmod, %function +.size __aeabi_ldivmod, .-__aeabi_ldivmod + +/* + * __value_in_regs ulldiv_t __aeabi_uldivmod( + * unsigned long long n, unsigned long long d) + */ +.section .text.__aeabi_uldivmod , "ax", %progbits +.globl __aeabi_uldivmod +.align 0 +.syntax unified +__aeabi_uldivmod : + push {ip, lr} + push {r0-r3} + mov r0, sp + bl __ul_divmod + pop {r0-r3} + pop {ip, pc} + +.type __aeabi_uldivmod, %function +.size __aeabi_uldivmod, .-__aeabi_uldivmod diff --git a/libexosphere/source/libc/libgcc_thumb_case.arch.arm.s b/libexosphere/source/libc/libgcc_thumb_case.arch.arm.s index 2658ad61..a7726543 100644 --- a/libexosphere/source/libc/libgcc_thumb_case.arch.arm.s +++ b/libexosphere/source/libc/libgcc_thumb_case.arch.arm.s @@ -33,3 +33,22 @@ __gnu_thumb1_case_uqi: bx lr .type __gnu_thumb1_case_uqi, %function .size __gnu_thumb1_case_uqi, .-__gnu_thumb1_case_uqi + +.section .text.__gnu_thumb1_case_uhi, "ax", %progbits +.globl __gnu_thumb1_case_uhi +.align 0 +.thumb_func +.syntax unified +__gnu_thumb1_case_uhi: + push {r0, r1} + mov r1, lr + lsrs r1, r1, #1 + lsls r0, r0, #1 + lsls r1, r1, #1 + ldrh r1, [r1, r0] + lsls r1, r1, #1 + add lr, lr, r1 + pop {r0, r1} + bx lr +.type __gnu_thumb1_case_uhi, %function +.size __gnu_thumb1_case_uhi, .-__gnu_thumb1_case_uhi diff --git a/libexosphere/source/log/log_api.cpp b/libexosphere/source/log/log_api.cpp index b6c96ed0..93ce0d7c 100644 --- a/libexosphere/source/log/log_api.cpp +++ b/libexosphere/source/log/log_api.cpp @@ -20,8 +20,8 @@ namespace ams::log { namespace { constexpr inline uart::Port UartLogPort = uart::Port_ReservedDebug; + constexpr inline int UartBaudRate = 115200; constinit bool g_initialized_uart = false; - constinit bool g_logging_enabled = false; constexpr inline u32 UartPortFlags = [] { if constexpr (UartLogPort == uart::Port_ReservedDebug) { @@ -78,20 +78,51 @@ namespace ams::log { g_initialized_uart = false; } - void SetDebugLogEnabled(bool en) { - g_logging_enabled = en; + NOINLINE void VPrintf(const char *fmt, ::std::va_list vl) { + /* TODO: What's a good size for the log buffer? Nintendo uses 0x100, but this seems big. */ + char log_buf[0x80]; + const auto len = util::TVSNPrintf(log_buf, sizeof(log_buf), fmt, vl); + + if (g_initialized_uart) { + uart::SendText(UartLogPort, log_buf, len); + } } + NOINLINE void Printf(const char *fmt, ...) { + ::std::va_list vl; + va_start(vl, fmt); + VPrintf(fmt, vl); + va_end(vl); + } + + NOINLINE void Dump(const void *src, size_t size) { + const u8 *src_u8 = static_cast(src); + + for (size_t i = 0; i < size; ++i) { + if ((i % 0x20) == 0x00) { + Printf("%03zx| ", i); + } + Printf("%02x ", src_u8[i]); + if ((i % 0x20) == 0x1F) { + Printf("\n"); + } + } + if ((size % 0x20) != 0) { + Printf("\n"); + } + } + + void SendText(const void *text, size_t size) { - if (g_initialized_uart && g_logging_enabled) { + if (g_initialized_uart) { uart::SendText(UartLogPort, text, size); } } void Flush() { - if (g_initialized_uart && g_logging_enabled) { + if (g_initialized_uart) { uart::WaitFlush(UartLogPort); } } -} \ No newline at end of file +} diff --git a/libstratosphere/source/diag/diag_assertion_impl.cpp b/libstratosphere/source/diag/diag_assertion_impl.cpp index 4108343a..11f6f4d2 100644 --- a/libstratosphere/source/diag/diag_assertion_impl.cpp +++ b/libstratosphere/source/diag/diag_assertion_impl.cpp @@ -43,7 +43,7 @@ namespace ams::diag { inline void DebugLog(const char *format, ...) __attribute__((format(printf, 1, 2))); -#ifdef AMS_ENABLE_DEBUG_PRINT +#ifdef AMS_ENABLE_DETAILED_ASSERTIONS os::Mutex g_debug_log_lock(true); char g_debug_buffer[0x400]; @@ -73,9 +73,9 @@ namespace ams::diag { DebugLog(" Location: %s:%d\n", file, line); DebugLog(" Function: %s\n", func); DebugLog(" Expression: %s\n", expr); - DebugLog(" Value: %016lx\n", value); + DebugLog(" Value: %016" PRIx64 "\n", value); DebugLog("\n"); -#ifdef AMS_ENABLE_DEBUG_PRINT +#ifdef AMS_ENABLE_DETAILED_ASSERTIONS { ::std::va_list vl; va_start(vl, format); @@ -93,7 +93,7 @@ namespace ams::diag { DebugLog(" Location: %s:%d\n", file, line); DebugLog(" Function: %s\n", func); DebugLog(" Expression: %s\n", expr); - DebugLog(" Value: %016lx\n", value); + DebugLog(" Value: %016" PRIx64 "\n", value); DebugLog("\n"); DebugLog("\n"); @@ -105,9 +105,9 @@ namespace ams::diag { DebugLog(" Location: %s:%d\n", file, line); DebugLog(" Function: %s\n", func); DebugLog(" Expression: %s\n", expr); - DebugLog(" Value: %016lx\n", value); + DebugLog(" Value: %016" PRIx64 "\n", value); DebugLog("\n"); -#ifdef AMS_ENABLE_DEBUG_PRINT +#ifdef AMS_ENABLE_DETAILED_ASSERTIONS { ::std::va_list vl; va_start(vl, format); @@ -125,7 +125,7 @@ namespace ams::diag { DebugLog(" Location: %s:%d\n", file, line); DebugLog(" Function: %s\n", func); DebugLog(" Expression: %s\n", expr); - DebugLog(" Value: %016lx\n", value); + DebugLog(" Value: %016" PRIx64 "\n", value); DebugLog("\n"); DebugLog("\n"); diff --git a/libvapours/include/vapours/assert.hpp b/libvapours/include/vapours/assert.hpp index d5034993..79882bd4 100644 --- a/libvapours/include/vapours/assert.hpp +++ b/libvapours/include/vapours/assert.hpp @@ -27,7 +27,7 @@ namespace ams::diag { } -#ifdef AMS_ENABLE_DEBUG_PRINT +#ifdef AMS_ENABLE_DETAILED_ASSERTIONS #define AMS_CALL_ASSERT_FAIL_IMPL(cond, ...) ::ams::diag::AssertionFailureImpl(__FILE__, __LINE__, __PRETTY_FUNCTION__, cond, 0, ## __VA_ARGS__) #define AMS_CALL_ABORT_IMPL(cond, ...) ::ams::diag::AbortImpl(__FILE__, __LINE__, __PRETTY_FUNCTION__, cond, 0, ## __VA_ARGS__) #else diff --git a/libvapours/include/vapours/common.hpp b/libvapours/include/vapours/common.hpp index 5bd76273..15060a03 100644 --- a/libvapours/include/vapours/common.hpp +++ b/libvapours/include/vapours/common.hpp @@ -17,15 +17,29 @@ #include #include -#if 0 -#define AMS_BUILD_FOR_AUDITING +#ifdef AMS_BUILD_FOR_AUDITING + +#define AMS_BUILD_FOR_DEBUGGING + +#if !defined(AMS_FORCE_DISABLE_DETAILED_ASSERTIONS) + #define AMS_ENABLE_DETAILED_ASSERTIONS #endif -#ifdef AMS_BUILD_FOR_AUDITING -#define AMS_BUILD_FOR_DEBUGGING #endif #ifdef AMS_BUILD_FOR_DEBUGGING + #define AMS_ENABLE_ASSERTIONS -#define AMS_ENABLE_DEBUG_PRINT + + #if !defined(AMS_ENABLE_DETAILED_ASSERTIONS) && !defined(AMS_FORCE_DISABLE_DETAILED_ASSERTIONS) + + #if !defined(ATMOSPHERE_IS_EXOSPHERE) || defined(AMS_FORCE_ENABLE_DETAILED_ASSERTIONS) + + #define AMS_ENABLE_DETAILED_ASSERTIONS + + #endif + + #endif + + #endif