Statistics
| Branch: | Revision:

root / darwin-user / main.c @ a062e36c

History | View | Annotate | Download (35.5 kB)

1
/*
2
 *  qemu user main
3
 *
4
 *  Copyright (c) 2003 Fabrice Bellard
5
 *  Copyright (c) 2006 Pierre d'Herbemont
6
 *
7
 *  This program is free software; you can redistribute it and/or modify
8
 *  it under the terms of the GNU General Public License as published by
9
 *  the Free Software Foundation; either version 2 of the License, or
10
 *  (at your option) any later version.
11
 *
12
 *  This program is distributed in the hope that it will be useful,
13
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 *  GNU General Public License for more details.
16
 *
17
 *  You should have received a copy of the GNU General Public License
18
 *  along with this program; if not, write to the Free Software
19
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20
 */
21
#include <stdlib.h>
22
#include <stdio.h>
23
#include <stdarg.h>
24
#include <string.h>
25
#include <errno.h>
26
#include <unistd.h>
27

    
28
#include <sys/syscall.h>
29
#include <sys/mman.h>
30

    
31
#include "qemu.h"
32

    
33
#define DEBUG_LOGFILE "/tmp/qemu.log"
34

    
35
#ifdef __APPLE__
36
#include <crt_externs.h>
37
# define environ  (*_NSGetEnviron())
38
#endif
39

    
40
#include <mach/mach_init.h>
41
#include <mach/vm_map.h>
42

    
43
const char *interp_prefix = "";
44

    
45
asm(".zerofill __STD_PROG_ZONE, __STD_PROG_ZONE, __std_prog_zone, 0x0dfff000");
46

    
47
/* XXX: on x86 MAP_GROWSDOWN only works if ESP <= address + 32, so
48
   we allocate a bigger stack. Need a better solution, for example
49
   by remapping the process stack directly at the right place */
