OSDev.org
https://forum.osdev.org/

Paging
https://forum.osdev.org/viewtopic.php?f=6&t=31281
Page 1 of 1

Author:  SukantPal [ Sat Jan 21, 2017 7:49 am ]
Post subject:  Paging

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).

Author:  SukantPal [ Sat Jan 21, 2017 7:52 am ]
Post subject:  Re: Paging

Also, where can I add my own article (not question)

Author:  dozniak [ Sat Jan 21, 2017 9:35 am ]
Post subject:  Re: Paging

SukantPal wrote:
Also, where can I add my own article (not question)


Article about what?

Author:  Brendan [ Sat Jan 21, 2017 10:05 am ]
Post subject:  Re: Paging

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

Page 1 of 1 All times are UTC - 6 hours
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/