mmap EINVAL error on UIO device

1.2k views Asked by At

I have trouble mapping physical memory on Xilinx Zynq after attempting to use UIO instead of mapping directly /dev/mem. While the plan is to run the application as a normal user instead of root this is still being run as root.

Apparently the first mapping is successful while the rest done to the same file descriptor 12 (/dev/uio/ps2pl) fail. While the obvious difference is the offset, it is within the range (see device tree) and it is properly page aligned. This application was working well with /dev/mem.

The error observed by running with strace is:

open("/dev/uio/ps2pl", O_RDWR|O_SYNC)   = 12
open("/sys/bus/i2c/devices/0-0050/eeprom", O_RDONLY) = 13
fstat64(13, {st_mode=S_IFREG|0600, st_size=8192, ...}) = 0
_llseek(13, 0, [0], SEEK_SET)           = 0
read(13, "\1\1\0\0\0\0\0\0", 8)         = 8
read(13, "(\\\217\2(\\\217\00233333333\0\0\0\0\0\0\0\0(\\\217\2(\\\217\2"..., 4096) = 4096
close(13)                               = 0
mmap2(NULL, 48, PROT_READ|PROT_WRITE, MAP_SHARED, 12, 0) = 0xb6f93000
mmap2(NULL, 48, PROT_READ|PROT_WRITE, MAP_SHARED, 12, 0x400000) = -1 EINVAL (Invalid argument)
mmap2(NULL, 196608, PROT_READ|PROT_WRITE, MAP_SHARED, 12, 0x200000) = -1 EINVAL (Invalid argument)
mmap2(NULL, 196608, PROT_READ|PROT_WRITE, MAP_SHARED, 12, 0x100000) = -1 EINVAL (Invalid argument)
--- SIGSEGV {si_signo=SIGSEGV, si_code=SEGV_MAPERR, si_addr=0x1f} ---
+++ killed by SIGSEGV +++
Segmentation fault

The device tree as loaded into the kernel:

# /root/dtc/dtc -f -I fs /sys/firmware/devicetree/base/amba_pl/ps2pl\@40000000/
ERROR (name_properties): "name" property in / is incorrect ("ps2pl" instead of base node name)
Warning: Input tree has errors, output forced
/dts-v1/;

/ {
    reg = <0x40000000 0x40000000>;
    name = "ps2pl";
    interrupts = <0x0 0x44 0x4>;
    compatible = "generic-uio";
    interrupt-parent = <0x3>;
};

The size of the UIO mapping is large enough to accommodate the above mmap sizes and offsets:

# cat /sys/devices/soc0/amba_pl/40000000.ps2pl/uio/uio0/maps/map0/size 
0x40000000
1

There are 1 answers

0
Iztok Jeras On

mmap offset is handled differently for /dev/mem then for UIO devices. It is not possible to use an arbitrary offset, instead it is only possible to map the start of each region. The above example only has one region defined in the device tree but it is possible to define multiple regions:

reg = <0x40000000 0x10000>,
      <0x40010000 0x10000>,
      <0x40020000 0x10000>,
      <0x40030000 0x10000>;
reg-names = "region0", "id", "region2", "gpio";

Access to each region/mapping is not obvious, as described here: https://lwn.net/Articles/232575/

The offset for accessing the n-th regions should be:

n * sysconf(_SC_PAGESIZE)

On arm the page size is a 12 bit window 0x1000.

Some more generic documentation. http://elinux.org/images/b/b0/Uio080417celfelc08.pdf