[165] | 1 | /////////////////////////////////////////////////////////////////////////////////// |
---|
| 2 | // File : kernel_init.c |
---|
| 3 | // Date : 26/05/2012 |
---|
| 4 | // Authors : alain greiner & mohamed karaoui |
---|
| 5 | // Copyright (c) UPMC-LIP6 |
---|
| 6 | //////////////////////////////////////////////////////////////////////////////////// |
---|
| 7 | // The kernel_init.c files is part of the GIET-VM nano-kernel. |
---|
[189] | 8 | // It contains the kernel entry point for the last step of system initialisation. |
---|
| 9 | // All procs in this phase have their MMU activated, and are running in parallel. |
---|
[199] | 10 | // Each processor perform the following actions: |
---|
| 11 | // 1/ contributes to _schedulers_paddr[] initialisation |
---|
| 12 | // 2/ contributes to _ptabs_paddr[] and _ptabs_vaddr arrays initialisation |
---|
| 13 | // 3/ computes and set the ICU mask for its private ICU channel |
---|
| 14 | // 4/ initialises its private TICK timer (if required) |
---|
| 15 | // 5/ initialises the "idle" task context in its private scheduler |
---|
| 16 | // 6/ initialises the SP, SR, PTPR, EPC registers |
---|
| 17 | // 7/ jumps to the user code with an eret. |
---|
[165] | 18 | //////////////////////////////////////////////////////////////////////////////////// |
---|
| 19 | |
---|
| 20 | #include <common.h> |
---|
[189] | 21 | #include <irq_handler.h> |
---|
[165] | 22 | #include <ctx_handler.h> |
---|
| 23 | #include <sys_handler.h> |
---|
| 24 | #include <mapping_info.h> |
---|
| 25 | #include <giet_config.h> |
---|
| 26 | #include <mips32_registers.h> |
---|
| 27 | #include <irq_handler.h> |
---|
[166] | 28 | #include <vm_handler.h> |
---|
[165] | 29 | #include <hwr_mapping.h> |
---|
| 30 | #include <mwmr_channel.h> |
---|
| 31 | #include <barrier.h> |
---|
| 32 | #include <drivers.h> |
---|
| 33 | |
---|
| 34 | /////////////////////////////////////////////////////////////////////////////////// |
---|
[238] | 35 | // array of pointers on the page tables (virtual addresses) |
---|
[167] | 36 | /////////////////////////////////////////////////////////////////////////////////// |
---|
| 37 | |
---|
[189] | 38 | __attribute__((section (".kdata"))) |
---|
[238] | 39 | unsigned int _ptabs[GIET_NB_VSPACE_MAX]; // virtual addresses |
---|
[167] | 40 | |
---|
[238] | 41 | __attribute__((section (".kdata"))) |
---|
| 42 | unsigned int _ptprs[GIET_NB_VSPACE_MAX]; // physical addresses >> 13 |
---|
[165] | 43 | |
---|
[218] | 44 | /////////////////////////////////////////////////////////////////////////////////// |
---|
| 45 | // array of pointers on the schedulers (physical addresses) |
---|
| 46 | /////////////////////////////////////////////////////////////////////////////////// |
---|
| 47 | |
---|
[189] | 48 | __attribute__((section (".kdata"))) |
---|
[238] | 49 | static_scheduler_t* _schedulers[NB_CLUSTERS * NB_PROCS_MAX]; |
---|
[165] | 50 | |
---|
[218] | 51 | /////////////////////////////////////////////////////////////////////////////////// |
---|
| 52 | // staks for the "idle" tasks (256 bytes for each processor) |
---|
| 53 | /////////////////////////////////////////////////////////////////////////////////// |
---|
| 54 | |
---|
[199] | 55 | __attribute__((section (".kdata"))) |
---|
[238] | 56 | unsigned int _idle_stack[NB_CLUSTERS * NB_PROCS_MAX * 64]; |
---|
[199] | 57 | |
---|
[238] | 58 | //////////////// |
---|
| 59 | void _sys_exit() |
---|
| 60 | { |
---|
[228] | 61 | while (1); |
---|
[215] | 62 | } |
---|
| 63 | |
---|
[228] | 64 | |
---|
[246] | 65 | |
---|
[165] | 66 | ////////////////////////////////////////////////////////////////////////////////// |
---|
[189] | 67 | // This function is the entry point for the last step of the boot sequence. |
---|
[238] | 68 | // that is done in parallel by all processors, with MMU activated. |
---|
[165] | 69 | ////////////////////////////////////////////////////////////////////////////////// |
---|
[238] | 70 | __attribute__((section (".kinit"))) void _kernel_init() |
---|
| 71 | { |
---|
| 72 | // Step 1 : get processor index, |
---|
| 73 | // get scheduler address |
---|
| 74 | // initialise _schedulers[] array |
---|
[165] | 75 | |
---|
[238] | 76 | unsigned int global_pid = _procid(); |
---|
| 77 | unsigned int cluster_id = global_pid / NB_PROCS_MAX; |
---|
| 78 | unsigned int proc_id = global_pid % NB_PROCS_MAX; |
---|
| 79 | static_scheduler_t* psched = _get_sched(); |
---|
| 80 | unsigned int tasks = psched->tasks; |
---|
[207] | 81 | |
---|
[238] | 82 | _schedulers[global_pid] = psched; |
---|
[207] | 83 | |
---|
| 84 | #if GIET_DEBUG_INIT |
---|
[238] | 85 | _get_lock(&_tty_put_lock); |
---|
| 86 | _puts("\n[GIET DEBUG] step 1 for processor "); |
---|
| 87 | _putd(global_pid); |
---|
| 88 | _puts(" : tasks = "); |
---|
| 89 | _putd(tasks); |
---|
| 90 | _puts(" / scheduler vbase = "); |
---|
| 91 | _putx((unsigned int) psched); |
---|
| 92 | _puts("\n"); |
---|
| 93 | _release_lock(&_tty_put_lock); |
---|
[207] | 94 | #endif |
---|
| 95 | |
---|
[238] | 96 | // step 2 : initialise ptabs[] & ptprs[] arrays |
---|
[203] | 97 | // each processor scans all tasks contexts in its |
---|
| 98 | // private scheduler and get VSID, PTAB and PTPR values |
---|
[199] | 99 | |
---|
| 100 | unsigned int ltid; |
---|
| 101 | |
---|
[238] | 102 | for (ltid = 0; ltid < tasks; ltid++) |
---|
| 103 | { |
---|
[246] | 104 | unsigned int vsid = _get_task_slot(ltid , CTX_VSID_ID); |
---|
| 105 | unsigned int ptab = _get_task_slot(ltid , CTX_PTAB_ID); |
---|
| 106 | unsigned int ptpr = _get_task_slot(ltid , CTX_PTPR_ID); |
---|
[203] | 107 | |
---|
[238] | 108 | _ptabs[vsid] = ptab; |
---|
| 109 | _ptprs[vsid] = ptpr; |
---|
[199] | 110 | |
---|
| 111 | #if GIET_DEBUG_INIT |
---|
[238] | 112 | _get_lock(&_tty_put_lock); |
---|
| 113 | _puts("\n[GIET DEBUG] step 2 for processor "); |
---|
| 114 | _putd(global_pid); |
---|
| 115 | _puts(" / vspace "); |
---|
| 116 | _putd(vsid); |
---|
| 117 | _puts("\n- ptab = "); |
---|
| 118 | _putx(ptab); |
---|
| 119 | _puts("\n- ptpr = "); |
---|
| 120 | _putx(ptpr); |
---|
| 121 | _puts("\n"); |
---|
| 122 | _release_lock(&_tty_put_lock); |
---|
[199] | 123 | #endif |
---|
| 124 | |
---|
| 125 | } |
---|
[228] | 126 | |
---|
[203] | 127 | // step 3 : compute and set ICU masks |
---|
| 128 | // there is at most 32 interrupts per processor |
---|
| 129 | // software interrupts are not supported yet |
---|
| 130 | |
---|
[238] | 131 | unsigned int isr_switch_channel = 0xFFFFFFFF; |
---|
| 132 | unsigned int irq_id; // IN_IRQ index |
---|
[203] | 133 | unsigned int hwi_mask = 0; |
---|
| 134 | unsigned int pti_mask = 0; |
---|
| 135 | |
---|
[238] | 136 | for (irq_id = 0; irq_id < 32; irq_id++) |
---|
| 137 | { |
---|
| 138 | unsigned int entry = psched->interrupt_vector[irq_id]; |
---|
| 139 | unsigned int isr = entry & 0x000000FF; |
---|
[203] | 140 | |
---|
[238] | 141 | if ((isr == ISR_DMA) || (isr == ISR_IOC) || (isr == ISR_TTY)) |
---|
| 142 | { |
---|
[228] | 143 | hwi_mask = hwi_mask | 0x1 << irq_id; |
---|
[203] | 144 | } |
---|
[238] | 145 | else if ((isr == ISR_SWITCH)) |
---|
| 146 | { |
---|
[228] | 147 | pti_mask = pti_mask | 0x1 << irq_id; |
---|
[215] | 148 | isr_switch_channel = irq_id; |
---|
[203] | 149 | } |
---|
[238] | 150 | else if ((isr == ISR_TIMER)) |
---|
| 151 | { |
---|
[228] | 152 | pti_mask = pti_mask | 0x1 << irq_id; |
---|
[215] | 153 | } |
---|
[165] | 154 | } |
---|
[228] | 155 | |
---|
[189] | 156 | #if GIET_DEBUG_INIT |
---|
[238] | 157 | _get_lock(&_tty_put_lock); |
---|
| 158 | _puts("\n[GIET DEBUG] step 3 for processor "); |
---|
| 159 | _putd(global_pid); |
---|
| 160 | _puts("\n - ICU HWI_MASK = "); |
---|
| 161 | _putx(hwi_mask); |
---|
| 162 | _puts("\n - ICU PTI_MASK = "); |
---|
| 163 | _putx(pti_mask); |
---|
| 164 | _puts("\n"); |
---|
| 165 | _release_lock(&_tty_put_lock); |
---|
[189] | 166 | #endif |
---|
[165] | 167 | |
---|
[238] | 168 | _icu_set_mask(cluster_id, proc_id, hwi_mask, 0); // set HWI_MASK |
---|
| 169 | _icu_set_mask(cluster_id, proc_id, pti_mask, 1); // set PTI_MASK |
---|
| 170 | |
---|
[189] | 171 | // step 4 : start TICK timer if more than one task |
---|
[238] | 172 | if (tasks > 1) |
---|
| 173 | { |
---|
| 174 | if (isr_switch_channel == 0xFFFFFFFF) |
---|
| 175 | { |
---|
[216] | 176 | _get_lock(&_tty_put_lock); |
---|
[215] | 177 | _puts("\n[GIET ERROR] ISR_SWITCH not found on proc "); |
---|
[228] | 178 | _putd(proc_id); |
---|
[215] | 179 | _puts("\n"); |
---|
[216] | 180 | _release_lock(&_tty_put_lock); |
---|
[215] | 181 | _sys_exit(); |
---|
| 182 | } |
---|
[216] | 183 | |
---|
[238] | 184 | if (_timer_start( cluster_id, isr_switch_channel, GIET_TICK_VALUE)) |
---|
| 185 | { |
---|
[216] | 186 | _get_lock(&_tty_put_lock); |
---|
| 187 | _puts("\n[GIET ERROR] ISR_SWITCH init error for proc "); |
---|
[228] | 188 | _putd(proc_id); |
---|
[216] | 189 | _puts("\n"); |
---|
| 190 | _release_lock(&_tty_put_lock); |
---|
| 191 | _sys_exit(); |
---|
| 192 | } |
---|
[228] | 193 | |
---|
[189] | 194 | #if GIET_DEBUG_INIT |
---|
[238] | 195 | _get_lock(&_tty_put_lock); |
---|
| 196 | _puts("\n[GIET DEBUG] Step 4 for processor "); |
---|
| 197 | _putd(global_pid); |
---|
| 198 | _puts(" / context switch activated\n"); |
---|
| 199 | _release_lock(&_tty_put_lock); |
---|
[189] | 200 | #endif |
---|
[228] | 201 | |
---|
[189] | 202 | } |
---|
| 203 | |
---|
[218] | 204 | // step 5 : initialise, for each processor, the "idle" task context: |
---|
[199] | 205 | // the SR initialisation value is 0xFF03 because |
---|
[218] | 206 | // the task _ctx_idle() executes in kernel mode. |
---|
[199] | 207 | // it uses the page table of vspace[0] |
---|
[218] | 208 | // the stack size is 256 bytes |
---|
[199] | 209 | |
---|
[238] | 210 | unsigned int stack = (unsigned int)_idle_stack + ((global_pid + 1)<<8); |
---|
[228] | 211 | |
---|
[238] | 212 | _set_task_slot( IDLE_TASK_INDEX, CTX_RUN_ID, 1); |
---|
| 213 | _set_task_slot( IDLE_TASK_INDEX, CTX_SR_ID, 0xFF03); |
---|
| 214 | _set_task_slot( IDLE_TASK_INDEX, CTX_SP_ID, stack); |
---|
| 215 | _set_task_slot( IDLE_TASK_INDEX, CTX_RA_ID, (unsigned int) &_ctx_eret); |
---|
| 216 | _set_task_slot( IDLE_TASK_INDEX, CTX_EPC_ID, (unsigned int) &_ctx_idle); |
---|
| 217 | _set_task_slot( IDLE_TASK_INDEX, CTX_LTID_ID, IDLE_TASK_INDEX); |
---|
| 218 | _set_task_slot( IDLE_TASK_INDEX, CTX_PTPR_ID, _ptprs[0]); |
---|
| 219 | |
---|
[199] | 220 | #if GIET_DEBUG_INIT |
---|
[238] | 221 | _get_lock(&_tty_put_lock); |
---|
| 222 | _puts("\n[GIET DEBUG] Step 5 for processor "); |
---|
| 223 | _putd(global_pid); |
---|
| 224 | _puts(" / idle task context set\n"); |
---|
| 225 | _release_lock(&_tty_put_lock); |
---|
[199] | 226 | #endif |
---|
[228] | 227 | |
---|
[199] | 228 | // step 6 : each processor initialises SP, SR, PTPR, EPC, registers |
---|
[189] | 229 | // with the values corresponding to the first allocated task, |
---|
[199] | 230 | // and starts the "idle" task if there is no task allocated. |
---|
[189] | 231 | |
---|
[238] | 232 | ltid = 0; |
---|
[199] | 233 | |
---|
[238] | 234 | if (tasks == 0) |
---|
| 235 | { |
---|
| 236 | ltid = IDLE_TASK_INDEX; |
---|
[165] | 237 | |
---|
[228] | 238 | _get_lock(&_tty_put_lock); |
---|
[218] | 239 | _puts("\n[GIET WARNING] No task allocated to processor "); |
---|
[228] | 240 | _putd(global_pid); |
---|
[199] | 241 | _puts(" => idle\n"); |
---|
[228] | 242 | _release_lock (&_tty_put_lock); |
---|
[199] | 243 | } |
---|
| 244 | |
---|
[238] | 245 | unsigned int sp_value = _get_task_slot(ltid, CTX_SP_ID); |
---|
| 246 | unsigned int sr_value = _get_task_slot(ltid, CTX_SR_ID); |
---|
| 247 | unsigned int ptpr_value = _get_task_slot(ltid, CTX_PTPR_ID); |
---|
| 248 | unsigned int epc_value = _get_task_slot(ltid, CTX_EPC_ID); |
---|
[228] | 249 | |
---|
[238] | 250 | _set_task_slot( ltid, CTX_LTID_ID, ltid); |
---|
| 251 | |
---|
[189] | 252 | #if GIET_DEBUG_INIT |
---|
[238] | 253 | _get_lock(&_tty_put_lock); |
---|
| 254 | _puts("\n[GIET DEBUG] step 6 for processor "); |
---|
| 255 | _putd(global_pid); |
---|
| 256 | _puts(" / registers initialised \n"); |
---|
| 257 | _puts("- sp = "); |
---|
| 258 | _putx(sp_value); |
---|
| 259 | _puts("\n"); |
---|
| 260 | _puts("- sr = "); |
---|
| 261 | _putx(sr_value); |
---|
| 262 | _puts("\n"); |
---|
| 263 | _puts("- ptpr = "); |
---|
| 264 | _putx(ptpr_value); |
---|
| 265 | _puts("\n"); |
---|
| 266 | _puts("- epc = "); |
---|
| 267 | _putx(epc_value); |
---|
| 268 | _puts("\n"); |
---|
| 269 | _release_lock(&_tty_put_lock); |
---|
[189] | 270 | #endif |
---|
[165] | 271 | |
---|
[238] | 272 | _get_lock(&_tty_put_lock); |
---|
| 273 | _puts("\n[GIET] Processor "); |
---|
| 274 | _putd( global_pid ); |
---|
| 275 | _puts(" starting user code at cycle "); |
---|
| 276 | _putd( _proctime() ); |
---|
| 277 | _puts("\n"); |
---|
| 278 | _release_lock(&_tty_put_lock); |
---|
| 279 | |
---|
[199] | 280 | // set registers and jump to user code |
---|
[228] | 281 | asm volatile ( |
---|
| 282 | "move $29, %0 \n" /* SP <= ctx[CTX_SP_ID] */ |
---|
| 283 | "mtc0 %1, $12 \n" /* SR <= ctx[CTX_SR_ID] */ |
---|
| 284 | "mtc2 %2, $0 \n" /* PTPR <= ctx[CTX_PTPR_ID] */ |
---|
| 285 | "mtc0 %3, $14 \n" /* EPC <= ctx[CTX_EPC_ID] */ |
---|
| 286 | "eret \n" /* jump to user code */ |
---|
| 287 | "nop \n" |
---|
| 288 | : |
---|
| 289 | : "r" (sp_value), "r" (sr_value), "r" (ptpr_value), "r" (epc_value)); |
---|
[165] | 290 | |
---|
| 291 | } // end _kernel_init() |
---|
[228] | 292 | |
---|
| 293 | // Local Variables: |
---|
| 294 | // tab-width: 4 |
---|
| 295 | // c-basic-offset: 4 |
---|
| 296 | // c-file-offsets:((innamespace . 0)(inline-open . 0)) |
---|
| 297 | // indent-tabs-mode: nil |
---|
| 298 | // End: |
---|
| 299 | // vim: filetype=c:expandtab:shiftwidth=4:tabstop=4:softtabstop=4 |
---|
| 300 | |
---|