__pa(x) ((x) - __PAGE_OFFSET) ?

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
bohunter11
Posts: 9
Joined: Tue Sep 14, 2010 5:13 pm

__pa(x) ((x) - __PAGE_OFFSET) ?

Post by bohunter11 »

I'm tring to understand this macro. This is from linux and I need some help
in understanding it. Can someone shed some light on this. I know that it
returns the physical address of x and there is a __va(x) sister macro that
returns the virtual address. I just don't understand how it's able to get the
physical or virtual address by adding or subtracting __PAGE_OFFSET witch
in a standard (ie: no PAE) case is 0xc0000000.

Thank You
Bo Hunter
Matthew
Member
Member
Posts: 48
Joined: Wed Jul 01, 2009 11:47 am

Re: __pa(x) ((x) - __PAGE_OFFSET) ?

Post by Matthew »

I'm not terribly familiar with Linux internals but such a macro would indicate to me that the physical memory mapped at virtual address 0xC0000000 begins at physical address 0x0 and is contiguous. In this case it appears that the kernel occupies the beginning of physical memory, and all of it is mapped directly to the upper gigabyte of virtual memory.
User avatar
Owen
Member
Member
Posts: 1700
Joined: Fri Jun 13, 2008 3:21 pm
Location: Cambridge, United Kingdom
Contact:

Re: __pa(x) ((x) - __PAGE_OFFSET) ?

Post by Owen »

Linux has a very broken and hacky memory model where the first N GB of RAM is mapped to the virtual address space starting at a certain address. These macros only work for such addresses.
bohunter11
Posts: 9
Joined: Tue Sep 14, 2010 5:13 pm

Re: __pa(x) ((x) - __PAGE_OFFSET) ?

Post by bohunter11 »

"the first N GB of RAM is mapped to the virtual address space starting at a certain address"

So are you saying that 0xC0000000 is mapped to physical 0 and 0xC0001000 is mapped to physical 0x1000
and ...

Thank You
Bo Hunter
gerryg400
Member
Member
Posts: 1801
Joined: Thu Mar 25, 2010 11:26 pm
Location: Melbourne, Australia

Re: __pa(x) ((x) - __PAGE_OFFSET) ?

Post by gerryg400 »

bohunter11 wrote:"the first N GB of RAM is mapped to the virtual address space starting at a certain address"

So are you saying that 0xC0000000 is mapped to physical 0 and 0xC0001000 is mapped to physical 0x1000
and ...

Thank You
Bo Hunter
Yes. In early Linux kernels when PCs had less than a few hundred MB of RAM the entire of physical memory was mapped into the kernel address space. This means that the kernel can, at any time and regardless of the current memory context, access any memory in the system. This is very handy when for example the memory manager wants to zero a fresh piece of memory to allocate to a process or for accessing the page directory of any process.. Of course problems started when PCs began shipping with GBs of memory and patches were added to the kernel to allow the extra memory to be mapped into the kernel temporarily so that the extra memory could be accessed.

The x86_64 arch of Linux, I believe, maps all of physical memory into the kernel again.

This book gives a quite thorough explanation of Linux memory management. It is a little out of date wrt the current kernel source, but it presents some very good information.

http://ptgmedia.pearsoncmg.com/images/0 ... n_book.pdf
If a trainstation is where trains stop, what is a workstation ?
bohunter11
Posts: 9
Joined: Tue Sep 14, 2010 5:13 pm

Re: __pa(x) ((x) - __PAGE_OFFSET) ?

Post by bohunter11 »

Dam, gerryg400 that's the best anwser I've ever gotten on a forum. Your my new best friend, lol.
I'm at a point in my kernel where I've got paging running and can allocate mem and have a heap.
I nead to figure out this address space layout that windows and linux have. linux with it's 3/1 split
kernel at 3gb and user space at 0-3 gb. I think windows does 2/2 split by default. I just need to layout
an address space and start tring to implement it. At least that what I think I need to do. I need
some kind of guide of the steps of the major steps that need to be done.

Thank You
Bo Hunter
bohunter11
Posts: 9
Joined: Tue Sep 14, 2010 5:13 pm

Re: __pa(x) ((x) - __PAGE_OFFSET) ?

Post by bohunter11 »

Dam again gerryg400, I also want to thank you for the book link. I'm reading it as I write this (well not while I write this).
It's really going to help me make some sense of al this code.

Thank You
Bo Hunter
gerryg400
Member
Member
Posts: 1801
Joined: Thu Mar 25, 2010 11:26 pm
Location: Melbourne, Australia

Re: __pa(x) ((x) - __PAGE_OFFSET) ?

Post by gerryg400 »

Umm, just to make it clear, I didn't actually write that book.
If a trainstation is where trains stop, what is a workstation ?
bohunter11
Posts: 9
Joined: Tue Sep 14, 2010 5:13 pm

Re: __pa(x) ((x) - __PAGE_OFFSET) ?

Post by bohunter11 »

"Umm, just to make it clear, I didn't actually write that book."

Yea, I know. Just greatful for the link.

Thank You
Bo Hunter
Post Reply