将数据从内核空间复制到用户空间 [英] copy data from kernel space to user space

查看:785
本文介绍了将数据从内核空间复制到用户空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试进行自定义系统调用.

I'm trying to make a custom system call.

我的系统调用使用2个参数struct buffer **mybuffer& int size.

my system call takes 2 parameters struct buffer **mybuffer & int size.

它强加了对**mybuffer所做的任何更改,这些更改应反映在用户空间中,但似乎不起作用.

it's imposed any change that happens to **mybuffer should reflect in the user-space, but it seems it doesn't work.

所以我已经看到其他地方可以使用copy_to_user(void *dest, void *src, int size)将数据从内核空间复制到用户空间.

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.

在用户空间中,我有一个名为buffer的结构,该结构在系统调用中也是如此.

in user-space i have a struct called buffer, also this struct appears the same in the system call.

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;
   }

我在系统通话中拥有

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 ?

P.s.我正在使用Linux内核3.16.0

P.s. I'm using linux kernel 3.16.0

推荐答案

函数copy_to_user用于将数据从内核地址空间复制到用户程序的地址空间.例如,要将已经用kmalloc分配的缓冲区复制到用户提供的缓冲区.

The function copy_to_user is used to copy data from the kernel address space to the address space of the user program. For example, to copy a buffer which has been allocated with kmalloc to the buffer provided by the user.

编辑:您的示例稍微复杂一点,因为您将指针数组传递给系统调用.访问这些指针 您必须先使用copy_from_user将数组buf复制到内核空间.

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.

因此,您的内核代码应如下所示:

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 */
    }
}

最后但并非最不重要的是,您的main函数存在错误.在第一次malloc调用中,它只为1个指针分配了足够的空间,而不是8.它应该是:

Last but not least, there is a mistake in your main function. At the first malloc call, it allocates only enough space for 1 pointer instead of 8. It should be:

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;
}

这篇关于将数据从内核空间复制到用户空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