[英]Why do i get “Unhandled fault: imprecise external abort” while trying to access shared memory from my kernel module?
I have this in a kernel module: 我在内核模块中有这个:
/*global scope declared*/
static int array[10]={1,2,3,4,5,6,7,8,9,10};
and I have functions for open close read and write works perfectly, i want to share the array[8]
with the user space application in the bottom of this page. 并且我具有用于打开关闭读写操作的功能,我想与本页底部的用户空间应用程序共享
array[8]
。
in the kernel module: 在内核模块中:
static int *my_mmap (struct file *filep, struct vm_area_struct *vma ) {
if (remap_pfn_range(vma,
vma->vm_start,
virt_to_phys(array)>> PAGE_SHIFT,
10,
vma->vm_page_prot) < 0) {
printk("remap_pfn_range failed\n");
return -EIO;
}
return 0;
the application in user-space's source code: 用户空间源代码中的应用程序:
#define LEN (64*1024)
/* prototype for thread routine */
#define FILE_PATH "/dev/my_module"
int main()
{
int i=0;
int fd = open(FILE_PATH,O_RDWR);
int* vadr = mmap(0, LEN, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
for (i=0;i<10;++i){
printf("%d",*(vadr+i));
}
return 0;
}
This is wrong on so many levels I don't know where to even start :-) 在很多级别上这都是错误的,我什至不知道从哪里开始:-)
There are probably more problems, but this is what pops to mind. 可能还有更多问题,但这是我想到的。
The right thing to do is not share data between kernel and user space, but copy it over using copy_to_user and friends, unless you really know what you are doing and why. 正确的做法不是在内核空间和用户空间之间共享数据,而是使用copy_to_user和好友复制数据,除非您真的知道自己在做什么以及为什么。
If you really must share the memory, then allocate a free page, map it from kernel space (eg kmap) and from user space (like you did) and hope that your platform doesn't have a VIPT cache. 如果您确实必须共享内存,则分配一个空闲页面,从内核空间(例如kmap)和用户空间(就像您一样)映射它,并希望您的平台没有VIPT缓存。
I'm a relative newcomer to kernel programming so I may be missing something but isn't it possible for you to use copy_to_user in this case? 我是内核编程的一个相对较新的人,因此我可能会缺少一些东西,但是在这种情况下您是否可以使用copy_to_user?
unsigned long copy_to_user(void __user * to, const void * from, unsigned long n);
In brief 简单来说
to = address in userspace (destination)
from = address in kernel (source)
n = number of bytes to copy
您的数组是char数组,但是您的用户空间程序正在将其作为int数组进行访问。
Refer to Implementing mmap for transferring data from user space to kernel space for a guide on how to share data between kernel and user space properly. 有关如何在内核空间和用户空间之间正确共享数据的指南,请参阅实现将mmap从用户空间传输到内核空间的mmap 。 I have it implemented in my custom driver and it works fine.
我已经在我的自定义驱动程序中实现了它,并且效果很好。 If you don't need the speed though, using copy_to_user is safer and easier.
如果您不需要速度,则使用copy_to_user更安全,更轻松。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.