1 #ifndef QEMU_OSDEP_H 2 #define QEMU_OSDEP_H 3 4 #include "config-host.h" 5 #include <stdarg.h> 6 #include <stddef.h> 7 #include <stdbool.h> 8 #include <stdint.h> 9 #include <sys/types.h> 10 #ifdef __OpenBSD__ 11 #include <sys/signal.h> 12 #endif 13 14 #ifndef _WIN32 15 #include <sys/wait.h> 16 #else 17 #define WIFEXITED(x) 1 18 #define WEXITSTATUS(x) (x) 19 #endif 20 21 #include <sys/time.h> 22 23 #if defined(CONFIG_SOLARIS) && CONFIG_SOLARIS_VERSION < 10 24 /* [u]int_fast*_t not in <sys/int_types.h> */ 25 typedef unsigned char uint_fast8_t; 26 typedef unsigned int uint_fast16_t; 27 typedef signed int int_fast16_t; 28 #endif 29 30 #ifndef glue 31 #define xglue(x, y) x ## y 32 #define glue(x, y) xglue(x, y) 33 #define stringify(s) tostring(s) 34 #define tostring(s) #s 35 #endif 36 37 #ifndef likely 38 #if __GNUC__ < 3 39 #define __builtin_expect(x, n) (x) 40 #endif 41 42 #define likely(x) __builtin_expect(!!(x), 1) 43 #define unlikely(x) __builtin_expect(!!(x), 0) 44 #endif 45 46 #ifndef container_of 47 #define container_of(ptr, type, member) ({ \ 48 const typeof(((type *) 0)->member) *__mptr = (ptr); \ 49 (type *) ((char *) __mptr - offsetof(type, member));}) 50 #endif 51 52 /* Convert from a base type to a parent type, with compile time checking. */ 53 #ifdef __GNUC__ 54 #define DO_UPCAST(type, field, dev) ( __extension__ ( { \ 55 char __attribute__((unused)) offset_must_be_zero[ \ 56 -offsetof(type, field)]; \ 57 container_of(dev, type, field);})) 58 #else 59 #define DO_UPCAST(type, field, dev) container_of(dev, type, field) 60 #endif 61 62 #define typeof_field(type, field) typeof(((type *)0)->field) 63 #define type_check(t1,t2) ((t1*)0 - (t2*)0) 64 65 #ifndef MIN 66 #define MIN(a, b) (((a) < (b)) ? (a) : (b)) 67 #endif 68 #ifndef MAX 69 #define MAX(a, b) (((a) > (b)) ? (a) : (b)) 70 #endif 71 72 /* Minimum function that returns zero only iff both values are zero. 73 * Intended for use with unsigned values only. */ 74 #ifndef MIN_NON_ZERO 75 #define MIN_NON_ZERO(a, b) (((a) != 0 && (a) < (b)) ? (a) : (b)) 76 #endif 77 78 #ifndef ROUND_UP 79 #define ROUND_UP(n,d) (((n) + (d) - 1) & -(d)) 80 #endif 81 82 #ifndef DIV_ROUND_UP 83 #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d)) 84 #endif 85 86 #ifndef ARRAY_SIZE 87 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) 88 #endif 89 90 #ifndef always_inline 91 #if !((__GNUC__ < 3) || defined(__APPLE__)) 92 #ifdef __OPTIMIZE__ 93 #undef inline 94 #define inline __attribute__ (( always_inline )) __inline__ 95 #endif 96 #endif 97 #else 98 #undef inline 99 #define inline always_inline 100 #endif 101 102 #define qemu_printf printf 103 104 int qemu_daemon(int nochdir, int noclose); 105 void *qemu_try_memalign(size_t alignment, size_t size); 106 void *qemu_memalign(size_t alignment, size_t size); 107 void *qemu_anon_ram_alloc(size_t size, uint64_t *align); 108 void qemu_vfree(void *ptr); 109 void qemu_anon_ram_free(void *ptr, size_t size); 110 111 #define QEMU_MADV_INVALID -1 112 113 #if defined(CONFIG_MADVISE) 114 115 #define QEMU_MADV_WILLNEED MADV_WILLNEED 116 #define QEMU_MADV_DONTNEED MADV_DONTNEED 117 #ifdef MADV_DONTFORK 118 #define QEMU_MADV_DONTFORK MADV_DONTFORK 119 #else 120 #define QEMU_MADV_DONTFORK QEMU_MADV_INVALID 121 #endif 122 #ifdef MADV_MERGEABLE 123 #define QEMU_MADV_MERGEABLE MADV_MERGEABLE 124 #else 125 #define QEMU_MADV_MERGEABLE QEMU_MADV_INVALID 126 #endif 127 #ifdef MADV_UNMERGEABLE 128 #define QEMU_MADV_UNMERGEABLE MADV_UNMERGEABLE 129 #else 130 #define QEMU_MADV_UNMERGEABLE QEMU_MADV_INVALID 131 #endif 132 #ifdef MADV_DODUMP 133 #define QEMU_MADV_DODUMP MADV_DODUMP 134 #else 135 #define QEMU_MADV_DODUMP QEMU_MADV_INVALID 136 #endif 137 #ifdef MADV_DONTDUMP 138 #define QEMU_MADV_DONTDUMP MADV_DONTDUMP 139 #else 140 #define QEMU_MADV_DONTDUMP QEMU_MADV_INVALID 141 #endif 142 #ifdef MADV_HUGEPAGE 143 #define QEMU_MADV_HUGEPAGE MADV_HUGEPAGE 144 #else 145 #define QEMU_MADV_HUGEPAGE QEMU_MADV_INVALID 146 #endif 147 148 #elif defined(CONFIG_POSIX_MADVISE) 149 150 #define QEMU_MADV_WILLNEED POSIX_MADV_WILLNEED 151 #define QEMU_MADV_DONTNEED POSIX_MADV_DONTNEED 152 #define QEMU_MADV_DONTFORK QEMU_MADV_INVALID 153 #define QEMU_MADV_MERGEABLE QEMU_MADV_INVALID 154 #define QEMU_MADV_UNMERGEABLE QEMU_MADV_INVALID 155 #define QEMU_MADV_DODUMP QEMU_MADV_INVALID 156 #define QEMU_MADV_DONTDUMP QEMU_MADV_INVALID 157 #define QEMU_MADV_HUGEPAGE QEMU_MADV_INVALID 158 159 #else /* no-op */ 160 161 #define QEMU_MADV_WILLNEED QEMU_MADV_INVALID 162 #define QEMU_MADV_DONTNEED QEMU_MADV_INVALID 163 #define QEMU_MADV_DONTFORK QEMU_MADV_INVALID 164 #define QEMU_MADV_MERGEABLE QEMU_MADV_INVALID 165 #define QEMU_MADV_UNMERGEABLE QEMU_MADV_INVALID 166 #define QEMU_MADV_DODUMP QEMU_MADV_INVALID 167 #define QEMU_MADV_DONTDUMP QEMU_MADV_INVALID 168 #define QEMU_MADV_HUGEPAGE QEMU_MADV_INVALID 169 170 #endif 171 172 int qemu_madvise(void *addr, size_t len, int advice); 173 174 int qemu_open(const char *name, int flags, ...); 175 int qemu_close(int fd); 176 177 #if defined(__HAIKU__) && defined(__i386__) 178 #define FMT_pid "%ld" 179 #elif defined(WIN64) 180 #define FMT_pid "%" PRId64 181 #else 182 #define FMT_pid "%d" 183 #endif 184 185 int qemu_create_pidfile(const char *filename); 186 int qemu_get_thread_id(void); 187 188 #ifndef CONFIG_IOVEC 189 struct iovec { 190 void *iov_base; 191 size_t iov_len; 192 }; 193 /* 194 * Use the same value as Linux for now. 195 */ 196 #define IOV_MAX 1024 197 198 ssize_t readv(int fd, const struct iovec *iov, int iov_cnt); 199 ssize_t writev(int fd, const struct iovec *iov, int iov_cnt); 200 #else 201 #include <sys/uio.h> 202 #endif 203 204 #ifdef _WIN32 205 static inline void qemu_timersub(const struct timeval *val1, 206 const struct timeval *val2, 207 struct timeval *res) 208 { 209 res->tv_sec = val1->tv_sec - val2->tv_sec; 210 if (val1->tv_usec < val2->tv_usec) { 211 res->tv_sec--; 212 res->tv_usec = val1->tv_usec - val2->tv_usec + 1000 * 1000; 213 } else { 214 res->tv_usec = val1->tv_usec - val2->tv_usec; 215 } 216 } 217 #else 218 #define qemu_timersub timersub 219 #endif 220 221 void qemu_set_cloexec(int fd); 222 223 void qemu_set_version(const char *); 224 const char *qemu_get_version(void); 225 226 void fips_set_state(bool requested); 227 bool fips_get_state(void); 228 229 /* Return a dynamically allocated pathname denoting a file or directory that is 230 * appropriate for storing local state. 231 * 232 * @relative_pathname need not start with a directory separator; one will be 233 * added automatically. 234 * 235 * The caller is responsible for releasing the value returned with g_free() 236 * after use. 237 */ 238 char *qemu_get_local_state_pathname(const char *relative_pathname); 239 240 /* Find program directory, and save it for later usage with 241 * qemu_get_exec_dir(). 242 * Try OS specific API first, if not working, parse from argv0. */ 243 void qemu_init_exec_dir(const char *argv0); 244 245 /* Get the saved exec dir. 246 * Caller needs to release the returned string by g_free() */ 247 char *qemu_get_exec_dir(void); 248 249 /** 250 * qemu_getauxval: 251 * @type: the auxiliary vector key to lookup 252 * 253 * Search the auxiliary vector for @type, returning the value 254 * or 0 if @type is not present. 255 */ 256 unsigned long qemu_getauxval(unsigned long type); 257 258 void qemu_set_tty_echo(int fd, bool echo); 259 260 void os_mem_prealloc(int fd, char *area, size_t sz); 261 262 int qemu_read_password(char *buf, int buf_size); 263 264 #endif 265