xref: /openbmc/qemu/linux-user/qemu.h (revision 2113aed687cb0b84ad512c440c1edf6eea8fcde2)
1 #ifndef QEMU_H
2 #define QEMU_H
3 
4 #include "hostdep.h"
5 #include "cpu.h"
6 #include "exec/exec-all.h"
7 #include "exec/cpu_ldst.h"
8 
9 #undef DEBUG_REMAP
10 
11 #include "exec/user/abitypes.h"
12 
13 #include "exec/user/thunk.h"
14 #include "syscall_defs.h"
15 #include "target_syscall.h"
16 #include "exec/gdbstub.h"
17 
18 /*
19  * This is the size of the host kernel's sigset_t, needed where we make
20  * direct system calls that take a sigset_t pointer and a size.
21  */
22 #define SIGSET_T_SIZE (_NSIG / 8)
23 
24 /*
25  * This struct is used to hold certain information about the image.
26  * Basically, it replicates in user space what would be certain
27  * task_struct fields in the kernel
28  */
29 struct image_info {
30         abi_ulong       load_bias;
31         abi_ulong       load_addr;
32         abi_ulong       start_code;
33         abi_ulong       end_code;
34         abi_ulong       start_data;
35         abi_ulong       end_data;
36         abi_ulong       start_brk;
37         abi_ulong       brk;
38         abi_ulong       reserve_brk;
39         abi_ulong       start_mmap;
40         abi_ulong       start_stack;
41         abi_ulong       stack_limit;
42         abi_ulong       entry;
43         abi_ulong       code_offset;
44         abi_ulong       data_offset;
45         abi_ulong       saved_auxv;
46         abi_ulong       auxv_len;
47         abi_ulong       arg_start;
48         abi_ulong       arg_end;
49         abi_ulong       arg_strings;
50         abi_ulong       env_strings;
51         abi_ulong       file_string;
52         uint32_t        elf_flags;
53         int             personality;
54         abi_ulong       alignment;
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 #define MAX_SIGQUEUE_SIZE 1024
97 
98 struct emulated_sigtable {
99     int pending; /* true if signal is pending */
100     target_siginfo_t info;
101 };
102 
103 /*
104  * NOTE: we force a big alignment so that the stack stored after is
105  * aligned too
106  */
107 typedef struct TaskState {
108     pid_t ts_tid;     /* tid (or pid) of this task */
109 #ifdef TARGET_ARM
110 # ifdef TARGET_ABI32
111     /* FPA state */
112     FPA11 fpa;
113 # endif
114 #endif
115 #if defined(TARGET_ARM) || defined(TARGET_RISCV)
116     int swi_errno;
117 #endif
118 #if defined(TARGET_I386) && !defined(TARGET_X86_64)
119     abi_ulong target_v86;
120     struct vm86_saved_state vm86_saved_regs;
121     struct target_vm86plus_struct vm86plus;
122     uint32_t v86flags;
123     uint32_t v86mask;
124 #endif
125     abi_ulong child_tidptr;
126 #ifdef TARGET_M68K
127     abi_ulong tp_value;
128 #endif
129 #if defined(TARGET_ARM) || defined(TARGET_M68K) || defined(TARGET_RISCV)
130     /* Extra fields for semihosted binaries.  */
131     abi_ulong heap_base;
132     abi_ulong heap_limit;
133 #endif
134     abi_ulong stack_base;
135     int used; /* non zero if used */
136     struct image_info *info;
137     struct linux_binprm *bprm;
138 
139     struct emulated_sigtable sync_signal;
140     struct emulated_sigtable sigtab[TARGET_NSIG];
141     /*
142      * This thread's signal mask, as requested by the guest program.
143      * The actual signal mask of this thread may differ:
144      *  + we don't let SIGSEGV and SIGBUS be blocked while running guest code
145      *  + sometimes we block all signals to avoid races
146      */
147     sigset_t signal_mask;
148     /*
149      * The signal mask imposed by a guest sigsuspend syscall, if we are
150      * currently in the middle of such a syscall
151      */
152     sigset_t sigsuspend_mask;
153     /* Nonzero if we're leaving a sigsuspend and sigsuspend_mask is valid. */
154     int in_sigsuspend;
155 
156     /*
157      * Nonzero if process_pending_signals() needs to do something (either
158      * handle a pending signal or unblock signals).
159      * This flag is written from a signal handler so should be accessed via
160      * the qatomic_read() and qatomic_set() functions. (It is not accessed
161      * from multiple threads.)
162      */
163     int signal_pending;
164 
165     /* This thread's sigaltstack, if it has one */
166     struct target_sigaltstack sigaltstack_used;
167 } __attribute__((aligned(16))) TaskState;
168 
169 extern char *exec_path;
170 void init_task_state(TaskState *ts);
171 void task_settid(TaskState *);
172 void stop_all_tasks(void);
173 extern const char *qemu_uname_release;
174 extern unsigned long mmap_min_addr;
175 
176 /* ??? See if we can avoid exposing so much of the loader internals.  */
177 
178 /*
179  * Read a good amount of data initially, to hopefully get all the
180  * program headers loaded.
181  */
182 #define BPRM_BUF_SIZE  1024
183 
184 /*
185  * This structure is used to hold the arguments that are
186  * used when loading binaries.
187  */
188 struct linux_binprm {
189         char buf[BPRM_BUF_SIZE] __attribute__((aligned));
190         abi_ulong p;
191         int fd;
192         int e_uid, e_gid;
193         int argc, envc;
194         char **argv;
195         char **envp;
196         char *filename;        /* Name of binary */
197         int (*core_dump)(int, const CPUArchState *); /* coredump routine */
198 };
199 
200 typedef struct IOCTLEntry IOCTLEntry;
201 
202 typedef abi_long do_ioctl_fn(const IOCTLEntry *ie, uint8_t *buf_temp,
203                              int fd, int cmd, abi_long arg);
204 
205 struct IOCTLEntry {
206     int target_cmd;
207     unsigned int host_cmd;
208     const char *name;
209     int access;
210     do_ioctl_fn *do_ioctl;
211     const argtype arg_type[5];
212 };
213 
214 extern IOCTLEntry ioctl_entries[];
215 
216 #define IOC_R 0x0001
217 #define IOC_W 0x0002
218 #define IOC_RW (IOC_R | IOC_W)
219 
220 void do_init_thread(struct target_pt_regs *regs, struct image_info *infop);
221 abi_ulong loader_build_argptr(int envc, int argc, abi_ulong sp,
222                               abi_ulong stringp, int push_ptr);
223 int loader_exec(int fdexec, const char *filename, char **argv, char **envp,
224              struct target_pt_regs *regs, struct image_info *infop,
225              struct linux_binprm *);
226 
227 /*
228  * Returns true if the image uses the FDPIC ABI. If this is the case,
229  * we have to provide some information (loadmap, pt_dynamic_info) such
230  * that the program can be relocated adequately. This is also useful
231  * when handling signals.
232  */
233 int info_is_fdpic(struct image_info *info);
234 
235 uint32_t get_elf_eflags(int fd);
236 int load_elf_binary(struct linux_binprm *bprm, struct image_info *info);
237 int load_flt_binary(struct linux_binprm *bprm, struct image_info *info);
238 
239 abi_long memcpy_to_target(abi_ulong dest, const void *src,
240                           unsigned long len);
241 void target_set_brk(abi_ulong new_brk);
242 abi_long do_brk(abi_ulong new_brk);
243 void syscall_init(void);
244 abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
245                     abi_long arg2, abi_long arg3, abi_long arg4,
246                     abi_long arg5, abi_long arg6, abi_long arg7,
247                     abi_long arg8);
248 extern __thread CPUState *thread_cpu;
249 void cpu_loop(CPUArchState *env);
250 const char *target_strerror(int err);
251 int get_osversion(void);
252 void init_qemu_uname_release(void);
253 void fork_start(void);
254 void fork_end(int child);
255 
256 /**
257  * probe_guest_base:
258  * @image_name: the executable being loaded
259  * @loaddr: the lowest fixed address in the executable
260  * @hiaddr: the highest fixed address in the executable
261  *
262  * Creates the initial guest address space in the host memory space.
263  *
264  * If @loaddr == 0, then no address in the executable is fixed,
265  * i.e. it is fully relocatable.  In that case @hiaddr is the size
266  * of the executable.
267  *
268  * This function will not return if a valid value for guest_base
269  * cannot be chosen.  On return, the executable loader can expect
270  *
271  *    target_mmap(loaddr, hiaddr - loaddr, ...)
272  *
273  * to succeed.
274  */
275 void probe_guest_base(const char *image_name,
276                       abi_ulong loaddr, abi_ulong hiaddr);
277 
278 #include "qemu/log.h"
279 
280 /* safe_syscall.S */
281 
282 /**
283  * safe_syscall:
284  * @int number: number of system call to make
285  * ...: arguments to the system call
286  *
287  * Call a system call if guest signal not pending.
288  * This has the same API as the libc syscall() function, except that it
289  * may return -1 with errno == TARGET_ERESTARTSYS if a signal was pending.
290  *
291  * Returns: the system call result, or -1 with an error code in errno
292  * (Errnos are host errnos; we rely on TARGET_ERESTARTSYS not clashing
293  * with any of the host errno values.)
294  */
295 
296 /*
297  * A guide to using safe_syscall() to handle interactions between guest
298  * syscalls and guest signals:
299  *
300  * Guest syscalls come in two flavours:
301  *
302  * (1) Non-interruptible syscalls
303  *
304  * These are guest syscalls that never get interrupted by signals and
305  * so never return EINTR. They can be implemented straightforwardly in
306  * QEMU: just make sure that if the implementation code has to make any
307  * blocking calls that those calls are retried if they return EINTR.
308  * It's also OK to implement these with safe_syscall, though it will be
309  * a little less efficient if a signal is delivered at the 'wrong' moment.
310  *
311  * Some non-interruptible syscalls need to be handled using block_signals()
312  * to block signals for the duration of the syscall. This mainly applies
313  * to code which needs to modify the data structures used by the
314  * host_signal_handler() function and the functions it calls, including
315  * all syscalls which change the thread's signal mask.
316  *
317  * (2) Interruptible syscalls
318  *
319  * These are guest syscalls that can be interrupted by signals and
320  * for which we need to either return EINTR or arrange for the guest
321  * syscall to be restarted. This category includes both syscalls which
322  * always restart (and in the kernel return -ERESTARTNOINTR), ones
323  * which only restart if there is no handler (kernel returns -ERESTARTNOHAND
324  * or -ERESTART_RESTARTBLOCK), and the most common kind which restart
325  * if the handler was registered with SA_RESTART (kernel returns
326  * -ERESTARTSYS). System calls which are only interruptible in some
327  * situations (like 'open') also need to be handled this way.
328  *
329  * Here it is important that the host syscall is made
330  * via this safe_syscall() function, and *not* via the host libc.
331  * If the host libc is used then the implementation will appear to work
332  * most of the time, but there will be a race condition where a
333  * signal could arrive just before we make the host syscall inside libc,
334  * and then then guest syscall will not correctly be interrupted.
335  * Instead the implementation of the guest syscall can use the safe_syscall
336  * function but otherwise just return the result or errno in the usual
337  * way; the main loop code will take care of restarting the syscall
338  * if appropriate.
339  *
340  * (If the implementation needs to make multiple host syscalls this is
341  * OK; any which might really block must be via safe_syscall(); for those
342  * which are only technically blocking (ie which we know in practice won't
343  * stay in the host kernel indefinitely) it's OK to use libc if necessary.
344  * You must be able to cope with backing out correctly if some safe_syscall
345  * you make in the implementation returns either -TARGET_ERESTARTSYS or
346  * EINTR though.)
347  *
348  * block_signals() cannot be used for interruptible syscalls.
349  *
350  *
351  * How and why the safe_syscall implementation works:
352  *
353  * The basic setup is that we make the host syscall via a known
354  * section of host native assembly. If a signal occurs, our signal
355  * handler checks the interrupted host PC against the addresse of that
356  * known section. If the PC is before or at the address of the syscall
357  * instruction then we change the PC to point at a "return
358  * -TARGET_ERESTARTSYS" code path instead, and then exit the signal handler
359  * (causing the safe_syscall() call to immediately return that value).
360  * Then in the main.c loop if we see this magic return value we adjust
361  * the guest PC to wind it back to before the system call, and invoke
362  * the guest signal handler as usual.
363  *
364  * This winding-back will happen in two cases:
365  * (1) signal came in just before we took the host syscall (a race);
366  *   in this case we'll take the guest signal and have another go
367  *   at the syscall afterwards, and this is indistinguishable for the
368  *   guest from the timing having been different such that the guest
369  *   signal really did win the race
370  * (2) signal came in while the host syscall was blocking, and the
371  *   host kernel decided the syscall should be restarted;
372  *   in this case we want to restart the guest syscall also, and so
373  *   rewinding is the right thing. (Note that "restart" semantics mean
374  *   "first call the signal handler, then reattempt the syscall".)
375  * The other situation to consider is when a signal came in while the
376  * host syscall was blocking, and the host kernel decided that the syscall
377  * should not be restarted; in this case QEMU's host signal handler will
378  * be invoked with the PC pointing just after the syscall instruction,
379  * with registers indicating an EINTR return; the special code in the
380  * handler will not kick in, and we will return EINTR to the guest as
381  * we should.
382  *
383  * Notice that we can leave the host kernel to make the decision for
384  * us about whether to do a restart of the syscall or not; we do not
385  * need to check SA_RESTART flags in QEMU or distinguish the various
386  * kinds of restartability.
387  */
388 #ifdef HAVE_SAFE_SYSCALL
389 /* The core part of this function is implemented in assembly */
390 extern long safe_syscall_base(int *pending, long number, ...);
391 
392 #define safe_syscall(...)                                               \
393     ({                                                                  \
394         long ret_;                                                      \
395         int *psp_ = &((TaskState *)thread_cpu->opaque)->signal_pending; \
396         ret_ = safe_syscall_base(psp_, __VA_ARGS__);                    \
397         if (is_error(ret_)) {                                           \
398             errno = -ret_;                                              \
399             ret_ = -1;                                                  \
400         }                                                               \
401         ret_;                                                           \
402     })
403 
404 #else
405 
406 /*
407  * Fallback for architectures which don't yet provide a safe-syscall assembly
408  * fragment; note that this is racy!
409  * This should go away when all host architectures have been updated.
410  */
411 #define safe_syscall syscall
412 
413 #endif
414 
415 /* syscall.c */
416 int host_to_target_waitstatus(int status);
417 
418 #ifdef TARGET_I386
419 /* vm86.c */
420 void save_v86_state(CPUX86State *env);
421 void handle_vm86_trap(CPUX86State *env, int trapno);
422 void handle_vm86_fault(CPUX86State *env);
423 int do_vm86(CPUX86State *env, long subfunction, abi_ulong v86_addr);
424 #elif defined(TARGET_SPARC64)
425 void sparc64_set_context(CPUSPARCState *env);
426 void sparc64_get_context(CPUSPARCState *env);
427 #endif
428 
429 /* mmap.c */
430 int target_mprotect(abi_ulong start, abi_ulong len, int prot);
431 abi_long target_mmap(abi_ulong start, abi_ulong len, int prot,
432                      int flags, int fd, abi_ulong offset);
433 int target_munmap(abi_ulong start, abi_ulong len);
434 abi_long target_mremap(abi_ulong old_addr, abi_ulong old_size,
435                        abi_ulong new_size, unsigned long flags,
436                        abi_ulong new_addr);
437 extern unsigned long last_brk;
438 extern abi_ulong mmap_next_start;
439 abi_ulong mmap_find_vma(abi_ulong, abi_ulong, abi_ulong);
440 void mmap_fork_start(void);
441 void mmap_fork_end(int child);
442 
443 /* main.c */
444 extern unsigned long guest_stack_size;
445 
446 /* user access */
447 
448 #define VERIFY_READ  PAGE_READ
449 #define VERIFY_WRITE (PAGE_READ | PAGE_WRITE)
450 
451 static inline bool access_ok_untagged(int type, abi_ulong addr, abi_ulong size)
452 {
453     if (size == 0
454         ? !guest_addr_valid_untagged(addr)
455         : !guest_range_valid_untagged(addr, size)) {
456         return false;
457     }
458     return page_check_range((target_ulong)addr, size, type) == 0;
459 }
460 
461 static inline bool access_ok(CPUState *cpu, int type,
462                              abi_ulong addr, abi_ulong size)
463 {
464     return access_ok_untagged(type, cpu_untagged_addr(cpu, addr), size);
465 }
466 
467 /* NOTE __get_user and __put_user use host pointers and don't check access.
468    These are usually used to access struct data members once the struct has
469    been locked - usually with lock_user_struct.  */
470 
471 /*
472  * Tricky points:
473  * - Use __builtin_choose_expr to avoid type promotion from ?:,
474  * - Invalid sizes result in a compile time error stemming from
475  *   the fact that abort has no parameters.
476  * - It's easier to use the endian-specific unaligned load/store
477  *   functions than host-endian unaligned load/store plus tswapN.
478  * - The pragmas are necessary only to silence a clang false-positive
479  *   warning: see https://bugs.llvm.org/show_bug.cgi?id=39113 .
480  * - gcc has bugs in its _Pragma() support in some versions, eg
481  *   https://gcc.gnu.org/bugzilla/show_bug.cgi?id=83256 -- so we only
482  *   include the warning-suppression pragmas for clang
483  */
484 #if defined(__clang__) && __has_warning("-Waddress-of-packed-member")
485 #define PRAGMA_DISABLE_PACKED_WARNING                                   \
486     _Pragma("GCC diagnostic push");                                     \
487     _Pragma("GCC diagnostic ignored \"-Waddress-of-packed-member\"")
488 
489 #define PRAGMA_REENABLE_PACKED_WARNING          \
490     _Pragma("GCC diagnostic pop")
491 
492 #else
493 #define PRAGMA_DISABLE_PACKED_WARNING
494 #define PRAGMA_REENABLE_PACKED_WARNING
495 #endif
496 
497 #define __put_user_e(x, hptr, e)                                            \
498     do {                                                                    \
499         PRAGMA_DISABLE_PACKED_WARNING;                                      \
500         (__builtin_choose_expr(sizeof(*(hptr)) == 1, stb_p,                 \
501         __builtin_choose_expr(sizeof(*(hptr)) == 2, stw_##e##_p,            \
502         __builtin_choose_expr(sizeof(*(hptr)) == 4, stl_##e##_p,            \
503         __builtin_choose_expr(sizeof(*(hptr)) == 8, stq_##e##_p, abort))))  \
504             ((hptr), (x)), (void)0);                                        \
505         PRAGMA_REENABLE_PACKED_WARNING;                                     \
506     } while (0)
507 
508 #define __get_user_e(x, hptr, e)                                            \
509     do {                                                                    \
510         PRAGMA_DISABLE_PACKED_WARNING;                                      \
511         ((x) = (typeof(*hptr))(                                             \
512         __builtin_choose_expr(sizeof(*(hptr)) == 1, ldub_p,                 \
513         __builtin_choose_expr(sizeof(*(hptr)) == 2, lduw_##e##_p,           \
514         __builtin_choose_expr(sizeof(*(hptr)) == 4, ldl_##e##_p,            \
515         __builtin_choose_expr(sizeof(*(hptr)) == 8, ldq_##e##_p, abort))))  \
516             (hptr)), (void)0);                                              \
517         PRAGMA_REENABLE_PACKED_WARNING;                                     \
518     } while (0)
519 
520 
521 #ifdef TARGET_WORDS_BIGENDIAN
522 # define __put_user(x, hptr)  __put_user_e(x, hptr, be)
523 # define __get_user(x, hptr)  __get_user_e(x, hptr, be)
524 #else
525 # define __put_user(x, hptr)  __put_user_e(x, hptr, le)
526 # define __get_user(x, hptr)  __get_user_e(x, hptr, le)
527 #endif
528 
529 /* put_user()/get_user() take a guest address and check access */
530 /* These are usually used to access an atomic data type, such as an int,
531  * that has been passed by address.  These internally perform locking
532  * and unlocking on the data type.
533  */
534 #define put_user(x, gaddr, target_type)					\
535 ({									\
536     abi_ulong __gaddr = (gaddr);					\
537     target_type *__hptr;						\
538     abi_long __ret = 0;							\
539     if ((__hptr = lock_user(VERIFY_WRITE, __gaddr, sizeof(target_type), 0))) { \
540         __put_user((x), __hptr);				\
541         unlock_user(__hptr, __gaddr, sizeof(target_type));		\
542     } else								\
543         __ret = -TARGET_EFAULT;						\
544     __ret;								\
545 })
546 
547 #define get_user(x, gaddr, target_type)					\
548 ({									\
549     abi_ulong __gaddr = (gaddr);					\
550     target_type *__hptr;						\
551     abi_long __ret = 0;							\
552     if ((__hptr = lock_user(VERIFY_READ, __gaddr, sizeof(target_type), 1))) { \
553         __get_user((x), __hptr);				\
554         unlock_user(__hptr, __gaddr, 0);				\
555     } else {								\
556         /* avoid warning */						\
557         (x) = 0;							\
558         __ret = -TARGET_EFAULT;						\
559     }									\
560     __ret;								\
561 })
562 
563 #define put_user_ual(x, gaddr) put_user((x), (gaddr), abi_ulong)
564 #define put_user_sal(x, gaddr) put_user((x), (gaddr), abi_long)
565 #define put_user_u64(x, gaddr) put_user((x), (gaddr), uint64_t)
566 #define put_user_s64(x, gaddr) put_user((x), (gaddr), int64_t)
567 #define put_user_u32(x, gaddr) put_user((x), (gaddr), uint32_t)
568 #define put_user_s32(x, gaddr) put_user((x), (gaddr), int32_t)
569 #define put_user_u16(x, gaddr) put_user((x), (gaddr), uint16_t)
570 #define put_user_s16(x, gaddr) put_user((x), (gaddr), int16_t)
571 #define put_user_u8(x, gaddr)  put_user((x), (gaddr), uint8_t)
572 #define put_user_s8(x, gaddr)  put_user((x), (gaddr), int8_t)
573 
574 #define get_user_ual(x, gaddr) get_user((x), (gaddr), abi_ulong)
575 #define get_user_sal(x, gaddr) get_user((x), (gaddr), abi_long)
576 #define get_user_u64(x, gaddr) get_user((x), (gaddr), uint64_t)
577 #define get_user_s64(x, gaddr) get_user((x), (gaddr), int64_t)
578 #define get_user_u32(x, gaddr) get_user((x), (gaddr), uint32_t)
579 #define get_user_s32(x, gaddr) get_user((x), (gaddr), int32_t)
580 #define get_user_u16(x, gaddr) get_user((x), (gaddr), uint16_t)
581 #define get_user_s16(x, gaddr) get_user((x), (gaddr), int16_t)
582 #define get_user_u8(x, gaddr)  get_user((x), (gaddr), uint8_t)
583 #define get_user_s8(x, gaddr)  get_user((x), (gaddr), int8_t)
584 
585 /* copy_from_user() and copy_to_user() are usually used to copy data
586  * buffers between the target and host.  These internally perform
587  * locking/unlocking of the memory.
588  */
589 int copy_from_user(void *hptr, abi_ulong gaddr, ssize_t len);
590 int copy_to_user(abi_ulong gaddr, void *hptr, ssize_t len);
591 
592 /* Functions for accessing guest memory.  The tget and tput functions
593    read/write single values, byteswapping as necessary.  The lock_user function
594    gets a pointer to a contiguous area of guest memory, but does not perform
595    any byteswapping.  lock_user may return either a pointer to the guest
596    memory, or a temporary buffer.  */
597 
598 /* Lock an area of guest memory into the host.  If copy is true then the
599    host area will have the same contents as the guest.  */
600 void *lock_user(int type, abi_ulong guest_addr, ssize_t len, bool copy);
601 
602 /* Unlock an area of guest memory.  The first LEN bytes must be
603    flushed back to guest memory. host_ptr = NULL is explicitly
604    allowed and does nothing. */
605 #ifndef DEBUG_REMAP
606 static inline void unlock_user(void *host_ptr, abi_ulong guest_addr,
607                                ssize_t len)
608 {
609     /* no-op */
610 }
611 #else
612 void unlock_user(void *host_ptr, abi_ulong guest_addr, ssize_t len);
613 #endif
614 
615 /* Return the length of a string in target memory or -TARGET_EFAULT if
616    access error. */
617 ssize_t target_strlen(abi_ulong gaddr);
618 
619 /* Like lock_user but for null terminated strings.  */
620 void *lock_user_string(abi_ulong guest_addr);
621 
622 /* Helper macros for locking/unlocking a target struct.  */
623 #define lock_user_struct(type, host_ptr, guest_addr, copy)	\
624     (host_ptr = lock_user(type, guest_addr, sizeof(*host_ptr), copy))
625 #define unlock_user_struct(host_ptr, guest_addr, copy)		\
626     unlock_user(host_ptr, guest_addr, (copy) ? sizeof(*host_ptr) : 0)
627 
628 #include <pthread.h>
629 
630 static inline int is_error(abi_long ret)
631 {
632     return (abi_ulong)ret >= (abi_ulong)(-4096);
633 }
634 
635 #if TARGET_ABI_BITS == 32
636 static inline uint64_t target_offset64(uint32_t word0, uint32_t word1)
637 {
638 #ifdef TARGET_WORDS_BIGENDIAN
639     return ((uint64_t)word0 << 32) | word1;
640 #else
641     return ((uint64_t)word1 << 32) | word0;
642 #endif
643 }
644 #else /* TARGET_ABI_BITS == 32 */
645 static inline uint64_t target_offset64(uint64_t word0, uint64_t word1)
646 {
647     return word0;
648 }
649 #endif /* TARGET_ABI_BITS != 32 */
650 
651 void print_termios(void *arg);
652 
653 /* ARM EABI and MIPS expect 64bit types aligned even on pairs or registers */
654 #ifdef TARGET_ARM
655 static inline int regpairs_aligned(void *cpu_env, int num)
656 {
657     return ((((CPUARMState *)cpu_env)->eabi) == 1) ;
658 }
659 #elif defined(TARGET_MIPS) && (TARGET_ABI_BITS == 32)
660 static inline int regpairs_aligned(void *cpu_env, int num) { return 1; }
661 #elif defined(TARGET_PPC) && !defined(TARGET_PPC64)
662 /*
663  * SysV AVI for PPC32 expects 64bit parameters to be passed on odd/even pairs
664  * of registers which translates to the same as ARM/MIPS, because we start with
665  * r3 as arg1
666  */
667 static inline int regpairs_aligned(void *cpu_env, int num) { return 1; }
668 #elif defined(TARGET_SH4)
669 /* SH4 doesn't align register pairs, except for p{read,write}64 */
670 static inline int regpairs_aligned(void *cpu_env, int num)
671 {
672     switch (num) {
673     case TARGET_NR_pread64:
674     case TARGET_NR_pwrite64:
675         return 1;
676 
677     default:
678         return 0;
679     }
680 }
681 #elif defined(TARGET_XTENSA)
682 static inline int regpairs_aligned(void *cpu_env, int num) { return 1; }
683 #elif defined(TARGET_HEXAGON)
684 static inline int regpairs_aligned(void *cpu_env, int num) { return 1; }
685 #else
686 static inline int regpairs_aligned(void *cpu_env, int num) { return 0; }
687 #endif
688 
689 /**
690  * preexit_cleanup: housekeeping before the guest exits
691  *
692  * env: the CPU state
693  * code: the exit code
694  */
695 void preexit_cleanup(CPUArchState *env, int code);
696 
697 /*
698  * Include target-specific struct and function definitions;
699  * they may need access to the target-independent structures
700  * above, so include them last.
701  */
702 #include "target_cpu.h"
703 #include "target_structs.h"
704 
705 #endif /* QEMU_H */
706