xref: /openbmc/qemu/bsd-user/qemu.h (revision 031fe7af)
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"
41022c62cbSPaolo Bonzini #include "exec/gdbstub.h"
4284778508Sblueswir1 
43c2bdd9a1SWarner Losh /*
44c2bdd9a1SWarner Losh  * This struct is used to hold certain information about the image.  Basically,
45c2bdd9a1SWarner Losh  * it replicates in user space what would be certain task_struct fields in the
46c2bdd9a1SWarner Losh  * kernel
4784778508Sblueswir1  */
4884778508Sblueswir1 struct image_info {
4984778508Sblueswir1     abi_ulong load_addr;
5084778508Sblueswir1     abi_ulong start_code;
5184778508Sblueswir1     abi_ulong end_code;
5284778508Sblueswir1     abi_ulong start_data;
5384778508Sblueswir1     abi_ulong end_data;
5484778508Sblueswir1     abi_ulong start_brk;
5584778508Sblueswir1     abi_ulong brk;
5684778508Sblueswir1     abi_ulong start_mmap;
5784778508Sblueswir1     abi_ulong mmap;
5884778508Sblueswir1     abi_ulong rss;
5984778508Sblueswir1     abi_ulong start_stack;
6084778508Sblueswir1     abi_ulong entry;
6184778508Sblueswir1     abi_ulong code_offset;
6284778508Sblueswir1     abi_ulong data_offset;
6384778508Sblueswir1 };
6484778508Sblueswir1 
6584778508Sblueswir1 #define MAX_SIGQUEUE_SIZE 1024
6684778508Sblueswir1 
6784778508Sblueswir1 struct sigqueue {
6884778508Sblueswir1     struct sigqueue *next;
6984778508Sblueswir1 };
7084778508Sblueswir1 
7184778508Sblueswir1 struct emulated_sigtable {
7284778508Sblueswir1     int pending; /* true if signal is pending */
7384778508Sblueswir1     struct sigqueue *first;
74c2bdd9a1SWarner Losh     /* in order to always have memory for the first signal, we put it here */
75c2bdd9a1SWarner Losh     struct sigqueue info;
7684778508Sblueswir1 };
7784778508Sblueswir1 
78c2bdd9a1SWarner Losh /*
79c2bdd9a1SWarner Losh  * NOTE: we force a big alignment so that the stack stored after is aligned too
80c2bdd9a1SWarner Losh  */
8184778508Sblueswir1 typedef struct TaskState {
82bd88c780SAlex Bennée     pid_t ts_tid;     /* tid (or pid) of this task */
83bd88c780SAlex Bennée 
8484778508Sblueswir1     struct TaskState *next;
85*031fe7afSWarner Losh     struct bsd_binprm *bprm;
8684778508Sblueswir1     int used; /* non zero if used */
8784778508Sblueswir1     struct image_info *info;
8884778508Sblueswir1 
8984778508Sblueswir1     struct emulated_sigtable sigtab[TARGET_NSIG];
9084778508Sblueswir1     struct sigqueue sigqueue_table[MAX_SIGQUEUE_SIZE]; /* siginfo queue */
9184778508Sblueswir1     struct sigqueue *first_free; /* first free siginfo queue entry */
9284778508Sblueswir1     int signal_pending; /* non zero if a signal may be pending */
9384778508Sblueswir1 
94f7795e40SPhilippe Mathieu-Daudé     uint8_t stack[];
9584778508Sblueswir1 } __attribute__((aligned(16))) TaskState;
9684778508Sblueswir1 
9784778508Sblueswir1 void init_task_state(TaskState *ts);
9884778508Sblueswir1 extern const char *qemu_uname_release;
992fa5d9baSBlue Swirl extern unsigned long mmap_min_addr;
10084778508Sblueswir1 
10184778508Sblueswir1 /*
10284778508Sblueswir1  * MAX_ARG_PAGES defines the number of pages allocated for arguments
10384778508Sblueswir1  * and envelope for the new program. 32 should suffice, this gives
10484778508Sblueswir1  * a maximum env+arg of 128kB w/4KB pages!
10584778508Sblueswir1  */
10684778508Sblueswir1 #define MAX_ARG_PAGES 32
10784778508Sblueswir1 
10884778508Sblueswir1 /*
10984778508Sblueswir1  * This structure is used to hold the arguments that are
11084778508Sblueswir1  * used when loading binaries.
11184778508Sblueswir1  */
112afcbcff8SWarner Losh struct bsd_binprm {
11384778508Sblueswir1         char buf[128];
11484778508Sblueswir1         void *page[MAX_ARG_PAGES];
11584778508Sblueswir1         abi_ulong p;
11684778508Sblueswir1         int fd;
11784778508Sblueswir1         int e_uid, e_gid;
11884778508Sblueswir1         int argc, envc;
11984778508Sblueswir1         char **argv;
12084778508Sblueswir1         char **envp;
1211b50ff64SWarner Losh         char *filename;         /* (Given) Name of binary */
1221b50ff64SWarner Losh         char *fullpath;         /* Full path of binary */
12384778508Sblueswir1 };
12484778508Sblueswir1 
12584778508Sblueswir1 void do_init_thread(struct target_pt_regs *regs, struct image_info *infop);
12684778508Sblueswir1 abi_ulong loader_build_argptr(int envc, int argc, abi_ulong sp,
127ffa03665SWarner Losh                               abi_ulong stringp);
12884778508Sblueswir1 int loader_exec(const char *filename, char **argv, char **envp,
129d37853f9SWarner Losh                 struct target_pt_regs *regs, struct image_info *infop,
130d37853f9SWarner Losh                 struct bsd_binprm *bprm);
13184778508Sblueswir1 
132afcbcff8SWarner Losh int load_elf_binary(struct bsd_binprm *bprm, struct target_pt_regs *regs,
13384778508Sblueswir1                     struct image_info *info);
134afcbcff8SWarner Losh int load_flt_binary(struct bsd_binprm *bprm, struct target_pt_regs *regs,
13584778508Sblueswir1                     struct image_info *info);
13684778508Sblueswir1 
13784778508Sblueswir1 abi_long memcpy_to_target(abi_ulong dest, const void *src,
13884778508Sblueswir1                           unsigned long len);
13984778508Sblueswir1 void target_set_brk(abi_ulong new_brk);
14084778508Sblueswir1 abi_long do_brk(abi_ulong new_brk);
14184778508Sblueswir1 void syscall_init(void);
14284778508Sblueswir1 abi_long do_freebsd_syscall(void *cpu_env, int num, abi_long arg1,
14384778508Sblueswir1                             abi_long arg2, abi_long arg3, abi_long arg4,
14478cfb07fSJuergen Lock                             abi_long arg5, abi_long arg6, abi_long arg7,
14578cfb07fSJuergen Lock                             abi_long arg8);
14684778508Sblueswir1 abi_long do_netbsd_syscall(void *cpu_env, int num, abi_long arg1,
14784778508Sblueswir1                            abi_long arg2, abi_long arg3, abi_long arg4,
14884778508Sblueswir1                            abi_long arg5, abi_long arg6);
14984778508Sblueswir1 abi_long do_openbsd_syscall(void *cpu_env, int num, abi_long arg1,
15084778508Sblueswir1                             abi_long arg2, abi_long arg3, abi_long arg4,
15184778508Sblueswir1                             abi_long arg5, abi_long arg6);
152e5924d89SStefan Weil void gemu_log(const char *fmt, ...) GCC_FMT_ATTR(1, 2);
153d42df502SWarner Losh extern __thread CPUState *thread_cpu;
1549349b4f9SAndreas Färber void cpu_loop(CPUArchState *env);
15584778508Sblueswir1 char *target_strerror(int err);
15684778508Sblueswir1 int get_osversion(void);
15784778508Sblueswir1 void fork_start(void);
15884778508Sblueswir1 void fork_end(int child);
15984778508Sblueswir1 
1601de7afc9SPaolo Bonzini #include "qemu/log.h"
16184778508Sblueswir1 
16284778508Sblueswir1 /* strace.c */
16388dae46dSSean Bruno struct syscallname {
16488dae46dSSean Bruno     int nr;
16588dae46dSSean Bruno     const char *name;
16688dae46dSSean Bruno     const char *format;
16788dae46dSSean Bruno     void (*call)(const struct syscallname *,
16888dae46dSSean Bruno                  abi_long, abi_long, abi_long,
16988dae46dSSean Bruno                  abi_long, abi_long, abi_long);
17088dae46dSSean Bruno     void (*result)(const struct syscallname *, abi_long);
17188dae46dSSean Bruno };
17288dae46dSSean Bruno 
17384778508Sblueswir1 void
17484778508Sblueswir1 print_freebsd_syscall(int num,
17584778508Sblueswir1                       abi_long arg1, abi_long arg2, abi_long arg3,
17684778508Sblueswir1                       abi_long arg4, abi_long arg5, abi_long arg6);
17784778508Sblueswir1 void print_freebsd_syscall_ret(int num, abi_long ret);
17884778508Sblueswir1 void
17984778508Sblueswir1 print_netbsd_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_netbsd_syscall_ret(int num, abi_long ret);
18384778508Sblueswir1 void
18484778508Sblueswir1 print_openbsd_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_openbsd_syscall_ret(int num, abi_long ret);
18884778508Sblueswir1 extern int do_strace;
18984778508Sblueswir1 
19084778508Sblueswir1 /* signal.c */
1919349b4f9SAndreas Färber void process_pending_signals(CPUArchState *cpu_env);
19284778508Sblueswir1 void signal_init(void);
1939349b4f9SAndreas Färber long do_sigreturn(CPUArchState *env);
1949349b4f9SAndreas Färber long do_rt_sigreturn(CPUArchState *env);
19584778508Sblueswir1 abi_long do_sigaltstack(abi_ulong uss_addr, abi_ulong uoss_addr, abi_ulong sp);
19684778508Sblueswir1 
19784778508Sblueswir1 /* mmap.c */
19884778508Sblueswir1 int target_mprotect(abi_ulong start, abi_ulong len, int prot);
19984778508Sblueswir1 abi_long target_mmap(abi_ulong start, abi_ulong len, int prot,
20084778508Sblueswir1                      int flags, int fd, abi_ulong offset);
20184778508Sblueswir1 int target_munmap(abi_ulong start, abi_ulong len);
20284778508Sblueswir1 abi_long target_mremap(abi_ulong old_addr, abi_ulong old_size,
20384778508Sblueswir1                        abi_ulong new_size, unsigned long flags,
20484778508Sblueswir1                        abi_ulong new_addr);
20584778508Sblueswir1 int target_msync(abi_ulong start, abi_ulong len, int flags);
20684778508Sblueswir1 extern unsigned long last_brk;
20784778508Sblueswir1 void mmap_fork_start(void);
20884778508Sblueswir1 void mmap_fork_end(int child);
20984778508Sblueswir1 
21028e738dcSBlue Swirl /* main.c */
21101a298a5SWarner Losh extern char qemu_proc_pathname[];
21228e738dcSBlue Swirl extern unsigned long x86_stack_size;
21328e738dcSBlue Swirl 
21484778508Sblueswir1 /* user access */
21584778508Sblueswir1 
2161720751fSRichard Henderson #define VERIFY_READ  PAGE_READ
2171720751fSRichard Henderson #define VERIFY_WRITE (PAGE_READ | PAGE_WRITE)
21884778508Sblueswir1 
2191720751fSRichard Henderson static inline bool access_ok(int type, abi_ulong addr, abi_ulong size)
22084778508Sblueswir1 {
2211720751fSRichard Henderson     return page_check_range((target_ulong)addr, size, type) == 0;
22284778508Sblueswir1 }
22384778508Sblueswir1 
224c2bdd9a1SWarner Losh /*
225c2bdd9a1SWarner Losh  * NOTE __get_user and __put_user use host pointers and don't check access.
226c2bdd9a1SWarner Losh  *
227c2bdd9a1SWarner Losh  * These are usually used to access struct data members once the struct has been
228c2bdd9a1SWarner Losh  * locked - usually with lock_user_struct().
22984778508Sblueswir1  */
23084778508Sblueswir1 #define __put_user(x, hptr)\
23184778508Sblueswir1 ({\
23284778508Sblueswir1     int size = sizeof(*hptr);\
23384778508Sblueswir1     switch (size) {\
23484778508Sblueswir1     case 1:\
23584778508Sblueswir1         *(uint8_t *)(hptr) = (uint8_t)(typeof(*hptr))(x);\
23684778508Sblueswir1         break;\
23784778508Sblueswir1     case 2:\
23884778508Sblueswir1         *(uint16_t *)(hptr) = tswap16((typeof(*hptr))(x));\
23984778508Sblueswir1         break;\
24084778508Sblueswir1     case 4:\
24184778508Sblueswir1         *(uint32_t *)(hptr) = tswap32((typeof(*hptr))(x));\
24284778508Sblueswir1         break;\
24384778508Sblueswir1     case 8:\
24484778508Sblueswir1         *(uint64_t *)(hptr) = tswap64((typeof(*hptr))(x));\
24584778508Sblueswir1         break;\
24684778508Sblueswir1     default:\
24784778508Sblueswir1         abort();\
24884778508Sblueswir1     } \
24984778508Sblueswir1     0;\
25084778508Sblueswir1 })
25184778508Sblueswir1 
25284778508Sblueswir1 #define __get_user(x, hptr) \
25384778508Sblueswir1 ({\
25484778508Sblueswir1     int size = sizeof(*hptr);\
25584778508Sblueswir1     switch (size) {\
25684778508Sblueswir1     case 1:\
25784778508Sblueswir1         x = (typeof(*hptr))*(uint8_t *)(hptr);\
25884778508Sblueswir1         break;\
25984778508Sblueswir1     case 2:\
26084778508Sblueswir1         x = (typeof(*hptr))tswap16(*(uint16_t *)(hptr));\
26184778508Sblueswir1         break;\
26284778508Sblueswir1     case 4:\
26384778508Sblueswir1         x = (typeof(*hptr))tswap32(*(uint32_t *)(hptr));\
26484778508Sblueswir1         break;\
26584778508Sblueswir1     case 8:\
26684778508Sblueswir1         x = (typeof(*hptr))tswap64(*(uint64_t *)(hptr));\
26784778508Sblueswir1         break;\
26884778508Sblueswir1     default:\
26984778508Sblueswir1         x = 0;\
27084778508Sblueswir1         abort();\
27184778508Sblueswir1     } \
27284778508Sblueswir1     0;\
27384778508Sblueswir1 })
27484778508Sblueswir1 
275c2bdd9a1SWarner Losh /*
276c2bdd9a1SWarner Losh  * put_user()/get_user() take a guest address and check access
277c2bdd9a1SWarner Losh  *
278c2bdd9a1SWarner Losh  * These are usually used to access an atomic data type, such as an int, that
279c2bdd9a1SWarner Losh  * has been passed by address.  These internally perform locking and unlocking
280c2bdd9a1SWarner Losh  * on the data type.
28184778508Sblueswir1  */
28284778508Sblueswir1 #define put_user(x, gaddr, target_type)                                 \
28384778508Sblueswir1 ({                                                                      \
28484778508Sblueswir1     abi_ulong __gaddr = (gaddr);                                        \
28584778508Sblueswir1     target_type *__hptr;                                                \
28684778508Sblueswir1     abi_long __ret;                                                     \
28733066934SWarner Losh     __hptr = lock_user(VERIFY_WRITE, __gaddr, sizeof(target_type), 0);  \
28833066934SWarner Losh     if (__hptr) {                                                       \
28984778508Sblueswir1         __ret = __put_user((x), __hptr);                                \
29084778508Sblueswir1         unlock_user(__hptr, __gaddr, sizeof(target_type));              \
29184778508Sblueswir1     } else                                                              \
29284778508Sblueswir1         __ret = -TARGET_EFAULT;                                         \
29384778508Sblueswir1     __ret;                                                              \
29484778508Sblueswir1 })
29584778508Sblueswir1 
29684778508Sblueswir1 #define get_user(x, gaddr, target_type)                                 \
29784778508Sblueswir1 ({                                                                      \
29884778508Sblueswir1     abi_ulong __gaddr = (gaddr);                                        \
29984778508Sblueswir1     target_type *__hptr;                                                \
30084778508Sblueswir1     abi_long __ret;                                                     \
30133066934SWarner Losh     __hptr = lock_user(VERIFY_READ, __gaddr, sizeof(target_type), 1);   \
30233066934SWarner Losh     if (__hptr) {                                                       \
30384778508Sblueswir1         __ret = __get_user((x), __hptr);                                \
30484778508Sblueswir1         unlock_user(__hptr, __gaddr, 0);                                \
30584778508Sblueswir1     } else {                                                            \
30684778508Sblueswir1         (x) = 0;                                                        \
30784778508Sblueswir1         __ret = -TARGET_EFAULT;                                         \
30884778508Sblueswir1     }                                                                   \
30984778508Sblueswir1     __ret;                                                              \
31084778508Sblueswir1 })
31184778508Sblueswir1 
31284778508Sblueswir1 #define put_user_ual(x, gaddr) put_user((x), (gaddr), abi_ulong)
31384778508Sblueswir1 #define put_user_sal(x, gaddr) put_user((x), (gaddr), abi_long)
31484778508Sblueswir1 #define put_user_u64(x, gaddr) put_user((x), (gaddr), uint64_t)
31584778508Sblueswir1 #define put_user_s64(x, gaddr) put_user((x), (gaddr), int64_t)
31684778508Sblueswir1 #define put_user_u32(x, gaddr) put_user((x), (gaddr), uint32_t)
31784778508Sblueswir1 #define put_user_s32(x, gaddr) put_user((x), (gaddr), int32_t)
31884778508Sblueswir1 #define put_user_u16(x, gaddr) put_user((x), (gaddr), uint16_t)
31984778508Sblueswir1 #define put_user_s16(x, gaddr) put_user((x), (gaddr), int16_t)
32084778508Sblueswir1 #define put_user_u8(x, gaddr)  put_user((x), (gaddr), uint8_t)
32184778508Sblueswir1 #define put_user_s8(x, gaddr)  put_user((x), (gaddr), int8_t)
32284778508Sblueswir1 
32384778508Sblueswir1 #define get_user_ual(x, gaddr) get_user((x), (gaddr), abi_ulong)
32484778508Sblueswir1 #define get_user_sal(x, gaddr) get_user((x), (gaddr), abi_long)
32584778508Sblueswir1 #define get_user_u64(x, gaddr) get_user((x), (gaddr), uint64_t)
32684778508Sblueswir1 #define get_user_s64(x, gaddr) get_user((x), (gaddr), int64_t)
32784778508Sblueswir1 #define get_user_u32(x, gaddr) get_user((x), (gaddr), uint32_t)
32884778508Sblueswir1 #define get_user_s32(x, gaddr) get_user((x), (gaddr), int32_t)
32984778508Sblueswir1 #define get_user_u16(x, gaddr) get_user((x), (gaddr), uint16_t)
33084778508Sblueswir1 #define get_user_s16(x, gaddr) get_user((x), (gaddr), int16_t)
33184778508Sblueswir1 #define get_user_u8(x, gaddr)  get_user((x), (gaddr), uint8_t)
33284778508Sblueswir1 #define get_user_s8(x, gaddr)  get_user((x), (gaddr), int8_t)
33384778508Sblueswir1 
334c2bdd9a1SWarner Losh /*
335c2bdd9a1SWarner Losh  * copy_from_user() and copy_to_user() are usually used to copy data
33684778508Sblueswir1  * buffers between the target and host.  These internally perform
33784778508Sblueswir1  * locking/unlocking of the memory.
33884778508Sblueswir1  */
33984778508Sblueswir1 abi_long copy_from_user(void *hptr, abi_ulong gaddr, size_t len);
34084778508Sblueswir1 abi_long copy_to_user(abi_ulong gaddr, void *hptr, size_t len);
34184778508Sblueswir1 
342c2bdd9a1SWarner Losh /*
343c2bdd9a1SWarner Losh  * Functions for accessing guest memory.  The tget and tput functions
344c2bdd9a1SWarner Losh  * read/write single values, byteswapping as necessary.  The lock_user function
345c2bdd9a1SWarner Losh  * gets a pointer to a contiguous area of guest memory, but does not perform
346c2bdd9a1SWarner Losh  * any byteswapping.  lock_user may return either a pointer to the guest
347c2bdd9a1SWarner Losh  * memory, or a temporary buffer.
348c2bdd9a1SWarner Losh  */
34984778508Sblueswir1 
350c2bdd9a1SWarner Losh /*
351c2bdd9a1SWarner Losh  * Lock an area of guest memory into the host.  If copy is true then the
352c2bdd9a1SWarner Losh  * host area will have the same contents as the guest.
353c2bdd9a1SWarner Losh  */
354c2bdd9a1SWarner Losh static inline void *lock_user(int type, abi_ulong guest_addr, long len,
355c2bdd9a1SWarner Losh                               int copy)
35684778508Sblueswir1 {
357cb0ea019SWarner Losh     if (!access_ok(type, guest_addr, len)) {
35884778508Sblueswir1         return NULL;
359cb0ea019SWarner Losh     }
36084778508Sblueswir1 #ifdef DEBUG_REMAP
36184778508Sblueswir1     {
36284778508Sblueswir1         void *addr;
363fd9a3048SMd Haris Iqbal         addr = g_malloc(len);
364cb0ea019SWarner Losh         if (copy) {
3653e8f1628SRichard Henderson             memcpy(addr, g2h_untagged(guest_addr), len);
366cb0ea019SWarner Losh         } else {
36784778508Sblueswir1             memset(addr, 0, len);
368cb0ea019SWarner Losh         }
36984778508Sblueswir1         return addr;
37084778508Sblueswir1     }
37184778508Sblueswir1 #else
3723e8f1628SRichard Henderson     return g2h_untagged(guest_addr);
37384778508Sblueswir1 #endif
37484778508Sblueswir1 }
37584778508Sblueswir1 
376c2bdd9a1SWarner Losh /*
377c2bdd9a1SWarner Losh  * Unlock an area of guest memory.  The first LEN bytes must be flushed back to
378c2bdd9a1SWarner Losh  * guest memory. host_ptr = NULL is explicitly allowed and does nothing.
379c2bdd9a1SWarner Losh  */
38084778508Sblueswir1 static inline void unlock_user(void *host_ptr, abi_ulong guest_addr,
38184778508Sblueswir1                                long len)
38284778508Sblueswir1 {
38384778508Sblueswir1 
38484778508Sblueswir1 #ifdef DEBUG_REMAP
385cb0ea019SWarner Losh     if (!host_ptr) {
38684778508Sblueswir1         return;
387cb0ea019SWarner Losh     }
388cb0ea019SWarner Losh     if (host_ptr == g2h_untagged(guest_addr)) {
38984778508Sblueswir1         return;
390cb0ea019SWarner Losh     }
391cb0ea019SWarner Losh     if (len > 0) {
3923e8f1628SRichard Henderson         memcpy(g2h_untagged(guest_addr), host_ptr, len);
393cb0ea019SWarner Losh     }
394fd9a3048SMd Haris Iqbal     g_free(host_ptr);
39584778508Sblueswir1 #endif
39684778508Sblueswir1 }
39784778508Sblueswir1 
398c2bdd9a1SWarner Losh /*
399c2bdd9a1SWarner Losh  * Return the length of a string in target memory or -TARGET_EFAULT if access
400c2bdd9a1SWarner Losh  * error.
401c2bdd9a1SWarner Losh  */
40284778508Sblueswir1 abi_long target_strlen(abi_ulong gaddr);
40384778508Sblueswir1 
40484778508Sblueswir1 /* Like lock_user but for null terminated strings.  */
40584778508Sblueswir1 static inline void *lock_user_string(abi_ulong guest_addr)
40684778508Sblueswir1 {
40784778508Sblueswir1     abi_long len;
40884778508Sblueswir1     len = target_strlen(guest_addr);
409cb0ea019SWarner Losh     if (len < 0) {
41084778508Sblueswir1         return NULL;
411cb0ea019SWarner Losh     }
41284778508Sblueswir1     return lock_user(VERIFY_READ, guest_addr, (long)(len + 1), 1);
41384778508Sblueswir1 }
41484778508Sblueswir1 
41541d1af4dSStefan Weil /* Helper macros for locking/unlocking a target struct.  */
41684778508Sblueswir1 #define lock_user_struct(type, host_ptr, guest_addr, copy)      \
41784778508Sblueswir1     (host_ptr = lock_user(type, guest_addr, sizeof(*host_ptr), copy))
41884778508Sblueswir1 #define unlock_user_struct(host_ptr, guest_addr, copy)          \
41984778508Sblueswir1     unlock_user(host_ptr, guest_addr, (copy) ? sizeof(*host_ptr) : 0)
42084778508Sblueswir1 
42184778508Sblueswir1 #include <pthread.h>
42284778508Sblueswir1 
42384778508Sblueswir1 #endif /* QEMU_H */
424