[294] | 1 | ////////////////////////////////////////////////////////////////////////////////// |
---|
[258] | 2 | // File : ctx_handler.c |
---|
| 3 | // Date : 01/04/2012 |
---|
| 4 | // Authors : alain greiner & joel porquet |
---|
| 5 | // Copyright (c) UPMC-LIP6 |
---|
[294] | 6 | ////////////////////////////////////////////////////////////////////////////////// |
---|
[258] | 7 | |
---|
[440] | 8 | #include <ctx_handler.h> |
---|
[695] | 9 | #include <sys_handler.h> |
---|
[258] | 10 | #include <giet_config.h> |
---|
[528] | 11 | #include <hard_config.h> |
---|
[440] | 12 | #include <utils.h> |
---|
[459] | 13 | #include <tty0.h> |
---|
[258] | 14 | #include <xcu_driver.h> |
---|
| 15 | |
---|
[528] | 16 | ///////////////////////////////////////////////////////////////////////////////// |
---|
| 17 | // Extern variables and functions |
---|
| 18 | ///////////////////////////////////////////////////////////////////////////////// |
---|
| 19 | |
---|
| 20 | // defined in giet_kernel/switch.s file |
---|
[258] | 21 | extern void _task_switch(unsigned int *, unsigned int *); |
---|
| 22 | |
---|
[528] | 23 | // allocated in boot.c or kernel_init.c files |
---|
| 24 | extern static_scheduler_t* _schedulers[X_SIZE][Y_SIZE][NB_PROCS_MAX]; |
---|
| 25 | |
---|
[695] | 26 | ////////////////// |
---|
| 27 | static void _ctx_kill_task( unsigned int ltid ) |
---|
| 28 | { |
---|
| 29 | // get scheduler address |
---|
| 30 | static_scheduler_t* psched = (static_scheduler_t*)_get_sched(); |
---|
| 31 | |
---|
[697] | 32 | // pretend the task to kill is scheduled (required for sys_handler calls) |
---|
| 33 | unsigned int cur_task = psched->current; |
---|
| 34 | psched->current = ltid; |
---|
| 35 | |
---|
[695] | 36 | // release private TTY terminal if required |
---|
| 37 | if ( psched->context[ltid][CTX_TTY_ID] < NB_TTY_CHANNELS ) |
---|
| 38 | { |
---|
| 39 | _sys_tty_release(); |
---|
[697] | 40 | psched->context[ltid][CTX_TTY_ID] = -1; |
---|
[695] | 41 | } |
---|
| 42 | |
---|
| 43 | // release private TIM channel if required |
---|
| 44 | if ( psched->context[ltid][CTX_TIM_ID] < NB_TIM_CHANNELS ) |
---|
| 45 | { |
---|
| 46 | _sys_tim_release(); |
---|
[697] | 47 | psched->context[ltid][CTX_TIM_ID] = -1; |
---|
[695] | 48 | } |
---|
| 49 | |
---|
| 50 | // release private NIC_RX channel if required |
---|
| 51 | if ( psched->context[ltid][CTX_NIC_RX_ID] < NB_NIC_CHANNELS ) |
---|
| 52 | { |
---|
| 53 | _sys_nic_release( 1 ); |
---|
[697] | 54 | psched->context[ltid][CTX_NIC_RX_ID] = -1; |
---|
[695] | 55 | } |
---|
| 56 | |
---|
| 57 | // release private NIC_TX channel if required |
---|
| 58 | if ( psched->context[ltid][CTX_NIC_TX_ID] < NB_NIC_CHANNELS ) |
---|
| 59 | { |
---|
| 60 | _sys_nic_release( 0 ); |
---|
[697] | 61 | psched->context[ltid][CTX_NIC_TX_ID] = -1; |
---|
[695] | 62 | } |
---|
| 63 | |
---|
[697] | 64 | // restore scheduled task |
---|
| 65 | psched->current = cur_task; |
---|
| 66 | |
---|
[695] | 67 | // set NORUN_MASK_TASK bit |
---|
| 68 | _atomic_or( &psched->context[ltid][CTX_NORUN_ID], NORUN_MASK_TASK ); |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | |
---|
[696] | 72 | ////////////////// |
---|
| 73 | static void _ctx_exec_task( unsigned int ltid ) |
---|
| 74 | { |
---|
| 75 | // get scheduler address |
---|
| 76 | static_scheduler_t* psched = (static_scheduler_t*)_get_sched(); |
---|
| 77 | |
---|
| 78 | // TODO: reload .data segment |
---|
| 79 | |
---|
| 80 | // find initial stack pointer |
---|
| 81 | mapping_header_t * header = (mapping_header_t *)SEG_BOOT_MAPPING_BASE; |
---|
| 82 | mapping_task_t * task = _get_task_base(header); |
---|
| 83 | mapping_vseg_t * vseg = _get_vseg_base(header); |
---|
| 84 | unsigned int task_id = psched->context[ltid][CTX_GTID_ID]; |
---|
| 85 | unsigned int vseg_id = task[task_id].stack_vseg_id; |
---|
| 86 | unsigned int sp_value = vseg[vseg_id].vbase + vseg[vseg_id].length; |
---|
| 87 | |
---|
| 88 | // reset task context: RA / SR / SP / EPC / NORUN |
---|
| 89 | psched->context[ltid][CTX_RA_ID] = (unsigned int)&_ctx_eret; |
---|
| 90 | psched->context[ltid][CTX_SR_ID] = GIET_SR_INIT_VALUE; |
---|
| 91 | psched->context[ltid][CTX_SP_ID] = sp_value; |
---|
| 92 | psched->context[ltid][CTX_EPC_ID] = psched->context[ltid][CTX_ENTRY_ID]; |
---|
| 93 | psched->context[ltid][CTX_NORUN_ID] = 0; |
---|
| 94 | } |
---|
| 95 | |
---|
| 96 | |
---|
[528] | 97 | ////////////////////////////////// |
---|
| 98 | void _ctx_display( unsigned int x, |
---|
| 99 | unsigned int y, |
---|
| 100 | unsigned int p, |
---|
| 101 | unsigned int ltid, |
---|
| 102 | char* string ) |
---|
| 103 | { |
---|
| 104 | static_scheduler_t* psched = _schedulers[x][y][p]; |
---|
| 105 | _printf("\n########## task[%d,%d,%d,%d] context\n" |
---|
| 106 | " - CTX_EPC = %x\n" |
---|
| 107 | " - CTX_PTAB = %x\n" |
---|
| 108 | " - CTX_PTPR = %x\n" |
---|
| 109 | " - CTX_VSID = %x\n" |
---|
| 110 | " - CTX_SR = %x\n" |
---|
| 111 | " - CTX_RA = %x\n" |
---|
| 112 | " - CTX_SP = %x\n" |
---|
[629] | 113 | " - CTX_NORUN = %x\n" |
---|
[695] | 114 | " - CTX_SIG = %x\n" |
---|
[528] | 115 | "########## %s\n", |
---|
| 116 | x , y , p , ltid , |
---|
| 117 | psched->context[ltid][CTX_EPC_ID], |
---|
| 118 | psched->context[ltid][CTX_PTAB_ID], |
---|
| 119 | psched->context[ltid][CTX_PTPR_ID], |
---|
| 120 | psched->context[ltid][CTX_VSID_ID], |
---|
| 121 | psched->context[ltid][CTX_SR_ID], |
---|
| 122 | psched->context[ltid][CTX_RA_ID], |
---|
| 123 | psched->context[ltid][CTX_SP_ID], |
---|
[629] | 124 | psched->context[ltid][CTX_NORUN_ID], |
---|
[695] | 125 | psched->context[ltid][CTX_SIG_ID], |
---|
[528] | 126 | string ); |
---|
| 127 | } // _ctx_display() |
---|
| 128 | |
---|
| 129 | |
---|
[440] | 130 | ////////////////// |
---|
[258] | 131 | void _ctx_switch() |
---|
| 132 | { |
---|
[294] | 133 | unsigned int gpid = _get_procid(); |
---|
[428] | 134 | unsigned int cluster_xy = gpid >> P_WIDTH; |
---|
| 135 | unsigned int lpid = gpid & ((1<<P_WIDTH)-1); |
---|
[294] | 136 | |
---|
[258] | 137 | // get scheduler address |
---|
| 138 | static_scheduler_t* psched = (static_scheduler_t*)_get_sched(); |
---|
| 139 | |
---|
| 140 | // get number of tasks allocated to scheduler |
---|
| 141 | unsigned int tasks = psched->tasks; |
---|
| 142 | |
---|
| 143 | // get current task index |
---|
| 144 | unsigned int curr_task_id = psched->current; |
---|
| 145 | |
---|
| 146 | // select the next task using a round-robin policy |
---|
| 147 | unsigned int next_task_id; |
---|
| 148 | unsigned int tid; |
---|
| 149 | unsigned int found = 0; |
---|
| 150 | |
---|
| 151 | for (tid = curr_task_id + 1; tid < curr_task_id + 1 + tasks; tid++) |
---|
| 152 | { |
---|
| 153 | next_task_id = tid % tasks; |
---|
[695] | 154 | |
---|
| 155 | // this task needs to be killed |
---|
| 156 | if ( psched->context[next_task_id][CTX_SIG_ID] & SIG_MASK_KILL ) |
---|
| 157 | { |
---|
| 158 | _ctx_kill_task( next_task_id ); |
---|
| 159 | |
---|
| 160 | // acknowledge signal |
---|
| 161 | _atomic_and( &psched->context[next_task_id][CTX_SIG_ID], ~SIG_MASK_KILL ); |
---|
[696] | 162 | } |
---|
[695] | 163 | |
---|
[696] | 164 | // this task needs to be executed |
---|
| 165 | if ( psched->context[next_task_id][CTX_SIG_ID] & SIG_MASK_EXEC ) |
---|
| 166 | { |
---|
| 167 | _ctx_exec_task( next_task_id ); |
---|
| 168 | |
---|
| 169 | // acknowledge signal |
---|
| 170 | _atomic_and( &psched->context[next_task_id][CTX_SIG_ID], ~SIG_MASK_EXEC ); |
---|
[695] | 171 | } |
---|
| 172 | |
---|
[258] | 173 | // test if the task is runable |
---|
[629] | 174 | if ( psched->context[next_task_id][CTX_NORUN_ID] == 0 ) |
---|
[258] | 175 | { |
---|
| 176 | found = 1; |
---|
[695] | 177 | // TODO: don't break to process all pending signals. |
---|
[258] | 178 | break; |
---|
| 179 | } |
---|
| 180 | } |
---|
| 181 | |
---|
| 182 | // launch "idle" task if no runable task |
---|
[528] | 183 | if (found == 0) next_task_id = IDLE_TASK_INDEX; |
---|
[258] | 184 | |
---|
| 185 | #if GIET_DEBUG_SWITCH |
---|
[294] | 186 | unsigned int x = cluster_xy >> Y_WIDTH; |
---|
| 187 | unsigned int y = cluster_xy & ((1<<Y_WIDTH)-1); |
---|
[528] | 188 | _printf("\n[DEBUG SWITCH] (%d) -> (%d) on processor[%d,%d,%d] at cycle %d\n", |
---|
[294] | 189 | curr_task_id, next_task_id, x, y , lpid, _get_proctime() ); |
---|
[258] | 190 | #endif |
---|
| 191 | |
---|
[294] | 192 | if (curr_task_id != next_task_id) // actual task switch required |
---|
| 193 | { |
---|
[275] | 194 | unsigned int* curr_ctx_vaddr = &(psched->context[curr_task_id][0]); |
---|
| 195 | unsigned int* next_ctx_vaddr = &(psched->context[next_task_id][0]); |
---|
| 196 | |
---|
[528] | 197 | // reset TICK timer counter. |
---|
[294] | 198 | _xcu_timer_reset_cpt( cluster_xy, lpid ); |
---|
[275] | 199 | |
---|
| 200 | // set current task index |
---|
| 201 | psched->current = next_task_id; |
---|
| 202 | |
---|
| 203 | // makes context switch |
---|
[528] | 204 | _task_switch( curr_ctx_vaddr , next_ctx_vaddr ); |
---|
[258] | 205 | } |
---|
| 206 | } //end _ctx_switch() |
---|
| 207 | |
---|
[528] | 208 | |
---|
[440] | 209 | ///////////////// |
---|
[258] | 210 | void _idle_task() |
---|
| 211 | { |
---|
[391] | 212 | unsigned int gpid = _get_procid(); |
---|
[428] | 213 | unsigned int cluster_xy = gpid >> P_WIDTH; |
---|
[391] | 214 | unsigned int x = cluster_xy >> Y_WIDTH; |
---|
| 215 | unsigned int y = cluster_xy & ((1<<Y_WIDTH)-1); |
---|
[440] | 216 | unsigned int p = gpid & ((1<<P_WIDTH)-1); |
---|
[391] | 217 | |
---|
[258] | 218 | while(1) |
---|
| 219 | { |
---|
[391] | 220 | // initialize counter |
---|
[294] | 221 | unsigned int count = GIET_IDLE_TASK_PERIOD; |
---|
[275] | 222 | |
---|
[294] | 223 | // decounting loop |
---|
[258] | 224 | asm volatile( |
---|
| 225 | "move $3, %0 \n" |
---|
| 226 | "_idle_task_loop: \n" |
---|
| 227 | "addi $3, $3, -1 \n" |
---|
| 228 | "bnez $3, _idle_task_loop \n" |
---|
| 229 | "nop \n" |
---|
| 230 | : |
---|
| 231 | : "r"(count) |
---|
| 232 | : "$3" ); |
---|
| 233 | |
---|
[294] | 234 | // warning message |
---|
[528] | 235 | _printf("\n[GIET WARNING] Processor[%d,%d,%d] still idle at cycle %d", |
---|
| 236 | x , y , p , _get_proctime() ); |
---|
[258] | 237 | } |
---|
| 238 | } // end ctx_idle() |
---|
| 239 | |
---|
| 240 | |
---|
[440] | 241 | //////////////// |
---|
[258] | 242 | void _ctx_eret() |
---|
| 243 | { |
---|
| 244 | asm volatile("eret"); |
---|
| 245 | } |
---|
| 246 | |
---|
| 247 | |
---|
| 248 | // Local Variables: |
---|
| 249 | // tab-width: 4 |
---|
| 250 | // c-basic-offset: 4 |
---|
| 251 | // c-file-offsets:((innamespace . 0)(inline-open . 0)) |
---|
| 252 | // indent-tabs-mode: nil |
---|
| 253 | // End: |
---|
| 254 | // vim: filetype=c:expandtab:shiftwidth=4:tabstop=4:softtabstop=4 |
---|
| 255 | |
---|