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