source: mainline/kernel/generic/src/main/main.c@ 69146b93

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 69146b93 was 8a64e81e, checked in by Adam Hraska <adam.hraska+hos@…>, 13 years ago

workq: Add work queues: allow blocking work items, queuing items from interrupt handlers.

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