toaruos/kernel/main.c

180 lines
6.0 KiB
C
Raw Normal View History

2011-12-10 17:45:04 -06:00
/* vim: tabstop=4 shiftwidth=4 noexpandtab
2012-03-10 15:03:59 -06:00
*
* The ToAruOS kernel is released under the terms of the
* University of Illinois / NCSA License.
2011-11-29 13:06:17 -06:00
*
2014-04-11 09:41:46 -07:00
* Copyright (c) 2011-2014 Kevin Lange. All rights reserved.
2011-01-28 16:45:27 -06:00
*
2011-10-12 23:24:19 -05:00
* Dedicated to the memory of
* Dennis Ritchie
* 1941-2011
*
2011-01-28 16:45:27 -06:00
* Developed by: ToAruOS Kernel Development Team
* http://github.com/klange/osdev
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal with the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimers.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimers in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the names of the ToAruOS Kernel Development Team, Kevin Lange,
* nor the names of its contributors may be used to endorse
* or promote products derived from this Software without specific prior
* written permission.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* WITH THE SOFTWARE.
*/
2011-01-15 20:01:19 -05:00
#include <system.h>
2011-02-19 19:27:46 -06:00
#include <boot.h>
#include <ext2.h>
#include <fs.h>
2011-12-14 17:47:30 -06:00
#include <logging.h>
#include <process.h>
2012-02-05 14:16:59 -06:00
#include <shm.h>
#include <args.h>
#include <module.h>
2011-01-15 20:01:19 -05:00
2014-03-09 23:31:13 -07:00
uintptr_t initial_esp = 0;
2014-03-18 21:37:12 -07:00
fs_node_t * ramdisk_mount(uintptr_t, size_t);
/*
2013-12-15 21:33:46 -08:00
* multiboot i386 (pc) kernel entry point
*/
2014-02-26 21:10:56 -08:00
int kmain(struct multiboot *mboot, uint32_t mboot_mag, uintptr_t esp) {
initial_esp = esp;
2013-03-18 00:52:12 -07:00
extern char * cmdline;
uint32_t mboot_mods_count = 0;
mboot_mod_t * mboot_mods = NULL;
2011-02-07 23:51:11 -06:00
if (mboot_mag == MULTIBOOT_EAX_MAGIC) {
2012-01-29 12:46:34 -06:00
/* Multiboot (GRUB, native QEMU, PXE) */
2012-12-07 18:33:07 -08:00
debug_print(NOTICE, "Relocating Multiboot structures...");
2011-04-06 17:06:07 -05:00
mboot_ptr = mboot;
char cmdline_[1024];
if (mboot_ptr->flags & (1 << 3)) {
debug_print(NOTICE, "There %s %d module%s starting at 0x%x.", mboot_ptr->mods_count == 1 ? "is" : "are", mboot_ptr->mods_count, mboot_ptr->mods_count == 1 ? "" : "s", mboot_ptr->mods_addr);
debug_print(NOTICE, "Current kernel heap start point would be 0x%x.", &end);
if (mboot_ptr->mods_count > 0) {
uintptr_t last_mod = (uintptr_t)&end;
uint32_t i;
mboot_mods = (mboot_mod_t *)mboot_ptr->mods_addr;
mboot_mods_count = mboot_ptr->mods_count;
for (i = 0; i < mboot_ptr->mods_count; ++i ) {
mboot_mod_t * mod = &mboot_mods[i];
uint32_t module_start = mod->mod_start;
uint32_t module_end = mod->mod_end;
if ((uintptr_t)mod + sizeof(mboot_mod_t) > last_mod) {
/* Just in case some silly person put this *behind* the modules... */
last_mod = (uintptr_t)mod + sizeof(mboot_mod_t);
}
debug_print(NOTICE, "Module %d is at 0x%x:0x%x", i, module_start, module_end);
if (last_mod < module_end) {
last_mod = module_end;
}
}
2014-04-05 15:23:17 -07:00
debug_print(NOTICE, "Moving kernel heap start to 0x%x", last_mod);
kmalloc_startat(last_mod);
}
}
size_t len = strlen((char *)mboot_ptr->cmdline);
memmove(cmdline_, (char *)mboot_ptr->cmdline, len + 1);
/* Relocate the command line */
cmdline = (char *)kmalloc(len + 1);
memcpy(cmdline, cmdline_, len + 1);
2011-02-07 23:51:11 -06:00
}
2011-01-28 16:45:27 -06:00
/* Initialize core modules */
2013-12-15 21:33:46 -08:00
gdt_install(); /* Global descriptor table */
idt_install(); /* IDT */
isrs_install(); /* Interrupt service requests */
irq_install(); /* Hardware interrupt requests */
/* Memory management */
2013-12-15 21:33:46 -08:00
paging_install(mboot_ptr->mem_upper + mboot_ptr->mem_lower);
heap_install(); /* Kernel heap */
if (cmdline) {
args_parse(cmdline);
}
2013-03-15 00:20:55 -07:00
vfs_install();
2011-01-28 16:45:27 -06:00
/* Hardware drivers */
2013-12-15 21:33:46 -08:00
timer_install(); /* PIC driver */
tasking_install(); /* Multi-tasking */
fpu_install(); /* FPU/SSE magic */
syscalls_install(); /* Install the system calls */
shm_install(); /* Install shared memory */
modules_install(); /* Modules! */
2014-03-14 23:47:07 -07:00
/* Load modules from bootloader */
debug_print(NOTICE, "%d modules to load", mboot_mods_count);
for (unsigned int i = 0; i < mboot_ptr->mods_count; ++i ) {
mboot_mod_t * mod = &mboot_mods[i];
uint32_t module_start = mod->mod_start;
uint32_t module_end = mod->mod_end;
size_t module_size = module_end - module_end;
2014-03-18 21:37:12 -07:00
if (!module_quickcheck((void *)module_start)) {
debug_print(NOTICE, "Loading ramdisk: 0x%x:0x%x", module_start, module_end);
ramdisk_mount(module_start, module_end-module_start);
} else {
debug_print(NOTICE, "Loading a module: 0x%x:0x%x", module_start, module_end);
module_data_t * mod_info = (module_data_t *)module_load_direct((void *)(module_start), module_size);
2014-04-10 23:08:07 -07:00
if (mod_info) {
debug_print(NOTICE, "Loaded: %s", mod_info->mod_info->name);
}
2014-03-18 21:37:12 -07:00
}
}
2014-04-10 23:08:07 -07:00
/* Map /dev to a device mapper */
map_vfs_directory("/dev");
2014-03-19 18:56:07 -07:00
if (args_present("start")) {
char * c = args_value("start");
if (!c) {
debug_print(WARNING, "Expected an argument to kernel option `start`. Ignoring.");
} else {
debug_print(NOTICE, "Got start argument: %s", c);
boot_arg = strdup(c);
}
}
2012-01-30 12:10:53 -06:00
/* Prepare to run /bin/init */
char * argv[] = {
2012-01-30 12:10:53 -06:00
"/bin/init",
boot_arg,
NULL
};
int argc = 0;
while (argv[argc]) {
argc++;
}
2012-01-30 12:10:53 -06:00
system(argv[0], argc, argv); /* Run init */
2011-04-08 17:53:52 -05:00
2011-01-15 20:01:19 -05:00
return 0;
}
2011-12-10 17:45:04 -06:00