xref: /qemu/include/qemu/osdep.h (revision 814bb12a)
1 /*
2  * OS includes and handling of OS dependencies
3  *
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).
7  *
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, config-target.h, qemu/compiler.h,
11  * sysemu/os-posix.h, sysemu/os-win32.h, glib-compat.h and
12  * qemu/typedefs.h, all of which are doing a similar job to this file
13  * and are under similar constraints.
14  *
15  * This header also contains prototypes for functions defined in
16  * os-*.c and util/oslib-*.c; those would probably be better split
17  * out into separate header files.
18  *
19  * In an ideal world this header would contain only:
20  *  (1) things which everybody needs
21  *  (2) things without which code would work on most platforms but
22  *      fail to compile or misbehave on a minority of host OSes
23  *
24  * This work is licensed under the terms of the GNU GPL, version 2 or later.
25  * See the COPYING file in the top-level directory.
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 #include "qemu/compiler.h"
37 
38 /* Older versions of C++ don't get definitions of various macros from
39  * stdlib.h unless we define these macros before first inclusion of
40  * that system header.
41  */
42 #ifndef __STDC_CONSTANT_MACROS
43 #define __STDC_CONSTANT_MACROS
44 #endif
45 #ifndef __STDC_LIMIT_MACROS
46 #define __STDC_LIMIT_MACROS
47 #endif
48 #ifndef __STDC_FORMAT_MACROS
49 #define __STDC_FORMAT_MACROS
50 #endif
51 
52 /* The following block of code temporarily renames the daemon() function so the
53  * compiler does not see the warning associated with it in stdlib.h on OSX
54  */
55 #ifdef __APPLE__
56 #define daemon qemu_fake_daemon_function
57 #include <stdlib.h>
58 #undef daemon
59 extern int daemon(int, int);
60 #endif
61 
62 #include <stdarg.h>
63 #include <stddef.h>
64 #include <stdbool.h>
65 #include <stdint.h>
66 #include <sys/types.h>
67 #include <stdlib.h>
68 #include <stdio.h>
69 #include <string.h>
70 #include <strings.h>
71 #include <inttypes.h>
72 #include <limits.h>
73 /* Put unistd.h before time.h as that triggers localtime_r/gmtime_r
74  * function availability on recentish Mingw-w64 platforms. */
75 #include <unistd.h>
76 #include <time.h>
77 #include <ctype.h>
78 #include <errno.h>
79 #include <fcntl.h>
80 #include <sys/stat.h>
81 #include <sys/time.h>
82 #include <assert.h>
83 /* setjmp must be declared before sysemu/os-win32.h
84  * because it is redefined there. */
85 #include <setjmp.h>
86 #include <signal.h>
87 
88 #ifdef __OpenBSD__
89 #include <sys/signal.h>
90 #endif
91 
92 #ifndef _WIN32
93 #include <sys/wait.h>
94 #else
95 #define WIFEXITED(x)   1
96 #define WEXITSTATUS(x) (x)
97 #endif
98 
99 #ifdef _WIN32
100 #include "sysemu/os-win32.h"
101 #endif
102 
103 #ifdef CONFIG_POSIX
104 #include "sysemu/os-posix.h"
105 #endif
106 
107 #include "glib-compat.h"
108 #include "qemu/typedefs.h"
109 
110 #ifndef O_LARGEFILE
111 #define O_LARGEFILE 0
112 #endif
113 #ifndef O_BINARY
114 #define O_BINARY 0
115 #endif
116 #ifndef MAP_ANONYMOUS
117 #define MAP_ANONYMOUS MAP_ANON
118 #endif
119 #ifndef ENOMEDIUM
120 #define ENOMEDIUM ENODEV
121 #endif
122 #if !defined(ENOTSUP)
123 #define ENOTSUP 4096
124 #endif
125 #if !defined(ECANCELED)
126 #define ECANCELED 4097
127 #endif
128 #if !defined(EMEDIUMTYPE)
129 #define EMEDIUMTYPE 4098
130 #endif
131 #ifndef TIME_MAX
132 #define TIME_MAX LONG_MAX
133 #endif
134 
135 /* HOST_LONG_BITS is the size of a native pointer in bits. */
136 #if UINTPTR_MAX == UINT32_MAX
137 # define HOST_LONG_BITS 32
138 #elif UINTPTR_MAX == UINT64_MAX
139 # define HOST_LONG_BITS 64
140 #else
141 # error Unknown pointer size
142 #endif
143 
144 /* Mac OSX has a <stdint.h> bug that incorrectly defines SIZE_MAX with
145  * the wrong type. Our replacement isn't usable in preprocessor
146  * expressions, but it is sufficient for our needs. */
147 #if defined(HAVE_BROKEN_SIZE_MAX) && HAVE_BROKEN_SIZE_MAX
148 #undef SIZE_MAX
149 #define SIZE_MAX ((size_t)-1)
150 #endif
151 
152 #ifndef MIN
153 #define MIN(a, b) (((a) < (b)) ? (a) : (b))
154 #endif
155 #ifndef MAX
156 #define MAX(a, b) (((a) > (b)) ? (a) : (b))
157 #endif
158 
159 /* Minimum function that returns zero only iff both values are zero.
160  * Intended for use with unsigned values only. */
161 #ifndef MIN_NON_ZERO
162 #define MIN_NON_ZERO(a, b) ((a) == 0 ? (b) : \
163                                 ((b) == 0 ? (a) : (MIN(a, b))))
164 #endif
165 
166 /* Round number down to multiple */
167 #define QEMU_ALIGN_DOWN(n, m) ((n) / (m) * (m))
168 
169 /* Round number up to multiple. Safe when m is not a power of 2 (see
170  * ROUND_UP for a faster version when a power of 2 is guaranteed) */
171 #define QEMU_ALIGN_UP(n, m) QEMU_ALIGN_DOWN((n) + (m) - 1, (m))
172 
173 /* Check if n is a multiple of m */
174 #define QEMU_IS_ALIGNED(n, m) (((n) % (m)) == 0)
175 
176 /* n-byte align pointer down */
177 #define QEMU_ALIGN_PTR_DOWN(p, n) \
178     ((typeof(p))QEMU_ALIGN_DOWN((uintptr_t)(p), (n)))
179 
180 /* n-byte align pointer up */
181 #define QEMU_ALIGN_PTR_UP(p, n) \
182     ((typeof(p))QEMU_ALIGN_UP((uintptr_t)(p), (n)))
183 
184 /* Check if pointer p is n-bytes aligned */
185 #define QEMU_PTR_IS_ALIGNED(p, n) QEMU_IS_ALIGNED((uintptr_t)(p), (n))
186 
187 /* Round number up to multiple. Requires that d be a power of 2 (see
188  * QEMU_ALIGN_UP for a safer but slower version on arbitrary
189  * numbers) */
190 #ifndef ROUND_UP
191 #define ROUND_UP(n,d) (((n) + (d) - 1) & -(d))
192 #endif
193 
194 #ifndef DIV_ROUND_UP
195 #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
196 #endif
197 
198 #ifndef ARRAY_SIZE
199 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
200 #endif
201 
202 int qemu_daemon(int nochdir, int noclose);
203 void *qemu_try_memalign(size_t alignment, size_t size);
204 void *qemu_memalign(size_t alignment, size_t size);
205 void *qemu_anon_ram_alloc(size_t size, uint64_t *align);
206 void qemu_vfree(void *ptr);
207 void qemu_anon_ram_free(void *ptr, size_t size);
208 
209 #define QEMU_MADV_INVALID -1
210 
211 #if defined(CONFIG_MADVISE)
212 
213 #define QEMU_MADV_WILLNEED  MADV_WILLNEED
214 #define QEMU_MADV_DONTNEED  MADV_DONTNEED
215 #ifdef MADV_DONTFORK
216 #define QEMU_MADV_DONTFORK  MADV_DONTFORK
217 #else
218 #define QEMU_MADV_DONTFORK  QEMU_MADV_INVALID
219 #endif
220 #ifdef MADV_MERGEABLE
221 #define QEMU_MADV_MERGEABLE MADV_MERGEABLE
222 #else
223 #define QEMU_MADV_MERGEABLE QEMU_MADV_INVALID
224 #endif
225 #ifdef MADV_UNMERGEABLE
226 #define QEMU_MADV_UNMERGEABLE MADV_UNMERGEABLE
227 #else
228 #define QEMU_MADV_UNMERGEABLE QEMU_MADV_INVALID
229 #endif
230 #ifdef MADV_DODUMP
231 #define QEMU_MADV_DODUMP MADV_DODUMP
232 #else
233 #define QEMU_MADV_DODUMP QEMU_MADV_INVALID
234 #endif
235 #ifdef MADV_DONTDUMP
236 #define QEMU_MADV_DONTDUMP MADV_DONTDUMP
237 #else
238 #define QEMU_MADV_DONTDUMP QEMU_MADV_INVALID
239 #endif
240 #ifdef MADV_HUGEPAGE
241 #define QEMU_MADV_HUGEPAGE MADV_HUGEPAGE
242 #else
243 #define QEMU_MADV_HUGEPAGE QEMU_MADV_INVALID
244 #endif
245 #ifdef MADV_NOHUGEPAGE
246 #define QEMU_MADV_NOHUGEPAGE MADV_NOHUGEPAGE
247 #else
248 #define QEMU_MADV_NOHUGEPAGE QEMU_MADV_INVALID
249 #endif
250 
251 #elif defined(CONFIG_POSIX_MADVISE)
252 
253 #define QEMU_MADV_WILLNEED  POSIX_MADV_WILLNEED
254 #define QEMU_MADV_DONTNEED  POSIX_MADV_DONTNEED
255 #define QEMU_MADV_DONTFORK  QEMU_MADV_INVALID
256 #define QEMU_MADV_MERGEABLE QEMU_MADV_INVALID
257 #define QEMU_MADV_UNMERGEABLE QEMU_MADV_INVALID
258 #define QEMU_MADV_DODUMP QEMU_MADV_INVALID
259 #define QEMU_MADV_DONTDUMP QEMU_MADV_INVALID
260 #define QEMU_MADV_HUGEPAGE  QEMU_MADV_INVALID
261 #define QEMU_MADV_NOHUGEPAGE  QEMU_MADV_INVALID
262 
263 #else /* no-op */
264 
265 #define QEMU_MADV_WILLNEED  QEMU_MADV_INVALID
266 #define QEMU_MADV_DONTNEED  QEMU_MADV_INVALID
267 #define QEMU_MADV_DONTFORK  QEMU_MADV_INVALID
268 #define QEMU_MADV_MERGEABLE QEMU_MADV_INVALID
269 #define QEMU_MADV_UNMERGEABLE QEMU_MADV_INVALID
270 #define QEMU_MADV_DODUMP QEMU_MADV_INVALID
271 #define QEMU_MADV_DONTDUMP QEMU_MADV_INVALID
272 #define QEMU_MADV_HUGEPAGE  QEMU_MADV_INVALID
273 #define QEMU_MADV_NOHUGEPAGE  QEMU_MADV_INVALID
274 
275 #endif
276 
277 #if defined(__linux__) && \
278     (defined(__x86_64__) || defined(__arm__) || defined(__aarch64__))
279    /* Use 2 MiB alignment so transparent hugepages can be used by KVM.
280       Valgrind does not support alignments larger than 1 MiB,
281       therefore we need special code which handles running on Valgrind. */
282 #  define QEMU_VMALLOC_ALIGN (512 * 4096)
283 #elif defined(__linux__) && defined(__s390x__)
284    /* Use 1 MiB (segment size) alignment so gmap can be used by KVM. */
285 #  define QEMU_VMALLOC_ALIGN (256 * 4096)
286 #else
287 #  define QEMU_VMALLOC_ALIGN getpagesize()
288 #endif
289 
290 int qemu_madvise(void *addr, size_t len, int advice);
291 
292 int qemu_open(const char *name, int flags, ...);
293 int qemu_close(int fd);
294 #ifndef _WIN32
295 int qemu_dup(int fd);
296 #endif
297 
298 #if defined(__HAIKU__) && defined(__i386__)
299 #define FMT_pid "%ld"
300 #elif defined(WIN64)
301 #define FMT_pid "%" PRId64
302 #else
303 #define FMT_pid "%d"
304 #endif
305 
306 int qemu_create_pidfile(const char *filename);
307 int qemu_get_thread_id(void);
308 
309 #ifndef CONFIG_IOVEC
310 struct iovec {
311     void *iov_base;
312     size_t iov_len;
313 };
314 /*
315  * Use the same value as Linux for now.
316  */
317 #define IOV_MAX 1024
318 
319 ssize_t readv(int fd, const struct iovec *iov, int iov_cnt);
320 ssize_t writev(int fd, const struct iovec *iov, int iov_cnt);
321 #else
322 #include <sys/uio.h>
323 #endif
324 
325 #ifdef _WIN32
326 static inline void qemu_timersub(const struct timeval *val1,
327                                  const struct timeval *val2,
328                                  struct timeval *res)
329 {
330     res->tv_sec = val1->tv_sec - val2->tv_sec;
331     if (val1->tv_usec < val2->tv_usec) {
332         res->tv_sec--;
333         res->tv_usec = val1->tv_usec - val2->tv_usec + 1000 * 1000;
334     } else {
335         res->tv_usec = val1->tv_usec - val2->tv_usec;
336     }
337 }
338 #else
339 #define qemu_timersub timersub
340 #endif
341 
342 void qemu_set_cloexec(int fd);
343 
344 /* Starting on QEMU 2.5, qemu_hw_version() returns "2.5+" by default
345  * instead of QEMU_VERSION, so setting hw_version on MachineClass
346  * is no longer mandatory.
347  *
348  * Do NOT change this string, or it will break compatibility on all
349  * machine classes that don't set hw_version.
350  */
351 #define QEMU_HW_VERSION "2.5+"
352 
353 /* QEMU "hardware version" setting. Used to replace code that exposed
354  * QEMU_VERSION to guests in the past and need to keep compatibility.
355  * Do not use qemu_hw_version() in new code.
356  */
357 void qemu_set_hw_version(const char *);
358 const char *qemu_hw_version(void);
359 
360 void fips_set_state(bool requested);
361 bool fips_get_state(void);
362 
363 /* Return a dynamically allocated pathname denoting a file or directory that is
364  * appropriate for storing local state.
365  *
366  * @relative_pathname need not start with a directory separator; one will be
367  * added automatically.
368  *
369  * The caller is responsible for releasing the value returned with g_free()
370  * after use.
371  */
372 char *qemu_get_local_state_pathname(const char *relative_pathname);
373 
374 /* Find program directory, and save it for later usage with
375  * qemu_get_exec_dir().
376  * Try OS specific API first, if not working, parse from argv0. */
377 void qemu_init_exec_dir(const char *argv0);
378 
379 /* Get the saved exec dir.
380  * Caller needs to release the returned string by g_free() */
381 char *qemu_get_exec_dir(void);
382 
383 /**
384  * qemu_getauxval:
385  * @type: the auxiliary vector key to lookup
386  *
387  * Search the auxiliary vector for @type, returning the value
388  * or 0 if @type is not present.
389  */
390 unsigned long qemu_getauxval(unsigned long type);
391 
392 void qemu_set_tty_echo(int fd, bool echo);
393 
394 void os_mem_prealloc(int fd, char *area, size_t sz, Error **errp);
395 
396 int qemu_read_password(char *buf, int buf_size);
397 
398 /**
399  * qemu_get_pid_name:
400  * @pid: pid of a process
401  *
402  * For given @pid fetch its name. Caller is responsible for
403  * freeing the string when no longer needed.
404  * Returns allocated string on success, NULL on failure.
405  */
406 char *qemu_get_pid_name(pid_t pid);
407 
408 /**
409  * qemu_fork:
410  *
411  * A version of fork that avoids signal handler race
412  * conditions that can lead to child process getting
413  * signals that are otherwise only expected by the
414  * parent. It also resets all signal handlers to the
415  * default settings.
416  *
417  * Returns 0 to child process, pid number to parent
418  * or -1 on failure.
419  */
420 pid_t qemu_fork(Error **errp);
421 
422 #endif
423