[1] | 1 | /* |
---|
[611] | 2 | * vmm.c - virtual memory manager related operations definition. |
---|
[1] | 3 | * |
---|
| 4 | * Authors Ghassan Almaless (2008,2009,2010,2011, 2012) |
---|
[625] | 5 | * Alain Greiner (2016,2017,2018,2019) |
---|
[21] | 6 | * |
---|
[1] | 7 | * Copyright (c) UPMC Sorbonne Universites |
---|
| 8 | * |
---|
| 9 | * This file is part of ALMOS-MKH. |
---|
| 10 | * |
---|
| 11 | * ALMOS-MKH is free software; you can redistribute it and/or modify it |
---|
| 12 | * under the terms of the GNU General Public License as published by |
---|
| 13 | * the Free Software Foundation; version 2.0 of the License. |
---|
| 14 | * |
---|
| 15 | * ALMOS-MKH is distributed in the hope that it will be useful, but |
---|
| 16 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
| 18 | * General Public License for more details. |
---|
| 19 | * |
---|
| 20 | * You should have received a copy of the GNU General Public License |
---|
| 21 | * along with ALMOS-MKH; if not, write to the Free Software Foundation, |
---|
| 22 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
| 23 | */ |
---|
| 24 | |
---|
[14] | 25 | #include <kernel_config.h> |
---|
[457] | 26 | #include <hal_kernel_types.h> |
---|
[1] | 27 | #include <hal_special.h> |
---|
| 28 | #include <hal_gpt.h> |
---|
[409] | 29 | #include <hal_vmm.h> |
---|
[641] | 30 | #include <hal_irqmask.h> |
---|
[577] | 31 | #include <hal_macros.h> |
---|
[1] | 32 | #include <printk.h> |
---|
[23] | 33 | #include <memcpy.h> |
---|
[567] | 34 | #include <remote_queuelock.h> |
---|
[1] | 35 | #include <list.h> |
---|
[408] | 36 | #include <xlist.h> |
---|
[1] | 37 | #include <bits.h> |
---|
| 38 | #include <process.h> |
---|
| 39 | #include <thread.h> |
---|
| 40 | #include <vseg.h> |
---|
| 41 | #include <cluster.h> |
---|
| 42 | #include <scheduler.h> |
---|
| 43 | #include <vfs.h> |
---|
| 44 | #include <mapper.h> |
---|
| 45 | #include <page.h> |
---|
| 46 | #include <kmem.h> |
---|
| 47 | #include <vmm.h> |
---|
[585] | 48 | #include <hal_exception.h> |
---|
[1] | 49 | |
---|
[635] | 50 | //////////////////////////////////////////////////////////////////////////////////////////// |
---|
[1] | 51 | // Extern global variables |
---|
[635] | 52 | //////////////////////////////////////////////////////////////////////////////////////////// |
---|
[1] | 53 | |
---|
[567] | 54 | extern process_t process_zero; // allocated in cluster.c |
---|
[1] | 55 | |
---|
[625] | 56 | //////////////////////////////////////////////////////////////////////////////////////////// |
---|
[651] | 57 | // This static function is called by the vmm_user_init() function. |
---|
| 58 | // It initialises the free lists of vsegs used by the VMM MMAP allocator. |
---|
| 59 | // It makes the assumption that HEAP_BASE == 1 Gbytes and HEAP_SIZE == 2 Gbytes. |
---|
| 60 | //////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 61 | static void vmm_stack_init( vmm_t * vmm ) |
---|
| 62 | { |
---|
| 63 | |
---|
| 64 | // check STACK zone |
---|
| 65 | assert( ((CONFIG_VMM_STACK_SIZE * CONFIG_THREADS_MAX_PER_CLUSTER) <= |
---|
| 66 | (CONFIG_VMM_VSPACE_SIZE - CONFIG_VMM_STACK_BASE)) , "STACK zone too small\n"); |
---|
| 67 | |
---|
| 68 | // get pointer on STACK allocator |
---|
| 69 | stack_mgr_t * mgr = &vmm->stack_mgr; |
---|
| 70 | |
---|
| 71 | mgr->bitmap = 0; |
---|
| 72 | mgr->vpn_base = CONFIG_VMM_STACK_BASE; |
---|
| 73 | busylock_init( &mgr->lock , LOCK_VMM_STACK ); |
---|
| 74 | |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | //////////////////////////////////////////////////////////////////////////////////////////// |
---|
[625] | 78 | // This static function is called by the vmm_create_vseg() function, and implements |
---|
[651] | 79 | // the VMM STACK specific allocator. Depending on the local thread index <ltid>, |
---|
| 80 | // it ckeks availability of the corresponding slot in the process STACKS region, |
---|
| 81 | // allocates a vseg descriptor, and initializes the "vpn_base" and "vpn_size" fields. |
---|
[625] | 82 | //////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 83 | // @ vmm : [in] pointer on VMM. |
---|
| 84 | // @ ltid : [in] requested slot == local user thread identifier. |
---|
| 85 | //////////////////////////////////////////////////////////////////////////////////////////// |
---|
[651] | 86 | static vseg_t * vmm_stack_alloc( vmm_t * vmm, |
---|
| 87 | ltid_t ltid ) |
---|
[21] | 88 | { |
---|
[625] | 89 | |
---|
| 90 | // check ltid argument |
---|
| 91 | assert( (ltid <= ((CONFIG_VMM_VSPACE_SIZE - CONFIG_VMM_STACK_BASE) / CONFIG_VMM_STACK_SIZE)), |
---|
| 92 | "slot index %d too large for an user stack vseg", ltid ); |
---|
| 93 | |
---|
| 94 | // get stack allocator pointer |
---|
| 95 | stack_mgr_t * mgr = &vmm->stack_mgr; |
---|
| 96 | |
---|
[651] | 97 | // get lock protecting stack allocator |
---|
[625] | 98 | busylock_acquire( &mgr->lock ); |
---|
| 99 | |
---|
| 100 | // check requested slot is available |
---|
| 101 | assert( (bitmap_state( &mgr->bitmap , ltid ) == false), |
---|
| 102 | "slot index %d already allocated", ltid ); |
---|
| 103 | |
---|
[651] | 104 | // allocate a vseg descriptor |
---|
| 105 | vseg_t * vseg = vseg_alloc(); |
---|
| 106 | |
---|
| 107 | if( vseg == NULL ) |
---|
| 108 | { |
---|
| 109 | // release lock protecting free lists |
---|
| 110 | busylock_release( &mgr->lock ); |
---|
| 111 | |
---|
| 112 | printk("\n[ERROR] %s cannot allocate memory for vseg in cluster %x\n", |
---|
| 113 | __FUNCTION__ , local_cxy ); |
---|
| 114 | |
---|
| 115 | return NULL; |
---|
| 116 | } |
---|
| 117 | |
---|
[625] | 118 | // update bitmap |
---|
| 119 | bitmap_set( &mgr->bitmap , ltid ); |
---|
| 120 | |
---|
| 121 | // release lock on stack allocator |
---|
| 122 | busylock_release( &mgr->lock ); |
---|
| 123 | |
---|
[651] | 124 | // set "vpn_base" & "vpn_size" fields (first page non allocated) |
---|
| 125 | vseg->vpn_base = mgr->vpn_base + (ltid * CONFIG_VMM_STACK_SIZE) + 1; |
---|
| 126 | vseg->vpn_size = CONFIG_VMM_STACK_SIZE - 1; |
---|
[625] | 127 | |
---|
[651] | 128 | return vseg; |
---|
| 129 | |
---|
[625] | 130 | } // end vmm_stack_alloc() |
---|
| 131 | |
---|
| 132 | //////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 133 | // This static function is called by the vmm_remove_vseg() function, and implements |
---|
| 134 | // the VMM STACK specific desallocator. |
---|
[651] | 135 | // It updates the bitmap to release the corresponding slot in the process STACKS region, |
---|
| 136 | // and releases memory allocated to vseg descriptor. |
---|
[625] | 137 | //////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 138 | // @ vmm : [in] pointer on VMM. |
---|
| 139 | // @ vseg : [in] pointer on released vseg. |
---|
| 140 | //////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 141 | static void vmm_stack_free( vmm_t * vmm, |
---|
| 142 | vseg_t * vseg ) |
---|
| 143 | { |
---|
| 144 | // get stack allocator pointer |
---|
| 145 | stack_mgr_t * mgr = &vmm->stack_mgr; |
---|
| 146 | |
---|
| 147 | // compute slot index |
---|
| 148 | uint32_t index = (vseg->vpn_base - 1 - mgr->vpn_base) / CONFIG_VMM_STACK_SIZE; |
---|
| 149 | |
---|
| 150 | // check index |
---|
| 151 | assert( (index <= ((CONFIG_VMM_VSPACE_SIZE - CONFIG_VMM_STACK_BASE) / CONFIG_VMM_STACK_SIZE)), |
---|
| 152 | "slot index %d too large for an user stack vseg", index ); |
---|
| 153 | |
---|
| 154 | // check released slot is allocated |
---|
| 155 | assert( (bitmap_state( &mgr->bitmap , index ) == true), |
---|
| 156 | "released slot index %d non allocated", index ); |
---|
| 157 | |
---|
| 158 | // get lock on stack allocator |
---|
| 159 | busylock_acquire( &mgr->lock ); |
---|
| 160 | |
---|
| 161 | // update stacks_bitmap |
---|
| 162 | bitmap_clear( &mgr->bitmap , index ); |
---|
| 163 | |
---|
| 164 | // release lock on stack allocator |
---|
| 165 | busylock_release( &mgr->lock ); |
---|
| 166 | |
---|
[651] | 167 | // release memory allocated to vseg descriptor |
---|
| 168 | vseg_free( vseg ); |
---|
| 169 | |
---|
[625] | 170 | } // end vmm_stack_free() |
---|
| 171 | |
---|
[651] | 172 | |
---|
| 173 | |
---|
[625] | 174 | //////////////////////////////////////////////////////////////////////////////////////////// |
---|
[651] | 175 | // This function display the current state of the VMM MMAP allocator of a process VMM |
---|
| 176 | // identified by the <vmm> argument. |
---|
| 177 | //////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 178 | void vmm_mmap_display( vmm_t * vmm ) |
---|
| 179 | { |
---|
| 180 | uint32_t order; |
---|
| 181 | xptr_t root_xp; |
---|
| 182 | xptr_t iter_xp; |
---|
| 183 | |
---|
| 184 | // get pointer on process |
---|
| 185 | process_t * process = (process_t *)(((char*)vmm) - OFFSETOF( process_t , vmm )); |
---|
| 186 | |
---|
| 187 | // get process PID |
---|
| 188 | pid_t pid = process->pid; |
---|
| 189 | |
---|
| 190 | // get pointer on VMM MMAP allocator |
---|
| 191 | mmap_mgr_t * mgr = &vmm->mmap_mgr; |
---|
| 192 | |
---|
| 193 | // display header |
---|
| 194 | printk("***** VMM MMAP allocator / process %x *****\n", pid ); |
---|
| 195 | |
---|
| 196 | // scan the array of free lists of vsegs |
---|
| 197 | for( order = 0 ; order <= CONFIG_VMM_HEAP_MAX_ORDER ; order++ ) |
---|
| 198 | { |
---|
| 199 | root_xp = XPTR( local_cxy , &mgr->free_list_root[order] ); |
---|
| 200 | |
---|
| 201 | if( !xlist_is_empty( root_xp ) ) |
---|
| 202 | { |
---|
| 203 | printk(" - %d (%x pages) : ", order , 1<<order ); |
---|
| 204 | |
---|
| 205 | XLIST_FOREACH( root_xp , iter_xp ) |
---|
| 206 | { |
---|
| 207 | xptr_t vseg_xp = XLIST_ELEMENT( iter_xp , vseg_t , xlist ); |
---|
| 208 | vseg_t * vseg = GET_PTR( vseg_xp ); |
---|
| 209 | |
---|
| 210 | printk("%x | ", vseg->vpn_base ); |
---|
| 211 | } |
---|
| 212 | |
---|
| 213 | printk("\n"); |
---|
| 214 | } |
---|
| 215 | } |
---|
| 216 | } // end vmm_mmap_display() |
---|
| 217 | |
---|
| 218 | //////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 219 | // This static function is called by the vmm_user_init() function. |
---|
| 220 | // It initialises the free lists of vsegs used by the VMM MMAP allocator. |
---|
| 221 | // TODO this function is only valid for 32 bits cores, and makes three assumptions: |
---|
| 222 | // HEAP_BASE == 1 Gbytes / HEAP_SIZE == 2 Gbytes / MMAP_MAX_SIZE == 1 Gbytes |
---|
| 223 | //////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 224 | void vmm_mmap_init( vmm_t * vmm ) |
---|
| 225 | { |
---|
| 226 | |
---|
| 227 | // check HEAP base and size |
---|
| 228 | assert( (CONFIG_VMM_HEAP_BASE == 0x40000) & (CONFIG_VMM_STACK_BASE == 0xc0000), |
---|
| 229 | "CONFIG_VMM_HEAP_BASE != 0x40000 or CONFIG_VMM_STACK_BASE != 0xc0000" ); |
---|
| 230 | |
---|
| 231 | // check MMAP vseg max order |
---|
| 232 | assert( (CONFIG_VMM_HEAP_MAX_ORDER == 18), "max mmap vseg size is 256K pages" ); |
---|
| 233 | |
---|
| 234 | // get pointer on MMAP allocator |
---|
| 235 | mmap_mgr_t * mgr = &vmm->mmap_mgr; |
---|
| 236 | |
---|
| 237 | // initialize HEAP base and size |
---|
| 238 | mgr->vpn_base = CONFIG_VMM_HEAP_BASE; |
---|
| 239 | mgr->vpn_size = CONFIG_VMM_STACK_BASE - CONFIG_VMM_HEAP_BASE; |
---|
| 240 | |
---|
| 241 | // initialize lock |
---|
| 242 | busylock_init( &mgr->lock , LOCK_VMM_MMAP ); |
---|
| 243 | |
---|
| 244 | // initialize free lists |
---|
| 245 | uint32_t i; |
---|
| 246 | for( i = 0 ; i <= CONFIG_VMM_HEAP_MAX_ORDER ; i++ ) |
---|
| 247 | { |
---|
| 248 | xlist_root_init( XPTR( local_cxy , &mgr->free_list_root[i] ) ); |
---|
| 249 | } |
---|
| 250 | |
---|
| 251 | // allocate and register first 1 Gbytes vseg |
---|
| 252 | vseg_t * vseg0 = vseg_alloc(); |
---|
| 253 | |
---|
| 254 | assert( (vseg0 != NULL) , "cannot allocate vseg" ); |
---|
| 255 | |
---|
| 256 | vseg0->vpn_base = CONFIG_VMM_HEAP_BASE; |
---|
| 257 | vseg0->vpn_size = CONFIG_VMM_HEAP_BASE; |
---|
| 258 | |
---|
| 259 | xlist_add_first( XPTR( local_cxy , &mgr->free_list_root[CONFIG_VMM_HEAP_MAX_ORDER] ), |
---|
| 260 | XPTR( local_cxy , &vseg0->xlist ) ); |
---|
| 261 | |
---|
| 262 | // allocate and register second 1 Gbytes vseg |
---|
| 263 | vseg_t * vseg1 = vseg_alloc(); |
---|
| 264 | |
---|
| 265 | assert( (vseg1 != NULL) , "cannot allocate vseg" ); |
---|
| 266 | |
---|
| 267 | vseg1->vpn_base = CONFIG_VMM_HEAP_BASE << 1; |
---|
| 268 | vseg1->vpn_size = CONFIG_VMM_HEAP_BASE; |
---|
| 269 | |
---|
| 270 | xlist_add_first( XPTR( local_cxy , &mgr->free_list_root[CONFIG_VMM_HEAP_MAX_ORDER] ), |
---|
| 271 | XPTR( local_cxy , &vseg1->xlist ) ); |
---|
| 272 | |
---|
| 273 | #if DEBUG_VMM_MMAP |
---|
| 274 | thread_t * this = CURRENT_THREAD; |
---|
| 275 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
| 276 | printk("\n[%s] thread[%x,%x] / cycle %d\n", |
---|
| 277 | __FUNCTION__, this->process->pid, this->trdid, cycle ); |
---|
| 278 | vmm_mmap_display( vmm ); |
---|
| 279 | #endif |
---|
| 280 | |
---|
| 281 | } // end vmm_mmap_init() |
---|
| 282 | |
---|
| 283 | //////////////////////////////////////////////////////////////////////////////////////////// |
---|
[625] | 284 | // This static function is called by the vmm_create_vseg() function, and implements |
---|
[651] | 285 | // the VMM MMAP specific allocator. Depending on the requested number of pages <npages>, |
---|
| 286 | // it get a free vseg from the relevant free_list, and initializes the "vpn_base" and |
---|
| 287 | // "vpn_size" fields. |
---|
[625] | 288 | //////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 289 | // @ vmm : [in] pointer on VMM. |
---|
| 290 | // @ npages : [in] requested number of pages. |
---|
[651] | 291 | // @ returns local pointer on vseg if success / returns NULL if failure. |
---|
[625] | 292 | //////////////////////////////////////////////////////////////////////////////////////////// |
---|
[651] | 293 | static vseg_t * vmm_mmap_alloc( vmm_t * vmm, |
---|
| 294 | vpn_t npages ) |
---|
[625] | 295 | { |
---|
| 296 | |
---|
[651] | 297 | #if DEBUG_VMM_MMAP |
---|
[625] | 298 | thread_t * this = CURRENT_THREAD; |
---|
| 299 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
[651] | 300 | if( DEBUG_VMM_MMAP < cycle ) |
---|
| 301 | printk("\n[%s] thread[%x,%x] for %x pages / cycle %d\n", |
---|
| 302 | __FUNCTION__, this->process->pid, this->trdid, npages, cycle ); |
---|
[625] | 303 | #endif |
---|
| 304 | |
---|
| 305 | // number of allocated pages must be power of 2 |
---|
| 306 | // compute actual size and order |
---|
[651] | 307 | vpn_t required_vpn_size = POW2_ROUNDUP( npages ); |
---|
| 308 | uint32_t required_order = bits_log2( required_vpn_size ); |
---|
[625] | 309 | |
---|
| 310 | // get mmap allocator pointer |
---|
| 311 | mmap_mgr_t * mgr = &vmm->mmap_mgr; |
---|
| 312 | |
---|
[651] | 313 | // take lock protecting free lists in MMAP allocator |
---|
[625] | 314 | busylock_acquire( &mgr->lock ); |
---|
| 315 | |
---|
[651] | 316 | // initialises the while loop variables |
---|
| 317 | uint32_t current_order = required_order; |
---|
| 318 | vseg_t * current_vseg = NULL; |
---|
[625] | 319 | |
---|
[651] | 320 | // search a free vseg equal or larger than requested size |
---|
| 321 | while( current_order <= CONFIG_VMM_HEAP_MAX_ORDER ) |
---|
| 322 | { |
---|
| 323 | // build extended pointer on free_pages_root[current_order] |
---|
| 324 | xptr_t root_xp = XPTR( local_cxy , &mgr->free_list_root[current_order] ); |
---|
[625] | 325 | |
---|
[651] | 326 | if( !xlist_is_empty( root_xp ) ) |
---|
| 327 | { |
---|
| 328 | // get extended pointer on first vseg in this free_list |
---|
| 329 | xptr_t current_vseg_xp = XLIST_FIRST( root_xp , vseg_t , xlist ); |
---|
| 330 | current_vseg = GET_PTR( current_vseg_xp ); |
---|
| 331 | |
---|
| 332 | // build extended pointer on xlist field in vseg descriptor |
---|
| 333 | xptr_t list_entry_xp = XPTR( local_cxy , ¤t_vseg->xlist ); |
---|
| 334 | |
---|
| 335 | // remove this vseg from the free_list |
---|
| 336 | xlist_unlink( list_entry_xp ); |
---|
| 337 | |
---|
| 338 | break; |
---|
| 339 | } |
---|
| 340 | |
---|
| 341 | // increment loop index |
---|
| 342 | current_order++; |
---|
| 343 | |
---|
| 344 | } // end while loop |
---|
| 345 | |
---|
| 346 | if( current_vseg == NULL ) // return failure |
---|
[625] | 347 | { |
---|
[651] | 348 | // release lock protecting free lists |
---|
| 349 | busylock_release( &mgr->lock ); |
---|
[625] | 350 | |
---|
[651] | 351 | printk("\n[ERROR] %s cannot allocate ) %d page(s) in cluster %x\n", |
---|
| 352 | __FUNCTION__, npages , local_cxy ); |
---|
[625] | 353 | |
---|
[651] | 354 | return NULL; |
---|
[625] | 355 | } |
---|
| 356 | |
---|
[651] | 357 | // split recursively the found vseg in smaller vsegs |
---|
| 358 | // if required, and update the free-lists accordingly |
---|
| 359 | while( current_order > required_order ) |
---|
| 360 | { |
---|
| 361 | // get found vseg base and size |
---|
| 362 | vpn_t vpn_base = current_vseg->vpn_base; |
---|
| 363 | vpn_t vpn_size = current_vseg->vpn_size; |
---|
| 364 | |
---|
| 365 | // allocate a new vseg for the upper half of current vseg |
---|
| 366 | vseg_t * new_vseg = vseg_alloc(); |
---|
[625] | 367 | |
---|
[651] | 368 | if( new_vseg == NULL ) |
---|
| 369 | { |
---|
| 370 | // release lock protecting free lists |
---|
| 371 | busylock_release( &mgr->lock ); |
---|
| 372 | |
---|
| 373 | printk("\n[ERROR] %s cannot allocate memory for vseg in cluster %x\n", |
---|
| 374 | __FUNCTION__ , local_cxy ); |
---|
| 375 | |
---|
| 376 | return NULL; |
---|
| 377 | } |
---|
| 378 | |
---|
| 379 | // initialise new vseg (upper half of found vseg) |
---|
| 380 | new_vseg->vmm = vmm; |
---|
| 381 | new_vseg->vpn_base = vpn_base + (vpn_size >> 1); |
---|
| 382 | new_vseg->vpn_size = vpn_size >> 1; |
---|
| 383 | |
---|
| 384 | // insert new vseg in relevant free_list |
---|
| 385 | xlist_add_first( XPTR( local_cxy , &mgr->free_list_root[current_order-1] ), |
---|
| 386 | XPTR( local_cxy , &new_vseg->xlist ) ); |
---|
| 387 | |
---|
| 388 | // update found vseg |
---|
| 389 | current_vseg->vpn_size = vpn_size>>1; |
---|
| 390 | |
---|
| 391 | // update order |
---|
| 392 | current_order --; |
---|
| 393 | } |
---|
| 394 | |
---|
| 395 | // release lock protecting free lists |
---|
| 396 | busylock_release( &mgr->lock ); |
---|
| 397 | |
---|
| 398 | #if DEBUG_VMM_MMAP |
---|
| 399 | vmm_mmap_display( vmm ); |
---|
[625] | 400 | #endif |
---|
| 401 | |
---|
[651] | 402 | return current_vseg; |
---|
[625] | 403 | |
---|
| 404 | } // end vmm_mmap_alloc() |
---|
| 405 | |
---|
| 406 | //////////////////////////////////////////////////////////////////////////////////////////// |
---|
[641] | 407 | // This static function implements the VMM MMAP specific desallocator. |
---|
| 408 | // It is called by the vmm_remove_vseg() function. |
---|
[651] | 409 | // It releases the vseg to the relevant free_list, after trying (recursively) to |
---|
| 410 | // merge it to the buddy vseg. |
---|
[625] | 411 | //////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 412 | // @ vmm : [in] pointer on VMM. |
---|
| 413 | // @ vseg : [in] pointer on released vseg. |
---|
| 414 | //////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 415 | static void vmm_mmap_free( vmm_t * vmm, |
---|
| 416 | vseg_t * vseg ) |
---|
| 417 | { |
---|
[651] | 418 | |
---|
| 419 | #if DEBUG_VMM_MMAP |
---|
| 420 | thread_t * this = CURRENT_THREAD; |
---|
| 421 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
| 422 | if( DEBUG_VMM_MMAP < cycle ) |
---|
| 423 | printk("\n[%s] thread[%x,%x] for vpn_base %x / vpn_size %x / cycle %d\n", |
---|
| 424 | __FUNCTION__, this->process->pid, this->trdid, vseg->vpn_base, vseg->vpn_size, cycle ); |
---|
| 425 | #endif |
---|
| 426 | |
---|
| 427 | vseg_t * buddy_vseg; |
---|
| 428 | |
---|
| 429 | // get mmap allocator pointer |
---|
[625] | 430 | mmap_mgr_t * mgr = &vmm->mmap_mgr; |
---|
| 431 | |
---|
[651] | 432 | // take lock protecting free lists |
---|
[625] | 433 | busylock_acquire( &mgr->lock ); |
---|
| 434 | |
---|
[651] | 435 | // initialise loop variables |
---|
| 436 | // released_vseg is the currently released vseg |
---|
| 437 | vseg_t * released_vseg = vseg; |
---|
| 438 | uint32_t released_order = bits_log2( vseg->vpn_size ); |
---|
[625] | 439 | |
---|
[651] | 440 | // iteratively merge the released vseg to the buddy vseg |
---|
| 441 | // release the current page and exit when buddy not found |
---|
| 442 | while( released_order <= CONFIG_VMM_HEAP_MAX_ORDER ) |
---|
| 443 | { |
---|
| 444 | // compute buddy_vseg vpn_base |
---|
| 445 | vpn_t buddy_vpn_base = released_vseg->vpn_base ^ (1 << released_order); |
---|
| 446 | |
---|
| 447 | // build extended pointer on free_pages_root[current_order] |
---|
| 448 | xptr_t root_xp = XPTR( local_cxy , &mgr->free_list_root[released_order] ); |
---|
| 449 | |
---|
| 450 | // scan this free list to find the buddy vseg |
---|
| 451 | xptr_t iter_xp; |
---|
| 452 | buddy_vseg = NULL; |
---|
| 453 | XLIST_FOREACH( root_xp , iter_xp ) |
---|
| 454 | { |
---|
| 455 | xptr_t current_vseg_xp = XLIST_ELEMENT( iter_xp , vseg_t , xlist ); |
---|
| 456 | vseg_t * current_vseg = GET_PTR( current_vseg_xp ); |
---|
| 457 | |
---|
| 458 | if( current_vseg->vpn_base == buddy_vpn_base ) |
---|
| 459 | { |
---|
| 460 | buddy_vseg = current_vseg; |
---|
| 461 | break; |
---|
| 462 | } |
---|
| 463 | } |
---|
| 464 | |
---|
| 465 | if( buddy_vseg != NULL ) // buddy found => merge released & buddy |
---|
| 466 | { |
---|
| 467 | // update released vseg fields |
---|
| 468 | released_vseg->vpn_size = buddy_vseg->vpn_size<<1; |
---|
| 469 | if( released_vseg->vpn_base > buddy_vseg->vpn_base) |
---|
| 470 | released_vseg->vpn_base = buddy_vseg->vpn_base; |
---|
| 471 | |
---|
| 472 | // remove buddy vseg from free_list |
---|
| 473 | xlist_unlink( XPTR( local_cxy , &buddy_vseg->xlist ) ); |
---|
| 474 | |
---|
| 475 | // release memory allocated to buddy descriptor |
---|
| 476 | vseg_free( buddy_vseg ); |
---|
| 477 | } |
---|
| 478 | else // buddy not found => register & exit |
---|
| 479 | { |
---|
| 480 | // register released vseg in free list |
---|
| 481 | xlist_add_first( root_xp , XPTR( local_cxy , &released_vseg->xlist ) ); |
---|
| 482 | |
---|
| 483 | // exit while loop |
---|
| 484 | break; |
---|
| 485 | } |
---|
| 486 | |
---|
| 487 | // increment released_order |
---|
| 488 | released_order++; |
---|
| 489 | } |
---|
| 490 | |
---|
[625] | 491 | // release lock |
---|
| 492 | busylock_release( &mgr->lock ); |
---|
| 493 | |
---|
[651] | 494 | #if DEBUG_VMM_MMAP |
---|
| 495 | vmm_mmap_display( vmm ); |
---|
| 496 | #endif |
---|
[625] | 497 | |
---|
[651] | 498 | } // end vmm_mmap_free() |
---|
| 499 | |
---|
[625] | 500 | //////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 501 | // This static function registers one vseg in the VSL of a local process descriptor. |
---|
| 502 | //////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 503 | // vmm : [in] pointer on VMM. |
---|
| 504 | // vseg : [in] pointer on vseg. |
---|
| 505 | //////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 506 | void vmm_attach_vseg_to_vsl( vmm_t * vmm, |
---|
| 507 | vseg_t * vseg ) |
---|
| 508 | { |
---|
| 509 | // update vseg descriptor |
---|
| 510 | vseg->vmm = vmm; |
---|
| 511 | |
---|
| 512 | // increment vsegs number |
---|
| 513 | vmm->vsegs_nr++; |
---|
| 514 | |
---|
| 515 | // add vseg in vmm list |
---|
| 516 | xlist_add_last( XPTR( local_cxy , &vmm->vsegs_root ), |
---|
| 517 | XPTR( local_cxy , &vseg->xlist ) ); |
---|
| 518 | |
---|
| 519 | } // end vmm_attach_vseg_from_vsl() |
---|
| 520 | |
---|
| 521 | //////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 522 | // This static function removes one vseg from the VSL of a local process descriptor. |
---|
| 523 | //////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 524 | // vmm : [in] pointer on VMM. |
---|
| 525 | // vseg : [in] pointer on vseg. |
---|
| 526 | //////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 527 | void vmm_detach_vseg_from_vsl( vmm_t * vmm, |
---|
| 528 | vseg_t * vseg ) |
---|
| 529 | { |
---|
| 530 | // update vseg descriptor |
---|
| 531 | vseg->vmm = NULL; |
---|
| 532 | |
---|
| 533 | // decrement vsegs number |
---|
| 534 | vmm->vsegs_nr--; |
---|
| 535 | |
---|
| 536 | // remove vseg from VSL |
---|
| 537 | xlist_unlink( XPTR( local_cxy , &vseg->xlist ) ); |
---|
| 538 | |
---|
| 539 | } // end vmm_detach_from_vsl() |
---|
| 540 | |
---|
| 541 | //////////////////////////////////////////// |
---|
| 542 | error_t vmm_user_init( process_t * process ) |
---|
| 543 | { |
---|
[1] | 544 | |
---|
[625] | 545 | #if DEBUG_VMM_USER_INIT |
---|
[567] | 546 | thread_t * this = CURRENT_THREAD; |
---|
[433] | 547 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
[625] | 548 | if( DEBUG_VMM_USER_INIT ) |
---|
[614] | 549 | printk("\n[%s] thread[%x,%x] enter for process %x in cluster %x / cycle %d\n", |
---|
| 550 | __FUNCTION__ , this->process->pid, this->trdid, process->pid, local_cxy, cycle ); |
---|
[433] | 551 | #endif |
---|
[204] | 552 | |
---|
[1] | 553 | // get pointer on VMM |
---|
| 554 | vmm_t * vmm = &process->vmm; |
---|
| 555 | |
---|
[625] | 556 | // check UTILS zone |
---|
[624] | 557 | assert( ((CONFIG_VMM_ARGS_SIZE + CONFIG_VMM_ENVS_SIZE) <= |
---|
| 558 | (CONFIG_VMM_ELF_BASE - CONFIG_VMM_UTILS_BASE)) , |
---|
| 559 | "UTILS zone too small\n" ); |
---|
[21] | 560 | |
---|
[651] | 561 | // initialize lock protecting the VSL |
---|
[640] | 562 | remote_queuelock_init( XPTR( local_cxy , &vmm->vsl_lock ) , LOCK_VMM_VSL ); |
---|
[635] | 563 | |
---|
| 564 | |
---|
[651] | 565 | // initialize STACK allocator |
---|
| 566 | vmm_stack_init( vmm ); |
---|
| 567 | |
---|
| 568 | // initialize MMAP allocator |
---|
| 569 | vmm_mmap_init( vmm ); |
---|
| 570 | |
---|
| 571 | // initialize instrumentation counters |
---|
| 572 | vmm->false_pgfault_nr = 0; |
---|
| 573 | vmm->local_pgfault_nr = 0; |
---|
| 574 | vmm->global_pgfault_nr = 0; |
---|
| 575 | vmm->false_pgfault_cost = 0; |
---|
| 576 | vmm->local_pgfault_cost = 0; |
---|
| 577 | vmm->global_pgfault_cost = 0; |
---|
| 578 | |
---|
[635] | 579 | /* |
---|
[625] | 580 | // register "args" vseg in VSL |
---|
[624] | 581 | base = CONFIG_VMM_UTILS_BASE << CONFIG_PPM_PAGE_SHIFT; |
---|
[1] | 582 | size = CONFIG_VMM_ARGS_SIZE << CONFIG_PPM_PAGE_SHIFT; |
---|
[406] | 583 | |
---|
[407] | 584 | vseg_args = vmm_create_vseg( process, |
---|
| 585 | VSEG_TYPE_DATA, |
---|
| 586 | base, |
---|
| 587 | size, |
---|
| 588 | 0, // file_offset unused |
---|
| 589 | 0, // file_size unused |
---|
| 590 | XPTR_NULL, // mapper_xp unused |
---|
| 591 | local_cxy ); |
---|
[415] | 592 | if( vseg_args == NULL ) |
---|
| 593 | { |
---|
| 594 | printk("\n[ERROR] in %s : cannot register args vseg\n", __FUNCTION__ ); |
---|
| 595 | return -1; |
---|
| 596 | } |
---|
[204] | 597 | |
---|
[406] | 598 | vmm->args_vpn_base = base; |
---|
[1] | 599 | |
---|
[625] | 600 | // register "envs" vseg in VSL |
---|
[624] | 601 | base = (CONFIG_VMM_UTILS_BASE + CONFIG_VMM_ARGS_SIZE) << CONFIG_PPM_PAGE_SHIFT; |
---|
[1] | 602 | size = CONFIG_VMM_ENVS_SIZE << CONFIG_PPM_PAGE_SHIFT; |
---|
[406] | 603 | |
---|
[407] | 604 | vseg_envs = vmm_create_vseg( process, |
---|
| 605 | VSEG_TYPE_DATA, |
---|
| 606 | base, |
---|
| 607 | size, |
---|
| 608 | 0, // file_offset unused |
---|
| 609 | 0, // file_size unused |
---|
| 610 | XPTR_NULL, // mapper_xp unused |
---|
| 611 | local_cxy ); |
---|
[415] | 612 | if( vseg_envs == NULL ) |
---|
| 613 | { |
---|
| 614 | printk("\n[ERROR] in %s : cannot register envs vseg\n", __FUNCTION__ ); |
---|
| 615 | return -1; |
---|
| 616 | } |
---|
[204] | 617 | |
---|
[406] | 618 | vmm->envs_vpn_base = base; |
---|
[635] | 619 | */ |
---|
[124] | 620 | hal_fence(); |
---|
[1] | 621 | |
---|
[625] | 622 | #if DEBUG_VMM_USER_INIT |
---|
[433] | 623 | cycle = (uint32_t)hal_get_cycles(); |
---|
[625] | 624 | if( DEBUG_VMM_USER_INIT ) |
---|
[614] | 625 | printk("\n[%s] thread[%x,%x] exit for process %x in cluster %x / cycle %d\n", |
---|
| 626 | __FUNCTION__, this->process->pid, this->trdid, process->pid, local_cxy, cycle ); |
---|
[433] | 627 | #endif |
---|
[204] | 628 | |
---|
[415] | 629 | return 0; |
---|
| 630 | |
---|
[625] | 631 | } // end vmm_user_init() |
---|
[204] | 632 | |
---|
[611] | 633 | ////////////////////////////////////////// |
---|
[625] | 634 | void vmm_user_reset( process_t * process ) |
---|
[567] | 635 | { |
---|
[625] | 636 | xptr_t vseg_xp; |
---|
| 637 | vseg_t * vseg; |
---|
| 638 | vseg_type_t vseg_type; |
---|
[567] | 639 | |
---|
[625] | 640 | #if DEBUG_VMM_USER_RESET |
---|
[635] | 641 | uint32_t cycle; |
---|
[625] | 642 | thread_t * this = CURRENT_THREAD; |
---|
[635] | 643 | #endif |
---|
| 644 | |
---|
| 645 | #if (DEBUG_VMM_USER_RESET & 1 ) |
---|
| 646 | cycle = (uint32_t)hal_get_cycles(); |
---|
[625] | 647 | if( DEBUG_VMM_USER_RESET < cycle ) |
---|
| 648 | printk("\n[%s] thread[%x,%x] enter for process %x in cluster %x / cycle %d\n", |
---|
| 649 | __FUNCTION__, this->process->pid, this->trdid, process->pid, local_cxy, cycle ); |
---|
| 650 | #endif |
---|
[567] | 651 | |
---|
[625] | 652 | #if (DEBUG_VMM_USER_RESET & 1 ) |
---|
| 653 | if( DEBUG_VMM_USER_RESET < cycle ) |
---|
[635] | 654 | hal_vmm_display( XPTR( local_cxy , process ) , true ); |
---|
[625] | 655 | #endif |
---|
[567] | 656 | |
---|
[625] | 657 | // get pointer on local VMM |
---|
| 658 | vmm_t * vmm = &process->vmm; |
---|
[624] | 659 | |
---|
[625] | 660 | // build extended pointer on VSL root and VSL lock |
---|
| 661 | xptr_t root_xp = XPTR( local_cxy , &vmm->vsegs_root ); |
---|
| 662 | xptr_t lock_xp = XPTR( local_cxy , &vmm->vsl_lock ); |
---|
[567] | 663 | |
---|
[625] | 664 | // take the VSL lock |
---|
[640] | 665 | remote_queuelock_acquire( lock_xp ); |
---|
[567] | 666 | |
---|
[625] | 667 | // scan the VSL to delete all non kernel vsegs |
---|
| 668 | // (we don't use a FOREACH in case of item deletion) |
---|
| 669 | xptr_t iter_xp; |
---|
| 670 | xptr_t next_xp; |
---|
| 671 | for( iter_xp = hal_remote_l64( root_xp ) ; |
---|
| 672 | iter_xp != root_xp ; |
---|
| 673 | iter_xp = next_xp ) |
---|
| 674 | { |
---|
| 675 | // save extended pointer on next item in xlist |
---|
| 676 | next_xp = hal_remote_l64( iter_xp ); |
---|
[611] | 677 | |
---|
[625] | 678 | // get pointers on current vseg in VSL |
---|
| 679 | vseg_xp = XLIST_ELEMENT( iter_xp , vseg_t , xlist ); |
---|
| 680 | vseg = GET_PTR( vseg_xp ); |
---|
| 681 | vseg_type = vseg->type; |
---|
[567] | 682 | |
---|
[625] | 683 | #if( DEBUG_VMM_USER_RESET & 1 ) |
---|
| 684 | if( DEBUG_VMM_USER_RESET < cycle ) |
---|
| 685 | printk("\n[%s] found %s vseg / vpn_base %x / vpn_size %d\n", |
---|
| 686 | __FUNCTION__ , vseg_type_str( vseg->type ), vseg->vpn_base, vseg->vpn_size ); |
---|
| 687 | #endif |
---|
| 688 | // delete non kernel vseg |
---|
| 689 | if( (vseg_type != VSEG_TYPE_KCODE) && |
---|
| 690 | (vseg_type != VSEG_TYPE_KDATA) && |
---|
| 691 | (vseg_type != VSEG_TYPE_KDEV ) ) |
---|
| 692 | { |
---|
| 693 | // remove vseg from VSL |
---|
| 694 | vmm_remove_vseg( process , vseg ); |
---|
[567] | 695 | |
---|
[625] | 696 | #if( DEBUG_VMM_USER_RESET & 1 ) |
---|
| 697 | if( DEBUG_VMM_USER_RESET < cycle ) |
---|
| 698 | printk("\n[%s] %s vseg deleted / vpn_base %x / vpn_size %d\n", |
---|
| 699 | __FUNCTION__ , vseg_type_str( vseg->type ), vseg->vpn_base, vseg->vpn_size ); |
---|
| 700 | #endif |
---|
| 701 | } |
---|
| 702 | else |
---|
| 703 | { |
---|
[567] | 704 | |
---|
[625] | 705 | #if( DEBUG_VMM_USER_RESET & 1 ) |
---|
| 706 | if( DEBUG_VMM_USER_RESET < cycle ) |
---|
| 707 | printk("\n[%s] keep %s vseg / vpn_base %x / vpn_size %d\n", |
---|
| 708 | __FUNCTION__ , vseg_type_str( vseg->type ), vseg->vpn_base, vseg->vpn_size ); |
---|
| 709 | #endif |
---|
| 710 | } |
---|
| 711 | } // end loop on vsegs in VSL |
---|
[567] | 712 | |
---|
[625] | 713 | // release the VSL lock |
---|
[640] | 714 | remote_queuelock_release( lock_xp ); |
---|
[567] | 715 | |
---|
[625] | 716 | // FIXME il faut gérer les process copies... |
---|
[611] | 717 | |
---|
[625] | 718 | #if DEBUG_VMM_USER_RESET |
---|
| 719 | cycle = (uint32_t)hal_get_cycles(); |
---|
| 720 | if( DEBUG_VMM_USER_RESET < cycle ) |
---|
| 721 | printk("\n[%s] thread[%x,%x] exit for process %x in cluster %x / cycle %d\n", |
---|
| 722 | __FUNCTION__, this->process->pid, this->trdid, process->pid, local_cxy , cycle ); |
---|
| 723 | #endif |
---|
[611] | 724 | |
---|
[635] | 725 | #if (DEBUG_VMM_USER_RESET & 1 ) |
---|
| 726 | if( DEBUG_VMM_USER_RESET < cycle ) |
---|
| 727 | hal_vmm_display( XPTR( local_cxy , process ) , true ); |
---|
| 728 | #endif |
---|
| 729 | |
---|
[625] | 730 | } // end vmm_user_reset() |
---|
[611] | 731 | |
---|
[640] | 732 | ///////////////////////////////////////////////// |
---|
| 733 | void vmm_global_delete_vseg( process_t * process, |
---|
| 734 | intptr_t base ) |
---|
| 735 | { |
---|
| 736 | cxy_t owner_cxy; |
---|
| 737 | lpid_t owner_lpid; |
---|
[641] | 738 | reg_t save_sr; |
---|
[640] | 739 | |
---|
[641] | 740 | xptr_t process_lock_xp; |
---|
[640] | 741 | xptr_t process_root_xp; |
---|
| 742 | xptr_t process_iter_xp; |
---|
| 743 | |
---|
| 744 | xptr_t remote_process_xp; |
---|
| 745 | cxy_t remote_process_cxy; |
---|
| 746 | process_t * remote_process_ptr; |
---|
| 747 | |
---|
| 748 | xptr_t vsl_root_xp; |
---|
| 749 | xptr_t vsl_lock_xp; |
---|
| 750 | xptr_t vsl_iter_xp; |
---|
| 751 | |
---|
[641] | 752 | rpc_desc_t rpc; // shared rpc descriptor for parallel RPCs |
---|
| 753 | uint32_t responses; // RPC responses counter |
---|
| 754 | |
---|
| 755 | thread_t * this = CURRENT_THREAD; |
---|
| 756 | pid_t pid = process->pid; |
---|
| 757 | cluster_t * cluster = LOCAL_CLUSTER; |
---|
| 758 | |
---|
[640] | 759 | #if DEBUG_VMM_GLOBAL_DELETE_VSEG |
---|
| 760 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
| 761 | #endif |
---|
| 762 | |
---|
| 763 | #if (DEBUG_VMM_GLOBAL_DELETE_VSEG & 1) |
---|
| 764 | if( DEBUG_VMM_GLOBAL_DELETE_VSEG < cycle ) |
---|
[641] | 765 | printk("\n[%s] thread[%x,%x] enters / process %x / base %x / cycle %d\n", |
---|
[640] | 766 | __FUNCTION__, this->process->pid, this->trdid, process->pid, base, cycle ); |
---|
| 767 | #endif |
---|
| 768 | |
---|
[641] | 769 | // initialize a shared RPC descriptor |
---|
| 770 | rpc.rsp = &responses; |
---|
| 771 | rpc.blocking = false; // non blocking behaviour for rpc_send() |
---|
| 772 | rpc.index = RPC_VMM_REMOVE_VSEG; |
---|
| 773 | rpc.thread = this; |
---|
| 774 | rpc.lid = this->core->lid; |
---|
| 775 | rpc.args[0] = this->process->pid; |
---|
| 776 | rpc.args[1] = base; |
---|
| 777 | |
---|
[640] | 778 | // get owner process cluster and local index |
---|
| 779 | owner_cxy = CXY_FROM_PID( pid ); |
---|
| 780 | owner_lpid = LPID_FROM_PID( pid ); |
---|
| 781 | |
---|
[641] | 782 | // get extended pointer on root and lock of process copies xlist in owner cluster |
---|
| 783 | process_root_xp = XPTR( owner_cxy , &cluster->pmgr.copies_root[owner_lpid] ); |
---|
| 784 | process_lock_xp = XPTR( owner_cxy , &cluster->pmgr.copies_lock[owner_lpid] ); |
---|
[640] | 785 | |
---|
[641] | 786 | // mask IRQs |
---|
| 787 | hal_disable_irq( &save_sr ); |
---|
| 788 | |
---|
| 789 | // client thread blocks itself |
---|
| 790 | thread_block( XPTR( local_cxy , this ) , THREAD_BLOCKED_RPC ); |
---|
| 791 | |
---|
| 792 | // take the lock protecting process copies |
---|
| 793 | remote_queuelock_acquire( process_lock_xp ); |
---|
| 794 | |
---|
| 795 | // initialize responses counter |
---|
| 796 | responses = 0; |
---|
| 797 | |
---|
[640] | 798 | // loop on process copies |
---|
| 799 | XLIST_FOREACH( process_root_xp , process_iter_xp ) |
---|
| 800 | { |
---|
| 801 | // get cluster and local pointer on remote process |
---|
| 802 | remote_process_xp = XLIST_ELEMENT( process_iter_xp , process_t , copies_list ); |
---|
| 803 | remote_process_ptr = GET_PTR( remote_process_xp ); |
---|
| 804 | remote_process_cxy = GET_CXY( remote_process_xp ); |
---|
| 805 | |
---|
| 806 | // build extended pointers on remote VSL root and lock |
---|
| 807 | vsl_root_xp = XPTR( remote_process_cxy , &remote_process_ptr->vmm.vsegs_root ); |
---|
| 808 | vsl_lock_xp = XPTR( remote_process_cxy , &remote_process_ptr->vmm.vsl_lock ); |
---|
| 809 | |
---|
| 810 | // get lock on remote VSL |
---|
| 811 | remote_queuelock_acquire( vsl_lock_xp ); |
---|
| 812 | |
---|
| 813 | // loop on vsegs in remote process VSL |
---|
| 814 | XLIST_FOREACH( vsl_root_xp , vsl_iter_xp ) |
---|
| 815 | { |
---|
| 816 | // get pointers on current vseg |
---|
| 817 | xptr_t vseg_xp = XLIST_ELEMENT( vsl_iter_xp , vseg_t , xlist ); |
---|
| 818 | vseg_t * vseg_ptr = GET_PTR( vseg_xp ); |
---|
| 819 | |
---|
| 820 | // get current vseg base address |
---|
| 821 | intptr_t vseg_base = (intptr_t)hal_remote_lpt( XPTR( remote_process_cxy, |
---|
| 822 | &vseg_ptr->min ) ); |
---|
| 823 | |
---|
| 824 | if( vseg_base == base ) // found searched vseg |
---|
| 825 | { |
---|
[641] | 826 | // atomically increment responses counter |
---|
| 827 | hal_atomic_add( &responses , 1 ); |
---|
[640] | 828 | |
---|
| 829 | #if (DEBUG_VMM_GLOBAL_DELETE_VSEG & 1) |
---|
| 830 | if( DEBUG_VMM_GLOBAL_DELETE_VSEG < cycle ) |
---|
[641] | 831 | printk("\n[%s] thread[%x,%x] register RPC request in cluster %x\n", |
---|
| 832 | __FUNCTION__, this->process->pid, this->trdid, remote_process_cxy ); |
---|
[640] | 833 | #endif |
---|
[641] | 834 | // send RPC to remote cluster |
---|
| 835 | rpc_send( remote_process_cxy , &rpc ); |
---|
[640] | 836 | |
---|
[641] | 837 | // exit loop on vsegs |
---|
| 838 | break; |
---|
[640] | 839 | } |
---|
| 840 | } // end of loop on vsegs |
---|
| 841 | |
---|
[641] | 842 | // release lock on remote VSL |
---|
| 843 | remote_queuelock_release( vsl_lock_xp ); |
---|
| 844 | |
---|
| 845 | } // end of loop on process copies |
---|
| 846 | |
---|
| 847 | // release the lock protecting process copies |
---|
| 848 | remote_queuelock_release( process_lock_xp ); |
---|
| 849 | |
---|
[640] | 850 | #if (DEBUG_VMM_GLOBAL_DELETE_VSEG & 1) |
---|
| 851 | if( DEBUG_VMM_GLOBAL_DELETE_VSEG < cycle ) |
---|
[641] | 852 | printk("\n[%s] thread[%x,%x] deschedule / process %x / base %x\n", |
---|
| 853 | __FUNCTION__, this->process->pid, this->trdid, process->pid, base ); |
---|
[640] | 854 | #endif |
---|
| 855 | |
---|
[641] | 856 | // client thread deschedule |
---|
| 857 | sched_yield("blocked on rpc_vmm_delete_vseg"); |
---|
| 858 | |
---|
| 859 | // restore IRQs |
---|
| 860 | hal_restore_irq( save_sr ); |
---|
[640] | 861 | |
---|
| 862 | #if DEBUG_VMM_GLOBAL_DELETE_VSEG |
---|
| 863 | cycle = (uint32_t)hal_get_cycles(); |
---|
| 864 | if( DEBUG_VMM_GLOBAL_DELETE_VSEG < cycle ) |
---|
[641] | 865 | printk("\n[%s] thread[%x,%x] exit / process %x / base %x / cycle %d\n", |
---|
| 866 | __FUNCTION__, this->process->pid, this->trdid, process->pid, base, cycle ); |
---|
[640] | 867 | #endif |
---|
| 868 | |
---|
| 869 | } // end vmm_global_delete_vseg() |
---|
| 870 | |
---|
[595] | 871 | //////////////////////////////////////////////// |
---|
[640] | 872 | void vmm_global_resize_vseg( process_t * process, |
---|
| 873 | intptr_t base, |
---|
| 874 | intptr_t new_base, |
---|
| 875 | intptr_t new_size ) |
---|
| 876 | { |
---|
| 877 | cxy_t owner_cxy; |
---|
| 878 | lpid_t owner_lpid; |
---|
[641] | 879 | reg_t save_sr; |
---|
[640] | 880 | |
---|
[641] | 881 | xptr_t process_lock_xp; |
---|
[640] | 882 | xptr_t process_root_xp; |
---|
| 883 | xptr_t process_iter_xp; |
---|
| 884 | |
---|
| 885 | xptr_t remote_process_xp; |
---|
| 886 | cxy_t remote_process_cxy; |
---|
| 887 | process_t * remote_process_ptr; |
---|
| 888 | |
---|
| 889 | xptr_t vsl_root_xp; |
---|
| 890 | xptr_t vsl_lock_xp; |
---|
| 891 | xptr_t vsl_iter_xp; |
---|
| 892 | |
---|
[641] | 893 | rpc_desc_t rpc; // shared rpc descriptor for parallel RPCs |
---|
| 894 | uint32_t responses; // RPC responses counter |
---|
| 895 | |
---|
| 896 | thread_t * this = CURRENT_THREAD; |
---|
| 897 | pid_t pid = process->pid; |
---|
| 898 | cluster_t * cluster = LOCAL_CLUSTER; |
---|
| 899 | |
---|
[640] | 900 | #if DEBUG_VMM_GLOBAL_RESIZE_VSEG |
---|
| 901 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
| 902 | #endif |
---|
| 903 | |
---|
| 904 | #if (DEBUG_VMM_GLOBAL_RESIZE_VSEG & 1) |
---|
| 905 | if( DEBUG_VMM_GLOBAL_RESIZE_VSEG < cycle ) |
---|
| 906 | printk("\n[%s] thread[%x,%x] : process %x / base %x / new_base %x / new_size %x / cycle %d\n", |
---|
| 907 | __FUNCTION__, this->process->pid, this->trdid, process->pid, base, new_base, new_size, cycle ); |
---|
| 908 | #endif |
---|
| 909 | |
---|
[641] | 910 | // initialize a shared RPC descriptor |
---|
| 911 | rpc.rsp = &responses; |
---|
| 912 | rpc.blocking = false; // non blocking behaviour for rpc_send() |
---|
| 913 | rpc.index = RPC_VMM_REMOVE_VSEG; |
---|
| 914 | rpc.thread = this; |
---|
| 915 | rpc.lid = this->core->lid; |
---|
| 916 | rpc.args[0] = this->process->pid; |
---|
| 917 | rpc.args[1] = base; |
---|
| 918 | rpc.args[2] = new_base; |
---|
| 919 | rpc.args[3] = new_size; |
---|
| 920 | |
---|
| 921 | // get owner process cluster and local index |
---|
[640] | 922 | owner_cxy = CXY_FROM_PID( pid ); |
---|
| 923 | owner_lpid = LPID_FROM_PID( pid ); |
---|
| 924 | |
---|
[641] | 925 | // get extended pointer on root and lock of process copies xlist in owner cluster |
---|
| 926 | process_root_xp = XPTR( owner_cxy , &cluster->pmgr.copies_root[owner_lpid] ); |
---|
| 927 | process_lock_xp = XPTR( owner_cxy , &cluster->pmgr.copies_lock[owner_lpid] ); |
---|
[640] | 928 | |
---|
[641] | 929 | // mask IRQs |
---|
| 930 | hal_disable_irq( &save_sr ); |
---|
| 931 | |
---|
| 932 | // client thread blocks itself |
---|
| 933 | thread_block( XPTR( local_cxy , this ) , THREAD_BLOCKED_RPC ); |
---|
| 934 | |
---|
| 935 | // take the lock protecting process copies |
---|
| 936 | remote_queuelock_acquire( process_lock_xp ); |
---|
| 937 | |
---|
| 938 | // initialize responses counter |
---|
| 939 | responses = 0; |
---|
| 940 | |
---|
[640] | 941 | // loop on process copies |
---|
| 942 | XLIST_FOREACH( process_root_xp , process_iter_xp ) |
---|
| 943 | { |
---|
| 944 | // get cluster and local pointer on remote process |
---|
| 945 | remote_process_xp = XLIST_ELEMENT( process_iter_xp , process_t , copies_list ); |
---|
| 946 | remote_process_ptr = GET_PTR( remote_process_xp ); |
---|
| 947 | remote_process_cxy = GET_CXY( remote_process_xp ); |
---|
| 948 | |
---|
| 949 | // build extended pointers on remote VSL root and lock |
---|
| 950 | vsl_root_xp = XPTR( remote_process_cxy , &remote_process_ptr->vmm.vsegs_root ); |
---|
| 951 | vsl_lock_xp = XPTR( remote_process_cxy , &remote_process_ptr->vmm.vsl_lock ); |
---|
| 952 | |
---|
| 953 | // get lock on remote VSL |
---|
| 954 | remote_queuelock_acquire( vsl_lock_xp ); |
---|
| 955 | |
---|
| 956 | // loop on vsegs in remote process VSL |
---|
| 957 | XLIST_FOREACH( vsl_root_xp , vsl_iter_xp ) |
---|
| 958 | { |
---|
| 959 | // get pointers on current vseg |
---|
| 960 | xptr_t vseg_xp = XLIST_ELEMENT( vsl_iter_xp , vseg_t , xlist ); |
---|
| 961 | vseg_t * vseg_ptr = GET_PTR( vseg_xp ); |
---|
| 962 | |
---|
| 963 | // get current vseg base address |
---|
| 964 | intptr_t vseg_base = (intptr_t)hal_remote_lpt( XPTR( remote_process_cxy, |
---|
| 965 | &vseg_ptr->min ) ); |
---|
| 966 | |
---|
| 967 | if( vseg_base == base ) // found searched vseg |
---|
| 968 | { |
---|
[641] | 969 | // atomically increment responses counter |
---|
| 970 | hal_atomic_add( &responses , 1 ); |
---|
| 971 | |
---|
[640] | 972 | #if (DEBUG_VMM_GLOBAL_RESIZE_VSEG & 1) |
---|
| 973 | if( DEBUG_VMM_GLOBAL_RESIZE_VSEG < cycle ) |
---|
[641] | 974 | printk("\n[%s] thread[%x,%x] register RPC request in cluster %x\n", |
---|
| 975 | __FUNCTION__, this->process->pid, this->trdid, remote_process_cxy ); |
---|
[640] | 976 | #endif |
---|
[641] | 977 | // send RPC to remote cluster |
---|
| 978 | rpc_send( remote_process_cxy , & rpc ); |
---|
[640] | 979 | |
---|
[641] | 980 | // exit loop on vsegs |
---|
| 981 | break; |
---|
[640] | 982 | } |
---|
[641] | 983 | |
---|
[640] | 984 | } // end of loop on vsegs |
---|
| 985 | |
---|
| 986 | #if (DEBUG_VMM_GLOBAL_RESIZE_VSEG & 1) |
---|
| 987 | if( DEBUG_VMM_GLOBAL_RESIZE_VSEG < cycle ) |
---|
| 988 | hal_vmm_display( remote_process_xp , false ); |
---|
| 989 | #endif |
---|
| 990 | |
---|
| 991 | // release lock on remote VSL |
---|
| 992 | remote_queuelock_release( vsl_lock_xp ); |
---|
[641] | 993 | |
---|
[640] | 994 | } // end of loop on process copies |
---|
| 995 | |
---|
[641] | 996 | // release the lock protecting process copies |
---|
| 997 | remote_queuelock_release( process_lock_xp ); |
---|
| 998 | |
---|
| 999 | #if (DEBUG_VMM_GLOBAL_RESIZE_VSEG & 1) |
---|
| 1000 | if( DEBUG_VMM_GLOBAL_RESIZE_VSEG < cycle ) |
---|
| 1001 | printk("\n[%s] thread[%x,%x] deschedule / process %x / base %x\n", |
---|
| 1002 | __FUNCTION__, this->process->pid, this->trdid, process->pid, base ); |
---|
| 1003 | #endif |
---|
| 1004 | |
---|
| 1005 | // client thread deschedule |
---|
| 1006 | sched_yield("blocked on rpc_vmm_delete_vseg"); |
---|
| 1007 | |
---|
| 1008 | // restore IRQs |
---|
| 1009 | hal_restore_irq( save_sr ); |
---|
| 1010 | |
---|
[640] | 1011 | #if DEBUG_VMM_GLOBAL_RESIZE_VSEG |
---|
| 1012 | cycle = (uint32_t)hal_get_cycles(); |
---|
| 1013 | if( DEBUG_VMM_GLOBAL_RESIZE_VSEG < cycle ) |
---|
| 1014 | printk("\n[%s] thread[%x,%x] exit for process %x / base %x / cycle %d\n", |
---|
| 1015 | __FUNCTION__, this->process->pid, this->trdid, process->pid , base, cycle ); |
---|
| 1016 | #endif |
---|
| 1017 | |
---|
| 1018 | } // end vmm_global_resize_vseg() |
---|
| 1019 | |
---|
| 1020 | //////////////////////////////////////////////// |
---|
[433] | 1021 | void vmm_global_update_pte( process_t * process, |
---|
| 1022 | vpn_t vpn, |
---|
| 1023 | uint32_t attr, |
---|
| 1024 | ppn_t ppn ) |
---|
[23] | 1025 | { |
---|
[640] | 1026 | pid_t pid; |
---|
| 1027 | cxy_t owner_cxy; |
---|
| 1028 | lpid_t owner_lpid; |
---|
| 1029 | |
---|
[408] | 1030 | xlist_entry_t * process_root_ptr; |
---|
| 1031 | xptr_t process_root_xp; |
---|
| 1032 | xptr_t process_iter_xp; |
---|
[23] | 1033 | |
---|
[408] | 1034 | xptr_t remote_process_xp; |
---|
| 1035 | cxy_t remote_process_cxy; |
---|
| 1036 | process_t * remote_process_ptr; |
---|
| 1037 | xptr_t remote_gpt_xp; |
---|
[23] | 1038 | |
---|
[640] | 1039 | #if DEBUG_VMM_GLOBAL_UPDATE_PTE |
---|
[433] | 1040 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
[595] | 1041 | thread_t * this = CURRENT_THREAD; |
---|
[640] | 1042 | #endif |
---|
| 1043 | |
---|
| 1044 | |
---|
| 1045 | #if (DEBUG_VMM_GLOBAL_UPDATE_PTE & 1) |
---|
| 1046 | if( DEBUG_VMM_GLOBAL_UPDATE_PTE < cycle ) |
---|
[635] | 1047 | printk("\n[%s] thread[%x,%x] enter for process %x / vpn %x / attr %x / ppn %x / ycle %d\n", |
---|
| 1048 | __FUNCTION__, this->process->pid, this->trdid, process->pid, vpn, attr, ppn, cycle ); |
---|
[433] | 1049 | #endif |
---|
| 1050 | |
---|
[640] | 1051 | // get owner process cluster and local index |
---|
[408] | 1052 | pid = process->pid; |
---|
| 1053 | owner_cxy = CXY_FROM_PID( pid ); |
---|
| 1054 | owner_lpid = LPID_FROM_PID( pid ); |
---|
[640] | 1055 | |
---|
| 1056 | // get extended pointer on root of process copies xlist in owner cluster |
---|
[408] | 1057 | process_root_ptr = &LOCAL_CLUSTER->pmgr.copies_root[owner_lpid]; |
---|
| 1058 | process_root_xp = XPTR( owner_cxy , process_root_ptr ); |
---|
[23] | 1059 | |
---|
[640] | 1060 | // loop on process copies |
---|
[408] | 1061 | XLIST_FOREACH( process_root_xp , process_iter_xp ) |
---|
| 1062 | { |
---|
| 1063 | // get cluster and local pointer on remote process |
---|
| 1064 | remote_process_xp = XLIST_ELEMENT( process_iter_xp , process_t , copies_list ); |
---|
[433] | 1065 | remote_process_ptr = GET_PTR( remote_process_xp ); |
---|
[408] | 1066 | remote_process_cxy = GET_CXY( remote_process_xp ); |
---|
[407] | 1067 | |
---|
[640] | 1068 | #if (DEBUG_VMM_GLOBAL_UPDATE_PTE & 1) |
---|
| 1069 | if( DEBUG_VMM_GLOBAL_UPDATE_PTE < cycle ) |
---|
[635] | 1070 | printk("\n[%s] thread[%x,%x] handling vpn %x for process %x in cluster %x\n", |
---|
[595] | 1071 | __FUNCTION__, this->process->pid, this->trdid, vpn, process->pid, remote_process_cxy ); |
---|
[433] | 1072 | #endif |
---|
| 1073 | |
---|
[408] | 1074 | // get extended pointer on remote gpt |
---|
| 1075 | remote_gpt_xp = XPTR( remote_process_cxy , &remote_process_ptr->vmm.gpt ); |
---|
| 1076 | |
---|
[433] | 1077 | // update remote GPT |
---|
| 1078 | hal_gpt_update_pte( remote_gpt_xp, vpn, attr, ppn ); |
---|
[408] | 1079 | } |
---|
| 1080 | |
---|
[640] | 1081 | #if DEBUG_VMM_GLOBAL_UPDATE_PTE |
---|
[433] | 1082 | cycle = (uint32_t)hal_get_cycles(); |
---|
[640] | 1083 | if( DEBUG_VMM_GLOBAL_UPDATE_PTE < cycle ) |
---|
[595] | 1084 | printk("\n[%s] thread[%x,%x] exit for process %x / vpn %x / cycle %d\n", |
---|
| 1085 | __FUNCTION__, this->process->pid, this->trdid, process->pid , vpn , cycle ); |
---|
[433] | 1086 | #endif |
---|
| 1087 | |
---|
[640] | 1088 | #if (DEBUG_VMM_GLOBAL_UPDATE_PTE & 1) |
---|
[635] | 1089 | hal_vmm_display( process , true ); |
---|
| 1090 | #endif |
---|
| 1091 | |
---|
[433] | 1092 | } // end vmm_global_update_pte() |
---|
| 1093 | |
---|
[408] | 1094 | /////////////////////////////////////// |
---|
| 1095 | void vmm_set_cow( process_t * process ) |
---|
| 1096 | { |
---|
| 1097 | vmm_t * vmm; |
---|
| 1098 | |
---|
| 1099 | xlist_entry_t * process_root_ptr; |
---|
| 1100 | xptr_t process_root_xp; |
---|
| 1101 | xptr_t process_iter_xp; |
---|
| 1102 | |
---|
| 1103 | xptr_t remote_process_xp; |
---|
| 1104 | cxy_t remote_process_cxy; |
---|
| 1105 | process_t * remote_process_ptr; |
---|
| 1106 | xptr_t remote_gpt_xp; |
---|
| 1107 | |
---|
| 1108 | xptr_t vseg_root_xp; |
---|
| 1109 | xptr_t vseg_iter_xp; |
---|
| 1110 | |
---|
| 1111 | xptr_t vseg_xp; |
---|
| 1112 | vseg_t * vseg; |
---|
| 1113 | |
---|
| 1114 | pid_t pid; |
---|
| 1115 | cxy_t owner_cxy; |
---|
| 1116 | lpid_t owner_lpid; |
---|
| 1117 | |
---|
[635] | 1118 | // get target process PID |
---|
| 1119 | pid = process->pid; |
---|
| 1120 | |
---|
[438] | 1121 | #if DEBUG_VMM_SET_COW |
---|
[595] | 1122 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
| 1123 | thread_t * this = CURRENT_THREAD; |
---|
[438] | 1124 | if( DEBUG_VMM_SET_COW < cycle ) |
---|
[595] | 1125 | printk("\n[%s] thread[%x,%x] enter for process %x / cycle %d\n", |
---|
[635] | 1126 | __FUNCTION__, this->process->pid, this->trdid, pid , cycle ); |
---|
[433] | 1127 | #endif |
---|
[408] | 1128 | |
---|
[635] | 1129 | #if (DEBUG_VMM_SET_COW & 1) |
---|
| 1130 | if( DEBUG_VMM_SET_COW < cycle ) |
---|
| 1131 | hal_vmm_display( process , true ); |
---|
| 1132 | #endif |
---|
| 1133 | |
---|
[567] | 1134 | // check cluster is reference |
---|
[635] | 1135 | assert( (XPTR( local_cxy , process ) == process->ref_xp), |
---|
| 1136 | "local cluster must be process reference cluster\n"); |
---|
[408] | 1137 | |
---|
| 1138 | // get pointer on reference VMM |
---|
| 1139 | vmm = &process->vmm; |
---|
| 1140 | |
---|
| 1141 | // get extended pointer on root of process copies xlist in owner cluster |
---|
| 1142 | owner_cxy = CXY_FROM_PID( pid ); |
---|
| 1143 | owner_lpid = LPID_FROM_PID( pid ); |
---|
| 1144 | process_root_ptr = &LOCAL_CLUSTER->pmgr.copies_root[owner_lpid]; |
---|
| 1145 | process_root_xp = XPTR( owner_cxy , process_root_ptr ); |
---|
| 1146 | |
---|
| 1147 | // get extended pointer on root of vsegs xlist from reference VMM |
---|
| 1148 | vseg_root_xp = XPTR( local_cxy , &vmm->vsegs_root ); |
---|
| 1149 | |
---|
[635] | 1150 | // loop on target process copies |
---|
[408] | 1151 | XLIST_FOREACH( process_root_xp , process_iter_xp ) |
---|
| 1152 | { |
---|
[635] | 1153 | // get cluster and local pointer on remote process copy |
---|
[408] | 1154 | remote_process_xp = XLIST_ELEMENT( process_iter_xp , process_t , copies_list ); |
---|
[433] | 1155 | remote_process_ptr = GET_PTR( remote_process_xp ); |
---|
[408] | 1156 | remote_process_cxy = GET_CXY( remote_process_xp ); |
---|
| 1157 | |
---|
[595] | 1158 | #if (DEBUG_VMM_SET_COW & 1) |
---|
[438] | 1159 | if( DEBUG_VMM_SET_COW < cycle ) |
---|
[635] | 1160 | printk("\n[%s] thread[%x,%x] (%x) handles process %x in cluster %x\n", |
---|
| 1161 | __FUNCTION__, this->process->pid, this->trdid, this, pid, remote_process_cxy ); |
---|
[433] | 1162 | #endif |
---|
[408] | 1163 | |
---|
| 1164 | // get extended pointer on remote gpt |
---|
| 1165 | remote_gpt_xp = XPTR( remote_process_cxy , &remote_process_ptr->vmm.gpt ); |
---|
| 1166 | |
---|
| 1167 | // loop on vsegs in (local) reference process VSL |
---|
| 1168 | XLIST_FOREACH( vseg_root_xp , vseg_iter_xp ) |
---|
| 1169 | { |
---|
| 1170 | // get pointer on vseg |
---|
| 1171 | vseg_xp = XLIST_ELEMENT( vseg_iter_xp , vseg_t , xlist ); |
---|
[433] | 1172 | vseg = GET_PTR( vseg_xp ); |
---|
[408] | 1173 | |
---|
| 1174 | // get vseg type, base and size |
---|
| 1175 | uint32_t type = vseg->type; |
---|
| 1176 | vpn_t vpn_base = vseg->vpn_base; |
---|
| 1177 | vpn_t vpn_size = vseg->vpn_size; |
---|
| 1178 | |
---|
[595] | 1179 | #if (DEBUG_VMM_SET_COW & 1) |
---|
[438] | 1180 | if( DEBUG_VMM_SET_COW < cycle ) |
---|
[635] | 1181 | printk("\n[%s] thread[%x,%x] found vseg %s / vpn_base = %x / vpn_size = %x\n", |
---|
[595] | 1182 | __FUNCTION__, this->process->pid, this->trdid, vseg_type_str(type), vpn_base, vpn_size ); |
---|
[433] | 1183 | #endif |
---|
| 1184 | // only DATA, ANON and REMOTE vsegs |
---|
[408] | 1185 | if( (type == VSEG_TYPE_DATA) || |
---|
| 1186 | (type == VSEG_TYPE_ANON) || |
---|
| 1187 | (type == VSEG_TYPE_REMOTE) ) |
---|
| 1188 | { |
---|
[433] | 1189 | vpn_t vpn; |
---|
| 1190 | uint32_t attr; |
---|
| 1191 | ppn_t ppn; |
---|
| 1192 | xptr_t page_xp; |
---|
| 1193 | cxy_t page_cxy; |
---|
| 1194 | page_t * page_ptr; |
---|
| 1195 | xptr_t forks_xp; |
---|
[469] | 1196 | xptr_t lock_xp; |
---|
[433] | 1197 | |
---|
| 1198 | // update flags in remote GPT |
---|
| 1199 | hal_gpt_set_cow( remote_gpt_xp, |
---|
| 1200 | vpn_base, |
---|
| 1201 | vpn_size ); |
---|
| 1202 | |
---|
| 1203 | // atomically increment pending forks counter in physical pages, |
---|
[635] | 1204 | // this is only done once, when handling the reference copy |
---|
[433] | 1205 | if( remote_process_cxy == local_cxy ) |
---|
| 1206 | { |
---|
[635] | 1207 | |
---|
| 1208 | #if (DEBUG_VMM_SET_COW & 1) |
---|
| 1209 | if( DEBUG_VMM_SET_COW < cycle ) |
---|
| 1210 | printk("\n[%s] thread[%x,%x] handles vseg %s / vpn_base = %x / vpn_size = %x\n", |
---|
| 1211 | __FUNCTION__, this->process->pid, this->trdid, vseg_type_str(type), vpn_base, vpn_size ); |
---|
| 1212 | #endif |
---|
[433] | 1213 | // scan all pages in vseg |
---|
| 1214 | for( vpn = vpn_base ; vpn < (vpn_base + vpn_size) ; vpn++ ) |
---|
| 1215 | { |
---|
| 1216 | // get page attributes and PPN from reference GPT |
---|
[585] | 1217 | hal_gpt_get_pte( remote_gpt_xp , vpn , &attr , &ppn ); |
---|
[433] | 1218 | |
---|
| 1219 | // atomically update pending forks counter if page is mapped |
---|
| 1220 | if( attr & GPT_MAPPED ) |
---|
| 1221 | { |
---|
[469] | 1222 | // get pointers and cluster on page descriptor |
---|
[433] | 1223 | page_xp = ppm_ppn2page( ppn ); |
---|
| 1224 | page_cxy = GET_CXY( page_xp ); |
---|
| 1225 | page_ptr = GET_PTR( page_xp ); |
---|
[469] | 1226 | |
---|
| 1227 | // get extended pointers on "forks" and "lock" |
---|
[433] | 1228 | forks_xp = XPTR( page_cxy , &page_ptr->forks ); |
---|
[469] | 1229 | lock_xp = XPTR( page_cxy , &page_ptr->lock ); |
---|
| 1230 | |
---|
[567] | 1231 | // take lock protecting "forks" counter |
---|
| 1232 | remote_busylock_acquire( lock_xp ); |
---|
| 1233 | |
---|
[469] | 1234 | // increment "forks" |
---|
[433] | 1235 | hal_remote_atomic_add( forks_xp , 1 ); |
---|
[567] | 1236 | |
---|
| 1237 | // release lock protecting "forks" counter |
---|
| 1238 | remote_busylock_release( lock_xp ); |
---|
[433] | 1239 | } |
---|
| 1240 | } // end loop on vpn |
---|
[635] | 1241 | |
---|
| 1242 | #if (DEBUG_VMM_SET_COW & 1) |
---|
| 1243 | if( DEBUG_VMM_SET_COW < cycle ) |
---|
| 1244 | printk("\n[%s] thread[%x,%x] completes vseg %s / vpn_base = %x / vpn_size = %x\n", |
---|
| 1245 | __FUNCTION__, this->process->pid, this->trdid, vseg_type_str(type), vpn_base, vpn_size ); |
---|
| 1246 | #endif |
---|
[433] | 1247 | } // end if local |
---|
| 1248 | } // end if vseg type |
---|
| 1249 | } // end loop on vsegs |
---|
[408] | 1250 | } // end loop on process copies |
---|
| 1251 | |
---|
[438] | 1252 | #if DEBUG_VMM_SET_COW |
---|
[433] | 1253 | cycle = (uint32_t)hal_get_cycles(); |
---|
[438] | 1254 | if( DEBUG_VMM_SET_COW < cycle ) |
---|
[595] | 1255 | printk("\n[%s] thread[%x,%x] exit for process %x / cycle %d\n", |
---|
| 1256 | __FUNCTION__, this->process->pid, this->trdid, process->pid , cycle ); |
---|
[433] | 1257 | #endif |
---|
[408] | 1258 | |
---|
| 1259 | } // end vmm_set-cow() |
---|
| 1260 | |
---|
| 1261 | ///////////////////////////////////////////////// |
---|
| 1262 | error_t vmm_fork_copy( process_t * child_process, |
---|
| 1263 | xptr_t parent_process_xp ) |
---|
| 1264 | { |
---|
| 1265 | error_t error; |
---|
| 1266 | cxy_t parent_cxy; |
---|
| 1267 | process_t * parent_process; |
---|
| 1268 | vmm_t * parent_vmm; |
---|
| 1269 | xptr_t parent_lock_xp; |
---|
| 1270 | vmm_t * child_vmm; |
---|
| 1271 | xptr_t iter_xp; |
---|
| 1272 | xptr_t parent_vseg_xp; |
---|
| 1273 | vseg_t * parent_vseg; |
---|
| 1274 | vseg_t * child_vseg; |
---|
| 1275 | uint32_t type; |
---|
| 1276 | vpn_t vpn; |
---|
| 1277 | vpn_t vpn_base; |
---|
| 1278 | vpn_t vpn_size; |
---|
| 1279 | xptr_t parent_root_xp; |
---|
| 1280 | bool_t mapped; |
---|
| 1281 | ppn_t ppn; |
---|
| 1282 | |
---|
[438] | 1283 | #if DEBUG_VMM_FORK_COPY |
---|
[433] | 1284 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
[595] | 1285 | thread_t * this = CURRENT_THREAD; |
---|
[438] | 1286 | if( DEBUG_VMM_FORK_COPY < cycle ) |
---|
[595] | 1287 | printk("\n[%s] thread %x enter / cycle %d\n", |
---|
| 1288 | __FUNCTION__ , this->process->pid, this->trdid, cycle ); |
---|
[433] | 1289 | #endif |
---|
[408] | 1290 | |
---|
| 1291 | // get parent process cluster and local pointer |
---|
| 1292 | parent_cxy = GET_CXY( parent_process_xp ); |
---|
[433] | 1293 | parent_process = GET_PTR( parent_process_xp ); |
---|
[408] | 1294 | |
---|
| 1295 | // get local pointers on parent and child VMM |
---|
| 1296 | parent_vmm = &parent_process->vmm; |
---|
| 1297 | child_vmm = &child_process->vmm; |
---|
| 1298 | |
---|
[625] | 1299 | // build extended pointer on parent VSL root and lock |
---|
[408] | 1300 | parent_root_xp = XPTR( parent_cxy , &parent_vmm->vsegs_root ); |
---|
[625] | 1301 | parent_lock_xp = XPTR( parent_cxy , &parent_vmm->vsl_lock ); |
---|
[408] | 1302 | |
---|
[640] | 1303 | // take the lock protecting the parent VSL |
---|
| 1304 | remote_queuelock_acquire( parent_lock_xp ); |
---|
[415] | 1305 | |
---|
[408] | 1306 | // loop on parent VSL xlist |
---|
| 1307 | XLIST_FOREACH( parent_root_xp , iter_xp ) |
---|
[23] | 1308 | { |
---|
[625] | 1309 | // get pointers on current parent vseg |
---|
[408] | 1310 | parent_vseg_xp = XLIST_ELEMENT( iter_xp , vseg_t , xlist ); |
---|
[433] | 1311 | parent_vseg = GET_PTR( parent_vseg_xp ); |
---|
[23] | 1312 | |
---|
[408] | 1313 | // get vseg type |
---|
[567] | 1314 | type = hal_remote_l32( XPTR( parent_cxy , &parent_vseg->type ) ); |
---|
[408] | 1315 | |
---|
[438] | 1316 | #if DEBUG_VMM_FORK_COPY |
---|
[433] | 1317 | cycle = (uint32_t)hal_get_cycles(); |
---|
[438] | 1318 | if( DEBUG_VMM_FORK_COPY < cycle ) |
---|
[595] | 1319 | printk("\n[%s] thread[%x,%x] found parent vseg %s / vpn_base = %x / cycle %d\n", |
---|
| 1320 | __FUNCTION__ , this->process->pid, this->trdid, vseg_type_str(type), |
---|
[567] | 1321 | hal_remote_l32( XPTR( parent_cxy , &parent_vseg->vpn_base ) ) , cycle ); |
---|
[433] | 1322 | #endif |
---|
[23] | 1323 | |
---|
[623] | 1324 | // all parent vsegs - but STACK and kernel vsegs - must be copied in child VSL |
---|
| 1325 | if( (type != VSEG_TYPE_STACK) && (type != VSEG_TYPE_KCODE) && |
---|
| 1326 | (type != VSEG_TYPE_KDATA) && (type != VSEG_TYPE_KDEV) ) |
---|
[23] | 1327 | { |
---|
[408] | 1328 | // allocate memory for a new child vseg |
---|
| 1329 | child_vseg = vseg_alloc(); |
---|
| 1330 | if( child_vseg == NULL ) // release all allocated vsegs |
---|
[23] | 1331 | { |
---|
[408] | 1332 | vmm_destroy( child_process ); |
---|
| 1333 | printk("\n[ERROR] in %s : cannot create vseg for child\n", __FUNCTION__ ); |
---|
| 1334 | return -1; |
---|
[23] | 1335 | } |
---|
| 1336 | |
---|
[408] | 1337 | // copy parent vseg to child vseg |
---|
| 1338 | vseg_init_from_ref( child_vseg , parent_vseg_xp ); |
---|
[23] | 1339 | |
---|
[640] | 1340 | // build extended pointer on child VSL lock |
---|
| 1341 | xptr_t child_lock_xp = XPTR( local_cxy , &child_vmm->vsl_lock ); |
---|
[625] | 1342 | |
---|
[640] | 1343 | // take the child VSL lock |
---|
| 1344 | remote_queuelock_acquire( child_lock_xp ); |
---|
[625] | 1345 | |
---|
[408] | 1346 | // register child vseg in child VSL |
---|
[611] | 1347 | vmm_attach_vseg_to_vsl( child_vmm , child_vseg ); |
---|
[407] | 1348 | |
---|
[640] | 1349 | // release the child VSL lock |
---|
| 1350 | remote_queuelock_release( child_lock_xp ); |
---|
[625] | 1351 | |
---|
[438] | 1352 | #if DEBUG_VMM_FORK_COPY |
---|
[433] | 1353 | cycle = (uint32_t)hal_get_cycles(); |
---|
[438] | 1354 | if( DEBUG_VMM_FORK_COPY < cycle ) |
---|
[595] | 1355 | printk("\n[%s] thread[%x,%x] copied vseg %s / vpn_base = %x to child VSL / cycle %d\n", |
---|
| 1356 | __FUNCTION__ , this->process->pid, this->trdid, vseg_type_str(type), |
---|
[567] | 1357 | hal_remote_l32( XPTR( parent_cxy , &parent_vseg->vpn_base ) ) , cycle ); |
---|
[433] | 1358 | #endif |
---|
[625] | 1359 | // copy DATA, ANON, REMOTE, FILE parent GPT entries to child GPT |
---|
[408] | 1360 | if( type != VSEG_TYPE_CODE ) |
---|
| 1361 | { |
---|
[625] | 1362 | // activate the COW for DATA, ANON, REMOTE vsegs only |
---|
[635] | 1363 | // cow = ( type != VSEG_TYPE_FILE ); |
---|
[23] | 1364 | |
---|
[408] | 1365 | vpn_base = child_vseg->vpn_base; |
---|
| 1366 | vpn_size = child_vseg->vpn_size; |
---|
[23] | 1367 | |
---|
[408] | 1368 | // scan pages in parent vseg |
---|
| 1369 | for( vpn = vpn_base ; vpn < (vpn_base + vpn_size) ; vpn++ ) |
---|
| 1370 | { |
---|
| 1371 | error = hal_gpt_pte_copy( &child_vmm->gpt, |
---|
[625] | 1372 | vpn, |
---|
[408] | 1373 | XPTR( parent_cxy , &parent_vmm->gpt ), |
---|
| 1374 | vpn, |
---|
[635] | 1375 | false, // does not handle COW flag |
---|
| 1376 | &ppn, // unused |
---|
| 1377 | &mapped ); // unused |
---|
[408] | 1378 | if( error ) |
---|
| 1379 | { |
---|
| 1380 | vmm_destroy( child_process ); |
---|
| 1381 | printk("\n[ERROR] in %s : cannot copy GPT\n", __FUNCTION__ ); |
---|
| 1382 | return -1; |
---|
| 1383 | } |
---|
| 1384 | |
---|
[438] | 1385 | #if DEBUG_VMM_FORK_COPY |
---|
[433] | 1386 | cycle = (uint32_t)hal_get_cycles(); |
---|
[438] | 1387 | if( DEBUG_VMM_FORK_COPY < cycle ) |
---|
[595] | 1388 | printk("\n[%s] thread[%x,%x] copied vpn %x to child GPT / cycle %d\n", |
---|
| 1389 | __FUNCTION__ , this->process->pid, this->trdid , vpn , cycle ); |
---|
[433] | 1390 | #endif |
---|
[408] | 1391 | } |
---|
| 1392 | } // end if no code & no stack |
---|
| 1393 | } // end if no stack |
---|
| 1394 | } // end loop on vsegs |
---|
| 1395 | |
---|
[567] | 1396 | // release the parent VSL lock in read mode |
---|
[640] | 1397 | remote_queuelock_release( parent_lock_xp ); |
---|
[408] | 1398 | |
---|
[651] | 1399 | /* deprecated [AG] : this is already done by the vmm_user_init() funcfion |
---|
| 1400 | |
---|
[408] | 1401 | // initialize the child VMM STACK allocator |
---|
[651] | 1402 | vmm_stack_init( child_vmm ); |
---|
[408] | 1403 | |
---|
| 1404 | // initialize the child VMM MMAP allocator |
---|
[651] | 1405 | vmm_mmap_init( child_vmm ); |
---|
[23] | 1406 | |
---|
[178] | 1407 | // initialize instrumentation counters |
---|
[635] | 1408 | child_vmm->false_pgfault_nr = 0; |
---|
| 1409 | child_vmm->local_pgfault_nr = 0; |
---|
| 1410 | child_vmm->global_pgfault_nr = 0; |
---|
| 1411 | child_vmm->false_pgfault_cost = 0; |
---|
| 1412 | child_vmm->local_pgfault_cost = 0; |
---|
| 1413 | child_vmm->global_pgfault_cost = 0; |
---|
[651] | 1414 | */ |
---|
[408] | 1415 | // copy base addresses from parent VMM to child VMM |
---|
| 1416 | child_vmm->args_vpn_base = (vpn_t)hal_remote_lpt(XPTR(parent_cxy, &parent_vmm->args_vpn_base)); |
---|
| 1417 | child_vmm->envs_vpn_base = (vpn_t)hal_remote_lpt(XPTR(parent_cxy, &parent_vmm->envs_vpn_base)); |
---|
| 1418 | child_vmm->heap_vpn_base = (vpn_t)hal_remote_lpt(XPTR(parent_cxy, &parent_vmm->heap_vpn_base)); |
---|
| 1419 | child_vmm->code_vpn_base = (vpn_t)hal_remote_lpt(XPTR(parent_cxy, &parent_vmm->code_vpn_base)); |
---|
| 1420 | child_vmm->data_vpn_base = (vpn_t)hal_remote_lpt(XPTR(parent_cxy, &parent_vmm->data_vpn_base)); |
---|
[23] | 1421 | |
---|
[408] | 1422 | child_vmm->entry_point = (intptr_t)hal_remote_lpt(XPTR(parent_cxy, &parent_vmm->entry_point)); |
---|
[23] | 1423 | |
---|
[124] | 1424 | hal_fence(); |
---|
[23] | 1425 | |
---|
[438] | 1426 | #if DEBUG_VMM_FORK_COPY |
---|
[433] | 1427 | cycle = (uint32_t)hal_get_cycles(); |
---|
[438] | 1428 | if( DEBUG_VMM_FORK_COPY < cycle ) |
---|
[595] | 1429 | printk("\n[%s] thread[%x,%x] exit successfully / cycle %d\n", |
---|
| 1430 | __FUNCTION__ , this->process->pid, this->trdid , cycle ); |
---|
[433] | 1431 | #endif |
---|
| 1432 | |
---|
[23] | 1433 | return 0; |
---|
| 1434 | |
---|
[408] | 1435 | } // vmm_fork_copy() |
---|
[204] | 1436 | |
---|
[1] | 1437 | /////////////////////////////////////// |
---|
| 1438 | void vmm_destroy( process_t * process ) |
---|
| 1439 | { |
---|
[408] | 1440 | xptr_t vseg_xp; |
---|
[1] | 1441 | vseg_t * vseg; |
---|
| 1442 | |
---|
[438] | 1443 | #if DEBUG_VMM_DESTROY |
---|
[635] | 1444 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
| 1445 | thread_t * this = CURRENT_THREAD; |
---|
[438] | 1446 | if( DEBUG_VMM_DESTROY < cycle ) |
---|
[595] | 1447 | printk("\n[%s] thread[%x,%x] enter for process %x in cluster %x / cycle %d\n", |
---|
| 1448 | __FUNCTION__, this->process->pid, this->trdid, process->pid, local_cxy, cycle ); |
---|
[433] | 1449 | #endif |
---|
[416] | 1450 | |
---|
[438] | 1451 | #if (DEBUG_VMM_DESTROY & 1 ) |
---|
[443] | 1452 | if( DEBUG_VMM_DESTROY < cycle ) |
---|
[635] | 1453 | hal_vmm_display( XPTR( local_cxy, process ) , true ); |
---|
[437] | 1454 | #endif |
---|
| 1455 | |
---|
[433] | 1456 | // get pointer on local VMM |
---|
[1] | 1457 | vmm_t * vmm = &process->vmm; |
---|
| 1458 | |
---|
[625] | 1459 | // build extended pointer on VSL root, VSL lock and GPT lock |
---|
| 1460 | xptr_t vsl_root_xp = XPTR( local_cxy , &vmm->vsegs_root ); |
---|
| 1461 | xptr_t vsl_lock_xp = XPTR( local_cxy , &vmm->vsl_lock ); |
---|
[408] | 1462 | |
---|
[625] | 1463 | // take the VSL lock |
---|
[640] | 1464 | remote_queuelock_acquire( vsl_lock_xp ); |
---|
[625] | 1465 | |
---|
[611] | 1466 | // scan the VSL to delete all registered vsegs |
---|
[625] | 1467 | // (we don't use a FOREACH in case of item deletion) |
---|
| 1468 | xptr_t iter_xp; |
---|
| 1469 | xptr_t next_xp; |
---|
| 1470 | for( iter_xp = hal_remote_l64( vsl_root_xp ) ; |
---|
| 1471 | iter_xp != vsl_root_xp ; |
---|
| 1472 | iter_xp = next_xp ) |
---|
[1] | 1473 | { |
---|
[625] | 1474 | // save extended pointer on next item in xlist |
---|
| 1475 | next_xp = hal_remote_l64( iter_xp ); |
---|
[409] | 1476 | |
---|
[625] | 1477 | // get pointers on current vseg in VSL |
---|
| 1478 | vseg_xp = XLIST_ELEMENT( iter_xp , vseg_t , xlist ); |
---|
| 1479 | vseg = GET_PTR( vseg_xp ); |
---|
| 1480 | |
---|
[611] | 1481 | // delete vseg and release physical pages |
---|
[625] | 1482 | vmm_remove_vseg( process , vseg ); |
---|
[409] | 1483 | |
---|
[443] | 1484 | #if( DEBUG_VMM_DESTROY & 1 ) |
---|
| 1485 | if( DEBUG_VMM_DESTROY < cycle ) |
---|
[611] | 1486 | printk("\n[%s] %s vseg deleted / vpn_base %x / vpn_size %d\n", |
---|
[443] | 1487 | __FUNCTION__ , vseg_type_str( vseg->type ), vseg->vpn_base, vseg->vpn_size ); |
---|
| 1488 | #endif |
---|
| 1489 | |
---|
[1] | 1490 | } |
---|
| 1491 | |
---|
[625] | 1492 | // release the VSL lock |
---|
[640] | 1493 | remote_queuelock_release( vsl_lock_xp ); |
---|
[625] | 1494 | |
---|
[651] | 1495 | // remove all registered MMAP vsegs from free_lists in MMAP allocator |
---|
[1] | 1496 | uint32_t i; |
---|
[651] | 1497 | for( i = 0 ; i <= CONFIG_VMM_HEAP_MAX_ORDER ; i++ ) |
---|
[1] | 1498 | { |
---|
[651] | 1499 | // build extended pointer on free list root |
---|
| 1500 | xptr_t root_xp = XPTR( local_cxy , &vmm->mmap_mgr.free_list_root[i] ); |
---|
[625] | 1501 | |
---|
| 1502 | // scan zombi_list[i] |
---|
| 1503 | while( !xlist_is_empty( root_xp ) ) |
---|
[1] | 1504 | { |
---|
[625] | 1505 | vseg_xp = XLIST_FIRST( root_xp , vseg_t , xlist ); |
---|
| 1506 | vseg = GET_PTR( vseg_xp ); |
---|
[443] | 1507 | |
---|
| 1508 | #if( DEBUG_VMM_DESTROY & 1 ) |
---|
| 1509 | if( DEBUG_VMM_DESTROY < cycle ) |
---|
[595] | 1510 | printk("\n[%s] found zombi vseg / vpn_base %x / vpn_size %d\n", |
---|
[443] | 1511 | __FUNCTION__ , vseg_type_str( vseg->type ), vseg->vpn_base, vseg->vpn_size ); |
---|
| 1512 | #endif |
---|
[611] | 1513 | // clean vseg descriptor |
---|
| 1514 | vseg->vmm = NULL; |
---|
| 1515 | |
---|
[625] | 1516 | // remove vseg from zombi_list |
---|
[611] | 1517 | xlist_unlink( XPTR( local_cxy , &vseg->xlist ) ); |
---|
| 1518 | |
---|
| 1519 | // release vseg descriptor |
---|
[1] | 1520 | vseg_free( vseg ); |
---|
[443] | 1521 | |
---|
| 1522 | #if( DEBUG_VMM_DESTROY & 1 ) |
---|
| 1523 | if( DEBUG_VMM_DESTROY < cycle ) |
---|
[595] | 1524 | printk("\n[%s] zombi vseg released / vpn_base %x / vpn_size %d\n", |
---|
[443] | 1525 | __FUNCTION__ , vseg_type_str( vseg->type ), vseg->vpn_base, vseg->vpn_size ); |
---|
| 1526 | #endif |
---|
[1] | 1527 | } |
---|
| 1528 | } |
---|
| 1529 | |
---|
[409] | 1530 | // release memory allocated to the GPT itself |
---|
[1] | 1531 | hal_gpt_destroy( &vmm->gpt ); |
---|
| 1532 | |
---|
[438] | 1533 | #if DEBUG_VMM_DESTROY |
---|
[433] | 1534 | cycle = (uint32_t)hal_get_cycles(); |
---|
[438] | 1535 | if( DEBUG_VMM_DESTROY < cycle ) |
---|
[595] | 1536 | printk("\n[%s] thread[%x,%x] exit for process %x in cluster %x / cycle %d\n", |
---|
| 1537 | __FUNCTION__, this->process->pid, this->trdid, process->pid, local_cxy , cycle ); |
---|
[433] | 1538 | #endif |
---|
[416] | 1539 | |
---|
[204] | 1540 | } // end vmm_destroy() |
---|
| 1541 | |
---|
[1] | 1542 | ///////////////////////////////////////////////// |
---|
| 1543 | vseg_t * vmm_check_conflict( process_t * process, |
---|
[21] | 1544 | vpn_t vpn_base, |
---|
[1] | 1545 | vpn_t vpn_size ) |
---|
| 1546 | { |
---|
| 1547 | vmm_t * vmm = &process->vmm; |
---|
[408] | 1548 | |
---|
| 1549 | // scan the VSL |
---|
[1] | 1550 | vseg_t * vseg; |
---|
[408] | 1551 | xptr_t iter_xp; |
---|
| 1552 | xptr_t vseg_xp; |
---|
| 1553 | xptr_t root_xp = XPTR( local_cxy , &vmm->vsegs_root ); |
---|
[1] | 1554 | |
---|
[408] | 1555 | XLIST_FOREACH( root_xp , iter_xp ) |
---|
[1] | 1556 | { |
---|
[408] | 1557 | vseg_xp = XLIST_ELEMENT( iter_xp , vseg_t , xlist ); |
---|
[433] | 1558 | vseg = GET_PTR( vseg_xp ); |
---|
[204] | 1559 | |
---|
[21] | 1560 | if( ((vpn_base + vpn_size) > vseg->vpn_base) && |
---|
| 1561 | (vpn_base < (vseg->vpn_base + vseg->vpn_size)) ) return vseg; |
---|
[1] | 1562 | } |
---|
| 1563 | return NULL; |
---|
| 1564 | |
---|
[204] | 1565 | } // end vmm_check_conflict() |
---|
| 1566 | |
---|
[407] | 1567 | //////////////////////////////////////////////// |
---|
| 1568 | vseg_t * vmm_create_vseg( process_t * process, |
---|
| 1569 | vseg_type_t type, |
---|
[635] | 1570 | intptr_t base, // ltid for VSEG_TYPE_STACK |
---|
[407] | 1571 | uint32_t size, |
---|
| 1572 | uint32_t file_offset, |
---|
| 1573 | uint32_t file_size, |
---|
| 1574 | xptr_t mapper_xp, |
---|
| 1575 | cxy_t cxy ) |
---|
[1] | 1576 | { |
---|
[651] | 1577 | vseg_t * vseg; // pointer on allocated vseg descriptor |
---|
[1] | 1578 | |
---|
[640] | 1579 | #if DEBUG_VMM_CREATE_VSEG |
---|
| 1580 | thread_t * this = CURRENT_THREAD; |
---|
| 1581 | uint32_t cycle; |
---|
| 1582 | #endif |
---|
| 1583 | |
---|
[635] | 1584 | #if (DEBUG_VMM_CREATE_VSEG & 1) |
---|
[640] | 1585 | cycle = (uint32_t)hal_get_cycles(); |
---|
[438] | 1586 | if( DEBUG_VMM_CREATE_VSEG < cycle ) |
---|
[635] | 1587 | printk("\n[%s] thread[%x,%x] enter / process %x / %s / base %x / cxy %x / cycle %d\n", |
---|
| 1588 | __FUNCTION__, this->process->pid, this->trdid, |
---|
| 1589 | process->pid, vseg_type_str(type), base, cxy, cycle ); |
---|
[433] | 1590 | #endif |
---|
[21] | 1591 | |
---|
[407] | 1592 | // get pointer on VMM |
---|
| 1593 | vmm_t * vmm = &process->vmm; |
---|
[21] | 1594 | |
---|
[651] | 1595 | // allocate a vseg descriptor and initialize it, depending on type |
---|
| 1596 | // we use specific allocators for "stack" and "mmap" types |
---|
[595] | 1597 | |
---|
[651] | 1598 | ///////////////////////////// |
---|
[1] | 1599 | if( type == VSEG_TYPE_STACK ) |
---|
| 1600 | { |
---|
[651] | 1601 | // get vseg from STACK allocator |
---|
| 1602 | vseg = vmm_stack_alloc( vmm , base ); // base == ltid |
---|
| 1603 | |
---|
| 1604 | if( vseg == NULL ) |
---|
| 1605 | { |
---|
| 1606 | printk("\n[ERROR] %s cannot create %s vseg for process %x in cluster %x\n", |
---|
| 1607 | __FUNCTION__ , vseg_type_str( type ) , process->pid , local_cxy ); |
---|
| 1608 | return NULL; |
---|
| 1609 | } |
---|
[1] | 1610 | |
---|
[651] | 1611 | // initialize vseg |
---|
| 1612 | vseg->type = type; |
---|
| 1613 | vseg->vmm = vmm; |
---|
| 1614 | vseg->min = vseg->vpn_base << CONFIG_PPM_PAGE_SHIFT; |
---|
| 1615 | vseg->max = vseg->min + (vseg->vpn_size << CONFIG_PPM_PAGE_SHIFT); |
---|
| 1616 | vseg->cxy = cxy; |
---|
| 1617 | |
---|
| 1618 | vseg_init_flags( vseg , type ); |
---|
[1] | 1619 | } |
---|
[651] | 1620 | ///////////////////////////////// |
---|
[595] | 1621 | else if( type == VSEG_TYPE_FILE ) |
---|
| 1622 | { |
---|
[651] | 1623 | // compute page index (in mapper) for first and last byte |
---|
[595] | 1624 | vpn_t vpn_min = file_offset >> CONFIG_PPM_PAGE_SHIFT; |
---|
| 1625 | vpn_t vpn_max = (file_offset + size - 1) >> CONFIG_PPM_PAGE_SHIFT; |
---|
| 1626 | |
---|
[651] | 1627 | // compute offset in first page and number of pages |
---|
[595] | 1628 | uint32_t offset = file_offset & CONFIG_PPM_PAGE_MASK; |
---|
| 1629 | vpn_t npages = vpn_max - vpn_min + 1; |
---|
| 1630 | |
---|
[651] | 1631 | // get vseg from MMAP allocator |
---|
| 1632 | vseg = vmm_mmap_alloc( vmm , npages ); |
---|
| 1633 | |
---|
| 1634 | if( vseg == NULL ) |
---|
[595] | 1635 | { |
---|
[651] | 1636 | printk("\n[ERROR] %s cannot create %s vseg for process %x in cluster %x\n", |
---|
| 1637 | __FUNCTION__ , vseg_type_str( type ) , process->pid , local_cxy ); |
---|
[595] | 1638 | return NULL; |
---|
| 1639 | } |
---|
| 1640 | |
---|
[651] | 1641 | // initialize vseg |
---|
| 1642 | vseg->type = type; |
---|
| 1643 | vseg->vmm = vmm; |
---|
| 1644 | vseg->min = (vseg->vpn_base << CONFIG_PPM_PAGE_SHIFT) + offset; |
---|
| 1645 | vseg->max = vseg->min + size; |
---|
| 1646 | vseg->file_offset = file_offset; |
---|
| 1647 | vseg->file_size = file_size; |
---|
| 1648 | vseg->mapper_xp = mapper_xp; |
---|
| 1649 | vseg->cxy = cxy; |
---|
| 1650 | |
---|
| 1651 | vseg_init_flags( vseg , type ); |
---|
[595] | 1652 | } |
---|
[651] | 1653 | ///////////////////////////////////////////////////////////////// |
---|
| 1654 | else if( (type == VSEG_TYPE_ANON) || (type == VSEG_TYPE_REMOTE) ) |
---|
[1] | 1655 | { |
---|
[595] | 1656 | // compute number of required pages in virtual space |
---|
| 1657 | vpn_t npages = size >> CONFIG_PPM_PAGE_SHIFT; |
---|
| 1658 | if( size & CONFIG_PPM_PAGE_MASK) npages++; |
---|
| 1659 | |
---|
[651] | 1660 | // allocate vseg from MMAP allocator |
---|
| 1661 | vseg = vmm_mmap_alloc( vmm , npages ); |
---|
| 1662 | |
---|
| 1663 | if( vseg == NULL ) |
---|
[1] | 1664 | { |
---|
[651] | 1665 | printk("\n[ERROR] %s cannot create %s vseg for process %x in cluster %x\n", |
---|
| 1666 | __FUNCTION__ , vseg_type_str( type ) , process->pid , local_cxy ); |
---|
[1] | 1667 | return NULL; |
---|
| 1668 | } |
---|
| 1669 | |
---|
[651] | 1670 | // initialize vseg |
---|
| 1671 | vseg->type = type; |
---|
| 1672 | vseg->vmm = vmm; |
---|
| 1673 | vseg->min = vseg->vpn_base << CONFIG_PPM_PAGE_SHIFT; |
---|
| 1674 | vseg->max = vseg->min + (vseg->vpn_size << CONFIG_PPM_PAGE_SHIFT); |
---|
| 1675 | vseg->cxy = cxy; |
---|
| 1676 | |
---|
| 1677 | vseg_init_flags( vseg , type ); |
---|
[1] | 1678 | } |
---|
[651] | 1679 | ///////////////////////////////////////////////////////////////// |
---|
[623] | 1680 | else // VSEG_TYPE_DATA, VSEG_TYPE_CODE or KERNEL vseg |
---|
[1] | 1681 | { |
---|
[204] | 1682 | uint32_t vpn_min = base >> CONFIG_PPM_PAGE_SHIFT; |
---|
| 1683 | uint32_t vpn_max = (base + size - 1) >> CONFIG_PPM_PAGE_SHIFT; |
---|
| 1684 | |
---|
[651] | 1685 | // allocate vseg descriptor |
---|
| 1686 | vseg = vseg_alloc(); |
---|
| 1687 | |
---|
| 1688 | if( vseg == NULL ) |
---|
| 1689 | { |
---|
| 1690 | printk("\n[ERROR] %s cannot create %s vseg for process %x in cluster %x\n", |
---|
| 1691 | __FUNCTION__ , vseg_type_str( type ) , process->pid , local_cxy ); |
---|
| 1692 | return NULL; |
---|
| 1693 | } |
---|
| 1694 | // initialize vseg |
---|
| 1695 | vseg->type = type; |
---|
| 1696 | vseg->vmm = vmm; |
---|
| 1697 | vseg->min = base; |
---|
| 1698 | vseg->max = base + size; |
---|
| 1699 | vseg->vpn_base = base >> CONFIG_PPM_PAGE_SHIFT; |
---|
| 1700 | vseg->vpn_size = vpn_max - vpn_min + 1; |
---|
| 1701 | vseg->file_offset = file_offset; |
---|
| 1702 | vseg->file_size = file_size; |
---|
| 1703 | vseg->mapper_xp = mapper_xp; |
---|
| 1704 | vseg->cxy = cxy; |
---|
| 1705 | |
---|
| 1706 | vseg_init_flags( vseg , type ); |
---|
[1] | 1707 | } |
---|
| 1708 | |
---|
| 1709 | // check collisions |
---|
[651] | 1710 | vseg_t * existing_vseg = vmm_check_conflict( process , vseg->vpn_base , vseg->vpn_size ); |
---|
[624] | 1711 | |
---|
[651] | 1712 | if( existing_vseg != NULL ) |
---|
[1] | 1713 | { |
---|
[651] | 1714 | printk("\n[ERROR] in %s for process %x : new vseg %s [vpn_base %x / vpn_size %x]\n" |
---|
| 1715 | " overlap existing vseg %s [vpn_base %x / vpn_size %x]\n", |
---|
| 1716 | __FUNCTION__ , process->pid, vseg_type_str(vseg->type), vseg->vpn_base, vseg->vpn_size, |
---|
| 1717 | vseg_type_str(existing_vseg->type), existing_vseg->vpn_base, existing_vseg->vpn_size ); |
---|
| 1718 | vseg_free( vseg ); |
---|
[1] | 1719 | return NULL; |
---|
| 1720 | } |
---|
| 1721 | |
---|
[625] | 1722 | // build extended pointer on VSL lock |
---|
| 1723 | xptr_t lock_xp = XPTR( local_cxy , &vmm->vsl_lock ); |
---|
| 1724 | |
---|
| 1725 | // take the VSL lock in write mode |
---|
[640] | 1726 | remote_queuelock_acquire( lock_xp ); |
---|
[625] | 1727 | |
---|
[408] | 1728 | // attach vseg to VSL |
---|
[611] | 1729 | vmm_attach_vseg_to_vsl( vmm , vseg ); |
---|
[1] | 1730 | |
---|
[625] | 1731 | // release the VSL lock |
---|
[640] | 1732 | remote_queuelock_release( lock_xp ); |
---|
[625] | 1733 | |
---|
[651] | 1734 | #if DEBUG_VMM_CREATE_VSEG |
---|
[433] | 1735 | cycle = (uint32_t)hal_get_cycles(); |
---|
[651] | 1736 | if( DEBUG_VMM_CREATE_VSEG < cycle ) |
---|
| 1737 | printk("\n[%s] thread[%x,%x] exit / %s / vpn_base %x / vpn_size %x / cycle %d\n", |
---|
| 1738 | __FUNCTION__, this->process->pid, this->trdid, |
---|
| 1739 | vseg_type_str(type), vseg->vpn_base, vseg->vpn_size, cycle ); |
---|
[433] | 1740 | #endif |
---|
[21] | 1741 | |
---|
[1] | 1742 | return vseg; |
---|
| 1743 | |
---|
[406] | 1744 | } // vmm_create_vseg() |
---|
| 1745 | |
---|
[640] | 1746 | //////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 1747 | // This static function is called by the vmm_remove_vseg() and vmm_resize_vseg() functions. |
---|
| 1748 | // Depending on the vseg <type>, it decrements the physical page refcount, and |
---|
| 1749 | // conditionnally release to the relevant kmem the physical page identified by <ppn>. |
---|
| 1750 | //////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 1751 | // @ process : local pointer on process. |
---|
| 1752 | // @ vseg : local pointer on vseg. |
---|
| 1753 | // @ ppn : released pysical page index. |
---|
| 1754 | //////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 1755 | static void vmm_ppn_release( process_t * process, |
---|
| 1756 | vseg_t * vseg, |
---|
| 1757 | ppn_t ppn ) |
---|
| 1758 | { |
---|
| 1759 | bool_t do_release; |
---|
[625] | 1760 | |
---|
[640] | 1761 | // get vseg type |
---|
| 1762 | vseg_type_t type = vseg->type; |
---|
| 1763 | |
---|
| 1764 | // compute is_ref |
---|
| 1765 | bool_t is_ref = (GET_CXY( process->ref_xp ) == local_cxy); |
---|
| 1766 | |
---|
| 1767 | // get pointers on physical page descriptor |
---|
| 1768 | xptr_t page_xp = ppm_ppn2page( ppn ); |
---|
| 1769 | cxy_t page_cxy = GET_CXY( page_xp ); |
---|
| 1770 | page_t * page_ptr = GET_PTR( page_xp ); |
---|
| 1771 | |
---|
| 1772 | // decrement page refcount |
---|
| 1773 | xptr_t count_xp = XPTR( page_cxy , &page_ptr->refcount ); |
---|
| 1774 | hal_remote_atomic_add( count_xp , -1 ); |
---|
| 1775 | |
---|
| 1776 | // compute the do_release condition depending on vseg type |
---|
| 1777 | if( (type == VSEG_TYPE_FILE) || |
---|
| 1778 | (type == VSEG_TYPE_KCODE) || |
---|
| 1779 | (type == VSEG_TYPE_KDATA) || |
---|
| 1780 | (type == VSEG_TYPE_KDEV) ) |
---|
| 1781 | { |
---|
| 1782 | // no physical page release for FILE and KERNEL |
---|
| 1783 | do_release = false; |
---|
| 1784 | } |
---|
| 1785 | else if( (type == VSEG_TYPE_CODE) || |
---|
| 1786 | (type == VSEG_TYPE_STACK) ) |
---|
| 1787 | { |
---|
| 1788 | // always release physical page for private vsegs |
---|
| 1789 | do_release = true; |
---|
| 1790 | } |
---|
| 1791 | else if( (type == VSEG_TYPE_ANON) || |
---|
| 1792 | (type == VSEG_TYPE_REMOTE) ) |
---|
| 1793 | { |
---|
| 1794 | // release physical page if reference cluster |
---|
| 1795 | do_release = is_ref; |
---|
| 1796 | } |
---|
| 1797 | else if( is_ref ) // vseg_type == DATA in reference cluster |
---|
| 1798 | { |
---|
| 1799 | // get extended pointers on forks and lock field in page descriptor |
---|
| 1800 | xptr_t forks_xp = XPTR( page_cxy , &page_ptr->forks ); |
---|
| 1801 | xptr_t lock_xp = XPTR( page_cxy , &page_ptr->lock ); |
---|
| 1802 | |
---|
| 1803 | // take lock protecting "forks" counter |
---|
| 1804 | remote_busylock_acquire( lock_xp ); |
---|
| 1805 | |
---|
| 1806 | // get number of pending forks from page descriptor |
---|
| 1807 | uint32_t forks = hal_remote_l32( forks_xp ); |
---|
| 1808 | |
---|
| 1809 | // decrement pending forks counter if required |
---|
| 1810 | if( forks ) hal_remote_atomic_add( forks_xp , -1 ); |
---|
| 1811 | |
---|
| 1812 | // release lock protecting "forks" counter |
---|
| 1813 | remote_busylock_release( lock_xp ); |
---|
| 1814 | |
---|
| 1815 | // release physical page if forks == 0 |
---|
| 1816 | do_release = (forks == 0); |
---|
| 1817 | } |
---|
| 1818 | else // vseg_type == DATA not in reference cluster |
---|
| 1819 | { |
---|
| 1820 | // no physical page release if not in reference cluster |
---|
| 1821 | do_release = false; |
---|
| 1822 | } |
---|
| 1823 | |
---|
| 1824 | // release physical page to relevant kmem when required |
---|
| 1825 | if( do_release ) |
---|
| 1826 | { |
---|
| 1827 | ppm_remote_free_pages( page_cxy , page_ptr ); |
---|
| 1828 | |
---|
| 1829 | #if DEBUG_VMM_PPN_RELEASE |
---|
| 1830 | thread_t * this = CURRENT_THREAD; |
---|
| 1831 | if( DEBUG_VMM_PPN_RELEASE < cycle ) |
---|
| 1832 | printk("\n[%s] thread[%x,%x] released ppn %x to kmem\n", |
---|
| 1833 | __FUNCTION__, this->process->pid, this->trdid, ppn ); |
---|
| 1834 | #endif |
---|
| 1835 | |
---|
| 1836 | } |
---|
| 1837 | } // end vmm_ppn_release() |
---|
| 1838 | |
---|
[625] | 1839 | ////////////////////////////////////////// |
---|
| 1840 | void vmm_remove_vseg( process_t * process, |
---|
| 1841 | vseg_t * vseg ) |
---|
[1] | 1842 | { |
---|
[625] | 1843 | uint32_t vseg_type; // vseg type |
---|
[21] | 1844 | vpn_t vpn; // VPN of current PTE |
---|
| 1845 | vpn_t vpn_min; // VPN of first PTE |
---|
[1] | 1846 | vpn_t vpn_max; // VPN of last PTE (excluded) |
---|
[409] | 1847 | ppn_t ppn; // current PTE ppn value |
---|
| 1848 | uint32_t attr; // current PTE attributes |
---|
[1] | 1849 | |
---|
[625] | 1850 | // check arguments |
---|
| 1851 | assert( (process != NULL), "process argument is NULL" ); |
---|
| 1852 | assert( (vseg != NULL), "vseg argument is NULL" ); |
---|
[409] | 1853 | |
---|
[625] | 1854 | // get pointers on local process VMM |
---|
[640] | 1855 | vmm_t * vmm = &process->vmm; |
---|
[611] | 1856 | |
---|
[629] | 1857 | // build extended pointer on GPT |
---|
[640] | 1858 | xptr_t gpt_xp = XPTR( local_cxy , &vmm->gpt ); |
---|
[629] | 1859 | |
---|
[623] | 1860 | // get relevant vseg infos |
---|
[624] | 1861 | vseg_type = vseg->type; |
---|
| 1862 | vpn_min = vseg->vpn_base; |
---|
| 1863 | vpn_max = vpn_min + vseg->vpn_size; |
---|
[623] | 1864 | |
---|
[625] | 1865 | #if DEBUG_VMM_REMOVE_VSEG |
---|
| 1866 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
| 1867 | thread_t * this = CURRENT_THREAD; |
---|
[640] | 1868 | #endif |
---|
| 1869 | |
---|
| 1870 | #if (DEBUG_VMM_REMOVE_VSEG & 1 ) |
---|
[625] | 1871 | if( DEBUG_VMM_REMOVE_VSEG < cycle ) |
---|
[641] | 1872 | printk("\n[%s] thread[%x,%x] enters / process %x / type %s / base %x / cycle %d\n", |
---|
[625] | 1873 | __FUNCTION__, this->process->pid, this->trdid, |
---|
| 1874 | process->pid, vseg_type_str(vseg->type), vseg->min, cycle ); |
---|
| 1875 | #endif |
---|
| 1876 | |
---|
[640] | 1877 | // loop on PTEs in GPT to unmap all mapped PTE |
---|
[1] | 1878 | for( vpn = vpn_min ; vpn < vpn_max ; vpn++ ) |
---|
| 1879 | { |
---|
[625] | 1880 | // get ppn and attr |
---|
[629] | 1881 | hal_gpt_get_pte( gpt_xp , vpn , &attr , &ppn ); |
---|
[409] | 1882 | |
---|
[625] | 1883 | if( attr & GPT_MAPPED ) // PTE is mapped |
---|
[409] | 1884 | { |
---|
[437] | 1885 | |
---|
[625] | 1886 | #if( DEBUG_VMM_REMOVE_VSEG & 1 ) |
---|
| 1887 | if( DEBUG_VMM_REMOVE_VSEG < cycle ) |
---|
[641] | 1888 | printk("\n[%s] thread[%x,%x] unmap vpn %x / ppn %x / type %s\n", |
---|
[640] | 1889 | __FUNCTION__, this->process->pid, this->trdid, vpn , ppn, vseg_type_str(vseg_type) ); |
---|
[437] | 1890 | #endif |
---|
[585] | 1891 | // unmap GPT entry in local GPT |
---|
[629] | 1892 | hal_gpt_reset_pte( gpt_xp , vpn ); |
---|
[409] | 1893 | |
---|
[640] | 1894 | // release physical page when required |
---|
| 1895 | vmm_ppn_release( process , vseg , ppn ); |
---|
[409] | 1896 | } |
---|
[1] | 1897 | } |
---|
[433] | 1898 | |
---|
[625] | 1899 | // remove vseg from VSL |
---|
[611] | 1900 | vmm_detach_vseg_from_vsl( vmm , vseg ); |
---|
| 1901 | |
---|
[625] | 1902 | // release vseg descriptor depending on vseg type |
---|
| 1903 | if( vseg_type == VSEG_TYPE_STACK ) |
---|
| 1904 | { |
---|
| 1905 | // release slot to local stack allocator |
---|
| 1906 | vmm_stack_free( vmm , vseg ); |
---|
| 1907 | } |
---|
| 1908 | else if( (vseg_type == VSEG_TYPE_ANON) || |
---|
| 1909 | (vseg_type == VSEG_TYPE_FILE) || |
---|
| 1910 | (vseg_type == VSEG_TYPE_REMOTE) ) |
---|
| 1911 | { |
---|
| 1912 | // release vseg to local mmap allocator |
---|
| 1913 | vmm_mmap_free( vmm , vseg ); |
---|
| 1914 | } |
---|
| 1915 | else |
---|
| 1916 | { |
---|
| 1917 | // release vseg descriptor to local kmem |
---|
| 1918 | vseg_free( vseg ); |
---|
| 1919 | } |
---|
| 1920 | |
---|
| 1921 | #if DEBUG_VMM_REMOVE_VSEG |
---|
[433] | 1922 | cycle = (uint32_t)hal_get_cycles(); |
---|
[625] | 1923 | if( DEBUG_VMM_REMOVE_VSEG < cycle ) |
---|
[641] | 1924 | printk("\n[%s] thread[%x,%x] exit / process %x / type %s / base %x / cycle %d\n", |
---|
[625] | 1925 | __FUNCTION__, this->process->pid, this->trdid, |
---|
| 1926 | process->pid, vseg_type_str(vseg->type), vseg->min, cycle ); |
---|
[433] | 1927 | #endif |
---|
| 1928 | |
---|
[625] | 1929 | } // end vmm_remove_vseg() |
---|
[1] | 1930 | |
---|
[611] | 1931 | ///////////////////////////////////////////// |
---|
[640] | 1932 | void vmm_resize_vseg( process_t * process, |
---|
| 1933 | vseg_t * vseg, |
---|
| 1934 | intptr_t new_base, |
---|
| 1935 | intptr_t new_size ) |
---|
[406] | 1936 | { |
---|
[640] | 1937 | vpn_t vpn; |
---|
| 1938 | ppn_t ppn; |
---|
| 1939 | uint32_t attr; |
---|
[406] | 1940 | |
---|
[640] | 1941 | // check arguments |
---|
| 1942 | assert( (process != NULL), "process argument is NULL" ); |
---|
| 1943 | assert( (vseg != NULL), "vseg argument is NULL" ); |
---|
[406] | 1944 | |
---|
[623] | 1945 | #if DEBUG_VMM_RESIZE_VSEG |
---|
| 1946 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
| 1947 | thread_t * this = CURRENT_THREAD; |
---|
[640] | 1948 | #endif |
---|
| 1949 | |
---|
| 1950 | #if (DEBUG_VMM_RESIZE_VSEG & 1) |
---|
[623] | 1951 | if( DEBUG_VMM_RESIZE_VSEG < cycle ) |
---|
[640] | 1952 | printk("\n[%s] thread[%x,%x] enter / process %x / %s / base %x / cycle %d\n", |
---|
| 1953 | __FUNCTION__, this->process->pid, this->trdid, |
---|
| 1954 | process->pid, vseg_type_str(vseg->type), old_base, cycle ); |
---|
[623] | 1955 | #endif |
---|
| 1956 | |
---|
[640] | 1957 | // get existing vseg vpn_min and vpn_max |
---|
| 1958 | vpn_t old_vpn_min = vseg->vpn_base; |
---|
| 1959 | vpn_t old_vpn_max = old_vpn_min + vseg->vpn_size - 1; |
---|
[1] | 1960 | |
---|
[640] | 1961 | // compute new vseg vpn_min & vpn_max |
---|
| 1962 | intptr_t min = new_base; |
---|
| 1963 | intptr_t max = new_base + new_size; |
---|
| 1964 | vpn_t new_vpn_min = min >> CONFIG_PPM_PAGE_SHIFT; |
---|
| 1965 | vpn_t new_vpn_max = (max - 1) >> CONFIG_PPM_PAGE_SHIFT; |
---|
[1] | 1966 | |
---|
[640] | 1967 | // build extended pointer on GPT |
---|
| 1968 | xptr_t gpt_xp = XPTR( local_cxy , &process->vmm.gpt ); |
---|
[1] | 1969 | |
---|
[640] | 1970 | // loop on PTEs in GPT to unmap PTE if (oldd_vpn_min <= vpn < new_vpn_min) |
---|
| 1971 | for( vpn = old_vpn_min ; vpn < new_vpn_min ; vpn++ ) |
---|
[623] | 1972 | { |
---|
[640] | 1973 | // get ppn and attr |
---|
| 1974 | hal_gpt_get_pte( gpt_xp , vpn , &attr , &ppn ); |
---|
[21] | 1975 | |
---|
[640] | 1976 | if( attr & GPT_MAPPED ) // PTE is mapped |
---|
| 1977 | { |
---|
[623] | 1978 | |
---|
| 1979 | #if( DEBUG_VMM_RESIZE_VSEG & 1 ) |
---|
| 1980 | if( DEBUG_VMM_RESIZE_VSEG < cycle ) |
---|
[640] | 1981 | printk("\n[%s] thread[%x,%x] unmap vpn %x / ppn %x / %s", |
---|
| 1982 | __FUNCTION__, this->process->pid, this->trdid, vpn , ppn, vseg_type_str(vseg_type) ); |
---|
[623] | 1983 | #endif |
---|
[640] | 1984 | // unmap GPT entry |
---|
| 1985 | hal_gpt_reset_pte( gpt_xp , vpn ); |
---|
[623] | 1986 | |
---|
[640] | 1987 | // release physical page when required |
---|
| 1988 | vmm_ppn_release( process , vseg , ppn ); |
---|
| 1989 | } |
---|
[1] | 1990 | } |
---|
[640] | 1991 | |
---|
| 1992 | // loop on PTEs in GPT to unmap PTE if (new vpn_max <= vpn < old_vpn_max) |
---|
| 1993 | for( vpn = new_vpn_max ; vpn < old_vpn_max ; vpn++ ) |
---|
[1] | 1994 | { |
---|
[640] | 1995 | // get ppn and attr |
---|
| 1996 | hal_gpt_get_pte( gpt_xp , vpn , &attr , &ppn ); |
---|
[623] | 1997 | |
---|
[640] | 1998 | if( attr & GPT_MAPPED ) // PTE is mapped |
---|
| 1999 | { |
---|
| 2000 | |
---|
[641] | 2001 | #if( DEBUG_VMM_RESIZE_VSEG & 1 ) |
---|
[623] | 2002 | if( DEBUG_VMM_RESIZE_VSEG < cycle ) |
---|
[640] | 2003 | printk("\n[%s] thread[%x,%x] unmap vpn %x / ppn %x / %s", |
---|
| 2004 | __FUNCTION__, this->process->pid, this->trdid, vpn , ppn, vseg_type_str(vseg_type) ); |
---|
[623] | 2005 | #endif |
---|
[640] | 2006 | // unmap GPT entry in local GPT |
---|
| 2007 | hal_gpt_reset_pte( gpt_xp , vpn ); |
---|
[406] | 2008 | |
---|
[640] | 2009 | // release physical page when required |
---|
| 2010 | vmm_ppn_release( process , vseg , ppn ); |
---|
| 2011 | } |
---|
[1] | 2012 | } |
---|
[623] | 2013 | |
---|
[640] | 2014 | // resize vseg in VSL |
---|
| 2015 | vseg->min = min; |
---|
| 2016 | vseg->max = max; |
---|
| 2017 | vseg->vpn_base = new_vpn_min; |
---|
| 2018 | vseg->vpn_size = new_vpn_max - new_vpn_min + 1; |
---|
| 2019 | |
---|
| 2020 | #if DEBUG_VMM_RESIZE_VSEG |
---|
| 2021 | cycle = (uint32_t)hal_get_cycles(); |
---|
[623] | 2022 | if( DEBUG_VMM_RESIZE_VSEG < cycle ) |
---|
[640] | 2023 | printk("[%s] thread[%x,%x] exit / process %x / %s / base %x / cycle %d\n", |
---|
| 2024 | __FUNCTION__, this->process->pid, this->trdid, |
---|
| 2025 | process->pid, vseg_type_str(vseg->type), vseg->min, cycle ); |
---|
[623] | 2026 | #endif |
---|
[406] | 2027 | |
---|
[640] | 2028 | } // end vmm_resize_vseg |
---|
[623] | 2029 | |
---|
[640] | 2030 | ///////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 2031 | // This static function is called twice by the vmm_get_vseg() function. |
---|
| 2032 | // It scan the - possibly remote - VSL defined by the <vmm_xp> argument to find the vseg |
---|
| 2033 | // containing a given virtual address <vaddr>. It uses remote accesses to access the remote |
---|
| 2034 | // VSL if required. The VSL lock protecting the VSL must be taken by the caller. |
---|
| 2035 | ///////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 2036 | // @ vmm_xp : extended pointer on the process VMM. |
---|
| 2037 | // @ vaddr : virtual address. |
---|
| 2038 | // @ return local pointer on remote vseg if success / return NULL if not found. |
---|
| 2039 | ///////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 2040 | static vseg_t * vmm_vseg_from_vaddr( xptr_t vmm_xp, |
---|
| 2041 | intptr_t vaddr ) |
---|
| 2042 | { |
---|
| 2043 | xptr_t iter_xp; |
---|
| 2044 | xptr_t vseg_xp; |
---|
| 2045 | vseg_t * vseg; |
---|
| 2046 | intptr_t min; |
---|
| 2047 | intptr_t max; |
---|
[623] | 2048 | |
---|
[640] | 2049 | // get cluster and local pointer on target VMM |
---|
| 2050 | vmm_t * vmm_ptr = GET_PTR( vmm_xp ); |
---|
| 2051 | cxy_t vmm_cxy = GET_CXY( vmm_xp ); |
---|
[623] | 2052 | |
---|
[640] | 2053 | // build extended pointer on VSL root |
---|
| 2054 | xptr_t root_xp = XPTR( vmm_cxy , &vmm_ptr->vsegs_root ); |
---|
[406] | 2055 | |
---|
[640] | 2056 | // scan the list of vsegs in VSL |
---|
| 2057 | XLIST_FOREACH( root_xp , iter_xp ) |
---|
| 2058 | { |
---|
| 2059 | vseg_xp = XLIST_ELEMENT( iter_xp , vseg_t , xlist ); |
---|
| 2060 | vseg = GET_PTR( vseg_xp ); |
---|
[406] | 2061 | |
---|
[640] | 2062 | min = hal_remote_l32( XPTR( vmm_cxy , &vseg->min ) ); |
---|
| 2063 | max = hal_remote_l32( XPTR( vmm_cxy , &vseg->max ) ); |
---|
[407] | 2064 | |
---|
[640] | 2065 | // return success when match |
---|
| 2066 | if( (vaddr >= min) && (vaddr < max) ) return vseg; |
---|
[1] | 2067 | } |
---|
| 2068 | |
---|
[640] | 2069 | // return failure |
---|
| 2070 | return NULL; |
---|
[1] | 2071 | |
---|
[640] | 2072 | } // end vmm_vseg_from_vaddr() |
---|
[1] | 2073 | |
---|
| 2074 | /////////////////////////////////////////// |
---|
[388] | 2075 | error_t vmm_get_vseg( process_t * process, |
---|
[394] | 2076 | intptr_t vaddr, |
---|
[388] | 2077 | vseg_t ** found_vseg ) |
---|
[1] | 2078 | { |
---|
[640] | 2079 | xptr_t loc_lock_xp; // extended pointer on local VSL lock |
---|
| 2080 | xptr_t ref_lock_xp; // extended pointer on reference VSL lock |
---|
| 2081 | vseg_t * loc_vseg; // local pointer on local vseg |
---|
| 2082 | vseg_t * ref_vseg; // local pointer on reference vseg |
---|
[1] | 2083 | |
---|
[640] | 2084 | // build extended pointer on local VSL lock |
---|
| 2085 | loc_lock_xp = XPTR( local_cxy , &process->vmm.vsl_lock ); |
---|
| 2086 | |
---|
| 2087 | // get local VSL lock |
---|
| 2088 | remote_queuelock_acquire( loc_lock_xp ); |
---|
[1] | 2089 | |
---|
[440] | 2090 | // try to get vseg from local VMM |
---|
[640] | 2091 | loc_vseg = vmm_vseg_from_vaddr( XPTR( local_cxy, &process->vmm ) , vaddr ); |
---|
[440] | 2092 | |
---|
[640] | 2093 | if (loc_vseg == NULL) // vseg not found => access reference VSL |
---|
| 2094 | { |
---|
[388] | 2095 | // get extended pointer on reference process |
---|
| 2096 | xptr_t ref_xp = process->ref_xp; |
---|
[1] | 2097 | |
---|
[640] | 2098 | // get cluster and local pointer on reference process |
---|
[388] | 2099 | cxy_t ref_cxy = GET_CXY( ref_xp ); |
---|
[433] | 2100 | process_t * ref_ptr = GET_PTR( ref_xp ); |
---|
[388] | 2101 | |
---|
[640] | 2102 | // build extended pointer on reference VSL lock |
---|
| 2103 | ref_lock_xp = XPTR( ref_cxy , &ref_ptr->vmm.vsl_lock ); |
---|
| 2104 | |
---|
| 2105 | // get reference VSL lock |
---|
| 2106 | remote_queuelock_acquire( ref_lock_xp ); |
---|
[388] | 2107 | |
---|
[640] | 2108 | // try to get vseg from reference VMM |
---|
| 2109 | ref_vseg = vmm_vseg_from_vaddr( XPTR( ref_cxy , &ref_ptr->vmm ) , vaddr ); |
---|
[388] | 2110 | |
---|
[640] | 2111 | if( ref_vseg == NULL ) // vseg not found => return error |
---|
| 2112 | { |
---|
| 2113 | printk("\n[ERROR] in %s : vaddr %x in process %x out of segment\n", |
---|
| 2114 | __FUNCTION__, vaddr, process->pid ); |
---|
[388] | 2115 | |
---|
[640] | 2116 | // release reference VSL lock |
---|
| 2117 | remote_queuelock_release( ref_lock_xp ); |
---|
[388] | 2118 | |
---|
[640] | 2119 | return -1; |
---|
| 2120 | } |
---|
| 2121 | else // vseg found => try to update local VSL |
---|
| 2122 | { |
---|
| 2123 | // allocate a local vseg descriptor |
---|
| 2124 | loc_vseg = vseg_alloc(); |
---|
[625] | 2125 | |
---|
[640] | 2126 | if( loc_vseg == NULL ) // no memory => return error |
---|
| 2127 | { |
---|
| 2128 | printk("\n[ERROR] in %s : vaddr %x in process %x / no memory for local vseg\n", |
---|
| 2129 | __FUNCTION__, vaddr, process->pid ); |
---|
[595] | 2130 | |
---|
[640] | 2131 | // release reference VSL & local VSL locks |
---|
| 2132 | remote_queuelock_release( ref_lock_xp ); |
---|
| 2133 | remote_queuelock_release( loc_lock_xp ); |
---|
[388] | 2134 | |
---|
[640] | 2135 | return -1; |
---|
| 2136 | } |
---|
| 2137 | else // update local VSL and return success |
---|
| 2138 | { |
---|
| 2139 | // initialize local vseg |
---|
| 2140 | vseg_init_from_ref( loc_vseg , XPTR( ref_cxy , ref_vseg ) ); |
---|
| 2141 | |
---|
| 2142 | // register local vseg in local VSL |
---|
| 2143 | vmm_attach_vseg_to_vsl( &process->vmm , loc_vseg ); |
---|
| 2144 | |
---|
| 2145 | // release reference VSL & local VSL locks |
---|
| 2146 | remote_queuelock_release( ref_lock_xp ); |
---|
| 2147 | remote_queuelock_release( loc_lock_xp ); |
---|
| 2148 | |
---|
| 2149 | *found_vseg = loc_vseg; |
---|
| 2150 | return 0; |
---|
| 2151 | } |
---|
| 2152 | } |
---|
| 2153 | } |
---|
| 2154 | else // vseg found in local VSL => return success |
---|
| 2155 | { |
---|
| 2156 | // release local VSL lock |
---|
| 2157 | remote_queuelock_release( loc_lock_xp ); |
---|
| 2158 | |
---|
| 2159 | *found_vseg = loc_vseg; |
---|
| 2160 | return 0; |
---|
| 2161 | } |
---|
[388] | 2162 | } // end vmm_get_vseg() |
---|
| 2163 | |
---|
[407] | 2164 | ////////////////////////////////////////////////////////////////////////////////////// |
---|
| 2165 | // This static function compute the target cluster to allocate a physical page |
---|
[632] | 2166 | // for a given <vpn> in a given <vseg>, allocates the page and returns an extended |
---|
| 2167 | // pointer on the allocated page descriptor. |
---|
[407] | 2168 | // The vseg cannot have the FILE type. |
---|
| 2169 | ////////////////////////////////////////////////////////////////////////////////////// |
---|
[640] | 2170 | // @ vseg : local pointer on vseg. |
---|
| 2171 | // @ vpn : unmapped vpn. |
---|
| 2172 | // @ return an extended pointer on the allocated page |
---|
| 2173 | ////////////////////////////////////////////////////////////////////////////////////// |
---|
[407] | 2174 | static xptr_t vmm_page_allocate( vseg_t * vseg, |
---|
| 2175 | vpn_t vpn ) |
---|
| 2176 | { |
---|
[433] | 2177 | |
---|
[632] | 2178 | #if DEBUG_VMM_PAGE_ALLOCATE |
---|
[619] | 2179 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
| 2180 | thread_t * this = CURRENT_THREAD; |
---|
[632] | 2181 | if( DEBUG_VMM_PAGE_ALLOCATE < cycle ) |
---|
[595] | 2182 | printk("\n[%s] thread[%x,%x] enter for vpn %x / cycle %d\n", |
---|
| 2183 | __FUNCTION__ , this->process->pid, this->trdid, vpn, cycle ); |
---|
[433] | 2184 | #endif |
---|
| 2185 | |
---|
[632] | 2186 | xptr_t page_xp; |
---|
[407] | 2187 | cxy_t page_cxy; |
---|
[635] | 2188 | page_t * page_ptr; |
---|
[577] | 2189 | uint32_t index; |
---|
[407] | 2190 | |
---|
[577] | 2191 | uint32_t type = vseg->type; |
---|
| 2192 | uint32_t flags = vseg->flags; |
---|
| 2193 | uint32_t x_size = LOCAL_CLUSTER->x_size; |
---|
| 2194 | uint32_t y_size = LOCAL_CLUSTER->y_size; |
---|
[407] | 2195 | |
---|
[567] | 2196 | // check vseg type |
---|
| 2197 | assert( ( type != VSEG_TYPE_FILE ) , "illegal vseg type\n" ); |
---|
[407] | 2198 | |
---|
| 2199 | if( flags & VSEG_DISTRIB ) // distributed => cxy depends on vpn LSB |
---|
| 2200 | { |
---|
[577] | 2201 | index = vpn & ((x_size * y_size) - 1); |
---|
| 2202 | page_cxy = HAL_CXY_FROM_XY( (index / y_size) , (index % y_size) ); |
---|
[561] | 2203 | |
---|
[577] | 2204 | // If the cluster selected from VPN's LSBs is empty, we select one randomly |
---|
| 2205 | if ( cluster_is_active( page_cxy ) == false ) |
---|
| 2206 | { |
---|
| 2207 | page_cxy = cluster_random_select(); |
---|
[561] | 2208 | } |
---|
[407] | 2209 | } |
---|
| 2210 | else // other cases => cxy specified in vseg |
---|
| 2211 | { |
---|
[561] | 2212 | page_cxy = vseg->cxy; |
---|
[407] | 2213 | } |
---|
| 2214 | |
---|
[635] | 2215 | // allocate one small physical page from target cluster |
---|
| 2216 | page_ptr = ppm_remote_alloc_pages( page_cxy , 0 ); |
---|
[407] | 2217 | |
---|
[635] | 2218 | page_xp = XPTR( page_cxy , page_ptr ); |
---|
| 2219 | |
---|
[632] | 2220 | #if DEBUG_VMM_PAGE_ALLOCATE |
---|
[595] | 2221 | cycle = (uint32_t)hal_get_cycles(); |
---|
[632] | 2222 | if( DEBUG_VMM_PAGE_ALLOCATE < cycle ) |
---|
[635] | 2223 | printk("\n[%s] thread[%x,%x] exit for vpn %x / ppn %x / cycle %d\n", |
---|
| 2224 | __FUNCTION__ , this->process->pid, this->trdid, vpn, ppm_page2ppn(page_xp), cycle ); |
---|
[433] | 2225 | #endif |
---|
| 2226 | |
---|
[632] | 2227 | return page_xp; |
---|
[407] | 2228 | |
---|
| 2229 | } // end vmm_page_allocate() |
---|
| 2230 | |
---|
[313] | 2231 | //////////////////////////////////////// |
---|
| 2232 | error_t vmm_get_one_ppn( vseg_t * vseg, |
---|
| 2233 | vpn_t vpn, |
---|
| 2234 | ppn_t * ppn ) |
---|
| 2235 | { |
---|
| 2236 | error_t error; |
---|
[407] | 2237 | xptr_t page_xp; // extended pointer on physical page descriptor |
---|
[606] | 2238 | uint32_t page_id; // missing page index in vseg mapper |
---|
[406] | 2239 | uint32_t type; // vseg type; |
---|
[313] | 2240 | |
---|
[406] | 2241 | type = vseg->type; |
---|
[606] | 2242 | page_id = vpn - vseg->vpn_base; |
---|
[313] | 2243 | |
---|
[438] | 2244 | #if DEBUG_VMM_GET_ONE_PPN |
---|
[595] | 2245 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
| 2246 | thread_t * this = CURRENT_THREAD; |
---|
[635] | 2247 | if( (DEBUG_VMM_GET_ONE_PPN < cycle) && (vpn == 0x40b) ) |
---|
[606] | 2248 | printk("\n[%s] thread[%x,%x] enter for vpn %x / type %s / page_id %d / cycle %d\n", |
---|
| 2249 | __FUNCTION__, this->process->pid, this->trdid, vpn, vseg_type_str(type), page_id, cycle ); |
---|
[433] | 2250 | #endif |
---|
[313] | 2251 | |
---|
[406] | 2252 | // FILE type : get the physical page from the file mapper |
---|
[313] | 2253 | if( type == VSEG_TYPE_FILE ) |
---|
| 2254 | { |
---|
[406] | 2255 | // get extended pointer on mapper |
---|
[407] | 2256 | xptr_t mapper_xp = vseg->mapper_xp; |
---|
[313] | 2257 | |
---|
[567] | 2258 | assert( (mapper_xp != XPTR_NULL), |
---|
| 2259 | "mapper not defined for a FILE vseg\n" ); |
---|
[406] | 2260 | |
---|
[606] | 2261 | // get extended pointer on page descriptor |
---|
| 2262 | page_xp = mapper_remote_get_page( mapper_xp , page_id ); |
---|
[406] | 2263 | |
---|
[606] | 2264 | if ( page_xp == XPTR_NULL ) return EINVAL; |
---|
[313] | 2265 | } |
---|
| 2266 | |
---|
[406] | 2267 | // Other types : allocate a physical page from target cluster, |
---|
[407] | 2268 | // as defined by vseg type and vpn value |
---|
[313] | 2269 | else |
---|
| 2270 | { |
---|
[433] | 2271 | // allocate one physical page |
---|
[407] | 2272 | page_xp = vmm_page_allocate( vseg , vpn ); |
---|
[406] | 2273 | |
---|
[635] | 2274 | if( page_xp == XPTR_NULL ) return -1; |
---|
[313] | 2275 | |
---|
[406] | 2276 | // initialise missing page from .elf file mapper for DATA and CODE types |
---|
[440] | 2277 | // the vseg->mapper_xp field is an extended pointer on the .elf file mapper |
---|
[313] | 2278 | if( (type == VSEG_TYPE_CODE) || (type == VSEG_TYPE_DATA) ) |
---|
| 2279 | { |
---|
[406] | 2280 | // get extended pointer on mapper |
---|
| 2281 | xptr_t mapper_xp = vseg->mapper_xp; |
---|
[313] | 2282 | |
---|
[567] | 2283 | assert( (mapper_xp != XPTR_NULL), |
---|
| 2284 | "mapper not defined for a CODE or DATA vseg\n" ); |
---|
[406] | 2285 | |
---|
| 2286 | // compute missing page offset in vseg |
---|
[606] | 2287 | uint32_t offset = page_id << CONFIG_PPM_PAGE_SHIFT; |
---|
[406] | 2288 | |
---|
[313] | 2289 | // compute missing page offset in .elf file |
---|
[406] | 2290 | uint32_t elf_offset = vseg->file_offset + offset; |
---|
[313] | 2291 | |
---|
[438] | 2292 | #if (DEBUG_VMM_GET_ONE_PPN & 0x1) |
---|
[635] | 2293 | if( (DEBUG_VMM_GET_ONE_PPN < cycle) && (vpn == 0x40b) ) |
---|
[595] | 2294 | printk("\n[%s] thread[%x,%x] for vpn = %x / elf_offset = %x\n", |
---|
| 2295 | __FUNCTION__, this->process->pid, this->trdid, vpn, elf_offset ); |
---|
[433] | 2296 | #endif |
---|
[406] | 2297 | // compute extended pointer on page base |
---|
[407] | 2298 | xptr_t base_xp = ppm_page2base( page_xp ); |
---|
[313] | 2299 | |
---|
[406] | 2300 | // file_size (in .elf mapper) can be smaller than vseg_size (BSS) |
---|
| 2301 | uint32_t file_size = vseg->file_size; |
---|
| 2302 | |
---|
| 2303 | if( file_size < offset ) // missing page fully in BSS |
---|
[313] | 2304 | { |
---|
[406] | 2305 | |
---|
[438] | 2306 | #if (DEBUG_VMM_GET_ONE_PPN & 0x1) |
---|
[635] | 2307 | if( (DEBUG_VMM_GET_ONE_PPN < cycle) && (vpn == 0x40b) ) |
---|
[595] | 2308 | printk("\n[%s] thread[%x,%x] for vpn %x / fully in BSS\n", |
---|
| 2309 | __FUNCTION__, this->process->pid, this->trdid, vpn ); |
---|
[433] | 2310 | #endif |
---|
[407] | 2311 | if( GET_CXY( page_xp ) == local_cxy ) |
---|
[313] | 2312 | { |
---|
[315] | 2313 | memset( GET_PTR( base_xp ) , 0 , CONFIG_PPM_PAGE_SIZE ); |
---|
[313] | 2314 | } |
---|
| 2315 | else |
---|
| 2316 | { |
---|
[315] | 2317 | hal_remote_memset( base_xp , 0 , CONFIG_PPM_PAGE_SIZE ); |
---|
[313] | 2318 | } |
---|
| 2319 | } |
---|
[406] | 2320 | else if( file_size >= (offset + CONFIG_PPM_PAGE_SIZE) ) // fully in mapper |
---|
[315] | 2321 | { |
---|
[406] | 2322 | |
---|
[438] | 2323 | #if (DEBUG_VMM_GET_ONE_PPN & 0x1) |
---|
[635] | 2324 | if( (DEBUG_VMM_GET_ONE_PPN < cycle) && (vpn == 0x40b) ) |
---|
[595] | 2325 | printk("\n[%s] thread[%x,%x] for vpn %x / fully in mapper\n", |
---|
| 2326 | __FUNCTION__, this->process->pid, this->trdid, vpn ); |
---|
[433] | 2327 | #endif |
---|
[606] | 2328 | error = mapper_move_kernel( mapper_xp, |
---|
| 2329 | true, // to_buffer |
---|
| 2330 | elf_offset, |
---|
| 2331 | base_xp, |
---|
| 2332 | CONFIG_PPM_PAGE_SIZE ); |
---|
[313] | 2333 | if( error ) return EINVAL; |
---|
| 2334 | } |
---|
[406] | 2335 | else // both in mapper and in BSS : |
---|
| 2336 | // - (file_size - offset) bytes from mapper |
---|
| 2337 | // - (page_size + offset - file_size) bytes from BSS |
---|
[313] | 2338 | { |
---|
[406] | 2339 | |
---|
[438] | 2340 | #if (DEBUG_VMM_GET_ONE_PPN & 0x1) |
---|
[635] | 2341 | if( (DEBUG_VMM_GET_ONE_PPN < cycle) && (vpn == 0x40b) ) |
---|
[610] | 2342 | printk("\n[%s] thread[%x,%x] for vpn %x / both mapper & BSS\n" |
---|
[433] | 2343 | " %d bytes from mapper / %d bytes from BSS\n", |
---|
[595] | 2344 | __FUNCTION__, this->process->pid, this->trdid, vpn, |
---|
[407] | 2345 | file_size - offset , offset + CONFIG_PPM_PAGE_SIZE - file_size ); |
---|
[433] | 2346 | #endif |
---|
[313] | 2347 | // initialize mapper part |
---|
[606] | 2348 | error = mapper_move_kernel( mapper_xp, |
---|
| 2349 | true, // to buffer |
---|
| 2350 | elf_offset, |
---|
| 2351 | base_xp, |
---|
| 2352 | file_size - offset ); |
---|
[313] | 2353 | if( error ) return EINVAL; |
---|
| 2354 | |
---|
| 2355 | // initialize BSS part |
---|
[407] | 2356 | if( GET_CXY( page_xp ) == local_cxy ) |
---|
[313] | 2357 | { |
---|
[406] | 2358 | memset( GET_PTR( base_xp ) + file_size - offset , 0 , |
---|
| 2359 | offset + CONFIG_PPM_PAGE_SIZE - file_size ); |
---|
[313] | 2360 | } |
---|
| 2361 | else |
---|
| 2362 | { |
---|
[406] | 2363 | hal_remote_memset( base_xp + file_size - offset , 0 , |
---|
| 2364 | offset + CONFIG_PPM_PAGE_SIZE - file_size ); |
---|
[313] | 2365 | } |
---|
| 2366 | } |
---|
| 2367 | } // end initialisation for CODE or DATA types |
---|
| 2368 | } |
---|
| 2369 | |
---|
| 2370 | // return ppn |
---|
[407] | 2371 | *ppn = ppm_page2ppn( page_xp ); |
---|
[406] | 2372 | |
---|
[438] | 2373 | #if DEBUG_VMM_GET_ONE_PPN |
---|
[595] | 2374 | cycle = (uint32_t)hal_get_cycles(); |
---|
[635] | 2375 | if( (DEBUG_VMM_GET_ONE_PPN < cycle) && (vpn == 0x40b) ) |
---|
| 2376 | printk("\n[%s] thread[%x,%x] exit for vpn %x / ppn %x / cycle %d\n", |
---|
[595] | 2377 | __FUNCTION__ , this->process->pid, this->trdid , vpn , *ppn, cycle ); |
---|
[433] | 2378 | #endif |
---|
[406] | 2379 | |
---|
[313] | 2380 | return 0; |
---|
| 2381 | |
---|
| 2382 | } // end vmm_get_one_ppn() |
---|
| 2383 | |
---|
[585] | 2384 | /////////////////////////////////////////////////// |
---|
| 2385 | error_t vmm_handle_page_fault( process_t * process, |
---|
| 2386 | vpn_t vpn ) |
---|
[1] | 2387 | { |
---|
[585] | 2388 | vseg_t * vseg; // vseg containing vpn |
---|
[629] | 2389 | uint32_t attr; // PTE_ATTR value |
---|
| 2390 | ppn_t ppn; // PTE_PPN value |
---|
[585] | 2391 | uint32_t ref_attr; // PTE_ATTR value in reference GPT |
---|
| 2392 | ppn_t ref_ppn; // PTE_PPN value in reference GPT |
---|
| 2393 | cxy_t ref_cxy; // reference cluster for missing vpn |
---|
| 2394 | process_t * ref_ptr; // reference process for missing vpn |
---|
| 2395 | xptr_t local_gpt_xp; // extended pointer on local GPT |
---|
| 2396 | xptr_t ref_gpt_xp; // extended pointer on reference GPT |
---|
| 2397 | error_t error; // value returned by called functions |
---|
[1] | 2398 | |
---|
[629] | 2399 | thread_t * this = CURRENT_THREAD; |
---|
| 2400 | |
---|
| 2401 | #if (CONFIG_INSTRUMENTATION_PGFAULTS || DEBUG_VMM_HANDLE_PAGE_FAULT) |
---|
| 2402 | uint32_t start_cycle = (uint32_t)hal_get_cycles(); |
---|
| 2403 | #endif |
---|
| 2404 | |
---|
[625] | 2405 | #if DEBUG_VMM_HANDLE_PAGE_FAULT |
---|
[635] | 2406 | if( (start_cycle > DEBUG_VMM_HANDLE_PAGE_FAULT) && (vpn > 0) ) |
---|
[625] | 2407 | printk("\n[%s] thread[%x,%x] enter for vpn %x / cycle %d\n", |
---|
[629] | 2408 | __FUNCTION__, this->process->pid, this->trdid, vpn, start_cycle ); |
---|
[625] | 2409 | #endif |
---|
| 2410 | |
---|
[629] | 2411 | #if (DEBUG_VMM_HANDLE_PAGE_FAULT & 1) |
---|
[635] | 2412 | if( (start_cycle > DEBUG_VMM_HANDLE_PAGE_FAULT) && (vpn > 0) ) |
---|
| 2413 | hal_vmm_display( this->process , true ); |
---|
[629] | 2414 | #endif |
---|
| 2415 | |
---|
[585] | 2416 | // get local vseg (access to reference VSL can be required) |
---|
| 2417 | error = vmm_get_vseg( process, |
---|
| 2418 | (intptr_t)vpn<<CONFIG_PPM_PAGE_SHIFT, |
---|
| 2419 | &vseg ); |
---|
| 2420 | if( error ) |
---|
| 2421 | { |
---|
[629] | 2422 | printk("\n[ERROR] in %s : vpn %x in thread[%x,%x] not in registered vseg\n", |
---|
| 2423 | __FUNCTION__ , vpn , process->pid, this->trdid ); |
---|
[585] | 2424 | |
---|
| 2425 | return EXCP_USER_ERROR; |
---|
| 2426 | } |
---|
| 2427 | |
---|
[635] | 2428 | #if (DEBUG_VMM_HANDLE_PAGE_FAULT & 1) |
---|
| 2429 | if( (start_cycle > DEBUG_VMM_HANDLE_PAGE_FAULT) && (vpn > 0) ) |
---|
[634] | 2430 | printk("\n[%s] thread[%x,%x] found vseg %s\n", |
---|
| 2431 | __FUNCTION__, this->process->pid, this->trdid, vseg_type_str(vseg->type) ); |
---|
[433] | 2432 | #endif |
---|
[406] | 2433 | |
---|
[629] | 2434 | // build extended pointer on local GPT |
---|
| 2435 | local_gpt_xp = XPTR( local_cxy , &process->vmm.gpt ); |
---|
| 2436 | |
---|
[632] | 2437 | // lock PTE in local GPT and get current PPN and attributes |
---|
[629] | 2438 | error = hal_gpt_lock_pte( local_gpt_xp, |
---|
| 2439 | vpn, |
---|
| 2440 | &attr, |
---|
| 2441 | &ppn ); |
---|
| 2442 | if( error ) |
---|
[438] | 2443 | { |
---|
[629] | 2444 | printk("\n[PANIC] in %s : cannot lock PTE in local GPT / vpn %x / process %x\n", |
---|
| 2445 | __FUNCTION__ , vpn , process->pid ); |
---|
| 2446 | |
---|
| 2447 | return EXCP_KERNEL_PANIC; |
---|
| 2448 | } |
---|
[407] | 2449 | |
---|
[635] | 2450 | #if (DEBUG_VMM_HANDLE_PAGE_FAULT & 1) |
---|
| 2451 | if( (start_cycle > DEBUG_VMM_HANDLE_PAGE_FAULT) && (vpn > 0) ) |
---|
| 2452 | printk("\n[%s] thread[%x,%x] locked vpn %x in cluster %x\n", |
---|
[634] | 2453 | __FUNCTION__, this->process->pid, this->trdid, vpn, local_cxy ); |
---|
[632] | 2454 | #endif |
---|
| 2455 | |
---|
| 2456 | // handle page fault only if local PTE still unmapped after lock |
---|
[629] | 2457 | if( (attr & GPT_MAPPED) == 0 ) |
---|
| 2458 | { |
---|
| 2459 | // get reference process cluster and local pointer |
---|
| 2460 | ref_cxy = GET_CXY( process->ref_xp ); |
---|
| 2461 | ref_ptr = GET_PTR( process->ref_xp ); |
---|
[407] | 2462 | |
---|
[630] | 2463 | /////////////// private vseg or (local == reference) |
---|
| 2464 | /////////////// => access only the local GPT |
---|
[629] | 2465 | if( (vseg->type == VSEG_TYPE_STACK) || |
---|
| 2466 | (vseg->type == VSEG_TYPE_CODE) || |
---|
| 2467 | (ref_cxy == local_cxy ) ) |
---|
| 2468 | { |
---|
[632] | 2469 | |
---|
[635] | 2470 | #if (DEBUG_VMM_HANDLE_PAGE_FAULT & 1) |
---|
| 2471 | if( (start_cycle > DEBUG_VMM_HANDLE_PAGE_FAULT) && (vpn > 0) ) |
---|
| 2472 | printk("\n[%s] thread[%x,%x] access local gpt : cxy %x / ref_cxy %x / type %s / cycle %d\n", |
---|
| 2473 | __FUNCTION__, this->process->pid, this->trdid, |
---|
| 2474 | local_cxy, ref_cxy, vseg_type_str(vseg->type), (uint32_t)hal_get_cycles() ); |
---|
[632] | 2475 | #endif |
---|
| 2476 | // allocate and initialise a physical page |
---|
[629] | 2477 | error = vmm_get_one_ppn( vseg , vpn , &ppn ); |
---|
[407] | 2478 | |
---|
[585] | 2479 | if( error ) |
---|
[408] | 2480 | { |
---|
[629] | 2481 | printk("\n[ERROR] in %s : no physical page / process = %x / vpn = %x\n", |
---|
[408] | 2482 | __FUNCTION__ , process->pid , vpn ); |
---|
[1] | 2483 | |
---|
[629] | 2484 | // unlock PTE in local GPT |
---|
| 2485 | hal_gpt_unlock_pte( local_gpt_xp , vpn ); |
---|
[406] | 2486 | |
---|
[585] | 2487 | return EXCP_KERNEL_PANIC; |
---|
[407] | 2488 | } |
---|
| 2489 | |
---|
[629] | 2490 | // define attr from vseg flags |
---|
[632] | 2491 | attr = GPT_MAPPED | GPT_SMALL | GPT_READABLE; |
---|
[629] | 2492 | if( vseg->flags & VSEG_USER ) attr |= GPT_USER; |
---|
| 2493 | if( vseg->flags & VSEG_WRITE ) attr |= GPT_WRITABLE; |
---|
| 2494 | if( vseg->flags & VSEG_EXEC ) attr |= GPT_EXECUTABLE; |
---|
| 2495 | if( vseg->flags & VSEG_CACHE ) attr |= GPT_CACHABLE; |
---|
[407] | 2496 | |
---|
[629] | 2497 | // set PTE to local GPT |
---|
[632] | 2498 | // it unlocks this PTE |
---|
[629] | 2499 | hal_gpt_set_pte( local_gpt_xp, |
---|
| 2500 | vpn, |
---|
| 2501 | attr, |
---|
| 2502 | ppn ); |
---|
[585] | 2503 | |
---|
[629] | 2504 | #if (CONFIG_INSTRUMENTATION_PGFAULTS || DEBUG_VMM_HANDLE_PAGE_FAULT) |
---|
| 2505 | uint32_t end_cycle = (uint32_t)hal_get_cycles(); |
---|
[641] | 2506 | uint32_t cost = end_cycle - start_cycle; |
---|
[629] | 2507 | #endif |
---|
[585] | 2508 | |
---|
| 2509 | #if DEBUG_VMM_HANDLE_PAGE_FAULT |
---|
[635] | 2510 | if( (end_cycle > DEBUG_VMM_HANDLE_PAGE_FAULT) && (vpn > 0) ) |
---|
[632] | 2511 | printk("\n[%s] thread[%x,%x] handled local pgfault / ppn %x / attr %x / cycle %d\n", |
---|
| 2512 | __FUNCTION__, this->process->pid, this->trdid, ppn, attr, end_cycle ); |
---|
[585] | 2513 | #endif |
---|
| 2514 | |
---|
[629] | 2515 | #if CONFIG_INSTRUMENTATION_PGFAULTS |
---|
| 2516 | this->info.local_pgfault_nr++; |
---|
[641] | 2517 | this->info.local_pgfault_cost += cost; |
---|
| 2518 | if( cost > this->info.local_pgfault_max ) this->info.local_pgfault_max = cost; |
---|
[629] | 2519 | #endif |
---|
| 2520 | return EXCP_NON_FATAL; |
---|
[585] | 2521 | |
---|
[629] | 2522 | } // end local GPT access |
---|
[585] | 2523 | |
---|
[630] | 2524 | /////////////////// public vseg and (local != reference) |
---|
| 2525 | /////////////////// => access ref GPT to update local GPT |
---|
[629] | 2526 | else |
---|
| 2527 | { |
---|
[632] | 2528 | |
---|
[635] | 2529 | #if (DEBUG_VMM_HANDLE_PAGE_FAULT & 1) |
---|
| 2530 | if( (start_cycle > DEBUG_VMM_HANDLE_PAGE_FAULT) && (vpn > 0) ) |
---|
| 2531 | printk("\n[%s] thread[%x,%x] access ref gpt : cxy %x / ref_cxy %x / type %s / cycle %d\n", |
---|
| 2532 | __FUNCTION__, this->process->pid, this->trdid, |
---|
| 2533 | local_cxy, ref_cxy, vseg_type_str(vseg->type), (uint32_t)hal_get_cycles() ); |
---|
[632] | 2534 | #endif |
---|
[629] | 2535 | // build extended pointer on reference GPT |
---|
| 2536 | ref_gpt_xp = XPTR( ref_cxy , &ref_ptr->vmm.gpt ); |
---|
[585] | 2537 | |
---|
[632] | 2538 | // lock PTE in reference GPT and get current PPN and attributes |
---|
| 2539 | error = hal_gpt_lock_pte( ref_gpt_xp, |
---|
| 2540 | vpn, |
---|
| 2541 | &ref_attr, |
---|
| 2542 | &ref_ppn ); |
---|
| 2543 | if( error ) |
---|
| 2544 | { |
---|
| 2545 | printk("\n[PANIC] in %s : cannot lock PTE in ref GPT / vpn %x / process %x\n", |
---|
| 2546 | __FUNCTION__ , vpn , process->pid ); |
---|
| 2547 | |
---|
| 2548 | // unlock PTE in local GPT |
---|
| 2549 | hal_gpt_unlock_pte( local_gpt_xp , vpn ); |
---|
| 2550 | |
---|
| 2551 | return EXCP_KERNEL_PANIC; |
---|
| 2552 | } |
---|
[1] | 2553 | |
---|
[635] | 2554 | #if (DEBUG_VMM_HANDLE_PAGE_FAULT & 1) |
---|
| 2555 | if( (start_cycle > DEBUG_VMM_HANDLE_PAGE_FAULT) && (vpn > 0) ) |
---|
[632] | 2556 | printk("\n[%s] thread[%x,%x] get pte from ref gpt / attr %x / ppn %x\n", |
---|
| 2557 | __FUNCTION__, this->process->pid, this->trdid, ref_attr, ref_ppn ); |
---|
| 2558 | #endif |
---|
| 2559 | |
---|
| 2560 | if( ref_attr & GPT_MAPPED ) // false page fault |
---|
[585] | 2561 | { |
---|
[629] | 2562 | // update local GPT from reference GPT values |
---|
[632] | 2563 | // this unlocks the PTE in local GPT |
---|
[629] | 2564 | hal_gpt_set_pte( local_gpt_xp, |
---|
| 2565 | vpn, |
---|
| 2566 | ref_attr, |
---|
| 2567 | ref_ppn ); |
---|
[585] | 2568 | |
---|
[635] | 2569 | #if (DEBUG_VMM_HANDLE_PAGE_FAULT & 1) |
---|
| 2570 | if( (start_cycle > DEBUG_VMM_HANDLE_PAGE_FAULT) && (vpn > 0) ) |
---|
[632] | 2571 | printk("\n[%s] thread[%x,%x] updated local gpt for a false pgfault\n", |
---|
| 2572 | __FUNCTION__, this->process->pid, this->trdid ); |
---|
| 2573 | #endif |
---|
| 2574 | |
---|
| 2575 | // unlock the PTE in reference GPT |
---|
| 2576 | hal_gpt_unlock_pte( ref_gpt_xp, vpn ); |
---|
| 2577 | |
---|
[635] | 2578 | #if (DEBUG_VMM_HANDLE_PAGE_FAULT &1) |
---|
| 2579 | if( (start_cycle > DEBUG_VMM_HANDLE_PAGE_FAULT) && (vpn > 0) ) |
---|
[632] | 2580 | printk("\n[%s] thread[%x,%x] unlock the ref gpt after a false pgfault\n", |
---|
| 2581 | __FUNCTION__, this->process->pid, this->trdid ); |
---|
| 2582 | #endif |
---|
| 2583 | |
---|
[629] | 2584 | #if (CONFIG_INSTRUMENTATION_PGFAULTS || DEBUG_VMM_HANDLE_PAGE_FAULT) |
---|
| 2585 | uint32_t end_cycle = (uint32_t)hal_get_cycles(); |
---|
[641] | 2586 | uint32_t cost = end_cycle - start_cycle; |
---|
[629] | 2587 | #endif |
---|
| 2588 | |
---|
[585] | 2589 | #if DEBUG_VMM_HANDLE_PAGE_FAULT |
---|
[635] | 2590 | if( (end_cycle > DEBUG_VMM_HANDLE_PAGE_FAULT) && (vpn > 0) ) |
---|
[632] | 2591 | printk("\n[%s] thread[%x,%x] handled false pgfault / ppn %x / attr %x / cycle %d\n", |
---|
| 2592 | __FUNCTION__, this->process->pid, this->trdid, ref_ppn, ref_attr, end_cycle ); |
---|
[433] | 2593 | #endif |
---|
[406] | 2594 | |
---|
[629] | 2595 | #if CONFIG_INSTRUMENTATION_PGFAULTS |
---|
| 2596 | this->info.false_pgfault_nr++; |
---|
[641] | 2597 | this->info.false_pgfault_cost += cost; |
---|
| 2598 | if( cost > this->info.false_pgfault_max ) this->info.false_pgfault_max = cost; |
---|
[629] | 2599 | #endif |
---|
| 2600 | return EXCP_NON_FATAL; |
---|
| 2601 | } |
---|
[632] | 2602 | else // true page fault |
---|
[629] | 2603 | { |
---|
[585] | 2604 | // allocate and initialise a physical page depending on the vseg type |
---|
[629] | 2605 | error = vmm_get_one_ppn( vseg , vpn , &ppn ); |
---|
[1] | 2606 | |
---|
[585] | 2607 | if( error ) |
---|
| 2608 | { |
---|
| 2609 | printk("\n[ERROR] in %s : no memory / process = %x / vpn = %x\n", |
---|
| 2610 | __FUNCTION__ , process->pid , vpn ); |
---|
[313] | 2611 | |
---|
[632] | 2612 | // unlock PTE in local GPT and in reference GPT |
---|
[629] | 2613 | hal_gpt_unlock_pte( local_gpt_xp , vpn ); |
---|
[632] | 2614 | hal_gpt_unlock_pte( ref_gpt_xp , vpn ); |
---|
[585] | 2615 | |
---|
[629] | 2616 | return EXCP_KERNEL_PANIC; |
---|
[585] | 2617 | } |
---|
[1] | 2618 | |
---|
[629] | 2619 | // define attr from vseg flags |
---|
[632] | 2620 | attr = GPT_MAPPED | GPT_SMALL | GPT_READABLE; |
---|
[629] | 2621 | if( vseg->flags & VSEG_USER ) attr |= GPT_USER; |
---|
| 2622 | if( vseg->flags & VSEG_WRITE ) attr |= GPT_WRITABLE; |
---|
| 2623 | if( vseg->flags & VSEG_EXEC ) attr |= GPT_EXECUTABLE; |
---|
| 2624 | if( vseg->flags & VSEG_CACHE ) attr |= GPT_CACHABLE; |
---|
[585] | 2625 | |
---|
[635] | 2626 | #if (DEBUG_VMM_HANDLE_PAGE_FAULT & 1) |
---|
| 2627 | if( (start_cycle > DEBUG_VMM_HANDLE_PAGE_FAULT) && (vpn > 0) ) |
---|
[632] | 2628 | printk("\n[%s] thread[%x,%x] build a new PTE for a true pgfault\n", |
---|
| 2629 | __FUNCTION__, this->process->pid, this->trdid ); |
---|
| 2630 | #endif |
---|
[629] | 2631 | // set PTE in reference GPT |
---|
[632] | 2632 | // this unlock the PTE |
---|
[629] | 2633 | hal_gpt_set_pte( ref_gpt_xp, |
---|
| 2634 | vpn, |
---|
| 2635 | attr, |
---|
| 2636 | ppn ); |
---|
| 2637 | |
---|
[635] | 2638 | #if (DEBUG_VMM_HANDLE_PAGE_FAULT & 1) |
---|
| 2639 | if( (start_cycle > DEBUG_VMM_HANDLE_PAGE_FAULT) && (vpn > 0) ) |
---|
[632] | 2640 | printk("\n[%s] thread[%x,%x] set new PTE in ref gpt for a true page fault\n", |
---|
| 2641 | __FUNCTION__, this->process->pid, this->trdid ); |
---|
| 2642 | #endif |
---|
| 2643 | |
---|
[629] | 2644 | // set PTE in local GPT |
---|
[632] | 2645 | // this unlock the PTE |
---|
[629] | 2646 | hal_gpt_set_pte( local_gpt_xp, |
---|
| 2647 | vpn, |
---|
| 2648 | attr, |
---|
| 2649 | ppn ); |
---|
| 2650 | |
---|
| 2651 | #if (CONFIG_INSTRUMENTATION_PGFAULTS || DEBUG_VMM_HANDLE_PAGE_FAULT) |
---|
| 2652 | uint32_t end_cycle = (uint32_t)hal_get_cycles(); |
---|
[641] | 2653 | uint32_t cost = end_cycle - start_cycle; |
---|
[629] | 2654 | #endif |
---|
| 2655 | |
---|
[440] | 2656 | #if DEBUG_VMM_HANDLE_PAGE_FAULT |
---|
[635] | 2657 | if( (end_cycle > DEBUG_VMM_HANDLE_PAGE_FAULT) && (vpn > 0) ) |
---|
[632] | 2658 | printk("\n[%s] thread[%x,%x] handled global pgfault / ppn %x / attr %x / cycle %d\n", |
---|
| 2659 | __FUNCTION__, this->process->pid, this->trdid, ppn, attr, end_cycle ); |
---|
[435] | 2660 | #endif |
---|
[629] | 2661 | |
---|
| 2662 | #if CONFIG_INSTRUMENTATION_PGFAULTS |
---|
| 2663 | this->info.global_pgfault_nr++; |
---|
[641] | 2664 | this->info.global_pgfault_cost += cost; |
---|
| 2665 | if( cost > this->info.global_pgfault_max ) this->info.global_pgfault_max = cost; |
---|
[629] | 2666 | #endif |
---|
| 2667 | return EXCP_NON_FATAL; |
---|
| 2668 | } |
---|
[585] | 2669 | } |
---|
| 2670 | } |
---|
[629] | 2671 | else // page has been locally mapped by another concurrent thread |
---|
| 2672 | { |
---|
[632] | 2673 | // unlock the PTE in local GPT |
---|
[629] | 2674 | hal_gpt_unlock_pte( local_gpt_xp , vpn ); |
---|
| 2675 | |
---|
[632] | 2676 | #if (CONFIG_INSTRUMENTATION_PGFAULTS || DEBUG_VMM_HANDLE_PAGE_FAULT) |
---|
| 2677 | uint32_t end_cycle = (uint32_t)hal_get_cycles(); |
---|
[641] | 2678 | uint32_t cost = end_cycle - start_cycle; |
---|
[632] | 2679 | #endif |
---|
| 2680 | |
---|
| 2681 | #if DEBUG_VMM_HANDLE_PAGE_FAULT |
---|
[635] | 2682 | if( (end_cycle > DEBUG_VMM_HANDLE_PAGE_FAULT) && (vpn > 0) ) |
---|
[632] | 2683 | printk("\n[%s] handled by another thread / vpn %x / ppn %x / attr %x / cycle %d\n", |
---|
| 2684 | __FUNCTION__, vpn, ppn, attr, end_cycle ); |
---|
| 2685 | #endif |
---|
| 2686 | |
---|
| 2687 | #if CONFIG_INSTRUMENTATION_PGFAULTS |
---|
| 2688 | this->info.false_pgfault_nr++; |
---|
[641] | 2689 | this->info.false_pgfault_cost += cost; |
---|
| 2690 | if( cost > this->info.false_pgfault_max ) this->info.false_pgfault_max = cost; |
---|
[632] | 2691 | #endif |
---|
[629] | 2692 | return EXCP_NON_FATAL; |
---|
| 2693 | } |
---|
| 2694 | |
---|
[585] | 2695 | } // end vmm_handle_page_fault() |
---|
[435] | 2696 | |
---|
[585] | 2697 | //////////////////////////////////////////// |
---|
| 2698 | error_t vmm_handle_cow( process_t * process, |
---|
| 2699 | vpn_t vpn ) |
---|
| 2700 | { |
---|
| 2701 | vseg_t * vseg; // vseg containing vpn |
---|
[629] | 2702 | xptr_t gpt_xp; // extended pointer on GPT (local or reference) |
---|
| 2703 | gpt_t * gpt_ptr; // local pointer on GPT (local or reference) |
---|
| 2704 | cxy_t gpt_cxy; // GPT cluster identifier |
---|
[585] | 2705 | uint32_t old_attr; // current PTE_ATTR value |
---|
| 2706 | ppn_t old_ppn; // current PTE_PPN value |
---|
| 2707 | uint32_t new_attr; // new PTE_ATTR value |
---|
| 2708 | ppn_t new_ppn; // new PTE_PPN value |
---|
[629] | 2709 | cxy_t ref_cxy; // reference process cluster |
---|
| 2710 | process_t * ref_ptr; // local pointer on reference process |
---|
[585] | 2711 | error_t error; |
---|
[1] | 2712 | |
---|
[629] | 2713 | thread_t * this = CURRENT_THREAD; |
---|
[625] | 2714 | |
---|
[585] | 2715 | #if DEBUG_VMM_HANDLE_COW |
---|
[629] | 2716 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
[640] | 2717 | if( (DEBUG_VMM_HANDLE_COW < cycle) && (vpn > 0) ) |
---|
[595] | 2718 | printk("\n[%s] thread[%x,%x] enter for vpn %x / core[%x,%d] / cycle %d\n", |
---|
[619] | 2719 | __FUNCTION__, this->process->pid, this->trdid, vpn, local_cxy, this->core->lid, cycle ); |
---|
[629] | 2720 | #endif |
---|
| 2721 | |
---|
[635] | 2722 | #if ((DEBUG_VMM_HANDLE_COW & 3) == 3 ) |
---|
[640] | 2723 | hal_vmm_display( XPTR( local_cxy , process ) , true ); |
---|
[585] | 2724 | #endif |
---|
| 2725 | |
---|
| 2726 | // get local vseg |
---|
| 2727 | error = vmm_get_vseg( process, |
---|
| 2728 | (intptr_t)vpn<<CONFIG_PPM_PAGE_SHIFT, |
---|
| 2729 | &vseg ); |
---|
[440] | 2730 | if( error ) |
---|
[1] | 2731 | { |
---|
[629] | 2732 | printk("\n[ERROR] in %s : vpn %x in thread[%x,%x] not in a registered vseg\n", |
---|
[625] | 2733 | __FUNCTION__, vpn, process->pid, this->trdid ); |
---|
[585] | 2734 | |
---|
[629] | 2735 | return EXCP_USER_ERROR; |
---|
[440] | 2736 | } |
---|
[407] | 2737 | |
---|
[629] | 2738 | #if DEBUG_VMM_HANDLE_COW |
---|
[640] | 2739 | if( (DEBUG_VMM_HANDLE_COW < cycle) && (vpn > 0) ) |
---|
[629] | 2740 | printk("\n[%s] thread[%x,%x] get vseg %s\n", |
---|
| 2741 | __FUNCTION__, this->process->pid, this->trdid, vseg_type_str(vseg->type) ); |
---|
[619] | 2742 | #endif |
---|
| 2743 | |
---|
[629] | 2744 | // get reference process cluster and local pointer |
---|
[585] | 2745 | ref_cxy = GET_CXY( process->ref_xp ); |
---|
| 2746 | ref_ptr = GET_PTR( process->ref_xp ); |
---|
[407] | 2747 | |
---|
[629] | 2748 | // build pointers on relevant GPT |
---|
| 2749 | // - access only local GPT for a private vseg |
---|
| 2750 | // - access reference GPT and all copies for a public vseg |
---|
[585] | 2751 | if( (vseg->type == VSEG_TYPE_STACK) || (vseg->type == VSEG_TYPE_CODE) ) |
---|
[440] | 2752 | { |
---|
[629] | 2753 | gpt_cxy = local_cxy; |
---|
| 2754 | gpt_ptr = &process->vmm.gpt; |
---|
| 2755 | gpt_xp = XPTR( gpt_cxy , gpt_ptr ); |
---|
[1] | 2756 | } |
---|
[440] | 2757 | else |
---|
[1] | 2758 | { |
---|
[629] | 2759 | gpt_cxy = ref_cxy; |
---|
| 2760 | gpt_ptr = &ref_ptr->vmm.gpt; |
---|
| 2761 | gpt_xp = XPTR( gpt_cxy , gpt_ptr ); |
---|
[1] | 2762 | } |
---|
| 2763 | |
---|
[629] | 2764 | // lock target PTE in relevant GPT (local or reference) |
---|
[632] | 2765 | // and get current PTE value |
---|
[629] | 2766 | error = hal_gpt_lock_pte( gpt_xp, |
---|
| 2767 | vpn, |
---|
| 2768 | &old_attr, |
---|
| 2769 | &old_ppn ); |
---|
| 2770 | if( error ) |
---|
| 2771 | { |
---|
| 2772 | printk("\n[PANIC] in %s : cannot lock PTE in GPT / cxy %x / vpn %x / process %x\n", |
---|
| 2773 | __FUNCTION__ , gpt_cxy, vpn , process->pid ); |
---|
| 2774 | |
---|
| 2775 | return EXCP_KERNEL_PANIC; |
---|
| 2776 | } |
---|
[441] | 2777 | |
---|
[629] | 2778 | #if DEBUG_VMM_HANDLE_COW |
---|
[640] | 2779 | if( (DEBUG_VMM_HANDLE_COW < cycle) && (vpn > 0) ) |
---|
[619] | 2780 | printk("\n[%s] thread[%x,%x] get pte for vpn %x : ppn %x / attr %x\n", |
---|
| 2781 | __FUNCTION__, this->process->pid, this->trdid, vpn, old_ppn, old_attr ); |
---|
| 2782 | #endif |
---|
| 2783 | |
---|
[629] | 2784 | // return user error if COW attribute not set or PTE2 unmapped |
---|
| 2785 | if( ((old_attr & GPT_COW) == 0) || ((old_attr & GPT_MAPPED) == 0) ) |
---|
[585] | 2786 | { |
---|
[629] | 2787 | hal_gpt_unlock_pte( gpt_xp , vpn ); |
---|
[407] | 2788 | |
---|
[629] | 2789 | return EXCP_USER_ERROR; |
---|
[407] | 2790 | } |
---|
| 2791 | |
---|
[619] | 2792 | // get pointers on physical page descriptor |
---|
[585] | 2793 | xptr_t page_xp = ppm_ppn2page( old_ppn ); |
---|
| 2794 | cxy_t page_cxy = GET_CXY( page_xp ); |
---|
| 2795 | page_t * page_ptr = GET_PTR( page_xp ); |
---|
[435] | 2796 | |
---|
[585] | 2797 | // get extended pointers on forks and lock field in page descriptor |
---|
| 2798 | xptr_t forks_xp = XPTR( page_cxy , &page_ptr->forks ); |
---|
| 2799 | xptr_t forks_lock_xp = XPTR( page_cxy , &page_ptr->lock ); |
---|
[407] | 2800 | |
---|
[585] | 2801 | // take lock protecting "forks" counter |
---|
| 2802 | remote_busylock_acquire( forks_lock_xp ); |
---|
[407] | 2803 | |
---|
[585] | 2804 | // get number of pending forks from page descriptor |
---|
| 2805 | uint32_t forks = hal_remote_l32( forks_xp ); |
---|
[441] | 2806 | |
---|
[629] | 2807 | #if DEBUG_VMM_HANDLE_COW |
---|
[640] | 2808 | if( (DEBUG_VMM_HANDLE_COW < cycle) && (vpn > 0) ) |
---|
[619] | 2809 | printk("\n[%s] thread[%x,%x] get forks = %d for vpn %x\n", |
---|
| 2810 | __FUNCTION__, this->process->pid, this->trdid, forks, vpn ); |
---|
| 2811 | #endif |
---|
| 2812 | |
---|
[585] | 2813 | if( forks ) // pending fork => allocate a new page, and copy old to new |
---|
| 2814 | { |
---|
[619] | 2815 | // decrement pending forks counter in page descriptor |
---|
| 2816 | hal_remote_atomic_add( forks_xp , -1 ); |
---|
| 2817 | |
---|
| 2818 | // release lock protecting "forks" counter |
---|
| 2819 | remote_busylock_release( forks_lock_xp ); |
---|
| 2820 | |
---|
[629] | 2821 | // allocate a new physical page depending on vseg type |
---|
[585] | 2822 | page_xp = vmm_page_allocate( vseg , vpn ); |
---|
[619] | 2823 | |
---|
[585] | 2824 | if( page_xp == XPTR_NULL ) |
---|
| 2825 | { |
---|
| 2826 | printk("\n[PANIC] in %s : no memory for vpn %x in process %x\n", |
---|
| 2827 | __FUNCTION__ , vpn, process->pid ); |
---|
[441] | 2828 | |
---|
[629] | 2829 | hal_gpt_unlock_pte( gpt_xp , vpn ); |
---|
[441] | 2830 | |
---|
[585] | 2831 | return EXCP_KERNEL_PANIC; |
---|
| 2832 | } |
---|
[441] | 2833 | |
---|
[585] | 2834 | // compute allocated page PPN |
---|
| 2835 | new_ppn = ppm_page2ppn( page_xp ); |
---|
[441] | 2836 | |
---|
[629] | 2837 | #if DEBUG_VMM_HANDLE_COW |
---|
[640] | 2838 | if( (DEBUG_VMM_HANDLE_COW < cycle) && (vpn > 0) ) |
---|
[619] | 2839 | printk("\n[%s] thread[%x,%x] get new ppn %x for vpn %x\n", |
---|
| 2840 | __FUNCTION__, this->process->pid, this->trdid, new_ppn, vpn ); |
---|
| 2841 | #endif |
---|
| 2842 | |
---|
[585] | 2843 | // copy old page content to new page |
---|
[619] | 2844 | hal_remote_memcpy( ppm_ppn2base( new_ppn ), |
---|
| 2845 | ppm_ppn2base( old_ppn ), |
---|
| 2846 | CONFIG_PPM_PAGE_SIZE ); |
---|
[441] | 2847 | |
---|
[629] | 2848 | #if DEBUG_VMM_HANDLE_COW |
---|
[640] | 2849 | if( (DEBUG_VMM_HANDLE_COW < cycle) && (vpn > 0) ) |
---|
[619] | 2850 | printk("\n[%s] thread[%x,%x] copied old page to new page\n", |
---|
| 2851 | __FUNCTION__, this->process->pid, this->trdid ); |
---|
[585] | 2852 | #endif |
---|
[440] | 2853 | |
---|
[585] | 2854 | } |
---|
| 2855 | else // no pending fork => keep the existing page |
---|
| 2856 | { |
---|
[619] | 2857 | // release lock protecting "forks" counter |
---|
| 2858 | remote_busylock_release( forks_lock_xp ); |
---|
[1] | 2859 | |
---|
[585] | 2860 | #if(DEBUG_VMM_HANDLE_COW & 1) |
---|
[640] | 2861 | if( (DEBUG_VMM_HANDLE_COW < cycle) && (vpn > 0) ) |
---|
[635] | 2862 | printk("\n[%s] thread[%x,%x] no pending forks / keep existing PPN %x\n", |
---|
[619] | 2863 | __FUNCTION__, this->process->pid, this->trdid, old_ppn ); |
---|
[585] | 2864 | #endif |
---|
| 2865 | new_ppn = old_ppn; |
---|
| 2866 | } |
---|
[1] | 2867 | |
---|
[629] | 2868 | // build new_attr : set WRITABLE, reset COW, reset LOCKED |
---|
| 2869 | new_attr = (((old_attr | GPT_WRITABLE) & (~GPT_COW)) & (~GPT_LOCKED)); |
---|
[585] | 2870 | |
---|
[635] | 2871 | #if(DEBUG_VMM_HANDLE_COW & 1) |
---|
[640] | 2872 | if( (DEBUG_VMM_HANDLE_COW < cycle) && (vpn > 0) ) |
---|
[635] | 2873 | printk("\n[%s] thread[%x,%x] new_attr %x / new_ppn %x\n", |
---|
| 2874 | __FUNCTION__, this->process->pid, this->trdid, new_attr, new_ppn ); |
---|
| 2875 | #endif |
---|
| 2876 | |
---|
[629] | 2877 | // update the relevant GPT(s) |
---|
| 2878 | // - private vseg => update only the local GPT |
---|
| 2879 | // - public vseg => update the reference GPT AND all the GPT copies |
---|
[585] | 2880 | if( (vseg->type == VSEG_TYPE_STACK) || (vseg->type == VSEG_TYPE_CODE) ) |
---|
[1] | 2881 | { |
---|
[635] | 2882 | // set new PTE in local gpt |
---|
[585] | 2883 | hal_gpt_set_pte( gpt_xp, |
---|
| 2884 | vpn, |
---|
| 2885 | new_attr, |
---|
| 2886 | new_ppn ); |
---|
[1] | 2887 | } |
---|
[585] | 2888 | else |
---|
[1] | 2889 | { |
---|
[640] | 2890 | // set new PTE in all GPT copies |
---|
| 2891 | vmm_global_update_pte( process, |
---|
| 2892 | vpn, |
---|
| 2893 | new_attr, |
---|
| 2894 | new_ppn ); |
---|
[1] | 2895 | } |
---|
| 2896 | |
---|
[585] | 2897 | #if DEBUG_VMM_HANDLE_COW |
---|
| 2898 | cycle = (uint32_t)hal_get_cycles(); |
---|
[640] | 2899 | if( (DEBUG_VMM_HANDLE_COW < cycle) && (vpn > 0) ) |
---|
[595] | 2900 | printk("\n[%s] thread[%x,%x] exit for vpn %x / core[%x,%d] / cycle %d\n", |
---|
[619] | 2901 | __FUNCTION__, this->process->pid, this->trdid, vpn, local_cxy, this->core->lid, cycle ); |
---|
[585] | 2902 | #endif |
---|
[313] | 2903 | |
---|
[635] | 2904 | #if ((DEBUG_VMM_HANDLE_COW & 3) == 3) |
---|
[640] | 2905 | hal_vmm_display( XPTR( local_cxy , process ) , true ); |
---|
[635] | 2906 | #endif |
---|
| 2907 | |
---|
[585] | 2908 | return EXCP_NON_FATAL; |
---|
[1] | 2909 | |
---|
[585] | 2910 | } // end vmm_handle_cow() |
---|
| 2911 | |
---|