Rev 1434 | Rev 1702 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 1 | jermar | 1 | /* |
| 2 | * Copyright (C) 2001-2004 Jakub Jermar |
||
| 3 | * All rights reserved. |
||
| 4 | * |
||
| 5 | * Redistribution and use in source and binary forms, with or without |
||
| 6 | * modification, are permitted provided that the following conditions |
||
| 7 | * are met: |
||
| 8 | * |
||
| 9 | * - Redistributions of source code must retain the above copyright |
||
| 10 | * notice, this list of conditions and the following disclaimer. |
||
| 11 | * - Redistributions in binary form must reproduce the above copyright |
||
| 12 | * notice, this list of conditions and the following disclaimer in the |
||
| 13 | * documentation and/or other materials provided with the distribution. |
||
| 14 | * - The name of the author may not be used to endorse or promote products |
||
| 15 | * derived from this software without specific prior written permission. |
||
| 16 | * |
||
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
||
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
||
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
||
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
||
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
||
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
||
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||
| 27 | */ |
||
| 28 | |||
| 1248 | jermar | 29 | /** |
| 30 | * @file main.c |
||
| 31 | * @brief Main initialization kernel function for all processors. |
||
| 32 | * |
||
| 33 | * During kernel boot, all processors, after architecture dependent |
||
| 34 | * initialization, start executing code found in this file. After |
||
| 35 | * bringing up all subsystems, control is passed to scheduler(). |
||
| 36 | * |
||
| 37 | * The bootstrap processor starts executing main_bsp() while |
||
| 38 | * the application processors start executing main_ap(). |
||
| 39 | * |
||
| 40 | * @see scheduler() |
||
| 41 | * @see main_bsp() |
||
| 42 | * @see main_ap() |
||
| 43 | */ |
||
| 44 | |||
| 1 | jermar | 45 | #include <arch/asm.h> |
| 97 | jermar | 46 | #include <context.h> |
| 1 | jermar | 47 | #include <print.h> |
| 68 | decky | 48 | #include <panic.h> |
| 561 | decky | 49 | #include <debug.h> |
| 1 | jermar | 50 | #include <config.h> |
| 51 | #include <time/clock.h> |
||
| 52 | #include <proc/scheduler.h> |
||
| 53 | #include <proc/thread.h> |
||
| 54 | #include <proc/task.h> |
||
| 55 | #include <main/kinit.h> |
||
| 675 | jermar | 56 | #include <main/version.h> |
| 518 | jermar | 57 | #include <console/kconsole.h> |
| 1 | jermar | 58 | #include <cpu.h> |
| 402 | jermar | 59 | #include <align.h> |
| 578 | palkovsky | 60 | #include <interrupt.h> |
| 146 | cejka | 61 | #include <arch/mm/memory_init.h> |
| 1 | jermar | 62 | #include <mm/frame.h> |
| 63 | #include <mm/page.h> |
||
| 684 | jermar | 64 | #include <genarch/mm/page_pt.h> |
| 5 | jermar | 65 | #include <mm/tlb.h> |
| 703 | jermar | 66 | #include <mm/as.h> |
| 759 | palkovsky | 67 | #include <mm/slab.h> |
| 1 | jermar | 68 | #include <synch/waitq.h> |
| 1109 | jermar | 69 | #include <synch/futex.h> |
| 210 | decky | 70 | #include <arch/arch.h> |
| 1 | jermar | 71 | #include <arch.h> |
| 76 | jermar | 72 | #include <arch/faddr.h> |
| 106 | jermar | 73 | #include <typedefs.h> |
| 955 | palkovsky | 74 | #include <ipc/ipc.h> |
| 1063 | palkovsky | 75 | #include <macros.h> |
| 1164 | jermar | 76 | #include <adt/btree.h> |
| 1595 | palkovsky | 77 | #include <console/klog.h> |
| 106 | jermar | 78 | |
| 675 | jermar | 79 | #ifdef CONFIG_SMP |
| 80 | #include <arch/smp/apic.h> |
||
| 81 | #include <arch/smp/mps.h> |
||
| 82 | #endif /* CONFIG_SMP */ |
||
| 83 | #include <smp/smp.h> |
||
| 84 | |||
| 1315 | jermar | 85 | /** Global configuration structure. */ |
| 86 | config_t config = { |
||
| 87 | .mm_initialized = false |
||
| 88 | }; |
||
| 675 | jermar | 89 | |
| 1315 | jermar | 90 | /** Initial user-space tasks */ |
| 91 | init_t init = { |
||
| 92 | |||
| 93 | }; |
||
| 94 | |||
| 1 | jermar | 95 | context_t ctx; |
| 96 | |||
| 523 | jermar | 97 | /** |
| 98 | * These 'hardcoded' variables will be intialized by |
||
| 105 | jermar | 99 | * the linker or the low level assembler code with |
| 100 | * appropriate sizes and addresses. |
||
| 1 | jermar | 101 | */ |
| 37 | jermar | 102 | __address hardcoded_load_address = 0; |
| 106 | jermar | 103 | size_t hardcoded_ktext_size = 0; |
| 104 | size_t hardcoded_kdata_size = 0; |
||
| 1 | jermar | 105 | |
| 106 | void main_bsp(void); |
||
| 107 | void main_ap(void); |
||
| 108 | |||
| 109 | /* |
||
| 110 | * These two functions prevent stack from underflowing during the |
||
| 111 | * kernel boot phase when SP is set to the very top of the reserved |
||
| 112 | * space. The stack could get corrupted by a fooled compiler-generated |
||
| 113 | * pop sequence otherwise. |
||
| 114 | */ |
||
| 115 | static void main_bsp_separated_stack(void); |
||
| 625 | palkovsky | 116 | #ifdef CONFIG_SMP |
| 1 | jermar | 117 | static void main_ap_separated_stack(void); |
| 625 | palkovsky | 118 | #endif |
| 1 | jermar | 119 | |
| 1138 | jermar | 120 | #define CONFIG_STACK_SIZE ((1<<STACK_FRAMES)*STACK_SIZE) |
| 121 | |||
| 1229 | jermar | 122 | /** Main kernel routine for bootstrap CPU. |
| 108 | decky | 123 | * |
| 124 | * Initializes the kernel by bootstrap CPU. |
||
| 675 | jermar | 125 | * This function passes control directly to |
| 126 | * main_bsp_separated_stack(). |
||
| 108 | decky | 127 | * |
| 413 | jermar | 128 | * Assuming interrupts_disable(). |
| 108 | decky | 129 | * |
| 1 | jermar | 130 | */ |
| 131 | void main_bsp(void) |
||
| 132 | { |
||
| 814 | palkovsky | 133 | __address stackaddr; |
| 134 | |||
| 1 | jermar | 135 | config.cpu_count = 1; |
| 136 | config.cpu_active = 1; |
||
| 651 | decky | 137 | |
| 369 | jermar | 138 | config.base = hardcoded_load_address; |
| 139 | config.memory_size = get_memory_size(); |
||
| 210 | decky | 140 | |
| 814 | palkovsky | 141 | config.kernel_size = ALIGN_UP(hardcoded_ktext_size + hardcoded_kdata_size, PAGE_SIZE); |
| 142 | stackaddr = config.base + config.kernel_size; |
||
| 1037 | decky | 143 | |
| 814 | palkovsky | 144 | /* Avoid placing kernel on top of init */ |
| 1037 | decky | 145 | count_t i; |
| 146 | bool overlap = false; |
||
| 147 | for (i = 0; i < init.cnt; i++) |
||
| 1063 | palkovsky | 148 | if (PA_overlaps(stackaddr, CONFIG_STACK_SIZE, init.tasks[i].addr, init.tasks[i].size)) { |
| 1037 | decky | 149 | stackaddr = ALIGN_UP(init.tasks[i].addr + init.tasks[i].size, CONFIG_STACK_SIZE); |
| 150 | init.tasks[i].size = ALIGN_UP(init.tasks[i].size, CONFIG_STACK_SIZE) + CONFIG_STACK_SIZE; |
||
| 151 | overlap = true; |
||
| 152 | } |
||
| 153 | |||
| 154 | if (!overlap) |
||
| 814 | palkovsky | 155 | config.kernel_size += CONFIG_STACK_SIZE; |
| 210 | decky | 156 | |
| 47 | jermar | 157 | context_save(&ctx); |
| 1138 | jermar | 158 | context_set(&ctx, FADDR(main_bsp_separated_stack), stackaddr, THREAD_STACK_SIZE); |
| 47 | jermar | 159 | context_restore(&ctx); |
| 1 | jermar | 160 | /* not reached */ |
| 161 | } |
||
| 162 | |||
| 108 | decky | 163 | |
| 1229 | jermar | 164 | /** Main kernel routine for bootstrap CPU using new stack. |
| 108 | decky | 165 | * |
| 166 | * Second part of main_bsp(). |
||
| 167 | * |
||
| 168 | */ |
||
| 174 | jermar | 169 | void main_bsp_separated_stack(void) |
| 170 | { |
||
| 1 | jermar | 171 | task_t *k; |
| 172 | thread_t *t; |
||
| 1109 | jermar | 173 | count_t i; |
| 207 | decky | 174 | |
| 184 | jermar | 175 | the_initialize(THE); |
| 1109 | jermar | 176 | |
| 517 | jermar | 177 | /* |
| 178 | * kconsole data structures must be initialized very early |
||
| 179 | * because other subsystems will register their respective |
||
| 180 | * commands. |
||
| 181 | */ |
||
| 182 | kconsole_init(); |
||
| 1037 | decky | 183 | |
| 756 | jermar | 184 | /* |
| 185 | * Exception handler initialization, before architecture |
||
| 673 | jermar | 186 | * starts adding its own handlers |
| 578 | palkovsky | 187 | */ |
| 188 | exc_init(); |
||
| 755 | jermar | 189 | |
| 190 | /* |
||
| 191 | * Memory management subsystems initialization. |
||
| 192 | */ |
||
| 26 | jermar | 193 | arch_pre_mm_init(); |
| 1101 | jermar | 194 | frame_init(); /* Initialize at least 1 memory segment big enough for slab to work */ |
| 789 | palkovsky | 195 | slab_cache_init(); |
| 1164 | jermar | 196 | btree_init(); |
| 756 | jermar | 197 | as_init(); |
| 23 | jermar | 198 | page_init(); |
| 199 | tlb_init(); |
||
| 1315 | jermar | 200 | config.mm_initialized = true; |
| 1434 | palkovsky | 201 | arch_post_mm_init(); |
| 1101 | jermar | 202 | |
| 673 | jermar | 203 | version_print(); |
| 1389 | decky | 204 | printf("%.*p: hardcoded_ktext_size=%zdK, hardcoded_kdata_size=%zdK\n", sizeof(__address) * 2, config.base, hardcoded_ktext_size >> 10, hardcoded_kdata_size >> 10); |
| 1 | jermar | 205 | |
| 503 | jermar | 206 | arch_pre_smp_init(); |
| 34 | jermar | 207 | smp_init(); |
| 1101 | jermar | 208 | |
| 209 | slab_enable_cpucache(); /* Slab must be initialized AFTER we know the number of processors */ |
||
| 773 | palkovsky | 210 | |
| 1389 | decky | 211 | printf("config.memory_size=%zdM\n", config.memory_size >> 20); |
| 1196 | cejka | 212 | printf("config.cpu_count=%zd\n", config.cpu_count); |
| 1 | jermar | 213 | cpu_init(); |
| 860 | decky | 214 | |
| 1 | jermar | 215 | calibrate_delay_loop(); |
| 1434 | palkovsky | 216 | clock_counter_init(); |
| 1 | jermar | 217 | timeout_init(); |
| 218 | scheduler_init(); |
||
| 219 | task_init(); |
||
| 220 | thread_init(); |
||
| 1109 | jermar | 221 | futex_init(); |
| 1595 | palkovsky | 222 | klog_init(); |
| 628 | decky | 223 | |
| 1037 | decky | 224 | for (i = 0; i < init.cnt; i++) |
| 1389 | decky | 225 | printf("init[%zd].addr=%.*p, init[%zd].size=%zd\n", i, sizeof(__address) * 2, init.tasks[i].addr, i, init.tasks[i].size); |
| 955 | palkovsky | 226 | |
| 227 | ipc_init(); |
||
| 1109 | jermar | 228 | |
| 1 | jermar | 229 | /* |
| 230 | * Create kernel task. |
||
| 231 | */ |
||
| 1062 | jermar | 232 | k = task_create(AS_KERNEL, "KERNEL"); |
| 210 | decky | 233 | if (!k) |
| 234 | panic("can't create kernel task\n"); |
||
| 860 | decky | 235 | |
| 1 | jermar | 236 | /* |
| 237 | * Create the first thread. |
||
| 238 | */ |
||
| 1062 | jermar | 239 | t = thread_create(kinit, NULL, k, 0, "kinit"); |
| 210 | decky | 240 | if (!t) |
| 241 | panic("can't create kinit thread\n"); |
||
| 1 | jermar | 242 | thread_ready(t); |
| 860 | decky | 243 | |
| 1 | jermar | 244 | /* |
| 245 | * This call to scheduler() will return to kinit, |
||
| 246 | * starting the thread of kernel threads. |
||
| 247 | */ |
||
| 248 | scheduler(); |
||
| 249 | /* not reached */ |
||
| 250 | } |
||
| 251 | |||
| 108 | decky | 252 | |
| 458 | decky | 253 | #ifdef CONFIG_SMP |
| 1229 | jermar | 254 | /** Main kernel routine for application CPUs. |
| 108 | decky | 255 | * |
| 256 | * Executed by application processors, temporary stack |
||
| 257 | * is at ctx.sp which was set during BP boot. |
||
| 675 | jermar | 258 | * This function passes control directly to |
| 259 | * main_ap_separated_stack(). |
||
| 108 | decky | 260 | * |
| 413 | jermar | 261 | * Assuming interrupts_disable()'d. |
| 108 | decky | 262 | * |
| 1 | jermar | 263 | */ |
| 264 | void main_ap(void) |
||
| 265 | { |
||
| 266 | /* |
||
| 267 | * Incrementing the active CPU counter will guarantee that the |
||
| 268 | * pm_init() will not attempt to build GDT and IDT tables again. |
||
| 269 | * Neither frame_init() will do the complete thing. Neither cpu_init() |
||
| 270 | * will do. |
||
| 271 | */ |
||
| 272 | config.cpu_active++; |
||
| 273 | |||
| 192 | jermar | 274 | /* |
| 275 | * The THE structure is well defined because ctx.sp is used as stack. |
||
| 276 | */ |
||
| 277 | the_initialize(THE); |
||
| 298 | decky | 278 | |
| 26 | jermar | 279 | arch_pre_mm_init(); |
| 1 | jermar | 280 | frame_init(); |
| 281 | page_init(); |
||
| 389 | jermar | 282 | tlb_init(); |
| 26 | jermar | 283 | arch_post_mm_init(); |
| 298 | decky | 284 | |
| 1 | jermar | 285 | cpu_init(); |
| 298 | decky | 286 | |
| 1 | jermar | 287 | calibrate_delay_loop(); |
| 288 | |||
| 289 | l_apic_init(); |
||
| 290 | l_apic_debug(); |
||
| 291 | |||
| 192 | jermar | 292 | the_copy(THE, (the_t *) CPU->stack); |
| 1 | jermar | 293 | |
| 294 | /* |
||
| 295 | * If we woke kmp up before we left the kernel stack, we could |
||
| 296 | * collide with another CPU coming up. To prevent this, we |
||
| 297 | * switch to this cpu's private stack prior to waking kmp up. |
||
| 298 | */ |
||
| 414 | jermar | 299 | context_set(&CPU->saved_context, FADDR(main_ap_separated_stack), (__address) CPU->stack, CPU_STACK_SIZE); |
| 47 | jermar | 300 | context_restore(&CPU->saved_context); |
| 1 | jermar | 301 | /* not reached */ |
| 302 | } |
||
| 303 | |||
| 108 | decky | 304 | |
| 1229 | jermar | 305 | /** Main kernel routine for application CPUs using new stack. |
| 108 | decky | 306 | * |
| 307 | * Second part of main_ap(). |
||
| 308 | * |
||
| 309 | */ |
||
| 1 | jermar | 310 | void main_ap_separated_stack(void) |
| 311 | { |
||
| 312 | /* |
||
| 313 | * Configure timeouts for this cpu. |
||
| 314 | */ |
||
| 315 | timeout_init(); |
||
| 316 | |||
| 317 | waitq_wakeup(&ap_completion_wq, WAKEUP_FIRST); |
||
| 318 | scheduler(); |
||
| 319 | /* not reached */ |
||
| 320 | } |
||
| 458 | decky | 321 | #endif /* CONFIG_SMP */ |