Page 1 of 1

Understanding paging

Posted: Sun Jul 03, 2016 8:31 am
by Sidneyy
Hi,

I've found your wiki very helpful, I've had a couple of attempts at making an OS (purely to understand how it works) and it's been fun so far.

I've had two attempts at getting trying to get paging working, and I'm not quite sure I've understood it properly. I've looked through the wiki and have looked at other resources online, as well as having searched the forum, too.

The way I understand it is:
* A page directory is created and instructions are called to make it clear where the page directory is in the memory.
* 1024 page tables fall within the page directory.
* Each page table contains an address, which refers to a physical address in memory (starting from a 4KB aligned block) for which the virtual address corresponds to.

Right?

So, from what I understand, in theory, page directory 0, page table 0, is virtual address 0x0 to 0x1000 ?
If page directory 0/page table 0 referred to, say, 0x999000, then:

Code: Select all

char *b =0x999000;
*b = 'a';

// ... Paging is enabled here and maps 0x0 to 0x999000

char *a = 0x0;
From what I understand, *a should equal 'a'. But in my code it doesn't. Is there something fundamental that I have missed here? (I have deliberately not attached any code just to check that I actually understand right!)

Any help is appreciated.

Thanks.

Re: Understanding paging

Posted: Sun Jul 03, 2016 8:57 am
by Octocontrabass
CR3 points to the page directory. The page directory contains 1024 page directory entries. Each page directory entry points to a page table. Each page table contains 1024 page table entries. Each page table entry points to a 4kB block of physical memory. (You can also have some PDEs and PTEs that are marked invalid, if you don't want to map the entire virtual address space to physical addresses.) All of these are aligned to 4kB.

It sounds like you've got the right idea, although your use of the terminology is wrong.

Your example code contains a null pointer access, which might be optimized to something you don't expect. You may also need the volatile keyword, depending on how much the compiler is allowed to reorder things.

Re: Understanding paging

Posted: Sun Jul 03, 2016 9:04 am
by glauxosdever
Hi,


You didn't specify any architecture, so I'll presume you mean x86 (32-bit).

The cr3 register holds the physical address of the page directory. The page directory contains 1024 entries, each one holding the physical address of one page table. Each page table contains 1024 entries, each one holding the physical address of one page. Each page is 4096 bytes long.

The first entry of the first page table described in the page directory corresponds to virtual address 0x00000000.
The second entry of the first page table described in the page directory corresponds to virtual address 0x00001000.
The third entry of the first page table described in the page directory corresponds to virtual address 0x00002000.
...
The 1024th entry of the first page table described in the page directory corresponds to virtual address 0x003FF000.

The first entry of the second page table described in the page directory corresponds to virtual address 0x00400000.
The second entry of the second page table described in the page directory corresponds to virtual address 0x00401000.
The third entry of the second page table described in the page directory corresponds to virtual address 0x00402000.
...
The 1024th entry of the second page table described in the page directory corresponds to virtual address 0x007FF000.

The first entry of the third page table described in the page directory corresponds to virtual address 0x00800000.
The second entry of the third page table described in the page directory corresponds to virtual address 0x00801000.
The third entry of the third page table described in the page directory corresponds to virtual address 0x00802000.
...
The 1024th entry of the third page table described in the page directory corresponds to virtual address 0x00BFF000.

...

The first entry of the 1024th page table described in the page directory corresponds to virtual address 0xFFC00000.
The second entry of the 1024th page table described in the page directory corresponds to virtual address 0xFFC01000.
The third entry of the 1024th page table described in the page directory corresponds to virtual address 0xFFC02000.
...
The 1024th entry of the 1024th page table described in the page directory corresponds to virtual address 0xFFFFF000.


Also, it is not our wiki. The wiki is here for everyone.


Hope this helps. :)


Regards,
glauxosdever

Re: Understanding paging

Posted: Sun Jul 03, 2016 9:18 am
by onlyonemac
What others have said is correct, but I want to add that you should probably use assembly language for testing your paging setup (you can always migrate your code to C later if you really want to) and when setting up paging in your kernel you should probably use assembly language there as well (but once you've got paging enabled it's probably simpler to use C to manipulate the table entries, because you can define a C struct that describes each entry). You could also use inline assembly.

Re: Understanding paging

Posted: Mon Jul 04, 2016 1:11 pm
by Sidneyy
Thanks all, this helped me to get this fixed.
I managed to get paging working eventually.

Thanks.