[f761f1eb] | 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 |
|
---|
[9179d0a] | 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 |
|
---|
[f761f1eb] | 45 | #include <arch/asm.h>
|
---|
[d5d2a3f] | 46 | #include <context.h>
|
---|
[f761f1eb] | 47 | #include <print.h>
|
---|
[02a99d2] | 48 | #include <panic.h>
|
---|
[940cac0] | 49 | #include <debug.h>
|
---|
[f761f1eb] | 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>
|
---|
[5fe5f1e] | 56 | #include <main/version.h>
|
---|
[f4338d2] | 57 | #include <console/kconsole.h>
|
---|
[f761f1eb] | 58 | #include <cpu.h>
|
---|
[e0cdb7b6] | 59 | #include <align.h>
|
---|
[aace6624] | 60 | #include <interrupt.h>
|
---|
[a55f97f] | 61 | #include <arch/mm/memory_init.h>
|
---|
[f761f1eb] | 62 | #include <mm/frame.h>
|
---|
| 63 | #include <mm/page.h>
|
---|
[6d7ffa65] | 64 | #include <genarch/mm/page_pt.h>
|
---|
[169587a] | 65 | #include <mm/tlb.h>
|
---|
[20d50a1] | 66 | #include <mm/as.h>
|
---|
[4e147a6] | 67 | #include <mm/slab.h>
|
---|
[f761f1eb] | 68 | #include <synch/waitq.h>
|
---|
[9aa72b4] | 69 | #include <synch/futex.h>
|
---|
[393f631] | 70 | #include <arch/arch.h>
|
---|
[f761f1eb] | 71 | #include <arch.h>
|
---|
[f2ffad4] | 72 | #include <arch/faddr.h>
|
---|
[ac5d02b] | 73 | #include <typedefs.h>
|
---|
[6d9c49a] | 74 | #include <ipc/ipc.h>
|
---|
[93165be] | 75 | #include <macros.h>
|
---|
[2810636] | 76 | #include <adt/btree.h>
|
---|
[874621f] | 77 | #include <console/klog.h>
|
---|
[ac5d02b] | 78 |
|
---|
[5fe5f1e] | 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 |
|
---|
[e459f12] | 85 | /** Global configuration structure. */
|
---|
| 86 | config_t config = {
|
---|
| 87 | .mm_initialized = false
|
---|
| 88 | };
|
---|
| 89 |
|
---|
| 90 | /** Initial user-space tasks */
|
---|
| 91 | init_t init = {
|
---|
| 92 | 0
|
---|
| 93 | };
|
---|
[5fe5f1e] | 94 |
|
---|
[f761f1eb] | 95 | context_t ctx;
|
---|
| 96 |
|
---|
[880de6e] | 97 | /**
|
---|
| 98 | * These 'hardcoded' variables will be intialized by
|
---|
[dcbc8be] | 99 | * the linker or the low level assembler code with
|
---|
| 100 | * appropriate sizes and addresses.
|
---|
[f761f1eb] | 101 | */
|
---|
[2217ac3] | 102 | __address hardcoded_load_address = 0;
|
---|
[ac5d02b] | 103 | size_t hardcoded_ktext_size = 0;
|
---|
| 104 | size_t hardcoded_kdata_size = 0;
|
---|
[f761f1eb] | 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);
|
---|
[80d2bdb] | 116 | #ifdef CONFIG_SMP
|
---|
[f761f1eb] | 117 | static void main_ap_separated_stack(void);
|
---|
[80d2bdb] | 118 | #endif
|
---|
[f761f1eb] | 119 |
|
---|
[b6d4566] | 120 | #define CONFIG_STACK_SIZE ((1<<STACK_FRAMES)*STACK_SIZE)
|
---|
| 121 |
|
---|
[7f0837c] | 122 | /** Main kernel routine for bootstrap CPU.
|
---|
[673104e] | 123 | *
|
---|
| 124 | * Initializes the kernel by bootstrap CPU.
|
---|
[5fe5f1e] | 125 | * This function passes control directly to
|
---|
| 126 | * main_bsp_separated_stack().
|
---|
[673104e] | 127 | *
|
---|
[22f7769] | 128 | * Assuming interrupts_disable().
|
---|
[673104e] | 129 | *
|
---|
[f761f1eb] | 130 | */
|
---|
| 131 | void main_bsp(void)
|
---|
| 132 | {
|
---|
[085d973] | 133 | __address stackaddr;
|
---|
| 134 |
|
---|
[f761f1eb] | 135 | config.cpu_count = 1;
|
---|
| 136 | config.cpu_active = 1;
|
---|
[d6e8529] | 137 |
|
---|
[aed4eca] | 138 | config.base = hardcoded_load_address;
|
---|
| 139 | config.memory_size = get_memory_size();
|
---|
[393f631] | 140 |
|
---|
[085d973] | 141 | config.kernel_size = ALIGN_UP(hardcoded_ktext_size + hardcoded_kdata_size, PAGE_SIZE);
|
---|
| 142 | stackaddr = config.base + config.kernel_size;
|
---|
[b6b576c] | 143 |
|
---|
[085d973] | 144 | /* Avoid placing kernel on top of init */
|
---|
[b6b576c] | 145 | count_t i;
|
---|
| 146 | bool overlap = false;
|
---|
| 147 | for (i = 0; i < init.cnt; i++)
|
---|
[93165be] | 148 | if (PA_overlaps(stackaddr, CONFIG_STACK_SIZE, init.tasks[i].addr, init.tasks[i].size)) {
|
---|
[b6b576c] | 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)
|
---|
[085d973] | 155 | config.kernel_size += CONFIG_STACK_SIZE;
|
---|
[393f631] | 156 |
|
---|
[be50915] | 157 | context_save(&ctx);
|
---|
[b6d4566] | 158 | context_set(&ctx, FADDR(main_bsp_separated_stack), stackaddr, THREAD_STACK_SIZE);
|
---|
[be50915] | 159 | context_restore(&ctx);
|
---|
[f761f1eb] | 160 | /* not reached */
|
---|
| 161 | }
|
---|
| 162 |
|
---|
[673104e] | 163 |
|
---|
[7f0837c] | 164 | /** Main kernel routine for bootstrap CPU using new stack.
|
---|
[673104e] | 165 | *
|
---|
| 166 | * Second part of main_bsp().
|
---|
| 167 | *
|
---|
| 168 | */
|
---|
[fde6429] | 169 | void main_bsp_separated_stack(void)
|
---|
| 170 | {
|
---|
[f761f1eb] | 171 | task_t *k;
|
---|
| 172 | thread_t *t;
|
---|
[9aa72b4] | 173 | count_t i;
|
---|
[8f91729] | 174 |
|
---|
[bcdd9aa] | 175 | the_initialize(THE);
|
---|
[9aa72b4] | 176 |
|
---|
[ff3b3197] | 177 | /*
|
---|
| 178 | * kconsole data structures must be initialized very early
|
---|
| 179 | * because other subsystems will register their respective
|
---|
| 180 | * commands.
|
---|
| 181 | */
|
---|
| 182 | kconsole_init();
|
---|
[b6b576c] | 183 |
|
---|
[ef67bab] | 184 | /*
|
---|
| 185 | * Exception handler initialization, before architecture
|
---|
[0132630] | 186 | * starts adding its own handlers
|
---|
[aace6624] | 187 | */
|
---|
| 188 | exc_init();
|
---|
[fc1e4f6] | 189 |
|
---|
| 190 | /*
|
---|
| 191 | * Memory management subsystems initialization.
|
---|
| 192 | */
|
---|
[1fbbcd6] | 193 | arch_pre_mm_init();
|
---|
[018d957e] | 194 | frame_init(); /* Initialize at least 1 memory segment big enough for slab to work */
|
---|
[8e1ea655] | 195 | slab_cache_init();
|
---|
[2810636] | 196 | btree_init();
|
---|
[ef67bab] | 197 | as_init();
|
---|
[7eade45] | 198 | page_init();
|
---|
| 199 | tlb_init();
|
---|
[e459f12] | 200 | config.mm_initialized = true;
|
---|
[d6e5cbc] | 201 | arch_post_mm_init();
|
---|
[0132630] | 202 |
|
---|
[018d957e] | 203 | version_print();
|
---|
[dadb68e] | 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);
|
---|
[f761f1eb] | 205 |
|
---|
[7453929] | 206 | arch_pre_smp_init();
|
---|
[ed0dd65] | 207 | smp_init();
|
---|
[018d957e] | 208 |
|
---|
| 209 | slab_enable_cpucache(); /* Slab must be initialized AFTER we know the number of processors */
|
---|
[c5613b72] | 210 |
|
---|
[dadb68e] | 211 | printf("config.memory_size=%zdM\n", config.memory_size >> 20);
|
---|
[280a27e] | 212 | printf("config.cpu_count=%zd\n", config.cpu_count);
|
---|
[f761f1eb] | 213 | cpu_init();
|
---|
[6f8a426] | 214 |
|
---|
[f761f1eb] | 215 | calibrate_delay_loop();
|
---|
[d6e5cbc] | 216 | clock_counter_init();
|
---|
[f761f1eb] | 217 | timeout_init();
|
---|
| 218 | scheduler_init();
|
---|
| 219 | task_init();
|
---|
| 220 | thread_init();
|
---|
[9aa72b4] | 221 | futex_init();
|
---|
[874621f] | 222 | klog_init();
|
---|
[6c68b97] | 223 |
|
---|
[b6b576c] | 224 | for (i = 0; i < init.cnt; i++)
|
---|
[dadb68e] | 225 | printf("init[%zd].addr=%.*p, init[%zd].size=%zd\n", i, sizeof(__address) * 2, init.tasks[i].addr, i, init.tasks[i].size);
|
---|
[6d9c49a] | 226 |
|
---|
| 227 | ipc_init();
|
---|
[9aa72b4] | 228 |
|
---|
[f761f1eb] | 229 | /*
|
---|
| 230 | * Create kernel task.
|
---|
| 231 | */
|
---|
[ff14c520] | 232 | k = task_create(AS_KERNEL, "KERNEL");
|
---|
[393f631] | 233 | if (!k)
|
---|
| 234 | panic("can't create kernel task\n");
|
---|
[6f8a426] | 235 |
|
---|
[f761f1eb] | 236 | /*
|
---|
| 237 | * Create the first thread.
|
---|
| 238 | */
|
---|
[ff14c520] | 239 | t = thread_create(kinit, NULL, k, 0, "kinit");
|
---|
[393f631] | 240 | if (!t)
|
---|
| 241 | panic("can't create kinit thread\n");
|
---|
[f761f1eb] | 242 | thread_ready(t);
|
---|
[6f8a426] | 243 |
|
---|
[f761f1eb] | 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 |
|
---|
[673104e] | 252 |
|
---|
[5f85c91] | 253 | #ifdef CONFIG_SMP
|
---|
[7f0837c] | 254 | /** Main kernel routine for application CPUs.
|
---|
[673104e] | 255 | *
|
---|
| 256 | * Executed by application processors, temporary stack
|
---|
| 257 | * is at ctx.sp which was set during BP boot.
|
---|
[5fe5f1e] | 258 | * This function passes control directly to
|
---|
| 259 | * main_ap_separated_stack().
|
---|
[673104e] | 260 | *
|
---|
[22f7769] | 261 | * Assuming interrupts_disable()'d.
|
---|
[673104e] | 262 | *
|
---|
[f761f1eb] | 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 |
|
---|
[7ce9284] | 274 | /*
|
---|
| 275 | * The THE structure is well defined because ctx.sp is used as stack.
|
---|
| 276 | */
|
---|
| 277 | the_initialize(THE);
|
---|
[c0b45fa] | 278 |
|
---|
[f07bba5] | 279 | arch_pre_mm_init();
|
---|
[f761f1eb] | 280 | frame_init();
|
---|
| 281 | page_init();
|
---|
[ce031f0] | 282 | tlb_init();
|
---|
[f07bba5] | 283 | arch_post_mm_init();
|
---|
[c0b45fa] | 284 |
|
---|
[f761f1eb] | 285 | cpu_init();
|
---|
[c0b45fa] | 286 |
|
---|
[f761f1eb] | 287 | calibrate_delay_loop();
|
---|
| 288 |
|
---|
| 289 | l_apic_init();
|
---|
| 290 | l_apic_debug();
|
---|
| 291 |
|
---|
[7ce9284] | 292 | the_copy(THE, (the_t *) CPU->stack);
|
---|
[f761f1eb] | 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 | */
|
---|
[4b2c872d] | 299 | context_set(&CPU->saved_context, FADDR(main_ap_separated_stack), (__address) CPU->stack, CPU_STACK_SIZE);
|
---|
[be50915] | 300 | context_restore(&CPU->saved_context);
|
---|
[f761f1eb] | 301 | /* not reached */
|
---|
| 302 | }
|
---|
| 303 |
|
---|
[673104e] | 304 |
|
---|
[7f0837c] | 305 | /** Main kernel routine for application CPUs using new stack.
|
---|
[673104e] | 306 | *
|
---|
| 307 | * Second part of main_ap().
|
---|
| 308 | *
|
---|
| 309 | */
|
---|
[f761f1eb] | 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 | }
|
---|
[5f85c91] | 321 | #endif /* CONFIG_SMP */
|
---|