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