4 * Copyright (c) 2003 Fabrice Bellard
5 * Copyright (c) 2006 Pierre d'Herbemont
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #include <sys/syscall.h>
33 #define DEBUG_LOGFILE "/tmp/qemu.log"
36 #include <crt_externs.h>
37 # define environ (*_NSGetEnviron())
40 #include <mach/mach_init.h>
41 #include <mach/vm_map.h>
43 const char *interp_prefix
= "";
45 asm(".zerofill __STD_PROG_ZONE, __STD_PROG_ZONE, __std_prog_zone, 0x0dfff000");
47 /* XXX: on x86 MAP_GROWSDOWN only works if ESP <= address + 32, so
48 we allocate a bigger stack. Need a better solution, for example
49 by remapping the process stack directly at the right place */
50 unsigned long stack_size
= 512 * 1024;
52 void qerror(const char *fmt
, ...)
57 vfprintf(stderr
, fmt
, ap
);
59 fprintf(stderr
, "\n");
63 void gemu_log(const char *fmt
, ...)
68 vfprintf(stderr
, fmt
, ap
);
72 void cpu_outb(CPUState
*env
, int addr
, int val
)
74 fprintf(stderr
, "outb: port=0x%04x, data=%02x\n", addr
, val
);
77 void cpu_outw(CPUState
*env
, int addr
, int val
)
79 fprintf(stderr
, "outw: port=0x%04x, data=%04x\n", addr
, val
);
82 void cpu_outl(CPUState
*env
, int addr
, int val
)
84 fprintf(stderr
, "outl: port=0x%04x, data=%08x\n", addr
, val
);
87 int cpu_inb(CPUState
*env
, int addr
)
89 fprintf(stderr
, "inb: port=0x%04x\n", addr
);
93 int cpu_inw(CPUState
*env
, int addr
)
95 fprintf(stderr
, "inw: port=0x%04x\n", addr
);
99 int cpu_inl(CPUState
*env
, int addr
)
101 fprintf(stderr
, "inl: port=0x%04x\n", addr
);
105 int cpu_get_pic_interrupt(CPUState
*env
)
111 static inline uint64_t cpu_ppc_get_tb (CPUState
*env
)
117 uint32_t cpu_ppc_load_tbl (CPUState
*env
)
119 return cpu_ppc_get_tb(env
) & 0xFFFFFFFF;
122 uint32_t cpu_ppc_load_tbu (CPUState
*env
)
124 return cpu_ppc_get_tb(env
) >> 32;
127 uint32_t cpu_ppc_load_atbl (CPUState
*env
)
129 return cpu_ppc_get_tb(env
) & 0xFFFFFFFF;
132 uint32_t cpu_ppc_load_atbu (CPUState
*env
)
134 return cpu_ppc_get_tb(env
) >> 32;
137 uint32_t cpu_ppc601_load_rtcu (CPUState
*env
)
139 cpu_ppc_load_tbu(env
);
142 uint32_t cpu_ppc601_load_rtcl (CPUState
*env
)
144 return cpu_ppc_load_tbl(env
) & 0x3FFFFF80;
147 /* XXX: to be fixed */
148 int ppc_dcr_read (ppc_dcr_t
*dcr_env
, int dcrn
, target_ulong
*valp
)
153 int ppc_dcr_write (ppc_dcr_t
*dcr_env
, int dcrn
, target_ulong val
)
158 #define EXCP_DUMP(env, fmt, args...) \
160 fprintf(stderr, fmt , ##args); \
161 cpu_dump_state(env, stderr, fprintf, 0); \
162 if (loglevel != 0) { \
163 fprintf(logfile, fmt , ##args); \
164 cpu_dump_state(env, logfile, fprintf, 0); \
168 void cpu_loop(CPUPPCState
*env
)
172 target_siginfo_t info
;
175 trapnr
= cpu_ppc_exec(env
);
177 case POWERPC_EXCP_NONE
:
180 case POWERPC_EXCP_CRITICAL
: /* Critical input */
181 cpu_abort(env
, "Critical interrupt while in user mode. "
184 case POWERPC_EXCP_MCHECK
: /* Machine check exception */
185 cpu_abort(env
, "Machine check exception while in user mode. "
188 case POWERPC_EXCP_DSI
: /* Data storage exception */
190 /* To deal with multiple qemu header version as host for the darwin-user code */
193 EXCP_DUMP(env
, "Invalid data memory access: 0x" ADDRX
"\n",
195 /* Handle this via the gdb */
196 gdb_handlesig (env
, SIGSEGV
);
198 info
.si_addr
= (void*)env
->nip
;
199 queue_signal(info
.si_signo
, &info
);
201 case POWERPC_EXCP_ISI
: /* Instruction storage exception */
202 EXCP_DUMP(env
, "Invalid instruction fetch: 0x\n" ADDRX
"\n",
204 /* Handle this via the gdb */
205 gdb_handlesig (env
, SIGSEGV
);
207 info
.si_addr
= (void*)(env
->nip
- 4);
208 queue_signal(info
.si_signo
, &info
);
210 case POWERPC_EXCP_EXTERNAL
: /* External input */
211 cpu_abort(env
, "External interrupt while in user mode. "
214 case POWERPC_EXCP_ALIGN
: /* Alignment exception */
215 EXCP_DUMP(env
, "Unaligned memory access\n");
217 info
.si_code
= BUS_ADRALN
;
218 info
.si_addr
= (void*)(env
->nip
- 4);
219 queue_signal(info
.si_signo
, &info
);
221 case POWERPC_EXCP_PROGRAM
: /* Program exception */
222 /* XXX: check this */
223 switch (env
->error_code
& ~0xF) {
224 case POWERPC_EXCP_FP
:
225 EXCP_DUMP(env
, "Floating point program exception\n");
227 info
.si_signo
= SIGFPE
;
229 switch (env
->error_code
& 0xF) {
230 case POWERPC_EXCP_FP_OX
:
231 info
.si_code
= FPE_FLTOVF
;
233 case POWERPC_EXCP_FP_UX
:
234 info
.si_code
= FPE_FLTUND
;
236 case POWERPC_EXCP_FP_ZX
:
237 case POWERPC_EXCP_FP_VXZDZ
:
238 info
.si_code
= FPE_FLTDIV
;
240 case POWERPC_EXCP_FP_XX
:
241 info
.si_code
= FPE_FLTRES
;
243 case POWERPC_EXCP_FP_VXSOFT
:
244 info
.si_code
= FPE_FLTINV
;
246 case POWERPC_EXCP_FP_VXSNAN
:
247 case POWERPC_EXCP_FP_VXISI
:
248 case POWERPC_EXCP_FP_VXIDI
:
249 case POWERPC_EXCP_FP_VXIMZ
:
250 case POWERPC_EXCP_FP_VXVC
:
251 case POWERPC_EXCP_FP_VXSQRT
:
252 case POWERPC_EXCP_FP_VXCVI
:
253 info
.si_code
= FPE_FLTSUB
;
256 EXCP_DUMP(env
, "Unknown floating point exception (%02x)\n",
261 case POWERPC_EXCP_INVAL
:
262 EXCP_DUMP(env
, "Invalid instruction\n");
263 info
.si_signo
= SIGILL
;
265 switch (env
->error_code
& 0xF) {
266 case POWERPC_EXCP_INVAL_INVAL
:
267 info
.si_code
= ILL_ILLOPC
;
269 case POWERPC_EXCP_INVAL_LSWX
:
270 info
.si_code
= ILL_ILLOPN
;
272 case POWERPC_EXCP_INVAL_SPR
:
273 info
.si_code
= ILL_PRVREG
;
275 case POWERPC_EXCP_INVAL_FP
:
276 info
.si_code
= ILL_COPROC
;
279 EXCP_DUMP(env
, "Unknown invalid operation (%02x)\n",
280 env
->error_code
& 0xF);
281 info
.si_code
= ILL_ILLADR
;
284 /* Handle this via the gdb */
285 gdb_handlesig (env
, SIGSEGV
);
287 case POWERPC_EXCP_PRIV
:
288 EXCP_DUMP(env
, "Privilege violation\n");
289 info
.si_signo
= SIGILL
;
291 switch (env
->error_code
& 0xF) {
292 case POWERPC_EXCP_PRIV_OPC
:
293 info
.si_code
= ILL_PRVOPC
;
295 case POWERPC_EXCP_PRIV_REG
:
296 info
.si_code
= ILL_PRVREG
;
299 EXCP_DUMP(env
, "Unknown privilege violation (%02x)\n",
300 env
->error_code
& 0xF);
301 info
.si_code
= ILL_PRVOPC
;
305 case POWERPC_EXCP_TRAP
:
306 cpu_abort(env
, "Tried to call a TRAP\n");
309 /* Should not happen ! */
310 cpu_abort(env
, "Unknown program exception (%02x)\n",
314 info
.si_addr
= (void*)(env
->nip
- 4);
315 queue_signal(info
.si_signo
, &info
);
317 case POWERPC_EXCP_FPU
: /* Floating-point unavailable exception */
318 EXCP_DUMP(env
, "No floating point allowed\n");
319 info
.si_signo
= SIGILL
;
321 info
.si_code
= ILL_COPROC
;
322 info
.si_addr
= (void*)(env
->nip
- 4);
323 queue_signal(info
.si_signo
, &info
);
325 case POWERPC_EXCP_SYSCALL
: /* System call exception */
326 cpu_abort(env
, "Syscall exception while in user mode. "
329 case POWERPC_EXCP_APU
: /* Auxiliary processor unavailable */
330 EXCP_DUMP(env
, "No APU instruction allowed\n");
331 info
.si_signo
= SIGILL
;
333 info
.si_code
= ILL_COPROC
;
334 info
.si_addr
= (void*)(env
->nip
- 4);
335 queue_signal(info
.si_signo
, &info
);
337 case POWERPC_EXCP_DECR
: /* Decrementer exception */
338 cpu_abort(env
, "Decrementer interrupt while in user mode. "
341 case POWERPC_EXCP_FIT
: /* Fixed-interval timer interrupt */
342 cpu_abort(env
, "Fix interval timer interrupt while in user mode. "
345 case POWERPC_EXCP_WDT
: /* Watchdog timer interrupt */
346 cpu_abort(env
, "Watchdog timer interrupt while in user mode. "
349 case POWERPC_EXCP_DTLB
: /* Data TLB error */
350 cpu_abort(env
, "Data TLB exception while in user mode. "
353 case POWERPC_EXCP_ITLB
: /* Instruction TLB error */
354 cpu_abort(env
, "Instruction TLB exception while in user mode. "
357 case POWERPC_EXCP_DEBUG
: /* Debug interrupt */
358 gdb_handlesig (env
, SIGTRAP
);
360 #if defined(TARGET_PPCEMB)
361 case POWERPC_EXCP_SPEU
: /* SPE/embedded floating-point unavail. */
362 EXCP_DUMP(env
, "No SPE/floating-point instruction allowed\n");
363 info
.si_signo
= SIGILL
;
365 info
.si_code
= ILL_COPROC
;
366 info
.si_addr
= (void*)(env
->nip
- 4);
367 queue_signal(info
.si_signo
, &info
);
369 case POWERPC_EXCP_EFPDI
: /* Embedded floating-point data IRQ */
370 cpu_abort(env
, "Embedded floating-point data IRQ not handled\n");
372 case POWERPC_EXCP_EFPRI
: /* Embedded floating-point round IRQ */
373 cpu_abort(env
, "Embedded floating-point round IRQ not handled\n");
375 case POWERPC_EXCP_EPERFM
: /* Embedded performance monitor IRQ */
376 cpu_abort(env
, "Performance monitor exception not handled\n");
378 case POWERPC_EXCP_DOORI
: /* Embedded doorbell interrupt */
379 cpu_abort(env
, "Doorbell interrupt while in user mode. "
382 case POWERPC_EXCP_DOORCI
: /* Embedded doorbell critical interrupt */
383 cpu_abort(env
, "Doorbell critical interrupt while in user mode. "
386 #endif /* defined(TARGET_PPCEMB) */
387 case POWERPC_EXCP_RESET
: /* System reset exception */
388 cpu_abort(env
, "Reset interrupt while in user mode. "
391 #if defined(TARGET_PPC64) /* PowerPC 64 */
392 case POWERPC_EXCP_DSEG
: /* Data segment exception */
393 cpu_abort(env
, "Data segment exception while in user mode. "
396 case POWERPC_EXCP_ISEG
: /* Instruction segment exception */
397 cpu_abort(env
, "Instruction segment exception "
398 "while in user mode. Aborting\n");
400 #endif /* defined(TARGET_PPC64) */
401 #if defined(TARGET_PPC64H) /* PowerPC 64 with hypervisor mode support */
402 case POWERPC_EXCP_HDECR
: /* Hypervisor decrementer exception */
403 cpu_abort(env
, "Hypervisor decrementer interrupt "
404 "while in user mode. Aborting\n");
406 #endif /* defined(TARGET_PPC64H) */
407 case POWERPC_EXCP_TRACE
: /* Trace exception */
409 * we use this exception to emulate step-by-step execution mode.
412 #if defined(TARGET_PPC64H) /* PowerPC 64 with hypervisor mode support */
413 case POWERPC_EXCP_HDSI
: /* Hypervisor data storage exception */
414 cpu_abort(env
, "Hypervisor data storage exception "
415 "while in user mode. Aborting\n");
417 case POWERPC_EXCP_HISI
: /* Hypervisor instruction storage excp */
418 cpu_abort(env
, "Hypervisor instruction storage exception "
419 "while in user mode. Aborting\n");
421 case POWERPC_EXCP_HDSEG
: /* Hypervisor data segment exception */
422 cpu_abort(env
, "Hypervisor data segment exception "
423 "while in user mode. Aborting\n");
425 case POWERPC_EXCP_HISEG
: /* Hypervisor instruction segment excp */
426 cpu_abort(env
, "Hypervisor instruction segment exception "
427 "while in user mode. Aborting\n");
429 #endif /* defined(TARGET_PPC64H) */
430 case POWERPC_EXCP_VPU
: /* Vector unavailable exception */
431 EXCP_DUMP(env
, "No Altivec instructions allowed\n");
432 info
.si_signo
= SIGILL
;
434 info
.si_code
= ILL_COPROC
;
435 info
.si_addr
= (void*)(env
->nip
- 4);
436 queue_signal(info
.si_signo
, &info
);
438 case POWERPC_EXCP_PIT
: /* Programmable interval timer IRQ */
439 cpu_abort(env
, "Programable interval timer interrupt "
440 "while in user mode. Aborting\n");
442 case POWERPC_EXCP_IO
: /* IO error exception */
443 cpu_abort(env
, "IO error exception while in user mode. "
446 case POWERPC_EXCP_RUNM
: /* Run mode exception */
447 cpu_abort(env
, "Run mode exception while in user mode. "
450 case POWERPC_EXCP_EMUL
: /* Emulation trap exception */
451 cpu_abort(env
, "Emulation trap exception not handled\n");
453 case POWERPC_EXCP_IFTLB
: /* Instruction fetch TLB error */
454 cpu_abort(env
, "Instruction fetch TLB exception "
455 "while in user-mode. Aborting");
457 case POWERPC_EXCP_DLTLB
: /* Data load TLB miss */
458 cpu_abort(env
, "Data load TLB exception while in user-mode. "
461 case POWERPC_EXCP_DSTLB
: /* Data store TLB miss */
462 cpu_abort(env
, "Data store TLB exception while in user-mode. "
465 case POWERPC_EXCP_FPA
: /* Floating-point assist exception */
466 cpu_abort(env
, "Floating-point assist exception not handled\n");
468 case POWERPC_EXCP_IABR
: /* Instruction address breakpoint */
469 cpu_abort(env
, "Instruction address breakpoint exception "
472 case POWERPC_EXCP_SMI
: /* System management interrupt */
473 cpu_abort(env
, "System management interrupt while in user mode. "
476 case POWERPC_EXCP_THERM
: /* Thermal interrupt */
477 cpu_abort(env
, "Thermal interrupt interrupt while in user mode. "
480 case POWERPC_EXCP_PERFM
: /* Embedded performance monitor IRQ */
481 cpu_abort(env
, "Performance monitor exception not handled\n");
483 case POWERPC_EXCP_VPUA
: /* Vector assist exception */
484 cpu_abort(env
, "Vector assist exception not handled\n");
486 case POWERPC_EXCP_SOFTP
: /* Soft patch exception */
487 cpu_abort(env
, "Soft patch exception not handled\n");
489 case POWERPC_EXCP_MAINT
: /* Maintenance exception */
490 cpu_abort(env
, "Maintenance exception while in user mode. "
493 case POWERPC_EXCP_STOP
: /* stop translation */
494 /* We did invalidate the instruction cache. Go on */
496 case POWERPC_EXCP_BRANCH
: /* branch instruction: */
497 /* We just stopped because of a branch. Go on */
499 case POWERPC_EXCP_SYSCALL_USER
:
500 /* system call in user-mode emulation */
502 if(((int)env
->gpr
[0]) <= SYS_MAXSYSCALL
&& ((int)env
->gpr
[0])>0)
503 ret
= do_unix_syscall(env
, env
->gpr
[0]/*, env->gpr[3], env->gpr[4],
504 env->gpr[5], env->gpr[6], env->gpr[7],
505 env->gpr[8], env->gpr[9], env->gpr[10]*/);
506 else if(((int)env
->gpr
[0])<0)
507 ret
= do_mach_syscall(env
, env
->gpr
[0], env
->gpr
[3], env
->gpr
[4],
508 env
->gpr
[5], env
->gpr
[6], env
->gpr
[7],
509 env
->gpr
[8], env
->gpr
[9], env
->gpr
[10]);
511 ret
= do_thread_syscall(env
, env
->gpr
[0], env
->gpr
[3], env
->gpr
[4],
512 env
->gpr
[5], env
->gpr
[6], env
->gpr
[7],
513 env
->gpr
[8], env
->gpr
[9], env
->gpr
[10]);
515 /* Unix syscall error signaling */
516 if(((int)env
->gpr
[0]) <= SYS_MAXSYSCALL
&& ((int)env
->gpr
[0])>0)
528 /* just indicate that signals should be handled asap */
531 cpu_abort(env
, "Unknown exception 0x%d. Aborting\n", trapnr
);
534 process_pending_signals(env
);
542 /***********************************************************/
543 /* CPUX86 core interface */
545 uint64_t cpu_get_tsc(CPUX86State
*env
)
547 return cpu_get_real_ticks();
551 write_dt(void *ptr
, unsigned long addr
, unsigned long limit
,
555 e1
= (addr
<< 16) | (limit
& 0xffff);
556 e2
= ((addr
>> 16) & 0xff) | (addr
& 0xff000000) | (limit
& 0x000f0000);
558 stl((uint8_t *)ptr
, e1
);
559 stl((uint8_t *)ptr
+ 4, e2
);
562 static void set_gate(void *ptr
, unsigned int type
, unsigned int dpl
,
563 unsigned long addr
, unsigned int sel
)
566 e1
= (addr
& 0xffff) | (sel
<< 16);
567 e2
= (addr
& 0xffff0000) | 0x8000 | (dpl
<< 13) | (type
<< 8);
568 stl((uint8_t *)ptr
, e1
);
569 stl((uint8_t *)ptr
+ 4, e2
);
572 #define GDT_TABLE_SIZE 14
573 #define LDT_TABLE_SIZE 15
574 #define IDT_TABLE_SIZE 256
576 uint64_t gdt_table
[GDT_TABLE_SIZE
];
577 uint64_t ldt_table
[LDT_TABLE_SIZE
];
578 uint64_t idt_table
[IDT_TABLE_SIZE
];
579 uint32_t tss
[TSS_SIZE
];
581 /* only dpl matters as we do only user space emulation */
582 static void set_idt(int n
, unsigned int dpl
)
584 set_gate(idt_table
+ n
, 0, dpl
, 0, 0);
587 /* ABI convention: after a syscall if there was an error the CF flag is set */
588 static inline void set_error(CPUX86State
*env
, int ret
)
591 env
->eflags
= env
->eflags
| 0x1;
594 env
->regs
[R_EAX
] = ret
;
597 void cpu_loop(CPUX86State
*env
)
602 target_siginfo_t info
;
605 trapnr
= cpu_x86_exec(env
);
606 uint32_t *params
= (uint32_t *)env
->regs
[R_ESP
];
608 case 0x79: /* Our commpage hack back door exit is here */
609 do_commpage(env
, env
->eip
, *(params
+ 1), *(params
+ 2),
610 *(params
+ 3), *(params
+ 4),
611 *(params
+ 5), *(params
+ 6),
612 *(params
+ 7), *(params
+ 8));
614 case 0x81: /* mach syscall */
616 ret
= do_mach_syscall(env
, env
->regs
[R_EAX
],
617 *(params
+ 1), *(params
+ 2),
618 *(params
+ 3), *(params
+ 4),
619 *(params
+ 5), *(params
+ 6),
620 *(params
+ 7), *(params
+ 8));
624 case 0x90: /* unix backdoor */
626 /* after sysenter, stack is in R_ECX, new eip in R_EDX (sysexit will flip them back)*/
627 int saved_stack
= env
->regs
[R_ESP
];
628 env
->regs
[R_ESP
] = env
->regs
[R_ECX
];
630 ret
= do_unix_syscall(env
, env
->regs
[R_EAX
]);
632 env
->regs
[R_ECX
] = env
->regs
[R_ESP
];
633 env
->regs
[R_ESP
] = saved_stack
;
638 case 0x80: /* unix syscall */
640 ret
= do_unix_syscall(env
, env
->regs
[R_EAX
]/*,
641 *(params + 1), *(params + 2),
642 *(params + 3), *(params + 4),
643 *(params + 5), *(params + 6),
644 *(params + 7), *(params + 8)*/);
648 case 0x82: /* thread syscall */
650 ret
= do_thread_syscall(env
, env
->regs
[R_EAX
],
651 *(params
+ 1), *(params
+ 2),
652 *(params
+ 3), *(params
+ 4),
653 *(params
+ 5), *(params
+ 6),
654 *(params
+ 7), *(params
+ 8));
660 info
.si_signo
= SIGBUS
;
662 info
.si_code
= BUS_NOOP
;
664 gdb_handlesig (env
, SIGBUS
);
665 queue_signal(info
.si_signo
, &info
);
668 info
.si_signo
= SIGSEGV
;
670 info
.si_code
= SEGV_NOOP
;
672 gdb_handlesig (env
, SIGSEGV
);
673 queue_signal(info
.si_signo
, &info
);
676 info
.si_signo
= SIGSEGV
;
678 if (!(env
->error_code
& 1))
679 info
.si_code
= SEGV_MAPERR
;
681 info
.si_code
= SEGV_ACCERR
;
682 info
.si_addr
= (void*)env
->cr
[2];
683 gdb_handlesig (env
, SIGSEGV
);
684 queue_signal(info
.si_signo
, &info
);
687 /* division by zero */
688 info
.si_signo
= SIGFPE
;
690 info
.si_code
= FPE_INTDIV
;
691 info
.si_addr
= (void*)env
->eip
;
692 gdb_handlesig (env
, SIGFPE
);
693 queue_signal(info
.si_signo
, &info
);
697 info
.si_signo
= SIGTRAP
;
699 info
.si_code
= TRAP_BRKPT
;
700 info
.si_addr
= (void*)env
->eip
;
701 gdb_handlesig (env
, SIGTRAP
);
702 queue_signal(info
.si_signo
, &info
);
706 info
.si_signo
= SIGSEGV
;
708 info
.si_code
= SEGV_NOOP
;
710 gdb_handlesig (env
, SIGSEGV
);
711 queue_signal(info
.si_signo
, &info
);
714 info
.si_signo
= SIGILL
;
716 info
.si_code
= ILL_ILLOPN
;
717 info
.si_addr
= (void*)env
->eip
;
718 gdb_handlesig (env
, SIGILL
);
719 queue_signal(info
.si_signo
, &info
);
722 /* just indicate that signals should be handled asap */
728 sig
= gdb_handlesig (env
, SIGTRAP
);
733 info
.si_code
= TRAP_BRKPT
;
734 queue_signal(info
.si_signo
, &info
);
739 pc
= (void*)(env
->segs
[R_CS
].base
+ env
->eip
);
740 fprintf(stderr
, "qemu: 0x%08lx: unhandled CPU exception 0x%x - aborting\n",
744 process_pending_signals(env
);
751 printf("qemu-" TARGET_ARCH
" version " QEMU_VERSION
", Copyright (c) 2003-2004 Fabrice Bellard\n"
752 "usage: qemu-" TARGET_ARCH
" [-h] [-d opts] [-L path] [-s size] program [arguments...]\n"
753 "Darwin CPU emulator (compiled for %s emulation)\n"
755 "-h print this help\n"
756 "-L path set the %s library path (default='%s')\n"
757 "-s size set the stack size in bytes (default=%ld)\n"
761 "-no-code-copy disable code copy acceleration\n"
763 "-d options activate log (logfile='%s')\n"
764 "-g wait for gdb on port 1234\n"
765 "-p pagesize set the host page size to 'pagesize'\n",
774 /* XXX: currently only used for async signals (see signal.c) */
775 CPUState
*global_env
;
776 /* used only if single thread */
777 CPUState
*cpu_single_env
= NULL
;
779 /* used to free thread contexts */
780 TaskState
*first_task_state
;
782 int main(int argc
, char **argv
)
784 const char *filename
;
785 struct target_pt_regs regs1
, *regs
= ®s1
;
786 TaskState ts1
, *ts
= &ts1
;
789 short use_gdbstub
= 0;
796 cpu_set_log_filename(DEBUG_LOGFILE
);
807 if (!strcmp(r
, "-")) {
809 } else if (!strcmp(r
, "d")) {
817 mask
= cpu_str_to_log_mask(r
);
819 printf("Log items (comma separated):\n");
820 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
821 printf("%-10s %s\n", item
->name
, item
->help
);
826 } else if (!strcmp(r
, "s")) {
828 stack_size
= strtol(r
, (char **)&r
, 0);
832 stack_size
*= 1024 * 1024;
833 else if (*r
== 'k' || *r
== 'K')
835 } else if (!strcmp(r
, "L")) {
836 interp_prefix
= argv
[optind
++];
837 } else if (!strcmp(r
, "p")) {
838 qemu_host_page_size
= atoi(argv
[optind
++]);
839 if (qemu_host_page_size
== 0 ||
840 (qemu_host_page_size
& (qemu_host_page_size
- 1)) != 0) {
841 fprintf(stderr
, "page size must be a power of two\n");
845 if (!strcmp(r
, "g")) {
849 if (!strcmp(r
, "no-code-copy")) {
850 code_copy_enabled
= 0;
859 filename
= argv
[optind
];
862 memset(regs
, 0, sizeof(struct target_pt_regs
));
864 /* NOTE: we need to init the CPU at this stage to get
865 qemu_host_page_size */
868 printf("Starting %s with qemu\n----------------\n", filename
);
872 if (mach_exec(filename
, argv
+optind
, environ
, regs
) != 0) {
873 printf("Error loading %s\n", filename
);
881 /* build Task State */
882 memset(ts
, 0, sizeof(TaskState
));
885 env
->user_mode_only
= 1;
887 #if defined(TARGET_I386)
888 cpu_x86_set_cpl(env
, 3);
890 env
->cr
[0] = CR0_PG_MASK
| CR0_WP_MASK
| CR0_PE_MASK
;
891 env
->hflags
|= HF_PE_MASK
;
893 if (env
->cpuid_features
& CPUID_SSE
) {
894 env
->cr
[4] |= CR4_OSFXSR_MASK
;
895 env
->hflags
|= HF_OSFXSR_MASK
;
898 /* flags setup : we activate the IRQs by default as in user mode */
899 env
->eflags
|= IF_MASK
;
901 /* darwin register setup */
902 env
->regs
[R_EAX
] = regs
->eax
;
903 env
->regs
[R_EBX
] = regs
->ebx
;
904 env
->regs
[R_ECX
] = regs
->ecx
;
905 env
->regs
[R_EDX
] = regs
->edx
;
906 env
->regs
[R_ESI
] = regs
->esi
;
907 env
->regs
[R_EDI
] = regs
->edi
;
908 env
->regs
[R_EBP
] = regs
->ebp
;
909 env
->regs
[R_ESP
] = regs
->esp
;
910 env
->eip
= regs
->eip
;
912 /* Darwin LDT setup */
913 /* 2 - User code segment
914 3 - User data segment
916 bzero(ldt_table
, LDT_TABLE_SIZE
* sizeof(ldt_table
[0]));
917 env
->ldt
.base
= (uint32_t) ldt_table
;
918 env
->ldt
.limit
= sizeof(ldt_table
) - 1;
920 write_dt(ldt_table
+ 2, 0, 0xfffff,
921 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
| DESC_S_MASK
|
922 (3 << DESC_DPL_SHIFT
) | (0xa << DESC_TYPE_SHIFT
));
923 write_dt(ldt_table
+ 3, 0, 0xfffff,
924 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
| DESC_S_MASK
|
925 (3 << DESC_DPL_SHIFT
) | (0x2 << DESC_TYPE_SHIFT
));
926 write_dt(ldt_table
+ 4, 0, 0xfffff,
927 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
| DESC_S_MASK
|
928 (3 << DESC_DPL_SHIFT
) | (0x2 << DESC_TYPE_SHIFT
));
931 * has changed a lot between old Darwin/x86 (pre-Mac Intel) and Mac OS X/x86,
932 now everything is done via int 0x81(mach) int 0x82 (thread) and sysenter/sysexit(unix) */
933 bzero(gdt_table
, sizeof(gdt_table
));
934 env
->gdt
.base
= (uint32_t)gdt_table
;
935 env
->gdt
.limit
= sizeof(gdt_table
) - 1;
937 /* Set up a back door to handle sysenter syscalls (unix) */
938 char * syscallbackdoor
= malloc(64);
939 page_set_flags((int)syscallbackdoor
, (int)syscallbackdoor
+ 64, PROT_EXEC
| PROT_READ
| PAGE_VALID
);
942 syscallbackdoor
[i
++] = 0xcd;
943 syscallbackdoor
[i
++] = 0x90; /* int 0x90 */
944 syscallbackdoor
[i
++] = 0x0F;
945 syscallbackdoor
[i
++] = 0x35; /* sysexit */
947 /* Darwin sysenter/sysexit setup */
948 env
->sysenter_cs
= 0x1; //XXX
949 env
->sysenter_eip
= (int)syscallbackdoor
;
950 env
->sysenter_esp
= (int)malloc(64);
953 This must match up with GDT[4] */
954 env
->tr
.base
= (uint32_t) tss
;
955 env
->tr
.limit
= sizeof(tss
) - 1;
956 env
->tr
.flags
= DESC_P_MASK
| (0x9 << DESC_TYPE_SHIFT
);
957 stw(tss
+ 2, 0x10); // ss0 = 0x10 = GDT[2] = Kernel Data Segment
959 /* Darwin interrupt setup */
960 bzero(idt_table
, sizeof(idt_table
));
961 env
->idt
.base
= (uint32_t) idt_table
;
962 env
->idt
.limit
= sizeof(idt_table
) - 1;
983 /* Syscalls are done via
984 int 0x80 (unix) (rarely used)
987 int 0x83 (diag) (not handled here)
988 sysenter/sysexit (unix) -> we redirect that to int 0x90 */
989 set_idt(0x79, 3); /* Commpage hack, here is our backdoor interrupt */
990 set_idt(0x80, 3); /* Unix Syscall */
991 set_idt(0x81, 3); /* Mach Syscalls */
992 set_idt(0x82, 3); /* thread Syscalls */
994 set_idt(0x90, 3); /* qemu-darwin-user's Unix syscalls backdoor */
997 cpu_x86_load_seg(env
, R_CS
, __USER_CS
);
998 cpu_x86_load_seg(env
, R_DS
, __USER_DS
);
999 cpu_x86_load_seg(env
, R_ES
, __USER_DS
);
1000 cpu_x86_load_seg(env
, R_SS
, __USER_DS
);
1001 cpu_x86_load_seg(env
, R_FS
, __USER_DS
);
1002 cpu_x86_load_seg(env
, R_GS
, __USER_DS
);
1004 #elif defined(TARGET_PPC)
1007 env
->nip
= regs
->nip
;
1008 for(i
= 0; i
< 32; i
++) {
1009 env
->gpr
[i
] = regs
->gpr
[i
];
1013 #error unsupported target CPU
1017 printf("Waiting for gdb Connection on port 1234...\n");
1018 gdbserver_start (1234);
1019 gdb_handlesig(env
, 0);