From patchwork Thu Feb 1 14:00:43 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexey Baturo X-Patchwork-Id: 13541098 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 603D6C4828F for ; Thu, 1 Feb 2024 14:02:31 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rVXd6-0000Tc-Ae; Thu, 01 Feb 2024 09:01:24 -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 1rVXcu-0000R4-HP; Thu, 01 Feb 2024 09:01:13 -0500 Received: from mail-ej1-x62f.google.com ([2a00:1450:4864:20::62f]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1rVXcq-0005Kb-HB; Thu, 01 Feb 2024 09:01:11 -0500 Received: by mail-ej1-x62f.google.com with SMTP id a640c23a62f3a-a3604697d63so139452966b.3; Thu, 01 Feb 2024 06:01:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1706796064; x=1707400864; 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=zI+B6EStSuV94gCkpvGehoEE7EpYrh+cPa9jogn6Uac=; b=k51yrmyayEVV+ZPDqfIJVNmTB3fyuoDp9/8P15yL2qGSjteOoWsMgJb/HEA/bG/VRt 6ObWV0gMzGzcboXQZ6iaUs481B7C7ouvVq+CWCkCXTec4gie7HmmQpTeK0sCderUelzl +y5oHQhg0NGX+FJbl3ZOiyB/9OATG1RCyDb/HQuv4LbZron263KzRdXkyqmb5RHqDQRG FRJfn2HdXLVTG2uM+bsddEJ8Q1L078NNe+JAtR3dvE0HSTLim2s71CtWAAWa+l+4KHHW 9S94f40ZygZZ2XQTx/Cn+ioYmDXim0k8HKrvRHpTB3YjajNtGPtmyuNTZeAt1rNP7MzB 6iag== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706796064; x=1707400864; 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=zI+B6EStSuV94gCkpvGehoEE7EpYrh+cPa9jogn6Uac=; b=ZbjEdAvQc6Fxhls0gu74IhK/uQUxn4FDZLZXPktw3LNzVqCTU6ImP7aBrFg0WDCIgp OZQQXVB4CWde1xo189LZHPbTrb/Uf1CsnZ0dsuqyz5BC87vbnwdxkW7swlJQ1Rs3WV/e C2BbOpW7BzB1kLvCzCfjcyowR6EzO1z6Oj/WnScb4RXHVXz8fEA+wYg8SVJCforfqzJF 5uRQ40a4eZQImx3iCLa3fitzB/fnwc2mu1bLM86AXKf5RV2ooyfXJKL6pu2A9QNx7jhl 4LHXjNuCMbhyuaBoGUU9CFizGwseoVxCW/dx5mkjzzWbMePU2qLjdrWGiGcPNsdyZveY KdOQ== X-Gm-Message-State: AOJu0YybQpx4mA6JZvBel1Uwf9X/Dcx/SzCC1VaLqeHm31wL5BTTFvFY XF6pvlbAVmuSUfC3kiAC4NwIqmhhyWIW4/ke+UwRrGNS+ZSFn/TAWJ/grWMgE04Wdw== X-Google-Smtp-Source: AGHT+IERUgV0vFqvFtg0onO2G+MHvEDSNIyVBzifgX2vGVyMc/VAc3ne0gReN3k56yi4avRaipJWnA== X-Received: by 2002:a17:907:171b:b0:a35:9078:12e with SMTP id le27-20020a170907171b00b00a359078012emr3509497ejc.63.1706796064472; Thu, 01 Feb 2024 06:01:04 -0800 (PST) X-Forwarded-Encrypted: i=0; AJvYcCV8eLANmIvxWYQwYlbOPjpUZVY4Hq1nkh0w437w3bIzz9tXHn41oN4p+fadmxdxfZvDJoCcQWpdutfYDNKyxTbvziRzSPAfmrmNOZ/toBUPd8InqSwXhNGVPOGCqGuQbbsXN9b3HtSekoPqVgSDxsHrV/TlmCaZ+8+L7oG47QwtHinApus4c89/nHtl89T3mK2qvPDvhda53i3g5HPiz8HeJxXq62rpDm9NBGmyar/CBszpqR5xS94tDrqNIr7nKXQLEHLpv82TKs0O2/+mBMqw/OHPOqTTNU3fUX0OJguQH42h4QCKDH+/+woF9yWLuCyqC/1JAeys0EUUsSoS5TOwb2zOz7CgJjX4ngdQlv9bRVabKM9GgpmJGaw= Received: from freya.midgard (broadband-188-255-126-251.ip.moscow.rt.ru. [188.255.126.251]) by smtp.gmail.com with ESMTPSA id un6-20020a170907cb8600b00a36a7f0f087sm902728ejc.222.2024.02.01.06.01.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 01 Feb 2024 06:01:03 -0800 (PST) From: Alexey Baturo X-Google-Original-From: Alexey Baturo To: Cc: baturo.alexey@gmail.com, richard.henderson@linaro.org, zhiwei_liu@linux.alibaba.com, palmer@dabbelt.com, Alistair.Francis@wdc.com, sagark@eecs.berkeley.edu, kbastian@mail.uni-paderborn.de, qemu-devel@nongnu.org, qemu-riscv@nongnu.org, Alistair Francis Subject: [PATCH v6 2/6] target/riscv: Add new CSR fields for S{sn, mn, m}pm extensions as part of Zjpm v0.8 Date: Thu, 1 Feb 2024 14:00:43 +0000 Message-Id: <20240201140047.3456114-3-me@deliversmonkey.space> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240201140047.3456114-1-me@deliversmonkey.space> References: <20240201140047.3456114-1-me@deliversmonkey.space> MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::62f; envelope-from=baturo.alexey@gmail.com; helo=mail-ej1-x62f.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, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 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: Alexey Baturo Signed-off-by: Alexey Baturo Reviewed-by: Alistair Francis --- target/riscv/cpu.h | 8 ++++++++ target/riscv/cpu_bits.h | 3 +++ target/riscv/cpu_cfg.h | 3 +++ target/riscv/csr.c | 11 +++++++++++ target/riscv/machine.c | 10 +++++++--- target/riscv/pmp.c | 13 ++++++++++--- target/riscv/pmp.h | 11 ++++++----- 7 files changed, 48 insertions(+), 11 deletions(-) diff --git a/target/riscv/cpu.h b/target/riscv/cpu.h index dfa80bf24d..e03ff3003f 100644 --- a/target/riscv/cpu.h +++ b/target/riscv/cpu.h @@ -122,6 +122,14 @@ typedef enum { EXT_STATUS_DIRTY, } RISCVExtStatus; +/* Enum holds PMM field values for Zjpm v0.8 extension */ +typedef enum { + PMM_FIELD_DISABLED = 0, + PMM_FIELD_RESERVED = 1, + PMM_FIELD_PMLEN7 = 2, + PMM_FIELD_PMLEN16 = 3, +} RISCVPmPmm; + #define MMU_USER_IDX 3 #define MAX_RISCV_PMPS (16) diff --git a/target/riscv/cpu_bits.h b/target/riscv/cpu_bits.h index 4d8e7ea0d5..5b7ab71e5f 100644 --- a/target/riscv/cpu_bits.h +++ b/target/riscv/cpu_bits.h @@ -716,6 +716,7 @@ typedef enum RISCVException { #define MENVCFG_CBIE (3UL << 4) #define MENVCFG_CBCFE BIT(6) #define MENVCFG_CBZE BIT(7) +#define MENVCFG_PMM (3ULL << 32) #define MENVCFG_ADUE (1ULL << 61) #define MENVCFG_PBMTE (1ULL << 62) #define MENVCFG_STCE (1ULL << 63) @@ -729,11 +730,13 @@ typedef enum RISCVException { #define SENVCFG_CBIE MENVCFG_CBIE #define SENVCFG_CBCFE MENVCFG_CBCFE #define SENVCFG_CBZE MENVCFG_CBZE +#define SENVCFG_PMM MENVCFG_PMM #define HENVCFG_FIOM MENVCFG_FIOM #define HENVCFG_CBIE MENVCFG_CBIE #define HENVCFG_CBCFE MENVCFG_CBCFE #define HENVCFG_CBZE MENVCFG_CBZE +#define HENVCFG_PMM MENVCFG_PMM #define HENVCFG_ADUE MENVCFG_ADUE #define HENVCFG_PBMTE MENVCFG_PBMTE #define HENVCFG_STCE MENVCFG_STCE diff --git a/target/riscv/cpu_cfg.h b/target/riscv/cpu_cfg.h index e241922f89..9e5a089894 100644 --- a/target/riscv/cpu_cfg.h +++ b/target/riscv/cpu_cfg.h @@ -115,6 +115,9 @@ struct RISCVCPUConfig { bool ext_ssaia; bool ext_sscofpmf; bool ext_smepmp; + bool ext_ssnpm; + bool ext_smnpm; + bool ext_smmpm; bool rvv_ta_all_1s; bool rvv_ma_all_1s; bool svade; diff --git a/target/riscv/csr.c b/target/riscv/csr.c index 4166cf79a9..07f05906b5 100644 --- a/target/riscv/csr.c +++ b/target/riscv/csr.c @@ -530,6 +530,9 @@ static RISCVException have_mseccfg(CPURISCVState *env, int csrno) if (riscv_cpu_cfg(env)->ext_zkr) { return RISCV_EXCP_NONE; } + if (riscv_cpu_cfg(env)->ext_smmpm) { + return RISCV_EXCP_NONE; + } return RISCV_EXCP_ILLEGAL_INST; } @@ -2036,6 +2039,10 @@ static RISCVException write_menvcfg(CPURISCVState *env, int csrno, (cfg->ext_sstc ? MENVCFG_STCE : 0) | (cfg->ext_svadu ? MENVCFG_ADUE : 0); } + /* Update PMM field only if the value is valid according to Zjpm v0.8 */ + if (((val & MENVCFG_PMM) >> 32) != PMM_FIELD_RESERVED) { + mask |= MENVCFG_PMM; + } env->menvcfg = (env->menvcfg & ~mask) | (val & mask); return RISCV_EXCP_NONE; @@ -2080,6 +2087,10 @@ static RISCVException write_senvcfg(CPURISCVState *env, int csrno, target_ulong val) { uint64_t mask = SENVCFG_FIOM | SENVCFG_CBIE | SENVCFG_CBCFE | SENVCFG_CBZE; + /* Update PMM field only if the value is valid according to Zjpm v0.8 */ + if (((val & SENVCFG_PMM) >> 32) != PMM_FIELD_RESERVED) { + mask |= SENVCFG_PMM; + } RISCVException ret; ret = smstateen_acc_ok(env, 0, SMSTATEEN0_HSENVCFG); diff --git a/target/riscv/machine.c b/target/riscv/machine.c index e0878af655..7ae12d0d72 100644 --- a/target/riscv/machine.c +++ b/target/riscv/machine.c @@ -152,15 +152,19 @@ static const VMStateDescription vmstate_vector = { static bool pointermasking_needed(void *opaque) { - return false; + RISCVCPU *cpu = opaque; + return cpu->cfg.ext_ssnpm || cpu->cfg.ext_smnpm || cpu->cfg.ext_smmpm; } static const VMStateDescription vmstate_pointermasking = { .name = "cpu/pointer_masking", - .version_id = 1, - .minimum_version_id = 1, + .version_id = 2, + .minimum_version_id = 2, .needed = pointermasking_needed, .fields = (VMStateField[]) { + VMSTATE_UINTTL(env.mseccfg, RISCVCPU), + VMSTATE_UINTTL(env.senvcfg, RISCVCPU), + VMSTATE_UINTTL(env.menvcfg, RISCVCPU), VMSTATE_END_OF_LIST() } }; diff --git a/target/riscv/pmp.c b/target/riscv/pmp.c index 2a76b611a0..7ddb9dbf0b 100644 --- a/target/riscv/pmp.c +++ b/target/riscv/pmp.c @@ -574,6 +574,12 @@ target_ulong pmpaddr_csr_read(CPURISCVState *env, uint32_t addr_index) void mseccfg_csr_write(CPURISCVState *env, target_ulong val) { int i; + uint64_t mask = MSECCFG_MMWP | MSECCFG_MML; + + /* Update PMM field only if the value is valid according to Zjpm v0.8 */ + if (((val & MSECCFG_PMM) >> 32) != PMM_FIELD_RESERVED) { + mask |= MSECCFG_PMM; + } trace_mseccfg_csr_write(env->mhartid, val); @@ -589,12 +595,13 @@ void mseccfg_csr_write(CPURISCVState *env, target_ulong val) if (riscv_cpu_cfg(env)->ext_smepmp) { /* Sticky bits */ - val |= (env->mseccfg & (MSECCFG_MMWP | MSECCFG_MML)); - if ((val ^ env->mseccfg) & (MSECCFG_MMWP | MSECCFG_MML)) { + val |= (env->mseccfg & mask); + if ((val ^ env->mseccfg) & mask) { tlb_flush(env_cpu(env)); } } else { - val &= ~(MSECCFG_MMWP | MSECCFG_MML | MSECCFG_RLB); + mask |= MSECCFG_RLB; + val &= ~(mask); } env->mseccfg = val; diff --git a/target/riscv/pmp.h b/target/riscv/pmp.h index f5c10ce85c..ccff0eb9b6 100644 --- a/target/riscv/pmp.h +++ b/target/riscv/pmp.h @@ -40,11 +40,12 @@ typedef enum { } pmp_am_t; typedef enum { - MSECCFG_MML = 1 << 0, - MSECCFG_MMWP = 1 << 1, - MSECCFG_RLB = 1 << 2, - MSECCFG_USEED = 1 << 8, - MSECCFG_SSEED = 1 << 9 + MSECCFG_MML = 1 << 0, + MSECCFG_MMWP = 1 << 1, + MSECCFG_RLB = 1 << 2, + MSECCFG_USEED = 1 << 8, + MSECCFG_SSEED = 1 << 9, + MSECCFG_PMM = 3UL << 32, } mseccfg_field_t; typedef struct {