xref: /openbmc/qemu/bsd-user/qemu.h (revision 0274bd7b)
1 /*
2  *  qemu bsd user mode definition
3  *
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; either version 2 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
16  */
17 #ifndef QEMU_H
18 #define QEMU_H
19 
20 #include "cpu.h"
21 #include "qemu/units.h"
22 #include "exec/cpu_ldst.h"
23 #include "exec/exec-all.h"
24 
25 #undef DEBUG_REMAP
26 
27 #include "exec/user/abitypes.h"
28 
29 extern char **environ;
30 
31 #include "exec/user/thunk.h"
32 #include "target_arch.h"
33 #include "syscall_defs.h"
34 #include "target_syscall.h"
35 #include "target_os_vmparam.h"
36 #include "target_os_signal.h"
37 #include "target.h"
38 #include "exec/gdbstub.h"
39 #include "qemu/clang-tsa.h"
40 
41 #include "qemu-os.h"
42 /*
43  * This struct is used to hold certain information about the image.  Basically,
44  * it replicates in user space what would be certain task_struct fields in the
45  * kernel
46  */
47 struct image_info {
48     abi_ulong load_bias;
49     abi_ulong load_addr;
50     abi_ulong start_code;
51     abi_ulong end_code;
52     abi_ulong start_data;
53     abi_ulong end_data;
54     abi_ulong brk;
55     abi_ulong rss;
56     abi_ulong start_stack;
57     abi_ulong entry;
58     abi_ulong code_offset;
59     abi_ulong data_offset;
60     abi_ulong arg_start;
61     abi_ulong arg_end;
62     uint32_t  elf_flags;
63 };
64 
65 struct emulated_sigtable {
66     int pending; /* true if signal is pending */
67     target_siginfo_t info;
68 };
69 
70 /*
71  * NOTE: we force a big alignment so that the stack stored after is aligned too
72  */
73 typedef struct TaskState {
74     pid_t ts_tid;     /* tid (or pid) of this task */
75 
76     struct TaskState *next;
77     struct bsd_binprm *bprm;
78     struct image_info *info;
79 
80     struct emulated_sigtable sync_signal;
81     /*
82      * TODO: Since we block all signals while returning to the main CPU
83      * loop, this needn't be an array
84      */
85     struct emulated_sigtable sigtab[TARGET_NSIG];
86     /*
87      * Nonzero if process_pending_signals() needs to do something (either
88      * handle a pending signal or unblock signals).
89      * This flag is written from a signal handler so should be accessed via
90      * the qatomic_read() and qatomic_set() functions. (It is not accessed
91      * from multiple threads.)
92      */
93     int signal_pending;
94     /* True if we're leaving a sigsuspend and sigsuspend_mask is valid. */
95     bool in_sigsuspend;
96     /*
97      * This thread's signal mask, as requested by the guest program.
98      * The actual signal mask of this thread may differ:
99      *  + we don't let SIGSEGV and SIGBUS be blocked while running guest code
100      *  + sometimes we block all signals to avoid races
101      */
102     sigset_t signal_mask;
103     /*
104      * The signal mask imposed by a guest sigsuspend syscall, if we are
105      * currently in the middle of such a syscall
106      */
107     sigset_t sigsuspend_mask;
108 
109     /* This thread's sigaltstack, if it has one */
110     struct target_sigaltstack sigaltstack_used;
111 } __attribute__((aligned(16))) TaskState;
112 
113 void stop_all_tasks(void);
114 extern const char *qemu_uname_release;
115 
116 /*
117  * TARGET_ARG_MAX defines the number of bytes allocated for arguments
118  * and envelope for the new program. 256k should suffice for a reasonable
119  * maxiumum env+arg in 32-bit environments, bump it up to 512k for !ILP32
120  * platforms.
121  */
122 #if TARGET_ABI_BITS > 32
123 #define TARGET_ARG_MAX (512 * KiB)
124 #else
125 #define TARGET_ARG_MAX (256 * KiB)
126 #endif
127 #define MAX_ARG_PAGES (TARGET_ARG_MAX / TARGET_PAGE_SIZE)
128 
129 /*
130  * This structure is used to hold the arguments that are
131  * used when loading binaries.
132  */
133 struct bsd_binprm {
134         char buf[128];
135         void *page[MAX_ARG_PAGES];
136         abi_ulong p;
137         abi_ulong stringp;
138         int fd;
139         int e_uid, e_gid;
140         int argc, envc;
141         char **argv;
142         char **envp;
143         char *filename;         /* (Given) Name of binary */
144         char *fullpath;         /* Full path of binary */
145         int (*core_dump)(int, CPUArchState *);
146 };
147 
148 void do_init_thread(struct target_pt_regs *regs, struct image_info *infop);
149 abi_ulong loader_build_argptr(int envc, int argc, abi_ulong sp,
150                               abi_ulong stringp);
151 int loader_exec(const char *filename, char **argv, char **envp,
152                 struct target_pt_regs *regs, struct image_info *infop,
153                 struct bsd_binprm *bprm);
154 
155 int load_elf_binary(struct bsd_binprm *bprm, struct target_pt_regs *regs,
156                     struct image_info *info);
157 int load_flt_binary(struct bsd_binprm *bprm, struct target_pt_regs *regs,
158                     struct image_info *info);
159 int is_target_elf_binary(int fd);
160 
161 abi_long memcpy_to_target(abi_ulong dest, const void *src,
162                           unsigned long len);
163 void target_set_brk(abi_ulong new_brk);
164 abi_long do_brk(abi_ulong new_brk);
165 void syscall_init(void);
166 abi_long do_freebsd_syscall(void *cpu_env, int num, abi_long arg1,
167                             abi_long arg2, abi_long arg3, abi_long arg4,
168                             abi_long arg5, abi_long arg6, abi_long arg7,
169                             abi_long arg8);
170 abi_long do_netbsd_syscall(void *cpu_env, int num, abi_long arg1,
171                            abi_long arg2, abi_long arg3, abi_long arg4,
172                            abi_long arg5, abi_long arg6);
173 abi_long do_openbsd_syscall(void *cpu_env, int num, abi_long arg1,
174                             abi_long arg2, abi_long arg3, abi_long arg4,
175                             abi_long arg5, abi_long arg6);
176 void gemu_log(const char *fmt, ...) G_GNUC_PRINTF(1, 2);
177 extern __thread CPUState *thread_cpu;
178 void cpu_loop(CPUArchState *env);
179 char *target_strerror(int err);
180 int get_osversion(void);
181 void fork_start(void);
182 void fork_end(int child);
183 
184 #include "qemu/log.h"
185 
186 /* strace.c */
187 struct syscallname {
188     int nr;
189     const char *name;
190     const char *format;
191     void (*call)(const struct syscallname *,
192                  abi_long, abi_long, abi_long,
193                  abi_long, abi_long, abi_long);
194     void (*result)(const struct syscallname *, abi_long);
195 };
196 
197 void
198 print_freebsd_syscall(int num,
199                       abi_long arg1, abi_long arg2, abi_long arg3,
200                       abi_long arg4, abi_long arg5, abi_long arg6);
201 void print_freebsd_syscall_ret(int num, abi_long ret);
202 void
203 print_netbsd_syscall(int num,
204                      abi_long arg1, abi_long arg2, abi_long arg3,
205                      abi_long arg4, abi_long arg5, abi_long arg6);
206 void print_netbsd_syscall_ret(int num, abi_long ret);
207 void
208 print_openbsd_syscall(int num,
209                       abi_long arg1, abi_long arg2, abi_long arg3,
210                       abi_long arg4, abi_long arg5, abi_long arg6);
211 void print_openbsd_syscall_ret(int num, abi_long ret);
212 /**
213  * print_taken_signal:
214  * @target_signum: target signal being taken
215  * @tinfo: target_siginfo_t which will be passed to the guest for the signal
216  *
217  * Print strace output indicating that this signal is being taken by the guest,
218  * in a format similar to:
219  * --- SIGSEGV {si_signo=SIGSEGV, si_code=SI_KERNEL, si_addr=0} ---
220  */
221 void print_taken_signal(int target_signum, const target_siginfo_t *tinfo);
222 extern int do_strace;
223 
224 /* mmap.c */
225 int target_mprotect(abi_ulong start, abi_ulong len, int prot);
226 abi_long target_mmap(abi_ulong start, abi_ulong len, int prot,
227                      int flags, int fd, off_t offset);
228 int target_munmap(abi_ulong start, abi_ulong len);
229 abi_long target_mremap(abi_ulong old_addr, abi_ulong old_size,
230                        abi_ulong new_size, unsigned long flags,
231                        abi_ulong new_addr);
232 int target_msync(abi_ulong start, abi_ulong len, int flags);
233 extern abi_ulong mmap_next_start;
234 abi_ulong mmap_find_vma(abi_ulong start, abi_ulong size);
235 void TSA_NO_TSA mmap_fork_start(void);
236 void TSA_NO_TSA mmap_fork_end(int child);
237 
238 /* main.c */
239 extern char qemu_proc_pathname[];
240 extern unsigned long target_maxtsiz;
241 extern unsigned long target_dfldsiz;
242 extern unsigned long target_maxdsiz;
243 extern unsigned long target_dflssiz;
244 extern unsigned long target_maxssiz;
245 extern unsigned long target_sgrowsiz;
246 
247 /* os-syscall.c */
248 abi_long get_errno(abi_long ret);
249 bool is_error(abi_long ret);
250 int host_to_target_errno(int err);
251 
252 /* os-sys.c */
253 abi_long do_freebsd_sysctl(CPUArchState *env, abi_ulong namep, int32_t namelen,
254         abi_ulong oldp, abi_ulong oldlenp, abi_ulong newp, abi_ulong newlen);
255 abi_long do_freebsd_sysctlbyname(CPUArchState *env, abi_ulong namep,
256         int32_t namelen, abi_ulong oldp, abi_ulong oldlenp, abi_ulong newp,
257         abi_ulong newlen);
258 abi_long do_freebsd_sysarch(void *cpu_env, abi_long arg1, abi_long arg2);
259 
260 /* user access */
261 
262 #define VERIFY_READ  PAGE_READ
263 #define VERIFY_WRITE (PAGE_READ | PAGE_WRITE)
264 
265 static inline bool access_ok(int type, abi_ulong addr, abi_ulong size)
266 {
267     return page_check_range((target_ulong)addr, size, type);
268 }
269 
270 /*
271  * NOTE __get_user and __put_user use host pointers and don't check access.
272  *
273  * These are usually used to access struct data members once the struct has been
274  * locked - usually with lock_user_struct().
275  */
276 
277 /*
278  * Tricky points:
279  * - Use __builtin_choose_expr to avoid type promotion from ?:,
280  * - Invalid sizes result in a compile time error stemming from
281  *   the fact that abort has no parameters.
282  * - It's easier to use the endian-specific unaligned load/store
283  *   functions than host-endian unaligned load/store plus tswapN.
284  * - The pragmas are necessary only to silence a clang false-positive
285  *   warning: see https://bugs.llvm.org/show_bug.cgi?id=39113 .
286  * - gcc has bugs in its _Pragma() support in some versions, eg
287  *   https://gcc.gnu.org/bugzilla/show_bug.cgi?id=83256 -- so we only
288  *   include the warning-suppression pragmas for clang
289  */
290 #if defined(__clang__) && __has_warning("-Waddress-of-packed-member")
291 #define PRAGMA_DISABLE_PACKED_WARNING                                   \
292     _Pragma("GCC diagnostic push");                                     \
293     _Pragma("GCC diagnostic ignored \"-Waddress-of-packed-member\"")
294 
295 #define PRAGMA_REENABLE_PACKED_WARNING          \
296     _Pragma("GCC diagnostic pop")
297 
298 #else
299 #define PRAGMA_DISABLE_PACKED_WARNING
300 #define PRAGMA_REENABLE_PACKED_WARNING
301 #endif
302 
303 #define __put_user_e(x, hptr, e)                                            \
304     do {                                                                    \
305         PRAGMA_DISABLE_PACKED_WARNING;                                      \
306         (__builtin_choose_expr(sizeof(*(hptr)) == 1, stb_p,                 \
307         __builtin_choose_expr(sizeof(*(hptr)) == 2, stw_##e##_p,            \
308         __builtin_choose_expr(sizeof(*(hptr)) == 4, stl_##e##_p,            \
309         __builtin_choose_expr(sizeof(*(hptr)) == 8, stq_##e##_p, abort))))  \
310             ((hptr), (x)), (void)0);                                        \
311         PRAGMA_REENABLE_PACKED_WARNING;                                     \
312     } while (0)
313 
314 #define __get_user_e(x, hptr, e)                                            \
315     do {                                                                    \
316         PRAGMA_DISABLE_PACKED_WARNING;                                      \
317         ((x) = (typeof(*hptr))(                                             \
318         __builtin_choose_expr(sizeof(*(hptr)) == 1, ldub_p,                 \
319         __builtin_choose_expr(sizeof(*(hptr)) == 2, lduw_##e##_p,           \
320         __builtin_choose_expr(sizeof(*(hptr)) == 4, ldl_##e##_p,            \
321         __builtin_choose_expr(sizeof(*(hptr)) == 8, ldq_##e##_p, abort))))  \
322             (hptr)), (void)0);                                              \
323         PRAGMA_REENABLE_PACKED_WARNING;                                     \
324     } while (0)
325 
326 
327 #if TARGET_BIG_ENDIAN
328 # define __put_user(x, hptr)  __put_user_e(x, hptr, be)
329 # define __get_user(x, hptr)  __get_user_e(x, hptr, be)
330 #else
331 # define __put_user(x, hptr)  __put_user_e(x, hptr, le)
332 # define __get_user(x, hptr)  __get_user_e(x, hptr, le)
333 #endif
334 
335 /*
336  * put_user()/get_user() take a guest address and check access
337  *
338  * These are usually used to access an atomic data type, such as an int, that
339  * has been passed by address.  These internally perform locking and unlocking
340  * on the data type.
341  */
342 #define put_user(x, gaddr, target_type)                                 \
343 ({                                                                      \
344     abi_ulong __gaddr = (gaddr);                                        \
345     target_type *__hptr;                                                \
346     abi_long __ret = 0;                                                 \
347     __hptr = lock_user(VERIFY_WRITE, __gaddr, sizeof(target_type), 0);  \
348     if (__hptr) {                                                       \
349         __put_user((x), __hptr);                                        \
350         unlock_user(__hptr, __gaddr, sizeof(target_type));              \
351     } else                                                              \
352         __ret = -TARGET_EFAULT;                                         \
353     __ret;                                                              \
354 })
355 
356 #define get_user(x, gaddr, target_type)                                 \
357 ({                                                                      \
358     abi_ulong __gaddr = (gaddr);                                        \
359     target_type *__hptr;                                                \
360     abi_long __ret = 0;                                                 \
361     __hptr = lock_user(VERIFY_READ, __gaddr, sizeof(target_type), 1);   \
362     if (__hptr) {                                                       \
363         __get_user((x), __hptr);                                        \
364         unlock_user(__hptr, __gaddr, 0);                                \
365     } else {                                                            \
366         (x) = 0;                                                        \
367         __ret = -TARGET_EFAULT;                                         \
368     }                                                                   \
369     __ret;                                                              \
370 })
371 
372 #define put_user_ual(x, gaddr) put_user((x), (gaddr), abi_ulong)
373 #define put_user_sal(x, gaddr) put_user((x), (gaddr), abi_long)
374 #define put_user_u64(x, gaddr) put_user((x), (gaddr), uint64_t)
375 #define put_user_s64(x, gaddr) put_user((x), (gaddr), int64_t)
376 #define put_user_u32(x, gaddr) put_user((x), (gaddr), uint32_t)
377 #define put_user_s32(x, gaddr) put_user((x), (gaddr), int32_t)
378 #define put_user_u16(x, gaddr) put_user((x), (gaddr), uint16_t)
379 #define put_user_s16(x, gaddr) put_user((x), (gaddr), int16_t)
380 #define put_user_u8(x, gaddr)  put_user((x), (gaddr), uint8_t)
381 #define put_user_s8(x, gaddr)  put_user((x), (gaddr), int8_t)
382 
383 #define get_user_ual(x, gaddr) get_user((x), (gaddr), abi_ulong)
384 #define get_user_sal(x, gaddr) get_user((x), (gaddr), abi_long)
385 #define get_user_u64(x, gaddr) get_user((x), (gaddr), uint64_t)
386 #define get_user_s64(x, gaddr) get_user((x), (gaddr), int64_t)
387 #define get_user_u32(x, gaddr) get_user((x), (gaddr), uint32_t)
388 #define get_user_s32(x, gaddr) get_user((x), (gaddr), int32_t)
389 #define get_user_u16(x, gaddr) get_user((x), (gaddr), uint16_t)
390 #define get_user_s16(x, gaddr) get_user((x), (gaddr), int16_t)
391 #define get_user_u8(x, gaddr)  get_user((x), (gaddr), uint8_t)
392 #define get_user_s8(x, gaddr)  get_user((x), (gaddr), int8_t)
393 
394 /*
395  * copy_from_user() and copy_to_user() are usually used to copy data
396  * buffers between the target and host.  These internally perform
397  * locking/unlocking of the memory.
398  */
399 abi_long copy_from_user(void *hptr, abi_ulong gaddr, size_t len);
400 abi_long copy_to_user(abi_ulong gaddr, void *hptr, size_t len);
401 
402 /*
403  * Functions for accessing guest memory.  The tget and tput functions
404  * read/write single values, byteswapping as necessary.  The lock_user function
405  * gets a pointer to a contiguous area of guest memory, but does not perform
406  * any byteswapping.  lock_user may return either a pointer to the guest
407  * memory, or a temporary buffer.
408  */
409 
410 /*
411  * Lock an area of guest memory into the host.  If copy is true then the
412  * host area will have the same contents as the guest.
413  */
414 static inline void *lock_user(int type, abi_ulong guest_addr, long len,
415                               int copy)
416 {
417     if (!access_ok(type, guest_addr, len)) {
418         return NULL;
419     }
420 #ifdef DEBUG_REMAP
421     {
422         void *addr;
423         addr = g_malloc(len);
424         if (copy) {
425             memcpy(addr, g2h_untagged(guest_addr), len);
426         } else {
427             memset(addr, 0, len);
428         }
429         return addr;
430     }
431 #else
432     return g2h_untagged(guest_addr);
433 #endif
434 }
435 
436 /*
437  * Unlock an area of guest memory.  The first LEN bytes must be flushed back to
438  * guest memory. host_ptr = NULL is explicitly allowed and does nothing.
439  */
440 static inline void unlock_user(void *host_ptr, abi_ulong guest_addr,
441                                long len)
442 {
443 
444 #ifdef DEBUG_REMAP
445     if (!host_ptr) {
446         return;
447     }
448     if (host_ptr == g2h_untagged(guest_addr)) {
449         return;
450     }
451     if (len > 0) {
452         memcpy(g2h_untagged(guest_addr), host_ptr, len);
453     }
454     g_free(host_ptr);
455 #endif
456 }
457 
458 /*
459  * Return the length of a string in target memory or -TARGET_EFAULT if access
460  * error.
461  */
462 abi_long target_strlen(abi_ulong gaddr);
463 
464 /* Like lock_user but for null terminated strings.  */
465 static inline void *lock_user_string(abi_ulong guest_addr)
466 {
467     abi_long len;
468     len = target_strlen(guest_addr);
469     if (len < 0) {
470         return NULL;
471     }
472     return lock_user(VERIFY_READ, guest_addr, (long)(len + 1), 1);
473 }
474 
475 /* Helper macros for locking/unlocking a target struct.  */
476 #define lock_user_struct(type, host_ptr, guest_addr, copy)      \
477     (host_ptr = lock_user(type, guest_addr, sizeof(*host_ptr), copy))
478 #define unlock_user_struct(host_ptr, guest_addr, copy)          \
479     unlock_user(host_ptr, guest_addr, (copy) ? sizeof(*host_ptr) : 0)
480 
481 static inline uint64_t target_arg64(uint32_t word0, uint32_t word1)
482 {
483 #if TARGET_ABI_BITS == 32
484 #if TARGET_BIG_ENDIAN
485     return ((uint64_t)word0 << 32) | word1;
486 #else
487     return ((uint64_t)word1 << 32) | word0;
488 #endif
489 #else /* TARGET_ABI_BITS != 32 */
490     return word0;
491 #endif /* TARGET_ABI_BITS != 32 */
492 }
493 
494 #include <pthread.h>
495 
496 #include "user/safe-syscall.h"
497 
498 #endif /* QEMU_H */
499