diff options
author | Jon Santmyer <jon@jonsantmyer.com> | 2025-09-04 12:01:44 -0400 |
---|---|---|
committer | Jon Santmyer <jon@jonsantmyer.com> | 2025-09-04 12:01:44 -0400 |
commit | 032a7bc4d79efea100a00cf3464bea3249a07ff6 (patch) | |
tree | dab1be8053ac540e4163ffbff850fefa4f22171f | |
parent | d26eb8b54969e79d933a8e20f2725343cd42deab (diff) | |
download | jove-kernel-032a7bc4d79efea100a00cf3464bea3249a07ff6.tar.gz jove-kernel-032a7bc4d79efea100a00cf3464bea3249a07ff6.tar.bz2 jove-kernel-032a7bc4d79efea100a00cf3464bea3249a07ff6.zip |
syscall message unmaps upon invokemain
-rw-r--r-- | arch/x86_64/memory/message.c | 48 | ||||
-rw-r--r-- | arch/x86_64/memory/page-mapping.c | 13 | ||||
-rw-r--r-- | arch/x86_64/syscall/invoke-untyped-retype.c | 9 | ||||
-rw-r--r-- | arch/x86_64/usermode.c | 3 | ||||
-rw-r--r-- | include/arch/x86_64/page-mapping.h | 7 | ||||
-rw-r--r-- | include/arch/x86_64/page.h | 4 | ||||
-rw-r--r-- | include/object.h | 14 | ||||
-rw-r--r-- | include/syscall.h | 4 | ||||
-rw-r--r-- | lib/untyped-retype.c | 11 | ||||
-rw-r--r-- | syscall/handler.c | 27 | ||||
-rw-r--r-- | syscall/invoke-untyped.c | 25 |
11 files changed, 148 insertions, 17 deletions
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 <object.h> +#include <stddef.h> +#include <error.h> +#include <device/processor.h> +#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 <stddef.h> +#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 <object.h> +#include <error.h> + +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. diff --git a/include/arch/x86_64/page-mapping.h b/include/arch/x86_64/page-mapping.h index bc495dd..fd620ea 100644 --- a/include/arch/x86_64/page-mapping.h +++ b/include/arch/x86_64/page-mapping.h @@ -5,4 +5,11 @@ pmle_t *page_mapping_traverse(pmle_t *pml4, uint8_t depth, uint16_t *path); +/**@FUNC Get the pmle associated with this virtual address down to a given depth. + * At depth=0, gets &pml4[pml4i] + * At depth=1, gets &pml4[pml4i][pml3i] + * At depth=2, gets &pml4[pml4i][pml3i][pml2i] + * etc...*/ +pmle_t *mem_mapping_vptr_mapping(pmle_t *pml4, uint8_t depth, uintptr_t vptr); + #endif diff --git a/include/arch/x86_64/page.h b/include/arch/x86_64/page.h index e62b8d6..7c6186a 100644 --- a/include/arch/x86_64/page.h +++ b/include/arch/x86_64/page.h @@ -33,8 +33,8 @@ typedef union jove_PageMapLevelEntry typedef uint16_t pmli_t; -#define PML_SHL(l) ((l * 9) + 3) -#define PML_I_FOR_LAYER(v, l) ((v >> PML_SHL(l)) % 512) +#define PML_SHL(l) (((l) * 9) + 3) +#define PML_I_FOR_LAYER(v, l) (((v) >> PML_SHL(l)) % 512) uintptr_t vmem_ident_tophys(void *vptr); void *vmem_phys_tovirt(uintptr_t pptr); diff --git a/include/object.h b/include/object.h index 94e6b15..c8621ee 100644 --- a/include/object.h +++ b/include/object.h @@ -19,7 +19,6 @@ enum /* Generic objects */ KO_NONE = 0, KO_OBJECT_DIRECTORY, - KO_INIT_DATA, KO_MEMORY_UNTYPED, KO_MEMORY_MAPPING, //4KiB aligned fixed width KO_INITRD_FILE, @@ -34,6 +33,8 @@ enum #define KO_MESSAGE_BYTES 4096 #define KO_MESSAGE_ALIGN 0x1000 +#define KODE_EX_MESSAGE_MAPPED 0b1 + typedef uint8_t path_byte_t; typedef uint16_t obj_type_t; @@ -43,7 +44,7 @@ typedef struct jove_ObjectDirectoryEntry union { struct { unsigned char lock; - char u0; + char extra; }; unsigned short flg; }; @@ -63,4 +64,13 @@ typedef struct jove_ObjectDirectory objdir_entry_t *objdir_seek(objdir_t *dir, uint8_t *path, unsigned long pathw); unsigned long objdir_pathw(objdir_t *dir, uint8_t *path); +int ko_message_unmap(objdir_entry_t *message, uintptr_t *saveptr); +int ko_message_remap(objdir_entry_t *message); +void ko_message_move(objdir_entry_t *message, uintptr_t vptr); + +int ko_untyped_retype_objdir(objdir_entry_t *target); +int ko_untyped_retype_memory_mapping(objdir_entry_t *target); +int ko_untyped_retype_tcb(objdir_entry_t *target); +int ko_untyped_retype_message(objdir_entry_t *target); + #endif diff --git a/include/syscall.h b/include/syscall.h index 7410acc..e04ff85 100644 --- a/include/syscall.h +++ b/include/syscall.h @@ -38,7 +38,9 @@ enum /*[first path][u8 funcid][second path]*/ INVOKE_UNTYPED_MERGE, /*[target path][u8 funcid][size_t ret]*/ - INVOKE_UNTYPED_ALIGNMENT + INVOKE_UNTYPED_ALIGNMENT, + /*[target path][u8 funcid][u16 type]*/ + INVOKE_UNTYPED_RETYPE, }; #endif diff --git a/lib/untyped-retype.c b/lib/untyped-retype.c new file mode 100644 index 0000000..70fb4d2 --- /dev/null +++ b/lib/untyped-retype.c @@ -0,0 +1,11 @@ +#include <object.h> +#include <memory.h> +#include <stddef.h> +#include <error.h> + +int +ko_untyped_retype_objdir(objdir_entry_t *target) +{ + size_t *untyped = ko_entry_data(target); + if(*untyped != 0x1000) return -KE_BADSIZE; +} diff --git a/syscall/handler.c b/syscall/handler.c index b3eea99..a586a73 100644 --- a/syscall/handler.c +++ b/syscall/handler.c @@ -60,21 +60,32 @@ _syscall_handler(uintmax_t argsi, int calli) return -KE_BADMSG; } mtx_acquire(&payload_entry->lock); - uint8_t *payload = ko_entry_data(payload_entry); + + uint8_t *payload; + int result = 0; + + if(ko_message_unmap(payload_entry, (uintptr_t*)&payload)) { + klogf("Failed to unmap message %p\n", payload_entry); + result = KE_BADMSG; + goto handle_end; + } switch(calli) { case SYSCALL_INVOKE: { - int e = s_syscall_handle_invoke(root_dir, payload); - mtx_release(&payload_entry->lock); - return e; + result = s_syscall_handle_invoke(root_dir, payload); + goto handle_end; } case SYSCALL_DEBUG_PUTC: kprintf("%c", (char)payload[0]); - mtx_release(&payload_entry->lock); - return 0; + goto handle_end; default: klogf("Invalid syscall %i caught! Failing.\n", calli); - mtx_release(&payload_entry->lock); - return -KE_BADCALL; + result = KE_BADCALL; + goto handle_end; } + +handle_end: + ko_message_remap(payload_entry); + mtx_release(&payload_entry->lock); + return result; } diff --git a/syscall/invoke-untyped.c b/syscall/invoke-untyped.c index fe34ce9..a89306e 100644 --- a/syscall/invoke-untyped.c +++ b/syscall/invoke-untyped.c @@ -1,4 +1,5 @@ #include "handles.h" +#include "object.h" #include "syscall.h" #include "error.h" #include "memory.h" @@ -69,7 +70,7 @@ s_handle_invoke_untyped_split( }; size_t *split = ko_entry_data(dest_entry); - *untyped -= dest_bytes; +*untyped -= dest_bytes; *split = dest_bytes; #ifdef DBG_SYSCALL @@ -80,11 +81,29 @@ s_handle_invoke_untyped_split( return 0; } +static int +s_handle_invoke_untyped_retype( + objdir_t *root_dir, + objdir_entry_t *target, + uint8_t *payload, + size_t payload_at + ) +{ + obj_type_t retype; + SYSCALL_PAYLOAD_TAKEL(payload, payload_at, retype, obj_type_t); + + switch(retype) { + case KO_OBJECT_DIRECTORY: + return ko_untyped_retype_objdir(target); + default: return KE_BADTYPE; + } +} + static int (*s_invoke_handles[])(objdir_t*, objdir_entry_t*, uint8_t*, size_t) = { [INVOKE_UNTYPED_SIZE] = s_handle_invoke_untyped_size, [INVOKE_UNTYPED_SPLIT] = s_handle_invoke_untyped_split, - - [INVOKE_UNTYPED_ALIGNMENT] = s_handle_invoke_untyped_alignment + [INVOKE_UNTYPED_ALIGNMENT] = s_handle_invoke_untyped_alignment, + [INVOKE_UNTYPED_RETYPE] = s_handle_invoke_untyped_retype, }; static size_t s_invoke_handles_count = sizeof(s_invoke_handles) / sizeof(void*); |