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 <mem.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 <sysinfo/stats.h>
|
---|
72 | #include <sysinfo/sysinfo.h>
|
---|
73 | #include <align.h>
|
---|
74 | #include <stdlib.h>
|
---|
75 |
|
---|
76 | #ifdef CONFIG_SMP
|
---|
77 | #include <smp/smp.h>
|
---|
78 | #endif /* CONFIG_SMP */
|
---|
79 |
|
---|
80 | #include <synch/waitq.h>
|
---|
81 | #include <synch/spinlock.h>
|
---|
82 |
|
---|
83 | #define ALIVE_CHARS 4
|
---|
84 |
|
---|
85 | #ifdef CONFIG_KCONSOLE
|
---|
86 | static 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 | */
|
---|
100 | void kinit(void *arg)
|
---|
101 | {
|
---|
102 | thread_t *thread;
|
---|
103 |
|
---|
104 | interrupts_disable();
|
---|
105 |
|
---|
106 | #ifdef CONFIG_SMP
|
---|
107 | if (config.cpu_count > 1) {
|
---|
108 | semaphore_initialize(&ap_completion_semaphore, 0);
|
---|
109 |
|
---|
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_ready(thread_ref(thread));
|
---|
123 | thread_join(thread);
|
---|
124 | thread_put(thread);
|
---|
125 |
|
---|
126 | /*
|
---|
127 | * For each CPU, create its load balancing thread.
|
---|
128 | */
|
---|
129 | unsigned int i;
|
---|
130 |
|
---|
131 | for (i = 0; i < config.cpu_count; i++) {
|
---|
132 | thread = thread_create(kcpulb, NULL, TASK,
|
---|
133 | THREAD_FLAG_UNCOUNTED, "kcpulb");
|
---|
134 | if (thread != NULL) {
|
---|
135 | thread_wire(thread, &cpus[i]);
|
---|
136 | thread_ready(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_ready(thread);
|
---|
154 | else
|
---|
155 | log(LF_OTHER, LVL_ERROR, "Unable to create kload thread");
|
---|
156 |
|
---|
157 | #ifdef CONFIG_KCONSOLE
|
---|
158 | if (stdin) {
|
---|
159 | /*
|
---|
160 | * Create kernel console.
|
---|
161 | */
|
---|
162 | thread = thread_create(kconsole_thread, NULL, TASK,
|
---|
163 | THREAD_FLAG_NONE, "kconsole");
|
---|
164 | if (thread != NULL)
|
---|
165 | thread_ready(thread);
|
---|
166 | else
|
---|
167 | log(LF_OTHER, LVL_ERROR,
|
---|
168 | "Unable to create kconsole thread");
|
---|
169 | }
|
---|
170 | #endif /* CONFIG_KCONSOLE */
|
---|
171 |
|
---|
172 | /*
|
---|
173 | * Store the default stack size in sysinfo so that uspace can create
|
---|
174 | * stack with this default size.
|
---|
175 | */
|
---|
176 | sysinfo_set_item_val("default.stack_size", NULL, STACK_SIZE_USER);
|
---|
177 |
|
---|
178 | interrupts_enable();
|
---|
179 |
|
---|
180 | /*
|
---|
181 | * Create user tasks, load RAM disk images.
|
---|
182 | */
|
---|
183 | size_t i;
|
---|
184 | program_t programs[CONFIG_INIT_TASKS];
|
---|
185 |
|
---|
186 | // FIXME: do not propagate arguments through sysinfo
|
---|
187 | // but pass them directly to the tasks
|
---|
188 | for (i = 0; i < init.cnt; i++) {
|
---|
189 | const char *arguments = init.tasks[i].arguments;
|
---|
190 | if (str_length(arguments) == 0)
|
---|
191 | continue;
|
---|
192 | if (str_length(init.tasks[i].name) == 0)
|
---|
193 | continue;
|
---|
194 | size_t arguments_size = str_size(arguments);
|
---|
195 |
|
---|
196 | void *arguments_copy = malloc(arguments_size);
|
---|
197 | if (arguments_copy == NULL)
|
---|
198 | continue;
|
---|
199 | memcpy(arguments_copy, arguments, arguments_size);
|
---|
200 |
|
---|
201 | char item_name[CONFIG_TASK_NAME_BUFLEN + 15];
|
---|
202 | snprintf(item_name, CONFIG_TASK_NAME_BUFLEN + 15,
|
---|
203 | "init_args.%s", init.tasks[i].name);
|
---|
204 |
|
---|
205 | sysinfo_set_item_data(item_name, NULL, arguments_copy, arguments_size);
|
---|
206 | }
|
---|
207 |
|
---|
208 | for (i = 0; i < init.cnt; i++) {
|
---|
209 | if (init.tasks[i].paddr % FRAME_SIZE) {
|
---|
210 | log(LF_OTHER, LVL_ERROR,
|
---|
211 | "init[%zu]: Address is not frame aligned", i);
|
---|
212 | programs[i].task = NULL;
|
---|
213 | continue;
|
---|
214 | }
|
---|
215 |
|
---|
216 | /*
|
---|
217 | * Construct task name from the 'init:' prefix and the
|
---|
218 | * name stored in the init structure (if any).
|
---|
219 | */
|
---|
220 |
|
---|
221 | char namebuf[TASK_NAME_BUFLEN];
|
---|
222 |
|
---|
223 | const char *name = init.tasks[i].name;
|
---|
224 | if (name[0] == 0)
|
---|
225 | name = "<unknown>";
|
---|
226 |
|
---|
227 | static_assert(TASK_NAME_BUFLEN >= INIT_PREFIX_LEN, "");
|
---|
228 | str_cpy(namebuf, TASK_NAME_BUFLEN, INIT_PREFIX);
|
---|
229 | str_cpy(namebuf + INIT_PREFIX_LEN,
|
---|
230 | TASK_NAME_BUFLEN - INIT_PREFIX_LEN, name);
|
---|
231 |
|
---|
232 | /*
|
---|
233 | * Create virtual memory mappings for init task images.
|
---|
234 | */
|
---|
235 | uintptr_t page = km_map(init.tasks[i].paddr,
|
---|
236 | init.tasks[i].size, PAGE_SIZE,
|
---|
237 | PAGE_READ | PAGE_WRITE | PAGE_CACHEABLE);
|
---|
238 | assert(page);
|
---|
239 |
|
---|
240 | if (str_cmp(name, "loader") == 0) {
|
---|
241 | /* Register image as the program loader */
|
---|
242 | if (program_loader == NULL) {
|
---|
243 | program_loader = (void *) page;
|
---|
244 | log(LF_OTHER, LVL_NOTE, "Program loader at %p",
|
---|
245 | program_loader);
|
---|
246 | } else {
|
---|
247 | log(LF_OTHER, LVL_ERROR,
|
---|
248 | "init[%zu]: Second binary named \"loader\""
|
---|
249 | " present.", i);
|
---|
250 | }
|
---|
251 |
|
---|
252 | programs[i].task = NULL;
|
---|
253 | continue;
|
---|
254 | }
|
---|
255 |
|
---|
256 | errno_t rc = program_create_from_image((void *) page, namebuf,
|
---|
257 | &programs[i]);
|
---|
258 |
|
---|
259 | if (rc == 0) {
|
---|
260 | assert(programs[i].task != NULL);
|
---|
261 |
|
---|
262 | /*
|
---|
263 | * Set permissions to init userspace tasks.
|
---|
264 | */
|
---|
265 | perm_set(programs[i].task,
|
---|
266 | PERM_PERM | PERM_MEM_MANAGER |
|
---|
267 | PERM_IO_MANAGER | PERM_IRQ_REG);
|
---|
268 |
|
---|
269 | if (!ipc_box_0) {
|
---|
270 | ipc_box_0 = &programs[i].task->answerbox;
|
---|
271 | /*
|
---|
272 | * Hold the first task so that
|
---|
273 | * ipc_box_0 remains a valid pointer
|
---|
274 | * even if the first task exits for
|
---|
275 | * whatever reason.
|
---|
276 | */
|
---|
277 | task_hold(programs[i].task);
|
---|
278 | }
|
---|
279 |
|
---|
280 | } else if (i == init.cnt - 1) {
|
---|
281 | /*
|
---|
282 | * Assume the last task is the RAM disk.
|
---|
283 | */
|
---|
284 | init_rd((void *) init.tasks[i].paddr, init.tasks[i].size);
|
---|
285 | } else {
|
---|
286 | log(LF_OTHER, LVL_ERROR,
|
---|
287 | "init[%zu]: Init binary load failed "
|
---|
288 | "(error %s, loader status %s)", i,
|
---|
289 | str_error_name(rc), str_error_name(programs[i].loader_status));
|
---|
290 | }
|
---|
291 | }
|
---|
292 |
|
---|
293 | /*
|
---|
294 | * Run user tasks.
|
---|
295 | */
|
---|
296 | for (i = 0; i < init.cnt; i++) {
|
---|
297 | if (programs[i].task != NULL)
|
---|
298 | program_ready(&programs[i]);
|
---|
299 | }
|
---|
300 |
|
---|
301 | #ifdef CONFIG_KCONSOLE
|
---|
302 | if (!stdin) {
|
---|
303 | thread_sleep(10);
|
---|
304 | printf("kinit: No stdin\nKernel alive: .");
|
---|
305 |
|
---|
306 | unsigned int i = 0;
|
---|
307 | while (true) {
|
---|
308 | printf("\b%c", alive[i % ALIVE_CHARS]);
|
---|
309 | thread_sleep(1);
|
---|
310 | i++;
|
---|
311 | }
|
---|
312 | }
|
---|
313 | #endif /* CONFIG_KCONSOLE */
|
---|
314 | }
|
---|
315 |
|
---|
316 | /** @}
|
---|
317 | */
|
---|