00001 #ifndef QEMU_H
00002 #define QEMU_H
00003
00004 #include <signal.h>
00005 #include <string.h>
00006
00007 #include "cpu.h"
00008
00009 #undef DEBUG_REMAP
00010 #ifdef DEBUG_REMAP
00011 #include <stdlib.h>
00012 #endif
00013
00014 #include "qemu-types.h"
00015
00016 #include "thunk.h"
00017 #include "syscall_defs.h"
00018 #include "syscall.h"
00019 #include "target_signal.h"
00020 #include "gdbstub.h"
00021
00022 #if defined(USE_NPTL)
00023 #define THREAD __thread
00024 #else
00025 #define THREAD
00026 #endif
00027
00028
00029
00030
00031
00032 struct image_info {
00033 abi_ulong load_addr;
00034 abi_ulong start_code;
00035 abi_ulong end_code;
00036 abi_ulong start_data;
00037 abi_ulong end_data;
00038 abi_ulong start_brk;
00039 abi_ulong brk;
00040 abi_ulong start_mmap;
00041 abi_ulong mmap;
00042 abi_ulong rss;
00043 abi_ulong start_stack;
00044 abi_ulong entry;
00045 abi_ulong code_offset;
00046 abi_ulong data_offset;
00047 char **host_argv;
00048 int personality;
00049 };
00050
00051 #ifdef TARGET_I386
00052
00053 struct vm86_saved_state {
00054 uint32_t eax;
00055 uint32_t ebx;
00056 uint32_t ecx;
00057 uint32_t edx;
00058 uint32_t esi;
00059 uint32_t edi;
00060 uint32_t ebp;
00061 uint32_t esp;
00062 uint32_t eflags;
00063 uint32_t eip;
00064 uint16_t cs, ss, ds, es, fs, gs;
00065 };
00066 #endif
00067
00068 #ifdef TARGET_ARM
00069
00070 #include "nwfpe/fpa11.h"
00071 #endif
00072
00073 #define MAX_SIGQUEUE_SIZE 1024
00074
00075 struct sigqueue {
00076 struct sigqueue *next;
00077 target_siginfo_t info;
00078 };
00079
00080 struct emulated_sigtable {
00081 int pending;
00082 struct sigqueue *first;
00083 struct sigqueue info;
00084
00085 };
00086
00087
00088
00089 typedef struct TaskState {
00090 struct TaskState *next;
00091 #ifdef TARGET_ARM
00092
00093 FPA11 fpa;
00094 int swi_errno;
00095 #endif
00096 #if defined(TARGET_I386) && !defined(TARGET_X86_64)
00097 abi_ulong target_v86;
00098 struct vm86_saved_state vm86_saved_regs;
00099 struct target_vm86plus_struct vm86plus;
00100 uint32_t v86flags;
00101 uint32_t v86mask;
00102 #endif
00103 #ifdef TARGET_M68K
00104 int sim_syscalls;
00105 #endif
00106 #if defined(TARGET_ARM) || defined(TARGET_M68K)
00107
00108 uint32_t stack_base;
00109 uint32_t heap_base;
00110 uint32_t heap_limit;
00111 #endif
00112 int used;
00113 struct image_info *info;
00114
00115 struct emulated_sigtable sigtab[TARGET_NSIG];
00116 struct sigqueue sigqueue_table[MAX_SIGQUEUE_SIZE];
00117 struct sigqueue *first_free;
00118 int signal_pending;
00119
00120 uint8_t stack[0];
00121 } __attribute__((aligned(16))) TaskState;
00122
00123 extern char *exec_path;
00124 void init_task_state(TaskState *ts);
00125 extern const char *qemu_uname_release;
00126
00127
00128
00129
00130
00131
00132
00133 #define MAX_ARG_PAGES 32
00134
00135
00136
00137
00138
00139 struct linux_binprm {
00140 char buf[128];
00141 void *page[MAX_ARG_PAGES];
00142 abi_ulong p;
00143 int fd;
00144 int e_uid, e_gid;
00145 int argc, envc;
00146 char **argv;
00147 char **envp;
00148 char * filename;
00149 };
00150
00151 void do_init_thread(struct target_pt_regs *regs, struct image_info *infop);
00152 abi_ulong loader_build_argptr(int envc, int argc, abi_ulong sp,
00153 abi_ulong stringp, int push_ptr);
00154 int loader_exec(const char * filename, char ** argv, char ** envp,
00155 struct target_pt_regs * regs, struct image_info *infop);
00156
00157 int load_elf_binary(struct linux_binprm * bprm, struct target_pt_regs * regs,
00158 struct image_info * info);
00159 int load_flt_binary(struct linux_binprm * bprm, struct target_pt_regs * regs,
00160 struct image_info * info);
00161 #ifdef TARGET_HAS_ELFLOAD32
00162 int load_elf_binary_multi(struct linux_binprm *bprm,
00163 struct target_pt_regs *regs,
00164 struct image_info *info);
00165 #endif
00166
00167 abi_long memcpy_to_target(abi_ulong dest, const void *src,
00168 unsigned long len);
00169 void target_set_brk(abi_ulong new_brk);
00170 abi_long do_brk(abi_ulong new_brk);
00171 void syscall_init(void);
00172 abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
00173 abi_long arg2, abi_long arg3, abi_long arg4,
00174 abi_long arg5, abi_long arg6);
00175 void gemu_log(const char *fmt, ...) __attribute__((format(printf,1,2)));
00176 extern THREAD CPUState *thread_env;
00177 void cpu_loop(CPUState *env);
00178 void init_paths(const char *prefix);
00179 const char *path(const char *pathname);
00180 char *target_strerror(int err);
00181 int get_osversion(void);
00182 void fork_start(void);
00183 void fork_end(int child);
00184
00185 #include "qemu-log.h"
00186
00187
00188 void print_syscall(int num,
00189 abi_long arg1, abi_long arg2, abi_long arg3,
00190 abi_long arg4, abi_long arg5, abi_long arg6);
00191 void print_syscall_ret(int num, abi_long arg1);
00192 extern int do_strace;
00193
00194
00195 void process_pending_signals(CPUState *cpu_env);
00196 void signal_init(void);
00197 int queue_signal(CPUState *env, int sig, target_siginfo_t *info);
00198 void host_to_target_siginfo(target_siginfo_t *tinfo, const siginfo_t *info);
00199 void target_to_host_siginfo(siginfo_t *info, const target_siginfo_t *tinfo);
00200 int target_to_host_signal(int sig);
00201 long do_sigreturn(CPUState *env);
00202 long do_rt_sigreturn(CPUState *env);
00203 abi_long do_sigaltstack(abi_ulong uss_addr, abi_ulong uoss_addr, abi_ulong sp);
00204
00205 #ifdef TARGET_I386
00206
00207 void save_v86_state(CPUX86State *env);
00208 void handle_vm86_trap(CPUX86State *env, int trapno);
00209 void handle_vm86_fault(CPUX86State *env);
00210 int do_vm86(CPUX86State *env, long subfunction, abi_ulong v86_addr);
00211 #elif defined(TARGET_SPARC64)
00212 void sparc64_set_context(CPUSPARCState *env);
00213 void sparc64_get_context(CPUSPARCState *env);
00214 #endif
00215
00216
00217 int target_mprotect(abi_ulong start, abi_ulong len, int prot);
00218 abi_long target_mmap(abi_ulong start, abi_ulong len, int prot,
00219 int flags, int fd, abi_ulong offset);
00220 int target_munmap(abi_ulong start, abi_ulong len);
00221 abi_long target_mremap(abi_ulong old_addr, abi_ulong old_size,
00222 abi_ulong new_size, unsigned long flags,
00223 abi_ulong new_addr);
00224 int target_msync(abi_ulong start, abi_ulong len, int flags);
00225 extern unsigned long last_brk;
00226 void mmap_lock(void);
00227 void mmap_unlock(void);
00228 #if defined(USE_NPTL)
00229 void mmap_fork_start(void);
00230 void mmap_fork_end(int child);
00231 #endif
00232
00233
00234 extern unsigned long x86_stack_size;
00235
00236
00237
00238 #define VERIFY_READ 0
00239 #define VERIFY_WRITE 1
00240
00241 static inline int access_ok(int type, abi_ulong addr, abi_ulong size)
00242 {
00243 return page_check_range((target_ulong)addr, size,
00244 (type == VERIFY_READ) ? PAGE_READ : (PAGE_READ | PAGE_WRITE)) == 0;
00245 }
00246
00247
00248
00249
00250
00251 #define __put_user(x, hptr)\
00252 ({\
00253 int size = sizeof(*hptr);\
00254 switch(size) {\
00255 case 1:\
00256 *(uint8_t *)(hptr) = (uint8_t)(typeof(*hptr))(x);\
00257 break;\
00258 case 2:\
00259 *(uint16_t *)(hptr) = tswap16((typeof(*hptr))(x));\
00260 break;\
00261 case 4:\
00262 *(uint32_t *)(hptr) = tswap32((typeof(*hptr))(x));\
00263 break;\
00264 case 8:\
00265 *(uint64_t *)(hptr) = tswap64((typeof(*hptr))(x));\
00266 break;\
00267 default:\
00268 abort();\
00269 }\
00270 0;\
00271 })
00272
00273 #define __get_user(x, hptr) \
00274 ({\
00275 int size = sizeof(*hptr);\
00276 switch(size) {\
00277 case 1:\
00278 x = (typeof(*hptr))*(uint8_t *)(hptr);\
00279 break;\
00280 case 2:\
00281 x = (typeof(*hptr))tswap16(*(uint16_t *)(hptr));\
00282 break;\
00283 case 4:\
00284 x = (typeof(*hptr))tswap32(*(uint32_t *)(hptr));\
00285 break;\
00286 case 8:\
00287 x = (typeof(*hptr))tswap64(*(uint64_t *)(hptr));\
00288 break;\
00289 default:\
00290 \
00291 x = 0;\
00292 abort();\
00293 }\
00294 0;\
00295 })
00296
00297
00298
00299
00300
00301
00302 #define put_user(x, gaddr, target_type) \
00303 ({ \
00304 abi_ulong __gaddr = (gaddr); \
00305 target_type *__hptr; \
00306 abi_long __ret; \
00307 if ((__hptr = lock_user(VERIFY_WRITE, __gaddr, sizeof(target_type), 0))) { \
00308 __ret = __put_user((x), __hptr); \
00309 unlock_user(__hptr, __gaddr, sizeof(target_type)); \
00310 } else \
00311 __ret = -TARGET_EFAULT; \
00312 __ret; \
00313 })
00314
00315 #define get_user(x, gaddr, target_type) \
00316 ({ \
00317 abi_ulong __gaddr = (gaddr); \
00318 target_type *__hptr; \
00319 abi_long __ret; \
00320 if ((__hptr = lock_user(VERIFY_READ, __gaddr, sizeof(target_type), 1))) { \
00321 __ret = __get_user((x), __hptr); \
00322 unlock_user(__hptr, __gaddr, 0); \
00323 } else { \
00324 \
00325 (x) = 0; \
00326 __ret = -TARGET_EFAULT; \
00327 } \
00328 __ret; \
00329 })
00330
00331 #define put_user_ual(x, gaddr) put_user((x), (gaddr), abi_ulong)
00332 #define put_user_sal(x, gaddr) put_user((x), (gaddr), abi_long)
00333 #define put_user_u64(x, gaddr) put_user((x), (gaddr), uint64_t)
00334 #define put_user_s64(x, gaddr) put_user((x), (gaddr), int64_t)
00335 #define put_user_u32(x, gaddr) put_user((x), (gaddr), uint32_t)
00336 #define put_user_s32(x, gaddr) put_user((x), (gaddr), int32_t)
00337 #define put_user_u16(x, gaddr) put_user((x), (gaddr), uint16_t)
00338 #define put_user_s16(x, gaddr) put_user((x), (gaddr), int16_t)
00339 #define put_user_u8(x, gaddr) put_user((x), (gaddr), uint8_t)
00340 #define put_user_s8(x, gaddr) put_user((x), (gaddr), int8_t)
00341
00342 #define get_user_ual(x, gaddr) get_user((x), (gaddr), abi_ulong)
00343 #define get_user_sal(x, gaddr) get_user((x), (gaddr), abi_long)
00344 #define get_user_u64(x, gaddr) get_user((x), (gaddr), uint64_t)
00345 #define get_user_s64(x, gaddr) get_user((x), (gaddr), int64_t)
00346 #define get_user_u32(x, gaddr) get_user((x), (gaddr), uint32_t)
00347 #define get_user_s32(x, gaddr) get_user((x), (gaddr), int32_t)
00348 #define get_user_u16(x, gaddr) get_user((x), (gaddr), uint16_t)
00349 #define get_user_s16(x, gaddr) get_user((x), (gaddr), int16_t)
00350 #define get_user_u8(x, gaddr) get_user((x), (gaddr), uint8_t)
00351 #define get_user_s8(x, gaddr) get_user((x), (gaddr), int8_t)
00352
00353
00354
00355
00356
00357 abi_long copy_from_user(void *hptr, abi_ulong gaddr, size_t len);
00358 abi_long copy_to_user(abi_ulong gaddr, void *hptr, size_t len);
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368 static inline void *lock_user(int type, abi_ulong guest_addr, long len, int copy)
00369 {
00370 if (!access_ok(type, guest_addr, len))
00371 return NULL;
00372 #ifdef DEBUG_REMAP
00373 {
00374 void *addr;
00375 addr = malloc(len);
00376 if (copy)
00377 memcpy(addr, g2h(guest_addr), len);
00378 else
00379 memset(addr, 0, len);
00380 return addr;
00381 }
00382 #else
00383 return g2h(guest_addr);
00384 #endif
00385 }
00386
00387
00388
00389
00390 static inline void unlock_user(void *host_ptr, abi_ulong guest_addr,
00391 long len)
00392 {
00393
00394 #ifdef DEBUG_REMAP
00395 if (!host_ptr)
00396 return;
00397 if (host_ptr == g2h(guest_addr))
00398 return;
00399 if (len > 0)
00400 memcpy(g2h(guest_addr), host_ptr, len);
00401 free(host_ptr);
00402 #endif
00403 }
00404
00405
00406
00407 abi_long target_strlen(abi_ulong gaddr);
00408
00409
00410 static inline void *lock_user_string(abi_ulong guest_addr)
00411 {
00412 abi_long len;
00413 len = target_strlen(guest_addr);
00414 if (len < 0)
00415 return NULL;
00416 return lock_user(VERIFY_READ, guest_addr, (long)(len + 1), 1);
00417 }
00418
00419
00420 #define lock_user_struct(type, host_ptr, guest_addr, copy) \
00421 (host_ptr = lock_user(type, guest_addr, sizeof(*host_ptr), copy))
00422 #define unlock_user_struct(host_ptr, guest_addr, copy) \
00423 unlock_user(host_ptr, guest_addr, (copy) ? sizeof(*host_ptr) : 0)
00424
00425 #if defined(USE_NPTL)
00426 #include <pthread.h>
00427 #endif
00428
00429 #endif