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

Last change on this file since 5 was 1, checked in by alain, 8 years ago

First import

File size: 21.2 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)
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 <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
52 * define the STACK zone state. 
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.
67 * This allocator sould be only used in the reference cluster.
68 * - allocation policy : all allocated vsegs occupy an integer number of pages that is
69 *   power of 2, and are aligned on a page boundary. The requested number of pages is
70 *   rounded if reqired. The first_free_vpn variable defines completely the MMAP zone state.
71 *   It is never decremented, as the released vsegs are simply registered in a zombi_list.
72 *   The relevant zombi_list is checked first for each allocation request.
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,
97 *    using dedicated allocators.
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       */
105        uint32_t       vsegs_nr;           /*! total number of vsegs                            */
106
107        grdxt_t        grdxt;              /*! embedded generic vsegs radix tree (key is vpn)   */
108
109    gpt_t          gpt;                /*! embedded generic page table descriptor           */
110
111    stack_mgr_t    stack_mgr;          /*! embedded STACK vsegs allocator                   */
112
113    mmap_mgr_t     mmap_mgr;           /*! embedded MMAP vsegs allocator                    */
114
115        uint32_t       pgfault_nr;         /*! page fault counter                               */
116        uint32_t       u_err_nr;           /*! TODO ??? [AG]                                    */
117        uint32_t       m_err_nr;           /*! TODO ??? [AG]                                    */
118
119    vpn_t          kent_vpn_base;      /*! kentry vseg first page                           */
120    vpn_t          args_vpn_base;      /*! args vseg first page                             */
121    vpn_t          envs_vpn_base;      /*! envs zone first page                             */
122    vpn_t          heap_vpn_base;      /*! envs zone first page                             */
123        vpn_t          code_vpn_base;      /*! code zone first page                             */
124        vpn_t          data_vpn_base;      /*! data zone first page                             */
125
126        intptr_t       entry_point;        /*! main thread entry point                          */
127
128    vseg_t       * heap_vseg;          /*! pointer on local heap vseg descriptor            */
129}
130vmm_t;
131
132/*********************************************************************************************
133 * This structure is used to store the arguments of the mmap() system call.
134 ********************************************************************************************/
135
136typedef struct mmap_attr_s
137{
138        void     * addr;            /*! requested virtual address (unused : should be NULL)     */ 
139        uint32_t   length;          /*! requested vseg size (bytes)                             */
140        uint32_t   prot;            /*! access modes                                            */
141        uint32_t   flags;           /*! only MAP_FILE / MAP_ANON / MAP_PRIVATE / MAP_SHARED     */
142        fdid_t     fdid;            /*! file descriptor index (if MAP_FILE is set)              */
143        int32_t    offset;          /*! file offset (if MAP_FILE is set)                        */
144}
145mmap_attr_t;
146
147/*********************************************************************************************
148 * This function initialises the virtual memory manager attached to a process.
149 * - It initializes the VSL (list of vsegs and associated radix tree).
150 * - It initializes the generic page table (empty).
151 * - It initializes the STAK and MMAP allocators.
152 * - It registers the "kentry", "args", "envs" and "heap" vsegs in the vsegs list.
153 * Any error in this function gives a kernel panic.
154 *********************************************************************************************
155 * @ process   : pointer on process descriptor
156 ********************************************************************************************/
157void vmm_init( struct process_s * process );
158
159/*********************************************************************************************
160 * This function removes all vsegs registered in in a virtual memory manager,
161 * and releases the memory allocated to the local generic page table.
162 *********************************************************************************************
163 * @ vmm   : pointer on process descriptor.
164 ********************************************************************************************/
165void vmm_destroy( struct process_s * process );
166
167/*********************************************************************************************
168 * This function scan the list of vsegs registered in the VMM of a given process descriptor
169 * to check if a given virtual region (defined by a base and size) overlap an existing vseg.
170 *********************************************************************************************
171 * @ process  : pointer on process descriptor.
172 * @ base     : region virtual base address.
173 * @ size     : region size (bytes).
174 * @ returns NULL if no conflict / return conflicting vseg pointer if conflict.
175 ********************************************************************************************/
176vseg_t * vmm_check_conflict( struct process_s * process,
177                             vpn_t              base,
178                             vpn_t              size );
179
180/*********************************************************************************************
181 * This function allocates memory for a vseg descriptor, initialises it, and register it
182 * in the VMM of the process. It checks the collision with pre-existing vsegs in VMM.
183 * For STACK and MMAP types vseg, it does not use the base argument, but uses the VMM STACK
184 * and MMAP specific allocators to get a base address in virtual space.
185 * To comply with the "on-demand" paging policy, this function does NOT mofify the
186 * page table, and does not allocate physical memory for vseg data.
187 *********************************************************************************************
188 * @ vmm   : pointer on process descriptor.
189 * @ base      : vseg base address
190 * @ size      : vseg size (bytes)
191 * @ type      : vseg type
192 * @ returns pointer on vseg if success / returns NULL if no memory or conflict.
193 ********************************************************************************************/
194vseg_t * vmm_create_vseg( struct process_s * process,
195                          intptr_t           base, 
196                              intptr_t           size, 
197                              uint32_t           type );
198
199/*********************************************************************************************
200 * Ths function removes a vseg identified by it's pointer from the VMM of the calling process.
201 * - If the vseg has not the STACK or MMAP type, it is removed from the vsegs list,
202 *   and the physical memory allocated to vseg descriptor is released to KMEM.
203 * - If the vseg has the STACK type, it is removed from the vsegs list, the physical memory
204 *   allocated to vseg descriptor is released to KMEM, and the stack slot is returned to the
205 *   VMM STACK allocator.
206 * - If the vseg has the MMAP type, it is removed from the vsegs list and is registered
207 *   in the zombi_list of the VMM MMAP allocator for future reuse. The physical memory
208 *   allocated to vseg descriptor is NOT released to KMEM.
209 *********************************************************************************************
210 * @ vseg      : pointer on vseg to be removed.
211 ********************************************************************************************/
212void vmm_remove_vseg( vseg_t * vseg );
213
214/*********************************************************************************************
215 * This function allocates physical memory from the local cluster to map all PTEs
216 * of a "kernel" vseg (type KCODE , KDATA, or KDEV) in the page table of process_zero.
217 * It should not be used for other vseg types, because "user" vsegs use the
218 * "on-demand-paging" policy.
219 *********************************************************************************************
220 * @ vseg     : pointer on the vseg to be mapped.
221 * @ attr     : GPT attributes to be set for all vseg pages.
222 * @ returns 0 if success / returns ENOMEM if no memory
223 ********************************************************************************************/
224error_t vmm_map_vseg( vseg_t           * vseg,
225                      uint32_t           attr );
226
227/*********************************************************************************************
228 * This function unmap all PTEs of a given vseg, in the generic page table asociated
229 * to a given process descriptor, and releases the corresponding physical memory.
230 * It can be used for any type of vseg.
231 *********************************************************************************************
232 * @ process  : pointer on process descriptor.
233 * @ vseg     : pointer on the vseg to be unmapped.
234 ********************************************************************************************/
235void vmm_unmap_vseg( struct process_s * process,
236                     vseg_t           * vseg );
237
238/*********************************************************************************************
239 * This function remove a given region (defined by a base address and a size) from
240 * the VMM of a given process descriptor. This can modify several vsegs:
241 * (a) if the region is not entirely mapped in an existing vseg, it's an error.
242 * (b) if the region has same base and size as an existing vseg, the vseg is removed.
243 * (c) if the removed region cut the vseg in two parts, it is removed and re-created.
244 * (d) if the removed region cut the vseg in three parts, it is removed, and two are created.
245 * TODO : cases (c) and (d) are not implemented [AG]
246 *********************************************************************************************
247 * @ process   : pointer on process descriptor
248 * @ base      : vseg base address
249 * @ size      : vseg size (bytes)
250 ********************************************************************************************/
251error_t vmm_resize_vseg( struct process_s * process,
252                         intptr_t           base,
253                         intptr_t           size );
254
255/*********************************************************************************************
256 * This function search if a given virtual address is contained in a vseg registered in
257 * the local process VMM and returns the vseg pointer if success.
258 *********************************************************************************************
259 * @ process   : pointer on process descriptor
260 * @ vaddr     : virtual address
261 * @ returns the pointer on vseg if success / returns NULL if failure.
262 *********************************************************************************************/
263vseg_t * vmm_get_vseg( struct process_s * process,
264                       intptr_t           vaddr );
265
266/*********************************************************************************************
267 * This function is called by the architecture specific exception handler when a
268 * page fault has been detected in a given cluster.
269 * If the local cluster is not the reference cluster, it send a RPC_VMM_GET_PTE
270 * to the reference cluster to get the missing PTE attributes and PPN, and update
271 * the local page table. If the local cluster is the reference, it call directly
272 * the vmm_get_pte() function.
273 *********************************************************************************************
274 * @ process   : pointer on process descriptor.
275 * @ vseg      : pointer on involved vseg.
276 * @ vpn       : VPN of the missing PTE.
277 * @ returns 0 if success / returns ENOMEM if no memory.
278 ********************************************************************************************/
279error_t vmm_handle_page_fault( struct process_s * process,
280                               vseg_t           * vseg,
281                               vpn_t              vpn ); 
282
283/*********************************************************************************************
284 * This function returns in the "attr" and "ppn" arguments the PTE associated to a given
285 * VPN for a given process. This function must be called on the reference cluster.
286 * To get the PTE from another cluster, use the RPC_VMM_GET_PTE.
287 * The vseg containing the searched VPN should be registered in the reference VMM.
288 * If the PTE in the reference page table is unmapped, this function allocates the missing
289 * physical page from the target cluster defined by the vseg type, and update the reference
290 * page table. It can call a RPC_PMEM_GET_PAGES to get the missing physical page,
291 * if the target cluster is not the reference cluster.
292 *********************************************************************************************
293 * @ process   : [in] pointer on process descriptor.
294 * @ vpn       : [in] VPN defining the missing PTE.
295 * @ attr      : [out] PTE attributes.
296 * @ ppn       : [out] PTE ppn.
297 * @ returns 0 if success / returns ENOMEM if error.
298 ********************************************************************************************/
299error_t vmm_get_pte( struct process_s * process,
300                     vpn_t              vpn,
301                     uint32_t         * attr,
302                     ppn_t            * ppn );
303
304/*********************************************************************************************
305 * This function makes the virtual to physical address translation, using the calling
306 * process page table. It uses identity mapping if required by the ident flag.
307 * This address translation is required to configure the devices
308 * that have a DMA capability, or to implement the software L2/L3 cache cohérence,
309 * using the MMC device synchronisation primitives.
310 * WARNING : the <ident> value must be defined by the CONFIG_KERNEL_IDENT parameter.
311 *********************************************************************************************
312 * @ ident     : [in] uses identity mapping if true.
313 * @ ptr       : [in] virtual address.
314 * @ paddr     : [out] pointer on buffer for physical address.
315 * @ returns 0 if success / returns ENOMEM if error.
316 ********************************************************************************************/
317error_t vmm_v2p_translate( bool_t    ident,
318                           void    * ptr,
319                           paddr_t * paddr );
320
321
322/*********************************************************************************************
323 * Pas a sa place ici [AG]
324 ********************************************************************************************/
325int sys_mmap( mmap_attr_t * mattr );
326
327/*********************************************************************************************
328 ********************************************************************************************/
329int sys_madvise( void    * start,
330                 uint32_t  length,
331                 uint32_t  advice );
332
333/*********************************************************************************************
334 ********************************************************************************************/
335int sys_sbrk( uint32_t current_heap_ptr, 
336              uint32_t size );
337
338/*********************************************************************************************
339 ********************************************************************************************/
340error_t vmm_sbrk( vmm_t   * vmm, 
341                  uint32_t  current,
342                  uint32_t  size );
343
344/*********************************************************************************************
345 ********************************************************************************************/
346error_t vmm_madvise_migrate( vmm_t    * vmm,
347                             uint32_t   start, 
348                             uint32_t   len );
349
350/*********************************************************************************************
351 ********************************************************************************************/
352error_t vmm_madvise_willneed( vmm_t   * vmm,
353                              uint32_t  start,
354                              uint32_t  len );
355
356/*********************************************************************************************
357 ********************************************************************************************/
358error_t vmm_set_auto_migrate( vmm_t    * vmm,
359                              uint32_t   start,
360                              uint32_t   flags );
361
362/*********************************************************************************************
363 * Hypothesis: the region is shared-anon, mapper list is rdlocked, page is locked
364 ********************************************************************************************/
365error_t vmm_broadcast_inval( vseg_t * region,
366                             page_t      * page,
367                             page_t     ** new );
368
369/*********************************************************************************************
370 * Hypothesis: the region is shared-anon, mapper list is rdlocked, page is locked
371 ********************************************************************************************/
372error_t vmm_migrate_shared_page_seq( vseg_t * region,
373                                     page_t      * page,
374                                     page_t     ** new );
375
376#endif /* _VMM_H_ */
Note: See TracBrowser for help on using the repository browser.