6 #include "exec/exec-all.h"
7 #include "exec/cpu_ldst.h"
11 #include "exec/user/abitypes.h"
13 #include "exec/user/thunk.h"
14 #include "syscall_defs.h"
15 #include "target_syscall.h"
16 #include "exec/gdbstub.h"
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.
22 #define SIGSET_T_SIZE (_NSIG / 8)
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
38 abi_ulong reserve_brk
;
40 abi_ulong start_stack
;
41 abi_ulong stack_limit
;
43 abi_ulong code_offset
;
44 abi_ulong data_offset
;
49 abi_ulong arg_strings
;
50 abi_ulong env_strings
;
51 abi_ulong file_string
;
56 /* The fields below are used in FDPIC mode. */
57 abi_ulong loadmap_addr
;
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
;
65 /* For target-specific processing of NT_GNU_PROPERTY_TYPE_0. */
75 /* Information about the current linux thread */
76 struct vm86_saved_state
{
77 uint32_t eax
; /* return code */
87 uint16_t cs
, ss
, ds
, es
, fs
, gs
;
91 #if defined(TARGET_ARM) && defined(TARGET_ABI32)
93 #include "nwfpe/fpa11.h"
96 #define MAX_SIGQUEUE_SIZE 1024
98 struct emulated_sigtable
{
99 int pending
; /* true if signal is pending */
100 target_siginfo_t info
;
104 * NOTE: we force a big alignment so that the stack stored after is
107 typedef struct TaskState
{
108 pid_t ts_tid
; /* tid (or pid) of this task */
115 #if defined(TARGET_ARM) || defined(TARGET_RISCV)
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
;
125 abi_ulong child_tidptr
;
129 #if defined(TARGET_ARM) || defined(TARGET_M68K) || defined(TARGET_RISCV)
130 /* Extra fields for semihosted binaries. */
132 abi_ulong heap_limit
;
134 abi_ulong stack_base
;
135 int used
; /* non zero if used */
136 struct image_info
*info
;
137 struct linux_binprm
*bprm
;
139 struct emulated_sigtable sync_signal
;
140 struct emulated_sigtable sigtab
[TARGET_NSIG
];
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
147 sigset_t signal_mask
;
149 * The signal mask imposed by a guest sigsuspend syscall, if we are
150 * currently in the middle of such a syscall
152 sigset_t sigsuspend_mask
;
153 /* Nonzero if we're leaving a sigsuspend and sigsuspend_mask is valid. */
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.)
165 /* This thread's sigaltstack, if it has one */
166 struct target_sigaltstack sigaltstack_used
;
167 } __attribute__((aligned(16))) TaskState
;
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
;
176 typedef struct IOCTLEntry IOCTLEntry
;
178 typedef abi_long
do_ioctl_fn(const IOCTLEntry
*ie
, uint8_t *buf_temp
,
179 int fd
, int cmd
, abi_long arg
);
183 unsigned int host_cmd
;
186 do_ioctl_fn
*do_ioctl
;
187 const argtype arg_type
[5];
190 extern IOCTLEntry ioctl_entries
[];
194 #define IOC_RW (IOC_R | IOC_W)
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.
202 int info_is_fdpic(struct image_info
*info
);
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
,
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
);
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
225 * Creates the initial guest address space in the host memory space.
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
231 * This function will not return if a valid value for guest_base
232 * cannot be chosen. On return, the executable loader can expect
234 * target_mmap(loaddr, hiaddr - loaddr, ...)
238 void probe_guest_base(const char *image_name
,
239 abi_ulong loaddr
, abi_ulong hiaddr
);
241 #include "qemu/log.h"
244 int host_to_target_waitstatus(int status
);
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
);
259 #define VERIFY_READ PAGE_READ
260 #define VERIFY_WRITE (PAGE_READ | PAGE_WRITE)
262 static inline bool access_ok_untagged(int type
, abi_ulong addr
, abi_ulong size
)
265 ? !guest_addr_valid_untagged(addr
)
266 : !guest_range_valid_untagged(addr
, size
)) {
269 return page_check_range((target_ulong
)addr
, size
, type
) == 0;
272 static inline bool access_ok(CPUState
*cpu
, int type
,
273 abi_ulong addr
, abi_ulong size
)
275 return access_ok_untagged(type
, cpu_untagged_addr(cpu
, addr
), size
);
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. */
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
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\"")
300 #define PRAGMA_REENABLE_PACKED_WARNING \
301 _Pragma("GCC diagnostic pop")
304 #define PRAGMA_DISABLE_PACKED_WARNING
305 #define PRAGMA_REENABLE_PACKED_WARNING
308 #define __put_user_e(x, hptr, e) \
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; \
319 #define __get_user_e(x, hptr, e) \
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)))) \
328 PRAGMA_REENABLE_PACKED_WARNING; \
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)
336 # define __put_user(x, hptr) __put_user_e(x, hptr, le)
337 # define __get_user(x, hptr) __get_user_e(x, hptr, le)
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.
345 #define put_user(x, gaddr, target_type) \
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)); \
354 __ret = -TARGET_EFAULT; \
358 #define get_user(x, gaddr, target_type) \
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); \
367 /* avoid warning */ \
369 __ret = -TARGET_EFAULT; \
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)
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)
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.
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
);
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. */
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
);
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. */
417 static inline void unlock_user(void *host_ptr
, abi_ulong guest_addr
,
423 void unlock_user(void *host_ptr
, abi_ulong guest_addr
, ssize_t len
);
426 /* Return the length of a string in target memory or -TARGET_EFAULT if
428 ssize_t
target_strlen(abi_ulong gaddr
);
430 /* Like lock_user but for null terminated strings. */
431 void *lock_user_string(abi_ulong guest_addr
);
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)
441 static inline int is_error(abi_long ret
)
443 return (abi_ulong
)ret
>= (abi_ulong
)(-4096);
446 #if TARGET_ABI_BITS == 32
447 static inline uint64_t target_offset64(uint32_t word0
, uint32_t word1
)
449 #ifdef TARGET_WORDS_BIGENDIAN
450 return ((uint64_t)word0
<< 32) | word1
;
452 return ((uint64_t)word1
<< 32) | word0
;
455 #else /* TARGET_ABI_BITS == 32 */
456 static inline uint64_t target_offset64(uint64_t word0
, uint64_t word1
)
460 #endif /* TARGET_ABI_BITS != 32 */
462 void print_termios(void *arg
);
464 /* ARM EABI and MIPS expect 64bit types aligned even on pairs or registers */
466 static inline int regpairs_aligned(void *cpu_env
, int num
)
468 return ((((CPUARMState
*)cpu_env
)->eabi
) == 1) ;
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)
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
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
)
484 case TARGET_NR_pread64
:
485 case TARGET_NR_pwrite64
:
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; }
497 static inline int regpairs_aligned(void *cpu_env
, int num
) { return 0; }
501 * preexit_cleanup: housekeeping before the guest exits
504 * code: the exit code
506 void preexit_cleanup(CPUArchState
*env
, int code
);
509 * Include target-specific struct and function definitions;
510 * they may need access to the target-independent structures
511 * above, so include them last.
513 #include "target_cpu.h"
514 #include "target_structs.h"