source: mainline/kernel/generic/src/main/main.c@ 9e889f6

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

Turn wait queue into a kobject usable by uspace

In order to provide an elegant synchronization mechanism for userspace,
this commit adds syscalls to create, sleep on and wakeup from a wait
queue.

  • Property mode set to 100644
File size: 9.7 KB
RevLine 
[f761f1eb]1/*
[df4ed85]2 * Copyright (c) 2001-2004 Jakub Jermar
[f761f1eb]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
[e88eb48]29/** @addtogroup kernel_generic
[b45c443]30 * @{
31 */
32
[9179d0a]33/**
[b45c443]34 * @file
[13a638d]35 * @brief Main initialization kernel function for all processors.
[9179d0a]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
[f761f1eb]49#include <arch/asm.h>
[63e27ef]50#include <debug.h>
[d5d2a3f]51#include <context.h>
[bab75df6]52#include <stdio.h>
[02a99d2]53#include <panic.h>
[63e27ef]54#include <assert.h>
[f761f1eb]55#include <config.h>
56#include <time/clock.h>
[b3f8fb7]57#include <time/timeout.h>
[f761f1eb]58#include <proc/scheduler.h>
59#include <proc/thread.h>
60#include <proc/task.h>
61#include <main/kinit.h>
[5fe5f1e]62#include <main/version.h>
[f4338d2]63#include <console/kconsole.h>
[5d67baa]64#include <console/console.h>
[91db0280]65#include <log.h>
[f761f1eb]66#include <cpu.h>
[e0cdb7b6]67#include <align.h>
[aace6624]68#include <interrupt.h>
[b5746a2]69#include <str.h>
[f761f1eb]70#include <mm/frame.h>
71#include <mm/page.h>
[6d7ffa65]72#include <genarch/mm/page_pt.h>
[622f409]73#include <mm/km.h>
[169587a]74#include <mm/tlb.h>
[20d50a1]75#include <mm/as.h>
[4e147a6]76#include <mm/slab.h>
[8d308b9]77#include <mm/reserve.h>
[f761f1eb]78#include <synch/waitq.h>
[9aa72b4]79#include <synch/futex.h>
[d314571]80#include <synch/syswaitq.h>
[393f631]81#include <arch/arch.h>
[f761f1eb]82#include <arch.h>
[f2ffad4]83#include <arch/faddr.h>
[6d9c49a]84#include <ipc/ipc.h>
[93165be]85#include <macros.h>
[2810636]86#include <adt/btree.h>
[5fe5f1e]87#include <smp/smp.h>
[f8ddd17]88#include <ddi/ddi.h>
[4b241f3]89#include <main/main.h>
[13a638d]90#include <ipc/event.h>
[d9fae235]91#include <sysinfo/sysinfo.h>
[9dae191e]92#include <sysinfo/stats.h>
[41deb2a]93#include <lib/ra.h>
[ce732e74]94#include <cap/cap.h>
[5fe5f1e]95
[7c3fb9b]96/*
97 * Ensure [u]int*_t types are of correct size.
[328cc31]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) \
[63e27ef]103 static_assert(sizeof(signness##size##_t) * 8 == size, \
[328cc31]104 #signness #size "_t does not have " #size " bits");
[9e40355e]105
[328cc31]106#define CHECK_INT_TYPE(size) \
[9e40355e]107 CHECK_INT_TYPE_(int, size); \
108 CHECK_INT_TYPE_(uint, size)
[328cc31]109
110CHECK_INT_TYPE(8);
111CHECK_INT_TYPE(16);
112CHECK_INT_TYPE(32);
113CHECK_INT_TYPE(64);
114
[e459f12]115/** Global configuration structure. */
[55896b6]116config_t config = {
117 .identity_configured = false,
[40c8c17]118 .non_identity_configured = false,
119 .physmem_end = 0
[55896b6]120};
[e459f12]121
[3b3faf51]122/** Boot arguments. */
[3bacee1]123char bargs[CONFIG_BOOT_ARGUMENTS_BUFLEN] = { };
[3b3faf51]124
[e459f12]125/** Initial user-space tasks */
126init_t init = {
[6c441cf8]127 .cnt = 0
[e459f12]128};
[5fe5f1e]129
[61e90dd]130/** Boot allocations. */
131ballocs_t ballocs = {
[0b4a67a]132 .base = (uintptr_t) NULL,
[61e90dd]133 .size = 0
134};
135
[f761f1eb]136context_t ctx;
137
[c0855a0]138/** Lowest safe stack virtual address. */
[263bda2]139uintptr_t stack_safe = 0;
[deaa22f]140
[f761f1eb]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);
[263bda2]148
[80d2bdb]149#ifdef CONFIG_SMP
[f761f1eb]150static void main_ap_separated_stack(void);
[80d2bdb]151#endif
[f761f1eb]152
[7f0837c]153/** Main kernel routine for bootstrap CPU.
[673104e]154 *
[7a4202d]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.
[673104e]160 *
[22f7769]161 * Assuming interrupts_disable().
[673104e]162 *
[f761f1eb]163 */
[7a0359b]164NO_TRACE void main_bsp(void)
[f761f1eb]165{
166 config.cpu_count = 1;
167 config.cpu_active = 1;
[a35b458]168
[8a1afd2]169 config.base = (uintptr_t) kernel_load_address;
170
171 config.kernel_size =
172 ALIGN_UP((uintptr_t) kdata_end - config.base, PAGE_SIZE);
[a35b458]173
[95d45482]174 // NOTE: All kernel stacks must be aligned to STACK_SIZE,
175 // see get_stack_base().
[d1da1ff2]176
[d59718e]177 /* Place the stack after the kernel, init and ballocs. */
[d1da1ff2]178 config.stack_base =
179 ALIGN_UP(config.base + config.kernel_size, STACK_SIZE);
[d59718e]180 config.stack_size = STACK_SIZE;
[a35b458]181
[deaa22f]182 /* Avoid placing stack on top of init */
[98000fb]183 size_t i;
[deaa22f]184 for (i = 0; i < init.cnt; i++) {
[d59718e]185 uintptr_t p = init.tasks[i].paddr + init.tasks[i].size;
[d1da1ff2]186 uintptr_t bottom = PA2KA(ALIGN_UP(p, STACK_SIZE));
[d59718e]187
188 if (config.stack_base < bottom)
189 config.stack_base = bottom;
[deaa22f]190 }
[a35b458]191
[61e90dd]192 /* Avoid placing stack on top of boot allocations. */
193 if (ballocs.size) {
[d59718e]194 uintptr_t bottom =
[d1da1ff2]195 ALIGN_UP(ballocs.base + ballocs.size, STACK_SIZE);
[d59718e]196 if (config.stack_base < bottom)
197 config.stack_base = bottom;
[61e90dd]198 }
[a35b458]199
[deaa22f]200 if (config.stack_base < stack_safe)
[d1da1ff2]201 config.stack_base = ALIGN_UP(stack_safe, STACK_SIZE);
[a35b458]202
[be50915]203 context_save(&ctx);
[26aafe8]204 context_set(&ctx, FADDR(main_bsp_separated_stack),
205 config.stack_base, STACK_SIZE);
[be50915]206 context_restore(&ctx);
[f761f1eb]207 /* not reached */
208}
209
[7f0837c]210/** Main kernel routine for bootstrap CPU using new stack.
[673104e]211 *
212 * Second part of main_bsp().
213 *
214 */
[263bda2]215void main_bsp_separated_stack(void)
[fde6429]216{
[7a4202d]217 /* Keep this the first thing. */
[a6e55886]218 current_initialize(CURRENT);
[a35b458]219
[7a4202d]220 version_print();
[a35b458]221
[7e752b2]222 LOG("\nconfig.base=%p config.kernel_size=%zu"
223 "\nconfig.stack_base=%p config.stack_size=%zu",
[1caeb2d]224 (void *) config.base, config.kernel_size,
225 (void *) config.stack_base, config.stack_size);
[a35b458]226
[76fca31]227#ifdef CONFIG_KCONSOLE
[ff3b3197]228 /*
229 * kconsole data structures must be initialized very early
230 * because other subsystems will register their respective
231 * commands.
232 */
[263bda2]233 kconsole_init();
[76fca31]234#endif
[a35b458]235
[ef67bab]236 /*
237 * Exception handler initialization, before architecture
[0132630]238 * starts adding its own handlers
[aace6624]239 */
[263bda2]240 exc_init();
[a35b458]241
[fc1e4f6]242 /*
243 * Memory management subsystems initialization.
[b84aaba]244 */
[36df4109]245 ARCH_OP(pre_mm_init);
[622f409]246 km_identity_init();
[263bda2]247 frame_init();
248 slab_cache_init();
[dabbe28]249 ra_init();
[263bda2]250 sysinfo_init();
251 btree_init();
252 as_init();
253 page_init();
254 tlb_init();
[622f409]255 km_non_identity_init();
[263bda2]256 ddi_init();
[36df4109]257 ARCH_OP(post_mm_init);
[8d308b9]258 reserve_init();
[36df4109]259 ARCH_OP(pre_smp_init);
[263bda2]260 smp_init();
[a35b458]261
[f8ddd17]262 /* Slab must be initialized after we know the number of processors. */
[263bda2]263 slab_enable_cpucache();
[a35b458]264
[933cadf]265 uint64_t size;
266 const char *size_suffix;
267 bin_order_suffix(zones_total_size(), &size, &size_suffix, false);
268 printf("Detected %u CPU(s), %" PRIu64 " %s free memory\n",
269 config.cpu_count, size, size_suffix);
[a35b458]270
[263bda2]271 cpu_init();
272 calibrate_delay_loop();
[36df4109]273 ARCH_OP(post_cpu_init);
[49e6c6b4]274
[263bda2]275 clock_counter_init();
276 timeout_init();
277 scheduler_init();
[ce732e74]278 caps_init();
[263bda2]279 task_init();
280 thread_init();
281 futex_init();
[d314571]282 sys_waitq_init();
[3b3faf51]283
284 sysinfo_set_item_data("boot_args", NULL, bargs, str_size(bargs) + 1);
285
[c06eb06]286 if (init.cnt > 0) {
[98000fb]287 size_t i;
[c06eb06]288 for (i = 0; i < init.cnt; i++)
[7e752b2]289 LOG("init[%zu].addr=%p, init[%zu].size=%zu",
[edd7c63c]290 i, (void *) init.tasks[i].paddr, i, init.tasks[i].size);
[c06eb06]291 } else
[ae5aa90]292 printf("No init binaries found.\n");
[a35b458]293
[263bda2]294 ipc_init();
295 event_init();
[6fa9a99d]296 kio_init();
[91db0280]297 log_init();
[263bda2]298 stats_init();
[a35b458]299
[f761f1eb]300 /*
301 * Create kernel task.
302 */
[b84aaba]303 task_t *kernel = task_create(AS_KERNEL, "kernel");
304 if (!kernel)
[f651e80]305 panic("Cannot create kernel task.");
[a35b458]306
[f761f1eb]307 /*
308 * Create the first thread.
309 */
[6eef3c4]310 thread_t *kinit_thread = thread_create(kinit, NULL, kernel,
311 THREAD_FLAG_UNCOUNTED, "kinit");
[b84aaba]312 if (!kinit_thread)
[f651e80]313 panic("Cannot create kinit thread.");
[263bda2]314 thread_ready(kinit_thread);
[a35b458]315
[f761f1eb]316 /*
317 * This call to scheduler() will return to kinit,
318 * starting the thread of kernel threads.
319 */
320 scheduler();
321 /* not reached */
322}
323
[5f85c91]324#ifdef CONFIG_SMP
[263bda2]325
[7f0837c]326/** Main kernel routine for application CPUs.
[673104e]327 *
328 * Executed by application processors, temporary stack
[26678e5]329 * is at ctx.sp which was set during BSP boot.
[5fe5f1e]330 * This function passes control directly to
331 * main_ap_separated_stack().
[673104e]332 *
[22f7769]333 * Assuming interrupts_disable()'d.
[673104e]334 *
[f761f1eb]335 */
336void main_ap(void)
337{
338 /*
339 * Incrementing the active CPU counter will guarantee that the
[26678e5]340 * *_init() functions can find out that they need to
341 * do initialization for AP only.
[f761f1eb]342 */
343 config.cpu_active++;
[a35b458]344
[7ce9284]345 /*
[a6e55886]346 * The CURRENT structure is well defined because ctx.sp is used as stack.
[7ce9284]347 */
[a6e55886]348 current_initialize(CURRENT);
[a35b458]349
[36df4109]350 ARCH_OP(pre_mm_init);
[f761f1eb]351 frame_init();
352 page_init();
[ce031f0]353 tlb_init();
[36df4109]354 ARCH_OP(post_mm_init);
[a35b458]355
[f761f1eb]356 cpu_init();
357 calibrate_delay_loop();
[36df4109]358 ARCH_OP(post_cpu_init);
[a35b458]359
[a6e55886]360 current_copy(CURRENT, (current_t *) CPU->stack);
[a35b458]361
[f761f1eb]362 /*
363 * If we woke kmp up before we left the kernel stack, we could
364 * collide with another CPU coming up. To prevent this, we
365 * switch to this cpu's private stack prior to waking kmp up.
366 */
[b24786a]367 context_save(&CPU->saved_context);
[f8ddd17]368 context_set(&CPU->saved_context, FADDR(main_ap_separated_stack),
[26aafe8]369 (uintptr_t) CPU->stack, STACK_SIZE);
[be50915]370 context_restore(&CPU->saved_context);
[f761f1eb]371 /* not reached */
372}
373
[7f0837c]374/** Main kernel routine for application CPUs using new stack.
[673104e]375 *
376 * Second part of main_ap().
377 *
378 */
[f761f1eb]379void main_ap_separated_stack(void)
380{
381 /*
382 * Configure timeouts for this cpu.
383 */
384 timeout_init();
[a35b458]385
[f761f1eb]386 waitq_wakeup(&ap_completion_wq, WAKEUP_FIRST);
387 scheduler();
388 /* not reached */
389}
[263bda2]390
[5f85c91]391#endif /* CONFIG_SMP */
[b45c443]392
[8e3bf3e2]393/** @}
[b45c443]394 */
Note: See TracBrowser for help on using the repository browser.