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

Last change on this file since 593 was 585, checked in by alain, 6 years ago

Introduce two separate vmm_handle_page_fault() & vmm_handle_cow() functions in vmm.c

File size: 26.9 KB
RevLine 
[1]1/*
2 * vmm.h - virtual memory management related operations
3 *
4 * Authors   Ghassan Almaless (2008,2009,2010,2011, 2012)
5 *           Mohamed Lamine Karaoui (2015)
[437]6 *           Alain Greiner (2016,2017,2018)
[18]7 *
[1]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
[457]29#include <hal_kernel_types.h>
[1]30#include <bits.h>
31#include <list.h>
[567]32#include <queuelock.h>
[1]33#include <hal_gpt.h>
34#include <vseg.h>
35#include <page.h>
36
37/****  Forward declarations  ****/
38
39struct process_s;
40
41/*********************************************************************************************
[407]42 * This structure defines the STACK allocator used by the VMM to dynamically handle
43 * a STACK vseg requested or released by an user process.
44 * This allocator handles a fixed size array of fixed size slots in the STACK zone.
[1]45 * The stack size and the number of slots are defined by the CONFIG_VMM_STACK_SIZE, and
[407]46 * CONFIG_VMM_STACK_BASE parameters.
[1]47 * Each slot can contain one user stack vseg. The first page in the slot is not allocated
48 * to detect stack overflow.
49 * The slot index can be computed form the slot base address, and reversely.
50 * All allocation / release operations are registered in the stack_bitmap, that completely
[18]51 * define the STACK zone state.
[1]52 ********************************************************************************************/
53
54typedef struct stack_mgr_s
55{
[567]56    busylock_t     lock;               /*! lock protecting STACK allocator                  */
[1]57    vpn_t          vpn_base;           /*! first page of STACK zone                         */
58    bitmap_t       bitmap;             /*! bit bector of allocated stacks                   */
59}
60stack_mgr_t;
61
62/*********************************************************************************************
[407]63 * This structure defines the MMAP allocator used by the VMM to dynamically handle 
[1]64 * MMAP vsegs requested or released by an user process.
[18]65 * This allocator should be only used in the reference cluster.
66 * - allocation policy : all allocated vsegs occupy an integer number of pages that is
[1]67 *   power of 2, and are aligned on a page boundary. The requested number of pages is
[18]68 *   rounded if required. The first_free_vpn variable defines completely the MMAP zone state.
[1]69 *   It is never decremented, as the released vsegs are simply registered in a zombi_list.
[18]70 *   The relevant zombi_list is checked first for each allocation request.
[1]71 * - release policy : a released MMAP vseg is registered in an array of zombi_lists.
72 *   This array is indexed by ln(number of pages), and each entry contains the root of
73 *   a local list of zombi vsegs that have the same size. The physical memory allocated
74 *   for a zombi vseg descriptor is not released, to use the "list" field.
75 *   This physical memory allocated for MMAP vseg descriptors is actually released
76 *   when the VMM is destroyed.
77 ********************************************************************************************/
78
79typedef struct mmap_mgr_s
80{
[567]81    busylock_t     lock;               /*! lock protecting MMAP allocator                   */
[1]82    vpn_t          vpn_base;           /*! first page of MMAP zone                          */
83    vpn_t          vpn_size;           /*! number of pages in MMAP zone                     */
84    vpn_t          first_free_vpn;     /*! first free page in MMAP zone                     */
85    list_entry_t   zombi_list[32];     /*! array of roots of released vsegs lists           */
86}
87mmap_mgr_t;
88
89/*********************************************************************************************
90 * This structure defines the Virtual Memory Manager for a given process in a given cluster.
[585]91 * This local VMM implements four main services:
[567]92 * 1) It contains the local copy of vseg list (VSL), only complete in referrence.
93 * 2) It contains the local copy of the generic page table (GPT), only complete in reference.
[408]94 * 3) The stack manager dynamically allocates virtual memory space for the STACK vsegs.
95 * 4) The mmap manager dynamically allocates virtual memory for the (FILE/ANON/REMOTE) vsegs.
96 ******************************************************a**************************************
97 * Implementation notes:
[585]98 * 1. In most clusters, the VSL and GPT are only partial copies of the reference VSL and GPT
99 *    structures, stored in the reference cluster.
100 * 2. The VSL contains only local vsegs, but it is implemented as an xlist, and protected by
[408]101 *    a remote_rwlock, because it can be accessed by a thread running in a remote cluster.
102 *    An exemple is the vmm_fork_copy() function.
[585]103 * 3. The GPT in the reference cluster can be directly accessed by remote threads to handle
104 *    false page-fault (page is mapped in the reference GPT, but the PTE copy is missing
105 *    in the local GPT). It is also protected by a remote_rwlock.
[1]106 ********************************************************************************************/
107
108typedef struct vmm_s
109{
[567]110        remote_rwlock_t  vsegs_lock;         /*! lock protecting the local VSL                  */
[585]111        xlist_entry_t    vsegs_root;         /*! Virtual Segment List (complete in reference)   */
[408]112        uint32_t         vsegs_nr;           /*! total number of local vsegs                    */
[1]113
[585]114    remote_rwlock_t  gpt_lock;           /*! lock protecting the local GPT                  */
[408]115    gpt_t            gpt;                /*! Generic Page Table (complete in reference)     */
[1]116
[408]117    stack_mgr_t      stack_mgr;          /*! embedded STACK vsegs allocator                 */
118    mmap_mgr_t       mmap_mgr;           /*! embedded MMAP vsegs allocator                  */
[1]119
[408]120        uint32_t         pgfault_nr;         /*! page fault counter (instrumentation)           */
[1]121
[408]122    vpn_t            kent_vpn_base;      /*! kentry vseg first page                         */
123    vpn_t            args_vpn_base;      /*! args vseg first page                           */
124    vpn_t            envs_vpn_base;      /*! envs zone first page                           */
125    vpn_t            heap_vpn_base;      /*! envs zone first page                           */
126        vpn_t            code_vpn_base;      /*! code zone first page                           */
127        vpn_t            data_vpn_base;      /*! data zone first page                           */
[1]128
[408]129        intptr_t         entry_point;        /*! main thread entry point                        */
[1]130}
131vmm_t;
132
133/*********************************************************************************************
[406]134 * This function initialises the virtual memory manager attached to an user process.
[407]135 * - It initializes the STACK and MMAP allocators.
136 * - It registers the "kentry", "args", "envs" vsegs in the VSL.
[409]137 * - It initializes the generic page table, calling the HAL specific hal_gpt_init() function.
138 * - For TSAR it map all pages for the "kentry" vseg, that must be identity mapping.
139 * Note:
[407]140 * - The "code" and "data" vsegs are registered by the elf_load_process() function.
141 * - The "stack" vsegs are dynamically created by the thread_user_create() function.
[409]142 * - The "file", "anon", "remote" vsegs are dynamically created by the mmap() syscall.
[1]143 *********************************************************************************************
144 * @ process   : pointer on process descriptor
[415]145 * @ return 0 if success / return -1 if failure.
[1]146 ********************************************************************************************/
[415]147error_t vmm_init( struct process_s * process );
[1]148
149/*********************************************************************************************
[407]150 * This function displays on TXY0 the list or registered vsegs for a given <process>.
[429]151 * It must be executed by a thread running in reference cluster.
152 * If the <mapping> argument is true, it displays for each vseg all mapped PTEs in GPT.
[23]153 *********************************************************************************************
[407]154 * @ process   : pointer on process descriptor.
155 * @ mapping   : detailed mapping if true.
156 ********************************************************************************************/
157void vmm_display( struct process_s * process,
158                  bool_t             mapping );
159
[567]160/*******************************************************************************************
161 * This function adds a vseg descriptor in the VSL of a given VMM,
162 * and updates the vmm field in the vseg descriptor.
163 * It takes the lock protecting VSL.
164 *******************************************************************************************
165 * @ vmm       : pointer on the VMM
166 * @ vseg      : pointer on the vseg descriptor
167 ******************************************************************************************/
168void vmm_vseg_attach( struct vmm_s  * vmm,
169                      vseg_t        * vseg );
170
171/*******************************************************************************************
172 * This function removes a vseg descriptor from the set of vsegs controlled by a given VMM,
173 * and updates the vmm field in the vseg descriptor. No memory is released.
174 * It takes the lock protecting VSL.
175 *******************************************************************************************
176 * @ vmm       : pointer on the VMM
177 * @ vseg      : pointer on the vseg descriptor
178 ******************************************************************************************/
179void vmm_vseg_detach( struct vmm_s  * vmm,
180                      vseg_t        * vseg );
181
[407]182/*********************************************************************************************
[433]183 * This function is called by the process_make_fork() function. It partially copies
[408]184 * the content of a remote parent process VMM to the local child process VMM:
185 * - all DATA, MMAP, REMOTE vsegs registered in the parent VSL are registered in the child
186 *   VSL, and all valid GPT entries in parent GPT are copied to the child GPT.
187 *   The WRITABLE flag is reset and the COW flag is set in child GPT.
188 * - all CODE vsegs registered in the parent VSL are registered in the child VSL, but the
189 *   GPT entries are not copied in the chilf GPT, that will be dynamically updated from
190 *   the .elf file when a page fault is reported.
191 * - all FILE vsegs registered in the parent VSL are registered in the child VSL, and all
192 *   valid GPT entries in parent GPT are copied to the child GPT. The COW flag is not set.
193 * - no STACK vseg is copied from  parent VMM to child VMM, because the child STACK vseg
[469]194 *   must be copied later from the cluster containing the user thread requesting the fork().
[407]195 *********************************************************************************************
[408]196 * @ child_process     : local pointer on local child process descriptor.
197 * @ parent_process_xp : extended pointer on remote parent process descriptor.
[415]198 * @ return 0 if success / return -1 if failure.
[23]199 ********************************************************************************************/
[408]200error_t vmm_fork_copy( struct process_s * child_process,
201                       xptr_t             parent_process_xp );
[23]202
203/*********************************************************************************************
[433]204 * This function is called by the process_make_fork() function executing the fork syscall.
[408]205 * It set the COW flag, and reset the WRITABLE flag of all GPT entries of the DATA, MMAP,
206 * and REMOTE vsegs of a process identified by the <process> argument.
207 * It must be called by a thread running in the reference cluster, that contains the complete
[433]208 * VSL and GPT (use the rpc_vmm_set_cow_client() when the calling thread client is remote).
[408]209 * It updates all copies of the process in all clusters, to maintain coherence in GPT copies,
210 * using the list of copies stored in the owner process, and using remote_write accesses to
[433]211 * update the remote GPTs. It atomically increment the pending_fork counter, in all involved
212 * physical page descriptors. It cannot fail, as only mapped entries in GPTs are updated.
[1]213 *********************************************************************************************
[408]214 * @ process   : local pointer on local reference process descriptor.
215 ********************************************************************************************/
216void vmm_set_cow( struct process_s * process );
217
218/*********************************************************************************************
[585]219 * This global function modifies a GPT entry identified by the <process> and <vpn>
[433]220 * arguments in all clusters containing a process copy.
221 * It must be called by a thread running in the reference cluster.
[408]222 * It updates all copies of the process in all clusters, to maintain coherence in GPT copies,
223 * using the list of copies stored in the owner process, and using remote_write accesses to
224 * update the remote GPTs. It cannot fail, as only mapped entries in GPT copies are updated.
225 *********************************************************************************************
226 * @ process   : local pointer on local process descriptor.
227 * @ vpn       : PTE index.
228 * @ attr      : PTE / attributes.
229 * @ ppn       : PTE / physical page index.
230 ********************************************************************************************/
[433]231void vmm_global_update_pte( struct process_s * process,
232                            vpn_t              vpn,
233                            uint32_t           attr,
234                            ppn_t              ppn );
[408]235
236/*********************************************************************************************
[433]237 * This function unmaps from the local GPT all mapped PTEs of a vseg identified by the
238 * <process> and <vseg> arguments. It can be used for any type of vseg.
239 * If this function is executed in the reference cluster, it handles for each referenced
240 * physical pages the pending forks counter :
241 * - if counter is non-zero, it decrements it.
242 * - if counter is zero, it releases the physical page to local kmem allocator.
243 *********************************************************************************************
244 * @ process  : pointer on process descriptor.
245 * @ vseg     : pointer on the vseg to be unmapped.
246 ********************************************************************************************/
247void vmm_unmap_vseg( struct process_s * process,
248                     vseg_t           * vseg );
249
250/*********************************************************************************************
251 * This function deletes, in the local cluster, all vsegs registered in the VSL
252 * of the process identified by the <process> argument. For each vseg:
253 * - it unmaps all vseg PTEs from the GPT (release the physical pages when required).
254 * - it removes the vseg from the local VSL.
255 * - it releases the memory allocated to the local vseg descriptors.
[409]256 * Finally, it releases the memory allocated to the GPT itself.
[408]257 *********************************************************************************************
[23]258 * @ process   : pointer on process descriptor.
[1]259 ********************************************************************************************/
260void vmm_destroy( struct process_s * process );
261
262/*********************************************************************************************
[18]263 * This function scans the list of vsegs registered in the VMM of a given process descriptor
[1]264 * to check if a given virtual region (defined by a base and size) overlap an existing vseg.
265 *********************************************************************************************
266 * @ process  : pointer on process descriptor.
267 * @ base     : region virtual base address.
268 * @ size     : region size (bytes).
269 * @ returns NULL if no conflict / return conflicting vseg pointer if conflict.
270 ********************************************************************************************/
271vseg_t * vmm_check_conflict( struct process_s * process,
272                             vpn_t              base,
273                             vpn_t              size );
274
275/*********************************************************************************************
[18]276 * This function allocates memory for a vseg descriptor, initialises it, and register it
[407]277 * in the VMM of the local process descriptor, that should be the reference process.
278 * For the 'stack", "file", "anon", & "remote" types, it does not use the <base> argument,
279 * but uses the STACK and MMAP virtual memory allocators.
280 * It checks collision with all pre-existing vsegs.
281 * To comply with the "on-demand" paging policy, this function does NOT modify the page table,
282 * and does not allocate physical memory for vseg data.
283 * It should be called by a local thread (could be a RPC thread if the client thread is not
284 * running in the regerence cluster).
[1]285 *********************************************************************************************
[407]286 * @ process     : pointer on local processor descriptor.
287 * @ type        : vseg type.
288 * @ base        : vseg base address (not used for dynamically allocated vsegs).
289 * @ size        : vseg size (bytes).
290 * @ file_offset : offset in file for CODE, DATA, FILE types.
291 * @ file_size   : can be smaller than "size" for DATA type.
292 * @ mapper_xp   : extended pointer on mapper for CODE, DATA, FILE types.
293 * @ cxy         : physical mapping cluster (for non distributed vsegs).
294 * @ returns pointer on vseg if success / returns NULL if no memory, or conflict.
[1]295 ********************************************************************************************/
296vseg_t * vmm_create_vseg( struct process_s * process,
[407]297                          vseg_type_t        type,
[18]298                          intptr_t           base,
[407]299                              uint32_t           size,
300                          uint32_t           file_offset,
301                          uint32_t           file_size,
302                          xptr_t             mapper_xp,
303                          cxy_t              cxy );
[1]304
305/*********************************************************************************************
[18]306 * This function removes a vseg identified by it's pointer from the VMM of the calling process.
[1]307 * - If the vseg has not the STACK or MMAP type, it is removed from the vsegs list,
308 *   and the physical memory allocated to vseg descriptor is released to KMEM.
309 * - If the vseg has the STACK type, it is removed from the vsegs list, the physical memory
310 *   allocated to vseg descriptor is released to KMEM, and the stack slot is returned to the
311 *   VMM STACK allocator.
312 * - If the vseg has the MMAP type, it is removed from the vsegs list and is registered
313 *   in the zombi_list of the VMM MMAP allocator for future reuse. The physical memory
314 *   allocated to vseg descriptor is NOT released to KMEM.
315 *********************************************************************************************
316 * @ vseg      : pointer on vseg to be removed.
317 ********************************************************************************************/
318void vmm_remove_vseg( vseg_t * vseg );
319
320/*********************************************************************************************
[18]321 * This function removes a given region (defined by a base address and a size) from
[407]322 * the VMM of a given process descriptor. This can modify the number of vsegs:
[1]323 * (a) if the region is not entirely mapped in an existing vseg, it's an error.
324 * (b) if the region has same base and size as an existing vseg, the vseg is removed.
[406]325 * (c) if the removed region cut the vseg in two parts, it is modified.
326 * (d) if the removed region cut the vseg in three parts, it is modified, and a new
327 *     vseg is created with same type.
[407]328 * FIXME [AG] this function must be called by a thread running in the reference cluster,
329 * and the VMM must be updated in all process descriptors copies.
[1]330 *********************************************************************************************
331 * @ process   : pointer on process descriptor
332 * @ base      : vseg base address
333 * @ size      : vseg size (bytes)
334 ********************************************************************************************/
335error_t vmm_resize_vseg( struct process_s * process,
336                         intptr_t           base,
337                         intptr_t           size );
338
339/*********************************************************************************************
[388]340 * This function checks that a given virtual address is contained in a registered vseg.
[399]341 * It can be called by any thread running in any cluster:
342 * - if the vseg is registered in the local process VMM, it returns the local vseg pointer.
[388]343 * - if the vseg is missing in local VMM, it uses a RPC to get it from the reference cluster,
344 *   register it in local VMM and returns the local vseg pointer, if success.
[406]345 * - it returns an user error if the vseg is missing in the reference VMM, or if there is
346 *   not enough memory for a new vseg descriptor in cluster containing the calling thread.
[1]347 *********************************************************************************************
[388]348 * @ process   : [in] pointer on process descriptor
349 * @ vaddr     : [in] virtual address
[440]350 * @ vseg      : [out] local pointer on local vseg
351 * @ returns 0 if success / returns -1 if user error (out of segment).
[1]352 *********************************************************************************************/
[388]353error_t vmm_get_vseg( struct process_s  * process,
354                      intptr_t            vaddr,
[394]355                      vseg_t           ** vseg );           
[1]356
357/*********************************************************************************************
[585]358 * This function is called by the generic exception handler in case of page-fault event,
359 * detected for a given <vpn> in a given <process> in any cluster.
360 * It checks the missing VPN and returns an user error if it is not in a registered vseg.
361 * For a legal VPN, there is actually 3 cases:
362 * 1) if the missing VPN belongs to a private vseg (STACK or CODE segment types, non
363 *    replicated in all clusters), it allocates a new physical page, computes the attributes,
364 *    depending on vseg type, and updates directly the local GPT.
365 * 2) if the missing VPN belongs to a public vseg, it can be a false page-fault, when the VPN
366 *    is mapped in the reference GPT, but not in the local GPT. For this false page-fault,
367 *    the local GPT is simply updated from the reference GPT.
368 * 3) if the missing VPN is public, and unmapped in the reference GPT, it's a true page fault.
369 *    The calling thread  allocates a new physical page, computes the attributes, depending
370 *    on vseg type, and updates directly (without RPC) the local GPT and the reference GPT.
371 *    Other GPT copies  will updated on demand.
372 * In the three cases, concurrent accesses to the GPT are handled, thanks to the
373 * remote_rwlock protecting each GPT copy.
[1]374 *********************************************************************************************
[440]375 * @ process   : pointer on local process descriptor copy.
[585]376 * @ vpn       : VPN of the missing PTE.
377 * @ returns EXCP_NON_FATAL / EXCP_USER_ERROR / EXCP_KERNEL_PANIC after analysis
[1]378 ********************************************************************************************/
379error_t vmm_handle_page_fault( struct process_s * process,
[585]380                               vpn_t              vpn );
[1]381
382/*********************************************************************************************
[585]383 * This function is called by the generic exception handler in case of copy-on-write event,
384 * detected for a given <vpn> in a given <process> in any cluster.
385 * It returns a kernel panic if VPN is not in a registered vseg or is not mapped.
386 * For a legal mapped vseg there is two cases:
387 * 1) If the missing VPN belongs to a private vseg (STACK or CODE segment types, non
388 *    replicated in all clusters), it access the local GPT to get the current PPN and ATTR.
389 *    It access the forks counter in the current physical page descriptor.
390 *    If there is a pending fork, it allocates a new physical page from the cluster defined
391 *    by the vseg type, copies the old physical page content to the new physical page,
392 *    and decrements the pending_fork counter in old physical page descriptor.
393 *    Finally, it reset the COW flag and set the WRITE flag in local GPT.
394 * 2) If the missing VPN is public, it access the reference GPT to get the current PPN and
395 *    ATTR. It access the forks counter in the current physical page descriptor.
396 *    If there is a pending fork, it allocates a new physical page from the cluster defined
397 *    by the vseg type, copies the old physical page content to the new physical page,
398 *    and decrements the pending_fork counter in old physical page descriptor.
399 *    Finally it calls the vmm_global_update_pte() function to reset the COW flag and set
400 *    the WRITE flag in all the GPT copies, using a RPC if the reference cluster is remote.
401 * In both cases, concurrent accesses to the GPT are handled, thanks to the
402 * remote_rwlock protecting each GPT copy.
[407]403 *********************************************************************************************
[585]404 * @ process   : pointer on local process descriptor copy.
405 * @ vpn       : VPN of the faulting PTE.
406 * @ returns EXCP_NON_FATAL / EXCP_USER_ERROR / EXCP_KERNEL_PANIC after analysis
[1]407 ********************************************************************************************/
[585]408error_t vmm_handle_cow( struct process_s * process,
409                        vpn_t              vpn );
[1]410
411/*********************************************************************************************
[401]412 * This function is called by the vmm_get_pte() function when a page is unmapped.
[313]413 * Depending on the vseg type, defined by the <vseg> argument, it returns the PPN
414 * (Physical Page Number) associated to a missing page defined by the <vpn> argument.
[406]415 * - For the FILE type, it returns directly the physical page from the file mapper.
[433]416 * - For the CODE and DATA types, it allocates a new physical page from the cluster defined
[406]417 *   by the <vseg->cxy> field, or by the <vpn> MSB bits for a distributed vseg,
418 *   and initialize this page from the .elf file mapper.
419 * - For all other types, it allocates a new physical page from the cluster defined
420 *   by the <vseg->cxy> field, or by the <vpn> MSB bits for a distributed vseg,
421 *   but the new page is not initialized.
[313]422 *********************************************************************************************
423 * @ vseg   : local pointer on vseg containing the mising page.
424 * @ vpn    : Virtual Page Number identifying the missing page.
425 * @ ppn    : [out] returned Physical Page Number.
[401]426 * return 0 if success / return EINVAL or ENOMEM if error.
[313]427 ********************************************************************************************/
428error_t vmm_get_one_ppn( vseg_t * vseg,
429                         vpn_t    vpn,
430                         ppn_t  * ppn );
431
[1]432
433#endif /* _VMM_H_ */
Note: See TracBrowser for help on using the repository browser.