2015-08-19 17:20:20 +02:00
|
|
|
/*
|
|
|
|
* OS includes and handling of OS dependencies
|
|
|
|
*
|
|
|
|
* This header exists to pull in some common system headers that
|
|
|
|
* most code in QEMU will want, and to fix up some possible issues with
|
|
|
|
* it (missing defines, Windows weirdness, and so on).
|
|
|
|
*
|
|
|
|
* To avoid getting into possible circular include dependencies, this
|
|
|
|
* file should not include any other QEMU headers, with the exceptions
|
include/qemu/osdep.h: Don't include qapi/error.h
Commit 57cb38b included qapi/error.h into qemu/osdep.h to get the
Error typedef. Since then, we've moved to include qemu/osdep.h
everywhere. Its file comment explains: "To avoid getting into
possible circular include dependencies, this file should not include
any other QEMU headers, with the exceptions of config-host.h,
compiler.h, os-posix.h and os-win32.h, all of which are doing a
similar job to this file and are under similar constraints."
qapi/error.h doesn't do a similar job, and it doesn't adhere to
similar constraints: it includes qapi-types.h. That's in excess of
100KiB of crap most .c files don't actually need.
Add the typedef to qemu/typedefs.h, and include that instead of
qapi/error.h. Include qapi/error.h in .c files that need it and don't
get it now. Include qapi-types.h in qom/object.h for uint16List.
Update scripts/clean-includes accordingly. Update it further to match
reality: replace config.h by config-target.h, add sysemu/os-posix.h,
sysemu/os-win32.h. Update the list of includes in the qemu/osdep.h
comment quoted above similarly.
This reduces the number of objects depending on qapi/error.h from "all
of them" to less than a third. Unfortunately, the number depending on
qapi-types.h shrinks only a little. More work is needed for that one.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
[Fix compilation without the spice devel packages. - Paolo]
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2016-03-14 09:01:28 +01:00
|
|
|
* of config-host.h, config-target.h, qemu/compiler.h,
|
|
|
|
* sysemu/os-posix.h, sysemu/os-win32.h, glib-compat.h and
|
|
|
|
* qemu/typedefs.h, all of which are doing a similar job to this file
|
|
|
|
* and are under similar constraints.
|
2015-08-19 17:20:20 +02:00
|
|
|
*
|
|
|
|
* This header also contains prototypes for functions defined in
|
|
|
|
* os-*.c and util/oslib-*.c; those would probably be better split
|
|
|
|
* out into separate header files.
|
|
|
|
*
|
|
|
|
* In an ideal world this header would contain only:
|
|
|
|
* (1) things which everybody needs
|
|
|
|
* (2) things without which code would work on most platforms but
|
|
|
|
* fail to compile or misbehave on a minority of host OSes
|
|
|
|
*
|
|
|
|
* This work is licensed under the terms of the GNU GPL, version 2 or later.
|
|
|
|
* See the COPYING file in the top-level directory.
|
|
|
|
*/
|
2004-02-16 23:12:40 +01:00
|
|
|
#ifndef QEMU_OSDEP_H
|
|
|
|
#define QEMU_OSDEP_H
|
|
|
|
|
2013-04-21 12:01:06 +02:00
|
|
|
#include "config-host.h"
|
2016-02-23 12:58:02 +01:00
|
|
|
#ifdef NEED_CPU_H
|
|
|
|
#include "config-target.h"
|
2016-03-15 12:46:10 +01:00
|
|
|
#else
|
|
|
|
#include "exec/poison.h"
|
2016-02-23 12:58:02 +01:00
|
|
|
#endif
|
2015-08-19 17:20:19 +02:00
|
|
|
#include "qemu/compiler.h"
|
2016-02-08 20:08:10 +01:00
|
|
|
|
2016-02-23 12:58:02 +01:00
|
|
|
/* Older versions of C++ don't get definitions of various macros from
|
|
|
|
* stdlib.h unless we define these macros before first inclusion of
|
|
|
|
* that system header.
|
|
|
|
*/
|
|
|
|
#ifndef __STDC_CONSTANT_MACROS
|
|
|
|
#define __STDC_CONSTANT_MACROS
|
|
|
|
#endif
|
|
|
|
#ifndef __STDC_LIMIT_MACROS
|
|
|
|
#define __STDC_LIMIT_MACROS
|
|
|
|
#endif
|
|
|
|
#ifndef __STDC_FORMAT_MACROS
|
|
|
|
#define __STDC_FORMAT_MACROS
|
|
|
|
#endif
|
|
|
|
|
2016-02-08 20:08:10 +01:00
|
|
|
/* The following block of code temporarily renames the daemon() function so the
|
|
|
|
* compiler does not see the warning associated with it in stdlib.h on OSX
|
|
|
|
*/
|
|
|
|
#ifdef __APPLE__
|
|
|
|
#define daemon qemu_fake_daemon_function
|
|
|
|
#include <stdlib.h>
|
|
|
|
#undef daemon
|
|
|
|
extern int daemon(int, int);
|
|
|
|
#endif
|
|
|
|
|
2004-02-16 23:12:40 +01:00
|
|
|
#include <stdarg.h>
|
2009-09-12 11:58:46 +02:00
|
|
|
#include <stddef.h>
|
2012-08-03 20:39:21 +02:00
|
|
|
#include <stdbool.h>
|
2014-10-31 17:38:37 +01:00
|
|
|
#include <stdint.h>
|
2008-08-15 20:33:42 +02:00
|
|
|
#include <sys/types.h>
|
2015-08-19 17:20:19 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <strings.h>
|
|
|
|
#include <inttypes.h>
|
|
|
|
#include <limits.h>
|
oslib-win32: only provide localtime_r/gmtime_r if missing
The oslib-win32 file currently provides a localtime_r and
gmtime_r replacement unconditionally. Some versions of
Mingw-w64 would provide crude macros for localtime_r/gmtime_r
which QEMU takes care to disable. Latest versions of Mingw-w64
now provide actual functions for localtime_r/gmtime_r, but
with a twist that you have to include unistd.h or pthread.h
before including time.h. By luck some files in QEMU have
such an include order, resulting in compile errors:
CC util/osdep.o
In file included from include/qemu-common.h:48:0,
from util/osdep.c:48:
include/sysemu/os-win32.h:77:12: error: redundant redeclaration of 'gmtime_r' [-Werror=redundant-decls]
struct tm *gmtime_r(const time_t *timep, struct tm *result);
^
In file included from include/qemu-common.h:35:0,
from util/osdep.c:48:
/usr/i686-w64-mingw32/sys-root/mingw/include/time.h:272:107: note: previous definition of 'gmtime_r' was here
In file included from include/qemu-common.h:48:0,
from util/osdep.c:48:
include/sysemu/os-win32.h:79:12: error: redundant redeclaration of 'localtime_r' [-Werror=redundant-decls]
struct tm *localtime_r(const time_t *timep, struct tm *result);
^
In file included from include/qemu-common.h:35:0,
from util/osdep.c:48:
/usr/i686-w64-mingw32/sys-root/mingw/include/time.h:269:107: note: previous definition of 'localtime_r' was here
This change adds a configure test to see if localtime_r
exits, and only enables the QEMU impl if missing. We also
re-arrange qemu-common.h try attempt to guarantee that all
source files get unistd.h before time.h and thus see the
localtime_r/gmtime_r defs.
[sw: Use "official" spellings for Mingw-w64, MinGW in comments.]
[sw: Terminate sentences with a dot in comments.]
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
Reviewed-by: Denis V. Lunev <den@openvz.org>
Signed-off-by: Stefan Weil <sw@weilnetz.de>
2015-09-22 16:13:26 +02:00
|
|
|
/* Put unistd.h before time.h as that triggers localtime_r/gmtime_r
|
|
|
|
* function availability on recentish Mingw-w64 platforms. */
|
|
|
|
#include <unistd.h>
|
2015-08-19 17:20:19 +02:00
|
|
|
#include <time.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <assert.h>
|
2016-03-12 07:20:49 +01:00
|
|
|
/* setjmp must be declared before sysemu/os-win32.h
|
|
|
|
* because it is redefined there. */
|
|
|
|
#include <setjmp.h>
|
2015-08-19 17:20:19 +02:00
|
|
|
#include <signal.h>
|
|
|
|
|
2013-05-10 21:58:21 +02:00
|
|
|
#ifdef __OpenBSD__
|
2008-08-15 20:33:42 +02:00
|
|
|
#include <sys/signal.h>
|
|
|
|
#endif
|
2004-02-16 23:12:40 +01:00
|
|
|
|
2013-02-22 17:36:38 +01:00
|
|
|
#ifndef _WIN32
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#else
|
|
|
|
#define WIFEXITED(x) 1
|
|
|
|
#define WEXITSTATUS(x) (x)
|
|
|
|
#endif
|
|
|
|
|
2015-08-19 17:20:19 +02:00
|
|
|
#ifdef _WIN32
|
|
|
|
#include "sysemu/os-win32.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CONFIG_POSIX
|
|
|
|
#include "sysemu/os-posix.h"
|
|
|
|
#endif
|
2009-01-07 18:40:15 +01:00
|
|
|
|
2015-12-04 18:34:20 +01:00
|
|
|
#include "glib-compat.h"
|
include/qemu/osdep.h: Don't include qapi/error.h
Commit 57cb38b included qapi/error.h into qemu/osdep.h to get the
Error typedef. Since then, we've moved to include qemu/osdep.h
everywhere. Its file comment explains: "To avoid getting into
possible circular include dependencies, this file should not include
any other QEMU headers, with the exceptions of config-host.h,
compiler.h, os-posix.h and os-win32.h, all of which are doing a
similar job to this file and are under similar constraints."
qapi/error.h doesn't do a similar job, and it doesn't adhere to
similar constraints: it includes qapi-types.h. That's in excess of
100KiB of crap most .c files don't actually need.
Add the typedef to qemu/typedefs.h, and include that instead of
qapi/error.h. Include qapi/error.h in .c files that need it and don't
get it now. Include qapi-types.h in qom/object.h for uint16List.
Update scripts/clean-includes accordingly. Update it further to match
reality: replace config.h by config-target.h, add sysemu/os-posix.h,
sysemu/os-win32.h. Update the list of includes in the qemu/osdep.h
comment quoted above similarly.
This reduces the number of objects depending on qapi/error.h from "all
of them" to less than a third. Unfortunately, the number depending on
qapi-types.h shrinks only a little. More work is needed for that one.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
[Fix compilation without the spice devel packages. - Paolo]
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2016-03-14 09:01:28 +01:00
|
|
|
#include "qemu/typedefs.h"
|
2015-08-28 15:40:01 +02:00
|
|
|
|
osdep.h: Prohibit disabling assert() in supported builds
We already have several files that knowingly require assert()
to work, sometimes because refactoring the code for proper
error handling has not been tackled yet; there are probably
other files that have a similar situation but with no comments
documenting the same. In fact, we have places in migration
that handle untrusted input with assertions, where disabling
the assertions risks a worse security hole than the current
behavior of losing the guest to SIGABRT when migration fails
because of the assertion. Promote our current per-file
safety-valve to instead be project-wide, and expand it to also
cover glib's g_assert().
Note that we do NOT want to encourage 'assert(side-effects);'
(that is a bad practice that prevents copy-and-paste of code to
other projects that CAN disable assertions; plus it costs
unnecessary reviewer mental cycles to remember whether a project
special-cases the crippling of asserts); and we would LIKE to
fix migration to not rely on asserts (but that takes a big code
audit). But in the meantime, we DO want to send a message
that anyone that disables assertions has to tweak code in order
to compile, making it obvious that they are taking on additional
risk that we are not going to support. At the same time, leave
comments mentioning NDEBUG in files that we know still need to
be scrubbed, so there is at least something to grep for.
It would be possible to come up with some other mechanism for
doing runtime checking by default, but which does not abort
the program on failure, while leaving side effects in place
(unlike how crippling assert() avoids even the side effects),
perhaps under the name q_verify(); but it was not deemed worth
the effort (developers should not have to learn a replacement
when the standard C macro works just fine, and it would be a lot
of churn for little gain). The patch specifically uses #error
rather than #warn so that a user is forced to tweak the header
to acknowledge the issue, even when not using a -Werror
compilation.
Signed-off-by: Eric Blake <eblake@redhat.com>
Reviewed-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
Reviewed-by: Thomas Huth <thuth@redhat.com>
Message-Id: <20170911211320.25385-1-eblake@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2017-09-11 23:13:20 +02:00
|
|
|
/*
|
|
|
|
* We have a lot of unaudited code that may fail in strange ways, or
|
|
|
|
* even be a security risk during migration, if you disable assertions
|
|
|
|
* at compile-time. You may comment out these safety checks if you
|
|
|
|
* absolutely want to disable assertion overhead, but it is not
|
|
|
|
* supported upstream so the risk is all yours. Meanwhile, please
|
|
|
|
* submit patches to remove any side-effects inside an assertion, or
|
|
|
|
* fixing error handling that should use Error instead of assert.
|
|
|
|
*/
|
|
|
|
#ifdef NDEBUG
|
|
|
|
#error building with NDEBUG is not supported
|
|
|
|
#endif
|
|
|
|
#ifdef G_DISABLE_ASSERT
|
|
|
|
#error building with G_DISABLE_ASSERT is not supported
|
|
|
|
#endif
|
|
|
|
|
2015-08-19 17:20:19 +02:00
|
|
|
#ifndef O_LARGEFILE
|
|
|
|
#define O_LARGEFILE 0
|
|
|
|
#endif
|
|
|
|
#ifndef O_BINARY
|
|
|
|
#define O_BINARY 0
|
|
|
|
#endif
|
|
|
|
#ifndef MAP_ANONYMOUS
|
|
|
|
#define MAP_ANONYMOUS MAP_ANON
|
|
|
|
#endif
|
|
|
|
#ifndef ENOMEDIUM
|
|
|
|
#define ENOMEDIUM ENODEV
|
|
|
|
#endif
|
|
|
|
#if !defined(ENOTSUP)
|
|
|
|
#define ENOTSUP 4096
|
|
|
|
#endif
|
|
|
|
#if !defined(ECANCELED)
|
|
|
|
#define ECANCELED 4097
|
|
|
|
#endif
|
|
|
|
#if !defined(EMEDIUMTYPE)
|
|
|
|
#define EMEDIUMTYPE 4098
|
|
|
|
#endif
|
2016-10-14 20:33:16 +02:00
|
|
|
#if !defined(ESHUTDOWN)
|
|
|
|
#define ESHUTDOWN 4099
|
|
|
|
#endif
|
2017-11-23 16:56:38 +01:00
|
|
|
|
|
|
|
/* time_t may be either 32 or 64 bits depending on the host OS, and
|
|
|
|
* can be either signed or unsigned, so we can't just hardcode a
|
|
|
|
* specific maximum value. This is not a C preprocessor constant,
|
|
|
|
* so you can't use TIME_MAX in an #ifdef, but for our purposes
|
|
|
|
* this isn't a problem.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* The macros TYPE_SIGNED, TYPE_WIDTH, and TYPE_MAXIMUM are from
|
|
|
|
* Gnulib, and are under the LGPL v2.1 or (at your option) any
|
|
|
|
* later version.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* True if the real type T is signed. */
|
|
|
|
#define TYPE_SIGNED(t) (!((t)0 < (t)-1))
|
|
|
|
|
|
|
|
/* The width in bits of the integer type or expression T.
|
|
|
|
* Padding bits are not supported.
|
|
|
|
*/
|
|
|
|
#define TYPE_WIDTH(t) (sizeof(t) * CHAR_BIT)
|
|
|
|
|
|
|
|
/* The maximum and minimum values for the integer type T. */
|
|
|
|
#define TYPE_MAXIMUM(t) \
|
|
|
|
((t) (!TYPE_SIGNED(t) \
|
|
|
|
? (t)-1 \
|
|
|
|
: ((((t)1 << (TYPE_WIDTH(t) - 2)) - 1) * 2 + 1)))
|
|
|
|
|
2015-08-19 17:20:19 +02:00
|
|
|
#ifndef TIME_MAX
|
2017-11-23 16:56:38 +01:00
|
|
|
#define TIME_MAX TYPE_MAXIMUM(time_t)
|
2015-08-19 17:20:19 +02:00
|
|
|
#endif
|
|
|
|
|
2016-03-11 13:41:13 +01:00
|
|
|
/* HOST_LONG_BITS is the size of a native pointer in bits. */
|
|
|
|
#if UINTPTR_MAX == UINT32_MAX
|
|
|
|
# define HOST_LONG_BITS 32
|
|
|
|
#elif UINTPTR_MAX == UINT64_MAX
|
|
|
|
# define HOST_LONG_BITS 64
|
|
|
|
#else
|
|
|
|
# error Unknown pointer size
|
|
|
|
#endif
|
|
|
|
|
2016-10-11 17:46:23 +02:00
|
|
|
/* Mac OSX has a <stdint.h> bug that incorrectly defines SIZE_MAX with
|
|
|
|
* the wrong type. Our replacement isn't usable in preprocessor
|
|
|
|
* expressions, but it is sufficient for our needs. */
|
|
|
|
#if defined(HAVE_BROKEN_SIZE_MAX) && HAVE_BROKEN_SIZE_MAX
|
|
|
|
#undef SIZE_MAX
|
|
|
|
#define SIZE_MAX ((size_t)-1)
|
|
|
|
#endif
|
|
|
|
|
2007-11-19 01:38:33 +01:00
|
|
|
#ifndef MIN
|
|
|
|
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
|
|
|
|
#endif
|
|
|
|
#ifndef MAX
|
|
|
|
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
|
|
|
|
#endif
|
|
|
|
|
2014-10-27 10:18:43 +01:00
|
|
|
/* Minimum function that returns zero only iff both values are zero.
|
|
|
|
* Intended for use with unsigned values only. */
|
|
|
|
#ifndef MIN_NON_ZERO
|
2016-07-12 08:48:33 +02:00
|
|
|
#define MIN_NON_ZERO(a, b) ((a) == 0 ? (b) : \
|
|
|
|
((b) == 0 ? (a) : (MIN(a, b))))
|
2014-10-27 10:18:43 +01:00
|
|
|
#endif
|
|
|
|
|
2016-03-11 16:27:23 +01:00
|
|
|
/* Round number down to multiple */
|
|
|
|
#define QEMU_ALIGN_DOWN(n, m) ((n) / (m) * (m))
|
|
|
|
|
2016-07-21 21:34:47 +02:00
|
|
|
/* Round number up to multiple. Safe when m is not a power of 2 (see
|
|
|
|
* ROUND_UP for a faster version when a power of 2 is guaranteed) */
|
2016-03-11 16:27:23 +01:00
|
|
|
#define QEMU_ALIGN_UP(n, m) QEMU_ALIGN_DOWN((n) + (m) - 1, (m))
|
|
|
|
|
2016-04-22 18:08:43 +02:00
|
|
|
/* Check if n is a multiple of m */
|
|
|
|
#define QEMU_IS_ALIGNED(n, m) (((n) % (m)) == 0)
|
|
|
|
|
2016-04-22 18:08:44 +02:00
|
|
|
/* n-byte align pointer down */
|
|
|
|
#define QEMU_ALIGN_PTR_DOWN(p, n) \
|
|
|
|
((typeof(p))QEMU_ALIGN_DOWN((uintptr_t)(p), (n)))
|
|
|
|
|
|
|
|
/* n-byte align pointer up */
|
|
|
|
#define QEMU_ALIGN_PTR_UP(p, n) \
|
|
|
|
((typeof(p))QEMU_ALIGN_UP((uintptr_t)(p), (n)))
|
|
|
|
|
|
|
|
/* Check if pointer p is n-bytes aligned */
|
|
|
|
#define QEMU_PTR_IS_ALIGNED(p, n) QEMU_IS_ALIGNED((uintptr_t)(p), (n))
|
|
|
|
|
2016-07-21 21:34:47 +02:00
|
|
|
/* Round number up to multiple. Requires that d be a power of 2 (see
|
|
|
|
* QEMU_ALIGN_UP for a safer but slower version on arbitrary
|
osdep: Fix ROUND_UP(64-bit, 32-bit)
When using bit-wise operations that exploit the power-of-two
nature of the second argument of ROUND_UP(), we still need to
ensure that the mask is as wide as the first argument (done
by using a ternary to force proper arithmetic promotion).
Unpatched, ROUND_UP(2ULL*1024*1024*1024*1024, 512U) produces 0,
instead of the intended 2TiB, because negation of an unsigned
32-bit quantity followed by widening to 64-bits does not
sign-extend the mask.
Broken since its introduction in commit 292c8e50 (v1.5.0).
Callers that passed the same width type to both macro parameters,
or that had other code to ensure the first parameter's maximum
runtime value did not exceed the second parameter's width, are
unaffected, but I did not audit to see which (if any) existing
clients of the macro could trigger incorrect behavior (I found
the bug while adding a new use of the macro).
While preparing the patch, checkpatch complained about poor
spacing, so I also fixed that here and in the nearby DIV_ROUND_UP.
CC: qemu-trivial@nongnu.org
CC: qemu-stable@nongnu.org
Signed-off-by: Eric Blake <eblake@redhat.com>
Reviewed-by: Laszlo Ersek <lersek@redhat.com>
Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
Signed-off-by: Michael Tokarev <mjt@tls.msk.ru>
2017-09-14 15:49:23 +02:00
|
|
|
* numbers); works even if d is a smaller type than n. */
|
2013-03-29 02:08:15 +01:00
|
|
|
#ifndef ROUND_UP
|
osdep: Fix ROUND_UP(64-bit, 32-bit)
When using bit-wise operations that exploit the power-of-two
nature of the second argument of ROUND_UP(), we still need to
ensure that the mask is as wide as the first argument (done
by using a ternary to force proper arithmetic promotion).
Unpatched, ROUND_UP(2ULL*1024*1024*1024*1024, 512U) produces 0,
instead of the intended 2TiB, because negation of an unsigned
32-bit quantity followed by widening to 64-bits does not
sign-extend the mask.
Broken since its introduction in commit 292c8e50 (v1.5.0).
Callers that passed the same width type to both macro parameters,
or that had other code to ensure the first parameter's maximum
runtime value did not exceed the second parameter's width, are
unaffected, but I did not audit to see which (if any) existing
clients of the macro could trigger incorrect behavior (I found
the bug while adding a new use of the macro).
While preparing the patch, checkpatch complained about poor
spacing, so I also fixed that here and in the nearby DIV_ROUND_UP.
CC: qemu-trivial@nongnu.org
CC: qemu-stable@nongnu.org
Signed-off-by: Eric Blake <eblake@redhat.com>
Reviewed-by: Laszlo Ersek <lersek@redhat.com>
Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
Signed-off-by: Michael Tokarev <mjt@tls.msk.ru>
2017-09-14 15:49:23 +02:00
|
|
|
#define ROUND_UP(n, d) (((n) + (d) - 1) & -(0 ? (n) : (d)))
|
2013-03-29 02:08:15 +01:00
|
|
|
#endif
|
|
|
|
|
2011-02-04 09:06:04 +01:00
|
|
|
#ifndef DIV_ROUND_UP
|
osdep: Fix ROUND_UP(64-bit, 32-bit)
When using bit-wise operations that exploit the power-of-two
nature of the second argument of ROUND_UP(), we still need to
ensure that the mask is as wide as the first argument (done
by using a ternary to force proper arithmetic promotion).
Unpatched, ROUND_UP(2ULL*1024*1024*1024*1024, 512U) produces 0,
instead of the intended 2TiB, because negation of an unsigned
32-bit quantity followed by widening to 64-bits does not
sign-extend the mask.
Broken since its introduction in commit 292c8e50 (v1.5.0).
Callers that passed the same width type to both macro parameters,
or that had other code to ensure the first parameter's maximum
runtime value did not exceed the second parameter's width, are
unaffected, but I did not audit to see which (if any) existing
clients of the macro could trigger incorrect behavior (I found
the bug while adding a new use of the macro).
While preparing the patch, checkpatch complained about poor
spacing, so I also fixed that here and in the nearby DIV_ROUND_UP.
CC: qemu-trivial@nongnu.org
CC: qemu-stable@nongnu.org
Signed-off-by: Eric Blake <eblake@redhat.com>
Reviewed-by: Laszlo Ersek <lersek@redhat.com>
Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
Signed-off-by: Michael Tokarev <mjt@tls.msk.ru>
2017-09-14 15:49:23 +02:00
|
|
|
#define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
|
2011-02-04 09:06:04 +01:00
|
|
|
#endif
|
|
|
|
|
2017-01-18 21:07:34 +01:00
|
|
|
/*
|
|
|
|
* &(x)[0] is always a pointer - if it's same type as x then the argument is a
|
|
|
|
* pointer, not an array.
|
|
|
|
*/
|
|
|
|
#define QEMU_IS_ARRAY(x) (!__builtin_types_compatible_p(typeof(x), \
|
|
|
|
typeof(&(x)[0])))
|
2008-03-11 22:01:02 +01:00
|
|
|
#ifndef ARRAY_SIZE
|
2017-01-18 21:07:34 +01:00
|
|
|
#define ARRAY_SIZE(x) ((sizeof(x) / sizeof((x)[0])) + \
|
|
|
|
QEMU_BUILD_BUG_ON_ZERO(!QEMU_IS_ARRAY(x)))
|
2008-03-11 22:01:02 +01:00
|
|
|
#endif
|
|
|
|
|
2011-06-07 05:34:10 +02:00
|
|
|
int qemu_daemon(int nochdir, int noclose);
|
2014-05-20 12:24:05 +02:00
|
|
|
void *qemu_try_memalign(size_t alignment, size_t size);
|
2007-12-24 15:33:24 +01:00
|
|
|
void *qemu_memalign(size_t alignment, size_t size);
|
2014-10-31 17:38:37 +01:00
|
|
|
void *qemu_anon_ram_alloc(size_t size, uint64_t *align);
|
2005-02-10 22:59:25 +01:00
|
|
|
void qemu_vfree(void *ptr);
|
2013-05-13 16:19:56 +02:00
|
|
|
void qemu_anon_ram_free(void *ptr, size_t size);
|
2004-02-16 23:12:40 +01:00
|
|
|
|
2010-09-25 13:26:05 +02:00
|
|
|
#define QEMU_MADV_INVALID -1
|
|
|
|
|
|
|
|
#if defined(CONFIG_MADVISE)
|
|
|
|
|
|
|
|
#define QEMU_MADV_WILLNEED MADV_WILLNEED
|
|
|
|
#define QEMU_MADV_DONTNEED MADV_DONTNEED
|
|
|
|
#ifdef MADV_DONTFORK
|
|
|
|
#define QEMU_MADV_DONTFORK MADV_DONTFORK
|
|
|
|
#else
|
|
|
|
#define QEMU_MADV_DONTFORK QEMU_MADV_INVALID
|
|
|
|
#endif
|
|
|
|
#ifdef MADV_MERGEABLE
|
|
|
|
#define QEMU_MADV_MERGEABLE MADV_MERGEABLE
|
|
|
|
#else
|
|
|
|
#define QEMU_MADV_MERGEABLE QEMU_MADV_INVALID
|
|
|
|
#endif
|
2014-06-10 13:15:22 +02:00
|
|
|
#ifdef MADV_UNMERGEABLE
|
|
|
|
#define QEMU_MADV_UNMERGEABLE MADV_UNMERGEABLE
|
|
|
|
#else
|
|
|
|
#define QEMU_MADV_UNMERGEABLE QEMU_MADV_INVALID
|
|
|
|
#endif
|
|
|
|
#ifdef MADV_DODUMP
|
|
|
|
#define QEMU_MADV_DODUMP MADV_DODUMP
|
|
|
|
#else
|
|
|
|
#define QEMU_MADV_DODUMP QEMU_MADV_INVALID
|
|
|
|
#endif
|
2012-08-02 21:44:16 +02:00
|
|
|
#ifdef MADV_DONTDUMP
|
|
|
|
#define QEMU_MADV_DONTDUMP MADV_DONTDUMP
|
|
|
|
#else
|
|
|
|
#define QEMU_MADV_DONTDUMP QEMU_MADV_INVALID
|
|
|
|
#endif
|
2012-10-05 21:47:57 +02:00
|
|
|
#ifdef MADV_HUGEPAGE
|
|
|
|
#define QEMU_MADV_HUGEPAGE MADV_HUGEPAGE
|
|
|
|
#else
|
|
|
|
#define QEMU_MADV_HUGEPAGE QEMU_MADV_INVALID
|
|
|
|
#endif
|
2015-11-05 19:10:37 +01:00
|
|
|
#ifdef MADV_NOHUGEPAGE
|
|
|
|
#define QEMU_MADV_NOHUGEPAGE MADV_NOHUGEPAGE
|
|
|
|
#else
|
|
|
|
#define QEMU_MADV_NOHUGEPAGE QEMU_MADV_INVALID
|
|
|
|
#endif
|
2017-08-24 21:23:14 +02:00
|
|
|
#ifdef MADV_REMOVE
|
|
|
|
#define QEMU_MADV_REMOVE MADV_REMOVE
|
|
|
|
#else
|
|
|
|
#define QEMU_MADV_REMOVE QEMU_MADV_INVALID
|
|
|
|
#endif
|
2010-09-25 13:26:05 +02:00
|
|
|
|
|
|
|
#elif defined(CONFIG_POSIX_MADVISE)
|
|
|
|
|
|
|
|
#define QEMU_MADV_WILLNEED POSIX_MADV_WILLNEED
|
|
|
|
#define QEMU_MADV_DONTNEED POSIX_MADV_DONTNEED
|
|
|
|
#define QEMU_MADV_DONTFORK QEMU_MADV_INVALID
|
|
|
|
#define QEMU_MADV_MERGEABLE QEMU_MADV_INVALID
|
2014-06-18 20:48:19 +02:00
|
|
|
#define QEMU_MADV_UNMERGEABLE QEMU_MADV_INVALID
|
|
|
|
#define QEMU_MADV_DODUMP QEMU_MADV_INVALID
|
2012-08-02 21:44:16 +02:00
|
|
|
#define QEMU_MADV_DONTDUMP QEMU_MADV_INVALID
|
2012-10-24 18:12:15 +02:00
|
|
|
#define QEMU_MADV_HUGEPAGE QEMU_MADV_INVALID
|
2015-11-05 19:10:37 +01:00
|
|
|
#define QEMU_MADV_NOHUGEPAGE QEMU_MADV_INVALID
|
2017-08-24 21:23:14 +02:00
|
|
|
#define QEMU_MADV_REMOVE QEMU_MADV_INVALID
|
2010-09-25 13:26:05 +02:00
|
|
|
|
|
|
|
#else /* no-op */
|
|
|
|
|
|
|
|
#define QEMU_MADV_WILLNEED QEMU_MADV_INVALID
|
|
|
|
#define QEMU_MADV_DONTNEED QEMU_MADV_INVALID
|
|
|
|
#define QEMU_MADV_DONTFORK QEMU_MADV_INVALID
|
|
|
|
#define QEMU_MADV_MERGEABLE QEMU_MADV_INVALID
|
2014-06-18 20:48:19 +02:00
|
|
|
#define QEMU_MADV_UNMERGEABLE QEMU_MADV_INVALID
|
|
|
|
#define QEMU_MADV_DODUMP QEMU_MADV_INVALID
|
2012-08-02 21:44:16 +02:00
|
|
|
#define QEMU_MADV_DONTDUMP QEMU_MADV_INVALID
|
2012-10-24 18:12:15 +02:00
|
|
|
#define QEMU_MADV_HUGEPAGE QEMU_MADV_INVALID
|
2015-11-05 19:10:37 +01:00
|
|
|
#define QEMU_MADV_NOHUGEPAGE QEMU_MADV_INVALID
|
2017-08-24 21:23:14 +02:00
|
|
|
#define QEMU_MADV_REMOVE QEMU_MADV_INVALID
|
2010-09-25 13:26:05 +02:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2017-06-07 19:33:29 +02:00
|
|
|
#ifdef _WIN32
|
|
|
|
#define HAVE_CHARDEV_SERIAL 1
|
|
|
|
#elif defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
|
|
|
|
|| defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \
|
|
|
|
|| defined(__GLIBC__)
|
|
|
|
#define HAVE_CHARDEV_SERIAL 1
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(__linux__) || defined(__FreeBSD__) || \
|
|
|
|
defined(__FreeBSD_kernel__) || defined(__DragonFly__)
|
|
|
|
#define HAVE_CHARDEV_PARPORT 1
|
|
|
|
#endif
|
|
|
|
|
2017-02-09 09:50:02 +01:00
|
|
|
#if defined(CONFIG_LINUX)
|
|
|
|
#ifndef BUS_MCEERR_AR
|
|
|
|
#define BUS_MCEERR_AR 4
|
|
|
|
#endif
|
|
|
|
#ifndef BUS_MCEERR_AO
|
|
|
|
#define BUS_MCEERR_AO 5
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2016-04-25 13:55:38 +02:00
|
|
|
#if defined(__linux__) && \
|
|
|
|
(defined(__x86_64__) || defined(__arm__) || defined(__aarch64__))
|
|
|
|
/* Use 2 MiB alignment so transparent hugepages can be used by KVM.
|
|
|
|
Valgrind does not support alignments larger than 1 MiB,
|
|
|
|
therefore we need special code which handles running on Valgrind. */
|
|
|
|
# define QEMU_VMALLOC_ALIGN (512 * 4096)
|
|
|
|
#elif defined(__linux__) && defined(__s390x__)
|
|
|
|
/* Use 1 MiB (segment size) alignment so gmap can be used by KVM. */
|
|
|
|
# define QEMU_VMALLOC_ALIGN (256 * 4096)
|
2017-12-08 17:57:28 +01:00
|
|
|
#elif defined(__linux__) && defined(__sparc__)
|
|
|
|
#include <sys/shm.h>
|
|
|
|
# define QEMU_VMALLOC_ALIGN MAX(getpagesize(), SHMLBA)
|
2016-04-25 13:55:38 +02:00
|
|
|
#else
|
|
|
|
# define QEMU_VMALLOC_ALIGN getpagesize()
|
|
|
|
#endif
|
|
|
|
|
2017-02-08 13:22:12 +01:00
|
|
|
#ifdef CONFIG_POSIX
|
|
|
|
struct qemu_signalfd_siginfo {
|
|
|
|
uint32_t ssi_signo; /* Signal number */
|
|
|
|
int32_t ssi_errno; /* Error number (unused) */
|
|
|
|
int32_t ssi_code; /* Signal code */
|
|
|
|
uint32_t ssi_pid; /* PID of sender */
|
|
|
|
uint32_t ssi_uid; /* Real UID of sender */
|
|
|
|
int32_t ssi_fd; /* File descriptor (SIGIO) */
|
|
|
|
uint32_t ssi_tid; /* Kernel timer ID (POSIX timers) */
|
|
|
|
uint32_t ssi_band; /* Band event (SIGIO) */
|
|
|
|
uint32_t ssi_overrun; /* POSIX timer overrun count */
|
|
|
|
uint32_t ssi_trapno; /* Trap number that caused signal */
|
|
|
|
int32_t ssi_status; /* Exit status or signal (SIGCHLD) */
|
|
|
|
int32_t ssi_int; /* Integer sent by sigqueue(2) */
|
|
|
|
uint64_t ssi_ptr; /* Pointer sent by sigqueue(2) */
|
|
|
|
uint64_t ssi_utime; /* User CPU time consumed (SIGCHLD) */
|
|
|
|
uint64_t ssi_stime; /* System CPU time consumed (SIGCHLD) */
|
|
|
|
uint64_t ssi_addr; /* Address that generated signal
|
|
|
|
(for hardware-generated signals) */
|
|
|
|
uint8_t pad[48]; /* Pad size to 128 bytes (allow for
|
|
|
|
additional fields in the future) */
|
|
|
|
};
|
|
|
|
|
|
|
|
int qemu_signalfd(const sigset_t *mask);
|
|
|
|
void sigaction_invoke(struct sigaction *action,
|
|
|
|
struct qemu_signalfd_siginfo *info);
|
|
|
|
#endif
|
|
|
|
|
2010-09-25 13:26:05 +02:00
|
|
|
int qemu_madvise(void *addr, size_t len, int advice);
|
2017-07-15 08:28:47 +02:00
|
|
|
int qemu_mprotect_rwx(void *addr, size_t size);
|
|
|
|
int qemu_mprotect_none(void *addr, size_t size);
|
2010-09-25 13:26:05 +02:00
|
|
|
|
2012-11-14 16:42:39 +01:00
|
|
|
int qemu_open(const char *name, int flags, ...);
|
|
|
|
int qemu_close(int fd);
|
2016-06-22 14:53:19 +02:00
|
|
|
#ifndef _WIN32
|
|
|
|
int qemu_dup(int fd);
|
|
|
|
#endif
|
2017-05-02 18:35:54 +02:00
|
|
|
int qemu_lock_fd(int fd, int64_t start, int64_t len, bool exclusive);
|
|
|
|
int qemu_unlock_fd(int fd, int64_t start, int64_t len);
|
|
|
|
int qemu_lock_fd_test(int fd, int64_t start, int64_t len, bool exclusive);
|
2017-08-11 13:44:46 +02:00
|
|
|
bool qemu_has_ofd_lock(void);
|
2012-11-14 16:42:39 +01:00
|
|
|
|
2011-06-02 19:58:06 +02:00
|
|
|
#if defined(__HAIKU__) && defined(__i386__)
|
|
|
|
#define FMT_pid "%ld"
|
2011-07-15 21:38:13 +02:00
|
|
|
#elif defined(WIN64)
|
|
|
|
#define FMT_pid "%" PRId64
|
2011-06-02 19:58:06 +02:00
|
|
|
#else
|
|
|
|
#define FMT_pid "%d"
|
|
|
|
#endif
|
|
|
|
|
2007-03-25 23:33:06 +02:00
|
|
|
int qemu_create_pidfile(const char *filename);
|
2011-03-15 12:26:31 +01:00
|
|
|
int qemu_get_thread_id(void);
|
2007-03-25 23:33:06 +02:00
|
|
|
|
2013-04-21 12:01:06 +02:00
|
|
|
#ifndef CONFIG_IOVEC
|
|
|
|
struct iovec {
|
|
|
|
void *iov_base;
|
|
|
|
size_t iov_len;
|
|
|
|
};
|
|
|
|
/*
|
|
|
|
* Use the same value as Linux for now.
|
|
|
|
*/
|
|
|
|
#define IOV_MAX 1024
|
|
|
|
|
|
|
|
ssize_t readv(int fd, const struct iovec *iov, int iov_cnt);
|
|
|
|
ssize_t writev(int fd, const struct iovec *iov, int iov_cnt);
|
|
|
|
#else
|
|
|
|
#include <sys/uio.h>
|
|
|
|
#endif
|
|
|
|
|
2011-03-13 11:30:52 +01:00
|
|
|
#ifdef _WIN32
|
|
|
|
static inline void qemu_timersub(const struct timeval *val1,
|
|
|
|
const struct timeval *val2,
|
|
|
|
struct timeval *res)
|
|
|
|
{
|
|
|
|
res->tv_sec = val1->tv_sec - val2->tv_sec;
|
|
|
|
if (val1->tv_usec < val2->tv_usec) {
|
|
|
|
res->tv_sec--;
|
|
|
|
res->tv_usec = val1->tv_usec - val2->tv_usec + 1000 * 1000;
|
|
|
|
} else {
|
|
|
|
res->tv_usec = val1->tv_usec - val2->tv_usec;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
#define qemu_timersub timersub
|
|
|
|
#endif
|
|
|
|
|
2012-03-28 15:42:05 +02:00
|
|
|
void qemu_set_cloexec(int fd);
|
|
|
|
|
2016-04-09 21:42:44 +02:00
|
|
|
/* Starting on QEMU 2.5, qemu_hw_version() returns "2.5+" by default
|
|
|
|
* instead of QEMU_VERSION, so setting hw_version on MachineClass
|
|
|
|
* is no longer mandatory.
|
|
|
|
*
|
|
|
|
* Do NOT change this string, or it will break compatibility on all
|
|
|
|
* machine classes that don't set hw_version.
|
|
|
|
*/
|
|
|
|
#define QEMU_HW_VERSION "2.5+"
|
|
|
|
|
2015-11-12 18:29:54 +01:00
|
|
|
/* QEMU "hardware version" setting. Used to replace code that exposed
|
2016-03-23 15:59:57 +01:00
|
|
|
* QEMU_VERSION to guests in the past and need to keep compatibility.
|
2015-11-12 18:29:54 +01:00
|
|
|
* Do not use qemu_hw_version() in new code.
|
|
|
|
*/
|
2015-10-30 20:36:08 +01:00
|
|
|
void qemu_set_hw_version(const char *);
|
|
|
|
const char *qemu_hw_version(void);
|
2012-05-30 05:35:51 +02:00
|
|
|
|
2012-08-03 20:39:21 +02:00
|
|
|
void fips_set_state(bool requested);
|
|
|
|
bool fips_get_state(void);
|
|
|
|
|
2013-05-18 06:31:48 +02:00
|
|
|
/* Return a dynamically allocated pathname denoting a file or directory that is
|
|
|
|
* appropriate for storing local state.
|
|
|
|
*
|
|
|
|
* @relative_pathname need not start with a directory separator; one will be
|
|
|
|
* added automatically.
|
|
|
|
*
|
|
|
|
* The caller is responsible for releasing the value returned with g_free()
|
|
|
|
* after use.
|
|
|
|
*/
|
|
|
|
char *qemu_get_local_state_pathname(const char *relative_pathname);
|
|
|
|
|
2014-02-10 07:48:51 +01:00
|
|
|
/* Find program directory, and save it for later usage with
|
|
|
|
* qemu_get_exec_dir().
|
|
|
|
* Try OS specific API first, if not working, parse from argv0. */
|
|
|
|
void qemu_init_exec_dir(const char *argv0);
|
|
|
|
|
|
|
|
/* Get the saved exec dir.
|
|
|
|
* Caller needs to release the returned string by g_free() */
|
|
|
|
char *qemu_get_exec_dir(void);
|
|
|
|
|
2013-06-04 20:24:49 +02:00
|
|
|
/**
|
|
|
|
* qemu_getauxval:
|
|
|
|
* @type: the auxiliary vector key to lookup
|
|
|
|
*
|
|
|
|
* Search the auxiliary vector for @type, returning the value
|
|
|
|
* or 0 if @type is not present.
|
|
|
|
*/
|
|
|
|
unsigned long qemu_getauxval(unsigned long type);
|
|
|
|
|
2013-11-14 11:54:16 +01:00
|
|
|
void qemu_set_tty_echo(int fd, bool echo);
|
|
|
|
|
2017-02-24 04:31:43 +01:00
|
|
|
void os_mem_prealloc(int fd, char *area, size_t sz, int smp_cpus,
|
|
|
|
Error **errp);
|
2014-05-14 11:43:21 +02:00
|
|
|
|
2016-09-27 17:24:56 +02:00
|
|
|
/**
|
|
|
|
* qemu_get_pid_name:
|
|
|
|
* @pid: pid of a process
|
|
|
|
*
|
|
|
|
* For given @pid fetch its name. Caller is responsible for
|
|
|
|
* freeing the string when no longer needed.
|
|
|
|
* Returns allocated string on success, NULL on failure.
|
|
|
|
*/
|
|
|
|
char *qemu_get_pid_name(pid_t pid);
|
|
|
|
|
2015-08-28 15:40:01 +02:00
|
|
|
/**
|
|
|
|
* qemu_fork:
|
|
|
|
*
|
|
|
|
* A version of fork that avoids signal handler race
|
|
|
|
* conditions that can lead to child process getting
|
|
|
|
* signals that are otherwise only expected by the
|
|
|
|
* parent. It also resets all signal handlers to the
|
|
|
|
* default settings.
|
|
|
|
*
|
|
|
|
* Returns 0 to child process, pid number to parent
|
|
|
|
* or -1 on failure.
|
|
|
|
*/
|
|
|
|
pid_t qemu_fork(Error **errp);
|
|
|
|
|
2017-07-15 09:24:27 +02:00
|
|
|
/* Using intptr_t ensures that qemu_*_page_mask is sign-extended even
|
|
|
|
* when intptr_t is 32-bit and we are aligning a long long.
|
|
|
|
*/
|
|
|
|
extern uintptr_t qemu_real_host_page_size;
|
|
|
|
extern intptr_t qemu_real_host_page_mask;
|
|
|
|
|
2017-06-07 02:17:04 +02:00
|
|
|
extern int qemu_icache_linesize;
|
|
|
|
extern int qemu_dcache_linesize;
|
|
|
|
|
2004-02-16 23:12:40 +01:00
|
|
|
#endif
|