| 1 | /* | 
|---|
| 2 | * vmm.c - virtual memory manager related operations interface. | 
|---|
| 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 | #include <kernel_config.h> | 
|---|
| 27 | #include <hal_types.h> | 
|---|
| 28 | #include <hal_special.h> | 
|---|
| 29 | #include <hal_gpt.h> | 
|---|
| 30 | #include <hal_vmm.h> | 
|---|
| 31 | #include <printk.h> | 
|---|
| 32 | #include <memcpy.h> | 
|---|
| 33 | #include <rwlock.h> | 
|---|
| 34 | #include <list.h> | 
|---|
| 35 | #include <xlist.h> | 
|---|
| 36 | #include <bits.h> | 
|---|
| 37 | #include <process.h> | 
|---|
| 38 | #include <thread.h> | 
|---|
| 39 | #include <vseg.h> | 
|---|
| 40 | #include <cluster.h> | 
|---|
| 41 | #include <scheduler.h> | 
|---|
| 42 | #include <vfs.h> | 
|---|
| 43 | #include <mapper.h> | 
|---|
| 44 | #include <page.h> | 
|---|
| 45 | #include <kmem.h> | 
|---|
| 46 | #include <vmm.h> | 
|---|
| 47 |  | 
|---|
| 48 | ////////////////////////////////////////////////////////////////////////////////// | 
|---|
| 49 | //   Extern global variables | 
|---|
| 50 | ////////////////////////////////////////////////////////////////////////////////// | 
|---|
| 51 |  | 
|---|
| 52 | extern  process_t  process_zero;   // defined in cluster.c file | 
|---|
| 53 |  | 
|---|
| 54 |  | 
|---|
| 55 | /////////////////////////////////////// | 
|---|
| 56 | error_t vmm_init( process_t * process ) | 
|---|
| 57 | { | 
|---|
| 58 | error_t   error; | 
|---|
| 59 | vseg_t  * vseg_kentry; | 
|---|
| 60 | vseg_t  * vseg_args; | 
|---|
| 61 | vseg_t  * vseg_envs; | 
|---|
| 62 | intptr_t  base; | 
|---|
| 63 | intptr_t  size; | 
|---|
| 64 |  | 
|---|
| 65 | #if CONFIG_DEBUG_VMM_INIT | 
|---|
| 66 | uint32_t cycle = (uint32_t)hal_get_cycles(); | 
|---|
| 67 | if( CONFIG_DEBUG_VMM_INIT ) | 
|---|
| 68 | printk("\n[DBG] %s : thread %x enter for process %x / cycle %d\n", | 
|---|
| 69 | __FUNCTION__ , CURRENT_THREAD , process->pid , cycle ); | 
|---|
| 70 | #endif | 
|---|
| 71 |  | 
|---|
| 72 | // get pointer on VMM | 
|---|
| 73 | vmm_t   * vmm = &process->vmm; | 
|---|
| 74 |  | 
|---|
| 75 | // initialize local list of vsegs | 
|---|
| 76 | vmm->vsegs_nr = 0; | 
|---|
| 77 | xlist_root_init( XPTR( local_cxy , &vmm->vsegs_root ) ); | 
|---|
| 78 | remote_rwlock_init( XPTR( local_cxy , &vmm->vsegs_lock ) ); | 
|---|
| 79 |  | 
|---|
| 80 | assert( ((CONFIG_VMM_KENTRY_SIZE + CONFIG_VMM_ARGS_SIZE + CONFIG_VMM_ENVS_SIZE) | 
|---|
| 81 | <= CONFIG_VMM_ELF_BASE) , __FUNCTION__ , "UTILS zone too small\n" ); | 
|---|
| 82 |  | 
|---|
| 83 | assert( (CONFIG_THREAD_MAX_PER_CLUSTER <= 32) , __FUNCTION__ , | 
|---|
| 84 | "no more than 32 threads per cluster for a single process\n"); | 
|---|
| 85 |  | 
|---|
| 86 | assert( ((CONFIG_VMM_STACK_SIZE * CONFIG_THREAD_MAX_PER_CLUSTER) <= | 
|---|
| 87 | (CONFIG_VMM_VSPACE_SIZE - CONFIG_VMM_STACK_BASE)) , __FUNCTION__ , | 
|---|
| 88 | "STACK zone too small\n"); | 
|---|
| 89 |  | 
|---|
| 90 | // register kentry vseg in VSL | 
|---|
| 91 | base = CONFIG_VMM_KENTRY_BASE << CONFIG_PPM_PAGE_SHIFT; | 
|---|
| 92 | size = CONFIG_VMM_KENTRY_SIZE << CONFIG_PPM_PAGE_SHIFT; | 
|---|
| 93 |  | 
|---|
| 94 | vseg_kentry = vmm_create_vseg( process, | 
|---|
| 95 | VSEG_TYPE_CODE, | 
|---|
| 96 | base, | 
|---|
| 97 | size, | 
|---|
| 98 | 0,             // file_offset unused | 
|---|
| 99 | 0,             // file_size unused | 
|---|
| 100 | XPTR_NULL,     // mapper_xp unused | 
|---|
| 101 | local_cxy ); | 
|---|
| 102 |  | 
|---|
| 103 | if( vseg_kentry == NULL ) | 
|---|
| 104 | { | 
|---|
| 105 | printk("\n[ERROR] in %s : cannot register kentry vseg\n", __FUNCTION__ ); | 
|---|
| 106 | return -1; | 
|---|
| 107 | } | 
|---|
| 108 |  | 
|---|
| 109 | vmm->kent_vpn_base = base; | 
|---|
| 110 |  | 
|---|
| 111 | // register args vseg in VSL | 
|---|
| 112 | base = (CONFIG_VMM_KENTRY_BASE + | 
|---|
| 113 | CONFIG_VMM_KENTRY_SIZE ) << CONFIG_PPM_PAGE_SHIFT; | 
|---|
| 114 | size = CONFIG_VMM_ARGS_SIZE << CONFIG_PPM_PAGE_SHIFT; | 
|---|
| 115 |  | 
|---|
| 116 | vseg_args = vmm_create_vseg( process, | 
|---|
| 117 | VSEG_TYPE_DATA, | 
|---|
| 118 | base, | 
|---|
| 119 | size, | 
|---|
| 120 | 0,             // file_offset unused | 
|---|
| 121 | 0,             // file_size unused | 
|---|
| 122 | XPTR_NULL,     // mapper_xp unused | 
|---|
| 123 | local_cxy ); | 
|---|
| 124 |  | 
|---|
| 125 | if( vseg_args == NULL ) | 
|---|
| 126 | { | 
|---|
| 127 | printk("\n[ERROR] in %s : cannot register args vseg\n", __FUNCTION__ ); | 
|---|
| 128 | return -1; | 
|---|
| 129 | } | 
|---|
| 130 |  | 
|---|
| 131 | vmm->args_vpn_base = base; | 
|---|
| 132 |  | 
|---|
| 133 | // register the envs vseg in VSL | 
|---|
| 134 | base = (CONFIG_VMM_KENTRY_BASE + | 
|---|
| 135 | CONFIG_VMM_KENTRY_SIZE + | 
|---|
| 136 | CONFIG_VMM_ARGS_SIZE   ) << CONFIG_PPM_PAGE_SHIFT; | 
|---|
| 137 | size = CONFIG_VMM_ENVS_SIZE << CONFIG_PPM_PAGE_SHIFT; | 
|---|
| 138 |  | 
|---|
| 139 | vseg_envs = vmm_create_vseg( process, | 
|---|
| 140 | VSEG_TYPE_DATA, | 
|---|
| 141 | base, | 
|---|
| 142 | size, | 
|---|
| 143 | 0,             // file_offset unused | 
|---|
| 144 | 0,             // file_size unused | 
|---|
| 145 | XPTR_NULL,     // mapper_xp unused | 
|---|
| 146 | local_cxy ); | 
|---|
| 147 |  | 
|---|
| 148 | if( vseg_envs == NULL ) | 
|---|
| 149 | { | 
|---|
| 150 | printk("\n[ERROR] in %s : cannot register envs vseg\n", __FUNCTION__ ); | 
|---|
| 151 | return -1; | 
|---|
| 152 | } | 
|---|
| 153 |  | 
|---|
| 154 | vmm->envs_vpn_base = base; | 
|---|
| 155 |  | 
|---|
| 156 | // create GPT (empty) | 
|---|
| 157 | error = hal_gpt_create( &vmm->gpt ); | 
|---|
| 158 |  | 
|---|
| 159 | if( error ) | 
|---|
| 160 | printk("\n[ERROR] in %s : cannot create GPT\n", __FUNCTION__ ); | 
|---|
| 161 |  | 
|---|
| 162 | // initialize GPT (architecture specic) | 
|---|
| 163 | // (For TSAR, identity map the kentry_vseg) | 
|---|
| 164 | error = hal_vmm_init( vmm ); | 
|---|
| 165 |  | 
|---|
| 166 | if( error ) | 
|---|
| 167 | printk("\n[ERROR] in %s : cannot initialize GPT\n", __FUNCTION__ ); | 
|---|
| 168 |  | 
|---|
| 169 | // initialize STACK allocator | 
|---|
| 170 | vmm->stack_mgr.bitmap   = 0; | 
|---|
| 171 | vmm->stack_mgr.vpn_base = CONFIG_VMM_STACK_BASE; | 
|---|
| 172 |  | 
|---|
| 173 | // initialize MMAP allocator | 
|---|
| 174 | vmm->mmap_mgr.vpn_base        = CONFIG_VMM_HEAP_BASE; | 
|---|
| 175 | vmm->mmap_mgr.vpn_size        = CONFIG_VMM_STACK_BASE - CONFIG_VMM_HEAP_BASE; | 
|---|
| 176 | vmm->mmap_mgr.first_free_vpn  = CONFIG_VMM_HEAP_BASE; | 
|---|
| 177 | uint32_t i; | 
|---|
| 178 | for( i = 0 ; i < 32 ; i++ ) list_root_init( &vmm->mmap_mgr.zombi_list[i] ); | 
|---|
| 179 |  | 
|---|
| 180 | // initialize instrumentation counters | 
|---|
| 181 | vmm->pgfault_nr = 0; | 
|---|
| 182 |  | 
|---|
| 183 | hal_fence(); | 
|---|
| 184 |  | 
|---|
| 185 | #if CONFIG_DEBUG_VMM_INIT | 
|---|
| 186 | cycle = (uint32_t)hal_get_cycles(); | 
|---|
| 187 | if( CONFIG_DEBUG_VMM_INIT ) | 
|---|
| 188 | printk("\n[DBG] %s : thread %x exit for process %x / entry_point = %x / cycle %d\n", | 
|---|
| 189 | __FUNCTION__ , CURRENT_THREAD , process->pid , process->vmm.entry_point , cycle ); | 
|---|
| 190 | #endif | 
|---|
| 191 |  | 
|---|
| 192 | return 0; | 
|---|
| 193 |  | 
|---|
| 194 | }  // end vmm_init() | 
|---|
| 195 |  | 
|---|
| 196 | ////////////////////////////////////// | 
|---|
| 197 | void vmm_display( process_t * process, | 
|---|
| 198 | bool_t      mapping ) | 
|---|
| 199 | { | 
|---|
| 200 | assert( (process->ref_xp == XPTR( local_cxy , process )) , __FUNCTION__, | 
|---|
| 201 | "this function must be executed in reference cluster" ); | 
|---|
| 202 |  | 
|---|
| 203 | vmm_t * vmm = &process->vmm; | 
|---|
| 204 | gpt_t * gpt = &vmm->gpt; | 
|---|
| 205 |  | 
|---|
| 206 | printk("\n***** VSL and GPT for process %x\n\n", | 
|---|
| 207 | process->pid ); | 
|---|
| 208 |  | 
|---|
| 209 | // get lock protecting the vseg list | 
|---|
| 210 | remote_rwlock_rd_lock( XPTR( local_cxy , &vmm->vsegs_lock ) ); | 
|---|
| 211 |  | 
|---|
| 212 | // scan the list of vsegs | 
|---|
| 213 | xptr_t         root_xp = XPTR( local_cxy , &vmm->vsegs_root ); | 
|---|
| 214 | xptr_t         iter_xp; | 
|---|
| 215 | xptr_t         vseg_xp; | 
|---|
| 216 | vseg_t       * vseg; | 
|---|
| 217 | XLIST_FOREACH( root_xp , iter_xp ) | 
|---|
| 218 | { | 
|---|
| 219 | vseg_xp = XLIST_ELEMENT( iter_xp , vseg_t , xlist ); | 
|---|
| 220 | vseg    = GET_PTR( vseg_xp ); | 
|---|
| 221 |  | 
|---|
| 222 | printk(" - %s : base = %X / size = %X / npages = %d\n", | 
|---|
| 223 | vseg_type_str( vseg->type ) , vseg->min , vseg->max - vseg->min , vseg->vpn_size ); | 
|---|
| 224 |  | 
|---|
| 225 | if( mapping ) | 
|---|
| 226 | { | 
|---|
| 227 | vpn_t    vpn; | 
|---|
| 228 | ppn_t    ppn; | 
|---|
| 229 | uint32_t attr; | 
|---|
| 230 | vpn_t    base = vseg->vpn_base; | 
|---|
| 231 | vpn_t    size = vseg->vpn_size; | 
|---|
| 232 | for( vpn = base ; vpn < (base+size) ; vpn++ ) | 
|---|
| 233 | { | 
|---|
| 234 | hal_gpt_get_pte( gpt , vpn , &attr , &ppn ); | 
|---|
| 235 | if( attr & GPT_MAPPED ) | 
|---|
| 236 | { | 
|---|
| 237 | printk("    . vpn = %X / attr = %X / ppn = %X\n", vpn , attr , ppn ); | 
|---|
| 238 | } | 
|---|
| 239 | } | 
|---|
| 240 | } | 
|---|
| 241 | } | 
|---|
| 242 |  | 
|---|
| 243 | // release the lock | 
|---|
| 244 | remote_rwlock_rd_unlock( XPTR( local_cxy , &vmm->vsegs_lock ) ); | 
|---|
| 245 |  | 
|---|
| 246 | }  // vmm_display() | 
|---|
| 247 |  | 
|---|
| 248 | /////////////////////i////////////////////////// | 
|---|
| 249 | void vmm_global_update_pte( process_t * process, | 
|---|
| 250 | vpn_t       vpn, | 
|---|
| 251 | uint32_t    attr, | 
|---|
| 252 | ppn_t       ppn ) | 
|---|
| 253 | { | 
|---|
| 254 |  | 
|---|
| 255 | xlist_entry_t * process_root_ptr; | 
|---|
| 256 | xptr_t          process_root_xp; | 
|---|
| 257 | xptr_t          process_iter_xp; | 
|---|
| 258 |  | 
|---|
| 259 | xptr_t          remote_process_xp; | 
|---|
| 260 | cxy_t           remote_process_cxy; | 
|---|
| 261 | process_t     * remote_process_ptr; | 
|---|
| 262 | xptr_t          remote_gpt_xp; | 
|---|
| 263 |  | 
|---|
| 264 | pid_t           pid; | 
|---|
| 265 | cxy_t           owner_cxy; | 
|---|
| 266 | lpid_t          owner_lpid; | 
|---|
| 267 |  | 
|---|
| 268 | #if CONFIG_DEBUG_VMM_UPDATE_PTE | 
|---|
| 269 | uint32_t cycle = (uint32_t)hal_get_cycles(); | 
|---|
| 270 | if( CONFIG_DEBUG_VMM_UPDATE_PTE < cycle ) | 
|---|
| 271 | printk("\n[DBG] %s : thread %x enter for process %x / vpn %x / cycle %d\n", | 
|---|
| 272 | __FUNCTION__ , CURRENT_THREAD , process->pid , vpn , cycle ); | 
|---|
| 273 | #endif | 
|---|
| 274 |  | 
|---|
| 275 | // check cluster is reference | 
|---|
| 276 | assert( (GET_CXY( process->ref_xp ) == local_cxy) , __FUNCTION__, | 
|---|
| 277 | "not called in reference cluster\n"); | 
|---|
| 278 |  | 
|---|
| 279 | // get extended pointer on root of process copies xlist in owner cluster | 
|---|
| 280 | pid              = process->pid; | 
|---|
| 281 | owner_cxy        = CXY_FROM_PID( pid ); | 
|---|
| 282 | owner_lpid       = LPID_FROM_PID( pid ); | 
|---|
| 283 | process_root_ptr = &LOCAL_CLUSTER->pmgr.copies_root[owner_lpid]; | 
|---|
| 284 | process_root_xp  = XPTR( owner_cxy , process_root_ptr ); | 
|---|
| 285 |  | 
|---|
| 286 | // loop on destination process copies | 
|---|
| 287 | XLIST_FOREACH( process_root_xp , process_iter_xp ) | 
|---|
| 288 | { | 
|---|
| 289 | // get cluster and local pointer on remote process | 
|---|
| 290 | remote_process_xp  = XLIST_ELEMENT( process_iter_xp , process_t , copies_list ); | 
|---|
| 291 | remote_process_ptr = GET_PTR( remote_process_xp ); | 
|---|
| 292 | remote_process_cxy = GET_CXY( remote_process_xp ); | 
|---|
| 293 |  | 
|---|
| 294 | #if (CONFIG_DEBUG_VMM_UPDATE_PTE & 0x1) | 
|---|
| 295 | if( CONFIG_DEBUG_VMM_UPDATE_PTE < cycle ) | 
|---|
| 296 | printk("\n[DBG] %s : thread %x handling process %x in cluster %x\n", | 
|---|
| 297 | __FUNCTION__ , CURRENT_THREAD , process->pid , remote_process_cxy ); | 
|---|
| 298 | #endif | 
|---|
| 299 |  | 
|---|
| 300 | // get extended pointer on remote gpt | 
|---|
| 301 | remote_gpt_xp = XPTR( remote_process_cxy , &remote_process_ptr->vmm.gpt ); | 
|---|
| 302 |  | 
|---|
| 303 | // update remote GPT | 
|---|
| 304 | hal_gpt_update_pte( remote_gpt_xp, vpn, attr, ppn ); | 
|---|
| 305 | } | 
|---|
| 306 |  | 
|---|
| 307 | #if CONFIG_DEBUG_VMM_UPDATE_PTE | 
|---|
| 308 | cycle = (uint32_t)hal_get_cycles(); | 
|---|
| 309 | if( CONFIG_DEBUG_VMM_UPDATE_PTE < cycle ) | 
|---|
| 310 | printk("\n[DBG] %s : thread %x exit for process %x / vpn %x / cycle %d\n", | 
|---|
| 311 | __FUNCTION__ , CURRENT_THREAD , process->pid , vpn , cycle ); | 
|---|
| 312 | #endif | 
|---|
| 313 |  | 
|---|
| 314 | }  // end vmm_global_update_pte() | 
|---|
| 315 |  | 
|---|
| 316 | /////////////////////////////////////// | 
|---|
| 317 | void vmm_set_cow( process_t * process ) | 
|---|
| 318 | { | 
|---|
| 319 | vmm_t         * vmm; | 
|---|
| 320 |  | 
|---|
| 321 | xlist_entry_t * process_root_ptr; | 
|---|
| 322 | xptr_t          process_root_xp; | 
|---|
| 323 | xptr_t          process_iter_xp; | 
|---|
| 324 |  | 
|---|
| 325 | xptr_t          remote_process_xp; | 
|---|
| 326 | cxy_t           remote_process_cxy; | 
|---|
| 327 | process_t     * remote_process_ptr; | 
|---|
| 328 | xptr_t          remote_gpt_xp; | 
|---|
| 329 |  | 
|---|
| 330 | xptr_t          vseg_root_xp; | 
|---|
| 331 | xptr_t          vseg_iter_xp; | 
|---|
| 332 |  | 
|---|
| 333 | xptr_t          vseg_xp; | 
|---|
| 334 | vseg_t        * vseg; | 
|---|
| 335 |  | 
|---|
| 336 | pid_t           pid; | 
|---|
| 337 | cxy_t           owner_cxy; | 
|---|
| 338 | lpid_t          owner_lpid; | 
|---|
| 339 |  | 
|---|
| 340 | #if CONFIG_DEBUG_VMM_SET_COW | 
|---|
| 341 | uint32_t cycle = (uint32_t)hal_get_cycles(); | 
|---|
| 342 | if( CONFIG_DEBUG_VMM_SET_COW < cycle ) | 
|---|
| 343 | printk("\n[DBG] %s : thread %x enter for process %x / cycle %d\n", | 
|---|
| 344 | __FUNCTION__ , CURRENT_THREAD , process->pid , cycle ); | 
|---|
| 345 | #endif | 
|---|
| 346 |  | 
|---|
| 347 | // check cluster is reference | 
|---|
| 348 | assert( (GET_CXY( process->ref_xp ) == local_cxy) , __FUNCTION__, | 
|---|
| 349 | "local cluster is not process reference cluster\n"); | 
|---|
| 350 |  | 
|---|
| 351 | // get pointer on reference VMM | 
|---|
| 352 | vmm = &process->vmm; | 
|---|
| 353 |  | 
|---|
| 354 | // get extended pointer on root of process copies xlist in owner cluster | 
|---|
| 355 | pid              = process->pid; | 
|---|
| 356 | owner_cxy        = CXY_FROM_PID( pid ); | 
|---|
| 357 | owner_lpid       = LPID_FROM_PID( pid ); | 
|---|
| 358 | process_root_ptr = &LOCAL_CLUSTER->pmgr.copies_root[owner_lpid]; | 
|---|
| 359 | process_root_xp  = XPTR( owner_cxy , process_root_ptr ); | 
|---|
| 360 |  | 
|---|
| 361 | // get extended pointer on root of vsegs xlist from reference VMM | 
|---|
| 362 | vseg_root_xp  = XPTR( local_cxy , &vmm->vsegs_root ); | 
|---|
| 363 |  | 
|---|
| 364 | // loop on destination process copies | 
|---|
| 365 | XLIST_FOREACH( process_root_xp , process_iter_xp ) | 
|---|
| 366 | { | 
|---|
| 367 | // get cluster and local pointer on remote process | 
|---|
| 368 | remote_process_xp  = XLIST_ELEMENT( process_iter_xp , process_t , copies_list ); | 
|---|
| 369 | remote_process_ptr = GET_PTR( remote_process_xp ); | 
|---|
| 370 | remote_process_cxy = GET_CXY( remote_process_xp ); | 
|---|
| 371 |  | 
|---|
| 372 | #if (CONFIG_DEBUG_VMM_SET_COW &0x1) | 
|---|
| 373 | if( CONFIG_DEBUG_VMM_SET_COW < cycle ) | 
|---|
| 374 | printk("\n[DBG] %s : thread %x handling process %x in cluster %x\n", | 
|---|
| 375 | __FUNCTION__ , CURRENT_THREAD , process->pid , remote_process_cxy ); | 
|---|
| 376 | #endif | 
|---|
| 377 |  | 
|---|
| 378 | // get extended pointer on remote gpt | 
|---|
| 379 | remote_gpt_xp = XPTR( remote_process_cxy , &remote_process_ptr->vmm.gpt ); | 
|---|
| 380 |  | 
|---|
| 381 | // loop on vsegs in (local) reference process VSL | 
|---|
| 382 | XLIST_FOREACH( vseg_root_xp , vseg_iter_xp ) | 
|---|
| 383 | { | 
|---|
| 384 | // get pointer on vseg | 
|---|
| 385 | vseg_xp  = XLIST_ELEMENT( vseg_iter_xp , vseg_t , xlist ); | 
|---|
| 386 | vseg     = GET_PTR( vseg_xp ); | 
|---|
| 387 |  | 
|---|
| 388 | assert( (GET_CXY( vseg_xp ) == local_cxy) , __FUNCTION__, | 
|---|
| 389 | "all vsegs in reference VSL must be local\n" ); | 
|---|
| 390 |  | 
|---|
| 391 | // get vseg type, base and size | 
|---|
| 392 | uint32_t type     = vseg->type; | 
|---|
| 393 | vpn_t    vpn_base = vseg->vpn_base; | 
|---|
| 394 | vpn_t    vpn_size = vseg->vpn_size; | 
|---|
| 395 |  | 
|---|
| 396 | #if (CONFIG_DEBUG_VMM_SET_COW & 0x1) | 
|---|
| 397 | if( CONFIG_DEBUG_VMM_SET_COW < cycle ) | 
|---|
| 398 | printk("\n[DBG] %s : thread %x handling vseg %s / vpn_base = %x / vpn_size = %x\n", | 
|---|
| 399 | __FUNCTION__, CURRENT_THREAD , vseg_type_str(type), vpn_base, vpn_size ); | 
|---|
| 400 | #endif | 
|---|
| 401 | // only DATA, ANON and REMOTE vsegs | 
|---|
| 402 | if( (type == VSEG_TYPE_DATA)  || | 
|---|
| 403 | (type == VSEG_TYPE_ANON)  || | 
|---|
| 404 | (type == VSEG_TYPE_REMOTE) ) | 
|---|
| 405 | { | 
|---|
| 406 | vpn_t      vpn; | 
|---|
| 407 | uint32_t   attr; | 
|---|
| 408 | ppn_t      ppn; | 
|---|
| 409 | xptr_t     page_xp; | 
|---|
| 410 | cxy_t      page_cxy; | 
|---|
| 411 | page_t   * page_ptr; | 
|---|
| 412 | xptr_t     forks_xp; | 
|---|
| 413 |  | 
|---|
| 414 | // update flags in remote GPT | 
|---|
| 415 | hal_gpt_set_cow( remote_gpt_xp, | 
|---|
| 416 | vpn_base, | 
|---|
| 417 | vpn_size ); | 
|---|
| 418 |  | 
|---|
| 419 | // atomically increment pending forks counter in physical pages, | 
|---|
| 420 | // for all vseg pages that are mapped in reference cluster | 
|---|
| 421 | if( remote_process_cxy == local_cxy ) | 
|---|
| 422 | { | 
|---|
| 423 | // the reference GPT is the local GPT | 
|---|
| 424 | gpt_t * gpt = GET_PTR( remote_gpt_xp ); | 
|---|
| 425 |  | 
|---|
| 426 | // scan all pages in vseg | 
|---|
| 427 | for( vpn = vpn_base ; vpn < (vpn_base + vpn_size) ; vpn++ ) | 
|---|
| 428 | { | 
|---|
| 429 | // get page attributes and PPN from reference GPT | 
|---|
| 430 | hal_gpt_get_pte( gpt , vpn , &attr , &ppn ); | 
|---|
| 431 |  | 
|---|
| 432 | // atomically update pending forks counter if page is mapped | 
|---|
| 433 | if( attr & GPT_MAPPED ) | 
|---|
| 434 | { | 
|---|
| 435 | page_xp  = ppm_ppn2page( ppn ); | 
|---|
| 436 | page_cxy = GET_CXY( page_xp ); | 
|---|
| 437 | page_ptr = GET_PTR( page_xp ); | 
|---|
| 438 | forks_xp = XPTR( page_cxy , &page_ptr->forks ); | 
|---|
| 439 | hal_remote_atomic_add( forks_xp , 1 ); | 
|---|
| 440 | } | 
|---|
| 441 | }   // end loop on vpn | 
|---|
| 442 | }   // end if local | 
|---|
| 443 | }   // end if vseg type | 
|---|
| 444 | }   // end loop on vsegs | 
|---|
| 445 | }   // end loop on process copies | 
|---|
| 446 |  | 
|---|
| 447 | #if CONFIG_DEBUG_VMM_SET_COW | 
|---|
| 448 | cycle = (uint32_t)hal_get_cycles(); | 
|---|
| 449 | if( CONFIG_DEBUG_VMM_SET_COW < cycle ) | 
|---|
| 450 | printk("\n[DBG] %s : thread %x exit for process %x / cycle %d\n", | 
|---|
| 451 | __FUNCTION__ , CURRENT_THREAD , process->pid , cycle ); | 
|---|
| 452 | #endif | 
|---|
| 453 |  | 
|---|
| 454 | }  // end vmm_set-cow() | 
|---|
| 455 |  | 
|---|
| 456 | ///////////////////////////////////////////////// | 
|---|
| 457 | error_t vmm_fork_copy( process_t * child_process, | 
|---|
| 458 | xptr_t      parent_process_xp ) | 
|---|
| 459 | { | 
|---|
| 460 | error_t     error; | 
|---|
| 461 | cxy_t       parent_cxy; | 
|---|
| 462 | process_t * parent_process; | 
|---|
| 463 | vmm_t     * parent_vmm; | 
|---|
| 464 | xptr_t      parent_lock_xp; | 
|---|
| 465 | vmm_t     * child_vmm; | 
|---|
| 466 | xptr_t      iter_xp; | 
|---|
| 467 | xptr_t      parent_vseg_xp; | 
|---|
| 468 | vseg_t    * parent_vseg; | 
|---|
| 469 | vseg_t    * child_vseg; | 
|---|
| 470 | uint32_t    type; | 
|---|
| 471 | bool_t      cow; | 
|---|
| 472 | vpn_t       vpn; | 
|---|
| 473 | vpn_t       vpn_base; | 
|---|
| 474 | vpn_t       vpn_size; | 
|---|
| 475 | xptr_t      page_xp; | 
|---|
| 476 | page_t    * page_ptr; | 
|---|
| 477 | cxy_t       page_cxy; | 
|---|
| 478 | xptr_t      parent_root_xp; | 
|---|
| 479 | bool_t      mapped; | 
|---|
| 480 | ppn_t       ppn; | 
|---|
| 481 |  | 
|---|
| 482 | #if CONFIG_DEBUG_VMM_FORK_COPY | 
|---|
| 483 | uint32_t cycle = (uint32_t)hal_get_cycles(); | 
|---|
| 484 | if( CONFIG_DEBUG_VMM_FORK_COPY < cycle ) | 
|---|
| 485 | printk("\n[DBG] %s : thread %x enter / cycle %d\n", | 
|---|
| 486 | __FUNCTION__ , CURRENT_THREAD, cycle ); | 
|---|
| 487 | #endif | 
|---|
| 488 |  | 
|---|
| 489 | // get parent process cluster and local pointer | 
|---|
| 490 | parent_cxy     = GET_CXY( parent_process_xp ); | 
|---|
| 491 | parent_process = GET_PTR( parent_process_xp ); | 
|---|
| 492 |  | 
|---|
| 493 | // get local pointers on parent and child VMM | 
|---|
| 494 | parent_vmm = &parent_process->vmm; | 
|---|
| 495 | child_vmm  = &child_process->vmm; | 
|---|
| 496 |  | 
|---|
| 497 | // get extended pointer on lock protecting the parent VSL | 
|---|
| 498 | parent_lock_xp = XPTR( parent_cxy , &parent_vmm->vsegs_lock ); | 
|---|
| 499 |  | 
|---|
| 500 | // initialize the lock protecting the child VSL | 
|---|
| 501 | remote_rwlock_init( XPTR( local_cxy , &child_vmm->vsegs_lock ) ); | 
|---|
| 502 |  | 
|---|
| 503 | // initialize the child VSL as empty | 
|---|
| 504 | xlist_root_init( XPTR( local_cxy, &child_vmm->vsegs_root ) ); | 
|---|
| 505 | child_vmm->vsegs_nr = 0; | 
|---|
| 506 |  | 
|---|
| 507 | // create child GPT | 
|---|
| 508 | error = hal_gpt_create( &child_vmm->gpt ); | 
|---|
| 509 |  | 
|---|
| 510 | if( error ) | 
|---|
| 511 | { | 
|---|
| 512 | printk("\n[ERROR] in %s : cannot create GPT\n", __FUNCTION__ ); | 
|---|
| 513 | return -1; | 
|---|
| 514 | } | 
|---|
| 515 |  | 
|---|
| 516 | // build extended pointer on parent VSL | 
|---|
| 517 | parent_root_xp = XPTR( parent_cxy , &parent_vmm->vsegs_root ); | 
|---|
| 518 |  | 
|---|
| 519 | // take the lock protecting the parent VSL | 
|---|
| 520 | remote_rwlock_rd_lock( parent_lock_xp ); | 
|---|
| 521 |  | 
|---|
| 522 | // loop on parent VSL xlist | 
|---|
| 523 | XLIST_FOREACH( parent_root_xp , iter_xp ) | 
|---|
| 524 | { | 
|---|
| 525 | // get local and extended pointers on current parent vseg | 
|---|
| 526 | parent_vseg_xp = XLIST_ELEMENT( iter_xp , vseg_t , xlist ); | 
|---|
| 527 | parent_vseg    = GET_PTR( parent_vseg_xp ); | 
|---|
| 528 |  | 
|---|
| 529 | // get vseg type | 
|---|
| 530 | type = hal_remote_lw( XPTR( parent_cxy , &parent_vseg->type ) ); | 
|---|
| 531 |  | 
|---|
| 532 | #if CONFIG_DEBUG_VMM_FORK_COPY | 
|---|
| 533 | cycle = (uint32_t)hal_get_cycles(); | 
|---|
| 534 | if( CONFIG_DEBUG_VMM_FORK_COPY < cycle ) | 
|---|
| 535 | printk("\n[DBG] %s : thread %x found parent vseg %s / vpn_base = %x / cycle %d\n", | 
|---|
| 536 | __FUNCTION__ , CURRENT_THREAD, vseg_type_str(type), | 
|---|
| 537 | hal_remote_lw( XPTR( parent_cxy , &parent_vseg->vpn_base ) ) , cycle ); | 
|---|
| 538 | #endif | 
|---|
| 539 |  | 
|---|
| 540 | // all parent vsegs - but STACK - must be copied in child VSL | 
|---|
| 541 | if( type != VSEG_TYPE_STACK ) | 
|---|
| 542 | { | 
|---|
| 543 | // allocate memory for a new child vseg | 
|---|
| 544 | child_vseg = vseg_alloc(); | 
|---|
| 545 | if( child_vseg == NULL )   // release all allocated vsegs | 
|---|
| 546 | { | 
|---|
| 547 | vmm_destroy( child_process ); | 
|---|
| 548 | printk("\n[ERROR] in %s : cannot create vseg for child\n", __FUNCTION__ ); | 
|---|
| 549 | return -1; | 
|---|
| 550 | } | 
|---|
| 551 |  | 
|---|
| 552 | // copy parent vseg to child vseg | 
|---|
| 553 | vseg_init_from_ref( child_vseg , parent_vseg_xp ); | 
|---|
| 554 |  | 
|---|
| 555 | // register child vseg in child VSL | 
|---|
| 556 | vseg_attach( child_vmm , child_vseg ); | 
|---|
| 557 |  | 
|---|
| 558 | #if CONFIG_DEBUG_VMM_FORK_COPY | 
|---|
| 559 | cycle = (uint32_t)hal_get_cycles(); | 
|---|
| 560 | if( CONFIG_DEBUG_VMM_FORK_COPY < cycle ) | 
|---|
| 561 | printk("\n[DBG] %s : thread %x copied vseg %s / vpn_base = %x to child VSL / cycle %d\n", | 
|---|
| 562 | __FUNCTION__ , CURRENT_THREAD , vseg_type_str(type), | 
|---|
| 563 | hal_remote_lw( XPTR( parent_cxy , &parent_vseg->vpn_base ) ) , cycle ); | 
|---|
| 564 | #endif | 
|---|
| 565 |  | 
|---|
| 566 | // copy DATA, MMAP, REMOTE, FILE parent GPT entries to child GPT | 
|---|
| 567 | if( type != VSEG_TYPE_CODE ) | 
|---|
| 568 | { | 
|---|
| 569 | // activate the COW for DATA, MMAP, REMOTE vsegs only | 
|---|
| 570 | cow = ( type != VSEG_TYPE_FILE ); | 
|---|
| 571 |  | 
|---|
| 572 | vpn_base = child_vseg->vpn_base; | 
|---|
| 573 | vpn_size = child_vseg->vpn_size; | 
|---|
| 574 |  | 
|---|
| 575 | // scan pages in parent vseg | 
|---|
| 576 | for( vpn = vpn_base ; vpn < (vpn_base + vpn_size) ; vpn++ ) | 
|---|
| 577 | { | 
|---|
| 578 | error = hal_gpt_pte_copy( &child_vmm->gpt, | 
|---|
| 579 | XPTR( parent_cxy , &parent_vmm->gpt ), | 
|---|
| 580 | vpn, | 
|---|
| 581 | cow, | 
|---|
| 582 | &ppn, | 
|---|
| 583 | &mapped ); | 
|---|
| 584 | if( error ) | 
|---|
| 585 | { | 
|---|
| 586 | vmm_destroy( child_process ); | 
|---|
| 587 | printk("\n[ERROR] in %s : cannot copy GPT\n", __FUNCTION__ ); | 
|---|
| 588 | return -1; | 
|---|
| 589 | } | 
|---|
| 590 |  | 
|---|
| 591 | // increment pending forks counter in page if mapped | 
|---|
| 592 | if( mapped ) | 
|---|
| 593 | { | 
|---|
| 594 | page_xp = ppm_ppn2page( ppn ); | 
|---|
| 595 | page_cxy = GET_CXY( page_xp ); | 
|---|
| 596 | page_ptr = GET_PTR( page_xp ); | 
|---|
| 597 | hal_remote_atomic_add( XPTR( page_cxy , &page_ptr->forks ) , 1 ); | 
|---|
| 598 |  | 
|---|
| 599 | #if CONFIG_DEBUG_VMM_FORK_COPY | 
|---|
| 600 | cycle = (uint32_t)hal_get_cycles(); | 
|---|
| 601 | if( CONFIG_DEBUG_VMM_FORK_COPY < cycle ) | 
|---|
| 602 | printk("\n[DBG] %s : thread %x copied vpn %x to child GPT / cycle %d\n", | 
|---|
| 603 | __FUNCTION__ , CURRENT_THREAD , vpn , cycle ); | 
|---|
| 604 | #endif | 
|---|
| 605 |  | 
|---|
| 606 | } | 
|---|
| 607 | } | 
|---|
| 608 | }   // end if no code & no stack | 
|---|
| 609 | }   // end if no stack | 
|---|
| 610 | }   // end loop on vsegs | 
|---|
| 611 |  | 
|---|
| 612 | // release the parent vsegs lock | 
|---|
| 613 | remote_rwlock_rd_unlock( parent_lock_xp ); | 
|---|
| 614 |  | 
|---|
| 615 | // initialize child GPT (architecture specic) | 
|---|
| 616 | // => For TSAR, identity map the kentry_vseg | 
|---|
| 617 | error = hal_vmm_init( child_vmm ); | 
|---|
| 618 |  | 
|---|
| 619 | if( error ) | 
|---|
| 620 | { | 
|---|
| 621 | printk("\n[ERROR] in %s : cannot create GPT\n", __FUNCTION__ ); | 
|---|
| 622 | return -1; | 
|---|
| 623 | } | 
|---|
| 624 |  | 
|---|
| 625 | // initialize the child VMM STACK allocator | 
|---|
| 626 | child_vmm->stack_mgr.bitmap   = 0; | 
|---|
| 627 | child_vmm->stack_mgr.vpn_base = CONFIG_VMM_STACK_BASE; | 
|---|
| 628 |  | 
|---|
| 629 | // initialize the child VMM MMAP allocator | 
|---|
| 630 | uint32_t i; | 
|---|
| 631 | child_vmm->mmap_mgr.vpn_base        = CONFIG_VMM_HEAP_BASE; | 
|---|
| 632 | child_vmm->mmap_mgr.vpn_size        = CONFIG_VMM_STACK_BASE - CONFIG_VMM_HEAP_BASE; | 
|---|
| 633 | child_vmm->mmap_mgr.first_free_vpn  = CONFIG_VMM_HEAP_BASE; | 
|---|
| 634 | for( i = 0 ; i < 32 ; i++ ) list_root_init( &child_vmm->mmap_mgr.zombi_list[i] ); | 
|---|
| 635 |  | 
|---|
| 636 | // initialize instrumentation counters | 
|---|
| 637 | child_vmm->pgfault_nr    = 0; | 
|---|
| 638 |  | 
|---|
| 639 | // copy base addresses from parent VMM to child VMM | 
|---|
| 640 | child_vmm->kent_vpn_base = (vpn_t)hal_remote_lpt(XPTR(parent_cxy, &parent_vmm->kent_vpn_base)); | 
|---|
| 641 | child_vmm->args_vpn_base = (vpn_t)hal_remote_lpt(XPTR(parent_cxy, &parent_vmm->args_vpn_base)); | 
|---|
| 642 | child_vmm->envs_vpn_base = (vpn_t)hal_remote_lpt(XPTR(parent_cxy, &parent_vmm->envs_vpn_base)); | 
|---|
| 643 | child_vmm->heap_vpn_base = (vpn_t)hal_remote_lpt(XPTR(parent_cxy, &parent_vmm->heap_vpn_base)); | 
|---|
| 644 | child_vmm->code_vpn_base = (vpn_t)hal_remote_lpt(XPTR(parent_cxy, &parent_vmm->code_vpn_base)); | 
|---|
| 645 | child_vmm->data_vpn_base = (vpn_t)hal_remote_lpt(XPTR(parent_cxy, &parent_vmm->data_vpn_base)); | 
|---|
| 646 |  | 
|---|
| 647 | child_vmm->entry_point = (intptr_t)hal_remote_lpt(XPTR(parent_cxy, &parent_vmm->entry_point)); | 
|---|
| 648 |  | 
|---|
| 649 | hal_fence(); | 
|---|
| 650 |  | 
|---|
| 651 | #if CONFIG_DEBUG_VMM_FORK_COPY | 
|---|
| 652 | cycle = (uint32_t)hal_get_cycles(); | 
|---|
| 653 | if( CONFIG_DEBUG_VMM_FORK_COPY < cycle ) | 
|---|
| 654 | printk("\n[DBG] %s : thread %x exit successfully / cycle %d\n", | 
|---|
| 655 | __FUNCTION__ , CURRENT_THREAD , cycle ); | 
|---|
| 656 | #endif | 
|---|
| 657 |  | 
|---|
| 658 | return 0; | 
|---|
| 659 |  | 
|---|
| 660 | }  // vmm_fork_copy() | 
|---|
| 661 |  | 
|---|
| 662 | /////////////////////////////////////// | 
|---|
| 663 | void vmm_destroy( process_t * process ) | 
|---|
| 664 | { | 
|---|
| 665 | xptr_t   vseg_xp; | 
|---|
| 666 | vseg_t * vseg; | 
|---|
| 667 |  | 
|---|
| 668 | #if CONFIG_DEBUG_VMM_DESTROY | 
|---|
| 669 | uint32_t cycle = (uint32_t)hal_get_cycles(); | 
|---|
| 670 | if( CONFIG_DEBUG_VMM_DESTROY < cycle ) | 
|---|
| 671 | printk("\n[DBG] %s : thread %x enter for process %x / cycle %d\n", | 
|---|
| 672 | __FUNCTION__ , CURRENT_THREAD , process->pid , cycle ); | 
|---|
| 673 | #endif | 
|---|
| 674 |  | 
|---|
| 675 | #if (CONFIG_DEBUG_VMM_DESTROY & 1 ) | 
|---|
| 676 | vmm_display( process , true ); | 
|---|
| 677 | #endif | 
|---|
| 678 |  | 
|---|
| 679 | // get pointer on local VMM | 
|---|
| 680 | vmm_t  * vmm = &process->vmm; | 
|---|
| 681 |  | 
|---|
| 682 | // get extended pointer on VSL root and VSL lock | 
|---|
| 683 | xptr_t   root_xp = XPTR( local_cxy , &vmm->vsegs_root ); | 
|---|
| 684 | xptr_t   lock_xp = XPTR( local_cxy , &vmm->vsegs_lock ); | 
|---|
| 685 |  | 
|---|
| 686 | // get lock protecting vseg list | 
|---|
| 687 | remote_rwlock_wr_lock( lock_xp ); | 
|---|
| 688 |  | 
|---|
| 689 | // remove all user vsegs registered in VSL | 
|---|
| 690 | while( !xlist_is_empty( root_xp ) ) | 
|---|
| 691 | { | 
|---|
| 692 | // get pointer on first vseg in VSL | 
|---|
| 693 | vseg_xp = XLIST_FIRST_ELEMENT( root_xp , vseg_t , xlist ); | 
|---|
| 694 | vseg    = GET_PTR( vseg_xp ); | 
|---|
| 695 |  | 
|---|
| 696 | #if( CONFIG_DEBUG_VMM_DESTROY & 1 ) | 
|---|
| 697 | if( CONFIG_DEBUG_VMM_DESTROY < cycle ) | 
|---|
| 698 | printk("\n[DBG] %s : %s / vpn_base %x / vpn_size %d\n", | 
|---|
| 699 | __FUNCTION__ , vseg_type_str( vseg->type ), vseg->vpn_base, vseg->vpn_size ); | 
|---|
| 700 | #endif | 
|---|
| 701 |  | 
|---|
| 702 | // unmap and release physical pages | 
|---|
| 703 | vmm_unmap_vseg( process , vseg ); | 
|---|
| 704 |  | 
|---|
| 705 | // remove vseg from VSL | 
|---|
| 706 | vseg_detach( vmm , vseg ); | 
|---|
| 707 |  | 
|---|
| 708 | // release memory allocated to vseg descriptor | 
|---|
| 709 | vseg_free( vseg ); | 
|---|
| 710 | } | 
|---|
| 711 |  | 
|---|
| 712 | // release lock protecting VSL | 
|---|
| 713 | remote_rwlock_wr_unlock( lock_xp ); | 
|---|
| 714 |  | 
|---|
| 715 | // remove all vsegs from zombi_lists in MMAP allocator | 
|---|
| 716 | uint32_t i; | 
|---|
| 717 | for( i = 0 ; i<32 ; i++ ) | 
|---|
| 718 | { | 
|---|
| 719 | while( !list_is_empty( &vmm->mmap_mgr.zombi_list[i] ) ) | 
|---|
| 720 | { | 
|---|
| 721 | vseg = LIST_FIRST( &vmm->mmap_mgr.zombi_list[i] , vseg_t , zlist ); | 
|---|
| 722 | vseg_detach( vmm , vseg ); | 
|---|
| 723 | vseg_free( vseg ); | 
|---|
| 724 | } | 
|---|
| 725 | } | 
|---|
| 726 |  | 
|---|
| 727 | // release memory allocated to the GPT itself | 
|---|
| 728 | hal_gpt_destroy( &vmm->gpt ); | 
|---|
| 729 |  | 
|---|
| 730 | #if CONFIG_DEBUG_VMM_DESTROY | 
|---|
| 731 | cycle = (uint32_t)hal_get_cycles(); | 
|---|
| 732 | if( CONFIG_DEBUG_VMM_DESTROY < cycle ) | 
|---|
| 733 | printk("\n[DBG] %s : thread %x exit / cycle %d\n", | 
|---|
| 734 | __FUNCTION__ , CURRENT_THREAD , cycle ); | 
|---|
| 735 | #endif | 
|---|
| 736 |  | 
|---|
| 737 | }  // end vmm_destroy() | 
|---|
| 738 |  | 
|---|
| 739 | ///////////////////////////////////////////////// | 
|---|
| 740 | vseg_t * vmm_check_conflict( process_t * process, | 
|---|
| 741 | vpn_t       vpn_base, | 
|---|
| 742 | vpn_t       vpn_size ) | 
|---|
| 743 | { | 
|---|
| 744 | vmm_t        * vmm = &process->vmm; | 
|---|
| 745 |  | 
|---|
| 746 | // scan the VSL | 
|---|
| 747 | vseg_t       * vseg; | 
|---|
| 748 | xptr_t         iter_xp; | 
|---|
| 749 | xptr_t         vseg_xp; | 
|---|
| 750 | xptr_t         root_xp = XPTR( local_cxy , &vmm->vsegs_root ); | 
|---|
| 751 |  | 
|---|
| 752 | XLIST_FOREACH( root_xp , iter_xp ) | 
|---|
| 753 | { | 
|---|
| 754 | vseg_xp = XLIST_ELEMENT( iter_xp , vseg_t , xlist ); | 
|---|
| 755 | vseg    = GET_PTR( vseg_xp ); | 
|---|
| 756 |  | 
|---|
| 757 | if( ((vpn_base + vpn_size) > vseg->vpn_base) && | 
|---|
| 758 | (vpn_base < (vseg->vpn_base + vseg->vpn_size)) ) return vseg; | 
|---|
| 759 | } | 
|---|
| 760 | return NULL; | 
|---|
| 761 |  | 
|---|
| 762 | }  // end vmm_check_conflict() | 
|---|
| 763 |  | 
|---|
| 764 | //////////////////////////////////////////////////////////////////////////////////////////// | 
|---|
| 765 | // This static function is called by the vmm_create_vseg() function, and implements | 
|---|
| 766 | // the VMM stack_vseg specific allocator. | 
|---|
| 767 | //////////////////////////////////////////////////////////////////////////////////////////// | 
|---|
| 768 | // @ vmm      : pointer on VMM. | 
|---|
| 769 | // @ vpn_base : (return value) first allocated page | 
|---|
| 770 | // @ vpn_size : (return value) number of allocated pages | 
|---|
| 771 | //////////////////////////////////////////////////////////////////////////////////////////// | 
|---|
| 772 | static error_t vmm_stack_alloc( vmm_t * vmm, | 
|---|
| 773 | vpn_t * vpn_base, | 
|---|
| 774 | vpn_t * vpn_size ) | 
|---|
| 775 | { | 
|---|
| 776 | // get stack allocator pointer | 
|---|
| 777 | stack_mgr_t * mgr = &vmm->stack_mgr; | 
|---|
| 778 |  | 
|---|
| 779 | // get lock on stack allocator | 
|---|
| 780 | spinlock_lock( &mgr->lock ); | 
|---|
| 781 |  | 
|---|
| 782 | // get first free slot index in bitmap | 
|---|
| 783 | int32_t index = bitmap_ffc( &mgr->bitmap , 4 ); | 
|---|
| 784 | if( (index < 0) || (index > 31) ) | 
|---|
| 785 | { | 
|---|
| 786 | spinlock_unlock( &mgr->lock ); | 
|---|
| 787 | return ENOMEM; | 
|---|
| 788 | } | 
|---|
| 789 |  | 
|---|
| 790 | // update bitmap | 
|---|
| 791 | bitmap_set( &mgr->bitmap , index ); | 
|---|
| 792 |  | 
|---|
| 793 | // release lock on stack allocator | 
|---|
| 794 | spinlock_unlock( &mgr->lock ); | 
|---|
| 795 |  | 
|---|
| 796 | // returns vpn_base, vpn_size (one page non allocated) | 
|---|
| 797 | *vpn_base = mgr->vpn_base + index * CONFIG_VMM_STACK_SIZE + 1; | 
|---|
| 798 | *vpn_size = CONFIG_VMM_STACK_SIZE - 1; | 
|---|
| 799 | return 0; | 
|---|
| 800 |  | 
|---|
| 801 | } // end vmm_stack_alloc() | 
|---|
| 802 |  | 
|---|
| 803 | //////////////////////////////////////////////////////////////////////////////////////////// | 
|---|
| 804 | // This static function is called by the vmm_create_vseg() function, and implements | 
|---|
| 805 | // the VMM MMAP specific allocator. | 
|---|
| 806 | //////////////////////////////////////////////////////////////////////////////////////////// | 
|---|
| 807 | // @ vmm      : [in] pointer on VMM. | 
|---|
| 808 | // @ npages   : [in] requested number of pages. | 
|---|
| 809 | // @ vpn_base : [out] first allocated page. | 
|---|
| 810 | // @ vpn_size : [out] actual number of allocated pages. | 
|---|
| 811 | //////////////////////////////////////////////////////////////////////////////////////////// | 
|---|
| 812 | static error_t vmm_mmap_alloc( vmm_t * vmm, | 
|---|
| 813 | vpn_t   npages, | 
|---|
| 814 | vpn_t * vpn_base, | 
|---|
| 815 | vpn_t * vpn_size ) | 
|---|
| 816 | { | 
|---|
| 817 | uint32_t   index; | 
|---|
| 818 | vseg_t   * vseg; | 
|---|
| 819 | vpn_t      base; | 
|---|
| 820 | vpn_t      size; | 
|---|
| 821 | vpn_t      free; | 
|---|
| 822 |  | 
|---|
| 823 | // mmap vseg size must be power of 2 | 
|---|
| 824 | // compute actual size and index in zombi_list array | 
|---|
| 825 | size  = POW2_ROUNDUP( npages ); | 
|---|
| 826 | index = bits_log2( size ); | 
|---|
| 827 |  | 
|---|
| 828 | // get mmap allocator pointer | 
|---|
| 829 | mmap_mgr_t * mgr = &vmm->mmap_mgr; | 
|---|
| 830 |  | 
|---|
| 831 | // get lock on mmap allocator | 
|---|
| 832 | spinlock_lock( &mgr->lock ); | 
|---|
| 833 |  | 
|---|
| 834 | // get vseg from zombi_list or from mmap zone | 
|---|
| 835 | if( list_is_empty( &mgr->zombi_list[index] ) )     // from mmap zone | 
|---|
| 836 | { | 
|---|
| 837 | // check overflow | 
|---|
| 838 | free = mgr->first_free_vpn; | 
|---|
| 839 | if( (free + size) > mgr->vpn_size ) return ENOMEM; | 
|---|
| 840 |  | 
|---|
| 841 | // update STACK allocator | 
|---|
| 842 | mgr->first_free_vpn += size; | 
|---|
| 843 |  | 
|---|
| 844 | // compute base | 
|---|
| 845 | base = free; | 
|---|
| 846 | } | 
|---|
| 847 | else                                             // from zombi_list | 
|---|
| 848 | { | 
|---|
| 849 | // get pointer on zombi vseg from zombi_list | 
|---|
| 850 | vseg = LIST_FIRST( &mgr->zombi_list[index] , vseg_t , zlist ); | 
|---|
| 851 |  | 
|---|
| 852 | // remove vseg from free-list | 
|---|
| 853 | list_unlink( &vseg->zlist ); | 
|---|
| 854 |  | 
|---|
| 855 | // compute base | 
|---|
| 856 | base = vseg->vpn_base; | 
|---|
| 857 | } | 
|---|
| 858 |  | 
|---|
| 859 | // release lock on mmap allocator | 
|---|
| 860 | spinlock_unlock( &mgr->lock ); | 
|---|
| 861 |  | 
|---|
| 862 | // returns vpn_base, vpn_size | 
|---|
| 863 | *vpn_base = base; | 
|---|
| 864 | *vpn_size = size; | 
|---|
| 865 | return 0; | 
|---|
| 866 |  | 
|---|
| 867 | }  // end vmm_mmap_alloc() | 
|---|
| 868 |  | 
|---|
| 869 | //////////////////////////////////////////////// | 
|---|
| 870 | vseg_t * vmm_create_vseg( process_t   * process, | 
|---|
| 871 | vseg_type_t   type, | 
|---|
| 872 | intptr_t      base, | 
|---|
| 873 | uint32_t      size, | 
|---|
| 874 | uint32_t      file_offset, | 
|---|
| 875 | uint32_t      file_size, | 
|---|
| 876 | xptr_t        mapper_xp, | 
|---|
| 877 | cxy_t         cxy ) | 
|---|
| 878 | { | 
|---|
| 879 | vseg_t     * vseg;          // created vseg pointer | 
|---|
| 880 | vpn_t        vpn_base;      // first page index | 
|---|
| 881 | vpn_t        vpn_size;      // number of pages | 
|---|
| 882 | error_t      error; | 
|---|
| 883 |  | 
|---|
| 884 | #if CONFIG_DEBUG_VMM_CREATE_VSEG | 
|---|
| 885 | uint32_t cycle = (uint32_t)hal_get_cycles(); | 
|---|
| 886 | if( CONFIG_DEBUG_VMM_CREATE_VSEG < cycle ) | 
|---|
| 887 | printk("\n[DBG] %s : thread %x enter / process %x / base %x / size %x / %s / cxy %x / cycle %d\n", | 
|---|
| 888 | __FUNCTION__, CURRENT_THREAD, process->pid, base, size, vseg_type_str(type), cxy, cycle ); | 
|---|
| 889 | #endif | 
|---|
| 890 |  | 
|---|
| 891 | // get pointer on VMM | 
|---|
| 892 | vmm_t * vmm    = &process->vmm; | 
|---|
| 893 |  | 
|---|
| 894 | // compute base, size, vpn_base, vpn_size, depending on vseg type | 
|---|
| 895 | // we use the VMM specific allocators for "stack", "file", "anon", & "remote" vsegs | 
|---|
| 896 | if( type == VSEG_TYPE_STACK ) | 
|---|
| 897 | { | 
|---|
| 898 | // get vpn_base and vpn_size from STACK allocator | 
|---|
| 899 | error = vmm_stack_alloc( vmm , &vpn_base , &vpn_size ); | 
|---|
| 900 | if( error ) | 
|---|
| 901 | { | 
|---|
| 902 | printk("\n[ERROR] in %s : no space for stack vseg / process %x in cluster %x\n", | 
|---|
| 903 | __FUNCTION__ , process->pid , local_cxy ); | 
|---|
| 904 | return NULL; | 
|---|
| 905 | } | 
|---|
| 906 |  | 
|---|
| 907 | // compute vseg base and size from vpn_base and vpn_size | 
|---|
| 908 | base = vpn_base << CONFIG_PPM_PAGE_SHIFT; | 
|---|
| 909 | size = vpn_size << CONFIG_PPM_PAGE_SHIFT; | 
|---|
| 910 | } | 
|---|
| 911 | else if( (type == VSEG_TYPE_ANON) || | 
|---|
| 912 | (type == VSEG_TYPE_FILE) || | 
|---|
| 913 | (type == VSEG_TYPE_REMOTE) ) | 
|---|
| 914 | { | 
|---|
| 915 | // get vpn_base and vpn_size from MMAP allocator | 
|---|
| 916 | vpn_t npages = size >> CONFIG_PPM_PAGE_SHIFT; | 
|---|
| 917 | error = vmm_mmap_alloc( vmm , npages , &vpn_base , &vpn_size ); | 
|---|
| 918 | if( error ) | 
|---|
| 919 | { | 
|---|
| 920 | printk("\n[ERROR] in %s : no vspace for mmap vseg / process %x in cluster %x\n", | 
|---|
| 921 | __FUNCTION__ , process->pid , local_cxy ); | 
|---|
| 922 | return NULL; | 
|---|
| 923 | } | 
|---|
| 924 |  | 
|---|
| 925 | // compute vseg base and size from vpn_base and vpn_size | 
|---|
| 926 | base = vpn_base << CONFIG_PPM_PAGE_SHIFT; | 
|---|
| 927 | size = vpn_size << CONFIG_PPM_PAGE_SHIFT; | 
|---|
| 928 | } | 
|---|
| 929 | else | 
|---|
| 930 | { | 
|---|
| 931 | uint32_t vpn_min = base >> CONFIG_PPM_PAGE_SHIFT; | 
|---|
| 932 | uint32_t vpn_max = (base + size - 1) >> CONFIG_PPM_PAGE_SHIFT; | 
|---|
| 933 |  | 
|---|
| 934 | vpn_base = vpn_min; | 
|---|
| 935 | vpn_size = vpn_max - vpn_min + 1; | 
|---|
| 936 | } | 
|---|
| 937 |  | 
|---|
| 938 | // check collisions | 
|---|
| 939 | vseg = vmm_check_conflict( process , vpn_base , vpn_size ); | 
|---|
| 940 | if( vseg != NULL ) | 
|---|
| 941 | { | 
|---|
| 942 | printk("\n[ERROR] in %s for process %x : new vseg [vpn_base = %x / vpn_size = %x]\n" | 
|---|
| 943 | "  overlap existing vseg [vpn_base = %x / vpn_size = %x]\n", | 
|---|
| 944 | __FUNCTION__ , process->pid, vpn_base, vpn_size, vseg->vpn_base, vseg->vpn_size ); | 
|---|
| 945 | return NULL; | 
|---|
| 946 | } | 
|---|
| 947 |  | 
|---|
| 948 | // allocate physical memory for vseg descriptor | 
|---|
| 949 | vseg = vseg_alloc(); | 
|---|
| 950 | if( vseg == NULL ) | 
|---|
| 951 | { | 
|---|
| 952 | printk("\n[ERROR] in %s for process %x : cannot allocate memory for vseg\n", | 
|---|
| 953 | __FUNCTION__ , process->pid ); | 
|---|
| 954 | return NULL; | 
|---|
| 955 | } | 
|---|
| 956 |  | 
|---|
| 957 | // initialize vseg descriptor | 
|---|
| 958 | vseg_init( vseg, | 
|---|
| 959 | type, | 
|---|
| 960 | base, | 
|---|
| 961 | size, | 
|---|
| 962 | vpn_base, | 
|---|
| 963 | vpn_size, | 
|---|
| 964 | file_offset, | 
|---|
| 965 | file_size, | 
|---|
| 966 | mapper_xp, | 
|---|
| 967 | cxy ); | 
|---|
| 968 |  | 
|---|
| 969 | // attach vseg to VSL | 
|---|
| 970 | xptr_t lock_xp = XPTR( local_cxy , &vmm->vsegs_lock ); | 
|---|
| 971 | remote_rwlock_wr_lock( lock_xp ); | 
|---|
| 972 | vseg_attach( vmm , vseg ); | 
|---|
| 973 | remote_rwlock_wr_unlock( lock_xp ); | 
|---|
| 974 |  | 
|---|
| 975 | #if CONFIG_DEBUG_VMM_CREATE_VSEG | 
|---|
| 976 | cycle = (uint32_t)hal_get_cycles(); | 
|---|
| 977 | if( CONFIG_DEBUG_VMM_CREATE_VSEG < cycle ) | 
|---|
| 978 | printk("\n[DBG] %s : thread %x exit / process %x / %s / cxy %x / cycle %d\n", | 
|---|
| 979 | __FUNCTION__, CURRENT_THREAD, process->pid, vseg_type_str(type), cxy, cycle ); | 
|---|
| 980 | #endif | 
|---|
| 981 |  | 
|---|
| 982 | return vseg; | 
|---|
| 983 |  | 
|---|
| 984 | }  // vmm_create_vseg() | 
|---|
| 985 |  | 
|---|
| 986 | ///////////////////////////////////// | 
|---|
| 987 | void vmm_remove_vseg( vseg_t * vseg ) | 
|---|
| 988 | { | 
|---|
| 989 | // get pointers on calling process and VMM | 
|---|
| 990 | thread_t   * this    = CURRENT_THREAD; | 
|---|
| 991 | process_t  * process = this->process; | 
|---|
| 992 | vmm_t      * vmm     = &this->process->vmm; | 
|---|
| 993 | uint32_t     type    = vseg->type; | 
|---|
| 994 |  | 
|---|
| 995 | // detach vseg from VSL | 
|---|
| 996 | xptr_t lock_xp = XPTR( local_cxy , &vmm->vsegs_lock ); | 
|---|
| 997 | remote_rwlock_wr_lock( lock_xp ); | 
|---|
| 998 | vseg_detach( &process->vmm , vseg ); | 
|---|
| 999 | remote_rwlock_wr_unlock( lock_xp ); | 
|---|
| 1000 |  | 
|---|
| 1001 | // release the stack slot to VMM stack allocator if STACK type | 
|---|
| 1002 | if( type == VSEG_TYPE_STACK ) | 
|---|
| 1003 | { | 
|---|
| 1004 | // get pointer on stack allocator | 
|---|
| 1005 | stack_mgr_t * mgr = &vmm->stack_mgr; | 
|---|
| 1006 |  | 
|---|
| 1007 | // compute slot index | 
|---|
| 1008 | uint32_t index = ((vseg->vpn_base - mgr->vpn_base - 1) / CONFIG_VMM_STACK_SIZE); | 
|---|
| 1009 |  | 
|---|
| 1010 | // update stacks_bitmap | 
|---|
| 1011 | spinlock_lock( &mgr->lock ); | 
|---|
| 1012 | bitmap_clear( &mgr->bitmap , index ); | 
|---|
| 1013 | spinlock_unlock( &mgr->lock ); | 
|---|
| 1014 | } | 
|---|
| 1015 |  | 
|---|
| 1016 | // release the vseg to VMM mmap allocator if MMAP type | 
|---|
| 1017 | if( (type == VSEG_TYPE_ANON) || (type == VSEG_TYPE_FILE) || (type == VSEG_TYPE_REMOTE) ) | 
|---|
| 1018 | { | 
|---|
| 1019 | // get pointer on mmap allocator | 
|---|
| 1020 | mmap_mgr_t * mgr = &vmm->mmap_mgr; | 
|---|
| 1021 |  | 
|---|
| 1022 | // compute zombi_list index | 
|---|
| 1023 | uint32_t index = bits_log2( vseg->vpn_size ); | 
|---|
| 1024 |  | 
|---|
| 1025 | // update zombi_list | 
|---|
| 1026 | spinlock_lock( &mgr->lock ); | 
|---|
| 1027 | list_add_first( &mgr->zombi_list[index] , &vseg->zlist ); | 
|---|
| 1028 | spinlock_unlock( &mgr->lock ); | 
|---|
| 1029 | } | 
|---|
| 1030 |  | 
|---|
| 1031 | // release physical memory allocated for vseg descriptor if no MMAP type | 
|---|
| 1032 | if( (type != VSEG_TYPE_ANON) && (type != VSEG_TYPE_FILE) && (type != VSEG_TYPE_REMOTE) ) | 
|---|
| 1033 | { | 
|---|
| 1034 | vseg_free( vseg ); | 
|---|
| 1035 | } | 
|---|
| 1036 | }  // end vmm_remove_vseg() | 
|---|
| 1037 |  | 
|---|
| 1038 | ////////////////////////////////////////////// | 
|---|
| 1039 | error_t vmm_map_kernel_vseg( vseg_t    * vseg, | 
|---|
| 1040 | uint32_t    attr ) | 
|---|
| 1041 | { | 
|---|
| 1042 | vpn_t       vpn;        // VPN of PTE to be set | 
|---|
| 1043 | vpn_t       vpn_min;    // VPN of first PTE to be set | 
|---|
| 1044 | vpn_t       vpn_max;    // VPN of last PTE to be set (excluded) | 
|---|
| 1045 | ppn_t       ppn;        // PPN of allocated physical page | 
|---|
| 1046 | uint32_t    order;      // ln( number of small pages for one single PTE ) | 
|---|
| 1047 | page_t    * page; | 
|---|
| 1048 | error_t     error; | 
|---|
| 1049 |  | 
|---|
| 1050 | // check vseg type : must be a kernel vseg | 
|---|
| 1051 | uint32_t type = vseg->type; | 
|---|
| 1052 | assert( ((type==VSEG_TYPE_KCODE) || (type==VSEG_TYPE_KDATA) || (type==VSEG_TYPE_KDEV)), | 
|---|
| 1053 | __FUNCTION__ , "not a kernel vseg\n" ); | 
|---|
| 1054 |  | 
|---|
| 1055 | // get pointer on page table | 
|---|
| 1056 | gpt_t * gpt = &process_zero.vmm.gpt; | 
|---|
| 1057 |  | 
|---|
| 1058 | // define number of small pages per PTE | 
|---|
| 1059 | if( attr & GPT_SMALL ) order = 0;   // 1 small page | 
|---|
| 1060 | else                   order = 9;   // 512 small pages | 
|---|
| 1061 |  | 
|---|
| 1062 | // loop on pages in vseg | 
|---|
| 1063 | vpn_min = vseg->vpn_base; | 
|---|
| 1064 | vpn_max = vpn_min + vseg->vpn_size; | 
|---|
| 1065 | for( vpn = vpn_min ; vpn < vpn_max ; vpn++ ) | 
|---|
| 1066 | { | 
|---|
| 1067 | // allocate a physical page from local PPM | 
|---|
| 1068 | kmem_req_t req; | 
|---|
| 1069 | req.type  = KMEM_PAGE; | 
|---|
| 1070 | req.size  = order; | 
|---|
| 1071 | req.flags = AF_KERNEL | AF_ZERO; | 
|---|
| 1072 | page      = (page_t *)kmem_alloc( &req ); | 
|---|
| 1073 | if( page == NULL ) | 
|---|
| 1074 | { | 
|---|
| 1075 | printk("\n[ERROR] in %s : cannot allocate physical memory\n", __FUNCTION__ ); | 
|---|
| 1076 | return ENOMEM; | 
|---|
| 1077 | } | 
|---|
| 1078 |  | 
|---|
| 1079 | // set page table entry | 
|---|
| 1080 | ppn = ppm_page2ppn( XPTR( local_cxy , page ) ); | 
|---|
| 1081 | error = hal_gpt_set_pte( gpt, | 
|---|
| 1082 | vpn, | 
|---|
| 1083 | attr, | 
|---|
| 1084 | ppn ); | 
|---|
| 1085 | if( error ) | 
|---|
| 1086 | { | 
|---|
| 1087 | printk("\n[ERROR] in %s : cannot register PPE\n", __FUNCTION__ ); | 
|---|
| 1088 | return ENOMEM; | 
|---|
| 1089 | } | 
|---|
| 1090 | } | 
|---|
| 1091 |  | 
|---|
| 1092 | return 0; | 
|---|
| 1093 |  | 
|---|
| 1094 | }  // end vmm_map_kernel_vseg() | 
|---|
| 1095 |  | 
|---|
| 1096 | ///////////////////////////////////////// | 
|---|
| 1097 | void vmm_unmap_vseg( process_t * process, | 
|---|
| 1098 | vseg_t    * vseg ) | 
|---|
| 1099 | { | 
|---|
| 1100 | vpn_t       vpn;        // VPN of current PTE | 
|---|
| 1101 | vpn_t       vpn_min;    // VPN of first PTE | 
|---|
| 1102 | vpn_t       vpn_max;    // VPN of last PTE (excluded) | 
|---|
| 1103 | ppn_t       ppn;        // current PTE ppn value | 
|---|
| 1104 | uint32_t    attr;       // current PTE attributes | 
|---|
| 1105 | kmem_req_t  req;        // request to release memory | 
|---|
| 1106 | xptr_t      page_xp;    // extended pointer on page descriptor | 
|---|
| 1107 | cxy_t       page_cxy;   // page descriptor cluster | 
|---|
| 1108 | page_t    * page_ptr;   // page descriptor pointer | 
|---|
| 1109 | xptr_t      forks_xp;   // extended pointer on pending forks counter | 
|---|
| 1110 | uint32_t    count;      // actual number of pendinf forks | 
|---|
| 1111 |  | 
|---|
| 1112 | #if CONFIG_DEBUG_VMM_UNMAP_VSEG | 
|---|
| 1113 | uint32_t cycle = (uint32_t)hal_get_cycles(); | 
|---|
| 1114 | if( CONFIG_DEBUG_VMM_UNMAP_VSEG < cycle ) | 
|---|
| 1115 | printk("\n[DBG] %s : thread %x enter / process %x / vseg %s / base %x / cycle %d\n", | 
|---|
| 1116 | __FUNCTION__, CURRENT_THREAD, process->pid, vseg_type_str( vseg->type ), vseg->vpn_base, cycle ); | 
|---|
| 1117 | #endif | 
|---|
| 1118 |  | 
|---|
| 1119 | // get pointer on local GPT | 
|---|
| 1120 | gpt_t     * gpt = &process->vmm.gpt; | 
|---|
| 1121 |  | 
|---|
| 1122 | // loop on pages in vseg | 
|---|
| 1123 | vpn_min = vseg->vpn_base; | 
|---|
| 1124 | vpn_max = vpn_min + vseg->vpn_size; | 
|---|
| 1125 | for( vpn = vpn_min ; vpn < vpn_max ; vpn++ ) | 
|---|
| 1126 | { | 
|---|
| 1127 | // get GPT entry | 
|---|
| 1128 | hal_gpt_get_pte( gpt , vpn , &attr , &ppn ); | 
|---|
| 1129 |  | 
|---|
| 1130 | if( attr & GPT_MAPPED )  // entry is mapped | 
|---|
| 1131 | { | 
|---|
| 1132 |  | 
|---|
| 1133 | #if( CONFIG_DEBUG_VMM_UNMAP_VSEG & 1 ) | 
|---|
| 1134 | if( CONFIG_DEBUG_VMM_UNMAP_VSEG < cycle ) | 
|---|
| 1135 | printk("- vpn %x / ppn %x\n" , vpn , ppn ); | 
|---|
| 1136 | #endif | 
|---|
| 1137 |  | 
|---|
| 1138 | // check small page | 
|---|
| 1139 | assert( (attr & GPT_SMALL) , __FUNCTION__ , | 
|---|
| 1140 | "an user vseg must use small pages" ); | 
|---|
| 1141 |  | 
|---|
| 1142 | // unmap GPT entry in all GPT copies | 
|---|
| 1143 | hal_gpt_reset_pte( gpt , vpn ); | 
|---|
| 1144 |  | 
|---|
| 1145 | // handle pending forks counter if | 
|---|
| 1146 | // 1) not identity mapped | 
|---|
| 1147 | // 2) running in reference cluster | 
|---|
| 1148 | if( ((vseg->flags & VSEG_IDENT)  == 0) && | 
|---|
| 1149 | (GET_CXY( process->ref_xp ) == local_cxy) ) | 
|---|
| 1150 | { | 
|---|
| 1151 | // get extended pointer on physical page descriptor | 
|---|
| 1152 | page_xp  = ppm_ppn2page( ppn ); | 
|---|
| 1153 | page_cxy = GET_CXY( page_xp ); | 
|---|
| 1154 | page_ptr = GET_PTR( page_xp ); | 
|---|
| 1155 |  | 
|---|
| 1156 | // FIXME lock the physical page | 
|---|
| 1157 |  | 
|---|
| 1158 | // get pending forks counter | 
|---|
| 1159 | count = hal_remote_lw( XPTR( page_cxy , &page_ptr->forks ) ); | 
|---|
| 1160 |  | 
|---|
| 1161 | if( count )  // decrement pending forks counter | 
|---|
| 1162 | { | 
|---|
| 1163 | forks_xp = XPTR( page_cxy , &page_ptr->forks ); | 
|---|
| 1164 | hal_remote_atomic_add( forks_xp , -1 ); | 
|---|
| 1165 | } | 
|---|
| 1166 | else         // release physical page to relevant cluster | 
|---|
| 1167 | { | 
|---|
| 1168 | if( page_cxy == local_cxy )   // local cluster | 
|---|
| 1169 | { | 
|---|
| 1170 | req.type = KMEM_PAGE; | 
|---|
| 1171 | req.ptr  = page_ptr; | 
|---|
| 1172 | kmem_free( &req ); | 
|---|
| 1173 | } | 
|---|
| 1174 | else                          // remote cluster | 
|---|
| 1175 | { | 
|---|
| 1176 | rpc_pmem_release_pages_client( page_cxy , page_ptr ); | 
|---|
| 1177 | } | 
|---|
| 1178 | } | 
|---|
| 1179 |  | 
|---|
| 1180 | // FIXME unlock the physical page | 
|---|
| 1181 | } | 
|---|
| 1182 | } | 
|---|
| 1183 | } | 
|---|
| 1184 |  | 
|---|
| 1185 | #if CONFIG_DEBUG_VMM_UNMAP_VSEG | 
|---|
| 1186 | cycle = (uint32_t)hal_get_cycles(); | 
|---|
| 1187 | if( CONFIG_DEBUG_VMM_UNMAP_VSEG < cycle ) | 
|---|
| 1188 | printk("\n[DBG] %s : thread %x exit / process %x / vseg %s / base %x / cycle %d\n", | 
|---|
| 1189 | __FUNCTION__, CURRENT_THREAD, process->pid, vseg_type_str( vseg->type ), vseg->vpn_base, cycle ); | 
|---|
| 1190 | #endif | 
|---|
| 1191 |  | 
|---|
| 1192 | }  // end vmm_unmap_vseg() | 
|---|
| 1193 |  | 
|---|
| 1194 | ////////////////////////////////////////////////////////////////////////////////////////// | 
|---|
| 1195 | // This low-level static function is called by the vmm_get_vseg() and vmm_resize_vseg() | 
|---|
| 1196 | // functions.  It scan the list of registered vsegs to find the unique vseg containing | 
|---|
| 1197 | // a given virtual address. | 
|---|
| 1198 | ////////////////////////////////////////////////////////////////////////////////////////// | 
|---|
| 1199 | // @ vmm     : pointer on the process VMM. | 
|---|
| 1200 | // @ vaddr   : virtual address. | 
|---|
| 1201 | // @ return vseg pointer if success / return NULL if not found. | 
|---|
| 1202 | ////////////////////////////////////////////////////////////////////////////////////////// | 
|---|
| 1203 | static vseg_t * vseg_from_vaddr( vmm_t    * vmm, | 
|---|
| 1204 | intptr_t   vaddr ) | 
|---|
| 1205 | { | 
|---|
| 1206 | xptr_t   iter_xp; | 
|---|
| 1207 | xptr_t   vseg_xp; | 
|---|
| 1208 | vseg_t * vseg; | 
|---|
| 1209 |  | 
|---|
| 1210 | // get extended pointers on VSL lock and root | 
|---|
| 1211 | xptr_t lock_xp = XPTR( local_cxy , &vmm->vsegs_lock ); | 
|---|
| 1212 | xptr_t root_xp = XPTR( local_cxy , &vmm->vsegs_root ); | 
|---|
| 1213 |  | 
|---|
| 1214 | // get lock protecting the VSL | 
|---|
| 1215 | remote_rwlock_rd_lock( lock_xp ); | 
|---|
| 1216 |  | 
|---|
| 1217 | // scan the list of vsegs in VSL | 
|---|
| 1218 | XLIST_FOREACH( root_xp , iter_xp ) | 
|---|
| 1219 | { | 
|---|
| 1220 | vseg_xp = XLIST_ELEMENT( iter_xp , vseg_t , xlist ); | 
|---|
| 1221 | vseg    = GET_PTR( vseg_xp ); | 
|---|
| 1222 | if( (vaddr >= vseg->min) && (vaddr < vseg->max) ) | 
|---|
| 1223 | { | 
|---|
| 1224 | // return success | 
|---|
| 1225 | remote_rwlock_rd_unlock( lock_xp ); | 
|---|
| 1226 | return vseg; | 
|---|
| 1227 | } | 
|---|
| 1228 | } | 
|---|
| 1229 |  | 
|---|
| 1230 | // return failure | 
|---|
| 1231 | remote_rwlock_rd_unlock( lock_xp ); | 
|---|
| 1232 | return NULL; | 
|---|
| 1233 |  | 
|---|
| 1234 | }  // end vseg_from_vaddr() | 
|---|
| 1235 |  | 
|---|
| 1236 | ///////////////////////////////////////////// | 
|---|
| 1237 | error_t vmm_resize_vseg( process_t * process, | 
|---|
| 1238 | intptr_t    base, | 
|---|
| 1239 | intptr_t    size ) | 
|---|
| 1240 | { | 
|---|
| 1241 | error_t   error; | 
|---|
| 1242 | vseg_t  * new; | 
|---|
| 1243 | vpn_t     vpn_min; | 
|---|
| 1244 | vpn_t     vpn_max; | 
|---|
| 1245 |  | 
|---|
| 1246 | // get pointer on process VMM | 
|---|
| 1247 | vmm_t * vmm = &process->vmm; | 
|---|
| 1248 |  | 
|---|
| 1249 | intptr_t addr_min = base; | 
|---|
| 1250 | intptr_t addr_max = base + size; | 
|---|
| 1251 |  | 
|---|
| 1252 | // get pointer on vseg | 
|---|
| 1253 | vseg_t * vseg = vseg_from_vaddr( vmm , base ); | 
|---|
| 1254 |  | 
|---|
| 1255 | if( vseg == NULL)  return EINVAL; | 
|---|
| 1256 |  | 
|---|
| 1257 | // get extended pointer on VSL lock | 
|---|
| 1258 | xptr_t lock_xp = XPTR( local_cxy , &vmm->vsegs_lock ); | 
|---|
| 1259 |  | 
|---|
| 1260 | // get lock protecting VSL | 
|---|
| 1261 | remote_rwlock_wr_lock( lock_xp ); | 
|---|
| 1262 |  | 
|---|
| 1263 | if( (vseg->min > addr_min) || (vseg->max < addr_max) )   // region not included in vseg | 
|---|
| 1264 | { | 
|---|
| 1265 | error = EINVAL; | 
|---|
| 1266 | } | 
|---|
| 1267 | else if( (vseg->min == addr_min) && (vseg->max == addr_max) ) // vseg must be removed | 
|---|
| 1268 | { | 
|---|
| 1269 | vmm_remove_vseg( vseg ); | 
|---|
| 1270 | error = 0; | 
|---|
| 1271 | } | 
|---|
| 1272 | else if( vseg->min == addr_min )                         // vseg must be resized | 
|---|
| 1273 | { | 
|---|
| 1274 | // update vseg base address | 
|---|
| 1275 | vseg->min = addr_max; | 
|---|
| 1276 |  | 
|---|
| 1277 | // update vpn_base and vpn_size | 
|---|
| 1278 | vpn_min        = vseg->min >> CONFIG_PPM_PAGE_SHIFT; | 
|---|
| 1279 | vpn_max        = (vseg->max - 1) >> CONFIG_PPM_PAGE_SHIFT; | 
|---|
| 1280 | vseg->vpn_base = vpn_min; | 
|---|
| 1281 | vseg->vpn_size = vpn_max - vpn_min + 1; | 
|---|
| 1282 | error = 0; | 
|---|
| 1283 | } | 
|---|
| 1284 | else if( vseg->max == addr_max )                          // vseg must be resized | 
|---|
| 1285 | { | 
|---|
| 1286 | // update vseg max address | 
|---|
| 1287 | vseg->max = addr_min; | 
|---|
| 1288 |  | 
|---|
| 1289 | // update vpn_base and vpn_size | 
|---|
| 1290 | vpn_min        = vseg->min >> CONFIG_PPM_PAGE_SHIFT; | 
|---|
| 1291 | vpn_max        = (vseg->max - 1) >> CONFIG_PPM_PAGE_SHIFT; | 
|---|
| 1292 | vseg->vpn_base = vpn_min; | 
|---|
| 1293 | vseg->vpn_size = vpn_max - vpn_min + 1; | 
|---|
| 1294 | error = 0; | 
|---|
| 1295 | } | 
|---|
| 1296 | else                                                      // vseg cut in three regions | 
|---|
| 1297 | { | 
|---|
| 1298 | // resize existing vseg | 
|---|
| 1299 | vseg->max = addr_min; | 
|---|
| 1300 |  | 
|---|
| 1301 | // update vpn_base and vpn_size | 
|---|
| 1302 | vpn_min        = vseg->min >> CONFIG_PPM_PAGE_SHIFT; | 
|---|
| 1303 | vpn_max        = (vseg->max - 1) >> CONFIG_PPM_PAGE_SHIFT; | 
|---|
| 1304 | vseg->vpn_base = vpn_min; | 
|---|
| 1305 | vseg->vpn_size = vpn_max - vpn_min + 1; | 
|---|
| 1306 |  | 
|---|
| 1307 | // create new vseg | 
|---|
| 1308 | new = vmm_create_vseg( process, | 
|---|
| 1309 | vseg->type, | 
|---|
| 1310 | addr_min, | 
|---|
| 1311 | (vseg->max - addr_max), | 
|---|
| 1312 | vseg->file_offset, | 
|---|
| 1313 | vseg->file_size, | 
|---|
| 1314 | vseg->mapper_xp, | 
|---|
| 1315 | vseg->cxy ); | 
|---|
| 1316 |  | 
|---|
| 1317 | if( new == NULL ) error = EINVAL; | 
|---|
| 1318 | else              error = 0; | 
|---|
| 1319 | } | 
|---|
| 1320 |  | 
|---|
| 1321 | // release VMM lock | 
|---|
| 1322 | remote_rwlock_wr_unlock( lock_xp ); | 
|---|
| 1323 |  | 
|---|
| 1324 | return error; | 
|---|
| 1325 |  | 
|---|
| 1326 | }  // vmm_resize_vseg() | 
|---|
| 1327 |  | 
|---|
| 1328 | /////////////////////////////////////////// | 
|---|
| 1329 | error_t  vmm_get_vseg( process_t * process, | 
|---|
| 1330 | intptr_t    vaddr, | 
|---|
| 1331 | vseg_t   ** found_vseg ) | 
|---|
| 1332 | { | 
|---|
| 1333 | vmm_t  * vmm = &process->vmm; | 
|---|
| 1334 |  | 
|---|
| 1335 | // get vseg from vaddr | 
|---|
| 1336 | vseg_t * vseg = vseg_from_vaddr( vmm , vaddr ); | 
|---|
| 1337 |  | 
|---|
| 1338 | if( vseg == NULL )   // vseg not found in local cluster => try to get it from ref | 
|---|
| 1339 | { | 
|---|
| 1340 | // get extended pointer on reference process | 
|---|
| 1341 | xptr_t ref_xp = process->ref_xp; | 
|---|
| 1342 |  | 
|---|
| 1343 | // get cluster and local pointer on reference process | 
|---|
| 1344 | cxy_t       ref_cxy = GET_CXY( ref_xp ); | 
|---|
| 1345 | process_t * ref_ptr = GET_PTR( ref_xp ); | 
|---|
| 1346 |  | 
|---|
| 1347 | if( local_cxy == ref_cxy )  return -1;   // local cluster is the reference | 
|---|
| 1348 |  | 
|---|
| 1349 | // get extended pointer on reference vseg | 
|---|
| 1350 | xptr_t   vseg_xp; | 
|---|
| 1351 | error_t  error; | 
|---|
| 1352 |  | 
|---|
| 1353 | rpc_vmm_get_vseg_client( ref_cxy , ref_ptr , vaddr , &vseg_xp , &error ); | 
|---|
| 1354 |  | 
|---|
| 1355 | if( error )   return -1;       // vseg not found => illegal user vaddr | 
|---|
| 1356 |  | 
|---|
| 1357 | // allocate a vseg in local cluster | 
|---|
| 1358 | vseg = vseg_alloc(); | 
|---|
| 1359 |  | 
|---|
| 1360 | if( vseg == NULL ) return -1; | 
|---|
| 1361 |  | 
|---|
| 1362 | // initialise local vseg from reference | 
|---|
| 1363 | vseg_init_from_ref( vseg , vseg_xp ); | 
|---|
| 1364 |  | 
|---|
| 1365 | // register local vseg in local VMM | 
|---|
| 1366 | vseg_attach( &process->vmm , vseg ); | 
|---|
| 1367 | } | 
|---|
| 1368 |  | 
|---|
| 1369 | // success | 
|---|
| 1370 | *found_vseg = vseg; | 
|---|
| 1371 | return 0; | 
|---|
| 1372 |  | 
|---|
| 1373 | }  // end vmm_get_vseg() | 
|---|
| 1374 |  | 
|---|
| 1375 | ////////////////////////////////////////////////////////////////////////////////////// | 
|---|
| 1376 | // This static function compute the target cluster to allocate a physical page | 
|---|
| 1377 | // for a given <vpn> in a given <vseg>, allocates the page (with an RPC if required) | 
|---|
| 1378 | // and returns an extended pointer on the allocated page descriptor. | 
|---|
| 1379 | // The vseg cannot have the FILE type. | 
|---|
| 1380 | ////////////////////////////////////////////////////////////////////////////////////// | 
|---|
| 1381 | static xptr_t vmm_page_allocate( vseg_t * vseg, | 
|---|
| 1382 | vpn_t    vpn ) | 
|---|
| 1383 | { | 
|---|
| 1384 |  | 
|---|
| 1385 | #if CONFIG_DEBUG_VMM_ALLOCATE_PAGE | 
|---|
| 1386 | if( CONFIG_DEBUG_VMM_ALLOCATE_PAGE < (uint32_t)hal_get_cycles() ) | 
|---|
| 1387 | printk("\n[DBG] in %s : thread %x enter for vpn %x\n", | 
|---|
| 1388 | __FUNCTION__ , CURRENT_THREAD, vpn ); | 
|---|
| 1389 | #endif | 
|---|
| 1390 |  | 
|---|
| 1391 | // compute target cluster | 
|---|
| 1392 | page_t     * page_ptr; | 
|---|
| 1393 | cxy_t        page_cxy; | 
|---|
| 1394 | kmem_req_t   req; | 
|---|
| 1395 |  | 
|---|
| 1396 | uint32_t     type  = vseg->type; | 
|---|
| 1397 | uint32_t     flags = vseg->flags; | 
|---|
| 1398 |  | 
|---|
| 1399 | assert( ( type != VSEG_TYPE_FILE ) , __FUNCTION__ , "illegal vseg type\n" ); | 
|---|
| 1400 |  | 
|---|
| 1401 | if( flags & VSEG_DISTRIB )    // distributed => cxy depends on vpn LSB | 
|---|
| 1402 | { | 
|---|
| 1403 | uint32_t x_size  = LOCAL_CLUSTER->x_size; | 
|---|
| 1404 | uint32_t y_size  = LOCAL_CLUSTER->y_size; | 
|---|
| 1405 | uint32_t y_width = LOCAL_CLUSTER->y_width; | 
|---|
| 1406 | uint32_t index   = vpn & ((x_size * y_size) - 1); | 
|---|
| 1407 | uint32_t x       = index / y_size; | 
|---|
| 1408 | uint32_t y       = index % y_size; | 
|---|
| 1409 | page_cxy         = (x<<y_width) + y; | 
|---|
| 1410 | } | 
|---|
| 1411 | else                          // other cases => cxy specified in vseg | 
|---|
| 1412 | { | 
|---|
| 1413 | page_cxy         = vseg->cxy; | 
|---|
| 1414 | } | 
|---|
| 1415 |  | 
|---|
| 1416 | // allocate a physical page from target cluster | 
|---|
| 1417 | if( page_cxy == local_cxy )  // target cluster is the local cluster | 
|---|
| 1418 | { | 
|---|
| 1419 | req.type  = KMEM_PAGE; | 
|---|
| 1420 | req.size  = 0; | 
|---|
| 1421 | req.flags = AF_NONE; | 
|---|
| 1422 | page_ptr  = (page_t *)kmem_alloc( &req ); | 
|---|
| 1423 | } | 
|---|
| 1424 | else                           // target cluster is not the local cluster | 
|---|
| 1425 | { | 
|---|
| 1426 | rpc_pmem_get_pages_client( page_cxy , 0 , &page_ptr ); | 
|---|
| 1427 | } | 
|---|
| 1428 |  | 
|---|
| 1429 | #if CONFIG_DEBUG_VMM_ALLOCATE_PAGE | 
|---|
| 1430 | if( CONFIG_DEBUG_VMM_ALLOCATE_PAGE < (uint32_t)hal_get_cycles() ) | 
|---|
| 1431 | printk("\n[DBG] in %s : thread %x exit for vpn = %d / ppn = %x\n", | 
|---|
| 1432 | __FUNCTION__ , CURRENT_THREAD, vpn, ppm_page2ppn( XPTR( page_cxy , page_ptr ) ) ); | 
|---|
| 1433 | #endif | 
|---|
| 1434 |  | 
|---|
| 1435 | if( page_ptr == NULL ) return XPTR_NULL; | 
|---|
| 1436 | else                   return XPTR( page_cxy , page_ptr ); | 
|---|
| 1437 |  | 
|---|
| 1438 | }  // end vmm_page_allocate() | 
|---|
| 1439 |  | 
|---|
| 1440 | //////////////////////////////////////// | 
|---|
| 1441 | error_t vmm_get_one_ppn( vseg_t * vseg, | 
|---|
| 1442 | vpn_t    vpn, | 
|---|
| 1443 | ppn_t  * ppn ) | 
|---|
| 1444 | { | 
|---|
| 1445 | error_t    error; | 
|---|
| 1446 | xptr_t     page_xp;           // extended pointer on physical page descriptor | 
|---|
| 1447 | page_t   * page_ptr;          // local pointer on physical page descriptor | 
|---|
| 1448 | uint32_t   index;             // missing page index in vseg mapper | 
|---|
| 1449 | uint32_t   type;              // vseg type; | 
|---|
| 1450 |  | 
|---|
| 1451 | type      = vseg->type; | 
|---|
| 1452 | index     = vpn - vseg->vpn_base; | 
|---|
| 1453 |  | 
|---|
| 1454 | #if CONFIG_DEBUG_VMM_GET_ONE_PPN | 
|---|
| 1455 | if( CONFIG_DEBUG_VMM_GET_ONE_PPN < (uint32_t)hal_get_cycles() ) | 
|---|
| 1456 | printk("\n[DBG] %s : thread %x enter for vpn = %x / type = %s / index = %d\n", | 
|---|
| 1457 | __FUNCTION__, CURRENT_THREAD, vpn, vseg_type_str(type), index ); | 
|---|
| 1458 | #endif | 
|---|
| 1459 |  | 
|---|
| 1460 | // FILE type : get the physical page from the file mapper | 
|---|
| 1461 | if( type == VSEG_TYPE_FILE ) | 
|---|
| 1462 | { | 
|---|
| 1463 | // get extended pointer on mapper | 
|---|
| 1464 | xptr_t mapper_xp = vseg->mapper_xp; | 
|---|
| 1465 |  | 
|---|
| 1466 | assert( (mapper_xp != XPTR_NULL), __FUNCTION__, | 
|---|
| 1467 | "mapper not defined for a FILE vseg\n" ); | 
|---|
| 1468 |  | 
|---|
| 1469 | // get mapper cluster and local pointer | 
|---|
| 1470 | cxy_t      mapper_cxy = GET_CXY( mapper_xp ); | 
|---|
| 1471 | mapper_t * mapper_ptr = GET_PTR( mapper_xp ); | 
|---|
| 1472 |  | 
|---|
| 1473 | // get page descriptor from mapper | 
|---|
| 1474 | if( mapper_cxy == local_cxy )             // mapper is local | 
|---|
| 1475 | { | 
|---|
| 1476 | page_ptr = mapper_get_page( mapper_ptr , index ); | 
|---|
| 1477 | } | 
|---|
| 1478 | else                                      // mapper is remote | 
|---|
| 1479 | { | 
|---|
| 1480 | rpc_mapper_get_page_client( mapper_cxy , mapper_ptr , index , &page_ptr ); | 
|---|
| 1481 | } | 
|---|
| 1482 |  | 
|---|
| 1483 | if ( page_ptr == NULL ) return EINVAL; | 
|---|
| 1484 |  | 
|---|
| 1485 | page_xp = XPTR( mapper_cxy , page_ptr ); | 
|---|
| 1486 | } | 
|---|
| 1487 |  | 
|---|
| 1488 | // Other types : allocate a physical page from target cluster, | 
|---|
| 1489 | // as defined by vseg type and vpn value | 
|---|
| 1490 | else | 
|---|
| 1491 | { | 
|---|
| 1492 | // allocate one physical page | 
|---|
| 1493 | page_xp = vmm_page_allocate( vseg , vpn ); | 
|---|
| 1494 |  | 
|---|
| 1495 | if( page_xp == XPTR_NULL ) return ENOMEM; | 
|---|
| 1496 |  | 
|---|
| 1497 | // initialise missing page from .elf file mapper for DATA and CODE types | 
|---|
| 1498 | // (the vseg->mapper_xp field is an extended pointer on the .elf file mapper) | 
|---|
| 1499 | if( (type == VSEG_TYPE_CODE) || (type == VSEG_TYPE_DATA) ) | 
|---|
| 1500 | { | 
|---|
| 1501 | // get extended pointer on mapper | 
|---|
| 1502 | xptr_t     mapper_xp = vseg->mapper_xp; | 
|---|
| 1503 |  | 
|---|
| 1504 | assert( (mapper_xp != XPTR_NULL), __FUNCTION__, | 
|---|
| 1505 | "mapper not defined for a CODE or DATA vseg\n" ); | 
|---|
| 1506 |  | 
|---|
| 1507 | // get mapper cluster and local pointer | 
|---|
| 1508 | cxy_t      mapper_cxy = GET_CXY( mapper_xp ); | 
|---|
| 1509 | mapper_t * mapper_ptr = GET_PTR( mapper_xp ); | 
|---|
| 1510 |  | 
|---|
| 1511 | // compute missing page offset in vseg | 
|---|
| 1512 | uint32_t offset = index << CONFIG_PPM_PAGE_SHIFT; | 
|---|
| 1513 |  | 
|---|
| 1514 | // compute missing page offset in .elf file | 
|---|
| 1515 | uint32_t elf_offset = vseg->file_offset + offset; | 
|---|
| 1516 |  | 
|---|
| 1517 | #if (CONFIG_DEBUG_VMM_GET_ONE_PPN & 0x1) | 
|---|
| 1518 | if( CONFIG_DEBUG_VMM_GET_ONE_PPN < (uint32_t)hal_get_cycles() ) | 
|---|
| 1519 | printk("\n[DBG] %s : thread %x for vpn = %x / elf_offset = %x\n", | 
|---|
| 1520 | __FUNCTION__, CURRENT_THREAD, vpn, elf_offset ); | 
|---|
| 1521 | #endif | 
|---|
| 1522 |  | 
|---|
| 1523 | // compute extended pointer on page base | 
|---|
| 1524 | xptr_t base_xp  = ppm_page2base( page_xp ); | 
|---|
| 1525 |  | 
|---|
| 1526 | // file_size (in .elf mapper) can be smaller than vseg_size (BSS) | 
|---|
| 1527 | uint32_t file_size = vseg->file_size; | 
|---|
| 1528 |  | 
|---|
| 1529 | if( file_size < offset )                 // missing page fully in  BSS | 
|---|
| 1530 | { | 
|---|
| 1531 |  | 
|---|
| 1532 | #if (CONFIG_DEBUG_VMM_GET_ONE_PPN & 0x1) | 
|---|
| 1533 | if( CONFIG_DEBUG_VMM_GET_ONE_PPN < (uint32_t)hal_get_cycles() ) | 
|---|
| 1534 | printk("\n[DBG] %s : thread%x for vpn = %x / fully in BSS\n", | 
|---|
| 1535 | __FUNCTION__, CURRENT_THREAD, vpn ); | 
|---|
| 1536 | #endif | 
|---|
| 1537 |  | 
|---|
| 1538 | if( GET_CXY( page_xp ) == local_cxy ) | 
|---|
| 1539 | { | 
|---|
| 1540 | memset( GET_PTR( base_xp ) , 0 , CONFIG_PPM_PAGE_SIZE ); | 
|---|
| 1541 | } | 
|---|
| 1542 | else | 
|---|
| 1543 | { | 
|---|
| 1544 | hal_remote_memset( base_xp , 0 , CONFIG_PPM_PAGE_SIZE ); | 
|---|
| 1545 | } | 
|---|
| 1546 | } | 
|---|
| 1547 | else if( file_size >= (offset + CONFIG_PPM_PAGE_SIZE) )  // fully in  mapper | 
|---|
| 1548 | { | 
|---|
| 1549 |  | 
|---|
| 1550 | #if (CONFIG_DEBUG_VMM_GET_ONE_PPN & 0x1) | 
|---|
| 1551 | if( CONFIG_DEBUG_VMM_GET_ONE_PPN < (uint32_t)hal_get_cycles() ) | 
|---|
| 1552 | printk("\n[DBG] %s : thread %x, for vpn = %x / fully in mapper\n", | 
|---|
| 1553 | __FUNCTION__, CURRENT_THREAD, vpn ); | 
|---|
| 1554 | #endif | 
|---|
| 1555 |  | 
|---|
| 1556 | if( mapper_cxy == local_cxy ) | 
|---|
| 1557 | { | 
|---|
| 1558 | error = mapper_move_kernel( mapper_ptr, | 
|---|
| 1559 | true,             // to_buffer | 
|---|
| 1560 | elf_offset, | 
|---|
| 1561 | base_xp, | 
|---|
| 1562 | CONFIG_PPM_PAGE_SIZE ); | 
|---|
| 1563 | } | 
|---|
| 1564 | else | 
|---|
| 1565 | { | 
|---|
| 1566 | rpc_mapper_move_buffer_client( mapper_cxy, | 
|---|
| 1567 | mapper_ptr, | 
|---|
| 1568 | true,         // to buffer | 
|---|
| 1569 | false,        // kernel buffer | 
|---|
| 1570 | elf_offset, | 
|---|
| 1571 | base_xp, | 
|---|
| 1572 | CONFIG_PPM_PAGE_SIZE, | 
|---|
| 1573 | &error ); | 
|---|
| 1574 | } | 
|---|
| 1575 | if( error ) return EINVAL; | 
|---|
| 1576 | } | 
|---|
| 1577 | else  // both in mapper and in BSS : | 
|---|
| 1578 | // - (file_size - offset)             bytes from mapper | 
|---|
| 1579 | // - (page_size + offset - file_size) bytes from BSS | 
|---|
| 1580 | { | 
|---|
| 1581 |  | 
|---|
| 1582 | #if (CONFIG_DEBUG_VMM_GET_ONE_PPN & 0x1) | 
|---|
| 1583 | if( CONFIG_DEBUG_VMM_GET_ONE_PPN < (uint32_t)hal_get_cycles() ) | 
|---|
| 1584 | printk("\n[DBG] %s : thread %x for vpn = %x / both mapper & BSS\n" | 
|---|
| 1585 | "      %d bytes from mapper / %d bytes from BSS\n", | 
|---|
| 1586 | __FUNCTION__, CURRENT_THREAD, vpn, | 
|---|
| 1587 | file_size - offset , offset + CONFIG_PPM_PAGE_SIZE - file_size  ); | 
|---|
| 1588 | #endif | 
|---|
| 1589 | // initialize mapper part | 
|---|
| 1590 | if( mapper_cxy == local_cxy ) | 
|---|
| 1591 | { | 
|---|
| 1592 | error = mapper_move_kernel( mapper_ptr, | 
|---|
| 1593 | true,         // to buffer | 
|---|
| 1594 | elf_offset, | 
|---|
| 1595 | base_xp, | 
|---|
| 1596 | file_size - offset ); | 
|---|
| 1597 | } | 
|---|
| 1598 | else | 
|---|
| 1599 | { | 
|---|
| 1600 | rpc_mapper_move_buffer_client( mapper_cxy, | 
|---|
| 1601 | mapper_ptr, | 
|---|
| 1602 | true,         // to buffer | 
|---|
| 1603 | false,        // kernel buffer | 
|---|
| 1604 | elf_offset, | 
|---|
| 1605 | base_xp, | 
|---|
| 1606 | file_size - offset, | 
|---|
| 1607 | &error ); | 
|---|
| 1608 | } | 
|---|
| 1609 | if( error ) return EINVAL; | 
|---|
| 1610 |  | 
|---|
| 1611 | // initialize BSS part | 
|---|
| 1612 | if( GET_CXY( page_xp ) == local_cxy ) | 
|---|
| 1613 | { | 
|---|
| 1614 | memset( GET_PTR( base_xp ) + file_size - offset , 0 , | 
|---|
| 1615 | offset + CONFIG_PPM_PAGE_SIZE - file_size ); | 
|---|
| 1616 | } | 
|---|
| 1617 | else | 
|---|
| 1618 | { | 
|---|
| 1619 | hal_remote_memset( base_xp + file_size - offset , 0 , | 
|---|
| 1620 | offset + CONFIG_PPM_PAGE_SIZE - file_size ); | 
|---|
| 1621 | } | 
|---|
| 1622 | } | 
|---|
| 1623 | }  // end initialisation for CODE or DATA types | 
|---|
| 1624 | } | 
|---|
| 1625 |  | 
|---|
| 1626 | // return ppn | 
|---|
| 1627 | *ppn = ppm_page2ppn( page_xp ); | 
|---|
| 1628 |  | 
|---|
| 1629 | #if CONFIG_DEBUG_VMM_GET_ONE_PPN | 
|---|
| 1630 | if( CONFIG_DEBUG_VMM_GET_ONE_PPN < (uint32_t)hal_get_cycles() ) | 
|---|
| 1631 | printk("\n[DBG] %s : thread %x exit for vpn = %x / ppn = %x\n", | 
|---|
| 1632 | __FUNCTION__ , CURRENT_THREAD , vpn , *ppn ); | 
|---|
| 1633 | #endif | 
|---|
| 1634 |  | 
|---|
| 1635 | return 0; | 
|---|
| 1636 |  | 
|---|
| 1637 | }  // end vmm_get_one_ppn() | 
|---|
| 1638 |  | 
|---|
| 1639 | ///////////////////////////////////////// | 
|---|
| 1640 | error_t vmm_get_pte( process_t * process, | 
|---|
| 1641 | vpn_t       vpn, | 
|---|
| 1642 | bool_t      cow, | 
|---|
| 1643 | uint32_t  * attr, | 
|---|
| 1644 | ppn_t     * ppn ) | 
|---|
| 1645 | { | 
|---|
| 1646 | vseg_t  * vseg;       // vseg containing VPN | 
|---|
| 1647 | ppn_t     old_ppn;    // current PTE_PPN | 
|---|
| 1648 | uint32_t  old_attr;   // current PTE_ATTR | 
|---|
| 1649 | ppn_t     new_ppn;    // new PTE_PPN | 
|---|
| 1650 | uint32_t  new_attr;   // new PTE_ATTR | 
|---|
| 1651 | error_t   error; | 
|---|
| 1652 |  | 
|---|
| 1653 | // this function must be called by a thread running in the reference cluster | 
|---|
| 1654 | assert( (GET_CXY( process->ref_xp ) == local_cxy ) , __FUNCTION__ , | 
|---|
| 1655 | "not called in the reference cluster\n" ); | 
|---|
| 1656 |  | 
|---|
| 1657 | #if CONFIG_DEBUG_VMM_GET_PTE | 
|---|
| 1658 | uint32_t cycle = (uint32_t)hal_get_cycles(); | 
|---|
| 1659 | if( CONFIG_DEBUG_VMM_GET_PTE < cycle ) | 
|---|
| 1660 | printk("\n[DBG] %s : thread %x enter for vpn = %x / process %x / cow = %d / cycle %d\n", | 
|---|
| 1661 | __FUNCTION__ , CURRENT_THREAD , vpn , process->pid , cow , cycle ); | 
|---|
| 1662 | #endif | 
|---|
| 1663 |  | 
|---|
| 1664 | // get VMM pointer | 
|---|
| 1665 | vmm_t * vmm = &process->vmm; | 
|---|
| 1666 |  | 
|---|
| 1667 | // get vseg pointer from reference VSL | 
|---|
| 1668 | error = vmm_get_vseg( process , vpn<<CONFIG_PPM_PAGE_SHIFT , &vseg ); | 
|---|
| 1669 |  | 
|---|
| 1670 | if( error ) | 
|---|
| 1671 | { | 
|---|
| 1672 | printk("\n[ERROR] in %s : out of segment / process = %x / vpn = %x\n", | 
|---|
| 1673 | __FUNCTION__ , process->pid , vpn ); | 
|---|
| 1674 | return error; | 
|---|
| 1675 | } | 
|---|
| 1676 |  | 
|---|
| 1677 | #if CONFIG_DEBUG_VMM_GET_PTE | 
|---|
| 1678 | cycle = (uint32_t)hal_get_cycles(); | 
|---|
| 1679 | if( CONFIG_DEBUG_VMM_GET_PTE < cycle ) | 
|---|
| 1680 | printk("\n[DBG] %s : thread %x found vseg %s / vpn_base = %x / vpn_size = %x\n", | 
|---|
| 1681 | __FUNCTION__, CURRENT_THREAD, vseg_type_str(vseg->type), vseg->vpn_base, vseg->vpn_size ); | 
|---|
| 1682 | #endif | 
|---|
| 1683 |  | 
|---|
| 1684 | // access GPT to get current PTE attributes and PPN | 
|---|
| 1685 | hal_gpt_get_pte( &vmm->gpt , vpn , &old_attr , &old_ppn ); | 
|---|
| 1686 |  | 
|---|
| 1687 | // for both "copy_on_write" and "page_fault" events, allocate a physical page, | 
|---|
| 1688 | // initialize it, register it in the reference GPT, update GPT copies in all | 
|---|
| 1689 | // clusters containing a copy, and return the new_ppn and new_attr | 
|---|
| 1690 |  | 
|---|
| 1691 | if( cow )  /////////////////////////// copy_on_write request ////////////////////// | 
|---|
| 1692 | { | 
|---|
| 1693 | assert( (old_attr & GPT_MAPPED) , __FUNCTION__ , | 
|---|
| 1694 | "PTE must be mapped for a copy-on-write exception\n" ); | 
|---|
| 1695 |  | 
|---|
| 1696 | #if CONFIG_DEBUG_VMM_GET_PTE | 
|---|
| 1697 | cycle = (uint32_t)hal_get_cycles(); | 
|---|
| 1698 | if( CONFIG_DEBUG_VMM_GET_PTE < cycle ) | 
|---|
| 1699 | printk("\n[DBG] %s : thread %x handling COW for vpn %x in process %x\n", | 
|---|
| 1700 | __FUNCTION__, CURRENT_THREAD, vpn, process->pid ); | 
|---|
| 1701 | #endif | 
|---|
| 1702 |  | 
|---|
| 1703 | // get extended pointer, cluster and local pointer on physical page descriptor | 
|---|
| 1704 | xptr_t   page_xp  = ppm_ppn2page( old_ppn ); | 
|---|
| 1705 | cxy_t    page_cxy = GET_CXY( page_xp ); | 
|---|
| 1706 | page_t * page_ptr = GET_PTR( page_xp ); | 
|---|
| 1707 |  | 
|---|
| 1708 | // get number of pending forks in page descriptor | 
|---|
| 1709 | uint32_t forks = hal_remote_lw( XPTR( page_cxy , &page_ptr->forks ) ); | 
|---|
| 1710 |  | 
|---|
| 1711 | if( forks )        // pending fork => allocate a new page, copy old to new | 
|---|
| 1712 | { | 
|---|
| 1713 | // allocate a new physical page | 
|---|
| 1714 | page_xp = vmm_page_allocate( vseg , vpn ); | 
|---|
| 1715 | if( page_xp == XPTR_NULL ) | 
|---|
| 1716 | { | 
|---|
| 1717 | printk("\n[ERROR] in %s : no memory / process = %x / vpn = %x\n", | 
|---|
| 1718 | __FUNCTION__ , process->pid , vpn ); | 
|---|
| 1719 | return -1; | 
|---|
| 1720 | } | 
|---|
| 1721 |  | 
|---|
| 1722 | // compute allocated page PPN | 
|---|
| 1723 | new_ppn = ppm_page2ppn( page_xp ); | 
|---|
| 1724 |  | 
|---|
| 1725 | // copy old page content to new page | 
|---|
| 1726 | xptr_t  old_base_xp = ppm_ppn2base( old_ppn ); | 
|---|
| 1727 | xptr_t  new_base_xp = ppm_ppn2base( new_ppn ); | 
|---|
| 1728 | memcpy( GET_PTR( new_base_xp ), | 
|---|
| 1729 | GET_PTR( old_base_xp ), | 
|---|
| 1730 | CONFIG_PPM_PAGE_SIZE ); | 
|---|
| 1731 | } | 
|---|
| 1732 | else               // no pending fork => keep the existing page, reset COW | 
|---|
| 1733 | { | 
|---|
| 1734 | new_ppn = old_ppn; | 
|---|
| 1735 | } | 
|---|
| 1736 |  | 
|---|
| 1737 | // build new_attr : reset COW and set WRITABLE, | 
|---|
| 1738 | new_attr = (old_attr | GPT_WRITABLE) & (~GPT_COW); | 
|---|
| 1739 |  | 
|---|
| 1740 | // update GPT[vpn] for all GPT copies | 
|---|
| 1741 | vmm_global_update_pte( process, vpn, new_attr, new_ppn ); | 
|---|
| 1742 |  | 
|---|
| 1743 | // decrement pending forks counter in page descriptor | 
|---|
| 1744 | hal_remote_atomic_add( XPTR( page_cxy , &page_ptr->forks ) , -1 ); | 
|---|
| 1745 | } | 
|---|
| 1746 | else  ////////////////////////////////// page_fault request //////////////////////// | 
|---|
| 1747 | { | 
|---|
| 1748 | if( (old_attr & GPT_MAPPED) == 0 )   // true page_fault => map it | 
|---|
| 1749 | { | 
|---|
| 1750 |  | 
|---|
| 1751 | #if CONFIG_DEBUG_VMM_GET_PTE | 
|---|
| 1752 | cycle = (uint32_t)hal_get_cycles(); | 
|---|
| 1753 | if( CONFIG_DEBUG_VMM_GET_PTE < cycle ) | 
|---|
| 1754 | printk("\n[DBG] %s : thread %x handling page fault for vpn %x in process %x\n", | 
|---|
| 1755 | __FUNCTION__, CURRENT_THREAD, vpn, process->pid ); | 
|---|
| 1756 | #endif | 
|---|
| 1757 |  | 
|---|
| 1758 | // allocate new_ppn, depending on vseg type | 
|---|
| 1759 | error = vmm_get_one_ppn( vseg , vpn , &new_ppn ); | 
|---|
| 1760 | if( error ) | 
|---|
| 1761 | { | 
|---|
| 1762 | printk("\n[ERROR] in %s : no memory / process = %x / vpn = %x\n", | 
|---|
| 1763 | __FUNCTION__ , process->pid , vpn ); | 
|---|
| 1764 | return -1; | 
|---|
| 1765 | } | 
|---|
| 1766 |  | 
|---|
| 1767 | // define new_attr from vseg flags | 
|---|
| 1768 | new_attr = GPT_MAPPED | GPT_SMALL; | 
|---|
| 1769 | if( vseg->flags & VSEG_USER  ) new_attr |= GPT_USER; | 
|---|
| 1770 | if( vseg->flags & VSEG_WRITE ) new_attr |= GPT_WRITABLE; | 
|---|
| 1771 | if( vseg->flags & VSEG_EXEC  ) new_attr |= GPT_EXECUTABLE; | 
|---|
| 1772 | if( vseg->flags & VSEG_CACHE ) new_attr |= GPT_CACHABLE; | 
|---|
| 1773 |  | 
|---|
| 1774 | // register new PTE in reference GPT | 
|---|
| 1775 | // on demand policy => no update of GPT copies | 
|---|
| 1776 | error = hal_gpt_set_pte( &vmm->gpt, | 
|---|
| 1777 | vpn, | 
|---|
| 1778 | new_attr, | 
|---|
| 1779 | new_ppn ); | 
|---|
| 1780 | if( error ) | 
|---|
| 1781 | { | 
|---|
| 1782 | printk("\n[ERROR] in %s : cannot update GPT / process = %x / vpn = %x\n", | 
|---|
| 1783 | __FUNCTION__ , process->pid , vpn ); | 
|---|
| 1784 | return -1; | 
|---|
| 1785 | } | 
|---|
| 1786 | } | 
|---|
| 1787 | else                                  // mapped in reference GPT => get it | 
|---|
| 1788 | { | 
|---|
| 1789 | new_ppn  = old_ppn; | 
|---|
| 1790 | new_attr = old_attr; | 
|---|
| 1791 | } | 
|---|
| 1792 | } | 
|---|
| 1793 |  | 
|---|
| 1794 | #if CONFIG_DEBUG_VMM_GET_PTE | 
|---|
| 1795 | cycle = (uint32_t)hal_get_cycles(); | 
|---|
| 1796 | if( CONFIG_DEBUG_VMM_GET_PTE < cycle ) | 
|---|
| 1797 | printk("\n[DBG] %s : thread,%x exit for vpn %x in process %x / ppn = %x / attr = %x / cycle %d\n", | 
|---|
| 1798 | __FUNCTION__, CURRENT_THREAD, vpn, process->pid, new_ppn, new_attr, cycle ); | 
|---|
| 1799 | #endif | 
|---|
| 1800 |  | 
|---|
| 1801 | // return success | 
|---|
| 1802 | *ppn  = new_ppn; | 
|---|
| 1803 | *attr = new_attr; | 
|---|
| 1804 | return 0; | 
|---|
| 1805 |  | 
|---|
| 1806 | }  // end vmm_get_pte() | 
|---|
| 1807 |  | 
|---|
| 1808 | /////////////////////////////////////////////////// | 
|---|
| 1809 | error_t vmm_handle_page_fault( process_t * process, | 
|---|
| 1810 | vpn_t       vpn ) | 
|---|
| 1811 | { | 
|---|
| 1812 | uint32_t         attr;          // missing page attributes | 
|---|
| 1813 | ppn_t            ppn;           // missing page PPN | 
|---|
| 1814 | error_t          error; | 
|---|
| 1815 |  | 
|---|
| 1816 | #if CONFIG_DEBUG_VMM_GET_PTE | 
|---|
| 1817 | uint32_t cycle = (uint32_t)hal_get_cycles(); | 
|---|
| 1818 | if( CONFIG_DEBUG_VMM_GET_PTE < cycle ) | 
|---|
| 1819 | printk("\n[DBG] %s : thread %x enter for vpn %x / process %x / cycle %d\n", | 
|---|
| 1820 | __FUNCTION__ , CURRENT_THREAD , vpn , process->pid , cycle ); | 
|---|
| 1821 | #endif | 
|---|
| 1822 |  | 
|---|
| 1823 | // get reference process cluster and local pointer | 
|---|
| 1824 | cxy_t       ref_cxy = GET_CXY( process->ref_xp ); | 
|---|
| 1825 | process_t * ref_ptr = GET_PTR( process->ref_xp ); | 
|---|
| 1826 |  | 
|---|
| 1827 | // get missing PTE attributes and PPN from reference cluster | 
|---|
| 1828 | if( local_cxy != ref_cxy ) | 
|---|
| 1829 | { | 
|---|
| 1830 | rpc_vmm_get_pte_client( ref_cxy, | 
|---|
| 1831 | ref_ptr, | 
|---|
| 1832 | vpn, | 
|---|
| 1833 | false,    // page_fault | 
|---|
| 1834 | &attr, | 
|---|
| 1835 | &ppn, | 
|---|
| 1836 | &error ); | 
|---|
| 1837 |  | 
|---|
| 1838 | // get local VMM pointer | 
|---|
| 1839 | vmm_t * vmm = &process->vmm; | 
|---|
| 1840 |  | 
|---|
| 1841 | // update local GPT | 
|---|
| 1842 | error |= hal_gpt_set_pte( &vmm->gpt, | 
|---|
| 1843 | vpn, | 
|---|
| 1844 | attr, | 
|---|
| 1845 | ppn ); | 
|---|
| 1846 | } | 
|---|
| 1847 | else   // local cluster is the reference cluster | 
|---|
| 1848 | { | 
|---|
| 1849 | error = vmm_get_pte( process, | 
|---|
| 1850 | vpn, | 
|---|
| 1851 | false,      // page-fault | 
|---|
| 1852 | &attr, | 
|---|
| 1853 | &ppn ); | 
|---|
| 1854 | } | 
|---|
| 1855 |  | 
|---|
| 1856 | #if CONFIG_DEBUG_VMM_GET_PTE | 
|---|
| 1857 | cycle = (uint32_t)hal_get_cycles(); | 
|---|
| 1858 | if( CONFIG_DEBUG_VMM_GET_PTE < cycle ) | 
|---|
| 1859 | printk("\n[DBG] %s : thread %x exit for vpn %x / process %x / cycle %d\n", | 
|---|
| 1860 | __FUNCTION__ , CURRENT_THREAD , vpn , process->pid , cycle ); | 
|---|
| 1861 | #endif | 
|---|
| 1862 |  | 
|---|
| 1863 | return error; | 
|---|
| 1864 |  | 
|---|
| 1865 | }  // end vmm_handle_page_fault() | 
|---|
| 1866 |  | 
|---|
| 1867 | //////////////////////////////////////////// | 
|---|
| 1868 | error_t vmm_handle_cow( process_t * process, | 
|---|
| 1869 | vpn_t       vpn ) | 
|---|
| 1870 | { | 
|---|
| 1871 | uint32_t         attr;          // page attributes | 
|---|
| 1872 | ppn_t            ppn;           // page PPN | 
|---|
| 1873 | error_t          error; | 
|---|
| 1874 |  | 
|---|
| 1875 | #if CONFIG_DEBUG_VMM_GET_PTE | 
|---|
| 1876 | uint32_t cycle = (uint32_t)hal_get_cycles(); | 
|---|
| 1877 | if( CONFIG_DEBUG_VMM_GET_PTE < cycle ) | 
|---|
| 1878 | printk("\n[DBG] %s : thread %x enter for vpn %x / process %x / cycle %d\n", | 
|---|
| 1879 | __FUNCTION__ , CURRENT_THREAD , vpn , process->pid , cycle ); | 
|---|
| 1880 | #endif | 
|---|
| 1881 |  | 
|---|
| 1882 | // get reference process cluster and local pointer | 
|---|
| 1883 | cxy_t       ref_cxy = GET_CXY( process->ref_xp ); | 
|---|
| 1884 | process_t * ref_ptr = GET_PTR( process->ref_xp ); | 
|---|
| 1885 |  | 
|---|
| 1886 | // get new PTE attributes and PPN from reference cluster | 
|---|
| 1887 | if( local_cxy != ref_cxy ) | 
|---|
| 1888 | { | 
|---|
| 1889 | rpc_vmm_get_pte_client( ref_cxy, | 
|---|
| 1890 | ref_ptr, | 
|---|
| 1891 | vpn, | 
|---|
| 1892 | true,     // copy-on-write | 
|---|
| 1893 | &attr, | 
|---|
| 1894 | &ppn, | 
|---|
| 1895 | &error ); | 
|---|
| 1896 |  | 
|---|
| 1897 | // get local VMM pointer | 
|---|
| 1898 | vmm_t * vmm = &process->vmm; | 
|---|
| 1899 |  | 
|---|
| 1900 | // update local GPT | 
|---|
| 1901 | error |= hal_gpt_set_pte( &vmm->gpt, | 
|---|
| 1902 | vpn, | 
|---|
| 1903 | attr, | 
|---|
| 1904 | ppn ); | 
|---|
| 1905 | } | 
|---|
| 1906 | else   // local cluster is the reference cluster | 
|---|
| 1907 | { | 
|---|
| 1908 | error = vmm_get_pte( process, | 
|---|
| 1909 | vpn, | 
|---|
| 1910 | true,      // copy-on-write | 
|---|
| 1911 | &attr, | 
|---|
| 1912 | &ppn ); | 
|---|
| 1913 | } | 
|---|
| 1914 |  | 
|---|
| 1915 | #if CONFIG_DEBUG_VMM_GET_PTE | 
|---|
| 1916 | cycle = (uint32_t)hal_get_cycles(); | 
|---|
| 1917 | if( CONFIG_DEBUG_VMM_GET_PTE < cycle ) | 
|---|
| 1918 | printk("\n[DBG] %s : thread %x exit for vpn %x / process %x / cycle %d\n", | 
|---|
| 1919 | __FUNCTION__ , CURRENT_THREAD , vpn , process->pid , cycle ); | 
|---|
| 1920 | #endif | 
|---|
| 1921 |  | 
|---|
| 1922 | return error; | 
|---|
| 1923 |  | 
|---|
| 1924 | }  // end vmm_handle_cow() | 
|---|
| 1925 |  | 
|---|
| 1926 | /////////////////////////////////////////// | 
|---|
| 1927 | error_t vmm_v2p_translate( bool_t    ident, | 
|---|
| 1928 | void    * ptr, | 
|---|
| 1929 | paddr_t * paddr ) | 
|---|
| 1930 | { | 
|---|
| 1931 | process_t * process = CURRENT_THREAD->process; | 
|---|
| 1932 |  | 
|---|
| 1933 | if( ident )  // identity mapping | 
|---|
| 1934 | { | 
|---|
| 1935 | *paddr = (paddr_t)PADDR( local_cxy , (lpa_t)ptr ); | 
|---|
| 1936 | return 0; | 
|---|
| 1937 | } | 
|---|
| 1938 |  | 
|---|
| 1939 | // access page table | 
|---|
| 1940 | error_t  error; | 
|---|
| 1941 | vpn_t    vpn; | 
|---|
| 1942 | uint32_t attr; | 
|---|
| 1943 | ppn_t    ppn; | 
|---|
| 1944 | uint32_t offset; | 
|---|
| 1945 |  | 
|---|
| 1946 | vpn    = (vpn_t)( (intptr_t)ptr >> CONFIG_PPM_PAGE_SHIFT ); | 
|---|
| 1947 | offset = (uint32_t)( ((intptr_t)ptr) & CONFIG_PPM_PAGE_MASK ); | 
|---|
| 1948 |  | 
|---|
| 1949 | if( local_cxy == GET_CXY( process->ref_xp) ) // calling process is reference process | 
|---|
| 1950 | { | 
|---|
| 1951 | error = vmm_get_pte( process, vpn , false , &attr , &ppn ); | 
|---|
| 1952 | } | 
|---|
| 1953 | else                                         // calling process is not reference process | 
|---|
| 1954 | { | 
|---|
| 1955 | cxy_t       ref_cxy = GET_CXY( process->ref_xp ); | 
|---|
| 1956 | process_t * ref_ptr = GET_PTR( process->ref_xp ); | 
|---|
| 1957 | rpc_vmm_get_pte_client( ref_cxy , ref_ptr , vpn , false , &attr , &ppn , &error ); | 
|---|
| 1958 | } | 
|---|
| 1959 |  | 
|---|
| 1960 | // set paddr | 
|---|
| 1961 | *paddr = (((paddr_t)ppn) << CONFIG_PPM_PAGE_SHIFT) | offset; | 
|---|
| 1962 |  | 
|---|
| 1963 | return error; | 
|---|
| 1964 |  | 
|---|
| 1965 | }  // end vmm_v2p_translate() | 
|---|
| 1966 |  | 
|---|
| 1967 |  | 
|---|