Subversion Repositories HelenOS

Compare Revisions

Ignore whitespace Rev HEAD → Rev 2725

/trunk/kernel/generic/src/main/main.c
32,7 → 32,7
 
/**
* @file
* @brief Main initialization kernel function for all processors.
* @brief Main initialization kernel function for all processors.
*
* During kernel boot, all processors, after architecture dependent
* initialization, start executing code found in this file. After
57,11 → 57,9
#include <proc/scheduler.h>
#include <proc/thread.h>
#include <proc/task.h>
#include <proc/tasklet.h>
#include <main/kinit.h>
#include <main/version.h>
#include <console/kconsole.h>
#include <console/console.h>
#include <cpu.h>
#include <align.h>
#include <interrupt.h>
79,10 → 77,9
#include <ipc/ipc.h>
#include <macros.h>
#include <adt/btree.h>
#include <console/klog.h>
#include <smp/smp.h>
#include <ddi/ddi.h>
#include <main/main.h>
#include <ipc/event.h>
 
/** Global configuration structure. */
config_t config;
89,7 → 86,7
 
/** Initial user-space tasks */
init_t init = {
.cnt = 0
0
};
 
/** Boot allocations. */
105,15 → 102,17
* the linker or the low level assembler code with
* appropriate sizes and addresses.
*/
uintptr_t hardcoded_load_address = 0; /**< Virtual address of where the kernel
* is loaded. */
size_t hardcoded_ktext_size = 0; /**< Size of the kernel code in bytes.
*/
size_t hardcoded_kdata_size = 0; /**< Size of the kernel data in bytes.
*/
uintptr_t stack_safe = 0; /**< Lowest safe stack virtual address.
*/
 
/** Virtual address of where the kernel is loaded. */
uintptr_t hardcoded_load_address = 0;
/** Size of the kernel code in bytes. */
size_t hardcoded_ktext_size = 0;
/** Size of the kernel data in bytes. */
size_t hardcoded_kdata_size = 0;
/** Lowest safe stack virtual address. */
uintptr_t stack_safe = 0;
void main_bsp(void);
void main_ap(void);
 
