xref: /openbmc/qemu/linux-user/riscv/cpu_loop.c (revision b10089a14cad93ca5cdcd441a23f522d1e15f554)
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 
245a0b6d22SLaurent Vivier void cpu_loop(CPURISCVState *env)
255a0b6d22SLaurent Vivier {
265a0b6d22SLaurent Vivier     CPUState *cs = CPU(riscv_env_get_cpu(env));
275a0b6d22SLaurent Vivier     int trapnr, signum, sigcode;
285a0b6d22SLaurent Vivier     target_ulong sigaddr;
295a0b6d22SLaurent Vivier     target_ulong ret;
305a0b6d22SLaurent Vivier 
315a0b6d22SLaurent Vivier     for (;;) {
325a0b6d22SLaurent Vivier         cpu_exec_start(cs);
335a0b6d22SLaurent Vivier         trapnr = cpu_exec(cs);
345a0b6d22SLaurent Vivier         cpu_exec_end(cs);
355a0b6d22SLaurent Vivier         process_queued_cpu_work(cs);
365a0b6d22SLaurent Vivier 
375a0b6d22SLaurent Vivier         signum = 0;
385a0b6d22SLaurent Vivier         sigcode = 0;
395a0b6d22SLaurent Vivier         sigaddr = 0;
405a0b6d22SLaurent Vivier 
415a0b6d22SLaurent Vivier         switch (trapnr) {
425a0b6d22SLaurent Vivier         case EXCP_INTERRUPT:
435a0b6d22SLaurent Vivier             /* just indicate that signals should be handled asap */
445a0b6d22SLaurent Vivier             break;
455a0b6d22SLaurent Vivier         case EXCP_ATOMIC:
465a0b6d22SLaurent Vivier             cpu_exec_step_atomic(cs);
475a0b6d22SLaurent Vivier             break;
485a0b6d22SLaurent Vivier         case RISCV_EXCP_U_ECALL:
495a0b6d22SLaurent Vivier             env->pc += 4;
505a0b6d22SLaurent Vivier             if (env->gpr[xA7] == TARGET_NR_arch_specific_syscall + 15) {
515a0b6d22SLaurent Vivier                 /* riscv_flush_icache_syscall is a no-op in QEMU as
525a0b6d22SLaurent Vivier                    self-modifying code is automatically detected */
535a0b6d22SLaurent Vivier                 ret = 0;
545a0b6d22SLaurent Vivier             } else {
555a0b6d22SLaurent Vivier                 ret = do_syscall(env,
565a0b6d22SLaurent Vivier                                  env->gpr[xA7],
575a0b6d22SLaurent Vivier                                  env->gpr[xA0],
585a0b6d22SLaurent Vivier                                  env->gpr[xA1],
595a0b6d22SLaurent Vivier                                  env->gpr[xA2],
605a0b6d22SLaurent Vivier                                  env->gpr[xA3],
615a0b6d22SLaurent Vivier                                  env->gpr[xA4],
625a0b6d22SLaurent Vivier                                  env->gpr[xA5],
635a0b6d22SLaurent Vivier                                  0, 0);
645a0b6d22SLaurent Vivier             }
655a0b6d22SLaurent Vivier             if (ret == -TARGET_ERESTARTSYS) {
665a0b6d22SLaurent Vivier                 env->pc -= 4;
675a0b6d22SLaurent Vivier             } else if (ret != -TARGET_QEMU_ESIGRETURN) {
685a0b6d22SLaurent Vivier                 env->gpr[xA0] = ret;
695a0b6d22SLaurent Vivier             }
705a0b6d22SLaurent Vivier             if (cs->singlestep_enabled) {
715a0b6d22SLaurent Vivier                 goto gdbstep;
725a0b6d22SLaurent Vivier             }
735a0b6d22SLaurent Vivier             break;
745a0b6d22SLaurent Vivier         case RISCV_EXCP_ILLEGAL_INST:
755a0b6d22SLaurent Vivier             signum = TARGET_SIGILL;
765a0b6d22SLaurent Vivier             sigcode = TARGET_ILL_ILLOPC;
775a0b6d22SLaurent Vivier             break;
785a0b6d22SLaurent Vivier         case RISCV_EXCP_BREAKPOINT:
795a0b6d22SLaurent Vivier             signum = TARGET_SIGTRAP;
805a0b6d22SLaurent Vivier             sigcode = TARGET_TRAP_BRKPT;
815a0b6d22SLaurent Vivier             sigaddr = env->pc;
825a0b6d22SLaurent Vivier             break;
835a0b6d22SLaurent Vivier         case RISCV_EXCP_INST_PAGE_FAULT:
845a0b6d22SLaurent Vivier         case RISCV_EXCP_LOAD_PAGE_FAULT:
855a0b6d22SLaurent Vivier         case RISCV_EXCP_STORE_PAGE_FAULT:
865a0b6d22SLaurent Vivier             signum = TARGET_SIGSEGV;
875a0b6d22SLaurent Vivier             sigcode = TARGET_SEGV_MAPERR;
885a0b6d22SLaurent Vivier             break;
895a0b6d22SLaurent Vivier         case EXCP_DEBUG:
905a0b6d22SLaurent Vivier         gdbstep:
91*b10089a1SPeter Maydell             signum = TARGET_SIGTRAP;
925a0b6d22SLaurent Vivier             sigcode = TARGET_TRAP_BRKPT;
935a0b6d22SLaurent Vivier             break;
945a0b6d22SLaurent Vivier         default:
955a0b6d22SLaurent Vivier             EXCP_DUMP(env, "\nqemu: unhandled CPU exception %#x - aborting\n",
965a0b6d22SLaurent Vivier                      trapnr);
975a0b6d22SLaurent Vivier             exit(EXIT_FAILURE);
985a0b6d22SLaurent Vivier         }
995a0b6d22SLaurent Vivier 
1005a0b6d22SLaurent Vivier         if (signum) {
1015a0b6d22SLaurent Vivier             target_siginfo_t info = {
1025a0b6d22SLaurent Vivier                 .si_signo = signum,
1035a0b6d22SLaurent Vivier                 .si_errno = 0,
1045a0b6d22SLaurent Vivier                 .si_code = sigcode,
1055a0b6d22SLaurent Vivier                 ._sifields._sigfault._addr = sigaddr
1065a0b6d22SLaurent Vivier             };
1075a0b6d22SLaurent Vivier             queue_signal(env, info.si_signo, QEMU_SI_KILL, &info);
1085a0b6d22SLaurent Vivier         }
1095a0b6d22SLaurent Vivier 
1105a0b6d22SLaurent Vivier         process_pending_signals(env);
1115a0b6d22SLaurent Vivier     }
1125a0b6d22SLaurent Vivier }
1135a0b6d22SLaurent Vivier 
114cd71c089SLaurent Vivier void target_cpu_copy_regs(CPUArchState *env, struct target_pt_regs *regs)
115cd71c089SLaurent Vivier {
1165a0b6d22SLaurent Vivier     env->pc = regs->sepc;
1175a0b6d22SLaurent Vivier     env->gpr[xSP] = regs->sp;
118cd71c089SLaurent Vivier }
119