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