foliagecanine wrote:I would highly recommend you learn how paging works before trying to implement an AHCI driver, as it is extremely important in OSDev.
For the purpose I have in mind I don't need paging but of course I strongly agree with you. Paging is important and I should learn it and implement it.
foliagecanine wrote:IAre you trying to make a 32-bit or 64-bit OS? The code you pasted appears to be for a 64-bit OS (based on the presence of PML4T).
Yes, it is a 64-but OS;
foliagecanine wrote:
(It looks like that code is present in MonkOS.
Yes, I borrowed it from that repository. At the end of the post I'll add all the code that manages paging.
foliagecanine wrote:
Another option (easier) is to disable paging entirely for now.
As far as I know, and also as Octocontrabass says, paging is mandatory for 64-bit so unfortunately, I should map all the memory with identity paging.
Maybe, for now, I can leave the code that maps the first two mibs in the bootloader as it is and write a function in the kernel to map the rest of the memory.
foliagecanine wrote:If you would like, I can give you a basic overview of paging.
If you can it could be very well accepted
I'm not completely grounded on the topic but I miss a lot of stuff.
Here follows the code to init paging:
Code: Select all
SetupPageTables:
; Constants for page table bits
.Present equ 1 << 0
.ReadWrite equ 1 << 1
.WriteThru equ 1 << 3
.CacheDisable equ 1 << 4
.AttribTable equ 1 << 7 ; valid only on PT entries
.LargePage equ 1 << 7 ; valid only on PDT entries
.Guard equ 1 << 9 ; use a bit ignored by the CPU
.StdBits equ .Present | .ReadWrite
pusha
; Set segment to the root of the page table.
mov ax, Mem.PageTable >> 4
mov es, ax
.clearMemory:
; Clear all memory used to hold the page tables.
cld
xor eax, eax
xor edi, edi
mov ecx, (Mem.PageTable.End - Mem.PageTable) >> 2
rep stosd
.makeTables:
; PML4T entry 0 points to the PDPT.
mov di, Mem.PageTable.PML4T & 0xffff
mov dword [es:di], Mem.PageTable.PDPT | .StdBits
; PDPT entry 0 points to the PDT.
mov di, Mem.PageTable.PDPT & 0xffff
mov dword [es:di], Mem.PageTable.PDT | .StdBits
; PDT entry 0 maps the first 2MiB using 4KiB pages.
mov di, Mem.PageTable.PDT & 0xffff
mov dword [es:di + 0x00], Mem.PageTable.PT | .StdBits
; PDT entries 1 through 5 map the next 8MiB using 2MiB pages.
; This memory holds the kernel image and its stack.
mov dword [es:di + 0x08], 0x00200000 | .StdBits | .LargePage
mov dword [es:di + 0x10], 0x00400000 | .StdBits | .LargePage
mov dword [es:di + 0x18], 0x00600000 | .StdBits | .LargePage
mov dword [es:di + 0x20], 0x00800000 | .StdBits | .LargePage
; Prepare to create 4K-page table entries for the first 2MiB.
mov di, Mem.PageTable.PT & 0xffff
mov eax, .StdBits
mov cx, 512 ; 512 entries in first page covering 2MiB
.makePage:
; Loop through each page table entry, incrementing the physical
; address by one page each time.
mov [es:di], eax ; store physical address + .StdBits
add eax, 0x1000 ; next physical address
add di, 8 ; next page table entry
loop .makePage
.initPageRegister:
; CR3 is the page directory base register.
mov edi, Mem.PageTable
mov cr3, edi
.done:
; Clear the upper bits of 32-bit registers we used.
xor eax, eax
xor ecx, ecx
xor edi, edi
popa
ret
...
call SetupPageTables
; Enable PAE paging.
mov eax, cr4
or eax, (1 << 5) ; CR4.PAE
mov cr4, eax