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#else
34#include "exec/poison.h"
35#endif
36#ifdef __COVERITY__
37
38
39
40
41
42
43
44typedef float _Float32;
45typedef double _Float32x;
46typedef double _Float64;
47typedef __float80 _Float64x;
48typedef __float128 _Float128;
49#endif
50
51#include "qemu/compiler.h"
52
53
54
55
56
57#ifndef __STDC_CONSTANT_MACROS
58#define __STDC_CONSTANT_MACROS
59#endif
60#ifndef __STDC_LIMIT_MACROS
61#define __STDC_LIMIT_MACROS
62#endif
63#ifndef __STDC_FORMAT_MACROS
64#define __STDC_FORMAT_MACROS
65#endif
66
67
68
69
70#ifdef __APPLE__
71#define daemon qemu_fake_daemon_function
72#include <stdlib.h>
73#undef daemon
74extern int daemon(int, int);
75#endif
76
77#include <stdarg.h>
78#include <stddef.h>
79#include <stdbool.h>
80#include <stdint.h>
81#include <sys/types.h>
82#include <stdlib.h>
83#include <stdio.h>
84#include <string.h>
85#include <strings.h>
86#include <inttypes.h>
87#include <limits.h>
88
89
90#include <unistd.h>
91#include <time.h>
92#include <ctype.h>
93#include <errno.h>
94#include <fcntl.h>
95#include <sys/stat.h>
96#include <sys/time.h>
97#include <assert.h>
98
99
100#include <setjmp.h>
101#include <signal.h>
102
103#ifdef __OpenBSD__
104#include <sys/signal.h>
105#endif
106
107#ifndef _WIN32
108#include <sys/wait.h>
109#else
110#define WIFEXITED(x) 1
111#define WEXITSTATUS(x) (x)
112#endif
113
114#ifdef _WIN32
115#include "sysemu/os-win32.h"
116#endif
117
118#ifdef CONFIG_POSIX
119#include "sysemu/os-posix.h"
120#endif
121
122#include "glib-compat.h"
123#include "qemu/typedefs.h"
124
125
126
127
128
129
130
131
132#ifdef __MINGW32__
133#undef assert
134#define assert(x) g_assert(x)
135#endif
136
137
138
139
140
141
142
143
144#ifndef WCOREDUMP
145#define WCOREDUMP(status) 0
146#endif
147
148
149
150
151
152
153
154
155
156#ifdef NDEBUG
157#error building with NDEBUG is not supported
158#endif
159#ifdef G_DISABLE_ASSERT
160#error building with G_DISABLE_ASSERT is not supported
161#endif
162
163#ifndef O_LARGEFILE
164#define O_LARGEFILE 0
165#endif
166#ifndef O_BINARY
167#define O_BINARY 0
168#endif
169#ifndef MAP_ANONYMOUS
170#define MAP_ANONYMOUS MAP_ANON
171#endif
172#ifndef ENOMEDIUM
173#define ENOMEDIUM ENODEV
174#endif
175#if !defined(ENOTSUP)
176#define ENOTSUP 4096
177#endif
178#if !defined(ECANCELED)
179#define ECANCELED 4097
180#endif
181#if !defined(EMEDIUMTYPE)
182#define EMEDIUMTYPE 4098
183#endif
184#if !defined(ESHUTDOWN)
185#define ESHUTDOWN 4099
186#endif
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201#define TYPE_SIGNED(t) (!((t)0 < (t)-1))
202
203
204
205
206#define TYPE_WIDTH(t) (sizeof(t) * CHAR_BIT)
207
208
209#define TYPE_MAXIMUM(t) \
210 ((t) (!TYPE_SIGNED(t) \
211 ? (t)-1 \
212 : ((((t)1 << (TYPE_WIDTH(t) - 2)) - 1) * 2 + 1)))
213
214#ifndef TIME_MAX
215#define TIME_MAX TYPE_MAXIMUM(time_t)
216#endif
217
218
219#if UINTPTR_MAX == UINT32_MAX
220# define HOST_LONG_BITS 32
221#elif UINTPTR_MAX == UINT64_MAX
222# define HOST_LONG_BITS 64
223#else
224# error Unknown pointer size
225#endif
226
227
228
229
230#if defined(HAVE_BROKEN_SIZE_MAX) && HAVE_BROKEN_SIZE_MAX
231#undef SIZE_MAX
232#define SIZE_MAX ((size_t)-1)
233#endif
234
235#ifndef MIN
236#define MIN(a, b) (((a) < (b)) ? (a) : (b))
237#endif
238#ifndef MAX
239#define MAX(a, b) (((a) > (b)) ? (a) : (b))
240#endif
241
242
243
244#ifndef MIN_NON_ZERO
245#define MIN_NON_ZERO(a, b) ((a) == 0 ? (b) : \
246 ((b) == 0 ? (a) : (MIN(a, b))))
247#endif
248
249
250#define QEMU_ALIGN_DOWN(n, m) ((n) / (m) * (m))
251
252
253
254#define QEMU_ALIGN_UP(n, m) QEMU_ALIGN_DOWN((n) + (m) - 1, (m))
255
256
257#define QEMU_IS_ALIGNED(n, m) (((n) % (m)) == 0)
258
259
260#define QEMU_ALIGN_PTR_DOWN(p, n) \
261 ((typeof(p))QEMU_ALIGN_DOWN((uintptr_t)(p), (n)))
262
263
264#define QEMU_ALIGN_PTR_UP(p, n) \
265 ((typeof(p))QEMU_ALIGN_UP((uintptr_t)(p), (n)))
266
267
268#define QEMU_PTR_IS_ALIGNED(p, n) QEMU_IS_ALIGNED((uintptr_t)(p), (n))
269
270
271
272
273#ifndef ROUND_UP
274#define ROUND_UP(n, d) (((n) + (d) - 1) & -(0 ? (n) : (d)))
275#endif
276
277#ifndef DIV_ROUND_UP
278#define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
279#endif
280
281
282
283
284
285#define QEMU_IS_ARRAY(x) (!__builtin_types_compatible_p(typeof(x), \
286 typeof(&(x)[0])))
287#ifndef ARRAY_SIZE
288#define ARRAY_SIZE(x) ((sizeof(x) / sizeof((x)[0])) + \
289 QEMU_BUILD_BUG_ON_ZERO(!QEMU_IS_ARRAY(x)))
290#endif
291
292int qemu_daemon(int nochdir, int noclose);
293void *qemu_try_memalign(size_t alignment, size_t size);
294void *qemu_memalign(size_t alignment, size_t size);
295void *qemu_anon_ram_alloc(size_t size, uint64_t *align, bool shared);
296void qemu_vfree(void *ptr);
297void qemu_anon_ram_free(void *ptr, size_t size);
298
299#define QEMU_MADV_INVALID -1
300
301#if defined(CONFIG_MADVISE)
302
303#define QEMU_MADV_WILLNEED MADV_WILLNEED
304#define QEMU_MADV_DONTNEED MADV_DONTNEED
305#ifdef MADV_DONTFORK
306#define QEMU_MADV_DONTFORK MADV_DONTFORK
307#else
308#define QEMU_MADV_DONTFORK QEMU_MADV_INVALID
309#endif
310#ifdef MADV_MERGEABLE
311#define QEMU_MADV_MERGEABLE MADV_MERGEABLE
312#else
313#define QEMU_MADV_MERGEABLE QEMU_MADV_INVALID
314#endif
315#ifdef MADV_UNMERGEABLE
316#define QEMU_MADV_UNMERGEABLE MADV_UNMERGEABLE
317#else
318#define QEMU_MADV_UNMERGEABLE QEMU_MADV_INVALID
319#endif
320#ifdef MADV_DODUMP
321#define QEMU_MADV_DODUMP MADV_DODUMP
322#else
323#define QEMU_MADV_DODUMP QEMU_MADV_INVALID
324#endif
325#ifdef MADV_DONTDUMP
326#define QEMU_MADV_DONTDUMP MADV_DONTDUMP
327#else
328#define QEMU_MADV_DONTDUMP QEMU_MADV_INVALID
329#endif
330#ifdef MADV_HUGEPAGE
331#define QEMU_MADV_HUGEPAGE MADV_HUGEPAGE
332#else
333#define QEMU_MADV_HUGEPAGE QEMU_MADV_INVALID
334#endif
335#ifdef MADV_NOHUGEPAGE
336#define QEMU_MADV_NOHUGEPAGE MADV_NOHUGEPAGE
337#else
338#define QEMU_MADV_NOHUGEPAGE QEMU_MADV_INVALID
339#endif
340#ifdef MADV_REMOVE
341#define QEMU_MADV_REMOVE MADV_REMOVE
342#else
343#define QEMU_MADV_REMOVE QEMU_MADV_INVALID
344#endif
345
346#elif defined(CONFIG_POSIX_MADVISE)
347
348#define QEMU_MADV_WILLNEED POSIX_MADV_WILLNEED
349#define QEMU_MADV_DONTNEED POSIX_MADV_DONTNEED
350#define QEMU_MADV_DONTFORK QEMU_MADV_INVALID
351#define QEMU_MADV_MERGEABLE QEMU_MADV_INVALID
352#define QEMU_MADV_UNMERGEABLE QEMU_MADV_INVALID
353#define QEMU_MADV_DODUMP QEMU_MADV_INVALID
354#define QEMU_MADV_DONTDUMP QEMU_MADV_INVALID
355#define QEMU_MADV_HUGEPAGE QEMU_MADV_INVALID
356#define QEMU_MADV_NOHUGEPAGE QEMU_MADV_INVALID
357#define QEMU_MADV_REMOVE QEMU_MADV_INVALID
358
359#else
360
361#define QEMU_MADV_WILLNEED QEMU_MADV_INVALID
362#define QEMU_MADV_DONTNEED QEMU_MADV_INVALID
363#define QEMU_MADV_DONTFORK QEMU_MADV_INVALID
364#define QEMU_MADV_MERGEABLE QEMU_MADV_INVALID
365#define QEMU_MADV_UNMERGEABLE QEMU_MADV_INVALID
366#define QEMU_MADV_DODUMP QEMU_MADV_INVALID
367#define QEMU_MADV_DONTDUMP QEMU_MADV_INVALID
368#define QEMU_MADV_HUGEPAGE QEMU_MADV_INVALID
369#define QEMU_MADV_NOHUGEPAGE QEMU_MADV_INVALID
370#define QEMU_MADV_REMOVE QEMU_MADV_INVALID
371
372#endif
373
374#ifdef _WIN32
375#define HAVE_CHARDEV_SERIAL 1
376#elif defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
377 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \
378 || defined(__GLIBC__)
379#define HAVE_CHARDEV_SERIAL 1
380#endif
381
382#if defined(__linux__) || defined(__FreeBSD__) || \
383 defined(__FreeBSD_kernel__) || defined(__DragonFly__)
384#define HAVE_CHARDEV_PARPORT 1
385#endif
386
387#if defined(CONFIG_LINUX)
388#ifndef BUS_MCEERR_AR
389#define BUS_MCEERR_AR 4
390#endif
391#ifndef BUS_MCEERR_AO
392#define BUS_MCEERR_AO 5
393#endif
394#endif
395
396#if defined(__linux__) && \
397 (defined(__x86_64__) || defined(__arm__) || defined(__aarch64__) \
398 || defined(__powerpc64__))
399
400
401
402# define QEMU_VMALLOC_ALIGN (512 * 4096)
403#elif defined(__linux__) && defined(__s390x__)
404
405# define QEMU_VMALLOC_ALIGN (256 * 4096)
406#elif defined(__linux__) && defined(__sparc__)
407#include <sys/shm.h>
408# define QEMU_VMALLOC_ALIGN MAX(getpagesize(), SHMLBA)
409#else
410# define QEMU_VMALLOC_ALIGN getpagesize()
411#endif
412
413#ifdef CONFIG_POSIX
414struct qemu_signalfd_siginfo {
415 uint32_t ssi_signo;
416 int32_t ssi_errno;
417 int32_t ssi_code;
418 uint32_t ssi_pid;
419 uint32_t ssi_uid;
420 int32_t ssi_fd;
421 uint32_t ssi_tid;
422 uint32_t ssi_band;
423 uint32_t ssi_overrun;
424 uint32_t ssi_trapno;
425 int32_t ssi_status;
426 int32_t ssi_int;
427 uint64_t ssi_ptr;
428 uint64_t ssi_utime;
429 uint64_t ssi_stime;
430 uint64_t ssi_addr;
431
432 uint8_t pad[48];
433
434};
435
436int qemu_signalfd(const sigset_t *mask);
437void sigaction_invoke(struct sigaction *action,
438 struct qemu_signalfd_siginfo *info);
439#endif
440
441int qemu_madvise(void *addr, size_t len, int advice);
442int qemu_mprotect_rwx(void *addr, size_t size);
443int qemu_mprotect_none(void *addr, size_t size);
444
445int qemu_open(const char *name, int flags, ...);
446int qemu_close(int fd);
447#ifndef _WIN32
448int qemu_dup(int fd);
449#endif
450int qemu_lock_fd(int fd, int64_t start, int64_t len, bool exclusive);
451int qemu_unlock_fd(int fd, int64_t start, int64_t len);
452int qemu_lock_fd_test(int fd, int64_t start, int64_t len, bool exclusive);
453bool qemu_has_ofd_lock(void);
454
455#if defined(__HAIKU__) && defined(__i386__)
456#define FMT_pid "%ld"
457#elif defined(WIN64)
458#define FMT_pid "%" PRId64
459#else
460#define FMT_pid "%d"
461#endif
462
463bool qemu_write_pidfile(const char *pidfile, Error **errp);
464
465int qemu_get_thread_id(void);
466
467#ifndef CONFIG_IOVEC
468struct iovec {
469 void *iov_base;
470 size_t iov_len;
471};
472
473
474
475#define IOV_MAX 1024
476
477ssize_t readv(int fd, const struct iovec *iov, int iov_cnt);
478ssize_t writev(int fd, const struct iovec *iov, int iov_cnt);
479#else
480#include <sys/uio.h>
481#endif
482
483#ifdef _WIN32
484static inline void qemu_timersub(const struct timeval *val1,
485 const struct timeval *val2,
486 struct timeval *res)
487{
488 res->tv_sec = val1->tv_sec - val2->tv_sec;
489 if (val1->tv_usec < val2->tv_usec) {
490 res->tv_sec--;
491 res->tv_usec = val1->tv_usec - val2->tv_usec + 1000 * 1000;
492 } else {
493 res->tv_usec = val1->tv_usec - val2->tv_usec;
494 }
495}
496#else
497#define qemu_timersub timersub
498#endif
499
500void qemu_set_cloexec(int fd);
501
502
503
504
505
506
507
508
509#define QEMU_HW_VERSION "2.5+"
510
511
512
513
514
515void qemu_set_hw_version(const char *);
516const char *qemu_hw_version(void);
517
518void fips_set_state(bool requested);
519bool fips_get_state(void);
520
521
522
523
524
525
526
527
528
529
530char *qemu_get_local_state_pathname(const char *relative_pathname);
531
532
533
534
535void qemu_init_exec_dir(const char *argv0);
536
537
538
539char *qemu_get_exec_dir(void);
540
541
542
543
544
545
546
547
548unsigned long qemu_getauxval(unsigned long type);
549
550void qemu_set_tty_echo(int fd, bool echo);
551
552void os_mem_prealloc(int fd, char *area, size_t sz, int smp_cpus,
553 Error **errp);
554
555
556
557
558
559
560
561
562
563char *qemu_get_pid_name(pid_t pid);
564
565
566
567
568
569
570
571
572
573
574
575
576
577pid_t qemu_fork(Error **errp);
578
579
580
581
582extern uintptr_t qemu_real_host_page_size;
583extern intptr_t qemu_real_host_page_mask;
584
585extern int qemu_icache_linesize;
586extern int qemu_icache_linesize_log;
587extern int qemu_dcache_linesize;
588extern int qemu_dcache_linesize_log;
589
590#endif
591