Statistics
| Branch: | Revision:

root / linux-user / qemu.h @ 1a14026e

History | View | Annotate | Download (13.3 kB)

1 e88de099 bellard
#ifndef QEMU_H
2 e88de099 bellard
#define QEMU_H
3 31e31b8a bellard
4 9de5e440 bellard
#include <signal.h>
5 edf779ff bellard
#include <string.h>
6 31e31b8a bellard
7 6180a181 bellard
#include "cpu.h"
8 992f48a0 blueswir1
9 06177d36 balrog
#undef DEBUG_REMAP
10 06177d36 balrog
#ifdef DEBUG_REMAP
11 06177d36 balrog
#include <stdlib.h>
12 06177d36 balrog
#endif /* DEBUG_REMAP */
13 06177d36 balrog
14 992f48a0 blueswir1
#ifdef TARGET_ABI32
15 992f48a0 blueswir1
typedef uint32_t abi_ulong;
16 992f48a0 blueswir1
typedef int32_t abi_long;
17 f3e3285d ths
#define TARGET_ABI_FMT_lx "%08x"
18 f3e3285d ths
#define TARGET_ABI_FMT_ld "%d"
19 f3e3285d ths
#define TARGET_ABI_FMT_lu "%u"
20 992f48a0 blueswir1
#define TARGET_ABI_BITS 32
21 992f48a0 blueswir1
#else
22 992f48a0 blueswir1
typedef target_ulong abi_ulong;
23 992f48a0 blueswir1
typedef target_long abi_long;
24 f3e3285d ths
#define TARGET_ABI_FMT_lx TARGET_FMT_lx
25 f3e3285d ths
#define TARGET_ABI_FMT_ld TARGET_FMT_ld
26 f3e3285d ths
#define TARGET_ABI_FMT_lu TARGET_FMT_lu
27 992f48a0 blueswir1
#define TARGET_ABI_BITS TARGET_LONG_BITS
28 4683b130 bellard
/* for consistency, define ABI32 too */
29 4683b130 bellard
#if TARGET_ABI_BITS == 32
30 4683b130 bellard
#define TARGET_ABI32 1
31 4683b130 bellard
#endif
32 992f48a0 blueswir1
#endif
33 992f48a0 blueswir1
34 992f48a0 blueswir1
#include "thunk.h"
35 992f48a0 blueswir1
#include "syscall_defs.h"
36 6180a181 bellard
#include "syscall.h"
37 a04e134a ths
#include "target_signal.h"
38 1fddef4b bellard
#include "gdbstub.h"
39 66fb9763 bellard
40 31e31b8a bellard
/* This struct is used to hold certain information about the image.
41 31e31b8a bellard
 * Basically, it replicates in user space what would be certain
42 31e31b8a bellard
 * task_struct fields in the kernel
43 31e31b8a bellard
 */
