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