免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
楼主: Godbach
打印 上一主题 下一主题

内核和用户空间共享内存的实现例程-proc和mmap [复制链接]

论坛徽章:
0
51 [报告]
发表于 2010-06-08 07:25 |只看该作者
试了一下,完全能成

论坛徽章:
0
52 [报告]
发表于 2010-06-08 12:32 |只看该作者
好东西,值得借鉴

论坛徽章:
36
IT运维版块每日发帖之星
日期:2016-04-10 06:20:00IT运维版块每日发帖之星
日期:2016-04-16 06:20:0015-16赛季CBA联赛之广东
日期:2016-04-16 19:59:32IT运维版块每日发帖之星
日期:2016-04-18 06:20:00IT运维版块每日发帖之星
日期:2016-04-19 06:20:00每日论坛发贴之星
日期:2016-04-19 06:20:00IT运维版块每日发帖之星
日期:2016-04-25 06:20:00IT运维版块每日发帖之星
日期:2016-05-06 06:20:00IT运维版块每日发帖之星
日期:2016-05-08 06:20:00IT运维版块每日发帖之星
日期:2016-05-13 06:20:00IT运维版块每日发帖之星
日期:2016-05-28 06:20:00每日论坛发贴之星
日期:2016-05-28 06:20:00
53 [报告]
发表于 2010-06-09 10:14 |只看该作者
回复 50# snriyt
这个配置就是管理/dev/mem的访问控制权限的。

论坛徽章:
0
54 [报告]
发表于 2011-07-07 14:21 |只看该作者
楼主,我用了你的程序,调试失败了,我觉得是mmap函数中最后一个参数传入有问题,如果传入0,就成功的,我传入的是kernel分配的一个地址,类似于0x76830000,这样mmap返回值就为-1,这是怎么回事呢?

论坛徽章:
0
55 [报告]
发表于 2011-08-24 11:30 |只看该作者
不错,拿来练习练习。

论坛徽章:
0
56 [报告]
发表于 2011-08-31 10:23 |只看该作者
本帖最后由 vonnyfly 于 2011-08-31 10:32 编辑

这个在fedora 14上已经不行了,看到网上很多人也是同样的情况,mmap失败,产生总线错误或者core dump。
原因是:
新版的kernel加入了对/dev/mem的限制。
mmap_mem 里面调用了range_is_allowed

drivers/char/mem.c
  1. #ifdef CONFIG_STRICT_DEVMEM
  2. static inline int range_is_allowed(unsigned long pfn, unsigned long size)
  3. {
  4.         u64 from = ((u64)pfn) << PAGE_SHIFT;
  5.         u64 to = from + size;
  6.         u64 cursor = from;

  7.         while (cursor < to) {
  8.                 if (!devmem_is_allowed(pfn)) {
  9.                         printk(KERN_INFO
  10.                 "Program %s tried to access /dev/mem between %Lx->%Lx.\n",
  11.                                 current->comm, from, to);
  12.                         return 0;
  13.                 }
  14.                 cursor += PAGE_SIZE;
  15.                 pfn++;
  16.         }
  17.         return 1;
  18. }
  19. #else
  20. static inline int range_is_allowed(unsigned long pfn, unsigned long size)
  21. {
  22.         return 1;
  23. }
  24. #endif
复制代码
  1. /*
  2. * devmem_is_allowed() checks to see if /dev/mem access to a certain address
  3. * is valid. The argument is a physical page number.
  4. *
  5. *
  6. * On x86, access has to be given to the first megabyte of ram because that area
  7. * contains bios code and data regions used by X and dosemu and similar apps.
  8. * Access has to be given to non-kernel-ram areas as well, these contain the PCI
  9. * mmio resources as well as potential bios/acpi data regions.
  10. */
  11. int devmem_is_allowed(unsigned long pagenr)
  12. {
  13.         if (pagenr <= 256)
  14.                 return 1;
  15.         if (iomem_is_exclusive(pagenr << PAGE_SHIFT))
  16.                 return 0;
  17.         if (!page_is_ram(pagenr))
  18.                 return 1;
  19.         return 0;
  20. }
复制代码
而CONFIG_STRICT_DEVMEM可以在arch/x86/Kconfig.debug里面找到
  1. config STRICT_DEVMEM
  2.         bool "Filter access to /dev/mem"
  3.         ---help---
  4.           If this option is disabled, you allow userspace (root) access to all
  5.           of memory, including kernel and userspace memory. Accidental
  6.           access to this is obviously disastrous, but specific access can
  7.           be used by people debugging the kernel. Note that with PAT support
  8.           enabled, even in this case there are restrictions on /dev/mem
  9.           use due to the cache aliasing requirements.

  10.           If this option is switched on, the /dev/mem file only allows
  11.           userspace access to PCI space and the BIOS code and data regions.
  12.           This is sufficient for dosemu and X and all common users of
  13.           /dev/mem.

  14.           If in doubt, say Y.
复制代码
可以看到,比较新的发行版都默认开启的,所以只允许用户空间访问PCI空间和BIOS 代码段和数据段。
参考:http://blog.chinaunix.net/space. ... og&cuid=1932455

论坛徽章:
0
57 [报告]
发表于 2012-03-07 10:24 |只看该作者
回复 27# blue006


    请问,该问题解决了吗,我也遇到了同样地问题。

论坛徽章:
0
58 [报告]
发表于 2012-07-17 14:47 |只看该作者
不错的例子

论坛徽章:
0
59 [报告]
发表于 2013-10-08 00:30 |只看该作者
mark一下,看情景分析看的头疼

论坛徽章:
0
60 [报告]
发表于 2013-11-15 18:10 |只看该作者
内核版本没有这个函数create_proc_info_entry,有没有替代的函数呢,一直都没找到
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP