本文参考以下博文:
Linux内核中_IO,_IOR,_IOW,_IOWR宏的用法与解析
特此致谢!
从本回开始,正式进入libdrm源码,展开对于源码的详细解析。
根据笔者的经验,要先从libdrm源码中各个操作的宏定义开始讲起,这是后续知识内容的基础。
这个包含各个操作宏定义的关键文件是include/drm/drm.h, 总共有将近1200行代码。下边按照代码中宏的顺序逐个进行分析和讲解。
一开始是一些基础宏,这是为后续宏定义所调用的,代码如下:
#define DRM_IOCTL_BASE 'd'
#define DRM_IO(nr) _IO(DRM_IOCTL_BASE,nr)
#define DRM_IOR(nr,type) _IOR(DRM_IOCTL_BASE,nr,type)
#define DRM_IOW(nr,type) _IOW(DRM_IOCTL_BASE,nr,type)
#define DRM_IOWR(nr,type) _IOWR(DRM_IOCTL_BASE,nr,type)
将DRM_IOCTL_BASE代入,展开来就是:
#define DRM_IOCTL_BASE 'd'
#define DRM_IO(nr) _IO('d',nr)
#define DRM_IOR(nr,type) _IOR('d',nr,type)
#define DRM_IOW(nr,type) _IOW('d',nr,type)
#define DRM_IOWR(nr,type) _IOWR('d',nr,type)
这里要对于_IO、_IOR、_IOW、_IOWR的相关知识进行补强。
在驱动程序里, ioctl系统调用中传送的变量cmd是应用程序用于区别设备驱动程序请求处理内容的值。cmd除了可区别数字外,还包含有助于处理的几种相应信息。cmd的大小为 32位,共分 4 个域:
- bit31~bit30:这2位为 “区别读写” 区,作用是区分是读取命令还是写入命令。
- bit29~bit16:这14位为 “数据大小” 区,表示ioctl()中的arg变量传送的内存大小。
- bit15~bit08:这8位为 “魔数”(也称“幻数”)区,这个值用以与其它设备驱动程序的 ioctl进行区别。
- bit07~bit00:这8位为 “区别序号” 区,是区分命令的命令顺序序号。
命令码“区分读写区” 的可能取值为:_IOC_NONE (0值),表示无数据传输;_IOC_READ (读);_IOC_WRITE (写) ;_IOC_READ|_IOC_WRITE (双向)。
Linux内核定义了_IO() 、_IOR() 、IOW()和_IOWR()这4个宏来辅助生成上面的cmd。
在内核源码的include/uapi/asm-generic/ioctl.h中可以看到_IO()、_IOR()、_IOW()、_IOWR()的定义,如下:
/*
* Used to create numbers.
*
* NOTE: _IOW means userland is writing and kernel is reading. _IOR
* means userland is reading and kernel is writing.
*/
#define _IO(type,nr) _IOC(_IOC_NONE,(type),(nr),0)
#define _IOR(type,nr,size) _IOC(_IOC_READ,(type),(nr),(_IOC_TYPECHECK(size)))
#define _IOW(type,nr,size) _IOC(_IOC_WRITE,(type),(nr),(_IOC_TYPECHECK(size)))
#define _IOWR(type,nr,size) _IOC(_IOC_READ|_IOC_WRITE,(type),(nr),(_IOC_TYPECHECK(size)))
#define _IOR_BAD(type,nr,size) _IOC(_IOC_READ,(type),(nr),sizeof(size))
#define _IOW_BAD(type,nr,size) _IOC(_IOC_WRITE,(type),(nr),sizeof(size))
#define _IOWR_BAD(type,nr,size) _IOC(_IOC_READ|_IOC_WRITE,(type),(nr),sizeof(size))
_IOC_NONE、_IOC_READ和_IOC_WRITE的定义就在上边,代码如下:
/*
* Direction bits, which any architecture can choose to override
* before including this file.
*
* NOTE: _IOC_WRITE means userland is writing and kernel is
* reading. _IOC_READ means userland is reading and kernel is writing.
*/
#ifndef _IOC_NONE
# define _IOC_NONE 0U
#endif
#ifndef _IOC_WRITE
# define _IOC_WRITE 1U
#endif
#ifndef _IOC_READ
# define _IOC_READ 2U
#endif
再看_IOC的定义,在同文件(include/uapi/asm-generic/ioctl.h)中:
#define _IOC(dir,type,nr,size) \
(((dir) << _IOC_DIRSHIFT) | \
((type) << _IOC_TYPESHIFT) | \
((nr) << _IOC_NRSHIFT) | \
((size) << _IOC_SIZESHIFT))
可见,_IO()、_IOR()、_IOW()、_IOWR()4个宏的最后结果由_IOC()中的4个参数移位组合而成。
_IOC_DIRSHIFT等4个移位参数的定义当然也在同文件中,代码如下:
#define _IOC_NRSHIFT 0
#define _IOC_TYPESHIFT (_IOC_NRSHIFT+_IOC_NRBITS)
#define _IOC_SIZESHIFT (_IOC_TYPESHIFT+_IOC_TYPEBITS)
#define _IOC_DIRSHIFT (_IOC_SIZESHIFT+_IOC_SIZEBITS)
_IOC_NRBITS、_IOC_TYPEBITS、_IOC_SIZEBITS宏的定义就在上边,如下:
/*
* The following is for compatibility across the various Linux
* platforms. The generic ioctl numbering scheme doesn't really enforce
* a type field. De facto, however, the top 8 bits of the lower 16
* bits are indeed used as a type field, so we might just as well make
* this explicit here. Please be sure to use the decoding macros
* below from now on.
*/
#define _IOC_NRBITS 8
#define _IOC_TYPEBITS 8
/*
* Let any architecture override either of the following before
* including this file.
*/
#ifndef _IOC_SIZEBITS
# define _IOC_SIZEBITS 14
#endif
#ifndef _IOC_DIRBITS
# define _IOC_DIRBITS 2
#endif
这些_IOC_XXXBITS以及_IOC_XXXSHIFT与上边介绍的cmd的4个域完全对应。
_IO() 、_IOR() 、IOW()和_IOWR()4个宏逐层展开如下:
- _IO(type,nr)
#define _IO(type,nr) _IOC(_IOC_NONE,(type),(nr),0)
--->
#define _IO(type,nr) _IOC(0,(type),(nr),0)
--->
#define _IO(type,nr) \
(((0) << 30) | \
((type) << 8) | \
((nr) << 0) | \
((0) << 16))
代入实际的调用:_IO('d',nr),最终为:
(((0) << 30) | \
(('d') << 8) | \
((nr) << 0) | \
((0) << 16))
整理成一行:
( ((0) << 30) | (('d') << 8) | ((nr) << 0) | ((0) << 16) )
- _IOR(type,nr,size)
#define _IOR(type,nr,size) _IOC(_IOC_READ,(type),(nr),(_IOC_TYPECHECK(size)))
--->
#define _IOR(type,nr,size) _IOC(2,(type),(nr),((sizeof(size)))
--->
#define _IOR(type,nr,size) \
(((2) << 30) | \
((type) << 8) | \
((nr) << 0) | \
((sizeof(size)) << 16))
代入实际的调用:_IOR('d',nr,type),最终为:
(((2) << 30) | \
(('d') << 8) | \
((nr) << 0) | \
((sizeof(size)) << 16))
整理成一行:
( ((2) << 30) | (('d') << 8) | ((nr) << 0) | ((sizeof(size)) << 16) )
其余两项不详细推了,直接给出结果。
- _IOW(type,nr,size)
#define _IOW(type,nr,size) \
(((1) << 30) | \
((type) << 8) | \
((nr) << 0) | \
((sizeof(size)) << 16))
代入实际的调用:_IOW('d',nr,type),最终为:
( ((1) << 30) | (('d') << 8) | ((nr) << 0) | ((sizeof(size)) << 16) )
- _IOWR(type,nr,size)
#define _IOWR(type,nr,size) \
(((3) << 30) | \
((type) << 8) | \
((nr) << 0) | \
((sizeof(size)) << 16))
代入实际的调用:_IOWR('d',nr,type),最终为:
( ((3) << 30) | (('d') << 8) | ((nr) << 0) | ((sizeof(size)) << 16) )