5 #include "exec/cpu_ldst.h"
9 #include "exec/user/abitypes.h"
11 #include "syscall_defs.h"
12 #include "target_syscall.h"
15 * This is the size of the host kernel's sigset_t, needed where we make
16 * direct system calls that take a sigset_t pointer and a size.
18 #define SIGSET_T_SIZE (_NSIG / 8)
21 * This struct is used to hold certain information about the image.
22 * Basically, it replicates in user space what would be certain
23 * task_struct fields in the kernel
33 abi_ulong start_stack
;
34 abi_ulong stack_limit
;
37 abi_ulong code_offset
;
38 abi_ulong data_offset
;
45 abi_ulong file_string
;
51 /* Generic semihosting knows about these pointers. */
52 abi_ulong arg_strings
; /* strings for argv */
53 abi_ulong env_strings
; /* strings for envp; ends arg_strings */
55 /* The fields below are used in FDPIC mode. */
56 abi_ulong loadmap_addr
;
59 abi_ulong pt_dynamic_addr
;
60 abi_ulong interpreter_loadmap_addr
;
61 abi_ulong interpreter_pt_dynamic_addr
;
62 struct image_info
*other_info
;
64 /* For target-specific processing of NT_GNU_PROPERTY_TYPE_0. */
74 /* Information about the current linux thread */
75 struct vm86_saved_state
{
76 uint32_t eax
; /* return code */
86 uint16_t cs
, ss
, ds
, es
, fs
, gs
;
90 #if defined(TARGET_ARM) && defined(TARGET_ABI32)
92 #include "nwfpe/fpa11.h"
95 struct emulated_sigtable
{
96 int pending
; /* true if signal is pending */
97 target_siginfo_t info
;
100 typedef struct TaskState
{
101 pid_t ts_tid
; /* tid (or pid) of this task */
108 #if defined(TARGET_ARM) || defined(TARGET_RISCV)
111 #if defined(TARGET_I386) && !defined(TARGET_X86_64)
112 abi_ulong target_v86
;
113 struct vm86_saved_state vm86_saved_regs
;
114 struct target_vm86plus_struct vm86plus
;
118 abi_ulong child_tidptr
;
122 #if defined(TARGET_ARM) || defined(TARGET_M68K) || defined(TARGET_RISCV)
123 /* Extra fields for semihosted binaries. */
125 abi_ulong heap_limit
;
127 abi_ulong stack_base
;
128 int used
; /* non zero if used */
129 struct image_info
*info
;
130 struct linux_binprm
*bprm
;
132 struct emulated_sigtable sync_signal
;
133 struct emulated_sigtable sigtab
[TARGET_NSIG
];
135 * This thread's signal mask, as requested by the guest program.
136 * The actual signal mask of this thread may differ:
137 * + we don't let SIGSEGV and SIGBUS be blocked while running guest code
138 * + sometimes we block all signals to avoid races
140 sigset_t signal_mask
;
142 * The signal mask imposed by a guest sigsuspend syscall, if we are
143 * currently in the middle of such a syscall
145 sigset_t sigsuspend_mask
;
146 /* Nonzero if we're leaving a sigsuspend and sigsuspend_mask is valid. */
150 * Nonzero if process_pending_signals() needs to do something (either
151 * handle a pending signal or unblock signals).
152 * This flag is written from a signal handler so should be accessed via
153 * the qatomic_read() and qatomic_set() functions. (It is not accessed
154 * from multiple threads.)
158 /* This thread's sigaltstack, if it has one */
159 struct target_sigaltstack sigaltstack_used
;
161 /* Start time of task after system boot in clock ticks */
162 uint64_t start_boottime
;
165 static inline TaskState
*get_task_state(CPUState
*cs
)
170 abi_long
do_brk(abi_ulong new_brk
);
171 int do_guest_openat(CPUArchState
*cpu_env
, int dirfd
, const char *pathname
,
172 int flags
, mode_t mode
, bool safe
);
173 ssize_t
do_guest_readlink(const char *pathname
, char *buf
, size_t bufsiz
);
177 #define VERIFY_NONE 0
178 #define VERIFY_READ PAGE_READ
179 #define VERIFY_WRITE (PAGE_READ | PAGE_WRITE)
181 static inline bool access_ok_untagged(int type
, abi_ulong addr
, abi_ulong size
)
184 ? !guest_addr_valid_untagged(addr
)
185 : !guest_range_valid_untagged(addr
, size
)) {
188 return page_check_range((target_ulong
)addr
, size
, type
);
191 static inline bool access_ok(CPUState
*cpu
, int type
,
192 abi_ulong addr
, abi_ulong size
)
194 return access_ok_untagged(type
, cpu_untagged_addr(cpu
, addr
), size
);
197 /* NOTE __get_user and __put_user use host pointers and don't check access.
198 These are usually used to access struct data members once the struct has
199 been locked - usually with lock_user_struct. */
203 * - Use __builtin_choose_expr to avoid type promotion from ?:,
204 * - Invalid sizes result in a compile time error stemming from
205 * the fact that abort has no parameters.
206 * - It's easier to use the endian-specific unaligned load/store
207 * functions than host-endian unaligned load/store plus tswapN.
208 * - The pragmas are necessary only to silence a clang false-positive
209 * warning: see https://bugs.llvm.org/show_bug.cgi?id=39113 .
210 * - gcc has bugs in its _Pragma() support in some versions, eg
211 * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=83256 -- so we only
212 * include the warning-suppression pragmas for clang
214 #if defined(__clang__) && __has_warning("-Waddress-of-packed-member")
215 #define PRAGMA_DISABLE_PACKED_WARNING \
216 _Pragma("GCC diagnostic push"); \
217 _Pragma("GCC diagnostic ignored \"-Waddress-of-packed-member\"")
219 #define PRAGMA_REENABLE_PACKED_WARNING \
220 _Pragma("GCC diagnostic pop")
223 #define PRAGMA_DISABLE_PACKED_WARNING
224 #define PRAGMA_REENABLE_PACKED_WARNING
227 #define __put_user_e(x, hptr, e) \
229 PRAGMA_DISABLE_PACKED_WARNING; \
230 (__builtin_choose_expr(sizeof(*(hptr)) == 1, stb_p, \
231 __builtin_choose_expr(sizeof(*(hptr)) == 2, stw_##e##_p, \
232 __builtin_choose_expr(sizeof(*(hptr)) == 4, stl_##e##_p, \
233 __builtin_choose_expr(sizeof(*(hptr)) == 8, stq_##e##_p, abort)))) \
234 ((hptr), (x)), (void)0); \
235 PRAGMA_REENABLE_PACKED_WARNING; \
238 #define __get_user_e(x, hptr, e) \
240 PRAGMA_DISABLE_PACKED_WARNING; \
241 ((x) = (typeof(*hptr))( \
242 __builtin_choose_expr(sizeof(*(hptr)) == 1, ldub_p, \
243 __builtin_choose_expr(sizeof(*(hptr)) == 2, lduw_##e##_p, \
244 __builtin_choose_expr(sizeof(*(hptr)) == 4, ldl_##e##_p, \
245 __builtin_choose_expr(sizeof(*(hptr)) == 8, ldq_##e##_p, abort)))) \
247 PRAGMA_REENABLE_PACKED_WARNING; \
251 #if TARGET_BIG_ENDIAN
252 # define __put_user(x, hptr) __put_user_e(x, hptr, be)
253 # define __get_user(x, hptr) __get_user_e(x, hptr, be)
255 # define __put_user(x, hptr) __put_user_e(x, hptr, le)
256 # define __get_user(x, hptr) __get_user_e(x, hptr, le)
259 /* put_user()/get_user() take a guest address and check access */
260 /* These are usually used to access an atomic data type, such as an int,
261 * that has been passed by address. These internally perform locking
262 * and unlocking on the data type.
264 #define put_user(x, gaddr, target_type) \
266 abi_ulong __gaddr = (gaddr); \
267 target_type *__hptr; \
268 abi_long __ret = 0; \
269 if ((__hptr = lock_user(VERIFY_WRITE, __gaddr, sizeof(target_type), 0))) { \
270 __put_user((x), __hptr); \
271 unlock_user(__hptr, __gaddr, sizeof(target_type)); \
273 __ret = -TARGET_EFAULT; \
277 #define get_user(x, gaddr, target_type) \
279 abi_ulong __gaddr = (gaddr); \
280 target_type *__hptr; \
281 abi_long __ret = 0; \
282 if ((__hptr = lock_user(VERIFY_READ, __gaddr, sizeof(target_type), 1))) { \
283 __get_user((x), __hptr); \
284 unlock_user(__hptr, __gaddr, 0); \
286 /* avoid warning */ \
288 __ret = -TARGET_EFAULT; \
293 #define put_user_ual(x, gaddr) put_user((x), (gaddr), abi_ulong)
294 #define put_user_sal(x, gaddr) put_user((x), (gaddr), abi_long)
295 #define put_user_u64(x, gaddr) put_user((x), (gaddr), uint64_t)
296 #define put_user_s64(x, gaddr) put_user((x), (gaddr), int64_t)
297 #define put_user_u32(x, gaddr) put_user((x), (gaddr), uint32_t)
298 #define put_user_s32(x, gaddr) put_user((x), (gaddr), int32_t)
299 #define put_user_u16(x, gaddr) put_user((x), (gaddr), uint16_t)
300 #define put_user_s16(x, gaddr) put_user((x), (gaddr), int16_t)
301 #define put_user_u8(x, gaddr) put_user((x), (gaddr), uint8_t)
302 #define put_user_s8(x, gaddr) put_user((x), (gaddr), int8_t)
304 #define get_user_ual(x, gaddr) get_user((x), (gaddr), abi_ulong)
305 #define get_user_sal(x, gaddr) get_user((x), (gaddr), abi_long)
306 #define get_user_u64(x, gaddr) get_user((x), (gaddr), uint64_t)
307 #define get_user_s64(x, gaddr) get_user((x), (gaddr), int64_t)
308 #define get_user_u32(x, gaddr) get_user((x), (gaddr), uint32_t)
309 #define get_user_s32(x, gaddr) get_user((x), (gaddr), int32_t)
310 #define get_user_u16(x, gaddr) get_user((x), (gaddr), uint16_t)
311 #define get_user_s16(x, gaddr) get_user((x), (gaddr), int16_t)
312 #define get_user_u8(x, gaddr) get_user((x), (gaddr), uint8_t)
313 #define get_user_s8(x, gaddr) get_user((x), (gaddr), int8_t)
315 /* copy_from_user() and copy_to_user() are usually used to copy data
316 * buffers between the target and host. These internally perform
317 * locking/unlocking of the memory.
319 int copy_from_user(void *hptr
, abi_ulong gaddr
, ssize_t len
);
320 int copy_to_user(abi_ulong gaddr
, void *hptr
, ssize_t len
);
322 /* Functions for accessing guest memory. The tget and tput functions
323 read/write single values, byteswapping as necessary. The lock_user function
324 gets a pointer to a contiguous area of guest memory, but does not perform
325 any byteswapping. lock_user may return either a pointer to the guest
326 memory, or a temporary buffer. */
328 /* Lock an area of guest memory into the host. If copy is true then the
329 host area will have the same contents as the guest. */
330 void *lock_user(int type
, abi_ulong guest_addr
, ssize_t len
, bool copy
);
332 /* Unlock an area of guest memory. The first LEN bytes must be
333 flushed back to guest memory. host_ptr = NULL is explicitly
334 allowed and does nothing. */
336 static inline void unlock_user(void *host_ptr
, abi_ulong guest_addr
,
342 void unlock_user(void *host_ptr
, abi_ulong guest_addr
, ssize_t len
);
345 /* Return the length of a string in target memory or -TARGET_EFAULT if
347 ssize_t
target_strlen(abi_ulong gaddr
);
349 /* Like lock_user but for null terminated strings. */
350 void *lock_user_string(abi_ulong guest_addr
);
352 /* Helper macros for locking/unlocking a target struct. */
353 #define lock_user_struct(type, host_ptr, guest_addr, copy) \
354 (host_ptr = lock_user(type, guest_addr, sizeof(*host_ptr), copy))
355 #define unlock_user_struct(host_ptr, guest_addr, copy) \
356 unlock_user(host_ptr, guest_addr, (copy) ? sizeof(*host_ptr) : 0)