OSDev.org

The Place to Start for Operating System Developers
It is currently Thu Mar 28, 2024 5:01 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: Paging on 86_64
PostPosted: Fri Jul 08, 2022 12:51 pm 
Offline
Member
Member

Joined: Tue Aug 30, 2016 1:31 pm
Posts: 69
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!


Top
 Profile  
 
 Post subject: Re: Paging on 86_64
PostPosted: Fri Jul 08, 2022 1:02 pm 
Offline
Member
Member
User avatar

Joined: Fri Jun 11, 2021 6:02 am
Posts: 96
Location: Belgium
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)


Top
 Profile  
 
 Post subject: Re: Paging on 86_64
PostPosted: Fri Jul 08, 2022 1:23 pm 
Offline
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 5099
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.)


Top
 Profile  
 
 Post subject: Re: Paging on 86_64
PostPosted: Fri Jul 08, 2022 2:33 pm 
Offline
Member
Member

Joined: Tue Aug 30, 2016 1:31 pm
Posts: 69
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!


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot] and 69 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group