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, 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.
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.
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
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.
30 #include "config-host.h"
32 #include "config-target.h"
34 #include "qemu/compiler.h"
36 /* Older versions of C++ don't get definitions of various macros from
37 * stdlib.h unless we define these macros before first inclusion of
40 #ifndef __STDC_CONSTANT_MACROS
41 #define __STDC_CONSTANT_MACROS
43 #ifndef __STDC_LIMIT_MACROS
44 #define __STDC_LIMIT_MACROS
46 #ifndef __STDC_FORMAT_MACROS
47 #define __STDC_FORMAT_MACROS
50 /* The following block of code temporarily renames the daemon() function so the
51 * compiler does not see the warning associated with it in stdlib.h on OSX
54 #define daemon qemu_fake_daemon_function
57 extern int daemon(int, int);
64 #include <sys/types.h>
71 /* Put unistd.h before time.h as that triggers localtime_r/gmtime_r
72 * function availability on recentish Mingw-w64 platforms. */
81 /* setjmp must be declared before sysemu/os-win32.h
82 * because it is redefined there. */
87 #include <sys/signal.h>
93 #define WIFEXITED(x) 1
94 #define WEXITSTATUS(x) (x)
98 #include "sysemu/os-win32.h"
102 #include "sysemu/os-posix.h"
105 #include "glib-compat.h"
106 #include "qemu/typedefs.h"
109 #define O_LARGEFILE 0
114 #ifndef MAP_ANONYMOUS
115 #define MAP_ANONYMOUS MAP_ANON
118 #define ENOMEDIUM ENODEV
120 #if !defined(ENOTSUP)
123 #if !defined(ECANCELED)
124 #define ECANCELED 4097
126 #if !defined(EMEDIUMTYPE)
127 #define EMEDIUMTYPE 4098
130 #define TIME_MAX LONG_MAX
133 /* HOST_LONG_BITS is the size of a native pointer in bits. */
134 #if UINTPTR_MAX == UINT32_MAX
135 # define HOST_LONG_BITS 32
136 #elif UINTPTR_MAX == UINT64_MAX
137 # define HOST_LONG_BITS 64
139 # error Unknown pointer size
143 #define MIN(a, b) (((a) < (b)) ? (a) : (b))
146 #define MAX(a, b) (((a) > (b)) ? (a) : (b))
149 /* Minimum function that returns zero only iff both values are zero.
150 * Intended for use with unsigned values only. */
152 #define MIN_NON_ZERO(a, b) (((a) != 0 && (a) < (b)) ? (a) : (b))
155 /* Round number down to multiple */
156 #define QEMU_ALIGN_DOWN(n, m) ((n) / (m) * (m))
158 /* Round number up to multiple */
159 #define QEMU_ALIGN_UP(n, m) QEMU_ALIGN_DOWN((n) + (m) - 1, (m))
162 #define ROUND_UP(n,d) (((n) + (d) - 1) & -(d))
166 #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
170 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
173 int qemu_daemon(int nochdir, int noclose);
174 void *qemu_try_memalign(size_t alignment, size_t size);
175 void *qemu_memalign(size_t alignment, size_t size);
176 void *qemu_anon_ram_alloc(size_t size, uint64_t *align);
177 void qemu_vfree(void *ptr);
178 void qemu_anon_ram_free(void *ptr, size_t size);
180 #define QEMU_MADV_INVALID -1
182 #if defined(CONFIG_MADVISE)
184 #include <sys/mman.h>
186 #define QEMU_MADV_WILLNEED MADV_WILLNEED
187 #define QEMU_MADV_DONTNEED MADV_DONTNEED
189 #define QEMU_MADV_DONTFORK MADV_DONTFORK
191 #define QEMU_MADV_DONTFORK QEMU_MADV_INVALID
193 #ifdef MADV_MERGEABLE
194 #define QEMU_MADV_MERGEABLE MADV_MERGEABLE
196 #define QEMU_MADV_MERGEABLE QEMU_MADV_INVALID
198 #ifdef MADV_UNMERGEABLE
199 #define QEMU_MADV_UNMERGEABLE MADV_UNMERGEABLE
201 #define QEMU_MADV_UNMERGEABLE QEMU_MADV_INVALID
204 #define QEMU_MADV_DODUMP MADV_DODUMP
206 #define QEMU_MADV_DODUMP QEMU_MADV_INVALID
209 #define QEMU_MADV_DONTDUMP MADV_DONTDUMP
211 #define QEMU_MADV_DONTDUMP QEMU_MADV_INVALID
214 #define QEMU_MADV_HUGEPAGE MADV_HUGEPAGE
216 #define QEMU_MADV_HUGEPAGE QEMU_MADV_INVALID
218 #ifdef MADV_NOHUGEPAGE
219 #define QEMU_MADV_NOHUGEPAGE MADV_NOHUGEPAGE
221 #define QEMU_MADV_NOHUGEPAGE QEMU_MADV_INVALID
224 #elif defined(CONFIG_POSIX_MADVISE)
226 #define QEMU_MADV_WILLNEED POSIX_MADV_WILLNEED
227 #define QEMU_MADV_DONTNEED POSIX_MADV_DONTNEED
228 #define QEMU_MADV_DONTFORK QEMU_MADV_INVALID
229 #define QEMU_MADV_MERGEABLE QEMU_MADV_INVALID
230 #define QEMU_MADV_UNMERGEABLE QEMU_MADV_INVALID
231 #define QEMU_MADV_DODUMP QEMU_MADV_INVALID
232 #define QEMU_MADV_DONTDUMP QEMU_MADV_INVALID
233 #define QEMU_MADV_HUGEPAGE QEMU_MADV_INVALID
234 #define QEMU_MADV_NOHUGEPAGE QEMU_MADV_INVALID
238 #define QEMU_MADV_WILLNEED QEMU_MADV_INVALID
239 #define QEMU_MADV_DONTNEED QEMU_MADV_INVALID
240 #define QEMU_MADV_DONTFORK QEMU_MADV_INVALID
241 #define QEMU_MADV_MERGEABLE QEMU_MADV_INVALID
242 #define QEMU_MADV_UNMERGEABLE QEMU_MADV_INVALID
243 #define QEMU_MADV_DODUMP QEMU_MADV_INVALID
244 #define QEMU_MADV_DONTDUMP QEMU_MADV_INVALID
245 #define QEMU_MADV_HUGEPAGE QEMU_MADV_INVALID
246 #define QEMU_MADV_NOHUGEPAGE QEMU_MADV_INVALID
250 int qemu_madvise(void *addr, size_t len, int advice);
252 int qemu_open(const char *name, int flags, ...);
253 int qemu_close(int fd);
255 #if defined(__HAIKU__) && defined(__i386__)
256 #define FMT_pid "%ld"
258 #define FMT_pid "%" PRId64
263 int qemu_create_pidfile(const char *filename);
264 int qemu_get_thread_id(void);
272 * Use the same value as Linux for now.
276 ssize_t readv(int fd, const struct iovec *iov, int iov_cnt);
277 ssize_t writev(int fd, const struct iovec *iov, int iov_cnt);
283 static inline void qemu_timersub(const struct timeval *val1,
284 const struct timeval *val2,
287 res->tv_sec = val1->tv_sec - val2->tv_sec;
288 if (val1->tv_usec < val2->tv_usec) {
290 res->tv_usec = val1->tv_usec - val2->tv_usec + 1000 * 1000;
292 res->tv_usec = val1->tv_usec - val2->tv_usec;
296 #define qemu_timersub timersub
299 void qemu_set_cloexec(int fd);
301 /* QEMU "hardware version" setting. Used to replace code that exposed
302 * QEMU_VERSION to guests in the past and need to keep compatibilty.
303 * Do not use qemu_hw_version() in new code.
305 void qemu_set_hw_version(const char *);
306 const char *qemu_hw_version(void);
308 void fips_set_state(bool requested);
309 bool fips_get_state(void);
311 /* Return a dynamically allocated pathname denoting a file or directory that is
312 * appropriate for storing local state.
314 * @relative_pathname need not start with a directory separator; one will be
315 * added automatically.
317 * The caller is responsible for releasing the value returned with g_free()
320 char *qemu_get_local_state_pathname(const char *relative_pathname);
322 /* Find program directory, and save it for later usage with
323 * qemu_get_exec_dir().
324 * Try OS specific API first, if not working, parse from argv0. */
325 void qemu_init_exec_dir(const char *argv0);
327 /* Get the saved exec dir.
328 * Caller needs to release the returned string by g_free() */
329 char *qemu_get_exec_dir(void);
333 * @type: the auxiliary vector key to lookup
335 * Search the auxiliary vector for @type, returning the value
336 * or 0 if @type is not present.
338 unsigned long qemu_getauxval(unsigned long type);
340 void qemu_set_tty_echo(int fd, bool echo);
342 void os_mem_prealloc(int fd, char *area, size_t sz);
344 int qemu_read_password(char *buf, int buf_size);
349 * A version of fork that avoids signal handler race
350 * conditions that can lead to child process getting
351 * signals that are otherwise only expected by the
352 * parent. It also resets all signal handlers to the
355 * Returns 0 to child process, pid number to parent
358 pid_t qemu_fork(Error **errp);