| 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 |
|
|---|
| 29 | #include <arch/asm.h>
|
|---|
| 30 | #include <context.h>
|
|---|
| 31 | #include <print.h>
|
|---|
| 32 | #include <panic.h>
|
|---|
| 33 | #include <config.h>
|
|---|
| 34 | #include <time/clock.h>
|
|---|
| 35 | #include <proc/scheduler.h>
|
|---|
| 36 | #include <proc/thread.h>
|
|---|
| 37 | #include <proc/task.h>
|
|---|
| 38 | #include <main/kinit.h>
|
|---|
| 39 | #include <console/kconsole.h>
|
|---|
| 40 | #include <cpu.h>
|
|---|
| 41 | #include <align.h>
|
|---|
| 42 |
|
|---|
| 43 | #ifdef CONFIG_SMP
|
|---|
| 44 | #include <arch/smp/apic.h>
|
|---|
| 45 | #include <arch/smp/mps.h>
|
|---|
| 46 | #endif /* CONFIG_SMP */
|
|---|
| 47 |
|
|---|
| 48 | #include <smp/smp.h>
|
|---|
| 49 |
|
|---|
| 50 | #include <arch/mm/memory_init.h>
|
|---|
| 51 | #include <mm/heap.h>
|
|---|
| 52 | #include <mm/frame.h>
|
|---|
| 53 | #include <mm/page.h>
|
|---|
| 54 | #include <mm/tlb.h>
|
|---|
| 55 | #include <mm/vm.h>
|
|---|
| 56 |
|
|---|
| 57 | #include <synch/waitq.h>
|
|---|
| 58 |
|
|---|
| 59 | #include <arch/arch.h>
|
|---|
| 60 | #include <arch.h>
|
|---|
| 61 | #include <arch/faddr.h>
|
|---|
| 62 |
|
|---|
| 63 | #include <typedefs.h>
|
|---|
| 64 |
|
|---|
| 65 | char *project = "SPARTAN kernel";
|
|---|
| 66 | char *release = RELEASE " (" NAME ")";
|
|---|
| 67 | #ifdef TAG
|
|---|
| 68 | char *rr_delimiter = "\n";
|
|---|
| 69 | char *revision = TAG;
|
|---|
| 70 | #else
|
|---|
| 71 | char *rr_delimiter = "";
|
|---|
| 72 | char *revision = "";
|
|---|
| 73 | #endif
|
|---|
| 74 | char *copyright = "Copyright (C) 2001-2005 HelenOS project";
|
|---|
| 75 |
|
|---|
| 76 | config_t config;
|
|---|
| 77 | context_t ctx;
|
|---|
| 78 |
|
|---|
| 79 | /*
|
|---|
| 80 | * These 'hardcoded' variables will be intialised by
|
|---|
| 81 | * the linker or the low level assembler code with
|
|---|
| 82 | * appropriate sizes and addresses.
|
|---|
| 83 | */
|
|---|
| 84 | __address hardcoded_load_address = 0;
|
|---|
| 85 | size_t hardcoded_ktext_size = 0;
|
|---|
| 86 | size_t hardcoded_kdata_size = 0;
|
|---|
| 87 |
|
|---|
| 88 | __address init_addr = 0;
|
|---|
| 89 | size_t init_size = 0;
|
|---|
| 90 |
|
|---|
| 91 | /*
|
|---|
| 92 | * Size of memory in bytes taken by kernel and heap.
|
|---|
| 93 | */
|
|---|
| 94 | static size_t kernel_size;
|
|---|
| 95 |
|
|---|
| 96 | /*
|
|---|
| 97 | * Size of heap.
|
|---|
| 98 | */
|
|---|
| 99 | static size_t heap_size;
|
|---|
| 100 |
|
|---|
| 101 |
|
|---|
| 102 | /*
|
|---|
| 103 | * Extra space between heap and stack
|
|---|
| 104 | * enforced by alignment requirements.
|
|---|
| 105 | */
|
|---|
| 106 | static size_t heap_delta;
|
|---|
| 107 |
|
|---|
| 108 | void main_bsp(void);
|
|---|
| 109 | void main_ap(void);
|
|---|
| 110 |
|
|---|
| 111 | /*
|
|---|
| 112 | * These two functions prevent stack from underflowing during the
|
|---|
| 113 | * kernel boot phase when SP is set to the very top of the reserved
|
|---|
| 114 | * space. The stack could get corrupted by a fooled compiler-generated
|
|---|
| 115 | * pop sequence otherwise.
|
|---|
| 116 | */
|
|---|
| 117 | static void main_bsp_separated_stack(void);
|
|---|
| 118 | static void main_ap_separated_stack(void);
|
|---|
| 119 |
|
|---|
| 120 | /** Bootstrap CPU main kernel routine
|
|---|
| 121 | *
|
|---|
| 122 | * Initializes the kernel by bootstrap CPU.
|
|---|
| 123 | *
|
|---|
| 124 | * Assuming interrupts_disable().
|
|---|
| 125 | *
|
|---|
| 126 | */
|
|---|
| 127 | void main_bsp(void)
|
|---|
| 128 | {
|
|---|
| 129 | config.cpu_count = 1;
|
|---|
| 130 | config.cpu_active = 1;
|
|---|
| 131 | config.base = hardcoded_load_address;
|
|---|
| 132 | config.memory_size = get_memory_size();
|
|---|
| 133 |
|
|---|
| 134 | heap_size = CONFIG_HEAP_SIZE + (config.memory_size/FRAME_SIZE)*sizeof(frame_t);
|
|---|
| 135 | kernel_size = ALIGN(hardcoded_ktext_size + hardcoded_kdata_size + heap_size, PAGE_SIZE);
|
|---|
| 136 | heap_delta = kernel_size - (hardcoded_ktext_size + hardcoded_kdata_size + heap_size);
|
|---|
| 137 |
|
|---|
| 138 | config.kernel_size = kernel_size + CONFIG_STACK_SIZE;
|
|---|
| 139 |
|
|---|
| 140 | context_save(&ctx);
|
|---|
| 141 | early_mapping(config.base + hardcoded_ktext_size + hardcoded_kdata_size, CONFIG_STACK_SIZE + heap_size + heap_delta);
|
|---|
| 142 | context_set(&ctx, FADDR(main_bsp_separated_stack), config.base + kernel_size, CONFIG_STACK_SIZE);
|
|---|
| 143 | context_restore(&ctx);
|
|---|
| 144 | /* not reached */
|
|---|
| 145 | }
|
|---|
| 146 |
|
|---|
| 147 |
|
|---|
| 148 | /** Bootstrap CPU main kernel routine stack wrapper
|
|---|
| 149 | *
|
|---|
| 150 | * Second part of main_bsp().
|
|---|
| 151 | *
|
|---|
| 152 | */
|
|---|
| 153 | void main_bsp_separated_stack(void)
|
|---|
| 154 | {
|
|---|
| 155 | vm_t *m;
|
|---|
| 156 | task_t *k;
|
|---|
| 157 | thread_t *t;
|
|---|
| 158 |
|
|---|
| 159 | the_initialize(THE);
|
|---|
| 160 |
|
|---|
| 161 | /*
|
|---|
| 162 | * kconsole data structures must be initialized very early
|
|---|
| 163 | * because other subsystems will register their respective
|
|---|
| 164 | * commands.
|
|---|
| 165 | */
|
|---|
| 166 | kconsole_init();
|
|---|
| 167 |
|
|---|
| 168 | arch_pre_mm_init();
|
|---|
| 169 | early_heap_init(config.base + hardcoded_ktext_size + hardcoded_kdata_size, heap_size + heap_delta);
|
|---|
| 170 | frame_init();
|
|---|
| 171 | page_init();
|
|---|
| 172 | tlb_init();
|
|---|
| 173 | arch_post_mm_init();
|
|---|
| 174 |
|
|---|
| 175 | printf("%s, release %s%s%s\n%s\n", project, release, rr_delimiter, revision, copyright);
|
|---|
| 176 | printf("%P: hardcoded_ktext_size=%dK, hardcoded_kdata_size=%dK\n",
|
|---|
| 177 | config.base, hardcoded_ktext_size/1024, hardcoded_kdata_size/1024);
|
|---|
| 178 |
|
|---|
| 179 | arch_pre_smp_init();
|
|---|
| 180 | smp_init();
|
|---|
| 181 | printf("config.memory_size=%dM\n", config.memory_size/(1024*1024));
|
|---|
| 182 | printf("config.cpu_count=%d\n", config.cpu_count);
|
|---|
| 183 |
|
|---|
| 184 | cpu_init();
|
|---|
| 185 |
|
|---|
| 186 | calibrate_delay_loop();
|
|---|
| 187 |
|
|---|
| 188 | timeout_init();
|
|---|
| 189 | scheduler_init();
|
|---|
| 190 | task_init();
|
|---|
| 191 | thread_init();
|
|---|
| 192 |
|
|---|
| 193 | /*
|
|---|
| 194 | * Create kernel vm mapping.
|
|---|
| 195 | */
|
|---|
| 196 | m = vm_create(GET_PTL0_ADDRESS());
|
|---|
| 197 | if (!m)
|
|---|
| 198 | panic("can't create kernel vm address space\n");
|
|---|
| 199 |
|
|---|
| 200 | /*
|
|---|
| 201 | * Create kernel task.
|
|---|
| 202 | */
|
|---|
| 203 | k = task_create(m);
|
|---|
| 204 | if (!k)
|
|---|
| 205 | panic("can't create kernel task\n");
|
|---|
| 206 |
|
|---|
| 207 | /*
|
|---|
| 208 | * Create the first thread.
|
|---|
| 209 | */
|
|---|
| 210 | t = thread_create(kinit, NULL, k, 0);
|
|---|
| 211 | if (!t)
|
|---|
| 212 | panic("can't create kinit thread\n");
|
|---|
| 213 | thread_ready(t);
|
|---|
| 214 | /*
|
|---|
| 215 | * This call to scheduler() will return to kinit,
|
|---|
| 216 | * starting the thread of kernel threads.
|
|---|
| 217 | */
|
|---|
| 218 | scheduler();
|
|---|
| 219 | /* not reached */
|
|---|
| 220 | }
|
|---|
| 221 |
|
|---|
| 222 |
|
|---|
| 223 | #ifdef CONFIG_SMP
|
|---|
| 224 | /** Application CPUs main kernel routine
|
|---|
| 225 | *
|
|---|
| 226 | * Executed by application processors, temporary stack
|
|---|
| 227 | * is at ctx.sp which was set during BP boot.
|
|---|
| 228 | *
|
|---|
| 229 | * Assuming interrupts_disable()'d.
|
|---|
| 230 | *
|
|---|
| 231 | */
|
|---|
| 232 | void main_ap(void)
|
|---|
| 233 | {
|
|---|
| 234 | /*
|
|---|
| 235 | * Incrementing the active CPU counter will guarantee that the
|
|---|
| 236 | * pm_init() will not attempt to build GDT and IDT tables again.
|
|---|
| 237 | * Neither frame_init() will do the complete thing. Neither cpu_init()
|
|---|
| 238 | * will do.
|
|---|
| 239 | */
|
|---|
| 240 | config.cpu_active++;
|
|---|
| 241 |
|
|---|
| 242 | /*
|
|---|
| 243 | * The THE structure is well defined because ctx.sp is used as stack.
|
|---|
| 244 | */
|
|---|
| 245 | the_initialize(THE);
|
|---|
| 246 |
|
|---|
| 247 | arch_pre_mm_init();
|
|---|
| 248 | frame_init();
|
|---|
| 249 | page_init();
|
|---|
| 250 | tlb_init();
|
|---|
| 251 | arch_post_mm_init();
|
|---|
| 252 |
|
|---|
| 253 | cpu_init();
|
|---|
| 254 |
|
|---|
| 255 | calibrate_delay_loop();
|
|---|
| 256 |
|
|---|
| 257 | l_apic_init();
|
|---|
| 258 | l_apic_debug();
|
|---|
| 259 |
|
|---|
| 260 | the_copy(THE, (the_t *) CPU->stack);
|
|---|
| 261 |
|
|---|
| 262 | /*
|
|---|
| 263 | * If we woke kmp up before we left the kernel stack, we could
|
|---|
| 264 | * collide with another CPU coming up. To prevent this, we
|
|---|
| 265 | * switch to this cpu's private stack prior to waking kmp up.
|
|---|
| 266 | */
|
|---|
| 267 | context_set(&CPU->saved_context, FADDR(main_ap_separated_stack), (__address) CPU->stack, CPU_STACK_SIZE);
|
|---|
| 268 | context_restore(&CPU->saved_context);
|
|---|
| 269 | /* not reached */
|
|---|
| 270 | }
|
|---|
| 271 |
|
|---|
| 272 |
|
|---|
| 273 | /** Application CPUs main kernel routine stack wrapper
|
|---|
| 274 | *
|
|---|
| 275 | * Second part of main_ap().
|
|---|
| 276 | *
|
|---|
| 277 | */
|
|---|
| 278 | void main_ap_separated_stack(void)
|
|---|
| 279 | {
|
|---|
| 280 | /*
|
|---|
| 281 | * Configure timeouts for this cpu.
|
|---|
| 282 | */
|
|---|
| 283 | timeout_init();
|
|---|
| 284 |
|
|---|
| 285 | waitq_wakeup(&ap_completion_wq, WAKEUP_FIRST);
|
|---|
| 286 | scheduler();
|
|---|
| 287 | /* not reached */
|
|---|
| 288 | }
|
|---|
| 289 | #endif /* CONFIG_SMP */
|
|---|