From 32392573cc6438e37c9f73aaac769eb934db385b Mon Sep 17 00:00:00 2001 From: Matheus Date: Sun, 15 Sep 2024 18:07:05 -0300 Subject: [PATCH] cpu: change all _cpu()->vmmctx to current_vmm_context --- kernel-src/fs/pipefs.c | 1 - kernel-src/fs/sockfs.c | 1 - kernel-src/include/x86-64/arch/cpu.h | 20 +++++++++--- kernel-src/io/fb.c | 8 ++--- kernel-src/mm/vmm.c | 46 ++++++++++++++-------------- kernel-src/sys/syscalls/execve.c | 2 +- 6 files changed, 43 insertions(+), 35 deletions(-) diff --git a/kernel-src/fs/pipefs.c b/kernel-src/fs/pipefs.c index 56c98e02..d5ddb62a 100644 --- a/kernel-src/fs/pipefs.c +++ b/kernel-src/fs/pipefs.c @@ -400,7 +400,6 @@ void pipefs_init() { } int pipefs_newpipe(vnode_t **nodep) { - __assert(_cpu()->intstatus); vnode_t *vnode = slab_allocate(nodecache); pipenode_t *node = (pipenode_t *)vnode; if (node == NULL) diff --git a/kernel-src/fs/sockfs.c b/kernel-src/fs/sockfs.c index 4424ce90..d1577445 100644 --- a/kernel-src/fs/sockfs.c +++ b/kernel-src/fs/sockfs.c @@ -266,7 +266,6 @@ void sockfs_init() { } int sockfs_newsocket(vnode_t **nodep, socket_t *socket) { - __assert(_cpu()->intstatus); vnode_t *vnode = slab_allocate(nodecache); socketnode_t *node = (socketnode_t *)vnode; if (node == NULL) diff --git a/kernel-src/include/x86-64/arch/cpu.h b/kernel-src/include/x86-64/arch/cpu.h index ca3ea975..29012fa3 100644 --- a/kernel-src/include/x86-64/arch/cpu.h +++ b/kernel-src/include/x86-64/arch/cpu.h @@ -18,11 +18,11 @@ typedef struct cpu_t { thread_t *thread; struct cpu_t *self; + vmmcontext_t *vmmctx; uint64_t gdt[7]; ist_t ist; long id; isr_t isr[MAX_ISR_COUNT]; - vmmcontext_t *vmmctx; int acpiid; timer_t *timer; bool intstatus; @@ -56,16 +56,26 @@ static inline uint16_t be_to_cpu_w(uint16_t w) { cpu_t *_bsp(void); +static inline thread_t *current_thread(void) { + thread_t *thread; + asm volatile ("mov %%gs:0, %%rax" : "=a"(thread) : : "memory"); + return thread; +} + static inline cpu_t *_cpu(void) { cpu_t *cpu; asm volatile ("mov %%gs:8, %%rax" : "=a"(cpu) : : "memory"); return cpu; } -static inline thread_t *current_thread(void) { - thread_t *thread; - asm volatile ("mov %%gs:0, %%rax" : "=a"(thread) : : "memory"); - return thread; +static inline vmmcontext_t *current_vmm_context(void) { + vmmcontext_t *context; + asm volatile ("mov %%gs:16, %%rax" : "=a"(context) : : "memory"); + return context; +} + +static inline void set_current_vmm_context(vmmcontext_t *context) { + asm volatile ("mov %%rax, %%gs:16" : : "a"(context) : "memory"); } static inline void cpu_set(cpu_t *ptr) { diff --git a/kernel-src/io/fb.c b/kernel-src/io/fb.c index ee0bc4e1..2063de08 100644 --- a/kernel-src/io/fb.c +++ b/kernel-src/io/fb.c @@ -136,7 +136,7 @@ static int mmap(int minor, void *addr, uintmax_t offset, int flags) { if (flags & V_FFLAGS_SHARED) { // make sure offset is ALWAYS page aligned when doing a shared mapping __assert((offset % PAGE_SIZE) == 0); - return arch_mmu_map(_cpu()->vmmctx->pagetable, FROM_HHDM((void *)((uintptr_t)fbs[minor]->address + offset)), addr, vnodeflagstommuflags(flags)) ? 0 : ENOMEM; + return arch_mmu_map(current_vmm_context()->pagetable, FROM_HHDM((void *)((uintptr_t)fbs[minor]->address + offset)), addr, vnodeflagstommuflags(flags)) ? 0 : ENOMEM; } else { size_t size = offset + PAGE_SIZE < end ? PAGE_SIZE : end - offset; paddr = pmm_allocpage(PMM_SECTION_DEFAULT); @@ -145,7 +145,7 @@ static int mmap(int minor, void *addr, uintmax_t offset, int flags) { memcpy(MAKE_HHDM(paddr), (void *)((uintptr_t)fbs[minor]->address + offset), size); - if (arch_mmu_map(_cpu()->vmmctx->pagetable, paddr, addr, vnodeflagstommuflags(flags)) == false) { + if (arch_mmu_map(current_vmm_context()->pagetable, paddr, addr, vnodeflagstommuflags(flags)) == false) { pmm_release(paddr); return ENOMEM; } @@ -155,8 +155,8 @@ static int mmap(int minor, void *addr, uintmax_t offset, int flags) { } static int munmap(int minor, void *addr, uintmax_t offset, int flags) { - void *phys = arch_mmu_getphysical(_cpu()->vmmctx->pagetable, addr); - arch_mmu_unmap(_cpu()->vmmctx->pagetable, addr); + void *phys = arch_mmu_getphysical(current_vmm_context()->pagetable, addr); + arch_mmu_unmap(current_vmm_context()->pagetable, addr); if ((flags & V_FFLAGS_SHARED) == 0) { pmm_release(phys); } diff --git a/kernel-src/mm/vmm.c b/kernel-src/mm/vmm.c index a4106f74..94faaae3 100644 --- a/kernel-src/mm/vmm.c +++ b/kernel-src/mm/vmm.c @@ -87,7 +87,7 @@ static vmmspace_t kernelspace = { // returns a pointer to the space of vaddr static vmmspace_t *getspace(void *vaddr) { if (USERSPACE_START <= vaddr && vaddr < USERSPACE_END) - return &_cpu()->vmmctx->space; + return ¤t_vmm_context()->space; else if (KERNELSPACE_START <= vaddr && vaddr < KERNELSPACE_END) return &kernelspace; else @@ -219,7 +219,7 @@ static void destroyrange(vmmrange_t *range, uintmax_t _offset, size_t size, int for (uintmax_t offset = _offset; offset < top; offset += PAGE_SIZE) { void *vaddr = (void *)((uintptr_t)range->start + offset); - void *physical = arch_mmu_getphysical(_cpu()->vmmctx->pagetable, vaddr); + void *physical = arch_mmu_getphysical(current_vmm_context()->pagetable, vaddr); if (physical == NULL) continue; @@ -234,21 +234,21 @@ static void destroyrange(vmmrange_t *range, uintmax_t _offset, size_t size, int VOP_LOCK(range->vnode); __assert(VOP_MUNMAP(range->vnode, vaddr, range->offset + offset, mmuflagstovnodeflags(range->mmuflags) | (range->flags & VMM_FLAGS_SHARED ? V_FFLAGS_SHARED : 0), cred) == 0); VOP_UNLOCK(range->vnode); - } else if (arch_mmu_iswritable(_cpu()->vmmctx->pagetable, vaddr)) { + } else if (arch_mmu_iswritable(current_vmm_context()->pagetable, vaddr)) { // dirty page cache mapping - arch_mmu_unmap(_cpu()->vmmctx->pagetable, vaddr); + arch_mmu_unmap(current_vmm_context()->pagetable, vaddr); VOP_LOCK(range->vnode); vmmcache_makedirty(pmm_getpage(physical)); VOP_UNLOCK(range->vnode); pmm_release(physical); } else { // non dirty page mapping - arch_mmu_unmap(_cpu()->vmmctx->pagetable, vaddr); + arch_mmu_unmap(current_vmm_context()->pagetable, vaddr); pmm_release(physical); } } else { // anonymous, physical or private non character device mapping - arch_mmu_unmap(_cpu()->vmmctx->pagetable, vaddr); + arch_mmu_unmap(current_vmm_context()->pagetable, vaddr); if ((range->flags & VMM_FLAGS_PHYSICAL) == 0) pmm_release(physical); } @@ -268,10 +268,10 @@ static void changemmurange(vmmrange_t *range, void *base, size_t size, mmuflags_ mmuflags_t currentflags; // if page is not mapped, do nothing - if (arch_mmu_getflags(_cpu()->vmmctx->pagetable, address, ¤tflags) == false) + if (arch_mmu_getflags(current_vmm_context()->pagetable, address, ¤tflags) == false) continue; - void *physical = arch_mmu_getphysical(_cpu()->vmmctx->pagetable, address); + void *physical = arch_mmu_getphysical(current_vmm_context()->pagetable, address); uintmax_t mask = 0; // check which flags are currently set and will be unset @@ -291,7 +291,7 @@ static void changemmurange(vmmrange_t *range, void *base, size_t size, mmuflags_ // we will only change the mapping if the permissions decreased if (mask) { - arch_mmu_remap(_cpu()->vmmctx->pagetable, physical, address, currentflags & ~mask); + arch_mmu_remap(current_vmm_context()->pagetable, physical, address, currentflags & ~mask); arch_mmu_invalidate(address); } } @@ -564,7 +564,7 @@ bool vmm_pagefault(void *addr, bool user, int actions) { proc_t *proc = thread ? thread->proc : NULL; cred_t *cred = proc ? &proc->cred : NULL; - if (arch_mmu_ispresent(_cpu()->vmmctx->pagetable, addr) == false) { + if (arch_mmu_ispresent(current_vmm_context()->pagetable, addr) == false) { // page not present in the page tables if (range->flags & VMM_FLAGS_FILE) { uintmax_t mapoffset = (uintptr_t)addr - (uintptr_t)range->start; @@ -590,7 +590,7 @@ bool vmm_pagefault(void *addr, bool user, int actions) { printf("vmm: error on vmmcache_getpage(): %d\n", error); status = false; } else { - status = arch_mmu_map(_cpu()->vmmctx->pagetable, pmm_getpageaddress(res), addr, range->mmuflags & ~ARCH_MMU_FLAGS_WRITE); + status = arch_mmu_map(current_vmm_context()->pagetable, pmm_getpageaddress(res), addr, range->mmuflags & ~ARCH_MMU_FLAGS_WRITE); if (!status) { printf("vmm: out of memory to map file into address space (sending SIGBUS)\n"); pmm_release(pmm_getpageaddress(res)); @@ -601,7 +601,7 @@ bool vmm_pagefault(void *addr, bool user, int actions) { } } else { // anonymous memory. map the zero'd page - status = arch_mmu_map(_cpu()->vmmctx->pagetable, zeropage, addr, range->mmuflags & ~ARCH_MMU_FLAGS_WRITE); + status = arch_mmu_map(current_vmm_context()->pagetable, zeropage, addr, range->mmuflags & ~ARCH_MMU_FLAGS_WRITE); if (!status) { printf("vmm: out of memory to map zero page into address space (sending SIGBUS)\n"); signal_signalthread(current_thread(), SIGBUS, true); @@ -610,13 +610,13 @@ bool vmm_pagefault(void *addr, bool user, int actions) { pmm_hold(zeropage); } } - } else if (arch_mmu_iswritable(_cpu()->vmmctx->pagetable, addr) == false) { + } else if (arch_mmu_iswritable(current_vmm_context()->pagetable, addr) == false) { // page present but not writeable in the page tables - void *oldphys = arch_mmu_getphysical(_cpu()->vmmctx->pagetable, addr); + void *oldphys = arch_mmu_getphysical(current_vmm_context()->pagetable, addr); if ((range->flags & VMM_FLAGS_FILE) && (range->flags & VMM_FLAGS_SHARED)) { // shared file, remap it as writable - arch_mmu_remap(_cpu()->vmmctx->pagetable, oldphys, addr, range->mmuflags); + arch_mmu_remap(current_vmm_context()->pagetable, oldphys, addr, range->mmuflags); if (vfs_iscacheable(range->vnode)) { // and if its a cache page, mark it as dirty VOP_LOCK(range->vnode); @@ -634,7 +634,7 @@ bool vmm_pagefault(void *addr, bool user, int actions) { status = true; } else { memcpy(MAKE_HHDM(newphys), MAKE_HHDM(oldphys), PAGE_SIZE); - arch_mmu_remap(_cpu()->vmmctx->pagetable, newphys, addr, range->mmuflags); + arch_mmu_remap(current_vmm_context()->pagetable, newphys, addr, range->mmuflags); if ((range->flags & VMM_FLAGS_FILE) == 0 || vfs_iscacheable(range->vnode)) pmm_release(oldphys); @@ -660,7 +660,7 @@ void *vmm_getphysical(void *addr) { MUTEX_ACQUIRE(&space->lock, false); - void *physical = arch_mmu_getphysical(_cpu()->vmmctx->pagetable, addr); + void *physical = arch_mmu_getphysical(current_vmm_context()->pagetable, addr); MUTEX_RELEASE(&space->lock); return physical + ((uintptr_t)addr - ROUND_DOWN((uintptr_t)addr, PAGE_SIZE)); @@ -726,9 +726,9 @@ void *vmm_map(void *addr, volatile size_t size, int flags, mmuflags_t mmuflags, if (flags & VMM_FLAGS_PHYSICAL) { // map to allocated virtual memory for (uintmax_t i = 0; i < size; i += PAGE_SIZE) { - if (arch_mmu_map(_cpu()->vmmctx->pagetable, (void *)((uintptr_t)private + i), (void *)((uintptr_t)start + i), mmuflags) == false) { + if (arch_mmu_map(current_vmm_context()->pagetable, (void *)((uintptr_t)private + i), (void *)((uintptr_t)start + i), mmuflags) == false) { for (uintmax_t j = 0; j < size; j += PAGE_SIZE) - arch_mmu_unmap(_cpu()->vmmctx->pagetable, (void *)((uintptr_t)start + j)); + arch_mmu_unmap(current_vmm_context()->pagetable, (void *)((uintptr_t)start + j)); retaddr = NULL; goto cleanup; @@ -743,14 +743,14 @@ void *vmm_map(void *addr, volatile size_t size, int flags, mmuflags_t mmuflags, goto cleanup; } - if (arch_mmu_map(_cpu()->vmmctx->pagetable, allocated, (void *)((uintptr_t)start + i), mmuflags) == false) { + if (arch_mmu_map(current_vmm_context()->pagetable, allocated, (void *)((uintptr_t)start + i), mmuflags) == false) { for (uintmax_t j = 0; j < size; j += PAGE_SIZE) { void *virt = (void *)((uintptr_t)start + i); - void *physical = arch_mmu_getphysical(_cpu()->vmmctx->pagetable, virt); + void *physical = arch_mmu_getphysical(current_vmm_context()->pagetable, virt); if (physical) { pmm_release(physical); - arch_mmu_unmap(_cpu()->vmmctx->pagetable, virt); + arch_mmu_unmap(current_vmm_context()->pagetable, virt); } } @@ -877,7 +877,7 @@ vmmcontext_t *vmm_fork(vmmcontext_t *oldcontext) { void vmm_switchcontext(vmmcontext_t *ctx) { if (current_thread()) current_thread()->vmmctx = ctx; - _cpu()->vmmctx = ctx; + set_current_vmm_context(ctx); arch_mmu_switch(ctx->pagetable); } diff --git a/kernel-src/sys/syscalls/execve.c b/kernel-src/sys/syscalls/execve.c index 04cb9212..de1e2e9a 100644 --- a/kernel-src/sys/syscalls/execve.c +++ b/kernel-src/sys/syscalls/execve.c @@ -56,7 +56,7 @@ static syscallret_t execve(context_t *context, char *upath, char *uargv[], char char **argv = NULL; char **envp = NULL; vmmcontext_t *vmmctx = NULL; - vmmcontext_t *oldctx = _cpu()->vmmctx; + vmmcontext_t *oldctx = current_vmm_context(); vnode_t *node = NULL; vnode_t *refnode = NULL; char *interp = NULL;