[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> |
---|
| 30 | #include <printk.h> |
---|
[23] | 31 | #include <memcpy.h> |
---|
[1] | 32 | #include <rwlock.h> |
---|
| 33 | #include <list.h> |
---|
| 34 | #include <bits.h> |
---|
| 35 | #include <process.h> |
---|
| 36 | #include <thread.h> |
---|
| 37 | #include <vseg.h> |
---|
| 38 | #include <cluster.h> |
---|
| 39 | #include <scheduler.h> |
---|
| 40 | #include <vfs.h> |
---|
| 41 | #include <mapper.h> |
---|
| 42 | #include <page.h> |
---|
| 43 | #include <kmem.h> |
---|
| 44 | #include <vmm.h> |
---|
| 45 | |
---|
| 46 | ////////////////////////////////////////////////////////////////////////////////// |
---|
| 47 | // Extern global variables |
---|
| 48 | ////////////////////////////////////////////////////////////////////////////////// |
---|
| 49 | |
---|
| 50 | extern process_t process_zero; // defined in cluster.c file |
---|
| 51 | |
---|
| 52 | |
---|
| 53 | //////////////////////////////////// |
---|
| 54 | void vmm_init( process_t * process ) |
---|
[21] | 55 | { |
---|
[1] | 56 | error_t error; |
---|
| 57 | vseg_t * vseg_kentry; |
---|
| 58 | vseg_t * vseg_args; |
---|
| 59 | vseg_t * vseg_envs; |
---|
| 60 | intptr_t base; |
---|
| 61 | intptr_t size; |
---|
| 62 | |
---|
[407] | 63 | vmm_dmsg("\n[DBG] %s : core[%x,%d] enters for process %x\n", |
---|
| 64 | __FUNCTION__ , local_cxy , CURRENT_THREAD->core->lid , process->pid ); |
---|
[204] | 65 | |
---|
[1] | 66 | // get pointer on VMM |
---|
| 67 | vmm_t * vmm = &process->vmm; |
---|
| 68 | |
---|
[407] | 69 | // initialize local list of vsegs |
---|
| 70 | vmm->vsegs_nr = 0; |
---|
| 71 | list_root_init( &vmm->vsegs_root ); |
---|
| 72 | rwlock_init( &vmm->vsegs_lock ); |
---|
| 73 | |
---|
[204] | 74 | assert( ((CONFIG_VMM_KENTRY_SIZE + CONFIG_VMM_ARGS_SIZE + CONFIG_VMM_ENVS_SIZE) |
---|
| 75 | <= CONFIG_VMM_ELF_BASE) , __FUNCTION__ , "UTILS zone too small\n" ); |
---|
[21] | 76 | |
---|
[204] | 77 | assert( (CONFIG_THREAD_MAX_PER_CLUSTER <= 32) , __FUNCTION__ , |
---|
| 78 | "no more than 32 threads per cluster for a single process\n"); |
---|
[1] | 79 | |
---|
[204] | 80 | assert( ((CONFIG_VMM_STACK_SIZE * CONFIG_THREAD_MAX_PER_CLUSTER) <= |
---|
| 81 | (CONFIG_VMM_VSPACE_SIZE - CONFIG_VMM_STACK_BASE)) , __FUNCTION__ , |
---|
| 82 | "STACK zone too small\n"); |
---|
[1] | 83 | |
---|
| 84 | // register kentry vseg in VMM |
---|
[406] | 85 | base = CONFIG_VMM_KENTRY_BASE << CONFIG_PPM_PAGE_SHIFT; |
---|
[1] | 86 | size = CONFIG_VMM_KENTRY_SIZE << CONFIG_PPM_PAGE_SHIFT; |
---|
[406] | 87 | |
---|
[407] | 88 | vseg_kentry = vmm_create_vseg( process, |
---|
| 89 | VSEG_TYPE_CODE, |
---|
| 90 | base, |
---|
| 91 | size, |
---|
| 92 | 0, // file_offset unused |
---|
| 93 | 0, // file_size unused |
---|
| 94 | XPTR_NULL, // mapper_xp unused |
---|
| 95 | local_cxy ); |
---|
[204] | 96 | |
---|
| 97 | assert( (vseg_kentry != NULL) , __FUNCTION__ , "cannot register kentry vseg\n" ); |
---|
| 98 | |
---|
[406] | 99 | vmm->kent_vpn_base = base; |
---|
[1] | 100 | |
---|
[406] | 101 | // register args vseg in VMM |
---|
| 102 | base = (CONFIG_VMM_KENTRY_BASE + |
---|
| 103 | CONFIG_VMM_KENTRY_SIZE ) << CONFIG_PPM_PAGE_SHIFT; |
---|
[1] | 104 | size = CONFIG_VMM_ARGS_SIZE << CONFIG_PPM_PAGE_SHIFT; |
---|
[406] | 105 | |
---|
[407] | 106 | vseg_args = vmm_create_vseg( process, |
---|
| 107 | VSEG_TYPE_DATA, |
---|
| 108 | base, |
---|
| 109 | size, |
---|
| 110 | 0, // file_offset unused |
---|
| 111 | 0, // file_size unused |
---|
| 112 | XPTR_NULL, // mapper_xp unused |
---|
| 113 | local_cxy ); |
---|
[204] | 114 | |
---|
[407] | 115 | assert( (vseg_args != NULL) , __FUNCTION__ , "cannot create args vseg\n" ); |
---|
[204] | 116 | |
---|
[406] | 117 | vmm->args_vpn_base = base; |
---|
[1] | 118 | |
---|
| 119 | // register the envs vseg in VMM |
---|
[406] | 120 | base = (CONFIG_VMM_KENTRY_BASE + |
---|
| 121 | CONFIG_VMM_KENTRY_SIZE + |
---|
| 122 | CONFIG_VMM_ARGS_SIZE ) << CONFIG_PPM_PAGE_SHIFT; |
---|
[1] | 123 | size = CONFIG_VMM_ENVS_SIZE << CONFIG_PPM_PAGE_SHIFT; |
---|
[406] | 124 | |
---|
[407] | 125 | vseg_envs = vmm_create_vseg( process, |
---|
| 126 | VSEG_TYPE_DATA, |
---|
| 127 | base, |
---|
| 128 | size, |
---|
| 129 | 0, // file_offset unused |
---|
| 130 | 0, // file_size unused |
---|
| 131 | XPTR_NULL, // mapper_xp unused |
---|
| 132 | local_cxy ); |
---|
[204] | 133 | |
---|
[407] | 134 | assert( (vseg_envs != NULL) , __FUNCTION__ , "cannot create envs vseg\n" ); |
---|
[204] | 135 | |
---|
[406] | 136 | vmm->envs_vpn_base = base; |
---|
[1] | 137 | |
---|
| 138 | // initialize generic page table |
---|
| 139 | error = hal_gpt_create( &vmm->gpt ); |
---|
| 140 | |
---|
[204] | 141 | assert( (error == 0) , __FUNCTION__ , "cannot initialize page table\n"); |
---|
| 142 | |
---|
[1] | 143 | // initialize STACK allocator |
---|
| 144 | vmm->stack_mgr.bitmap = 0; |
---|
| 145 | vmm->stack_mgr.vpn_base = CONFIG_VMM_STACK_BASE; |
---|
| 146 | |
---|
| 147 | // initialize MMAP allocator |
---|
[407] | 148 | vmm->mmap_mgr.vpn_base = CONFIG_VMM_HEAP_BASE; |
---|
| 149 | vmm->mmap_mgr.vpn_size = CONFIG_VMM_STACK_BASE - CONFIG_VMM_HEAP_BASE; |
---|
| 150 | vmm->mmap_mgr.first_free_vpn = CONFIG_VMM_HEAP_BASE; |
---|
[1] | 151 | uint32_t i; |
---|
| 152 | for( i = 0 ; i < 32 ; i++ ) list_root_init( &vmm->mmap_mgr.zombi_list[i] ); |
---|
| 153 | |
---|
[21] | 154 | // initialize instrumentation counters |
---|
[1] | 155 | vmm->pgfault_nr = 0; |
---|
| 156 | vmm->u_err_nr = 0; |
---|
| 157 | vmm->m_err_nr = 0; |
---|
| 158 | |
---|
[124] | 159 | hal_fence(); |
---|
[1] | 160 | |
---|
[407] | 161 | vmm_dmsg("\n[DBG] %s : core[%x,%d] exit for process %x / entry_point = %x\n", |
---|
| 162 | __FUNCTION__ , local_cxy , CURRENT_THREAD->core->lid , |
---|
| 163 | process->pid , process->vmm.entry_point ); |
---|
[204] | 164 | |
---|
| 165 | } // end vmm_init() |
---|
| 166 | |
---|
[407] | 167 | ////////////////////////////////////// |
---|
| 168 | void vmm_display( process_t * process, |
---|
| 169 | bool_t mapping ) |
---|
| 170 | { |
---|
| 171 | vmm_t * vmm = &process->vmm; |
---|
| 172 | gpt_t * gpt = &vmm->gpt; |
---|
| 173 | |
---|
| 174 | printk("\n***** VSL and GPT for process %x\n\n", |
---|
| 175 | process->pid ); |
---|
| 176 | |
---|
| 177 | // get lock protecting the vseg list |
---|
| 178 | rwlock_rd_lock( &vmm->vsegs_lock ); |
---|
| 179 | |
---|
| 180 | // scan the list of vsegs |
---|
| 181 | list_entry_t * iter; |
---|
| 182 | vseg_t * vseg; |
---|
| 183 | LIST_FOREACH( &vmm->vsegs_root , iter ) |
---|
| 184 | { |
---|
| 185 | vseg = LIST_ELEMENT( iter , vseg_t , list ); |
---|
| 186 | printk(" - %s : base = %X / size = %X / npages = %d\n", |
---|
| 187 | vseg_type_str( vseg->type ) , vseg->min , vseg->max - vseg->min , vseg->vpn_size ); |
---|
| 188 | |
---|
| 189 | if( mapping ) |
---|
| 190 | { |
---|
| 191 | vpn_t vpn; |
---|
| 192 | ppn_t ppn; |
---|
| 193 | uint32_t attr; |
---|
| 194 | vpn_t base = vseg->vpn_base; |
---|
| 195 | vpn_t size = vseg->vpn_size; |
---|
| 196 | for( vpn = base ; vpn < (base+size) ; vpn++ ) |
---|
| 197 | { |
---|
| 198 | hal_gpt_get_pte( gpt , vpn , &attr , &ppn ); |
---|
| 199 | if( attr & GPT_MAPPED ) |
---|
| 200 | { |
---|
| 201 | printk(" . vpn = %X / attr = %X / ppn = %X\n", vpn , attr , ppn ); |
---|
| 202 | } |
---|
| 203 | } |
---|
| 204 | } |
---|
| 205 | } |
---|
| 206 | |
---|
| 207 | // release the lock |
---|
| 208 | rwlock_rd_unlock( &vmm->vsegs_lock ); |
---|
| 209 | } |
---|
| 210 | |
---|
[23] | 211 | ////////////////////////////////////////// |
---|
| 212 | error_t vmm_copy( process_t * dst_process, |
---|
| 213 | process_t * src_process ) |
---|
| 214 | { |
---|
| 215 | error_t error; |
---|
| 216 | |
---|
| 217 | vmm_t * src_vmm = &src_process->vmm; |
---|
| 218 | vmm_t * dst_vmm = &dst_process->vmm; |
---|
| 219 | |
---|
| 220 | // take the src_vmm vsegs_lock |
---|
| 221 | rwlock_wr_lock( &src_vmm->vsegs_lock ); |
---|
| 222 | |
---|
[178] | 223 | // initialize dst_vmm vsegs_lock |
---|
[23] | 224 | rwlock_init( &dst_vmm->vsegs_lock ); |
---|
| 225 | |
---|
[407] | 226 | // initialize the dst_vmm vsegs list |
---|
[23] | 227 | dst_vmm->vsegs_nr = 0; |
---|
| 228 | list_root_init( &dst_vmm->vsegs_root ); |
---|
| 229 | |
---|
[407] | 230 | // initialize generic page table |
---|
| 231 | error = hal_gpt_create( &dst_vmm->gpt ); |
---|
| 232 | |
---|
| 233 | if( error ) |
---|
| 234 | { |
---|
| 235 | printk("\n[ERROR] in %s : cannot initialize page table\n", __FUNCTION__ ); |
---|
| 236 | return ENOMEM; |
---|
| 237 | } |
---|
| 238 | |
---|
| 239 | // loop on SRC VSL to register vsegs copies in DST VSL |
---|
| 240 | // and copy valid PTEs from SRC GPT to DST GPT |
---|
[23] | 241 | list_entry_t * iter; |
---|
| 242 | vseg_t * src_vseg; |
---|
| 243 | vseg_t * dst_vseg; |
---|
| 244 | LIST_FOREACH( &src_vmm->vsegs_root , iter ) |
---|
| 245 | { |
---|
| 246 | // get pointer on current src_vseg |
---|
| 247 | src_vseg = LIST_ELEMENT( iter , vseg_t , list ); |
---|
| 248 | |
---|
| 249 | // allocate memory for a new dst_vseg |
---|
| 250 | dst_vseg = vseg_alloc(); |
---|
| 251 | |
---|
[178] | 252 | if( dst_vseg == NULL ) |
---|
[23] | 253 | { |
---|
| 254 | // release all allocated vsegs |
---|
| 255 | LIST_FOREACH( &dst_vmm->vsegs_root , iter ) |
---|
| 256 | { |
---|
| 257 | dst_vseg = LIST_ELEMENT( iter , vseg_t , list ); |
---|
| 258 | vseg_free( dst_vseg ); |
---|
| 259 | } |
---|
| 260 | return ENOMEM; |
---|
| 261 | } |
---|
| 262 | |
---|
| 263 | // copy src_vseg to dst_vseg |
---|
| 264 | vseg_init_from_ref( dst_vseg , XPTR( local_cxy , src_vseg ) ); |
---|
| 265 | |
---|
[407] | 266 | // register dst_vseg in DST VSL |
---|
[23] | 267 | vseg_attach( dst_vmm , dst_vseg ); |
---|
[407] | 268 | |
---|
| 269 | // copy SRC GPT to DST GPT / set COW for all writable vsegs, but the FILE type |
---|
| 270 | bool_t cow = (src_vseg->type != VSEG_TYPE_FILE) && (src_vseg->flags & VSEG_WRITE); |
---|
| 271 | error = hal_gpt_copy( &dst_vmm->gpt, |
---|
| 272 | &src_vmm->gpt, |
---|
| 273 | src_vseg->vpn_base, |
---|
| 274 | src_vseg->vpn_size, |
---|
| 275 | cow ); |
---|
| 276 | if( error ) |
---|
| 277 | { |
---|
| 278 | printk("\n[ERROR] in %s : cannot copy page GPT\n", __FUNCTION__ ); |
---|
| 279 | hal_gpt_destroy( &dst_vmm->gpt ); |
---|
| 280 | return ENOMEM; |
---|
| 281 | } |
---|
[23] | 282 | } |
---|
| 283 | |
---|
| 284 | // release the src_vmm vsegs_lock |
---|
| 285 | rwlock_wr_unlock( &src_vmm->vsegs_lock ); |
---|
| 286 | |
---|
| 287 | // initialize STACK allocator |
---|
| 288 | dst_vmm->stack_mgr.bitmap = 0; |
---|
| 289 | dst_vmm->stack_mgr.vpn_base = CONFIG_VMM_STACK_BASE; |
---|
| 290 | |
---|
| 291 | // initialize MMAP allocator |
---|
[407] | 292 | dst_vmm->mmap_mgr.vpn_base = CONFIG_VMM_HEAP_BASE; |
---|
| 293 | dst_vmm->mmap_mgr.vpn_size = CONFIG_VMM_STACK_BASE - CONFIG_VMM_HEAP_BASE; |
---|
| 294 | dst_vmm->mmap_mgr.first_free_vpn = CONFIG_VMM_HEAP_BASE; |
---|
[23] | 295 | uint32_t i; |
---|
| 296 | for( i = 0 ; i < 32 ; i++ ) list_root_init( &dst_vmm->mmap_mgr.zombi_list[i] ); |
---|
| 297 | |
---|
[178] | 298 | // initialize instrumentation counters |
---|
[23] | 299 | dst_vmm->pgfault_nr = 0; |
---|
| 300 | dst_vmm->u_err_nr = 0; |
---|
| 301 | dst_vmm->m_err_nr = 0; |
---|
| 302 | |
---|
| 303 | // copy base addresses |
---|
[178] | 304 | dst_vmm->kent_vpn_base = src_vmm->kent_vpn_base; |
---|
| 305 | dst_vmm->args_vpn_base = src_vmm->args_vpn_base; |
---|
| 306 | dst_vmm->envs_vpn_base = src_vmm->envs_vpn_base; |
---|
| 307 | dst_vmm->heap_vpn_base = src_vmm->heap_vpn_base; |
---|
| 308 | dst_vmm->code_vpn_base = src_vmm->code_vpn_base; |
---|
| 309 | dst_vmm->data_vpn_base = src_vmm->data_vpn_base; |
---|
[23] | 310 | |
---|
| 311 | dst_vmm->entry_point = src_vmm->entry_point; |
---|
| 312 | |
---|
[124] | 313 | hal_fence(); |
---|
[23] | 314 | |
---|
| 315 | return 0; |
---|
| 316 | |
---|
[204] | 317 | } // vmm_copy() |
---|
| 318 | |
---|
[1] | 319 | /////////////////////////////////////// |
---|
| 320 | void vmm_destroy( process_t * process ) |
---|
| 321 | { |
---|
| 322 | vseg_t * vseg; |
---|
| 323 | |
---|
| 324 | // get pointer on VMM |
---|
| 325 | vmm_t * vmm = &process->vmm; |
---|
| 326 | |
---|
| 327 | // get lock protecting vseg list |
---|
| 328 | rwlock_wr_lock( &vmm->vsegs_lock ); |
---|
| 329 | |
---|
| 330 | // remove all vsegs registered in vmm |
---|
| 331 | while( !list_is_empty( &vmm->vsegs_root ) ) |
---|
| 332 | { |
---|
| 333 | vseg = LIST_FIRST( &vmm->vsegs_root , vseg_t , list ); |
---|
| 334 | vseg_detach( vmm , vseg ); |
---|
| 335 | vseg_free( vseg ); |
---|
| 336 | } |
---|
| 337 | |
---|
| 338 | // release lock |
---|
| 339 | rwlock_wr_unlock(&vmm->vsegs_lock); |
---|
| 340 | |
---|
| 341 | // remove all vsegs from zombi_lists in MMAP allocator |
---|
| 342 | uint32_t i; |
---|
| 343 | for( i = 0 ; i<32 ; i++ ) |
---|
| 344 | { |
---|
| 345 | while( !list_is_empty( &vmm->mmap_mgr.zombi_list[i] ) ) |
---|
| 346 | { |
---|
| 347 | vseg = LIST_FIRST( &vmm->mmap_mgr.zombi_list[i] , vseg_t , list ); |
---|
| 348 | vseg_detach( vmm , vseg ); |
---|
| 349 | vseg_free( vseg ); |
---|
| 350 | } |
---|
| 351 | } |
---|
| 352 | |
---|
| 353 | // release memory allocated to the local page table |
---|
| 354 | hal_gpt_destroy( &vmm->gpt ); |
---|
| 355 | |
---|
[204] | 356 | } // end vmm_destroy() |
---|
| 357 | |
---|
[1] | 358 | ///////////////////////////////////////////////// |
---|
| 359 | vseg_t * vmm_check_conflict( process_t * process, |
---|
[21] | 360 | vpn_t vpn_base, |
---|
[1] | 361 | vpn_t vpn_size ) |
---|
| 362 | { |
---|
| 363 | vmm_t * vmm = &process->vmm; |
---|
| 364 | vseg_t * vseg; |
---|
| 365 | list_entry_t * iter; |
---|
| 366 | |
---|
| 367 | // scan the list of registered vsegs |
---|
| 368 | LIST_FOREACH( &vmm->vsegs_root , iter ) |
---|
| 369 | { |
---|
| 370 | vseg = LIST_ELEMENT( iter , vseg_t , list ); |
---|
[204] | 371 | |
---|
[21] | 372 | if( ((vpn_base + vpn_size) > vseg->vpn_base) && |
---|
| 373 | (vpn_base < (vseg->vpn_base + vseg->vpn_size)) ) return vseg; |
---|
[1] | 374 | } |
---|
| 375 | return NULL; |
---|
| 376 | |
---|
[204] | 377 | } // end vmm_check_conflict() |
---|
| 378 | |
---|
[1] | 379 | //////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 380 | // This static function is called by the vmm_create_vseg() function, and implements |
---|
| 381 | // the VMM stack_vseg specific allocator. |
---|
| 382 | //////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 383 | // @ vmm : pointer on VMM. |
---|
[21] | 384 | // @ vpn_base : (return value) first allocated page |
---|
[1] | 385 | // @ vpn_size : (return value) number of allocated pages |
---|
| 386 | //////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 387 | static error_t vmm_stack_alloc( vmm_t * vmm, |
---|
| 388 | vpn_t * vpn_base, |
---|
| 389 | vpn_t * vpn_size ) |
---|
| 390 | { |
---|
| 391 | // get stack allocator pointer |
---|
| 392 | stack_mgr_t * mgr = &vmm->stack_mgr; |
---|
| 393 | |
---|
| 394 | // get lock on stack allocator |
---|
| 395 | spinlock_lock( &mgr->lock ); |
---|
| 396 | |
---|
| 397 | // get first free slot index in bitmap |
---|
| 398 | int32_t index = bitmap_ffc( &mgr->bitmap , 4 ); |
---|
[179] | 399 | if( (index < 0) || (index > 31) ) |
---|
| 400 | { |
---|
| 401 | spinlock_unlock( &mgr->lock ); |
---|
| 402 | return ENOMEM; |
---|
| 403 | } |
---|
[1] | 404 | |
---|
| 405 | // update bitmap |
---|
| 406 | bitmap_set( &mgr->bitmap , index ); |
---|
[21] | 407 | |
---|
[1] | 408 | // release lock on stack allocator |
---|
| 409 | spinlock_unlock( &mgr->lock ); |
---|
| 410 | |
---|
[21] | 411 | // returns vpn_base, vpn_size (one page non allocated) |
---|
[1] | 412 | *vpn_base = mgr->vpn_base + index * CONFIG_VMM_STACK_SIZE + 1; |
---|
| 413 | *vpn_size = CONFIG_VMM_STACK_SIZE - 1; |
---|
| 414 | return 0; |
---|
| 415 | |
---|
[204] | 416 | } // end vmm_stack_alloc() |
---|
| 417 | |
---|
[1] | 418 | //////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 419 | // This static function is called by the vmm_create_vseg() function, and implements |
---|
| 420 | // the VMM MMAP specific allocator. |
---|
| 421 | //////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 422 | // @ vmm : [in] pointer on VMM. |
---|
| 423 | // @ npages : [in] requested number of pages. |
---|
[21] | 424 | // @ vpn_base : [out] first allocated page. |
---|
[1] | 425 | // @ vpn_size : [out] actual number of allocated pages. |
---|
| 426 | //////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 427 | static error_t vmm_mmap_alloc( vmm_t * vmm, |
---|
| 428 | vpn_t npages, |
---|
| 429 | vpn_t * vpn_base, |
---|
| 430 | vpn_t * vpn_size ) |
---|
| 431 | { |
---|
| 432 | uint32_t index; |
---|
| 433 | vseg_t * vseg; |
---|
| 434 | vpn_t base; |
---|
| 435 | vpn_t size; |
---|
[21] | 436 | vpn_t free; |
---|
[1] | 437 | |
---|
[21] | 438 | // mmap vseg size must be power of 2 |
---|
[1] | 439 | // compute actual size and index in zombi_list array |
---|
| 440 | size = POW2_ROUNDUP( npages ); |
---|
| 441 | index = bits_log2( size ); |
---|
| 442 | |
---|
| 443 | // get mmap allocator pointer |
---|
| 444 | mmap_mgr_t * mgr = &vmm->mmap_mgr; |
---|
| 445 | |
---|
| 446 | // get lock on mmap allocator |
---|
| 447 | spinlock_lock( &mgr->lock ); |
---|
| 448 | |
---|
| 449 | // get vseg from zombi_list or from mmap zone |
---|
| 450 | if( list_is_empty( &mgr->zombi_list[index] ) ) // from mmap zone |
---|
| 451 | { |
---|
| 452 | // check overflow |
---|
| 453 | free = mgr->first_free_vpn; |
---|
| 454 | if( (free + size) > mgr->vpn_size ) return ENOMEM; |
---|
| 455 | |
---|
| 456 | // update STACK allocator |
---|
| 457 | mgr->first_free_vpn += size; |
---|
| 458 | |
---|
| 459 | // compute base |
---|
| 460 | base = free; |
---|
| 461 | } |
---|
| 462 | else // from zombi_list |
---|
| 463 | { |
---|
| 464 | // get pointer on zombi vseg from zombi_list |
---|
| 465 | vseg = LIST_FIRST( &mgr->zombi_list[index] , vseg_t , list ); |
---|
| 466 | |
---|
| 467 | // remove vseg from free-list |
---|
| 468 | list_unlink( &vseg->list ); |
---|
| 469 | |
---|
| 470 | // compute base |
---|
| 471 | base = vseg->vpn_base; |
---|
[21] | 472 | } |
---|
| 473 | |
---|
[1] | 474 | // release lock on mmap allocator |
---|
| 475 | spinlock_unlock( &mgr->lock ); |
---|
| 476 | |
---|
| 477 | // returns vpn_base, vpn_size |
---|
| 478 | *vpn_base = base; |
---|
| 479 | *vpn_size = size; |
---|
| 480 | return 0; |
---|
| 481 | |
---|
[204] | 482 | } // end vmm_mmap_alloc() |
---|
| 483 | |
---|
[407] | 484 | //////////////////////////////////////////////// |
---|
| 485 | vseg_t * vmm_create_vseg( process_t * process, |
---|
| 486 | vseg_type_t type, |
---|
| 487 | intptr_t base, |
---|
| 488 | uint32_t size, |
---|
| 489 | uint32_t file_offset, |
---|
| 490 | uint32_t file_size, |
---|
| 491 | xptr_t mapper_xp, |
---|
| 492 | cxy_t cxy ) |
---|
[1] | 493 | { |
---|
| 494 | vseg_t * vseg; // created vseg pointer |
---|
[204] | 495 | vpn_t vpn_base; // first page index |
---|
[1] | 496 | vpn_t vpn_size; // number of pages |
---|
| 497 | error_t error; |
---|
| 498 | |
---|
[407] | 499 | vmm_dmsg("\n[DBG] %s : core[%x,%d] enters / process %x / base %x / size %x / %s / cxy = %x\n", |
---|
| 500 | __FUNCTION__ , local_cxy , CURRENT_THREAD->core->lid , |
---|
| 501 | process->pid , base , size , vseg_type_str(type) , cxy ); |
---|
[21] | 502 | |
---|
[407] | 503 | // get pointer on VMM |
---|
| 504 | vmm_t * vmm = &process->vmm; |
---|
[21] | 505 | |
---|
[204] | 506 | // compute base, size, vpn_base, vpn_size, depending on vseg type |
---|
[407] | 507 | // we use the VMM specific allocators for "stack", "file", "anon", & "remote" vsegs |
---|
[1] | 508 | if( type == VSEG_TYPE_STACK ) |
---|
| 509 | { |
---|
| 510 | // get vpn_base and vpn_size from STACK allocator |
---|
| 511 | error = vmm_stack_alloc( vmm , &vpn_base , &vpn_size ); |
---|
| 512 | if( error ) |
---|
| 513 | { |
---|
[407] | 514 | printk("\n[ERROR] in %s : no space for stack vseg / process %x in cluster %x\n", |
---|
| 515 | __FUNCTION__ , process->pid , local_cxy ); |
---|
[1] | 516 | return NULL; |
---|
| 517 | } |
---|
| 518 | |
---|
| 519 | // compute vseg base and size from vpn_base and vpn_size |
---|
| 520 | base = vpn_base << CONFIG_PPM_PAGE_SHIFT; |
---|
| 521 | size = vpn_size << CONFIG_PPM_PAGE_SHIFT; |
---|
| 522 | } |
---|
[21] | 523 | else if( (type == VSEG_TYPE_ANON) || |
---|
| 524 | (type == VSEG_TYPE_FILE) || |
---|
[1] | 525 | (type == VSEG_TYPE_REMOTE) ) |
---|
| 526 | { |
---|
| 527 | // get vpn_base and vpn_size from MMAP allocator |
---|
| 528 | vpn_t npages = size >> CONFIG_PPM_PAGE_SHIFT; |
---|
| 529 | error = vmm_mmap_alloc( vmm , npages , &vpn_base , &vpn_size ); |
---|
| 530 | if( error ) |
---|
| 531 | { |
---|
| 532 | printk("\n[ERROR] in %s : no vspace for mmap vseg / process %x in cluster %x\n", |
---|
| 533 | __FUNCTION__ , process->pid , local_cxy ); |
---|
| 534 | return NULL; |
---|
| 535 | } |
---|
| 536 | |
---|
| 537 | // compute vseg base and size from vpn_base and vpn_size |
---|
| 538 | base = vpn_base << CONFIG_PPM_PAGE_SHIFT; |
---|
| 539 | size = vpn_size << CONFIG_PPM_PAGE_SHIFT; |
---|
| 540 | } |
---|
| 541 | else |
---|
| 542 | { |
---|
[204] | 543 | uint32_t vpn_min = base >> CONFIG_PPM_PAGE_SHIFT; |
---|
| 544 | uint32_t vpn_max = (base + size - 1) >> CONFIG_PPM_PAGE_SHIFT; |
---|
| 545 | |
---|
| 546 | vpn_base = vpn_min; |
---|
| 547 | vpn_size = vpn_max - vpn_min + 1; |
---|
[1] | 548 | } |
---|
| 549 | |
---|
| 550 | // check collisions |
---|
| 551 | vseg = vmm_check_conflict( process , vpn_base , vpn_size ); |
---|
| 552 | if( vseg != NULL ) |
---|
| 553 | { |
---|
[21] | 554 | printk("\n[ERROR] in %s for process %x : new vseg [vpn_base = %x / vpn_size = %x]\n" |
---|
[1] | 555 | " overlap existing vseg [vpn_base = %x / vpn_size = %x]\n", |
---|
[407] | 556 | __FUNCTION__ , process->pid, vpn_base, vpn_size, vseg->vpn_base, vseg->vpn_size ); |
---|
[1] | 557 | return NULL; |
---|
| 558 | } |
---|
| 559 | |
---|
| 560 | // allocate physical memory for vseg descriptor |
---|
| 561 | vseg = vseg_alloc(); |
---|
| 562 | if( vseg == NULL ) |
---|
| 563 | { |
---|
| 564 | printk("\n[ERROR] in %s for process %x : cannot allocate memory for vseg\n", |
---|
[407] | 565 | __FUNCTION__ , process->pid ); |
---|
[1] | 566 | return NULL; |
---|
| 567 | } |
---|
| 568 | |
---|
| 569 | // initialize vseg descriptor |
---|
[407] | 570 | vseg_init( vseg, |
---|
| 571 | type, |
---|
| 572 | base, |
---|
| 573 | size, |
---|
| 574 | vpn_base, |
---|
| 575 | vpn_size, |
---|
| 576 | file_offset, |
---|
| 577 | file_size, |
---|
| 578 | mapper_xp, |
---|
| 579 | cxy ); |
---|
[1] | 580 | |
---|
| 581 | // attach vseg to vmm |
---|
| 582 | rwlock_wr_lock( &vmm->vsegs_lock ); |
---|
| 583 | vseg_attach( vmm , vseg ); |
---|
| 584 | rwlock_wr_unlock( &vmm->vsegs_lock ); |
---|
| 585 | |
---|
[407] | 586 | vmm_dmsg("\n[DBG] %s : core[%x,%d] exit / process %x / base %x / size %x / type %s\n", |
---|
| 587 | __FUNCTION__ , local_cxy , CURRENT_THREAD->core->lid , |
---|
| 588 | process->pid , base , size , vseg_type_str(type) ); |
---|
[21] | 589 | |
---|
[1] | 590 | return vseg; |
---|
| 591 | |
---|
[406] | 592 | } // vmm_create_vseg() |
---|
| 593 | |
---|
[1] | 594 | ///////////////////////////////////// |
---|
| 595 | void vmm_remove_vseg( vseg_t * vseg ) |
---|
| 596 | { |
---|
| 597 | // get pointers on calling process and VMM |
---|
| 598 | thread_t * this = CURRENT_THREAD; |
---|
| 599 | process_t * process = this->process; |
---|
| 600 | vmm_t * vmm = &this->process->vmm; |
---|
| 601 | uint32_t type = vseg->type; |
---|
| 602 | |
---|
| 603 | // detach vseg from VMM |
---|
| 604 | rwlock_wr_lock( &vmm->vsegs_lock ); |
---|
| 605 | vseg_detach( &process->vmm , vseg ); |
---|
| 606 | rwlock_wr_unlock( &vmm->vsegs_lock ); |
---|
| 607 | |
---|
| 608 | // release the stack slot to VMM stack allocator if STACK type |
---|
| 609 | if( type == VSEG_TYPE_STACK ) |
---|
| 610 | { |
---|
| 611 | // get pointer on stack allocator |
---|
| 612 | stack_mgr_t * mgr = &vmm->stack_mgr; |
---|
| 613 | |
---|
| 614 | // compute slot index |
---|
| 615 | uint32_t index = ((vseg->vpn_base - mgr->vpn_base - 1) / CONFIG_VMM_STACK_SIZE); |
---|
| 616 | |
---|
| 617 | // update stacks_bitmap |
---|
| 618 | spinlock_lock( &mgr->lock ); |
---|
| 619 | bitmap_clear( &mgr->bitmap , index ); |
---|
| 620 | spinlock_unlock( &mgr->lock ); |
---|
| 621 | } |
---|
| 622 | |
---|
| 623 | // release the vseg to VMM mmap allocator if MMAP type |
---|
| 624 | if( (type == VSEG_TYPE_ANON) || (type == VSEG_TYPE_FILE) || (type == VSEG_TYPE_REMOTE) ) |
---|
| 625 | { |
---|
| 626 | // get pointer on mmap allocator |
---|
| 627 | mmap_mgr_t * mgr = &vmm->mmap_mgr; |
---|
| 628 | |
---|
| 629 | // compute zombi_list index |
---|
| 630 | uint32_t index = bits_log2( vseg->vpn_size ); |
---|
| 631 | |
---|
| 632 | // update zombi_list |
---|
| 633 | spinlock_lock( &mgr->lock ); |
---|
| 634 | list_add_first( &mgr->zombi_list[index] , &vseg->list ); |
---|
| 635 | spinlock_unlock( &mgr->lock ); |
---|
| 636 | } |
---|
| 637 | |
---|
| 638 | // release physical memory allocated for vseg descriptor if no MMAP type |
---|
| 639 | if( (type != VSEG_TYPE_ANON) && (type != VSEG_TYPE_FILE) && (type != VSEG_TYPE_REMOTE) ) |
---|
| 640 | { |
---|
| 641 | vseg_free( vseg ); |
---|
| 642 | } |
---|
[407] | 643 | } // end vmm_remove_vseg() |
---|
[1] | 644 | |
---|
[68] | 645 | ////////////////////////////////////////////// |
---|
| 646 | error_t vmm_map_kernel_vseg( vseg_t * vseg, |
---|
| 647 | uint32_t attr ) |
---|
[1] | 648 | { |
---|
| 649 | vpn_t vpn; // VPN of PTE to be set |
---|
| 650 | vpn_t vpn_min; // VPN of first PTE to be set |
---|
| 651 | vpn_t vpn_max; // VPN of last PTE to be set (excluded) |
---|
| 652 | ppn_t ppn; // PPN of allocated physical page |
---|
| 653 | uint32_t order; // ln( number of small pages for one single PTE ) |
---|
| 654 | page_t * page; |
---|
| 655 | error_t error; |
---|
| 656 | |
---|
[68] | 657 | // check vseg type : must be a kernel vseg |
---|
[1] | 658 | uint32_t type = vseg->type; |
---|
[68] | 659 | assert( ((type==VSEG_TYPE_KCODE) || (type==VSEG_TYPE_KDATA) || (type==VSEG_TYPE_KDEV)), |
---|
| 660 | __FUNCTION__ , "not a kernel vseg\n" ); |
---|
[1] | 661 | |
---|
| 662 | // get pointer on page table |
---|
| 663 | gpt_t * gpt = &process_zero.vmm.gpt; |
---|
| 664 | |
---|
[21] | 665 | // define number of small pages per PTE |
---|
[1] | 666 | if( attr & GPT_SMALL ) order = 0; // 1 small page |
---|
| 667 | else order = 9; // 512 small pages |
---|
| 668 | |
---|
| 669 | // loop on pages in vseg |
---|
| 670 | vpn_min = vseg->vpn_base; |
---|
| 671 | vpn_max = vpn_min + vseg->vpn_size; |
---|
| 672 | for( vpn = vpn_min ; vpn < vpn_max ; vpn++ ) |
---|
| 673 | { |
---|
[68] | 674 | // allocate a physical page from local PPM |
---|
[1] | 675 | kmem_req_t req; |
---|
| 676 | req.type = KMEM_PAGE; |
---|
| 677 | req.size = order; |
---|
| 678 | req.flags = AF_KERNEL | AF_ZERO; |
---|
| 679 | page = (page_t *)kmem_alloc( &req ); |
---|
[21] | 680 | if( page == NULL ) |
---|
[1] | 681 | { |
---|
| 682 | printk("\n[ERROR] in %s : cannot allocate physical memory\n", __FUNCTION__ ); |
---|
| 683 | return ENOMEM; |
---|
| 684 | } |
---|
| 685 | |
---|
| 686 | // set page table entry |
---|
[315] | 687 | ppn = ppm_page2ppn( XPTR( local_cxy , page ) ); |
---|
[1] | 688 | error = hal_gpt_set_pte( gpt , vpn , ppn , attr ); |
---|
[21] | 689 | if( error ) |
---|
[1] | 690 | { |
---|
| 691 | printk("\n[ERROR] in %s : cannot register PPE\n", __FUNCTION__ ); |
---|
| 692 | return ENOMEM; |
---|
| 693 | } |
---|
| 694 | } |
---|
[21] | 695 | |
---|
[1] | 696 | return 0; |
---|
[178] | 697 | } |
---|
[1] | 698 | |
---|
| 699 | ///////////////////////////////////////// |
---|
| 700 | void vmm_unmap_vseg( process_t * process, |
---|
| 701 | vseg_t * vseg ) |
---|
| 702 | { |
---|
[21] | 703 | vpn_t vpn; // VPN of current PTE |
---|
| 704 | vpn_t vpn_min; // VPN of first PTE |
---|
[1] | 705 | vpn_t vpn_max; // VPN of last PTE (excluded) |
---|
| 706 | |
---|
| 707 | // get pointer on process page table |
---|
| 708 | gpt_t * gpt = &process->vmm.gpt; |
---|
| 709 | |
---|
| 710 | // loop on pages in vseg |
---|
| 711 | vpn_min = vseg->vpn_base; |
---|
| 712 | vpn_max = vpn_min + vseg->vpn_size; |
---|
| 713 | for( vpn = vpn_min ; vpn < vpn_max ; vpn++ ) |
---|
| 714 | { |
---|
| 715 | hal_gpt_reset_pte( gpt , vpn ); |
---|
| 716 | } |
---|
[178] | 717 | } |
---|
[1] | 718 | |
---|
[407] | 719 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
[406] | 720 | // This low-level static function is called by the vmm_get_vseg() and vmm_resize_vseg() |
---|
| 721 | // functions. It scan the list of registered vsegs to find the unique vseg containing |
---|
| 722 | // a given virtual address. |
---|
[407] | 723 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
[406] | 724 | // @ vmm : pointer on the process VMM. |
---|
| 725 | // @ vaddr : virtual address. |
---|
| 726 | // @ return vseg pointer if success / return NULL if not found. |
---|
[407] | 727 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
[406] | 728 | static vseg_t * vseg_from_vaddr( vmm_t * vmm, |
---|
| 729 | intptr_t vaddr ) |
---|
| 730 | { |
---|
| 731 | list_entry_t * iter; |
---|
| 732 | vseg_t * vseg = NULL; |
---|
| 733 | |
---|
| 734 | // get lock protecting the vseg list |
---|
| 735 | rwlock_rd_lock( &vmm->vsegs_lock ); |
---|
| 736 | |
---|
| 737 | // scan the list of vsegs |
---|
| 738 | LIST_FOREACH( &vmm->vsegs_root , iter ) |
---|
| 739 | { |
---|
| 740 | vseg = LIST_ELEMENT( iter , vseg_t , list ); |
---|
| 741 | if( (vaddr >= vseg->min) && (vaddr < vseg->max) ) break; |
---|
| 742 | } |
---|
| 743 | |
---|
| 744 | // release the lock |
---|
| 745 | rwlock_rd_unlock( &vmm->vsegs_lock ); |
---|
| 746 | |
---|
| 747 | return vseg; |
---|
| 748 | } |
---|
| 749 | |
---|
[1] | 750 | ///////////////////////////////////////////// |
---|
| 751 | error_t vmm_resize_vseg( process_t * process, |
---|
| 752 | intptr_t base, |
---|
| 753 | intptr_t size ) |
---|
| 754 | { |
---|
[406] | 755 | error_t error; |
---|
| 756 | vseg_t * new; |
---|
| 757 | vpn_t vpn_min; |
---|
| 758 | vpn_t vpn_max; |
---|
[1] | 759 | |
---|
| 760 | // get pointer on process VMM |
---|
| 761 | vmm_t * vmm = &process->vmm; |
---|
| 762 | |
---|
| 763 | intptr_t addr_min = base; |
---|
| 764 | intptr_t addr_max = base + size; |
---|
| 765 | |
---|
| 766 | // get pointer on vseg |
---|
[406] | 767 | vseg_t * vseg = vseg_from_vaddr( vmm , base ); |
---|
[1] | 768 | |
---|
| 769 | if( vseg == NULL) return EINVAL; |
---|
[21] | 770 | |
---|
[1] | 771 | // get VMM lock protecting vsegs list |
---|
| 772 | rwlock_wr_lock( &vmm->vsegs_lock ); |
---|
[21] | 773 | |
---|
[1] | 774 | if( (vseg->min > addr_min) || (vseg->max < addr_max) ) // region not included in vseg |
---|
| 775 | { |
---|
| 776 | error = EINVAL; |
---|
| 777 | } |
---|
| 778 | else if( (vseg->min == addr_min) && (vseg->max == addr_max) ) // vseg must be removed |
---|
| 779 | { |
---|
| 780 | vmm_remove_vseg( vseg ); |
---|
| 781 | error = 0; |
---|
| 782 | } |
---|
[406] | 783 | else if( vseg->min == addr_min ) // vseg must be resized |
---|
[1] | 784 | { |
---|
[406] | 785 | // update vseg base address |
---|
| 786 | vseg->min = addr_max; |
---|
| 787 | |
---|
| 788 | // update vpn_base and vpn_size |
---|
| 789 | vpn_min = vseg->min >> CONFIG_PPM_PAGE_SHIFT; |
---|
| 790 | vpn_max = (vseg->max - 1) >> CONFIG_PPM_PAGE_SHIFT; |
---|
| 791 | vseg->vpn_base = vpn_min; |
---|
| 792 | vseg->vpn_size = vpn_max - vpn_min + 1; |
---|
| 793 | error = 0; |
---|
[1] | 794 | } |
---|
[406] | 795 | else if( vseg->max == addr_max ) // vseg must be resized |
---|
[1] | 796 | { |
---|
[406] | 797 | // update vseg max address |
---|
| 798 | vseg->max = addr_min; |
---|
| 799 | |
---|
| 800 | // update vpn_base and vpn_size |
---|
| 801 | vpn_min = vseg->min >> CONFIG_PPM_PAGE_SHIFT; |
---|
| 802 | vpn_max = (vseg->max - 1) >> CONFIG_PPM_PAGE_SHIFT; |
---|
| 803 | vseg->vpn_base = vpn_min; |
---|
| 804 | vseg->vpn_size = vpn_max - vpn_min + 1; |
---|
| 805 | error = 0; |
---|
[1] | 806 | } |
---|
[406] | 807 | else // vseg cut in three regions |
---|
[1] | 808 | { |
---|
[406] | 809 | // resize existing vseg |
---|
| 810 | vseg->max = addr_min; |
---|
| 811 | |
---|
| 812 | // update vpn_base and vpn_size |
---|
| 813 | vpn_min = vseg->min >> CONFIG_PPM_PAGE_SHIFT; |
---|
| 814 | vpn_max = (vseg->max - 1) >> CONFIG_PPM_PAGE_SHIFT; |
---|
| 815 | vseg->vpn_base = vpn_min; |
---|
| 816 | vseg->vpn_size = vpn_max - vpn_min + 1; |
---|
| 817 | |
---|
| 818 | // create new vseg |
---|
[407] | 819 | new = vmm_create_vseg( process, |
---|
| 820 | vseg->type, |
---|
| 821 | addr_min, |
---|
| 822 | (vseg->max - addr_max), |
---|
| 823 | vseg->file_offset, |
---|
| 824 | vseg->file_size, |
---|
| 825 | vseg->mapper_xp, |
---|
| 826 | vseg->cxy ); |
---|
| 827 | |
---|
[406] | 828 | if( new == NULL ) error = EINVAL; |
---|
| 829 | else error = 0; |
---|
[1] | 830 | } |
---|
| 831 | |
---|
| 832 | // release VMM lock |
---|
| 833 | rwlock_wr_unlock( &vmm->vsegs_lock ); |
---|
| 834 | |
---|
| 835 | return error; |
---|
| 836 | |
---|
[406] | 837 | } // vmm_resize_vseg() |
---|
| 838 | |
---|
[1] | 839 | /////////////////////////////////////////// |
---|
[388] | 840 | error_t vmm_get_vseg( process_t * process, |
---|
[394] | 841 | intptr_t vaddr, |
---|
[388] | 842 | vseg_t ** found_vseg ) |
---|
[1] | 843 | { |
---|
[406] | 844 | vmm_t * vmm = &process->vmm; |
---|
[1] | 845 | |
---|
[406] | 846 | // get vseg from vaddr |
---|
| 847 | vseg_t * vseg = vseg_from_vaddr( vmm , vaddr ); |
---|
[1] | 848 | |
---|
[388] | 849 | if( vseg == NULL ) // vseg not found in local cluster => try to get it from ref |
---|
| 850 | { |
---|
| 851 | // get extended pointer on reference process |
---|
| 852 | xptr_t ref_xp = process->ref_xp; |
---|
[1] | 853 | |
---|
[388] | 854 | // get cluster and local pointer on reference process |
---|
| 855 | cxy_t ref_cxy = GET_CXY( ref_xp ); |
---|
| 856 | process_t * ref_ptr = (process_t *)GET_PTR( ref_xp ); |
---|
| 857 | |
---|
| 858 | if( local_cxy == ref_cxy ) return -1; // local cluster is the reference |
---|
| 859 | |
---|
| 860 | // get extended pointer on reference vseg |
---|
| 861 | xptr_t vseg_xp; |
---|
| 862 | error_t error; |
---|
[406] | 863 | |
---|
[394] | 864 | rpc_vmm_get_vseg_client( ref_cxy , ref_ptr , vaddr , &vseg_xp , &error ); |
---|
[388] | 865 | |
---|
| 866 | if( error ) return -1; // vseg not found => illegal user vaddr |
---|
| 867 | |
---|
| 868 | // allocate a vseg in local cluster |
---|
| 869 | vseg = vseg_alloc(); |
---|
| 870 | |
---|
[406] | 871 | if( vseg == NULL ) return -1; |
---|
[388] | 872 | |
---|
| 873 | // initialise local vseg from reference |
---|
| 874 | vseg_init_from_ref( vseg , vseg_xp ); |
---|
| 875 | |
---|
| 876 | // register local vseg in local VMM |
---|
[406] | 877 | vseg_attach( &process->vmm , vseg ); |
---|
[388] | 878 | } |
---|
| 879 | |
---|
| 880 | // success |
---|
| 881 | *found_vseg = vseg; |
---|
[394] | 882 | return 0; |
---|
[388] | 883 | |
---|
| 884 | } // end vmm_get_vseg() |
---|
| 885 | |
---|
[407] | 886 | ////////////////////////////////////////////////////////////////////////////////////// |
---|
| 887 | // This static function compute the target cluster to allocate a physical page |
---|
| 888 | // for a given <vpn> in a given <vseg>, allocates the page (with an RPC if required) |
---|
| 889 | // and returns an extended pointer on the allocated page descriptor. |
---|
| 890 | // The vseg cannot have the FILE type. |
---|
| 891 | ////////////////////////////////////////////////////////////////////////////////////// |
---|
| 892 | static xptr_t vmm_page_allocate( vseg_t * vseg, |
---|
| 893 | vpn_t vpn ) |
---|
| 894 | { |
---|
| 895 | // compute target cluster |
---|
| 896 | page_t * page_ptr; |
---|
| 897 | cxy_t page_cxy; |
---|
| 898 | kmem_req_t req; |
---|
| 899 | |
---|
| 900 | uint32_t type = vseg->type; |
---|
| 901 | uint32_t flags = vseg->flags; |
---|
| 902 | |
---|
| 903 | assert( ( type != VSEG_TYPE_FILE ) , __FUNCTION__ , "illegal vseg type\n" ); |
---|
| 904 | |
---|
| 905 | if( flags & VSEG_DISTRIB ) // distributed => cxy depends on vpn LSB |
---|
| 906 | { |
---|
| 907 | uint32_t x_size = LOCAL_CLUSTER->x_size; |
---|
| 908 | uint32_t y_size = LOCAL_CLUSTER->y_size; |
---|
| 909 | uint32_t y_width = LOCAL_CLUSTER->y_width; |
---|
| 910 | uint32_t index = vpn & ((x_size * y_size) - 1); |
---|
| 911 | uint32_t x = index / y_size; |
---|
| 912 | uint32_t y = index % y_size; |
---|
| 913 | page_cxy = (x<<y_width) + y; |
---|
| 914 | } |
---|
| 915 | else // other cases => cxy specified in vseg |
---|
| 916 | { |
---|
| 917 | page_cxy = vseg->cxy; |
---|
| 918 | } |
---|
| 919 | |
---|
| 920 | // allocate a physical page from target cluster |
---|
| 921 | if( page_cxy == local_cxy ) // target cluster is the local cluster |
---|
| 922 | { |
---|
| 923 | req.type = KMEM_PAGE; |
---|
| 924 | req.size = 0; |
---|
| 925 | req.flags = AF_NONE; |
---|
| 926 | page_ptr = (page_t *)kmem_alloc( &req ); |
---|
| 927 | } |
---|
| 928 | else // target cluster is not the local cluster |
---|
| 929 | { |
---|
| 930 | rpc_pmem_get_pages_client( page_cxy , 0 , &page_ptr ); |
---|
| 931 | } |
---|
| 932 | |
---|
| 933 | if( page_ptr == NULL ) return XPTR_NULL; |
---|
| 934 | else return XPTR( page_cxy , page_ptr ); |
---|
| 935 | |
---|
| 936 | } // end vmm_page_allocate() |
---|
| 937 | |
---|
[313] | 938 | //////////////////////////////////////// |
---|
| 939 | error_t vmm_get_one_ppn( vseg_t * vseg, |
---|
| 940 | vpn_t vpn, |
---|
| 941 | ppn_t * ppn ) |
---|
| 942 | { |
---|
| 943 | error_t error; |
---|
[407] | 944 | xptr_t page_xp; // extended pointer on physical page descriptor |
---|
[313] | 945 | page_t * page_ptr; // local pointer on physical page descriptor |
---|
[406] | 946 | uint32_t index; // missing page index in vseg mapper |
---|
| 947 | uint32_t type; // vseg type; |
---|
[313] | 948 | |
---|
[406] | 949 | type = vseg->type; |
---|
| 950 | index = vpn - vseg->vpn_base; |
---|
[313] | 951 | |
---|
[407] | 952 | vmm_dmsg("\n[DBG] %s : core[%x,%d] enter for vpn = %x / type = %s / index = %d\n", |
---|
| 953 | __FUNCTION__, local_cxy, CURRENT_THREAD->core->lid, vpn, vseg_type_str(type), index ); |
---|
[313] | 954 | |
---|
[406] | 955 | // FILE type : get the physical page from the file mapper |
---|
[313] | 956 | if( type == VSEG_TYPE_FILE ) |
---|
| 957 | { |
---|
[406] | 958 | // get extended pointer on mapper |
---|
[407] | 959 | xptr_t mapper_xp = vseg->mapper_xp; |
---|
[313] | 960 | |
---|
[406] | 961 | assert( (mapper_xp != XPTR_NULL), __FUNCTION__, |
---|
| 962 | "mapper not defined for a FILE vseg\n" ); |
---|
| 963 | |
---|
| 964 | // get mapper cluster and local pointer |
---|
| 965 | cxy_t mapper_cxy = GET_CXY( mapper_xp ); |
---|
| 966 | mapper_t * mapper_ptr = (mapper_t *)GET_PTR( mapper_xp ); |
---|
| 967 | |
---|
[313] | 968 | // get page descriptor from mapper |
---|
| 969 | if( mapper_cxy == local_cxy ) // mapper is local |
---|
| 970 | { |
---|
| 971 | page_ptr = mapper_get_page( mapper_ptr , index ); |
---|
| 972 | } |
---|
| 973 | else // mapper is remote |
---|
| 974 | { |
---|
| 975 | rpc_mapper_get_page_client( mapper_cxy , mapper_ptr , index , &page_ptr ); |
---|
| 976 | } |
---|
| 977 | |
---|
| 978 | if ( page_ptr == NULL ) return EINVAL; |
---|
| 979 | |
---|
[407] | 980 | page_xp = XPTR( mapper_cxy , page_ptr ); |
---|
[313] | 981 | } |
---|
| 982 | |
---|
[406] | 983 | // Other types : allocate a physical page from target cluster, |
---|
[407] | 984 | // as defined by vseg type and vpn value |
---|
[313] | 985 | else |
---|
| 986 | { |
---|
[407] | 987 | // allocate physical page |
---|
| 988 | page_xp = vmm_page_allocate( vseg , vpn ); |
---|
[406] | 989 | |
---|
[407] | 990 | if( page_xp == XPTR_NULL ) return ENOMEM; |
---|
[313] | 991 | |
---|
[406] | 992 | // initialise missing page from .elf file mapper for DATA and CODE types |
---|
| 993 | // => the mapper_xp field is an extended pointer on the .elf file mapper |
---|
[313] | 994 | if( (type == VSEG_TYPE_CODE) || (type == VSEG_TYPE_DATA) ) |
---|
| 995 | { |
---|
[406] | 996 | // get extended pointer on mapper |
---|
| 997 | xptr_t mapper_xp = vseg->mapper_xp; |
---|
[313] | 998 | |
---|
[406] | 999 | assert( (mapper_xp != XPTR_NULL), __FUNCTION__, |
---|
| 1000 | "mapper not defined for a CODE or DATA vseg\n" ); |
---|
| 1001 | |
---|
| 1002 | // get mapper cluster and local pointer |
---|
| 1003 | cxy_t mapper_cxy = GET_CXY( mapper_xp ); |
---|
| 1004 | mapper_t * mapper_ptr = (mapper_t *)GET_PTR( mapper_xp ); |
---|
| 1005 | |
---|
| 1006 | // compute missing page offset in vseg |
---|
| 1007 | uint32_t offset = index << CONFIG_PPM_PAGE_SHIFT; |
---|
| 1008 | |
---|
[313] | 1009 | // compute missing page offset in .elf file |
---|
[406] | 1010 | uint32_t elf_offset = vseg->file_offset + offset; |
---|
[313] | 1011 | |
---|
[407] | 1012 | vmm_dmsg("\n[DBG] %s : core[%x,%d] for vpn = %x / elf_offset = %x\n", |
---|
| 1013 | __FUNCTION__, local_cxy, CURRENT_THREAD->core->lid, vpn, elf_offset ); |
---|
[313] | 1014 | |
---|
[406] | 1015 | // compute extended pointer on page base |
---|
[407] | 1016 | xptr_t base_xp = ppm_page2base( page_xp ); |
---|
[313] | 1017 | |
---|
[406] | 1018 | // file_size (in .elf mapper) can be smaller than vseg_size (BSS) |
---|
| 1019 | uint32_t file_size = vseg->file_size; |
---|
| 1020 | |
---|
| 1021 | if( file_size < offset ) // missing page fully in BSS |
---|
[313] | 1022 | { |
---|
[407] | 1023 | vmm_dmsg("\n[DBG] %s : core[%x,%d] for vpn = %x / fully in BSS\n", |
---|
| 1024 | __FUNCTION__, local_cxy, CURRENT_THREAD->core->lid, vpn ); |
---|
[406] | 1025 | |
---|
[407] | 1026 | if( GET_CXY( page_xp ) == local_cxy ) |
---|
[313] | 1027 | { |
---|
[315] | 1028 | memset( GET_PTR( base_xp ) , 0 , CONFIG_PPM_PAGE_SIZE ); |
---|
[313] | 1029 | } |
---|
| 1030 | else |
---|
| 1031 | { |
---|
[315] | 1032 | hal_remote_memset( base_xp , 0 , CONFIG_PPM_PAGE_SIZE ); |
---|
[313] | 1033 | } |
---|
| 1034 | } |
---|
[406] | 1035 | else if( file_size >= (offset + CONFIG_PPM_PAGE_SIZE) ) // fully in mapper |
---|
[315] | 1036 | { |
---|
[406] | 1037 | |
---|
[407] | 1038 | vmm_dmsg("\n[DBG] %s : core[%x,%d] for vpn = %x / fully in mapper\n", |
---|
| 1039 | __FUNCTION__, local_cxy, CURRENT_THREAD->core->lid, vpn ); |
---|
| 1040 | |
---|
[313] | 1041 | if( mapper_cxy == local_cxy ) |
---|
| 1042 | { |
---|
| 1043 | error = mapper_move_kernel( mapper_ptr, |
---|
| 1044 | true, // to_buffer |
---|
[406] | 1045 | elf_offset, |
---|
[313] | 1046 | base_xp, |
---|
[315] | 1047 | CONFIG_PPM_PAGE_SIZE ); |
---|
[313] | 1048 | } |
---|
| 1049 | else |
---|
| 1050 | { |
---|
| 1051 | rpc_mapper_move_buffer_client( mapper_cxy, |
---|
| 1052 | mapper_ptr, |
---|
| 1053 | true, // to buffer |
---|
| 1054 | false, // kernel buffer |
---|
[406] | 1055 | elf_offset, |
---|
| 1056 | base_xp, |
---|
[315] | 1057 | CONFIG_PPM_PAGE_SIZE, |
---|
[313] | 1058 | &error ); |
---|
| 1059 | } |
---|
| 1060 | if( error ) return EINVAL; |
---|
| 1061 | } |
---|
[406] | 1062 | else // both in mapper and in BSS : |
---|
| 1063 | // - (file_size - offset) bytes from mapper |
---|
| 1064 | // - (page_size + offset - file_size) bytes from BSS |
---|
[313] | 1065 | { |
---|
[406] | 1066 | |
---|
[407] | 1067 | vmm_dmsg("\n[DBG] %s : core[%x,%d] for vpn = %x / both mapper & BSS\n" |
---|
| 1068 | " %d bytes from mapper / %d bytes from BSS\n", |
---|
| 1069 | __FUNCTION__, local_cxy, CURRENT_THREAD->core->lid, vpn, |
---|
| 1070 | file_size - offset , offset + CONFIG_PPM_PAGE_SIZE - file_size ); |
---|
| 1071 | |
---|
[313] | 1072 | // initialize mapper part |
---|
[315] | 1073 | if( mapper_cxy == local_cxy ) |
---|
[313] | 1074 | { |
---|
| 1075 | error = mapper_move_kernel( mapper_ptr, |
---|
[315] | 1076 | true, // to buffer |
---|
[406] | 1077 | elf_offset, |
---|
[313] | 1078 | base_xp, |
---|
[406] | 1079 | file_size - offset ); |
---|
[313] | 1080 | } |
---|
[315] | 1081 | else |
---|
[313] | 1082 | { |
---|
| 1083 | rpc_mapper_move_buffer_client( mapper_cxy, |
---|
| 1084 | mapper_ptr, |
---|
[315] | 1085 | true, // to buffer |
---|
[313] | 1086 | false, // kernel buffer |
---|
[406] | 1087 | elf_offset, |
---|
| 1088 | base_xp, |
---|
| 1089 | file_size - offset, |
---|
[313] | 1090 | &error ); |
---|
| 1091 | } |
---|
| 1092 | if( error ) return EINVAL; |
---|
| 1093 | |
---|
| 1094 | // initialize BSS part |
---|
[407] | 1095 | if( GET_CXY( page_xp ) == local_cxy ) |
---|
[313] | 1096 | { |
---|
[406] | 1097 | memset( GET_PTR( base_xp ) + file_size - offset , 0 , |
---|
| 1098 | offset + CONFIG_PPM_PAGE_SIZE - file_size ); |
---|
[313] | 1099 | } |
---|
| 1100 | else |
---|
| 1101 | { |
---|
[406] | 1102 | hal_remote_memset( base_xp + file_size - offset , 0 , |
---|
| 1103 | offset + CONFIG_PPM_PAGE_SIZE - file_size ); |
---|
[313] | 1104 | } |
---|
| 1105 | } |
---|
| 1106 | } // end initialisation for CODE or DATA types |
---|
| 1107 | } |
---|
| 1108 | |
---|
| 1109 | // return ppn |
---|
[407] | 1110 | *ppn = ppm_page2ppn( page_xp ); |
---|
[406] | 1111 | |
---|
[407] | 1112 | vmm_dmsg("\n[DBG] %s : core[%x,%d] exit for vpn = %x / ppn = %x\n", |
---|
| 1113 | __FUNCTION__ , local_cxy , CURRENT_THREAD->core->lid , vpn , *ppn ); |
---|
[406] | 1114 | |
---|
[313] | 1115 | return 0; |
---|
| 1116 | |
---|
| 1117 | } // end vmm_get_one_ppn() |
---|
| 1118 | |
---|
[1] | 1119 | ///////////////////////////////////////// |
---|
| 1120 | error_t vmm_get_pte( process_t * process, |
---|
| 1121 | vpn_t vpn, |
---|
[407] | 1122 | bool_t cow, |
---|
| 1123 | uint32_t * attr, |
---|
| 1124 | ppn_t * ppn ) |
---|
[1] | 1125 | { |
---|
[407] | 1126 | vseg_t * vseg; // pointer on vseg containing VPN |
---|
| 1127 | ppn_t old_ppn; // current PTE_PPN |
---|
| 1128 | uint32_t old_attr; // current PTE_ATTR |
---|
| 1129 | ppn_t new_ppn; // new PTE_PPN |
---|
| 1130 | uint32_t new_attr; // new PTE_ATTR |
---|
| 1131 | xptr_t page_xp; // extended pointer on allocated page descriptor |
---|
[1] | 1132 | error_t error; |
---|
| 1133 | |
---|
[178] | 1134 | // this function must be called by a thread running in the reference cluster |
---|
[68] | 1135 | assert( (GET_CXY( process->ref_xp ) == local_cxy ) , __FUNCTION__ , |
---|
[406] | 1136 | "not called in the reference cluster\n" ); |
---|
[1] | 1137 | |
---|
[407] | 1138 | vmm_dmsg("\n[DBG] %s : core[%x,%d] enter for vpn = %x in process %x / cow = %d\n", |
---|
| 1139 | __FUNCTION__ , local_cxy , CURRENT_THREAD->core->lid , vpn , process->pid , %d); |
---|
[406] | 1140 | |
---|
[1] | 1141 | // get VMM pointer |
---|
| 1142 | vmm_t * vmm = &process->vmm; |
---|
| 1143 | |
---|
[407] | 1144 | // get vseg pointer from ref VSL |
---|
| 1145 | error = vmm_get_vseg( process , vpn<<CONFIG_PPM_PAGE_SHIFT , &vseg ); |
---|
[1] | 1146 | |
---|
[407] | 1147 | if( error ) |
---|
[1] | 1148 | { |
---|
[407] | 1149 | printk("\n[ERROR] in %s : out of segment / process = %x / vpn = %x\n", |
---|
| 1150 | __FUNCTION__ , process->pid , vpn ); |
---|
| 1151 | return error; |
---|
| 1152 | } |
---|
[406] | 1153 | |
---|
[407] | 1154 | vmm_dmsg("\n[DBG] %s : core[%x,%d] found vseg %s / vpn_base = %x / vpn_size = %x\n", |
---|
| 1155 | __FUNCTION__ , local_cxy , CURRENT_THREAD->core->lid , |
---|
| 1156 | vseg_type_str(vseg->type) , vseg->vpn_base , vseg->vpn_size ); |
---|
[1] | 1157 | |
---|
[407] | 1158 | // access GPT to get current PTE attributes and PPN |
---|
| 1159 | hal_gpt_get_pte( &vmm->gpt , vpn , &old_attr , &old_ppn ); |
---|
| 1160 | |
---|
| 1161 | // for both copy_on_write and page_fault events, we allocate a physical page, |
---|
| 1162 | // initialize it, register it in the GPT, and return the new_ppn and new_attr |
---|
| 1163 | |
---|
| 1164 | if( cow ) ////////////// copy_on_write request /////////// |
---|
| 1165 | { |
---|
| 1166 | assert( (*attr & GPT_MAPPED) , __FUNCTION__ , |
---|
| 1167 | "PTE must be mapped for a copy-on-write\n" ); |
---|
| 1168 | |
---|
| 1169 | vmm_dmsg("\n[DBG] %s : core[%x,%d] page %x must be copied => do it\n", |
---|
| 1170 | __FUNCTION__ , local_cxy , CURRENT_THREAD->core->lid , vpn ); |
---|
| 1171 | |
---|
| 1172 | // allocate a physical page depending on vseg type |
---|
| 1173 | page_xp = vmm_page_allocate( vseg , vpn ); |
---|
| 1174 | |
---|
| 1175 | if( page_xp == XPTR_NULL ) |
---|
[1] | 1176 | { |
---|
[407] | 1177 | printk("\n[ERROR] in %s : no memory / process = %x / vpn = %x\n", |
---|
| 1178 | __FUNCTION__ , process->pid , vpn ); |
---|
| 1179 | return ENOMEM; |
---|
[1] | 1180 | } |
---|
| 1181 | |
---|
[407] | 1182 | // compute allocated page PPN |
---|
| 1183 | new_ppn = ppm_page2ppn( page_xp ); |
---|
[406] | 1184 | |
---|
[407] | 1185 | // copy old page content to new page |
---|
| 1186 | xptr_t old_base_xp = ppm_ppn2base( old_ppn ); |
---|
| 1187 | xptr_t new_base_xp = ppm_ppn2base( new_ppn ); |
---|
| 1188 | memcpy( GET_PTR( new_base_xp ), |
---|
| 1189 | GET_PTR( old_base_xp ), |
---|
| 1190 | CONFIG_PPM_PAGE_SIZE ); |
---|
[1] | 1191 | |
---|
[407] | 1192 | // update attributes: reset COW and set WRITABLE |
---|
| 1193 | new_attr = old_attr & ~GPT_COW; |
---|
| 1194 | new_attr = new_attr | GPT_WRITABLE; |
---|
| 1195 | |
---|
| 1196 | // register PTE in GPT |
---|
| 1197 | error = hal_gpt_set_pte( &vmm->gpt , vpn , new_ppn , new_attr ); |
---|
| 1198 | |
---|
[1] | 1199 | if( error ) |
---|
| 1200 | { |
---|
[407] | 1201 | printk("\n[ERROR] in %s : cannot update GPT / process = %x / vpn = %x\n", |
---|
| 1202 | __FUNCTION__ , process->pid , vpn ); |
---|
[313] | 1203 | return error; |
---|
[1] | 1204 | } |
---|
[407] | 1205 | } |
---|
| 1206 | else //////////////////// page_fault request /////////// |
---|
| 1207 | { |
---|
| 1208 | if( (old_attr & GPT_MAPPED) == 0 ) // PTE unmapped in ref GPT |
---|
| 1209 | { |
---|
[1] | 1210 | |
---|
[407] | 1211 | vmm_dmsg("\n[DBG] %s : core[%x,%d] page %x unmapped => try to map it\n", |
---|
| 1212 | __FUNCTION__ , local_cxy , CURRENT_THREAD->core->lid , vpn ); |
---|
[1] | 1213 | |
---|
[407] | 1214 | // allocate one physical page, depending on vseg type |
---|
| 1215 | error = vmm_get_one_ppn( vseg , vpn , &new_ppn ); |
---|
[313] | 1216 | |
---|
[407] | 1217 | if( error ) |
---|
| 1218 | { |
---|
| 1219 | printk("\n[ERROR] in %s : no memory / process = %x / vpn = %x\n", |
---|
| 1220 | __FUNCTION__ , process->pid , vpn ); |
---|
| 1221 | return error; |
---|
| 1222 | } |
---|
| 1223 | |
---|
| 1224 | // define attributes from vseg flags |
---|
| 1225 | new_attr = GPT_MAPPED | GPT_SMALL; |
---|
| 1226 | if( vseg->flags & VSEG_USER ) new_attr |= GPT_USER; |
---|
| 1227 | if( vseg->flags & VSEG_WRITE ) new_attr |= GPT_WRITABLE; |
---|
| 1228 | if( vseg->flags & VSEG_EXEC ) new_attr |= GPT_EXECUTABLE; |
---|
| 1229 | if( vseg->flags & VSEG_CACHE ) new_attr |= GPT_CACHABLE; |
---|
| 1230 | |
---|
| 1231 | // register PTE in GPT |
---|
| 1232 | error = hal_gpt_set_pte( &vmm->gpt , vpn , new_ppn , new_attr ); |
---|
| 1233 | |
---|
| 1234 | if( error ) |
---|
| 1235 | { |
---|
| 1236 | printk("\n[ERROR] in %s : cannot update GPT / process = %x / vpn = %x\n", |
---|
| 1237 | __FUNCTION__ , process->pid , vpn ); |
---|
| 1238 | return error; |
---|
| 1239 | } |
---|
| 1240 | } |
---|
| 1241 | else |
---|
[1] | 1242 | { |
---|
[407] | 1243 | new_attr = old_attr; |
---|
| 1244 | new_ppn = old_ppn; |
---|
[1] | 1245 | } |
---|
[407] | 1246 | } |
---|
[1] | 1247 | |
---|
[407] | 1248 | vmm_dmsg("\n[DBG] %s : core[%x,%d] exit for vpn = %x / ppn = %x / attr = %x\n", |
---|
| 1249 | __FUNCTION__ , local_cxy , CURRENT_THREAD->core->lid , vpn , new_ppn , new_attr ); |
---|
[406] | 1250 | |
---|
[407] | 1251 | *ppn = new_ppn; |
---|
| 1252 | *attr = new_attr; |
---|
[1] | 1253 | return 0; |
---|
| 1254 | |
---|
[313] | 1255 | } // end vmm_get_pte() |
---|
| 1256 | |
---|
[1] | 1257 | /////////////////////////////////////////////////// |
---|
| 1258 | error_t vmm_handle_page_fault( process_t * process, |
---|
| 1259 | vpn_t vpn ) |
---|
| 1260 | { |
---|
| 1261 | uint32_t attr; // missing page attributes |
---|
[21] | 1262 | ppn_t ppn; // missing page PPN |
---|
[407] | 1263 | error_t error; |
---|
[1] | 1264 | |
---|
| 1265 | // get reference process cluster and local pointer |
---|
| 1266 | cxy_t ref_cxy = GET_CXY( process->ref_xp ); |
---|
| 1267 | process_t * ref_ptr = (process_t *)GET_PTR( process->ref_xp ); |
---|
| 1268 | |
---|
[313] | 1269 | // get missing PTE attributes and PPN from reference cluster |
---|
[407] | 1270 | if( local_cxy != ref_cxy ) |
---|
[1] | 1271 | { |
---|
[407] | 1272 | rpc_vmm_get_pte_client( ref_cxy, |
---|
| 1273 | ref_ptr, |
---|
| 1274 | vpn, |
---|
| 1275 | false, // page_fault |
---|
| 1276 | &attr, |
---|
| 1277 | &ppn, |
---|
| 1278 | &error ); |
---|
| 1279 | |
---|
| 1280 | // get local VMM pointer |
---|
| 1281 | vmm_t * vmm = &process->vmm; |
---|
| 1282 | |
---|
| 1283 | // update local GPT |
---|
| 1284 | error |= hal_gpt_set_pte( &vmm->gpt , vpn , ppn , attr ); |
---|
[1] | 1285 | } |
---|
[407] | 1286 | else // local cluster is the reference cluster |
---|
[1] | 1287 | { |
---|
[407] | 1288 | error = vmm_get_pte( process, |
---|
| 1289 | vpn, |
---|
| 1290 | false, // page-fault |
---|
| 1291 | &attr, |
---|
| 1292 | &ppn ); |
---|
[1] | 1293 | } |
---|
| 1294 | |
---|
[388] | 1295 | return error; |
---|
[1] | 1296 | |
---|
[313] | 1297 | } // end vmm_handle_page_fault() |
---|
| 1298 | |
---|
[407] | 1299 | /////////////////////////////////////////////// |
---|
| 1300 | error_t vmm_copy_on_write( process_t * process, |
---|
| 1301 | vpn_t vpn ) |
---|
| 1302 | { |
---|
| 1303 | uint32_t attr; // missing page attributes |
---|
| 1304 | ppn_t ppn; // missing page PPN |
---|
| 1305 | error_t error; |
---|
[313] | 1306 | |
---|
[407] | 1307 | // get reference process cluster and local pointer |
---|
| 1308 | cxy_t ref_cxy = GET_CXY( process->ref_xp ); |
---|
| 1309 | process_t * ref_ptr = (process_t *)GET_PTR( process->ref_xp ); |
---|
| 1310 | |
---|
| 1311 | // get new PTE attributes and PPN from reference cluster |
---|
| 1312 | if( local_cxy != ref_cxy ) |
---|
| 1313 | { |
---|
| 1314 | rpc_vmm_get_pte_client( ref_cxy, |
---|
| 1315 | ref_ptr, |
---|
| 1316 | vpn, |
---|
| 1317 | true, // copy-on-write |
---|
| 1318 | &attr, |
---|
| 1319 | &ppn, |
---|
| 1320 | &error ); |
---|
| 1321 | |
---|
| 1322 | // get local VMM pointer |
---|
| 1323 | vmm_t * vmm = &process->vmm; |
---|
| 1324 | |
---|
| 1325 | // update local GPT |
---|
| 1326 | error |= hal_gpt_set_pte( &vmm->gpt , vpn , ppn , attr ); |
---|
| 1327 | } |
---|
| 1328 | else // local cluster is the reference cluster |
---|
| 1329 | { |
---|
| 1330 | error = vmm_get_pte( process, |
---|
| 1331 | vpn, |
---|
| 1332 | true, // copy-on-write |
---|
| 1333 | &attr, |
---|
| 1334 | &ppn ); |
---|
| 1335 | } |
---|
| 1336 | |
---|
| 1337 | return error; |
---|
| 1338 | |
---|
| 1339 | } // end vmm_copy_on_write() |
---|
| 1340 | |
---|
[1] | 1341 | /////////////////////////////////////////// |
---|
| 1342 | error_t vmm_v2p_translate( bool_t ident, |
---|
| 1343 | void * ptr, |
---|
| 1344 | paddr_t * paddr ) |
---|
| 1345 | { |
---|
[23] | 1346 | process_t * process = CURRENT_THREAD->process; |
---|
[1] | 1347 | |
---|
| 1348 | if( ident ) // identity mapping |
---|
| 1349 | { |
---|
[23] | 1350 | *paddr = (paddr_t)PADDR( local_cxy , (lpa_t)ptr ); |
---|
[1] | 1351 | return 0; |
---|
| 1352 | } |
---|
| 1353 | |
---|
[21] | 1354 | // access page table |
---|
[1] | 1355 | error_t error; |
---|
| 1356 | vpn_t vpn; |
---|
| 1357 | uint32_t attr; |
---|
| 1358 | ppn_t ppn; |
---|
| 1359 | uint32_t offset; |
---|
| 1360 | |
---|
[23] | 1361 | vpn = (vpn_t)( (intptr_t)ptr >> CONFIG_PPM_PAGE_SHIFT ); |
---|
| 1362 | offset = (uint32_t)( ((intptr_t)ptr) & CONFIG_PPM_PAGE_MASK ); |
---|
[1] | 1363 | |
---|
[50] | 1364 | if( local_cxy == GET_CXY( process->ref_xp) ) // calling process is reference process |
---|
[1] | 1365 | { |
---|
[407] | 1366 | error = vmm_get_pte( process, vpn , false , &attr , &ppn ); |
---|
[1] | 1367 | } |
---|
[50] | 1368 | else // calling process is not reference process |
---|
[1] | 1369 | { |
---|
| 1370 | cxy_t ref_cxy = GET_CXY( process->ref_xp ); |
---|
| 1371 | process_t * ref_ptr = (process_t *)GET_PTR( process->ref_xp ); |
---|
[407] | 1372 | rpc_vmm_get_pte_client( ref_cxy , ref_ptr , vpn , false , &attr , &ppn , &error ); |
---|
[1] | 1373 | } |
---|
| 1374 | |
---|
[23] | 1375 | // set paddr |
---|
[1] | 1376 | *paddr = (((paddr_t)ppn) << CONFIG_PPM_PAGE_SHIFT) | offset; |
---|
[21] | 1377 | |
---|
[23] | 1378 | return error; |
---|
[313] | 1379 | |
---|
[315] | 1380 | } // end vmm_v2p_translate() |
---|
[1] | 1381 | |
---|
[315] | 1382 | |
---|