mmap invalid argument for uio0 device

1.3k views Asked by At

I configured my host system so that PCI devices(here ethernet controller) are available to drivers running in userspace. I have bound my ethernet controller to UIO_PCI_GENERIC and trying to map the device memory in userspace using mmap(). I am running this code on a VM.

     #define MAPPED_SIZE (4*1024) //place the size here
     #define DDR_RAM_PHYS (0)  //place the physical address here
     int main(int argc, char const *argv[])
     {
        int fduio;
        int *map = NULL;
        const char uioDevice[] = "/dev/uio0";

        /* open /dev/uio and error checking */
        fduio = open( uioDevice, O_RDWR | O_SYNC );
        if(fduio < 0)
        {
            printf("Failed to open the /dev/uio0 !\n");
            return 0;
        }
        else{
            printf("open /dev/uio0 successfully !\n");
        }
        /* mmap() the opened /dev/uio */
        map=(int*)mmap(0,MAPPED_SIZE,PROT_READ|PROT_WRITE,MAP_SHARED,fduio,DDR_RAM_PHYS);
        if (map == MAP_FAILED)
        {
            perror("mmap");
        }
        /* use 'map' pointer to access the mapped area! */
        for(int i=0;i<100;i++)
        printf("content: 0x%x\n",*(map+i));
        /* unmap the area & error checking */
        if(munmap(map,MAPPED_SIZE)==-1)
        {
            perror("Error un-mmapping the file");
        }
        /* close the character device */
        close(fduio);
        return 0;
      }

OUTPUT:

    open /dev/uio0 successfully !
    mmap : invalid argument
    Segmentation fault (core dumped)

What am i doing wrong? Is it because of offset? when i run this same code for /dev/mem it runs perfectly? Are there any special configuration to be made for doing it for UIO devices?

0

There are 0 answers