Statistics
| Branch: | Revision:

root / linux-user / qemu.h @ 3d97b40b

History | View | Annotate | Download (9.7 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_BITS 32
13
#else
14
typedef target_ulong abi_ulong;
15
typedef target_long abi_long;
16
#define TARGET_ABI_BITS TARGET_LONG_BITS
17
#endif
18

    
19
#include "thunk.h"
20
#include "syscall_defs.h"
21
#include "syscall.h"
22
#include "target_signal.h"
23
#include "gdbstub.h"
24

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

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

    
65
#ifdef TARGET_ARM
66
/* FPU emulator */
67
#include "nwfpe/fpa11.h"
68
#endif
69

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

    
100
extern TaskState *first_task_state;
101
extern const char *qemu_uname_release;
102

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

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

    
127
void do_init_thread(struct target_pt_regs *regs, struct image_info *infop);
128
abi_ulong loader_build_argptr(int envc, int argc, abi_ulong sp,
129
                              abi_ulong stringp, int push_ptr);
130
int loader_exec(const char * filename, char ** argv, char ** envp,
131
             struct target_pt_regs * regs, struct image_info *infop);
132

    
133
int load_elf_binary(struct linux_binprm * bprm, struct target_pt_regs * regs,
134
                    struct image_info * info);
135
int load_flt_binary(struct linux_binprm * bprm, struct target_pt_regs * regs,
136
                    struct image_info * info);
137
#ifdef TARGET_HAS_ELFLOAD32
138
int load_elf_binary_multi(struct linux_binprm *bprm,
139
                          struct target_pt_regs *regs,
140
                          struct image_info *info);
141
#endif
142

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

    
158
extern int loglevel;
159
extern FILE *logfile;
160

    
161
/* strace.c */
162
void print_syscall(int num,
163
                   target_long arg1, target_long arg2, target_long arg3,
164
                   target_long arg4, target_long arg5, target_long arg6);
165
void print_syscall_ret(int num, target_long arg1);
166
extern int do_strace;
167

    
168
/* signal.c */
169
void process_pending_signals(void *cpu_env);
170
void signal_init(void);
171
int queue_signal(int sig, target_siginfo_t *info);
172
void host_to_target_siginfo(target_siginfo_t *tinfo, const siginfo_t *info);
173
void target_to_host_siginfo(siginfo_t *info, const target_siginfo_t *tinfo);
174
long do_sigreturn(CPUState *env);
175
long do_rt_sigreturn(CPUState *env);
176
int do_sigaltstack(const struct target_sigaltstack *uss,
177
                   struct target_sigaltstack *uoss,
178
                   abi_ulong sp);
179

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

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

    
201
/* user access */
202

    
203
#define VERIFY_READ 0
204
#define VERIFY_WRITE 1
205

    
206
#define access_ok(type,addr,size) \
207
    (page_check_range((target_ulong)addr,size,(type==VERIFY_READ)?PAGE_READ:PAGE_WRITE)==0)
208

    
209
/* NOTE get_user and put_user use host addresses.  */
210
#define __put_user(x,ptr)\
211
({\
212
    int size = sizeof(*ptr);\
213
    switch(size) {\
214
    case 1:\
215
        *(uint8_t *)(ptr) = (typeof(*ptr))(x);\
216
        break;\
217
    case 2:\
218
        *(uint16_t *)(ptr) = tswap16((typeof(*ptr))(x));\
219
        break;\
220
    case 4:\
221
        *(uint32_t *)(ptr) = tswap32((typeof(*ptr))(x));\
222
        break;\
223
    case 8:\
224
        *(uint64_t *)(ptr) = tswap64((typeof(*ptr))(x));\
225
        break;\
226
    default:\
227
        abort();\
228
    }\
229
    0;\
230
})
231

    
232
#define __get_user(x, ptr) \
233
({\
234
    int size = sizeof(*ptr);\
235
    switch(size) {\
236
    case 1:\
237
        x = (typeof(*ptr))*(uint8_t *)(ptr);\
238
        break;\
239
    case 2:\
240
        x = (typeof(*ptr))tswap16(*(uint16_t *)(ptr));\
241
        break;\
242
    case 4:\
243
        x = (typeof(*ptr))tswap32(*(uint32_t *)(ptr));\
244
        break;\
245
    case 8:\
246
        x = (typeof(*ptr))tswap64(*(uint64_t *)(ptr));\
247
        break;\
248
    default:\
249
        abort();\
250
    }\
251
    0;\
252
})
253

    
254
#define put_user(x,ptr)\
255
({\
256
    int __ret;\
257
    if (access_ok(VERIFY_WRITE, ptr, sizeof(*ptr)))\
258
        __ret = __put_user(x, ptr);\
259
    else\
260
        __ret = -EFAULT;\
261
    __ret;\
262
})
263

    
264
#define get_user(x,ptr)\
265
({\
266
    int __ret;\
267
    if (access_ok(VERIFY_READ, ptr, sizeof(*ptr)))\
268
        __ret = __get_user(x, ptr);\
269
    else\
270
        __ret = -EFAULT;\
271
    __ret;\
272
})
273

    
274
/* Functions for accessing guest memory.  The tget and tput functions
275
   read/write single values, byteswapping as neccessary.  The lock_user
276
   gets a pointer to a contiguous area of guest memory, but does not perform
277
   and byteswapping.  lock_user may return either a pointer to the guest
278
   memory, or a temporary buffer.  */
279

    
280
/* Lock an area of guest memory into the host.  If copy is true then the
281
   host area will have the same contents as the guest.  */
282
static inline void *lock_user(abi_ulong guest_addr, long len, int copy)
283
{
284
#ifdef DEBUG_REMAP
285
    void *addr;
286
    addr = malloc(len);
287
    if (copy)
288
        memcpy(addr, g2h(guest_addr), len);
289
    else
290
        memset(addr, 0, len);
291
    return addr;
292
#else
293
    return g2h(guest_addr);
294
#endif
295
}
296

    
297
/* Unlock an area of guest memory.  The first LEN bytes must be flushed back
298
   to guest memory.  */
299
static inline void unlock_user(void *host_addr, abi_ulong guest_addr,
300
                               long len)
301
{
302
#ifdef DEBUG_REMAP
303
    if (host_addr == g2h(guest_addr))
304
        return;
305
    if (len > 0)
306
        memcpy(g2h(guest_addr), host_addr, len);
307
    free(host_addr);
308
#endif
309
}
310

    
311
/* Return the length of a string in target memory.  */
312
static inline int target_strlen(abi_ulong ptr)
313
{
314
  return strlen(g2h(ptr));
315
}
316

    
317
/* Like lock_user but for null terminated strings.  */
318
static inline void *lock_user_string(abi_ulong guest_addr)
319
{
320
    long len;
321
    len = target_strlen(guest_addr) + 1;
322
    return lock_user(guest_addr, len, 1);
323
}
324

    
325
/* Helper macros for locking/ulocking a target struct.  */
326
#define lock_user_struct(host_ptr, guest_addr, copy) \
327
    host_ptr = lock_user(guest_addr, sizeof(*host_ptr), copy)
328
#define unlock_user_struct(host_ptr, guest_addr, copy) \
329
    unlock_user(host_ptr, guest_addr, (copy) ? sizeof(*host_ptr) : 0)
330

    
331
#define tget8(addr) ldub(addr)
332
#define tput8(addr, val) stb(addr, val)
333
#define tget16(addr) lduw(addr)
334
#define tput16(addr, val) stw(addr, val)
335
#define tget32(addr) ldl(addr)
336
#define tput32(addr, val) stl(addr, val)
337
#define tget64(addr) ldq(addr)
338
#define tput64(addr, val) stq(addr, val)
339
#if TARGET_ABI_BITS == 64
340
#define tgetl(addr) ldq(addr)
341
#define tputl(addr, val) stq(addr, val)
342
#else
343
#define tgetl(addr) ldl(addr)
344
#define tputl(addr, val) stl(addr, val)
345
#endif
346

    
347
#endif /* QEMU_H */