[411] | 1 | /* |
---|
[587] | 2 | * hal_vmm.c - Virtual Memory Manager Initialisation for TSAR |
---|
[411] | 3 | * |
---|
[623] | 4 | * Authors Alain Greiner (2016,2017,2018,2019) |
---|
[411] | 5 | * |
---|
| 6 | * Copyright (c) UPMC Sorbonne Universites |
---|
| 7 | * |
---|
| 8 | * This file is part of ALMOS-MKH. |
---|
| 9 | * |
---|
| 10 | * ALMOS-MKH is free software; you can redistribute it and/or modify it |
---|
| 11 | * under the terms of the GNU General Public License as published by |
---|
| 12 | * the Free Software Foundation; version 2.0 of the License. |
---|
| 13 | * |
---|
| 14 | * ALMOS-MKH is distributed in the hope that it will be useful, but |
---|
| 15 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
| 17 | * General Public License for more details. |
---|
| 18 | * |
---|
| 19 | * You should have received a copy of the GNU General Public License |
---|
| 20 | * along with ALMOS-MKH; if not, write to the Free Software Foundation, |
---|
| 21 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
| 22 | */ |
---|
| 23 | |
---|
| 24 | #include <kernel_config.h> |
---|
[457] | 25 | #include <hal_kernel_types.h> |
---|
[411] | 26 | #include <hal_vmm.h> |
---|
| 27 | #include <hal_gpt.h> |
---|
[623] | 28 | #include <process.h> |
---|
[624] | 29 | #include <thread.h> |
---|
[411] | 30 | #include <vseg.h> |
---|
| 31 | #include <xlist.h> |
---|
| 32 | #include <vmm.h> |
---|
| 33 | #include <remote_rwlock.h> |
---|
| 34 | |
---|
| 35 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
[623] | 36 | // This file contains the TSAR specific code used to initialize the kernel process VMM, |
---|
| 37 | // or to update an user process VMM with informations related to the kernel vsegs. |
---|
| 38 | // As the TSAR architure does not use the DATA MMU, but use only the DATA extension |
---|
| 39 | // address register to access local and remote kernel data, the kernel VSL contains only |
---|
| 40 | // one "kcode" segment, and the kernel GPT contains only one big page in PT1[0] slot. |
---|
[411] | 41 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 42 | |
---|
[623] | 43 | // extern global variables |
---|
[624] | 44 | extern process_t process_zero; |
---|
| 45 | extern chdev_directory_t chdev_dir; |
---|
[635] | 46 | extern char * lock_type_str[]; |
---|
[623] | 47 | |
---|
| 48 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 49 | // This function is called by the process_zero_init() function during kernel_init. |
---|
| 50 | // It initializes the VMM of the kernel proces_zero (containing all kernel threads) |
---|
[635] | 51 | // in the local cluster. |
---|
| 52 | // For TSAR, it registers one "kcode" vseg in kernel VSL, and registers one big page |
---|
| 53 | // in slot[0] of kernel GPT. |
---|
[623] | 54 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 55 | error_t hal_vmm_kernel_init( boot_info_t * info ) |
---|
[411] | 56 | { |
---|
[623] | 57 | error_t error; |
---|
[411] | 58 | |
---|
[623] | 59 | // get pointer on kernel GPT |
---|
| 60 | gpt_t * gpt = &process_zero.vmm.gpt; |
---|
| 61 | |
---|
[635] | 62 | #if DEBUG_HAL_VMM |
---|
| 63 | thread_t * this = CURRENT_THREAD; |
---|
| 64 | printk("\n[%s] thread[%x,%x] enter in cluster %x\n", |
---|
| 65 | __FUNCTION__, this->process->pid, this->trdid, local_cxy ); |
---|
| 66 | #endif |
---|
[623] | 67 | |
---|
| 68 | // allocate memory for kernel GPT |
---|
| 69 | error = hal_gpt_create( gpt ); |
---|
| 70 | |
---|
| 71 | if( error ) |
---|
[411] | 72 | { |
---|
[623] | 73 | printk("\n[PANIC] in %s : cannot allocate kernel GPT in cluster %x\n", |
---|
[635] | 74 | __FUNCTION__ , local_cxy ); |
---|
[623] | 75 | hal_core_sleep(); |
---|
| 76 | } |
---|
[411] | 77 | |
---|
[624] | 78 | #if DEBUG_HAL_VMM |
---|
[635] | 79 | printk("\n[%s] thread[%x,%x] created GPT PT1 in cluster %x / gpt %x\n", |
---|
[624] | 80 | __FUNCTION__, this->process->pid, this->trdid, local_cxy, gpt ); |
---|
| 81 | #endif |
---|
| 82 | |
---|
[623] | 83 | // compute attr and ppn for one PTE1 |
---|
[624] | 84 | uint32_t attr = GPT_MAPPED | GPT_READABLE | GPT_CACHABLE | GPT_EXECUTABLE | GPT_GLOBAL; |
---|
[635] | 85 | uint32_t ppn = local_cxy << 20; |
---|
[623] | 86 | |
---|
[629] | 87 | // set PT1[0] |
---|
[635] | 88 | hal_gpt_set_pte( XPTR( local_cxy , gpt ) , 0 , attr , ppn ); |
---|
[623] | 89 | |
---|
[624] | 90 | #if DEBUG_HAL_VMM |
---|
[635] | 91 | printk("\n[%s] thread[%x,%x] mapped PT1[0] in cluster %d : ppn %x / attr %x\n", |
---|
| 92 | __FUNCTION__, this->process->pid, this->trdid, local_cxy, ppn, attr ); |
---|
[624] | 93 | #endif |
---|
| 94 | |
---|
[623] | 95 | // create kcode vseg and register it in kernel VSL |
---|
| 96 | vseg_t * vseg = vmm_create_vseg( &process_zero, |
---|
[624] | 97 | VSEG_TYPE_KCODE, |
---|
[623] | 98 | info->kcode_base, |
---|
| 99 | info->kcode_size, |
---|
[635] | 100 | 0, 0, // file ofset and file size (unused) |
---|
| 101 | XPTR_NULL, // no mapper |
---|
[623] | 102 | local_cxy ); |
---|
| 103 | if( vseg == NULL ) |
---|
| 104 | { |
---|
| 105 | printk("\n[PANIC] in %s : cannot register vseg to VSL in cluster %x\n", |
---|
[635] | 106 | __FUNCTION__ , local_cxy ); |
---|
[623] | 107 | hal_core_sleep(); |
---|
| 108 | } |
---|
| 109 | |
---|
[624] | 110 | #if DEBUG_HAL_VMM |
---|
[635] | 111 | printk("\n[%s] thread[%x,%x] registered kcode vseg[%x,%x] in cluster %x\n", |
---|
| 112 | __FUNCTION__, this->process->pid, this->trdid, info->kcode_base, info->kcode_size, local_cxy ); |
---|
[637] | 113 | hal_vmm_display( XPTR( local_cxy, &process_zero ) , true ); |
---|
[624] | 114 | #endif |
---|
[623] | 115 | |
---|
[624] | 116 | return 0; |
---|
| 117 | |
---|
[625] | 118 | } // end hal_vmm_kernel_init() |
---|
[624] | 119 | |
---|
[623] | 120 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
[625] | 121 | // This function registers in the VMM of an user process identified by the <process> |
---|
| 122 | // argument all required kernel vsegs. |
---|
| 123 | // For TSAR, it registers in the user VSL the "kcode" vseg, from the local kernel VSL, |
---|
| 124 | // and register in the user GPT the big page[0] from the local kernel GPT. |
---|
[623] | 125 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 126 | error_t hal_vmm_kernel_update( process_t * process ) |
---|
| 127 | { |
---|
| 128 | uint32_t attr; |
---|
| 129 | uint32_t ppn; |
---|
| 130 | |
---|
[625] | 131 | // get cluster identifier |
---|
| 132 | cxy_t cxy = local_cxy; |
---|
| 133 | |
---|
[624] | 134 | #if DEBUG_HAL_VMM |
---|
| 135 | thread_t * this = CURRENT_THREAD; |
---|
| 136 | printk("\n[%s] thread[%x,%x] enter in cluster %x \n", |
---|
[625] | 137 | __FUNCTION__, this->process->pid, this->trdid, cxy ); |
---|
[637] | 138 | hal_vmm_display( XPTR( local_cxy , process ) , true ); |
---|
[624] | 139 | #endif |
---|
[623] | 140 | |
---|
[625] | 141 | // get extended pointer on local kernel GPT |
---|
[624] | 142 | xptr_t k_gpt_xp = XPTR( cxy , &process_zero.vmm.gpt ); |
---|
| 143 | |
---|
| 144 | // get ppn and attributes from slot[0] of kernel GPT |
---|
| 145 | hal_gpt_get_pte( k_gpt_xp , 0 , &attr , &ppn ); |
---|
| 146 | |
---|
| 147 | #if DEBUG_HAL_VMM |
---|
| 148 | printk("\n[%s] thread[%x,%x] get PT1[0] ( ppn %x / attr %x ) from kernel GPT\n", |
---|
| 149 | __FUNCTION__, this->process->pid, this->trdid, ppn, attr ); |
---|
| 150 | #endif |
---|
| 151 | |
---|
[623] | 152 | // get extended pointer on user GPT |
---|
[624] | 153 | xptr_t u_gpt_xp = XPTR( cxy , &process->vmm.gpt ); |
---|
[623] | 154 | |
---|
| 155 | // update user GPT : set PTE1 in slot[0] |
---|
[629] | 156 | hal_gpt_set_pte( u_gpt_xp , 0 , attr , ppn ); |
---|
[623] | 157 | |
---|
[624] | 158 | #if DEBUG_HAL_VMM |
---|
| 159 | printk("\n[%s] thread[%x,%x] registered PT1[0] ( ppn %x / attr %x ) to user GPT\n", |
---|
| 160 | __FUNCTION__, this->process->pid, this->trdid, ppn, attr ); |
---|
| 161 | #endif |
---|
| 162 | |
---|
[623] | 163 | // get pointer on the unique vseg registered in kernel VSL |
---|
[624] | 164 | xptr_t root_xp = XPTR( cxy , &process_zero.vmm.vsegs_root ); |
---|
| 165 | xptr_t vseg_xp = XLIST_FIRST( root_xp , vseg_t , xlist ); |
---|
| 166 | vseg_t * vseg = GET_PTR( vseg_xp ); |
---|
[411] | 167 | |
---|
[623] | 168 | // check vsegs_nr |
---|
[624] | 169 | assert( (process_zero.vmm.vsegs_nr == 1 ) , |
---|
| 170 | "bad vsegs number in kernel VSL = %d\n", process_zero.vmm.vsegs_nr ); |
---|
[411] | 171 | |
---|
[623] | 172 | // update user VSL : register one new vseg for kcode |
---|
| 173 | vseg_t * new = vmm_create_vseg( process, |
---|
| 174 | vseg->type, |
---|
| 175 | vseg->min, |
---|
| 176 | vseg->max - vseg->min, |
---|
[633] | 177 | 0, 0, // file ofset and file size (unused) |
---|
| 178 | XPTR_NULL, // no mapper |
---|
[623] | 179 | local_cxy ); |
---|
| 180 | if( new == NULL ) |
---|
| 181 | { |
---|
[624] | 182 | printk("\n[ERROR] in %s : cannot update user VSL in cluster %x\n", |
---|
[623] | 183 | __FUNCTION__ , cxy ); |
---|
| 184 | return -1; |
---|
| 185 | } |
---|
[411] | 186 | |
---|
[624] | 187 | #if DEBUG_HAL_VMM |
---|
| 188 | printk("\n[%s] thread[%x,%x] created vseg %s ( base %x / size %x ) to user VSL\n", |
---|
| 189 | __FUNCTION__, this->process->pid, this->trdid, |
---|
| 190 | vseg_type_str(vseg->type) , vseg->min, (vseg->max - vseg->min) ); |
---|
[637] | 191 | hal_vmm_display( XPTR( local_cxy , process ) , true ); |
---|
[624] | 192 | #endif |
---|
[411] | 193 | |
---|
[624] | 194 | return 0; |
---|
| 195 | |
---|
| 196 | } // end hal_vmm_kernel_update() |
---|
| 197 | |
---|
| 198 | ////////////////////////////////////////// |
---|
[635] | 199 | void hal_vmm_display( xptr_t process_xp, |
---|
| 200 | bool_t mapping ) |
---|
[624] | 201 | { |
---|
[635] | 202 | // get target process cluster and local pointer |
---|
| 203 | process_t * process_ptr = GET_PTR( process_xp ); |
---|
| 204 | cxy_t process_cxy = GET_CXY( process_xp ); |
---|
[624] | 205 | |
---|
[635] | 206 | // get local pointer on target process VMM |
---|
| 207 | vmm_t * vmm = &process_ptr->vmm; |
---|
| 208 | |
---|
[624] | 209 | // get pointers on TXT0 chdev |
---|
| 210 | xptr_t txt0_xp = chdev_dir.txt_tx[0]; |
---|
| 211 | cxy_t txt0_cxy = GET_CXY( txt0_xp ); |
---|
| 212 | chdev_t * txt0_ptr = GET_PTR( txt0_xp ); |
---|
| 213 | |
---|
[635] | 214 | // build extended pointer on TXT0 lock |
---|
[625] | 215 | xptr_t txt_lock_xp = XPTR( txt0_cxy , &txt0_ptr->wait_lock ); |
---|
[624] | 216 | |
---|
[635] | 217 | // build extended pointers on VSL lock and VSL root |
---|
| 218 | xptr_t vsl_root_xp = XPTR( process_cxy , &vmm->vsegs_root ); |
---|
| 219 | xptr_t vsl_lock_xp = XPTR( process_cxy , &vmm->vsl_lock ); |
---|
[624] | 220 | |
---|
[635] | 221 | // get the locks protecting TXT0 and VSL |
---|
[625] | 222 | remote_rwlock_rd_acquire( vsl_lock_xp ); |
---|
| 223 | remote_busylock_acquire( txt_lock_xp ); |
---|
[624] | 224 | |
---|
[635] | 225 | // get PID and PT1 values |
---|
| 226 | pid_t pid = hal_remote_l32( XPTR( process_cxy , &process_ptr->pid ) ); |
---|
| 227 | uint32_t * pt1 = hal_remote_lpt( XPTR( process_cxy , &vmm->gpt.ptr ) ); |
---|
[624] | 228 | |
---|
[635] | 229 | nolock_printk("\n***** VSL and GPT / process %x / cluster %x / PT1 %x / cycle %d\n", |
---|
| 230 | pid , process_cxy , pt1 , (uint32_t)hal_get_cycles() ); |
---|
| 231 | |
---|
| 232 | if( xlist_is_empty( vsl_root_xp ) ) |
---|
[624] | 233 | { |
---|
[625] | 234 | nolock_printk(" ... no vsegs registered\n"); |
---|
| 235 | } |
---|
| 236 | else // scan the list of vsegs |
---|
| 237 | { |
---|
| 238 | xptr_t iter_xp; |
---|
| 239 | xptr_t vseg_xp; |
---|
[635] | 240 | vseg_t * vseg_ptr; |
---|
| 241 | cxy_t vseg_cxy; |
---|
| 242 | intptr_t min; |
---|
| 243 | intptr_t max; |
---|
| 244 | uint32_t type; |
---|
| 245 | intptr_t vpn_base; |
---|
| 246 | intptr_t vpn_size; |
---|
[624] | 247 | |
---|
[635] | 248 | XLIST_FOREACH( vsl_root_xp , iter_xp ) |
---|
[624] | 249 | { |
---|
[635] | 250 | vseg_xp = XLIST_ELEMENT( iter_xp , vseg_t , xlist ); |
---|
| 251 | vseg_ptr = GET_PTR( vseg_xp ); |
---|
| 252 | vseg_cxy = GET_CXY( vseg_xp ); |
---|
[624] | 253 | |
---|
[635] | 254 | type = hal_remote_l32( XPTR( vseg_cxy , &vseg_ptr->type ) ); |
---|
| 255 | min = (intptr_t)hal_remote_lpt( XPTR( vseg_cxy , &vseg_ptr->min ) ); |
---|
| 256 | max = (intptr_t)hal_remote_lpt( XPTR( vseg_cxy , &vseg_ptr->max ) ); |
---|
| 257 | vpn_size = (intptr_t)hal_remote_lpt( XPTR( vseg_cxy , &vseg_ptr->vpn_size ) ); |
---|
| 258 | vpn_base = (intptr_t)hal_remote_lpt( XPTR( vseg_cxy , &vseg_ptr->vpn_base ) ); |
---|
| 259 | |
---|
[625] | 260 | nolock_printk(" - %s : base = %X / size = %X / npages = %d\n", |
---|
[635] | 261 | vseg_type_str(type), min, max - min, vpn_size ); |
---|
[625] | 262 | |
---|
| 263 | if( mapping ) |
---|
[624] | 264 | { |
---|
[635] | 265 | vpn_t vpn = vpn_base; |
---|
| 266 | vpn_t vpn_max = vpn_base + vpn_size; |
---|
[625] | 267 | ppn_t ppn; |
---|
| 268 | uint32_t attr; |
---|
[624] | 269 | |
---|
[625] | 270 | while( vpn < vpn_max ) // scan the PTEs |
---|
[624] | 271 | { |
---|
[635] | 272 | hal_gpt_get_pte( XPTR( process_cxy , &vmm->gpt ) , vpn , &attr , &ppn ); |
---|
[625] | 273 | |
---|
| 274 | if( attr & GPT_MAPPED ) |
---|
[624] | 275 | { |
---|
[625] | 276 | if( attr & GPT_SMALL ) |
---|
| 277 | { |
---|
| 278 | nolock_printk(" . SMALL : vpn = %X / attr = %X / ppn = %X\n", |
---|
| 279 | vpn , attr , ppn ); |
---|
| 280 | vpn++; |
---|
| 281 | } |
---|
| 282 | else |
---|
| 283 | { |
---|
| 284 | nolock_printk(" . BIG : vpn = %X / attr = %X / ppn = %X\n", |
---|
| 285 | vpn , attr , ppn ); |
---|
| 286 | vpn += 512; |
---|
| 287 | } |
---|
[624] | 288 | } |
---|
| 289 | else |
---|
| 290 | { |
---|
[625] | 291 | vpn++; |
---|
[624] | 292 | } |
---|
| 293 | } |
---|
| 294 | } |
---|
| 295 | } |
---|
| 296 | } |
---|
| 297 | |
---|
[640] | 298 | #if CONFIG_INSTRUMENTATION_GPT |
---|
| 299 | uint32_t pte1_events = hal_remote_l32( XPTR( process_cxy , &vmm->gpt.pte1_wait_events ) ); |
---|
| 300 | uint32_t pte1_iters = hal_remote_l32( XPTR( process_cxy , &vmm->gpt.pte1_wait_iters ) ); |
---|
| 301 | uint32_t pte1_ratio = (pte1_events == 0 ) ? 0 : (pte1_iters / pte1_events); |
---|
| 302 | nolock_printk("\nGPT_WAIT_PTE1 : %d events / %d iterations => %d iter/event\n", |
---|
| 303 | pte1_events, pte1_iters, pte1_ratio ); |
---|
| 304 | |
---|
| 305 | uint32_t pte2_events = hal_remote_l32( XPTR( process_cxy , &vmm->gpt.pte1_wait_events ) ); |
---|
| 306 | uint32_t pte2_iters = hal_remote_l32( XPTR( process_cxy , &vmm->gpt.pte1_wait_iters ) ); |
---|
| 307 | uint32_t pte2_ratio = (pte2_events == 0 ) ? 0 : (pte2_iters / pte2_events); |
---|
| 308 | nolock_printk("GPT_WAIT_PTE2 : %d events / %d iterations => %d iter/event\n", |
---|
| 309 | pte2_events, pte2_iters, pte2_ratio ); |
---|
| 310 | #endif |
---|
| 311 | |
---|
[625] | 312 | // release locks |
---|
| 313 | remote_busylock_release( txt_lock_xp ); |
---|
| 314 | remote_rwlock_rd_release( vsl_lock_xp ); |
---|
[624] | 315 | |
---|
| 316 | } // hal_vmm_display() |
---|
| 317 | |
---|
[640] | 318 | |
---|