从用户空间应用程序调用内核空间中的用户定义函数

我在我的设备驱动程序中编写了一个用户定义的函数,我想从用户空间应用程序中调用它。 我该如何实现这一目标?

我所说的用户定义函数是除了内核定义函数之外的任何函数。 在struct file_operations定义的指针,如下所示。

 struct file_operations { struct module *owner; loff_t (*llseek) (struct file *, loff_t, int); ssize_t (*read) (struct file *, char *, size_t, loff_t *); ssize_t (*write) (struct file *, const char *, size_t, loff_t *); int (*readdir) (struct file *, void *, filldir_t); unsigned int (*poll) (struct file *, struct poll_table_struct *); int (*ioctl) (struct inode *, struct file *, unsigned int, unsigned long); int (*mmap) (struct file *, struct vm_area_struct *); int (*open) (struct inode *, struct file *); int (*flush) (struct file *); int (*release) (struct inode *, struct file *); int (*fsync) (struct file *, struct dentry *, int datasync); int (*fasync) (int, struct file *, int); int (*lock) (struct file *, int, struct file_lock *); ssize_t (*readv) (struct file *, const struct iovec *, unsigned long, loff_t *); ssize_t (*writev) (struct file *, const struct iovec *, unsigned long, loff_t *); }; 

例如。 在我的司机我有以下,

 struct file_operations fops = { .read = my_read, .write = my_write, }; 

我可以使用write调用从用户空间应用程序调用这些函数。

我在内核源代码中也有一个名为user_defined的函数,我的问题是如何从用户空间程序中调用它?

那么,你所拥有的通常被称为“系统调用”。 我建议你阅读内核开发者文档,了解如何公开新的系统调用。 在用户空间方面,libc为您提供了一个函数系统调用,您可以使用它来调用内核空间。 但通常你会围绕它写一个包装器。

但是应该避免引入新的系统调用。 调用内核空间的首选方法是使用sysfs并将用户空间可调用函数作为文件公开。

更快更简单的方法是从用户空间应用程序编写ioctl()并调用ioctl()。 编写系统调用也可以,但是我们应该避免编写新的系统调用,这也很耗时。 要编写ioctl,您可以参考以下链接:

  1. TPLD

  2. LJ

  3. LDD3