2018-07-20 15:41:58 +02:00
|
|
|
#include "exception.h"
|
|
|
|
#include "gdt.h"
|
|
|
|
#include "idt.h"
|
|
|
|
#include "interrupt.h"
|
|
|
|
#include "io.h"
|
|
|
|
#include "irq.h"
|
2018-08-09 22:19:34 +02:00
|
|
|
#include "klibc.h"
|
2018-08-06 18:41:45 +02:00
|
|
|
#include "mem.h"
|
2018-08-05 15:09:32 +02:00
|
|
|
#include "multiboot.h"
|
2018-07-20 15:41:58 +02:00
|
|
|
#include "pit.h"
|
2018-08-06 21:00:58 +02:00
|
|
|
#include "stdarg.h"
|
2018-07-20 15:41:58 +02:00
|
|
|
#include "vga.h"
|
|
|
|
|
2018-08-09 22:19:34 +02:00
|
|
|
#define CHECK_FLAG(flags, bit) ((flags) & (1 << (bit)))
|
2018-07-20 15:41:58 +02:00
|
|
|
void cpuid(int code, uint32_t *a, uint32_t *d)
|
|
|
|
{
|
|
|
|
asm volatile("cpuid" : "=a"(*a), "=d"(*d) : "0"(code) : "ebx", "ecx");
|
|
|
|
}
|
|
|
|
|
2018-08-09 22:19:34 +02:00
|
|
|
// Multiboot information available here :
|
|
|
|
// https://www.gnu.org/software/grub/manual/multiboot/multiboot.html#kernel_002ec
|
2018-08-05 15:09:32 +02:00
|
|
|
void kmain(unsigned long magic, unsigned long addr)
|
2018-07-20 15:41:58 +02:00
|
|
|
{
|
2018-08-06 18:41:45 +02:00
|
|
|
unsigned long upper_mem = 0;
|
2018-07-20 15:41:58 +02:00
|
|
|
initVGA(BLACK, GREEN);
|
2018-08-06 22:28:45 +02:00
|
|
|
printf("Setting up IDT\n");
|
2018-07-20 15:41:58 +02:00
|
|
|
gdtSetup();
|
|
|
|
idtSetup();
|
|
|
|
irqSetup();
|
|
|
|
initPit(100);
|
|
|
|
|
2018-08-05 15:09:32 +02:00
|
|
|
if (magic == MULTIBOOT_BOOTLOADER_MAGIC) { // Get loaded by Grub wuth mutliboot version 1
|
|
|
|
multiboot_info_t *mbi = (multiboot_info_t *)addr;
|
|
|
|
/* Are mem_* valid? */
|
2018-08-09 22:19:34 +02:00
|
|
|
if (CHECK_FLAG(mbi->flags, 0)) {
|
2018-08-06 22:28:45 +02:00
|
|
|
printf("mem_lower = %dKB mem_upper %dKB\n", mbi->mem_lower, mbi->mem_upper);
|
2018-08-06 18:41:45 +02:00
|
|
|
upper_mem = mbi->mem_upper;
|
2018-08-05 15:09:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Is boot_device valid? */
|
2018-08-09 22:19:34 +02:00
|
|
|
if (CHECK_FLAG(mbi->flags, 1)) {
|
2018-08-06 22:28:45 +02:00
|
|
|
printf("boot_device = %d\n", mbi->boot_device);
|
2018-08-05 15:09:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Is the command line passed? */
|
2018-08-09 22:19:34 +02:00
|
|
|
if (CHECK_FLAG(mbi->flags, 2)) {
|
2018-08-06 22:28:45 +02:00
|
|
|
printf("cmdline = %s\n", (char *)mbi->cmdline);
|
2018-08-05 15:09:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-09 22:19:34 +02:00
|
|
|
if (upper_mem == 0) {
|
2018-08-06 22:28:45 +02:00
|
|
|
printf("Cannot get upper phy mem bound. Using default value 32MB\n");
|
2018-08-06 18:41:45 +02:00
|
|
|
upper_mem = 32 * 1024;
|
|
|
|
}
|
|
|
|
memInit(upper_mem);
|
|
|
|
|
2018-08-06 22:28:45 +02:00
|
|
|
printf("Setting up IRQ handlers\n");
|
2018-07-20 15:41:58 +02:00
|
|
|
irqSetRoutine(IRQ_KEYBOARD, keyboard_handler);
|
|
|
|
irqSetRoutine(IRQ_TIMER, timer_handler);
|
2018-08-06 22:28:45 +02:00
|
|
|
printf("Enabling HW interrupts\n");
|
2018-07-20 15:41:58 +02:00
|
|
|
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);
|
|
|
|
}
|
2018-08-06 22:28:45 +02:00
|
|
|
printf("exiting\n");
|
2018-07-20 15:41:58 +02:00
|
|
|
}
|