1

I currently try to access UIO shared memory via memcopy.

My approach is:

  1. open the corresponding device
  2. map the memory via mmap with the offset speciality N*getpagesize()
  3. memcopy / memset to the pointer that is returned from mmap

I tried also with ftruncate after Step 2 resulting in an error.

The memcopy / memset causes a bus error which is normaly a sign of writing out of the files boundaries.

Via cat /proc/'pid'/maps I'm able to see that there is a mapping for /dev/uioX Also /sys/class/uio/uioX/maps/ has two map directories, of which I try to access the second one (map1 therefore N = 1)

Am I missing out something? Would I have to mmap the full size of the memory specified in /sys/class/uio/uioX/maps/map1/size ?

I could not find any example for accessing the memory via memcopy, is there something that prevents memcopy on UIO mmaped memory?

Sources

unsigned char* GetMemPtr(const char *name, unsigned long Size)
{
    long fd;
    long truncret;
    void* MemPtr;

    unsigned long offst;

    printf("Opening: %s with size %u\n" , name, Size);

    fd = open(name, O_RDWR);

    if (fd < 0) {
        printf("Error: open : %u : %s\n", fd, strerror(errno));
    }

    offst = 1 * getpagesize();

    /*truncret = ftruncate(fd, offst + Size);
    if (truncret < 0)
    {
        printf("Error: ftruncate : %s : %d\n", strerror(errno), truncret);

    }*/

    MemPtr = mmap(0, Size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, offst);
    if (MemPtr == MAP_FAILED)
    {
        printf("Error: mmap : %p : %s\n", MemPtr, strerror(errno));

    }
    //MemPtr = MemPtr + offst;

    printf("Mem pointer is %p\n", MemPtr);

    memset(MemPtr, 0, Size);

    printf("Pointer is : %p\n" , MemPtr);
    return (unsigned char*) MemPtr;

}

Results in the output:
Opening: /dev/uio0 with size 4096
Mem pointer is 0xffff89232000
Bus error (core dumped)

Community
  • 1
  • 1
uslx
  • 31
  • 4
  • 1
    Welcome to StackOverflow! Please include the code that is causing this error. It's hard to help without a [minimum, reproducible example!](https://stackoverflow.com/help/minimal-reproducible-example) – Nick Reed Oct 01 '19 at 22:58
  • 1
    `MemPtr = ftruncate` - ftruncate returns an int. So what is the size of file named `name`? – KamilCuk Oct 02 '19 at 11:04
  • I changed the return value, thank you! The size of the file is 0. If I include the ftruncate part I recive an error _Error: ftruncate : Invalid argument : -1_ I assume this is because `fd` is not a regular file descriptor (opening the UIO device) ? – uslx Oct 02 '19 at 11:38
  • *The memcopy / memset causes a bus error which is normaly a sign of writing out of the files boundaries.* Offhand, I'd guess the device driver doesn't support `mmap()` access properly. Does "normal" file IO via `open()`/`read()`/`write()` work properly? Even if you use `lseek()`/`pread()`/`pwrite()` to access any offset? This might be relevant: https://forums.xilinx.com/t5/Embedded-Linux/Large-BRAM-access-using-mmap/td-p/664702 along with this: https://stackoverflow.com/questions/41311792/mmap-einval-error-on-uio-device – Andrew Henle Oct 02 '19 at 16:07
  • The UIO system is meant to control memory and interrupts from userspace. [Doc](https://www.kernel.org/doc/html/v4.12/driver-api/uio-howto.html) states that the following file operations should be supported `read()` - blocking read of interrupts (waits till interrupt occures) `select()` - non blocking read of interrupts `mmap()` - _map the device’s memory to userspace_ with `N*getpagesize()` to address different mappings in one device. _(2nd Link)_ I think accessing "normal" files work _(at least `open()` and `mmap()`)_ – uslx Oct 02 '19 at 16:39
  • I don't have enough knowledge to turn in a full answer, but this helped my own case - https://jeffq.com/blog/over-aggressive-gcc-optimization-can-cause-sigbus-crash-when-using-memcpy-with-the-android-ndk/ I ended up using array indexing combined with a memory barrier - https://stackoverflow.com/a/51266471 – paulluap Dec 27 '22 at 06:15

0 Answers0