]> git.proxmox.com Git - mirror_qemu.git/blob - linux-user/main.c
7c223a56292ff2d2954d875dfe99f1411839eda5
[mirror_qemu.git] / linux-user / main.c
1 /*
2 * qemu user main
3 *
4 * Copyright (c) 2003-2008 Fabrice Bellard
5 *
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
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
18 */
19 #include "qemu/osdep.h"
20 #include "qemu-version.h"
21 #include <sys/syscall.h>
22 #include <sys/resource.h>
23
24 #include "qapi/error.h"
25 #include "qemu.h"
26 #include "qemu/path.h"
27 #include "qemu/config-file.h"
28 #include "qemu/cutils.h"
29 #include "qemu/help_option.h"
30 #include "cpu.h"
31 #include "exec/exec-all.h"
32 #include "tcg.h"
33 #include "qemu/timer.h"
34 #include "qemu/envlist.h"
35 #include "elf.h"
36 #include "trace/control.h"
37 #include "target_elf.h"
38 #include "cpu_loop-common.h"
39
40 char *exec_path;
41
42 int singlestep;
43 static const char *filename;
44 static const char *argv0;
45 static int gdbstub_port;
46 static envlist_t *envlist;
47 static const char *cpu_model;
48 static const char *cpu_type;
49 unsigned long mmap_min_addr;
50 unsigned long guest_base;
51 int have_guest_base;
52
53 /*
54 * When running 32-on-64 we should make sure we can fit all of the possible
55 * guest address space into a contiguous chunk of virtual host memory.
56 *
57 * This way we will never overlap with our own libraries or binaries or stack
58 * or anything else that QEMU maps.
59 *
60 * Many cpus reserve the high bit (or more than one for some 64-bit cpus)
61 * of the address for the kernel. Some cpus rely on this and user space
62 * uses the high bit(s) for pointer tagging and the like. For them, we
63 * must preserve the expected address space.
64 */
65 #ifndef MAX_RESERVED_VA
66 # if HOST_LONG_BITS > TARGET_VIRT_ADDR_SPACE_BITS
67 # if TARGET_VIRT_ADDR_SPACE_BITS == 32 && \
68 (TARGET_LONG_BITS == 32 || defined(TARGET_ABI32))
69 /* There are a number of places where we assign reserved_va to a variable
70 of type abi_ulong and expect it to fit. Avoid the last page. */
71 # define MAX_RESERVED_VA (0xfffffffful & TARGET_PAGE_MASK)
72 # else
73 # define MAX_RESERVED_VA (1ul << TARGET_VIRT_ADDR_SPACE_BITS)
74 # endif
75 # else
76 # define MAX_RESERVED_VA 0
77 # endif
78 #endif
79
80 /* That said, reserving *too* much vm space via mmap can run into problems
81 with rlimits, oom due to page table creation, etc. We will still try it,
82 if directed by the command-line option, but not by default. */
83 #if HOST_LONG_BITS == 64 && TARGET_VIRT_ADDR_SPACE_BITS <= 32
84 unsigned long reserved_va = MAX_RESERVED_VA;
85 #else
86 unsigned long reserved_va;
87 #endif
88
89 static void usage(int exitcode);
90
91 static const char *interp_prefix = CONFIG_QEMU_INTERP_PREFIX;
92 const char *qemu_uname_release;
93
94 /* XXX: on x86 MAP_GROWSDOWN only works if ESP <= address + 32, so
95 we allocate a bigger stack. Need a better solution, for example
96 by remapping the process stack directly at the right place */
97 unsigned long guest_stack_size = 8 * 1024 * 1024UL;
98
99 void gemu_log(const char *fmt, ...)
100 {
101 va_list ap;
102
103 va_start(ap, fmt);
104 vfprintf(stderr, fmt, ap);
105 va_end(ap);
106 }
107
108 #if defined(TARGET_I386)
109 int cpu_get_pic_interrupt(CPUX86State *env)
110 {
111 return -1;
112 }
113 #endif
114
115 /***********************************************************/
116 /* Helper routines for implementing atomic operations. */
117
118 /* Make sure everything is in a consistent state for calling fork(). */
119 void fork_start(void)
120 {
121 start_exclusive();
122 mmap_fork_start();
123 qemu_mutex_lock(&tb_ctx.tb_lock);
124 cpu_list_lock();
125 }
126
127 void fork_end(int child)
128 {
129 mmap_fork_end(child);
130 if (child) {
131 CPUState *cpu, *next_cpu;
132 /* Child processes created by fork() only have a single thread.
133 Discard information about the parent threads. */
134 CPU_FOREACH_SAFE(cpu, next_cpu) {
135 if (cpu != thread_cpu) {
136 QTAILQ_REMOVE(&cpus, cpu, node);
137 }
138 }
139 qemu_mutex_init(&tb_ctx.tb_lock);
140 qemu_init_cpu_list();
141 gdbserver_fork(thread_cpu);
142 /* qemu_init_cpu_list() takes care of reinitializing the
143 * exclusive state, so we don't need to end_exclusive() here.
144 */
145 } else {
146 qemu_mutex_unlock(&tb_ctx.tb_lock);
147 cpu_list_unlock();
148 end_exclusive();
149 }
150 }
151
152 #ifdef TARGET_ALPHA
153 void cpu_loop(CPUAlphaState *env)
154 {
155 CPUState *cs = CPU(alpha_env_get_cpu(env));
156 int trapnr;
157 target_siginfo_t info;
158 abi_long sysret;
159
160 while (1) {
161 bool arch_interrupt = true;
162
163 cpu_exec_start(cs);
164 trapnr = cpu_exec(cs);
165 cpu_exec_end(cs);
166 process_queued_cpu_work(cs);
167
168 switch (trapnr) {
169 case EXCP_RESET:
170 fprintf(stderr, "Reset requested. Exit\n");
171 exit(EXIT_FAILURE);
172 break;
173 case EXCP_MCHK:
174 fprintf(stderr, "Machine check exception. Exit\n");
175 exit(EXIT_FAILURE);
176 break;
177 case EXCP_SMP_INTERRUPT:
178 case EXCP_CLK_INTERRUPT:
179 case EXCP_DEV_INTERRUPT:
180 fprintf(stderr, "External interrupt. Exit\n");
181 exit(EXIT_FAILURE);
182 break;
183 case EXCP_MMFAULT:
184 info.si_signo = TARGET_SIGSEGV;
185 info.si_errno = 0;
186 info.si_code = (page_get_flags(env->trap_arg0) & PAGE_VALID
187 ? TARGET_SEGV_ACCERR : TARGET_SEGV_MAPERR);
188 info._sifields._sigfault._addr = env->trap_arg0;
189 queue_signal(env, info.si_signo, QEMU_SI_FAULT, &info);
190 break;
191 case EXCP_UNALIGN:
192 info.si_signo = TARGET_SIGBUS;
193 info.si_errno = 0;
194 info.si_code = TARGET_BUS_ADRALN;
195 info._sifields._sigfault._addr = env->trap_arg0;
196 queue_signal(env, info.si_signo, QEMU_SI_FAULT, &info);
197 break;
198 case EXCP_OPCDEC:
199 do_sigill:
200 info.si_signo = TARGET_SIGILL;
201 info.si_errno = 0;
202 info.si_code = TARGET_ILL_ILLOPC;
203 info._sifields._sigfault._addr = env->pc;
204 queue_signal(env, info.si_signo, QEMU_SI_FAULT, &info);
205 break;
206 case EXCP_ARITH:
207 info.si_signo = TARGET_SIGFPE;
208 info.si_errno = 0;
209 info.si_code = TARGET_FPE_FLTINV;
210 info._sifields._sigfault._addr = env->pc;
211 queue_signal(env, info.si_signo, QEMU_SI_FAULT, &info);
212 break;
213 case EXCP_FEN:
214 /* No-op. Linux simply re-enables the FPU. */
215 break;
216 case EXCP_CALL_PAL:
217 switch (env->error_code) {
218 case 0x80:
219 /* BPT */
220 info.si_signo = TARGET_SIGTRAP;
221 info.si_errno = 0;
222 info.si_code = TARGET_TRAP_BRKPT;
223 info._sifields._sigfault._addr = env->pc;
224 queue_signal(env, info.si_signo, QEMU_SI_FAULT, &info);
225 break;
226 case 0x81:
227 /* BUGCHK */
228 info.si_signo = TARGET_SIGTRAP;
229 info.si_errno = 0;
230 info.si_code = 0;
231 info._sifields._sigfault._addr = env->pc;
232 queue_signal(env, info.si_signo, QEMU_SI_FAULT, &info);
233 break;
234 case 0x83:
235 /* CALLSYS */
236 trapnr = env->ir[IR_V0];
237 sysret = do_syscall(env, trapnr,
238 env->ir[IR_A0], env->ir[IR_A1],
239 env->ir[IR_A2], env->ir[IR_A3],
240 env->ir[IR_A4], env->ir[IR_A5],
241 0, 0);
242 if (sysret == -TARGET_ERESTARTSYS) {
243 env->pc -= 4;
244 break;
245 }
246 if (sysret == -TARGET_QEMU_ESIGRETURN) {
247 break;
248 }
249 /* Syscall writes 0 to V0 to bypass error check, similar
250 to how this is handled internal to Linux kernel.
251 (Ab)use trapnr temporarily as boolean indicating error. */
252 trapnr = (env->ir[IR_V0] != 0 && sysret < 0);
253 env->ir[IR_V0] = (trapnr ? -sysret : sysret);
254 env->ir[IR_A3] = trapnr;
255 break;
256 case 0x86:
257 /* IMB */
258 /* ??? We can probably elide the code using page_unprotect
259 that is checking for self-modifying code. Instead we
260 could simply call tb_flush here. Until we work out the
261 changes required to turn off the extra write protection,
262 this can be a no-op. */
263 break;
264 case 0x9E:
265 /* RDUNIQUE */
266 /* Handled in the translator for usermode. */
267 abort();
268 case 0x9F:
269 /* WRUNIQUE */
270 /* Handled in the translator for usermode. */
271 abort();
272 case 0xAA:
273 /* GENTRAP */
274 info.si_signo = TARGET_SIGFPE;
275 switch (env->ir[IR_A0]) {
276 case TARGET_GEN_INTOVF:
277 info.si_code = TARGET_FPE_INTOVF;
278 break;
279 case TARGET_GEN_INTDIV:
280 info.si_code = TARGET_FPE_INTDIV;
281 break;
282 case TARGET_GEN_FLTOVF:
283 info.si_code = TARGET_FPE_FLTOVF;
284 break;
285 case TARGET_GEN_FLTUND:
286 info.si_code = TARGET_FPE_FLTUND;
287 break;
288 case TARGET_GEN_FLTINV:
289 info.si_code = TARGET_FPE_FLTINV;
290 break;
291 case TARGET_GEN_FLTINE:
292 info.si_code = TARGET_FPE_FLTRES;
293 break;
294 case TARGET_GEN_ROPRAND:
295 info.si_code = 0;
296 break;
297 default:
298 info.si_signo = TARGET_SIGTRAP;
299 info.si_code = 0;
300 break;
301 }
302 info.si_errno = 0;
303 info._sifields._sigfault._addr = env->pc;
304 queue_signal(env, info.si_signo, QEMU_SI_FAULT, &info);
305 break;
306 default:
307 goto do_sigill;
308 }
309 break;
310 case EXCP_DEBUG:
311 info.si_signo = gdb_handlesig(cs, TARGET_SIGTRAP);
312 if (info.si_signo) {
313 info.si_errno = 0;
314 info.si_code = TARGET_TRAP_BRKPT;
315 queue_signal(env, info.si_signo, QEMU_SI_FAULT, &info);
316 } else {
317 arch_interrupt = false;
318 }
319 break;
320 case EXCP_INTERRUPT:
321 /* Just indicate that signals should be handled asap. */
322 break;
323 case EXCP_ATOMIC:
324 cpu_exec_step_atomic(cs);
325 arch_interrupt = false;
326 break;
327 default:
328 printf ("Unhandled trap: 0x%x\n", trapnr);
329 cpu_dump_state(cs, stderr, fprintf, 0);
330 exit(EXIT_FAILURE);
331 }
332 process_pending_signals (env);
333
334 /* Most of the traps imply a transition through PALcode, which
335 implies an REI instruction has been executed. Which means
336 that RX and LOCK_ADDR should be cleared. But there are a
337 few exceptions for traps internal to QEMU. */
338 if (arch_interrupt) {
339 env->flags &= ~ENV_FLAG_RX_FLAG;
340 env->lock_addr = -1;
341 }
342 }
343 }
344 #endif /* TARGET_ALPHA */
345
346 #ifdef TARGET_S390X
347
348 /* s390x masks the fault address it reports in si_addr for SIGSEGV and SIGBUS */
349 #define S390X_FAIL_ADDR_MASK -4096LL
350
351 void cpu_loop(CPUS390XState *env)
352 {
353 CPUState *cs = CPU(s390_env_get_cpu(env));
354 int trapnr, n, sig;
355 target_siginfo_t info;
356 target_ulong addr;
357 abi_long ret;
358
359 while (1) {
360 cpu_exec_start(cs);
361 trapnr = cpu_exec(cs);
362 cpu_exec_end(cs);
363 process_queued_cpu_work(cs);
364
365 switch (trapnr) {
366 case EXCP_INTERRUPT:
367 /* Just indicate that signals should be handled asap. */
368 break;
369
370 case EXCP_SVC:
371 n = env->int_svc_code;
372 if (!n) {
373 /* syscalls > 255 */
374 n = env->regs[1];
375 }
376 env->psw.addr += env->int_svc_ilen;
377 ret = do_syscall(env, n, env->regs[2], env->regs[3],
378 env->regs[4], env->regs[5],
379 env->regs[6], env->regs[7], 0, 0);
380 if (ret == -TARGET_ERESTARTSYS) {
381 env->psw.addr -= env->int_svc_ilen;
382 } else if (ret != -TARGET_QEMU_ESIGRETURN) {
383 env->regs[2] = ret;
384 }
385 break;
386
387 case EXCP_DEBUG:
388 sig = gdb_handlesig(cs, TARGET_SIGTRAP);
389 if (sig) {
390 n = TARGET_TRAP_BRKPT;
391 goto do_signal_pc;
392 }
393 break;
394 case EXCP_PGM:
395 n = env->int_pgm_code;
396 switch (n) {
397 case PGM_OPERATION:
398 case PGM_PRIVILEGED:
399 sig = TARGET_SIGILL;
400 n = TARGET_ILL_ILLOPC;
401 goto do_signal_pc;
402 case PGM_PROTECTION:
403 case PGM_ADDRESSING:
404 sig = TARGET_SIGSEGV;
405 /* XXX: check env->error_code */
406 n = TARGET_SEGV_MAPERR;
407 addr = env->__excp_addr & S390X_FAIL_ADDR_MASK;
408 goto do_signal;
409 case PGM_EXECUTE:
410 case PGM_SPECIFICATION:
411 case PGM_SPECIAL_OP:
412 case PGM_OPERAND:
413 do_sigill_opn:
414 sig = TARGET_SIGILL;
415 n = TARGET_ILL_ILLOPN;
416 goto do_signal_pc;
417
418 case PGM_FIXPT_OVERFLOW:
419 sig = TARGET_SIGFPE;
420 n = TARGET_FPE_INTOVF;
421 goto do_signal_pc;
422 case PGM_FIXPT_DIVIDE:
423 sig = TARGET_SIGFPE;
424 n = TARGET_FPE_INTDIV;
425 goto do_signal_pc;
426
427 case PGM_DATA:
428 n = (env->fpc >> 8) & 0xff;
429 if (n == 0xff) {
430 /* compare-and-trap */
431 goto do_sigill_opn;
432 } else {
433 /* An IEEE exception, simulated or otherwise. */
434 if (n & 0x80) {
435 n = TARGET_FPE_FLTINV;
436 } else if (n & 0x40) {
437 n = TARGET_FPE_FLTDIV;
438 } else if (n & 0x20) {
439 n = TARGET_FPE_FLTOVF;
440 } else if (n & 0x10) {
441 n = TARGET_FPE_FLTUND;
442 } else if (n & 0x08) {
443 n = TARGET_FPE_FLTRES;
444 } else {
445 /* ??? Quantum exception; BFP, DFP error. */
446 goto do_sigill_opn;
447 }
448 sig = TARGET_SIGFPE;
449 goto do_signal_pc;
450 }
451
452 default:
453 fprintf(stderr, "Unhandled program exception: %#x\n", n);
454 cpu_dump_state(cs, stderr, fprintf, 0);
455 exit(EXIT_FAILURE);
456 }
457 break;
458
459 do_signal_pc:
460 addr = env->psw.addr;
461 do_signal:
462 info.si_signo = sig;
463 info.si_errno = 0;
464 info.si_code = n;
465 info._sifields._sigfault._addr = addr;
466 queue_signal(env, info.si_signo, QEMU_SI_FAULT, &info);
467 break;
468
469 case EXCP_ATOMIC:
470 cpu_exec_step_atomic(cs);
471 break;
472 default:
473 fprintf(stderr, "Unhandled trap: 0x%x\n", trapnr);
474 cpu_dump_state(cs, stderr, fprintf, 0);
475 exit(EXIT_FAILURE);
476 }
477 process_pending_signals (env);
478 }
479 }
480
481 #endif /* TARGET_S390X */
482
483 #ifdef TARGET_TILEGX
484
485 static void gen_sigill_reg(CPUTLGState *env)
486 {
487 target_siginfo_t info;
488
489 info.si_signo = TARGET_SIGILL;
490 info.si_errno = 0;
491 info.si_code = TARGET_ILL_PRVREG;
492 info._sifields._sigfault._addr = env->pc;
493 queue_signal(env, info.si_signo, QEMU_SI_FAULT, &info);
494 }
495
496 static void do_signal(CPUTLGState *env, int signo, int sigcode)
497 {
498 target_siginfo_t info;
499
500 info.si_signo = signo;
501 info.si_errno = 0;
502 info._sifields._sigfault._addr = env->pc;
503
504 if (signo == TARGET_SIGSEGV) {
505 /* The passed in sigcode is a dummy; check for a page mapping
506 and pass either MAPERR or ACCERR. */
507 target_ulong addr = env->excaddr;
508 info._sifields._sigfault._addr = addr;
509 if (page_check_range(addr, 1, PAGE_VALID) < 0) {
510 sigcode = TARGET_SEGV_MAPERR;
511 } else {
512 sigcode = TARGET_SEGV_ACCERR;
513 }
514 }
515 info.si_code = sigcode;
516
517 queue_signal(env, info.si_signo, QEMU_SI_FAULT, &info);
518 }
519
520 static void gen_sigsegv_maperr(CPUTLGState *env, target_ulong addr)
521 {
522 env->excaddr = addr;
523 do_signal(env, TARGET_SIGSEGV, 0);
524 }
525
526 static void set_regval(CPUTLGState *env, uint8_t reg, uint64_t val)
527 {
528 if (unlikely(reg >= TILEGX_R_COUNT)) {
529 switch (reg) {
530 case TILEGX_R_SN:
531 case TILEGX_R_ZERO:
532 return;
533 case TILEGX_R_IDN0:
534 case TILEGX_R_IDN1:
535 case TILEGX_R_UDN0:
536 case TILEGX_R_UDN1:
537 case TILEGX_R_UDN2:
538 case TILEGX_R_UDN3:
539 gen_sigill_reg(env);
540 return;
541 default:
542 g_assert_not_reached();
543 }
544 }
545 env->regs[reg] = val;
546 }
547
548 /*
549 * Compare the 8-byte contents of the CmpValue SPR with the 8-byte value in
550 * memory at the address held in the first source register. If the values are
551 * not equal, then no memory operation is performed. If the values are equal,
552 * the 8-byte quantity from the second source register is written into memory
553 * at the address held in the first source register. In either case, the result
554 * of the instruction is the value read from memory. The compare and write to
555 * memory are atomic and thus can be used for synchronization purposes. This
556 * instruction only operates for addresses aligned to a 8-byte boundary.
557 * Unaligned memory access causes an Unaligned Data Reference interrupt.
558 *
559 * Functional Description (64-bit)
560 * uint64_t memVal = memoryReadDoubleWord (rf[SrcA]);
561 * rf[Dest] = memVal;
562 * if (memVal == SPR[CmpValueSPR])
563 * memoryWriteDoubleWord (rf[SrcA], rf[SrcB]);
564 *
565 * Functional Description (32-bit)
566 * uint64_t memVal = signExtend32 (memoryReadWord (rf[SrcA]));
567 * rf[Dest] = memVal;
568 * if (memVal == signExtend32 (SPR[CmpValueSPR]))
569 * memoryWriteWord (rf[SrcA], rf[SrcB]);
570 *
571 *
572 * This function also processes exch and exch4 which need not process SPR.
573 */
574 static void do_exch(CPUTLGState *env, bool quad, bool cmp)
575 {
576 target_ulong addr;
577 target_long val, sprval;
578
579 start_exclusive();
580
581 addr = env->atomic_srca;
582 if (quad ? get_user_s64(val, addr) : get_user_s32(val, addr)) {
583 goto sigsegv_maperr;
584 }
585
586 if (cmp) {
587 if (quad) {
588 sprval = env->spregs[TILEGX_SPR_CMPEXCH];
589 } else {
590 sprval = sextract64(env->spregs[TILEGX_SPR_CMPEXCH], 0, 32);
591 }
592 }
593
594 if (!cmp || val == sprval) {
595 target_long valb = env->atomic_srcb;
596 if (quad ? put_user_u64(valb, addr) : put_user_u32(valb, addr)) {
597 goto sigsegv_maperr;
598 }
599 }
600
601 set_regval(env, env->atomic_dstr, val);
602 end_exclusive();
603 return;
604
605 sigsegv_maperr:
606 end_exclusive();
607 gen_sigsegv_maperr(env, addr);
608 }
609
610 static void do_fetch(CPUTLGState *env, int trapnr, bool quad)
611 {
612 int8_t write = 1;
613 target_ulong addr;
614 target_long val, valb;
615
616 start_exclusive();
617
618 addr = env->atomic_srca;
619 valb = env->atomic_srcb;
620 if (quad ? get_user_s64(val, addr) : get_user_s32(val, addr)) {
621 goto sigsegv_maperr;
622 }
623
624 switch (trapnr) {
625 case TILEGX_EXCP_OPCODE_FETCHADD:
626 case TILEGX_EXCP_OPCODE_FETCHADD4:
627 valb += val;
628 break;
629 case TILEGX_EXCP_OPCODE_FETCHADDGEZ:
630 valb += val;
631 if (valb < 0) {
632 write = 0;
633 }
634 break;
635 case TILEGX_EXCP_OPCODE_FETCHADDGEZ4:
636 valb += val;
637 if ((int32_t)valb < 0) {
638 write = 0;
639 }
640 break;
641 case TILEGX_EXCP_OPCODE_FETCHAND:
642 case TILEGX_EXCP_OPCODE_FETCHAND4:
643 valb &= val;
644 break;
645 case TILEGX_EXCP_OPCODE_FETCHOR:
646 case TILEGX_EXCP_OPCODE_FETCHOR4:
647 valb |= val;
648 break;
649 default:
650 g_assert_not_reached();
651 }
652
653 if (write) {
654 if (quad ? put_user_u64(valb, addr) : put_user_u32(valb, addr)) {
655 goto sigsegv_maperr;
656 }
657 }
658
659 set_regval(env, env->atomic_dstr, val);
660 end_exclusive();
661 return;
662
663 sigsegv_maperr:
664 end_exclusive();
665 gen_sigsegv_maperr(env, addr);
666 }
667
668 void cpu_loop(CPUTLGState *env)
669 {
670 CPUState *cs = CPU(tilegx_env_get_cpu(env));
671 int trapnr;
672
673 while (1) {
674 cpu_exec_start(cs);
675 trapnr = cpu_exec(cs);
676 cpu_exec_end(cs);
677 process_queued_cpu_work(cs);
678
679 switch (trapnr) {
680 case TILEGX_EXCP_SYSCALL:
681 {
682 abi_ulong ret = do_syscall(env, env->regs[TILEGX_R_NR],
683 env->regs[0], env->regs[1],
684 env->regs[2], env->regs[3],
685 env->regs[4], env->regs[5],
686 env->regs[6], env->regs[7]);
687 if (ret == -TARGET_ERESTARTSYS) {
688 env->pc -= 8;
689 } else if (ret != -TARGET_QEMU_ESIGRETURN) {
690 env->regs[TILEGX_R_RE] = ret;
691 env->regs[TILEGX_R_ERR] = TILEGX_IS_ERRNO(ret) ? -ret : 0;
692 }
693 break;
694 }
695 case TILEGX_EXCP_OPCODE_EXCH:
696 do_exch(env, true, false);
697 break;
698 case TILEGX_EXCP_OPCODE_EXCH4:
699 do_exch(env, false, false);
700 break;
701 case TILEGX_EXCP_OPCODE_CMPEXCH:
702 do_exch(env, true, true);
703 break;
704 case TILEGX_EXCP_OPCODE_CMPEXCH4:
705 do_exch(env, false, true);
706 break;
707 case TILEGX_EXCP_OPCODE_FETCHADD:
708 case TILEGX_EXCP_OPCODE_FETCHADDGEZ:
709 case TILEGX_EXCP_OPCODE_FETCHAND:
710 case TILEGX_EXCP_OPCODE_FETCHOR:
711 do_fetch(env, trapnr, true);
712 break;
713 case TILEGX_EXCP_OPCODE_FETCHADD4:
714 case TILEGX_EXCP_OPCODE_FETCHADDGEZ4:
715 case TILEGX_EXCP_OPCODE_FETCHAND4:
716 case TILEGX_EXCP_OPCODE_FETCHOR4:
717 do_fetch(env, trapnr, false);
718 break;
719 case TILEGX_EXCP_SIGNAL:
720 do_signal(env, env->signo, env->sigcode);
721 break;
722 case TILEGX_EXCP_REG_IDN_ACCESS:
723 case TILEGX_EXCP_REG_UDN_ACCESS:
724 gen_sigill_reg(env);
725 break;
726 case EXCP_ATOMIC:
727 cpu_exec_step_atomic(cs);
728 break;
729 default:
730 fprintf(stderr, "trapnr is %d[0x%x].\n", trapnr, trapnr);
731 g_assert_not_reached();
732 }
733 process_pending_signals(env);
734 }
735 }
736
737 #endif
738
739 #ifdef TARGET_RISCV
740
741 void cpu_loop(CPURISCVState *env)
742 {
743 CPUState *cs = CPU(riscv_env_get_cpu(env));
744 int trapnr, signum, sigcode;
745 target_ulong sigaddr;
746 target_ulong ret;
747
748 for (;;) {
749 cpu_exec_start(cs);
750 trapnr = cpu_exec(cs);
751 cpu_exec_end(cs);
752 process_queued_cpu_work(cs);
753
754 signum = 0;
755 sigcode = 0;
756 sigaddr = 0;
757
758 switch (trapnr) {
759 case EXCP_INTERRUPT:
760 /* just indicate that signals should be handled asap */
761 break;
762 case EXCP_ATOMIC:
763 cpu_exec_step_atomic(cs);
764 break;
765 case RISCV_EXCP_U_ECALL:
766 env->pc += 4;
767 if (env->gpr[xA7] == TARGET_NR_arch_specific_syscall + 15) {
768 /* riscv_flush_icache_syscall is a no-op in QEMU as
769 self-modifying code is automatically detected */
770 ret = 0;
771 } else {
772 ret = do_syscall(env,
773 env->gpr[xA7],
774 env->gpr[xA0],
775 env->gpr[xA1],
776 env->gpr[xA2],
777 env->gpr[xA3],
778 env->gpr[xA4],
779 env->gpr[xA5],
780 0, 0);
781 }
782 if (ret == -TARGET_ERESTARTSYS) {
783 env->pc -= 4;
784 } else if (ret != -TARGET_QEMU_ESIGRETURN) {
785 env->gpr[xA0] = ret;
786 }
787 if (cs->singlestep_enabled) {
788 goto gdbstep;
789 }
790 break;
791 case RISCV_EXCP_ILLEGAL_INST:
792 signum = TARGET_SIGILL;
793 sigcode = TARGET_ILL_ILLOPC;
794 break;
795 case RISCV_EXCP_BREAKPOINT:
796 signum = TARGET_SIGTRAP;
797 sigcode = TARGET_TRAP_BRKPT;
798 sigaddr = env->pc;
799 break;
800 case RISCV_EXCP_INST_PAGE_FAULT:
801 case RISCV_EXCP_LOAD_PAGE_FAULT:
802 case RISCV_EXCP_STORE_PAGE_FAULT:
803 signum = TARGET_SIGSEGV;
804 sigcode = TARGET_SEGV_MAPERR;
805 break;
806 case EXCP_DEBUG:
807 gdbstep:
808 signum = gdb_handlesig(cs, TARGET_SIGTRAP);
809 sigcode = TARGET_TRAP_BRKPT;
810 break;
811 default:
812 EXCP_DUMP(env, "\nqemu: unhandled CPU exception %#x - aborting\n",
813 trapnr);
814 exit(EXIT_FAILURE);
815 }
816
817 if (signum) {
818 target_siginfo_t info = {
819 .si_signo = signum,
820 .si_errno = 0,
821 .si_code = sigcode,
822 ._sifields._sigfault._addr = sigaddr
823 };
824 queue_signal(env, info.si_signo, QEMU_SI_KILL, &info);
825 }
826
827 process_pending_signals(env);
828 }
829 }
830
831 #endif /* TARGET_RISCV */
832
833 #ifdef TARGET_HPPA
834
835 static abi_ulong hppa_lws(CPUHPPAState *env)
836 {
837 uint32_t which = env->gr[20];
838 abi_ulong addr = env->gr[26];
839 abi_ulong old = env->gr[25];
840 abi_ulong new = env->gr[24];
841 abi_ulong size, ret;
842
843 switch (which) {
844 default:
845 return -TARGET_ENOSYS;
846
847 case 0: /* elf32 atomic 32bit cmpxchg */
848 if ((addr & 3) || !access_ok(VERIFY_WRITE, addr, 4)) {
849 return -TARGET_EFAULT;
850 }
851 old = tswap32(old);
852 new = tswap32(new);
853 ret = atomic_cmpxchg((uint32_t *)g2h(addr), old, new);
854 ret = tswap32(ret);
855 break;
856
857 case 2: /* elf32 atomic "new" cmpxchg */
858 size = env->gr[23];
859 if (size >= 4) {
860 return -TARGET_ENOSYS;
861 }
862 if (((addr | old | new) & ((1 << size) - 1))
863 || !access_ok(VERIFY_WRITE, addr, 1 << size)
864 || !access_ok(VERIFY_READ, old, 1 << size)
865 || !access_ok(VERIFY_READ, new, 1 << size)) {
866 return -TARGET_EFAULT;
867 }
868 /* Note that below we use host-endian loads so that the cmpxchg
869 can be host-endian as well. */
870 switch (size) {
871 case 0:
872 old = *(uint8_t *)g2h(old);
873 new = *(uint8_t *)g2h(new);
874 ret = atomic_cmpxchg((uint8_t *)g2h(addr), old, new);
875 ret = ret != old;
876 break;
877 case 1:
878 old = *(uint16_t *)g2h(old);
879 new = *(uint16_t *)g2h(new);
880 ret = atomic_cmpxchg((uint16_t *)g2h(addr), old, new);
881 ret = ret != old;
882 break;
883 case 2:
884 old = *(uint32_t *)g2h(old);
885 new = *(uint32_t *)g2h(new);
886 ret = atomic_cmpxchg((uint32_t *)g2h(addr), old, new);
887 ret = ret != old;
888 break;
889 case 3:
890 {
891 uint64_t o64, n64, r64;
892 o64 = *(uint64_t *)g2h(old);
893 n64 = *(uint64_t *)g2h(new);
894 #ifdef CONFIG_ATOMIC64
895 r64 = atomic_cmpxchg__nocheck((uint64_t *)g2h(addr), o64, n64);
896 ret = r64 != o64;
897 #else
898 start_exclusive();
899 r64 = *(uint64_t *)g2h(addr);
900 ret = 1;
901 if (r64 == o64) {
902 *(uint64_t *)g2h(addr) = n64;
903 ret = 0;
904 }
905 end_exclusive();
906 #endif
907 }
908 break;
909 }
910 break;
911 }
912
913 env->gr[28] = ret;
914 return 0;
915 }
916
917 void cpu_loop(CPUHPPAState *env)
918 {
919 CPUState *cs = CPU(hppa_env_get_cpu(env));
920 target_siginfo_t info;
921 abi_ulong ret;
922 int trapnr;
923
924 while (1) {
925 cpu_exec_start(cs);
926 trapnr = cpu_exec(cs);
927 cpu_exec_end(cs);
928 process_queued_cpu_work(cs);
929
930 switch (trapnr) {
931 case EXCP_SYSCALL:
932 ret = do_syscall(env, env->gr[20],
933 env->gr[26], env->gr[25],
934 env->gr[24], env->gr[23],
935 env->gr[22], env->gr[21], 0, 0);
936 switch (ret) {
937 default:
938 env->gr[28] = ret;
939 /* We arrived here by faking the gateway page. Return. */
940 env->iaoq_f = env->gr[31];
941 env->iaoq_b = env->gr[31] + 4;
942 break;
943 case -TARGET_ERESTARTSYS:
944 case -TARGET_QEMU_ESIGRETURN:
945 break;
946 }
947 break;
948 case EXCP_SYSCALL_LWS:
949 env->gr[21] = hppa_lws(env);
950 /* We arrived here by faking the gateway page. Return. */
951 env->iaoq_f = env->gr[31];
952 env->iaoq_b = env->gr[31] + 4;
953 break;
954 case EXCP_ITLB_MISS:
955 case EXCP_DTLB_MISS:
956 case EXCP_NA_ITLB_MISS:
957 case EXCP_NA_DTLB_MISS:
958 case EXCP_IMP:
959 case EXCP_DMP:
960 case EXCP_DMB:
961 case EXCP_PAGE_REF:
962 case EXCP_DMAR:
963 case EXCP_DMPI:
964 info.si_signo = TARGET_SIGSEGV;
965 info.si_errno = 0;
966 info.si_code = TARGET_SEGV_ACCERR;
967 info._sifields._sigfault._addr = env->cr[CR_IOR];
968 queue_signal(env, info.si_signo, QEMU_SI_FAULT, &info);
969 break;
970 case EXCP_UNALIGN:
971 info.si_signo = TARGET_SIGBUS;
972 info.si_errno = 0;
973 info.si_code = 0;
974 info._sifields._sigfault._addr = env->cr[CR_IOR];
975 queue_signal(env, info.si_signo, QEMU_SI_FAULT, &info);
976 break;
977 case EXCP_ILL:
978 case EXCP_PRIV_OPR:
979 case EXCP_PRIV_REG:
980 info.si_signo = TARGET_SIGILL;
981 info.si_errno = 0;
982 info.si_code = TARGET_ILL_ILLOPN;
983 info._sifields._sigfault._addr = env->iaoq_f;
984 queue_signal(env, info.si_signo, QEMU_SI_FAULT, &info);
985 break;
986 case EXCP_OVERFLOW:
987 case EXCP_COND:
988 case EXCP_ASSIST:
989 info.si_signo = TARGET_SIGFPE;
990 info.si_errno = 0;
991 info.si_code = 0;
992 info._sifields._sigfault._addr = env->iaoq_f;
993 queue_signal(env, info.si_signo, QEMU_SI_FAULT, &info);
994 break;
995 case EXCP_DEBUG:
996 trapnr = gdb_handlesig(cs, TARGET_SIGTRAP);
997 if (trapnr) {
998 info.si_signo = trapnr;
999 info.si_errno = 0;
1000 info.si_code = TARGET_TRAP_BRKPT;
1001 queue_signal(env, trapnr, QEMU_SI_FAULT, &info);
1002 }
1003 break;
1004 case EXCP_INTERRUPT:
1005 /* just indicate that signals should be handled asap */
1006 break;
1007 default:
1008 g_assert_not_reached();
1009 }
1010 process_pending_signals(env);
1011 }
1012 }
1013
1014 #endif /* TARGET_HPPA */
1015
1016 #ifdef TARGET_XTENSA
1017
1018 static void xtensa_rfw(CPUXtensaState *env)
1019 {
1020 xtensa_restore_owb(env);
1021 env->pc = env->sregs[EPC1];
1022 }
1023
1024 static void xtensa_rfwu(CPUXtensaState *env)
1025 {
1026 env->sregs[WINDOW_START] |= (1 << env->sregs[WINDOW_BASE]);
1027 xtensa_rfw(env);
1028 }
1029
1030 static void xtensa_rfwo(CPUXtensaState *env)
1031 {
1032 env->sregs[WINDOW_START] &= ~(1 << env->sregs[WINDOW_BASE]);
1033 xtensa_rfw(env);
1034 }
1035
1036 static void xtensa_overflow4(CPUXtensaState *env)
1037 {
1038 put_user_ual(env->regs[0], env->regs[5] - 16);
1039 put_user_ual(env->regs[1], env->regs[5] - 12);
1040 put_user_ual(env->regs[2], env->regs[5] - 8);
1041 put_user_ual(env->regs[3], env->regs[5] - 4);
1042 xtensa_rfwo(env);
1043 }
1044
1045 static void xtensa_underflow4(CPUXtensaState *env)
1046 {
1047 get_user_ual(env->regs[0], env->regs[5] - 16);
1048 get_user_ual(env->regs[1], env->regs[5] - 12);
1049 get_user_ual(env->regs[2], env->regs[5] - 8);
1050 get_user_ual(env->regs[3], env->regs[5] - 4);
1051 xtensa_rfwu(env);
1052 }
1053
1054 static void xtensa_overflow8(CPUXtensaState *env)
1055 {
1056 put_user_ual(env->regs[0], env->regs[9] - 16);
1057 get_user_ual(env->regs[0], env->regs[1] - 12);
1058 put_user_ual(env->regs[1], env->regs[9] - 12);
1059 put_user_ual(env->regs[2], env->regs[9] - 8);
1060 put_user_ual(env->regs[3], env->regs[9] - 4);
1061 put_user_ual(env->regs[4], env->regs[0] - 32);
1062 put_user_ual(env->regs[5], env->regs[0] - 28);
1063 put_user_ual(env->regs[6], env->regs[0] - 24);
1064 put_user_ual(env->regs[7], env->regs[0] - 20);
1065 xtensa_rfwo(env);
1066 }
1067
1068 static void xtensa_underflow8(CPUXtensaState *env)
1069 {
1070 get_user_ual(env->regs[0], env->regs[9] - 16);
1071 get_user_ual(env->regs[1], env->regs[9] - 12);
1072 get_user_ual(env->regs[2], env->regs[9] - 8);
1073 get_user_ual(env->regs[7], env->regs[1] - 12);
1074 get_user_ual(env->regs[3], env->regs[9] - 4);
1075 get_user_ual(env->regs[4], env->regs[7] - 32);
1076 get_user_ual(env->regs[5], env->regs[7] - 28);
1077 get_user_ual(env->regs[6], env->regs[7] - 24);
1078 get_user_ual(env->regs[7], env->regs[7] - 20);
1079 xtensa_rfwu(env);
1080 }
1081
1082 static void xtensa_overflow12(CPUXtensaState *env)
1083 {
1084 put_user_ual(env->regs[0], env->regs[13] - 16);
1085 get_user_ual(env->regs[0], env->regs[1] - 12);
1086 put_user_ual(env->regs[1], env->regs[13] - 12);
1087 put_user_ual(env->regs[2], env->regs[13] - 8);
1088 put_user_ual(env->regs[3], env->regs[13] - 4);
1089 put_user_ual(env->regs[4], env->regs[0] - 48);
1090 put_user_ual(env->regs[5], env->regs[0] - 44);
1091 put_user_ual(env->regs[6], env->regs[0] - 40);
1092 put_user_ual(env->regs[7], env->regs[0] - 36);
1093 put_user_ual(env->regs[8], env->regs[0] - 32);
1094 put_user_ual(env->regs[9], env->regs[0] - 28);
1095 put_user_ual(env->regs[10], env->regs[0] - 24);
1096 put_user_ual(env->regs[11], env->regs[0] - 20);
1097 xtensa_rfwo(env);
1098 }
1099
1100 static void xtensa_underflow12(CPUXtensaState *env)
1101 {
1102 get_user_ual(env->regs[0], env->regs[13] - 16);
1103 get_user_ual(env->regs[1], env->regs[13] - 12);
1104 get_user_ual(env->regs[2], env->regs[13] - 8);
1105 get_user_ual(env->regs[11], env->regs[1] - 12);
1106 get_user_ual(env->regs[3], env->regs[13] - 4);
1107 get_user_ual(env->regs[4], env->regs[11] - 48);
1108 get_user_ual(env->regs[5], env->regs[11] - 44);
1109 get_user_ual(env->regs[6], env->regs[11] - 40);
1110 get_user_ual(env->regs[7], env->regs[11] - 36);
1111 get_user_ual(env->regs[8], env->regs[11] - 32);
1112 get_user_ual(env->regs[9], env->regs[11] - 28);
1113 get_user_ual(env->regs[10], env->regs[11] - 24);
1114 get_user_ual(env->regs[11], env->regs[11] - 20);
1115 xtensa_rfwu(env);
1116 }
1117
1118 void cpu_loop(CPUXtensaState *env)
1119 {
1120 CPUState *cs = CPU(xtensa_env_get_cpu(env));
1121 target_siginfo_t info;
1122 abi_ulong ret;
1123 int trapnr;
1124
1125 while (1) {
1126 cpu_exec_start(cs);
1127 trapnr = cpu_exec(cs);
1128 cpu_exec_end(cs);
1129 process_queued_cpu_work(cs);
1130
1131 env->sregs[PS] &= ~PS_EXCM;
1132 switch (trapnr) {
1133 case EXCP_INTERRUPT:
1134 break;
1135
1136 case EXC_WINDOW_OVERFLOW4:
1137 xtensa_overflow4(env);
1138 break;
1139 case EXC_WINDOW_UNDERFLOW4:
1140 xtensa_underflow4(env);
1141 break;
1142 case EXC_WINDOW_OVERFLOW8:
1143 xtensa_overflow8(env);
1144 break;
1145 case EXC_WINDOW_UNDERFLOW8:
1146 xtensa_underflow8(env);
1147 break;
1148 case EXC_WINDOW_OVERFLOW12:
1149 xtensa_overflow12(env);
1150 break;
1151 case EXC_WINDOW_UNDERFLOW12:
1152 xtensa_underflow12(env);
1153 break;
1154
1155 case EXC_USER:
1156 switch (env->sregs[EXCCAUSE]) {
1157 case ILLEGAL_INSTRUCTION_CAUSE:
1158 case PRIVILEGED_CAUSE:
1159 info.si_signo = TARGET_SIGILL;
1160 info.si_errno = 0;
1161 info.si_code =
1162 env->sregs[EXCCAUSE] == ILLEGAL_INSTRUCTION_CAUSE ?
1163 TARGET_ILL_ILLOPC : TARGET_ILL_PRVOPC;
1164 info._sifields._sigfault._addr = env->sregs[EPC1];
1165 queue_signal(env, info.si_signo, QEMU_SI_FAULT, &info);
1166 break;
1167
1168 case SYSCALL_CAUSE:
1169 env->pc += 3;
1170 ret = do_syscall(env, env->regs[2],
1171 env->regs[6], env->regs[3],
1172 env->regs[4], env->regs[5],
1173 env->regs[8], env->regs[9], 0, 0);
1174 switch (ret) {
1175 default:
1176 env->regs[2] = ret;
1177 break;
1178
1179 case -TARGET_ERESTARTSYS:
1180 env->pc -= 3;
1181 break;
1182
1183 case -TARGET_QEMU_ESIGRETURN:
1184 break;
1185 }
1186 break;
1187
1188 case ALLOCA_CAUSE:
1189 env->sregs[PS] = deposit32(env->sregs[PS],
1190 PS_OWB_SHIFT,
1191 PS_OWB_LEN,
1192 env->sregs[WINDOW_BASE]);
1193
1194 switch (env->regs[0] & 0xc0000000) {
1195 case 0x00000000:
1196 case 0x40000000:
1197 xtensa_rotate_window(env, -1);
1198 xtensa_underflow4(env);
1199 break;
1200
1201 case 0x80000000:
1202 xtensa_rotate_window(env, -2);
1203 xtensa_underflow8(env);
1204 break;
1205
1206 case 0xc0000000:
1207 xtensa_rotate_window(env, -3);
1208 xtensa_underflow12(env);
1209 break;
1210 }
1211 break;
1212
1213 case INTEGER_DIVIDE_BY_ZERO_CAUSE:
1214 info.si_signo = TARGET_SIGFPE;
1215 info.si_errno = 0;
1216 info.si_code = TARGET_FPE_INTDIV;
1217 info._sifields._sigfault._addr = env->sregs[EPC1];
1218 queue_signal(env, info.si_signo, QEMU_SI_FAULT, &info);
1219 break;
1220
1221 case LOAD_PROHIBITED_CAUSE:
1222 case STORE_PROHIBITED_CAUSE:
1223 info.si_signo = TARGET_SIGSEGV;
1224 info.si_errno = 0;
1225 info.si_code = TARGET_SEGV_ACCERR;
1226 info._sifields._sigfault._addr = env->sregs[EXCVADDR];
1227 queue_signal(env, info.si_signo, QEMU_SI_FAULT, &info);
1228 break;
1229
1230 default:
1231 fprintf(stderr, "exccause = %d\n", env->sregs[EXCCAUSE]);
1232 g_assert_not_reached();
1233 }
1234 break;
1235 case EXCP_DEBUG:
1236 trapnr = gdb_handlesig(cs, TARGET_SIGTRAP);
1237 if (trapnr) {
1238 info.si_signo = trapnr;
1239 info.si_errno = 0;
1240 info.si_code = TARGET_TRAP_BRKPT;
1241 queue_signal(env, trapnr, QEMU_SI_FAULT, &info);
1242 }
1243 break;
1244 case EXC_DEBUG:
1245 default:
1246 fprintf(stderr, "trapnr = %d\n", trapnr);
1247 g_assert_not_reached();
1248 }
1249 process_pending_signals(env);
1250 }
1251 }
1252
1253 #endif /* TARGET_XTENSA */
1254
1255 __thread CPUState *thread_cpu;
1256
1257 bool qemu_cpu_is_self(CPUState *cpu)
1258 {
1259 return thread_cpu == cpu;
1260 }
1261
1262 void qemu_cpu_kick(CPUState *cpu)
1263 {
1264 cpu_exit(cpu);
1265 }
1266
1267 void task_settid(TaskState *ts)
1268 {
1269 if (ts->ts_tid == 0) {
1270 ts->ts_tid = (pid_t)syscall(SYS_gettid);
1271 }
1272 }
1273
1274 void stop_all_tasks(void)
1275 {
1276 /*
1277 * We trust that when using NPTL, start_exclusive()
1278 * handles thread stopping correctly.
1279 */
1280 start_exclusive();
1281 }
1282
1283 /* Assumes contents are already zeroed. */
1284 void init_task_state(TaskState *ts)
1285 {
1286 ts->used = 1;
1287 }
1288
1289 CPUArchState *cpu_copy(CPUArchState *env)
1290 {
1291 CPUState *cpu = ENV_GET_CPU(env);
1292 CPUState *new_cpu = cpu_create(cpu_type);
1293 CPUArchState *new_env = new_cpu->env_ptr;
1294 CPUBreakpoint *bp;
1295 CPUWatchpoint *wp;
1296
1297 /* Reset non arch specific state */
1298 cpu_reset(new_cpu);
1299
1300 memcpy(new_env, env, sizeof(CPUArchState));
1301
1302 /* Clone all break/watchpoints.
1303 Note: Once we support ptrace with hw-debug register access, make sure
1304 BP_CPU break/watchpoints are handled correctly on clone. */
1305 QTAILQ_INIT(&new_cpu->breakpoints);
1306 QTAILQ_INIT(&new_cpu->watchpoints);
1307 QTAILQ_FOREACH(bp, &cpu->breakpoints, entry) {
1308 cpu_breakpoint_insert(new_cpu, bp->pc, bp->flags, NULL);
1309 }
1310 QTAILQ_FOREACH(wp, &cpu->watchpoints, entry) {
1311 cpu_watchpoint_insert(new_cpu, wp->vaddr, wp->len, wp->flags, NULL);
1312 }
1313
1314 return new_env;
1315 }
1316
1317 static void handle_arg_help(const char *arg)
1318 {
1319 usage(EXIT_SUCCESS);
1320 }
1321
1322 static void handle_arg_log(const char *arg)
1323 {
1324 int mask;
1325
1326 mask = qemu_str_to_log_mask(arg);
1327 if (!mask) {
1328 qemu_print_log_usage(stdout);
1329 exit(EXIT_FAILURE);
1330 }
1331 qemu_log_needs_buffers();
1332 qemu_set_log(mask);
1333 }
1334
1335 static void handle_arg_dfilter(const char *arg)
1336 {
1337 qemu_set_dfilter_ranges(arg, NULL);
1338 }
1339
1340 static void handle_arg_log_filename(const char *arg)
1341 {
1342 qemu_set_log_filename(arg, &error_fatal);
1343 }
1344
1345 static void handle_arg_set_env(const char *arg)
1346 {
1347 char *r, *p, *token;
1348 r = p = strdup(arg);
1349 while ((token = strsep(&p, ",")) != NULL) {
1350 if (envlist_setenv(envlist, token) != 0) {
1351 usage(EXIT_FAILURE);
1352 }
1353 }
1354 free(r);
1355 }
1356
1357 static void handle_arg_unset_env(const char *arg)
1358 {
1359 char *r, *p, *token;
1360 r = p = strdup(arg);
1361 while ((token = strsep(&p, ",")) != NULL) {
1362 if (envlist_unsetenv(envlist, token) != 0) {
1363 usage(EXIT_FAILURE);
1364 }
1365 }
1366 free(r);
1367 }
1368
1369 static void handle_arg_argv0(const char *arg)
1370 {
1371 argv0 = strdup(arg);
1372 }
1373
1374 static void handle_arg_stack_size(const char *arg)
1375 {
1376 char *p;
1377 guest_stack_size = strtoul(arg, &p, 0);
1378 if (guest_stack_size == 0) {
1379 usage(EXIT_FAILURE);
1380 }
1381
1382 if (*p == 'M') {
1383 guest_stack_size *= 1024 * 1024;
1384 } else if (*p == 'k' || *p == 'K') {
1385 guest_stack_size *= 1024;
1386 }
1387 }
1388
1389 static void handle_arg_ld_prefix(const char *arg)
1390 {
1391 interp_prefix = strdup(arg);
1392 }
1393
1394 static void handle_arg_pagesize(const char *arg)
1395 {
1396 qemu_host_page_size = atoi(arg);
1397 if (qemu_host_page_size == 0 ||
1398 (qemu_host_page_size & (qemu_host_page_size - 1)) != 0) {
1399 fprintf(stderr, "page size must be a power of two\n");
1400 exit(EXIT_FAILURE);
1401 }
1402 }
1403
1404 static void handle_arg_randseed(const char *arg)
1405 {
1406 unsigned long long seed;
1407
1408 if (parse_uint_full(arg, &seed, 0) != 0 || seed > UINT_MAX) {
1409 fprintf(stderr, "Invalid seed number: %s\n", arg);
1410 exit(EXIT_FAILURE);
1411 }
1412 srand(seed);
1413 }
1414
1415 static void handle_arg_gdb(const char *arg)
1416 {
1417 gdbstub_port = atoi(arg);
1418 }
1419
1420 static void handle_arg_uname(const char *arg)
1421 {
1422 qemu_uname_release = strdup(arg);
1423 }
1424
1425 static void handle_arg_cpu(const char *arg)
1426 {
1427 cpu_model = strdup(arg);
1428 if (cpu_model == NULL || is_help_option(cpu_model)) {
1429 /* XXX: implement xxx_cpu_list for targets that still miss it */
1430 #if defined(cpu_list)
1431 cpu_list(stdout, &fprintf);
1432 #endif
1433 exit(EXIT_FAILURE);
1434 }
1435 }
1436
1437 static void handle_arg_guest_base(const char *arg)
1438 {
1439 guest_base = strtol(arg, NULL, 0);
1440 have_guest_base = 1;
1441 }
1442
1443 static void handle_arg_reserved_va(const char *arg)
1444 {
1445 char *p;
1446 int shift = 0;
1447 reserved_va = strtoul(arg, &p, 0);
1448 switch (*p) {
1449 case 'k':
1450 case 'K':
1451 shift = 10;
1452 break;
1453 case 'M':
1454 shift = 20;
1455 break;
1456 case 'G':
1457 shift = 30;
1458 break;
1459 }
1460 if (shift) {
1461 unsigned long unshifted = reserved_va;
1462 p++;
1463 reserved_va <<= shift;
1464 if (reserved_va >> shift != unshifted
1465 || (MAX_RESERVED_VA && reserved_va > MAX_RESERVED_VA)) {
1466 fprintf(stderr, "Reserved virtual address too big\n");
1467 exit(EXIT_FAILURE);
1468 }
1469 }
1470 if (*p) {
1471 fprintf(stderr, "Unrecognised -R size suffix '%s'\n", p);
1472 exit(EXIT_FAILURE);
1473 }
1474 }
1475
1476 static void handle_arg_singlestep(const char *arg)
1477 {
1478 singlestep = 1;
1479 }
1480
1481 static void handle_arg_strace(const char *arg)
1482 {
1483 do_strace = 1;
1484 }
1485
1486 static void handle_arg_version(const char *arg)
1487 {
1488 printf("qemu-" TARGET_NAME " version " QEMU_FULL_VERSION
1489 "\n" QEMU_COPYRIGHT "\n");
1490 exit(EXIT_SUCCESS);
1491 }
1492
1493 static char *trace_file;
1494 static void handle_arg_trace(const char *arg)
1495 {
1496 g_free(trace_file);
1497 trace_file = trace_opt_parse(arg);
1498 }
1499
1500 struct qemu_argument {
1501 const char *argv;
1502 const char *env;
1503 bool has_arg;
1504 void (*handle_opt)(const char *arg);
1505 const char *example;
1506 const char *help;
1507 };
1508
1509 static const struct qemu_argument arg_table[] = {
1510 {"h", "", false, handle_arg_help,
1511 "", "print this help"},
1512 {"help", "", false, handle_arg_help,
1513 "", ""},
1514 {"g", "QEMU_GDB", true, handle_arg_gdb,
1515 "port", "wait gdb connection to 'port'"},
1516 {"L", "QEMU_LD_PREFIX", true, handle_arg_ld_prefix,
1517 "path", "set the elf interpreter prefix to 'path'"},
1518 {"s", "QEMU_STACK_SIZE", true, handle_arg_stack_size,
1519 "size", "set the stack size to 'size' bytes"},
1520 {"cpu", "QEMU_CPU", true, handle_arg_cpu,
1521 "model", "select CPU (-cpu help for list)"},
1522 {"E", "QEMU_SET_ENV", true, handle_arg_set_env,
1523 "var=value", "sets targets environment variable (see below)"},
1524 {"U", "QEMU_UNSET_ENV", true, handle_arg_unset_env,
1525 "var", "unsets targets environment variable (see below)"},
1526 {"0", "QEMU_ARGV0", true, handle_arg_argv0,
1527 "argv0", "forces target process argv[0] to be 'argv0'"},
1528 {"r", "QEMU_UNAME", true, handle_arg_uname,
1529 "uname", "set qemu uname release string to 'uname'"},
1530 {"B", "QEMU_GUEST_BASE", true, handle_arg_guest_base,
1531 "address", "set guest_base address to 'address'"},
1532 {"R", "QEMU_RESERVED_VA", true, handle_arg_reserved_va,
1533 "size", "reserve 'size' bytes for guest virtual address space"},
1534 {"d", "QEMU_LOG", true, handle_arg_log,
1535 "item[,...]", "enable logging of specified items "
1536 "(use '-d help' for a list of items)"},
1537 {"dfilter", "QEMU_DFILTER", true, handle_arg_dfilter,
1538 "range[,...]","filter logging based on address range"},
1539 {"D", "QEMU_LOG_FILENAME", true, handle_arg_log_filename,
1540 "logfile", "write logs to 'logfile' (default stderr)"},
1541 {"p", "QEMU_PAGESIZE", true, handle_arg_pagesize,
1542 "pagesize", "set the host page size to 'pagesize'"},
1543 {"singlestep", "QEMU_SINGLESTEP", false, handle_arg_singlestep,
1544 "", "run in singlestep mode"},
1545 {"strace", "QEMU_STRACE", false, handle_arg_strace,
1546 "", "log system calls"},
1547 {"seed", "QEMU_RAND_SEED", true, handle_arg_randseed,
1548 "", "Seed for pseudo-random number generator"},
1549 {"trace", "QEMU_TRACE", true, handle_arg_trace,
1550 "", "[[enable=]<pattern>][,events=<file>][,file=<file>]"},
1551 {"version", "QEMU_VERSION", false, handle_arg_version,
1552 "", "display version information and exit"},
1553 {NULL, NULL, false, NULL, NULL, NULL}
1554 };
1555
1556 static void usage(int exitcode)
1557 {
1558 const struct qemu_argument *arginfo;
1559 int maxarglen;
1560 int maxenvlen;
1561
1562 printf("usage: qemu-" TARGET_NAME " [options] program [arguments...]\n"
1563 "Linux CPU emulator (compiled for " TARGET_NAME " emulation)\n"
1564 "\n"
1565 "Options and associated environment variables:\n"
1566 "\n");
1567
1568 /* Calculate column widths. We must always have at least enough space
1569 * for the column header.
1570 */
1571 maxarglen = strlen("Argument");
1572 maxenvlen = strlen("Env-variable");
1573
1574 for (arginfo = arg_table; arginfo->handle_opt != NULL; arginfo++) {
1575 int arglen = strlen(arginfo->argv);
1576 if (arginfo->has_arg) {
1577 arglen += strlen(arginfo->example) + 1;
1578 }
1579 if (strlen(arginfo->env) > maxenvlen) {
1580 maxenvlen = strlen(arginfo->env);
1581 }
1582 if (arglen > maxarglen) {
1583 maxarglen = arglen;
1584 }
1585 }
1586
1587 printf("%-*s %-*s Description\n", maxarglen+1, "Argument",
1588 maxenvlen, "Env-variable");
1589
1590 for (arginfo = arg_table; arginfo->handle_opt != NULL; arginfo++) {
1591 if (arginfo->has_arg) {
1592 printf("-%s %-*s %-*s %s\n", arginfo->argv,
1593 (int)(maxarglen - strlen(arginfo->argv) - 1),
1594 arginfo->example, maxenvlen, arginfo->env, arginfo->help);
1595 } else {
1596 printf("-%-*s %-*s %s\n", maxarglen, arginfo->argv,
1597 maxenvlen, arginfo->env,
1598 arginfo->help);
1599 }
1600 }
1601
1602 printf("\n"
1603 "Defaults:\n"
1604 "QEMU_LD_PREFIX = %s\n"
1605 "QEMU_STACK_SIZE = %ld byte\n",
1606 interp_prefix,
1607 guest_stack_size);
1608
1609 printf("\n"
1610 "You can use -E and -U options or the QEMU_SET_ENV and\n"
1611 "QEMU_UNSET_ENV environment variables to set and unset\n"
1612 "environment variables for the target process.\n"
1613 "It is possible to provide several variables by separating them\n"
1614 "by commas in getsubopt(3) style. Additionally it is possible to\n"
1615 "provide the -E and -U options multiple times.\n"
1616 "The following lines are equivalent:\n"
1617 " -E var1=val2 -E var2=val2 -U LD_PRELOAD -U LD_DEBUG\n"
1618 " -E var1=val2,var2=val2 -U LD_PRELOAD,LD_DEBUG\n"
1619 " QEMU_SET_ENV=var1=val2,var2=val2 QEMU_UNSET_ENV=LD_PRELOAD,LD_DEBUG\n"
1620 "Note that if you provide several changes to a single variable\n"
1621 "the last change will stay in effect.\n"
1622 "\n"
1623 QEMU_HELP_BOTTOM "\n");
1624
1625 exit(exitcode);
1626 }
1627
1628 static int parse_args(int argc, char **argv)
1629 {
1630 const char *r;
1631 int optind;
1632 const struct qemu_argument *arginfo;
1633
1634 for (arginfo = arg_table; arginfo->handle_opt != NULL; arginfo++) {
1635 if (arginfo->env == NULL) {
1636 continue;
1637 }
1638
1639 r = getenv(arginfo->env);
1640 if (r != NULL) {
1641 arginfo->handle_opt(r);
1642 }
1643 }
1644
1645 optind = 1;
1646 for (;;) {
1647 if (optind >= argc) {
1648 break;
1649 }
1650 r = argv[optind];
1651 if (r[0] != '-') {
1652 break;
1653 }
1654 optind++;
1655 r++;
1656 if (!strcmp(r, "-")) {
1657 break;
1658 }
1659 /* Treat --foo the same as -foo. */
1660 if (r[0] == '-') {
1661 r++;
1662 }
1663
1664 for (arginfo = arg_table; arginfo->handle_opt != NULL; arginfo++) {
1665 if (!strcmp(r, arginfo->argv)) {
1666 if (arginfo->has_arg) {
1667 if (optind >= argc) {
1668 (void) fprintf(stderr,
1669 "qemu: missing argument for option '%s'\n", r);
1670 exit(EXIT_FAILURE);
1671 }
1672 arginfo->handle_opt(argv[optind]);
1673 optind++;
1674 } else {
1675 arginfo->handle_opt(NULL);
1676 }
1677 break;
1678 }
1679 }
1680
1681 /* no option matched the current argv */
1682 if (arginfo->handle_opt == NULL) {
1683 (void) fprintf(stderr, "qemu: unknown option '%s'\n", r);
1684 exit(EXIT_FAILURE);
1685 }
1686 }
1687
1688 if (optind >= argc) {
1689 (void) fprintf(stderr, "qemu: no user program specified\n");
1690 exit(EXIT_FAILURE);
1691 }
1692
1693 filename = argv[optind];
1694 exec_path = argv[optind];
1695
1696 return optind;
1697 }
1698
1699 int main(int argc, char **argv, char **envp)
1700 {
1701 struct target_pt_regs regs1, *regs = &regs1;
1702 struct image_info info1, *info = &info1;
1703 struct linux_binprm bprm;
1704 TaskState *ts;
1705 CPUArchState *env;
1706 CPUState *cpu;
1707 int optind;
1708 char **target_environ, **wrk;
1709 char **target_argv;
1710 int target_argc;
1711 int i;
1712 int ret;
1713 int execfd;
1714
1715 module_call_init(MODULE_INIT_TRACE);
1716 qemu_init_cpu_list();
1717 module_call_init(MODULE_INIT_QOM);
1718
1719 envlist = envlist_create();
1720
1721 /* add current environment into the list */
1722 for (wrk = environ; *wrk != NULL; wrk++) {
1723 (void) envlist_setenv(envlist, *wrk);
1724 }
1725
1726 /* Read the stack limit from the kernel. If it's "unlimited",
1727 then we can do little else besides use the default. */
1728 {
1729 struct rlimit lim;
1730 if (getrlimit(RLIMIT_STACK, &lim) == 0
1731 && lim.rlim_cur != RLIM_INFINITY
1732 && lim.rlim_cur == (target_long)lim.rlim_cur) {
1733 guest_stack_size = lim.rlim_cur;
1734 }
1735 }
1736
1737 cpu_model = NULL;
1738
1739 srand(time(NULL));
1740
1741 qemu_add_opts(&qemu_trace_opts);
1742
1743 optind = parse_args(argc, argv);
1744
1745 if (!trace_init_backends()) {
1746 exit(1);
1747 }
1748 trace_init_file(trace_file);
1749
1750 /* Zero out regs */
1751 memset(regs, 0, sizeof(struct target_pt_regs));
1752
1753 /* Zero out image_info */
1754 memset(info, 0, sizeof(struct image_info));
1755
1756 memset(&bprm, 0, sizeof (bprm));
1757
1758 /* Scan interp_prefix dir for replacement files. */
1759 init_paths(interp_prefix);
1760
1761 init_qemu_uname_release();
1762
1763 execfd = qemu_getauxval(AT_EXECFD);
1764 if (execfd == 0) {
1765 execfd = open(filename, O_RDONLY);
1766 if (execfd < 0) {
1767 printf("Error while loading %s: %s\n", filename, strerror(errno));
1768 _exit(EXIT_FAILURE);
1769 }
1770 }
1771
1772 if (cpu_model == NULL) {
1773 cpu_model = cpu_get_model(get_elf_eflags(execfd));
1774 }
1775 cpu_type = parse_cpu_model(cpu_model);
1776
1777 tcg_exec_init(0);
1778 /* NOTE: we need to init the CPU at this stage to get
1779 qemu_host_page_size */
1780
1781 cpu = cpu_create(cpu_type);
1782 env = cpu->env_ptr;
1783 cpu_reset(cpu);
1784
1785 thread_cpu = cpu;
1786
1787 if (getenv("QEMU_STRACE")) {
1788 do_strace = 1;
1789 }
1790
1791 if (getenv("QEMU_RAND_SEED")) {
1792 handle_arg_randseed(getenv("QEMU_RAND_SEED"));
1793 }
1794
1795 target_environ = envlist_to_environ(envlist, NULL);
1796 envlist_free(envlist);
1797
1798 /*
1799 * Now that page sizes are configured in cpu_init() we can do
1800 * proper page alignment for guest_base.
1801 */
1802 guest_base = HOST_PAGE_ALIGN(guest_base);
1803
1804 if (reserved_va || have_guest_base) {
1805 guest_base = init_guest_space(guest_base, reserved_va, 0,
1806 have_guest_base);
1807 if (guest_base == (unsigned long)-1) {
1808 fprintf(stderr, "Unable to reserve 0x%lx bytes of virtual address "
1809 "space for use as guest address space (check your virtual "
1810 "memory ulimit setting or reserve less using -R option)\n",
1811 reserved_va);
1812 exit(EXIT_FAILURE);
1813 }
1814
1815 if (reserved_va) {
1816 mmap_next_start = reserved_va;
1817 }
1818 }
1819
1820 /*
1821 * Read in mmap_min_addr kernel parameter. This value is used
1822 * When loading the ELF image to determine whether guest_base
1823 * is needed. It is also used in mmap_find_vma.
1824 */
1825 {
1826 FILE *fp;
1827
1828 if ((fp = fopen("/proc/sys/vm/mmap_min_addr", "r")) != NULL) {
1829 unsigned long tmp;
1830 if (fscanf(fp, "%lu", &tmp) == 1) {
1831 mmap_min_addr = tmp;
1832 qemu_log_mask(CPU_LOG_PAGE, "host mmap_min_addr=0x%lx\n", mmap_min_addr);
1833 }
1834 fclose(fp);
1835 }
1836 }
1837
1838 /*
1839 * Prepare copy of argv vector for target.
1840 */
1841 target_argc = argc - optind;
1842 target_argv = calloc(target_argc + 1, sizeof (char *));
1843 if (target_argv == NULL) {
1844 (void) fprintf(stderr, "Unable to allocate memory for target_argv\n");
1845 exit(EXIT_FAILURE);
1846 }
1847
1848 /*
1849 * If argv0 is specified (using '-0' switch) we replace
1850 * argv[0] pointer with the given one.
1851 */
1852 i = 0;
1853 if (argv0 != NULL) {
1854 target_argv[i++] = strdup(argv0);
1855 }
1856 for (; i < target_argc; i++) {
1857 target_argv[i] = strdup(argv[optind + i]);
1858 }
1859 target_argv[target_argc] = NULL;
1860
1861 ts = g_new0(TaskState, 1);
1862 init_task_state(ts);
1863 /* build Task State */
1864 ts->info = info;
1865 ts->bprm = &bprm;
1866 cpu->opaque = ts;
1867 task_settid(ts);
1868
1869 ret = loader_exec(execfd, filename, target_argv, target_environ, regs,
1870 info, &bprm);
1871 if (ret != 0) {
1872 printf("Error while loading %s: %s\n", filename, strerror(-ret));
1873 _exit(EXIT_FAILURE);
1874 }
1875
1876 for (wrk = target_environ; *wrk; wrk++) {
1877 g_free(*wrk);
1878 }
1879
1880 g_free(target_environ);
1881
1882 if (qemu_loglevel_mask(CPU_LOG_PAGE)) {
1883 qemu_log("guest_base 0x%lx\n", guest_base);
1884 log_page_dump();
1885
1886 qemu_log("start_brk 0x" TARGET_ABI_FMT_lx "\n", info->start_brk);
1887 qemu_log("end_code 0x" TARGET_ABI_FMT_lx "\n", info->end_code);
1888 qemu_log("start_code 0x" TARGET_ABI_FMT_lx "\n", info->start_code);
1889 qemu_log("start_data 0x" TARGET_ABI_FMT_lx "\n", info->start_data);
1890 qemu_log("end_data 0x" TARGET_ABI_FMT_lx "\n", info->end_data);
1891 qemu_log("start_stack 0x" TARGET_ABI_FMT_lx "\n", info->start_stack);
1892 qemu_log("brk 0x" TARGET_ABI_FMT_lx "\n", info->brk);
1893 qemu_log("entry 0x" TARGET_ABI_FMT_lx "\n", info->entry);
1894 qemu_log("argv_start 0x" TARGET_ABI_FMT_lx "\n", info->arg_start);
1895 qemu_log("env_start 0x" TARGET_ABI_FMT_lx "\n",
1896 info->arg_end + (abi_ulong)sizeof(abi_ulong));
1897 qemu_log("auxv_start 0x" TARGET_ABI_FMT_lx "\n", info->saved_auxv);
1898 }
1899
1900 target_set_brk(info->brk);
1901 syscall_init();
1902 signal_init();
1903
1904 /* Now that we've loaded the binary, GUEST_BASE is fixed. Delay
1905 generating the prologue until now so that the prologue can take
1906 the real value of GUEST_BASE into account. */
1907 tcg_prologue_init(tcg_ctx);
1908 tcg_region_init();
1909
1910 target_cpu_copy_regs(env, regs);
1911
1912 #if defined(TARGET_RISCV)
1913 {
1914 env->pc = regs->sepc;
1915 env->gpr[xSP] = regs->sp;
1916 }
1917 #elif defined(TARGET_ALPHA)
1918 {
1919 int i;
1920
1921 for(i = 0; i < 28; i++) {
1922 env->ir[i] = ((abi_ulong *)regs)[i];
1923 }
1924 env->ir[IR_SP] = regs->usp;
1925 env->pc = regs->pc;
1926 }
1927 #elif defined(TARGET_S390X)
1928 {
1929 int i;
1930 for (i = 0; i < 16; i++) {
1931 env->regs[i] = regs->gprs[i];
1932 }
1933 env->psw.mask = regs->psw.mask;
1934 env->psw.addr = regs->psw.addr;
1935 }
1936 #elif defined(TARGET_TILEGX)
1937 {
1938 int i;
1939 for (i = 0; i < TILEGX_R_COUNT; i++) {
1940 env->regs[i] = regs->regs[i];
1941 }
1942 for (i = 0; i < TILEGX_SPR_COUNT; i++) {
1943 env->spregs[i] = 0;
1944 }
1945 env->pc = regs->pc;
1946 }
1947 #elif defined(TARGET_HPPA)
1948 {
1949 int i;
1950 for (i = 1; i < 32; i++) {
1951 env->gr[i] = regs->gr[i];
1952 }
1953 env->iaoq_f = regs->iaoq[0];
1954 env->iaoq_b = regs->iaoq[1];
1955 }
1956 #elif defined(TARGET_XTENSA)
1957 {
1958 int i;
1959 for (i = 0; i < 16; ++i) {
1960 env->regs[i] = regs->areg[i];
1961 }
1962 env->sregs[WINDOW_START] = regs->windowstart;
1963 env->pc = regs->pc;
1964 }
1965 #endif
1966
1967 if (gdbstub_port) {
1968 if (gdbserver_start(gdbstub_port) < 0) {
1969 fprintf(stderr, "qemu: could not open gdbserver on port %d\n",
1970 gdbstub_port);
1971 exit(EXIT_FAILURE);
1972 }
1973 gdb_handlesig(cpu, 0);
1974 }
1975 cpu_loop(env);
1976 /* never exits */
1977 return 0;
1978 }