matos/core/main.c
2018-11-14 18:03:11 +01:00

93 lines
2.4 KiB
C

#include "exception.h"
#include "gdt.h"
#include "idt.h"
#include "interrupt.h"
#include "io.h"
#include "irq.h"
#include "klibc.h"
#include "mem.h"
#include "multiboot.h"
#include "paging.h"
#include "pit.h"
#include "serial.h"
#include "stdarg.h"
#ifdef RUN_TEST
#include "test.h"
#endif
#include "types.h"
#include "vga.h"
#define CHECK_FLAG(flags, bit) ((flags) & (1 << (bit)))
void cpuid(int code, uint32_t *a, uint32_t *d)
{
asm volatile("cpuid" : "=a"(*a), "=d"(*d) : "0"(code) : "ebx", "ecx");
}
// Multiboot information available here :
// https://www.gnu.org/software/grub/manual/multiboot/multiboot.html#kernel_002ec
void kmain(unsigned long magic, unsigned long addr)
{
unsigned long upper_mem = 0;
VGASetup(BLACK, GREEN);
cursorEnable(14, 15);
printf("Setting up Interruptions\n");
gdtSetup();
idtSetup();
irqSetup();
pitSetup(100);
if (magic == MULTIBOOT_BOOTLOADER_MAGIC) { // Get loaded by Grub wuth mutliboot version 1
multiboot_info_t *mbi = (multiboot_info_t *)addr;
/* Are mem_* valid? */
if (CHECK_FLAG(mbi->flags, 0)) {
printf("mem_lower = %dKB mem_upper %dKB\n", mbi->mem_lower, mbi->mem_upper);
upper_mem = mbi->mem_upper;
}
/* Is boot_device valid? */
if (CHECK_FLAG(mbi->flags, 1)) {
printf("boot_device = %d\n", mbi->boot_device);
}
/* Is the command line passed? */
if (CHECK_FLAG(mbi->flags, 2)) {
printf("cmdline = %s\n", (char *)mbi->cmdline);
}
}
if (upper_mem == 0) {
printf("Cannot get upper phy mem bound. Using default value 32MB\n");
upper_mem = 32 * 1024;
}
printf("Setting up Pagination\n");
paddr_t lastUserByMem;
memSetup(upper_mem, &lastUserByMem);
#ifdef RUN_TEST
testPhymem();
#endif
pagingSetup(lastUserByMem);
printf("Setting up IRQ handlers\n");
irqSetRoutine(IRQ_KEYBOARD, keyboard_handler);
printf("Enabling HW interrupts\n");
exceptionSetRoutine(EXCEPTION_DOUBLE_FAULT, print_handler);
exceptionSetRoutine(EXCEPTION_PAGE_FAULT, pagefault_handler);
// Enabling the HW interrupts
asm volatile("sti\n");
printf("Setting up Serial link (115200)\n");
serialSetup(115200);
#ifdef RUN_TEST
run_test();
#endif
int count = 0;
while (1) {
printIntDetails(count++, GREEN, BLACK, 0, VGA_HEIGHT - 1);
}
printf("exiting\n");
}