内核和用户空间共享内存的实现例程-proc和mmap

作者: xingfuhaitun 和linux相关  

本文转自  http://bbs.chinaunix.net/thread-2017863-1-1.html Godbach (哥德巴赫) 版主

整个内核模块,在模块插入时建立proc文件,分配内存。卸载模块的时候将用户空间写入的内容打印出来。
以下是内核模块的代码和用户空间的测试代码。

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <string.h>
  5. #include <fcntl.h>
  6. #include <sys/stat.h>
  7. #include <sys/types.h>
  8. #include <sys/mman.h>
  9. int main(int argc, char* argv[])
  10. {
  11.         if(argc != 2)
  12.         {
  13.                 printf("Usage: %s string\n", argv[0]);
  14.                 return 0;
  15.         }
  16.         
  17.         unsigned long phymem_addr, phymem_size;
  18.         char *map_addr;
  19.         char s[256];
  20.         int fd;
  21.         
  22.         /*get the physical address of allocated memory in kernel*/
  23.         fd = open("/proc/memshare/phymem_addr", O_RDONLY);
  24.         if(fd < 0)
  25.         {
  26.                 printf("cannot open file /proc/memshare/phymem_addr\n");
  27.                 return 0;
  28.         }
  29.         read(fd, s, sizeof(s));
  30.         sscanf(s, "%lx", &phymem_addr);
  31.         close(fd);
  32.         /*get the size of allocated memory in kernel*/
  33.         fd = open("/proc/memshare/phymem_size", O_RDONLY);
  34.         if(fd < 0)
  35.         {
  36.                 printf("cannot open file /proc/memshare/phymem_size\n");
  37.                 return 0;
  38.         }
  39.         read(fd, s, sizeof(s));
  40.         sscanf(s, "%lu", &phymem_size);
  41.         close(fd);
  42.         
  43.         printf("phymem_addr=%lx, phymem_size=%lu\n", phymem_addr, phymem_size);
  44.         /*memory map*/
  45.         int map_fd = open("/dev/mem", O_RDWR);
  46.         if(map_fd < 0)
  47.         {
  48.                 printf("cannot open file /dev/mem\n");
  49.                 return 0;
  50.         }
  51.         
  52.         map_addr = mmap(0, phymem_size, PROT_READ|PROT_WRITE, MAP_SHARED, map_fd, phymem_addr);
  53.         strcpy(map_addr, argv[1]);
  54.         munmap(map_addr, phymem_size);
  55.         close(map_fd);
  56.         return 0;
  57.         
  58. }

用户空间的测试代码:

  1. /*This program is used to allocate memory in kernel
  2. and pass the physical address to userspace through proc file.*/
  3. #include <linux/version.h>
  4. #include <linux/module.h>
  5. #include <linux/proc_fs.h>
  6. #include <linux/mm.h>
  7. #define PROC_MEMSHARE_DIR "memshare"
  8. #define PROC_MEMSHARE_PHYADDR "phymem_addr"
  9. #define PROC_MEMSHARE_SIZE "phymem_size"
  10. /*alloc one page. 4096 bytes*/
  11. #define PAGE_ORDER 0
  12. /*this value can get from PAGE_ORDER*/
  13. #define PAGES_NUMBER 1
  14. struct proc_dir_entry *proc_memshare_dir ;
  15. unsigned long kernel_memaddr = 0;
  16. unsigned long kernel_memsize= 0;
  17. static int proc_read_phymem_addr(char *page, char **start, off_t off, int count)
  18. {
  19.         return sprintf(page, "%08lx\n", __pa(kernel_memaddr));
  20. }
  21. static int proc_read_phymem_size(char *page, char **start, off_t off, int count)
  22. {
  23.         return sprintf(page, "%lu\n", kernel_memsize);
  24. }
  25. static int __init init(void)
  26. {
  27.         /*build proc dir "memshare"and two proc files: phymem_addr, phymem_size in the dir*/
  28.         proc_memshare_dir = proc_mkdir(PROC_MEMSHARE_DIR, NULL);
  29.         create_proc_info_entry(PROC_MEMSHARE_PHYADDR, 0, proc_memshare_dir, proc_read_phymem_addr);
  30.         create_proc_info_entry(PROC_MEMSHARE_SIZE, 0, proc_memshare_dir, proc_read_phymem_size);
  31.         /*alloc one page*/
  32.         kernel_memaddr =__get_free_pages(GFP_KERNEL, PAGE_ORDER);
  33.         if(!kernel_memaddr)
  34.         {
  35.                 printk("Allocate memory failure!\n");
  36.         }
  37.         else
  38.         {
  39.                 SetPageReserved(virt_to_page(kernel_memaddr));
  40.                 kernel_memsize = PAGES_NUMBER * PAGE_SIZE;
  41.                 printk("Allocate memory success!. The phy mem addr=%08lx, size=%lu\n", __pa(kernel_memaddr), kernel_memsize);
  42.         }
  43.         return 0;
  44. }
  45. static void __exit fini(void)
  46. {
  47.         printk("The content written by user is: %s\n", (unsigned char *) kernel_memaddr);
  48.         ClearPageReserved(virt_to_page(kernel_memaddr));
  49.         free_pages(kernel_memaddr, PAGE_ORDER);
  50.         remove_proc_entry(PROC_MEMSHARE_PHYADDR, proc_memshare_dir);
  51.         remove_proc_entry(PROC_MEMSHARE_SIZE, proc_memshare_dir);
  52.         remove_proc_entry(PROC_MEMSHARE_DIR, NULL);
  53.         return;
  54. }
  55. module_init(init);
  56. module_exit(fini);
  57. MODULE_LICENSE("GPL");
  58. MODULE_AUTHOR("Godbach (nylzhaowei@163.com)");
  59. MODULE_DESCRIPTION("Kernel memory share module.");
 测试的内核是2.6.25.以下是执行结果。
  1. debian:/home/km/memshare# insmod memshare_kernel.ko
  2. debian:/home/km/memshare# ./memshare_user 'hello,
  3. phymem_addr=e64e000, phymem_size=4096
  4. debian:/home/km/memshare# cat /proc/memshare/phymem_addr
  5. 0e64e000
  6. debian:/home/km/memshare# cat /proc/memshare/phymem_size
  7. 4096
  8. debian:/home/km/memshare# rmmod memshare_kernel
  9. debian:/home/km/memshare# tail /var/log/messages
  10. Sep 27 18:14:24 debian kernel: [50527.567931] Allocate memory The phy mem addr=0e64e000, size=4096
  11. Sep 27 18:15:31 debian kernel: [50592.570986] The content written by user is: hello,

相关资料:

内核和用户空间共享内存的实现例程-proc和mmap来源网络,如有侵权请告知,即处理!

编程Tags: