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