44 31e31b8a bellard
struct image_info {
45 992f48a0 blueswir1
        abi_ulong       load_addr;
46 992f48a0 blueswir1
        abi_ulong       start_code;
47 992f48a0 blueswir1
        abi_ulong       end_code;
48 992f48a0 blueswir1
        abi_ulong       start_data;
49 992f48a0 blueswir1
        abi_ulong       end_data;
50 992f48a0 blueswir1
        abi_ulong       start_brk;
51 992f48a0 blueswir1
        abi_ulong       brk;
52 992f48a0 blueswir1
        abi_ulong       start_mmap;
53 992f48a0 blueswir1
        abi_ulong       mmap;
54 992f48a0 blueswir1
        abi_ulong       rss;
55 992f48a0 blueswir1
        abi_ulong       start_stack;
56 992f48a0 blueswir1
        abi_ulong       entry;
57 992f48a0 blueswir1
        abi_ulong       code_offset;
58 992f48a0 blueswir1
        abi_ulong       data_offset;
59 38d0662a pbrook
        char            **host_argv;
60 31e31b8a bellard
        int                personality;
61 31e31b8a bellard
};
62 31e31b8a bellard
63 b346ff46 bellard
#ifdef TARGET_I386
64 851e67a1 bellard
/* Information about the current linux thread */
65 851e67a1 bellard
struct vm86_saved_state {
66 851e67a1 bellard
    uint32_t eax; /* return code */
67 851e67a1 bellard
    uint32_t ebx;
68 851e67a1 bellard
    uint32_t ecx;
69 851e67a1 bellard
    uint32_t edx;
70 851e67a1 bellard
    uint32_t esi;
71 851e67a1 bellard
    uint32_t edi;
72 851e67a1 bellard
    uint32_t ebp;
73 851e67a1 bellard
    uint32_t esp;
74 851e67a1 bellard
    uint32_t eflags;
75 851e67a1 bellard
    uint32_t eip;
76 851e67a1 bellard
    uint16_t cs, ss, ds, es, fs, gs;
77 851e67a1 bellard
};
78 b346ff46 bellard
#endif
79 851e67a1 bellard
80 28c4f361 bellard
#ifdef TARGET_ARM
81 28c4f361 bellard
/* FPU emulator */
82 28c4f361 bellard
#include "nwfpe/fpa11.h"
83 28c4f361 bellard
#endif
84 28c4f361 bellard
85 624f7979 pbrook
#define MAX_SIGQUEUE_SIZE 1024
86 624f7979 pbrook
87 624f7979 pbrook
struct sigqueue {
88 624f7979 pbrook
    struct sigqueue *next;
89 624f7979 pbrook
    target_siginfo_t info;
90 624f7979 pbrook
};
91 624f7979 pbrook
92 624f7979 pbrook
struct emulated_sigtable {
93 624f7979 pbrook
    int pending; /* true if signal is pending */
94 624f7979 pbrook
    struct sigqueue *first;
95 624f7979 pbrook
    struct sigqueue info; /* in order to always have memory for the
96 624f7979 pbrook
                             first signal, we put it here */
97 624f7979 pbrook
};
98 624f7979 pbrook
99 851e67a1 bellard
/* NOTE: we force a big alignment so that the stack stored after is
100 851e67a1 bellard
   aligned too */
101 851e67a1 bellard
typedef struct TaskState {
102 851e67a1 bellard
    struct TaskState *next;
103 28c4f361 bellard
#ifdef TARGET_ARM
104 28c4f361 bellard
    /* FPA state */
105 28c4f361 bellard
    FPA11 fpa;
106 a4f81979 bellard
    int swi_errno;
107 28c4f361 bellard
#endif
108 84409ddb j_mayer
#if defined(TARGET_I386) && !defined(TARGET_X86_64)
109 992f48a0 blueswir1
    abi_ulong target_v86;
110 851e67a1 bellard
    struct vm86_saved_state vm86_saved_regs;
111 b333af06 bellard
    struct target_vm86plus_struct vm86plus;
112 631271d7 bellard
    uint32_t v86flags;
113 631271d7 bellard
    uint32_t v86mask;
114 b346ff46 bellard
#endif
115 e6e5906b pbrook
#ifdef TARGET_M68K
116 e6e5906b pbrook
    int sim_syscalls;
117 e6e5906b pbrook
#endif
118 a87295e8 pbrook
#if defined(TARGET_ARM) || defined(TARGET_M68K)
119 a87295e8 pbrook
    /* Extra fields for semihosted binaries.  */
120 a87295e8 pbrook
    uint32_t stack_base;
121 a87295e8 pbrook
    uint32_t heap_base;
122 a87295e8 pbrook
    uint32_t heap_limit;
123 a87295e8 pbrook
#endif
124 851e67a1 bellard
    int used; /* non zero if used */
125 978efd6a pbrook
    struct image_info *info;
126 624f7979 pbrook
127 624f7979 pbrook
    struct emulated_sigtable sigtab[TARGET_NSIG];
128 624f7979 pbrook
    struct sigqueue sigqueue_table[MAX_SIGQUEUE_SIZE]; /* siginfo queue */
129 624f7979 pbrook
    struct sigqueue *first_free; /* first free siginfo queue entry */
130 624f7979 pbrook
    int signal_pending; /* non zero if a signal may be pending */
131 624f7979 pbrook
132 851e67a1 bellard
    uint8_t stack[0];
133 851e67a1 bellard
} __attribute__((aligned(16))) TaskState;
134 851e67a1 bellard
135 624f7979 pbrook
void init_task_state(TaskState *ts);
136 c5937220 pbrook
extern const char *qemu_uname_release;
137 851e67a1 bellard
138 e5fe0c52 pbrook
/* ??? See if we can avoid exposing so much of the loader internals.  */
139 e5fe0c52 pbrook
/*
140 e5fe0c52 pbrook
 * MAX_ARG_PAGES defines the number of pages allocated for arguments
141 e5fe0c52 pbrook
 * and envelope for the new program. 32 should suffice, this gives
142 e5fe0c52 pbrook
 * a maximum env+arg of 128kB w/4KB pages!
143 e5fe0c52 pbrook
 */
