Commit f98f0cda by malcolm

Interrupts work now, fixed up GDT and IDT

parent b7c23627
#include "gdt.h" #include "gdt.h"
#include "string.h" #include "string.h"
#include "serial.h"
#include "term.h"
uint16_t gdt_limit; uint16_t gdt_limit;
uint32_t gdt_base; uint32_t gdt_base;
static gdt_desc _gdt [MAX_GDT_DESC]; gdt_desc _gdt [MAX_GDT_DESC];
static void gdt_install(){ #define set_cs( cs ) asm volatile ( "ljmp %0, $1337f \n\t 1337: \n\t" :: "i"(cs) )
void gdt_install(){
struct{ struct{
uint16_t limit; uint16_t limit;
uint32_t base; uint32_t base;
...@@ -14,6 +17,20 @@ static void gdt_install(){ ...@@ -14,6 +17,20 @@ static void gdt_install(){
gdtr.limit = gdt_limit; gdtr.limit = gdt_limit;
gdtr.base = gdt_base; gdtr.base = gdt_base;
asm volatile ("lgdt (%0)": :"r"(&gdtr)); asm volatile ("lgdt (%0)": :"r"(&gdtr));
//serial_writestring("gdt installed, now setting cs\n");
set_cs(8);
//serial_writestring("cs set, setting ds\n");
//asm volatile ("ljmp $0x8,$1f;
// 1:
asm volatile(" mov $0x10, %ax;\
mov %ax, %ds;\
mov %ax, %es;\
mov %ax, %fs;\
mov %ax, %gs;\
mov %ax, %ss;");
//serial_writestring("ds set, we done here. moving on to idt\n");
//terminal_writestring("");
strlen("");
} }
void gdt_set_desc(uint32_t i, uint64_t base, uint64_t limit, uint8_t access, uint8_t gran){ void gdt_set_desc(uint32_t i, uint64_t base, uint64_t limit, uint8_t access, uint8_t gran){
if (i > MAX_GDT_DESC) if (i > MAX_GDT_DESC)
...@@ -30,7 +47,6 @@ void gdt_set_desc(uint32_t i, uint64_t base, uint64_t limit, uint8_t access, uin ...@@ -30,7 +47,6 @@ void gdt_set_desc(uint32_t i, uint64_t base, uint64_t limit, uint8_t access, uin
_gdt[i].gran |= gran & 0xf0; _gdt[i].gran |= gran & 0xf0;
} }
gdt_desc* gdt_get_desc (int i) { gdt_desc* gdt_get_desc (int i) {
if (i > MAX_GDT_DESC) if (i > MAX_GDT_DESC)
return 0; return 0;
return &_gdt[i]; return &_gdt[i];
...@@ -44,6 +60,7 @@ int gdt_init() { ...@@ -44,6 +60,7 @@ int gdt_init() {
gdt_set_desc(1,0,0xffffffff, GDT_RW|GDT_EXE|GDT_CDT|GDT_MEM, GDT_GR_PG|GDT_GR_32|GDT_GR_HI); gdt_set_desc(1,0,0xffffffff, GDT_RW|GDT_EXE|GDT_CDT|GDT_MEM, GDT_GR_PG|GDT_GR_32|GDT_GR_HI);
//data //data
gdt_set_desc(2,0,0xffffffff, GDT_RW|GDT_CDT|GDT_MEM, GDT_GR_PG|GDT_GR_32|GDT_GR_HI); gdt_set_desc(2,0,0xffffffff, GDT_RW|GDT_CDT|GDT_MEM, GDT_GR_PG|GDT_GR_32|GDT_GR_HI);
//serial_writestring("gdt filled, now installing\n");
gdt_install(); gdt_install();
return 0; return 0;
} }
...@@ -5,22 +5,33 @@ ...@@ -5,22 +5,33 @@
uint16_t idt_limit; uint16_t idt_limit;
uint32_t idt_base; uint32_t idt_base;
static idt_desc _idt [MAX_IDT_INT]; idt_desc _idt [MAX_IDT_INT];
static void idt_install(){ void idt_install() {
struct{ struct{
uint16_t limit; uint16_t limit;
uint32_t base; uint32_t base;
} __attribute__((packed)) idtr; } __attribute__((packed)) idtr;
//char s[1];
//itoa(sizeof(idtr),s);
//serial_writestring(s);
idtr.limit = idt_limit; idtr.limit = idt_limit;
idtr.base = idt_base; idtr.base = idt_base;
char f[20];
itoa(idtr.limit,f);
//serial_writestring(f);
//serial_writestring("\n");
itoa(idtr.base,f);
//serial_writestring(f);
//serial_writestring("\n");
asm volatile ("lidt (%0)": :"r"(&idtr)); asm volatile ("lidt (%0)": :"r"(&idtr));
} }
static void default_handler () { static void default_handler () {
//DEBUG OUTPUT MAYBE? //DEBUG OUTPUT MAYBE?
serial_writestring("int caught\n"); serial_writestring("int caught\n");
for(;;); //for(;;);
return;
} }
idt_desc* get_ir (uint32_t i) { idt_desc* get_ir (uint32_t i) {
...@@ -28,6 +39,7 @@ idt_desc* get_ir (uint32_t i) { ...@@ -28,6 +39,7 @@ idt_desc* get_ir (uint32_t i) {
return 0; return 0;
return &_idt[i]; return &_idt[i];
} }
int install_ir (uint32_t i, uint16_t flags, uint16_t code_sel, IRQ_HANDLER irq) { int install_ir (uint32_t i, uint16_t flags, uint16_t code_sel, IRQ_HANDLER irq) {
if (i>MAX_IDT_INT) if (i>MAX_IDT_INT)
return 0; return 0;
...@@ -41,12 +53,16 @@ int install_ir (uint32_t i, uint16_t flags, uint16_t code_sel, IRQ_HANDLER irq) ...@@ -41,12 +53,16 @@ int install_ir (uint32_t i, uint16_t flags, uint16_t code_sel, IRQ_HANDLER irq)
_idt[i].code_sel = code_sel; _idt[i].code_sel = code_sel;
return 0; return 0;
} }
int idt_init (uint16_t code_sel) { int idt_init (uint16_t code_sel) {
idt_limit = sizeof (idt_desc) * MAX_IDT_INT -1; idt_limit = sizeof (idt_desc) * MAX_IDT_INT -1;
idt_base = (uint32_t)&_idt[0]; idt_base = (uint32_t)&_idt[0];
memset ((void*)&_idt[0], 0, sizeof (idt_desc) * MAX_IDT_INT-1); memset ((void*)&_idt[0], 0, sizeof (idt_desc) * MAX_IDT_INT-1);
for (int i=0; i<MAX_IDT_INT; i++) for (int i=0; i<MAX_IDT_INT; i++)
install_ir (i, IDT_PR|IDT_32,code_sel,(IRQ_HANDLER)default_handler); install_ir (i, IDT_PR|IDT_32,code_sel,(IRQ_HANDLER)default_handler);
idt_install (); idt_install();
char f[20];
itoa((uintptr_t)&_idt,f);
//serial_writestring(f);
return 0; return 0;
} }
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
#include <stddef.h> #include <stddef.h>
#include <stdint.h> #include <stdint.h>
#include "string.h"
#include "serial.h" #include "serial.h"
#include "term.h" #include "term.h"
#include "mem.h" #include "mem.h"
...@@ -15,16 +16,21 @@ void kernel_main() { ...@@ -15,16 +16,21 @@ void kernel_main() {
/* Initialize terminal and serial interfaces */ /* Initialize terminal and serial interfaces */
terminal_initialize(); terminal_initialize();
serial_init(); serial_init();
char b[2];
int a = strlen("");
itoa(a,b);
terminal_writestring(b);
gdt_init(); gdt_init();
idt_init(0x0); idt_init(0x8);
/* Since there is no support for newlines in terminal_putchar /* Since there is no support for newlines in terminal_putchar
* yet, '\n' will produce some VGA specific character instead. * yet, '\n' will produce some VGA specific character instead.
* This is normal. * This is normal.
*/ */
serial_writestring("First Serial Test!\n"); serial_writestring("IDT initialized\n");
terminal_writestring("Hello, kernel World!\n"); terminal_writestring("Hello, kernel World!\n");
serial_writestring("Second Serial Test\n"); //serial_writestring("Second Serial Test\n");
// Enable paging of memory. For now there is only one virtual // Enable paging of memory. For now there is only one virtual
// memory space defined by page_dir // memory space defined by page_dir
...@@ -35,4 +41,7 @@ void kernel_main() { ...@@ -35,4 +41,7 @@ void kernel_main() {
enablePaging(); enablePaging();
serial_writestring("Finished Initilizing memory\n"); serial_writestring("Finished Initilizing memory\n");
asm ("int $0x80");
asm ("int $0x90");
asm ("hlt");
} }
C=/home/adam/college/kernel/cross/bin/i686-elf-gcc C=i686-elf-gcc
all: all:
/usr/bin/nasm -felf32 head.asm -o boot.o /usr/bin/nasm -felf32 head.asm -o boot.o
......
...@@ -6,3 +6,19 @@ void *memset(void *s, int c, size_t n){ ...@@ -6,3 +6,19 @@ void *memset(void *s, int c, size_t n){
*p++=(unsigned char) c; *p++=(unsigned char) c;
return s; return s;
} }
void itoa(int n, char s[])
{
int i, sign;
if ((sign = n) < 0) /* record sign */
n = -n; /* make n positive */
i = 0;
do { /* generate digits in reverse order */
s[i++] = n % 10 + '0'; /* get next digit */
} while ((n /= 10) > 0); /* delete it */
if (sign < 0)
s[i++] = '-';
s[i] = '\0';
//reverse(s);
}
...@@ -3,4 +3,6 @@ ...@@ -3,4 +3,6 @@
#include<stddef.h> #include<stddef.h>
void* memset(void *s, int c, size_t n); void* memset(void *s, int c, size_t n);
void itoa(int n, char s[]);
#endif #endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment