当前位置: 首页 > 工具软件 > iCopy > 使用案例 >

copy_to_user/copy_from_user解析与示例

顾昌翰
2023-12-01

copy_to_user/copy_from_user解析
copy_to_user和copy_from_user是在进行驱动相关程序设计的时候,要经常遇到的函数。由于内核空间与用户空间的内存不能直接互访,
因此借助函数copy_to_user()完成内核空间到用户空间的复制,
函数copy_from_user()完成用户空间到内核空间的复制

copy_to_user

To 目标地址,这个地址是用户空间的地址;
From 源地址,这个地址是内核空间的地址;
N 将要拷贝的数据的字节数。

copy_from_user

To 目标地址,这个地址是内核空间的地址;
From 源地址,这个地址是用户空间的地址;
N 将要拷贝的数据的字节数。

driver和user之间经常需要数据传输,使用copy_to_user()和copy_from_user()来实现driver到user和user到driver的数据传送。

函数原型:

unsigned long copy_to_user(void *to, const void __user *from, usigned long count);

unsigned long copy_from_user(void __user *to, const void *from, usigned long count);

代码实例:

hello.c:


```c
#include <linux/init.h>

#include <linux/module.h>

#include <linux/fs.h>

#include <linux/cdev.h>
#include <asm/uaccess.h>

MODULE_LICENSE("Dual BSD/GPL");
static int count = 10;

static char *init_mesg = "hello,world\n";

static char *exit_mesg = "goodbye\n";

static int major = 252;

static int minor = 0;

dev_t devnum;
int static hello_dev_open(struct inode *inode, struct file *file)
{
         printk("file open in hello_dev_open......finished!\n");
         return 0;
}

int static hello_dev_release(struct inode *inode, struct file *file)
{
         printk("file release in hello_dev_release......finished!\n");
         return 0;
}

ssize_t hello_dev_read(struct file *file, char __user *buf,size_t count, loff_t *offset)
{
  char alpha[27];
  int i, cnt;
  memset(alpha, 0, 27);
  for(i = 0; i < 26; i++)
         alpha[i] = 'a' + i;
  if(count > 26)
         cnt = 26;
  else
         cnt = count;
//使用copy_to_user ()函数从driver读数据到user
  if(!copy_to_user((char *)buf, alpha, cnt))
         return cnt;
  else
     return -1;

}

ssize_t hello_dev_write(struct file *file, const char __user *buf, size_t count, loff_t *offset)
{
  char alpha[27];  

  int cnt;

  memset(alpha, 0, 27);

  if(count > 26)
         cnt = 26;
  else
       cnt = count;

//使用copy_from_user()函数从user写数据到driver

  if(!copy_from_user((char *)alpha, buf, cnt))
  {  
         printk(alpha);

         printk("\n");

         return cnt;

  }  

  else
         return -1;

}

static struct cdev hello_dev;

static struct file_operations fops ={

         .owner = THIS_MODULE,

         .open  = hello_dev_open,

         .release = hello_dev_release,

         .read = hello_dev_read,

         .write = hello_dev_write,

};


static int __init hello_init(void)

{

  int i;

  int ret;

  for(i = 0; i < count; i++)

         printk(init_mesg);

//ret = register_chrdev_region(MKDEV(major,minor), 1, "hello_dev");

  ret = alloc_chrdev_region(&devnum, 10, 1, "hello_dev");

  if(!ret)

  {

       major = MAJOR(devnum);

         minor = MINOR(devnum);

         printk("major = %d; minor = %d\n", major, minor);

  }

  cdev_init(&hello_dev, &fops);

  ret = cdev_add(&hello_dev, devnum, 1);

  return ret;

}

void hello_exit(void)

{
  printk(exit_mesg);
  cdev_del(&hello_dev);
  unregister_chrdev_region(MKDEV(major, minor),1);
  return;

}

void hello(void)

{
         printk("good mornig1\n");
}

 

module_param(count, int, S_IRUGO);

module_param(init_mesg, charp, S_IRUGO);

module_param(exit_mesg, charp, S_IRUGO);

 

EXPORT_SYMBOL_GPL(hello);

module_init(hello_init);

module_exit(hello_exit);

 

测试程序:

main.c:

#include <stdio.h>

#include <unistd.h>

#include <sys/types.h>

#include <fcntl.h>

#include <string.h>

#define DEVNAME "/dev/hello"

int main()
{

  char alpha[27];

  int fd,i;

  memset(alpha, 0, 27);

  for(i = 0; i < 26; i++)

         alpha[i] = 'A' + i;

  fd = open(DEVNAME, O_RDWR);//在系统内创建DEVNAME文件的时候,需要使用内核注册设备是使用的major和minor设备号,这也就是应用层和内核层的纽带(这个注释是转载时的个人理解,不知道对不对?非原作者的意见。)

  if(fd == -1)
  {
         printf("file %s is opening......failure!", DEVNAME);
  }

  else
  {
         printf("file %s is opening......successfully!\nits fd is %d\n", DEVNAME, fd);

  }

 

  getchar();

  printf("write A-Z to kernel......\n");
  write(fd, alpha, 26);
  getchar();

  printf("read datas from kernel.......\n");

  read(fd, alpha, 26);

  printf("%s\n", alpha);

  getchar();

  close(fd);

  return 0;

}

编译:gcc  main.c  -o main

模块加载并创建结点后(过程参考前面日志),运行./main

结果如下:

file /dev/hello is opening……successfully!

Its fd is 3

(输入回车)

write  A-Z  to  kernel……

(输入回车)

read  datas  from  kernel……

abcdefghijklmnopqrstuvwxyz

(输入回车)

 

 

**输入dmesg命令,结果如下**:

file open in hello_dev_open……finished!

ABCDEFGHIJKLMNOPQRSTUVWXYZ

file release in hello_dev_open……finished!
 类似资料: