source: mainline/kernel/generic/src/main/main.c@ b1c57a8

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since b1c57a8 was b1c57a8, checked in by Jakub Jermar <jakub@…>, 11 years ago

Merge from lp:~adam-hraska+lp/helenos/rcu/.

Only merge from the feature branch and resolve all conflicts.

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