From nobody Wed Oct 23 00:41:02 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=reject dis=none) header.from=sifive.com ARC-Seal: i=1; a=rsa-sha256; t=1729479894; cv=none; d=zohomail.com; s=zohoarc; b=elZVuq9aVPwx6pqA1r+u1hhKfUAy/HTmP43VE4ZF4b3vkdvii/iIKbc/pM0VojQ83lnOVJ++z7sd1WYmPJDmenoJYh6j/2XgMk8KzVWW5cBnSRLPdRW5a9ye7Q1rSReA0JZkia+gDydOeWbRYsB0eQjs+80wdPUKgpTEn7RFuMw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1729479894; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=7IAWwwp4vXj7ejwKefoL8u1R7KPHA5pay3vjw+0Q3CQ=; b=ZmHlpLP774MYcvXqFNKVB4k6tR/tyCOoLoVzJEtfuMPOFCa57HiR8t/adLBE/0GHoRBVB9LlT02QqZipnjyoPQm2fDSGLeFzQfCzyOtTOENtJIyGCyoN6XmVTUpXDjSGwdCq6DvSMH4dsQ2SyabRn/uTHcvcZHz8g9uyUBSlgQE= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=reject dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1729479893960354.63716821838193; Sun, 20 Oct 2024 20:04:53 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1t2iiS-00052O-Qx; Sun, 20 Oct 2024 23:04:20 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1t2iiR-000526-FP for qemu-devel@nongnu.org; Sun, 20 Oct 2024 23:04:19 -0400 Received: from mail-pj1-x1031.google.com ([2607:f8b0:4864:20::1031]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1t2iiP-00076t-IT for qemu-devel@nongnu.org; Sun, 20 Oct 2024 23:04:19 -0400 Received: by mail-pj1-x1031.google.com with SMTP id 98e67ed59e1d1-2e56750bb0dso1580025a91.0 for ; Sun, 20 Oct 2024 20:04:17 -0700 (PDT) Received: from hsinchu16.internal.sifive.com (59-124-168-89.hinet-ip.hinet.net. [59.124.168.89]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2e5ad368b25sm2348846a91.24.2024.10.20.20.04.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 20 Oct 2024 20:04:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1729479856; x=1730084656; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=7IAWwwp4vXj7ejwKefoL8u1R7KPHA5pay3vjw+0Q3CQ=; b=UIrV9zgkLA0vC/J7PnBXXhi4MxMjxHSiUjDk9fZouwwINVS87qdPcI5nKgMJGl7Fvn 60e2cD4i8H7sRfV5bfAKWPPJTtDpguqP8RLpXpytQRg+shXroGAZ0KKmXttOfJwFNtyy 8KGNpgo+LCzuGknKxqa+v6xl/z6FQ0vm/JZYUL7j8TJbfJj/uF0qskyNtu9YpQUnAmBO UECWpsZbxBv9xgHVx8H2Fk5TtiUunSiCFv1qV9Q3oDVUx2o7nTYLYgt97PXaT7gC29AH TCJGFBpZsoUcxrLFewn22fJYns079QsuiYDnXGUyXHeDhGSiRbGceRi+s9BNMYgmy4F1 cGfg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729479856; x=1730084656; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=7IAWwwp4vXj7ejwKefoL8u1R7KPHA5pay3vjw+0Q3CQ=; b=V8xNPon26A9kPe4lOwl2lF2jPW9h7kPAg2PpJ5PVkxG9LWwQmJG44GjxBou/wYNmzh 7J7iUrlz2StUXJH+k2XMHxrFPhPELsZ0pdoIuDuCiTkc8Eke/wVQnmpafGHlntx+T5Ul K6IsWccHEcnZSBpaT0+xtbpAz0ks6/fwUrSfrluH8JRkbVinO7Fvj2Dvakfc3A1wS9vI fQ+C8arpm4yjFogJHZa/33ZPfe9yhgINZs+C8KGkFtaiYkpASjDdA6AnOw4owAZ8i9u2 9KQszeDAZPKFdVxDY0giQpXCPwLuWnzjmrFmWidFGjJ1KkbENCFCQgjHwSI2tJKl805W bZrw== X-Gm-Message-State: AOJu0YxlNBoijORj3SWPpcTZI6a3w95tPa0kyeygdkq7Y8x7aKZFdxYq nkCvGcoSrfJed3i2ArVDeLUlP7wbpuI/5y5CUo/ocTohWPEhVbgbyV2L/SHZzkXe+hBD49nLt5Q deLiB0XMLQ8YzqL5PWN70C9GurODGAcqJmDA9vjl6ByrTQP6ctiMTuYFAzei+TRd/U9xmQ/hwmc hVIaMD71yGppPXxCq0kYLjmGcJcX48Y40zimrUIBQ= X-Google-Smtp-Source: AGHT+IFb5LUnE+MHww/BeOr+7sika4VKf/mguu3XFAV91zhGQ7Y2DIrJx0Je43NbTj85z9n55GqF6Q== X-Received: by 2002:a17:90a:d48b:b0:2e2:8299:2701 with SMTP id 98e67ed59e1d1-2e561732dfbmr12524618a91.20.1729479855540; Sun, 20 Oct 2024 20:04:15 -0700 (PDT) From: frank.chang@sifive.com To: qemu-devel@nongnu.org Cc: Palmer Dabbelt , Alistair Francis , Bin Meng , Weiwei Li , Daniel Henrique Barboza , Liu Zhiwei , qemu-riscv@nongnu.org (open list:RISC-V TCG CPUs), Tommy Wu , Frank Chang Subject: [PATCH v8 4/5] target/riscv: Add Smrnmi mnret instruction Date: Mon, 21 Oct 2024 11:03:57 +0800 Message-ID: <20241021030358.12454-5-frank.chang@sifive.com> X-Mailer: git-send-email 2.43.2 In-Reply-To: <20241021030358.12454-1-frank.chang@sifive.com> References: <20241021030358.12454-1-frank.chang@sifive.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::1031; envelope-from=frank.chang@sifive.com; helo=mail-pj1-x1031.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @sifive.com) X-ZM-MESSAGEID: 1729479895109116600 Content-Type: text/plain; charset="utf-8" From: Tommy Wu This patch adds a new instruction 'mnret'. 'mnret' is an M-mode-only instruction that uses the values in `mnepc` and `mnstatus` to return to the program counter, privilege mode, and virtualization mode of the interrupted context. Signed-off-by: Frank Chang Signed-off-by: Tommy Wu --- target/riscv/helper.h | 1 + target/riscv/insn32.decode | 3 ++ .../riscv/insn_trans/trans_privileged.c.inc | 20 +++++++++ target/riscv/op_helper.c | 45 ++++++++++++++++--- 4 files changed, 64 insertions(+), 5 deletions(-) diff --git a/target/riscv/helper.h b/target/riscv/helper.h index 451261ce5a..16ea240d26 100644 --- a/target/riscv/helper.h +++ b/target/riscv/helper.h @@ -131,6 +131,7 @@ DEF_HELPER_6(csrrw_i128, tl, env, int, tl, tl, tl, tl) #ifndef CONFIG_USER_ONLY DEF_HELPER_1(sret, tl, env) DEF_HELPER_1(mret, tl, env) +DEF_HELPER_1(mnret, tl, env) DEF_HELPER_1(wfi, void, env) DEF_HELPER_1(wrs_nto, void, env) DEF_HELPER_1(tlb_flush, void, env) diff --git a/target/riscv/insn32.decode b/target/riscv/insn32.decode index c45b8fa1d8..d320631e8c 100644 --- a/target/riscv/insn32.decode +++ b/target/riscv/insn32.decode @@ -121,6 +121,9 @@ wfi 0001000 00101 00000 000 00000 1110011 sfence_vma 0001001 ..... ..... 000 00000 1110011 @sfence_vma sfence_vm 0001000 00100 ..... 000 00000 1110011 @sfence_vm =20 +# *** NMI *** +mnret 0111000 00010 00000 000 00000 1110011 + # *** RV32I Base Instruction Set *** lui .................... ..... 0110111 @u auipc .................... ..... 0010111 @u diff --git a/target/riscv/insn_trans/trans_privileged.c.inc b/target/riscv/= insn_trans/trans_privileged.c.inc index bc5263a4e0..b51f30846d 100644 --- a/target/riscv/insn_trans/trans_privileged.c.inc +++ b/target/riscv/insn_trans/trans_privileged.c.inc @@ -18,6 +18,12 @@ * this program. If not, see . */ =20 +#define REQUIRE_SMRNMI(ctx) do { \ + if (!ctx->cfg_ptr->ext_smrnmi) { \ + return false; \ + } \ +} while (0) + static bool trans_ecall(DisasContext *ctx, arg_ecall *a) { /* always generates U-level ECALL, fixed in do_interrupt handler */ @@ -106,6 +112,20 @@ static bool trans_mret(DisasContext *ctx, arg_mret *a) #endif } =20 +static bool trans_mnret(DisasContext *ctx, arg_mnret *a) +{ +#ifndef CONFIG_USER_ONLY + REQUIRE_SMRNMI(ctx); + decode_save_opc(ctx); + gen_helper_mnret(cpu_pc, tcg_env); + tcg_gen_exit_tb(NULL, 0); /* no chaining */ + ctx->base.is_jmp =3D DISAS_NORETURN; + return true; +#else + return false; +#endif +} + static bool trans_wfi(DisasContext *ctx, arg_wfi *a) { #ifndef CONFIG_USER_ONLY diff --git a/target/riscv/op_helper.c b/target/riscv/op_helper.c index 25a5263573..180886f32a 100644 --- a/target/riscv/op_helper.c +++ b/target/riscv/op_helper.c @@ -312,24 +312,30 @@ target_ulong helper_sret(CPURISCVState *env) return retpc; } =20 -target_ulong helper_mret(CPURISCVState *env) +static void check_ret_from_m_mode(CPURISCVState *env, target_ulong retpc, + target_ulong prev_priv) { if (!(env->priv >=3D PRV_M)) { riscv_raise_exception(env, RISCV_EXCP_ILLEGAL_INST, GETPC()); } =20 - target_ulong retpc =3D env->mepc; if (!riscv_has_ext(env, RVC) && (retpc & 0x3)) { riscv_raise_exception(env, RISCV_EXCP_INST_ADDR_MIS, GETPC()); } =20 - uint64_t mstatus =3D env->mstatus; - target_ulong prev_priv =3D get_field(mstatus, MSTATUS_MPP); - if (riscv_cpu_cfg(env)->pmp && !pmp_get_num_rules(env) && (prev_priv !=3D PRV_M)) { riscv_raise_exception(env, RISCV_EXCP_INST_ACCESS_FAULT, GETPC()); } +} + +target_ulong helper_mret(CPURISCVState *env) +{ + target_ulong retpc =3D env->mepc; + uint64_t mstatus =3D env->mstatus; + target_ulong prev_priv =3D get_field(mstatus, MSTATUS_MPP); + + check_ret_from_m_mode(env, retpc, prev_priv); =20 target_ulong prev_virt =3D get_field(env->mstatus, MSTATUS_MPV) && (prev_priv !=3D PRV_M); @@ -353,6 +359,35 @@ target_ulong helper_mret(CPURISCVState *env) return retpc; } =20 +target_ulong helper_mnret(CPURISCVState *env) +{ + target_ulong retpc =3D env->mnepc; + target_ulong prev_priv =3D get_field(env->mnstatus, MNSTATUS_MNPP); + target_ulong prev_virt; + + check_ret_from_m_mode(env, retpc, prev_priv); + + prev_virt =3D get_field(env->mnstatus, MNSTATUS_MNPV) && + (prev_priv !=3D PRV_M); + env->mnstatus =3D set_field(env->mnstatus, MNSTATUS_NMIE, true); + + /* + * If MNRET changes the privilege mode to a mode + * less privileged than M, it also sets mstatus.MPRV to 0. + */ + if (prev_priv < PRV_M) { + env->mstatus =3D set_field(env->mstatus, MSTATUS_MPRV, false); + } + + if (riscv_has_ext(env, RVH) && prev_virt) { + riscv_cpu_swap_hypervisor_regs(env); + } + + riscv_cpu_set_mode(env, prev_priv, prev_virt); + + return retpc; +} + void helper_wfi(CPURISCVState *env) { CPUState *cs =3D env_cpu(env); --=20 2.43.2