op_helper.c 4.7 KB
Newer Older
M
Michael Clark 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/*
 * 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 "qemu/log.h"
#include "cpu.h"
#include "qemu/main-loop.h"
#include "exec/exec-all.h"
#include "exec/helper-proto.h"

/* Exceptions processing helpers */
28
void QEMU_NORETURN riscv_raise_exception(CPURISCVState *env,
M
Michael Clark 已提交
29 30
                                          uint32_t exception, uintptr_t pc)
{
31
    CPUState *cs = env_cpu(env);
M
Michael Clark 已提交
32 33 34 35 36 37 38
    qemu_log_mask(CPU_LOG_INT, "%s: %d\n", __func__, exception);
    cs->exception_index = exception;
    cpu_loop_exit_restore(cs, pc);
}

void helper_raise_exception(CPURISCVState *env, uint32_t exception)
{
39
    riscv_raise_exception(env, exception, 0);
M
Michael Clark 已提交
40 41 42 43 44
}

target_ulong helper_csrrw(CPURISCVState *env, target_ulong src,
        target_ulong csr)
{
45 46
    target_ulong val = 0;
    if (riscv_csrrw(env, csr, &val, src, -1) < 0) {
47
        riscv_raise_exception(env, RISCV_EXCP_ILLEGAL_INST, GETPC());
48 49
    }
    return val;
M
Michael Clark 已提交
50 51 52 53 54
}

target_ulong helper_csrrs(CPURISCVState *env, target_ulong src,
        target_ulong csr, target_ulong rs1_pass)
{
55 56
    target_ulong val = 0;
    if (riscv_csrrw(env, csr, &val, -1, rs1_pass ? src : 0) < 0) {
57
        riscv_raise_exception(env, RISCV_EXCP_ILLEGAL_INST, GETPC());
M
Michael Clark 已提交
58
    }
59
    return val;
M
Michael Clark 已提交
60 61 62 63 64
}

target_ulong helper_csrrc(CPURISCVState *env, target_ulong src,
        target_ulong csr, target_ulong rs1_pass)
{
65 66
    target_ulong val = 0;
    if (riscv_csrrw(env, csr, &val, 0, rs1_pass ? src : 0) < 0) {
67
        riscv_raise_exception(env, RISCV_EXCP_ILLEGAL_INST, GETPC());
M
Michael Clark 已提交
68
    }
69
    return val;
M
Michael Clark 已提交
70 71 72 73 74 75 76
}

#ifndef CONFIG_USER_ONLY

target_ulong helper_sret(CPURISCVState *env, target_ulong cpu_pc_deb)
{
    if (!(env->priv >= PRV_S)) {
77
        riscv_raise_exception(env, RISCV_EXCP_ILLEGAL_INST, GETPC());
M
Michael Clark 已提交
78 79 80 81
    }

    target_ulong retpc = env->sepc;
    if (!riscv_has_ext(env, RVC) && (retpc & 0x3)) {
82
        riscv_raise_exception(env, RISCV_EXCP_INST_ADDR_MIS, GETPC());
M
Michael Clark 已提交
83 84
    }

85 86
    if (env->priv_ver >= PRIV_VERSION_1_10_0 &&
        get_field(env->mstatus, MSTATUS_TSR)) {
87
        riscv_raise_exception(env, RISCV_EXCP_ILLEGAL_INST, GETPC());
88 89
    }

M
Michael Clark 已提交
90 91 92 93 94 95 96 97
    target_ulong mstatus = env->mstatus;
    target_ulong prev_priv = get_field(mstatus, MSTATUS_SPP);
    mstatus = set_field(mstatus,
        env->priv_ver >= PRIV_VERSION_1_10_0 ?
        MSTATUS_SIE : MSTATUS_UIE << prev_priv,
        get_field(mstatus, MSTATUS_SPIE));
    mstatus = set_field(mstatus, MSTATUS_SPIE, 0);
    mstatus = set_field(mstatus, MSTATUS_SPP, PRV_U);
98
    riscv_cpu_set_mode(env, prev_priv);
99
    env->mstatus = mstatus;
M
Michael Clark 已提交
100 101 102 103 104 105 106

    return retpc;
}

target_ulong helper_mret(CPURISCVState *env, target_ulong cpu_pc_deb)
{
    if (!(env->priv >= PRV_M)) {
107
        riscv_raise_exception(env, RISCV_EXCP_ILLEGAL_INST, GETPC());
M
Michael Clark 已提交
108 109 110 111
    }

    target_ulong retpc = env->mepc;
    if (!riscv_has_ext(env, RVC) && (retpc & 0x3)) {
112
        riscv_raise_exception(env, RISCV_EXCP_INST_ADDR_MIS, GETPC());
M
Michael Clark 已提交
113 114 115 116 117 118 119 120 121 122
    }

    target_ulong mstatus = env->mstatus;
    target_ulong prev_priv = get_field(mstatus, MSTATUS_MPP);
    mstatus = set_field(mstatus,
        env->priv_ver >= PRIV_VERSION_1_10_0 ?
        MSTATUS_MIE : MSTATUS_UIE << prev_priv,
        get_field(mstatus, MSTATUS_MPIE));
    mstatus = set_field(mstatus, MSTATUS_MPIE, 0);
    mstatus = set_field(mstatus, MSTATUS_MPP, PRV_U);
123
    riscv_cpu_set_mode(env, prev_priv);
124
    env->mstatus = mstatus;
M
Michael Clark 已提交
125 126 127 128 129 130

    return retpc;
}

void helper_wfi(CPURISCVState *env)
{
131
    CPUState *cs = env_cpu(env);
M
Michael Clark 已提交
132

133 134 135
    if (env->priv == PRV_S &&
        env->priv_ver >= PRIV_VERSION_1_10_0 &&
        get_field(env->mstatus, MSTATUS_TW)) {
136
        riscv_raise_exception(env, RISCV_EXCP_ILLEGAL_INST, GETPC());
137 138 139 140 141
    } else {
        cs->halted = 1;
        cs->exception_index = EXCP_HLT;
        cpu_loop_exit(cs);
    }
M
Michael Clark 已提交
142 143 144 145
}

void helper_tlb_flush(CPURISCVState *env)
{
146
    CPUState *cs = env_cpu(env);
147 148 149 150
    if (!(env->priv >= PRV_S) ||
        (env->priv == PRV_S &&
         env->priv_ver >= PRIV_VERSION_1_10_0 &&
         get_field(env->mstatus, MSTATUS_TVM))) {
151
        riscv_raise_exception(env, RISCV_EXCP_ILLEGAL_INST, GETPC());
152 153 154
    } else {
        tlb_flush(cs);
    }
M
Michael Clark 已提交
155 156 157
}

#endif /* !CONFIG_USER_ONLY */