UIO设备上的mmap EINVAL错误[英] mmap EINVAL error on UIO device

本文是小编为大家收集整理的关于UIO设备上的mmap EINVAL错误的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我在尝试使用UIO后而不是直接映射/dev/mem之后,在Xilinx Zynq上映射物理内存很难.虽然计划将应用程序作为普通用户而不是root运行,但仍以root.

运行.

显然,第一个映射是成功的,而其余的对同一文件描述符12(/dev/uio/ps2pl)失败.虽然明显的差异是偏移量,但它在范围内(请参阅设备树),并且页面对齐.此应用程序与/dev/mem.

合作良好

通过使用strace运行观察到的错误是:

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

加载到内核的设备树:

# /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>;
};

UIO映射的大小足够大,可以容纳上述mmap尺寸和偏移:

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

推荐答案

mmap对于/dev/mem而言,对于UIO设备而言,偏移量的处理方式不同. 不可能使用任意偏移,而只能映射每个区域的开始.上面的示例仅在设备树中定义一个区域,但可以定义多个区域:

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

访问每个区域/映射并不明显,如下所述: https://lwn.net/articles/232575/

访问n -th区域的偏移应为:

n * sysconf(_SC_PAGESIZE)

手臂上的页面大小为12位窗口0x1000.

一些更通用的文档. >

本文地址:https://www.itbaoku.cn/post/2090982.html

问题描述

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

推荐答案

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