diff --git a/kernel/proc.c b/kernel/proc.c index 0655783..6ba3fec 100644 --- a/kernel/proc.c +++ b/kernel/proc.c @@ -544,7 +544,7 @@ sleep(void *chan, struct spinlock *lk) } //PAGEBREAK! -// Wake up p, used by exit(). +// Wake up p if it is sleeping in wait(); used by exit(). // Caller must hold p->lock. static void wakeup1(struct proc *p) diff --git a/kernel/riscv.h b/kernel/riscv.h index e5c0f64..e35f3bc 100644 --- a/kernel/riscv.h +++ b/kernel/riscv.h @@ -312,6 +312,17 @@ r_ra() return x; } +// tell the machine to finish any previous writes to +// PTEs, so that a subsequent use of a virtual +// address or load of the SATP will see those writes. +// perhaps this also flushes the TLB. +static inline void +sfence_vma() +{ + // the zero, zero means flush all TLB entries. + asm volatile("sfence.vma zero, zero"); +} + #define PGSIZE 4096 // bytes per page #define PGSHIFT 12 // bits of offset within a page diff --git a/kernel/trampoline.S b/kernel/trampoline.S index b992ea6..471a29c 100644 --- a/kernel/trampoline.S +++ b/kernel/trampoline.S @@ -17,7 +17,8 @@ trampout: # a0: p->tf in user page table # a1: new value for satp, for user page table - # switch to user page table + # switch to user page table. + sfence.vma zero, zero csrw satp, a1 # put the saved user a0 in sscratch, so we @@ -128,6 +129,7 @@ trampin: # restore kernel page table from p->tf->kernel_satp ld t1, 0(a0) + sfence.vma zero, zero csrw satp, t1 # a0 is no longer valid, since the kernel page diff --git a/kernel/vm.c b/kernel/vm.c index bdb53c2..412ec8c 100644 --- a/kernel/vm.c +++ b/kernel/vm.c @@ -61,6 +61,7 @@ kvminit() void kvminithart() { + sfence_vma(); w_satp(MAKE_SATP(kernel_pagetable)); }