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