50
unsigned long stack_size = 512 * 1024;
51

    
52
void qerror(const char *fmt, ...)
53
{
54
    va_list ap;
55

    
56
    va_start(ap, fmt);
57
    vfprintf(stderr, fmt, ap);
58
    va_end(ap);
59
    fprintf(stderr, "\n");
60
    exit(1);
61
}
62

    
63
void gemu_log(const char *fmt, ...)
64
{
65
    va_list ap;
66

    
67
    va_start(ap, fmt);
68
    vfprintf(stderr, fmt, ap);
69
    va_end(ap);
70
}
71

    
72
void cpu_outb(CPUState *env, int addr, int val)
73
{
74
    fprintf(stderr, "outb: port=0x%04x, data=%02x\n", addr, val);
75
}
76

    
77
void cpu_outw(CPUState *env, int addr, int val)
78
{
79
    fprintf(stderr, "outw: port=0x%04x, data=%04x\n", addr, val);
80
}
81

    
82
void cpu_outl(CPUState *env, int addr, int val)
83
{
84
    fprintf(stderr, "outl: port=0x%04x, data=%08x\n", addr, val);
85
}
86

    
87
int cpu_inb(CPUState *env, int addr)
88
{
89
    fprintf(stderr, "inb: port=0x%04x\n", addr);
90
    return 0;
91
}
92

    
93
int cpu_inw(CPUState *env, int addr)
94
{
95
    fprintf(stderr, "inw: port=0x%04x\n", addr);
96
    return 0;
97
}
98

    
99
int cpu_inl(CPUState *env, int addr)
100
{
101
    fprintf(stderr, "inl: port=0x%04x\n", addr);
102
    return 0;
103
}
104

    
105
int cpu_get_pic_interrupt(CPUState *env)
106
{
107
    return -1;
108
}
109
#ifdef TARGET_PPC
110

    
111
static inline uint64_t cpu_ppc_get_tb (CPUState *env)
112
{
113
    /* TO FIX */
114
    return 0;
115
}
116

    
117
uint32_t cpu_ppc_load_tbl (CPUState *env)
118
{
119
    return cpu_ppc_get_tb(env) & 0xFFFFFFFF;
120
}
121

    
122
uint32_t cpu_ppc_load_tbu (CPUState *env)
123
{
124
    return cpu_ppc_get_tb(env) >> 32;
125
}
126

    
127
uint32_t cpu_ppc_load_atbl (CPUState *env)
128
{
129
    return cpu_ppc_get_tb(env) & 0xFFFFFFFF;
130
}
131

    
132
uint32_t cpu_ppc_load_atbu (CPUState *env)
133
{
134
    return cpu_ppc_get_tb(env) >> 32;
135
}
136

    
137
uint32_t cpu_ppc601_load_rtcu (CPUState *env)
138
{
139
    cpu_ppc_load_tbu(env);
140
}
141

    
142
uint32_t cpu_ppc601_load_rtcl (CPUState *env)
143
{
144
    return cpu_ppc_load_tbl(env) & 0x3FFFFF80;
145
}
146

    
147
/* XXX: to be fixed */
148
int ppc_dcr_read (ppc_dcr_t *dcr_env, int dcrn, target_ulong *valp)
149
{
150
    return -1;
151
}
152

    
153
int 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...)                                         \
159
do {                                                                          \
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

    
168
void 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);
176
        switch(trapnr) {
177
        case POWERPC_EXCP_NONE:
178
            /* Just go on */
179
            break;
180
        case POWERPC_EXCP_CRITICAL: /* Critical input                        */
181
            cpu_abort(env, "Critical interrupt while in user mode. "
182
                      "Aborting\n");
183
            break;
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                */
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
193
            EXCP_DUMP(env, "Invalid data memory access: 0x" ADDRX "\n",
194
                      env->spr[SPR_DAR]);
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;
201
        case POWERPC_EXCP_ISI:      /* Instruction storage exception         */
202
            EXCP_DUMP(env, "Invalid instruction fetch: 0x\n" ADDRX "\n",
203
                      env->spr[SPR_DAR]);
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;
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");
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;
221
        case POWERPC_EXCP_PROGRAM:  /* Program exception                     */
222
            /* XXX: check this */
223
            switch (env->error_code & ~0xF) {
224
            case POWERPC_EXCP_FP:
225
                EXCP_DUMP(env, "Floating point program exception\n");
226
                /* Set FX */
227
                env->fpscr[7] |= 0x8;
228
                /* Finally, update FEX */
229
                if ((((env->fpscr[7] & 0x3) << 3) | (env->fpscr[6] >> 1)) &
230
                    ((env->fpscr[1] << 1) | (env->fpscr[0] >> 3)))
231
                    env->fpscr[7] |= 0x4;
232
                info.si_signo = SIGFPE;
233
                info.si_errno = 0;
234
                switch (env->error_code & 0xF) {
235
                case POWERPC_EXCP_FP_OX:
236
                    info.si_code = FPE_FLTOVF;
237
                    break;
238
                case POWERPC_EXCP_FP_UX:
239
                    info.si_code = FPE_FLTUND;
240
                    break;
241
                case POWERPC_EXCP_FP_ZX:
242
                case POWERPC_EXCP_FP_VXZDZ:
243
                    info.si_code = FPE_FLTDIV;
244
                    break;
245
                case POWERPC_EXCP_FP_XX:
246
                    info.si_code = FPE_FLTRES;
247
                    break;
248
                case POWERPC_EXCP_FP_VXSOFT:
249
                    info.si_code = FPE_FLTINV;
250
                    break;
251
                case POWERPC_EXCP_FP_VXNAN:
252
                case POWERPC_EXCP_FP_VXISI:
253
                case POWERPC_EXCP_FP_VXIDI:
254
                case POWERPC_EXCP_FP_VXIMZ:
255
                case POWERPC_EXCP_FP_VXVC:
256
                case POWERPC_EXCP_FP_VXSQRT:
257
                case POWERPC_EXCP_FP_VXCVI:
258
                    info.si_code = FPE_FLTSUB;
259
                    break;
260
                default:
261
                    EXCP_DUMP(env, "Unknown floating point exception (%02x)\n",
262
                              env->error_code);
263
                    break;
264
                }
265
                break;
266
            case POWERPC_EXCP_INVAL:
267
                EXCP_DUMP(env, "Invalid instruction\n");
268
                info.si_signo = SIGILL;
269
                info.si_errno = 0;
270
                switch (env->error_code & 0xF) {
271
                case POWERPC_EXCP_INVAL_INVAL:
272
                    info.si_code = ILL_ILLOPC;
273
                    break;
274
                case POWERPC_EXCP_INVAL_LSWX:
275
                    info.si_code = ILL_ILLOPN;
276
                    break;
277
                case POWERPC_EXCP_INVAL_SPR:
278
                    info.si_code = ILL_PRVREG;
279
                    break;
280
                case POWERPC_EXCP_INVAL_FP:
281
                    info.si_code = ILL_COPROC;
282
                    break;
283
                default:
284
                    EXCP_DUMP(env, "Unknown invalid operation (%02x)\n",
285
                              env->error_code & 0xF);
286
                    info.si_code = ILL_ILLADR;
287
                    break;
288
                }
289
                /* Handle this via the gdb */
290
                gdb_handlesig (env, SIGSEGV);
291
                break;
292
            case POWERPC_EXCP_PRIV:
293
                EXCP_DUMP(env, "Privilege violation\n");
294
                info.si_signo = SIGILL;
295
                info.si_errno = 0;
296
                switch (env->error_code & 0xF) {
297
                case POWERPC_EXCP_PRIV_OPC:
298
                    info.si_code = ILL_PRVOPC;
299
                    break;
300
                case POWERPC_EXCP_PRIV_REG:
301
                    info.si_code = ILL_PRVREG;
302
                    break;
303
                default:
304
                    EXCP_DUMP(env, "Unknown privilege violation (%02x)\n",
305
                              env->error_code & 0xF);
306
                    info.si_code = ILL_PRVOPC;
307
                    break;
308
                }
309
                break;
310
            case POWERPC_EXCP_TRAP:
311
                cpu_abort(env, "Tried to call a TRAP\n");
312
                break;
313
            default:
314
                /* Should not happen ! */
315
                cpu_abort(env, "Unknown program exception (%02x)\n",
316
                          env->error_code);
317
                break;
318
            }
319
            info.si_addr = (void*)(env->nip - 4);
320
            queue_signal(info.si_signo, &info);
321
            break;
322
        case POWERPC_EXCP_FPU:      /* Floating-point unavailable exception  */
323
            EXCP_DUMP(env, "No floating point allowed\n");
324
            info.si_signo = SIGILL;
325
            info.si_errno = 0;
326
            info.si_code = ILL_COPROC;
327
            info.si_addr = (void*)(env->nip - 4);
328
            queue_signal(info.si_signo, &info);
329
            break;
330
        case POWERPC_EXCP_SYSCALL:  /* System call exception                 */
331
            cpu_abort(env, "Syscall exception while in user mode. "
332
                      "Aborting\n");
333
            break;
334
        case POWERPC_EXCP_APU:      /* Auxiliary processor unavailable       */
335
            EXCP_DUMP(env, "No APU instruction allowed\n");
336
            info.si_signo = SIGILL;
337
            info.si_errno = 0;
338
            info.si_code = ILL_COPROC;
339
            info.si_addr = (void*)(env->nip - 4);
340
            queue_signal(info.si_signo, &info);
341
            break;
342
        case POWERPC_EXCP_DECR:     /* Decrementer exception                 */
343
            cpu_abort(env, "Decrementer interrupt while in user mode. "
344
                      "Aborting\n");
345
            break;
346
        case POWERPC_EXCP_FIT:      /* Fixed-interval timer interrupt        */
347
            cpu_abort(env, "Fix interval timer interrupt while in user mode. "
348
                      "Aborting\n");
349
            break;
350
        case POWERPC_EXCP_WDT:      /* Watchdog timer interrupt              */
351
            cpu_abort(env, "Watchdog timer interrupt while in user mode. "
352
                      "Aborting\n");
353
            break;
354
        case POWERPC_EXCP_DTLB:     /* Data TLB error                        */
355
            cpu_abort(env, "Data TLB exception while in user mode. "
356
                      "Aborting\n");
357
            break;
358
        case POWERPC_EXCP_ITLB:     /* Instruction TLB error                 */
359
            cpu_abort(env, "Instruction TLB exception while in user mode. "
360
                      "Aborting\n");
361
            break;
362
        case POWERPC_EXCP_DEBUG:    /* Debug interrupt                       */
363
            gdb_handlesig (env, SIGTRAP);
364
            break;
365
#if defined(TARGET_PPCEMB)
366
        case POWERPC_EXCP_SPEU:     /* SPE/embedded floating-point unavail.  */
367
            EXCP_DUMP(env, "No SPE/floating-point instruction allowed\n");
368
            info.si_signo = SIGILL;
369
            info.si_errno = 0;
370
            info.si_code = ILL_COPROC;
371
            info.si_addr = (void*)(env->nip - 4);
372
            queue_signal(info.si_signo, &info);
373
            break;
374
        case POWERPC_EXCP_EFPDI:    /* Embedded floating-point data IRQ      */
375
            cpu_abort(env, "Embedded floating-point data IRQ not handled\n");
376
            break;
377
        case POWERPC_EXCP_EFPRI:    /* Embedded floating-point round IRQ     */
378
            cpu_abort(env, "Embedded floating-point round IRQ not handled\n");
379
            break;
380
        case POWERPC_EXCP_EPERFM:   /* Embedded performance monitor IRQ      */
381
            cpu_abort(env, "Performance monitor exception not handled\n");
382
            break;
383
        case POWERPC_EXCP_DOORI:    /* Embedded doorbell interrupt           */
384
            cpu_abort(env, "Doorbell interrupt while in user mode. "
385
                       "Aborting\n");
386
            break;
387
        case POWERPC_EXCP_DOORCI:   /* Embedded doorbell critical interrupt  */
388
            cpu_abort(env, "Doorbell critical interrupt while in user mode. "
389
                      "Aborting\n");
390
            break;
391
#endif /* defined(TARGET_PPCEMB) */
392
        case POWERPC_EXCP_RESET:    /* System reset exception                */
393
            cpu_abort(env, "Reset interrupt while in user mode. "
394
                      "Aborting\n");
395
            break;
396
#if defined(TARGET_PPC64) /* PowerPC 64 */
397
        case POWERPC_EXCP_DSEG:     /* Data segment exception                */
398
            cpu_abort(env, "Data segment exception while in user mode. "
399
                      "Aborting\n");
400
            break;
401
        case POWERPC_EXCP_ISEG:     /* Instruction segment exception         */
402
            cpu_abort(env, "Instruction segment exception "
403
                      "while in user mode. Aborting\n");
404
            break;
405
#endif /* defined(TARGET_PPC64) */
406
#if defined(TARGET_PPC64H) /* PowerPC 64 with hypervisor mode support */
407
        case POWERPC_EXCP_HDECR:    /* Hypervisor decrementer exception      */
408
            cpu_abort(env, "Hypervisor decrementer interrupt "
409
                      "while in user mode. Aborting\n");
410
            break;
411
#endif /* defined(TARGET_PPC64H) */
412
        case POWERPC_EXCP_TRACE:    /* Trace exception                       */
413
            /* Nothing to do:
414
             * we use this exception to emulate step-by-step execution mode.
415
             */
416
            break;
417
#if defined(TARGET_PPC64H) /* PowerPC 64 with hypervisor mode support */
418
        case POWERPC_EXCP_HDSI:     /* Hypervisor data storage exception     */
419
            cpu_abort(env, "Hypervisor data storage exception "
420
                      "while in user mode. Aborting\n");
421
            break;
422
        case POWERPC_EXCP_HISI:     /* Hypervisor instruction storage excp   */
423
            cpu_abort(env, "Hypervisor instruction storage exception "
424
                      "while in user mode. Aborting\n");
425
            break;
426
        case POWERPC_EXCP_HDSEG:    /* Hypervisor data segment exception     */
427
            cpu_abort(env, "Hypervisor data segment exception "
428
                      "while in user mode. Aborting\n");
429
            break;
430
        case POWERPC_EXCP_HISEG:    /* Hypervisor instruction segment excp   */
431
            cpu_abort(env, "Hypervisor instruction segment exception "
432
                      "while in user mode. Aborting\n");
433
            break;
434
#endif /* defined(TARGET_PPC64H) */
435
        case POWERPC_EXCP_VPU:      /* Vector unavailable exception          */
436
            EXCP_DUMP(env, "No Altivec instructions allowed\n");
437
            info.si_signo = SIGILL;
438
            info.si_errno = 0;
439
            info.si_code = ILL_COPROC;
440
            info.si_addr = (void*)(env->nip - 4);
441
            queue_signal(info.si_signo, &info);
442
            break;
443
        case POWERPC_EXCP_PIT:      /* Programmable interval timer IRQ       */
444
            cpu_abort(env, "Programable interval timer interrupt "
445
                      "while in user mode. Aborting\n");
446
            break;
447
        case POWERPC_EXCP_IO:       /* IO error exception                    */
448
            cpu_abort(env, "IO error exception while in user mode. "
449
                      "Aborting\n");
450
            break;
451
        case POWERPC_EXCP_RUNM:     /* Run mode exception                    */
452
            cpu_abort(env, "Run mode exception while in user mode. "
453
                      "Aborting\n");
454
            break;
455
        case POWERPC_EXCP_EMUL:     /* Emulation trap exception              */
456
            cpu_abort(env, "Emulation trap exception not handled\n");
457
            break;
458
        case POWERPC_EXCP_IFTLB:    /* Instruction fetch TLB error           */
459
            cpu_abort(env, "Instruction fetch TLB exception "
460
                      "while in user-mode. Aborting");
461
            break;
462
        case POWERPC_EXCP_DLTLB:    /* Data load TLB miss                    */
463
            cpu_abort(env, "Data load TLB exception while in user-mode. "
464
                      "Aborting");
465
            break;
466
        case POWERPC_EXCP_DSTLB:    /* Data store TLB miss                   */
467
            cpu_abort(env, "Data store TLB exception while in user-mode. "
468
                      "Aborting");
469
            break;
470
        case POWERPC_EXCP_FPA:      /* Floating-point assist exception       */
471
            cpu_abort(env, "Floating-point assist exception not handled\n");
472
            break;
473
        case POWERPC_EXCP_IABR:     /* Instruction address breakpoint        */
474
            cpu_abort(env, "Instruction address breakpoint exception "
475
                      "not handled\n");
476
            break;
477
        case POWERPC_EXCP_SMI:      /* System management interrupt           */
478
            cpu_abort(env, "System management interrupt while in user mode. "
479
                      "Aborting\n");
480
            break;
481
        case POWERPC_EXCP_THERM:    /* Thermal interrupt                     */
482
            cpu_abort(env, "Thermal interrupt interrupt while in user mode. "
483
                      "Aborting\n");
484
            break;
485
        case POWERPC_EXCP_PERFM:    /* Embedded performance monitor IRQ      */
486
            cpu_abort(env, "Performance monitor exception not handled\n");
487
            break;
488
        case POWERPC_EXCP_VPUA:     /* Vector assist exception               */
489
            cpu_abort(env, "Vector assist exception not handled\n");
490
            break;
491
        case POWERPC_EXCP_SOFTP:    /* Soft patch exception                  */
492
            cpu_abort(env, "Soft patch exception not handled\n");
493
            break;
494
        case POWERPC_EXCP_MAINT:    /* Maintenance exception                 */
495
            cpu_abort(env, "Maintenance exception while in user mode. "
496
                      "Aborting\n");
497
            break;
498
        case POWERPC_EXCP_STOP:     /* stop translation                      */
499
            /* We did invalidate the instruction cache. Go on */
500
            break;
501
        case POWERPC_EXCP_BRANCH:   /* branch instruction:                   */
502
            /* We just stopped because of a branch. Go on */
503
            break;
504
        case POWERPC_EXCP_SYSCALL_USER:
505
            /* system call in user-mode emulation */
506
            /* system call */
507
            if(((int)env->gpr[0]) <= SYS_MAXSYSCALL && ((int)env->gpr[0])>0)
508
                ret = do_unix_syscall(env, env->gpr[0]/*, env->gpr[3], env->gpr[4],
509
                                      env->gpr[5], env->gpr[6], env->gpr[7],
510
                                      env->gpr[8], env->gpr[9], env->gpr[10]*/);
511
            else if(((int)env->gpr[0])<0)
512
                ret = do_mach_syscall(env, env->gpr[0], env->gpr[3], env->gpr[4],
513
                                      env->gpr[5], env->gpr[6], env->gpr[7],
514
                                      env->gpr[8], env->gpr[9], env->gpr[10]);
515
            else
516
                ret = do_thread_syscall(env, env->gpr[0], env->gpr[3], env->gpr[4],
517
                                        env->gpr[5], env->gpr[6], env->gpr[7],
518
                                        env->gpr[8], env->gpr[9], env->gpr[10]);
519

    
520
            /* Unix syscall error signaling */
521
            if(((int)env->gpr[0]) <= SYS_MAXSYSCALL && ((int)env->gpr[0])>0)
522
            {
523
                if( (int)ret < 0 )
524
                    env->nip += 0;
525
                else
526
                    env->nip += 4;
527
            }
528

    
529
            /* Return value */
530
            env->gpr[3] = ret;
531
            break;
532
        default:
533
            cpu_abort(env, "Unknown exception 0x%d. Aborting\n", trapnr);
534
            break;
535
        }
