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

Last change on this file since 326 was 313, checked in by alain, 7 years ago

RSeveral modifs in the page-fault handling.

File size: 22.8 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)
[23]6 *           Alain Greiner (2016,2017)
[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
29#include <hal_types.h>
30#include <bits.h>
31#include <list.h>
32#include <grdxt.h>
33#include <spinlock.h>
34#include <hal_gpt.h>
35#include <vseg.h>
36#include <page.h>
37
38/****  Forward declarations  ****/
39
40struct process_s;
41
42/*********************************************************************************************
43 * This structure defines the STACK allocator used by the VMM to dynamically allocate
44 * STACK vsegs requested or released by the user process.
45 * This allocator handles a fixed size array of fixed size slots stores in the STACK zone.
46 * The stack size and the number of slots are defined by the CONFIG_VMM_STACK_SIZE, and
47 * CONFIG_THREAD
48 * Each slot can contain one user stack vseg. The first page in the slot is not allocated
49 * to detect stack overflow.
50 * The slot index can be computed form the slot base address, and reversely.
51 * All allocation / release operations are registered in the stack_bitmap, that completely
[18]52 * define the STACK zone state.
[1]53 * In this implementation, the max number of slots is 32.
54 ********************************************************************************************/
55
56typedef struct stack_mgr_s
57{
58    spinlock_t     lock;               /*! lock protecting STACK allocator                  */
59    vpn_t          vpn_base;           /*! first page of STACK zone                         */
60    bitmap_t       bitmap;             /*! bit bector of allocated stacks                   */
61}
62stack_mgr_t;
63
64/*********************************************************************************************
65 * This structure defines the MMAP allocator used by the VMM to dynamically allocate
66 * MMAP vsegs requested or released by an user process.
[18]67 * This allocator should be only used in the reference cluster.
68 * - allocation policy : all allocated vsegs occupy an integer number of pages that is
[1]69 *   power of 2, and are aligned on a page boundary. The requested number of pages is
[18]70 *   rounded if required. The first_free_vpn variable defines completely the MMAP zone state.
[1]71 *   It is never decremented, as the released vsegs are simply registered in a zombi_list.
[18]72 *   The relevant zombi_list is checked first for each allocation request.
[1]73 * - release policy : a released MMAP vseg is registered in an array of zombi_lists.
74 *   This array is indexed by ln(number of pages), and each entry contains the root of
75 *   a local list of zombi vsegs that have the same size. The physical memory allocated
76 *   for a zombi vseg descriptor is not released, to use the "list" field.
77 *   This physical memory allocated for MMAP vseg descriptors is actually released
78 *   when the VMM is destroyed.
79 ********************************************************************************************/
80
81typedef struct mmap_mgr_s
82{
83    spinlock_t     lock;               /*! lock protecting MMAP allocator                   */
84    vpn_t          vpn_base;           /*! first page of MMAP zone                          */
85    vpn_t          vpn_size;           /*! number of pages in MMAP zone                     */
86    vpn_t          first_free_vpn;     /*! first free page in MMAP zone                     */
87    list_entry_t   zombi_list[32];     /*! array of roots of released vsegs lists           */
88}
89mmap_mgr_t;
90
91/*********************************************************************************************
92 * This structure defines the Virtual Memory Manager for a given process in a given cluster.
93 * This local VMM provides three main services:
94 * 1) It registers all vsegs statically or dynamically defined in the vseg list,
95 *    and in the associated radix-tree.
96 * 2) It allocates virtual memory space for the STACKS and MMAP vsegs,
[18]97 *    using dedicated allocators.
[1]98 * 3) It contains the local copy of the generic page table descriptor.
99 ********************************************************************************************/
100
101typedef struct vmm_s
102{
103        rwlock_t       vsegs_lock;         /*! lock protecting the vsegs list & radix tree      */
104        list_entry_t   vsegs_root;         /*! all vsegs in same process and same cluster       */
[23]105        uint32_t       vsegs_nr;           /*! total number of local vsegs                      */
[1]106        grdxt_t        grdxt;              /*! embedded generic vsegs radix tree (key is vpn)   */
107
108    gpt_t          gpt;                /*! embedded generic page table descriptor           */
109
110    stack_mgr_t    stack_mgr;          /*! embedded STACK vsegs allocator                   */
111    mmap_mgr_t     mmap_mgr;           /*! embedded MMAP vsegs allocator                    */
112
113        uint32_t       pgfault_nr;         /*! page fault counter                               */
114        uint32_t       u_err_nr;           /*! TODO ??? [AG]                                    */
115        uint32_t       m_err_nr;           /*! TODO ??? [AG]                                    */
116
117    vpn_t          kent_vpn_base;      /*! kentry vseg first page                           */
118    vpn_t          args_vpn_base;      /*! args vseg first page                             */
119    vpn_t          envs_vpn_base;      /*! envs zone first page                             */
120    vpn_t          heap_vpn_base;      /*! envs zone first page                             */
121        vpn_t          code_vpn_base;      /*! code zone first page                             */
122        vpn_t          data_vpn_base;      /*! data zone first page                             */
123
124        intptr_t       entry_point;        /*! main thread entry point                          */
125
126    vseg_t       * heap_vseg;          /*! pointer on local heap vseg descriptor            */
127}
128vmm_t;
129
130/*********************************************************************************************
131 * This structure is used to store the arguments of the mmap() system call.
132 ********************************************************************************************/
133
134typedef struct mmap_attr_s
135{
[18]136        void     * addr;            /*! requested virtual address (unused : should be NULL)     */
[1]137        uint32_t   length;          /*! requested vseg size (bytes)                             */
138        uint32_t   prot;            /*! access modes                                            */
139        uint32_t   flags;           /*! only MAP_FILE / MAP_ANON / MAP_PRIVATE / MAP_SHARED     */
140        fdid_t     fdid;            /*! file descriptor index (if MAP_FILE is set)              */
141        int32_t    offset;          /*! file offset (if MAP_FILE is set)                        */
142}
143mmap_attr_t;
144
145/*********************************************************************************************
146 * This function initialises the virtual memory manager attached to a process.
147 * - It initializes the VSL (list of vsegs and associated radix tree).
148 * - It initializes the generic page table (empty).
149 * - It initializes the STAK and MMAP allocators.
150 * - It registers the "kentry", "args", "envs" and "heap" vsegs in the vsegs list.
151 * Any error in this function gives a kernel panic.
152 *********************************************************************************************
153 * @ process   : pointer on process descriptor
154 ********************************************************************************************/
155void vmm_init( struct process_s * process );
156
157/*********************************************************************************************
[23]158 * This function copies the content of a source VMM to a destination VMM.
159 *********************************************************************************************
160 * @ dst_process   : pointer on destination process descriptor.
161 * @ src_process   : pointer on source process descriptor.
162 * @ return 0 if success / return ENOMEM if failure.
163 ********************************************************************************************/
164error_t vmm_copy( struct process_s * dst_process,
165                  struct process_s * src_process );
166
167/*********************************************************************************************
[1]168 * This function removes all vsegs registered in in a virtual memory manager,
[18]169 * and releases the memory allocated to the local generic page table.
[1]170 *********************************************************************************************
[23]171 * @ process   : pointer on process descriptor.
[1]172 ********************************************************************************************/
173void vmm_destroy( struct process_s * process );
174
175/*********************************************************************************************
[18]176 * This function scans the list of vsegs registered in the VMM of a given process descriptor
[1]177 * to check if a given virtual region (defined by a base and size) overlap an existing vseg.
178 *********************************************************************************************
179 * @ process  : pointer on process descriptor.
180 * @ base     : region virtual base address.
181 * @ size     : region size (bytes).
182 * @ returns NULL if no conflict / return conflicting vseg pointer if conflict.
183 ********************************************************************************************/
184vseg_t * vmm_check_conflict( struct process_s * process,
185                             vpn_t              base,
186                             vpn_t              size );
187
188/*********************************************************************************************
[18]189 * This function allocates memory for a vseg descriptor, initialises it, and register it
[1]190 * in the VMM of the process. It checks the collision with pre-existing vsegs in VMM.
191 * For STACK and MMAP types vseg, it does not use the base argument, but uses the VMM STACK
192 * and MMAP specific allocators to get a base address in virtual space.
[18]193 * To comply with the "on-demand" paging policy, this function does NOT modify the
[1]194 * page table, and does not allocate physical memory for vseg data.
195 *********************************************************************************************
196 * @ vmm   : pointer on process descriptor.
197 * @ base      : vseg base address
198 * @ size      : vseg size (bytes)
199 * @ type      : vseg type
200 * @ returns pointer on vseg if success / returns NULL if no memory or conflict.
201 ********************************************************************************************/
202vseg_t * vmm_create_vseg( struct process_s * process,
[18]203                          intptr_t           base,
204                              intptr_t           size,
[1]205                              uint32_t           type );
206
207/*********************************************************************************************
[18]208 * This function removes a vseg identified by it's pointer from the VMM of the calling process.
[1]209 * - If the vseg has not the STACK or MMAP type, it is removed from the vsegs list,
210 *   and the physical memory allocated to vseg descriptor is released to KMEM.
211 * - If the vseg has the STACK type, it is removed from the vsegs list, the physical memory
212 *   allocated to vseg descriptor is released to KMEM, and the stack slot is returned to the
213 *   VMM STACK allocator.
214 * - If the vseg has the MMAP type, it is removed from the vsegs list and is registered
215 *   in the zombi_list of the VMM MMAP allocator for future reuse. The physical memory
216 *   allocated to vseg descriptor is NOT released to KMEM.
217 *********************************************************************************************
218 * @ vseg      : pointer on vseg to be removed.
219 ********************************************************************************************/
220void vmm_remove_vseg( vseg_t * vseg );
221
222/*********************************************************************************************
[18]223 * This function allocates physical memory from the local cluster to map all PTEs
[1]224 * of a "kernel" vseg (type KCODE , KDATA, or KDEV) in the page table of process_zero.
[68]225 * WARNING : It should not be used for "user" vsegs, that must be mapped using the
[1]226 * "on-demand-paging" policy.
227 *********************************************************************************************
228 * @ vseg     : pointer on the vseg to be mapped.
229 * @ attr     : GPT attributes to be set for all vseg pages.
230 * @ returns 0 if success / returns ENOMEM if no memory
231 ********************************************************************************************/
[68]232error_t vmm_map_kernel_vseg( vseg_t           * vseg,
233                             uint32_t           attr );
[1]234
235/*********************************************************************************************
[18]236 * This function unmaps all PTEs of a given vseg, in the generic page table associated
[1]237 * to a given process descriptor, and releases the corresponding physical memory.
238 * It can be used for any type of vseg.
239 *********************************************************************************************
240 * @ process  : pointer on process descriptor.
241 * @ vseg     : pointer on the vseg to be unmapped.
242 ********************************************************************************************/
243void vmm_unmap_vseg( struct process_s * process,
244                     vseg_t           * vseg );
245
246/*********************************************************************************************
[18]247 * This function removes a given region (defined by a base address and a size) from
[1]248 * the VMM of a given process descriptor. This can modify several vsegs:
249 * (a) if the region is not entirely mapped in an existing vseg, it's an error.
250 * (b) if the region has same base and size as an existing vseg, the vseg is removed.
251 * (c) if the removed region cut the vseg in two parts, it is removed and re-created.
252 * (d) if the removed region cut the vseg in three parts, it is removed, and two are created.
253 * TODO : cases (c) and (d) are not implemented [AG]
254 *********************************************************************************************
255 * @ process   : pointer on process descriptor
256 * @ base      : vseg base address
257 * @ size      : vseg size (bytes)
258 ********************************************************************************************/
259error_t vmm_resize_vseg( struct process_s * process,
260                         intptr_t           base,
261                         intptr_t           size );
262
263/*********************************************************************************************
[18]264 * This function searches if a given virtual address is contained in a vseg registered in
[1]265 * the local process VMM and returns the vseg pointer if success.
266 *********************************************************************************************
267 * @ process   : pointer on process descriptor
268 * @ vaddr     : virtual address
269 * @ returns the pointer on vseg if success / returns NULL if failure.
270 *********************************************************************************************/
271vseg_t * vmm_get_vseg( struct process_s * process,
272                       intptr_t           vaddr );
273
[313]274
[1]275/*********************************************************************************************
[23]276 * This function is called by the generic exception handler when a page fault
277 * has been detected in a given cluster.
[1]278 * If the local cluster is not the reference cluster, it send a RPC_VMM_GET_PTE
279 * to the reference cluster to get the missing PTE attributes and PPN, and update
280 * the local page table. If the local cluster is the reference, it call directly
281 * the vmm_get_pte() function.
282 *********************************************************************************************
283 * @ process   : pointer on process descriptor.
284 * @ vseg      : pointer on involved vseg.
285 * @ vpn       : VPN of the missing PTE.
286 * @ returns 0 if success / returns ENOMEM if no memory.
287 ********************************************************************************************/
288error_t vmm_handle_page_fault( struct process_s * process,
289                               vseg_t           * vseg,
[18]290                               vpn_t              vpn );
[1]291
292/*********************************************************************************************
293 * This function returns in the "attr" and "ppn" arguments the PTE associated to a given
[313]294 * VPN for a given process. This function must be called by a thread running in the
295 * reference cluster. To get the PTE from another cluster, use the RPC_VMM_GET_PTE.
[1]296 * The vseg containing the searched VPN should be registered in the reference VMM.
297 * If the PTE in the reference page table is unmapped, this function allocates the missing
[313]298 * physical page from the target cluster defined by the vseg type, initilize it,
299 * and update the reference page table. It calls the RPC_PMEM_GET_PAGES to get and
300 * initialize the missing physical page, if the target cluster is not the reference cluster.
[1]301 *********************************************************************************************
302 * @ process   : [in] pointer on process descriptor.
303 * @ vpn       : [in] VPN defining the missing PTE.
304 * @ attr      : [out] PTE attributes.
305 * @ ppn       : [out] PTE ppn.
306 * @ returns 0 if success / returns ENOMEM if error.
307 ********************************************************************************************/
308error_t vmm_get_pte( struct process_s * process,
309                     vpn_t              vpn,
310                     uint32_t         * attr,
311                     ppn_t            * ppn );
312
313/*********************************************************************************************
[313]314 * This function is called by the vmm_get_pte() function.
315 * Depending on the vseg type, defined by the <vseg> argument, it returns the PPN
316 * (Physical Page Number) associated to a missing page defined by the <vpn> argument.
317 * - For the VSEG_TYPE_FILE, it returns the physical page from the file mapper.
318 *   For all other types, it allocates a new physical page from the cluster defined
319 *   by the <vseg->cxy> field, or by the <vpn> MSB bits for a distributed vseg.
320 * - For the VSEG_TYPE_CODE and VSEG_TYPE_DATA types, the allocated page is initialized
321 *   from the .elf file mapper. For others vseg types it is not initialised.
322 *********************************************************************************************
323 * @ vseg   : local pointer on vseg containing the mising page.
324 * @ vpn    : Virtual Page Number identifying the missing page.
325 * @ ppn    : [out] returned Physical Page Number.
326 ********************************************************************************************/
327error_t vmm_get_one_ppn( vseg_t * vseg,
328                         vpn_t    vpn,
329                         ppn_t  * ppn );
330
331/*********************************************************************************************
[18]332 * This function makes the virtual to physical address translation, using the calling
[68]333 * process page table. It uses identity mapping if required by the <ident> argument.
[23]334 * This address translation is required to configure the peripherals having a DMA
335 * capability, or to implement the software L2/L3 cache cohérence, using the MMC device
336 * synchronisation primitives.
337 * WARNING : the <ident> value must be defined by the CONFIG_KERNEL_IDENTITY_MAP parameter.
[1]338 *********************************************************************************************
339 * @ ident     : [in] uses identity mapping if true.
340 * @ ptr       : [in] virtual address.
341 * @ paddr     : [out] pointer on buffer for physical address.
342 * @ returns 0 if success / returns ENOMEM if error.
343 ********************************************************************************************/
344error_t vmm_v2p_translate( bool_t    ident,
345                           void    * ptr,
346                           paddr_t * paddr );
347
348
349/*********************************************************************************************
350 ********************************************************************************************/
351int sys_madvise( void    * start,
352                 uint32_t  length,
353                 uint32_t  advice );
354
355/*********************************************************************************************
356 ********************************************************************************************/
[18]357int sys_sbrk( uint32_t current_heap_ptr,
[1]358              uint32_t size );
359
360/*********************************************************************************************
361 ********************************************************************************************/
[18]362error_t vmm_sbrk( vmm_t   * vmm,
[1]363                  uint32_t  current,
364                  uint32_t  size );
365
366/*********************************************************************************************
367 ********************************************************************************************/
368error_t vmm_madvise_migrate( vmm_t    * vmm,
[18]369                             uint32_t   start,
[1]370                             uint32_t   len );
371
372/*********************************************************************************************
373 ********************************************************************************************/
374error_t vmm_madvise_willneed( vmm_t   * vmm,
375                              uint32_t  start,
376                              uint32_t  len );
377
378/*********************************************************************************************
379 ********************************************************************************************/
380error_t vmm_set_auto_migrate( vmm_t    * vmm,
381                              uint32_t   start,
382                              uint32_t   flags );
383
384/*********************************************************************************************
[18]385 * Hypothesis: the region is shared-anon, mapper list is rdlocked, page is locked
[1]386 ********************************************************************************************/
387error_t vmm_broadcast_inval( vseg_t * region,
388                             page_t      * page,
389                             page_t     ** new );
390
391/*********************************************************************************************
[18]392 * Hypothesis: the region is shared-anon, mapper list is rdlocked, page is locked
[1]393 ********************************************************************************************/
394error_t vmm_migrate_shared_page_seq( vseg_t * region,
395                                     page_t      * page,
396                                     page_t     ** new );
397
398#endif /* _VMM_H_ */
Note: See TracBrowser for help on using the repository browser.