linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* mmap() to a specific address
@ 2002-07-01 19:03 Elias Athanasopoulos
  2002-07-01 19:22 ` Glynn Clements
  2002-07-01 19:36 ` Mehran Rezaei
  0 siblings, 2 replies; 4+ messages in thread
From: Elias Athanasopoulos @ 2002-07-01 19:03 UTC (permalink / raw)
  To: linux-c-programming

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

-- 
http://gnewtellium.sourceforge.net			MP3 is not a crime.	

^ 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-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

* Re: mmap() to a specific address
  2002-07-01 19:22 ` Glynn Clements
@ 2002-07-02 18:42   ` Elias Athanasopoulos
  0 siblings, 0 replies; 4+ messages in thread
From: Elias Athanasopoulos @ 2002-07-02 18:42 UTC (permalink / raw)
  To: Glynn Clements; +Cc: linux-c-programming, Mehran Rezaei

Hi Glynn, Mehran,

On Mon, Jul 01, 2002 at 08:22:47PM +0100, Glynn Clements wrote:
> If you specify MAP_FIXED, either the segment will be mapped at the
> specified address, or mmap() will fail.

Thanks for your replies. Actually I have tried MAP_FIXED but it doesn't
help since I want to map a file in a memory area which most probably is
occupied from the running executable. So, it's 100% a redesign issue on
my side. :-)

Just for the record: what I try to do is to run a static elf from user-
land, without using execve() and friends.

Elias

-- 
http://gnewtellium.sourceforge.net			MP3 is not a crime.	

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2002-07-02 18:42 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).