]> git.ipfire.org Git - thirdparty/qemu.git/blame - linux-user/qemu.h
meson: Make DEBUG_REMAP a meson option
[thirdparty/qemu.git] / linux-user / qemu.h
CommitLineData
e88de099
FB
1#ifndef QEMU_H
2#define QEMU_H
31e31b8a 3
6180a181 4#include "cpu.h"
f08b6170 5#include "exec/cpu_ldst.h"
992f48a0 6
022c62cb 7#include "exec/user/abitypes.h"
992f48a0 8
992f48a0 9#include "syscall_defs.h"
460c579f 10#include "target_syscall.h"
66fb9763 11
400b7f6d
PM
12/*
13 * This is the size of the host kernel's sigset_t, needed where we make
1d48fdd9
PM
14 * direct system calls that take a sigset_t pointer and a size.
15 */
16#define SIGSET_T_SIZE (_NSIG / 8)
17
400b7f6d
PM
18/*
19 * This struct is used to hold certain information about the image.
31e31b8a
FB
20 * Basically, it replicates in user space what would be certain
21 * task_struct fields in the kernel
22 */
23struct image_info {
9955ffac 24 abi_ulong load_bias;
992f48a0
BS
25 abi_ulong load_addr;
26 abi_ulong start_code;
27 abi_ulong end_code;
28 abi_ulong start_data;
29 abi_ulong end_data;
992f48a0 30 abi_ulong brk;
992f48a0 31 abi_ulong start_stack;
97374d38 32 abi_ulong stack_limit;
5d94c2ff 33 abi_ulong vdso;
992f48a0
BS
34 abi_ulong entry;
35 abi_ulong code_offset;
36 abi_ulong data_offset;
edf8e2af 37 abi_ulong saved_auxv;
125b0f55 38 abi_ulong auxv_len;
60f1c801
RH
39 abi_ulong argc;
40 abi_ulong argv;
41 abi_ulong envc;
42 abi_ulong envp;
7c4ee5bc 43 abi_ulong file_string;
d8fd2954 44 uint32_t elf_flags;
400b7f6d 45 int personality;
33143c44 46 abi_ulong alignment;
872f3d04 47 bool exec_stack;
a99856cd 48
60f1c801
RH
49 /* Generic semihosting knows about these pointers. */
50 abi_ulong arg_strings; /* strings for argv */
51 abi_ulong env_strings; /* strings for envp; ends arg_strings */
52
a99856cd 53 /* The fields below are used in FDPIC mode. */
1af02e83
MF
54 abi_ulong loadmap_addr;
55 uint16_t nsegs;
400b7f6d 56 void *loadsegs;
1af02e83 57 abi_ulong pt_dynamic_addr;
3cb10cfa
CL
58 abi_ulong interpreter_loadmap_addr;
59 abi_ulong interpreter_pt_dynamic_addr;
1af02e83 60 struct image_info *other_info;
83f990eb
RH
61
62 /* For target-specific processing of NT_GNU_PROPERTY_TYPE_0. */
63 uint32_t note_flags;
64
74cfc704
SM
65#ifdef TARGET_MIPS
66 int fp_abi;
67 int interp_fp_abi;
68#endif
31e31b8a
FB
69};
70
b346ff46 71#ifdef TARGET_I386
851e67a1
FB
72/* Information about the current linux thread */
73struct vm86_saved_state {
74 uint32_t eax; /* return code */
75 uint32_t ebx;
76 uint32_t ecx;
77 uint32_t edx;
78 uint32_t esi;
79 uint32_t edi;
80 uint32_t ebp;
81 uint32_t esp;
82 uint32_t eflags;
83 uint32_t eip;
84 uint16_t cs, ss, ds, es, fs, gs;
85};
b346ff46 86#endif
851e67a1 87
848d72cd 88#if defined(TARGET_ARM) && defined(TARGET_ABI32)
28c4f361
FB
89/* FPU emulator */
90#include "nwfpe/fpa11.h"
28c4f361
FB
91#endif
92
624f7979
PB
93struct emulated_sigtable {
94 int pending; /* true if signal is pending */
907f5fdd 95 target_siginfo_t info;
624f7979
PB
96};
97
851e67a1 98typedef struct TaskState {
edf8e2af 99 pid_t ts_tid; /* tid (or pid) of this task */
28c4f361 100#ifdef TARGET_ARM
848d72cd 101# ifdef TARGET_ABI32
28c4f361
FB
102 /* FPA state */
103 FPA11 fpa;
848d72cd 104# endif
a10b9d93
KP
105#endif
106#if defined(TARGET_ARM) || defined(TARGET_RISCV)
a4f81979 107 int swi_errno;
28c4f361 108#endif
84409ddb 109#if defined(TARGET_I386) && !defined(TARGET_X86_64)
992f48a0 110 abi_ulong target_v86;
851e67a1 111 struct vm86_saved_state vm86_saved_regs;
b333af06 112 struct target_vm86plus_struct vm86plus;
631271d7
FB
113 uint32_t v86flags;
114 uint32_t v86mask;
e6e5906b 115#endif
c2764719 116 abi_ulong child_tidptr;
e6e5906b 117#ifdef TARGET_M68K
1ccd9374 118 abi_ulong tp_value;
a87295e8 119#endif
a10b9d93 120#if defined(TARGET_ARM) || defined(TARGET_M68K) || defined(TARGET_RISCV)
a87295e8 121 /* Extra fields for semihosted binaries. */
d317091d
PM
122 abi_ulong heap_base;
123 abi_ulong heap_limit;
b346ff46 124#endif
d317091d 125 abi_ulong stack_base;
851e67a1 126 int used; /* non zero if used */
978efd6a 127 struct image_info *info;
edf8e2af 128 struct linux_binprm *bprm;
624f7979 129
655ed67c 130 struct emulated_sigtable sync_signal;
624f7979 131 struct emulated_sigtable sigtab[TARGET_NSIG];
400b7f6d
PM
132 /*
133 * This thread's signal mask, as requested by the guest program.
3d3efba0
PM
134 * The actual signal mask of this thread may differ:
135 * + we don't let SIGSEGV and SIGBUS be blocked while running guest code
136 * + sometimes we block all signals to avoid races
137 */
138 sigset_t signal_mask;
400b7f6d
PM
139 /*
140 * The signal mask imposed by a guest sigsuspend syscall, if we are
3d3efba0
PM
141 * currently in the middle of such a syscall
142 */
143 sigset_t sigsuspend_mask;
144 /* Nonzero if we're leaving a sigsuspend and sigsuspend_mask is valid. */
145 int in_sigsuspend;
146
400b7f6d
PM
147 /*
148 * Nonzero if process_pending_signals() needs to do something (either
3d3efba0
PM
149 * handle a pending signal or unblock signals).
150 * This flag is written from a signal handler so should be accessed via
d73415a3 151 * the qatomic_read() and qatomic_set() functions. (It is not accessed
3d3efba0
PM
152 * from multiple threads.)
153 */
154 int signal_pending;
155
5bfce0b7
PM
156 /* This thread's sigaltstack, if it has one */
157 struct target_sigaltstack sigaltstack_used;
eb33cdae
CE
158
159 /* Start time of task after system boot in clock ticks */
160 uint64_t start_boottime;
b1b21387 161} TaskState;
851e67a1 162
e4e5cb4a
IL
163static inline TaskState *get_task_state(CPUState *cs)
164{
165 return cs->opaque;
166}
167
3b249d26 168abi_long do_brk(abi_ulong new_brk);
a4dab0a0 169int do_guest_openat(CPUArchState *cpu_env, int dirfd, const char *pathname,
35be898e 170 int flags, mode_t mode, bool safe);
a4dab0a0 171ssize_t do_guest_readlink(const char *pathname, char *buf, size_t bufsiz);
631271d7 172
edf779ff
FB
173/* user access */
174
64d06015 175#define VERIFY_NONE 0
68f77666
RH
176#define VERIFY_READ PAGE_READ
177#define VERIFY_WRITE (PAGE_READ | PAGE_WRITE)
edf779ff 178
c7169b02 179static inline bool access_ok_untagged(int type, abi_ulong addr, abi_ulong size)
dae3270c 180{
114556c5 181 if (size == 0
46b12f46
RH
182 ? !guest_addr_valid_untagged(addr)
183 : !guest_range_valid_untagged(addr, size)) {
4feac83a
RH
184 return false;
185 }
bef6f008 186 return page_check_range((target_ulong)addr, size, type);
dae3270c 187}
edf779ff 188
c7169b02
RH
189static inline bool access_ok(CPUState *cpu, int type,
190 abi_ulong addr, abi_ulong size)
191{
192 return access_ok_untagged(type, cpu_untagged_addr(cpu, addr), size);
193}
194
658f2dc9
RH
195/* NOTE __get_user and __put_user use host pointers and don't check access.
196 These are usually used to access struct data members once the struct has
197 been locked - usually with lock_user_struct. */
198
850d5e33
PM
199/*
200 * Tricky points:
201 * - Use __builtin_choose_expr to avoid type promotion from ?:,
202 * - Invalid sizes result in a compile time error stemming from
203 * the fact that abort has no parameters.
204 * - It's easier to use the endian-specific unaligned load/store
205 * functions than host-endian unaligned load/store plus tswapN.
206 * - The pragmas are necessary only to silence a clang false-positive
207 * warning: see https://bugs.llvm.org/show_bug.cgi?id=39113 .
850d5e33
PM
208 * - gcc has bugs in its _Pragma() support in some versions, eg
209 * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=83256 -- so we only
210 * include the warning-suppression pragmas for clang
211 */
798b8581 212#if defined(__clang__) && __has_warning("-Waddress-of-packed-member")
850d5e33
PM
213#define PRAGMA_DISABLE_PACKED_WARNING \
214 _Pragma("GCC diagnostic push"); \
850d5e33
PM
215 _Pragma("GCC diagnostic ignored \"-Waddress-of-packed-member\"")
216
217#define PRAGMA_REENABLE_PACKED_WARNING \
218 _Pragma("GCC diagnostic pop")
219
220#else
221#define PRAGMA_DISABLE_PACKED_WARNING
222#define PRAGMA_REENABLE_PACKED_WARNING
223#endif
224
225#define __put_user_e(x, hptr, e) \
226 do { \
227 PRAGMA_DISABLE_PACKED_WARNING; \
228 (__builtin_choose_expr(sizeof(*(hptr)) == 1, stb_p, \
229 __builtin_choose_expr(sizeof(*(hptr)) == 2, stw_##e##_p, \
230 __builtin_choose_expr(sizeof(*(hptr)) == 4, stl_##e##_p, \
231 __builtin_choose_expr(sizeof(*(hptr)) == 8, stq_##e##_p, abort)))) \
232 ((hptr), (x)), (void)0); \
233 PRAGMA_REENABLE_PACKED_WARNING; \
234 } while (0)
235
236#define __get_user_e(x, hptr, e) \
237 do { \
238 PRAGMA_DISABLE_PACKED_WARNING; \
239 ((x) = (typeof(*hptr))( \
240 __builtin_choose_expr(sizeof(*(hptr)) == 1, ldub_p, \
241 __builtin_choose_expr(sizeof(*(hptr)) == 2, lduw_##e##_p, \
242 __builtin_choose_expr(sizeof(*(hptr)) == 4, ldl_##e##_p, \
243 __builtin_choose_expr(sizeof(*(hptr)) == 8, ldq_##e##_p, abort)))) \
244 (hptr)), (void)0); \
245 PRAGMA_REENABLE_PACKED_WARNING; \
246 } while (0)
247
658f2dc9 248
ee3eb3a7 249#if TARGET_BIG_ENDIAN
658f2dc9
RH
250# define __put_user(x, hptr) __put_user_e(x, hptr, be)
251# define __get_user(x, hptr) __get_user_e(x, hptr, be)
252#else
253# define __put_user(x, hptr) __put_user_e(x, hptr, le)
254# define __get_user(x, hptr) __get_user_e(x, hptr, le)
255#endif
edf779ff 256
579a97f7
FB
257/* put_user()/get_user() take a guest address and check access */
258/* These are usually used to access an atomic data type, such as an int,
259 * that has been passed by address. These internally perform locking
260 * and unlocking on the data type.
261 */
262#define put_user(x, gaddr, target_type) \
263({ \
264 abi_ulong __gaddr = (gaddr); \
265 target_type *__hptr; \
a42267ef 266 abi_long __ret = 0; \
579a97f7 267 if ((__hptr = lock_user(VERIFY_WRITE, __gaddr, sizeof(target_type), 0))) { \
a42267ef 268 __put_user((x), __hptr); \
579a97f7
FB
269 unlock_user(__hptr, __gaddr, sizeof(target_type)); \
270 } else \
271 __ret = -TARGET_EFAULT; \
272 __ret; \
edf779ff
FB
273})
274
579a97f7
FB
275#define get_user(x, gaddr, target_type) \
276({ \
277 abi_ulong __gaddr = (gaddr); \
278 target_type *__hptr; \
a42267ef 279 abi_long __ret = 0; \
579a97f7 280 if ((__hptr = lock_user(VERIFY_READ, __gaddr, sizeof(target_type), 1))) { \
a42267ef 281 __get_user((x), __hptr); \
579a97f7 282 unlock_user(__hptr, __gaddr, 0); \
2f619698
FB
283 } else { \
284 /* avoid warning */ \
285 (x) = 0; \
579a97f7 286 __ret = -TARGET_EFAULT; \
2f619698 287 } \
579a97f7 288 __ret; \
edf779ff
FB
289})
290
2f619698
FB
291#define put_user_ual(x, gaddr) put_user((x), (gaddr), abi_ulong)
292#define put_user_sal(x, gaddr) put_user((x), (gaddr), abi_long)
293#define put_user_u64(x, gaddr) put_user((x), (gaddr), uint64_t)
294#define put_user_s64(x, gaddr) put_user((x), (gaddr), int64_t)
295#define put_user_u32(x, gaddr) put_user((x), (gaddr), uint32_t)
296#define put_user_s32(x, gaddr) put_user((x), (gaddr), int32_t)
297#define put_user_u16(x, gaddr) put_user((x), (gaddr), uint16_t)
298#define put_user_s16(x, gaddr) put_user((x), (gaddr), int16_t)
299#define put_user_u8(x, gaddr) put_user((x), (gaddr), uint8_t)
300#define put_user_s8(x, gaddr) put_user((x), (gaddr), int8_t)
301
302#define get_user_ual(x, gaddr) get_user((x), (gaddr), abi_ulong)
303#define get_user_sal(x, gaddr) get_user((x), (gaddr), abi_long)
304#define get_user_u64(x, gaddr) get_user((x), (gaddr), uint64_t)
305#define get_user_s64(x, gaddr) get_user((x), (gaddr), int64_t)
306#define get_user_u32(x, gaddr) get_user((x), (gaddr), uint32_t)
307#define get_user_s32(x, gaddr) get_user((x), (gaddr), int32_t)
308#define get_user_u16(x, gaddr) get_user((x), (gaddr), uint16_t)
309#define get_user_s16(x, gaddr) get_user((x), (gaddr), int16_t)
310#define get_user_u8(x, gaddr) get_user((x), (gaddr), uint8_t)
311#define get_user_s8(x, gaddr) get_user((x), (gaddr), int8_t)
312
579a97f7
FB
313/* copy_from_user() and copy_to_user() are usually used to copy data
314 * buffers between the target and host. These internally perform
315 * locking/unlocking of the memory.
316 */
360f0abd
RH
317int copy_from_user(void *hptr, abi_ulong gaddr, ssize_t len);
318int copy_to_user(abi_ulong gaddr, void *hptr, ssize_t len);
579a97f7 319
53a5960a 320/* Functions for accessing guest memory. The tget and tput functions
6f20f55b 321 read/write single values, byteswapping as necessary. The lock_user function
53a5960a 322 gets a pointer to a contiguous area of guest memory, but does not perform
6f20f55b 323 any byteswapping. lock_user may return either a pointer to the guest
53a5960a
PB
324 memory, or a temporary buffer. */
325
326/* Lock an area of guest memory into the host. If copy is true then the
327 host area will have the same contents as the guest. */
360f0abd 328void *lock_user(int type, abi_ulong guest_addr, ssize_t len, bool copy);
edf779ff 329
579a97f7 330/* Unlock an area of guest memory. The first LEN bytes must be
1235fc06 331 flushed back to guest memory. host_ptr = NULL is explicitly
579a97f7 332 allowed and does nothing. */
1f2355f5 333#ifndef CONFIG_DEBUG_REMAP
360f0abd
RH
334static inline void unlock_user(void *host_ptr, abi_ulong guest_addr,
335 ssize_t len)
336{
337 /* no-op */
338}
687ca797 339#else
360f0abd 340void unlock_user(void *host_ptr, abi_ulong guest_addr, ssize_t len);
53a5960a 341#endif
edf779ff 342
579a97f7
FB
343/* Return the length of a string in target memory or -TARGET_EFAULT if
344 access error. */
09f679b6 345ssize_t target_strlen(abi_ulong gaddr);
53a5960a
PB
346
347/* Like lock_user but for null terminated strings. */
687ca797 348void *lock_user_string(abi_ulong guest_addr);
edf779ff 349
41d1af4d 350/* Helper macros for locking/unlocking a target struct. */
579a97f7
FB
351#define lock_user_struct(type, host_ptr, guest_addr, copy) \
352 (host_ptr = lock_user(type, guest_addr, sizeof(*host_ptr), copy))
353#define unlock_user_struct(host_ptr, guest_addr, copy) \
53a5960a
PB
354 unlock_user(host_ptr, guest_addr, (copy) ? sizeof(*host_ptr) : 0)
355
e88de099 356#endif /* QEMU_H */