KraKern

C++ Kernel
Log | Files | Refs | README | LICENSE | git clone https://git.ne02ptzero.me/git/KraKern

commit 415a1c215f8fdc87489558726ac281ee61bb0482
parent a681e9f709bc9ab07d90a746ef748f076c25c41b
Author: Ne02ptzero <louis@ne02ptzero.me>
Date:   Wed, 13 Jan 2016 01:23:21 +0100

Add(IDT & PIC): Some code. Need some sleep before memory.

Diffstat:
M.gitignore | 2+-
Aboot/x86/IDT.cpp | 61+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mboot/x86/Makefile | 2+-
Aboot/x86/x86.asm | 74++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mincludes/boot/x86.hpp | 42++++++++++++++++++++++++++++++++++++++++++
Mincludes/core/Kernel.hpp | 1+
Mkern.img | 0
Mkernel/Kernel.cpp | 3+++
8 files changed, 183 insertions(+), 2 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -1,3 +1,3 @@ *.pyc -./KraKern +KraKern *.o diff --git a/boot/x86/IDT.cpp b/boot/x86/IDT.cpp @@ -0,0 +1,61 @@ +# include <x86.hpp> + +// Asm global access +struct idtr idtr; + +// Empty functions ATM +extern void _asm_int_0(void) {}; +extern void _asm_int_1(void) {}; +extern void _asm_syscalls(void) {}; +extern void _asm_exc_GP(void) {}; +extern void _asm_exc_PF(void) {}; +extern void _asm_schedule(void) {}; + +IDT __idt; + +void IDT::init(void) { + __kern.io.loading("Interrupts Descriptor Table ..."); + this->_init_idt(); + __kern.io.done(); + __kern.io.loading("Programmable Interrupts Controller ..."); + this->_init_pic(); + __kern.io.done(); +} + +void IDT::_init_idt(void) { + // Useless atm. + for (int i = 0; i < IDT_SIZE; i++) + this->_init_idt_desc(0x08, (u32)_asm_schedule, INT_GATE, &this->_idtdesc[i]); + this->_init_idt_desc(0x08, (u32)_asm_exc_GP, INT_GATE, &this->_idtdesc[13]); + this->_init_idt_desc(0x08, (u32)_asm_exc_PF, INT_GATE, &this->_idtdesc[14]); + this->_init_idt_desc(0x08, (u32)_asm_schedule, INT_GATE, &this->_idtdesc[32]); + this->_init_idt_desc(0x08, (u32)_asm_int_1, INT_GATE, &this->_idtdesc[33]); + this->_init_idt_desc(0x08, (u32)_asm_syscalls, TRAP_GATE, &this->_idtdesc[48]); + this->_init_idt_desc(0x08, (u32)_asm_syscalls, TRAP_GATE, &this->_idtdesc[128]); + this->_idtr.limite = IDT_SIZE * 8; + this->_idtr.base = IDT_BASE; + memcpy((char *)IDT_BASE, (char *)this->_idtdesc, this->_idtr.limite); + idtr = this->_idtr; + asm("lidtl (idtr)"); +} + +void IDT::_init_idt_desc(u16 select, u32 offset, u16 type, struct idtdesc *desc) { + desc->offset0_15 = (offset & 0xffff); + desc->select = select; + desc->type = type; + desc->offset16_31 = (offset & 0xffff0000) >> 16; +} + +// PIC +void IDT::_init_pic(void) { + __kern.io.obyte(0x20, 0x11); + __kern.io.obyte(0xA0, 0x11); + __kern.io.obyte(0x21, 0x20); + __kern.io.obyte(0xA1, 0x70); + __kern.io.obyte(0x21, 0x04); + __kern.io.obyte(0xA1, 0x02); + __kern.io.obyte(0x21, 0x01); + __kern.io.obyte(0xA1, 0x01); + __kern.io.obyte(0x21, 0x0); + __kern.io.obyte(0xA1, 0x0); +} diff --git a/boot/x86/Makefile b/boot/x86/Makefile @@ -1 +1 @@ -OBJS += boot/x86/start.o boot/x86/GDT.o +OBJS += boot/x86/start.o boot/x86/GDT.o boot/x86/x86.o boot/x86/IDT.o diff --git a/boot/x86/x86.asm b/boot/x86/x86.asm @@ -0,0 +1,74 @@ +;; x86 asm functions +extern isr_default_int, do_syscalls, isr_schedule_int + +%macro SAVE_REGS 0 + pushad + push ds + push es + push fs + push gs + push ebx + mov bx,0x10 + mov ds,bx + pop ebx +%endmacro + +%macro RESTORE_REGS 0 + pop gs + pop fs + pop es + pop ds + popad +%endmacro + +%macro INTERRUPT 1 +global _asm_int_%1 +_asm_int_%1: + ; SAVE_REGS + ;push %1 + ;call isr_default_int + ;pop eax + ;mov al,0x20 + ;out 0x20,al + ;RESTORE_REGS + ;iret +%endmacro + +extern isr_GP_exc, isr_PF_exc +global _asm_syscalls, _asm_exc_GP, _asm_exc_PF +_asm_syscalls: + ; SAVE_REGS + ;push eax + ;call do_syscalls + ;pop eax + ;cli + ;sti + ;RESTORE_REGS + ;iret + + +_asm_exc_GP: + ; SAVE_REGS + ;call isr_GP_exc + ;RESTORE_REGS + ;add esp,4 + ;iret + +_asm_exc_PF: + ; SAVE_REGS + ;call isr_PF_exc + ;RESTORE_REGS + ;add esp,4 + ;iret + +global _asm_schedule +_asm_schedule: + ; SAVE_REGS + ;call isr_schedule_int + ;mov al,0x20 + ;out 0x20,al + ;RESTORE_REGS + ;iret + +INTERRUPT 1 +INTERRUPT 2 diff --git a/includes/boot/x86.hpp b/includes/boot/x86.hpp @@ -2,6 +2,15 @@ # define __x86__ # include <os.h> +// ASM functions (x86.asm) + +# define INT_GATE 0x8E00 +# define TRAP_GATE 0xEF00 + +/** +*** Global descriptor Table +**/ + # define GDT_BASE 0x00000800 # define GDT_MAX 0xFF @@ -20,6 +29,7 @@ struct gdtdesc { u8 base24_31; } __attribute__ ((packed)); + class GDT { public: void init(void); @@ -32,6 +42,38 @@ class GDT { struct gdtdesc _gdtdesc[GDT_MAX]; }; +/** +*** Interrupt descriptor Table +**/ + +# define IDT_BASE 0x00000000 +# define IDT_SIZE 0XFF + +struct idtr { + u16 limite; + u32 base; +} __attribute__ ((packed)); + +struct idtdesc { + u16 offset0_15; + u16 select; + u16 type; + u16 offset16_31; +} __attribute__ ((packed)); + +class IDT { + public: + void init(void); + + private: + void _init_idt(void); + void _init_idt_desc(u16 select, u32 offset, u16 type, struct idtdesc *desc); + void _init_pic(void); + + struct idtr _idtr; + struct idtdesc _idtdesc[IDT_SIZE]; +}; + # ifndef __KERN__ # include <Kernel.hpp> class Kernel; diff --git a/includes/core/Kernel.hpp b/includes/core/Kernel.hpp @@ -14,6 +14,7 @@ class Kernel { Io io; GDT gdt; + IDT idt; }; #endif diff --git a/kern.img b/kern.img Binary files differ. diff --git a/kernel/Kernel.cpp b/kernel/Kernel.cpp @@ -2,6 +2,7 @@ extern Io __io; extern GDT __gdt; +extern IDT __idt; Kernel __kern; Kernel::Kernel(void) { @@ -10,10 +11,12 @@ Kernel::Kernel(void) { void Kernel::init(void) { this->io = __io; this->gdt = __gdt; + this->idt = __idt; this->io.init(); this->welcome(); this->gdt.init(); + this->idt.init(); } void Kernel::welcome(void) {