[258] | 1 | ///////////////////////////////////////////////////////////////////////////////////////// |
---|
| 2 | // File : ctx_handler.c |
---|
| 3 | // Date : 01/04/2012 |
---|
| 4 | // Authors : alain greiner & joel porquet |
---|
| 5 | // Copyright (c) UPMC-LIP6 |
---|
| 6 | ///////////////////////////////////////////////////////////////////////////////////////// |
---|
| 7 | // The ctx_handler.h and ctx_handler.c files are part of the GIET-VM nano-kernel. |
---|
| 8 | // This code is used to support context switch when several tasks are executing |
---|
| 9 | // in time multiplexing on a single processor. |
---|
| 10 | // The tasks are statically allocated to a processor in the boot phase, and |
---|
| 11 | // there is one private scheduler per processor. Each sheduler occupies 4K bytes, |
---|
| 12 | // and contains up to 14 task contexts (task_id is from 0 to 13). |
---|
| 13 | // The task context [14] is reserved for the "idle" task that does nothing, and |
---|
| 14 | // is launched by the scheduler when there is no other runable task. |
---|
| 15 | ///////////////////////////////////////////////////////////////////////////////////////// |
---|
| 16 | |
---|
| 17 | #include <giet_config.h> |
---|
| 18 | #include <tim_driver.h> |
---|
| 19 | #include <xcu_driver.h> |
---|
| 20 | #include <tty_driver.h> |
---|
| 21 | #include <utils.h> |
---|
| 22 | #include <ctx_handler.h> |
---|
| 23 | #include <mapping_info.h> |
---|
| 24 | #include <sys_handler.h> |
---|
| 25 | |
---|
| 26 | ///////////////////////////////////////////////////////////////////////////////////////// |
---|
| 27 | // A task context is an array of 64 words = 256 bytes. |
---|
| 28 | // It contains copies of processor registers (when the task is preempted): |
---|
| 29 | // - GPR[i], generally stored in slot (i). $0, $26 & $27 are not saved. |
---|
| 30 | // - HI & LO registers |
---|
| 31 | // - CP0 registers: EPC, SR, CR, BVAR |
---|
| 32 | // - CP2 registers : PTPR |
---|
| 33 | // It contains some general informations associated to the task: |
---|
| 34 | // - TTY : TTY channel global index |
---|
| 35 | // - NIC : NIC channel global index |
---|
| 36 | // - CMA : CMA channel global index |
---|
| 37 | // - HBA : HBA channel global index |
---|
| 38 | // - DMA : DMA channel local index |
---|
| 39 | // - TIM : TIM channel local index |
---|
| 40 | // - PTAB : page table virtual base address |
---|
| 41 | // - LTID : Task local index (in scheduler) |
---|
| 42 | // - VSID : Virtual space index |
---|
| 43 | // - RUN : Task state (0 => sleeping / 1 => runable ) |
---|
| 44 | // |
---|
| 45 | // ctx[0]<- ***|ctx[8] <- $8 |ctx[16]<- $16|ctx[24]<- $24|ctx[32]<- EPC |ctx[40]<- TTY |
---|
| 46 | // ctx[1]<- $1 |ctx[9] <- $9 |ctx[17]<- $17|ctx[25]<- $25|ctx[33]<- CR |ctx[41]<- DMA |
---|
| 47 | // ctx[2]<- $2 |ctx[10]<- $10|ctx[18]<- $18|ctx[26]<- LO |ctx[34]<- SR |ctx[42]<- NIC |
---|
| 48 | // ctx[3]<- $3 |ctx[11]<- $11|ctx[19]<- $19|ctx[27]<- HI |ctx[35]<- BVAR |ctx[43]<- TIM |
---|
| 49 | // ctx[4]<- $4 |ctx[12]<- $12|ctx[20]<- $20|ctx[28]<- $28|ctx[36]<- PTAB |ctx[44]<- HBA |
---|
| 50 | // ctx[5]<- $5 |ctx[13]<- $13|ctx[21]<- $21|ctx[29]<- SP |ctx[37]<- LTID |ctx[45]<- CMA |
---|
| 51 | // ctx[6]<- $6 |ctx[14]<- $14|ctx[22]<- $22|ctx[30]<- $30|ctx[38]<- VSID |ctx[46]<- GTID |
---|
| 52 | // ctx[7]<- $7 |ctx[15]<- $15|ctx[23]<- $23|ctx[31]<- RA |ctx[39]<- PTPR |ctx[47]<- RUN |
---|
| 53 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 54 | |
---|
| 55 | extern void _task_switch(unsigned int *, unsigned int *); |
---|
| 56 | |
---|
| 57 | ///////////////////////////////////////////////////////////////////////////////// |
---|
| 58 | // _ctx_switch() |
---|
| 59 | // This function performs a context switch between the running task |
---|
| 60 | // and another task, using a round-robin sheduling policy between all |
---|
| 61 | // tasks allocated to a given processor (static allocation). |
---|
| 62 | // It selects the next runable task to resume execution. |
---|
| 63 | // If the only runable task is the current task, return without context switch. |
---|
| 64 | // If there is no runable task, the scheduler switch to the default "idle" task. |
---|
| 65 | // |
---|
| 66 | // Implementation note |
---|
| 67 | // The return address contained in $31 is saved in the current task context |
---|
| 68 | // (in the ctx[31] slot), and the function actually returns to the address |
---|
| 69 | // contained in the ctx[31] slot of the next task context. |
---|
| 70 | ///////////////////////////////////////////////////////////////////////////////// |
---|
| 71 | void _ctx_switch() |
---|
| 72 | { |
---|
| 73 | // get scheduler address |
---|
| 74 | static_scheduler_t* psched = (static_scheduler_t*)_get_sched(); |
---|
| 75 | |
---|
| 76 | // get number of tasks allocated to scheduler |
---|
| 77 | unsigned int tasks = psched->tasks; |
---|
| 78 | |
---|
| 79 | // get current task index |
---|
| 80 | unsigned int curr_task_id = psched->current; |
---|
| 81 | |
---|
| 82 | // select the next task using a round-robin policy |
---|
| 83 | unsigned int next_task_id; |
---|
| 84 | unsigned int tid; |
---|
| 85 | unsigned int found = 0; |
---|
| 86 | |
---|
| 87 | for (tid = curr_task_id + 1; tid < curr_task_id + 1 + tasks; tid++) |
---|
| 88 | { |
---|
| 89 | next_task_id = tid % tasks; |
---|
| 90 | // test if the task is runable |
---|
| 91 | if ( psched->context[next_task_id][CTX_RUN_ID] ) |
---|
| 92 | { |
---|
| 93 | found = 1; |
---|
| 94 | break; |
---|
| 95 | } |
---|
| 96 | } |
---|
| 97 | |
---|
| 98 | // launch "idle" task if no runable task |
---|
| 99 | if (found == 0) |
---|
| 100 | { |
---|
| 101 | next_task_id = IDLE_TASK_INDEX; |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | // no switch if no change |
---|
| 105 | if (curr_task_id != next_task_id) |
---|
| 106 | { |
---|
| 107 | unsigned int* curr_ctx_vaddr = &(psched->context[curr_task_id][0]); |
---|
| 108 | unsigned int* next_ctx_vaddr = &(psched->context[next_task_id][0]); |
---|
| 109 | unsigned int procid = _get_procid(); |
---|
| 110 | unsigned int local_id = procid % NB_PROCS_MAX; |
---|
| 111 | unsigned int cluster_id = procid / NB_PROCS_MAX; |
---|
| 112 | |
---|
| 113 | // set current task index |
---|
| 114 | psched->current = next_task_id; |
---|
| 115 | |
---|
| 116 | // reset timer counter |
---|
| 117 | #if USE_XICU |
---|
| 118 | _xcu_timer_reset_cpt(cluster_id, local_id); |
---|
| 119 | #else |
---|
| 120 | _timer_reset_cpt(cluster_id, local_id); |
---|
| 121 | #endif |
---|
| 122 | |
---|
| 123 | // makes context switch |
---|
| 124 | _task_switch(curr_ctx_vaddr, next_ctx_vaddr); |
---|
| 125 | |
---|
| 126 | #if GIET_DEBUG_SWITCH |
---|
| 127 | _tty_get_lock( 0 ); |
---|
| 128 | _puts("\n[GIET DEBUG] Context switch for processor "); |
---|
| 129 | _putd(_get_procid()); |
---|
| 130 | _puts(" at cycle "); |
---|
| 131 | _putd(_get_proctime()); |
---|
| 132 | _puts("\n"); |
---|
| 133 | _puts(" - tasks = "); |
---|
| 134 | _putd(tasks); |
---|
| 135 | _puts("\n"); |
---|
| 136 | _puts(" - curr_task_id = "); |
---|
| 137 | _putd( curr_task_id ); |
---|
| 138 | _puts("\n"); |
---|
| 139 | _puts(" - next_task_id = "); |
---|
| 140 | _putd(next_task_id); |
---|
| 141 | _puts("\n"); |
---|
| 142 | _tty_release_lock( 0 ); |
---|
| 143 | #endif |
---|
| 144 | |
---|
| 145 | } |
---|
| 146 | } //end _ctx_switch() |
---|
| 147 | |
---|
| 148 | ///////////////////////////////////////////////////////////////////////////////////// |
---|
| 149 | // This function is executed as the"idle" task when no other task can be executed |
---|
| 150 | ///////////////////////////////////////////////////////////////////////////////////// |
---|
| 151 | void _idle_task() |
---|
| 152 | { |
---|
| 153 | unsigned int count = GIET_IDLE_TASK_PERIOD; |
---|
| 154 | while(1) |
---|
| 155 | { |
---|
| 156 | asm volatile( |
---|
| 157 | "move $3, %0 \n" |
---|
| 158 | "_idle_task_loop: \n" |
---|
| 159 | "addi $3, $3, -1 \n" |
---|
| 160 | "bnez $3, _idle_task_loop \n" |
---|
| 161 | "nop \n" |
---|
| 162 | : |
---|
| 163 | : "r"(count) |
---|
| 164 | : "$3" ); |
---|
| 165 | |
---|
| 166 | _tty_get_lock( 0 ); |
---|
| 167 | _puts("\n[GIET WARNING] Processor "); |
---|
| 168 | _putd(_get_procid()); |
---|
| 169 | _puts(" idle at cycle "); |
---|
| 170 | _putd(_get_proctime()); |
---|
| 171 | _puts("\n"); |
---|
| 172 | _tty_release_lock( 0 ); |
---|
| 173 | |
---|
| 174 | count = GIET_IDLE_TASK_PERIOD; |
---|
| 175 | } |
---|
| 176 | } // end ctx_idle() |
---|
| 177 | |
---|
| 178 | |
---|
| 179 | ///////////////////////////////////////////////////////////////////////////////// |
---|
| 180 | // The address of this functionis used to initialise the return address |
---|
| 181 | // in the "idle" task context. |
---|
| 182 | ///////////////////////////////////////////////////////////////////////////////// |
---|
| 183 | void _ctx_eret() |
---|
| 184 | { |
---|
| 185 | asm volatile("eret"); |
---|
| 186 | } |
---|
| 187 | |
---|
| 188 | |
---|
| 189 | // Local Variables: |
---|
| 190 | // tab-width: 4 |
---|
| 191 | // c-basic-offset: 4 |
---|
| 192 | // c-file-offsets:((innamespace . 0)(inline-open . 0)) |
---|
| 193 | // indent-tabs-mode: nil |
---|
| 194 | // End: |
---|
| 195 | // vim: filetype=c:expandtab:shiftwidth=4:tabstop=4:softtabstop=4 |
---|
| 196 | |
---|