1 /* 2 * OS includes and handling of OS dependencies 3 * 4 * This header exists to pull in some common system headers that 5 * most code in QEMU will want, and to fix up some possible issues with 6 * it (missing defines, Windows weirdness, and so on). 7 * 8 * To avoid getting into possible circular include dependencies, this 9 * file should not include any other QEMU headers, with the exceptions 10 * of config-host.h, config-target.h, qemu/compiler.h, 11 * sysemu/os-posix.h, sysemu/os-win32.h, glib-compat.h and 12 * qemu/typedefs.h, all of which are doing a similar job to this file 13 * and are under similar constraints. 14 * 15 * This header also contains prototypes for functions defined in 16 * os-*.c and util/oslib-*.c; those would probably be better split 17 * out into separate header files. 18 * 19 * In an ideal world this header would contain only: 20 * (1) things which everybody needs 21 * (2) things without which code would work on most platforms but 22 * fail to compile or misbehave on a minority of host OSes 23 * 24 * This work is licensed under the terms of the GNU GPL, version 2 or later. 25 * See the COPYING file in the top-level directory. 26 */ 27 #ifndef QEMU_OSDEP_H 28 #define QEMU_OSDEP_H 29 30 #include "config-host.h" 31 #ifdef NEED_CPU_H 32 #include "config-target.h" 33 #else 34 #include "exec/poison.h" 35 #endif 36 #ifdef __COVERITY__ 37 /* Coverity does not like the new _Float* types that are used by 38 * recent glibc, and croaks on every single file that includes 39 * stdlib.h. These typedefs are enough to please it. 40 * 41 * Note that these fix parse errors so they cannot be placed in 42 * scripts/coverity-model.c. 43 */ 44 typedef float _Float32; 45 typedef double _Float32x; 46 typedef double _Float64; 47 typedef __float80 _Float64x; 48 typedef __float128 _Float128; 49 #endif 50 51 #include "qemu/compiler.h" 52 53 /* Older versions of C++ don't get definitions of various macros from 54 * stdlib.h unless we define these macros before first inclusion of 55 * that system header. 56 */ 57 #ifndef __STDC_CONSTANT_MACROS 58 #define __STDC_CONSTANT_MACROS 59 #endif 60 #ifndef __STDC_LIMIT_MACROS 61 #define __STDC_LIMIT_MACROS 62 #endif 63 #ifndef __STDC_FORMAT_MACROS 64 #define __STDC_FORMAT_MACROS 65 #endif 66 67 /* The following block of code temporarily renames the daemon() function so the 68 * compiler does not see the warning associated with it in stdlib.h on OSX 69 */ 70 #ifdef __APPLE__ 71 #define daemon qemu_fake_daemon_function 72 #include <stdlib.h> 73 #undef daemon 74 extern int daemon(int, int); 75 #endif 76 77 #ifdef _WIN32 78 /* as defined in sdkddkver.h */ 79 #ifndef _WIN32_WINNT 80 #define _WIN32_WINNT 0x0600 /* Vista */ 81 #endif 82 /* reduces the number of implicitly included headers */ 83 #ifndef WIN32_LEAN_AND_MEAN 84 #define WIN32_LEAN_AND_MEAN 85 #endif 86 #endif 87 88 #include <stdarg.h> 89 #include <stddef.h> 90 #include <stdbool.h> 91 #include <stdint.h> 92 #include <sys/types.h> 93 #include <stdlib.h> 94 95 /* enable C99/POSIX format strings (needs mingw32-runtime 3.15 or later) */ 96 #ifdef __MINGW32__ 97 #define __USE_MINGW_ANSI_STDIO 1 98 #endif 99 #include <stdio.h> 100 101 #include <string.h> 102 #include <strings.h> 103 #include <inttypes.h> 104 #include <limits.h> 105 /* Put unistd.h before time.h as that triggers localtime_r/gmtime_r 106 * function availability on recentish Mingw-w64 platforms. */ 107 #include <unistd.h> 108 #include <time.h> 109 #include <ctype.h> 110 #include <errno.h> 111 #include <fcntl.h> 112 #include <sys/stat.h> 113 #include <sys/time.h> 114 #include <assert.h> 115 /* setjmp must be declared before sysemu/os-win32.h 116 * because it is redefined there. */ 117 #include <setjmp.h> 118 #include <signal.h> 119 120 #ifdef __OpenBSD__ 121 #include <sys/signal.h> 122 #endif 123 124 #ifndef _WIN32 125 #include <sys/wait.h> 126 #else 127 #define WIFEXITED(x) 1 128 #define WEXITSTATUS(x) (x) 129 #endif 130 131 #ifdef _WIN32 132 #include "sysemu/os-win32.h" 133 #endif 134 135 #ifdef CONFIG_POSIX 136 #include "sysemu/os-posix.h" 137 #endif 138 139 #include "glib-compat.h" 140 #include "qemu/typedefs.h" 141 142 /* 143 * For mingw, as of v6.0.0, the function implementing the assert macro is 144 * not marked as noreturn, so the compiler cannot delete code following an 145 * assert(false) as unused. We rely on this within the code base to delete 146 * code that is unreachable when features are disabled. 147 * All supported versions of Glib's g_assert() satisfy this requirement. 148 */ 149 #ifdef __MINGW32__ 150 #undef assert 151 #define assert(x) g_assert(x) 152 #endif 153 154 /* 155 * According to waitpid man page: 156 * WCOREDUMP 157 * This macro is not specified in POSIX.1-2001 and is not 158 * available on some UNIX implementations (e.g., AIX, SunOS). 159 * Therefore, enclose its use inside #ifdef WCOREDUMP ... #endif. 160 */ 161 #ifndef WCOREDUMP 162 #define WCOREDUMP(status) 0 163 #endif 164 /* 165 * We have a lot of unaudited code that may fail in strange ways, or 166 * even be a security risk during migration, if you disable assertions 167 * at compile-time. You may comment out these safety checks if you 168 * absolutely want to disable assertion overhead, but it is not 169 * supported upstream so the risk is all yours. Meanwhile, please 170 * submit patches to remove any side-effects inside an assertion, or 171 * fixing error handling that should use Error instead of assert. 172 */ 173 #ifdef NDEBUG 174 #error building with NDEBUG is not supported 175 #endif 176 #ifdef G_DISABLE_ASSERT 177 #error building with G_DISABLE_ASSERT is not supported 178 #endif 179 180 #ifndef O_LARGEFILE 181 #define O_LARGEFILE 0 182 #endif 183 #ifndef O_BINARY 184 #define O_BINARY 0 185 #endif 186 #ifndef MAP_ANONYMOUS 187 #define MAP_ANONYMOUS MAP_ANON 188 #endif 189 #ifndef ENOMEDIUM 190 #define ENOMEDIUM ENODEV 191 #endif 192 #if !defined(ENOTSUP) 193 #define ENOTSUP 4096 194 #endif 195 #if !defined(ECANCELED) 196 #define ECANCELED 4097 197 #endif 198 #if !defined(EMEDIUMTYPE) 199 #define EMEDIUMTYPE 4098 200 #endif 201 #if !defined(ESHUTDOWN) 202 #define ESHUTDOWN 4099 203 #endif 204 205 /* time_t may be either 32 or 64 bits depending on the host OS, and 206 * can be either signed or unsigned, so we can't just hardcode a 207 * specific maximum value. This is not a C preprocessor constant, 208 * so you can't use TIME_MAX in an #ifdef, but for our purposes 209 * this isn't a problem. 210 */ 211 212 /* The macros TYPE_SIGNED, TYPE_WIDTH, and TYPE_MAXIMUM are from 213 * Gnulib, and are under the LGPL v2.1 or (at your option) any 214 * later version. 215 */ 216 217 /* True if the real type T is signed. */ 218 #define TYPE_SIGNED(t) (!((t)0 < (t)-1)) 219 220 /* The width in bits of the integer type or expression T. 221 * Padding bits are not supported. 222 */ 223 #define TYPE_WIDTH(t) (sizeof(t) * CHAR_BIT) 224 225 /* The maximum and minimum values for the integer type T. */ 226 #define TYPE_MAXIMUM(t) \ 227 ((t) (!TYPE_SIGNED(t) \ 228 ? (t)-1 \ 229 : ((((t)1 << (TYPE_WIDTH(t) - 2)) - 1) * 2 + 1))) 230 231 #ifndef TIME_MAX 232 #define TIME_MAX TYPE_MAXIMUM(time_t) 233 #endif 234 235 /* HOST_LONG_BITS is the size of a native pointer in bits. */ 236 #if UINTPTR_MAX == UINT32_MAX 237 # define HOST_LONG_BITS 32 238 #elif UINTPTR_MAX == UINT64_MAX 239 # define HOST_LONG_BITS 64 240 #else 241 # error Unknown pointer size 242 #endif 243 244 /* Mac OSX has a <stdint.h> bug that incorrectly defines SIZE_MAX with 245 * the wrong type. Our replacement isn't usable in preprocessor 246 * expressions, but it is sufficient for our needs. */ 247 #if defined(HAVE_BROKEN_SIZE_MAX) && HAVE_BROKEN_SIZE_MAX 248 #undef SIZE_MAX 249 #define SIZE_MAX ((size_t)-1) 250 #endif 251 252 #ifndef MIN 253 #define MIN(a, b) (((a) < (b)) ? (a) : (b)) 254 #endif 255 #ifndef MAX 256 #define MAX(a, b) (((a) > (b)) ? (a) : (b)) 257 #endif 258 259 /* Minimum function that returns zero only iff both values are zero. 260 * Intended for use with unsigned values only. */ 261 #ifndef MIN_NON_ZERO 262 #define MIN_NON_ZERO(a, b) ((a) == 0 ? (b) : \ 263 ((b) == 0 ? (a) : (MIN(a, b)))) 264 #endif 265 266 /* Round number down to multiple */ 267 #define QEMU_ALIGN_DOWN(n, m) ((n) / (m) * (m)) 268 269 /* Round number up to multiple. Safe when m is not a power of 2 (see 270 * ROUND_UP for a faster version when a power of 2 is guaranteed) */ 271 #define QEMU_ALIGN_UP(n, m) QEMU_ALIGN_DOWN((n) + (m) - 1, (m)) 272 273 /* Check if n is a multiple of m */ 274 #define QEMU_IS_ALIGNED(n, m) (((n) % (m)) == 0) 275 276 /* n-byte align pointer down */ 277 #define QEMU_ALIGN_PTR_DOWN(p, n) \ 278 ((typeof(p))QEMU_ALIGN_DOWN((uintptr_t)(p), (n))) 279 280 /* n-byte align pointer up */ 281 #define QEMU_ALIGN_PTR_UP(p, n) \ 282 ((typeof(p))QEMU_ALIGN_UP((uintptr_t)(p), (n))) 283 284 /* Check if pointer p is n-bytes aligned */ 285 #define QEMU_PTR_IS_ALIGNED(p, n) QEMU_IS_ALIGNED((uintptr_t)(p), (n)) 286 287 /* Round number up to multiple. Requires that d be a power of 2 (see 288 * QEMU_ALIGN_UP for a safer but slower version on arbitrary 289 * numbers); works even if d is a smaller type than n. */ 290 #ifndef ROUND_UP 291 #define ROUND_UP(n, d) (((n) + (d) - 1) & -(0 ? (n) : (d))) 292 #endif 293 294 #ifndef DIV_ROUND_UP 295 #define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d)) 296 #endif 297 298 /* 299 * &(x)[0] is always a pointer - if it's same type as x then the argument is a 300 * pointer, not an array. 301 */ 302 #define QEMU_IS_ARRAY(x) (!__builtin_types_compatible_p(typeof(x), \ 303 typeof(&(x)[0]))) 304 #ifndef ARRAY_SIZE 305 #define ARRAY_SIZE(x) ((sizeof(x) / sizeof((x)[0])) + \ 306 QEMU_BUILD_BUG_ON_ZERO(!QEMU_IS_ARRAY(x))) 307 #endif 308 309 int qemu_daemon(int nochdir, int noclose); 310 void *qemu_try_memalign(size_t alignment, size_t size); 311 void *qemu_memalign(size_t alignment, size_t size); 312 void *qemu_anon_ram_alloc(size_t size, uint64_t *align, bool shared); 313 void qemu_vfree(void *ptr); 314 void qemu_anon_ram_free(void *ptr, size_t size); 315 316 #define QEMU_MADV_INVALID -1 317 318 #if defined(CONFIG_MADVISE) 319 320 #define QEMU_MADV_WILLNEED MADV_WILLNEED 321 #define QEMU_MADV_DONTNEED MADV_DONTNEED 322 #ifdef MADV_DONTFORK 323 #define QEMU_MADV_DONTFORK MADV_DONTFORK 324 #else 325 #define QEMU_MADV_DONTFORK QEMU_MADV_INVALID 326 #endif 327 #ifdef MADV_MERGEABLE 328 #define QEMU_MADV_MERGEABLE MADV_MERGEABLE 329 #else 330 #define QEMU_MADV_MERGEABLE QEMU_MADV_INVALID 331 #endif 332 #ifdef MADV_UNMERGEABLE 333 #define QEMU_MADV_UNMERGEABLE MADV_UNMERGEABLE 334 #else 335 #define QEMU_MADV_UNMERGEABLE QEMU_MADV_INVALID 336 #endif 337 #ifdef MADV_DODUMP 338 #define QEMU_MADV_DODUMP MADV_DODUMP 339 #else 340 #define QEMU_MADV_DODUMP QEMU_MADV_INVALID 341 #endif 342 #ifdef MADV_DONTDUMP 343 #define QEMU_MADV_DONTDUMP MADV_DONTDUMP 344 #else 345 #define QEMU_MADV_DONTDUMP QEMU_MADV_INVALID 346 #endif 347 #ifdef MADV_HUGEPAGE 348 #define QEMU_MADV_HUGEPAGE MADV_HUGEPAGE 349 #else 350 #define QEMU_MADV_HUGEPAGE QEMU_MADV_INVALID 351 #endif 352 #ifdef MADV_NOHUGEPAGE 353 #define QEMU_MADV_NOHUGEPAGE MADV_NOHUGEPAGE 354 #else 355 #define QEMU_MADV_NOHUGEPAGE QEMU_MADV_INVALID 356 #endif 357 #ifdef MADV_REMOVE 358 #define QEMU_MADV_REMOVE MADV_REMOVE 359 #else 360 #define QEMU_MADV_REMOVE QEMU_MADV_INVALID 361 #endif 362 363 #elif defined(CONFIG_POSIX_MADVISE) 364 365 #define QEMU_MADV_WILLNEED POSIX_MADV_WILLNEED 366 #define QEMU_MADV_DONTNEED POSIX_MADV_DONTNEED 367 #define QEMU_MADV_DONTFORK QEMU_MADV_INVALID 368 #define QEMU_MADV_MERGEABLE QEMU_MADV_INVALID 369 #define QEMU_MADV_UNMERGEABLE QEMU_MADV_INVALID 370 #define QEMU_MADV_DODUMP QEMU_MADV_INVALID 371 #define QEMU_MADV_DONTDUMP QEMU_MADV_INVALID 372 #define QEMU_MADV_HUGEPAGE QEMU_MADV_INVALID 373 #define QEMU_MADV_NOHUGEPAGE QEMU_MADV_INVALID 374 #define QEMU_MADV_REMOVE QEMU_MADV_INVALID 375 376 #else /* no-op */ 377 378 #define QEMU_MADV_WILLNEED QEMU_MADV_INVALID 379 #define QEMU_MADV_DONTNEED QEMU_MADV_INVALID 380 #define QEMU_MADV_DONTFORK QEMU_MADV_INVALID 381 #define QEMU_MADV_MERGEABLE QEMU_MADV_INVALID 382 #define QEMU_MADV_UNMERGEABLE QEMU_MADV_INVALID 383 #define QEMU_MADV_DODUMP QEMU_MADV_INVALID 384 #define QEMU_MADV_DONTDUMP QEMU_MADV_INVALID 385 #define QEMU_MADV_HUGEPAGE QEMU_MADV_INVALID 386 #define QEMU_MADV_NOHUGEPAGE QEMU_MADV_INVALID 387 #define QEMU_MADV_REMOVE QEMU_MADV_INVALID 388 389 #endif 390 391 #ifdef _WIN32 392 #define HAVE_CHARDEV_SERIAL 1 393 #elif defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \ 394 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \ 395 || defined(__GLIBC__) 396 #define HAVE_CHARDEV_SERIAL 1 397 #endif 398 399 #if defined(__linux__) || defined(__FreeBSD__) || \ 400 defined(__FreeBSD_kernel__) || defined(__DragonFly__) 401 #define HAVE_CHARDEV_PARPORT 1 402 #endif 403 404 #if defined(CONFIG_LINUX) 405 #ifndef BUS_MCEERR_AR 406 #define BUS_MCEERR_AR 4 407 #endif 408 #ifndef BUS_MCEERR_AO 409 #define BUS_MCEERR_AO 5 410 #endif 411 #endif 412 413 #if defined(__linux__) && \ 414 (defined(__x86_64__) || defined(__arm__) || defined(__aarch64__) \ 415 || defined(__powerpc64__)) 416 /* Use 2 MiB alignment so transparent hugepages can be used by KVM. 417 Valgrind does not support alignments larger than 1 MiB, 418 therefore we need special code which handles running on Valgrind. */ 419 # define QEMU_VMALLOC_ALIGN (512 * 4096) 420 #elif defined(__linux__) && defined(__s390x__) 421 /* Use 1 MiB (segment size) alignment so gmap can be used by KVM. */ 422 # define QEMU_VMALLOC_ALIGN (256 * 4096) 423 #elif defined(__linux__) && defined(__sparc__) 424 #include <sys/shm.h> 425 # define QEMU_VMALLOC_ALIGN MAX(getpagesize(), SHMLBA) 426 #else 427 # define QEMU_VMALLOC_ALIGN getpagesize() 428 #endif 429 430 #ifdef CONFIG_POSIX 431 struct qemu_signalfd_siginfo { 432 uint32_t ssi_signo; /* Signal number */ 433 int32_t ssi_errno; /* Error number (unused) */ 434 int32_t ssi_code; /* Signal code */ 435 uint32_t ssi_pid; /* PID of sender */ 436 uint32_t ssi_uid; /* Real UID of sender */ 437 int32_t ssi_fd; /* File descriptor (SIGIO) */ 438 uint32_t ssi_tid; /* Kernel timer ID (POSIX timers) */ 439 uint32_t ssi_band; /* Band event (SIGIO) */ 440 uint32_t ssi_overrun; /* POSIX timer overrun count */ 441 uint32_t ssi_trapno; /* Trap number that caused signal */ 442 int32_t ssi_status; /* Exit status or signal (SIGCHLD) */ 443 int32_t ssi_int; /* Integer sent by sigqueue(2) */ 444 uint64_t ssi_ptr; /* Pointer sent by sigqueue(2) */ 445 uint64_t ssi_utime; /* User CPU time consumed (SIGCHLD) */ 446 uint64_t ssi_stime; /* System CPU time consumed (SIGCHLD) */ 447 uint64_t ssi_addr; /* Address that generated signal 448 (for hardware-generated signals) */ 449 uint8_t pad[48]; /* Pad size to 128 bytes (allow for 450 additional fields in the future) */ 451 }; 452 453 int qemu_signalfd(const sigset_t *mask); 454 void sigaction_invoke(struct sigaction *action, 455 struct qemu_signalfd_siginfo *info); 456 #endif 457 458 int qemu_madvise(void *addr, size_t len, int advice); 459 int qemu_mprotect_rwx(void *addr, size_t size); 460 int qemu_mprotect_none(void *addr, size_t size); 461 462 int qemu_open(const char *name, int flags, ...); 463 int qemu_close(int fd); 464 #ifndef _WIN32 465 int qemu_dup(int fd); 466 #endif 467 int qemu_lock_fd(int fd, int64_t start, int64_t len, bool exclusive); 468 int qemu_unlock_fd(int fd, int64_t start, int64_t len); 469 int qemu_lock_fd_test(int fd, int64_t start, int64_t len, bool exclusive); 470 bool qemu_has_ofd_lock(void); 471 472 #if defined(__HAIKU__) && defined(__i386__) 473 #define FMT_pid "%ld" 474 #elif defined(WIN64) 475 #define FMT_pid "%" PRId64 476 #else 477 #define FMT_pid "%d" 478 #endif 479 480 bool qemu_write_pidfile(const char *pidfile, Error **errp); 481 482 int qemu_get_thread_id(void); 483 484 #ifndef CONFIG_IOVEC 485 struct iovec { 486 void *iov_base; 487 size_t iov_len; 488 }; 489 /* 490 * Use the same value as Linux for now. 491 */ 492 #define IOV_MAX 1024 493 494 ssize_t readv(int fd, const struct iovec *iov, int iov_cnt); 495 ssize_t writev(int fd, const struct iovec *iov, int iov_cnt); 496 #else 497 #include <sys/uio.h> 498 #endif 499 500 #ifdef _WIN32 501 static inline void qemu_timersub(const struct timeval *val1, 502 const struct timeval *val2, 503 struct timeval *res) 504 { 505 res->tv_sec = val1->tv_sec - val2->tv_sec; 506 if (val1->tv_usec < val2->tv_usec) { 507 res->tv_sec--; 508 res->tv_usec = val1->tv_usec - val2->tv_usec + 1000 * 1000; 509 } else { 510 res->tv_usec = val1->tv_usec - val2->tv_usec; 511 } 512 } 513 #else 514 #define qemu_timersub timersub 515 #endif 516 517 void qemu_set_cloexec(int fd); 518 519 /* Starting on QEMU 2.5, qemu_hw_version() returns "2.5+" by default 520 * instead of QEMU_VERSION, so setting hw_version on MachineClass 521 * is no longer mandatory. 522 * 523 * Do NOT change this string, or it will break compatibility on all 524 * machine classes that don't set hw_version. 525 */ 526 #define QEMU_HW_VERSION "2.5+" 527 528 /* QEMU "hardware version" setting. Used to replace code that exposed 529 * QEMU_VERSION to guests in the past and need to keep compatibility. 530 * Do not use qemu_hw_version() in new code. 531 */ 532 void qemu_set_hw_version(const char *); 533 const char *qemu_hw_version(void); 534 535 void fips_set_state(bool requested); 536 bool fips_get_state(void); 537 538 /* Return a dynamically allocated pathname denoting a file or directory that is 539 * appropriate for storing local state. 540 * 541 * @relative_pathname need not start with a directory separator; one will be 542 * added automatically. 543 * 544 * The caller is responsible for releasing the value returned with g_free() 545 * after use. 546 */ 547 char *qemu_get_local_state_pathname(const char *relative_pathname); 548 549 /* Find program directory, and save it for later usage with 550 * qemu_get_exec_dir(). 551 * Try OS specific API first, if not working, parse from argv0. */ 552 void qemu_init_exec_dir(const char *argv0); 553 554 /* Get the saved exec dir. 555 * Caller needs to release the returned string by g_free() */ 556 char *qemu_get_exec_dir(void); 557 558 /** 559 * qemu_getauxval: 560 * @type: the auxiliary vector key to lookup 561 * 562 * Search the auxiliary vector for @type, returning the value 563 * or 0 if @type is not present. 564 */ 565 unsigned long qemu_getauxval(unsigned long type); 566 567 void qemu_set_tty_echo(int fd, bool echo); 568 569 void os_mem_prealloc(int fd, char *area, size_t sz, int smp_cpus, 570 Error **errp); 571 572 /** 573 * qemu_get_pid_name: 574 * @pid: pid of a process 575 * 576 * For given @pid fetch its name. Caller is responsible for 577 * freeing the string when no longer needed. 578 * Returns allocated string on success, NULL on failure. 579 */ 580 char *qemu_get_pid_name(pid_t pid); 581 582 /** 583 * qemu_fork: 584 * 585 * A version of fork that avoids signal handler race 586 * conditions that can lead to child process getting 587 * signals that are otherwise only expected by the 588 * parent. It also resets all signal handlers to the 589 * default settings. 590 * 591 * Returns 0 to child process, pid number to parent 592 * or -1 on failure. 593 */ 594 pid_t qemu_fork(Error **errp); 595 596 /* Using intptr_t ensures that qemu_*_page_mask is sign-extended even 597 * when intptr_t is 32-bit and we are aligning a long long. 598 */ 599 extern uintptr_t qemu_real_host_page_size; 600 extern intptr_t qemu_real_host_page_mask; 601 602 extern int qemu_icache_linesize; 603 extern int qemu_icache_linesize_log; 604 extern int qemu_dcache_linesize; 605 extern int qemu_dcache_linesize_log; 606 607 #endif 608