source: mainline/kernel/generic/src/main/main.c@ 8a1afd2

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

Remove hardcoded data variables from kernel linker scripts

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