source: mainline/kernel/generic/src/main/kinit.c@ 8f6858d0

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 8f6858d0 was 6eef3c4, checked in by Martin Decky <martin@…>, 13 years ago

cleanup thread_create() and thread_t structure

  • remove 'flag' bitfield from thread_t, use individual boolean flags (can be optimized later on by adding : 1)
  • use an enum for call flags, add THREAD_FLAG_NONE
  • remove the 'uncounted' argument in favour of a call flag
  • introduce thread_wire() to setup wired threads
  • Property mode set to 100644
File size: 6.6 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 <align.h>
72
73#ifdef CONFIG_SMP
74#include <smp/smp.h>
75#endif /* CONFIG_SMP */
76
77#include <synch/waitq.h>
78#include <synch/spinlock.h>
79
80#define ALIVE_CHARS 4
81
82#ifdef CONFIG_KCONSOLE
83static char alive[ALIVE_CHARS] = "-\\|/";
84#endif
85
86#define INIT_PREFIX "init:"
87#define INIT_PREFIX_LEN 5
88
89/** Kernel initialization thread.
90 *
91 * kinit takes care of higher level kernel
92 * initialization (i.e. thread creation,
93 * userspace initialization etc.).
94 *
95 * @param arg Not used.
96 */
97void kinit(void *arg)
98{
99 thread_t *thread;
100
101 /*
102 * Detach kinit as nobody will call thread_join_timeout() on it.
103 */
104 thread_detach(THREAD);
105
106 interrupts_disable();
107
108#ifdef CONFIG_SMP
109 if (config.cpu_count > 1) {
110 waitq_initialize(&ap_completion_wq);
111
112 /*
113 * Create the kmp thread and wait for its completion.
114 * cpu1 through cpuN-1 will come up consecutively and
115 * not mess together with kcpulb threads.
116 * Just a beautification.
117 */
118 thread = thread_create(kmp, NULL, TASK,
119 THREAD_FLAG_UNCOUNTED, "kmp");
120 if (thread != NULL) {
121 thread_wire(thread, &cpus[0]);
122 thread_ready(thread);
123 } else
124 panic("Unable to create kmp thread.");
125
126 thread_join(thread);
127 thread_detach(thread);
128
129 /*
130 * For each CPU, create its load balancing thread.
131 */
132 unsigned int i;
133
134 for (i = 0; i < config.cpu_count; i++) {
135 thread = thread_create(kcpulb, NULL, TASK,
136 THREAD_FLAG_UNCOUNTED, "kcpulb");
137 if (thread != NULL) {
138 thread_wire(thread, &cpus[i]);
139 thread_ready(thread);
140 } else
141 printf("Unable to create kcpulb thread for cpu%u\n", i);
142 }
143 }
144#endif /* CONFIG_SMP */
145
146 /*
147 * At this point SMP, if present, is configured.
148 */
149 arch_post_smp_init();
150
151 /* Start thread computing system load */
152 thread = thread_create(kload, NULL, TASK, THREAD_FLAG_NONE,
153 "kload");
154 if (thread != NULL)
155 thread_ready(thread);
156 else
157 printf("Unable to create kload thread\n");
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_ready(thread);
168 else
169 printf("Unable to create kconsole thread\n");
170 }
171#endif /* CONFIG_KCONSOLE */
172
173 interrupts_enable();
174
175 /*
176 * Create user tasks, load RAM disk images.
177 */
178 size_t i;
179 program_t programs[CONFIG_INIT_TASKS];
180
181 for (i = 0; i < init.cnt; i++) {
182 if (init.tasks[i].paddr % FRAME_SIZE) {
183 printf("init[%zu]: Address is not frame aligned\n", i);
184 programs[i].task = NULL;
185 continue;
186 }
187
188 /*
189 * Construct task name from the 'init:' prefix and the
190 * name stored in the init structure (if any).
191 */
192
193 char namebuf[TASK_NAME_BUFLEN];
194
195 const char *name = init.tasks[i].name;
196 if (name[0] == 0)
197 name = "<unknown>";
198
199 ASSERT(TASK_NAME_BUFLEN >= INIT_PREFIX_LEN);
200 str_cpy(namebuf, TASK_NAME_BUFLEN, INIT_PREFIX);
201 str_cpy(namebuf + INIT_PREFIX_LEN,
202 TASK_NAME_BUFLEN - INIT_PREFIX_LEN, name);
203
204 /*
205 * Create virtual memory mappings for init task images.
206 */
207 uintptr_t page = km_map(init.tasks[i].paddr,
208 init.tasks[i].size,
209 PAGE_READ | PAGE_WRITE | PAGE_CACHEABLE);
210 ASSERT(page);
211
212 int rc = program_create_from_image((void *) page, namebuf,
213 &programs[i]);
214
215 if (rc == 0) {
216 if (programs[i].task != NULL) {
217 /*
218 * Set capabilities to init userspace tasks.
219 */
220 cap_set(programs[i].task, CAP_CAP | CAP_MEM_MANAGER |
221 CAP_IO_MANAGER | CAP_IRQ_REG);
222
223 if (!ipc_phone_0)
224 ipc_phone_0 = &programs[i].task->answerbox;
225 }
226
227 /*
228 * If programs[i].task == NULL then it is
229 * the program loader and it was registered
230 * successfully.
231 */
232 } else if (i == init.cnt - 1) {
233 /*
234 * Assume the last task is the RAM disk.
235 */
236 init_rd((void *) init.tasks[i].paddr, init.tasks[i].size);
237 } else
238 printf("init[%zu]: Init binary load failed "
239 "(error %d, loader status %u)\n", i, rc,
240 programs[i].loader_status);
241 }
242
243 /*
244 * Run user tasks.
245 */
246 for (i = 0; i < init.cnt; i++) {
247 if (programs[i].task != NULL)
248 program_ready(&programs[i]);
249 }
250
251#ifdef CONFIG_KCONSOLE
252 if (!stdin) {
253 thread_sleep(10);
254 printf("kinit: No stdin\nKernel alive: .");
255
256 unsigned int i = 0;
257 while (true) {
258 printf("\b%c", alive[i % ALIVE_CHARS]);
259 thread_sleep(1);
260 i++;
261 }
262 }
263#endif /* CONFIG_KCONSOLE */
264}
265
266/** @}
267 */
Note: See TracBrowser for help on using the repository browser.