Files
haiku-beta6/src/system/kernel/main.c
T

269 lines
6.9 KiB
C
Raw Normal View History

2002-07-09 12:24:59 +00:00
/*
* Copyright 2002-2006, Axel Dörfler, [email protected]. All rights reserved.
* Distributed under the terms of the MIT License.
*
* Copyright 2001-2002, Travis Geiselbrecht. All rights reserved.
* Distributed under the terms of the NewOS License.
*/
2004-10-19 23:21:07 +00:00
/* This is main - initializes processors and starts init */
#include <OS.h>
#include <arch/platform.h>
2005-10-25 16:59:12 +00:00
#include <boot_item.h>
2002-07-09 12:24:59 +00:00
#include <cbuf.h>
#include <cpu.h>
2005-10-25 16:59:12 +00:00
#include <debug.h>
#include <elf.h>
2002-07-09 12:24:59 +00:00
#include <int.h>
#include <kdevice_manager.h>
2005-10-25 16:59:12 +00:00
#include <kdriver_settings.h>
#include <kernel_daemon.h>
2005-10-25 16:59:12 +00:00
#include <kmodule.h>
#include <kscheduler.h>
#include <ksyscalls.h>
#include <messaging.h>
2005-10-25 16:59:12 +00:00
#include <port.h>
#include <real_time_clock.h>
#include <sem.h>
#include <smp.h>
#include <system_info.h>
#include <team.h>
#include <timer.h>
2005-03-25 18:29:12 +00:00
#include <user_debugger.h>
2005-10-25 16:59:12 +00:00
#include <vfs.h>
#include <vm.h>
#include <boot/kernel_args.h>
2002-07-09 12:24:59 +00:00
#include <string.h>
//#define TRACE_BOOT
#ifdef TRACE_BOOT
# define TRACE(x...) dprintf("INIT : " x)
#else
# define TRACE(x...) ;
#endif
bool kernel_startup;
2005-10-25 16:59:12 +00:00
static kernel_args sKernelArgs;
2002-07-09 12:24:59 +00:00
static int32 main2(void *);
2005-10-25 16:59:12 +00:00
int _start(kernel_args *bootKernelArgs, int cpu); /* keep compiler happy */
int
2005-10-25 16:59:12 +00:00
_start(kernel_args *bootKernelArgs, int currentCPU)
2002-07-09 12:24:59 +00:00
{
kernel_startup = true;
2005-10-25 16:59:12 +00:00
if (bootKernelArgs->kernel_args_size != sizeof(kernel_args)
|| bootKernelArgs->version != CURRENT_KERNEL_ARGS_VERSION) {
// This is something we cannot handle right now - release kernels
// should always be able to handle the kernel_args of earlier
// released kernels.
debug_early_boot_message("Version mismatch between boot loader and kernel!\n");
return -1;
}
2005-10-25 16:59:12 +00:00
memcpy(&sKernelArgs, bootKernelArgs, sizeof(kernel_args));
// the passed in kernel args are in a non-allocated range of memory
2002-07-09 12:24:59 +00:00
2005-10-25 16:59:12 +00:00
smp_set_num_cpus(sKernelArgs.num_cpus);
2002-07-09 12:24:59 +00:00
// do any pre-booting cpu config
cpu_preboot_init_percpu(&sKernelArgs, currentCPU);
thread_preboot_init_percpu(&sKernelArgs, currentCPU);
2002-07-09 12:24:59 +00:00
// if we're not a boot cpu, spin here until someone wakes us up
2005-10-25 16:59:12 +00:00
if (smp_trap_non_boot_cpus(currentCPU)) {
thread_id thread;
2002-07-09 12:24:59 +00:00
2005-12-30 21:20:07 +00:00
// init platform
arch_platform_init(&sKernelArgs);
2005-12-30 21:20:07 +00:00
2002-07-09 12:24:59 +00:00
// setup debug output
2005-10-25 16:59:12 +00:00
debug_init(&sKernelArgs);
set_dprintf_enabled(true);
2002-07-09 12:24:59 +00:00
dprintf("Welcome to kernel debugger output!\n");
2005-10-25 16:59:12 +00:00
// we're the boot processor, so wait for all of the APs to enter the kernel
smp_wait_for_non_boot_cpus();
2002-07-09 12:24:59 +00:00
// init modules
TRACE("init CPU\n");
2005-10-25 16:59:12 +00:00
cpu_init(&sKernelArgs);
2007-02-05 01:46:28 +00:00
cpu_init_percpu(&sKernelArgs, currentCPU);
TRACE("init interrupts\n");
2005-10-25 16:59:12 +00:00
int_init(&sKernelArgs);
2002-07-09 12:24:59 +00:00
TRACE("init VM\n");
2005-10-25 16:59:12 +00:00
vm_init(&sKernelArgs);
// Before vm_init_post_sem() is called, we have to make sure that
// the boot loader allocated region is not used anymore
2002-07-09 12:24:59 +00:00
// now we can use the heap and create areas
arch_platform_init_post_vm(&sKernelArgs);
TRACE("init driver_settings\n");
boot_item_init();
2005-10-25 16:59:12 +00:00
driver_settings_init(&sKernelArgs);
debug_init_post_vm(&sKernelArgs);
int_init_post_vm(&sKernelArgs);
cpu_init_post_vm(&sKernelArgs);
TRACE("init system info\n");
2005-10-25 16:59:12 +00:00
system_info_init(&sKernelArgs);
2002-07-09 12:24:59 +00:00
TRACE("init SMP\n");
2005-10-25 16:59:12 +00:00
smp_init(&sKernelArgs);
TRACE("init timer\n");
2005-10-25 16:59:12 +00:00
timer_init(&sKernelArgs);
TRACE("init real time clock\n");
2005-10-25 16:59:12 +00:00
rtc_init(&sKernelArgs);
2002-07-09 12:24:59 +00:00
TRACE("init semaphores\n");
2005-10-25 16:59:12 +00:00
sem_init(&sKernelArgs);
2002-07-09 12:24:59 +00:00
// now we can create and use semaphores
TRACE("init VM semaphores\n");
2005-10-25 16:59:12 +00:00
vm_init_post_sem(&sKernelArgs);
TRACE("init driver_settings\n");
2005-10-25 16:59:12 +00:00
driver_settings_init_post_sem(&sKernelArgs);
TRACE("init generic syscall\n");
generic_syscall_init();
TRACE("init cbuf\n");
2002-07-09 12:24:59 +00:00
cbuf_init();
TRACE("init teams\n");
2005-10-25 16:59:12 +00:00
team_init(&sKernelArgs);
TRACE("init threads\n");
2005-10-25 16:59:12 +00:00
thread_init(&sKernelArgs);
TRACE("init ports\n");
2005-10-25 16:59:12 +00:00
port_init(&sKernelArgs);
TRACE("init kernel daemons\n");
kernel_daemon_init();
arch_platform_init_post_thread(&sKernelArgs);
2002-07-09 12:24:59 +00:00
TRACE("init VM threads\n");
2005-10-25 16:59:12 +00:00
vm_init_post_thread(&sKernelArgs);
TRACE("init ELF loader\n");
2005-10-25 16:59:12 +00:00
elf_init(&sKernelArgs);
TRACE("init scheduler\n");
2005-10-25 16:59:12 +00:00
scheduler_init();
TRACE("init VFS\n");
vfs_init(&sKernelArgs);
2002-07-09 12:24:59 +00:00
TRACE("enable interrupts, exit kernel startup\n");
kernel_startup = false;
2002-07-09 12:24:59 +00:00
TRACE("waking up AP cpus\n");
smp_wake_up_non_boot_cpus();
2005-10-25 16:59:12 +00:00
enable_interrupts();
scheduler_start();
// start a thread to finish initializing the rest of the system
TRACE("starting main2 thread\n");
thread = spawn_kernel_thread(&main2, "main2", B_NORMAL_PRIORITY, NULL);
resume_thread(thread);
2005-10-25 16:59:12 +00:00
} else {
// this is run for each non boot processor after they've been set loose
2007-02-05 01:46:28 +00:00
cpu_init_percpu(&sKernelArgs, currentCPU);
2005-10-25 16:59:12 +00:00
smp_per_cpu_init(&sKernelArgs, currentCPU);
// welcome to the machine
2005-10-25 16:59:12 +00:00
enable_interrupts();
scheduler_start();
2005-10-25 16:59:12 +00:00
}
TRACE("main: done... begin idle loop on cpu %d\n", currentCPU);
for (;;)
arch_cpu_idle();
2002-07-09 12:24:59 +00:00
return 0;
}
static int32
main2(void *unused)
2002-07-09 12:24:59 +00:00
{
(void)(unused);
TRACE("start of main2: initializing devices\n");
2002-07-09 12:24:59 +00:00
TRACE("Init modules\n");
2005-10-25 16:59:12 +00:00
module_init(&sKernelArgs);
// ToDo: the preloaded image debug data is placed in the kernel args, and
// thus, if they are enabled, the kernel args shouldn't be freed, so
// that we don't have to copy them.
// What is yet missing is a mechanism that controls this (via driver settings).
if (0) {
2004-10-23 13:53:13 +00:00
// module_init() is supposed to be the last user of the kernel args
// Note: don't confuse the kernel_args structure (which is never freed)
// with the kernel args ranges it contains (and which are freed here).
2005-10-25 16:59:12 +00:00
vm_free_kernel_args(&sKernelArgs);
}
2004-10-23 13:53:13 +00:00
2005-03-25 18:29:12 +00:00
// init userland debugging
TRACE("Init Userland debugging\n");
2005-03-25 18:29:12 +00:00
init_user_debug();
// init the messaging service
TRACE("Init Messaging Service\n");
init_messaging_service();
2002-07-13 00:55:02 +00:00
/* bootstrap all the filesystems */
TRACE("Bootstrap file systems\n");
vfs_bootstrap_file_systems();
2002-07-09 12:24:59 +00:00
TRACE("Init Device Manager\n");
2005-10-25 16:59:12 +00:00
device_manager_init(&sKernelArgs);
// ToDo: device manager starts here, bus_init()/dev_init() won't be necessary anymore,
// but instead, the hardware and drivers are rescanned then.
int_init_post_device_manager(&sKernelArgs);
TRACE("Mount boot file system\n");
2005-10-25 16:59:12 +00:00
vfs_mount_boot_file_system(&sKernelArgs);
2002-07-09 12:24:59 +00:00
// CPU specific modules may now be available
cpu_init_post_modules(&sKernelArgs);
vm_init_post_modules(&sKernelArgs);
debug_init_post_modules(&sKernelArgs);
device_manager_init_post_modules(&sKernelArgs);
2002-07-09 12:24:59 +00:00
// start the init process
{
const char *shellArgs[] = {"/bin/sh", "/boot/beos/system/boot/Bootscript", NULL};
const char *initArgs[] = {"/bin/init", NULL};
const char **args;
int32 argc;
thread_id thread;
struct stat st;
if (stat(shellArgs[1], &st) == 0) {
// start Bootscript
args = shellArgs;
argc = 2;
} else {
// ToDo: this is only necessary as long as we have the bootdir mechanism
// start init
args = initArgs;
argc = 1;
}
thread = load_image(argc, args, NULL);
if (thread >= B_OK) {
resume_thread(thread);
TRACE("Bootscript started\n");
} else
dprintf("error starting \"%s\" error = %ld \n", args[0], thread);
2002-07-09 12:24:59 +00:00
}
return 0;
}