riscv: Fix virt_to_phys
After translating a virtual page address to a physical page address,
virt_to_phys() needs to tack the page offset of the original virtual
address back on to complete the translation.
Fixes: 23100d972705 ("riscv: Enable vmalloc")
Signed-off-by: Cade Richard <cade.richard@berkeley.edu>
[Added commit message, Fixes tag, and offset_in_page()]
Signed-off-by: Andrew Jones <andrew.jones@linux.dev>
diff --git a/lib/asm-generic/page.h b/lib/asm-generic/page.h
index 5ed0861..2ed77f4 100644
--- a/lib/asm-generic/page.h
+++ b/lib/asm-generic/page.h
@@ -19,6 +19,8 @@
#define PAGE_ALIGN(addr) ALIGN(addr, PAGE_SIZE)
+#define offset_in_page(p) ((unsigned long)(p) & ~PAGE_MASK)
+
#define __va(x) ((void *)((unsigned long) (x)))
#define __pa(x) ((unsigned long) (x))
#define virt_to_pfn(kaddr) (__pa(kaddr) >> PAGE_SHIFT)
diff --git a/lib/riscv/mmu.c b/lib/riscv/mmu.c
index bd00688..165a703 100644
--- a/lib/riscv/mmu.c
+++ b/lib/riscv/mmu.c
@@ -194,7 +194,7 @@
paddr = virt_to_pte_phys(pgtable, (void *)address);
assert(sizeof(long) == 8 || !(paddr >> 32));
- return (unsigned long)paddr;
+ return (unsigned long)paddr | offset_in_page(address);
}
void *phys_to_virt(unsigned long address)