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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 1ccd0aa was 32817cc, checked in by Jakub Jermar <jakub@…>, 13 years ago

Make the kernel ready for init tasks loaded to high memory.

  • Property mode set to 100644
File size: 6.9 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>
79
[c19a4169]80#define ALIVE_CHARS 4
81
[753d851]82#ifdef CONFIG_KCONSOLE
[c19a4169]83static char alive[ALIVE_CHARS] = "-\\|/";
[753d851]84#endif
[c19a4169]85
[da581872]86#define INIT_PREFIX "init:"
87#define INIT_PREFIX_LEN 5
[20f1597]88
[880de6e]89/** Kernel initialization thread.
90 *
91 * kinit takes care of higher level kernel
92 * initialization (i.e. thread creation,
93 * userspace initialization etc.).
94 *
95 * @param arg Not used.
96 */
[f761f1eb]97void kinit(void *arg)
98{
[76fca31]99 thread_t *thread;
[2f57690]100
[2cb5e64]101 /*
102 * Detach kinit as nobody will call thread_join_timeout() on it.
103 */
104 thread_detach(THREAD);
[2f57690]105
[22f7769]106 interrupts_disable();
[2f57690]107
108#ifdef CONFIG_SMP
[f761f1eb]109 if (config.cpu_count > 1) {
[26678e5]110 waitq_initialize(&ap_completion_wq);
[49eb681]111
[f761f1eb]112 /*
113 * Create the kmp thread and wait for its completion.
114 * cpu1 through cpuN-1 will come up consecutively and
[169c408]115 * not mess together with kcpulb threads.
[f761f1eb]116 * Just a beautification.
117 */
[76fca31]118 thread = thread_create(kmp, NULL, TASK, THREAD_FLAG_WIRED, "kmp", true);
119 if (thread != NULL) {
[da1bafb]120 irq_spinlock_lock(&thread->lock, false);
[76fca31]121 thread->cpu = &cpus[0];
[da1bafb]122 irq_spinlock_unlock(&thread->lock, false);
[76fca31]123 thread_ready(thread);
[8e3bf3e2]124 } else
[f651e80]125 panic("Unable to create kmp thread.");
[9dae191e]126
[76fca31]127 thread_join(thread);
128 thread_detach(thread);
[0132630]129
[76cec1e]130 /*
[f761f1eb]131 * For each CPU, create its load balancing thread.
132 */
[7e752b2]133 unsigned int i;
[49eb681]134
[f761f1eb]135 for (i = 0; i < config.cpu_count; i++) {
[76fca31]136 thread = thread_create(kcpulb, NULL, TASK, THREAD_FLAG_WIRED, "kcpulb", true);
137 if (thread != NULL) {
[da1bafb]138 irq_spinlock_lock(&thread->lock, false);
[76fca31]139 thread->cpu = &cpus[i];
[da1bafb]140 irq_spinlock_unlock(&thread->lock, false);
[76fca31]141 thread_ready(thread);
[8e3bf3e2]142 } else
[7e752b2]143 printf("Unable to create kcpulb thread for cpu%u\n", i);
[f761f1eb]144 }
145 }
[5f85c91]146#endif /* CONFIG_SMP */
[76fca31]147
[a83a802]148 /*
149 * At this point SMP, if present, is configured.
150 */
151 arch_post_smp_init();
[2f57690]152
[9dae191e]153 /* Start thread computing system load */
154 thread = thread_create(kload, NULL, TASK, 0, "kload", false);
155 if (thread != NULL)
156 thread_ready(thread);
157 else
158 printf("Unable to create kload thread\n");
159
[76fca31]160#ifdef CONFIG_KCONSOLE
161 if (stdin) {
162 /*
163 * Create kernel console.
164 */
165 thread = thread_create(kconsole_thread, NULL, TASK, 0, "kconsole", false);
166 if (thread != NULL)
167 thread_ready(thread);
168 else
169 printf("Unable to create kconsole thread\n");
170 }
171#endif /* CONFIG_KCONSOLE */
172
[22f7769]173 interrupts_enable();
[49eec93]174
175 /*
176 * Create user tasks, load RAM disk images.
177 */
[98000fb]178 size_t i;
[c98e6ee]179 program_t programs[CONFIG_INIT_TASKS];
[49eec93]180
[b6b576c]181 for (i = 0; i < init.cnt; i++) {
[32817cc]182 if (init.tasks[i].paddr % FRAME_SIZE) {
[fb48a0e]183 printf("init[%zu]: Address is not frame aligned\n", i);
[2319df3]184 programs[i].task = NULL;
[228b135]185 continue;
186 }
[2f57690]187
[20f1597]188 /*
[da581872]189 * Construct task name from the 'init:' prefix and the
[20f1597]190 * name stored in the init structure (if any).
191 */
[2f57690]192
193 char namebuf[TASK_NAME_BUFLEN];
194
[a000878c]195 const char *name = init.tasks[i].name;
[b60c582]196 if (name[0] == 0)
[2f57690]197 name = "<unknown>";
198
[da581872]199 ASSERT(TASK_NAME_BUFLEN >= INIT_PREFIX_LEN);
[f4b1535]200 str_cpy(namebuf, TASK_NAME_BUFLEN, INIT_PREFIX);
201 str_cpy(namebuf + INIT_PREFIX_LEN,
202 TASK_NAME_BUFLEN - INIT_PREFIX_LEN, name);
[32817cc]203
204 /*
205 * Create virtual memory mappings for init task images.
206 */
207 size_t size = ALIGN_UP(init.tasks[i].size, PAGE_SIZE);
208 size_t offs;
209 uintptr_t page = km_page_alloc(size, PAGE_SIZE);
210 uintptr_t frame = init.tasks[i].paddr;
211
212 page_table_lock(AS_KERNEL, true);
213 for (offs = 0; offs < size; offs += PAGE_SIZE) {
214 page_mapping_insert(AS_KERNEL, page + offs,
215 frame + offs,
216 PAGE_READ | PAGE_WRITE | PAGE_CACHEABLE |
217 PAGE_PRESENT);
218 }
219 page_table_unlock(AS_KERNEL, true);
[da1bafb]220
[32817cc]221 int rc = program_create_from_image((void *) page, namebuf,
222 &programs[i]);
[76fca31]223
[fb48a0e]224 if (rc == 0) {
225 if (programs[i].task != NULL) {
226 /*
227 * Set capabilities to init userspace tasks.
228 */
229 cap_set(programs[i].task, CAP_CAP | CAP_MEM_MANAGER |
230 CAP_IO_MANAGER | CAP_IRQ_REG);
231
232 if (!ipc_phone_0)
233 ipc_phone_0 = &programs[i].task->answerbox;
234 }
235
[1077d91]236 /*
[fb48a0e]237 * If programs[i].task == NULL then it is
238 * the program loader and it was registered
239 * successfully.
[1077d91]240 */
[fb48a0e]241 } else if (i == init.cnt - 1) {
242 /*
243 * Assume the last task is the RAM disk.
244 */
[32817cc]245 init_rd((void *) frame, init.tasks[i].size);
[fb48a0e]246 } else
247 printf("init[%zu]: Init binary load failed (error %d)\n", i, rc);
[49eec93]248 }
[da1bafb]249
[49eec93]250 /*
[566f4cfb]251 * Run user tasks.
[49eec93]252 */
253 for (i = 0; i < init.cnt; i++) {
[566f4cfb]254 if (programs[i].task != NULL)
[c98e6ee]255 program_ready(&programs[i]);
[44c259c]256 }
[da1bafb]257
[76fca31]258#ifdef CONFIG_KCONSOLE
[ff3b3197]259 if (!stdin) {
[c19a4169]260 thread_sleep(10);
[eddf924]261 printf("kinit: No stdin\nKernel alive: .");
[76fca31]262
[c19a4169]263 unsigned int i = 0;
264 while (true) {
265 printf("\b%c", alive[i % ALIVE_CHARS]);
[ff3b3197]266 thread_sleep(1);
[76fca31]267 i++;
[ff3b3197]268 }
[f761f1eb]269 }
[76fca31]270#endif /* CONFIG_KCONSOLE */
[f761f1eb]271}
[b45c443]272
[8e3bf3e2]273/** @}
[b45c443]274 */
Note: See TracBrowser for help on using the repository browser.