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., 51 Franklin Street - Fifth Floor, Boston,
29 #include <sys/syscall.h>
34 #define DEBUG_LOGFILE "/tmp/qemu.log"
37 #include <crt_externs.h>
38 # define environ (*_NSGetEnviron())
41 #include <mach/mach_init.h>
42 #include <mach/vm_map.h>
46 const char *interp_prefix
= "";
48 asm(".zerofill __STD_PROG_ZONE, __STD_PROG_ZONE, __std_prog_zone, 0x0dfff000");
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 stack_size
= 512 * 1024;
55 void qerror(const char *fmt
, ...)
60 vfprintf(stderr
, fmt
, ap
);
62 fprintf(stderr
, "\n");
66 void gemu_log(const char *fmt
, ...)
71 vfprintf(stderr
, fmt
, ap
);
75 int cpu_get_pic_interrupt(CPUState
*env
)
81 static inline uint64_t cpu_ppc_get_tb (CPUState
*env
)
87 uint32_t cpu_ppc_load_tbl (CPUState
*env
)
89 return cpu_ppc_get_tb(env
) & 0xFFFFFFFF;
92 uint32_t cpu_ppc_load_tbu (CPUState
*env
)
94 return cpu_ppc_get_tb(env
) >> 32;
97 uint32_t cpu_ppc_load_atbl (CPUState
*env
)
99 return cpu_ppc_get_tb(env
) & 0xFFFFFFFF;
102 uint32_t cpu_ppc_load_atbu (CPUState
*env
)
104 return cpu_ppc_get_tb(env
) >> 32;
107 uint32_t cpu_ppc601_load_rtcu (CPUState
*env
)
109 cpu_ppc_load_tbu(env
);
112 uint32_t cpu_ppc601_load_rtcl (CPUState
*env
)
114 return cpu_ppc_load_tbl(env
) & 0x3FFFFF80;
117 /* XXX: to be fixed */
118 int ppc_dcr_read (ppc_dcr_t
*dcr_env
, int dcrn
, target_ulong
*valp
)
123 int ppc_dcr_write (ppc_dcr_t
*dcr_env
, int dcrn
, target_ulong val
)
128 #define EXCP_DUMP(env, fmt, ...) \
130 fprintf(stderr, fmt , ## __VA_ARGS__); \
131 cpu_dump_state(env, stderr, fprintf, 0); \
132 qemu_log(fmt, ## __VA_ARGS__); \
133 log_cpu_state(env, 0); \
136 void cpu_loop(CPUPPCState
*env
)
140 target_siginfo_t info
;
143 trapnr
= cpu_ppc_exec(env
);
145 case POWERPC_EXCP_NONE
:
148 case POWERPC_EXCP_CRITICAL
: /* Critical input */
149 cpu_abort(env
, "Critical interrupt while in user mode. "
152 case POWERPC_EXCP_MCHECK
: /* Machine check exception */
153 cpu_abort(env
, "Machine check exception while in user mode. "
156 case POWERPC_EXCP_DSI
: /* Data storage exception */
158 /* To deal with multiple qemu header version as host for the darwin-user code */
161 EXCP_DUMP(env
, "Invalid data memory access: 0x" ADDRX
"\n",
163 /* Handle this via the gdb */
164 gdb_handlesig (env
, SIGSEGV
);
166 info
.si_addr
= (void*)env
->nip
;
167 queue_signal(info
.si_signo
, &info
);
169 case POWERPC_EXCP_ISI
: /* Instruction storage exception */
170 EXCP_DUMP(env
, "Invalid instruction fetch: 0x\n" ADDRX
"\n",
172 /* Handle this via the gdb */
173 gdb_handlesig (env
, SIGSEGV
);
175 info
.si_addr
= (void*)(env
->nip
- 4);
176 queue_signal(info
.si_signo
, &info
);
178 case POWERPC_EXCP_EXTERNAL
: /* External input */
179 cpu_abort(env
, "External interrupt while in user mode. "
182 case POWERPC_EXCP_ALIGN
: /* Alignment exception */
183 EXCP_DUMP(env
, "Unaligned memory access\n");
185 info
.si_code
= BUS_ADRALN
;
186 info
.si_addr
= (void*)(env
->nip
- 4);
187 queue_signal(info
.si_signo
, &info
);
189 case POWERPC_EXCP_PROGRAM
: /* Program exception */
190 /* XXX: check this */
191 switch (env
->error_code
& ~0xF) {
192 case POWERPC_EXCP_FP
:
193 EXCP_DUMP(env
, "Floating point program exception\n");
195 info
.si_signo
= SIGFPE
;
197 switch (env
->error_code
& 0xF) {
198 case POWERPC_EXCP_FP_OX
:
199 info
.si_code
= FPE_FLTOVF
;
201 case POWERPC_EXCP_FP_UX
:
202 info
.si_code
= FPE_FLTUND
;
204 case POWERPC_EXCP_FP_ZX
:
205 case POWERPC_EXCP_FP_VXZDZ
:
206 info
.si_code
= FPE_FLTDIV
;
208 case POWERPC_EXCP_FP_XX
:
209 info
.si_code
= FPE_FLTRES
;
211 case POWERPC_EXCP_FP_VXSOFT
:
212 info
.si_code
= FPE_FLTINV
;
214 case POWERPC_EXCP_FP_VXSNAN
:
215 case POWERPC_EXCP_FP_VXISI
:
216 case POWERPC_EXCP_FP_VXIDI
:
217 case POWERPC_EXCP_FP_VXIMZ
:
218 case POWERPC_EXCP_FP_VXVC
:
219 case POWERPC_EXCP_FP_VXSQRT
:
220 case POWERPC_EXCP_FP_VXCVI
:
221 info
.si_code
= FPE_FLTSUB
;
224 EXCP_DUMP(env
, "Unknown floating point exception (%02x)\n",
229 case POWERPC_EXCP_INVAL
:
230 EXCP_DUMP(env
, "Invalid instruction\n");
231 info
.si_signo
= SIGILL
;
233 switch (env
->error_code
& 0xF) {
234 case POWERPC_EXCP_INVAL_INVAL
:
235 info
.si_code
= ILL_ILLOPC
;
237 case POWERPC_EXCP_INVAL_LSWX
:
238 info
.si_code
= ILL_ILLOPN
;
240 case POWERPC_EXCP_INVAL_SPR
:
241 info
.si_code
= ILL_PRVREG
;
243 case POWERPC_EXCP_INVAL_FP
:
244 info
.si_code
= ILL_COPROC
;
247 EXCP_DUMP(env
, "Unknown invalid operation (%02x)\n",
248 env
->error_code
& 0xF);
249 info
.si_code
= ILL_ILLADR
;
252 /* Handle this via the gdb */
253 gdb_handlesig (env
, SIGSEGV
);
255 case POWERPC_EXCP_PRIV
:
256 EXCP_DUMP(env
, "Privilege violation\n");
257 info
.si_signo
= SIGILL
;
259 switch (env
->error_code
& 0xF) {
260 case POWERPC_EXCP_PRIV_OPC
:
261 info
.si_code
= ILL_PRVOPC
;
263 case POWERPC_EXCP_PRIV_REG
:
264 info
.si_code
= ILL_PRVREG
;
267 EXCP_DUMP(env
, "Unknown privilege violation (%02x)\n",
268 env
->error_code
& 0xF);
269 info
.si_code
= ILL_PRVOPC
;
273 case POWERPC_EXCP_TRAP
:
274 cpu_abort(env
, "Tried to call a TRAP\n");
277 /* Should not happen ! */
278 cpu_abort(env
, "Unknown program exception (%02x)\n",
282 info
.si_addr
= (void*)(env
->nip
- 4);
283 queue_signal(info
.si_signo
, &info
);
285 case POWERPC_EXCP_FPU
: /* Floating-point unavailable exception */
286 EXCP_DUMP(env
, "No floating point allowed\n");
287 info
.si_signo
= SIGILL
;
289 info
.si_code
= ILL_COPROC
;
290 info
.si_addr
= (void*)(env
->nip
- 4);
291 queue_signal(info
.si_signo
, &info
);
293 case POWERPC_EXCP_SYSCALL
: /* System call exception */
294 cpu_abort(env
, "Syscall exception while in user mode. "
297 case POWERPC_EXCP_APU
: /* Auxiliary processor unavailable */
298 EXCP_DUMP(env
, "No APU instruction allowed\n");
299 info
.si_signo
= SIGILL
;
301 info
.si_code
= ILL_COPROC
;
302 info
.si_addr
= (void*)(env
->nip
- 4);
303 queue_signal(info
.si_signo
, &info
);
305 case POWERPC_EXCP_DECR
: /* Decrementer exception */
306 cpu_abort(env
, "Decrementer interrupt while in user mode. "
309 case POWERPC_EXCP_FIT
: /* Fixed-interval timer interrupt */
310 cpu_abort(env
, "Fix interval timer interrupt while in user mode. "
313 case POWERPC_EXCP_WDT
: /* Watchdog timer interrupt */
314 cpu_abort(env
, "Watchdog timer interrupt while in user mode. "
317 case POWERPC_EXCP_DTLB
: /* Data TLB error */
318 cpu_abort(env
, "Data TLB exception while in user mode. "
321 case POWERPC_EXCP_ITLB
: /* Instruction TLB error */
322 cpu_abort(env
, "Instruction TLB exception while in user mode. "
325 case POWERPC_EXCP_DEBUG
: /* Debug interrupt */
326 gdb_handlesig (env
, SIGTRAP
);
328 case POWERPC_EXCP_SPEU
: /* SPE/embedded floating-point unavail. */
329 EXCP_DUMP(env
, "No SPE/floating-point instruction allowed\n");
330 info
.si_signo
= SIGILL
;
332 info
.si_code
= ILL_COPROC
;
333 info
.si_addr
= (void*)(env
->nip
- 4);
334 queue_signal(info
.si_signo
, &info
);
336 case POWERPC_EXCP_EFPDI
: /* Embedded floating-point data IRQ */
337 cpu_abort(env
, "Embedded floating-point data IRQ not handled\n");
339 case POWERPC_EXCP_EFPRI
: /* Embedded floating-point round IRQ */
340 cpu_abort(env
, "Embedded floating-point round IRQ not handled\n");
342 case POWERPC_EXCP_EPERFM
: /* Embedded performance monitor IRQ */
343 cpu_abort(env
, "Performance monitor exception not handled\n");
345 case POWERPC_EXCP_DOORI
: /* Embedded doorbell interrupt */
346 cpu_abort(env
, "Doorbell interrupt while in user mode. "
349 case POWERPC_EXCP_DOORCI
: /* Embedded doorbell critical interrupt */
350 cpu_abort(env
, "Doorbell critical interrupt while in user mode. "
353 case POWERPC_EXCP_RESET
: /* System reset exception */
354 cpu_abort(env
, "Reset interrupt while in user mode. "
357 case POWERPC_EXCP_DSEG
: /* Data segment exception */
358 cpu_abort(env
, "Data segment exception while in user mode. "
361 case POWERPC_EXCP_ISEG
: /* Instruction segment exception */
362 cpu_abort(env
, "Instruction segment exception "
363 "while in user mode. Aborting\n");
365 case POWERPC_EXCP_HDECR
: /* Hypervisor decrementer exception */
366 cpu_abort(env
, "Hypervisor decrementer interrupt "
367 "while in user mode. Aborting\n");
369 case POWERPC_EXCP_TRACE
: /* Trace exception */
371 * we use this exception to emulate step-by-step execution mode.
374 case POWERPC_EXCP_HDSI
: /* Hypervisor data storage exception */
375 cpu_abort(env
, "Hypervisor data storage exception "
376 "while in user mode. Aborting\n");
378 case POWERPC_EXCP_HISI
: /* Hypervisor instruction storage excp */
379 cpu_abort(env
, "Hypervisor instruction storage exception "
380 "while in user mode. Aborting\n");
382 case POWERPC_EXCP_HDSEG
: /* Hypervisor data segment exception */
383 cpu_abort(env
, "Hypervisor data segment exception "
384 "while in user mode. Aborting\n");
386 case POWERPC_EXCP_HISEG
: /* Hypervisor instruction segment excp */
387 cpu_abort(env
, "Hypervisor instruction segment exception "
388 "while in user mode. Aborting\n");
390 case POWERPC_EXCP_VPU
: /* Vector unavailable exception */
391 EXCP_DUMP(env
, "No Altivec instructions allowed\n");
392 info
.si_signo
= SIGILL
;
394 info
.si_code
= ILL_COPROC
;
395 info
.si_addr
= (void*)(env
->nip
- 4);
396 queue_signal(info
.si_signo
, &info
);
398 case POWERPC_EXCP_PIT
: /* Programmable interval timer IRQ */
399 cpu_abort(env
, "Programable interval timer interrupt "
400 "while in user mode. Aborting\n");
402 case POWERPC_EXCP_IO
: /* IO error exception */
403 cpu_abort(env
, "IO error exception while in user mode. "
406 case POWERPC_EXCP_RUNM
: /* Run mode exception */
407 cpu_abort(env
, "Run mode exception while in user mode. "
410 case POWERPC_EXCP_EMUL
: /* Emulation trap exception */
411 cpu_abort(env
, "Emulation trap exception not handled\n");
413 case POWERPC_EXCP_IFTLB
: /* Instruction fetch TLB error */
414 cpu_abort(env
, "Instruction fetch TLB exception "
415 "while in user-mode. Aborting");
417 case POWERPC_EXCP_DLTLB
: /* Data load TLB miss */
418 cpu_abort(env
, "Data load TLB exception while in user-mode. "
421 case POWERPC_EXCP_DSTLB
: /* Data store TLB miss */
422 cpu_abort(env
, "Data store TLB exception while in user-mode. "
425 case POWERPC_EXCP_FPA
: /* Floating-point assist exception */
426 cpu_abort(env
, "Floating-point assist exception not handled\n");
428 case POWERPC_EXCP_IABR
: /* Instruction address breakpoint */
429 cpu_abort(env
, "Instruction address breakpoint exception "
432 case POWERPC_EXCP_SMI
: /* System management interrupt */
433 cpu_abort(env
, "System management interrupt while in user mode. "
436 case POWERPC_EXCP_THERM
: /* Thermal interrupt */
437 cpu_abort(env
, "Thermal interrupt interrupt while in user mode. "
440 case POWERPC_EXCP_PERFM
: /* Embedded performance monitor IRQ */
441 cpu_abort(env
, "Performance monitor exception not handled\n");
443 case POWERPC_EXCP_VPUA
: /* Vector assist exception */
444 cpu_abort(env
, "Vector assist exception not handled\n");
446 case POWERPC_EXCP_SOFTP
: /* Soft patch exception */
447 cpu_abort(env
, "Soft patch exception not handled\n");
449 case POWERPC_EXCP_MAINT
: /* Maintenance exception */
450 cpu_abort(env
, "Maintenance exception while in user mode. "
453 case POWERPC_EXCP_STOP
: /* stop translation */
454 /* We did invalidate the instruction cache. Go on */
456 case POWERPC_EXCP_BRANCH
: /* branch instruction: */
457 /* We just stopped because of a branch. Go on */
459 case POWERPC_EXCP_SYSCALL_USER
:
460 /* system call in user-mode emulation */
462 if(((int)env
->gpr
[0]) <= SYS_MAXSYSCALL
&& ((int)env
->gpr
[0])>0)
463 ret
= do_unix_syscall(env
, env
->gpr
[0]/*, env->gpr[3], env->gpr[4],
464 env->gpr[5], env->gpr[6], env->gpr[7],
465 env->gpr[8], env->gpr[9], env->gpr[10]*/);
466 else if(((int)env
->gpr
[0])<0)
467 ret
= do_mach_syscall(env
, env
->gpr
[0], env
->gpr
[3], env
->gpr
[4],
468 env
->gpr
[5], env
->gpr
[6], env
->gpr
[7],
469 env
->gpr
[8], env
->gpr
[9], env
->gpr
[10]);
471 ret
= do_thread_syscall(env
, env
->gpr
[0], env
->gpr
[3], env
->gpr
[4],
472 env
->gpr
[5], env
->gpr
[6], env
->gpr
[7],
473 env
->gpr
[8], env
->gpr
[9], env
->gpr
[10]);
475 /* Unix syscall error signaling */
476 if(((int)env
->gpr
[0]) <= SYS_MAXSYSCALL
&& ((int)env
->gpr
[0])>0)
488 /* just indicate that signals should be handled asap */
491 cpu_abort(env
, "Unknown exception 0x%d. Aborting\n", trapnr
);
494 process_pending_signals(env
);
502 /***********************************************************/
503 /* CPUX86 core interface */
505 uint64_t cpu_get_tsc(CPUX86State
*env
)
507 return cpu_get_real_ticks();
511 write_dt(void *ptr
, unsigned long addr
, unsigned long limit
,
515 e1
= (addr
<< 16) | (limit
& 0xffff);
516 e2
= ((addr
>> 16) & 0xff) | (addr
& 0xff000000) | (limit
& 0x000f0000);
518 stl((uint8_t *)ptr
, e1
);
519 stl((uint8_t *)ptr
+ 4, e2
);
522 static void set_gate(void *ptr
, unsigned int type
, unsigned int dpl
,
523 unsigned long addr
, unsigned int sel
)
526 e1
= (addr
& 0xffff) | (sel
<< 16);
527 e2
= (addr
& 0xffff0000) | 0x8000 | (dpl
<< 13) | (type
<< 8);
528 stl((uint8_t *)ptr
, e1
);
529 stl((uint8_t *)ptr
+ 4, e2
);
532 #define GDT_TABLE_SIZE 14
533 #define LDT_TABLE_SIZE 15
534 #define IDT_TABLE_SIZE 256
536 uint64_t gdt_table
[GDT_TABLE_SIZE
];
537 uint64_t ldt_table
[LDT_TABLE_SIZE
];
538 uint64_t idt_table
[IDT_TABLE_SIZE
];
539 uint32_t tss
[TSS_SIZE
];
541 /* only dpl matters as we do only user space emulation */
542 static void set_idt(int n
, unsigned int dpl
)
544 set_gate(idt_table
+ n
, 0, dpl
, 0, 0);
547 /* ABI convention: after a syscall if there was an error the CF flag is set */
548 static inline void set_error(CPUX86State
*env
, int ret
)
551 env
->eflags
= env
->eflags
| 0x1;
554 env
->regs
[R_EAX
] = ret
;
557 void cpu_loop(CPUX86State
*env
)
562 target_siginfo_t info
;
565 trapnr
= cpu_x86_exec(env
);
566 uint32_t *params
= (uint32_t *)env
->regs
[R_ESP
];
568 case 0x79: /* Our commpage hack back door exit is here */
569 do_commpage(env
, env
->eip
, *(params
+ 1), *(params
+ 2),
570 *(params
+ 3), *(params
+ 4),
571 *(params
+ 5), *(params
+ 6),
572 *(params
+ 7), *(params
+ 8));
574 case 0x81: /* mach syscall */
576 ret
= do_mach_syscall(env
, env
->regs
[R_EAX
],
577 *(params
+ 1), *(params
+ 2),
578 *(params
+ 3), *(params
+ 4),
579 *(params
+ 5), *(params
+ 6),
580 *(params
+ 7), *(params
+ 8));
584 case 0x90: /* unix backdoor */
586 /* after sysenter, stack is in R_ECX, new eip in R_EDX (sysexit will flip them back)*/
587 int saved_stack
= env
->regs
[R_ESP
];
588 env
->regs
[R_ESP
] = env
->regs
[R_ECX
];
590 ret
= do_unix_syscall(env
, env
->regs
[R_EAX
]);
592 env
->regs
[R_ECX
] = env
->regs
[R_ESP
];
593 env
->regs
[R_ESP
] = saved_stack
;
598 case 0x80: /* unix syscall */
600 ret
= do_unix_syscall(env
, env
->regs
[R_EAX
]/*,
601 *(params + 1), *(params + 2),
602 *(params + 3), *(params + 4),
603 *(params + 5), *(params + 6),
604 *(params + 7), *(params + 8)*/);
608 case 0x82: /* thread syscall */
610 ret
= do_thread_syscall(env
, env
->regs
[R_EAX
],
611 *(params
+ 1), *(params
+ 2),
612 *(params
+ 3), *(params
+ 4),
613 *(params
+ 5), *(params
+ 6),
614 *(params
+ 7), *(params
+ 8));
620 info
.si_signo
= SIGBUS
;
622 info
.si_code
= BUS_NOOP
;
624 gdb_handlesig (env
, SIGBUS
);
625 queue_signal(info
.si_signo
, &info
);
628 info
.si_signo
= SIGSEGV
;
630 info
.si_code
= SEGV_NOOP
;
632 gdb_handlesig (env
, SIGSEGV
);
633 queue_signal(info
.si_signo
, &info
);
636 info
.si_signo
= SIGSEGV
;
638 if (!(env
->error_code
& 1))
639 info
.si_code
= SEGV_MAPERR
;
641 info
.si_code
= SEGV_ACCERR
;
642 info
.si_addr
= (void*)env
->cr
[2];
643 gdb_handlesig (env
, SIGSEGV
);
644 queue_signal(info
.si_signo
, &info
);
647 /* division by zero */
648 info
.si_signo
= SIGFPE
;
650 info
.si_code
= FPE_INTDIV
;
651 info
.si_addr
= (void*)env
->eip
;
652 gdb_handlesig (env
, SIGFPE
);
653 queue_signal(info
.si_signo
, &info
);
657 info
.si_signo
= SIGTRAP
;
659 info
.si_code
= TRAP_BRKPT
;
660 info
.si_addr
= (void*)env
->eip
;
661 gdb_handlesig (env
, SIGTRAP
);
662 queue_signal(info
.si_signo
, &info
);
666 info
.si_signo
= SIGSEGV
;
668 info
.si_code
= SEGV_NOOP
;
670 gdb_handlesig (env
, SIGSEGV
);
671 queue_signal(info
.si_signo
, &info
);
674 info
.si_signo
= SIGILL
;
676 info
.si_code
= ILL_ILLOPN
;
677 info
.si_addr
= (void*)env
->eip
;
678 gdb_handlesig (env
, SIGILL
);
679 queue_signal(info
.si_signo
, &info
);
682 /* just indicate that signals should be handled asap */
688 sig
= gdb_handlesig (env
, SIGTRAP
);
693 info
.si_code
= TRAP_BRKPT
;
694 queue_signal(info
.si_signo
, &info
);
699 pc
= (void*)(env
->segs
[R_CS
].base
+ env
->eip
);
700 fprintf(stderr
, "qemu: 0x%08lx: unhandled CPU exception 0x%x - aborting\n",
704 process_pending_signals(env
);
711 printf("qemu-" TARGET_ARCH
" version " QEMU_VERSION
", Copyright (c) 2003-2004 Fabrice Bellard\n"
712 "usage: qemu-" TARGET_ARCH
" [-h] [-d opts] [-L path] [-s size] program [arguments...]\n"
713 "Darwin CPU emulator (compiled for %s emulation)\n"
715 "-h print this help\n"
716 "-L path set the %s library path (default='%s')\n"
717 "-s size set the stack size in bytes (default=%ld)\n"
720 "-d options activate log (logfile='%s')\n"
721 "-g wait for gdb on port 1234\n"
722 "-p pagesize set the host page size to 'pagesize'\n",
723 "-singlestep always run in singlestep mode\n"
732 /* XXX: currently only used for async signals (see signal.c) */
733 CPUState
*global_env
;
734 /* used only if single thread */
735 CPUState
*cpu_single_env
= NULL
;
737 /* used to free thread contexts */
738 TaskState
*first_task_state
;
740 int main(int argc
, char **argv
)
742 const char *filename
;
743 struct target_pt_regs regs1
, *regs
= ®s1
;
744 TaskState ts1
, *ts
= &ts1
;
747 short use_gdbstub
= 0;
749 const char *cpu_model
;
755 cpu_set_log_filename(DEBUG_LOGFILE
);
766 if (!strcmp(r
, "-")) {
768 } else if (!strcmp(r
, "d")) {
776 mask
= cpu_str_to_log_mask(r
);
778 printf("Log items (comma separated):\n");
779 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
780 printf("%-10s %s\n", item
->name
, item
->help
);
785 } else if (!strcmp(r
, "s")) {
787 stack_size
= strtol(r
, (char **)&r
, 0);
791 stack_size
*= 1024 * 1024;
792 else if (*r
== 'k' || *r
== 'K')
794 } else if (!strcmp(r
, "L")) {
795 interp_prefix
= argv
[optind
++];
796 } else if (!strcmp(r
, "p")) {
797 qemu_host_page_size
= atoi(argv
[optind
++]);
798 if (qemu_host_page_size
== 0 ||
799 (qemu_host_page_size
& (qemu_host_page_size
- 1)) != 0) {
800 fprintf(stderr
, "page size must be a power of two\n");
804 if (!strcmp(r
, "g")) {
806 } else if (!strcmp(r
, "cpu")) {
807 cpu_model
= argv
[optind
++];
808 if (strcmp(cpu_model
, "?") == 0) {
809 /* XXX: implement xxx_cpu_list for targets that still miss it */
810 #if defined(cpu_list)
811 cpu_list(stdout
, &fprintf
);
815 } else if (!strcmp(r
, "singlestep")) {
824 filename
= argv
[optind
];
827 memset(regs
, 0, sizeof(struct target_pt_regs
));
829 if (cpu_model
== NULL
) {
830 #if defined(TARGET_I386)
832 cpu_model
= "qemu64";
834 cpu_model
= "qemu32";
836 #elif defined(TARGET_PPC)
843 #error unsupported CPU
847 cpu_exec_init_all(0);
848 /* NOTE: we need to init the CPU at this stage to get
849 qemu_host_page_size */
850 env
= cpu_init(cpu_model
);
852 printf("Starting %s with qemu\n----------------\n", filename
);
856 if (mach_exec(filename
, argv
+optind
, environ
, regs
) != 0) {
857 printf("Error loading %s\n", filename
);
865 /* build Task State */
866 memset(ts
, 0, sizeof(TaskState
));
870 #if defined(TARGET_I386)
871 cpu_x86_set_cpl(env
, 3);
873 env
->cr
[0] = CR0_PG_MASK
| CR0_WP_MASK
| CR0_PE_MASK
;
874 env
->hflags
|= HF_PE_MASK
;
876 if (env
->cpuid_features
& CPUID_SSE
) {
877 env
->cr
[4] |= CR4_OSFXSR_MASK
;
878 env
->hflags
|= HF_OSFXSR_MASK
;
881 /* flags setup : we activate the IRQs by default as in user mode */
882 env
->eflags
|= IF_MASK
;
884 /* darwin register setup */
885 env
->regs
[R_EAX
] = regs
->eax
;
886 env
->regs
[R_EBX
] = regs
->ebx
;
887 env
->regs
[R_ECX
] = regs
->ecx
;
888 env
->regs
[R_EDX
] = regs
->edx
;
889 env
->regs
[R_ESI
] = regs
->esi
;
890 env
->regs
[R_EDI
] = regs
->edi
;
891 env
->regs
[R_EBP
] = regs
->ebp
;
892 env
->regs
[R_ESP
] = regs
->esp
;
893 env
->eip
= regs
->eip
;
895 /* Darwin LDT setup */
896 /* 2 - User code segment
897 3 - User data segment
899 bzero(ldt_table
, LDT_TABLE_SIZE
* sizeof(ldt_table
[0]));
900 env
->ldt
.base
= (uint32_t) ldt_table
;
901 env
->ldt
.limit
= sizeof(ldt_table
) - 1;
903 write_dt(ldt_table
+ 2, 0, 0xfffff,
904 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
| DESC_S_MASK
|
905 (3 << DESC_DPL_SHIFT
) | (0xa << DESC_TYPE_SHIFT
));
906 write_dt(ldt_table
+ 3, 0, 0xfffff,
907 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
| DESC_S_MASK
|
908 (3 << DESC_DPL_SHIFT
) | (0x2 << DESC_TYPE_SHIFT
));
909 write_dt(ldt_table
+ 4, 0, 0xfffff,
910 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
| DESC_S_MASK
|
911 (3 << DESC_DPL_SHIFT
) | (0x2 << DESC_TYPE_SHIFT
));
914 * has changed a lot between old Darwin/x86 (pre-Mac Intel) and Mac OS X/x86,
915 now everything is done via int 0x81(mach) int 0x82 (thread) and sysenter/sysexit(unix) */
916 bzero(gdt_table
, sizeof(gdt_table
));
917 env
->gdt
.base
= (uint32_t)gdt_table
;
918 env
->gdt
.limit
= sizeof(gdt_table
) - 1;
920 /* Set up a back door to handle sysenter syscalls (unix) */
921 char * syscallbackdoor
= malloc(64);
922 page_set_flags((int)syscallbackdoor
, (int)syscallbackdoor
+ 64, PROT_EXEC
| PROT_READ
| PAGE_VALID
);
925 syscallbackdoor
[i
++] = 0xcd;
926 syscallbackdoor
[i
++] = 0x90; /* int 0x90 */
927 syscallbackdoor
[i
++] = 0x0F;
928 syscallbackdoor
[i
++] = 0x35; /* sysexit */
930 /* Darwin sysenter/sysexit setup */
931 env
->sysenter_cs
= 0x1; //XXX
932 env
->sysenter_eip
= (int)syscallbackdoor
;
933 env
->sysenter_esp
= (int)malloc(64);
936 This must match up with GDT[4] */
937 env
->tr
.base
= (uint32_t) tss
;
938 env
->tr
.limit
= sizeof(tss
) - 1;
939 env
->tr
.flags
= DESC_P_MASK
| (0x9 << DESC_TYPE_SHIFT
);
940 stw(tss
+ 2, 0x10); // ss0 = 0x10 = GDT[2] = Kernel Data Segment
942 /* Darwin interrupt setup */
943 bzero(idt_table
, sizeof(idt_table
));
944 env
->idt
.base
= (uint32_t) idt_table
;
945 env
->idt
.limit
= sizeof(idt_table
) - 1;
966 /* Syscalls are done via
967 int 0x80 (unix) (rarely used)
970 int 0x83 (diag) (not handled here)
971 sysenter/sysexit (unix) -> we redirect that to int 0x90 */
972 set_idt(0x79, 3); /* Commpage hack, here is our backdoor interrupt */
973 set_idt(0x80, 3); /* Unix Syscall */
974 set_idt(0x81, 3); /* Mach Syscalls */
975 set_idt(0x82, 3); /* thread Syscalls */
977 set_idt(0x90, 3); /* qemu-darwin-user's Unix syscalls backdoor */
980 cpu_x86_load_seg(env
, R_CS
, __USER_CS
);
981 cpu_x86_load_seg(env
, R_DS
, __USER_DS
);
982 cpu_x86_load_seg(env
, R_ES
, __USER_DS
);
983 cpu_x86_load_seg(env
, R_SS
, __USER_DS
);
984 cpu_x86_load_seg(env
, R_FS
, __USER_DS
);
985 cpu_x86_load_seg(env
, R_GS
, __USER_DS
);
987 #elif defined(TARGET_PPC)
991 #if defined(TARGET_PPC64)
992 #if defined(TARGET_ABI32)
993 env
->msr
&= ~((target_ulong
)1 << MSR_SF
);
995 env
->msr
|= (target_ulong
)1 << MSR_SF
;
998 env
->nip
= regs
->nip
;
999 for(i
= 0; i
< 32; i
++) {
1000 env
->gpr
[i
] = regs
->gpr
[i
];
1004 #error unsupported target CPU
1008 printf("Waiting for gdb Connection on port 1234...\n");
1009 gdbserver_start (1234);
1010 gdb_handlesig(env
, 0);