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 | /** @addtogroup main
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * @file
|
---|
35 | * @brief Main initialization kernel function for all processors.
|
---|
36 | *
|
---|
37 | * During kernel boot, all processors, after architecture dependent
|
---|
38 | * initialization, start executing code found in this file. After
|
---|
39 | * bringing up all subsystems, control is passed to scheduler().
|
---|
40 | *
|
---|
41 | * The bootstrap processor starts executing main_bsp() while
|
---|
42 | * the application processors start executing main_ap().
|
---|
43 | *
|
---|
44 | * @see scheduler()
|
---|
45 | * @see main_bsp()
|
---|
46 | * @see main_ap()
|
---|
47 | */
|
---|
48 |
|
---|
49 | #include <arch/asm.h>
|
---|
50 | #include <debug.h>
|
---|
51 | #include <context.h>
|
---|
52 | #include <print.h>
|
---|
53 | #include <panic.h>
|
---|
54 | #include <assert.h>
|
---|
55 | #include <config.h>
|
---|
56 | #include <time/clock.h>
|
---|
57 | #include <time/timeout.h>
|
---|
58 | #include <proc/scheduler.h>
|
---|
59 | #include <proc/thread.h>
|
---|
60 | #include <proc/task.h>
|
---|
61 | #include <main/kinit.h>
|
---|
62 | #include <main/version.h>
|
---|
63 | #include <console/kconsole.h>
|
---|
64 | #include <console/console.h>
|
---|
65 | #include <log.h>
|
---|
66 | #include <cpu.h>
|
---|
67 | #include <align.h>
|
---|
68 | #include <interrupt.h>
|
---|
69 | #include <mm/frame.h>
|
---|
70 | #include <mm/page.h>
|
---|
71 | #include <genarch/mm/page_pt.h>
|
---|
72 | #include <mm/km.h>
|
---|
73 | #include <mm/tlb.h>
|
---|
74 | #include <mm/as.h>
|
---|
75 | #include <mm/slab.h>
|
---|
76 | #include <mm/reserve.h>
|
---|
77 | #include <synch/waitq.h>
|
---|
78 | #include <synch/futex.h>
|
---|
79 | #include <synch/workqueue.h>
|
---|
80 | #include <smp/smp_call.h>
|
---|
81 | #include <arch/arch.h>
|
---|
82 | #include <arch.h>
|
---|
83 | #include <arch/faddr.h>
|
---|
84 | #include <ipc/ipc.h>
|
---|
85 | #include <macros.h>
|
---|
86 | #include <adt/btree.h>
|
---|
87 | #include <smp/smp.h>
|
---|
88 | #include <ddi/ddi.h>
|
---|
89 | #include <main/main.h>
|
---|
90 | #include <ipc/event.h>
|
---|
91 | #include <sysinfo/sysinfo.h>
|
---|
92 | #include <sysinfo/stats.h>
|
---|
93 | #include <lib/ra.h>
|
---|
94 |
|
---|
95 | /* Ensure [u]int*_t types are of correct size.
|
---|
96 | *
|
---|
97 | * Probably, this is not the best place for such tests
|
---|
98 | * but this file is compiled on all architectures.
|
---|
99 | */
|
---|
100 | #define CHECK_INT_TYPE_(signness, size) \
|
---|
101 | static_assert(sizeof(signness##size##_t) * 8 == size, \
|
---|
102 | #signness #size "_t does not have " #size " bits");
|
---|
103 |
|
---|
104 | #define CHECK_INT_TYPE(size) \
|
---|
105 | CHECK_INT_TYPE_(int, size); \
|
---|
106 | CHECK_INT_TYPE_(uint, size)
|
---|
107 |
|
---|
108 | CHECK_INT_TYPE(8);
|
---|
109 | CHECK_INT_TYPE(16);
|
---|
110 | CHECK_INT_TYPE(32);
|
---|
111 | CHECK_INT_TYPE(64);
|
---|
112 |
|
---|
113 | /** Global configuration structure. */
|
---|
114 | config_t config = {
|
---|
115 | .identity_configured = false,
|
---|
116 | .non_identity_configured = false,
|
---|
117 | .physmem_end = 0
|
---|
118 | };
|
---|
119 |
|
---|
120 | /** Boot arguments. */
|
---|
121 | char bargs[CONFIG_BOOT_ARGUMENTS_BUFLEN] = {};
|
---|
122 |
|
---|
123 | /** Initial user-space tasks */
|
---|
124 | init_t init = {
|
---|
125 | .cnt = 0
|
---|
126 | };
|
---|
127 |
|
---|
128 | /** Boot allocations. */
|
---|
129 | ballocs_t ballocs = {
|
---|
130 | .base = (uintptr_t) NULL,
|
---|
131 | .size = 0
|
---|
132 | };
|
---|
133 |
|
---|
134 | context_t ctx;
|
---|
135 |
|
---|
136 | /** Lowest safe stack virtual address. */
|
---|
137 | uintptr_t stack_safe = 0;
|
---|
138 |
|
---|
139 | /*
|
---|
140 | * These two functions prevent stack from underflowing during the
|
---|
141 | * kernel boot phase when SP is set to the very top of the reserved
|
---|
142 | * space. The stack could get corrupted by a fooled compiler-generated
|
---|
143 | * pop sequence otherwise.
|
---|
144 | */
|
---|
145 | static void main_bsp_separated_stack(void);
|
---|
146 |
|
---|
147 | #ifdef CONFIG_SMP
|
---|
148 | static void main_ap_separated_stack(void);
|
---|
149 | #endif
|
---|
150 |
|
---|
151 | /** Main kernel routine for bootstrap CPU.
|
---|
152 | *
|
---|
153 | * The code here still runs on the boot stack, which knows nothing about
|
---|
154 | * preemption counts. Because of that, this function cannot directly call
|
---|
155 | * functions that disable or enable preemption (e.g. spinlock_lock()). The
|
---|
156 | * primary task of this function is to calculate address of a new stack and
|
---|
157 | * switch to it.
|
---|
158 | *
|
---|
159 | * Assuming interrupts_disable().
|
---|
160 | *
|
---|
161 | */
|
---|
162 | NO_TRACE void main_bsp(void)
|
---|
163 | {
|
---|
164 | config.cpu_count = 1;
|
---|
165 | config.cpu_active = 1;
|
---|
166 |
|
---|
167 | config.base = hardcoded_load_address;
|
---|
168 | config.kernel_size = ALIGN_UP(hardcoded_ktext_size +
|
---|
169 | hardcoded_kdata_size, PAGE_SIZE);
|
---|
170 | config.stack_size = STACK_SIZE;
|
---|
171 |
|
---|
172 | /* Initialy the stack is placed just after the kernel */
|
---|
173 | config.stack_base = config.base + config.kernel_size;
|
---|
174 |
|
---|
175 | /* Avoid placing stack on top of init */
|
---|
176 | size_t i;
|
---|
177 | for (i = 0; i < init.cnt; i++) {
|
---|
178 | if (overlaps(KA2PA(config.stack_base), config.stack_size,
|
---|
179 | init.tasks[i].paddr, init.tasks[i].size)) {
|
---|
180 | /*
|
---|
181 | * The init task overlaps with the memory behind the
|
---|
182 | * kernel image so it must be in low memory and we can
|
---|
183 | * use PA2KA on the init task's physical address.
|
---|
184 | */
|
---|
185 | config.stack_base = ALIGN_UP(
|
---|
186 | PA2KA(init.tasks[i].paddr) + init.tasks[i].size,
|
---|
187 | config.stack_size);
|
---|
188 | }
|
---|
189 | }
|
---|
190 |
|
---|
191 | /* Avoid placing stack on top of boot allocations. */
|
---|
192 | if (ballocs.size) {
|
---|
193 | if (PA_OVERLAPS(config.stack_base, config.stack_size,
|
---|
194 | ballocs.base, ballocs.size))
|
---|
195 | config.stack_base = ALIGN_UP(ballocs.base +
|
---|
196 | ballocs.size, PAGE_SIZE);
|
---|
197 | }
|
---|
198 |
|
---|
199 | if (config.stack_base < stack_safe)
|
---|
200 | config.stack_base = ALIGN_UP(stack_safe, PAGE_SIZE);
|
---|
201 |
|
---|
202 | context_save(&ctx);
|
---|
203 | context_set(&ctx, FADDR(main_bsp_separated_stack),
|
---|
204 | config.stack_base, STACK_SIZE);
|
---|
205 | context_restore(&ctx);
|
---|
206 | /* not reached */
|
---|
207 | }
|
---|
208 |
|
---|
209 | /** Main kernel routine for bootstrap CPU using new stack.
|
---|
210 | *
|
---|
211 | * Second part of main_bsp().
|
---|
212 | *
|
---|
213 | */
|
---|
214 | void main_bsp_separated_stack(void)
|
---|
215 | {
|
---|
216 | /* Keep this the first thing. */
|
---|
217 | the_initialize(THE);
|
---|
218 |
|
---|
219 | version_print();
|
---|
220 |
|
---|
221 | LOG("\nconfig.base=%p config.kernel_size=%zu"
|
---|
222 | "\nconfig.stack_base=%p config.stack_size=%zu",
|
---|
223 | (void *) config.base, config.kernel_size,
|
---|
224 | (void *) config.stack_base, config.stack_size);
|
---|
225 |
|
---|
226 | #ifdef CONFIG_KCONSOLE
|
---|
227 | /*
|
---|
228 | * kconsole data structures must be initialized very early
|
---|
229 | * because other subsystems will register their respective
|
---|
230 | * commands.
|
---|
231 | */
|
---|
232 | kconsole_init();
|
---|
233 | #endif
|
---|
234 |
|
---|
235 | /*
|
---|
236 | * Exception handler initialization, before architecture
|
---|
237 | * starts adding its own handlers
|
---|
238 | */
|
---|
239 | exc_init();
|
---|
240 |
|
---|
241 | /*
|
---|
242 | * Memory management subsystems initialization.
|
---|
243 | */
|
---|
244 | ARCH_OP(pre_mm_init);
|
---|
245 | km_identity_init();
|
---|
246 | frame_init();
|
---|
247 | slab_cache_init();
|
---|
248 | ra_init();
|
---|
249 | sysinfo_init();
|
---|
250 | btree_init();
|
---|
251 | as_init();
|
---|
252 | page_init();
|
---|
253 | tlb_init();
|
---|
254 | km_non_identity_init();
|
---|
255 | ddi_init();
|
---|
256 | ARCH_OP(post_mm_init);
|
---|
257 | reserve_init();
|
---|
258 | ARCH_OP(pre_smp_init);
|
---|
259 | smp_init();
|
---|
260 |
|
---|
261 | /* Slab must be initialized after we know the number of processors. */
|
---|
262 | slab_enable_cpucache();
|
---|
263 |
|
---|
264 | uint64_t size;
|
---|
265 | const char *size_suffix;
|
---|
266 | bin_order_suffix(zones_total_size(), &size, &size_suffix, false);
|
---|
267 | printf("Detected %u CPU(s), %" PRIu64 " %s free memory\n",
|
---|
268 | config.cpu_count, size, size_suffix);
|
---|
269 |
|
---|
270 | cpu_init();
|
---|
271 | calibrate_delay_loop();
|
---|
272 | ARCH_OP(post_cpu_init);
|
---|
273 |
|
---|
274 | smp_call_init();
|
---|
275 | workq_global_init();
|
---|
276 | clock_counter_init();
|
---|
277 | timeout_init();
|
---|
278 | scheduler_init();
|
---|
279 | task_init();
|
---|
280 | thread_init();
|
---|
281 | futex_init();
|
---|
282 |
|
---|
283 | sysinfo_set_item_data("boot_args", NULL, bargs, str_size(bargs) + 1);
|
---|
284 |
|
---|
285 | if (init.cnt > 0) {
|
---|
286 | size_t i;
|
---|
287 | for (i = 0; i < init.cnt; i++)
|
---|
288 | LOG("init[%zu].addr=%p, init[%zu].size=%zu",
|
---|
289 | i, (void *) init.tasks[i].paddr, i, init.tasks[i].size);
|
---|
290 | } else
|
---|
291 | printf("No init binaries found.\n");
|
---|
292 |
|
---|
293 | ipc_init();
|
---|
294 | event_init();
|
---|
295 | kio_init();
|
---|
296 | log_init();
|
---|
297 | stats_init();
|
---|
298 |
|
---|
299 | /*
|
---|
300 | * Create kernel task.
|
---|
301 | */
|
---|
302 | task_t *kernel = task_create(AS_KERNEL, "kernel");
|
---|
303 | if (!kernel)
|
---|
304 | panic("Cannot create kernel task.");
|
---|
305 |
|
---|
306 | /*
|
---|
307 | * Create the first thread.
|
---|
308 | */
|
---|
309 | thread_t *kinit_thread = thread_create(kinit, NULL, kernel,
|
---|
310 | THREAD_FLAG_UNCOUNTED, "kinit");
|
---|
311 | if (!kinit_thread)
|
---|
312 | panic("Cannot create kinit thread.");
|
---|
313 | thread_ready(kinit_thread);
|
---|
314 |
|
---|
315 | /*
|
---|
316 | * This call to scheduler() will return to kinit,
|
---|
317 | * starting the thread of kernel threads.
|
---|
318 | */
|
---|
319 | scheduler();
|
---|
320 | /* not reached */
|
---|
321 | }
|
---|
322 |
|
---|
323 | #ifdef CONFIG_SMP
|
---|
324 |
|
---|
325 | /** Main kernel routine for application CPUs.
|
---|
326 | *
|
---|
327 | * Executed by application processors, temporary stack
|
---|
328 | * is at ctx.sp which was set during BSP boot.
|
---|
329 | * This function passes control directly to
|
---|
330 | * main_ap_separated_stack().
|
---|
331 | *
|
---|
332 | * Assuming interrupts_disable()'d.
|
---|
333 | *
|
---|
334 | */
|
---|
335 | void main_ap(void)
|
---|
336 | {
|
---|
337 | /*
|
---|
338 | * Incrementing the active CPU counter will guarantee that the
|
---|
339 | * *_init() functions can find out that they need to
|
---|
340 | * do initialization for AP only.
|
---|
341 | */
|
---|
342 | config.cpu_active++;
|
---|
343 |
|
---|
344 | /*
|
---|
345 | * The THE structure is well defined because ctx.sp is used as stack.
|
---|
346 | */
|
---|
347 | the_initialize(THE);
|
---|
348 |
|
---|
349 | ARCH_OP(pre_mm_init);
|
---|
350 | frame_init();
|
---|
351 | page_init();
|
---|
352 | tlb_init();
|
---|
353 | ARCH_OP(post_mm_init);
|
---|
354 |
|
---|
355 | cpu_init();
|
---|
356 | calibrate_delay_loop();
|
---|
357 | ARCH_OP(post_cpu_init);
|
---|
358 |
|
---|
359 | the_copy(THE, (the_t *) CPU->stack);
|
---|
360 |
|
---|
361 | /*
|
---|
362 | * If we woke kmp up before we left the kernel stack, we could
|
---|
363 | * collide with another CPU coming up. To prevent this, we
|
---|
364 | * switch to this cpu's private stack prior to waking kmp up.
|
---|
365 | */
|
---|
366 | context_save(&CPU->saved_context);
|
---|
367 | context_set(&CPU->saved_context, FADDR(main_ap_separated_stack),
|
---|
368 | (uintptr_t) CPU->stack, STACK_SIZE);
|
---|
369 | context_restore(&CPU->saved_context);
|
---|
370 | /* not reached */
|
---|
371 | }
|
---|
372 |
|
---|
373 | /** Main kernel routine for application CPUs using new stack.
|
---|
374 | *
|
---|
375 | * Second part of main_ap().
|
---|
376 | *
|
---|
377 | */
|
---|
378 | void main_ap_separated_stack(void)
|
---|
379 | {
|
---|
380 | smp_call_init();
|
---|
381 |
|
---|
382 | /*
|
---|
383 | * Configure timeouts for this cpu.
|
---|
384 | */
|
---|
385 | timeout_init();
|
---|
386 |
|
---|
387 | waitq_wakeup(&ap_completion_wq, WAKEUP_FIRST);
|
---|
388 | scheduler();
|
---|
389 | /* not reached */
|
---|
390 | }
|
---|
391 |
|
---|
392 | #endif /* CONFIG_SMP */
|
---|
393 |
|
---|
394 | /** @}
|
---|
395 | */
|
---|