536
        process_pending_signals(env);
537
    }
538
}
539
#endif
540

    
541

    
542
#ifdef TARGET_I386
543

    
544
/***********************************************************/
545
/* CPUX86 core interface */
546

    
547
uint64_t cpu_get_tsc(CPUX86State *env)
548
{
549
    return cpu_get_real_ticks();
550
}
551

    
552
void
553
write_dt(void *ptr, unsigned long addr, unsigned long limit,
554
                     int flags)
555
{
556
    unsigned int e1, e2;
557
    e1 = (addr << 16) | (limit & 0xffff);
558
    e2 = ((addr >> 16) & 0xff) | (addr & 0xff000000) | (limit & 0x000f0000);
559
    e2 |= flags;
560
    stl((uint8_t *)ptr, e1);
561
    stl((uint8_t *)ptr + 4, e2);
562
}
563

    
564
static void set_gate(void *ptr, unsigned int type, unsigned int dpl,
565
                     unsigned long addr, unsigned int sel)
566
{
567
    unsigned int e1, e2;
568
    e1 = (addr & 0xffff) | (sel << 16);
569
    e2 = (addr & 0xffff0000) | 0x8000 | (dpl << 13) | (type << 8);
570
    stl((uint8_t *)ptr, e1);
571
    stl((uint8_t *)ptr + 4, e2);
572
}
573

    
574
#define GDT_TABLE_SIZE 14
575
#define LDT_TABLE_SIZE 15
576
#define IDT_TABLE_SIZE 256
577
#define TSS_SIZE 104
578
uint64_t gdt_table[GDT_TABLE_SIZE];
579
uint64_t ldt_table[LDT_TABLE_SIZE];
580
uint64_t idt_table[IDT_TABLE_SIZE];
581
uint32_t tss[TSS_SIZE];
582

    
583
/* only dpl matters as we do only user space emulation */
584
static void set_idt(int n, unsigned int dpl)
585
{
586
    set_gate(idt_table + n, 0, dpl, 0, 0);
587
}
588

    
589
/* ABI convention: after a syscall if there was an error the CF flag is set */
590
static inline void set_error(CPUX86State *env, int ret)
591
{
592
    if(ret<0)
593
        env->eflags = env->eflags | 0x1;
594
    else
595
        env->eflags &= ~0x1;
596
    env->regs[R_EAX] = ret;
597
}
598

    
599
void cpu_loop(CPUX86State *env)
600
{
601
    int trapnr;
602
    int ret;
603
    uint8_t *pc;
604
    target_siginfo_t info;
605

    
606
    for(;;) {
607
        trapnr = cpu_x86_exec(env);
608
        uint32_t *params = (uint32_t *)env->regs[R_ESP];
609
        switch(trapnr) {
610
        case 0x79: /* Our commpage hack back door exit is here */
611
            do_commpage(env,  env->eip,   *(params + 1), *(params + 2),
612
                                          *(params + 3), *(params + 4),
613
                                          *(params + 5), *(params + 6),
614
                                          *(params + 7), *(params + 8));
615
            break;
616
        case 0x81: /* mach syscall */
617
        {
618
            ret = do_mach_syscall(env,  env->regs[R_EAX],
619
                                          *(params + 1), *(params + 2),
620
                                          *(params + 3), *(params + 4),
621
                                          *(params + 5), *(params + 6),
622
                                          *(params + 7), *(params + 8));
623
            set_error(env, ret);
624
            break;
625
        }
626
        case 0x90: /* unix backdoor */
627
        {
628
            /* after sysenter, stack is in R_ECX, new eip in R_EDX (sysexit will flip them back)*/
629
            int saved_stack = env->regs[R_ESP];
630
            env->regs[R_ESP] = env->regs[R_ECX];
631

    
632
            ret = do_unix_syscall(env, env->regs[R_EAX]);
633

    
634
            env->regs[R_ECX] = env->regs[R_ESP];
635
            env->regs[R_ESP] = saved_stack;
636

    
637
            set_error(env, ret);
638
            break;
639
        }
640
        case 0x80: /* unix syscall */
641
        {
642
            ret = do_unix_syscall(env, env->regs[R_EAX]/*,
643
                                          *(params + 1), *(params + 2),
644
                                          *(params + 3), *(params + 4),
645
                                          *(params + 5), *(params + 6),
646
                                          *(params + 7), *(params + 8)*/);
647
            set_error(env, ret);
648
            break;
649
        }
650
        case 0x82: /* thread syscall */
651
        {
652
            ret = do_thread_syscall(env,  env->regs[R_EAX],
653
                                          *(params + 1), *(params + 2),
654
                                          *(params + 3), *(params + 4),
655
                                          *(params + 5), *(params + 6),
656
                                          *(params + 7), *(params + 8));
657
            set_error(env, ret);
658
            break;
659
        }
660
        case EXCP0B_NOSEG:
661
        case EXCP0C_STACK:
662
            info.si_signo = SIGBUS;
663
            info.si_errno = 0;
664
            info.si_code = BUS_NOOP;
665
            info.si_addr = 0;
666
            gdb_handlesig (env, SIGBUS);
667
            queue_signal(info.si_signo, &info);
668
            break;
669
        case EXCP0D_GPF:
670
            info.si_signo = SIGSEGV;
671
            info.si_errno = 0;
672
            info.si_code = SEGV_NOOP;
673
            info.si_addr = 0;
674
            gdb_handlesig (env, SIGSEGV);
675
            queue_signal(info.si_signo, &info);
676
            break;
677
        case EXCP0E_PAGE:
678
            info.si_signo = SIGSEGV;
679
            info.si_errno = 0;
680
            if (!(env->error_code & 1))
681
                info.si_code = SEGV_MAPERR;
682
            else
683
                info.si_code = SEGV_ACCERR;
684
            info.si_addr = (void*)env->cr[2];
685
            gdb_handlesig (env, SIGSEGV);
686
            queue_signal(info.si_signo, &info);
687
            break;
688
        case EXCP00_DIVZ:
689
            /* division by zero */
690
            info.si_signo = SIGFPE;
691
            info.si_errno = 0;
692
            info.si_code = FPE_INTDIV;
693
            info.si_addr = (void*)env->eip;
694
            gdb_handlesig (env, SIGFPE);
695
            queue_signal(info.si_signo, &info);
696
            break;
697
        case EXCP01_SSTP:
698
        case EXCP03_INT3:
699
            info.si_signo = SIGTRAP;
700
            info.si_errno = 0;
701
            info.si_code = TRAP_BRKPT;
702
            info.si_addr = (void*)env->eip;
703
            gdb_handlesig (env, SIGTRAP);
704
            queue_signal(info.si_signo, &info);
705
            break;
706
        case EXCP04_INTO:
707
        case EXCP05_BOUND:
708
            info.si_signo = SIGSEGV;
709
            info.si_errno = 0;
710
            info.si_code = SEGV_NOOP;
711
            info.si_addr = 0;
712
            gdb_handlesig (env, SIGSEGV);
713
            queue_signal(info.si_signo, &info);
714
            break;
715
        case EXCP06_ILLOP:
716
            info.si_signo = SIGILL;
717
            info.si_errno = 0;
718
            info.si_code = ILL_ILLOPN;
719
            info.si_addr = (void*)env->eip;
720
            gdb_handlesig (env, SIGILL);
721
            queue_signal(info.si_signo, &info);
722
            break;
723
        case EXCP_INTERRUPT:
724
            /* just indicate that signals should be handled asap */
725
            break;
726
        case EXCP_DEBUG:
727
            {
728
                int sig;
729

    
730
                sig = gdb_handlesig (env, SIGTRAP);
731
                if (sig)
732
                  {
733
                    info.si_signo = sig;
734
                    info.si_errno = 0;
735
                    info.si_code = TRAP_BRKPT;
736
                    queue_signal(info.si_signo, &info);
737
                  }
738
            }
739
            break;
740
        default:
741
            pc = (void*)(env->segs[R_CS].base + env->eip);
742
            fprintf(stderr, "qemu: 0x%08lx: unhandled CPU exception 0x%x - aborting\n",
743
                    (long)pc, trapnr);
744
            abort();
745
        }
746
        process_pending_signals(env);
747
    }
748
}
749
#endif
750

    
751
void usage(void)
752
{
753
    printf("qemu-" TARGET_ARCH " version " QEMU_VERSION ", Copyright (c) 2003-2004 Fabrice Bellard\n"
754
           "usage: qemu-" TARGET_ARCH " [-h] [-d opts] [-L path] [-s size] program [arguments...]\n"
755
           "Darwin CPU emulator (compiled for %s emulation)\n"
756
           "\n"
757
           "-h           print this help\n"
758
           "-L path      set the %s library path (default='%s')\n"
759
           "-s size      set the stack size in bytes (default=%ld)\n"
760
           "\n"
761
           "debug options:\n"
762
#ifdef USE_CODE_COPY
763
           "-no-code-copy   disable code copy acceleration\n"
764
#endif
765
           "-d options   activate log (logfile='%s')\n"
766
           "-g wait for gdb on port 1234\n"
767
           "-p pagesize  set the host page size to 'pagesize'\n",
768
           TARGET_ARCH,
769
           TARGET_ARCH,
770
           interp_prefix,
771
           stack_size,
772
           DEBUG_LOGFILE);
773
    _exit(1);
774
}
775

    
776
/* XXX: currently only used for async signals (see signal.c) */
777
CPUState *global_env;
778
/* used only if single thread */
779
CPUState *cpu_single_env = NULL;
780

    
781
/* used to free thread contexts */
782
TaskState *first_task_state;
783

    
784
int main(int argc, char **argv)
785
{
786
    const char *filename;
787
    struct target_pt_regs regs1, *regs = &regs1;
788
    TaskState ts1, *ts = &ts1;
789
    CPUState *env;
790
    int optind;
791
    short use_gdbstub = 0;
792
    const char *r;
793

    
794
    if (argc <= 1)
795
        usage();
796

    
797
    /* init debug */
798
    cpu_set_log_filename(DEBUG_LOGFILE);
799

    
800
    optind = 1;
801
    for(;;) {
802
        if (optind >= argc)
803
            break;
804
        r = argv[optind];
805
        if (r[0] != '-')
806
            break;
807
        optind++;
808
        r++;
809
        if (!strcmp(r, "-")) {
810
            break;
811
        } else if (!strcmp(r, "d")) {
812
            int mask;
813
            CPULogItem *item;
814

    
815
        if (optind >= argc)
816
        break;
817

    
818
        r = argv[optind++];
819
            mask = cpu_str_to_log_mask(r);
820
            if (!mask) {
821
                printf("Log items (comma separated):\n");
822
                for(item = cpu_log_items; item->mask != 0; item++) {
823
                    printf("%-10s %s\n", item->name, item->help);
824
                }
825
                exit(1);
826
            }
827
            cpu_set_log(mask);
828
        } else if (!strcmp(r, "s")) {
829
            r = argv[optind++];
830
            stack_size = strtol(r, (char **)&r, 0);
831
            if (stack_size <= 0)
832
                usage();
833
            if (*r == 'M')
834
                stack_size *= 1024 * 1024;
835
            else if (*r == 'k' || *r == 'K')
836
                stack_size *= 1024;
837
        } else if (!strcmp(r, "L")) {
838
            interp_prefix = argv[optind++];
839
        } else if (!strcmp(r, "p")) {
840
            qemu_host_page_size = atoi(argv[optind++]);
841
            if (qemu_host_page_size == 0 ||
842
                (qemu_host_page_size & (qemu_host_page_size - 1)) != 0) {
843
                fprintf(stderr, "page size must be a power of two\n");
844
                exit(1);
845
            }
846
        } else
847
        if (!strcmp(r, "g")) {
848
            use_gdbstub = 1;
849
        } else
850
#ifdef USE_CODE_COPY
851
        if (!strcmp(r, "no-code-copy")) {
852
            code_copy_enabled = 0;
853
        } else
854
#endif
855
        {
856
            usage();
857
        }
858
    }
859
    if (optind >= argc)
860
        usage();
861
    filename = argv[optind];
862

    
863
    /* Zero out regs */
864
    memset(regs, 0, sizeof(struct target_pt_regs));
865

    
866
    /* NOTE: we need to init the CPU at this stage to get
867
       qemu_host_page_size */
868
    env = cpu_init();
869

    
870
    printf("Starting %s with qemu\n----------------\n", filename);
871

    
872
    commpage_init();
873

    
874
    if (mach_exec(filename, argv+optind, environ, regs) != 0) {
875
    printf("Error loading %s\n", filename);
876
    _exit(1);
877
    }
878

    
879
    syscall_init();
880
    signal_init();
881
    global_env = env;
882

    
883
    /* build Task State */
884
    memset(ts, 0, sizeof(TaskState));
885
    env->opaque = ts;
886
    ts->used = 1;
887
    env->user_mode_only = 1;
888

    
889
#if defined(TARGET_I386)
890
    cpu_x86_set_cpl(env, 3);
891

    
892
    env->cr[0] = CR0_PG_MASK | CR0_WP_MASK | CR0_PE_MASK;
893
    env->hflags |= HF_PE_MASK;
894

    
895
    if (env->cpuid_features & CPUID_SSE) {
896
        env->cr[4] |= CR4_OSFXSR_MASK;
897
        env->hflags |= HF_OSFXSR_MASK;
898
    }
899

    
900
    /* flags setup : we activate the IRQs by default as in user mode */
901
    env->eflags |= IF_MASK;
902

    
903
    /* darwin register setup */
904
    env->regs[R_EAX] = regs->eax;
905
    env->regs[R_EBX] = regs->ebx;
906
    env->regs[R_ECX] = regs->ecx;
907
    env->regs[R_EDX] = regs->edx;
908
    env->regs[R_ESI] = regs->esi;
909
    env->regs[R_EDI] = regs->edi;
910
    env->regs[R_EBP] = regs->ebp;
911
    env->regs[R_ESP] = regs->esp;
912
    env->eip = regs->eip;
913

    
914
    /* Darwin LDT setup */
915
    /* 2 - User code segment
916
       3 - User data segment
917
       4 - User cthread */
918
    bzero(ldt_table, LDT_TABLE_SIZE * sizeof(ldt_table[0]));
919
    env->ldt.base = (uint32_t) ldt_table;
920
    env->ldt.limit = sizeof(ldt_table) - 1;
921

    
922
    write_dt(ldt_table + 2, 0, 0xfffff,
923
             DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | DESC_S_MASK |
924
             (3 << DESC_DPL_SHIFT) | (0xa << DESC_TYPE_SHIFT));
925
    write_dt(ldt_table + 3, 0, 0xfffff,
926
             DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | DESC_S_MASK |
927
             (3 << DESC_DPL_SHIFT) | (0x2 << DESC_TYPE_SHIFT));
928
    write_dt(ldt_table + 4, 0, 0xfffff,
929
             DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | DESC_S_MASK |
930
             (3 << DESC_DPL_SHIFT) | (0x2 << DESC_TYPE_SHIFT));
931

    
932
    /* Darwin GDT setup.
933
     * has changed a lot between old Darwin/x86 (pre-Mac Intel) and Mac OS X/x86,
934
       now everything is done via  int 0x81(mach) int 0x82 (thread) and sysenter/sysexit(unix) */
935
    bzero(gdt_table, sizeof(gdt_table));
936
    env->gdt.base = (uint32_t)gdt_table;
937
    env->gdt.limit = sizeof(gdt_table) - 1;
938

    
939
    /* Set up a back door to handle sysenter syscalls (unix) */
940
    char * syscallbackdoor = malloc(64);
941
    page_set_flags((int)syscallbackdoor, (int)syscallbackdoor + 64, PROT_EXEC | PROT_READ | PAGE_VALID);
942

    
943
    int i = 0;
944
    syscallbackdoor[i++] = 0xcd;
945
    syscallbackdoor[i++] = 0x90; /* int 0x90 */
946
    syscallbackdoor[i++] = 0x0F;
947
    syscallbackdoor[i++] = 0x35; /* sysexit */
948

    
949
    /* Darwin sysenter/sysexit setup */
950
    env->sysenter_cs = 0x1; //XXX
951
    env->sysenter_eip = (int)syscallbackdoor;
952
    env->sysenter_esp = (int)malloc(64);
953

    
954
    /* Darwin TSS setup
955
       This must match up with GDT[4] */
956
    env->tr.base = (uint32_t) tss;
957
    env->tr.limit = sizeof(tss) - 1;
958
    env->tr.flags = DESC_P_MASK | (0x9 << DESC_TYPE_SHIFT);
959
    stw(tss + 2, 0x10);  // ss0 = 0x10 = GDT[2] = Kernel Data Segment
960

    
961
    /* Darwin interrupt setup */
962
    bzero(idt_table, sizeof(idt_table));
963
    env->idt.base = (uint32_t) idt_table;
964
    env->idt.limit = sizeof(idt_table) - 1;
965
    set_idt(0, 0);
966
    set_idt(1, 0);
967
    set_idt(2, 0);
968
    set_idt(3, 3);
969
    set_idt(4, 3);
970
    set_idt(5, 3);
971
    set_idt(6, 0);
972
    set_idt(7, 0);
973
    set_idt(8, 0);
974
    set_idt(9, 0);
975
    set_idt(10, 0);
976
    set_idt(11, 0);
977
    set_idt(12, 0);
978
    set_idt(13, 0);
979
    set_idt(14, 0);
980
    set_idt(15, 0);
981
    set_idt(16, 0);
982
    set_idt(17, 0);
983
    set_idt(18, 0);
984
    set_idt(19, 0);
985
    /* Syscalls are done via
986
        int 0x80 (unix) (rarely used)
987
        int 0x81 (mach)
988
        int 0x82 (thread)
989
        int 0x83 (diag) (not handled here)
990
        sysenter/sysexit (unix) -> we redirect that to int 0x90 */
991
    set_idt(0x79, 3); /* Commpage hack, here is our backdoor interrupt */
992
    set_idt(0x80, 3); /* Unix Syscall */
993
    set_idt(0x81, 3); /* Mach Syscalls */
994
    set_idt(0x82, 3); /* thread Syscalls */
995

    
996
    set_idt(0x90, 3); /* qemu-darwin-user's Unix syscalls backdoor */
997

    
998

    
999
    cpu_x86_load_seg(env, R_CS, __USER_CS);
1000
    cpu_x86_load_seg(env, R_DS, __USER_DS);
1001
    cpu_x86_load_seg(env, R_ES, __USER_DS);
1002
    cpu_x86_load_seg(env, R_SS, __USER_DS);
1003
    cpu_x86_load_seg(env, R_FS, __USER_DS);
1004
    cpu_x86_load_seg(env, R_GS, __USER_DS);
1005

    
1006
#elif defined(TARGET_PPC)
1007
    {
1008
        int i;
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
}