a76b9de979dd8e93ad68c6e9a24fdc7e7a571664
[lunaix-os.git] / lunaix-os / kernel / mm / dmm.c
1 #include <lunaix/mm/mmap.h>
2 #include <lunaix/mm/vmm.h>
3 #include <lunaix/process.h>
4 #include <lunaix/status.h>
5
6 #include <lunaix/spike.h>
7 #include <lunaix/syscall.h>
8 #include <lunaix/syscall_utils.h>
9
10 void
11 __heap_copied(struct mm_region* region)
12 {
13     mm_index((void**)&region->proc_vms->heap, region);
14 }
15
16 int
17 create_heap(struct proc_mm* pvms, ptr_t addr)
18 {
19     struct mmap_param map_param = { .pvms = pvms,
20                                     .vms_mnt = VMS_SELF,
21                                     .flags = MAP_ANON | MAP_PRIVATE,
22                                     .type = REGION_TYPE_HEAP,
23                                     .proct = PROT_READ | PROT_WRITE,
24                                     .mlen = PG_SIZE };
25     int status = 0;
26     struct mm_region* heap;
27     if ((status = mmap_user(NULL, &heap, addr, NULL, &map_param))) {
28         return status;
29     }
30
31     heap->region_copied = __heap_copied;
32     mm_index((void**)&pvms->heap, heap);
33
34     return status;
35 }
36
37 __DEFINE_LXSYSCALL1(void*, sbrk, ssize_t, incr)
38 {
39     struct proc_mm* pvms = vmspace(__current);
40     struct mm_region* heap = pvms->heap;
41
42     assert(heap);
43     int err = mem_adjust_inplace(&pvms->regions, heap, heap->end + incr);
44     if (err) {
45         return (void*)DO_STATUS(err);
46     }
47     return (void*)heap->end;
48 }
49
50 __DEFINE_LXSYSCALL1(int, brk, void*, addr)
51 {
52     struct proc_mm* pvms = vmspace(__current);
53     struct mm_region* heap = pvms->heap;
54
55     if (!heap) {
56         return DO_STATUS(create_heap(pvms, (ptr_t)addr));
57     }
58
59     assert(heap);
60     int err = mem_adjust_inplace(&pvms->regions, heap, (ptr_t)addr);
61     return DO_STATUS(err);
62 }