1 /*- 2 * Copyright (c) 2015 Dmitry Chagin <dchagin@FreeBSD.org> 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 16 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 23 * SUCH DAMAGE. 24 */ 25 26 #ifndef _LINUX_MI_H_ 27 #define _LINUX_MI_H_ 28 29 /* 30 * Machine independent set of types for the Linux types. 31 */ 32 typedef uint32_t l_dev_t; 33 34 /* 35 * Linux dev_t conversion routines. 36 * 37 * As of version 2.6.0 of the Linux kernel, dev_t is a 32-bit quantity 38 * with 12 bits set asaid for the major number and 20 for the minor number. 39 * The in-kernel dev_t encoded as MMMmmmmm, where M is a hex digit of the 40 * major number and m is a hex digit of the minor number. 41 * The user-space dev_t encoded as mmmM MMmm, where M and m is the major 42 * and minor numbers accordingly. This is downward compatible with legacy 43 * systems where dev_t is 16 bits wide, encoded as MMmm. 44 * In glibc dev_t is a 64-bit quantity, with 32-bit major and minor numbers, 45 * encoded as MMMM Mmmm mmmM MMmm. This is downward compatible with the Linux 46 * kernel and with legacy systems where dev_t is 16 bits wide. 47 * 48 * In the FreeBSD dev_t is a 64-bit quantity. The major and minor numbers 49 * are encoded as MMMmmmMm, therefore conversion of the device numbers between 50 * Linux user-space and FreeBSD kernel required. 51 */ 52 static __inline l_dev_t 53 linux_encode_dev(int _major, int _minor) 54 { 55 56 return ((_minor & 0xff) | ((_major & 0xfff) << 8) | 57 (((_minor & ~0xff) << 12) & 0xfff00000)); 58 } 59 60 static __inline l_dev_t 61 linux_new_encode_dev(dev_t _dev) 62 { 63 64 return (_dev == NODEV ? 0 : linux_encode_dev(major(_dev), minor(_dev))); 65 } 66 67 static __inline int 68 linux_encode_major(dev_t _dev) 69 { 70 71 return (_dev == NODEV ? 0 : major(_dev) & 0xfff); 72 } 73 74 static __inline int 75 linux_encode_minor(dev_t _dev) 76 { 77 78 return (_dev == NODEV ? 0 : minor(_dev) & 0xfffff); 79 } 80 81 static __inline int 82 linux_decode_major(l_dev_t _dev) 83 { 84 85 return ((_dev & 0xfff00) >> 8); 86 } 87 88 static __inline int 89 linux_decode_minor(l_dev_t _dev) 90 { 91 92 return ((_dev & 0xff) | ((_dev & 0xfff00000) >> 12)); 93 } 94 95 static __inline dev_t 96 linux_decode_dev(l_dev_t _dev) 97 { 98 99 return (makedev(linux_decode_major(_dev), linux_decode_minor(_dev))); 100 } 101 102 /* 103 * Private Brandinfo flags 104 */ 105 #define LINUX_BI_FUTEX_REQUEUE 0x01000000 106 107 /* 108 * poll() 109 */ 110 #define LINUX_POLLIN 0x0001 111 #define LINUX_POLLPRI 0x0002 112 #define LINUX_POLLOUT 0x0004 113 #define LINUX_POLLERR 0x0008 114 #define LINUX_POLLHUP 0x0010 115 #define LINUX_POLLNVAL 0x0020 116 #define LINUX_POLLRDNORM 0x0040 117 #define LINUX_POLLRDBAND 0x0080 118 #define LINUX_POLLWRNORM 0x0100 119 #define LINUX_POLLWRBAND 0x0200 120 #define LINUX_POLLMSG 0x0400 121 #define LINUX_POLLREMOVE 0x1000 122 #define LINUX_POLLRDHUP 0x2000 123 124 #define LINUX_IFHWADDRLEN 6 125 #define LINUX_IFNAMSIZ 16 126 127 struct l_sockaddr { 128 unsigned short sa_family; 129 char sa_data[14]; 130 }; 131 132 #define LINUX_ARPHRD_ETHER 1 133 #define LINUX_ARPHRD_LOOPBACK 772 134 135 /* 136 * Supported address families 137 */ 138 #define LINUX_AF_UNSPEC 0 139 #define LINUX_AF_UNIX 1 140 #define LINUX_AF_INET 2 141 #define LINUX_AF_AX25 3 142 #define LINUX_AF_IPX 4 143 #define LINUX_AF_APPLETALK 5 144 #define LINUX_AF_INET6 10 145 #define LINUX_AF_NETLINK 16 146 147 #define LINUX_NETLINK_ROUTE 0 148 #define LINUX_NETLINK_SOCK_DIAG 4 149 #define LINUX_NETLINK_NFLOG 5 150 #define LINUX_NETLINK_SELINUX 7 151 #define LINUX_NETLINK_AUDIT 9 152 #define LINUX_NETLINK_FIB_LOOKUP 10 153 #define LINUX_NETLINK_NETFILTER 12 154 #define LINUX_NETLINK_KOBJECT_UEVENT 15 155 156 /* 157 * net device flags 158 */ 159 #define LINUX_IFF_UP 0x0001 160 #define LINUX_IFF_BROADCAST 0x0002 161 #define LINUX_IFF_DEBUG 0x0004 162 #define LINUX_IFF_LOOPBACK 0x0008 163 #define LINUX_IFF_POINTOPOINT 0x0010 164 #define LINUX_IFF_NOTRAILERS 0x0020 165 #define LINUX_IFF_RUNNING 0x0040 166 #define LINUX_IFF_NOARP 0x0080 167 #define LINUX_IFF_PROMISC 0x0100 168 #define LINUX_IFF_ALLMULTI 0x0200 169 #define LINUX_IFF_MASTER 0x0400 170 #define LINUX_IFF_SLAVE 0x0800 171 #define LINUX_IFF_MULTICAST 0x1000 172 #define LINUX_IFF_PORTSEL 0x2000 173 #define LINUX_IFF_AUTOMEDIA 0x4000 174 #define LINUX_IFF_DYNAMIC 0x8000 175 176 /* sigaltstack */ 177 #define LINUX_SS_ONSTACK 1 178 #define LINUX_SS_DISABLE 2 179 180 int linux_to_bsd_sigaltstack(int lsa); 181 int bsd_to_linux_sigaltstack(int bsa); 182 183 /* sigset */ 184 typedef struct { 185 uint64_t __mask; 186 } l_sigset_t; 187 188 /* primitives to manipulate sigset_t */ 189 #define LINUX_SIGEMPTYSET(set) (set).__mask = 0 190 #define LINUX_SIGISMEMBER(set, sig) (1ULL & ((set).__mask >> _SIG_IDX(sig))) 191 #define LINUX_SIGADDSET(set, sig) (set).__mask |= 1ULL << _SIG_IDX(sig) 192 193 void linux_to_bsd_sigset(l_sigset_t *, sigset_t *); 194 void bsd_to_linux_sigset(sigset_t *, l_sigset_t *); 195 196 /* signaling */ 197 #define LINUX_SIGHUP 1 198 #define LINUX_SIGINT 2 199 #define LINUX_SIGQUIT 3 200 #define LINUX_SIGILL 4 201 #define LINUX_SIGTRAP 5 202 #define LINUX_SIGABRT 6 203 #define LINUX_SIGIOT LINUX_SIGABRT 204 #define LINUX_SIGBUS 7 205 #define LINUX_SIGFPE 8 206 #define LINUX_SIGKILL 9 207 #define LINUX_SIGUSR1 10 208 #define LINUX_SIGSEGV 11 209 #define LINUX_SIGUSR2 12 210 #define LINUX_SIGPIPE 13 211 #define LINUX_SIGALRM 14 212 #define LINUX_SIGTERM 15 213 #define LINUX_SIGSTKFLT 16 214 #define LINUX_SIGCHLD 17 215 #define LINUX_SIGCONT 18 216 #define LINUX_SIGSTOP 19 217 #define LINUX_SIGTSTP 20 218 #define LINUX_SIGTTIN 21 219 #define LINUX_SIGTTOU 22 220 #define LINUX_SIGURG 23 221 #define LINUX_SIGXCPU 24 222 #define LINUX_SIGXFSZ 25 223 #define LINUX_SIGVTALRM 26 224 #define LINUX_SIGPROF 27 225 #define LINUX_SIGWINCH 28 226 #define LINUX_SIGIO 29 227 #define LINUX_SIGPOLL LINUX_SIGIO 228 #define LINUX_SIGPWR 30 229 #define LINUX_SIGSYS 31 230 #define LINUX_SIGTBLSZ 31 231 #define LINUX_SIGRTMIN 32 232 #define LINUX_SIGRTMAX 64 233 234 #define LINUX_SIG_VALID(sig) ((sig) <= LINUX_SIGRTMAX && (sig) > 0) 235 236 int linux_to_bsd_signal(int sig); 237 int bsd_to_linux_signal(int sig); 238 239 /* sigprocmask actions */ 240 #define LINUX_SIG_BLOCK 0 241 #define LINUX_SIG_UNBLOCK 1 242 #define LINUX_SIG_SETMASK 2 243 244 void linux_dev_shm_create(void); 245 void linux_dev_shm_destroy(void); 246 247 /* 248 * mask=0 is not sensible for this application, so it will be taken to mean 249 * a mask equivalent to the value. Otherwise, (word & mask) == value maps to 250 * (word & ~mask) | value in a bitfield for the platform we're converting to. 251 */ 252 struct bsd_to_linux_bitmap { 253 int bsd_mask; 254 int bsd_value; 255 int linux_mask; 256 int linux_value; 257 }; 258 259 int bsd_to_linux_bits_(int value, struct bsd_to_linux_bitmap *bitmap, 260 size_t mapcnt, int no_value); 261 int linux_to_bsd_bits_(int value, struct bsd_to_linux_bitmap *bitmap, 262 size_t mapcnt, int no_value); 263 264 /* 265 * These functions are used for simplification of BSD <-> Linux bit conversions. 266 * Given `value`, a bit field, these functions will walk the given bitmap table 267 * and set the appropriate bits for the target platform. If any bits were 268 * successfully converted, then the return value is the equivalent of value 269 * represented with the bit values appropriate for the target platform. 270 * Otherwise, the value supplied as `no_value` is returned. 271 */ 272 #define bsd_to_linux_bits(_val, _bmap, _noval) \ 273 bsd_to_linux_bits_((_val), (_bmap), nitems((_bmap)), (_noval)) 274 #define linux_to_bsd_bits(_val, _bmap, _noval) \ 275 linux_to_bsd_bits_((_val), (_bmap), nitems((_bmap)), (_noval)) 276 277 /* 278 * Easy mapping helpers. BITMAP_EASY_LINUX represents a single bit to be 279 * translated, and the FreeBSD and Linux values are supplied. BITMAP_1t1_LINUX 280 * is the extreme version of this, where not only is it a single bit, but the 281 * name of the macro used to represent the Linux version of a bit literally has 282 * LINUX_ prepended to the normal name. 283 */ 284 #define BITMAP_EASY_LINUX(_name, _linux_name) \ 285 { \ 286 .bsd_value = (_name), \ 287 .linux_value = (_linux_name), \ 288 } 289 #define BITMAP_1t1_LINUX(_name) BITMAP_EASY_LINUX(_name, LINUX_##_name) 290 291 int bsd_to_linux_errno(int error); 292 void linux_check_errtbl(void); 293 294 #define STATX_BASIC_STATS 0x07ff 295 #define STATX_BTIME 0x0800 296 #define STATX_ALL 0x0fff 297 298 #define STATX_ATTR_COMPRESSED 0x0004 299 #define STATX_ATTR_IMMUTABLE 0x0010 300 #define STATX_ATTR_APPEND 0x0020 301 #define STATX_ATTR_NODUMP 0x0040 302 #define STATX_ATTR_ENCRYPTED 0x0800 303 #define STATX_ATTR_AUTOMOUNT 0x1000 304 305 struct l_statx_timestamp { 306 int64_t tv_sec; 307 int32_t tv_nsec; 308 int32_t __spare0; 309 }; 310 311 struct l_statx { 312 uint32_t stx_mask; 313 uint32_t stx_blksize; 314 uint64_t stx_attributes; 315 uint32_t stx_nlink; 316 uint32_t stx_uid; 317 uint32_t stx_gid; 318 uint16_t stx_mode; 319 uint16_t __spare0[1]; 320 uint64_t stx_ino; 321 uint64_t stx_size; 322 uint64_t stx_blocks; 323 uint64_t stx_attributes_mask; 324 struct l_statx_timestamp stx_atime; 325 struct l_statx_timestamp stx_btime; 326 struct l_statx_timestamp stx_ctime; 327 struct l_statx_timestamp stx_mtime; 328 uint32_t stx_rdev_major; 329 uint32_t stx_rdev_minor; 330 uint32_t stx_dev_major; 331 uint32_t stx_dev_minor; 332 uint64_t stx_mnt_id; 333 uint64_t __spare2[13]; 334 }; 335 336 /* 337 * statfs f_flags 338 */ 339 #define LINUX_ST_RDONLY 0x0001 340 #define LINUX_ST_NOSUID 0x0002 341 #define LINUX_ST_NODEV 0x0004 /* No native analogue */ 342 #define LINUX_ST_NOEXEC 0x0008 343 #define LINUX_ST_SYNCHRONOUS 0x0010 344 #define LINUX_ST_VALID 0x0020 345 #define LINUX_ST_MANDLOCK 0x0040 /* No native analogue */ 346 #define LINUX_ST_NOATIME 0x0400 347 #define LINUX_ST_NODIRATIME 0x0800 /* No native analogue */ 348 #define LINUX_ST_RELATIME 0x1000 /* No native analogue */ 349 #define LINUX_ST_NOSYMFOLLOW 0x2000 350 351 #define lower_32_bits(n) ((uint32_t)((n) & 0xffffffff)) 352 353 #ifdef KTRACE 354 #define linux_ktrsigset(s, l) \ 355 ktrstruct("l_sigset_t", (s), l) 356 #endif 357 358 /* 359 * Criteria for interface name translation 360 */ 361 #define IFP_IS_ETH(ifp) (if_gettype(ifp) == IFT_ETHER) 362 #define IFP_IS_LOOP(ifp) (if_gettype(ifp) == IFT_LOOP) 363 364 struct ifnet; 365 366 bool linux_use_real_ifname(const struct ifnet *); 367 368 void linux_netlink_register(void); 369 void linux_netlink_deregister(void); 370 371 #endif /* _LINUX_MI_H_ */ 372