2 * OS includes and handling of OS dependencies
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).
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, compiler.h, os-posix.h and os-win32.h, all of which
11 * are doing a similar job to this file and are under similar constraints.
13 * This header also contains prototypes for functions defined in
14 * os-*.c and util/oslib-*.c; those would probably be better split
15 * out into separate header files.
17 * In an ideal world this header would contain only:
18 * (1) things which everybody needs
19 * (2) things without which code would work on most platforms but
20 * fail to compile or misbehave on a minority of host OSes
22 * This work is licensed under the terms of the GNU GPL, version 2 or later.
23 * See the COPYING file in the top-level directory.
28 #include "config-host.h"
30 #include "config-target.h"
32 #include "qemu/compiler.h"
34 /* Older versions of C++ don't get definitions of various macros from
35 * stdlib.h unless we define these macros before first inclusion of
38 #ifndef __STDC_CONSTANT_MACROS
39 #define __STDC_CONSTANT_MACROS
41 #ifndef __STDC_LIMIT_MACROS
42 #define __STDC_LIMIT_MACROS
44 #ifndef __STDC_FORMAT_MACROS
45 #define __STDC_FORMAT_MACROS
48 /* The following block of code temporarily renames the daemon() function so the
49 * compiler does not see the warning associated with it in stdlib.h on OSX
52 #define daemon qemu_fake_daemon_function
55 extern int daemon(int, int);
62 #include <sys/types.h>
69 /* Put unistd.h before time.h as that triggers localtime_r/gmtime_r
70 * function availability on recentish Mingw-w64 platforms. */
82 #include <sys/signal.h>
88 #define WIFEXITED(x) 1
89 #define WEXITSTATUS(x) (x)
93 #include "sysemu/os-win32.h"
97 #include "sysemu/os-posix.h"
100 #include "glib-compat.h"
102 #include "qapi/error.h"
105 #define O_LARGEFILE 0
110 #ifndef MAP_ANONYMOUS
111 #define MAP_ANONYMOUS MAP_ANON
114 #define ENOMEDIUM ENODEV
116 #if !defined(ENOTSUP)
119 #if !defined(ECANCELED)
120 #define ECANCELED 4097
122 #if !defined(EMEDIUMTYPE)
123 #define EMEDIUMTYPE 4098
126 #define TIME_MAX LONG_MAX
130 #define MIN(a, b) (((a) < (b)) ? (a) : (b))
133 #define MAX(a, b) (((a) > (b)) ? (a) : (b))
136 /* Minimum function that returns zero only iff both values are zero.
137 * Intended for use with unsigned values only. */
139 #define MIN_NON_ZERO(a, b) (((a) != 0 && (a) < (b)) ? (a) : (b))
143 #define ROUND_UP(n,d) (((n) + (d) - 1) & -(d))
147 #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
151 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
154 int qemu_daemon(int nochdir
, int noclose
);
155 void *qemu_try_memalign(size_t alignment
, size_t size
);
156 void *qemu_memalign(size_t alignment
, size_t size
);
157 void *qemu_anon_ram_alloc(size_t size
, uint64_t *align
);
158 void qemu_vfree(void *ptr
);
159 void qemu_anon_ram_free(void *ptr
, size_t size
);
161 #define QEMU_MADV_INVALID -1
163 #if defined(CONFIG_MADVISE)
165 #include <sys/mman.h>
167 #define QEMU_MADV_WILLNEED MADV_WILLNEED
168 #define QEMU_MADV_DONTNEED MADV_DONTNEED
170 #define QEMU_MADV_DONTFORK MADV_DONTFORK
172 #define QEMU_MADV_DONTFORK QEMU_MADV_INVALID
174 #ifdef MADV_MERGEABLE
175 #define QEMU_MADV_MERGEABLE MADV_MERGEABLE
177 #define QEMU_MADV_MERGEABLE QEMU_MADV_INVALID
179 #ifdef MADV_UNMERGEABLE
180 #define QEMU_MADV_UNMERGEABLE MADV_UNMERGEABLE
182 #define QEMU_MADV_UNMERGEABLE QEMU_MADV_INVALID
185 #define QEMU_MADV_DODUMP MADV_DODUMP
187 #define QEMU_MADV_DODUMP QEMU_MADV_INVALID
190 #define QEMU_MADV_DONTDUMP MADV_DONTDUMP
192 #define QEMU_MADV_DONTDUMP QEMU_MADV_INVALID
195 #define QEMU_MADV_HUGEPAGE MADV_HUGEPAGE
197 #define QEMU_MADV_HUGEPAGE QEMU_MADV_INVALID
199 #ifdef MADV_NOHUGEPAGE
200 #define QEMU_MADV_NOHUGEPAGE MADV_NOHUGEPAGE
202 #define QEMU_MADV_NOHUGEPAGE QEMU_MADV_INVALID
205 #elif defined(CONFIG_POSIX_MADVISE)
207 #define QEMU_MADV_WILLNEED POSIX_MADV_WILLNEED
208 #define QEMU_MADV_DONTNEED POSIX_MADV_DONTNEED
209 #define QEMU_MADV_DONTFORK QEMU_MADV_INVALID
210 #define QEMU_MADV_MERGEABLE QEMU_MADV_INVALID
211 #define QEMU_MADV_UNMERGEABLE QEMU_MADV_INVALID
212 #define QEMU_MADV_DODUMP QEMU_MADV_INVALID
213 #define QEMU_MADV_DONTDUMP QEMU_MADV_INVALID
214 #define QEMU_MADV_HUGEPAGE QEMU_MADV_INVALID
215 #define QEMU_MADV_NOHUGEPAGE QEMU_MADV_INVALID
219 #define QEMU_MADV_WILLNEED QEMU_MADV_INVALID
220 #define QEMU_MADV_DONTNEED QEMU_MADV_INVALID
221 #define QEMU_MADV_DONTFORK QEMU_MADV_INVALID
222 #define QEMU_MADV_MERGEABLE QEMU_MADV_INVALID
223 #define QEMU_MADV_UNMERGEABLE QEMU_MADV_INVALID
224 #define QEMU_MADV_DODUMP QEMU_MADV_INVALID
225 #define QEMU_MADV_DONTDUMP QEMU_MADV_INVALID
226 #define QEMU_MADV_HUGEPAGE QEMU_MADV_INVALID
227 #define QEMU_MADV_NOHUGEPAGE QEMU_MADV_INVALID
231 int qemu_madvise(void *addr
, size_t len
, int advice
);
233 int qemu_open(const char *name
, int flags
, ...);
234 int qemu_close(int fd
);
236 #if defined(__HAIKU__) && defined(__i386__)
237 #define FMT_pid "%ld"
239 #define FMT_pid "%" PRId64
244 int qemu_create_pidfile(const char *filename
);
245 int qemu_get_thread_id(void);
253 * Use the same value as Linux for now.
257 ssize_t
readv(int fd
, const struct iovec
*iov
, int iov_cnt
);
258 ssize_t
writev(int fd
, const struct iovec
*iov
, int iov_cnt
);
264 static inline void qemu_timersub(const struct timeval
*val1
,
265 const struct timeval
*val2
,
268 res
->tv_sec
= val1
->tv_sec
- val2
->tv_sec
;
269 if (val1
->tv_usec
< val2
->tv_usec
) {
271 res
->tv_usec
= val1
->tv_usec
- val2
->tv_usec
+ 1000 * 1000;
273 res
->tv_usec
= val1
->tv_usec
- val2
->tv_usec
;
277 #define qemu_timersub timersub
280 void qemu_set_cloexec(int fd
);
282 /* QEMU "hardware version" setting. Used to replace code that exposed
283 * QEMU_VERSION to guests in the past and need to keep compatibilty.
284 * Do not use qemu_hw_version() in new code.
286 void qemu_set_hw_version(const char *);
287 const char *qemu_hw_version(void);
289 void fips_set_state(bool requested
);
290 bool fips_get_state(void);
292 /* Return a dynamically allocated pathname denoting a file or directory that is
293 * appropriate for storing local state.
295 * @relative_pathname need not start with a directory separator; one will be
296 * added automatically.
298 * The caller is responsible for releasing the value returned with g_free()
301 char *qemu_get_local_state_pathname(const char *relative_pathname
);
303 /* Find program directory, and save it for later usage with
304 * qemu_get_exec_dir().
305 * Try OS specific API first, if not working, parse from argv0. */
306 void qemu_init_exec_dir(const char *argv0
);
308 /* Get the saved exec dir.
309 * Caller needs to release the returned string by g_free() */
310 char *qemu_get_exec_dir(void);
314 * @type: the auxiliary vector key to lookup
316 * Search the auxiliary vector for @type, returning the value
317 * or 0 if @type is not present.
319 unsigned long qemu_getauxval(unsigned long type
);
321 void qemu_set_tty_echo(int fd
, bool echo
);
323 void os_mem_prealloc(int fd
, char *area
, size_t sz
);
325 int qemu_read_password(char *buf
, int buf_size
);
330 * A version of fork that avoids signal handler race
331 * conditions that can lead to child process getting
332 * signals that are otherwise only expected by the
333 * parent. It also resets all signal handlers to the
336 * Returns 0 to child process, pid number to parent
339 pid_t
qemu_fork(Error
**errp
);