Triple fault when enabling paging
Posted: Sun Feb 14, 2016 6:42 am
Sorry if my code is completely wrong, I'm not familiar with Paging.
Code is triple-faulting on setting bit in CR0.
Here's my code:
Code is triple-faulting on setting bit in CR0.
Here's my code:
Code: Select all
//paging.c
#include "../../../include/arch/i686/paging.h"
#include <stdint.h>
#include <stddef.h>
#include "../../../include/stdio.h"
page_directory_t pgdir[1024] __attribute__((aligned(4096)));
page_table_t pgtbl[1024] __attribute__((aligned(4096)));
void setup_pgdir(page_directory_t pgdir[1024])
{
for(int i=0; i<1024; i++)
{
//Those page directory entries AREN'T USABLE. It just fills all page directory by not present page tables.
pgdir[i].present = 0;
pgdir[i].rw_flag = 1;
pgdir[i].access_lvl = 0;
}
}
void paging_ident(uint32_t* tbl)
{
size_t size=4096*1024;
uint32_t from=0x0;
//Code from OSDev.org.
from &= 0xfffff000; // discard bits we don't want
for(;size>0;from+=4096,size-=4096,tbl++)
{
*tbl=from|1; // mark page present.
}
}
void setup_paging()
{
void load_pg_dir(uint32_t*);
void paging_enable(void);
setup_pgdir(pgdir);
paging_ident((uint32_t*)pgtbl);
load_pg_dir((uint32_t*)pgdir);
paging_enable();
}
//paging.h
#ifndef PAGING_H
#define PAGING_H
typedef struct page_directory_t page_directory_t;
typedef struct page_table_t page_table_t;
struct page_directory_t
{
int present : 1;
int rw_flag : 1;
int access_lvl : 1; //0 is for only ring0. 1 is for anybody.
int write_through : 1;
int cache_off : 1;
int accessed : 1;
int zero : 1;
int page_size : 1;
int reserved : 3;
int tbl_addr : 21;
};
struct page_table_t
{
int present : 1;
int rw_flag : 1;
int access_lvl : 1; //0 is for only ring0. 1 is for anybody.
int cache_off : 1;
int accessed : 1;
int dirty : 1;
int zero : 1;
int global : 1;
int reserved : 3;
int tbl_addr : 21;
};
void load_page_directory(page_directory_t);
#endif // PAGING_H
//paging.asm
global load_pg_dir
load_pg_dir:
; Loaging page directory.
push ebp
mov ebp, esp
mov eax, [esp+8]
mov cr3, eax
mov esp, ebp
pop ebp
ret
global paging_enable
paging_enable: ; Enable the paging by setting bit in CR0.
push ebp
mov ebp, esp
mov eax, cr0
or eax, 0x80000000
mov cr0, eax ; Here it crashes, noo :(
mov esp, ebp
pop ebp
ret