Project Home
Project Home
Documents
Documents
Wiki
Wiki
Discussion Forums
Discussions
Project Information
Project Info
Forum Topic - Questions about mmap(): (7 Items)
   
Questions about mmap()  
I've been tackling half a dozen PRs about the docs for mmap(), and I have a few questions (and a much better 
understanding of memory mapping):

1. The header file defines these flags that aren't documented:

    - MAP_PRIVATEANON -- I think this is deprecated in favour of MAP_PRIVATE | MAP_ANON (because you can also have 
shared anonymous memory)

    - MAP_NORESERVE, MAP_RENAME -- I don't think these are used in our code

    - MAP_ELF
    - MAP_BELOW (but MAP_BELOW16M is documented)
    - MAP_SYSRAM

  Should any of these be documented?

2. As far as I can tell, only MAP_PRIVATE, MAP_SHARED, and MAP_FIXED are POSIX. Is this correct, and should the docs 
point this out?

3. POSIX says that mmap() can give an error of ENOTSUP if the implementation doesn't support MAP_FIXED or MAP_PRIVATE, 
or if the implementation doesn't support the given combination of PROT_* flags. I know we support MAP_FIXED and 
MAP_PRIVATE, so that part doesn't apply, but does the second?

4. One of the PRs asks for an example of memory-mapped files. I cooked up one, but I doubt that it's very good (although
 it no longer core dumps). Does anyone have a good (but short) example that could go in the docs?

5. The docs say that msync() might not work on all filesystems. The PR mentions that a filesystem needs to support the 
_IO_MMAP message for memory-mapped files  to work. Does anyone know of any QNX ones that don't support this message?  

Thanks.
RE: Questions about mmap()  
Regarding the flags:

MAP_PRIVATEANON, if used in an mmap call, gets translated to
MAP_PRIVATE|MAP_ANON.  So, no difference. 

MAP_NORESERVE and MAP_RENAME seems to be Sun things -- they have no
effect in our code if specified.  For documentation, maybe just say
they're there for compatibility and have no effect.

MAP_BELOW -- this one looks like it only has an effect on systems with
cache colours, but its not clear what it means from the code.  I'll let
somebody else comment on it...

MAP_ELF is (obviously) used to indicate that the memory is an ELF
object.  Its used by the program loader.  I don't know if it should ever
be used by "normal" programs.  Again, I'll have to let somebody else
comment.

MAP_SYSRAM is used internally (bit of a kludge, really) and will result
in EINVAL if given to the mmap call.

Doug
Re: Questions about mmap()  
On Tue, Dec 16, 2008 at 10:56:48AM -0500, Douglas Bailey wrote:
> MAP_SYSRAM is used internally (bit of a kludge, really) and will result
> in EINVAL if given to the mmap call.

Actually, it's an output only status flag for the DCMD_PROC_MAPINFO, 
DCMD_PROC_PAGEDATA devctl()'s (similar to the PG_* bits). Personally, I
wouldn't have put that flag in the MAP_* constants, but that's where it is.

	Brian

-- 
Brian Stecher (bstecher@qnx.com)        QNX Software Systems
phone: +1 (613) 591-0931 (voice)        175 Terence Matthews Cr.
       +1 (613) 591-3579 (fax)          Kanata, Ontario, Canada K2M 1W8
Re: Questions about mmap()  
Thanks, Doug and Brian, for the information. In the interests of keeping this thread alive, here's what I came up for a 
simple example of memory-mapped files. It reads a section from a text file, prints it, and then maps the same section 
into memory and prints it from there:

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>

#define SOME_OFFSET 100
#define SOME_LEN 50

int main () {
	int fd;
	void *addr;
	char buf[SOME_LEN+1];
		
	fd = open ("/tmp/000-log", O_RDWR);

	/* Read the file into a buffer: */
	
	lseek(fd, SOME_OFFSET, SEEK_SET);
	memset (buf, 0, SOME_LEN+1);
	read(fd, buf, SOME_LEN);
	printf ("Read from the file: \n%s\n\n", buf);

	/* Map the file into memory: */
	
	addr = mmap (NULL, SOME_LEN, PROT_READ|PROT_WRITE, MAP_SHARED,
				 fd, SOME_OFFSET);
	if (addr == MAP_FAILED) {
		perror ("mmap() failed");
	}
	else {
		printf ("From the memory-mapped file: \n%.50s\n", (char *)addr);
	}
	
	return EXIT_SUCCESS;
}

It works, but is it a good example? Any other comments?

RE: Questions about mmap()  
Yeah, it looks perfectly reasonable for a simple example.  You could, of
course, get much more complex if you wanted to...

Doug


-----Original Message-----
From: Steve Reid [mailto:community-noreply@qnx.com] 
Sent: Wednesday, December 17, 2008 10:07 AM
To: ostech-core_os
Subject: Re: Questions about mmap()

Thanks, Doug and Brian, for the information. In the interests of keeping
this thread alive, here's what I came up for a simple example of
memory-mapped files. It reads a section from a text file, prints it, and
then maps the same section into memory and prints it from there:

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>

#define SOME_OFFSET 100
#define SOME_LEN 50

int main () {
	int fd;
	void *addr;
	char buf[SOME_LEN+1];
		
	fd = open ("/tmp/000-log", O_RDWR);

	/* Read the file into a buffer: */
	
	lseek(fd, SOME_OFFSET, SEEK_SET);
	memset (buf, 0, SOME_LEN+1);
	read(fd, buf, SOME_LEN);
	printf ("Read from the file: \n%s\n\n", buf);

	/* Map the file into memory: */
	
	addr = mmap (NULL, SOME_LEN, PROT_READ|PROT_WRITE, MAP_SHARED,
				 fd, SOME_OFFSET);
	if (addr == MAP_FAILED) {
		perror ("mmap() failed");
	}
	else {
		printf ("From the memory-mapped file: \n%.50s\n", (char
*)addr);
	}
	
	return EXIT_SUCCESS;
}

