Merge branch 'master' into prog-loader
[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 void
143 mem_sync_pages(ptr_t mnt,
144                struct mm_region* region,
145                ptr_t start,
146                ptr_t length,
147                int options)
148 {
149     if (!region->mfile || !(region->attr & REGION_WSHARED)) {
150         return;
151     }
152
153     v_mapping mapping;
154     for (size_t i = 0; i < length; i += PG_SIZE) {
155         if (!vmm_lookupat(mnt, start + i, &mapping)) {
156             continue;
157         }
158
159         if (PG_IS_DIRTY(*mapping.pte)) {
160             size_t offset = mapping.va - region->start + region->foff;
161             struct v_inode* inode = region->mfile->inode;
162             region->mfile->ops->write_page(inode, mapping.va, PG_SIZE, offset);
163             *mapping.pte &= ~PG_DIRTY;
164             cpu_invplg(mapping.pte);
165         } else if ((options & MS_INVALIDATE)) {
166             goto invalidate;
167         }
168
169         if (options & MS_INVALIDATE_ALL) {
170             goto invalidate;
171         }
172
173         continue;
174
175     invalidate:
176         *mapping.pte &= ~PG_PRESENT;
177         pmm_free_page(KERNEL_PID, mapping.pa);
178         cpu_invplg(mapping.pte);
179     }
180 }
181
182 int
183 mem_msync(ptr_t mnt,
184           vm_regions_t* regions,
185           ptr_t addr,
186           size_t length,
187           int options)
188 {
189     struct mm_region* pos = list_entry(regions->next, struct mm_region, head);
190     while (length && (ptr_t)&pos->head != (ptr_t)regions) {
191         if (pos->end >= addr && pos->start <= addr) {
192             size_t l = MIN(length, pos->end - addr);
193             mem_sync_pages(mnt, pos, addr, l, options);
194
195             addr += l;
196             length -= l;
197         }
198         pos = list_entry(pos->head.next, struct mm_region, head);
199     }
200
201     if (length) {
202         return ENOMEM;
203     }
204
205     return 0;
206 }
207
208 void
209 mem_unmap_region(ptr_t mnt, struct mm_region* region)
210 {
211     size_t len = ROUNDUP(region->end - region->start, PG_SIZE);
212     mem_sync_pages(mnt, region, region->start, len, 0);
213
214     for (size_t i = region->start; i <= region->end; i += PG_SIZE) {
215         ptr_t pa = vmm_del_mapping(mnt, i);
216         if (pa) {
217             pmm_free_page(__current->pid, pa);
218         }
219     }
220     llist_delete(&region->head);
221     region_release(region);
222 }
223
224 int
225 mem_unmap(ptr_t mnt, vm_regions_t* regions, void* addr, size_t length)
226 {
227     length = ROUNDUP(length, PG_SIZE);
228     ptr_t cur_addr = PG_ALIGN(addr);
229     struct mm_region *pos, *n;
230
231     llist_for_each(pos, n, regions, head)
232     {
233         if (pos->start <= cur_addr && pos->end >= cur_addr) {
234             break;
235         }
236     }
237
238     while (&pos->head != regions && cur_addr >= pos->start) {
239         u32_t l = pos->end - cur_addr;
240         pos->end = cur_addr;
241
242         if (l > length) {
243             // unmap cause discontinunity in a memory region -  do split
244             struct mm_region* region = valloc(sizeof(struct mm_region));
245             *region = *pos;
246             region->start = cur_addr + length;
247             llist_insert_after(&pos->head, &region->head);
248             l = length;
249         }
250
251         mem_sync_pages(mnt, pos, cur_addr, l, 0);
252
253         for (size_t i = 0; i < l; i += PG_SIZE) {
254             ptr_t pa = vmm_del_mapping(mnt, cur_addr + i);
255             if (pa) {
256                 pmm_free_page(pos->proc_vms->pid, pa);
257             }
258         }
259
260         n = container_of(pos->head.next, typeof(*pos), head);
261         if (pos->end == pos->start) {
262             llist_delete(&pos->head);
263             region_release(pos);
264         }
265
266         pos = n;
267         length -= l;
268         cur_addr += length;
269     }
270
271     return 0;
272 }
273
274 __DEFINE_LXSYSCALL3(void*, sys_mmap, void*, addr, size_t, length, va_list, lst)
275 {
276     int proct = va_arg(lst, int);
277     int fd = va_arg(lst, u32_t);
278     off_t offset = va_arg(lst, off_t);
279     int options = va_arg(lst, int);
280     int errno = 0;
281     void* result = (void*)-1;
282
283     if (!length || length > BS_SIZE || !PG_ALIGNED(addr)) {
284         errno = EINVAL;
285         goto done;
286     }
287
288     if (!addr) {
289         addr = UMMAP_START;
290     } else if (addr < UMMAP_START || addr + length >= UMMAP_END) {
291         errno = ENOMEM;
292         goto done;
293     }
294
295     struct v_fd* vfd;
296     if ((errno = vfs_getfd(fd, &vfd))) {
297         goto done;
298     }
299
300     struct v_file* file = vfd->file;
301
302     if (!(options & MAP_ANON)) {
303         if (!file->ops->read_page) {
304             errno = ENODEV;
305             goto done;
306         }
307     } else {
308         file = NULL;
309     }
310
311     struct mmap_param param = { .flags = options,
312                                 .mlen = ROUNDUP(length, PG_SIZE),
313                                 .offset = offset,
314                                 .type = REGION_TYPE_GENERAL,
315                                 .proct = proct,
316                                 .pvms = &__current->mm,
317                                 .vms_mnt = VMS_SELF };
318
319     errno = mem_map(&result, NULL, addr, file, &param);
320
321 done:
322     __current->k_status = errno;
323     return result;
324 }
325
326 __DEFINE_LXSYSCALL2(void, munmap, void*, addr, size_t, length)
327 {
328     return mem_unmap(VMS_SELF, &__current->mm.regions, addr, length);
329 }
330
331 __DEFINE_LXSYSCALL3(int, msync, void*, addr, size_t, length, int, flags)
332 {
333     if (!PG_ALIGNED(addr) || ((flags & MS_ASYNC) && (flags & MS_SYNC))) {
334         return DO_STATUS(EINVAL);
335     }
336
337     int status =
338       mem_msync(VMS_SELF, &__current->mm.regions, addr, length, flags);
339
340     return DO_STATUS(status);
341 }