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