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