]> git.ipfire.org Git - thirdparty/qemu.git/blob - darwin-user/main.c
27c7284eb70310afecadcdd437adcf9c8762cc8e
[thirdparty/qemu.git] / darwin-user / main.c
1 /*
2 * qemu user main
3 *
4 * Copyright (c) 2003 Fabrice Bellard
5 * Copyright (c) 2006 Pierre d'Herbemont
6 *
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.
11 *
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.
16 *
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,
20 * MA 02110-1301, USA.
21 */
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <stdarg.h>
25 #include <string.h>
26 #include <errno.h>
27 #include <unistd.h>
28
29 #include <sys/syscall.h>
30 #include <sys/mman.h>
31
32 #include "qemu.h"
33
34 #define DEBUG_LOGFILE "/tmp/qemu.log"
35
36 #ifdef __APPLE__
37 #include <crt_externs.h>
38 # define environ (*_NSGetEnviron())
39 #endif
40
41 #include <mach/mach_init.h>
42 #include <mach/vm_map.h>
43
44 int singlestep;
45
46 const char *interp_prefix = "";
47
48 asm(".zerofill __STD_PROG_ZONE, __STD_PROG_ZONE, __std_prog_zone, 0x0dfff000");
49
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;
54
55 void qerror(const char *fmt, ...)
56 {
57 va_list ap;
58
59 va_start(ap, fmt);
60 vfprintf(stderr, fmt, ap);
61 va_end(ap);
62 fprintf(stderr, "\n");
63 exit(1);
64 }
65
66 void gemu_log(const char *fmt, ...)
67 {
68 va_list ap;
69
70 va_start(ap, fmt);
71 vfprintf(stderr, fmt, ap);
72 va_end(ap);
73 }
74
75 int cpu_get_pic_interrupt(CPUState *env)
76 {
77 return -1;
78 }
79 #ifdef TARGET_PPC
80
81 static inline uint64_t cpu_ppc_get_tb (CPUState *env)
82 {
83 /* TO FIX */
84 return 0;
85 }
86
87 uint32_t cpu_ppc_load_tbl (CPUState *env)
88 {
89 return cpu_ppc_get_tb(env) & 0xFFFFFFFF;
90 }
91
92 uint32_t cpu_ppc_load_tbu (CPUState *env)
93 {
94 return cpu_ppc_get_tb(env) >> 32;
95 }
96
97 uint32_t cpu_ppc_load_atbl (CPUState *env)
98 {
99 return cpu_ppc_get_tb(env) & 0xFFFFFFFF;
100 }
101
102 uint32_t cpu_ppc_load_atbu (CPUState *env)
103 {
104 return cpu_ppc_get_tb(env) >> 32;
105 }
106
107 uint32_t cpu_ppc601_load_rtcu (CPUState *env)
108 {
109 cpu_ppc_load_tbu(env);
110 }
111
112 uint32_t cpu_ppc601_load_rtcl (CPUState *env)
113 {
114 return cpu_ppc_load_tbl(env) & 0x3FFFFF80;
115 }
116
117 /* XXX: to be fixed */
118 int ppc_dcr_read (ppc_dcr_t *dcr_env, int dcrn, target_ulong *valp)
119 {
120 return -1;
121 }
122
123 int ppc_dcr_write (ppc_dcr_t *dcr_env, int dcrn, target_ulong val)
124 {
125 return -1;
126 }
127
128 #define EXCP_DUMP(env, fmt, ...) \
129 do { \
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); \
134 } while (0)
135
136 void cpu_loop(CPUPPCState *env)
137 {
138 int trapnr;
139 uint32_t ret;
140 target_siginfo_t info;
141
142 for(;;) {
143 trapnr = cpu_ppc_exec(env);
144 switch(trapnr) {
145 case POWERPC_EXCP_NONE:
146 /* Just go on */
147 break;
148 case POWERPC_EXCP_CRITICAL: /* Critical input */
149 cpu_abort(env, "Critical interrupt while in user mode. "
150 "Aborting\n");
151 break;
152 case POWERPC_EXCP_MCHECK: /* Machine check exception */
153 cpu_abort(env, "Machine check exception while in user mode. "
154 "Aborting\n");
155 break;
156 case POWERPC_EXCP_DSI: /* Data storage exception */
157 #ifndef DAR
158 /* To deal with multiple qemu header version as host for the darwin-user code */
159 # define DAR SPR_DAR
160 #endif
161 EXCP_DUMP(env, "Invalid data memory access: 0x" ADDRX "\n",
162 env->spr[SPR_DAR]);
163 /* Handle this via the gdb */
164 gdb_handlesig (env, SIGSEGV);
165
166 info.si_addr = (void*)env->nip;
167 queue_signal(info.si_signo, &info);
168 break;
169 case POWERPC_EXCP_ISI: /* Instruction storage exception */
170 EXCP_DUMP(env, "Invalid instruction fetch: 0x\n" ADDRX "\n",
171 env->spr[SPR_DAR]);
172 /* Handle this via the gdb */
173 gdb_handlesig (env, SIGSEGV);
174
175 info.si_addr = (void*)(env->nip - 4);
176 queue_signal(info.si_signo, &info);
177 break;
178 case POWERPC_EXCP_EXTERNAL: /* External input */
179 cpu_abort(env, "External interrupt while in user mode. "
180 "Aborting\n");
181 break;
182 case POWERPC_EXCP_ALIGN: /* Alignment exception */
183 EXCP_DUMP(env, "Unaligned memory access\n");
184 info.si_errno = 0;
185 info.si_code = BUS_ADRALN;
186 info.si_addr = (void*)(env->nip - 4);
187 queue_signal(info.si_signo, &info);
188 break;
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");
194 /* Set FX */
195 info.si_signo = SIGFPE;
196 info.si_errno = 0;
197 switch (env->error_code & 0xF) {
198 case POWERPC_EXCP_FP_OX:
199 info.si_code = FPE_FLTOVF;
200 break;
201 case POWERPC_EXCP_FP_UX:
202 info.si_code = FPE_FLTUND;
203 break;
204 case POWERPC_EXCP_FP_ZX:
205 case POWERPC_EXCP_FP_VXZDZ:
206 info.si_code = FPE_FLTDIV;
207 break;
208 case POWERPC_EXCP_FP_XX:
209 info.si_code = FPE_FLTRES;
210 break;
211 case POWERPC_EXCP_FP_VXSOFT:
212 info.si_code = FPE_FLTINV;
213 break;
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;
222 break;
223 default:
224 EXCP_DUMP(env, "Unknown floating point exception (%02x)\n",
225 env->error_code);
226 break;
227 }
228 break;
229 case POWERPC_EXCP_INVAL:
230 EXCP_DUMP(env, "Invalid instruction\n");
231 info.si_signo = SIGILL;
232 info.si_errno = 0;
233 switch (env->error_code & 0xF) {
234 case POWERPC_EXCP_INVAL_INVAL:
235 info.si_code = ILL_ILLOPC;
236 break;
237 case POWERPC_EXCP_INVAL_LSWX:
238 info.si_code = ILL_ILLOPN;
239 break;
240 case POWERPC_EXCP_INVAL_SPR:
241 info.si_code = ILL_PRVREG;
242 break;
243 case POWERPC_EXCP_INVAL_FP:
244 info.si_code = ILL_COPROC;
245 break;
246 default:
247 EXCP_DUMP(env, "Unknown invalid operation (%02x)\n",
248 env->error_code & 0xF);
249 info.si_code = ILL_ILLADR;
250 break;
251 }
252 /* Handle this via the gdb */
253 gdb_handlesig (env, SIGSEGV);
254 break;
255 case POWERPC_EXCP_PRIV:
256 EXCP_DUMP(env, "Privilege violation\n");
257 info.si_signo = SIGILL;
258 info.si_errno = 0;
259 switch (env->error_code & 0xF) {
260 case POWERPC_EXCP_PRIV_OPC:
261 info.si_code = ILL_PRVOPC;
262 break;
263 case POWERPC_EXCP_PRIV_REG:
264 info.si_code = ILL_PRVREG;
265 break;
266 default:
267 EXCP_DUMP(env, "Unknown privilege violation (%02x)\n",
268 env->error_code & 0xF);
269 info.si_code = ILL_PRVOPC;
270 break;
271 }
272 break;
273 case POWERPC_EXCP_TRAP:
274 cpu_abort(env, "Tried to call a TRAP\n");
275 break;
276 default:
277 /* Should not happen ! */
278 cpu_abort(env, "Unknown program exception (%02x)\n",
279 env->error_code);
280 break;
281 }
282 info.si_addr = (void*)(env->nip - 4);
283 queue_signal(info.si_signo, &info);
284 break;
285 case POWERPC_EXCP_FPU: /* Floating-point unavailable exception */
286 EXCP_DUMP(env, "No floating point allowed\n");
287 info.si_signo = SIGILL;
288 info.si_errno = 0;
289 info.si_code = ILL_COPROC;
290 info.si_addr = (void*)(env->nip - 4);
291 queue_signal(info.si_signo, &info);
292 break;
293 case POWERPC_EXCP_SYSCALL: /* System call exception */
294 cpu_abort(env, "Syscall exception while in user mode. "
295 "Aborting\n");
296 break;
297 case POWERPC_EXCP_APU: /* Auxiliary processor unavailable */
298 EXCP_DUMP(env, "No APU instruction allowed\n");
299 info.si_signo = SIGILL;
300 info.si_errno = 0;
301 info.si_code = ILL_COPROC;
302 info.si_addr = (void*)(env->nip - 4);
303 queue_signal(info.si_signo, &info);
304 break;
305 case POWERPC_EXCP_DECR: /* Decrementer exception */
306 cpu_abort(env, "Decrementer interrupt while in user mode. "
307 "Aborting\n");
308 break;
309 case POWERPC_EXCP_FIT: /* Fixed-interval timer interrupt */
310 cpu_abort(env, "Fix interval timer interrupt while in user mode. "
311 "Aborting\n");
312 break;
313 case POWERPC_EXCP_WDT: /* Watchdog timer interrupt */
314 cpu_abort(env, "Watchdog timer interrupt while in user mode. "
315 "Aborting\n");
316 break;
317 case POWERPC_EXCP_DTLB: /* Data TLB error */
318 cpu_abort(env, "Data TLB exception while in user mode. "
319 "Aborting\n");
320 break;
321 case POWERPC_EXCP_ITLB: /* Instruction TLB error */
322 cpu_abort(env, "Instruction TLB exception while in user mode. "
323 "Aborting\n");
324 break;
325 case POWERPC_EXCP_DEBUG: /* Debug interrupt */
326 gdb_handlesig (env, SIGTRAP);
327 break;
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;
331 info.si_errno = 0;
332 info.si_code = ILL_COPROC;
333 info.si_addr = (void*)(env->nip - 4);
334 queue_signal(info.si_signo, &info);
335 break;
336 case POWERPC_EXCP_EFPDI: /* Embedded floating-point data IRQ */
337 cpu_abort(env, "Embedded floating-point data IRQ not handled\n");
338 break;
339 case POWERPC_EXCP_EFPRI: /* Embedded floating-point round IRQ */
340 cpu_abort(env, "Embedded floating-point round IRQ not handled\n");
341 break;
342 case POWERPC_EXCP_EPERFM: /* Embedded performance monitor IRQ */
343 cpu_abort(env, "Performance monitor exception not handled\n");
344 break;
345 case POWERPC_EXCP_DOORI: /* Embedded doorbell interrupt */
346 cpu_abort(env, "Doorbell interrupt while in user mode. "
347 "Aborting\n");
348 break;
349 case POWERPC_EXCP_DOORCI: /* Embedded doorbell critical interrupt */
350 cpu_abort(env, "Doorbell critical interrupt while in user mode. "
351 "Aborting\n");
352 break;
353 case POWERPC_EXCP_RESET: /* System reset exception */
354 cpu_abort(env, "Reset interrupt while in user mode. "
355 "Aborting\n");
356 break;
357 case POWERPC_EXCP_DSEG: /* Data segment exception */
358 cpu_abort(env, "Data segment exception while in user mode. "
359 "Aborting\n");
360 break;
361 case POWERPC_EXCP_ISEG: /* Instruction segment exception */
362 cpu_abort(env, "Instruction segment exception "
363 "while in user mode. Aborting\n");
364 break;
365 case POWERPC_EXCP_HDECR: /* Hypervisor decrementer exception */
366 cpu_abort(env, "Hypervisor decrementer interrupt "
367 "while in user mode. Aborting\n");
368 break;
369 case POWERPC_EXCP_TRACE: /* Trace exception */
370 /* Nothing to do:
371 * we use this exception to emulate step-by-step execution mode.
372 */
373 break;
374 case POWERPC_EXCP_HDSI: /* Hypervisor data storage exception */
375 cpu_abort(env, "Hypervisor data storage exception "
376 "while in user mode. Aborting\n");
377 break;
378 case POWERPC_EXCP_HISI: /* Hypervisor instruction storage excp */
379 cpu_abort(env, "Hypervisor instruction storage exception "
380 "while in user mode. Aborting\n");
381 break;
382 case POWERPC_EXCP_HDSEG: /* Hypervisor data segment exception */
383 cpu_abort(env, "Hypervisor data segment exception "
384 "while in user mode. Aborting\n");
385 break;
386 case POWERPC_EXCP_HISEG: /* Hypervisor instruction segment excp */
387 cpu_abort(env, "Hypervisor instruction segment exception "
388 "while in user mode. Aborting\n");
389 break;
390 case POWERPC_EXCP_VPU: /* Vector unavailable exception */
391 EXCP_DUMP(env, "No Altivec instructions allowed\n");
392 info.si_signo = SIGILL;
393 info.si_errno = 0;
394 info.si_code = ILL_COPROC;
395 info.si_addr = (void*)(env->nip - 4);
396 queue_signal(info.si_signo, &info);
397 break;
398 case POWERPC_EXCP_PIT: /* Programmable interval timer IRQ */
399 cpu_abort(env, "Programable interval timer interrupt "
400 "while in user mode. Aborting\n");
401 break;
402 case POWERPC_EXCP_IO: /* IO error exception */
403 cpu_abort(env, "IO error exception while in user mode. "
404 "Aborting\n");
405 break;
406 case POWERPC_EXCP_RUNM: /* Run mode exception */
407 cpu_abort(env, "Run mode exception while in user mode. "
408 "Aborting\n");
409 break;
410 case POWERPC_EXCP_EMUL: /* Emulation trap exception */
411 cpu_abort(env, "Emulation trap exception not handled\n");
412 break;
413 case POWERPC_EXCP_IFTLB: /* Instruction fetch TLB error */
414 cpu_abort(env, "Instruction fetch TLB exception "
415 "while in user-mode. Aborting");
416 break;
417 case POWERPC_EXCP_DLTLB: /* Data load TLB miss */
418 cpu_abort(env, "Data load TLB exception while in user-mode. "
419 "Aborting");
420 break;
421 case POWERPC_EXCP_DSTLB: /* Data store TLB miss */
422 cpu_abort(env, "Data store TLB exception while in user-mode. "
423 "Aborting");
424 break;
425 case POWERPC_EXCP_FPA: /* Floating-point assist exception */
426 cpu_abort(env, "Floating-point assist exception not handled\n");
427 break;
428 case POWERPC_EXCP_IABR: /* Instruction address breakpoint */
429 cpu_abort(env, "Instruction address breakpoint exception "
430 "not handled\n");
431 break;
432 case POWERPC_EXCP_SMI: /* System management interrupt */
433 cpu_abort(env, "System management interrupt while in user mode. "
434 "Aborting\n");
435 break;
436 case POWERPC_EXCP_THERM: /* Thermal interrupt */
437 cpu_abort(env, "Thermal interrupt interrupt while in user mode. "
438 "Aborting\n");
439 break;
440 case POWERPC_EXCP_PERFM: /* Embedded performance monitor IRQ */
441 cpu_abort(env, "Performance monitor exception not handled\n");
442 break;
443 case POWERPC_EXCP_VPUA: /* Vector assist exception */
444 cpu_abort(env, "Vector assist exception not handled\n");
445 break;
446 case POWERPC_EXCP_SOFTP: /* Soft patch exception */
447 cpu_abort(env, "Soft patch exception not handled\n");
448 break;
449 case POWERPC_EXCP_MAINT: /* Maintenance exception */
450 cpu_abort(env, "Maintenance exception while in user mode. "
451 "Aborting\n");
452 break;
453 case POWERPC_EXCP_STOP: /* stop translation */
454 /* We did invalidate the instruction cache. Go on */
455 break;
456 case POWERPC_EXCP_BRANCH: /* branch instruction: */
457 /* We just stopped because of a branch. Go on */
458 break;
459 case POWERPC_EXCP_SYSCALL_USER:
460 /* system call in user-mode emulation */
461 /* system call */
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]);
470 else
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]);
474
475 /* Unix syscall error signaling */
476 if(((int)env->gpr[0]) <= SYS_MAXSYSCALL && ((int)env->gpr[0])>0)
477 {
478 if( (int)ret < 0 )
479 env->nip += 0;
480 else
481 env->nip += 4;
482 }
483
484 /* Return value */
485 env->gpr[3] = ret;
486 break;
487 case EXCP_INTERRUPT:
488 /* just indicate that signals should be handled asap */
489 break;
490 default:
491 cpu_abort(env, "Unknown exception 0x%d. Aborting\n", trapnr);
492 break;
493 }
494 process_pending_signals(env);
495 }
496 }
497 #endif
498
499
500 #ifdef TARGET_I386
501
502 /***********************************************************/
503 /* CPUX86 core interface */
504
505 uint64_t cpu_get_tsc(CPUX86State *env)
506 {
507 return cpu_get_real_ticks();
508 }
509
510 void
511 write_dt(void *ptr, unsigned long addr, unsigned long limit,
512 int flags)
513 {
514 unsigned int e1, e2;
515 e1 = (addr << 16) | (limit & 0xffff);
516 e2 = ((addr >> 16) & 0xff) | (addr & 0xff000000) | (limit & 0x000f0000);
517 e2 |= flags;
518 stl((uint8_t *)ptr, e1);
519 stl((uint8_t *)ptr + 4, e2);
520 }
521
522 static void set_gate(void *ptr, unsigned int type, unsigned int dpl,
523 unsigned long addr, unsigned int sel)
524 {
525 unsigned int e1, e2;
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);
530 }
531
532 #define GDT_TABLE_SIZE 14
533 #define LDT_TABLE_SIZE 15
534 #define IDT_TABLE_SIZE 256
535 #define TSS_SIZE 104
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];
540
541 /* only dpl matters as we do only user space emulation */
542 static void set_idt(int n, unsigned int dpl)
543 {
544 set_gate(idt_table + n, 0, dpl, 0, 0);
545 }
546
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)
549 {
550 if(ret<0)
551 env->eflags = env->eflags | 0x1;
552 else
553 env->eflags &= ~0x1;
554 env->regs[R_EAX] = ret;
555 }
556
557 void cpu_loop(CPUX86State *env)
558 {
559 int trapnr;
560 int ret;
561 uint8_t *pc;
562 target_siginfo_t info;
563
564 for(;;) {
565 trapnr = cpu_x86_exec(env);
566 uint32_t *params = (uint32_t *)env->regs[R_ESP];
567 switch(trapnr) {
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));
573 break;
574 case 0x81: /* mach syscall */
575 {
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));
581 set_error(env, ret);
582 break;
583 }
584 case 0x90: /* unix backdoor */
585 {
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];
589
590 ret = do_unix_syscall(env, env->regs[R_EAX]);
591
592 env->regs[R_ECX] = env->regs[R_ESP];
593 env->regs[R_ESP] = saved_stack;
594
595 set_error(env, ret);
596 break;
597 }
598 case 0x80: /* unix syscall */
599 {
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)*/);
605 set_error(env, ret);
606 break;
607 }
608 case 0x82: /* thread syscall */
609 {
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));
615 set_error(env, ret);
616 break;
617 }
618 case EXCP0B_NOSEG:
619 case EXCP0C_STACK:
620 info.si_signo = SIGBUS;
621 info.si_errno = 0;
622 info.si_code = BUS_NOOP;
623 info.si_addr = 0;
624 gdb_handlesig (env, SIGBUS);
625 queue_signal(info.si_signo, &info);
626 break;
627 case EXCP0D_GPF:
628 info.si_signo = SIGSEGV;
629 info.si_errno = 0;
630 info.si_code = SEGV_NOOP;
631 info.si_addr = 0;
632 gdb_handlesig (env, SIGSEGV);
633 queue_signal(info.si_signo, &info);
634 break;
635 case EXCP0E_PAGE:
636 info.si_signo = SIGSEGV;
637 info.si_errno = 0;
638 if (!(env->error_code & 1))
639 info.si_code = SEGV_MAPERR;
640 else
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);
645 break;
646 case EXCP00_DIVZ:
647 /* division by zero */
648 info.si_signo = SIGFPE;
649 info.si_errno = 0;
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);
654 break;
655 case EXCP01_SSTP:
656 case EXCP03_INT3:
657 info.si_signo = SIGTRAP;
658 info.si_errno = 0;
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);
663 break;
664 case EXCP04_INTO:
665 case EXCP05_BOUND:
666 info.si_signo = SIGSEGV;
667 info.si_errno = 0;
668 info.si_code = SEGV_NOOP;
669 info.si_addr = 0;
670 gdb_handlesig (env, SIGSEGV);
671 queue_signal(info.si_signo, &info);
672 break;
673 case EXCP06_ILLOP:
674 info.si_signo = SIGILL;
675 info.si_errno = 0;
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);
680 break;
681 case EXCP_INTERRUPT:
682 /* just indicate that signals should be handled asap */
683 break;
684 case EXCP_DEBUG:
685 {
686 int sig;
687
688 sig = gdb_handlesig (env, SIGTRAP);
689 if (sig)
690 {
691 info.si_signo = sig;
692 info.si_errno = 0;
693 info.si_code = TRAP_BRKPT;
694 queue_signal(info.si_signo, &info);
695 }
696 }
697 break;
698 default:
699 pc = (void*)(env->segs[R_CS].base + env->eip);
700 fprintf(stderr, "qemu: 0x%08lx: unhandled CPU exception 0x%x - aborting\n",
701 (long)pc, trapnr);
702 abort();
703 }
704 process_pending_signals(env);
705 }
706 }
707 #endif
708
709 void usage(void)
710 {
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"
714 "\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"
718 "\n"
719 "debug options:\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"
724 TARGET_ARCH,
725 TARGET_ARCH,
726 interp_prefix,
727 stack_size,
728 DEBUG_LOGFILE);
729 exit(1);
730 }
731
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;
736
737 /* used to free thread contexts */
738 TaskState *first_task_state;
739
740 int main(int argc, char **argv)
741 {
742 const char *filename;
743 struct target_pt_regs regs1, *regs = &regs1;
744 TaskState ts1, *ts = &ts1;
745 CPUState *env;
746 int optind;
747 short use_gdbstub = 0;
748 const char *r;
749 const char *cpu_model;
750
751 if (argc <= 1)
752 usage();
753
754 /* init debug */
755 cpu_set_log_filename(DEBUG_LOGFILE);
756
757 optind = 1;
758 for(;;) {
759 if (optind >= argc)
760 break;
761 r = argv[optind];
762 if (r[0] != '-')
763 break;
764 optind++;
765 r++;
766 if (!strcmp(r, "-")) {
767 break;
768 } else if (!strcmp(r, "d")) {
769 int mask;
770 CPULogItem *item;
771
772 if (optind >= argc)
773 break;
774
775 r = argv[optind++];
776 mask = cpu_str_to_log_mask(r);
777 if (!mask) {
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);
781 }
782 exit(1);
783 }
784 cpu_set_log(mask);
785 } else if (!strcmp(r, "s")) {
786 r = argv[optind++];
787 stack_size = strtol(r, (char **)&r, 0);
788 if (stack_size <= 0)
789 usage();
790 if (*r == 'M')
791 stack_size *= 1024 * 1024;
792 else if (*r == 'k' || *r == 'K')
793 stack_size *= 1024;
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");
801 exit(1);
802 }
803 } else
804 if (!strcmp(r, "g")) {
805 use_gdbstub = 1;
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);
812 #endif
813 exit(1);
814 }
815 } else if (!strcmp(r, "singlestep")) {
816 singlestep = 1;
817 } else
818 {
819 usage();
820 }
821 }
822 if (optind >= argc)
823 usage();
824 filename = argv[optind];
825
826 /* Zero out regs */
827 memset(regs, 0, sizeof(struct target_pt_regs));
828
829 if (cpu_model == NULL) {
830 #if defined(TARGET_I386)
831 #ifdef TARGET_X86_64
832 cpu_model = "qemu64";
833 #else
834 cpu_model = "qemu32";
835 #endif
836 #elif defined(TARGET_PPC)
837 #ifdef TARGET_PPC64
838 cpu_model = "970";
839 #else
840 cpu_model = "750";
841 #endif
842 #else
843 #error unsupported CPU
844 #endif
845 }
846
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);
851
852 printf("Starting %s with qemu\n----------------\n", filename);
853
854 commpage_init();
855
856 if (mach_exec(filename, argv+optind, environ, regs) != 0) {
857 printf("Error loading %s\n", filename);
858 _exit(1);
859 }
860
861 syscall_init();
862 signal_init();
863 global_env = env;
864
865 /* build Task State */
866 memset(ts, 0, sizeof(TaskState));
867 env->opaque = ts;
868 ts->used = 1;
869
870 #if defined(TARGET_I386)
871 cpu_x86_set_cpl(env, 3);
872
873 env->cr[0] = CR0_PG_MASK | CR0_WP_MASK | CR0_PE_MASK;
874 env->hflags |= HF_PE_MASK;
875
876 if (env->cpuid_features & CPUID_SSE) {
877 env->cr[4] |= CR4_OSFXSR_MASK;
878 env->hflags |= HF_OSFXSR_MASK;
879 }
880
881 /* flags setup : we activate the IRQs by default as in user mode */
882 env->eflags |= IF_MASK;
883
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;
894
895 /* Darwin LDT setup */
896 /* 2 - User code segment
897 3 - User data segment
898 4 - User cthread */
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;
902
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));
912
913 /* Darwin GDT setup.
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;
919
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);
923
924 int i = 0;
925 syscallbackdoor[i++] = 0xcd;
926 syscallbackdoor[i++] = 0x90; /* int 0x90 */
927 syscallbackdoor[i++] = 0x0F;
928 syscallbackdoor[i++] = 0x35; /* sysexit */
929
930 /* Darwin sysenter/sysexit setup */
931 env->sysenter_cs = 0x1; //XXX
932 env->sysenter_eip = (int)syscallbackdoor;
933 env->sysenter_esp = (int)malloc(64);
934
935 /* Darwin TSS setup
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
941
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;
946 set_idt(0, 0);
947 set_idt(1, 0);
948 set_idt(2, 0);
949 set_idt(3, 3);
950 set_idt(4, 3);
951 set_idt(5, 3);
952 set_idt(6, 0);
953 set_idt(7, 0);
954 set_idt(8, 0);
955 set_idt(9, 0);
956 set_idt(10, 0);
957 set_idt(11, 0);
958 set_idt(12, 0);
959 set_idt(13, 0);
960 set_idt(14, 0);
961 set_idt(15, 0);
962 set_idt(16, 0);
963 set_idt(17, 0);
964 set_idt(18, 0);
965 set_idt(19, 0);
966 /* Syscalls are done via
967 int 0x80 (unix) (rarely used)
968 int 0x81 (mach)
969 int 0x82 (thread)
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 */
976
977 set_idt(0x90, 3); /* qemu-darwin-user's Unix syscalls backdoor */
978
979
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);
986
987 #elif defined(TARGET_PPC)
988 {
989 int i;
990
991 #if defined(TARGET_PPC64)
992 #if defined(TARGET_ABI32)
993 env->msr &= ~((target_ulong)1 << MSR_SF);
994 #else
995 env->msr |= (target_ulong)1 << MSR_SF;
996 #endif
997 #endif
998 env->nip = regs->nip;
999 for(i = 0; i < 32; i++) {
1000 env->gpr[i] = regs->gpr[i];
1001 }
1002 }
1003 #else
1004 #error unsupported target CPU
1005 #endif
1006
1007 if (use_gdbstub) {
1008 printf("Waiting for gdb Connection on port 1234...\n");
1009 gdbserver_start (1234);
1010 gdb_handlesig(env, 0);
1011 }
1012
1013 cpu_loop(env);
1014 /* never exits */
1015 return 0;
1016 }