Discussion:
How to map to Physical Address by using mmap() in Linux
(too old to reply)
khadar
2008-12-18 14:08:53 UTC
Permalink
Hi,
I want to use mmap() system call from User space to map the user
space file(/dev/mem) to memory and write some file at that address. I
have to write the file at Physical address 0x7000000. I have 128MB of
SDRAM memory.My task is to load file(hardware engine at Physical Addr
0x7000000 ).
But by mmap() what we put in arg1 is start address ,this is Virtual
address.

So how to convert Physical (0x7000000) to Virtual Address.

After Linux booting the linux kernel , i have to use this.
I am using 2.6.24 FC-7 , and board has 128 MB SD RAM.

Please help me , if anybody knows.

ThanQ
Josef Moellers
2008-12-18 14:36:22 UTC
Permalink
Post by khadar
Hi,
I want to use mmap() system call from User space to map the user
space file(/dev/mem) to memory and write some file at that address. I
have to write the file at Physical address 0x7000000. I have 128MB of
SDRAM memory.My task is to load file(hardware engine at Physical Addr
0x7000000 ).
But by mmap() what we put in arg1 is start address ,this is Virtual
address.
So how to convert Physical (0x7000000) to Virtual Address.
That would become the file offset into /dev/mem:

fd = open("/dev/mem", O_RDWR);
ptr = mmap(NULL, 128*1024*1024, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd,
0x7000000);

HTH,

Josef
--
These are my personal views and not those of Fujitsu Siemens Computers!
Josef Möllers (Pinguinpfleger bei FSC)
If failure had no penalty success would not be a prize (T. Pratchett)
Company Details: http://www.fujitsu-siemens.com/imprint.html
khadar
2008-12-19 09:47:00 UTC
Permalink
Hi,
Thanks for your reply.

What I want is, after linux kernel booted up , I want to write a
file at 0x7000000 (This is physical address).
We can not access Physical Address from User space.
I am using 128MB RAM out of this some is consumed for kernel
lremaining is 123MB of free space.

I want to write 1 or 2 MB of file at specified Physical location (in
Free RAM location)
That location is 0x7000000.

By using mmap(), we can map our file but not to physical address but
mapped address is Virtual..

So how to convert that Virtual equivalent of Physical Address(i.e.
0x7000000).

If it possible to calculate Physical Address in User space by
available virtual address.

If u know ?please clarify...

ThakQ
Post by Josef Moellers
Post by khadar
Hi,
I want to use mmap() system call from User space to map the user
space file(/dev/mem) to memory and write some file at that address. I
have to write the file at Physical address 0x7000000. I have 128MB of
SDRAM memory.My task is to load file(hardware engine at Physical Addr
0x7000000 ).
But by mmap() what we put in arg1 is start address ,this is Virtual
address.
So how to convert Physical (0x7000000) to Virtual Address.
fd = open("/dev/mem", O_RDWR);
ptr = mmap(NULL, 128*1024*1024, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd,
0x7000000);
HTH,
Josef
--
These are my personal views and not those of Fujitsu Siemens Computers!
Josef M�llers (Pinguinpfleger bei FSC)
If failure had no penalty success would not be a prize (T. Pratchett)
Company Details: http://www.fujitsu-siemens.com/imprint.html
Josef Moellers
2008-12-19 10:51:17 UTC
Permalink
Post by khadar
Hi,
Thanks for your reply.
What I want is, after linux kernel booted up , I want to write a
file at 0x7000000 (This is physical address).
We can not access Physical Address from User space.
I am using 128MB RAM out of this some is consumed for kernel
lremaining is 123MB of free space.
I want to write 1 or 2 MB of file at specified Physical location (in
Free RAM location)
That location is 0x7000000.
By using mmap(), we can map our file but not to physical address but
mapped address is Virtual..
So how to convert that Virtual equivalent of Physical Address(i.e.
0x7000000).
If it possible to calculate Physical Address in User space by
available virtual address.
If u know ?please clarify...
Your application can only write to virtual addresses. The memory
management unit (MMU) then translates these virtual addresses into
physical addresses. This mapping is set up by the kernel and usually the
kernel choses physical addresses which suit itself.

In order to access a specific physical address, rather than a physical
address chosen by the kernel, you have to instruct the kernel to setup
this mapping.

This can be done by requesting to mmap a specific section of the
pseudo-device file "/dev/mem" (which is a means to access physical
memory locations) to a virtual address in your process' address space.

