Statistics
| Branch: | Revision:

root / linux-user / qemu.h @ 9168b3a5

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