/*
* These two functions prevent stack from underflowing during the
130,11 → 129,9
 
/** Main kernel routine for bootstrap CPU.
*
* The code here still runs on the boot stack, which knows nothing about
* preemption counts. Because of that, this function cannot directly call
* functions that disable or enable preemption (e.g. spinlock_lock()). The
* primary task of this function is to calculate address of a new stack and
* switch to it.
* Initializes the kernel by bootstrap CPU.
* This function passes control directly to
* main_bsp_separated_stack().
*
* Assuming interrupts_disable().
*
153,7 → 150,7
config.stack_base = config.base + config.kernel_size;
/* Avoid placing stack on top of init */
size_t i;
count_t i;
for (i = 0; i < init.cnt; i++) {
if (PA_overlaps(config.stack_base, config.stack_size,
init.tasks[i].addr, init.tasks[i].size))
187,93 → 184,89
*/
void main_bsp_separated_stack(void)
{
/* Keep this the first thing. */
task_t *k;
thread_t *t;
count_t i;
the_initialize(THE);
version_print();
LOG("\nconfig.base=%#" PRIp " config.kernel_size=%" PRIs
"\nconfig.stack_base=%#" PRIp " config.stack_size=%" PRIs,
config.base, config.kernel_size, config.stack_base,
config.stack_size);
#ifdef CONFIG_KCONSOLE
 
/*
* kconsole data structures must be initialized very early
* because other subsystems will register their respective
* commands.
*/
LOG_EXEC(kconsole_init());
#endif
kconsole_init();
/*
* Exception handler initialization, before architecture
* starts adding its own handlers
*/
LOG_EXEC(exc_init());
exc_init();
 
/*
* Memory management subsystems initialization.
*/
LOG_EXEC(arch_pre_mm_init());
LOG_EXEC(frame_init());
*/
arch_pre_mm_init();
frame_init();
/* Initialize at least 1 memory segment big enough for slab to work. */
LOG_EXEC(slab_cache_init());
LOG_EXEC(btree_init());
LOG_EXEC(as_init());
LOG_EXEC(page_init());
LOG_EXEC(tlb_init());
LOG_EXEC(ddi_init());
LOG_EXEC(tasklet_init());
LOG_EXEC(arch_post_mm_init());
LOG_EXEC(arch_pre_smp_init());
LOG_EXEC(smp_init());
slab_cache_init();
btree_init();
as_init();
page_init();
tlb_init();
ddi_init();
arch_post_mm_init();
version_print();
printf("kernel: %.*p hardcoded_ktext_size=%zd KB, "
"hardcoded_kdata_size=%zd KB\n", sizeof(uintptr_t) * 2,
config.base, SIZE2KB(hardcoded_ktext_size),
SIZE2KB(hardcoded_kdata_size));
printf("stack: %.*p size=%zd KB\n", sizeof(uintptr_t) * 2,
config.stack_base, SIZE2KB(config.stack_size));
arch_pre_smp_init();
smp_init();
/* Slab must be initialized after we know the number of processors. */
LOG_EXEC(slab_enable_cpucache());
slab_enable_cpucache();
printf("Detected %" PRIs " CPU(s), %" PRIu64" MiB free memory\n",
config.cpu_count, SIZE2MB(zone_total_size()));
printf("Detected %zu CPU(s), %llu MB free memory\n",
config.cpu_count, SIZE2MB(zone_total_size()));
cpu_init();
LOG_EXEC(cpu_init());
calibrate_delay_loop();
clock_counter_init();
timeout_init();
scheduler_init();
task_init();
thread_init();
futex_init();
klog_init();
LOG_EXEC(calibrate_delay_loop());
LOG_EXEC(clock_counter_init());
LOG_EXEC(timeout_init());
LOG_EXEC(scheduler_init());
LOG_EXEC(task_init());
LOG_EXEC(thread_init());
LOG_EXEC(futex_init());
if (init.cnt > 0) {
size_t i;
for (i = 0; i < init.cnt; i++)
LOG("init[%" PRIs "].addr=%#" PRIp ", init[%" PRIs
"].size=%#" PRIs, i, init.tasks[i].addr, i,
printf("init[%zd].addr=%.*p, init[%zd].size=%zd\n", i,
sizeof(uintptr_t) * 2, init.tasks[i].addr, i,
init.tasks[i].size);
} else
printf("No init binaries found.\n");
printf("No init binaries found\n");
LOG_EXEC(ipc_init());
LOG_EXEC(event_init());
LOG_EXEC(klog_init());
ipc_init();
 
/*
* Create kernel task.
*/
task_t *kernel = task_create(AS_KERNEL, "kernel");
if (!kernel)
panic("Cannot create kernel task.");
k = task_create(AS_KERNEL, "kernel");
if (!k)
panic("can't create kernel task\n");
/*
* Create the first thread.
*/
thread_t *kinit_thread
= thread_create(kinit, NULL, kernel, 0, "kinit", true);
if (!kinit_thread)
panic("Cannot create kinit thread.");
LOG_EXEC(thread_ready(kinit_thread));
t = thread_create(kinit, NULL, k, 0, "kinit", true);
if (!t)
panic("can't create kinit thread\n");
thread_ready(t);
/*
* This call to scheduler() will return to kinit,
326,7 → 319,6
* collide with another CPU coming up. To prevent this, we
* switch to this cpu's private stack prior to waking kmp up.
*/
context_save(&CPU->saved_context);
context_set(&CPU->saved_context, FADDR(main_ap_separated_stack),
(uintptr_t) CPU->stack, CPU_STACK_SIZE);
context_restore(&CPU->saved_context);
/trunk/kernel/generic/src/main/version.c
34,22 → 34,21
 
#include <main/version.h>
#include <print.h>
#include <macros.h>
 
char *project = "SPARTAN kernel";
char *copyright = "Copyright (c) 2001-2009 HelenOS project";
char *release = STRING(RELEASE);
char *name = STRING(NAME);
char *arch = STRING(KARCH);
char *copyright = "Copyright (c) 2001-2008 HelenOS project";
char *release = RELEASE;
char *name = NAME;
char *arch = ARCH;
 
#ifdef REVISION
char *revision = ", revision " STRING(REVISION);
char *revision = ", revision " REVISION;
#else
char *revision = "";
#endif
 
#ifdef TIMESTAMP
char *timestamp = " on " STRING(TIMESTAMP);
char *timestamp = " on " TIMESTAMP;
#else
char *timestamp = "";
#endif
/trunk/kernel/generic/src/main/uinit.c
46,9 → 46,7
#include <userspace.h>
#include <mm/slab.h>
#include <arch.h>
#include <udebug/udebug.h>
 
 
/** Thread used to bring up userspace thread.
*
* @param arg Pointer to structure containing userspace entry and stack
67,10 → 65,6
* deployed for the event of forceful task termination.
*/
thread_detach(THREAD);
 
#ifdef CONFIG_UDEBUG
udebug_stoppable_end();
#endif
uarg.uspace_entry = ((uspace_arg_t *) arg)->uspace_entry;
uarg.uspace_stack = ((uspace_arg_t *) arg)->uspace_stack;
79,14 → 73,6
uarg.uspace_thread_arg = NULL;
 
free((uspace_arg_t *) arg);
/*
* Disable interrupts so that the execution of userspace() is not
* disturbed by any interrupts as some of the userspace()
* implementations will switch to the userspace stack before switching
* the mode.
*/
(void) interrupts_disable();
userspace(&uarg);
}
 
/trunk/kernel/generic/src/main/kinit.c
32,7 → 32,7
 
/**
* @file
* @brief Kernel initialization thread.
* @brief Kernel initialization thread.
*
* This file contains kinit kernel thread which carries out
* high level system initialization.
47,7 → 47,6
#include <proc/scheduler.h>
#include <proc/task.h>
#include <proc/thread.h>
#include <proc/program.h>
#include <panic.h>
#include <func.h>
#include <cpu.h>
64,8 → 63,6
#include <security/cap.h>
#include <lib/rd.h>
#include <ipc/ipc.h>
#include <debug.h>
#include <string.h>
 
#ifdef CONFIG_SMP
#include <smp/smp.h>
74,15 → 71,6
#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
 
/** Kernel initialization thread.
*
* kinit takes care of higher level kernel
93,19 → 81,16
*/
void kinit(void *arg)
{
thread_t *t;
 
#if defined(CONFIG_SMP) || defined(CONFIG_KCONSOLE)
thread_t *thread;
#endif
/*
* Detach kinit as nobody will call thread_join_timeout() on it.
*/
thread_detach(THREAD);
 
interrupts_disable();
#ifdef CONFIG_SMP
 
#ifdef CONFIG_SMP
if (config.cpu_count > 1) {
waitq_initialize(&ap_completion_wq);
/*
114,130 → 99,100
* not mess together with kcpulb threads.
* Just a beautification.
*/
thread = thread_create(kmp, NULL, TASK, THREAD_FLAG_WIRED, "kmp", true);
if (thread != NULL) {
spinlock_lock(&thread->lock);
thread->cpu = &cpus[0];
spinlock_unlock(&thread->lock);
thread_ready(thread);
if ((t = thread_create(kmp, NULL, TASK, THREAD_FLAG_WIRED,
"kmp", true))) {
spinlock_lock(&t->lock);
t->cpu = &cpus[0];
spinlock_unlock(&t->lock);
thread_ready(t);
} else
panic("Unable to create kmp thread.");
thread_join(thread);
thread_detach(thread);
panic("thread_create/kmp\n");
thread_join(t);
thread_detach(t);
}
#endif /* CONFIG_SMP */
/*
* Now that all CPUs are up, we can report what we've found.
*/
cpu_list();
 
#ifdef CONFIG_SMP
if (config.cpu_count > 1) {
size_t i;
count_t i;
/*
* For each CPU, create its load balancing thread.
*/
for (i = 0; i < config.cpu_count; i++) {
thread = thread_create(kcpulb, NULL, TASK, THREAD_FLAG_WIRED, "kcpulb", true);
if (thread != NULL) {
spinlock_lock(&thread->lock);
thread->cpu = &cpus[i];
spinlock_unlock(&thread->lock);
thread_ready(thread);
 
if ((t = thread_create(kcpulb, NULL, TASK,
THREAD_FLAG_WIRED, "kcpulb", true))) {
spinlock_lock(&t->lock);
t->cpu = &cpus[i];
spinlock_unlock(&t->lock);
thread_ready(t);
} else
printf("Unable to create kcpulb thread for cpu" PRIs "\n", i);
panic("thread_create/kcpulb\n");
 
}
}
#endif /* CONFIG_SMP */
 
/*
* At this point SMP, if present, is configured.
*/
arch_post_smp_init();
#ifdef CONFIG_KCONSOLE
if (stdin) {
/*
* Create kernel console.
*/
thread = thread_create(kconsole_thread, NULL, TASK, 0, "kconsole", false);
if (thread != NULL)
thread_ready(thread);
else
printf("Unable to create kconsole thread\n");
}
#endif /* CONFIG_KCONSOLE */
interrupts_enable();
 
/*
* Create user tasks, load RAM disk images.
* Create kernel console.
*/
size_t i;
program_t programs[CONFIG_INIT_TASKS];
t = thread_create(kconsole, (void *) "kconsole", TASK, 0, "kconsole", false);
if (t)
thread_ready(t);
else
panic("thread_create/kconsole\n");
 
interrupts_enable();
 
count_t i;
for (i = 0; i < init.cnt; i++) {
/*
* Run user tasks, load RAM disk images.
*/
if (init.tasks[i].addr % FRAME_SIZE) {
printf("init[%" PRIs "].addr is not frame aligned\n", i);
printf("init[%d].addr is not frame aligned", i);
continue;
}
/*
* Construct task name from the 'init:' prefix and the
* name stored in the init structure (if any).
*/
char namebuf[TASK_NAME_BUFLEN];
char *name;
name = init.tasks[i].name;
if (name[0] == 0)
name = "<unknown>";
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);
 
int rc = program_create_from_image((void *) init.tasks[i].addr,
namebuf, &programs[i]);
task_t *utask = task_run_program((void *) init.tasks[i].addr,
"uspace");
if ((rc == 0) && (programs[i].task != NULL)) {
if (utask) {
/*
* Set capabilities to init userspace tasks.
*/
cap_set(programs[i].task, CAP_CAP | CAP_MEM_MANAGER |
cap_set(utask, CAP_CAP | CAP_MEM_MANAGER |
CAP_IO_MANAGER | CAP_PREEMPT_CONTROL | CAP_IRQ_REG);
if (!ipc_phone_0)
ipc_phone_0 = &programs[i].task->answerbox;
} else if (rc == 0) {
/* It was the program loader and was registered */
if (!ipc_phone_0)
ipc_phone_0 = &utask->answerbox;
} else {
/* RAM disk image */
int rd = init_rd((rd_header_t *) init.tasks[i].addr, init.tasks[i].size);
int rd = init_rd((rd_header *) init.tasks[i].addr,
init.tasks[i].size);
if (rd != RE_OK)
printf("Init binary %" PRIs " not used (error %d)\n", i, rd);
printf("Init binary %zd not used, error code %d.\n", i, rd);
}
}
/*
* Run user tasks.
*/
for (i = 0; i < init.cnt; i++) {
if (programs[i].task != NULL)
program_ready(&programs[i]);
}
#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]);
while (1) {
thread_sleep(1);
i++;
printf("kinit... ");
}
}
#endif /* CONFIG_KCONSOLE */
}
 
/** @}
/trunk/kernel/generic/src/main/shutdown.c
32,11 → 32,10
 
/**
* @file
* @brief Shutdown procedures.
* @brief Shutdown procedures.
*/
 
#include <arch.h>
#include <func.h>
#include <print.h>
 
void reboot(void)
48,7 → 47,6
#endif
arch_reboot();
halt();
}
 
/** @}