前面的一篇文章中简单的描写叙述了一下内存映射的内容,http://blog.csdn.net/codectq/article/details/25658813,这篇文章作为用户把内存规划好之后,在用户空间使用IOCTL对设备进行控制时的经常使用函数的代码摘录。兴许我会把这部分完好起来。
#ifdefCONFIG_MMU
externunsigned long __must_check __copy_from_user(void *to, const void __user *from,unsigned long n);
externunsigned long __must_check __copy_to_user(void __user *to, const void *from,unsigned long n);
externunsigned long __must_check __copy_to_user_std(void __user *to, const void*from, unsigned long n);
externunsigned long __must_check __clear_user(void __user *addr, unsigned long n);
externunsigned long __must_check __clear_user_std(void __user *addr, unsigned longn);
#else
#define__copy_from_user(to,from,n)
(memcpy(to, (void __force *)from, n),0)
#define__copy_to_user(to,from,n)
(memcpy((void __force *)to, from, n),0)
#define__clear_user(addr,n)
(memset((void __force *)addr, 0, n), 0)
#endif
externunsigned long __must_check __strncpy_from_user(char *to, const char __user*from, unsigned long count);
externunsigned long __must_check __strnlen_user(const char __user *s, long n);
staticinline unsigned long __must_check copy_from_user(void *to, const void __user*from, unsigned long n)
{
if (access_ok(VERIFY_READ, from, n))
n = __copy_from_user(to, from, n);
else /* security hole - plug it */
memset(to, 0, n);
return n;
}
staticinline unsigned long __must_check copy_to_user(void __user *to, const void*from, unsigned long n)
{
if (access_ok(VERIFY_WRITE, to, n))
n = __copy_to_user(to, from, n);
return n;
}
在新的内核中,改动了这部分代码。部分网上可以查到的资料还一直以非常老的内核版本号在说明。实在受不了有些对新内核解说的文章不负责任的加入链接。
./arch/arm/lib/uaccess.S:288:/*Prototype: unsigned long __copy_from_user(void *to,const void *from,unsignedlong n);
./arch/arm/lib/uaccess.S:307:ENTRY(__copy_from_user)
./arch/arm/lib/uaccess.S:547:ENDPROC(__copy_from_user)
copy_to_user与mmap的工作原理
copy_to_user在每次拷贝时须要检測指针的合法性,也就是用户空间的指针所指向的地址的确是一段该进程本身的地址,而不是指向了不属于它的地方,并且每次都会拷贝一次数据,频繁訪问内存,因为虚拟地址连续,物理地址不一定会连续,从而造成CPU的CACHE频繁失效,从而使速度减少