blob: e2e93fbd1d5d7cb8fa30cb9ca803ce1b94a1b1cf [file] [log] [blame]
bellarde88de092005-02-07 12:35:39 +00001#ifndef QEMU_H
2#define QEMU_H
bellard31e31b82003-02-18 22:55:36 +00003
bellard6180a182003-09-30 21:04:53 +00004#include "cpu.h"
Paolo Bonzinif08b6172014-03-28 19:42:10 +01005#include "exec/cpu_ldst.h"
blueswir1992f48a2007-10-14 16:27:31 +00006
balrog06177d32007-12-24 13:47:52 +00007#undef DEBUG_REMAP
balrog06177d32007-12-24 13:47:52 +00008
Paolo Bonzini022c62c2012-12-17 18:19:49 +01009#include "exec/user/abitypes.h"
blueswir1992f48a2007-10-14 16:27:31 +000010
blueswir1992f48a2007-10-14 16:27:31 +000011#include "syscall_defs.h"
LluĂ­s Vilanova460c5792016-02-01 19:38:42 +010012#include "target_syscall.h"
bellard66fb9762003-03-23 01:06:05 +000013
Peter Maydell400b7f62021-09-08 16:43:57 +010014/*
15 * This is the size of the host kernel's sigset_t, needed where we make
Peter Maydell1d48fdd2016-06-14 12:49:18 +010016 * direct system calls that take a sigset_t pointer and a size.
17 */
18#define SIGSET_T_SIZE (_NSIG / 8)
19
Peter Maydell400b7f62021-09-08 16:43:57 +010020/*
21 * This struct is used to hold certain information about the image.
bellard31e31b82003-02-18 22:55:36 +000022 * Basically, it replicates in user space what would be certain
23 * task_struct fields in the kernel
24 */
25struct image_info {
Richard Henderson9955ffa2010-07-27 10:25:30 -070026 abi_ulong load_bias;
blueswir1992f48a2007-10-14 16:27:31 +000027 abi_ulong load_addr;
28 abi_ulong start_code;
29 abi_ulong end_code;
30 abi_ulong start_data;
31 abi_ulong end_data;
32 abi_ulong start_brk;
33 abi_ulong brk;
Richard Henderson6fd59442020-01-17 13:02:45 -100034 abi_ulong reserve_brk;
blueswir1992f48a2007-10-14 16:27:31 +000035 abi_ulong start_mmap;
blueswir1992f48a2007-10-14 16:27:31 +000036 abi_ulong start_stack;
Paul Brook97374d32010-06-16 13:03:51 +010037 abi_ulong stack_limit;
blueswir1992f48a2007-10-14 16:27:31 +000038 abi_ulong entry;
39 abi_ulong code_offset;
40 abi_ulong data_offset;
Mika Westerbergedf8e2a2009-04-07 09:57:11 +030041 abi_ulong saved_auxv;
Alexander Graf125b0f52012-01-28 21:12:14 +020042 abi_ulong auxv_len;
Richard Henderson60f1c802022-04-26 19:51:29 -070043 abi_ulong argc;
44 abi_ulong argv;
45 abi_ulong envc;
46 abi_ulong envp;
Richard Henderson7c4ee5b2016-12-15 09:38:11 -080047 abi_ulong file_string;
Paul Brookd8fd2952012-03-30 18:02:50 +010048 uint32_t elf_flags;
Peter Maydell400b7f62021-09-08 16:43:57 +010049 int personality;
Laurent Vivier33143c42018-07-16 21:53:49 +020050 abi_ulong alignment;
Richard Henderson872f3d02022-08-10 23:00:52 -070051 bool exec_stack;
Christophe Lyona99856c2018-04-30 10:03:41 +020052
Richard Henderson60f1c802022-04-26 19:51:29 -070053 /* Generic semihosting knows about these pointers. */
54 abi_ulong arg_strings; /* strings for argv */
55 abi_ulong env_strings; /* strings for envp; ends arg_strings */
56
Christophe Lyona99856c2018-04-30 10:03:41 +020057 /* The fields below are used in FDPIC mode. */
Mike Frysinger1af02e82011-02-07 01:05:50 -050058 abi_ulong loadmap_addr;
59 uint16_t nsegs;
Peter Maydell400b7f62021-09-08 16:43:57 +010060 void *loadsegs;
Mike Frysinger1af02e82011-02-07 01:05:50 -050061 abi_ulong pt_dynamic_addr;
Christophe Lyon3cb10cf2018-04-30 10:03:43 +020062 abi_ulong interpreter_loadmap_addr;
63 abi_ulong interpreter_pt_dynamic_addr;
Mike Frysinger1af02e82011-02-07 01:05:50 -050064 struct image_info *other_info;
Richard Henderson83f990e2020-10-21 10:37:47 -070065
66 /* For target-specific processing of NT_GNU_PROPERTY_TYPE_0. */
67 uint32_t note_flags;
68
Stefan Markovic74cfc702018-10-22 16:43:40 +020069#ifdef TARGET_MIPS
70 int fp_abi;
71 int interp_fp_abi;
72#endif
bellard31e31b82003-02-18 22:55:36 +000073};
74
bellardb346ff42003-06-15 20:05:50 +000075#ifdef TARGET_I386
bellard851e67a2003-03-29 16:53:14 +000076/* Information about the current linux thread */
77struct vm86_saved_state {
78 uint32_t eax; /* return code */
79 uint32_t ebx;
80 uint32_t ecx;
81 uint32_t edx;
82 uint32_t esi;
83 uint32_t edi;
84 uint32_t ebp;
85 uint32_t esp;
86 uint32_t eflags;
87 uint32_t eip;
88 uint16_t cs, ss, ds, es, fs, gs;
89};
bellardb346ff42003-06-15 20:05:50 +000090#endif
bellard851e67a2003-03-29 16:53:14 +000091
Peter Maydell848d72c2013-09-03 20:12:17 +010092#if defined(TARGET_ARM) && defined(TARGET_ABI32)
bellard28c4f362004-02-16 21:47:43 +000093/* FPU emulator */
94#include "nwfpe/fpa11.h"
bellard28c4f362004-02-16 21:47:43 +000095#endif
96
pbrook624f7972008-05-31 16:11:38 +000097struct emulated_sigtable {
98 int pending; /* true if signal is pending */
Timothy E Baldwin907f5fd2016-05-27 15:51:52 +010099 target_siginfo_t info;
pbrook624f7972008-05-31 16:11:38 +0000100};
101
bellard851e67a2003-03-29 16:53:14 +0000102typedef struct TaskState {
Mika Westerbergedf8e2a2009-04-07 09:57:11 +0300103 pid_t ts_tid; /* tid (or pid) of this task */
bellard28c4f362004-02-16 21:47:43 +0000104#ifdef TARGET_ARM
Peter Maydell848d72c2013-09-03 20:12:17 +0100105# ifdef TARGET_ABI32
bellard28c4f362004-02-16 21:47:43 +0000106 /* FPA state */
107 FPA11 fpa;
Peter Maydell848d72c2013-09-03 20:12:17 +0100108# endif
Keith Packarda10b9d92021-01-08 22:42:52 +0000109#endif
110#if defined(TARGET_ARM) || defined(TARGET_RISCV)
bellarda4f81972005-04-23 18:25:41 +0000111 int swi_errno;
bellard28c4f362004-02-16 21:47:43 +0000112#endif
j_mayer84409dd2007-04-06 08:56:50 +0000113#if defined(TARGET_I386) && !defined(TARGET_X86_64)
blueswir1992f48a2007-10-14 16:27:31 +0000114 abi_ulong target_v86;
bellard851e67a2003-03-29 16:53:14 +0000115 struct vm86_saved_state vm86_saved_regs;
bellardb333af02003-05-14 21:48:51 +0000116 struct target_vm86plus_struct vm86plus;
bellard631271d2003-05-10 13:14:52 +0000117 uint32_t v86flags;
118 uint32_t v86mask;
bellardb346ff42003-06-15 20:05:50 +0000119#endif
pbrookc2764712009-03-07 15:24:59 +0000120 abi_ulong child_tidptr;
pbrooke6e59062006-10-22 00:18:54 +0000121#ifdef TARGET_M68K
Peter Maydell1ccd9372013-07-16 18:44:55 +0100122 abi_ulong tp_value;
pbrooke6e59062006-10-22 00:18:54 +0000123#endif
Keith Packarda10b9d92021-01-08 22:42:52 +0000124#if defined(TARGET_ARM) || defined(TARGET_M68K) || defined(TARGET_RISCV)
pbrooka87295e2007-05-26 15:09:38 +0000125 /* Extra fields for semihosted binaries. */
Peter Maydelld3170912016-07-04 13:06:35 +0100126 abi_ulong heap_base;
127 abi_ulong heap_limit;
pbrooka87295e2007-05-26 15:09:38 +0000128#endif
Peter Maydelld3170912016-07-04 13:06:35 +0100129 abi_ulong stack_base;
bellard851e67a2003-03-29 16:53:14 +0000130 int used; /* non zero if used */
pbrook978efd62006-06-17 18:30:42 +0000131 struct image_info *info;
Mika Westerbergedf8e2a2009-04-07 09:57:11 +0300132 struct linux_binprm *bprm;
pbrook624f7972008-05-31 16:11:38 +0000133
Timothy E Baldwin655ed672016-05-27 15:51:53 +0100134 struct emulated_sigtable sync_signal;
pbrook624f7972008-05-31 16:11:38 +0000135 struct emulated_sigtable sigtab[TARGET_NSIG];
Peter Maydell400b7f62021-09-08 16:43:57 +0100136 /*
137 * This thread's signal mask, as requested by the guest program.
Peter Maydell3d3efba2016-05-27 15:51:49 +0100138 * The actual signal mask of this thread may differ:
139 * + we don't let SIGSEGV and SIGBUS be blocked while running guest code
140 * + sometimes we block all signals to avoid races
141 */
142 sigset_t signal_mask;
Peter Maydell400b7f62021-09-08 16:43:57 +0100143 /*
144 * The signal mask imposed by a guest sigsuspend syscall, if we are
Peter Maydell3d3efba2016-05-27 15:51:49 +0100145 * currently in the middle of such a syscall
146 */
147 sigset_t sigsuspend_mask;
148 /* Nonzero if we're leaving a sigsuspend and sigsuspend_mask is valid. */
149 int in_sigsuspend;
150
Peter Maydell400b7f62021-09-08 16:43:57 +0100151 /*
152 * Nonzero if process_pending_signals() needs to do something (either
Peter Maydell3d3efba2016-05-27 15:51:49 +0100153 * handle a pending signal or unblock signals).
154 * This flag is written from a signal handler so should be accessed via
Stefan Hajnoczid73415a2020-09-23 11:56:46 +0100155 * the qatomic_read() and qatomic_set() functions. (It is not accessed
Peter Maydell3d3efba2016-05-27 15:51:49 +0100156 * from multiple threads.)
157 */
158 int signal_pending;
159
Peter Maydell5bfce0b2019-07-25 14:16:45 +0100160 /* This thread's sigaltstack, if it has one */
161 struct target_sigaltstack sigaltstack_used;
Cameron Esfahanieb33cda2022-01-27 16:12:51 -0800162
163 /* Start time of task after system boot in clock ticks */
164 uint64_t start_boottime;
Peter Maydellb1b21382022-01-14 15:37:30 +0000165} TaskState;
bellard851e67a2003-03-29 16:53:14 +0000166
Peter Maydell3b249d22021-09-08 16:44:03 +0100167abi_long do_brk(abi_ulong new_brk);
bellard631271d2003-05-10 13:14:52 +0000168
bellardedf779f2004-02-22 13:40:13 +0000169/* user access */
170
Richard Henderson68f77662021-02-12 10:48:38 -0800171#define VERIFY_READ PAGE_READ
172#define VERIFY_WRITE (PAGE_READ | PAGE_WRITE)
bellardedf779f2004-02-22 13:40:13 +0000173
Richard Hendersonc7169b02021-02-12 10:48:47 -0800174static inline bool access_ok_untagged(int type, abi_ulong addr, abi_ulong size)
bellarddae32702007-11-14 10:51:00 +0000175{
Richard Henderson114556c2021-02-12 10:48:45 -0800176 if (size == 0
Richard Henderson46b12f42021-02-12 10:48:46 -0800177 ? !guest_addr_valid_untagged(addr)
178 : !guest_range_valid_untagged(addr, size)) {
Richard Henderson4feac832021-02-12 10:48:37 -0800179 return false;
180 }
Richard Henderson68f77662021-02-12 10:48:38 -0800181 return page_check_range((target_ulong)addr, size, type) == 0;
bellarddae32702007-11-14 10:51:00 +0000182}
bellardedf779f2004-02-22 13:40:13 +0000183
Richard Hendersonc7169b02021-02-12 10:48:47 -0800184static inline bool access_ok(CPUState *cpu, int type,
185 abi_ulong addr, abi_ulong size)
186{
187 return access_ok_untagged(type, cpu_untagged_addr(cpu, addr), size);
188}
189
Richard Henderson658f2dc2013-01-04 16:39:31 -0800190/* NOTE __get_user and __put_user use host pointers and don't check access.
191 These are usually used to access struct data members once the struct has
192 been locked - usually with lock_user_struct. */
bellardedf779f2004-02-22 13:40:13 +0000193
Peter Maydell850d5e32018-10-09 17:18:14 +0100194/*
195 * Tricky points:
196 * - Use __builtin_choose_expr to avoid type promotion from ?:,
197 * - Invalid sizes result in a compile time error stemming from
198 * the fact that abort has no parameters.
199 * - It's easier to use the endian-specific unaligned load/store
200 * functions than host-endian unaligned load/store plus tswapN.
201 * - The pragmas are necessary only to silence a clang false-positive
202 * warning: see https://bugs.llvm.org/show_bug.cgi?id=39113 .
Peter Maydell850d5e32018-10-09 17:18:14 +0100203 * - gcc has bugs in its _Pragma() support in some versions, eg
204 * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=83256 -- so we only
205 * include the warning-suppression pragmas for clang
206 */
Thomas Huth798b8582018-11-30 09:23:16 +0100207#if defined(__clang__) && __has_warning("-Waddress-of-packed-member")
Peter Maydell850d5e32018-10-09 17:18:14 +0100208#define PRAGMA_DISABLE_PACKED_WARNING \
209 _Pragma("GCC diagnostic push"); \
Peter Maydell850d5e32018-10-09 17:18:14 +0100210 _Pragma("GCC diagnostic ignored \"-Waddress-of-packed-member\"")
Richard Henderson658f2dc2013-01-04 16:39:31 -0800211
Peter Maydell850d5e32018-10-09 17:18:14 +0100212#define PRAGMA_REENABLE_PACKED_WARNING \
213 _Pragma("GCC diagnostic pop")
Richard Henderson658f2dc2013-01-04 16:39:31 -0800214
Peter Maydell850d5e32018-10-09 17:18:14 +0100215#else
216#define PRAGMA_DISABLE_PACKED_WARNING
217#define PRAGMA_REENABLE_PACKED_WARNING
218#endif
219
220#define __put_user_e(x, hptr, e) \
221 do { \
222 PRAGMA_DISABLE_PACKED_WARNING; \
223 (__builtin_choose_expr(sizeof(*(hptr)) == 1, stb_p, \
224 __builtin_choose_expr(sizeof(*(hptr)) == 2, stw_##e##_p, \
225 __builtin_choose_expr(sizeof(*(hptr)) == 4, stl_##e##_p, \
226 __builtin_choose_expr(sizeof(*(hptr)) == 8, stq_##e##_p, abort)))) \
227 ((hptr), (x)), (void)0); \
228 PRAGMA_REENABLE_PACKED_WARNING; \
229 } while (0)
230
231#define __get_user_e(x, hptr, e) \
232 do { \
233 PRAGMA_DISABLE_PACKED_WARNING; \
234 ((x) = (typeof(*hptr))( \
235 __builtin_choose_expr(sizeof(*(hptr)) == 1, ldub_p, \
236 __builtin_choose_expr(sizeof(*(hptr)) == 2, lduw_##e##_p, \
237 __builtin_choose_expr(sizeof(*(hptr)) == 4, ldl_##e##_p, \
238 __builtin_choose_expr(sizeof(*(hptr)) == 8, ldq_##e##_p, abort)))) \
239 (hptr)), (void)0); \
240 PRAGMA_REENABLE_PACKED_WARNING; \
241 } while (0)
242
Richard Henderson658f2dc2013-01-04 16:39:31 -0800243
Marc-André Lureauee3eb3a2022-03-23 19:57:18 +0400244#if TARGET_BIG_ENDIAN
Richard Henderson658f2dc2013-01-04 16:39:31 -0800245# define __put_user(x, hptr) __put_user_e(x, hptr, be)
246# define __get_user(x, hptr) __get_user_e(x, hptr, be)
247#else
248# define __put_user(x, hptr) __put_user_e(x, hptr, le)
249# define __get_user(x, hptr) __get_user_e(x, hptr, le)
250#endif
bellardedf779f2004-02-22 13:40:13 +0000251
bellard579a97f2007-11-11 14:26:47 +0000252/* put_user()/get_user() take a guest address and check access */
253/* These are usually used to access an atomic data type, such as an int,
254 * that has been passed by address. These internally perform locking
255 * and unlocking on the data type.
256 */
257#define put_user(x, gaddr, target_type) \
258({ \
259 abi_ulong __gaddr = (gaddr); \
260 target_type *__hptr; \
Riku Voipioa42267e2014-04-22 15:40:50 +0300261 abi_long __ret = 0; \
bellard579a97f2007-11-11 14:26:47 +0000262 if ((__hptr = lock_user(VERIFY_WRITE, __gaddr, sizeof(target_type), 0))) { \
Riku Voipioa42267e2014-04-22 15:40:50 +0300263 __put_user((x), __hptr); \
bellard579a97f2007-11-11 14:26:47 +0000264 unlock_user(__hptr, __gaddr, sizeof(target_type)); \
265 } else \
266 __ret = -TARGET_EFAULT; \
267 __ret; \
bellardedf779f2004-02-22 13:40:13 +0000268})
269
bellard579a97f2007-11-11 14:26:47 +0000270#define get_user(x, gaddr, target_type) \
271({ \
272 abi_ulong __gaddr = (gaddr); \
273 target_type *__hptr; \
Riku Voipioa42267e2014-04-22 15:40:50 +0300274 abi_long __ret = 0; \
bellard579a97f2007-11-11 14:26:47 +0000275 if ((__hptr = lock_user(VERIFY_READ, __gaddr, sizeof(target_type), 1))) { \
Riku Voipioa42267e2014-04-22 15:40:50 +0300276 __get_user((x), __hptr); \
bellard579a97f2007-11-11 14:26:47 +0000277 unlock_user(__hptr, __gaddr, 0); \
bellard2f619692007-11-16 10:46:05 +0000278 } else { \
279 /* avoid warning */ \
280 (x) = 0; \
bellard579a97f2007-11-11 14:26:47 +0000281 __ret = -TARGET_EFAULT; \
bellard2f619692007-11-16 10:46:05 +0000282 } \
bellard579a97f2007-11-11 14:26:47 +0000283 __ret; \
bellardedf779f2004-02-22 13:40:13 +0000284})
285
bellard2f619692007-11-16 10:46:05 +0000286#define put_user_ual(x, gaddr) put_user((x), (gaddr), abi_ulong)
287#define put_user_sal(x, gaddr) put_user((x), (gaddr), abi_long)
288#define put_user_u64(x, gaddr) put_user((x), (gaddr), uint64_t)
289#define put_user_s64(x, gaddr) put_user((x), (gaddr), int64_t)
290#define put_user_u32(x, gaddr) put_user((x), (gaddr), uint32_t)
291#define put_user_s32(x, gaddr) put_user((x), (gaddr), int32_t)
292#define put_user_u16(x, gaddr) put_user((x), (gaddr), uint16_t)
293#define put_user_s16(x, gaddr) put_user((x), (gaddr), int16_t)
294#define put_user_u8(x, gaddr) put_user((x), (gaddr), uint8_t)
295#define put_user_s8(x, gaddr) put_user((x), (gaddr), int8_t)
296
297#define get_user_ual(x, gaddr) get_user((x), (gaddr), abi_ulong)
298#define get_user_sal(x, gaddr) get_user((x), (gaddr), abi_long)
299#define get_user_u64(x, gaddr) get_user((x), (gaddr), uint64_t)
300#define get_user_s64(x, gaddr) get_user((x), (gaddr), int64_t)
301#define get_user_u32(x, gaddr) get_user((x), (gaddr), uint32_t)
302#define get_user_s32(x, gaddr) get_user((x), (gaddr), int32_t)
303#define get_user_u16(x, gaddr) get_user((x), (gaddr), uint16_t)
304#define get_user_s16(x, gaddr) get_user((x), (gaddr), int16_t)
305#define get_user_u8(x, gaddr) get_user((x), (gaddr), uint8_t)
306#define get_user_s8(x, gaddr) get_user((x), (gaddr), int8_t)
307
bellard579a97f2007-11-11 14:26:47 +0000308/* copy_from_user() and copy_to_user() are usually used to copy data
309 * buffers between the target and host. These internally perform
310 * locking/unlocking of the memory.
311 */
Richard Henderson360f0ab2021-03-15 14:40:04 -0600312int copy_from_user(void *hptr, abi_ulong gaddr, ssize_t len);
313int copy_to_user(abi_ulong gaddr, void *hptr, ssize_t len);
bellard579a97f2007-11-11 14:26:47 +0000314
pbrook53a59602006-03-25 19:31:22 +0000315/* Functions for accessing guest memory. The tget and tput functions
Stefan Weil6f20f552013-09-12 19:57:15 +0200316 read/write single values, byteswapping as necessary. The lock_user function
pbrook53a59602006-03-25 19:31:22 +0000317 gets a pointer to a contiguous area of guest memory, but does not perform
Stefan Weil6f20f552013-09-12 19:57:15 +0200318 any byteswapping. lock_user may return either a pointer to the guest
pbrook53a59602006-03-25 19:31:22 +0000319 memory, or a temporary buffer. */
320
321/* Lock an area of guest memory into the host. If copy is true then the
322 host area will have the same contents as the guest. */
Richard Henderson360f0ab2021-03-15 14:40:04 -0600323void *lock_user(int type, abi_ulong guest_addr, ssize_t len, bool copy);
bellardedf779f2004-02-22 13:40:13 +0000324
bellard579a97f2007-11-11 14:26:47 +0000325/* Unlock an area of guest memory. The first LEN bytes must be
ths1235fc02008-06-03 19:51:57 +0000326 flushed back to guest memory. host_ptr = NULL is explicitly
bellard579a97f2007-11-11 14:26:47 +0000327 allowed and does nothing. */
Richard Henderson687ca792021-02-12 10:48:48 -0800328#ifndef DEBUG_REMAP
Richard Henderson360f0ab2021-03-15 14:40:04 -0600329static inline void unlock_user(void *host_ptr, abi_ulong guest_addr,
330 ssize_t len)
331{
332 /* no-op */
333}
Richard Henderson687ca792021-02-12 10:48:48 -0800334#else
Richard Henderson360f0ab2021-03-15 14:40:04 -0600335void unlock_user(void *host_ptr, abi_ulong guest_addr, ssize_t len);
pbrook53a59602006-03-25 19:31:22 +0000336#endif
bellardedf779f2004-02-22 13:40:13 +0000337
bellard579a97f2007-11-11 14:26:47 +0000338/* Return the length of a string in target memory or -TARGET_EFAULT if
339 access error. */
Richard Henderson09f679b2021-02-12 10:48:49 -0800340ssize_t target_strlen(abi_ulong gaddr);
bellardedf779f2004-02-22 13:40:13 +0000341
pbrook53a59602006-03-25 19:31:22 +0000342/* Like lock_user but for null terminated strings. */
Richard Henderson687ca792021-02-12 10:48:48 -0800343void *lock_user_string(abi_ulong guest_addr);
pbrook53a59602006-03-25 19:31:22 +0000344
Stefan Weil41d1af42013-09-12 19:57:41 +0200345/* Helper macros for locking/unlocking a target struct. */
bellard579a97f2007-11-11 14:26:47 +0000346#define lock_user_struct(type, host_ptr, guest_addr, copy) \
347 (host_ptr = lock_user(type, guest_addr, sizeof(*host_ptr), copy))
348#define unlock_user_struct(host_ptr, guest_addr, copy) \
pbrook53a59602006-03-25 19:31:22 +0000349 unlock_user(host_ptr, guest_addr, (copy) ? sizeof(*host_ptr) : 0)
350
bellarde88de092005-02-07 12:35:39 +0000351#endif /* QEMU_H */