summaryrefslogtreecommitdiff
path: root/src/kernel/core/kmain.c
blob: 6c9700e93c43f701c03aae2c42b82b12c3f38645 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <types.h>
#include "multiboot.h"
#include "monitor.h"
#include "sys.h"
#include "test.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>

/*	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. */
void kmain(struct multiboot_info_t* mbd, int32_t magic) {
	monitor_clear();

	ASSERT(magic == MULTIBOOT_BOOTLOADER_MAGIC);

	size_t totalRam = 0;
	uint32_t i;

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

	monitor_write("Grapes 0.0.4 'Cat in my heart' starting up :\n");

	idt_init();

	totalRam = ((mbd->mem_upper + mbd->mem_lower) * 1024);
	paging_init(totalRam);
	gdt_init();
	paging_cleanup();

	kheap_init();
	timer_init(30);
	tasking_init();
	
	monitor_write("\nLoading modules :\n");
	for (i = 0; i < mbd->mods_count; i++) {
		monitor_write(" * ");
		monitor_write((char*)mods[i].string);
		if (elf_check((uint8_t*)mods[i].mod_start)) {
			monitor_write(" : Invalid ELF file\n");
		} else {
			struct process *pr = elf_exec((uint8_t*)mods[i].mod_start, PL_DRIVER);
			if (pr == 0) {
				monitor_write(" : Error loading\n");
			} else {
				monitor_write(" : OK pid:"); monitor_writeDec(pr->pid); monitor_write("\n");
			}
		}
	}

	test_run();

	monitor_write("Modules now RULE THE WORLD !\n");
	sti();
	tasking_switch();
	PANIC("Should never happen. Something probably went wrong with multitasking.");
}