source: trunk/kernel/mm/vmm.h @ 640

Last change on this file since 640 was 640, checked in by alain, 4 years ago

Remove all RPCs in page-fault handling.

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