copy_to_user,copy_from_user,get_user,put_user函数比较

copy_to_user --  Copy a block of data into user space. copy_from_user --  Copy a block of data from user space.get_user --  Get a simple variable from user space.
put_user --  Write a simple value into user space.
文章出处:飞诺网(www.diybl.com):http://www.diybl.com/course/6_system/linux/Linuxjs/20101230/551611.html

copy_from_user


Name
copy_from_user --  Copy a block of data from user space.

Synopsis

 

unsigned long copy_from_user (void * to, const void __user * from, unsigned long n);

 


Arguments

 

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.

 

Description
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.

 

copy_to_user


Name
copy_to_user --  Copy a block of data into user space.

Synopsis

 

unsigned long copy_to_user (void __user * to, const void * from, unsigned long n);

 


Arguments

 

to
Destination address, in user space.

from
Source address, in kernel space.

n
Number of bytes to copy.

 

Context
User context only. This function may sleep.

 

Description
Copy data from kernel space to user space.

Returns number of bytes that could not be copied. On success, this will be zero.

put_user


Name
put_user --  Write a simple value into user space.

Synopsis

 

put_user ( x, ptr);

 


Arguments

 

x
Value to copy to user space.

ptr
Destination address, in user space.

 

Context
User context only. This function may sleep.

 

Description
This macro copies a single simple value from kernel space to user space. It supports simple types like char and int, but not larger data types like structures or arrays.

ptr must have pointer-to-simple-variable type, and x must be assignable to the result of dereferencing ptr.

Returns zero on success, or -EFAULT on error.

 


put_user() 宏函数

 

功能:把内核变量值写入用户空间

原型: put_user (x, ptr)

说明:  put_user 宏函数在 ptr 指向的用户内存空间上写入 x 变量大小的数据

变量:

•x      内核变量
•ptr   用户内存块的起始地址
get_user


Name
get_user --  Get a simple variable from user space.

Synopsis

 

get_user ( x, ptr);

 


Arguments

 

x
Variable to store result.

ptr
Source address, in user space.

 

Context
User context only. This function may sleep.

 

Description
This macro copies a single simple variable from user space to kernel space. It supports simple types like char and int, but not larger data types like structures or arrays.

ptr must have pointer-to-simple-variable type, and the result of dereferencing ptr must be assignable to x without a cast.

Returns zero on success, or -EFAULT on error. On error, the variable x is set to zero.
 

 

Linux中put_user和memcpy的区别

 

http://bbs.51soc.com/read.php?tid=361
    我在从事linux kernel开发的时候,知道user app和kernel模块之间传输数据不能使用memcpy,必须使用copy_to/from_user或是put/get_user。原因就是kernel和user app内存不能直接互访。仅仅明白到这个层次。具体为什么不能直接互访,也说不出什么来。
    最近在ARM平台上开发,看了一下kernel中put_user的代码和关于linux kernel的资料,自己对这个问题的理解有加深了一些。现在总结如下
    首先 Linux的kernel和user app运行在不同的模式。在ARM上就是kernel运行在SVC模式(最高级),App运行在user模式。当user app执行一个system call调用kernel代码,kernel会完成CPU的模式切换。SVC模式能访问的寄存器更多,如果在kernel中对user app传入的地址直接访问,可能会有以下问题:传入的地址是错误的,则kernel也可以访问,可能会破坏其他kernel内容,这会引起安全问题,memcpy不会进行地址检查,使用专用的put_user就会执行地址检查,判断是否是<0xc0000000,是否在user app地址空间之内。同时在ARM平台上,最后调用的函数是__put_user_1/2/4/8。这个是一个汇编函数,使用的数据访问指令为ldrbt,这个ARM汇编指令使用user mode来访问内存,如果这个访问非法,这会触发一个exception,在kernel中都加入了一个异常函数表,出现异常则调用__get_user_bad
    个人感觉使用专用函数在kernel和user app之间传递数据就是为了安全,防止参数非法破坏kernel
文章出处:飞诺网(www.diybl.com):http://www.diybl.com/course/6_system/linux/Linuxjs/20101230/551611.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值