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

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

First implementation of fork/exec.

File size: 23.1 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 three main services:
92 * 1) It registers all vsegs statically or dynamically defined in the vseg list.
93 * 2) It allocates virtual memory space for the STACKS and MMAP vsegs (FILE/ANON/REMOTE).
94 * 3) It contains the local copy of the generic page table descriptor.
95 ********************************************************************************************/
96
97typedef struct vmm_s
98{
99        rwlock_t       vsegs_lock;         /*! lock protecting the vsegs list                   */
100        list_entry_t   vsegs_root;         /*! all vsegs in same process and same cluster       */
101        uint32_t       vsegs_nr;           /*! total number of local vsegs                      */
102
103    gpt_t          gpt;                /*! embedded generic page table descriptor           */
104
105    stack_mgr_t    stack_mgr;          /*! embedded STACK vsegs allocator                   */
106    mmap_mgr_t     mmap_mgr;           /*! embedded MMAP vsegs allocator                    */
107
108        uint32_t       pgfault_nr;         /*! page fault counter (instrumentation)             */
109        uint32_t       u_err_nr;           /*! TODO ??? [AG]                                    */
110        uint32_t       m_err_nr;           /*! TODO ??? [AG]                                    */
111
112    vpn_t          kent_vpn_base;      /*! kentry vseg first page                           */
113    vpn_t          args_vpn_base;      /*! args vseg first page                             */
114    vpn_t          envs_vpn_base;      /*! envs zone first page                             */
115    vpn_t          heap_vpn_base;      /*! envs zone first page                             */
116        vpn_t          code_vpn_base;      /*! code zone first page                             */
117        vpn_t          data_vpn_base;      /*! data zone first page                             */
118
119        intptr_t       entry_point;        /*! main thread entry point                          */
120}
121vmm_t;
122
123/*********************************************************************************************
124 * This function initialises the virtual memory manager attached to an user process.
125 * - It initializes the STACK and MMAP allocators.
126 * - It registers the "kentry", "args", "envs" vsegs in the VSL.
127 * - The "code" and "data" vsegs are registered by the elf_load_process() function.
128 * - The "stack" vsegs are dynamically created by the thread_user_create() function.
129 * - The "file", "anon", "remote" vsegs are dynamically created by the mmap() syscalls.
130 * - It initializes the generic page table, calling the HAL specific hal_gpt_init() function.
131 * - For TSAR it map all pages for the "kentry" vseg, that must be identity mapping.
132 * TODO : Any error in this function gives a kernel panic => improve error handling.
133 *********************************************************************************************
134 * @ process   : pointer on process descriptor
135 ********************************************************************************************/
136void vmm_init( struct process_s * process );
137
138/*********************************************************************************************
139 * This function displays on TXY0 the list or registered vsegs for a given <process>.
140 * If the <mapping> argument is true, it displays for each vesg all mapped PTEs in GPT.
141 *********************************************************************************************
142 * @ process   : pointer on process descriptor.
143 * @ mapping   : detailed mapping if true.
144 ********************************************************************************************/
145void vmm_display( struct process_s * process,
146                  bool_t             mapping );
147
148/*********************************************************************************************
149 * This function is called by the sys_fork() system call.
150 * It copies the content of a parent process descriptor VMM to a child process VMM.
151 * - All vsegs registered in the source VSL are copied in the destination VSL.
152 * - All PTEs registered in the source GPT are copied in destination GPT. For all writable
153 *   PTEs - but the FILE vsegs - the WRITABLE flag is reset and the COW flag is set in
154 *   the destination GPT.
155 *********************************************************************************************
156 * @ dst_process   : pointer on destination process descriptor.
157 * @ src_process   : pointer on source process descriptor.
158 * @ return 0 if success / return ENOMEM if failure.
159 ********************************************************************************************/
160error_t vmm_copy( struct process_s * dst_process,
161                  struct process_s * src_process );
162
163/*********************************************************************************************
164 * This function removes all vsegs registered in in a virtual memory manager,
165 * and releases the memory allocated to the local generic page table.
166 *********************************************************************************************
167 * @ process   : pointer on process descriptor.
168 ********************************************************************************************/
169void vmm_destroy( struct process_s * process );
170
171/*********************************************************************************************
172 * This function scans the list of vsegs registered in the VMM of a given process descriptor
173 * to check if a given virtual region (defined by a base and size) overlap an existing vseg.
174 *********************************************************************************************
175 * @ process  : pointer on process descriptor.
176 * @ base     : region virtual base address.
177 * @ size     : region size (bytes).
178 * @ returns NULL if no conflict / return conflicting vseg pointer if conflict.
179 ********************************************************************************************/
180vseg_t * vmm_check_conflict( struct process_s * process,
181                             vpn_t              base,
182                             vpn_t              size );
183
184/*********************************************************************************************
185 * This function allocates memory for a vseg descriptor, initialises it, and register it
186 * in the VMM of the local process descriptor, that should be the reference process.
187 * For the 'stack", "file", "anon", & "remote" types, it does not use the <base> argument,
188 * but uses the STACK and MMAP virtual memory allocators.
189 * It checks collision with all pre-existing vsegs.
190 * To comply with the "on-demand" paging policy, this function does NOT modify the page table,
191 * and does not allocate physical memory for vseg data.
192 * It should be called by a local thread (could be a RPC thread if the client thread is not
193 * running in the regerence cluster).
194 *********************************************************************************************
195 * @ process     : pointer on local processor descriptor.
196 * @ type        : vseg type.
197 * @ base        : vseg base address (not used for dynamically allocated vsegs).
198 * @ size        : vseg size (bytes).
199 * @ file_offset : offset in file for CODE, DATA, FILE types.
200 * @ file_size   : can be smaller than "size" for DATA type.
201 * @ mapper_xp   : extended pointer on mapper for CODE, DATA, FILE types.
202 * @ cxy         : physical mapping cluster (for non distributed vsegs).
203 * @ returns pointer on vseg if success / returns NULL if no memory, or conflict.
204 ********************************************************************************************/
205vseg_t * vmm_create_vseg( struct process_s * process,
206                          vseg_type_t        type,
207                          intptr_t           base,
208                              uint32_t           size,
209                          uint32_t           file_offset,
210                          uint32_t           file_size,
211                          xptr_t             mapper_xp,
212                          cxy_t              cxy );
213
214/*********************************************************************************************
215 * This function removes a vseg identified by it's pointer from the VMM of the calling process.
216 * - If the vseg has not the STACK or MMAP type, it is removed from the vsegs list,
217 *   and the physical memory allocated to vseg descriptor is released to KMEM.
218 * - If the vseg has the STACK type, it is removed from the vsegs list, the physical memory
219 *   allocated to vseg descriptor is released to KMEM, and the stack slot is returned to the
220 *   VMM STACK allocator.
221 * - If the vseg has the MMAP type, it is removed from the vsegs list and is registered
222 *   in the zombi_list of the VMM MMAP allocator for future reuse. The physical memory
223 *   allocated to vseg descriptor is NOT released to KMEM.
224 *********************************************************************************************
225 * @ vseg      : pointer on vseg to be removed.
226 ********************************************************************************************/
227void vmm_remove_vseg( vseg_t * vseg );
228
229/*********************************************************************************************
230 * This function allocates physical memory from the local cluster to map all PTEs
231 * of a "kernel" vseg (type KCODE , KDATA, or KDEV) in the page table of process_zero.
232 * WARNING : It should not be used for "user" vsegs, that must be mapped using the
233 * "on-demand-paging" policy.
234 *********************************************************************************************
235 * @ vseg     : pointer on the vseg to be mapped.
236 * @ attr     : GPT attributes to be set for all vseg pages.
237 * @ returns 0 if success / returns ENOMEM if no memory
238 ********************************************************************************************/
239error_t vmm_map_kernel_vseg( vseg_t           * vseg,
240                             uint32_t           attr );
241
242/*********************************************************************************************
243 * This function unmaps all PTEs of a given vseg, in the generic page table associated
244 * to a given process descriptor, and releases the corresponding physical memory.
245 * It can be used for any type of vseg.
246 *********************************************************************************************
247 * @ process  : pointer on process descriptor.
248 * @ vseg     : pointer on the vseg to be unmapped.
249 ********************************************************************************************/
250void vmm_unmap_vseg( struct process_s * process,
251                     vseg_t           * vseg );
252
253/*********************************************************************************************
254 * This function removes a given region (defined by a base address and a size) from
255 * the VMM of a given process descriptor. This can modify the number of vsegs:
256 * (a) if the region is not entirely mapped in an existing vseg, it's an error.
257 * (b) if the region has same base and size as an existing vseg, the vseg is removed.
258 * (c) if the removed region cut the vseg in two parts, it is modified.
259 * (d) if the removed region cut the vseg in three parts, it is modified, and a new
260 *     vseg is created with same type.
261 * FIXME [AG] this function must be called by a thread running in the reference cluster,
262 * and the VMM must be updated in all process descriptors copies.
263 *********************************************************************************************
264 * @ process   : pointer on process descriptor
265 * @ base      : vseg base address
266 * @ size      : vseg size (bytes)
267 ********************************************************************************************/
268error_t vmm_resize_vseg( struct process_s * process,
269                         intptr_t           base,
270                         intptr_t           size );
271
272/*********************************************************************************************
273 * This function checks that a given virtual address is contained in a registered vseg.
274 * It can be called by any thread running in any cluster:
275 * - if the vseg is registered in the local process VMM, it returns the local vseg pointer.
276 * - if the vseg is missing in local VMM, it uses a RPC to get it from the reference cluster,
277 *   register it in local VMM and returns the local vseg pointer, if success.
278 * - it returns an user error if the vseg is missing in the reference VMM, or if there is
279 *   not enough memory for a new vseg descriptor in cluster containing the calling thread.
280 *********************************************************************************************
281 * @ process   : [in] pointer on process descriptor
282 * @ vaddr     : [in] virtual address
283 * @ vseg      : [out] pointer on found vseg
284 * @ returns 0 if success / returns -1 if user error.
285 *********************************************************************************************/
286error_t vmm_get_vseg( struct process_s  * process,
287                      intptr_t            vaddr,
288                      vseg_t           ** vseg );           
289
290/*********************************************************************************************
291 * This function is called by the generic exception handler when a page-fault event
292 * has been detected in a given cluster.
293 * - If the local cluster is the reference, it call directly the vmm_get_pte() function.
294 * - If the local cluster is not the reference cluster, it send a RPC_VMM_GET_PTE
295 *   to the reference cluster to get the missing PTE attributes and PPN,
296 *   and update the local page table.
297 *********************************************************************************************
298 * @ process   : pointer on process descriptor.
299 * @ vpn       : VPN of the missing PTE.
300 * @ returns 0 if success / returns ENOMEM if no memory.
301 ********************************************************************************************/
302error_t vmm_handle_page_fault( struct process_s * process,
303                               vpn_t              vpn );
304
305/*********************************************************************************************
306 * This function is called by the generic exception handler when a copy-on-write event
307 * has been detected in a given cluster.
308 * - If the local cluster is the reference, it call directly the vmm_get_pte() function.
309 * - If the local cluster is not the reference cluster, it send a RPC_VMM_GET_PTE
310 *   to the reference cluster to get the missing PTE attributes and PPN,
311 *   and update the local page table.
312 *********************************************************************************************
313 * @ process   : pointer on process descriptor.
314 * @ vpn       : VPN of the missing PTE.
315 * @ returns 0 if success / returns ENOMEM if no memory.
316 ********************************************************************************************/
317error_t vmm_copy_on_write( struct process_s * process,
318                           vpn_t              vpn );
319
320/*********************************************************************************************
321 * This function is called when a new PTE (GPT entry) is required because a "page-fault",
322 * or "copy-on_write" event has been detected for a given <vpn> in a given <process>.
323 * The <cow> argument defines the type of event to be handled.
324 * This function must be called by a thread running in reference cluster, and the vseg
325 * containing the searched VPN should be registered in the reference VMM.
326 * - for an actual page-fault, it allocates the missing physical page from the target cluster
327 *   defined by the vseg type, initialize it, and update the reference page table.
328 * - for a copy-on-write, it allocates a new physical page from the target cluster,
329 *   initialise it from the old physical page, and update the reference page table.
330 * In both cases, it calls the RPC_PMEM_GET_PAGES to get the new physical page if the
331 * target cluster is not the reference cluster.
332 * It returns in the <attr> and <ppn> arguments the accessed or modified PTE.
333 *********************************************************************************************
334 * @ process   : [in] pointer on process descriptor.
335 * @ vpn       : [in] VPN defining the missing PTE.
336 * @ cow       : [in] "copy_on_write" if true / "page_fault" if false.
337 * @ attr      : [out] PTE attributes.
338 * @ ppn       : [out] PTE ppn.
339 * @ returns 0 if success / returns ENOMEM if error.
340 ********************************************************************************************/
341error_t vmm_get_pte( struct process_s * process,
342                     vpn_t              vpn,
343                     bool_t             cow,
344                     uint32_t         * attr,
345                     ppn_t            * ppn );
346
347/*********************************************************************************************
348 * This function is called by the vmm_get_pte() function when a page is unmapped.
349 * Depending on the vseg type, defined by the <vseg> argument, it returns the PPN
350 * (Physical Page Number) associated to a missing page defined by the <vpn> argument.
351 * - For the FILE type, it returns directly the physical page from the file mapper.
352 * - For the CODE and DATA types, it allocates a new phsical page from the cluster defined
353 *   by the <vseg->cxy> field, or by the <vpn> MSB bits for a distributed vseg,
354 *   and initialize this page from the .elf file mapper.
355 * - For all other types, it allocates a new physical page from the cluster defined
356 *   by the <vseg->cxy> field, or by the <vpn> MSB bits for a distributed vseg,
357 *   but the new page is not initialized.
358 *********************************************************************************************
359 * @ vseg   : local pointer on vseg containing the mising page.
360 * @ vpn    : Virtual Page Number identifying the missing page.
361 * @ ppn    : [out] returned Physical Page Number.
362 * return 0 if success / return EINVAL or ENOMEM if error.
363 ********************************************************************************************/
364error_t vmm_get_one_ppn( vseg_t * vseg,
365                         vpn_t    vpn,
366                         ppn_t  * ppn );
367
368/*********************************************************************************************
369 * This function makes the virtual to physical address translation, using the calling
370 * process page table. It uses identity mapping if required by the <ident> argument.
371 * This address translation is required to configure the peripherals having a DMA
372 * capability, or to implement the software L2/L3 cache cohérence, using the MMC device
373 * synchronisation primitives.
374 * WARNING : the <ident> value must be defined by the CONFIG_KERNEL_IDENTITY_MAP parameter.
375 *********************************************************************************************
376 * @ ident     : [in] uses identity mapping if true.
377 * @ ptr       : [in] virtual address.
378 * @ paddr     : [out] pointer on buffer for physical address.
379 * @ returns 0 if success / returns ENOMEM if error.
380 ********************************************************************************************/
381error_t vmm_v2p_translate( bool_t    ident,
382                           void    * ptr,
383                           paddr_t * paddr );
384
385
386
387#endif /* _VMM_H_ */
Note: See TracBrowser for help on using the repository browser.