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