[英]copy data from kernel space to user space
I'm trying to make a custom system call. 我正在尝试进行自定义系统调用。
my system call takes 2 parameters struct buffer **mybuffer
& int size
. 我的系统调用需要2个参数
struct buffer **mybuffer
& int size
。
it's imposed any change that happens to **mybuffer
should reflect in the user-space, but it seems it doesn't work. 它强加了
**mybuffer
发生的任何变化应该反映在用户空间中,但似乎它不起作用。
so I've seen somewhere else that i can use copy_to_user(void *dest, void *src, int size)
to copy data from kernel space to user space. 所以我在其他地方看到我可以使用
copy_to_user(void *dest, void *src, int size)
将数据从内核空间复制到用户空间。
in user-space i have a struct called buffer, also this struct appears the same in the system call. 在用户空间中,我有一个名为buffer的结构,此结构在系统调用中也是相同的。
typedef struct buffer {
int n;
}buffer;
int main(void)
{
buffer **buf = malloc(sizeof(buffer *));
int i = 0
for(;i<8;i++)
buf[i] = malloc(sizeof(buffer));
long int sys = systemcall(801,buf,8)
//print out buf
return 0;
}
In system call i have 在系统调用我有
asmlinkage long sys_something(buffer **buf,int size)
{
//allocate buffer same as it appears in int main
//fill buf with some data
for(i = 0; i<size,i++)
copy_to_user(buf[i],buf[i],sizeof(buffer));
I'm pretty sure that i'm doing something wrong. 我很确定我做错了什么。 how actually to copy data from kernel space to user space ?
如何将数据从内核空间复制到用户空间?
Ps I'm using linux kernel 3.16.0 Ps我正在使用Linux内核3.16.0
The function copy_to_user
is used to copy data from the kernel address space to the address space of the user program. 函数
copy_to_user
用于将数据从内核地址空间复制到用户程序的地址空间。 For example, to copy a buffer which has been allocated with kmalloc
to the buffer provided by the user. 例如,将已分配了
kmalloc
的缓冲区复制到用户提供的缓冲区。
EDIT: Your example is a little bit more complex, because you pass an array of pointers to the system-call. 编辑:您的示例有点复杂,因为您传递了一系列指向系统调用的指针。 To access these pointers you have to copy the array
buf
to kernel space first using copy_from_user
. 要访问这些指针,您必须首先使用
copy_from_user
将数组buf
复制到内核空间。
Thus, your kernel code should look like this: 因此,您的内核代码应如下所示:
asmlinkage long sys_something(buffer **buf, int size)
{
/* Allocate buffers_in_kernel on stack just for demonstration.
* These buffers would normally allocated by kmalloc.
*/
buffer buffers_in_kernel[size];
buffer *user_pointers[size];
int i;
unsigned long res;
/* Fill buffers_in_kernel with some data */
for (i = 0; i < size; i++)
buffers_in_kernel[i].n = i; /* just some example data */
/* Get user pointers for access in kernel space.
* This is a shallow copy, so that, the entries in user_pointers
* still point to the user space.
*/
res = copy_from_user(user_pointers, buf, sizeof(buffer *) * size);
/* TODO: check result here */
/* Now copy data to user space. */
for (i = 0; i < size; i++) {
res = copy_to_user(user_pointers[i], &buffers_in_kernel[i], sizeof(buffer));
/* TODO: check result here */
}
}
Last but not least, there is a mistake in your main
function. 最后但并非最不重要的是,你的
main
功能有一个错误。 At the first malloc
call, it allocates only enough space for 1 pointer instead of 8. It should be: 在第一个
malloc
调用时,它只为1个指针而不是8分配足够的空间。它应该是:
int main(void)
{
const int size = 8;
buffer **buf = malloc(sizeof(buffer *) * size);
for(int i=0; i<size; i++) buf[i] = malloc(sizeof(buffer));
long int sys = systemcall(801,buf,size)
//print out buf
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.