Statistics
| Branch: | Revision:

root / linux-user / qemu.h @ dae3270c

History | View | Annotate | Download (11.4 kB)

1
#ifndef QEMU_H
2
#define QEMU_H
3

    
4
#include <signal.h>
5
#include <string.h>
6

    
7
#include "cpu.h"
8

    
9
#ifdef TARGET_ABI32
10
typedef uint32_t abi_ulong;
11
typedef int32_t abi_long;
12
#define TARGET_ABI_FMT_lx "%08x"
13
#define TARGET_ABI_FMT_ld "%d"
14
#define TARGET_ABI_FMT_lu "%u"
15
#define TARGET_ABI_BITS 32
16
#else
17
typedef target_ulong abi_ulong;
18
typedef target_long abi_long;
19
#define TARGET_ABI_FMT_lx TARGET_FMT_lx
20
#define TARGET_ABI_FMT_ld TARGET_FMT_ld
21
#define TARGET_ABI_FMT_lu TARGET_FMT_lu
22
#define TARGET_ABI_BITS TARGET_LONG_BITS
23
#endif
24

    
25
#include "thunk.h"
26
#include "syscall_defs.h"
27
#include "syscall.h"
28
#include "target_signal.h"
29
#include "gdbstub.h"
30

    
31
/* This struct is used to hold certain information about the image.
32
 * Basically, it replicates in user space what would be certain
33
 * task_struct fields in the kernel
34
 */
35
struct image_info {
36
        abi_ulong       load_addr;
37
        abi_ulong       start_code;
38
        abi_ulong       end_code;
39
        abi_ulong       start_data;
40
        abi_ulong       end_data;
41
        abi_ulong       start_brk;
42
        abi_ulong       brk;
43
        abi_ulong       start_mmap;
44
        abi_ulong       mmap;
45
        abi_ulong       rss;
46
        abi_ulong       start_stack;
47
        abi_ulong       entry;
48
        abi_ulong       code_offset;
49
        abi_ulong       data_offset;
50
        char            **host_argv;
51
        int                personality;
52
};
53

    
54
#ifdef TARGET_I386
55
/* Information about the current linux thread */
56
struct vm86_saved_state {
57
    uint32_t eax; /* return code */
58
    uint32_t ebx;
59
    uint32_t ecx;
60
    uint32_t edx;
61
    uint32_t esi;
62
    uint32_t edi;
63
    uint32_t ebp;
64
    uint32_t esp;
65
    uint32_t eflags;
66
    uint32_t eip;
67
    uint16_t cs, ss, ds, es, fs, gs;
68
};
69
#endif
70

    
71
#ifdef TARGET_ARM
72
/* FPU emulator */
73
#include "nwfpe/fpa11.h"
74
#endif
75

    
76
/* NOTE: we force a big alignment so that the stack stored after is
77
   aligned too */
78
typedef struct TaskState {
79
    struct TaskState *next;
80
#ifdef TARGET_ARM
81
    /* FPA state */
82
    FPA11 fpa;
83
    int swi_errno;
84
#endif
85
#if defined(TARGET_I386) && !defined(TARGET_X86_64)
86
    abi_ulong target_v86;
87
    struct vm86_saved_state vm86_saved_regs;
88
    struct target_vm86plus_struct vm86plus;
89
    uint32_t v86flags;
90
    uint32_t v86mask;
91
#endif
92
#ifdef TARGET_M68K
93
    int sim_syscalls;
94
#endif
95
#if defined(TARGET_ARM) || defined(TARGET_M68K)
96
    /* Extra fields for semihosted binaries.  */
97
    uint32_t stack_base;
98
    uint32_t heap_base;
99
    uint32_t heap_limit;
100
#endif
101
    int used; /* non zero if used */
102
    struct image_info *info;
103
    uint8_t stack[0];
104
} __attribute__((aligned(16))) TaskState;
105

    
106
extern TaskState *first_task_state;
107
extern const char *qemu_uname_release;
108

    
109
/* ??? See if we can avoid exposing so much of the loader internals.  */
110
/*
111
 * MAX_ARG_PAGES defines the number of pages allocated for arguments
112
 * and envelope for the new program. 32 should suffice, this gives
113
 * a maximum env+arg of 128kB w/4KB pages!
114
 */
