64 lines
2.1 KiB
C
64 lines
2.1 KiB
C
#include <stdio.h>
|
|
|
|
/*
|
|
Handler for page faults.
|
|
*/
|
|
void page_fault(registers_t regs){
|
|
// A page fault has occurred.
|
|
// The faulting address is stored in the CR2 register.
|
|
uint32_t faulting_address;
|
|
asm volatile("mov %%cr2, %0" : "=r" (faulting_address));
|
|
|
|
// The error code gives us details of what happened.
|
|
int present = !(regs.err_code & 0x1); // Page not present
|
|
int rw = regs.err_code & 0x2; // Write operation?
|
|
int us = regs.err_code & 0x4; // Processor was in user-mode?
|
|
int reserved = regs.err_code & 0x8; // Overwritten CPU-reserved bits of page entry?
|
|
int id = regs.err_code & 0x10; // Caused by an instruction fetch?
|
|
|
|
// Output an error message.
|
|
printf("Page fault: ( ");
|
|
if (present) {printf("present ");}
|
|
if (rw) {printf("read-only ");}
|
|
if (us) {printf("user-mode ");}
|
|
if (reserved) {printf("reserved ");}
|
|
printf(") at 0x%c\n", faulting_address); // FIXME: fix %c to %X
|
|
printf("PAGE FAULT\n");
|
|
}
|
|
|
|
|
|
uint32_t page_directory[1024] __attribute__((aligned(4096)));
|
|
uint32_t first_page_table[1024] __attribute__((aligned(4096)));
|
|
|
|
extern void loadPageDirectory(unsigned int*);
|
|
extern void enable_paging();
|
|
|
|
void init_paging() {
|
|
//set each entry to not present
|
|
int i;
|
|
for(i = 0; i < 1024; i++)
|
|
{
|
|
// This sets the following flags to the pages:
|
|
// Supervisor: Only kernel-mode can access them
|
|
// Write Enabled: It can be both read from and written to
|
|
// Not Present: The page table is not present
|
|
page_directory[i] = 0x00000002;
|
|
}
|
|
|
|
//we will fill all 1024 entries in the table, mapping 4 megabytes
|
|
for(i = 0; i < 1024; i++)
|
|
{
|
|
// As the address is page aligned, it will always leave 12 bits zeroed.
|
|
// Those bits are used by the attributes ;)
|
|
first_page_table[i] = (i * 0x1000) | 3; // attributes: supervisor level, read/write, present.
|
|
}
|
|
|
|
// attributes: supervisor level, read/write, present
|
|
page_directory[0] = ((unsigned int)first_page_table) | 3;
|
|
|
|
register_interrupt_handler(14, &page_fault);
|
|
|
|
loadPageDirectory(page_directory);
|
|
enable_paging();
|
|
}
|