Implemented IDT

But something's wrong with the GDT
Next up is a Serial Console
This commit is contained in:
Jenny Curle 2019-04-07 13:16:53 +01:00
parent 7c3dc4cacc
commit 245d09b056
8 changed files with 101 additions and 3 deletions

View File

@ -52,4 +52,11 @@ gdt_flush:
push $flush2
ljmp *(%esp) #long jump
flush2:
ret #return to c code
ret #return to c code
.global idt_load
.extern idtp
idt_load:
lidt (idtp)
ret

View File

@ -0,0 +1,7 @@
#pragma once
void gdt_set_gate(int, unsigned long, unsigned long, unsigned char, unsigned char);
void gdt_install();
void idt_set_gate(unsigned char, unsigned long, unsigned short, unsigned char);
void idt_install();

19
include/kernel/idt.h Executable file
View File

@ -0,0 +1,19 @@
#include <kernel/utils.h>
struct idt_entry {
unsigned short base_low;
unsigned short selector;
unsigned char always0;
unsigned char flags;
unsigned short base_high;
} __attribute__((packed));
struct idt_ptr {
unsigned short limit;
unsigned int base;
}__attribute__((packed));
struct idt_entry idt[256]; //interrupt table of 256 entries
struct idt_ptr idtp; //pointer to idt table
extern void idt_load();

View File

@ -9,3 +9,7 @@ size_t strlen(const char*);
unsigned char inb(unsigned short);
void outb(unsigned short, unsigned char);
void memcpy(void*, void*, size_t);
void memset(void*, int, size_t);

42
kernel/idt.c Executable file
View File

@ -0,0 +1,42 @@
#include <kernel/utils.h>
struct idt_entry {
unsigned short base_low;
unsigned short selector;
unsigned char always0;
unsigned char flags;
unsigned short base_high;
} __attribute__((packed));
struct idt_ptr {
unsigned short limit;
unsigned int base;
}__attribute__((packed));
struct idt_entry idt[256]; // Interrupt table of 256 entries
struct idt_ptr idtp; // Pointer to idt table
extern void idt_load();
void idt_set_gate(unsigned char num, unsigned long base, unsigned short sel, unsigned char flags) {
idt[num].base_low = base & 0xFFFF;
idt[num].base_high = (base >> 16) & 0xFFFF;
idt[num].selector = sel;
idt[num].always0 = 0;
// Must be uncommented when we get to usermode - it sets the permission
// level to 3
idt[num].flags = flags; // | 0x60;
}
void idt_install() {
idtp.limit = (sizeof (struct idt_entry) * 256) - 1;
idtp.base = &idt;
memset(&idt, 0, sizeof(struct idt_entry) * 256);
// All ISRs go here
idt_load();
}

View File

@ -6,7 +6,9 @@
int kernel_main(void) {
//Prepare the GDT
gdt_install();
gdt_install();
//Prepare interrupts
//idt_install();
//Prepare the screen, and blank it out.
screen_initialize();

View File

@ -16,4 +16,20 @@ unsigned char inb(unsigned short port) {
void outb(unsigned short port, unsigned char data) {
asm volatile("outb %1, %0" : : "dN" (port), "a" (data));
}
}
void memcpy(void* dest, void* src, size_t n) {
char* src_c = (char*)src;
char* dest_c = (char*)dest;
for(size_t i = 0; i < n; i++) {
dest_c[i] = src_c[i];
}
}
void memset(void* src, int chr, size_t n) {
unsigned char* ptr = src;
while(n--) {
*ptr++ = (unsigned char) chr;
}
}

View File

@ -31,6 +31,7 @@ KERNEL_OBJS= \
$(KERNEL_ARCH_OBJS) \
kernel/utils.o \
kernel/gdt.o \
kernel/idt.o \
kernel/kernel.o
OBJS=\