summaryrefslogblamecommitdiff
path: root/src/kernel/core/kmain.cpp
blob: e74ff33e1c1d5a3b462210e338849f10f631e0c0 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
                  

                   
                      
                
                       





                       
                       
                     

                            
 




                                                                            
                                                             

                                                    
                            
                   
 
                                                                 
                                                                           
                                                   
                                               


                                                     


                                                                                    
 
 
                                          
                   



                                                              
                              
 
                                  
                       
                       
                    























                                                                      
                                               

                                                       
                                                             
                                                                 
                        
                                                                                     
                                      
                                                                      
                                
                                                                                                                



                         
                                                                     
              
                   
                                                                                       
 
#include <types.h>
#include <config.h>

#include "multiboot.h"
#include "sys.h"
#include <dev/vgatxt.h>
#include <task/idt.h>
#include <task/timer.h>
#include <task/task.h>
#include <mem/gdt.h>
#include <mem/paging.h>
#include <mem/mem.h>
#include <linker/elf.h>
#include <vfs/node.h>
#include <ui/vt.h>
#include <dev/ps2keyboard.h>

/*	The kernel's main procedure. This function is called in loader_.asm.
	This function calls the initializer functions for all system parts.
	It then loads the modules the kernel was given by the bootloader. 
	This function never returns : once multitasking is started for good,
	the execution flow of this function is never returned to. */
extern "C" void kmain(multiboot_info_t* mbd, int32_t magic) {
	ASSERT(magic == MULTIBOOT_BOOTLOADER_MAGIC);

	size_t totalRam = 0;
	uint32_t i;

	mem_placementAddr = ((size_t)&end & 0xFFFFF000) + 0x1000;
	mbd->cmdline += K_HIGHHALF_ADDR; mbd->mods_addr += K_HIGHHALF_ADDR;
	module_t *mods = (module_t*)mbd->mods_addr;
	for (i = 0; i < mbd->mods_count; i++) {
		mods[i].mod_start += K_HIGHHALF_ADDR;
		mods[i].mod_end += K_HIGHHALF_ADDR;
		mods[i].string += K_HIGHHALF_ADDR;
		if (mods[i].mod_end > mem_placementAddr)
			mem_placementAddr = (mods[i].mod_end & 0xFFFFF000) + 0x1000;
	}


	// Init memory managment functions
	idt_init();
	totalRam = ((mbd->mem_upper + mbd->mem_lower) * 1024);
	paging_init(totalRam);
	gdt_init();
	paging_cleanup();
	_no_more_ksbrk = true;

	// Init higher level stuff
	timer_init(30);
	tasking_init();
	vfs_setup();

	// Init display devices
	text_display = new vgatxt(dot_dev);
	dot_dev->add_child("vgatxt", text_display);
	ke_vt = new vt(dot_dev, 80, 25);
	dot_ui->add_child("klog", ke_vt);
	ke_vt->outputTo(text_display);

	// Say hello
	ke_vt->fgcolor = TC_LIGHTGRAY;
	ke_vt->writeStr("Hello. This is ");
	ke_vt->fgcolor = TC_WHITE; ke_vt->writeStr(K_OS_NAME);
	ke_vt->fgcolor = TC_LIGHTGRAY; ke_vt->writeStr(" version ");
	ke_vt->fgcolor = TC_WHITE; ke_vt->writeStr(K_OS_VER);
	ke_vt->fgcolor = TC_LIGHTGRAY; ke_vt->writeStr(" codename '");
	ke_vt->fgcolor = TC_WHITE; ke_vt->writeStr(K_OS_CODENAME);
	ke_vt->fgcolor = TC_LIGHTGRAY; ke_vt->writeStr("'. Enjoy!\n");

	// Init devices
	kbd = new ps2kbd(dot_dev);
	dot_dev->add_child("ps2kbd", kbd);

	// Load modules
	ke_vt->writeStr("Loading modules :\n");
	for (i = 0; i < mbd->mods_count; i++) {
		ke_vt->writeStr(" * ");
		ke_vt->writeStr((char*)mods[i].string);
		if (elf_check((uint8_t*)mods[i].mod_start)) {
			ke_vt->writeStr(" : Invalid ELF file\n");
		} else {
			process *pr = elf_exec((uint8_t*)mods[i].mod_start, PL_USER);
			if (pr == 0) {
				ke_vt->writeStr(" : Error loading\n");
			} else {
				ke_vt->writeStr(" : OK, pid="); ke_vt->writeDec(pr->pid); ke_vt->writeStr("\n");
			}
		}
	}

	ke_vt->writeStr("Giving control to userland processes.\n\n");
	sti();
	schedule();
	PANIC("Should never happen. Something probably went wrong with multitasking.");
}