From 727c5fb46b3b68ed1d00912e87de4874e63405cf Mon Sep 17 00:00:00 2001 From: Ian Moffett Date: Tue, 16 Sep 2025 16:19:15 -0400 Subject: kern/amd64: mmu: Add routine to switch VAS This commit introduces a new mmu_write_vas() function to allow the caller to change the virtual address space used by the current processor. Signed-off-by: Ian Moffett --- src/sys/arch/amd64/cpu/mmu.c | 30 ++++++++++++++++++++++++++++++ src/sys/include/vm/mmu.h | 11 +++++++++++ 2 files changed, 41 insertions(+) (limited to 'src') diff --git a/src/sys/arch/amd64/cpu/mmu.c b/src/sys/arch/amd64/cpu/mmu.c index 3f11781..66f8584 100644 --- a/src/sys/arch/amd64/cpu/mmu.c +++ b/src/sys/arch/amd64/cpu/mmu.c @@ -146,6 +146,22 @@ __mmu_read_cr3(void) return cr3; } +/* + * Write a value to the CR3 register + * + * @cr3: Value to write + */ +static inline void +__mmu_write_cr3(uint64_t val) +{ + __ASMV( + "mov %0, %%cr3" + : + : "r" (val) + : "memory" + ); +} + /* * Acquire the paging level used by the * current processing element (pcore) @@ -363,6 +379,20 @@ mmu_new_vas(struct vm_vas *res) return 0; } +/* + * Switch the VAS + */ +int +mmu_write_vas(struct vm_vas *vas) +{ + if (vas->cr3 == 0) { + return -EINVAL; + } + + __mmu_write_cr3(vas->cr3); + return 0; +} + /* * Verify that we are in a known state */ diff --git a/src/sys/include/vm/mmu.h b/src/sys/include/vm/mmu.h index 908748b..820b24d 100644 --- a/src/sys/include/vm/mmu.h +++ b/src/sys/include/vm/mmu.h @@ -106,4 +106,15 @@ int mmu_this_vas(struct vm_vas *vasres_p); */ int mmu_new_vas(struct vm_vas *res); +/* + * Apply a virtual address descriptor to the current + * process and flush out the entire TLB + * + * @vas: Virtual address space to switch to + * + * Returns zero on success, otherwise a less than zero + * value to indicate error. + */ +int mmu_write_vas(struct vm_vas *vas); + #endif /* !_MACHINE_MMU_H_ */ -- cgit v1.2.3