Page 1 of 1

Paging on 86_64

Posted: Fri Jul 08, 2022 12:51 pm
by SanderR
Hello everyone!

I have a doubt about paging.
I was watching the tutorial made by Poncho OS at https://www.youtube.com/watch?v=e47SApmmx44&t=258s
And my own attempt (including what I have learned from there is: https://github.com/AdeRegt/SanderOS64/b ... s/paging.c
Tutorial link: https://github.com/Absurdponcho/PonchoO ... anager.cpp

My questions are:
  • Do I understand it well that paging is also used to make programs think they are in a memory region while in fact, they are in another region. like, program A is at 0xC00000 and program B is at 0xD00000 but they have a virtual address of 0x4000 ?
  • In the code produced by the tutorial, they give the physical address to the last directory, but this offset is calculated by a script with the input of a virtual address. does this mean you can only assign one physical page per virtual address?
Thank you in advantage!

Re: Paging on 86_64

Posted: Fri Jul 08, 2022 1:02 pm
by Demindiro
SanderR wrote:Do I understand it well that paging is also used to make programs think they are in a memory region while in fact, they are in another region. like, program A is at 0xC00000 and program B is at 0xD00000 but they have a virtual address of 0x4000 ?
In a sense, yes.
SanderR wrote:In the code produced by the tutorial, they give the physical address to the last directory, but this offset is calculated by a script with the input of a virtual address. does this mean you can only assign one physical page per virtual address?
Yes, at least in a single address space. The way paging works on x86_64 is by splitting off 12 bits for the offset in the page and then chunks of 9 bits which acts as an index in each table (node).

e.g. 0x4000 would be split in an offset of 0 and 4 chunks of 4, 0, 0 and 0 respectively. The page walker then loads the physical address from the first table (PML4) at index 0, then twice again at index 0 and finally at index 4.

The diagram of the Paging article visualizes it well

Re: Paging on 86_64

Posted: Fri Jul 08, 2022 1:23 pm
by Octocontrabass
SanderR wrote:Do I understand it well that paging is also used to make programs think they are in a memory region while in fact, they are in another region. like, program A is at 0xC00000 and program B is at 0xD00000 but they have a virtual address of 0x4000 ?
That is indeed one of the things you can do with paging.
SanderR wrote:In the code produced by the tutorial, they give the physical address to the last directory, but this offset is calculated by a script with the input of a virtual address. does this mean you can only assign one physical page per virtual address?
Yes, but don't forget you can change the assignment whenever you like. A typical OS will have separate page tables for each program and update CR3 on each task switch. This is how you can have program A and program B both running at virtual address 0x4000. (This may or may not be possible using the tutorial code - I haven't checked, but it's common for tutorials to simplify things in order to better focus on specific topics.)

Re: Paging on 86_64

Posted: Fri Jul 08, 2022 2:33 pm
by SanderR
Octocontrabass wrote: Yes, but don't forget you can change the assignment whenever you like. A typical OS will have separate page tables for each program and update CR3 on each task switch. This is how you can have program A and program B both running at virtual address 0x4000. (This may or may not be possible using the tutorial code - I haven't checked, but it's common for tutorials to simplify things in order to better focus on specific topics.)
Thank you for this explanation. This part was not mentioned in the tutorial, now I can continue developing it!