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