osdep.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819
  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. #if !defined _FORTIFY_SOURCE && defined __OPTIMIZE__ && __OPTIMIZE__ && defined __linux__
  30. # define _FORTIFY_SOURCE 2
  31. #endif
  32. #include "config-host.h"
  33. #ifdef COMPILING_PER_TARGET
  34. #include CONFIG_TARGET
  35. #else
  36. #include "exec/poison.h"
  37. #endif
  38. /*
  39. * HOST_WORDS_BIGENDIAN was replaced with HOST_BIG_ENDIAN. Prevent it from
  40. * creeping back in.
  41. */
  42. #pragma GCC poison HOST_WORDS_BIGENDIAN
  43. /*
  44. * TARGET_WORDS_BIGENDIAN was replaced with TARGET_BIG_ENDIAN. Prevent it from
  45. * creeping back in.
  46. */
  47. #pragma GCC poison TARGET_WORDS_BIGENDIAN
  48. #include "qemu/compiler.h"
  49. /* Older versions of C++ don't get definitions of various macros from
  50. * stdlib.h unless we define these macros before first inclusion of
  51. * that system header.
  52. */
  53. #ifndef __STDC_CONSTANT_MACROS
  54. #define __STDC_CONSTANT_MACROS
  55. #endif
  56. #ifndef __STDC_LIMIT_MACROS
  57. #define __STDC_LIMIT_MACROS
  58. #endif
  59. #ifndef __STDC_FORMAT_MACROS
  60. #define __STDC_FORMAT_MACROS
  61. #endif
  62. /* The following block of code temporarily renames the daemon() function so the
  63. * compiler does not see the warning associated with it in stdlib.h on OSX
  64. */
  65. #ifdef __APPLE__
  66. #define daemon qemu_fake_daemon_function
  67. #include <stdlib.h>
  68. #undef daemon
  69. QEMU_EXTERN_C int daemon(int, int);
  70. #endif
  71. #ifdef _WIN32
  72. /* as defined in sdkddkver.h */
  73. #ifndef _WIN32_WINNT
  74. #define _WIN32_WINNT 0x0602 /* Windows 8 API (should be >= the one from glib) */
  75. #endif
  76. /* reduces the number of implicitly included headers */
  77. #ifndef WIN32_LEAN_AND_MEAN
  78. #define WIN32_LEAN_AND_MEAN
  79. #endif
  80. #endif
  81. /* enable C99/POSIX format strings (needs mingw32-runtime 3.15 or later) */
  82. #ifdef __MINGW32__
  83. #define __USE_MINGW_ANSI_STDIO 1
  84. #endif
  85. /*
  86. * We need the FreeBSD "legacy" definitions. Rust needs the FreeBSD 11 system
  87. * calls since it doesn't use libc at all, so we have to emulate that despite
  88. * FreeBSD 11 being EOL'd.
  89. */
  90. #ifdef __FreeBSD__
  91. #define _WANT_FREEBSD11_STAT
  92. #define _WANT_FREEBSD11_STATFS
  93. #define _WANT_FREEBSD11_DIRENT
  94. #define _WANT_KERNEL_ERRNO
  95. #define _WANT_SEMUN
  96. #endif
  97. #include <stdarg.h>
  98. #include <stddef.h>
  99. #include <stdbool.h>
  100. #include <stdint.h>
  101. #include <sys/types.h>
  102. #include <stdlib.h>
  103. #include <stdio.h>
  104. #include <string.h>
  105. #include <strings.h>
  106. #include <inttypes.h>
  107. #include <limits.h>
  108. /* Put unistd.h before time.h as that triggers localtime_r/gmtime_r
  109. * function availability on recentish Mingw-w64 platforms. */
  110. #include <unistd.h>
  111. #include <time.h>
  112. #include <ctype.h>
  113. #include <errno.h>
  114. #include <fcntl.h>
  115. #include <getopt.h>
  116. #include <sys/stat.h>
  117. #include <sys/time.h>
  118. #include <assert.h>
  119. /* setjmp must be declared before sysemu/os-win32.h
  120. * because it is redefined there. */
  121. #include <setjmp.h>
  122. #include <signal.h>
  123. #ifdef CONFIG_IOVEC
  124. #include <sys/uio.h>
  125. #endif
  126. #if defined(__linux__) && defined(__sparc__)
  127. /* The SPARC definition of QEMU_VMALLOC_ALIGN needs SHMLBA */
  128. #include <sys/shm.h>
  129. #endif
  130. #ifndef _WIN32
  131. #include <sys/wait.h>
  132. #else
  133. #define WIFEXITED(x) 1
  134. #define WEXITSTATUS(x) (x)
  135. #endif
  136. #ifdef __APPLE__
  137. #include <AvailabilityMacros.h>
  138. #endif
  139. /*
  140. * This is somewhat like a system header; it must be outside any extern "C"
  141. * block because it includes system headers itself, including glib.h,
  142. * which will not compile if inside an extern "C" block.
  143. */
  144. #include "glib-compat.h"
  145. #ifdef _WIN32
  146. #include "sysemu/os-win32.h"
  147. #endif
  148. #ifdef CONFIG_POSIX
  149. #include "sysemu/os-posix.h"
  150. #endif
  151. #ifdef __cplusplus
  152. extern "C" {
  153. #endif
  154. #include "qemu/typedefs.h"
  155. /**
  156. * Mark a function that executes in coroutine context
  157. *
  158. * Functions that execute in coroutine context cannot be called directly from
  159. * normal functions. In the future it would be nice to enable compiler or
  160. * static checker support for catching such errors. This annotation might make
  161. * it possible and in the meantime it serves as documentation.
  162. *
  163. * For example:
  164. *
  165. * static void coroutine_fn foo(void) {
  166. * ....
  167. * }
  168. */
  169. #ifdef __clang__
  170. #define coroutine_fn QEMU_ANNOTATE("coroutine_fn")
  171. #else
  172. #define coroutine_fn
  173. #endif
  174. /**
  175. * Mark a function that can suspend when executed in coroutine context,
  176. * but can handle running in non-coroutine context too.
  177. */
  178. #ifdef __clang__
  179. #define coroutine_mixed_fn QEMU_ANNOTATE("coroutine_mixed_fn")
  180. #else
  181. #define coroutine_mixed_fn
  182. #endif
  183. /**
  184. * Mark a function that should not be called from a coroutine context.
  185. * Usually there will be an analogous, coroutine_fn function that should
  186. * be used instead.
  187. *
  188. * When the function is also marked as coroutine_mixed_fn, the function should
  189. * only be called if the caller does not know whether it is in coroutine
  190. * context.
  191. *
  192. * Functions that are only no_coroutine_fn, on the other hand, should not
  193. * be called from within coroutines at all. This for example includes
  194. * functions that block.
  195. *
  196. * In the future it would be nice to enable compiler or static checker
  197. * support for catching such errors. This annotation is the first step
  198. * towards this, and in the meantime it serves as documentation.
  199. *
  200. * For example:
  201. *
  202. * static void no_coroutine_fn foo(void) {
  203. * ....
  204. * }
  205. */
  206. #ifdef __clang__
  207. #define no_coroutine_fn QEMU_ANNOTATE("no_coroutine_fn")
  208. #else
  209. #define no_coroutine_fn
  210. #endif
  211. /*
  212. * For mingw, as of v6.0.0, the function implementing the assert macro is
  213. * not marked as noreturn, so the compiler cannot delete code following an
  214. * assert(false) as unused. We rely on this within the code base to delete
  215. * code that is unreachable when features are disabled.
  216. * All supported versions of Glib's g_assert() satisfy this requirement.
  217. */
  218. #ifdef __MINGW32__
  219. #undef assert
  220. #define assert(x) g_assert(x)
  221. #endif
  222. /**
  223. * qemu_build_not_reached()
  224. *
  225. * The compiler, during optimization, is expected to prove that a call
  226. * to this function cannot be reached and remove it. If the compiler
  227. * supports QEMU_ERROR, this will be reported at compile time; otherwise
  228. * this will be reported at link time due to the missing symbol.
  229. */
  230. G_NORETURN
  231. void QEMU_ERROR("code path is reachable")
  232. qemu_build_not_reached_always(void);
  233. #if defined(__OPTIMIZE__) && !defined(__NO_INLINE__)
  234. #define qemu_build_not_reached() qemu_build_not_reached_always()
  235. #else
  236. #define qemu_build_not_reached() g_assert_not_reached()
  237. #endif
  238. /**
  239. * qemu_build_assert()
  240. *
  241. * The compiler, during optimization, is expected to prove that the
  242. * assertion is true.
  243. */
  244. #define qemu_build_assert(test) while (!(test)) qemu_build_not_reached()
  245. /*
  246. * According to waitpid man page:
  247. * WCOREDUMP
  248. * This macro is not specified in POSIX.1-2001 and is not
  249. * available on some UNIX implementations (e.g., AIX, SunOS).
  250. * Therefore, enclose its use inside #ifdef WCOREDUMP ... #endif.
  251. */
  252. #ifndef WCOREDUMP
  253. #define WCOREDUMP(status) 0
  254. #endif
  255. /*
  256. * We have a lot of unaudited code that may fail in strange ways, or
  257. * even be a security risk during migration, if you disable assertions
  258. * at compile-time. You may comment out these safety checks if you
  259. * absolutely want to disable assertion overhead, but it is not
  260. * supported upstream so the risk is all yours. Meanwhile, please
  261. * submit patches to remove any side-effects inside an assertion, or
  262. * fixing error handling that should use Error instead of assert.
  263. */
  264. #ifdef NDEBUG
  265. #error building with NDEBUG is not supported
  266. #endif
  267. #ifdef G_DISABLE_ASSERT
  268. #error building with G_DISABLE_ASSERT is not supported
  269. #endif
  270. #ifndef O_LARGEFILE
  271. #define O_LARGEFILE 0
  272. #endif
  273. #ifndef O_BINARY
  274. #define O_BINARY 0
  275. #endif
  276. #ifndef MAP_ANONYMOUS
  277. #define MAP_ANONYMOUS MAP_ANON
  278. #endif
  279. #ifndef MAP_NORESERVE
  280. #define MAP_NORESERVE 0
  281. #endif
  282. #ifndef ENOMEDIUM
  283. #define ENOMEDIUM ENODEV
  284. #endif
  285. #if !defined(ENOTSUP)
  286. #define ENOTSUP 4096
  287. #endif
  288. #if !defined(ECANCELED)
  289. #define ECANCELED 4097
  290. #endif
  291. #if !defined(EMEDIUMTYPE)
  292. #define EMEDIUMTYPE 4098
  293. #endif
  294. #if !defined(ESHUTDOWN)
  295. #define ESHUTDOWN 4099
  296. #endif
  297. #define RETRY_ON_EINTR(expr) \
  298. (__extension__ \
  299. ({ typeof(expr) __result; \
  300. do { \
  301. __result = (expr); \
  302. } while (__result == -1 && errno == EINTR); \
  303. __result; }))
  304. /* time_t may be either 32 or 64 bits depending on the host OS, and
  305. * can be either signed or unsigned, so we can't just hardcode a
  306. * specific maximum value. This is not a C preprocessor constant,
  307. * so you can't use TIME_MAX in an #ifdef, but for our purposes
  308. * this isn't a problem.
  309. */
  310. /* The macros TYPE_SIGNED, TYPE_WIDTH, and TYPE_MAXIMUM are from
  311. * Gnulib, and are under the LGPL v2.1 or (at your option) any
  312. * later version.
  313. */
  314. /* True if the real type T is signed. */
  315. #define TYPE_SIGNED(t) (!((t)0 < (t)-1))
  316. /* The width in bits of the integer type or expression T.
  317. * Padding bits are not supported.
  318. */
  319. #define TYPE_WIDTH(t) (sizeof(t) * CHAR_BIT)
  320. /* The maximum and minimum values for the integer type T. */
  321. #define TYPE_MAXIMUM(t) \
  322. ((t) (!TYPE_SIGNED(t) \
  323. ? (t)-1 \
  324. : ((((t)1 << (TYPE_WIDTH(t) - 2)) - 1) * 2 + 1)))
  325. #ifndef TIME_MAX
  326. #define TIME_MAX TYPE_MAXIMUM(time_t)
  327. #endif
  328. /* Mac OSX has a <stdint.h> bug that incorrectly defines SIZE_MAX with
  329. * the wrong type. Our replacement isn't usable in preprocessor
  330. * expressions, but it is sufficient for our needs. */
  331. #ifdef HAVE_BROKEN_SIZE_MAX
  332. #undef SIZE_MAX
  333. #define SIZE_MAX ((size_t)-1)
  334. #endif
  335. /*
  336. * Two variations of MIN/MAX macros. The first is for runtime use, and
  337. * evaluates arguments only once (so it is safe even with side
  338. * effects), but will not work in constant contexts (such as array
  339. * size declarations) because of the '{}'. The second is for constant
  340. * expression use, where evaluating arguments twice is safe because
  341. * the result is going to be constant anyway, but will not work in a
  342. * runtime context because of a void expression where a value is
  343. * expected. Thus, both gcc and clang will fail to compile if you use
  344. * the wrong macro (even if the error may seem a bit cryptic).
  345. *
  346. * Note that neither form is usable as an #if condition; if you truly
  347. * need to write conditional code that depends on a minimum or maximum
  348. * determined by the pre-processor instead of the compiler, you'll
  349. * have to open-code it. Sadly, Coverity is severely confused by the
  350. * constant variants, so we have to dumb things down there.
  351. *
  352. * Preprocessor sorcery ahead: use different identifiers for the local
  353. * variables in each expansion, so we can nest macro calls without
  354. * shadowing variables.
  355. */
  356. #define MIN_INTERNAL(a, b, _a, _b) \
  357. ({ \
  358. typeof(1 ? (a) : (b)) _a = (a), _b = (b); \
  359. _a < _b ? _a : _b; \
  360. })
  361. #undef MIN
  362. #define MIN(a, b) \
  363. MIN_INTERNAL((a), (b), MAKE_IDENTFIER(_a), MAKE_IDENTFIER(_b))
  364. #define MAX_INTERNAL(a, b, _a, _b) \
  365. ({ \
  366. typeof(1 ? (a) : (b)) _a = (a), _b = (b); \
  367. _a > _b ? _a : _b; \
  368. })
  369. #undef MAX
  370. #define MAX(a, b) \
  371. MAX_INTERNAL((a), (b), MAKE_IDENTFIER(_a), MAKE_IDENTFIER(_b))
  372. #ifdef __COVERITY__
  373. # define MIN_CONST(a, b) ((a) < (b) ? (a) : (b))
  374. # define MAX_CONST(a, b) ((a) > (b) ? (a) : (b))
  375. #else
  376. # define MIN_CONST(a, b) \
  377. __builtin_choose_expr( \
  378. __builtin_constant_p(a) && __builtin_constant_p(b), \
  379. (a) < (b) ? (a) : (b), \
  380. ((void)0))
  381. # define MAX_CONST(a, b) \
  382. __builtin_choose_expr( \
  383. __builtin_constant_p(a) && __builtin_constant_p(b), \
  384. (a) > (b) ? (a) : (b), \
  385. ((void)0))
  386. #endif
  387. /*
  388. * Minimum function that returns zero only if both values are zero.
  389. * Intended for use with unsigned values only.
  390. *
  391. * Preprocessor sorcery ahead: use different identifiers for the local
  392. * variables in each expansion, so we can nest macro calls without
  393. * shadowing variables.
  394. */
  395. #define MIN_NON_ZERO_INTERNAL(a, b, _a, _b) \
  396. ({ \
  397. typeof(1 ? (a) : (b)) _a = (a), _b = (b); \
  398. _a == 0 ? _b : (_b == 0 || _b > _a) ? _a : _b; \
  399. })
  400. #define MIN_NON_ZERO(a, b) \
  401. MIN_NON_ZERO_INTERNAL((a), (b), MAKE_IDENTFIER(_a), MAKE_IDENTFIER(_b))
  402. /*
  403. * Round number down to multiple. Safe when m is not a power of 2 (see
  404. * ROUND_DOWN for a faster version when a power of 2 is guaranteed).
  405. */
  406. #define QEMU_ALIGN_DOWN(n, m) ((n) / (m) * (m))
  407. /*
  408. * Round number up to multiple. Safe when m is not a power of 2 (see
  409. * ROUND_UP for a faster version when a power of 2 is guaranteed).
  410. */
  411. #define QEMU_ALIGN_UP(n, m) QEMU_ALIGN_DOWN((n) + (m) - 1, (m))
  412. /* Check if n is a multiple of m */
  413. #define QEMU_IS_ALIGNED(n, m) (((n) % (m)) == 0)
  414. /* n-byte align pointer down */
  415. #define QEMU_ALIGN_PTR_DOWN(p, n) \
  416. ((typeof(p))QEMU_ALIGN_DOWN((uintptr_t)(p), (n)))
  417. /* n-byte align pointer up */
  418. #define QEMU_ALIGN_PTR_UP(p, n) \
  419. ((typeof(p))QEMU_ALIGN_UP((uintptr_t)(p), (n)))
  420. /* Check if pointer p is n-bytes aligned */
  421. #define QEMU_PTR_IS_ALIGNED(p, n) QEMU_IS_ALIGNED((uintptr_t)(p), (n))
  422. /*
  423. * Round number down to multiple. Requires that d be a power of 2 (see
  424. * QEMU_ALIGN_UP for a safer but slower version on arbitrary
  425. * numbers); works even if d is a smaller type than n.
  426. */
  427. #ifndef ROUND_DOWN
  428. #define ROUND_DOWN(n, d) ((n) & -(0 ? (n) : (d)))
  429. #endif
  430. /*
  431. * Round number up to multiple. Requires that d be a power of 2 (see
  432. * QEMU_ALIGN_UP for a safer but slower version on arbitrary
  433. * numbers); works even if d is a smaller type than n.
  434. */
  435. #ifndef ROUND_UP
  436. #define ROUND_UP(n, d) ROUND_DOWN((n) + (d) - 1, (d))
  437. #endif
  438. #ifndef DIV_ROUND_UP
  439. #define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
  440. #endif
  441. /*
  442. * &(x)[0] is always a pointer - if it's same type as x then the argument is a
  443. * pointer, not an array.
  444. */
  445. #define QEMU_IS_ARRAY(x) (!__builtin_types_compatible_p(typeof(x), \
  446. typeof(&(x)[0])))
  447. #ifndef ARRAY_SIZE
  448. #define ARRAY_SIZE(x) ((sizeof(x) / sizeof((x)[0])) + \
  449. QEMU_BUILD_BUG_ON_ZERO(!QEMU_IS_ARRAY(x)))
  450. #endif
  451. int qemu_daemon(int nochdir, int noclose);
  452. void *qemu_anon_ram_alloc(size_t size, uint64_t *align, bool shared,
  453. bool noreserve);
  454. void qemu_anon_ram_free(void *ptr, size_t size);
  455. #ifdef _WIN32
  456. #define HAVE_CHARDEV_SERIAL 1
  457. #define HAVE_CHARDEV_PARALLEL 1
  458. #else
  459. #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
  460. || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \
  461. || defined(__GLIBC__) || defined(__APPLE__)
  462. #define HAVE_CHARDEV_SERIAL 1
  463. #endif
  464. #if defined(__linux__) || defined(__FreeBSD__) \
  465. || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
  466. #define HAVE_CHARDEV_PARALLEL 1
  467. #endif
  468. #endif
  469. #if defined(__HAIKU__)
  470. #define SIGIO SIGPOLL
  471. #endif
  472. #ifdef HAVE_MADVISE_WITHOUT_PROTOTYPE
  473. /*
  474. * See MySQL bug #7156 (http://bugs.mysql.com/bug.php?id=7156) for discussion
  475. * about Solaris missing the madvise() prototype.
  476. */
  477. int madvise(char *, size_t, int);
  478. #endif
  479. #if defined(CONFIG_LINUX)
  480. #ifndef BUS_MCEERR_AR
  481. #define BUS_MCEERR_AR 4
  482. #endif
  483. #ifndef BUS_MCEERR_AO
  484. #define BUS_MCEERR_AO 5
  485. #endif
  486. #endif
  487. #if defined(__linux__) && \
  488. (defined(__x86_64__) || defined(__arm__) || defined(__aarch64__) \
  489. || defined(__powerpc64__))
  490. /* Use 2 MiB alignment so transparent hugepages can be used by KVM.
  491. Valgrind does not support alignments larger than 1 MiB,
  492. therefore we need special code which handles running on Valgrind. */
  493. # define QEMU_VMALLOC_ALIGN (512 * 4096)
  494. #elif defined(__linux__) && defined(__s390x__)
  495. /* Use 1 MiB (segment size) alignment so gmap can be used by KVM. */
  496. # define QEMU_VMALLOC_ALIGN (256 * 4096)
  497. #elif defined(__linux__) && defined(__sparc__)
  498. # define QEMU_VMALLOC_ALIGN MAX(qemu_real_host_page_size(), SHMLBA)
  499. #elif defined(__linux__) && defined(__loongarch__)
  500. /*
  501. * For transparent hugepage optimization, it has better be huge page
  502. * aligned. LoongArch host system supports two kinds of pagesize: 4K
  503. * and 16K, here calculate huge page size from host page size
  504. */
  505. # define QEMU_VMALLOC_ALIGN (qemu_real_host_page_size() * \
  506. qemu_real_host_page_size() / sizeof(long))
  507. #else
  508. # define QEMU_VMALLOC_ALIGN qemu_real_host_page_size()
  509. #endif
  510. #ifdef CONFIG_POSIX
  511. struct qemu_signalfd_siginfo {
  512. uint32_t ssi_signo; /* Signal number */
  513. int32_t ssi_errno; /* Error number (unused) */
  514. int32_t ssi_code; /* Signal code */
  515. uint32_t ssi_pid; /* PID of sender */
  516. uint32_t ssi_uid; /* Real UID of sender */
  517. int32_t ssi_fd; /* File descriptor (SIGIO) */
  518. uint32_t ssi_tid; /* Kernel timer ID (POSIX timers) */
  519. uint32_t ssi_band; /* Band event (SIGIO) */
  520. uint32_t ssi_overrun; /* POSIX timer overrun count */
  521. uint32_t ssi_trapno; /* Trap number that caused signal */
  522. int32_t ssi_status; /* Exit status or signal (SIGCHLD) */
  523. int32_t ssi_int; /* Integer sent by sigqueue(2) */
  524. uint64_t ssi_ptr; /* Pointer sent by sigqueue(2) */
  525. uint64_t ssi_utime; /* User CPU time consumed (SIGCHLD) */
  526. uint64_t ssi_stime; /* System CPU time consumed (SIGCHLD) */
  527. uint64_t ssi_addr; /* Address that generated signal
  528. (for hardware-generated signals) */
  529. uint8_t pad[48]; /* Pad size to 128 bytes (allow for
  530. additional fields in the future) */
  531. };
  532. int qemu_signalfd(const sigset_t *mask);
  533. void sigaction_invoke(struct sigaction *action,
  534. struct qemu_signalfd_siginfo *info);
  535. #endif
  536. /*
  537. * Don't introduce new usage of this function, prefer the following
  538. * qemu_open/qemu_create that take an "Error **errp"
  539. */
  540. int qemu_open_old(const char *name, int flags, ...);
  541. int qemu_open(const char *name, int flags, Error **errp);
  542. int qemu_create(const char *name, int flags, mode_t mode, Error **errp);
  543. int qemu_close(int fd);
  544. int qemu_unlink(const char *name);
  545. #ifndef _WIN32
  546. int qemu_dup_flags(int fd, int flags);
  547. int qemu_dup(int fd);
  548. int qemu_lock_fd(int fd, int64_t start, int64_t len, bool exclusive);
  549. int qemu_unlock_fd(int fd, int64_t start, int64_t len);
  550. int qemu_lock_fd_test(int fd, int64_t start, int64_t len, bool exclusive);
  551. bool qemu_has_ofd_lock(void);
  552. #endif
  553. #if defined(__HAIKU__) && defined(__i386__)
  554. #define FMT_pid "%ld"
  555. #elif defined(WIN64)
  556. #define FMT_pid "%" PRId64
  557. #else
  558. #define FMT_pid "%d"
  559. #endif
  560. bool qemu_write_pidfile(const char *pidfile, Error **errp);
  561. int qemu_get_thread_id(void);
  562. #ifndef CONFIG_IOVEC
  563. struct iovec {
  564. void *iov_base;
  565. size_t iov_len;
  566. };
  567. /*
  568. * Use the same value as Linux for now.
  569. */
  570. #define IOV_MAX 1024
  571. ssize_t readv(int fd, const struct iovec *iov, int iov_cnt);
  572. ssize_t writev(int fd, const struct iovec *iov, int iov_cnt);
  573. #endif
  574. #ifdef _WIN32
  575. static inline void qemu_timersub(const struct timeval *val1,
  576. const struct timeval *val2,
  577. struct timeval *res)
  578. {
  579. res->tv_sec = val1->tv_sec - val2->tv_sec;
  580. if (val1->tv_usec < val2->tv_usec) {
  581. res->tv_sec--;
  582. res->tv_usec = val1->tv_usec - val2->tv_usec + 1000 * 1000;
  583. } else {
  584. res->tv_usec = val1->tv_usec - val2->tv_usec;
  585. }
  586. }
  587. #else
  588. #define qemu_timersub timersub
  589. #endif
  590. ssize_t qemu_write_full(int fd, const void *buf, size_t count)
  591. G_GNUC_WARN_UNUSED_RESULT;
  592. void qemu_set_cloexec(int fd);
  593. /* Return a dynamically allocated directory path that is appropriate for storing
  594. * local state.
  595. *
  596. * The caller is responsible for releasing the value returned with g_free()
  597. * after use.
  598. */
  599. char *qemu_get_local_state_dir(void);
  600. /**
  601. * qemu_getauxval:
  602. * @type: the auxiliary vector key to lookup
  603. *
  604. * Search the auxiliary vector for @type, returning the value
  605. * or 0 if @type is not present.
  606. */
  607. unsigned long qemu_getauxval(unsigned long type);
  608. void qemu_set_tty_echo(int fd, bool echo);
  609. typedef struct ThreadContext ThreadContext;
  610. /**
  611. * qemu_prealloc_mem:
  612. * @fd: the fd mapped into the area, -1 for anonymous memory
  613. * @area: start address of the are to preallocate
  614. * @sz: the size of the area to preallocate
  615. * @max_threads: maximum number of threads to use
  616. * @tc: prealloc context threads pointer, NULL if not in use
  617. * @async: request asynchronous preallocation, requires @tc
  618. * @errp: returns an error if this function fails
  619. *
  620. * Preallocate memory (populate/prefault page tables writable) for the virtual
  621. * memory area starting at @area with the size of @sz. After a successful call,
  622. * each page in the area was faulted in writable at least once, for example,
  623. * after allocating file blocks for mapped files.
  624. *
  625. * When setting @async, allocation might be performed asynchronously.
  626. * qemu_finish_async_prealloc_mem() must be called to finish any asynchronous
  627. * preallocation.
  628. *
  629. * Return: true on success, else false setting @errp with error.
  630. */
  631. bool qemu_prealloc_mem(int fd, char *area, size_t sz, int max_threads,
  632. ThreadContext *tc, bool async, Error **errp);
  633. /**
  634. * qemu_finish_async_prealloc_mem:
  635. * @errp: returns an error if this function fails
  636. *
  637. * Finish all outstanding asynchronous memory preallocation.
  638. *
  639. * Return: true on success, else false setting @errp with error.
  640. */
  641. bool qemu_finish_async_prealloc_mem(Error **errp);
  642. /**
  643. * qemu_get_pid_name:
  644. * @pid: pid of a process
  645. *
  646. * For given @pid fetch its name. Caller is responsible for
  647. * freeing the string when no longer needed.
  648. * Returns allocated string on success, NULL on failure.
  649. */
  650. char *qemu_get_pid_name(pid_t pid);
  651. /* Using intptr_t ensures that qemu_*_page_mask is sign-extended even
  652. * when intptr_t is 32-bit and we are aligning a long long.
  653. */
  654. static inline uintptr_t qemu_real_host_page_size(void)
  655. {
  656. return getpagesize();
  657. }
  658. static inline intptr_t qemu_real_host_page_mask(void)
  659. {
  660. return -(intptr_t)qemu_real_host_page_size();
  661. }
  662. /*
  663. * After using getopt or getopt_long, if you need to parse another set
  664. * of options, then you must reset optind. Unfortunately the way to
  665. * do this varies between implementations of getopt.
  666. */
  667. static inline void qemu_reset_optind(void)
  668. {
  669. #ifdef HAVE_OPTRESET
  670. optind = 1;
  671. optreset = 1;
  672. #else
  673. optind = 0;
  674. #endif
  675. }
  676. int qemu_fdatasync(int fd);
  677. /**
  678. * Sync changes made to the memory mapped file back to the backing
  679. * storage. For POSIX compliant systems this will fallback
  680. * to regular msync call. Otherwise it will trigger whole file sync
  681. * (including the metadata case there is no support to skip that otherwise)
  682. *
  683. * @addr - start of the memory area to be synced
  684. * @length - length of the are to be synced
  685. * @fd - file descriptor for the file to be synced
  686. * (mandatory only for POSIX non-compliant systems)
  687. */
  688. int qemu_msync(void *addr, size_t length, int fd);
  689. /**
  690. * qemu_get_host_physmem:
  691. *
  692. * Operating system agnostic way of querying host memory.
  693. *
  694. * Returns amount of physical memory on the system. This is purely
  695. * advisery and may return 0 if we can't work it out. At the other
  696. * end we saturate to SIZE_MAX if you are lucky enough to have that
  697. * much memory.
  698. */
  699. size_t qemu_get_host_physmem(void);
  700. /*
  701. * Toggle write/execute on the pages marked MAP_JIT
  702. * for the current thread.
  703. */
  704. #if defined(MAC_OS_VERSION_11_0) && \
  705. MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_VERSION_11_0
  706. static inline void qemu_thread_jit_execute(void)
  707. {
  708. pthread_jit_write_protect_np(true);
  709. }
  710. static inline void qemu_thread_jit_write(void)
  711. {
  712. pthread_jit_write_protect_np(false);
  713. }
  714. #else
  715. static inline void qemu_thread_jit_write(void) {}
  716. static inline void qemu_thread_jit_execute(void) {}
  717. #endif
  718. /**
  719. * Platforms which do not support system() return ENOSYS
  720. */
  721. #ifndef HAVE_SYSTEM_FUNCTION
  722. #define system platform_does_not_support_system
  723. static inline int platform_does_not_support_system(const char *command)
  724. {
  725. errno = ENOSYS;
  726. return -1;
  727. }
  728. #endif /* !HAVE_SYSTEM_FUNCTION */
  729. #ifdef __cplusplus
  730. }
  731. #endif
  732. #endif