* Re: mmap() to a specific address
2002-07-01 19:03 mmap() to a specific address Elias Athanasopoulos
@ 2002-07-01 19:22 ` Glynn Clements
2002-07-02 18:42 ` Elias Athanasopoulos
2002-07-01 19:36 ` Mehran Rezaei
1 sibling, 1 reply; 4+ messages in thread
From: Glynn Clements @ 2002-07-01 19:22 UTC (permalink / raw)
To: Elias Athanasopoulos; +Cc: linux-c-programming
Elias Athanasopoulos wrote:
> Can I force a mmap() to a specific address? As I read in the man page the
> address which is passed in mmap() is only a hint; the kernel will eventual-
> ly decide in which page the object will be mapped. But, is there any ugly
> hack to accomplish a very specific mmap()?
If you specify MAP_FIXED, either the segment will be mapped at the
specified address, or mmap() will fail.
AFAICT, the kernel will use the specified address whenever possible.
If it isn't possible (i.e. because something else is already mapped
there), then, well, it just isn't possible; the kernel can't
unilaterally deallocate an existing mapping.
Also, even if mmap(MAP_FIXED) succeeds, it may result in subsequent
operations failing; e.g. brk() will fail if it would result in the
heap being extended into the mapped region, pthread_create() will fail
if the thread's stack would overlap the mapped region, etc.
--
Glynn Clements <glynn.clements@virgin.net>
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: mmap() to a specific address
2002-07-01 19:03 mmap() to a specific address Elias Athanasopoulos
2002-07-01 19:22 ` Glynn Clements
@ 2002-07-01 19:36 ` Mehran Rezaei
1 sibling, 0 replies; 4+ messages in thread
From: Mehran Rezaei @ 2002-07-01 19:36 UTC (permalink / raw)
To: Elias Athanasopoulos, linux-c-programming
Hello Elias,
You can use MAP_FIXED, and indicate an address. Then the object is mapped in
the address that you wanted.
Following is taken from mmap man page. The first paragraph addresses your
question.
////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////
If MAP_FIXED is set in the flags parameter:
+ If the requested address is not null, the mmap() function succeeds
even if the requested address is already part of another region. (If
the address is within an existing region, the effect on the pages
within that region and within the area of the overlap produced by the
two regions is the same as if they were unmapped. In other words,
whatever is mapped between addr and addr + len will be unmapped.)
+ If the requested address is null and MAP_FIXED is specified, the
region is placed at the default exact mapping address for the region.
It places the region at this value exactly, replacing previous map-
pings if necessary. The exact mapping address is determined from a
combination of the flag and protection parameters passed to the
mmap()
function.
////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////
And here is an example:
void *address=0x4000;
void *test;
test=mmap(address,500,PROT_READ|PROT_WRITE,MAP_FIXED|MAP_PRIVATE,fd,0);
fd should be either a filedes (of existing file) or if you have
MAP_ANONYMOUS coud be "-1". If in your system MAP_ANONYMOUS is not defined
and you want to have unnamed memory region, you can specify it as:
int df = open("/dev/zero",O_RDWR);
Good luck,
Mehran
> Hi all,
>
> Can I force a mmap() to a specific address? As I read in the man page the
> address which is passed in mmap() is only a hint; the kernel will
eventual-
> ly decide in which page the object will be mapped. But, is there any ugly
> hack to accomplish a very specific mmap()?
>
> Elias
>
^ permalink raw reply [flat|nested] 4+ messages in thread