Paging
Posted: Sat Jan 21, 2017 7:49 am
I am asking my first question, thus, this may be the wrong place.
I am developing a OS and am stuck, kind of, on virtual memory management aka Paging. I setup a page directory (1024 u32). I also have a array of 1024 page tables - u32[1024]1024] __aligned. I map each page table to a page directory 1 to 1. After enabling paging, I add a page for 0xb8000 and use that address to print to screen but a page fault comes - Page Not Present, Write.
Code -
extern "C" void LoadDirectory(DirectoryEntry*);
extern "C" void EnablePaging(void);
extern "C" {
PageTable KernelMap[1024] __attribute__((aligned(4096)));
DirectoryEntry KernelDirectory[1024] __attribute__((aligned(4096)));
}
PagingBroker::PagingBroker() {
if(!pagingSetup){
int setupIndex = 0;
for( ; setupIndex < 1024; setupIndex++) {
KernelDirectory[setupIndex] = 0x2; // Supervisor, Read-Write, Not Present
}
pagingSetup = true;
}
}
/* Higher Half */
void PagingBroker::setupPaging() {
unsigned int pageIndex;
for(pageIndex = 0; pageIndex < 1024; pageIndex++) {
KernelMap[0][pageIndex] = (pageIndex * 0x1000) | 3;
}
for(pageIndex = 0; pageIndex < 1024; pageIndex++) {
KernelMap[1][pageIndex] = ((pageIndex + 1024) * 0x1000) | 3;
}
KernelDirectory[0] = ((DirectoryEntry) KernelMap[0]) | 3; // Identity Map First 4MB
KernelDirectory[1] = ((DirectoryEntry) KernelMap[1]) | 3; // 8MB
LoadDirectory(KernelDirectory);
EnablePaging();
}void PagingBroker::AddPage(unsigned long virtualAddress, unsigned long physicalAddress, unsigned int pflags) {
unsigned long pdIndex = virtualAddress >> 22;
unsigned long ptIndex = virtualAddress >> 12;
KernelDirectory[pdIndex] = ((DirectoryEntry) KernelDirectory[pdIndex]) | 3;
KernelMap[pdIndex][ptIndex] = physicalAddress | (pflags & 0xfff); // Present
FlushTLB(pdIndex);
}
In my Main() - {
PagingBroker pb;
pb.setupPaging();
pb.AddPage((anything more than 8mb - 'xaddr'), 0xb8000, 3);
char *write = (char*) xaddr;
*write = 'd';
write[1] = 0x7;
}
-------------------------------
I am using GCC and NASM with a linker (ld).
I am developing a OS and am stuck, kind of, on virtual memory management aka Paging. I setup a page directory (1024 u32). I also have a array of 1024 page tables - u32[1024]1024] __aligned. I map each page table to a page directory 1 to 1. After enabling paging, I add a page for 0xb8000 and use that address to print to screen but a page fault comes - Page Not Present, Write.
Code -
extern "C" void LoadDirectory(DirectoryEntry*);
extern "C" void EnablePaging(void);
extern "C" {
PageTable KernelMap[1024] __attribute__((aligned(4096)));
DirectoryEntry KernelDirectory[1024] __attribute__((aligned(4096)));
}
PagingBroker::PagingBroker() {
if(!pagingSetup){
int setupIndex = 0;
for( ; setupIndex < 1024; setupIndex++) {
KernelDirectory[setupIndex] = 0x2; // Supervisor, Read-Write, Not Present
}
pagingSetup = true;
}
}
/* Higher Half */
void PagingBroker::setupPaging() {
unsigned int pageIndex;
for(pageIndex = 0; pageIndex < 1024; pageIndex++) {
KernelMap[0][pageIndex] = (pageIndex * 0x1000) | 3;
}
for(pageIndex = 0; pageIndex < 1024; pageIndex++) {
KernelMap[1][pageIndex] = ((pageIndex + 1024) * 0x1000) | 3;
}
KernelDirectory[0] = ((DirectoryEntry) KernelMap[0]) | 3; // Identity Map First 4MB
KernelDirectory[1] = ((DirectoryEntry) KernelMap[1]) | 3; // 8MB
LoadDirectory(KernelDirectory);
EnablePaging();
}void PagingBroker::AddPage(unsigned long virtualAddress, unsigned long physicalAddress, unsigned int pflags) {
unsigned long pdIndex = virtualAddress >> 22;
unsigned long ptIndex = virtualAddress >> 12;
KernelDirectory[pdIndex] = ((DirectoryEntry) KernelDirectory[pdIndex]) | 3;
KernelMap[pdIndex][ptIndex] = physicalAddress | (pflags & 0xfff); // Present
FlushTLB(pdIndex);
}
In my Main() - {
PagingBroker pb;
pb.setupPaging();
pb.AddPage((anything more than 8mb - 'xaddr'), 0xb8000, 3);
char *write = (char*) xaddr;
*write = 'd';
write[1] = 0x7;
}
-------------------------------
I am using GCC and NASM with a linker (ld).