X-Git-Url: https://scm.lunaixsky.com/lunaix-os.git/blobdiff_plain/a13ddcba315eaa75dca84dde30b17a78b2933354..b632f535c4a6882bdca0317fb88cbe6e165f24eb:/lunaix-os/kernel/mm/mmap.c diff --git a/lunaix-os/kernel/mm/mmap.c b/lunaix-os/kernel/mm/mmap.c index 8eb6b20..8a4c9bc 100644 --- a/lunaix-os/kernel/mm/mmap.c +++ b/lunaix-os/kernel/mm/mmap.c @@ -1,80 +1,259 @@ #include #include -#include #include #include #include #include +#include -void* -mem_map(ptr_t mnt, - struct llist_header* regions, - void* addr, - struct v_file* file, - u32_t offset, - size_t length, - u32_t proct) +#include + +// any size beyond this is bullshit +#define BS_SIZE (KERNEL_EXEC - UMMAP_START) + +int +mem_has_overlap(vm_regions_t* regions, ptr_t start, ptr_t end) { - if (!length || (length & (PG_SIZE - 1)) || (offset & (PG_SIZE - 1))) { - __current->k_status = EINVAL; - return (void*)-1; + 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; + } } - // read_page is not supported - if (!file->ops->read_page) { - __current->k_status = ENODEV; - return (void*)-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; } - ptr_t last_end = USER_START; + if (mem_has_overlap(regions, region->start, newend)) { + return ENOMEM; + } + + region->end = newend; + + return 0; +} + +int +mem_map(void** addr_out, + struct mm_region** created, + ptr_t addr, + struct v_file* file, + struct mmap_param* param) +{ + assert_msg(addr, "addr can not be NULL"); + + ptr_t last_end = USER_START, found_loc = addr; struct mm_region *pos, *n; - llist_for_each(pos, n, regions, head) + + 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; + } + goto found; + } + + 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; } - __current->k_status = ENOMEM; - return (void*)-1; + return ENOMEM; found: - addr = last_end; - ptr_t end = addr + length; + 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_add(regions, addr, end, proct); region->mfile = file; - region->offset = offset; + region->foff = param->offset; + region->flen = param->flen; + region->proc_vms = param->pvms; + + 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); + } + + 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, + ptr_t start, + ptr_t length, + int options) +{ + if (!region->mfile || !(region->attr & REGION_WSHARED)) { + return; + } + + v_mapping mapping; + for (size_t i = 0; i < length; i += PG_SIZE) { + if (!vmm_lookupat(mnt, start + i, &mapping)) { + continue; + } + + if (PG_IS_DIRTY(*mapping.pte)) { + size_t offset = mapping.va - region->start + region->foff; + struct v_inode* inode = region->mfile->inode; + + region->mfile->ops->write_page( + inode, (void*)mapping.va, PG_SIZE, offset); + + *mapping.pte &= ~PG_DIRTY; + + cpu_flush_page((ptr_t)mapping.pte); + } else if ((options & MS_INVALIDATE)) { + 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); } +} + +int +mem_msync(ptr_t mnt, + vm_regions_t* regions, + ptr_t addr, + size_t length, + int options) +{ + struct mm_region* pos = list_entry(regions->next, struct mm_region, head); + while (length && (ptr_t)&pos->head != (ptr_t)regions) { + if (pos->end >= addr && pos->start <= addr) { + size_t l = MIN(length, pos->end - addr); + mem_sync_pages(mnt, pos, addr, l, options); + + addr += l; + length -= l; + } + pos = list_entry(pos->head.next, struct mm_region, head); + } + + if (length) { + return ENOMEM; + } + + 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); - return addr; + 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); } -void* -mem_unmap(ptr_t mnt, struct llist_header* regions, void* addr, size_t length) +int +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; @@ -87,55 +266,102 @@ mem_unmap(ptr_t mnt, struct llist_header* regions, void* addr, size_t length) l = length; } + mem_sync_pages(mnt, pos, cur_addr, l, 0); + 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_LXSYSCALL5(void*, - mmap, - void*, - addr, - size_t, - length, - int, - proct, - int, - fd, - size_t, - offset) +__DEFINE_LXSYSCALL3(void*, sys_mmap, void*, addr, size_t, length, va_list, lst) { + int proct = va_arg(lst, int); + int fd = va_arg(lst, u32_t); + off_t offset = va_arg(lst, off_t); + int options = va_arg(lst, int); int errno = 0; - struct v_fd* vfd; - if ((errno = vfs_getfd(fd, &vfd))) { - __current->k_status = errno; - return (void*)-1; - } - - return mem_map(PD_REFERENCED, - &__current->mm.regions, - addr, - vfd->file, - offset, - length, - proct); + void* result = (void*)-1; + + ptr_t addr_ptr = (ptr_t)addr; + + if (!length || length > BS_SIZE || !PG_ALIGNED(addr_ptr)) { + errno = EINVAL; + 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 = 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; + } + } + + 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, 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(PD_REFERENCED, &__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) +{ + if (!PG_ALIGNED(addr) || ((flags & MS_ASYNC) && (flags & MS_SYNC))) { + return DO_STATUS(EINVAL); + } + + 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