| 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 <arch/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 <mm/vm.h>
|
|---|
| 39 | #include <main/kinit.h>
|
|---|
| 40 | #include <cpu.h>
|
|---|
| 41 | #include <mm/heap.h>
|
|---|
| 42 |
|
|---|
| 43 | #ifdef __SMP__
|
|---|
| 44 | #include <arch/smp/apic.h>
|
|---|
| 45 | #include <arch/smp/mps.h>
|
|---|
| 46 | #endif /* __SMP__ */
|
|---|
| 47 |
|
|---|
| 48 | #include <smp/smp.h>
|
|---|
| 49 |
|
|---|
| 50 | #include <mm/frame.h>
|
|---|
| 51 | #include <mm/page.h>
|
|---|
| 52 | #include <mm/tlb.h>
|
|---|
| 53 | #include <synch/waitq.h>
|
|---|
| 54 |
|
|---|
| 55 | #include <arch.h>
|
|---|
| 56 | #include <arch/faddr.h>
|
|---|
| 57 |
|
|---|
| 58 | char *project = "SPARTAN kernel";
|
|---|
| 59 | char *copyright = "Copyright (C) 2001-2005 Jakub Jermar\nCopyright (C) 2005 HelenOS project";
|
|---|
| 60 |
|
|---|
| 61 | config_t config;
|
|---|
| 62 | context_t ctx;
|
|---|
| 63 |
|
|---|
| 64 | /*
|
|---|
| 65 | * These 'hardcoded' variables will be intialised by
|
|---|
| 66 | * the linker with appropriate sizes and addresses.
|
|---|
| 67 | */
|
|---|
| 68 | __address hardcoded_load_address = 0;
|
|---|
| 69 | __u32 hardcoded_ktext_size = 0;
|
|---|
| 70 | __u32 hardcoded_kdata_size = 0;
|
|---|
| 71 |
|
|---|
| 72 | void main_bsp(void);
|
|---|
| 73 | void main_ap(void);
|
|---|
| 74 |
|
|---|
| 75 | /*
|
|---|
| 76 | * These two functions prevent stack from underflowing during the
|
|---|
| 77 | * kernel boot phase when SP is set to the very top of the reserved
|
|---|
| 78 | * space. The stack could get corrupted by a fooled compiler-generated
|
|---|
| 79 | * pop sequence otherwise.
|
|---|
| 80 | */
|
|---|
| 81 | static void main_bsp_separated_stack(void);
|
|---|
| 82 | static void main_ap_separated_stack(void);
|
|---|
| 83 |
|
|---|
| 84 | /*
|
|---|
| 85 | * Executed by the bootstrap processor. cpu_priority_high()'d
|
|---|
| 86 | */
|
|---|
| 87 | void main_bsp(void)
|
|---|
| 88 | {
|
|---|
| 89 | config.cpu_count = 1;
|
|---|
| 90 | config.cpu_active = 1;
|
|---|
| 91 |
|
|---|
| 92 | config.base = hardcoded_load_address;
|
|---|
| 93 | config.memory_size = CONFIG_MEMORY_SIZE;
|
|---|
| 94 | config.kernel_size = hardcoded_ktext_size + hardcoded_kdata_size + CONFIG_HEAP_SIZE + CONFIG_STACK_SIZE;
|
|---|
| 95 |
|
|---|
| 96 | context_save(&ctx);
|
|---|
| 97 | ctx.sp = config.base + config.kernel_size - SP_DELTA;
|
|---|
| 98 | ctx.pc = FADDR(main_bsp_separated_stack);
|
|---|
| 99 | context_restore(&ctx);
|
|---|
| 100 | /* not reached */
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | void main_bsp_separated_stack(void) {
|
|---|
| 104 | vm_t *m;
|
|---|
| 105 | task_t *k;
|
|---|
| 106 | thread_t *t;
|
|---|
| 107 |
|
|---|
| 108 | arch_pre_mm_init();
|
|---|
| 109 |
|
|---|
| 110 | heap_init(config.base + hardcoded_ktext_size + hardcoded_kdata_size, CONFIG_HEAP_SIZE);
|
|---|
| 111 | frame_init();
|
|---|
| 112 | page_init();
|
|---|
| 113 | tlb_init();
|
|---|
| 114 |
|
|---|
| 115 | arch_post_mm_init();
|
|---|
| 116 |
|
|---|
| 117 | printf("%s\n%s\n", project, copyright);
|
|---|
| 118 |
|
|---|
| 119 | printf("%P: hardcoded_ktext_size=%dK, hardcoded_kdata_size=%dK\n",
|
|---|
| 120 | config.base, hardcoded_ktext_size/1024, hardcoded_kdata_size/1024);
|
|---|
| 121 |
|
|---|
| 122 | arch_late_init();
|
|---|
| 123 |
|
|---|
| 124 | smp_init();
|
|---|
| 125 | printf("config.cpu_count=%d\n", config.cpu_count);
|
|---|
| 126 |
|
|---|
| 127 | cpu_init();
|
|---|
| 128 | calibrate_delay_loop();
|
|---|
| 129 |
|
|---|
| 130 | timeout_init();
|
|---|
| 131 | scheduler_init();
|
|---|
| 132 | task_init();
|
|---|
| 133 | thread_init();
|
|---|
| 134 |
|
|---|
| 135 | /*
|
|---|
| 136 | * Create kernel vm mapping.
|
|---|
| 137 | */
|
|---|
| 138 | m = vm_create();
|
|---|
| 139 | if (!m) panic("can't create kernel vm address space\n");
|
|---|
| 140 |
|
|---|
| 141 | /*
|
|---|
| 142 | * Create kernel task.
|
|---|
| 143 | */
|
|---|
| 144 | k = task_create(m);
|
|---|
| 145 | if (!k) panic("can't create kernel task\n");
|
|---|
| 146 |
|
|---|
| 147 | /*
|
|---|
| 148 | * Create the first thread.
|
|---|
| 149 | */
|
|---|
| 150 | t = thread_create(kinit, NULL, k, 0);
|
|---|
| 151 | if (!t) panic("can't create kinit thread\n");
|
|---|
| 152 |
|
|---|
| 153 | thread_ready(t);
|
|---|
| 154 |
|
|---|
| 155 | /*
|
|---|
| 156 | * This call to scheduler() will return to kinit,
|
|---|
| 157 | * starting the thread of kernel threads.
|
|---|
| 158 | */
|
|---|
| 159 | scheduler();
|
|---|
| 160 | /* not reached */
|
|---|
| 161 | }
|
|---|
| 162 |
|
|---|
| 163 | #ifdef __SMP__
|
|---|
| 164 | /*
|
|---|
| 165 | * Executed by application processors. cpu_priority_high()'d
|
|---|
| 166 | * Temporary stack is at ctx.sp which was set during BP boot.
|
|---|
| 167 | */
|
|---|
| 168 | void main_ap(void)
|
|---|
| 169 | {
|
|---|
| 170 | /*
|
|---|
| 171 | * Incrementing the active CPU counter will guarantee that the
|
|---|
| 172 | * pm_init() will not attempt to build GDT and IDT tables again.
|
|---|
| 173 | * Neither frame_init() will do the complete thing. Neither cpu_init()
|
|---|
| 174 | * will do.
|
|---|
| 175 | */
|
|---|
| 176 | config.cpu_active++;
|
|---|
| 177 |
|
|---|
| 178 | arch_pre_mm_init();
|
|---|
| 179 | frame_init();
|
|---|
| 180 | page_init();
|
|---|
| 181 | arch_post_mm_init();
|
|---|
| 182 |
|
|---|
| 183 | cpu_init();
|
|---|
| 184 | calibrate_delay_loop();
|
|---|
| 185 |
|
|---|
| 186 | l_apic_init();
|
|---|
| 187 | l_apic_debug();
|
|---|
| 188 |
|
|---|
| 189 |
|
|---|
| 190 | /*
|
|---|
| 191 | * If we woke kmp up before we left the kernel stack, we could
|
|---|
| 192 | * collide with another CPU coming up. To prevent this, we
|
|---|
| 193 | * switch to this cpu's private stack prior to waking kmp up.
|
|---|
| 194 | */
|
|---|
| 195 | CPU->saved_context.sp = (__address) &CPU->stack[CPU_STACK_SIZE-SP_DELTA];
|
|---|
| 196 | CPU->saved_context.pc = FADDR(main_ap_separated_stack);
|
|---|
| 197 | context_restore(&CPU->saved_context);
|
|---|
| 198 | /* not reached */
|
|---|
| 199 | }
|
|---|
| 200 |
|
|---|
| 201 | void main_ap_separated_stack(void)
|
|---|
| 202 | {
|
|---|
| 203 | /*
|
|---|
| 204 | * Configure timeouts for this cpu.
|
|---|
| 205 | */
|
|---|
| 206 | timeout_init();
|
|---|
| 207 |
|
|---|
| 208 | waitq_wakeup(&ap_completion_wq, WAKEUP_FIRST);
|
|---|
| 209 | scheduler();
|
|---|
| 210 | /* not reached */
|
|---|
| 211 | }
|
|---|
| 212 | #endif /* __SMP__*/
|
|---|