From b4f47f0f793734240a4ed6ed55c36f1d6e2ce57b Mon Sep 17 00:00:00 2001 From: Jonas 'Sortie' Termansen Date: Thu, 1 Mar 2012 00:15:28 +0100 Subject: [PATCH] Split descriptor_tables.cpp into a gdt.cpp and idt.cpp. This was about time, since descriptor_tables was a really bad name! --- sortix/Makefile | 4 +- sortix/interrupt.cpp | 2 +- sortix/kernel.cpp | 2 +- sortix/scheduler.cpp | 2 +- sortix/x64/gdt.s | 19 ++--- sortix/x64/idt.s | 33 ++++++++ sortix/x64/scheduler.cpp | 1 - .../gdt.cpp} | 45 +---------- .../{descriptor_tables.h => x86-family/gdt.h} | 55 +------------ sortix/x86-family/idt.cpp | 68 ++++++++++++++++ sortix/x86-family/idt.h | 78 +++++++++++++++++++ sortix/x86/gdt.asm | 7 -- sortix/x86/idt.asm | 13 ++++ sortix/x86/scheduler.cpp | 1 - 14 files changed, 212 insertions(+), 118 deletions(-) create mode 100644 sortix/x64/idt.s rename sortix/{descriptor_tables.cpp => x86-family/gdt.cpp} (84%) rename sortix/{descriptor_tables.h => x86-family/gdt.h} (69%) create mode 100644 sortix/x86-family/idt.cpp create mode 100644 sortix/x86-family/idt.h create mode 100644 sortix/x86/idt.asm diff --git a/sortix/Makefile b/sortix/Makefile index e4dea8d9..344f2d4a 100644 --- a/sortix/Makefile +++ b/sortix/Makefile @@ -34,6 +34,9 @@ ifdef X86FAMILY x86-family/memorymanagement.o \ $(CPU)/interrupt.o \ $(CPU)/gdt.o \ + x86-family/gdt.o \ + $(CPU)/idt.o \ + x86-family/idt.o \ $(CPU)/syscall.o \ $(CPU)/thread.o \ $(CPU)/scheduler.o \ @@ -74,7 +77,6 @@ STATICLIBS=\ OBJS=$(CPUOBJS) \ kernel.o \ -descriptor_tables.o \ interrupt.o \ time.o \ log.o \ diff --git a/sortix/interrupt.cpp b/sortix/interrupt.cpp index 0fe9d40a..bc5ccf8e 100644 --- a/sortix/interrupt.cpp +++ b/sortix/interrupt.cpp @@ -23,7 +23,7 @@ *******************************************************************************/ #include "platform.h" -#include "descriptor_tables.h" +#include "x86-family/idt.h" #include "interrupt.h" #include "process.h" // Hack for SIGSEGV diff --git a/sortix/kernel.cpp b/sortix/kernel.cpp index f9f6e32f..8272ad5f 100644 --- a/sortix/kernel.cpp +++ b/sortix/kernel.cpp @@ -29,7 +29,7 @@ #include #include "log.h" #include "panic.h" -#include "descriptor_tables.h" +#include "x86-family/gdt.h" #include "time.h" #include "keyboard.h" #include "multiboot.h" diff --git a/sortix/scheduler.cpp b/sortix/scheduler.cpp index 1b620d9b..51a69594 100644 --- a/sortix/scheduler.cpp +++ b/sortix/scheduler.cpp @@ -32,7 +32,7 @@ #include "memorymanagement.h" #include "syscall.h" #include "sound.h" // HACK FOR SIGINT -#include "descriptor_tables.h" +#include "x86-family/gdt.h" namespace Sortix { diff --git a/sortix/x64/gdt.s b/sortix/x64/gdt.s index fedc0a5f..f3dd053a 100644 --- a/sortix/x64/gdt.s +++ b/sortix/x64/gdt.s @@ -1,6 +1,6 @@ -/****************************************************************************** +/******************************************************************************* - COPYRIGHT(C) JONAS 'SORTIE' TERMANSEN 2011. + COPYRIGHT(C) JONAS 'SORTIE' TERMANSEN 2011, 2012. This file is part of Sortix. @@ -14,13 +14,13 @@ FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. - You should have received a copy of the GNU General Public License along - with Sortix. If not, see . + You should have received a copy of the GNU General Public License along with + Sortix. If not, see . - x64/gdt.cpp + x64/gdt.s Handles initialization of the 64-bit global descriptor table. -******************************************************************************/ +*******************************************************************************/ .section .text @@ -44,13 +44,6 @@ gdt_flush: gdt_flush_postjmp: ret -.global idt_flush -.type idt_flush, @function -idt_flush: - # Load the IDT pointer. - lidt (%rdi) - ret - .global tss_flush .type tss_flush, @function tss_flush: diff --git a/sortix/x64/idt.s b/sortix/x64/idt.s new file mode 100644 index 00000000..b8a55c68 --- /dev/null +++ b/sortix/x64/idt.s @@ -0,0 +1,33 @@ +/******************************************************************************* + + COPYRIGHT(C) JONAS 'SORTIE' TERMANSEN 2011, 2012. + + This file is part of Sortix. + + Sortix is free software: you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation, either version 3 of the License, or (at your option) any later + version. + + Sortix is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + details. + + You should have received a copy of the GNU General Public License along with + Sortix. If not, see . + + x64/idt.s + Handles initialization of the 64-bit IDT. + +*******************************************************************************/ + +.section .text + +.global idt_flush +.type idt_flush, @function +idt_flush: + # Load the IDT pointer. + lidt (%rdi) + ret + diff --git a/sortix/x64/scheduler.cpp b/sortix/x64/scheduler.cpp index 25dedf89..56cc8921 100644 --- a/sortix/x64/scheduler.cpp +++ b/sortix/x64/scheduler.cpp @@ -25,7 +25,6 @@ #include "platform.h" #include "scheduler.h" #include "../memorymanagement.h" -#include "descriptor_tables.h" namespace Sortix { diff --git a/sortix/descriptor_tables.cpp b/sortix/x86-family/gdt.cpp similarity index 84% rename from sortix/descriptor_tables.cpp rename to sortix/x86-family/gdt.cpp index 139d0442..437cfce8 100644 --- a/sortix/descriptor_tables.cpp +++ b/sortix/x86-family/gdt.cpp @@ -17,16 +17,14 @@ You should have received a copy of the GNU General Public License along with Sortix. If not, see . - descriptor_tables.cpp - Initializes and handles the GDT, TSS and IDT. + x86-family/gdt.cpp + Initializes and handles the GDT and TSS. *******************************************************************************/ -#include "platform.h" +#include "../platform.h" #include -#include "descriptor_tables.h" -#include "panic.h" -#include "syscall.h" +#include "gdt.h" using namespace Maxsi; @@ -156,40 +154,5 @@ namespace Sortix #endif } } - - namespace IDT - { - extern "C" void idt_flush(addr_t); - - idt_entry_t idt_entries[256]; - idt_ptr_t idt_ptr; - - void Init() - { - idt_ptr.limit = sizeof(idt_entry_t) * 256 - 1; - idt_ptr.base = (addr_t) &idt_entries; - - Memory::Set(&idt_entries, 0, sizeof(idt_entry_t)*256); - } - - void Flush() - { - idt_flush((addr_t) &idt_ptr); - } - - void SetGate(uint8_t num, addr_t base, uint16_t sel, uint8_t flags) - { - idt_entries[num].base_low = base & 0xFFFF; - idt_entries[num].base_high = (base >> 16) & 0xFFFF; -#ifdef PLATFORM_X64 - idt_entries[num].base_highest = (base >> 32 ) & 0xFFFFFFFFU; - idt_entries[num].zero1 = 0; -#endif - - idt_entries[num].sel = sel; - idt_entries[num].always0 = 0; - idt_entries[num].flags = flags; - } - } } diff --git a/sortix/descriptor_tables.h b/sortix/x86-family/gdt.h similarity index 69% rename from sortix/descriptor_tables.h rename to sortix/x86-family/gdt.h index 8155aeff..a3ec78e5 100644 --- a/sortix/descriptor_tables.h +++ b/sortix/x86-family/gdt.h @@ -17,13 +17,13 @@ You should have received a copy of the GNU General Public License along with Sortix. If not, see . - descriptor_tables.h - Initializes and handles the GDT, TSS and IDT. + x86-family/gdt.h + Initializes and handles the GDT and TSS. *******************************************************************************/ -#ifndef SORTIX_DESCRIPTOR_TABLES_H -#define SORTIX_DESCRIPTOR_TABLES_H +#ifndef SORTIX_X86_FAMILY_GDT_H +#define SORTIX_X86_FAMILY_GDT_H namespace Sortix { @@ -122,53 +122,6 @@ namespace Sortix void WriteTSS(int32_t num, uint16_t ss0, addr_t stack0); void SetKernelStack(size_t* stack); } - - namespace IDT - { - // A struct describing an interrupt gate. - struct idt_entry32_struct - { - uint16_t base_low; // The lower 16 bits of the address to jump to when this interrupt fires. - uint16_t sel; // Kernel segment selector. - uint8_t always0; // This must always be zero. - uint8_t flags; // More flags. See documentation. - uint16_t base_high; // The upper 16 bits of the address to jump to. - } __attribute__((packed)); - - struct idt_entry64_struct - { - uint16_t base_low; // The lower 16 bits of the address to jump to when this interrupt fires. - uint16_t sel; // Kernel segment selector. - uint8_t always0; // This must always be zero. - uint8_t flags; // More flags. See documentation. - uint16_t base_high; // The upper 16 bits of the address to jump to. - uint32_t base_highest; - uint32_t zero1; // Reserved - } __attribute__((packed)); - - typedef struct idt_entry32_struct idt_entry32_t; - typedef struct idt_entry64_struct idt_entry64_t; -#ifdef PLATFORM_X64 - typedef idt_entry64_t idt_entry_t; -#else - typedef idt_entry32_t idt_entry_t; - -#endif - - // A struct describing a pointer to an array of interrupt handlers. - // This is in a format suitable for giving to 'lidt'. - struct idt_ptr_struct - { - uint16_t limit; - addr_t base; // The address of the first element in our idt_entry_t array. - } __attribute__((packed)); - - typedef struct idt_ptr_struct idt_ptr_t; - - void Init(); - void SetGate(uint8_t num, addr_t base, uint16_t sel, uint8_t flags); - void Flush(); - } } #endif diff --git a/sortix/x86-family/idt.cpp b/sortix/x86-family/idt.cpp new file mode 100644 index 00000000..e31fbd71 --- /dev/null +++ b/sortix/x86-family/idt.cpp @@ -0,0 +1,68 @@ +/******************************************************************************* + + COPYRIGHT(C) JONAS 'SORTIE' TERMANSEN 2011, 2012. + + This file is part of Sortix. + + Sortix is free software: you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation, either version 3 of the License, or (at your option) any later + version. + + Sortix is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + details. + + You should have received a copy of the GNU General Public License along with + Sortix. If not, see . + + x86-family/idt.cpp + Initializes and handles the IDT. + +*******************************************************************************/ + +#include "../platform.h" +#include +#include "idt.h" + +using namespace Maxsi; + +namespace Sortix +{ + namespace IDT + { + extern "C" void idt_flush(addr_t); + + idt_entry_t idt_entries[256]; + idt_ptr_t idt_ptr; + + void Init() + { + idt_ptr.limit = sizeof(idt_entry_t) * 256 - 1; + idt_ptr.base = (addr_t) &idt_entries; + + Memory::Set(&idt_entries, 0, sizeof(idt_entry_t)*256); + } + + void Flush() + { + idt_flush((addr_t) &idt_ptr); + } + + void SetGate(uint8_t num, addr_t base, uint16_t sel, uint8_t flags) + { + idt_entries[num].base_low = base & 0xFFFF; + idt_entries[num].base_high = (base >> 16) & 0xFFFF; +#ifdef PLATFORM_X64 + idt_entries[num].base_highest = (base >> 32 ) & 0xFFFFFFFFU; + idt_entries[num].zero1 = 0; +#endif + + idt_entries[num].sel = sel; + idt_entries[num].always0 = 0; + idt_entries[num].flags = flags; + } + } +} + diff --git a/sortix/x86-family/idt.h b/sortix/x86-family/idt.h new file mode 100644 index 00000000..2441937a --- /dev/null +++ b/sortix/x86-family/idt.h @@ -0,0 +1,78 @@ +/******************************************************************************* + + COPYRIGHT(C) JONAS 'SORTIE' TERMANSEN 2011, 2012. + + This file is part of Sortix. + + Sortix is free software: you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation, either version 3 of the License, or (at your option) any later + version. + + Sortix is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + details. + + You should have received a copy of the GNU General Public License along with + Sortix. If not, see . + + x86-family/idt.h + Initializes and handles the IDT. + +*******************************************************************************/ + +#ifndef SORTIX_X86_FAMILY_IDT_H +#define SORTIX_X86_FAMILY_IDT_H + +namespace Sortix +{ + namespace IDT + { + // A struct describing an interrupt gate. + struct idt_entry32_struct + { + uint16_t base_low; // The lower 16 bits of the address to jump to when this interrupt fires. + uint16_t sel; // Kernel segment selector. + uint8_t always0; // This must always be zero. + uint8_t flags; // More flags. See documentation. + uint16_t base_high; // The upper 16 bits of the address to jump to. + } __attribute__((packed)); + + struct idt_entry64_struct + { + uint16_t base_low; // The lower 16 bits of the address to jump to when this interrupt fires. + uint16_t sel; // Kernel segment selector. + uint8_t always0; // This must always be zero. + uint8_t flags; // More flags. See documentation. + uint16_t base_high; // The upper 16 bits of the address to jump to. + uint32_t base_highest; + uint32_t zero1; // Reserved + } __attribute__((packed)); + + typedef struct idt_entry32_struct idt_entry32_t; + typedef struct idt_entry64_struct idt_entry64_t; +#ifdef PLATFORM_X64 + typedef idt_entry64_t idt_entry_t; +#else + typedef idt_entry32_t idt_entry_t; +#endif + + // A struct describing a pointer to an array of interrupt handlers. + // This is in a format suitable for giving to 'lidt'. + struct idt_ptr_struct + { + uint16_t limit; + addr_t base; // The address of the first element in our idt_entry_t array. + } __attribute__((packed)); + + typedef struct idt_ptr_struct idt_ptr_t; + + void Init(); + void SetGate(uint8_t num, addr_t base, uint16_t sel, uint8_t flags); + void Flush(); + } +} + +#endif + diff --git a/sortix/x86/gdt.asm b/sortix/x86/gdt.asm index 9062a8e7..2dbd149e 100644 --- a/sortix/x86/gdt.asm +++ b/sortix/x86/gdt.asm @@ -20,13 +20,6 @@ gdt_flush: .flush: ret -[GLOBAL idt_flush] ; Allows the C code to call idt_flush(). - -idt_flush: - mov eax, [esp+4] ; Get the pointer to the IDT, passed as a parameter. - lidt [eax] ; Load the IDT pointer. - ret - [GLOBAL tss_flush] ; Allows our C code to call tss_flush(). tss_flush: mov ax, 0x2B ; Load the index of our TSS structure - The index is diff --git a/sortix/x86/idt.asm b/sortix/x86/idt.asm new file mode 100644 index 00000000..249ac96e --- /dev/null +++ b/sortix/x86/idt.asm @@ -0,0 +1,13 @@ +; +; idt.s -- contains global descriptor table and interrupt descriptor table +; setup code. +; Based on code from Bran's kernel development tutorials. +; Rewritten for JamesM's kernel development tutorials. + +[GLOBAL idt_flush] ; Allows the C code to call idt_flush(). + +idt_flush: + mov eax, [esp+4] ; Get the pointer to the IDT, passed as a parameter. + lidt [eax] ; Load the IDT pointer. + ret + diff --git a/sortix/x86/scheduler.cpp b/sortix/x86/scheduler.cpp index 1525e47a..6ac68e84 100644 --- a/sortix/x86/scheduler.cpp +++ b/sortix/x86/scheduler.cpp @@ -25,7 +25,6 @@ #include "platform.h" #include "scheduler.h" #include "../memorymanagement.h" -#include "descriptor_tables.h" namespace Sortix {