fix leakage on mid-level page table when freeing vms
[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 #include <lunaix/mm/mm.h>
9
10 struct proc_mm;
11 struct proc_info;
12
13 struct remote_vmctx
14 {
15     ptr_t vms_mnt;
16     ptr_t local_mnt;
17     ptr_t remote;
18     pfn_t page_cnt;
19 };
20
21
22 static inline void
23 mm_index(void** index, struct mm_region* target)
24 {
25     *index = (void*)target;
26     target->index = index;
27 }
28
29 typedef struct llist_header vm_regions_t;
30
31 struct proc_mm
32 {
33     // virtual memory root (i.e. root page table)
34     ptr_t             vmroot;
35     ptr_t             vm_mnt;       // current mount point
36     vm_regions_t      regions;
37
38     struct mm_region* heap;
39     struct proc_info* proc;
40     struct proc_mm*   guest_mm;     // vmspace mounted by this vmspace
41 };
42
43 /**
44  * @brief Create a process virtual memory space descriptor
45  * 
46  * @param proc 
47  * @return struct proc_mm* 
48  */
49 struct proc_mm*
50 procvm_create(struct proc_info* proc);
51
52 void
53 procvm_prune_vmr(ptr_t vm_mnt, struct mm_region* region);
54
55 /**
56  * @brief Initialize and mount the vm of `proc` to duplication of current process
57  * 
58  * @param proc 
59  * @return struct proc_mm* 
60  */
61 void
62 procvm_dupvms_mount(struct proc_mm* proc);
63
64 void
65 procvm_unmount_release(struct proc_mm* proc);
66
67 void
68 procvm_mount(struct proc_mm* mm);
69
70 void
71 procvm_unmount(struct proc_mm* mm);
72
73 /**
74  * @brief Initialize and mount the vms of `proc` as a clean slate which contains
75  * nothing but shared global mapping of kernel image.
76  * 
77  * @param proc 
78  */
79 void
80 procvm_initvms_mount(struct proc_mm* mm);
81
82
83 /*
84     Mount and unmount from VMS_SELF.
85     Although every vms is mounted to that spot by default,
86     this just serve the purpose to ensure the scheduled
87     vms does not dangling in some other's vms.
88 */
89
90 void
91 procvm_mount_self(struct proc_mm* mm);
92
93 void
94 procvm_unmount_self(struct proc_mm* mm);
95
96
97 /*
98     remote virtual memory manipulation
99 */
100
101 #define REMOTEVM_MAX_PAGES 128
102
103 ptr_t
104 procvm_enter_remote_transaction(struct remote_vmctx* rvmctx, struct proc_mm* mm,
105                     ptr_t remote_base, size_t size);
106
107 int
108 procvm_copy_remote(struct remote_vmctx* rvmctx, 
109                    ptr_t remote_dest, void* local_src, size_t sz);
110
111 void
112 procvm_exit_remote(struct remote_vmctx* rvmctx);
113
114 /*
115     architecture-specific
116 */
117
118 void
119 procvm_link_kernel(ptr_t dest_mnt);
120
121 void
122 procvm_unlink_kernel();
123
124 #endif /* __LUNAIX_PROCVM_H */