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