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