Changeset 406 for trunk/hal/tsar_mips32/core/hal_context.c
- Timestamp:
- Aug 29, 2017, 12:03:37 PM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/hal/tsar_mips32/core/hal_context.c
r317 r406 33 33 #include <cluster.h> 34 34 #include <hal_context.h> 35 #include <hal_kentry.h> 35 36 36 37 ///////////////////////////////////////////////////////////////////////////////////////// … … 38 39 ///////////////////////////////////////////////////////////////////////////////////////// 39 40 40 #define SR_USR_MODE 0x FC1141 #define SR_USR_MODE_FPU 0x2000FC1 142 #define SR_SYS_MODE 0x FC0043 44 ///////////////////////////////////////////////////////////////////////////////////////// 45 // Th ese structuree defines the cpu_context anf fpu_context for TSAR MIPS32.41 #define SR_USR_MODE 0x0000FC13 42 #define SR_USR_MODE_FPU 0x2000FC13 43 #define SR_SYS_MODE 0x0000FC00 44 45 ///////////////////////////////////////////////////////////////////////////////////////// 46 // This structuree defines the cpu_context for TSAR MIPS32. 46 47 // These registers are saved/restored at each context switch. 47 // WARNING : update the hal_***_context_save() and hal_***_context_restore() 48 // functions when modifying this structure, and check the two 49 // CONFIG_CPU_CTX_SIZE & CONFIGFPU_CTX_SIZE configuration parameterss. 48 // WARNING : check the two CONFIG_CPU_CTX_SIZE & CONFIG_FPU_CTX_SIZE configuration 49 // parameterss when modifying this structure. 50 50 ///////////////////////////////////////////////////////////////////////////////////////// 51 51 … … 80 80 81 81 uint32_t t8_24; // slot 24 82 uint32_t t 8_25; // slot 2582 uint32_t t9_25; // slot 25 83 83 uint32_t hi_26; // slot 26 84 84 uint32_t lo_27; // slot 27 … … 107 107 108 108 109 110 109 ///////////////////////////////////////////////////////////////////////////////////////// 111 110 // CPU context access functions 112 111 ///////////////////////////////////////////////////////////////////////////////////////// 113 112 114 115 // ///////////////////////////////////////////////////////////////////////////////////////116 // Seven registers are initialised by this function:117 // GPR : sp_29 / fp_30 / ra_31118 // CP0 : c0_sr / c0_th 113 ///////////////////////////////////////////////////////////////////////////////////////// 114 // This function allocates and initializes the cpu_context stucture in thread descriptor. 115 // The following context slots are initialised by this function: 116 // GPR : a0_04 / sp_29 / fp_30 / ra_31 117 // CP0 : c0_sr / c0_th / c0_epc 119 118 // CP2 : c2_ptpr / c2_mode 120 119 ///////////////////////////////////////////////////////////////////////////////////////// … … 123 122 kmem_req_t req; 124 123 125 context_dmsg("\n[INFO] %s : enters for thread %x in process %x\n", 124 assert( (sizeof(hal_cpu_context_t) <= CONFIG_CPU_CTX_SIZE) , __FUNCTION__ , 125 "inconsistent CPU context size" ); 126 127 context_dmsg("\n[DMSG] %s : enters for thread %x in process %x\n", 126 128 __FUNCTION__ , thread->trdid , thread->process->pid ); 127 129 128 130 // allocate memory for cpu_context 129 131 req.type = KMEM_CPU_CTX; 130 req.size = sizeof(hal_cpu_context_t);131 132 req.flags = AF_KERNEL | AF_ZERO; 132 133 … … 158 159 159 160 // initialise context 161 context->a0_04 = (uint32_t)thread->entry_args; 160 162 context->sp_29 = sp_29; 161 context->fp_30 = sp_29; // TODO check this [AG] 162 context->ra_31 = (uint32_t)thread->entry_func; 163 context->fp_30 = sp_29; // TODO check this [AG] 164 context->ra_31 = (uint32_t)&hal_kentry_eret; 165 context->c0_epc = (uint32_t)thread->entry_func; 163 166 context->c0_sr = c0_sr; 164 167 context->c0_th = (uint32_t)thread; … … 166 169 context->c2_mode = c2_mode; 167 170 168 context_dmsg("\n[INFO] %s : exit for thread %x in process %x / ra = %x\n", 169 __FUNCTION__ , thread->trdid , thread->process->pid , context->ra_31 ); 170 171 context_dmsg("\n[DMSG] %s : exit for thread %x in process %x\n" 172 " - a0 = %x\n" 173 " - sp = %x\n" 174 " - fp = %x\n" 175 " - ra = %x\n" 176 " - sr = %x\n" 177 " - th = %x\n" 178 " - epc = %x\n" 179 " - ptpr = %x\n" 180 " - mode = %x\n", 181 __FUNCTION__ , thread->trdid , thread->process->pid, 182 context->a0_04, context->sp_29, context->fp_30, context->ra_31, 183 context->c0_sr, context->c0_th, context->c0_epc, 184 context->c2_ptpr, context->c2_mode ); 171 185 return 0; 186 172 187 } // end hal_cpu_context_create() 173 188 … … 177 192 hal_cpu_context_t * ctx = (hal_cpu_context_t *)thread->cpu_context; 178 193 179 printk("\n***** cpu_context for thread %x in cluster %x / ctx = %x\n"194 printk("\n***** CPU context for thread %x in process %x / cycle %d\n" 180 195 " gp_28 = %X sp_29 = %X ra_31 = %X\n" 181 196 " c0_sr = %X c0_epc = %X c0_th = %X\n" 182 197 " c2_ptpr = %X c2_mode = %X\n", 183 thread->trdid, local_cxy, ctx,198 thread->trdid, thread->process->pid, hal_time_stamp(), 184 199 ctx->gp_28 , ctx->sp_29 , ctx->ra_31, 185 200 ctx->c0_sr , ctx->c0_epc , ctx->c0_th, … … 188 203 } // end hal_context_display() 189 204 190 /*191 ////////////////////////////////////////////////////////////////////////////////////////192 // This static function makes the actual context switch.193 ////////////////////////////////////////////////////////////////////////////////////////194 static void hal_do_switch( hal_cpu_context_t * ctx_old,195 hal_cpu_context_t * ctx_new )196 {197 asm volatile(198 ".set noat \n"199 ".set noreorder \n"200 "move $26, %0 \n"201 202 "mfc0 $27, $14 \n"203 "sw $27, 0*4($26) \n"204 205 "sw $1, 1*4($26) \n"206 "sw $2, 2*4($26) \n"207 "sw $3, 3*4($26) \n"208 "sw $4, 4*4($26) \n"209 "sw $5, 5*4($26) \n"210 "sw $6, 6*4($26) \n"211 "sw $7, 7*4($26) \n"212 213 "sw $8, 8*4($26) \n"214 "sw $9, 9*4($26) \n"215 "sw $10, 10*4($26) \n"216 "sw $11, 11*4($26) \n"217 "sw $12, 12*4($26) \n"218 "sw $13, 13*4($26) \n"219 "sw $14, 14*4($26) \n"220 "sw $15, 15*4($26) \n"221 222 "sw $16, 16*4($26) \n"223 "sw $17, 17*4($26) \n"224 "sw $18, 18*4($26) \n"225 "sw $19, 19*4($26) \n"226 "sw $20, 20*4($26) \n"227 "sw $21, 21*4($26) \n"228 "sw $22, 22*4($26) \n"229 "sw $23, 23*4($26) \n"230 231 "sw $24, 24*4($26) \n"232 "sw $25, 25*4($26) \n"233 234 "mfhi $27 \n"235 "sw $27, 26*4($26) \n"236 "mflo $27 \n"237 "sw $27, 27*4($26) \n"238 239 "sw $28, 28*4($26) \n"240 "sw $29, 29*4($26) \n"241 "sw $30, 30*4($26) \n"242 "sw $31, 31*4($26) \n"243 244 "mfc2 $27, $0 \n"245 "sw $27, 32*4($26) \n"246 "mfc2 $27, $1 \n"247 "sw $27, 33*4($26) \n"248 249 "mfc0 $27, $12 \n"250 "sw $27, 34*4($26) \n"251 "mfc0 $27, $4, 2 \n"252 "sw $27, 35*4($26) \n"253 254 "sync \n"255 256 "move $26, %1 \n"257 258 "lw $27, 0*4($26) \n"259 "mtc0 $27, $14 \n"260 261 "lw $1, 1*4($26) \n"262 "lw $2, 2*4($26) \n"263 "lw $3, 3*4($26) \n"264 "lw $4, 4*4($26) \n"265 "lw $5, 5*4($26) \n"266 "lw $6, 6*4($26) \n"267 "lw $7, 7*4($26) \n"268 269 "lw $8, 8*4($26) \n"270 "lw $9, 9*4($26) \n"271 "lw $10, 10*4($26) \n"272 "lw $11, 11*4($26) \n"273 "lw $12, 12*4($26) \n"274 "lw $13, 13*4($26) \n"275 "lw $14, 14*4($26) \n"276 "lw $15, 15*4($26) \n"277 278 "lw $16, 16*4($26) \n"279 "lw $17, 17*4($26) \n"280 "lw $18, 18*4($26) \n"281 "lw $19, 19*4($26) \n"282 "lw $20, 20*4($26) \n"283 "lw $21, 21*4($26) \n"284 "lw $22, 22*4($26) \n"285 "lw $23, 23*4($26) \n"286 287 "lw $24, 24*4($26) \n"288 "lw $25, 25*4($26) \n"289 290 "lw $27, 26*4($26) \n"291 "mthi $27 \n"292 "lw $27, 27*4($26) \n"293 "mtlo $27 \n"294 295 "lw $28, 28*4($26) \n"296 "lw $29, 29*4($26) \n"297 "lw $30, 30*4($26) \n"298 "lw $31, 31*4($26) \n"299 300 "lw $27, 32*4($26) \n"301 "mtc2 $27, $0 \n"302 "lw $27, 33*4($26) \n"303 "mtc2 $27, $1 \n"304 305 "lw $27, 34*4($26) \n"306 "mtc0 $27, $12 \n"307 "lw $27, 35*4($26) \n"308 "mtc0 $27, $4, 2 \n"309 310 "jr $31 \n"311 312 ".set reorder \n"313 ".set at \n"314 : : "r"(ctx_old) , "r"(ctx_new) : "$26" , "$27" , "memory" );315 316 } // hal_context_switch()317 318 */319 320 205 ///////////////////////////////////////////////////////////////////////////////////////// 321 206 // These registers are saved/restored to/from CPU context defined by <ctx> argument. 322 207 // - GPR : all, but (zero, k0, k1), plus (hi, lo) 323 // - CP0 : c0_th , c0_sr 324 // - CP2 : c2_ptpr , C2_mode, C2_epc 325 ///////////////////////////////////////////////////////////////////////////////////////// 326 void hal_cpu_context_switch( thread_t * old, 327 thread_t * new ) 328 { 329 hal_cpu_context_t * ctx_old = old->cpu_context; 330 hal_cpu_context_t * ctx_new = new->cpu_context; 208 // - CP0 : c0_th , c0_sr , C0_epc 209 // - CP2 : c2_ptpr , C2_mode 210 ///////////////////////////////////////////////////////////////////////////////////////// 211 // old_thread : pointer on current thread descriptor 212 // new_thread : pointer on new thread descriptor 213 ///////////////////////////////////////////////////////////////////////////////////////// 214 void hal_cpu_context_switch( thread_t * old_thread, 215 thread_t * new_thread ) 216 { 217 hal_cpu_context_t * ctx_old = old_thread->cpu_context; 218 hal_cpu_context_t * ctx_new = new_thread->cpu_context; 331 219 332 220 #if CONFIG_CONTEXT_DEBUG 333 hal_cpu_context_display( old );334 hal_cpu_context_display( new );221 hal_cpu_context_display( old_thread ); 222 hal_cpu_context_display( new_thread ); 335 223 #endif 224 225 // reset loadable field in new thread descriptor 226 new_thread->flags &= ~THREAD_FLAG_LOADABLE; 336 227 337 228 hal_do_switch( ctx_old , ctx_new ); … … 377 268 378 269 379 380 381 382 270 /////////////////////////////////////////////////// 383 271 error_t hal_fpu_context_create( thread_t * thread ) … … 385 273 kmem_req_t req; 386 274 275 assert( (sizeof(hal_fpu_context_t) <= CONFIG_FPU_CTX_SIZE) , __FUNCTION__ , 276 "inconsistent FPU context size" ); 277 387 278 // allocate memory for uzone 388 279 req.type = KMEM_FPU_CTX; 389 req.size = sizeof(hal_fpu_context_t);390 280 req.flags = AF_KERNEL | AF_ZERO; 391 281 … … 407 297 // allocate memory for dst fpu_context 408 298 req.type = KMEM_FPU_CTX; 409 req.size = sizeof(hal_fpu_context_t);410 299 req.flags = AF_KERNEL | AF_ZERO; 411 300 … … 435 324 436 325 } // end hal_fpu_context_destroy() 437 438 /////////////////////////////////////////////////////////////////////////////////////////439 // These registers are initialised:440 // - GPR : sp_29 , fp_30 , a0441 // - CP0 : c0_sr , c0_epc , c0_th442 // - CP2 : C2_ptpr , c2_mode443 // TODO Quand cette fonction est-elle appelée? [AG]444 /////////////////////////////////////////////////////////////////////////////////////////445 void hal_cpu_context_load( thread_t * thread )446 {447 // get relevant values from thread context448 hal_cpu_context_t * ctx = (hal_cpu_context_t *)thread->cpu_context;449 uint32_t sp_29 = ctx->sp_29;450 uint32_t fp_30 = ctx->fp_30;451 uint32_t c0_th = ctx->c0_th;452 uint32_t c0_sr = ctx->c0_sr;453 uint32_t c2_ptpr = ctx->c2_ptpr;454 uint32_t c2_mode = ctx->c2_mode;455 456 // get pointer on entry function & argument from thread attributes457 uint32_t func = (uint32_t)thread->entry_func;458 uint32_t args = (uint32_t)thread->entry_args;459 460 // reset loadable field in thread descriptor461 thread->flags &= ~THREAD_FLAG_LOADABLE;462 463 // load registers464 asm volatile(465 ".set noreorder \n"466 "or $26, %0, $0 \n" /* $26 <= stack pointer */467 "or $27, %2, $0 \n" /* $27 <= status register */468 "addiu $26, $26, -4 \n" /* decrement stack pointer */469 "or $4, %7, $0 \n" /* load a0 */470 "sw $4, ($26) \n" /* set entry_args in stack */471 "ori $27, $27, 0x2 \n" /* set EXL flag in status register */472 "mtc0 $27, $12 \n" /* load c0_sr */473 "mtc0 %3, $4, 2 \n" /* load c0_th */474 "mtc2 %4, $0 \n" /* load c2 ptpr */475 "mtc0 %6, $14 \n" /* load c0_epc */476 "or $29, $16, $0 \n" /* load sp_29 */477 "or $30, %1, $0 \n" /* load fp_30 */478 "mtc2 %5, $1 \n" /* load c2_mode */479 "nop \n"480 "eret \n" /* jump to user code */481 "nop \n"482 ".set reorder \n"483 :484 : "r"(sp_29),"r"(fp_30),"r"(c0_sr),"r"(c0_th),485 "r"(c2_ptpr),"r"(c2_mode),"r"(func),"r"(args)486 : "$4","$26","$27","$29","$30" );487 488 } // end hal_cpu_context_load()489 490 326 491 327 //////////////////////////////////////////////
Note: See TracChangeset
for help on using the changeset viewer.