source: mainline/kernel/generic/src/main/kinit.c@ 0f4f1b2

Last change on this file since 0f4f1b2 was 0f4f1b2, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 2 years ago

Add (and use) functions thread_start() and thread_detach()

Mostly cosmetic, with thread_start() replacing calls to thread_ready(),
but not consuming the passed reference, and thread_detach() being
synonym for thread_put(). Makes the code's function more obvious.

Also modify some threaded tests to use thread_join() for waiting,
instead of counting threads with atomics or semaphores.

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