Find the answer to your Linux question:
Results 1 to 6 of 6
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 ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Nov 2005
    Posts
    3

    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;

    fd = open("/dev/mem", O_RDWR);
    printf("%d\n",fd);
    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));
    exit(1);
    }

    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?

    TIA

    justin

  2. #2
    Linux Guru lakerdonald's Avatar
    Join Date
    Jun 2004
    Location
    St. Petersburg, FL
    Posts
    5,035
    Can you give an exact cut/paste of the error?

  3. #3
    Just Joined!
    Join Date
    Nov 2005
    Posts
    3
    as i described, the error message is

    map failed: Invalid Argument

    Thank you for reply

  4. $spacer_open
    $spacer_close
  5. #4
    Linux Guru lakerdonald's Avatar
    Join Date
    Jun 2004
    Location
    St. Petersburg, FL
    Posts
    5,035
    According to the manpage for mmap(2), EINVAL is returned when the start, length, or offset is incorrect, so you might want to check on those values

  6. #5
    Just Joined!
    Join Date
    Nov 2005
    Posts
    3
    thanks again.
    yeah, i know because it says the argument is invalid. what is more, since 'start' is always NULL, i guess this is not the problem. then the 'bad guy' should hide in length or offset, right?

    if i make length very small (for instance, 1) and still get the same error, it must be the problem in offset, right?

    Thank you for being patient and replying so fast...

  7. #6
    Linux Guru lakerdonald's Avatar
    Join Date
    Jun 2004
    Location
    St. Petersburg, FL
    Posts
    5,035
    I suppose so.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •