8eb7482b60af4b3bf7686a8e478ae0f54f1c7f52
[lunaix-os.git] / lunaix-os / kernel / mm / mmap.c
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>
6
7 #include <lunaix/syscall.h>
8 #include <lunaix/syscall_utils.h>
9
10 // any size beyond this is bullshit
11 #define BS_SIZE (KERNEL_MM_BASE - UMMAP_START)
12
13 int
14 mem_has_overlap(vm_regions_t* regions, ptr_t start, ptr_t end)
15 {
16     struct mm_region *pos, *n;
17     llist_for_each(pos, n, regions, head)
18     {
19         if (pos->end >= start && pos->start < start) {
20             return 1;
21         }
22
23         if (pos->end <= end && pos->start >= start) {
24             return 1;
25         }
26
27         if (pos->end >= end && pos->start < end) {
28             return 1;
29         }
30     }
31
32     return 0;
33 }
34
35 int
36 mem_adjust_inplace(vm_regions_t* regions,
37                    struct mm_region* region,
38                    ptr_t newend)
39 {
40     ssize_t len = newend - region->start;
41     if (len == 0) {
42         return 0;
43     }
44
45     if (len < 0) {
46         return EINVAL;
47     }
48
49     if (mem_has_overlap(regions, region->start, newend)) {
50         return ENOMEM;
51     }
52
53     region->end = newend;
54
55     return 0;
56 }
57
58 int
59 mem_map(void** addr_out,
60         struct mm_region** created,
61         void* addr,
62         struct v_file* file,
63         struct mmap_param* param)
64 {
65     assert_msg(addr, "addr can not be NULL");
66
67     ptr_t last_end = USER_START, found_loc = (ptr_t)addr;
68     struct mm_region *pos, *n;
69
70     vm_regions_t* vm_regions = &param->pvms->regions;
71
72     if ((param->flags & MAP_FIXED_NOREPLACE)) {
73         if (mem_has_overlap(vm_regions, found_loc, param->mlen + found_loc)) {
74             return EEXIST;
75         }
76         goto found;
77     }
78
79     if ((param->flags & MAP_FIXED)) {
80         int status =
81           mem_unmap(param->vms_mnt, vm_regions, found_loc, param->mlen);
82         if (status) {
83             return status;
84         }
85         goto found;
86     }
87
88     llist_for_each(pos, n, vm_regions, head)
89     {
90         if (last_end < found_loc) {
91             size_t avail_space = pos->start - found_loc;
92             if (pos->start > found_loc && avail_space > param->mlen) {
93                 goto found;
94             }
95             found_loc = pos->end + PG_SIZE;
96         }
97
98         last_end = pos->end;
99     }
100
101     return ENOMEM;
102
103 found:
104     if (found_loc >= KERNEL_MM_BASE || found_loc < USER_START) {
105         return ENOMEM;
106     }
107
108     struct mm_region* region = region_create_range(
109       found_loc,
110       param->mlen,
111       ((param->proct | param->flags) & 0x3f) | (param->type & ~0xffff));
112
113     region->mfile = file;
114     region->foff = param->offset;
115     region->flen = param->flen;
116     region->proc_vms = param->pvms;
117
118     region_add(vm_regions, region);
119
120     u32_t attr = PG_ALLOW_USER;
121     if ((param->proct & REGION_WRITE)) {
122         attr |= PG_WRITE;
123     }
124
125     for (u32_t i = 0; i < param->mlen; i += PG_SIZE) {
126         vmm_set_mapping(param->vms_mnt, found_loc + i, 0, attr, 0);
127     }
128
129     if (file) {
130         vfs_ref_file(file);
131     }
132
133     if (addr_out) {
134         *addr_out = found_loc;
135     }
136     if (created) {
137         *created = region;
138     }
139     return 0;
140 }
141
142 int
143 mem_remap(void** addr_out,
144           struct mm_region** remapped,
145           void* addr,
146           struct v_file* file,
147           struct mmap_param* param)
148 {
149 }
150
151 void
152 mem_sync_pages(ptr_t mnt,
153                struct mm_region* region,
154                ptr_t start,
155                ptr_t length,
156                int options)
157 {
158     if (!region->mfile || !(region->attr & REGION_WSHARED)) {
159         return;
160     }
161
162     v_mapping mapping;
163     for (size_t i = 0; i < length; i += PG_SIZE) {
164         if (!vmm_lookupat(mnt, start + i, &mapping)) {
165             continue;
166         }
167
168         if (PG_IS_DIRTY(*mapping.pte)) {
169             size_t offset = mapping.va - region->start + region->foff;
170             struct v_inode* inode = region->mfile->inode;
171             region->mfile->ops->write_page(inode, mapping.va, PG_SIZE, offset);
172             *mapping.pte &= ~PG_DIRTY;
173             cpu_invplg(mapping.pte);
174         } else if ((options & MS_INVALIDATE)) {
175             goto invalidate;
176         }
177
178         if (options & MS_INVALIDATE_ALL) {
179             goto invalidate;
180         }
181
182         continue;
183
184     invalidate:
185         *mapping.pte &= ~PG_PRESENT;
186         pmm_free_page(KERNEL_PID, mapping.pa);
187         cpu_invplg(mapping.pte);
188     }
189 }
190
191 int
192 mem_msync(ptr_t mnt,
193           vm_regions_t* regions,
194           ptr_t addr,
195           size_t length,
196           int options)
197 {
198     struct mm_region* pos = list_entry(regions->next, struct mm_region, head);
199     while (length && (ptr_t)&pos->head != (ptr_t)regions) {
200         if (pos->end >= addr && pos->start <= addr) {
201             size_t l = MIN(length, pos->end - addr);
202             mem_sync_pages(mnt, pos, addr, l, options);
203
204             addr += l;
205             length -= l;
206         }
207         pos = list_entry(pos->head.next, struct mm_region, head);
208     }
209
210     if (length) {
211         return ENOMEM;
212     }
213
214     return 0;
215 }
216
217 void
218 mem_unmap_region(ptr_t mnt, struct mm_region* region)
219 {
220     size_t len = ROUNDUP(region->end - region->start, PG_SIZE);
221     mem_sync_pages(mnt, region, region->start, len, 0);
222
223     for (size_t i = region->start; i <= region->end; i += PG_SIZE) {
224         ptr_t pa = vmm_del_mapping(mnt, i);
225         if (pa) {
226             pmm_free_page(__current->pid, pa);
227         }
228     }
229     llist_delete(&region->head);
230     region_release(region);
231 }
232
233 int
234 mem_unmap(ptr_t mnt, vm_regions_t* regions, void* addr, size_t length)
235 {
236     length = ROUNDUP(length, PG_SIZE);
237     ptr_t cur_addr = PG_ALIGN(addr);
238     struct mm_region *pos, *n;
239
240     llist_for_each(pos, n, regions, head)
241     {
242         if (pos->start <= cur_addr && pos->end >= cur_addr) {
243             break;
244         }
245     }
246
247     while (&pos->head != regions && cur_addr >= pos->start) {
248         u32_t l = pos->end - cur_addr;
249         pos->end = cur_addr;
250
251         if (l > length) {
252             // unmap cause discontinunity in a memory region -  do split
253             struct mm_region* region = valloc(sizeof(struct mm_region));
254             *region = *pos;
255             region->start = cur_addr + length;
256             llist_insert_after(&pos->head, &region->head);
257             l = length;
258         }
259
260         mem_sync_pages(mnt, pos, cur_addr, l, 0);
261
262         for (size_t i = 0; i < l; i += PG_SIZE) {
263             ptr_t pa = vmm_del_mapping(mnt, cur_addr + i);
264             if (pa) {
265                 pmm_free_page(pos->proc_vms->pid, pa);
266             }
267         }
268
269         n = container_of(pos->head.next, typeof(*pos), head);
270         if (pos->end == pos->start) {
271             llist_delete(&pos->head);
272             region_release(pos);
273         }
274
275         pos = n;
276         length -= l;
277         cur_addr += length;
278     }
279
280     return 0;
281 }
282
283 __DEFINE_LXSYSCALL3(void*, sys_mmap, void*, addr, size_t, length, va_list, lst)
284 {
285     int proct = va_arg(lst, int);
286     int fd = va_arg(lst, u32_t);
287     off_t offset = va_arg(lst, off_t);
288     int options = va_arg(lst, int);
289     int errno = 0;
290     void* result = (void*)-1;
291
292     if (!length || length > BS_SIZE || !PG_ALIGNED(addr)) {
293         errno = EINVAL;
294         goto done;
295     }
296
297     if (!addr) {
298         addr = UMMAP_START;
299     } else if (addr < UMMAP_START || addr + length >= UMMAP_END) {
300         if (!(options & (MAP_FIXED | MAP_FIXED_NOREPLACE))) {
301             errno = ENOMEM;
302             goto done;
303         }
304     }
305
306     struct v_file* file = NULL;
307
308     if (!(options & MAP_ANON)) {
309         struct v_fd* vfd;
310         if ((errno = vfs_getfd(fd, &vfd))) {
311             goto done;
312         }
313
314         file = vfd->file;
315         if (!file->ops->read_page) {
316             errno = ENODEV;
317             goto done;
318         }
319     }
320
321     struct mmap_param param = { .flags = options,
322                                 .mlen = ROUNDUP(length, PG_SIZE),
323                                 .offset = offset,
324                                 .type = REGION_TYPE_GENERAL,
325                                 .proct = proct,
326                                 .pvms = &__current->mm,
327                                 .vms_mnt = VMS_SELF };
328
329     errno = mem_map(&result, NULL, addr, file, &param);
330
331 done:
332     __current->k_status = errno;
333     return result;
334 }
335
336 __DEFINE_LXSYSCALL2(void, munmap, void*, addr, size_t, length)
337 {
338     return mem_unmap(VMS_SELF, &__current->mm.regions, addr, length);
339 }
340
341 __DEFINE_LXSYSCALL3(int, msync, void*, addr, size_t, length, int, flags)
342 {
343     if (!PG_ALIGNED(addr) || ((flags & MS_ASYNC) && (flags & MS_SYNC))) {
344         return DO_STATUS(EINVAL);
345     }
346
347     int status =
348       mem_msync(VMS_SELF, &__current->mm.regions, addr, length, flags);
349
350     return DO_STATUS(status);
351 }