arm: soc-dma: use hwaddr instead of target_ulong in printf
[qemu.git] / include / qemu / osdep.h
blob84e84ac700c2f8aadf180b9ab6bf00a571136eec
1 /*
2 * OS includes and handling of OS dependencies
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).
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.
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.
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
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.
25 #ifndef QEMU_OSDEP_H
26 #define QEMU_OSDEP_H
28 #include "config-host.h"
29 #include "qemu/compiler.h"
30 #include <stdarg.h>
31 #include <stddef.h>
32 #include <stdbool.h>
33 #include <stdint.h>
34 #include <sys/types.h>
35 #include <stdlib.h>
36 #include <stdio.h>
37 #include <string.h>
38 #include <strings.h>
39 #include <inttypes.h>
40 #include <limits.h>
41 /* Put unistd.h before time.h as that triggers localtime_r/gmtime_r
42 * function availability on recentish Mingw-w64 platforms. */
43 #include <unistd.h>
44 #include <time.h>
45 #include <ctype.h>
46 #include <errno.h>
47 #include <fcntl.h>
48 #include <sys/stat.h>
49 #include <sys/time.h>
50 #include <assert.h>
51 #include <signal.h>
53 #ifdef __OpenBSD__
54 #include <sys/signal.h>
55 #endif
57 #ifndef _WIN32
58 #include <sys/wait.h>
59 #else
60 #define WIFEXITED(x) 1
61 #define WEXITSTATUS(x) (x)
62 #endif
64 #ifdef _WIN32
65 #include "sysemu/os-win32.h"
66 #endif
68 #ifdef CONFIG_POSIX
69 #include "sysemu/os-posix.h"
70 #endif
72 #include "qapi/error.h"
74 #if defined(CONFIG_SOLARIS) && CONFIG_SOLARIS_VERSION < 10
75 /* [u]int_fast*_t not in <sys/int_types.h> */
76 typedef unsigned char uint_fast8_t;
77 typedef unsigned int uint_fast16_t;
78 typedef signed int int_fast16_t;
79 #endif
81 #ifndef O_LARGEFILE
82 #define O_LARGEFILE 0
83 #endif
84 #ifndef O_BINARY
85 #define O_BINARY 0
86 #endif
87 #ifndef MAP_ANONYMOUS
88 #define MAP_ANONYMOUS MAP_ANON
89 #endif
90 #ifndef ENOMEDIUM
91 #define ENOMEDIUM ENODEV
92 #endif
93 #if !defined(ENOTSUP)
94 #define ENOTSUP 4096
95 #endif
96 #if !defined(ECANCELED)
97 #define ECANCELED 4097
98 #endif
99 #if !defined(EMEDIUMTYPE)
100 #define EMEDIUMTYPE 4098
101 #endif
102 #ifndef TIME_MAX
103 #define TIME_MAX LONG_MAX
104 #endif
106 #ifndef MIN
107 #define MIN(a, b) (((a) < (b)) ? (a) : (b))
108 #endif
109 #ifndef MAX
110 #define MAX(a, b) (((a) > (b)) ? (a) : (b))
111 #endif
113 /* Minimum function that returns zero only iff both values are zero.
114 * Intended for use with unsigned values only. */
115 #ifndef MIN_NON_ZERO
116 #define MIN_NON_ZERO(a, b) (((a) != 0 && (a) < (b)) ? (a) : (b))
117 #endif
119 #ifndef ROUND_UP
120 #define ROUND_UP(n,d) (((n) + (d) - 1) & -(d))
121 #endif
123 #ifndef DIV_ROUND_UP
124 #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
125 #endif
127 #ifndef ARRAY_SIZE
128 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
129 #endif
131 int qemu_daemon(int nochdir, int noclose);
132 void *qemu_try_memalign(size_t alignment, size_t size);
133 void *qemu_memalign(size_t alignment, size_t size);
134 void *qemu_anon_ram_alloc(size_t size, uint64_t *align);
135 void qemu_vfree(void *ptr);
136 void qemu_anon_ram_free(void *ptr, size_t size);
138 #define QEMU_MADV_INVALID -1
140 #if defined(CONFIG_MADVISE)
142 #include <sys/mman.h>
144 #define QEMU_MADV_WILLNEED MADV_WILLNEED
145 #define QEMU_MADV_DONTNEED MADV_DONTNEED
146 #ifdef MADV_DONTFORK
147 #define QEMU_MADV_DONTFORK MADV_DONTFORK
148 #else
149 #define QEMU_MADV_DONTFORK QEMU_MADV_INVALID
150 #endif
151 #ifdef MADV_MERGEABLE
152 #define QEMU_MADV_MERGEABLE MADV_MERGEABLE
153 #else
154 #define QEMU_MADV_MERGEABLE QEMU_MADV_INVALID
155 #endif
156 #ifdef MADV_UNMERGEABLE
157 #define QEMU_MADV_UNMERGEABLE MADV_UNMERGEABLE
158 #else
159 #define QEMU_MADV_UNMERGEABLE QEMU_MADV_INVALID
160 #endif
161 #ifdef MADV_DODUMP
162 #define QEMU_MADV_DODUMP MADV_DODUMP
163 #else
164 #define QEMU_MADV_DODUMP QEMU_MADV_INVALID
165 #endif
166 #ifdef MADV_DONTDUMP
167 #define QEMU_MADV_DONTDUMP MADV_DONTDUMP
168 #else
169 #define QEMU_MADV_DONTDUMP QEMU_MADV_INVALID
170 #endif
171 #ifdef MADV_HUGEPAGE
172 #define QEMU_MADV_HUGEPAGE MADV_HUGEPAGE
173 #else
174 #define QEMU_MADV_HUGEPAGE QEMU_MADV_INVALID
175 #endif
176 #ifdef MADV_NOHUGEPAGE
177 #define QEMU_MADV_NOHUGEPAGE MADV_NOHUGEPAGE
178 #else
179 #define QEMU_MADV_NOHUGEPAGE QEMU_MADV_INVALID
180 #endif
182 #elif defined(CONFIG_POSIX_MADVISE)
184 #define QEMU_MADV_WILLNEED POSIX_MADV_WILLNEED
185 #define QEMU_MADV_DONTNEED POSIX_MADV_DONTNEED
186 #define QEMU_MADV_DONTFORK QEMU_MADV_INVALID
187 #define QEMU_MADV_MERGEABLE QEMU_MADV_INVALID
188 #define QEMU_MADV_UNMERGEABLE QEMU_MADV_INVALID
189 #define QEMU_MADV_DODUMP QEMU_MADV_INVALID
190 #define QEMU_MADV_DONTDUMP QEMU_MADV_INVALID
191 #define QEMU_MADV_HUGEPAGE QEMU_MADV_INVALID
192 #define QEMU_MADV_NOHUGEPAGE QEMU_MADV_INVALID
194 #else /* no-op */
196 #define QEMU_MADV_WILLNEED QEMU_MADV_INVALID
197 #define QEMU_MADV_DONTNEED QEMU_MADV_INVALID
198 #define QEMU_MADV_DONTFORK QEMU_MADV_INVALID
199 #define QEMU_MADV_MERGEABLE QEMU_MADV_INVALID
200 #define QEMU_MADV_UNMERGEABLE QEMU_MADV_INVALID
201 #define QEMU_MADV_DODUMP QEMU_MADV_INVALID
202 #define QEMU_MADV_DONTDUMP QEMU_MADV_INVALID
203 #define QEMU_MADV_HUGEPAGE QEMU_MADV_INVALID
204 #define QEMU_MADV_NOHUGEPAGE QEMU_MADV_INVALID
206 #endif
208 int qemu_madvise(void *addr, size_t len, int advice);
210 int qemu_open(const char *name, int flags, ...);
211 int qemu_close(int fd);
213 #if defined(__HAIKU__) && defined(__i386__)
214 #define FMT_pid "%ld"
215 #elif defined(WIN64)
216 #define FMT_pid "%" PRId64
217 #else
218 #define FMT_pid "%d"
219 #endif
221 int qemu_create_pidfile(const char *filename);
222 int qemu_get_thread_id(void);
224 #ifndef CONFIG_IOVEC
225 struct iovec {
226 void *iov_base;
227 size_t iov_len;
230 * Use the same value as Linux for now.
232 #define IOV_MAX 1024
234 ssize_t readv(int fd, const struct iovec *iov, int iov_cnt);
235 ssize_t writev(int fd, const struct iovec *iov, int iov_cnt);
236 #else
237 #include <sys/uio.h>
238 #endif
240 #ifdef _WIN32
241 static inline void qemu_timersub(const struct timeval *val1,
242 const struct timeval *val2,
243 struct timeval *res)
245 res->tv_sec = val1->tv_sec - val2->tv_sec;
246 if (val1->tv_usec < val2->tv_usec) {
247 res->tv_sec--;
248 res->tv_usec = val1->tv_usec - val2->tv_usec + 1000 * 1000;
249 } else {
250 res->tv_usec = val1->tv_usec - val2->tv_usec;
253 #else
254 #define qemu_timersub timersub
255 #endif
257 void qemu_set_cloexec(int fd);
259 /* QEMU "hardware version" setting. Used to replace code that exposed
260 * QEMU_VERSION to guests in the past and need to keep compatibilty.
261 * Do not use qemu_hw_version() in new code.
263 void qemu_set_hw_version(const char *);
264 const char *qemu_hw_version(void);
266 void fips_set_state(bool requested);
267 bool fips_get_state(void);
269 /* Return a dynamically allocated pathname denoting a file or directory that is
270 * appropriate for storing local state.
272 * @relative_pathname need not start with a directory separator; one will be
273 * added automatically.
275 * The caller is responsible for releasing the value returned with g_free()
276 * after use.
278 char *qemu_get_local_state_pathname(const char *relative_pathname);
280 /* Find program directory, and save it for later usage with
281 * qemu_get_exec_dir().
282 * Try OS specific API first, if not working, parse from argv0. */
283 void qemu_init_exec_dir(const char *argv0);
285 /* Get the saved exec dir.
286 * Caller needs to release the returned string by g_free() */
287 char *qemu_get_exec_dir(void);
290 * qemu_getauxval:
291 * @type: the auxiliary vector key to lookup
293 * Search the auxiliary vector for @type, returning the value
294 * or 0 if @type is not present.
296 unsigned long qemu_getauxval(unsigned long type);
298 void qemu_set_tty_echo(int fd, bool echo);
300 void os_mem_prealloc(int fd, char *area, size_t sz);
302 int qemu_read_password(char *buf, int buf_size);
305 * qemu_fork:
307 * A version of fork that avoids signal handler race
308 * conditions that can lead to child process getting
309 * signals that are otherwise only expected by the
310 * parent. It also resets all signal handlers to the
311 * default settings.
313 * Returns 0 to child process, pid number to parent
314 * or -1 on failure.
316 pid_t qemu_fork(Error **errp);
318 #endif