144 e5fe0c52 pbrook
#define MAX_ARG_PAGES 32
145 e5fe0c52 pbrook
146 e5fe0c52 pbrook
/*
147 5fafdf24 ths
 * This structure is used to hold the arguments that are
148 e5fe0c52 pbrook
 * used when loading binaries.
149 e5fe0c52 pbrook
 */
150 e5fe0c52 pbrook
struct linux_binprm {
151 e5fe0c52 pbrook
        char buf[128];
152 e5fe0c52 pbrook
        void *page[MAX_ARG_PAGES];
153 992f48a0 blueswir1
        abi_ulong p;
154 e5fe0c52 pbrook
        int fd;
155 e5fe0c52 pbrook
        int e_uid, e_gid;
156 e5fe0c52 pbrook
        int argc, envc;
157 e5fe0c52 pbrook
        char **argv;
158 e5fe0c52 pbrook
        char **envp;
159 e5fe0c52 pbrook
        char * filename;        /* Name of binary */
160 e5fe0c52 pbrook
};
161 e5fe0c52 pbrook
162 e5fe0c52 pbrook
void do_init_thread(struct target_pt_regs *regs, struct image_info *infop);
163 992f48a0 blueswir1
abi_ulong loader_build_argptr(int envc, int argc, abi_ulong sp,
164 992f48a0 blueswir1
                              abi_ulong stringp, int push_ptr);
165 5fafdf24 ths
int loader_exec(const char * filename, char ** argv, char ** envp,
166 01ffc75b bellard
             struct target_pt_regs * regs, struct image_info *infop);
167 31e31b8a bellard
168 e5fe0c52 pbrook
int load_elf_binary(struct linux_binprm * bprm, struct target_pt_regs * regs,
169 e5fe0c52 pbrook
                    struct image_info * info);
170 e5fe0c52 pbrook
int load_flt_binary(struct linux_binprm * bprm, struct target_pt_regs * regs,
171 e5fe0c52 pbrook
                    struct image_info * info);
172 cb33da57 blueswir1
#ifdef TARGET_HAS_ELFLOAD32
173 cb33da57 blueswir1
int load_elf_binary_multi(struct linux_binprm *bprm,
174 cb33da57 blueswir1
                          struct target_pt_regs *regs,
175 cb33da57 blueswir1
                          struct image_info *info);
176 cb33da57 blueswir1
#endif
177 e5fe0c52 pbrook
178 579a97f7 bellard
abi_long memcpy_to_target(abi_ulong dest, const void *src,
179 579a97f7 bellard
                          unsigned long len);
180 992f48a0 blueswir1
void target_set_brk(abi_ulong new_brk);
181 992f48a0 blueswir1
abi_long do_brk(abi_ulong new_brk);
182 31e31b8a bellard
void syscall_init(void);
183 992f48a0 blueswir1
abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
184 992f48a0 blueswir1
                    abi_long arg2, abi_long arg3, abi_long arg4,
185 992f48a0 blueswir1
                    abi_long arg5, abi_long arg6);
