Idt in progress !

This commit is contained in:
Adrien Bourmault 2019-04-23 17:06:03 +02:00
parent ca6c7bbcf9
commit 3dbe85b936
5 changed files with 131 additions and 73 deletions

View File

@ -37,7 +37,9 @@ typedef struct IdtPtr_t IdtPtr_t;
typedef struct Registers_t Registers_t; typedef struct Registers_t Registers_t;
// -------------------------------------------------------------------------- // // -------------------------------------------------------------------------- //
//
#define interrupt(n) asm volatile ("int %0" : : "N" (n) : "cc", "memory") \
// CPU features masks // CPU features masks
enum { enum {
FEAT_ECX_SSE3 = 1 << 0, FEAT_ECX_SSE3 = 1 << 0,
@ -99,6 +101,41 @@ enum {
FEAT_EDX_PBE = 1 << 31 FEAT_EDX_PBE = 1 << 31
}; };
static const char *IsrExceptions[32] = {
"Divide Error Fault",
"Debug Exception Trap",
"Non-maskable Interrupt",
"Breakpoint Trap",
"Overflow Trap",
"Bound Range Exceeded Fault",
"Invalid Opcode Fault",
"Device Not Available or No Math Coprocessor Fault",
"Double Fault Abort",
"Coprocessor Segment Overrun Fault",
"Invalid TSS Fault",
"Segment Not Present Fault",
"Stack Segment fault",
"General Protection Fault",
"Page Fault",
"Reserved",
"x87 FPU Floating Point or Math Fault",
"Alignment Check Fault",
"Machine Check Abort",
"SIMD Floating Point Fault",
"Virtualization Exception Fault",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved"
};
struct IdtDescriptor_t { struct IdtDescriptor_t {
ushort limit; ushort limit;
ulong base; ulong base;
@ -134,8 +171,8 @@ struct Registers_t
// -------------------------------------------------------------------------- // // -------------------------------------------------------------------------- //
void CpuIdtSetup(void); void CpuIdtSetup(void);
void idtSet(uchar rank, ulong base, ushort selector, uchar flags); void IdtSetGate(uchar rank, ulong base, ushort selector, uchar flags);
void isrHandler(Registers_t reg); void IdtHandler(ulong intNo);
void disablePIC(void); void disablePIC(void);
// -------------------------------------------------------------------------- // // -------------------------------------------------------------------------- //

View File

@ -1,37 +1,39 @@
//----------------------------------------------------------------------------// //----------------------------------------------------------------------------//
// GNU GPL OS/K // // GNU GPL OS/K //
// // // //
// Desc: Interrupt related functions // // Desc: Interrupt related functions //
// // // //
// // // //
// Copyright © 2018-2019 The OS/K Team // // Copyright © 2018-2019 The OS/K Team //
// // // //
// This file is part of OS/K. // // This file is part of OS/K. //
// // // //
// OS/K is free software: you can redistribute it and/or modify // // OS/K is free software: you can redistribute it and/or modify //
// it under the terms of the GNU General Public License as published by // // it under the terms of the GNU General Public License as published by //
// the Free Software Foundation, either version 3 of the License, or // // the Free Software Foundation, either version 3 of the License, or //
// any later version. // // any later version. //
// // // //
// OS/K is distributed in the hope that it will be useful, // // OS/K is distributed in the hope that it will be useful, //
// but WITHOUT ANY WARRANTY//without even the implied warranty of // // but WITHOUT ANY WARRANTY//without even the implied warranty of //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
// GNU General Public License for more details. // // GNU General Public License for more details. //
// // // //
// You should have received a copy of the GNU General Public License // // You should have received a copy of the GNU General Public License //
// along with OS/K. If not, see <https://www.gnu.org/licenses/>. // // along with OS/K. If not, see <https://www.gnu.org/licenses/>. //
//----------------------------------------------------------------------------// //----------------------------------------------------------------------------//
#include <kernel/base.h> #include <kernel/base.h>
#include <kernel/cpu.h> #include <kernel/cpu.h>
#include <kernel/boot.h> #include <kernel/boot.h>
#include <kernel/iomisc.h> #include <kernel/iomisc.h>
#include <extras/buf.h>
IdtEntry_t idt[256] = { 0 }; IdtEntry_t idt[256] = { 0 };
IdtPtr_t idtPtr; IdtPtr_t idtPtr;
void CpuIdtSetup(void) void CpuIdtSetup(void)
{ {
// XXX detect the APIC with cpuid !
disablePIC(); disablePIC();
ushort codeSeg = (ushort)(ulong)BtLoaderInfo.codeSegment; ushort codeSeg = (ushort)(ulong)BtLoaderInfo.codeSegment;
@ -41,37 +43,37 @@ void CpuIdtSetup(void)
idtPtr.base = &idt; idtPtr.base = &idt;
// Set IDT gates // Set IDT gates
idtSet(0, (ulong)isr0, codeSeg, 0x8E); IdtSetGate(0, (ulong)isr0, codeSeg, 0x8E);
idtSet(1, (ulong)isr1, codeSeg, 0x8E); IdtSetGate(1, (ulong)isr1, codeSeg, 0x8E);
idtSet(2, (ulong)isr2, codeSeg, 0x8E); IdtSetGate(2, (ulong)isr2, codeSeg, 0x8E);
idtSet(3, (ulong)isr3, codeSeg, 0x8E); IdtSetGate(3, (ulong)isr3, codeSeg, 0x8E);
idtSet(4, (ulong)isr4, codeSeg, 0x8E); IdtSetGate(4, (ulong)isr4, codeSeg, 0x8E);
idtSet(5, (ulong)isr5, codeSeg, 0x8E); IdtSetGate(5, (ulong)isr5, codeSeg, 0x8E);
idtSet(6, (ulong)isr6, codeSeg, 0x8E); IdtSetGate(6, (ulong)isr6, codeSeg, 0x8E);
idtSet(7, (ulong)isr7, codeSeg, 0x8E); IdtSetGate(7, (ulong)isr7, codeSeg, 0x8E);
idtSet(8, (ulong)isr8, codeSeg, 0x8E); IdtSetGate(8, (ulong)isr8, codeSeg, 0x8E);
idtSet(9, (ulong)isr9, codeSeg, 0x8E); IdtSetGate(9, (ulong)isr9, codeSeg, 0x8E);
idtSet(10, (ulong)isr10, codeSeg, 0x8E); IdtSetGate(10, (ulong)isr10, codeSeg, 0x8E);
idtSet(11, (ulong)isr11, codeSeg, 0x8E); IdtSetGate(11, (ulong)isr11, codeSeg, 0x8E);
idtSet(12, (ulong)isr12, codeSeg, 0x8E); IdtSetGate(12, (ulong)isr12, codeSeg, 0x8E);
idtSet(13, (ulong)isr13, codeSeg, 0x8E); IdtSetGate(13, (ulong)isr13, codeSeg, 0x8E);
idtSet(14, (ulong)isr14, codeSeg, 0x8E); IdtSetGate(14, (ulong)isr14, codeSeg, 0x8E);
//idtSet(15, (ulong)isr15, codeSeg, 0x8E); INTEL RESERVED IdtSetGate(15, (ulong)isr15, codeSeg, 0x8E); // INTEL RESERVED
idtSet(16, (ulong)isr16, codeSeg, 0x8E); IdtSetGate(16, (ulong)isr16, codeSeg, 0x8E);
idtSet(17, (ulong)isr17, codeSeg, 0x8E); IdtSetGate(17, (ulong)isr17, codeSeg, 0x8E);
idtSet(18, (ulong)isr18, codeSeg, 0x8E); IdtSetGate(18, (ulong)isr18, codeSeg, 0x8E);
idtSet(19, (ulong)isr19, codeSeg, 0x8E); IdtSetGate(19, (ulong)isr19, codeSeg, 0x8E);
idtSet(20, (ulong)isr20, codeSeg, 0x8E); IdtSetGate(20, (ulong)isr20, codeSeg, 0x8E);
//idtSet(21, (ulong)isr21, codeSeg, 0x8E); INTEL RESERVED IdtSetGate(21, (ulong)isr21, codeSeg, 0x8E); // INTEL RESERVED
//idtSet(22, (ulong)isr22, codeSeg, 0x8E); INTEL RESERVED IdtSetGate(22, (ulong)isr22, codeSeg, 0x8E); // INTEL RESERVED
//idtSet(23, (ulong)isr23, codeSeg, 0x8E); INTEL RESERVED IdtSetGate(23, (ulong)isr23, codeSeg, 0x8E); // INTEL RESERVED
//idtSet(24, (ulong)isr24, codeSeg, 0x8E); INTEL RESERVED IdtSetGate(24, (ulong)isr24, codeSeg, 0x8E); // INTEL RESERVED
//idtSet(25, (ulong)isr25, codeSeg, 0x8E); INTEL RESERVED IdtSetGate(25, (ulong)isr25, codeSeg, 0x8E); // INTEL RESERVED
//idtSet(26, (ulong)isr26, codeSeg, 0x8E); INTEL RESERVED IdtSetGate(26, (ulong)isr26, codeSeg, 0x8E); // INTEL RESERVED
//idtSet(27, (ulong)isr27, codeSeg, 0x8E); INTEL RESERVED IdtSetGate(27, (ulong)isr27, codeSeg, 0x8E); // INTEL RESERVED
//idtSet(28, (ulong)isr28, codeSeg, 0x8E); INTEL RESERVED IdtSetGate(28, (ulong)isr28, codeSeg, 0x8E); // INTEL RESERVED
//idtSet(29, (ulong)isr29, codeSeg, 0x8E); INTEL RESERVED IdtSetGate(29, (ulong)isr29, codeSeg, 0x8E); // INTEL RESERVED
//idtSet(30, (ulong)isr30, codeSeg, 0x8E); INTEL RESERVED IdtSetGate(30, (ulong)isr30, codeSeg, 0x8E); // INTEL RESERVED
// Load IDT // Load IDT
DebugLog("[IdtSetup] Filled \n"); DebugLog("[IdtSetup] Filled \n");
@ -79,7 +81,7 @@ void CpuIdtSetup(void)
DebugLog("[IdtSetup] Initialized !\n"); DebugLog("[IdtSetup] Initialized !\n");
} }
void idtSet(uchar rank, ulong base, ushort selector, uchar flags) void IdtSetGate(uchar rank, ulong base, ushort selector, uchar flags)
{ {
// Set Base Address // Set Base Address
idt[rank].baseLow = base & 0xFFFF; idt[rank].baseLow = base & 0xFFFF;
@ -121,8 +123,20 @@ void disablePIC(void) {
*val |= (1<<8); *val |= (1<<8);
} }
void isrHandler(Registers_t regs) void IdtHandler(ulong intNo)
{ {
DebugLog("Interrupt %d !!! \n", regs.intNo); int irrecoverable = 0;
char *exceptionMsg = "Unhandled ISR exception";
if (intNo == 6 || intNo == 8 || intNo == 13) irrecoverable++;
if (intNo < 32) exceptionMsg = IsrExceptions[intNo];
if (irrecoverable) {
KeStartPanic("Irrecoverable exception 0x%x : %s\n", intNo, exceptionMsg);
} else {
bprintf(BStdOut, "Exception 0x%x : %s\n", intNo, exceptionMsg);
}
return; return;
} }

View File

@ -26,14 +26,13 @@
global idtInit global idtInit
extern idtPtr extern idtPtr
extern isrHandler extern IdtHandler
;; ;;
;; Loads the IDT ;; Loads the IDT
;; ;;
idtInit: idtInit:
lidt [idtPtr] lidt [idtPtr]
sti
ret ret
;; ;;
@ -46,13 +45,12 @@ isrPreHandler:
mov ax, ds mov ax, ds
push rax push rax
call isrHandler call IdtHandler
pop rax pop rax
mov ds, ax mov ds, ax
popAll popAll
add rsp, 8
sti sti
iretq iretq
@ -115,3 +113,18 @@ IsrWithErrCode 13
;; Page Fault ;; Page Fault
IsrWithErrCode 14 IsrWithErrCode 14
;; Reserved
IsrWithoutErrCode 15
IsrWithoutErrCode 21
IsrWithoutErrCode 22
IsrWithoutErrCode 23
IsrWithoutErrCode 24
IsrWithoutErrCode 25
IsrWithoutErrCode 26
IsrWithoutErrCode 27
IsrWithoutErrCode 28
IsrWithoutErrCode 29
IsrWithoutErrCode 30
IsrWithoutErrCode 31
IsrWithoutErrCode 32

View File

@ -45,8 +45,7 @@
global isr%1 global isr%1
isr%1: isr%1:
cli cli
push byte 0 mov rdi, %1
push byte %1
jmp isrPreHandler jmp isrPreHandler
%endmacro %endmacro
@ -54,6 +53,6 @@
global isr%1 global isr%1
isr%1: isr%1:
cli cli
push byte %1 mov rdi, %1
jmp isrPreHandler jmp isrPreHandler
%endmacro %endmacro

View File

@ -40,12 +40,6 @@ extern error_t IoInitVGABuffer(void);
// ps/proc.c test function // ps/proc.c test function
extern void pstest(void); extern void pstest(void);
void bug(void)
{
asm volatile ("int %0" : : "N" (0) : "cc", "memory");
}
// //
// Entry point of the Kaleid kernel // Entry point of the Kaleid kernel
// //
@ -75,9 +69,10 @@ noreturn void BtStartKern(multiboot_info_t *mbInfo, uint mbMagic, void *codeSeg)
//MmPrintMemoryMap(); //MmPrintMemoryMap();
CpuIdtSetup(); CpuIdtSetup();
KeEnableIRQs();
bug(); long a = -1;
DebugLog("%s", a);
// End this machine's suffering // End this machine's suffering
BFlushBuf(BStdOut); BFlushBuf(BStdOut);
KeCrashSystem(); KeCrashSystem();