]> git.corax.cc Git - corax/commitdiff
Add pg_dir_mmap() and pg_dir_munmap() functions to implement the low-level half of...
authorMatthias Kruk <m@m10k.eu>
Wed, 18 Dec 2019 08:44:15 +0000 (17:44 +0900)
committerMatthias Kruk <m@m10k.eu>
Wed, 18 Dec 2019 08:44:15 +0000 (17:44 +0900)
kernel/arch/paging.c
kernel/include/arch.h

index 23a36e666742d98b1c1567cf6a2ec221b3b442be..e31b0d2201ace236c5d3698e18b9aff675c5fab4 100644 (file)
@@ -23,6 +23,7 @@
 #include <debug.h>
 #include <arch.h>
 #include <string.h>
+#include <sys/mman.h>
 #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);
+}
index f275d4f05396cd87cb3b863f9d79eecbcdcecc1c..cc8830895188eff7068c181926fb962f3c92b22b 100644 (file)
@@ -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 */