简体   繁体   English

我如何将用户空间中的缓冲区地址传递给内核

[英]How can i pass the address of a buffer in user space to kernel

I want to use copy_to_user() to copy some information of a platform net device to a buffer in user space.But i don't how can kernel knows the "to" pointer which used by copy_to_user(). 我想使用copy_to_user()将平台网络设备的某些信息复制到用户空间中的缓冲区。但是我不知道内核如何知道copy_to_user()使用的“ to”指针。 The driver's ioctl() needs struct ifreq as a parameter.And i know i can initialize the ifreq.name to find the driver's ioctl().But how can i pass the "to" pointer to kernel exactly the driver'ioctl()? 驱动程序的ioctl()需要结构ifreq作为参数。我知道我可以初始化ifreq.name来查找驱动程序的ioctl()。但是如何将“ to”指针精确地传递给驱动程序的ioctl()?

struct ifreq looks like this: struct ifreq看起来像这样:

struct ifreq {
    char    ifr_name[IFNAMSIZ];/* Interface name */
    union {
            struct sockaddrifr_addr;
            struct sockaddrifr_dstaddr;
            struct sockaddrifr_broadaddr;
            struct sockaddrifr_netmask;
            struct sockaddrifr_hwaddr;
            short   ifr_flags;
            int     ifr_ifindex;
            int     ifr_metric;
            int     ifr_mtu;
            struct ifmapifr_map;
            char    ifr_slave[IFNAMSIZ];
            char    ifr_newname[IFNAMSIZ];
            char *  ifr_data;
    };
};

If you are implementing an existing ioctl command, you must figure out which of the members in the union you're supposed to use, both in user space and the kernel. 如果要实现现有的ioctl命令,则必须确定用户空间和内核中应使用联合中的哪些成员。

If you're implementing your own ioctl command, you can use ifr_data . 如果要实现自己的ioctl命令,则可以使用ifr_data The caller (in user space) sets the member to point to a local buffer, which you'd fill in with copy_to_user() in the kernel (ie ifr_data is the to that you're looking for.) 主叫方(用户空间)设置为指向一个本地缓冲区,你会填用copy_to_user()在内核中的成员(即ifr_data是你要找的。)

ie user space does 即用户空间确实

char buf[128];
struct ifreq req;
strcpy(req.ifr_name,"eth0");
req.ifr_data = buf;
ioctl(fd, SIOCMYIOCTL, &ifr);

Here the buffer is just a fixed size array, if you need more flexibility you can ofcourse use a struct, as long as user space and your kernel ioctl() agrees on what ifr_data is. 这里的缓冲区只是一个固定大小的数组,如果您需要更大的灵活性,则可以使用结构,只要用户空间和内核ioctl()同意ifr_data是什么。

struct my_ioctl_data {
     int a, b, c;
};
struct my_ioctl_data data;
struct ifreq req;
strcpy(req.ifr_name,"eth0");
req.ifr_data = (char*)&data;
ioctl(fd, SIOCMYIOCTL, &ifr);

In the platform net device driver change the read method : 在平台网络设备驱动程序中更改读取方法:

static ssize_t net_device_read(arg1,char __user *user_buffer,size_t count,loff_t *position)

{


   copy_to_user(user_buffer, position,count) != 0 

}

Access this buffer from the user space using cat command 使用cat命令从用户空间访问此缓冲区

 cat /dev/net-device 
>> string to pass

read more about this here : http://www.codeproject.com/Articles/112474/A-Simple-Driver-for-Linux-OS 在此处阅读有关此内容的更多信息: http : //www.codeproject.com/Articles/112474/A-Simple-Driver-for-Linux-OS

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果我只有设备缓冲区(PCIe)的物理地址,该如何将该缓冲区映射到用户空间? - If I have only the physical address of device buffer (PCIe), how can I map this buffer to user-space? 用户空间缓冲区和内核缓冲区 - User space buffer and Kernel buffer 如何在内核驱动程序中分配用户空间缓冲区? - How to allocate user space buffer in kernel driver? 我可以将指针传递给Linux内核空间以获取__user参数吗? - Can I pass a pointer to linux kernel space for a __user parameter? 如何在Opencl内核的本地地址空间中传递数组 - How to pass a array in local address space in Opencl kernel 如何在内核模块中获取数组的地址,以便能够在用户空间应用程序中使用它? - How to get the address of an array in kernel module so I'll be able to use it within a user-space application? 如何从 Linux kernel 空间模块执行/调用用户空间定义的 function? - How can I Execute/Call a user-space defined function from Linux kernel space module? 如何将字节内存从内核模块映射到用户空间应用程序? - How can I map a memory of bytes from a kernel module to user space app? 如何在linux内核中不使用malloc()创建缓冲区? - How can i create a buffer without using malloc() in linux kernel? 如何通过缓冲区传递整数? - How can I pass integer through the buffer?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM