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" 24cd71c089SLaurent Vivier #include "cpu_loop-common.h" 25*2113aed6SPeter Maydell #include "signal-common.h" 265836c3ecSKito Cheng #include "elf.h" 276b5fe137SPhilippe Mathieu-Daudé #include "semihosting/common-semi.h" 28cd71c089SLaurent Vivier 295a0b6d22SLaurent Vivier void cpu_loop(CPURISCVState *env) 305a0b6d22SLaurent Vivier { 313109cd98SRichard Henderson CPUState *cs = env_cpu(env); 325a0b6d22SLaurent Vivier int trapnr, signum, sigcode; 335a0b6d22SLaurent Vivier target_ulong sigaddr; 345a0b6d22SLaurent Vivier target_ulong ret; 355a0b6d22SLaurent Vivier 365a0b6d22SLaurent Vivier for (;;) { 375a0b6d22SLaurent Vivier cpu_exec_start(cs); 385a0b6d22SLaurent Vivier trapnr = cpu_exec(cs); 395a0b6d22SLaurent Vivier cpu_exec_end(cs); 405a0b6d22SLaurent Vivier process_queued_cpu_work(cs); 415a0b6d22SLaurent Vivier 425a0b6d22SLaurent Vivier signum = 0; 435a0b6d22SLaurent Vivier sigcode = 0; 445a0b6d22SLaurent Vivier sigaddr = 0; 455a0b6d22SLaurent Vivier 465a0b6d22SLaurent Vivier switch (trapnr) { 475a0b6d22SLaurent Vivier case EXCP_INTERRUPT: 485a0b6d22SLaurent Vivier /* just indicate that signals should be handled asap */ 495a0b6d22SLaurent Vivier break; 505a0b6d22SLaurent Vivier case EXCP_ATOMIC: 515a0b6d22SLaurent Vivier cpu_exec_step_atomic(cs); 525a0b6d22SLaurent Vivier break; 535a0b6d22SLaurent Vivier case RISCV_EXCP_U_ECALL: 545a0b6d22SLaurent Vivier env->pc += 4; 555a0b6d22SLaurent Vivier if (env->gpr[xA7] == TARGET_NR_arch_specific_syscall + 15) { 565a0b6d22SLaurent Vivier /* riscv_flush_icache_syscall is a no-op in QEMU as 575a0b6d22SLaurent Vivier self-modifying code is automatically detected */ 585a0b6d22SLaurent Vivier ret = 0; 595a0b6d22SLaurent Vivier } else { 605a0b6d22SLaurent Vivier ret = do_syscall(env, 615836c3ecSKito Cheng env->gpr[(env->elf_flags & EF_RISCV_RVE) 625836c3ecSKito Cheng ? xT0 : xA7], 635a0b6d22SLaurent Vivier env->gpr[xA0], 645a0b6d22SLaurent Vivier env->gpr[xA1], 655a0b6d22SLaurent Vivier env->gpr[xA2], 665a0b6d22SLaurent Vivier env->gpr[xA3], 675a0b6d22SLaurent Vivier env->gpr[xA4], 685a0b6d22SLaurent Vivier env->gpr[xA5], 695a0b6d22SLaurent Vivier 0, 0); 705a0b6d22SLaurent Vivier } 715a0b6d22SLaurent Vivier if (ret == -TARGET_ERESTARTSYS) { 725a0b6d22SLaurent Vivier env->pc -= 4; 735a0b6d22SLaurent Vivier } else if (ret != -TARGET_QEMU_ESIGRETURN) { 745a0b6d22SLaurent Vivier env->gpr[xA0] = ret; 755a0b6d22SLaurent Vivier } 765a0b6d22SLaurent Vivier if (cs->singlestep_enabled) { 775a0b6d22SLaurent Vivier goto gdbstep; 785a0b6d22SLaurent Vivier } 795a0b6d22SLaurent Vivier break; 805a0b6d22SLaurent Vivier case RISCV_EXCP_ILLEGAL_INST: 815a0b6d22SLaurent Vivier signum = TARGET_SIGILL; 825a0b6d22SLaurent Vivier sigcode = TARGET_ILL_ILLOPC; 835a0b6d22SLaurent Vivier break; 845a0b6d22SLaurent Vivier case RISCV_EXCP_BREAKPOINT: 855a0b6d22SLaurent Vivier signum = TARGET_SIGTRAP; 865a0b6d22SLaurent Vivier sigcode = TARGET_TRAP_BRKPT; 875a0b6d22SLaurent Vivier sigaddr = env->pc; 885a0b6d22SLaurent Vivier break; 895a0b6d22SLaurent Vivier case RISCV_EXCP_INST_PAGE_FAULT: 905a0b6d22SLaurent Vivier case RISCV_EXCP_LOAD_PAGE_FAULT: 915a0b6d22SLaurent Vivier case RISCV_EXCP_STORE_PAGE_FAULT: 925a0b6d22SLaurent Vivier signum = TARGET_SIGSEGV; 935a0b6d22SLaurent Vivier sigcode = TARGET_SEGV_MAPERR; 942921343bSGiuseppe Musacchio sigaddr = env->badaddr; 955a0b6d22SLaurent Vivier break; 966b80cb25SKito Cheng case RISCV_EXCP_SEMIHOST: 976b80cb25SKito Cheng env->gpr[xA0] = do_common_semihosting(cs); 986b80cb25SKito Cheng env->pc += 4; 996b80cb25SKito Cheng break; 1005a0b6d22SLaurent Vivier case EXCP_DEBUG: 1015a0b6d22SLaurent Vivier gdbstep: 102b10089a1SPeter Maydell signum = TARGET_SIGTRAP; 1035a0b6d22SLaurent Vivier sigcode = TARGET_TRAP_BRKPT; 1045a0b6d22SLaurent Vivier break; 1055a0b6d22SLaurent Vivier default: 1065a0b6d22SLaurent Vivier EXCP_DUMP(env, "\nqemu: unhandled CPU exception %#x - aborting\n", 1075a0b6d22SLaurent Vivier trapnr); 1085a0b6d22SLaurent Vivier exit(EXIT_FAILURE); 1095a0b6d22SLaurent Vivier } 1105a0b6d22SLaurent Vivier 1115a0b6d22SLaurent Vivier if (signum) { 1125a0b6d22SLaurent Vivier target_siginfo_t info = { 1135a0b6d22SLaurent Vivier .si_signo = signum, 1145a0b6d22SLaurent Vivier .si_errno = 0, 1155a0b6d22SLaurent Vivier .si_code = sigcode, 1165a0b6d22SLaurent Vivier ._sifields._sigfault._addr = sigaddr 1175a0b6d22SLaurent Vivier }; 1182921343bSGiuseppe Musacchio queue_signal(env, info.si_signo, QEMU_SI_FAULT, &info); 1195a0b6d22SLaurent Vivier } 1205a0b6d22SLaurent Vivier 1215a0b6d22SLaurent Vivier process_pending_signals(env); 1225a0b6d22SLaurent Vivier } 1235a0b6d22SLaurent Vivier } 1245a0b6d22SLaurent Vivier 125cd71c089SLaurent Vivier void target_cpu_copy_regs(CPUArchState *env, struct target_pt_regs *regs) 126cd71c089SLaurent Vivier { 12729a0af61SRichard Henderson CPUState *cpu = env_cpu(env); 1285836c3ecSKito Cheng TaskState *ts = cpu->opaque; 1295836c3ecSKito Cheng struct image_info *info = ts->info; 1305836c3ecSKito Cheng 1315a0b6d22SLaurent Vivier env->pc = regs->sepc; 1325a0b6d22SLaurent Vivier env->gpr[xSP] = regs->sp; 1335836c3ecSKito Cheng env->elf_flags = info->elf_flags; 1345836c3ecSKito Cheng 1355836c3ecSKito Cheng if ((env->misa & RVE) && !(env->elf_flags & EF_RISCV_RVE)) { 1365836c3ecSKito Cheng error_report("Incompatible ELF: RVE cpu requires RVE ABI binary"); 1375836c3ecSKito Cheng exit(EXIT_FAILURE); 1385836c3ecSKito Cheng } 1397967d1daSAlex Bennée 1407967d1daSAlex Bennée ts->stack_base = info->start_stack; 1417967d1daSAlex Bennée ts->heap_base = info->brk; 1427967d1daSAlex Bennée /* This will be filled in on the first SYS_HEAPINFO call. */ 1437967d1daSAlex Bennée ts->heap_limit = 0; 144cd71c089SLaurent Vivier } 145