2004-02-16 22:12:40 +00:00
|
|
|
#ifndef QEMU_OSDEP_H
|
|
|
|
#define QEMU_OSDEP_H
|
|
|
|
|
2013-04-21 10:01:06 +00:00
|
|
|
#include "config-host.h"
|
2004-02-16 22:12:40 +00:00
|
|
|
#include <stdarg.h>
|
2009-09-12 09:58:46 +00:00
|
|
|
#include <stddef.h>
|
2012-08-03 18:39:21 +00:00
|
|
|
#include <stdbool.h>
|
2014-10-31 16:38:37 +00:00
|
|
|
#include <stdint.h>
|
2008-08-15 18:33:42 +00:00
|
|
|
#include <sys/types.h>
|
2013-05-10 19:58:21 +00:00
|
|
|
#ifdef __OpenBSD__
|
2008-08-15 18:33:42 +00:00
|
|
|
#include <sys/signal.h>
|
|
|
|
#endif
|
2004-02-16 22:12:40 +00:00
|
|
|
|
2013-02-22 16:36:38 +00:00
|
|
|
#ifndef _WIN32
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#else
|
|
|
|
#define WIFEXITED(x) 1
|
|
|
|
#define WEXITSTATUS(x) (x)
|
|
|
|
#endif
|
|
|
|
|
2009-01-07 17:40:15 +00:00
|
|
|
#include <sys/time.h>
|
|
|
|
|
2012-04-25 22:15:54 +00:00
|
|
|
#if defined(CONFIG_SOLARIS) && CONFIG_SOLARIS_VERSION < 10
|
|
|
|
/* [u]int_fast*_t not in <sys/int_types.h> */
|
|
|
|
typedef unsigned char uint_fast8_t;
|
|
|
|
typedef unsigned int uint_fast16_t;
|
2012-04-25 22:15:56 +00:00
|
|
|
typedef signed int int_fast16_t;
|
2012-04-25 22:15:54 +00:00
|
|
|
#endif
|
|
|
|
|
2007-11-19 00:38:33 +00:00
|
|
|
#ifndef glue
|
|
|
|
#define xglue(x, y) x ## y
|
|
|
|
#define glue(x, y) xglue(x, y)
|
|
|
|
#define stringify(s) tostring(s)
|
|
|
|
#define tostring(s) #s
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef likely
|
|
|
|
#if __GNUC__ < 3
|
|
|
|
#define __builtin_expect(x, n) (x)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define likely(x) __builtin_expect(!!(x), 1)
|
|
|
|
#define unlikely(x) __builtin_expect(!!(x), 0)
|
|
|
|
#endif
|
|
|
|
|
2008-09-16 13:36:57 +00:00
|
|
|
#ifndef container_of
|
2008-08-21 20:11:11 +00:00
|
|
|
#define container_of(ptr, type, member) ({ \
|
2008-09-16 13:36:57 +00:00
|
|
|
const typeof(((type *) 0)->member) *__mptr = (ptr); \
|
|
|
|
(type *) ((char *) __mptr - offsetof(type, member));})
|
|
|
|
#endif
|
2008-08-21 20:11:11 +00:00
|
|
|
|
2009-11-25 18:49:03 +00:00
|
|
|
/* Convert from a base type to a parent type, with compile time checking. */
|
|
|
|
#ifdef __GNUC__
|
|
|
|
#define DO_UPCAST(type, field, dev) ( __extension__ ( { \
|
|
|
|
char __attribute__((unused)) offset_must_be_zero[ \
|
|
|
|
-offsetof(type, field)]; \
|
|
|
|
container_of(dev, type, field);}))
|
|
|
|
#else
|
|
|
|
#define DO_UPCAST(type, field, dev) container_of(dev, type, field)
|
|
|
|
#endif
|
|
|
|
|
2009-08-20 17:42:19 +00:00
|
|
|
#define typeof_field(type, field) typeof(((type *)0)->field)
|
|
|
|
#define type_check(t1,t2) ((t1*)0 - (t2*)0)
|
|
|
|
|
2007-11-19 00:38:33 +00:00
|
|
|
#ifndef MIN
|
|
|
|
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
|
|
|
|
#endif
|
|
|
|
#ifndef MAX
|
|
|
|
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
|
|
|
|
#endif
|
|
|
|
|
2014-10-27 09:18:43 +00:00
|
|
|
/* Minimum function that returns zero only iff both values are zero.
|
|
|
|
* Intended for use with unsigned values only. */
|
|
|
|
#ifndef MIN_NON_ZERO
|
|
|
|
#define MIN_NON_ZERO(a, b) (((a) != 0 && (a) < (b)) ? (a) : (b))
|
|
|
|
#endif
|
|
|
|
|
2013-03-29 01:08:15 +00:00
|
|
|
#ifndef ROUND_UP
|
|
|
|
#define ROUND_UP(n,d) (((n) + (d) - 1) & -(d))
|
|
|
|
#endif
|
|
|
|
|
2011-02-04 08:06:04 +00:00
|
|
|
#ifndef DIV_ROUND_UP
|
|
|
|
#define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
|
|
|
|
#endif
|
|
|
|
|
2008-03-11 21:01:02 +00:00
|
|
|
#ifndef ARRAY_SIZE
|
|
|
|
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
|
|
|
|
#endif
|
|
|
|
|
2007-11-19 00:38:33 +00:00
|
|
|
#ifndef always_inline
|
2009-08-16 09:06:54 +00:00
|
|
|
#if !((__GNUC__ < 3) || defined(__APPLE__))
|
2008-11-19 01:31:52 +00:00
|
|
|
#ifdef __OPTIMIZE__
|
2012-07-08 07:00:50 +00:00
|
|
|
#undef inline
|
2009-08-16 09:06:54 +00:00
|
|
|
#define inline __attribute__ (( always_inline )) __inline__
|
2007-11-19 00:38:33 +00:00
|
|
|
#endif
|
2008-11-19 01:31:52 +00:00
|
|
|
#endif
|
2008-06-05 22:55:54 +00:00
|
|
|
#else
|
2012-07-08 07:00:50 +00:00
|
|
|
#undef inline
|
2007-11-19 00:38:33 +00:00
|
|
|
#define inline always_inline
|
2008-06-05 22:55:54 +00:00
|
|
|
#endif
|
2007-11-19 00:38:33 +00:00
|
|
|
|
2006-08-01 15:50:14 +00:00
|
|
|
#define qemu_printf printf
|
2004-02-16 22:12:40 +00:00
|
|
|
|
2011-06-07 03:34:10 +00:00
|
|
|
int qemu_daemon(int nochdir, int noclose);
|
2014-05-20 10:24:05 +00:00
|
|
|
void *qemu_try_memalign(size_t alignment, size_t size);
|
2007-12-24 14:33:24 +00:00
|
|
|
void *qemu_memalign(size_t alignment, size_t size);
|
2014-10-31 16:38:37 +00:00
|
|
|
void *qemu_anon_ram_alloc(size_t size, uint64_t *align);
|
2005-02-10 21:59:25 +00:00
|
|
|
void qemu_vfree(void *ptr);
|
2013-05-13 14:19:56 +00:00
|
|
|
void qemu_anon_ram_free(void *ptr, size_t size);
|
2004-02-16 22:12:40 +00:00
|
|
|
|
2010-09-25 11:26:05 +00:00
|
|
|
#define QEMU_MADV_INVALID -1
|
|
|
|
|
|
|
|
#if defined(CONFIG_MADVISE)
|
|
|
|
|
|
|
|
#define QEMU_MADV_WILLNEED MADV_WILLNEED
|
|
|
|
#define QEMU_MADV_DONTNEED MADV_DONTNEED
|
|
|
|
#ifdef MADV_DONTFORK
|
|
|
|
#define QEMU_MADV_DONTFORK MADV_DONTFORK
|
|
|
|
#else
|
|
|
|
#define QEMU_MADV_DONTFORK QEMU_MADV_INVALID
|
|
|
|
#endif
|
|
|
|
#ifdef MADV_MERGEABLE
|
|
|
|
#define QEMU_MADV_MERGEABLE MADV_MERGEABLE
|
|
|
|
#else
|
|
|
|
#define QEMU_MADV_MERGEABLE QEMU_MADV_INVALID
|
|
|
|
#endif
|
2014-06-10 11:15:22 +00:00
|
|
|
#ifdef MADV_UNMERGEABLE
|
|
|
|
#define QEMU_MADV_UNMERGEABLE MADV_UNMERGEABLE
|
|
|
|
#else
|
|
|
|
#define QEMU_MADV_UNMERGEABLE QEMU_MADV_INVALID
|
|
|
|
#endif
|
|
|
|
#ifdef MADV_DODUMP
|
|
|
|
#define QEMU_MADV_DODUMP MADV_DODUMP
|
|
|
|
#else
|
|
|
|
#define QEMU_MADV_DODUMP QEMU_MADV_INVALID
|
|
|
|
#endif
|
2012-08-02 19:44:16 +00:00
|
|
|
#ifdef MADV_DONTDUMP
|
|
|
|
#define QEMU_MADV_DONTDUMP MADV_DONTDUMP
|
|
|
|
#else
|
|
|
|
#define QEMU_MADV_DONTDUMP QEMU_MADV_INVALID
|
|
|
|
#endif
|
2012-10-05 19:47:57 +00:00
|
|
|
#ifdef MADV_HUGEPAGE
|
|
|
|
#define QEMU_MADV_HUGEPAGE MADV_HUGEPAGE
|
|
|
|
#else
|
|
|
|
#define QEMU_MADV_HUGEPAGE QEMU_MADV_INVALID
|
|
|
|
#endif
|
2010-09-25 11:26:05 +00:00
|
|
|
|
|
|
|
#elif defined(CONFIG_POSIX_MADVISE)
|
|
|
|
|
|
|
|
#define QEMU_MADV_WILLNEED POSIX_MADV_WILLNEED
|
|
|
|
#define QEMU_MADV_DONTNEED POSIX_MADV_DONTNEED
|
|
|
|
#define QEMU_MADV_DONTFORK QEMU_MADV_INVALID
|
|
|
|
#define QEMU_MADV_MERGEABLE QEMU_MADV_INVALID
|
2014-06-18 18:48:19 +00:00
|
|
|
#define QEMU_MADV_UNMERGEABLE QEMU_MADV_INVALID
|
|
|
|
#define QEMU_MADV_DODUMP QEMU_MADV_INVALID
|
2012-08-02 19:44:16 +00:00
|
|
|
#define QEMU_MADV_DONTDUMP QEMU_MADV_INVALID
|
2012-10-24 16:12:15 +00:00
|
|
|
#define QEMU_MADV_HUGEPAGE QEMU_MADV_INVALID
|
2010-09-25 11:26:05 +00:00
|
|
|
|
|
|
|
#else /* no-op */
|
|
|
|
|
|
|
|
#define QEMU_MADV_WILLNEED QEMU_MADV_INVALID
|
|
|
|
#define QEMU_MADV_DONTNEED QEMU_MADV_INVALID
|
|
|
|
#define QEMU_MADV_DONTFORK QEMU_MADV_INVALID
|
|
|
|
#define QEMU_MADV_MERGEABLE QEMU_MADV_INVALID
|
2014-06-18 18:48:19 +00:00
|
|
|
#define QEMU_MADV_UNMERGEABLE QEMU_MADV_INVALID
|
|
|
|
#define QEMU_MADV_DODUMP QEMU_MADV_INVALID
|
2012-08-02 19:44:16 +00:00
|
|
|
#define QEMU_MADV_DONTDUMP QEMU_MADV_INVALID
|
2012-10-24 16:12:15 +00:00
|
|
|
#define QEMU_MADV_HUGEPAGE QEMU_MADV_INVALID
|
2010-09-25 11:26:05 +00:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
int qemu_madvise(void *addr, size_t len, int advice);
|
|
|
|
|
2012-11-14 15:42:39 +00:00
|
|
|
int qemu_open(const char *name, int flags, ...);
|
|
|
|
int qemu_close(int fd);
|
|
|
|
|
2011-06-02 17:58:06 +00:00
|
|
|
#if defined(__HAIKU__) && defined(__i386__)
|
|
|
|
#define FMT_pid "%ld"
|
2011-07-15 19:38:13 +00:00
|
|
|
#elif defined(WIN64)
|
|
|
|
#define FMT_pid "%" PRId64
|
2011-06-02 17:58:06 +00:00
|
|
|
#else
|
|
|
|
#define FMT_pid "%d"
|
|
|
|
#endif
|
|
|
|
|
2007-03-25 21:33:06 +00:00
|
|
|
int qemu_create_pidfile(const char *filename);
|
2011-03-15 11:26:31 +00:00
|
|
|
int qemu_get_thread_id(void);
|
2007-03-25 21:33:06 +00:00
|
|
|
|
2013-04-21 10:01:06 +00:00
|
|
|
#ifndef CONFIG_IOVEC
|
|
|
|
struct iovec {
|
|
|
|
void *iov_base;
|
|
|
|
size_t iov_len;
|
|
|
|
};
|
|
|
|
/*
|
|
|
|
* Use the same value as Linux for now.
|
|
|
|
*/
|
|
|
|
#define IOV_MAX 1024
|
|
|
|
|
|
|
|
ssize_t readv(int fd, const struct iovec *iov, int iov_cnt);
|
|
|
|
ssize_t writev(int fd, const struct iovec *iov, int iov_cnt);
|
|
|
|
#else
|
|
|
|
#include <sys/uio.h>
|
|
|
|
#endif
|
|
|
|
|
2011-03-13 10:30:52 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
static inline void qemu_timersub(const struct timeval *val1,
|
|
|
|
const struct timeval *val2,
|
|
|
|
struct timeval *res)
|
|
|
|
{
|
|
|
|
res->tv_sec = val1->tv_sec - val2->tv_sec;
|
|
|
|
if (val1->tv_usec < val2->tv_usec) {
|
|
|
|
res->tv_sec--;
|
|
|
|
res->tv_usec = val1->tv_usec - val2->tv_usec + 1000 * 1000;
|
|
|
|
} else {
|
|
|
|
res->tv_usec = val1->tv_usec - val2->tv_usec;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
#define qemu_timersub timersub
|
|
|
|
#endif
|
|
|
|
|
2012-03-28 13:42:05 +00:00
|
|
|
void qemu_set_cloexec(int fd);
|
|
|
|
|
2012-05-30 03:35:51 +00:00
|
|
|
void qemu_set_version(const char *);
|
|
|
|
const char *qemu_get_version(void);
|
|
|
|
|
2012-08-03 18:39:21 +00:00
|
|
|
void fips_set_state(bool requested);
|
|
|
|
bool fips_get_state(void);
|
|
|
|
|
2013-05-18 04:31:48 +00:00
|
|
|
/* Return a dynamically allocated pathname denoting a file or directory that is
|
|
|
|
* appropriate for storing local state.
|
|
|
|
*
|
|
|
|
* @relative_pathname need not start with a directory separator; one will be
|
|
|
|
* added automatically.
|
|
|
|
*
|
|
|
|
* The caller is responsible for releasing the value returned with g_free()
|
|
|
|
* after use.
|
|
|
|
*/
|
|
|
|
char *qemu_get_local_state_pathname(const char *relative_pathname);
|
|
|
|
|
2014-02-10 06:48:51 +00:00
|
|
|
/* Find program directory, and save it for later usage with
|
|
|
|
* qemu_get_exec_dir().
|
|
|
|
* Try OS specific API first, if not working, parse from argv0. */
|
|
|
|
void qemu_init_exec_dir(const char *argv0);
|
|
|
|
|
|
|
|
/* Get the saved exec dir.
|
|
|
|
* Caller needs to release the returned string by g_free() */
|
|
|
|
char *qemu_get_exec_dir(void);
|
|
|
|
|
2013-06-04 18:24:49 +00:00
|
|
|
/**
|
|
|
|
* qemu_getauxval:
|
|
|
|
* @type: the auxiliary vector key to lookup
|
|
|
|
*
|
|
|
|
* Search the auxiliary vector for @type, returning the value
|
|
|
|
* or 0 if @type is not present.
|
|
|
|
*/
|
|
|
|
unsigned long qemu_getauxval(unsigned long type);
|
|
|
|
|
2013-11-14 10:54:16 +00:00
|
|
|
void qemu_set_tty_echo(int fd, bool echo);
|
|
|
|
|
2014-05-14 09:43:21 +00:00
|
|
|
void os_mem_prealloc(int fd, char *area, size_t sz);
|
|
|
|
|
2004-02-16 22:12:40 +00:00
|
|
|
#endif
|