Statistics
| Branch: | Revision:

root / darwin-user / main.c @ a73d39ba

History | View | Annotate | Download (35.7 kB)

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