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