Simplified GDT installation.

This commit is contained in:
neviyn 2018-04-28 01:03:53 +01:00
parent 6ead11b75a
commit 8a6d52e8d4
2 changed files with 14 additions and 26 deletions

View File

@ -18,23 +18,14 @@ type
entries: array[0..2, GDTEntry] entries: array[0..2, GDTEntry]
const const
gdtAddress: uint32 = 0x00000800 gdtAddress: uint32 = 0x800
gdt = cast[ptr GDT](gdtAddress) gdt = cast[ptr GDT](gdtAddress)
{.push stackTrace:off.}
proc gdtFlush(){.inline,asmNoStackFrame.} = proc gdtFlush(){.inline,asmNoStackFrame.} =
asm """ asm """
cli lgdt (0x800)
lgdtl [`0x00000800`] ret
movw $0x10, %ax
movw %ax, %ds
movw %ax, %es
movw %ax, %fs
movw %ax, %gs
ljmp $0x08, $next
next:
""" """
{.pop.}
proc gdtSetGate(num: int, base: uint16, limit: uint32, access: AccessByte, flags: Flags) = proc gdtSetGate(num: int, base: uint16, limit: uint32, access: AccessByte, flags: Flags) =
# Setup the descriptor base address # Setup the descriptor base address
@ -62,15 +53,23 @@ proc createFlags(granularity: bool, size: bool): Flags =
result = result or 0b01000000'u8 result = result or 0b01000000'u8
proc gdtInstall*() = proc gdtInstall*() =
gdt.descriptor.limit = uint16(sizeof(GDTEntry) * gdt.entries.len) - 1; gdt.descriptor.limit = uint16(sizeof(gdt.entries)) - 1
gdt.descriptor.base = cast[uint32](gdt.entries.addr) gdt.descriptor.base = cast[uint32](gdt.entries.addr)
# NULL descriptor # NULL descriptor
gdtSetGate(0, 0, 0, 0, 0); gdtSetGate(0, 0, 0, 0, 0);
# The second entry is our Code Segment # The second entry is our Code Segment
gdtSetGate(1, 0'u16, 0xFFFFFFFF'u32, createAccessByte(0, false, true), createFlags(true, true)); gdtSetGate(1, 0'u16, 0xFFFFFFFF'u32, 0x9A, createFlags(true, true));
# The third entry is our Data Segment # The third entry is our Data Segment
gdtSetGate(2, 0'u16, 0xFFFFFFFF'u32, createAccessByte(0, false, true), createFlags(true, true)); gdtSetGate(2, 0'u16, 0xFFFFFFFF'u32, 0x92, createFlags(true, true));
#gdtSetGate(3, 0'u16, 0xFFFFFFFF'u32, 0xFA, createFlags(true, true));
#gdtSetGate(4, 0'u16, 0xFFFFFFFF'u32, 0xF2, createFlags(true, true));
serial.write("GDT Address:")
serial.write(gdt.descriptor.base)
serial.write("\L")
serial.write("Flushing GDT.\L") serial.write("Flushing GDT.\L")
gdtFlush() gdtFlush()

View File

@ -1,11 +0,0 @@
type
InterruptFrame = ptr object
ip: uint
cs: uint
flags: uint
sp: uint
ss: uint
# http://clang.llvm.org/docs/AttributeReference.html#interrupt-avr
proc interrupt_handler(frame: InterruptFrame) {.exportc, codegenDecl: "__attribute__((interrupt)) $# $#$#".} =
discard