1 #include <lunaix/mm/mmap.h>
2 #include <lunaix/mm/pmm.h>
3 #include <lunaix/mm/valloc.h>
4 #include <lunaix/mm/vmm.h>
5 #include <lunaix/spike.h>
7 #include <lunaix/syscall.h>
8 #include <lunaix/syscall_utils.h>
10 // any size beyond this is bullshit
11 #define BS_SIZE (2 << 30)
14 mem_map(void** addr_out,
16 vm_regions_t* regions,
24 ptr_t last_end = USER_START;
25 struct mm_region *pos, *n;
27 if ((options & MAP_FIXED)) {
28 pos = region_get(regions, addr);
36 llist_for_each(pos, n, regions, head)
38 if (pos->start - last_end >= length && last_end >= addr) {
49 struct mm_region* region =
50 region_create_range(addr, length, proct | (options & 0x1f));
52 region->offset = offset;
54 region_add(regions, region);
56 u32_t attr = PG_ALLOW_USER;
57 if ((proct & REGION_WRITE)) {
61 for (u32_t i = 0; i < length; i += PG_SIZE) {
62 vmm_set_mapping(mnt, addr + i, 0, attr, 0);
70 mem_sync_pages(ptr_t mnt,
71 struct mm_region* region,
76 if (!region->mfile || !(region->attr & REGION_WSHARED)) {
81 for (size_t i = 0; i < length; i += PG_SIZE) {
82 if (!vmm_lookupat(mnt, start + i, &mapping)) {
85 if (PG_IS_DIRTY(*mapping.pte)) {
86 size_t offset = mapping.va - region->start + region->offset;
87 struct v_inode* inode = region->mfile->inode;
88 region->mfile->ops->write_page(inode, mapping.va, PG_SIZE, offset);
89 *mapping.pte &= ~PG_DIRTY;
90 cpu_invplg(mapping.va);
91 } else if ((options & MS_INVALIDATE)) {
92 *mapping.pte &= ~PG_PRESENT;
93 cpu_invplg(mapping.va);
100 vm_regions_t* regions,
105 struct mm_region* pos = list_entry(regions->next, struct mm_region, head);
106 while (length && (ptr_t)&pos->head != (ptr_t)regions) {
107 if (pos->end >= addr && pos->start <= addr) {
108 size_t l = MIN(length, pos->end - addr);
109 mem_sync_pages(mnt, pos, addr, l, options);
114 pos = list_entry(pos->head.next, struct mm_region, head);
125 mem_unmap(ptr_t mnt, vm_regions_t* regions, void* addr, size_t length)
127 length = ROUNDUP(length, PG_SIZE);
128 ptr_t cur_addr = ROUNDDOWN((ptr_t)addr, PG_SIZE);
129 struct mm_region *pos, *n;
131 llist_for_each(pos, n, regions, head)
133 if (pos->start <= cur_addr) {
138 while (&pos->head != regions && cur_addr > pos->start) {
139 u32_t l = pos->end - cur_addr;
143 // unmap cause discontinunity in a memory region - do split
144 struct mm_region* region = valloc(sizeof(struct mm_region));
146 region->start = cur_addr + length;
147 llist_insert_after(&pos->head, ®ion->head);
151 mem_sync_pages(mnt, pos, cur_addr, l, 0);
153 for (size_t i = 0; i < l; i += PG_SIZE) {
154 ptr_t pa = vmm_del_mapping(mnt, cur_addr + i);
156 pmm_free_page(__current->pid, pa);
160 n = container_of(pos->head.next, typeof(*pos), head);
161 if (pos->end == pos->start) {
162 llist_delete(&pos->head);
172 __DEFINE_LXSYSCALL3(void*, sys_mmap, void*, addr, size_t, length, va_list, lst)
174 int proct = va_arg(lst, int);
175 int fd = va_arg(lst, u32_t);
176 off_t offset = va_arg(lst, off_t);
177 int options = va_arg(lst, int);
179 void* result = (void*)-1;
181 if (!length || length > BS_SIZE || !PG_ALIGNED(addr)) {
187 if ((errno = vfs_getfd(fd, &vfd))) {
191 struct v_file* file = vfd->file;
193 if (!(options & MAP_ANON)) {
194 if (!file->ops->read_page) {
202 length = ROUNDUP(length, PG_SIZE);
204 errno = mem_map(&result,
206 &__current->mm.regions,
215 __current->k_status = errno;
219 __DEFINE_LXSYSCALL2(void, munmap, void*, addr, size_t, length)
221 return mem_unmap(VMS_SELF, &__current->mm.regions, addr, length);
224 __DEFINE_LXSYSCALL3(int, msync, void*, addr, size_t, length, int, flags)
226 if (!PG_ALIGNED(addr) || ((flags & MS_ASYNC) && (flags & MS_SYNC))) {
227 return DO_STATUS(EINVAL);
231 mem_msync(VMS_SELF, &__current->mm.regions, addr, length, flags);
233 return DO_STATUS(status);