]> git.proxmox.com Git - mirror_qemu.git/blob - linux-user/main.c
linux-user: move s390x cpu loop to s390x directory
[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_TILEGX
153
154 static void gen_sigill_reg(CPUTLGState *env)
155 {
156 target_siginfo_t info;
157
158 info.si_signo = TARGET_SIGILL;
159 info.si_errno = 0;
160 info.si_code = TARGET_ILL_PRVREG;
161 info._sifields._sigfault._addr = env->pc;
162 queue_signal(env, info.si_signo, QEMU_SI_FAULT, &info);
163 }
164
165 static void do_signal(CPUTLGState *env, int signo, int sigcode)
166 {
167 target_siginfo_t info;
168
169 info.si_signo = signo;
170 info.si_errno = 0;
171 info._sifields._sigfault._addr = env->pc;
172
173 if (signo == TARGET_SIGSEGV) {
174 /* The passed in sigcode is a dummy; check for a page mapping
175 and pass either MAPERR or ACCERR. */
176 target_ulong addr = env->excaddr;
177 info._sifields._sigfault._addr = addr;
178 if (page_check_range(addr, 1, PAGE_VALID) < 0) {
179 sigcode = TARGET_SEGV_MAPERR;
180 } else {
181 sigcode = TARGET_SEGV_ACCERR;
182 }
183 }
184 info.si_code = sigcode;
185
186 queue_signal(env, info.si_signo, QEMU_SI_FAULT, &info);
187 }
188
189 static void gen_sigsegv_maperr(CPUTLGState *env, target_ulong addr)
190 {
191 env->excaddr = addr;
192 do_signal(env, TARGET_SIGSEGV, 0);
193 }
194
195 static void set_regval(CPUTLGState *env, uint8_t reg, uint64_t val)
196 {
197 if (unlikely(reg >= TILEGX_R_COUNT)) {
198 switch (reg) {
199 case TILEGX_R_SN:
200 case TILEGX_R_ZERO:
201 return;
202 case TILEGX_R_IDN0:
203 case TILEGX_R_IDN1:
204 case TILEGX_R_UDN0:
205 case TILEGX_R_UDN1:
206 case TILEGX_R_UDN2:
207 case TILEGX_R_UDN3:
208 gen_sigill_reg(env);
209 return;
210 default:
211 g_assert_not_reached();
212 }
213 }
214 env->regs[reg] = val;
215 }
216
217 /*
218 * Compare the 8-byte contents of the CmpValue SPR with the 8-byte value in
219 * memory at the address held in the first source register. If the values are
220 * not equal, then no memory operation is performed. If the values are equal,
221 * the 8-byte quantity from the second source register is written into memory
222 * at the address held in the first source register. In either case, the result
223 * of the instruction is the value read from memory. The compare and write to
224 * memory are atomic and thus can be used for synchronization purposes. This
225 * instruction only operates for addresses aligned to a 8-byte boundary.
226 * Unaligned memory access causes an Unaligned Data Reference interrupt.
227 *
228 * Functional Description (64-bit)
229 * uint64_t memVal = memoryReadDoubleWord (rf[SrcA]);
230 * rf[Dest] = memVal;
231 * if (memVal == SPR[CmpValueSPR])
232 * memoryWriteDoubleWord (rf[SrcA], rf[SrcB]);
233 *
234 * Functional Description (32-bit)
235 * uint64_t memVal = signExtend32 (memoryReadWord (rf[SrcA]));
236 * rf[Dest] = memVal;
237 * if (memVal == signExtend32 (SPR[CmpValueSPR]))
238 * memoryWriteWord (rf[SrcA], rf[SrcB]);
239 *
240 *
241 * This function also processes exch and exch4 which need not process SPR.
242 */
243 static void do_exch(CPUTLGState *env, bool quad, bool cmp)
244 {
245 target_ulong addr;
246 target_long val, sprval;
247
248 start_exclusive();
249
250 addr = env->atomic_srca;
251 if (quad ? get_user_s64(val, addr) : get_user_s32(val, addr)) {
252 goto sigsegv_maperr;
253 }
254
255 if (cmp) {
256 if (quad) {
257 sprval = env->spregs[TILEGX_SPR_CMPEXCH];
258 } else {
259 sprval = sextract64(env->spregs[TILEGX_SPR_CMPEXCH], 0, 32);
260 }
261 }
262
263 if (!cmp || val == sprval) {
264 target_long valb = env->atomic_srcb;
265 if (quad ? put_user_u64(valb, addr) : put_user_u32(valb, addr)) {
266 goto sigsegv_maperr;
267 }
268 }
269
270 set_regval(env, env->atomic_dstr, val);
271 end_exclusive();
272 return;
273
274 sigsegv_maperr:
275 end_exclusive();
276 gen_sigsegv_maperr(env, addr);
277 }
278
279 static void do_fetch(CPUTLGState *env, int trapnr, bool quad)
280 {
281 int8_t write = 1;
282 target_ulong addr;
283 target_long val, valb;
284
285 start_exclusive();
286
287 addr = env->atomic_srca;
288 valb = env->atomic_srcb;
289 if (quad ? get_user_s64(val, addr) : get_user_s32(val, addr)) {
290 goto sigsegv_maperr;
291 }
292
293 switch (trapnr) {
294 case TILEGX_EXCP_OPCODE_FETCHADD:
295 case TILEGX_EXCP_OPCODE_FETCHADD4:
296 valb += val;
297 break;
298 case TILEGX_EXCP_OPCODE_FETCHADDGEZ:
299 valb += val;
300 if (valb < 0) {
301 write = 0;
302 }
303 break;
304 case TILEGX_EXCP_OPCODE_FETCHADDGEZ4:
305 valb += val;
306 if ((int32_t)valb < 0) {
307 write = 0;
308 }
309 break;
310 case TILEGX_EXCP_OPCODE_FETCHAND:
311 case TILEGX_EXCP_OPCODE_FETCHAND4:
312 valb &= val;
313 break;
314 case TILEGX_EXCP_OPCODE_FETCHOR:
315 case TILEGX_EXCP_OPCODE_FETCHOR4:
316 valb |= val;
317 break;
318 default:
319 g_assert_not_reached();
320 }
321
322 if (write) {
323 if (quad ? put_user_u64(valb, addr) : put_user_u32(valb, addr)) {
324 goto sigsegv_maperr;
325 }
326 }
327
328 set_regval(env, env->atomic_dstr, val);
329 end_exclusive();
330 return;
331
332 sigsegv_maperr:
333 end_exclusive();
334 gen_sigsegv_maperr(env, addr);
335 }
336
337 void cpu_loop(CPUTLGState *env)
338 {
339 CPUState *cs = CPU(tilegx_env_get_cpu(env));
340 int trapnr;
341
342 while (1) {
343 cpu_exec_start(cs);
344 trapnr = cpu_exec(cs);
345 cpu_exec_end(cs);
346 process_queued_cpu_work(cs);
347
348 switch (trapnr) {
349 case TILEGX_EXCP_SYSCALL:
350 {
351 abi_ulong ret = do_syscall(env, env->regs[TILEGX_R_NR],
352 env->regs[0], env->regs[1],
353 env->regs[2], env->regs[3],
354 env->regs[4], env->regs[5],
355 env->regs[6], env->regs[7]);
356 if (ret == -TARGET_ERESTARTSYS) {
357 env->pc -= 8;
358 } else if (ret != -TARGET_QEMU_ESIGRETURN) {
359 env->regs[TILEGX_R_RE] = ret;
360 env->regs[TILEGX_R_ERR] = TILEGX_IS_ERRNO(ret) ? -ret : 0;
361 }
362 break;
363 }
364 case TILEGX_EXCP_OPCODE_EXCH:
365 do_exch(env, true, false);
366 break;
367 case TILEGX_EXCP_OPCODE_EXCH4:
368 do_exch(env, false, false);
369 break;
370 case TILEGX_EXCP_OPCODE_CMPEXCH:
371 do_exch(env, true, true);
372 break;
373 case TILEGX_EXCP_OPCODE_CMPEXCH4:
374 do_exch(env, false, true);
375 break;
376 case TILEGX_EXCP_OPCODE_FETCHADD:
377 case TILEGX_EXCP_OPCODE_FETCHADDGEZ:
378 case TILEGX_EXCP_OPCODE_FETCHAND:
379 case TILEGX_EXCP_OPCODE_FETCHOR:
380 do_fetch(env, trapnr, true);
381 break;
382 case TILEGX_EXCP_OPCODE_FETCHADD4:
383 case TILEGX_EXCP_OPCODE_FETCHADDGEZ4:
384 case TILEGX_EXCP_OPCODE_FETCHAND4:
385 case TILEGX_EXCP_OPCODE_FETCHOR4:
386 do_fetch(env, trapnr, false);
387 break;
388 case TILEGX_EXCP_SIGNAL:
389 do_signal(env, env->signo, env->sigcode);
390 break;
391 case TILEGX_EXCP_REG_IDN_ACCESS:
392 case TILEGX_EXCP_REG_UDN_ACCESS:
393 gen_sigill_reg(env);
394 break;
395 case EXCP_ATOMIC:
396 cpu_exec_step_atomic(cs);
397 break;
398 default:
399 fprintf(stderr, "trapnr is %d[0x%x].\n", trapnr, trapnr);
400 g_assert_not_reached();
401 }
402 process_pending_signals(env);
403 }
404 }
405
406 #endif
407
408 #ifdef TARGET_RISCV
409
410 void cpu_loop(CPURISCVState *env)
411 {
412 CPUState *cs = CPU(riscv_env_get_cpu(env));
413 int trapnr, signum, sigcode;
414 target_ulong sigaddr;
415 target_ulong ret;
416
417 for (;;) {
418 cpu_exec_start(cs);
419 trapnr = cpu_exec(cs);
420 cpu_exec_end(cs);
421 process_queued_cpu_work(cs);
422
423 signum = 0;
424 sigcode = 0;
425 sigaddr = 0;
426
427 switch (trapnr) {
428 case EXCP_INTERRUPT:
429 /* just indicate that signals should be handled asap */
430 break;
431 case EXCP_ATOMIC:
432 cpu_exec_step_atomic(cs);
433 break;
434 case RISCV_EXCP_U_ECALL:
435 env->pc += 4;
436 if (env->gpr[xA7] == TARGET_NR_arch_specific_syscall + 15) {
437 /* riscv_flush_icache_syscall is a no-op in QEMU as
438 self-modifying code is automatically detected */
439 ret = 0;
440 } else {
441 ret = do_syscall(env,
442 env->gpr[xA7],
443 env->gpr[xA0],
444 env->gpr[xA1],
445 env->gpr[xA2],
446 env->gpr[xA3],
447 env->gpr[xA4],
448 env->gpr[xA5],
449 0, 0);
450 }
451 if (ret == -TARGET_ERESTARTSYS) {
452 env->pc -= 4;
453 } else if (ret != -TARGET_QEMU_ESIGRETURN) {
454 env->gpr[xA0] = ret;
455 }
456 if (cs->singlestep_enabled) {
457 goto gdbstep;
458 }
459 break;
460 case RISCV_EXCP_ILLEGAL_INST:
461 signum = TARGET_SIGILL;
462 sigcode = TARGET_ILL_ILLOPC;
463 break;
464 case RISCV_EXCP_BREAKPOINT:
465 signum = TARGET_SIGTRAP;
466 sigcode = TARGET_TRAP_BRKPT;
467 sigaddr = env->pc;
468 break;
469 case RISCV_EXCP_INST_PAGE_FAULT:
470 case RISCV_EXCP_LOAD_PAGE_FAULT:
471 case RISCV_EXCP_STORE_PAGE_FAULT:
472 signum = TARGET_SIGSEGV;
473 sigcode = TARGET_SEGV_MAPERR;
474 break;
475 case EXCP_DEBUG:
476 gdbstep:
477 signum = gdb_handlesig(cs, TARGET_SIGTRAP);
478 sigcode = TARGET_TRAP_BRKPT;
479 break;
480 default:
481 EXCP_DUMP(env, "\nqemu: unhandled CPU exception %#x - aborting\n",
482 trapnr);
483 exit(EXIT_FAILURE);
484 }
485
486 if (signum) {
487 target_siginfo_t info = {
488 .si_signo = signum,
489 .si_errno = 0,
490 .si_code = sigcode,
491 ._sifields._sigfault._addr = sigaddr
492 };
493 queue_signal(env, info.si_signo, QEMU_SI_KILL, &info);
494 }
495
496 process_pending_signals(env);
497 }
498 }
499
500 #endif /* TARGET_RISCV */
501
502 #ifdef TARGET_HPPA
503
504 static abi_ulong hppa_lws(CPUHPPAState *env)
505 {
506 uint32_t which = env->gr[20];
507 abi_ulong addr = env->gr[26];
508 abi_ulong old = env->gr[25];
509 abi_ulong new = env->gr[24];
510 abi_ulong size, ret;
511
512 switch (which) {
513 default:
514 return -TARGET_ENOSYS;
515
516 case 0: /* elf32 atomic 32bit cmpxchg */
517 if ((addr & 3) || !access_ok(VERIFY_WRITE, addr, 4)) {
518 return -TARGET_EFAULT;
519 }
520 old = tswap32(old);
521 new = tswap32(new);
522 ret = atomic_cmpxchg((uint32_t *)g2h(addr), old, new);
523 ret = tswap32(ret);
524 break;
525
526 case 2: /* elf32 atomic "new" cmpxchg */
527 size = env->gr[23];
528 if (size >= 4) {
529 return -TARGET_ENOSYS;
530 }
531 if (((addr | old | new) & ((1 << size) - 1))
532 || !access_ok(VERIFY_WRITE, addr, 1 << size)
533 || !access_ok(VERIFY_READ, old, 1 << size)
534 || !access_ok(VERIFY_READ, new, 1 << size)) {
535 return -TARGET_EFAULT;
536 }
537 /* Note that below we use host-endian loads so that the cmpxchg
538 can be host-endian as well. */
539 switch (size) {
540 case 0:
541 old = *(uint8_t *)g2h(old);
542 new = *(uint8_t *)g2h(new);
543 ret = atomic_cmpxchg((uint8_t *)g2h(addr), old, new);
544 ret = ret != old;
545 break;
546 case 1:
547 old = *(uint16_t *)g2h(old);
548 new = *(uint16_t *)g2h(new);
549 ret = atomic_cmpxchg((uint16_t *)g2h(addr), old, new);
550 ret = ret != old;
551 break;
552 case 2:
553 old = *(uint32_t *)g2h(old);
554 new = *(uint32_t *)g2h(new);
555 ret = atomic_cmpxchg((uint32_t *)g2h(addr), old, new);
556 ret = ret != old;
557 break;
558 case 3:
559 {
560 uint64_t o64, n64, r64;
561 o64 = *(uint64_t *)g2h(old);
562 n64 = *(uint64_t *)g2h(new);
563 #ifdef CONFIG_ATOMIC64
564 r64 = atomic_cmpxchg__nocheck((uint64_t *)g2h(addr), o64, n64);
565 ret = r64 != o64;
566 #else
567 start_exclusive();
568 r64 = *(uint64_t *)g2h(addr);
569 ret = 1;
570 if (r64 == o64) {
571 *(uint64_t *)g2h(addr) = n64;
572 ret = 0;
573 }
574 end_exclusive();
575 #endif
576 }
577 break;
578 }
579 break;
580 }
581
582 env->gr[28] = ret;
583 return 0;
584 }
585
586 void cpu_loop(CPUHPPAState *env)
587 {
588 CPUState *cs = CPU(hppa_env_get_cpu(env));
589 target_siginfo_t info;
590 abi_ulong ret;
591 int trapnr;
592
593 while (1) {
594 cpu_exec_start(cs);
595 trapnr = cpu_exec(cs);
596 cpu_exec_end(cs);
597 process_queued_cpu_work(cs);
598
599 switch (trapnr) {
600 case EXCP_SYSCALL:
601 ret = do_syscall(env, env->gr[20],
602 env->gr[26], env->gr[25],
603 env->gr[24], env->gr[23],
604 env->gr[22], env->gr[21], 0, 0);
605 switch (ret) {
606 default:
607 env->gr[28] = ret;
608 /* We arrived here by faking the gateway page. Return. */
609 env->iaoq_f = env->gr[31];
610 env->iaoq_b = env->gr[31] + 4;
611 break;
612 case -TARGET_ERESTARTSYS:
613 case -TARGET_QEMU_ESIGRETURN:
614 break;
615 }
616 break;
617 case EXCP_SYSCALL_LWS:
618 env->gr[21] = hppa_lws(env);
619 /* We arrived here by faking the gateway page. Return. */
620 env->iaoq_f = env->gr[31];
621 env->iaoq_b = env->gr[31] + 4;
622 break;
623 case EXCP_ITLB_MISS:
624 case EXCP_DTLB_MISS:
625 case EXCP_NA_ITLB_MISS:
626 case EXCP_NA_DTLB_MISS:
627 case EXCP_IMP:
628 case EXCP_DMP:
629 case EXCP_DMB:
630 case EXCP_PAGE_REF:
631 case EXCP_DMAR:
632 case EXCP_DMPI:
633 info.si_signo = TARGET_SIGSEGV;
634 info.si_errno = 0;
635 info.si_code = TARGET_SEGV_ACCERR;
636 info._sifields._sigfault._addr = env->cr[CR_IOR];
637 queue_signal(env, info.si_signo, QEMU_SI_FAULT, &info);
638 break;
639 case EXCP_UNALIGN:
640 info.si_signo = TARGET_SIGBUS;
641 info.si_errno = 0;
642 info.si_code = 0;
643 info._sifields._sigfault._addr = env->cr[CR_IOR];
644 queue_signal(env, info.si_signo, QEMU_SI_FAULT, &info);
645 break;
646 case EXCP_ILL:
647 case EXCP_PRIV_OPR:
648 case EXCP_PRIV_REG:
649 info.si_signo = TARGET_SIGILL;
650 info.si_errno = 0;
651 info.si_code = TARGET_ILL_ILLOPN;
652 info._sifields._sigfault._addr = env->iaoq_f;
653 queue_signal(env, info.si_signo, QEMU_SI_FAULT, &info);
654 break;
655 case EXCP_OVERFLOW:
656 case EXCP_COND:
657 case EXCP_ASSIST:
658 info.si_signo = TARGET_SIGFPE;
659 info.si_errno = 0;
660 info.si_code = 0;
661 info._sifields._sigfault._addr = env->iaoq_f;
662 queue_signal(env, info.si_signo, QEMU_SI_FAULT, &info);
663 break;
664 case EXCP_DEBUG:
665 trapnr = gdb_handlesig(cs, TARGET_SIGTRAP);
666 if (trapnr) {
667 info.si_signo = trapnr;
668 info.si_errno = 0;
669 info.si_code = TARGET_TRAP_BRKPT;
670 queue_signal(env, trapnr, QEMU_SI_FAULT, &info);
671 }
672 break;
673 case EXCP_INTERRUPT:
674 /* just indicate that signals should be handled asap */
675 break;
676 default:
677 g_assert_not_reached();
678 }
679 process_pending_signals(env);
680 }
681 }
682
683 #endif /* TARGET_HPPA */
684
685 #ifdef TARGET_XTENSA
686
687 static void xtensa_rfw(CPUXtensaState *env)
688 {
689 xtensa_restore_owb(env);
690 env->pc = env->sregs[EPC1];
691 }
692
693 static void xtensa_rfwu(CPUXtensaState *env)
694 {
695 env->sregs[WINDOW_START] |= (1 << env->sregs[WINDOW_BASE]);
696 xtensa_rfw(env);
697 }
698
699 static void xtensa_rfwo(CPUXtensaState *env)
700 {
701 env->sregs[WINDOW_START] &= ~(1 << env->sregs[WINDOW_BASE]);
702 xtensa_rfw(env);
703 }
704
705 static void xtensa_overflow4(CPUXtensaState *env)
706 {
707 put_user_ual(env->regs[0], env->regs[5] - 16);
708 put_user_ual(env->regs[1], env->regs[5] - 12);
709 put_user_ual(env->regs[2], env->regs[5] - 8);
710 put_user_ual(env->regs[3], env->regs[5] - 4);
711 xtensa_rfwo(env);
712 }
713
714 static void xtensa_underflow4(CPUXtensaState *env)
715 {
716 get_user_ual(env->regs[0], env->regs[5] - 16);
717 get_user_ual(env->regs[1], env->regs[5] - 12);
718 get_user_ual(env->regs[2], env->regs[5] - 8);
719 get_user_ual(env->regs[3], env->regs[5] - 4);
720 xtensa_rfwu(env);
721 }
722
723 static void xtensa_overflow8(CPUXtensaState *env)
724 {
725 put_user_ual(env->regs[0], env->regs[9] - 16);
726 get_user_ual(env->regs[0], env->regs[1] - 12);
727 put_user_ual(env->regs[1], env->regs[9] - 12);
728 put_user_ual(env->regs[2], env->regs[9] - 8);
729 put_user_ual(env->regs[3], env->regs[9] - 4);
730 put_user_ual(env->regs[4], env->regs[0] - 32);
731 put_user_ual(env->regs[5], env->regs[0] - 28);
732 put_user_ual(env->regs[6], env->regs[0] - 24);
733 put_user_ual(env->regs[7], env->regs[0] - 20);
734 xtensa_rfwo(env);
735 }
736
737 static void xtensa_underflow8(CPUXtensaState *env)
738 {
739 get_user_ual(env->regs[0], env->regs[9] - 16);
740 get_user_ual(env->regs[1], env->regs[9] - 12);
741 get_user_ual(env->regs[2], env->regs[9] - 8);
742 get_user_ual(env->regs[7], env->regs[1] - 12);
743 get_user_ual(env->regs[3], env->regs[9] - 4);
744 get_user_ual(env->regs[4], env->regs[7] - 32);
745 get_user_ual(env->regs[5], env->regs[7] - 28);
746 get_user_ual(env->regs[6], env->regs[7] - 24);
747 get_user_ual(env->regs[7], env->regs[7] - 20);
748 xtensa_rfwu(env);
749 }
750
751 static void xtensa_overflow12(CPUXtensaState *env)
752 {
753 put_user_ual(env->regs[0], env->regs[13] - 16);
754 get_user_ual(env->regs[0], env->regs[1] - 12);
755 put_user_ual(env->regs[1], env->regs[13] - 12);
756 put_user_ual(env->regs[2], env->regs[13] - 8);
757 put_user_ual(env->regs[3], env->regs[13] - 4);
758 put_user_ual(env->regs[4], env->regs[0] - 48);
759 put_user_ual(env->regs[5], env->regs[0] - 44);
760 put_user_ual(env->regs[6], env->regs[0] - 40);
761 put_user_ual(env->regs[7], env->regs[0] - 36);
762 put_user_ual(env->regs[8], env->regs[0] - 32);
763 put_user_ual(env->regs[9], env->regs[0] - 28);
764 put_user_ual(env->regs[10], env->regs[0] - 24);
765 put_user_ual(env->regs[11], env->regs[0] - 20);
766 xtensa_rfwo(env);
767 }
768
769 static void xtensa_underflow12(CPUXtensaState *env)
770 {
771 get_user_ual(env->regs[0], env->regs[13] - 16);
772 get_user_ual(env->regs[1], env->regs[13] - 12);
773 get_user_ual(env->regs[2], env->regs[13] - 8);
774 get_user_ual(env->regs[11], env->regs[1] - 12);
775 get_user_ual(env->regs[3], env->regs[13] - 4);
776 get_user_ual(env->regs[4], env->regs[11] - 48);
777 get_user_ual(env->regs[5], env->regs[11] - 44);
778 get_user_ual(env->regs[6], env->regs[11] - 40);
779 get_user_ual(env->regs[7], env->regs[11] - 36);
780 get_user_ual(env->regs[8], env->regs[11] - 32);
781 get_user_ual(env->regs[9], env->regs[11] - 28);
782 get_user_ual(env->regs[10], env->regs[11] - 24);
783 get_user_ual(env->regs[11], env->regs[11] - 20);
784 xtensa_rfwu(env);
785 }
786
787 void cpu_loop(CPUXtensaState *env)
788 {
789 CPUState *cs = CPU(xtensa_env_get_cpu(env));
790 target_siginfo_t info;
791 abi_ulong ret;
792 int trapnr;
793
794 while (1) {
795 cpu_exec_start(cs);
796 trapnr = cpu_exec(cs);
797 cpu_exec_end(cs);
798 process_queued_cpu_work(cs);
799
800 env->sregs[PS] &= ~PS_EXCM;
801 switch (trapnr) {
802 case EXCP_INTERRUPT:
803 break;
804
805 case EXC_WINDOW_OVERFLOW4:
806 xtensa_overflow4(env);
807 break;
808 case EXC_WINDOW_UNDERFLOW4:
809 xtensa_underflow4(env);
810 break;
811 case EXC_WINDOW_OVERFLOW8:
812 xtensa_overflow8(env);
813 break;
814 case EXC_WINDOW_UNDERFLOW8:
815 xtensa_underflow8(env);
816 break;
817 case EXC_WINDOW_OVERFLOW12:
818 xtensa_overflow12(env);
819 break;
820 case EXC_WINDOW_UNDERFLOW12:
821 xtensa_underflow12(env);
822 break;
823
824 case EXC_USER:
825 switch (env->sregs[EXCCAUSE]) {
826 case ILLEGAL_INSTRUCTION_CAUSE:
827 case PRIVILEGED_CAUSE:
828 info.si_signo = TARGET_SIGILL;
829 info.si_errno = 0;
830 info.si_code =
831 env->sregs[EXCCAUSE] == ILLEGAL_INSTRUCTION_CAUSE ?
832 TARGET_ILL_ILLOPC : TARGET_ILL_PRVOPC;
833 info._sifields._sigfault._addr = env->sregs[EPC1];
834 queue_signal(env, info.si_signo, QEMU_SI_FAULT, &info);
835 break;
836
837 case SYSCALL_CAUSE:
838 env->pc += 3;
839 ret = do_syscall(env, env->regs[2],
840 env->regs[6], env->regs[3],
841 env->regs[4], env->regs[5],
842 env->regs[8], env->regs[9], 0, 0);
843 switch (ret) {
844 default:
845 env->regs[2] = ret;
846 break;
847
848 case -TARGET_ERESTARTSYS:
849 env->pc -= 3;
850 break;
851
852 case -TARGET_QEMU_ESIGRETURN:
853 break;
854 }
855 break;
856
857 case ALLOCA_CAUSE:
858 env->sregs[PS] = deposit32(env->sregs[PS],
859 PS_OWB_SHIFT,
860 PS_OWB_LEN,
861 env->sregs[WINDOW_BASE]);
862
863 switch (env->regs[0] & 0xc0000000) {
864 case 0x00000000:
865 case 0x40000000:
866 xtensa_rotate_window(env, -1);
867 xtensa_underflow4(env);
868 break;
869
870 case 0x80000000:
871 xtensa_rotate_window(env, -2);
872 xtensa_underflow8(env);
873 break;
874
875 case 0xc0000000:
876 xtensa_rotate_window(env, -3);
877 xtensa_underflow12(env);
878 break;
879 }
880 break;
881
882 case INTEGER_DIVIDE_BY_ZERO_CAUSE:
883 info.si_signo = TARGET_SIGFPE;
884 info.si_errno = 0;
885 info.si_code = TARGET_FPE_INTDIV;
886 info._sifields._sigfault._addr = env->sregs[EPC1];
887 queue_signal(env, info.si_signo, QEMU_SI_FAULT, &info);
888 break;
889
890 case LOAD_PROHIBITED_CAUSE:
891 case STORE_PROHIBITED_CAUSE:
892 info.si_signo = TARGET_SIGSEGV;
893 info.si_errno = 0;
894 info.si_code = TARGET_SEGV_ACCERR;
895 info._sifields._sigfault._addr = env->sregs[EXCVADDR];
896 queue_signal(env, info.si_signo, QEMU_SI_FAULT, &info);
897 break;
898
899 default:
900 fprintf(stderr, "exccause = %d\n", env->sregs[EXCCAUSE]);
901 g_assert_not_reached();
902 }
903 break;
904 case EXCP_DEBUG:
905 trapnr = gdb_handlesig(cs, TARGET_SIGTRAP);
906 if (trapnr) {
907 info.si_signo = trapnr;
908 info.si_errno = 0;
909 info.si_code = TARGET_TRAP_BRKPT;
910 queue_signal(env, trapnr, QEMU_SI_FAULT, &info);
911 }
912 break;
913 case EXC_DEBUG:
914 default:
915 fprintf(stderr, "trapnr = %d\n", trapnr);
916 g_assert_not_reached();
917 }
918 process_pending_signals(env);
919 }
920 }
921
922 #endif /* TARGET_XTENSA */
923
924 __thread CPUState *thread_cpu;
925
926 bool qemu_cpu_is_self(CPUState *cpu)
927 {
928 return thread_cpu == cpu;
929 }
930
931 void qemu_cpu_kick(CPUState *cpu)
932 {
933 cpu_exit(cpu);
934 }
935
936 void task_settid(TaskState *ts)
937 {
938 if (ts->ts_tid == 0) {
939 ts->ts_tid = (pid_t)syscall(SYS_gettid);
940 }
941 }
942
943 void stop_all_tasks(void)
944 {
945 /*
946 * We trust that when using NPTL, start_exclusive()
947 * handles thread stopping correctly.
948 */
949 start_exclusive();
950 }
951
952 /* Assumes contents are already zeroed. */
953 void init_task_state(TaskState *ts)
954 {
955 ts->used = 1;
956 }
957
958 CPUArchState *cpu_copy(CPUArchState *env)
959 {
960 CPUState *cpu = ENV_GET_CPU(env);
961 CPUState *new_cpu = cpu_create(cpu_type);
962 CPUArchState *new_env = new_cpu->env_ptr;
963 CPUBreakpoint *bp;
964 CPUWatchpoint *wp;
965
966 /* Reset non arch specific state */
967 cpu_reset(new_cpu);
968
969 memcpy(new_env, env, sizeof(CPUArchState));
970
971 /* Clone all break/watchpoints.
972 Note: Once we support ptrace with hw-debug register access, make sure
973 BP_CPU break/watchpoints are handled correctly on clone. */
974 QTAILQ_INIT(&new_cpu->breakpoints);
975 QTAILQ_INIT(&new_cpu->watchpoints);
976 QTAILQ_FOREACH(bp, &cpu->breakpoints, entry) {
977 cpu_breakpoint_insert(new_cpu, bp->pc, bp->flags, NULL);
978 }
979 QTAILQ_FOREACH(wp, &cpu->watchpoints, entry) {
980 cpu_watchpoint_insert(new_cpu, wp->vaddr, wp->len, wp->flags, NULL);
981 }
982
983 return new_env;
984 }
985
986 static void handle_arg_help(const char *arg)
987 {
988 usage(EXIT_SUCCESS);
989 }
990
991 static void handle_arg_log(const char *arg)
992 {
993 int mask;
994
995 mask = qemu_str_to_log_mask(arg);
996 if (!mask) {
997 qemu_print_log_usage(stdout);
998 exit(EXIT_FAILURE);
999 }
1000 qemu_log_needs_buffers();
1001 qemu_set_log(mask);
1002 }
1003
1004 static void handle_arg_dfilter(const char *arg)
1005 {
1006 qemu_set_dfilter_ranges(arg, NULL);
1007 }
1008
1009 static void handle_arg_log_filename(const char *arg)
1010 {
1011 qemu_set_log_filename(arg, &error_fatal);
1012 }
1013
1014 static void handle_arg_set_env(const char *arg)
1015 {
1016 char *r, *p, *token;
1017 r = p = strdup(arg);
1018 while ((token = strsep(&p, ",")) != NULL) {
1019 if (envlist_setenv(envlist, token) != 0) {
1020 usage(EXIT_FAILURE);
1021 }
1022 }
1023 free(r);
1024 }
1025
1026 static void handle_arg_unset_env(const char *arg)
1027 {
1028 char *r, *p, *token;
1029 r = p = strdup(arg);
1030 while ((token = strsep(&p, ",")) != NULL) {
1031 if (envlist_unsetenv(envlist, token) != 0) {
1032 usage(EXIT_FAILURE);
1033 }
1034 }
1035 free(r);
1036 }
1037
1038 static void handle_arg_argv0(const char *arg)
1039 {
1040 argv0 = strdup(arg);
1041 }
1042
1043 static void handle_arg_stack_size(const char *arg)
1044 {
1045 char *p;
1046 guest_stack_size = strtoul(arg, &p, 0);
1047 if (guest_stack_size == 0) {
1048 usage(EXIT_FAILURE);
1049 }
1050
1051 if (*p == 'M') {
1052 guest_stack_size *= 1024 * 1024;
1053 } else if (*p == 'k' || *p == 'K') {
1054 guest_stack_size *= 1024;
1055 }
1056 }
1057
1058 static void handle_arg_ld_prefix(const char *arg)
1059 {
1060 interp_prefix = strdup(arg);
1061 }
1062
1063 static void handle_arg_pagesize(const char *arg)
1064 {
1065 qemu_host_page_size = atoi(arg);
1066 if (qemu_host_page_size == 0 ||
1067 (qemu_host_page_size & (qemu_host_page_size - 1)) != 0) {
1068 fprintf(stderr, "page size must be a power of two\n");
1069 exit(EXIT_FAILURE);
1070 }
1071 }
1072
1073 static void handle_arg_randseed(const char *arg)
1074 {
1075 unsigned long long seed;
1076
1077 if (parse_uint_full(arg, &seed, 0) != 0 || seed > UINT_MAX) {
1078 fprintf(stderr, "Invalid seed number: %s\n", arg);
1079 exit(EXIT_FAILURE);
1080 }
1081 srand(seed);
1082 }
1083
1084 static void handle_arg_gdb(const char *arg)
1085 {
1086 gdbstub_port = atoi(arg);
1087 }
1088
1089 static void handle_arg_uname(const char *arg)
1090 {
1091 qemu_uname_release = strdup(arg);
1092 }
1093
1094 static void handle_arg_cpu(const char *arg)
1095 {
1096 cpu_model = strdup(arg);
1097 if (cpu_model == NULL || is_help_option(cpu_model)) {
1098 /* XXX: implement xxx_cpu_list for targets that still miss it */
1099 #if defined(cpu_list)
1100 cpu_list(stdout, &fprintf);
1101 #endif
1102 exit(EXIT_FAILURE);
1103 }
1104 }
1105
1106 static void handle_arg_guest_base(const char *arg)
1107 {
1108 guest_base = strtol(arg, NULL, 0);
1109 have_guest_base = 1;
1110 }
1111
1112 static void handle_arg_reserved_va(const char *arg)
1113 {
1114 char *p;
1115 int shift = 0;
1116 reserved_va = strtoul(arg, &p, 0);
1117 switch (*p) {
1118 case 'k':
1119 case 'K':
1120 shift = 10;
1121 break;
1122 case 'M':
1123 shift = 20;
1124 break;
1125 case 'G':
1126 shift = 30;
1127 break;
1128 }
1129 if (shift) {
1130 unsigned long unshifted = reserved_va;
1131 p++;
1132 reserved_va <<= shift;
1133 if (reserved_va >> shift != unshifted
1134 || (MAX_RESERVED_VA && reserved_va > MAX_RESERVED_VA)) {
1135 fprintf(stderr, "Reserved virtual address too big\n");
1136 exit(EXIT_FAILURE);
1137 }
1138 }
1139 if (*p) {
1140 fprintf(stderr, "Unrecognised -R size suffix '%s'\n", p);
1141 exit(EXIT_FAILURE);
1142 }
1143 }
1144
1145 static void handle_arg_singlestep(const char *arg)
1146 {
1147 singlestep = 1;
1148 }
1149
1150 static void handle_arg_strace(const char *arg)
1151 {
1152 do_strace = 1;
1153 }
1154
1155 static void handle_arg_version(const char *arg)
1156 {
1157 printf("qemu-" TARGET_NAME " version " QEMU_FULL_VERSION
1158 "\n" QEMU_COPYRIGHT "\n");
1159 exit(EXIT_SUCCESS);
1160 }
1161
1162 static char *trace_file;
1163 static void handle_arg_trace(const char *arg)
1164 {
1165 g_free(trace_file);
1166 trace_file = trace_opt_parse(arg);
1167 }
1168
1169 struct qemu_argument {
1170 const char *argv;
1171 const char *env;
1172 bool has_arg;
1173 void (*handle_opt)(const char *arg);
1174 const char *example;
1175 const char *help;
1176 };
1177
1178 static const struct qemu_argument arg_table[] = {
1179 {"h", "", false, handle_arg_help,
1180 "", "print this help"},
1181 {"help", "", false, handle_arg_help,
1182 "", ""},
1183 {"g", "QEMU_GDB", true, handle_arg_gdb,
1184 "port", "wait gdb connection to 'port'"},
1185 {"L", "QEMU_LD_PREFIX", true, handle_arg_ld_prefix,
1186 "path", "set the elf interpreter prefix to 'path'"},
1187 {"s", "QEMU_STACK_SIZE", true, handle_arg_stack_size,
1188 "size", "set the stack size to 'size' bytes"},
1189 {"cpu", "QEMU_CPU", true, handle_arg_cpu,
1190 "model", "select CPU (-cpu help for list)"},
1191 {"E", "QEMU_SET_ENV", true, handle_arg_set_env,
1192 "var=value", "sets targets environment variable (see below)"},
1193 {"U", "QEMU_UNSET_ENV", true, handle_arg_unset_env,
1194 "var", "unsets targets environment variable (see below)"},
1195 {"0", "QEMU_ARGV0", true, handle_arg_argv0,
1196 "argv0", "forces target process argv[0] to be 'argv0'"},
1197 {"r", "QEMU_UNAME", true, handle_arg_uname,
1198 "uname", "set qemu uname release string to 'uname'"},
1199 {"B", "QEMU_GUEST_BASE", true, handle_arg_guest_base,
1200 "address", "set guest_base address to 'address'"},
1201 {"R", "QEMU_RESERVED_VA", true, handle_arg_reserved_va,
1202 "size", "reserve 'size' bytes for guest virtual address space"},
1203 {"d", "QEMU_LOG", true, handle_arg_log,
1204 "item[,...]", "enable logging of specified items "
1205 "(use '-d help' for a list of items)"},
1206 {"dfilter", "QEMU_DFILTER", true, handle_arg_dfilter,
1207 "range[,...]","filter logging based on address range"},
1208 {"D", "QEMU_LOG_FILENAME", true, handle_arg_log_filename,
1209 "logfile", "write logs to 'logfile' (default stderr)"},
1210 {"p", "QEMU_PAGESIZE", true, handle_arg_pagesize,
1211 "pagesize", "set the host page size to 'pagesize'"},
1212 {"singlestep", "QEMU_SINGLESTEP", false, handle_arg_singlestep,
1213 "", "run in singlestep mode"},
1214 {"strace", "QEMU_STRACE", false, handle_arg_strace,
1215 "", "log system calls"},
1216 {"seed", "QEMU_RAND_SEED", true, handle_arg_randseed,
1217 "", "Seed for pseudo-random number generator"},
1218 {"trace", "QEMU_TRACE", true, handle_arg_trace,
1219 "", "[[enable=]<pattern>][,events=<file>][,file=<file>]"},
1220 {"version", "QEMU_VERSION", false, handle_arg_version,
1221 "", "display version information and exit"},
1222 {NULL, NULL, false, NULL, NULL, NULL}
1223 };
1224
1225 static void usage(int exitcode)
1226 {
1227 const struct qemu_argument *arginfo;
1228 int maxarglen;
1229 int maxenvlen;
1230
1231 printf("usage: qemu-" TARGET_NAME " [options] program [arguments...]\n"
1232 "Linux CPU emulator (compiled for " TARGET_NAME " emulation)\n"
1233 "\n"
1234 "Options and associated environment variables:\n"
1235 "\n");
1236
1237 /* Calculate column widths. We must always have at least enough space
1238 * for the column header.
1239 */
1240 maxarglen = strlen("Argument");
1241 maxenvlen = strlen("Env-variable");
1242
1243 for (arginfo = arg_table; arginfo->handle_opt != NULL; arginfo++) {
1244 int arglen = strlen(arginfo->argv);
1245 if (arginfo->has_arg) {
1246 arglen += strlen(arginfo->example) + 1;
1247 }
1248 if (strlen(arginfo->env) > maxenvlen) {
1249 maxenvlen = strlen(arginfo->env);
1250 }
1251 if (arglen > maxarglen) {
1252 maxarglen = arglen;
1253 }
1254 }
1255
1256 printf("%-*s %-*s Description\n", maxarglen+1, "Argument",
1257 maxenvlen, "Env-variable");
1258
1259 for (arginfo = arg_table; arginfo->handle_opt != NULL; arginfo++) {
1260 if (arginfo->has_arg) {
1261 printf("-%s %-*s %-*s %s\n", arginfo->argv,
1262 (int)(maxarglen - strlen(arginfo->argv) - 1),
1263 arginfo->example, maxenvlen, arginfo->env, arginfo->help);
1264 } else {
1265 printf("-%-*s %-*s %s\n", maxarglen, arginfo->argv,
1266 maxenvlen, arginfo->env,
1267 arginfo->help);
1268 }
1269 }
1270
1271 printf("\n"
1272 "Defaults:\n"
1273 "QEMU_LD_PREFIX = %s\n"
1274 "QEMU_STACK_SIZE = %ld byte\n",
1275 interp_prefix,
1276 guest_stack_size);
1277
1278 printf("\n"
1279 "You can use -E and -U options or the QEMU_SET_ENV and\n"
1280 "QEMU_UNSET_ENV environment variables to set and unset\n"
1281 "environment variables for the target process.\n"
1282 "It is possible to provide several variables by separating them\n"
1283 "by commas in getsubopt(3) style. Additionally it is possible to\n"
1284 "provide the -E and -U options multiple times.\n"
1285 "The following lines are equivalent:\n"
1286 " -E var1=val2 -E var2=val2 -U LD_PRELOAD -U LD_DEBUG\n"
1287 " -E var1=val2,var2=val2 -U LD_PRELOAD,LD_DEBUG\n"
1288 " QEMU_SET_ENV=var1=val2,var2=val2 QEMU_UNSET_ENV=LD_PRELOAD,LD_DEBUG\n"
1289 "Note that if you provide several changes to a single variable\n"
1290 "the last change will stay in effect.\n"
1291 "\n"
1292 QEMU_HELP_BOTTOM "\n");
1293
1294 exit(exitcode);
1295 }
1296
1297 static int parse_args(int argc, char **argv)
1298 {
1299 const char *r;
1300 int optind;
1301 const struct qemu_argument *arginfo;
1302
1303 for (arginfo = arg_table; arginfo->handle_opt != NULL; arginfo++) {
1304 if (arginfo->env == NULL) {
1305 continue;
1306 }
1307
1308 r = getenv(arginfo->env);
1309 if (r != NULL) {
1310 arginfo->handle_opt(r);
1311 }
1312 }
1313
1314 optind = 1;
1315 for (;;) {
1316 if (optind >= argc) {
1317 break;
1318 }
1319 r = argv[optind];
1320 if (r[0] != '-') {
1321 break;
1322 }
1323 optind++;
1324 r++;
1325 if (!strcmp(r, "-")) {
1326 break;
1327 }
1328 /* Treat --foo the same as -foo. */
1329 if (r[0] == '-') {
1330 r++;
1331 }
1332
1333 for (arginfo = arg_table; arginfo->handle_opt != NULL; arginfo++) {
1334 if (!strcmp(r, arginfo->argv)) {
1335 if (arginfo->has_arg) {
1336 if (optind >= argc) {
1337 (void) fprintf(stderr,
1338 "qemu: missing argument for option '%s'\n", r);
1339 exit(EXIT_FAILURE);
1340 }
1341 arginfo->handle_opt(argv[optind]);
1342 optind++;
1343 } else {
1344 arginfo->handle_opt(NULL);
1345 }
1346 break;
1347 }
1348 }
1349
1350 /* no option matched the current argv */
1351 if (arginfo->handle_opt == NULL) {
1352 (void) fprintf(stderr, "qemu: unknown option '%s'\n", r);
1353 exit(EXIT_FAILURE);
1354 }
1355 }
1356
1357 if (optind >= argc) {
1358 (void) fprintf(stderr, "qemu: no user program specified\n");
1359 exit(EXIT_FAILURE);
1360 }
1361
1362 filename = argv[optind];
1363 exec_path = argv[optind];
1364
1365 return optind;
1366 }
1367
1368 int main(int argc, char **argv, char **envp)
1369 {
1370 struct target_pt_regs regs1, *regs = &regs1;
1371 struct image_info info1, *info = &info1;
1372 struct linux_binprm bprm;
1373 TaskState *ts;
1374 CPUArchState *env;
1375 CPUState *cpu;
1376 int optind;
1377 char **target_environ, **wrk;
1378 char **target_argv;
1379 int target_argc;
1380 int i;
1381 int ret;
1382 int execfd;
1383
1384 module_call_init(MODULE_INIT_TRACE);
1385 qemu_init_cpu_list();
1386 module_call_init(MODULE_INIT_QOM);
1387
1388 envlist = envlist_create();
1389
1390 /* add current environment into the list */
1391 for (wrk = environ; *wrk != NULL; wrk++) {
1392 (void) envlist_setenv(envlist, *wrk);
1393 }
1394
1395 /* Read the stack limit from the kernel. If it's "unlimited",
1396 then we can do little else besides use the default. */
1397 {
1398 struct rlimit lim;
1399 if (getrlimit(RLIMIT_STACK, &lim) == 0
1400 && lim.rlim_cur != RLIM_INFINITY
1401 && lim.rlim_cur == (target_long)lim.rlim_cur) {
1402 guest_stack_size = lim.rlim_cur;
1403 }
1404 }
1405
1406 cpu_model = NULL;
1407
1408 srand(time(NULL));
1409
1410 qemu_add_opts(&qemu_trace_opts);
1411
1412 optind = parse_args(argc, argv);
1413
1414 if (!trace_init_backends()) {
1415 exit(1);
1416 }
1417 trace_init_file(trace_file);
1418
1419 /* Zero out regs */
1420 memset(regs, 0, sizeof(struct target_pt_regs));
1421
1422 /* Zero out image_info */
1423 memset(info, 0, sizeof(struct image_info));
1424
1425 memset(&bprm, 0, sizeof (bprm));
1426
1427 /* Scan interp_prefix dir for replacement files. */
1428 init_paths(interp_prefix);
1429
1430 init_qemu_uname_release();
1431
1432 execfd = qemu_getauxval(AT_EXECFD);
1433 if (execfd == 0) {
1434 execfd = open(filename, O_RDONLY);
1435 if (execfd < 0) {
1436 printf("Error while loading %s: %s\n", filename, strerror(errno));
1437 _exit(EXIT_FAILURE);
1438 }
1439 }
1440
1441 if (cpu_model == NULL) {
1442 cpu_model = cpu_get_model(get_elf_eflags(execfd));
1443 }
1444 cpu_type = parse_cpu_model(cpu_model);
1445
1446 tcg_exec_init(0);
1447 /* NOTE: we need to init the CPU at this stage to get
1448 qemu_host_page_size */
1449
1450 cpu = cpu_create(cpu_type);
1451 env = cpu->env_ptr;
1452 cpu_reset(cpu);
1453
1454 thread_cpu = cpu;
1455
1456 if (getenv("QEMU_STRACE")) {
1457 do_strace = 1;
1458 }
1459
1460 if (getenv("QEMU_RAND_SEED")) {
1461 handle_arg_randseed(getenv("QEMU_RAND_SEED"));
1462 }
1463
1464 target_environ = envlist_to_environ(envlist, NULL);
1465 envlist_free(envlist);
1466
1467 /*
1468 * Now that page sizes are configured in cpu_init() we can do
1469 * proper page alignment for guest_base.
1470 */
1471 guest_base = HOST_PAGE_ALIGN(guest_base);
1472
1473 if (reserved_va || have_guest_base) {
1474 guest_base = init_guest_space(guest_base, reserved_va, 0,
1475 have_guest_base);
1476 if (guest_base == (unsigned long)-1) {
1477 fprintf(stderr, "Unable to reserve 0x%lx bytes of virtual address "
1478 "space for use as guest address space (check your virtual "
1479 "memory ulimit setting or reserve less using -R option)\n",
1480 reserved_va);
1481 exit(EXIT_FAILURE);
1482 }
1483
1484 if (reserved_va) {
1485 mmap_next_start = reserved_va;
1486 }
1487 }
1488
1489 /*
1490 * Read in mmap_min_addr kernel parameter. This value is used
1491 * When loading the ELF image to determine whether guest_base
1492 * is needed. It is also used in mmap_find_vma.
1493 */
1494 {
1495 FILE *fp;
1496
1497 if ((fp = fopen("/proc/sys/vm/mmap_min_addr", "r")) != NULL) {
1498 unsigned long tmp;
1499 if (fscanf(fp, "%lu", &tmp) == 1) {
1500 mmap_min_addr = tmp;
1501 qemu_log_mask(CPU_LOG_PAGE, "host mmap_min_addr=0x%lx\n", mmap_min_addr);
1502 }
1503 fclose(fp);
1504 }
1505 }
1506
1507 /*
1508 * Prepare copy of argv vector for target.
1509 */
1510 target_argc = argc - optind;
1511 target_argv = calloc(target_argc + 1, sizeof (char *));
1512 if (target_argv == NULL) {
1513 (void) fprintf(stderr, "Unable to allocate memory for target_argv\n");
1514 exit(EXIT_FAILURE);
1515 }
1516
1517 /*
1518 * If argv0 is specified (using '-0' switch) we replace
1519 * argv[0] pointer with the given one.
1520 */
1521 i = 0;
1522 if (argv0 != NULL) {
1523 target_argv[i++] = strdup(argv0);
1524 }
1525 for (; i < target_argc; i++) {
1526 target_argv[i] = strdup(argv[optind + i]);
1527 }
1528 target_argv[target_argc] = NULL;
1529
1530 ts = g_new0(TaskState, 1);
1531 init_task_state(ts);
1532 /* build Task State */
1533 ts->info = info;
1534 ts->bprm = &bprm;
1535 cpu->opaque = ts;
1536 task_settid(ts);
1537
1538 ret = loader_exec(execfd, filename, target_argv, target_environ, regs,
1539 info, &bprm);
1540 if (ret != 0) {
1541 printf("Error while loading %s: %s\n", filename, strerror(-ret));
1542 _exit(EXIT_FAILURE);
1543 }
1544
1545 for (wrk = target_environ; *wrk; wrk++) {
1546 g_free(*wrk);
1547 }
1548
1549 g_free(target_environ);
1550
1551 if (qemu_loglevel_mask(CPU_LOG_PAGE)) {
1552 qemu_log("guest_base 0x%lx\n", guest_base);
1553 log_page_dump();
1554
1555 qemu_log("start_brk 0x" TARGET_ABI_FMT_lx "\n", info->start_brk);
1556 qemu_log("end_code 0x" TARGET_ABI_FMT_lx "\n", info->end_code);
1557 qemu_log("start_code 0x" TARGET_ABI_FMT_lx "\n", info->start_code);
1558 qemu_log("start_data 0x" TARGET_ABI_FMT_lx "\n", info->start_data);
1559 qemu_log("end_data 0x" TARGET_ABI_FMT_lx "\n", info->end_data);
1560 qemu_log("start_stack 0x" TARGET_ABI_FMT_lx "\n", info->start_stack);
1561 qemu_log("brk 0x" TARGET_ABI_FMT_lx "\n", info->brk);
1562 qemu_log("entry 0x" TARGET_ABI_FMT_lx "\n", info->entry);
1563 qemu_log("argv_start 0x" TARGET_ABI_FMT_lx "\n", info->arg_start);
1564 qemu_log("env_start 0x" TARGET_ABI_FMT_lx "\n",
1565 info->arg_end + (abi_ulong)sizeof(abi_ulong));
1566 qemu_log("auxv_start 0x" TARGET_ABI_FMT_lx "\n", info->saved_auxv);
1567 }
1568
1569 target_set_brk(info->brk);
1570 syscall_init();
1571 signal_init();
1572
1573 /* Now that we've loaded the binary, GUEST_BASE is fixed. Delay
1574 generating the prologue until now so that the prologue can take
1575 the real value of GUEST_BASE into account. */
1576 tcg_prologue_init(tcg_ctx);
1577 tcg_region_init();
1578
1579 target_cpu_copy_regs(env, regs);
1580
1581 #if defined(TARGET_RISCV)
1582 {
1583 env->pc = regs->sepc;
1584 env->gpr[xSP] = regs->sp;
1585 }
1586 #elif defined(TARGET_TILEGX)
1587 {
1588 int i;
1589 for (i = 0; i < TILEGX_R_COUNT; i++) {
1590 env->regs[i] = regs->regs[i];
1591 }
1592 for (i = 0; i < TILEGX_SPR_COUNT; i++) {
1593 env->spregs[i] = 0;
1594 }
1595 env->pc = regs->pc;
1596 }
1597 #elif defined(TARGET_HPPA)
1598 {
1599 int i;
1600 for (i = 1; i < 32; i++) {
1601 env->gr[i] = regs->gr[i];
1602 }
1603 env->iaoq_f = regs->iaoq[0];
1604 env->iaoq_b = regs->iaoq[1];
1605 }
1606 #elif defined(TARGET_XTENSA)
1607 {
1608 int i;
1609 for (i = 0; i < 16; ++i) {
1610 env->regs[i] = regs->areg[i];
1611 }
1612 env->sregs[WINDOW_START] = regs->windowstart;
1613 env->pc = regs->pc;
1614 }
1615 #endif
1616
1617 if (gdbstub_port) {
1618 if (gdbserver_start(gdbstub_port) < 0) {
1619 fprintf(stderr, "qemu: could not open gdbserver on port %d\n",
1620 gdbstub_port);
1621 exit(EXIT_FAILURE);
1622 }
1623 gdb_handlesig(cpu, 0);
1624 }
1625 cpu_loop(env);
1626 /* never exits */
1627 return 0;
1628 }