1cd71c089SLaurent Vivier /* 2cd71c089SLaurent Vivier * qemu user cpu loop 3cd71c089SLaurent Vivier * 4cd71c089SLaurent Vivier * Copyright (c) 2003-2008 Fabrice Bellard 5cd71c089SLaurent Vivier * 6cd71c089SLaurent Vivier * This program is free software; you can redistribute it and/or modify 7cd71c089SLaurent Vivier * it under the terms of the GNU General Public License as published by 8cd71c089SLaurent Vivier * the Free Software Foundation; either version 2 of the License, or 9cd71c089SLaurent Vivier * (at your option) any later version. 10cd71c089SLaurent Vivier * 11cd71c089SLaurent Vivier * This program is distributed in the hope that it will be useful, 12cd71c089SLaurent Vivier * but WITHOUT ANY WARRANTY; without even the implied warranty of 13cd71c089SLaurent Vivier * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14cd71c089SLaurent Vivier * GNU General Public License for more details. 15cd71c089SLaurent Vivier * 16cd71c089SLaurent Vivier * You should have received a copy of the GNU General Public License 17cd71c089SLaurent Vivier * along with this program; if not, see <http://www.gnu.org/licenses/>. 18cd71c089SLaurent Vivier */ 19cd71c089SLaurent Vivier 20cd71c089SLaurent Vivier #include "qemu/osdep.h" 21a8d25326SMarkus Armbruster #include "qemu-common.h" 225836c3ecSKito Cheng #include "qemu/error-report.h" 23cd71c089SLaurent Vivier #include "qemu.h" 243b249d26SPeter Maydell #include "user-internals.h" 25cd71c089SLaurent Vivier #include "cpu_loop-common.h" 262113aed6SPeter Maydell #include "signal-common.h" 275836c3ecSKito Cheng #include "elf.h" 286b5fe137SPhilippe Mathieu-Daudé #include "semihosting/common-semi.h" 29cd71c089SLaurent Vivier 305a0b6d22SLaurent Vivier void cpu_loop(CPURISCVState *env) 315a0b6d22SLaurent Vivier { 323109cd98SRichard Henderson CPUState *cs = env_cpu(env); 335a0b6d22SLaurent Vivier int trapnr, signum, sigcode; 345a0b6d22SLaurent Vivier target_ulong sigaddr; 355a0b6d22SLaurent Vivier target_ulong ret; 365a0b6d22SLaurent Vivier 375a0b6d22SLaurent Vivier for (;;) { 385a0b6d22SLaurent Vivier cpu_exec_start(cs); 395a0b6d22SLaurent Vivier trapnr = cpu_exec(cs); 405a0b6d22SLaurent Vivier cpu_exec_end(cs); 415a0b6d22SLaurent Vivier process_queued_cpu_work(cs); 425a0b6d22SLaurent Vivier 435a0b6d22SLaurent Vivier signum = 0; 445a0b6d22SLaurent Vivier sigcode = 0; 455a0b6d22SLaurent Vivier sigaddr = 0; 465a0b6d22SLaurent Vivier 475a0b6d22SLaurent Vivier switch (trapnr) { 485a0b6d22SLaurent Vivier case EXCP_INTERRUPT: 495a0b6d22SLaurent Vivier /* just indicate that signals should be handled asap */ 505a0b6d22SLaurent Vivier break; 515a0b6d22SLaurent Vivier case EXCP_ATOMIC: 525a0b6d22SLaurent Vivier cpu_exec_step_atomic(cs); 535a0b6d22SLaurent Vivier break; 545a0b6d22SLaurent Vivier case RISCV_EXCP_U_ECALL: 555a0b6d22SLaurent Vivier env->pc += 4; 565a0b6d22SLaurent Vivier if (env->gpr[xA7] == TARGET_NR_arch_specific_syscall + 15) { 575a0b6d22SLaurent Vivier /* riscv_flush_icache_syscall is a no-op in QEMU as 585a0b6d22SLaurent Vivier self-modifying code is automatically detected */ 595a0b6d22SLaurent Vivier ret = 0; 605a0b6d22SLaurent Vivier } else { 615a0b6d22SLaurent Vivier ret = do_syscall(env, 625836c3ecSKito Cheng env->gpr[(env->elf_flags & EF_RISCV_RVE) 635836c3ecSKito Cheng ? xT0 : xA7], 645a0b6d22SLaurent Vivier env->gpr[xA0], 655a0b6d22SLaurent Vivier env->gpr[xA1], 665a0b6d22SLaurent Vivier env->gpr[xA2], 675a0b6d22SLaurent Vivier env->gpr[xA3], 685a0b6d22SLaurent Vivier env->gpr[xA4], 695a0b6d22SLaurent Vivier env->gpr[xA5], 705a0b6d22SLaurent Vivier 0, 0); 715a0b6d22SLaurent Vivier } 72af254a27SRichard Henderson if (ret == -QEMU_ERESTARTSYS) { 735a0b6d22SLaurent Vivier env->pc -= 4; 74*57a0c938SRichard Henderson } else if (ret != -QEMU_ESIGRETURN) { 755a0b6d22SLaurent Vivier env->gpr[xA0] = ret; 765a0b6d22SLaurent Vivier } 775a0b6d22SLaurent Vivier if (cs->singlestep_enabled) { 785a0b6d22SLaurent Vivier goto gdbstep; 795a0b6d22SLaurent Vivier } 805a0b6d22SLaurent Vivier break; 815a0b6d22SLaurent Vivier case RISCV_EXCP_ILLEGAL_INST: 825a0b6d22SLaurent Vivier signum = TARGET_SIGILL; 835a0b6d22SLaurent Vivier sigcode = TARGET_ILL_ILLOPC; 845a0b6d22SLaurent Vivier break; 855a0b6d22SLaurent Vivier case RISCV_EXCP_BREAKPOINT: 865a0b6d22SLaurent Vivier signum = TARGET_SIGTRAP; 875a0b6d22SLaurent Vivier sigcode = TARGET_TRAP_BRKPT; 885a0b6d22SLaurent Vivier sigaddr = env->pc; 895a0b6d22SLaurent Vivier break; 906b80cb25SKito Cheng case RISCV_EXCP_SEMIHOST: 916b80cb25SKito Cheng env->gpr[xA0] = do_common_semihosting(cs); 926b80cb25SKito Cheng env->pc += 4; 936b80cb25SKito Cheng break; 945a0b6d22SLaurent Vivier case EXCP_DEBUG: 955a0b6d22SLaurent Vivier gdbstep: 96b10089a1SPeter Maydell signum = TARGET_SIGTRAP; 975a0b6d22SLaurent Vivier sigcode = TARGET_TRAP_BRKPT; 985a0b6d22SLaurent Vivier break; 995a0b6d22SLaurent Vivier default: 1005a0b6d22SLaurent Vivier EXCP_DUMP(env, "\nqemu: unhandled CPU exception %#x - aborting\n", 1015a0b6d22SLaurent Vivier trapnr); 1025a0b6d22SLaurent Vivier exit(EXIT_FAILURE); 1035a0b6d22SLaurent Vivier } 1045a0b6d22SLaurent Vivier 1055a0b6d22SLaurent Vivier if (signum) { 1065a0b6d22SLaurent Vivier target_siginfo_t info = { 1075a0b6d22SLaurent Vivier .si_signo = signum, 1085a0b6d22SLaurent Vivier .si_errno = 0, 1095a0b6d22SLaurent Vivier .si_code = sigcode, 1105a0b6d22SLaurent Vivier ._sifields._sigfault._addr = sigaddr 1115a0b6d22SLaurent Vivier }; 1122921343bSGiuseppe Musacchio queue_signal(env, info.si_signo, QEMU_SI_FAULT, &info); 1135a0b6d22SLaurent Vivier } 1145a0b6d22SLaurent Vivier 1155a0b6d22SLaurent Vivier process_pending_signals(env); 1165a0b6d22SLaurent Vivier } 1175a0b6d22SLaurent Vivier } 1185a0b6d22SLaurent Vivier 119cd71c089SLaurent Vivier void target_cpu_copy_regs(CPUArchState *env, struct target_pt_regs *regs) 120cd71c089SLaurent Vivier { 12129a0af61SRichard Henderson CPUState *cpu = env_cpu(env); 1225836c3ecSKito Cheng TaskState *ts = cpu->opaque; 1235836c3ecSKito Cheng struct image_info *info = ts->info; 1245836c3ecSKito Cheng 1255a0b6d22SLaurent Vivier env->pc = regs->sepc; 1265a0b6d22SLaurent Vivier env->gpr[xSP] = regs->sp; 1275836c3ecSKito Cheng env->elf_flags = info->elf_flags; 1285836c3ecSKito Cheng 129e91a7227SRichard Henderson if ((env->misa_ext & RVE) && !(env->elf_flags & EF_RISCV_RVE)) { 1305836c3ecSKito Cheng error_report("Incompatible ELF: RVE cpu requires RVE ABI binary"); 1315836c3ecSKito Cheng exit(EXIT_FAILURE); 1325836c3ecSKito Cheng } 1337967d1daSAlex Bennée 1347967d1daSAlex Bennée ts->stack_base = info->start_stack; 1357967d1daSAlex Bennée ts->heap_base = info->brk; 1367967d1daSAlex Bennée /* This will be filled in on the first SYS_HEAPINFO call. */ 1377967d1daSAlex Bennée ts->heap_limit = 0; 138cd71c089SLaurent Vivier } 139