From mboxrd@z Thu Jan 1 00:00:00 1970 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 33364C5B549 for ; Wed, 4 Jun 2025 17:17:32 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C1A158D004E; Wed, 4 Jun 2025 13:17:28 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id BCA538D0007; Wed, 4 Jun 2025 13:17:28 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A91E78D004E; Wed, 4 Jun 2025 13:17:28 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 89F9F8D0007 for ; Wed, 4 Jun 2025 13:17:28 -0400 (EDT) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 3DEC71A02B6 for ; Wed, 4 Jun 2025 17:17:28 +0000 (UTC) X-FDA: 83518374576.09.0941540 Received: from mail-pg1-f172.google.com (mail-pg1-f172.google.com [209.85.215.172]) by imf20.hostedemail.com (Postfix) with ESMTP id 3CBB01C0009 for ; Wed, 4 Jun 2025 17:17:26 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=rvg+0cnL; spf=pass (imf20.hostedemail.com: domain of debug@rivosinc.com designates 209.85.215.172 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1749057446; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=H0Lpssa4FFZpcPHQwOt7vFOEiyyUWWrWhSwc1jf+7uw=; b=a8/mosNIzYVnffOtV4zLBbx5poei2GKE30YQ9jDpeBreMSwxrqLD5dxgloV3AGRO3j0zXF Vev4z/4+AN68hiXVU8uYnOhZQ6P748dnBsE0WEghxlVVILXCW8Y20NtKD2IOBexv+ELeyS 8oZ57wMvBS/638av9SX3Cbe6kCwu+dQ= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=rvg+0cnL; spf=pass (imf20.hostedemail.com: domain of debug@rivosinc.com designates 209.85.215.172 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1749057446; a=rsa-sha256; cv=none; b=BcSX2z3F1x5gV0EcUceN8KeLmBl/0j8bEMGXw/yXZ2p2wYeJLTckMSvnW0hLaWA/K0DhZM Z0gOWccJqYID5LYvn1dCN/APUSEV/QPG7pZDn8/+AV/bhBzaT+pKpfdClXBwMLA69QG2Rk Dlf1P66Qrb3UgiyZKcP1bRZuKuxTTto= Received: by mail-pg1-f172.google.com with SMTP id 41be03b00d2f7-b1fd59851baso26907a12.0 for ; Wed, 04 Jun 2025 10:17:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1749057445; x=1749662245; darn=kvack.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=H0Lpssa4FFZpcPHQwOt7vFOEiyyUWWrWhSwc1jf+7uw=; b=rvg+0cnLh1xwZ2/3AXX4BuqlQ1lT/ic7MSBb2EGM9c36dQocMG6klTkC0u/rxaYFEp E+6YKeaYDENO1iCv7iwLPWkFcg6A7SbM80eNGfbR8FUybefOVAFqlSiH113tptAlVvDq Aj1th0hD2i8BDYAYtATk/A29M5f0kJ3v7CbzKB76lvh7hY1eKzNrT7kOdksZwyVPRGLu 49I58XTjsJ1RbaaZ3I4HccBawzljbHG6e1VV2vLWzWTOw5di2JLKZC1SYpIxqMrqXnp0 AeSHEFaili6v/r7lfgodFleg6Iqf0vaOxCQI7ZvK2IuVuquGx/P18plTkMkyHAN3plOU M96A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1749057445; x=1749662245; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=H0Lpssa4FFZpcPHQwOt7vFOEiyyUWWrWhSwc1jf+7uw=; b=EY2iWg2S1Nh86vKyt45itb+WHVHzimqcls4TzJ5zhE2BpV9O6lylxb2vkCDp5lJtgP CwiiE7vLiu/4Cb1/vfOpdYIFKk6hanZjdlQMwDJzTBPDlooLvXxSngdE1WS2pC1xmqbr FAYacnKH7RxARtFw4BpdJWcVshMd4AqFuChHG05JyauWU6pwMIyHslnuHU8xJx1qcz4L vE8ml/rascLXVF91hne8rrxn/gUZJ57Ck4gehEYZbfULCjgy3xkrIxlaEc9zApv/ftXO z/AKdTVdQIJRZ7341IRyUXIZabhF5j0gYVdBQdBOKslEu6Num26gUdTV5gsIrgwkEa3V QDYg== X-Forwarded-Encrypted: i=1; AJvYcCXMwfoVpongvXhRcdtiR26Q8204V8fJQVtQc74ImS0llihi6X4hrcWWTglB9HiNiw4AjztLVNDxVw==@kvack.org X-Gm-Message-State: AOJu0YwrugA6snj5lpi+AC72GdfW4Cq9Hm5eD/apUpoQ55SYBiHu6Oh7 bzX+Kx3Bh7/CAI/E1JrB5V1olL07onAfc/u60NXwKxCFj3z3p5g0qQF+abJ+2rPDsVg= X-Gm-Gg: ASbGncvctO6h/eb/+LprSEJLC3ByRxo2SInEp0AdmtY8Z8dD+DuDCruc5QGngny5ZNL 9waIxyKzlaz0HF/OBGIlHKNt2xSs3EAkw9APi1sO1jhqi0N81NoJjBlsechtPC/mjnkutSckjrI NGhLWB27O4PNiGKgzYMNjLE6mq8K2Fq6twWnbvOlStgsy/mz3qzU17dOstRYOWNRLwjVI0DTmux wqk03jvMPBnfZ50zsPbcw1jSV+/65L/Ze5nxRtUDjJfn0C7CcOlstyd0QtY8+gCaa69O/2Gq+zQ NAkb/igrZhkq+l1Agwq+9DSUsSVtkP5GbBvCz2jMo+bwCGYeKEtgOqRpQchRhQ== X-Google-Smtp-Source: AGHT+IHtnJUflTu1uvvucB6Ee3tNMuV0H6i/cc/cNNLd9oBxeeqr3LHx3B28lleNPgDKQ5dpFxUxRA== X-Received: by 2002:a17:90b:350c:b0:313:27b2:f729 with SMTP id 98e67ed59e1d1-31327b2f8fdmr894157a91.23.1749057444796; Wed, 04 Jun 2025 10:17:24 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-3124e2e9c9fsm9178972a91.30.2025.06.04.10.17.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 04 Jun 2025 10:17:24 -0700 (PDT) From: Deepak Gupta Date: Wed, 04 Jun 2025 10:15:47 -0700 Subject: [PATCH v17 23/27] arch/riscv: compile vdso with landing pad MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20250604-v5_user_cfi_series-v17-23-4565c2cf869f@rivosinc.com> References: <20250604-v5_user_cfi_series-v17-0-4565c2cf869f@rivosinc.com> In-Reply-To: <20250604-v5_user_cfi_series-v17-0-4565c2cf869f@rivosinc.com> To: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Andrew Morton , "Liam R. Howlett" , Vlastimil Babka , Lorenzo Stoakes , Paul Walmsley , Palmer Dabbelt , Albert Ou , Conor Dooley , Rob Herring , Krzysztof Kozlowski , Arnd Bergmann , Christian Brauner , Peter Zijlstra , Oleg Nesterov , Eric Biederman , Kees Cook , Jonathan Corbet , Shuah Khan , Jann Horn , Conor Dooley , Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross Cc: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-riscv@lists.infradead.org, devicetree@vger.kernel.org, linux-arch@vger.kernel.org, linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org, alistair.francis@wdc.com, richard.henderson@linaro.org, jim.shu@sifive.com, andybnac@gmail.com, kito.cheng@sifive.com, charlie@rivosinc.com, atishp@rivosinc.com, evan@rivosinc.com, cleger@rivosinc.com, alexghiti@rivosinc.com, samitolvanen@google.com, broonie@kernel.org, rick.p.edgecombe@intel.com, rust-for-linux@vger.kernel.org, Zong Li , Deepak Gupta X-Mailer: b4 0.13.0 X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 3CBB01C0009 X-Stat-Signature: ck76thms8s8a8eoka53w95q1mz9f3ynq X-Rspam-User: X-HE-Tag: 1749057446-73595 X-HE-Meta: U2FsdGVkX18ms821mEq8/hf8d0XsRPmKvjm7L16UMTRimVew+5I9jmZ3ydxK76ZgoAnmze7ex2n/ig2yGJYJytteIZMNVh7J8BLynwjSLWzOpGs9BiGYcAyvSweAzz565nwqsfwWADIVw5dlRmhUTCtzpGqHJYkjpD16khwG2xWIRxw3wtwrjEWmY5gNoE0CnQoeFzQb/G9F4H6oafSnWe/JxgvyGHOVHRXAE34JGgAq9WjaTaPh+hok/nO9phSjafjDo2kUAwrCtsTt5ua1kraTTkuKwf25K3/njkGIL3yVG9OF1n24Iz8VnaVjS7TpJHwTNQa1/7j9TZFMRvWVjry8qaA+OoEs1ZtebbI1Q/WO4Ie7IE3ErLFPra2jJr8JCmq1azEL2ZmnFGGlX5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Q3ocnCb4HkPPylOYvnG63E= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Jim Shu user mode tasks compiled with zicfilp may call indirectly into vdso (like hwprobe indirect calls). Add landing pad compile support in vdso. vdso with landing pad in it will be nop for tasks which have not enabled landing pad. This patch allows to run user mode tasks with cfi eanbled and do no harm. Future work can be done on this to do below - labeled landing pad on vdso functions (whenever labeling support shows up in gnu-toolchain) - emit shadow stack instructions only in vdso compiled objects as part of kernel compile. Signed-off-by: Jim Shu Reviewed-by: Zong Li Signed-off-by: Deepak Gupta --- arch/riscv/Makefile | 5 +++- arch/riscv/include/asm/assembler.h | 44 +++++++++++++++++++++++++++++++++++ arch/riscv/kernel/vdso/Makefile | 6 +++++ arch/riscv/kernel/vdso/flush_icache.S | 4 ++++ arch/riscv/kernel/vdso/getcpu.S | 4 ++++ arch/riscv/kernel/vdso/rt_sigreturn.S | 4 ++++ arch/riscv/kernel/vdso/sys_hwprobe.S | 4 ++++ 7 files changed, 70 insertions(+), 1 deletion(-) diff --git a/arch/riscv/Makefile b/arch/riscv/Makefile index 539d2aef5cab..c2dd09bb9db3 100644 --- a/arch/riscv/Makefile +++ b/arch/riscv/Makefile @@ -88,9 +88,12 @@ riscv-march-$(CONFIG_TOOLCHAIN_HAS_ZACAS) := $(riscv-march-y)_zacas # Check if the toolchain supports Zabha riscv-march-$(CONFIG_TOOLCHAIN_HAS_ZABHA) := $(riscv-march-y)_zabha +KBUILD_BASE_ISA = -march=$(shell echo $(riscv-march-y) | sed -E 's/(rv32ima|rv64ima)fd([^v_]*)v?/\1\2/') +export KBUILD_BASE_ISA + # Remove F,D,V from isa string for all. Keep extensions between "fd" and "v" by # matching non-v and non-multi-letter extensions out with the filter ([^v_]*) -KBUILD_CFLAGS += -march=$(shell echo $(riscv-march-y) | sed -E 's/(rv32ima|rv64ima)fd([^v_]*)v?/\1\2/') +KBUILD_CFLAGS += $(KBUILD_BASE_ISA) KBUILD_AFLAGS += -march=$(riscv-march-y) diff --git a/arch/riscv/include/asm/assembler.h b/arch/riscv/include/asm/assembler.h index 44b1457d3e95..a058ea5e9c58 100644 --- a/arch/riscv/include/asm/assembler.h +++ b/arch/riscv/include/asm/assembler.h @@ -80,3 +80,47 @@ .endm #endif /* __ASM_ASSEMBLER_H */ + +#if defined(CONFIG_RISCV_USER_CFI) && (__riscv_xlen == 64) +.macro vdso_lpad +lpad 0 +.endm +#else +.macro vdso_lpad +.endm +#endif + +/* + * This macro emits a program property note section identifying + * architecture features which require special handling, mainly for + * use in assembly files included in the VDSO. + */ +#define NT_GNU_PROPERTY_TYPE_0 5 +#define GNU_PROPERTY_RISCV_FEATURE_1_AND 0xc0000000 + +#define GNU_PROPERTY_RISCV_FEATURE_1_ZICFILP (1U << 0) +#define GNU_PROPERTY_RISCV_FEATURE_1_ZICFISS (1U << 1) + +#if defined(CONFIG_RISCV_USER_CFI) && (__riscv_xlen == 64) +#define GNU_PROPERTY_RISCV_FEATURE_1_DEFAULT \ + (GNU_PROPERTY_RISCV_FEATURE_1_ZICFILP) +#endif + +#ifdef GNU_PROPERTY_RISCV_FEATURE_1_DEFAULT +.macro emit_riscv_feature_1_and, feat = GNU_PROPERTY_RISCV_FEATURE_1_DEFAULT + .pushsection .note.gnu.property, "a" + .p2align 3 + .word 4 + .word 16 + .word NT_GNU_PROPERTY_TYPE_0 + .asciz "GNU" + .word GNU_PROPERTY_RISCV_FEATURE_1_AND + .word 4 + .word \feat + .word 0 + .popsection +.endm +#else +.macro emit_riscv_feature_1_and, feat = 0 +.endm +#endif diff --git a/arch/riscv/kernel/vdso/Makefile b/arch/riscv/kernel/vdso/Makefile index ad73607abc28..441c5431d27e 100644 --- a/arch/riscv/kernel/vdso/Makefile +++ b/arch/riscv/kernel/vdso/Makefile @@ -13,12 +13,18 @@ vdso-syms += flush_icache vdso-syms += hwprobe vdso-syms += sys_hwprobe +ifdef CONFIG_RISCV_USER_CFI +LPAD_MARCH = _zicfilp_zicfiss -fcf-protection=full +endif + # Files to link into the vdso obj-vdso = $(patsubst %, %.o, $(vdso-syms)) note.o ccflags-y := -fno-stack-protector ccflags-y += -DDISABLE_BRANCH_PROFILING ccflags-y += -fno-builtin +ccflags-y += $(KBUILD_BASE_ISA)$(LPAD_MARCH) +asflags-y += $(KBUILD_BASE_ISA)$(LPAD_MARCH) ifneq ($(c-gettimeofday-y),) CFLAGS_vgettimeofday.o += -fPIC -include $(c-gettimeofday-y) diff --git a/arch/riscv/kernel/vdso/flush_icache.S b/arch/riscv/kernel/vdso/flush_icache.S index 8f884227e8bc..e4c56970905e 100644 --- a/arch/riscv/kernel/vdso/flush_icache.S +++ b/arch/riscv/kernel/vdso/flush_icache.S @@ -5,11 +5,13 @@ #include #include +#include .text /* int __vdso_flush_icache(void *start, void *end, unsigned long flags); */ SYM_FUNC_START(__vdso_flush_icache) .cfi_startproc + vdso_lpad #ifdef CONFIG_SMP li a7, __NR_riscv_flush_icache ecall @@ -20,3 +22,5 @@ SYM_FUNC_START(__vdso_flush_icache) ret .cfi_endproc SYM_FUNC_END(__vdso_flush_icache) + +emit_riscv_feature_1_and diff --git a/arch/riscv/kernel/vdso/getcpu.S b/arch/riscv/kernel/vdso/getcpu.S index 9c1bd531907f..5c1ecc4e1465 100644 --- a/arch/riscv/kernel/vdso/getcpu.S +++ b/arch/riscv/kernel/vdso/getcpu.S @@ -5,14 +5,18 @@ #include #include +#include .text /* int __vdso_getcpu(unsigned *cpu, unsigned *node, void *unused); */ SYM_FUNC_START(__vdso_getcpu) .cfi_startproc + vdso_lpad /* For now, just do the syscall. */ li a7, __NR_getcpu ecall ret .cfi_endproc SYM_FUNC_END(__vdso_getcpu) + +emit_riscv_feature_1_and diff --git a/arch/riscv/kernel/vdso/rt_sigreturn.S b/arch/riscv/kernel/vdso/rt_sigreturn.S index 3dc022aa8931..e82987dc3739 100644 --- a/arch/riscv/kernel/vdso/rt_sigreturn.S +++ b/arch/riscv/kernel/vdso/rt_sigreturn.S @@ -5,12 +5,16 @@ #include #include +#include .text SYM_FUNC_START(__vdso_rt_sigreturn) .cfi_startproc .cfi_signal_frame + vdso_lpad li a7, __NR_rt_sigreturn ecall .cfi_endproc SYM_FUNC_END(__vdso_rt_sigreturn) + +emit_riscv_feature_1_and diff --git a/arch/riscv/kernel/vdso/sys_hwprobe.S b/arch/riscv/kernel/vdso/sys_hwprobe.S index 77e57f830521..f1694451a60c 100644 --- a/arch/riscv/kernel/vdso/sys_hwprobe.S +++ b/arch/riscv/kernel/vdso/sys_hwprobe.S @@ -3,13 +3,17 @@ #include #include +#include .text SYM_FUNC_START(riscv_hwprobe) .cfi_startproc + vdso_lpad li a7, __NR_riscv_hwprobe ecall ret .cfi_endproc SYM_FUNC_END(riscv_hwprobe) + +emit_riscv_feature_1_and -- 2.43.0