Linux 设备驱动程序中的静态函数 [英] Static functions in Linux device driver

查看:29
本文介绍了Linux 设备驱动程序中的静态函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么大多数设备驱动程序中的每个函数都是静态的?由于静态函数在文件范围之外不可见.那么,这些驱动函数是如何被用户空间应用调用的呢?

Why is it that every function in most device drivers are static? As static functions are not visible outside of the file scope. Then, how do these driver function get called by user space applications?

推荐答案

请记住,在 C 中一切都是地址.这意味着如果你有地址,你就可以调用一个函数.内核有一个名为 EXPORT_SYMBOL 的宏就是这样做的.它导出函数的地址,以便可以调用驱动程序函数而不必放置头声明,因为这些函数有时在编译时不知道.在这种情况下,静态限定符只是为了确保它们仅通过此方法调用,而不是从可能包含该驱动程序代码的其他文件中调用(在某些情况下,包含驱动程序代码标头并直接调用它们不是一个好主意).

Remember than in C everything is addresses. That means you can call a function if you have the address. The kernel has a macro named EXPORT_SYMBOL that does just that. It exports the address of a function so that driver functions can be called without having to place header declarations since those functions are sometimes not know at compile time. In cases like this the static qualifier is just made to ensure that they are only called through this method and not from other files that may include that driver code (in some cases it's not a good idea to include driver code headers and call them directly).

因为有人指出我没有涵盖用户空间.

Since it was pointed out that I did not cover userspace.

驱动函数通常不直接通过用户空间调用(除了 SYSCALL 指令的 x86 实现,它有时会做一些小技巧来保存上下文切换).所以这里的 static 关键字没有区别.它只会在内核空间上有所不同.正如@Cong Wang 所指出的那样,函数通常被放置在一个函数指针结构中,这样它们就可以通过简单地指向这个结构的结构(例如 file_ops、调度程序、文件系统、网络代码等)来调用.

Driver functions are usually not called through userspace directly (except for x86 implementation of SYSCALL instruction which does some little tricks to save the context switch sometimes). So the static keyword here makes no difference. It only makes a difference in kernel space. As pointed out by @Cong Wang, functions are usually place into a structure of function pointers so that they may be called by simply having structures point to this structure (such as file_ops, schedulers, filesystems, network code, etc...).

这篇关于Linux 设备驱动程序中的静态函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