xref: /openbmc/qemu/bsd-user/qemu.h (revision c2bdd9a1)
188dae46dSSean Bruno /*
288dae46dSSean Bruno  *  qemu bsd user mode definition
388dae46dSSean Bruno  *
488dae46dSSean Bruno  *  This program is free software; you can redistribute it and/or modify
588dae46dSSean Bruno  *  it under the terms of the GNU General Public License as published by
688dae46dSSean Bruno  *  the Free Software Foundation; either version 2 of the License, or
788dae46dSSean Bruno  *  (at your option) any later version.
888dae46dSSean Bruno  *
988dae46dSSean Bruno  *  This program is distributed in the hope that it will be useful,
1088dae46dSSean Bruno  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
1188dae46dSSean Bruno  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1288dae46dSSean Bruno  *  GNU General Public License for more details.
1388dae46dSSean Bruno  *
1488dae46dSSean Bruno  *  You should have received a copy of the GNU General Public License
1588dae46dSSean Bruno  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
1688dae46dSSean Bruno  */
1784778508Sblueswir1 #ifndef QEMU_H
1884778508Sblueswir1 #define QEMU_H
1984778508Sblueswir1 
2084778508Sblueswir1 
2184778508Sblueswir1 #include "cpu.h"
22f08b6170SPaolo Bonzini #include "exec/cpu_ldst.h"
2384778508Sblueswir1 
2484778508Sblueswir1 #undef DEBUG_REMAP
2584778508Sblueswir1 #ifdef DEBUG_REMAP
2684778508Sblueswir1 #endif /* DEBUG_REMAP */
2784778508Sblueswir1 
28022c62cbSPaolo Bonzini #include "exec/user/abitypes.h"
2984778508Sblueswir1 
304b599848SWarner Losh extern char **environ;
314b599848SWarner Losh 
3284778508Sblueswir1 enum BSDType {
3384778508Sblueswir1     target_freebsd,
3484778508Sblueswir1     target_netbsd,
3584778508Sblueswir1     target_openbsd,
3684778508Sblueswir1 };
3778cfb07fSJuergen Lock extern enum BSDType bsd_type;
3884778508Sblueswir1 
3984778508Sblueswir1 #include "syscall_defs.h"
400c6940d0SLluís Vilanova #include "target_syscall.h"
4184778508Sblueswir1 #include "target_signal.h"
42022c62cbSPaolo Bonzini #include "exec/gdbstub.h"
4384778508Sblueswir1 
442f7bb878SJuan Quintela #if defined(CONFIG_USE_NPTL)
4584778508Sblueswir1 #define THREAD __thread
4684778508Sblueswir1 #else
4784778508Sblueswir1 #define THREAD
4884778508Sblueswir1 #endif
4984778508Sblueswir1 
50*c2bdd9a1SWarner Losh /*
51*c2bdd9a1SWarner Losh  * This struct is used to hold certain information about the image.  Basically,
52*c2bdd9a1SWarner Losh  * it replicates in user space what would be certain task_struct fields in the
53*c2bdd9a1SWarner Losh  * kernel
5484778508Sblueswir1  */
5584778508Sblueswir1 struct image_info {
5684778508Sblueswir1     abi_ulong load_addr;
5784778508Sblueswir1     abi_ulong start_code;
5884778508Sblueswir1     abi_ulong end_code;
5984778508Sblueswir1     abi_ulong start_data;
6084778508Sblueswir1     abi_ulong end_data;
6184778508Sblueswir1     abi_ulong start_brk;
6284778508Sblueswir1     abi_ulong brk;
6384778508Sblueswir1     abi_ulong start_mmap;
6484778508Sblueswir1     abi_ulong mmap;
6584778508Sblueswir1     abi_ulong rss;
6684778508Sblueswir1     abi_ulong start_stack;
6784778508Sblueswir1     abi_ulong entry;
6884778508Sblueswir1     abi_ulong code_offset;
6984778508Sblueswir1     abi_ulong data_offset;
7084778508Sblueswir1     int       personality;
7184778508Sblueswir1 };
7284778508Sblueswir1 
7384778508Sblueswir1 #define MAX_SIGQUEUE_SIZE 1024
7484778508Sblueswir1 
7584778508Sblueswir1 struct sigqueue {
7684778508Sblueswir1     struct sigqueue *next;
7784778508Sblueswir1 };
7884778508Sblueswir1 
7984778508Sblueswir1 struct emulated_sigtable {
8084778508Sblueswir1     int pending; /* true if signal is pending */
8184778508Sblueswir1     struct sigqueue *first;
82*c2bdd9a1SWarner Losh     /* in order to always have memory for the first signal, we put it here */
83*c2bdd9a1SWarner Losh     struct sigqueue info;
8484778508Sblueswir1 };
8584778508Sblueswir1 
86*c2bdd9a1SWarner Losh /*
87*c2bdd9a1SWarner Losh  * NOTE: we force a big alignment so that the stack stored after is aligned too
88*c2bdd9a1SWarner Losh  */
8984778508Sblueswir1 typedef struct TaskState {
90bd88c780SAlex Bennée     pid_t ts_tid;     /* tid (or pid) of this task */
91bd88c780SAlex Bennée 
9284778508Sblueswir1     struct TaskState *next;
9384778508Sblueswir1     int used; /* non zero if used */
9484778508Sblueswir1     struct image_info *info;
9584778508Sblueswir1 
9684778508Sblueswir1     struct emulated_sigtable sigtab[TARGET_NSIG];
9784778508Sblueswir1     struct sigqueue sigqueue_table[MAX_SIGQUEUE_SIZE]; /* siginfo queue */
9884778508Sblueswir1     struct sigqueue *first_free; /* first free siginfo queue entry */
9984778508Sblueswir1     int signal_pending; /* non zero if a signal may be pending */
10084778508Sblueswir1 
101f7795e40SPhilippe Mathieu-Daudé     uint8_t stack[];
10284778508Sblueswir1 } __attribute__((aligned(16))) TaskState;
10384778508Sblueswir1 
10484778508Sblueswir1 void init_task_state(TaskState *ts);
10584778508Sblueswir1 extern const char *qemu_uname_release;
1062fa5d9baSBlue Swirl extern unsigned long mmap_min_addr;
10784778508Sblueswir1 
10884778508Sblueswir1 /*
10984778508Sblueswir1  * MAX_ARG_PAGES defines the number of pages allocated for arguments
11084778508Sblueswir1  * and envelope for the new program. 32 should suffice, this gives
11184778508Sblueswir1  * a maximum env+arg of 128kB w/4KB pages!
11284778508Sblueswir1  */
11384778508Sblueswir1 #define MAX_ARG_PAGES 32
11484778508Sblueswir1 
11584778508Sblueswir1 /*
11684778508Sblueswir1  * This structure is used to hold the arguments that are
11784778508Sblueswir1  * used when loading binaries.
11884778508Sblueswir1  */
11984778508Sblueswir1 struct linux_binprm {
12084778508Sblueswir1         char buf[128];
12184778508Sblueswir1         void *page[MAX_ARG_PAGES];
12284778508Sblueswir1         abi_ulong p;
12384778508Sblueswir1         int fd;
12484778508Sblueswir1         int e_uid, e_gid;
12584778508Sblueswir1         int argc, envc;
12684778508Sblueswir1         char **argv;
12784778508Sblueswir1         char **envp;
12884778508Sblueswir1         char *filename;         /* Name of binary */
12984778508Sblueswir1 };
13084778508Sblueswir1 
13184778508Sblueswir1 void do_init_thread(struct target_pt_regs *regs, struct image_info *infop);
13284778508Sblueswir1 abi_ulong loader_build_argptr(int envc, int argc, abi_ulong sp,
13384778508Sblueswir1                               abi_ulong stringp, int push_ptr);
13484778508Sblueswir1 int loader_exec(const char *filename, char **argv, char **envp,
13584778508Sblueswir1              struct target_pt_regs *regs, struct image_info *infop);
13684778508Sblueswir1 
13784778508Sblueswir1 int load_elf_binary(struct linux_binprm *bprm, struct target_pt_regs *regs,
13884778508Sblueswir1                     struct image_info *info);
13984778508Sblueswir1 int load_flt_binary(struct linux_binprm *bprm, struct target_pt_regs *regs,
14084778508Sblueswir1                     struct image_info *info);
14184778508Sblueswir1 
14284778508Sblueswir1 abi_long memcpy_to_target(abi_ulong dest, const void *src,
14384778508Sblueswir1                           unsigned long len);
14484778508Sblueswir1 void target_set_brk(abi_ulong new_brk);
14584778508Sblueswir1 abi_long do_brk(abi_ulong new_brk);
14684778508Sblueswir1 void syscall_init(void);
14784778508Sblueswir1 abi_long do_freebsd_syscall(void *cpu_env, int num, abi_long arg1,
14884778508Sblueswir1                             abi_long arg2, abi_long arg3, abi_long arg4,
14978cfb07fSJuergen Lock                             abi_long arg5, abi_long arg6, abi_long arg7,
15078cfb07fSJuergen Lock                             abi_long arg8);
15184778508Sblueswir1 abi_long do_netbsd_syscall(void *cpu_env, int num, abi_long arg1,
15284778508Sblueswir1                            abi_long arg2, abi_long arg3, abi_long arg4,
15384778508Sblueswir1                            abi_long arg5, abi_long arg6);
15484778508Sblueswir1 abi_long do_openbsd_syscall(void *cpu_env, int num, abi_long arg1,
15584778508Sblueswir1                             abi_long arg2, abi_long arg3, abi_long arg4,
15684778508Sblueswir1                             abi_long arg5, abi_long arg6);
157e5924d89SStefan Weil void gemu_log(const char *fmt, ...) GCC_FMT_ATTR(1, 2);
158dca1173cSAndreas Färber extern THREAD CPUState *thread_cpu;
1599349b4f9SAndreas Färber void cpu_loop(CPUArchState *env);
16084778508Sblueswir1 char *target_strerror(int err);
16184778508Sblueswir1 int get_osversion(void);
16284778508Sblueswir1 void fork_start(void);
16384778508Sblueswir1 void fork_end(int child);
16484778508Sblueswir1 
1651de7afc9SPaolo Bonzini #include "qemu/log.h"
16684778508Sblueswir1 
16784778508Sblueswir1 /* strace.c */
16888dae46dSSean Bruno struct syscallname {
16988dae46dSSean Bruno     int nr;
17088dae46dSSean Bruno     const char *name;
17188dae46dSSean Bruno     const char *format;
17288dae46dSSean Bruno     void (*call)(const struct syscallname *,
17388dae46dSSean Bruno                  abi_long, abi_long, abi_long,
17488dae46dSSean Bruno                  abi_long, abi_long, abi_long);
17588dae46dSSean Bruno     void (*result)(const struct syscallname *, abi_long);
17688dae46dSSean Bruno };
17788dae46dSSean Bruno 
17884778508Sblueswir1 void
17984778508Sblueswir1 print_freebsd_syscall(int num,
18084778508Sblueswir1                       abi_long arg1, abi_long arg2, abi_long arg3,
18184778508Sblueswir1                       abi_long arg4, abi_long arg5, abi_long arg6);
18284778508Sblueswir1 void print_freebsd_syscall_ret(int num, abi_long ret);
18384778508Sblueswir1 void
18484778508Sblueswir1 print_netbsd_syscall(int num,
18584778508Sblueswir1                      abi_long arg1, abi_long arg2, abi_long arg3,
18684778508Sblueswir1                      abi_long arg4, abi_long arg5, abi_long arg6);
18784778508Sblueswir1 void print_netbsd_syscall_ret(int num, abi_long ret);
18884778508Sblueswir1 void
18984778508Sblueswir1 print_openbsd_syscall(int num,
19084778508Sblueswir1                       abi_long arg1, abi_long arg2, abi_long arg3,
19184778508Sblueswir1                       abi_long arg4, abi_long arg5, abi_long arg6);
19284778508Sblueswir1 void print_openbsd_syscall_ret(int num, abi_long ret);
19384778508Sblueswir1 extern int do_strace;
19484778508Sblueswir1 
19584778508Sblueswir1 /* signal.c */
1969349b4f9SAndreas Färber void process_pending_signals(CPUArchState *cpu_env);
19784778508Sblueswir1 void signal_init(void);
1989349b4f9SAndreas Färber long do_sigreturn(CPUArchState *env);
1999349b4f9SAndreas Färber long do_rt_sigreturn(CPUArchState *env);
20084778508Sblueswir1 abi_long do_sigaltstack(abi_ulong uss_addr, abi_ulong uoss_addr, abi_ulong sp);
20184778508Sblueswir1 
20284778508Sblueswir1 /* mmap.c */
20384778508Sblueswir1 int target_mprotect(abi_ulong start, abi_ulong len, int prot);
20484778508Sblueswir1 abi_long target_mmap(abi_ulong start, abi_ulong len, int prot,
20584778508Sblueswir1                      int flags, int fd, abi_ulong offset);
20684778508Sblueswir1 int target_munmap(abi_ulong start, abi_ulong len);
20784778508Sblueswir1 abi_long target_mremap(abi_ulong old_addr, abi_ulong old_size,
20884778508Sblueswir1                        abi_ulong new_size, unsigned long flags,
20984778508Sblueswir1                        abi_ulong new_addr);
21084778508Sblueswir1 int target_msync(abi_ulong start, abi_ulong len, int flags);
21184778508Sblueswir1 extern unsigned long last_brk;
21284778508Sblueswir1 void mmap_fork_start(void);
21384778508Sblueswir1 void mmap_fork_end(int child);
21484778508Sblueswir1 
21528e738dcSBlue Swirl /* main.c */
21628e738dcSBlue Swirl extern unsigned long x86_stack_size;
21728e738dcSBlue Swirl 
21884778508Sblueswir1 /* user access */
21984778508Sblueswir1 
2201720751fSRichard Henderson #define VERIFY_READ  PAGE_READ
2211720751fSRichard Henderson #define VERIFY_WRITE (PAGE_READ | PAGE_WRITE)
22284778508Sblueswir1 
2231720751fSRichard Henderson static inline bool access_ok(int type, abi_ulong addr, abi_ulong size)
22484778508Sblueswir1 {
2251720751fSRichard Henderson     return page_check_range((target_ulong)addr, size, type) == 0;
22684778508Sblueswir1 }
22784778508Sblueswir1 
228*c2bdd9a1SWarner Losh /*
229*c2bdd9a1SWarner Losh  * NOTE __get_user and __put_user use host pointers and don't check access.
230*c2bdd9a1SWarner Losh  *
231*c2bdd9a1SWarner Losh  * These are usually used to access struct data members once the struct has been
232*c2bdd9a1SWarner Losh  * locked - usually with lock_user_struct().
23384778508Sblueswir1  */
23484778508Sblueswir1 #define __put_user(x, hptr)\
23584778508Sblueswir1 ({\
23684778508Sblueswir1     int size = sizeof(*hptr);\
23784778508Sblueswir1     switch (size) {\
23884778508Sblueswir1     case 1:\
23984778508Sblueswir1         *(uint8_t *)(hptr) = (uint8_t)(typeof(*hptr))(x);\
24084778508Sblueswir1         break;\
24184778508Sblueswir1     case 2:\
24284778508Sblueswir1         *(uint16_t *)(hptr) = tswap16((typeof(*hptr))(x));\
24384778508Sblueswir1         break;\
24484778508Sblueswir1     case 4:\
24584778508Sblueswir1         *(uint32_t *)(hptr) = tswap32((typeof(*hptr))(x));\
24684778508Sblueswir1         break;\
24784778508Sblueswir1     case 8:\
24884778508Sblueswir1         *(uint64_t *)(hptr) = tswap64((typeof(*hptr))(x));\
24984778508Sblueswir1         break;\
25084778508Sblueswir1     default:\
25184778508Sblueswir1         abort();\
25284778508Sblueswir1     } \
25384778508Sblueswir1     0;\
25484778508Sblueswir1 })
25584778508Sblueswir1 
25684778508Sblueswir1 #define __get_user(x, hptr) \
25784778508Sblueswir1 ({\
25884778508Sblueswir1     int size = sizeof(*hptr);\
25984778508Sblueswir1     switch (size) {\
26084778508Sblueswir1     case 1:\
26184778508Sblueswir1         x = (typeof(*hptr))*(uint8_t *)(hptr);\
26284778508Sblueswir1         break;\
26384778508Sblueswir1     case 2:\
26484778508Sblueswir1         x = (typeof(*hptr))tswap16(*(uint16_t *)(hptr));\
26584778508Sblueswir1         break;\
26684778508Sblueswir1     case 4:\
26784778508Sblueswir1         x = (typeof(*hptr))tswap32(*(uint32_t *)(hptr));\
26884778508Sblueswir1         break;\
26984778508Sblueswir1     case 8:\
27084778508Sblueswir1         x = (typeof(*hptr))tswap64(*(uint64_t *)(hptr));\
27184778508Sblueswir1         break;\
27284778508Sblueswir1     default:\
27384778508Sblueswir1         x = 0;\
27484778508Sblueswir1         abort();\
27584778508Sblueswir1     } \
27684778508Sblueswir1     0;\
27784778508Sblueswir1 })
27884778508Sblueswir1 
279*c2bdd9a1SWarner Losh /*
280*c2bdd9a1SWarner Losh  * put_user()/get_user() take a guest address and check access
281*c2bdd9a1SWarner Losh  *
282*c2bdd9a1SWarner Losh  * These are usually used to access an atomic data type, such as an int, that
283*c2bdd9a1SWarner Losh  * has been passed by address.  These internally perform locking and unlocking
284*c2bdd9a1SWarner Losh  * on the data type.
28584778508Sblueswir1  */
28684778508Sblueswir1 #define put_user(x, gaddr, target_type)                                 \
28784778508Sblueswir1 ({                                                                      \
28884778508Sblueswir1     abi_ulong __gaddr = (gaddr);                                        \
28984778508Sblueswir1     target_type *__hptr;                                                \
29084778508Sblueswir1     abi_long __ret;                                                     \
29184778508Sblueswir1     if ((__hptr = lock_user(VERIFY_WRITE, __gaddr, sizeof(target_type), 0))) { \
29284778508Sblueswir1         __ret = __put_user((x), __hptr);                                \
29384778508Sblueswir1         unlock_user(__hptr, __gaddr, sizeof(target_type));              \
29484778508Sblueswir1     } else                                                              \
29584778508Sblueswir1         __ret = -TARGET_EFAULT;                                         \
29684778508Sblueswir1     __ret;                                                              \
29784778508Sblueswir1 })
29884778508Sblueswir1 
29984778508Sblueswir1 #define get_user(x, gaddr, target_type)                                 \
30084778508Sblueswir1 ({                                                                      \
30184778508Sblueswir1     abi_ulong __gaddr = (gaddr);                                        \
30284778508Sblueswir1     target_type *__hptr;                                                \
30384778508Sblueswir1     abi_long __ret;                                                     \
30484778508Sblueswir1     if ((__hptr = lock_user(VERIFY_READ, __gaddr, sizeof(target_type), 1))) { \
30584778508Sblueswir1         __ret = __get_user((x), __hptr);                                \
30684778508Sblueswir1         unlock_user(__hptr, __gaddr, 0);                                \
30784778508Sblueswir1     } else {                                                            \
30884778508Sblueswir1         (x) = 0;                                                        \
30984778508Sblueswir1         __ret = -TARGET_EFAULT;                                         \
31084778508Sblueswir1     }                                                                   \
31184778508Sblueswir1     __ret;                                                              \
31284778508Sblueswir1 })
31384778508Sblueswir1 
31484778508Sblueswir1 #define put_user_ual(x, gaddr) put_user((x), (gaddr), abi_ulong)
31584778508Sblueswir1 #define put_user_sal(x, gaddr) put_user((x), (gaddr), abi_long)
31684778508Sblueswir1 #define put_user_u64(x, gaddr) put_user((x), (gaddr), uint64_t)
31784778508Sblueswir1 #define put_user_s64(x, gaddr) put_user((x), (gaddr), int64_t)
31884778508Sblueswir1 #define put_user_u32(x, gaddr) put_user((x), (gaddr), uint32_t)
31984778508Sblueswir1 #define put_user_s32(x, gaddr) put_user((x), (gaddr), int32_t)
32084778508Sblueswir1 #define put_user_u16(x, gaddr) put_user((x), (gaddr), uint16_t)
32184778508Sblueswir1 #define put_user_s16(x, gaddr) put_user((x), (gaddr), int16_t)
32284778508Sblueswir1 #define put_user_u8(x, gaddr)  put_user((x), (gaddr), uint8_t)
32384778508Sblueswir1 #define put_user_s8(x, gaddr)  put_user((x), (gaddr), int8_t)
32484778508Sblueswir1 
32584778508Sblueswir1 #define get_user_ual(x, gaddr) get_user((x), (gaddr), abi_ulong)
32684778508Sblueswir1 #define get_user_sal(x, gaddr) get_user((x), (gaddr), abi_long)
32784778508Sblueswir1 #define get_user_u64(x, gaddr) get_user((x), (gaddr), uint64_t)
32884778508Sblueswir1 #define get_user_s64(x, gaddr) get_user((x), (gaddr), int64_t)
32984778508Sblueswir1 #define get_user_u32(x, gaddr) get_user((x), (gaddr), uint32_t)
33084778508Sblueswir1 #define get_user_s32(x, gaddr) get_user((x), (gaddr), int32_t)
33184778508Sblueswir1 #define get_user_u16(x, gaddr) get_user((x), (gaddr), uint16_t)
33284778508Sblueswir1 #define get_user_s16(x, gaddr) get_user((x), (gaddr), int16_t)
33384778508Sblueswir1 #define get_user_u8(x, gaddr)  get_user((x), (gaddr), uint8_t)
33484778508Sblueswir1 #define get_user_s8(x, gaddr)  get_user((x), (gaddr), int8_t)
33584778508Sblueswir1 
336*c2bdd9a1SWarner Losh /*
337*c2bdd9a1SWarner Losh  * copy_from_user() and copy_to_user() are usually used to copy data
33884778508Sblueswir1  * buffers between the target and host.  These internally perform
33984778508Sblueswir1  * locking/unlocking of the memory.
34084778508Sblueswir1  */
34184778508Sblueswir1 abi_long copy_from_user(void *hptr, abi_ulong gaddr, size_t len);
34284778508Sblueswir1 abi_long copy_to_user(abi_ulong gaddr, void *hptr, size_t len);
34384778508Sblueswir1 
344*c2bdd9a1SWarner Losh /*
345*c2bdd9a1SWarner Losh  * Functions for accessing guest memory.  The tget and tput functions
346*c2bdd9a1SWarner Losh  * read/write single values, byteswapping as necessary.  The lock_user function
347*c2bdd9a1SWarner Losh  * gets a pointer to a contiguous area of guest memory, but does not perform
348*c2bdd9a1SWarner Losh  * any byteswapping.  lock_user may return either a pointer to the guest
349*c2bdd9a1SWarner Losh  * memory, or a temporary buffer.
350*c2bdd9a1SWarner Losh  */
35184778508Sblueswir1 
352*c2bdd9a1SWarner Losh /*
353*c2bdd9a1SWarner Losh  * Lock an area of guest memory into the host.  If copy is true then the
354*c2bdd9a1SWarner Losh  * host area will have the same contents as the guest.
355*c2bdd9a1SWarner Losh  */
356*c2bdd9a1SWarner Losh static inline void *lock_user(int type, abi_ulong guest_addr, long len,
357*c2bdd9a1SWarner Losh                               int copy)
35884778508Sblueswir1 {
35984778508Sblueswir1     if (!access_ok(type, guest_addr, len))
36084778508Sblueswir1         return NULL;
36184778508Sblueswir1 #ifdef DEBUG_REMAP
36284778508Sblueswir1     {
36384778508Sblueswir1         void *addr;
364fd9a3048SMd Haris Iqbal         addr = g_malloc(len);
36584778508Sblueswir1         if (copy)
3663e8f1628SRichard Henderson             memcpy(addr, g2h_untagged(guest_addr), len);
36784778508Sblueswir1         else
36884778508Sblueswir1             memset(addr, 0, len);
36984778508Sblueswir1         return addr;
37084778508Sblueswir1     }
37184778508Sblueswir1 #else
3723e8f1628SRichard Henderson     return g2h_untagged(guest_addr);
37384778508Sblueswir1 #endif
37484778508Sblueswir1 }
37584778508Sblueswir1 
376*c2bdd9a1SWarner Losh /*
377*c2bdd9a1SWarner Losh  * Unlock an area of guest memory.  The first LEN bytes must be flushed back to
378*c2bdd9a1SWarner Losh  * guest memory. host_ptr = NULL is explicitly allowed and does nothing.
379*c2bdd9a1SWarner Losh  */
38084778508Sblueswir1 static inline void unlock_user(void *host_ptr, abi_ulong guest_addr,
38184778508Sblueswir1                                long len)
38284778508Sblueswir1 {
38384778508Sblueswir1 
38484778508Sblueswir1 #ifdef DEBUG_REMAP
38584778508Sblueswir1     if (!host_ptr)
38684778508Sblueswir1         return;
3873e8f1628SRichard Henderson     if (host_ptr == g2h_untagged(guest_addr))
38884778508Sblueswir1         return;
38984778508Sblueswir1     if (len > 0)
3903e8f1628SRichard Henderson         memcpy(g2h_untagged(guest_addr), host_ptr, len);
391fd9a3048SMd Haris Iqbal     g_free(host_ptr);
39284778508Sblueswir1 #endif
39384778508Sblueswir1 }
39484778508Sblueswir1 
395*c2bdd9a1SWarner Losh /*
396*c2bdd9a1SWarner Losh  * Return the length of a string in target memory or -TARGET_EFAULT if access
397*c2bdd9a1SWarner Losh  * error.
398*c2bdd9a1SWarner Losh  */
39984778508Sblueswir1 abi_long target_strlen(abi_ulong gaddr);
40084778508Sblueswir1 
40184778508Sblueswir1 /* Like lock_user but for null terminated strings.  */
40284778508Sblueswir1 static inline void *lock_user_string(abi_ulong guest_addr)
40384778508Sblueswir1 {
40484778508Sblueswir1     abi_long len;
40584778508Sblueswir1     len = target_strlen(guest_addr);
40684778508Sblueswir1     if (len < 0)
40784778508Sblueswir1         return NULL;
40884778508Sblueswir1     return lock_user(VERIFY_READ, guest_addr, (long)(len + 1), 1);
40984778508Sblueswir1 }
41084778508Sblueswir1 
41141d1af4dSStefan Weil /* Helper macros for locking/unlocking a target struct.  */
41284778508Sblueswir1 #define lock_user_struct(type, host_ptr, guest_addr, copy)      \
41384778508Sblueswir1     (host_ptr = lock_user(type, guest_addr, sizeof(*host_ptr), copy))
41484778508Sblueswir1 #define unlock_user_struct(host_ptr, guest_addr, copy)          \
41584778508Sblueswir1     unlock_user(host_ptr, guest_addr, (copy) ? sizeof(*host_ptr) : 0)
41684778508Sblueswir1 
4172f7bb878SJuan Quintela #if defined(CONFIG_USE_NPTL)
41884778508Sblueswir1 #include <pthread.h>
41984778508Sblueswir1 #endif
42084778508Sblueswir1 
42184778508Sblueswir1 #endif /* QEMU_H */
422