Statistics
| Branch: | Revision:

root / linux-user / qemu.h @ 5379557b

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