186 31e31b8a bellard
void gemu_log(const char *fmt, ...) __attribute__((format(printf,1,2)));
187 b346ff46 bellard
extern CPUState *global_env;
188 b346ff46 bellard
void cpu_loop(CPUState *env);
189 32ce6337 bellard
void init_paths(const char *prefix);
190 32ce6337 bellard
const char *path(const char *pathname);
191 b92c47c1 ths
char *target_strerror(int err);
192 a745ec6d pbrook
int get_osversion(void);
193 6977fbfd bellard
194 6977fbfd bellard
extern int loglevel;
195 631271d7 bellard
extern FILE *logfile;
196 631271d7 bellard
197 b92c47c1 ths
/* strace.c */
198 b92c47c1 ths
void print_syscall(int num,
199 c16f9ed3 bellard
                   abi_long arg1, abi_long arg2, abi_long arg3,
200 c16f9ed3 bellard
                   abi_long arg4, abi_long arg5, abi_long arg6);
201 c16f9ed3 bellard
void print_syscall_ret(int num, abi_long arg1);
202 b92c47c1 ths
extern int do_strace;
203 b92c47c1 ths
204 b346ff46 bellard
/* signal.c */
205 624f7979 pbrook
void process_pending_signals(CPUState *cpu_env);
206 b346ff46 bellard
void signal_init(void);
207 624f7979 pbrook
int queue_signal(CPUState *env, int sig, target_siginfo_t *info);
208 b346ff46 bellard
void host_to_target_siginfo(target_siginfo_t *tinfo, const siginfo_t *info);
209 b346ff46 bellard
void target_to_host_siginfo(siginfo_t *info, const target_siginfo_t *tinfo);
210 4cb05961 pbrook
int target_to_host_signal(int sig);
211 b346ff46 bellard
long do_sigreturn(CPUState *env);
212 b346ff46 bellard
long do_rt_sigreturn(CPUState *env);
213 579a97f7 bellard
abi_long do_sigaltstack(abi_ulong uss_addr, abi_ulong uoss_addr, abi_ulong sp);
214 b346ff46 bellard
215 b346ff46 bellard
#ifdef TARGET_I386
216 631271d7 bellard
/* vm86.c */
217 631271d7 bellard
void save_v86_state(CPUX86State *env);
218 447db213 bellard
void handle_vm86_trap(CPUX86State *env, int trapno);
219 631271d7 bellard
void handle_vm86_fault(CPUX86State *env);
220 992f48a0 blueswir1
int do_vm86(CPUX86State *env, long subfunction, abi_ulong v86_addr);
221 5bfb56b2 blueswir1
#elif defined(TARGET_SPARC64)
222 5bfb56b2 blueswir1
void sparc64_set_context(CPUSPARCState *env);
223 5bfb56b2 blueswir1
void sparc64_get_context(CPUSPARCState *env);
224 b346ff46 bellard
#endif
225 631271d7 bellard
226 54936004 bellard
/* mmap.c */
227 992f48a0 blueswir1
int target_mprotect(abi_ulong start, abi_ulong len, int prot);
228 992f48a0 blueswir1
abi_long target_mmap(abi_ulong start, abi_ulong len, int prot,
229 992f48a0 blueswir1
                     int flags, int fd, abi_ulong offset);
230 992f48a0 blueswir1
int target_munmap(abi_ulong start, abi_ulong len);
231 992f48a0 blueswir1
abi_long target_mremap(abi_ulong old_addr, abi_ulong old_size,
232 992f48a0 blueswir1
                       abi_ulong new_size, unsigned long flags,
233 992f48a0 blueswir1
                       abi_ulong new_addr);
234 992f48a0 blueswir1
int target_msync(abi_ulong start, abi_ulong len, int flags);
235 0776590d pbrook
extern unsigned long last_brk;
236 c8a706fe pbrook
void mmap_lock(void);
237 c8a706fe pbrook
void mmap_unlock(void);
238 54936004 bellard
239 edf779ff bellard
/* user access */
240 edf779ff bellard
241 edf779ff bellard
#define VERIFY_READ 0
242 579a97f7 bellard
#define VERIFY_WRITE 1 /* implies read access */
243 edf779ff bellard
244 dae3270c bellard
static inline int access_ok(int type, abi_ulong addr, abi_ulong size)
245 dae3270c bellard
{
246 dae3270c bellard
    return page_check_range((target_ulong)addr, size,
247 dae3270c bellard
                            (type == VERIFY_READ) ? PAGE_READ : (PAGE_READ | PAGE_WRITE)) == 0;
248 dae3270c bellard
}
249 edf779ff bellard
250 89343ecd ths
/* NOTE __get_user and __put_user use host pointers and don't check access. */
251 579a97f7 bellard
/* These are usually used to access struct data members once the
252 579a97f7 bellard
 * struct has been locked - usually with lock_user_struct().
253 579a97f7 bellard
 */
