mirror of
https://github.com/tailix/kernel.git
synced 2025-02-10 15:36:37 -05:00
Load IDT
This commit is contained in:
parent
278e3c43d0
commit
21ce3927d9
7 changed files with 222 additions and 2 deletions
|
@ -3,7 +3,7 @@ CCPREFIX = i686-elf-
|
|||
AS = $(CCPREFIX)as
|
||||
CC = $(CCPREFIX)gcc
|
||||
|
||||
OBJS = boot.s.o main.c.o logger.c.o console.c.o gdt.c.o gdt.asm.o
|
||||
OBJS = boot.s.o main.c.o logger.c.o console.c.o gdt.c.o gdt.asm.o idt.c.o idt.asm.o isr.c.o isr.asm.o
|
||||
|
||||
all: kernel
|
||||
|
||||
|
@ -14,7 +14,7 @@ kernel: $(OBJS)
|
|||
$(CC) -T linker.ld -o $@ -ffreestanding -nostdlib -O2 -lgcc $(OBJS)
|
||||
|
||||
%.c.o: %.c
|
||||
$(CC) -c $< -o $@ -std=c99 -ffreestanding -O2 -Wall -Wextra
|
||||
$(CC) -c $< -o $@ -std=gnu99 -ffreestanding -O2 -Wall -Wextra
|
||||
|
||||
%.s.o: %.s
|
||||
$(AS) $< -o $@
|
||||
|
|
6
src/idt.asm
Normal file
6
src/idt.asm
Normal file
|
@ -0,0 +1,6 @@
|
|||
[GLOBAL idt_flush]
|
||||
|
||||
idt_flush:
|
||||
mov eax, [esp+4]
|
||||
lidt [eax]
|
||||
ret
|
104
src/idt.c
Normal file
104
src/idt.c
Normal file
|
@ -0,0 +1,104 @@
|
|||
#include "idt.h"
|
||||
|
||||
#include "logger.h"
|
||||
|
||||
static struct IdtPointer idt_pointer;
|
||||
|
||||
static struct IdtEntry idt_entries[256];
|
||||
|
||||
static void idt_set_gate(uint8_t num, uint32_t base, uint16_t sel, uint8_t flags);
|
||||
|
||||
void idt_flush(uint32_t pointer);
|
||||
|
||||
void isr0();
|
||||
void isr1();
|
||||
void isr2();
|
||||
void isr3();
|
||||
void isr4();
|
||||
void isr5();
|
||||
void isr6();
|
||||
void isr7();
|
||||
void isr8();
|
||||
void isr9();
|
||||
void isr10();
|
||||
void isr11();
|
||||
void isr12();
|
||||
void isr13();
|
||||
void isr14();
|
||||
void isr15();
|
||||
void isr16();
|
||||
void isr17();
|
||||
void isr18();
|
||||
void isr19();
|
||||
void isr20();
|
||||
void isr21();
|
||||
void isr22();
|
||||
void isr23();
|
||||
void isr24();
|
||||
void isr25();
|
||||
void isr26();
|
||||
void isr27();
|
||||
void isr28();
|
||||
void isr29();
|
||||
void isr30();
|
||||
void isr31();
|
||||
|
||||
void idt_initialize()
|
||||
{
|
||||
logger_info("Setup IDT.");
|
||||
|
||||
for (uint8_t *p = (uint8_t*)idt_entries; p < (uint8_t*)&idt_entries[256]; ++p) {
|
||||
*p = 0;
|
||||
}
|
||||
|
||||
idt_set_gate(0, (uint32_t)isr0, 0x08, 0x8E);
|
||||
idt_set_gate(1, (uint32_t)isr1, 0x08, 0x8E);
|
||||
idt_set_gate(2, (uint32_t)isr2, 0x08, 0x8E);
|
||||
idt_set_gate(3, (uint32_t)isr3, 0x08, 0x8E);
|
||||
idt_set_gate(4, (uint32_t)isr4, 0x08, 0x8E);
|
||||
idt_set_gate(5, (uint32_t)isr5, 0x08, 0x8E);
|
||||
idt_set_gate(6, (uint32_t)isr6, 0x08, 0x8E);
|
||||
idt_set_gate(7, (uint32_t)isr7, 0x08, 0x8E);
|
||||
idt_set_gate(8, (uint32_t)isr8, 0x08, 0x8E);
|
||||
idt_set_gate(9, (uint32_t)isr9, 0x08, 0x8E);
|
||||
idt_set_gate(10, (uint32_t)isr10, 0x08, 0x8E);
|
||||
idt_set_gate(11, (uint32_t)isr11, 0x08, 0x8E);
|
||||
idt_set_gate(12, (uint32_t)isr12, 0x08, 0x8E);
|
||||
idt_set_gate(13, (uint32_t)isr13, 0x08, 0x8E);
|
||||
idt_set_gate(14, (uint32_t)isr14, 0x08, 0x8E);
|
||||
idt_set_gate(15, (uint32_t)isr15, 0x08, 0x8E);
|
||||
idt_set_gate(16, (uint32_t)isr16, 0x08, 0x8E);
|
||||
idt_set_gate(17, (uint32_t)isr17, 0x08, 0x8E);
|
||||
idt_set_gate(18, (uint32_t)isr18, 0x08, 0x8E);
|
||||
idt_set_gate(19, (uint32_t)isr19, 0x08, 0x8E);
|
||||
idt_set_gate(20, (uint32_t)isr20, 0x08, 0x8E);
|
||||
idt_set_gate(21, (uint32_t)isr21, 0x08, 0x8E);
|
||||
idt_set_gate(22, (uint32_t)isr22, 0x08, 0x8E);
|
||||
idt_set_gate(23, (uint32_t)isr23, 0x08, 0x8E);
|
||||
idt_set_gate(24, (uint32_t)isr24, 0x08, 0x8E);
|
||||
idt_set_gate(25, (uint32_t)isr25, 0x08, 0x8E);
|
||||
idt_set_gate(26, (uint32_t)isr26, 0x08, 0x8E);
|
||||
idt_set_gate(27, (uint32_t)isr27, 0x08, 0x8E);
|
||||
idt_set_gate(28, (uint32_t)isr28, 0x08, 0x8E);
|
||||
idt_set_gate(29, (uint32_t)isr29, 0x08, 0x8E);
|
||||
idt_set_gate(30, (uint32_t)isr30, 0x08, 0x8E);
|
||||
idt_set_gate(31, (uint32_t)isr31, 0x08, 0x8E);
|
||||
|
||||
logger_info("Load IDT.");
|
||||
|
||||
idt_pointer.limit = sizeof(struct IdtEntry) * 256 - 1;
|
||||
idt_pointer.base = (uint32_t)&idt_entries;
|
||||
|
||||
idt_flush((uint32_t)&idt_pointer);
|
||||
}
|
||||
|
||||
void idt_set_gate(uint8_t num, uint32_t base, uint16_t sel, uint8_t flags)
|
||||
{
|
||||
idt_entries[num].base_lo = base & 0xFFFF;
|
||||
idt_entries[num].base_hi = (base >> 16) & 0xFFFF;
|
||||
idt_entries[num].sel = sel;
|
||||
idt_entries[num].always0 = 0;
|
||||
// We must uncomment the OR below when we get to using user-mode.
|
||||
// It sets the interrupt gate's privilege level to 3.
|
||||
idt_entries[num].flags = flags /* | 0x60 */;
|
||||
}
|
23
src/idt.h
Normal file
23
src/idt.h
Normal file
|
@ -0,0 +1,23 @@
|
|||
#ifndef TAILIX_KERNEL_INCLUDED_IDT
|
||||
#define TAILIX_KERNEL_INCLUDED_IDT 1
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
struct IdtPointer {
|
||||
uint16_t limit;
|
||||
uint32_t base;
|
||||
}
|
||||
__attribute__((packed));
|
||||
|
||||
struct IdtEntry {
|
||||
uint16_t base_lo;
|
||||
uint16_t sel;
|
||||
uint8_t always0;
|
||||
uint8_t flags;
|
||||
uint16_t base_hi;
|
||||
}
|
||||
__attribute__((packed));
|
||||
|
||||
void idt_initialize();
|
||||
|
||||
#endif
|
76
src/isr.asm
Normal file
76
src/isr.asm
Normal file
|
@ -0,0 +1,76 @@
|
|||
[EXTERN isr]
|
||||
|
||||
%macro ISR_NOERRCODE 1
|
||||
[GLOBAL isr%1]
|
||||
isr%1:
|
||||
cli
|
||||
push byte 0
|
||||
push byte %1
|
||||
jmp isr_common_stub
|
||||
%endmacro
|
||||
|
||||
%macro ISR_ERRCODE 1
|
||||
[GLOBAL isr%1]
|
||||
isr%1:
|
||||
cli
|
||||
push byte %1
|
||||
jmp isr_common_stub
|
||||
%endmacro
|
||||
|
||||
ISR_NOERRCODE 0
|
||||
ISR_NOERRCODE 1
|
||||
ISR_NOERRCODE 2
|
||||
ISR_NOERRCODE 3
|
||||
ISR_NOERRCODE 4
|
||||
ISR_NOERRCODE 5
|
||||
ISR_NOERRCODE 6
|
||||
ISR_NOERRCODE 7
|
||||
ISR_NOERRCODE 8
|
||||
ISR_NOERRCODE 9
|
||||
ISR_NOERRCODE 10
|
||||
ISR_NOERRCODE 11
|
||||
ISR_NOERRCODE 12
|
||||
ISR_NOERRCODE 13
|
||||
ISR_NOERRCODE 14
|
||||
ISR_NOERRCODE 15
|
||||
ISR_NOERRCODE 16
|
||||
ISR_NOERRCODE 17
|
||||
ISR_NOERRCODE 18
|
||||
ISR_NOERRCODE 19
|
||||
ISR_NOERRCODE 20
|
||||
ISR_NOERRCODE 21
|
||||
ISR_NOERRCODE 22
|
||||
ISR_NOERRCODE 23
|
||||
ISR_NOERRCODE 24
|
||||
ISR_NOERRCODE 25
|
||||
ISR_NOERRCODE 26
|
||||
ISR_NOERRCODE 27
|
||||
ISR_NOERRCODE 28
|
||||
ISR_NOERRCODE 29
|
||||
ISR_NOERRCODE 30
|
||||
ISR_NOERRCODE 31
|
||||
|
||||
isr_common_stub:
|
||||
pusha ; Pushes edi,esi,ebp,esp,ebx,edx,ecx,eax
|
||||
|
||||
mov ax, ds ; Lower 16-bits of eax = ds.
|
||||
push eax ; save the data segment descriptor
|
||||
|
||||
mov ax, 0x10 ; load the kernel data segment descriptor
|
||||
mov ds, ax
|
||||
mov es, ax
|
||||
mov fs, ax
|
||||
mov gs, ax
|
||||
|
||||
call isr
|
||||
|
||||
pop eax ; reload the original data segment descriptor
|
||||
mov ds, ax
|
||||
mov es, ax
|
||||
mov fs, ax
|
||||
mov gs, ax
|
||||
|
||||
popa ; Pops edi,esi,ebp...
|
||||
add esp, 8 ; Cleans up the pushed error code and pushed ISR number
|
||||
sti
|
||||
iret ; pops 5 things at once: CS, EIP, EFLAGS, SS, and ESP
|
6
src/isr.c
Normal file
6
src/isr.c
Normal file
|
@ -0,0 +1,6 @@
|
|||
#include "logger.h"
|
||||
|
||||
void isr()
|
||||
{
|
||||
logger_warn("ISR.");
|
||||
}
|
|
@ -1,5 +1,6 @@
|
|||
#include "logger.h"
|
||||
#include "gdt.h"
|
||||
#include "idt.h"
|
||||
|
||||
void main()
|
||||
{
|
||||
|
@ -7,6 +8,10 @@ void main()
|
|||
logger_info("Kernel initialization started.");
|
||||
|
||||
gdt_initialize();
|
||||
idt_initialize();
|
||||
|
||||
asm volatile ("int $0x3");
|
||||
asm volatile ("int $0x4");
|
||||
|
||||
logger_warn("Nothing to do.");
|
||||
logger_fail("Halt.");
|
||||
|
|
Loading…
Add table
Reference in a new issue