I gave you the required call in my response.

Josef
--
Mails please to josef dot moellers
and I'm on gmx dot de.
Nate Eldredge
2008-12-19 17:55:56 UTC
Permalink
Post by khadar
Hi,
Thanks for your reply.
What I want is, after linux kernel booted up , I want to write a
file at 0x7000000 (This is physical address).
We can not access Physical Address from User space.
I am using 128MB RAM out of this some is consumed for kernel
lremaining is 123MB of free space.
I want to write 1 or 2 MB of file at specified Physical location (in
Free RAM location)
That location is 0x7000000.
By using mmap(), we can map our file but not to physical address but
mapped address is Virtual..
You can't map other data to a physical address. You can only map other
data to a virtual address.

Perhaps you're thinking something like "since mmap makes data appear at
a virtual address, maybe I can also use it to make data appear at a
physical address". That's not correct. If you want data to appear at
a physical address, you have to write it there.
Post by khadar
So how to convert that Virtual equivalent of Physical Address(i.e.
0x7000000).
If it possible to calculate Physical Address in User space by
available virtual address.
It's not generally possible to ask what physical address corresponds to
an arbitrary virtual address within your program. It could be changing
continually as the kernel reshuffles pages behind your back. There
could even be no physical address associated to a particular virtual
address, if the page in question has been swapped out, or was never
mapped.

You can, however, go the other way, and *tell* the kernel that a certain
physical address should be made to correspond to a certain virtual
address. This is what mmap'ing /dev/mem does. You can (and should)
even leave the virtual address unspecified, and the kernel will choose
one for you that you're not using already.

Therefore I think what you want to do is something like

#define SIZE (2 * 1024 * 1024) /* 2 MB */
buf = malloc(SIZE);
/* read your file into buf */
fd = open("/dev/mem", O_RDWR);
map = mmap(NULL, SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0x7000000);
memcpy(map, buf, SIZE);
Jasen Betts
2008-12-21 03:06:12 UTC
Permalink
Post by khadar
Hi,
Thanks for your reply.
What I want is, after linux kernel booted up , I want to write a
file at 0x7000000 (This is physical address).
We can not access Physical Address from User space.
I am using 128MB RAM out of this some is consumed for kernel
lremaining is 123MB of free space.
I want to write 1 or 2 MB of file at specified Physical location (in
Free RAM location)
That location is 0x7000000.
By using mmap(), we can map our file but not to physical address but
mapped address is Virtual..
mmap does not "write files to memory". it associates virual address
space with disk files.
Post by khadar
So how to convert that Virtual equivalent of Physical Address(i.e.
0x7000000).
can't.
Post by khadar
If it possible to calculate Physical Address in User space by
available virtual address.
probably.
Post by khadar
If u know ?please clarify...
mmap the apropriate region of /dev/mem into your applications virtual
address space, then read the files contents and write them into
physical ram.
Rainer Weikusat
2008-12-21 11:14:29 UTC
Permalink
[...]
Post by Jasen Betts
Post by khadar
If it possible to calculate Physical Address in User space by
available virtual address.
probably.
Generally, no. The kernel will assign 'physical pages' to processes as
it sees fit and can remove the presently assigned pages at any time in
order to use them for something different and assign a completely
different set to the same set of virtual addresses in future.
khadar
2008-12-24 07:49:30 UTC
Permalink
Post by Rainer Weikusat
[...]
Post by Jasen Betts
Post by khadar
If it possible to calculate Physical Address in User space by
available virtual address.
probably.
Generally, no. The kernel will assign 'physical pages' to processes as
it sees fit and can remove the presently assigned pages at any time in
order to use them for something different and assign a completely
different set to the same set of virtual addresses in future.
Hi !
fd = open("/dev/mem", O_RDWR);
map = mmap(NULL, SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd,
0x7000000);
is working fine. I checked the content through Debugger.Physical
memory contains same content
what i wrote there through application programe..

Thank you very much for all of you.
p***@gmail.com
2016-02-01 19:57:14 UTC
Permalink
It is probably worth mentioning that this technique requires root access to the machine. I think you must be root to open /dev/mem
Continue reading on narkive:
Search results for 'How to map to Physical Address by using mmap() in Linux' (Questions and Answers)
3
replies
what is the importent of filesystem?
started 2008-01-09 03:55:24 UTC
earth day
Loading...