Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <system/System.hpp>
- #include <system/gdt/Gdt.hpp>
- #include <Kernel.hpp>
- // Values used in the GDT
- const uint8_t gdtLength = 4;
- /**
- * The GdtEntry array containing 4 entries.
- * - the null entry
- * - the Kernel code segment descriptor
- * - the Kernel data segment descriptor
- * - the TSS entry
- */
- GdtEntry gdt[gdtLength];
- /**
- * A GdtPointer pointing to the GDT.
- */
- GdtPointer gdtPointer;
- /**
- * The TSS
- */
- Tss tss;
- /**
- * External method for loading the GDT and the TSS
- */
- extern "C" void _loadGdt();
- extern "C" void _loadTss();
- /**
- * Creates a GDT gate
- *
- * @param index the gate index
- * @param base the base address
- * @param limit the limit
- * @param access the access byte
- */
- void Gdt::createGdtGate(uint32_t index, uint32_t base, uint32_t limit,
- uint8_t access, uint8_t granularity) {
- gdt[index].baseLow = (base & 0xFFFF);
- gdt[index].baseMiddle = (base >> 16) & 0xFF;
- gdt[index].baseHigh = (base >> 24) & 0xFF;
- gdt[index].limitLow = (limit & 0xFFFF);
- gdt[index].granularity = ((limit >> 16) & 0x0F);
- gdt[index].granularity |= (granularity & 0xF0);
- gdt[index].access = access;
- }
- /**
- * Installs the global descriptor table.
- *
- * - Sets the limit of the GDT pointer to the size of the GDT entries
- * - Sets the base address to the gdt's address
- * - Installs the GDT gates
- * - Mounts the GDT
- */
- void Gdt::install() {
- LOGLN("Installing GDT");
- gdtPointer.limit = (sizeof(GdtEntry) * gdtLength) - 1;
- gdtPointer.base = (uint32_t) &gdt;
- // Null descriptor, position 0x00
- createGdtGate(0, 0, 0, 0, 0);
- // Kernel code segment descriptor, position 0x08
- createGdtGate(1, 0, 0xFFFFFFFF, ACCESS_BYTE__KERNEL_CODE_SEGMENT, 0xCF);
- // Kernel data segment descriptor, position 0x10
- createGdtGate(2, 0, 0xFFFFFFFF, ACCESS_BYTE__KERNEL_DATA_SEGMENT, 0xCF);
- // TSS descriptor, position 0x18
- createGdtGate(3, (uint32_t) &tss, sizeof(Tss),
- ACCESS_BYTE__TSS_386_SEGMENT, 0x40);
- tss.ss0 = 0x10; // Kernel data
- tss.esp0 = 0; // This will be initially set once the Scheduler creates the first task
- // Load the GDT and TSS
- _loadGdt();
- _loadTss();
- }
- /**
- * Sets the ESP0 of the TSS to the value.
- */
- void Gdt::setTssEsp0(uint32_t esp) {
- tss.esp0 = esp;
- }
- // ###########################################################################
- #ifndef SYSTEM__GDT
- #define SYSTEM__GDT
- #include <stdint.h>
- #include <system/gdt/Tss.hpp>
- /**
- * Macro to create the ACCESS byte for a GDT entry to a code or data segment
- *
- * Parameters:
- * readWrite if code read allowed, if data write allowed
- * codeOrData determines if its a code (1) or a data (0) segment
- * privilege Privilege level (Ring 0 - Ring 3)
- * present 1 if present
- *
- * Bits: Values: Info:
- * 0 0 Accessed bit, free for developers use
- * 1 parameter readWrite read if code segment, or write if data segment
- * 2 0 Direction bit or conforming bit)
- * 3 parameter codeOrData Entry is a code (1) or data (0) segment
- * 4 0 Entry is for Code/Data (1) or Gate/TSS (0)
- * 5 - 6 parameter privilege Ring 0 - Ring 3
- * 7 parameter present Must be 1 for active entry
- */
- #define ACCESS_BYTE__FOR__CODE_OR_DATA(readWrite, codeOrData, privilege, present) \
- (0 | (readWrite << 1) | (0 << 2) | (codeOrData << 3) | (1 << 4) | (privilege << 5) | (present << 7))
- /**
- * Macro to create the ACCESS byte for a GDT entry to a Gate/TSS
- *
- * Parameters:
- * segmentType e.g. 0x9 for 386-TSS
- * privilege Privilege level (Ring 0 - Ring 3)
- * present 1 if present
- *
- * Bits: Values: Info:
- * 0 - 3 parameter segmentType Segment type
- * 4 0 Entry is for Code/Data (1) or Gate/TSS (0)
- * 5 - 6 parameter privilege Ring 0 - Ring 3
- * 7 parameter present Must be 1 for active entry
- */
- #define ACCESS_BYTE__FOR__GATE_OR_TSS(segmentType, privilege, present) \
- (segmentType | (0 << 4) | (privilege << 5) | (present << 7))
- /**
- * Common descriptor access flags
- */
- #define ACCESS_BYTE__KERNEL_CODE_SEGMENT ACCESS_BYTE__FOR__CODE_OR_DATA(1, 1, 0, 1)
- #define ACCESS_BYTE__KERNEL_DATA_SEGMENT ACCESS_BYTE__FOR__CODE_OR_DATA(1, 0, 0, 1)
- #define ACCESS_BYTE__USER_CODE_SEGMENT ACCESS_BYTE__FOR__CODE_OR_DATA(1, 1, 3, 1)
- #define ACCESS_BYTE__USER_DATA_SEGMENT ACCESS_BYTE__FOR__CODE_OR_DATA(1, 0, 3, 1)
- #define ACCESS_BYTE__TSS_386_SEGMENT ACCESS_BYTE__FOR__GATE_OR_TSS(0x9, 0, 1)
- /**
- * Structure of a GDT entry
- */
- struct GdtEntry {
- uint16_t limitLow :16;
- uint16_t baseLow :16;
- uint8_t baseMiddle :8;
- uint8_t access :8;
- uint16_t limitHigh :4;
- uint8_t granularity :4;
- uint8_t baseHigh :8;
- }__attribute__((packed));
- /**
- * Structure of the GDT pointer
- */
- struct GdtPointer {
- uint16_t limit;
- uint32_t base;
- }__attribute__((packed));
- /**
- * The Gdt class provides methods for setting up the
- * global descriptor table.
- */
- class Gdt {
- public:
- static void install();
- static void createGdtGate(uint32_t index, uint32_t base, uint32_t limit,
- uint8_t access, uint8_t granularity);
- static void setTssEsp0(uint32_t esp);
- };
- #endif
- // ###########################################################################
- global _loadGdt
- global _loadTss
- extern gdtPointer
- ; Mounts the global descriptor table
- ; and does a far jump back to the code
- _loadGdt:
- lgdt [gdtPointer]
- mov ax, 0x10
- mov ds, ax
- mov es, ax
- mov fs, ax
- mov gs, ax
- mov ss, ax
- jmp 0x08:flush
- flush:
- ret
- _loadTss:
- mov ax, 0x18 ; 4th entry of GDT
- ltr ax
- ret
Advertisement
Add Comment
Please, Sign In to add comment