Laurent Vivier | cd71c08 | 2018-04-11 20:56:33 +0200 | [diff] [blame] | 1 | /* |
| 2 | * qemu user cpu loop |
| 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 | |
| 20 | #include "qemu/osdep.h" |
Kito Cheng | 5836c3e | 2019-03-16 01:20:46 +0000 | [diff] [blame] | 21 | #include "qemu/error-report.h" |
Laurent Vivier | cd71c08 | 2018-04-11 20:56:33 +0200 | [diff] [blame] | 22 | #include "qemu.h" |
Peter Maydell | 3b249d2 | 2021-09-08 16:44:03 +0100 | [diff] [blame] | 23 | #include "user-internals.h" |
Laurent Vivier | cd71c08 | 2018-04-11 20:56:33 +0200 | [diff] [blame] | 24 | #include "cpu_loop-common.h" |
Peter Maydell | 2113aed | 2021-09-08 16:43:59 +0100 | [diff] [blame] | 25 | #include "signal-common.h" |
Kito Cheng | 5836c3e | 2019-03-16 01:20:46 +0000 | [diff] [blame] | 26 | #include "elf.h" |
Philippe Mathieu-Daudé | 6b5fe13 | 2021-03-05 13:54:49 +0000 | [diff] [blame] | 27 | #include "semihosting/common-semi.h" |
Laurent Vivier | cd71c08 | 2018-04-11 20:56:33 +0200 | [diff] [blame] | 28 | |
Laurent Vivier | 5a0b6d2 | 2018-04-11 20:56:49 +0200 | [diff] [blame] | 29 | void cpu_loop(CPURISCVState *env) |
| 30 | { |
Richard Henderson | 3109cd9 | 2019-03-22 19:11:37 -0700 | [diff] [blame] | 31 | CPUState *cs = env_cpu(env); |
Richard Henderson | 8521cc2 | 2022-01-07 13:32:39 -0800 | [diff] [blame] | 32 | int trapnr; |
Laurent Vivier | 5a0b6d2 | 2018-04-11 20:56:49 +0200 | [diff] [blame] | 33 | target_ulong ret; |
| 34 | |
| 35 | for (;;) { |
| 36 | cpu_exec_start(cs); |
| 37 | trapnr = cpu_exec(cs); |
| 38 | cpu_exec_end(cs); |
| 39 | process_queued_cpu_work(cs); |
| 40 | |
Laurent Vivier | 5a0b6d2 | 2018-04-11 20:56:49 +0200 | [diff] [blame] | 41 | switch (trapnr) { |
| 42 | case EXCP_INTERRUPT: |
| 43 | /* just indicate that signals should be handled asap */ |
| 44 | break; |
| 45 | case EXCP_ATOMIC: |
| 46 | cpu_exec_step_atomic(cs); |
| 47 | break; |
| 48 | case RISCV_EXCP_U_ECALL: |
| 49 | env->pc += 4; |
| 50 | if (env->gpr[xA7] == TARGET_NR_arch_specific_syscall + 15) { |
| 51 | /* riscv_flush_icache_syscall is a no-op in QEMU as |
| 52 | self-modifying code is automatically detected */ |
| 53 | ret = 0; |
| 54 | } else { |
| 55 | ret = do_syscall(env, |
Kito Cheng | 5836c3e | 2019-03-16 01:20:46 +0000 | [diff] [blame] | 56 | env->gpr[(env->elf_flags & EF_RISCV_RVE) |
| 57 | ? xT0 : xA7], |
Laurent Vivier | 5a0b6d2 | 2018-04-11 20:56:49 +0200 | [diff] [blame] | 58 | env->gpr[xA0], |
| 59 | env->gpr[xA1], |
| 60 | env->gpr[xA2], |
| 61 | env->gpr[xA3], |
| 62 | env->gpr[xA4], |
| 63 | env->gpr[xA5], |
| 64 | 0, 0); |
| 65 | } |
Richard Henderson | af254a2 | 2021-11-22 19:47:33 +0100 | [diff] [blame] | 66 | if (ret == -QEMU_ERESTARTSYS) { |
Laurent Vivier | 5a0b6d2 | 2018-04-11 20:56:49 +0200 | [diff] [blame] | 67 | env->pc -= 4; |
Richard Henderson | 57a0c93 | 2021-11-17 05:14:52 -0800 | [diff] [blame] | 68 | } else if (ret != -QEMU_ESIGRETURN) { |
Laurent Vivier | 5a0b6d2 | 2018-04-11 20:56:49 +0200 | [diff] [blame] | 69 | env->gpr[xA0] = ret; |
| 70 | } |
| 71 | if (cs->singlestep_enabled) { |
| 72 | goto gdbstep; |
| 73 | } |
| 74 | break; |
| 75 | case RISCV_EXCP_ILLEGAL_INST: |
Richard Henderson | 8521cc2 | 2022-01-07 13:32:39 -0800 | [diff] [blame] | 76 | force_sig_fault(TARGET_SIGILL, TARGET_ILL_ILLOPC, env->pc); |
Laurent Vivier | 5a0b6d2 | 2018-04-11 20:56:49 +0200 | [diff] [blame] | 77 | break; |
| 78 | case RISCV_EXCP_BREAKPOINT: |
Richard Henderson | 8521cc2 | 2022-01-07 13:32:39 -0800 | [diff] [blame] | 79 | case EXCP_DEBUG: |
| 80 | gdbstep: |
| 81 | force_sig_fault(TARGET_SIGTRAP, TARGET_TRAP_BRKPT, env->pc); |
Laurent Vivier | 5a0b6d2 | 2018-04-11 20:56:49 +0200 | [diff] [blame] | 82 | break; |
Kito Cheng | 6b80cb2 | 2021-01-08 22:42:53 +0000 | [diff] [blame] | 83 | case RISCV_EXCP_SEMIHOST: |
Richard Henderson | ed3a06b | 2022-04-28 01:10:55 -0700 | [diff] [blame] | 84 | do_common_semihosting(cs); |
Kito Cheng | 6b80cb2 | 2021-01-08 22:42:53 +0000 | [diff] [blame] | 85 | env->pc += 4; |
| 86 | break; |
Laurent Vivier | 5a0b6d2 | 2018-04-11 20:56:49 +0200 | [diff] [blame] | 87 | default: |
| 88 | EXCP_DUMP(env, "\nqemu: unhandled CPU exception %#x - aborting\n", |
| 89 | trapnr); |
| 90 | exit(EXIT_FAILURE); |
| 91 | } |
| 92 | |
Laurent Vivier | 5a0b6d2 | 2018-04-11 20:56:49 +0200 | [diff] [blame] | 93 | process_pending_signals(env); |
| 94 | } |
| 95 | } |
| 96 | |
Laurent Vivier | cd71c08 | 2018-04-11 20:56:33 +0200 | [diff] [blame] | 97 | void target_cpu_copy_regs(CPUArchState *env, struct target_pt_regs *regs) |
| 98 | { |
Richard Henderson | 29a0af6 | 2019-03-22 16:07:18 -0700 | [diff] [blame] | 99 | CPUState *cpu = env_cpu(env); |
Ilya Leoshkevich | e4e5cb4 | 2024-03-05 12:09:39 +0000 | [diff] [blame] | 100 | TaskState *ts = get_task_state(cpu); |
Kito Cheng | 5836c3e | 2019-03-16 01:20:46 +0000 | [diff] [blame] | 101 | struct image_info *info = ts->info; |
| 102 | |
Laurent Vivier | 5a0b6d2 | 2018-04-11 20:56:49 +0200 | [diff] [blame] | 103 | env->pc = regs->sepc; |
| 104 | env->gpr[xSP] = regs->sp; |
Kito Cheng | 5836c3e | 2019-03-16 01:20:46 +0000 | [diff] [blame] | 105 | env->elf_flags = info->elf_flags; |
| 106 | |
Richard Henderson | e91a722 | 2021-10-19 20:16:57 -0700 | [diff] [blame] | 107 | if ((env->misa_ext & RVE) && !(env->elf_flags & EF_RISCV_RVE)) { |
Kito Cheng | 5836c3e | 2019-03-16 01:20:46 +0000 | [diff] [blame] | 108 | error_report("Incompatible ELF: RVE cpu requires RVE ABI binary"); |
| 109 | exit(EXIT_FAILURE); |
| 110 | } |
Alex Bennée | 7967d1d | 2021-03-23 16:52:54 +0000 | [diff] [blame] | 111 | |
| 112 | ts->stack_base = info->start_stack; |
| 113 | ts->heap_base = info->brk; |
| 114 | /* This will be filled in on the first SYS_HEAPINFO call. */ |
| 115 | ts->heap_limit = 0; |
Laurent Vivier | cd71c08 | 2018-04-11 20:56:33 +0200 | [diff] [blame] | 116 | } |