Chinaunix首页 | 论坛 | 博客
  • 博客访问: 35148
  • 博文数量: 42
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 70
  • 用 户 组: 普通用户
  • 注册时间: 2015-12-06 20:19
文章分类

全部博文(42)

文章存档

2016年(12)

2015年(30)

我的朋友

分类: LINUX

2015-12-06 20:25:11

 

copy_to_user,copy_from_user,get_user,put_user函数比较

 --  Copy a block of data into user space.

 --  Copy a block of data from user space.

 --  Get a simple variable from user space.

 --  Write a simple value into user space.

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的区别

    我在从事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
阅读(262) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~