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 <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 <print.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 |
|
---|
75 | #ifdef CONFIG_SMP
|
---|
76 | #include <smp/smp.h>
|
---|
77 | #endif /* CONFIG_SMP */
|
---|
78 |
|
---|
79 | #include <synch/waitq.h>
|
---|
80 | #include <synch/spinlock.h>
|
---|
81 | #include <synch/workqueue.h>
|
---|
82 | #include <synch/rcu.h>
|
---|
83 |
|
---|
84 | #define ALIVE_CHARS 4
|
---|
85 |
|
---|
86 | #ifdef CONFIG_KCONSOLE
|
---|
87 | static char alive[ALIVE_CHARS] = "-\\|/";
|
---|
88 | #endif
|
---|
89 |
|
---|
90 | #define INIT_PREFIX "init:"
|
---|
91 | #define INIT_PREFIX_LEN 5
|
---|
92 |
|
---|
93 | /** Kernel initialization thread.
|
---|
94 | *
|
---|
95 | * kinit takes care of higher level kernel
|
---|
96 | * initialization (i.e. thread creation,
|
---|
97 | * userspace initialization etc.).
|
---|
98 | *
|
---|
99 | * @param arg Not used.
|
---|
100 | */
|
---|
101 | void kinit(void *arg)
|
---|
102 | {
|
---|
103 | thread_t *thread;
|
---|
104 |
|
---|
105 | /*
|
---|
106 | * Detach kinit as nobody will call thread_join_timeout() on it.
|
---|
107 | */
|
---|
108 | thread_detach(THREAD);
|
---|
109 |
|
---|
110 | interrupts_disable();
|
---|
111 |
|
---|
112 | /* Start processing RCU callbacks. RCU is fully functional afterwards. */
|
---|
113 | rcu_kinit_init();
|
---|
114 |
|
---|
115 | /*
|
---|
116 | * Start processing work queue items. Some may have been queued during boot.
|
---|
117 | */
|
---|
118 | workq_global_worker_init();
|
---|
119 |
|
---|
120 | #ifdef CONFIG_SMP
|
---|
121 | if (config.cpu_count > 1) {
|
---|
122 | waitq_initialize(&ap_completion_wq);
|
---|
123 |
|
---|
124 | /*
|
---|
125 | * Create the kmp thread and wait for its completion.
|
---|
126 | * cpu1 through cpuN-1 will come up consecutively and
|
---|
127 | * not mess together with kcpulb threads.
|
---|
128 | * Just a beautification.
|
---|
129 | */
|
---|
130 | thread = thread_create(kmp, NULL, TASK,
|
---|
131 | THREAD_FLAG_UNCOUNTED, "kmp");
|
---|
132 | if (thread != NULL) {
|
---|
133 | thread_wire(thread, &cpus[0]);
|
---|
134 | thread_ready(thread);
|
---|
135 | } else
|
---|
136 | panic("Unable to create kmp thread.");
|
---|
137 |
|
---|
138 | thread_join(thread);
|
---|
139 | thread_detach(thread);
|
---|
140 |
|
---|
141 | /*
|
---|
142 | * For each CPU, create its load balancing thread.
|
---|
143 | */
|
---|
144 | unsigned int i;
|
---|
145 |
|
---|
146 | for (i = 0; i < config.cpu_count; i++) {
|
---|
147 | thread = thread_create(kcpulb, NULL, TASK,
|
---|
148 | THREAD_FLAG_UNCOUNTED, "kcpulb");
|
---|
149 | if (thread != NULL) {
|
---|
150 | thread_wire(thread, &cpus[i]);
|
---|
151 | thread_ready(thread);
|
---|
152 | } else
|
---|
153 | log(LF_OTHER, LVL_ERROR,
|
---|
154 | "Unable to create kcpulb thread for cpu%u", i);
|
---|
155 | }
|
---|
156 | }
|
---|
157 | #endif /* CONFIG_SMP */
|
---|
158 |
|
---|
159 | /*
|
---|
160 | * At this point SMP, if present, is configured.
|
---|
161 | */
|
---|
162 | ARCH_OP(post_smp_init);
|
---|
163 |
|
---|
164 | /* Start thread computing system load */
|
---|
165 | thread = thread_create(kload, NULL, TASK, THREAD_FLAG_NONE,
|
---|
166 | "kload");
|
---|
167 | if (thread != NULL)
|
---|
168 | thread_ready(thread);
|
---|
169 | else
|
---|
170 | log(LF_OTHER, LVL_ERROR, "Unable to create kload thread");
|
---|
171 |
|
---|
172 | #ifdef CONFIG_KCONSOLE
|
---|
173 | if (stdin) {
|
---|
174 | /*
|
---|
175 | * Create kernel console.
|
---|
176 | */
|
---|
177 | thread = thread_create(kconsole_thread, NULL, TASK,
|
---|
178 | THREAD_FLAG_NONE, "kconsole");
|
---|
179 | if (thread != NULL)
|
---|
180 | thread_ready(thread);
|
---|
181 | else
|
---|
182 | log(LF_OTHER, LVL_ERROR,
|
---|
183 | "Unable to create kconsole thread");
|
---|
184 | }
|
---|
185 | #endif /* CONFIG_KCONSOLE */
|
---|
186 |
|
---|
187 | /*
|
---|
188 | * Store the default stack size in sysinfo so that uspace can create
|
---|
189 | * stack with this default size.
|
---|
190 | */
|
---|
191 | sysinfo_set_item_val("default.stack_size", NULL, STACK_SIZE_USER);
|
---|
192 |
|
---|
193 | interrupts_enable();
|
---|
194 |
|
---|
195 | /*
|
---|
196 | * Create user tasks, load RAM disk images.
|
---|
197 | */
|
---|
198 | size_t i;
|
---|
199 | program_t programs[CONFIG_INIT_TASKS];
|
---|
200 |
|
---|
201 | // FIXME: do not propagate arguments through sysinfo
|
---|
202 | // but pass them directly to the tasks
|
---|
203 | for (i = 0; i < init.cnt; i++) {
|
---|
204 | const char *arguments = init.tasks[i].arguments;
|
---|
205 | if (str_length(arguments) == 0)
|
---|
206 | continue;
|
---|
207 | if (str_length(init.tasks[i].name) == 0)
|
---|
208 | continue;
|
---|
209 | size_t arguments_size = str_size(arguments);
|
---|
210 |
|
---|
211 | void *arguments_copy = malloc(arguments_size, 0);
|
---|
212 | if (arguments_copy == NULL)
|
---|
213 | continue;
|
---|
214 | memcpy(arguments_copy, arguments, arguments_size);
|
---|
215 |
|
---|
216 | char item_name[CONFIG_TASK_NAME_BUFLEN + 15];
|
---|
217 | snprintf(item_name, CONFIG_TASK_NAME_BUFLEN + 15,
|
---|
218 | "init_args.%s", init.tasks[i].name);
|
---|
219 |
|
---|
220 | sysinfo_set_item_data(item_name, NULL, arguments_copy, arguments_size);
|
---|
221 | }
|
---|
222 |
|
---|
223 | for (i = 0; i < init.cnt; i++) {
|
---|
224 | if (init.tasks[i].paddr % FRAME_SIZE) {
|
---|
225 | log(LF_OTHER, LVL_ERROR,
|
---|
226 | "init[%zu]: Address is not frame aligned", i);
|
---|
227 | programs[i].task = NULL;
|
---|
228 | continue;
|
---|
229 | }
|
---|
230 |
|
---|
231 | /*
|
---|
232 | * Construct task name from the 'init:' prefix and the
|
---|
233 | * name stored in the init structure (if any).
|
---|
234 | */
|
---|
235 |
|
---|
236 | char namebuf[TASK_NAME_BUFLEN];
|
---|
237 |
|
---|
238 | const char *name = init.tasks[i].name;
|
---|
239 | if (name[0] == 0)
|
---|
240 | name = "<unknown>";
|
---|
241 |
|
---|
242 | static_assert(TASK_NAME_BUFLEN >= INIT_PREFIX_LEN, "");
|
---|
243 | str_cpy(namebuf, TASK_NAME_BUFLEN, INIT_PREFIX);
|
---|
244 | str_cpy(namebuf + INIT_PREFIX_LEN,
|
---|
245 | TASK_NAME_BUFLEN - INIT_PREFIX_LEN, name);
|
---|
246 |
|
---|
247 | /*
|
---|
248 | * Create virtual memory mappings for init task images.
|
---|
249 | */
|
---|
250 | uintptr_t page = km_map(init.tasks[i].paddr,
|
---|
251 | init.tasks[i].size,
|
---|
252 | PAGE_READ | PAGE_WRITE | PAGE_CACHEABLE);
|
---|
253 | assert(page);
|
---|
254 |
|
---|
255 | if (str_cmp(name, "loader") == 0) {
|
---|
256 | /* Register image as the program loader */
|
---|
257 | if (program_loader == NULL) {
|
---|
258 | program_loader = (void *) page;
|
---|
259 | log(LF_OTHER, LVL_NOTE, "Program loader at %p",
|
---|
260 | program_loader);
|
---|
261 | } else {
|
---|
262 | log(LF_OTHER, LVL_ERROR,
|
---|
263 | "init[%zu]: Second binary named \"loader\""
|
---|
264 | " present.", i);
|
---|
265 | }
|
---|
266 | continue;
|
---|
267 | }
|
---|
268 |
|
---|
269 | errno_t rc = program_create_from_image((void *) page, namebuf,
|
---|
270 | &programs[i]);
|
---|
271 |
|
---|
272 | if (rc == 0) {
|
---|
273 | assert(programs[i].task != NULL);
|
---|
274 |
|
---|
275 | /*
|
---|
276 | * Set permissions to init userspace tasks.
|
---|
277 | */
|
---|
278 | perm_set(programs[i].task,
|
---|
279 | PERM_PERM | PERM_MEM_MANAGER |
|
---|
280 | PERM_IO_MANAGER | PERM_IRQ_REG);
|
---|
281 |
|
---|
282 | if (!ipc_box_0) {
|
---|
283 | ipc_box_0 = &programs[i].task->answerbox;
|
---|
284 | /*
|
---|
285 | * Hold the first task so that
|
---|
286 | * ipc_box_0 remains a valid pointer
|
---|
287 | * even if the first task exits for
|
---|
288 | * whatever reason.
|
---|
289 | */
|
---|
290 | task_hold(programs[i].task);
|
---|
291 | }
|
---|
292 |
|
---|
293 | } else if (i == init.cnt - 1) {
|
---|
294 | /*
|
---|
295 | * Assume the last task is the RAM disk.
|
---|
296 | */
|
---|
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 %u)", i,
|
---|
302 | str_error_name(rc), 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 | }
|
---|
313 |
|
---|
314 | #ifdef CONFIG_KCONSOLE
|
---|
315 | if (!stdin) {
|
---|
316 | thread_sleep(10);
|
---|
317 | printf("kinit: No stdin\nKernel alive: .");
|
---|
318 |
|
---|
319 | unsigned int i = 0;
|
---|
320 | while (true) {
|
---|
321 | printf("\b%c", alive[i % ALIVE_CHARS]);
|
---|
322 | thread_sleep(1);
|
---|
323 | i++;
|
---|
324 | }
|
---|
325 | }
|
---|
326 | #endif /* CONFIG_KCONSOLE */
|
---|
327 | }
|
---|
328 |
|
---|
329 | /** @}
|
---|
330 | */
|
---|