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