Received: by 2002:a05:7412:8d10:b0:f3:1519:9f41 with SMTP id bj16csp4986589rdb; Tue, 12 Dec 2023 15:45:52 -0800 (PST) X-Google-Smtp-Source: AGHT+IHD5woa/5QkSqk2g2a/90MQQeHEHBatCX/9baj49lmi+Ixrn2terO1NCLSqA97lHamcNlG1 X-Received: by 2002:a05:6a00:6082:b0:6d0:8556:1a75 with SMTP id fp2-20020a056a00608200b006d085561a75mr4080529pfb.16.1702424752410; Tue, 12 Dec 2023 15:45:52 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702424752; cv=none; d=google.com; s=arc-20160816; b=bRz5xsAtU1Mvb2Hb4XW3XnI3dR6DwBEvEtFRxD4uZm5hfOFXR1MqtCPjwwvUMhwPF4 t+KP8Thp4JXB5Y/7jCMToplrAih5O+ElNi1jiUBjWwVp6Gehlox4Osy7q9WSftNwIw6I XlTLG9HxhsIDRQbfTqHhgc0Z2eNqnffiTorE9pFqmJMZEBJw5p65sfl6tvEoTFpjaFOq lWmaHF0VYzwOrkpYaB9NH5QGKciuYW4I+zksqUgwpjJkj+UIMaR/qAZAyFroH7zHVEM3 NpQ7sPL2dDmJL87XfAnX/f7uYpyPXSMPfH5lkIoSle+93l4TB9cn1PEQavIKF1xyIlem 04Ag== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:to:content-transfer-encoding:cc:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=nyjaDMcAOxN0dCLePetIVRjmx5K1JwIuU6FTTfYueuY=; fh=m0tLuRHOOw3+0/ubs+5BIvtmR5rXTQzeZ5Ko1oLcQVg=; b=yI2gbjxAyJHd6K4Dr1ts8FCaSyUPpwBBn4/B6fs2Sx+d3pRQuDuPHHZUSLqRMdnNKA qxi/20I7BAS2MbpS/EmsSQV++o5bx6doxOLXY7i2RCYkY+GL2MPvmeqbdAcmIYE9C4Yl 1DE9DNaLdEmrTJxhWTS2MZ4AncPSEx8l3W+BeTlZdkjmBrF6zcOit3yiHrAGRy+lpxhO P1PKeJwrevmq9ZtmA6rEhffy/wra2o+FpMYkeIENJGLSV227GtX9Bh5EqIJdfMrlhb04 gty2PULEJO4Qj/Mk9HGJ9HCMdt3dG6w0ZguUsObSGPZ0NGE5m6LIOQHRYM2Db4r1gqvV OpRA== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=HBdGiqKw; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from lipwig.vger.email (lipwig.vger.email. [2620:137:e000::3:3]) by mx.google.com with ESMTPS id 38-20020a630c66000000b005c203ad0e2csi7225215pgm.827.2023.12.12.15.45.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Dec 2023 15:45:52 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) client-ip=2620:137:e000::3:3; Authentication-Results: mx.google.com; dkim=fail header.i=@rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=HBdGiqKw; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id D3A1D80B26EF; Tue, 12 Dec 2023 15:45:49 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1377465AbjLLXpf (ORCPT + 99 others); Tue, 12 Dec 2023 18:45:35 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42312 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232486AbjLLXpe (ORCPT ); Tue, 12 Dec 2023 18:45:34 -0500 Received: from mail-yw1-x1131.google.com (mail-yw1-x1131.google.com [IPv6:2607:f8b0:4864:20::1131]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BB48C8F for ; Tue, 12 Dec 2023 15:45:40 -0800 (PST) Received: by mail-yw1-x1131.google.com with SMTP id 00721157ae682-5e2b8f480b3so380887b3.0 for ; Tue, 12 Dec 2023 15:45:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1702424740; x=1703029540; darn=vger.kernel.org; h=content-transfer-encoding:cc:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=nyjaDMcAOxN0dCLePetIVRjmx5K1JwIuU6FTTfYueuY=; b=HBdGiqKwvrDjWIT9O0tBbrNs0N781+aWJlZBmHpcmurV8Y9WSFkYJDM69AQdXm+MdO e4B9oRIpi5Zlmq8WMobdy2U9v6KtR8mnDVosPDP4RCdkZoELhiDsJj96TZZRznjcOlDW E7czyvZ9vOIhxPFarEMLNF5IN3AfzYbHW5l0ZOytOid+1etTqmfSDIOXRqiiltWyx1mh 7mfBsPCBJx2n19aW3zGCgdR4AxHOz1iodmN8HfaayJJtT4Q9n/ryXt6xldIbckGG7wqi 7YQ4xKSdm4DMez5bo5QELGBJkCE47E7RjUivcUirHhSt89acskn6ocLk35hXYiV6VKgk 1q7Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702424740; x=1703029540; h=content-transfer-encoding:cc:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=nyjaDMcAOxN0dCLePetIVRjmx5K1JwIuU6FTTfYueuY=; b=RSb8L0lgbmSScYmle7/sIrCKtYXKIsmjgTH1pas4dvLJXrFb2lyjg72Z7IJpnvGWRq JKZKBCArRlEeznjV3UGsbOhYTtYNIf5Rhcj/q/X+gUFkY72gL2ajfHgJ86RPK4LGmGbU l0Cwq0mPz2bPWZgCjtXw4k9uxB9YXBzmH2GxtSc1m+FlvhhxDrb26+fKDLg/4nnvoUGn 4lUInz+fCC/HJ33BNiBc8xLWkqmNW8MQ6i7tW3OY4uUN9pXfNdHapxhx49rsV9PLSQI0 qtLRtUZRYrYp9JVockOyjCS5rLrDNSQaCN6v00OuS1QfRWk7kJbEJNc/YYe1qG8b5oHA fmlA== X-Gm-Message-State: AOJu0Yw5ud0eaXrmFQYDablB7I5F/v6G3/hxUqT4ry7s/s8lpJtApL55 n2RKMWv+bnccRI7khOloqVS3rq8+J8lGyRI44GQPpKmQzfPt6iPQFDcosA== X-Received: by 2002:a5b:889:0:b0:db9:e78d:7eff with SMTP id e9-20020a5b0889000000b00db9e78d7effmt942246ybq.59.1702424739879; Tue, 12 Dec 2023 15:45:39 -0800 (PST) MIME-Version: 1.0 References: <20231212234310.2034900-1-debug@rivosinc.com> In-Reply-To: <20231212234310.2034900-1-debug@rivosinc.com> From: Deepak Gupta Date: Tue, 12 Dec 2023 15:45:28 -0800 Message-ID: Subject: Re: [PATCH v1 1/2] riscv: abstract envcfg CSR Cc: Paul Walmsley , Palmer Dabbelt , Albert Ou , Anup Patel , Andrew Jones , Guo Ren , Conor Dooley , Mayuresh Chitale , Greentime Hu , =?UTF-8?B?QmrDtnJuIFTDtnBlbA==?= , Sami Tolvanen , Ley Foon Tan , Sia Jee Heng , Heiko Stuebner , Evan Green , Jisheng Zhang , =?UTF-8?B?Q2zDqW1lbnQgTMOpZ2Vy?= , linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-0.6 required=5.0 tests=DKIM_INVALID,DKIM_SIGNED, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.vger.email To: unlisted-recipients:; (no To-header on input) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (lipwig.vger.email [0.0.0.0]); Tue, 12 Dec 2023 15:45:50 -0800 (PST) Some issues in workflow. Patches didn't come out separately. Ignore this. On Tue, Dec 12, 2023 at 3:43=E2=80=AFPM Deepak Gupta w= rote: > > This patch abstracts envcfg CSR in kernel (as is done for other homonyn > CSRs). CSR_ENVCFG is used as alias for CSR_SENVCFG or CSR_MENVCFG dependi= ng > on how kernel is compiled. > > Additionally it changes CBZE enabling to start using CSR_ENVCFG instead o= f > CSR_SENVCFG. > > Signed-off-by: Deepak Gupta > --- > arch/riscv/include/asm/csr.h | 2 ++ > arch/riscv/kernel/cpufeature.c | 2 +- > 2 files changed, 3 insertions(+), 1 deletion(-) > > diff --git a/arch/riscv/include/asm/csr.h b/arch/riscv/include/asm/csr.h > index 306a19a5509c..b3400517b0a9 100644 > --- a/arch/riscv/include/asm/csr.h > +++ b/arch/riscv/include/asm/csr.h > @@ -415,6 +415,7 @@ > # define CSR_STATUS CSR_MSTATUS > # define CSR_IE CSR_MIE > # define CSR_TVEC CSR_MTVEC > +# define CSR_ENVCFG CSR_MENVCFG > # define CSR_SCRATCH CSR_MSCRATCH > # define CSR_EPC CSR_MEPC > # define CSR_CAUSE CSR_MCAUSE > @@ -439,6 +440,7 @@ > # define CSR_STATUS CSR_SSTATUS > # define CSR_IE CSR_SIE > # define CSR_TVEC CSR_STVEC > +# define CSR_ENVCFG CSR_SENVCFG > # define CSR_SCRATCH CSR_SSCRATCH > # define CSR_EPC CSR_SEPC > # define CSR_CAUSE CSR_SCAUSE > diff --git a/arch/riscv/kernel/cpufeature.c b/arch/riscv/kernel/cpufeatur= e.c > index b3785ffc1570..98623393fd1f 100644 > --- a/arch/riscv/kernel/cpufeature.c > +++ b/arch/riscv/kernel/cpufeature.c > @@ -725,7 +725,7 @@ arch_initcall(check_unaligned_access_all_cpus); > void riscv_user_isa_enable(void) > { > if (riscv_cpu_has_extension_unlikely(smp_processor_id(), RISCV_IS= A_EXT_ZICBOZ)) > - csr_set(CSR_SENVCFG, ENVCFG_CBZE); > + csr_set(CSR_ENVCFG, ENVCFG_CBZE); > } > > #ifdef CONFIG_RISCV_ALTERNATIVE > -- > 2.43.0 > > > From 2ad5b91bda97f6dbc9f48a2e0e8c6eae5b8452ca Mon Sep 17 00:00:00 2001 > From: Deepak Gupta > Date: Tue, 12 Dec 2023 14:28:59 -0800 > Subject: [PATCH v1 2/2] riscv: envcfg save and restore on trap entry/exit > > envcfg CSR defines enabling bits for cache management instructions and so= on > will control enabling for control flow integrity and pointer masking feat= ures. > > Control flow integrity and pointer masking features need to be enabled on= per > thread basis. Additionally, I believe cache management instructions need = to be > enabled on per thread basis. As an example a seccomped task on riscv may = be > restricted to not use cache management instructions > > This patch creates a place holder for envcfg CSR in `thread_info` and add= s > logic to save and restore on trap entry and exits. This allows such isa f= eature > to be enabled on per thread basis. > > Signed-off-by: Deepak Gupta > --- > arch/riscv/include/asm/thread_info.h | 1 + > arch/riscv/kernel/asm-offsets.c | 1 + > arch/riscv/kernel/entry.S | 6 ++++++ > 3 files changed, 8 insertions(+) > > diff --git a/arch/riscv/include/asm/thread_info.h b/arch/riscv/include/as= m/thread_info.h > index 574779900bfb..320bc899a63b 100644 > --- a/arch/riscv/include/asm/thread_info.h > +++ b/arch/riscv/include/asm/thread_info.h > @@ -57,6 +57,7 @@ struct thread_info { > long user_sp; /* User stack pointer */ > int cpu; > unsigned long syscall_work; /* SYSCALL_WORK_ flags */ > + unsigned long envcfg; > #ifdef CONFIG_SHADOW_CALL_STACK > void *scs_base; > void *scs_sp; > diff --git a/arch/riscv/kernel/asm-offsets.c b/arch/riscv/kernel/asm-offs= ets.c > index a03129f40c46..cdd8f095c30c 100644 > --- a/arch/riscv/kernel/asm-offsets.c > +++ b/arch/riscv/kernel/asm-offsets.c > @@ -39,6 +39,7 @@ void asm_offsets(void) > OFFSET(TASK_TI_PREEMPT_COUNT, task_struct, thread_info.preempt_co= unt); > OFFSET(TASK_TI_KERNEL_SP, task_struct, thread_info.kernel_sp); > OFFSET(TASK_TI_USER_SP, task_struct, thread_info.user_sp); > + OFFSET(TASK_TI_ENVCFG, task_struct, thread_info.envcfg); > #ifdef CONFIG_SHADOW_CALL_STACK > OFFSET(TASK_TI_SCS_SP, task_struct, thread_info.scs_sp); > #endif > diff --git a/arch/riscv/kernel/entry.S b/arch/riscv/kernel/entry.S > index 54ca4564a926..a1d87013f15a 100644 > --- a/arch/riscv/kernel/entry.S > +++ b/arch/riscv/kernel/entry.S > @@ -64,12 +64,14 @@ SYM_CODE_START(handle_exception) > csrr s3, CSR_TVAL > csrr s4, CSR_CAUSE > csrr s5, CSR_SCRATCH > + csrr s6, CSR_ENVCFG > REG_S s0, PT_SP(sp) > REG_S s1, PT_STATUS(sp) > REG_S s2, PT_EPC(sp) > REG_S s3, PT_BADADDR(sp) > REG_S s4, PT_CAUSE(sp) > REG_S s5, PT_TP(sp) > + REG_S s6, TASK_TI_ENVCFG(tp) > > /* > * Set the scratch register to 0, so that if a recursive exceptio= n > @@ -129,6 +131,10 @@ SYM_CODE_START_NOALIGN(ret_from_exception) > addi s0, sp, PT_SIZE_ON_STACK > REG_S s0, TASK_TI_KERNEL_SP(tp) > > + /* restore envcfg bits for current thread */ > + REG_L s0, TASK_TI_ENVCFG(tp) > + csrw CSR_ENVCFG, s0 > + > /* Save the kernel shadow call stack pointer */ > scs_save_current > > -- > 2.43.0 >