getting physical address from virtual and vice versa

If you are new to OS Development, plan on spending some time here first before going into the other forums.

Moderator: Moderators

Post Reply
vjain20
Posts: 12
Joined: Fri Apr 06, 2012 7:24 am

getting physical address from virtual and vice versa

Post by vjain20 » Mon May 28, 2012 7:30 am

Hi,

I am following the OS development tutorial to write my own OS.
I am trying to implement multi-tasking in kernel mode.
I need to have functions to
1. translate virtual address to physical address. - This is required to get the physical
address of page directory lo load in the cr3 register.

2. translate physical address to virtual address - this is required to make
changes to a page table when the physical address of page table is available from the page directory.

What is a good of doing the above functions?

Thanks
Vaibhav Jain

User avatar
Mike
Site Admin
Posts: 464
Joined: Sat Oct 20, 2007 7:58 pm
Contact:

Re: getting physical address from virtual and vice versa

Post by Mike » Tue Jun 12, 2012 12:21 am

Hello,

Translating virtual to physical addresses
In order to translate from a virtual to a physical address, we have to take into consideration that the virtual address itself gives us the PDE and PTE and page offset. With this information we can just look up the frame from the page tables and add the offset to get its physical address.

Translating physical to virtual addresses
It is not possible to translate every virtual address to a physical one because we need to store the mappings our self, taking into consideration multiple virtual addresses can refer to the same physical address. OSs like Linux and Windows have it for kernel mode addresses though; but the way its done is heavily specific to your design. The idea is that if the base of the physical and virtual addresses are known, then it becomes as easy as a simple addition or subtraction to translate.

Page 62 of this book explains one way that this can be done. I can also suggest studying _va and _pa in Linux.

Post Reply