From nobody Sun Feb 8 05:29:03 2026 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 ARC-Seal: i=1; a=rsa-sha256; t=1648488327; cv=none; d=zohomail.com; s=zohoarc; b=QN7TBUGJsvj6D+G4tcmu7qnHA8ZDKnYzrl8Z0U0vYy1LUg1iW+ieJCo/2O/zyOvRrV+eTSsydS7zKAplNWTtlnKDmHUVX2pC+TQW4WNcR3TwWLw8fRryeUleaM5BXtv5NqMIvkl7TyHVoKgoRtT4Llx9hHB6dD9YOtdGas3dpmA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1648488327; h=Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:To; bh=bZF1myso7Bf1GeIOhOOdvrusgverIvdrFtIffKfTV6c=; b=DdK+nO23h6VRy4bVCvBn3LtYiqdQQtBMHyMUV4WSoxOzAY3cI81JFNBDMIez2Lvwj1+BzawR+xp047aydgQ43JBqkXOyy6aaNDE5msGWrV1qdq+5WJjx2098J2IZdwrtiODnpJ2AXiFkQoxQkihJXcGRGpQc8R9IW67RemkQ4NM= 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1648488327336573.0047415653576; Mon, 28 Mar 2022 10:25:27 -0700 (PDT) Received: from localhost ([::1]:41540 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nYt7N-0002FH-Ml for importer@patchew.org; Mon, 28 Mar 2022 13:25:25 -0400 Received: from eggs.gnu.org ([209.51.188.92]:57696) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nYt5a-0008A6-Vn for qemu-devel@nongnu.org; Mon, 28 Mar 2022 13:23:36 -0400 Received: from [2607:f8b0:4864:20::430] (port=45652 helo=mail-pf1-x430.google.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nYt5Y-00033F-U1 for qemu-devel@nongnu.org; Mon, 28 Mar 2022 13:23:34 -0400 Received: by mail-pf1-x430.google.com with SMTP id s8so13278077pfk.12 for ; Mon, 28 Mar 2022 10:23:32 -0700 (PDT) Received: from ThinkPad-T490.dc1.ventanamicro.com ([171.50.204.174]) by smtp.googlemail.com with ESMTPSA id 132-20020a62168a000000b004f40e8b3133sm17802504pfw.188.2022.03.28.10.23.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 28 Mar 2022 10:23:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ventanamicro.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=bZF1myso7Bf1GeIOhOOdvrusgverIvdrFtIffKfTV6c=; b=c+EWgO7r4p6fpN4UnHPZxBxYhwK3g8XT4AXW9d0+tQ9JOTv/vX1Ua3JzS+4NgZ+okN CCV+D9D+/l40Bme1MSDquyeCi1BAC0WI+8EVDLwmhUfbPW7J6c9pUHNXZq59qUVm4ccR MvyHREOImoqxdn5d62DEVDO5lN8YsuzOmBQEmVwd+yv0lBF+8ZhDo6Qt+5rVEhjWIGoW n/XCtboUazHBEgWoIkxAhvOFy84jdwUsFulc07FAtDJk+IqeAhDmMZ0nsnKNb3dTPk3y rW7k7ngP7jkNQaEfQ7E8+8P/LnbXMTumsk9N1Y3GgjqP9VDVoPnO6oADsXN+TiEe2mbJ iDeQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=bZF1myso7Bf1GeIOhOOdvrusgverIvdrFtIffKfTV6c=; b=zMnq5anunv7tR5VchXxQPIhhFDnj+X6xiSujmVBqdkZ07CysacJSuayu16PN9YQ4OL 5qy7Reih4Y57NdpDpr3/6Y9NRWw/tvcYXYETJzseomaOnrVnjlVZH6/3fczMsdMG6Wus s0Ey7YdY+9l91nxhcuqo4KvbwvUYVrTtb6KeYYzStIpSbpUdAToL7ZWdJvia/ps9Wili GXDGrXOolQFr0WVxyfZjJA15SO2AVpS9NBmuR07+44TkQijycCSy7JXaqJdaF+skpODD QY4S5+lI1NeIGd3WdPccB2090fZZriFohdq2xCrndCWTaKloHj7AQezpowKKQkflOOpg QczA== X-Gm-Message-State: AOAM532BE76fjYEVqGM9+vZ4jWWm036j4XGSL9uUTl4qjsVKVboPqcC1 gW8bFZdSD64cuWhtE76xgM4VDJUgDk5StCOh X-Google-Smtp-Source: ABdhPJxV9VY8j9Eqnpf/+GDtzoigXxHkl7bhB0CMzLd2rFo/3TpYqWBZ16VeUD2CrFDyOgysl8zsFA== X-Received: by 2002:a63:5ce:0:b0:382:1f05:c8b1 with SMTP id 197-20020a6305ce000000b003821f05c8b1mr10861305pgf.19.1648488210889; Mon, 28 Mar 2022 10:23:30 -0700 (PDT) From: Mayuresh Chitale To: qemu-devel@nongnu.org, qemu-riscv@nongnu.org Subject: [RFC PATCH v3 1/4] target/riscv: Add smstateen support Date: Mon, 28 Mar 2022 22:53:16 +0530 Message-Id: <20220328172319.6802-2-mchitale@ventanamicro.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20220328172319.6802-1-mchitale@ventanamicro.com> References: <20220328172319.6802-1-mchitale@ventanamicro.com> X-Host-Lookup-Failed: Reverse DNS lookup failed for 2607:f8b0:4864:20::430 (failed) 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::430; envelope-from=mchitale@ventanamicro.com; helo=mail-pf1-x430.google.com X-Spam_score_int: -6 X-Spam_score: -0.7 X-Spam_bar: / X-Spam_report: (-0.7 / 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, PDS_HP_HELO_NORDNS=0.659, RCVD_IN_DNSWL_NONE=-0.0001, RDNS_NONE=0.793, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=no 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: , Cc: Mayuresh Chitale , alistair.francis@wdc.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: pass (identity @ventanamicro.com) X-ZM-MESSAGEID: 1648488329635100001 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Smstateen extension specifies a mechanism to close the potential covert channels that could cause security issues. This patch adds the CSRs defined in the specification and the corresponding predicates and read/write functions. Signed-off-by: Mayuresh Chitale --- target/riscv/cpu.c | 2 + target/riscv/cpu.h | 4 + target/riscv/cpu_bits.h | 36 +++++++ target/riscv/csr.c | 210 ++++++++++++++++++++++++++++++++++++++++ target/riscv/machine.c | 21 ++++ 5 files changed, 273 insertions(+) diff --git a/target/riscv/cpu.c b/target/riscv/cpu.c index 3aa2ef5cce..dc5472834d 100644 --- a/target/riscv/cpu.c +++ b/target/riscv/cpu.c @@ -788,6 +788,7 @@ static Property riscv_cpu_properties[] =3D { DEFINE_PROP_BOOL("Zve64f", RISCVCPU, cfg.ext_zve64f, false), DEFINE_PROP_BOOL("mmu", RISCVCPU, cfg.mmu, true), DEFINE_PROP_BOOL("pmp", RISCVCPU, cfg.pmp, true), + DEFINE_PROP_BOOL("smstateen", RISCVCPU, cfg.ext_smstateen, true), =20 DEFINE_PROP_STRING("priv_spec", RISCVCPU, cfg.priv_spec), DEFINE_PROP_STRING("vext_spec", RISCVCPU, cfg.vext_spec), @@ -929,6 +930,7 @@ static void riscv_isa_string_ext(RISCVCPU *cpu, char **= isa_str, int max_str_len) ISA_EDATA_ENTRY(zhinxmin, ext_zhinxmin), ISA_EDATA_ENTRY(zve32f, ext_zve32f), ISA_EDATA_ENTRY(zve64f, ext_zve64f), + ISA_EDATA_ENTRY(smstateen, ext_smstateen), }; =20 for (i =3D 0; i < ARRAY_SIZE(isa_edata_arr); i++) { diff --git a/target/riscv/cpu.h b/target/riscv/cpu.h index c9956513bc..4a3b5a6c23 100644 --- a/target/riscv/cpu.h +++ b/target/riscv/cpu.h @@ -307,6 +307,9 @@ struct CPUArchState { =20 /* CSRs for execution enviornment configuration */ uint64_t menvcfg; + uint64_t mstateen[SMSTATEEN_MAX_COUNT]; + uint64_t hstateen[SMSTATEEN_MAX_COUNT]; + uint64_t sstateen[SMSTATEEN_MAX_COUNT]; target_ulong senvcfg; uint64_t henvcfg; #endif @@ -378,6 +381,7 @@ struct RISCVCPUConfig { bool ext_zhinxmin; bool ext_zve32f; bool ext_zve64f; + bool ext_smstateen; =20 /* Vendor-specific custom extensions */ bool ext_XVentanaCondOps; diff --git a/target/riscv/cpu_bits.h b/target/riscv/cpu_bits.h index bb47cf7e77..b2b0bcb929 100644 --- a/target/riscv/cpu_bits.h +++ b/target/riscv/cpu_bits.h @@ -205,6 +205,12 @@ /* Supervisor Configuration CSRs */ #define CSR_SENVCFG 0x10A =20 +/* Supervisor state CSRs */ +#define CSR_SSTATEEN0 0x10C +#define CSR_SSTATEEN1 0x10D +#define CSR_SSTATEEN2 0x10E +#define CSR_SSTATEEN3 0x10F + /* Supervisor Trap Handling */ #define CSR_SSCRATCH 0x140 #define CSR_SEPC 0x141 @@ -254,6 +260,16 @@ #define CSR_HENVCFG 0x60A #define CSR_HENVCFGH 0x61A =20 +/* Hypervisor state CSRs */ +#define CSR_HSTATEEN0 0x60C +#define CSR_HSTATEEN0H 0x61C +#define CSR_HSTATEEN1 0x60D +#define CSR_HSTATEEN1H 0x61D +#define CSR_HSTATEEN2 0x60E +#define CSR_HSTATEEN2H 0x61E +#define CSR_HSTATEEN3 0x60F +#define CSR_HSTATEEN3H 0x61F + /* Virtual CSRs */ #define CSR_VSSTATUS 0x200 #define CSR_VSIE 0x204 @@ -301,6 +317,26 @@ #define CSR_MENVCFG 0x30A #define CSR_MENVCFGH 0x31A =20 +/* Machine state CSRs */ +#define CSR_MSTATEEN0 0x30C +#define CSR_MSTATEEN0H 0x31C +#define CSR_MSTATEEN1 0x30D +#define CSR_MSTATEEN1H 0x31D +#define CSR_MSTATEEN2 0x30E +#define CSR_MSTATEEN2H 0x31E +#define CSR_MSTATEEN3 0x30F +#define CSR_MSTATEEN3H 0x31F + +/* Common defines for all smstateen */ +#define SMSTATEEN_MAX_COUNT 4 +#define SMSTATEEN0_CS 0 +#define SMSTATEEN0_FCSR 0 +#define SMSTATEEN0_IMSIC 58 +#define SMSTATEEN0_AIA 59 +#define SMSTATEEN0_SVSLCT 60 +#define SMSTATEEN0_HSENVCFG 62 +#define SMSTATEEN_STATEN 63 + /* Enhanced Physical Memory Protection (ePMP) */ #define CSR_MSECCFG 0x747 #define CSR_MSECCFGH 0x757 diff --git a/target/riscv/csr.c b/target/riscv/csr.c index 2c0319ad12..e3dafc37ef 100644 --- a/target/riscv/csr.c +++ b/target/riscv/csr.c @@ -245,6 +245,42 @@ static RISCVException hmode32(CPURISCVState *env, int = csrno) =20 } =20 +static RISCVException mstateen(CPURISCVState *env, int csrno) +{ + CPUState *cs =3D env_cpu(env); + RISCVCPU *cpu =3D RISCV_CPU(cs); + + if (!cpu->cfg.ext_smstateen) { + return RISCV_EXCP_ILLEGAL_INST; + } + + return any(env, csrno); +} + +static RISCVException hstateen(CPURISCVState *env, int csrno) +{ + CPUState *cs =3D env_cpu(env); + RISCVCPU *cpu =3D RISCV_CPU(cs); + + if (!cpu->cfg.ext_smstateen) { + return RISCV_EXCP_ILLEGAL_INST; + } + + return hmode(env, csrno); +} + +static RISCVException sstateen(CPURISCVState *env, int csrno) +{ + CPUState *cs =3D env_cpu(env); + RISCVCPU *cpu =3D RISCV_CPU(cs); + + if (!cpu->cfg.ext_smstateen) { + return RISCV_EXCP_ILLEGAL_INST; + } + + return smode(env, csrno); +} + /* Checks if PointerMasking registers could be accessed */ static RISCVException pointer_masking(CPURISCVState *env, int csrno) { @@ -1493,6 +1529,129 @@ static RISCVException write_henvcfgh(CPURISCVState = *env, int csrno, return RISCV_EXCP_NONE; } =20 +static inline void write_smstateen(CPURISCVState *env, uint64_t *reg, + uint64_t wr_mask, uint64_t new_val) +{ + *reg =3D (*reg & ~wr_mask) | (new_val & wr_mask); +} + +static RISCVException read_mstateen(CPURISCVState *env, int csrno, + target_ulong *val) +{ + *val =3D env->mstateen[csrno - CSR_MSTATEEN0]; + + return RISCV_EXCP_NONE; +} + +static RISCVException write_mstateen(CPURISCVState *env, int csrno, + target_ulong new_val) +{ + uint64_t *reg; + uint64_t wr_mask =3D 1UL << SMSTATEEN_STATEN; + + reg =3D &env->mstateen[csrno - CSR_MSTATEEN0]; + write_smstateen(env, reg, wr_mask, new_val); + + return RISCV_EXCP_NONE; +} + +static RISCVException read_mstateenh(CPURISCVState *env, int csrno, + target_ulong *val) +{ + *val =3D env->mstateen[csrno - CSR_MSTATEEN0H] >> 32; + + return RISCV_EXCP_NONE; +} + +static RISCVException write_mstateenh(CPURISCVState *env, int csrno, + target_ulong new_val) +{ + uint64_t *reg; + uint64_t val; + uint64_t wr_mask =3D 1UL << SMSTATEEN_STATEN; + + reg =3D &env->mstateen[csrno - CSR_MSTATEEN0H]; + val =3D (uint64_t)new_val << 32; + val |=3D *reg & 0xFFFFFFFF; + write_smstateen(env, reg, wr_mask, val); + + return RISCV_EXCP_NONE; +} + +static RISCVException read_hstateen(CPURISCVState *env, int csrno, + target_ulong *val) +{ + *val =3D env->hstateen[csrno - CSR_HSTATEEN0]; + + return RISCV_EXCP_NONE; +} + +static RISCVException write_hstateen(CPURISCVState *env, int csrno, + target_ulong new_val) +{ + uint64_t *reg; + uint64_t wr_mask =3D 1UL << SMSTATEEN_STATEN; + int index =3D csrno - CSR_HSTATEEN0; + + reg =3D &env->hstateen[index]; + wr_mask &=3D env->mstateen[index]; + write_smstateen(env, reg, wr_mask, new_val); + + return RISCV_EXCP_NONE; +} + +static RISCVException read_hstateenh(CPURISCVState *env, int csrno, + target_ulong *val) +{ + *val =3D env->hstateen[csrno - CSR_HSTATEEN0H] >> 32; + + return RISCV_EXCP_NONE; +} + +static RISCVException write_hstateenh(CPURISCVState *env, int csrno, + target_ulong new_val) +{ + uint64_t *reg; + uint64_t val; + uint64_t wr_mask =3D 1UL << SMSTATEEN_STATEN; + int index =3D csrno - CSR_HSTATEEN0H; + + reg =3D &env->hstateen[index]; + val =3D (uint64_t)new_val << 32; + val |=3D *reg & 0xFFFFFFFF; + wr_mask &=3D env->mstateen[index]; + + write_smstateen(env, reg, wr_mask, val); + return RISCV_EXCP_NONE; +} + +static RISCVException read_sstateen(CPURISCVState *env, int csrno, + target_ulong *val) +{ + *val =3D env->sstateen[csrno - CSR_SSTATEEN0]; + + return RISCV_EXCP_NONE; +} + +static RISCVException write_sstateen(CPURISCVState *env, int csrno, + target_ulong new_val) +{ + uint64_t *reg; + uint64_t wr_mask =3D 0; + int index =3D csrno - CSR_SSTATEEN0; + bool virt =3D riscv_cpu_virt_enabled(env); + + reg =3D &env->sstateen[index]; + if (virt) { + wr_mask &=3D env->mstateen[index]; + } else { + wr_mask &=3D env->hstateen[index]; + } + write_smstateen(env, reg, wr_mask, new_val); + + return RISCV_EXCP_NONE; +} + static RISCVException rmw_mip64(CPURISCVState *env, int csrno, uint64_t *ret_val, uint64_t new_val, uint64_t wr_mask) @@ -3268,6 +3427,57 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] =3D { [CSR_HENVCFGH] =3D { "henvcfgh", hmode32, read_henvcfgh, write_henvcfg= h, .min_priv_ver =3D PRIV_VERSION_1= _12_0 }, =20 + /* Smstateen extension CSRs */ + [CSR_MSTATEEN0] =3D { "mstateen0", mstateen, read_mstateen, write_msta= teen, + .min_priv_ver =3D PRIV_VERSION_1_12_0 }, + [CSR_MSTATEEN0H] =3D { "mstateen0h", mstateen, read_mstateenh, + write_mstateenh, + .min_priv_ver =3D PRIV_VERSION_1_12_0 }, + [CSR_MSTATEEN1] =3D { "mstateen1", mstateen, read_mstateen, write_msta= teen, + .min_priv_ver =3D PRIV_VERSION_1_12_0 }, + [CSR_MSTATEEN1H] =3D { "mstateen1h", mstateen, read_mstateenh, + write_mstateenh, + .min_priv_ver =3D PRIV_VERSION_1_12_0 }, + [CSR_MSTATEEN2] =3D { "mstateen2", mstateen, read_mstateen, write_msta= teen, + .min_priv_ver =3D PRIV_VERSION_1_12_0 }, + [CSR_MSTATEEN2H] =3D { "mstateen2h", mstateen, read_mstateenh, + write_mstateenh, + .min_priv_ver =3D PRIV_VERSION_1_12_0 }, + [CSR_MSTATEEN3] =3D { "mstateen3", mstateen, read_mstateen, write_msta= teen, + .min_priv_ver =3D PRIV_VERSION_1_12_0 }, + [CSR_MSTATEEN3H] =3D { "mstateen3h", mstateen, read_mstateenh, + write_mstateenh, + .min_priv_ver =3D PRIV_VERSION_1_12_0 }, + + [CSR_HSTATEEN0] =3D { "hstateen0", hstateen, read_hstateen, write_hsta= teen, + .min_priv_ver =3D PRIV_VERSION_1_12_0 }, + [CSR_HSTATEEN0H] =3D { "hstateen0h", hstateen, read_hstateenh, + write_hstateenh, + .min_priv_ver =3D PRIV_VERSION_1_12_0 }, + [CSR_HSTATEEN1] =3D { "hstateen1", hstateen, read_hstateen, write_hsta= teen, + .min_priv_ver =3D PRIV_VERSION_1_12_0 }, + [CSR_HSTATEEN1H] =3D { "hstateen1h", hstateen, read_hstateenh, + write_hstateenh, + .min_priv_ver =3D PRIV_VERSION_1_12_0 }, + [CSR_HSTATEEN2] =3D { "hstateen2", hstateen, read_hstateen, write_hsta= teen, + .min_priv_ver =3D PRIV_VERSION_1_12_0 }, + [CSR_HSTATEEN2H] =3D { "hstateen2h", hstateen, read_hstateenh, + write_hstateenh, + .min_priv_ver =3D PRIV_VERSION_1_12_0 }, + [CSR_HSTATEEN3] =3D { "hstateen3", hstateen, read_hstateen, write_hsta= teen, + .min_priv_ver =3D PRIV_VERSION_1_12_0 }, + [CSR_HSTATEEN3H] =3D { "hstateen3h", hstateen, read_hstateenh, + write_hstateenh, + .min_priv_ver =3D PRIV_VERSION_1_12_0 }, + + [CSR_SSTATEEN0] =3D { "sstateen0", sstateen, read_sstateen, write_ssta= teen, + .min_priv_ver =3D PRIV_VERSION_1_12_0 }, + [CSR_SSTATEEN1] =3D { "sstateen1", sstateen, read_sstateen, write_ssta= teen, + .min_priv_ver =3D PRIV_VERSION_1_12_0 }, + [CSR_SSTATEEN2] =3D { "sstateen2", sstateen, read_sstateen, write_ssta= teen, + .min_priv_ver =3D PRIV_VERSION_1_12_0 }, + [CSR_SSTATEEN3] =3D { "sstateen3", sstateen, read_sstateen, write_ssta= teen, + .min_priv_ver =3D PRIV_VERSION_1_12_0 }, /* Supervisor Trap Setup */ [CSR_SSTATUS] =3D { "sstatus", smode, read_sstatus, write_sst= atus, NULL, read_sstatus_i128 = }, diff --git a/target/riscv/machine.c b/target/riscv/machine.c index 243f567949..40e146c578 100644 --- a/target/riscv/machine.c +++ b/target/riscv/machine.c @@ -231,6 +231,26 @@ static int riscv_cpu_post_load(void *opaque, int versi= on_id) return 0; } =20 +static bool smstateen_needed(void *opaque) +{ + RISCVCPU *cpu =3D opaque; + + return cpu->cfg.ext_smstateen; +} + +static const VMStateDescription vmstate_smstateen =3D { + .name =3D "cpu/smtateen", + .version_id =3D 1, + .minimum_version_id =3D 1, + .needed =3D smstateen_needed, + .fields =3D (VMStateField[]) { + VMSTATE_UINT64_ARRAY(env.mstateen, RISCVCPU, 4), + VMSTATE_UINT64_ARRAY(env.hstateen, RISCVCPU, 4), + VMSTATE_UINT64_ARRAY(env.sstateen, RISCVCPU, 4), + VMSTATE_END_OF_LIST() + } +}; + static bool envcfg_needed(void *opaque) { RISCVCPU *cpu =3D opaque; @@ -315,6 +335,7 @@ const VMStateDescription vmstate_riscv_cpu =3D { &vmstate_rv128, &vmstate_kvmtimer, &vmstate_envcfg, + &vmstate_smstateen, NULL } }; --=20 2.17.1 From nobody Sun Feb 8 05:29:03 2026 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 ARC-Seal: i=1; a=rsa-sha256; t=1648488481; cv=none; d=zohomail.com; s=zohoarc; b=O9C3wdj1P4WGj4galxpBQyftAWxpOoVDINRRS0U57+TaTD3GuilP1BtvPN8BTSdAXCuSi32o03oqcqQAhiphxtGdkr4ttkdhdrmIRgUWFc8gGWdtSm8hGK5vxUtnJrq0PkPLbYaJfuOwD4IuqVe3DZdOTxD7V2cJvMVs+2RHvMA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1648488481; h=Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:To; bh=qKbYbS6yVnzI3TB0TfHI/CcS8OZcbf69ROzh6fJumgI=; b=buwaTV7VRfC9JUxLP2fvxfRoIkve2N5moIJP/DIleyHhnNI+EwkcscjRKsLr8b8skyrWF+/ZqHbWw8uEJk4MdSsRXZJorsXCHViNXX4PTOdRf52lUYwbVpgyrqAcTPY6MCOIoOjAUIJpfsr/CP2KQ/GFpo9rUbGT7jweNi5KHkM= 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1648488481776782.3860116900223; Mon, 28 Mar 2022 10:28:01 -0700 (PDT) Received: from localhost ([::1]:47124 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nYt9s-00062H-NK for importer@patchew.org; Mon, 28 Mar 2022 13:28:00 -0400 Received: from eggs.gnu.org ([209.51.188.92]:57752) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nYt5e-0008Eu-Ii for qemu-devel@nongnu.org; Mon, 28 Mar 2022 13:23:38 -0400 Received: from [2607:f8b0:4864:20::530] (port=44971 helo=mail-pg1-x530.google.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nYt5c-00033f-QA for qemu-devel@nongnu.org; Mon, 28 Mar 2022 13:23:38 -0400 Received: by mail-pg1-x530.google.com with SMTP id c11so12719381pgu.11 for ; Mon, 28 Mar 2022 10:23:36 -0700 (PDT) Received: from ThinkPad-T490.dc1.ventanamicro.com ([171.50.204.174]) by smtp.googlemail.com with ESMTPSA id 132-20020a62168a000000b004f40e8b3133sm17802504pfw.188.2022.03.28.10.23.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 28 Mar 2022 10:23:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ventanamicro.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=qKbYbS6yVnzI3TB0TfHI/CcS8OZcbf69ROzh6fJumgI=; b=eDS3PyDQPbGYIJApQgSWDomjbgk5TFh2akUFAVMI8u77deiIUx4YkargsGjALV3MYt 8/XYXAa6oMzNqpRzCzx6TMQYL96aCrjaxJkObwyPyOdX5QcFCj1Xf9af/VmyVhYe2M24 QS8Coe1uBhqD95yqT6Ra20u5GgwUjuDYklznl/HryfEkitfUhJMji2sSwCqUNkwVCOlk FS5RCT7NhixGDu4kmSbC0NTH9lNHrRq5KK2XQKqB5ggqdJURyTtnLM2bG6nnd/d2PDXr C1VtzUXy4q5p4Zq2xk0K6j+ykmg1jkTahmQyPn/GryeuZsBzc/xWDcaUXGxjVzKu76wI VYKA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=qKbYbS6yVnzI3TB0TfHI/CcS8OZcbf69ROzh6fJumgI=; b=Ry6Lmi+Xx5R9GwX2JyFwCu9UchQGbtLondBuldPfRqajB5t2EUNqHY9VhybMbw8Qaw WP3IzQy19wC3evHDJH1E8rGHKlm4pIIHq0DdHcPCpWUZiY4ndKgYggk+sS4dNvdplWE+ RZs6/AGLpCPCbuPo3ivtFzheUpCSLhpdpsceVWlXsnwOMLJnft0aJGRcphVIRGf44jUS BP1B+L8rB7cF0MoLk4iJQJZM5o4w5BbxhGrQ0wvenvpjk4UswWu9Fant1IDf+kwRoDo9 7MaP9lb9CuaclynN9oBRM4MdP2YZ/voztu3W+eZbpjBRUAGvJVSwvMJWUGIV5xxjvpjK X3lg== X-Gm-Message-State: AOAM531yTr6eozK7MN3MwduGqevK5xGA1p1pxnIzrIQ+yqpQNfT5Lm/b MbyOTO+ZsZgDKF68bssKln0hz1x7dc2lZIDB X-Google-Smtp-Source: ABdhPJwqY6PA//m7IFAPS1+UHbL6wybKTek3+Zbf5mKypzUDUt1xT/90mqLPxWcBxkhTv888MI4N5w== X-Received: by 2002:a65:530b:0:b0:382:b21d:82eb with SMTP id m11-20020a65530b000000b00382b21d82ebmr11064236pgq.215.1648488214268; Mon, 28 Mar 2022 10:23:34 -0700 (PDT) From: Mayuresh Chitale To: qemu-devel@nongnu.org, qemu-riscv@nongnu.org Subject: [RFC PATCH v3 2/4] target/riscv: smstateen check for h/senvcfg Date: Mon, 28 Mar 2022 22:53:17 +0530 Message-Id: <20220328172319.6802-3-mchitale@ventanamicro.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20220328172319.6802-1-mchitale@ventanamicro.com> References: <20220328172319.6802-1-mchitale@ventanamicro.com> X-Host-Lookup-Failed: Reverse DNS lookup failed for 2607:f8b0:4864:20::530 (failed) 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::530; envelope-from=mchitale@ventanamicro.com; helo=mail-pg1-x530.google.com X-Spam_score_int: -6 X-Spam_score: -0.7 X-Spam_bar: / X-Spam_report: (-0.7 / 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, PDS_HP_HELO_NORDNS=0.659, RCVD_IN_DNSWL_NONE=-0.0001, RDNS_NONE=0.793, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=no 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: , Cc: Mayuresh Chitale , alistair.francis@wdc.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: pass (identity @ventanamicro.com) X-ZM-MESSAGEID: 1648488482361100003 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Accesses to henvcfg, henvcfgh and senvcfg are allowed only if corresponding bit in mstateen0/hstateen0 is enabled. Otherwise an illegal instruction trap is generated. Signed-off-by: Mayuresh Chitale --- target/riscv/csr.c | 82 ++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 76 insertions(+), 6 deletions(-) diff --git a/target/riscv/csr.c b/target/riscv/csr.c index e3dafc37ef..dda254a6c9 100644 --- a/target/riscv/csr.c +++ b/target/riscv/csr.c @@ -37,6 +37,35 @@ void riscv_set_csr_ops(int csrno, riscv_csr_operations *= ops) } =20 /* Predicates */ +static RISCVException smstateen_acc_ok(CPURISCVState *env, int mode, int b= it) +{ + CPUState *cs =3D env_cpu(env); + RISCVCPU *cpu =3D RISCV_CPU(cs); + bool virt =3D riscv_cpu_virt_enabled(env); + + if (!cpu->cfg.ext_smstateen) { + return RISCV_EXCP_NONE; + } + + if (!(env->mstateen[0] & (1UL << bit))) { + return RISCV_EXCP_ILLEGAL_INST; + } + + if (virt) { + if (!(env->hstateen[0] & (1UL << bit))) { + return RISCV_EXCP_VIRT_INSTRUCTION_FAULT; + } + } + + if (mode =3D=3D PRV_U) { + if (!(env->sstateen[0] & (1UL << bit))) { + return RISCV_EXCP_ILLEGAL_INST; + } + } + + return RISCV_EXCP_NONE; +} + static RISCVException fs(CPURISCVState *env, int csrno) { #if !defined(CONFIG_USER_ONLY) @@ -1476,6 +1505,13 @@ static RISCVException write_menvcfgh(CPURISCVState *= env, int csrno, static RISCVException read_senvcfg(CPURISCVState *env, int csrno, target_ulong *val) { + RISCVException ret; + + ret =3D smstateen_acc_ok(env, PRV_S, SMSTATEEN0_HSENVCFG); + if (ret !=3D RISCV_EXCP_NONE) { + return ret; + } + *val =3D env->senvcfg; return RISCV_EXCP_NONE; } @@ -1484,15 +1520,27 @@ static RISCVException write_senvcfg(CPURISCVState *= env, int csrno, target_ulong val) { uint64_t mask =3D SENVCFG_FIOM | SENVCFG_CBIE | SENVCFG_CBCFE | SENVCF= G_CBZE; + RISCVException ret; =20 - env->senvcfg =3D (env->senvcfg & ~mask) | (val & mask); + ret =3D smstateen_acc_ok(env, PRV_S, SMSTATEEN0_HSENVCFG); + if (ret !=3D RISCV_EXCP_NONE) { + return ret; + } =20 + env->senvcfg =3D (env->senvcfg & ~mask) | (val & mask); return RISCV_EXCP_NONE; } =20 static RISCVException read_henvcfg(CPURISCVState *env, int csrno, target_ulong *val) { + RISCVException ret; + + ret =3D smstateen_acc_ok(env, PRV_S, SMSTATEEN0_HSENVCFG); + if (ret !=3D RISCV_EXCP_NONE) { + return ret; + } + *val =3D env->henvcfg; return RISCV_EXCP_NONE; } @@ -1501,6 +1549,12 @@ static RISCVException write_henvcfg(CPURISCVState *e= nv, int csrno, target_ulong val) { uint64_t mask =3D HENVCFG_FIOM | HENVCFG_CBIE | HENVCFG_CBCFE | HENVCF= G_CBZE; + RISCVException ret; + + ret =3D smstateen_acc_ok(env, PRV_S, SMSTATEEN0_HSENVCFG); + if (ret !=3D RISCV_EXCP_NONE) { + return ret; + } =20 if (riscv_cpu_mxl(env) =3D=3D MXL_RV64) { mask |=3D HENVCFG_PBMTE | HENVCFG_STCE; @@ -1514,6 +1568,13 @@ static RISCVException write_henvcfg(CPURISCVState *e= nv, int csrno, static RISCVException read_henvcfgh(CPURISCVState *env, int csrno, target_ulong *val) { + RISCVException ret; + + ret =3D smstateen_acc_ok(env, PRV_S, SMSTATEEN0_HSENVCFG); + if (ret !=3D RISCV_EXCP_NONE) { + return ret; + } + *val =3D env->henvcfg >> 32; return RISCV_EXCP_NONE; } @@ -1523,9 +1584,14 @@ static RISCVException write_henvcfgh(CPURISCVState *= env, int csrno, { uint64_t mask =3D HENVCFG_PBMTE | HENVCFG_STCE; uint64_t valh =3D (uint64_t)val << 32; + RISCVException ret; =20 - env->henvcfg =3D (env->henvcfg & ~mask) | (valh & mask); + ret =3D smstateen_acc_ok(env, PRV_S, SMSTATEEN0_HSENVCFG); + if (ret !=3D RISCV_EXCP_NONE) { + return ret; + } =20 + env->henvcfg =3D (env->henvcfg & ~mask) | (valh & mask); return RISCV_EXCP_NONE; } =20 @@ -1547,7 +1613,8 @@ static RISCVException write_mstateen(CPURISCVState *e= nv, int csrno, target_ulong new_val) { uint64_t *reg; - uint64_t wr_mask =3D 1UL << SMSTATEEN_STATEN; + uint64_t wr_mask =3D (1UL << SMSTATEEN_STATEN) | + (1UL << SMSTATEEN0_HSENVCFG); =20 reg =3D &env->mstateen[csrno - CSR_MSTATEEN0]; write_smstateen(env, reg, wr_mask, new_val); @@ -1568,7 +1635,8 @@ static RISCVException write_mstateenh(CPURISCVState *= env, int csrno, { uint64_t *reg; uint64_t val; - uint64_t wr_mask =3D 1UL << SMSTATEEN_STATEN; + uint64_t wr_mask =3D (1UL << SMSTATEEN_STATEN) | + (1UL << SMSTATEEN0_HSENVCFG); =20 reg =3D &env->mstateen[csrno - CSR_MSTATEEN0H]; val =3D (uint64_t)new_val << 32; @@ -1590,7 +1658,8 @@ static RISCVException write_hstateen(CPURISCVState *e= nv, int csrno, target_ulong new_val) { uint64_t *reg; - uint64_t wr_mask =3D 1UL << SMSTATEEN_STATEN; + uint64_t wr_mask =3D (1UL << SMSTATEEN_STATEN) | + (1UL << SMSTATEEN0_HSENVCFG); int index =3D csrno - CSR_HSTATEEN0; =20 reg =3D &env->hstateen[index]; @@ -1613,8 +1682,9 @@ static RISCVException write_hstateenh(CPURISCVState *= env, int csrno, { uint64_t *reg; uint64_t val; - uint64_t wr_mask =3D 1UL << SMSTATEEN_STATEN; int index =3D csrno - CSR_HSTATEEN0H; + uint64_t wr_mask =3D (1UL << SMSTATEEN_STATEN) | + (1UL << SMSTATEEN0_HSENVCFG); =20 reg =3D &env->hstateen[index]; val =3D (uint64_t)new_val << 32; --=20 2.17.1 From nobody Sun Feb 8 05:29:03 2026 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 ARC-Seal: i=1; a=rsa-sha256; t=1648488327; cv=none; d=zohomail.com; s=zohoarc; b=n5i96LVxRPyS0C89RQE6YKugwLa6nrDl1rMGPWBzoOR+F6yvTJvM4qzIzVKLriLv1NAvCM85LpTr8frwtG0d+rXWGBaq/Qg17o0oDsfRHv0WexLWDzq0kpTffQZVwvqEPkd53McLRsG0c9h5YVRK8ySD2mXN6DUf2SD7oqxG8HA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1648488327; h=Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:To; bh=L6+AqKjXtNkbK+bDI19e24j/hE2NKj1kvcBJT6uzycs=; b=YcCAzZuZK0O0pgh17oem56/8SG3G54qK+FwLMp2n2oSblC4292wjItC5i/cqvlMBSLkK3wp9BKrnuGjbS3kwGqKYHxTV7RV4EVxa094J+XdxvCV34x7MtVQbRig0GOMjpdHSiNGk8bD6LDh1yY1TecaI0TZCsmL0ZykwJwWbRjw= 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1648488327959937.1561754291561; Mon, 28 Mar 2022 10:25:27 -0700 (PDT) Received: from localhost ([::1]:41708 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nYt7O-0002M1-Pi for importer@patchew.org; Mon, 28 Mar 2022 13:25:26 -0400 Received: from eggs.gnu.org ([209.51.188.92]:57784) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nYt5g-0008MX-Tf for qemu-devel@nongnu.org; Mon, 28 Mar 2022 13:23:41 -0400 Received: from [2607:f8b0:4864:20::633] (port=42754 helo=mail-pl1-x633.google.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nYt5f-000346-9Z for qemu-devel@nongnu.org; Mon, 28 Mar 2022 13:23:40 -0400 Received: by mail-pl1-x633.google.com with SMTP id p17so15253772plo.9 for ; Mon, 28 Mar 2022 10:23:38 -0700 (PDT) Received: from ThinkPad-T490.dc1.ventanamicro.com ([171.50.204.174]) by smtp.googlemail.com with ESMTPSA id 132-20020a62168a000000b004f40e8b3133sm17802504pfw.188.2022.03.28.10.23.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 28 Mar 2022 10:23:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ventanamicro.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=L6+AqKjXtNkbK+bDI19e24j/hE2NKj1kvcBJT6uzycs=; b=YE1ASoogvsv5KlCpQX0XDaceGi6nmqRzokyuNVvmVVISpoaih84PSLupkhdrNBCg8L uDp0E2n1fYtvsSum4vDJC6MLyMbeChuw9bxfw1gA1HY7V768XpDQ3XpYoGPV5icui1ty 7cd2DqAi1PWcCXjB3zTM8MFMyv8aSNecy6OHzKitjPEzd+crVPbFvYe5vFrUq/p/3dKe zJmgzPzgePeGpzCRclDPKoPGC7h+/pDsjVN0ksPPXjsrcOcikHS7EtopPq3ioeehYEiC qozgkYLwaRE7DxSLEf2VY18jMmOaBpzKeBK7SzIUyqrM+GlEllTO+r/Cxc6Fpi2nroln I+2A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=L6+AqKjXtNkbK+bDI19e24j/hE2NKj1kvcBJT6uzycs=; b=8KBc5tKGwrdjG2DejHFXVUL+Elj32ucGbTWzn0xTa/WyTl6vQPKGAPgefdj4+zJIHN dr3dFzDXYUU/gcHe937BJqEKHky1EOlFgzPW849OjrwCu8OoB+7m8eCoMCjbxFTQ69OO HlOLJKxrzU9OdM7EMBmfWjL/l/gouqULk/QhTVIZap9PkJU1kSpdXzcO378p8E+wyySj BU+n7xRRxu5mY8fFXpYsLM51sH1K6SE6ddkGbl1gHmYwwkU0aTi1vOdiNMUwQd9udmkV jwhkqUR99ZqfkhvcDU4HfffUHR60e+B8ROYamSAKmo05qheW81xQ+uWiqgOFgrSkPdkX u2Vg== X-Gm-Message-State: AOAM530TWmtIjEayh1Aj9tbALSqc6icNukAm+mIVm/W6CT0JRsywvk8y Ib04YGPv4F8bW3nuu5db5CAs2ztAUmMqYQDr X-Google-Smtp-Source: ABdhPJxSmHshDLqNck26bx0yH8wFXvOwPr0qiEoxx/9jOq61iw1/KOzm2c63gJsuXb87ixd+QapQtQ== X-Received: by 2002:a17:902:6943:b0:156:1d3c:1e0d with SMTP id k3-20020a170902694300b001561d3c1e0dmr2744706plt.76.1648488217590; Mon, 28 Mar 2022 10:23:37 -0700 (PDT) From: Mayuresh Chitale To: qemu-devel@nongnu.org, qemu-riscv@nongnu.org Subject: [RFC PATCH v3 3/4] target/riscv: smstateen check for fcsr Date: Mon, 28 Mar 2022 22:53:18 +0530 Message-Id: <20220328172319.6802-4-mchitale@ventanamicro.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20220328172319.6802-1-mchitale@ventanamicro.com> References: <20220328172319.6802-1-mchitale@ventanamicro.com> X-Host-Lookup-Failed: Reverse DNS lookup failed for 2607:f8b0:4864:20::633 (failed) 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::633; envelope-from=mchitale@ventanamicro.com; helo=mail-pl1-x633.google.com X-Spam_score_int: -6 X-Spam_score: -0.7 X-Spam_bar: / X-Spam_report: (-0.7 / 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, PDS_HP_HELO_NORDNS=0.659, RCVD_IN_DNSWL_NONE=-0.0001, RDNS_NONE=0.793, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=no 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: , Cc: Mayuresh Chitale , alistair.francis@wdc.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: pass (identity @ventanamicro.com) X-ZM-MESSAGEID: 1648489231240100001 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" If smstateen is implemented and sstateen0.fcsr is clear then the floating point operations must return illegal instruction exception. Signed-off-by: Mayuresh Chitale --- target/riscv/csr.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/target/riscv/csr.c b/target/riscv/csr.c index dda254a6c9..658f51bd40 100644 --- a/target/riscv/csr.c +++ b/target/riscv/csr.c @@ -73,6 +73,10 @@ static RISCVException fs(CPURISCVState *env, int csrno) !RISCV_CPU(env_cpu(env))->cfg.ext_zfinx) { return RISCV_EXCP_ILLEGAL_INST; } + + if (!env->debugger && !riscv_cpu_fp_enabled(env)) { + return smstateen_acc_ok(env, PRV_U, SMSTATEEN0_FCSR); + } #endif return RISCV_EXCP_NONE; } @@ -1617,6 +1621,10 @@ static RISCVException write_mstateen(CPURISCVState *= env, int csrno, (1UL << SMSTATEEN0_HSENVCFG); =20 reg =3D &env->mstateen[csrno - CSR_MSTATEEN0]; + if (riscv_has_ext(env, RVF)) { + wr_mask |=3D 1UL << SMSTATEEN0_FCSR; + } + write_smstateen(env, reg, wr_mask, new_val); =20 return RISCV_EXCP_NONE; @@ -1641,6 +1649,10 @@ static RISCVException write_mstateenh(CPURISCVState = *env, int csrno, reg =3D &env->mstateen[csrno - CSR_MSTATEEN0H]; val =3D (uint64_t)new_val << 32; val |=3D *reg & 0xFFFFFFFF; + if (riscv_has_ext(env, RVF)) { + wr_mask |=3D 1UL << SMSTATEEN0_FCSR; + } + write_smstateen(env, reg, wr_mask, val); =20 return RISCV_EXCP_NONE; @@ -1662,6 +1674,10 @@ static RISCVException write_hstateen(CPURISCVState *= env, int csrno, (1UL << SMSTATEEN0_HSENVCFG); int index =3D csrno - CSR_HSTATEEN0; =20 + if (riscv_has_ext(env, RVF)) { + wr_mask |=3D 1UL << SMSTATEEN0_FCSR; + } + reg =3D &env->hstateen[index]; wr_mask &=3D env->mstateen[index]; write_smstateen(env, reg, wr_mask, new_val); @@ -1686,6 +1702,10 @@ static RISCVException write_hstateenh(CPURISCVState = *env, int csrno, uint64_t wr_mask =3D (1UL << SMSTATEEN_STATEN) | (1UL << SMSTATEEN0_HSENVCFG); =20 + if (riscv_has_ext(env, RVF)) { + wr_mask |=3D 1UL << SMSTATEEN0_FCSR; + } + reg =3D &env->hstateen[index]; val =3D (uint64_t)new_val << 32; val |=3D *reg & 0xFFFFFFFF; @@ -1711,6 +1731,10 @@ static RISCVException write_sstateen(CPURISCVState *= env, int csrno, int index =3D csrno - CSR_SSTATEEN0; bool virt =3D riscv_cpu_virt_enabled(env); =20 + if (riscv_has_ext(env, RVF)) { + wr_mask |=3D 1UL << SMSTATEEN0_FCSR; + } + reg =3D &env->sstateen[index]; if (virt) { wr_mask &=3D env->mstateen[index]; --=20 2.17.1 From nobody Sun Feb 8 05:29:03 2026 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 ARC-Seal: i=1; a=rsa-sha256; t=1648488479; cv=none; d=zohomail.com; s=zohoarc; b=I11Oq4hH2PsAtZOisHPUntQOaJKS9mD4z8bJfCiojXi9qbUR5geac/cig9824tdoy+GJ6Ln2VDaHdoeHLeFAL55S8a0MHnKWbjbuqzFUk01i9FQtVaK/l7dgoiDw1/zL7jiE33Z0ZRqgaNxNjSMQ5zBGrAa3w1nchMhXEWN80og= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1648488479; h=Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:To; bh=N4PhcLUgMjhVVcJ/lTuiA/1ywNDkD0qbVjch5k3Oq28=; b=aY1Gxo2khdkdBuWf4JAXtvqNpZEIb0LBLuRbLGKr74Oj6cK7bT4ayofFOYTyDF9iBm24QAGsEXHjIIiCJ8aq/4Q/Ub+6HCihIzAcWXJN/WFwmkFhLZRV+qf9j+a9f1OaXm2QXwIp3jKyXl4C036z8QppdMOvPXHzlEDKJ9gtb78= 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1648488479611502.6483769261217; Mon, 28 Mar 2022 10:27:59 -0700 (PDT) Received: from localhost ([::1]:46932 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nYt9q-0005uj-8O for importer@patchew.org; Mon, 28 Mar 2022 13:27:58 -0400 Received: from eggs.gnu.org ([209.51.188.92]:57818) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nYt5l-00006R-99 for qemu-devel@nongnu.org; Mon, 28 Mar 2022 13:23:45 -0400 Received: from [2607:f8b0:4864:20::536] (port=33573 helo=mail-pg1-x536.google.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nYt5i-00034W-Qs for qemu-devel@nongnu.org; Mon, 28 Mar 2022 13:23:44 -0400 Received: by mail-pg1-x536.google.com with SMTP id k14so12774779pga.0 for ; Mon, 28 Mar 2022 10:23:42 -0700 (PDT) Received: from ThinkPad-T490.dc1.ventanamicro.com ([171.50.204.174]) by smtp.googlemail.com with ESMTPSA id 132-20020a62168a000000b004f40e8b3133sm17802504pfw.188.2022.03.28.10.23.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 28 Mar 2022 10:23:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ventanamicro.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=N4PhcLUgMjhVVcJ/lTuiA/1ywNDkD0qbVjch5k3Oq28=; b=Ofw0X8uQhdgCRAha0Nfn0f7F56tZFQ7wRnBtjigQMDaaHiNJ3mMWG/+Dx/9bba6p7m FNlfD+iUXU8AYui+J7fBlTPvEChUMrGQFcjYQf94DbtTMn14yMTsWpnnisahVxsM/9SM PpQG+uLEHOCdXO8IoVa1IeNiW/ct5jtRPBSdbHlXQNGjDEJgWGydCVVbx0LugQ4tmiWb +VCo/zd/J8w45COiE00hEycYu9D95K6XMfgNXFzyfCEj9jgGO2OJRWAdaoI1kYbB4F3E k2Sg3uJtHwBtyeKZoZmH/sSY9HBmBVeBWstEg9a85YpkhLItEU30Nwu5XHcLOBhQeA8w PijA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=N4PhcLUgMjhVVcJ/lTuiA/1ywNDkD0qbVjch5k3Oq28=; b=tMJj4W4VjjTIb0XSBHTa+w860KEVrvIuV/OKt2KT4fpIYdpd7SRrnJQ4uWrTUWOk+n /brrLts8/2V6l5F92/0PL821CuLeSUje6EpK0/EOAHu+MDWMaj6C8h02iwphfDD2OZd+ CnJzLwcOL3RnsuWaeZ7TjgBytgq7I/BrTgF8kHZ7EJEsY00SqH6K5yX1/je6RFqwBThk /YxWc8bMb1N6v6zym+nzeu/VGR1IOsrOjL0H1DV8h51LifUBJVs8KA65z+YpIrt/Ymsn Vq0INwkjAX3YEdFxdvEaThZTvLZ72A9tdC7XQPaJNAuuuHsFHxlUR+TFkwK4u46lYdd/ e+Wg== X-Gm-Message-State: AOAM531iFKLxcAPrU4bP7nGJkMlEAkD/Vz3dI6zqAj5g8aahW8sFIwkz IPRFjgbLFP2KzXGhtatF7fU9PCn/sF0yxYAv X-Google-Smtp-Source: ABdhPJwZbYMqWzOaSbd4pba0nM5oyML5vO8O/+zD8JfdPevaZRqNhfqKWZyH74Clu736AHZkGJ0JXQ== X-Received: by 2002:a63:7502:0:b0:37f:f089:7d56 with SMTP id q2-20020a637502000000b0037ff0897d56mr10851291pgc.531.1648488221022; Mon, 28 Mar 2022 10:23:41 -0700 (PDT) From: Mayuresh Chitale To: qemu-devel@nongnu.org, qemu-riscv@nongnu.org Subject: [RFC PATCH v3 4/4] target/riscv: smstateen check for AIA/IMSIC Date: Mon, 28 Mar 2022 22:53:19 +0530 Message-Id: <20220328172319.6802-5-mchitale@ventanamicro.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20220328172319.6802-1-mchitale@ventanamicro.com> References: <20220328172319.6802-1-mchitale@ventanamicro.com> X-Host-Lookup-Failed: Reverse DNS lookup failed for 2607:f8b0:4864:20::536 (failed) 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::536; envelope-from=mchitale@ventanamicro.com; helo=mail-pg1-x536.google.com X-Spam_score_int: -6 X-Spam_score: -0.7 X-Spam_bar: / X-Spam_report: (-0.7 / 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, PDS_HP_HELO_NORDNS=0.659, RCVD_IN_DNSWL_NONE=-0.0001, RDNS_NONE=0.793, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=no 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: , Cc: Mayuresh Chitale , alistair.francis@wdc.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: pass (identity @ventanamicro.com) X-ZM-MESSAGEID: 1648488480634100001 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" If smstateen is implemented then accesses to AIA registers CSRS, IMSIC CSRs and other IMSIC registers is controlled by setting of corresponding bits in mstateen/hstateen registers. Otherwise an illegal instruction trap or virtual instruction trap is generated. Signed-off-by: Mayuresh Chitale --- target/riscv/csr.c | 248 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 246 insertions(+), 2 deletions(-) diff --git a/target/riscv/csr.c b/target/riscv/csr.c index 658f51bd40..93866fd773 100644 --- a/target/riscv/csr.c +++ b/target/riscv/csr.c @@ -66,6 +66,53 @@ static RISCVException smstateen_acc_ok(CPURISCVState *en= v, int mode, int bit) return RISCV_EXCP_NONE; } =20 +static RISCVException smstateen_aia_acc_ok(CPURISCVState *env, int csrno) +{ + int bit, mode; + + switch (csrno) { + case CSR_SSETEIPNUM: + case CSR_SCLREIPNUM: + case CSR_SSETEIENUM: + case CSR_SCLREIENUM: + case CSR_STOPEI: + case CSR_VSSETEIPNUM: + case CSR_VSCLREIPNUM: + case CSR_VSSETEIENUM: + case CSR_VSCLREIENUM: + case CSR_VSTOPEI: + case CSR_HSTATUS: + mode =3D PRV_S; + bit =3D SMSTATEEN0_IMSIC; + break; + + case CSR_SIEH: + case CSR_SIPH: + case CSR_HVIPH: + case CSR_HVICTL: + case CSR_HVIPRIO1: + case CSR_HVIPRIO2: + case CSR_HVIPRIO1H: + case CSR_HVIPRIO2H: + case CSR_VSIEH: + case CSR_VSIPH: + mode =3D PRV_S; + bit =3D SMSTATEEN0_AIA; + break; + + case CSR_SISELECT: + case CSR_VSISELECT: + mode =3D PRV_S; + bit =3D SMSTATEEN0_SVSLCT; + break; + + default: + return RISCV_EXCP_NONE; + } + + return smstateen_acc_ok(env, mode, bit); +} + static RISCVException fs(CPURISCVState *env, int csrno) { #if !defined(CONFIG_USER_ONLY) @@ -1047,6 +1094,13 @@ static int rmw_xiselect(CPURISCVState *env, int csrn= o, target_ulong *val, target_ulong new_val, target_ulong wr_mask) { target_ulong *iselect; + RISCVException ret; + + /* Check if smstateen is enabled and this access is allowed */ + ret =3D smstateen_aia_acc_ok(env, csrno); + if (ret !=3D RISCV_EXCP_NONE) { + return ret; + } =20 /* Translate CSR number for VS-mode */ csrno =3D aia_xlate_vs_csrno(env, csrno); @@ -1129,7 +1183,9 @@ static int rmw_xireg(CPURISCVState *env, int csrno, t= arget_ulong *val, bool virt; uint8_t *iprio; int ret =3D -EINVAL; - target_ulong priv, isel, vgein; + target_ulong priv, isel, vgein =3D 0; + CPUState *cs =3D env_cpu(env); + RISCVCPU *cpu =3D RISCV_CPU(cs); =20 /* Translate CSR number for VS-mode */ csrno =3D aia_xlate_vs_csrno(env, csrno); @@ -1158,11 +1214,20 @@ static int rmw_xireg(CPURISCVState *env, int csrno,= target_ulong *val, }; =20 /* Find the selected guest interrupt file */ - vgein =3D (virt) ? get_field(env->hstatus, HSTATUS_VGEIN) : 0; + if (virt) { + if (!cpu->cfg.ext_smstateen || + (env->hstateen[0] & (1UL << SMSTATEEN0_IMSIC))) { + vgein =3D get_field(env->hstatus, HSTATUS_VGEIN); + } + } =20 if (ISELECT_IPRIO0 <=3D isel && isel <=3D ISELECT_IPRIO15) { /* Local interrupt priority registers not available for VS-mode */ if (!virt) { + if (priv =3D=3D PRV_S && cpu->cfg.ext_smstateen && + !(env->hstateen[0] & (1UL << SMSTATEEN0_AIA))) { + goto done; + } ret =3D rmw_iprio(riscv_cpu_mxl_bits(env), isel, iprio, val, new_val, wr_mask, (priv =3D=3D PRV_M) ? IRQ_M_EXT : IRQ_S_EXT); @@ -1196,6 +1261,13 @@ static int rmw_xsetclreinum(CPURISCVState *env, int = csrno, target_ulong *val, int ret =3D -EINVAL; bool set, pend, virt; target_ulong priv, isel, vgein, xlen, nval, wmask; + RISCVException excp; + + /* Check if smstateen is enabled and this access is allowed */ + excp =3D smstateen_aia_acc_ok(env, csrno); + if (excp !=3D RISCV_EXCP_NONE) { + return excp; + } =20 /* Translate CSR number for VS-mode */ csrno =3D aia_xlate_vs_csrno(env, csrno); @@ -1314,6 +1386,13 @@ static int rmw_xtopei(CPURISCVState *env, int csrno,= target_ulong *val, bool virt; int ret =3D -EINVAL; target_ulong priv, vgein; + RISCVException excp; + + /* Check if smstateen is enabled and this access is allowed */ + excp =3D smstateen_aia_acc_ok(env, csrno); + if (excp !=3D RISCV_EXCP_NONE) { + return excp; + } =20 /* Translate CSR number for VS-mode */ csrno =3D aia_xlate_vs_csrno(env, csrno); @@ -1625,6 +1704,12 @@ static RISCVException write_mstateen(CPURISCVState *= env, int csrno, wr_mask |=3D 1UL << SMSTATEEN0_FCSR; } =20 + if (riscv_feature(env, RISCV_FEATURE_AIA)) { + wr_mask |=3D (1UL << SMSTATEEN0_IMSIC) | + (1UL << SMSTATEEN0_AIA) | + (1UL << SMSTATEEN0_SVSLCT); + } + write_smstateen(env, reg, wr_mask, new_val); =20 return RISCV_EXCP_NONE; @@ -1653,6 +1738,12 @@ static RISCVException write_mstateenh(CPURISCVState = *env, int csrno, wr_mask |=3D 1UL << SMSTATEEN0_FCSR; } =20 + if (riscv_feature(env, RISCV_FEATURE_AIA)) { + wr_mask |=3D (1UL << SMSTATEEN0_IMSIC) | + (1UL << SMSTATEEN0_AIA) | + (1UL << SMSTATEEN0_SVSLCT); + } + write_smstateen(env, reg, wr_mask, val); =20 return RISCV_EXCP_NONE; @@ -1678,6 +1769,12 @@ static RISCVException write_hstateen(CPURISCVState *= env, int csrno, wr_mask |=3D 1UL << SMSTATEEN0_FCSR; } =20 + if (riscv_feature(env, RISCV_FEATURE_AIA)) { + wr_mask |=3D (1UL << SMSTATEEN0_IMSIC) | + (1UL << SMSTATEEN0_AIA) | + (1UL << SMSTATEEN0_SVSLCT); + } + reg =3D &env->hstateen[index]; wr_mask &=3D env->mstateen[index]; write_smstateen(env, reg, wr_mask, new_val); @@ -1706,6 +1803,12 @@ static RISCVException write_hstateenh(CPURISCVState = *env, int csrno, wr_mask |=3D 1UL << SMSTATEEN0_FCSR; } =20 + if (riscv_feature(env, RISCV_FEATURE_AIA)) { + wr_mask |=3D (1UL << SMSTATEEN0_IMSIC) | + (1UL << SMSTATEEN0_AIA) | + (1UL << SMSTATEEN0_SVSLCT); + } + reg =3D &env->hstateen[index]; val =3D (uint64_t)new_val << 32; val |=3D *reg & 0xFFFFFFFF; @@ -1892,6 +1995,12 @@ static RISCVException rmw_vsieh(CPURISCVState *env, = int csrno, uint64_t rval; RISCVException ret; =20 + /* Check if smstateen is enabled and this access is allowed */ + ret =3D smstateen_aia_acc_ok(env, csrno); + if (ret !=3D RISCV_EXCP_NONE) { + return ret; + } + ret =3D rmw_vsie64(env, csrno, &rval, ((uint64_t)new_val) << 32, ((uint64_t)wr_mask) << 32); if (ret_val) { @@ -1946,6 +2055,12 @@ static RISCVException rmw_sieh(CPURISCVState *env, i= nt csrno, uint64_t rval; RISCVException ret; =20 + /* Check if smstateen is enabled and this access is allowed */ + ret =3D smstateen_aia_acc_ok(env, csrno); + if (ret !=3D RISCV_EXCP_NONE) { + return ret; + } + ret =3D rmw_sie64(env, csrno, &rval, ((uint64_t)new_val) << 32, ((uint64_t)wr_mask) << 32); if (ret_val) { @@ -2108,6 +2223,12 @@ static RISCVException rmw_vsiph(CPURISCVState *env, = int csrno, uint64_t rval; RISCVException ret; =20 + /* Check if smstateen is enabled and this access is allowed */ + ret =3D smstateen_aia_acc_ok(env, csrno); + if (ret !=3D RISCV_EXCP_NONE) { + return ret; + } + ret =3D rmw_vsip64(env, csrno, &rval, ((uint64_t)new_val) << 32, ((uint64_t)wr_mask) << 32); if (ret_val) { @@ -2162,6 +2283,12 @@ static RISCVException rmw_siph(CPURISCVState *env, i= nt csrno, uint64_t rval; RISCVException ret; =20 + /* Check if smstateen is enabled and this access is allowed */ + ret =3D smstateen_aia_acc_ok(env, csrno); + if (ret !=3D RISCV_EXCP_NONE) { + return ret; + } + ret =3D rmw_sip64(env, csrno, &rval, ((uint64_t)new_val) << 32, ((uint64_t)wr_mask) << 32); if (ret_val) { @@ -2352,6 +2479,10 @@ static RISCVException read_hstatus(CPURISCVState *en= v, int csrno, static RISCVException write_hstatus(CPURISCVState *env, int csrno, target_ulong val) { + if (smstateen_aia_acc_ok(env, csrno) !=3D RISCV_EXCP_NONE) { + val &=3D ~HSTATUS_VGEIN; + } + env->hstatus =3D val; if (riscv_cpu_mxl(env) !=3D MXL_RV32 && get_field(val, HSTATUS_VSXL) != =3D 2) { qemu_log_mask(LOG_UNIMP, "QEMU does not support mixed HSXLEN optio= ns."); @@ -2412,6 +2543,12 @@ static RISCVException rmw_hidelegh(CPURISCVState *en= v, int csrno, uint64_t rval; RISCVException ret; =20 + /* Check if smstateen is enabled and this access is allowed */ + ret =3D smstateen_aia_acc_ok(env, csrno); + if (ret !=3D RISCV_EXCP_NONE) { + return ret; + } + ret =3D rmw_hideleg64(env, csrno, &rval, ((uint64_t)new_val) << 32, ((uint64_t)wr_mask) << 32); if (ret_val) { @@ -2458,6 +2595,12 @@ static RISCVException rmw_hviph(CPURISCVState *env, = int csrno, uint64_t rval; RISCVException ret; =20 + /* Check if smstateen is enabled and this access is allowed */ + ret =3D smstateen_aia_acc_ok(env, csrno); + if (ret !=3D RISCV_EXCP_NONE) { + return ret; + } + ret =3D rmw_hvip64(env, csrno, &rval, ((uint64_t)new_val) << 32, ((uint64_t)wr_mask) << 32); if (ret_val) { @@ -2512,6 +2655,13 @@ static RISCVException write_hcounteren(CPURISCVState= *env, int csrno, static RISCVException read_hgeie(CPURISCVState *env, int csrno, target_ulong *val) { + RISCVException ret; + + ret =3D smstateen_acc_ok(env, PRV_S, SMSTATEEN0_IMSIC); + if (ret !=3D RISCV_EXCP_NONE) { + return ret; + } + if (val) { *val =3D env->hgeie; } @@ -2521,6 +2671,13 @@ static RISCVException read_hgeie(CPURISCVState *env,= int csrno, static RISCVException write_hgeie(CPURISCVState *env, int csrno, target_ulong val) { + RISCVException ret; + + ret =3D smstateen_acc_ok(env, PRV_S, SMSTATEEN0_IMSIC); + if (ret !=3D RISCV_EXCP_NONE) { + return ret; + } + /* Only GEILEN:1 bits implemented and BIT0 is never implemented */ val &=3D ((((target_ulong)1) << env->geilen) - 1) << 1; env->hgeie =3D val; @@ -2560,6 +2717,13 @@ static RISCVException write_htinst(CPURISCVState *en= v, int csrno, static RISCVException read_hgeip(CPURISCVState *env, int csrno, target_ulong *val) { + RISCVException ret; + + ret =3D smstateen_acc_ok(env, PRV_S, SMSTATEEN0_IMSIC); + if (ret !=3D RISCV_EXCP_NONE) { + return ret; + } + if (val) { *val =3D env->hgeip; } @@ -2630,12 +2794,28 @@ static RISCVException write_htimedeltah(CPURISCVSta= te *env, int csrno, =20 static int read_hvictl(CPURISCVState *env, int csrno, target_ulong *val) { + RISCVException ret; + + /* Check if smstateen is enabled and this access is allowed */ + ret =3D smstateen_aia_acc_ok(env, csrno); + if (ret !=3D RISCV_EXCP_NONE) { + return ret; + } + *val =3D env->hvictl; return RISCV_EXCP_NONE; } =20 static int write_hvictl(CPURISCVState *env, int csrno, target_ulong val) { + RISCVException ret =3D RISCV_EXCP_NONE; + + /* Check if smstateen is enabled and this access is allowed */ + ret =3D smstateen_aia_acc_ok(env, csrno); + if (ret !=3D RISCV_EXCP_NONE) { + return ret; + } + env->hvictl =3D val & HVICTL_VALID_MASK; return RISCV_EXCP_NONE; } @@ -2694,41 +2874,105 @@ static int write_hvipriox(CPURISCVState *env, int = first_index, =20 static int read_hviprio1(CPURISCVState *env, int csrno, target_ulong *val) { + RISCVException ret; + + /* Check if smstateen is enabled and this access is allowed */ + ret =3D smstateen_aia_acc_ok(env, csrno); + if (ret !=3D RISCV_EXCP_NONE) { + return ret; + } + return read_hvipriox(env, 0, env->hviprio, val); } =20 static int write_hviprio1(CPURISCVState *env, int csrno, target_ulong val) { + RISCVException ret; + + /* Check if smstateen is enabled and this access is allowed */ + ret =3D smstateen_aia_acc_ok(env, csrno); + if (ret !=3D RISCV_EXCP_NONE) { + return ret; + } + return write_hvipriox(env, 0, env->hviprio, val); } =20 static int read_hviprio1h(CPURISCVState *env, int csrno, target_ulong *val) { + RISCVException ret; + + /* Check if smstateen is enabled and this access is allowed */ + ret =3D smstateen_aia_acc_ok(env, csrno); + if (ret !=3D RISCV_EXCP_NONE) { + return ret; + } + return read_hvipriox(env, 4, env->hviprio, val); } =20 static int write_hviprio1h(CPURISCVState *env, int csrno, target_ulong val) { + RISCVException ret; + + /* Check if smstateen is enabled and this access is allowed */ + ret =3D smstateen_aia_acc_ok(env, csrno); + if (ret !=3D RISCV_EXCP_NONE) { + return ret; + } + return write_hvipriox(env, 4, env->hviprio, val); } =20 static int read_hviprio2(CPURISCVState *env, int csrno, target_ulong *val) { + RISCVException ret; + + /* Check if smstateen is enabled and this access is allowed */ + ret =3D smstateen_aia_acc_ok(env, csrno); + if (ret !=3D RISCV_EXCP_NONE) { + return ret; + } + return read_hvipriox(env, 8, env->hviprio, val); } =20 static int write_hviprio2(CPURISCVState *env, int csrno, target_ulong val) { + RISCVException ret; + + /* Check if smstateen is enabled and this access is allowed */ + ret =3D smstateen_aia_acc_ok(env, csrno); + if (ret !=3D RISCV_EXCP_NONE) { + return ret; + } + return write_hvipriox(env, 8, env->hviprio, val); } =20 static int read_hviprio2h(CPURISCVState *env, int csrno, target_ulong *val) { + RISCVException ret; + + /* Check if smstateen is enabled and this access is allowed */ + ret =3D smstateen_aia_acc_ok(env, csrno); + if (ret !=3D RISCV_EXCP_NONE) { + return ret; + } + return read_hvipriox(env, 12, env->hviprio, val); } =20 static int write_hviprio2h(CPURISCVState *env, int csrno, target_ulong val) { + RISCVException ret; + + /* Check if smstateen is enabled and this access is allowed */ + ret =3D smstateen_aia_acc_ok(env, csrno); + if (ret !=3D RISCV_EXCP_NONE) { + return ret; + } + return write_hvipriox(env, 12, env->hviprio, val); } =20 --=20 2.17.1