[528] | 1 | ///////////////////////////////////////////////////////////////////////////////// |
---|
[294] | 2 | // File : ctx_handler.h |
---|
| 3 | // Date : 01/04/2012 |
---|
| 4 | // Authors : alain greiner & joel porquet |
---|
| 5 | // Copyright (c) UPMC-LIP6 |
---|
[528] | 6 | ///////////////////////////////////////////////////////////////////////////////// |
---|
[294] | 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 |
---|
[440] | 11 | // there is one private scheduler per processor. Each sheduler occupies 8K bytes, |
---|
[294] | 12 | // and contains up to 14 task contexts (task_id is from 0 to 13). |
---|
| 13 | // The task context [13] is reserved for the "idle" task that does nothing, and |
---|
| 14 | // is launched by the scheduler when there is no other runable task. |
---|
[528] | 15 | ///////////////////////////////////////////////////////////////////////////////// |
---|
[294] | 16 | // A task context is an array of 64 words = 256 bytes. |
---|
[440] | 17 | // It contains copies of processor registers (when the task is preempted) |
---|
| 18 | // and some general informations associated to a task, such as the peripherals |
---|
[528] | 19 | // allocated to the task (private peripheral channel) |
---|
| 20 | ///////////////////////////////////////////////////////////////////////////////// |
---|
[556] | 21 | // ctx[0] <- *** |ctx[8] <- $8 |ctx[16]<- $16 |ctx[24]<- $24 |
---|
| 22 | // ctx[1] <- $1 |ctx[9] <- $9 |ctx[17]<- $17 |ctx[25]<- $25 |
---|
| 23 | // ctx[2] <- $2 |ctx[10]<- $10 |ctx[18]<- $18 |ctx[26]<- LO |
---|
| 24 | // ctx[3] <- $3 |ctx[11]<- $11 |ctx[19]<- $19 |ctx[27]<- HI |
---|
| 25 | // ctx[4] <- $4 |ctx[12]<- $12 |ctx[20]<- $20 |ctx[28]<- $28 |
---|
| 26 | // ctx[5] <- $5 |ctx[13]<- $13 |ctx[21]<- $21 |ctx[29]<- SP |
---|
| 27 | // ctx[6] <- $6 |ctx[14]<- $14 |ctx[22]<- $22 |ctx[30]<- $30 |
---|
| 28 | // ctx[7] <- $7 |ctx[15]<- $15 |ctx[23]<- $23 |ctx[31]<- RA |
---|
[294] | 29 | // |
---|
[556] | 30 | // ctx[32]<- EPC |ctx[40]<- TTY |ctx[48]<- TRDID |ctx[56]<- *** |
---|
| 31 | // ctx[33]<- CR |ctx[41]<- CMA_FB |ctx[49]<- GTID |ctx[57]<- *** |
---|
| 32 | // ctx[34]<- SR |ctx[42]<- CMA_RX |ctx[50]<- RUN |ctx[58]<- *** |
---|
| 33 | // ctx[35]<- BVAR |ctx[43]<- CMA_TX |ctx[51]<- COPROC |ctx[59]<- *** |
---|
| 34 | // ctx[36]<- PTAB |ctx[44]<- NIC_RX |ctx[52]<- *** |ctx[60]<- *** |
---|
| 35 | // ctx[37]<- LTID |ctx[45]<- NIC_TX |ctx[53]<- *** |ctx[61]<- *** |
---|
| 36 | // ctx[38]<- VSID |ctx[46]<- TIM |ctx[54]<- *** |ctx[62]<- *** |
---|
| 37 | // ctx[39]<- PTPR |ctx[47]<- HBA |ctx[55]<- *** |ctx[63]<- *** |
---|
[528] | 38 | ///////////////////////////////////////////////////////////////////////////////// |
---|
[294] | 39 | |
---|
[258] | 40 | #ifndef _CTX_HANDLER_H |
---|
| 41 | #define _CTX_HANDLER_H |
---|
| 42 | |
---|
| 43 | #include <giet_config.h> |
---|
| 44 | |
---|
| 45 | ///////////////////////////////////////////////////////////////////////////////// |
---|
[440] | 46 | // Definition of the task context slots indexes |
---|
| 47 | ///////////////////////////////////////////////////////////////////////////////// |
---|
| 48 | |
---|
| 49 | #define CTX_SP_ID 29 // Stack Pointer |
---|
| 50 | #define CTX_RA_ID 31 // Return Address |
---|
| 51 | |
---|
| 52 | #define CTX_EPC_ID 32 // Exception Program Counter (CP0) |
---|
| 53 | #define CTX_CR_ID 33 // Cause Register (CP0) |
---|
| 54 | #define CTX_SR_ID 34 // Status Register (CP0) |
---|
| 55 | #define CTX_BVAR_ID 35 // Bad Virtual Address Register (CP0) |
---|
| 56 | #define CTX_PTAB_ID 36 // Page Table Virtual address |
---|
| 57 | #define CTX_LTID_ID 37 // Local Task Index (in scheduler) |
---|
| 58 | #define CTX_VSID_ID 38 // Vspace Index |
---|
| 59 | #define CTX_PTPR_ID 39 // Page Table Pointer Register (PADDR>>13) |
---|
| 60 | |
---|
| 61 | #define CTX_TTY_ID 40 // private TTY channel index |
---|
[449] | 62 | #define CTX_CMA_FB_ID 41 // private CMA channel index for FBF write |
---|
| 63 | #define CTX_CMA_RX_ID 42 // private CMA channel index for NIC_TX |
---|
| 64 | #define CTX_CMA_TX_ID 43 // private CMA channel index for NIC_RX |
---|
| 65 | #define CTX_NIC_RX_ID 44 // private NIC channel index RX transfer |
---|
| 66 | #define CTX_NIC_TX_ID 45 // private NIC channel index TX transfer |
---|
[440] | 67 | #define CTX_TIM_ID 46 // ptivate TIM channel index |
---|
[449] | 68 | #define CTX_HBA_ID 47 // private HBA channel index |
---|
[440] | 69 | |
---|
| 70 | #define CTX_TRDID_ID 48 // Thread Task Index in vspace |
---|
| 71 | #define CTX_GTID_ID 49 // Global Task Index in all system |
---|
[449] | 72 | #define CTX_RUN_ID 50 // Boolean: task runable |
---|
[556] | 73 | #define CTX_COPROC_ID 51 // cluster_xy : coprocessor coordinates |
---|
[440] | 74 | |
---|
| 75 | ///////////////////////////////////////////////////////////////////////////////// |
---|
| 76 | |
---|
| 77 | ///////////////////////////////////////////////////////////////////////////////// |
---|
[258] | 78 | // Definition of the scheduler structure |
---|
| 79 | ///////////////////////////////////////////////////////////////////////////////// |
---|
| 80 | |
---|
| 81 | typedef struct static_scheduler_s |
---|
| 82 | { |
---|
[294] | 83 | unsigned int context[14][64]; // at most 14 task (including idle_task) |
---|
[258] | 84 | unsigned int tasks; // actual number of tasks |
---|
| 85 | unsigned int current; // current task index |
---|
[294] | 86 | unsigned int hwi_vector[32]; // hardware interrupt vector |
---|
| 87 | unsigned int pti_vector[32]; // timer interrupt vector |
---|
| 88 | unsigned int wti_vector[32]; // software interrupt vector |
---|
[310] | 89 | unsigned int reserved[30]; // padding to 4 Kbytes |
---|
[391] | 90 | unsigned int idle_stack[1024]; // private stack for idle stack (4Kbytes) |
---|
[258] | 91 | } static_scheduler_t; |
---|
| 92 | |
---|
[440] | 93 | #define IDLE_TASK_INDEX 13 |
---|
[258] | 94 | |
---|
[440] | 95 | |
---|
[258] | 96 | ///////////////////////////////////////////////////////////////////////////////// |
---|
[528] | 97 | // Extern Functions |
---|
[258] | 98 | ///////////////////////////////////////////////////////////////////////////////// |
---|
| 99 | |
---|
[440] | 100 | ///////////////////////////////////////////////////////////////////////////////// |
---|
| 101 | // This function performs a context switch between the running task |
---|
| 102 | // and another task, using a round-robin sheduling policy between all |
---|
| 103 | // tasks allocated to a given processor (static allocation). |
---|
| 104 | // It selects the next runable task to resume execution. |
---|
| 105 | // If the only runable task is the current task, return without context switch. |
---|
| 106 | // If there is no runable task, the scheduler switch to the default "idle" task. |
---|
| 107 | // The return address contained in $31 is saved in the current task context |
---|
| 108 | // (in the ctx[31] slot), and the function actually returns to the address |
---|
| 109 | // contained in the ctx[31] slot of the next task context. |
---|
[258] | 110 | ///////////////////////////////////////////////////////////////////////////////// |
---|
[440] | 111 | extern void _ctx_switch(); |
---|
[258] | 112 | |
---|
[440] | 113 | ///////////////////////////////////////////////////////////////////////////////// |
---|
| 114 | // The address of this function is used to initialise the return address |
---|
| 115 | // in the "idle" task context. |
---|
| 116 | ///////////////////////////////////////////////////////////////////////////////// |
---|
| 117 | extern void _ctx_eret(); |
---|
[258] | 118 | |
---|
[440] | 119 | ///////////////////////////////////////////////////////////////////////////////// |
---|
| 120 | // This function is executed task when no other task can be executed. |
---|
| 121 | ///////////////////////////////////////////////////////////////////////////////// |
---|
[258] | 122 | extern void _idle_task(); |
---|
| 123 | |
---|
[528] | 124 | ///////////////////////////////////////////////////////////////////////////////// |
---|
| 125 | // This function displays the context of a task identified by the processor |
---|
| 126 | // coordinates (x,y,p), and by the local task index ltid. |
---|
| 127 | // The string argument can be used for debug. |
---|
| 128 | ///////////////////////////////////////////////////////////////////////////////// |
---|
| 129 | extern void _ctx_display( unsigned int x, |
---|
| 130 | unsigned int y, |
---|
| 131 | unsigned int p, |
---|
| 132 | unsigned int ltid, |
---|
| 133 | char* string ); |
---|
[258] | 134 | |
---|
| 135 | #endif |
---|