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