From patchwork Tue Dec 17 06:24:34 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Frank Chang X-Patchwork-Id: 13911168 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 4C6B6E7716A for ; Tue, 17 Dec 2024 06:27:10 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tNR1A-0002zD-Rw; Tue, 17 Dec 2024 01:25:16 -0500 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 1tNR14-0002wi-5Y for qemu-devel@nongnu.org; Tue, 17 Dec 2024 01:25:11 -0500 Received: from mail-pf1-x42c.google.com ([2607:f8b0:4864:20::42c]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1tNR12-00052n-1c for qemu-devel@nongnu.org; Tue, 17 Dec 2024 01:25:09 -0500 Received: by mail-pf1-x42c.google.com with SMTP id d2e1a72fcca58-7292a83264eso1687891b3a.0 for ; Mon, 16 Dec 2024 22:25:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1734416704; x=1735021504; 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=H/EMwbW9ao32OaXwASoWqvLNsXxITTvHnX2/S/pK7bk=; b=nw/8vQABY3ZDfK5I9enrytT01ZMb2kJmfCjxIKEM0uKGsT9SrH+jfcWWc61TDuOLYW c7bkjmjpy+0bNYsy7+hI6mN2ciqXUk94pQENk9d7KFBx2edA6bOm58kgSrnF1NAI3raM DxwcaE34u86l8tPNxRZLRwLLf3rl9qekn2FiZtzrYpYt3bBuUb9Bo9YwAXd+ncyc9wHz uoBG4NA2gO6Dvu21LnSS4x4AG32qQaLTE7ouJ+vW+WVTFap1R3JSd6rqs8k+yz/hqu6U so3MPD1D6g09vbMMIknxT67LrT9hV9vc9UkBEjKCeDQ139yAA85hdoNidorJYGhT+vEC I8ig== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734416704; x=1735021504; 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=H/EMwbW9ao32OaXwASoWqvLNsXxITTvHnX2/S/pK7bk=; b=EN1/QpDRdhIUomWMQPnJKZwCTm2nC9JqWivEdntnRBWyMo6AKV5OoQilrmBsPhPc3U mKSqahPBGRTW2OGM1tmfpPvmhBOmLuiYWdMyvNOpRWQhxYD82T3d699nGS21FKha94rh 0W1Dk24qPJJTQNgPX3n8YPAjeb8xUQETUY4Xc9yivscgnsWnkkMFGBxsYZJTS+/iR17f BXhDNFpNGggKa/PEg7ULjOeeNBtInQX9W5iDS5elLAacfv+XhZrvs3xGNBY7c8kcn7wg dDCcIrM8sXAnDXp/ajhOboGWNPJ8N6TjKzG7Iq3bgeHZgWOMiANaalfBOKCgMFAAPHTK P+cA== X-Gm-Message-State: AOJu0YwcaVeQ+5fOGPNbmbgriNwgik3N3G6DyB4loMfsEBjz68JvxFK+ 0l4P+C3d8sBr+Vc+0AVKOTokPGpo4V1aH8GxhHRMFeBXcaIaFrvl0dqNeic1wLW2Z2PfwIcxFB5 6j6J4zVaj3hCEJm8gPPv4/ruAKJTPF4/jYBwi0yekVP7x2BrG6CEIkt3++4eBUw9a54JKfjfZWD UsfRsRrteV6E7a7+MNpUj8VEKWooMySlLL9CX3m3hq7Q== X-Gm-Gg: ASbGncu8231QjLyPC1VWsK6rDxo5fKSiRVhpinLPchcVLrN18fkq5J1nbjhkMLfFK1d 3KqlwV0PgJKXYcNk4S9yUBN2wqk4l64vTX0e+ftVqbfFFwea6bVLPt0YCY4bxYUGnrQwEvodMIT 885TbEaFk/lTOdALYSE8M5tXxLsLmcam8kccQvRnMjXiUZwBu97WFZtQe9F2RgZ5y0+aWMafMXt vlPaGrz+v5urpUf1YH/HOt1YC1/JGXjHaHrKt/3DlpMycGt309WiJYCHItP7ovelxjX2XjMasPL qNxWKG3IfDQ= X-Google-Smtp-Source: AGHT+IEhxxNcDgS5bc3zYHMdMy0u2jBj4nrJj3SyESHEWESAy3C8OzSN9WJYOGvVzyXnh5WMQXmpYA== X-Received: by 2002:a05:6a00:cd5:b0:724:f86e:e3d9 with SMTP id d2e1a72fcca58-7290c181255mr23057700b3a.14.1734416704046; Mon, 16 Dec 2024 22:25:04 -0800 (PST) Received: from fchang-1826.internal.sifive.com ([210.176.154.33]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-72925e6e8b1sm4354301b3a.139.2024.12.16.22.25.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 16 Dec 2024 22:25:03 -0800 (PST) From: frank.chang@sifive.com To: qemu-devel@nongnu.org, qemu-riscv@nongnu.org Cc: Palmer Dabbelt , Alistair Francis , Bin Meng , Weiwei Li , Daniel Henrique Barboza , Liu Zhiwei , Tommy Wu , Frank Chang Subject: [PATCH v10 1/7] target/riscv: Add 'ext_smrnmi' in the RISCVCPUConfig Date: Tue, 17 Dec 2024 14:24:34 +0800 Message-Id: <20241217062440.884261-2-frank.chang@sifive.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241217062440.884261-1-frank.chang@sifive.com> References: <20241217062440.884261-1-frank.chang@sifive.com> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::42c; envelope-from=frank.chang@sifive.com; helo=mail-pf1-x42c.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+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org From: Tommy Wu The boolean variable 'ext_smrnmi' is used to determine whether the Smrnmi extension exists. Signed-off-by: Frank Chang Signed-off-by: Tommy Wu Reviewed-by: Alistair Francis --- target/riscv/cpu_cfg.h | 1 + 1 file changed, 1 insertion(+) diff --git a/target/riscv/cpu_cfg.h b/target/riscv/cpu_cfg.h index fe0c4173d2..28b43932db 100644 --- a/target/riscv/cpu_cfg.h +++ b/target/riscv/cpu_cfg.h @@ -129,6 +129,7 @@ struct RISCVCPUConfig { bool ext_ssaia; bool ext_sscofpmf; bool ext_smepmp; + bool ext_smrnmi; bool rvv_ta_all_1s; bool rvv_ma_all_1s; bool rvv_vl_half_avl; From patchwork Tue Dec 17 06:24:35 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Frank Chang X-Patchwork-Id: 13911167 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 40A3FE7717F for ; Tue, 17 Dec 2024 06:26:59 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tNR1M-00034R-A2; Tue, 17 Dec 2024 01:25:28 -0500 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 1tNR1A-0002zH-Qk for qemu-devel@nongnu.org; Tue, 17 Dec 2024 01:25:16 -0500 Received: from mail-pf1-x42b.google.com ([2607:f8b0:4864:20::42b]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1tNR13-00053K-W5 for qemu-devel@nongnu.org; Tue, 17 Dec 2024 01:25:16 -0500 Received: by mail-pf1-x42b.google.com with SMTP id d2e1a72fcca58-725dbdf380aso3860017b3a.3 for ; Mon, 16 Dec 2024 22:25:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1734416707; x=1735021507; 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=GxSpYyjxaKzA9Tf0vwncvn7U3uu9NWfwmI2ts/PzYy8=; b=YhNCTzc96hKa94vWL6tetunn16Bu6ANDmOSREGcKeDyikXPCKBorqPJoMqqO+vOifw rgVnkGzsq8lnZCdNiggr4P/EWzzqeWdFV5GL9rkCtBY71MnnFbi6WuYTwPgcT4aIy2n4 WXKPasFHBlkiM+1riTtThFBj3JN7nj/TPAvHe8r55scEVH5/A6b5AtRzqLA5oSUKJ91E PFWlktrmRQw2mZ0zu07k++iVwk5CSwQfGOX25YNTaCqG+/cgx3bUMKEVC7Hbf96z2Dkz TbddizTtavSwWvUPI15mSo/FiaDEmD38nz6cVzGXidg9/Cx9v/xIhaNYV8ro/zvfiYzZ qhHA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734416707; x=1735021507; 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=GxSpYyjxaKzA9Tf0vwncvn7U3uu9NWfwmI2ts/PzYy8=; b=hQ+Lw2LWqH3RFoA3W9+LYBKZnoYrkj2iIxi1GLxFH+Q0oXBUT/V0g4OwGAbhTRbNXn 5g6nqGQAREgA5hh4w8xzYpPqFWJiFZK9JhnKBC0wTn/wCyFqQYf4bRSEHNyMKRiznbsW qjfqZX2qcc3pOz2Z0jjYrJpItVD2cmew0IEcFrb00uKUln++kef8OdhnmvcXcz3dVa9c BBWAap/zuRQGL+ASjGAAf5Dc5J8MKsjXCG8fNIulklgS8t0+HTdlMYu+hMOq0tJkWNMi nnyl/phgcP9Hvc0p8P5w/mNL+CIq/74sIS8wSpwXY35W5Q7JuGCwOh/VBvKBS3CvHn+x 25uA== X-Gm-Message-State: AOJu0YxTGfCFVBJTXdxVxj3uCQz1Ii9h9mQm5OxIhQzFCR+DwwmE7z7r gMQ9/FOOH9w8pSbpkhilaq30xu47emGywmsRumaj2rrH2sV4xOnMiGbScdSteoS4Ui8vQ5D6xv+ bQSgoFK93550fcwNFgcjwek6NzIAJz0VK3iRr+ORYqEqOT+TQBIxVPJc1t2CCzA+egRQaYVuGfV jiN+GuaeaTW6MoGykZhR9QIQdBW7O4t6EMIIEC+TuOhw== X-Gm-Gg: ASbGncvLGbFRkntWSObk+RveqRmfFaXbfvHzrMC+lAuug/pjTu76QfS7sSCDXyRUnZ2 HCF3teoCRQIgpX6QLy9qej0EoKRaHU4ulNA5+FCPJxUdz/ruBrPohHuuc7JVeTiARRNvq+tugNI 2sh3WNR3dxoB1JURgY5u60u4dKsYovoMxX0f4QM3aRyXDijv7CUhaWRp3xPTpGiDMTNtfIoXsRK plviI3t/cbJxrwd8tN475GwdOnSPLpm0yGDM8CPtM9JvOU8flcFnpQQ9M9oRTEC61Ahb4HQIp1M DU3ehmbX6f0= X-Google-Smtp-Source: AGHT+IGrLhIe2sqtyJErG/yy1ccoToATmMhB2liie8YXhp31e8nyggegPi3SLTYmh/Z5XS/j5LAuxA== X-Received: by 2002:a05:6a00:9090:b0:725:df1a:273 with SMTP id d2e1a72fcca58-7290c28faf2mr23243690b3a.26.1734416706523; Mon, 16 Dec 2024 22:25:06 -0800 (PST) Received: from fchang-1826.internal.sifive.com ([210.176.154.33]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-72925e6e8b1sm4354301b3a.139.2024.12.16.22.25.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 16 Dec 2024 22:25:06 -0800 (PST) From: frank.chang@sifive.com To: qemu-devel@nongnu.org, qemu-riscv@nongnu.org Cc: Palmer Dabbelt , Alistair Francis , Bin Meng , Weiwei Li , Daniel Henrique Barboza , Liu Zhiwei , Tommy Wu , Frank Chang Subject: [PATCH v10 2/7] target/riscv: Add Smrnmi CSRs Date: Tue, 17 Dec 2024 14:24:35 +0800 Message-Id: <20241217062440.884261-3-frank.chang@sifive.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241217062440.884261-1-frank.chang@sifive.com> References: <20241217062440.884261-1-frank.chang@sifive.com> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::42b; envelope-from=frank.chang@sifive.com; helo=mail-pf1-x42b.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+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org From: Tommy Wu The Smrnmi extension adds the 'mnscratch', 'mnepc', 'mncause', 'mnstatus' CSRs. Signed-off-by: Frank Chang Signed-off-by: Tommy Wu Reviewed-by: Alistair Francis --- target/riscv/cpu.c | 5 +++ target/riscv/cpu.h | 7 ++++ target/riscv/cpu_bits.h | 11 ++++++ target/riscv/csr.c | 82 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 105 insertions(+) diff --git a/target/riscv/cpu.c b/target/riscv/cpu.c index 6748185206..ade9e6e190 100644 --- a/target/riscv/cpu.c +++ b/target/riscv/cpu.c @@ -1134,6 +1134,11 @@ static void riscv_cpu_reset_hold(Object *obj, ResetType type) riscv_trigger_reset_hold(env); } + if (cpu->cfg.ext_smrnmi) { + env->rnmip = 0; + env->mnstatus = set_field(env->mnstatus, MNSTATUS_NMIE, false); + } + if (kvm_enabled()) { kvm_riscv_reset_vcpu(cpu); } diff --git a/target/riscv/cpu.h b/target/riscv/cpu.h index 284b112821..a2cb471b3c 100644 --- a/target/riscv/cpu.h +++ b/target/riscv/cpu.h @@ -486,6 +486,13 @@ struct CPUArchState { uint64_t kvm_timer_state; uint64_t kvm_timer_frequency; #endif /* CONFIG_KVM */ + + /* RNMI */ + target_ulong mnscratch; + target_ulong mnepc; + target_ulong mncause; /* mncause without bit XLEN-1 set to 1 */ + target_ulong mnstatus; + target_ulong rnmip; }; /* diff --git a/target/riscv/cpu_bits.h b/target/riscv/cpu_bits.h index fe4e34c64a..9e9637263d 100644 --- a/target/riscv/cpu_bits.h +++ b/target/riscv/cpu_bits.h @@ -353,6 +353,12 @@ #define CSR_PMPADDR14 0x3be #define CSR_PMPADDR15 0x3bf +/* RNMI */ +#define CSR_MNSCRATCH 0x740 +#define CSR_MNEPC 0x741 +#define CSR_MNCAUSE 0x742 +#define CSR_MNSTATUS 0x744 + /* Debug/Trace Registers (shared with Debug Mode) */ #define CSR_TSELECT 0x7a0 #define CSR_TDATA1 0x7a1 @@ -634,6 +640,11 @@ typedef enum { #define SATP64_ASID 0x0FFFF00000000000ULL #define SATP64_PPN 0x00000FFFFFFFFFFFULL +/* RNMI mnstatus CSR mask */ +#define MNSTATUS_NMIE 0x00000008 +#define MNSTATUS_MNPV 0x00000080 +#define MNSTATUS_MNPP 0x00001800 + /* VM modes (satp.mode) privileged ISA 1.10 */ #define VM_1_10_MBARE 0 #define VM_1_10_SV32 1 diff --git a/target/riscv/csr.c b/target/riscv/csr.c index b6fa8ae53f..65daf6d138 100644 --- a/target/riscv/csr.c +++ b/target/riscv/csr.c @@ -597,6 +597,17 @@ static RISCVException debug(CPURISCVState *env, int csrno) return RISCV_EXCP_ILLEGAL_INST; } + +static RISCVException rnmi(CPURISCVState *env, int csrno) +{ + RISCVCPU *cpu = env_archcpu(env); + + if (cpu->cfg.ext_smrnmi) { + return RISCV_EXCP_NONE; + } + + return RISCV_EXCP_ILLEGAL_INST; +} #endif static RISCVException seed(CPURISCVState *env, int csrno) @@ -4654,6 +4665,67 @@ static RISCVException write_upmbase(CPURISCVState *env, int csrno, return RISCV_EXCP_NONE; } +static RISCVException read_mnscratch(CPURISCVState *env, int csrno, + target_ulong *val) +{ + *val = env->mnscratch; + return RISCV_EXCP_NONE; +} + +static int write_mnscratch(CPURISCVState *env, int csrno, target_ulong val) +{ + env->mnscratch = val; + return RISCV_EXCP_NONE; +} + +static int read_mnepc(CPURISCVState *env, int csrno, target_ulong *val) +{ + *val = env->mnepc; + return RISCV_EXCP_NONE; +} + +static int write_mnepc(CPURISCVState *env, int csrno, target_ulong val) +{ + env->mnepc = val; + return RISCV_EXCP_NONE; +} + +static int read_mncause(CPURISCVState *env, int csrno, target_ulong *val) +{ + *val = env->mncause; + return RISCV_EXCP_NONE; +} + +static int write_mncause(CPURISCVState *env, int csrno, target_ulong val) +{ + env->mncause = val; + return RISCV_EXCP_NONE; +} + +static int read_mnstatus(CPURISCVState *env, int csrno, target_ulong *val) +{ + *val = env->mnstatus; + return RISCV_EXCP_NONE; +} + +static int write_mnstatus(CPURISCVState *env, int csrno, target_ulong val) +{ + target_ulong mask = (MNSTATUS_NMIE | MNSTATUS_MNPP); + + if (riscv_has_ext(env, RVH)) { + /* Flush tlb on mnstatus fields that affect VM. */ + if ((val ^ env->mnstatus) & MNSTATUS_MNPV) { + tlb_flush(env_cpu(env)); + } + + mask |= MNSTATUS_MNPV; + } + + /* mnstatus.mnie can only be cleared by hardware. */ + env->mnstatus = (env->mnstatus & MNSTATUS_NMIE) | (val & mask); + return RISCV_EXCP_NONE; +} + #endif /* Crypto Extension */ @@ -5161,6 +5233,16 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = { write_sstateen_1_3, .min_priv_ver = PRIV_VERSION_1_12_0 }, + /* RNMI */ + [CSR_MNSCRATCH] = { "mnscratch", rnmi, read_mnscratch, write_mnscratch, + .min_priv_ver = PRIV_VERSION_1_12_0 }, + [CSR_MNEPC] = { "mnepc", rnmi, read_mnepc, write_mnepc, + .min_priv_ver = PRIV_VERSION_1_12_0 }, + [CSR_MNCAUSE] = { "mncause", rnmi, read_mncause, write_mncause, + .min_priv_ver = PRIV_VERSION_1_12_0 }, + [CSR_MNSTATUS] = { "mnstatus", rnmi, read_mnstatus, write_mnstatus, + .min_priv_ver = PRIV_VERSION_1_12_0 }, + /* Supervisor Trap Setup */ [CSR_SSTATUS] = { "sstatus", smode, read_sstatus, write_sstatus, NULL, read_sstatus_i128 }, From patchwork Tue Dec 17 06:24:36 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Frank Chang X-Patchwork-Id: 13911165 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 87E0CE7716A for ; Tue, 17 Dec 2024 06:26:21 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tNR1I-00031T-1b; Tue, 17 Dec 2024 01:25:26 -0500 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 1tNR1A-0002zE-P6 for qemu-devel@nongnu.org; Tue, 17 Dec 2024 01:25:16 -0500 Received: from mail-pf1-x429.google.com ([2607:f8b0:4864:20::429]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1tNR16-000545-MW for qemu-devel@nongnu.org; Tue, 17 Dec 2024 01:25:16 -0500 Received: by mail-pf1-x429.google.com with SMTP id d2e1a72fcca58-728f28744c5so4465295b3a.1 for ; Mon, 16 Dec 2024 22:25:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1734416709; x=1735021509; 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=XGSNn9ae/MPwYaGPrUgG0bzpmz+FrdagSkhwlUWUTKY=; b=ZBT0mxnrVEDV+XpQ/YeGrINmSQQdLIkCI2CWerxddPdraiVW10Hvaxy/Sf6z53zucm 57fLo7F5Eq3JMcAverCWBnwsVKdfpvLsK5ve0Hg+48t6uriksp9cKcR6flXlFe5mwBww JfdxNLNnuukYxQgDmMVuV6Ka83a4DqPqinUzW9Ajiz179k+IULAn+Cecjthtum/hGB0z 3JCo6vTCwcBmLY3BksuEOnptnwuxq0kUTmudf1fxbDaWIw6S6sbntufqpd3Rm/vpQp5o abEZpY3nIFmVNW35N+m8wKI3p+piAACGmaFZzyJsAzrYDX95OzQNz9Wu9NhZ2rdJnyrL j7TQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734416709; x=1735021509; 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=XGSNn9ae/MPwYaGPrUgG0bzpmz+FrdagSkhwlUWUTKY=; b=MmUWdUp8cIfqClYfmFAH8smJgoxPbKXODitUDg3rNjGak1QCRasG/9jCDWNBH1E4Yh TdtifjXvzQR+BdsBMpRNVveXoTUPoDiANJKPzKkpoAImbRFBq+H5+aUs8Xwm7V3RNB90 2veIZz/aFpgELmUym2fAwpuC8eNeTyEPf2GYtCzln2MNQmReCxDhAPdYLys47ttRrXCR pB75Sd/zy3ESRGUsP/lNzCxVtZG5gIa2IpJGPiPWM+6BMRfDSlGKww0LNdnBSPsX6NAv mF+RIW+5SBBI9kajNq7VxHOATi39xqkB8uajDv29YSRnhtLj60A2hE90ixLCGVBfxHX1 pC1A== X-Gm-Message-State: AOJu0YzDsrShhPUyk7QxQvMDiuLTynW5iS0AKqO02f83iO30bbuISpaX Hbv+X0pk71+nA6zop2xw46uU1mDrXDqIFwSig2ZGVvW8o6m72m6QlCBFuNJaSk/fNdKmMg3lSsI 18dJO6H3yyGiR44bdPe/4UpolUfz1UDHnnx0BCiEOGETxDoJtI4pDVOBgIx4SXxqTXM+BSxupx/ bekKcG1BJbWxG1UwBB61g9BJ52xPyRFn2MU+ZxQ/9zbg== X-Gm-Gg: ASbGnctwKNh49vQovTlIHoKqI8QRia7tBZk9RZfFfz0FXUwzXODuztE4Ym5gYb5x7ar xPsuXd42zQfydnfOQyn2MPd1qwPWGQCwfdGjutKbbM1ypdOrFIe+j3kCBlcZ5dZTqj9udPiv0+M a0wEv2q55F96ZlHoAGLHiSU+smfgn9goXedydwrC0/onPbI1BmWx4xjg47Jg0S81A5ykbcfdWAk Z94/bK1Zoxqi+IuZVWIX3zPNBh6dzxRW42ElHnRpl2eyahzcQ3OTNR7Snrk+ES3kzaFgSQTc/VG oHcdHDlvGQM= X-Google-Smtp-Source: AGHT+IFa9cjaNPdYOPMTatzEJWZM3W2ox30ZAjQfiJgEGub3xN4rRBvT/5fD4DH5PGDNmszaHsks0Q== X-Received: by 2002:a05:6a00:3d4b:b0:725:df1a:288 with SMTP id d2e1a72fcca58-7290c27461emr23067465b3a.24.1734416709068; Mon, 16 Dec 2024 22:25:09 -0800 (PST) Received: from fchang-1826.internal.sifive.com ([210.176.154.33]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-72925e6e8b1sm4354301b3a.139.2024.12.16.22.25.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 16 Dec 2024 22:25:08 -0800 (PST) From: frank.chang@sifive.com To: qemu-devel@nongnu.org, qemu-riscv@nongnu.org Cc: Palmer Dabbelt , Alistair Francis , Bin Meng , Weiwei Li , Daniel Henrique Barboza , Liu Zhiwei , Tommy Wu , Frank Chang Subject: [PATCH v10 3/7] target/riscv: Handle Smrnmi interrupt and exception Date: Tue, 17 Dec 2024 14:24:36 +0800 Message-Id: <20241217062440.884261-4-frank.chang@sifive.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241217062440.884261-1-frank.chang@sifive.com> References: <20241217062440.884261-1-frank.chang@sifive.com> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::429; envelope-from=frank.chang@sifive.com; helo=mail-pf1-x429.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+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org From: Tommy Wu Because the RNMI interrupt trap handler address is implementation defined. We add the 'rnmi-interrupt-vector' and 'rnmi-exception-vector' as the property of the harts. It’s very easy for users to set the address based on their expectation. This patch also adds the functionality to handle the RNMI signals. Signed-off-by: Frank Chang Signed-off-by: Tommy Wu Reviewed-by: Daniel Henrique Barboza --- hw/riscv/riscv_hart.c | 40 +++++++++++++++++ include/hw/riscv/riscv_hart.h | 4 ++ target/riscv/cpu.c | 11 +++++ target/riscv/cpu.h | 3 ++ target/riscv/cpu_bits.h | 12 +++++ target/riscv/cpu_helper.c | 85 ++++++++++++++++++++++++++++++++--- 6 files changed, 150 insertions(+), 5 deletions(-) diff --git a/hw/riscv/riscv_hart.c b/hw/riscv/riscv_hart.c index 0df454772f..f5e40e608d 100644 --- a/hw/riscv/riscv_hart.c +++ b/hw/riscv/riscv_hart.c @@ -26,6 +26,7 @@ #include "target/riscv/cpu.h" #include "hw/qdev-properties.h" #include "hw/riscv/riscv_hart.h" +#include "qemu/error-report.h" static const Property riscv_harts_props[] = { DEFINE_PROP_UINT32("num-harts", RISCVHartArrayState, num_harts, 1), @@ -33,6 +34,22 @@ static const Property riscv_harts_props[] = { DEFINE_PROP_STRING("cpu-type", RISCVHartArrayState, cpu_type), DEFINE_PROP_UINT64("resetvec", RISCVHartArrayState, resetvec, DEFAULT_RSTVEC), + /* + * Smrnmi implementation-defined interrupt and exception trap handlers. + * + * When an RNMI interrupt is detected, the hart then enters M-mode and + * jumps to the address defined by "rnmi-interrupt-vector". + * + * When the hart encounters an exception while executing in M-mode with + * the mnstatus.NMIE bit clear, the hart then jumps to the address + * defined by "rnmi-exception-vector". + */ + DEFINE_PROP_ARRAY("rnmi-interrupt-vector", RISCVHartArrayState, + num_rnmi_irqvec, rnmi_irqvec, qdev_prop_uint64, + uint64_t), + DEFINE_PROP_ARRAY("rnmi-exception-vector", RISCVHartArrayState, + num_rnmi_excpvec, rnmi_excpvec, qdev_prop_uint64, + uint64_t), DEFINE_PROP_END_OF_LIST(), }; @@ -47,6 +64,29 @@ static bool riscv_hart_realize(RISCVHartArrayState *s, int idx, { object_initialize_child(OBJECT(s), "harts[*]", &s->harts[idx], cpu_type); qdev_prop_set_uint64(DEVICE(&s->harts[idx]), "resetvec", s->resetvec); + + if (s->harts[idx].cfg.ext_smrnmi) { + if (idx < s->num_rnmi_irqvec) { + qdev_prop_set_uint64(DEVICE(&s->harts[idx]), + "rnmi-interrupt-vector", s->rnmi_irqvec[idx]); + } + + if (idx < s->num_rnmi_excpvec) { + qdev_prop_set_uint64(DEVICE(&s->harts[idx]), + "rnmi-exception-vector", s->rnmi_excpvec[idx]); + } + } else { + if (s->num_rnmi_irqvec > 0) { + warn_report_once("rnmi-interrupt-vector property is ignored " + "because Smrnmi extension is not enabled."); + } + + if (s->num_rnmi_excpvec > 0) { + warn_report_once("rnmi-exception-vector property is ignored " + "because Smrnmi extension is not enabled."); + } + } + s->harts[idx].env.mhartid = s->hartid_base + idx; qemu_register_reset(riscv_harts_cpu_reset, &s->harts[idx]); return qdev_realize(DEVICE(&s->harts[idx]), NULL, errp); diff --git a/include/hw/riscv/riscv_hart.h b/include/hw/riscv/riscv_hart.h index 912b4a2682..a6ed73a195 100644 --- a/include/hw/riscv/riscv_hart.h +++ b/include/hw/riscv/riscv_hart.h @@ -38,6 +38,10 @@ struct RISCVHartArrayState { uint32_t hartid_base; char *cpu_type; uint64_t resetvec; + uint32_t num_rnmi_irqvec; + uint64_t *rnmi_irqvec; + uint32_t num_rnmi_excpvec; + uint64_t *rnmi_excpvec; RISCVCPU *harts; }; diff --git a/target/riscv/cpu.c b/target/riscv/cpu.c index ade9e6e190..e6988f44c6 100644 --- a/target/riscv/cpu.c +++ b/target/riscv/cpu.c @@ -1419,6 +1419,11 @@ static void riscv_cpu_set_irq(void *opaque, int irq, int level) g_assert_not_reached(); } } + +static void riscv_cpu_set_nmi(void *opaque, int irq, int level) +{ + riscv_cpu_set_rnmi(RISCV_CPU(opaque), irq, level); +} #endif /* CONFIG_USER_ONLY */ static bool riscv_cpu_is_dynamic(Object *cpu_obj) @@ -1442,6 +1447,8 @@ static void riscv_cpu_init(Object *obj) #ifndef CONFIG_USER_ONLY qdev_init_gpio_in(DEVICE(obj), riscv_cpu_set_irq, IRQ_LOCAL_MAX + IRQ_LOCAL_GUEST_MAX); + qdev_init_gpio_in_named(DEVICE(cpu), riscv_cpu_set_nmi, + "riscv.cpu.rnmi", RNMI_MAX); #endif /* CONFIG_USER_ONLY */ general_user_opts = g_hash_table_new(g_str_hash, g_str_equal); @@ -2797,6 +2804,10 @@ static const Property riscv_cpu_properties[] = { #ifndef CONFIG_USER_ONLY DEFINE_PROP_UINT64("resetvec", RISCVCPU, env.resetvec, DEFAULT_RSTVEC), + DEFINE_PROP_UINT64("rnmi-interrupt-vector", RISCVCPU, env.rnmi_irqvec, + DEFAULT_RNMI_IRQVEC), + DEFINE_PROP_UINT64("rnmi-exception-vector", RISCVCPU, env.rnmi_excpvec, + DEFAULT_RNMI_EXCPVEC), #endif DEFINE_PROP_BOOL("short-isa-string", RISCVCPU, cfg.short_isa_string, false), diff --git a/target/riscv/cpu.h b/target/riscv/cpu.h index a2cb471b3c..8dc5b4d002 100644 --- a/target/riscv/cpu.h +++ b/target/riscv/cpu.h @@ -493,6 +493,8 @@ struct CPUArchState { target_ulong mncause; /* mncause without bit XLEN-1 set to 1 */ target_ulong mnstatus; target_ulong rnmip; + uint64_t rnmi_irqvec; + uint64_t rnmi_excpvec; }; /* @@ -591,6 +593,7 @@ void riscv_cpu_swap_hypervisor_regs(CPURISCVState *env); int riscv_cpu_claim_interrupts(RISCVCPU *cpu, uint64_t interrupts); uint64_t riscv_cpu_update_mip(CPURISCVState *env, uint64_t mask, uint64_t value); +void riscv_cpu_set_rnmi(RISCVCPU *cpu, uint32_t irq, bool level); void riscv_cpu_interrupt(CPURISCVState *env); #define BOOL_TO_MASK(x) (-!!(x)) /* helper for riscv_cpu_update_mip value */ void riscv_cpu_set_rdtime_fn(CPURISCVState *env, uint64_t (*fn)(void *), diff --git a/target/riscv/cpu_bits.h b/target/riscv/cpu_bits.h index 9e9637263d..17787fd693 100644 --- a/target/riscv/cpu_bits.h +++ b/target/riscv/cpu_bits.h @@ -680,6 +680,12 @@ typedef enum { /* Default Reset Vector address */ #define DEFAULT_RSTVEC 0x1000 +/* Default RNMI Interrupt Vector address */ +#define DEFAULT_RNMI_IRQVEC 0x0 + +/* Default RNMI Exception Vector address */ +#define DEFAULT_RNMI_EXCPVEC 0x0 + /* Exception causes */ typedef enum RISCVException { RISCV_EXCP_NONE = -1, /* sentinel value */ @@ -734,6 +740,9 @@ typedef enum RISCVException { /* -1 is due to bit zero of hgeip and hgeie being ROZ. */ #define IRQ_LOCAL_GUEST_MAX (TARGET_LONG_BITS - 1) +/* RNMI causes */ +#define RNMI_MAX 16 + /* mip masks */ #define MIP_USIP (1 << IRQ_U_SOFT) #define MIP_SSIP (1 << IRQ_S_SOFT) @@ -972,6 +981,9 @@ typedef enum RISCVException { #define MHPMEVENT_IDX_MASK 0xFFFFF #define MHPMEVENT_SSCOF_RESVD 16 +/* RISC-V-specific interrupt pending bits. */ +#define CPU_INTERRUPT_RNMI CPU_INTERRUPT_TGT_EXT_0 + /* JVT CSR bits */ #define JVT_MODE 0x3F #define JVT_BASE (~0x3F) diff --git a/target/riscv/cpu_helper.c b/target/riscv/cpu_helper.c index 750c0537ca..e5ffbbbd83 100644 --- a/target/riscv/cpu_helper.c +++ b/target/riscv/cpu_helper.c @@ -505,6 +505,18 @@ static int riscv_cpu_local_irq_pending(CPURISCVState *env) uint64_t vsbits, irq_delegated; int virq; + /* Priority: RNMI > Other interrupt. */ + if (riscv_cpu_cfg(env)->ext_smrnmi) { + /* If mnstatus.NMIE == 0, all interrupts are disabled. */ + if (!get_field(env->mnstatus, MNSTATUS_NMIE)) { + return RISCV_EXCP_NONE; + } + + if (env->rnmip) { + return ctz64(env->rnmip); /* since non-zero */ + } + } + /* Determine interrupt enable state of all privilege modes */ if (env->virt_enabled) { mie = 1; @@ -567,7 +579,9 @@ static int riscv_cpu_local_irq_pending(CPURISCVState *env) bool riscv_cpu_exec_interrupt(CPUState *cs, int interrupt_request) { - if (interrupt_request & CPU_INTERRUPT_HARD) { + uint32_t mask = CPU_INTERRUPT_HARD | CPU_INTERRUPT_RNMI; + + if (interrupt_request & mask) { RISCVCPU *cpu = RISCV_CPU(cs); CPURISCVState *env = &cpu->env; int interruptno = riscv_cpu_local_irq_pending(env); @@ -699,6 +713,30 @@ void riscv_cpu_set_geilen(CPURISCVState *env, target_ulong geilen) env->geilen = geilen; } +void riscv_cpu_set_rnmi(RISCVCPU *cpu, uint32_t irq, bool level) +{ + CPURISCVState *env = &cpu->env; + CPUState *cs = CPU(cpu); + bool release_lock = false; + + if (!bql_locked()) { + release_lock = true; + bql_lock(); + } + + if (level) { + env->rnmip |= 1 << irq; + cpu_interrupt(cs, CPU_INTERRUPT_RNMI); + } else { + env->rnmip &= ~(1 << irq); + cpu_reset_interrupt(cs, CPU_INTERRUPT_RNMI); + } + + if (release_lock) { + bql_unlock(); + } +} + int riscv_cpu_claim_interrupts(RISCVCPU *cpu, uint64_t interrupts) { CPURISCVState *env = &cpu->env; @@ -1849,6 +1887,7 @@ void riscv_cpu_do_interrupt(CPUState *cs) bool write_gva = false; bool always_storeamo = (env->excp_uw2 & RISCV_UW2_ALWAYS_STORE_AMO); uint64_t s; + int mode; /* * cs->exception is 32-bits wide unlike mcause which is XLEN-bits wide @@ -1867,6 +1906,23 @@ void riscv_cpu_do_interrupt(CPUState *cs) target_ulong mtval2 = 0; int sxlen = 0; int mxlen = 0; + bool nnmi_excep = false; + + if (cpu->cfg.ext_smrnmi && env->rnmip && async) { + env->mnstatus = set_field(env->mnstatus, MNSTATUS_NMIE, false); + env->mnstatus = set_field(env->mnstatus, MNSTATUS_MNPV, + env->virt_enabled); + env->mnstatus = set_field(env->mnstatus, MNSTATUS_MNPP, + env->priv); + env->mncause = cause | ((target_ulong)1U << (TARGET_LONG_BITS - 1)); + env->mnepc = env->pc; + env->pc = env->rnmi_irqvec; + + /* Trapping to M mode, virt is disabled */ + riscv_cpu_set_mode(env, PRV_M, false); + + return; + } if (!async) { /* set tval to badaddr for traps with address information */ @@ -1960,8 +2016,10 @@ void riscv_cpu_do_interrupt(CPUState *cs) __func__, env->mhartid, async, cause, env->pc, tval, riscv_cpu_get_trap_name(cause, async)); - if (env->priv <= PRV_S && cause < 64 && - (((deleg >> cause) & 1) || s_injected || vs_injected)) { + mode = env->priv <= PRV_S && cause < 64 && + (((deleg >> cause) & 1) || s_injected || vs_injected) ? PRV_S : PRV_M; + + if (mode == PRV_S) { /* handle the trap in S-mode */ /* save elp status */ if (cpu_get_fcfien(env)) { @@ -2016,6 +2074,14 @@ void riscv_cpu_do_interrupt(CPUState *cs) ((async && (env->stvec & 3) == 1) ? cause * 4 : 0); riscv_cpu_set_mode(env, PRV_S, virt); } else { + /* + * If the hart encounters an exception while executing in M-mode + * with the mnstatus.NMIE bit clear, the exception is an RNMI exception. + */ + nnmi_excep = cpu->cfg.ext_smrnmi && + !get_field(env->mnstatus, MNSTATUS_NMIE) && + !async; + /* handle the trap in M-mode */ /* save elp status */ if (cpu_get_fcfien(env)) { @@ -2049,8 +2115,17 @@ void riscv_cpu_do_interrupt(CPUState *cs) env->mtval = tval; env->mtval2 = mtval2; env->mtinst = tinst; - env->pc = (env->mtvec >> 2 << 2) + - ((async && (env->mtvec & 3) == 1) ? cause * 4 : 0); + + /* + * For RNMI exception, program counter is set to the RNMI exception + * trap handler address. + */ + if (nnmi_excep) { + env->pc = env->rnmi_excpvec; + } else { + env->pc = (env->mtvec >> 2 << 2) + + ((async && (env->mtvec & 3) == 1) ? cause * 4 : 0); + } riscv_cpu_set_mode(env, PRV_M, virt); } From patchwork Tue Dec 17 06:24:37 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Frank Chang X-Patchwork-Id: 13911164 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 11283E7716A for ; Tue, 17 Dec 2024 06:25:59 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tNR1O-00036U-Ax; Tue, 17 Dec 2024 01:25:30 -0500 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 1tNR1A-0002zF-Pv for qemu-devel@nongnu.org; Tue, 17 Dec 2024 01:25:16 -0500 Received: from mail-pf1-x42c.google.com ([2607:f8b0:4864:20::42c]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1tNR17-00054L-MP for qemu-devel@nongnu.org; Tue, 17 Dec 2024 01:25:16 -0500 Received: by mail-pf1-x42c.google.com with SMTP id d2e1a72fcca58-728e729562fso4180164b3a.0 for ; Mon, 16 Dec 2024 22:25:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1734416712; x=1735021512; 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=1YBszf0JEY6lGH98UF5Cbrpj+6ZfpD+LYDcXM9Rwtik=; b=L5jMV+GAnm+VDqHqhuccHbccJqf3HXZA+Fmo6ba6V/Qw7x5SVWsON2VpwcWHmo7Ddw mqwQknYk0P6T9wwJnZwbPmP9XhKh2+3bpb0c4vdeDkI4VhsFCDl1MxrScgw4bDKh1aP+ LVgzcXXi66P7HMAKE5UUcFA/PCyc0cMzgRzia2I9dqZshWAsQYvEctm/HnifEpB1Lv4A KnF5PCi4o+bBhZPiYQJj0wsnKvxxK3qerPpbOUnGMH7ok7ZJtgGbny2qryHiDWkNB00X 0bus9Lolm4Bt6/6D0QM9hV3/LPZKSI3MeA+OnRYBLjg3qwBvbQeKvLAeytfT9S/tlr2i tWww== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734416712; x=1735021512; 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=1YBszf0JEY6lGH98UF5Cbrpj+6ZfpD+LYDcXM9Rwtik=; b=p7B9KMGC8p53YD08vHbffWSK596TtUkJ6yoPlCjImKEBr8KhfMPnOhsXlLIQkP4JUy /6KX2KgDV4s9l5tv5bKcg5wqlG25ys9zgyMM1fglReqtPDl7rwwqM4rGaDjGBt4Y5wwy x2z2a33ymUel78JmkQ+ZlSxHvfZfbw2XyedmfdBq86N0fGV+TT1DuJB341/a475jjq9i pxH5ZnG0YcNFc2GTB06MVGRJyH7Mhw9hZ9XucznN+/u5VyxBftFfDMlnnQciBgtq/s+9 HttDc3cfV9dSW8zE1sBi4ru6LauYmdGDexaSMwUesAprdXTBNlw//poCAfCEAZ7gvUH7 WQTA== X-Gm-Message-State: AOJu0Ywj6qCilM2zu6EjAZ02+UwCYmOn29lJ0ndGXam34LufEaOVBgbj 3r3NmVF/J9Fl5XAkboWZmhavLb2UpcanXnMxchIUIAvYDr8BXtZnjPAD3qB5szIgyE8DJ1xa/9W 7IPHd1PdTwWzu1H7nAU6OJqF7fgXcvkITNJ0Knkd6oNUfH65CRg2a27KdOBqMopHHIvbzl+A6gB hRA+uAUAzI1ejtimBx8qJuFEQXjo55WX2IlkSzWu7tiA== X-Gm-Gg: ASbGncuslUBI+L2uMfJvFcuu9lCwqtanzscCdH4h4w4RYnP6zNk5qHAvXJA8RFqkBR5 4ybeSDAtvpMy4BvGtwLZ7rHS+wrNwmD0Kwjn9weoQwCR3g43lH9//F/ZoN6xnfo9oIg33eVCliS S+kUXZX14DQ+aGOnXluD2jXRX+27xJrFZ66x/N1iGEnXIgK/WpoVq1Gjpuphej2bkht86AZodel 75lb2p4Xw9ZwH/GcNAAbyBRy+ubSjY7cUywGTnruz28KowE0uA97DmgLyoUkRBwc+qc4X5xwQCr J3CBmIkWqB8= X-Google-Smtp-Source: AGHT+IEsCps6JcmiXQ13A8bvBhhQaAz2zYfx9lwwi+Fq2rY8xMTRnyswiZeaA0QlY7alWQe1NS9uNg== X-Received: by 2002:a05:6a21:3947:b0:1dc:e8d:c8f0 with SMTP id adf61e73a8af0-1e1dfe12af4mr24555198637.29.1734416711618; Mon, 16 Dec 2024 22:25:11 -0800 (PST) Received: from fchang-1826.internal.sifive.com ([210.176.154.33]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-72925e6e8b1sm4354301b3a.139.2024.12.16.22.25.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 16 Dec 2024 22:25:11 -0800 (PST) From: frank.chang@sifive.com To: qemu-devel@nongnu.org, qemu-riscv@nongnu.org Cc: Palmer Dabbelt , Alistair Francis , Bin Meng , Weiwei Li , Daniel Henrique Barboza , Liu Zhiwei , Tommy Wu , Frank Chang Subject: [PATCH v10 4/7] target/riscv: Add Smrnmi mnret instruction Date: Tue, 17 Dec 2024 14:24:37 +0800 Message-Id: <20241217062440.884261-5-frank.chang@sifive.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241217062440.884261-1-frank.chang@sifive.com> References: <20241217062440.884261-1-frank.chang@sifive.com> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::42c; envelope-from=frank.chang@sifive.com; helo=mail-pf1-x42c.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=ham 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+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org 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 Reviewed-by: Daniel Henrique Barboza --- 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 e9139ec1b9..942c434c6e 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 +# *** NMI *** +mnret 0111000 00010 00000 000 00000 1110011 + # *** RV32I Base Instruction Set *** lui .................... ..... 0110111 @u { diff --git a/target/riscv/insn_trans/trans_privileged.c.inc b/target/riscv/insn_trans/trans_privileged.c.inc index ecd3b8b2c9..73f940d406 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 . */ +#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 } +static bool trans_mnret(DisasContext *ctx, arg_mnret *a) +{ +#ifndef CONFIG_USER_ONLY + REQUIRE_SMRNMI(ctx); + decode_save_opc(ctx, 0); + gen_helper_mnret(cpu_pc, tcg_env); + tcg_gen_exit_tb(NULL, 0); /* no chaining */ + ctx->base.is_jmp = 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 eddedacf4b..63ec53e992 100644 --- a/target/riscv/op_helper.c +++ b/target/riscv/op_helper.c @@ -321,24 +321,30 @@ target_ulong helper_sret(CPURISCVState *env) return retpc; } -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 >= PRV_M)) { riscv_raise_exception(env, RISCV_EXCP_ILLEGAL_INST, GETPC()); } - target_ulong retpc = env->mepc; if (!riscv_has_ext(env, RVC) && (retpc & 0x3)) { riscv_raise_exception(env, RISCV_EXCP_INST_ADDR_MIS, GETPC()); } - uint64_t mstatus = env->mstatus; - target_ulong prev_priv = get_field(mstatus, MSTATUS_MPP); - if (riscv_cpu_cfg(env)->pmp && !pmp_get_num_rules(env) && (prev_priv != PRV_M)) { riscv_raise_exception(env, RISCV_EXCP_INST_ACCESS_FAULT, GETPC()); } +} + +target_ulong helper_mret(CPURISCVState *env) +{ + target_ulong retpc = env->mepc; + uint64_t mstatus = env->mstatus; + target_ulong prev_priv = get_field(mstatus, MSTATUS_MPP); + + check_ret_from_m_mode(env, retpc, prev_priv); target_ulong prev_virt = get_field(env->mstatus, MSTATUS_MPV) && (prev_priv != PRV_M); @@ -370,6 +376,35 @@ target_ulong helper_mret(CPURISCVState *env) return retpc; } +target_ulong helper_mnret(CPURISCVState *env) +{ + target_ulong retpc = env->mnepc; + target_ulong prev_priv = get_field(env->mnstatus, MNSTATUS_MNPP); + target_ulong prev_virt; + + check_ret_from_m_mode(env, retpc, prev_priv); + + prev_virt = get_field(env->mnstatus, MNSTATUS_MNPV) && + (prev_priv != PRV_M); + env->mnstatus = 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 = 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 = env_cpu(env); From patchwork Tue Dec 17 06:24:38 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Frank Chang X-Patchwork-Id: 13911166 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 79C33E77184 for ; Tue, 17 Dec 2024 06:26:23 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tNR1T-0003BH-9M; Tue, 17 Dec 2024 01:25:36 -0500 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 1tNR1I-00031g-Jv for qemu-devel@nongnu.org; Tue, 17 Dec 2024 01:25:26 -0500 Received: from mail-pf1-x42f.google.com ([2607:f8b0:4864:20::42f]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1tNR1B-00054o-LI for qemu-devel@nongnu.org; Tue, 17 Dec 2024 01:25:24 -0500 Received: by mail-pf1-x42f.google.com with SMTP id d2e1a72fcca58-725ef0397aeso4336204b3a.2 for ; Mon, 16 Dec 2024 22:25:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1734416714; x=1735021514; 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=ao9h6MAmm6BUcuG9oLKvxKpzXvKNCgrPHPGGD/55dt0=; b=JzLFZN3TczODuAIAdCrucRzBGhIb5MuifucWHnYElqsAmUCrSsn98oeAJVmantQ0KW yvjKOR8hGQGKYS+tPgElLQ2scHvf5g8XZc49Yt2dPwmy4N/xG7YIt53EQ2t+//mWU6/i /5BpJoYycpjpVaPePP6rqtta1lMpUiWFT+FpNYES7jjgq+lh0zm88JYHEXX7tO/uwWIU 7WbKR8Vival65CrS8xQ6niAmZYCbbvmQSZ7pPoC0G0h6z4VJMWPAoWBzsM0S5Wc3tQVy TaIoWxtOFtKRnEI+eDoCoTapiZwb7yopHTYRmeg0YbaNFJT3XeeOouT+PhXFptFxRjLz uHNw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734416714; x=1735021514; 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=ao9h6MAmm6BUcuG9oLKvxKpzXvKNCgrPHPGGD/55dt0=; b=jSHcJnvWoMGARnnBHRbPEqRBZVW1sfeYNI1jJdXk3dm86J6hOAY31PUGmK0JksHg/A mWMruZsQt6Cks7N1ujz6uXFzBLmS7O7WdYB9L2JQXx5xqYJXnEzFcbpDk+AaxVJAw93C NKrp0YjsyvQx3HDO0Rxn24d4rBrchUwMXJsDpXn6AM90XZOBDBVjSZU3miUsbwvD/hIG 1ehCa8bLjS/cgcLAs7I6axXkMELvald/9uBO+XLK/n0mwNvZPbXEFdMZq1Jmr4LAxgKS 5sfydahshS0jCgEnJfbODW7OPoRtOJmkMRfMXZUtDE+Iwvr/ojio7L7kODy02mJClQLU YXGA== X-Gm-Message-State: AOJu0YwD4PhpCcqO0P7NvU6ZUx5+t1FhqCdj8PNjI3tLEdIjrrAogY91 GLP/Q7UktmK7dQgJx7lRiNSyzJO8QGIgGJpbeUs2FiGyqZEi3K4iypt/BeOIlfBd5Hs50U03W0Z bmcj3/Y7k5T23dw7fEOSYE7d3fLHc+BXADVb1W8zliWBrE9mKvl5e4Ccdd/LA6P5c8prn5H63ON bLJ6cVqzuVUrBt5Gn9nIV6N+RvXevsPhG8VuKR7dSyiQ== X-Gm-Gg: ASbGnctym/cJYAci6icOtiRNWJdKzDNkorOL5YoSbUvMHvDDg8ejQ9bNOkJeXSuBOju F+J/rXXRQboGOM+154kDI1X2yW+dkH5dvLxZXdM5Meg0sGbHGrpHI7icYy3Caa5E7U+glub9yRR BEVCoYTL6s/vrsfSTiiPVallyUFoT//U5MUaV4NscRs6S3e0/4622EXlxvDuf5DYzZqjDYcEC0W su1RpGK9SdIAbl8ZwmYgmsvRHGLGJloMFU5cZRGnCicWym7JS1K6C5Y7EsxZFhOL0ZP9Q2VgIwg PeL4T7o7Vdk= X-Google-Smtp-Source: AGHT+IHceF+NCERcd30L/eEI3ORecwFOuuPdPpucKZl3xiJ8hixvPiajKtt/c0yVxFAG5NyKKbJ/Yw== X-Received: by 2002:a05:6a00:301a:b0:725:e015:9082 with SMTP id d2e1a72fcca58-7290c0e17dcmr22978385b3a.5.1734416714123; Mon, 16 Dec 2024 22:25:14 -0800 (PST) Received: from fchang-1826.internal.sifive.com ([210.176.154.33]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-72925e6e8b1sm4354301b3a.139.2024.12.16.22.25.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 16 Dec 2024 22:25:13 -0800 (PST) From: frank.chang@sifive.com To: qemu-devel@nongnu.org, qemu-riscv@nongnu.org Cc: Palmer Dabbelt , Alistair Francis , Bin Meng , Weiwei Li , Daniel Henrique Barboza , Liu Zhiwei , Tommy Wu , Frank Chang Subject: [PATCH v10 5/7] target/riscv: Add Smrnmi cpu extension Date: Tue, 17 Dec 2024 14:24:38 +0800 Message-Id: <20241217062440.884261-6-frank.chang@sifive.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241217062440.884261-1-frank.chang@sifive.com> References: <20241217062440.884261-1-frank.chang@sifive.com> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::42f; envelope-from=frank.chang@sifive.com; helo=mail-pf1-x42f.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+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org From: Tommy Wu This adds the properties for ISA extension Smrnmi. Signed-off-by: Frank Chang Signed-off-by: Tommy Wu Reviewed-by: Daniel Henrique Barboza --- target/riscv/cpu.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/target/riscv/cpu.c b/target/riscv/cpu.c index e6988f44c6..7a4aa235ce 100644 --- a/target/riscv/cpu.c +++ b/target/riscv/cpu.c @@ -193,6 +193,7 @@ const RISCVIsaExtData isa_edata_arr[] = { ISA_EXT_DATA_ENTRY(smaia, PRIV_VERSION_1_12_0, ext_smaia), ISA_EXT_DATA_ENTRY(smcntrpmf, PRIV_VERSION_1_12_0, ext_smcntrpmf), ISA_EXT_DATA_ENTRY(smepmp, PRIV_VERSION_1_12_0, ext_smepmp), + ISA_EXT_DATA_ENTRY(smrnmi, PRIV_VERSION_1_12_0, ext_smrnmi), ISA_EXT_DATA_ENTRY(smstateen, PRIV_VERSION_1_12_0, ext_smstateen), ISA_EXT_DATA_ENTRY(ssaia, PRIV_VERSION_1_12_0, ext_ssaia), ISA_EXT_DATA_ENTRY(ssccptr, PRIV_VERSION_1_11_0, has_priv_1_11), @@ -1621,6 +1622,7 @@ const RISCVCPUMultiExtConfig riscv_cpu_extensions[] = { MULTI_EXT_CFG_BOOL("smaia", ext_smaia, false), MULTI_EXT_CFG_BOOL("smepmp", ext_smepmp, false), + MULTI_EXT_CFG_BOOL("smrnmi", ext_smrnmi, false), MULTI_EXT_CFG_BOOL("smstateen", ext_smstateen, false), MULTI_EXT_CFG_BOOL("ssaia", ext_ssaia, false), MULTI_EXT_CFG_BOOL("svade", ext_svade, false), From patchwork Tue Dec 17 06:24:39 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Frank Chang X-Patchwork-Id: 13911162 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id A0576E7717F for ; Tue, 17 Dec 2024 06:25:51 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tNR1S-0003Ao-4S; Tue, 17 Dec 2024 01:25:34 -0500 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 1tNR1H-00031X-HC for qemu-devel@nongnu.org; Tue, 17 Dec 2024 01:25:24 -0500 Received: from mail-pf1-x42a.google.com ([2607:f8b0:4864:20::42a]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1tNR1D-00055Q-Qr for qemu-devel@nongnu.org; Tue, 17 Dec 2024 01:25:22 -0500 Received: by mail-pf1-x42a.google.com with SMTP id d2e1a72fcca58-728e3826211so3869414b3a.0 for ; Mon, 16 Dec 2024 22:25:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1734416716; x=1735021516; 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=1bK2ArkonOP0houuEuxLWOOOjjUDvLRrzXIxB3WX61g=; b=WRoWqIn0G11IPA3N9oyO2EwAbilzWqKz9qfeK35mqEO2AGBISlhYwFNAOkpqir7jdL euCoPgbftAHBQenffQNut4IJRfWT+pSLnFbtrN+LXWpy5d77GabsPGs0LG7ugC8OYVUv BKBoFRu6cj6OdrYZy/0dDw+9ZgYPsuy2Ic+OgFYjE1mpaFWh+eRCcX2UtnlDFiK9ofIa a2qc9cL+/nD+2BNVILleVVQC+4vAX3aPEN1pmfdwZ32iZDRmQjC7WflZb19PRNNr4MCv OWOkafgqKqn2KlcdaFLmB/ULEUq5/3GubHInl9kDon5ShH+y4/WEUlYmqJa+keFovLPJ Bh/A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734416716; x=1735021516; 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=1bK2ArkonOP0houuEuxLWOOOjjUDvLRrzXIxB3WX61g=; b=cOPlheOXvkPCzRqzIXfOxZAbF4kvvL8gjbSUNtz5aF+UJHdAQm3ZSJuPJbUVf7TPGN YFYZZ7+TeJ9N9hYuFvVvSXdJAoHccGPfxIh9uhmMWZHNq9TpPgCxCdEF5VQvJRvh59zz H5OJw2YqxKGMQlMNH5V3TOFQLfugTvkCDIaLAbo0p1IpwBWQgdO837HYemCNNTAr5rey nvIKSkoo7EHUllO1SV9Ox0ucllgNWQH4p2z1/t+mwU1w5pMQrqlCoLIReByUdvlfLbve AguhENxfTA6Fz5EhJLqTkGuA53HoRIE9GQXDTjxOv8va7lTTm64kotFm4VoqmZ0fVPqJ vcGA== X-Gm-Message-State: AOJu0YxFNypb3GbcZEs6Cxm3mnwMHN7ItehSDgUyZsKxcgFUFXZOz7kf DMq8yYR4/3Q0kX7i0HYwEaoB51K4A8pGjMjah0uh89qLsleLDteBd+kfF8M3JAjoDG3BvLXKRXM fQ+anU0e80GHqUbDOmsp+0MjwMiiGDJwJnrWGlq5yfYtHqi46OHYEKM697dNTky+ode0RSUuPjM OE94e7gMH9pmy8pTB+dRFtW94xqVee/kVcHYegrTmR+A== X-Gm-Gg: ASbGncsq6Y6B9SRJul+6JoVuqbd2fNJPTycllXZduwIKEIZMS50yCdVxmSYwNY3p1bY t8U4VSPJ3LlqWMS7/0oGg6h1svDcNijX6r1JY/8xZAFadR3DJQsnLlDuh5Utsfso8d5YMG1nKdW XfZFnNPQfLOq3Yuta58w+4f98WcxVZ6ggqq4rSjrzOTqjhX78GWkQyKK7hRjpiZU/BYqDyAixH3 67LLyFDtHXF7UsRPF7RuJdZZSZJEZTgx6TTCI1K7OkBTeEUHT2sVG3VsDZphaSp76IkhKmm9KnE g8chQpLCb2s= X-Google-Smtp-Source: AGHT+IFuAMSns6PhkQ/yfA7NVjy2jjCp9gCmIn8Lk1qea84ycwOZHml7k11L9CYGzSG/T7/1at8XgQ== X-Received: by 2002:a05:6a20:cf8d:b0:1e1:a094:f20e with SMTP id adf61e73a8af0-1e45ab381d0mr4876969637.17.1734416716474; Mon, 16 Dec 2024 22:25:16 -0800 (PST) Received: from fchang-1826.internal.sifive.com ([210.176.154.33]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-72925e6e8b1sm4354301b3a.139.2024.12.16.22.25.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 16 Dec 2024 22:25:16 -0800 (PST) From: frank.chang@sifive.com To: qemu-devel@nongnu.org, qemu-riscv@nongnu.org Cc: Palmer Dabbelt , Alistair Francis , Bin Meng , Weiwei Li , Daniel Henrique Barboza , Liu Zhiwei , Frank Chang Subject: [PATCH v10 6/7] target/riscv: Add Zicfilp support for Smrnmi Date: Tue, 17 Dec 2024 14:24:39 +0800 Message-Id: <20241217062440.884261-7-frank.chang@sifive.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241217062440.884261-1-frank.chang@sifive.com> References: <20241217062440.884261-1-frank.chang@sifive.com> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::42a; envelope-from=frank.chang@sifive.com; helo=mail-pf1-x42a.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+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org From: Frank Chang Zicfilp extension introduces the MNPELP (bit 9) in mnstatus. The MNPELP field holds the previous ELP. When a RNMI trap is delivered, the MNPELP is set to ELP and ELP set to NO_LP_EXPECTED. Upon a mnret, if the mnstatus.MNPP holds the value y, then ELP is set to the value of MNPELP if yLPE is 1; otherwise, it is set to NO_LP_EXPECTED. Signed-off-by: Frank Chang Reviewed-by: Daniel Henrique Barboza --- target/riscv/cpu_bits.h | 1 + target/riscv/cpu_helper.c | 11 ++++++++++- target/riscv/op_helper.c | 9 +++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/target/riscv/cpu_bits.h b/target/riscv/cpu_bits.h index 17787fd693..be9d0f5c05 100644 --- a/target/riscv/cpu_bits.h +++ b/target/riscv/cpu_bits.h @@ -643,6 +643,7 @@ typedef enum { /* RNMI mnstatus CSR mask */ #define MNSTATUS_NMIE 0x00000008 #define MNSTATUS_MNPV 0x00000080 +#define MNSTATUS_MNPELP 0x00000200 #define MNSTATUS_MNPP 0x00001800 /* VM modes (satp.mode) privileged ISA 1.10 */ diff --git a/target/riscv/cpu_helper.c b/target/riscv/cpu_helper.c index e5ffbbbd83..1fb1e31031 100644 --- a/target/riscv/cpu_helper.c +++ b/target/riscv/cpu_helper.c @@ -1918,6 +1918,10 @@ void riscv_cpu_do_interrupt(CPUState *cs) env->mnepc = env->pc; env->pc = env->rnmi_irqvec; + if (cpu_get_fcfien(env)) { + env->mnstatus = set_field(env->mnstatus, MNSTATUS_MNPELP, env->elp); + } + /* Trapping to M mode, virt is disabled */ riscv_cpu_set_mode(env, PRV_M, false); @@ -2085,7 +2089,12 @@ void riscv_cpu_do_interrupt(CPUState *cs) /* handle the trap in M-mode */ /* save elp status */ if (cpu_get_fcfien(env)) { - env->mstatus = set_field(env->mstatus, MSTATUS_MPELP, env->elp); + if (nnmi_excep) { + env->mnstatus = set_field(env->mnstatus, MNSTATUS_MNPELP, + env->elp); + } else { + env->mstatus = set_field(env->mstatus, MSTATUS_MPELP, env->elp); + } } if (riscv_has_ext(env, RVH)) { diff --git a/target/riscv/op_helper.c b/target/riscv/op_helper.c index 63ec53e992..a4b625fcd9 100644 --- a/target/riscv/op_helper.c +++ b/target/riscv/op_helper.c @@ -402,6 +402,15 @@ target_ulong helper_mnret(CPURISCVState *env) riscv_cpu_set_mode(env, prev_priv, prev_virt); + /* + * If forward cfi enabled for new priv, restore elp status + * and clear mnpelp in mnstatus + */ + if (cpu_get_fcfien(env)) { + env->elp = get_field(env->mnstatus, MNSTATUS_MNPELP); + } + env->mnstatus = set_field(env->mnstatus, MNSTATUS_MNPELP, 0); + return retpc; } From patchwork Tue Dec 17 06:24:40 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Frank Chang X-Patchwork-Id: 13911169 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 66FA1E7717F for ; Tue, 17 Dec 2024 06:27:20 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tNR1Q-000371-2x; Tue, 17 Dec 2024 01:25:32 -0500 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 1tNR1J-00033Q-IZ for qemu-devel@nongnu.org; Tue, 17 Dec 2024 01:25:26 -0500 Received: from mail-pf1-x432.google.com ([2607:f8b0:4864:20::432]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1tNR1F-00055e-Rc for qemu-devel@nongnu.org; Tue, 17 Dec 2024 01:25:24 -0500 Received: by mail-pf1-x432.google.com with SMTP id d2e1a72fcca58-728f1525565so5815167b3a.1 for ; Mon, 16 Dec 2024 22:25:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1734416719; x=1735021519; 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=tl/PDO9SOOn4SJKgZz+HudXPKCnTCdotF3cwk73STA0=; b=MzDCMtkYWA01WKyxuu8tH0hxi0dWYmK3bZHoofdWCi2P82euhsUfMGfZ40BquCTE+Q 8NKVtWl+sVJ5sAvWoN/p4S9A1cA4XvkJrpUY/cW46v2KB0vo1tRuhgjziGFQdo41fk8J H2DPv8z88BxE0CyQwgdVJW3TMwvGWRgWwghJyCOrLwOKTsGCWJLDRMxExpzMwP1iv5ns mzKfMVXHmi+03qPKdopte6R+UBEXcccZIvxz/26N6kUPXkFkKeDJAQGesR6YQC5Yw2py 1ZiKjZL9z3S/rrfJ6+CfU2I3GyaK0CYNbsZHpFgDe7lzElZlAKHf+99SmyRv7BSXztzL wTcQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734416719; x=1735021519; 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=tl/PDO9SOOn4SJKgZz+HudXPKCnTCdotF3cwk73STA0=; b=eoq8KMcU7/JrLrHgpXNodkw/KnJ6SLWa1bJliQ0t1wN2z7vhlSqPNnRv0CWZsCg/DW 1ZLLf7k/rKpCuCIk9rfYcpjRQtEEhRQPcs8pKAkvs5GeIWTKwn+J9HsrVyA+tAQgH8+o UI2kDPTRpLHXHfzuY+FEj6gtEHdimdExbLKJxRHA4GlzWFOvVMaUuE/78TRaG+hdtRW5 a4zdDt1ex8vCOeB6oWgg9y2YLZ6VAWv8JsYYvAiOp2hCmlnf5SXkJwJw+x8fHYGZJbIL pBVCIlbyT04s9ZHSVVULAnRDhiBvf5mQr86cZAy5+gYY9CnfuHiL/4HAcGd2pZagawxI iDlg== X-Gm-Message-State: AOJu0Yyjvg9d0i6kddQGAk/yCtt3aSDp0Kqkcd54X8FWtYk6r78Zb+8v WxqT9DKte5SbBhANJJ8VCIXlO3njBP3C54qMtD9DuSAVGZYV4hfXFF4cG9yaAGNhBAB5apDD58x gUq+6twUg/gjZK6X1YXcwC3NE5vkTysCDzuMDd9D4A2MAZxBOaKbpS7jwAqhGJchohno/aPeFgx uvhSuaM3wsy14ul/FygdJUMUMGibuRFkgEKxDhrczgxw== X-Gm-Gg: ASbGnctCE+PlkRsUF1b7VeREZ6iLy024fHJbHANZm1HF+ZYV9YMalW6ze73LcB8DTSe io1mhoL0zuTWzv9W90c+86D4fry15r0Hd1lzUlp3ZtOIQlMDgP8dMoqlT+KD1raMusO5fyWVsCv 7+2N36vSCovPHtfmSnfdH10f4SR2PhxilRoyUQKesC3QTziBZE3PnNGlcHTL4SebIlXUwx16tOA aVBaXRIVNrvJaf7BMIbhzDd8tiHxPHsXYCqUxks+UuNSQbh/XH2Xe+A2lXmu8yyphPfGM+qVk9x rL8XFMEZtLo= X-Google-Smtp-Source: AGHT+IGEzxKvdwsW3/o8+hACJ64Q7X4/KJM+RPkNXJ2J2uih2Ep2XnbA9al+Pcu1PUrUCbHwnJ2kmg== X-Received: by 2002:a05:6a00:ac02:b0:725:ebab:bb32 with SMTP id d2e1a72fcca58-7290c27c888mr22537423b3a.26.1734416718898; Mon, 16 Dec 2024 22:25:18 -0800 (PST) Received: from fchang-1826.internal.sifive.com ([210.176.154.33]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-72925e6e8b1sm4354301b3a.139.2024.12.16.22.25.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 16 Dec 2024 22:25:18 -0800 (PST) From: frank.chang@sifive.com To: qemu-devel@nongnu.org, qemu-riscv@nongnu.org Cc: Palmer Dabbelt , Alistair Francis , Bin Meng , Weiwei Li , Daniel Henrique Barboza , Liu Zhiwei , Frank Chang Subject: [PATCH v10 7/7] target/riscv: Disable Smrnmi for the 'max' type CPU Date: Tue, 17 Dec 2024 14:24:40 +0800 Message-Id: <20241217062440.884261-8-frank.chang@sifive.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241217062440.884261-1-frank.chang@sifive.com> References: <20241217062440.884261-1-frank.chang@sifive.com> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::432; envelope-from=frank.chang@sifive.com; helo=mail-pf1-x432.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+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org From: Frank Chang When Smrnmi is present, the firmware (e.g., OpenSBI) must set mnstatus.NMIE to 1 before enabling any interrupts. Otherwise, all interrupts will be disabled. Since our current OpenSBI does not support Smrnmi yet, let's disable Smrnmi for the 'max' type CPU for now. We can re-enable it once OpenSBI includes proper support for it. Signed-off-by: Frank Chang Signed-off-by: Daniel Henrique Barboza --- target/riscv/tcg/tcg-cpu.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/target/riscv/tcg/tcg-cpu.c b/target/riscv/tcg/tcg-cpu.c index 3480767b35..f3fb1c432b 100644 --- a/target/riscv/tcg/tcg-cpu.c +++ b/target/riscv/tcg/tcg-cpu.c @@ -1429,6 +1429,12 @@ static void riscv_init_max_cpu_extensions(Object *obj) if (env->misa_mxl != MXL_RV32) { isa_ext_update_enabled(cpu, CPU_CFG_OFFSET(ext_zcf), false); } + + /* + * ext_smrnmi requires OpenSBI changes that our current + * image does not have. Disable it for now. + */ + isa_ext_update_enabled(cpu, CPU_CFG_OFFSET(ext_smrnmi), false); } static bool riscv_cpu_has_max_extensions(Object *cpu_obj)