blob: 537facbf2d504bca76916f9fbc21f41ed7e3f95b [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
bellard4683b132007-11-14 15:16:52 +000023/* for consistency, define ABI32 too */
24#if TARGET_ABI_BITS == 32
25#define TARGET_ABI32 1
26#endif
blueswir1992f48a2007-10-14 16:27:31 +000027#endif
28
29#include "thunk.h"
30#include "syscall_defs.h"
bellard6180a182003-09-30 21:04:53 +000031#include "syscall.h"
thsa04e1342007-09-27 13:57:58 +000032#include "target_signal.h"
bellard1fddef42005-04-17 19:16:13 +000033#include "gdbstub.h"
bellard66fb9762003-03-23 01:06:05 +000034
bellard31e31b82003-02-18 22:55:36 +000035/* This struct is used to hold certain information about the image.
36 * Basically, it replicates in user space what would be certain
37 * task_struct fields in the kernel
38 */
39struct image_info {
blueswir1992f48a2007-10-14 16:27:31 +000040 abi_ulong load_addr;
41 abi_ulong start_code;
42 abi_ulong end_code;
43 abi_ulong start_data;
44 abi_ulong end_data;
45 abi_ulong start_brk;
46 abi_ulong brk;
47 abi_ulong start_mmap;
48 abi_ulong mmap;
49 abi_ulong rss;
50 abi_ulong start_stack;
51 abi_ulong entry;
52 abi_ulong code_offset;
53 abi_ulong data_offset;
pbrook38d06622006-11-19 20:29:35 +000054 char **host_argv;
bellard31e31b82003-02-18 22:55:36 +000055 int personality;
56};
57
bellardb346ff42003-06-15 20:05:50 +000058#ifdef TARGET_I386
bellard851e67a2003-03-29 16:53:14 +000059/* Information about the current linux thread */
60struct vm86_saved_state {
61 uint32_t eax; /* return code */
62 uint32_t ebx;
63 uint32_t ecx;
64 uint32_t edx;
65 uint32_t esi;
66 uint32_t edi;
67 uint32_t ebp;
68 uint32_t esp;
69 uint32_t eflags;
70 uint32_t eip;
71 uint16_t cs, ss, ds, es, fs, gs;
72};
bellardb346ff42003-06-15 20:05:50 +000073#endif
bellard851e67a2003-03-29 16:53:14 +000074
bellard28c4f362004-02-16 21:47:43 +000075#ifdef TARGET_ARM
76/* FPU emulator */
77#include "nwfpe/fpa11.h"
bellard28c4f362004-02-16 21:47:43 +000078#endif
79
bellard851e67a2003-03-29 16:53:14 +000080/* NOTE: we force a big alignment so that the stack stored after is
81 aligned too */
82typedef struct TaskState {
83 struct TaskState *next;
bellard28c4f362004-02-16 21:47:43 +000084#ifdef TARGET_ARM
85 /* FPA state */
86 FPA11 fpa;
bellarda4f81972005-04-23 18:25:41 +000087 int swi_errno;
bellard28c4f362004-02-16 21:47:43 +000088#endif
j_mayer84409dd2007-04-06 08:56:50 +000089#if defined(TARGET_I386) && !defined(TARGET_X86_64)
blueswir1992f48a2007-10-14 16:27:31 +000090 abi_ulong target_v86;
bellard851e67a2003-03-29 16:53:14 +000091 struct vm86_saved_state vm86_saved_regs;
bellardb333af02003-05-14 21:48:51 +000092 struct target_vm86plus_struct vm86plus;
bellard631271d2003-05-10 13:14:52 +000093 uint32_t v86flags;
94 uint32_t v86mask;
bellardb346ff42003-06-15 20:05:50 +000095#endif
pbrooke6e59062006-10-22 00:18:54 +000096#ifdef TARGET_M68K
97 int sim_syscalls;
98#endif
pbrooka87295e2007-05-26 15:09:38 +000099#if defined(TARGET_ARM) || defined(TARGET_M68K)
100 /* Extra fields for semihosted binaries. */
101 uint32_t stack_base;
102 uint32_t heap_base;
103 uint32_t heap_limit;
104#endif
bellard851e67a2003-03-29 16:53:14 +0000105 int used; /* non zero if used */
pbrook978efd62006-06-17 18:30:42 +0000106 struct image_info *info;
bellard851e67a2003-03-29 16:53:14 +0000107 uint8_t stack[0];
108} __attribute__((aligned(16))) TaskState;
109
110extern TaskState *first_task_state;
pbrookc5937222006-05-14 11:30:38 +0000111extern const char *qemu_uname_release;
bellard851e67a2003-03-29 16:53:14 +0000112
pbrooke5fe0c52006-06-11 13:32:59 +0000113/* ??? See if we can avoid exposing so much of the loader internals. */
114/*
115 * MAX_ARG_PAGES defines the number of pages allocated for arguments
116 * and envelope for the new program. 32 should suffice, this gives
117 * a maximum env+arg of 128kB w/4KB pages!
118 */
119#define MAX_ARG_PAGES 32
120
121/*
ths5fafdf22007-09-16 21:08:06 +0000122 * This structure is used to hold the arguments that are
pbrooke5fe0c52006-06-11 13:32:59 +0000123 * used when loading binaries.
124 */
125struct linux_binprm {
126 char buf[128];
127 void *page[MAX_ARG_PAGES];
blueswir1992f48a2007-10-14 16:27:31 +0000128 abi_ulong p;
pbrooke5fe0c52006-06-11 13:32:59 +0000129 int fd;
130 int e_uid, e_gid;
131 int argc, envc;
132 char **argv;
133 char **envp;
134 char * filename; /* Name of binary */
135};
136
137void do_init_thread(struct target_pt_regs *regs, struct image_info *infop);
blueswir1992f48a2007-10-14 16:27:31 +0000138abi_ulong loader_build_argptr(int envc, int argc, abi_ulong sp,
139 abi_ulong stringp, int push_ptr);
ths5fafdf22007-09-16 21:08:06 +0000140int loader_exec(const char * filename, char ** argv, char ** envp,
bellard01ffc752003-02-18 23:00:51 +0000141 struct target_pt_regs * regs, struct image_info *infop);
bellard31e31b82003-02-18 22:55:36 +0000142
pbrooke5fe0c52006-06-11 13:32:59 +0000143int load_elf_binary(struct linux_binprm * bprm, struct target_pt_regs * regs,
144 struct image_info * info);
145int load_flt_binary(struct linux_binprm * bprm, struct target_pt_regs * regs,
146 struct image_info * info);
blueswir1cb33da52007-10-09 16:34:29 +0000147#ifdef TARGET_HAS_ELFLOAD32
148int load_elf_binary_multi(struct linux_binprm *bprm,
149 struct target_pt_regs *regs,
150 struct image_info *info);
151#endif
pbrooke5fe0c52006-06-11 13:32:59 +0000152
bellard579a97f2007-11-11 14:26:47 +0000153abi_long memcpy_to_target(abi_ulong dest, const void *src,
154 unsigned long len);
blueswir1992f48a2007-10-14 16:27:31 +0000155void target_set_brk(abi_ulong new_brk);
156abi_long do_brk(abi_ulong new_brk);
bellard31e31b82003-02-18 22:55:36 +0000157void syscall_init(void);
blueswir1992f48a2007-10-14 16:27:31 +0000158abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
159 abi_long arg2, abi_long arg3, abi_long arg4,
160 abi_long arg5, abi_long arg6);
bellard31e31b82003-02-18 22:55:36 +0000161void gemu_log(const char *fmt, ...) __attribute__((format(printf,1,2)));
bellardb346ff42003-06-15 20:05:50 +0000162extern CPUState *global_env;
163void cpu_loop(CPUState *env);
bellard32ce6332003-04-11 00:16:16 +0000164void init_paths(const char *prefix);
165const char *path(const char *pathname);
thsb92c47c2007-11-01 00:07:38 +0000166char *target_strerror(int err);
bellard6977fbf2003-04-29 20:39:23 +0000167
168extern int loglevel;
bellard631271d2003-05-10 13:14:52 +0000169extern FILE *logfile;
170
thsb92c47c2007-11-01 00:07:38 +0000171/* strace.c */
172void print_syscall(int num,
bellardc16f9ed2007-11-11 17:23:29 +0000173 abi_long arg1, abi_long arg2, abi_long arg3,
174 abi_long arg4, abi_long arg5, abi_long arg6);
175void print_syscall_ret(int num, abi_long arg1);
thsb92c47c2007-11-01 00:07:38 +0000176extern int do_strace;
177
bellardb346ff42003-06-15 20:05:50 +0000178/* signal.c */
179void process_pending_signals(void *cpu_env);
180void signal_init(void);
181int queue_signal(int sig, target_siginfo_t *info);
182void host_to_target_siginfo(target_siginfo_t *tinfo, const siginfo_t *info);
183void target_to_host_siginfo(siginfo_t *info, const target_siginfo_t *tinfo);
184long do_sigreturn(CPUState *env);
185long do_rt_sigreturn(CPUState *env);
bellard579a97f2007-11-11 14:26:47 +0000186abi_long do_sigaltstack(abi_ulong uss_addr, abi_ulong uoss_addr, abi_ulong sp);
bellardb346ff42003-06-15 20:05:50 +0000187
188#ifdef TARGET_I386
bellard631271d2003-05-10 13:14:52 +0000189/* vm86.c */
190void save_v86_state(CPUX86State *env);
bellard447db212003-05-10 15:10:36 +0000191void handle_vm86_trap(CPUX86State *env, int trapno);
bellard631271d2003-05-10 13:14:52 +0000192void handle_vm86_fault(CPUX86State *env);
blueswir1992f48a2007-10-14 16:27:31 +0000193int do_vm86(CPUX86State *env, long subfunction, abi_ulong v86_addr);
blueswir15bfb56b2007-10-05 17:01:51 +0000194#elif defined(TARGET_SPARC64)
195void sparc64_set_context(CPUSPARCState *env);
196void sparc64_get_context(CPUSPARCState *env);
bellardb346ff42003-06-15 20:05:50 +0000197#endif
bellard631271d2003-05-10 13:14:52 +0000198
bellard54936002003-05-13 00:25:15 +0000199/* mmap.c */
blueswir1992f48a2007-10-14 16:27:31 +0000200int target_mprotect(abi_ulong start, abi_ulong len, int prot);
201abi_long target_mmap(abi_ulong start, abi_ulong len, int prot,
202 int flags, int fd, abi_ulong offset);
203int target_munmap(abi_ulong start, abi_ulong len);
204abi_long target_mremap(abi_ulong old_addr, abi_ulong old_size,
205 abi_ulong new_size, unsigned long flags,
206 abi_ulong new_addr);
207int target_msync(abi_ulong start, abi_ulong len, int flags);
bellard54936002003-05-13 00:25:15 +0000208
bellardedf779f2004-02-22 13:40:13 +0000209/* user access */
210
211#define VERIFY_READ 0
bellard579a97f2007-11-11 14:26:47 +0000212#define VERIFY_WRITE 1 /* implies read access */
bellardedf779f2004-02-22 13:40:13 +0000213
bellarddae32702007-11-14 10:51:00 +0000214static inline int access_ok(int type, abi_ulong addr, abi_ulong size)
215{
216 return page_check_range((target_ulong)addr, size,
217 (type == VERIFY_READ) ? PAGE_READ : (PAGE_READ | PAGE_WRITE)) == 0;
218}
bellardedf779f2004-02-22 13:40:13 +0000219
ths89343ec2007-11-02 20:24:22 +0000220/* NOTE __get_user and __put_user use host pointers and don't check access. */
bellard579a97f2007-11-11 14:26:47 +0000221/* These are usually used to access struct data members once the
222 * struct has been locked - usually with lock_user_struct().
223 */
ths89343ec2007-11-02 20:24:22 +0000224#define __put_user(x, hptr)\
bellardedf779f2004-02-22 13:40:13 +0000225({\
ths89343ec2007-11-02 20:24:22 +0000226 int size = sizeof(*hptr);\
bellardedf779f2004-02-22 13:40:13 +0000227 switch(size) {\
228 case 1:\
ths89343ec2007-11-02 20:24:22 +0000229 *(uint8_t *)(hptr) = (typeof(*hptr))(x);\
bellardedf779f2004-02-22 13:40:13 +0000230 break;\
231 case 2:\
ths89343ec2007-11-02 20:24:22 +0000232 *(uint16_t *)(hptr) = tswap16((typeof(*hptr))(x));\
bellardedf779f2004-02-22 13:40:13 +0000233 break;\
234 case 4:\
ths89343ec2007-11-02 20:24:22 +0000235 *(uint32_t *)(hptr) = tswap32((typeof(*hptr))(x));\
bellardedf779f2004-02-22 13:40:13 +0000236 break;\
237 case 8:\
ths89343ec2007-11-02 20:24:22 +0000238 *(uint64_t *)(hptr) = tswap64((typeof(*hptr))(x));\
bellardedf779f2004-02-22 13:40:13 +0000239 break;\
240 default:\
241 abort();\
242 }\
243 0;\
244})
245
ths89343ec2007-11-02 20:24:22 +0000246#define __get_user(x, hptr) \
bellardedf779f2004-02-22 13:40:13 +0000247({\
ths89343ec2007-11-02 20:24:22 +0000248 int size = sizeof(*hptr);\
bellardedf779f2004-02-22 13:40:13 +0000249 switch(size) {\
250 case 1:\
ths89343ec2007-11-02 20:24:22 +0000251 x = (typeof(*hptr))*(uint8_t *)(hptr);\
bellardedf779f2004-02-22 13:40:13 +0000252 break;\
253 case 2:\
ths89343ec2007-11-02 20:24:22 +0000254 x = (typeof(*hptr))tswap16(*(uint16_t *)(hptr));\
bellardedf779f2004-02-22 13:40:13 +0000255 break;\
256 case 4:\
ths89343ec2007-11-02 20:24:22 +0000257 x = (typeof(*hptr))tswap32(*(uint32_t *)(hptr));\
bellardedf779f2004-02-22 13:40:13 +0000258 break;\
259 case 8:\
ths89343ec2007-11-02 20:24:22 +0000260 x = (typeof(*hptr))tswap64(*(uint64_t *)(hptr));\
bellardedf779f2004-02-22 13:40:13 +0000261 break;\
262 default:\
263 abort();\
264 }\
265 0;\
266})
267
bellard579a97f2007-11-11 14:26:47 +0000268/* put_user()/get_user() take a guest address and check access */
269/* These are usually used to access an atomic data type, such as an int,
270 * that has been passed by address. These internally perform locking
271 * and unlocking on the data type.
272 */
273#define put_user(x, gaddr, target_type) \
274({ \
275 abi_ulong __gaddr = (gaddr); \
276 target_type *__hptr; \
277 abi_long __ret; \
278 if ((__hptr = lock_user(VERIFY_WRITE, __gaddr, sizeof(target_type), 0))) { \
279 __ret = __put_user((x), __hptr); \
280 unlock_user(__hptr, __gaddr, sizeof(target_type)); \
281 } else \
282 __ret = -TARGET_EFAULT; \
283 __ret; \
bellardedf779f2004-02-22 13:40:13 +0000284})
285
bellard579a97f2007-11-11 14:26:47 +0000286#define get_user(x, gaddr, target_type) \
287({ \
288 abi_ulong __gaddr = (gaddr); \
289 target_type *__hptr; \
290 abi_long __ret; \
291 if ((__hptr = lock_user(VERIFY_READ, __gaddr, sizeof(target_type), 1))) { \
292 __ret = __get_user((x), __hptr); \
293 unlock_user(__hptr, __gaddr, 0); \
294 } else \
295 __ret = -TARGET_EFAULT; \
296 __ret; \
bellardedf779f2004-02-22 13:40:13 +0000297})
298
bellard579a97f2007-11-11 14:26:47 +0000299/* copy_from_user() and copy_to_user() are usually used to copy data
300 * buffers between the target and host. These internally perform
301 * locking/unlocking of the memory.
302 */
303abi_long copy_from_user(void *hptr, abi_ulong gaddr, size_t len);
304abi_long copy_to_user(abi_ulong gaddr, void *hptr, size_t len);
305
pbrook53a59602006-03-25 19:31:22 +0000306/* Functions for accessing guest memory. The tget and tput functions
307 read/write single values, byteswapping as neccessary. The lock_user
308 gets a pointer to a contiguous area of guest memory, but does not perform
309 and byteswapping. lock_user may return either a pointer to the guest
310 memory, or a temporary buffer. */
311
312/* Lock an area of guest memory into the host. If copy is true then the
313 host area will have the same contents as the guest. */
bellard579a97f2007-11-11 14:26:47 +0000314static inline void *lock_user(int type, abi_ulong guest_addr, long len, int copy)
bellardedf779f2004-02-22 13:40:13 +0000315{
bellard579a97f2007-11-11 14:26:47 +0000316 if (!access_ok(type, guest_addr, len))
317 return NULL;
pbrook53a59602006-03-25 19:31:22 +0000318#ifdef DEBUG_REMAP
bellard579a97f2007-11-11 14:26:47 +0000319 {
320 void *addr;
321 addr = malloc(len);
322 if (copy)
323 memcpy(addr, g2h(guest_addr), len);
324 else
325 memset(addr, 0, len);
326 return addr;
327 }
pbrook53a59602006-03-25 19:31:22 +0000328#else
329 return g2h(guest_addr);
330#endif
bellardedf779f2004-02-22 13:40:13 +0000331}
332
bellard579a97f2007-11-11 14:26:47 +0000333/* Unlock an area of guest memory. The first LEN bytes must be
334 flushed back to guest memory. host_ptr = NULL is explicitely
335 allowed and does nothing. */
336static inline void unlock_user(void *host_ptr, abi_ulong guest_addr,
blueswir1992f48a2007-10-14 16:27:31 +0000337 long len)
bellardedf779f2004-02-22 13:40:13 +0000338{
bellard579a97f2007-11-11 14:26:47 +0000339
pbrook53a59602006-03-25 19:31:22 +0000340#ifdef DEBUG_REMAP
bellard579a97f2007-11-11 14:26:47 +0000341 if (!host_ptr)
342 return;
343 if (host_ptr == g2h(guest_addr))
pbrook53a59602006-03-25 19:31:22 +0000344 return;
345 if (len > 0)
bellard579a97f2007-11-11 14:26:47 +0000346 memcpy(g2h(guest_ptr), host_ptr, len);
347 free(host_ptr);
pbrook53a59602006-03-25 19:31:22 +0000348#endif
bellardedf779f2004-02-22 13:40:13 +0000349}
350
bellard579a97f2007-11-11 14:26:47 +0000351/* Return the length of a string in target memory or -TARGET_EFAULT if
352 access error. */
353abi_long target_strlen(abi_ulong gaddr);
bellardedf779f2004-02-22 13:40:13 +0000354
pbrook53a59602006-03-25 19:31:22 +0000355/* Like lock_user but for null terminated strings. */
blueswir1992f48a2007-10-14 16:27:31 +0000356static inline void *lock_user_string(abi_ulong guest_addr)
pbrook53a59602006-03-25 19:31:22 +0000357{
bellard579a97f2007-11-11 14:26:47 +0000358 abi_long len;
359 len = target_strlen(guest_addr);
360 if (len < 0)
361 return NULL;
362 return lock_user(VERIFY_READ, guest_addr, (long)(len + 1), 1);
pbrook53a59602006-03-25 19:31:22 +0000363}
364
365/* Helper macros for locking/ulocking a target struct. */
bellard579a97f2007-11-11 14:26:47 +0000366#define lock_user_struct(type, host_ptr, guest_addr, copy) \
367 (host_ptr = lock_user(type, guest_addr, sizeof(*host_ptr), copy))
368#define unlock_user_struct(host_ptr, guest_addr, copy) \
pbrook53a59602006-03-25 19:31:22 +0000369 unlock_user(host_ptr, guest_addr, (copy) ? sizeof(*host_ptr) : 0)
370
371#define tget8(addr) ldub(addr)
372#define tput8(addr, val) stb(addr, val)
373#define tget16(addr) lduw(addr)
374#define tput16(addr, val) stw(addr, val)
375#define tget32(addr) ldl(addr)
376#define tput32(addr, val) stl(addr, val)
377#define tget64(addr) ldq(addr)
378#define tput64(addr, val) stq(addr, val)
blueswir1992f48a2007-10-14 16:27:31 +0000379#if TARGET_ABI_BITS == 64
pbrook53a59602006-03-25 19:31:22 +0000380#define tgetl(addr) ldq(addr)
381#define tputl(addr, val) stq(addr, val)
382#else
383#define tgetl(addr) ldl(addr)
384#define tputl(addr, val) stl(addr, val)
385#endif
386
bellarde88de092005-02-07 12:35:39 +0000387#endif /* QEMU_H */