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