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