5-malloc.md (#25)
[lunaix-os.git] / lunaix-os / includes / lunaix / mm / procvm.h
1 #ifndef __LUNAIX_PROCVM_H
2 #define __LUNAIX_PROCVM_H
3
4 #include <lunaix/ds/llist.h>
5 #include <lunaix/ds/mutex.h>
6 #include <lunaix/fs.h>
7 #include <lunaix/types.h>
8
9 struct proc_mm;
10 struct proc_info;
11
12 struct mm_region
13 {
14     struct llist_header head; // must be first field!
15     struct proc_mm* proc_vms;
16
17     // file mapped to this region
18     struct v_file* mfile;
19     // mapped file offset
20     off_t foff;
21     // mapped file length
22     u32_t flen; // XXX it seems that we don't need this actually..
23
24     ptr_t start;
25     ptr_t end;
26     u32_t attr;
27
28     void** index; // fast reference, to accelerate access to this very region.
29
30     void* data;
31     // when a region is copied
32     void (*region_copied)(struct mm_region*);
33     // when a region is unmapped
34     void (*destruct_region)(struct mm_region*);
35 };
36
37 struct remote_vmctx
38 {
39     ptr_t vms_mnt;
40     ptr_t local_mnt;
41     ptr_t remote;
42     size_t page_cnt;
43 };
44
45
46 static inline void
47 mm_index(void** index, struct mm_region* target)
48 {
49     *index = (void*)target;
50     target->index = index;
51 }
52
53 typedef struct llist_header vm_regions_t;
54
55 struct proc_mm
56 {
57     // virtual memory root (i.e. root page table)
58     ptr_t vmroot;
59     vm_regions_t regions;
60     struct mm_region* heap;
61     struct proc_info* proc;
62 };
63
64 /**
65  * @brief Create a process virtual memory space descriptor
66  * 
67  * @param proc 
68  * @return struct proc_mm* 
69  */
70 struct proc_mm*
71 procvm_create(struct proc_info* proc);
72
73 /**
74  * @brief Initialize the vm of `proc` to duplication of current process
75  * 
76  * @param proc 
77  * @return struct proc_mm* 
78  */
79 void
80 procvm_dup(struct proc_info* proc);
81
82 void
83 procvm_cleanup(ptr_t vm_mnt, struct proc_info* proc);
84
85
86 /**
87  * @brief Initialize the vm of `proc` as a clean slate which contains
88  * nothing but shared global mapping of kernel image.
89  * 
90  * @param proc 
91  */
92 void
93 procvm_init_clean(struct proc_info* proc);
94
95
96 /*
97     remote virtual memory manipulation
98 */
99
100 #define REMOTEVM_MAX_PAGES 128
101
102 ptr_t
103 procvm_enter_remote_transaction(struct remote_vmctx* rvmctx, struct proc_mm* mm,
104                     ptr_t vm_mnt, ptr_t remote_base, size_t size);
105
106 int
107 procvm_copy_remote(struct remote_vmctx* rvmctx, 
108                    ptr_t remote_dest, void* local_src, size_t sz);
109
110 void
111 procvm_exit_remote_transaction(struct remote_vmctx* rvmctx);
112
113 #endif /* __LUNAIX_PROCVM_H */