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

Last change on this file since 41 was 23, checked in by alain, 7 years ago

Introduce syscalls.

File size: 21.5 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.
[18]225 * It should not be used for other vseg types, because "user" vsegs use 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 ********************************************************************************************/
232error_t vmm_map_vseg( vseg_t           * vseg,
233                      uint32_t           attr );
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
274/*********************************************************************************************
[23]275 * This function is called by the generic exception handler when a page fault
276 * has been detected in a given cluster.
[1]277 * If the local cluster is not the reference cluster, it send a RPC_VMM_GET_PTE
278 * to the reference cluster to get the missing PTE attributes and PPN, and update
279 * the local page table. If the local cluster is the reference, it call directly
280 * the vmm_get_pte() function.
281 *********************************************************************************************
282 * @ process   : pointer on process descriptor.
283 * @ vseg      : pointer on involved vseg.
284 * @ vpn       : VPN of the missing PTE.
285 * @ returns 0 if success / returns ENOMEM if no memory.
286 ********************************************************************************************/
287error_t vmm_handle_page_fault( struct process_s * process,
288                               vseg_t           * vseg,
[18]289                               vpn_t              vpn );
[1]290
291/*********************************************************************************************
292 * This function returns in the "attr" and "ppn" arguments the PTE associated to a given
[18]293 * VPN for a given process. This function must be called on the reference cluster.
[1]294 * To get the PTE from another cluster, use the RPC_VMM_GET_PTE.
295 * The vseg containing the searched VPN should be registered in the reference VMM.
296 * If the PTE in the reference page table is unmapped, this function allocates the missing
297 * physical page from the target cluster defined by the vseg type, and update the reference
[18]298 * page table. It can call a RPC_PMEM_GET_PAGES to get the missing physical page,
[1]299 * if the target cluster is not the reference cluster.
300 *********************************************************************************************
301 * @ process   : [in] pointer on process descriptor.
302 * @ vpn       : [in] VPN defining the missing PTE.
303 * @ attr      : [out] PTE attributes.
304 * @ ppn       : [out] PTE ppn.
305 * @ returns 0 if success / returns ENOMEM if error.
306 ********************************************************************************************/
307error_t vmm_get_pte( struct process_s * process,
308                     vpn_t              vpn,
309                     uint32_t         * attr,
310                     ppn_t            * ppn );
311
312/*********************************************************************************************
[18]313 * This function makes the virtual to physical address translation, using the calling
314 * process page table. It uses identity mapping if required by the ident flag.
[23]315 * This address translation is required to configure the peripherals having a DMA
316 * capability, or to implement the software L2/L3 cache cohérence, using the MMC device
317 * synchronisation primitives.
318 * WARNING : the <ident> value must be defined by the CONFIG_KERNEL_IDENTITY_MAP parameter.
[1]319 *********************************************************************************************
320 * @ ident     : [in] uses identity mapping if true.
321 * @ ptr       : [in] virtual address.
322 * @ paddr     : [out] pointer on buffer for physical address.
323 * @ returns 0 if success / returns ENOMEM if error.
324 ********************************************************************************************/
325error_t vmm_v2p_translate( bool_t    ident,
326                           void    * ptr,
327                           paddr_t * paddr );
328
329
330/*********************************************************************************************
331 ********************************************************************************************/
332int sys_madvise( void    * start,
333                 uint32_t  length,
334                 uint32_t  advice );
335
336/*********************************************************************************************
337 ********************************************************************************************/
[18]338int sys_sbrk( uint32_t current_heap_ptr,
[1]339              uint32_t size );
340
341/*********************************************************************************************
342 ********************************************************************************************/
[18]343error_t vmm_sbrk( vmm_t   * vmm,
[1]344                  uint32_t  current,
345                  uint32_t  size );
346
347/*********************************************************************************************
348 ********************************************************************************************/
349error_t vmm_madvise_migrate( vmm_t    * vmm,
[18]350                             uint32_t   start,
[1]351                             uint32_t   len );
352
353/*********************************************************************************************
354 ********************************************************************************************/
355error_t vmm_madvise_willneed( vmm_t   * vmm,
356                              uint32_t  start,
357                              uint32_t  len );
358
359/*********************************************************************************************
360 ********************************************************************************************/
361error_t vmm_set_auto_migrate( vmm_t    * vmm,
362                              uint32_t   start,
363                              uint32_t   flags );
364
365/*********************************************************************************************
[18]366 * Hypothesis: the region is shared-anon, mapper list is rdlocked, page is locked
[1]367 ********************************************************************************************/
368error_t vmm_broadcast_inval( vseg_t * region,
369                             page_t      * page,
370                             page_t     ** new );
371
372/*********************************************************************************************
[18]373 * Hypothesis: the region is shared-anon, mapper list is rdlocked, page is locked
[1]374 ********************************************************************************************/
375error_t vmm_migrate_shared_page_seq( vseg_t * region,
376                                     page_t      * page,
377                                     page_t     ** new );
378
379#endif /* _VMM_H_ */
Note: See TracBrowser for help on using the repository browser.