1#ifndef QEMU_H
2#define QEMU_H
3
4#include "cpu.h"
5#include "exec/cpu_ldst.h"
6
7#undef DEBUG_REMAP
8
9#include "exec/user/abitypes.h"
10
11#include "syscall_defs.h"
12#include "target_syscall.h"
13
14
15
16
17
18#define SIGSET_T_SIZE (_NSIG / 8)
19
20
21
22
23
24
25struct image_info {
26 abi_ulong load_bias;
27 abi_ulong load_addr;
28 abi_ulong start_code;
29 abi_ulong end_code;
30 abi_ulong start_data;
31 abi_ulong end_data;
32 abi_ulong start_brk;
33 abi_ulong brk;
34 abi_ulong reserve_brk;
35 abi_ulong start_mmap;
36 abi_ulong start_stack;
37 abi_ulong stack_limit;
38 abi_ulong entry;
39 abi_ulong code_offset;
40 abi_ulong data_offset;
41 abi_ulong saved_auxv;
42 abi_ulong auxv_len;
43 abi_ulong arg_start;
44 abi_ulong arg_end;
45 abi_ulong arg_strings;
46 abi_ulong env_strings;
47 abi_ulong file_string;
48 uint32_t elf_flags;
49 int personality;
50 abi_ulong alignment;
51
52
53 abi_ulong loadmap_addr;
54 uint16_t nsegs;
55 void *loadsegs;
56 abi_ulong pt_dynamic_addr;
57 abi_ulong interpreter_loadmap_addr;
58 abi_ulong interpreter_pt_dynamic_addr;
59 struct image_info *other_info;
60
61
62 uint32_t note_flags;
63
64#ifdef TARGET_MIPS
65 int fp_abi;
66 int interp_fp_abi;
67#endif
68};
69
70#ifdef TARGET_I386
71
72struct vm86_saved_state {
73 uint32_t eax;
74 uint32_t ebx;
75 uint32_t ecx;
76 uint32_t edx;
77 uint32_t esi;
78 uint32_t edi;
79 uint32_t ebp;
80 uint32_t esp;
81 uint32_t eflags;
82 uint32_t eip;
83 uint16_t cs, ss, ds, es, fs, gs;
84};
85#endif
86
87#if defined(TARGET_ARM) && defined(TARGET_ABI32)
88
89#include "nwfpe/fpa11.h"
90#endif
91
92#define MAX_SIGQUEUE_SIZE 1024
93
94struct emulated_sigtable {
95 int pending;
96 target_siginfo_t info;
97};
98
99
100
101
102
103typedef struct TaskState {
104 pid_t ts_tid;
105#ifdef TARGET_ARM
106# ifdef TARGET_ABI32
107
108 FPA11 fpa;
109# endif
110#endif
111#if defined(TARGET_ARM) || defined(TARGET_RISCV)
112 int swi_errno;
113#endif
114#if defined(TARGET_I386) && !defined(TARGET_X86_64)
115 abi_ulong target_v86;
116 struct vm86_saved_state vm86_saved_regs;
117 struct target_vm86plus_struct vm86plus;
118 uint32_t v86flags;
119 uint32_t v86mask;
120#endif
121 abi_ulong child_tidptr;
122#ifdef TARGET_M68K
123 abi_ulong tp_value;
124#endif
125#if defined(TARGET_ARM) || defined(TARGET_M68K) || defined(TARGET_RISCV)
126
127 abi_ulong heap_base;
128 abi_ulong heap_limit;
129#endif
130 abi_ulong stack_base;
131 int used;
132 struct image_info *info;
133 struct linux_binprm *bprm;
134
135 struct emulated_sigtable sync_signal;
136 struct emulated_sigtable sigtab[TARGET_NSIG];
137
138
139
140
141
142
143 sigset_t signal_mask;
144
145
146
147
148 sigset_t sigsuspend_mask;
149
150 int in_sigsuspend;
151
152
153
154
155
156
157
158
159 int signal_pending;
160
161
162 struct target_sigaltstack sigaltstack_used;
163} __attribute__((aligned(16))) TaskState;
164
165abi_long do_brk(abi_ulong new_brk);
166
167
168
169#define VERIFY_READ PAGE_READ
170#define VERIFY_WRITE (PAGE_READ | PAGE_WRITE)
171
172static inline bool access_ok_untagged(int type, abi_ulong addr, abi_ulong size)
173{
174 if (size == 0
175 ? !guest_addr_valid_untagged(addr)
176 : !guest_range_valid_untagged(addr, size)) {
177 return false;
178 }
179 return page_check_range((target_ulong)addr, size, type) == 0;
180}
181
182static inline bool access_ok(CPUState *cpu, int type,
183 abi_ulong addr, abi_ulong size)
184{
185 return access_ok_untagged(type, cpu_untagged_addr(cpu, addr), size);
186}
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205#if defined(__clang__) && __has_warning("-Waddress-of-packed-member")
206#define PRAGMA_DISABLE_PACKED_WARNING \
207 _Pragma("GCC diagnostic push"); \
208 _Pragma("GCC diagnostic ignored \"-Waddress-of-packed-member\"")
209
210#define PRAGMA_REENABLE_PACKED_WARNING \
211 _Pragma("GCC diagnostic pop")
212
213#else
214#define PRAGMA_DISABLE_PACKED_WARNING
215#define PRAGMA_REENABLE_PACKED_WARNING
216#endif
217
218#define __put_user_e(x, hptr, e) \
219 do { \
220 PRAGMA_DISABLE_PACKED_WARNING; \
221 (__builtin_choose_expr(sizeof(*(hptr)) == 1, stb_p, \
222 __builtin_choose_expr(sizeof(*(hptr)) == 2, stw_##e##_p, \
223 __builtin_choose_expr(sizeof(*(hptr)) == 4, stl_##e##_p, \
224 __builtin_choose_expr(sizeof(*(hptr)) == 8, stq_##e##_p, abort)))) \
225 ((hptr), (x)), (void)0); \
226 PRAGMA_REENABLE_PACKED_WARNING; \
227 } while (0)
228
229#define __get_user_e(x, hptr, e) \
230 do { \
231 PRAGMA_DISABLE_PACKED_WARNING; \
232 ((x) = (typeof(*hptr))( \
233 __builtin_choose_expr(sizeof(*(hptr)) == 1, ldub_p, \
234 __builtin_choose_expr(sizeof(*(hptr)) == 2, lduw_##e##_p, \
235 __builtin_choose_expr(sizeof(*(hptr)) == 4, ldl_##e##_p, \
236 __builtin_choose_expr(sizeof(*(hptr)) == 8, ldq_##e##_p, abort)))) \
237 (hptr)), (void)0); \
238 PRAGMA_REENABLE_PACKED_WARNING; \
239 } while (0)
240
241
242#ifdef TARGET_WORDS_BIGENDIAN
243# define __put_user(x, hptr) __put_user_e(x, hptr, be)
244# define __get_user(x, hptr) __get_user_e(x, hptr, be)
245#else
246# define __put_user(x, hptr) __put_user_e(x, hptr, le)
247# define __get_user(x, hptr) __get_user_e(x, hptr, le)
248#endif
249
250
251
252
253
254
255#define put_user(x, gaddr, target_type) \
256({ \
257 abi_ulong __gaddr = (gaddr); \
258 target_type *__hptr; \
259 abi_long __ret = 0; \
260 if ((__hptr = lock_user(VERIFY_WRITE, __gaddr, sizeof(target_type), 0))) { \
261 __put_user((x), __hptr); \
262 unlock_user(__hptr, __gaddr, sizeof(target_type)); \
263 } else \
264 __ret = -TARGET_EFAULT; \
265 __ret; \
266})
267
268#define get_user(x, gaddr, target_type) \
269({ \
270 abi_ulong __gaddr = (gaddr); \
271 target_type *__hptr; \
272 abi_long __ret = 0; \
273 if ((__hptr = lock_user(VERIFY_READ, __gaddr, sizeof(target_type), 1))) { \
274 __get_user((x), __hptr); \
275 unlock_user(__hptr, __gaddr, 0); \
276 } else { \
277 \
278 (x) = 0; \
279 __ret = -TARGET_EFAULT; \
280 } \
281 __ret; \
282})
283
284#define put_user_ual(x, gaddr) put_user((x), (gaddr), abi_ulong)
285#define put_user_sal(x, gaddr) put_user((x), (gaddr), abi_long)
286#define put_user_u64(x, gaddr) put_user((x), (gaddr), uint64_t)
287#define put_user_s64(x, gaddr) put_user((x), (gaddr), int64_t)
288#define put_user_u32(x, gaddr) put_user((x), (gaddr), uint32_t)
289#define put_user_s32(x, gaddr) put_user((x), (gaddr), int32_t)
290#define put_user_u16(x, gaddr) put_user((x), (gaddr), uint16_t)
291#define put_user_s16(x, gaddr) put_user((x), (gaddr), int16_t)
292#define put_user_u8(x, gaddr) put_user((x), (gaddr), uint8_t)
293#define put_user_s8(x, gaddr) put_user((x), (gaddr), int8_t)
294
295#define get_user_ual(x, gaddr) get_user((x), (gaddr), abi_ulong)
296#define get_user_sal(x, gaddr) get_user((x), (gaddr), abi_long)
297#define get_user_u64(x, gaddr) get_user((x), (gaddr), uint64_t)
298#define get_user_s64(x, gaddr) get_user((x), (gaddr), int64_t)
299#define get_user_u32(x, gaddr) get_user((x), (gaddr), uint32_t)
300#define get_user_s32(x, gaddr) get_user((x), (gaddr), int32_t)
301#define get_user_u16(x, gaddr) get_user((x), (gaddr), uint16_t)
302#define get_user_s16(x, gaddr) get_user((x), (gaddr), int16_t)
303#define get_user_u8(x, gaddr) get_user((x), (gaddr), uint8_t)
304#define get_user_s8(x, gaddr) get_user((x), (gaddr), int8_t)
305
306
307
308
309
310int copy_from_user(void *hptr, abi_ulong gaddr, ssize_t len);
311int copy_to_user(abi_ulong gaddr, void *hptr, ssize_t len);
312
313
314
315
316
317
318
319
320
321void *lock_user(int type, abi_ulong guest_addr, ssize_t len, bool copy);
322
323
324
325
326#ifndef DEBUG_REMAP
327static inline void unlock_user(void *host_ptr, abi_ulong guest_addr,
328 ssize_t len)
329{
330
331}
332#else
333void unlock_user(void *host_ptr, abi_ulong guest_addr, ssize_t len);
334#endif
335
336
337
338ssize_t target_strlen(abi_ulong gaddr);
339
340
341void *lock_user_string(abi_ulong guest_addr);
342
343
344#define lock_user_struct(type, host_ptr, guest_addr, copy) \
345 (host_ptr = lock_user(type, guest_addr, sizeof(*host_ptr), copy))
346#define unlock_user_struct(host_ptr, guest_addr, copy) \
347 unlock_user(host_ptr, guest_addr, (copy) ? sizeof(*host_ptr) : 0)
348
349#endif
350