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

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

This version executed successfully the user "init" process on a mono-processor TSAR architecture.

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