blob: 71a1b6737e52d734705fb32c6996f9b793aba610 [file] [log] [blame]
bellard31e31b82003-02-18 22:55:36 +00001/*
bellard93ac68b2003-09-30 20:57:29 +00002 * qemu user main
ths5fafdf22007-09-16 21:08:06 +00003 *
bellard68d0f702008-01-06 17:21:48 +00004 * Copyright (c) 2003-2008 Fabrice Bellard
bellard31e31b82003-02-18 22:55:36 +00005 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
Blue Swirl8167ee82009-07-16 20:47:01 +000017 * along with this program; if not, see <http://www.gnu.org/licenses/>.
bellard31e31b82003-02-18 22:55:36 +000018 */
19#include <stdlib.h>
20#include <stdio.h>
21#include <stdarg.h>
bellard04369ff2003-03-20 22:33:23 +000022#include <string.h>
bellard31e31b82003-02-18 22:55:36 +000023#include <errno.h>
bellard0ecfa992003-03-03 14:32:43 +000024#include <unistd.h>
balroge4415702008-11-10 02:55:33 +000025#include <sys/mman.h>
Mika Westerbergedf8e2a2009-04-07 09:57:11 +030026#include <sys/syscall.h>
Richard Henderson703e0e82010-03-19 14:21:13 -070027#include <sys/resource.h>
bellard31e31b82003-02-18 22:55:36 +000028
bellard3ef693a2003-03-23 20:17:16 +000029#include "qemu.h"
aurel32ca10f862008-04-11 21:35:42 +000030#include "qemu-common.h"
malc902b3d52008-12-10 19:18:40 +000031#include "cache-utils.h"
pbrookd5975362008-06-07 20:50:51 +000032/* For tb_lock */
33#include "exec-all.h"
bellard31e31b82003-02-18 22:55:36 +000034
Blue Swirl29e922b2010-03-29 19:24:00 +000035#include "qemu-timer.h"
aurel3204a6dfe2009-01-30 19:59:17 +000036#include "envlist.h"
37
bellard3ef693a2003-03-23 20:17:16 +000038#define DEBUG_LOGFILE "/tmp/qemu.log"
bellard586314f2003-03-03 15:02:29 +000039
aurel32d088d662009-01-30 20:09:01 +000040char *exec_path;
41
aurel321b530a62009-04-05 20:08:59 +000042int singlestep;
Paul Brook379f6692009-07-17 12:48:08 +010043unsigned long mmap_min_addr;
Richard Henderson14f24e12010-03-10 15:39:07 -080044#if defined(CONFIG_USE_GUEST_BASE)
Paul Brook379f6692009-07-17 12:48:08 +010045unsigned long guest_base;
46int have_guest_base;
47#endif
aurel321b530a62009-04-05 20:08:59 +000048
bellard74cd30b2003-04-11 00:13:41 +000049static const char *interp_prefix = CONFIG_QEMU_PREFIX;
pbrookc5937222006-05-14 11:30:38 +000050const char *qemu_uname_release = CONFIG_UNAME_RELEASE;
bellard586314f2003-03-03 15:02:29 +000051
bellard9de5e442003-03-23 16:49:39 +000052/* XXX: on x86 MAP_GROWSDOWN only works if ESP <= address + 32, so
53 we allocate a bigger stack. Need a better solution, for example
54 by remapping the process stack directly at the right place */
Richard Henderson703e0e82010-03-19 14:21:13 -070055unsigned long guest_stack_size = 8 * 1024 * 1024UL;
bellard31e31b82003-02-18 22:55:36 +000056
57void gemu_log(const char *fmt, ...)
58{
59 va_list ap;
60
61 va_start(ap, fmt);
62 vfprintf(stderr, fmt, ap);
63 va_end(ap);
64}
65
blueswir18fcd3692008-08-17 20:26:25 +000066#if defined(TARGET_I386)
bellarda541f292004-04-12 20:39:29 +000067int cpu_get_pic_interrupt(CPUState *env)
bellard92ccca62003-06-24 13:30:31 +000068{
69 return -1;
70}
blueswir18fcd3692008-08-17 20:26:25 +000071#endif
bellard92ccca62003-06-24 13:30:31 +000072
bellard28ab0e22004-05-20 14:02:14 +000073/* timers for rdtsc */
74
bellard1dce7c32006-07-13 23:20:22 +000075#if 0
bellard28ab0e22004-05-20 14:02:14 +000076
77static uint64_t emu_time;
78
79int64_t cpu_get_real_ticks(void)
80{
81 return emu_time++;
82}
83
84#endif
85
Juan Quintela2f7bb872009-07-27 16:13:24 +020086#if defined(CONFIG_USE_NPTL)
pbrookd5975362008-06-07 20:50:51 +000087/***********************************************************/
88/* Helper routines for implementing atomic operations. */
89
90/* To implement exclusive operations we force all cpus to syncronise.
91 We don't require a full sync, only that no cpus are executing guest code.
92 The alternative is to map target atomic ops onto host equivalents,
93 which requires quite a lot of per host/target work. */
pbrookc2764712009-03-07 15:24:59 +000094static pthread_mutex_t cpu_list_mutex = PTHREAD_MUTEX_INITIALIZER;
pbrookd5975362008-06-07 20:50:51 +000095static pthread_mutex_t exclusive_lock = PTHREAD_MUTEX_INITIALIZER;
96static pthread_cond_t exclusive_cond = PTHREAD_COND_INITIALIZER;
97static pthread_cond_t exclusive_resume = PTHREAD_COND_INITIALIZER;
98static int pending_cpus;
99
100/* Make sure everything is in a consistent state for calling fork(). */
101void fork_start(void)
102{
pbrookd5975362008-06-07 20:50:51 +0000103 pthread_mutex_lock(&tb_lock);
104 pthread_mutex_lock(&exclusive_lock);
Riku Voipiod032d1b2009-12-04 15:16:31 +0200105 mmap_fork_start();
pbrookd5975362008-06-07 20:50:51 +0000106}
107
108void fork_end(int child)
109{
Riku Voipiod032d1b2009-12-04 15:16:31 +0200110 mmap_fork_end(child);
pbrookd5975362008-06-07 20:50:51 +0000111 if (child) {
112 /* Child processes created by fork() only have a single thread.
113 Discard information about the parent threads. */
114 first_cpu = thread_env;
115 thread_env->next_cpu = NULL;
116 pending_cpus = 0;
117 pthread_mutex_init(&exclusive_lock, NULL);
pbrookc2764712009-03-07 15:24:59 +0000118 pthread_mutex_init(&cpu_list_mutex, NULL);
pbrookd5975362008-06-07 20:50:51 +0000119 pthread_cond_init(&exclusive_cond, NULL);
120 pthread_cond_init(&exclusive_resume, NULL);
121 pthread_mutex_init(&tb_lock, NULL);
aurel322b1319c2008-12-18 22:44:04 +0000122 gdbserver_fork(thread_env);
pbrookd5975362008-06-07 20:50:51 +0000123 } else {
124 pthread_mutex_unlock(&exclusive_lock);
125 pthread_mutex_unlock(&tb_lock);
126 }
pbrookd5975362008-06-07 20:50:51 +0000127}
128
129/* Wait for pending exclusive operations to complete. The exclusive lock
130 must be held. */
131static inline void exclusive_idle(void)
132{
133 while (pending_cpus) {
134 pthread_cond_wait(&exclusive_resume, &exclusive_lock);
135 }
136}
137
138/* Start an exclusive operation.
139 Must only be called from outside cpu_arm_exec. */
140static inline void start_exclusive(void)
141{
142 CPUState *other;
143 pthread_mutex_lock(&exclusive_lock);
144 exclusive_idle();
145
146 pending_cpus = 1;
147 /* Make all other cpus stop executing. */
148 for (other = first_cpu; other; other = other->next_cpu) {
149 if (other->running) {
150 pending_cpus++;
aurel323098dba2009-03-07 21:28:24 +0000151 cpu_exit(other);
pbrookd5975362008-06-07 20:50:51 +0000152 }
153 }
154 if (pending_cpus > 1) {
155 pthread_cond_wait(&exclusive_cond, &exclusive_lock);
156 }
157}
158
159/* Finish an exclusive operation. */
160static inline void end_exclusive(void)
161{
162 pending_cpus = 0;
163 pthread_cond_broadcast(&exclusive_resume);
164 pthread_mutex_unlock(&exclusive_lock);
165}
166
167/* Wait for exclusive ops to finish, and begin cpu execution. */
168static inline void cpu_exec_start(CPUState *env)
169{
170 pthread_mutex_lock(&exclusive_lock);
171 exclusive_idle();
172 env->running = 1;
173 pthread_mutex_unlock(&exclusive_lock);
174}
175
176/* Mark cpu as not executing, and release pending exclusive ops. */
177static inline void cpu_exec_end(CPUState *env)
178{
179 pthread_mutex_lock(&exclusive_lock);
180 env->running = 0;
181 if (pending_cpus > 1) {
182 pending_cpus--;
183 if (pending_cpus == 1) {
184 pthread_cond_signal(&exclusive_cond);
185 }
186 }
187 exclusive_idle();
188 pthread_mutex_unlock(&exclusive_lock);
189}
pbrookc2764712009-03-07 15:24:59 +0000190
191void cpu_list_lock(void)
192{
193 pthread_mutex_lock(&cpu_list_mutex);
194}
195
196void cpu_list_unlock(void)
197{
198 pthread_mutex_unlock(&cpu_list_mutex);
199}
Juan Quintela2f7bb872009-07-27 16:13:24 +0200200#else /* if !CONFIG_USE_NPTL */
pbrookd5975362008-06-07 20:50:51 +0000201/* These are no-ops because we are not threadsafe. */
202static inline void cpu_exec_start(CPUState *env)
203{
204}
205
206static inline void cpu_exec_end(CPUState *env)
207{
208}
209
210static inline void start_exclusive(void)
211{
212}
213
214static inline void end_exclusive(void)
215{
216}
217
218void fork_start(void)
219{
220}
221
222void fork_end(int child)
223{
aurel322b1319c2008-12-18 22:44:04 +0000224 if (child) {
225 gdbserver_fork(thread_env);
226 }
pbrookd5975362008-06-07 20:50:51 +0000227}
pbrookc2764712009-03-07 15:24:59 +0000228
229void cpu_list_lock(void)
230{
231}
232
233void cpu_list_unlock(void)
234{
235}
pbrookd5975362008-06-07 20:50:51 +0000236#endif
237
238
bellarda541f292004-04-12 20:39:29 +0000239#ifdef TARGET_I386
240/***********************************************************/
241/* CPUX86 core interface */
242
bellard02a16022006-09-24 18:48:23 +0000243void cpu_smm_update(CPUState *env)
244{
245}
246
bellard28ab0e22004-05-20 14:02:14 +0000247uint64_t cpu_get_tsc(CPUX86State *env)
248{
249 return cpu_get_real_ticks();
250}
251
ths5fafdf22007-09-16 21:08:06 +0000252static void write_dt(void *ptr, unsigned long addr, unsigned long limit,
bellardf4beb512003-05-27 23:28:08 +0000253 int flags)
bellard6dbad632003-03-16 18:05:05 +0000254{
bellardf4beb512003-05-27 23:28:08 +0000255 unsigned int e1, e2;
pbrook53a59602006-03-25 19:31:22 +0000256 uint32_t *p;
bellard6dbad632003-03-16 18:05:05 +0000257 e1 = (addr << 16) | (limit & 0xffff);
258 e2 = ((addr >> 16) & 0xff) | (addr & 0xff000000) | (limit & 0x000f0000);
bellardf4beb512003-05-27 23:28:08 +0000259 e2 |= flags;
pbrook53a59602006-03-25 19:31:22 +0000260 p = ptr;
malcd538e8f2008-08-20 22:39:26 +0000261 p[0] = tswap32(e1);
262 p[1] = tswap32(e2);
bellardf4beb512003-05-27 23:28:08 +0000263}
264
balroge4415702008-11-10 02:55:33 +0000265static uint64_t *idt_table;
blueswir1eb38c522008-09-06 17:47:39 +0000266#ifdef TARGET_X86_64
bellardd2fd1af2007-11-14 18:08:56 +0000267static void set_gate64(void *ptr, unsigned int type, unsigned int dpl,
268 uint64_t addr, unsigned int sel)
269{
bellard4dbc4222007-11-15 15:27:03 +0000270 uint32_t *p, e1, e2;
bellardd2fd1af2007-11-14 18:08:56 +0000271 e1 = (addr & 0xffff) | (sel << 16);
272 e2 = (addr & 0xffff0000) | 0x8000 | (dpl << 13) | (type << 8);
273 p = ptr;
bellard4dbc4222007-11-15 15:27:03 +0000274 p[0] = tswap32(e1);
275 p[1] = tswap32(e2);
276 p[2] = tswap32(addr >> 32);
277 p[3] = 0;
bellardd2fd1af2007-11-14 18:08:56 +0000278}
279/* only dpl matters as we do only user space emulation */
280static void set_idt(int n, unsigned int dpl)
281{
282 set_gate64(idt_table + n * 2, 0, dpl, 0, 0);
283}
284#else
ths5fafdf22007-09-16 21:08:06 +0000285static void set_gate(void *ptr, unsigned int type, unsigned int dpl,
bellardd2fd1af2007-11-14 18:08:56 +0000286 uint32_t addr, unsigned int sel)
bellardf4beb512003-05-27 23:28:08 +0000287{
bellard4dbc4222007-11-15 15:27:03 +0000288 uint32_t *p, e1, e2;
bellardf4beb512003-05-27 23:28:08 +0000289 e1 = (addr & 0xffff) | (sel << 16);
290 e2 = (addr & 0xffff0000) | 0x8000 | (dpl << 13) | (type << 8);
pbrook53a59602006-03-25 19:31:22 +0000291 p = ptr;
bellard4dbc4222007-11-15 15:27:03 +0000292 p[0] = tswap32(e1);
293 p[1] = tswap32(e2);
bellard6dbad632003-03-16 18:05:05 +0000294}
295
bellardf4beb512003-05-27 23:28:08 +0000296/* only dpl matters as we do only user space emulation */
297static void set_idt(int n, unsigned int dpl)
298{
299 set_gate(idt_table + n, 0, dpl, 0, 0);
300}
bellardd2fd1af2007-11-14 18:08:56 +0000301#endif
bellard31e31b82003-02-18 22:55:36 +0000302
bellard89e957e2003-05-10 12:33:15 +0000303void cpu_loop(CPUX86State *env)
bellard1b6b0292003-03-22 17:31:38 +0000304{
bellardbc8a22c2003-03-30 21:02:40 +0000305 int trapnr;
blueswir1992f48a2007-10-14 16:27:31 +0000306 abi_ulong pc;
Anthony Liguoric227f092009-10-01 16:12:16 -0500307 target_siginfo_t info;
bellard851e67a2003-03-29 16:53:14 +0000308
bellard1b6b0292003-03-22 17:31:38 +0000309 for(;;) {
bellardbc8a22c2003-03-30 21:02:40 +0000310 trapnr = cpu_x86_exec(env);
bellardbc8a22c2003-03-30 21:02:40 +0000311 switch(trapnr) {
bellardf4beb512003-05-27 23:28:08 +0000312 case 0x80:
bellardd2fd1af2007-11-14 18:08:56 +0000313 /* linux syscall from int $0x80 */
ths5fafdf22007-09-16 21:08:06 +0000314 env->regs[R_EAX] = do_syscall(env,
315 env->regs[R_EAX],
bellardf4beb512003-05-27 23:28:08 +0000316 env->regs[R_EBX],
317 env->regs[R_ECX],
318 env->regs[R_EDX],
319 env->regs[R_ESI],
320 env->regs[R_EDI],
321 env->regs[R_EBP]);
322 break;
bellardd2fd1af2007-11-14 18:08:56 +0000323#ifndef TARGET_ABI32
324 case EXCP_SYSCALL:
325 /* linux syscall from syscall intruction */
326 env->regs[R_EAX] = do_syscall(env,
327 env->regs[R_EAX],
328 env->regs[R_EDI],
329 env->regs[R_ESI],
330 env->regs[R_EDX],
331 env->regs[10],
332 env->regs[8],
333 env->regs[9]);
334 env->eip = env->exception_next_eip;
335 break;
336#endif
bellardf4beb512003-05-27 23:28:08 +0000337 case EXCP0B_NOSEG:
338 case EXCP0C_STACK:
339 info.si_signo = SIGBUS;
340 info.si_errno = 0;
341 info.si_code = TARGET_SI_KERNEL;
342 info._sifields._sigfault._addr = 0;
pbrook624f7972008-05-31 16:11:38 +0000343 queue_signal(env, info.si_signo, &info);
bellardf4beb512003-05-27 23:28:08 +0000344 break;
bellard1b6b0292003-03-22 17:31:38 +0000345 case EXCP0D_GPF:
bellardd2fd1af2007-11-14 18:08:56 +0000346 /* XXX: potential problem if ABI32 */
j_mayer84409dd2007-04-06 08:56:50 +0000347#ifndef TARGET_X86_64
bellard851e67a2003-03-29 16:53:14 +0000348 if (env->eflags & VM_MASK) {
bellard89e957e2003-05-10 12:33:15 +0000349 handle_vm86_fault(env);
j_mayer84409dd2007-04-06 08:56:50 +0000350 } else
351#endif
352 {
bellardf4beb512003-05-27 23:28:08 +0000353 info.si_signo = SIGSEGV;
354 info.si_errno = 0;
355 info.si_code = TARGET_SI_KERNEL;
356 info._sifields._sigfault._addr = 0;
pbrook624f7972008-05-31 16:11:38 +0000357 queue_signal(env, info.si_signo, &info);
bellard1b6b0292003-03-22 17:31:38 +0000358 }
359 break;
bellardb689bc52003-05-08 15:33:33 +0000360 case EXCP0E_PAGE:
361 info.si_signo = SIGSEGV;
362 info.si_errno = 0;
363 if (!(env->error_code & 1))
364 info.si_code = TARGET_SEGV_MAPERR;
365 else
366 info.si_code = TARGET_SEGV_ACCERR;
bellard970a87a2003-06-21 13:13:25 +0000367 info._sifields._sigfault._addr = env->cr[2];
pbrook624f7972008-05-31 16:11:38 +0000368 queue_signal(env, info.si_signo, &info);
bellardb689bc52003-05-08 15:33:33 +0000369 break;
bellard9de5e442003-03-23 16:49:39 +0000370 case EXCP00_DIVZ:
j_mayer84409dd2007-04-06 08:56:50 +0000371#ifndef TARGET_X86_64
bellardbc8a22c2003-03-30 21:02:40 +0000372 if (env->eflags & VM_MASK) {
bellard447db212003-05-10 15:10:36 +0000373 handle_vm86_trap(env, trapnr);
j_mayer84409dd2007-04-06 08:56:50 +0000374 } else
375#endif
376 {
bellardbc8a22c2003-03-30 21:02:40 +0000377 /* division by zero */
378 info.si_signo = SIGFPE;
379 info.si_errno = 0;
380 info.si_code = TARGET_FPE_INTDIV;
381 info._sifields._sigfault._addr = env->eip;
pbrook624f7972008-05-31 16:11:38 +0000382 queue_signal(env, info.si_signo, &info);
bellardbc8a22c2003-03-30 21:02:40 +0000383 }
bellard9de5e442003-03-23 16:49:39 +0000384 break;
aliguori01df0402008-11-18 21:08:15 +0000385 case EXCP01_DB:
bellard447db212003-05-10 15:10:36 +0000386 case EXCP03_INT3:
j_mayer84409dd2007-04-06 08:56:50 +0000387#ifndef TARGET_X86_64
bellard447db212003-05-10 15:10:36 +0000388 if (env->eflags & VM_MASK) {
389 handle_vm86_trap(env, trapnr);
j_mayer84409dd2007-04-06 08:56:50 +0000390 } else
391#endif
392 {
bellard447db212003-05-10 15:10:36 +0000393 info.si_signo = SIGTRAP;
394 info.si_errno = 0;
aliguori01df0402008-11-18 21:08:15 +0000395 if (trapnr == EXCP01_DB) {
bellard447db212003-05-10 15:10:36 +0000396 info.si_code = TARGET_TRAP_BRKPT;
397 info._sifields._sigfault._addr = env->eip;
398 } else {
399 info.si_code = TARGET_SI_KERNEL;
400 info._sifields._sigfault._addr = 0;
401 }
pbrook624f7972008-05-31 16:11:38 +0000402 queue_signal(env, info.si_signo, &info);
bellard447db212003-05-10 15:10:36 +0000403 }
404 break;
bellard9de5e442003-03-23 16:49:39 +0000405 case EXCP04_INTO:
406 case EXCP05_BOUND:
j_mayer84409dd2007-04-06 08:56:50 +0000407#ifndef TARGET_X86_64
bellardbc8a22c2003-03-30 21:02:40 +0000408 if (env->eflags & VM_MASK) {
bellard447db212003-05-10 15:10:36 +0000409 handle_vm86_trap(env, trapnr);
j_mayer84409dd2007-04-06 08:56:50 +0000410 } else
411#endif
412 {
bellardbc8a22c2003-03-30 21:02:40 +0000413 info.si_signo = SIGSEGV;
414 info.si_errno = 0;
bellardb689bc52003-05-08 15:33:33 +0000415 info.si_code = TARGET_SI_KERNEL;
bellardbc8a22c2003-03-30 21:02:40 +0000416 info._sifields._sigfault._addr = 0;
pbrook624f7972008-05-31 16:11:38 +0000417 queue_signal(env, info.si_signo, &info);
bellardbc8a22c2003-03-30 21:02:40 +0000418 }
bellard9de5e442003-03-23 16:49:39 +0000419 break;
420 case EXCP06_ILLOP:
421 info.si_signo = SIGILL;
422 info.si_errno = 0;
423 info.si_code = TARGET_ILL_ILLOPN;
424 info._sifields._sigfault._addr = env->eip;
pbrook624f7972008-05-31 16:11:38 +0000425 queue_signal(env, info.si_signo, &info);
bellard9de5e442003-03-23 16:49:39 +0000426 break;
427 case EXCP_INTERRUPT:
428 /* just indicate that signals should be handled asap */
429 break;
bellard1fddef42005-04-17 19:16:13 +0000430 case EXCP_DEBUG:
431 {
432 int sig;
433
434 sig = gdb_handlesig (env, TARGET_SIGTRAP);
435 if (sig)
436 {
437 info.si_signo = sig;
438 info.si_errno = 0;
439 info.si_code = TARGET_TRAP_BRKPT;
pbrook624f7972008-05-31 16:11:38 +0000440 queue_signal(env, info.si_signo, &info);
bellard1fddef42005-04-17 19:16:13 +0000441 }
442 }
443 break;
bellard1b6b0292003-03-22 17:31:38 +0000444 default:
bellard970a87a2003-06-21 13:13:25 +0000445 pc = env->segs[R_CS].base + env->eip;
ths5fafdf22007-09-16 21:08:06 +0000446 fprintf(stderr, "qemu: 0x%08lx: unhandled CPU exception 0x%x - aborting\n",
bellardbc8a22c2003-03-30 21:02:40 +0000447 (long)pc, trapnr);
bellard1b6b0292003-03-22 17:31:38 +0000448 abort();
449 }
bellard66fb9762003-03-23 01:06:05 +0000450 process_pending_signals(env);
bellard1b6b0292003-03-22 17:31:38 +0000451 }
452}
bellardb346ff42003-06-15 20:05:50 +0000453#endif
454
455#ifdef TARGET_ARM
456
blueswir1992f48a2007-10-14 16:27:31 +0000457static void arm_cache_flush(abi_ulong start, abi_ulong last)
bellard6f1f31c2004-04-25 18:00:45 +0000458{
blueswir1992f48a2007-10-14 16:27:31 +0000459 abi_ulong addr, last1;
bellard6f1f31c2004-04-25 18:00:45 +0000460
461 if (last < start)
462 return;
463 addr = start;
464 for(;;) {
465 last1 = ((addr + TARGET_PAGE_SIZE) & TARGET_PAGE_MASK) - 1;
466 if (last1 > last)
467 last1 = last;
468 tb_invalidate_page_range(addr, last1 + 1);
469 if (last1 == last)
470 break;
471 addr = last1 + 1;
472 }
473}
474
pbrookfbb4a2e2008-05-29 00:20:44 +0000475/* Handle a jump to the kernel code page. */
476static int
477do_kernel_trap(CPUARMState *env)
478{
479 uint32_t addr;
480 uint32_t cpsr;
481 uint32_t val;
482
483 switch (env->regs[15]) {
484 case 0xffff0fa0: /* __kernel_memory_barrier */
485 /* ??? No-op. Will need to do better for SMP. */
486 break;
487 case 0xffff0fc0: /* __kernel_cmpxchg */
pbrookd5975362008-06-07 20:50:51 +0000488 /* XXX: This only works between threads, not between processes.
489 It's probably possible to implement this with native host
490 operations. However things like ldrex/strex are much harder so
491 there's not much point trying. */
492 start_exclusive();
pbrookfbb4a2e2008-05-29 00:20:44 +0000493 cpsr = cpsr_read(env);
494 addr = env->regs[2];
495 /* FIXME: This should SEGV if the access fails. */
496 if (get_user_u32(val, addr))
497 val = ~env->regs[0];
498 if (val == env->regs[0]) {
499 val = env->regs[1];
500 /* FIXME: Check for segfaults. */
501 put_user_u32(val, addr);
502 env->regs[0] = 0;
503 cpsr |= CPSR_C;
504 } else {
505 env->regs[0] = -1;
506 cpsr &= ~CPSR_C;
507 }
508 cpsr_write(env, cpsr, CPSR_C);
pbrookd5975362008-06-07 20:50:51 +0000509 end_exclusive();
pbrookfbb4a2e2008-05-29 00:20:44 +0000510 break;
511 case 0xffff0fe0: /* __kernel_get_tls */
512 env->regs[0] = env->cp15.c13_tls2;
513 break;
514 default:
515 return 1;
516 }
517 /* Jump back to the caller. */
518 addr = env->regs[14];
519 if (addr & 1) {
520 env->thumb = 1;
521 addr &= ~1;
522 }
523 env->regs[15] = addr;
524
525 return 0;
526}
527
Paul Brook426f5ab2009-11-22 21:35:13 +0000528static int do_strex(CPUARMState *env)
529{
530 uint32_t val;
531 int size;
532 int rc = 1;
533 int segv = 0;
534 uint32_t addr;
535 start_exclusive();
536 addr = env->exclusive_addr;
537 if (addr != env->exclusive_test) {
538 goto fail;
539 }
540 size = env->exclusive_info & 0xf;
541 switch (size) {
542 case 0:
543 segv = get_user_u8(val, addr);
544 break;
545 case 1:
546 segv = get_user_u16(val, addr);
547 break;
548 case 2:
549 case 3:
550 segv = get_user_u32(val, addr);
551 break;
Aurelien Jarnof7001a32009-12-24 00:17:12 +0100552 default:
553 abort();
Paul Brook426f5ab2009-11-22 21:35:13 +0000554 }
555 if (segv) {
556 env->cp15.c6_data = addr;
557 goto done;
558 }
559 if (val != env->exclusive_val) {
560 goto fail;
561 }
562 if (size == 3) {
563 segv = get_user_u32(val, addr + 4);
564 if (segv) {
565 env->cp15.c6_data = addr + 4;
566 goto done;
567 }
568 if (val != env->exclusive_high) {
569 goto fail;
570 }
571 }
572 val = env->regs[(env->exclusive_info >> 8) & 0xf];
573 switch (size) {
574 case 0:
575 segv = put_user_u8(val, addr);
576 break;
577 case 1:
578 segv = put_user_u16(val, addr);
579 break;
580 case 2:
581 case 3:
582 segv = put_user_u32(val, addr);
583 break;
584 }
585 if (segv) {
586 env->cp15.c6_data = addr;
587 goto done;
588 }
589 if (size == 3) {
590 val = env->regs[(env->exclusive_info >> 12) & 0xf];
591 segv = put_user_u32(val, addr);
592 if (segv) {
593 env->cp15.c6_data = addr + 4;
594 goto done;
595 }
596 }
597 rc = 0;
598fail:
Paul Brook725b8a62009-12-11 15:38:10 +0000599 env->regs[15] += 4;
Paul Brook426f5ab2009-11-22 21:35:13 +0000600 env->regs[(env->exclusive_info >> 4) & 0xf] = rc;
601done:
602 end_exclusive();
603 return segv;
604}
605
bellardb346ff42003-06-15 20:05:50 +0000606void cpu_loop(CPUARMState *env)
607{
608 int trapnr;
609 unsigned int n, insn;
Anthony Liguoric227f092009-10-01 16:12:16 -0500610 target_siginfo_t info;
bellardb5ff1b32005-11-26 10:38:39 +0000611 uint32_t addr;
ths3b46e622007-09-17 08:09:54 +0000612
bellardb346ff42003-06-15 20:05:50 +0000613 for(;;) {
pbrookd5975362008-06-07 20:50:51 +0000614 cpu_exec_start(env);
bellardb346ff42003-06-15 20:05:50 +0000615 trapnr = cpu_arm_exec(env);
pbrookd5975362008-06-07 20:50:51 +0000616 cpu_exec_end(env);
bellardb346ff42003-06-15 20:05:50 +0000617 switch(trapnr) {
618 case EXCP_UDEF:
bellardc6981052004-02-16 21:49:03 +0000619 {
620 TaskState *ts = env->opaque;
621 uint32_t opcode;
aurel326d9a42b2008-04-07 20:30:53 +0000622 int rc;
bellardc6981052004-02-16 21:49:03 +0000623
624 /* we handle the FPU emulation here, as Linux */
625 /* we get the opcode */
bellard2f619692007-11-16 10:46:05 +0000626 /* FIXME - what to do if get_user() fails? */
627 get_user_u32(opcode, env->regs[15]);
ths3b46e622007-09-17 08:09:54 +0000628
aurel326d9a42b2008-04-07 20:30:53 +0000629 rc = EmulateAll(opcode, &ts->fpa, env);
630 if (rc == 0) { /* illegal instruction */
bellardc6981052004-02-16 21:49:03 +0000631 info.si_signo = SIGILL;
632 info.si_errno = 0;
633 info.si_code = TARGET_ILL_ILLOPN;
634 info._sifields._sigfault._addr = env->regs[15];
pbrook624f7972008-05-31 16:11:38 +0000635 queue_signal(env, info.si_signo, &info);
aurel326d9a42b2008-04-07 20:30:53 +0000636 } else if (rc < 0) { /* FP exception */
637 int arm_fpe=0;
638
639 /* translate softfloat flags to FPSR flags */
640 if (-rc & float_flag_invalid)
641 arm_fpe |= BIT_IOC;
642 if (-rc & float_flag_divbyzero)
643 arm_fpe |= BIT_DZC;
644 if (-rc & float_flag_overflow)
645 arm_fpe |= BIT_OFC;
646 if (-rc & float_flag_underflow)
647 arm_fpe |= BIT_UFC;
648 if (-rc & float_flag_inexact)
649 arm_fpe |= BIT_IXC;
650
651 FPSR fpsr = ts->fpa.fpsr;
652 //printf("fpsr 0x%x, arm_fpe 0x%x\n",fpsr,arm_fpe);
653
654 if (fpsr & (arm_fpe << 16)) { /* exception enabled? */
655 info.si_signo = SIGFPE;
656 info.si_errno = 0;
657
658 /* ordered by priority, least first */
659 if (arm_fpe & BIT_IXC) info.si_code = TARGET_FPE_FLTRES;
660 if (arm_fpe & BIT_UFC) info.si_code = TARGET_FPE_FLTUND;
661 if (arm_fpe & BIT_OFC) info.si_code = TARGET_FPE_FLTOVF;
662 if (arm_fpe & BIT_DZC) info.si_code = TARGET_FPE_FLTDIV;
663 if (arm_fpe & BIT_IOC) info.si_code = TARGET_FPE_FLTINV;
664
665 info._sifields._sigfault._addr = env->regs[15];
pbrook624f7972008-05-31 16:11:38 +0000666 queue_signal(env, info.si_signo, &info);
aurel326d9a42b2008-04-07 20:30:53 +0000667 } else {
668 env->regs[15] += 4;
669 }
670
671 /* accumulate unenabled exceptions */
672 if ((!(fpsr & BIT_IXE)) && (arm_fpe & BIT_IXC))
673 fpsr |= BIT_IXC;
674 if ((!(fpsr & BIT_UFE)) && (arm_fpe & BIT_UFC))
675 fpsr |= BIT_UFC;
676 if ((!(fpsr & BIT_OFE)) && (arm_fpe & BIT_OFC))
677 fpsr |= BIT_OFC;
678 if ((!(fpsr & BIT_DZE)) && (arm_fpe & BIT_DZC))
679 fpsr |= BIT_DZC;
680 if ((!(fpsr & BIT_IOE)) && (arm_fpe & BIT_IOC))
681 fpsr |= BIT_IOC;
682 ts->fpa.fpsr=fpsr;
683 } else { /* everything OK */
bellardc6981052004-02-16 21:49:03 +0000684 /* increment PC */
685 env->regs[15] += 4;
686 }
687 }
bellardb346ff42003-06-15 20:05:50 +0000688 break;
689 case EXCP_SWI:
pbrook06c949e2006-02-04 19:35:26 +0000690 case EXCP_BKPT:
bellardb346ff42003-06-15 20:05:50 +0000691 {
pbrookce4defa2006-02-09 16:49:55 +0000692 env->eabi = 1;
bellardb346ff42003-06-15 20:05:50 +0000693 /* system call */
pbrook06c949e2006-02-04 19:35:26 +0000694 if (trapnr == EXCP_BKPT) {
695 if (env->thumb) {
bellard2f619692007-11-16 10:46:05 +0000696 /* FIXME - what to do if get_user() fails? */
697 get_user_u16(insn, env->regs[15]);
pbrook06c949e2006-02-04 19:35:26 +0000698 n = insn & 0xff;
699 env->regs[15] += 2;
700 } else {
bellard2f619692007-11-16 10:46:05 +0000701 /* FIXME - what to do if get_user() fails? */
702 get_user_u32(insn, env->regs[15]);
pbrook06c949e2006-02-04 19:35:26 +0000703 n = (insn & 0xf) | ((insn >> 4) & 0xff0);
704 env->regs[15] += 4;
705 }
bellard192c7bd2005-04-27 20:11:21 +0000706 } else {
pbrook06c949e2006-02-04 19:35:26 +0000707 if (env->thumb) {
bellard2f619692007-11-16 10:46:05 +0000708 /* FIXME - what to do if get_user() fails? */
709 get_user_u16(insn, env->regs[15] - 2);
pbrook06c949e2006-02-04 19:35:26 +0000710 n = insn & 0xff;
711 } else {
bellard2f619692007-11-16 10:46:05 +0000712 /* FIXME - what to do if get_user() fails? */
713 get_user_u32(insn, env->regs[15] - 4);
pbrook06c949e2006-02-04 19:35:26 +0000714 n = insn & 0xffffff;
715 }
bellard192c7bd2005-04-27 20:11:21 +0000716 }
717
bellard6f1f31c2004-04-25 18:00:45 +0000718 if (n == ARM_NR_cacheflush) {
719 arm_cache_flush(env->regs[0], env->regs[1]);
bellarda4f81972005-04-23 18:25:41 +0000720 } else if (n == ARM_NR_semihosting
721 || n == ARM_NR_thumb_semihosting) {
722 env->regs[0] = do_arm_semihosting (env);
pbrookce4defa2006-02-09 16:49:55 +0000723 } else if (n == 0 || n >= ARM_SYSCALL_BASE
bellard192c7bd2005-04-27 20:11:21 +0000724 || (env->thumb && n == ARM_THUMB_SYSCALL)) {
bellardb346ff42003-06-15 20:05:50 +0000725 /* linux syscall */
pbrookce4defa2006-02-09 16:49:55 +0000726 if (env->thumb || n == 0) {
bellard192c7bd2005-04-27 20:11:21 +0000727 n = env->regs[7];
728 } else {
729 n -= ARM_SYSCALL_BASE;
pbrookce4defa2006-02-09 16:49:55 +0000730 env->eabi = 0;
bellard192c7bd2005-04-27 20:11:21 +0000731 }
pbrookfbb4a2e2008-05-29 00:20:44 +0000732 if ( n > ARM_NR_BASE) {
733 switch (n) {
734 case ARM_NR_cacheflush:
735 arm_cache_flush(env->regs[0], env->regs[1]);
736 break;
737 case ARM_NR_set_tls:
738 cpu_set_tls(env, env->regs[0]);
739 env->regs[0] = 0;
740 break;
741 default:
742 gemu_log("qemu: Unsupported ARM syscall: 0x%x\n",
743 n);
744 env->regs[0] = -TARGET_ENOSYS;
745 break;
746 }
747 } else {
748 env->regs[0] = do_syscall(env,
749 n,
750 env->regs[0],
751 env->regs[1],
752 env->regs[2],
753 env->regs[3],
754 env->regs[4],
755 env->regs[5]);
756 }
bellardb346ff42003-06-15 20:05:50 +0000757 } else {
758 goto error;
759 }
760 }
761 break;
bellard43fff232003-07-09 19:31:39 +0000762 case EXCP_INTERRUPT:
763 /* just indicate that signals should be handled asap */
764 break;
bellard68016c62005-02-07 23:12:27 +0000765 case EXCP_PREFETCH_ABORT:
balrogeae473c2008-07-29 14:09:57 +0000766 addr = env->cp15.c6_insn;
bellardb5ff1b32005-11-26 10:38:39 +0000767 goto do_segv;
bellard68016c62005-02-07 23:12:27 +0000768 case EXCP_DATA_ABORT:
balrogeae473c2008-07-29 14:09:57 +0000769 addr = env->cp15.c6_data;
bellardb5ff1b32005-11-26 10:38:39 +0000770 goto do_segv;
771 do_segv:
bellard68016c62005-02-07 23:12:27 +0000772 {
773 info.si_signo = SIGSEGV;
774 info.si_errno = 0;
775 /* XXX: check env->error_code */
776 info.si_code = TARGET_SEGV_MAPERR;
bellardb5ff1b32005-11-26 10:38:39 +0000777 info._sifields._sigfault._addr = addr;
pbrook624f7972008-05-31 16:11:38 +0000778 queue_signal(env, info.si_signo, &info);
bellard68016c62005-02-07 23:12:27 +0000779 }
780 break;
bellard1fddef42005-04-17 19:16:13 +0000781 case EXCP_DEBUG:
782 {
783 int sig;
784
785 sig = gdb_handlesig (env, TARGET_SIGTRAP);
786 if (sig)
787 {
788 info.si_signo = sig;
789 info.si_errno = 0;
790 info.si_code = TARGET_TRAP_BRKPT;
pbrook624f7972008-05-31 16:11:38 +0000791 queue_signal(env, info.si_signo, &info);
bellard1fddef42005-04-17 19:16:13 +0000792 }
793 }
794 break;
pbrookfbb4a2e2008-05-29 00:20:44 +0000795 case EXCP_KERNEL_TRAP:
796 if (do_kernel_trap(env))
797 goto error;
798 break;
Paul Brook426f5ab2009-11-22 21:35:13 +0000799 case EXCP_STREX:
800 if (do_strex(env)) {
801 addr = env->cp15.c6_data;
802 goto do_segv;
803 }
Paul Brooke9273452009-11-24 13:10:08 +0000804 break;
bellardb346ff42003-06-15 20:05:50 +0000805 default:
806 error:
ths5fafdf22007-09-16 21:08:06 +0000807 fprintf(stderr, "qemu: unhandled CPU exception 0x%x - aborting\n",
bellardb346ff42003-06-15 20:05:50 +0000808 trapnr);
bellard7fe48482004-10-09 18:08:01 +0000809 cpu_dump_state(env, stderr, fprintf, 0);
bellardb346ff42003-06-15 20:05:50 +0000810 abort();
811 }
812 process_pending_signals(env);
813 }
814}
815
816#endif
bellard1b6b0292003-03-22 17:31:38 +0000817
bellard93ac68b2003-09-30 20:57:29 +0000818#ifdef TARGET_SPARC
blueswir1ed23fbd2008-08-30 09:20:21 +0000819#define SPARC64_STACK_BIAS 2047
bellard93ac68b2003-09-30 20:57:29 +0000820
bellard060366c2004-01-04 15:50:01 +0000821//#define DEBUG_WIN
822
bellard2623cba2005-02-19 17:25:31 +0000823/* WARNING: dealing with register windows _is_ complicated. More info
824 can be found at http://www.sics.se/~psm/sparcstack.html */
bellard060366c2004-01-04 15:50:01 +0000825static inline int get_reg_index(CPUSPARCState *env, int cwp, int index)
826{
blueswir11a140262008-06-07 08:07:37 +0000827 index = (index + cwp * 16) % (16 * env->nwindows);
bellard060366c2004-01-04 15:50:01 +0000828 /* wrap handling : if cwp is on the last window, then we use the
829 registers 'after' the end */
blueswir11a140262008-06-07 08:07:37 +0000830 if (index < 8 && env->cwp == env->nwindows - 1)
831 index += 16 * env->nwindows;
bellard060366c2004-01-04 15:50:01 +0000832 return index;
833}
834
bellard2623cba2005-02-19 17:25:31 +0000835/* save the register window 'cwp1' */
836static inline void save_window_offset(CPUSPARCState *env, int cwp1)
bellard060366c2004-01-04 15:50:01 +0000837{
bellard2623cba2005-02-19 17:25:31 +0000838 unsigned int i;
blueswir1992f48a2007-10-14 16:27:31 +0000839 abi_ulong sp_ptr;
ths3b46e622007-09-17 08:09:54 +0000840
pbrook53a59602006-03-25 19:31:22 +0000841 sp_ptr = env->regbase[get_reg_index(env, cwp1, 6)];
blueswir1ed23fbd2008-08-30 09:20:21 +0000842#ifdef TARGET_SPARC64
843 if (sp_ptr & 3)
844 sp_ptr += SPARC64_STACK_BIAS;
845#endif
bellard060366c2004-01-04 15:50:01 +0000846#if defined(DEBUG_WIN)
blueswir12daf0282008-06-15 18:02:48 +0000847 printf("win_overflow: sp_ptr=0x" TARGET_ABI_FMT_lx " save_cwp=%d\n",
848 sp_ptr, cwp1);
bellard060366c2004-01-04 15:50:01 +0000849#endif
bellard2623cba2005-02-19 17:25:31 +0000850 for(i = 0; i < 16; i++) {
bellard2f619692007-11-16 10:46:05 +0000851 /* FIXME - what to do if put_user() fails? */
852 put_user_ual(env->regbase[get_reg_index(env, cwp1, 8 + i)], sp_ptr);
blueswir1992f48a2007-10-14 16:27:31 +0000853 sp_ptr += sizeof(abi_ulong);
bellard2623cba2005-02-19 17:25:31 +0000854 }
bellard060366c2004-01-04 15:50:01 +0000855}
856
857static void save_window(CPUSPARCState *env)
858{
bellard5ef54112006-07-18 21:14:09 +0000859#ifndef TARGET_SPARC64
bellard2623cba2005-02-19 17:25:31 +0000860 unsigned int new_wim;
blueswir11a140262008-06-07 08:07:37 +0000861 new_wim = ((env->wim >> 1) | (env->wim << (env->nwindows - 1))) &
862 ((1LL << env->nwindows) - 1);
863 save_window_offset(env, cpu_cwp_dec(env, env->cwp - 2));
bellard2623cba2005-02-19 17:25:31 +0000864 env->wim = new_wim;
bellard5ef54112006-07-18 21:14:09 +0000865#else
blueswir11a140262008-06-07 08:07:37 +0000866 save_window_offset(env, cpu_cwp_dec(env, env->cwp - 2));
bellard5ef54112006-07-18 21:14:09 +0000867 env->cansave++;
868 env->canrestore--;
869#endif
bellard060366c2004-01-04 15:50:01 +0000870}
871
872static void restore_window(CPUSPARCState *env)
873{
blueswir1eda52952008-08-27 19:19:44 +0000874#ifndef TARGET_SPARC64
875 unsigned int new_wim;
876#endif
877 unsigned int i, cwp1;
blueswir1992f48a2007-10-14 16:27:31 +0000878 abi_ulong sp_ptr;
ths3b46e622007-09-17 08:09:54 +0000879
blueswir1eda52952008-08-27 19:19:44 +0000880#ifndef TARGET_SPARC64
blueswir11a140262008-06-07 08:07:37 +0000881 new_wim = ((env->wim << 1) | (env->wim >> (env->nwindows - 1))) &
882 ((1LL << env->nwindows) - 1);
blueswir1eda52952008-08-27 19:19:44 +0000883#endif
ths3b46e622007-09-17 08:09:54 +0000884
bellard060366c2004-01-04 15:50:01 +0000885 /* restore the invalid window */
blueswir11a140262008-06-07 08:07:37 +0000886 cwp1 = cpu_cwp_inc(env, env->cwp + 1);
pbrook53a59602006-03-25 19:31:22 +0000887 sp_ptr = env->regbase[get_reg_index(env, cwp1, 6)];
blueswir1ed23fbd2008-08-30 09:20:21 +0000888#ifdef TARGET_SPARC64
889 if (sp_ptr & 3)
890 sp_ptr += SPARC64_STACK_BIAS;
891#endif
bellard060366c2004-01-04 15:50:01 +0000892#if defined(DEBUG_WIN)
blueswir12daf0282008-06-15 18:02:48 +0000893 printf("win_underflow: sp_ptr=0x" TARGET_ABI_FMT_lx " load_cwp=%d\n",
894 sp_ptr, cwp1);
bellard060366c2004-01-04 15:50:01 +0000895#endif
bellard2623cba2005-02-19 17:25:31 +0000896 for(i = 0; i < 16; i++) {
bellard2f619692007-11-16 10:46:05 +0000897 /* FIXME - what to do if get_user() fails? */
898 get_user_ual(env->regbase[get_reg_index(env, cwp1, 8 + i)], sp_ptr);
blueswir1992f48a2007-10-14 16:27:31 +0000899 sp_ptr += sizeof(abi_ulong);
bellard2623cba2005-02-19 17:25:31 +0000900 }
bellard5ef54112006-07-18 21:14:09 +0000901#ifdef TARGET_SPARC64
902 env->canrestore++;
blueswir11a140262008-06-07 08:07:37 +0000903 if (env->cleanwin < env->nwindows - 1)
904 env->cleanwin++;
bellard5ef54112006-07-18 21:14:09 +0000905 env->cansave--;
blueswir1eda52952008-08-27 19:19:44 +0000906#else
907 env->wim = new_wim;
bellard5ef54112006-07-18 21:14:09 +0000908#endif
bellard060366c2004-01-04 15:50:01 +0000909}
910
911static void flush_windows(CPUSPARCState *env)
912{
913 int offset, cwp1;
bellard2623cba2005-02-19 17:25:31 +0000914
915 offset = 1;
bellard060366c2004-01-04 15:50:01 +0000916 for(;;) {
917 /* if restore would invoke restore_window(), then we can stop */
blueswir11a140262008-06-07 08:07:37 +0000918 cwp1 = cpu_cwp_inc(env, env->cwp + offset);
blueswir1eda52952008-08-27 19:19:44 +0000919#ifndef TARGET_SPARC64
bellard060366c2004-01-04 15:50:01 +0000920 if (env->wim & (1 << cwp1))
921 break;
blueswir1eda52952008-08-27 19:19:44 +0000922#else
923 if (env->canrestore == 0)
924 break;
925 env->cansave++;
926 env->canrestore--;
927#endif
bellard2623cba2005-02-19 17:25:31 +0000928 save_window_offset(env, cwp1);
bellard060366c2004-01-04 15:50:01 +0000929 offset++;
930 }
blueswir11a140262008-06-07 08:07:37 +0000931 cwp1 = cpu_cwp_inc(env, env->cwp + 1);
blueswir1eda52952008-08-27 19:19:44 +0000932#ifndef TARGET_SPARC64
933 /* set wim so that restore will reload the registers */
bellard2623cba2005-02-19 17:25:31 +0000934 env->wim = 1 << cwp1;
blueswir1eda52952008-08-27 19:19:44 +0000935#endif
bellard2623cba2005-02-19 17:25:31 +0000936#if defined(DEBUG_WIN)
937 printf("flush_windows: nb=%d\n", offset - 1);
bellard80a9d032005-01-03 23:31:27 +0000938#endif
bellard2623cba2005-02-19 17:25:31 +0000939}
bellard060366c2004-01-04 15:50:01 +0000940
bellard93ac68b2003-09-30 20:57:29 +0000941void cpu_loop (CPUSPARCState *env)
942{
Richard Henderson2cc20262010-04-25 11:01:25 -0700943 int trapnr;
944 abi_long ret;
Anthony Liguoric227f092009-10-01 16:12:16 -0500945 target_siginfo_t info;
ths3b46e622007-09-17 08:09:54 +0000946
bellard060366c2004-01-04 15:50:01 +0000947 while (1) {
948 trapnr = cpu_sparc_exec (env);
ths3b46e622007-09-17 08:09:54 +0000949
bellard060366c2004-01-04 15:50:01 +0000950 switch (trapnr) {
bellard5ef54112006-07-18 21:14:09 +0000951#ifndef TARGET_SPARC64
ths5fafdf22007-09-16 21:08:06 +0000952 case 0x88:
bellard060366c2004-01-04 15:50:01 +0000953 case 0x90:
bellard5ef54112006-07-18 21:14:09 +0000954#else
blueswir1cb33da52007-10-09 16:34:29 +0000955 case 0x110:
bellard5ef54112006-07-18 21:14:09 +0000956 case 0x16d:
957#endif
bellard060366c2004-01-04 15:50:01 +0000958 ret = do_syscall (env, env->gregs[1],
ths5fafdf22007-09-16 21:08:06 +0000959 env->regwptr[0], env->regwptr[1],
960 env->regwptr[2], env->regwptr[3],
bellard060366c2004-01-04 15:50:01 +0000961 env->regwptr[4], env->regwptr[5]);
Richard Henderson2cc20262010-04-25 11:01:25 -0700962 if ((abi_ulong)ret >= (abi_ulong)(-515)) {
blueswir1992f48a2007-10-14 16:27:31 +0000963#if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
bellard27908722006-10-23 21:31:01 +0000964 env->xcc |= PSR_CARRY;
965#else
bellard060366c2004-01-04 15:50:01 +0000966 env->psr |= PSR_CARRY;
bellard27908722006-10-23 21:31:01 +0000967#endif
bellard060366c2004-01-04 15:50:01 +0000968 ret = -ret;
969 } else {
blueswir1992f48a2007-10-14 16:27:31 +0000970#if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
bellard27908722006-10-23 21:31:01 +0000971 env->xcc &= ~PSR_CARRY;
972#else
bellard060366c2004-01-04 15:50:01 +0000973 env->psr &= ~PSR_CARRY;
bellard27908722006-10-23 21:31:01 +0000974#endif
bellard060366c2004-01-04 15:50:01 +0000975 }
976 env->regwptr[0] = ret;
977 /* next instruction */
978 env->pc = env->npc;
979 env->npc = env->npc + 4;
980 break;
981 case 0x83: /* flush windows */
blueswir1992f48a2007-10-14 16:27:31 +0000982#ifdef TARGET_ABI32
983 case 0x103:
984#endif
bellard2623cba2005-02-19 17:25:31 +0000985 flush_windows(env);
bellard060366c2004-01-04 15:50:01 +0000986 /* next instruction */
987 env->pc = env->npc;
988 env->npc = env->npc + 4;
989 break;
bellard34751872005-07-02 14:31:34 +0000990#ifndef TARGET_SPARC64
bellard060366c2004-01-04 15:50:01 +0000991 case TT_WIN_OVF: /* window overflow */
992 save_window(env);
993 break;
994 case TT_WIN_UNF: /* window underflow */
995 restore_window(env);
996 break;
bellard61ff6f52005-02-15 22:54:53 +0000997 case TT_TFAULT:
998 case TT_DFAULT:
999 {
1000 info.si_signo = SIGSEGV;
1001 info.si_errno = 0;
1002 /* XXX: check env->error_code */
1003 info.si_code = TARGET_SEGV_MAPERR;
1004 info._sifields._sigfault._addr = env->mmuregs[4];
pbrook624f7972008-05-31 16:11:38 +00001005 queue_signal(env, info.si_signo, &info);
bellard61ff6f52005-02-15 22:54:53 +00001006 }
1007 break;
bellard34751872005-07-02 14:31:34 +00001008#else
bellard5ef54112006-07-18 21:14:09 +00001009 case TT_SPILL: /* window overflow */
1010 save_window(env);
1011 break;
1012 case TT_FILL: /* window underflow */
1013 restore_window(env);
1014 break;
blueswir17f84a722007-07-07 20:46:41 +00001015 case TT_TFAULT:
1016 case TT_DFAULT:
1017 {
1018 info.si_signo = SIGSEGV;
1019 info.si_errno = 0;
1020 /* XXX: check env->error_code */
1021 info.si_code = TARGET_SEGV_MAPERR;
1022 if (trapnr == TT_DFAULT)
1023 info._sifields._sigfault._addr = env->dmmuregs[4];
1024 else
Igor Kovalenko8194f352009-08-03 23:15:02 +04001025 info._sifields._sigfault._addr = cpu_tsptr(env)->tpc;
pbrook624f7972008-05-31 16:11:38 +00001026 queue_signal(env, info.si_signo, &info);
blueswir17f84a722007-07-07 20:46:41 +00001027 }
1028 break;
bellard27524dc2007-11-11 19:32:52 +00001029#ifndef TARGET_ABI32
blueswir15bfb56b2007-10-05 17:01:51 +00001030 case 0x16e:
1031 flush_windows(env);
1032 sparc64_get_context(env);
1033 break;
1034 case 0x16f:
1035 flush_windows(env);
1036 sparc64_set_context(env);
1037 break;
bellard34751872005-07-02 14:31:34 +00001038#endif
bellard27524dc2007-11-11 19:32:52 +00001039#endif
bellard48dc41e2006-06-21 18:15:50 +00001040 case EXCP_INTERRUPT:
1041 /* just indicate that signals should be handled asap */
1042 break;
bellard1fddef42005-04-17 19:16:13 +00001043 case EXCP_DEBUG:
1044 {
1045 int sig;
1046
1047 sig = gdb_handlesig (env, TARGET_SIGTRAP);
1048 if (sig)
1049 {
1050 info.si_signo = sig;
1051 info.si_errno = 0;
1052 info.si_code = TARGET_TRAP_BRKPT;
pbrook624f7972008-05-31 16:11:38 +00001053 queue_signal(env, info.si_signo, &info);
bellard1fddef42005-04-17 19:16:13 +00001054 }
1055 }
1056 break;
bellard060366c2004-01-04 15:50:01 +00001057 default:
1058 printf ("Unhandled trap: 0x%x\n", trapnr);
bellard7fe48482004-10-09 18:08:01 +00001059 cpu_dump_state(env, stderr, fprintf, 0);
bellard060366c2004-01-04 15:50:01 +00001060 exit (1);
1061 }
1062 process_pending_signals (env);
1063 }
bellard93ac68b2003-09-30 20:57:29 +00001064}
1065
1066#endif
1067
bellard67867302003-11-23 17:05:30 +00001068#ifdef TARGET_PPC
bellard9fddaa02004-05-21 12:59:32 +00001069static inline uint64_t cpu_ppc_get_tb (CPUState *env)
1070{
1071 /* TO FIX */
1072 return 0;
1073}
ths3b46e622007-09-17 08:09:54 +00001074
Alexander Grafe3ea6522009-12-21 12:24:17 +01001075uint64_t cpu_ppc_load_tbl (CPUState *env)
bellard9fddaa02004-05-21 12:59:32 +00001076{
Alexander Grafe3ea6522009-12-21 12:24:17 +01001077 return cpu_ppc_get_tb(env);
bellard9fddaa02004-05-21 12:59:32 +00001078}
ths3b46e622007-09-17 08:09:54 +00001079
bellard9fddaa02004-05-21 12:59:32 +00001080uint32_t cpu_ppc_load_tbu (CPUState *env)
1081{
1082 return cpu_ppc_get_tb(env) >> 32;
1083}
ths3b46e622007-09-17 08:09:54 +00001084
Aurelien Jarnob711de92009-12-21 13:52:08 +01001085uint64_t cpu_ppc_load_atbl (CPUState *env)
bellard9fddaa02004-05-21 12:59:32 +00001086{
Aurelien Jarnob711de92009-12-21 13:52:08 +01001087 return cpu_ppc_get_tb(env);
bellard9fddaa02004-05-21 12:59:32 +00001088}
1089
j_mayera062e362007-09-30 00:38:38 +00001090uint32_t cpu_ppc_load_atbu (CPUState *env)
bellard9fddaa02004-05-21 12:59:32 +00001091{
j_mayera062e362007-09-30 00:38:38 +00001092 return cpu_ppc_get_tb(env) >> 32;
bellard9fddaa02004-05-21 12:59:32 +00001093}
ths5fafdf22007-09-16 21:08:06 +00001094
j_mayer76a66252007-03-07 08:32:30 +00001095uint32_t cpu_ppc601_load_rtcu (CPUState *env)
1096__attribute__ (( alias ("cpu_ppc_load_tbu") ));
1097
j_mayer76a66252007-03-07 08:32:30 +00001098uint32_t cpu_ppc601_load_rtcl (CPUState *env)
bellard9fddaa02004-05-21 12:59:32 +00001099{
j_mayer76a66252007-03-07 08:32:30 +00001100 return cpu_ppc_load_tbl(env) & 0x3FFFFF80;
bellard9fddaa02004-05-21 12:59:32 +00001101}
j_mayer76a66252007-03-07 08:32:30 +00001102
j_mayera750fc02007-09-26 23:54:22 +00001103/* XXX: to be fixed */
Alexander Graf73b01962009-12-21 14:02:39 +01001104int ppc_dcr_read (ppc_dcr_t *dcr_env, int dcrn, uint32_t *valp)
j_mayera750fc02007-09-26 23:54:22 +00001105{
1106 return -1;
1107}
1108
Alexander Graf73b01962009-12-21 14:02:39 +01001109int ppc_dcr_write (ppc_dcr_t *dcr_env, int dcrn, uint32_t val)
j_mayera750fc02007-09-26 23:54:22 +00001110{
1111 return -1;
1112}
1113
Blue Swirl001faf32009-05-13 17:53:17 +00001114#define EXCP_DUMP(env, fmt, ...) \
1115do { \
1116 fprintf(stderr, fmt , ## __VA_ARGS__); \
1117 cpu_dump_state(env, stderr, fprintf, 0); \
1118 qemu_log(fmt, ## __VA_ARGS__); \
malc430c7ec2009-07-15 20:52:47 +04001119 if (logfile) \
1120 log_cpu_state(env, 0); \
j_mayere1833e12007-09-29 13:06:16 +00001121} while (0)
1122
Nathan Froyd56f066b2009-08-03 08:43:27 -07001123static int do_store_exclusive(CPUPPCState *env)
1124{
1125 target_ulong addr;
1126 target_ulong page_addr;
1127 target_ulong val;
1128 int flags;
1129 int segv = 0;
1130
1131 addr = env->reserve_ea;
1132 page_addr = addr & TARGET_PAGE_MASK;
1133 start_exclusive();
1134 mmap_lock();
1135 flags = page_get_flags(page_addr);
1136 if ((flags & PAGE_READ) == 0) {
1137 segv = 1;
1138 } else {
1139 int reg = env->reserve_info & 0x1f;
1140 int size = (env->reserve_info >> 5) & 0xf;
1141 int stored = 0;
1142
1143 if (addr == env->reserve_addr) {
1144 switch (size) {
1145 case 1: segv = get_user_u8(val, addr); break;
1146 case 2: segv = get_user_u16(val, addr); break;
1147 case 4: segv = get_user_u32(val, addr); break;
1148#if defined(TARGET_PPC64)
1149 case 8: segv = get_user_u64(val, addr); break;
1150#endif
1151 default: abort();
1152 }
1153 if (!segv && val == env->reserve_val) {
1154 val = env->gpr[reg];
1155 switch (size) {
1156 case 1: segv = put_user_u8(val, addr); break;
1157 case 2: segv = put_user_u16(val, addr); break;
1158 case 4: segv = put_user_u32(val, addr); break;
1159#if defined(TARGET_PPC64)
1160 case 8: segv = put_user_u64(val, addr); break;
1161#endif
1162 default: abort();
1163 }
1164 if (!segv) {
1165 stored = 1;
1166 }
1167 }
1168 }
1169 env->crf[0] = (stored << 1) | xer_so;
1170 env->reserve_addr = (target_ulong)-1;
1171 }
1172 if (!segv) {
1173 env->nip += 4;
1174 }
1175 mmap_unlock();
1176 end_exclusive();
1177 return segv;
1178}
1179
bellard67867302003-11-23 17:05:30 +00001180void cpu_loop(CPUPPCState *env)
1181{
Anthony Liguoric227f092009-10-01 16:12:16 -05001182 target_siginfo_t info;
bellard61190b12004-01-04 23:54:31 +00001183 int trapnr;
1184 uint32_t ret;
ths3b46e622007-09-17 08:09:54 +00001185
bellard67867302003-11-23 17:05:30 +00001186 for(;;) {
Nathan Froyd56f066b2009-08-03 08:43:27 -07001187 cpu_exec_start(env);
bellard67867302003-11-23 17:05:30 +00001188 trapnr = cpu_ppc_exec(env);
Nathan Froyd56f066b2009-08-03 08:43:27 -07001189 cpu_exec_end(env);
bellard67867302003-11-23 17:05:30 +00001190 switch(trapnr) {
j_mayere1833e12007-09-29 13:06:16 +00001191 case POWERPC_EXCP_NONE:
1192 /* Just go on */
bellard67867302003-11-23 17:05:30 +00001193 break;
j_mayere1833e12007-09-29 13:06:16 +00001194 case POWERPC_EXCP_CRITICAL: /* Critical input */
1195 cpu_abort(env, "Critical interrupt while in user mode. "
1196 "Aborting\n");
1197 break;
1198 case POWERPC_EXCP_MCHECK: /* Machine check exception */
1199 cpu_abort(env, "Machine check exception while in user mode. "
1200 "Aborting\n");
1201 break;
1202 case POWERPC_EXCP_DSI: /* Data storage exception */
Blue Swirl90e189e2009-08-16 11:13:18 +00001203 EXCP_DUMP(env, "Invalid data memory access: 0x" TARGET_FMT_lx "\n",
j_mayere1833e12007-09-29 13:06:16 +00001204 env->spr[SPR_DAR]);
1205 /* XXX: check this. Seems bugged */
1206 switch (env->error_code & 0xFF000000) {
1207 case 0x40000000:
1208 info.si_signo = TARGET_SIGSEGV;
1209 info.si_errno = 0;
1210 info.si_code = TARGET_SEGV_MAPERR;
1211 break;
1212 case 0x04000000:
1213 info.si_signo = TARGET_SIGILL;
1214 info.si_errno = 0;
1215 info.si_code = TARGET_ILL_ILLADR;
1216 break;
1217 case 0x08000000:
1218 info.si_signo = TARGET_SIGSEGV;
1219 info.si_errno = 0;
1220 info.si_code = TARGET_SEGV_ACCERR;
1221 break;
1222 default:
1223 /* Let's send a regular segfault... */
1224 EXCP_DUMP(env, "Invalid segfault errno (%02x)\n",
1225 env->error_code);
1226 info.si_signo = TARGET_SIGSEGV;
1227 info.si_errno = 0;
1228 info.si_code = TARGET_SEGV_MAPERR;
1229 break;
1230 }
1231 info._sifields._sigfault._addr = env->nip;
pbrook624f7972008-05-31 16:11:38 +00001232 queue_signal(env, info.si_signo, &info);
j_mayere1833e12007-09-29 13:06:16 +00001233 break;
1234 case POWERPC_EXCP_ISI: /* Instruction storage exception */
Blue Swirl90e189e2009-08-16 11:13:18 +00001235 EXCP_DUMP(env, "Invalid instruction fetch: 0x\n" TARGET_FMT_lx
1236 "\n", env->spr[SPR_SRR0]);
j_mayere1833e12007-09-29 13:06:16 +00001237 /* XXX: check this */
1238 switch (env->error_code & 0xFF000000) {
1239 case 0x40000000:
1240 info.si_signo = TARGET_SIGSEGV;
1241 info.si_errno = 0;
1242 info.si_code = TARGET_SEGV_MAPERR;
1243 break;
1244 case 0x10000000:
1245 case 0x08000000:
1246 info.si_signo = TARGET_SIGSEGV;
1247 info.si_errno = 0;
1248 info.si_code = TARGET_SEGV_ACCERR;
1249 break;
1250 default:
1251 /* Let's send a regular segfault... */
1252 EXCP_DUMP(env, "Invalid segfault errno (%02x)\n",
1253 env->error_code);
1254 info.si_signo = TARGET_SIGSEGV;
1255 info.si_errno = 0;
1256 info.si_code = TARGET_SEGV_MAPERR;
1257 break;
1258 }
1259 info._sifields._sigfault._addr = env->nip - 4;
pbrook624f7972008-05-31 16:11:38 +00001260 queue_signal(env, info.si_signo, &info);
j_mayere1833e12007-09-29 13:06:16 +00001261 break;
1262 case POWERPC_EXCP_EXTERNAL: /* External input */
1263 cpu_abort(env, "External interrupt while in user mode. "
1264 "Aborting\n");
1265 break;
1266 case POWERPC_EXCP_ALIGN: /* Alignment exception */
1267 EXCP_DUMP(env, "Unaligned memory access\n");
1268 /* XXX: check this */
1269 info.si_signo = TARGET_SIGBUS;
1270 info.si_errno = 0;
1271 info.si_code = TARGET_BUS_ADRALN;
1272 info._sifields._sigfault._addr = env->nip - 4;
pbrook624f7972008-05-31 16:11:38 +00001273 queue_signal(env, info.si_signo, &info);
j_mayere1833e12007-09-29 13:06:16 +00001274 break;
1275 case POWERPC_EXCP_PROGRAM: /* Program exception */
1276 /* XXX: check this */
1277 switch (env->error_code & ~0xF) {
1278 case POWERPC_EXCP_FP:
1279 EXCP_DUMP(env, "Floating point program exception\n");
j_mayere1833e12007-09-29 13:06:16 +00001280 info.si_signo = TARGET_SIGFPE;
1281 info.si_errno = 0;
1282 switch (env->error_code & 0xF) {
1283 case POWERPC_EXCP_FP_OX:
1284 info.si_code = TARGET_FPE_FLTOVF;
1285 break;
1286 case POWERPC_EXCP_FP_UX:
1287 info.si_code = TARGET_FPE_FLTUND;
1288 break;
1289 case POWERPC_EXCP_FP_ZX:
1290 case POWERPC_EXCP_FP_VXZDZ:
1291 info.si_code = TARGET_FPE_FLTDIV;
1292 break;
1293 case POWERPC_EXCP_FP_XX:
1294 info.si_code = TARGET_FPE_FLTRES;
1295 break;
1296 case POWERPC_EXCP_FP_VXSOFT:
1297 info.si_code = TARGET_FPE_FLTINV;
1298 break;
j_mayer7c580442007-10-27 17:54:30 +00001299 case POWERPC_EXCP_FP_VXSNAN:
j_mayere1833e12007-09-29 13:06:16 +00001300 case POWERPC_EXCP_FP_VXISI:
1301 case POWERPC_EXCP_FP_VXIDI:
1302 case POWERPC_EXCP_FP_VXIMZ:
1303 case POWERPC_EXCP_FP_VXVC:
1304 case POWERPC_EXCP_FP_VXSQRT:
1305 case POWERPC_EXCP_FP_VXCVI:
1306 info.si_code = TARGET_FPE_FLTSUB;
1307 break;
1308 default:
1309 EXCP_DUMP(env, "Unknown floating point exception (%02x)\n",
1310 env->error_code);
1311 break;
1312 }
1313 break;
1314 case POWERPC_EXCP_INVAL:
1315 EXCP_DUMP(env, "Invalid instruction\n");
1316 info.si_signo = TARGET_SIGILL;
1317 info.si_errno = 0;
1318 switch (env->error_code & 0xF) {
1319 case POWERPC_EXCP_INVAL_INVAL:
1320 info.si_code = TARGET_ILL_ILLOPC;
1321 break;
1322 case POWERPC_EXCP_INVAL_LSWX:
1323 info.si_code = TARGET_ILL_ILLOPN;
1324 break;
1325 case POWERPC_EXCP_INVAL_SPR:
1326 info.si_code = TARGET_ILL_PRVREG;
1327 break;
1328 case POWERPC_EXCP_INVAL_FP:
1329 info.si_code = TARGET_ILL_COPROC;
1330 break;
1331 default:
1332 EXCP_DUMP(env, "Unknown invalid operation (%02x)\n",
1333 env->error_code & 0xF);
1334 info.si_code = TARGET_ILL_ILLADR;
1335 break;
1336 }
1337 break;
1338 case POWERPC_EXCP_PRIV:
1339 EXCP_DUMP(env, "Privilege violation\n");
1340 info.si_signo = TARGET_SIGILL;
1341 info.si_errno = 0;
1342 switch (env->error_code & 0xF) {
1343 case POWERPC_EXCP_PRIV_OPC:
1344 info.si_code = TARGET_ILL_PRVOPC;
1345 break;
1346 case POWERPC_EXCP_PRIV_REG:
1347 info.si_code = TARGET_ILL_PRVREG;
1348 break;
1349 default:
1350 EXCP_DUMP(env, "Unknown privilege violation (%02x)\n",
1351 env->error_code & 0xF);
1352 info.si_code = TARGET_ILL_PRVOPC;
1353 break;
1354 }
1355 break;
1356 case POWERPC_EXCP_TRAP:
1357 cpu_abort(env, "Tried to call a TRAP\n");
1358 break;
1359 default:
1360 /* Should not happen ! */
1361 cpu_abort(env, "Unknown program exception (%02x)\n",
1362 env->error_code);
1363 break;
1364 }
1365 info._sifields._sigfault._addr = env->nip - 4;
pbrook624f7972008-05-31 16:11:38 +00001366 queue_signal(env, info.si_signo, &info);
j_mayere1833e12007-09-29 13:06:16 +00001367 break;
1368 case POWERPC_EXCP_FPU: /* Floating-point unavailable exception */
1369 EXCP_DUMP(env, "No floating point allowed\n");
1370 info.si_signo = TARGET_SIGILL;
1371 info.si_errno = 0;
1372 info.si_code = TARGET_ILL_COPROC;
1373 info._sifields._sigfault._addr = env->nip - 4;
pbrook624f7972008-05-31 16:11:38 +00001374 queue_signal(env, info.si_signo, &info);
j_mayere1833e12007-09-29 13:06:16 +00001375 break;
1376 case POWERPC_EXCP_SYSCALL: /* System call exception */
1377 cpu_abort(env, "Syscall exception while in user mode. "
1378 "Aborting\n");
1379 break;
1380 case POWERPC_EXCP_APU: /* Auxiliary processor unavailable */
1381 EXCP_DUMP(env, "No APU instruction allowed\n");
1382 info.si_signo = TARGET_SIGILL;
1383 info.si_errno = 0;
1384 info.si_code = TARGET_ILL_COPROC;
1385 info._sifields._sigfault._addr = env->nip - 4;
pbrook624f7972008-05-31 16:11:38 +00001386 queue_signal(env, info.si_signo, &info);
j_mayere1833e12007-09-29 13:06:16 +00001387 break;
1388 case POWERPC_EXCP_DECR: /* Decrementer exception */
1389 cpu_abort(env, "Decrementer interrupt while in user mode. "
1390 "Aborting\n");
1391 break;
1392 case POWERPC_EXCP_FIT: /* Fixed-interval timer interrupt */
1393 cpu_abort(env, "Fix interval timer interrupt while in user mode. "
1394 "Aborting\n");
1395 break;
1396 case POWERPC_EXCP_WDT: /* Watchdog timer interrupt */
1397 cpu_abort(env, "Watchdog timer interrupt while in user mode. "
1398 "Aborting\n");
1399 break;
1400 case POWERPC_EXCP_DTLB: /* Data TLB error */
1401 cpu_abort(env, "Data TLB exception while in user mode. "
1402 "Aborting\n");
1403 break;
1404 case POWERPC_EXCP_ITLB: /* Instruction TLB error */
1405 cpu_abort(env, "Instruction TLB exception while in user mode. "
1406 "Aborting\n");
1407 break;
j_mayere1833e12007-09-29 13:06:16 +00001408 case POWERPC_EXCP_SPEU: /* SPE/embedded floating-point unavail. */
1409 EXCP_DUMP(env, "No SPE/floating-point instruction allowed\n");
1410 info.si_signo = TARGET_SIGILL;
1411 info.si_errno = 0;
1412 info.si_code = TARGET_ILL_COPROC;
1413 info._sifields._sigfault._addr = env->nip - 4;
pbrook624f7972008-05-31 16:11:38 +00001414 queue_signal(env, info.si_signo, &info);
j_mayere1833e12007-09-29 13:06:16 +00001415 break;
1416 case POWERPC_EXCP_EFPDI: /* Embedded floating-point data IRQ */
1417 cpu_abort(env, "Embedded floating-point data IRQ not handled\n");
1418 break;
1419 case POWERPC_EXCP_EFPRI: /* Embedded floating-point round IRQ */
1420 cpu_abort(env, "Embedded floating-point round IRQ not handled\n");
1421 break;
1422 case POWERPC_EXCP_EPERFM: /* Embedded performance monitor IRQ */
1423 cpu_abort(env, "Performance monitor exception not handled\n");
1424 break;
1425 case POWERPC_EXCP_DOORI: /* Embedded doorbell interrupt */
1426 cpu_abort(env, "Doorbell interrupt while in user mode. "
1427 "Aborting\n");
1428 break;
1429 case POWERPC_EXCP_DOORCI: /* Embedded doorbell critical interrupt */
1430 cpu_abort(env, "Doorbell critical interrupt while in user mode. "
1431 "Aborting\n");
1432 break;
1433 case POWERPC_EXCP_RESET: /* System reset exception */
1434 cpu_abort(env, "Reset interrupt while in user mode. "
1435 "Aborting\n");
1436 break;
j_mayere1833e12007-09-29 13:06:16 +00001437 case POWERPC_EXCP_DSEG: /* Data segment exception */
1438 cpu_abort(env, "Data segment exception while in user mode. "
1439 "Aborting\n");
1440 break;
1441 case POWERPC_EXCP_ISEG: /* Instruction segment exception */
1442 cpu_abort(env, "Instruction segment exception "
1443 "while in user mode. Aborting\n");
1444 break;
j_mayere85e7c62007-10-18 19:59:49 +00001445 /* PowerPC 64 with hypervisor mode support */
j_mayere1833e12007-09-29 13:06:16 +00001446 case POWERPC_EXCP_HDECR: /* Hypervisor decrementer exception */
1447 cpu_abort(env, "Hypervisor decrementer interrupt "
1448 "while in user mode. Aborting\n");
1449 break;
j_mayere1833e12007-09-29 13:06:16 +00001450 case POWERPC_EXCP_TRACE: /* Trace exception */
1451 /* Nothing to do:
1452 * we use this exception to emulate step-by-step execution mode.
1453 */
1454 break;
j_mayere85e7c62007-10-18 19:59:49 +00001455 /* PowerPC 64 with hypervisor mode support */
j_mayere1833e12007-09-29 13:06:16 +00001456 case POWERPC_EXCP_HDSI: /* Hypervisor data storage exception */
1457 cpu_abort(env, "Hypervisor data storage exception "
1458 "while in user mode. Aborting\n");
1459 break;
1460 case POWERPC_EXCP_HISI: /* Hypervisor instruction storage excp */
1461 cpu_abort(env, "Hypervisor instruction storage exception "
1462 "while in user mode. Aborting\n");
1463 break;
1464 case POWERPC_EXCP_HDSEG: /* Hypervisor data segment exception */
1465 cpu_abort(env, "Hypervisor data segment exception "
1466 "while in user mode. Aborting\n");
1467 break;
1468 case POWERPC_EXCP_HISEG: /* Hypervisor instruction segment excp */
1469 cpu_abort(env, "Hypervisor instruction segment exception "
1470 "while in user mode. Aborting\n");
1471 break;
j_mayere1833e12007-09-29 13:06:16 +00001472 case POWERPC_EXCP_VPU: /* Vector unavailable exception */
1473 EXCP_DUMP(env, "No Altivec instructions allowed\n");
1474 info.si_signo = TARGET_SIGILL;
1475 info.si_errno = 0;
1476 info.si_code = TARGET_ILL_COPROC;
1477 info._sifields._sigfault._addr = env->nip - 4;
pbrook624f7972008-05-31 16:11:38 +00001478 queue_signal(env, info.si_signo, &info);
j_mayere1833e12007-09-29 13:06:16 +00001479 break;
1480 case POWERPC_EXCP_PIT: /* Programmable interval timer IRQ */
1481 cpu_abort(env, "Programable interval timer interrupt "
1482 "while in user mode. Aborting\n");
1483 break;
1484 case POWERPC_EXCP_IO: /* IO error exception */
1485 cpu_abort(env, "IO error exception while in user mode. "
1486 "Aborting\n");
1487 break;
1488 case POWERPC_EXCP_RUNM: /* Run mode exception */
1489 cpu_abort(env, "Run mode exception while in user mode. "
1490 "Aborting\n");
1491 break;
1492 case POWERPC_EXCP_EMUL: /* Emulation trap exception */
1493 cpu_abort(env, "Emulation trap exception not handled\n");
1494 break;
1495 case POWERPC_EXCP_IFTLB: /* Instruction fetch TLB error */
1496 cpu_abort(env, "Instruction fetch TLB exception "
1497 "while in user-mode. Aborting");
1498 break;
1499 case POWERPC_EXCP_DLTLB: /* Data load TLB miss */
1500 cpu_abort(env, "Data load TLB exception while in user-mode. "
1501 "Aborting");
1502 break;
1503 case POWERPC_EXCP_DSTLB: /* Data store TLB miss */
1504 cpu_abort(env, "Data store TLB exception while in user-mode. "
1505 "Aborting");
1506 break;
1507 case POWERPC_EXCP_FPA: /* Floating-point assist exception */
1508 cpu_abort(env, "Floating-point assist exception not handled\n");
1509 break;
1510 case POWERPC_EXCP_IABR: /* Instruction address breakpoint */
1511 cpu_abort(env, "Instruction address breakpoint exception "
1512 "not handled\n");
1513 break;
1514 case POWERPC_EXCP_SMI: /* System management interrupt */
1515 cpu_abort(env, "System management interrupt while in user mode. "
1516 "Aborting\n");
1517 break;
1518 case POWERPC_EXCP_THERM: /* Thermal interrupt */
1519 cpu_abort(env, "Thermal interrupt interrupt while in user mode. "
1520 "Aborting\n");
1521 break;
1522 case POWERPC_EXCP_PERFM: /* Embedded performance monitor IRQ */
1523 cpu_abort(env, "Performance monitor exception not handled\n");
1524 break;
1525 case POWERPC_EXCP_VPUA: /* Vector assist exception */
1526 cpu_abort(env, "Vector assist exception not handled\n");
1527 break;
1528 case POWERPC_EXCP_SOFTP: /* Soft patch exception */
1529 cpu_abort(env, "Soft patch exception not handled\n");
1530 break;
1531 case POWERPC_EXCP_MAINT: /* Maintenance exception */
1532 cpu_abort(env, "Maintenance exception while in user mode. "
1533 "Aborting\n");
1534 break;
1535 case POWERPC_EXCP_STOP: /* stop translation */
1536 /* We did invalidate the instruction cache. Go on */
1537 break;
1538 case POWERPC_EXCP_BRANCH: /* branch instruction: */
1539 /* We just stopped because of a branch. Go on */
1540 break;
1541 case POWERPC_EXCP_SYSCALL_USER:
1542 /* system call in user-mode emulation */
bellard67867302003-11-23 17:05:30 +00001543 /* WARNING:
1544 * PPC ABI uses overflow flag in cr0 to signal an error
1545 * in syscalls.
1546 */
bellard61190b12004-01-04 23:54:31 +00001547#if 0
1548 printf("syscall %d 0x%08x 0x%08x 0x%08x 0x%08x\n", env->gpr[0],
1549 env->gpr[3], env->gpr[4], env->gpr[5], env->gpr[6]);
1550#endif
bellard67867302003-11-23 17:05:30 +00001551 env->crf[0] &= ~0x1;
1552 ret = do_syscall(env, env->gpr[0], env->gpr[3], env->gpr[4],
1553 env->gpr[5], env->gpr[6], env->gpr[7],
1554 env->gpr[8]);
Nathan Froydbcd49332009-05-12 19:13:18 -07001555 if (ret == (uint32_t)(-TARGET_QEMU_ESIGRETURN)) {
1556 /* Returning from a successful sigreturn syscall.
1557 Avoid corrupting register state. */
1558 break;
1559 }
bellard67867302003-11-23 17:05:30 +00001560 if (ret > (uint32_t)(-515)) {
1561 env->crf[0] |= 0x1;
1562 ret = -ret;
1563 }
1564 env->gpr[3] = ret;
bellard61190b12004-01-04 23:54:31 +00001565#if 0
1566 printf("syscall returned 0x%08x (%d)\n", ret, ret);
1567#endif
bellard67867302003-11-23 17:05:30 +00001568 break;
Nathan Froyd56f066b2009-08-03 08:43:27 -07001569 case POWERPC_EXCP_STCX:
1570 if (do_store_exclusive(env)) {
1571 info.si_signo = TARGET_SIGSEGV;
1572 info.si_errno = 0;
1573 info.si_code = TARGET_SEGV_MAPERR;
1574 info._sifields._sigfault._addr = env->nip;
1575 queue_signal(env, info.si_signo, &info);
1576 }
1577 break;
aurel3271f75752008-11-14 17:05:54 +00001578 case EXCP_DEBUG:
1579 {
1580 int sig;
1581
1582 sig = gdb_handlesig(env, TARGET_SIGTRAP);
1583 if (sig) {
1584 info.si_signo = sig;
1585 info.si_errno = 0;
1586 info.si_code = TARGET_TRAP_BRKPT;
1587 queue_signal(env, info.si_signo, &info);
1588 }
1589 }
1590 break;
j_mayer56ba31f2007-09-30 15:15:18 +00001591 case EXCP_INTERRUPT:
1592 /* just indicate that signals should be handled asap */
1593 break;
bellard67867302003-11-23 17:05:30 +00001594 default:
j_mayere1833e12007-09-29 13:06:16 +00001595 cpu_abort(env, "Unknown exception 0x%d. Aborting\n", trapnr);
1596 break;
bellard67867302003-11-23 17:05:30 +00001597 }
1598 process_pending_signals(env);
1599 }
1600}
1601#endif
1602
bellard048f6b42005-11-26 18:47:20 +00001603#ifdef TARGET_MIPS
1604
1605#define MIPS_SYS(name, args) args,
1606
1607static const uint8_t mips_syscall_args[] = {
1608 MIPS_SYS(sys_syscall , 0) /* 4000 */
1609 MIPS_SYS(sys_exit , 1)
1610 MIPS_SYS(sys_fork , 0)
1611 MIPS_SYS(sys_read , 3)
1612 MIPS_SYS(sys_write , 3)
1613 MIPS_SYS(sys_open , 3) /* 4005 */
1614 MIPS_SYS(sys_close , 1)
1615 MIPS_SYS(sys_waitpid , 3)
1616 MIPS_SYS(sys_creat , 2)
1617 MIPS_SYS(sys_link , 2)
1618 MIPS_SYS(sys_unlink , 1) /* 4010 */
1619 MIPS_SYS(sys_execve , 0)
1620 MIPS_SYS(sys_chdir , 1)
1621 MIPS_SYS(sys_time , 1)
1622 MIPS_SYS(sys_mknod , 3)
1623 MIPS_SYS(sys_chmod , 2) /* 4015 */
1624 MIPS_SYS(sys_lchown , 3)
1625 MIPS_SYS(sys_ni_syscall , 0)
1626 MIPS_SYS(sys_ni_syscall , 0) /* was sys_stat */
1627 MIPS_SYS(sys_lseek , 3)
1628 MIPS_SYS(sys_getpid , 0) /* 4020 */
1629 MIPS_SYS(sys_mount , 5)
1630 MIPS_SYS(sys_oldumount , 1)
1631 MIPS_SYS(sys_setuid , 1)
1632 MIPS_SYS(sys_getuid , 0)
1633 MIPS_SYS(sys_stime , 1) /* 4025 */
1634 MIPS_SYS(sys_ptrace , 4)
1635 MIPS_SYS(sys_alarm , 1)
1636 MIPS_SYS(sys_ni_syscall , 0) /* was sys_fstat */
1637 MIPS_SYS(sys_pause , 0)
1638 MIPS_SYS(sys_utime , 2) /* 4030 */
1639 MIPS_SYS(sys_ni_syscall , 0)
1640 MIPS_SYS(sys_ni_syscall , 0)
1641 MIPS_SYS(sys_access , 2)
1642 MIPS_SYS(sys_nice , 1)
1643 MIPS_SYS(sys_ni_syscall , 0) /* 4035 */
1644 MIPS_SYS(sys_sync , 0)
1645 MIPS_SYS(sys_kill , 2)
1646 MIPS_SYS(sys_rename , 2)
1647 MIPS_SYS(sys_mkdir , 2)
1648 MIPS_SYS(sys_rmdir , 1) /* 4040 */
1649 MIPS_SYS(sys_dup , 1)
1650 MIPS_SYS(sys_pipe , 0)
1651 MIPS_SYS(sys_times , 1)
1652 MIPS_SYS(sys_ni_syscall , 0)
1653 MIPS_SYS(sys_brk , 1) /* 4045 */
1654 MIPS_SYS(sys_setgid , 1)
1655 MIPS_SYS(sys_getgid , 0)
1656 MIPS_SYS(sys_ni_syscall , 0) /* was signal(2) */
1657 MIPS_SYS(sys_geteuid , 0)
1658 MIPS_SYS(sys_getegid , 0) /* 4050 */
1659 MIPS_SYS(sys_acct , 0)
1660 MIPS_SYS(sys_umount , 2)
1661 MIPS_SYS(sys_ni_syscall , 0)
1662 MIPS_SYS(sys_ioctl , 3)
1663 MIPS_SYS(sys_fcntl , 3) /* 4055 */
1664 MIPS_SYS(sys_ni_syscall , 2)
1665 MIPS_SYS(sys_setpgid , 2)
1666 MIPS_SYS(sys_ni_syscall , 0)
1667 MIPS_SYS(sys_olduname , 1)
1668 MIPS_SYS(sys_umask , 1) /* 4060 */
1669 MIPS_SYS(sys_chroot , 1)
1670 MIPS_SYS(sys_ustat , 2)
1671 MIPS_SYS(sys_dup2 , 2)
1672 MIPS_SYS(sys_getppid , 0)
1673 MIPS_SYS(sys_getpgrp , 0) /* 4065 */
1674 MIPS_SYS(sys_setsid , 0)
1675 MIPS_SYS(sys_sigaction , 3)
1676 MIPS_SYS(sys_sgetmask , 0)
1677 MIPS_SYS(sys_ssetmask , 1)
1678 MIPS_SYS(sys_setreuid , 2) /* 4070 */
1679 MIPS_SYS(sys_setregid , 2)
1680 MIPS_SYS(sys_sigsuspend , 0)
1681 MIPS_SYS(sys_sigpending , 1)
1682 MIPS_SYS(sys_sethostname , 2)
1683 MIPS_SYS(sys_setrlimit , 2) /* 4075 */
1684 MIPS_SYS(sys_getrlimit , 2)
1685 MIPS_SYS(sys_getrusage , 2)
1686 MIPS_SYS(sys_gettimeofday, 2)
1687 MIPS_SYS(sys_settimeofday, 2)
1688 MIPS_SYS(sys_getgroups , 2) /* 4080 */
1689 MIPS_SYS(sys_setgroups , 2)
1690 MIPS_SYS(sys_ni_syscall , 0) /* old_select */
1691 MIPS_SYS(sys_symlink , 2)
1692 MIPS_SYS(sys_ni_syscall , 0) /* was sys_lstat */
1693 MIPS_SYS(sys_readlink , 3) /* 4085 */
1694 MIPS_SYS(sys_uselib , 1)
1695 MIPS_SYS(sys_swapon , 2)
1696 MIPS_SYS(sys_reboot , 3)
1697 MIPS_SYS(old_readdir , 3)
1698 MIPS_SYS(old_mmap , 6) /* 4090 */
1699 MIPS_SYS(sys_munmap , 2)
1700 MIPS_SYS(sys_truncate , 2)
1701 MIPS_SYS(sys_ftruncate , 2)
1702 MIPS_SYS(sys_fchmod , 2)
1703 MIPS_SYS(sys_fchown , 3) /* 4095 */
1704 MIPS_SYS(sys_getpriority , 2)
1705 MIPS_SYS(sys_setpriority , 3)
1706 MIPS_SYS(sys_ni_syscall , 0)
1707 MIPS_SYS(sys_statfs , 2)
1708 MIPS_SYS(sys_fstatfs , 2) /* 4100 */
1709 MIPS_SYS(sys_ni_syscall , 0) /* was ioperm(2) */
1710 MIPS_SYS(sys_socketcall , 2)
1711 MIPS_SYS(sys_syslog , 3)
1712 MIPS_SYS(sys_setitimer , 3)
1713 MIPS_SYS(sys_getitimer , 2) /* 4105 */
1714 MIPS_SYS(sys_newstat , 2)
1715 MIPS_SYS(sys_newlstat , 2)
1716 MIPS_SYS(sys_newfstat , 2)
1717 MIPS_SYS(sys_uname , 1)
1718 MIPS_SYS(sys_ni_syscall , 0) /* 4110 was iopl(2) */
1719 MIPS_SYS(sys_vhangup , 0)
1720 MIPS_SYS(sys_ni_syscall , 0) /* was sys_idle() */
1721 MIPS_SYS(sys_ni_syscall , 0) /* was sys_vm86 */
1722 MIPS_SYS(sys_wait4 , 4)
1723 MIPS_SYS(sys_swapoff , 1) /* 4115 */
1724 MIPS_SYS(sys_sysinfo , 1)
1725 MIPS_SYS(sys_ipc , 6)
1726 MIPS_SYS(sys_fsync , 1)
1727 MIPS_SYS(sys_sigreturn , 0)
Paul Brook18113962009-07-09 13:11:52 +01001728 MIPS_SYS(sys_clone , 6) /* 4120 */
bellard048f6b42005-11-26 18:47:20 +00001729 MIPS_SYS(sys_setdomainname, 2)
1730 MIPS_SYS(sys_newuname , 1)
1731 MIPS_SYS(sys_ni_syscall , 0) /* sys_modify_ldt */
1732 MIPS_SYS(sys_adjtimex , 1)
1733 MIPS_SYS(sys_mprotect , 3) /* 4125 */
1734 MIPS_SYS(sys_sigprocmask , 3)
1735 MIPS_SYS(sys_ni_syscall , 0) /* was create_module */
1736 MIPS_SYS(sys_init_module , 5)
1737 MIPS_SYS(sys_delete_module, 1)
1738 MIPS_SYS(sys_ni_syscall , 0) /* 4130 was get_kernel_syms */
1739 MIPS_SYS(sys_quotactl , 0)
1740 MIPS_SYS(sys_getpgid , 1)
1741 MIPS_SYS(sys_fchdir , 1)
1742 MIPS_SYS(sys_bdflush , 2)
1743 MIPS_SYS(sys_sysfs , 3) /* 4135 */
1744 MIPS_SYS(sys_personality , 1)
1745 MIPS_SYS(sys_ni_syscall , 0) /* for afs_syscall */
1746 MIPS_SYS(sys_setfsuid , 1)
1747 MIPS_SYS(sys_setfsgid , 1)
1748 MIPS_SYS(sys_llseek , 5) /* 4140 */
1749 MIPS_SYS(sys_getdents , 3)
1750 MIPS_SYS(sys_select , 5)
1751 MIPS_SYS(sys_flock , 2)
1752 MIPS_SYS(sys_msync , 3)
1753 MIPS_SYS(sys_readv , 3) /* 4145 */
1754 MIPS_SYS(sys_writev , 3)
1755 MIPS_SYS(sys_cacheflush , 3)
1756 MIPS_SYS(sys_cachectl , 3)
1757 MIPS_SYS(sys_sysmips , 4)
1758 MIPS_SYS(sys_ni_syscall , 0) /* 4150 */
1759 MIPS_SYS(sys_getsid , 1)
1760 MIPS_SYS(sys_fdatasync , 0)
1761 MIPS_SYS(sys_sysctl , 1)
1762 MIPS_SYS(sys_mlock , 2)
1763 MIPS_SYS(sys_munlock , 2) /* 4155 */
1764 MIPS_SYS(sys_mlockall , 1)
1765 MIPS_SYS(sys_munlockall , 0)
1766 MIPS_SYS(sys_sched_setparam, 2)
1767 MIPS_SYS(sys_sched_getparam, 2)
1768 MIPS_SYS(sys_sched_setscheduler, 3) /* 4160 */
1769 MIPS_SYS(sys_sched_getscheduler, 1)
1770 MIPS_SYS(sys_sched_yield , 0)
1771 MIPS_SYS(sys_sched_get_priority_max, 1)
1772 MIPS_SYS(sys_sched_get_priority_min, 1)
1773 MIPS_SYS(sys_sched_rr_get_interval, 2) /* 4165 */
1774 MIPS_SYS(sys_nanosleep, 2)
1775 MIPS_SYS(sys_mremap , 4)
1776 MIPS_SYS(sys_accept , 3)
1777 MIPS_SYS(sys_bind , 3)
1778 MIPS_SYS(sys_connect , 3) /* 4170 */
1779 MIPS_SYS(sys_getpeername , 3)
1780 MIPS_SYS(sys_getsockname , 3)
1781 MIPS_SYS(sys_getsockopt , 5)
1782 MIPS_SYS(sys_listen , 2)
1783 MIPS_SYS(sys_recv , 4) /* 4175 */
1784 MIPS_SYS(sys_recvfrom , 6)
1785 MIPS_SYS(sys_recvmsg , 3)
1786 MIPS_SYS(sys_send , 4)
1787 MIPS_SYS(sys_sendmsg , 3)
1788 MIPS_SYS(sys_sendto , 6) /* 4180 */
1789 MIPS_SYS(sys_setsockopt , 5)
1790 MIPS_SYS(sys_shutdown , 2)
1791 MIPS_SYS(sys_socket , 3)
1792 MIPS_SYS(sys_socketpair , 4)
1793 MIPS_SYS(sys_setresuid , 3) /* 4185 */
1794 MIPS_SYS(sys_getresuid , 3)
1795 MIPS_SYS(sys_ni_syscall , 0) /* was sys_query_module */
1796 MIPS_SYS(sys_poll , 3)
1797 MIPS_SYS(sys_nfsservctl , 3)
1798 MIPS_SYS(sys_setresgid , 3) /* 4190 */
1799 MIPS_SYS(sys_getresgid , 3)
1800 MIPS_SYS(sys_prctl , 5)
1801 MIPS_SYS(sys_rt_sigreturn, 0)
1802 MIPS_SYS(sys_rt_sigaction, 4)
1803 MIPS_SYS(sys_rt_sigprocmask, 4) /* 4195 */
1804 MIPS_SYS(sys_rt_sigpending, 2)
1805 MIPS_SYS(sys_rt_sigtimedwait, 4)
1806 MIPS_SYS(sys_rt_sigqueueinfo, 3)
1807 MIPS_SYS(sys_rt_sigsuspend, 0)
1808 MIPS_SYS(sys_pread64 , 6) /* 4200 */
1809 MIPS_SYS(sys_pwrite64 , 6)
1810 MIPS_SYS(sys_chown , 3)
1811 MIPS_SYS(sys_getcwd , 2)
1812 MIPS_SYS(sys_capget , 2)
1813 MIPS_SYS(sys_capset , 2) /* 4205 */
1814 MIPS_SYS(sys_sigaltstack , 0)
1815 MIPS_SYS(sys_sendfile , 4)
1816 MIPS_SYS(sys_ni_syscall , 0)
1817 MIPS_SYS(sys_ni_syscall , 0)
1818 MIPS_SYS(sys_mmap2 , 6) /* 4210 */
1819 MIPS_SYS(sys_truncate64 , 4)
1820 MIPS_SYS(sys_ftruncate64 , 4)
1821 MIPS_SYS(sys_stat64 , 2)
1822 MIPS_SYS(sys_lstat64 , 2)
1823 MIPS_SYS(sys_fstat64 , 2) /* 4215 */
1824 MIPS_SYS(sys_pivot_root , 2)
1825 MIPS_SYS(sys_mincore , 3)
1826 MIPS_SYS(sys_madvise , 3)
1827 MIPS_SYS(sys_getdents64 , 3)
1828 MIPS_SYS(sys_fcntl64 , 3) /* 4220 */
1829 MIPS_SYS(sys_ni_syscall , 0)
1830 MIPS_SYS(sys_gettid , 0)
1831 MIPS_SYS(sys_readahead , 5)
1832 MIPS_SYS(sys_setxattr , 5)
1833 MIPS_SYS(sys_lsetxattr , 5) /* 4225 */
1834 MIPS_SYS(sys_fsetxattr , 5)
1835 MIPS_SYS(sys_getxattr , 4)
1836 MIPS_SYS(sys_lgetxattr , 4)
1837 MIPS_SYS(sys_fgetxattr , 4)
1838 MIPS_SYS(sys_listxattr , 3) /* 4230 */
1839 MIPS_SYS(sys_llistxattr , 3)
1840 MIPS_SYS(sys_flistxattr , 3)
1841 MIPS_SYS(sys_removexattr , 2)
1842 MIPS_SYS(sys_lremovexattr, 2)
1843 MIPS_SYS(sys_fremovexattr, 2) /* 4235 */
1844 MIPS_SYS(sys_tkill , 2)
1845 MIPS_SYS(sys_sendfile64 , 5)
1846 MIPS_SYS(sys_futex , 2)
1847 MIPS_SYS(sys_sched_setaffinity, 3)
1848 MIPS_SYS(sys_sched_getaffinity, 3) /* 4240 */
1849 MIPS_SYS(sys_io_setup , 2)
1850 MIPS_SYS(sys_io_destroy , 1)
1851 MIPS_SYS(sys_io_getevents, 5)
1852 MIPS_SYS(sys_io_submit , 3)
1853 MIPS_SYS(sys_io_cancel , 3) /* 4245 */
1854 MIPS_SYS(sys_exit_group , 1)
1855 MIPS_SYS(sys_lookup_dcookie, 3)
1856 MIPS_SYS(sys_epoll_create, 1)
1857 MIPS_SYS(sys_epoll_ctl , 4)
1858 MIPS_SYS(sys_epoll_wait , 3) /* 4250 */
1859 MIPS_SYS(sys_remap_file_pages, 5)
1860 MIPS_SYS(sys_set_tid_address, 1)
1861 MIPS_SYS(sys_restart_syscall, 0)
1862 MIPS_SYS(sys_fadvise64_64, 7)
1863 MIPS_SYS(sys_statfs64 , 3) /* 4255 */
1864 MIPS_SYS(sys_fstatfs64 , 2)
1865 MIPS_SYS(sys_timer_create, 3)
1866 MIPS_SYS(sys_timer_settime, 4)
1867 MIPS_SYS(sys_timer_gettime, 2)
1868 MIPS_SYS(sys_timer_getoverrun, 1) /* 4260 */
1869 MIPS_SYS(sys_timer_delete, 1)
1870 MIPS_SYS(sys_clock_settime, 2)
1871 MIPS_SYS(sys_clock_gettime, 2)
1872 MIPS_SYS(sys_clock_getres, 2)
1873 MIPS_SYS(sys_clock_nanosleep, 4) /* 4265 */
1874 MIPS_SYS(sys_tgkill , 3)
1875 MIPS_SYS(sys_utimes , 2)
1876 MIPS_SYS(sys_mbind , 4)
1877 MIPS_SYS(sys_ni_syscall , 0) /* sys_get_mempolicy */
1878 MIPS_SYS(sys_ni_syscall , 0) /* 4270 sys_set_mempolicy */
1879 MIPS_SYS(sys_mq_open , 4)
1880 MIPS_SYS(sys_mq_unlink , 1)
1881 MIPS_SYS(sys_mq_timedsend, 5)
1882 MIPS_SYS(sys_mq_timedreceive, 5)
1883 MIPS_SYS(sys_mq_notify , 2) /* 4275 */
1884 MIPS_SYS(sys_mq_getsetattr, 3)
1885 MIPS_SYS(sys_ni_syscall , 0) /* sys_vserver */
1886 MIPS_SYS(sys_waitid , 4)
1887 MIPS_SYS(sys_ni_syscall , 0) /* available, was setaltroot */
1888 MIPS_SYS(sys_add_key , 5)
ths388bb212007-05-13 13:58:00 +00001889 MIPS_SYS(sys_request_key, 4)
bellard048f6b42005-11-26 18:47:20 +00001890 MIPS_SYS(sys_keyctl , 5)
ths6f5b89a2007-03-02 20:48:00 +00001891 MIPS_SYS(sys_set_thread_area, 1)
ths388bb212007-05-13 13:58:00 +00001892 MIPS_SYS(sys_inotify_init, 0)
1893 MIPS_SYS(sys_inotify_add_watch, 3) /* 4285 */
1894 MIPS_SYS(sys_inotify_rm_watch, 2)
1895 MIPS_SYS(sys_migrate_pages, 4)
1896 MIPS_SYS(sys_openat, 4)
1897 MIPS_SYS(sys_mkdirat, 3)
1898 MIPS_SYS(sys_mknodat, 4) /* 4290 */
1899 MIPS_SYS(sys_fchownat, 5)
1900 MIPS_SYS(sys_futimesat, 3)
1901 MIPS_SYS(sys_fstatat64, 4)
1902 MIPS_SYS(sys_unlinkat, 3)
1903 MIPS_SYS(sys_renameat, 4) /* 4295 */
1904 MIPS_SYS(sys_linkat, 5)
1905 MIPS_SYS(sys_symlinkat, 3)
1906 MIPS_SYS(sys_readlinkat, 4)
1907 MIPS_SYS(sys_fchmodat, 3)
1908 MIPS_SYS(sys_faccessat, 3) /* 4300 */
1909 MIPS_SYS(sys_pselect6, 6)
1910 MIPS_SYS(sys_ppoll, 5)
1911 MIPS_SYS(sys_unshare, 1)
1912 MIPS_SYS(sys_splice, 4)
1913 MIPS_SYS(sys_sync_file_range, 7) /* 4305 */
1914 MIPS_SYS(sys_tee, 4)
1915 MIPS_SYS(sys_vmsplice, 4)
1916 MIPS_SYS(sys_move_pages, 6)
1917 MIPS_SYS(sys_set_robust_list, 2)
1918 MIPS_SYS(sys_get_robust_list, 3) /* 4310 */
1919 MIPS_SYS(sys_kexec_load, 4)
1920 MIPS_SYS(sys_getcpu, 3)
1921 MIPS_SYS(sys_epoll_pwait, 6)
1922 MIPS_SYS(sys_ioprio_set, 3)
1923 MIPS_SYS(sys_ioprio_get, 2)
bellard048f6b42005-11-26 18:47:20 +00001924};
1925
1926#undef MIPS_SYS
1927
Paul Brook590bc602009-07-09 17:45:17 +01001928static int do_store_exclusive(CPUMIPSState *env)
1929{
1930 target_ulong addr;
1931 target_ulong page_addr;
1932 target_ulong val;
1933 int flags;
1934 int segv = 0;
1935 int reg;
1936 int d;
1937
Aurelien Jarno5499b6f2009-11-22 13:08:14 +01001938 addr = env->lladdr;
Paul Brook590bc602009-07-09 17:45:17 +01001939 page_addr = addr & TARGET_PAGE_MASK;
1940 start_exclusive();
1941 mmap_lock();
1942 flags = page_get_flags(page_addr);
1943 if ((flags & PAGE_READ) == 0) {
1944 segv = 1;
1945 } else {
1946 reg = env->llreg & 0x1f;
1947 d = (env->llreg & 0x20) != 0;
1948 if (d) {
1949 segv = get_user_s64(val, addr);
1950 } else {
1951 segv = get_user_s32(val, addr);
1952 }
1953 if (!segv) {
1954 if (val != env->llval) {
1955 env->active_tc.gpr[reg] = 0;
1956 } else {
1957 if (d) {
1958 segv = put_user_u64(env->llnewval, addr);
1959 } else {
1960 segv = put_user_u32(env->llnewval, addr);
1961 }
1962 if (!segv) {
1963 env->active_tc.gpr[reg] = 1;
1964 }
1965 }
1966 }
1967 }
Aurelien Jarno5499b6f2009-11-22 13:08:14 +01001968 env->lladdr = -1;
Paul Brook590bc602009-07-09 17:45:17 +01001969 if (!segv) {
1970 env->active_tc.PC += 4;
1971 }
1972 mmap_unlock();
1973 end_exclusive();
1974 return segv;
1975}
1976
bellard048f6b42005-11-26 18:47:20 +00001977void cpu_loop(CPUMIPSState *env)
1978{
Anthony Liguoric227f092009-10-01 16:12:16 -05001979 target_siginfo_t info;
ths388bb212007-05-13 13:58:00 +00001980 int trapnr, ret;
bellard048f6b42005-11-26 18:47:20 +00001981 unsigned int syscall_num;
bellard048f6b42005-11-26 18:47:20 +00001982
1983 for(;;) {
Paul Brook590bc602009-07-09 17:45:17 +01001984 cpu_exec_start(env);
bellard048f6b42005-11-26 18:47:20 +00001985 trapnr = cpu_mips_exec(env);
Paul Brook590bc602009-07-09 17:45:17 +01001986 cpu_exec_end(env);
bellard048f6b42005-11-26 18:47:20 +00001987 switch(trapnr) {
1988 case EXCP_SYSCALL:
thsb5dc7732008-06-27 10:02:35 +00001989 syscall_num = env->active_tc.gpr[2] - 4000;
1990 env->active_tc.PC += 4;
ths388bb212007-05-13 13:58:00 +00001991 if (syscall_num >= sizeof(mips_syscall_args)) {
1992 ret = -ENOSYS;
1993 } else {
1994 int nb_args;
blueswir1992f48a2007-10-14 16:27:31 +00001995 abi_ulong sp_reg;
1996 abi_ulong arg5 = 0, arg6 = 0, arg7 = 0, arg8 = 0;
ths388bb212007-05-13 13:58:00 +00001997
1998 nb_args = mips_syscall_args[syscall_num];
thsb5dc7732008-06-27 10:02:35 +00001999 sp_reg = env->active_tc.gpr[29];
ths388bb212007-05-13 13:58:00 +00002000 switch (nb_args) {
2001 /* these arguments are taken from the stack */
bellard2f619692007-11-16 10:46:05 +00002002 /* FIXME - what to do if get_user() fails? */
2003 case 8: get_user_ual(arg8, sp_reg + 28);
2004 case 7: get_user_ual(arg7, sp_reg + 24);
2005 case 6: get_user_ual(arg6, sp_reg + 20);
2006 case 5: get_user_ual(arg5, sp_reg + 16);
ths388bb212007-05-13 13:58:00 +00002007 default:
2008 break;
bellard048f6b42005-11-26 18:47:20 +00002009 }
thsb5dc7732008-06-27 10:02:35 +00002010 ret = do_syscall(env, env->active_tc.gpr[2],
2011 env->active_tc.gpr[4],
2012 env->active_tc.gpr[5],
2013 env->active_tc.gpr[6],
2014 env->active_tc.gpr[7],
ths388bb212007-05-13 13:58:00 +00002015 arg5, arg6/*, arg7, arg8*/);
bellard048f6b42005-11-26 18:47:20 +00002016 }
pbrook0b1bcb02009-04-21 01:41:10 +00002017 if (ret == -TARGET_QEMU_ESIGRETURN) {
2018 /* Returning from a successful sigreturn syscall.
2019 Avoid clobbering register state. */
2020 break;
2021 }
ths388bb212007-05-13 13:58:00 +00002022 if ((unsigned int)ret >= (unsigned int)(-1133)) {
thsb5dc7732008-06-27 10:02:35 +00002023 env->active_tc.gpr[7] = 1; /* error flag */
ths388bb212007-05-13 13:58:00 +00002024 ret = -ret;
2025 } else {
thsb5dc7732008-06-27 10:02:35 +00002026 env->active_tc.gpr[7] = 0; /* error flag */
ths388bb212007-05-13 13:58:00 +00002027 }
thsb5dc7732008-06-27 10:02:35 +00002028 env->active_tc.gpr[2] = ret;
bellard048f6b42005-11-26 18:47:20 +00002029 break;
thsca7c2b12006-12-10 22:08:10 +00002030 case EXCP_TLBL:
2031 case EXCP_TLBS:
pbrooke4474232009-04-21 01:03:10 +00002032 info.si_signo = TARGET_SIGSEGV;
2033 info.si_errno = 0;
2034 /* XXX: check env->error_code */
2035 info.si_code = TARGET_SEGV_MAPERR;
2036 info._sifields._sigfault._addr = env->CP0_BadVAddr;
2037 queue_signal(env, info.si_signo, &info);
2038 break;
bellard6900e842005-12-05 21:04:24 +00002039 case EXCP_CpU:
bellard048f6b42005-11-26 18:47:20 +00002040 case EXCP_RI:
bellardbc1ad2d2006-06-14 13:37:55 +00002041 info.si_signo = TARGET_SIGILL;
2042 info.si_errno = 0;
2043 info.si_code = 0;
pbrook624f7972008-05-31 16:11:38 +00002044 queue_signal(env, info.si_signo, &info);
bellard048f6b42005-11-26 18:47:20 +00002045 break;
bellard106ec872006-06-27 21:08:10 +00002046 case EXCP_INTERRUPT:
2047 /* just indicate that signals should be handled asap */
2048 break;
pbrookd08b2a22006-11-04 16:46:29 +00002049 case EXCP_DEBUG:
2050 {
2051 int sig;
2052
2053 sig = gdb_handlesig (env, TARGET_SIGTRAP);
2054 if (sig)
2055 {
2056 info.si_signo = sig;
2057 info.si_errno = 0;
2058 info.si_code = TARGET_TRAP_BRKPT;
pbrook624f7972008-05-31 16:11:38 +00002059 queue_signal(env, info.si_signo, &info);
pbrookd08b2a22006-11-04 16:46:29 +00002060 }
2061 }
2062 break;
Paul Brook590bc602009-07-09 17:45:17 +01002063 case EXCP_SC:
2064 if (do_store_exclusive(env)) {
2065 info.si_signo = TARGET_SIGSEGV;
2066 info.si_errno = 0;
2067 info.si_code = TARGET_SEGV_MAPERR;
2068 info._sifields._sigfault._addr = env->active_tc.PC;
2069 queue_signal(env, info.si_signo, &info);
2070 }
2071 break;
bellard048f6b42005-11-26 18:47:20 +00002072 default:
2073 // error:
ths5fafdf22007-09-16 21:08:06 +00002074 fprintf(stderr, "qemu: unhandled CPU exception 0x%x - aborting\n",
bellard048f6b42005-11-26 18:47:20 +00002075 trapnr);
2076 cpu_dump_state(env, stderr, fprintf, 0);
2077 abort();
2078 }
2079 process_pending_signals(env);
2080 }
2081}
2082#endif
2083
bellardfdf9b3e2006-04-27 21:07:38 +00002084#ifdef TARGET_SH4
2085void cpu_loop (CPUState *env)
2086{
2087 int trapnr, ret;
Anthony Liguoric227f092009-10-01 16:12:16 -05002088 target_siginfo_t info;
ths3b46e622007-09-17 08:09:54 +00002089
bellardfdf9b3e2006-04-27 21:07:38 +00002090 while (1) {
2091 trapnr = cpu_sh4_exec (env);
ths3b46e622007-09-17 08:09:54 +00002092
bellardfdf9b3e2006-04-27 21:07:38 +00002093 switch (trapnr) {
2094 case 0x160:
aurel320b6d3ae2008-09-15 07:43:43 +00002095 env->pc += 2;
ths5fafdf22007-09-16 21:08:06 +00002096 ret = do_syscall(env,
2097 env->gregs[3],
2098 env->gregs[4],
2099 env->gregs[5],
2100 env->gregs[6],
2101 env->gregs[7],
2102 env->gregs[0],
thsfca743f2007-11-20 15:22:44 +00002103 env->gregs[1]);
pbrook9c2a9ea2006-06-18 19:12:54 +00002104 env->gregs[0] = ret;
bellardfdf9b3e2006-04-27 21:07:38 +00002105 break;
thsc3b5bc82007-12-02 06:31:25 +00002106 case EXCP_INTERRUPT:
2107 /* just indicate that signals should be handled asap */
2108 break;
pbrook355fb232006-06-17 19:58:25 +00002109 case EXCP_DEBUG:
2110 {
2111 int sig;
2112
2113 sig = gdb_handlesig (env, TARGET_SIGTRAP);
2114 if (sig)
2115 {
2116 info.si_signo = sig;
2117 info.si_errno = 0;
2118 info.si_code = TARGET_TRAP_BRKPT;
pbrook624f7972008-05-31 16:11:38 +00002119 queue_signal(env, info.si_signo, &info);
pbrook355fb232006-06-17 19:58:25 +00002120 }
2121 }
2122 break;
thsc3b5bc82007-12-02 06:31:25 +00002123 case 0xa0:
2124 case 0xc0:
2125 info.si_signo = SIGSEGV;
2126 info.si_errno = 0;
2127 info.si_code = TARGET_SEGV_MAPERR;
2128 info._sifields._sigfault._addr = env->tea;
pbrook624f7972008-05-31 16:11:38 +00002129 queue_signal(env, info.si_signo, &info);
thsc3b5bc82007-12-02 06:31:25 +00002130 break;
2131
bellardfdf9b3e2006-04-27 21:07:38 +00002132 default:
2133 printf ("Unhandled trap: 0x%x\n", trapnr);
2134 cpu_dump_state(env, stderr, fprintf, 0);
2135 exit (1);
2136 }
2137 process_pending_signals (env);
2138 }
2139}
2140#endif
2141
ths48733d12007-10-08 13:36:46 +00002142#ifdef TARGET_CRIS
2143void cpu_loop (CPUState *env)
2144{
2145 int trapnr, ret;
Anthony Liguoric227f092009-10-01 16:12:16 -05002146 target_siginfo_t info;
ths48733d12007-10-08 13:36:46 +00002147
2148 while (1) {
2149 trapnr = cpu_cris_exec (env);
2150 switch (trapnr) {
2151 case 0xaa:
2152 {
2153 info.si_signo = SIGSEGV;
2154 info.si_errno = 0;
2155 /* XXX: check env->error_code */
2156 info.si_code = TARGET_SEGV_MAPERR;
edgar_igle00c1e72008-05-27 21:12:09 +00002157 info._sifields._sigfault._addr = env->pregs[PR_EDA];
pbrook624f7972008-05-31 16:11:38 +00002158 queue_signal(env, info.si_signo, &info);
ths48733d12007-10-08 13:36:46 +00002159 }
2160 break;
edgar_iglb6d3abd2008-02-28 11:29:27 +00002161 case EXCP_INTERRUPT:
2162 /* just indicate that signals should be handled asap */
2163 break;
ths48733d12007-10-08 13:36:46 +00002164 case EXCP_BREAK:
2165 ret = do_syscall(env,
2166 env->regs[9],
2167 env->regs[10],
2168 env->regs[11],
2169 env->regs[12],
2170 env->regs[13],
2171 env->pregs[7],
2172 env->pregs[11]);
2173 env->regs[10] = ret;
ths48733d12007-10-08 13:36:46 +00002174 break;
2175 case EXCP_DEBUG:
2176 {
2177 int sig;
2178
2179 sig = gdb_handlesig (env, TARGET_SIGTRAP);
2180 if (sig)
2181 {
2182 info.si_signo = sig;
2183 info.si_errno = 0;
2184 info.si_code = TARGET_TRAP_BRKPT;
pbrook624f7972008-05-31 16:11:38 +00002185 queue_signal(env, info.si_signo, &info);
ths48733d12007-10-08 13:36:46 +00002186 }
2187 }
2188 break;
2189 default:
2190 printf ("Unhandled trap: 0x%x\n", trapnr);
2191 cpu_dump_state(env, stderr, fprintf, 0);
2192 exit (1);
2193 }
2194 process_pending_signals (env);
2195 }
2196}
2197#endif
2198
Edgar E. Iglesiasb779e292009-05-20 21:31:33 +02002199#ifdef TARGET_MICROBLAZE
2200void cpu_loop (CPUState *env)
2201{
2202 int trapnr, ret;
Anthony Liguoric227f092009-10-01 16:12:16 -05002203 target_siginfo_t info;
Edgar E. Iglesiasb779e292009-05-20 21:31:33 +02002204
2205 while (1) {
2206 trapnr = cpu_mb_exec (env);
2207 switch (trapnr) {
2208 case 0xaa:
2209 {
2210 info.si_signo = SIGSEGV;
2211 info.si_errno = 0;
2212 /* XXX: check env->error_code */
2213 info.si_code = TARGET_SEGV_MAPERR;
2214 info._sifields._sigfault._addr = 0;
2215 queue_signal(env, info.si_signo, &info);
2216 }
2217 break;
2218 case EXCP_INTERRUPT:
2219 /* just indicate that signals should be handled asap */
2220 break;
2221 case EXCP_BREAK:
2222 /* Return address is 4 bytes after the call. */
2223 env->regs[14] += 4;
2224 ret = do_syscall(env,
2225 env->regs[12],
2226 env->regs[5],
2227 env->regs[6],
2228 env->regs[7],
2229 env->regs[8],
2230 env->regs[9],
2231 env->regs[10]);
2232 env->regs[3] = ret;
2233 env->sregs[SR_PC] = env->regs[14];
2234 break;
2235 case EXCP_DEBUG:
2236 {
2237 int sig;
2238
2239 sig = gdb_handlesig (env, TARGET_SIGTRAP);
2240 if (sig)
2241 {
2242 info.si_signo = sig;
2243 info.si_errno = 0;
2244 info.si_code = TARGET_TRAP_BRKPT;
2245 queue_signal(env, info.si_signo, &info);
2246 }
2247 }
2248 break;
2249 default:
2250 printf ("Unhandled trap: 0x%x\n", trapnr);
2251 cpu_dump_state(env, stderr, fprintf, 0);
2252 exit (1);
2253 }
2254 process_pending_signals (env);
2255 }
2256}
2257#endif
2258
pbrooke6e59062006-10-22 00:18:54 +00002259#ifdef TARGET_M68K
2260
2261void cpu_loop(CPUM68KState *env)
2262{
2263 int trapnr;
2264 unsigned int n;
Anthony Liguoric227f092009-10-01 16:12:16 -05002265 target_siginfo_t info;
pbrooke6e59062006-10-22 00:18:54 +00002266 TaskState *ts = env->opaque;
ths3b46e622007-09-17 08:09:54 +00002267
pbrooke6e59062006-10-22 00:18:54 +00002268 for(;;) {
2269 trapnr = cpu_m68k_exec(env);
2270 switch(trapnr) {
2271 case EXCP_ILLEGAL:
2272 {
2273 if (ts->sim_syscalls) {
2274 uint16_t nr;
2275 nr = lduw(env->pc + 2);
2276 env->pc += 4;
2277 do_m68k_simcall(env, nr);
2278 } else {
2279 goto do_sigill;
2280 }
2281 }
2282 break;
pbrooka87295e2007-05-26 15:09:38 +00002283 case EXCP_HALT_INSN:
pbrooke6e59062006-10-22 00:18:54 +00002284 /* Semihosing syscall. */
pbrooka87295e2007-05-26 15:09:38 +00002285 env->pc += 4;
pbrooke6e59062006-10-22 00:18:54 +00002286 do_m68k_semihosting(env, env->dregs[0]);
2287 break;
2288 case EXCP_LINEA:
2289 case EXCP_LINEF:
2290 case EXCP_UNSUPPORTED:
2291 do_sigill:
2292 info.si_signo = SIGILL;
2293 info.si_errno = 0;
2294 info.si_code = TARGET_ILL_ILLOPN;
2295 info._sifields._sigfault._addr = env->pc;
pbrook624f7972008-05-31 16:11:38 +00002296 queue_signal(env, info.si_signo, &info);
pbrooke6e59062006-10-22 00:18:54 +00002297 break;
2298 case EXCP_TRAP0:
2299 {
2300 ts->sim_syscalls = 0;
2301 n = env->dregs[0];
2302 env->pc += 2;
ths5fafdf22007-09-16 21:08:06 +00002303 env->dregs[0] = do_syscall(env,
2304 n,
pbrooke6e59062006-10-22 00:18:54 +00002305 env->dregs[1],
2306 env->dregs[2],
2307 env->dregs[3],
2308 env->dregs[4],
2309 env->dregs[5],
pbrookbb7ec042008-03-25 22:28:25 +00002310 env->aregs[0]);
pbrooke6e59062006-10-22 00:18:54 +00002311 }
2312 break;
2313 case EXCP_INTERRUPT:
2314 /* just indicate that signals should be handled asap */
2315 break;
2316 case EXCP_ACCESS:
2317 {
2318 info.si_signo = SIGSEGV;
2319 info.si_errno = 0;
2320 /* XXX: check env->error_code */
2321 info.si_code = TARGET_SEGV_MAPERR;
2322 info._sifields._sigfault._addr = env->mmu.ar;
pbrook624f7972008-05-31 16:11:38 +00002323 queue_signal(env, info.si_signo, &info);
pbrooke6e59062006-10-22 00:18:54 +00002324 }
2325 break;
2326 case EXCP_DEBUG:
2327 {
2328 int sig;
2329
2330 sig = gdb_handlesig (env, TARGET_SIGTRAP);
2331 if (sig)
2332 {
2333 info.si_signo = sig;
2334 info.si_errno = 0;
2335 info.si_code = TARGET_TRAP_BRKPT;
pbrook624f7972008-05-31 16:11:38 +00002336 queue_signal(env, info.si_signo, &info);
pbrooke6e59062006-10-22 00:18:54 +00002337 }
2338 }
2339 break;
2340 default:
ths5fafdf22007-09-16 21:08:06 +00002341 fprintf(stderr, "qemu: unhandled CPU exception 0x%x - aborting\n",
pbrooke6e59062006-10-22 00:18:54 +00002342 trapnr);
2343 cpu_dump_state(env, stderr, fprintf, 0);
2344 abort();
2345 }
2346 process_pending_signals(env);
2347 }
2348}
2349#endif /* TARGET_M68K */
2350
j_mayer7a3148a2007-04-05 07:13:51 +00002351#ifdef TARGET_ALPHA
2352void cpu_loop (CPUState *env)
2353{
j_mayere96efcf2007-04-14 12:17:09 +00002354 int trapnr;
Anthony Liguoric227f092009-10-01 16:12:16 -05002355 target_siginfo_t info;
Richard Henderson6049f4f2009-12-27 18:30:03 -08002356 abi_long sysret;
ths3b46e622007-09-17 08:09:54 +00002357
j_mayer7a3148a2007-04-05 07:13:51 +00002358 while (1) {
2359 trapnr = cpu_alpha_exec (env);
ths3b46e622007-09-17 08:09:54 +00002360
j_mayer7a3148a2007-04-05 07:13:51 +00002361 switch (trapnr) {
2362 case EXCP_RESET:
2363 fprintf(stderr, "Reset requested. Exit\n");
2364 exit(1);
2365 break;
2366 case EXCP_MCHK:
2367 fprintf(stderr, "Machine check exception. Exit\n");
2368 exit(1);
2369 break;
2370 case EXCP_ARITH:
Richard Henderson6049f4f2009-12-27 18:30:03 -08002371 info.si_signo = TARGET_SIGFPE;
2372 info.si_errno = 0;
2373 info.si_code = TARGET_FPE_FLTINV;
2374 info._sifields._sigfault._addr = env->pc;
2375 queue_signal(env, info.si_signo, &info);
j_mayer7a3148a2007-04-05 07:13:51 +00002376 break;
2377 case EXCP_HW_INTERRUPT:
ths5fafdf22007-09-16 21:08:06 +00002378 fprintf(stderr, "External interrupt. Exit\n");
j_mayer7a3148a2007-04-05 07:13:51 +00002379 exit(1);
2380 break;
2381 case EXCP_DFAULT:
Richard Henderson6049f4f2009-12-27 18:30:03 -08002382 info.si_signo = TARGET_SIGSEGV;
2383 info.si_errno = 0;
2384 info.si_code = 0; /* ??? SEGV_MAPERR vs SEGV_ACCERR. */
2385 info._sifields._sigfault._addr = env->pc;
2386 queue_signal(env, info.si_signo, &info);
j_mayer7a3148a2007-04-05 07:13:51 +00002387 break;
2388 case EXCP_DTB_MISS_PAL:
2389 fprintf(stderr, "MMU data TLB miss in PALcode\n");
2390 exit(1);
2391 break;
2392 case EXCP_ITB_MISS:
2393 fprintf(stderr, "MMU instruction TLB miss\n");
2394 exit(1);
2395 break;
2396 case EXCP_ITB_ACV:
2397 fprintf(stderr, "MMU instruction access violation\n");
2398 exit(1);
2399 break;
2400 case EXCP_DTB_MISS_NATIVE:
2401 fprintf(stderr, "MMU data TLB miss\n");
2402 exit(1);
2403 break;
2404 case EXCP_UNALIGN:
Richard Henderson6049f4f2009-12-27 18:30:03 -08002405 info.si_signo = TARGET_SIGBUS;
2406 info.si_errno = 0;
2407 info.si_code = TARGET_BUS_ADRALN;
2408 info._sifields._sigfault._addr = env->pc;
2409 queue_signal(env, info.si_signo, &info);
j_mayer7a3148a2007-04-05 07:13:51 +00002410 break;
2411 case EXCP_OPCDEC:
Richard Henderson6049f4f2009-12-27 18:30:03 -08002412 do_sigill:
2413 info.si_signo = TARGET_SIGILL;
2414 info.si_errno = 0;
2415 info.si_code = TARGET_ILL_ILLOPC;
2416 info._sifields._sigfault._addr = env->pc;
2417 queue_signal(env, info.si_signo, &info);
j_mayer7a3148a2007-04-05 07:13:51 +00002418 break;
2419 case EXCP_FEN:
Richard Henderson6049f4f2009-12-27 18:30:03 -08002420 /* No-op. Linux simply re-enables the FPU. */
j_mayer7a3148a2007-04-05 07:13:51 +00002421 break;
2422 case EXCP_CALL_PAL ... (EXCP_CALL_PALP - 1):
Richard Henderson6049f4f2009-12-27 18:30:03 -08002423 switch ((trapnr >> 6) | 0x80) {
2424 case 0x80:
2425 /* BPT */
2426 info.si_signo = TARGET_SIGTRAP;
2427 info.si_errno = 0;
2428 info.si_code = TARGET_TRAP_BRKPT;
2429 info._sifields._sigfault._addr = env->pc;
2430 queue_signal(env, info.si_signo, &info);
2431 break;
2432 case 0x81:
2433 /* BUGCHK */
2434 info.si_signo = TARGET_SIGTRAP;
2435 info.si_errno = 0;
2436 info.si_code = 0;
2437 info._sifields._sigfault._addr = env->pc;
2438 queue_signal(env, info.si_signo, &info);
2439 break;
2440 case 0x83:
2441 /* CALLSYS */
2442 trapnr = env->ir[IR_V0];
2443 sysret = do_syscall(env, trapnr,
2444 env->ir[IR_A0], env->ir[IR_A1],
2445 env->ir[IR_A2], env->ir[IR_A3],
2446 env->ir[IR_A4], env->ir[IR_A5]);
2447 if (trapnr != TARGET_NR_sigreturn
2448 && trapnr != TARGET_NR_rt_sigreturn) {
2449 env->ir[IR_V0] = (sysret < 0 ? -sysret : sysret);
2450 env->ir[IR_A3] = (sysret < 0);
2451 }
2452 break;
2453 case 0x86:
2454 /* IMB */
2455 /* ??? We can probably elide the code using page_unprotect
2456 that is checking for self-modifying code. Instead we
2457 could simply call tb_flush here. Until we work out the
2458 changes required to turn off the extra write protection,
2459 this can be a no-op. */
2460 break;
2461 case 0x9E:
2462 /* RDUNIQUE */
2463 /* Handled in the translator for usermode. */
2464 abort();
2465 case 0x9F:
2466 /* WRUNIQUE */
2467 /* Handled in the translator for usermode. */
2468 abort();
2469 case 0xAA:
2470 /* GENTRAP */
2471 info.si_signo = TARGET_SIGFPE;
2472 switch (env->ir[IR_A0]) {
2473 case TARGET_GEN_INTOVF:
2474 info.si_code = TARGET_FPE_INTOVF;
2475 break;
2476 case TARGET_GEN_INTDIV:
2477 info.si_code = TARGET_FPE_INTDIV;
2478 break;
2479 case TARGET_GEN_FLTOVF:
2480 info.si_code = TARGET_FPE_FLTOVF;
2481 break;
2482 case TARGET_GEN_FLTUND:
2483 info.si_code = TARGET_FPE_FLTUND;
2484 break;
2485 case TARGET_GEN_FLTINV:
2486 info.si_code = TARGET_FPE_FLTINV;
2487 break;
2488 case TARGET_GEN_FLTINE:
2489 info.si_code = TARGET_FPE_FLTRES;
2490 break;
2491 case TARGET_GEN_ROPRAND:
2492 info.si_code = 0;
2493 break;
2494 default:
2495 info.si_signo = TARGET_SIGTRAP;
2496 info.si_code = 0;
2497 break;
2498 }
2499 info.si_errno = 0;
2500 info._sifields._sigfault._addr = env->pc;
2501 queue_signal(env, info.si_signo, &info);
2502 break;
2503 default:
2504 goto do_sigill;
2505 }
j_mayer7a3148a2007-04-05 07:13:51 +00002506 break;
2507 case EXCP_CALL_PALP ... (EXCP_CALL_PALE - 1):
Richard Henderson6049f4f2009-12-27 18:30:03 -08002508 goto do_sigill;
j_mayer7a3148a2007-04-05 07:13:51 +00002509 case EXCP_DEBUG:
Richard Henderson6049f4f2009-12-27 18:30:03 -08002510 info.si_signo = gdb_handlesig (env, TARGET_SIGTRAP);
2511 if (info.si_signo) {
2512 info.si_errno = 0;
2513 info.si_code = TARGET_TRAP_BRKPT;
2514 queue_signal(env, info.si_signo, &info);
j_mayer7a3148a2007-04-05 07:13:51 +00002515 }
2516 break;
2517 default:
2518 printf ("Unhandled trap: 0x%x\n", trapnr);
2519 cpu_dump_state(env, stderr, fprintf, 0);
2520 exit (1);
2521 }
2522 process_pending_signals (env);
2523 }
2524}
2525#endif /* TARGET_ALPHA */
2526
blueswir18fcd3692008-08-17 20:26:25 +00002527static void usage(void)
bellard31e31b82003-02-18 22:55:36 +00002528{
pbrook4a19f1e2009-04-07 23:17:49 +00002529 printf("qemu-" TARGET_ARCH " version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n"
bellard68d0f702008-01-06 17:21:48 +00002530 "usage: qemu-" TARGET_ARCH " [options] program [arguments...]\n"
bellardb346ff42003-06-15 20:05:50 +00002531 "Linux CPU emulator (compiled for %s emulation)\n"
bellardd691f662003-03-24 21:58:34 +00002532 "\n"
bellard68d0f702008-01-06 17:21:48 +00002533 "Standard options:\n"
thsb12b6a12007-06-17 16:38:39 +00002534 "-h print this help\n"
2535 "-g port wait gdb connection to port\n"
2536 "-L path set the elf interpreter prefix (default=%s)\n"
2537 "-s size set the stack size in bytes (default=%ld)\n"
2538 "-cpu model select CPU (-cpu ? for list)\n"
2539 "-drop-ld-preload drop LD_PRELOAD for target process\n"
aurel3204a6dfe2009-01-30 19:59:17 +00002540 "-E var=value sets/modifies targets environment variable(s)\n"
2541 "-U var unsets targets environment variable(s)\n"
aurel327d8cec92009-04-15 16:11:52 +00002542 "-0 argv0 forces target process argv[0] to be argv0\n"
Paul Brook379f6692009-07-17 12:48:08 +01002543#if defined(CONFIG_USE_GUEST_BASE)
2544 "-B address set guest_base address to address\n"
2545#endif
bellard54936002003-05-13 00:25:15 +00002546 "\n"
bellard68d0f702008-01-06 17:21:48 +00002547 "Debug options:\n"
bellard6f1f31c2004-04-25 18:00:45 +00002548 "-d options activate log (logfile=%s)\n"
bellardb6741952007-11-11 14:46:06 +00002549 "-p pagesize set the host page size to 'pagesize'\n"
aurel321b530a62009-04-05 20:08:59 +00002550 "-singlestep always run in singlestep mode\n"
balrogb01bcae2007-12-16 13:05:59 +00002551 "-strace log system calls\n"
2552 "\n"
bellard68d0f702008-01-06 17:21:48 +00002553 "Environment variables:\n"
balrogb01bcae2007-12-16 13:05:59 +00002554 "QEMU_STRACE Print system calls and arguments similar to the\n"
2555 " 'strace' program. Enable by setting to any value.\n"
aurel3204a6dfe2009-01-30 19:59:17 +00002556 "You can use -E and -U options to set/unset environment variables\n"
2557 "for target process. It is possible to provide several variables\n"
2558 "by repeating the option. For example:\n"
2559 " -E var1=val2 -E var2=val2 -U LD_PRELOAD -U LD_DEBUG\n"
2560 "Note that if you provide several changes to single variable\n"
2561 "last change will stay in effect.\n"
balrogb01bcae2007-12-16 13:05:59 +00002562 ,
bellardb346ff42003-06-15 20:05:50 +00002563 TARGET_ARCH,
ths5fafdf22007-09-16 21:08:06 +00002564 interp_prefix,
Richard Henderson703e0e82010-03-19 14:21:13 -07002565 guest_stack_size,
bellard54936002003-05-13 00:25:15 +00002566 DEBUG_LOGFILE);
blueswir12d18e632009-02-28 20:14:00 +00002567 exit(1);
bellard31e31b82003-02-18 22:55:36 +00002568}
2569
pbrookd5975362008-06-07 20:50:51 +00002570THREAD CPUState *thread_env;
bellard59faf6d2003-06-25 16:18:50 +00002571
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002572void task_settid(TaskState *ts)
2573{
2574 if (ts->ts_tid == 0) {
Juan Quintela2f7bb872009-07-27 16:13:24 +02002575#ifdef CONFIG_USE_NPTL
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002576 ts->ts_tid = (pid_t)syscall(SYS_gettid);
2577#else
2578 /* when no threads are used, tid becomes pid */
2579 ts->ts_tid = getpid();
2580#endif
2581 }
2582}
2583
2584void stop_all_tasks(void)
2585{
2586 /*
2587 * We trust that when using NPTL, start_exclusive()
2588 * handles thread stopping correctly.
2589 */
2590 start_exclusive();
2591}
2592
pbrookc3a92832008-06-09 14:02:50 +00002593/* Assumes contents are already zeroed. */
pbrook624f7972008-05-31 16:11:38 +00002594void init_task_state(TaskState *ts)
2595{
2596 int i;
2597
pbrook624f7972008-05-31 16:11:38 +00002598 ts->used = 1;
2599 ts->first_free = ts->sigqueue_table;
2600 for (i = 0; i < MAX_SIGQUEUE_SIZE - 1; i++) {
2601 ts->sigqueue_table[i].next = &ts->sigqueue_table[i + 1];
2602 }
2603 ts->sigqueue_table[i].next = NULL;
2604}
2605
malc902b3d52008-12-10 19:18:40 +00002606int main(int argc, char **argv, char **envp)
bellard31e31b82003-02-18 22:55:36 +00002607{
2608 const char *filename;
j_mayerb1f9be32007-03-19 08:08:28 +00002609 const char *cpu_model;
bellard01ffc752003-02-18 23:00:51 +00002610 struct target_pt_regs regs1, *regs = &regs1;
bellard31e31b82003-02-18 22:55:36 +00002611 struct image_info info1, *info = &info1;
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002612 struct linux_binprm bprm;
bellard851e67a2003-03-29 16:53:14 +00002613 TaskState ts1, *ts = &ts1;
bellardb346ff42003-06-15 20:05:50 +00002614 CPUState *env;
bellard586314f2003-03-03 15:02:29 +00002615 int optind;
bellardd691f662003-03-24 21:58:34 +00002616 const char *r;
bellard74c33be2005-10-30 21:01:05 +00002617 int gdbstub_port = 0;
aurel3204a6dfe2009-01-30 19:59:17 +00002618 char **target_environ, **wrk;
aurel327d8cec92009-04-15 16:11:52 +00002619 char **target_argv;
2620 int target_argc;
aurel3204a6dfe2009-01-30 19:59:17 +00002621 envlist_t *envlist = NULL;
aurel327d8cec92009-04-15 16:11:52 +00002622 const char *argv0 = NULL;
2623 int i;
Arnaud Patardfd4d81d2009-06-19 10:39:36 +03002624 int ret;
thsb12b6a12007-06-17 16:38:39 +00002625
bellard31e31b82003-02-18 22:55:36 +00002626 if (argc <= 1)
pbrook44de1b32008-03-26 22:40:25 +00002627 usage();
bellardf801f972003-04-07 21:31:06 +00002628
malc902b3d52008-12-10 19:18:40 +00002629 qemu_cache_utils_init(envp);
2630
bellardcc38b842003-10-27 21:16:14 +00002631 /* init debug */
2632 cpu_set_log_filename(DEBUG_LOGFILE);
2633
aurel3204a6dfe2009-01-30 19:59:17 +00002634 if ((envlist = envlist_create()) == NULL) {
2635 (void) fprintf(stderr, "Unable to allocate envlist\n");
2636 exit(1);
2637 }
2638
2639 /* add current environment into the list */
2640 for (wrk = environ; *wrk != NULL; wrk++) {
2641 (void) envlist_setenv(envlist, *wrk);
2642 }
2643
Richard Henderson703e0e82010-03-19 14:21:13 -07002644 /* Read the stack limit from the kernel. If it's "unlimited",
2645 then we can do little else besides use the default. */
2646 {
2647 struct rlimit lim;
2648 if (getrlimit(RLIMIT_STACK, &lim) == 0
2649 && lim.rlim_cur != RLIM_INFINITY) {
2650 guest_stack_size = lim.rlim_cur;
2651 }
2652 }
2653
j_mayerb1f9be32007-03-19 08:08:28 +00002654 cpu_model = NULL;
john cooperb5ec5ce2010-02-20 11:14:59 -06002655#if defined(cpudef_setup)
2656 cpudef_setup(); /* parse cpu definitions in target config file (TBD) */
2657#endif
2658
bellard586314f2003-03-03 15:02:29 +00002659 optind = 1;
bellardd691f662003-03-24 21:58:34 +00002660 for(;;) {
2661 if (optind >= argc)
2662 break;
2663 r = argv[optind];
2664 if (r[0] != '-')
2665 break;
bellard586314f2003-03-03 15:02:29 +00002666 optind++;
bellardd691f662003-03-24 21:58:34 +00002667 r++;
2668 if (!strcmp(r, "-")) {
2669 break;
2670 } else if (!strcmp(r, "d")) {
bellarde19e89a2004-03-21 17:08:23 +00002671 int mask;
blueswir1c7cd6a32008-10-02 18:27:46 +00002672 const CPULogItem *item;
bellard6f1f31c2004-04-25 18:00:45 +00002673
2674 if (optind >= argc)
2675 break;
ths3b46e622007-09-17 08:09:54 +00002676
bellard6f1f31c2004-04-25 18:00:45 +00002677 r = argv[optind++];
2678 mask = cpu_str_to_log_mask(r);
bellarde19e89a2004-03-21 17:08:23 +00002679 if (!mask) {
2680 printf("Log items (comma separated):\n");
2681 for(item = cpu_log_items; item->mask != 0; item++) {
2682 printf("%-10s %s\n", item->name, item->help);
2683 }
2684 exit(1);
2685 }
2686 cpu_set_log(mask);
aurel3204a6dfe2009-01-30 19:59:17 +00002687 } else if (!strcmp(r, "E")) {
2688 r = argv[optind++];
2689 if (envlist_setenv(envlist, r) != 0)
2690 usage();
2691 } else if (!strcmp(r, "U")) {
2692 r = argv[optind++];
2693 if (envlist_unsetenv(envlist, r) != 0)
2694 usage();
aurel327d8cec92009-04-15 16:11:52 +00002695 } else if (!strcmp(r, "0")) {
2696 r = argv[optind++];
2697 argv0 = r;
bellardd691f662003-03-24 21:58:34 +00002698 } else if (!strcmp(r, "s")) {
aurel32491150d2009-02-09 19:02:09 +00002699 if (optind >= argc)
2700 break;
bellardd691f662003-03-24 21:58:34 +00002701 r = argv[optind++];
Richard Henderson703e0e82010-03-19 14:21:13 -07002702 guest_stack_size = strtoul(r, (char **)&r, 0);
2703 if (guest_stack_size == 0)
pbrook44de1b32008-03-26 22:40:25 +00002704 usage();
bellardd691f662003-03-24 21:58:34 +00002705 if (*r == 'M')
Richard Henderson703e0e82010-03-19 14:21:13 -07002706 guest_stack_size *= 1024 * 1024;
bellardd691f662003-03-24 21:58:34 +00002707 else if (*r == 'k' || *r == 'K')
Richard Henderson703e0e82010-03-19 14:21:13 -07002708 guest_stack_size *= 1024;
bellardd691f662003-03-24 21:58:34 +00002709 } else if (!strcmp(r, "L")) {
2710 interp_prefix = argv[optind++];
bellard54936002003-05-13 00:25:15 +00002711 } else if (!strcmp(r, "p")) {
aurel32491150d2009-02-09 19:02:09 +00002712 if (optind >= argc)
2713 break;
bellard83fb7ad2004-07-05 21:25:26 +00002714 qemu_host_page_size = atoi(argv[optind++]);
2715 if (qemu_host_page_size == 0 ||
2716 (qemu_host_page_size & (qemu_host_page_size - 1)) != 0) {
bellard54936002003-05-13 00:25:15 +00002717 fprintf(stderr, "page size must be a power of two\n");
2718 exit(1);
2719 }
bellard1fddef42005-04-17 19:16:13 +00002720 } else if (!strcmp(r, "g")) {
aurel32491150d2009-02-09 19:02:09 +00002721 if (optind >= argc)
2722 break;
bellard74c33be2005-10-30 21:01:05 +00002723 gdbstub_port = atoi(argv[optind++]);
pbrookc5937222006-05-14 11:30:38 +00002724 } else if (!strcmp(r, "r")) {
2725 qemu_uname_release = argv[optind++];
j_mayerb1f9be32007-03-19 08:08:28 +00002726 } else if (!strcmp(r, "cpu")) {
2727 cpu_model = argv[optind++];
aurel32491150d2009-02-09 19:02:09 +00002728 if (cpu_model == NULL || strcmp(cpu_model, "?") == 0) {
j_mayerc732abe2007-10-12 06:47:46 +00002729/* XXX: implement xxx_cpu_list for targets that still miss it */
john cooperb5ec5ce2010-02-20 11:14:59 -06002730#if defined(cpu_list_id)
2731 cpu_list_id(stdout, &fprintf, "");
j_mayerb1f9be32007-03-19 08:08:28 +00002732#endif
blueswir12d18e632009-02-28 20:14:00 +00002733 exit(1);
j_mayerb1f9be32007-03-19 08:08:28 +00002734 }
Paul Brook379f6692009-07-17 12:48:08 +01002735#if defined(CONFIG_USE_GUEST_BASE)
2736 } else if (!strcmp(r, "B")) {
2737 guest_base = strtol(argv[optind++], NULL, 0);
2738 have_guest_base = 1;
2739#endif
thsb12b6a12007-06-17 16:38:39 +00002740 } else if (!strcmp(r, "drop-ld-preload")) {
aurel3204a6dfe2009-01-30 19:59:17 +00002741 (void) envlist_unsetenv(envlist, "LD_PRELOAD");
aurel321b530a62009-04-05 20:08:59 +00002742 } else if (!strcmp(r, "singlestep")) {
2743 singlestep = 1;
bellardb6741952007-11-11 14:46:06 +00002744 } else if (!strcmp(r, "strace")) {
2745 do_strace = 1;
ths5fafdf22007-09-16 21:08:06 +00002746 } else
bellardc6981052004-02-16 21:49:03 +00002747 {
bellardd691f662003-03-24 21:58:34 +00002748 usage();
2749 }
bellard586314f2003-03-03 15:02:29 +00002750 }
bellardd691f662003-03-24 21:58:34 +00002751 if (optind >= argc)
2752 usage();
bellard586314f2003-03-03 15:02:29 +00002753 filename = argv[optind];
aurel32d088d662009-01-30 20:09:01 +00002754 exec_path = argv[optind];
bellard586314f2003-03-03 15:02:29 +00002755
bellard31e31b82003-02-18 22:55:36 +00002756 /* Zero out regs */
bellard01ffc752003-02-18 23:00:51 +00002757 memset(regs, 0, sizeof(struct target_pt_regs));
bellard31e31b82003-02-18 22:55:36 +00002758
2759 /* Zero out image_info */
2760 memset(info, 0, sizeof(struct image_info));
2761
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002762 memset(&bprm, 0, sizeof (bprm));
2763
bellard74cd30b2003-04-11 00:13:41 +00002764 /* Scan interp_prefix dir for replacement files. */
2765 init_paths(interp_prefix);
2766
bellard46027c02007-11-08 13:56:19 +00002767 if (cpu_model == NULL) {
bellardaaed9092007-11-10 15:15:54 +00002768#if defined(TARGET_I386)
bellard46027c02007-11-08 13:56:19 +00002769#ifdef TARGET_X86_64
2770 cpu_model = "qemu64";
2771#else
2772 cpu_model = "qemu32";
2773#endif
bellardaaed9092007-11-10 15:15:54 +00002774#elif defined(TARGET_ARM)
pbrook088ab162009-04-09 15:20:50 +00002775 cpu_model = "any";
bellardaaed9092007-11-10 15:15:54 +00002776#elif defined(TARGET_M68K)
2777 cpu_model = "any";
2778#elif defined(TARGET_SPARC)
2779#ifdef TARGET_SPARC64
2780 cpu_model = "TI UltraSparc II";
2781#else
2782 cpu_model = "Fujitsu MB86904";
bellard46027c02007-11-08 13:56:19 +00002783#endif
bellardaaed9092007-11-10 15:15:54 +00002784#elif defined(TARGET_MIPS)
2785#if defined(TARGET_ABI_MIPSN32) || defined(TARGET_ABI_MIPSN64)
2786 cpu_model = "20Kc";
2787#else
2788 cpu_model = "24Kf";
2789#endif
2790#elif defined(TARGET_PPC)
bellard7ded4f52007-11-15 15:37:50 +00002791#ifdef TARGET_PPC64
Aurelien Jarnof7177932010-04-06 12:21:05 +02002792 cpu_model = "970fx";
bellard7ded4f52007-11-15 15:37:50 +00002793#else
bellardaaed9092007-11-10 15:15:54 +00002794 cpu_model = "750";
bellard7ded4f52007-11-15 15:37:50 +00002795#endif
bellardaaed9092007-11-10 15:15:54 +00002796#else
2797 cpu_model = "any";
2798#endif
2799 }
bellard26a5f132008-05-28 12:30:31 +00002800 cpu_exec_init_all(0);
bellard83fb7ad2004-07-05 21:25:26 +00002801 /* NOTE: we need to init the CPU at this stage to get
2802 qemu_host_page_size */
bellardaaed9092007-11-10 15:15:54 +00002803 env = cpu_init(cpu_model);
2804 if (!env) {
2805 fprintf(stderr, "Unable to find CPU definition\n");
2806 exit(1);
2807 }
Blue Swirlb55a37c2009-11-07 10:37:06 +00002808#if defined(TARGET_I386) || defined(TARGET_SPARC) || defined(TARGET_PPC)
2809 cpu_reset(env);
2810#endif
2811
pbrookd5975362008-06-07 20:50:51 +00002812 thread_env = env;
ths3b46e622007-09-17 08:09:54 +00002813
bellardb6741952007-11-11 14:46:06 +00002814 if (getenv("QEMU_STRACE")) {
2815 do_strace = 1;
thsb92c47c2007-11-01 00:07:38 +00002816 }
2817
aurel3204a6dfe2009-01-30 19:59:17 +00002818 target_environ = envlist_to_environ(envlist, NULL);
2819 envlist_free(envlist);
thsb12b6a12007-06-17 16:38:39 +00002820
Paul Brook379f6692009-07-17 12:48:08 +01002821#if defined(CONFIG_USE_GUEST_BASE)
2822 /*
2823 * Now that page sizes are configured in cpu_init() we can do
2824 * proper page alignment for guest_base.
2825 */
2826 guest_base = HOST_PAGE_ALIGN(guest_base);
Richard Henderson14f24e12010-03-10 15:39:07 -08002827#endif /* CONFIG_USE_GUEST_BASE */
Paul Brook379f6692009-07-17 12:48:08 +01002828
2829 /*
2830 * Read in mmap_min_addr kernel parameter. This value is used
2831 * When loading the ELF image to determine whether guest_base
Richard Henderson14f24e12010-03-10 15:39:07 -08002832 * is needed. It is also used in mmap_find_vma.
Paul Brook379f6692009-07-17 12:48:08 +01002833 */
Richard Henderson14f24e12010-03-10 15:39:07 -08002834 {
Paul Brook379f6692009-07-17 12:48:08 +01002835 FILE *fp;
2836
2837 if ((fp = fopen("/proc/sys/vm/mmap_min_addr", "r")) != NULL) {
2838 unsigned long tmp;
2839 if (fscanf(fp, "%lu", &tmp) == 1) {
2840 mmap_min_addr = tmp;
2841 qemu_log("host mmap_min_addr=0x%lx\n", mmap_min_addr);
2842 }
2843 fclose(fp);
2844 }
2845 }
Paul Brook379f6692009-07-17 12:48:08 +01002846
aurel327d8cec92009-04-15 16:11:52 +00002847 /*
2848 * Prepare copy of argv vector for target.
2849 */
2850 target_argc = argc - optind;
2851 target_argv = calloc(target_argc + 1, sizeof (char *));
2852 if (target_argv == NULL) {
2853 (void) fprintf(stderr, "Unable to allocate memory for target_argv\n");
2854 exit(1);
2855 }
2856
2857 /*
2858 * If argv0 is specified (using '-0' switch) we replace
2859 * argv[0] pointer with the given one.
2860 */
2861 i = 0;
2862 if (argv0 != NULL) {
2863 target_argv[i++] = strdup(argv0);
2864 }
2865 for (; i < target_argc; i++) {
2866 target_argv[i] = strdup(argv[optind + i]);
2867 }
2868 target_argv[target_argc] = NULL;
2869
Mika Westerbergedf8e2a2009-04-07 09:57:11 +03002870 memset(ts, 0, sizeof(TaskState));
2871 init_task_state(ts);
2872 /* build Task State */
2873 ts->info = info;
2874 ts->bprm = &bprm;
2875 env->opaque = ts;
2876 task_settid(ts);
2877
Arnaud Patardfd4d81d2009-06-19 10:39:36 +03002878 ret = loader_exec(filename, target_argv, target_environ, regs,
2879 info, &bprm);
2880 if (ret != 0) {
2881 printf("Error %d while loading %s\n", ret, filename);
thsb12b6a12007-06-17 16:38:39 +00002882 _exit(1);
2883 }
2884
aurel327d8cec92009-04-15 16:11:52 +00002885 for (i = 0; i < target_argc; i++) {
2886 free(target_argv[i]);
2887 }
2888 free(target_argv);
2889
thsb12b6a12007-06-17 16:38:39 +00002890 for (wrk = target_environ; *wrk; wrk++) {
2891 free(*wrk);
bellard31e31b82003-02-18 22:55:36 +00002892 }
ths3b46e622007-09-17 08:09:54 +00002893
thsb12b6a12007-06-17 16:38:39 +00002894 free(target_environ);
2895
blueswir12e77eac2009-01-20 16:57:34 +00002896 if (qemu_log_enabled()) {
Paul Brook379f6692009-07-17 12:48:08 +01002897#if defined(CONFIG_USE_GUEST_BASE)
2898 qemu_log("guest_base 0x%lx\n", guest_base);
2899#endif
blueswir12e77eac2009-01-20 16:57:34 +00002900 log_page_dump();
ths3b46e622007-09-17 08:09:54 +00002901
blueswir12e77eac2009-01-20 16:57:34 +00002902 qemu_log("start_brk 0x" TARGET_ABI_FMT_lx "\n", info->start_brk);
2903 qemu_log("end_code 0x" TARGET_ABI_FMT_lx "\n", info->end_code);
2904 qemu_log("start_code 0x" TARGET_ABI_FMT_lx "\n",
2905 info->start_code);
2906 qemu_log("start_data 0x" TARGET_ABI_FMT_lx "\n",
2907 info->start_data);
2908 qemu_log("end_data 0x" TARGET_ABI_FMT_lx "\n", info->end_data);
2909 qemu_log("start_stack 0x" TARGET_ABI_FMT_lx "\n",
2910 info->start_stack);
2911 qemu_log("brk 0x" TARGET_ABI_FMT_lx "\n", info->brk);
2912 qemu_log("entry 0x" TARGET_ABI_FMT_lx "\n", info->entry);
2913 }
bellard31e31b82003-02-18 22:55:36 +00002914
pbrook53a59602006-03-25 19:31:22 +00002915 target_set_brk(info->brk);
bellard31e31b82003-02-18 22:55:36 +00002916 syscall_init();
bellard66fb9762003-03-23 01:06:05 +00002917 signal_init();
bellard31e31b82003-02-18 22:55:36 +00002918
bellardb346ff42003-06-15 20:05:50 +00002919#if defined(TARGET_I386)
bellard2e255c62003-08-21 23:25:21 +00002920 cpu_x86_set_cpl(env, 3);
2921
bellard3802ce22003-07-26 18:02:28 +00002922 env->cr[0] = CR0_PG_MASK | CR0_WP_MASK | CR0_PE_MASK;
bellard1bde4652005-01-12 22:34:47 +00002923 env->hflags |= HF_PE_MASK;
2924 if (env->cpuid_features & CPUID_SSE) {
2925 env->cr[4] |= CR4_OSFXSR_MASK;
2926 env->hflags |= HF_OSFXSR_MASK;
2927 }
bellardd2fd1af2007-11-14 18:08:56 +00002928#ifndef TARGET_ABI32
bellard4dbc4222007-11-15 15:27:03 +00002929 /* enable 64 bit mode if possible */
2930 if (!(env->cpuid_ext2_features & CPUID_EXT2_LM)) {
2931 fprintf(stderr, "The selected x86 CPU does not support 64 bit mode\n");
2932 exit(1);
2933 }
bellardd2fd1af2007-11-14 18:08:56 +00002934 env->cr[4] |= CR4_PAE_MASK;
bellard4dbc4222007-11-15 15:27:03 +00002935 env->efer |= MSR_EFER_LMA | MSR_EFER_LME;
bellardd2fd1af2007-11-14 18:08:56 +00002936 env->hflags |= HF_LMA_MASK;
2937#endif
bellard1bde4652005-01-12 22:34:47 +00002938
bellard415e5612004-02-03 23:37:12 +00002939 /* flags setup : we activate the IRQs by default as in user mode */
2940 env->eflags |= IF_MASK;
ths3b46e622007-09-17 08:09:54 +00002941
bellard6dbad632003-03-16 18:05:05 +00002942 /* linux register setup */
bellardd2fd1af2007-11-14 18:08:56 +00002943#ifndef TARGET_ABI32
j_mayer84409dd2007-04-06 08:56:50 +00002944 env->regs[R_EAX] = regs->rax;
2945 env->regs[R_EBX] = regs->rbx;
2946 env->regs[R_ECX] = regs->rcx;
2947 env->regs[R_EDX] = regs->rdx;
2948 env->regs[R_ESI] = regs->rsi;
2949 env->regs[R_EDI] = regs->rdi;
2950 env->regs[R_EBP] = regs->rbp;
2951 env->regs[R_ESP] = regs->rsp;
2952 env->eip = regs->rip;
2953#else
bellard0ecfa992003-03-03 14:32:43 +00002954 env->regs[R_EAX] = regs->eax;
2955 env->regs[R_EBX] = regs->ebx;
2956 env->regs[R_ECX] = regs->ecx;
2957 env->regs[R_EDX] = regs->edx;
2958 env->regs[R_ESI] = regs->esi;
2959 env->regs[R_EDI] = regs->edi;
2960 env->regs[R_EBP] = regs->ebp;
2961 env->regs[R_ESP] = regs->esp;
bellarddab2ed92003-03-22 15:23:14 +00002962 env->eip = regs->eip;
j_mayer84409dd2007-04-06 08:56:50 +00002963#endif
bellard31e31b82003-02-18 22:55:36 +00002964
bellardf4beb512003-05-27 23:28:08 +00002965 /* linux interrupt setup */
balroge4415702008-11-10 02:55:33 +00002966#ifndef TARGET_ABI32
2967 env->idt.limit = 511;
2968#else
2969 env->idt.limit = 255;
2970#endif
2971 env->idt.base = target_mmap(0, sizeof(uint64_t) * (env->idt.limit + 1),
2972 PROT_READ|PROT_WRITE,
2973 MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
2974 idt_table = g2h(env->idt.base);
bellardf4beb512003-05-27 23:28:08 +00002975 set_idt(0, 0);
2976 set_idt(1, 0);
2977 set_idt(2, 0);
2978 set_idt(3, 3);
2979 set_idt(4, 3);
bellardec95da62008-05-12 12:23:31 +00002980 set_idt(5, 0);
bellardf4beb512003-05-27 23:28:08 +00002981 set_idt(6, 0);
2982 set_idt(7, 0);
2983 set_idt(8, 0);
2984 set_idt(9, 0);
2985 set_idt(10, 0);
2986 set_idt(11, 0);
2987 set_idt(12, 0);
2988 set_idt(13, 0);
2989 set_idt(14, 0);
2990 set_idt(15, 0);
2991 set_idt(16, 0);
2992 set_idt(17, 0);
2993 set_idt(18, 0);
2994 set_idt(19, 0);
2995 set_idt(0x80, 3);
2996
bellard6dbad632003-03-16 18:05:05 +00002997 /* linux segment setup */
bellard8d18e892007-11-14 15:18:40 +00002998 {
2999 uint64_t *gdt_table;
balroge4415702008-11-10 02:55:33 +00003000 env->gdt.base = target_mmap(0, sizeof(uint64_t) * TARGET_GDT_ENTRIES,
3001 PROT_READ|PROT_WRITE,
3002 MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
bellard8d18e892007-11-14 15:18:40 +00003003 env->gdt.limit = sizeof(uint64_t) * TARGET_GDT_ENTRIES - 1;
balroge4415702008-11-10 02:55:33 +00003004 gdt_table = g2h(env->gdt.base);
bellardd2fd1af2007-11-14 18:08:56 +00003005#ifdef TARGET_ABI32
bellard8d18e892007-11-14 15:18:40 +00003006 write_dt(&gdt_table[__USER_CS >> 3], 0, 0xfffff,
3007 DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | DESC_S_MASK |
3008 (3 << DESC_DPL_SHIFT) | (0xa << DESC_TYPE_SHIFT));
bellardd2fd1af2007-11-14 18:08:56 +00003009#else
3010 /* 64 bit code segment */
3011 write_dt(&gdt_table[__USER_CS >> 3], 0, 0xfffff,
3012 DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | DESC_S_MASK |
3013 DESC_L_MASK |
3014 (3 << DESC_DPL_SHIFT) | (0xa << DESC_TYPE_SHIFT));
3015#endif
bellard8d18e892007-11-14 15:18:40 +00003016 write_dt(&gdt_table[__USER_DS >> 3], 0, 0xfffff,
3017 DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | DESC_S_MASK |
3018 (3 << DESC_DPL_SHIFT) | (0x2 << DESC_TYPE_SHIFT));
3019 }
bellard6dbad632003-03-16 18:05:05 +00003020 cpu_x86_load_seg(env, R_CS, __USER_CS);
bellardd2fd1af2007-11-14 18:08:56 +00003021 cpu_x86_load_seg(env, R_SS, __USER_DS);
3022#ifdef TARGET_ABI32
bellard6dbad632003-03-16 18:05:05 +00003023 cpu_x86_load_seg(env, R_DS, __USER_DS);
3024 cpu_x86_load_seg(env, R_ES, __USER_DS);
bellard6dbad632003-03-16 18:05:05 +00003025 cpu_x86_load_seg(env, R_FS, __USER_DS);
3026 cpu_x86_load_seg(env, R_GS, __USER_DS);
thsd6eb40f2007-06-21 22:55:02 +00003027 /* This hack makes Wine work... */
3028 env->segs[R_FS].selector = 0;
bellardd2fd1af2007-11-14 18:08:56 +00003029#else
3030 cpu_x86_load_seg(env, R_DS, 0);
3031 cpu_x86_load_seg(env, R_ES, 0);
3032 cpu_x86_load_seg(env, R_FS, 0);
3033 cpu_x86_load_seg(env, R_GS, 0);
3034#endif
bellardb346ff42003-06-15 20:05:50 +00003035#elif defined(TARGET_ARM)
3036 {
3037 int i;
bellardb5ff1b32005-11-26 10:38:39 +00003038 cpsr_write(env, regs->uregs[16], 0xffffffff);
bellardb346ff42003-06-15 20:05:50 +00003039 for(i = 0; i < 16; i++) {
3040 env->regs[i] = regs->uregs[i];
3041 }
bellardb346ff42003-06-15 20:05:50 +00003042 }
bellard93ac68b2003-09-30 20:57:29 +00003043#elif defined(TARGET_SPARC)
bellard060366c2004-01-04 15:50:01 +00003044 {
3045 int i;
3046 env->pc = regs->pc;
3047 env->npc = regs->npc;
3048 env->y = regs->y;
3049 for(i = 0; i < 8; i++)
3050 env->gregs[i] = regs->u_regs[i];
3051 for(i = 0; i < 8; i++)
3052 env->regwptr[i] = regs->u_regs[i + 8];
3053 }
bellard67867302003-11-23 17:05:30 +00003054#elif defined(TARGET_PPC)
3055 {
3056 int i;
bellard3fc6c082005-07-02 20:59:34 +00003057
j_mayer0411a972007-10-25 21:35:50 +00003058#if defined(TARGET_PPC64)
3059#if defined(TARGET_ABI32)
3060 env->msr &= ~((target_ulong)1 << MSR_SF);
j_mayere85e7c62007-10-18 19:59:49 +00003061#else
j_mayer0411a972007-10-25 21:35:50 +00003062 env->msr |= (target_ulong)1 << MSR_SF;
3063#endif
j_mayer84409dd2007-04-06 08:56:50 +00003064#endif
bellard67867302003-11-23 17:05:30 +00003065 env->nip = regs->nip;
3066 for(i = 0; i < 32; i++) {
3067 env->gpr[i] = regs->gpr[i];
3068 }
3069 }
pbrooke6e59062006-10-22 00:18:54 +00003070#elif defined(TARGET_M68K)
3071 {
pbrooke6e59062006-10-22 00:18:54 +00003072 env->pc = regs->pc;
3073 env->dregs[0] = regs->d0;
3074 env->dregs[1] = regs->d1;
3075 env->dregs[2] = regs->d2;
3076 env->dregs[3] = regs->d3;
3077 env->dregs[4] = regs->d4;
3078 env->dregs[5] = regs->d5;
3079 env->dregs[6] = regs->d6;
3080 env->dregs[7] = regs->d7;
3081 env->aregs[0] = regs->a0;
3082 env->aregs[1] = regs->a1;
3083 env->aregs[2] = regs->a2;
3084 env->aregs[3] = regs->a3;
3085 env->aregs[4] = regs->a4;
3086 env->aregs[5] = regs->a5;
3087 env->aregs[6] = regs->a6;
3088 env->aregs[7] = regs->usp;
3089 env->sr = regs->sr;
3090 ts->sim_syscalls = 1;
3091 }
Edgar E. Iglesiasb779e292009-05-20 21:31:33 +02003092#elif defined(TARGET_MICROBLAZE)
3093 {
3094 env->regs[0] = regs->r0;
3095 env->regs[1] = regs->r1;
3096 env->regs[2] = regs->r2;
3097 env->regs[3] = regs->r3;
3098 env->regs[4] = regs->r4;
3099 env->regs[5] = regs->r5;
3100 env->regs[6] = regs->r6;
3101 env->regs[7] = regs->r7;
3102 env->regs[8] = regs->r8;
3103 env->regs[9] = regs->r9;
3104 env->regs[10] = regs->r10;
3105 env->regs[11] = regs->r11;
3106 env->regs[12] = regs->r12;
3107 env->regs[13] = regs->r13;
3108 env->regs[14] = regs->r14;
3109 env->regs[15] = regs->r15;
3110 env->regs[16] = regs->r16;
3111 env->regs[17] = regs->r17;
3112 env->regs[18] = regs->r18;
3113 env->regs[19] = regs->r19;
3114 env->regs[20] = regs->r20;
3115 env->regs[21] = regs->r21;
3116 env->regs[22] = regs->r22;
3117 env->regs[23] = regs->r23;
3118 env->regs[24] = regs->r24;
3119 env->regs[25] = regs->r25;
3120 env->regs[26] = regs->r26;
3121 env->regs[27] = regs->r27;
3122 env->regs[28] = regs->r28;
3123 env->regs[29] = regs->r29;
3124 env->regs[30] = regs->r30;
3125 env->regs[31] = regs->r31;
3126 env->sregs[SR_PC] = regs->pc;
3127 }
bellard048f6b42005-11-26 18:47:20 +00003128#elif defined(TARGET_MIPS)
3129 {
3130 int i;
3131
3132 for(i = 0; i < 32; i++) {
thsb5dc7732008-06-27 10:02:35 +00003133 env->active_tc.gpr[i] = regs->regs[i];
bellard048f6b42005-11-26 18:47:20 +00003134 }
thsb5dc7732008-06-27 10:02:35 +00003135 env->active_tc.PC = regs->cp0_epc;
bellard048f6b42005-11-26 18:47:20 +00003136 }
bellardfdf9b3e2006-04-27 21:07:38 +00003137#elif defined(TARGET_SH4)
3138 {
3139 int i;
3140
3141 for(i = 0; i < 16; i++) {
3142 env->gregs[i] = regs->regs[i];
3143 }
3144 env->pc = regs->pc;
3145 }
j_mayer7a3148a2007-04-05 07:13:51 +00003146#elif defined(TARGET_ALPHA)
3147 {
3148 int i;
3149
3150 for(i = 0; i < 28; i++) {
blueswir1992f48a2007-10-14 16:27:31 +00003151 env->ir[i] = ((abi_ulong *)regs)[i];
j_mayer7a3148a2007-04-05 07:13:51 +00003152 }
Richard Hendersondad081e2010-01-04 11:19:14 -08003153 env->ir[IR_SP] = regs->usp;
j_mayer7a3148a2007-04-05 07:13:51 +00003154 env->pc = regs->pc;
j_mayer7a3148a2007-04-05 07:13:51 +00003155 }
ths48733d12007-10-08 13:36:46 +00003156#elif defined(TARGET_CRIS)
3157 {
3158 env->regs[0] = regs->r0;
3159 env->regs[1] = regs->r1;
3160 env->regs[2] = regs->r2;
3161 env->regs[3] = regs->r3;
3162 env->regs[4] = regs->r4;
3163 env->regs[5] = regs->r5;
3164 env->regs[6] = regs->r6;
3165 env->regs[7] = regs->r7;
3166 env->regs[8] = regs->r8;
3167 env->regs[9] = regs->r9;
3168 env->regs[10] = regs->r10;
3169 env->regs[11] = regs->r11;
3170 env->regs[12] = regs->r12;
3171 env->regs[13] = regs->r13;
3172 env->regs[14] = info->start_stack;
3173 env->regs[15] = regs->acr;
3174 env->pc = regs->erp;
3175 }
bellardb346ff42003-06-15 20:05:50 +00003176#else
3177#error unsupported target CPU
3178#endif
bellard31e31b82003-02-18 22:55:36 +00003179
pbrooka87295e2007-05-26 15:09:38 +00003180#if defined(TARGET_ARM) || defined(TARGET_M68K)
3181 ts->stack_base = info->start_stack;
3182 ts->heap_base = info->brk;
3183 /* This will be filled in on the first SYS_HEAPINFO call. */
3184 ts->heap_limit = 0;
3185#endif
3186
bellard74c33be2005-10-30 21:01:05 +00003187 if (gdbstub_port) {
3188 gdbserver_start (gdbstub_port);
bellard1fddef42005-04-17 19:16:13 +00003189 gdb_handlesig(env, 0);
3190 }
bellard1b6b0292003-03-22 17:31:38 +00003191 cpu_loop(env);
3192 /* never exits */
bellard31e31b82003-02-18 22:55:36 +00003193 return 0;
3194}