LDD simple中的代码,make 报错

发布于 2022-07-26 17:43:01 字数 12258 浏览 20 评论 0

  1. #include <linux/config.h>
  2. #include <linux/module.h>
  3. #include <linux/moduleparam.h>
  4. #include <linux/init.h>
  5. #include <linux/kernel.h>   /* printk() */
  6. #include <linux/slab.h>   /* kmalloc() */
  7. #include <linux/fs.h>       /* everything... */
  8. #include <linux/errno.h>    /* error codes */
  9. #include <linux/types.h>    /* size_t */
  10. #include <linux/mm.h>
  11. #include <linux/kdev_t.h>
  12. #include <asm/page.h>
  13. #include <linux/cdev.h>
  14. #include <linux/device.h>
  15. static int simple_major = 0;
  16. module_param(simple_major, int, 0);
  17. MODULE_AUTHOR("Jonathan Corbet");
  18. MODULE_LICENSE("Dual BSD/GPL");
  19. /*
  20. * Open the device; in fact, there's nothing to do here.
  21. */
  22. static int simple_open (struct inode *inode, struct file *filp)
  23. {
  24.         return 0;
  25. }
  26. /*
  27. * Closing is just as simpler.
  28. */
  29. static int simple_release(struct inode *inode, struct file *filp)
  30. {
  31.         return 0;
  32. }
  33. /*
  34. * Common VMA ops.
  35. */
  36. void simple_vma_open(struct vm_area_struct *vma)
  37. {
  38.         printk(KERN_NOTICE "Simple VMA open, virt %lx, phys %lxn",
  39.                         vma->vm_start, vma->vm_pgoff << PAGE_SHIFT);
  40. }
  41. void simple_vma_close(struct vm_area_struct *vma)
  42. {
  43.         printk(KERN_NOTICE "Simple VMA close.n");
  44. }
  45. /*
  46. * The remap_pfn_range version of mmap.  This one is heavily borrowed
  47. * from drivers/char/mem.c.
  48. */
  49. static struct vm_operations_struct simple_remap_vm_ops = {
  50.         .open =  simple_vma_open,
  51.         .close = simple_vma_close,
  52. };
  53. static int simple_remap_mmap(struct file *filp, struct vm_area_struct *vma)
  54. {
  55.         if (remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
  56.                             vma->vm_end - vma->vm_start,
  57.                             vma->vm_page_prot))
  58.                 return -EAGAIN;
  59.         vma->vm_ops = &simple_remap_vm_ops;
  60.         simple_vma_open(vma);
  61.         return 0;
  62. }
  63. /*
  64. * The nopage version.
  65. */
  66. struct page *simple_vma_nopage(struct vm_area_struct *vma,
  67.                 unsigned long address, int *type)
  68. {
  69.         struct page *pageptr;
  70.         unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
  71.         unsigned long physaddr = address - vma->vm_start + offset;
  72.         unsigned long pageframe = physaddr >> PAGE_SHIFT;
  73. // Eventually remove these printks
  74.         printk (KERN_NOTICE "---- Nopage, off %lx phys %lxn", offset, physaddr);
  75.         printk (KERN_NOTICE "VA is %pn", __va (physaddr));
  76.         printk (KERN_NOTICE "Page at %pn", virt_to_page (__va (physaddr)));
  77.         if (!pfn_valid(pageframe))
  78.                 return NOPAGE_SIGBUS;
  79.         pageptr = pfn_to_page(pageframe);
  80.         printk (KERN_NOTICE "page->index = %ld mapping %pn", pageptr->index, pageptr->mapping);
  81.         printk (KERN_NOTICE "Page frame %ldn", pageframe);
  82.         get_page(pageptr);
  83.         if (type)
  84.                 *type = VM_FAULT_MINOR;
  85.         return pageptr;
  86. }
  87. static struct vm_operations_struct simple_nopage_vm_ops = {
  88.         .open =   simple_vma_open,
  89.         .close =  simple_vma_close,
  90.         .nopage = simple_vma_nopage,
  91. };
  92. static int simple_nopage_mmap(struct file *filp, struct vm_area_struct *vma)
  93. {
  94.         unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
  95.         if (offset >= __pa(high_memory) || (filp->f_flags & O_SYNC))
  96.                 vma->vm_flags |= VM_IO;
  97.         vma->vm_flags |= VM_RESERVED;
  98.         vma->vm_ops = &simple_nopage_vm_ops;
  99.         simple_vma_open(vma);
  100.         return 0;
  101. }
  102. /*
  103. * Set up the cdev structure for a device.
  104. */
  105. static void simple_setup_cdev(struct cdev *dev, int minor,
  106.                 struct file_operations *fops)
  107. {
  108.         int err, devno = MKDEV(simple_major, minor);
  109.    
  110.         cdev_init(dev, fops);
  111.         dev->owner = THIS_MODULE;
  112.         dev->ops = fops;
  113.         err = cdev_add (dev, devno, 1);
  114.         /* Fail gracefully if need be */
  115.         if (err)
  116.                 printk (KERN_NOTICE "Error %d adding simple%d", err, minor);
  117. }
  118. /*
  119. * Our various sub-devices.
  120. */
  121. /* Device 0 uses remap_pfn_range */
  122. static struct file_operations simple_remap_ops = {
  123.         .owner   = THIS_MODULE,
  124.         .open    = simple_open,
  125.         .release = simple_release,
  126.         .mmap    = simple_remap_mmap,
  127. };
  128. /* Device 1 uses nopage */
  129. static struct file_operations simple_nopage_ops = {
  130.         .owner   = THIS_MODULE,
  131.         .open    = simple_open,
  132.         .release = simple_release,
  133.         .mmap    = simple_nopage_mmap,
  134. };
  135. #define MAX_SIMPLE_DEV 2
  136. #if 0
  137. static struct file_operations *simple_fops[MAX_SIMPLE_DEV] = {
  138.         &simple_remap_ops,
  139.         &simple_nopage_ops,
  140. };
  141. #endif
  142. /*
  143. * We export two simple devices.  There's no need for us to maintain any
  144. * special housekeeping info, so we just deal with raw cdevs.
  145. */
  146. static struct cdev SimpleDevs[MAX_SIMPLE_DEV];
  147. /*
  148. * Module housekeeping.
  149. */
  150. static int simple_init(void)
  151. {
  152.         int result;
  153.         dev_t dev = MKDEV(simple_major, 0);
  154.         /* Figure out our device number. */
  155.         if (simple_major)
  156.                 result = register_chrdev_region(dev, 2, "simple");
  157.         else {
  158.                 result = alloc_chrdev_region(&dev, 0, 2, "simple");
  159.                 simple_major = MAJOR(dev);
  160.         }
  161.         if (result < 0) {
  162.                 printk(KERN_WARNING "simple: unable to get major %dn", simple_major);
  163.                 return result;
  164.         }
  165.         if (simple_major == 0)
  166.                 simple_major = result;
  167.         /* Now set up two cdevs. */
  168.         simple_setup_cdev(SimpleDevs, 0, &simple_remap_ops);
  169.         simple_setup_cdev(SimpleDevs + 1, 1, &simple_nopage_ops);
  170.         return 0;
  171. }
  172. static void simple_cleanup(void)
  173. {
  174.         cdev_del(SimpleDevs);
  175.         cdev_del(SimpleDevs + 1);
  176.         unregister_chrdev_region(MKDEV(simple_major, 0), 2);
  177. }
  178. module_init(simple_init);
  179. module_exit(simple_cleanup);

