.
分类: LINUX
2012-06-05 22:19:43
/*include\asm-mips\Uaccess.h*/
/*
* copy_from_user: - Copy a block of data from user space.
* @to: Destination address, in kernel space.
* @from: Source address, in user space.
* @n: Number of bytes to copy.
*
* Context: User context only. This function may sleep.
*
* Copy data from user space to kernel space.
*
* Returns number of bytes that could not be copied.
* On success, this will be zero.
*
* If some data could not be copied, this function will pad the copied
* data to the requested size using zero bytes.
*/
#define copy_from_user(to, from, n) \
({ \
void *__cu_to; \
const void __user *__cu_from; \
long __cu_len; \
\
might_sleep(); \
__cu_to = (to); \
__cu_from = (from); \
__cu_len = (n); \
if (access_ok(VERIFY_READ, __cu_from, __cu_len)) \
__cu_len = __invoke_copy_from_user(__cu_to, __cu_from, \
__cu_len); \
__cu_len; \
})
昨天在接触CC2500 Device Driver时,在应用程序(用户空间中)向内核写入一数据时,需要调用copy_from_user来传递,可是发现传不过来...
通过在用户空间获得errno,提示Bad Address
后来发现在使用copy_from_user时,需要对其返回作下判断就可以了...
之前代码:
内核空间:
if(!copy_from_user(tmpbuf, buf, count))
return -EFAULT;
用户空间:
write(CC2500_Fd, buf, count);
printf(" errno = %s\n", strerror(errno));
此时会显示errno = Bad Address,无法向内核空间写入数据,也即无法在内核空间向下进展了
之后代码:
ret = copy_from_user(tmpbuf, buf, count);
if(ret != 0)
return -EFAULT;
用户空间:
write(CC2500_Fd, buf, count);
printf(" errno = %s\n", strerror(errno));
此时显示errno = Success,从用户空间成功的向内核空间内写入数据了