254 89343ecd ths
#define __put_user(x, hptr)\
255 edf779ff bellard
({\
256 89343ecd ths
    int size = sizeof(*hptr);\
257 edf779ff bellard
    switch(size) {\
258 edf779ff bellard
    case 1:\
259 2f619698 bellard
        *(uint8_t *)(hptr) = (uint8_t)(typeof(*hptr))(x);\
260 edf779ff bellard
        break;\
261 edf779ff bellard
    case 2:\
262 89343ecd ths
        *(uint16_t *)(hptr) = tswap16((typeof(*hptr))(x));\
263 edf779ff bellard
        break;\
264 edf779ff bellard
    case 4:\
265 89343ecd ths
        *(uint32_t *)(hptr) = tswap32((typeof(*hptr))(x));\
266 edf779ff bellard
        break;\
267 edf779ff bellard
    case 8:\
268 89343ecd ths
        *(uint64_t *)(hptr) = tswap64((typeof(*hptr))(x));\
269 edf779ff bellard
        break;\
270 edf779ff bellard
    default:\
271 edf779ff bellard
        abort();\
272 edf779ff bellard
    }\
273 edf779ff bellard
    0;\
274 edf779ff bellard
})
275 edf779ff bellard
276 89343ecd ths
#define __get_user(x, hptr) \
277 edf779ff bellard
({\
278 89343ecd ths
    int size = sizeof(*hptr);\
279 edf779ff bellard
    switch(size) {\
280 edf779ff bellard
    case 1:\
281 89343ecd ths
        x = (typeof(*hptr))*(uint8_t *)(hptr);\
282 edf779ff bellard
        break;\
283 edf779ff bellard
    case 2:\
284 89343ecd ths
        x = (typeof(*hptr))tswap16(*(uint16_t *)(hptr));\
285 edf779ff bellard
        break;\
286 edf779ff bellard
    case 4:\
287 89343ecd ths
        x = (typeof(*hptr))tswap32(*(uint32_t *)(hptr));\
288 edf779ff bellard
        break;\
289 edf779ff bellard
    case 8:\
290 89343ecd ths
        x = (typeof(*hptr))tswap64(*(uint64_t *)(hptr));\
291 edf779ff bellard
        break;\
292 edf779ff bellard
    default:\
293 2f619698 bellard
        /* avoid warning */\
294 2f619698 bellard
        x = 0;\
295 edf779ff bellard
        abort();\
296 edf779ff bellard
    }\
297 edf779ff bellard
    0;\
298 edf779ff bellard
})
299 edf779ff bellard
300 579a97f7 bellard
/* put_user()/get_user() take a guest address and check access */
301 579a97f7 bellard
/* These are usually used to access an atomic data type, such as an int,
302 579a97f7 bellard
 * that has been passed by address.  These internally perform locking
303 579a97f7 bellard
 * and unlocking on the data type.
304 579a97f7 bellard
 */
