]> git.proxmox.com Git - qemu.git/blob - darwin-user/main.c
Rework PowerPC exceptions model to make it more versatile:
[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., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <stdarg.h>
24 #include <string.h>
25 #include <errno.h>
26 #include <unistd.h>
27
28 #include <sys/syscall.h>
29 #include <sys/mman.h>
30
31 #include "qemu.h"
32
33 #define DEBUG_LOGFILE "/tmp/qemu.log"
34
35 #ifdef __APPLE__
36 #include <crt_externs.h>
37 # define environ (*_NSGetEnviron())
38 #endif
39
40 #include <mach/mach_init.h>
41 #include <mach/vm_map.h>
42
43 const char *interp_prefix = "";
44
45 asm(".zerofill __STD_PROG_ZONE, __STD_PROG_ZONE, __std_prog_zone, 0x0dfff000");
46
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;
51
52 void qerror(const char *fmt, ...)
53 {
54 va_list ap;
55
56 va_start(ap, fmt);
57 vfprintf(stderr, fmt, ap);
58 va_end(ap);
59 fprintf(stderr, "\n");
60 exit(1);
61 }
62
63 void gemu_log(const char *fmt, ...)
64 {
65 va_list ap;
66
67 va_start(ap, fmt);
68 vfprintf(stderr, fmt, ap);
69 va_end(ap);
70 }
71
72 void cpu_outb(CPUState *env, int addr, int val)
73 {
74 fprintf(stderr, "outb: port=0x%04x, data=%02x\n", addr, val);
75 }
76
77 void cpu_outw(CPUState *env, int addr, int val)
78 {
79 fprintf(stderr, "outw: port=0x%04x, data=%04x\n", addr, val);
80 }
81
82 void cpu_outl(CPUState *env, int addr, int val)
83 {
84 fprintf(stderr, "outl: port=0x%04x, data=%08x\n", addr, val);
85 }
86
87 int cpu_inb(CPUState *env, int addr)
88 {
89 fprintf(stderr, "inb: port=0x%04x\n", addr);
90 return 0;
91 }
92
93 int cpu_inw(CPUState *env, int addr)
94 {
95 fprintf(stderr, "inw: port=0x%04x\n", addr);
96 return 0;
97 }
98
99 int cpu_inl(CPUState *env, int addr)
100 {
101 fprintf(stderr, "inl: port=0x%04x\n", addr);
102 return 0;
103 }
104
105 int cpu_get_pic_interrupt(CPUState *env)
106 {
107 return -1;
108 }
109 #ifdef TARGET_PPC
110
111 static inline uint64_t cpu_ppc_get_tb (CPUState *env)
112 {
113 /* TO FIX */
114 return 0;
115 }
116
117 uint32_t cpu_ppc_load_tbl (CPUState *env)
118 {
119 return cpu_ppc_get_tb(env) & 0xFFFFFFFF;
120 }
121
122 uint32_t cpu_ppc_load_tbu (CPUState *env)
123 {
124 return cpu_ppc_get_tb(env) >> 32;
125 }
126
127 static void cpu_ppc_store_tb (CPUState *env, uint64_t value)
128 {
129 /* TO FIX */
130 }
131
132 void cpu_ppc_store_tbu (CPUState *env, uint32_t value)
133 {
134 cpu_ppc_store_tb(env, ((uint64_t)value << 32) | cpu_ppc_load_tbl(env));
135 }
136
137 void cpu_ppc_store_tbl (CPUState *env, uint32_t value)
138 {
139 cpu_ppc_store_tb(env, ((uint64_t)cpu_ppc_load_tbl(env) << 32) | value);
140 }
141
142 void cpu_ppc601_store_rtcu (CPUState *env, uint32_t value)
143 {
144 cpu_ppc_store_tbu( env, value );
145 }
146
147 uint32_t cpu_ppc601_load_rtcu (CPUState *env)
148 {
149 cpu_ppc_load_tbu(env);
150 }
151
152 uint32_t cpu_ppc601_load_rtcl (CPUState *env)
153 {
154 return cpu_ppc_load_tbl(env) & 0x3FFFFF80;
155 }
156
157 /* XXX: to be fixed */
158 int ppc_dcr_read (ppc_dcr_t *dcr_env, int dcrn, target_ulong *valp)
159 {
160 return -1;
161 }
162
163 int ppc_dcr_write (ppc_dcr_t *dcr_env, int dcrn, target_ulong val)
164 {
165 return -1;
166 }
167
168 #define EXCP_DUMP(env, fmt, args...) \
169 do { \
170 fprintf(stderr, fmt , ##args); \
171 cpu_dump_state(env, stderr, fprintf, 0); \
172 if (loglevel != 0) { \
173 fprintf(logfile, fmt , ##args); \
174 cpu_dump_state(env, logfile, fprintf, 0); \
175 } \
176 } while (0)
177
178 void cpu_loop(CPUPPCState *env)
179 {
180 int trapnr;
181 uint32_t ret;
182 target_siginfo_t info;
183
184 for(;;) {
185 trapnr = cpu_ppc_exec(env);
186 switch(trapnr) {
187 case POWERPC_EXCP_NONE:
188 /* Just go on */
189 break;
190 case POWERPC_EXCP_CRITICAL: /* Critical input */
191 cpu_abort(env, "Critical interrupt while in user mode. "
192 "Aborting\n");
193 break;
194 case POWERPC_EXCP_MCHECK: /* Machine check exception */
195 cpu_abort(env, "Machine check exception while in user mode. "
196 "Aborting\n");
197 break;
198 case POWERPC_EXCP_DSI: /* Data storage exception */
199 #ifndef DAR
200 /* To deal with multiple qemu header version as host for the darwin-user code */
201 # define DAR SPR_DAR
202 #endif
203 EXCP_DUMP(env, "Invalid data memory access: 0x" ADDRX "\n",
204 env->spr[SPR_DAR]);
205 /* Handle this via the gdb */
206 gdb_handlesig (env, SIGSEGV);
207
208 info.si_addr = (void*)env->nip;
209 queue_signal(info.si_signo, &info);
210 break;
211 case POWERPC_EXCP_ISI: /* Instruction storage exception */
212 EXCP_DUMP(env, "Invalid instruction fetch: 0x\n" ADDRX "\n",
213 env->spr[SPR_DAR]);
214 /* Handle this via the gdb */
215 gdb_handlesig (env, SIGSEGV);
216
217 info.si_addr = (void*)(env->nip - 4);
218 queue_signal(info.si_signo, &info);
219 break;
220 case POWERPC_EXCP_EXTERNAL: /* External input */
221 cpu_abort(env, "External interrupt while in user mode. "
222 "Aborting\n");
223 break;
224 case POWERPC_EXCP_ALIGN: /* Alignment exception */
225 EXCP_DUMP(env, "Unaligned memory access\n");
226 info.si_errno = 0;
227 info.si_code = BUS_ADRALN;
228 info.si_addr = (void*)(env->nip - 4);
229 queue_signal(info.si_signo, &info);
230 break;
231 case POWERPC_EXCP_PROGRAM: /* Program exception */
232 /* XXX: check this */
233 switch (env->error_code & ~0xF) {
234 case POWERPC_EXCP_FP:
235 EXCP_DUMP(env, "Floating point program exception\n");
236 /* Set FX */
237 env->fpscr[7] |= 0x8;
238 /* Finally, update FEX */
239 if ((((env->fpscr[7] & 0x3) << 3) | (env->fpscr[6] >> 1)) &
240 ((env->fpscr[1] << 1) | (env->fpscr[0] >> 3)))
241 env->fpscr[7] |= 0x4;
242 info.si_signo = SIGFPE;
243 info.si_errno = 0;
244 switch (env->error_code & 0xF) {
245 case POWERPC_EXCP_FP_OX:
246 info.si_code = FPE_FLTOVF;
247 break;
248 case POWERPC_EXCP_FP_UX:
249 info.si_code = FPE_FLTUND;
250 break;
251 case POWERPC_EXCP_FP_ZX:
252 case POWERPC_EXCP_FP_VXZDZ:
253 info.si_code = FPE_FLTDIV;
254 break;
255 case POWERPC_EXCP_FP_XX:
256 info.si_code = FPE_FLTRES;
257 break;
258 case POWERPC_EXCP_FP_VXSOFT:
259 info.si_code = FPE_FLTINV;
260 break;
261 case POWERPC_EXCP_FP_VXNAN:
262 case POWERPC_EXCP_FP_VXISI:
263 case POWERPC_EXCP_FP_VXIDI:
264 case POWERPC_EXCP_FP_VXIMZ:
265 case POWERPC_EXCP_FP_VXVC:
266 case POWERPC_EXCP_FP_VXSQRT:
267 case POWERPC_EXCP_FP_VXCVI:
268 info.si_code = FPE_FLTSUB;
269 break;
270 default:
271 EXCP_DUMP(env, "Unknown floating point exception (%02x)\n",
272 env->error_code);
273 break;
274 }
275 break;
276 case POWERPC_EXCP_INVAL:
277 EXCP_DUMP(env, "Invalid instruction\n");
278 info.si_signo = SIGILL;
279 info.si_errno = 0;
280 switch (env->error_code & 0xF) {
281 case POWERPC_EXCP_INVAL_INVAL:
282 info.si_code = ILL_ILLOPC;
283 break;
284 case POWERPC_EXCP_INVAL_LSWX:
285 info.si_code = ILL_ILLOPN;
286 break;
287 case POWERPC_EXCP_INVAL_SPR:
288 info.si_code = ILL_PRVREG;
289 break;
290 case POWERPC_EXCP_INVAL_FP:
291 info.si_code = ILL_COPROC;
292 break;
293 default:
294 EXCP_DUMP(env, "Unknown invalid operation (%02x)\n",
295 env->error_code & 0xF);
296 info.si_code = ILL_ILLADR;
297 break;
298 }
299 /* Handle this via the gdb */
300 gdb_handlesig (env, SIGSEGV);
301 break;
302 case POWERPC_EXCP_PRIV:
303 EXCP_DUMP(env, "Privilege violation\n");
304 info.si_signo = SIGILL;
305 info.si_errno = 0;
306 switch (env->error_code & 0xF) {
307 case POWERPC_EXCP_PRIV_OPC:
308 info.si_code = ILL_PRVOPC;
309 break;
310 case POWERPC_EXCP_PRIV_REG:
311 info.si_code = ILL_PRVREG;
312 break;
313 default:
314 EXCP_DUMP(env, "Unknown privilege violation (%02x)\n",
315 env->error_code & 0xF);
316 info.si_code = ILL_PRVOPC;
317 break;
318 }
319 break;
320 case POWERPC_EXCP_TRAP:
321 cpu_abort(env, "Tried to call a TRAP\n");
322 break;
323 default:
324 /* Should not happen ! */
325 cpu_abort(env, "Unknown program exception (%02x)\n",
326 env->error_code);
327 break;
328 }
329 info.si_addr = (void*)(env->nip - 4);
330 queue_signal(info.si_signo, &info);
331 break;
332 case POWERPC_EXCP_FPU: /* Floating-point unavailable exception */
333 EXCP_DUMP(env, "No floating point allowed\n");
334 info.si_signo = SIGILL;
335 info.si_errno = 0;
336 info.si_code = ILL_COPROC;
337 info.si_addr = (void*)(env->nip - 4);
338 queue_signal(info.si_signo, &info);
339 break;
340 case POWERPC_EXCP_SYSCALL: /* System call exception */
341 cpu_abort(env, "Syscall exception while in user mode. "
342 "Aborting\n");
343 break;
344 case POWERPC_EXCP_APU: /* Auxiliary processor unavailable */
345 EXCP_DUMP(env, "No APU instruction allowed\n");
346 info.si_signo = SIGILL;
347 info.si_errno = 0;
348 info.si_code = ILL_COPROC;
349 info.si_addr = (void*)(env->nip - 4);
350 queue_signal(info.si_signo, &info);
351 break;
352 case POWERPC_EXCP_DECR: /* Decrementer exception */
353 cpu_abort(env, "Decrementer interrupt while in user mode. "
354 "Aborting\n");
355 break;
356 case POWERPC_EXCP_FIT: /* Fixed-interval timer interrupt */
357 cpu_abort(env, "Fix interval timer interrupt while in user mode. "
358 "Aborting\n");
359 break;
360 case POWERPC_EXCP_WDT: /* Watchdog timer interrupt */
361 cpu_abort(env, "Watchdog timer interrupt while in user mode. "
362 "Aborting\n");
363 break;
364 case POWERPC_EXCP_DTLB: /* Data TLB error */
365 cpu_abort(env, "Data TLB exception while in user mode. "
366 "Aborting\n");
367 break;
368 case POWERPC_EXCP_ITLB: /* Instruction TLB error */
369 cpu_abort(env, "Instruction TLB exception while in user mode. "
370 "Aborting\n");
371 break;
372 case POWERPC_EXCP_DEBUG: /* Debug interrupt */
373 gdb_handlesig (env, SIGTRAP);
374 break;
375 #if defined(TARGET_PPCEMB)
376 case POWERPC_EXCP_SPEU: /* SPE/embedded floating-point unavail. */
377 EXCP_DUMP(env, "No SPE/floating-point instruction allowed\n");
378 info.si_signo = SIGILL;
379 info.si_errno = 0;
380 info.si_code = ILL_COPROC;
381 info.si_addr = (void*)(env->nip - 4);
382 queue_signal(info.si_signo, &info);
383 break;
384 case POWERPC_EXCP_EFPDI: /* Embedded floating-point data IRQ */
385 cpu_abort(env, "Embedded floating-point data IRQ not handled\n");
386 break;
387 case POWERPC_EXCP_EFPRI: /* Embedded floating-point round IRQ */
388 cpu_abort(env, "Embedded floating-point round IRQ not handled\n");
389 break;
390 case POWERPC_EXCP_EPERFM: /* Embedded performance monitor IRQ */
391 cpu_abort(env, "Performance monitor exception not handled\n");
392 break;
393 case POWERPC_EXCP_DOORI: /* Embedded doorbell interrupt */
394 cpu_abort(env, "Doorbell interrupt while in user mode. "
395 "Aborting\n");
396 break;
397 case POWERPC_EXCP_DOORCI: /* Embedded doorbell critical interrupt */
398 cpu_abort(env, "Doorbell critical interrupt while in user mode. "
399 "Aborting\n");
400 break;
401 #endif /* defined(TARGET_PPCEMB) */
402 case POWERPC_EXCP_RESET: /* System reset exception */
403 cpu_abort(env, "Reset interrupt while in user mode. "
404 "Aborting\n");
405 break;
406 #if defined(TARGET_PPC64) /* PowerPC 64 */
407 case POWERPC_EXCP_DSEG: /* Data segment exception */
408 cpu_abort(env, "Data segment exception while in user mode. "
409 "Aborting\n");
410 break;
411 case POWERPC_EXCP_ISEG: /* Instruction segment exception */
412 cpu_abort(env, "Instruction segment exception "
413 "while in user mode. Aborting\n");
414 break;
415 #endif /* defined(TARGET_PPC64) */
416 #if defined(TARGET_PPC64H) /* PowerPC 64 with hypervisor mode support */
417 case POWERPC_EXCP_HDECR: /* Hypervisor decrementer exception */
418 cpu_abort(env, "Hypervisor decrementer interrupt "
419 "while in user mode. Aborting\n");
420 break;
421 #endif /* defined(TARGET_PPC64H) */
422 case POWERPC_EXCP_TRACE: /* Trace exception */
423 /* Nothing to do:
424 * we use this exception to emulate step-by-step execution mode.
425 */
426 break;
427 #if defined(TARGET_PPC64H) /* PowerPC 64 with hypervisor mode support */
428 case POWERPC_EXCP_HDSI: /* Hypervisor data storage exception */
429 cpu_abort(env, "Hypervisor data storage exception "
430 "while in user mode. Aborting\n");
431 break;
432 case POWERPC_EXCP_HISI: /* Hypervisor instruction storage excp */
433 cpu_abort(env, "Hypervisor instruction storage exception "
434 "while in user mode. Aborting\n");
435 break;
436 case POWERPC_EXCP_HDSEG: /* Hypervisor data segment exception */
437 cpu_abort(env, "Hypervisor data segment exception "
438 "while in user mode. Aborting\n");
439 break;
440 case POWERPC_EXCP_HISEG: /* Hypervisor instruction segment excp */
441 cpu_abort(env, "Hypervisor instruction segment exception "
442 "while in user mode. Aborting\n");
443 break;
444 #endif /* defined(TARGET_PPC64H) */
445 case POWERPC_EXCP_VPU: /* Vector unavailable exception */
446 EXCP_DUMP(env, "No Altivec instructions allowed\n");
447 info.si_signo = SIGILL;
448 info.si_errno = 0;
449 info.si_code = ILL_COPROC;
450 info.si_addr = (void*)(env->nip - 4);
451 queue_signal(info.si_signo, &info);
452 break;
453 case POWERPC_EXCP_PIT: /* Programmable interval timer IRQ */
454 cpu_abort(env, "Programable interval timer interrupt "
455 "while in user mode. Aborting\n");
456 break;
457 case POWERPC_EXCP_IO: /* IO error exception */
458 cpu_abort(env, "IO error exception while in user mode. "
459 "Aborting\n");
460 break;
461 case POWERPC_EXCP_RUNM: /* Run mode exception */
462 cpu_abort(env, "Run mode exception while in user mode. "
463 "Aborting\n");
464 break;
465 case POWERPC_EXCP_EMUL: /* Emulation trap exception */
466 cpu_abort(env, "Emulation trap exception not handled\n");
467 break;
468 case POWERPC_EXCP_IFTLB: /* Instruction fetch TLB error */
469 cpu_abort(env, "Instruction fetch TLB exception "
470 "while in user-mode. Aborting");
471 break;
472 case POWERPC_EXCP_DLTLB: /* Data load TLB miss */
473 cpu_abort(env, "Data load TLB exception while in user-mode. "
474 "Aborting");
475 break;
476 case POWERPC_EXCP_DSTLB: /* Data store TLB miss */
477 cpu_abort(env, "Data store TLB exception while in user-mode. "
478 "Aborting");
479 break;
480 case POWERPC_EXCP_FPA: /* Floating-point assist exception */
481 cpu_abort(env, "Floating-point assist exception not handled\n");
482 break;
483 case POWERPC_EXCP_IABR: /* Instruction address breakpoint */
484 cpu_abort(env, "Instruction address breakpoint exception "
485 "not handled\n");
486 break;
487 case POWERPC_EXCP_SMI: /* System management interrupt */
488 cpu_abort(env, "System management interrupt while in user mode. "
489 "Aborting\n");
490 break;
491 case POWERPC_EXCP_THERM: /* Thermal interrupt */
492 cpu_abort(env, "Thermal interrupt interrupt while in user mode. "
493 "Aborting\n");
494 break;
495 case POWERPC_EXCP_PERFM: /* Embedded performance monitor IRQ */
496 cpu_abort(env, "Performance monitor exception not handled\n");
497 break;
498 case POWERPC_EXCP_VPUA: /* Vector assist exception */
499 cpu_abort(env, "Vector assist exception not handled\n");
500 break;
501 case POWERPC_EXCP_SOFTP: /* Soft patch exception */
502 cpu_abort(env, "Soft patch exception not handled\n");
503 break;
504 case POWERPC_EXCP_MAINT: /* Maintenance exception */
505 cpu_abort(env, "Maintenance exception while in user mode. "
506 "Aborting\n");
507 break;
508 case POWERPC_EXCP_STOP: /* stop translation */
509 /* We did invalidate the instruction cache. Go on */
510 break;
511 case POWERPC_EXCP_BRANCH: /* branch instruction: */
512 /* We just stopped because of a branch. Go on */
513 break;
514 case POWERPC_EXCP_SYSCALL_USER:
515 /* system call in user-mode emulation */
516 /* system call */
517 if(((int)env->gpr[0]) <= SYS_MAXSYSCALL && ((int)env->gpr[0])>0)
518 ret = do_unix_syscall(env, env->gpr[0]/*, env->gpr[3], env->gpr[4],
519 env->gpr[5], env->gpr[6], env->gpr[7],
520 env->gpr[8], env->gpr[9], env->gpr[10]*/);
521 else if(((int)env->gpr[0])<0)
522 ret = do_mach_syscall(env, env->gpr[0], env->gpr[3], env->gpr[4],
523 env->gpr[5], env->gpr[6], env->gpr[7],
524 env->gpr[8], env->gpr[9], env->gpr[10]);
525 else
526 ret = do_thread_syscall(env, env->gpr[0], env->gpr[3], env->gpr[4],
527 env->gpr[5], env->gpr[6], env->gpr[7],
528 env->gpr[8], env->gpr[9], env->gpr[10]);
529
530 /* Unix syscall error signaling */
531 if(((int)env->gpr[0]) <= SYS_MAXSYSCALL && ((int)env->gpr[0])>0)
532 {
533 if( (int)ret < 0 )
534 env->nip += 0;
535 else
536 env->nip += 4;
537 }
538
539 /* Return value */
540 env->gpr[3] = ret;
541 break;
542 default:
543 cpu_abort(env, "Unknown exception 0x%d. Aborting\n", trapnr);
544 break;
545 }
546 process_pending_signals(env);
547 }
548 }
549 #endif
550
551
552 #ifdef TARGET_I386
553
554 /***********************************************************/
555 /* CPUX86 core interface */
556
557 uint64_t cpu_get_tsc(CPUX86State *env)
558 {
559 return cpu_get_real_ticks();
560 }
561
562 void
563 write_dt(void *ptr, unsigned long addr, unsigned long limit,
564 int flags)
565 {
566 unsigned int e1, e2;
567 e1 = (addr << 16) | (limit & 0xffff);
568 e2 = ((addr >> 16) & 0xff) | (addr & 0xff000000) | (limit & 0x000f0000);
569 e2 |= flags;
570 stl((uint8_t *)ptr, e1);
571 stl((uint8_t *)ptr + 4, e2);
572 }
573
574 static void set_gate(void *ptr, unsigned int type, unsigned int dpl,
575 unsigned long addr, unsigned int sel)
576 {
577 unsigned int e1, e2;
578 e1 = (addr & 0xffff) | (sel << 16);
579 e2 = (addr & 0xffff0000) | 0x8000 | (dpl << 13) | (type << 8);
580 stl((uint8_t *)ptr, e1);
581 stl((uint8_t *)ptr + 4, e2);
582 }
583
584 #define GDT_TABLE_SIZE 14
585 #define LDT_TABLE_SIZE 15
586 #define IDT_TABLE_SIZE 256
587 #define TSS_SIZE 104
588 uint64_t gdt_table[GDT_TABLE_SIZE];
589 uint64_t ldt_table[LDT_TABLE_SIZE];
590 uint64_t idt_table[IDT_TABLE_SIZE];
591 uint32_t tss[TSS_SIZE];
592
593 /* only dpl matters as we do only user space emulation */
594 static void set_idt(int n, unsigned int dpl)
595 {
596 set_gate(idt_table + n, 0, dpl, 0, 0);
597 }
598
599 /* ABI convention: after a syscall if there was an error the CF flag is set */
600 static inline void set_error(CPUX86State *env, int ret)
601 {
602 if(ret<0)
603 env->eflags = env->eflags | 0x1;
604 else
605 env->eflags &= ~0x1;
606 env->regs[R_EAX] = ret;
607 }
608
609 void cpu_loop(CPUX86State *env)
610 {
611 int trapnr;
612 int ret;
613 uint8_t *pc;
614 target_siginfo_t info;
615
616 for(;;) {
617 trapnr = cpu_x86_exec(env);
618 uint32_t *params = (uint32_t *)env->regs[R_ESP];
619 switch(trapnr) {
620 case 0x79: /* Our commpage hack back door exit is here */
621 do_commpage(env, env->eip, *(params + 1), *(params + 2),
622 *(params + 3), *(params + 4),
623 *(params + 5), *(params + 6),
624 *(params + 7), *(params + 8));
625 break;
626 case 0x81: /* mach syscall */
627 {
628 ret = do_mach_syscall(env, env->regs[R_EAX],
629 *(params + 1), *(params + 2),
630 *(params + 3), *(params + 4),
631 *(params + 5), *(params + 6),
632 *(params + 7), *(params + 8));
633 set_error(env, ret);
634 break;
635 }
636 case 0x90: /* unix backdoor */
637 {
638 /* after sysenter, stack is in R_ECX, new eip in R_EDX (sysexit will flip them back)*/
639 int saved_stack = env->regs[R_ESP];
640 env->regs[R_ESP] = env->regs[R_ECX];
641
642 ret = do_unix_syscall(env, env->regs[R_EAX]);
643
644 env->regs[R_ECX] = env->regs[R_ESP];
645 env->regs[R_ESP] = saved_stack;
646
647 set_error(env, ret);
648 break;
649 }
650 case 0x80: /* unix syscall */
651 {
652 ret = do_unix_syscall(env, env->regs[R_EAX]/*,
653 *(params + 1), *(params + 2),
654 *(params + 3), *(params + 4),
655 *(params + 5), *(params + 6),
656 *(params + 7), *(params + 8)*/);
657 set_error(env, ret);
658 break;
659 }
660 case 0x82: /* thread syscall */
661 {
662 ret = do_thread_syscall(env, env->regs[R_EAX],
663 *(params + 1), *(params + 2),
664 *(params + 3), *(params + 4),
665 *(params + 5), *(params + 6),
666 *(params + 7), *(params + 8));
667 set_error(env, ret);
668 break;
669 }
670 case EXCP0B_NOSEG:
671 case EXCP0C_STACK:
672 info.si_signo = SIGBUS;
673 info.si_errno = 0;
674 info.si_code = BUS_NOOP;
675 info.si_addr = 0;
676 gdb_handlesig (env, SIGBUS);
677 queue_signal(info.si_signo, &info);
678 break;
679 case EXCP0D_GPF:
680 info.si_signo = SIGSEGV;
681 info.si_errno = 0;
682 info.si_code = SEGV_NOOP;
683 info.si_addr = 0;
684 gdb_handlesig (env, SIGSEGV);
685 queue_signal(info.si_signo, &info);
686 break;
687 case EXCP0E_PAGE:
688 info.si_signo = SIGSEGV;
689 info.si_errno = 0;
690 if (!(env->error_code & 1))
691 info.si_code = SEGV_MAPERR;
692 else
693 info.si_code = SEGV_ACCERR;
694 info.si_addr = (void*)env->cr[2];
695 gdb_handlesig (env, SIGSEGV);
696 queue_signal(info.si_signo, &info);
697 break;
698 case EXCP00_DIVZ:
699 /* division by zero */
700 info.si_signo = SIGFPE;
701 info.si_errno = 0;
702 info.si_code = FPE_INTDIV;
703 info.si_addr = (void*)env->eip;
704 gdb_handlesig (env, SIGFPE);
705 queue_signal(info.si_signo, &info);
706 break;
707 case EXCP01_SSTP:
708 case EXCP03_INT3:
709 info.si_signo = SIGTRAP;
710 info.si_errno = 0;
711 info.si_code = TRAP_BRKPT;
712 info.si_addr = (void*)env->eip;
713 gdb_handlesig (env, SIGTRAP);
714 queue_signal(info.si_signo, &info);
715 break;
716 case EXCP04_INTO:
717 case EXCP05_BOUND:
718 info.si_signo = SIGSEGV;
719 info.si_errno = 0;
720 info.si_code = SEGV_NOOP;
721 info.si_addr = 0;
722 gdb_handlesig (env, SIGSEGV);
723 queue_signal(info.si_signo, &info);
724 break;
725 case EXCP06_ILLOP:
726 info.si_signo = SIGILL;
727 info.si_errno = 0;
728 info.si_code = ILL_ILLOPN;
729 info.si_addr = (void*)env->eip;
730 gdb_handlesig (env, SIGILL);
731 queue_signal(info.si_signo, &info);
732 break;
733 case EXCP_INTERRUPT:
734 /* just indicate that signals should be handled asap */
735 break;
736 case EXCP_DEBUG:
737 {
738 int sig;
739
740 sig = gdb_handlesig (env, SIGTRAP);
741 if (sig)
742 {
743 info.si_signo = sig;
744 info.si_errno = 0;
745 info.si_code = TRAP_BRKPT;
746 queue_signal(info.si_signo, &info);
747 }
748 }
749 break;
750 default:
751 pc = (void*)(env->segs[R_CS].base + env->eip);
752 fprintf(stderr, "qemu: 0x%08lx: unhandled CPU exception 0x%x - aborting\n",
753 (long)pc, trapnr);
754 abort();
755 }
756 process_pending_signals(env);
757 }
758 }
759 #endif
760
761 void usage(void)
762 {
763 printf("qemu-" TARGET_ARCH " version " QEMU_VERSION ", Copyright (c) 2003-2004 Fabrice Bellard\n"
764 "usage: qemu-" TARGET_ARCH " [-h] [-d opts] [-L path] [-s size] program [arguments...]\n"
765 "Darwin CPU emulator (compiled for %s emulation)\n"
766 "\n"
767 "-h print this help\n"
768 "-L path set the %s library path (default='%s')\n"
769 "-s size set the stack size in bytes (default=%ld)\n"
770 "\n"
771 "debug options:\n"
772 #ifdef USE_CODE_COPY
773 "-no-code-copy disable code copy acceleration\n"
774 #endif
775 "-d options activate log (logfile='%s')\n"
776 "-g wait for gdb on port 1234\n"
777 "-p pagesize set the host page size to 'pagesize'\n",
778 TARGET_ARCH,
779 TARGET_ARCH,
780 interp_prefix,
781 stack_size,
782 DEBUG_LOGFILE);
783 _exit(1);
784 }
785
786 /* XXX: currently only used for async signals (see signal.c) */
787 CPUState *global_env;
788 /* used only if single thread */
789 CPUState *cpu_single_env = NULL;
790
791 /* used to free thread contexts */
792 TaskState *first_task_state;
793
794 int main(int argc, char **argv)
795 {
796 const char *filename;
797 struct target_pt_regs regs1, *regs = &regs1;
798 TaskState ts1, *ts = &ts1;
799 CPUState *env;
800 int optind;
801 short use_gdbstub = 0;
802 const char *r;
803
804 if (argc <= 1)
805 usage();
806
807 /* init debug */
808 cpu_set_log_filename(DEBUG_LOGFILE);
809
810 optind = 1;
811 for(;;) {
812 if (optind >= argc)
813 break;
814 r = argv[optind];
815 if (r[0] != '-')
816 break;
817 optind++;
818 r++;
819 if (!strcmp(r, "-")) {
820 break;
821 } else if (!strcmp(r, "d")) {
822 int mask;
823 CPULogItem *item;
824
825 if (optind >= argc)
826 break;
827
828 r = argv[optind++];
829 mask = cpu_str_to_log_mask(r);
830 if (!mask) {
831 printf("Log items (comma separated):\n");
832 for(item = cpu_log_items; item->mask != 0; item++) {
833 printf("%-10s %s\n", item->name, item->help);
834 }
835 exit(1);
836 }
837 cpu_set_log(mask);
838 } else if (!strcmp(r, "s")) {
839 r = argv[optind++];
840 stack_size = strtol(r, (char **)&r, 0);
841 if (stack_size <= 0)
842 usage();
843 if (*r == 'M')
844 stack_size *= 1024 * 1024;
845 else if (*r == 'k' || *r == 'K')
846 stack_size *= 1024;
847 } else if (!strcmp(r, "L")) {
848 interp_prefix = argv[optind++];
849 } else if (!strcmp(r, "p")) {
850 qemu_host_page_size = atoi(argv[optind++]);
851 if (qemu_host_page_size == 0 ||
852 (qemu_host_page_size & (qemu_host_page_size - 1)) != 0) {
853 fprintf(stderr, "page size must be a power of two\n");
854 exit(1);
855 }
856 } else
857 if (!strcmp(r, "g")) {
858 use_gdbstub = 1;
859 } else
860 #ifdef USE_CODE_COPY
861 if (!strcmp(r, "no-code-copy")) {
862 code_copy_enabled = 0;
863 } else
864 #endif
865 {
866 usage();
867 }
868 }
869 if (optind >= argc)
870 usage();
871 filename = argv[optind];
872
873 /* Zero out regs */
874 memset(regs, 0, sizeof(struct target_pt_regs));
875
876 /* NOTE: we need to init the CPU at this stage to get
877 qemu_host_page_size */
878 env = cpu_init();
879
880 printf("Starting %s with qemu\n----------------\n", filename);
881
882 commpage_init();
883
884 if (mach_exec(filename, argv+optind, environ, regs) != 0) {
885 printf("Error loading %s\n", filename);
886 _exit(1);
887 }
888
889 syscall_init();
890 signal_init();
891 global_env = env;
892
893 /* build Task State */
894 memset(ts, 0, sizeof(TaskState));
895 env->opaque = ts;
896 ts->used = 1;
897 env->user_mode_only = 1;
898
899 #if defined(TARGET_I386)
900 cpu_x86_set_cpl(env, 3);
901
902 env->cr[0] = CR0_PG_MASK | CR0_WP_MASK | CR0_PE_MASK;
903 env->hflags |= HF_PE_MASK;
904
905 if (env->cpuid_features & CPUID_SSE) {
906 env->cr[4] |= CR4_OSFXSR_MASK;
907 env->hflags |= HF_OSFXSR_MASK;
908 }
909
910 /* flags setup : we activate the IRQs by default as in user mode */
911 env->eflags |= IF_MASK;
912
913 /* darwin register setup */
914 env->regs[R_EAX] = regs->eax;
915 env->regs[R_EBX] = regs->ebx;
916 env->regs[R_ECX] = regs->ecx;
917 env->regs[R_EDX] = regs->edx;
918 env->regs[R_ESI] = regs->esi;
919 env->regs[R_EDI] = regs->edi;
920 env->regs[R_EBP] = regs->ebp;
921 env->regs[R_ESP] = regs->esp;
922 env->eip = regs->eip;
923
924 /* Darwin LDT setup */
925 /* 2 - User code segment
926 3 - User data segment
927 4 - User cthread */
928 bzero(ldt_table, LDT_TABLE_SIZE * sizeof(ldt_table[0]));
929 env->ldt.base = (uint32_t) ldt_table;
930 env->ldt.limit = sizeof(ldt_table) - 1;
931
932 write_dt(ldt_table + 2, 0, 0xfffff,
933 DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | DESC_S_MASK |
934 (3 << DESC_DPL_SHIFT) | (0xa << DESC_TYPE_SHIFT));
935 write_dt(ldt_table + 3, 0, 0xfffff,
936 DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | DESC_S_MASK |
937 (3 << DESC_DPL_SHIFT) | (0x2 << DESC_TYPE_SHIFT));
938 write_dt(ldt_table + 4, 0, 0xfffff,
939 DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | DESC_S_MASK |
940 (3 << DESC_DPL_SHIFT) | (0x2 << DESC_TYPE_SHIFT));
941
942 /* Darwin GDT setup.
943 * has changed a lot between old Darwin/x86 (pre-Mac Intel) and Mac OS X/x86,
944 now everything is done via int 0x81(mach) int 0x82 (thread) and sysenter/sysexit(unix) */
945 bzero(gdt_table, sizeof(gdt_table));
946 env->gdt.base = (uint32_t)gdt_table;
947 env->gdt.limit = sizeof(gdt_table) - 1;
948
949 /* Set up a back door to handle sysenter syscalls (unix) */
950 char * syscallbackdoor = malloc(64);
951 page_set_flags((int)syscallbackdoor, (int)syscallbackdoor + 64, PROT_EXEC | PROT_READ | PAGE_VALID);
952
953 int i = 0;
954 syscallbackdoor[i++] = 0xcd;
955 syscallbackdoor[i++] = 0x90; /* int 0x90 */
956 syscallbackdoor[i++] = 0x0F;
957 syscallbackdoor[i++] = 0x35; /* sysexit */
958
959 /* Darwin sysenter/sysexit setup */
960 env->sysenter_cs = 0x1; //XXX
961 env->sysenter_eip = (int)syscallbackdoor;
962 env->sysenter_esp = (int)malloc(64);
963
964 /* Darwin TSS setup
965 This must match up with GDT[4] */
966 env->tr.base = (uint32_t) tss;
967 env->tr.limit = sizeof(tss) - 1;
968 env->tr.flags = DESC_P_MASK | (0x9 << DESC_TYPE_SHIFT);
969 stw(tss + 2, 0x10); // ss0 = 0x10 = GDT[2] = Kernel Data Segment
970
971 /* Darwin interrupt setup */
972 bzero(idt_table, sizeof(idt_table));
973 env->idt.base = (uint32_t) idt_table;
974 env->idt.limit = sizeof(idt_table) - 1;
975 set_idt(0, 0);
976 set_idt(1, 0);
977 set_idt(2, 0);
978 set_idt(3, 3);
979 set_idt(4, 3);
980 set_idt(5, 3);
981 set_idt(6, 0);
982 set_idt(7, 0);
983 set_idt(8, 0);
984 set_idt(9, 0);
985 set_idt(10, 0);
986 set_idt(11, 0);
987 set_idt(12, 0);
988 set_idt(13, 0);
989 set_idt(14, 0);
990 set_idt(15, 0);
991 set_idt(16, 0);
992 set_idt(17, 0);
993 set_idt(18, 0);
994 set_idt(19, 0);
995 /* Syscalls are done via
996 int 0x80 (unix) (rarely used)
997 int 0x81 (mach)
998 int 0x82 (thread)
999 int 0x83 (diag) (not handled here)
1000 sysenter/sysexit (unix) -> we redirect that to int 0x90 */
1001 set_idt(0x79, 3); /* Commpage hack, here is our backdoor interrupt */
1002 set_idt(0x80, 3); /* Unix Syscall */
1003 set_idt(0x81, 3); /* Mach Syscalls */
1004 set_idt(0x82, 3); /* thread Syscalls */
1005
1006 set_idt(0x90, 3); /* qemu-darwin-user's Unix syscalls backdoor */
1007
1008
1009 cpu_x86_load_seg(env, R_CS, __USER_CS);
1010 cpu_x86_load_seg(env, R_DS, __USER_DS);
1011 cpu_x86_load_seg(env, R_ES, __USER_DS);
1012 cpu_x86_load_seg(env, R_SS, __USER_DS);
1013 cpu_x86_load_seg(env, R_FS, __USER_DS);
1014 cpu_x86_load_seg(env, R_GS, __USER_DS);
1015
1016 #elif defined(TARGET_PPC)
1017 {
1018 int i;
1019 env->nip = regs->nip;
1020 for(i = 0; i < 32; i++) {
1021 env->gpr[i] = regs->gpr[i];
1022 }
1023 }
1024 #else
1025 #error unsupported target CPU
1026 #endif
1027
1028 if (use_gdbstub) {
1029 printf("Waiting for gdb Connection on port 1234...\n");
1030 gdbserver_start (1234);
1031 gdb_handlesig(env, 0);
1032 }
1033
1034 cpu_loop(env);
1035 /* never exits */
1036 return 0;
1037 }