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