305 579a97f7 bellard
#define put_user(x, gaddr, target_type)                                        \
306 579a97f7 bellard
({                                                                        \
307 579a97f7 bellard
    abi_ulong __gaddr = (gaddr);                                        \
308 579a97f7 bellard
    target_type *__hptr;                                                \
309 579a97f7 bellard
    abi_long __ret;                                                        \
310 579a97f7 bellard
    if ((__hptr = lock_user(VERIFY_WRITE, __gaddr, sizeof(target_type), 0))) { \
311 579a97f7 bellard
        __ret = __put_user((x), __hptr);                                \
312 579a97f7 bellard
        unlock_user(__hptr, __gaddr, sizeof(target_type));                \
313 579a97f7 bellard
    } else                                                                \
314 579a97f7 bellard
        __ret = -TARGET_EFAULT;                                                \
315 579a97f7 bellard
    __ret;                                                                \
316 edf779ff bellard
})
317 edf779ff bellard
318 579a97f7 bellard
#define get_user(x, gaddr, target_type)                                        \
319 579a97f7 bellard
({                                                                        \
320 579a97f7 bellard
    abi_ulong __gaddr = (gaddr);                                        \
321 579a97f7 bellard
    target_type *__hptr;                                                \
322 579a97f7 bellard
    abi_long __ret;                                                        \
323 579a97f7 bellard
    if ((__hptr = lock_user(VERIFY_READ, __gaddr, sizeof(target_type), 1))) { \
324 579a97f7 bellard
        __ret = __get_user((x), __hptr);                                \
325 579a97f7 bellard
        unlock_user(__hptr, __gaddr, 0);                                \
326 2f619698 bellard
    } else {                                                                \
327 2f619698 bellard
        /* avoid warning */                                                \
328 2f619698 bellard
        (x) = 0;                                                        \
329 579a97f7 bellard
        __ret = -TARGET_EFAULT;                                                \
330 2f619698 bellard
    }                                                                        \
331 579a97f7 bellard
    __ret;                                                                \
332 edf779ff bellard
})
333 edf779ff bellard
334 2f619698 bellard
#define put_user_ual(x, gaddr) put_user((x), (gaddr), abi_ulong)
335 2f619698 bellard
#define put_user_sal(x, gaddr) put_user((x), (gaddr), abi_long)
336 2f619698 bellard
#define put_user_u64(x, gaddr) put_user((x), (gaddr), uint64_t)
337 2f619698 bellard
#define put_user_s64(x, gaddr) put_user((x), (gaddr), int64_t)
338 2f619698 bellard
#define put_user_u32(x, gaddr) put_user((x), (gaddr), uint32_t)
339 2f619698 bellard
#define put_user_s32(x, gaddr) put_user((x), (gaddr), int32_t)
340 2f619698 bellard
#define put_user_u16(x, gaddr) put_user((x), (gaddr), uint16_t)
341 2f619698 bellard
#define put_user_s16(x, gaddr) put_user((x), (gaddr), int16_t)
342 2f619698 bellard
#define put_user_u8(x, gaddr)  put_user((x), (gaddr), uint8_t)
343 2f619698 bellard
#define put_user_s8(x, gaddr)  put_user((x), (gaddr), int8_t)
344 2f619698 bellard
345 2f619698 bellard
#define get_user_ual(x, gaddr) get_user((x), (gaddr), abi_ulong)
346 2f619698 bellard
#define get_user_sal(x, gaddr) get_user((x), (gaddr), abi_long)
347 2f619698 bellard
#define get_user_u64(x, gaddr) get_user((x), (gaddr), uint64_t)
348 2f619698 bellard
#define get_user_s64(x, gaddr) get_user((x), (gaddr), int64_t)
349 2f619698 bellard
#define get_user_u32(x, gaddr) get_user((x), (gaddr), uint32_t)
350 2f619698 bellard
#define get_user_s32(x, gaddr) get_user((x), (gaddr), int32_t)
351 2f619698 bellard
#define get_user_u16(x, gaddr) get_user((x), (gaddr), uint16_t)
352 2f619698 bellard
#define get_user_s16(x, gaddr) get_user((x), (gaddr), int16_t)
353 2f619698 bellard
#define get_user_u8(x, gaddr)  get_user((x), (gaddr), uint8_t)
354 2f619698 bellard
#define get_user_s8(x, gaddr)  get_user((x), (gaddr), int8_t)
355 2f619698 bellard
356 579a97f7 bellard
/* copy_from_user() and copy_to_user() are usually used to copy data
357 579a97f7 bellard
 * buffers between the target and host.  These internally perform
358 579a97f7 bellard
 * locking/unlocking of the memory.
359 579a97f7 bellard
 */
360 579a97f7 bellard
abi_long copy_from_user(void *hptr, abi_ulong gaddr, size_t len);
361 579a97f7 bellard
abi_long copy_to_user(abi_ulong gaddr, void *hptr, size_t len);
362 579a97f7 bellard
363 53a5960a pbrook
/* Functions for accessing guest memory.  The tget and tput functions
364 53a5960a pbrook
   read/write single values, byteswapping as neccessary.  The lock_user
365 53a5960a pbrook
   gets a pointer to a contiguous area of guest memory, but does not perform
366 53a5960a pbrook
   and byteswapping.  lock_user may return either a pointer to the guest
367 53a5960a pbrook
   memory, or a temporary buffer.  */
