阅读《Linux Kernel Delelop》时遇到的问题

以下是讲述系统调用的实现的所给的例子:
/*
* silly_copy - utterly worthless syscall that copies the len bytes from
* 'src' to 'dst' using the kernel as an intermediary in the copy for no
* good reason.  But it makes for a good example!
*/
asmlinkage long sys_silly_copy(unsigned long *src,
                               unsigned long *dst,
                               unsigned long len)
{
        unsigned long buf;

       /* fail if the kernel wordsize and user wordsize do not match */
        if (len != sizeof(buf))
                return -EINVAL;

        /* copy src, which is in the user's address space, into buf */
        if (copy_from_user(&buf, src, len))
                return -EFAULT;

        /* copy buf into dst, which is in the user's address space */
        if (copy_to_user(dst, &buf, len))
                return -EFAULT;

        /* return amount of data copied */
        return len;
}
标红部分不明白, len是要复制数据的长度  它和sizeof(buf)得到long型字节数比较 怎么就能确定用户空间和内核空间的字长是否相等,这两个量在逻辑上好像没有联系

作者: Yuek_Lee   发布时间: 2011-01-21

注释说得很清楚啊
len 就是用户空间 unsinged long的size。

作者: baozhao   发布时间: 2011-01-21

就是拿它做跳板吧,长度当然应该吻合了

作者: amarant   发布时间: 2011-01-21

len 的值就是你用户空间传递的内存的大小。因为这个函数可能就是传递一个 unsigned long 的数值,但是需要读取和写入,因此使用指针形式。

用户空间调用的时候,len 的值应该是 sizeof(unsigned long)。这里判断一下,防止读写出现错误。

作者: Godbach   发布时间: 2011-01-21