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