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

Last change on this file was a78b0a0, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 3 months ago

Use static initialization for ap_completion_semaphore

  • Property mode set to 100644
File size: 8.4 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 kernel_generic
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 <assert.h>
45#include <main/kinit.h>
46#include <config.h>
47#include <arch.h>
48#include <proc/scheduler.h>
49#include <proc/task.h>
50#include <proc/thread.h>
51#include <proc/program.h>
52#include <panic.h>
53#include <halt.h>
54#include <cpu.h>
55#include <arch/asm.h>
56#include <mm/page.h>
57#include <arch/mm/page.h>
58#include <mm/as.h>
59#include <mm/frame.h>
60#include <mm/km.h>
61#include <stdio.h>
62#include <log.h>
63#include <memw.h>
64#include <console/console.h>
65#include <interrupt.h>
66#include <console/kconsole.h>
67#include <security/perm.h>
68#include <lib/rd.h>
69#include <ipc/ipc.h>
70#include <str.h>
71#include <str_error.h>
72#include <sysinfo/stats.h>
73#include <sysinfo/sysinfo.h>
74#include <align.h>
75#include <stdlib.h>
76#include <debug/register.h>
77
78#ifdef CONFIG_SMP
79#include <smp/smp.h>
80#endif /* CONFIG_SMP */
81
82#include <synch/waitq.h>
83#include <synch/spinlock.h>
84
85#define ALIVE_CHARS 4
86
87#ifdef CONFIG_KCONSOLE
88static char alive[ALIVE_CHARS] = "-\\|/";
89#endif
90
91#define INIT_PREFIX "init:"
92#define INIT_PREFIX_LEN 5
93
94/** Kernel initialization thread.
95 *
96 * kinit takes care of higher level kernel
97 * initialization (i.e. thread creation,
98 * userspace initialization etc.).
99 *
100 * @param arg Not used.
101 */
102void kinit(void *arg)
103{
104 thread_t *thread;
105
106 interrupts_disable();
107
108#ifdef CONFIG_SMP
109 if (config.cpu_count > 1) {
110 /*
111 * Create the kmp thread and wait for its completion.
112 * cpu1 through cpuN-1 will come up consecutively and
113 * not mess together with kcpulb threads.
114 * Just a beautification.
115 */
116 thread = thread_create(kmp, NULL, TASK,
117 THREAD_FLAG_UNCOUNTED, "kmp");
118 if (!thread)
119 panic("Unable to create kmp thread.");
120
121 thread_wire(thread, &cpus[0]);
122 thread_start(thread);
123 thread_join(thread);
124
125 /*
126 * For each CPU, create its load balancing thread.
127 */
128 unsigned int i;
129
130 for (i = 0; i < config.cpu_count; i++) {
131 thread = thread_create(kcpulb, NULL, TASK,
132 THREAD_FLAG_UNCOUNTED, "kcpulb");
133 if (thread != NULL) {
134 thread_wire(thread, &cpus[i]);
135 thread_start(thread);
136 thread_detach(thread);
137 } else
138 log(LF_OTHER, LVL_ERROR,
139 "Unable to create kcpulb thread for cpu%u", i);
140 }
141 }
142#endif /* CONFIG_SMP */
143
144 /*
145 * At this point SMP, if present, is configured.
146 */
147 ARCH_OP(post_smp_init);
148
149 /* Start thread computing system load */
150 thread = thread_create(kload, NULL, TASK, THREAD_FLAG_NONE,
151 "kload");
152 if (thread != NULL) {
153 thread_start(thread);
154 thread_detach(thread);
155 } else {
156 log(LF_OTHER, LVL_ERROR, "Unable to create kload thread");
157 }
158
159#ifdef CONFIG_KCONSOLE
160 if (stdin) {
161 /*
162 * Create kernel console.
163 */
164 thread = thread_create(kconsole_thread, NULL, TASK,
165 THREAD_FLAG_NONE, "kconsole");
166 if (thread != NULL) {
167 thread_start(thread);
168 thread_detach(thread);
169 } else {
170 log(LF_OTHER, LVL_ERROR,
171 "Unable to create kconsole thread");
172 }
173 }
174#endif /* CONFIG_KCONSOLE */
175
176 /*
177 * Store the default stack size in sysinfo so that uspace can create
178 * stack with this default size.
179 */
180 sysinfo_set_item_val("default.stack_size", NULL, STACK_SIZE_USER);
181
182 interrupts_enable();
183
184 /*
185 * Create user tasks, load RAM disk images.
186 */
187 size_t i;
188 program_t programs[CONFIG_INIT_TASKS] = { };
189
190 // FIXME: do not propagate arguments through sysinfo
191 // but pass them directly to the tasks
192 for (i = 0; i < init.cnt; i++) {
193 const char *arguments = init.tasks[i].arguments;
194 if (str_length(arguments) == 0)
195 continue;
196 if (str_length(init.tasks[i].name) == 0)
197 continue;
198 size_t arguments_size = str_size(arguments);
199
200 void *arguments_copy = malloc(arguments_size);
201 if (arguments_copy == NULL)
202 continue;
203 memcpy(arguments_copy, arguments, arguments_size);
204
205 char item_name[CONFIG_TASK_NAME_BUFLEN + 15];
206 snprintf(item_name, CONFIG_TASK_NAME_BUFLEN + 15,
207 "init_args.%s", init.tasks[i].name);
208
209 sysinfo_set_item_data(item_name, NULL, arguments_copy, arguments_size);
210 }
211
212 for (i = 0; i < init.cnt; i++) {
213 if (init.tasks[i].paddr % FRAME_SIZE) {
214 log(LF_OTHER, LVL_ERROR,
215 "init[%zu]: Address is not frame aligned", i);
216 programs[i].task = NULL;
217 continue;
218 }
219
220 /*
221 * Construct task name from the 'init:' prefix and the
222 * name stored in the init structure (if any).
223 */
224
225 char namebuf[TASK_NAME_BUFLEN];
226
227 const char *name = init.tasks[i].name;
228 if (name[0] == 0)
229 name = "<unknown>";
230
231 static_assert(TASK_NAME_BUFLEN >= INIT_PREFIX_LEN, "");
232 str_cpy(namebuf, TASK_NAME_BUFLEN, INIT_PREFIX);
233 str_cpy(namebuf + INIT_PREFIX_LEN,
234 TASK_NAME_BUFLEN - INIT_PREFIX_LEN, name);
235
236 /*
237 * Create virtual memory mappings for init task images.
238 */
239 uintptr_t page = km_map(init.tasks[i].paddr,
240 init.tasks[i].size, PAGE_SIZE,
241 PAGE_READ | PAGE_WRITE | PAGE_CACHEABLE);
242 assert(page);
243
244 if (str_cmp(name, "kernel.dbg") == 0) {
245 /*
246 * Not an actual init task, but rather debug sections extracted
247 * from the kernel ELF file and handed to us here so we can use
248 * it for debugging.
249 */
250
251 register_debug_data((void *) page, init.tasks[i].size);
252 programs[i].task = NULL;
253 continue;
254 }
255
256 if (str_cmp(name, "loader") == 0) {
257 /* Register image as the program loader */
258 if (program_loader == NULL) {
259 program_loader = (void *) page;
260 log(LF_OTHER, LVL_NOTE, "Program loader at %p",
261 program_loader);
262 } else {
263 log(LF_OTHER, LVL_ERROR,
264 "init[%zu]: Second binary named \"loader\""
265 " present.", i);
266 }
267
268 programs[i].task = NULL;
269 continue;
270 }
271
272 errno_t rc = program_create_from_image((void *) page, init.tasks[i].size, namebuf,
273 &programs[i]);
274
275 if (rc == 0) {
276 assert(programs[i].task != NULL);
277
278 /*
279 * Set permissions to init userspace tasks.
280 */
281 perm_set(programs[i].task,
282 PERM_PERM | PERM_MEM_MANAGER |
283 PERM_IO_MANAGER | PERM_IRQ_REG);
284
285 if (!ipc_box_0) {
286 ipc_box_0 = &programs[i].task->answerbox;
287 /*
288 * Hold the first task so that
289 * ipc_box_0 remains a valid pointer
290 * even if the first task exits for
291 * whatever reason.
292 */
293 task_hold(programs[i].task);
294 }
295
296 } else if (str_cmp(name, "initrd.img") == 0) {
297 init_rd((void *) init.tasks[i].paddr, init.tasks[i].size);
298 } else {
299 log(LF_OTHER, LVL_ERROR,
300 "init[%zu]: Init binary load failed "
301 "(error %s, loader status %s)", i,
302 str_error_name(rc), str_error_name(programs[i].loader_status));
303 }
304 }
305
306 /*
307 * Run user tasks.
308 */
309 for (i = 0; i < init.cnt; i++) {
310 if (programs[i].task != NULL) {
311 program_ready(&programs[i]);
312 task_release(programs[i].task);
313 }
314 }
315
316#ifdef CONFIG_KCONSOLE
317 if (!stdin) {
318 thread_sleep(10);
319 printf("kinit: No stdin\nKernel alive: .");
320
321 unsigned int i = 0;
322 while (true) {
323 printf("\b%c", alive[i % ALIVE_CHARS]);
324 thread_sleep(1);
325 i++;
326 }
327 }
328#endif /* CONFIG_KCONSOLE */
329}
330
331/** @}
332 */
Note: See TracBrowser for help on using the repository browser.