mmap内存映射/dev/mem到用户空间 + kernel config

来源:互联网 发布:c语言好考么 编辑:程序博客网 时间:2024/04/27 22:46

1 mmap总是报错:“Operation not permitted”

2 mmap还是返回错误:“Invalid argument”。( or EINVAL)

3 Solution:

4 (to solve issue 1)

在.config文件中设置CONFIG_STRICT_DEVMEM is not set

5 (to solve issue 2)

在.config文件中设置CONFIG_X86_PAT is not set

6 another solution:

7 (substitution of step 5)

PAT can be disabled by adding the "nopat" argument to the kernel command line at boot time.


Ref

1 http://blog.sina.com.cn/s/blog_6f5b220601012xbc.html

2 http://blog.csdn.net/cjsycyl/article/details/8083868

3 http://stackoverflow.com/questions/11891979/accessing-mmaped-dev-mem


Attachment of Ref 3

I think I've found the issue -- it's to do with /dev/mem memory mapping protection on the x86.

Pl refer to this LWN article:"x86: introduce /dev/mem restrictions with a config option"http://lwn.net/Articles/267427/

CONFIG_NONPROMISC_DEVMEM

Now (i tested this on a recent 3.2.21 kernel), the config option seems to be called CONFIG_STRICT_DEVMEM.

I changed my kernel config:

$ grep DEVMEM .config# CONFIG_STRICT_DEVMEM is not set$ 

When the above prg was run with the previous kernel, with CONFIG_STRICT_DEVMEM SET:dmesg shows:

[29537.565599] Program a.out tried to access /dev/mem between 1000000->1001000.[29537.565663] a.out[13575]: segfault at ffffffff ip 080485bd sp bfb8d640 error 4 in a.out[8048000+1000]

This is because of the kernel protection..

When the kernel was rebuilt (with the CONFIG_STRICT_DEVMEM UNSET) and the above prg was run :

# ./a.out mmap failed: Invalid argument# 

This is because the 'offset' parameter is > 1 MB (invalid on x86) (it was 16MB).

After making the mmap offset to be within 1 MB:

# ./a.out addr: 0xb7758000*addr: 138293760 # 

It works!See the above LWN article for details.

On x86 architectures with PAT support (Page Attribute Table), the kernel still prevents the mapping of DRAM regions. The reason for this as mentioned in thekernel source is:

This check is nedded to avoid cache aliasing when PAT is enabled

This check will cause a similar error to the one mentioned above. For example:

Program a.out tried to access /dev/mem between [mem 68200000-68201000].

This restriction can be removed by disabling PAT. PAT can be disabled by adding the "nopat" argument to the kernel command line at boot time.


0 0
原创粉丝点击