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" 21cd71c089SLaurent Vivier #include "qemu.h" 22cd71c089SLaurent Vivier #include "cpu_loop-common.h" 23cd71c089SLaurent Vivier 24*5a0b6d22SLaurent Vivier void cpu_loop(CPURISCVState *env) 25*5a0b6d22SLaurent Vivier { 26*5a0b6d22SLaurent Vivier CPUState *cs = CPU(riscv_env_get_cpu(env)); 27*5a0b6d22SLaurent Vivier int trapnr, signum, sigcode; 28*5a0b6d22SLaurent Vivier target_ulong sigaddr; 29*5a0b6d22SLaurent Vivier target_ulong ret; 30*5a0b6d22SLaurent Vivier 31*5a0b6d22SLaurent Vivier for (;;) { 32*5a0b6d22SLaurent Vivier cpu_exec_start(cs); 33*5a0b6d22SLaurent Vivier trapnr = cpu_exec(cs); 34*5a0b6d22SLaurent Vivier cpu_exec_end(cs); 35*5a0b6d22SLaurent Vivier process_queued_cpu_work(cs); 36*5a0b6d22SLaurent Vivier 37*5a0b6d22SLaurent Vivier signum = 0; 38*5a0b6d22SLaurent Vivier sigcode = 0; 39*5a0b6d22SLaurent Vivier sigaddr = 0; 40*5a0b6d22SLaurent Vivier 41*5a0b6d22SLaurent Vivier switch (trapnr) { 42*5a0b6d22SLaurent Vivier case EXCP_INTERRUPT: 43*5a0b6d22SLaurent Vivier /* just indicate that signals should be handled asap */ 44*5a0b6d22SLaurent Vivier break; 45*5a0b6d22SLaurent Vivier case EXCP_ATOMIC: 46*5a0b6d22SLaurent Vivier cpu_exec_step_atomic(cs); 47*5a0b6d22SLaurent Vivier break; 48*5a0b6d22SLaurent Vivier case RISCV_EXCP_U_ECALL: 49*5a0b6d22SLaurent Vivier env->pc += 4; 50*5a0b6d22SLaurent Vivier if (env->gpr[xA7] == TARGET_NR_arch_specific_syscall + 15) { 51*5a0b6d22SLaurent Vivier /* riscv_flush_icache_syscall is a no-op in QEMU as 52*5a0b6d22SLaurent Vivier self-modifying code is automatically detected */ 53*5a0b6d22SLaurent Vivier ret = 0; 54*5a0b6d22SLaurent Vivier } else { 55*5a0b6d22SLaurent Vivier ret = do_syscall(env, 56*5a0b6d22SLaurent Vivier env->gpr[xA7], 57*5a0b6d22SLaurent Vivier env->gpr[xA0], 58*5a0b6d22SLaurent Vivier env->gpr[xA1], 59*5a0b6d22SLaurent Vivier env->gpr[xA2], 60*5a0b6d22SLaurent Vivier env->gpr[xA3], 61*5a0b6d22SLaurent Vivier env->gpr[xA4], 62*5a0b6d22SLaurent Vivier env->gpr[xA5], 63*5a0b6d22SLaurent Vivier 0, 0); 64*5a0b6d22SLaurent Vivier } 65*5a0b6d22SLaurent Vivier if (ret == -TARGET_ERESTARTSYS) { 66*5a0b6d22SLaurent Vivier env->pc -= 4; 67*5a0b6d22SLaurent Vivier } else if (ret != -TARGET_QEMU_ESIGRETURN) { 68*5a0b6d22SLaurent Vivier env->gpr[xA0] = ret; 69*5a0b6d22SLaurent Vivier } 70*5a0b6d22SLaurent Vivier if (cs->singlestep_enabled) { 71*5a0b6d22SLaurent Vivier goto gdbstep; 72*5a0b6d22SLaurent Vivier } 73*5a0b6d22SLaurent Vivier break; 74*5a0b6d22SLaurent Vivier case RISCV_EXCP_ILLEGAL_INST: 75*5a0b6d22SLaurent Vivier signum = TARGET_SIGILL; 76*5a0b6d22SLaurent Vivier sigcode = TARGET_ILL_ILLOPC; 77*5a0b6d22SLaurent Vivier break; 78*5a0b6d22SLaurent Vivier case RISCV_EXCP_BREAKPOINT: 79*5a0b6d22SLaurent Vivier signum = TARGET_SIGTRAP; 80*5a0b6d22SLaurent Vivier sigcode = TARGET_TRAP_BRKPT; 81*5a0b6d22SLaurent Vivier sigaddr = env->pc; 82*5a0b6d22SLaurent Vivier break; 83*5a0b6d22SLaurent Vivier case RISCV_EXCP_INST_PAGE_FAULT: 84*5a0b6d22SLaurent Vivier case RISCV_EXCP_LOAD_PAGE_FAULT: 85*5a0b6d22SLaurent Vivier case RISCV_EXCP_STORE_PAGE_FAULT: 86*5a0b6d22SLaurent Vivier signum = TARGET_SIGSEGV; 87*5a0b6d22SLaurent Vivier sigcode = TARGET_SEGV_MAPERR; 88*5a0b6d22SLaurent Vivier break; 89*5a0b6d22SLaurent Vivier case EXCP_DEBUG: 90*5a0b6d22SLaurent Vivier gdbstep: 91*5a0b6d22SLaurent Vivier signum = gdb_handlesig(cs, TARGET_SIGTRAP); 92*5a0b6d22SLaurent Vivier sigcode = TARGET_TRAP_BRKPT; 93*5a0b6d22SLaurent Vivier break; 94*5a0b6d22SLaurent Vivier default: 95*5a0b6d22SLaurent Vivier EXCP_DUMP(env, "\nqemu: unhandled CPU exception %#x - aborting\n", 96*5a0b6d22SLaurent Vivier trapnr); 97*5a0b6d22SLaurent Vivier exit(EXIT_FAILURE); 98*5a0b6d22SLaurent Vivier } 99*5a0b6d22SLaurent Vivier 100*5a0b6d22SLaurent Vivier if (signum) { 101*5a0b6d22SLaurent Vivier target_siginfo_t info = { 102*5a0b6d22SLaurent Vivier .si_signo = signum, 103*5a0b6d22SLaurent Vivier .si_errno = 0, 104*5a0b6d22SLaurent Vivier .si_code = sigcode, 105*5a0b6d22SLaurent Vivier ._sifields._sigfault._addr = sigaddr 106*5a0b6d22SLaurent Vivier }; 107*5a0b6d22SLaurent Vivier queue_signal(env, info.si_signo, QEMU_SI_KILL, &info); 108*5a0b6d22SLaurent Vivier } 109*5a0b6d22SLaurent Vivier 110*5a0b6d22SLaurent Vivier process_pending_signals(env); 111*5a0b6d22SLaurent Vivier } 112*5a0b6d22SLaurent Vivier } 113*5a0b6d22SLaurent Vivier 114cd71c089SLaurent Vivier void target_cpu_copy_regs(CPUArchState *env, struct target_pt_regs *regs) 115cd71c089SLaurent Vivier { 116*5a0b6d22SLaurent Vivier env->pc = regs->sepc; 117*5a0b6d22SLaurent Vivier env->gpr[xSP] = regs->sp; 118cd71c089SLaurent Vivier } 119