Add paging
This commit is contained in:
parent
1079061353
commit
b4b30fbac6
|
@ -22,6 +22,7 @@ build/early_kernel.o \
|
|||
build/kernel.o \
|
||||
build/vga.o \
|
||||
build/idt.o \
|
||||
build/paging.o \
|
||||
$(CRTEND_OBJ) $(CRTN_OBJ)
|
||||
|
||||
KERNELFILE = build/orion.bin
|
||||
|
@ -41,6 +42,7 @@ all:
|
|||
|
||||
i686-elf-gcc -c vga.c -o build/vga.o $(CFLAGS)
|
||||
i686-elf-gcc -c idt.c -o build/idt.o $(CFLAGS)
|
||||
i686-elf-gcc -c paging.c -o build/paging.o $(CFLAGS)
|
||||
i686-elf-gcc -c kernel.c -o build/kernel.o $(CFLAGS)
|
||||
i686-elf-gcc -c early_kernel.c -o build/early_kernel.o $(CFLAGS)
|
||||
|
||||
|
|
|
@ -1,9 +1,11 @@
|
|||
#include <idt.h>
|
||||
#include <vga.h>
|
||||
#include <paging.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
void early_kernel_main() {
|
||||
terminal_initialize();
|
||||
idt_init();
|
||||
paging_init();
|
||||
}
|
||||
|
|
|
@ -0,0 +1,9 @@
|
|||
#ifndef PAGING_H
|
||||
#define PAGING_H
|
||||
|
||||
#include <idt.h>
|
||||
|
||||
void paging_init();
|
||||
void page_fault(registers_t regs);
|
||||
|
||||
#endif
|
|
@ -0,0 +1,50 @@
|
|||
#include <paging.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
uint32_t page_directory[1024] __attribute__((aligned(4096)));
|
||||
uint32_t first_page_table[1024] __attribute__((aligned(4096)));
|
||||
|
||||
void page_fault(registers_t regs) {
|
||||
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?
|
||||
|
||||
kprintf("Page fault: ( ");
|
||||
if (present) {kprintf("present ");}
|
||||
if (rw) {kprintf("read-only ");}
|
||||
if (us) {kprintf("user-mode ");}
|
||||
if (reserved) {kprintf("reserved ");}
|
||||
kprintf(") at 0x%x\n", faulting_address);
|
||||
kprintf("PAGE FAULT\n");
|
||||
}
|
||||
|
||||
void enable_paging(uint32_t *dir) {
|
||||
__asm__ volatile("mov %0, %%cr3":: "r"(dir));
|
||||
uint32_t cr0;
|
||||
__asm__ volatile("mov %%cr0, %0": "=r"(cr0));
|
||||
cr0 |= 0x80000001; // Enable paging!
|
||||
__asm__ volatile("mov %0, %%cr0":: "r"(cr0));
|
||||
}
|
||||
|
||||
void paging_init() {
|
||||
int i;
|
||||
for(i = 0; i < 1024; i++) {
|
||||
page_directory[i] = 0x00000002;
|
||||
}
|
||||
|
||||
for(i = 0; i < 1024; i++) {
|
||||
first_page_table[i] = (i * 0x1000) | 3; // attributes: supervisor level, read/write, present.
|
||||
}
|
||||
|
||||
page_directory[0] = ((unsigned int)first_page_table) | 3;
|
||||
|
||||
register_interrupt_handler(14, &page_fault);
|
||||
enable_paging(page_directory);
|
||||
}
|
Loading…
Reference in New Issue