From 2b237b24c4db5513f8046e15d24501257e7feeaa Mon Sep 17 00:00:00 2001 From: Richard Qin Date: Sun, 3 May 2026 01:47:15 +0800 Subject: feat: implement KASLR support and runtime address mapping for RISC-V architecture Co-authored-by: Copilot --- na/drivers/uart.cc | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) (limited to 'na/drivers/uart.cc') diff --git a/na/drivers/uart.cc b/na/drivers/uart.cc index 6a35588..2e553d6 100644 --- a/na/drivers/uart.cc +++ b/na/drivers/uart.cc @@ -15,10 +15,21 @@ static uint64 g_uart_pa = 0; static uint64 g_uart_size = 0; +static bool g_runtime_mapping_ready = false; extern "C" void early_putc(char c) { if (!g_uart_pa) return; - volatile auto* uart = reinterpret_cast(pa_to_va(phys_addr(g_uart_pa)).raw); + + /* + * Before vmm_init() activates the final page table, UART MMIO is only + * reachable via the bootstrap fixed direct map from entry.S. + * After activation, switch to the runtime (possibly KASLR-slid) base. + */ + uint64 uart_va = g_runtime_mapping_ready + ? pa_to_va(phys_addr(g_uart_pa)).raw + : boot_pa_to_va(phys_addr(g_uart_pa)).raw; + + volatile auto* uart = reinterpret_cast(uart_va); *uart = static_cast(c); } @@ -45,3 +56,7 @@ void console_init() { vmm_map_kernel_mmio(g_uart_pa, g_uart_size); } } + +void console_use_runtime_mapping() { + g_runtime_mapping_ready = true; +} -- cgit v1.2.3