2018-03-02 20:31:10 +08:00
|
|
|
/*
|
|
|
|
* RISC-V Emulation Helpers for QEMU.
|
|
|
|
*
|
|
|
|
* Copyright (c) 2016-2017 Sagar Karandikar, sagark@eecs.berkeley.edu
|
|
|
|
* Copyright (c) 2017-2018 SiFive, Inc.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms and conditions of the GNU General Public License,
|
|
|
|
* version 2 or later, as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
* more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along with
|
|
|
|
* this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "qemu/osdep.h"
|
|
|
|
#include "cpu.h"
|
|
|
|
#include "qemu/main-loop.h"
|
|
|
|
#include "exec/exec-all.h"
|
|
|
|
#include "exec/helper-proto.h"
|
|
|
|
|
|
|
|
/* Exceptions processing helpers */
|
2019-01-15 07:58:23 +08:00
|
|
|
void QEMU_NORETURN riscv_raise_exception(CPURISCVState *env,
|
2018-03-02 20:31:10 +08:00
|
|
|
uint32_t exception, uintptr_t pc)
|
|
|
|
{
|
2019-03-23 10:11:37 +08:00
|
|
|
CPUState *cs = env_cpu(env);
|
2018-03-02 20:31:10 +08:00
|
|
|
cs->exception_index = exception;
|
|
|
|
cpu_loop_exit_restore(cs, pc);
|
|
|
|
}
|
|
|
|
|
|
|
|
void helper_raise_exception(CPURISCVState *env, uint32_t exception)
|
|
|
|
{
|
2019-01-15 07:58:23 +08:00
|
|
|
riscv_raise_exception(env, exception, 0);
|
2018-03-02 20:31:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
target_ulong helper_csrrw(CPURISCVState *env, target_ulong src,
|
|
|
|
target_ulong csr)
|
|
|
|
{
|
2019-01-05 07:23:55 +08:00
|
|
|
target_ulong val = 0;
|
2021-04-01 23:18:07 +08:00
|
|
|
RISCVException ret = riscv_csrrw(env, csr, &val, src, -1);
|
2020-08-13 03:13:46 +08:00
|
|
|
|
2021-04-01 23:18:07 +08:00
|
|
|
if (ret != RISCV_EXCP_NONE) {
|
|
|
|
riscv_raise_exception(env, ret, GETPC());
|
2019-01-05 07:23:55 +08:00
|
|
|
}
|
|
|
|
return val;
|
2018-03-02 20:31:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
target_ulong helper_csrrs(CPURISCVState *env, target_ulong src,
|
|
|
|
target_ulong csr, target_ulong rs1_pass)
|
|
|
|
{
|
2019-01-05 07:23:55 +08:00
|
|
|
target_ulong val = 0;
|
2021-04-01 23:18:07 +08:00
|
|
|
RISCVException ret = riscv_csrrw(env, csr, &val, -1, rs1_pass ? src : 0);
|
2020-08-13 03:13:46 +08:00
|
|
|
|
2021-04-01 23:18:07 +08:00
|
|
|
if (ret != RISCV_EXCP_NONE) {
|
|
|
|
riscv_raise_exception(env, ret, GETPC());
|
2018-03-02 20:31:10 +08:00
|
|
|
}
|
2019-01-05 07:23:55 +08:00
|
|
|
return val;
|
2018-03-02 20:31:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
target_ulong helper_csrrc(CPURISCVState *env, target_ulong src,
|
|
|
|
target_ulong csr, target_ulong rs1_pass)
|
|
|
|
{
|
2019-01-05 07:23:55 +08:00
|
|
|
target_ulong val = 0;
|
2021-04-01 23:18:07 +08:00
|
|
|
RISCVException ret = riscv_csrrw(env, csr, &val, 0, rs1_pass ? src : 0);
|
2020-08-13 03:13:46 +08:00
|
|
|
|
2021-04-01 23:18:07 +08:00
|
|
|
if (ret != RISCV_EXCP_NONE) {
|
|
|
|
riscv_raise_exception(env, ret, GETPC());
|
2018-03-02 20:31:10 +08:00
|
|
|
}
|
2019-01-05 07:23:55 +08:00
|
|
|
return val;
|
2018-03-02 20:31:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef CONFIG_USER_ONLY
|
|
|
|
|
|
|
|
target_ulong helper_sret(CPURISCVState *env, target_ulong cpu_pc_deb)
|
|
|
|
{
|
2020-10-26 19:55:25 +08:00
|
|
|
uint64_t mstatus;
|
|
|
|
target_ulong prev_priv, prev_virt;
|
2020-02-01 09:02:33 +08:00
|
|
|
|
2018-03-02 20:31:10 +08:00
|
|
|
if (!(env->priv >= PRV_S)) {
|
2019-01-15 07:58:23 +08:00
|
|
|
riscv_raise_exception(env, RISCV_EXCP_ILLEGAL_INST, GETPC());
|
2018-03-02 20:31:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
target_ulong retpc = env->sepc;
|
|
|
|
if (!riscv_has_ext(env, RVC) && (retpc & 0x3)) {
|
2019-01-15 07:58:23 +08:00
|
|
|
riscv_raise_exception(env, RISCV_EXCP_INST_ADDR_MIS, GETPC());
|
2018-03-02 20:31:10 +08:00
|
|
|
}
|
|
|
|
|
2020-05-06 04:04:50 +08:00
|
|
|
if (get_field(env->mstatus, MSTATUS_TSR) && !(env->priv >= PRV_M)) {
|
2019-01-15 07:58:23 +08:00
|
|
|
riscv_raise_exception(env, RISCV_EXCP_ILLEGAL_INST, GETPC());
|
2019-01-15 07:58:08 +08:00
|
|
|
}
|
|
|
|
|
2020-08-13 03:13:49 +08:00
|
|
|
if (riscv_has_ext(env, RVH) && riscv_cpu_virt_enabled(env) &&
|
|
|
|
get_field(env->hstatus, HSTATUS_VTSR)) {
|
|
|
|
riscv_raise_exception(env, RISCV_EXCP_VIRT_INSTRUCTION_FAULT, GETPC());
|
|
|
|
}
|
|
|
|
|
2020-02-01 09:02:33 +08:00
|
|
|
mstatus = env->mstatus;
|
|
|
|
|
|
|
|
if (riscv_has_ext(env, RVH) && !riscv_cpu_virt_enabled(env)) {
|
|
|
|
/* We support Hypervisor extensions and virtulisation is disabled */
|
|
|
|
target_ulong hstatus = env->hstatus;
|
|
|
|
|
|
|
|
prev_priv = get_field(mstatus, MSTATUS_SPP);
|
|
|
|
prev_virt = get_field(hstatus, HSTATUS_SPV);
|
|
|
|
|
2020-08-13 03:13:33 +08:00
|
|
|
hstatus = set_field(hstatus, HSTATUS_SPV, 0);
|
|
|
|
mstatus = set_field(mstatus, MSTATUS_SPP, 0);
|
2020-02-01 09:02:33 +08:00
|
|
|
mstatus = set_field(mstatus, SSTATUS_SIE,
|
|
|
|
get_field(mstatus, SSTATUS_SPIE));
|
|
|
|
mstatus = set_field(mstatus, SSTATUS_SPIE, 1);
|
|
|
|
|
|
|
|
env->mstatus = mstatus;
|
|
|
|
env->hstatus = hstatus;
|
|
|
|
|
|
|
|
if (prev_virt) {
|
|
|
|
riscv_cpu_swap_hypervisor_regs(env);
|
|
|
|
}
|
|
|
|
|
|
|
|
riscv_cpu_set_virt_enabled(env, prev_virt);
|
|
|
|
} else {
|
|
|
|
prev_priv = get_field(mstatus, MSTATUS_SPP);
|
|
|
|
|
2020-05-06 04:04:50 +08:00
|
|
|
mstatus = set_field(mstatus, MSTATUS_SIE,
|
|
|
|
get_field(mstatus, MSTATUS_SPIE));
|
2020-02-01 09:02:33 +08:00
|
|
|
mstatus = set_field(mstatus, MSTATUS_SPIE, 1);
|
|
|
|
mstatus = set_field(mstatus, MSTATUS_SPP, PRV_U);
|
|
|
|
env->mstatus = mstatus;
|
|
|
|
}
|
|
|
|
|
2019-01-15 07:58:23 +08:00
|
|
|
riscv_cpu_set_mode(env, prev_priv);
|
2018-03-02 20:31:10 +08:00
|
|
|
|
|
|
|
return retpc;
|
|
|
|
}
|
|
|
|
|
|
|
|
target_ulong helper_mret(CPURISCVState *env, target_ulong cpu_pc_deb)
|
|
|
|
{
|
|
|
|
if (!(env->priv >= PRV_M)) {
|
2019-01-15 07:58:23 +08:00
|
|
|
riscv_raise_exception(env, RISCV_EXCP_ILLEGAL_INST, GETPC());
|
2018-03-02 20:31:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
target_ulong retpc = env->mepc;
|
|
|
|
if (!riscv_has_ext(env, RVC) && (retpc & 0x3)) {
|
2019-01-15 07:58:23 +08:00
|
|
|
riscv_raise_exception(env, RISCV_EXCP_INST_ADDR_MIS, GETPC());
|
2018-03-02 20:31:10 +08:00
|
|
|
}
|
|
|
|
|
2020-10-26 19:55:25 +08:00
|
|
|
uint64_t mstatus = env->mstatus;
|
2018-03-02 20:31:10 +08:00
|
|
|
target_ulong prev_priv = get_field(mstatus, MSTATUS_MPP);
|
2020-12-24 03:25:53 +08:00
|
|
|
|
|
|
|
if (!pmp_get_num_rules(env) && (prev_priv != PRV_M)) {
|
|
|
|
riscv_raise_exception(env, RISCV_EXCP_ILLEGAL_INST, GETPC());
|
|
|
|
}
|
|
|
|
|
2020-10-26 19:55:25 +08:00
|
|
|
target_ulong prev_virt = get_field(env->mstatus, MSTATUS_MPV);
|
2020-05-06 04:04:50 +08:00
|
|
|
mstatus = set_field(mstatus, MSTATUS_MIE,
|
|
|
|
get_field(mstatus, MSTATUS_MPIE));
|
2020-01-03 11:53:42 +08:00
|
|
|
mstatus = set_field(mstatus, MSTATUS_MPIE, 1);
|
2018-03-02 20:31:10 +08:00
|
|
|
mstatus = set_field(mstatus, MSTATUS_MPP, PRV_U);
|
2020-02-01 09:02:33 +08:00
|
|
|
mstatus = set_field(mstatus, MSTATUS_MPV, 0);
|
2019-01-05 07:23:55 +08:00
|
|
|
env->mstatus = mstatus;
|
2020-02-01 09:02:33 +08:00
|
|
|
riscv_cpu_set_mode(env, prev_priv);
|
|
|
|
|
|
|
|
if (riscv_has_ext(env, RVH)) {
|
|
|
|
if (prev_virt) {
|
|
|
|
riscv_cpu_swap_hypervisor_regs(env);
|
|
|
|
}
|
|
|
|
|
|
|
|
riscv_cpu_set_virt_enabled(env, prev_virt);
|
|
|
|
}
|
2018-03-02 20:31:10 +08:00
|
|
|
|
|
|
|
return retpc;
|
|
|
|
}
|
|
|
|
|
|
|
|
void helper_wfi(CPURISCVState *env)
|
|
|
|
{
|
2019-03-23 10:11:37 +08:00
|
|
|
CPUState *cs = env_cpu(env);
|
2018-03-02 20:31:10 +08:00
|
|
|
|
2020-02-01 09:02:28 +08:00
|
|
|
if ((env->priv == PRV_S &&
|
|
|
|
get_field(env->mstatus, MSTATUS_TW)) ||
|
|
|
|
riscv_cpu_virt_enabled(env)) {
|
2020-08-13 03:13:49 +08:00
|
|
|
riscv_raise_exception(env, RISCV_EXCP_VIRT_INSTRUCTION_FAULT, GETPC());
|
2019-01-15 07:58:08 +08:00
|
|
|
} else {
|
|
|
|
cs->halted = 1;
|
|
|
|
cs->exception_index = EXCP_HLT;
|
|
|
|
cpu_loop_exit(cs);
|
|
|
|
}
|
2018-03-02 20:31:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void helper_tlb_flush(CPURISCVState *env)
|
|
|
|
{
|
2019-03-23 10:11:37 +08:00
|
|
|
CPUState *cs = env_cpu(env);
|
2019-04-02 03:12:07 +08:00
|
|
|
if (!(env->priv >= PRV_S) ||
|
|
|
|
(env->priv == PRV_S &&
|
|
|
|
get_field(env->mstatus, MSTATUS_TVM))) {
|
2019-01-15 07:58:23 +08:00
|
|
|
riscv_raise_exception(env, RISCV_EXCP_ILLEGAL_INST, GETPC());
|
2020-08-13 03:13:49 +08:00
|
|
|
} else if (riscv_has_ext(env, RVH) && riscv_cpu_virt_enabled(env) &&
|
|
|
|
get_field(env->hstatus, HSTATUS_VTVM)) {
|
|
|
|
riscv_raise_exception(env, RISCV_EXCP_VIRT_INSTRUCTION_FAULT, GETPC());
|
2019-01-15 07:58:08 +08:00
|
|
|
} else {
|
|
|
|
tlb_flush(cs);
|
|
|
|
}
|
2018-03-02 20:31:10 +08:00
|
|
|
}
|
|
|
|
|
2020-04-04 06:54:59 +08:00
|
|
|
void helper_hyp_tlb_flush(CPURISCVState *env)
|
|
|
|
{
|
|
|
|
CPUState *cs = env_cpu(env);
|
|
|
|
|
2020-08-13 03:13:49 +08:00
|
|
|
if (env->priv == PRV_S && riscv_cpu_virt_enabled(env)) {
|
|
|
|
riscv_raise_exception(env, RISCV_EXCP_VIRT_INSTRUCTION_FAULT, GETPC());
|
|
|
|
}
|
|
|
|
|
2020-04-04 06:54:59 +08:00
|
|
|
if (env->priv == PRV_M ||
|
|
|
|
(env->priv == PRV_S && !riscv_cpu_virt_enabled(env))) {
|
|
|
|
tlb_flush(cs);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
riscv_raise_exception(env, RISCV_EXCP_ILLEGAL_INST, GETPC());
|
|
|
|
}
|
|
|
|
|
2020-08-13 03:13:49 +08:00
|
|
|
void helper_hyp_gvma_tlb_flush(CPURISCVState *env)
|
|
|
|
{
|
|
|
|
if (env->priv == PRV_S && !riscv_cpu_virt_enabled(env) &&
|
|
|
|
get_field(env->mstatus, MSTATUS_TVM)) {
|
|
|
|
riscv_raise_exception(env, RISCV_EXCP_ILLEGAL_INST, GETPC());
|
|
|
|
}
|
|
|
|
|
|
|
|
helper_hyp_tlb_flush(env);
|
|
|
|
}
|
|
|
|
|
2020-11-04 12:43:34 +08:00
|
|
|
target_ulong helper_hyp_hlvx_hu(CPURISCVState *env, target_ulong address)
|
2020-08-13 03:13:19 +08:00
|
|
|
{
|
2020-11-04 12:43:34 +08:00
|
|
|
int mmu_idx = cpu_mmu_index(env, true) | TB_FLAGS_PRIV_HYP_ACCESS_MASK;
|
2020-08-13 03:13:19 +08:00
|
|
|
|
2020-11-04 12:43:34 +08:00
|
|
|
return cpu_lduw_mmuidx_ra(env, address, mmu_idx, GETPC());
|
|
|
|
}
|
2020-08-13 03:13:19 +08:00
|
|
|
|
2020-11-04 12:43:34 +08:00
|
|
|
target_ulong helper_hyp_hlvx_wu(CPURISCVState *env, target_ulong address)
|
|
|
|
{
|
|
|
|
int mmu_idx = cpu_mmu_index(env, true) | TB_FLAGS_PRIV_HYP_ACCESS_MASK;
|
|
|
|
|
|
|
|
return cpu_ldl_mmuidx_ra(env, address, mmu_idx, GETPC());
|
2020-08-13 03:13:19 +08:00
|
|
|
}
|
|
|
|
|
2018-03-02 20:31:10 +08:00
|
|
|
#endif /* !CONFIG_USER_ONLY */
|