blob: 6fc0bbffcb60d9ebf2619c0ba3ff38c4629338d2 [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;
thsf3e32852007-11-03 15:12:16 +000012#define TARGET_ABI_FMT_lx "%08x"
13#define TARGET_ABI_FMT_ld "%d"
14#define TARGET_ABI_FMT_lu "%u"
blueswir1992f48a2007-10-14 16:27:31 +000015#define TARGET_ABI_BITS 32
16#else
17typedef target_ulong abi_ulong;
18typedef target_long abi_long;
thsf3e32852007-11-03 15:12:16 +000019#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
blueswir1992f48a2007-10-14 16:27:31 +000022#define TARGET_ABI_BITS TARGET_LONG_BITS
23#endif
24
25#include "thunk.h"
26#include "syscall_defs.h"
bellard6180a182003-09-30 21:04:53 +000027#include "syscall.h"
thsa04e1342007-09-27 13:57:58 +000028#include "target_signal.h"
bellard1fddef42005-04-17 19:16:13 +000029#include "gdbstub.h"
bellard66fb9762003-03-23 01:06:05 +000030
bellard31e31b82003-02-18 22:55:36 +000031/* 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 */
35struct image_info {
blueswir1992f48a2007-10-14 16:27:31 +000036 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;
pbrook38d06622006-11-19 20:29:35 +000050 char **host_argv;
bellard31e31b82003-02-18 22:55:36 +000051 int personality;
52};
53
bellardb346ff42003-06-15 20:05:50 +000054#ifdef TARGET_I386
bellard851e67a2003-03-29 16:53:14 +000055/* Information about the current linux thread */
56struct 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};
bellardb346ff42003-06-15 20:05:50 +000069#endif
bellard851e67a2003-03-29 16:53:14 +000070
bellard28c4f362004-02-16 21:47:43 +000071#ifdef TARGET_ARM
72/* FPU emulator */
73#include "nwfpe/fpa11.h"
bellard28c4f362004-02-16 21:47:43 +000074#endif
75
bellard851e67a2003-03-29 16:53:14 +000076/* NOTE: we force a big alignment so that the stack stored after is
77 aligned too */
78typedef struct TaskState {
79 struct TaskState *next;
bellard28c4f362004-02-16 21:47:43 +000080#ifdef TARGET_ARM
81 /* FPA state */
82 FPA11 fpa;
bellarda4f81972005-04-23 18:25:41 +000083 int swi_errno;
bellard28c4f362004-02-16 21:47:43 +000084#endif
j_mayer84409dd2007-04-06 08:56:50 +000085#if defined(TARGET_I386) && !defined(TARGET_X86_64)
blueswir1992f48a2007-10-14 16:27:31 +000086 abi_ulong target_v86;
bellard851e67a2003-03-29 16:53:14 +000087 struct vm86_saved_state vm86_saved_regs;
bellardb333af02003-05-14 21:48:51 +000088 struct target_vm86plus_struct vm86plus;
bellard631271d2003-05-10 13:14:52 +000089 uint32_t v86flags;
90 uint32_t v86mask;
bellardb346ff42003-06-15 20:05:50 +000091#endif
pbrooke6e59062006-10-22 00:18:54 +000092#ifdef TARGET_M68K
93 int sim_syscalls;
94#endif
pbrooka87295e2007-05-26 15:09:38 +000095#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
bellard851e67a2003-03-29 16:53:14 +0000101 int used; /* non zero if used */
pbrook978efd62006-06-17 18:30:42 +0000102 struct image_info *info;
bellard851e67a2003-03-29 16:53:14 +0000103 uint8_t stack[0];
104} __attribute__((aligned(16))) TaskState;
105
106extern TaskState *first_task_state;
pbrookc5937222006-05-14 11:30:38 +0000107extern const char *qemu_uname_release;
bellard851e67a2003-03-29 16:53:14 +0000108
pbrooke5fe0c52006-06-11 13:32:59 +0000109/* ??? 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/*
ths5fafdf22007-09-16 21:08:06 +0000118 * This structure is used to hold the arguments that are
pbrooke5fe0c52006-06-11 13:32:59 +0000119 * used when loading binaries.
120 */
121struct linux_binprm {
122 char buf[128];
123 void *page[MAX_ARG_PAGES];
blueswir1992f48a2007-10-14 16:27:31 +0000124 abi_ulong p;
pbrooke5fe0c52006-06-11 13:32:59 +0000125 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
133void do_init_thread(struct target_pt_regs *regs, struct image_info *infop);
blueswir1992f48a2007-10-14 16:27:31 +0000134abi_ulong loader_build_argptr(int envc, int argc, abi_ulong sp,
135 abi_ulong stringp, int push_ptr);
ths5fafdf22007-09-16 21:08:06 +0000136int loader_exec(const char * filename, char ** argv, char ** envp,
bellard01ffc752003-02-18 23:00:51 +0000137 struct target_pt_regs * regs, struct image_info *infop);
bellard31e31b82003-02-18 22:55:36 +0000138
pbrooke5fe0c52006-06-11 13:32:59 +0000139int load_elf_binary(struct linux_binprm * bprm, struct target_pt_regs * regs,
140 struct image_info * info);
141int load_flt_binary(struct linux_binprm * bprm, struct target_pt_regs * regs,
142 struct image_info * info);
blueswir1cb33da52007-10-09 16:34:29 +0000143#ifdef TARGET_HAS_ELFLOAD32
144int load_elf_binary_multi(struct linux_binprm *bprm,
145 struct target_pt_regs *regs,
146 struct image_info *info);
147#endif
pbrooke5fe0c52006-06-11 13:32:59 +0000148
blueswir1992f48a2007-10-14 16:27:31 +0000149void memcpy_to_target(abi_ulong dest, const void *src,
pbrooke5fe0c52006-06-11 13:32:59 +0000150 unsigned long len);
blueswir1992f48a2007-10-14 16:27:31 +0000151void target_set_brk(abi_ulong new_brk);
152abi_long do_brk(abi_ulong new_brk);
bellard31e31b82003-02-18 22:55:36 +0000153void syscall_init(void);
blueswir1992f48a2007-10-14 16:27:31 +0000154abi_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);
bellard31e31b82003-02-18 22:55:36 +0000157void gemu_log(const char *fmt, ...) __attribute__((format(printf,1,2)));
bellardb346ff42003-06-15 20:05:50 +0000158extern CPUState *global_env;
159void cpu_loop(CPUState *env);
bellard32ce6332003-04-11 00:16:16 +0000160void init_paths(const char *prefix);
161const char *path(const char *pathname);
thsb92c47c2007-11-01 00:07:38 +0000162char *target_strerror(int err);
bellard6977fbf2003-04-29 20:39:23 +0000163
164extern int loglevel;
bellard631271d2003-05-10 13:14:52 +0000165extern FILE *logfile;
166
thsb92c47c2007-11-01 00:07:38 +0000167/* strace.c */
168void print_syscall(int num,
169 target_long arg1, target_long arg2, target_long arg3,
170 target_long arg4, target_long arg5, target_long arg6);
171void print_syscall_ret(int num, target_long arg1);
172extern int do_strace;
173
bellardb346ff42003-06-15 20:05:50 +0000174/* signal.c */
175void process_pending_signals(void *cpu_env);
176void signal_init(void);
177int queue_signal(int sig, target_siginfo_t *info);
178void host_to_target_siginfo(target_siginfo_t *tinfo, const siginfo_t *info);
179void target_to_host_siginfo(siginfo_t *info, const target_siginfo_t *tinfo);
180long do_sigreturn(CPUState *env);
181long do_rt_sigreturn(CPUState *env);
thsa04e1342007-09-27 13:57:58 +0000182int do_sigaltstack(const struct target_sigaltstack *uss,
183 struct target_sigaltstack *uoss,
blueswir1992f48a2007-10-14 16:27:31 +0000184 abi_ulong sp);
bellardb346ff42003-06-15 20:05:50 +0000185
186#ifdef TARGET_I386
bellard631271d2003-05-10 13:14:52 +0000187/* vm86.c */
188void save_v86_state(CPUX86State *env);
bellard447db212003-05-10 15:10:36 +0000189void handle_vm86_trap(CPUX86State *env, int trapno);
bellard631271d2003-05-10 13:14:52 +0000190void handle_vm86_fault(CPUX86State *env);
blueswir1992f48a2007-10-14 16:27:31 +0000191int do_vm86(CPUX86State *env, long subfunction, abi_ulong v86_addr);
blueswir15bfb56b2007-10-05 17:01:51 +0000192#elif defined(TARGET_SPARC64)
193void sparc64_set_context(CPUSPARCState *env);
194void sparc64_get_context(CPUSPARCState *env);
bellardb346ff42003-06-15 20:05:50 +0000195#endif
bellard631271d2003-05-10 13:14:52 +0000196
bellard54936002003-05-13 00:25:15 +0000197/* mmap.c */
blueswir1992f48a2007-10-14 16:27:31 +0000198int target_mprotect(abi_ulong start, abi_ulong len, int prot);
199abi_long target_mmap(abi_ulong start, abi_ulong len, int prot,
200 int flags, int fd, abi_ulong offset);
201int target_munmap(abi_ulong start, abi_ulong len);
202abi_long target_mremap(abi_ulong old_addr, abi_ulong old_size,
203 abi_ulong new_size, unsigned long flags,
204 abi_ulong new_addr);
205int target_msync(abi_ulong start, abi_ulong len, int flags);
bellard54936002003-05-13 00:25:15 +0000206
bellardedf779f2004-02-22 13:40:13 +0000207/* user access */
208
209#define VERIFY_READ 0
210#define VERIFY_WRITE 1
211
ths3d97b402007-11-02 19:02:07 +0000212#define access_ok(type,addr,size) \
213 (page_check_range((target_ulong)addr,size,(type==VERIFY_READ)?PAGE_READ:PAGE_WRITE)==0)
bellardedf779f2004-02-22 13:40:13 +0000214
ths89343ec2007-11-02 20:24:22 +0000215/* NOTE __get_user and __put_user use host pointers and don't check access. */
216#define __put_user(x, hptr)\
bellardedf779f2004-02-22 13:40:13 +0000217({\
ths89343ec2007-11-02 20:24:22 +0000218 int size = sizeof(*hptr);\
bellardedf779f2004-02-22 13:40:13 +0000219 switch(size) {\
220 case 1:\
ths89343ec2007-11-02 20:24:22 +0000221 *(uint8_t *)(hptr) = (typeof(*hptr))(x);\
bellardedf779f2004-02-22 13:40:13 +0000222 break;\
223 case 2:\
ths89343ec2007-11-02 20:24:22 +0000224 *(uint16_t *)(hptr) = tswap16((typeof(*hptr))(x));\
bellardedf779f2004-02-22 13:40:13 +0000225 break;\
226 case 4:\
ths89343ec2007-11-02 20:24:22 +0000227 *(uint32_t *)(hptr) = tswap32((typeof(*hptr))(x));\
bellardedf779f2004-02-22 13:40:13 +0000228 break;\
229 case 8:\
ths89343ec2007-11-02 20:24:22 +0000230 *(uint64_t *)(hptr) = tswap64((typeof(*hptr))(x));\
bellardedf779f2004-02-22 13:40:13 +0000231 break;\
232 default:\
233 abort();\
234 }\
235 0;\
236})
237
ths89343ec2007-11-02 20:24:22 +0000238#define __get_user(x, hptr) \
bellardedf779f2004-02-22 13:40:13 +0000239({\
ths89343ec2007-11-02 20:24:22 +0000240 int size = sizeof(*hptr);\
bellardedf779f2004-02-22 13:40:13 +0000241 switch(size) {\
242 case 1:\
ths89343ec2007-11-02 20:24:22 +0000243 x = (typeof(*hptr))*(uint8_t *)(hptr);\
bellardedf779f2004-02-22 13:40:13 +0000244 break;\
245 case 2:\
ths89343ec2007-11-02 20:24:22 +0000246 x = (typeof(*hptr))tswap16(*(uint16_t *)(hptr));\
bellardedf779f2004-02-22 13:40:13 +0000247 break;\
248 case 4:\
ths89343ec2007-11-02 20:24:22 +0000249 x = (typeof(*hptr))tswap32(*(uint32_t *)(hptr));\
bellardedf779f2004-02-22 13:40:13 +0000250 break;\
251 case 8:\
ths89343ec2007-11-02 20:24:22 +0000252 x = (typeof(*hptr))tswap64(*(uint64_t *)(hptr));\
bellardedf779f2004-02-22 13:40:13 +0000253 break;\
254 default:\
255 abort();\
256 }\
257 0;\
258})
259
bellardedf779f2004-02-22 13:40:13 +0000260#define put_user(x,ptr)\
261({\
262 int __ret;\
263 if (access_ok(VERIFY_WRITE, ptr, sizeof(*ptr)))\
264 __ret = __put_user(x, ptr);\
265 else\
266 __ret = -EFAULT;\
267 __ret;\
268})
269
270#define get_user(x,ptr)\
271({\
272 int __ret;\
273 if (access_ok(VERIFY_READ, ptr, sizeof(*ptr)))\
274 __ret = __get_user(x, ptr);\
275 else\
276 __ret = -EFAULT;\
277 __ret;\
278})
279
pbrook53a59602006-03-25 19:31:22 +0000280/* Functions for accessing guest memory. The tget and tput functions
281 read/write single values, byteswapping as neccessary. The lock_user
282 gets a pointer to a contiguous area of guest memory, but does not perform
283 and byteswapping. lock_user may return either a pointer to the guest
284 memory, or a temporary buffer. */
285
286/* Lock an area of guest memory into the host. If copy is true then the
287 host area will have the same contents as the guest. */
blueswir1992f48a2007-10-14 16:27:31 +0000288static inline void *lock_user(abi_ulong guest_addr, long len, int copy)
bellardedf779f2004-02-22 13:40:13 +0000289{
pbrook53a59602006-03-25 19:31:22 +0000290#ifdef DEBUG_REMAP
291 void *addr;
292 addr = malloc(len);
293 if (copy)
294 memcpy(addr, g2h(guest_addr), len);
bellardedf779f2004-02-22 13:40:13 +0000295 else
pbrook53a59602006-03-25 19:31:22 +0000296 memset(addr, 0, len);
297 return addr;
298#else
299 return g2h(guest_addr);
300#endif
bellardedf779f2004-02-22 13:40:13 +0000301}
302
pbrook53a59602006-03-25 19:31:22 +0000303/* Unlock an area of guest memory. The first LEN bytes must be flushed back
304 to guest memory. */
blueswir1992f48a2007-10-14 16:27:31 +0000305static inline void unlock_user(void *host_addr, abi_ulong guest_addr,
306 long len)
bellardedf779f2004-02-22 13:40:13 +0000307{
pbrook53a59602006-03-25 19:31:22 +0000308#ifdef DEBUG_REMAP
309 if (host_addr == g2h(guest_addr))
310 return;
311 if (len > 0)
312 memcpy(g2h(guest_addr), host_addr, len);
313 free(host_addr);
314#endif
bellardedf779f2004-02-22 13:40:13 +0000315}
316
pbrook53a59602006-03-25 19:31:22 +0000317/* Return the length of a string in target memory. */
blueswir1992f48a2007-10-14 16:27:31 +0000318static inline int target_strlen(abi_ulong ptr)
bellardedf779f2004-02-22 13:40:13 +0000319{
pbrook53a59602006-03-25 19:31:22 +0000320 return strlen(g2h(ptr));
bellardedf779f2004-02-22 13:40:13 +0000321}
322
pbrook53a59602006-03-25 19:31:22 +0000323/* Like lock_user but for null terminated strings. */
blueswir1992f48a2007-10-14 16:27:31 +0000324static inline void *lock_user_string(abi_ulong guest_addr)
pbrook53a59602006-03-25 19:31:22 +0000325{
326 long len;
327 len = target_strlen(guest_addr) + 1;
328 return lock_user(guest_addr, len, 1);
329}
330
331/* Helper macros for locking/ulocking a target struct. */
332#define lock_user_struct(host_ptr, guest_addr, copy) \
333 host_ptr = lock_user(guest_addr, sizeof(*host_ptr), copy)
334#define unlock_user_struct(host_ptr, guest_addr, copy) \
335 unlock_user(host_ptr, guest_addr, (copy) ? sizeof(*host_ptr) : 0)
336
337#define tget8(addr) ldub(addr)
338#define tput8(addr, val) stb(addr, val)
339#define tget16(addr) lduw(addr)
340#define tput16(addr, val) stw(addr, val)
341#define tget32(addr) ldl(addr)
342#define tput32(addr, val) stl(addr, val)
343#define tget64(addr) ldq(addr)
344#define tput64(addr, val) stq(addr, val)
blueswir1992f48a2007-10-14 16:27:31 +0000345#if TARGET_ABI_BITS == 64
pbrook53a59602006-03-25 19:31:22 +0000346#define tgetl(addr) ldq(addr)
347#define tputl(addr, val) stq(addr, val)
348#else
349#define tgetl(addr) ldl(addr)
350#define tputl(addr, val) stl(addr, val)
351#endif
352
bellarde88de092005-02-07 12:35:39 +0000353#endif /* QEMU_H */