master
h 373 lines 13.2 KB
Raw
1 #ifndef QEMU_H
2 #define QEMU_H
3
4 #include "cpu.h"
5 #include "accel/tcg/cpu-ldst.h"
6
7 #include "user/abitypes.h"
8 #include "user/page-protection.h"
9 #include "exec/page-protection.h"
10
11 #include "syscall_defs.h"
12 #include "target_syscall.h"
13 #include "accel/tcg/vcpu-state.h"
14 #include "user/guest-host.h"
15
16 /*
17 * This is the size of the host kernel's sigset_t, needed where we make
18 * direct system calls that take a sigset_t pointer and a size.
19 */
20 #define SIGSET_T_SIZE (_NSIG / 8)
21
22 /*
23 * This struct is used to hold certain information about the image.
24 * Basically, it replicates in user space what would be certain
25 * task_struct fields in the kernel
26 */
27 struct image_info {
28 abi_ulong load_bias;
29 abi_ulong load_addr;
30 abi_ulong phdr_addr;
31 abi_ulong start_code;
32 abi_ulong end_code;
33 abi_ulong start_data;
34 abi_ulong end_data;
35 abi_ulong brk;
36 abi_ulong start_stack;
37 abi_ulong stack_limit;
38 abi_ulong vdso;
39 abi_ulong entry;
40 abi_ulong code_offset;
41 abi_ulong data_offset;
42 abi_ulong saved_auxv;
43 abi_ulong auxv_len;
44 abi_ulong argc;
45 abi_ulong argv;
46 abi_ulong envc;
47 abi_ulong envp;
48 abi_ulong file_string;
49 uint32_t elf_flags;
50 int personality;
51 bool exec_stack;
52
53 /* Generic semihosting knows about these pointers. */
54 abi_ulong arg_strings; /* strings for argv */
55 abi_ulong env_strings; /* strings for envp; ends arg_strings */
56
57 /* The fields below are used in FDPIC mode. */
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 /* For target-specific processing of NT_GNU_PROPERTY_TYPE_0. */
67 uint32_t note_flags;
68
69 #ifdef TARGET_MIPS
70 bool use_k0_tls;
71 int fp_abi;
72 int interp_fp_abi;
73 #endif
74 };
75
76 #ifdef TARGET_I386
77 /* Information about the current linux thread */
78 struct vm86_saved_state {
79 uint32_t eax; /* return code */
80 uint32_t ebx;
81 uint32_t ecx;
82 uint32_t edx;
83 uint32_t esi;
84 uint32_t edi;
85 uint32_t ebp;
86 uint32_t esp;
87 uint32_t eflags;
88 uint32_t eip;
89 uint16_t cs, ss, ds, es, fs, gs;
90 };
91 #endif
92
93 #if defined(TARGET_ARM) && defined(TARGET_ABI32)
94 /* FPU emulator */
95 #include "nwfpe/fpa11.h"
96 #endif
97
98 struct emulated_sigtable {
99 int pending; /* true if signal is pending */
100 target_siginfo_t info;
101 };
102
103 struct TaskState {
104 pid_t ts_tid; /* tid (or pid) of this task */
105 #ifdef TARGET_ARM
106 # ifdef TARGET_ABI32
107 /* FPA state */
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 #if defined(TARGET_I386)
122 /* Last syscall number. */
123 target_ulong orig_ax;
124 #endif
125 abi_ulong child_tidptr;
126 #ifdef TARGET_M68K
127 abi_ulong tp_value;
128 #endif
129 #if defined(TARGET_AARCH64)
130 vaddr gcs_base;
131 abi_ulong gcs_size;
132 abi_ulong gcs_el0_locked;
133 #endif
134 int used; /* non zero if used */
135 struct image_info *info;
136 struct linux_binprm *bprm;
137
138 struct emulated_sigtable sync_signal;
139 struct emulated_sigtable sigtab[TARGET_NSIG];
140 /*
141 * This thread's signal mask, as requested by the guest program.
142 * The actual signal mask of this thread may differ:
143 * + we don't let SIGSEGV and SIGBUS be blocked while running guest code
144 * + sometimes we block all signals to avoid races
145 */
146 sigset_t signal_mask;
147 /*
148 * The signal mask imposed by a guest sigsuspend syscall, if we are
149 * currently in the middle of such a syscall
150 */
151 sigset_t sigsuspend_mask;
152 /* Nonzero if we're leaving a sigsuspend and sigsuspend_mask is valid. */
153 int in_sigsuspend;
154
155 /*
156 * Nonzero if process_pending_signals() needs to do something (either
157 * handle a pending signal or unblock signals).
158 * This flag is written from a signal handler so should be accessed via
159 * the qatomic_read() and qatomic_set() functions. (It is not accessed
160 * from multiple threads.)
161 */
162 int signal_pending;
163
164 /* This thread's sigaltstack, if it has one */
165 struct target_sigaltstack sigaltstack_used;
166
167 /* This thread's SYSCALL_USER_DISPATCH state, len=~0 means disabled */
168 vaddr sys_dispatch;
169 vaddr sys_dispatch_selector;
170 abi_ulong sys_dispatch_len;
171
172 /* Start time of task after system boot in clock ticks */
173 uint64_t start_boottime;
174 };
175
176 abi_long do_brk(abi_ulong new_brk);
177 int do_guest_openat(CPUArchState *cpu_env, int dirfd, const char *pathname,
178 int flags, mode_t mode, bool safe);
179 ssize_t do_guest_readlink(const char *pathname, char *buf, size_t bufsiz);
180
181 /* user access */
182
183 #define VERIFY_NONE 0
184 #define VERIFY_READ PAGE_READ
185 #define VERIFY_WRITE (PAGE_READ | PAGE_WRITE)
186
187 static inline bool access_ok_untagged(int type, abi_ulong addr, abi_ulong size)
188 {
189 if (size == 0
190 ? !guest_addr_valid_untagged(addr)
191 : !guest_range_valid_untagged(addr, size)) {
192 return false;
193 }
194 return page_check_range((target_ulong)addr, size, type);
195 }
196
197 static inline bool access_ok(CPUState *cpu, int type,
198 abi_ulong addr, abi_ulong size)
199 {
200 return access_ok_untagged(type, cpu_untagged_addr(cpu, addr), size);
201 }
202
203 /* NOTE __get_user and __put_user use host pointers and don't check access.
204 These are usually used to access struct data members once the struct has
205 been locked - usually with lock_user_struct. */
206
207 /*
208 * Tricky points:
209 * - Use __builtin_choose_expr to avoid type promotion from ?:,
210 * - Invalid sizes result in a compile time error stemming from
211 * the fact that abort has no parameters.
212 * - It's easier to use the endian-specific unaligned load/store
213 * functions than host-endian unaligned load/store plus tswapN.
214 * - The pragmas are necessary only to silence a clang false-positive
215 * warning: see https://bugs.llvm.org/show_bug.cgi?id=39113 .
216 * - gcc has bugs in its _Pragma() support in some versions, eg
217 * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=83256 -- so we only
218 * include the warning-suppression pragmas for clang
219 */
220 #if defined(__clang__) && __has_warning("-Waddress-of-packed-member")
221 #define PRAGMA_DISABLE_PACKED_WARNING \
222 _Pragma("GCC diagnostic push"); \
223 _Pragma("GCC diagnostic ignored \"-Waddress-of-packed-member\"")
224
225 #define PRAGMA_REENABLE_PACKED_WARNING \
226 _Pragma("GCC diagnostic pop")
227
228 #else
229 #define PRAGMA_DISABLE_PACKED_WARNING
230 #define PRAGMA_REENABLE_PACKED_WARNING
231 #endif
232
233 #define __put_user_e(x, hptr, e) \
234 do { \
235 PRAGMA_DISABLE_PACKED_WARNING; \
236 (__builtin_choose_expr(sizeof(*(hptr)) == 1, stb_p, \
237 __builtin_choose_expr(sizeof(*(hptr)) == 2, stw_##e##_p, \
238 __builtin_choose_expr(sizeof(*(hptr)) == 4, stl_##e##_p, \
239 __builtin_choose_expr(sizeof(*(hptr)) == 8, stq_##e##_p, abort)))) \
240 ((hptr), (x)), (void)0); \
241 PRAGMA_REENABLE_PACKED_WARNING; \
242 } while (0)
243
244 #define __get_user_e(x, hptr, e) \
245 do { \
246 PRAGMA_DISABLE_PACKED_WARNING; \
247 ((x) = (typeof(*hptr))( \
248 __builtin_choose_expr(sizeof(*(hptr)) == 1, ldub_p, \
249 __builtin_choose_expr(sizeof(*(hptr)) == 2, lduw_##e##_p, \
250 __builtin_choose_expr(sizeof(*(hptr)) == 4, ldl_##e##_p, \
251 __builtin_choose_expr(sizeof(*(hptr)) == 8, ldq_##e##_p, abort)))) \
252 (hptr)), (void)0); \
253 PRAGMA_REENABLE_PACKED_WARNING; \
254 } while (0)
255
256
257 #if TARGET_BIG_ENDIAN
258 # define __put_user(x, hptr) __put_user_e(x, hptr, be)
259 # define __get_user(x, hptr) __get_user_e(x, hptr, be)
260 #else
261 # define __put_user(x, hptr) __put_user_e(x, hptr, le)
262 # define __get_user(x, hptr) __get_user_e(x, hptr, le)
263 #endif
264
265 /* put_user()/get_user() take a guest address and check access */
266 /* These are usually used to access an atomic data type, such as an int,
267 * that has been passed by address. These internally perform locking
268 * and unlocking on the data type.
269 */
270 #define put_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_WRITE, __gaddr, sizeof(target_type), 0))) { \
276 __put_user((x), __hptr); \
277 unlock_user(__hptr, __gaddr, sizeof(target_type)); \
278 } else \
279 __ret = -TARGET_EFAULT; \
280 __ret; \
281 })
282
283 #define get_user(x, gaddr, target_type) \
284 ({ \
285 abi_ulong __gaddr = (gaddr); \
286 target_type *__hptr; \
287 abi_long __ret = 0; \
288 if ((__hptr = lock_user(VERIFY_READ, __gaddr, sizeof(target_type), 1))) { \
289 __get_user((x), __hptr); \
290 unlock_user(__hptr, __gaddr, 0); \
291 } else { \
292 /* avoid warning */ \
293 (x) = 0; \
294 __ret = -TARGET_EFAULT; \
295 } \
296 __ret; \
297 })
298
299 #define put_user_ual(x, gaddr) put_user((x), (gaddr), abi_ulong)
300 #define put_user_sal(x, gaddr) put_user((x), (gaddr), abi_long)
301 #define put_user_u64(x, gaddr) put_user((x), (gaddr), uint64_t)
302 #define put_user_s64(x, gaddr) put_user((x), (gaddr), int64_t)
303 #define put_user_u32(x, gaddr) put_user((x), (gaddr), uint32_t)
304 #define put_user_s32(x, gaddr) put_user((x), (gaddr), int32_t)
305 #define put_user_u16(x, gaddr) put_user((x), (gaddr), uint16_t)
306 #define put_user_s16(x, gaddr) put_user((x), (gaddr), int16_t)
307 #define put_user_u8(x, gaddr) put_user((x), (gaddr), uint8_t)
308 #define put_user_s8(x, gaddr) put_user((x), (gaddr), int8_t)
309
310 #define get_user_ual(x, gaddr) get_user((x), (gaddr), abi_ulong)
311 #define get_user_sal(x, gaddr) get_user((x), (gaddr), abi_long)
312 #define get_user_u64(x, gaddr) get_user((x), (gaddr), uint64_t)
313 #define get_user_s64(x, gaddr) get_user((x), (gaddr), int64_t)
314 #define get_user_u32(x, gaddr) get_user((x), (gaddr), uint32_t)
315 #define get_user_s32(x, gaddr) get_user((x), (gaddr), int32_t)
316 #define get_user_u16(x, gaddr) get_user((x), (gaddr), uint16_t)
317 #define get_user_s16(x, gaddr) get_user((x), (gaddr), int16_t)
318 #define get_user_u8(x, gaddr) get_user((x), (gaddr), uint8_t)
319 #define get_user_s8(x, gaddr) get_user((x), (gaddr), int8_t)
320
321 /* copy_from_user() and copy_to_user() are usually used to copy data
322 * buffers between the target and host. These internally perform
323 * locking/unlocking of the memory.
324 */
325 int copy_from_user(void *hptr, abi_ulong gaddr, ssize_t len);
326 int copy_to_user(abi_ulong gaddr, void *hptr, ssize_t len);
327
328 /*
329 * copy_struct_from_user() copies a target struct to a host struct, in
330 * a way that guarantees backwards-compatibility for struct syscall
331 * arguments.
332 *
333 * Similar to kernels uaccess.h:copy_struct_from_user()
334 */
335 int copy_struct_from_user(void *dst, size_t ksize, abi_ptr src, size_t usize);
336
337 /* Functions for accessing guest memory. The tget and tput functions
338 read/write single values, byteswapping as necessary. The lock_user function
339 gets a pointer to a contiguous area of guest memory, but does not perform
340 any byteswapping. lock_user may return either a pointer to the guest
341 memory, or a temporary buffer. */
342
343 /* Lock an area of guest memory into the host. If copy is true then the
344 host area will have the same contents as the guest. */
345 void *lock_user(int type, abi_ulong guest_addr, ssize_t len, bool copy);
346
347 /* Unlock an area of guest memory. The first LEN bytes must be
348 flushed back to guest memory. host_ptr = NULL is explicitly
349 allowed and does nothing. */
350 #ifndef CONFIG_DEBUG_REMAP
351 static inline void unlock_user(void *host_ptr, abi_ulong guest_addr,
352 ssize_t len)
353 {
354 /* no-op */
355 }
356 #else
357 void unlock_user(void *host_ptr, abi_ulong guest_addr, ssize_t len);
358 #endif
359
360 /* Return the length of a string in target memory or -TARGET_EFAULT if
361 access error. */
362 ssize_t target_strlen(abi_ulong gaddr);
363
364 /* Like lock_user but for null terminated strings. */
365 void *lock_user_string(abi_ulong guest_addr);
366
367 /* Helper macros for locking/unlocking a target struct. */
368 #define lock_user_struct(type, host_ptr, guest_addr, copy) \
369 (host_ptr = lock_user(type, guest_addr, sizeof(*host_ptr), copy))
370 #define unlock_user_struct(host_ptr, guest_addr, copy) \
371 unlock_user(host_ptr, guest_addr, (copy) ? sizeof(*host_ptr) : 0)
372
373 #endif /* QEMU_H */