[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 | vseg_t * vseg_heap; |
---|
| 61 | intptr_t base; |
---|
| 62 | intptr_t size; |
---|
| 63 | |
---|
| 64 | // get pointer on VMM |
---|
| 65 | vmm_t * vmm = &process->vmm; |
---|
| 66 | |
---|
| 67 | // check UTILS zone size |
---|
| 68 | if( (CONFIG_VMM_KENTRY_SIZE + CONFIG_VMM_ARGS_SIZE + CONFIG_VMM_ENVS_SIZE ) > |
---|
| 69 | CONFIG_VMM_ELF_BASE ) |
---|
| 70 | { |
---|
| 71 | printk("\n[PANIC] in %s : UTILS zone too small for process %x\n", |
---|
| 72 | __FUNCTION__ , process->pid ); |
---|
| 73 | hal_core_sleep(); |
---|
| 74 | } |
---|
[21] | 75 | |
---|
[1] | 76 | // check max number of stacks slots |
---|
| 77 | if( CONFIG_THREAD_MAX_PER_CLUSTER > 32 ) |
---|
| 78 | { |
---|
[21] | 79 | printk("\n[PANIC] in %s : max number of threads per cluster for a single process" |
---|
[1] | 80 | " cannot be larger than 32\n", __FUNCTION__ ); |
---|
| 81 | hal_core_sleep(); |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | // check STACK zone size |
---|
[21] | 85 | if( (CONFIG_VMM_STACK_SIZE * CONFIG_THREAD_MAX_PER_CLUSTER) > |
---|
[1] | 86 | (CONFIG_VMM_VSPACE_SIZE - CONFIG_VMM_STACK_BASE) ) |
---|
| 87 | { |
---|
| 88 | printk("\n[PANIC] in %s : STACK zone too small for process %x\n", |
---|
| 89 | __FUNCTION__ , process->pid ); |
---|
| 90 | hal_core_sleep(); |
---|
| 91 | } |
---|
| 92 | |
---|
[21] | 93 | // initialize the rwlock protecting the vsegs list |
---|
[1] | 94 | rwlock_init( &vmm->vsegs_lock ); |
---|
| 95 | |
---|
| 96 | // initialize local list of vsegs and radix-tree |
---|
[23] | 97 | vmm->vsegs_nr = 0; |
---|
[1] | 98 | list_root_init( &vmm->vsegs_root ); |
---|
| 99 | error = grdxt_init( &vmm->grdxt, |
---|
| 100 | CONFIG_VMM_GRDXT_W1, |
---|
| 101 | CONFIG_VMM_GRDXT_W2, |
---|
| 102 | CONFIG_VMM_GRDXT_W3 ); |
---|
| 103 | if( error ) |
---|
| 104 | { |
---|
[21] | 105 | printk("\n[PANIC] in %s : cannot initialize radix tree for process %x\n", |
---|
[1] | 106 | __FUNCTION__ , process->pid ); |
---|
| 107 | hal_core_sleep(); |
---|
| 108 | } |
---|
| 109 | |
---|
| 110 | // register kentry vseg in VMM |
---|
| 111 | base = 1 << CONFIG_PPM_PAGE_SHIFT; |
---|
| 112 | size = CONFIG_VMM_KENTRY_SIZE << CONFIG_PPM_PAGE_SHIFT; |
---|
| 113 | vseg_kentry = vmm_create_vseg( process , base , size , VSEG_TYPE_CODE ); |
---|
[21] | 114 | if( vseg_kentry == NULL ) |
---|
[1] | 115 | { |
---|
| 116 | printk("\n[PANIC] in %s : cannot register kent vseg for process %x\n", |
---|
| 117 | __FUNCTION__ , process->pid ); |
---|
| 118 | hal_core_sleep(); |
---|
| 119 | } |
---|
| 120 | vmm->kent_vpn_base = 1; |
---|
| 121 | |
---|
| 122 | // register the args vseg in VMM |
---|
| 123 | base = (CONFIG_VMM_KENTRY_SIZE + 1 )<<CONFIG_PPM_PAGE_SHIFT; |
---|
| 124 | size = CONFIG_VMM_ARGS_SIZE << CONFIG_PPM_PAGE_SHIFT; |
---|
| 125 | vseg_args = vmm_create_vseg( process , base , size , VSEG_TYPE_DATA ); |
---|
[21] | 126 | if( vseg_args == NULL ) |
---|
[1] | 127 | { |
---|
| 128 | printk("\n[PANIC] in %s : cannot register args vseg for process %x\n", |
---|
| 129 | __FUNCTION__ , process->pid ); |
---|
| 130 | hal_core_sleep(); |
---|
| 131 | } |
---|
| 132 | vmm->args_vpn_base = CONFIG_VMM_KENTRY_SIZE + 1; |
---|
| 133 | |
---|
| 134 | // register the envs vseg in VMM |
---|
| 135 | base = (CONFIG_VMM_KENTRY_SIZE + CONFIG_VMM_ARGS_SIZE + 1 )<<CONFIG_PPM_PAGE_SHIFT; |
---|
| 136 | size = CONFIG_VMM_ENVS_SIZE << CONFIG_PPM_PAGE_SHIFT; |
---|
| 137 | vseg_envs = vmm_create_vseg( process , base , size , VSEG_TYPE_DATA ); |
---|
[21] | 138 | if( vseg_envs == NULL ) |
---|
[1] | 139 | { |
---|
| 140 | printk("\n[PANIC] in %s : cannot register envs vseg for process %x\n", |
---|
| 141 | __FUNCTION__ , process->pid ); |
---|
| 142 | hal_core_sleep(); |
---|
| 143 | } |
---|
| 144 | vmm->envs_vpn_base = CONFIG_VMM_KENTRY_SIZE + CONFIG_VMM_ARGS_SIZE + 1; |
---|
| 145 | |
---|
| 146 | // register the heap vseg in VMM |
---|
| 147 | base = CONFIG_VMM_HEAP_BASE << CONFIG_PPM_PAGE_SHIFT; |
---|
| 148 | size = (CONFIG_VMM_MMAP_BASE-CONFIG_VMM_HEAP_BASE) << CONFIG_PPM_PAGE_SHIFT; |
---|
| 149 | vseg_heap = vmm_create_vseg( process , base , size , VSEG_TYPE_HEAP ); |
---|
[21] | 150 | if( vseg_heap == NULL ) |
---|
[1] | 151 | { |
---|
| 152 | printk("\n[PANIC] in %s : cannot register heap vseg in for process %x\n", |
---|
| 153 | __FUNCTION__ , process->pid ); |
---|
| 154 | hal_core_sleep(); |
---|
| 155 | } |
---|
| 156 | vmm->heap_vpn_base = CONFIG_VMM_HEAP_BASE; |
---|
| 157 | |
---|
| 158 | // initialize generic page table |
---|
| 159 | error = hal_gpt_create( &vmm->gpt ); |
---|
| 160 | if( error ) |
---|
| 161 | { |
---|
| 162 | printk("PANIC in %s : cannot initialize page table\n", __FUNCTION__ ); |
---|
| 163 | hal_core_sleep(); |
---|
| 164 | } |
---|
| 165 | |
---|
| 166 | // initialize STACK allocator |
---|
| 167 | vmm->stack_mgr.bitmap = 0; |
---|
| 168 | vmm->stack_mgr.vpn_base = CONFIG_VMM_STACK_BASE; |
---|
| 169 | |
---|
| 170 | // initialize MMAP allocator |
---|
| 171 | vmm->mmap_mgr.vpn_base = CONFIG_VMM_MMAP_BASE; |
---|
| 172 | vmm->mmap_mgr.vpn_size = CONFIG_VMM_STACK_BASE - CONFIG_VMM_MMAP_BASE; |
---|
| 173 | vmm->mmap_mgr.first_free_vpn = CONFIG_VMM_MMAP_BASE; |
---|
| 174 | uint32_t i; |
---|
| 175 | for( i = 0 ; i < 32 ; i++ ) list_root_init( &vmm->mmap_mgr.zombi_list[i] ); |
---|
| 176 | |
---|
[21] | 177 | // initialize instrumentation counters |
---|
[1] | 178 | vmm->pgfault_nr = 0; |
---|
| 179 | vmm->u_err_nr = 0; |
---|
| 180 | vmm->m_err_nr = 0; |
---|
| 181 | |
---|
[124] | 182 | hal_fence(); |
---|
[1] | 183 | |
---|
| 184 | } // end vmm_init() |
---|
| 185 | |
---|
[23] | 186 | ////////////////////////////////////////// |
---|
| 187 | error_t vmm_copy( process_t * dst_process, |
---|
| 188 | process_t * src_process ) |
---|
| 189 | { |
---|
| 190 | error_t error; |
---|
| 191 | |
---|
| 192 | vmm_t * src_vmm = &src_process->vmm; |
---|
| 193 | vmm_t * dst_vmm = &dst_process->vmm; |
---|
| 194 | |
---|
| 195 | // take the src_vmm vsegs_lock |
---|
| 196 | rwlock_wr_lock( &src_vmm->vsegs_lock ); |
---|
| 197 | |
---|
| 198 | // initialise dst_vmm vsegs_lock |
---|
| 199 | rwlock_init( &dst_vmm->vsegs_lock ); |
---|
| 200 | |
---|
| 201 | // initialise the dst_vmm vsegs list and the radix tree |
---|
| 202 | dst_vmm->vsegs_nr = 0; |
---|
| 203 | list_root_init( &dst_vmm->vsegs_root ); |
---|
| 204 | error = grdxt_init( &dst_vmm->grdxt, |
---|
| 205 | CONFIG_VMM_GRDXT_W1, |
---|
| 206 | CONFIG_VMM_GRDXT_W2, |
---|
| 207 | CONFIG_VMM_GRDXT_W3 ); |
---|
| 208 | if( error ) |
---|
| 209 | { |
---|
| 210 | printk("\n[ERROR] in %s : cannot initialise radix tree for process %x\n", |
---|
| 211 | __FUNCTION__ , dst_process->pid ); |
---|
| 212 | return ENOMEM; |
---|
| 213 | } |
---|
| 214 | |
---|
| 215 | // loop on src_vmm list of vsegs to create |
---|
| 216 | // and register vsegs copies in dst_vmm |
---|
| 217 | list_entry_t * iter; |
---|
| 218 | vseg_t * src_vseg; |
---|
| 219 | vseg_t * dst_vseg; |
---|
| 220 | LIST_FOREACH( &src_vmm->vsegs_root , iter ) |
---|
| 221 | { |
---|
| 222 | // get pointer on current src_vseg |
---|
| 223 | src_vseg = LIST_ELEMENT( iter , vseg_t , list ); |
---|
| 224 | |
---|
| 225 | // allocate memory for a new dst_vseg |
---|
| 226 | dst_vseg = vseg_alloc(); |
---|
| 227 | |
---|
| 228 | if( dst_vseg == NULL ) |
---|
| 229 | { |
---|
| 230 | // release all allocated vsegs |
---|
| 231 | LIST_FOREACH( &dst_vmm->vsegs_root , iter ) |
---|
| 232 | { |
---|
| 233 | dst_vseg = LIST_ELEMENT( iter , vseg_t , list ); |
---|
| 234 | vseg_free( dst_vseg ); |
---|
| 235 | } |
---|
| 236 | return ENOMEM; |
---|
| 237 | } |
---|
| 238 | |
---|
| 239 | // copy src_vseg to dst_vseg |
---|
| 240 | vseg_init_from_ref( dst_vseg , XPTR( local_cxy , src_vseg ) ); |
---|
| 241 | |
---|
| 242 | // register dst_vseg in dst_vmm |
---|
| 243 | vseg_attach( dst_vmm , dst_vseg ); |
---|
| 244 | } |
---|
| 245 | |
---|
| 246 | // release the src_vmm vsegs_lock |
---|
| 247 | rwlock_wr_unlock( &src_vmm->vsegs_lock ); |
---|
| 248 | |
---|
| 249 | // initialize generic page table |
---|
| 250 | error = hal_gpt_create( &dst_vmm->gpt ); |
---|
| 251 | |
---|
| 252 | if( error ) |
---|
| 253 | { |
---|
| 254 | printk("\n[ERROR] in %s : cannot initialize page table\n", __FUNCTION__ ); |
---|
| 255 | return ENOMEM; |
---|
| 256 | } |
---|
| 257 | |
---|
| 258 | // initialize STACK allocator |
---|
| 259 | dst_vmm->stack_mgr.bitmap = 0; |
---|
| 260 | dst_vmm->stack_mgr.vpn_base = CONFIG_VMM_STACK_BASE; |
---|
| 261 | |
---|
| 262 | // initialize MMAP allocator |
---|
| 263 | dst_vmm->mmap_mgr.vpn_base = CONFIG_VMM_MMAP_BASE; |
---|
| 264 | dst_vmm->mmap_mgr.vpn_size = CONFIG_VMM_STACK_BASE - CONFIG_VMM_MMAP_BASE; |
---|
| 265 | dst_vmm->mmap_mgr.first_free_vpn = CONFIG_VMM_MMAP_BASE; |
---|
| 266 | uint32_t i; |
---|
| 267 | for( i = 0 ; i < 32 ; i++ ) list_root_init( &dst_vmm->mmap_mgr.zombi_list[i] ); |
---|
| 268 | |
---|
| 269 | // initialise instrumentation counters |
---|
| 270 | dst_vmm->pgfault_nr = 0; |
---|
| 271 | dst_vmm->u_err_nr = 0; |
---|
| 272 | dst_vmm->m_err_nr = 0; |
---|
| 273 | |
---|
| 274 | // copy base addresses |
---|
| 275 | dst_vmm->kent_vpn_base = src_vmm->kent_vpn_base; |
---|
| 276 | dst_vmm->args_vpn_base = src_vmm->args_vpn_base; |
---|
| 277 | dst_vmm->envs_vpn_base = src_vmm->envs_vpn_base; |
---|
| 278 | dst_vmm->heap_vpn_base = src_vmm->heap_vpn_base; |
---|
| 279 | dst_vmm->code_vpn_base = src_vmm->code_vpn_base; |
---|
| 280 | dst_vmm->data_vpn_base = src_vmm->data_vpn_base; |
---|
| 281 | |
---|
| 282 | dst_vmm->entry_point = src_vmm->entry_point; |
---|
| 283 | |
---|
| 284 | // HEAP TODO : new heap for child ??? |
---|
| 285 | dst_vmm->heap_vseg = src_vmm->heap_vseg; |
---|
| 286 | |
---|
| 287 | // initialize generic page table |
---|
| 288 | error = hal_gpt_create( &dst_vmm->gpt ); |
---|
| 289 | |
---|
| 290 | if( error ) |
---|
| 291 | { |
---|
| 292 | printk("\n[ERROR] in %s : cannot initialize page table\n", __FUNCTION__ ); |
---|
| 293 | return ENOMEM; |
---|
| 294 | } |
---|
| 295 | |
---|
| 296 | // copy GPT content from src_vmm to dst_vmm, activating "Copy-On-Write" |
---|
| 297 | // TODO register Copy-On_Write in page descriptors |
---|
| 298 | bool_t cow = true; |
---|
| 299 | hal_gpt_copy( &dst_vmm->gpt , &src_vmm->gpt , cow ); |
---|
| 300 | |
---|
[124] | 301 | hal_fence(); |
---|
[23] | 302 | |
---|
| 303 | return 0; |
---|
| 304 | |
---|
| 305 | } // end vmm_copy() |
---|
| 306 | |
---|
[1] | 307 | /////////////////////////////////////// |
---|
| 308 | void vmm_destroy( process_t * process ) |
---|
| 309 | { |
---|
| 310 | vseg_t * vseg; |
---|
| 311 | |
---|
| 312 | // get pointer on VMM |
---|
| 313 | vmm_t * vmm = &process->vmm; |
---|
| 314 | |
---|
| 315 | // get lock protecting vseg list |
---|
| 316 | rwlock_wr_lock( &vmm->vsegs_lock ); |
---|
| 317 | |
---|
| 318 | // remove all vsegs registered in vmm |
---|
| 319 | while( !list_is_empty( &vmm->vsegs_root ) ) |
---|
| 320 | { |
---|
| 321 | vseg = LIST_FIRST( &vmm->vsegs_root , vseg_t , list ); |
---|
| 322 | vseg_detach( vmm , vseg ); |
---|
| 323 | vseg_free( vseg ); |
---|
| 324 | } |
---|
| 325 | |
---|
| 326 | // delete vsegs radix_tree |
---|
| 327 | grdxt_destroy( &vmm->grdxt ); |
---|
| 328 | |
---|
| 329 | // release lock |
---|
| 330 | rwlock_wr_unlock(&vmm->vsegs_lock); |
---|
| 331 | |
---|
| 332 | // remove all vsegs from zombi_lists in MMAP allocator |
---|
| 333 | uint32_t i; |
---|
| 334 | for( i = 0 ; i<32 ; i++ ) |
---|
| 335 | { |
---|
| 336 | while( !list_is_empty( &vmm->mmap_mgr.zombi_list[i] ) ) |
---|
| 337 | { |
---|
| 338 | vseg = LIST_FIRST( &vmm->mmap_mgr.zombi_list[i] , vseg_t , list ); |
---|
| 339 | vseg_detach( vmm , vseg ); |
---|
| 340 | vseg_free( vseg ); |
---|
| 341 | } |
---|
| 342 | } |
---|
| 343 | |
---|
| 344 | // release memory allocated to the local page table |
---|
| 345 | hal_gpt_destroy( &vmm->gpt ); |
---|
| 346 | |
---|
| 347 | } // end vmm_destroy() |
---|
| 348 | |
---|
| 349 | ///////////////////////////////////////////////// |
---|
| 350 | vseg_t * vmm_check_conflict( process_t * process, |
---|
[21] | 351 | vpn_t vpn_base, |
---|
[1] | 352 | vpn_t vpn_size ) |
---|
| 353 | { |
---|
| 354 | vmm_t * vmm = &process->vmm; |
---|
| 355 | vseg_t * vseg; |
---|
| 356 | list_entry_t * iter; |
---|
| 357 | |
---|
| 358 | // scan the list of registered vsegs |
---|
| 359 | LIST_FOREACH( &vmm->vsegs_root , iter ) |
---|
| 360 | { |
---|
| 361 | vseg = LIST_ELEMENT( iter , vseg_t , list ); |
---|
[21] | 362 | if( ((vpn_base + vpn_size) > vseg->vpn_base) && |
---|
| 363 | (vpn_base < (vseg->vpn_base + vseg->vpn_size)) ) return vseg; |
---|
[1] | 364 | } |
---|
| 365 | return NULL; |
---|
| 366 | } // vmm_check_conflict() |
---|
| 367 | |
---|
| 368 | //////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 369 | // This static function is called by the vmm_create_vseg() function, and implements |
---|
| 370 | // the VMM stack_vseg specific allocator. |
---|
| 371 | //////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 372 | // @ vmm : pointer on VMM. |
---|
[21] | 373 | // @ vpn_base : (return value) first allocated page |
---|
[1] | 374 | // @ vpn_size : (return value) number of allocated pages |
---|
| 375 | //////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 376 | static error_t vmm_stack_alloc( vmm_t * vmm, |
---|
| 377 | vpn_t * vpn_base, |
---|
| 378 | vpn_t * vpn_size ) |
---|
| 379 | { |
---|
| 380 | // get stack allocator pointer |
---|
| 381 | stack_mgr_t * mgr = &vmm->stack_mgr; |
---|
| 382 | |
---|
| 383 | // get lock on stack allocator |
---|
| 384 | spinlock_lock( &mgr->lock ); |
---|
| 385 | |
---|
| 386 | // get first free slot index in bitmap |
---|
| 387 | int32_t index = bitmap_ffc( &mgr->bitmap , 4 ); |
---|
| 388 | if( (index < 0) || (index > 31) ) return ENOMEM; |
---|
| 389 | |
---|
| 390 | // update bitmap |
---|
| 391 | bitmap_set( &mgr->bitmap , index ); |
---|
[21] | 392 | |
---|
[1] | 393 | // release lock on stack allocator |
---|
| 394 | spinlock_unlock( &mgr->lock ); |
---|
| 395 | |
---|
[21] | 396 | // returns vpn_base, vpn_size (one page non allocated) |
---|
[1] | 397 | *vpn_base = mgr->vpn_base + index * CONFIG_VMM_STACK_SIZE + 1; |
---|
| 398 | *vpn_size = CONFIG_VMM_STACK_SIZE - 1; |
---|
| 399 | return 0; |
---|
| 400 | |
---|
| 401 | } // end vmm_stack_alloc() |
---|
| 402 | |
---|
| 403 | //////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 404 | // This static function is called by the vmm_create_vseg() function, and implements |
---|
| 405 | // the VMM MMAP specific allocator. |
---|
| 406 | //////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 407 | // @ vmm : [in] pointer on VMM. |
---|
| 408 | // @ npages : [in] requested number of pages. |
---|
[21] | 409 | // @ vpn_base : [out] first allocated page. |
---|
[1] | 410 | // @ vpn_size : [out] actual number of allocated pages. |
---|
| 411 | //////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 412 | static error_t vmm_mmap_alloc( vmm_t * vmm, |
---|
| 413 | vpn_t npages, |
---|
| 414 | vpn_t * vpn_base, |
---|
| 415 | vpn_t * vpn_size ) |
---|
| 416 | { |
---|
| 417 | uint32_t index; |
---|
| 418 | vseg_t * vseg; |
---|
| 419 | vpn_t base; |
---|
| 420 | vpn_t size; |
---|
[21] | 421 | vpn_t free; |
---|
[1] | 422 | |
---|
[21] | 423 | // mmap vseg size must be power of 2 |
---|
[1] | 424 | // compute actual size and index in zombi_list array |
---|
| 425 | size = POW2_ROUNDUP( npages ); |
---|
| 426 | index = bits_log2( size ); |
---|
| 427 | |
---|
| 428 | // get mmap allocator pointer |
---|
| 429 | mmap_mgr_t * mgr = &vmm->mmap_mgr; |
---|
| 430 | |
---|
| 431 | // get lock on mmap allocator |
---|
| 432 | spinlock_lock( &mgr->lock ); |
---|
| 433 | |
---|
| 434 | // get vseg from zombi_list or from mmap zone |
---|
| 435 | if( list_is_empty( &mgr->zombi_list[index] ) ) // from mmap zone |
---|
| 436 | { |
---|
| 437 | // check overflow |
---|
| 438 | free = mgr->first_free_vpn; |
---|
| 439 | if( (free + size) > mgr->vpn_size ) return ENOMEM; |
---|
| 440 | |
---|
| 441 | // update STACK allocator |
---|
| 442 | mgr->first_free_vpn += size; |
---|
| 443 | |
---|
| 444 | // compute base |
---|
| 445 | base = free; |
---|
| 446 | } |
---|
| 447 | else // from zombi_list |
---|
| 448 | { |
---|
| 449 | // get pointer on zombi vseg from zombi_list |
---|
| 450 | vseg = LIST_FIRST( &mgr->zombi_list[index] , vseg_t , list ); |
---|
| 451 | |
---|
| 452 | // remove vseg from free-list |
---|
| 453 | list_unlink( &vseg->list ); |
---|
| 454 | |
---|
| 455 | // compute base |
---|
| 456 | base = vseg->vpn_base; |
---|
[21] | 457 | } |
---|
| 458 | |
---|
[1] | 459 | // release lock on mmap allocator |
---|
| 460 | spinlock_unlock( &mgr->lock ); |
---|
| 461 | |
---|
| 462 | // returns vpn_base, vpn_size |
---|
| 463 | *vpn_base = base; |
---|
| 464 | *vpn_size = size; |
---|
| 465 | return 0; |
---|
| 466 | |
---|
[21] | 467 | } // end vmm_mmap_alloc() |
---|
[1] | 468 | |
---|
| 469 | ////////////////////////////////////////////// |
---|
| 470 | vseg_t * vmm_create_vseg( process_t * process, |
---|
[21] | 471 | intptr_t base, |
---|
| 472 | intptr_t size, |
---|
[1] | 473 | uint32_t type ) |
---|
| 474 | { |
---|
| 475 | vseg_t * vseg; // created vseg pointer |
---|
[21] | 476 | vpn_t vpn_base; // vseg first page |
---|
[1] | 477 | vpn_t vpn_size; // number of pages |
---|
| 478 | error_t error; |
---|
| 479 | |
---|
| 480 | // get pointer on VMM |
---|
| 481 | vmm_t * vmm = &process->vmm; |
---|
[21] | 482 | |
---|
[1] | 483 | vmm_dmsg("\n[INFO] %s enter for process %x / base = %x / size = %x / type = %s\n", |
---|
[101] | 484 | __FUNCTION__ , process->pid , base , size , vseg_type_str(type) ); |
---|
[21] | 485 | |
---|
[1] | 486 | // compute base, size, vpn_base, vpn_size, depending on type |
---|
| 487 | // we use the VMM specific allocators for STACK and MMAP vsegs |
---|
| 488 | if( type == VSEG_TYPE_STACK ) |
---|
| 489 | { |
---|
| 490 | // get vpn_base and vpn_size from STACK allocator |
---|
| 491 | error = vmm_stack_alloc( vmm , &vpn_base , &vpn_size ); |
---|
| 492 | if( error ) |
---|
| 493 | { |
---|
| 494 | printk("\n[ERROR] in %s : no vspace for stack vseg / process %x in cluster %x\n", |
---|
| 495 | __FUNCTION__ , process->pid , local_cxy ); |
---|
| 496 | return NULL; |
---|
| 497 | } |
---|
| 498 | |
---|
| 499 | // compute vseg base and size from vpn_base and vpn_size |
---|
| 500 | base = vpn_base << CONFIG_PPM_PAGE_SHIFT; |
---|
| 501 | size = vpn_size << CONFIG_PPM_PAGE_SHIFT; |
---|
| 502 | } |
---|
[21] | 503 | else if( (type == VSEG_TYPE_ANON) || |
---|
| 504 | (type == VSEG_TYPE_FILE) || |
---|
[1] | 505 | (type == VSEG_TYPE_REMOTE) ) |
---|
| 506 | { |
---|
| 507 | // get vpn_base and vpn_size from MMAP allocator |
---|
| 508 | vpn_t npages = size >> CONFIG_PPM_PAGE_SHIFT; |
---|
| 509 | error = vmm_mmap_alloc( vmm , npages , &vpn_base , &vpn_size ); |
---|
| 510 | if( error ) |
---|
| 511 | { |
---|
| 512 | printk("\n[ERROR] in %s : no vspace for mmap vseg / process %x in cluster %x\n", |
---|
| 513 | __FUNCTION__ , process->pid , local_cxy ); |
---|
| 514 | return NULL; |
---|
| 515 | } |
---|
| 516 | |
---|
| 517 | // compute vseg base and size from vpn_base and vpn_size |
---|
| 518 | base = vpn_base << CONFIG_PPM_PAGE_SHIFT; |
---|
| 519 | size = vpn_size << CONFIG_PPM_PAGE_SHIFT; |
---|
| 520 | } |
---|
| 521 | else |
---|
| 522 | { |
---|
| 523 | vpn_base = ARROUND_DOWN( base , CONFIG_PPM_PAGE_SIZE ) >> CONFIG_PPM_PAGE_SHIFT; |
---|
| 524 | vpn_size = ARROUND_UP( base + size , CONFIG_PPM_PAGE_SIZE ) >> CONFIG_PPM_PAGE_SHIFT; |
---|
| 525 | } |
---|
| 526 | |
---|
| 527 | // check collisions |
---|
| 528 | vseg = vmm_check_conflict( process , vpn_base , vpn_size ); |
---|
| 529 | if( vseg != NULL ) |
---|
| 530 | { |
---|
[21] | 531 | printk("\n[ERROR] in %s for process %x : new vseg [vpn_base = %x / vpn_size = %x]\n" |
---|
[1] | 532 | " overlap existing vseg [vpn_base = %x / vpn_size = %x]\n", |
---|
[21] | 533 | __FUNCTION__ , process->pid, vpn_base, vpn_size, |
---|
| 534 | vseg->vpn_base, vseg->vpn_size ); |
---|
[1] | 535 | return NULL; |
---|
| 536 | } |
---|
| 537 | |
---|
| 538 | // allocate physical memory for vseg descriptor |
---|
| 539 | vseg = vseg_alloc(); |
---|
| 540 | if( vseg == NULL ) |
---|
| 541 | { |
---|
| 542 | printk("\n[ERROR] in %s for process %x : cannot allocate memory for vseg\n", |
---|
| 543 | __FUNCTION__ , process->pid ); |
---|
| 544 | return NULL; |
---|
| 545 | } |
---|
| 546 | |
---|
| 547 | // initialize vseg descriptor |
---|
| 548 | vseg_init( vseg , base, size , vpn_base , vpn_size , type , local_cxy , 0 , 0 ); |
---|
| 549 | |
---|
| 550 | // update "heap_vseg" in VMM |
---|
| 551 | process->vmm.heap_vseg = vseg; |
---|
| 552 | |
---|
| 553 | // attach vseg to vmm |
---|
| 554 | rwlock_wr_lock( &vmm->vsegs_lock ); |
---|
| 555 | vseg_attach( vmm , vseg ); |
---|
| 556 | rwlock_wr_unlock( &vmm->vsegs_lock ); |
---|
| 557 | |
---|
[21] | 558 | vmm_dmsg("\n[INFO] : %s exit for process %x, vseg [%x, %x] has been mapped\n", |
---|
[1] | 559 | __FUNCTION__ , process->pid , vseg->min , vseg->max ); |
---|
[21] | 560 | |
---|
[1] | 561 | return vseg; |
---|
| 562 | |
---|
| 563 | } // end vmm_create_vseg() |
---|
| 564 | |
---|
| 565 | ///////////////////////////////////// |
---|
| 566 | void vmm_remove_vseg( vseg_t * vseg ) |
---|
| 567 | { |
---|
| 568 | // get pointers on calling process and VMM |
---|
| 569 | thread_t * this = CURRENT_THREAD; |
---|
| 570 | process_t * process = this->process; |
---|
| 571 | vmm_t * vmm = &this->process->vmm; |
---|
| 572 | uint32_t type = vseg->type; |
---|
| 573 | |
---|
| 574 | // detach vseg from VMM |
---|
| 575 | rwlock_wr_lock( &vmm->vsegs_lock ); |
---|
| 576 | vseg_detach( &process->vmm , vseg ); |
---|
| 577 | rwlock_wr_unlock( &vmm->vsegs_lock ); |
---|
| 578 | |
---|
| 579 | // release the stack slot to VMM stack allocator if STACK type |
---|
| 580 | if( type == VSEG_TYPE_STACK ) |
---|
| 581 | { |
---|
| 582 | // get pointer on stack allocator |
---|
| 583 | stack_mgr_t * mgr = &vmm->stack_mgr; |
---|
| 584 | |
---|
| 585 | // compute slot index |
---|
| 586 | uint32_t index = ((vseg->vpn_base - mgr->vpn_base - 1) / CONFIG_VMM_STACK_SIZE); |
---|
| 587 | |
---|
| 588 | // update stacks_bitmap |
---|
| 589 | spinlock_lock( &mgr->lock ); |
---|
| 590 | bitmap_clear( &mgr->bitmap , index ); |
---|
| 591 | spinlock_unlock( &mgr->lock ); |
---|
| 592 | } |
---|
| 593 | |
---|
| 594 | // release the vseg to VMM mmap allocator if MMAP type |
---|
| 595 | if( (type == VSEG_TYPE_ANON) || (type == VSEG_TYPE_FILE) || (type == VSEG_TYPE_REMOTE) ) |
---|
| 596 | { |
---|
| 597 | // get pointer on mmap allocator |
---|
| 598 | mmap_mgr_t * mgr = &vmm->mmap_mgr; |
---|
| 599 | |
---|
| 600 | // compute zombi_list index |
---|
| 601 | uint32_t index = bits_log2( vseg->vpn_size ); |
---|
| 602 | |
---|
| 603 | // update zombi_list |
---|
| 604 | spinlock_lock( &mgr->lock ); |
---|
| 605 | list_add_first( &mgr->zombi_list[index] , &vseg->list ); |
---|
| 606 | spinlock_unlock( &mgr->lock ); |
---|
| 607 | } |
---|
| 608 | |
---|
| 609 | // release physical memory allocated for vseg descriptor if no MMAP type |
---|
| 610 | if( (type != VSEG_TYPE_ANON) && (type != VSEG_TYPE_FILE) && (type != VSEG_TYPE_REMOTE) ) |
---|
| 611 | { |
---|
| 612 | vseg_free( vseg ); |
---|
| 613 | } |
---|
| 614 | |
---|
| 615 | } // end vmm_remove_vseg() |
---|
| 616 | |
---|
| 617 | |
---|
[68] | 618 | ////////////////////////////////////////////// |
---|
| 619 | error_t vmm_map_kernel_vseg( vseg_t * vseg, |
---|
| 620 | uint32_t attr ) |
---|
[1] | 621 | { |
---|
| 622 | vpn_t vpn; // VPN of PTE to be set |
---|
| 623 | vpn_t vpn_min; // VPN of first PTE to be set |
---|
| 624 | vpn_t vpn_max; // VPN of last PTE to be set (excluded) |
---|
| 625 | ppn_t ppn; // PPN of allocated physical page |
---|
| 626 | uint32_t order; // ln( number of small pages for one single PTE ) |
---|
| 627 | page_t * page; |
---|
| 628 | error_t error; |
---|
| 629 | |
---|
[68] | 630 | // check vseg type : must be a kernel vseg |
---|
[1] | 631 | uint32_t type = vseg->type; |
---|
[68] | 632 | assert( ((type==VSEG_TYPE_KCODE) || (type==VSEG_TYPE_KDATA) || (type==VSEG_TYPE_KDEV)), |
---|
| 633 | __FUNCTION__ , "not a kernel vseg\n" ); |
---|
[1] | 634 | |
---|
| 635 | // get pointer on page table |
---|
| 636 | gpt_t * gpt = &process_zero.vmm.gpt; |
---|
| 637 | |
---|
[21] | 638 | // define number of small pages per PTE |
---|
[1] | 639 | if( attr & GPT_SMALL ) order = 0; // 1 small page |
---|
| 640 | else order = 9; // 512 small pages |
---|
| 641 | |
---|
| 642 | // loop on pages in vseg |
---|
| 643 | vpn_min = vseg->vpn_base; |
---|
| 644 | vpn_max = vpn_min + vseg->vpn_size; |
---|
| 645 | for( vpn = vpn_min ; vpn < vpn_max ; vpn++ ) |
---|
| 646 | { |
---|
[68] | 647 | // allocate a physical page from local PPM |
---|
[1] | 648 | kmem_req_t req; |
---|
| 649 | req.type = KMEM_PAGE; |
---|
| 650 | req.size = order; |
---|
| 651 | req.flags = AF_KERNEL | AF_ZERO; |
---|
| 652 | page = (page_t *)kmem_alloc( &req ); |
---|
[21] | 653 | if( page == NULL ) |
---|
[1] | 654 | { |
---|
| 655 | printk("\n[ERROR] in %s : cannot allocate physical memory\n", __FUNCTION__ ); |
---|
| 656 | return ENOMEM; |
---|
| 657 | } |
---|
| 658 | |
---|
| 659 | // set page table entry |
---|
| 660 | ppn = ppm_page2ppn( page ); |
---|
| 661 | error = hal_gpt_set_pte( gpt , vpn , ppn , attr ); |
---|
[21] | 662 | if( error ) |
---|
[1] | 663 | { |
---|
| 664 | printk("\n[ERROR] in %s : cannot register PPE\n", __FUNCTION__ ); |
---|
| 665 | return ENOMEM; |
---|
| 666 | } |
---|
| 667 | } |
---|
[21] | 668 | |
---|
[1] | 669 | return 0; |
---|
| 670 | } // end vmm_map_vseg() |
---|
| 671 | |
---|
| 672 | ///////////////////////////////////////// |
---|
| 673 | void vmm_unmap_vseg( process_t * process, |
---|
| 674 | vseg_t * vseg ) |
---|
| 675 | { |
---|
[21] | 676 | vpn_t vpn; // VPN of current PTE |
---|
| 677 | vpn_t vpn_min; // VPN of first PTE |
---|
[1] | 678 | vpn_t vpn_max; // VPN of last PTE (excluded) |
---|
| 679 | |
---|
| 680 | // get pointer on process page table |
---|
| 681 | gpt_t * gpt = &process->vmm.gpt; |
---|
| 682 | |
---|
| 683 | // loop on pages in vseg |
---|
| 684 | vpn_min = vseg->vpn_base; |
---|
| 685 | vpn_max = vpn_min + vseg->vpn_size; |
---|
| 686 | for( vpn = vpn_min ; vpn < vpn_max ; vpn++ ) |
---|
| 687 | { |
---|
| 688 | hal_gpt_reset_pte( gpt , vpn ); |
---|
| 689 | } |
---|
| 690 | } // end vmm_unmap_vseg() |
---|
| 691 | |
---|
| 692 | |
---|
| 693 | ///////////////////////////////////////////// |
---|
| 694 | error_t vmm_resize_vseg( process_t * process, |
---|
| 695 | intptr_t base, |
---|
| 696 | intptr_t size ) |
---|
| 697 | { |
---|
| 698 | error_t error; |
---|
| 699 | |
---|
| 700 | // get pointer on process VMM |
---|
| 701 | vmm_t * vmm = &process->vmm; |
---|
| 702 | |
---|
| 703 | intptr_t addr_min = base; |
---|
| 704 | intptr_t addr_max = base + size; |
---|
[21] | 705 | uint32_t shift = CONFIG_PPM_PAGE_SHIFT; |
---|
[1] | 706 | |
---|
| 707 | // get pointer on vseg |
---|
| 708 | vseg_t * vseg = grdxt_lookup( &vmm->grdxt , (uint32_t)(base >> shift) ); |
---|
| 709 | |
---|
| 710 | if( vseg == NULL) return EINVAL; |
---|
[21] | 711 | |
---|
[1] | 712 | // get VMM lock protecting vsegs list |
---|
| 713 | rwlock_wr_lock( &vmm->vsegs_lock ); |
---|
[21] | 714 | |
---|
[1] | 715 | if( (vseg->min > addr_min) || (vseg->max < addr_max) ) // region not included in vseg |
---|
| 716 | { |
---|
| 717 | error = EINVAL; |
---|
| 718 | } |
---|
| 719 | else if( (vseg->min == addr_min) && (vseg->max == addr_max) ) // vseg must be removed |
---|
| 720 | { |
---|
| 721 | vmm_remove_vseg( vseg ); |
---|
| 722 | error = 0; |
---|
| 723 | } |
---|
| 724 | else if( vseg->min == addr_min ) // vseg must be resized |
---|
| 725 | { |
---|
| 726 | printk("\n[PANIC] in %s : resize not implemented yet\n", __FUNCTION__ ); |
---|
| 727 | hal_core_sleep(); |
---|
| 728 | error = 0; |
---|
| 729 | } |
---|
| 730 | else if( vseg->max == addr_max ) // vseg must be resized |
---|
| 731 | { |
---|
| 732 | printk("\n[PANIC] in %s : resize not implemented yet\n", __FUNCTION__ ); |
---|
| 733 | hal_core_sleep(); |
---|
| 734 | error = 0; |
---|
| 735 | } |
---|
| 736 | else // vseg cut in three regions => vseg must be resized & new vseg created |
---|
| 737 | { |
---|
| 738 | printk("\n[PANIC] in %s : resize not implemented yet\n", __FUNCTION__ ); |
---|
| 739 | hal_core_sleep(); |
---|
| 740 | error = 0; |
---|
| 741 | } |
---|
| 742 | |
---|
| 743 | // release VMM lock |
---|
| 744 | rwlock_wr_unlock( &vmm->vsegs_lock ); |
---|
| 745 | |
---|
| 746 | return error; |
---|
| 747 | } // end vmm_resize_vseg() |
---|
| 748 | |
---|
| 749 | /////////////////////////////////////////// |
---|
| 750 | vseg_t * vmm_get_vseg( process_t * process, |
---|
| 751 | intptr_t vaddr ) |
---|
| 752 | { |
---|
| 753 | |
---|
| 754 | // get pointer on process VMM |
---|
| 755 | vmm_t * vmm = &process->vmm; |
---|
| 756 | |
---|
| 757 | // get lock protecting the vseg list |
---|
| 758 | rwlock_rd_lock( &vmm->vsegs_lock ); |
---|
| 759 | |
---|
| 760 | // get pointer on vseg from radix tree |
---|
| 761 | vseg_t * vseg = grdxt_lookup( &vmm->grdxt, (uint32_t)(vaddr >> CONFIG_PPM_PAGE_SHIFT) ); |
---|
| 762 | |
---|
| 763 | // release the lock |
---|
| 764 | rwlock_rd_unlock( &vmm->vsegs_lock ); |
---|
| 765 | |
---|
| 766 | return vseg; |
---|
| 767 | |
---|
| 768 | } // end vmm_get_vseg() |
---|
| 769 | |
---|
| 770 | ///////////////////////////////////////// |
---|
| 771 | error_t vmm_get_pte( process_t * process, |
---|
| 772 | vpn_t vpn, |
---|
| 773 | uint32_t * ret_attr, |
---|
| 774 | ppn_t * ret_ppn ) |
---|
| 775 | { |
---|
| 776 | vseg_t * vseg; // pointer on vseg containing VPN |
---|
| 777 | ppn_t ppn; // PPN from GPT entry |
---|
| 778 | uint32_t attr; // attributes from GPT entry |
---|
| 779 | error_t error; |
---|
| 780 | |
---|
[23] | 781 | // this function must be called by a thread running in the reference cluster |
---|
[68] | 782 | assert( (GET_CXY( process->ref_xp ) == local_cxy ) , __FUNCTION__ , |
---|
| 783 | " not called in the reference cluster\n" ); |
---|
[1] | 784 | |
---|
| 785 | // get VMM pointer |
---|
| 786 | vmm_t * vmm = &process->vmm; |
---|
| 787 | |
---|
| 788 | // access GPT to get PTE attributes and PPN |
---|
| 789 | hal_gpt_get_pte( &vmm->gpt , vpn , &attr , &ppn ); |
---|
| 790 | |
---|
| 791 | // if PTE unmapped => allocate one small physical page to map it |
---|
[21] | 792 | if( (attr & GPT_MAPPED) == 0 ) |
---|
[1] | 793 | { |
---|
| 794 | // get vseg pointer |
---|
| 795 | vseg = vmm_get_vseg( process , vpn<<CONFIG_PPM_PAGE_SHIFT ); |
---|
| 796 | |
---|
| 797 | if( vseg == NULL ); |
---|
| 798 | { |
---|
| 799 | printk("\n[ERROR] in %s : out of segment / process = %x / vpn = %x\n", |
---|
| 800 | __FUNCTION__ , process->pid , vpn ); |
---|
| 801 | return EINVAL; |
---|
| 802 | } |
---|
| 803 | |
---|
| 804 | // select the target cluster for physical mapping |
---|
| 805 | uint32_t target_cxy; |
---|
| 806 | if( vseg->flags & VSEG_DISTRIB ) // depends on VPN LSB |
---|
| 807 | { |
---|
| 808 | uint32_t x_width = LOCAL_CLUSTER->x_width; |
---|
| 809 | uint32_t y_width = LOCAL_CLUSTER->y_width; |
---|
| 810 | target_cxy = vpn & ((1<<(x_width + y_width)) - 1); |
---|
| 811 | } |
---|
| 812 | else // defined in vseg descriptor |
---|
| 813 | { |
---|
| 814 | target_cxy = vseg->cxy; |
---|
| 815 | } |
---|
| 816 | |
---|
| 817 | // allocate memory for page fault |
---|
| 818 | kmem_req_t req; |
---|
| 819 | page_t * page; |
---|
| 820 | if( target_cxy == local_cxy ) // target cluster is the local cluster |
---|
[21] | 821 | { |
---|
[1] | 822 | req.type = KMEM_PAGE; |
---|
| 823 | req.size = 0; |
---|
| 824 | req.flags = AF_NONE; |
---|
| 825 | page = (page_t *)kmem_alloc( &req ); |
---|
| 826 | |
---|
| 827 | error = ( page == NULL ) ? 1 : 0; |
---|
| 828 | ppn = ppm_page2ppn( page ); |
---|
| 829 | } |
---|
| 830 | else // target cluster is not the local cluster |
---|
| 831 | { |
---|
| 832 | rpc_pmem_get_pages_client( target_cxy , 0 , &error , &ppn ); |
---|
| 833 | } |
---|
| 834 | |
---|
| 835 | if( error ) |
---|
| 836 | { |
---|
| 837 | printk("\n[ERROR] in %s : cannot allocate memory / process = %x / vpn = %x\n", |
---|
| 838 | __FUNCTION__ , process->pid , vpn ); |
---|
| 839 | return ENOMEM; |
---|
| 840 | } |
---|
| 841 | |
---|
[21] | 842 | // define GPT attributes from vseg flags |
---|
[1] | 843 | attr = GPT_MAPPED | GPT_SMALL; |
---|
| 844 | if( vseg->flags & VSEG_USER ) attr |= GPT_USER; |
---|
| 845 | if( vseg->flags & VSEG_WRITE ) attr |= GPT_WRITABLE; |
---|
| 846 | if( vseg->flags & VSEG_EXEC ) attr |= GPT_EXECUTABLE; |
---|
| 847 | if( vseg->flags & VSEG_CACHE ) attr |= GPT_CACHABLE; |
---|
| 848 | |
---|
| 849 | // set the missing PTE in local VMM |
---|
| 850 | error = hal_gpt_set_pte( &vmm->gpt , vpn , ppn , attr ); |
---|
| 851 | if( error ) |
---|
| 852 | { |
---|
| 853 | printk("\n[ERROR] in %s : cannot register PTE / process = %x / vpn = %x\n", |
---|
| 854 | __FUNCTION__ , process->pid , vpn ); |
---|
| 855 | return ENOMEM; |
---|
| 856 | } |
---|
| 857 | } |
---|
| 858 | |
---|
| 859 | *ret_ppn = ppn; |
---|
[21] | 860 | *ret_attr = attr; |
---|
[1] | 861 | return 0; |
---|
| 862 | } // end vmm_get_pte() |
---|
| 863 | |
---|
| 864 | /////////////////////////////////////////////////// |
---|
| 865 | error_t vmm_handle_page_fault( process_t * process, |
---|
| 866 | vseg_t * vseg, |
---|
| 867 | vpn_t vpn ) |
---|
| 868 | { |
---|
| 869 | uint32_t attr; // missing page attributes |
---|
[21] | 870 | ppn_t ppn; // missing page PPN |
---|
[1] | 871 | error_t error; // return value |
---|
| 872 | |
---|
| 873 | // get local VMM pointer |
---|
| 874 | vmm_t * vmm = &process->vmm; |
---|
| 875 | |
---|
| 876 | // get reference process cluster and local pointer |
---|
| 877 | cxy_t ref_cxy = GET_CXY( process->ref_xp ); |
---|
| 878 | process_t * ref_ptr = (process_t *)GET_PTR( process->ref_xp ); |
---|
| 879 | |
---|
| 880 | // get missing PTE attributes and PPN |
---|
| 881 | if( local_cxy != ref_cxy ) // local cluster is not the reference cluster |
---|
| 882 | { |
---|
| 883 | rpc_vmm_get_pte_client( ref_cxy , ref_ptr , vpn , &attr , &ppn , &error ); |
---|
| 884 | } |
---|
[21] | 885 | else // local cluster is the reference cluster |
---|
[1] | 886 | { |
---|
| 887 | error = vmm_get_pte( process , vpn , &attr , &ppn ); |
---|
| 888 | } |
---|
| 889 | |
---|
| 890 | // check page allocation error |
---|
| 891 | if( error ) |
---|
| 892 | { |
---|
| 893 | printk("\n[ERROR] in %s : cannot allocate memory / process = %x / vpn = %x\n", |
---|
[21] | 894 | __FUNCTION__ , process->pid , vpn ); |
---|
[1] | 895 | return ENOMEM; |
---|
| 896 | } |
---|
| 897 | |
---|
| 898 | // set the missing PTE in local VMM |
---|
| 899 | error = hal_gpt_set_pte( &vmm->gpt , vpn , attr , ppn ); |
---|
| 900 | if( error ) |
---|
| 901 | { |
---|
| 902 | printk("\n[ERROR] in %s : cannot register PTE / process = %x / vpn = %x\n", |
---|
[21] | 903 | __FUNCTION__ , process->pid , vpn ); |
---|
[1] | 904 | return ENOMEM; |
---|
| 905 | } |
---|
[21] | 906 | |
---|
[1] | 907 | return 0; |
---|
| 908 | } // end vmm_handle_page_fault() |
---|
| 909 | |
---|
| 910 | /////////////////////////////////////////// |
---|
| 911 | error_t vmm_v2p_translate( bool_t ident, |
---|
| 912 | void * ptr, |
---|
| 913 | paddr_t * paddr ) |
---|
| 914 | { |
---|
[23] | 915 | process_t * process = CURRENT_THREAD->process; |
---|
[1] | 916 | |
---|
| 917 | if( ident ) // identity mapping |
---|
| 918 | { |
---|
[23] | 919 | *paddr = (paddr_t)PADDR( local_cxy , (lpa_t)ptr ); |
---|
[1] | 920 | return 0; |
---|
| 921 | } |
---|
| 922 | |
---|
[21] | 923 | // access page table |
---|
[1] | 924 | error_t error; |
---|
| 925 | vpn_t vpn; |
---|
| 926 | uint32_t attr; |
---|
| 927 | ppn_t ppn; |
---|
| 928 | uint32_t offset; |
---|
| 929 | |
---|
[23] | 930 | vpn = (vpn_t)( (intptr_t)ptr >> CONFIG_PPM_PAGE_SHIFT ); |
---|
| 931 | offset = (uint32_t)( ((intptr_t)ptr) & CONFIG_PPM_PAGE_MASK ); |
---|
[1] | 932 | |
---|
[50] | 933 | if( local_cxy == GET_CXY( process->ref_xp) ) // calling process is reference process |
---|
[1] | 934 | { |
---|
| 935 | error = vmm_get_pte( process, vpn , &attr , &ppn ); |
---|
| 936 | } |
---|
[50] | 937 | else // calling process is not reference process |
---|
[1] | 938 | { |
---|
| 939 | cxy_t ref_cxy = GET_CXY( process->ref_xp ); |
---|
| 940 | process_t * ref_ptr = (process_t *)GET_PTR( process->ref_xp ); |
---|
| 941 | rpc_vmm_get_pte_client( ref_cxy , ref_ptr , vpn , &attr , &ppn , &error ); |
---|
| 942 | } |
---|
| 943 | |
---|
[23] | 944 | // set paddr |
---|
[1] | 945 | *paddr = (((paddr_t)ppn) << CONFIG_PPM_PAGE_SHIFT) | offset; |
---|
[21] | 946 | |
---|
[23] | 947 | return error; |
---|
| 948 | |
---|
[1] | 949 | } // end vmm_v2p_translate() |
---|
| 950 | |
---|
| 951 | |
---|
| 952 | |
---|
| 953 | |
---|
[23] | 954 | |
---|
[1] | 955 | /* |
---|
| 956 | |
---|
| 957 | /////////////////////////////////////////////////////////////////// |
---|
| 958 | /////////////////////////////////////////////////////////////////// |
---|
| 959 | error_t vmm_inval_shared_page( vseg_t *vseg, vma_t vaddr, ppn_t ppn) |
---|
| 960 | { |
---|
| 961 | pmm_page_info_t current; |
---|
| 962 | error_t err; |
---|
| 963 | |
---|
| 964 | error= pmm_get_page(&vseg->vmm->pmm, vaddr, ¤t); |
---|
| 965 | |
---|
| 966 | if((err) || (current.ppn != ppn)) |
---|
| 967 | goto ended; |
---|
| 968 | |
---|
| 969 | current.ppn = 0; |
---|
| 970 | current.attr = 0; |
---|
| 971 | current.cluster = NULL; |
---|
| 972 | |
---|
| 973 | error= pmm_set_page(&vseg->vmm->pmm, vaddr, ¤t); |
---|
| 974 | |
---|
| 975 | ended: |
---|
| 976 | return err; |
---|
| 977 | } |
---|
| 978 | |
---|
| 979 | error_t vmm_update_shared_page( vseg_t *vseg, vma_t vaddr, ppn_t ppn) |
---|
| 980 | { |
---|
| 981 | pmm_page_info_t current; |
---|
| 982 | error_t err; |
---|
| 983 | |
---|
| 984 | error= pmm_get_page(&vseg->vmm->pmm, vaddr, ¤t); |
---|
| 985 | |
---|
| 986 | if((err) || (current.attr != 0)) |
---|
| 987 | goto ended; |
---|
| 988 | |
---|
| 989 | current.ppn = ppn; |
---|
| 990 | current.attr = vseg->vm_pgprot; |
---|
| 991 | current.cluster = NULL; // this function is called after invalidate one |
---|
| 992 | |
---|
| 993 | error= pmm_set_page(&vseg->vmm->pmm, vaddr , ¤t); |
---|
| 994 | |
---|
| 995 | ended: |
---|
| 996 | return err; |
---|
| 997 | } |
---|
| 998 | |
---|
| 999 | // Hypothesis: the vseg is shared-anon, mapper list is rdlocked, page is locked |
---|
| 1000 | error_t vmm_migrate_shared_page_seq( vseg_t *vseg, struct page_s *page, struct page_s **new) |
---|
| 1001 | { |
---|
| 1002 | register vseg_t *reg; |
---|
| 1003 | register struct process_s *process; |
---|
| 1004 | register struct process_s *this_process; |
---|
| 1005 | struct page_s *new_pg; |
---|
| 1006 | struct list_entry *iter; |
---|
| 1007 | kmem_req_t req; |
---|
| 1008 | vma_t vaddr; |
---|
| 1009 | ppn_t ppn; |
---|
| 1010 | error_t err; |
---|
| 1011 | |
---|
| 1012 | vaddr = (page->index << PMM_PAGE_SHIFT) + vseg->vm_start + vseg->vm_offset; |
---|
| 1013 | ppn = ppm_page2ppn(page); |
---|
| 1014 | this_process = (new == NULL) ? NULL : current_process; |
---|
| 1015 | iter = &vseg->vm_shared_list; |
---|
| 1016 | error = ECANCELED; |
---|
| 1017 | |
---|
| 1018 | // Invalidate All |
---|
| 1019 | do |
---|
| 1020 | { |
---|
| 1021 | reg = list_element(iter, vseg_t, vm_shared_list); |
---|
| 1022 | |
---|
| 1023 | process = vmm_get_process(reg->vmm); |
---|
| 1024 | |
---|
| 1025 | if(process != this_process) |
---|
| 1026 | { |
---|
| 1027 | error= vmm_inval_shared_page(reg, vaddr, ppn); |
---|
| 1028 | |
---|
| 1029 | if(err) goto fail_inval; |
---|
| 1030 | } |
---|
| 1031 | |
---|
| 1032 | assert(vseg->vm_mapper.m_home_cid == current_cid); |
---|
| 1033 | iter = list_next(&vseg->vm_mapper.m_reg_root, iter); |
---|
| 1034 | |
---|
| 1035 | }while(iter != NULL); |
---|
| 1036 | |
---|
| 1037 | req.type = KMEM_PAGE; |
---|
| 1038 | req.size = 0; |
---|
| 1039 | req.excep_code = AF_USER; |
---|
| 1040 | |
---|
| 1041 | new_pg = kmem_alloc(&req); |
---|
| 1042 | *new = new_pg; |
---|
| 1043 | |
---|
| 1044 | if(new_pg == NULL) |
---|
| 1045 | { |
---|
| 1046 | error= ENOMEM; |
---|
| 1047 | goto fail_alloc; |
---|
| 1048 | } |
---|
| 1049 | |
---|
| 1050 | page_copy(new_pg, page); |
---|
| 1051 | |
---|
| 1052 | page_lock(new_pg); |
---|
| 1053 | |
---|
| 1054 | new_pg->mapper = page->mapper; |
---|
| 1055 | new_pg->index = page->index; |
---|
| 1056 | |
---|
| 1057 | // TODO: do the complet job regading dirty page |
---|
| 1058 | if(PAGE_IS(page, PG_DIRTY)) |
---|
| 1059 | PAGE_SET(new_pg, PG_DIRTY); |
---|
| 1060 | |
---|
| 1061 | ppn = ppm_page2ppn(new_pg); |
---|
| 1062 | iter = &vseg->vm_shared_list; |
---|
| 1063 | |
---|
| 1064 | // Update All |
---|
| 1065 | do |
---|
| 1066 | { |
---|
| 1067 | reg = list_element(iter, vseg_t, vm_shared_list); |
---|
| 1068 | |
---|
| 1069 | process = vmm_get_process(reg->vmm); |
---|
| 1070 | |
---|
| 1071 | if(process != this_process) |
---|
| 1072 | (void) vmm_update_shared_page(reg, vaddr, ppn); |
---|
| 1073 | |
---|
| 1074 | assert(vseg->vm_mapper.m_home_cid == current_cid); |
---|
| 1075 | iter = list_next(&vseg->vm_mapper.m_reg_root, iter); |
---|
| 1076 | |
---|
| 1077 | |
---|
| 1078 | }while(iter != NULL); |
---|
| 1079 | |
---|
| 1080 | page_unlock(new_pg); |
---|
| 1081 | |
---|
| 1082 | fail_alloc: |
---|
| 1083 | fail_inval: |
---|
| 1084 | return err; |
---|
| 1085 | } |
---|
| 1086 | |
---|
| 1087 | //TODO: revisit all manipulation of the page->refcount |
---|
| 1088 | /////////////////////////////////////////////////////////////// |
---|
| 1089 | static inline error_t vmm_do_migrate( vseg_t * vseg, |
---|
| 1090 | pmm_page_info_t * pinfo, |
---|
| 1091 | uint32_t vaddr ) |
---|
| 1092 | { |
---|
| 1093 | kmem_req_t req; |
---|
| 1094 | pmm_page_info_t current; |
---|
| 1095 | page_t * newpage; |
---|
| 1096 | cluster_t * cluster; |
---|
| 1097 | thread_t * this; |
---|
| 1098 | error_t err; |
---|
| 1099 | ppn_t ppn; |
---|
| 1100 | |
---|
| 1101 | assert( pinfo->ppn != 0 ); |
---|
| 1102 | |
---|
| 1103 | ppn = pinfo->ppn; |
---|
| 1104 | this = current_thread; |
---|
| 1105 | newpage = NULL; |
---|
| 1106 | cluster = current_cluster; |
---|
| 1107 | |
---|
| 1108 | current.attr = 0; |
---|
| 1109 | current.ppn = 0; |
---|
| 1110 | |
---|
| 1111 | error= pmm_lock_page(&vseg->vmm->pmm, vaddr, ¤t); |
---|
| 1112 | |
---|
| 1113 | if(error|| (current.isAtomic == false) || |
---|
| 1114 | (current.ppn != ppn) || !(current.attr & PMM_MIGRATE)) |
---|
| 1115 | { |
---|
| 1116 | #if CONFIG_SHOW_SPURIOUS_PGFAULT |
---|
| 1117 | printk(INFO, "%s: pid %d, tid %d, cpu %d, nothing to do for vaddr %x\n", |
---|
| 1118 | __FUNCTION__, |
---|
| 1119 | this->process->pid, |
---|
| 1120 | this->info.order, |
---|
| 1121 | cpu_get_id(), |
---|
| 1122 | vaddr); |
---|
| 1123 | #endif |
---|
| 1124 | this->info.spurious_pgfault_cntr ++; |
---|
| 1125 | pmm_unlock_page(&vseg->vmm->pmm, vaddr, ¤t); |
---|
| 1126 | pmm_tlb_flush_vaddr(vaddr, PMM_DATA); |
---|
| 1127 | return 0; |
---|
| 1128 | } |
---|
| 1129 | |
---|
| 1130 | if(!ppn_is_local(ppn)) |
---|
| 1131 | { |
---|
| 1132 | req.type = KMEM_PAGE; |
---|
| 1133 | req.size = 0; |
---|
| 1134 | req.excep_code = AF_PGFAULT; |
---|
| 1135 | |
---|
| 1136 | newpage = kmem_alloc(&req); |
---|
| 1137 | |
---|
| 1138 | if(newpage) |
---|
| 1139 | { |
---|
| 1140 | newpage->mapper = NULL;//? |
---|
| 1141 | ppn_copy(ppm_page2ppn(newpage), ppn); |
---|
| 1142 | |
---|
| 1143 | if(current.attr & PMM_COW) |
---|
| 1144 | { |
---|
| 1145 | current.attr |= PMM_WRITE; |
---|
| 1146 | current.attr &= ~(PMM_COW); |
---|
| 1147 | } |
---|
| 1148 | |
---|
| 1149 | current.ppn = ppm_page2ppn(newpage); |
---|
| 1150 | } |
---|
| 1151 | } |
---|
| 1152 | |
---|
| 1153 | current.attr |= PMM_PRESENT; |
---|
| 1154 | current.attr &= ~(PMM_MIGRATE); |
---|
| 1155 | current.attr &= ~(PMM_LOCKED); |
---|
| 1156 | current.cluster = NULL; |
---|
| 1157 | |
---|
| 1158 | //also unlock the table entry |
---|
| 1159 | error= pmm_set_page(&vseg->vmm->pmm, vaddr, ¤t); |
---|
| 1160 | |
---|
| 1161 | if(err) |
---|
| 1162 | { |
---|
| 1163 | // TODO: we should differ the kmem_free call |
---|
| 1164 | //page_unlock(page); |
---|
| 1165 | (void)pmm_unlock_page(&vseg->vmm->pmm, vaddr, ¤t); |
---|
| 1166 | req.ptr = newpage; |
---|
| 1167 | kmem_free(&req); |
---|
| 1168 | return err; |
---|
| 1169 | } |
---|
| 1170 | |
---|
| 1171 | |
---|
| 1172 | if(newpage) |
---|
| 1173 | { |
---|
| 1174 | ppn_refcount_down(ppn); |
---|
| 1175 | current_thread->info.remote_pages_cntr ++; |
---|
| 1176 | #if CONFIG_SHOW_REMOTE_PGALLOC |
---|
| 1177 | printk(INFO, "%s: pid %d, tid %x, cpu %d, cid %d: got new remote page from cluster %d (vaddr %x)\n", |
---|
| 1178 | __FUNCTION__, |
---|
| 1179 | current_process->pid, |
---|
| 1180 | current_thread, |
---|
| 1181 | cpu_get_id(), |
---|
| 1182 | cluster->id, |
---|
| 1183 | newpage->cid, |
---|
| 1184 | vaddr); |
---|
| 1185 | #endif |
---|
| 1186 | } |
---|
| 1187 | |
---|
| 1188 | #if CONFIG_SHOW_VMMMGRT_MSG |
---|
| 1189 | printk(INFO, "%s: pid %d, tid %d, cpu %d: Asked to migrate page (vaddr %x) from cluster %d to cluster %d, error%d\n", |
---|
| 1190 | __FUNCTION__, |
---|
| 1191 | current_process->pid, |
---|
| 1192 | current_thread->info.order, |
---|
| 1193 | cpu_get_id(), |
---|
| 1194 | vaddr, |
---|
| 1195 | ppn_ppn2cid(ppn), |
---|
| 1196 | cluster->id, |
---|
| 1197 | err); |
---|
| 1198 | #endif |
---|
| 1199 | |
---|
| 1200 | return err; |
---|
| 1201 | } |
---|
| 1202 | |
---|
| 1203 | error_t vmm_do_cow( vseg_t *vseg, pmm_page_info_t *pinfo, uint32_t vaddr) |
---|
| 1204 | { |
---|
| 1205 | register struct page_s *newpage; |
---|
| 1206 | register struct page_s *page; |
---|
| 1207 | register struct thread_s *this; |
---|
| 1208 | register error_t err; |
---|
| 1209 | register uint32_t count; |
---|
| 1210 | register bool_t isCountDown; |
---|
| 1211 | pmm_page_info_t old; |
---|
| 1212 | pmm_page_info_t new; |
---|
| 1213 | kmem_req_t req; |
---|
| 1214 | |
---|
| 1215 | this = current_thread; |
---|
| 1216 | old.attr = 0; |
---|
| 1217 | newpage = NULL; |
---|
| 1218 | isCountDown = true; |
---|
| 1219 | |
---|
| 1220 | vmm_dmsg(2,"%s: pid %d, tid %d, cpu %d, vaddr %x\n", |
---|
| 1221 | __FUNCTION__, |
---|
| 1222 | this->process->pid, |
---|
| 1223 | this->info.order, |
---|
| 1224 | cpu_get_id(), |
---|
| 1225 | vaddr); |
---|
| 1226 | |
---|
| 1227 | |
---|
| 1228 | error= pmm_lock_page(&vseg->vmm->pmm, vaddr, &old); |
---|
| 1229 | |
---|
| 1230 | //TODO: check this condition |
---|
| 1231 | if(error|| (old.isAtomic == false) || !(old.attr & PMM_COW)) |
---|
| 1232 | { |
---|
| 1233 | #if CONFIG_SHOW_SPURIOUS_PGFAULT |
---|
| 1234 | printk(INFO, "%s: pid %d, tid %d, cpu %d, nothing to do for vaddr %x\n", |
---|
| 1235 | __FUNCTION__, |
---|
| 1236 | this->process->pid, |
---|
| 1237 | this->info.order, |
---|
| 1238 | cpu_get_id(), |
---|
| 1239 | vaddr); |
---|
| 1240 | #endif |
---|
| 1241 | this->info.spurious_pgfault_cntr ++; |
---|
| 1242 | pmm_tlb_flush_vaddr(vaddr, PMM_DATA); |
---|
| 1243 | pmm_unlock_page(&vseg->vmm->pmm, vaddr, &old); |
---|
| 1244 | return err; |
---|
| 1245 | //goto VMM_COW_END; |
---|
| 1246 | } |
---|
| 1247 | |
---|
| 1248 | //if the ppn is local and the others (processus with wich we share the page) |
---|
| 1249 | //has done cow, then use the old.ppn directly |
---|
| 1250 | if(ppn_is_local(old.ppn)) |
---|
| 1251 | { |
---|
| 1252 | page = ppm_ppn2page(¤t_cluster->ppm, old.ppn); |
---|
| 1253 | |
---|
| 1254 | if(page->mapper == NULL) |
---|
| 1255 | { |
---|
| 1256 | count = page_refcount_get(page); |
---|
| 1257 | if(count == 1) |
---|
| 1258 | { |
---|
| 1259 | newpage = page;//don't copy the page. use it directly. |
---|
| 1260 | isCountDown = false; |
---|
| 1261 | vmm_dmsg(2, "%s: pid %d, tid %d, cpu %d, reuse same page for vaddr %x, pg_addr %x\n", |
---|
| 1262 | __FUNCTION__, |
---|
| 1263 | this->process->pid, |
---|
| 1264 | this->info.order, |
---|
| 1265 | cpu_get_id(), |
---|
| 1266 | vaddr, |
---|
| 1267 | ppm_page2addr(page)); |
---|
| 1268 | } |
---|
| 1269 | } |
---|
| 1270 | //else: we need to do the cow even if it's local! |
---|
| 1271 | |
---|
| 1272 | } |
---|
| 1273 | |
---|
| 1274 | //else: alocate newpage and copy the data from the remote node |
---|
| 1275 | //also defcount down the ppn |
---|
| 1276 | if(newpage == NULL) |
---|
| 1277 | { |
---|
| 1278 | req.type = KMEM_PAGE; |
---|
| 1279 | req.size = 0; |
---|
| 1280 | req.excep_code = AF_PGFAULT; |
---|
| 1281 | |
---|
| 1282 | if((newpage = kmem_alloc(&req)) == NULL) |
---|
| 1283 | { |
---|
| 1284 | (void)pmm_unlock_page(&vseg->vmm->pmm, vaddr, &old); |
---|
| 1285 | return ENOMEM; |
---|
| 1286 | } |
---|
| 1287 | |
---|
| 1288 | newpage->mapper = NULL; |
---|
| 1289 | |
---|
| 1290 | ppn_copy(ppm_page2ppn(newpage), old.ppn); |
---|
| 1291 | assert(isCountDown); |
---|
| 1292 | |
---|
| 1293 | vmm_dmsg(2, |
---|
| 1294 | "%s: pid %d, tid %d, cpu %d, newpage for vaddr %x, pg_addr %x\n", |
---|
| 1295 | __FUNCTION__, |
---|
| 1296 | this->process->pid, |
---|
| 1297 | this->info.order, |
---|
| 1298 | cpu_get_id(), |
---|
| 1299 | vaddr, |
---|
| 1300 | ppm_page2addr(newpage)); |
---|
| 1301 | |
---|
| 1302 | if(newpage->cid != current_cid) |
---|
| 1303 | this->info.remote_pages_cntr ++; |
---|
| 1304 | } |
---|
| 1305 | |
---|
| 1306 | new.attr = vseg->vm_pgprot | PMM_WRITE; |
---|
| 1307 | new.attr &= ~(PMM_COW | PMM_MIGRATE); |
---|
| 1308 | new.ppn = ppm_page2ppn(newpage); |
---|
| 1309 | new.cluster = NULL; |
---|
| 1310 | |
---|
| 1311 | //this also unlock the table entry (if no error) |
---|
| 1312 | error= pmm_set_page(&vseg->vmm->pmm, vaddr, &new); |
---|
| 1313 | |
---|
| 1314 | if(err) |
---|
| 1315 | { |
---|
| 1316 | (void)pmm_unlock_page(&vseg->vmm->pmm, vaddr, &old); |
---|
| 1317 | req.ptr = newpage; |
---|
| 1318 | kmem_free(&req); |
---|
| 1319 | vmm_dmsg(3, "%s: ended [ error%d ]\n", __FUNCTION__, err); |
---|
| 1320 | return err; |
---|
| 1321 | } |
---|
| 1322 | |
---|
| 1323 | if(isCountDown) ppn_refcount_down(old.ppn); |
---|
| 1324 | |
---|
| 1325 | vmm_dmsg(2, "%s, pid %d, tid %d, cpu %d, COW ended [vaddr %x]\n", |
---|
| 1326 | __FUNCTION__, |
---|
| 1327 | this->process->pid, |
---|
| 1328 | this->info.order, |
---|
| 1329 | cpu_get_id(), |
---|
| 1330 | vaddr); |
---|
| 1331 | |
---|
| 1332 | return 0; |
---|
| 1333 | } |
---|
| 1334 | |
---|
| 1335 | |
---|
| 1336 | //refcount is taken on the file at mmap |
---|
| 1337 | static inline error_t vmm_do_mapped( vseg_t *vseg, uint32_t vaddr, uint32_t excep_code) |
---|
| 1338 | { |
---|
| 1339 | ppn_t ppn; |
---|
| 1340 | error_t err; |
---|
| 1341 | uint32_t index; |
---|
| 1342 | bool_t isDone; |
---|
| 1343 | pmm_page_info_t info; |
---|
| 1344 | pmm_page_info_t current; |
---|
| 1345 | struct thread_s *this; |
---|
| 1346 | |
---|
| 1347 | this = current_thread; |
---|
| 1348 | |
---|
| 1349 | current.attr = 1; |
---|
| 1350 | current.ppn = 1; |
---|
| 1351 | isDone = false; |
---|
| 1352 | |
---|
| 1353 | error= pmm_lock_page(&vseg->vmm->pmm, vaddr, ¤t); |
---|
| 1354 | |
---|
| 1355 | if(err) return err; |
---|
| 1356 | |
---|
| 1357 | if((current.isAtomic == false) || (current.attr != 0)) |
---|
| 1358 | { |
---|
| 1359 | #if CONFIG_SHOW_SPURIOUS_PGFAULT |
---|
| 1360 | printk(INFO, "%s: pid %d, tid %d, cpu %d, nothing to do for vaddr %x\n", |
---|
| 1361 | __FUNCTION__, |
---|
| 1362 | this->process->pid, |
---|
| 1363 | this->info.order, |
---|
| 1364 | cpu_get_id(), |
---|
| 1365 | vaddr); |
---|
| 1366 | #endif |
---|
| 1367 | this->info.spurious_pgfault_cntr ++; |
---|
| 1368 | pmm_tlb_flush_vaddr(vaddr, PMM_DATA); |
---|
| 1369 | return 0; |
---|
| 1370 | } |
---|
| 1371 | |
---|
| 1372 | index = ((vaddr - vseg->vm_start) + vseg->vm_offset) >> PMM_PAGE_SHIFT; |
---|
| 1373 | |
---|
| 1374 | //also hold a refcount! |
---|
| 1375 | ppn = mapper_get_ppn(&vseg->vm_mapper, |
---|
| 1376 | index, |
---|
| 1377 | MAPPER_SYNC_OP); |
---|
| 1378 | |
---|
| 1379 | if(!ppn) |
---|
| 1380 | { |
---|
| 1381 | error= pmm_unlock_page(&vseg->vmm->pmm, vaddr, ¤t); |
---|
| 1382 | assert(!err); //FIXME: liberate the ppn ... |
---|
| 1383 | return (VFS_FILE_IS_NULL(vseg->vm_file)) ? EIO : ENOMEM; |
---|
| 1384 | } |
---|
| 1385 | |
---|
| 1386 | info.attr = vseg->vm_pgprot; |
---|
| 1387 | info.ppn = ppn; |
---|
| 1388 | info.cluster = NULL; |
---|
| 1389 | |
---|
| 1390 | //also unlock the page |
---|
| 1391 | error= pmm_set_page(&vseg->vmm->pmm, vaddr, &info); |
---|
| 1392 | |
---|
| 1393 | assert(!err);//FIXME: liberate the ppn and unlock the table entry ... |
---|
| 1394 | //error= pmm_unlock_page(&vseg->vmm->pmm, vaddr, ¤t); |
---|
| 1395 | |
---|
| 1396 | return err; |
---|
| 1397 | } |
---|
| 1398 | |
---|
| 1399 | ///////////////////////////////////////////////////// |
---|
| 1400 | static inline error_t vmm_do_aod( vseg_t *vseg, uint32_t vaddr) |
---|
| 1401 | { |
---|
| 1402 | register error_t err; |
---|
| 1403 | register struct page_s *page; |
---|
| 1404 | register struct cluster_s *cluster; |
---|
| 1405 | struct thread_s *this; |
---|
| 1406 | pmm_page_info_t old; |
---|
| 1407 | pmm_page_info_t new; |
---|
| 1408 | kmem_req_t req; |
---|
| 1409 | |
---|
| 1410 | page = NULL; |
---|
| 1411 | old.attr = 0; |
---|
| 1412 | this = current_thread; |
---|
| 1413 | |
---|
| 1414 | error= pmm_lock_page(&vseg->vmm->pmm, vaddr, &old); |
---|
| 1415 | |
---|
| 1416 | if(err) return err; |
---|
| 1417 | |
---|
| 1418 | if(old.isAtomic == false) |
---|
| 1419 | { |
---|
| 1420 | this->info.spurious_pgfault_cntr ++; |
---|
| 1421 | pmm_tlb_flush_vaddr(vaddr, PMM_DATA); |
---|
| 1422 | return 0; |
---|
| 1423 | } |
---|
| 1424 | |
---|
| 1425 | req.type = KMEM_PAGE; |
---|
| 1426 | req.size = 0; |
---|
| 1427 | req.excep_code = AF_PGFAULT | AF_ZERO; |
---|
| 1428 | |
---|
| 1429 | if((page = kmem_alloc(&req)) == NULL) |
---|
| 1430 | { |
---|
| 1431 | (void)pmm_unlock_page(&vseg->vmm->pmm, vaddr, &old); |
---|
| 1432 | return ENOMEM; |
---|
| 1433 | } |
---|
| 1434 | |
---|
| 1435 | page->mapper = NULL; |
---|
| 1436 | |
---|
| 1437 | new.attr = vseg->vm_pgprot; |
---|
| 1438 | new.ppn = ppm_page2ppn(page); |
---|
| 1439 | new.cluster = NULL; |
---|
| 1440 | |
---|
| 1441 | error= pmm_set_page(&vseg->vmm->pmm, vaddr, &new); |
---|
| 1442 | |
---|
| 1443 | if(err) goto fail_set_pg; |
---|
| 1444 | |
---|
| 1445 | cluster = current_cluster; |
---|
| 1446 | |
---|
| 1447 | if(page->cid != cluster->id) |
---|
| 1448 | this->info.remote_pages_cntr ++; |
---|
| 1449 | |
---|
| 1450 | return 0; |
---|
| 1451 | |
---|
| 1452 | fail_set_pg: |
---|
| 1453 | (void)pmm_unlock_page(&vseg->vmm->pmm, vaddr, &old); |
---|
| 1454 | req.ptr = page; |
---|
| 1455 | kmem_free(&req); |
---|
| 1456 | |
---|
| 1457 | vmm_dmsg(3, "%s: ended [ error%d ]\n", __FUNCTION__, err); |
---|
| 1458 | return err; |
---|
| 1459 | } |
---|
| 1460 | |
---|
| 1461 | VSEGION_PAGE_FAULT(vmm_default_pagefault) |
---|
| 1462 | { |
---|
| 1463 | register struct thread_s *this; |
---|
| 1464 | register error_t err; |
---|
| 1465 | pmm_page_info_t info; |
---|
| 1466 | |
---|
| 1467 | if((error= pmm_get_page(&vseg->vmm->pmm, vaddr, &info))) |
---|
| 1468 | return err; |
---|
| 1469 | |
---|
| 1470 | if((info.attr != 0) && (info.ppn != 0)) |
---|
| 1471 | { |
---|
| 1472 | if((info.attr & PMM_COW) && pmm_except_isWrite(excep_code)) |
---|
| 1473 | { |
---|
| 1474 | error= vmm_do_cow(vseg, &info, vaddr); |
---|
| 1475 | return err; |
---|
| 1476 | } |
---|
| 1477 | |
---|
| 1478 | if(info.attr & PMM_MIGRATE) |
---|
| 1479 | return vmm_do_migrate(vseg, &info, vaddr); |
---|
| 1480 | |
---|
| 1481 | if(info.attr & PMM_PRESENT) |
---|
| 1482 | { |
---|
| 1483 | this = current_thread; |
---|
| 1484 | |
---|
| 1485 | #if CONFIG_SHOW_SPURIOUS_PGFAULT |
---|
| 1486 | printk(WARNING, "WARNING: %s: pid %d, tid %d, cpu %d, excep_code %x but vaddr is valid %x, attr %x, ppn %x\n", |
---|
| 1487 | __FUNCTION__, |
---|
| 1488 | this->process->pid, |
---|
| 1489 | this->info.order, |
---|
| 1490 | cpu_get_id(), |
---|
| 1491 | excep_code, |
---|
| 1492 | vaddr, |
---|
| 1493 | info.attr, |
---|
| 1494 | info.ppn); |
---|
| 1495 | #endif |
---|
| 1496 | |
---|
| 1497 | current_thread->info.spurious_pgfault_cntr ++; |
---|
| 1498 | pmm_tlb_flush_vaddr(vaddr, PMM_UNKNOWN); |
---|
| 1499 | return 0; |
---|
| 1500 | } |
---|
| 1501 | |
---|
| 1502 | current_thread->info.spurious_pgfault_cntr ++; |
---|
| 1503 | pmm_tlb_flush_vaddr(vaddr, PMM_UNKNOWN); |
---|
| 1504 | return 0; |
---|
| 1505 | #if 0 |
---|
| 1506 | #if CONFIG_SHOW_VMM_ERROR_MSG |
---|
| 1507 | printk(ERROR, |
---|
| 1508 | "ERROR: %s: pid %d, cpu %d, Unexpected page attributes configuration for vaddr %x, found: ppn %x, attr %x\n", |
---|
| 1509 | __FUNCTION__, |
---|
| 1510 | current_process->pid, |
---|
| 1511 | cpu_get_id(), |
---|
| 1512 | vaddr, |
---|
| 1513 | info.ppn, |
---|
| 1514 | info.attr); |
---|
| 1515 | #endif |
---|
| 1516 | |
---|
| 1517 | return EPERM; |
---|
| 1518 | #endif |
---|
| 1519 | } |
---|
| 1520 | |
---|
| 1521 | if(!MAPPER_IS_NULL(vseg->vm_mapper)) |
---|
| 1522 | return vmm_do_mapped(vseg, vaddr, excep_code); |
---|
| 1523 | |
---|
| 1524 | return vmm_do_aod(vseg, vaddr); |
---|
| 1525 | } |
---|
| 1526 | |
---|
| 1527 | */ |
---|
| 1528 | |
---|
| 1529 | |
---|