matos/core/main.c
2018-08-09 22:19:34 +02:00

70 lines
2.0 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 "pit.h"
#include "stdarg.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;
initVGA(BLACK, GREEN);
printf("Setting up IDT\n");
gdtSetup();
idtSetup();
irqSetup();
initPit(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;
}
memInit(upper_mem);
printf("Setting up IRQ handlers\n");
irqSetRoutine(IRQ_KEYBOARD, keyboard_handler);
irqSetRoutine(IRQ_TIMER, timer_handler);
printf("Enabling HW interrupts\n");
exceptionSetRoutine(EXCEPTION_DOUBLE_FAULT, print_handler);
// Enabling the HW interrupts
asm volatile("sti\n");
int count = 0;
while (1) {
printIntDetails(count++, GREEN, BLACK, 0, VGA_HEIGHT - 1);
}
printf("exiting\n");
}