1 | /* |
---|
2 | * vmm.h - virtual memory management related operations |
---|
3 | * |
---|
4 | * Authors Ghassan Almaless (2008,2009,2010,2011, 2012) |
---|
5 | * Mohamed Lamine Karaoui (2015) |
---|
6 | * Alain Greiner (2016,2017) |
---|
7 | * |
---|
8 | * Copyright (c) UPMC Sorbonne Universites |
---|
9 | * |
---|
10 | * This file is part of ALMOS-MKH. |
---|
11 | * |
---|
12 | * ALMOS-MKH is free software; you can redistribute it and/or modify it |
---|
13 | * under the terms of the GNU General Public License as published by |
---|
14 | * the Free Software Foundation; version 2.0 of the License. |
---|
15 | * |
---|
16 | * ALMOS-MKH is distributed in the hope that it will be useful, but |
---|
17 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
19 | * General Public License for more details. |
---|
20 | * |
---|
21 | * You should have received a copy of the GNU General Public License |
---|
22 | * along with ALMOS-MKH; if not, write to the Free Software Foundation, |
---|
23 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
24 | */ |
---|
25 | |
---|
26 | #ifndef _VMM_H_ |
---|
27 | #define _VMM_H_ |
---|
28 | |
---|
29 | #include <hal_types.h> |
---|
30 | #include <bits.h> |
---|
31 | #include <list.h> |
---|
32 | #include <grdxt.h> |
---|
33 | #include <spinlock.h> |
---|
34 | #include <hal_gpt.h> |
---|
35 | #include <vseg.h> |
---|
36 | #include <page.h> |
---|
37 | |
---|
38 | /**** Forward declarations ****/ |
---|
39 | |
---|
40 | struct process_s; |
---|
41 | |
---|
42 | /********************************************************************************************* |
---|
43 | * This structure defines the STACK allocator used by the VMM to dynamically allocate |
---|
44 | * STACK vsegs requested or released by the user process. |
---|
45 | * This allocator handles a fixed size array of fixed size slots stores in the STACK zone. |
---|
46 | * The stack size and the number of slots are defined by the CONFIG_VMM_STACK_SIZE, and |
---|
47 | * CONFIG_THREAD |
---|
48 | * Each slot can contain one user stack vseg. The first page in the slot is not allocated |
---|
49 | * to detect stack overflow. |
---|
50 | * The slot index can be computed form the slot base address, and reversely. |
---|
51 | * All allocation / release operations are registered in the stack_bitmap, that completely |
---|
52 | * define the STACK zone state. |
---|
53 | * In this implementation, the max number of slots is 32. |
---|
54 | ********************************************************************************************/ |
---|
55 | |
---|
56 | typedef struct stack_mgr_s |
---|
57 | { |
---|
58 | spinlock_t lock; /*! lock protecting STACK allocator */ |
---|
59 | vpn_t vpn_base; /*! first page of STACK zone */ |
---|
60 | bitmap_t bitmap; /*! bit bector of allocated stacks */ |
---|
61 | } |
---|
62 | stack_mgr_t; |
---|
63 | |
---|
64 | /********************************************************************************************* |
---|
65 | * This structure defines the MMAP allocator used by the VMM to dynamically allocate |
---|
66 | * MMAP vsegs requested or released by an user process. |
---|
67 | * This allocator should be only used in the reference cluster. |
---|
68 | * - allocation policy : all allocated vsegs occupy an integer number of pages that is |
---|
69 | * power of 2, and are aligned on a page boundary. The requested number of pages is |
---|
70 | * rounded if required. The first_free_vpn variable defines completely the MMAP zone state. |
---|
71 | * It is never decremented, as the released vsegs are simply registered in a zombi_list. |
---|
72 | * The relevant zombi_list is checked first for each allocation request. |
---|
73 | * - release policy : a released MMAP vseg is registered in an array of zombi_lists. |
---|
74 | * This array is indexed by ln(number of pages), and each entry contains the root of |
---|
75 | * a local list of zombi vsegs that have the same size. The physical memory allocated |
---|
76 | * for a zombi vseg descriptor is not released, to use the "list" field. |
---|
77 | * This physical memory allocated for MMAP vseg descriptors is actually released |
---|
78 | * when the VMM is destroyed. |
---|
79 | ********************************************************************************************/ |
---|
80 | |
---|
81 | typedef struct mmap_mgr_s |
---|
82 | { |
---|
83 | spinlock_t lock; /*! lock protecting MMAP allocator */ |
---|
84 | vpn_t vpn_base; /*! first page of MMAP zone */ |
---|
85 | vpn_t vpn_size; /*! number of pages in MMAP zone */ |
---|
86 | vpn_t first_free_vpn; /*! first free page in MMAP zone */ |
---|
87 | list_entry_t zombi_list[32]; /*! array of roots of released vsegs lists */ |
---|
88 | } |
---|
89 | mmap_mgr_t; |
---|
90 | |
---|
91 | /********************************************************************************************* |
---|
92 | * This structure defines the Virtual Memory Manager for a given process in a given cluster. |
---|
93 | * This local VMM provides three main services: |
---|
94 | * 1) It registers all vsegs statically or dynamically defined in the vseg list, |
---|
95 | * and in the associated radix-tree. |
---|
96 | * 2) It allocates virtual memory space for the STACKS and MMAP vsegs, |
---|
97 | * using dedicated allocators. |
---|
98 | * 3) It contains the local copy of the generic page table descriptor. |
---|
99 | ********************************************************************************************/ |
---|
100 | |
---|
101 | typedef struct vmm_s |
---|
102 | { |
---|
103 | rwlock_t vsegs_lock; /*! lock protecting the vsegs list & radix tree */ |
---|
104 | list_entry_t vsegs_root; /*! all vsegs in same process and same cluster */ |
---|
105 | uint32_t vsegs_nr; /*! total number of local vsegs */ |
---|
106 | grdxt_t grdxt; /*! embedded generic vsegs radix tree (key is vpn) */ |
---|
107 | |
---|
108 | gpt_t gpt; /*! embedded generic page table descriptor */ |
---|
109 | |
---|
110 | stack_mgr_t stack_mgr; /*! embedded STACK vsegs allocator */ |
---|
111 | mmap_mgr_t mmap_mgr; /*! embedded MMAP vsegs allocator */ |
---|
112 | |
---|
113 | uint32_t pgfault_nr; /*! page fault counter */ |
---|
114 | uint32_t u_err_nr; /*! TODO ??? [AG] */ |
---|
115 | uint32_t m_err_nr; /*! TODO ??? [AG] */ |
---|
116 | |
---|
117 | vpn_t kent_vpn_base; /*! kentry vseg first page */ |
---|
118 | vpn_t args_vpn_base; /*! args vseg first page */ |
---|
119 | vpn_t envs_vpn_base; /*! envs zone first page */ |
---|
120 | vpn_t heap_vpn_base; /*! envs zone first page */ |
---|
121 | vpn_t code_vpn_base; /*! code zone first page */ |
---|
122 | vpn_t data_vpn_base; /*! data zone first page */ |
---|
123 | |
---|
124 | intptr_t entry_point; /*! main thread entry point */ |
---|
125 | |
---|
126 | vseg_t * heap_vseg; /*! pointer on local heap vseg descriptor */ |
---|
127 | } |
---|
128 | vmm_t; |
---|
129 | |
---|
130 | /********************************************************************************************* |
---|
131 | * This structure is used to store the arguments of the mmap() system call. |
---|
132 | ********************************************************************************************/ |
---|
133 | |
---|
134 | typedef struct mmap_attr_s |
---|
135 | { |
---|
136 | void * addr; /*! requested virtual address (unused : should be NULL) */ |
---|
137 | uint32_t length; /*! requested vseg size (bytes) */ |
---|
138 | uint32_t prot; /*! access modes */ |
---|
139 | uint32_t flags; /*! only MAP_FILE / MAP_ANON / MAP_PRIVATE / MAP_SHARED */ |
---|
140 | fdid_t fdid; /*! file descriptor index (if MAP_FILE is set) */ |
---|
141 | int32_t offset; /*! file offset (if MAP_FILE is set) */ |
---|
142 | } |
---|
143 | mmap_attr_t; |
---|
144 | |
---|
145 | /********************************************************************************************* |
---|
146 | * This function initialises the virtual memory manager attached to a process. |
---|
147 | * - It initializes the VSL (list of vsegs and associated radix tree). |
---|
148 | * - It initializes the generic page table (empty). |
---|
149 | * - It initializes the STAK and MMAP allocators. |
---|
150 | * - It registers the "kentry", "args", "envs" and "heap" vsegs in the vsegs list. |
---|
151 | * Any error in this function gives a kernel panic. |
---|
152 | ********************************************************************************************* |
---|
153 | * @ process : pointer on process descriptor |
---|
154 | ********************************************************************************************/ |
---|
155 | void vmm_init( struct process_s * process ); |
---|
156 | |
---|
157 | /********************************************************************************************* |
---|
158 | * This function copies the content of a source VMM to a destination VMM. |
---|
159 | ********************************************************************************************* |
---|
160 | * @ dst_process : pointer on destination process descriptor. |
---|
161 | * @ src_process : pointer on source process descriptor. |
---|
162 | * @ return 0 if success / return ENOMEM if failure. |
---|
163 | ********************************************************************************************/ |
---|
164 | error_t vmm_copy( struct process_s * dst_process, |
---|
165 | struct process_s * src_process ); |
---|
166 | |
---|
167 | /********************************************************************************************* |
---|
168 | * This function removes all vsegs registered in in a virtual memory manager, |
---|
169 | * and releases the memory allocated to the local generic page table. |
---|
170 | ********************************************************************************************* |
---|
171 | * @ process : pointer on process descriptor. |
---|
172 | ********************************************************************************************/ |
---|
173 | void vmm_destroy( struct process_s * process ); |
---|
174 | |
---|
175 | /********************************************************************************************* |
---|
176 | * This function scans the list of vsegs registered in the VMM of a given process descriptor |
---|
177 | * to check if a given virtual region (defined by a base and size) overlap an existing vseg. |
---|
178 | ********************************************************************************************* |
---|
179 | * @ process : pointer on process descriptor. |
---|
180 | * @ base : region virtual base address. |
---|
181 | * @ size : region size (bytes). |
---|
182 | * @ returns NULL if no conflict / return conflicting vseg pointer if conflict. |
---|
183 | ********************************************************************************************/ |
---|
184 | vseg_t * vmm_check_conflict( struct process_s * process, |
---|
185 | vpn_t base, |
---|
186 | vpn_t size ); |
---|
187 | |
---|
188 | /********************************************************************************************* |
---|
189 | * This function allocates memory for a vseg descriptor, initialises it, and register it |
---|
190 | * in the VMM of the process. It checks the collision with pre-existing vsegs in VMM. |
---|
191 | * For STACK and MMAP types vseg, it does not use the base argument, but uses the VMM STACK |
---|
192 | * and MMAP specific allocators to get a base address in virtual space. |
---|
193 | * To comply with the "on-demand" paging policy, this function does NOT modify the |
---|
194 | * page table, and does not allocate physical memory for vseg data. |
---|
195 | ********************************************************************************************* |
---|
196 | * @ vmm : pointer on process descriptor. |
---|
197 | * @ base : vseg base address |
---|
198 | * @ size : vseg size (bytes) |
---|
199 | * @ type : vseg type |
---|
200 | * @ returns pointer on vseg if success / returns NULL if no memory or conflict. |
---|
201 | ********************************************************************************************/ |
---|
202 | vseg_t * vmm_create_vseg( struct process_s * process, |
---|
203 | intptr_t base, |
---|
204 | intptr_t size, |
---|
205 | uint32_t type ); |
---|
206 | |
---|
207 | /********************************************************************************************* |
---|
208 | * This function removes a vseg identified by it's pointer from the VMM of the calling process. |
---|
209 | * - If the vseg has not the STACK or MMAP type, it is removed from the vsegs list, |
---|
210 | * and the physical memory allocated to vseg descriptor is released to KMEM. |
---|
211 | * - If the vseg has the STACK type, it is removed from the vsegs list, the physical memory |
---|
212 | * allocated to vseg descriptor is released to KMEM, and the stack slot is returned to the |
---|
213 | * VMM STACK allocator. |
---|
214 | * - If the vseg has the MMAP type, it is removed from the vsegs list and is registered |
---|
215 | * in the zombi_list of the VMM MMAP allocator for future reuse. The physical memory |
---|
216 | * allocated to vseg descriptor is NOT released to KMEM. |
---|
217 | ********************************************************************************************* |
---|
218 | * @ vseg : pointer on vseg to be removed. |
---|
219 | ********************************************************************************************/ |
---|
220 | void vmm_remove_vseg( vseg_t * vseg ); |
---|
221 | |
---|
222 | /********************************************************************************************* |
---|
223 | * This function allocates physical memory from the local cluster to map all PTEs |
---|
224 | * of a "kernel" vseg (type KCODE , KDATA, or KDEV) in the page table of process_zero. |
---|
225 | * WARNING : It should not be used for "user" vsegs, that must be mapped using the |
---|
226 | * "on-demand-paging" policy. |
---|
227 | ********************************************************************************************* |
---|
228 | * @ vseg : pointer on the vseg to be mapped. |
---|
229 | * @ attr : GPT attributes to be set for all vseg pages. |
---|
230 | * @ returns 0 if success / returns ENOMEM if no memory |
---|
231 | ********************************************************************************************/ |
---|
232 | error_t vmm_map_kernel_vseg( vseg_t * vseg, |
---|
233 | uint32_t attr ); |
---|
234 | |
---|
235 | /********************************************************************************************* |
---|
236 | * This function unmaps all PTEs of a given vseg, in the generic page table associated |
---|
237 | * to a given process descriptor, and releases the corresponding physical memory. |
---|
238 | * It can be used for any type of vseg. |
---|
239 | ********************************************************************************************* |
---|
240 | * @ process : pointer on process descriptor. |
---|
241 | * @ vseg : pointer on the vseg to be unmapped. |
---|
242 | ********************************************************************************************/ |
---|
243 | void vmm_unmap_vseg( struct process_s * process, |
---|
244 | vseg_t * vseg ); |
---|
245 | |
---|
246 | /********************************************************************************************* |
---|
247 | * This function removes a given region (defined by a base address and a size) from |
---|
248 | * the VMM of a given process descriptor. This can modify several vsegs: |
---|
249 | * (a) if the region is not entirely mapped in an existing vseg, it's an error. |
---|
250 | * (b) if the region has same base and size as an existing vseg, the vseg is removed. |
---|
251 | * (c) if the removed region cut the vseg in two parts, it is removed and re-created. |
---|
252 | * (d) if the removed region cut the vseg in three parts, it is removed, and two are created. |
---|
253 | * TODO : cases (c) and (d) are not implemented [AG] |
---|
254 | ********************************************************************************************* |
---|
255 | * @ process : pointer on process descriptor |
---|
256 | * @ base : vseg base address |
---|
257 | * @ size : vseg size (bytes) |
---|
258 | ********************************************************************************************/ |
---|
259 | error_t vmm_resize_vseg( struct process_s * process, |
---|
260 | intptr_t base, |
---|
261 | intptr_t size ); |
---|
262 | |
---|
263 | /********************************************************************************************* |
---|
264 | * This function checks that a given virtual address is contained in a registered vseg. |
---|
265 | * - if the vseg is registered in local process VMM, it returns the local vseg pointer. |
---|
266 | * - if the vseg is missing in local VMM, it uses a RPC to get it from the reference cluster, |
---|
267 | * register it in local VMM and returns the local vseg pointer, if success. |
---|
268 | * - if the vseg is missing in reference VMM, it returns an user error. |
---|
269 | * It creates a kernel panic if there is not enough memory to create a new vseg descriptor |
---|
270 | * in the cluster containing the calling thread. |
---|
271 | ********************************************************************************************* |
---|
272 | * @ process : [in] pointer on process descriptor |
---|
273 | * @ vaddr : [in] virtual address |
---|
274 | * @ vseg : [out] pointer on found vseg |
---|
275 | * @ returns 0 if success / returns -1 if user error. |
---|
276 | *********************************************************************************************/ |
---|
277 | error_t vmm_get_vseg( struct process_s * process, |
---|
278 | intptr_t vaddr, |
---|
279 | vseg_t ** vseg ); |
---|
280 | |
---|
281 | /********************************************************************************************* |
---|
282 | * This function is called by the generic exception handler when a page fault |
---|
283 | * has been detected in a given cluster. |
---|
284 | * If the local cluster is not the reference cluster, it send a RPC_VMM_GET_PTE |
---|
285 | * to the reference cluster to get the missing PTE attributes and PPN, and update |
---|
286 | * the local page table. If the local cluster is the reference, it call directly |
---|
287 | * the vmm_get_pte() function. |
---|
288 | ********************************************************************************************* |
---|
289 | * @ process : pointer on process descriptor. |
---|
290 | * @ vseg : pointer on involved vseg. |
---|
291 | * @ vpn : VPN of the missing PTE. |
---|
292 | * @ returns 0 if success / returns ENOMEM if no memory. |
---|
293 | ********************************************************************************************/ |
---|
294 | error_t vmm_handle_page_fault( struct process_s * process, |
---|
295 | vseg_t * vseg, |
---|
296 | vpn_t vpn ); |
---|
297 | |
---|
298 | /********************************************************************************************* |
---|
299 | * This function returns in the "attr" and "ppn" arguments the PTE associated to a given |
---|
300 | * VPN for a given process. This function must be called by a thread running in the |
---|
301 | * reference cluster. To get the PTE from another cluster, use the RPC_VMM_GET_PTE. |
---|
302 | * The vseg containing the searched VPN should be registered in the reference VMM. |
---|
303 | * If the PTE in the reference page table is unmapped, this function allocates the missing |
---|
304 | * physical page from the target cluster defined by the vseg type, initialize it, |
---|
305 | * and update the reference page table. It calls the RPC_PMEM_GET_PAGES to get and |
---|
306 | * initialize the missing physical page, if the target cluster is not the reference cluster. |
---|
307 | ********************************************************************************************* |
---|
308 | * @ process : [in] pointer on process descriptor. |
---|
309 | * @ vpn : [in] VPN defining the missing PTE. |
---|
310 | * @ attr : [out] PTE attributes. |
---|
311 | * @ ppn : [out] PTE ppn. |
---|
312 | * @ returns 0 if success / returns ENOMEM if error. |
---|
313 | ********************************************************************************************/ |
---|
314 | error_t vmm_get_pte( struct process_s * process, |
---|
315 | vpn_t vpn, |
---|
316 | uint32_t * attr, |
---|
317 | ppn_t * ppn ); |
---|
318 | |
---|
319 | /********************************************************************************************* |
---|
320 | * This function is called by the vmm_get_pte() function. |
---|
321 | * Depending on the vseg type, defined by the <vseg> argument, it returns the PPN |
---|
322 | * (Physical Page Number) associated to a missing page defined by the <vpn> argument. |
---|
323 | * - For the VSEG_TYPE_FILE, it returns the physical page from the file mapper. |
---|
324 | * For all other types, it allocates a new physical page from the cluster defined |
---|
325 | * by the <vseg->cxy> field, or by the <vpn> MSB bits for a distributed vseg. |
---|
326 | * - For the VSEG_TYPE_CODE and VSEG_TYPE_DATA types, the allocated page is initialized |
---|
327 | * from the .elf file mapper. For others vseg types it is not initialised. |
---|
328 | ********************************************************************************************* |
---|
329 | * @ vseg : local pointer on vseg containing the mising page. |
---|
330 | * @ vpn : Virtual Page Number identifying the missing page. |
---|
331 | * @ ppn : [out] returned Physical Page Number. |
---|
332 | ********************************************************************************************/ |
---|
333 | error_t vmm_get_one_ppn( vseg_t * vseg, |
---|
334 | vpn_t vpn, |
---|
335 | ppn_t * ppn ); |
---|
336 | |
---|
337 | /********************************************************************************************* |
---|
338 | * This function makes the virtual to physical address translation, using the calling |
---|
339 | * process page table. It uses identity mapping if required by the <ident> argument. |
---|
340 | * This address translation is required to configure the peripherals having a DMA |
---|
341 | * capability, or to implement the software L2/L3 cache cohérence, using the MMC device |
---|
342 | * synchronisation primitives. |
---|
343 | * WARNING : the <ident> value must be defined by the CONFIG_KERNEL_IDENTITY_MAP parameter. |
---|
344 | ********************************************************************************************* |
---|
345 | * @ ident : [in] uses identity mapping if true. |
---|
346 | * @ ptr : [in] virtual address. |
---|
347 | * @ paddr : [out] pointer on buffer for physical address. |
---|
348 | * @ returns 0 if success / returns ENOMEM if error. |
---|
349 | ********************************************************************************************/ |
---|
350 | error_t vmm_v2p_translate( bool_t ident, |
---|
351 | void * ptr, |
---|
352 | paddr_t * paddr ); |
---|
353 | |
---|
354 | |
---|
355 | |
---|
356 | #endif /* _VMM_H_ */ |
---|