Can't enable paging

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
Nasmare
Posts: 1
Joined: Wed Jan 12, 2022 7:20 am

Can't enable paging

Post by Nasmare »

Hello, i'm trying to create os in c++ and i stumbled upon 1 problem: setting the paging bit causes a Triple Fault. Code here:

paging.asm:

Code: Select all

[Bits 32]
[Global PagingLoadDirectory]
[Global PagingEnable]

PagingLoadDirectory:
    push ebp
    mov ebp, esp
    mov eax, [esp+8]
    mov cr3, eax
    mov esp, ebp
    pop ebp
    ret

PagingEnable:
    push ebp
    mov ebp, esp
    mov eax, cr0
    or eax, 0x80000000
    mov cr0, eax
    mov esp, ebp
    pop ebp
    ret
kernel.cpp:

Code: Select all

extern "C" void PagingLoadDirectory(psize_t directory);
extern "C" void PagingEnable();
//...
psize_t PageDirectory = (psize_t)0x1004000;
psize_t PageTable = (psize_t)0x1005000;
for (size_t i = 0; i < 1024; i++) PageDirectory[i] = 0x00000002;
for (size_t i = 0x1000; i < 0x1400; i++) PageTable[i] = (i * 0x1000) | 3;
PageDirectory[0] = (size_t)PageTable | 3;
PagingLoadDirectory(PageDirectory);
PagingEnable();    //Triple Fault
What am I doing wrong?
P.S. the value of the control registers after the error occurred:
CR0=0x80000011 CR2=0x01002580 CR3=0x01004000 CR4=0x00000000

The kernel is placed after 16 MB (at address 0x1000000)
Octocontrabass
Member
Member
Posts: 5563
Joined: Mon Mar 25, 2013 7:01 pm

Re: Can't enable paging

Post by Octocontrabass »

Code: Select all

for (size_t i = 0x1000; i < 0x1400; i++) PageTable[i] = (i * 0x1000) | 3;
I'm pretty sure this code isn't doing what you want. How big is the PageTable array supposed to be?

Code: Select all

PageDirectory[0] = (size_t)PageTable | 3;
Your kernel is loaded above 16MiB, but entry 0 of the page directory only covers virtual addresses between 0 and 4MiB.
Post Reply