115
#define MAX_ARG_PAGES 32
116

    
117
/*
118
 * This structure is used to hold the arguments that are
119
 * used when loading binaries.
120
 */
121
struct linux_binprm {
122
        char buf[128];
123
        void *page[MAX_ARG_PAGES];
124
        abi_ulong p;
125
        int fd;
126
        int e_uid, e_gid;
127
        int argc, envc;
128
        char **argv;
129
        char **envp;
130
        char * filename;        /* Name of binary */
131
};
132

    
133
void do_init_thread(struct target_pt_regs *regs, struct image_info *infop);
134
abi_ulong loader_build_argptr(int envc, int argc, abi_ulong sp,
135
                              abi_ulong stringp, int push_ptr);
136
int loader_exec(const char * filename, char ** argv, char ** envp,
137
             struct target_pt_regs * regs, struct image_info *infop);
138

    
139
int load_elf_binary(struct linux_binprm * bprm, struct target_pt_regs * regs,
140
                    struct image_info * info);
141
int load_flt_binary(struct linux_binprm * bprm, struct target_pt_regs * regs,
142
                    struct image_info * info);
143
#ifdef TARGET_HAS_ELFLOAD32
144
int load_elf_binary_multi(struct linux_binprm *bprm,
145
                          struct target_pt_regs *regs,
146
                          struct image_info *info);
147
#endif
148

    
149
abi_long memcpy_to_target(abi_ulong dest, const void *src,
150
                          unsigned long len);
151
void target_set_brk(abi_ulong new_brk);
152
abi_long do_brk(abi_ulong new_brk);
153
void syscall_init(void);
154
abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
155
                    abi_long arg2, abi_long arg3, abi_long arg4,
156
                    abi_long arg5, abi_long arg6);
157
void gemu_log(const char *fmt, ...) __attribute__((format(printf,1,2)));
158
extern CPUState *global_env;
159
void cpu_loop(CPUState *env);
160
void init_paths(const char *prefix);
161
const char *path(const char *pathname);
162
char *target_strerror(int err);
163

    
164
extern int loglevel;
165
extern FILE *logfile;
166

    
167
/* strace.c */
168
void print_syscall(int num,
169
                   abi_long arg1, abi_long arg2, abi_long arg3,
170
                   abi_long arg4, abi_long arg5, abi_long arg6);
171
void print_syscall_ret(int num, abi_long arg1);
172
extern int do_strace;
173

    
174
/* signal.c */
175
void process_pending_signals(void *cpu_env);
176
void signal_init(void);
177
int queue_signal(int sig, target_siginfo_t *info);
178
void host_to_target_siginfo(target_siginfo_t *tinfo, const siginfo_t *info);
179
void target_to_host_siginfo(siginfo_t *info, const target_siginfo_t *tinfo);
180
long do_sigreturn(CPUState *env);
181
long do_rt_sigreturn(CPUState *env);
182
abi_long do_sigaltstack(abi_ulong uss_addr, abi_ulong uoss_addr, abi_ulong sp);
183

    
184
#ifdef TARGET_I386
185
/* vm86.c */
186
void save_v86_state(CPUX86State *env);
187
void handle_vm86_trap(CPUX86State *env, int trapno);
188
void handle_vm86_fault(CPUX86State *env);
189
int do_vm86(CPUX86State *env, long subfunction, abi_ulong v86_addr);
190
#elif defined(TARGET_SPARC64)
191
void sparc64_set_context(CPUSPARCState *env);
192
void sparc64_get_context(CPUSPARCState *env);
193
#endif
194

    
195
/* mmap.c */
196
int target_mprotect(abi_ulong start, abi_ulong len, int prot);
197
abi_long target_mmap(abi_ulong start, abi_ulong len, int prot,
198
                     int flags, int fd, abi_ulong offset);
