1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27#ifndef QEMU_OSDEP_H
28#define QEMU_OSDEP_H
29
30#include "config-host.h"
31#ifdef NEED_CPU_H
32#include "config-target.h"
33#endif
34#include "qemu/compiler.h"
35
36
37
38
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
50
51
52
53#ifdef __APPLE__
54#define daemon qemu_fake_daemon_function
55#include <stdlib.h>
56#undef daemon
57extern int daemon(int, int);
58#endif
59
60#include <stdarg.h>
61#include <stddef.h>
62#include <stdbool.h>
63#include <stdint.h>
64#include <sys/types.h>
65#include <stdlib.h>
66#include <stdio.h>
67#include <string.h>
68#include <strings.h>
69#include <inttypes.h>
70#include <limits.h>
71
72
73#include <unistd.h>
74#include <time.h>
75#include <ctype.h>
76#include <errno.h>
77#include <fcntl.h>
78#include <sys/stat.h>
79#include <sys/time.h>
80#include <assert.h>
81
82
83#include <setjmp.h>
84#include <signal.h>
85
86#ifdef __OpenBSD__
87#include <sys/signal.h>
88#endif
89
90#ifndef _WIN32
91#include <sys/wait.h>
92#else
93#define WIFEXITED(x) 1
94#define WEXITSTATUS(x) (x)
95#endif
96
97#ifdef _WIN32
98#include "sysemu/os-win32.h"
99#endif
100
101#ifdef CONFIG_POSIX
102#include "sysemu/os-posix.h"
103#endif
104
105#include "glib-compat.h"
106#include "qemu/typedefs.h"
107
108#ifndef O_LARGEFILE
109#define O_LARGEFILE 0
110#endif
111#ifndef O_BINARY
112#define O_BINARY 0
113#endif
114#ifndef MAP_ANONYMOUS
115#define MAP_ANONYMOUS MAP_ANON
116#endif
117#ifndef ENOMEDIUM
118#define ENOMEDIUM ENODEV
119#endif
120#if !defined(ENOTSUP)
121#define ENOTSUP 4096
122#endif
123#if !defined(ECANCELED)
124#define ECANCELED 4097
125#endif
126#if !defined(EMEDIUMTYPE)
127#define EMEDIUMTYPE 4098
128#endif
129#ifndef TIME_MAX
130#define TIME_MAX LONG_MAX
131#endif
132
133
134#if UINTPTR_MAX == UINT32_MAX
135# define HOST_LONG_BITS 32
136#elif UINTPTR_MAX == UINT64_MAX
137# define HOST_LONG_BITS 64
138#else
139# error Unknown pointer size
140#endif
141
142#ifndef MIN
143#define MIN(a, b) (((a) < (b)) ? (a) : (b))
144#endif
145#ifndef MAX
146#define MAX(a, b) (((a) > (b)) ? (a) : (b))
147#endif
148
149
150
151#ifndef MIN_NON_ZERO
152#define MIN_NON_ZERO(a, b) ((a) == 0 ? (b) : \
153 ((b) == 0 ? (a) : (MIN(a, b))))
154#endif
155
156
157#define QEMU_ALIGN_DOWN(n, m) ((n) / (m) * (m))
158
159
160#define QEMU_ALIGN_UP(n, m) QEMU_ALIGN_DOWN((n) + (m) - 1, (m))
161
162#ifndef ROUND_UP
163#define ROUND_UP(n,d) (((n) + (d) - 1) & -(d))
164#endif
165
166#ifndef DIV_ROUND_UP
167#define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
168#endif
169
170#ifndef ARRAY_SIZE
171#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
172#endif
173
174int qemu_daemon(int nochdir, int noclose);
175void *qemu_try_memalign(size_t alignment, size_t size);
176void *qemu_memalign(size_t alignment, size_t size);
177void *qemu_anon_ram_alloc(size_t size, uint64_t *align);
178void qemu_vfree(void *ptr);
179void qemu_anon_ram_free(void *ptr, size_t size);
180
181#define QEMU_MADV_INVALID -1
182
183#if defined(CONFIG_MADVISE)
184
185#include <sys/mman.h>
186
187#define QEMU_MADV_WILLNEED MADV_WILLNEED
188#define QEMU_MADV_DONTNEED MADV_DONTNEED
189#ifdef MADV_DONTFORK
190#define QEMU_MADV_DONTFORK MADV_DONTFORK
191#else
192#define QEMU_MADV_DONTFORK QEMU_MADV_INVALID
193#endif
194#ifdef MADV_MERGEABLE
195#define QEMU_MADV_MERGEABLE MADV_MERGEABLE
196#else
197#define QEMU_MADV_MERGEABLE QEMU_MADV_INVALID
198#endif
199#ifdef MADV_UNMERGEABLE
200#define QEMU_MADV_UNMERGEABLE MADV_UNMERGEABLE
201#else
202#define QEMU_MADV_UNMERGEABLE QEMU_MADV_INVALID
203#endif
204#ifdef MADV_DODUMP
205#define QEMU_MADV_DODUMP MADV_DODUMP
206#else
207#define QEMU_MADV_DODUMP QEMU_MADV_INVALID
208#endif
209#ifdef MADV_DONTDUMP
210#define QEMU_MADV_DONTDUMP MADV_DONTDUMP
211#else
212#define QEMU_MADV_DONTDUMP QEMU_MADV_INVALID
213#endif
214#ifdef MADV_HUGEPAGE
215#define QEMU_MADV_HUGEPAGE MADV_HUGEPAGE
216#else
217#define QEMU_MADV_HUGEPAGE QEMU_MADV_INVALID
218#endif
219#ifdef MADV_NOHUGEPAGE
220#define QEMU_MADV_NOHUGEPAGE MADV_NOHUGEPAGE
221#else
222#define QEMU_MADV_NOHUGEPAGE QEMU_MADV_INVALID
223#endif
224
225#elif defined(CONFIG_POSIX_MADVISE)
226
227#define QEMU_MADV_WILLNEED POSIX_MADV_WILLNEED
228#define QEMU_MADV_DONTNEED POSIX_MADV_DONTNEED
229#define QEMU_MADV_DONTFORK QEMU_MADV_INVALID
230#define QEMU_MADV_MERGEABLE QEMU_MADV_INVALID
231#define QEMU_MADV_UNMERGEABLE QEMU_MADV_INVALID
232#define QEMU_MADV_DODUMP QEMU_MADV_INVALID
233#define QEMU_MADV_DONTDUMP QEMU_MADV_INVALID
234#define QEMU_MADV_HUGEPAGE QEMU_MADV_INVALID
235#define QEMU_MADV_NOHUGEPAGE QEMU_MADV_INVALID
236
237#else
238
239#define QEMU_MADV_WILLNEED QEMU_MADV_INVALID
240#define QEMU_MADV_DONTNEED QEMU_MADV_INVALID
241#define QEMU_MADV_DONTFORK QEMU_MADV_INVALID
242#define QEMU_MADV_MERGEABLE QEMU_MADV_INVALID
243#define QEMU_MADV_UNMERGEABLE QEMU_MADV_INVALID
244#define QEMU_MADV_DODUMP QEMU_MADV_INVALID
245#define QEMU_MADV_DONTDUMP QEMU_MADV_INVALID
246#define QEMU_MADV_HUGEPAGE QEMU_MADV_INVALID
247#define QEMU_MADV_NOHUGEPAGE QEMU_MADV_INVALID
248
249#endif
250
251#if defined(__linux__) && \
252 (defined(__x86_64__) || defined(__arm__) || defined(__aarch64__))
253
254
255
256# define QEMU_VMALLOC_ALIGN (512 * 4096)
257#elif defined(__linux__) && defined(__s390x__)
258
259# define QEMU_VMALLOC_ALIGN (256 * 4096)
260#else
261# define QEMU_VMALLOC_ALIGN getpagesize()
262#endif
263
264int qemu_madvise(void *addr, size_t len, int advice);
265
266int qemu_open(const char *name, int flags, ...);
267int qemu_close(int fd);
268
269#if defined(__HAIKU__) && defined(__i386__)
270#define FMT_pid "%ld"
271#elif defined(WIN64)
272#define FMT_pid "%" PRId64
273#else
274#define FMT_pid "%d"
275#endif
276
277int qemu_create_pidfile(const char *filename);
278int qemu_get_thread_id(void);
279
280#ifndef CONFIG_IOVEC
281struct iovec {
282 void *iov_base;
283 size_t iov_len;
284};
285
286
287
288#define IOV_MAX 1024
289
290ssize_t readv(int fd, const struct iovec *iov, int iov_cnt);
291ssize_t writev(int fd, const struct iovec *iov, int iov_cnt);
292#else
293#include <sys/uio.h>
294#endif
295
296#ifdef _WIN32
297static inline void qemu_timersub(const struct timeval *val1,
298 const struct timeval *val2,
299 struct timeval *res)
300{
301 res->tv_sec = val1->tv_sec - val2->tv_sec;
302 if (val1->tv_usec < val2->tv_usec) {
303 res->tv_sec--;
304 res->tv_usec = val1->tv_usec - val2->tv_usec + 1000 * 1000;
305 } else {
306 res->tv_usec = val1->tv_usec - val2->tv_usec;
307 }
308}
309#else
310#define qemu_timersub timersub
311#endif
312
313void qemu_set_cloexec(int fd);
314
315
316
317
318
319void qemu_set_hw_version(const char *);
320const char *qemu_hw_version(void);
321
322void fips_set_state(bool requested);
323bool fips_get_state(void);
324
325
326
327
328
329
330
331
332
333
334char *qemu_get_local_state_pathname(const char *relative_pathname);
335
336
337
338
339void qemu_init_exec_dir(const char *argv0);
340
341
342
343char *qemu_get_exec_dir(void);
344
345
346
347
348
349
350
351
352unsigned long qemu_getauxval(unsigned long type);
353
354void qemu_set_tty_echo(int fd, bool echo);
355
356void os_mem_prealloc(int fd, char *area, size_t sz);
357
358int qemu_read_password(char *buf, int buf_size);
359
360
361
362
363
364
365
366
367
368
369
370
371
372pid_t qemu_fork(Error **errp);
373
374#endif
375