|
| 1 | +// SPDX-FileCopyrightText: Linux kernel contributors |
| 2 | +// |
| 3 | +// SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note |
| 4 | + |
| 5 | +/* |
| 6 | + * Vendored linux/ioctl.h for portable ioctl macros. |
| 7 | + * |
| 8 | + * This provides the _IO/_IOR/_IOW/_IOWR macros that accept struct types |
| 9 | + * and compute ioctl numbers based on sizeof(). These are needed by |
| 10 | + * tdx_attest.c and work consistently across glibc and MUSL. |
| 11 | + * |
| 12 | + * Based on: Linux kernel include/uapi/asm-generic/ioctl.h |
| 13 | + */ |
| 14 | + |
| 15 | +#ifndef _LINUX_IOCTL_H |
| 16 | +#define _LINUX_IOCTL_H |
| 17 | + |
| 18 | +/* ioctl command encoding: 32 bits total, command in lower 16 bits, |
| 19 | + * size of the parameter structure in the lower 14 bits of the |
| 20 | + * upper 16 bits. |
| 21 | + */ |
| 22 | + |
| 23 | +#define _IOC_NRBITS 8 |
| 24 | +#define _IOC_TYPEBITS 8 |
| 25 | +#define _IOC_SIZEBITS 14 |
| 26 | +#define _IOC_DIRBITS 2 |
| 27 | + |
| 28 | +#define _IOC_NRMASK ((1 << _IOC_NRBITS)-1) |
| 29 | +#define _IOC_TYPEMASK ((1 << _IOC_TYPEBITS)-1) |
| 30 | +#define _IOC_SIZEMASK ((1 << _IOC_SIZEBITS)-1) |
| 31 | +#define _IOC_DIRMASK ((1 << _IOC_DIRBITS)-1) |
| 32 | + |
| 33 | +#define _IOC_NRSHIFT 0 |
| 34 | +#define _IOC_TYPESHIFT (_IOC_NRSHIFT+_IOC_NRBITS) |
| 35 | +#define _IOC_SIZESHIFT (_IOC_TYPESHIFT+_IOC_TYPEBITS) |
| 36 | +#define _IOC_DIRSHIFT (_IOC_SIZESHIFT+_IOC_SIZEBITS) |
| 37 | + |
| 38 | +/* Direction bits */ |
| 39 | +#define _IOC_NONE 0U |
| 40 | +#define _IOC_WRITE 1U |
| 41 | +#define _IOC_READ 2U |
| 42 | + |
| 43 | +#define _IOC(dir,type,nr,size) \ |
| 44 | + (((dir) << _IOC_DIRSHIFT) | \ |
| 45 | + ((type) << _IOC_TYPESHIFT) | \ |
| 46 | + ((nr) << _IOC_NRSHIFT) | \ |
| 47 | + ((size) << _IOC_SIZESHIFT)) |
| 48 | + |
| 49 | +#define _IOC_TYPECHECK(t) (sizeof(t)) |
| 50 | + |
| 51 | +/* Used to create numbers. |
| 52 | + * NOTE: _IOW means userland is writing and kernel is reading. |
| 53 | + * _IOR means userland is reading and kernel is writing. |
| 54 | + */ |
| 55 | +#define _IO(type,nr) _IOC(_IOC_NONE,(type),(nr),0) |
| 56 | +#define _IOR(type,nr,size) _IOC(_IOC_READ,(type),(nr),(_IOC_TYPECHECK(size))) |
| 57 | +#define _IOW(type,nr,size) _IOC(_IOC_WRITE,(type),(nr),(_IOC_TYPECHECK(size))) |
| 58 | +#define _IOWR(type,nr,size) _IOC(_IOC_READ|_IOC_WRITE,(type),(nr),(_IOC_TYPECHECK(size))) |
| 59 | + |
| 60 | +/* Used to decode ioctl numbers */ |
| 61 | +#define _IOC_DIR(nr) (((nr) >> _IOC_DIRSHIFT) & _IOC_DIRMASK) |
| 62 | +#define _IOC_TYPE(nr) (((nr) >> _IOC_TYPESHIFT) & _IOC_TYPEMASK) |
| 63 | +#define _IOC_NR(nr) (((nr) >> _IOC_NRSHIFT) & _IOC_NRMASK) |
| 64 | +#define _IOC_SIZE(nr) (((nr) >> _IOC_SIZESHIFT) & _IOC_SIZEMASK) |
| 65 | + |
| 66 | +#endif /* _LINUX_IOCTL_H */ |
0 commit comments