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

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

Few bugs.

File size: 26.4 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)
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 <spinlock.h>
33#include <hal_gpt.h>
34#include <vseg.h>
35#include <page.h>
36
37/****  Forward declarations  ****/
38
39struct process_s;
40
41/*********************************************************************************************
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.
45 * The stack size and the number of slots are defined by the CONFIG_VMM_STACK_SIZE, and
46 * CONFIG_VMM_STACK_BASE parameters.
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
51 * define the STACK zone state.
52 ********************************************************************************************/
53
54typedef struct stack_mgr_s
55{
56    spinlock_t     lock;               /*! lock protecting STACK allocator                  */
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/*********************************************************************************************
63 * This structure defines the MMAP allocator used by the VMM to dynamically handle 
64 * MMAP vsegs requested or released by an user process.
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
67 *   power of 2, and are aligned on a page boundary. The requested number of pages is
68 *   rounded if required. The first_free_vpn variable defines completely the MMAP zone state.
69 *   It is never decremented, as the released vsegs are simply registered in a zombi_list.
70 *   The relevant zombi_list is checked first for each allocation request.
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{
81    spinlock_t     lock;               /*! lock protecting MMAP allocator                   */
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.
91 * This local VMM provides four main services:
92 * 1) It registers all vsegs in the local copy of the vseg list (VSL).
93 * 2) It contains the local copy of the generic page table (GPT).
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:
98 * 1. The VSL contains only local vsegs, but it is implemented as an xlist, and protected by
99 *    a remote_rwlock, because it can be accessed by a thread running in a remote cluster.
100 *    An exemple is the vmm_fork_copy() function.
101 * 2. In most custers, the VSL and GPT are only partial copies of the reference VSL and GPT
102 *    structures, stored in the reference cluster.
103 ********************************************************************************************/
104
105typedef struct vmm_s
106{
107        remote_rwlock_t  vsegs_lock;         /*! lock protecting the vsegs list                 */
108        xlist_entry_t    vsegs_root;         /*! VSL root (VSL only complete in reference)      */
109        uint32_t         vsegs_nr;           /*! total number of local vsegs                    */
110
111    gpt_t            gpt;                /*! Generic Page Table (complete in reference)     */
112
113    stack_mgr_t      stack_mgr;          /*! embedded STACK vsegs allocator                 */
114    mmap_mgr_t       mmap_mgr;           /*! embedded MMAP vsegs allocator                  */
115
116        uint32_t         pgfault_nr;         /*! page fault counter (instrumentation)           */
117
118    vpn_t            kent_vpn_base;      /*! kentry vseg first page                         */
119    vpn_t            args_vpn_base;      /*! args vseg first page                           */
120    vpn_t            envs_vpn_base;      /*! envs zone first page                           */
121    vpn_t            heap_vpn_base;      /*! envs zone first page                           */
122        vpn_t            code_vpn_base;      /*! code zone first page                           */
123        vpn_t            data_vpn_base;      /*! data zone first page                           */
124
125        intptr_t         entry_point;        /*! main thread entry point                        */
126}
127vmm_t;
128
129/*********************************************************************************************
130 * This function initialises the virtual memory manager attached to an user process.
131 * - It initializes the STACK and MMAP allocators.
132 * - It registers the "kentry", "args", "envs" vsegs in the VSL.
133 * - It initializes the generic page table, calling the HAL specific hal_gpt_init() function.
134 * - For TSAR it map all pages for the "kentry" vseg, that must be identity mapping.
135 * Note:
136 * - The "code" and "data" vsegs are registered by the elf_load_process() function.
137 * - The "stack" vsegs are dynamically created by the thread_user_create() function.
138 * - The "file", "anon", "remote" vsegs are dynamically created by the mmap() syscall.
139 *********************************************************************************************
140 * @ process   : pointer on process descriptor
141 * @ return 0 if success / return -1 if failure.
142 ********************************************************************************************/
143error_t vmm_init( struct process_s * process );
144
145/*********************************************************************************************
146 * This function displays on TXY0 the list or registered vsegs for a given <process>.
147 * If the <mapping> argument is true, it displays for each vesg all mapped PTEs in GPT.
148 *********************************************************************************************
149 * @ process   : pointer on process descriptor.
150 * @ mapping   : detailed mapping if true.
151 ********************************************************************************************/
152void vmm_display( struct process_s * process,
153                  bool_t             mapping );
154
155/*********************************************************************************************
156 * This function is called by the process_fork_create() function. It partially copies
157 * the content of a remote parent process VMM to the local child process VMM:
158 * - all DATA, MMAP, REMOTE vsegs registered in the parent VSL are registered in the child
159 *   VSL, and all valid GPT entries in parent GPT are copied to the child GPT.
160 *   The WRITABLE flag is reset and the COW flag is set in child GPT.
161 * - all CODE vsegs registered in the parent VSL are registered in the child VSL, but the
162 *   GPT entries are not copied in the chilf GPT, that will be dynamically updated from
163 *   the .elf file when a page fault is reported.
164 * - all FILE vsegs registered in the parent VSL are registered in the child VSL, and all
165 *   valid GPT entries in parent GPT are copied to the child GPT. The COW flag is not set.
166 * - no STACK vseg is copied from  parent VMM to child VMM, because the child STACK vseg
167 *   must be copied from the cluster containing the user thread requesting the fork().
168 *********************************************************************************************
169 * @ child_process     : local pointer on local child process descriptor.
170 * @ parent_process_xp : extended pointer on remote parent process descriptor.
171 * @ return 0 if success / return -1 if failure.
172 ********************************************************************************************/
173error_t vmm_fork_copy( struct process_s * child_process,
174                       xptr_t             parent_process_xp );
175
176/*********************************************************************************************
177 * This function is called by the process_make_fork() function to handle the fork syscall.
178 * It set the COW flag, and reset the WRITABLE flag of all GPT entries of the DATA, MMAP,
179 * and REMOTE vsegs of a process identified by the <process> argument.
180 * It must be called by a thread running in the reference cluster, that contains the complete
181 * list of vsegs. Use the rpc_vmm_set_cow_client() when the calling thread client is remote.
182 * It updates all copies of the process in all clusters, to maintain coherence in GPT copies,
183 * using the list of copies stored in the owner process, and using remote_write accesses to
184 * update the remote GPTs. It cannot fail, as only mapped entries in GPT copies are updated.
185 *********************************************************************************************
186 * @ process   : local pointer on local reference process descriptor.
187 ********************************************************************************************/
188void vmm_set_cow( struct process_s * process );
189
190/*********************************************************************************************
191 * This function is called by the vmm_get_pte() function in case of COW exception.
192 * It modifies both the PPN an the attributes for a GPT entry identified by the <process>
193 * and <vpn> arguments.
194 * It updates all copies of the process in all clusters, to maintain coherence in GPT copies,
195 * using the list of copies stored in the owner process, and using remote_write accesses to
196 * update the remote GPTs. It cannot fail, as only mapped entries in GPT copies are updated.
197 *********************************************************************************************
198 * @ process   : local pointer on local process descriptor.
199 * @ vpn       : PTE index.
200 * @ attr      : PTE / attributes.
201 * @ ppn       : PTE / physical page index.
202 ********************************************************************************************/
203void vmm_update_pte( struct process_s * process,
204                     vpn_t              vpn,
205                     uint32_t           attr,
206                     ppn_t              ppn );
207
208/*********************************************************************************************
209 * This function scan the list of vsegs registered in the VSL of the process
210 * identified by the <process> argument, and for each vseg:
211 * - it unmap from the GPT and releases all mapped pages in vseg.
212 * - it removes the vseg from the process VSL.
213 * - It releases the memory allocated to the vseg descriptor.
214 * Finally, it releases the memory allocated to the GPT itself.
215 *********************************************************************************************
216 * @ process   : pointer on process descriptor.
217 ********************************************************************************************/
218void vmm_destroy( struct process_s * process );
219
220/*********************************************************************************************
221 * This function scans the list of vsegs registered in the VMM of a given process descriptor
222 * to check if a given virtual region (defined by a base and size) overlap an existing vseg.
223 *********************************************************************************************
224 * @ process  : pointer on process descriptor.
225 * @ base     : region virtual base address.
226 * @ size     : region size (bytes).
227 * @ returns NULL if no conflict / return conflicting vseg pointer if conflict.
228 ********************************************************************************************/
229vseg_t * vmm_check_conflict( struct process_s * process,
230                             vpn_t              base,
231                             vpn_t              size );
232
233/*********************************************************************************************
234 * This function allocates memory for a vseg descriptor, initialises it, and register it
235 * in the VMM of the local process descriptor, that should be the reference process.
236 * For the 'stack", "file", "anon", & "remote" types, it does not use the <base> argument,
237 * but uses the STACK and MMAP virtual memory allocators.
238 * It checks collision with all pre-existing vsegs.
239 * To comply with the "on-demand" paging policy, this function does NOT modify the page table,
240 * and does not allocate physical memory for vseg data.
241 * It should be called by a local thread (could be a RPC thread if the client thread is not
242 * running in the regerence cluster).
243 *********************************************************************************************
244 * @ process     : pointer on local processor descriptor.
245 * @ type        : vseg type.
246 * @ base        : vseg base address (not used for dynamically allocated vsegs).
247 * @ size        : vseg size (bytes).
248 * @ file_offset : offset in file for CODE, DATA, FILE types.
249 * @ file_size   : can be smaller than "size" for DATA type.
250 * @ mapper_xp   : extended pointer on mapper for CODE, DATA, FILE types.
251 * @ cxy         : physical mapping cluster (for non distributed vsegs).
252 * @ returns pointer on vseg if success / returns NULL if no memory, or conflict.
253 ********************************************************************************************/
254vseg_t * vmm_create_vseg( struct process_s * process,
255                          vseg_type_t        type,
256                          intptr_t           base,
257                              uint32_t           size,
258                          uint32_t           file_offset,
259                          uint32_t           file_size,
260                          xptr_t             mapper_xp,
261                          cxy_t              cxy );
262
263/*********************************************************************************************
264 * This function removes a vseg identified by it's pointer from the VMM of the calling process.
265 * - If the vseg has not the STACK or MMAP type, it is removed from the vsegs list,
266 *   and the physical memory allocated to vseg descriptor is released to KMEM.
267 * - If the vseg has the STACK type, it is removed from the vsegs list, the physical memory
268 *   allocated to vseg descriptor is released to KMEM, and the stack slot is returned to the
269 *   VMM STACK allocator.
270 * - If the vseg has the MMAP type, it is removed from the vsegs list and is registered
271 *   in the zombi_list of the VMM MMAP allocator for future reuse. The physical memory
272 *   allocated to vseg descriptor is NOT released to KMEM.
273 *********************************************************************************************
274 * @ vseg      : pointer on vseg to be removed.
275 ********************************************************************************************/
276void vmm_remove_vseg( vseg_t * vseg );
277
278/*********************************************************************************************
279 * This function allocates physical memory from the local cluster to map all PTEs
280 * of a "kernel" vseg (type KCODE , KDATA, or KDEV) in the page table of process_zero.
281 * WARNING : It should not be used for "user" vsegs, that must be mapped using the
282 * "on-demand-paging" policy.
283 *********************************************************************************************
284 * @ vseg     : pointer on the vseg to be mapped.
285 * @ attr     : GPT attributes to be set for all vseg pages.
286 * @ returns 0 if success / returns ENOMEM if no memory
287 ********************************************************************************************/
288error_t vmm_map_kernel_vseg( vseg_t           * vseg,
289                             uint32_t           attr );
290
291/*********************************************************************************************
292 * This function unmaps all mapped PTEs of a given vseg, from the generic page table
293 * associated to a given process descriptor, and releases the physical memory allocated
294 * to all mapped GPT entries.  It can be used for any type of vseg.
295 *********************************************************************************************
296 * @ process  : pointer on process descriptor.
297 * @ vseg     : pointer on the vseg to be unmapped.
298 ********************************************************************************************/
299void vmm_unmap_vseg( struct process_s * process,
300                     vseg_t           * vseg );
301
302/*********************************************************************************************
303 * This function removes a given region (defined by a base address and a size) from
304 * the VMM of a given process descriptor. This can modify the number of vsegs:
305 * (a) if the region is not entirely mapped in an existing vseg, it's an error.
306 * (b) if the region has same base and size as an existing vseg, the vseg is removed.
307 * (c) if the removed region cut the vseg in two parts, it is modified.
308 * (d) if the removed region cut the vseg in three parts, it is modified, and a new
309 *     vseg is created with same type.
310 * FIXME [AG] this function must be called by a thread running in the reference cluster,
311 * and the VMM must be updated in all process descriptors copies.
312 *********************************************************************************************
313 * @ process   : pointer on process descriptor
314 * @ base      : vseg base address
315 * @ size      : vseg size (bytes)
316 ********************************************************************************************/
317error_t vmm_resize_vseg( struct process_s * process,
318                         intptr_t           base,
319                         intptr_t           size );
320
321/*********************************************************************************************
322 * This function checks that a given virtual address is contained in a registered vseg.
323 * It can be called by any thread running in any cluster:
324 * - if the vseg is registered in the local process VMM, it returns the local vseg pointer.
325 * - if the vseg is missing in local VMM, it uses a RPC to get it from the reference cluster,
326 *   register it in local VMM and returns the local vseg pointer, if success.
327 * - it returns an user error if the vseg is missing in the reference VMM, or if there is
328 *   not enough memory for a new vseg descriptor in cluster containing the calling thread.
329 *********************************************************************************************
330 * @ process   : [in] pointer on process descriptor
331 * @ vaddr     : [in] virtual address
332 * @ vseg      : [out] pointer on found vseg
333 * @ returns 0 if success / returns -1 if user error.
334 *********************************************************************************************/
335error_t vmm_get_vseg( struct process_s  * process,
336                      intptr_t            vaddr,
337                      vseg_t           ** vseg );           
338
339/*********************************************************************************************
340 * This function is called by the generic exception handler when a page-fault event
341 * has been detected in a given cluster.
342 * - If the local cluster is the reference, it call directly the vmm_get_pte() function.
343 * - If the local cluster is not the reference cluster, it send a RPC_VMM_GET_PTE
344 *   to the reference cluster to get the missing PTE attributes and PPN,
345 *   and update the local page table.
346 *********************************************************************************************
347 * @ process   : pointer on process descriptor.
348 * @ vpn       : VPN of the missing PTE.
349 * @ returns 0 if success / returns ENOMEM if no memory.
350 ********************************************************************************************/
351error_t vmm_handle_page_fault( struct process_s * process,
352                               vpn_t              vpn );
353
354/*********************************************************************************************
355 * This function is called by the generic exception handler when a copy-on-write event
356 * has been detected in a given cluster.
357 * - If the local cluster is the reference, it call directly the vmm_get_pte() function.
358 * - If the local cluster is not the reference cluster, it send a RPC_VMM_GET_PTE
359 *   to the reference cluster to get the missing PTE attributes and PPN,
360 *   and update the local page table.
361 *********************************************************************************************
362 * @ process   : pointer on process descriptor.
363 * @ vpn       : VPN of the missing PTE.
364 * @ returns 0 if success / returns ENOMEM if no memory.
365 ********************************************************************************************/
366error_t vmm_handle_cow( struct process_s * process,
367                        vpn_t              vpn );
368
369/*********************************************************************************************
370 * This function is called when a new PTE (GPT entry) is required because a "page-fault",
371 * or "copy-on_write" event has been detected for a given <vpn> in a given <process>.
372 * The <cow> argument defines the type of event to be handled.
373 * This function must be called by a thread running in reference cluster, and the vseg
374 * containing the searched VPN should be registered in the reference VMM.
375 * - for an actual page-fault, it allocates the missing physical page from the target cluster
376 *   defined by the vseg type, initialize it, and update the reference page table.
377 * - for a copy-on-write, it allocates a new physical page from the target cluster,
378 *   initialise it from the old physical page, and update the reference page table.
379 * In both cases, it calls the RPC_PMEM_GET_PAGES to get the new physical page if the
380 * target cluster is not the reference cluster.
381 * It returns in the <attr> and <ppn> arguments the accessed or modified PTE.
382 *********************************************************************************************
383 * @ process   : [in] pointer on process descriptor.
384 * @ vpn       : [in] VPN defining the missing PTE.
385 * @ cow       : [in] "copy_on_write" if true / "page_fault" if false.
386 * @ attr      : [out] PTE attributes.
387 * @ ppn       : [out] PTE ppn.
388 * @ returns 0 if success / returns ENOMEM if error.
389 ********************************************************************************************/
390error_t vmm_get_pte( struct process_s * process,
391                     vpn_t              vpn,
392                     bool_t             cow,
393                     uint32_t         * attr,
394                     ppn_t            * ppn );
395
396/*********************************************************************************************
397 * This function is called by the vmm_get_pte() function when a page is unmapped.
398 * Depending on the vseg type, defined by the <vseg> argument, it returns the PPN
399 * (Physical Page Number) associated to a missing page defined by the <vpn> argument.
400 * - For the FILE type, it returns directly the physical page from the file mapper.
401 * - For the CODE and DATA types, it allocates a new phsical page from the cluster defined
402 *   by the <vseg->cxy> field, or by the <vpn> MSB bits for a distributed vseg,
403 *   and initialize this page from the .elf file mapper.
404 * - For all other types, it allocates a new physical page from the cluster defined
405 *   by the <vseg->cxy> field, or by the <vpn> MSB bits for a distributed vseg,
406 *   but the new page is not initialized.
407 *********************************************************************************************
408 * @ vseg   : local pointer on vseg containing the mising page.
409 * @ vpn    : Virtual Page Number identifying the missing page.
410 * @ ppn    : [out] returned Physical Page Number.
411 * return 0 if success / return EINVAL or ENOMEM if error.
412 ********************************************************************************************/
413error_t vmm_get_one_ppn( vseg_t * vseg,
414                         vpn_t    vpn,
415                         ppn_t  * ppn );
416
417/*********************************************************************************************
418 * This function makes the virtual to physical address translation, using the calling
419 * process page table. It uses identity mapping if required by the <ident> argument.
420 * This address translation is required to configure the peripherals having a DMA
421 * capability, or to implement the software L2/L3 cache cohérence, using the MMC device
422 * synchronisation primitives.
423 * WARNING : the <ident> value must be defined by the CONFIG_KERNEL_IDENTITY_MAP parameter.
424 *********************************************************************************************
425 * @ ident     : [in] uses identity mapping if true.
426 * @ ptr       : [in] virtual address.
427 * @ paddr     : [out] pointer on buffer for physical address.
428 * @ returns 0 if success / returns ENOMEM if error.
429 ********************************************************************************************/
430error_t vmm_v2p_translate( bool_t    ident,
431                           void    * ptr,
432                           paddr_t * paddr );
433
434
435
436#endif /* _VMM_H_ */
Note: See TracBrowser for help on using the repository browser.