4 #include "config-host.h"
11 #include <sys/signal.h>
17 #define WIFEXITED(x) 1
18 #define WEXITSTATUS(x) (x)
23 #if defined(CONFIG_SOLARIS) && CONFIG_SOLARIS_VERSION < 10
24 /* [u]int_fast*_t not in <sys/int_types.h> */
25 typedef unsigned char uint_fast8_t;
26 typedef unsigned int uint_fast16_t;
27 typedef signed int int_fast16_t;
31 #define xglue(x, y) x ## y
32 #define glue(x, y) xglue(x, y)
33 #define stringify(s) tostring(s)
34 #define tostring(s) #s
39 #define __builtin_expect(x, n) (x)
42 #define likely(x) __builtin_expect(!!(x), 1)
43 #define unlikely(x) __builtin_expect(!!(x), 0)
47 #define container_of(ptr, type, member) ({ \
48 const typeof(((type *) 0)->member) *__mptr = (ptr); \
49 (type *) ((char *) __mptr - offsetof(type, member));})
52 /* Convert from a base type to a parent type, with compile time checking. */
54 #define DO_UPCAST(type, field, dev) ( __extension__ ( { \
55 char __attribute__((unused)) offset_must_be_zero[ \
56 -offsetof(type, field)]; \
57 container_of(dev, type, field);}))
59 #define DO_UPCAST(type, field, dev) container_of(dev, type, field)
62 #define typeof_field(type, field) typeof(((type *)0)->field)
63 #define type_check(t1,t2) ((t1*)0 - (t2*)0)
66 #define MIN(a, b) (((a) < (b)) ? (a) : (b))
69 #define MAX(a, b) (((a) > (b)) ? (a) : (b))
72 /* Minimum function that returns zero only iff both values are zero.
73 * Intended for use with unsigned values only. */
75 #define MIN_NON_ZERO(a, b) (((a) != 0 && (a) < (b)) ? (a) : (b))
79 #define ROUND_UP(n,d) (((n) + (d) - 1) & -(d))
83 #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
87 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
91 #if !((__GNUC__ < 3) || defined(__APPLE__))
94 #define inline __attribute__ (( always_inline )) __inline__
99 #define inline always_inline
102 #define qemu_printf printf
104 int qemu_daemon(int nochdir
, int noclose
);
105 void *qemu_try_memalign(size_t alignment
, size_t size
);
106 void *qemu_memalign(size_t alignment
, size_t size
);
107 void *qemu_anon_ram_alloc(size_t size
, uint64_t *align
);
108 void qemu_vfree(void *ptr
);
109 void qemu_anon_ram_free(void *ptr
, size_t size
);
111 #define QEMU_MADV_INVALID -1
113 #if defined(CONFIG_MADVISE)
115 #define QEMU_MADV_WILLNEED MADV_WILLNEED
116 #define QEMU_MADV_DONTNEED MADV_DONTNEED
118 #define QEMU_MADV_DONTFORK MADV_DONTFORK
120 #define QEMU_MADV_DONTFORK QEMU_MADV_INVALID
122 #ifdef MADV_MERGEABLE
123 #define QEMU_MADV_MERGEABLE MADV_MERGEABLE
125 #define QEMU_MADV_MERGEABLE QEMU_MADV_INVALID
127 #ifdef MADV_UNMERGEABLE
128 #define QEMU_MADV_UNMERGEABLE MADV_UNMERGEABLE
130 #define QEMU_MADV_UNMERGEABLE QEMU_MADV_INVALID
133 #define QEMU_MADV_DODUMP MADV_DODUMP
135 #define QEMU_MADV_DODUMP QEMU_MADV_INVALID
138 #define QEMU_MADV_DONTDUMP MADV_DONTDUMP
140 #define QEMU_MADV_DONTDUMP QEMU_MADV_INVALID
143 #define QEMU_MADV_HUGEPAGE MADV_HUGEPAGE
145 #define QEMU_MADV_HUGEPAGE QEMU_MADV_INVALID
148 #elif defined(CONFIG_POSIX_MADVISE)
150 #define QEMU_MADV_WILLNEED POSIX_MADV_WILLNEED
151 #define QEMU_MADV_DONTNEED POSIX_MADV_DONTNEED
152 #define QEMU_MADV_DONTFORK QEMU_MADV_INVALID
153 #define QEMU_MADV_MERGEABLE QEMU_MADV_INVALID
154 #define QEMU_MADV_UNMERGEABLE QEMU_MADV_INVALID
155 #define QEMU_MADV_DODUMP QEMU_MADV_INVALID
156 #define QEMU_MADV_DONTDUMP QEMU_MADV_INVALID
157 #define QEMU_MADV_HUGEPAGE QEMU_MADV_INVALID
161 #define QEMU_MADV_WILLNEED QEMU_MADV_INVALID
162 #define QEMU_MADV_DONTNEED QEMU_MADV_INVALID
163 #define QEMU_MADV_DONTFORK QEMU_MADV_INVALID
164 #define QEMU_MADV_MERGEABLE QEMU_MADV_INVALID
165 #define QEMU_MADV_UNMERGEABLE QEMU_MADV_INVALID
166 #define QEMU_MADV_DODUMP QEMU_MADV_INVALID
167 #define QEMU_MADV_DONTDUMP QEMU_MADV_INVALID
168 #define QEMU_MADV_HUGEPAGE QEMU_MADV_INVALID
172 int qemu_madvise(void *addr
, size_t len
, int advice
);
174 int qemu_open(const char *name
, int flags
, ...);
175 int qemu_close(int fd
);
177 #if defined(__HAIKU__) && defined(__i386__)
178 #define FMT_pid "%ld"
180 #define FMT_pid "%" PRId64
185 int qemu_create_pidfile(const char *filename
);
186 int qemu_get_thread_id(void);
194 * Use the same value as Linux for now.
198 ssize_t
readv(int fd
, const struct iovec
*iov
, int iov_cnt
);
199 ssize_t
writev(int fd
, const struct iovec
*iov
, int iov_cnt
);
205 static inline void qemu_timersub(const struct timeval
*val1
,
206 const struct timeval
*val2
,
209 res
->tv_sec
= val1
->tv_sec
- val2
->tv_sec
;
210 if (val1
->tv_usec
< val2
->tv_usec
) {
212 res
->tv_usec
= val1
->tv_usec
- val2
->tv_usec
+ 1000 * 1000;
214 res
->tv_usec
= val1
->tv_usec
- val2
->tv_usec
;
218 #define qemu_timersub timersub
221 void qemu_set_cloexec(int fd
);
223 void qemu_set_version(const char *);
224 const char *qemu_get_version(void);
226 void fips_set_state(bool requested
);
227 bool fips_get_state(void);
229 /* Return a dynamically allocated pathname denoting a file or directory that is
230 * appropriate for storing local state.
232 * @relative_pathname need not start with a directory separator; one will be
233 * added automatically.
235 * The caller is responsible for releasing the value returned with g_free()
238 char *qemu_get_local_state_pathname(const char *relative_pathname
);
240 /* Find program directory, and save it for later usage with
241 * qemu_get_exec_dir().
242 * Try OS specific API first, if not working, parse from argv0. */
243 void qemu_init_exec_dir(const char *argv0
);
245 /* Get the saved exec dir.
246 * Caller needs to release the returned string by g_free() */
247 char *qemu_get_exec_dir(void);
251 * @type: the auxiliary vector key to lookup
253 * Search the auxiliary vector for @type, returning the value
254 * or 0 if @type is not present.
256 unsigned long qemu_getauxval(unsigned long type
);
258 void qemu_set_tty_echo(int fd
, bool echo
);
260 void os_mem_prealloc(int fd
, char *area
, size_t sz
);