]> git.proxmox.com Git - mirror_qemu.git/blob - linux-user/qemu.h
a82a46236e640c39416696d6e0a1ac4c8ace7684
[mirror_qemu.git] / linux-user / qemu.h
1 #ifndef QEMU_H
2 #define QEMU_H
3
4 #include "hostdep.h"
5 #include "cpu.h"
6 #include "exec/exec-all.h"
7 #include "exec/cpu_ldst.h"
8
9 #undef DEBUG_REMAP
10
11 #include "exec/user/abitypes.h"
12
13 #include "exec/user/thunk.h"
14 #include "syscall_defs.h"
15 #include "target_syscall.h"
16 #include "exec/gdbstub.h"
17
18 /*
19 * This is the size of the host kernel's sigset_t, needed where we make
20 * direct system calls that take a sigset_t pointer and a size.
21 */
22 #define SIGSET_T_SIZE (_NSIG / 8)
23
24 /*
25 * This struct is used to hold certain information about the image.
26 * Basically, it replicates in user space what would be certain
27 * task_struct fields in the kernel
28 */
29 struct image_info {
30 abi_ulong load_bias;
31 abi_ulong load_addr;
32 abi_ulong start_code;
33 abi_ulong end_code;
34 abi_ulong start_data;
35 abi_ulong end_data;
36 abi_ulong start_brk;
37 abi_ulong brk;
38 abi_ulong reserve_brk;
39 abi_ulong start_mmap;
40 abi_ulong start_stack;
41 abi_ulong stack_limit;
42 abi_ulong entry;
43 abi_ulong code_offset;
44 abi_ulong data_offset;
45 abi_ulong saved_auxv;
46 abi_ulong auxv_len;
47 abi_ulong arg_start;
48 abi_ulong arg_end;
49 abi_ulong arg_strings;
50 abi_ulong env_strings;
51 abi_ulong file_string;
52 uint32_t elf_flags;
53 int personality;
54 abi_ulong alignment;
55
56 /* The fields below are used in FDPIC mode. */
57 abi_ulong loadmap_addr;
58 uint16_t nsegs;
59 void *loadsegs;
60 abi_ulong pt_dynamic_addr;
61 abi_ulong interpreter_loadmap_addr;
62 abi_ulong interpreter_pt_dynamic_addr;
63 struct image_info *other_info;
64
65 /* For target-specific processing of NT_GNU_PROPERTY_TYPE_0. */
66 uint32_t note_flags;
67
68 #ifdef TARGET_MIPS
69 int fp_abi;
70 int interp_fp_abi;
71 #endif
72 };
73
74 #ifdef TARGET_I386
75 /* Information about the current linux thread */
76 struct vm86_saved_state {
77 uint32_t eax; /* return code */
78 uint32_t ebx;
79 uint32_t ecx;
80 uint32_t edx;
81 uint32_t esi;
82 uint32_t edi;
83 uint32_t ebp;
84 uint32_t esp;
85 uint32_t eflags;
86 uint32_t eip;
87 uint16_t cs, ss, ds, es, fs, gs;
88 };
89 #endif
90
91 #if defined(TARGET_ARM) && defined(TARGET_ABI32)
92 /* FPU emulator */
93 #include "nwfpe/fpa11.h"
94 #endif
95
96 #define MAX_SIGQUEUE_SIZE 1024
97
98 struct emulated_sigtable {
99 int pending; /* true if signal is pending */
100 target_siginfo_t info;
101 };
102
103 /*
104 * NOTE: we force a big alignment so that the stack stored after is
105 * aligned too
106 */
107 typedef struct TaskState {
108 pid_t ts_tid; /* tid (or pid) of this task */
109 #ifdef TARGET_ARM
110 # ifdef TARGET_ABI32
111 /* FPA state */
112 FPA11 fpa;
113 # endif
114 #endif
115 #if defined(TARGET_ARM) || defined(TARGET_RISCV)
116 int swi_errno;
117 #endif
118 #if defined(TARGET_I386) && !defined(TARGET_X86_64)
119 abi_ulong target_v86;
120 struct vm86_saved_state vm86_saved_regs;
121 struct target_vm86plus_struct vm86plus;
122 uint32_t v86flags;
123 uint32_t v86mask;
124 #endif
125 abi_ulong child_tidptr;
126 #ifdef TARGET_M68K
127 abi_ulong tp_value;
128 #endif
129 #if defined(TARGET_ARM) || defined(TARGET_M68K) || defined(TARGET_RISCV)
130 /* Extra fields for semihosted binaries. */
131 abi_ulong heap_base;
132 abi_ulong heap_limit;
133 #endif
134 abi_ulong stack_base;
135 int used; /* non zero if used */
136 struct image_info *info;
137 struct linux_binprm *bprm;
138
139 struct emulated_sigtable sync_signal;
140 struct emulated_sigtable sigtab[TARGET_NSIG];
141 /*
142 * This thread's signal mask, as requested by the guest program.
143 * The actual signal mask of this thread may differ:
144 * + we don't let SIGSEGV and SIGBUS be blocked while running guest code
145 * + sometimes we block all signals to avoid races
146 */
147 sigset_t signal_mask;
148 /*
149 * The signal mask imposed by a guest sigsuspend syscall, if we are
150 * currently in the middle of such a syscall
151 */
152 sigset_t sigsuspend_mask;
153 /* Nonzero if we're leaving a sigsuspend and sigsuspend_mask is valid. */
154 int in_sigsuspend;
155
156 /*
157 * Nonzero if process_pending_signals() needs to do something (either
158 * handle a pending signal or unblock signals).
159 * This flag is written from a signal handler so should be accessed via
160 * the qatomic_read() and qatomic_set() functions. (It is not accessed
161 * from multiple threads.)
162 */
163 int signal_pending;
164
165 /* This thread's sigaltstack, if it has one */
166 struct target_sigaltstack sigaltstack_used;
167 } __attribute__((aligned(16))) TaskState;
168
169 extern char *exec_path;
170 void init_task_state(TaskState *ts);
171 void task_settid(TaskState *);
172 void stop_all_tasks(void);
173 extern const char *qemu_uname_release;
174 extern unsigned long mmap_min_addr;
175
176 typedef struct IOCTLEntry IOCTLEntry;
177
178 typedef abi_long do_ioctl_fn(const IOCTLEntry *ie, uint8_t *buf_temp,
179 int fd, int cmd, abi_long arg);
180
181 struct IOCTLEntry {
182 int target_cmd;
183 unsigned int host_cmd;
184 const char *name;
185 int access;
186 do_ioctl_fn *do_ioctl;
187 const argtype arg_type[5];
188 };
189
190 extern IOCTLEntry ioctl_entries[];
191
192 #define IOC_R 0x0001
193 #define IOC_W 0x0002
194 #define IOC_RW (IOC_R | IOC_W)
195
196 /*
197 * Returns true if the image uses the FDPIC ABI. If this is the case,
198 * we have to provide some information (loadmap, pt_dynamic_info) such
199 * that the program can be relocated adequately. This is also useful
200 * when handling signals.
201 */
202 int info_is_fdpic(struct image_info *info);
203
204 void target_set_brk(abi_ulong new_brk);
205 abi_long do_brk(abi_ulong new_brk);
206 void syscall_init(void);
207 abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
208 abi_long arg2, abi_long arg3, abi_long arg4,
209 abi_long arg5, abi_long arg6, abi_long arg7,
210 abi_long arg8);
211 extern __thread CPUState *thread_cpu;
212 void cpu_loop(CPUArchState *env);
213 const char *target_strerror(int err);
214 int get_osversion(void);
215 void init_qemu_uname_release(void);
216 void fork_start(void);
217 void fork_end(int child);
218
219 /**
220 * probe_guest_base:
221 * @image_name: the executable being loaded
222 * @loaddr: the lowest fixed address in the executable
223 * @hiaddr: the highest fixed address in the executable
224 *
225 * Creates the initial guest address space in the host memory space.
226 *
227 * If @loaddr == 0, then no address in the executable is fixed,
228 * i.e. it is fully relocatable. In that case @hiaddr is the size
229 * of the executable.
230 *
231 * This function will not return if a valid value for guest_base
232 * cannot be chosen. On return, the executable loader can expect
233 *
234 * target_mmap(loaddr, hiaddr - loaddr, ...)
235 *
236 * to succeed.
237 */
238 void probe_guest_base(const char *image_name,
239 abi_ulong loaddr, abi_ulong hiaddr);
240
241 #include "qemu/log.h"
242
243 /* syscall.c */
244 int host_to_target_waitstatus(int status);
245
246 #ifdef TARGET_I386
247 /* vm86.c */
248 void save_v86_state(CPUX86State *env);
249 void handle_vm86_trap(CPUX86State *env, int trapno);
250 void handle_vm86_fault(CPUX86State *env);
251 int do_vm86(CPUX86State *env, long subfunction, abi_ulong v86_addr);
252 #elif defined(TARGET_SPARC64)
253 void sparc64_set_context(CPUSPARCState *env);
254 void sparc64_get_context(CPUSPARCState *env);
255 #endif
256
257 /* user access */
258
259 #define VERIFY_READ PAGE_READ
260 #define VERIFY_WRITE (PAGE_READ | PAGE_WRITE)
261
262 static inline bool access_ok_untagged(int type, abi_ulong addr, abi_ulong size)
263 {
264 if (size == 0
265 ? !guest_addr_valid_untagged(addr)
266 : !guest_range_valid_untagged(addr, size)) {
267 return false;
268 }
269 return page_check_range((target_ulong)addr, size, type) == 0;
270 }
271
272 static inline bool access_ok(CPUState *cpu, int type,
273 abi_ulong addr, abi_ulong size)
274 {
275 return access_ok_untagged(type, cpu_untagged_addr(cpu, addr), size);
276 }
277
278 /* NOTE __get_user and __put_user use host pointers and don't check access.
279 These are usually used to access struct data members once the struct has
280 been locked - usually with lock_user_struct. */
281
282 /*
283 * Tricky points:
284 * - Use __builtin_choose_expr to avoid type promotion from ?:,
285 * - Invalid sizes result in a compile time error stemming from
286 * the fact that abort has no parameters.
287 * - It's easier to use the endian-specific unaligned load/store
288 * functions than host-endian unaligned load/store plus tswapN.
289 * - The pragmas are necessary only to silence a clang false-positive
290 * warning: see https://bugs.llvm.org/show_bug.cgi?id=39113 .
291 * - gcc has bugs in its _Pragma() support in some versions, eg
292 * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=83256 -- so we only
293 * include the warning-suppression pragmas for clang
294 */
295 #if defined(__clang__) && __has_warning("-Waddress-of-packed-member")
296 #define PRAGMA_DISABLE_PACKED_WARNING \
297 _Pragma("GCC diagnostic push"); \
298 _Pragma("GCC diagnostic ignored \"-Waddress-of-packed-member\"")
299
300 #define PRAGMA_REENABLE_PACKED_WARNING \
301 _Pragma("GCC diagnostic pop")
302
303 #else
304 #define PRAGMA_DISABLE_PACKED_WARNING
305 #define PRAGMA_REENABLE_PACKED_WARNING
306 #endif
307
308 #define __put_user_e(x, hptr, e) \
309 do { \
310 PRAGMA_DISABLE_PACKED_WARNING; \
311 (__builtin_choose_expr(sizeof(*(hptr)) == 1, stb_p, \
312 __builtin_choose_expr(sizeof(*(hptr)) == 2, stw_##e##_p, \
313 __builtin_choose_expr(sizeof(*(hptr)) == 4, stl_##e##_p, \
314 __builtin_choose_expr(sizeof(*(hptr)) == 8, stq_##e##_p, abort)))) \
315 ((hptr), (x)), (void)0); \
316 PRAGMA_REENABLE_PACKED_WARNING; \
317 } while (0)
318
319 #define __get_user_e(x, hptr, e) \
320 do { \
321 PRAGMA_DISABLE_PACKED_WARNING; \
322 ((x) = (typeof(*hptr))( \
323 __builtin_choose_expr(sizeof(*(hptr)) == 1, ldub_p, \
324 __builtin_choose_expr(sizeof(*(hptr)) == 2, lduw_##e##_p, \
325 __builtin_choose_expr(sizeof(*(hptr)) == 4, ldl_##e##_p, \
326 __builtin_choose_expr(sizeof(*(hptr)) == 8, ldq_##e##_p, abort)))) \
327 (hptr)), (void)0); \
328 PRAGMA_REENABLE_PACKED_WARNING; \
329 } while (0)
330
331
332 #ifdef TARGET_WORDS_BIGENDIAN
333 # define __put_user(x, hptr) __put_user_e(x, hptr, be)
334 # define __get_user(x, hptr) __get_user_e(x, hptr, be)
335 #else
336 # define __put_user(x, hptr) __put_user_e(x, hptr, le)
337 # define __get_user(x, hptr) __get_user_e(x, hptr, le)
338 #endif
339
340 /* put_user()/get_user() take a guest address and check access */
341 /* These are usually used to access an atomic data type, such as an int,
342 * that has been passed by address. These internally perform locking
343 * and unlocking on the data type.
344 */
345 #define put_user(x, gaddr, target_type) \
346 ({ \
347 abi_ulong __gaddr = (gaddr); \
348 target_type *__hptr; \
349 abi_long __ret = 0; \
350 if ((__hptr = lock_user(VERIFY_WRITE, __gaddr, sizeof(target_type), 0))) { \
351 __put_user((x), __hptr); \
352 unlock_user(__hptr, __gaddr, sizeof(target_type)); \
353 } else \
354 __ret = -TARGET_EFAULT; \
355 __ret; \
356 })
357
358 #define get_user(x, gaddr, target_type) \
359 ({ \
360 abi_ulong __gaddr = (gaddr); \
361 target_type *__hptr; \
362 abi_long __ret = 0; \
363 if ((__hptr = lock_user(VERIFY_READ, __gaddr, sizeof(target_type), 1))) { \
364 __get_user((x), __hptr); \
365 unlock_user(__hptr, __gaddr, 0); \
366 } else { \
367 /* avoid warning */ \
368 (x) = 0; \
369 __ret = -TARGET_EFAULT; \
370 } \
371 __ret; \
372 })
373
374 #define put_user_ual(x, gaddr) put_user((x), (gaddr), abi_ulong)
375 #define put_user_sal(x, gaddr) put_user((x), (gaddr), abi_long)
376 #define put_user_u64(x, gaddr) put_user((x), (gaddr), uint64_t)
377 #define put_user_s64(x, gaddr) put_user((x), (gaddr), int64_t)
378 #define put_user_u32(x, gaddr) put_user((x), (gaddr), uint32_t)
379 #define put_user_s32(x, gaddr) put_user((x), (gaddr), int32_t)
380 #define put_user_u16(x, gaddr) put_user((x), (gaddr), uint16_t)
381 #define put_user_s16(x, gaddr) put_user((x), (gaddr), int16_t)
382 #define put_user_u8(x, gaddr) put_user((x), (gaddr), uint8_t)
383 #define put_user_s8(x, gaddr) put_user((x), (gaddr), int8_t)
384
385 #define get_user_ual(x, gaddr) get_user((x), (gaddr), abi_ulong)
386 #define get_user_sal(x, gaddr) get_user((x), (gaddr), abi_long)
387 #define get_user_u64(x, gaddr) get_user((x), (gaddr), uint64_t)
388 #define get_user_s64(x, gaddr) get_user((x), (gaddr), int64_t)
389 #define get_user_u32(x, gaddr) get_user((x), (gaddr), uint32_t)
390 #define get_user_s32(x, gaddr) get_user((x), (gaddr), int32_t)
391 #define get_user_u16(x, gaddr) get_user((x), (gaddr), uint16_t)
392 #define get_user_s16(x, gaddr) get_user((x), (gaddr), int16_t)
393 #define get_user_u8(x, gaddr) get_user((x), (gaddr), uint8_t)
394 #define get_user_s8(x, gaddr) get_user((x), (gaddr), int8_t)
395
396 /* copy_from_user() and copy_to_user() are usually used to copy data
397 * buffers between the target and host. These internally perform
398 * locking/unlocking of the memory.
399 */
400 int copy_from_user(void *hptr, abi_ulong gaddr, ssize_t len);
401 int copy_to_user(abi_ulong gaddr, void *hptr, ssize_t len);
402
403 /* Functions for accessing guest memory. The tget and tput functions
404 read/write single values, byteswapping as necessary. The lock_user function
405 gets a pointer to a contiguous area of guest memory, but does not perform
406 any byteswapping. lock_user may return either a pointer to the guest
407 memory, or a temporary buffer. */
408
409 /* Lock an area of guest memory into the host. If copy is true then the
410 host area will have the same contents as the guest. */
411 void *lock_user(int type, abi_ulong guest_addr, ssize_t len, bool copy);
412
413 /* Unlock an area of guest memory. The first LEN bytes must be
414 flushed back to guest memory. host_ptr = NULL is explicitly
415 allowed and does nothing. */
416 #ifndef DEBUG_REMAP
417 static inline void unlock_user(void *host_ptr, abi_ulong guest_addr,
418 ssize_t len)
419 {
420 /* no-op */
421 }
422 #else
423 void unlock_user(void *host_ptr, abi_ulong guest_addr, ssize_t len);
424 #endif
425
426 /* Return the length of a string in target memory or -TARGET_EFAULT if
427 access error. */
428 ssize_t target_strlen(abi_ulong gaddr);
429
430 /* Like lock_user but for null terminated strings. */
431 void *lock_user_string(abi_ulong guest_addr);
432
433 /* Helper macros for locking/unlocking a target struct. */
434 #define lock_user_struct(type, host_ptr, guest_addr, copy) \
435 (host_ptr = lock_user(type, guest_addr, sizeof(*host_ptr), copy))
436 #define unlock_user_struct(host_ptr, guest_addr, copy) \
437 unlock_user(host_ptr, guest_addr, (copy) ? sizeof(*host_ptr) : 0)
438
439 #include <pthread.h>
440
441 static inline int is_error(abi_long ret)
442 {
443 return (abi_ulong)ret >= (abi_ulong)(-4096);
444 }
445
446 #if TARGET_ABI_BITS == 32
447 static inline uint64_t target_offset64(uint32_t word0, uint32_t word1)
448 {
449 #ifdef TARGET_WORDS_BIGENDIAN
450 return ((uint64_t)word0 << 32) | word1;
451 #else
452 return ((uint64_t)word1 << 32) | word0;
453 #endif
454 }
455 #else /* TARGET_ABI_BITS == 32 */
456 static inline uint64_t target_offset64(uint64_t word0, uint64_t word1)
457 {
458 return word0;
459 }
460 #endif /* TARGET_ABI_BITS != 32 */
461
462 void print_termios(void *arg);
463
464 /* ARM EABI and MIPS expect 64bit types aligned even on pairs or registers */
465 #ifdef TARGET_ARM
466 static inline int regpairs_aligned(void *cpu_env, int num)
467 {
468 return ((((CPUARMState *)cpu_env)->eabi) == 1) ;
469 }
470 #elif defined(TARGET_MIPS) && (TARGET_ABI_BITS == 32)
471 static inline int regpairs_aligned(void *cpu_env, int num) { return 1; }
472 #elif defined(TARGET_PPC) && !defined(TARGET_PPC64)
473 /*
474 * SysV AVI for PPC32 expects 64bit parameters to be passed on odd/even pairs
475 * of registers which translates to the same as ARM/MIPS, because we start with
476 * r3 as arg1
477 */
478 static inline int regpairs_aligned(void *cpu_env, int num) { return 1; }
479 #elif defined(TARGET_SH4)
480 /* SH4 doesn't align register pairs, except for p{read,write}64 */
481 static inline int regpairs_aligned(void *cpu_env, int num)
482 {
483 switch (num) {
484 case TARGET_NR_pread64:
485 case TARGET_NR_pwrite64:
486 return 1;
487
488 default:
489 return 0;
490 }
491 }
492 #elif defined(TARGET_XTENSA)
493 static inline int regpairs_aligned(void *cpu_env, int num) { return 1; }
494 #elif defined(TARGET_HEXAGON)
495 static inline int regpairs_aligned(void *cpu_env, int num) { return 1; }
496 #else
497 static inline int regpairs_aligned(void *cpu_env, int num) { return 0; }
498 #endif
499
500 /**
501 * preexit_cleanup: housekeeping before the guest exits
502 *
503 * env: the CPU state
504 * code: the exit code
505 */
506 void preexit_cleanup(CPUArchState *env, int code);
507
508 /*
509 * Include target-specific struct and function definitions;
510 * they may need access to the target-independent structures
511 * above, so include them last.
512 */
513 #include "target_cpu.h"
514 #include "target_structs.h"
515
516 #endif /* QEMU_H */