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

Last change on this file since 25939997 was 25939997, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 18 months ago

Make separate-stack-scheduler a loop with persistent context

We can see scheduler as a looping idle thread in which a thread
is repeatedly retrieved from a queue, prepared, switched to,
returned from, and cleaned up after.

IMO this is a more natural view of the process.

  • Property mode set to 100644
File size: 8.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 <stdio.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/syswaitq.h>
80#include <arch/arch.h>
81#include <arch.h>
82#include <ipc/ipc.h>
83#include <macros.h>
84#include <smp/smp.h>
85#include <ddi/ddi.h>
86#include <main/main.h>
87#include <ipc/event.h>
88#include <sysinfo/sysinfo.h>
89#include <sysinfo/stats.h>
90#include <lib/ra.h>
91#include <cap/cap.h>
92
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(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/** Boot arguments. */
120char bargs[CONFIG_BOOT_ARGUMENTS_BUFLEN] = { };
121
122/** Initial user-space tasks */
123init_t init = {
124 .cnt = 0
125};
126
127/** Boot allocations. */
128ballocs_t ballocs = {
129 .base = (uintptr_t) NULL,
130 .size = 0
131};
132
133static context_t ctx;
134
135// NOTE: All kernel stacks must be aligned to STACK_SIZE, see CURRENT.
136static const size_t bootstrap_stack_size = STACK_SIZE;
137static _Alignas(STACK_SIZE) uint8_t bootstrap_stack[STACK_SIZE];
138
139/* Just a convenient value for some assembly code. */
140uint8_t *const bootstrap_stack_top = bootstrap_stack + STACK_SIZE;
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 */
165_NO_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
175 context_create(&ctx, main_bsp_separated_stack,
176 bootstrap_stack, bootstrap_stack_size);
177 context_restore(&ctx);
178 /* not reached */
179}
180
181/** Main kernel routine for bootstrap CPU using new stack.
182 *
183 * Second part of main_bsp().
184 *
185 */
186void main_bsp_separated_stack(void)
187{
188 /* Keep this the first thing. */
189 current_initialize(CURRENT);
190
191 version_print();
192
193 LOG("\nconfig.base=%p config.kernel_size=%zu",
194 (void *) config.base, config.kernel_size);
195
196#ifdef CONFIG_KCONSOLE
197 /*
198 * kconsole data structures must be initialized very early
199 * because other subsystems will register their respective
200 * commands.
201 */
202 kconsole_init();
203#endif
204
205 /*
206 * Exception handler initialization, before architecture
207 * starts adding its own handlers
208 */
209 exc_init();
210
211 /*
212 * Memory management subsystems initialization.
213 */
214 ARCH_OP(pre_mm_init);
215 km_identity_init();
216 frame_init();
217 slab_cache_init();
218 malloc_init();
219 ra_init();
220 sysinfo_init();
221 as_init();
222 page_init();
223 tlb_init();
224 km_non_identity_init();
225 ddi_init();
226 ARCH_OP(post_mm_init);
227 reserve_init();
228 ARCH_OP(pre_smp_init);
229 smp_init();
230
231 /* Slab must be initialized after we know the number of processors. */
232 slab_enable_cpucache();
233
234 uint64_t size;
235 const char *size_suffix;
236 bin_order_suffix(zones_total_size(), &size, &size_suffix, false);
237 printf("Detected %u CPU(s), %" PRIu64 " %s free memory\n",
238 config.cpu_count, size, size_suffix);
239
240 cpu_init();
241 calibrate_delay_loop();
242 ARCH_OP(post_cpu_init);
243
244 clock_counter_init();
245 timeout_init();
246 scheduler_init();
247 caps_init();
248 task_init();
249 thread_init();
250 sys_waitq_init();
251
252 sysinfo_set_item_data("boot_args", NULL, bargs, str_size(bargs) + 1);
253
254 if (init.cnt > 0) {
255 size_t i;
256 for (i = 0; i < init.cnt; i++)
257 LOG("init[%zu].addr=%p, init[%zu].size=%zu",
258 i, (void *) init.tasks[i].paddr, i, init.tasks[i].size);
259 } else
260 printf("No init binaries found.\n");
261
262 ipc_init();
263 event_init();
264 kio_init();
265 log_init();
266 stats_init();
267
268 /*
269 * Create kernel task.
270 */
271 task_t *kernel = task_create(AS_KERNEL, "kernel");
272 if (!kernel)
273 panic("Cannot create kernel task.");
274
275 /*
276 * Create the first thread.
277 */
278 thread_t *kinit_thread = thread_create(kinit, NULL, kernel,
279 THREAD_FLAG_UNCOUNTED, "kinit");
280 if (!kinit_thread)
281 panic("Cannot create kinit thread.");
282 thread_start(kinit_thread);
283 thread_detach(kinit_thread);
284
285 /*
286 * This call to scheduler_run() will return to kinit,
287 * starting the thread of kernel threads.
288 */
289 current_copy(CURRENT, (current_t *) CPU_LOCAL->stack);
290 context_replace(scheduler_run, CPU_LOCAL->stack, STACK_SIZE);
291 /* not reached */
292}
293
294#ifdef CONFIG_SMP
295
296/** Main kernel routine for application CPUs.
297 *
298 * Executed by application processors, temporary stack
299 * is at ctx.sp which was set during BSP boot.
300 * This function passes control directly to
301 * main_ap_separated_stack().
302 *
303 * Assuming interrupts_disable()'d.
304 *
305 */
306void main_ap(void)
307{
308 /*
309 * Incrementing the active CPU counter will guarantee that the
310 * *_init() functions can find out that they need to
311 * do initialization for AP only.
312 */
313 config.cpu_active++;
314
315 /*
316 * The CURRENT structure is well defined because ctx.sp is used as stack.
317 */
318 current_initialize(CURRENT);
319
320 ARCH_OP(pre_mm_init);
321 frame_init();
322 page_init();
323 tlb_init();
324 ARCH_OP(post_mm_init);
325
326 cpu_init();
327 calibrate_delay_loop();
328 ARCH_OP(post_cpu_init);
329
330 /*
331 * If we woke kmp up before we left the kernel stack, we could
332 * collide with another CPU coming up. To prevent this, we
333 * switch to this cpu's private stack prior to waking kmp up.
334 */
335 current_copy(CURRENT, (current_t *) CPU_LOCAL->stack);
336 context_replace(main_ap_separated_stack, CPU_LOCAL->stack, STACK_SIZE);
337 /* not reached */
338}
339
340/** Main kernel routine for application CPUs using new stack.
341 *
342 * Second part of main_ap().
343 *
344 */
345void main_ap_separated_stack(void)
346{
347 /*
348 * Configure timeouts for this cpu.
349 */
350 timeout_init();
351
352 semaphore_up(&ap_completion_semaphore);
353 scheduler_run();
354 /* not reached */
355}
356
357#endif /* CONFIG_SMP */
358
359/** @}
360 */
Note: See TracBrowser for help on using the repository browser.