Initial commit
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
bits 32
|
||||
|
||||
section .text
|
||||
align 4
|
||||
dd 0x1BADB002
|
||||
dd 0x00
|
||||
dd - (0x1BADB002+0x00)
|
||||
|
||||
global start
|
||||
extern kmain ; this function is gonna be located in our c code(kernel.c)
|
||||
|
||||
start:
|
||||
cli ;clears the interrupts
|
||||
call kmain ;send processor to continue execution from the kamin funtion in c code
|
||||
|
||||
|
||||
;;;;;;;;;;;DIVISION BY ZERO TEST
|
||||
; mov ax, 10
|
||||
; mov bx, 0
|
||||
; div bx
|
||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||
|
||||
hlt ; halt the cpu(pause it from executing from this address
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
#include "../include/idt.h"
|
||||
#include <stdbool.h>
|
||||
//IDT_MAX_DESCRIPTORS = 256
|
||||
static bool vectors[sizeof(idt)];
|
||||
|
||||
extern void* isr_stub_table[];
|
||||
|
||||
#define KERNEL_CS 0x08
|
||||
|
||||
|
||||
/*void idt_init() {
|
||||
idtr.base = (uintptr_t)&idt[0];
|
||||
idtr.limit = (uint16_t)sizeof(idt_entry_t) * sizeof(idt) - 1;
|
||||
|
||||
for (uint8_t vector = 0; vector < 32; vector++) {
|
||||
idt_set_descriptor(vector, isr_stub_table[vector], 0x8E);
|
||||
vectors[vector] = true;
|
||||
}
|
||||
|
||||
__asm__ volatile ("lidt %0" : : "m"(idtr)); // load the new IDT
|
||||
__asm__ volatile ("sti"); // set the interrupt flag
|
||||
}*/
|
||||
|
||||
void idt_set() {
|
||||
idtr.base = (uint32) &idt;
|
||||
idtr.limit = sizeof(idt) * sizeof(idt_entry_t) - 1;
|
||||
__asm__ __volatile__("lidtl (%0)" : : "r" (&idtr));
|
||||
}
|
||||
|
||||
|
||||
//void idt_set_descriptor(uint8_t vector, void* isr, uint8_t flags)
|
||||
void idt_set_descriptor(uint8_t vector, uint32 isr, uint8_t flags) {
|
||||
idt_entry_t* descriptor = &idt[vector];
|
||||
|
||||
descriptor->isr_low = isr & 0xFFFF;
|
||||
descriptor->kernel_cs = 0x08; // this value can be whatever offset your kernel code selector is in your GDT
|
||||
descriptor->attributes = flags;
|
||||
descriptor->isr_high = high_16(isr);//(uint32_t)isr >> 16;
|
||||
descriptor->reserved = 0;
|
||||
}
|
||||
@@ -0,0 +1,241 @@
|
||||
#include "../include/idt.h"
|
||||
#include "../include/isr.h"
|
||||
#include "../include/types.h"
|
||||
#include "../include/screen.h"
|
||||
|
||||
void isr_install() {
|
||||
idt_set_descriptor(0, (uint32)isr0, 0x8E);
|
||||
idt_set_descriptor(1, (uint32)isr1, 0x8E);
|
||||
idt_set_descriptor(2, (uint32)isr2, 0x8E);
|
||||
idt_set_descriptor(3, (uint32)isr3, 0x8E);
|
||||
idt_set_descriptor(4, (uint32)isr4, 0x8E);
|
||||
idt_set_descriptor(5, (uint32)isr5, 0x8E);
|
||||
idt_set_descriptor(6, (uint32)isr6, 0x8E);
|
||||
idt_set_descriptor(7, (uint32)isr7, 0x8E);
|
||||
idt_set_descriptor(8, (uint32)isr8, 0x8E);
|
||||
idt_set_descriptor(9, (uint32)isr9, 0x8E);
|
||||
idt_set_descriptor(10, (uint32)isr10, 0x8E);
|
||||
idt_set_descriptor(11, (uint32)isr11, 0x8E);
|
||||
idt_set_descriptor(12, (uint32)isr12, 0x8E);
|
||||
idt_set_descriptor(13, (uint32)isr13, 0x8E);
|
||||
idt_set_descriptor(14, (uint32)isr14, 0x8E);
|
||||
idt_set_descriptor(15, (uint32)isr15, 0x8E);
|
||||
idt_set_descriptor(16, (uint32)isr16, 0x8E);
|
||||
idt_set_descriptor(17, (uint32)isr17, 0x8E);
|
||||
idt_set_descriptor(18, (uint32)isr18, 0x8E);
|
||||
idt_set_descriptor(19, (uint32)isr19, 0x8E);
|
||||
idt_set_descriptor(20, (uint32)isr20, 0x8E);
|
||||
idt_set_descriptor(21, (uint32)isr21, 0x8E);
|
||||
idt_set_descriptor(22, (uint32)isr22, 0x8E);
|
||||
idt_set_descriptor(23, (uint32)isr23, 0x8E);
|
||||
idt_set_descriptor(24, (uint32)isr24, 0x8E);
|
||||
idt_set_descriptor(25, (uint32)isr25, 0x8E);
|
||||
idt_set_descriptor(26, (uint32)isr26, 0x8E);
|
||||
idt_set_descriptor(27, (uint32)isr27, 0x8E);
|
||||
idt_set_descriptor(28, (uint32)isr28, 0x8E);
|
||||
idt_set_descriptor(29, (uint32)isr29, 0x8E);
|
||||
idt_set_descriptor(30, (uint32)isr30, 0x8E);
|
||||
idt_set_descriptor(31, (uint32)isr31, 0x8E);
|
||||
|
||||
idt_set(); // Load with ASM
|
||||
terminal_writestring("isr_install done");
|
||||
}
|
||||
|
||||
void isr0()
|
||||
{
|
||||
terminal_writestring(exception_messages[0]);
|
||||
asm("hlt");
|
||||
}
|
||||
void isr1()
|
||||
{
|
||||
terminal_writestring(exception_messages[1]);
|
||||
asm("hlt");
|
||||
}
|
||||
void isr2()
|
||||
{
|
||||
terminal_writestring(exception_messages[2]);
|
||||
asm("hlt");
|
||||
}
|
||||
void isr3()
|
||||
{
|
||||
terminal_writestring(exception_messages[3]);
|
||||
asm("hlt");
|
||||
}
|
||||
void isr4()
|
||||
{
|
||||
terminal_writestring(exception_messages[4]);
|
||||
asm("hlt");
|
||||
}
|
||||
void isr5()
|
||||
{
|
||||
terminal_writestring(exception_messages[5]);
|
||||
asm("hlt");
|
||||
}
|
||||
void isr6()
|
||||
{
|
||||
terminal_writestring(exception_messages[6]);
|
||||
asm("hlt");
|
||||
}
|
||||
void isr7()
|
||||
{
|
||||
terminal_writestring(exception_messages[7]);
|
||||
asm("hlt");
|
||||
}
|
||||
void isr8()
|
||||
{
|
||||
terminal_writestring(exception_messages[8]);
|
||||
asm("hlt");
|
||||
}
|
||||
void isr9()
|
||||
{
|
||||
terminal_writestring(exception_messages[9]);
|
||||
asm("hlt");
|
||||
}
|
||||
void isr10()
|
||||
{
|
||||
terminal_writestring(exception_messages[10]);
|
||||
asm("hlt");
|
||||
}
|
||||
void isr11()
|
||||
{
|
||||
terminal_writestring(exception_messages[11]);
|
||||
asm("hlt");
|
||||
}
|
||||
void isr12()
|
||||
{
|
||||
terminal_writestring(exception_messages[12]);
|
||||
asm("hlt");
|
||||
}
|
||||
void isr13()
|
||||
{
|
||||
terminal_writestring(exception_messages[13]);
|
||||
asm("hlt");
|
||||
}
|
||||
void isr14()
|
||||
{
|
||||
terminal_writestring(exception_messages[14]);
|
||||
asm("hlt");
|
||||
}
|
||||
void isr15()
|
||||
{
|
||||
terminal_writestring(exception_messages[15]);
|
||||
asm("hlt");
|
||||
}
|
||||
void isr16()
|
||||
{
|
||||
terminal_writestring(exception_messages[16]);
|
||||
asm("hlt");
|
||||
}
|
||||
void isr17()
|
||||
{
|
||||
terminal_writestring(exception_messages[17]);
|
||||
asm("hlt");
|
||||
}
|
||||
void isr18()
|
||||
{
|
||||
terminal_writestring(exception_messages[18]);
|
||||
asm("hlt");
|
||||
}
|
||||
void isr19()
|
||||
{
|
||||
terminal_writestring(exception_messages[19]);
|
||||
asm("hlt");
|
||||
}
|
||||
void isr20()
|
||||
{
|
||||
terminal_writestring(exception_messages[20]);
|
||||
asm("hlt");
|
||||
}
|
||||
void isr21()
|
||||
{
|
||||
terminal_writestring(exception_messages[21]);
|
||||
asm("hlt");
|
||||
}
|
||||
void isr22()
|
||||
{
|
||||
terminal_writestring(exception_messages[22]);
|
||||
asm("hlt");
|
||||
}
|
||||
void isr23()
|
||||
{
|
||||
terminal_writestring(exception_messages[23]);
|
||||
asm("hlt");
|
||||
}
|
||||
void isr24()
|
||||
{
|
||||
terminal_writestring(exception_messages[24]);
|
||||
asm("hlt");
|
||||
}
|
||||
void isr25()
|
||||
{
|
||||
terminal_writestring(exception_messages[25]);
|
||||
asm("hlt");
|
||||
}
|
||||
void isr26()
|
||||
{
|
||||
terminal_writestring(exception_messages[26]);
|
||||
asm("hlt");
|
||||
}
|
||||
void isr27()
|
||||
{
|
||||
terminal_writestring(exception_messages[27]);
|
||||
asm("hlt");
|
||||
}
|
||||
void isr28()
|
||||
{
|
||||
terminal_writestring(exception_messages[28]);
|
||||
asm("hlt");
|
||||
}
|
||||
void isr29()
|
||||
{
|
||||
terminal_writestring(exception_messages[29]);
|
||||
asm("hlt");
|
||||
}
|
||||
void isr30()
|
||||
{
|
||||
terminal_writestring(exception_messages[30]);
|
||||
asm("hlt");
|
||||
}
|
||||
void isr31()
|
||||
{
|
||||
terminal_writestring(exception_messages[31]);
|
||||
asm("hlt");
|
||||
}
|
||||
|
||||
static string exception_messages[] = {
|
||||
"Division By Zero",
|
||||
"Debug",
|
||||
"Non Maskable Interrupt",
|
||||
"Breakpoint",
|
||||
"Into Detected Overflow",
|
||||
"Out of Bounds",
|
||||
"Invalid Opcode",
|
||||
"No Coprocessor",
|
||||
|
||||
"Double Fault",
|
||||
"Coprocessor Segment Overrun",
|
||||
"Bad TSS",
|
||||
"Segment Not Present",
|
||||
"Stack Fault",
|
||||
"General Protection Fault",
|
||||
"Page Fault",
|
||||
"Unknown Interrupt",
|
||||
|
||||
"Coprocessor Fault",
|
||||
"Alignment Check",
|
||||
"Machine Check",
|
||||
"Reserved",
|
||||
"Reserved",
|
||||
"Reserved",
|
||||
"Reserved",
|
||||
"Reserved",
|
||||
|
||||
"Reserved",
|
||||
"Reserved",
|
||||
"Reserved",
|
||||
"Reserved",
|
||||
"Reserved",
|
||||
"Reserved",
|
||||
"Reserved",
|
||||
"Reserved"
|
||||
};
|
||||
@@ -0,0 +1,22 @@
|
||||
#include "../include/isr.h"
|
||||
#include "../include/screen.h"
|
||||
|
||||
/* Check if the compiler thinks you are targeting the wrong operating system. */
|
||||
/*#if defined(__linux__)
|
||||
#error "You are not using a cross-compiler, you will most certainly run into trouble"
|
||||
#endif
|
||||
*/
|
||||
/* This tutorial will only work for the 32-bit ix86 targets. */
|
||||
/*#if !defined(__i386__)
|
||||
#error "This tutorial needs to be compiled with a ix86-elf compiler"
|
||||
#endif
|
||||
*/
|
||||
|
||||
void kmain()
|
||||
{
|
||||
terminal_initialize();
|
||||
terminal_writestring("KatauOS booting up");
|
||||
isr_install();
|
||||
terminal_writestring("Kernel init sequence completed");
|
||||
return;
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
// screen.c
|
||||
#include "../include/screen.h"
|
||||
|
||||
static size_t terminal_row;
|
||||
static size_t terminal_column;
|
||||
static uint8_t terminal_color;
|
||||
static uint16_t* terminal_buffer;
|
||||
|
||||
uint8_t vga_entry_color(enum vga_color fg, enum vga_color bg)
|
||||
{
|
||||
return fg | bg << 4;
|
||||
}
|
||||
|
||||
uint16_t vga_entry(unsigned char uc, uint8_t color)
|
||||
{
|
||||
return (uint16_t) uc | (uint16_t) color << 8;
|
||||
}
|
||||
|
||||
size_t strlen(const char* str)
|
||||
{
|
||||
size_t len = 0;
|
||||
while (str[len])
|
||||
len++;
|
||||
return len;
|
||||
}
|
||||
|
||||
void terminal_initialize(void)
|
||||
{
|
||||
terminal_row = 0;
|
||||
terminal_column = 0;
|
||||
terminal_color = vga_entry_color(VGA_COLOR_LIGHT_GREY, VGA_COLOR_BLACK);
|
||||
terminal_buffer = (uint16_t*) 0xB8000;
|
||||
for (size_t y = 0; y < VGA_HEIGHT; y++) {
|
||||
for (size_t x = 0; x < VGA_WIDTH; x++) {
|
||||
const size_t index = y * VGA_WIDTH + x;
|
||||
terminal_buffer[index] = vga_entry(' ', terminal_color);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void terminal_setcolor(uint8_t color)
|
||||
{
|
||||
terminal_color = color;
|
||||
}
|
||||
|
||||
void terminal_putentryat(char c, uint8_t color, size_t x, size_t y)
|
||||
{
|
||||
const size_t index = y * VGA_WIDTH + x;
|
||||
terminal_buffer[index] = vga_entry(c, color);
|
||||
}
|
||||
|
||||
void terminal_putchar(char c)
|
||||
{
|
||||
terminal_putentryat(c, terminal_color, terminal_column, terminal_row);
|
||||
if (++terminal_column == VGA_WIDTH) {
|
||||
terminal_column = 0;
|
||||
if (++terminal_row == VGA_HEIGHT)
|
||||
terminal_row = 0;
|
||||
}
|
||||
}
|
||||
|
||||
void terminal_write(const char* data, size_t size)
|
||||
{
|
||||
for (size_t i = 0; i < size; i++)
|
||||
terminal_putchar(data[i]);
|
||||
}
|
||||
|
||||
void terminal_writestring(const char* data)
|
||||
{
|
||||
terminal_write(data, strlen(data));
|
||||
terminal_row++;
|
||||
terminal_column = 0;
|
||||
}
|
||||
Reference in New Issue
Block a user