Paging on 86_64

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
SanderR
Member
Member
Posts: 70
Joined: Tue Aug 30, 2016 1:31 pm
Libera.chat IRC: SDR

Paging on 86_64

Post 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!
User avatar
Demindiro
Member
Member
Posts: 96
Joined: Fri Jun 11, 2021 6:02 am
Libera.chat IRC: demindiro
Location: Belgium
Contact:

Re: Paging on 86_64

Post 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
My OS is Norost B (website, Github, sourcehut)
My filesystem is NRFS (Github, sourcehut)
Octocontrabass
Member
Member
Posts: 5563
Joined: Mon Mar 25, 2013 7:01 pm

Re: Paging on 86_64

Post 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.)
SanderR
Member
Member
Posts: 70
Joined: Tue Aug 30, 2016 1:31 pm
Libera.chat IRC: SDR

Re: Paging on 86_64

Post 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!
Post Reply