From 18c389411a0f6283c1b6dffc78bbcfcb237e367b Mon Sep 17 00:00:00 2001 From: Jon Santmyer Date: Tue, 19 Aug 2025 16:16:10 -0400 Subject: move pager to libjove --- lib/libjove/arch/x86_64/invoke-pagemap.c | 6 +-- lib/libjove/arch/x86_64/object/pagemap.c | 4 -- lib/libjove/arch/x86_64/pager/ensure.c | 88 ++++++++++++++++++++++++++++++++ 3 files changed, 91 insertions(+), 7 deletions(-) create mode 100644 lib/libjove/arch/x86_64/pager/ensure.c (limited to 'lib/libjove/arch/x86_64') diff --git a/lib/libjove/arch/x86_64/invoke-pagemap.c b/lib/libjove/arch/x86_64/invoke-pagemap.c index e3f3dff..131b22a 100644 --- a/lib/libjove/arch/x86_64/invoke-pagemap.c +++ b/lib/libjove/arch/x86_64/invoke-pagemap.c @@ -10,7 +10,7 @@ int _syscall_invoke_mapping_exists(KernelObjectPageMap *map, uint8_t depth, uint16_t *path) { - uint8_t *syscallData = _syscall_message_ptr; + uint8_t *syscallData = __jove_syscall_ptr; int syscall_at = 0; SYSCALL_PAYLOAD_PUTOBJ(syscallData, syscall_at, map); @@ -23,7 +23,7 @@ _syscall_invoke_mapping_exists(KernelObjectPageMap *map, uint8_t depth, uint16_t int _syscall_invoke_mapping_map(KernelObjectPageMap *map, uint8_t depth, uint16_t *path, KernelObjectUntyped *untyped) { - uint8_t *syscallData = _syscall_message_ptr; + uint8_t *syscallData = __jove_syscall_ptr; int syscall_at = 0; SYSCALL_PAYLOAD_PUTOBJ(syscallData, syscall_at, map); @@ -37,7 +37,7 @@ _syscall_invoke_mapping_map(KernelObjectPageMap *map, uint8_t depth, uint16_t *p int _syscall_invoke_mapping_unmap(KernelObjectPageMap *map, uint8_t depth, uint16_t *path, KernelObjectUntyped *dest) { - uint8_t *syscallData = _syscall_message_ptr; + uint8_t *syscallData = __jove_syscall_ptr; int syscall_at = 0; SYSCALL_PAYLOAD_PUTOBJ(syscallData, syscall_at, map); diff --git a/lib/libjove/arch/x86_64/object/pagemap.c b/lib/libjove/arch/x86_64/object/pagemap.c index c58d7c6..0c03028 100644 --- a/lib/libjove/arch/x86_64/object/pagemap.c +++ b/lib/libjove/arch/x86_64/object/pagemap.c @@ -59,10 +59,6 @@ jove_pagemap_exists( uint16_t *path ) { - if(!_jove_alloc) { - jove_errno = EJOVE_NOALLOC; - return 0; - } int kerr = _syscall_invoke_mapping_exists(map, depth, path); if(kerr == KE_DNE) return 0; diff --git a/lib/libjove/arch/x86_64/pager/ensure.c b/lib/libjove/arch/x86_64/pager/ensure.c new file mode 100644 index 0000000..bfe68ff --- /dev/null +++ b/lib/libjove/arch/x86_64/pager/ensure.c @@ -0,0 +1,88 @@ +#include "arch/x86_64/pager.h" +#include "object-untyped.h" +#include "jove.h" +#include + +KernelObjectPageMap __jove_pagemap; +static unsigned d_cache[3] = { -1, -1, -1 }; +static uintptr_t vptr_cache = 0; +static KernelObjectUntyped work_untyped; + +#define PMLI_SHL(d) (((4 - d) * 9) + 3) +#define PMLI_DL(v, d) ((v >> PMLI_SHL(d)) % 512) + +static uint64_t +pager_write_path(uintptr_t vptr, uint8_t depth) +{ + uint64_t r = 0; + if(depth >= 4) return -1; + + uint16_t *path = (uint16_t*)&r; + for(uint8_t i = 0; i < depth; i++) path[i] = PMLI_DL(vptr, i); + return r; +} + +/* Places new page in work_page. */ +static JoveError +pager_alloc_page_untyped(void) +{ + uint8_t lastmemb = jove_objdir_lastmemb(&__jove_untyped_directory); + if(jove_errno) { + JoveError err = jove_errno; + jove_errno = EJOVE_OK; + return err; + } + + KernelObjectUntyped last_untyped; + _jove_alloc_untyped_inplace(&last_untyped, &__jove_untyped_directory, lastmemb); + + if(jove_untyped_size(&last_untyped) == 0x1000) { + jove_objdir_move(&__jove_untyped_directory, lastmemb, &__rootdir, __jove_work_obj.membi); + return EJOVE_OK; + } + + work_untyped.typed = __jove_work_obj; + return jove_untyped_split_inplace(&last_untyped, 0x1000, &work_untyped); +} + +static JoveError +pager_ensure_at_depth(KernelObjectPageMap *map, uint8_t depth, uint16_t *path) +{ + int exists = jove_pagemap_exists(map, depth, path); + if(exists) { + return EJOVE_OK; + } + + JoveError pagealloc_err = pager_alloc_page_untyped(); + if(pagealloc_err) { + return pagealloc_err; + } + return jove_pagemap_map(map, depth, path, &work_untyped); +} + +#define PAGER_CACHE_ENSURE_DEPTH(map, path_seg, d) \ + if(path_seg[d] != d_cache[d]) { \ + JoveError d_err = pager_ensure_at_depth(map, d, path_seg); \ + if(d_err) return d_err; \ + d_cache[d] = path_seg[d]; \ + } + +JoveError +jove_pager_ensure_for(KernelObjectPageMap *map, uintptr_t vptr) +{ + uint64_t path = pager_write_path(vptr, 3); + if(path == -1) return EJOVE_BADARG; + + vptr &= ~0xFFFULL; + uint16_t *path_seg = (uint16_t*)&path; + + jove_kprintf("%p Alloc %p : %x:%x:%x:%x\n", map, vptr, path_seg[0], path_seg[1], path_seg[2], path_seg[3]); + + PAGER_CACHE_ENSURE_DEPTH(map, path_seg, 0); + PAGER_CACHE_ENSURE_DEPTH(map, path_seg, 1); + PAGER_CACHE_ENSURE_DEPTH(map, path_seg, 2); + if(vptr != vptr_cache) { + return pager_ensure_at_depth(map, 3, path_seg); + } + return EJOVE_OK; +} -- cgit v1.2.1