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