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, see <http://www.gnu.org/licenses/>.
27 #include <sys/syscall.h>
32 #define DEBUG_LOGFILE "/tmp/qemu.log"
35 #include <crt_externs.h>
36 # define environ (*_NSGetEnviron())
39 #include <mach/mach_init.h>
40 #include <mach/vm_map.h>
44 const char *interp_prefix
= "";
46 asm(".zerofill __STD_PROG_ZONE, __STD_PROG_ZONE, __std_prog_zone, 0x0dfff000");
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 stack_size
= 512 * 1024;
53 void qerror(const char *fmt
, ...)
58 vfprintf(stderr
, fmt
, ap
);
60 fprintf(stderr
, "\n");
64 void gemu_log(const char *fmt
, ...)
69 vfprintf(stderr
, fmt
, ap
);
73 int cpu_get_pic_interrupt(CPUState
*env
)
79 static inline uint64_t cpu_ppc_get_tb (CPUState
*env
)
85 uint64_t cpu_ppc_load_tbl (CPUState
*env
)
87 return cpu_ppc_get_tb(env
);
90 uint32_t cpu_ppc_load_tbu (CPUState
*env
)
92 return cpu_ppc_get_tb(env
) >> 32;
95 uint64_t cpu_ppc_load_atbl (CPUState
*env
)
97 return cpu_ppc_get_tb(env
);
100 uint32_t cpu_ppc_load_atbu (CPUState
*env
)
102 return cpu_ppc_get_tb(env
) >> 32;
105 uint32_t cpu_ppc601_load_rtcu (CPUState
*env
)
107 cpu_ppc_load_tbu(env
);
110 uint32_t cpu_ppc601_load_rtcl (CPUState
*env
)
112 return cpu_ppc_load_tbl(env
) & 0x3FFFFF80;
115 /* XXX: to be fixed */
116 int ppc_dcr_read (ppc_dcr_t
*dcr_env
, int dcrn
, uint32_t *valp
)
121 int ppc_dcr_write (ppc_dcr_t
*dcr_env
, int dcrn
, uint32_t val
)
126 #define EXCP_DUMP(env, fmt, ...) \
128 fprintf(stderr, fmt , ## __VA_ARGS__); \
129 cpu_dump_state(env, stderr, fprintf, 0); \
130 qemu_log(fmt, ## __VA_ARGS__); \
131 log_cpu_state(env, 0); \
134 void cpu_loop(CPUPPCState
*env
)
138 target_siginfo_t info
;
141 trapnr
= cpu_ppc_exec(env
);
143 case POWERPC_EXCP_NONE
:
146 case POWERPC_EXCP_CRITICAL
: /* Critical input */
147 cpu_abort(env
, "Critical interrupt while in user mode. "
150 case POWERPC_EXCP_MCHECK
: /* Machine check exception */
151 cpu_abort(env
, "Machine check exception while in user mode. "
154 case POWERPC_EXCP_DSI
: /* Data storage exception */
156 /* To deal with multiple qemu header version as host for the darwin-user code */
159 EXCP_DUMP(env
, "Invalid data memory access: 0x" TARGET_FMT_lx
"\n",
161 /* Handle this via the gdb */
162 gdb_handlesig (env
, SIGSEGV
);
164 info
.si_addr
= (void*)env
->nip
;
165 queue_signal(info
.si_signo
, &info
);
167 case POWERPC_EXCP_ISI
: /* Instruction storage exception */
168 EXCP_DUMP(env
, "Invalid instruction fetch: 0x\n" TARGET_FMT_lx
"\n",
170 /* Handle this via the gdb */
171 gdb_handlesig (env
, SIGSEGV
);
173 info
.si_addr
= (void*)(env
->nip
- 4);
174 queue_signal(info
.si_signo
, &info
);
176 case POWERPC_EXCP_EXTERNAL
: /* External input */
177 cpu_abort(env
, "External interrupt while in user mode. "
180 case POWERPC_EXCP_ALIGN
: /* Alignment exception */
181 EXCP_DUMP(env
, "Unaligned memory access\n");
183 info
.si_code
= BUS_ADRALN
;
184 info
.si_addr
= (void*)(env
->nip
- 4);
185 queue_signal(info
.si_signo
, &info
);
187 case POWERPC_EXCP_PROGRAM
: /* Program exception */
188 /* XXX: check this */
189 switch (env
->error_code
& ~0xF) {
190 case POWERPC_EXCP_FP
:
191 EXCP_DUMP(env
, "Floating point program exception\n");
193 info
.si_signo
= SIGFPE
;
195 switch (env
->error_code
& 0xF) {
196 case POWERPC_EXCP_FP_OX
:
197 info
.si_code
= FPE_FLTOVF
;
199 case POWERPC_EXCP_FP_UX
:
200 info
.si_code
= FPE_FLTUND
;
202 case POWERPC_EXCP_FP_ZX
:
203 case POWERPC_EXCP_FP_VXZDZ
:
204 info
.si_code
= FPE_FLTDIV
;
206 case POWERPC_EXCP_FP_XX
:
207 info
.si_code
= FPE_FLTRES
;
209 case POWERPC_EXCP_FP_VXSOFT
:
210 info
.si_code
= FPE_FLTINV
;
212 case POWERPC_EXCP_FP_VXSNAN
:
213 case POWERPC_EXCP_FP_VXISI
:
214 case POWERPC_EXCP_FP_VXIDI
:
215 case POWERPC_EXCP_FP_VXIMZ
:
216 case POWERPC_EXCP_FP_VXVC
:
217 case POWERPC_EXCP_FP_VXSQRT
:
218 case POWERPC_EXCP_FP_VXCVI
:
219 info
.si_code
= FPE_FLTSUB
;
222 EXCP_DUMP(env
, "Unknown floating point exception (%02x)\n",
227 case POWERPC_EXCP_INVAL
:
228 EXCP_DUMP(env
, "Invalid instruction\n");
229 info
.si_signo
= SIGILL
;
231 switch (env
->error_code
& 0xF) {
232 case POWERPC_EXCP_INVAL_INVAL
:
233 info
.si_code
= ILL_ILLOPC
;
235 case POWERPC_EXCP_INVAL_LSWX
:
236 info
.si_code
= ILL_ILLOPN
;
238 case POWERPC_EXCP_INVAL_SPR
:
239 info
.si_code
= ILL_PRVREG
;
241 case POWERPC_EXCP_INVAL_FP
:
242 info
.si_code
= ILL_COPROC
;
245 EXCP_DUMP(env
, "Unknown invalid operation (%02x)\n",
246 env
->error_code
& 0xF);
247 info
.si_code
= ILL_ILLADR
;
250 /* Handle this via the gdb */
251 gdb_handlesig (env
, SIGSEGV
);
253 case POWERPC_EXCP_PRIV
:
254 EXCP_DUMP(env
, "Privilege violation\n");
255 info
.si_signo
= SIGILL
;
257 switch (env
->error_code
& 0xF) {
258 case POWERPC_EXCP_PRIV_OPC
:
259 info
.si_code
= ILL_PRVOPC
;
261 case POWERPC_EXCP_PRIV_REG
:
262 info
.si_code
= ILL_PRVREG
;
265 EXCP_DUMP(env
, "Unknown privilege violation (%02x)\n",
266 env
->error_code
& 0xF);
267 info
.si_code
= ILL_PRVOPC
;
271 case POWERPC_EXCP_TRAP
:
272 cpu_abort(env
, "Tried to call a TRAP\n");
275 /* Should not happen ! */
276 cpu_abort(env
, "Unknown program exception (%02x)\n",
280 info
.si_addr
= (void*)(env
->nip
- 4);
281 queue_signal(info
.si_signo
, &info
);
283 case POWERPC_EXCP_FPU
: /* Floating-point unavailable exception */
284 EXCP_DUMP(env
, "No floating point allowed\n");
285 info
.si_signo
= SIGILL
;
287 info
.si_code
= ILL_COPROC
;
288 info
.si_addr
= (void*)(env
->nip
- 4);
289 queue_signal(info
.si_signo
, &info
);
291 case POWERPC_EXCP_SYSCALL
: /* System call exception */
292 cpu_abort(env
, "Syscall exception while in user mode. "
295 case POWERPC_EXCP_APU
: /* Auxiliary processor unavailable */
296 EXCP_DUMP(env
, "No APU instruction allowed\n");
297 info
.si_signo
= SIGILL
;
299 info
.si_code
= ILL_COPROC
;
300 info
.si_addr
= (void*)(env
->nip
- 4);
301 queue_signal(info
.si_signo
, &info
);
303 case POWERPC_EXCP_DECR
: /* Decrementer exception */
304 cpu_abort(env
, "Decrementer interrupt while in user mode. "
307 case POWERPC_EXCP_FIT
: /* Fixed-interval timer interrupt */
308 cpu_abort(env
, "Fix interval timer interrupt while in user mode. "
311 case POWERPC_EXCP_WDT
: /* Watchdog timer interrupt */
312 cpu_abort(env
, "Watchdog timer interrupt while in user mode. "
315 case POWERPC_EXCP_DTLB
: /* Data TLB error */
316 cpu_abort(env
, "Data TLB exception while in user mode. "
319 case POWERPC_EXCP_ITLB
: /* Instruction TLB error */
320 cpu_abort(env
, "Instruction TLB exception while in user mode. "
323 case POWERPC_EXCP_DEBUG
: /* Debug interrupt */
324 gdb_handlesig (env
, SIGTRAP
);
326 case POWERPC_EXCP_SPEU
: /* SPE/embedded floating-point unavail. */
327 EXCP_DUMP(env
, "No SPE/floating-point instruction allowed\n");
328 info
.si_signo
= SIGILL
;
330 info
.si_code
= ILL_COPROC
;
331 info
.si_addr
= (void*)(env
->nip
- 4);
332 queue_signal(info
.si_signo
, &info
);
334 case POWERPC_EXCP_EFPDI
: /* Embedded floating-point data IRQ */
335 cpu_abort(env
, "Embedded floating-point data IRQ not handled\n");
337 case POWERPC_EXCP_EFPRI
: /* Embedded floating-point round IRQ */
338 cpu_abort(env
, "Embedded floating-point round IRQ not handled\n");
340 case POWERPC_EXCP_EPERFM
: /* Embedded performance monitor IRQ */
341 cpu_abort(env
, "Performance monitor exception not handled\n");
343 case POWERPC_EXCP_DOORI
: /* Embedded doorbell interrupt */
344 cpu_abort(env
, "Doorbell interrupt while in user mode. "
347 case POWERPC_EXCP_DOORCI
: /* Embedded doorbell critical interrupt */
348 cpu_abort(env
, "Doorbell critical interrupt while in user mode. "
351 case POWERPC_EXCP_RESET
: /* System reset exception */
352 cpu_abort(env
, "Reset interrupt while in user mode. "
355 case POWERPC_EXCP_DSEG
: /* Data segment exception */
356 cpu_abort(env
, "Data segment exception while in user mode. "
359 case POWERPC_EXCP_ISEG
: /* Instruction segment exception */
360 cpu_abort(env
, "Instruction segment exception "
361 "while in user mode. Aborting\n");
363 case POWERPC_EXCP_HDECR
: /* Hypervisor decrementer exception */
364 cpu_abort(env
, "Hypervisor decrementer interrupt "
365 "while in user mode. Aborting\n");
367 case POWERPC_EXCP_TRACE
: /* Trace exception */
369 * we use this exception to emulate step-by-step execution mode.
372 case POWERPC_EXCP_HDSI
: /* Hypervisor data storage exception */
373 cpu_abort(env
, "Hypervisor data storage exception "
374 "while in user mode. Aborting\n");
376 case POWERPC_EXCP_HISI
: /* Hypervisor instruction storage excp */
377 cpu_abort(env
, "Hypervisor instruction storage exception "
378 "while in user mode. Aborting\n");
380 case POWERPC_EXCP_HDSEG
: /* Hypervisor data segment exception */
381 cpu_abort(env
, "Hypervisor data segment exception "
382 "while in user mode. Aborting\n");
384 case POWERPC_EXCP_HISEG
: /* Hypervisor instruction segment excp */
385 cpu_abort(env
, "Hypervisor instruction segment exception "
386 "while in user mode. Aborting\n");
388 case POWERPC_EXCP_VPU
: /* Vector unavailable exception */
389 EXCP_DUMP(env
, "No Altivec instructions allowed\n");
390 info
.si_signo
= SIGILL
;
392 info
.si_code
= ILL_COPROC
;
393 info
.si_addr
= (void*)(env
->nip
- 4);
394 queue_signal(info
.si_signo
, &info
);
396 case POWERPC_EXCP_PIT
: /* Programmable interval timer IRQ */
397 cpu_abort(env
, "Programable interval timer interrupt "
398 "while in user mode. Aborting\n");
400 case POWERPC_EXCP_IO
: /* IO error exception */
401 cpu_abort(env
, "IO error exception while in user mode. "
404 case POWERPC_EXCP_RUNM
: /* Run mode exception */
405 cpu_abort(env
, "Run mode exception while in user mode. "
408 case POWERPC_EXCP_EMUL
: /* Emulation trap exception */
409 cpu_abort(env
, "Emulation trap exception not handled\n");
411 case POWERPC_EXCP_IFTLB
: /* Instruction fetch TLB error */
412 cpu_abort(env
, "Instruction fetch TLB exception "
413 "while in user-mode. Aborting");
415 case POWERPC_EXCP_DLTLB
: /* Data load TLB miss */
416 cpu_abort(env
, "Data load TLB exception while in user-mode. "
419 case POWERPC_EXCP_DSTLB
: /* Data store TLB miss */
420 cpu_abort(env
, "Data store TLB exception while in user-mode. "
423 case POWERPC_EXCP_FPA
: /* Floating-point assist exception */
424 cpu_abort(env
, "Floating-point assist exception not handled\n");
426 case POWERPC_EXCP_IABR
: /* Instruction address breakpoint */
427 cpu_abort(env
, "Instruction address breakpoint exception "
430 case POWERPC_EXCP_SMI
: /* System management interrupt */
431 cpu_abort(env
, "System management interrupt while in user mode. "
434 case POWERPC_EXCP_THERM
: /* Thermal interrupt */
435 cpu_abort(env
, "Thermal interrupt interrupt while in user mode. "
438 case POWERPC_EXCP_PERFM
: /* Embedded performance monitor IRQ */
439 cpu_abort(env
, "Performance monitor exception not handled\n");
441 case POWERPC_EXCP_VPUA
: /* Vector assist exception */
442 cpu_abort(env
, "Vector assist exception not handled\n");
444 case POWERPC_EXCP_SOFTP
: /* Soft patch exception */
445 cpu_abort(env
, "Soft patch exception not handled\n");
447 case POWERPC_EXCP_MAINT
: /* Maintenance exception */
448 cpu_abort(env
, "Maintenance exception while in user mode. "
451 case POWERPC_EXCP_STOP
: /* stop translation */
452 /* We did invalidate the instruction cache. Go on */
454 case POWERPC_EXCP_BRANCH
: /* branch instruction: */
455 /* We just stopped because of a branch. Go on */
457 case POWERPC_EXCP_SYSCALL_USER
:
458 /* system call in user-mode emulation */
460 if(((int)env
->gpr
[0]) <= SYS_MAXSYSCALL
&& ((int)env
->gpr
[0])>0)
461 ret
= do_unix_syscall(env
, env
->gpr
[0]/*, env->gpr[3], env->gpr[4],
462 env->gpr[5], env->gpr[6], env->gpr[7],
463 env->gpr[8], env->gpr[9], env->gpr[10]*/);
464 else if(((int)env
->gpr
[0])<0)
465 ret
= do_mach_syscall(env
, env
->gpr
[0], env
->gpr
[3], env
->gpr
[4],
466 env
->gpr
[5], env
->gpr
[6], env
->gpr
[7],
467 env
->gpr
[8], env
->gpr
[9], env
->gpr
[10]);
469 ret
= do_thread_syscall(env
, env
->gpr
[0], env
->gpr
[3], env
->gpr
[4],
470 env
->gpr
[5], env
->gpr
[6], env
->gpr
[7],
471 env
->gpr
[8], env
->gpr
[9], env
->gpr
[10]);
473 /* Unix syscall error signaling */
474 if(((int)env
->gpr
[0]) <= SYS_MAXSYSCALL
&& ((int)env
->gpr
[0])>0)
486 /* just indicate that signals should be handled asap */
489 cpu_abort(env
, "Unknown exception 0x%d. Aborting\n", trapnr
);
492 process_pending_signals(env
);
500 /***********************************************************/
501 /* CPUX86 core interface */
503 uint64_t cpu_get_tsc(CPUX86State
*env
)
505 return cpu_get_real_ticks();
509 write_dt(void *ptr
, unsigned long addr
, unsigned long limit
,
513 e1
= (addr
<< 16) | (limit
& 0xffff);
514 e2
= ((addr
>> 16) & 0xff) | (addr
& 0xff000000) | (limit
& 0x000f0000);
516 stl((uint8_t *)ptr
, e1
);
517 stl((uint8_t *)ptr
+ 4, e2
);
520 static void set_gate(void *ptr
, unsigned int type
, unsigned int dpl
,
521 unsigned long addr
, unsigned int sel
)
524 e1
= (addr
& 0xffff) | (sel
<< 16);
525 e2
= (addr
& 0xffff0000) | 0x8000 | (dpl
<< 13) | (type
<< 8);
526 stl((uint8_t *)ptr
, e1
);
527 stl((uint8_t *)ptr
+ 4, e2
);
530 #define GDT_TABLE_SIZE 14
531 #define LDT_TABLE_SIZE 15
532 #define IDT_TABLE_SIZE 256
534 uint64_t gdt_table
[GDT_TABLE_SIZE
];
535 uint64_t ldt_table
[LDT_TABLE_SIZE
];
536 uint64_t idt_table
[IDT_TABLE_SIZE
];
537 uint32_t tss
[TSS_SIZE
];
539 /* only dpl matters as we do only user space emulation */
540 static void set_idt(int n
, unsigned int dpl
)
542 set_gate(idt_table
+ n
, 0, dpl
, 0, 0);
545 /* ABI convention: after a syscall if there was an error the CF flag is set */
546 static inline void set_error(CPUX86State
*env
, int ret
)
549 env
->eflags
= env
->eflags
| 0x1;
552 env
->regs
[R_EAX
] = ret
;
555 void cpu_loop(CPUX86State
*env
)
560 target_siginfo_t info
;
563 trapnr
= cpu_x86_exec(env
);
564 uint32_t *params
= (uint32_t *)env
->regs
[R_ESP
];
566 case 0x79: /* Our commpage hack back door exit is here */
567 do_commpage(env
, env
->eip
, *(params
+ 1), *(params
+ 2),
568 *(params
+ 3), *(params
+ 4),
569 *(params
+ 5), *(params
+ 6),
570 *(params
+ 7), *(params
+ 8));
572 case 0x81: /* mach syscall */
574 ret
= do_mach_syscall(env
, env
->regs
[R_EAX
],
575 *(params
+ 1), *(params
+ 2),
576 *(params
+ 3), *(params
+ 4),
577 *(params
+ 5), *(params
+ 6),
578 *(params
+ 7), *(params
+ 8));
582 case 0x90: /* unix backdoor */
584 /* after sysenter, stack is in R_ECX, new eip in R_EDX (sysexit will flip them back)*/
585 int saved_stack
= env
->regs
[R_ESP
];
586 env
->regs
[R_ESP
] = env
->regs
[R_ECX
];
588 ret
= do_unix_syscall(env
, env
->regs
[R_EAX
]);
590 env
->regs
[R_ECX
] = env
->regs
[R_ESP
];
591 env
->regs
[R_ESP
] = saved_stack
;
596 case 0x80: /* unix syscall */
598 ret
= do_unix_syscall(env
, env
->regs
[R_EAX
]/*,
599 *(params + 1), *(params + 2),
600 *(params + 3), *(params + 4),
601 *(params + 5), *(params + 6),
602 *(params + 7), *(params + 8)*/);
606 case 0x82: /* thread syscall */
608 ret
= do_thread_syscall(env
, env
->regs
[R_EAX
],
609 *(params
+ 1), *(params
+ 2),
610 *(params
+ 3), *(params
+ 4),
611 *(params
+ 5), *(params
+ 6),
612 *(params
+ 7), *(params
+ 8));
618 info
.si_signo
= SIGBUS
;
620 info
.si_code
= BUS_NOOP
;
622 gdb_handlesig (env
, SIGBUS
);
623 queue_signal(info
.si_signo
, &info
);
626 info
.si_signo
= SIGSEGV
;
628 info
.si_code
= SEGV_NOOP
;
630 gdb_handlesig (env
, SIGSEGV
);
631 queue_signal(info
.si_signo
, &info
);
634 info
.si_signo
= SIGSEGV
;
636 if (!(env
->error_code
& 1))
637 info
.si_code
= SEGV_MAPERR
;
639 info
.si_code
= SEGV_ACCERR
;
640 info
.si_addr
= (void*)env
->cr
[2];
641 gdb_handlesig (env
, SIGSEGV
);
642 queue_signal(info
.si_signo
, &info
);
645 /* division by zero */
646 info
.si_signo
= SIGFPE
;
648 info
.si_code
= FPE_INTDIV
;
649 info
.si_addr
= (void*)env
->eip
;
650 gdb_handlesig (env
, SIGFPE
);
651 queue_signal(info
.si_signo
, &info
);
655 info
.si_signo
= SIGTRAP
;
657 info
.si_code
= TRAP_BRKPT
;
658 info
.si_addr
= (void*)env
->eip
;
659 gdb_handlesig (env
, SIGTRAP
);
660 queue_signal(info
.si_signo
, &info
);
664 info
.si_signo
= SIGSEGV
;
666 info
.si_code
= SEGV_NOOP
;
668 gdb_handlesig (env
, SIGSEGV
);
669 queue_signal(info
.si_signo
, &info
);
672 info
.si_signo
= SIGILL
;
674 info
.si_code
= ILL_ILLOPN
;
675 info
.si_addr
= (void*)env
->eip
;
676 gdb_handlesig (env
, SIGILL
);
677 queue_signal(info
.si_signo
, &info
);
680 /* just indicate that signals should be handled asap */
686 sig
= gdb_handlesig (env
, SIGTRAP
);
691 info
.si_code
= TRAP_BRKPT
;
692 queue_signal(info
.si_signo
, &info
);
697 pc
= (void*)(env
->segs
[R_CS
].base
+ env
->eip
);
698 fprintf(stderr
, "qemu: 0x%08lx: unhandled CPU exception 0x%x - aborting\n",
702 process_pending_signals(env
);
707 static void usage(void)
709 printf("qemu-" TARGET_ARCH
" version " QEMU_VERSION
", Copyright (c) 2003-2004 Fabrice Bellard\n"
710 "usage: qemu-" TARGET_ARCH
" [-h] [-d opts] [-L path] [-s size] program [arguments...]\n"
711 "Darwin CPU emulator (compiled for %s emulation)\n"
713 "-h print this help\n"
714 "-L path set the %s library path (default='%s')\n"
715 "-s size set the stack size in bytes (default=%ld)\n"
718 "-d options activate log (logfile='%s')\n"
719 "-g wait for gdb on port 1234\n"
720 "-p pagesize set the host page size to 'pagesize'\n",
721 "-singlestep always run in singlestep mode\n"
730 /* XXX: currently only used for async signals (see signal.c) */
731 CPUState
*global_env
;
732 /* used only if single thread */
733 CPUState
*cpu_single_env
= NULL
;
735 /* used to free thread contexts */
736 TaskState
*first_task_state
;
738 int main(int argc
, char **argv
)
740 const char *filename
;
741 const char *log_file
= DEBUG_LOGFILE
;
742 const char *log_mask
= NULL
;
743 struct target_pt_regs regs1
, *regs
= ®s1
;
744 TaskState ts1
, *ts
= &ts1
;
747 short use_gdbstub
= 0;
749 const char *cpu_model
;
763 if (!strcmp(r
, "-")) {
765 } else if (!strcmp(r
, "d")) {
766 if (optind
>= argc
) {
769 log_mask
= argv
[optind
++];
770 } else if (!strcmp(r
, "D")) {
771 if (optind
>= argc
) {
774 log_file
= argv
[optind
++];
775 } else if (!strcmp(r
, "s")) {
777 stack_size
= strtol(r
, (char **)&r
, 0);
781 stack_size
*= 1024 * 1024;
782 else if (*r
== 'k' || *r
== 'K')
784 } else if (!strcmp(r
, "L")) {
785 interp_prefix
= argv
[optind
++];
786 } else if (!strcmp(r
, "p")) {
787 qemu_host_page_size
= atoi(argv
[optind
++]);
788 if (qemu_host_page_size
== 0 ||
789 (qemu_host_page_size
& (qemu_host_page_size
- 1)) != 0) {
790 fprintf(stderr
, "page size must be a power of two\n");
794 if (!strcmp(r
, "g")) {
796 } else if (!strcmp(r
, "cpu")) {
797 cpu_model
= argv
[optind
++];
798 if (strcmp(cpu_model
, "?") == 0) {
799 /* XXX: implement xxx_cpu_list for targets that still miss it */
800 #if defined(cpu_list)
801 cpu_list(stdout
, &fprintf
);
805 } else if (!strcmp(r
, "singlestep")) {
814 cpu_set_log_filename(log_file
);
819 mask
= cpu_str_to_log_mask(log_mask
);
821 printf("Log items (comma separated):\n");
822 for (item
= cpu_log_items
; item
->mask
!= 0; item
++) {
823 printf("%-10s %s\n", item
->name
, item
->help
);
830 if (optind
>= argc
) {
833 filename
= argv
[optind
];
836 memset(regs
, 0, sizeof(struct target_pt_regs
));
838 if (cpu_model
== NULL
) {
839 #if defined(TARGET_I386)
841 cpu_model
= "qemu64";
843 cpu_model
= "qemu32";
845 #elif defined(TARGET_PPC)
852 #error unsupported CPU
856 cpu_exec_init_all(0);
857 /* NOTE: we need to init the CPU at this stage to get
858 qemu_host_page_size */
859 env
= cpu_init(cpu_model
);
862 printf("Starting %s with qemu\n----------------\n", filename
);
866 if (mach_exec(filename
, argv
+optind
, environ
, regs
) != 0) {
867 printf("Error loading %s\n", filename
);
875 /* build Task State */
876 memset(ts
, 0, sizeof(TaskState
));
880 #if defined(TARGET_I386)
881 cpu_x86_set_cpl(env
, 3);
883 env
->cr
[0] = CR0_PG_MASK
| CR0_WP_MASK
| CR0_PE_MASK
;
884 env
->hflags
|= HF_PE_MASK
;
886 if (env
->cpuid_features
& CPUID_SSE
) {
887 env
->cr
[4] |= CR4_OSFXSR_MASK
;
888 env
->hflags
|= HF_OSFXSR_MASK
;
891 /* flags setup : we activate the IRQs by default as in user mode */
892 env
->eflags
|= IF_MASK
;
894 /* darwin register setup */
895 env
->regs
[R_EAX
] = regs
->eax
;
896 env
->regs
[R_EBX
] = regs
->ebx
;
897 env
->regs
[R_ECX
] = regs
->ecx
;
898 env
->regs
[R_EDX
] = regs
->edx
;
899 env
->regs
[R_ESI
] = regs
->esi
;
900 env
->regs
[R_EDI
] = regs
->edi
;
901 env
->regs
[R_EBP
] = regs
->ebp
;
902 env
->regs
[R_ESP
] = regs
->esp
;
903 env
->eip
= regs
->eip
;
905 /* Darwin LDT setup */
906 /* 2 - User code segment
907 3 - User data segment
909 bzero(ldt_table
, LDT_TABLE_SIZE
* sizeof(ldt_table
[0]));
910 env
->ldt
.base
= (uint32_t) ldt_table
;
911 env
->ldt
.limit
= sizeof(ldt_table
) - 1;
913 write_dt(ldt_table
+ 2, 0, 0xfffff,
914 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
| DESC_S_MASK
|
915 (3 << DESC_DPL_SHIFT
) | (0xa << DESC_TYPE_SHIFT
));
916 write_dt(ldt_table
+ 3, 0, 0xfffff,
917 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
| DESC_S_MASK
|
918 (3 << DESC_DPL_SHIFT
) | (0x2 << DESC_TYPE_SHIFT
));
919 write_dt(ldt_table
+ 4, 0, 0xfffff,
920 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
| DESC_S_MASK
|
921 (3 << DESC_DPL_SHIFT
) | (0x2 << DESC_TYPE_SHIFT
));
924 * has changed a lot between old Darwin/x86 (pre-Mac Intel) and Mac OS X/x86,
925 now everything is done via int 0x81(mach) int 0x82 (thread) and sysenter/sysexit(unix) */
926 bzero(gdt_table
, sizeof(gdt_table
));
927 env
->gdt
.base
= (uint32_t)gdt_table
;
928 env
->gdt
.limit
= sizeof(gdt_table
) - 1;
930 /* Set up a back door to handle sysenter syscalls (unix) */
931 char * syscallbackdoor
= malloc(64);
932 page_set_flags((int)syscallbackdoor
, (int)syscallbackdoor
+ 64, PROT_EXEC
| PROT_READ
| PAGE_VALID
);
935 syscallbackdoor
[i
++] = 0xcd;
936 syscallbackdoor
[i
++] = 0x90; /* int 0x90 */
937 syscallbackdoor
[i
++] = 0x0F;
938 syscallbackdoor
[i
++] = 0x35; /* sysexit */
940 /* Darwin sysenter/sysexit setup */
941 env
->sysenter_cs
= 0x1; //XXX
942 env
->sysenter_eip
= (int)syscallbackdoor
;
943 env
->sysenter_esp
= (int)malloc(64);
946 This must match up with GDT[4] */
947 env
->tr
.base
= (uint32_t) tss
;
948 env
->tr
.limit
= sizeof(tss
) - 1;
949 env
->tr
.flags
= DESC_P_MASK
| (0x9 << DESC_TYPE_SHIFT
);
950 stw(tss
+ 2, 0x10); // ss0 = 0x10 = GDT[2] = Kernel Data Segment
952 /* Darwin interrupt setup */
953 bzero(idt_table
, sizeof(idt_table
));
954 env
->idt
.base
= (uint32_t) idt_table
;
955 env
->idt
.limit
= sizeof(idt_table
) - 1;
976 /* Syscalls are done via
977 int 0x80 (unix) (rarely used)
980 int 0x83 (diag) (not handled here)
981 sysenter/sysexit (unix) -> we redirect that to int 0x90 */
982 set_idt(0x79, 3); /* Commpage hack, here is our backdoor interrupt */
983 set_idt(0x80, 3); /* Unix Syscall */
984 set_idt(0x81, 3); /* Mach Syscalls */
985 set_idt(0x82, 3); /* thread Syscalls */
987 set_idt(0x90, 3); /* qemu-darwin-user's Unix syscalls backdoor */
990 cpu_x86_load_seg(env
, R_CS
, __USER_CS
);
991 cpu_x86_load_seg(env
, R_DS
, __USER_DS
);
992 cpu_x86_load_seg(env
, R_ES
, __USER_DS
);
993 cpu_x86_load_seg(env
, R_SS
, __USER_DS
);
994 cpu_x86_load_seg(env
, R_FS
, __USER_DS
);
995 cpu_x86_load_seg(env
, R_GS
, __USER_DS
);
997 #elif defined(TARGET_PPC)
1001 #if defined(TARGET_PPC64)
1002 #if defined(TARGET_ABI32)
1003 env
->msr
&= ~((target_ulong
)1 << MSR_SF
);
1005 env
->msr
|= (target_ulong
)1 << MSR_SF
;
1008 env
->nip
= regs
->nip
;
1009 for(i
= 0; i
< 32; i
++) {
1010 env
->gpr
[i
] = regs
->gpr
[i
];
1014 #error unsupported target CPU
1018 printf("Waiting for gdb Connection on port 1234...\n");
1019 gdbserver_start (1234);
1020 gdb_handlesig(env
, 0);