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

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

Merged mainline,1723.

  • Property mode set to 100644
File size: 7.7 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 main
30 * @{
31 */
32
33/**
34 * @file
35 * @brief Kernel initialization thread.
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
44#include <main/kinit.h>
45#include <config.h>
46#include <arch.h>
47#include <proc/scheduler.h>
48#include <proc/task.h>
49#include <proc/thread.h>
50#include <proc/program.h>
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>
57#include <mm/as.h>
58#include <mm/frame.h>
59#include <mm/km.h>
60#include <print.h>
61#include <memstr.h>
62#include <console/console.h>
63#include <interrupt.h>
64#include <console/kconsole.h>
65#include <security/cap.h>
66#include <lib/rd.h>
67#include <ipc/ipc.h>
68#include <debug.h>
69#include <str.h>
70#include <sysinfo/stats.h>
71#include <sysinfo/sysinfo.h>
72#include <align.h>
73
74#ifdef CONFIG_SMP
75#include <smp/smp.h>
76#endif /* CONFIG_SMP */
77
78#include <synch/waitq.h>
79#include <synch/spinlock.h>
80#include <synch/workqueue.h>
81#include <synch/rcu.h>
82
83#define ALIVE_CHARS 4
84
85#ifdef CONFIG_KCONSOLE
86static char alive[ALIVE_CHARS] = "-\\|/";
87#endif
88
89#define INIT_PREFIX "init:"
90#define INIT_PREFIX_LEN 5
91
92/** Kernel initialization thread.
93 *
94 * kinit takes care of higher level kernel
95 * initialization (i.e. thread creation,
96 * userspace initialization etc.).
97 *
98 * @param arg Not used.
99 */
100void kinit(void *arg)
101{
102 thread_t *thread;
103
104 /*
105 * Detach kinit as nobody will call thread_join_timeout() on it.
106 */
107 thread_detach(THREAD);
108
109 interrupts_disable();
110
111 /* Start processing RCU callbacks. RCU is fully functional afterwards. */
112 rcu_kinit_init();
113
114 /*
115 * Start processing work queue items. Some may have been queued during boot.
116 */
117 workq_global_worker_init();
118
119#ifdef CONFIG_SMP
120 if (config.cpu_count > 1) {
121 waitq_initialize(&ap_completion_wq);
122
123 /*
124 * Create the kmp thread and wait for its completion.
125 * cpu1 through cpuN-1 will come up consecutively and
126 * not mess together with kcpulb threads.
127 * Just a beautification.
128 */
129 thread = thread_create(kmp, NULL, TASK,
130 THREAD_FLAG_UNCOUNTED, "kmp");
131 if (thread != NULL) {
132 thread_wire(thread, &cpus[0]);
133 thread_ready(thread);
134 } else
135 panic("Unable to create kmp thread.");
136
137 thread_join(thread);
138 thread_detach(thread);
139
140 /*
141 * For each CPU, create its load balancing thread.
142 */
143 unsigned int i;
144
145 for (i = 0; i < config.cpu_count; i++) {
146 thread = thread_create(kcpulb, NULL, TASK,
147 THREAD_FLAG_UNCOUNTED, "kcpulb");
148 if (thread != NULL) {
149 thread_wire(thread, &cpus[i]);
150 thread_ready(thread);
151 } else
152 printf("Unable to create kcpulb thread for cpu%u\n", i);
153 }
154 }
155#endif /* CONFIG_SMP */
156
157 /*
158 * At this point SMP, if present, is configured.
159 */
160 arch_post_smp_init();
161
162 /* Start thread computing system load */
163 thread = thread_create(kload, NULL, TASK, THREAD_FLAG_NONE,
164 "kload");
165 if (thread != NULL)
166 thread_ready(thread);
167 else
168 printf("Unable to create kload thread\n");
169
170#ifdef CONFIG_KCONSOLE
171 if (stdin) {
172 /*
173 * Create kernel console.
174 */
175 thread = thread_create(kconsole_thread, NULL, TASK,
176 THREAD_FLAG_NONE, "kconsole");
177 if (thread != NULL)
178 thread_ready(thread);
179 else
180 printf("Unable to create kconsole thread\n");
181 }
182#endif /* CONFIG_KCONSOLE */
183
184 /*
185 * Store the default stack size in sysinfo so that uspace can create
186 * stack with this default size.
187 */
188 sysinfo_set_item_val("default.stack_size", NULL, STACK_SIZE_USER);
189
190 interrupts_enable();
191
192 /*
193 * Create user tasks, load RAM disk images.
194 */
195 size_t i;
196 program_t programs[CONFIG_INIT_TASKS];
197
198 // FIXME: do not propagate arguments through sysinfo
199 // but pass them directly to the tasks
200 for (i = 0; i < init.cnt; i++) {
201 const char *arguments = init.tasks[i].arguments;
202 if (str_length(arguments) == 0)
203 continue;
204 if (str_length(init.tasks[i].name) == 0)
205 continue;
206 size_t arguments_size = str_size(arguments);
207
208 void *arguments_copy = malloc(arguments_size, 0);
209 if (arguments_copy == NULL)
210 continue;
211 memcpy(arguments_copy, arguments, arguments_size);
212
213 char item_name[CONFIG_TASK_NAME_BUFLEN + 15];
214 snprintf(item_name, CONFIG_TASK_NAME_BUFLEN + 15,
215 "init_args.%s", init.tasks[i].name);
216
217 sysinfo_set_item_data(item_name, NULL, arguments_copy, arguments_size);
218 }
219
220 for (i = 0; i < init.cnt; i++) {
221 if (init.tasks[i].paddr % FRAME_SIZE) {
222 printf("init[%zu]: Address is not frame aligned\n", i);
223 programs[i].task = NULL;
224 continue;
225 }
226
227 /*
228 * Construct task name from the 'init:' prefix and the
229 * name stored in the init structure (if any).
230 */
231
232 char namebuf[TASK_NAME_BUFLEN];
233
234 const char *name = init.tasks[i].name;
235 if (name[0] == 0)
236 name = "<unknown>";
237
238 ASSERT(TASK_NAME_BUFLEN >= INIT_PREFIX_LEN);
239 str_cpy(namebuf, TASK_NAME_BUFLEN, INIT_PREFIX);
240 str_cpy(namebuf + INIT_PREFIX_LEN,
241 TASK_NAME_BUFLEN - INIT_PREFIX_LEN, name);
242
243 /*
244 * Create virtual memory mappings for init task images.
245 */
246 uintptr_t page = km_map(init.tasks[i].paddr,
247 init.tasks[i].size,
248 PAGE_READ | PAGE_WRITE | PAGE_CACHEABLE);
249 ASSERT(page);
250
251 int rc = program_create_from_image((void *) page, namebuf,
252 &programs[i]);
253
254 if (rc == 0) {
255 if (programs[i].task != NULL) {
256 /*
257 * Set capabilities to init userspace tasks.
258 */
259 cap_set(programs[i].task, CAP_CAP | CAP_MEM_MANAGER |
260 CAP_IO_MANAGER | CAP_IRQ_REG);
261
262 if (!ipc_phone_0)
263 ipc_phone_0 = &programs[i].task->answerbox;
264 }
265
266 /*
267 * If programs[i].task == NULL then it is
268 * the program loader and it was registered
269 * successfully.
270 */
271 } else if (i == init.cnt - 1) {
272 /*
273 * Assume the last task is the RAM disk.
274 */
275 init_rd((void *) init.tasks[i].paddr, init.tasks[i].size);
276 } else
277 printf("init[%zu]: Init binary load failed "
278 "(error %d, loader status %u)\n", i, rc,
279 programs[i].loader_status);
280 }
281
282 /*
283 * Run user tasks.
284 */
285 for (i = 0; i < init.cnt; i++) {
286 if (programs[i].task != NULL)
287 program_ready(&programs[i]);
288 }
289
290#ifdef CONFIG_KCONSOLE
291 if (!stdin) {
292 thread_sleep(10);
293 printf("kinit: No stdin\nKernel alive: .");
294
295 unsigned int i = 0;
296 while (true) {
297 printf("\b%c", alive[i % ALIVE_CHARS]);
298 thread_sleep(1);
299 i++;
300 }
301 }
302#endif /* CONFIG_KCONSOLE */
303}
304
305/** @}
306 */
Note: See TracBrowser for help on using the repository browser.