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