bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 1 | /* |
bellard | 93ac68b | 2003-09-30 20:57:29 +0000 | [diff] [blame] | 2 | * qemu user main |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 3 | * |
| 4 | * Copyright (c) 2003 Fabrice Bellard |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 19 | */ |
| 20 | #include <stdlib.h> |
| 21 | #include <stdio.h> |
| 22 | #include <stdarg.h> |
bellard | 04369ff | 2003-03-20 22:33:23 +0000 | [diff] [blame] | 23 | #include <string.h> |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 24 | #include <errno.h> |
bellard | 0ecfa99 | 2003-03-03 14:32:43 +0000 | [diff] [blame] | 25 | #include <unistd.h> |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 26 | |
bellard | 3ef693a | 2003-03-23 20:17:16 +0000 | [diff] [blame] | 27 | #include "qemu.h" |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 28 | |
bellard | 3ef693a | 2003-03-23 20:17:16 +0000 | [diff] [blame] | 29 | #define DEBUG_LOGFILE "/tmp/qemu.log" |
bellard | 586314f | 2003-03-03 15:02:29 +0000 | [diff] [blame] | 30 | |
bellard | 74cd30b | 2003-04-11 00:13:41 +0000 | [diff] [blame] | 31 | static const char *interp_prefix = CONFIG_QEMU_PREFIX; |
bellard | 586314f | 2003-03-03 15:02:29 +0000 | [diff] [blame] | 32 | |
bellard | 3a4739d | 2003-10-28 00:48:22 +0000 | [diff] [blame] | 33 | #if defined(__i386__) && !defined(CONFIG_STATIC) |
bellard | f801f97 | 2003-04-07 21:31:06 +0000 | [diff] [blame] | 34 | /* Force usage of an ELF interpreter even if it is an ELF shared |
| 35 | object ! */ |
| 36 | const char interp[] __attribute__((section(".interp"))) = "/lib/ld-linux.so.2"; |
bellard | 4304763 | 2003-05-29 20:05:35 +0000 | [diff] [blame] | 37 | #endif |
bellard | 74cd30b | 2003-04-11 00:13:41 +0000 | [diff] [blame] | 38 | |
bellard | 93ac68b | 2003-09-30 20:57:29 +0000 | [diff] [blame] | 39 | /* for recent libc, we add these dummy symbols which are not declared |
bellard | 74cd30b | 2003-04-11 00:13:41 +0000 | [diff] [blame] | 40 | when generating a linked object (bug in ld ?) */ |
| 41 | #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 3) |
bellard | c698105 | 2004-02-16 21:49:03 +0000 | [diff] [blame] | 42 | long __preinit_array_start[0]; |
| 43 | long __preinit_array_end[0]; |
bellard | 74cd30b | 2003-04-11 00:13:41 +0000 | [diff] [blame] | 44 | long __init_array_start[0]; |
| 45 | long __init_array_end[0]; |
| 46 | long __fini_array_start[0]; |
| 47 | long __fini_array_end[0]; |
| 48 | #endif |
| 49 | |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 50 | /* XXX: on x86 MAP_GROWSDOWN only works if ESP <= address + 32, so |
| 51 | we allocate a bigger stack. Need a better solution, for example |
| 52 | by remapping the process stack directly at the right place */ |
| 53 | unsigned long x86_stack_size = 512 * 1024; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 54 | |
| 55 | void gemu_log(const char *fmt, ...) |
| 56 | { |
| 57 | va_list ap; |
| 58 | |
| 59 | va_start(ap, fmt); |
| 60 | vfprintf(stderr, fmt, ap); |
| 61 | va_end(ap); |
| 62 | } |
| 63 | |
bellard | 61190b1 | 2004-01-04 23:54:31 +0000 | [diff] [blame] | 64 | void cpu_outb(CPUState *env, int addr, int val) |
bellard | 367e86e | 2003-03-01 17:13:26 +0000 | [diff] [blame] | 65 | { |
| 66 | fprintf(stderr, "outb: port=0x%04x, data=%02x\n", addr, val); |
| 67 | } |
| 68 | |
bellard | 61190b1 | 2004-01-04 23:54:31 +0000 | [diff] [blame] | 69 | void cpu_outw(CPUState *env, int addr, int val) |
bellard | 367e86e | 2003-03-01 17:13:26 +0000 | [diff] [blame] | 70 | { |
| 71 | fprintf(stderr, "outw: port=0x%04x, data=%04x\n", addr, val); |
| 72 | } |
| 73 | |
bellard | 61190b1 | 2004-01-04 23:54:31 +0000 | [diff] [blame] | 74 | void cpu_outl(CPUState *env, int addr, int val) |
bellard | 367e86e | 2003-03-01 17:13:26 +0000 | [diff] [blame] | 75 | { |
| 76 | fprintf(stderr, "outl: port=0x%04x, data=%08x\n", addr, val); |
| 77 | } |
| 78 | |
bellard | 61190b1 | 2004-01-04 23:54:31 +0000 | [diff] [blame] | 79 | int cpu_inb(CPUState *env, int addr) |
bellard | 367e86e | 2003-03-01 17:13:26 +0000 | [diff] [blame] | 80 | { |
| 81 | fprintf(stderr, "inb: port=0x%04x\n", addr); |
| 82 | return 0; |
| 83 | } |
| 84 | |
bellard | 61190b1 | 2004-01-04 23:54:31 +0000 | [diff] [blame] | 85 | int cpu_inw(CPUState *env, int addr) |
bellard | 367e86e | 2003-03-01 17:13:26 +0000 | [diff] [blame] | 86 | { |
| 87 | fprintf(stderr, "inw: port=0x%04x\n", addr); |
| 88 | return 0; |
| 89 | } |
| 90 | |
bellard | 61190b1 | 2004-01-04 23:54:31 +0000 | [diff] [blame] | 91 | int cpu_inl(CPUState *env, int addr) |
bellard | 367e86e | 2003-03-01 17:13:26 +0000 | [diff] [blame] | 92 | { |
| 93 | fprintf(stderr, "inl: port=0x%04x\n", addr); |
| 94 | return 0; |
| 95 | } |
| 96 | |
bellard | a541f29 | 2004-04-12 20:39:29 +0000 | [diff] [blame^] | 97 | int cpu_get_pic_interrupt(CPUState *env) |
bellard | 92ccca6 | 2003-06-24 13:30:31 +0000 | [diff] [blame] | 98 | { |
| 99 | return -1; |
| 100 | } |
| 101 | |
bellard | a541f29 | 2004-04-12 20:39:29 +0000 | [diff] [blame^] | 102 | #ifdef TARGET_I386 |
| 103 | /***********************************************************/ |
| 104 | /* CPUX86 core interface */ |
| 105 | |
bellard | f4beb51 | 2003-05-27 23:28:08 +0000 | [diff] [blame] | 106 | static void write_dt(void *ptr, unsigned long addr, unsigned long limit, |
| 107 | int flags) |
bellard | 6dbad63 | 2003-03-16 18:05:05 +0000 | [diff] [blame] | 108 | { |
bellard | f4beb51 | 2003-05-27 23:28:08 +0000 | [diff] [blame] | 109 | unsigned int e1, e2; |
bellard | 6dbad63 | 2003-03-16 18:05:05 +0000 | [diff] [blame] | 110 | e1 = (addr << 16) | (limit & 0xffff); |
| 111 | e2 = ((addr >> 16) & 0xff) | (addr & 0xff000000) | (limit & 0x000f0000); |
bellard | f4beb51 | 2003-05-27 23:28:08 +0000 | [diff] [blame] | 112 | e2 |= flags; |
| 113 | stl((uint8_t *)ptr, e1); |
| 114 | stl((uint8_t *)ptr + 4, e2); |
| 115 | } |
| 116 | |
| 117 | static void set_gate(void *ptr, unsigned int type, unsigned int dpl, |
| 118 | unsigned long addr, unsigned int sel) |
| 119 | { |
| 120 | unsigned int e1, e2; |
| 121 | e1 = (addr & 0xffff) | (sel << 16); |
| 122 | e2 = (addr & 0xffff0000) | 0x8000 | (dpl << 13) | (type << 8); |
bellard | 6dbad63 | 2003-03-16 18:05:05 +0000 | [diff] [blame] | 123 | stl((uint8_t *)ptr, e1); |
| 124 | stl((uint8_t *)ptr + 4, e2); |
| 125 | } |
| 126 | |
| 127 | uint64_t gdt_table[6]; |
bellard | f4beb51 | 2003-05-27 23:28:08 +0000 | [diff] [blame] | 128 | uint64_t idt_table[256]; |
| 129 | |
| 130 | /* only dpl matters as we do only user space emulation */ |
| 131 | static void set_idt(int n, unsigned int dpl) |
| 132 | { |
| 133 | set_gate(idt_table + n, 0, dpl, 0, 0); |
| 134 | } |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 135 | |
bellard | 89e957e | 2003-05-10 12:33:15 +0000 | [diff] [blame] | 136 | void cpu_loop(CPUX86State *env) |
bellard | 1b6b029 | 2003-03-22 17:31:38 +0000 | [diff] [blame] | 137 | { |
bellard | bc8a22c | 2003-03-30 21:02:40 +0000 | [diff] [blame] | 138 | int trapnr; |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 139 | uint8_t *pc; |
| 140 | target_siginfo_t info; |
bellard | 851e67a | 2003-03-29 16:53:14 +0000 | [diff] [blame] | 141 | |
bellard | 1b6b029 | 2003-03-22 17:31:38 +0000 | [diff] [blame] | 142 | for(;;) { |
bellard | bc8a22c | 2003-03-30 21:02:40 +0000 | [diff] [blame] | 143 | trapnr = cpu_x86_exec(env); |
bellard | bc8a22c | 2003-03-30 21:02:40 +0000 | [diff] [blame] | 144 | switch(trapnr) { |
bellard | f4beb51 | 2003-05-27 23:28:08 +0000 | [diff] [blame] | 145 | case 0x80: |
| 146 | /* linux syscall */ |
| 147 | env->regs[R_EAX] = do_syscall(env, |
| 148 | env->regs[R_EAX], |
| 149 | env->regs[R_EBX], |
| 150 | env->regs[R_ECX], |
| 151 | env->regs[R_EDX], |
| 152 | env->regs[R_ESI], |
| 153 | env->regs[R_EDI], |
| 154 | env->regs[R_EBP]); |
| 155 | break; |
| 156 | case EXCP0B_NOSEG: |
| 157 | case EXCP0C_STACK: |
| 158 | info.si_signo = SIGBUS; |
| 159 | info.si_errno = 0; |
| 160 | info.si_code = TARGET_SI_KERNEL; |
| 161 | info._sifields._sigfault._addr = 0; |
| 162 | queue_signal(info.si_signo, &info); |
| 163 | break; |
bellard | 1b6b029 | 2003-03-22 17:31:38 +0000 | [diff] [blame] | 164 | case EXCP0D_GPF: |
bellard | 851e67a | 2003-03-29 16:53:14 +0000 | [diff] [blame] | 165 | if (env->eflags & VM_MASK) { |
bellard | 89e957e | 2003-05-10 12:33:15 +0000 | [diff] [blame] | 166 | handle_vm86_fault(env); |
bellard | 1b6b029 | 2003-03-22 17:31:38 +0000 | [diff] [blame] | 167 | } else { |
bellard | f4beb51 | 2003-05-27 23:28:08 +0000 | [diff] [blame] | 168 | info.si_signo = SIGSEGV; |
| 169 | info.si_errno = 0; |
| 170 | info.si_code = TARGET_SI_KERNEL; |
| 171 | info._sifields._sigfault._addr = 0; |
| 172 | queue_signal(info.si_signo, &info); |
bellard | 1b6b029 | 2003-03-22 17:31:38 +0000 | [diff] [blame] | 173 | } |
| 174 | break; |
bellard | b689bc5 | 2003-05-08 15:33:33 +0000 | [diff] [blame] | 175 | case EXCP0E_PAGE: |
| 176 | info.si_signo = SIGSEGV; |
| 177 | info.si_errno = 0; |
| 178 | if (!(env->error_code & 1)) |
| 179 | info.si_code = TARGET_SEGV_MAPERR; |
| 180 | else |
| 181 | info.si_code = TARGET_SEGV_ACCERR; |
bellard | 970a87a | 2003-06-21 13:13:25 +0000 | [diff] [blame] | 182 | info._sifields._sigfault._addr = env->cr[2]; |
bellard | b689bc5 | 2003-05-08 15:33:33 +0000 | [diff] [blame] | 183 | queue_signal(info.si_signo, &info); |
| 184 | break; |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 185 | case EXCP00_DIVZ: |
bellard | bc8a22c | 2003-03-30 21:02:40 +0000 | [diff] [blame] | 186 | if (env->eflags & VM_MASK) { |
bellard | 447db21 | 2003-05-10 15:10:36 +0000 | [diff] [blame] | 187 | handle_vm86_trap(env, trapnr); |
bellard | bc8a22c | 2003-03-30 21:02:40 +0000 | [diff] [blame] | 188 | } else { |
| 189 | /* division by zero */ |
| 190 | info.si_signo = SIGFPE; |
| 191 | info.si_errno = 0; |
| 192 | info.si_code = TARGET_FPE_INTDIV; |
| 193 | info._sifields._sigfault._addr = env->eip; |
| 194 | queue_signal(info.si_signo, &info); |
| 195 | } |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 196 | break; |
bellard | 447db21 | 2003-05-10 15:10:36 +0000 | [diff] [blame] | 197 | case EXCP01_SSTP: |
| 198 | case EXCP03_INT3: |
| 199 | if (env->eflags & VM_MASK) { |
| 200 | handle_vm86_trap(env, trapnr); |
| 201 | } else { |
| 202 | info.si_signo = SIGTRAP; |
| 203 | info.si_errno = 0; |
| 204 | if (trapnr == EXCP01_SSTP) { |
| 205 | info.si_code = TARGET_TRAP_BRKPT; |
| 206 | info._sifields._sigfault._addr = env->eip; |
| 207 | } else { |
| 208 | info.si_code = TARGET_SI_KERNEL; |
| 209 | info._sifields._sigfault._addr = 0; |
| 210 | } |
| 211 | queue_signal(info.si_signo, &info); |
| 212 | } |
| 213 | break; |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 214 | case EXCP04_INTO: |
| 215 | case EXCP05_BOUND: |
bellard | bc8a22c | 2003-03-30 21:02:40 +0000 | [diff] [blame] | 216 | if (env->eflags & VM_MASK) { |
bellard | 447db21 | 2003-05-10 15:10:36 +0000 | [diff] [blame] | 217 | handle_vm86_trap(env, trapnr); |
bellard | bc8a22c | 2003-03-30 21:02:40 +0000 | [diff] [blame] | 218 | } else { |
| 219 | info.si_signo = SIGSEGV; |
| 220 | info.si_errno = 0; |
bellard | b689bc5 | 2003-05-08 15:33:33 +0000 | [diff] [blame] | 221 | info.si_code = TARGET_SI_KERNEL; |
bellard | bc8a22c | 2003-03-30 21:02:40 +0000 | [diff] [blame] | 222 | info._sifields._sigfault._addr = 0; |
| 223 | queue_signal(info.si_signo, &info); |
| 224 | } |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 225 | break; |
| 226 | case EXCP06_ILLOP: |
| 227 | info.si_signo = SIGILL; |
| 228 | info.si_errno = 0; |
| 229 | info.si_code = TARGET_ILL_ILLOPN; |
| 230 | info._sifields._sigfault._addr = env->eip; |
| 231 | queue_signal(info.si_signo, &info); |
| 232 | break; |
| 233 | case EXCP_INTERRUPT: |
| 234 | /* just indicate that signals should be handled asap */ |
| 235 | break; |
bellard | 1b6b029 | 2003-03-22 17:31:38 +0000 | [diff] [blame] | 236 | default: |
bellard | 970a87a | 2003-06-21 13:13:25 +0000 | [diff] [blame] | 237 | pc = env->segs[R_CS].base + env->eip; |
bellard | bc8a22c | 2003-03-30 21:02:40 +0000 | [diff] [blame] | 238 | fprintf(stderr, "qemu: 0x%08lx: unhandled CPU exception 0x%x - aborting\n", |
| 239 | (long)pc, trapnr); |
bellard | 1b6b029 | 2003-03-22 17:31:38 +0000 | [diff] [blame] | 240 | abort(); |
| 241 | } |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 242 | process_pending_signals(env); |
bellard | 1b6b029 | 2003-03-22 17:31:38 +0000 | [diff] [blame] | 243 | } |
| 244 | } |
bellard | b346ff4 | 2003-06-15 20:05:50 +0000 | [diff] [blame] | 245 | #endif |
| 246 | |
| 247 | #ifdef TARGET_ARM |
| 248 | |
bellard | b346ff4 | 2003-06-15 20:05:50 +0000 | [diff] [blame] | 249 | void cpu_loop(CPUARMState *env) |
| 250 | { |
| 251 | int trapnr; |
| 252 | unsigned int n, insn; |
| 253 | target_siginfo_t info; |
| 254 | |
| 255 | for(;;) { |
| 256 | trapnr = cpu_arm_exec(env); |
| 257 | switch(trapnr) { |
| 258 | case EXCP_UDEF: |
bellard | c698105 | 2004-02-16 21:49:03 +0000 | [diff] [blame] | 259 | { |
| 260 | TaskState *ts = env->opaque; |
| 261 | uint32_t opcode; |
| 262 | |
| 263 | /* we handle the FPU emulation here, as Linux */ |
| 264 | /* we get the opcode */ |
| 265 | opcode = ldl_raw((uint8_t *)env->regs[15]); |
| 266 | |
| 267 | if (EmulateAll(opcode, &ts->fpa, env->regs) == 0) { |
| 268 | info.si_signo = SIGILL; |
| 269 | info.si_errno = 0; |
| 270 | info.si_code = TARGET_ILL_ILLOPN; |
| 271 | info._sifields._sigfault._addr = env->regs[15]; |
| 272 | queue_signal(info.si_signo, &info); |
| 273 | } else { |
| 274 | /* increment PC */ |
| 275 | env->regs[15] += 4; |
| 276 | } |
| 277 | } |
bellard | b346ff4 | 2003-06-15 20:05:50 +0000 | [diff] [blame] | 278 | break; |
| 279 | case EXCP_SWI: |
| 280 | { |
| 281 | /* system call */ |
| 282 | insn = ldl((void *)(env->regs[15] - 4)); |
| 283 | n = insn & 0xffffff; |
| 284 | if (n >= ARM_SYSCALL_BASE) { |
| 285 | /* linux syscall */ |
| 286 | n -= ARM_SYSCALL_BASE; |
| 287 | env->regs[0] = do_syscall(env, |
| 288 | n, |
| 289 | env->regs[0], |
| 290 | env->regs[1], |
| 291 | env->regs[2], |
| 292 | env->regs[3], |
| 293 | env->regs[4], |
| 294 | 0); |
| 295 | } else { |
| 296 | goto error; |
| 297 | } |
| 298 | } |
| 299 | break; |
bellard | 43fff23 | 2003-07-09 19:31:39 +0000 | [diff] [blame] | 300 | case EXCP_INTERRUPT: |
| 301 | /* just indicate that signals should be handled asap */ |
| 302 | break; |
bellard | b346ff4 | 2003-06-15 20:05:50 +0000 | [diff] [blame] | 303 | default: |
| 304 | error: |
| 305 | fprintf(stderr, "qemu: unhandled CPU exception 0x%x - aborting\n", |
| 306 | trapnr); |
| 307 | cpu_arm_dump_state(env, stderr, 0); |
| 308 | abort(); |
| 309 | } |
| 310 | process_pending_signals(env); |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | #endif |
bellard | 1b6b029 | 2003-03-22 17:31:38 +0000 | [diff] [blame] | 315 | |
bellard | 93ac68b | 2003-09-30 20:57:29 +0000 | [diff] [blame] | 316 | #ifdef TARGET_SPARC |
| 317 | |
bellard | 060366c | 2004-01-04 15:50:01 +0000 | [diff] [blame] | 318 | //#define DEBUG_WIN |
| 319 | |
| 320 | /* WARNING: dealing with register windows _is_ complicated */ |
| 321 | static inline int get_reg_index(CPUSPARCState *env, int cwp, int index) |
| 322 | { |
| 323 | index = (index + cwp * 16) & (16 * NWINDOWS - 1); |
| 324 | /* wrap handling : if cwp is on the last window, then we use the |
| 325 | registers 'after' the end */ |
| 326 | if (index < 8 && env->cwp == (NWINDOWS - 1)) |
| 327 | index += (16 * NWINDOWS); |
| 328 | return index; |
| 329 | } |
| 330 | |
| 331 | static inline void save_window_offset(CPUSPARCState *env, int offset) |
| 332 | { |
| 333 | unsigned int new_wim, i, cwp1; |
| 334 | uint32_t *sp_ptr; |
| 335 | |
| 336 | new_wim = ((env->wim >> 1) | (env->wim << (NWINDOWS - 1))) & |
| 337 | ((1LL << NWINDOWS) - 1); |
| 338 | /* save the window */ |
| 339 | cwp1 = (env->cwp + offset) & (NWINDOWS - 1); |
| 340 | sp_ptr = (uint32_t *)(env->regbase[get_reg_index(env, cwp1, 6)]); |
| 341 | #if defined(DEBUG_WIN) |
| 342 | printf("win_overflow: sp_ptr=0x%x save_cwp=%d\n", |
| 343 | (int)sp_ptr, cwp1); |
| 344 | #endif |
| 345 | for(i = 0; i < 16; i++) |
| 346 | stl_raw(sp_ptr + i, env->regbase[get_reg_index(env, cwp1, 8 + i)]); |
| 347 | env->wim = new_wim; |
| 348 | } |
| 349 | |
| 350 | static void save_window(CPUSPARCState *env) |
| 351 | { |
| 352 | save_window_offset(env, 2); |
| 353 | } |
| 354 | |
| 355 | static void restore_window(CPUSPARCState *env) |
| 356 | { |
| 357 | unsigned int new_wim, i, cwp1; |
| 358 | uint32_t *sp_ptr; |
| 359 | |
| 360 | new_wim = ((env->wim << 1) | (env->wim >> (NWINDOWS - 1))) & |
| 361 | ((1LL << NWINDOWS) - 1); |
| 362 | |
| 363 | /* restore the invalid window */ |
| 364 | cwp1 = (env->cwp + 1) & (NWINDOWS - 1); |
| 365 | sp_ptr = (uint32_t *)(env->regbase[get_reg_index(env, cwp1, 6)]); |
| 366 | #if defined(DEBUG_WIN) |
| 367 | printf("win_underflow: sp_ptr=0x%x load_cwp=%d\n", |
| 368 | (int)sp_ptr, cwp1); |
| 369 | #endif |
| 370 | for(i = 0; i < 16; i++) |
| 371 | env->regbase[get_reg_index(env, cwp1, 8 + i)] = ldl_raw(sp_ptr + i); |
| 372 | env->wim = new_wim; |
| 373 | } |
| 374 | |
| 375 | static void flush_windows(CPUSPARCState *env) |
| 376 | { |
| 377 | int offset, cwp1; |
| 378 | #if defined(DEBUG_WIN) |
| 379 | printf("flush_windows:\n"); |
| 380 | #endif |
| 381 | offset = 2; |
| 382 | for(;;) { |
| 383 | /* if restore would invoke restore_window(), then we can stop */ |
| 384 | cwp1 = (env->cwp + 1) & (NWINDOWS - 1); |
| 385 | if (env->wim & (1 << cwp1)) |
| 386 | break; |
| 387 | #if defined(DEBUG_WIN) |
| 388 | printf("offset=%d: ", offset); |
| 389 | #endif |
| 390 | save_window_offset(env, offset); |
| 391 | offset++; |
| 392 | } |
| 393 | } |
| 394 | |
bellard | 93ac68b | 2003-09-30 20:57:29 +0000 | [diff] [blame] | 395 | void cpu_loop (CPUSPARCState *env) |
| 396 | { |
bellard | 060366c | 2004-01-04 15:50:01 +0000 | [diff] [blame] | 397 | int trapnr, ret; |
| 398 | |
| 399 | while (1) { |
| 400 | trapnr = cpu_sparc_exec (env); |
| 401 | |
| 402 | switch (trapnr) { |
| 403 | case 0x88: |
| 404 | case 0x90: |
| 405 | ret = do_syscall (env, env->gregs[1], |
| 406 | env->regwptr[0], env->regwptr[1], |
| 407 | env->regwptr[2], env->regwptr[3], |
| 408 | env->regwptr[4], env->regwptr[5]); |
| 409 | if ((unsigned int)ret >= (unsigned int)(-515)) { |
| 410 | env->psr |= PSR_CARRY; |
| 411 | ret = -ret; |
| 412 | } else { |
| 413 | env->psr &= ~PSR_CARRY; |
| 414 | } |
| 415 | env->regwptr[0] = ret; |
| 416 | /* next instruction */ |
| 417 | env->pc = env->npc; |
| 418 | env->npc = env->npc + 4; |
| 419 | break; |
| 420 | case 0x83: /* flush windows */ |
| 421 | // flush_windows(env); |
| 422 | /* next instruction */ |
| 423 | env->pc = env->npc; |
| 424 | env->npc = env->npc + 4; |
| 425 | break; |
| 426 | case TT_WIN_OVF: /* window overflow */ |
| 427 | save_window(env); |
| 428 | break; |
| 429 | case TT_WIN_UNF: /* window underflow */ |
| 430 | restore_window(env); |
| 431 | break; |
| 432 | default: |
| 433 | printf ("Unhandled trap: 0x%x\n", trapnr); |
| 434 | cpu_sparc_dump_state(env, stderr, 0); |
| 435 | exit (1); |
| 436 | } |
| 437 | process_pending_signals (env); |
| 438 | } |
bellard | 93ac68b | 2003-09-30 20:57:29 +0000 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | #endif |
| 442 | |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 443 | #ifdef TARGET_PPC |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 444 | void cpu_loop(CPUPPCState *env) |
| 445 | { |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 446 | target_siginfo_t info; |
bellard | 61190b1 | 2004-01-04 23:54:31 +0000 | [diff] [blame] | 447 | int trapnr; |
| 448 | uint32_t ret; |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 449 | |
| 450 | for(;;) { |
| 451 | trapnr = cpu_ppc_exec(env); |
bellard | 61190b1 | 2004-01-04 23:54:31 +0000 | [diff] [blame] | 452 | if (trapnr != EXCP_SYSCALL_USER && trapnr != EXCP_BRANCH && |
| 453 | trapnr != EXCP_TRACE) { |
| 454 | if (loglevel > 0) { |
| 455 | cpu_ppc_dump_state(env, logfile, 0); |
| 456 | } |
| 457 | } |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 458 | switch(trapnr) { |
| 459 | case EXCP_NONE: |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 460 | break; |
bellard | 61190b1 | 2004-01-04 23:54:31 +0000 | [diff] [blame] | 461 | case EXCP_SYSCALL_USER: |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 462 | /* system call */ |
| 463 | /* WARNING: |
| 464 | * PPC ABI uses overflow flag in cr0 to signal an error |
| 465 | * in syscalls. |
| 466 | */ |
bellard | 61190b1 | 2004-01-04 23:54:31 +0000 | [diff] [blame] | 467 | #if 0 |
| 468 | printf("syscall %d 0x%08x 0x%08x 0x%08x 0x%08x\n", env->gpr[0], |
| 469 | env->gpr[3], env->gpr[4], env->gpr[5], env->gpr[6]); |
| 470 | #endif |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 471 | env->crf[0] &= ~0x1; |
| 472 | ret = do_syscall(env, env->gpr[0], env->gpr[3], env->gpr[4], |
| 473 | env->gpr[5], env->gpr[6], env->gpr[7], |
| 474 | env->gpr[8]); |
| 475 | if (ret > (uint32_t)(-515)) { |
| 476 | env->crf[0] |= 0x1; |
| 477 | ret = -ret; |
| 478 | } |
| 479 | env->gpr[3] = ret; |
bellard | 61190b1 | 2004-01-04 23:54:31 +0000 | [diff] [blame] | 480 | #if 0 |
| 481 | printf("syscall returned 0x%08x (%d)\n", ret, ret); |
| 482 | #endif |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 483 | break; |
bellard | 61190b1 | 2004-01-04 23:54:31 +0000 | [diff] [blame] | 484 | case EXCP_RESET: |
| 485 | /* Should not happen ! */ |
| 486 | fprintf(stderr, "RESET asked... Stop emulation\n"); |
| 487 | if (loglevel) |
| 488 | fprintf(logfile, "RESET asked... Stop emulation\n"); |
| 489 | abort(); |
| 490 | case EXCP_MACHINE_CHECK: |
| 491 | fprintf(stderr, "Machine check exeption... Stop emulation\n"); |
| 492 | if (loglevel) |
| 493 | fprintf(logfile, "RESET asked... Stop emulation\n"); |
| 494 | info.si_signo = TARGET_SIGBUS; |
| 495 | info.si_errno = 0; |
| 496 | info.si_code = TARGET_BUS_OBJERR; |
| 497 | info._sifields._sigfault._addr = env->nip - 4; |
| 498 | queue_signal(info.si_signo, &info); |
| 499 | case EXCP_DSI: |
| 500 | fprintf(stderr, "Invalid data memory access: 0x%08x\n", env->spr[DAR]); |
| 501 | if (loglevel) { |
| 502 | fprintf(logfile, "Invalid data memory access: 0x%08x\n", |
| 503 | env->spr[DAR]); |
| 504 | } |
| 505 | switch (env->error_code & 0xF) { |
| 506 | case EXCP_DSI_TRANSLATE: |
| 507 | info.si_signo = TARGET_SIGSEGV; |
| 508 | info.si_errno = 0; |
| 509 | info.si_code = TARGET_SEGV_MAPERR; |
| 510 | break; |
| 511 | case EXCP_DSI_NOTSUP: |
| 512 | case EXCP_DSI_EXTERNAL: |
| 513 | info.si_signo = TARGET_SIGILL; |
| 514 | info.si_errno = 0; |
| 515 | info.si_code = TARGET_ILL_ILLADR; |
| 516 | break; |
| 517 | case EXCP_DSI_PROT: |
| 518 | info.si_signo = TARGET_SIGSEGV; |
| 519 | info.si_errno = 0; |
| 520 | info.si_code = TARGET_SEGV_ACCERR; |
| 521 | break; |
| 522 | case EXCP_DSI_DABR: |
| 523 | info.si_signo = TARGET_SIGTRAP; |
| 524 | info.si_errno = 0; |
| 525 | info.si_code = TARGET_TRAP_BRKPT; |
| 526 | break; |
| 527 | default: |
| 528 | /* Let's send a regular segfault... */ |
| 529 | fprintf(stderr, "Invalid segfault errno (%02x)\n", |
| 530 | env->error_code); |
| 531 | if (loglevel) { |
| 532 | fprintf(logfile, "Invalid segfault errno (%02x)\n", |
| 533 | env->error_code); |
| 534 | } |
| 535 | info.si_signo = TARGET_SIGSEGV; |
| 536 | info.si_errno = 0; |
| 537 | info.si_code = TARGET_SEGV_MAPERR; |
| 538 | break; |
| 539 | } |
| 540 | info._sifields._sigfault._addr = env->nip; |
| 541 | queue_signal(info.si_signo, &info); |
| 542 | break; |
| 543 | case EXCP_ISI: |
| 544 | fprintf(stderr, "Invalid instruction fetch\n"); |
| 545 | if (loglevel) |
| 546 | fprintf(logfile, "Invalid instruction fetch\n"); |
| 547 | switch (env->error_code) { |
| 548 | case EXCP_ISI_TRANSLATE: |
| 549 | info.si_signo = TARGET_SIGSEGV; |
| 550 | info.si_errno = 0; |
| 551 | info.si_code = TARGET_SEGV_MAPERR; |
| 552 | break; |
| 553 | case EXCP_ISI_GUARD: |
| 554 | info.si_signo = TARGET_SIGILL; |
| 555 | info.si_errno = 0; |
| 556 | info.si_code = TARGET_ILL_ILLADR; |
| 557 | break; |
| 558 | case EXCP_ISI_NOEXEC: |
| 559 | case EXCP_ISI_PROT: |
| 560 | info.si_signo = TARGET_SIGSEGV; |
| 561 | info.si_errno = 0; |
| 562 | info.si_code = TARGET_SEGV_ACCERR; |
| 563 | break; |
| 564 | default: |
| 565 | /* Let's send a regular segfault... */ |
| 566 | fprintf(stderr, "Invalid segfault errno (%02x)\n", |
| 567 | env->error_code); |
| 568 | if (loglevel) { |
| 569 | fprintf(logfile, "Invalid segfault errno (%02x)\n", |
| 570 | env->error_code); |
| 571 | } |
| 572 | info.si_signo = TARGET_SIGSEGV; |
| 573 | info.si_errno = 0; |
| 574 | info.si_code = TARGET_SEGV_MAPERR; |
| 575 | break; |
| 576 | } |
| 577 | info._sifields._sigfault._addr = env->nip - 4; |
| 578 | queue_signal(info.si_signo, &info); |
| 579 | break; |
| 580 | case EXCP_EXTERNAL: |
| 581 | /* Should not happen ! */ |
| 582 | fprintf(stderr, "External interruption... Stop emulation\n"); |
| 583 | if (loglevel) |
| 584 | fprintf(logfile, "External interruption... Stop emulation\n"); |
| 585 | abort(); |
| 586 | case EXCP_ALIGN: |
| 587 | fprintf(stderr, "Invalid unaligned memory access\n"); |
| 588 | if (loglevel) |
| 589 | fprintf(logfile, "Invalid unaligned memory access\n"); |
| 590 | info.si_signo = TARGET_SIGBUS; |
| 591 | info.si_errno = 0; |
| 592 | info.si_code = TARGET_BUS_ADRALN; |
| 593 | info._sifields._sigfault._addr = env->nip - 4; |
| 594 | queue_signal(info.si_signo, &info); |
| 595 | break; |
| 596 | case EXCP_PROGRAM: |
| 597 | switch (env->error_code & ~0xF) { |
| 598 | case EXCP_FP: |
| 599 | fprintf(stderr, "Program exception\n"); |
| 600 | if (loglevel) |
| 601 | fprintf(logfile, "Program exception\n"); |
| 602 | /* Set FX */ |
| 603 | env->fpscr[7] |= 0x8; |
| 604 | /* Finally, update FEX */ |
| 605 | if ((((env->fpscr[7] & 0x3) << 3) | (env->fpscr[6] >> 1)) & |
| 606 | ((env->fpscr[1] << 1) | (env->fpscr[0] >> 3))) |
| 607 | env->fpscr[7] |= 0x4; |
| 608 | info.si_signo = TARGET_SIGFPE; |
| 609 | info.si_errno = 0; |
| 610 | switch (env->error_code & 0xF) { |
| 611 | case EXCP_FP_OX: |
| 612 | info.si_code = TARGET_FPE_FLTOVF; |
| 613 | break; |
| 614 | case EXCP_FP_UX: |
| 615 | info.si_code = TARGET_FPE_FLTUND; |
| 616 | break; |
| 617 | case EXCP_FP_ZX: |
| 618 | case EXCP_FP_VXZDZ: |
| 619 | info.si_code = TARGET_FPE_FLTDIV; |
| 620 | break; |
| 621 | case EXCP_FP_XX: |
| 622 | info.si_code = TARGET_FPE_FLTRES; |
| 623 | break; |
| 624 | case EXCP_FP_VXSOFT: |
| 625 | info.si_code = TARGET_FPE_FLTINV; |
| 626 | break; |
| 627 | case EXCP_FP_VXNAN: |
| 628 | case EXCP_FP_VXISI: |
| 629 | case EXCP_FP_VXIDI: |
| 630 | case EXCP_FP_VXIMZ: |
| 631 | case EXCP_FP_VXVC: |
| 632 | case EXCP_FP_VXSQRT: |
| 633 | case EXCP_FP_VXCVI: |
| 634 | info.si_code = TARGET_FPE_FLTSUB; |
| 635 | break; |
| 636 | default: |
| 637 | fprintf(stderr, "Unknown floating point exception " |
| 638 | "(%02x)\n", env->error_code); |
| 639 | if (loglevel) { |
| 640 | fprintf(logfile, "Unknown floating point exception " |
| 641 | "(%02x)\n", env->error_code & 0xF); |
| 642 | } |
| 643 | } |
| 644 | break; |
| 645 | case EXCP_INVAL: |
| 646 | fprintf(stderr, "Invalid instruction\n"); |
| 647 | if (loglevel) |
| 648 | fprintf(logfile, "Invalid instruction\n"); |
| 649 | info.si_signo = TARGET_SIGILL; |
| 650 | info.si_errno = 0; |
| 651 | switch (env->error_code & 0xF) { |
| 652 | case EXCP_INVAL_INVAL: |
| 653 | info.si_code = TARGET_ILL_ILLOPC; |
| 654 | break; |
| 655 | case EXCP_INVAL_LSWX: |
| 656 | info.si_code = TARGET_ILL_ILLOPN; |
| 657 | break; |
| 658 | case EXCP_INVAL_SPR: |
| 659 | info.si_code = TARGET_ILL_PRVREG; |
| 660 | break; |
| 661 | case EXCP_INVAL_FP: |
| 662 | info.si_code = TARGET_ILL_COPROC; |
| 663 | break; |
| 664 | default: |
| 665 | fprintf(stderr, "Unknown invalid operation (%02x)\n", |
| 666 | env->error_code & 0xF); |
| 667 | if (loglevel) { |
| 668 | fprintf(logfile, "Unknown invalid operation (%02x)\n", |
| 669 | env->error_code & 0xF); |
| 670 | } |
| 671 | info.si_code = TARGET_ILL_ILLADR; |
| 672 | break; |
| 673 | } |
| 674 | break; |
| 675 | case EXCP_PRIV: |
| 676 | fprintf(stderr, "Privilege violation\n"); |
| 677 | if (loglevel) |
| 678 | fprintf(logfile, "Privilege violation\n"); |
| 679 | info.si_signo = TARGET_SIGILL; |
| 680 | info.si_errno = 0; |
| 681 | switch (env->error_code & 0xF) { |
| 682 | case EXCP_PRIV_OPC: |
| 683 | info.si_code = TARGET_ILL_PRVOPC; |
| 684 | break; |
| 685 | case EXCP_PRIV_REG: |
| 686 | info.si_code = TARGET_ILL_PRVREG; |
| 687 | break; |
| 688 | default: |
| 689 | fprintf(stderr, "Unknown privilege violation (%02x)\n", |
| 690 | env->error_code & 0xF); |
| 691 | info.si_code = TARGET_ILL_PRVOPC; |
| 692 | break; |
| 693 | } |
| 694 | break; |
| 695 | case EXCP_TRAP: |
| 696 | fprintf(stderr, "Tried to call a TRAP\n"); |
| 697 | if (loglevel) |
| 698 | fprintf(logfile, "Tried to call a TRAP\n"); |
| 699 | abort(); |
| 700 | default: |
| 701 | /* Should not happen ! */ |
| 702 | fprintf(stderr, "Unknown program exception (%02x)\n", |
| 703 | env->error_code); |
| 704 | if (loglevel) { |
| 705 | fprintf(logfile, "Unknwon program exception (%02x)\n", |
| 706 | env->error_code); |
| 707 | } |
| 708 | abort(); |
| 709 | } |
| 710 | info._sifields._sigfault._addr = env->nip - 4; |
| 711 | queue_signal(info.si_signo, &info); |
| 712 | break; |
| 713 | case EXCP_NO_FP: |
| 714 | fprintf(stderr, "No floating point allowed\n"); |
| 715 | if (loglevel) |
| 716 | fprintf(logfile, "No floating point allowed\n"); |
| 717 | info.si_signo = TARGET_SIGILL; |
| 718 | info.si_errno = 0; |
| 719 | info.si_code = TARGET_ILL_COPROC; |
| 720 | info._sifields._sigfault._addr = env->nip - 4; |
| 721 | queue_signal(info.si_signo, &info); |
| 722 | break; |
| 723 | case EXCP_DECR: |
| 724 | /* Should not happen ! */ |
| 725 | fprintf(stderr, "Decrementer exception\n"); |
| 726 | if (loglevel) |
| 727 | fprintf(logfile, "Decrementer exception\n"); |
| 728 | abort(); |
| 729 | case EXCP_RESA: /* Implementation specific */ |
| 730 | /* Should not happen ! */ |
| 731 | fprintf(stderr, "RESA exception should never happen !\n"); |
| 732 | if (loglevel) |
| 733 | fprintf(logfile, "RESA exception should never happen !\n"); |
| 734 | abort(); |
| 735 | case EXCP_RESB: /* Implementation specific */ |
| 736 | /* Should not happen ! */ |
| 737 | fprintf(stderr, "RESB exception should never happen !\n"); |
| 738 | if (loglevel) |
| 739 | fprintf(logfile, "RESB exception should never happen !\n"); |
| 740 | abort(); |
| 741 | case EXCP_TRACE: |
| 742 | /* Do nothing: we use this to trace execution */ |
| 743 | break; |
| 744 | case EXCP_FP_ASSIST: |
| 745 | /* Should not happen ! */ |
| 746 | fprintf(stderr, "Floating point assist exception\n"); |
| 747 | if (loglevel) |
| 748 | fprintf(logfile, "Floating point assist exception\n"); |
| 749 | abort(); |
| 750 | case EXCP_MTMSR: |
| 751 | /* We reloaded the msr, just go on */ |
| 752 | if (msr_pr) { |
| 753 | fprintf(stderr, "Tried to go into supervisor mode !\n"); |
| 754 | if (loglevel) |
| 755 | fprintf(logfile, "Tried to go into supervisor mode !\n"); |
| 756 | abort(); |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 757 | } |
bellard | 61190b1 | 2004-01-04 23:54:31 +0000 | [diff] [blame] | 758 | break; |
| 759 | case EXCP_BRANCH: |
| 760 | /* We stopped because of a jump... */ |
| 761 | break; |
| 762 | case EXCP_RFI: |
| 763 | /* Should not occur: we always are in user mode */ |
| 764 | fprintf(stderr, "Return from interrupt ?\n"); |
| 765 | if (loglevel) |
| 766 | fprintf(logfile, "Return from interrupt ?\n"); |
| 767 | abort(); |
| 768 | case EXCP_INTERRUPT: |
| 769 | /* Don't know why this should ever happen... */ |
| 770 | break; |
bellard | a541f29 | 2004-04-12 20:39:29 +0000 | [diff] [blame^] | 771 | case EXCP_DEBUG: |
| 772 | break; |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 773 | default: |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 774 | fprintf(stderr, "qemu: unhandled CPU exception 0x%x - aborting\n", |
| 775 | trapnr); |
bellard | 61190b1 | 2004-01-04 23:54:31 +0000 | [diff] [blame] | 776 | if (loglevel) { |
| 777 | fprintf(logfile, "qemu: unhandled CPU exception 0x%02x - " |
| 778 | "0x%02x - aborting\n", trapnr, env->error_code); |
| 779 | } |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 780 | abort(); |
| 781 | } |
bellard | 61190b1 | 2004-01-04 23:54:31 +0000 | [diff] [blame] | 782 | if (trapnr < EXCP_PPC_MAX) |
| 783 | env->exceptions &= ~(1 << trapnr); |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 784 | process_pending_signals(env); |
bellard | 61190b1 | 2004-01-04 23:54:31 +0000 | [diff] [blame] | 785 | if (env->exceptions != 0) { |
| 786 | check_exception_state(env); |
| 787 | } |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 788 | } |
| 789 | } |
| 790 | #endif |
| 791 | |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 792 | void usage(void) |
| 793 | { |
bellard | 93ac68b | 2003-09-30 20:57:29 +0000 | [diff] [blame] | 794 | printf("qemu-" TARGET_ARCH " version " QEMU_VERSION ", Copyright (c) 2003 Fabrice Bellard\n" |
| 795 | "usage: qemu-" TARGET_ARCH " [-h] [-d] [-L path] [-s size] program [arguments...]\n" |
bellard | b346ff4 | 2003-06-15 20:05:50 +0000 | [diff] [blame] | 796 | "Linux CPU emulator (compiled for %s emulation)\n" |
bellard | d691f66 | 2003-03-24 21:58:34 +0000 | [diff] [blame] | 797 | "\n" |
bellard | 5493600 | 2003-05-13 00:25:15 +0000 | [diff] [blame] | 798 | "-h print this help\n" |
bellard | b346ff4 | 2003-06-15 20:05:50 +0000 | [diff] [blame] | 799 | "-L path set the elf interpreter prefix (default=%s)\n" |
| 800 | "-s size set the stack size in bytes (default=%ld)\n" |
bellard | 5493600 | 2003-05-13 00:25:15 +0000 | [diff] [blame] | 801 | "\n" |
| 802 | "debug options:\n" |
bellard | c698105 | 2004-02-16 21:49:03 +0000 | [diff] [blame] | 803 | #ifdef USE_CODE_COPY |
| 804 | "-no-code-copy disable code copy acceleration\n" |
| 805 | #endif |
bellard | 5493600 | 2003-05-13 00:25:15 +0000 | [diff] [blame] | 806 | "-d activate log (logfile=%s)\n" |
| 807 | "-p pagesize set the host page size to 'pagesize'\n", |
bellard | b346ff4 | 2003-06-15 20:05:50 +0000 | [diff] [blame] | 808 | TARGET_ARCH, |
bellard | d691f66 | 2003-03-24 21:58:34 +0000 | [diff] [blame] | 809 | interp_prefix, |
bellard | 5493600 | 2003-05-13 00:25:15 +0000 | [diff] [blame] | 810 | x86_stack_size, |
| 811 | DEBUG_LOGFILE); |
bellard | 74cd30b | 2003-04-11 00:13:41 +0000 | [diff] [blame] | 812 | _exit(1); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 813 | } |
| 814 | |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 815 | /* XXX: currently only used for async signals (see signal.c) */ |
bellard | b346ff4 | 2003-06-15 20:05:50 +0000 | [diff] [blame] | 816 | CPUState *global_env; |
bellard | 59faf6d | 2003-06-25 16:18:50 +0000 | [diff] [blame] | 817 | /* used only if single thread */ |
| 818 | CPUState *cpu_single_env = NULL; |
| 819 | |
bellard | 851e67a | 2003-03-29 16:53:14 +0000 | [diff] [blame] | 820 | /* used to free thread contexts */ |
| 821 | TaskState *first_task_state; |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 822 | |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 823 | int main(int argc, char **argv) |
| 824 | { |
| 825 | const char *filename; |
bellard | 01ffc75 | 2003-02-18 23:00:51 +0000 | [diff] [blame] | 826 | struct target_pt_regs regs1, *regs = ®s1; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 827 | struct image_info info1, *info = &info1; |
bellard | 851e67a | 2003-03-29 16:53:14 +0000 | [diff] [blame] | 828 | TaskState ts1, *ts = &ts1; |
bellard | b346ff4 | 2003-06-15 20:05:50 +0000 | [diff] [blame] | 829 | CPUState *env; |
bellard | 586314f | 2003-03-03 15:02:29 +0000 | [diff] [blame] | 830 | int optind; |
bellard | d691f66 | 2003-03-24 21:58:34 +0000 | [diff] [blame] | 831 | const char *r; |
| 832 | |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 833 | if (argc <= 1) |
| 834 | usage(); |
bellard | f801f97 | 2003-04-07 21:31:06 +0000 | [diff] [blame] | 835 | |
bellard | cc38b84 | 2003-10-27 21:16:14 +0000 | [diff] [blame] | 836 | /* init debug */ |
| 837 | cpu_set_log_filename(DEBUG_LOGFILE); |
| 838 | |
bellard | 586314f | 2003-03-03 15:02:29 +0000 | [diff] [blame] | 839 | optind = 1; |
bellard | d691f66 | 2003-03-24 21:58:34 +0000 | [diff] [blame] | 840 | for(;;) { |
| 841 | if (optind >= argc) |
| 842 | break; |
| 843 | r = argv[optind]; |
| 844 | if (r[0] != '-') |
| 845 | break; |
bellard | 586314f | 2003-03-03 15:02:29 +0000 | [diff] [blame] | 846 | optind++; |
bellard | d691f66 | 2003-03-24 21:58:34 +0000 | [diff] [blame] | 847 | r++; |
| 848 | if (!strcmp(r, "-")) { |
| 849 | break; |
| 850 | } else if (!strcmp(r, "d")) { |
bellard | e19e89a | 2004-03-21 17:08:23 +0000 | [diff] [blame] | 851 | int mask; |
| 852 | CPULogItem *item; |
| 853 | |
| 854 | mask = cpu_str_to_log_mask(optarg); |
| 855 | if (!mask) { |
| 856 | printf("Log items (comma separated):\n"); |
| 857 | for(item = cpu_log_items; item->mask != 0; item++) { |
| 858 | printf("%-10s %s\n", item->name, item->help); |
| 859 | } |
| 860 | exit(1); |
| 861 | } |
| 862 | cpu_set_log(mask); |
bellard | d691f66 | 2003-03-24 21:58:34 +0000 | [diff] [blame] | 863 | } else if (!strcmp(r, "s")) { |
| 864 | r = argv[optind++]; |
| 865 | x86_stack_size = strtol(r, (char **)&r, 0); |
| 866 | if (x86_stack_size <= 0) |
| 867 | usage(); |
| 868 | if (*r == 'M') |
| 869 | x86_stack_size *= 1024 * 1024; |
| 870 | else if (*r == 'k' || *r == 'K') |
| 871 | x86_stack_size *= 1024; |
| 872 | } else if (!strcmp(r, "L")) { |
| 873 | interp_prefix = argv[optind++]; |
bellard | 5493600 | 2003-05-13 00:25:15 +0000 | [diff] [blame] | 874 | } else if (!strcmp(r, "p")) { |
| 875 | host_page_size = atoi(argv[optind++]); |
| 876 | if (host_page_size == 0 || |
| 877 | (host_page_size & (host_page_size - 1)) != 0) { |
| 878 | fprintf(stderr, "page size must be a power of two\n"); |
| 879 | exit(1); |
| 880 | } |
bellard | c698105 | 2004-02-16 21:49:03 +0000 | [diff] [blame] | 881 | } else |
| 882 | #ifdef USE_CODE_COPY |
| 883 | if (!strcmp(r, "no-code-copy")) { |
| 884 | code_copy_enabled = 0; |
| 885 | } else |
| 886 | #endif |
| 887 | { |
bellard | d691f66 | 2003-03-24 21:58:34 +0000 | [diff] [blame] | 888 | usage(); |
| 889 | } |
bellard | 586314f | 2003-03-03 15:02:29 +0000 | [diff] [blame] | 890 | } |
bellard | d691f66 | 2003-03-24 21:58:34 +0000 | [diff] [blame] | 891 | if (optind >= argc) |
| 892 | usage(); |
bellard | 586314f | 2003-03-03 15:02:29 +0000 | [diff] [blame] | 893 | filename = argv[optind]; |
| 894 | |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 895 | /* Zero out regs */ |
bellard | 01ffc75 | 2003-02-18 23:00:51 +0000 | [diff] [blame] | 896 | memset(regs, 0, sizeof(struct target_pt_regs)); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 897 | |
| 898 | /* Zero out image_info */ |
| 899 | memset(info, 0, sizeof(struct image_info)); |
| 900 | |
bellard | 74cd30b | 2003-04-11 00:13:41 +0000 | [diff] [blame] | 901 | /* Scan interp_prefix dir for replacement files. */ |
| 902 | init_paths(interp_prefix); |
| 903 | |
bellard | 5493600 | 2003-05-13 00:25:15 +0000 | [diff] [blame] | 904 | /* NOTE: we need to init the CPU at this stage to get the |
| 905 | host_page_size */ |
bellard | b346ff4 | 2003-06-15 20:05:50 +0000 | [diff] [blame] | 906 | env = cpu_init(); |
bellard | 92ccca6 | 2003-06-24 13:30:31 +0000 | [diff] [blame] | 907 | |
bellard | 74cd30b | 2003-04-11 00:13:41 +0000 | [diff] [blame] | 908 | if (elf_exec(filename, argv+optind, environ, regs, info) != 0) { |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 909 | printf("Error loading %s\n", filename); |
bellard | 74cd30b | 2003-04-11 00:13:41 +0000 | [diff] [blame] | 910 | _exit(1); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 911 | } |
| 912 | |
bellard | 4b74fe1 | 2003-03-03 23:23:09 +0000 | [diff] [blame] | 913 | if (loglevel) { |
bellard | 5493600 | 2003-05-13 00:25:15 +0000 | [diff] [blame] | 914 | page_dump(logfile); |
| 915 | |
bellard | 4b74fe1 | 2003-03-03 23:23:09 +0000 | [diff] [blame] | 916 | fprintf(logfile, "start_brk 0x%08lx\n" , info->start_brk); |
| 917 | fprintf(logfile, "end_code 0x%08lx\n" , info->end_code); |
| 918 | fprintf(logfile, "start_code 0x%08lx\n" , info->start_code); |
| 919 | fprintf(logfile, "end_data 0x%08lx\n" , info->end_data); |
| 920 | fprintf(logfile, "start_stack 0x%08lx\n" , info->start_stack); |
| 921 | fprintf(logfile, "brk 0x%08lx\n" , info->brk); |
bellard | b346ff4 | 2003-06-15 20:05:50 +0000 | [diff] [blame] | 922 | fprintf(logfile, "entry 0x%08lx\n" , info->entry); |
bellard | 4b74fe1 | 2003-03-03 23:23:09 +0000 | [diff] [blame] | 923 | } |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 924 | |
| 925 | target_set_brk((char *)info->brk); |
| 926 | syscall_init(); |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 927 | signal_init(); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 928 | |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 929 | global_env = env; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 930 | |
bellard | 851e67a | 2003-03-29 16:53:14 +0000 | [diff] [blame] | 931 | /* build Task State */ |
| 932 | memset(ts, 0, sizeof(TaskState)); |
| 933 | env->opaque = ts; |
| 934 | ts->used = 1; |
bellard | 59faf6d | 2003-06-25 16:18:50 +0000 | [diff] [blame] | 935 | env->user_mode_only = 1; |
bellard | 851e67a | 2003-03-29 16:53:14 +0000 | [diff] [blame] | 936 | |
bellard | b346ff4 | 2003-06-15 20:05:50 +0000 | [diff] [blame] | 937 | #if defined(TARGET_I386) |
bellard | 2e255c6 | 2003-08-21 23:25:21 +0000 | [diff] [blame] | 938 | cpu_x86_set_cpl(env, 3); |
| 939 | |
bellard | 3802ce2 | 2003-07-26 18:02:28 +0000 | [diff] [blame] | 940 | env->cr[0] = CR0_PG_MASK | CR0_WP_MASK | CR0_PE_MASK; |
bellard | 75c6215 | 2004-01-04 18:08:37 +0000 | [diff] [blame] | 941 | env->hflags |= HF_PE_MASK; |
bellard | 3802ce2 | 2003-07-26 18:02:28 +0000 | [diff] [blame] | 942 | |
bellard | 415e561 | 2004-02-03 23:37:12 +0000 | [diff] [blame] | 943 | /* flags setup : we activate the IRQs by default as in user mode */ |
| 944 | env->eflags |= IF_MASK; |
| 945 | |
bellard | 6dbad63 | 2003-03-16 18:05:05 +0000 | [diff] [blame] | 946 | /* linux register setup */ |
bellard | 0ecfa99 | 2003-03-03 14:32:43 +0000 | [diff] [blame] | 947 | env->regs[R_EAX] = regs->eax; |
| 948 | env->regs[R_EBX] = regs->ebx; |
| 949 | env->regs[R_ECX] = regs->ecx; |
| 950 | env->regs[R_EDX] = regs->edx; |
| 951 | env->regs[R_ESI] = regs->esi; |
| 952 | env->regs[R_EDI] = regs->edi; |
| 953 | env->regs[R_EBP] = regs->ebp; |
| 954 | env->regs[R_ESP] = regs->esp; |
bellard | dab2ed9 | 2003-03-22 15:23:14 +0000 | [diff] [blame] | 955 | env->eip = regs->eip; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 956 | |
bellard | f4beb51 | 2003-05-27 23:28:08 +0000 | [diff] [blame] | 957 | /* linux interrupt setup */ |
| 958 | env->idt.base = (void *)idt_table; |
| 959 | env->idt.limit = sizeof(idt_table) - 1; |
| 960 | set_idt(0, 0); |
| 961 | set_idt(1, 0); |
| 962 | set_idt(2, 0); |
| 963 | set_idt(3, 3); |
| 964 | set_idt(4, 3); |
| 965 | set_idt(5, 3); |
| 966 | set_idt(6, 0); |
| 967 | set_idt(7, 0); |
| 968 | set_idt(8, 0); |
| 969 | set_idt(9, 0); |
| 970 | set_idt(10, 0); |
| 971 | set_idt(11, 0); |
| 972 | set_idt(12, 0); |
| 973 | set_idt(13, 0); |
| 974 | set_idt(14, 0); |
| 975 | set_idt(15, 0); |
| 976 | set_idt(16, 0); |
| 977 | set_idt(17, 0); |
| 978 | set_idt(18, 0); |
| 979 | set_idt(19, 0); |
| 980 | set_idt(0x80, 3); |
| 981 | |
bellard | 6dbad63 | 2003-03-16 18:05:05 +0000 | [diff] [blame] | 982 | /* linux segment setup */ |
| 983 | env->gdt.base = (void *)gdt_table; |
| 984 | env->gdt.limit = sizeof(gdt_table) - 1; |
bellard | f4beb51 | 2003-05-27 23:28:08 +0000 | [diff] [blame] | 985 | write_dt(&gdt_table[__USER_CS >> 3], 0, 0xfffff, |
| 986 | DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | DESC_S_MASK | |
| 987 | (3 << DESC_DPL_SHIFT) | (0xa << DESC_TYPE_SHIFT)); |
| 988 | write_dt(&gdt_table[__USER_DS >> 3], 0, 0xfffff, |
| 989 | DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | DESC_S_MASK | |
| 990 | (3 << DESC_DPL_SHIFT) | (0x2 << DESC_TYPE_SHIFT)); |
bellard | 6dbad63 | 2003-03-16 18:05:05 +0000 | [diff] [blame] | 991 | cpu_x86_load_seg(env, R_CS, __USER_CS); |
| 992 | cpu_x86_load_seg(env, R_DS, __USER_DS); |
| 993 | cpu_x86_load_seg(env, R_ES, __USER_DS); |
| 994 | cpu_x86_load_seg(env, R_SS, __USER_DS); |
| 995 | cpu_x86_load_seg(env, R_FS, __USER_DS); |
| 996 | cpu_x86_load_seg(env, R_GS, __USER_DS); |
bellard | 92ccca6 | 2003-06-24 13:30:31 +0000 | [diff] [blame] | 997 | |
bellard | b346ff4 | 2003-06-15 20:05:50 +0000 | [diff] [blame] | 998 | #elif defined(TARGET_ARM) |
| 999 | { |
| 1000 | int i; |
| 1001 | for(i = 0; i < 16; i++) { |
| 1002 | env->regs[i] = regs->uregs[i]; |
| 1003 | } |
| 1004 | env->cpsr = regs->uregs[16]; |
| 1005 | } |
bellard | 93ac68b | 2003-09-30 20:57:29 +0000 | [diff] [blame] | 1006 | #elif defined(TARGET_SPARC) |
bellard | 060366c | 2004-01-04 15:50:01 +0000 | [diff] [blame] | 1007 | { |
| 1008 | int i; |
| 1009 | env->pc = regs->pc; |
| 1010 | env->npc = regs->npc; |
| 1011 | env->y = regs->y; |
| 1012 | for(i = 0; i < 8; i++) |
| 1013 | env->gregs[i] = regs->u_regs[i]; |
| 1014 | for(i = 0; i < 8; i++) |
| 1015 | env->regwptr[i] = regs->u_regs[i + 8]; |
| 1016 | } |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 1017 | #elif defined(TARGET_PPC) |
| 1018 | { |
| 1019 | int i; |
bellard | 61190b1 | 2004-01-04 23:54:31 +0000 | [diff] [blame] | 1020 | for (i = 0; i < 32; i++) { |
| 1021 | if (i != 12 && i != 6) |
| 1022 | env->msr[i] = (regs->msr >> i) & 1; |
| 1023 | } |
bellard | 6786730 | 2003-11-23 17:05:30 +0000 | [diff] [blame] | 1024 | env->nip = regs->nip; |
| 1025 | for(i = 0; i < 32; i++) { |
| 1026 | env->gpr[i] = regs->gpr[i]; |
| 1027 | } |
| 1028 | } |
bellard | b346ff4 | 2003-06-15 20:05:50 +0000 | [diff] [blame] | 1029 | #else |
| 1030 | #error unsupported target CPU |
| 1031 | #endif |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 1032 | |
bellard | 1b6b029 | 2003-03-22 17:31:38 +0000 | [diff] [blame] | 1033 | cpu_loop(env); |
| 1034 | /* never exits */ |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 1035 | return 0; |
| 1036 | } |