From nobody Sun Nov 24 08:21:27 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1724801008; cv=none; d=zohomail.com; s=zohoarc; b=AmLOV9QrnWPqnqNcq9SaRQFyrR6RfFrGDN20zxehh//U8xhvllWe1GHGTDZR97fSk4xRbTuowlbEK7jXas4YaW4qchT6Mo9f6IyHxMkiWGO1O+JppIvDIG8Mx16LEzpQNmLACeiHtomkx82Z0DgFXZzz4tLCgb0CV/U0lEHutXU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1724801008; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=xt8tPHRhEsX6UWlxFxKrwKdVADrkbOwbkq9Tk+hkRaE=; b=S42ahyU4U8HAqVEnrqnyDq9aWhYQuSn5NAaxm4lLxqMkhZTsYa4FoLEd53VR+rmhehEFetBQSN1ZkM3kONjcwzOQ9K2BBx7a+XvYVAiLvUSR9BUwGyAKrxT5aBKu+2S1D6Oy7FcvGvRD9uDj841x5gHyS0W5Sg88Npf/VOt+dGo= 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 1724801008345912.5754934361474; Tue, 27 Aug 2024 16:23:28 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sj5TT-00049q-52; Tue, 27 Aug 2024 19:19:43 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sj5TH-0003Nq-51 for qemu-devel@nongnu.org; Tue, 27 Aug 2024 19:19:32 -0400 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 1sj5TD-0000te-Bt for qemu-devel@nongnu.org; Tue, 27 Aug 2024 19:19:30 -0400 Received: by mail-pf1-x42c.google.com with SMTP id d2e1a72fcca58-7142e002aceso4986525b3a.2 for ; Tue, 27 Aug 2024 16:19:27 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-203b0ee6179sm57785155ad.92.2024.08.27.16.19.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 27 Aug 2024 16:19:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1724800766; x=1725405566; 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=xt8tPHRhEsX6UWlxFxKrwKdVADrkbOwbkq9Tk+hkRaE=; b=abdTMVVmTHe58YNhreEDGoP8uLoumX1yqUQR88hBqGEazX4wEIdQXEADmXr6w+mTPz 4L3NUpqfzCHgztVsirJZ05JGDhNWAar89iRsM0X35G1jlU8S6SDknpelgraD8N8locL4 qr/UvghZtuZwxBYyuU4U12OtwcHZFD2SJhY/MKJ9TCZuOPyVmpMOyWCmi4Z2Gd/CU64D lsecMDj0ipGkktZygL1DRjBpzvagHqkCNVzIwSKQiPB1/sbBNHwIl3iXTAEQy5Cc8xbb jSz2Th5NHlcEgvkMpKe1YPHJ/w07w1BnDMgCo/LQFYpFAFkgQdm9poefPyKdR8BZf4HJ 9MRg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724800766; x=1725405566; 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=xt8tPHRhEsX6UWlxFxKrwKdVADrkbOwbkq9Tk+hkRaE=; b=a/X9tDNI2B1Kmx7FwhcvylfphbANSnhjPxS7dqSn7WKD4RS7YoWSFAKqg1DAMVEUuv GSnwEHA/QA1EgH8nXom+Nt55oXySFUxgol2WIk8ya3KnD0O9iyVt2IoY1MZLJ97D6xKJ x6mQ3PAAh/itDFmCKwY9R6cNcZot0Btwq8W+J76NnYA82qB1CVsTGiqpaAzytB9IenTA JSCe8SYLhElFYu1wDlZyEXtVQYtEET8UayP1v2p7rlvp1uOsIyjaX4lwwImsAJmoNqKA ORD12S+Sv60y+XGFcxBcZKrD2Wg6dmPNen7OXVPWdV+GW1V8zUzPfPYPDOzaEBo9c5st VEiQ== X-Forwarded-Encrypted: i=1; AJvYcCU3pvsCXyKZ04NBOMyLVfRvO8tB5Wkoz2NHCo5JH6R7v8C0PspNggqAj/f6GA/9NoBHyhZ4utGLVQRZ@nongnu.org X-Gm-Message-State: AOJu0YxjxySgPF49bfVooKLoGat9cmepO0cnC68zFSjbfjI/ob0BcPYd 5tyYtPRJPf4g7dPzRgpokqgUcx+5gX1Db3kMuIUYzyLPRjQn9B1H2Gqo4edg5N0= X-Google-Smtp-Source: AGHT+IE/4szEjw+xQwOCxzJvozXaViTWIrmUBqnZTRQgtoBAu7rfppQr74iONlw2a8KEUnYBDpnLTg== X-Received: by 2002:a05:6a20:9f91:b0:1cc:c27e:bdee with SMTP id adf61e73a8af0-1ccd286f4acmr113388637.2.1724800765863; Tue, 27 Aug 2024 16:19:25 -0700 (PDT) From: Deepak Gupta To: qemu-riscv@nongnu.org, qemu-devel@nongnu.org Cc: palmer@dabbelt.com, Alistair.Francis@wdc.com, laurent@vivier.eu, bmeng.cn@gmail.com, liwei1518@gmail.com, dbarboza@ventanamicro.com, zhiwei_liu@linux.alibaba.com, jim.shu@sifive.com, andy.chiu@sifive.com, kito.cheng@sifive.com, Deepak Gupta , Richard Henderson Subject: [PATCH v10 12/21] target/riscv: introduce ssp and enabling controls for zicfiss Date: Tue, 27 Aug 2024 16:18:56 -0700 Message-ID: <20240827231906.553327-13-debug@rivosinc.com> X-Mailer: git-send-email 2.45.0 In-Reply-To: <20240827231906.553327-1-debug@rivosinc.com> References: <20240827231906.553327-1-debug@rivosinc.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::42c; envelope-from=debug@rivosinc.com; helo=mail-pf1-x42c.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, 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+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @rivosinc-com.20230601.gappssmtp.com) X-ZM-MESSAGEID: 1724801009232116600 Content-Type: text/plain; charset="utf-8" zicfiss introduces a new state ssp ("shadow stack register") in cpu. ssp is expressed as a new unprivileged csr (CSR_SSP=3D0x11) and holds virtual address for shadow stack as programmed by software. Shadow stack (for each mode) is enabled via bit3 in *envcfg CSRs. Shadow stack can be enabled for a mode only if it's higher privileged mode had it enabled for itself. M mode doesn't need enabling control, it's always available if extension is available on cpu. This patch also implements helper bcfi function which determines if bcfi is enabled at current privilege or not. qemu-user also gets field `ubcfien` indicating whether qemu user has shadow stack enabled or not. Adds ssp to migration state as well. Signed-off-by: Deepak Gupta Co-developed-by: Jim Shu Co-developed-by: Andy Chiu Reviewed-by: Richard Henderson --- target/riscv/cpu.c | 3 +++ target/riscv/cpu.h | 3 +++ target/riscv/cpu_bits.h | 6 +++++ target/riscv/cpu_helper.c | 27 ++++++++++++++++++++ target/riscv/csr.c | 52 +++++++++++++++++++++++++++++++++++++++ target/riscv/machine.c | 19 ++++++++++++++ 6 files changed, 110 insertions(+) diff --git a/target/riscv/cpu.c b/target/riscv/cpu.c index 0dab110a3f..d6cdd81fd8 100644 --- a/target/riscv/cpu.c +++ b/target/riscv/cpu.c @@ -998,6 +998,9 @@ static void riscv_cpu_reset_hold(Object *obj, ResetType= type) /* on reset elp is clear */ env->elp =3D false; =20 + /* on reset ssp is set to 0 */ + env->ssp =3D 0; + /* * Bits 10, 6, 2 and 12 of mideleg are read only 1 when the Hypervisor * extension is enabled. diff --git a/target/riscv/cpu.h b/target/riscv/cpu.h index f372a4074b..4ace54a2eb 100644 --- a/target/riscv/cpu.h +++ b/target/riscv/cpu.h @@ -224,6 +224,8 @@ struct CPUArchState { =20 /* elp state for zicfilp extension */ bool elp; + /* shadow stack register for zicfiss extension */ + target_ulong ssp; /* sw check code for sw check exception */ target_ulong sw_check_code; #ifdef CONFIG_USER_ONLY @@ -534,6 +536,7 @@ bool riscv_cpu_vector_enabled(CPURISCVState *env); void riscv_cpu_set_virt_enabled(CPURISCVState *env, bool enable); int riscv_env_mmu_index(CPURISCVState *env, bool ifetch); bool cpu_get_fcfien(CPURISCVState *env); +bool cpu_get_bcfien(CPURISCVState *env); G_NORETURN void riscv_cpu_do_unaligned_access(CPUState *cs, vaddr addr, MMUAccessType access_type, int mmu_idx, uintptr_t reta= ddr); diff --git a/target/riscv/cpu_bits.h b/target/riscv/cpu_bits.h index 900769ce60..48ce24dc32 100644 --- a/target/riscv/cpu_bits.h +++ b/target/riscv/cpu_bits.h @@ -34,6 +34,9 @@ =20 /* Control and Status Registers */ =20 +/* zicfiss user ssp csr */ +#define CSR_SSP 0x011 + /* User Trap Setup */ #define CSR_USTATUS 0x000 #define CSR_UIE 0x004 @@ -754,6 +757,7 @@ typedef enum RISCVException { /* Execution environment configuration bits */ #define MENVCFG_FIOM BIT(0) #define MENVCFG_LPE BIT(2) /* zicfilp */ +#define MENVCFG_SSE BIT(3) /* zicfiss */ #define MENVCFG_CBIE (3UL << 4) #define MENVCFG_CBCFE BIT(6) #define MENVCFG_CBZE BIT(7) @@ -768,12 +772,14 @@ typedef enum RISCVException { =20 #define SENVCFG_FIOM MENVCFG_FIOM #define SENVCFG_LPE MENVCFG_LPE +#define SENVCFG_SSE MENVCFG_SSE #define SENVCFG_CBIE MENVCFG_CBIE #define SENVCFG_CBCFE MENVCFG_CBCFE #define SENVCFG_CBZE MENVCFG_CBZE =20 #define HENVCFG_FIOM MENVCFG_FIOM #define HENVCFG_LPE MENVCFG_LPE +#define HENVCFG_SSE MENVCFG_SSE #define HENVCFG_CBIE MENVCFG_CBIE #define HENVCFG_CBCFE MENVCFG_CBCFE #define HENVCFG_CBZE MENVCFG_CBZE diff --git a/target/riscv/cpu_helper.c b/target/riscv/cpu_helper.c index c3820eff8f..f7e97eabfa 100644 --- a/target/riscv/cpu_helper.c +++ b/target/riscv/cpu_helper.c @@ -91,6 +91,33 @@ bool cpu_get_fcfien(CPURISCVState *env) } } =20 +bool cpu_get_bcfien(CPURISCVState *env) +{ + /* no cfi extension, return false */ + if (!env_archcpu(env)->cfg.ext_zicfiss) { + return false; + } + + switch (env->priv) { + case PRV_U: + if (riscv_has_ext(env, RVS)) { + return env->senvcfg & SENVCFG_SSE; + } + return env->menvcfg & MENVCFG_SSE; +#ifndef CONFIG_USER_ONLY + case PRV_S: + if (env->virt_enabled) { + return env->henvcfg & HENVCFG_SSE; + } + return env->menvcfg & MENVCFG_SSE; + case PRV_M: /* M-mode shadow stack is always on if hart implements */ + return true; +#endif + default: + g_assert_not_reached(); + } +} + void cpu_get_tb_cpu_state(CPURISCVState *env, vaddr *pc, uint64_t *cs_base, uint32_t *pflags) { diff --git a/target/riscv/csr.c b/target/riscv/csr.c index a5a969a377..ec04b2b32b 100644 --- a/target/riscv/csr.c +++ b/target/riscv/csr.c @@ -185,6 +185,25 @@ static RISCVException zcmt(CPURISCVState *env, int csr= no) return RISCV_EXCP_NONE; } =20 +static RISCVException cfi_ss(CPURISCVState *env, int csrno) +{ + if (!env_archcpu(env)->cfg.ext_zicfiss) { + return RISCV_EXCP_ILLEGAL_INST; + } + + /* if bcfi not active for current env, access to csr is illegal */ + if (!cpu_get_bcfien(env)) { +#if !defined(CONFIG_USER_ONLY) + if (env->debugger) { + return RISCV_EXCP_NONE; + } +#endif + return RISCV_EXCP_ILLEGAL_INST; + } + + return RISCV_EXCP_NONE; +} + #if !defined(CONFIG_USER_ONLY) static RISCVException mctr(CPURISCVState *env, int csrno) { @@ -596,6 +615,19 @@ static RISCVException seed(CPURISCVState *env, int csr= no) #endif } =20 +/* zicfiss CSR_SSP read and write */ +static int read_ssp(CPURISCVState *env, int csrno, target_ulong *val) +{ + *val =3D env->ssp; + return RISCV_EXCP_NONE; +} + +static int write_ssp(CPURISCVState *env, int csrno, target_ulong val) +{ + env->ssp =3D val; + return RISCV_EXCP_NONE; +} + /* User Floating-Point CSRs */ static RISCVException read_fflags(CPURISCVState *env, int csrno, target_ulong *val) @@ -2111,6 +2143,10 @@ static RISCVException write_menvcfg(CPURISCVState *e= nv, int csrno, if (env_archcpu(env)->cfg.ext_zicfilp) { mask |=3D MENVCFG_LPE; } + + if (env_archcpu(env)->cfg.ext_zicfiss) { + mask |=3D MENVCFG_SSE; + } } env->menvcfg =3D (env->menvcfg & ~mask) | (val & mask); =20 @@ -2167,6 +2203,13 @@ static RISCVException write_senvcfg(CPURISCVState *e= nv, int csrno, mask |=3D SENVCFG_LPE; } =20 + /* Higher mode SSE must be ON for next-less mode SSE to be ON */ + if (env_archcpu(env)->cfg.ext_zicfiss && + get_field(env->menvcfg, MENVCFG_SSE) && + (env->virt_enabled ? get_field(env->henvcfg, HENVCFG_SSE) : true))= { + mask |=3D SENVCFG_SSE; + } + env->senvcfg =3D (env->senvcfg & ~mask) | (val & mask); return RISCV_EXCP_NONE; } @@ -2208,6 +2251,12 @@ static RISCVException write_henvcfg(CPURISCVState *e= nv, int csrno, if (env_archcpu(env)->cfg.ext_zicfilp) { mask |=3D HENVCFG_LPE; } + + /* H can light up SSE for VS only if HS had it from menvcfg */ + if (env_archcpu(env)->cfg.ext_zicfiss && + get_field(env->menvcfg, MENVCFG_SSE)) { + mask |=3D HENVCFG_SSE; + } } =20 env->henvcfg =3D (env->henvcfg & ~mask) | (val & mask); @@ -4663,6 +4712,9 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] =3D { /* Zcmt Extension */ [CSR_JVT] =3D {"jvt", zcmt, read_jvt, write_jvt}, =20 + /* zicfiss Extension, shadow stack register */ + [CSR_SSP] =3D { "ssp", cfi_ss, read_ssp, write_ssp }, + #if !defined(CONFIG_USER_ONLY) /* Machine Timers and Counters */ [CSR_MCYCLE] =3D { "mcycle", any, read_hpmcounter, diff --git a/target/riscv/machine.c b/target/riscv/machine.c index 873957c4ab..84d5ecf436 100644 --- a/target/riscv/machine.c +++ b/target/riscv/machine.c @@ -369,6 +369,24 @@ static const VMStateDescription vmstate_elp =3D { } }; =20 +static bool ssp_needed(void *opaque) +{ + RISCVCPU *cpu =3D opaque; + + return cpu->cfg.ext_zicfiss; +} + +static const VMStateDescription vmstate_ssp =3D { + .name =3D "cpu/ssp", + .version_id =3D 1, + .minimum_version_id =3D 1, + .needed =3D ssp_needed, + .fields =3D (const VMStateField[]) { + VMSTATE_UINTTL(env.ssp, RISCVCPU), + VMSTATE_END_OF_LIST() + } +}; + const VMStateDescription vmstate_riscv_cpu =3D { .name =3D "cpu", .version_id =3D 10, @@ -442,6 +460,7 @@ const VMStateDescription vmstate_riscv_cpu =3D { &vmstate_smstateen, &vmstate_jvt, &vmstate_elp, + &vmstate_ssp, NULL } }; --=20 2.44.0