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