Merge branch 'master' into isa/arm64
[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 /**
53  * @brief Initialize and mount the vm of `proc` to duplication of current process
54  * 
55  * @param proc 
56  * @return struct proc_mm* 
57  */
58 void
59 procvm_dupvms_mount(struct proc_mm* proc);
60
61 void
62 procvm_unmount_release(struct proc_mm* proc);
63
64 void
65 procvm_mount(struct proc_mm* mm);
66
67 void
68 procvm_unmount(struct proc_mm* mm);
69
70 /**
71  * @brief Initialize and mount the vms of `proc` as a clean slate which contains
72  * nothing but shared global mapping of kernel image.
73  * 
74  * @param proc 
75  */
76 void
77 procvm_initvms_mount(struct proc_mm* mm);
78
79
80 /*
81     Mount and unmount from VMS_SELF.
82     Although every vms is mounted to that spot by default,
83     this just serve the purpose to ensure the scheduled
84     vms does not dangling in some other's vms.
85 */
86
87 void
88 procvm_mount_self(struct proc_mm* mm);
89
90 void
91 procvm_unmount_self(struct proc_mm* mm);
92
93
94 /*
95     remote virtual memory manipulation
96 */
97
98 #define REMOTEVM_MAX_PAGES 128
99
100 ptr_t
101 procvm_enter_remote_transaction(struct remote_vmctx* rvmctx, struct proc_mm* mm,
102                     ptr_t remote_base, size_t size);
103
104 int
105 procvm_copy_remote(struct remote_vmctx* rvmctx, 
106                    ptr_t remote_dest, void* local_src, size_t sz);
107
108 void
109 procvm_exit_remote(struct remote_vmctx* rvmctx);
110
111 /*
112     architecture-specific
113 */
114
115 void
116 procvm_link_kernel(ptr_t dest_mnt);
117
118 void
119 procvm_unlink_kernel();
120
121 #endif /* __LUNAIX_PROCVM_H */