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