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