blob: 22a4fec61dc93cf1495e331381b34637ecd6b2f1 [file] [log] [blame]
bellarde88de092005-02-07 12:35:39 +00001#ifndef QEMU_H
2#define QEMU_H
bellard31e31b82003-02-18 22:55:36 +00003
bellard9de5e442003-03-23 16:49:39 +00004#include <signal.h>
bellardedf779f2004-02-22 13:40:13 +00005#include <string.h>
bellard9de5e442003-03-23 16:49:39 +00006
bellard6180a182003-09-30 21:04:53 +00007#include "cpu.h"
blueswir1992f48a2007-10-14 16:27:31 +00008
9#ifdef TARGET_ABI32
10typedef uint32_t abi_ulong;
11typedef int32_t abi_long;
12#define TARGET_ABI_BITS 32
13#else
14typedef target_ulong abi_ulong;
15typedef target_long abi_long;
16#define TARGET_ABI_BITS TARGET_LONG_BITS
17#endif
18
19#include "thunk.h"
20#include "syscall_defs.h"
bellard6180a182003-09-30 21:04:53 +000021#include "syscall.h"
thsa04e1342007-09-27 13:57:58 +000022#include "target_signal.h"
bellard1fddef42005-04-17 19:16:13 +000023#include "gdbstub.h"
bellard66fb9762003-03-23 01:06:05 +000024
bellard31e31b82003-02-18 22:55:36 +000025/* 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 */
29struct image_info {
blueswir1992f48a2007-10-14 16:27:31 +000030 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;
pbrook38d06622006-11-19 20:29:35 +000044 char **host_argv;
bellard31e31b82003-02-18 22:55:36 +000045 int personality;
46};
47
bellardb346ff42003-06-15 20:05:50 +000048#ifdef TARGET_I386
bellard851e67a2003-03-29 16:53:14 +000049/* Information about the current linux thread */
50struct 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};
bellardb346ff42003-06-15 20:05:50 +000063#endif
bellard851e67a2003-03-29 16:53:14 +000064
bellard28c4f362004-02-16 21:47:43 +000065#ifdef TARGET_ARM
66/* FPU emulator */
67#include "nwfpe/fpa11.h"
bellard28c4f362004-02-16 21:47:43 +000068#endif
69
bellard851e67a2003-03-29 16:53:14 +000070/* NOTE: we force a big alignment so that the stack stored after is
71 aligned too */
72typedef struct TaskState {
73 struct TaskState *next;
bellard28c4f362004-02-16 21:47:43 +000074#ifdef TARGET_ARM
75 /* FPA state */
76 FPA11 fpa;
bellarda4f81972005-04-23 18:25:41 +000077 int swi_errno;
bellard28c4f362004-02-16 21:47:43 +000078#endif
j_mayer84409dd2007-04-06 08:56:50 +000079#if defined(TARGET_I386) && !defined(TARGET_X86_64)
blueswir1992f48a2007-10-14 16:27:31 +000080 abi_ulong target_v86;
bellard851e67a2003-03-29 16:53:14 +000081 struct vm86_saved_state vm86_saved_regs;
bellardb333af02003-05-14 21:48:51 +000082 struct target_vm86plus_struct vm86plus;
bellard631271d2003-05-10 13:14:52 +000083 uint32_t v86flags;
84 uint32_t v86mask;
bellardb346ff42003-06-15 20:05:50 +000085#endif
pbrooke6e59062006-10-22 00:18:54 +000086#ifdef TARGET_M68K
87 int sim_syscalls;
88#endif
pbrooka87295e2007-05-26 15:09:38 +000089#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
bellard851e67a2003-03-29 16:53:14 +000095 int used; /* non zero if used */
pbrook978efd62006-06-17 18:30:42 +000096 struct image_info *info;
bellard851e67a2003-03-29 16:53:14 +000097 uint8_t stack[0];
98} __attribute__((aligned(16))) TaskState;
99
100extern TaskState *first_task_state;
pbrookc5937222006-05-14 11:30:38 +0000101extern const char *qemu_uname_release;
bellard851e67a2003-03-29 16:53:14 +0000102
pbrooke5fe0c52006-06-11 13:32:59 +0000103/* ??? 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/*
ths5fafdf22007-09-16 21:08:06 +0000112 * This structure is used to hold the arguments that are
pbrooke5fe0c52006-06-11 13:32:59 +0000113 * used when loading binaries.
114 */
115struct linux_binprm {
116 char buf[128];
117 void *page[MAX_ARG_PAGES];
blueswir1992f48a2007-10-14 16:27:31 +0000118 abi_ulong p;
pbrooke5fe0c52006-06-11 13:32:59 +0000119 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
127void do_init_thread(struct target_pt_regs *regs, struct image_info *infop);
blueswir1992f48a2007-10-14 16:27:31 +0000128abi_ulong loader_build_argptr(int envc, int argc, abi_ulong sp,
129 abi_ulong stringp, int push_ptr);
ths5fafdf22007-09-16 21:08:06 +0000130int loader_exec(const char * filename, char ** argv, char ** envp,
bellard01ffc752003-02-18 23:00:51 +0000131 struct target_pt_regs * regs, struct image_info *infop);
bellard31e31b82003-02-18 22:55:36 +0000132
pbrooke5fe0c52006-06-11 13:32:59 +0000133int load_elf_binary(struct linux_binprm * bprm, struct target_pt_regs * regs,
134 struct image_info * info);
135int load_flt_binary(struct linux_binprm * bprm, struct target_pt_regs * regs,
136 struct image_info * info);
blueswir1cb33da52007-10-09 16:34:29 +0000137#ifdef TARGET_HAS_ELFLOAD32
138int load_elf_binary_multi(struct linux_binprm *bprm,
139 struct target_pt_regs *regs,
140 struct image_info *info);
141#endif
pbrooke5fe0c52006-06-11 13:32:59 +0000142
blueswir1992f48a2007-10-14 16:27:31 +0000143void memcpy_to_target(abi_ulong dest, const void *src,
pbrooke5fe0c52006-06-11 13:32:59 +0000144 unsigned long len);
blueswir1992f48a2007-10-14 16:27:31 +0000145void target_set_brk(abi_ulong new_brk);
146abi_long do_brk(abi_ulong new_brk);
bellard31e31b82003-02-18 22:55:36 +0000147void syscall_init(void);
blueswir1992f48a2007-10-14 16:27:31 +0000148abi_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);
bellard31e31b82003-02-18 22:55:36 +0000151void gemu_log(const char *fmt, ...) __attribute__((format(printf,1,2)));
bellardb346ff42003-06-15 20:05:50 +0000152extern CPUState *global_env;
153void cpu_loop(CPUState *env);
bellard32ce6332003-04-11 00:16:16 +0000154void init_paths(const char *prefix);
155const char *path(const char *pathname);
thsb92c47c2007-11-01 00:07:38 +0000156char *target_strerror(int err);
bellard6977fbf2003-04-29 20:39:23 +0000157
158extern int loglevel;
bellard631271d2003-05-10 13:14:52 +0000159extern FILE *logfile;
160
thsb92c47c2007-11-01 00:07:38 +0000161/* strace.c */
162void print_syscall(int num,
163 target_long arg1, target_long arg2, target_long arg3,
164 target_long arg4, target_long arg5, target_long arg6);
165void print_syscall_ret(int num, target_long arg1);
166extern int do_strace;
167
bellardb346ff42003-06-15 20:05:50 +0000168/* signal.c */
169void process_pending_signals(void *cpu_env);
170void signal_init(void);
171int queue_signal(int sig, target_siginfo_t *info);
172void host_to_target_siginfo(target_siginfo_t *tinfo, const siginfo_t *info);
173void target_to_host_siginfo(siginfo_t *info, const target_siginfo_t *tinfo);
174long do_sigreturn(CPUState *env);
175long do_rt_sigreturn(CPUState *env);
thsa04e1342007-09-27 13:57:58 +0000176int do_sigaltstack(const struct target_sigaltstack *uss,
177 struct target_sigaltstack *uoss,
blueswir1992f48a2007-10-14 16:27:31 +0000178 abi_ulong sp);
bellardb346ff42003-06-15 20:05:50 +0000179
180#ifdef TARGET_I386
bellard631271d2003-05-10 13:14:52 +0000181/* vm86.c */
182void save_v86_state(CPUX86State *env);
bellard447db212003-05-10 15:10:36 +0000183void handle_vm86_trap(CPUX86State *env, int trapno);
bellard631271d2003-05-10 13:14:52 +0000184void handle_vm86_fault(CPUX86State *env);
blueswir1992f48a2007-10-14 16:27:31 +0000185int do_vm86(CPUX86State *env, long subfunction, abi_ulong v86_addr);
blueswir15bfb56b2007-10-05 17:01:51 +0000186#elif defined(TARGET_SPARC64)
187void sparc64_set_context(CPUSPARCState *env);
188void sparc64_get_context(CPUSPARCState *env);
bellardb346ff42003-06-15 20:05:50 +0000189#endif
bellard631271d2003-05-10 13:14:52 +0000190
bellard54936002003-05-13 00:25:15 +0000191/* mmap.c */
blueswir1992f48a2007-10-14 16:27:31 +0000192int target_mprotect(abi_ulong start, abi_ulong len, int prot);
193abi_long target_mmap(abi_ulong start, abi_ulong len, int prot,
194 int flags, int fd, abi_ulong offset);
195int target_munmap(abi_ulong start, abi_ulong len);
196abi_long target_mremap(abi_ulong old_addr, abi_ulong old_size,
197 abi_ulong new_size, unsigned long flags,
198 abi_ulong new_addr);
199int target_msync(abi_ulong start, abi_ulong len, int flags);
bellard54936002003-05-13 00:25:15 +0000200
bellardedf779f2004-02-22 13:40:13 +0000201/* user access */
202
203#define VERIFY_READ 0
204#define VERIFY_WRITE 1
205
206#define access_ok(type,addr,size) (1)
207
pbrook53a59602006-03-25 19:31:22 +0000208/* NOTE get_user and put_user use host addresses. */
bellardedf779f2004-02-22 13:40:13 +0000209#define __put_user(x,ptr)\
210({\
211 int size = sizeof(*ptr);\
212 switch(size) {\
213 case 1:\
pbrook53a59602006-03-25 19:31:22 +0000214 *(uint8_t *)(ptr) = (typeof(*ptr))(x);\
bellardedf779f2004-02-22 13:40:13 +0000215 break;\
216 case 2:\
pbrook53a59602006-03-25 19:31:22 +0000217 *(uint16_t *)(ptr) = tswap16((typeof(*ptr))(x));\
bellardedf779f2004-02-22 13:40:13 +0000218 break;\
219 case 4:\
pbrook53a59602006-03-25 19:31:22 +0000220 *(uint32_t *)(ptr) = tswap32((typeof(*ptr))(x));\
bellardedf779f2004-02-22 13:40:13 +0000221 break;\
222 case 8:\
pbrook53a59602006-03-25 19:31:22 +0000223 *(uint64_t *)(ptr) = tswap64((typeof(*ptr))(x));\
bellardedf779f2004-02-22 13:40:13 +0000224 break;\
225 default:\
226 abort();\
227 }\
228 0;\
229})
230
231#define __get_user(x, ptr) \
232({\
233 int size = sizeof(*ptr);\
234 switch(size) {\
235 case 1:\
pbrook53a59602006-03-25 19:31:22 +0000236 x = (typeof(*ptr))*(uint8_t *)(ptr);\
bellardedf779f2004-02-22 13:40:13 +0000237 break;\
238 case 2:\
pbrook53a59602006-03-25 19:31:22 +0000239 x = (typeof(*ptr))tswap16(*(uint16_t *)(ptr));\
bellardedf779f2004-02-22 13:40:13 +0000240 break;\
241 case 4:\
pbrook53a59602006-03-25 19:31:22 +0000242 x = (typeof(*ptr))tswap32(*(uint32_t *)(ptr));\
bellardedf779f2004-02-22 13:40:13 +0000243 break;\
244 case 8:\
pbrook53a59602006-03-25 19:31:22 +0000245 x = (typeof(*ptr))tswap64(*(uint64_t *)(ptr));\
bellardedf779f2004-02-22 13:40:13 +0000246 break;\
247 default:\
248 abort();\
249 }\
250 0;\
251})
252
bellardedf779f2004-02-22 13:40:13 +0000253#define put_user(x,ptr)\
254({\
255 int __ret;\
256 if (access_ok(VERIFY_WRITE, ptr, sizeof(*ptr)))\
257 __ret = __put_user(x, ptr);\
258 else\
259 __ret = -EFAULT;\
260 __ret;\
261})
262
263#define get_user(x,ptr)\
264({\
265 int __ret;\
266 if (access_ok(VERIFY_READ, ptr, sizeof(*ptr)))\
267 __ret = __get_user(x, ptr);\
268 else\
269 __ret = -EFAULT;\
270 __ret;\
271})
272
pbrook53a59602006-03-25 19:31:22 +0000273/* Functions for accessing guest memory. The tget and tput functions
274 read/write single values, byteswapping as neccessary. The lock_user
275 gets a pointer to a contiguous area of guest memory, but does not perform
276 and byteswapping. lock_user may return either a pointer to the guest
277 memory, or a temporary buffer. */
278
279/* Lock an area of guest memory into the host. If copy is true then the
280 host area will have the same contents as the guest. */
blueswir1992f48a2007-10-14 16:27:31 +0000281static inline void *lock_user(abi_ulong guest_addr, long len, int copy)
bellardedf779f2004-02-22 13:40:13 +0000282{
pbrook53a59602006-03-25 19:31:22 +0000283#ifdef DEBUG_REMAP
284 void *addr;
285 addr = malloc(len);
286 if (copy)
287 memcpy(addr, g2h(guest_addr), len);
bellardedf779f2004-02-22 13:40:13 +0000288 else
pbrook53a59602006-03-25 19:31:22 +0000289 memset(addr, 0, len);
290 return addr;
291#else
292 return g2h(guest_addr);
293#endif
bellardedf779f2004-02-22 13:40:13 +0000294}
295
pbrook53a59602006-03-25 19:31:22 +0000296/* Unlock an area of guest memory. The first LEN bytes must be flushed back
297 to guest memory. */
blueswir1992f48a2007-10-14 16:27:31 +0000298static inline void unlock_user(void *host_addr, abi_ulong guest_addr,
299 long len)
bellardedf779f2004-02-22 13:40:13 +0000300{
pbrook53a59602006-03-25 19:31:22 +0000301#ifdef DEBUG_REMAP
302 if (host_addr == g2h(guest_addr))
303 return;
304 if (len > 0)
305 memcpy(g2h(guest_addr), host_addr, len);
306 free(host_addr);
307#endif
bellardedf779f2004-02-22 13:40:13 +0000308}
309
pbrook53a59602006-03-25 19:31:22 +0000310/* Return the length of a string in target memory. */
blueswir1992f48a2007-10-14 16:27:31 +0000311static inline int target_strlen(abi_ulong ptr)
bellardedf779f2004-02-22 13:40:13 +0000312{
pbrook53a59602006-03-25 19:31:22 +0000313 return strlen(g2h(ptr));
bellardedf779f2004-02-22 13:40:13 +0000314}
315
pbrook53a59602006-03-25 19:31:22 +0000316/* Like lock_user but for null terminated strings. */
blueswir1992f48a2007-10-14 16:27:31 +0000317static inline void *lock_user_string(abi_ulong guest_addr)
pbrook53a59602006-03-25 19:31:22 +0000318{
319 long len;
320 len = target_strlen(guest_addr) + 1;
321 return lock_user(guest_addr, len, 1);
322}
323
324/* Helper macros for locking/ulocking a target struct. */
325#define lock_user_struct(host_ptr, guest_addr, copy) \
326 host_ptr = lock_user(guest_addr, sizeof(*host_ptr), copy)
327#define unlock_user_struct(host_ptr, guest_addr, copy) \
328 unlock_user(host_ptr, guest_addr, (copy) ? sizeof(*host_ptr) : 0)
329
330#define tget8(addr) ldub(addr)
331#define tput8(addr, val) stb(addr, val)
332#define tget16(addr) lduw(addr)
333#define tput16(addr, val) stw(addr, val)
334#define tget32(addr) ldl(addr)
335#define tput32(addr, val) stl(addr, val)
336#define tget64(addr) ldq(addr)
337#define tput64(addr, val) stq(addr, val)
blueswir1992f48a2007-10-14 16:27:31 +0000338#if TARGET_ABI_BITS == 64
pbrook53a59602006-03-25 19:31:22 +0000339#define tgetl(addr) ldq(addr)
340#define tputl(addr, val) stq(addr, val)
341#else
342#define tgetl(addr) ldl(addr)
343#define tputl(addr, val) stl(addr, val)
344#endif
345
bellarde88de092005-02-07 12:35:39 +0000346#endif /* QEMU_H */