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