- 论坛徽章:
- 0
|
本帖最后由 vonnyfly 于 2011-08-31 10:32 编辑
这个在fedora 14上已经不行了,看到网上很多人也是同样的情况,mmap失败,产生总线错误或者core dump。
原因是:
新版的kernel加入了对/dev/mem的限制。
mmap_mem 里面调用了range_is_allowed
drivers/char/mem.c- #ifdef CONFIG_STRICT_DEVMEM
- static inline int range_is_allowed(unsigned long pfn, unsigned long size)
- {
- u64 from = ((u64)pfn) << PAGE_SHIFT;
- u64 to = from + size;
- u64 cursor = from;
- while (cursor < to) {
- if (!devmem_is_allowed(pfn)) {
- printk(KERN_INFO
- "Program %s tried to access /dev/mem between %Lx->%Lx.\n",
- current->comm, from, to);
- return 0;
- }
- cursor += PAGE_SIZE;
- pfn++;
- }
- return 1;
- }
- #else
- static inline int range_is_allowed(unsigned long pfn, unsigned long size)
- {
- return 1;
- }
- #endif
复制代码- /*
- * devmem_is_allowed() checks to see if /dev/mem access to a certain address
- * is valid. The argument is a physical page number.
- *
- *
- * On x86, access has to be given to the first megabyte of ram because that area
- * contains bios code and data regions used by X and dosemu and similar apps.
- * Access has to be given to non-kernel-ram areas as well, these contain the PCI
- * mmio resources as well as potential bios/acpi data regions.
- */
- int devmem_is_allowed(unsigned long pagenr)
- {
- if (pagenr <= 256)
- return 1;
- if (iomem_is_exclusive(pagenr << PAGE_SHIFT))
- return 0;
- if (!page_is_ram(pagenr))
- return 1;
- return 0;
- }
复制代码 而CONFIG_STRICT_DEVMEM可以在arch/x86/Kconfig.debug里面找到- config STRICT_DEVMEM
- bool "Filter access to /dev/mem"
- ---help---
- If this option is disabled, you allow userspace (root) access to all
- of memory, including kernel and userspace memory. Accidental
- access to this is obviously disastrous, but specific access can
- be used by people debugging the kernel. Note that with PAT support
- enabled, even in this case there are restrictions on /dev/mem
- use due to the cache aliasing requirements.
- If this option is switched on, the /dev/mem file only allows
- userspace access to PCI space and the BIOS code and data regions.
- This is sufficient for dosemu and X and all common users of
- /dev/mem.
- If in doubt, say Y.
复制代码 可以看到,比较新的发行版都默认开启的,所以只允许用户空间访问PCI空间和BIOS 代码段和数据段。
参考:http://blog.chinaunix.net/space. ... og&cuid=1932455 |
|