From 91566327012cad2ca99df47f9321dfc012d8a7ff Mon Sep 17 00:00:00 2001 From: Frans Kaashoek Date: Fri, 19 Jul 2019 08:38:51 -0400 Subject: One way of supporting a guard page below kstack: allocate kstacks in procinit() and map them high up (below TRAMPOLNE) with an empty mapping below each stack. Never free a kernel stack. Another way would be to allocate and map them dynamically, but then we need to reload page table when switching processes in scheduler() and/or have a kernel pagetable per proc (if we want k->stack to be the same virtual address in each process). One gotcha: kernel addresses are not equal to physical addresses for stack addresses. A stack address must be translated if we need its physical address (e.g., virtio passes a stack address to the disk). --- kernel/vm.c | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) (limited to 'kernel/vm.c') diff --git a/kernel/vm.c b/kernel/vm.c index 412ec8c..d42e719 100644 --- a/kernel/vm.c +++ b/kernel/vm.c @@ -404,3 +404,34 @@ copyinstr(pagetable_t pagetable, char *dst, uint64 srcva, uint64 max) return -1; } } + +char *map_kstack(uint64 kstack) +{ + char *k = kalloc(); + if(k == 0) { + return 0; + } + if (mappages(kernel_pagetable, (uint64) kstack, PGSIZE, + (uint64) k, PTE_R | PTE_W) == 0) { + kvminithart(); + return (char *) kstack; + } + kfree(k); + return 0; +} + +// assumes va is page aligned +uint64 +kernelpa(uint64 va) { + uint64 off = va % PGSIZE; + pte_t *pte; + uint64 pa; + + pte = walk(kernel_pagetable, va, 0); + if(pte == 0) + panic("kernelpa"); + if((*pte & PTE_V) == 0) + panic("kernelpa"); + pa = PTE2PA(*pte); + return pa+off; +} -- cgit v1.2.3 From 06109901c9c12733e86fb1b264dabef60655de04 Mon Sep 17 00:00:00 2001 From: Frans Kaashoek Date: Fri, 19 Jul 2019 11:27:02 -0400 Subject: x --- kernel/vm.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'kernel/vm.c') diff --git a/kernel/vm.c b/kernel/vm.c index d42e719..f7ca101 100644 --- a/kernel/vm.c +++ b/kernel/vm.c @@ -405,7 +405,8 @@ copyinstr(pagetable_t pagetable, char *dst, uint64 srcva, uint64 max) } } -char *map_kstack(uint64 kstack) +char +*map_kstack(uint64 kstack) { char *k = kalloc(); if(k == 0) { -- cgit v1.2.3