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