Paging

Questions, comments, and suggestions about this site should go here.
Post Reply
SukantPal
Member
Member
Posts: 35
Joined: Sat Jan 21, 2017 7:35 am
Freenode IRC: DefiniteEngineer

Paging

Post by SukantPal »

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).
SukantPal
Member
Member
Posts: 35
Joined: Sat Jan 21, 2017 7:35 am
Freenode IRC: DefiniteEngineer

Re: Paging

Post by SukantPal »

Also, where can I add my own article (not question)
User avatar
dozniak
Member
Member
Posts: 723
Joined: Thu Jul 12, 2012 7:29 am
Location: Tallinn, Estonia

Re: Paging

Post by dozniak »

SukantPal wrote:Also, where can I add my own article (not question)
Article about what?
Learn to read.
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re: Paging

Post by Brendan »

Hi,
SukantPal wrote:I am asking my first question, thus, this may be the wrong place.
Your original post got moved out of "About This Site" and into "OS Development". You can find it here.
SukantPal wrote:Also, where can I add my own article (not question)
It sounds like you want to add an article to the wiki. The best place for that would be the wiki. ;)

Note: As a new member you'll probably want to see the OSDev Wiki access FAQ.


Cheers,

Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
Post Reply