source: mainline/kernel/generic/src/main/kinit.c@ 1066041

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

rcu: Added preemptible RCU's core API implementation.

  • Property mode set to 100644
File size: 6.8 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
[8e3bf3e2]29/** @addtogroup main
[b45c443]30 * @{
31 */
32
[9179d0a]33/**
[b45c443]34 * @file
[2f57690]35 * @brief Kernel initialization thread.
[9179d0a]36 *
37 * This file contains kinit kernel thread which carries out
38 * high level system initialization.
39 *
40 * This file is responsible for finishing SMP configuration
41 * and creation of userspace init tasks.
42 */
43
[f761f1eb]44#include <main/kinit.h>
[2677758]45#include <config.h>
46#include <arch.h>
[f761f1eb]47#include <proc/scheduler.h>
48#include <proc/task.h>
49#include <proc/thread.h>
[c98e6ee]50#include <proc/program.h>
[f761f1eb]51#include <panic.h>
52#include <func.h>
53#include <cpu.h>
54#include <arch/asm.h>
55#include <mm/page.h>
56#include <arch/mm/page.h>
[20d50a1]57#include <mm/as.h>
[ff3b3197]58#include <mm/frame.h>
[32817cc]59#include <mm/km.h>
[9c0a9b3]60#include <print.h>
[399ccd9]61#include <memstr.h>
[ff3b3197]62#include <console/console.h>
[aace6624]63#include <interrupt.h>
[f4338d2]64#include <console/kconsole.h>
[1077d91]65#include <security/cap.h>
[d4b5542]66#include <lib/rd.h>
[b3f8fb7]67#include <ipc/ipc.h>
[20f1597]68#include <debug.h>
[19f857a]69#include <str.h>
[9dae191e]70#include <sysinfo/stats.h>
[32817cc]71#include <align.h>
[f761f1eb]72
[5f85c91]73#ifdef CONFIG_SMP
[26678e5]74#include <smp/smp.h>
[5f85c91]75#endif /* CONFIG_SMP */
[f761f1eb]76
77#include <synch/waitq.h>
78#include <synch/spinlock.h>
[8a64e81e]79#include <synch/workqueue.h>
[181a746]80#include <synch/rcu.h>
[f761f1eb]81
[c19a4169]82#define ALIVE_CHARS 4
83
[753d851]84#ifdef CONFIG_KCONSOLE
[c19a4169]85static char alive[ALIVE_CHARS] = "-\\|/";
[753d851]86#endif
[c19a4169]87
[da581872]88#define INIT_PREFIX "init:"
89#define INIT_PREFIX_LEN 5
[20f1597]90
[880de6e]91/** Kernel initialization thread.
92 *
93 * kinit takes care of higher level kernel
94 * initialization (i.e. thread creation,
95 * userspace initialization etc.).
96 *
97 * @param arg Not used.
98 */
[f761f1eb]99void kinit(void *arg)
100{
[76fca31]101 thread_t *thread;
[2f57690]102
[2cb5e64]103 /*
104 * Detach kinit as nobody will call thread_join_timeout() on it.
105 */
106 thread_detach(THREAD);
[8a64e81e]107
[22f7769]108 interrupts_disable();
[2f57690]109
[181a746]110 /* Start processing RCU callbacks. RCU is fully functional afterwards. */
111 rcu_kinit_init();
112
[8a64e81e]113 /*
114 * Start processing work queue items. Some may have been queued during boot.
115 */
116 workq_global_worker_init();
117
[2f57690]118#ifdef CONFIG_SMP
[f761f1eb]119 if (config.cpu_count > 1) {
[26678e5]120 waitq_initialize(&ap_completion_wq);
[49eb681]121
[f761f1eb]122 /*
123 * Create the kmp thread and wait for its completion.
124 * cpu1 through cpuN-1 will come up consecutively and
[169c408]125 * not mess together with kcpulb threads.
[f761f1eb]126 * Just a beautification.
127 */
[6eef3c4]128 thread = thread_create(kmp, NULL, TASK,
129 THREAD_FLAG_UNCOUNTED, "kmp");
[76fca31]130 if (thread != NULL) {
[6eef3c4]131 thread_wire(thread, &cpus[0]);
[76fca31]132 thread_ready(thread);
[8e3bf3e2]133 } else
[f651e80]134 panic("Unable to create kmp thread.");
[9dae191e]135
[76fca31]136 thread_join(thread);
137 thread_detach(thread);
[0132630]138
[76cec1e]139 /*
[f761f1eb]140 * For each CPU, create its load balancing thread.
141 */
[7e752b2]142 unsigned int i;
[49eb681]143
[f761f1eb]144 for (i = 0; i < config.cpu_count; i++) {
[6eef3c4]145 thread = thread_create(kcpulb, NULL, TASK,
146 THREAD_FLAG_UNCOUNTED, "kcpulb");
[76fca31]147 if (thread != NULL) {
[6eef3c4]148 thread_wire(thread, &cpus[i]);
[76fca31]149 thread_ready(thread);
[8e3bf3e2]150 } else
[7e752b2]151 printf("Unable to create kcpulb thread for cpu%u\n", i);
[f761f1eb]152 }
153 }
[5f85c91]154#endif /* CONFIG_SMP */
[76fca31]155
[a83a802]156 /*
157 * At this point SMP, if present, is configured.
158 */
159 arch_post_smp_init();
[2f57690]160
[9dae191e]161 /* Start thread computing system load */
[6eef3c4]162 thread = thread_create(kload, NULL, TASK, THREAD_FLAG_NONE,
163 "kload");
[9dae191e]164 if (thread != NULL)
165 thread_ready(thread);
166 else
167 printf("Unable to create kload thread\n");
168
[76fca31]169#ifdef CONFIG_KCONSOLE
170 if (stdin) {
171 /*
172 * Create kernel console.
173 */
[6eef3c4]174 thread = thread_create(kconsole_thread, NULL, TASK,
175 THREAD_FLAG_NONE, "kconsole");
[76fca31]176 if (thread != NULL)
177 thread_ready(thread);
178 else
179 printf("Unable to create kconsole thread\n");
180 }
181#endif /* CONFIG_KCONSOLE */
182
[22f7769]183 interrupts_enable();
[49eec93]184
185 /*
186 * Create user tasks, load RAM disk images.
187 */
[98000fb]188 size_t i;
[c98e6ee]189 program_t programs[CONFIG_INIT_TASKS];
[49eec93]190
[b6b576c]191 for (i = 0; i < init.cnt; i++) {
[32817cc]192 if (init.tasks[i].paddr % FRAME_SIZE) {
[fb48a0e]193 printf("init[%zu]: Address is not frame aligned\n", i);
[2319df3]194 programs[i].task = NULL;
[228b135]195 continue;
196 }
[2f57690]197
[20f1597]198 /*
[da581872]199 * Construct task name from the 'init:' prefix and the
[20f1597]200 * name stored in the init structure (if any).
201 */
[2f57690]202
203 char namebuf[TASK_NAME_BUFLEN];
204
[a000878c]205 const char *name = init.tasks[i].name;
[b60c582]206 if (name[0] == 0)
[2f57690]207 name = "<unknown>";
208
[da581872]209 ASSERT(TASK_NAME_BUFLEN >= INIT_PREFIX_LEN);
[f4b1535]210 str_cpy(namebuf, TASK_NAME_BUFLEN, INIT_PREFIX);
211 str_cpy(namebuf + INIT_PREFIX_LEN,
212 TASK_NAME_BUFLEN - INIT_PREFIX_LEN, name);
[db675dd]213
[32817cc]214 /*
215 * Create virtual memory mappings for init task images.
216 */
[221c9ec]217 uintptr_t page = km_map(init.tasks[i].paddr,
218 init.tasks[i].size,
219 PAGE_READ | PAGE_WRITE | PAGE_CACHEABLE);
220 ASSERT(page);
[da1bafb]221
[32817cc]222 int rc = program_create_from_image((void *) page, namebuf,
223 &programs[i]);
[76fca31]224
[fb48a0e]225 if (rc == 0) {
226 if (programs[i].task != NULL) {
227 /*
228 * Set capabilities to init userspace tasks.
229 */
230 cap_set(programs[i].task, CAP_CAP | CAP_MEM_MANAGER |
231 CAP_IO_MANAGER | CAP_IRQ_REG);
232
233 if (!ipc_phone_0)
234 ipc_phone_0 = &programs[i].task->answerbox;
235 }
236
[1077d91]237 /*
[fb48a0e]238 * If programs[i].task == NULL then it is
239 * the program loader and it was registered
240 * successfully.
[1077d91]241 */
[fb48a0e]242 } else if (i == init.cnt - 1) {
243 /*
244 * Assume the last task is the RAM disk.
245 */
[221c9ec]246 init_rd((void *) init.tasks[i].paddr, init.tasks[i].size);
[fb48a0e]247 } else
[db675dd]248 printf("init[%zu]: Init binary load failed "
249 "(error %d, loader status %u)\n", i, rc,
250 programs[i].loader_status);
[49eec93]251 }
[da1bafb]252
[49eec93]253 /*
[566f4cfb]254 * Run user tasks.
[49eec93]255 */
256 for (i = 0; i < init.cnt; i++) {
[566f4cfb]257 if (programs[i].task != NULL)
[c98e6ee]258 program_ready(&programs[i]);
[44c259c]259 }
[da1bafb]260
[76fca31]261#ifdef CONFIG_KCONSOLE
[ff3b3197]262 if (!stdin) {
[c19a4169]263 thread_sleep(10);
[eddf924]264 printf("kinit: No stdin\nKernel alive: .");
[76fca31]265
[c19a4169]266 unsigned int i = 0;
267 while (true) {
268 printf("\b%c", alive[i % ALIVE_CHARS]);
[ff3b3197]269 thread_sleep(1);
[76fca31]270 i++;
[ff3b3197]271 }
[f761f1eb]272 }
[76fca31]273#endif /* CONFIG_KCONSOLE */
[f761f1eb]274}
[b45c443]275
[8e3bf3e2]276/** @}
[b45c443]277 */
Note: See TracBrowser for help on using the repository browser.