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