368 53a5960a pbrook
369 53a5960a pbrook
/* Lock an area of guest memory into the host.  If copy is true then the
370 53a5960a pbrook
   host area will have the same contents as the guest.  */
371 579a97f7 bellard
static inline void *lock_user(int type, abi_ulong guest_addr, long len, int copy)
372 edf779ff bellard
{
373 579a97f7 bellard
    if (!access_ok(type, guest_addr, len))
374 579a97f7 bellard
        return NULL;
375 53a5960a pbrook
#ifdef DEBUG_REMAP
376 579a97f7 bellard
    {
377 579a97f7 bellard
        void *addr;
378 579a97f7 bellard
        addr = malloc(len);
379 579a97f7 bellard
        if (copy)
380 579a97f7 bellard
            memcpy(addr, g2h(guest_addr), len);
381 579a97f7 bellard
        else
382 579a97f7 bellard
            memset(addr, 0, len);
383 579a97f7 bellard
        return addr;
384 579a97f7 bellard
    }
385 53a5960a pbrook
#else
386 53a5960a pbrook
    return g2h(guest_addr);
387 53a5960a pbrook
#endif
388 edf779ff bellard
}
389 edf779ff bellard
390 579a97f7 bellard
/* Unlock an area of guest memory.  The first LEN bytes must be
391 1235fc06 ths
   flushed back to guest memory. host_ptr = NULL is explicitly
392 579a97f7 bellard
   allowed and does nothing. */
393 579a97f7 bellard
static inline void unlock_user(void *host_ptr, abi_ulong guest_addr,
394 992f48a0 blueswir1
                               long len)
395 edf779ff bellard
{
396 579a97f7 bellard
397 53a5960a pbrook
#ifdef DEBUG_REMAP
398 579a97f7 bellard
    if (!host_ptr)
399 579a97f7 bellard
        return;
400 579a97f7 bellard
    if (host_ptr == g2h(guest_addr))
401 53a5960a pbrook
        return;
402 53a5960a pbrook
    if (len > 0)
403 06177d36 balrog
        memcpy(g2h(guest_addr), host_ptr, len);
404 579a97f7 bellard
    free(host_ptr);
405 53a5960a pbrook
#endif
406 edf779ff bellard
}
407 edf779ff bellard
408 579a97f7 bellard
/* Return the length of a string in target memory or -TARGET_EFAULT if
409 579a97f7 bellard
   access error. */
410 579a97f7 bellard
abi_long target_strlen(abi_ulong gaddr);
411 53a5960a pbrook
412 53a5960a pbrook
/* Like lock_user but for null terminated strings.  */
413 992f48a0 blueswir1
static inline void *lock_user_string(abi_ulong guest_addr)
414 53a5960a pbrook
{
415 579a97f7 bellard
    abi_long len;
416 579a97f7 bellard
    len = target_strlen(guest_addr);
417 579a97f7 bellard
    if (len < 0)
418 579a97f7 bellard
        return NULL;
419 579a97f7 bellard
    return lock_user(VERIFY_READ, guest_addr, (long)(len + 1), 1);
420 edf779ff bellard
}
421 edf779ff bellard
422 53a5960a pbrook
/* Helper macros for locking/ulocking a target struct.  */
423 579a97f7 bellard
#define lock_user_struct(type, host_ptr, guest_addr, copy)        \
424 579a97f7 bellard
    (host_ptr = lock_user(type, guest_addr, sizeof(*host_ptr), copy))
425 579a97f7 bellard
#define unlock_user_struct(host_ptr, guest_addr, copy)                \
426 53a5960a pbrook
    unlock_user(host_ptr, guest_addr, (copy) ? sizeof(*host_ptr) : 0)
427 53a5960a pbrook
428 c8a706fe pbrook
#if defined(USE_NPTL)
429 c8a706fe pbrook
#include <pthread.h>
430 c8a706fe pbrook
#endif
431 c8a706fe pbrook
432 e88de099 bellard
#endif /* QEMU_H */