From f349e0801e88757edfa1be4403c452766c65333c Mon Sep 17 00:00:00 2001 From: Marti Alonso <41269236+martialonso@users.noreply.github.com> Date: Tue, 6 May 2025 02:01:39 +0200 Subject: Fix cputchar on RV32 with virtual memory (#49) When cputchar was fixed for RV32, virtual memory was not taken into consideration. If cputchar was called in supervisor mode, with virtual memory enabled, the pointers given to the HTIF would have wrongfully been virtual addresses. Translate pointers to always be physical addresses to support cputchar with both virtual memory enabled and disabled. Defined macro kaa2pa is short for 'kernel any address to physical address'. --- v/vm.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'v/vm.c') diff --git a/v/vm.c b/v/vm.c index cc58417..061c590 100644 --- a/v/vm.c +++ b/v/vm.c @@ -29,6 +29,7 @@ static void do_tohost(uint64_t tohost_value) tohost = tohost_value; } +#define kaa2pa(aa) ((uintptr_t)(aa) & (uintptr_t)(~(-MEGAPAGE_SIZE)) | (uintptr_t)(DRAM_BASE)) #define pa2kva(pa) ((void*)(pa) - DRAM_BASE - MEGAPAGE_SIZE) #define uva2kva(pa) ((void*)(pa) - MEGAPAGE_SIZE) @@ -48,9 +49,9 @@ static void cputchar(int x) volatile int buff = x; syscall_struct[0] = SYS_write; syscall_struct[1] = 1; - syscall_struct[2] = (uintptr_t)&buff; + syscall_struct[2] = kaa2pa(&buff); syscall_struct[3] = 1; - do_tohost((uintptr_t)&syscall_struct); + do_tohost(kaa2pa(&syscall_struct)); // Wait for response as struct has to be read by HTIF while(!fromhost); #else -- cgit v1.1