-   Linux - Hardware (
-   -   Newbie question about usage of mmap (

justin8086 11-22-2005 10:29 AM

Newbie question about usage of mmap
Hi there,

I have a PCI device and I tried to write a short code to access it in user space with mmap.

first I "cat /proc/bus/pci/devices" and get the device's address: 0x00001800.

then I write the following code:

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

int main(void)
void *map;
int fd;
int src = 0;
ulong base;
ulong length;

base = 0x00001800;
length = 0xFF;

map = mmap(NULL, (size_t)length, (PROT_READ | PROT_WRITE), MAP_SHARED, fd,(off_t)base);
if (map == MAP_FAILED)
printf("map failed: %s\n",strerror(errno));

memcpy(map+0x85,&src, 1);

return 0;

I run the code and got the error says that "Invalid Argument", I guess that is because of some misuse of mmap, but where is the error?



ioerror 11-22-2005 01:10 PM

The offset (the last arg to mmap) must be multiple of the page size (4K on x86, 8K on 64-bit archs). Try just using an offset of zero and taking the 0x1800 into account when accessing the memory.
