HelenOS sources
This source file includes following definitions.
- kinit
#include <assert.h>
#include <main/kinit.h>
#include <config.h>
#include <arch.h>
#include <proc/scheduler.h>
#include <proc/task.h>
#include <proc/thread.h>
#include <proc/program.h>
#include <panic.h>
#include <halt.h>
#include <cpu.h>
#include <arch/asm.h>
#include <mm/page.h>
#include <arch/mm/page.h>
#include <mm/as.h>
#include <mm/frame.h>
#include <mm/km.h>
#include <stdio.h>
#include <log.h>
#include <memw.h>
#include <console/console.h>
#include <interrupt.h>
#include <console/kconsole.h>
#include <security/perm.h>
#include <lib/rd.h>
#include <ipc/ipc.h>
#include <str.h>
#include <str_error.h>
#include <sysinfo/stats.h>
#include <sysinfo/sysinfo.h>
#include <align.h>
#include <stdlib.h>
#include <debug/register.h>
#ifdef CONFIG_SMP
#include <smp/smp.h>
#endif
#include <synch/waitq.h>
#include <synch/spinlock.h>
#define ALIVE_CHARS 4
#ifdef CONFIG_KCONSOLE
static char alive[ALIVE_CHARS] = "-\\|/";
#endif
#define INIT_PREFIX "init:"
#define INIT_PREFIX_LEN 5
void kinit(void *arg)
{
thread_t *thread;
interrupts_disable();
#ifdef CONFIG_SMP
if (config.cpu_count > 1) {
semaphore_initialize(&ap_completion_semaphore, 0);
thread = thread_create(kmp, NULL, TASK,
THREAD_FLAG_UNCOUNTED, "kmp");
if (!thread)
panic("Unable to create kmp thread.");
thread_wire(thread, &cpus[0]);
thread_start(thread);
thread_join(thread);
unsigned int i;
for (i = 0; i < config.cpu_count; i++) {
thread = thread_create(kcpulb, NULL, TASK,
THREAD_FLAG_UNCOUNTED, "kcpulb");
if (thread != NULL) {
thread_wire(thread, &cpus[i]);
thread_start(thread);
thread_detach(thread);
} else
log(LF_OTHER, LVL_ERROR,
"Unable to create kcpulb thread for cpu%u", i);
}
}
#endif
ARCH_OP(post_smp_init);
thread = thread_create(kload, NULL, TASK, THREAD_FLAG_NONE,
"kload");
if (thread != NULL) {
thread_start(thread);
thread_detach(thread);
} else {
log(LF_OTHER, LVL_ERROR, "Unable to create kload thread");
}
#ifdef CONFIG_KCONSOLE
if (stdin) {
thread = thread_create(kconsole_thread, NULL, TASK,
THREAD_FLAG_NONE, "kconsole");
if (thread != NULL) {
thread_start(thread);
thread_detach(thread);
} else {
log(LF_OTHER, LVL_ERROR,
"Unable to create kconsole thread");
}
}
#endif
sysinfo_set_item_val("default.stack_size", NULL, STACK_SIZE_USER);
interrupts_enable();
size_t i;
program_t programs[CONFIG_INIT_TASKS] = { };
for (i = 0; i < init.cnt; i++) {
const char *arguments = init.tasks[i].arguments;
if (str_length(arguments) == 0)
continue;
if (str_length(init.tasks[i].name) == 0)
continue;
size_t arguments_size = str_size(arguments);
void *arguments_copy = malloc(arguments_size);
if (arguments_copy == NULL)
continue;
memcpy(arguments_copy, arguments, arguments_size);
char item_name[CONFIG_TASK_NAME_BUFLEN + 15];
snprintf(item_name, CONFIG_TASK_NAME_BUFLEN + 15,
"init_args.%s", init.tasks[i].name);
sysinfo_set_item_data(item_name, NULL, arguments_copy, arguments_size);
}
for (i = 0; i < init.cnt; i++) {
if (init.tasks[i].paddr % FRAME_SIZE) {
log(LF_OTHER, LVL_ERROR,
"init[%zu]: Address is not frame aligned", i);
programs[i].task = NULL;
continue;
}
char namebuf[TASK_NAME_BUFLEN];
const char *name = init.tasks[i].name;
if (name[0] == 0)
name = "<unknown>";
static_assert(TASK_NAME_BUFLEN >= INIT_PREFIX_LEN, "");
str_cpy(namebuf, TASK_NAME_BUFLEN, INIT_PREFIX);
str_cpy(namebuf + INIT_PREFIX_LEN,
TASK_NAME_BUFLEN - INIT_PREFIX_LEN, name);
uintptr_t page = km_map(init.tasks[i].paddr,
init.tasks[i].size, PAGE_SIZE,
PAGE_READ | PAGE_WRITE | PAGE_CACHEABLE);
assert(page);
if (str_cmp(name, "kernel.dbg") == 0) {
register_debug_data((void *) page, init.tasks[i].size);
programs[i].task = NULL;
continue;
}
if (str_cmp(name, "loader") == 0) {
if (program_loader == NULL) {
program_loader = (void *) page;
log(LF_OTHER, LVL_NOTE, "Program loader at %p",
program_loader);
} else {
log(LF_OTHER, LVL_ERROR,
"init[%zu]: Second binary named \"loader\""
" present.", i);
}
programs[i].task = NULL;
continue;
}
errno_t rc = program_create_from_image((void *) page, init.tasks[i].size, namebuf,
&programs[i]);
if (rc == 0) {
assert(programs[i].task != NULL);
perm_set(programs[i].task,
PERM_PERM | PERM_MEM_MANAGER |
PERM_IO_MANAGER | PERM_IRQ_REG);
if (!ipc_box_0) {
ipc_box_0 = &programs[i].task->answerbox;
task_hold(programs[i].task);
}
} else if (str_cmp(name, "initrd.img") == 0) {
init_rd((void *) init.tasks[i].paddr, init.tasks[i].size);
} else {
log(LF_OTHER, LVL_ERROR,
"init[%zu]: Init binary load failed "
"(error %s, loader status %s)", i,
str_error_name(rc), str_error_name(programs[i].loader_status));
}
}
for (i = 0; i < init.cnt; i++) {
if (programs[i].task != NULL) {
program_ready(&programs[i]);
task_release(programs[i].task);
}
}
#ifdef CONFIG_KCONSOLE
if (!stdin) {
thread_sleep(10);
printf("kinit: No stdin\nKernel alive: .");
unsigned int i = 0;
while (true) {
printf("\b%c", alive[i % ALIVE_CHARS]);
thread_sleep(1);
i++;
}
}
#endif
}
HelenOS homepage, sources at GitHub