It works, but is it a good example? Any other comments?



_______________________________________________
OSTech
http://community.qnx.com/sf/go/post18821
Re: RE: Questions about mmap()  
Hello,

is it possible to map a file to a specific hardware address ?

E.g by replacing the NULL address in the mmap call:  addr = mmap (NULL, SOME_LEN, PROT_READ|PROT_WRITE, MAP_SHARED,  ??

--Armin



> 
> Yeah, it looks perfectly reasonable for a simple example.  You could, of
> course, get much more complex if you wanted to...
> 
> Doug
> 
> 
> -----Original Message-----
> From: Steve Reid [mailto:community-noreply@qnx.com] 
> Sent: Wednesday, December 17, 2008 10:07 AM
> To: ostech-core_os
> Subject: Re: Questions about mmap()
> 
> Thanks, Doug and Brian, for the information. In the interests of keeping
> this thread alive, here's what I came up for a simple example of
> memory-mapped files. It reads a section from a text file, prints it, and
> then maps the same section into memory and prints it from there:
> 
> #include <sys/types.h>
> #include <sys/stat.h>
> #include <fcntl.h>
> #include <stdio.h>
> #include <stdlib.h>
> #include <string.h>
> #include <sys/mman.h>
> 
> #define SOME_OFFSET 100
> #define SOME_LEN 50
> 
> int main () {
> 	int fd;
> 	void *addr;
> 	char buf[SOME_LEN+1];
> 		
> 	fd = open ("/tmp/000-log", O_RDWR);
> 
> 	/* Read the file into a buffer: */
> 	
> 	lseek(fd, SOME_OFFSET, SEEK_SET);
> 	memset (buf, 0, SOME_LEN+1);
> 	read(fd, buf, SOME_LEN);
> 	printf ("Read from the file: \n%s\n\n", buf);
> 
> 	/* Map the file into memory: */
> 	
> 	addr = mmap (NULL, SOME_LEN, PROT_READ|PROT_WRITE, MAP_SHARED,
> 				 fd, SOME_OFFSET);
> 	if (addr == MAP_FAILED) {
> 		perror ("mmap() failed");
> 	}
> 	else {
> 		printf ("From the memory-mapped file: \n%.50s\n", (char
> *)addr);
> 	}
> 	
> 	return EXIT_SUCCESS;
> }
> 
> It works, but is it a good example? Any other comments?
> 
> 
> 
> _______________________________________________
> OSTech
> http://community.qnx.com/sf/go/post18821


Re: Questions about mmap()  
To do that, create a shared memory object, then use shm_ctl() with SHMCTL_PHYS to associate the memory object with
a specific physical location.

Or, in your bsp setup a typed memory name and use the posix typed memory api

Armin Steinhoff wrote:
> Hello,
> 
> is it possible to map a file to a specific hardware address ?
> 
> E.g by replacing the NULL address in the mmap call:  addr = mmap (NULL, SOME_LEN, PROT_READ|PROT_WRITE, MAP_SHARED,  ?
?
> 
> --Armin
> 
> 
> 
>> Yeah, it looks perfectly reasonable for a simple example.  You could, of
>> course, get much more complex if you wanted to...
>>
>> Doug
>>
>>
>> -----Original Message-----
>> From: Steve Reid [mailto:community-noreply@qnx.com] 
>> Sent: Wednesday, December 17, 2008 10:07 AM
>> To: ostech-core_os
>> Subject: Re: Questions about mmap()
>>
>> Thanks, Doug and Brian, for the information. In the interests of keeping
>> this thread alive, here's what I came up for a simple example of
>> memory-mapped files. It reads a section from a text file, prints it, and
>> then maps the same section into memory and prints it from there:
>>
>> #include <sys/types.h>
>> #include <sys/stat.h>
>> #include <fcntl.h>
>> #include <stdio.h>
>> #include <stdlib.h>
>> #include <string.h>
>> #include <sys/mman.h>
>>
>> #define SOME_OFFSET 100
>> #define SOME_LEN 50
>>
>> int main () {
>> 	int fd;
>> 	void *addr;
>> 	char buf[SOME_LEN+1];
>> 		
>> 	fd = open ("/tmp/000-log", O_RDWR);
>>
>> 	/* Read the file into a buffer: */
>> 	
>> 	lseek(fd, SOME_OFFSET, SEEK_SET);
>> 	memset (buf, 0, SOME_LEN+1);
>> 	read(fd, buf, SOME_LEN);
>> 	printf ("Read from the file: \n%s\n\n", buf);
>>
>> 	/* Map the file into memory: */
>> 	
>> 	addr = mmap (NULL, SOME_LEN, PROT_READ|PROT_WRITE, MAP_SHARED,
>> 				 fd, SOME_OFFSET);
>> 	if (addr == MAP_FAILED) {
>> 		perror ("mmap() failed");
>> 	}
>> 	else {
>> 		printf ("From the memory-mapped file: \n%.50s\n", (char
>> *)addr);
>> 	}
>> 	
>> 	return EXIT_SUCCESS;
>> }
>>
>> It works, but is it a good example? Any other comments?
>>
>>
>>
>> _______________________________________________
>> OSTech
>> http://community.qnx.com/sf/go/post18821
> 
> 
> 
> 
> _______________________________________________
> OSTech
> http://community.qnx.com/sf/go/post28799
> 

-- 
cburgess@qnx.com