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
34 abi_ulong reserve_brk
;
36 abi_ulong start_stack
;
37 abi_ulong stack_limit
;
39 abi_ulong code_offset
;
40 abi_ulong data_offset
;
45 abi_ulong arg_strings
;
46 abi_ulong env_strings
;
47 abi_ulong file_string
;
52 /* The fields below are used in FDPIC mode. */
53 abi_ulong loadmap_addr
;
56 abi_ulong pt_dynamic_addr
;
57 abi_ulong interpreter_loadmap_addr
;
58 abi_ulong interpreter_pt_dynamic_addr
;
59 struct image_info
*other_info
;
61 /* For target-specific processing of NT_GNU_PROPERTY_TYPE_0. */
71 /* Information about the current linux thread */
72 struct vm86_saved_state
{
73 uint32_t eax
; /* return code */
83 uint16_t cs
, ss
, ds
, es
, fs
, gs
;
87 #if defined(TARGET_ARM) && defined(TARGET_ABI32)
89 #include "nwfpe/fpa11.h"
92 struct emulated_sigtable
{
93 int pending
; /* true if signal is pending */
94 target_siginfo_t info
;
97 typedef struct TaskState
{
98 pid_t ts_tid
; /* tid (or pid) of this task */
105 #if defined(TARGET_ARM) || defined(TARGET_RISCV)
108 #if defined(TARGET_I386) && !defined(TARGET_X86_64)
109 abi_ulong target_v86
;
110 struct vm86_saved_state vm86_saved_regs
;
111 struct target_vm86plus_struct vm86plus
;
115 abi_ulong child_tidptr
;
119 #if defined(TARGET_ARM) || defined(TARGET_M68K) || defined(TARGET_RISCV)
120 /* Extra fields for semihosted binaries. */
122 abi_ulong heap_limit
;
124 abi_ulong stack_base
;
125 int used
; /* non zero if used */
126 struct image_info
*info
;
127 struct linux_binprm
*bprm
;
129 struct emulated_sigtable sync_signal
;
130 struct emulated_sigtable sigtab
[TARGET_NSIG
];
132 * This thread's signal mask, as requested by the guest program.
133 * The actual signal mask of this thread may differ:
134 * + we don't let SIGSEGV and SIGBUS be blocked while running guest code
135 * + sometimes we block all signals to avoid races
137 sigset_t signal_mask
;
139 * The signal mask imposed by a guest sigsuspend syscall, if we are
140 * currently in the middle of such a syscall
142 sigset_t sigsuspend_mask
;
143 /* Nonzero if we're leaving a sigsuspend and sigsuspend_mask is valid. */
147 * Nonzero if process_pending_signals() needs to do something (either
148 * handle a pending signal or unblock signals).
149 * This flag is written from a signal handler so should be accessed via
150 * the qatomic_read() and qatomic_set() functions. (It is not accessed
151 * from multiple threads.)
155 /* This thread's sigaltstack, if it has one */
156 struct target_sigaltstack sigaltstack_used
;
158 /* Start time of task after system boot in clock ticks */
159 uint64_t start_boottime
;
162 abi_long
do_brk(abi_ulong new_brk
);
166 #define VERIFY_READ PAGE_READ
167 #define VERIFY_WRITE (PAGE_READ | PAGE_WRITE)
169 static inline bool access_ok_untagged(int type
, abi_ulong addr
, abi_ulong size
)
172 ? !guest_addr_valid_untagged(addr
)
173 : !guest_range_valid_untagged(addr
, size
)) {
176 return page_check_range((target_ulong
)addr
, size
, type
) == 0;
179 static inline bool access_ok(CPUState
*cpu
, int type
,
180 abi_ulong addr
, abi_ulong size
)
182 return access_ok_untagged(type
, cpu_untagged_addr(cpu
, addr
), size
);
185 /* NOTE __get_user and __put_user use host pointers and don't check access.
186 These are usually used to access struct data members once the struct has
187 been locked - usually with lock_user_struct. */
191 * - Use __builtin_choose_expr to avoid type promotion from ?:,
192 * - Invalid sizes result in a compile time error stemming from
193 * the fact that abort has no parameters.
194 * - It's easier to use the endian-specific unaligned load/store
195 * functions than host-endian unaligned load/store plus tswapN.
196 * - The pragmas are necessary only to silence a clang false-positive
197 * warning: see https://bugs.llvm.org/show_bug.cgi?id=39113 .
198 * - gcc has bugs in its _Pragma() support in some versions, eg
199 * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=83256 -- so we only
200 * include the warning-suppression pragmas for clang
202 #if defined(__clang__) && __has_warning("-Waddress-of-packed-member")
203 #define PRAGMA_DISABLE_PACKED_WARNING \
204 _Pragma("GCC diagnostic push"); \
205 _Pragma("GCC diagnostic ignored \"-Waddress-of-packed-member\"")
207 #define PRAGMA_REENABLE_PACKED_WARNING \
208 _Pragma("GCC diagnostic pop")
211 #define PRAGMA_DISABLE_PACKED_WARNING
212 #define PRAGMA_REENABLE_PACKED_WARNING
215 #define __put_user_e(x, hptr, e) \
217 PRAGMA_DISABLE_PACKED_WARNING; \
218 (__builtin_choose_expr(sizeof(*(hptr)) == 1, stb_p, \
219 __builtin_choose_expr(sizeof(*(hptr)) == 2, stw_##e##_p, \
220 __builtin_choose_expr(sizeof(*(hptr)) == 4, stl_##e##_p, \
221 __builtin_choose_expr(sizeof(*(hptr)) == 8, stq_##e##_p, abort)))) \
222 ((hptr), (x)), (void)0); \
223 PRAGMA_REENABLE_PACKED_WARNING; \
226 #define __get_user_e(x, hptr, e) \
228 PRAGMA_DISABLE_PACKED_WARNING; \
229 ((x) = (typeof(*hptr))( \
230 __builtin_choose_expr(sizeof(*(hptr)) == 1, ldub_p, \
231 __builtin_choose_expr(sizeof(*(hptr)) == 2, lduw_##e##_p, \
232 __builtin_choose_expr(sizeof(*(hptr)) == 4, ldl_##e##_p, \
233 __builtin_choose_expr(sizeof(*(hptr)) == 8, ldq_##e##_p, abort)))) \
235 PRAGMA_REENABLE_PACKED_WARNING; \
239 #if TARGET_BIG_ENDIAN
240 # define __put_user(x, hptr) __put_user_e(x, hptr, be)
241 # define __get_user(x, hptr) __get_user_e(x, hptr, be)
243 # define __put_user(x, hptr) __put_user_e(x, hptr, le)
244 # define __get_user(x, hptr) __get_user_e(x, hptr, le)
247 /* put_user()/get_user() take a guest address and check access */
248 /* These are usually used to access an atomic data type, such as an int,
249 * that has been passed by address. These internally perform locking
250 * and unlocking on the data type.
252 #define put_user(x, gaddr, target_type) \
254 abi_ulong __gaddr = (gaddr); \
255 target_type *__hptr; \
256 abi_long __ret = 0; \
257 if ((__hptr = lock_user(VERIFY_WRITE, __gaddr, sizeof(target_type), 0))) { \
258 __put_user((x), __hptr); \
259 unlock_user(__hptr, __gaddr, sizeof(target_type)); \
261 __ret = -TARGET_EFAULT; \
265 #define get_user(x, gaddr, target_type) \
267 abi_ulong __gaddr = (gaddr); \
268 target_type *__hptr; \
269 abi_long __ret = 0; \
270 if ((__hptr = lock_user(VERIFY_READ, __gaddr, sizeof(target_type), 1))) { \
271 __get_user((x), __hptr); \
272 unlock_user(__hptr, __gaddr, 0); \
274 /* avoid warning */ \
276 __ret = -TARGET_EFAULT; \
281 #define put_user_ual(x, gaddr) put_user((x), (gaddr), abi_ulong)
282 #define put_user_sal(x, gaddr) put_user((x), (gaddr), abi_long)
283 #define put_user_u64(x, gaddr) put_user((x), (gaddr), uint64_t)
284 #define put_user_s64(x, gaddr) put_user((x), (gaddr), int64_t)
285 #define put_user_u32(x, gaddr) put_user((x), (gaddr), uint32_t)
286 #define put_user_s32(x, gaddr) put_user((x), (gaddr), int32_t)
287 #define put_user_u16(x, gaddr) put_user((x), (gaddr), uint16_t)
288 #define put_user_s16(x, gaddr) put_user((x), (gaddr), int16_t)
289 #define put_user_u8(x, gaddr) put_user((x), (gaddr), uint8_t)
290 #define put_user_s8(x, gaddr) put_user((x), (gaddr), int8_t)
292 #define get_user_ual(x, gaddr) get_user((x), (gaddr), abi_ulong)
293 #define get_user_sal(x, gaddr) get_user((x), (gaddr), abi_long)
294 #define get_user_u64(x, gaddr) get_user((x), (gaddr), uint64_t)
295 #define get_user_s64(x, gaddr) get_user((x), (gaddr), int64_t)
296 #define get_user_u32(x, gaddr) get_user((x), (gaddr), uint32_t)
297 #define get_user_s32(x, gaddr) get_user((x), (gaddr), int32_t)
298 #define get_user_u16(x, gaddr) get_user((x), (gaddr), uint16_t)
299 #define get_user_s16(x, gaddr) get_user((x), (gaddr), int16_t)
300 #define get_user_u8(x, gaddr) get_user((x), (gaddr), uint8_t)
301 #define get_user_s8(x, gaddr) get_user((x), (gaddr), int8_t)
303 /* copy_from_user() and copy_to_user() are usually used to copy data
304 * buffers between the target and host. These internally perform
305 * locking/unlocking of the memory.
307 int copy_from_user(void *hptr
, abi_ulong gaddr
, ssize_t len
);
308 int copy_to_user(abi_ulong gaddr
, void *hptr
, ssize_t len
);
310 /* Functions for accessing guest memory. The tget and tput functions
311 read/write single values, byteswapping as necessary. The lock_user function
312 gets a pointer to a contiguous area of guest memory, but does not perform
313 any byteswapping. lock_user may return either a pointer to the guest
314 memory, or a temporary buffer. */
316 /* Lock an area of guest memory into the host. If copy is true then the
317 host area will have the same contents as the guest. */
318 void *lock_user(int type
, abi_ulong guest_addr
, ssize_t len
, bool copy
);
320 /* Unlock an area of guest memory. The first LEN bytes must be
321 flushed back to guest memory. host_ptr = NULL is explicitly
322 allowed and does nothing. */
324 static inline void unlock_user(void *host_ptr
, abi_ulong guest_addr
,
330 void unlock_user(void *host_ptr
, abi_ulong guest_addr
, ssize_t len
);
333 /* Return the length of a string in target memory or -TARGET_EFAULT if
335 ssize_t
target_strlen(abi_ulong gaddr
);
337 /* Like lock_user but for null terminated strings. */
338 void *lock_user_string(abi_ulong guest_addr
);
340 /* Helper macros for locking/unlocking a target struct. */
341 #define lock_user_struct(type, host_ptr, guest_addr, copy) \
342 (host_ptr = lock_user(type, guest_addr, sizeof(*host_ptr), copy))
343 #define unlock_user_struct(host_ptr, guest_addr, copy) \
344 unlock_user(host_ptr, guest_addr, (copy) ? sizeof(*host_ptr) : 0)