From: Matthias Kruk Date: Wed, 18 Dec 2019 08:44:15 +0000 (+0900) Subject: Add pg_dir_mmap() and pg_dir_munmap() functions to implement the low-level half of... X-Git-Url: https://git.corax.cc/?a=commitdiff_plain;h=96eed2e4c9a8fb8159c5d0aeddaf628fb9d080f3;p=corax Add pg_dir_mmap() and pg_dir_munmap() functions to implement the low-level half of the mmap() and munmap() syscalls --- diff --git a/kernel/arch/paging.c b/kernel/arch/paging.c index 23a36e6..e31b0d2 100644 --- a/kernel/arch/paging.c +++ b/kernel/arch/paging.c @@ -23,6 +23,7 @@ #include #include #include +#include #include "cpu.h" #include "paging.h" #include "heap.h" @@ -788,6 +789,7 @@ static void* _pg_dir_heap_start(pg_dir_t *pd) break; + case REGION_MMAP: case REGION_TEXT: case REGION_BSS: case REGION_DATA: @@ -859,6 +861,7 @@ static int _pg_dir_heap_map(pg_dir_t *pd, u32_t heap_size) */ break; + case REGION_MMAP: case REGION_TEXT: case REGION_BSS: case REGION_DATA: @@ -1593,7 +1596,7 @@ void pg_fault_debug(pg_dir_t *pdir, void *addr) dbg_printf("pdir[%03u] = 0x%08x\n", pdi, (u32_t)pde); if(!(pde & PAGE_ATTR_SIZE)) { - pte = ((page_table_t*)(pde & 0xfffff000))->pt_entries[pti]; + pte = ((page_table_t*)((size_t)pde & 0xfffff000))->pt_entries[pti]; dbg_printf("pdir[%03u][%03u] = 0x%08x\n", pdi, pti, (u32_t)pte); } @@ -1609,3 +1612,64 @@ void pg_fault_debug(pg_dir_t *pdir, void *addr) return; } + +int pg_dir_mmap(pg_dir_t *pgdir, void *addr, u32_t size, int prot, int flags, void **dst) +{ + int ret_val; + + ret_val = -EINVAL; + + if(pgdir && dst) { + ret_val = -EFAULT; + + /* FIXME: Lock the page directory */ + + if(flags & MAP_PHYS) { + u32_t attrs; + + /* map the physical memory addr:size into the page directory */ + /* FIXME: Add another flag for identity mappings and otherwise map non-identically */ + + attrs = PAGE_ATTR_PRESENT; + + if(!(prot & PROT_EXEC)) { + attrs |= PAGE_ATTR_NO_EXEC; + } + + if(prot & PROT_WRITE) { + attrs |= PAGE_ATTR_WRITABLE; + } + + if(prot & PROT_READ) { + attrs |= PAGE_ATTR_USER; + } + + ret_val = pg_dir_map(pgdir, addr, addr, size, attrs); + + if(!ret_val) { + /* also account for the new region */ + ret_val = _pg_dir_add_region(pgdir, addr, size, + REGION_MMAP, 0, REGION_SHARED); + + /* or unmap if it can't be accounted for */ + if(ret_val < 0) { + pg_dir_unmap(pgdir, addr, size); + } + } + } + + /* FIXME: Unlock the page directory */ + + if(!ret_val) { + /* success - write the result to *dst */ + *dst = addr; + } + } + + return(ret_val); +} + +int pg_dir_munmap(pg_dir_t *pgdir, void *addr, u32_t size) +{ + return(-ENOSYS); +} diff --git a/kernel/include/arch.h b/kernel/include/arch.h index f275d4f..cc88308 100644 --- a/kernel/include/arch.h +++ b/kernel/include/arch.h @@ -79,6 +79,7 @@ struct task { #define REGION_KSTACK 4 #define REGION_STACK 5 #define REGION_HEAP 6 +#define REGION_MMAP 7 #define REGION_SHARED (1 << 0) #define REGION_KERNEL (1 << 1) @@ -185,6 +186,8 @@ void* pg_dir_get_ustack(pg_dir_t*); void* pg_dir_sbrk(pg_dir_t*, i32_t); int pg_dir_foreach_region(pg_dir_t*, int(*)(pg_dir_t*, struct region*, void*), void*); int pg_dir_memcpy(pg_dir_t*, void*, pg_dir_t*, void*, u32_t); +int pg_dir_mmap(pg_dir_t*, void*, u32_t, int, int, void**); +int pg_dir_munmap(pg_dir_t*, void*, u32_t); #endif /* !__ASSEMBLY_SOURCE */