#include <debug.h>
#include <arch.h>
#include <string.h>
+#include <sys/mman.h>
#include "cpu.h"
#include "paging.h"
#include "heap.h"
break;
+ case REGION_MMAP:
case REGION_TEXT:
case REGION_BSS:
case REGION_DATA:
*/
break;
+ case REGION_MMAP:
case REGION_TEXT:
case REGION_BSS:
case REGION_DATA:
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);
}
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);
+}
#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)
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 */