source: trunk/kernel/mm/vmm.h

Last change on this file was 672, checked in by alain, 6 years ago

1) Introduce up to 4 command lines arguments in the KSH "load" command.
These arguments are transfered to the user process through the
argc/argv mechanism, using the user space "args" vseg.

2) Introduce the named and anonymous "pipes", for inter-process communication
through the pipe() and mkfifo() syscalls.

3) Introduce the "chat" application to validate the two above mechanisms.

4) Improve printk() and assert() fonctions in printk.c.

File size: 28.8 KB
Line 
1/*
2 * vmm.h - virtual memory management related operations
3 *
4 * Authors Ghassan Almaless (2008,2009,2010,2011, 2012)
5 * Alain Greiner (2016,2017,2018,2019,2020))
6 *
7 * Copyright (c) UPMC Sorbonne Universites
8 *
9 * This file is part of ALMOS-MKH.
10 *
11 * ALMOS-MKH is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; version 2.0 of the License.
14 *
15 * ALMOS-MKH is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with ALMOS-MKH; if not, write to the Free Software Foundation,
22 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 */
24
25#ifndef _VMM_H_
26#define _VMM_H_
27
28#include <hal_kernel_types.h>
29#include <bits.h>
30#include <list.h>
31#include <queuelock.h>
32#include <remote_queuelock.h>
33#include <hal_gpt.h>
34#include <vseg.h>
35#include <page.h>
36
37/**** Forward declarations ****/
38
39struct process_s;
40struct vseg_s;
41
42/*********************************************************************************************
43 * This structure defines the STACK allocator used by the VMM to dynamically handle
44 * vseg allocation or release requests for an user thread.
45 * This allocator handles a fixed size array of fixed size slots in STACK zone of user space.
46 * The stack size and the number of slots are defined by the CONFIG_VMM_STACK_SIZE, and
47 * CONFIG_VMM_STACK_BASE parameters.
48 * Each slot can contain one user stack vseg. The first 4 Kbytes page in the slot is not
49 * mapped to detect stack overflow.
50 * In this implementation, the slot index is defined by the user thead LTID.
51 * All allocated stacks are registered in a bitmap defining the STACK zone state:
52 * - The allocator checks that the requested slot has not been already allocated, and set the
53 * corresponding bit in the bitmap.
54 * - The de-allocator reset the corresponding bit in the bitmap.
55 *
56 * An architecture dependant hal_vmm_display() function is defined in <hal_vmm.h> file.
57 ********************************************************************************************/
58
59typedef struct stack_mgr_s
60{
61 busylock_t lock; /*! lock protecting STACK allocator */
62 vpn_t vpn_base; /*! first page of STACK zone */
63 bitmap_t bitmap; /*! bit vector of allocated stacks */
64}
65stack_mgr_t;
66
67/*********************************************************************************************
68 * This structure defines the MMAP allocator used by the VMM to dynamically handle MMAP vsegs
69 * requested or released by an user process. It must be called in the reference cluster.
70 * - allocation policy :
71 * This allocator implements the buddy algorithm. All allocated vsegs occupy an integer
72 * number of pages, that is power of 2, and are aligned (vpn_base is multiple of vpn_size).
73 * The requested number of pages is rounded if required. The global allocator state is
74 * completely defined by the free_pages_root[] array indexed by the vseg order.
75 * These free lists are local, but are implemented as xlist because we use the existing
76 * vseg.xlist to register a free vseg in its free list.
77 * - release policy :
78 * A released vseg is recursively merged with the "buddy" vseg when it is free, in
79 * order to build the largest possible aligned free vsegs. The resulting vseg.vpn_size
80 * field is updated.
81 * Implementation note:
82 * The only significant (and documented) fiels in the vsegs registered in the MMAP allocator
83 * free lists are "xlist", "vpn_base", and "vpn_size".
84 ********************************************************************************************/
85
86typedef struct mmap_mgr_s
87{
88 busylock_t lock; /*! lock protecting MMAP allocator */
89 vpn_t vpn_base; /*! first page of MMAP zone */
90 vpn_t vpn_size; /*! number of pages in MMAP zone */
91 xlist_entry_t free_list_root[CONFIG_VMM_HEAP_MAX_ORDER + 1]; /* roots of free lists */
92}
93mmap_mgr_t;
94
95/*********************************************************************************************
96 * This structure defines the Virtual Memory Manager for a given process in a given cluster.
97 * This VMM implements four main services:
98 * 1) It contains the local copy of the vseg list (VSL), only complete in reference cluster.
99 * 2) It contains the local copy of the generic page table (GPT), only complete in reference.
100 * 3) The stack manager dynamically allocates virtual memory space for the STACK vsegs.
101 * 4) The mmap manager dynamically allocates virtual memory for the (FILE/ANON/REMOTE) vsegs.
102 ******************************************************a**************************************
103 * Implementation notes:
104 * 1. In most clusters, the VSL and GPT are only partial copies of the reference VSL and GPT
105 * structures, stored in the reference cluster.
106 * 2. The VSL contains only local vsegs, but it is implemented as an xlist, and protected by
107 * a remote_rwlock, because it can be accessed by a thread running in a remote cluster.
108 * An example is the vmm_fork_copy() function.
109 * 3. The GPT in the reference cluster can be directly accessed by remote threads to handle
110 * false page-fault (page is mapped in the reference GPT, but the PTE copy is missing
111 * in the local GPT). As each PTE can be protected by a specific GPT_LOCKED attribute
112 * for exclusive access, it is NOT protected by a global lock.
113 ********************************************************************************************/
114
115typedef struct vmm_s
116{
117 remote_queuelock_t vsl_lock; /*! lock protecting the local VSL */
118 xlist_entry_t vsegs_root; /*! Virtual Segment List root */
119 uint32_t vsegs_nr; /*! total number of local vsegs */
120
121 gpt_t gpt; /*! Generic Page Table descriptor */
122
123 stack_mgr_t stack_mgr; /*! embedded STACK vsegs allocator */
124
125 mmap_mgr_t mmap_mgr; /*! embedded MMAP vsegs allocator */
126
127 uint32_t false_pgfault_nr; /*! false page fault counter (for all threads) */
128 uint32_t local_pgfault_nr; /*! false page fault counter (for all threads) */
129 uint32_t global_pgfault_nr; /*! false page fault counter (for all threads) */
130 uint32_t false_pgfault_cost; /*! cumulated cost (for all threads) */
131 uint32_t local_pgfault_cost; /*! cumulated cost (for all threads) */
132 uint32_t global_pgfault_cost; /*! cumulated cost (for all threads) */
133
134 vpn_t args_vpn_base; /*! args vseg first page */
135 vpn_t envs_vpn_base; /*! envs vseg first page */
136 vpn_t code_vpn_base; /*! code vseg first page */
137 vpn_t data_vpn_base; /*! data vseg first page */
138 vpn_t heap_vpn_base; /*! heap zone first page */
139
140 intptr_t entry_point; /*! main thread entry point */
141}
142vmm_t;
143
144/*********************************************************************************************
145 * This function makes a partial initialisation of the VMM attached to an user process.
146 * It intializes the STACK and MMAP allocators, the VSL lock, and the instrumentation
147 * counters, but it does not register any vseg in the VSL:
148 * - The "kernel" vsegs are registered, by the hal_vmm_kernel_update() function.
149 * - The "args" & "envs" vsegs are registered by the process_make_exec() function.
150 * - The "code" and "data" vsegs are registered by the elf_load_process() function.
151 * - The "stack" vsegs are registered by process_make_exec() / thread_user_create().
152 * - The "file", "anon", "remote" vsegs are dynamically registered by the mmap() syscall.
153 *********************************************************************************************
154 * @ process : pointer on process descriptor
155 * @ return 0 if success / return -1 if failure.
156 ********************************************************************************************/
157error_t vmm_user_init( struct process_s * process );
158
159/*********************************************************************************************
160 * This function is called by the process_make_exec() function to re-initialises the VMM
161 * attached to an user process:
162 * - It removes from the VMM of the process identified by the <process> argument all user
163 * vsegs, by calling the vmm_remove_vseg() function: the vsegs are removed from the VSL,
164 * the corresponding GPT entries are removed from the GPT, and the physical pages are
165 * released to the relevant kmem when they are not shared.
166 * - The VSL and the GPT are not modified for the kernel vsegs.
167 * - Finally, it calls the vmm_user_init() function to re-initialize the STAK and MMAP
168 * allocators, and the lock protecting GPT.
169 *********************************************************************************************
170 * @ process : pointer on process descriptor.
171 ********************************************************************************************/
172void vmm_user_reset( struct process_s * process );
173
174/*********************************************************************************************
175 * This function is called by the process_make_fork() function. It partially copies
176 * the content of a remote parent process VMM to the local child process VMM:
177 * - The KERNEL vsegs required by the architecture must have been previously
178 * created in the child VMM, using the hal_vmm_kernel_update() function.
179 * - The DATA, ANON, REMOTE vsegs registered in the parent VSL are registered in the
180 * child VSL. All valid PTEs in parent GPT are copied to the child GPT.
181 * The WRITABLE and COW flags are not modified, as it will be done later for those
182 * shared pages by the vmm_set_cow() function.
183 * - The CODE vsegs registered in the parent VSL are registered in the child VSL, but the
184 * GPT entries are not copied in the child GPT, and will be dynamically updated from
185 * the .elf file when a page fault is reported.
186 * - The FILE vsegs registered in the parent VSL are registered in the child VSL, and all
187 * valid GPT entries in parent GPT are copied to the child GPT. The COW flag is not set.
188 * - No STACK vseg is copied from parent VMM to child VMM: the child stack vseg is copied
189 * later from the cluster containing the user thread requesting the fork().
190 *********************************************************************************************
191 * @ child_process : local pointer on local child process descriptor.
192 * @ parent_process_xp : extended pointer on remote parent process descriptor.
193 * @ return 0 if success / return -1 if failure.
194 ********************************************************************************************/
195error_t vmm_fork_copy( struct process_s * child_process,
196 xptr_t parent_process_xp );
197
198/*********************************************************************************************
199 * This function is called by the process_make_fork() function to update the COW attribute
200 * in the parent parent process vsegs. It set the COW flag, and reset the WRITABLE flag of
201 * all GPT entries of the DATA, MMAP, and REMOTE vsegs of the <process> argument.
202 * It must be called by a thread running in the reference cluster, that contains the complete
203 * VSL and GPT (use the rpc_vmm_set_cow_client() when the calling thread client is remote).
204 * It updates all copies of the process in all clusters, to maintain coherence in GPT copies,
205 * using the list of copies stored in the owner process, and using remote_write accesses to
206 * update the remote GPTs. It atomically increment the pending_fork counter, in all involved
207 * physical page descriptors. It cannot fail, as only mapped entries in GPTs are updated.
208 *********************************************************************************************
209 * @ process : local pointer on local reference process descriptor.
210 ********************************************************************************************/
211void vmm_set_cow( struct process_s * process );
212
213/*********************************************************************************************
214 * This function modifies the vseg identified by <process> and <base> arguments in all
215 * clusters containing a VSL copy, as defined by <new_base> and <new_size> arguments.
216 * The new vseg, defined by the <new_base> and <new_size> arguments must be included
217 * in the existing vseg. The target VSL size and base fields are modified in the VSL.
218 * This is done in all clusters containing a VMM copy to maintain VMM coherence.
219 * It is called by the sys_munmap() and dev_fbf_resize_window() functions.
220 * It can be called by a thread running in any cluster, as it uses the vmm_resize_vseg() in
221 * the local cluster, and parallel RPC_VMM_RESIZE_VSEG for remote clusters.
222 * It cannot fail, as only vseg registered in VSL copies are updated.
223 *********************************************************************************************
224 * @ process : local pointer on process descriptor.
225 * @ base : current vseg base address in user space.
226 * @ new_base : new vseg base.
227 * @ new_size : new vseg size.
228 ********************************************************************************************/
229void vmm_global_resize_vseg( struct process_s * process,
230 intptr_t base,
231 intptr_t new_base,
232 intptr_t new_size );
233
234/*********************************************************************************************
235 * This function removes the vseg identified by the <process> and <base> arguments from
236 * the VSL and remove all associated PTE entries from the GPT.
237 * This is done in all clusters containing a VMM copy to maintain VMM coherence.
238 * It is called by the sys_munmap() and dev_fbf_resize_window() functions.
239 * It can be called by a thread running in any cluster, as it uses the vmm_remove_vseg() in
240 * the local cluster, and parallel RPC_VMM_REMOVE_VSEG for remote clusters.
241 * It cannot fail, as only vseg registered in VSL copies are deleted.
242 *********************************************************************************************
243 * @ pid : local pointer on process identifier.
244 * @ base : vseg base address in user space.
245 ********************************************************************************************/
246void vmm_global_delete_vseg( struct process_s * process,
247 intptr_t base );
248
249/*********************************************************************************************
250 * This function modifies one GPT entry identified by the <process> and <vpn> arguments
251 * in all clusters containing a process copy. It maintains coherence in GPT copies,
252 * using remote_write accesses.
253 * It cannot fail, as only mapped PTE2 in GPT copies are updated.
254 *********************************************************************************************
255 * @ process : local pointer on local process descriptor.
256 * @ vpn : PTE index.
257 * @ attr : PTE / attributes.
258 * @ ppn : PTE / physical page index.
259 ********************************************************************************************/
260void vmm_global_update_pte( struct process_s * process,
261 vpn_t vpn,
262 uint32_t attr,
263 ppn_t ppn );
264
265/*********************************************************************************************
266 * This function deletes, in the local cluster, all vsegs registered in the VSL
267 * of the process identified by the <process> argument. For each vseg:
268 * - it unmaps all vseg PTEs from the GPT (release the physical pages when required).
269 * - it removes the vseg from the local VSL.
270 * - it releases the memory allocated to the local vseg descriptors.
271 * - it releases the memory allocated to the GPT itself.
272 *********************************************************************************************
273 * @ process : pointer on process descriptor.
274 ********************************************************************************************/
275void vmm_destroy( struct process_s * process );
276
277/*********************************************************************************************
278 * This function scans the list of vsegs registered in the VMM of a given process descriptor
279 * to check if a given virtual region (defined by a base and size) overlap an existing vseg.
280 *********************************************************************************************
281 * @ process : pointer on process descriptor.
282 * @ base : region virtual base address.
283 * @ size : region size (bytes).
284 * @ returns NULL if no conflict / return conflicting vseg pointer if conflict.
285 ********************************************************************************************/
286vseg_t * vmm_check_conflict( struct process_s * process,
287 vpn_t base,
288 vpn_t size );
289
290/*********************************************************************************************
291 * This function allocates memory for a vseg descriptor, initialises it, and register it
292 * in the VSL of the local process descriptor.
293 * - For the FILE, ANON, & REMOTE types, it does not use the <base> argument, but uses
294 * the specific VMM MMAP allocator.
295 * - For the STACK type, it does not use the <base> and <size> arguments, but uses the
296 * the specific VMM STACK allocator.
297 * It checks collision with pre-existing vsegs.
298 * To comply with the "on-demand" paging policy, this function does NOT modify the GPT,
299 * and does not allocate physical memory for vseg data.
300 * It should be called by a local thread (could be a RPC thread if the client thread is not
301 * running in the reference cluster).
302 *********************************************************************************************
303 * @ process : pointer on local processor descriptor.
304 * @ type : vseg type.
305 * @ base : vseg base address (or user thread ltid for an user stack vseg).
306 * @ size : vseg size (bytes).
307 * @ file_offset : offset in file for CODE, DATA, FILE types.
308 * @ file_size : can be smaller than "size" for DATA type.
309 * @ mapper_xp : extended pointer on mapper for CODE, DATA, FILE types.
310 * @ cxy : physical mapping cluster (for non distributed vsegs).
311 * @ returns pointer on vseg if success / returns NULL if no memory, or conflict.
312 ********************************************************************************************/
313vseg_t * vmm_create_vseg( struct process_s * process,
314 vseg_type_t type,
315 intptr_t base,
316 uint32_t size,
317 uint32_t file_offset,
318 uint32_t file_size,
319 xptr_t mapper_xp,
320 cxy_t cxy );
321
322/*********************************************************************************************
323 * This function removes from the VMM of a local process descriptor, identified by
324 * the <process> argument, the vseg identified by the <vseg> argument.
325 * It is called by the vmm_user_reset(), vmm_global_delete_vseg(), vmm_destroy() functions.
326 * It must be called by a local thread, running in the cluster containing the modified VMM.
327 * Use the RPC_VMM_REMOVE_VSEG if required.
328 * It makes a kernel panic if the process is not registered in the local cluster.
329 * For all vseg types, the vseg is detached from local VSL, and all associated PTEs are
330 * unmapped from local GPT. Other actions depend on the vseg type:
331 * Regarding the vseg descriptor release:
332 * . for ANON and REMOTE, the vseg is not released, but registered in local zombi_list.
333 * . for STACK the vseg is released to the local stack allocator.
334 * . for all other types, the vseg descriptor is released to the local kmem.
335 * Regarding the physical pages release:
336 * . for KERNEL and FILE, the pages are not released to kmem.
337 * . for CODE and STACK, the pages are released to local kmem.
338 * . for DATA, ANON and REMOTE, the pages are released to relevant kmem only when
339 * the local cluster is the reference cluster.
340 * The VSL lock protecting the VSL must be taken by the caller.
341 *********************************************************************************************
342 * @ process : local pointer on process descriptor.
343 * @ vseg : local pointer on target vseg.
344 ********************************************************************************************/
345void vmm_remove_vseg( struct process_s * process,
346 struct vseg_s * vseg );
347
348/*********************************************************************************************
349 * This function resize a local vseg identified by the <process> and <vseg> arguments.
350 * Both the "size" and "base" fields are modified in the process VSL. When the new vseg
351 * contains less pages than the target vseg, the relevant pages are removed from the GPT.
352 * It is called by the vmm_global_resize() and dev_fbf_resize_window() functions.
353 * It must be called by a local thread, running in the cluster containing the modified VSL.
354 * Use the RPC_VMM_RESIZE_VSEG if required.
355 * The VSL lock protecting the VSL must be taken by the caller.
356 *********************************************************************************************
357 * @ process : local pointer on process descriptor
358 * @ vseg : local pointer on target vseg
359 * @ new_base : vseg base address
360 * @ new_size : vseg size (bytes)
361 ********************************************************************************************/
362void vmm_resize_vseg( struct process_s * process,
363 struct vseg_s * vseg,
364 intptr_t new_base,
365 intptr_t new_size );
366
367/*********************************************************************************************
368 * This function checks that a given virtual address <vaddr> in a given <process> is
369 * contained in a registered vseg. It can be called by any thread running in any cluster.
370 * - if the vseg is registered in the local process VSL, it returns the local vseg pointer.
371 * - if the vseg is missing in local VSL, it access directly the reference VSL.
372 * - if the vseg is found in reference VSL, it updates the local VSL and returns this pointer.
373 * It returns an error when the vseg is missing in the reference VMM, or when there is
374 * not enough memory for a new vseg descriptor in the calling thread cluster.
375 * For both the local and the reference VSL, it takes the VSL lock before scanning the VSL.
376 *********************************************************************************************
377 * @ process : [in] pointer on process descriptor.
378 * @ vaddr : [in] virtual address.
379 * @ vseg : [out] local pointer on local vseg.
380 * @ returns 0 if success / returns -1 if user error
381 ********************************************************************************************/
382error_t vmm_get_vseg( struct process_s * process,
383 intptr_t vaddr,
384 vseg_t ** vseg );
385
386/*********************************************************************************************
387 * This function is called by the generic exception handler in case of page-fault event,
388 * detected for a given <vpn>. The <process> argument is used to access the relevant VMM.
389 * It checks the missing VPN and returns an user error if it is not in a registered vseg.
390 * For a legal VPN, there is actually 3 cases:
391 * 1) if the missing VPN belongs to a private vseg (STACK or CODE segment types, non
392 * replicated in all clusters), it allocates a new physical page, computes the attributes,
393 * depending on vseg type, and updates directly the local GPT.
394 * 2) if the missing VPN belongs to a public vseg, it can be a false page-fault, when the VPN
395 * is mapped in the reference GPT, but not in the local GPT. For this false page-fault,
396 * the local GPT is simply updated from the reference GPT.
397 * 3) if the missing VPN is public, and unmapped in the ref GPT, it is a true page fault.
398 * The calling thread allocates a new physical page, computes the attributes, depending
399 * on vseg type, and updates directly (without RPC) the local GPT and the reference GPT.
400 * Other GPT copies will updated on demand.
401 * Concurrent accesses to the GPT(s) are handled, by locking the target PTE before accessing
402 * the local and/or reference GPT(s).
403 *********************************************************************************************
404 * @ process : local pointer on local process.
405 * @ vpn : VPN of the missing PTE.
406 * @ returns EXCP_NON_FATAL / EXCP_USER_ERROR / EXCP_KERNEL_PANIC after analysis
407 ********************************************************************************************/
408error_t vmm_handle_page_fault( struct process_s * process,
409 vpn_t vpn );
410
411/*********************************************************************************************
412 * This function is called by the generic exception handler in case of WRITE violation event,
413 * detected for a given <vpn>. The <process> argument is used to access the relevant VMM.
414 * It returns a kernel panic if the faulty VPN is not in a registered vseg, or is not mapped.
415 * For a legal mapped vseg there is two cases:
416 * 1) If the missing VPN belongs to a private vseg (STACK), it access only the local GPT.
417 * It access the forks counter in the current physical page descriptor.
418 * If there is a pending fork, it allocates a new physical page from the cluster defined
419 * by the vseg type, copies the old physical page content to the new physical page,
420 * and decrements the pending_fork counter in old physical page descriptor.
421 * Finally, it reset the COW flag and set the WRITE flag in local GPT.
422 * 2) If the missing VPN is public, it access only the reference GPT.
423 * It access the forks counter in the current physical page descriptor.
424 * If there is a pending fork, it allocates a new physical page from the cluster defined
425 * by the vseg type, copies the old physical page content to the new physical page,
426 * and decrements the pending_fork counter in old physical page descriptor.
427 * Finally it calls the vmm_global_update_pte() function to reset the COW flag and set
428 * the WRITE flag in all the GPT copies, using a RPC if the reference cluster is remote.
429 * In both cases, concurrent accesses to the GPT are handled by locking the target PTE
430 * before accessing the GPT.
431 *********************************************************************************************
432 * @ process : pointer on local process descriptor copy.
433 * @ vpn : VPN of the faulting PTE.
434 * @ returns EXCP_NON_FATAL / EXCP_USER_ERROR / EXCP_KERNEL_PANIC after analysis
435 ********************************************************************************************/
436error_t vmm_handle_cow( struct process_s * process,
437 vpn_t vpn );
438
439/*********************************************************************************************
440 * This function is called by the vmm_get_pte() function when a page is unmapped.
441 * Depending on the vseg type, defined by the <vseg> argument, it returns the PPN
442 * (Physical Page Number) associated to a missing page defined by the <vpn> argument.
443 * - For the FILE type, it returns directly the physical page from the file mapper.
444 * - For the CODE and DATA types, it allocates a new physical page from the cluster defined
445 * by the <vseg->cxy> field, or by the <vpn> MSB bits for a distributed vseg,
446 * and initialize this page from the .elf file mapper.
447 * - For all other types, it allocates a new physical page from the cluster defined
448 * by the <vseg->cxy> field, or by the <vpn> MSB bits for a distributed vseg,
449 * but the new page is not initialized.
450 *********************************************************************************************
451 * @ vseg : local pointer on vseg containing the mising page.
452 * @ vpn : Virtual Page Number identifying the missing page.
453 * @ ppn : [out] returned Physical Page Number.
454 * return 0 if success / return EINVAL or ENOMEM if error.
455 ********************************************************************************************/
456error_t vmm_get_one_ppn( vseg_t * vseg,
457 vpn_t vpn,
458 ppn_t * ppn );
459
460#endif /* _VMM_H_ */
Note: See TracBrowser for help on using the repository browser.