source: mainline/kernel/generic/src/main/kinit.c@ 24345a5

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 24345a5 was 24345a5, checked in by Jiri Svoboda <jirik.svoboda@…>, 17 years ago

Set meaningful names for loaded programs. Now 'tasks' kconsole command is much less obscure.

  • Property mode set to 100644
File size: 5.3 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
[8e3bf3e2]29/** @addtogroup main
[b45c443]30 * @{
31 */
32
[9179d0a]33/**
[b45c443]34 * @file
[9179d0a]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
[f761f1eb]44#include <main/kinit.h>
[2677758]45#include <config.h>
46#include <arch.h>
[f761f1eb]47#include <proc/scheduler.h>
48#include <proc/task.h>
49#include <proc/thread.h>
[c98e6ee]50#include <proc/program.h>
[f761f1eb]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>
[20d50a1]57#include <mm/as.h>
[ff3b3197]58#include <mm/frame.h>
[9c0a9b3]59#include <print.h>
[399ccd9]60#include <memstr.h>
[ff3b3197]61#include <console/console.h>
[aace6624]62#include <interrupt.h>
[f4338d2]63#include <console/kconsole.h>
[1077d91]64#include <security/cap.h>
[d4b5542]65#include <lib/rd.h>
[b3f8fb7]66#include <ipc/ipc.h>
[f761f1eb]67
[5f85c91]68#ifdef CONFIG_SMP
[26678e5]69#include <smp/smp.h>
[5f85c91]70#endif /* CONFIG_SMP */
[f761f1eb]71
72#include <synch/waitq.h>
73#include <synch/spinlock.h>
74
[880de6e]75/** Kernel initialization thread.
76 *
77 * kinit takes care of higher level kernel
78 * initialization (i.e. thread creation,
79 * userspace initialization etc.).
80 *
81 * @param arg Not used.
82 */
[f761f1eb]83void kinit(void *arg)
84{
[80d2bdb]85 thread_t *t;
[f761f1eb]86
[2cb5e64]87 /*
88 * Detach kinit as nobody will call thread_join_timeout() on it.
89 */
90 thread_detach(THREAD);
91
[22f7769]92 interrupts_disable();
[f761f1eb]93
[5f85c91]94#ifdef CONFIG_SMP
[f761f1eb]95 if (config.cpu_count > 1) {
[26678e5]96 waitq_initialize(&ap_completion_wq);
[f761f1eb]97 /*
98 * Create the kmp thread and wait for its completion.
99 * cpu1 through cpuN-1 will come up consecutively and
[169c408]100 * not mess together with kcpulb threads.
[f761f1eb]101 * Just a beautification.
102 */
[6f4495f5]103 if ((t = thread_create(kmp, NULL, TASK, THREAD_FLAG_WIRED,
104 "kmp", true))) {
[f761f1eb]105 spinlock_lock(&t->lock);
106 t->cpu = &cpus[0];
107 spinlock_unlock(&t->lock);
108 thread_ready(t);
[8e3bf3e2]109 } else
[2cb5e64]110 panic("thread_create/kmp\n");
111 thread_join(t);
[c778c1a]112 thread_detach(t);
[f761f1eb]113 }
[5f85c91]114#endif /* CONFIG_SMP */
[f761f1eb]115 /*
116 * Now that all CPUs are up, we can report what we've found.
117 */
[0132630]118 cpu_list();
[f761f1eb]119
[5f85c91]120#ifdef CONFIG_SMP
[f761f1eb]121 if (config.cpu_count > 1) {
[e5dbbe5]122 count_t i;
[0132630]123
[76cec1e]124 /*
[f761f1eb]125 * For each CPU, create its load balancing thread.
126 */
127 for (i = 0; i < config.cpu_count; i++) {
128
[6f4495f5]129 if ((t = thread_create(kcpulb, NULL, TASK,
130 THREAD_FLAG_WIRED, "kcpulb", true))) {
[f761f1eb]131 spinlock_lock(&t->lock);
132 t->cpu = &cpus[i];
133 spinlock_unlock(&t->lock);
134 thread_ready(t);
[8e3bf3e2]135 } else
136 panic("thread_create/kcpulb\n");
[f761f1eb]137
138 }
139 }
[5f85c91]140#endif /* CONFIG_SMP */
[f761f1eb]141
[a83a802]142 /*
143 * At this point SMP, if present, is configured.
144 */
145 arch_post_smp_init();
146
[2677758]147 /*
148 * Create kernel console.
149 */
[1e9d0e3]150 t = thread_create(kconsole, (void *) "kconsole", TASK, 0, "kconsole",
151 false);
[6f4495f5]152 if (t)
[2677758]153 thread_ready(t);
[77147d6]154 else
155 panic("thread_create/kconsole\n");
[2677758]156
[22f7769]157 interrupts_enable();
[49eec93]158
159 /*
160 * Create user tasks, load RAM disk images.
161 */
[b6b576c]162 count_t i;
[c98e6ee]163 program_t programs[CONFIG_INIT_TASKS];
[49eec93]164
[b6b576c]165 for (i = 0; i < init.cnt; i++) {
[228b135]166 if (init.tasks[i].addr % FRAME_SIZE) {
[49eec93]167 printf("init[%" PRIc "].addr is not frame aligned", i);
[228b135]168 continue;
169 }
[ef67bab]170
[c98e6ee]171 int rc = program_create_from_image((void *) init.tasks[i].addr,
[24345a5]172 "init-bin", &programs[i]);
[c98e6ee]173
174 if (rc == 0 && programs[i].task != NULL) {
[1077d91]175 /*
176 * Set capabilities to init userspace tasks.
177 */
[c98e6ee]178 cap_set(programs[i].task, CAP_CAP | CAP_MEM_MANAGER |
[6f4495f5]179 CAP_IO_MANAGER | CAP_PREEMPT_CONTROL | CAP_IRQ_REG);
[1077d91]180
[49eec93]181 if (!ipc_phone_0)
[c98e6ee]182 ipc_phone_0 = &programs[i].task->answerbox;
183 } else if (rc == 0) {
184 /* It was the program loader and was registered */
[228b135]185 } else {
[c98e6ee]186 /* RAM disk image */
[6387c18]187 int rd = init_rd((rd_header_t *) init.tasks[i].addr,
[6f4495f5]188 init.tasks[i].size);
[228b135]189
[058b021]190 if (rd != RE_OK)
[1e9d0e3]191 printf("Init binary %" PRIc " not used, error "
192 "code %d.\n", i, rd);
[49eec93]193 }
194 }
195
196 /*
197 * Run user tasks with reasonable delays
198 */
199 for (i = 0; i < init.cnt; i++) {
[c98e6ee]200 if (programs[i].task != NULL) {
[49eec93]201 thread_usleep(50000);
[c98e6ee]202 program_ready(&programs[i]);
[228b135]203 }
[44c259c]204 }
[f761f1eb]205
[ff3b3197]206 if (!stdin) {
207 while (1) {
208 thread_sleep(1);
209 printf("kinit... ");
210 }
[f761f1eb]211 }
212}
[b45c443]213
[8e3bf3e2]214/** @}
[b45c443]215 */
Note: See TracBrowser for help on using the repository browser.