199
int target_munmap(abi_ulong start, abi_ulong len);
200
abi_long target_mremap(abi_ulong old_addr, abi_ulong old_size,
201
                       abi_ulong new_size, unsigned long flags,
202
                       abi_ulong new_addr);
203
int target_msync(abi_ulong start, abi_ulong len, int flags);
204

    
205
/* user access */
206

    
207
#define VERIFY_READ 0
208
#define VERIFY_WRITE 1 /* implies read access */
209

    
210
static inline int access_ok(int type, abi_ulong addr, abi_ulong size)
211
{
212
    return page_check_range((target_ulong)addr, size,
213
                            (type == VERIFY_READ) ? PAGE_READ : (PAGE_READ | PAGE_WRITE)) == 0;
214
}
215

    
216
/* NOTE __get_user and __put_user use host pointers and don't check access. */
217
/* These are usually used to access struct data members once the
218
 * struct has been locked - usually with lock_user_struct().
219
 */
220
#define __put_user(x, hptr)\
221
({\
222
    int size = sizeof(*hptr);\
223
    switch(size) {\
224
    case 1:\
225
        *(uint8_t *)(hptr) = (typeof(*hptr))(x);\
226
        break;\
227
    case 2:\
228
        *(uint16_t *)(hptr) = tswap16((typeof(*hptr))(x));\
229
        break;\
230
    case 4:\
231
        *(uint32_t *)(hptr) = tswap32((typeof(*hptr))(x));\
232
        break;\
233
    case 8:\
234
        *(uint64_t *)(hptr) = tswap64((typeof(*hptr))(x));\
235
        break;\
236
    default:\
237
        abort();\
238
    }\
239
    0;\
240
})
241

    
242
#define __get_user(x, hptr) \
243
({\
244
    int size = sizeof(*hptr);\
245
    switch(size) {\
246
    case 1:\
247
        x = (typeof(*hptr))*(uint8_t *)(hptr);\
248
        break;\
249
    case 2:\
250
        x = (typeof(*hptr))tswap16(*(uint16_t *)(hptr));\
251
        break;\
252
    case 4:\
253
        x = (typeof(*hptr))tswap32(*(uint32_t *)(hptr));\
254
        break;\
255
    case 8:\
256
        x = (typeof(*hptr))tswap64(*(uint64_t *)(hptr));\
257
        break;\
258
    default:\
259
        abort();\
260
    }\
261
    0;\
262
})
263

    
264
/* put_user()/get_user() take a guest address and check access */
265
/* These are usually used to access an atomic data type, such as an int,
266
 * that has been passed by address.  These internally perform locking
267
 * and unlocking on the data type.
268
 */
269
#define put_user(x, gaddr, target_type)                                        \
270
({                                                                        \
271
    abi_ulong __gaddr = (gaddr);                                        \
272
    target_type *__hptr;                                                \
273
    abi_long __ret;                                                        \
274
    if ((__hptr = lock_user(VERIFY_WRITE, __gaddr, sizeof(target_type), 0))) { \
275
        __ret = __put_user((x), __hptr);                                \
276
        unlock_user(__hptr, __gaddr, sizeof(target_type));                \
277
    } else                                                                \
278
        __ret = -TARGET_EFAULT;                                                \
279
    __ret;                                                                \
280
})
281

    
282
#define get_user(x, gaddr, target_type)                                        \
283
({                                                                        \
284
    abi_ulong __gaddr = (gaddr);                                        \
285
    target_type *__hptr;                                                \
286
    abi_long __ret;                                                        \
287
    if ((__hptr = lock_user(VERIFY_READ, __gaddr, sizeof(target_type), 1))) { \
288
        __ret = __get_user((x), __hptr);                                \
289
        unlock_user(__hptr, __gaddr, 0);                                \
290
    } else                                                                \
291
        __ret = -TARGET_EFAULT;                                                \
292
    __ret;                                                                \
293
})
294

    
295
/* copy_from_user() and copy_to_user() are usually used to copy data
296
 * buffers between the target and host.  These internally perform
297
 * locking/unlocking of the memory.
298
 */
