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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since a35b458 was a35b458, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 7 years ago

style: Remove trailing whitespace on _all_ lines, including empty ones, for particular file types.

Command used: tools/srepl '\s\+$' '' -- *.c *.h *.py *.sh *.s *.S *.ag

Currently, whitespace on empty lines is very inconsistent.
There are two basic choices: Either remove the whitespace, or keep empty lines
indented to the level of surrounding code. The former is AFAICT more common,
and also much easier to do automatically.

Alternatively, we could write script for automatic indentation, and use that
instead. However, if such a script exists, it's possible to use the indented
style locally, by having the editor apply relevant conversions on load/save,
without affecting remote repository. IMO, it makes more sense to adopt
the simpler rule.

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