summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorIan Moffett <ian@osmora.org>2025-09-16 00:29:14 -0400
committerIan Moffett <ian@osmora.org>2025-09-16 00:29:14 -0400
commit2c17a4db1d32a9ace5bdb430a0faf7e28a00110e (patch)
treeed07cd6c5288b8fc6a57c862b059d287fadb2e79 /src
parent83cdb493e5e74871548e20e2885416b38245dc75 (diff)
kern/amd64: mmu: Add mmu_new_vas() routine
This commit introduces the ability to create new virtual address spaces for user programs and a zeroed lower half. Signed-off-by: Ian Moffett <ian@osmora.org>
Diffstat (limited to 'src')
-rw-r--r--src/sys/arch/amd64/cpu/mmu.c32
-rw-r--r--src/sys/include/vm/mmu.h9
2 files changed, 41 insertions, 0 deletions
diff --git a/src/sys/arch/amd64/cpu/mmu.c b/src/sys/arch/amd64/cpu/mmu.c
index 60fb4f1..05e8b55 100644
--- a/src/sys/arch/amd64/cpu/mmu.c
+++ b/src/sys/arch/amd64/cpu/mmu.c
@@ -333,6 +333,38 @@ mmu_map_single(struct vm_vas *vas, struct mmu_map *spec, int prot)
}
/*
+ * Create a new virtual address space
+ */
+int
+mmu_new_vas(struct vm_vas *res)
+{
+ uint64_t *dest, *src;
+ paddr_t frame;
+
+ src = PHYS_TO_VIRT(g_kvas.cr3 & PTE_ADDR_MASK);
+ frame = vm_alloc_frame(1);
+ if (frame == 0) {
+ return -ENOMEM;
+ }
+
+ /*
+ * Copy only the higher half but zero the
+ * lower half.
+ */
+ dest = PHYS_TO_VIRT(frame);
+ for (int i = 0; i < 512; ++i) {
+ if (i < 256) {
+ dest[i] = 0;
+ } else {
+ dest[i] = src[i];
+ }
+ }
+
+ res->cr3 = VIRT_TO_PHYS(dest);
+ return 0;
+}
+
+/*
* Verify that we are in a known state
*/
int
diff --git a/src/sys/include/vm/mmu.h b/src/sys/include/vm/mmu.h
index 5c1c2bb..908748b 100644
--- a/src/sys/include/vm/mmu.h
+++ b/src/sys/include/vm/mmu.h
@@ -97,4 +97,13 @@ int mmu_map_single(struct vm_vas *vas, struct mmu_map *spec, int prot);
*/
int mmu_this_vas(struct vm_vas *vasres_p);
+/*
+ * Create a new virtual address structure with a zeroed
+ * user porition.
+ *
+ * Returns zero on success, otherwise a less than zero
+ * value on failure.
+ */
+int mmu_new_vas(struct vm_vas *res);
+
#endif /* !_MACHINE_MMU_H_ */