Data transmission between user space and kernel space: get_user; put_user; copy_to_user; copy_from_user

Source: Internet
Author: User

During the development of Linux kernel, the memcpy () function cannot be used when data is transmitted between user_app and kernel. copy_to/from_kernel or put/get_user must be used. The reason is that the kernel and user_app memory cannot communicate with each other directly.

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

PTRMust have pointer-to-simple-variable type, andXMust be assignable to the result of dereferencingPTR.

Returns zero on success, or-efault on error.

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

PTRMust have pointer-to-simple-variable type, and the result of dereferencingPTRMust be assignableXWithout a cast.

Returns zero on success, or-efault on error. on error, the variableXIs set to zero.

 

 

1. 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 cocould not be copied. on success, this will be zero.

2. 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 cocould not be copied. on success, this will be zero.

If some data cocould not be copied, this function will pad the copied data to the requested Size Using Zero bytes.

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.