]>
Commit | Line | Data |
---|---|---|
03557b9a PM |
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 | |
da34e65c MA |
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. | |
03557b9a PM |
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 | */ | |
ea88812f FB |
27 | #ifndef QEMU_OSDEP_H |
28 | #define QEMU_OSDEP_H | |
29 | ||
9adea5f7 | 30 | #include "config-host.h" |
b1e34d1c PM |
31 | #ifdef NEED_CPU_H |
32 | #include "config-target.h" | |
33 | #endif | |
49120868 | 34 | #include "qemu/compiler.h" |
d5db2ec1 | 35 | |
79f56d82 PM |
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 | |
38 | * that system header. | |
39 | */ | |
40 | #ifndef __STDC_CONSTANT_MACROS | |
41 | #define __STDC_CONSTANT_MACROS | |
42 | #endif | |
43 | #ifndef __STDC_LIMIT_MACROS | |
44 | #define __STDC_LIMIT_MACROS | |
45 | #endif | |
46 | #ifndef __STDC_FORMAT_MACROS | |
47 | #define __STDC_FORMAT_MACROS | |
48 | #endif | |
49 | ||
d5db2ec1 PM |
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 | |
52 | */ | |
53 | #ifdef __APPLE__ | |
54 | #define daemon qemu_fake_daemon_function | |
55 | #include <stdlib.h> | |
56 | #undef daemon | |
57 | extern int daemon(int, int); | |
58 | #endif | |
59 | ||
ea88812f | 60 | #include <stdarg.h> |
de5071c5 | 61 | #include <stddef.h> |
0f66998f | 62 | #include <stdbool.h> |
a2b257d6 | 63 | #include <stdint.h> |
128ab2ff | 64 | #include <sys/types.h> |
bfe7e449 PM |
65 | #include <stdlib.h> |
66 | #include <stdio.h> | |
67 | #include <string.h> | |
68 | #include <strings.h> | |
69 | #include <inttypes.h> | |
70 | #include <limits.h> | |
4d9310f4 DB |
71 | /* Put unistd.h before time.h as that triggers localtime_r/gmtime_r |
72 | * function availability on recentish Mingw-w64 platforms. */ | |
73 | #include <unistd.h> | |
bfe7e449 PM |
74 | #include <time.h> |
75 | #include <ctype.h> | |
76 | #include <errno.h> | |
bfe7e449 PM |
77 | #include <fcntl.h> |
78 | #include <sys/stat.h> | |
79 | #include <sys/time.h> | |
80 | #include <assert.h> | |
81 | #include <signal.h> | |
82 | ||
98b2d199 | 83 | #ifdef __OpenBSD__ |
128ab2ff BS |
84 | #include <sys/signal.h> |
85 | #endif | |
ea88812f | 86 | |
13c7b2da PB |
87 | #ifndef _WIN32 |
88 | #include <sys/wait.h> | |
89 | #else | |
90 | #define WIFEXITED(x) 1 | |
91 | #define WEXITSTATUS(x) (x) | |
92 | #endif | |
93 | ||
bfe7e449 PM |
94 | #ifdef _WIN32 |
95 | #include "sysemu/os-win32.h" | |
96 | #endif | |
97 | ||
98 | #ifdef CONFIG_POSIX | |
99 | #include "sysemu/os-posix.h" | |
100 | #endif | |
f7b4a940 | 101 | |
529490e5 | 102 | #include "glib-compat.h" |
da34e65c | 103 | #include "qemu/typedefs.h" |
57cb38b3 | 104 | |
bfe7e449 PM |
105 | #ifndef O_LARGEFILE |
106 | #define O_LARGEFILE 0 | |
107 | #endif | |
108 | #ifndef O_BINARY | |
109 | #define O_BINARY 0 | |
110 | #endif | |
111 | #ifndef MAP_ANONYMOUS | |
112 | #define MAP_ANONYMOUS MAP_ANON | |
113 | #endif | |
114 | #ifndef ENOMEDIUM | |
115 | #define ENOMEDIUM ENODEV | |
116 | #endif | |
117 | #if !defined(ENOTSUP) | |
118 | #define ENOTSUP 4096 | |
119 | #endif | |
120 | #if !defined(ECANCELED) | |
121 | #define ECANCELED 4097 | |
122 | #endif | |
123 | #if !defined(EMEDIUMTYPE) | |
124 | #define EMEDIUMTYPE 4098 | |
125 | #endif | |
126 | #ifndef TIME_MAX | |
127 | #define TIME_MAX LONG_MAX | |
128 | #endif | |
129 | ||
a8139632 MA |
130 | /* HOST_LONG_BITS is the size of a native pointer in bits. */ |
131 | #if UINTPTR_MAX == UINT32_MAX | |
132 | # define HOST_LONG_BITS 32 | |
133 | #elif UINTPTR_MAX == UINT64_MAX | |
134 | # define HOST_LONG_BITS 64 | |
135 | #else | |
136 | # error Unknown pointer size | |
137 | #endif | |
138 | ||
df2542c7 JM |
139 | #ifndef MIN |
140 | #define MIN(a, b) (((a) < (b)) ? (a) : (b)) | |
141 | #endif | |
142 | #ifndef MAX | |
143 | #define MAX(a, b) (((a) > (b)) ? (a) : (b)) | |
144 | #endif | |
145 | ||
ac3a8726 PL |
146 | /* Minimum function that returns zero only iff both values are zero. |
147 | * Intended for use with unsigned values only. */ | |
148 | #ifndef MIN_NON_ZERO | |
149 | #define MIN_NON_ZERO(a, b) (((a) != 0 && (a) < (b)) ? (a) : (b)) | |
150 | #endif | |
151 | ||
292c8e50 PB |
152 | #ifndef ROUND_UP |
153 | #define ROUND_UP(n,d) (((n) + (d) - 1) & -(d)) | |
154 | #endif | |
155 | ||
e0e53b2f CC |
156 | #ifndef DIV_ROUND_UP |
157 | #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d)) | |
158 | #endif | |
159 | ||
0954d0d9 BS |
160 | #ifndef ARRAY_SIZE |
161 | #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) | |
162 | #endif | |
163 | ||
f97742d0 | 164 | int qemu_daemon(int nochdir, int noclose); |
7d2a35cc | 165 | void *qemu_try_memalign(size_t alignment, size_t size); |
33f00271 | 166 | void *qemu_memalign(size_t alignment, size_t size); |
a2b257d6 | 167 | void *qemu_anon_ram_alloc(size_t size, uint64_t *align); |
49b470eb | 168 | void qemu_vfree(void *ptr); |
e7a09b92 | 169 | void qemu_anon_ram_free(void *ptr, size_t size); |
ea88812f | 170 | |
e78815a5 AF |
171 | #define QEMU_MADV_INVALID -1 |
172 | ||
173 | #if defined(CONFIG_MADVISE) | |
174 | ||
ebf81150 DDAG |
175 | #include <sys/mman.h> |
176 | ||
e78815a5 AF |
177 | #define QEMU_MADV_WILLNEED MADV_WILLNEED |
178 | #define QEMU_MADV_DONTNEED MADV_DONTNEED | |
179 | #ifdef MADV_DONTFORK | |
180 | #define QEMU_MADV_DONTFORK MADV_DONTFORK | |
181 | #else | |
182 | #define QEMU_MADV_DONTFORK QEMU_MADV_INVALID | |
183 | #endif | |
184 | #ifdef MADV_MERGEABLE | |
185 | #define QEMU_MADV_MERGEABLE MADV_MERGEABLE | |
186 | #else | |
187 | #define QEMU_MADV_MERGEABLE QEMU_MADV_INVALID | |
188 | #endif | |
605d0a94 PB |
189 | #ifdef MADV_UNMERGEABLE |
190 | #define QEMU_MADV_UNMERGEABLE MADV_UNMERGEABLE | |
191 | #else | |
192 | #define QEMU_MADV_UNMERGEABLE QEMU_MADV_INVALID | |
193 | #endif | |
194 | #ifdef MADV_DODUMP | |
195 | #define QEMU_MADV_DODUMP MADV_DODUMP | |
196 | #else | |
197 | #define QEMU_MADV_DODUMP QEMU_MADV_INVALID | |
198 | #endif | |
ddb97f1d JB |
199 | #ifdef MADV_DONTDUMP |
200 | #define QEMU_MADV_DONTDUMP MADV_DONTDUMP | |
201 | #else | |
202 | #define QEMU_MADV_DONTDUMP QEMU_MADV_INVALID | |
203 | #endif | |
ad0b5321 LC |
204 | #ifdef MADV_HUGEPAGE |
205 | #define QEMU_MADV_HUGEPAGE MADV_HUGEPAGE | |
206 | #else | |
207 | #define QEMU_MADV_HUGEPAGE QEMU_MADV_INVALID | |
208 | #endif | |
ebf81150 DDAG |
209 | #ifdef MADV_NOHUGEPAGE |
210 | #define QEMU_MADV_NOHUGEPAGE MADV_NOHUGEPAGE | |
211 | #else | |
212 | #define QEMU_MADV_NOHUGEPAGE QEMU_MADV_INVALID | |
213 | #endif | |
e78815a5 AF |
214 | |
215 | #elif defined(CONFIG_POSIX_MADVISE) | |
216 | ||
217 | #define QEMU_MADV_WILLNEED POSIX_MADV_WILLNEED | |
218 | #define QEMU_MADV_DONTNEED POSIX_MADV_DONTNEED | |
219 | #define QEMU_MADV_DONTFORK QEMU_MADV_INVALID | |
220 | #define QEMU_MADV_MERGEABLE QEMU_MADV_INVALID | |
2925020d MT |
221 | #define QEMU_MADV_UNMERGEABLE QEMU_MADV_INVALID |
222 | #define QEMU_MADV_DODUMP QEMU_MADV_INVALID | |
ddb97f1d | 223 | #define QEMU_MADV_DONTDUMP QEMU_MADV_INVALID |
8473f377 | 224 | #define QEMU_MADV_HUGEPAGE QEMU_MADV_INVALID |
ebf81150 | 225 | #define QEMU_MADV_NOHUGEPAGE QEMU_MADV_INVALID |
e78815a5 AF |
226 | |
227 | #else /* no-op */ | |
228 | ||
229 | #define QEMU_MADV_WILLNEED QEMU_MADV_INVALID | |
230 | #define QEMU_MADV_DONTNEED QEMU_MADV_INVALID | |
231 | #define QEMU_MADV_DONTFORK QEMU_MADV_INVALID | |
232 | #define QEMU_MADV_MERGEABLE QEMU_MADV_INVALID | |
2925020d MT |
233 | #define QEMU_MADV_UNMERGEABLE QEMU_MADV_INVALID |
234 | #define QEMU_MADV_DODUMP QEMU_MADV_INVALID | |
ddb97f1d | 235 | #define QEMU_MADV_DONTDUMP QEMU_MADV_INVALID |
8473f377 | 236 | #define QEMU_MADV_HUGEPAGE QEMU_MADV_INVALID |
ebf81150 | 237 | #define QEMU_MADV_NOHUGEPAGE QEMU_MADV_INVALID |
e78815a5 AF |
238 | |
239 | #endif | |
240 | ||
241 | int qemu_madvise(void *addr, size_t len, int advice); | |
242 | ||
17e0b6ab AF |
243 | int qemu_open(const char *name, int flags, ...); |
244 | int qemu_close(int fd); | |
245 | ||
953ffe0f AF |
246 | #if defined(__HAIKU__) && defined(__i386__) |
247 | #define FMT_pid "%ld" | |
0e0167ba SW |
248 | #elif defined(WIN64) |
249 | #define FMT_pid "%" PRId64 | |
953ffe0f AF |
250 | #else |
251 | #define FMT_pid "%d" | |
252 | #endif | |
253 | ||
aa26bb2d | 254 | int qemu_create_pidfile(const char *filename); |
dc7a09cf | 255 | int qemu_get_thread_id(void); |
aa26bb2d | 256 | |
9adea5f7 PB |
257 | #ifndef CONFIG_IOVEC |
258 | struct iovec { | |
259 | void *iov_base; | |
260 | size_t iov_len; | |
261 | }; | |
262 | /* | |
263 | * Use the same value as Linux for now. | |
264 | */ | |
265 | #define IOV_MAX 1024 | |
266 | ||
267 | ssize_t readv(int fd, const struct iovec *iov, int iov_cnt); | |
268 | ssize_t writev(int fd, const struct iovec *iov, int iov_cnt); | |
269 | #else | |
270 | #include <sys/uio.h> | |
271 | #endif | |
272 | ||
ad620c29 BS |
273 | #ifdef _WIN32 |
274 | static inline void qemu_timersub(const struct timeval *val1, | |
275 | const struct timeval *val2, | |
276 | struct timeval *res) | |
277 | { | |
278 | res->tv_sec = val1->tv_sec - val2->tv_sec; | |
279 | if (val1->tv_usec < val2->tv_usec) { | |
280 | res->tv_sec--; | |
281 | res->tv_usec = val1->tv_usec - val2->tv_usec + 1000 * 1000; | |
282 | } else { | |
283 | res->tv_usec = val1->tv_usec - val2->tv_usec; | |
284 | } | |
285 | } | |
286 | #else | |
287 | #define qemu_timersub timersub | |
288 | #endif | |
289 | ||
49ee3590 AL |
290 | void qemu_set_cloexec(int fd); |
291 | ||
fac862ff EH |
292 | /* QEMU "hardware version" setting. Used to replace code that exposed |
293 | * QEMU_VERSION to guests in the past and need to keep compatibilty. | |
294 | * Do not use qemu_hw_version() in new code. | |
295 | */ | |
35c2c8dc EH |
296 | void qemu_set_hw_version(const char *); |
297 | const char *qemu_hw_version(void); | |
93bfef4c | 298 | |
0f66998f PM |
299 | void fips_set_state(bool requested); |
300 | bool fips_get_state(void); | |
301 | ||
e2ea3515 LE |
302 | /* Return a dynamically allocated pathname denoting a file or directory that is |
303 | * appropriate for storing local state. | |
304 | * | |
305 | * @relative_pathname need not start with a directory separator; one will be | |
306 | * added automatically. | |
307 | * | |
308 | * The caller is responsible for releasing the value returned with g_free() | |
309 | * after use. | |
310 | */ | |
311 | char *qemu_get_local_state_pathname(const char *relative_pathname); | |
312 | ||
10f5bff6 FZ |
313 | /* Find program directory, and save it for later usage with |
314 | * qemu_get_exec_dir(). | |
315 | * Try OS specific API first, if not working, parse from argv0. */ | |
316 | void qemu_init_exec_dir(const char *argv0); | |
317 | ||
318 | /* Get the saved exec dir. | |
319 | * Caller needs to release the returned string by g_free() */ | |
320 | char *qemu_get_exec_dir(void); | |
321 | ||
b6a3e690 RH |
322 | /** |
323 | * qemu_getauxval: | |
324 | * @type: the auxiliary vector key to lookup | |
325 | * | |
326 | * Search the auxiliary vector for @type, returning the value | |
327 | * or 0 if @type is not present. | |
328 | */ | |
b6a3e690 | 329 | unsigned long qemu_getauxval(unsigned long type); |
b6a3e690 | 330 | |
13401ba0 SH |
331 | void qemu_set_tty_echo(int fd, bool echo); |
332 | ||
38183310 PB |
333 | void os_mem_prealloc(int fd, char *area, size_t sz); |
334 | ||
d57e4e48 DB |
335 | int qemu_read_password(char *buf, int buf_size); |
336 | ||
57cb38b3 DB |
337 | /** |
338 | * qemu_fork: | |
339 | * | |
340 | * A version of fork that avoids signal handler race | |
341 | * conditions that can lead to child process getting | |
342 | * signals that are otherwise only expected by the | |
343 | * parent. It also resets all signal handlers to the | |
344 | * default settings. | |
345 | * | |
346 | * Returns 0 to child process, pid number to parent | |
347 | * or -1 on failure. | |
348 | */ | |
349 | pid_t qemu_fork(Error **errp); | |
350 | ||
ea88812f | 351 | #endif |