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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 3e200736 was b2fa1204, checked in by Martin Sucha <sucha14@…>, 12 years ago

Cherrypick usage of kernel logger

  • Property mode set to 100644
File size: 7.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>
[b2fa1204]61#include <log.h>
[399ccd9]62#include <memstr.h>
[ff3b3197]63#include <console/console.h>
[aace6624]64#include <interrupt.h>
[f4338d2]65#include <console/kconsole.h>
[1077d91]66#include <security/cap.h>
[d4b5542]67#include <lib/rd.h>
[b3f8fb7]68#include <ipc/ipc.h>
[20f1597]69#include <debug.h>
[19f857a]70#include <str.h>
[9dae191e]71#include <sysinfo/stats.h>
[c8cbd39]72#include <sysinfo/sysinfo.h>
[32817cc]73#include <align.h>
[f761f1eb]74
[5f85c91]75#ifdef CONFIG_SMP
[26678e5]76#include <smp/smp.h>
[5f85c91]77#endif /* CONFIG_SMP */
[f761f1eb]78
79#include <synch/waitq.h>
80#include <synch/spinlock.h>
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);
[2f57690]107
[22f7769]108 interrupts_disable();
[2f57690]109
110#ifdef CONFIG_SMP
[f761f1eb]111 if (config.cpu_count > 1) {
[26678e5]112 waitq_initialize(&ap_completion_wq);
[49eb681]113
[f761f1eb]114 /*
115 * Create the kmp thread and wait for its completion.
116 * cpu1 through cpuN-1 will come up consecutively and
[169c408]117 * not mess together with kcpulb threads.
[f761f1eb]118 * Just a beautification.
119 */
[6eef3c4]120 thread = thread_create(kmp, NULL, TASK,
121 THREAD_FLAG_UNCOUNTED, "kmp");
[76fca31]122 if (thread != NULL) {
[6eef3c4]123 thread_wire(thread, &cpus[0]);
[76fca31]124 thread_ready(thread);
[8e3bf3e2]125 } else
[f651e80]126 panic("Unable to create kmp thread.");
[9dae191e]127
[76fca31]128 thread_join(thread);
129 thread_detach(thread);
[0132630]130
[76cec1e]131 /*
[f761f1eb]132 * For each CPU, create its load balancing thread.
133 */
[7e752b2]134 unsigned int i;
[49eb681]135
[f761f1eb]136 for (i = 0; i < config.cpu_count; i++) {
[6eef3c4]137 thread = thread_create(kcpulb, NULL, TASK,
138 THREAD_FLAG_UNCOUNTED, "kcpulb");
[76fca31]139 if (thread != NULL) {
[6eef3c4]140 thread_wire(thread, &cpus[i]);
[76fca31]141 thread_ready(thread);
[8e3bf3e2]142 } else
[b2fa1204]143 log(LF_OTHER, LVL_ERROR,
144 "Unable to create kcpulb thread for cpu%u", i);
[f761f1eb]145 }
146 }
[5f85c91]147#endif /* CONFIG_SMP */
[76fca31]148
[a83a802]149 /*
150 * At this point SMP, if present, is configured.
151 */
152 arch_post_smp_init();
[2f57690]153
[9dae191e]154 /* Start thread computing system load */
[6eef3c4]155 thread = thread_create(kload, NULL, TASK, THREAD_FLAG_NONE,
156 "kload");
[9dae191e]157 if (thread != NULL)
158 thread_ready(thread);
159 else
[b2fa1204]160 log(LF_OTHER, LVL_ERROR, "Unable to create kload thread");
[9dae191e]161
[76fca31]162#ifdef CONFIG_KCONSOLE
163 if (stdin) {
164 /*
165 * Create kernel console.
166 */
[6eef3c4]167 thread = thread_create(kconsole_thread, NULL, TASK,
168 THREAD_FLAG_NONE, "kconsole");
[76fca31]169 if (thread != NULL)
170 thread_ready(thread);
171 else
[b2fa1204]172 log(LF_OTHER, LVL_ERROR,
173 "Unable to create kconsole thread");
[76fca31]174 }
175#endif /* CONFIG_KCONSOLE */
176
[0aae87a6]177 /*
178 * Store the default stack size in sysinfo so that uspace can create
179 * stack with this default size.
180 */
[67b152e]181 sysinfo_set_item_val("default.stack_size", NULL, STACK_SIZE_USER);
[0aae87a6]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
[c8cbd39]191 // FIXME: do not propagate arguments through sysinfo
192 // but pass them directly to the tasks
193 for (i = 0; i < init.cnt; i++) {
194 const char *arguments = init.tasks[i].arguments;
195 if (str_length(arguments) == 0)
196 continue;
197 if (str_length(init.tasks[i].name) == 0)
198 continue;
199 size_t arguments_size = str_size(arguments);
200
201 void *arguments_copy = malloc(arguments_size, 0);
202 if (arguments_copy == NULL)
203 continue;
204 memcpy(arguments_copy, arguments, arguments_size);
205
206 char item_name[CONFIG_TASK_NAME_BUFLEN + 15];
207 snprintf(item_name, CONFIG_TASK_NAME_BUFLEN + 15,
208 "init_args.%s", init.tasks[i].name);
209
210 sysinfo_set_item_data(item_name, NULL, arguments_copy, arguments_size);
211 }
212
[b6b576c]213 for (i = 0; i < init.cnt; i++) {
[32817cc]214 if (init.tasks[i].paddr % FRAME_SIZE) {
[b2fa1204]215 log(LF_OTHER, LVL_ERROR,
216 "init[%zu]: Address is not frame aligned", i);
[2319df3]217 programs[i].task = NULL;
[228b135]218 continue;
219 }
[2f57690]220
[20f1597]221 /*
[da581872]222 * Construct task name from the 'init:' prefix and the
[20f1597]223 * name stored in the init structure (if any).
224 */
[2f57690]225
226 char namebuf[TASK_NAME_BUFLEN];
227
[a000878c]228 const char *name = init.tasks[i].name;
[b60c582]229 if (name[0] == 0)
[2f57690]230 name = "<unknown>";
231
[da581872]232 ASSERT(TASK_NAME_BUFLEN >= INIT_PREFIX_LEN);
[f4b1535]233 str_cpy(namebuf, TASK_NAME_BUFLEN, INIT_PREFIX);
234 str_cpy(namebuf + INIT_PREFIX_LEN,
235 TASK_NAME_BUFLEN - INIT_PREFIX_LEN, name);
[db675dd]236
[32817cc]237 /*
238 * Create virtual memory mappings for init task images.
239 */
[221c9ec]240 uintptr_t page = km_map(init.tasks[i].paddr,
241 init.tasks[i].size,
242 PAGE_READ | PAGE_WRITE | PAGE_CACHEABLE);
243 ASSERT(page);
[da1bafb]244
[32817cc]245 int rc = program_create_from_image((void *) page, namebuf,
246 &programs[i]);
[76fca31]247
[fb48a0e]248 if (rc == 0) {
249 if (programs[i].task != NULL) {
250 /*
251 * Set capabilities to init userspace tasks.
252 */
253 cap_set(programs[i].task, CAP_CAP | CAP_MEM_MANAGER |
254 CAP_IO_MANAGER | CAP_IRQ_REG);
255
[a4e23f8c]256 if (!ipc_phone_0) {
[fb48a0e]257 ipc_phone_0 = &programs[i].task->answerbox;
[a4e23f8c]258 /*
259 * Hold the first task so that the
260 * ipc_phone_0 remains a valid pointer
261 * even if the first task exits for
262 * whatever reason.
263 */
264 task_hold(programs[i].task);
265 }
[fb48a0e]266 }
267
[1077d91]268 /*
[fb48a0e]269 * If programs[i].task == NULL then it is
270 * the program loader and it was registered
271 * successfully.
[1077d91]272 */
[fb48a0e]273 } else if (i == init.cnt - 1) {
274 /*
275 * Assume the last task is the RAM disk.
276 */
[221c9ec]277 init_rd((void *) init.tasks[i].paddr, init.tasks[i].size);
[fb48a0e]278 } else
[b2fa1204]279 log(LF_OTHER, LVL_ERROR,
280 "init[%zu]: Init binary load failed "
281 "(error %d, loader status %u)", i, rc,
[db675dd]282 programs[i].loader_status);
[49eec93]283 }
[da1bafb]284
[49eec93]285 /*
[566f4cfb]286 * Run user tasks.
[49eec93]287 */
288 for (i = 0; i < init.cnt; i++) {
[566f4cfb]289 if (programs[i].task != NULL)
[c98e6ee]290 program_ready(&programs[i]);
[44c259c]291 }
[da1bafb]292
[76fca31]293#ifdef CONFIG_KCONSOLE
[ff3b3197]294 if (!stdin) {
[c19a4169]295 thread_sleep(10);
[eddf924]296 printf("kinit: No stdin\nKernel alive: .");
[76fca31]297
[c19a4169]298 unsigned int i = 0;
299 while (true) {
300 printf("\b%c", alive[i % ALIVE_CHARS]);
[ff3b3197]301 thread_sleep(1);
[76fca31]302 i++;
[ff3b3197]303 }
[f761f1eb]304 }
[76fca31]305#endif /* CONFIG_KCONSOLE */
[f761f1eb]306}
[b45c443]307
[8e3bf3e2]308/** @}
[b45c443]309 */
Note: See TracBrowser for help on using the repository browser.