From 76957e502d16b845cf4b850b9ddd8ddcd723e4e6 Mon Sep 17 00:00:00 2001 From: Michael Scire Date: Mon, 7 Sep 2020 21:41:03 -0700 Subject: [PATCH] kern: add build-define for logging to iram ringbuffer --- .../kern_debug_log_impl.board.nintendo_nx.cpp | 50 +++++++++++++++++++ ...kern_k_memory_layout.board.nintendo_nx.cpp | 2 + 2 files changed, 52 insertions(+) diff --git a/libraries/libmesosphere/source/kern_debug_log_impl.board.nintendo_nx.cpp b/libraries/libmesosphere/source/kern_debug_log_impl.board.nintendo_nx.cpp index cb6d095b9..0f54d3361 100644 --- a/libraries/libmesosphere/source/kern_debug_log_impl.board.nintendo_nx.cpp +++ b/libraries/libmesosphere/source/kern_debug_log_impl.board.nintendo_nx.cpp @@ -18,6 +18,8 @@ namespace ams::kern { +#if defined(MESOSPHERE_DEBUG_LOG_USE_UART_A) || defined(MESOSPHERE_DEBUG_LOG_USE_UART_B) || defined(MESOSPHERE_DEBUG_LOG_USE_UART_C) || defined(MESOSPHERE_DEBUG_LOG_USE_UART_D) + namespace { enum UartRegister { @@ -138,4 +140,52 @@ namespace ams::kern { ReadUartRegister(UartRegister_FCR); } +#elif defined(MESOSPHERE_DEBUG_LOG_USE_IRAM_RINGBUFFER) + + namespace { + + constinit KVirtualAddress g_debug_iram_address = 0; + + constexpr size_t RingBufferSize = 0x5000; + constinit uintptr_t g_offset = 0; + + constinit u8 g_saved_buffer[RingBufferSize]; + + } + + bool KDebugLogImpl::Initialize() { + /* Set the base address. */ + g_debug_iram_address = KMemoryLayout::GetDeviceVirtualAddress(KMemoryRegionType_LegacyLpsIram) + 0x38000; + + std::memset(GetVoidPointer(g_debug_iram_address), 0xFF, RingBufferSize); + + return true; + } + + void KDebugLogImpl::PutChar(char c) { + GetPointer(g_debug_iram_address)[g_offset++] = c; + + if (g_offset == RingBufferSize) { + g_offset = 0; + } + } + + void KDebugLogImpl::Flush() { + /* ... */ + } + + void KDebugLogImpl::Save() { + std::memcpy(g_saved_buffer, GetVoidPointer(g_debug_iram_address), RingBufferSize); + } + + void KDebugLogImpl::Restore() { + std::memcpy(GetVoidPointer(g_debug_iram_address), g_saved_buffer, RingBufferSize); + } + +#else + + #error "Unknown Debug UART device!" + +#endif + } diff --git a/libraries/libmesosphere/source/kern_k_memory_layout.board.nintendo_nx.cpp b/libraries/libmesosphere/source/kern_k_memory_layout.board.nintendo_nx.cpp index 5112c9ac0..d3a98cc62 100644 --- a/libraries/libmesosphere/source/kern_k_memory_layout.board.nintendo_nx.cpp +++ b/libraries/libmesosphere/source/kern_k_memory_layout.board.nintendo_nx.cpp @@ -34,6 +34,8 @@ namespace ams::kern { return KMemoryLayout::GetPhysicalMemoryRegionTree().Insert(0x70006200, 0x100, KMemoryRegionType_Uart | KMemoryRegionAttr_ShouldKernelMap); #elif defined(MESOSPHERE_DEBUG_LOG_USE_UART_D) return KMemoryLayout::GetPhysicalMemoryRegionTree().Insert(0x70006300, 0x100, KMemoryRegionType_Uart | KMemoryRegionAttr_ShouldKernelMap); + #elif defined(MESOSPHERE_DEBUG_LOG_USE_IRAM_RINGBUFFER) + return true; #else #error "Unknown Debug UART device!" #endif