From 032a7bc4d79efea100a00cf3464bea3249a07ff6 Mon Sep 17 00:00:00 2001 From: Jon Santmyer Date: Thu, 4 Sep 2025 12:01:44 -0400 Subject: syscall message unmaps upon invoke --- arch/x86_64/memory/message.c | 48 +++++++++++++++++++++++++++++ arch/x86_64/memory/page-mapping.c | 13 ++++++++ arch/x86_64/syscall/invoke-untyped-retype.c | 9 ++++++ arch/x86_64/usermode.c | 3 +- 4 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 arch/x86_64/memory/message.c create mode 100644 arch/x86_64/syscall/invoke-untyped-retype.c (limited to 'arch') diff --git a/arch/x86_64/memory/message.c b/arch/x86_64/memory/message.c new file mode 100644 index 0000000..1fbdfc8 --- /dev/null +++ b/arch/x86_64/memory/message.c @@ -0,0 +1,48 @@ +#include +#include +#include +#include +#include "arch/x86_64/page.h" +#include "arch/x86_64/page-mapping.h" +#include "print.h" + +int +ko_message_unmap(objdir_entry_t *message, uintptr_t *saveptr) +{ + /* message data should point to the virtual address. */ + /* If it doesn't, fail*/ + if(!(message->extra & KODE_EX_MESSAGE_MAPPED)) return KE_BADCALL; + uintptr_t vptr = message->data; + + processor_t *cproc = processor_current(); + pmle_t *pml4 = vmem_phys_tovirt(cproc->pdir); + + pmle_t *message_pmle = mem_mapping_vptr_mapping(pml4, 4, vptr); + *saveptr = (uintptr_t)vmem_phys_tovirt(message_pmle->paddr << 12); + + message_pmle->p = 0; + __asm__ volatile("invlpg (%0)":: "r"(vptr): "memory"); + + message->extra &= ~KODE_EX_MESSAGE_MAPPED; + return 0; +} + +int +ko_message_remap(objdir_entry_t *message) +{ + if(message->extra & KODE_EX_MESSAGE_MAPPED) return KE_BADCALL; + uintptr_t vptr = message->data; + + processor_t *cproc = processor_current(); + pmle_t *pml4 = vmem_phys_tovirt(cproc->pdir); + + pmle_t *message_pmle = mem_mapping_vptr_mapping(pml4, 4, vptr); + + message_pmle->p = 1; + __asm__ volatile("invlpg (%0)":: "r"(vptr): "memory"); + + message->extra |= KODE_EX_MESSAGE_MAPPED; + return 0; +} + +void ko_message_move(objdir_entry_t *message, uintptr_t vptr); diff --git a/arch/x86_64/memory/page-mapping.c b/arch/x86_64/memory/page-mapping.c index 0de5bfa..d4b4e98 100644 --- a/arch/x86_64/memory/page-mapping.c +++ b/arch/x86_64/memory/page-mapping.c @@ -1,6 +1,7 @@ #include "arch/x86_64/page-mapping.h" #include "arch/x86_64/page.h" #include +#include "print.h" pmle_t* page_mapping_traverse(pmle_t *pml4, uint8_t depth, uint16_t *path) @@ -13,3 +14,15 @@ page_mapping_traverse(pmle_t *pml4, uint8_t depth, uint16_t *path) if(!pmle->p) return NULL; return page_mapping_traverse(pmle_table, depth - 1, path + 1); } + +pmle_t* +mem_mapping_vptr_mapping(pmle_t *pml4, uint8_t depth, uintptr_t vptr) +{ + uint64_t pathval = 0; + uint16_t *path = (uint16_t*)&pathval; + + for(uint8_t i = 0; i < depth; i++) { + path[i] = PML_I_FOR_LAYER(vptr, 4 - i); + } + return page_mapping_traverse(pml4, depth - 1, path); +} diff --git a/arch/x86_64/syscall/invoke-untyped-retype.c b/arch/x86_64/syscall/invoke-untyped-retype.c new file mode 100644 index 0000000..4db91b9 --- /dev/null +++ b/arch/x86_64/syscall/invoke-untyped-retype.c @@ -0,0 +1,9 @@ +#include +#include + +int +ko_untyped_retype_memory_mapping( + objdir_entry_t *target) +{ + +} diff --git a/arch/x86_64/usermode.c b/arch/x86_64/usermode.c index 99ee128..aa626a9 100644 --- a/arch/x86_64/usermode.c +++ b/arch/x86_64/usermode.c @@ -138,7 +138,8 @@ init_load(void) uintptr_t message_phys = s_map_page(pml4, untyped_dir, message_base); _initDirectory.entries[INIT_OBJECT_MESSAGE] = (objdir_entry_t) { .type = KO_MESSAGE, - .data = (uintptr_t)vmem_phys_tovirt(message_phys) + .extra = KODE_EX_MESSAGE_MAPPED, + .data = message_base }; //Write message address to user stack. -- cgit v1.2.1