复制代码

  1. # Comment/uncomment the following line to disable/enable debugging
  2. #DEBUG = y
  3. # Add your debugging flag (or not) to CFLAGS
  4. ifeq ($(DEBUG),y)
  5.   DEBFLAGS = -O -g # "-O" is needed to expand inlines
  6. else
  7.   DEBFLAGS = -O2
  8. endif
  9. CFLAGS += $(DEBFLAGS) -I$(LDDINCDIR)
  10. ifneq ($(KERNELRELEASE),)
  11. # call from kernel build system
  12. obj-m        := simple.o
  13. else
  14. KERNELDIR ?= /lib/modules/$(shell uname -r)/build
  15. PWD       := $(shell pwd)
  16. default:
  17.         $(MAKE) -C $(KERNELDIR) M=$(PWD) LDDINCDIR=$(PWD)/../include modules
  18. endif
  19. clean:
  20.         rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions
  21. depend .depend dep:
  22.         $(CC) $(CFLAGS) -M *.c > .depend
  23. ifeq (.depend,$(wildcard .depend))
  24. include .depend
  25. endif

复制代码

OS是redhat9.0, 用make xconfig在kenerl配置选项中看到enable loadable module 是打开的,
但make是却提示modules disabled

  1. make -C /lib/modules/2.4.20-8smp/build M=/home/qiang/video/test/examples/simple LDDINCDIR=/home/qiang/video/test/examples/simple/../include modules
  2. make[1]: Entering directory `/usr/src/linux-2.4.20-8'
  3. The present kernel configuration has modules disabled.
  4. Type 'make config' and enable loadable module support.
  5. Then build a kernel with module support enabled.
  6. make[1]: *** [modules] Error 1
  7. make[1]: Leaving directory `/usr/src/linux-2.4.20-8'
  8. make: *** [default] Error

复制代码

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文