X-Git-Url: https://scm.lunaixsky.com/lunaix-os.git/blobdiff_plain/9b8e0c494de6b447b44454112748f702dffec90d..b632f535c4a6882bdca0317fb88cbe6e165f24eb:/lunaix-os/kernel/mm/mmap.c diff --git a/lunaix-os/kernel/mm/mmap.c b/lunaix-os/kernel/mm/mmap.c index c1ebe09..8a4c9bc 100644 --- a/lunaix-os/kernel/mm/mmap.c +++ b/lunaix-os/kernel/mm/mmap.c @@ -7,65 +7,152 @@ #include #include +#include + // any size beyond this is bullshit -#define BS_SIZE (2 << 30) +#define BS_SIZE (KERNEL_EXEC - UMMAP_START) + +int +mem_has_overlap(vm_regions_t* regions, ptr_t start, ptr_t end) +{ + struct mm_region *pos, *n; + llist_for_each(pos, n, regions, head) + { + if (pos->end >= start && pos->start < start) { + return 1; + } + + if (pos->end <= end && pos->start >= start) { + return 1; + } + + if (pos->end >= end && pos->start < end) { + return 1; + } + } + + return 0; +} + +int +mem_adjust_inplace(vm_regions_t* regions, + struct mm_region* region, + ptr_t newend) +{ + ssize_t len = newend - region->start; + if (len == 0) { + return 0; + } + + if (len < 0) { + return EINVAL; + } + + if (mem_has_overlap(regions, region->start, newend)) { + return ENOMEM; + } + + region->end = newend; + + return 0; +} int mem_map(void** addr_out, - ptr_t mnt, - vm_regions_t* regions, - void* addr, + struct mm_region** created, + ptr_t addr, struct v_file* file, - off_t offset, - size_t length, - u32_t proct, - u32_t options) + struct mmap_param* param) { - ptr_t last_end = USER_START; + assert_msg(addr, "addr can not be NULL"); + + ptr_t last_end = USER_START, found_loc = addr; struct mm_region *pos, *n; - if ((options & MAP_FIXED)) { - pos = region_get(regions, addr); - if (!pos) { - last_end = addr; - goto found; + vm_regions_t* vm_regions = ¶m->pvms->regions; + + if ((param->flags & MAP_FIXED_NOREPLACE)) { + if (mem_has_overlap(vm_regions, found_loc, param->mlen + found_loc)) { + return EEXIST; } - return EEXIST; + goto found; } - llist_for_each(pos, n, regions, head) + if ((param->flags & MAP_FIXED)) { + int status = + mem_unmap(param->vms_mnt, vm_regions, found_loc, param->mlen); + if (status) { + return status; + } + goto found; + } + + llist_for_each(pos, n, vm_regions, head) { - if (pos->start - last_end >= length && last_end >= addr) { - goto found; + if (last_end < found_loc) { + size_t avail_space = pos->start - found_loc; + if (pos->start > found_loc && avail_space > param->mlen) { + goto found; + } + found_loc = pos->end + PG_SIZE; } + last_end = pos->end; } return ENOMEM; found: - addr = last_end; + if (found_loc >= KERNEL_EXEC || found_loc < USER_START) { + return ENOMEM; + } + + struct mm_region* region = region_create_range( + found_loc, + param->mlen, + ((param->proct | param->flags) & 0x3f) | (param->type & ~0xffff)); - struct mm_region* region = - region_create_range(addr, length, proct | (options & 0x1f)); region->mfile = file; - region->offset = offset; + region->foff = param->offset; + region->flen = param->flen; + region->proc_vms = param->pvms; - region_add(regions, region); + region_add(vm_regions, region); u32_t attr = PG_ALLOW_USER; - if ((proct & REGION_WRITE)) { + if ((param->proct & REGION_WRITE)) { attr |= PG_WRITE; } - for (u32_t i = 0; i < length; i += PG_SIZE) { - vmm_set_mapping(mnt, addr + i, 0, attr, 0); + for (u32_t i = 0; i < param->mlen; i += PG_SIZE) { + vmm_set_mapping(param->vms_mnt, found_loc + i, 0, attr, 0); + } + + if (file) { + vfs_ref_file(file); } - *addr_out = addr; + if (addr_out) { + *addr_out = (void*)found_loc; + } + if (created) { + *created = region; + } return 0; } +int +mem_remap(void** addr_out, + struct mm_region** remapped, + void* addr, + struct v_file* file, + struct mmap_param* param) +{ + // TODO + + return EINVAL; +} + void mem_sync_pages(ptr_t mnt, struct mm_region* region, @@ -82,16 +169,31 @@ mem_sync_pages(ptr_t mnt, if (!vmm_lookupat(mnt, start + i, &mapping)) { continue; } + if (PG_IS_DIRTY(*mapping.pte)) { - size_t offset = mapping.va - region->start + region->offset; + size_t offset = mapping.va - region->start + region->foff; struct v_inode* inode = region->mfile->inode; - region->mfile->ops->write_page(inode, mapping.va, PG_SIZE, offset); + + region->mfile->ops->write_page( + inode, (void*)mapping.va, PG_SIZE, offset); + *mapping.pte &= ~PG_DIRTY; - cpu_invplg(mapping.va); + + cpu_flush_page((ptr_t)mapping.pte); } else if ((options & MS_INVALIDATE)) { - *mapping.pte &= ~PG_PRESENT; - cpu_invplg(mapping.va); + goto invalidate; + } + + if (options & MS_INVALIDATE_ALL) { + goto invalidate; } + + continue; + + invalidate: + *mapping.pte &= ~PG_PRESENT; + pmm_free_page(KERNEL_PID, mapping.pa); + cpu_flush_page((ptr_t)mapping.pte); } } @@ -121,21 +223,37 @@ mem_msync(ptr_t mnt, return 0; } +void +mem_unmap_region(ptr_t mnt, struct mm_region* region) +{ + size_t len = ROUNDUP(region->end - region->start, PG_SIZE); + mem_sync_pages(mnt, region, region->start, len, 0); + + for (size_t i = region->start; i <= region->end; i += PG_SIZE) { + ptr_t pa = vmm_del_mapping(mnt, i); + if (pa) { + pmm_free_page(__current->pid, pa); + } + } + llist_delete(®ion->head); + region_release(region); +} + int -mem_unmap(ptr_t mnt, vm_regions_t* regions, void* addr, size_t length) +mem_unmap(ptr_t mnt, vm_regions_t* regions, ptr_t addr, size_t length) { length = ROUNDUP(length, PG_SIZE); - ptr_t cur_addr = ROUNDDOWN((ptr_t)addr, PG_SIZE); + ptr_t cur_addr = PG_ALIGN(addr); struct mm_region *pos, *n; llist_for_each(pos, n, regions, head) { - if (pos->start <= cur_addr) { + if (pos->start <= cur_addr && pos->end >= cur_addr) { break; } } - while (&pos->head != regions && cur_addr > pos->start) { + while (&pos->head != regions && cur_addr >= pos->start) { u32_t l = pos->end - cur_addr; pos->end = cur_addr; @@ -153,20 +271,22 @@ mem_unmap(ptr_t mnt, vm_regions_t* regions, void* addr, size_t length) for (size_t i = 0; i < l; i += PG_SIZE) { ptr_t pa = vmm_del_mapping(mnt, cur_addr + i); if (pa) { - pmm_free_page(__current->pid, pa); + pmm_free_page(pos->proc_vms->pid, pa); } } n = container_of(pos->head.next, typeof(*pos), head); if (pos->end == pos->start) { llist_delete(&pos->head); - vfree(pos); + region_release(pos); } pos = n; length -= l; cur_addr += length; } + + return 0; } __DEFINE_LXSYSCALL3(void*, sys_mmap, void*, addr, size_t, length, va_list, lst) @@ -178,47 +298,57 @@ __DEFINE_LXSYSCALL3(void*, sys_mmap, void*, addr, size_t, length, va_list, lst) int errno = 0; void* result = (void*)-1; - if (!length || length > BS_SIZE || !PG_ALIGNED(addr)) { + ptr_t addr_ptr = (ptr_t)addr; + + if (!length || length > BS_SIZE || !PG_ALIGNED(addr_ptr)) { errno = EINVAL; goto done; } - struct v_fd* vfd; - if ((errno = vfs_getfd(fd, &vfd))) { - goto done; + if (!addr_ptr) { + addr_ptr = UMMAP_START; + } else if (addr_ptr < UMMAP_START || addr_ptr + length >= UMMAP_END) { + if (!(options & (MAP_FIXED | MAP_FIXED_NOREPLACE))) { + errno = ENOMEM; + goto done; + } } - struct v_file* file = vfd->file; + struct v_file* file = NULL; if (!(options & MAP_ANON)) { + struct v_fd* vfd; + if ((errno = vfs_getfd(fd, &vfd))) { + goto done; + } + + file = vfd->file; if (!file->ops->read_page) { errno = ENODEV; goto done; } - } else { - file = NULL; } - length = ROUNDUP(length, PG_SIZE); + struct mmap_param param = { .flags = options, + .mlen = ROUNDUP(length, PG_SIZE), + .flen = length, + .offset = offset, + .type = REGION_TYPE_GENERAL, + .proct = proct, + .pvms = (struct proc_mm*)&__current->mm, + .vms_mnt = VMS_SELF }; - errno = mem_map(&result, - VMS_SELF, - &__current->mm.regions, - addr, - file, - offset, - length, - proct, - options); + errno = mem_map(&result, NULL, addr_ptr, file, ¶m); done: __current->k_status = errno; return result; } -__DEFINE_LXSYSCALL2(void, munmap, void*, addr, size_t, length) +__DEFINE_LXSYSCALL2(int, munmap, void*, addr, size_t, length) { - return mem_unmap(VMS_SELF, &__current->mm.regions, addr, length); + return mem_unmap( + VMS_SELF, (vm_regions_t*)&__current->mm.regions, (ptr_t)addr, length); } __DEFINE_LXSYSCALL3(int, msync, void*, addr, size_t, length, int, flags) @@ -227,8 +357,11 @@ __DEFINE_LXSYSCALL3(int, msync, void*, addr, size_t, length, int, flags) return DO_STATUS(EINVAL); } - int status = - mem_msync(VMS_SELF, &__current->mm.regions, addr, length, flags); + int status = mem_msync(VMS_SELF, + (vm_regions_t*)&__current->mm.regions, + (ptr_t)addr, + length, + flags); return DO_STATUS(status); } \ No newline at end of file