alcedo/kernel/arch/i386/paging.nim

49 lines
2.0 KiB
Nim
Raw Normal View History

2018-05-02 23:03:01 +01:00
import serial
type
PageDirectoryEntry = uint32
PageTableEntry = uint32
PageDirectory = array[1024, PageDirectoryEntry]
PageTable = array[1024, PageTableEntry]
const
blankPageDirectory: PageDirectoryEntry = 0x00000002
var
pageDirectory{.exportc, codegenDecl: "$# $# __attribute__((aligned(0x1000)))".}: PageDirectory
pageTable0{.exportc, codegenDecl: "$# $# __attribute__((aligned(0x1000)))".}: PageTable
2018-05-02 23:03:01 +01:00
proc createEntry(address: uint32, present: bool = false, readWrite: bool = false, userAccessible: bool = false, writeThrough: bool = false, disableCache: bool = false): uint32 =
result = address and 0xFFFFF000'u32 # Set attributes section to zero
if present: result = result or 0b1
if readWrite: result = result or 0b10
if userAccessible: result = result or 0b100
if writeThrough: result = result or 0b1000
if disableCache: result = result or 0b10000
proc createDirectory(address: uint32, present: bool = false, readWrite: bool = false, userAccessible: bool = false, writeThrough: bool = false, disableCache: bool = false, pageSize: bool = false): uint32 =
var checkAddress = address
if pageSize: checkAddress = checkAddress and 0xFFFF0000'u32
result = createEntry(address, present, readWrite, userAccessible, writeThrough, disableCache)
if pageSize: result = result or 0b10000000
proc loadPageDirectory(address: uint32){.inline.} =
serial.writeLine("Loading paging data")
2018-05-02 23:03:01 +01:00
asm """
mov %%cr3, %%eax
mov %%eax, %%cr0
or $0x80000001, %%eax
mov %%cr0, %%eax
::"a"(`address`)
2018-05-02 23:03:01 +01:00
"""
proc init*() =
serial.writeLine("Initialising paging tables and directories.")
for i in 0..1023:
pageDirectory[i] = blankPageDirectory
for i in 0..1023:
pageTable0[i] = createEntry(address = uint32(i) * 0x1000, readWrite = true, present = true)
pageDirectory[0] = createDirectory(address = cast[uint32](pageTable0), readWrite = true, present = true)
loadPageDirectory(cast[uint32](pageDirectory.addr))
serial.write("Paging setup complete at ")
serial.writeLine(cast[uint32](pageDirectory.addr))