299
abi_long copy_from_user(void *hptr, abi_ulong gaddr, size_t len);
300
abi_long copy_to_user(abi_ulong gaddr, void *hptr, size_t len);
301

    
302
/* Functions for accessing guest memory.  The tget and tput functions
303
   read/write single values, byteswapping as neccessary.  The lock_user
304
   gets a pointer to a contiguous area of guest memory, but does not perform
305
   and byteswapping.  lock_user may return either a pointer to the guest
306
   memory, or a temporary buffer.  */
307

    
308
/* Lock an area of guest memory into the host.  If copy is true then the
309
   host area will have the same contents as the guest.  */
310
static inline void *lock_user(int type, abi_ulong guest_addr, long len, int copy)
311
{
312
    if (!access_ok(type, guest_addr, len))
313
        return NULL;
314
#ifdef DEBUG_REMAP
315
    {
316
        void *addr;
317
        addr = malloc(len);
318
        if (copy)
319
            memcpy(addr, g2h(guest_addr), len);
320
        else
321
            memset(addr, 0, len);
322
        return addr;
323
    }
324
#else
325
    return g2h(guest_addr);
326
#endif
327
}
328

    
329
/* Unlock an area of guest memory.  The first LEN bytes must be
330
   flushed back to guest memory. host_ptr = NULL is explicitely
331
   allowed and does nothing. */
332
static inline void unlock_user(void *host_ptr, abi_ulong guest_addr,
333
                               long len)
334
{
335

    
336
#ifdef DEBUG_REMAP
337
    if (!host_ptr)
338
        return;
339
    if (host_ptr == g2h(guest_addr))
340
        return;
341
    if (len > 0)
342
        memcpy(g2h(guest_ptr), host_ptr, len);
343
    free(host_ptr);
344
#endif
345
}
346

    
347
/* Return the length of a string in target memory or -TARGET_EFAULT if
348
   access error. */
349
abi_long target_strlen(abi_ulong gaddr);
350

    
351
/* Like lock_user but for null terminated strings.  */
352
static inline void *lock_user_string(abi_ulong guest_addr)
353
{
354
    abi_long len;
355
    len = target_strlen(guest_addr);
356
    if (len < 0)
357
        return NULL;
358
    return lock_user(VERIFY_READ, guest_addr, (long)(len + 1), 1);
359
}
360

    
361
/* Helper macros for locking/ulocking a target struct.  */
362
#define lock_user_struct(type, host_ptr, guest_addr, copy)        \
363
    (host_ptr = lock_user(type, guest_addr, sizeof(*host_ptr), copy))
364
#define unlock_user_struct(host_ptr, guest_addr, copy)                \
365
    unlock_user(host_ptr, guest_addr, (copy) ? sizeof(*host_ptr) : 0)
366

    
367
#define tget8(addr) ldub(addr)
368
#define tput8(addr, val) stb(addr, val)
369
#define tget16(addr) lduw(addr)
370
#define tput16(addr, val) stw(addr, val)
371
#define tget32(addr) ldl(addr)
372
#define tput32(addr, val) stl(addr, val)
373
#define tget64(addr) ldq(addr)
374
#define tput64(addr, val) stq(addr, val)
375
#if TARGET_ABI_BITS == 64
376
#define tgetl(addr) ldq(addr)
377
#define tputl(addr, val) stq(addr, val)
378
#else
379
#define tgetl(addr) ldl(addr)
380
#define tputl(addr, val) stl(addr, val)
381
#endif
382

    
383
#endif /* QEMU_H */