From patchwork Thu Sep 12 23:16:20 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13802824 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 D7576EEE271 for ; Thu, 12 Sep 2024 23:17:15 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3A4D56B0089; Thu, 12 Sep 2024 19:17:15 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 355DE6B008A; Thu, 12 Sep 2024 19:17:15 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 181486B008C; Thu, 12 Sep 2024 19:17:15 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id ECAF96B0089 for ; Thu, 12 Sep 2024 19:17:14 -0400 (EDT) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 7024EA0E32 for ; Thu, 12 Sep 2024 23:17:14 +0000 (UTC) X-FDA: 82557649188.27.C377B49 Received: from mail-pl1-f170.google.com (mail-pl1-f170.google.com [209.85.214.170]) by imf10.hostedemail.com (Postfix) with ESMTP id 826FBC000D for ; Thu, 12 Sep 2024 23:17:11 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=e7fWWBMM; spf=pass (imf10.hostedemail.com: domain of debug@rivosinc.com designates 209.85.214.170 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=1726182926; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=Nw1dKcVSLCCt5un4d6VomXZplHXDD/4tgdiLE024NX8=; b=B4pz+6eFKyJJHKoykhAaNQwcUY3bGDvNQkX+Qy0CVsC+cEIenUtSfrDkWeIE7A0y7ojHw3 DjrAr9zhyTBuJh4uGRCEIEWjKzLR0FtY3awZ3ayywLa/veY6Wxe/+hp7y1TbuuC1W0lt3A aSM0pkc6AqwbVsCcB/C1UZ5gXeMiqMU= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1726182926; a=rsa-sha256; cv=none; b=4MlZX6xncuZK9iUotw15Y4+MEoy+cIeysPJMINFfGw4NMpQeRAYYdVLPAOvpUSq/Alcy+g LDnLVxRA3Q2eKynARXyhBP/15gZswuQx4PdgqsRLk65lVlidhIh/enoo4GoFF+qAMUbqSU xvGC1M3bNA8+NWfwELtt0xzD50ORoLk= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=e7fWWBMM; spf=pass (imf10.hostedemail.com: domain of debug@rivosinc.com designates 209.85.214.170 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none Received: by mail-pl1-f170.google.com with SMTP id d9443c01a7336-2068bee21d8so17142125ad.2 for ; Thu, 12 Sep 2024 16:17:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1726183030; x=1726787830; darn=kvack.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=Nw1dKcVSLCCt5un4d6VomXZplHXDD/4tgdiLE024NX8=; b=e7fWWBMMsU9i31Pe6tHEEFgmJooIae+gDrg69RTc3YCt7opR00Tq21qJke1hp4Ktjv lcD3Me0E4hv7vT1mD5AuRLBhEwMRhNeZ8kHKrRLL+sOHaVZf+drsnfEEjxkEWLeFi8+Q MVIjX09NIShNBPts4rXl32THvS+vMLEFJjun7MEq/VdCcTyZ4z4/1AV3rhdzYzYWWlH0 +Eiy/IaMpw0eYjNW3c5I301YnUbiCXRmmsZ9bq2aMXcPx+VPf6oSF1JblfBRmAAnUPCb WaSRs8GvYf8OR1np+DWIHuw2vfOB0194uvg30NvotSpCnczdqSmc7wcdIzKiFzFA4RHK T7CA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726183030; x=1726787830; 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=Nw1dKcVSLCCt5un4d6VomXZplHXDD/4tgdiLE024NX8=; b=Ufj6eNf6SXGGKbd0YqTYAIu2pXjUPTlW4qq5K6wApnpKhFv4h/jXPXlAMB6Pb2LUMW sRG0gOIr8YhkqSm6sSVpOymJlflqmPlRpjHJbhV4zeUs6RYhhgDcWVIJ0rpaoLP42VH/ yhWoa3BKu1REN/3fKbXZUhlZ9ak7c7mQlG7fu8A8yjdfNJlTG5MPUjU5O9MpMdqhM//5 YbheRi8J+1MjZxwCApltWGoNIHWKZ+rlJVTLOytBwodSCBbmInqCpgPDED3DWBDTE8dY YjrPA9jhgEKJ6181OI/sVKXa9j+818Yjels6DgiDFrTXT4v9Jj1ax5U3Zi9+Zp/W0Wq9 qQqA== X-Forwarded-Encrypted: i=1; AJvYcCWJsc9/pRJx5xiG/k/wPbNjMd35CcEwwd8vwMSCFy2HzY61oFJ3ICwjqds1V6V5mI0D2u8exRl0aA==@kvack.org X-Gm-Message-State: AOJu0YzUFqbQtZ1I+sabqak/czKosqLLFc32m3e4qqPRj1xiay+sMDk/ OR6LOixFfDRndzEg2sRuYKA5rgKild+oWPPRJbHQYIkl9cxGn++pklLpqMfdHjg= X-Google-Smtp-Source: AGHT+IHV0R4LSdRMN0UeGhZXF9FypysCDDOC5LIEh5xrA8oVwZ2edglc2TZ3k8QjvVsKUPvcjGWQgQ== X-Received: by 2002:a17:90b:17cb:b0:2d8:e7db:9996 with SMTP id 98e67ed59e1d1-2db9ff93f7amr4718623a91.13.1726183029966; Thu, 12 Sep 2024 16:17:09 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2db6c1ac69asm3157591a91.0.2024.09.12.16.17.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 12 Sep 2024 16:17:09 -0700 (PDT) From: Deepak Gupta To: paul.walmsley@sifive.com, palmer@sifive.com, conor@kernel.org, linux-doc@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kselftest@vger.kernel.org Cc: corbet@lwn.net, palmer@dabbelt.com, aou@eecs.berkeley.edu, robh@kernel.org, krzk+dt@kernel.org, oleg@redhat.com, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, hpa@zytor.com, peterz@infradead.org, akpm@linux-foundation.org, arnd@arndb.de, ebiederm@xmission.com, kees@kernel.org, Liam.Howlett@oracle.com, vbabka@suse.cz, lorenzo.stoakes@oracle.com, shuah@kernel.org, brauner@kernel.org, samuel.holland@sifive.com, debug@rivosinc.com, andy.chiu@sifive.com, jerry.shih@sifive.com, greentime.hu@sifive.com, charlie@rivosinc.com, evan@rivosinc.com, cleger@rivosinc.com, xiao.w.wang@intel.com, ajones@ventanamicro.com, anup@brainfault.org, mchitale@ventanamicro.com, atishp@rivosinc.com, sameo@rivosinc.com, bjorn@rivosinc.com, alexghiti@rivosinc.com, david@redhat.com, libang.li@antgroup.com, jszhang@kernel.org, leobras@redhat.com, guoren@kernel.org, samitolvanen@google.com, songshuaishuai@tinylab.org, costa.shul@redhat.com, bhe@redhat.com, zong.li@sifive.com, puranjay@kernel.org, namcaov@gmail.com, antonb@tenstorrent.com, sorear@fastmail.com, quic_bjorande@quicinc.com, ancientmodern4@gmail.com, ben.dooks@codethink.co.uk, quic_zhonhan@quicinc.com, cuiyunhui@bytedance.com, yang.lee@linux.alibaba.com, ke.zhao@shingroup.cn, sunilvl@ventanamicro.com, tanzhasanwork@gmail.com, schwab@suse.de, dawei.li@shingroup.cn, rppt@kernel.org, willy@infradead.org, usama.anjum@collabora.com, osalvador@suse.de, ryan.roberts@arm.com, andrii@kernel.org, alx@kernel.org, catalin.marinas@arm.com, broonie@kernel.org, revest@chromium.org, bgray@linux.ibm.com, deller@gmx.de, zev@bewilderbeest.net, Rick Edgecombe Subject: [PATCH v4 01/30] mm: Introduce ARCH_HAS_USER_SHADOW_STACK Date: Thu, 12 Sep 2024 16:16:20 -0700 Message-ID: <20240912231650.3740732-2-debug@rivosinc.com> X-Mailer: git-send-email 2.45.0 In-Reply-To: <20240912231650.3740732-1-debug@rivosinc.com> References: <20240912231650.3740732-1-debug@rivosinc.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 826FBC000D X-Stat-Signature: 498rrkri4q3r6yce7acqhc3xkqipqxx7 X-HE-Tag: 1726183031-271540 X-HE-Meta: U2FsdGVkX1/mL8dtedgOYm/eNblN8xxQ8DF4Zh2Xz9irsi2uOlB9WjYy1//2BlQ9asnJqFNq9ejE+3xo3+X5Q62NQWBrCIPhIghDrTJUPhnM2WzozsQCGJhQN9OGu1M7ONIy8YjQDrYJZoewt1w29r41trOUIOD3woFVBCllCcV7l2c39Lf4uFRW/EOVyBFwG2XDGvr+00r2ZkbGB3wY5bX6IHYef+pVlj0Z8x90c0n+ZfA1NLjHmsleiQO1ZNGa53vkWSdPSuEE+DDaqOrJ3OFbFl9ZGZH5GuNlqyR+QGPoiArmKSkcOI/iNR92paKFr+/Hhy12ZTFJc+AL1ilB40o850NGWTKk5OLEcpNUiBDa2lv5s44RWIGfQWSGA6t+AYPh19r6bdn78hYxcbla7IMcTaFE8X/F4UVvXnj+/ARAtFhUjRVNarhDzfRQzfgBu4YBu8dfdt0Ub+08VKG11/usXFkHyY4gCLH7i6UhVIlmRD3B4yQi3ZNgMVFbYlwCav3C+2MVDx8wTuUQbZ80v1Vrt2nT3dpWdu5zJXXSFDnvTbjeMziyxeq9Ua5eZ8SlpQ3EedmK8AnfiTZCBoAbhsTpVfst7GsM09UyakTXOJoQvRZyvX5a/3frC+4otzr0wVOFkGN3sJ063Yj23cv7I1csUMOVtcRR5I98Rfz7qjlKh1YF7B6CuqCa5fYrw+fqyX3BEejpVHk2wZvmK3gEdBO7YzNnSw6+TaSdC16zwefi9bIENWCA7qEaBThtuDYv6TqyW6hs/8rksO8Ae0mFV2xFSloT1Ak8tXXfA4Om2puSb9gHsHJ8VWm58OESOCqWlFtwlQOkZjCyMzsRBNlBsZfQ75UDNUkeD66sXIbZnBF0IpBf5THCfRolcnfM2ikSP0skKeXH8nD4zpb8955zX9Ic97BOMHVv6TbSiUlVHveFzRxUwqk4MiMWwFdZ+5tNHKBz/lwJicS07blnoU2 JlT3shXd D83QLmj3SSMMhgE8/UGD2cfR/FieqQNhx9MKNfwmK8JFbxBSi8zGvmFswaKlJyiLrLFINjIVKwxbcqQJbF7sZcOlB/MAJo3nzzJlbrkfOeF6W1rq9kS3SkwDuJEcORgn++I0kgEHKfn/BTLgFEDG19fQzJXZtJJq3Qh8FDyNy2BJj6Hg7hr5Bm5Dys3flM8yMNGkZpPPahAWJ1LUBX3CvuzS6WCQ4lcDyOFBD/cN52FGn8DWJYYSkYUjhdfA/8dGvJrFf8jHdVhO5k9ZnmnSau4aCFx4T7oLe1OQXHN9mVfGBx103hUaP5rYY3ZkK3xtS5Crvl/nUwJYHHsaeBaB2anBm61/7NZhgBbB/CYo3bZhB0JXeJ7DU2HPoAHQDFU44VzcQ+k08oTN1TWraIt6D0/Z5kC3OGZ5UWB4UiYC52mHiSdIDNgIVOZGUo+5HI22u8/ud+RUNRCj+LoNyRVJj2ZLCfLLVchWFd0OJ6heAgmSxcI1kxkxD1Yuu8zrXKRaOEs5zLIktbBXpTJ239QxF/xfR9MgGbwMzCcV4OPCoNnJPZ4cYzrUJSxw+V4Rsb8gecZuw 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: Mark Brown Since multiple architectures have support for shadow stacks and we need to select support for this feature in several places in the generic code provide a generic config option that the architectures can select. Suggested-by: David Hildenbrand Acked-by: David Hildenbrand Signed-off-by: Mark Brown Reviewed-by: Rick Edgecombe Reviewed-by: Deepak Gupta Signed-off-by: Deepak Gupta Reviewed-by: Carlos Bilbao --- arch/x86/Kconfig | 1 + fs/proc/task_mmu.c | 2 +- include/linux/mm.h | 2 +- mm/Kconfig | 6 ++++++ 4 files changed, 9 insertions(+), 2 deletions(-) diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig index 007bab9f2a0e..320e1f411163 100644 --- a/arch/x86/Kconfig +++ b/arch/x86/Kconfig @@ -1957,6 +1957,7 @@ config X86_USER_SHADOW_STACK depends on AS_WRUSS depends on X86_64 select ARCH_USES_HIGH_VMA_FLAGS + select ARCH_HAS_USER_SHADOW_STACK select X86_CET help Shadow stack protection is a hardware feature that detects function diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c index 5f171ad7b436..0ea49725f524 100644 --- a/fs/proc/task_mmu.c +++ b/fs/proc/task_mmu.c @@ -984,7 +984,7 @@ static void show_smap_vma_flags(struct seq_file *m, struct vm_area_struct *vma) #ifdef CONFIG_HAVE_ARCH_USERFAULTFD_MINOR [ilog2(VM_UFFD_MINOR)] = "ui", #endif /* CONFIG_HAVE_ARCH_USERFAULTFD_MINOR */ -#ifdef CONFIG_X86_USER_SHADOW_STACK +#ifdef CONFIG_ARCH_HAS_USER_SHADOW_STACK [ilog2(VM_SHADOW_STACK)] = "ss", #endif #ifdef CONFIG_64BIT diff --git a/include/linux/mm.h b/include/linux/mm.h index 147073601716..e39796ea17db 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -346,7 +346,7 @@ extern unsigned int kobjsize(const void *objp); #endif #endif /* CONFIG_ARCH_HAS_PKEYS */ -#ifdef CONFIG_X86_USER_SHADOW_STACK +#ifdef CONFIG_ARCH_HAS_USER_SHADOW_STACK /* * VM_SHADOW_STACK should not be set with VM_SHARED because of lack of * support core mm. diff --git a/mm/Kconfig b/mm/Kconfig index b72e7d040f78..3167be663bca 100644 --- a/mm/Kconfig +++ b/mm/Kconfig @@ -1263,6 +1263,12 @@ config IOMMU_MM_DATA config EXECMEM bool +config ARCH_HAS_USER_SHADOW_STACK + bool + help + The architecture has hardware support for userspace shadow call + stacks (eg, x86 CET, arm64 GCS or RISC-V Zicfiss). + source "mm/damon/Kconfig" endmenu From patchwork Thu Sep 12 23:16:21 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13802825 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 11C9CEEE274 for ; Thu, 12 Sep 2024 23:17:19 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8FD3E6B008C; Thu, 12 Sep 2024 19:17:18 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 885856B0092; Thu, 12 Sep 2024 19:17:18 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6AF6D6B0093; Thu, 12 Sep 2024 19:17:18 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 4AD366B008C for ; Thu, 12 Sep 2024 19:17:18 -0400 (EDT) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id C9D2BC0595 for ; Thu, 12 Sep 2024 23:17:17 +0000 (UTC) X-FDA: 82557649314.26.2A9B563 Received: from mail-pg1-f172.google.com (mail-pg1-f172.google.com [209.85.215.172]) by imf26.hostedemail.com (Postfix) with ESMTP id EBC09140005 for ; Thu, 12 Sep 2024 23:17:15 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=cqCdANpm; spf=pass (imf26.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=1726182930; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=067qKPjJ+n9l6Epc1eYM3Yt2z4Jj28HWHwDjljiSStY=; b=pnFFDKGi/3HHnDCntLW84P7WVweMr9UE6n7Krr7fnCuAoegdpiIr9ayCdppoVsn+2Zpi18 Fx66JcMChLlquzKG/WKFj9xkwWSdHR+WSWCEhizhyC3Uv+L3YAr8DugHS78/NcCH4kNauZ KmcbLbSRP2SV4efNHWORBbd8NB/fSiE= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1726182930; a=rsa-sha256; cv=none; b=MZABQEcoun5LcYECsd1evFgRpHzLdihA5puDjb14aX1lKqJTd7LaHOstfgbbvKc5xvIFop xe3oWNdL7XTKS0N5zLVPuBApdCM467SFS1cH0zQOyuEX2apLQyPCuPo7y45G3KIVn7FwAK 9qR+r4NYjGpegPKVLrvlYnDFOhqJ350= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=cqCdANpm; spf=pass (imf26.hostedemail.com: domain of debug@rivosinc.com designates 209.85.215.172 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none Received: by mail-pg1-f172.google.com with SMTP id 41be03b00d2f7-7db233cef22so183850a12.0 for ; Thu, 12 Sep 2024 16:17:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1726183034; x=1726787834; darn=kvack.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=067qKPjJ+n9l6Epc1eYM3Yt2z4Jj28HWHwDjljiSStY=; b=cqCdANpmdn5LCNx6nuj7SkCrgFCwpIWda161ye+T3Ve1l5Ss6iyZGJtlTqbzLh7T7h cF5RHWbklw9AJdSynaUBQKHdAESmAE09pi7gYX3cK5XpY/yCpIu2EBQwa9+6FBmwL9mI 3VrSPVME2pD6klndZMdyd6xePgAqF5AH2pPy2FqlTUeEB2A5CjUzkht7Z98LgTmWiebu 5SrxFTH90t7Z0+XOmokoa2sTbYUeh61i9lGyyzCzQ1CHQnRiKedOm2KjaWUB5BQup72m NHUc5epWun7Sux8Zn3TQTW7G2ms8PXPFER9Te3cHDrVqOKsf3MPpfAKTP7GDD+nN13NP Z5/A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726183034; x=1726787834; 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=067qKPjJ+n9l6Epc1eYM3Yt2z4Jj28HWHwDjljiSStY=; b=IySy8JV1krgzt6NqhyF/3BR+OaaXu91FAlb5nYoF8M071TEqVj9ULWWMqc9GzOUROd OXYcs6K/qnbZapC7hgQZlM+gJpbgT4Iv/mlPUXAgzGNAnOehBfaP4aPbX4EJs1BnBclg 3v+cCNK2LkO/RsDMdWGEz01Q6v00kqUub5oaQ++32gKnSztXThpnvHe5/fQdTmCFLGSx TorLg3wzUDFNggOp6JQWAGwBI0o5DzzVYKQ91LD2CIQXOToMqr2lSNfEtyPlq6EzNCEk IVKkYRm+Vs064EXkNtie570TMeiJPCmKoem/uUVEtiFN8J1m4SSqFPN73r3y99K7uvAf jXdQ== X-Forwarded-Encrypted: i=1; AJvYcCXAgSLZO9iHpkR6CDZsVkTekMkz+2ueaC9X2uaP0mMoHQBLxXMvKjF60K7o0TYCG6hSTV6WxwUy6Q==@kvack.org X-Gm-Message-State: AOJu0Yy2U6NiW3w3yiA0iLYcr7QctFVZ3T0WoMODDVhsooAN2X9KtRYo LdEi6ARZ5yD+u5+RClI3IYZVhjWIDgV7mZaS4gEB/ALwJc8WSiVPN/W711RWixm8kuXR4m7MBVX N X-Google-Smtp-Source: AGHT+IGUPoWnvS5/UyAev2mMCxr4fAbJAZEQ5NXGYl3k4LYiYl5m+yOy9I9Oauh7+qEP1ZcWno2Wvg== X-Received: by 2002:a17:90a:d489:b0:2d3:cd57:bd3 with SMTP id 98e67ed59e1d1-2dbb9f3a7cemr1099703a91.29.1726183034358; Thu, 12 Sep 2024 16:17:14 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2db6c1ac69asm3157591a91.0.2024.09.12.16.17.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 12 Sep 2024 16:17:13 -0700 (PDT) From: Deepak Gupta To: paul.walmsley@sifive.com, palmer@sifive.com, conor@kernel.org, linux-doc@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kselftest@vger.kernel.org Cc: corbet@lwn.net, palmer@dabbelt.com, aou@eecs.berkeley.edu, robh@kernel.org, krzk+dt@kernel.org, oleg@redhat.com, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, hpa@zytor.com, peterz@infradead.org, akpm@linux-foundation.org, arnd@arndb.de, ebiederm@xmission.com, kees@kernel.org, Liam.Howlett@oracle.com, vbabka@suse.cz, lorenzo.stoakes@oracle.com, shuah@kernel.org, brauner@kernel.org, samuel.holland@sifive.com, debug@rivosinc.com, andy.chiu@sifive.com, jerry.shih@sifive.com, greentime.hu@sifive.com, charlie@rivosinc.com, evan@rivosinc.com, cleger@rivosinc.com, xiao.w.wang@intel.com, ajones@ventanamicro.com, anup@brainfault.org, mchitale@ventanamicro.com, atishp@rivosinc.com, sameo@rivosinc.com, bjorn@rivosinc.com, alexghiti@rivosinc.com, david@redhat.com, libang.li@antgroup.com, jszhang@kernel.org, leobras@redhat.com, guoren@kernel.org, samitolvanen@google.com, songshuaishuai@tinylab.org, costa.shul@redhat.com, bhe@redhat.com, zong.li@sifive.com, puranjay@kernel.org, namcaov@gmail.com, antonb@tenstorrent.com, sorear@fastmail.com, quic_bjorande@quicinc.com, ancientmodern4@gmail.com, ben.dooks@codethink.co.uk, quic_zhonhan@quicinc.com, cuiyunhui@bytedance.com, yang.lee@linux.alibaba.com, ke.zhao@shingroup.cn, sunilvl@ventanamicro.com, tanzhasanwork@gmail.com, schwab@suse.de, dawei.li@shingroup.cn, rppt@kernel.org, willy@infradead.org, usama.anjum@collabora.com, osalvador@suse.de, ryan.roberts@arm.com, andrii@kernel.org, alx@kernel.org, catalin.marinas@arm.com, broonie@kernel.org, revest@chromium.org, bgray@linux.ibm.com, deller@gmx.de, zev@bewilderbeest.net Subject: [PATCH v4 02/30] mm: helper `is_shadow_stack_vma` to check shadow stack vma Date: Thu, 12 Sep 2024 16:16:21 -0700 Message-ID: <20240912231650.3740732-3-debug@rivosinc.com> X-Mailer: git-send-email 2.45.0 In-Reply-To: <20240912231650.3740732-1-debug@rivosinc.com> References: <20240912231650.3740732-1-debug@rivosinc.com> MIME-Version: 1.0 X-Stat-Signature: t7japkwgcbshtpsa9cobtw1rxnbwngd8 X-Rspamd-Queue-Id: EBC09140005 X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1726183035-374670 X-HE-Meta: U2FsdGVkX1+clVxQ92SgDoKWtan5iltUFLVq6pILsN927TUIBCy68kfeD7QtJwuQIJTEjE9OftegnPLfPMTfsEw5y2kLPE7j08nwGDh0EKI3FTTQC482jvvwuPAW3J4bgPQ8QbtACCPhWbrjjsO8P8JyNbckJ5jtOXTAZoGurocN7F96ORKUKjHOMCYJS45namwIeC7KHFlUZ6iG0kP7LsJe9uUtDiYicbYXaSsOVBmYpk7ktIGcppQ4pzcDWZq/z0dyNoxKywH0Z7wt6X+lqMnpqh5JTxzISCTBMt+4dmIH3fReQZZmzvWpbjFUDUgJ2pEgs9jPwsqgLlhwyKWUnSEN6kuEpQwyakou2qexJjpXHQHx8NInaFpBvGeig87UVVvlLM42m5NTi1bQTEzFc5bjGHsMMP7eYRyTibI9iOrKw4S80dQ3erbHqgrv5N8DSwXw++DlRXMiJ7nuAmkq3+eeuERL2vuIj5spU73+wsbclRS5OI/ZRSdH4e6TnqkJP+pNTZVsCrcU9YvvTJ/1eYYDiWSe1cYrQU+7/dAa3aCfQltKHb+JrgonTxUSqRpaeiJM0klWpxQ3MCXwYGAANjmQ7l5wR+IhUCzTvo1c5rQOdM8vvQS6L1cJZwth127l/W/vM3rdfCi4gXQbJjCH/VlsuvC2vlalr3lrQDb9nYl5pEv5+Cs4yApngA3rRdPzs8hAVuidgOsarpCTn5zlT7kRfc9zH4RioBDLdI/xTDt8+uP6RgKm55Gb2SEJh4FTdtor+FIHPo50IEdNvpJbCLjUDojTzz491Hh1H7GqSk7+mGE9VPUHJOC+7SHAd0Ee3gmlpGDH0g6r/dX6plturJjx4+XzZ5JyLDI5z0Mk4CrtGzs/EsfCA9pvpE2vIErf9Nuu9H6pcw6qBk1o06/5T1GW4VyFu2lSbjCRVDzu9NWAa7rEHlKuNsXhC19eFVp7k7Phi+yWljXopwXpqEZ dEX5mfR7 K5i8MbQ4gZIHr9KvOcJJ5wTsy87yR3gJLk6doC1er9wkqWrry6xV3jx5CCafkgeHgzRGKu1/yOyHlVXsEO8OIgynZntevuZn1gH2SK2Ia0Jw3Iv4HBaUx4gp3mSR7gZDZvzQf0qPM2ejPgMebcJft9VJXBEU37Tba8Ytp+88xfp89vEFh4mwcsBXVMprs0rZiMoWRZXucp0a6Iq9XkSKTe5+Mjf4LNbMGCyvQjxwaLwvlayoG1WEKCDXA8hbupRukNPSCAcGoHWVjTrFH7DtntbF0YCFtoSIfXIQL272xvIb/+ra0f8hu1xcU/WrlYninPMYiXi5IJPklV71opQs7yu5m/aA7PfT+Y4uXU8FoV4Zsk1sA1/ot50ps+HRBMCbdJlA+u5zHuHxk8jewgXeobYbaNPoYj3eJLQv64Q/O2/ChuHFIDua9T78YuRrRZZ1Fzd6Db5FBfnpPmfL636W74t1EoCbvMdl6zcvx+feyK9KZ97URgSSF7UGBjQ== 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: VM_SHADOW_STACK (alias to VM_HIGH_ARCH_5) is used to encode shadow stack VMA on three architectures (x86 shadow stack, arm GCS and RISC-V shadow stack). In case architecture doesn't implement shadow stack, it's VM_NONE Introducing a helper `is_shadow_stack_vma` to determine shadow stack vma or not. Signed-off-by: Deepak Gupta --- include/linux/mm.h | 7 ++++++- mm/gup.c | 2 +- mm/internal.h | 2 +- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index e39796ea17db..f0dc94fb782a 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -361,6 +361,11 @@ extern unsigned int kobjsize(const void *objp); # define VM_SHADOW_STACK VM_NONE #endif +static inline bool is_shadow_stack_vma(vm_flags_t vm_flags) +{ + return !!(vm_flags & VM_SHADOW_STACK); +} + #if defined(CONFIG_X86) # define VM_PAT VM_ARCH_1 /* PAT reserves whole VMA at once (x86) */ #elif defined(CONFIG_PPC) @@ -3504,7 +3509,7 @@ static inline unsigned long stack_guard_start_gap(struct vm_area_struct *vma) return stack_guard_gap; /* See reasoning around the VM_SHADOW_STACK definition */ - if (vma->vm_flags & VM_SHADOW_STACK) + if (is_shadow_stack_vma(vma->vm_flags)) return PAGE_SIZE; return 0; diff --git a/mm/gup.c b/mm/gup.c index 54d0dc3831fb..2f84a0a80cfe 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -1289,7 +1289,7 @@ static int check_vma_flags(struct vm_area_struct *vma, unsigned long gup_flags) !writable_file_mapping_allowed(vma, gup_flags)) return -EFAULT; - if (!(vm_flags & VM_WRITE) || (vm_flags & VM_SHADOW_STACK)) { + if (!(vm_flags & VM_WRITE) || is_shadow_stack_vma(vm_flags)) { if (!(gup_flags & FOLL_FORCE)) return -EFAULT; /* hugetlb does not support FOLL_FORCE|FOLL_WRITE. */ diff --git a/mm/internal.h b/mm/internal.h index b4d86436565b..f7732c793f3f 100644 --- a/mm/internal.h +++ b/mm/internal.h @@ -798,7 +798,7 @@ static inline bool is_exec_mapping(vm_flags_t flags) */ static inline bool is_stack_mapping(vm_flags_t flags) { - return ((flags & VM_STACK) == VM_STACK) || (flags & VM_SHADOW_STACK); + return ((flags & VM_STACK) == VM_STACK) || is_shadow_stack_vma(flags); } /* From patchwork Thu Sep 12 23:16:22 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13802826 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 70599EEE270 for ; Thu, 12 Sep 2024 23:17:24 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id F3D286B0093; Thu, 12 Sep 2024 19:17:23 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id EEE1C6B0095; Thu, 12 Sep 2024 19:17:23 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D41346B0096; Thu, 12 Sep 2024 19:17:23 -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 B45656B0093 for ; Thu, 12 Sep 2024 19:17:23 -0400 (EDT) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 2F741140C70 for ; Thu, 12 Sep 2024 23:17:23 +0000 (UTC) X-FDA: 82557649566.17.8D725D5 Received: from mail-pg1-f170.google.com (mail-pg1-f170.google.com [209.85.215.170]) by imf21.hostedemail.com (Postfix) with ESMTP id 5780A1C0003 for ; Thu, 12 Sep 2024 23:17:20 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=h2pggIgw; spf=pass (imf21.hostedemail.com: domain of debug@rivosinc.com designates 209.85.215.170 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=1726182987; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=AYIoljWVtOX+4iWfX6n3x0ZmKPdqj9thDEJcRhBom38=; b=bcCaY4lDOrWhdSluC/1GzjHlMT4OKbDXca7gUhGHg2oqH59nGGuRLIiWlfOH+DlmeedXyP BJavlBX9mv+GbEnTe27do0grJLMFzzFVIXVL8w1ssjPwjATJV09hOjWbRqlZApNQxrRPn5 oo0QGK5CctVvn3NLzaQq30Z4Swl1Ebs= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=h2pggIgw; spf=pass (imf21.hostedemail.com: domain of debug@rivosinc.com designates 209.85.215.170 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1726182987; a=rsa-sha256; cv=none; b=FaTc6v5oXp3RIXeyhG0BUf6PNIWlwp7y7erHsI0ThX6y0phk3jVF2WmWrHgoEQQcWx9Hgz Ll0kVJ7eeoIKdXyl6o/W4bvnc+mstGfq1/SZbiqKIOiA7akSRkOsyI1pgtpQbSlUkG0VDo OX4LztlQoQZYR/duDvbUQGA/9cfHi4g= Received: by mail-pg1-f170.google.com with SMTP id 41be03b00d2f7-7d82b0ebd51so87361a12.2 for ; Thu, 12 Sep 2024 16:17:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1726183039; x=1726787839; darn=kvack.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=AYIoljWVtOX+4iWfX6n3x0ZmKPdqj9thDEJcRhBom38=; b=h2pggIgwUJKmqHC1xy6P/0s4ya9z+AwSLk0PsMNulxUiJlj/zu9lwYBATscShLWW1V 2ckDuyB1G+pAfbhrQp6VCeFBb/xhGhd+kyxfroWxI0E9WxhpIBMWAECwpKH9Dh9qq8eK ib3oOQHiy/Gr3v2ysxyz2ujrqTKO+yPf2AchFyjcZuqh2Zu+CpkUiE95M/rsXiFcOjMU dukrZi2bw17Md8erXaridvWUyuVyFXasU99WILGLHzJK1dN8ZvI6hpo7oCUH+2EUvzSQ FQeLxczAXHoxNIfBIPt1O1rdk3zGrVLHvBxuJrfvtGPaYcMaxsdfFXxBwAeYZSI5IYFN rB2Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726183039; x=1726787839; 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=AYIoljWVtOX+4iWfX6n3x0ZmKPdqj9thDEJcRhBom38=; b=W/mE59Gn6N5LTTanUmRrsvUQE4xVSUzVhdaSBD1NmRugiXQGcQn19fPdb6oCqzpH0s HHkk7AcTaFBA/yDpQ8fjAD12eh7ABEHmZ6GyvMrOh3+Ryd5MA121QMEsyZ7yBt+p9Cdu E1IJ4QDMW8afli/Kr9wYkT+29JG3xvPzk9cO+Ki1efkCZ/OgoqrTAqsx2u6cAygizFgP /RsnTqz8KEx9KbVewtHaPHl88v4RjtbZZxhhTb7zcQ7CNDFxBSZEYgco/USBqJq5eHYy zyau/9ds7j14tagXpFk1SFG+ilNSYfUudRMbme4bx1l5UgBK7aJsnTGkuFFkCFN0ECKh 2Usw== X-Forwarded-Encrypted: i=1; AJvYcCW5pbQRCE1e7OK5ksyLNm9MBMvFyXNv/2G8hmUhVjU6gJGWZn59br9N272rH9N9X4e0aGUUtEJwHQ==@kvack.org X-Gm-Message-State: AOJu0YzFa/oL0Cc4jMsvvPJGSuovAB8O4ZvZwMyOiymaJCujyRR357jh 3nHhGMqT2gEPyIFgRLCRRrQPPxDjo1RHQ28IZ67gT7jS813TCHL17dJPCRYgvQI= X-Google-Smtp-Source: AGHT+IHhk1/Ai6cyL1jJrvo5NU2IwAp0mq4oXO+l/4MJM1q0WDrPHsg2BmH5QLgdNFEq/wAXrgX1nQ== X-Received: by 2002:a17:90a:1648:b0:2d3:c9bb:9cd7 with SMTP id 98e67ed59e1d1-2dbb9f08b4amr1099367a91.36.1726183038878; Thu, 12 Sep 2024 16:17:18 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2db6c1ac69asm3157591a91.0.2024.09.12.16.17.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 12 Sep 2024 16:17:18 -0700 (PDT) From: Deepak Gupta To: paul.walmsley@sifive.com, palmer@sifive.com, conor@kernel.org, linux-doc@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kselftest@vger.kernel.org Cc: corbet@lwn.net, palmer@dabbelt.com, aou@eecs.berkeley.edu, robh@kernel.org, krzk+dt@kernel.org, oleg@redhat.com, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, hpa@zytor.com, peterz@infradead.org, akpm@linux-foundation.org, arnd@arndb.de, ebiederm@xmission.com, kees@kernel.org, Liam.Howlett@oracle.com, vbabka@suse.cz, lorenzo.stoakes@oracle.com, shuah@kernel.org, brauner@kernel.org, samuel.holland@sifive.com, debug@rivosinc.com, andy.chiu@sifive.com, jerry.shih@sifive.com, greentime.hu@sifive.com, charlie@rivosinc.com, evan@rivosinc.com, cleger@rivosinc.com, xiao.w.wang@intel.com, ajones@ventanamicro.com, anup@brainfault.org, mchitale@ventanamicro.com, atishp@rivosinc.com, sameo@rivosinc.com, bjorn@rivosinc.com, alexghiti@rivosinc.com, david@redhat.com, libang.li@antgroup.com, jszhang@kernel.org, leobras@redhat.com, guoren@kernel.org, samitolvanen@google.com, songshuaishuai@tinylab.org, costa.shul@redhat.com, bhe@redhat.com, zong.li@sifive.com, puranjay@kernel.org, namcaov@gmail.com, antonb@tenstorrent.com, sorear@fastmail.com, quic_bjorande@quicinc.com, ancientmodern4@gmail.com, ben.dooks@codethink.co.uk, quic_zhonhan@quicinc.com, cuiyunhui@bytedance.com, yang.lee@linux.alibaba.com, ke.zhao@shingroup.cn, sunilvl@ventanamicro.com, tanzhasanwork@gmail.com, schwab@suse.de, dawei.li@shingroup.cn, rppt@kernel.org, willy@infradead.org, usama.anjum@collabora.com, osalvador@suse.de, ryan.roberts@arm.com, andrii@kernel.org, alx@kernel.org, catalin.marinas@arm.com, broonie@kernel.org, revest@chromium.org, bgray@linux.ibm.com, deller@gmx.de, zev@bewilderbeest.net, Conor Dooley Subject: [PATCH v4 03/30] riscv: Enable cbo.zero only when all harts support Zicboz Date: Thu, 12 Sep 2024 16:16:22 -0700 Message-ID: <20240912231650.3740732-4-debug@rivosinc.com> X-Mailer: git-send-email 2.45.0 In-Reply-To: <20240912231650.3740732-1-debug@rivosinc.com> References: <20240912231650.3740732-1-debug@rivosinc.com> MIME-Version: 1.0 X-Rspamd-Server: rspam03 X-Rspam-User: X-Rspamd-Queue-Id: 5780A1C0003 X-Stat-Signature: 4h74f6qst5n1tno67t8qcyxeibb6xaqo X-HE-Tag: 1726183040-93209 X-HE-Meta: U2FsdGVkX1+kRfwy+OhJeQpJLu+v4/fQ1S9FFOBWtqFB+4Cc29QIlRfBTjobtV7kWOBdBXo2GwjILWYJVRWdFPL+70LjJ29IHK0WwriOpHXe2bhS2TSoa9FFfrf5JNodzOgoIRqmSuDWvf5AZ9/XZ5xdcrwgFHw4GC95rdRNCJ37NvpKesMMLTDwphXhoW/gTo1ihpH5zHxU8PEER4HzcjJ3RyV0teyKTnTrfAlazKcs2ObI2prUKYa4eMQFoGXNl0qo/DKKyfltpwxnZo0Y+yc7Q3ip2JN7lRbu+sk9MPPN9OIJWccv8TTzEw9vigsZQOCepvFEcu04mfharAMcwLOpiKrzX3+mZfvcR6X8RXBdIFTl1xm0wVRfcb28SOO/3PyludVdpXUU9hIJo3kaMu7jaJTJhC2ge9QmFbN4wFlGmuVeKzV+vL1n9euyTSC0V+gi6+oHjcJzA1Iw9Lccx7FyI0PLMQ3bOUAfHMefiK/vjN/oGtIWH2kZOMMfDn985/mGcqT11S5V40JQlmbKFlCFsQR587LHU9oNNvJ+NROD5sI5ZxvE48fpOoa4GmJ9D5CXl5BsTMd4F73lmreEO4oyfdppg78TlTS47BTSB3orXUhRXhKtB6Xw5s+iUkppTk2lAyGaY7ucZTMVxXvCzIrVJfGCY2zQe9Z99u/fxsGbe7lNPOY6UdcyP1nyO7VEWAkRbeJaerDDM/5FD7slO4+ml38+bNFObwZZ8R3aWfCFN5Us2XxjN92XH7q283kOAEqrXyFUAYSr/sDnDcr7dvmJ/85oWq/WbqXkq55DYXoDfzwj2YN2DQyN0ip+Mq/2Xfrd2IzVD77a8q5Vq+BQedLaSxNwafvKzbwpV/owRZOFmq/QE1nf6NPlN1dH2oXl601lrffMjAN8kO4dpQaNDa2K7B3WPN3Xsn4xm/8t15ZMIkls3fo8Fgy5cNNTEIJ2wv/KLEt9A8xpx5BeDd2 IoVqa+ih EM3tx5GyLWeOHAcHd153RKFQTayFTJ78Su7vcmIEy3MZDzMSO4qxCflp7r9POo2yaPv1P6ITgTGi3qZHsdXSsviheaIanKlTBVTQbtTWKPYJymHuFlShz8iCBl79/7mzNKdnmKLrpzHgn58GGPl/DhEb0IjBFqxGH0BgeYhocOsc7gZqqXRy4MXXe2kPOxZybx4I5ASg0vBunSzR45zxyAlP4AHRlir3eDe6OUGFNwV7OPGbW1rS79PYveYMYAqBWTCJ7Dyhy63Ep9Iz7Yvr49kfNnSHv/tKR0pCmWZuWBzw0NTY/PPjnDDDPDodwgZQXvw6VOHjCpe4Jx+AEgcbtKsSsXB7pI6Y4p9idC24eMcVxGjb6yNs181mTMjiZ81WjA1hqK3UaYuvS9H1rcLCo3pPUYjRcpNrFbIpMdygA4H4Ny+WT8FNCO+70iHdTBPnEgGkxnZnBn//dw4pXjoa9zlocA0fl+nzclNghlsdKsSM8YYoWTtsHjV4cQKTRuqrijyBAgnnWqu/WJsX/286BERD9lkMCu1vy0ZnxGQuJLtdMlQ1WjjlWOOw1IqqISLRNe44n/R3mCy5Y314Ri3uv3tXhTV/DkUbY2m4Wp7HC/iuqt9Wm+9kDAKKgmBKew1vq31Mxa8NA/psex0A= 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: Samuel Holland Currently, we enable cbo.zero for usermode on each hart that supports the Zicboz extension. This means that the [ms]envcfg CSR value may differ between harts. Other features, such as pointer masking and CFI, require setting [ms]envcfg bits on a per-thread basis. The combination of these two adds quite some complexity and overhead to context switching, as we would need to maintain two separate masks for the per-hart and per-thread bits. Andrew Jones, who originally added Zicboz support, writes[1][2]: I've approached Zicboz the same way I would approach all extensions, which is to be per-hart. I'm not currently aware of a platform that is / will be composed of harts where some have Zicboz and others don't, but there's nothing stopping a platform like that from being built. So, how about we add code that confirms Zicboz is on all harts. If any hart does not have it, then we complain loudly and disable it on all the other harts. If it was just a hardware description bug, then it'll get fixed. If there's actually a platform which doesn't have Zicboz on all harts, then, when the issue is reported, we can decide to not support it, support it with defconfig, or support it under a Kconfig guard which must be enabled by the user. Let's follow his suggested solution and require the extension to be available on all harts, so the envcfg CSR value does not need to change when a thread migrates between harts. Since we are doing this for all extensions with fields in envcfg, the CSR itself only needs to be saved/ restored when it is present on all harts. This should not be a regression as no known hardware has asymmetric Zicboz support, but if anyone reports seeing the warning, we will re-evaluate our solution. Link: https://lore.kernel.org/linux-riscv/20240322-168f191eeb8479b2ea169a5e@orel/ [1] Link: https://lore.kernel.org/linux-riscv/20240323-28943722feb57a41fb0ff488@orel/ [2] Reviewed-by: Andrew Jones Reviewed-by: Conor Dooley Reviewed-by: Deepak Gupta Signed-off-by: Samuel Holland Signed-off-by: Deepak Gupta --- arch/riscv/kernel/cpufeature.c | 7 ++++++- arch/riscv/kernel/suspend.c | 4 ++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/arch/riscv/kernel/cpufeature.c b/arch/riscv/kernel/cpufeature.c index b427188b28fc..0139d4ea8426 100644 --- a/arch/riscv/kernel/cpufeature.c +++ b/arch/riscv/kernel/cpufeature.c @@ -28,6 +28,8 @@ #define NUM_ALPHA_EXTS ('z' - 'a' + 1) +static bool any_cpu_has_zicboz; + unsigned long elf_hwcap __read_mostly; /* Host ISA bitmap */ @@ -98,6 +100,7 @@ static int riscv_ext_zicboz_validate(const struct riscv_isa_ext_data *data, pr_err("Zicboz disabled as cboz-block-size present, but is not a power-of-2\n"); return -EINVAL; } + any_cpu_has_zicboz = true; return 0; } @@ -918,8 +921,10 @@ unsigned long riscv_get_elf_hwcap(void) void riscv_user_isa_enable(void) { - if (riscv_cpu_has_extension_unlikely(smp_processor_id(), RISCV_ISA_EXT_ZICBOZ)) + if (riscv_has_extension_unlikely(RISCV_ISA_EXT_ZICBOZ)) csr_set(CSR_ENVCFG, ENVCFG_CBZE); + else if (any_cpu_has_zicboz) + pr_warn_once("Zicboz disabled as it is unavailable on some harts\n"); } #ifdef CONFIG_RISCV_ALTERNATIVE diff --git a/arch/riscv/kernel/suspend.c b/arch/riscv/kernel/suspend.c index c8cec0cc5833..9a8a0dc035b2 100644 --- a/arch/riscv/kernel/suspend.c +++ b/arch/riscv/kernel/suspend.c @@ -14,7 +14,7 @@ void suspend_save_csrs(struct suspend_context *context) { - if (riscv_cpu_has_extension_unlikely(smp_processor_id(), RISCV_ISA_EXT_XLINUXENVCFG)) + if (riscv_has_extension_unlikely(RISCV_ISA_EXT_XLINUXENVCFG)) context->envcfg = csr_read(CSR_ENVCFG); context->tvec = csr_read(CSR_TVEC); context->ie = csr_read(CSR_IE); @@ -37,7 +37,7 @@ void suspend_save_csrs(struct suspend_context *context) void suspend_restore_csrs(struct suspend_context *context) { csr_write(CSR_SCRATCH, 0); - if (riscv_cpu_has_extension_unlikely(smp_processor_id(), RISCV_ISA_EXT_XLINUXENVCFG)) + if (riscv_has_extension_unlikely(RISCV_ISA_EXT_XLINUXENVCFG)) csr_write(CSR_ENVCFG, context->envcfg); csr_write(CSR_TVEC, context->tvec); csr_write(CSR_IE, context->ie); From patchwork Thu Sep 12 23:16:23 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13802827 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 831DBEEE270 for ; Thu, 12 Sep 2024 23:17:27 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 183AB6B0095; Thu, 12 Sep 2024 19:17:27 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 133C06B0099; Thu, 12 Sep 2024 19:17:27 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E51886B0098; Thu, 12 Sep 2024 19:17:26 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id C774C6B0095 for ; Thu, 12 Sep 2024 19:17:26 -0400 (EDT) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 7E1461609B3 for ; Thu, 12 Sep 2024 23:17:26 +0000 (UTC) X-FDA: 82557649692.22.63A3236 Received: from mail-pl1-f172.google.com (mail-pl1-f172.google.com [209.85.214.172]) by imf29.hostedemail.com (Postfix) with ESMTP id A1583120008 for ; Thu, 12 Sep 2024 23:17:24 +0000 (UTC) Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b="ehlw5uF/"; dmarc=none; spf=pass (imf29.hostedemail.com: domain of debug@rivosinc.com designates 209.85.214.172 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1726182966; a=rsa-sha256; cv=none; b=fjvGRYP1qBM4VV8ACVGPMUo328YJ1a8HwJbGJoUgms+7zQgRugXRorN2n2ikmCqRoq1HTt zzSQJYj5eJy9b2ShulmM2xRxYErQa1laSvIev5ZrrJDnxgFkUdLLPeOQwzVt6RFtsACEVU v51Sw+FBAo0FmXqZgmKojnH0Iua2Msw= ARC-Authentication-Results: i=1; imf29.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b="ehlw5uF/"; dmarc=none; spf=pass (imf29.hostedemail.com: domain of debug@rivosinc.com designates 209.85.214.172 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1726182966; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=zDmvOTSrnPg54HHGP9mWRdaXRn5Dr4fSLwQGe7BW+Sc=; b=3Tj7sm7LO76byhVxQfHB9CrJC82tOL7DL6dIkQ2Yk2f3kMNNnsDZTNKGCelirQbJqQ5mAr uuoA26lOP5L0h2TsBrAud0XdLTGC2WYWUw/GBLp+TSJswE1Zfrb4SRbO/1CJ+2nhkYX0Za 9z3oLIh9G2LiGZk140y8qGIv1SSLTtE= Received: by mail-pl1-f172.google.com with SMTP id d9443c01a7336-206bd1c6ccdso2787285ad.3 for ; Thu, 12 Sep 2024 16:17:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1726183043; x=1726787843; darn=kvack.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=zDmvOTSrnPg54HHGP9mWRdaXRn5Dr4fSLwQGe7BW+Sc=; b=ehlw5uF/b4pP/7ojO5+O7hbyp10NCYDNUd72oiBqEqhCmMUr4Jw+YL50za4Ucg679w RoNk3BmDK/N1uhrK14kHi2jHkgfjUoDrkrGXvIXpHiivE5BBzwE1K3+2m4f61eUdecEA Bq6HCIG0/9oTjgKkePjmF4a7heF14eYbH8SfiRBa7+QXuA8bDUGBDUYBfhKKXu1w/Zpm cZGIra+YVuDVTAMwJxQ7QIv6T7warpBDySZTQhOvySPRgKyKszKQGWNr2DiuNZr4VtEd LvGSbObkb1bspjlzSlEU+K7i1hZGGPm3FwZBrcxu0FA272VwdEJPbLQ7jczp2nURpOEE 98eA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726183043; x=1726787843; 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=zDmvOTSrnPg54HHGP9mWRdaXRn5Dr4fSLwQGe7BW+Sc=; b=irzhqOmnEz288yCngwI4WbOkpodWz2s+UrF0Yf5cduD9e9xF70G35hhHu+pK0GAB32 ObOVhIozkbLYaUVWFWPHmJOMsEQM3NUZ8/E//LlORWRRA3vKQmGJ6xM32E05AkfI6NUw re59RX7PzyY5H4x9NgaVHmz34ePOo4YcU4gYG6BZ94joHiIslWIAxs9CpWAH6BUvSx6B b7gPuFBNowReIvbQBzdEwk4MbxZytCAuJzAuuAeVONoO/uCUXOYZTKv73xEOEnn12rDj Unw9Xr+Yx0+Q7KKO5gIylW6PMLJnr++xhqsfyVbeCAWAHi9gwwnw8KLWXZkRZvNVXqEG xckg== X-Forwarded-Encrypted: i=1; AJvYcCU9t+6fC9IE+F2vXhabSEubLFqo4wtoB3OUV9X2K/4gv09Au02uEaMTye4/tBbdxBEMEl5pCdfo2g==@kvack.org X-Gm-Message-State: AOJu0YxXY/PQimPTW4hAb+WFP9OAP6+mshHQKw4rFAv2E0N9qIAbqA8z ct0aYYX+Z+8EufIGaLTlqux4hm0U4YGYCKNwYMdSHF+n455v+U5bsaAVwJT9mLk= X-Google-Smtp-Source: AGHT+IFCbH4CWj3cRlGP/tTcFYJu5lqNBzSaDboWQslgR+Qb4odzPou2d9TOn5WUDlzlxhbuOJA+kw== X-Received: by 2002:a17:90a:cf0f:b0:2d8:8ce3:1e9d with SMTP id 98e67ed59e1d1-2dbb9dbda04mr929613a91.3.1726183043273; Thu, 12 Sep 2024 16:17:23 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2db6c1ac69asm3157591a91.0.2024.09.12.16.17.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 12 Sep 2024 16:17:22 -0700 (PDT) From: Deepak Gupta To: paul.walmsley@sifive.com, palmer@sifive.com, conor@kernel.org, linux-doc@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kselftest@vger.kernel.org Cc: corbet@lwn.net, palmer@dabbelt.com, aou@eecs.berkeley.edu, robh@kernel.org, krzk+dt@kernel.org, oleg@redhat.com, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, hpa@zytor.com, peterz@infradead.org, akpm@linux-foundation.org, arnd@arndb.de, ebiederm@xmission.com, kees@kernel.org, Liam.Howlett@oracle.com, vbabka@suse.cz, lorenzo.stoakes@oracle.com, shuah@kernel.org, brauner@kernel.org, samuel.holland@sifive.com, debug@rivosinc.com, andy.chiu@sifive.com, jerry.shih@sifive.com, greentime.hu@sifive.com, charlie@rivosinc.com, evan@rivosinc.com, cleger@rivosinc.com, xiao.w.wang@intel.com, ajones@ventanamicro.com, anup@brainfault.org, mchitale@ventanamicro.com, atishp@rivosinc.com, sameo@rivosinc.com, bjorn@rivosinc.com, alexghiti@rivosinc.com, david@redhat.com, libang.li@antgroup.com, jszhang@kernel.org, leobras@redhat.com, guoren@kernel.org, samitolvanen@google.com, songshuaishuai@tinylab.org, costa.shul@redhat.com, bhe@redhat.com, zong.li@sifive.com, puranjay@kernel.org, namcaov@gmail.com, antonb@tenstorrent.com, sorear@fastmail.com, quic_bjorande@quicinc.com, ancientmodern4@gmail.com, ben.dooks@codethink.co.uk, quic_zhonhan@quicinc.com, cuiyunhui@bytedance.com, yang.lee@linux.alibaba.com, ke.zhao@shingroup.cn, sunilvl@ventanamicro.com, tanzhasanwork@gmail.com, schwab@suse.de, dawei.li@shingroup.cn, rppt@kernel.org, willy@infradead.org, usama.anjum@collabora.com, osalvador@suse.de, ryan.roberts@arm.com, andrii@kernel.org, alx@kernel.org, catalin.marinas@arm.com, broonie@kernel.org, revest@chromium.org, bgray@linux.ibm.com, deller@gmx.de, zev@bewilderbeest.net Subject: [PATCH v4 04/30] riscv: Add support for per-thread envcfg CSR values Date: Thu, 12 Sep 2024 16:16:23 -0700 Message-ID: <20240912231650.3740732-5-debug@rivosinc.com> X-Mailer: git-send-email 2.45.0 In-Reply-To: <20240912231650.3740732-1-debug@rivosinc.com> References: <20240912231650.3740732-1-debug@rivosinc.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: A1583120008 X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: pnzrohutoyp3uz1hj5xn6ekd8nwfy3fe X-HE-Tag: 1726183044-713602 X-HE-Meta: U2FsdGVkX1/dCBwk5NLFbgjpqzEQIlsya8AT6/CDcNiHTBJ9bpJSmwPckCX4QBJNoxvMUtVSuLhdDgLWr7TUamjHS9Zht5pT5o3H/hHusGYOOoxFEzn7pFEkgexco2KRo92ocvRp7asLlM7ksP6oBiaJRnfv2t1e2GoSsfnZ47asSU4FyoxdjHUPZfh6t6vVrzq35PpyVtCWumZ7HWNfK3wbSTkHXw+nS6LpX9KVooscKUfVsgPAFNEnlp1xQKmRpNyvmaVqlS1KEn1b726P1YoI10Z+aEFMcZG9TNKpUHO5HnoWsHth5n/wkmCpsZEmn2FZV4k7isZapK7EYFo8T4t0ivafsaIwIqgYpKazLoimslJKhkXpwFxzazPcFWTudWRLwRiqMVYDwlI4+5CNWJCIU/9vqhMSIxlcLDWyLnr1kGFxNGPnodflWO5Y8lJask77VdhUhQb/jj1BYHYVszG87U4smKq3ht+TCEse2CSTRSSHEXbBlTuEzn6uWM6G0w2IRny1+9ypleNkXS9fZOUOkJLNxi5b8iayN4UK+wmJEWFGJY6tC1hyp6l9f03ba+GU2/roQJHTsZHQoXCGY99rYYiv+2WpdU7aWgCSw0JJm6u9NCZc2EI24wn1N9KqKs1z5jEbBj8j3jqoPRu6P7KgoBeVoalDgNix8eSikSJA8JlV32nqYoHRUyGxhNawHzMXwKtdyvyK4Y06+PMYlmgCR3iWPTaQUNSFrtn6jVR5d3TQY7MACT4j98ROYEdCqusNnjTV0i/xFM3u3A62aTGWVySLSU1NP0y6g+OJY43ZgNjZz9aT7Bpwyr60AmdwvXrVHyMZ2oyAw4NebTWIc4OJbtb3ntOFe6TfMlXZY+8x7bt+J3zMLQALBk3K+sMO1IFzSSV4SpEenkcS6jqbFSluoT7At+yy18RD4gEIos6WIpOu2m+bQdc87iXp5MZHjc7ak3Hm5PK/HeaCHRN JXRIo9cV NfMlck2hK+CsgdsexEBaEnIaLUF2uwK2Fhr3doKp94vYseogbf5d4uM1J/D35KIoZaVB0IMNRRdjDOUtABjEKHLoSw9ATItVGeIvDsdRziZMTpfGyAfW/1rwoxL6FGmbTn25dnWoscu4hUiGAQ27xS3vdu8Vm4v6E0cjKb6Uos8/BM0w5UCaWPl4h5QB/gvrjgSWS48LpSpB4N12DTJeGL2jLq+7ka6Rtzwc/FTtBBdTTzXA/BQJc25FBSXJePBz/BvfmGGPqeQ6dopenjrHi5xYK97RAcvdJcktvIaIiCKU1BfxpTRqqBESfNskgmb2gCk4g53OcTcnLO1wqxoMRb/5xVNTiZqdRcCbMHzFiRBop3WKbSApq1pjMvFG5rI1qOuhdm/l8rsktLm9UbbGNLH6t9jfWyVrQd9vV243kj/CVuzzgv9/yIXvlhjyDlZiTr7T5k7IUS8+5lkh6940ojgELkpaFJKVEYfpouw46DV4CuCRWEfDyK9WG3lynbjPd0qewU+FiYw+zZFeAxiwpkIQ5tiUTgf0ZzNKn 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: Samuel Holland Some bits in the [ms]envcfg CSR, such as the CFI state and pointer masking mode, need to be controlled on a per-thread basis. Support this by keeping a copy of the CSR value in struct thread_struct and writing it during context switches. It is safe to discard the old CSR value during the context switch because the CSR is modified only by software, so the CSR will remain in sync with the copy in thread_struct. Use ALTERNATIVE directly instead of riscv_has_extension_unlikely() to minimize branchiness in the context switching code. Since thread_struct is copied during fork(), setting the value for the init task sets the default value for all other threads. Reviewed-by: Andrew Jones Reviewed-by: Deepak Gupta Signed-off-by: Samuel Holland Signed-off-by: Deepak Gupta --- arch/riscv/include/asm/switch_to.h | 8 ++++++++ arch/riscv/include/asm/thread_info.h | 1 + arch/riscv/kernel/cpufeature.c | 2 +- 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/arch/riscv/include/asm/switch_to.h b/arch/riscv/include/asm/switch_to.h index 7594df37cc9f..dd4a36ff4356 100644 --- a/arch/riscv/include/asm/switch_to.h +++ b/arch/riscv/include/asm/switch_to.h @@ -70,6 +70,13 @@ static __always_inline bool has_fpu(void) { return false; } #define __switch_to_fpu(__prev, __next) do { } while (0) #endif +static inline void __switch_to_envcfg(struct task_struct *next) +{ + asm volatile (ALTERNATIVE("nop", "csrw " __stringify(CSR_ENVCFG) ", %0", + 0, RISCV_ISA_EXT_XLINUXENVCFG, 1) + :: "r" (next->thread_info.envcfg) : "memory"); +} + extern struct task_struct *__switch_to(struct task_struct *, struct task_struct *); @@ -103,6 +110,7 @@ do { \ __switch_to_vector(__prev, __next); \ if (switch_to_should_flush_icache(__next)) \ local_flush_icache_all(); \ + __switch_to_envcfg(__next); \ ((last) = __switch_to(__prev, __next)); \ } while (0) diff --git a/arch/riscv/include/asm/thread_info.h b/arch/riscv/include/asm/thread_info.h index fca5c6be2b81..c74536194626 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/cpufeature.c b/arch/riscv/kernel/cpufeature.c index 0139d4ea8426..f7fcd23d55de 100644 --- a/arch/riscv/kernel/cpufeature.c +++ b/arch/riscv/kernel/cpufeature.c @@ -922,7 +922,7 @@ unsigned long riscv_get_elf_hwcap(void) void riscv_user_isa_enable(void) { if (riscv_has_extension_unlikely(RISCV_ISA_EXT_ZICBOZ)) - csr_set(CSR_ENVCFG, ENVCFG_CBZE); + current->thread_info.envcfg |= ENVCFG_CBZE; else if (any_cpu_has_zicboz) pr_warn_once("Zicboz disabled as it is unavailable on some harts\n"); } From patchwork Thu Sep 12 23:16:24 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13802828 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 41338EEE271 for ; Thu, 12 Sep 2024 23:17:32 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C31836B0099; Thu, 12 Sep 2024 19:17:31 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id BE0246B009A; Thu, 12 Sep 2024 19:17:31 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9E63C6B009B; Thu, 12 Sep 2024 19:17:31 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 7FA4F6B0099 for ; Thu, 12 Sep 2024 19:17:31 -0400 (EDT) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 283BEC0C2B for ; Thu, 12 Sep 2024 23:17:31 +0000 (UTC) X-FDA: 82557649902.05.98EB82B Received: from mail-pj1-f48.google.com (mail-pj1-f48.google.com [209.85.216.48]) by imf04.hostedemail.com (Postfix) with ESMTP id 51F564000D for ; Thu, 12 Sep 2024 23:17:29 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=htAqUFlr; spf=pass (imf04.hostedemail.com: domain of debug@rivosinc.com designates 209.85.216.48 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1726182995; a=rsa-sha256; cv=none; b=BaZtB9r6U8q3uB6lD22hJWRhpdzvdrpCwbnQwQLeO4k8kwvOUEfTSJlh5+KIUppL131cAc ftYr+UUNVkXdoOJAYu1pa1uPL59HP1YWaujW6axZHtaRrQgmr9Ng5EkUbiGQ7DPrw7QcQb Q7LCztHET2ausZSVOldr6HIBbo8u2Oc= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=htAqUFlr; spf=pass (imf04.hostedemail.com: domain of debug@rivosinc.com designates 209.85.216.48 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=1726182995; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=KDHpycfSyd14eKEYNZ2Tiwh37NTkssfzdaJprWVmYic=; b=R3JXQuBvOa6m1Xn+z1SFc2mWO64S8Io4PzvhNDpJ7FpRrMogG6CDzp2YVS7vXqbWXlkcDk etNL0F5zw5wJSTBR6WKOUfvm3fUDDDC/jFeNs6nXMFgB3g/0x6KN4mjlCxw9S9o3tA7sGr /PfI6WPYPs0AyIJ6z1t0idEZiQzl0hM= Received: by mail-pj1-f48.google.com with SMTP id 98e67ed59e1d1-2d8b96c18f0so1265412a91.2 for ; Thu, 12 Sep 2024 16:17:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1726183048; x=1726787848; darn=kvack.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=KDHpycfSyd14eKEYNZ2Tiwh37NTkssfzdaJprWVmYic=; b=htAqUFlrOZAwzYLx+W9luBgr5aWOpec+6pJuJSrH8NDYix2ij38SzFIQRhPrwD9bfR 4SLmCbTQtJk+VSkf4lUbUlXT3h9JFH6TbHHj8l5p11ShGfqspqmYIqPjSqHnYOKqWzPU ua28CaRyuV3HROi4VXv9slzXvLqxvcZqo1dhO1zm41cgLpbhxBSwUeBrMLsHR1u2QNsi 70pvIXcVVGqRQcFMCF+YvheDDnpuyQrhfwX/H+8+zWJofEl21ZM/d17f9+YKObMHO39z RLgFy2RphLN3/Qdwza+Xpxp7R/DeRaSqYRFM/jCVOUIcv5q2FFQVoskR7xB5SjvjtsiL EhqQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726183048; x=1726787848; 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=KDHpycfSyd14eKEYNZ2Tiwh37NTkssfzdaJprWVmYic=; b=WBOGwpDo9KP6e6WCTEtcc7nwqARHTQr4ZngKveLKPIhwrrTvXqSSrreKiutgQneuUS sF7uVaLNA8JT1Zt28jpuSN8lXzA/2uFGj8qPXY9MuK8YG25dANbtEqQiEWhLFdee00z+ FVtIVjgidrJSQrQ63OGWiVkCY4b1O8+Vy0QkiI9F5qEohcPydQjLZTAfntkUA5yo9rVA /i6FUYJXgjpbvNrMmvAmYroLnSuc78Zx3OBUZ2qHg1DJpmDNzwVOwesAbABP3fipIFMI M2w9UaBs8WHqyR4SDAXcz2qtS8OmxMoGQR6S1LPqOXUzaeE/eHzosbk8AlOZ+9A1VTd6 wzGw== X-Forwarded-Encrypted: i=1; AJvYcCXEACqpf36G9V1H+N4cXc4PRdpwpbWZOGYOHdnALLrnggqnb2IAFs4KPMsBzGgN59gaITyk48ZCPw==@kvack.org X-Gm-Message-State: AOJu0YyzUDjpUQnpfJR5Q3NdsBfp5ZH76Vl2x7udqRSenFkb3nUvs9Lx GFvMoT9RJcvWYQB+3XcEGcVeocuG3AEaXcfeL1xddoNcTsZLv6K6J4r0hCiQZE0= X-Google-Smtp-Source: AGHT+IGgw8wuv6wUiP6Uqm3H0s61sB80F1WhERdlqMkwOmwPP9ITQsqy/bvbAfvC2aqbyGgjaY4ahg== X-Received: by 2002:a17:90a:f2c9:b0:2da:6a4d:53a6 with SMTP id 98e67ed59e1d1-2db9ffb3672mr5841992a91.19.1726183047961; Thu, 12 Sep 2024 16:17:27 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2db6c1ac69asm3157591a91.0.2024.09.12.16.17.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 12 Sep 2024 16:17:27 -0700 (PDT) From: Deepak Gupta To: paul.walmsley@sifive.com, palmer@sifive.com, conor@kernel.org, linux-doc@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kselftest@vger.kernel.org Cc: corbet@lwn.net, palmer@dabbelt.com, aou@eecs.berkeley.edu, robh@kernel.org, krzk+dt@kernel.org, oleg@redhat.com, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, hpa@zytor.com, peterz@infradead.org, akpm@linux-foundation.org, arnd@arndb.de, ebiederm@xmission.com, kees@kernel.org, Liam.Howlett@oracle.com, vbabka@suse.cz, lorenzo.stoakes@oracle.com, shuah@kernel.org, brauner@kernel.org, samuel.holland@sifive.com, debug@rivosinc.com, andy.chiu@sifive.com, jerry.shih@sifive.com, greentime.hu@sifive.com, charlie@rivosinc.com, evan@rivosinc.com, cleger@rivosinc.com, xiao.w.wang@intel.com, ajones@ventanamicro.com, anup@brainfault.org, mchitale@ventanamicro.com, atishp@rivosinc.com, sameo@rivosinc.com, bjorn@rivosinc.com, alexghiti@rivosinc.com, david@redhat.com, libang.li@antgroup.com, jszhang@kernel.org, leobras@redhat.com, guoren@kernel.org, samitolvanen@google.com, songshuaishuai@tinylab.org, costa.shul@redhat.com, bhe@redhat.com, zong.li@sifive.com, puranjay@kernel.org, namcaov@gmail.com, antonb@tenstorrent.com, sorear@fastmail.com, quic_bjorande@quicinc.com, ancientmodern4@gmail.com, ben.dooks@codethink.co.uk, quic_zhonhan@quicinc.com, cuiyunhui@bytedance.com, yang.lee@linux.alibaba.com, ke.zhao@shingroup.cn, sunilvl@ventanamicro.com, tanzhasanwork@gmail.com, schwab@suse.de, dawei.li@shingroup.cn, rppt@kernel.org, willy@infradead.org, usama.anjum@collabora.com, osalvador@suse.de, ryan.roberts@arm.com, andrii@kernel.org, alx@kernel.org, catalin.marinas@arm.com, broonie@kernel.org, revest@chromium.org, bgray@linux.ibm.com, deller@gmx.de, zev@bewilderbeest.net, Conor Dooley Subject: [PATCH v4 05/30] riscv: Call riscv_user_isa_enable() only on the boot hart Date: Thu, 12 Sep 2024 16:16:24 -0700 Message-ID: <20240912231650.3740732-6-debug@rivosinc.com> X-Mailer: git-send-email 2.45.0 In-Reply-To: <20240912231650.3740732-1-debug@rivosinc.com> References: <20240912231650.3740732-1-debug@rivosinc.com> MIME-Version: 1.0 X-Stat-Signature: i1b85dimda17m4x65a4tmhppu6ccgxck X-Rspamd-Queue-Id: 51F564000D X-Rspam-User: X-Rspamd-Server: rspam10 X-HE-Tag: 1726183049-276695 X-HE-Meta: U2FsdGVkX1+YC8jRAkFOczZ0VH1OSAMgU8Lv20txaY5ik7YUEF714jU3dGllVWr7Bre1ZZ3QveVLsbt4fe7k96CJqdWg78pqsY2cDKjqN30H+e6V8BG5nvn1TODLdo78oKzFlt9TpTlhzvMFH92k//HLkTaUG5JAcM7kPJOZZQim792vZho4Gh3/w9dvW8b7gA5m6HWQQf7mYOhe3UovApGsW/ZY/YiUbOdJq4n7zUlQsnUFy9d50nadoY6Uw+XefAZkvYT8i4kotF0/FZ/pj3rulHp9+3i0ciE5gcS/HaxlXf+GLM4TsRU0HoqEdWlW9Bo4MPVirLgrNMCJG6bJa1FroM1wSJOAKkKpVOt/ytLIv6Fref1zY45q6VwRitThMNmLoWM1Kvc6vDwVQAxptVq37elmQMjkXeendYoYRiNKkJbNa8BsukEF5I5J3lV2IPqTa8mDKL96RHi8Ewr0C+BqSgvXtAYOIDekR5CVcfFw4P574uZkZgR9JEjNRNRuUCOm3zptd2t3ju1Gajp+txvRwL+pB6yzo/WcqPsFZE44Un8LZrUsFU2uBL4nMxFHQcg6yRNUsPFY3dh27vj3bEiHohpNOPNmShx3O8P21mm0rxYmOWFnN6tnwWdBFrdUFYvVp9Nhwqc92KRfcjKiyXLFtrPHw15urgrGWN5AbzVFab94ChB71GERC+P41BGUUdZJ8Ay5LCnYUj6LThm9hcofXoY4m7mCu6zgx77GKf3EwzFvOyTplZGDsJeAV6lQNQi2eh5EZSCeBnsP5pCIrdOTRQoRSuFGZ8TBw+2RkkhVEJilsJXThRU3t0JZohy4Zgg51kdtqUvKdSqM8YQbY8BYxYz92SKgLMPMFWZ0e7Uyga7f8H1IjN7auAr/aQNHb7zwhtilJ/jK3qPZkYRr3NupUlb/eqZ03xlubE29UzBwFh3w3cDZWbvDgu+krPfnKSPRqgnacnLKhRp9u1h grgpA27M IQzw18YPnjstZWaSttScii/eZzmlnEJdoLzVucDbnKEAsV9twhslKn+ZieVj8n25OL2j92NWJbrEulsTHFX/F/wrGT3qKz7gIRerSy4BXd6OvGPexdqui+JTWWnPTW6IScj0Vr5MKnEfQhMF5zt4vyUblnVR9yXoaqPgIJhrO3LkbTLwROcYG76TKoyfZ8FgGGB7INr0+OvK2Z5CfRz6iYJgqXn/XPZK0/Hf7PxbP09q4yFdzIcu83yaRlcucIEz1EbVcHwUKUHDZKpKwnySti8YjInQlBwMgRutwNGd1ri5328sUPO0ncwBP+yNuxzk97qE52bNyVpjdtp4X1RBkt8krthLyMSMwslPl1Vfu4PdxZKE+9cO07NJVL1+2u+6VxSVHD9WeRxpNjX6JTAWAqvfkpkpsLJx5w6XSM6xx6D0qmJvAUOkaTKu0dRzdzcarG+wH+WaAzpqAcgxWzTnt1QLG8B8I7Vshju0rODAjZoSFWft8tFB+DsvLroaeds2GiTT59g8BIFklyejzXkHI6ArL82NKcrbbrZQGegSW0r0QO6ySLByUH8tejZRRa6hWH9yZdoJNcfc1Zhn3a0LLufrD8YkWbWUe9TCxwSv9AG2vqlHQ+5rzhqWfw5Mn4vT4BJFgmZJx71tu0uZy6HLrTLsGJfOVBEwtD7pEIemKiTGUL5o= 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: Samuel Holland Now that the [ms]envcfg CSR value is maintained per thread, not per hart, riscv_user_isa_enable() only needs to be called once during boot, to set the value for the init task. This also allows it to be marked as __init. Reviewed-by: Andrew Jones Reviewed-by: Conor Dooley Reviewed-by: Deepak Gupta Signed-off-by: Samuel Holland Signed-off-by: Deepak Gupta --- arch/riscv/include/asm/cpufeature.h | 2 +- arch/riscv/kernel/cpufeature.c | 4 ++-- arch/riscv/kernel/smpboot.c | 2 -- 3 files changed, 3 insertions(+), 5 deletions(-) diff --git a/arch/riscv/include/asm/cpufeature.h b/arch/riscv/include/asm/cpufeature.h index 45f9c1171a48..ce9a995730c1 100644 --- a/arch/riscv/include/asm/cpufeature.h +++ b/arch/riscv/include/asm/cpufeature.h @@ -31,7 +31,7 @@ DECLARE_PER_CPU(struct riscv_cpuinfo, riscv_cpuinfo); /* Per-cpu ISA extensions. */ extern struct riscv_isainfo hart_isa[NR_CPUS]; -void riscv_user_isa_enable(void); +void __init riscv_user_isa_enable(void); #define _RISCV_ISA_EXT_DATA(_name, _id, _subset_exts, _subset_exts_size, _validate) { \ .name = #_name, \ diff --git a/arch/riscv/kernel/cpufeature.c b/arch/riscv/kernel/cpufeature.c index f7fcd23d55de..41fd0be25bd8 100644 --- a/arch/riscv/kernel/cpufeature.c +++ b/arch/riscv/kernel/cpufeature.c @@ -919,12 +919,12 @@ unsigned long riscv_get_elf_hwcap(void) return hwcap; } -void riscv_user_isa_enable(void) +void __init riscv_user_isa_enable(void) { if (riscv_has_extension_unlikely(RISCV_ISA_EXT_ZICBOZ)) current->thread_info.envcfg |= ENVCFG_CBZE; else if (any_cpu_has_zicboz) - pr_warn_once("Zicboz disabled as it is unavailable on some harts\n"); + pr_warn("Zicboz disabled as it is unavailable on some harts\n"); } #ifdef CONFIG_RISCV_ALTERNATIVE diff --git a/arch/riscv/kernel/smpboot.c b/arch/riscv/kernel/smpboot.c index 0f8f1c95ac38..e36d20205bd7 100644 --- a/arch/riscv/kernel/smpboot.c +++ b/arch/riscv/kernel/smpboot.c @@ -233,8 +233,6 @@ asmlinkage __visible void smp_callin(void) numa_add_cpu(curr_cpuid); set_cpu_online(curr_cpuid, true); - riscv_user_isa_enable(); - /* * Remote cache and TLB flushes are ignored while the CPU is offline, * so flush them both right now just in case. From patchwork Thu Sep 12 23:16:25 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13802829 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 A99CEEEE270 for ; Thu, 12 Sep 2024 23:17:36 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3A4DD6B009B; Thu, 12 Sep 2024 19:17:36 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 32F186B009C; Thu, 12 Sep 2024 19:17:36 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 180486B009D; Thu, 12 Sep 2024 19:17:36 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id EA8A76B009B for ; Thu, 12 Sep 2024 19:17:35 -0400 (EDT) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 9A636120CCE for ; Thu, 12 Sep 2024 23:17:35 +0000 (UTC) X-FDA: 82557650070.21.F4F196E Received: from mail-pj1-f42.google.com (mail-pj1-f42.google.com [209.85.216.42]) by imf08.hostedemail.com (Postfix) with ESMTP id BFBE4160019 for ; Thu, 12 Sep 2024 23:17:33 +0000 (UTC) Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=s1Hv0Grk; spf=pass (imf08.hostedemail.com: domain of debug@rivosinc.com designates 209.85.216.42 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=1726182948; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=OL3ZuDMqCGbJryHQ73fpa2Z59erwJkwOpRXQ2rxwv1I=; b=7FJxHjGW0ZXqLV/PgH3YfIjTMbfnEw4erV8caN0yNqTR4nQB5GcUCSwgnKonGvVT5I5e+u 3bzIjsEqEHWUyF38PVF1BgOQIzyWMLZ4Ia7r7NtfN86mUxfPcrkE8pjmLx71/71SrkyO5v HFJZjkSpbgzx2qJvMHzFVt6pf7D/lWA= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1726182948; a=rsa-sha256; cv=none; b=7WQH9vqoDC1/Rc6md3+Qk1nF+x5bk6XUvnYKC2ndtgPMazKv7SXZ0850bvwyb+7vuuX2Zj q//sBCfvJPIvJhtLBjlmOlcl3lpbmRe6gDnvW7qor/vs9Z2vZT+6w/MIfMye80kjEv6iqj LF4yZRkiW7Dg2j6nHVNLzg0leB6lN6Q= ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=s1Hv0Grk; spf=pass (imf08.hostedemail.com: domain of debug@rivosinc.com designates 209.85.216.42 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none Received: by mail-pj1-f42.google.com with SMTP id 98e67ed59e1d1-2d8a7c50607so1075055a91.1 for ; Thu, 12 Sep 2024 16:17:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1726183052; x=1726787852; darn=kvack.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=OL3ZuDMqCGbJryHQ73fpa2Z59erwJkwOpRXQ2rxwv1I=; b=s1Hv0GrkslvMRDptQ+EkW0W7T18CkQDYyCrdNsTSsxEedD5E8rI7nolqlclc0aSIYY dD8itNqHxNcO6si/Qxmuu5adotfZ28XllGiCy6MrHFUMJGqBI2lMT11Y/gnjDCogjzSC NemutaEuzIzIfIwsPgIfzVcKZ1hrXKFVek74zTU8U8TYLjnCaNvUxtNTbhDCFpamTcSE yj+Ocqw6qNThIV1xcoy+8jSI1y8NAfuE4Y6jhnkGvRqUWgFbm77dKLoh+UHUFHfkDcpf HWIcztRYlZ3xh7kJcZPPUOrjxkFm2PnNQMX8s2mf3qONLBbYSc6bdh4/s4kRGhauNkZk M2fw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726183052; x=1726787852; 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=OL3ZuDMqCGbJryHQ73fpa2Z59erwJkwOpRXQ2rxwv1I=; b=KcGYztToGBxS8PxKEpozCsM9rdojUhK48o/cBRU+OOEH1Yk7jifXRLwN02KelOI/QP /VKENVxZKBXMUHvZfdZ5U8zQkKkiggzmp7q4xolv0DxGydHI2Cj3eS9yrb53iFkg6Ko1 r+BjLAiM3bJw9wm5hDbo3ygf7h0fs9IrEbqoVdwRySfFog4XbyE251IqQqeiHvN5uDJD WGtj9O3qQ6kNv2nULlLN9dMrEURElDjsFLZHdHiKwK6GmgiLMjS1mQCmevT8gR0/mw4n UDw8u7ziyGjuZJb+/SZTsDj6cGRQRuQ/m7hnwG9OnR0UJrLpcOf0XHzlJpBSOlvUGz7E ppMA== X-Forwarded-Encrypted: i=1; AJvYcCVOwYPdotIn6MgOtpW0G2TAV2rpBAbo7ZCIC8QSyWHD/qigsre6xBo/r3Wz7okJKUkHNcZDJ9OSgQ==@kvack.org X-Gm-Message-State: AOJu0Yz1eaUyU9flN865s9ozRuB1B1PDcuLA4tN9921uk4NDfWb6XT7q DA0r+OmCzuhvNBjLsqBi/nuLaCLjNRY5lg5LQtWTJdNLGyIU2ZFibAQBNmg5SUg= X-Google-Smtp-Source: AGHT+IE9fam6IfuuZkZ+ZvbmFrswnqok3RLTHJaic61nQCF+zeo3Ji5hunRDoKXFzuz+ce22C8cSog== X-Received: by 2002:a17:90b:1889:b0:2d3:c4d3:de19 with SMTP id 98e67ed59e1d1-2db9fec65ddmr5214534a91.0.1726183052278; Thu, 12 Sep 2024 16:17:32 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2db6c1ac69asm3157591a91.0.2024.09.12.16.17.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 12 Sep 2024 16:17:31 -0700 (PDT) From: Deepak Gupta To: paul.walmsley@sifive.com, palmer@sifive.com, conor@kernel.org, linux-doc@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kselftest@vger.kernel.org Cc: corbet@lwn.net, palmer@dabbelt.com, aou@eecs.berkeley.edu, robh@kernel.org, krzk+dt@kernel.org, oleg@redhat.com, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, hpa@zytor.com, peterz@infradead.org, akpm@linux-foundation.org, arnd@arndb.de, ebiederm@xmission.com, kees@kernel.org, Liam.Howlett@oracle.com, vbabka@suse.cz, lorenzo.stoakes@oracle.com, shuah@kernel.org, brauner@kernel.org, samuel.holland@sifive.com, debug@rivosinc.com, andy.chiu@sifive.com, jerry.shih@sifive.com, greentime.hu@sifive.com, charlie@rivosinc.com, evan@rivosinc.com, cleger@rivosinc.com, xiao.w.wang@intel.com, ajones@ventanamicro.com, anup@brainfault.org, mchitale@ventanamicro.com, atishp@rivosinc.com, sameo@rivosinc.com, bjorn@rivosinc.com, alexghiti@rivosinc.com, david@redhat.com, libang.li@antgroup.com, jszhang@kernel.org, leobras@redhat.com, guoren@kernel.org, samitolvanen@google.com, songshuaishuai@tinylab.org, costa.shul@redhat.com, bhe@redhat.com, zong.li@sifive.com, puranjay@kernel.org, namcaov@gmail.com, antonb@tenstorrent.com, sorear@fastmail.com, quic_bjorande@quicinc.com, ancientmodern4@gmail.com, ben.dooks@codethink.co.uk, quic_zhonhan@quicinc.com, cuiyunhui@bytedance.com, yang.lee@linux.alibaba.com, ke.zhao@shingroup.cn, sunilvl@ventanamicro.com, tanzhasanwork@gmail.com, schwab@suse.de, dawei.li@shingroup.cn, rppt@kernel.org, willy@infradead.org, usama.anjum@collabora.com, osalvador@suse.de, ryan.roberts@arm.com, andrii@kernel.org, alx@kernel.org, catalin.marinas@arm.com, broonie@kernel.org, revest@chromium.org, bgray@linux.ibm.com, deller@gmx.de, zev@bewilderbeest.net Subject: [PATCH v4 06/30] riscv/Kconfig: enable HAVE_EXIT_THREAD for riscv Date: Thu, 12 Sep 2024 16:16:25 -0700 Message-ID: <20240912231650.3740732-7-debug@rivosinc.com> X-Mailer: git-send-email 2.45.0 In-Reply-To: <20240912231650.3740732-1-debug@rivosinc.com> References: <20240912231650.3740732-1-debug@rivosinc.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: BFBE4160019 X-Stat-Signature: rkoaq3m6q598omzcbbwdoyw88b3uquig X-HE-Tag: 1726183053-691825 X-HE-Meta: U2FsdGVkX1+GhENkisdPuLo25d6qGVPnasEyQN6NQkSpwS0SiPKCSH7QBk+kCjQcIyYLBWeuA7Z8oRKm+B0TPpeA4ZQE+cL/tO/Oujl8QrYEcT+j4Z2mRB39RDm1d5RlRrI59BqeaGkAZnghFDA/JAFiX6BNhC4HK28t8LVLDyF8sPt/56ZupAryf+yQMhQbsYBoGtHVi5B7vYDMpiFklCT6ra6DstVYEg7wBv3ww/6hLc29XedFGCP0QFjzDV/4S0Wa1Ftu+PvN1JqZ1jaMkvTaxw1Sjo/Kgmk/EWUsH/qu4NEcUTjRY5c/k5IImt2Sil1isZHQHMTUlhJIsxUTjTyr9zIvgr5D5HS724icnr/oNOO2PKwISc1/s004L/P0at0SfiIyQHFKUdfrwJYhsvVvZx0fnAASSM1dFBQzqpLi/djlpNQ/U5+UAaLu9cNJzrPuLd2MwDy6Q2OoI0vrZP8fMK5Neagbvs2oGpHTpHpnyY+WuvBZvlxffMPQffLLiCFlQZuIBQIryHpAS2lLfFUKRmli1WrEkfTUIg2lKhoxLy9QM9FvtraOjzdaTBgArYCZBGChpNsgHGlJ/7c3bS8aTxTaUiUduVWNoEB+s02pQ1NxVRdqciSCD3cKOZokf334PvQsKv0bcn5KA3m3C1DJWr0Deu7tz3hJxmSoxBqlGxGqLO+xMOS4gRXc0YLL8RwedRDWlGo6SB5NLNtJLACIv9VfYqR3uhygrYcPZg1Urmnl/k+E5YFyNhPkfHrU//nnolWngT2GNjrKoSeup7XfCZFIkNG6PjFnt9OtXpe/k2AA0oKAuMnvUEXjeykEsai6IsdiIIZVaJyyStOZpS4KDR7peBa4qN5Uop5vZqZxaKRoDrkOEm8bofCXevn/z8pTvyQ2fqEBJQ3XXdUszPktRKpwRaPnoetNZRufUkNV8+aJUDU36M3D6Y8kr92iTdwet1VjgoI7o/fOC+3 PDHtJOzN nlLq9DuTDYG/+fpABpYXdLvHO1XyNaFjfDbU/KE5bi/eIrmAvQI5ayIwTGAD2kDjZ3cTwRW7VzfZpQCsI6oWP5yoN84ozDPwgoRc3uIs2itR78pYhasm9U0Sdler6N2hK9NO/nbn1mvz8aliPqsYcRuvcS3w9gHr3BB/hdRvNWBRIinrlzbWUiUfUeBWHcDO9qquDhJ18N2CdmOUedEPsycy+U0naQGyF8UGof7MmEmUxe2rqEfE3zYHvBUb/Y1k7HuyGpM4Yt+96FuBE033GD4YCcfCxMm3ga36SCDHOKcC9546FMzR+iD9WvQ8eLEehqYZpIoXPEHmx1f30sFRGYs9m5QZ18twBMSCm6lz2maLFhAh+aXaXUPJZxPeRUs3FQ2OAgg3XZNV1ifYsa8vgTATTHuvZfN29LAM2Tcy0gBFGCK/ns//WiZgsH5NTKpYM32QTgv3CD6EEvNtAzFvck8YHg+UQW4TUM1aLHEAUX1ZprWOWCJfMc9mGUA== 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: riscv will need an implementation for exit_thread to clean up shadow stack when thread exits. If current thread had shadow stack enabled, shadow stack is allocated by default for any new thread. Signed-off-by: Deepak Gupta Reviewed-by: Charlie Jenkins --- arch/riscv/Kconfig | 1 + arch/riscv/kernel/process.c | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/arch/riscv/Kconfig b/arch/riscv/Kconfig index 0f3cd7c3a436..d1d629a3eb91 100644 --- a/arch/riscv/Kconfig +++ b/arch/riscv/Kconfig @@ -177,6 +177,7 @@ config RISCV select HAVE_SAMPLE_FTRACE_DIRECT_MULTI select HAVE_STACKPROTECTOR select HAVE_SYSCALL_TRACEPOINTS + select HAVE_EXIT_THREAD select HOTPLUG_CORE_SYNC_DEAD if HOTPLUG_CPU select IRQ_DOMAIN select IRQ_FORCED_THREADING diff --git a/arch/riscv/kernel/process.c b/arch/riscv/kernel/process.c index e4bc61c4e58a..9b66dc07c3d2 100644 --- a/arch/riscv/kernel/process.c +++ b/arch/riscv/kernel/process.c @@ -192,6 +192,11 @@ int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src) return 0; } +void exit_thread(struct task_struct *tsk) +{ + +} + int copy_thread(struct task_struct *p, const struct kernel_clone_args *args) { unsigned long clone_flags = args->flags; From patchwork Thu Sep 12 23:16:26 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13802830 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 CED4BEEE271 for ; Thu, 12 Sep 2024 23:17:40 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 664646B009D; Thu, 12 Sep 2024 19:17:40 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 5EDA56B009E; Thu, 12 Sep 2024 19:17:40 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 418336B009F; Thu, 12 Sep 2024 19:17:40 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 20FE16B009D for ; Thu, 12 Sep 2024 19:17:40 -0400 (EDT) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id D787D160CC3 for ; Thu, 12 Sep 2024 23:17:39 +0000 (UTC) X-FDA: 82557650238.07.5DD3834 Received: from mail-pj1-f51.google.com (mail-pj1-f51.google.com [209.85.216.51]) by imf14.hostedemail.com (Postfix) with ESMTP id 054D210000D for ; Thu, 12 Sep 2024 23:17:37 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=HlhJl5Bk; spf=pass (imf14.hostedemail.com: domain of debug@rivosinc.com designates 209.85.216.51 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1726183004; a=rsa-sha256; cv=none; b=bVb9MB7GMqYPGKZ5jyvrY3WToShJBNvua8kDx9BI9mRSB5P2WXC5LavlhN5kK1LUtGnCWN TblTW0MIJhOYRaOJnmsSnA6GURe9u08Xe6C0et/kXQ7S6fE4lYF4gNrdT0qeGdHtsrVUy5 xsI8fJ9XflKJP6TAYt+sUuvaUXVzzu8= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=HlhJl5Bk; spf=pass (imf14.hostedemail.com: domain of debug@rivosinc.com designates 209.85.216.51 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=1726183004; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=veWGRHreCjgq8h7I/yOaDBNsZmlAimjJ+4EKqlz5IR0=; b=Seio6aq7112BQhPxTotcLZJN4dsMhxA1kdfns+YMl6W7XcNCWv7fkxhM2Gp4R/7Zy2F/Hb PBsZdNZYUIfyeozC6s3kV44BrfVlRgc+/kxwui4yNFN2QxZG8UApxSFH2NlN9/a2vpgT8d rya6nz2c5FSLG0U+8FrGh0OyrNd9H0s= Received: by mail-pj1-f51.google.com with SMTP id 98e67ed59e1d1-2d88edf1340so289034a91.1 for ; Thu, 12 Sep 2024 16:17:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1726183057; x=1726787857; darn=kvack.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=veWGRHreCjgq8h7I/yOaDBNsZmlAimjJ+4EKqlz5IR0=; b=HlhJl5BkpDkxhMPr0Iro+eIL2993wN8bOfX6Lvb0ofSzT8W37IWXlHkPSKixNmdI/z HqBW9zRf2m3MtD256dIs1Le+wohTXAnhjesr+PmAIZmIZM277TV0vd4S5BXxKsyiYij1 pWe0ZJoYRdyipDavnmvSzcOdQ+mopnHHk3OPG6/KynJBhepZxDSolGv8aFdVnTP1BhQa 4iC+b50XuWmHEAGLz20z2l0nhGZWs8WZht5MJVBYA16SOpFfIZE2Ifce1ZJMLh/ovfjh h4+0ez/+YOgF9LTlPa1xDIXvqTxnmyGwcMIZMpMJvVAImsWWEp+TSmYmAaZ2RhPi+T18 UedA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726183057; x=1726787857; 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=veWGRHreCjgq8h7I/yOaDBNsZmlAimjJ+4EKqlz5IR0=; b=Fke6JLZhsR6kZTfs3DPvcnWzGWr2Vlx3lydZR6qlPDp29FdkdV4KgfhGDjO0s0aMJ5 Qh+Tvr9wxQNa5OiWRN5VCg9Fhpi0qTfkI1K/tny2VlyZK1BoH4x7lkPn5myjR2Cwlag9 uwlwIQnsNc2uSwKCv7wVxKzTJmgtwtEwprQldP/NOIpp02E2o9Aef4J/1GOkVA/EKF3r bnZKYipK28kYai0uyuQWzoDC+2jD5fDV6JB8k8l0ftYd8xaDqqqtoH3KP1l+CL5HGi6l K8Odm7/Pe1O/koKs9lVgRvtpjyz51/8uN9CfaoUwre3aa3E5h3+b+isz2VnO/cmaWPeI 8A9Q== X-Forwarded-Encrypted: i=1; AJvYcCXcHwBt8Zb9a1H4n2l4nm/PStOo1Xy1Z6uajV1Xd2bYD7s1kx9ofNgSXM7zLV0wfog83Lzlgq0iEQ==@kvack.org X-Gm-Message-State: AOJu0YyHzrHJLqehZspwBIDorowvyV4+kxvXoCZCYz0DIGQGQ2km3ul1 gvXApPAHFtLNl78wd5mQYgurclZ++S59BouOe6O+Q8vG3Dgcx3ZaZ3oezbIw9HM= X-Google-Smtp-Source: AGHT+IGi2PGJAtalAbRDIiYrcdX60Gd/jBapQHqvC1T650woNs+55FVEzfftOtbKP4fBLGwK941M2A== X-Received: by 2002:a17:90b:1c88:b0:2d8:9a0c:36c0 with SMTP id 98e67ed59e1d1-2dbb9dc0f39mr1063170a91.8.1726183056629; Thu, 12 Sep 2024 16:17:36 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2db6c1ac69asm3157591a91.0.2024.09.12.16.17.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 12 Sep 2024 16:17:36 -0700 (PDT) From: Deepak Gupta To: paul.walmsley@sifive.com, palmer@sifive.com, conor@kernel.org, linux-doc@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kselftest@vger.kernel.org Cc: corbet@lwn.net, palmer@dabbelt.com, aou@eecs.berkeley.edu, robh@kernel.org, krzk+dt@kernel.org, oleg@redhat.com, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, hpa@zytor.com, peterz@infradead.org, akpm@linux-foundation.org, arnd@arndb.de, ebiederm@xmission.com, kees@kernel.org, Liam.Howlett@oracle.com, vbabka@suse.cz, lorenzo.stoakes@oracle.com, shuah@kernel.org, brauner@kernel.org, samuel.holland@sifive.com, debug@rivosinc.com, andy.chiu@sifive.com, jerry.shih@sifive.com, greentime.hu@sifive.com, charlie@rivosinc.com, evan@rivosinc.com, cleger@rivosinc.com, xiao.w.wang@intel.com, ajones@ventanamicro.com, anup@brainfault.org, mchitale@ventanamicro.com, atishp@rivosinc.com, sameo@rivosinc.com, bjorn@rivosinc.com, alexghiti@rivosinc.com, david@redhat.com, libang.li@antgroup.com, jszhang@kernel.org, leobras@redhat.com, guoren@kernel.org, samitolvanen@google.com, songshuaishuai@tinylab.org, costa.shul@redhat.com, bhe@redhat.com, zong.li@sifive.com, puranjay@kernel.org, namcaov@gmail.com, antonb@tenstorrent.com, sorear@fastmail.com, quic_bjorande@quicinc.com, ancientmodern4@gmail.com, ben.dooks@codethink.co.uk, quic_zhonhan@quicinc.com, cuiyunhui@bytedance.com, yang.lee@linux.alibaba.com, ke.zhao@shingroup.cn, sunilvl@ventanamicro.com, tanzhasanwork@gmail.com, schwab@suse.de, dawei.li@shingroup.cn, rppt@kernel.org, willy@infradead.org, usama.anjum@collabora.com, osalvador@suse.de, ryan.roberts@arm.com, andrii@kernel.org, alx@kernel.org, catalin.marinas@arm.com, broonie@kernel.org, revest@chromium.org, bgray@linux.ibm.com, deller@gmx.de, zev@bewilderbeest.net Subject: [PATCH v4 07/30] riscv: zicfilp / zicfiss in dt-bindings (extensions.yaml) Date: Thu, 12 Sep 2024 16:16:26 -0700 Message-ID: <20240912231650.3740732-8-debug@rivosinc.com> X-Mailer: git-send-email 2.45.0 In-Reply-To: <20240912231650.3740732-1-debug@rivosinc.com> References: <20240912231650.3740732-1-debug@rivosinc.com> MIME-Version: 1.0 X-Stat-Signature: 1aweonatywn6ien1uz6gfye6ys69p8jq X-Rspamd-Queue-Id: 054D210000D X-Rspam-User: X-Rspamd-Server: rspam10 X-HE-Tag: 1726183057-157293 X-HE-Meta: U2FsdGVkX19lSWyBdHlCyya3T6ip76qd/kgW7siEDTK7+D+EggTvqbzosuJ5SPEcQfSitqx6YszbefPj8Qu5QDmk4Y7I4MqUfjdkU1HEULADYf1mUTbJ4jyoslRBk6W59N+Govt+CuLj+wzkEapqpzgRILn+0CuhsJyaFqEJUrsc/jVb3G8Y9l0pBxiFAeC0l2ZSecqdz/Pd5HxbGG96Ok/ATwcrEPkq5g/LD7NbH4bgBa0u8a0S56oJ1DdgwgNWs0LznyZpgbzqmL5yya91Kh2b1qHX5szOR29GvLQBSpeDdVlc57VTAWmZ+he5tIq6FKdeUgRklfP+N1Y1Vf+amu0LDBCH4dxChTO0iedXBKlTY6KLctyRp8FrwxqGXIL1RbYHY1GAJSdbWUs5UaDxqlcKbX3QxX/Ui+lNar0OBx1UJQPjbFHMSJQ/Xh9drugLYnwFCGGxIDB7NGmkoN6zLsRIYnQlWrx35iNqGRrS2iOMCwOdUU5hF4xmxXcpM+pdUZtuW50DSwcDLpsUwUvmNNxj1DHv+Ae6sBZj0r5xNy5X4oNBVWLvg8tILItBQc3G09wRRh+Sv10zju+ZMaPnQAlvT1OIxAJMs8+3C2gPESpfOf9GOFUAuZqhlpCqKfq3dJ+1ctmb75QiLtl5qmYi4s0Sza1ZOdUjIh2zc9aWOZqjw6cr3kz0lEs2IccirCGKnR0Kd5CHTqj49EdR50chTrUZ2YQSp/b/WK80t01Wvc0KhSU2+MgGhkSebJj0Y1J3nQQ0VpyJ34KBbnaOhq40Tg2+nUIyYPBXIf2hhusPuroHgvFewDEyxu7Uga2D6qNioOwG/dkmVOXSLUXBWLbeh1STxZ++CjSpW4f/Gphzt/9T/qTlyp9zbVBrN42XiE0/jSUxAVailrsEz/Y2ilLwtdcLBikefJ0tXQid7nCPMcYgtrIDpBiOqRLL9fzspIjn1i5ZVGjhMT3lwae/I4I 944xnhFH WvvM2EUiNrm4GHH0k05VCpKrWTWCtOv9G+UvnWcos6IQY0g7Pf2mXYSfoglxJ62q9fjRIC4JPPsaMJj5+c4MX2EJXo0pxq5XlvREeTDPA39LxtAob0qbhWsZ7MuvZqBAr2ULuQl8JiaLs3n/cRY+LR5z710Le0/ABqmE7TRBFzI7Nbk6f4Qn9zAv5qOfdaFphrac9s5VvbX2xdwiRDYzvyD3Pmy+qlfFq4a2zcGpot0gmbIN7V7muE5jSqiQRU1tPF9w9rjABD0V+2fw21yriaPa9FDaf9zIm4Nj8Z+hjirRENcAMU+FyewIunws/6VA7gk3aHaewN0F29MNJ9PDqTCtKUWx6B9Ilz4w38hSiOE6YO9SwgaKQ7cemDvpOOqcMF91yEh1XpdeWIrXDWXckOPioGOvXdBhVEQYoldrZp7z4fyCt3HYYMiGTp0+QVTZgJJDX X-Bogosity: Ham, tests=bogofilter, spamicity=0.017249, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Make an entry for cfi extensions in extensions.yaml. Signed-off-by: Deepak Gupta --- .../devicetree/bindings/riscv/extensions.yaml | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/Documentation/devicetree/bindings/riscv/extensions.yaml b/Documentation/devicetree/bindings/riscv/extensions.yaml index a06dbc6b4928..b7c86fb91984 100644 --- a/Documentation/devicetree/bindings/riscv/extensions.yaml +++ b/Documentation/devicetree/bindings/riscv/extensions.yaml @@ -361,6 +361,18 @@ properties: The standard Zicboz extension for cache-block zeroing as ratified in commit 3dd606f ("Create cmobase-v1.0.pdf") of riscv-CMOs. + - const: zicfilp + description: + The standard Zicfilp extension for enforcing forward edge control-flow + integrity as ratified in commit 3f8e450 ("merge pull request #227 from + ved-rivos/0709") of riscv-cfi github repo. + + - const: zicfiss + description: + The standard Zicfilp extension for enforcing forward edge control-flow + integrity as ratified in commit 3f8e450 ("merge pull request #227 from + ved-rivos/0709") of riscv-cfi github repo. + - const: zicntr description: The standard Zicntr extension for base counters and timers, as From patchwork Thu Sep 12 23:16:27 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13802831 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 4792CEEE271 for ; Thu, 12 Sep 2024 23:17:45 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id CCCB36B009F; Thu, 12 Sep 2024 19:17:44 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C7CA86B00A0; Thu, 12 Sep 2024 19:17:44 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id ACF396B00A1; Thu, 12 Sep 2024 19:17:44 -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 8876F6B009F for ; Thu, 12 Sep 2024 19:17:44 -0400 (EDT) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 506AFA0E3C for ; Thu, 12 Sep 2024 23:17:44 +0000 (UTC) X-FDA: 82557650448.25.59D4D9A Received: from mail-pl1-f181.google.com (mail-pl1-f181.google.com [209.85.214.181]) by imf03.hostedemail.com (Postfix) with ESMTP id 765E72000C for ; Thu, 12 Sep 2024 23:17:42 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=nqdNJ7qb; spf=pass (imf03.hostedemail.com: domain of debug@rivosinc.com designates 209.85.214.181 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=1726182957; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=TC8HJTmLFTCxnzwVbVDHY5HOw5On7Gme9EHJxcVvh2Y=; b=OeHawfMzFlmsOXf2o+ruO+frcq2/fE2YMA+CydKXeXWOy4xqbYqLnusgDg5tPG0pHmDciJ lOX86UTatvP1YDrkFdBITHbD48qUgmrwGNskrwZgEkbOM5GSRepCoexu29NbZrvPMcNvob 6mLR3GTtOuqYrRRCkV0TCgPWQnoEkY4= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1726182957; a=rsa-sha256; cv=none; b=L08tRiabgncELtdTx2YAPPS4F7sHWjyOjdWl1pVT3fuZbYVr85UOAYddIAJQ67meiIB6LB 5jpRk0OyIsU4gFG6WoJ57ae0qiSeo2unnzvdQNiae9jnR9taqSD3WwRHv1IHVvpYs9pCiM 6GcUNjGvVvuXHbyRCEJR+58/AYLec3Q= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=nqdNJ7qb; spf=pass (imf03.hostedemail.com: domain of debug@rivosinc.com designates 209.85.214.181 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none Received: by mail-pl1-f181.google.com with SMTP id d9443c01a7336-205659dc63aso16899535ad.1 for ; Thu, 12 Sep 2024 16:17:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1726183061; x=1726787861; darn=kvack.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=TC8HJTmLFTCxnzwVbVDHY5HOw5On7Gme9EHJxcVvh2Y=; b=nqdNJ7qbD/ZD5XhzEUIIjrTd8ao1ZuAwCGnK3wEJI7Sd4Jp7kpySUSBlwdYl/TuYjJ wl87VkPjYhTH6mhPAOyX2kTNlLjUn2vnBP/d3uGO61JVzx/e/Fhy78ZPPgZBSlsYg4VI 4wTq0epFr3zzxlzuZkDN/VjnTzqejqzGKG17ERUaGGYpKXZGrbWbsnmfDRQh1G/LDbJ3 BO/kHfU3a2SkYxTWFvPJFhKPJeoM6fiDGDt7f/abQMgtqYq5CD1E/9NBjV3A3o7WU80P kk/wzZj6MS9BbzO9oSC0mLrbASGUzu78mZyqgmUMnlOv+3/LUWCcYEjqjE1frQ7ktjUg fe2w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726183061; x=1726787861; 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=TC8HJTmLFTCxnzwVbVDHY5HOw5On7Gme9EHJxcVvh2Y=; b=KHOUrk2JtYDaN24CQZCmTSO6gbyDoYcbxxFYSUtsreD3muRI6JXkQ819UOxggbL7YQ 8ITLoBlcCI9aLxJXMfsjflzFfOScvinShJDv83y74g3BxhzD2U9RAfFC9rViqHkGiHDr lLo2aDCg9KOBe03JN644x9uHGiyuDbXvTs3gXWs9k3jeMaOMOrqdvHtMw/qsiE/3Ibju YVAoXMcC+Jzm03DZmsghsASiduFi1t3bmpyZg3hINO7pMNGtaBzy+ZlGKkuMvs6Sx+Fq dUkA2dtKulJyqyCDQ/oQNd+iSN+sxlYVsmijmsUlKD4CAFIEnRGAUylOZHnKWWblu96u JacQ== X-Forwarded-Encrypted: i=1; AJvYcCX9rKR+YpsgTj2iklhZBoVcRRsU2pM1hQEoLtcYppHaDpqtHyCPPIyk0BG9yme99uhTZGMSnpYhcQ==@kvack.org X-Gm-Message-State: AOJu0YyVq2IPcb0Uk2rq/+Jvstg3A2sF+ATQMMnBQ2dS+ehkyvfdUyls jBRrlZMceBzgDCOk4tvQ/bKr0lDLXQVn0vcvLln/1XS0w0wxMXY6+VbUusmZZmM= X-Google-Smtp-Source: AGHT+IEpTcNeHZnDkxzsBefZDBG9gwlZQAPff/y8vG/iX+SRyJGFk81Qx5PcE3129Vm1O5X6NKyS+w== X-Received: by 2002:a17:90b:4c41:b0:2c9:3370:56e3 with SMTP id 98e67ed59e1d1-2dba008304amr4873291a91.34.1726183060973; Thu, 12 Sep 2024 16:17:40 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2db6c1ac69asm3157591a91.0.2024.09.12.16.17.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 12 Sep 2024 16:17:40 -0700 (PDT) From: Deepak Gupta To: paul.walmsley@sifive.com, palmer@sifive.com, conor@kernel.org, linux-doc@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kselftest@vger.kernel.org Cc: corbet@lwn.net, palmer@dabbelt.com, aou@eecs.berkeley.edu, robh@kernel.org, krzk+dt@kernel.org, oleg@redhat.com, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, hpa@zytor.com, peterz@infradead.org, akpm@linux-foundation.org, arnd@arndb.de, ebiederm@xmission.com, kees@kernel.org, Liam.Howlett@oracle.com, vbabka@suse.cz, lorenzo.stoakes@oracle.com, shuah@kernel.org, brauner@kernel.org, samuel.holland@sifive.com, debug@rivosinc.com, andy.chiu@sifive.com, jerry.shih@sifive.com, greentime.hu@sifive.com, charlie@rivosinc.com, evan@rivosinc.com, cleger@rivosinc.com, xiao.w.wang@intel.com, ajones@ventanamicro.com, anup@brainfault.org, mchitale@ventanamicro.com, atishp@rivosinc.com, sameo@rivosinc.com, bjorn@rivosinc.com, alexghiti@rivosinc.com, david@redhat.com, libang.li@antgroup.com, jszhang@kernel.org, leobras@redhat.com, guoren@kernel.org, samitolvanen@google.com, songshuaishuai@tinylab.org, costa.shul@redhat.com, bhe@redhat.com, zong.li@sifive.com, puranjay@kernel.org, namcaov@gmail.com, antonb@tenstorrent.com, sorear@fastmail.com, quic_bjorande@quicinc.com, ancientmodern4@gmail.com, ben.dooks@codethink.co.uk, quic_zhonhan@quicinc.com, cuiyunhui@bytedance.com, yang.lee@linux.alibaba.com, ke.zhao@shingroup.cn, sunilvl@ventanamicro.com, tanzhasanwork@gmail.com, schwab@suse.de, dawei.li@shingroup.cn, rppt@kernel.org, willy@infradead.org, usama.anjum@collabora.com, osalvador@suse.de, ryan.roberts@arm.com, andrii@kernel.org, alx@kernel.org, catalin.marinas@arm.com, broonie@kernel.org, revest@chromium.org, bgray@linux.ibm.com, deller@gmx.de, zev@bewilderbeest.net Subject: [PATCH v4 08/30] riscv: zicfiss / zicfilp enumeration Date: Thu, 12 Sep 2024 16:16:27 -0700 Message-ID: <20240912231650.3740732-9-debug@rivosinc.com> X-Mailer: git-send-email 2.45.0 In-Reply-To: <20240912231650.3740732-1-debug@rivosinc.com> References: <20240912231650.3740732-1-debug@rivosinc.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 765E72000C X-Stat-Signature: piwwxc8tai9576hpmbdn476efzwhzwue X-Rspamd-Server: rspam09 X-Rspam-User: X-HE-Tag: 1726183062-173102 X-HE-Meta: U2FsdGVkX1/FmepKYs7DrKEGVREgUYl8UxszmKbtci4yXyxIjjQZQhKNzFJedbVbf07cRS6Npik9fAYjDwfz9r6V4c/lO+qH/D+LZ5ZBDP+ceCxTorbiJCVGeWONbfDc1+E5QDtM4gLZeu3g865pTJTk783FmlEeRGzvL9fkB0IYpQg7f5CVf39dnqNnLtGHfyADL8R8hAn15o5F53t/jwYQUqusweVc5TbwtpkMqnNMQlmVorVxZzH/kzKcjlzaDqkz6hLRtIRQEgU+UmBGu41mOjs//y/gopSJ2ooV4mWvo3b9wB7YsfZFS80qJkjfq0NMjfJpsxdU5jJcCPi54Tczzest/02oWe9Ys9wlEOvFq5cvMCmQ4PSaDxd0DqW8JFTjHU6uf+PUlhRPJLsT43imAzKWy2Q7mQ4Yb/Wt+DMXQ0HxX9QGEGf9aUPZ3i+uNjoTIdlz4nKAcZ19PvENGvVzcnXo0nE8MwLSQYQgjvh0Xl3Wu8IQXkClvHOU1l8Rxm8rAPIVWtHlExwSQXb/yS9KsE1vpxs0YmKmeZ2JAZPqbuuI21g1CFUV/GXJLiIj+AL0SZhWI/iFB8bDcxuWEUNyoOOhuS0ZBSz9W8KH8bcYVBxZdgyOmbJLIS65zVEqtCdfb3svEnw6ty9w39j7LS5saMv/KhBvqmdNArj46084UH1zYtwCXUmImVPB9tyxHopl6HWV4BU4u2L0LqUqhL29wXzceN7XHBSMJeqCvhldnvFribmqYnHmVeyo1tpVw3d2GkrDwvapXC0K/SiPXv3OM6oVHOGHd/MFy7m1vjCngDAv8PBiQvbieOyYQY83IkWkzPSWw1yrj0kXRCcNgK19X+U1RfpmKPrggx+fQ0NtnvB28O1qTSiLsNjQjaaaWKFsb1ogOojQqQEFX3v7S23gzddB0t9lRRQg4gqbYF7KeZmiB2pQAM0MaPqq0dhZ9hpGfcsO56z17o06W2A AVlhShZr U8v9B0ECSkhEwUFwmgSaGxw83vDdDJSLjV8T6z7WDw2sjNsLK2csuCc5rnbM4HETNB/CRJPAHjzKeSPr+YW6G9Tqso3UUd7wWWs7Ln2aj0XucRWnpbav0N3CRxIsHTtfvY4nD+32tYnwTcP/MK93TGec0OYZeeThb6z9MsgoUETdnUjm4eaiSFO8NQdc1arFH4FwSD7JmS/APumGS8vvKELA+CJlrdI8u06ik18eTDuIYzib6ydaDiiDgBj0IA5hpCHwKaS9U9e7pGDvLnK0+wZlkIaATw0u0Oc/YvMatBqvVA6gFK60PEExDmLn9oVTbVlu0MOSlZSoDBp8UpfCr/L5WsyEcbrTLkdME8yHhwV2L0g82Jya4Mj4biREaUfIX4gjHkKxX8dq2MzJUwJzarBS36vuDHxsS5oU1apXaRwrhVWSX9akaaNa3M/XThpTr3QKrvgR8SarUum5wYXVCcdYJM3TVvr2HppkiI+usCj99ytM= 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: This patch adds support for detecting zicfiss and zicfilp. zicfiss and zicfilp stands for unprivleged integer spec extension for shadow stack and branch tracking on indirect branches, respectively. This patch looks for zicfiss and zicfilp in device tree and accordinlgy lights up bit in cpu feature bitmap. Furthermore this patch adds detection utility functions to return whether shadow stack or landing pads are supported by cpu. Signed-off-by: Deepak Gupta --- arch/riscv/include/asm/cpufeature.h | 13 +++++++++++++ arch/riscv/include/asm/hwcap.h | 2 ++ arch/riscv/include/asm/processor.h | 1 + arch/riscv/kernel/cpufeature.c | 2 ++ 4 files changed, 18 insertions(+) diff --git a/arch/riscv/include/asm/cpufeature.h b/arch/riscv/include/asm/cpufeature.h index ce9a995730c1..344b8e8cd3e8 100644 --- a/arch/riscv/include/asm/cpufeature.h +++ b/arch/riscv/include/asm/cpufeature.h @@ -8,6 +8,7 @@ #include #include +#include #include #include #include @@ -180,4 +181,16 @@ static __always_inline bool riscv_cpu_has_extension_unlikely(int cpu, const unsi return __riscv_isa_extension_available(hart_isa[cpu].isa, ext); } +static inline bool cpu_supports_shadow_stack(void) +{ + return (IS_ENABLED(CONFIG_RISCV_USER_CFI) && + riscv_cpu_has_extension_unlikely(smp_processor_id(), RISCV_ISA_EXT_ZICFISS)); +} + +static inline bool cpu_supports_indirect_br_lp_instr(void) +{ + return (IS_ENABLED(CONFIG_RISCV_USER_CFI) && + riscv_cpu_has_extension_unlikely(smp_processor_id(), RISCV_ISA_EXT_ZICFILP)); +} + #endif diff --git a/arch/riscv/include/asm/hwcap.h b/arch/riscv/include/asm/hwcap.h index 5a0bd27fd11a..04425476526a 100644 --- a/arch/riscv/include/asm/hwcap.h +++ b/arch/riscv/include/asm/hwcap.h @@ -92,6 +92,8 @@ #define RISCV_ISA_EXT_ZCF 83 #define RISCV_ISA_EXT_ZCMOP 84 #define RISCV_ISA_EXT_ZAWRS 85 +#define RISCV_ISA_EXT_ZICFILP 86 +#define RISCV_ISA_EXT_ZICFISS 87 #define RISCV_ISA_EXT_XLINUXENVCFG 127 diff --git a/arch/riscv/include/asm/processor.h b/arch/riscv/include/asm/processor.h index 8702b8721a27..d61587964bd7 100644 --- a/arch/riscv/include/asm/processor.h +++ b/arch/riscv/include/asm/processor.h @@ -13,6 +13,7 @@ #include #include +#include /* * addr is a hint to the maximum userspace address that mmap should provide, so diff --git a/arch/riscv/kernel/cpufeature.c b/arch/riscv/kernel/cpufeature.c index 41fd0be25bd8..ae6ea2f1d1db 100644 --- a/arch/riscv/kernel/cpufeature.c +++ b/arch/riscv/kernel/cpufeature.c @@ -317,6 +317,8 @@ const struct riscv_isa_ext_data riscv_isa_ext[] = { riscv_ext_zicbom_validate), __RISCV_ISA_EXT_SUPERSET_VALIDATE(zicboz, RISCV_ISA_EXT_ZICBOZ, riscv_xlinuxenvcfg_exts, riscv_ext_zicboz_validate), + __RISCV_ISA_EXT_SUPERSET(zicfilp, RISCV_ISA_EXT_ZICFILP, riscv_xlinuxenvcfg_exts), + __RISCV_ISA_EXT_SUPERSET(zicfiss, RISCV_ISA_EXT_ZICFISS, riscv_xlinuxenvcfg_exts), __RISCV_ISA_EXT_DATA(zicntr, RISCV_ISA_EXT_ZICNTR), __RISCV_ISA_EXT_DATA(zicond, RISCV_ISA_EXT_ZICOND), __RISCV_ISA_EXT_DATA(zicsr, RISCV_ISA_EXT_ZICSR), From patchwork Thu Sep 12 23:16:28 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13802832 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 519B9EEE270 for ; Thu, 12 Sep 2024 23:17:49 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D6A016B00A1; Thu, 12 Sep 2024 19:17:48 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id CF1946B00A2; Thu, 12 Sep 2024 19:17:48 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B43B86B00A3; Thu, 12 Sep 2024 19:17:48 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 945CA6B00A1 for ; Thu, 12 Sep 2024 19:17:48 -0400 (EDT) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 633BC1C5307 for ; Thu, 12 Sep 2024 23:17:48 +0000 (UTC) X-FDA: 82557650616.02.7B69B40 Received: from mail-pj1-f47.google.com (mail-pj1-f47.google.com [209.85.216.47]) by imf16.hostedemail.com (Postfix) with ESMTP id 8FF6B180004 for ; Thu, 12 Sep 2024 23:17:46 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=EMKyZrQ9; dmarc=none; spf=pass (imf16.hostedemail.com: domain of debug@rivosinc.com designates 209.85.216.47 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1726182988; a=rsa-sha256; cv=none; b=X3c3YuSRub1M8PNRk3FCYqhy6hqsbX0Zq83r0L3AaFcEldmi2HGzcxn05TulDsvZfp5Uga 0BPfLtcCMG08oZqQwmY2/k+expnlKbrMAeFeFEGorHE1Dt/HyxhPk3crMHcLborPLLvCtB dv6enzjGL9oZ5e6XG6CS6GQB2QI0TBM= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=EMKyZrQ9; dmarc=none; spf=pass (imf16.hostedemail.com: domain of debug@rivosinc.com designates 209.85.216.47 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1726182988; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=L4VD0pn+7fxYAYMmu2vkxqQSkT8TrKqfbPDKikIcbYI=; b=7hN1elbWef+DXEEQMnvIyrBPaEuHsJDqmBf91WnUhXh/wU838NcykoFYGhHD+KrjnMXtdW pB9nSQhTPZusSzmuCIDCTwMKnLaJWqkgy1nmpVequSxjfqQOfxkcfUKID3+q1OAWOw1vLo 4YTuSWPhal3ypjPd1e0FM2hhDhx4X+o= Received: by mail-pj1-f47.google.com with SMTP id 98e67ed59e1d1-2d86f71353dso1108181a91.2 for ; Thu, 12 Sep 2024 16:17:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1726183065; x=1726787865; darn=kvack.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=L4VD0pn+7fxYAYMmu2vkxqQSkT8TrKqfbPDKikIcbYI=; b=EMKyZrQ9us1oNBnBEeJN2qOLAyphikeC39ynJBFd8k0U2R2YEnmP5EkH8STLHpN08f z1Nw664SSF0it+hU1ZaXBc6V4YXxWXmvZdmnn1orSjFdBpcBFFyjb53iQ5Vrk5P1DeXa TnqYXzm5MfCq2H5bOtq6IN44thmLfXuUlG7HOzFtS65Dud/OQ1nzPyP4i3xv6cGSJVoj +2zK+t8mVH+nQLOw2Ayp7jzpSfQejYEkpwqdaAWakccpKvx7bgTgZZv2UChQsMZpkCfz L+nGXPxPNtXzmL1kPgo+f4SNL4YuvVPTJpEMRKrfkZNDV7KacjGNe7QiyNYgq7l7TYn+ RSLA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726183065; x=1726787865; 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=L4VD0pn+7fxYAYMmu2vkxqQSkT8TrKqfbPDKikIcbYI=; b=Dkz09wC6oqGIbXiPr+GhhMHWWQ/qaX0MBxwT5zW3XS0fNvj4xfauvXfc4h0PIdV5Fv ee97jR1Ny7UAAniWPR8HebylV+RR2j3SwH0pnkaYB1D/tFD9fHCCpY2QZTJDqEjjbpvG 1+pD0SMPUYQ7kGQXwMshINtUyI27CeOf0LJinhzPj7B7BfiJvv9cD2dB/kYUqoiWT2AN 9fxXIhPTT8pufaTXtY580bCNTCY6iATMRHZB9E4jkqjVVsCP6R7Iy1pmTc+CkaOVMRdO Zvp6iBuuWpHPFNAY91LEu56VF2Fki0yP2PFVXBVOXq+aojIpHsfmfkOE+XRx5wfUaewY 8fxA== X-Forwarded-Encrypted: i=1; AJvYcCUtn7mMUypJyuWMPufdF+4k4gUvO0IKGRtk6kvnSh7P48uPx02jQqBjqWddC45fqkROYQJTxoNIHg==@kvack.org X-Gm-Message-State: AOJu0YyYiib1vPptB0wWs9bBY29oJfTKRrGop1vrYSCmaVJIZVZO5blk UN64KONv4VZUVce1sM9ig7hxqyRyfx8CuJ0TwwWastfV+tVPemESewB5odHEXNc= X-Google-Smtp-Source: AGHT+IHE2/uUipG3AbFYQLbtsQ6XTH0oLyDxTmTJL8HOpmNqW0hVQmxkI0Zi8FntOLvceExzi1MfEA== X-Received: by 2002:a17:90a:c10:b0:2d8:f7e2:eff with SMTP id 98e67ed59e1d1-2dba006821cmr4395000a91.36.1726183065339; Thu, 12 Sep 2024 16:17:45 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2db6c1ac69asm3157591a91.0.2024.09.12.16.17.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 12 Sep 2024 16:17:45 -0700 (PDT) From: Deepak Gupta To: paul.walmsley@sifive.com, palmer@sifive.com, conor@kernel.org, linux-doc@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kselftest@vger.kernel.org Cc: corbet@lwn.net, palmer@dabbelt.com, aou@eecs.berkeley.edu, robh@kernel.org, krzk+dt@kernel.org, oleg@redhat.com, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, hpa@zytor.com, peterz@infradead.org, akpm@linux-foundation.org, arnd@arndb.de, ebiederm@xmission.com, kees@kernel.org, Liam.Howlett@oracle.com, vbabka@suse.cz, lorenzo.stoakes@oracle.com, shuah@kernel.org, brauner@kernel.org, samuel.holland@sifive.com, debug@rivosinc.com, andy.chiu@sifive.com, jerry.shih@sifive.com, greentime.hu@sifive.com, charlie@rivosinc.com, evan@rivosinc.com, cleger@rivosinc.com, xiao.w.wang@intel.com, ajones@ventanamicro.com, anup@brainfault.org, mchitale@ventanamicro.com, atishp@rivosinc.com, sameo@rivosinc.com, bjorn@rivosinc.com, alexghiti@rivosinc.com, david@redhat.com, libang.li@antgroup.com, jszhang@kernel.org, leobras@redhat.com, guoren@kernel.org, samitolvanen@google.com, songshuaishuai@tinylab.org, costa.shul@redhat.com, bhe@redhat.com, zong.li@sifive.com, puranjay@kernel.org, namcaov@gmail.com, antonb@tenstorrent.com, sorear@fastmail.com, quic_bjorande@quicinc.com, ancientmodern4@gmail.com, ben.dooks@codethink.co.uk, quic_zhonhan@quicinc.com, cuiyunhui@bytedance.com, yang.lee@linux.alibaba.com, ke.zhao@shingroup.cn, sunilvl@ventanamicro.com, tanzhasanwork@gmail.com, schwab@suse.de, dawei.li@shingroup.cn, rppt@kernel.org, willy@infradead.org, usama.anjum@collabora.com, osalvador@suse.de, ryan.roberts@arm.com, andrii@kernel.org, alx@kernel.org, catalin.marinas@arm.com, broonie@kernel.org, revest@chromium.org, bgray@linux.ibm.com, deller@gmx.de, zev@bewilderbeest.net Subject: [PATCH v4 09/30] riscv: zicfiss / zicfilp extension csr and bit definitions Date: Thu, 12 Sep 2024 16:16:28 -0700 Message-ID: <20240912231650.3740732-10-debug@rivosinc.com> X-Mailer: git-send-email 2.45.0 In-Reply-To: <20240912231650.3740732-1-debug@rivosinc.com> References: <20240912231650.3740732-1-debug@rivosinc.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 8FF6B180004 X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: tiiyrxssmg8uxmx4t9ctn6q4wiqdgdow X-HE-Tag: 1726183066-838063 X-HE-Meta: U2FsdGVkX1+4ToJGq0qlJ35bpeSc+/hLtJCO/6X5wnK4w1XWE9anVmlPGRXbVEzjqldH2lANOvWwrQtBpNyUysELOk4E5nI3FaQQM+zoLyyDLrGjQPp8c+XWnTRU1lM60Cjuf9FoCsHsn3wH3Ygtm6hJJTl0kOKklbeoSxWHimg8S12qIqZG2GidvOAB2X/3qCoxI76kj+nzaffDPYIML7Abs7yn0gKJ5sX2mXlRYnIYs7JkQKnyEPEHuhWtr60S+1B9yBbxZIWb3v2geUAMhMSohze7E98ohxjFCrEHQUhZKuqcPnEn7tZpC9aN9WAMoLWjfh9oQ/8BZKbzEWJkcQ+O9IOjqzWALvD5NKAcUntpiZss774eaDjAMwrpAWjVIwZXtYJnzHSFHsPis9i3BpONtN3yQlIoplnjj05BdcIJoFw7P+FGf7OyR+DQkvEN0ZrhCP86P3NzDcdsTGMP1qiOh6564JSVoHpaZJIZkzyT+HPXBMKvn89FRMUI8ii/mOXe1/inm9ycoLasZ1sfR/tF043T2AhrlS0WO5Va/malAcn9u1MnwD+1hNG/4855CX0EqECjoOUyy2QvC7e5HSe7olRflaYBTK12l/pDQrfI9sios2CQi3JyaEbgAjtpQ1otnzgTWFuLh9GH5GAOgJMmn20lRck5wURLhCKb57Nm1GUaD4PxPSW9gFAKilgBRnOgau2tPqPb22YlOv1W8Yv50O6BdtZPCg1ykpiGbftuvxwjfBnVVxAtD9HTXjw6NNJOHPgJ0lq7b+ow9cxWQRCVBpPjUYQ+95eI91GB2/9Im8T84DU+TgGRzc9qnxv2c2FjzYRMpaPKrb/mfFgzY9pnYIbEkEMB6lX1NWRkabmECletU3KT7glHrQ78bkVbcv3jzkxwPrqbR/tKW+QD9/MDjcET6tywAN4j2M/KIx32uyRmUl6/AQTfHg46dgcnvvbLiWIh0NP1HITGvUZ EO80sfxi g2DjuoN59gMi0mpJs5lvh2i8CGg3AkHEzP3U/i74qR6DusoqeVYFCJKAi+1nJvRwHTACNEzcxRmxK3I+xloCsj93vJ2MvudC5CIo+VF5KlsIPQd9bGiMWCkyae746GOJCqa/c1CTuVKMClwBIejSjaXPDqJrmb+BkDwUi8pfQGYF1KiuEUvSCGmDqcxc3zoDFoHnZPzHPMdzx0mtWyeoZyFFhilsJB4gPEqa6mdsN1Z3xWJSkHli+10SgxT5aF+169/6cgCxNA6/fSYivDUg2h9lY2zsGsMa9LznoymACcpsNVSCk8zLBJxm131HdRm/FEXhztbDWCLNfq/ScPwHkBJn+27V4yHQ+cqWXg2WoWqrzc8TbdlQERrhIdf9QnIpElkyVVqy6A5QWr4U3VU8uIlyzVXAuse4XRz+dJxsssOEy+NI= 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: zicfiss and zicfilp extension gets enabled via b3 and b2 in *envcfg CSR. menvcfg controls enabling for S/HS mode. henvcfg control enabling for VS while senvcfg controls enabling for U/VU mode. zicfilp extension extends *status CSR to hold `expected landing pad` bit. A trap or interrupt can occur between an indirect jmp/call and target instr. `expected landing pad` bit from CPU is recorded into xstatus CSR so that when supervisor performs xret, `expected landing pad` state of CPU can be restored. zicfiss adds one new CSR - CSR_SSP: CSR_SSP contains current shadow stack pointer. Signed-off-by: Deepak Gupta Reviewed-by: Charlie Jenkins --- arch/riscv/include/asm/csr.h | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/arch/riscv/include/asm/csr.h b/arch/riscv/include/asm/csr.h index 25966995da04..af7ed9bedaee 100644 --- a/arch/riscv/include/asm/csr.h +++ b/arch/riscv/include/asm/csr.h @@ -18,6 +18,15 @@ #define SR_MPP _AC(0x00001800, UL) /* Previously Machine */ #define SR_SUM _AC(0x00040000, UL) /* Supervisor User Memory Access */ +/* zicfilp landing pad status bit */ +#define SR_SPELP _AC(0x00800000, UL) +#define SR_MPELP _AC(0x020000000000, UL) +#ifdef CONFIG_RISCV_M_MODE +#define SR_ELP SR_MPELP +#else +#define SR_ELP SR_SPELP +#endif + #define SR_FS _AC(0x00006000, UL) /* Floating-point Status */ #define SR_FS_OFF _AC(0x00000000, UL) #define SR_FS_INITIAL _AC(0x00002000, UL) @@ -197,6 +206,8 @@ #define ENVCFG_PBMTE (_AC(1, ULL) << 62) #define ENVCFG_CBZE (_AC(1, UL) << 7) #define ENVCFG_CBCFE (_AC(1, UL) << 6) +#define ENVCFG_LPE (_AC(1, UL) << 2) +#define ENVCFG_SSE (_AC(1, UL) << 3) #define ENVCFG_CBIE_SHIFT 4 #define ENVCFG_CBIE (_AC(0x3, UL) << ENVCFG_CBIE_SHIFT) #define ENVCFG_CBIE_ILL _AC(0x0, UL) @@ -215,6 +226,11 @@ #define SMSTATEEN0_HSENVCFG (_ULL(1) << SMSTATEEN0_HSENVCFG_SHIFT) #define SMSTATEEN0_SSTATEEN0_SHIFT 63 #define SMSTATEEN0_SSTATEEN0 (_ULL(1) << SMSTATEEN0_SSTATEEN0_SHIFT) +/* + * zicfiss user mode csr + * CSR_SSP holds current shadow stack pointer. + */ +#define CSR_SSP 0x011 /* symbolic CSR names: */ #define CSR_CYCLE 0xc00 From patchwork Thu Sep 12 23:16:29 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13802833 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 0DE48EEE270 for ; Thu, 12 Sep 2024 23:17:54 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8FD556B00A3; Thu, 12 Sep 2024 19:17:53 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 8ACDF6B00A4; Thu, 12 Sep 2024 19:17:53 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6FF586B00A5; Thu, 12 Sep 2024 19:17:53 -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 4EEFC6B00A3 for ; Thu, 12 Sep 2024 19:17:53 -0400 (EDT) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 0E86A12066A for ; Thu, 12 Sep 2024 23:17:53 +0000 (UTC) X-FDA: 82557650826.28.0DC7C65 Received: from mail-pj1-f47.google.com (mail-pj1-f47.google.com [209.85.216.47]) by imf19.hostedemail.com (Postfix) with ESMTP id 312091A0010 for ; Thu, 12 Sep 2024 23:17:50 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=NRDCgiO7; dmarc=none; spf=pass (imf19.hostedemail.com: domain of debug@rivosinc.com designates 209.85.216.47 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1726183065; a=rsa-sha256; cv=none; b=gK5d8R6D+7fQm0vgqZ2a3H1yGDkSiflnc0wtCCppgMZlB11OBHg54eL5dulPod84g5W6d3 maj88XO7tv3DRKnQkvI7jzEUrQXrq/sz8x9JzPq2IE9FvoVHkBmYON3WhCXTix/3wI0DoC w1uy7QEM/fIdc4R3edKtNwcQiwnPEoM= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=NRDCgiO7; dmarc=none; spf=pass (imf19.hostedemail.com: domain of debug@rivosinc.com designates 209.85.216.47 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1726183065; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=IdMH+b8hMWx7M6md7N6CXtcv0PrleWKto++QHl76wt4=; b=cnMkc3j/K57C0iX0BQ8tfEaSJvxdQVwTr9qVgKlxsb0t0NuzZ/P+wAoXRulX/Ia9ecMuLK R+VAlxZ9NX376UOxJmU5xeduYlPQbTEpEn6ADj7hE2v6es0lHQPNwk2ErJtOtBe8xptyMF BLM9p6X6h3PBZQmM2ZqalI9YcwJi1Lc= Received: by mail-pj1-f47.google.com with SMTP id 98e67ed59e1d1-2d88690837eso1257351a91.2 for ; Thu, 12 Sep 2024 16:17:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1726183070; x=1726787870; darn=kvack.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=IdMH+b8hMWx7M6md7N6CXtcv0PrleWKto++QHl76wt4=; b=NRDCgiO7PuJLeaSKH1PiNphBkHT+zoSfWkCfIapsOzbqLIUrnNlfw7XCS1iOvIUGRb YUZfUQCINZYha3dl3dWKIGF5QWjPoQneiAUkmFwCu27BArSvcXy16dTBBgTOXxxLmNFL 6XjMA8jq210GFKggQ65Gy0lvJkc3/G1DuQ1q0fJv8X+xMKeANij1NM+Be6K34iMc+PhS j1367+7vs+osBb5Z+4HGmUGqWYwlMNNGvNGQ3/Xy++/mjVztNrMOJCNNJmf3csmNrqa8 TlS7yToeu6P31hIa4M7MEmtsg9L6IZKFDwE2Gt8F71jPi/3PfDWAsMMeCuFhVNqaPtGH SF+g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726183070; x=1726787870; 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=IdMH+b8hMWx7M6md7N6CXtcv0PrleWKto++QHl76wt4=; b=vJN1vocr24L1wUdlQfdt752kZ4TxrHkH0zVeDSfjrG8OYVkCBEAjkhrDPEtrIn1qxV SYS8XdGvJvA2bC0FYN1+O97BPknBqfyqXlFLORFKouHyEUbfKcI15elul0awThg0zT4U b/eTKFTnxK13oS105tB9VvOLTRoAyw9Ebrll43bAFA+z3T4vEmLgFQRdrRDxMktZ4Bdz XSA4NckAAAk0Ghna9k314FehPfN/8j8twmyAQuAs1SC8YatuxCtY8pMxnzeHdq8uKxf4 715IeQvdtEtQu1aaqpjLuIjex8Y8M3kP6FP4sQ3GEH9byjaR0MpbRl72nMYrEcY/EiU8 97cg== X-Forwarded-Encrypted: i=1; AJvYcCUsuyCzi8S3fefDgrCuaI75X2RaY4DT9PHQaNwm1y7qUccEjKBYEUHMJf8Pg9+ZQBa2glMOlYsErA==@kvack.org X-Gm-Message-State: AOJu0YwnRRg4xqCer23aQnwFSJssbjUQWkBoaRvIwEK0azPTuvikre2N uQERpSjT0MAwj2kME+VqFwk342TbTold9mqULknpONuVhjzxH+8+TuD6lBu1I8w= X-Google-Smtp-Source: AGHT+IFFLSdBWtF2TFEc0MNeO8MBZSGLro8EZzW5E3fSn2Pc5KYqBR0cHiRpYxrN8cHcKSAWR1r/XQ== X-Received: by 2002:a17:90a:9a86:b0:2d8:8818:4d53 with SMTP id 98e67ed59e1d1-2dba0090ccbmr4436318a91.41.1726183069691; Thu, 12 Sep 2024 16:17:49 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2db6c1ac69asm3157591a91.0.2024.09.12.16.17.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 12 Sep 2024 16:17:49 -0700 (PDT) From: Deepak Gupta To: paul.walmsley@sifive.com, palmer@sifive.com, conor@kernel.org, linux-doc@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kselftest@vger.kernel.org Cc: corbet@lwn.net, palmer@dabbelt.com, aou@eecs.berkeley.edu, robh@kernel.org, krzk+dt@kernel.org, oleg@redhat.com, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, hpa@zytor.com, peterz@infradead.org, akpm@linux-foundation.org, arnd@arndb.de, ebiederm@xmission.com, kees@kernel.org, Liam.Howlett@oracle.com, vbabka@suse.cz, lorenzo.stoakes@oracle.com, shuah@kernel.org, brauner@kernel.org, samuel.holland@sifive.com, debug@rivosinc.com, andy.chiu@sifive.com, jerry.shih@sifive.com, greentime.hu@sifive.com, charlie@rivosinc.com, evan@rivosinc.com, cleger@rivosinc.com, xiao.w.wang@intel.com, ajones@ventanamicro.com, anup@brainfault.org, mchitale@ventanamicro.com, atishp@rivosinc.com, sameo@rivosinc.com, bjorn@rivosinc.com, alexghiti@rivosinc.com, david@redhat.com, libang.li@antgroup.com, jszhang@kernel.org, leobras@redhat.com, guoren@kernel.org, samitolvanen@google.com, songshuaishuai@tinylab.org, costa.shul@redhat.com, bhe@redhat.com, zong.li@sifive.com, puranjay@kernel.org, namcaov@gmail.com, antonb@tenstorrent.com, sorear@fastmail.com, quic_bjorande@quicinc.com, ancientmodern4@gmail.com, ben.dooks@codethink.co.uk, quic_zhonhan@quicinc.com, cuiyunhui@bytedance.com, yang.lee@linux.alibaba.com, ke.zhao@shingroup.cn, sunilvl@ventanamicro.com, tanzhasanwork@gmail.com, schwab@suse.de, dawei.li@shingroup.cn, rppt@kernel.org, willy@infradead.org, usama.anjum@collabora.com, osalvador@suse.de, ryan.roberts@arm.com, andrii@kernel.org, alx@kernel.org, catalin.marinas@arm.com, broonie@kernel.org, revest@chromium.org, bgray@linux.ibm.com, deller@gmx.de, zev@bewilderbeest.net Subject: [PATCH v4 10/30] riscv: usercfi state for task and save/restore of CSR_SSP on trap entry/exit Date: Thu, 12 Sep 2024 16:16:29 -0700 Message-ID: <20240912231650.3740732-11-debug@rivosinc.com> X-Mailer: git-send-email 2.45.0 In-Reply-To: <20240912231650.3740732-1-debug@rivosinc.com> References: <20240912231650.3740732-1-debug@rivosinc.com> MIME-Version: 1.0 X-Rspam-User: X-Stat-Signature: bezi8rpxmqxie586nqzm7ooxxidcfmwd X-Rspamd-Queue-Id: 312091A0010 X-Rspamd-Server: rspam02 X-HE-Tag: 1726183070-344840 X-HE-Meta: U2FsdGVkX1+dBh9Xorbe7XosR43qS5ikC+RCyIJadbKJnBzJZrE4tXObY7/X28eAwje0+D5R0sKdwsve1xVvoNuRAMhT9Ibt2ck8OVnPxi8ZRCWFGCkSyxAkoJDL02yMx3DHw6O96eAFo1mcx69Hv7h7RqLlQNxbeCyy92N54m9KAVMZI4F4/dYPOldKoKdDedZi95MRwao0Ikb9VjZe2MAG7E8iiIeLIqpuEiYB5VDRts1xs/HpuUmMPKeFbUBBdEJgwm2Y2b1EhzC818OP1dJOzzl9dflY86e6RykXKftWh1IF4g2cuy8Q0mkk7AKJM0sHOrVs/BqtPFOrnHl+r+xvH8xquXVnRpdHjMUTXokTcZmL1iMdiZJ4hBHxcJGQS9Q3IZkxcr/pcvmMLikd8eXZhhZCWNi9e3JJ/6D1P7vCXbZJHVApM8B7iIkaX12j4J0Qzd14L2q3/BVIF1c91ZJbPZef329OBpemR8Jse/2yH8ycqWzJmh44Nu6ogkykaIEtOQE9UamVIvr2/Dt+7l0yTR8bzoPs/7ahFvxiys8ZXo5RKlyn2iuInUR5LqxC6LzkmSgBkCve4Ct84LntTrkVTMLFIvFat8XD/0E4ksNZ1XHzrOo7Yo3QfIIA32YniB1npdaixseXbdO78omSJhSJNDWMWNoyS+6zgVxwT9N6/YFr1RD343slgH4uNF2APubCpbKA1E9OmPzZZnJ9ESV2WBkRnKMJD616fiUdAZEnZu5UA9JRREB4NEnBy6KGp2F6vKADeMUrGWZQ7fUFLLSPCSNvUEftUcfO6ZTgIA0A8oCyxStKBMdeL++52nkRUMHJc0dFKuXC4eIemsmQBAqj2n0oEaiJQiLbVOWdi5dJsPIyF5lVv8ZzR3BnMlqh/JoYLfu/EQen1uwARSUw/qEkE1ZWinQU3Io4k4JRJxh25VYTpxAzOSQj8eERdp99m9xBffBbeuEXScgu0NV Q1Od7eFJ M3+bLNJbzoTL4Cvdk/GKfVgmMzKxlq77BCUEpyi1EvgOXCgVzrZtfdjPWNqc3NwjKEkkHIsQO01lkg25AbxvdYHoBprwb1w+YINFtctDC/pCoHlN1YV9/tOKPuwOIkRjXo6pOpx16f3VggXxRgmkit1dwvf7eskw9w8iKS9ICSa4IuV98DXX0n8w+lZqR/+gH6aXdA7gUNQLj2KnQ9SnZpLxBPgMmxH2IK/an1UxWrS/34mMoJ+mQ6NiE/nUZn8klL08gCsEAU3tZg+/e9XOGxVHTk9BlC5lhPdBcLrPBmcBzhYoJePxzJ6N9KZzAYaig2Xqs68mgrtKAd+ISC4jaM/FCtjuVxbKLw12A6WZ++iSEVGKm9Y8XNfBlQFXN7E6W/hERlYq9dnnluY7miu2OqvCwgqUh1Kc/S8hdWsze3tynTLLsQ1lOKc4Jd9hrx0y4jPUUTGY0sQ6pyplDKloyanP3gEXH4Fc/BJlD2vem8sLamq3FgKI5iC1LM1aEkYhN4NFKH1/5I7YZnDqzqfityWNhPaGCs1whtOxK X-Bogosity: Ham, tests=bogofilter, spamicity=0.000003, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Carves out space in arch specific thread struct for cfi status and shadow stack in usermode on riscv. This patch does following - defines a new structure cfi_status with status bit for cfi feature - defines shadow stack pointer, base and size in cfi_status structure - defines offsets to new member fields in thread in asm-offsets.c - Saves and restore shadow stack pointer on trap entry (U --> S) and exit (S --> U) Shadow stack save/restore is gated on feature availiblity and implemented using alternative. CSR can be context switched in `switch_to` as well but soon as kernel shadow stack support gets rolled in, shadow stack pointer will need to be switched at trap entry/exit point (much like `sp`). It can be argued that kernel using shadow stack deployment scenario may not be as prevalant as user mode using this feature. But even if there is some minimal deployment of kernel shadow stack, that means that it needs to be supported. And thus save/restore of shadow stack pointer in entry.S instead of in `switch_to.h`. Signed-off-by: Deepak Gupta Reviewed-by: Charlie Jenkins --- arch/riscv/include/asm/processor.h | 1 + arch/riscv/include/asm/thread_info.h | 3 +++ arch/riscv/include/asm/usercfi.h | 24 ++++++++++++++++++++++++ arch/riscv/kernel/asm-offsets.c | 4 ++++ arch/riscv/kernel/entry.S | 26 ++++++++++++++++++++++++++ 5 files changed, 58 insertions(+) create mode 100644 arch/riscv/include/asm/usercfi.h diff --git a/arch/riscv/include/asm/processor.h b/arch/riscv/include/asm/processor.h index d61587964bd7..83d6ca4e0bba 100644 --- a/arch/riscv/include/asm/processor.h +++ b/arch/riscv/include/asm/processor.h @@ -14,6 +14,7 @@ #include #include +#include /* * addr is a hint to the maximum userspace address that mmap should provide, so diff --git a/arch/riscv/include/asm/thread_info.h b/arch/riscv/include/asm/thread_info.h index c74536194626..cb694aef337d 100644 --- a/arch/riscv/include/asm/thread_info.h +++ b/arch/riscv/include/asm/thread_info.h @@ -58,6 +58,9 @@ struct thread_info { int cpu; unsigned long syscall_work; /* SYSCALL_WORK_ flags */ unsigned long envcfg; +#ifdef CONFIG_RISCV_USER_CFI + struct cfi_status user_cfi_state; +#endif #ifdef CONFIG_SHADOW_CALL_STACK void *scs_base; void *scs_sp; diff --git a/arch/riscv/include/asm/usercfi.h b/arch/riscv/include/asm/usercfi.h new file mode 100644 index 000000000000..4fa201b4fc4e --- /dev/null +++ b/arch/riscv/include/asm/usercfi.h @@ -0,0 +1,24 @@ +/* SPDX-License-Identifier: GPL-2.0 + * Copyright (C) 2024 Rivos, Inc. + * Deepak Gupta + */ +#ifndef _ASM_RISCV_USERCFI_H +#define _ASM_RISCV_USERCFI_H + +#ifndef __ASSEMBLY__ +#include + +#ifdef CONFIG_RISCV_USER_CFI +struct cfi_status { + unsigned long ubcfi_en : 1; /* Enable for backward cfi. */ + unsigned long rsvd : ((sizeof(unsigned long)*8) - 1); + unsigned long user_shdw_stk; /* Current user shadow stack pointer */ + unsigned long shdw_stk_base; /* Base address of shadow stack */ + unsigned long shdw_stk_size; /* size of shadow stack */ +}; + +#endif /* CONFIG_RISCV_USER_CFI */ + +#endif /* __ASSEMBLY__ */ + +#endif /* _ASM_RISCV_USERCFI_H */ diff --git a/arch/riscv/kernel/asm-offsets.c b/arch/riscv/kernel/asm-offsets.c index b09ca5f944f7..5457f9070cff 100644 --- a/arch/riscv/kernel/asm-offsets.c +++ b/arch/riscv/kernel/asm-offsets.c @@ -45,6 +45,10 @@ void asm_offsets(void) #endif OFFSET(TASK_TI_CPU_NUM, task_struct, thread_info.cpu); +#ifdef CONFIG_RISCV_USER_CFI + OFFSET(TASK_TI_CFI_STATUS, task_struct, thread_info.user_cfi_state); + OFFSET(TASK_TI_USER_SSP, task_struct, thread_info.user_cfi_state.user_shdw_stk); +#endif OFFSET(TASK_THREAD_F0, task_struct, thread.fstate.f[0]); OFFSET(TASK_THREAD_F1, task_struct, thread.fstate.f[1]); OFFSET(TASK_THREAD_F2, task_struct, thread.fstate.f[2]); diff --git a/arch/riscv/kernel/entry.S b/arch/riscv/kernel/entry.S index ac2e908d4418..ca9203e6d76d 100644 --- a/arch/riscv/kernel/entry.S +++ b/arch/riscv/kernel/entry.S @@ -60,6 +60,20 @@ SYM_CODE_START(handle_exception) REG_L s0, TASK_TI_USER_SP(tp) csrrc s1, CSR_STATUS, t0 + /* + * If previous mode was U, capture shadow stack pointer and save it away + * Zero CSR_SSP at the same time for sanitization. + */ + ALTERNATIVE("nop; nop; nop; nop", + __stringify( \ + andi s2, s1, SR_SPP; \ + bnez s2, skip_ssp_save; \ + csrrw s2, CSR_SSP, x0; \ + REG_S s2, TASK_TI_USER_SSP(tp); \ + skip_ssp_save:), + 0, + RISCV_ISA_EXT_ZICFISS, + CONFIG_RISCV_USER_CFI) csrr s2, CSR_EPC csrr s3, CSR_TVAL csrr s4, CSR_CAUSE @@ -149,6 +163,18 @@ SYM_CODE_START_NOALIGN(ret_from_exception) * structures again. */ csrw CSR_SCRATCH, tp + + /* + * Going back to U mode, restore shadow stack pointer + */ + ALTERNATIVE("nop; nop", + __stringify( \ + REG_L s3, TASK_TI_USER_SSP(tp); \ + csrw CSR_SSP, s3), + 0, + RISCV_ISA_EXT_ZICFISS, + CONFIG_RISCV_USER_CFI) + 1: #ifdef CONFIG_RISCV_ISA_V_PREEMPTIVE move a0, sp From patchwork Thu Sep 12 23:16:30 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13802834 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 A7431EEE270 for ; Thu, 12 Sep 2024 23:17:58 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 357336B00A5; Thu, 12 Sep 2024 19:17:58 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 2E02D6B00A6; Thu, 12 Sep 2024 19:17:58 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 10C7F6B00A7; Thu, 12 Sep 2024 19:17:58 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id E1B276B00A5 for ; Thu, 12 Sep 2024 19:17:57 -0400 (EDT) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 67618C0D48 for ; Thu, 12 Sep 2024 23:17:57 +0000 (UTC) X-FDA: 82557650994.29.A138B14 Received: from mail-pj1-f44.google.com (mail-pj1-f44.google.com [209.85.216.44]) by imf04.hostedemail.com (Postfix) with ESMTP id 80F5040019 for ; Thu, 12 Sep 2024 23:17:55 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=dLl6tH4y; spf=pass (imf04.hostedemail.com: domain of debug@rivosinc.com designates 209.85.216.44 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1726183021; a=rsa-sha256; cv=none; b=imUtD/FmM+tRizwAgk2J2bR7RaGYOh9qtW87UM6RK9uebMYqImeO/KWa8slzvPAe1T4qTt lkVO4+YwxjYjc4m1DfVEAq1yWazIinbNNtw7nK42zC4mU9P3sa0Tyh+lgVYWBQgmqewIKU B0cRu7ZqcK086+4S0Slv0ctTghzhfqY= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=dLl6tH4y; spf=pass (imf04.hostedemail.com: domain of debug@rivosinc.com designates 209.85.216.44 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=1726183021; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=eiprnGA2BOGVGzJqIZ9S6uz8auKceb/MWPdqRC4IaOM=; b=j5xA7B6T/DCZ1/DV3VYvito6G2rc4yX3Dec7u4qY8kAk1UItvo55MGU2l0emyGmNW/W62H VhRo24s+uFWbxxYXa39B9SUASr7/FG4fpUy6cGnr6P+wiHWLuBQrZnch+wWt2tUPid3e8V VXe/RULfOh784vKXx0Ic0AhI1RGEGR0= Received: by mail-pj1-f44.google.com with SMTP id 98e67ed59e1d1-2d873dc644dso1259520a91.3 for ; Thu, 12 Sep 2024 16:17:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1726183074; x=1726787874; darn=kvack.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=eiprnGA2BOGVGzJqIZ9S6uz8auKceb/MWPdqRC4IaOM=; b=dLl6tH4y8OZeKzPytdtVskzL+G8Ynpyj+AfrCB+FAGvsKTSO4PNS48vHMp6iL6WA6z WWTAHm/BuTbLdiZUJvMnKUYEiNQ/fz2Q5wVa+EJmhGRUztIPxrGmVp7OXvAK/kPfZXNp IJALHGRBhuRZkYhKrkvpmaKm9aOIL6TLb1BTNN8qqrAhdLbmZfIq3OHpuzPoEtdXXRPX hPdgffywUdtbmc3tWNg3r0ZbJPaFsm1mVCBod/kH6W4khzPD84PIpC7quzMUOHZ1UURM IHbB+waYb0eqk/OzCqw2ZgeJ2ZoGIOQf4HMBt4R8RsWg+LANECad5m4VQ3GKvaW7xmRT 4niQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726183074; x=1726787874; 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=eiprnGA2BOGVGzJqIZ9S6uz8auKceb/MWPdqRC4IaOM=; b=DS+u6fSUT3bDiE4oAvxdWj32sCS7xEwunkvtcUShc1GclQ2uvndqZxi9zmUisaVJeS UmilN30OM8U3uWnrDtoeL1FJelC/uXfEPNhozj8BsTH119NzFklmaloZ9mxQWqtIJ6V+ iVK/BMyQcyk1fn59Ma6KYSuERk4WF+S6dH6QhWXW6zOEKVy1wSyNo+SvHcvyAfewvOoM Q4JjcWyOB/PXRwABA2RAjHwLM2Ut70RJzIaFrqPMUKYuzrSKDPiRuxDQVpvU59tIcdIC yNvMk0Jb8CZhOraI5ajtH7VGSiz9Lw5C1N9PYxbclZ2yvKm/Cn7qpR+7RO5zLch9WUTy HdBg== X-Forwarded-Encrypted: i=1; AJvYcCVDqnKer6wv97/te2a3Z1wZGmwmfB3jVJ5DlyIh6E8Zk77yplrFF8V9L563Xh8KjuNMwtz7rKiuMw==@kvack.org X-Gm-Message-State: AOJu0Yx0/S3/rNPSvB4FuFUd1n1F8IYu4Jm5tmhcLf/Mpx63/hafFHl0 nTffk4qxhryndNYKaP6wY+g1QzsAr+MGJtURwRuZUHfiW9dQvwdBlSpuaGUj+0Y= X-Google-Smtp-Source: AGHT+IEnJrdTR1Y5qPMoOCKu4YCibaPdfaIv67ki6aFPE+yQ7lsYshwSwmMRsNa90sGyf+0sNSEfcA== X-Received: by 2002:a17:90a:8a13:b0:2da:bd08:edc2 with SMTP id 98e67ed59e1d1-2db9ffcaaaamr5258868a91.9.1726183074122; Thu, 12 Sep 2024 16:17:54 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2db6c1ac69asm3157591a91.0.2024.09.12.16.17.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 12 Sep 2024 16:17:53 -0700 (PDT) From: Deepak Gupta To: paul.walmsley@sifive.com, palmer@sifive.com, conor@kernel.org, linux-doc@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kselftest@vger.kernel.org Cc: corbet@lwn.net, palmer@dabbelt.com, aou@eecs.berkeley.edu, robh@kernel.org, krzk+dt@kernel.org, oleg@redhat.com, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, hpa@zytor.com, peterz@infradead.org, akpm@linux-foundation.org, arnd@arndb.de, ebiederm@xmission.com, kees@kernel.org, Liam.Howlett@oracle.com, vbabka@suse.cz, lorenzo.stoakes@oracle.com, shuah@kernel.org, brauner@kernel.org, samuel.holland@sifive.com, debug@rivosinc.com, andy.chiu@sifive.com, jerry.shih@sifive.com, greentime.hu@sifive.com, charlie@rivosinc.com, evan@rivosinc.com, cleger@rivosinc.com, xiao.w.wang@intel.com, ajones@ventanamicro.com, anup@brainfault.org, mchitale@ventanamicro.com, atishp@rivosinc.com, sameo@rivosinc.com, bjorn@rivosinc.com, alexghiti@rivosinc.com, david@redhat.com, libang.li@antgroup.com, jszhang@kernel.org, leobras@redhat.com, guoren@kernel.org, samitolvanen@google.com, songshuaishuai@tinylab.org, costa.shul@redhat.com, bhe@redhat.com, zong.li@sifive.com, puranjay@kernel.org, namcaov@gmail.com, antonb@tenstorrent.com, sorear@fastmail.com, quic_bjorande@quicinc.com, ancientmodern4@gmail.com, ben.dooks@codethink.co.uk, quic_zhonhan@quicinc.com, cuiyunhui@bytedance.com, yang.lee@linux.alibaba.com, ke.zhao@shingroup.cn, sunilvl@ventanamicro.com, tanzhasanwork@gmail.com, schwab@suse.de, dawei.li@shingroup.cn, rppt@kernel.org, willy@infradead.org, usama.anjum@collabora.com, osalvador@suse.de, ryan.roberts@arm.com, andrii@kernel.org, alx@kernel.org, catalin.marinas@arm.com, broonie@kernel.org, revest@chromium.org, bgray@linux.ibm.com, deller@gmx.de, zev@bewilderbeest.net Subject: [PATCH v4 11/30] riscv/mm : ensure PROT_WRITE leads to VM_READ | VM_WRITE Date: Thu, 12 Sep 2024 16:16:30 -0700 Message-ID: <20240912231650.3740732-12-debug@rivosinc.com> X-Mailer: git-send-email 2.45.0 In-Reply-To: <20240912231650.3740732-1-debug@rivosinc.com> References: <20240912231650.3740732-1-debug@rivosinc.com> MIME-Version: 1.0 X-Stat-Signature: ydu351a8giug3n7zs69mge5zri485gzj X-Rspamd-Queue-Id: 80F5040019 X-Rspam-User: X-Rspamd-Server: rspam10 X-HE-Tag: 1726183075-203360 X-HE-Meta: U2FsdGVkX1/XntOAChaXdbX1NJXGGEBz5wjs9JKIzmPcDSg5twthtQn6/dxMQL1SEykr5KUfPdZEs8P3TgKgDLhxYnssPrS3t+HtiRkcslnWbIWD0Cm8atsqjEZRFf3wUBrlY30QZ82QoOBkj6KpX+cnYmEKjujG9PK1duV0/1QixWHr4hoiPmmEp9jtR5v6bzetj64fy5D1L+alpHZ03zMy5Gj6wVkynj5SLcWtVGhxX0bE8rfEi4qKaxNtbVjzKLykiQR2w6p/L/IfsFnTODKjPdr2wOK+CnrxHThnE2ACnFcUqkwnaTUD/ozRa5Bw5mlfJTSWkM164628eed16coYgSgvxHJofp38BvF0OiP9xX0fr0mGpfYbmDOvV9wW8QHoENgs7o1XGBFDzikeP8NwNNQhua3IX9aZHZvntrLyRZjXBMcBLjpqaGc5YFMRBQlTEvqqabI/gdIf8U5swk3sWp1J2ErQzv1oeDq8sXHtFqQqlud31C3lodN8NRmBjAhFdOfkDs3YEPnCGJ2yKcRuKw7snptsruZUB2my4yF54UHTZ/S8rY2MKgUnXGRpFoqdQyZlmc2Vj2rwenOnEwDvmhPEelYRnqKm/Q3TzKyt9VPaU4TU+YGmxeDPVh+mddZUU+rlYKd4QQcxWTj/5QBIFKryStyrtB6OEg2hDcmlh0785MkEe24r+lhz6Iay3hvUeQ646YG+3qckUuEJS/r7H95oRl3HAQT8xGr9Sl/bBQRyB+LfKvOLUEFwJlmeeIKV4K9hBCpFWPuOCYaxB2MaUQqQ0LiEsC/syztgFEmstjOzHsAaUclTbZsTV9h3cnb+NGMwOTNDYIQOFGmgnCSwraGcSkBs+p3nlJpgG/beRrbFHRIW1agIEFPAypabsCyb/RxXro2YSb6JXFAB4xXJgWkdVGZ2exETj1IYvzVFfKl4g6Z0KPH25+yYCAHyw8BgrYO4AjuQrrsCdv2 BWTNmL0S A28Ox5eqZDhdeyd2BCD45SPcuq02dg4gWjs6RZ0KfoY1zpt0BtSYomZdnoSVf9gkO9KUfhaX5DNWcsWaWONlzKa+Zb7cTTWNb1skMs5aIAzzbJORj2B0NfiOkCtLd4Gu5Y5/umVHuOFNCLMmoQvrgnZEiwBj5J7gvk1+fTBsTH2S/ca7Hi1DwZM/WaeduH03dR5dRrjbkggDroODHySWxWsKOYK59r+erjosbP9jTXSf4Wje7feaw4M5gASiYSEz0SH0GC9G1vXT179D728x1VeN3ikMk4MsBSXATFCsgrpVYallS6GMGaXBpmuRHsaJqOsyuNRbMOO+gyXE8Ddc3ng8K6WD34HzcG+YbYamygecnkh8d7PBm0xV2aMGnsdaxHID9FGXtoqsxX1yMfrQCXW5hQKY0nget8P7sUy4GXhWzu/569IjNpEKnUxyr1WFuvNufuIHABJvA7ftgEu81PE406Fw6kQmRiHMIPHzlRkBITB+GCe1BlCXa2g== 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: `arch_calc_vm_prot_bits` is implemented on risc-v to return VM_READ | VM_WRITE if PROT_WRITE is specified. Similarly `riscv_sys_mmap` is updated to convert all incoming PROT_WRITE to (PROT_WRITE | PROT_READ). This is to make sure that any existing apps using PROT_WRITE still work. Earlier `protection_map[VM_WRITE]` used to pick read-write PTE encodings. Now `protection_map[VM_WRITE]` will always pick PAGE_SHADOWSTACK PTE encodings for shadow stack. Above changes ensure that existing apps continue to work because underneath kernel will be picking `protection_map[VM_WRITE|VM_READ]` PTE encodings. Signed-off-by: Deepak Gupta --- arch/riscv/include/asm/mman.h | 24 ++++++++++++++++++++++++ arch/riscv/include/asm/pgtable.h | 1 + arch/riscv/kernel/sys_riscv.c | 10 ++++++++++ arch/riscv/mm/init.c | 2 +- mm/mmap.c | 1 + 5 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 arch/riscv/include/asm/mman.h diff --git a/arch/riscv/include/asm/mman.h b/arch/riscv/include/asm/mman.h new file mode 100644 index 000000000000..ef9fedf32546 --- /dev/null +++ b/arch/riscv/include/asm/mman.h @@ -0,0 +1,24 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef __ASM_MMAN_H__ +#define __ASM_MMAN_H__ + +#include +#include +#include + +static inline unsigned long arch_calc_vm_prot_bits(unsigned long prot, + unsigned long pkey __always_unused) +{ + unsigned long ret = 0; + + /* + * If PROT_WRITE was specified, force it to VM_READ | VM_WRITE. + * Only VM_WRITE means shadow stack. + */ + if (prot & PROT_WRITE) + ret = (VM_READ | VM_WRITE); + return ret; +} +#define arch_calc_vm_prot_bits(prot, pkey) arch_calc_vm_prot_bits(prot, pkey) + +#endif /* ! __ASM_MMAN_H__ */ diff --git a/arch/riscv/include/asm/pgtable.h b/arch/riscv/include/asm/pgtable.h index 089f3c9f56a3..af4337774fe5 100644 --- a/arch/riscv/include/asm/pgtable.h +++ b/arch/riscv/include/asm/pgtable.h @@ -181,6 +181,7 @@ extern struct pt_alloc_ops pt_ops __meminitdata; #define PAGE_READ_EXEC __pgprot(_PAGE_BASE | _PAGE_READ | _PAGE_EXEC) #define PAGE_WRITE_EXEC __pgprot(_PAGE_BASE | _PAGE_READ | \ _PAGE_EXEC | _PAGE_WRITE) +#define PAGE_SHADOWSTACK __pgprot(_PAGE_BASE | _PAGE_WRITE) #define PAGE_COPY PAGE_READ #define PAGE_COPY_EXEC PAGE_READ_EXEC diff --git a/arch/riscv/kernel/sys_riscv.c b/arch/riscv/kernel/sys_riscv.c index d77afe05578f..43a448bf254b 100644 --- a/arch/riscv/kernel/sys_riscv.c +++ b/arch/riscv/kernel/sys_riscv.c @@ -7,6 +7,7 @@ #include #include +#include static long riscv_sys_mmap(unsigned long addr, unsigned long len, unsigned long prot, unsigned long flags, @@ -16,6 +17,15 @@ static long riscv_sys_mmap(unsigned long addr, unsigned long len, if (unlikely(offset & (~PAGE_MASK >> page_shift_offset))) return -EINVAL; + /* + * If PROT_WRITE is specified then extend that to PROT_READ + * protection_map[VM_WRITE] is now going to select shadow stack encodings. + * So specifying PROT_WRITE actually should select protection_map [VM_WRITE | VM_READ] + * If user wants to create shadow stack then they should use `map_shadow_stack` syscall. + */ + if (unlikely((prot & PROT_WRITE) && !(prot & PROT_READ))) + prot |= PROT_READ; + return ksys_mmap_pgoff(addr, len, prot, flags, fd, offset >> (PAGE_SHIFT - page_shift_offset)); } diff --git a/arch/riscv/mm/init.c b/arch/riscv/mm/init.c index eb0649a61b4c..000b51c26943 100644 --- a/arch/riscv/mm/init.c +++ b/arch/riscv/mm/init.c @@ -324,7 +324,7 @@ pgd_t early_pg_dir[PTRS_PER_PGD] __initdata __aligned(PAGE_SIZE); static const pgprot_t protection_map[16] = { [VM_NONE] = PAGE_NONE, [VM_READ] = PAGE_READ, - [VM_WRITE] = PAGE_COPY, + [VM_WRITE] = PAGE_SHADOWSTACK, [VM_WRITE | VM_READ] = PAGE_COPY, [VM_EXEC] = PAGE_EXEC, [VM_EXEC | VM_READ] = PAGE_READ_EXEC, diff --git a/mm/mmap.c b/mm/mmap.c index d0dfc85b209b..04023a464fac 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -47,6 +47,7 @@ #include #include #include +#include #include #include From patchwork Thu Sep 12 23:16:31 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13802835 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 A086DEEE272 for ; Thu, 12 Sep 2024 23:18:02 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 331F36B00A7; Thu, 12 Sep 2024 19:18:02 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 2B8C56B00A8; Thu, 12 Sep 2024 19:18:02 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0E59A6B00A9; Thu, 12 Sep 2024 19:18:02 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id DE5CA6B00A7 for ; Thu, 12 Sep 2024 19:18:01 -0400 (EDT) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 94E44A882B for ; Thu, 12 Sep 2024 23:18:01 +0000 (UTC) X-FDA: 82557651162.15.C6C8223 Received: from mail-pg1-f178.google.com (mail-pg1-f178.google.com [209.85.215.178]) by imf16.hostedemail.com (Postfix) with ESMTP id BE74B180004 for ; Thu, 12 Sep 2024 23:17:59 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=ZbJXHfO7; spf=pass (imf16.hostedemail.com: domain of debug@rivosinc.com designates 209.85.215.178 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=1726182973; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=KiNSEhiMyi/vclkP3i3QvX4mx663SI7U/pxriPsJ9Y4=; b=ZHFmBhNtLTpqWWUMIy1lgoUTBvJyLR04dgacmiLX9PKaFpTqjXJgcl7greNdwvI3WEhMnN facm+I6dZgeWRCJ+0rZrPwHYsjw0MnzRQ7Jr1VRx3eVw/O3mFt+25icUZegxvSbXZgnPd9 /fxl8+9kJS9cQiDBIPq2sKCjQZPKmdU= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1726182973; a=rsa-sha256; cv=none; b=aR2SNhRwZ051AiXi+HxPM0tv7sYaR0OIVimskWFdDPM3sn5ae/11JNYXDn9cXKGgrsXgaa vhlGrIMe9P5+YBZn0AOJvbNT1+Zz3scOn4eAdEwrt4B8mJM9ilnYVU341GcwtLOW29zd94 6spd8HUhrsr8LTI/KLlUUPN5mkKsF40= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=ZbJXHfO7; spf=pass (imf16.hostedemail.com: domain of debug@rivosinc.com designates 209.85.215.178 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none Received: by mail-pg1-f178.google.com with SMTP id 41be03b00d2f7-7c3d9a5e050so82432a12.2 for ; Thu, 12 Sep 2024 16:17:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1726183078; x=1726787878; darn=kvack.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=KiNSEhiMyi/vclkP3i3QvX4mx663SI7U/pxriPsJ9Y4=; b=ZbJXHfO7qd7LwcwHzIupNCI44V3gQDiJszzevw/Z/MUhnCvazhVphP1cLm07+K8e/t hN3z0E7SM0fflaU8qMZo+7W6TUf9AQpVQLay/Xn6N59s57uRSiHx5Y6V8ywmNiXjqktO D8+5GGPisylr1sxB1MzJDk0pfrqSsN2uiJxVM1SasYdb0JqX0WL7pcBqrqA6qDoPPk/Y TXxHanvrBDwbUkFDw2dH6RM3CyPRMQA8qCGRV4qoqgndPkTOJMBBY8694xgXq3Auyoyz +5tsSeRLpQSj2yjHeC9Ozv6s43CJnajnkkv1xVu9IiBjGXyMVNmAXkzUfALc5Gbw44hN kzVA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726183078; x=1726787878; 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=KiNSEhiMyi/vclkP3i3QvX4mx663SI7U/pxriPsJ9Y4=; b=Vms/IPgifn88CmzXrrA165UOqFz35u3AhRpLmU6OlrtUZVEI1mnlLIh+oclwDXLKAH ek8pQX3ONRKhW4LvqGx7VLZZfmKo3GQa+YlLEdN1WQghTfJNK/wuVj57E0a2C3zgwmpx DEH0iX6KuWmtg/hnYQl6ip20yPo9FI/5LY7gFAXv+nHMahH8LQPFnShOwindRJMjx3vO zZVp6P8fkRHsv0CtzfcF4l+WpIFof/SqJ0uywlZhlGaG8Pw6h84FB4zqM6YqZaU/RwnM xZQdpSfAJLfhD68Fau2tZJ95VmMVg99NTwK7vQ2NHIouG69M+fufCmzNk7JINjYeUaLb x6kQ== X-Forwarded-Encrypted: i=1; AJvYcCXG9hcg9+EQCOINSXXExbbAI/I1abSkCl5wyuxSbkyp/Wycwx8PGWW3fcqRT9+bSRMVudtPCW8ojA==@kvack.org X-Gm-Message-State: AOJu0Yz/iGDI7NgzDWVJTuNJ2XZttNCYFdCuOLn1qe3hUD3Lg+qv8NAo K+hEL1BaCPqlBG1rYkmQax0w5XsBotiwiqxSoibHpRwr4ktYu/BnsTA7fpo9POY= X-Google-Smtp-Source: AGHT+IF4e0NCjicspxUUMmlG4elk65ZCg5cB6xw18ixrBvYAVw02n2WNOur1Dg21kRqmRwms2SEZcA== X-Received: by 2002:a17:90a:fd04:b0:2c9:a3d4:f044 with SMTP id 98e67ed59e1d1-2db9ffbcbd4mr5139066a91.11.1726183078465; Thu, 12 Sep 2024 16:17:58 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2db6c1ac69asm3157591a91.0.2024.09.12.16.17.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 12 Sep 2024 16:17:58 -0700 (PDT) From: Deepak Gupta To: paul.walmsley@sifive.com, palmer@sifive.com, conor@kernel.org, linux-doc@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kselftest@vger.kernel.org Cc: corbet@lwn.net, palmer@dabbelt.com, aou@eecs.berkeley.edu, robh@kernel.org, krzk+dt@kernel.org, oleg@redhat.com, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, hpa@zytor.com, peterz@infradead.org, akpm@linux-foundation.org, arnd@arndb.de, ebiederm@xmission.com, kees@kernel.org, Liam.Howlett@oracle.com, vbabka@suse.cz, lorenzo.stoakes@oracle.com, shuah@kernel.org, brauner@kernel.org, samuel.holland@sifive.com, debug@rivosinc.com, andy.chiu@sifive.com, jerry.shih@sifive.com, greentime.hu@sifive.com, charlie@rivosinc.com, evan@rivosinc.com, cleger@rivosinc.com, xiao.w.wang@intel.com, ajones@ventanamicro.com, anup@brainfault.org, mchitale@ventanamicro.com, atishp@rivosinc.com, sameo@rivosinc.com, bjorn@rivosinc.com, alexghiti@rivosinc.com, david@redhat.com, libang.li@antgroup.com, jszhang@kernel.org, leobras@redhat.com, guoren@kernel.org, samitolvanen@google.com, songshuaishuai@tinylab.org, costa.shul@redhat.com, bhe@redhat.com, zong.li@sifive.com, puranjay@kernel.org, namcaov@gmail.com, antonb@tenstorrent.com, sorear@fastmail.com, quic_bjorande@quicinc.com, ancientmodern4@gmail.com, ben.dooks@codethink.co.uk, quic_zhonhan@quicinc.com, cuiyunhui@bytedance.com, yang.lee@linux.alibaba.com, ke.zhao@shingroup.cn, sunilvl@ventanamicro.com, tanzhasanwork@gmail.com, schwab@suse.de, dawei.li@shingroup.cn, rppt@kernel.org, willy@infradead.org, usama.anjum@collabora.com, osalvador@suse.de, ryan.roberts@arm.com, andrii@kernel.org, alx@kernel.org, catalin.marinas@arm.com, broonie@kernel.org, revest@chromium.org, bgray@linux.ibm.com, deller@gmx.de, zev@bewilderbeest.net Subject: [PATCH v4 12/30] riscv mm: manufacture shadow stack pte Date: Thu, 12 Sep 2024 16:16:31 -0700 Message-ID: <20240912231650.3740732-13-debug@rivosinc.com> X-Mailer: git-send-email 2.45.0 In-Reply-To: <20240912231650.3740732-1-debug@rivosinc.com> References: <20240912231650.3740732-1-debug@rivosinc.com> MIME-Version: 1.0 X-Stat-Signature: da9cmgdf4kb9o68rcbz5e7crc4ig55hk X-Rspamd-Queue-Id: BE74B180004 X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1726183079-224342 X-HE-Meta: U2FsdGVkX18Aas100Xb9OqTjiqcK65chfvEaOFMtIIQZPCaa6uLBwNRyeBz9HcsueewhQWjUfYQv6+H6cup8NC+ZriGdy+KQACvOWrUAzDhpnrtelCA0JF28JmQrssW4hJf2SsUNRv1F5cidfbFtPk7HN51pvve8vzthpeh2s1TZRuaggUnPAj9o2Kv4eCUcP3sc/B/qkWcVPwKM5mhvkQvpeUgFFzaH+OQfhl+OT2qQwkHrmdUcl5xrU0g2F/bkYOu2ShYXnIlgtv5fIKj/QPwwtMQPcqryfS7VQboXKhJSvrzbQDSsZsZB1+alUrkT7pHuX8ZOb2HS3fYa1Aaao63tj+0w7Jh5ok6cHcgPTvJthup/oQcaspHSbdN8hL2+LS9bhm02MbvgUcuzorbOWcRcM2zjnykenQHpUittcftZpPDLh8qhWzl4z/NaYZm+EhufDbPIeBwcKK8kQNETq90GGwCPhULDdKdrMqfiDsjkjCTtbGkDadv8qXCrYD0Hq2qja6/1VT3BTB2YRPQlDT7NMhpOW/+333JB5beGFIoO5fH4BbqoRB46LeBfpV+DKk1s8rL6NZofp7eusKGJ+tWLJQStIyrdFbITxw9MM3nNPuuja22jIGPrm4OWpkdp/g6wvyYfSbyAIHJtpECxpmCFbGXrrfxnNQxFIgNRpyLyaToL/Abum28Nwf6mieQv1OaZbUI4Qm9vYEmbNq4zIHQWIhdrCUTLtF0fpl9wUYSqL/3HDN0Y+jv6woHYk9M6fXFl7oFPdo0R/93CauQcnuz9MyqraEK+/GmP6tsHmU/7ZReHpxxpZskmXttjTUS3O3slmmN+nPVHyMeuKzwQWPgaMrL6pG4fm9Ut7Nic10tKaa6gyOp7Ide5Z5dXyM+zmx0DK1EQT+ZwruR2HQJQkns9UhyEjP/MSAvvpdxTH/lSL+TEsvTyvvA206kSQPG+bNh2GO9e71zHNOvaYwp 3IapmAPr G3m6R8xDA/H8Lh1ie5OMD4sNXp/b5OiJ7NzyDD2wuM1ZMmnf/4bqpvK3Kh+ZbRTxg4tc3b2aXqM+efYEZkykc497u/EU4N4GNm4SlTvsXx8NOI0w0BgJRRLvDp4CCOF7ZOn7wKVEkwF7pROfxBaY+aBsUp7Mb6wfddrHWNUmKHFda2L+XojQpsnOmW5M76WnntrcuXuEOq08sm/9G5bZOAfGh/QtBiQwYf+a6yIb+d1s59Ex79fyXpKbZICaIX3rZk+BE/dgsP1X+x7oqDy8/VQ1lg8IpXFsEbZ7is8SEAzma1aY3Qxsw/geVWtumneRLv3niW4z+oneX3iW4UPfWx+4lee8QhgYWB3Bcf+1xeM8uGPGniWHc6+fwvymII0PE0bC6LtK0vYnDDMrUav6ZWrCCGLcY9uEO9o+vxBUmaCYtPLQ= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000002, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: This patch implements creating shadow stack pte (on riscv). Creating shadow stack PTE on riscv means that clearing RWX and then setting W=1. Signed-off-by: Deepak Gupta Reviewed-by: Alexandre Ghiti --- arch/riscv/include/asm/pgtable.h | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/arch/riscv/include/asm/pgtable.h b/arch/riscv/include/asm/pgtable.h index af4337774fe5..0b6c66fb853a 100644 --- a/arch/riscv/include/asm/pgtable.h +++ b/arch/riscv/include/asm/pgtable.h @@ -425,6 +425,11 @@ static inline pte_t pte_mkwrite_novma(pte_t pte) return __pte(pte_val(pte) | _PAGE_WRITE); } +static inline pte_t pte_mkwrite_shstk(pte_t pte) +{ + return __pte((pte_val(pte) & ~(_PAGE_LEAF)) | _PAGE_WRITE); +} + /* static inline pte_t pte_mkexec(pte_t pte) */ static inline pte_t pte_mkdirty(pte_t pte) @@ -732,6 +737,11 @@ static inline pmd_t pmd_mkwrite_novma(pmd_t pmd) return pte_pmd(pte_mkwrite_novma(pmd_pte(pmd))); } +static inline pmd_t pmd_mkwrite_shstk(pmd_t pte) +{ + return __pmd((pmd_val(pte) & ~(_PAGE_LEAF)) | _PAGE_WRITE); +} + static inline pmd_t pmd_wrprotect(pmd_t pmd) { return pte_pmd(pte_wrprotect(pmd_pte(pmd))); From patchwork Thu Sep 12 23:16:32 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13802836 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 D55BFEEE271 for ; Thu, 12 Sep 2024 23:18:06 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 702C76B00A8; Thu, 12 Sep 2024 19:18:06 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 6B10D6B00AA; Thu, 12 Sep 2024 19:18:06 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4B5B36B00AB; Thu, 12 Sep 2024 19:18:06 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 2A6596B00A8 for ; Thu, 12 Sep 2024 19:18:06 -0400 (EDT) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id E3E98C0595 for ; Thu, 12 Sep 2024 23:18:05 +0000 (UTC) X-FDA: 82557651330.28.79BB934 Received: from mail-pg1-f169.google.com (mail-pg1-f169.google.com [209.85.215.169]) by imf19.hostedemail.com (Postfix) with ESMTP id 27A781A0007 for ; Thu, 12 Sep 2024 23:18:03 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=SQfku0bI; spf=pass (imf19.hostedemail.com: domain of debug@rivosinc.com designates 209.85.215.169 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=1726183031; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=2o2VksxHwPquNK6/4RcwFNcMNDB9IND71Ze1od4h7MM=; b=XcKgee1OYWcMZ4flGDC+NSgC8paj+ZXL+uEAQXZuL8D0yXN24TNeFD+4U2ugFmyefL+MB5 aYFwV0INFRMT0GXauZrRoN18q8TSGKv+qKxbqj7+pmECjfQ1hIqgZzvE90eNnoYJF3nK91 Oswcho/IXKzArcF+DF8hrs56rnbFXNk= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=SQfku0bI; spf=pass (imf19.hostedemail.com: domain of debug@rivosinc.com designates 209.85.215.169 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1726183031; a=rsa-sha256; cv=none; b=uCg7gyZTClG4HUCuvYqxHacgdDsjZEDHrzzRS1zjl9yRS023/Nbc/pxznHWc+tPSIMznRi li/U6e8XnQl46LfRIIRdrVXEmowd55VsaW+t7rPdm0B/NPHEdx9tCwXJ6jV7BmuLpi3X91 9MTvXQbBsm77x3xx+5GsV51rsny2UHk= Received: by mail-pg1-f169.google.com with SMTP id 41be03b00d2f7-70b2421471aso180296a12.0 for ; Thu, 12 Sep 2024 16:18:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1726183083; x=1726787883; darn=kvack.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=2o2VksxHwPquNK6/4RcwFNcMNDB9IND71Ze1od4h7MM=; b=SQfku0bIy28Mew+2NMELYMWCr1NXBxVBMrmgXBkjTDO6963xF0djPjN7gYx7aZTxZ5 Ue1wIwUOoBxIr7n+ImJzjKZ1ofGIixyroGsZpTKOQz8+zQRwpSIDA56ESIRsU6pts9RG mIKnWeGRJJta9E5uGPUcuF3cPVqduId14oEYlIObt8LmRgZQJSHQkxPH3uG5LxRM3OO3 j0E12NFxJLFSYG530DmQYlWG63AmhQRY1l4LCGF/l7HnerPH0P5AS2V8MJvvu8MDLcwo 1uyxOtP4W32H8jLRK/gFCWq9VY6Qn4Nv7ZL4CoUCNGpOSL0XRcjCVoTkuyRk3bKFobLQ RomA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726183083; x=1726787883; 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=2o2VksxHwPquNK6/4RcwFNcMNDB9IND71Ze1od4h7MM=; b=ohEfXbNvsCLfOUXQbpE8MKB2PjOmbVrpbNK/DavztlNOBxCz9SbQ3S0xdeHZZA7t++ f4SEaQVoo/zpVj7UVxIs74n/GOK/2dK5MLJyaQ7LSyPwzqeau/v0fwfEbR9D+0/3/3OT nBIrj1CbmPwDq3VBhRZfY31/E2dNuPvvjYe1TuXHiTe0+xkQQvLIHpICJBon5igFS9BO vX+nUE9++AYvEJsLegth+5mqHU2VuBa9xFJhemrQsiGqA56wvMc2FYI5ICQYG3LU/nbr 3ovbsN04BAhcRPpsshnaQe+T2Ghudyv8uo9S4aWMA3NLKeP1yE6YgfVtp1IXvDffpMCH fx6g== X-Forwarded-Encrypted: i=1; AJvYcCWoPnwJaU5IY2vYrD94Lw2wvdwCEYQ0RdEQB8tC4RIRGpx94fq2N1gvGFWoTd+fEJxQbPrNSgfgpQ==@kvack.org X-Gm-Message-State: AOJu0YxF1mCcSFgBXIMv6VCCrKOU1ZT1w+P5XIF59i4un6EYk1Q8ITQl qWIuXuu4cm7RAxRhXx1uDY+XJ/OlRdeA/KXegF3pakeeig8a2WcfUsNU5zOKiZ4= X-Google-Smtp-Source: AGHT+IFc5Xo3W7imJs6v1qZAyylQCnIMRJTm9oY3HnptRktjFuhlx1nDwXattiRmY7sUUOR5c5uRiQ== X-Received: by 2002:a17:90b:1e45:b0:2d3:b748:96dd with SMTP id 98e67ed59e1d1-2dbb9eda70cmr1036124a91.25.1726183082840; Thu, 12 Sep 2024 16:18:02 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2db6c1ac69asm3157591a91.0.2024.09.12.16.17.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 12 Sep 2024 16:18:02 -0700 (PDT) From: Deepak Gupta To: paul.walmsley@sifive.com, palmer@sifive.com, conor@kernel.org, linux-doc@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kselftest@vger.kernel.org Cc: corbet@lwn.net, palmer@dabbelt.com, aou@eecs.berkeley.edu, robh@kernel.org, krzk+dt@kernel.org, oleg@redhat.com, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, hpa@zytor.com, peterz@infradead.org, akpm@linux-foundation.org, arnd@arndb.de, ebiederm@xmission.com, kees@kernel.org, Liam.Howlett@oracle.com, vbabka@suse.cz, lorenzo.stoakes@oracle.com, shuah@kernel.org, brauner@kernel.org, samuel.holland@sifive.com, debug@rivosinc.com, andy.chiu@sifive.com, jerry.shih@sifive.com, greentime.hu@sifive.com, charlie@rivosinc.com, evan@rivosinc.com, cleger@rivosinc.com, xiao.w.wang@intel.com, ajones@ventanamicro.com, anup@brainfault.org, mchitale@ventanamicro.com, atishp@rivosinc.com, sameo@rivosinc.com, bjorn@rivosinc.com, alexghiti@rivosinc.com, david@redhat.com, libang.li@antgroup.com, jszhang@kernel.org, leobras@redhat.com, guoren@kernel.org, samitolvanen@google.com, songshuaishuai@tinylab.org, costa.shul@redhat.com, bhe@redhat.com, zong.li@sifive.com, puranjay@kernel.org, namcaov@gmail.com, antonb@tenstorrent.com, sorear@fastmail.com, quic_bjorande@quicinc.com, ancientmodern4@gmail.com, ben.dooks@codethink.co.uk, quic_zhonhan@quicinc.com, cuiyunhui@bytedance.com, yang.lee@linux.alibaba.com, ke.zhao@shingroup.cn, sunilvl@ventanamicro.com, tanzhasanwork@gmail.com, schwab@suse.de, dawei.li@shingroup.cn, rppt@kernel.org, willy@infradead.org, usama.anjum@collabora.com, osalvador@suse.de, ryan.roberts@arm.com, andrii@kernel.org, alx@kernel.org, catalin.marinas@arm.com, broonie@kernel.org, revest@chromium.org, bgray@linux.ibm.com, deller@gmx.de, zev@bewilderbeest.net Subject: [PATCH v4 13/30] riscv mmu: teach pte_mkwrite to manufacture shadow stack PTEs Date: Thu, 12 Sep 2024 16:16:32 -0700 Message-ID: <20240912231650.3740732-14-debug@rivosinc.com> X-Mailer: git-send-email 2.45.0 In-Reply-To: <20240912231650.3740732-1-debug@rivosinc.com> References: <20240912231650.3740732-1-debug@rivosinc.com> MIME-Version: 1.0 X-Rspamd-Server: rspam03 X-Rspam-User: X-Rspamd-Queue-Id: 27A781A0007 X-Stat-Signature: gcxtjsxma1bi4u53tcd13crh86ndxe76 X-HE-Tag: 1726183083-506176 X-HE-Meta: U2FsdGVkX19JjYoX832xVrp+/1jr/iLMySvqo5O+Vr0NdphE85nDJgmwe9VvrNDdj2K8aQj4MMFBTcwtGTBYe+JP8MEytCqyhGwDNAakklhaUehJd9xtClhz2KSbqSBqTEMdg5FIV7/LbrdSOQnt+dFYijuaHI7QaW/pmyfStZZ6YpQfUW+yfIvjUodMgN4tb9wiGPAVdh79xXZV2VBfDonK16p9tQFgXLFQYgt05REn3vRfd0F8wUe/TnwBTuoQLHaNRlhak6bEdudmx5TyjB9KHwzFnvm+QSb9JIHPVTWbQJTkJYsI1/kU/9AsMc1ycQCNBlJidzXB0t4GjKTyRSoqcpWkU3n1T/j1H4/x/Y9S92JL5GjiCyEjM3SOhCTeyboLvd+KQuiuSF0X5I+DzsObCvSB3P3dKH4ptjyCEzVJDLa5WKwOwe/Yoau8kDX0V4MGjhNbUfCxsB1LDn2rzwJZzMCpE0HIh8t3ILcaj5WhXhP7ZE3bde3dMrvKzc1+mdnzDOlvnGE355xuOgLpIrYhYwLKxFDeM8wkJlQtNz+dtch0xCTi6UnvyIzPTGW4NzcbV/SIILZhZnqrXSscXjbtMG4LGiSYvetKpspjY5izaiABdKrnYAhdtPizO0nT9vAYSKV7xnewqVXn94/zmggj9xFl+S41WNgigJYLcVWr8aj2a7Fonvs0N8II7nqqAly93SC/R0PNSwF/29XkAIOS01/aitS/pQrcR5eu5DE1KlfTS45dqln+JGOc37t3e1LhYOVUi5zXik33zbSW5KmOTn1KPAdp7z2p+v4stAf0IjLbXqxLp08BjG8O7bAijsaLS0H6mYpJeYRs593oyXKlDfdWXMpOOb+2DSfHqAQzCJP85KVfhgs3MA7vGOHpw2VphTPC42UFbvY56bhQ96DrvLDP8KUsp885szZ3XwdMiZdyL72+WW7Ye2BRcqrtRQsJmnLB5EJ06/0zsDp aswNGYZH Decd0qOR9m1RP7ZbK6Kt95r3LDhRypfmlw6LARUy+waEjmee16XJxDFq+lUB9lozwdf3hk8qIz5IVIsHwpcHYDAh6of4AbZcBHNhSMK1esaKKMsakuOB0wjHWX0Au1de4wmHoVfnSPUxa2pk9uJOotT5hDXmuf4U75kEnpob4yX/7IgW8EstXWlWDFmVtEdn+L4oyEPs3ECOT3PnT1xBL6u80bTpwPAewSd8FlG/PD3Rs4w3or2t2Ykbr/5sgXDLsv1yK4GHTbWFxVyXBHXJatnN9OTpRLToRyJOmARb1El9Hvu3gkX47RtzE18zVOhWG3+a8bdqWkeXha9pXWZmjpJdDlFGIHkY3kQI7hUsy9of4raTFKP/OpK4QU3xJg2VQ1kEZ2UPcz4nnwSfkxkQD/n5APlGolmHg5bRSjpKGRVeTM07PkmvhleuIP7xi8dRoeh3sGs//9nrdzDUXLnzsuIE4F+5T9b7toPdU 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: pte_mkwrite creates PTEs with WRITE encodings for underlying arch. Underlying arch can have two types of writeable mappings. One that can be written using regular store instructions. Another one that can only be written using specialized store instructions (like shadow stack stores). pte_mkwrite can select write PTE encoding based on VMA range (i.e. VM_SHADOW_STACK) Signed-off-by: Deepak Gupta Reviewed-by: Alexandre Ghiti --- arch/riscv/include/asm/pgtable.h | 7 +++++++ arch/riscv/mm/pgtable.c | 17 +++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/arch/riscv/include/asm/pgtable.h b/arch/riscv/include/asm/pgtable.h index 0b6c66fb853a..30fd4874e871 100644 --- a/arch/riscv/include/asm/pgtable.h +++ b/arch/riscv/include/asm/pgtable.h @@ -420,6 +420,10 @@ static inline pte_t pte_wrprotect(pte_t pte) /* static inline pte_t pte_mkread(pte_t pte) */ +struct vm_area_struct; +pte_t pte_mkwrite(pte_t pte, struct vm_area_struct *vma); +#define pte_mkwrite pte_mkwrite + static inline pte_t pte_mkwrite_novma(pte_t pte) { return __pte(pte_val(pte) | _PAGE_WRITE); @@ -732,6 +736,9 @@ static inline pmd_t pmd_mkyoung(pmd_t pmd) return pte_pmd(pte_mkyoung(pmd_pte(pmd))); } +pmd_t pmd_mkwrite(pmd_t pmd, struct vm_area_struct *vma); +#define pmd_mkwrite pmd_mkwrite + static inline pmd_t pmd_mkwrite_novma(pmd_t pmd) { return pte_pmd(pte_mkwrite_novma(pmd_pte(pmd))); diff --git a/arch/riscv/mm/pgtable.c b/arch/riscv/mm/pgtable.c index 533ec9055fa0..0dd6104a35ac 100644 --- a/arch/riscv/mm/pgtable.c +++ b/arch/riscv/mm/pgtable.c @@ -142,3 +142,20 @@ pmd_t pmdp_collapse_flush(struct vm_area_struct *vma, return pmd; } #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ + +pte_t pte_mkwrite(pte_t pte, struct vm_area_struct *vma) +{ + if (vma->vm_flags & VM_SHADOW_STACK) + return pte_mkwrite_shstk(pte); + + return pte_mkwrite_novma(pte); +} + +pmd_t pmd_mkwrite(pmd_t pmd, struct vm_area_struct *vma) +{ + if (vma->vm_flags & VM_SHADOW_STACK) + return pmd_mkwrite_shstk(pmd); + + return pmd_mkwrite_novma(pmd); +} + From patchwork Thu Sep 12 23:16:33 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13802837 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 3EEB2EEE271 for ; Thu, 12 Sep 2024 23:18:11 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C58A46B00AB; Thu, 12 Sep 2024 19:18:10 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C08216B00AC; Thu, 12 Sep 2024 19:18:10 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A33C16B00AD; Thu, 12 Sep 2024 19:18:10 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 825996B00AB for ; Thu, 12 Sep 2024 19:18:10 -0400 (EDT) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 46FD7C0C2B for ; Thu, 12 Sep 2024 23:18:10 +0000 (UTC) X-FDA: 82557651540.17.F59F85F Received: from mail-pl1-f177.google.com (mail-pl1-f177.google.com [209.85.214.177]) by imf28.hostedemail.com (Postfix) with ESMTP id 7027FC0007 for ; Thu, 12 Sep 2024 23:18:08 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=xa2q09UW; spf=pass (imf28.hostedemail.com: domain of debug@rivosinc.com designates 209.85.214.177 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=1726183059; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=VBBK/J5ZkF8zw3prIEqqwLEmBIMH3Wf5Ldc6S2vu7i8=; b=A2jWrfxqGRNjy5kcFOg3HfU2sGDsMLM3KsVKgp+xdet7Jv+ENVb89v09vysfHTRmzI7wDG uxnDo+WumVEjAvlMpDqtYyLuIpe8rmMVH+R5OViOW71pZ34W4o1faNGzN2QFrl40xiZEez 6mfamD1325rtfvz8sk6qxqz4+2g3gTc= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=xa2q09UW; spf=pass (imf28.hostedemail.com: domain of debug@rivosinc.com designates 209.85.214.177 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1726183059; a=rsa-sha256; cv=none; b=M/O1t60/ptiZkOn0Fa4CuTJyVQK2cjFyqCInHFk7gTTdsNHbh6/vudwWeEegoed6GwTmuw FL/9FK0CWLb7LntVY2NEvf0GDzbn1FFPdm/hBUgB3YqiEwWmioiYkykr6Oh037ffj0mpjM EgCEVhWzwGS1T9aFRRdnzlGi4A6lYLQ= Received: by mail-pl1-f177.google.com with SMTP id d9443c01a7336-206aee40676so2678175ad.0 for ; Thu, 12 Sep 2024 16:18:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1726183087; x=1726787887; darn=kvack.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=VBBK/J5ZkF8zw3prIEqqwLEmBIMH3Wf5Ldc6S2vu7i8=; b=xa2q09UWh9n2xgCgVxpRxs4J9YhoFCuj/1rHVdr9Pp11G6MWzVUJdD+i6W1jLoHK5i NpEUQj3VPAmIcVxAaBb9b36jQ/bGv0yk8skKLN54Mf7huIKkmY/FiUlEPyj/HPMMENy1 nFKLuo1yMNKY1Se7AODbtP2Cmhwgu0Hd+POIPx3cfNz7hv0k3yp6fxPDzx09S5607dGC 4h2M8AY6rxWtESpbMh4yDAay9NpMJDgH7VsmJHPKqVFApk8aKLOAs7EvZIPrflJK/rZU kIsiKT6748zwc3lqw8DkJrlTViafzfg/2VNPigDrgkos3Is78uhCAa6nacFcB54lupMa mWCA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726183087; x=1726787887; 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=VBBK/J5ZkF8zw3prIEqqwLEmBIMH3Wf5Ldc6S2vu7i8=; b=pR/1GttpW9hqdFVH8UVcYeDPfuWfrL1gIWa4hNyykYAmrxEeR/Hr+JKaJxmaW+3+pf cnsMpzT0de6Qzj/8yAyorr69Bt0GqirWIB+/OwGj+KQ9qOiR1vd6ly9O+0bdzODzKLMW 76wbeYU+dUa0Gq3QcB6tvcOpwsU8MCWhONeRS8lBNod+3pKYk3O0MqKuWOCf3SDr/oZv PB5rZmh2YuB1X8QOk3Ii9HmN21AHoFt0ya58h6Lsb8k35pHwP8XwowFkFx2ky2dhWKDi 6TORB9/b+4f+INZndSxe6W+jf4mjKN0mpMQ8M618wfQAYxwnFoPozKGVlZh7SkM9ru6n 4bWQ== X-Forwarded-Encrypted: i=1; AJvYcCWmBI3uyZlfu/YxXSApCvZoHFgTMPpok6/aH6JqgqqW6sZtj7tcezQ/dQghD1yDSZJN0rFL5oh3LA==@kvack.org X-Gm-Message-State: AOJu0Yxc7Y/1MjxVusxXXQWFYuHV7sT/EmnibNb73GgesPs4tLDFdx7i k7hp2eJrXk9Qs9DofjOufMJ/+yCGTmRfBwts/XgHgN3Su+kippnzjiuvLJzKgZ4= X-Google-Smtp-Source: AGHT+IE7QT3LPeXx/i7CBXip6JfNZehNxjoWUrudSu14L33B4uvstSRbHxig02g+rV2oP6p01q6aLg== X-Received: by 2002:a17:90b:1c88:b0:2d8:9a0c:36c0 with SMTP id 98e67ed59e1d1-2dbb9dc0f39mr1065039a91.8.1726183087183; Thu, 12 Sep 2024 16:18:07 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2db6c1ac69asm3157591a91.0.2024.09.12.16.18.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 12 Sep 2024 16:18:06 -0700 (PDT) From: Deepak Gupta To: paul.walmsley@sifive.com, palmer@sifive.com, conor@kernel.org, linux-doc@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kselftest@vger.kernel.org Cc: corbet@lwn.net, palmer@dabbelt.com, aou@eecs.berkeley.edu, robh@kernel.org, krzk+dt@kernel.org, oleg@redhat.com, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, hpa@zytor.com, peterz@infradead.org, akpm@linux-foundation.org, arnd@arndb.de, ebiederm@xmission.com, kees@kernel.org, Liam.Howlett@oracle.com, vbabka@suse.cz, lorenzo.stoakes@oracle.com, shuah@kernel.org, brauner@kernel.org, samuel.holland@sifive.com, debug@rivosinc.com, andy.chiu@sifive.com, jerry.shih@sifive.com, greentime.hu@sifive.com, charlie@rivosinc.com, evan@rivosinc.com, cleger@rivosinc.com, xiao.w.wang@intel.com, ajones@ventanamicro.com, anup@brainfault.org, mchitale@ventanamicro.com, atishp@rivosinc.com, sameo@rivosinc.com, bjorn@rivosinc.com, alexghiti@rivosinc.com, david@redhat.com, libang.li@antgroup.com, jszhang@kernel.org, leobras@redhat.com, guoren@kernel.org, samitolvanen@google.com, songshuaishuai@tinylab.org, costa.shul@redhat.com, bhe@redhat.com, zong.li@sifive.com, puranjay@kernel.org, namcaov@gmail.com, antonb@tenstorrent.com, sorear@fastmail.com, quic_bjorande@quicinc.com, ancientmodern4@gmail.com, ben.dooks@codethink.co.uk, quic_zhonhan@quicinc.com, cuiyunhui@bytedance.com, yang.lee@linux.alibaba.com, ke.zhao@shingroup.cn, sunilvl@ventanamicro.com, tanzhasanwork@gmail.com, schwab@suse.de, dawei.li@shingroup.cn, rppt@kernel.org, willy@infradead.org, usama.anjum@collabora.com, osalvador@suse.de, ryan.roberts@arm.com, andrii@kernel.org, alx@kernel.org, catalin.marinas@arm.com, broonie@kernel.org, revest@chromium.org, bgray@linux.ibm.com, deller@gmx.de, zev@bewilderbeest.net Subject: [PATCH v4 14/30] riscv mmu: write protect and shadow stack Date: Thu, 12 Sep 2024 16:16:33 -0700 Message-ID: <20240912231650.3740732-15-debug@rivosinc.com> X-Mailer: git-send-email 2.45.0 In-Reply-To: <20240912231650.3740732-1-debug@rivosinc.com> References: <20240912231650.3740732-1-debug@rivosinc.com> MIME-Version: 1.0 X-Rspam-User: X-Stat-Signature: ijgzzfkfm4txr83rpbzqrsm75ninuac7 X-Rspamd-Queue-Id: 7027FC0007 X-Rspamd-Server: rspam11 X-HE-Tag: 1726183088-400108 X-HE-Meta: U2FsdGVkX1/Nz+1PcekeaiakFFGxv+b7mJ6RLWRer7SqHEUWWaspEYFA/KS/fojXo3Q5PJvBrgBCTXzeK2uKh/tPkT9jFDIyDPAOhgRqFNcj7FFQJGITrqb7HO2LSiBYUbP+nGcb8f2LHPzwT8SFYlwIg7lMlnCuD8r4EfvEb3rY5ugXeVOr+0CwazRFEEgFHOLgumzNy9XDS+YayZIKOYtfAv4stLHo4h33lF/KCu/IA/ZO641QuQ7Qabfp8q+gaANE1wUHKUkZ6Y1IHRiYMnV3rjpmD0xaePzJX/HLi2/ENOOcnpnhjRlADqxv9DZytDBVKpAnPBVYKw3wiDi/WPPXfTVFBsqctVnltrhD10oqwTkkEbwWYAI8dBMwI9vX5m/Cw9BQe8AK/Iz2odS9hJ7Qo10sH3fxB1sD6Tew4d+V9BjoQ3AcEvntc1KCHtjtsuz3paJRfMvznbSBPKIUdF0b4KO91q5qGj6JpkXimXZj7nK+ujE1OZoMdwEnYaG9trJ17eWRYIJLIqg0YxRnA8lfSH5X35xy+WhxYABFwidndeS+GUpt04ZsmvC+qIVzjUBGw8BIshe84J4q7h8TDC/PbmbJHcnizBFUyZg+MQ8jsZSiVnvOEIKAwub265RsX+tLiUsJWZfSLq0zjcuORR9M7S1Ghiifgmtoc61xHHCFOZcozI9ERMDPifUbSy/FEOcIdi8Ithkj1bwSJ+sd/coLOqW3dEaZVUsmcwvrmCWFMS693yc0hL/D/Q7ni8Ot+yj7khFm/4NiFZrHzJIlR4c8QNLkpa+Pup5y08it6PYN9zxQhmdiuSXBX+YUDLycTM4iedKuibztA5TOvvmFpW1W+u3EliEMZHTxB1Wm2Ysce48i0B4ImmdNYUjOjtVmRzyAntHCLqpKRsW6vxoXIKR8KhKJYABVL7i79vqpdvDixuuJVM3EVw5xrXaDyZ+aSGPl7lj8a2rfKIrsa1g a8RssUmu AVOJWcUcLFrZTH/0UEzBtwVbtmCQeTJZ6UBEskXypaCAHGLCBtZY/FmtpKSS1qM0XbfZ3jVCLEq75GU9OTJTA89qjV2uNipFSaSeoFh+6AT/qfNyO2sFVHma9lJY42Ay5KCx/0caJ5nHuhcnMjIgdu652e9obO9UeqgNpqjSdsxkq08JVq+7O89W80wK+5um4W2ze+tavuwZ6LM0dvFEFyb1icD64h4JcFbtjlWeImUh9SwsNQRg4f0xoa0I/xuuHI/8mwkSlD22rx8djJlWZZM1NskZRTLHBtMizKdCjnN6J9cGYVSOQZAi2sa8I76OOvwA8/ATfzs91WYntPmQHXbOWHGsZJQfcCBRAHX3VUSXoc1Zm/MHIAY62jdPrtxJYEq7YlN82f32A4QXYEd5Xs6xsG3/TXWgdLVwb8OY7Auxap1w2AwEjaHOXvI+hCI7r0N3G 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: `fork` implements copy on write (COW) by making pages readonly in child and parent both. ptep_set_wrprotect and pte_wrprotect clears _PAGE_WRITE in PTE. Assumption is that page is readable and on fault copy on write happens. To implement COW on shadow stack pages, clearing up W bit makes them XWR = 000. This will result in wrong PTE setting which says no perms but V=1 and PFN field pointing to final page. Instead desired behavior is to turn it into a readable page, take an access (load/store) fault on sspush/sspop (shadow stack) and then perform COW on such pages. This way regular reads would still be allowed and not lead to COW maintaining current behavior of COW on non-shadow stack but writeable memory. On the other hand it doesn't interfere with existing COW for read-write memory. Assumption is always that _PAGE_READ must have been set and thus setting _PAGE_READ is harmless. Signed-off-by: Deepak Gupta Alexandre Ghiti --- arch/riscv/include/asm/pgtable.h | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/arch/riscv/include/asm/pgtable.h b/arch/riscv/include/asm/pgtable.h index 30fd4874e871..3e05fedb871c 100644 --- a/arch/riscv/include/asm/pgtable.h +++ b/arch/riscv/include/asm/pgtable.h @@ -415,7 +415,7 @@ static inline int pte_devmap(pte_t pte) static inline pte_t pte_wrprotect(pte_t pte) { - return __pte(pte_val(pte) & ~(_PAGE_WRITE)); + return __pte((pte_val(pte) & ~(_PAGE_WRITE)) | (_PAGE_READ)); } /* static inline pte_t pte_mkread(pte_t pte) */ @@ -606,7 +606,15 @@ static inline pte_t ptep_get_and_clear(struct mm_struct *mm, static inline void ptep_set_wrprotect(struct mm_struct *mm, unsigned long address, pte_t *ptep) { - atomic_long_and(~(unsigned long)_PAGE_WRITE, (atomic_long_t *)ptep); + pte_t read_pte = READ_ONCE(*ptep); + /* + * ptep_set_wrprotect can be called for shadow stack ranges too. + * shadow stack memory is XWR = 010 and thus clearing _PAGE_WRITE will lead to + * encoding 000b which is wrong encoding with V = 1. This should lead to page fault + * but we dont want this wrong configuration to be set in page tables. + */ + atomic_long_set((atomic_long_t *)ptep, + ((pte_val(read_pte) & ~(unsigned long)_PAGE_WRITE) | _PAGE_READ)); } #define __HAVE_ARCH_PTEP_CLEAR_YOUNG_FLUSH From patchwork Thu Sep 12 23:16:34 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13802838 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 BCA9FEEE271 for ; Thu, 12 Sep 2024 23:18:15 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4B7E56B00AD; Thu, 12 Sep 2024 19:18:15 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 466656B00AE; Thu, 12 Sep 2024 19:18:15 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2BCD66B00AF; Thu, 12 Sep 2024 19:18:15 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 094566B00AD for ; Thu, 12 Sep 2024 19:18:15 -0400 (EDT) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id AF87780DA1 for ; Thu, 12 Sep 2024 23:18:14 +0000 (UTC) X-FDA: 82557651708.09.58A9F5C Received: from mail-pg1-f175.google.com (mail-pg1-f175.google.com [209.85.215.175]) by imf29.hostedemail.com (Postfix) with ESMTP id DAE2C120006 for ; Thu, 12 Sep 2024 23:18:12 +0000 (UTC) Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=V3CSFdMX; spf=pass (imf29.hostedemail.com: domain of debug@rivosinc.com designates 209.85.215.175 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=1726183064; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=rdgy/CGCFKRExYNFF2hZmH3cAGg+NjdcY9UFhxdLc24=; b=NJCCAJijJnyq5pTkdpsT+4m7rt0qDZoObXvmESkWB5fianf6HHpOsVsSinK3rcMr99oAYC I41MeGHns7cIIzBj1rkO5zcnWIoNEaeyMZFIHe2QwE3j7wTF2s+P7Quw4xUaxlumBbueHo kH+Kux1Sd2kCemA3sqdh/pBXucWx3U4= ARC-Authentication-Results: i=1; imf29.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=V3CSFdMX; spf=pass (imf29.hostedemail.com: domain of debug@rivosinc.com designates 209.85.215.175 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1726183064; a=rsa-sha256; cv=none; b=J8HEaYonu7E7+j4LXsRoJJANO/YIx0OrgcNgbatPMQShWUG0uiOX6inJcn9wmMCsK2cVba 1xcSW647oHazJNSDSM4XQVe2Cf7sHTeuFMRHBdKlZUnGYFlhRS4/EqZuFWFMiqhzAq/4S4 AmpICWdQrOyxi7k6ZYek7WECeTR8xhs= Received: by mail-pg1-f175.google.com with SMTP id 41be03b00d2f7-7d666fb3fb9so793995a12.0 for ; Thu, 12 Sep 2024 16:18:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1726183091; x=1726787891; darn=kvack.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=rdgy/CGCFKRExYNFF2hZmH3cAGg+NjdcY9UFhxdLc24=; b=V3CSFdMXa9lYRuVVD9M1GrVFShWd1GhWo+lpw68QGkAephgPhkF+400XxHLjOTqtB2 D7J3T3jopkjDodNtXBAsgCtE3qFxqHomZO91p+fzy0RYLleEbNNFHkIuKqbyGacbisir +U911JKo6fBJrfcmuFwLcfqWz81wd8Tp5Q7rcAv0DzF7CGer6PvIiSySlwyV0cuyAWru n6IbgeqG7oDZQa81qPZ5a86h8ciJQ1XS7jFjv7hRVvREGVKNjFdy1fJplKHzEnkcaUnL ZDBhYaEIjbVnTB8nmeL4n7aF95gPp5MlMy8uEJa2WRH70JEzRvHZezXU9sEljY/2S1Fx hq+w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726183092; x=1726787892; 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=rdgy/CGCFKRExYNFF2hZmH3cAGg+NjdcY9UFhxdLc24=; b=t1K1NDh7FRdSXQfxnhEqBONafquisU00WTzXy8kvF0+LOIbZVuiYFE9BAfbusVpHcS Dm5kwL2kpHMRf3/GPJbuwB9kP7Y7hrrfZoA+NosjNC3864RidfgCm/c4WGuP6cBjr3tX 6elkPp07SwXtANnV5sbwxiimnS53CqRUIXvliTl8nJdQP3tmXwfdhRSmg3k0Hf3ZSbTA 5bLVbMuA5/bN7ZUF83Z5qDUGwSVmX5n0T5GWp6I+3crMc+nK8rjgZ+XzPQMx7U3vpcqF rq10FvuN1fLGObAZQFxoZKdvX3uXyEGRf6jeVAGeLUW3wOpqXEpL78KIWYyhbnRg62gF 71iA== X-Forwarded-Encrypted: i=1; AJvYcCVT0TsFoZJtTeu17ZacYILzFuDAam7GjAL1Ohtt/csVnNJEY+DvIH9c718gipXss0K0FCko+SgPcA==@kvack.org X-Gm-Message-State: AOJu0Yy9x/B+xyNkPYE24pWUc9rzRWTNlL/75s2lygQwUbTXl5d0Q1Be mAH1yLwdGrSe8uC7S+AidqMOPUph8LXqFR3b2zKTcJr6vMZ/HHGj0SfOpAcJ/Jc= X-Google-Smtp-Source: AGHT+IESDHrIQw/Lk5Sw+SaGBVo0Q6tMGJLfnd6+pJqi4Bo9xgZ/IFnG+NBJSuQ3hyayEdxMkUSRHQ== X-Received: by 2002:a17:90b:5244:b0:2d8:b043:9414 with SMTP id 98e67ed59e1d1-2db9fcd4c9amr7048620a91.18.1726183091490; Thu, 12 Sep 2024 16:18:11 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2db6c1ac69asm3157591a91.0.2024.09.12.16.18.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 12 Sep 2024 16:18:11 -0700 (PDT) From: Deepak Gupta To: paul.walmsley@sifive.com, palmer@sifive.com, conor@kernel.org, linux-doc@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kselftest@vger.kernel.org Cc: corbet@lwn.net, palmer@dabbelt.com, aou@eecs.berkeley.edu, robh@kernel.org, krzk+dt@kernel.org, oleg@redhat.com, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, hpa@zytor.com, peterz@infradead.org, akpm@linux-foundation.org, arnd@arndb.de, ebiederm@xmission.com, kees@kernel.org, Liam.Howlett@oracle.com, vbabka@suse.cz, lorenzo.stoakes@oracle.com, shuah@kernel.org, brauner@kernel.org, samuel.holland@sifive.com, debug@rivosinc.com, andy.chiu@sifive.com, jerry.shih@sifive.com, greentime.hu@sifive.com, charlie@rivosinc.com, evan@rivosinc.com, cleger@rivosinc.com, xiao.w.wang@intel.com, ajones@ventanamicro.com, anup@brainfault.org, mchitale@ventanamicro.com, atishp@rivosinc.com, sameo@rivosinc.com, bjorn@rivosinc.com, alexghiti@rivosinc.com, david@redhat.com, libang.li@antgroup.com, jszhang@kernel.org, leobras@redhat.com, guoren@kernel.org, samitolvanen@google.com, songshuaishuai@tinylab.org, costa.shul@redhat.com, bhe@redhat.com, zong.li@sifive.com, puranjay@kernel.org, namcaov@gmail.com, antonb@tenstorrent.com, sorear@fastmail.com, quic_bjorande@quicinc.com, ancientmodern4@gmail.com, ben.dooks@codethink.co.uk, quic_zhonhan@quicinc.com, cuiyunhui@bytedance.com, yang.lee@linux.alibaba.com, ke.zhao@shingroup.cn, sunilvl@ventanamicro.com, tanzhasanwork@gmail.com, schwab@suse.de, dawei.li@shingroup.cn, rppt@kernel.org, willy@infradead.org, usama.anjum@collabora.com, osalvador@suse.de, ryan.roberts@arm.com, andrii@kernel.org, alx@kernel.org, catalin.marinas@arm.com, broonie@kernel.org, revest@chromium.org, bgray@linux.ibm.com, deller@gmx.de, zev@bewilderbeest.net Subject: [PATCH v4 15/30] riscv/mm: Implement map_shadow_stack() syscall Date: Thu, 12 Sep 2024 16:16:34 -0700 Message-ID: <20240912231650.3740732-16-debug@rivosinc.com> X-Mailer: git-send-email 2.45.0 In-Reply-To: <20240912231650.3740732-1-debug@rivosinc.com> References: <20240912231650.3740732-1-debug@rivosinc.com> MIME-Version: 1.0 X-Rspam-User: X-Stat-Signature: hrzy7bhj4hrpqszj9tc4x1a1k48i4mey X-Rspamd-Queue-Id: DAE2C120006 X-Rspamd-Server: rspam11 X-HE-Tag: 1726183092-866183 X-HE-Meta: U2FsdGVkX1/lMK8nkPoC1tQGFn01z5Fb/RF9zK5N3KQUjlOlktCDnblCYLEYF9WSoEPY52mBUaLBxoE9wjiZnq7gdpkINRyG0dbsy4R28gBGHeezV8Pvfki76mqvx4m7IX0J2qHh3gXzb8sPiLnvA1t2nA0DaoPiHNCDxgXEf1Wy0aZMg/R1GvJ4hMjp7CvmWSJNmDeln9OvcIpsUA/LI4Eu+KmhK5QyTZnHYYyhVtOQwumgLhr2tZ4oFYji34+ZKB9q80UoiEKfIdPbWEzB32c2RkQqqUFAmp0gxczSGKaLNTlkzP+8JmlpgWvrFwwcQ6LAhwVrPiEMzikIbi8E49LHaGBUKhIuNqyY8vxbDoTbLirCAoRwYeY8N6f1huX/im5jrqFmYnKjy/3LgRnnI/2JEDjFETkl+dlmRXGEcC73s0JRvFvdoPFuCKXXlsjABi4EvfJTtzj0a4HMbp2GQOGz2KKLuwctS/2Ba46AYJj44fWj4Y/drIijXTOzWWY9k0/W3fR3FEX2YrbN2rWU68rHnzMBYwUpBES1iax7oF2jasGU55KZPkAqQtan3nDZ4R0Ng6bt6e0He+QGJ9ffcZjcFZygmBRJvP7X38gsbCfSr7x0bdnsoQhQblWf1o1wArj91fTEOJYvjlkZEvwaCjkl7o4mlh0UrQTUnbcbmE5OfAlOzaErWcDhRp8EHrCOHCYQJqmTlD80Cvv8Q62lWo6S1dt1PYb1757VMbogcikR/cvChVlrC4HbC0B+UcAYR9zGyxBOl4pwQ799IiDKTWXzjVseqKF+c/zcvVAuF9e5VcKqitihoX6es12dndYOxaBdtBA9E7bpVgMaqItdDnuO7lXs6rH+ba46KlCblpeWkzcJhQxWEu4uKXr6MAJTVRZZcEfr5zt2EdiGK2v+Ed1umLQsMA2ZT1vuR6kaPYWz6XdXQLWM4EqBVRIggz20HYeMCFi82HIx4QJ/g3b S/eUYGTf FkUwk30pyHYV153sdzZFZHwiLvp7N666JYWMWVVaJwm2QkzHAPP+Zt1AWU+Msd5VFnphxC/RzsSqjhAzXF151/UxXdegewZoHtjKSVhGIz07xp2jvSwhmHRhRY1rL2qfgTXtMAEs4RoXK9ELziIKrlsKdxDWF97wNOPj0NcIAk+TFum5WYN5ciRr1RWfou4T6WE0xwqOnB+8ksgKX64135o2Lk2GXyy7VjDPNsUzjWWcIs+Mmj1xN5nR7tSa6r8fqaWYTIsispMLE7ev50FEt0LLOF/stue4Ao3pg9iAgMDB0Mr08aWlYhRBGR9lcz54otPPF4IEkQuLj+bYOkJVfL7K/kY7b0QR9NQKVIF8Tx7W8NphTGJbf3J+cIJTTlh1UUG1TMwO0IzEYMp4TB4JPQxsHo5abdRjtT/o51RXATKpoVUl4OEo6TM2HW9flE9GUpCiws+rDIbqq69xI99Y1re+nR+Cmglqh+2B/ai3l9fnhO/qEkbBk1ouj/Q== 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: As discussed extensively in the changelog for the addition of this syscall on x86 ("x86/shstk: Introduce map_shadow_stack syscall") the existing mmap() and madvise() syscalls do not map entirely well onto the security requirements for shadow stack memory since they lead to windows where memory is allocated but not yet protected or stacks which are not properly and safely initialised. Instead a new syscall map_shadow_stack() has been defined which allocates and initialises a shadow stack page. This patch implements this syscall for riscv. riscv doesn't require token to be setup by kernel because user mode can do that by itself. However to provide compatibility and portability with other architectues, user mode can specify token set flag. Signed-off-by: Deepak Gupta --- arch/riscv/kernel/Makefile | 2 + arch/riscv/kernel/usercfi.c | 145 ++++++++++++++++++++++++++++++++ include/uapi/asm-generic/mman.h | 1 + 3 files changed, 148 insertions(+) create mode 100644 arch/riscv/kernel/usercfi.c diff --git a/arch/riscv/kernel/Makefile b/arch/riscv/kernel/Makefile index 06d407f1b30b..7d673b2f5f3e 100644 --- a/arch/riscv/kernel/Makefile +++ b/arch/riscv/kernel/Makefile @@ -113,3 +113,5 @@ obj-$(CONFIG_COMPAT) += compat_vdso/ obj-$(CONFIG_64BIT) += pi/ obj-$(CONFIG_ACPI) += acpi.o obj-$(CONFIG_ACPI_NUMA) += acpi_numa.o + +obj-$(CONFIG_RISCV_USER_CFI) += usercfi.o diff --git a/arch/riscv/kernel/usercfi.c b/arch/riscv/kernel/usercfi.c new file mode 100644 index 000000000000..ce002eabbdc1 --- /dev/null +++ b/arch/riscv/kernel/usercfi.c @@ -0,0 +1,145 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (C) 2024 Rivos, Inc. + * Deepak Gupta + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define SHSTK_ENTRY_SIZE sizeof(void *) + +/* + * Writes on shadow stack can either be `sspush` or `ssamoswap`. `sspush` can happen + * implicitly on current shadow stack pointed to by CSR_SSP. `ssamoswap` takes pointer to + * shadow stack. To keep it simple, we plan to use `ssamoswap` to perform writes on shadow + * stack. + */ +static noinline unsigned long amo_user_shstk(unsigned long *addr, unsigned long val) +{ + /* + * Never expect -1 on shadow stack. Expect return addresses and zero + */ + unsigned long swap = -1; + + __enable_user_access(); + asm goto( + ".option push\n" + ".option arch, +zicfiss\n" + "1: ssamoswap.d %[swap], %[val], %[addr]\n" + _ASM_EXTABLE(1b, %l[fault]) + RISCV_ACQUIRE_BARRIER + ".option pop\n" + : [swap] "=r" (swap), [addr] "+A" (*addr) + : [val] "r" (val) + : "memory" + : fault + ); + __disable_user_access(); + return swap; +fault: + __disable_user_access(); + return -1; +} + +/* + * Create a restore token on the shadow stack. A token is always XLEN wide + * and aligned to XLEN. + */ +static int create_rstor_token(unsigned long ssp, unsigned long *token_addr) +{ + unsigned long addr; + + /* Token must be aligned */ + if (!IS_ALIGNED(ssp, SHSTK_ENTRY_SIZE)) + return -EINVAL; + + /* On RISC-V we're constructing token to be function of address itself */ + addr = ssp - SHSTK_ENTRY_SIZE; + + if (amo_user_shstk((unsigned long __user *)addr, (unsigned long) ssp) == -1) + return -EFAULT; + + if (token_addr) + *token_addr = addr; + + return 0; +} + +static unsigned long allocate_shadow_stack(unsigned long addr, unsigned long size, + unsigned long token_offset, + bool set_tok) +{ + int flags = MAP_ANONYMOUS | MAP_PRIVATE; + struct mm_struct *mm = current->mm; + unsigned long populate, tok_loc = 0; + + if (addr) + flags |= MAP_FIXED_NOREPLACE; + + mmap_write_lock(mm); + addr = do_mmap(NULL, addr, size, PROT_READ, flags, + VM_SHADOW_STACK | VM_WRITE, 0, &populate, NULL); + mmap_write_unlock(mm); + + if (!set_tok || IS_ERR_VALUE(addr)) + goto out; + + if (create_rstor_token(addr + token_offset, &tok_loc)) { + vm_munmap(addr, size); + return -EINVAL; + } + + addr = tok_loc; + +out: + return addr; +} + +SYSCALL_DEFINE3(map_shadow_stack, unsigned long, addr, unsigned long, size, unsigned int, flags) +{ + bool set_tok = flags & SHADOW_STACK_SET_TOKEN; + unsigned long aligned_size = 0; + + if (!cpu_supports_shadow_stack()) + return -EOPNOTSUPP; + + /* Anything other than set token should result in invalid param */ + if (flags & ~SHADOW_STACK_SET_TOKEN) + return -EINVAL; + + /* + * Unlike other architectures, on RISC-V, SSP pointer is held in CSR_SSP and is available + * CSR in all modes. CSR accesses are performed using 12bit index programmed in instruction + * itself. This provides static property on register programming and writes to CSR can't + * be unintentional from programmer's perspective. As long as programmer has guarded areas + * which perform writes to CSR_SSP properly, shadow stack pivoting is not possible. Since + * CSR_SSP is writeable by user mode, it itself can setup a shadow stack token subsequent + * to allocation. Although in order to provide portablity with other architecture (because + * `map_shadow_stack` is arch agnostic syscall), RISC-V will follow expectation of a token + * flag in flags and if provided in flags, setup a token at the base. + */ + + /* If there isn't space for a token */ + if (set_tok && size < SHSTK_ENTRY_SIZE) + return -ENOSPC; + + if (addr && (addr & (PAGE_SIZE - 1))) + return -EINVAL; + + aligned_size = PAGE_ALIGN(size); + if (aligned_size < size) + return -EOVERFLOW; + + return allocate_shadow_stack(addr, aligned_size, size, set_tok); +} diff --git a/include/uapi/asm-generic/mman.h b/include/uapi/asm-generic/mman.h index 57e8195d0b53..0c0ac6214de6 100644 --- a/include/uapi/asm-generic/mman.h +++ b/include/uapi/asm-generic/mman.h @@ -19,4 +19,5 @@ #define MCL_FUTURE 2 /* lock all future mappings */ #define MCL_ONFAULT 4 /* lock all pages that are faulted in */ +#define SHADOW_STACK_SET_TOKEN (1ULL << 0) /* Set up a restore token in the shadow stack */ #endif /* __ASM_GENERIC_MMAN_H */ From patchwork Thu Sep 12 23:16:35 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13802839 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 3D451EEE270 for ; Thu, 12 Sep 2024 23:18:20 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C0CA16B007B; Thu, 12 Sep 2024 19:18:19 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id BBB256B00AF; Thu, 12 Sep 2024 19:18:19 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9BFA26B00B0; Thu, 12 Sep 2024 19:18:19 -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 7C2EA6B007B for ; Thu, 12 Sep 2024 19:18:19 -0400 (EDT) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 41910160CBB for ; Thu, 12 Sep 2024 23:18:19 +0000 (UTC) X-FDA: 82557651918.20.AB73FFB Received: from mail-pj1-f43.google.com (mail-pj1-f43.google.com [209.85.216.43]) by imf26.hostedemail.com (Postfix) with ESMTP id 5E46A140017 for ; Thu, 12 Sep 2024 23:18:17 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=0NyG50y5; spf=pass (imf26.hostedemail.com: domain of debug@rivosinc.com designates 209.85.216.43 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=1726183068; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=LCtPR9BmJ6DODxbSJXPEMg2KBG0LhzjyQImQVuDyFoI=; b=fVPQEfY7TPgx5XevJJzz/9Hvh+LSg6X4V+thb5i/6+pdJYlolCJIFGGszDbbpGNz+rsvuH RgwGE7exJN6QJpJ1qmDycr/I9KZ1ymegw+VRWl6nI5CoCuoHZVPxXy8yGrvQF+81cv89ba r9bdnJ7ZgBFQjMXwFGo9wr0TcPSdrso= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=0NyG50y5; spf=pass (imf26.hostedemail.com: domain of debug@rivosinc.com designates 209.85.216.43 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1726183068; a=rsa-sha256; cv=none; b=qdAh/VobhsmeCnolZsgcLwQgeceTShgH2mFGdbaZgWdj9Oaa63zVGWmKd18/Mdop2su/7H 1ml6X22C6j4qNbX12JAsAmGY6R/V+j8uHHLXmWHqlXUh/VKI6gXAJCpTv9130fRtgXhqFp 3InrVX027/3diMbQxQKGCj3GdUyYcu0= Received: by mail-pj1-f43.google.com with SMTP id 98e67ed59e1d1-2d87f34a650so1072116a91.1 for ; Thu, 12 Sep 2024 16:18:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1726183096; x=1726787896; darn=kvack.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=LCtPR9BmJ6DODxbSJXPEMg2KBG0LhzjyQImQVuDyFoI=; b=0NyG50y53BEbZAqfMzEnetANFqJwZz+UPEgPIQaZDeRQSBncf9oaoWQlUUeuOsOn70 XF0FnT3E+Zl1vgoDgEyirHQCcKroLzQiqyL6HDJxAaAPdBy6baG237zwU+7NzTN8YNWy mIxJ40h1jCVP654xDgiY9pThXToaZgxwKiH3tipXxWDeqFcCG4hWQY9KuDozQ5al8gl8 lv1TemBfDqrYM8INZLkHENFfT4HhLpbAucHHtolzblZdTDDnO5PnPPcVldbVS7w/sevg U6o7Rgfef5Cb+8UrmzYq5kl5LKodw0xB2MvyRqpCVgIPl4E1EVJmtKpu3j7UuUSFVItn VZJw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726183096; x=1726787896; 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=LCtPR9BmJ6DODxbSJXPEMg2KBG0LhzjyQImQVuDyFoI=; b=NdzI+FBUbSq4KEe55le6qQ/G5Mo6ifTagsdxonxU12k8Dckc5j838yZSngGVgXA/5+ 0xP5RW8bJrnCET8OQ0Vn9iMxxXG6uSjuiMiRxBYSP+DLMmlxPiBhuWACvrwgAPDiEVdf 0PoiwoO8lRcfUozepfOA3uxoCT6w1XcGoDGzbJfCkfZ2AlLfNy1wD2EFP+APACoI60FC RQQYZZLjroJs1dceSNRDdX3JzxsmldqcZJc1eSmnXuP32xgXU5GyVNoCNVCaT0P6ePlW 5P4j8IbHBnJAklpIciSsSnlmEeWG7Xz7UzY6S3DlUP8TpD+jEnfoPuOJAIk7TJxC5R9N xyCA== X-Forwarded-Encrypted: i=1; AJvYcCVx9xH1+VB9DmQdomX2XXfZmW2aAcYt/5xOL4b/zbfPxPErruVTGnxxaAS8BOvtAgt1P97U5W08bA==@kvack.org X-Gm-Message-State: AOJu0YwVQ+cXbKYImVGLOr9+z4ggaGepxhfyMe6MNyVBmFJB//QCE82O dBnC1rnpcUqyLnM4KokLwXiANtiJ8PQStDGEPvbwQgm1qsv56zsLYiTX77NvkKc= X-Google-Smtp-Source: AGHT+IGgverTQv+Xo/0N4fBcX6Yv1Oz/GCSb0qsYtm/a9zkp0fzNx4s9v8cFBlXoYng7BTWKu3muTQ== X-Received: by 2002:a17:90a:c396:b0:2d8:7a63:f9c8 with SMTP id 98e67ed59e1d1-2db67211ecdmr17510608a91.14.1726183095964; Thu, 12 Sep 2024 16:18:15 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2db6c1ac69asm3157591a91.0.2024.09.12.16.18.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 12 Sep 2024 16:18:15 -0700 (PDT) From: Deepak Gupta To: paul.walmsley@sifive.com, palmer@sifive.com, conor@kernel.org, linux-doc@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kselftest@vger.kernel.org Cc: corbet@lwn.net, palmer@dabbelt.com, aou@eecs.berkeley.edu, robh@kernel.org, krzk+dt@kernel.org, oleg@redhat.com, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, hpa@zytor.com, peterz@infradead.org, akpm@linux-foundation.org, arnd@arndb.de, ebiederm@xmission.com, kees@kernel.org, Liam.Howlett@oracle.com, vbabka@suse.cz, lorenzo.stoakes@oracle.com, shuah@kernel.org, brauner@kernel.org, samuel.holland@sifive.com, debug@rivosinc.com, andy.chiu@sifive.com, jerry.shih@sifive.com, greentime.hu@sifive.com, charlie@rivosinc.com, evan@rivosinc.com, cleger@rivosinc.com, xiao.w.wang@intel.com, ajones@ventanamicro.com, anup@brainfault.org, mchitale@ventanamicro.com, atishp@rivosinc.com, sameo@rivosinc.com, bjorn@rivosinc.com, alexghiti@rivosinc.com, david@redhat.com, libang.li@antgroup.com, jszhang@kernel.org, leobras@redhat.com, guoren@kernel.org, samitolvanen@google.com, songshuaishuai@tinylab.org, costa.shul@redhat.com, bhe@redhat.com, zong.li@sifive.com, puranjay@kernel.org, namcaov@gmail.com, antonb@tenstorrent.com, sorear@fastmail.com, quic_bjorande@quicinc.com, ancientmodern4@gmail.com, ben.dooks@codethink.co.uk, quic_zhonhan@quicinc.com, cuiyunhui@bytedance.com, yang.lee@linux.alibaba.com, ke.zhao@shingroup.cn, sunilvl@ventanamicro.com, tanzhasanwork@gmail.com, schwab@suse.de, dawei.li@shingroup.cn, rppt@kernel.org, willy@infradead.org, usama.anjum@collabora.com, osalvador@suse.de, ryan.roberts@arm.com, andrii@kernel.org, alx@kernel.org, catalin.marinas@arm.com, broonie@kernel.org, revest@chromium.org, bgray@linux.ibm.com, deller@gmx.de, zev@bewilderbeest.net Subject: [PATCH v4 16/30] riscv/shstk: If needed allocate a new shadow stack on clone Date: Thu, 12 Sep 2024 16:16:35 -0700 Message-ID: <20240912231650.3740732-17-debug@rivosinc.com> X-Mailer: git-send-email 2.45.0 In-Reply-To: <20240912231650.3740732-1-debug@rivosinc.com> References: <20240912231650.3740732-1-debug@rivosinc.com> MIME-Version: 1.0 X-Rspam-User: X-Stat-Signature: dw7i4f69yx9up3kikzt8bdw3pa9hrubf X-Rspamd-Queue-Id: 5E46A140017 X-Rspamd-Server: rspam11 X-HE-Tag: 1726183097-919917 X-HE-Meta: U2FsdGVkX19rq6mmjsbitbZAfjMFDBHdztW01n5gIge6cPHS9d9mMKFzwQxyXvA33lutUPyh5iJS+LNX1lvbzuJvvVm/OKGQrnkdxDOEPpHOvfv/1zFw9eTL5cHaiCtathpkvvU3PkbzRayYeDLioBwSUQJ+VLvRqIIQ5AXKwXcNyMwXRjuP1mztOGzt0KDmCuCNFWssQqAjBpzgkjyESYj6/SkFS7+8rbpXaNzo206O2FPbUlj/xDtzxwQTfknDMtiovl1cd261oBTEK8VyVcsCXQ206Mym+jdRrj/zV4PMyi34UADnk11GGn5PxFhLjghjiOTrqY0fw4Th+0tf6kkgtMsUk/ScCt6FP5szhsI6VHapHKXQIlNtoMZYu7BLfzfy4U5oNaU9sqWRXPyX0cAFyEvqssCmk8g2ro2TeEbXoul2VfnCI+64cvCjqVKv9/oDPh6AC+8Ac1HsyuYkgMRWuY5AkMH/tYyqjxDWoAJQh3G9+YckY0ZsPQodYjRmh4GQGe4vyjS22RHvlZAD+QGOYVEq4nzvLicgp1u8BwrQDb1DbgCpDuiKAw60weia9zTq5FqrePlnUDXyg57kO5tIMa4lYS6rnS8oVfR3+GJcPx4d4FYY8nrm4bMbKpatePxwJSPQJ/EhfSaCe7axXKJEtjZS6JGHKu1ZQOy4KVXlzM5YAqTVmdR68qKdaJBOt/o55cw8adN19Gg9Sv9Jhx7xqYB5hYKXeZZ6uJHqadjoVH0pJP11dckHbk2bKRymZkcGZHEXn6DB5q8A4X2R7z7sTw4HaUyc+DfhWNNQKoCRICjgIglMxScJWluZBHr6ZTidJcTBhsa2kt/ZldZFvjg3KJGB6DAmlSxt+eSDQygr6mIGMjWcl7aUFyLBHJwSGRt0wTOqme4iAC85Du22c8Wx2thCHZbFZx4ZcukJpO21D/Y4Apxw043lmnZZQkT3oaHpqte2tt9edzdCgls Jl30Mcup yh74H2qkNK/ad3HfXYSIqp3+dcoadTdPf+NGHeJh3NkTt7KxyhU3Dy2Hi0zZQZaHGLYuVHt/8xE+abL7AF8ehacfaQXVL+dlEVs5MOPhuLxx8/3ZzZz9XRUyzgiW6pgMunc4UXQMg/V2wxodZura0HIG9Fky4f5myZQevTFZcsavxJ4IkIjAUEVwnFUxxXWheP9r0um+CiCfijiAmmy8MnD9XmDL045SV9bT97ytgQ+UArPmyS6XVxZIuVQU8FcMAhy/vsf0qrPy4B+hVo2jnP6PvyUDy6mKv4MHZsG17pMjE8FJRZ10VxoPBPHg8zqzJ0FFJs70dl2zwiID1qzQK0FGk810OtLFJuDU/A2CT9Fua/5Jvp3GBRcy7mqZ3S4BmUe1Njot+NVWxoogyBarS4QD9Hqcc6Wdi2PrLnGkqDofS2b2Gur5ecq48e5NO1PRQUNYZclz4xhmbWu0= 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: Userspace specifies CLONE_VM to share address space and spawn new thread. `clone` allow userspace to specify a new stack for new thread. However there is no way to specify new shadow stack base address without changing API. This patch allocates a new shadow stack whenever CLONE_VM is given. In case of CLONE_VFORK, parent is suspended until child finishes and thus can child use parent shadow stack. In case of !CLONE_VM, COW kicks in because entire address space is copied from parent to child. `clone3` is extensible and can provide mechanisms using which shadow stack as an input parameter can be provided. This is not settled yet and being extensively discussed on mailing list. Once that's settled, this commit will adapt to that. Signed-off-by: Deepak Gupta --- arch/riscv/include/asm/usercfi.h | 45 ++++++++++++ arch/riscv/kernel/process.c | 12 ++- arch/riscv/kernel/usercfi.c | 121 +++++++++++++++++++++++++++++++ 3 files changed, 177 insertions(+), 1 deletion(-) diff --git a/arch/riscv/include/asm/usercfi.h b/arch/riscv/include/asm/usercfi.h index 4fa201b4fc4e..a6974307ca6c 100644 --- a/arch/riscv/include/asm/usercfi.h +++ b/arch/riscv/include/asm/usercfi.h @@ -8,6 +8,9 @@ #ifndef __ASSEMBLY__ #include +struct task_struct; +struct kernel_clone_args; + #ifdef CONFIG_RISCV_USER_CFI struct cfi_status { unsigned long ubcfi_en : 1; /* Enable for backward cfi. */ @@ -17,6 +20,48 @@ struct cfi_status { unsigned long shdw_stk_size; /* size of shadow stack */ }; +unsigned long shstk_alloc_thread_stack(struct task_struct *tsk, + const struct kernel_clone_args *args); +void shstk_release(struct task_struct *tsk); +void set_shstk_base(struct task_struct *task, unsigned long shstk_addr, unsigned long size); +unsigned long get_shstk_base(struct task_struct *task, unsigned long *size); +void set_active_shstk(struct task_struct *task, unsigned long shstk_addr); +bool is_shstk_enabled(struct task_struct *task); + +#else + +static inline unsigned long shstk_alloc_thread_stack(struct task_struct *tsk, + const struct kernel_clone_args *args) +{ + return 0; +} + +static inline void shstk_release(struct task_struct *tsk) +{ + +} + +unsigned long get_shstk_base(struct task_struct *task, unsigned long *size) +{ + return 0; +} + +static inline void set_shstk_base(struct task_struct *task, unsigned long shstk_addr, + unsigned long size) +{ + +} + +static inline void set_active_shstk(struct task_struct *task, unsigned long shstk_addr) +{ + +} + +static inline bool is_shstk_enabled(struct task_struct *task) +{ + return false; +} + #endif /* CONFIG_RISCV_USER_CFI */ #endif /* __ASSEMBLY__ */ diff --git a/arch/riscv/kernel/process.c b/arch/riscv/kernel/process.c index 9b66dc07c3d2..6747db72435a 100644 --- a/arch/riscv/kernel/process.c +++ b/arch/riscv/kernel/process.c @@ -26,6 +26,7 @@ #include #include #include +#include #if defined(CONFIG_STACKPROTECTOR) && !defined(CONFIG_STACKPROTECTOR_PER_TASK) #include @@ -194,7 +195,8 @@ int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src) void exit_thread(struct task_struct *tsk) { - + if (IS_ENABLED(CONFIG_RISCV_USER_CFI)) + shstk_release(tsk); } int copy_thread(struct task_struct *p, const struct kernel_clone_args *args) @@ -202,6 +204,7 @@ int copy_thread(struct task_struct *p, const struct kernel_clone_args *args) unsigned long clone_flags = args->flags; unsigned long usp = args->stack; unsigned long tls = args->tls; + unsigned long ssp = 0; struct pt_regs *childregs = task_pt_regs(p); memset(&p->thread.s, 0, sizeof(p->thread.s)); @@ -216,11 +219,18 @@ int copy_thread(struct task_struct *p, const struct kernel_clone_args *args) p->thread.s[0] = (unsigned long)args->fn; p->thread.s[1] = (unsigned long)args->fn_arg; } else { + /* allocate new shadow stack if needed. In case of CLONE_VM we have to */ + ssp = shstk_alloc_thread_stack(p, args); + if (IS_ERR_VALUE(ssp)) + return PTR_ERR((void *)ssp); + *childregs = *(current_pt_regs()); /* Turn off status.VS */ riscv_v_vstate_off(childregs); if (usp) /* User fork */ childregs->sp = usp; + if (ssp) /* if needed, set new ssp */ + set_active_shstk(p, ssp); if (clone_flags & CLONE_SETTLS) childregs->tp = tls; childregs->a0 = 0; /* Return value of fork() */ diff --git a/arch/riscv/kernel/usercfi.c b/arch/riscv/kernel/usercfi.c index ce002eabbdc1..7a7f0b57b2d4 100644 --- a/arch/riscv/kernel/usercfi.c +++ b/arch/riscv/kernel/usercfi.c @@ -19,6 +19,41 @@ #define SHSTK_ENTRY_SIZE sizeof(void *) +bool is_shstk_enabled(struct task_struct *task) +{ + return task->thread_info.user_cfi_state.ubcfi_en ? true : false; +} + +void set_shstk_base(struct task_struct *task, unsigned long shstk_addr, unsigned long size) +{ + task->thread_info.user_cfi_state.shdw_stk_base = shstk_addr; + task->thread_info.user_cfi_state.shdw_stk_size = size; +} + +unsigned long get_shstk_base(struct task_struct *task, unsigned long *size) +{ + if (size) + *size = task->thread_info.user_cfi_state.shdw_stk_size; + return task->thread_info.user_cfi_state.shdw_stk_base; +} + +void set_active_shstk(struct task_struct *task, unsigned long shstk_addr) +{ + task->thread_info.user_cfi_state.user_shdw_stk = shstk_addr; +} + +/* + * If size is 0, then to be compatible with regular stack we want it to be as big as + * regular stack. Else PAGE_ALIGN it and return back + */ +static unsigned long calc_shstk_size(unsigned long size) +{ + if (size) + return PAGE_ALIGN(size); + + return PAGE_ALIGN(min_t(unsigned long long, rlimit(RLIMIT_STACK), SZ_4G)); +} + /* * Writes on shadow stack can either be `sspush` or `ssamoswap`. `sspush` can happen * implicitly on current shadow stack pointed to by CSR_SSP. `ssamoswap` takes pointer to @@ -143,3 +178,89 @@ SYSCALL_DEFINE3(map_shadow_stack, unsigned long, addr, unsigned long, size, unsi return allocate_shadow_stack(addr, aligned_size, size, set_tok); } + +/* + * This gets called during clone/clone3/fork. And is needed to allocate a shadow stack for + * cases where CLONE_VM is specified and thus a different stack is specified by user. We + * thus need a separate shadow stack too. How does separate shadow stack is specified by + * user is still being debated. Once that's settled, remove this part of the comment. + * This function simply returns 0 if shadow stack are not supported or if separate shadow + * stack allocation is not needed (like in case of !CLONE_VM) + */ +unsigned long shstk_alloc_thread_stack(struct task_struct *tsk, + const struct kernel_clone_args *args) +{ + unsigned long addr, size; + + /* If shadow stack is not supported, return 0 */ + if (!cpu_supports_shadow_stack()) + return 0; + + /* + * If shadow stack is not enabled on the new thread, skip any + * switch to a new shadow stack. + */ + if (is_shstk_enabled(tsk)) + return 0; + + /* + * For CLONE_VFORK the child will share the parents shadow stack. + * Set base = 0 and size = 0, this is special means to track this state + * so the freeing logic run for child knows to leave it alone. + */ + if (args->flags & CLONE_VFORK) { + set_shstk_base(tsk, 0, 0); + return 0; + } + + /* + * For !CLONE_VM the child will use a copy of the parents shadow + * stack. + */ + if (!(args->flags & CLONE_VM)) + return 0; + + /* + * reaching here means, CLONE_VM was specified and thus a separate shadow + * stack is needed for new cloned thread. Note: below allocation is happening + * using current mm. + */ + size = calc_shstk_size(args->stack_size); + addr = allocate_shadow_stack(0, size, 0, false); + if (IS_ERR_VALUE(addr)) + return addr; + + set_shstk_base(tsk, addr, size); + + return addr + size; +} + +void shstk_release(struct task_struct *tsk) +{ + unsigned long base = 0, size = 0; + /* If shadow stack is not supported or not enabled, nothing to release */ + if (!cpu_supports_shadow_stack() || + !is_shstk_enabled(tsk)) + return; + + /* + * When fork() with CLONE_VM fails, the child (tsk) already has a + * shadow stack allocated, and exit_thread() calls this function to + * free it. In this case the parent (current) and the child share + * the same mm struct. Move forward only when they're same. + */ + if (!tsk->mm || tsk->mm != current->mm) + return; + + /* + * We know shadow stack is enabled but if base is NULL, then + * this task is not managing its own shadow stack (CLONE_VFORK). So + * skip freeing it. + */ + base = get_shstk_base(tsk, &size); + if (!base) + return; + + vm_munmap(base, size); + set_shstk_base(tsk, 0, 0); +} From patchwork Thu Sep 12 23:16:36 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13802840 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 BB361EEE271 for ; Thu, 12 Sep 2024 23:18:24 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2BEF26B00B1; Thu, 12 Sep 2024 19:18:24 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 26CB76B00B2; Thu, 12 Sep 2024 19:18:24 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0BDB96B00B3; Thu, 12 Sep 2024 19:18:24 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id E31D66B00B1 for ; Thu, 12 Sep 2024 19:18:23 -0400 (EDT) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 8C5604048B for ; Thu, 12 Sep 2024 23:18:23 +0000 (UTC) X-FDA: 82557652086.07.61B2C7D Received: from mail-pj1-f42.google.com (mail-pj1-f42.google.com [209.85.216.42]) by imf19.hostedemail.com (Postfix) with ESMTP id ABC561A0015 for ; Thu, 12 Sep 2024 23:18:21 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b="YTiC/OYh"; dmarc=none; spf=pass (imf19.hostedemail.com: domain of debug@rivosinc.com designates 209.85.216.42 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1726183073; a=rsa-sha256; cv=none; b=Ij3csc+G/qBJG59+Ozcct2/UwTijxxIbZTd2AfUorYOcEdjPpsUO8YmKI7+RuKUFXoioYR 4BoDaF3TuQ78DiznR452wVcFrjUuarEh0GAEatAfs+p+VBwA3HhUhfA8LD5nveZ1ed4/iQ FLKN0v92eUasRQSjWxTPOgmd6jSY7TA= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b="YTiC/OYh"; dmarc=none; spf=pass (imf19.hostedemail.com: domain of debug@rivosinc.com designates 209.85.216.42 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1726183073; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=eQcGpVU0IdnPD1mHTUz69E9YXFdr868sAk68PrVb1pE=; b=tOHEnE+mJWF7exe84uDhs7e4SoAcUscCYb/27+GvyApCR3NkuGL6dtN3otZ8Lly2uqrT17 s30fvUzOoRj0GUURG227KE+YcpXAPY5H9i2lyO0ZTn91C2DoAPezbwTQIF6QW8OsnCd3Gq 5arIu3RiiMbEA72YkvKD7qufYWFTNO0= Received: by mail-pj1-f42.google.com with SMTP id 98e67ed59e1d1-2d8818337a5so1945002a91.1 for ; Thu, 12 Sep 2024 16:18:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1726183100; x=1726787900; darn=kvack.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=eQcGpVU0IdnPD1mHTUz69E9YXFdr868sAk68PrVb1pE=; b=YTiC/OYhMkYJ7DsxoAPWsprbwMUNC3bu+04T5Xqzf5LWf+yVwyyHOjqqHa0goSZnDx TFgzmxteDqd4bErDO6N15gl4uXqIS2S8NKQOevSNZEhs/KfUL1+SLJ1D1Q0ljpKhEG67 sxK4LEISWfagFcW30QeW7dgNpFmoY6LHNfYGthwvN1hR7u4bWRHAGTahLA/UL9ibAbbb 2kwlPcb4EqTppfRtVFMSKiTnvwtVm4o2JHMh6HnwZd/U/D9DeE1ol0anRiaYKLY5Y90o 0+sHvHQ4+cVM+oA1ZaGzVcop4oOryNVzGwkNdnE0doRrBxT7YZfXt3D4TWEV5Wq5Ztmu /mzg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726183100; x=1726787900; 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=eQcGpVU0IdnPD1mHTUz69E9YXFdr868sAk68PrVb1pE=; b=fZ1CHycNFX8srj5kxe/XdbAqNOU3Lcb6sb6uJaX2DJOwjr5IonjJ4XIjA3oZm93VB3 B3D5GydyFfvqm/v43NitbZMWcicN69rFuLKVQ2KsjWMwvhNcIUcAPxBlrBqPNdJqa1+q VzAkj0EGp8pAb6FVVDgxLJ8Z0YvB9xH1iFPqIjbbWqJKxON5XJSH3t685W9a+KOqvex6 D5PXEh4zfUOy+goMPhZLOajb4Qb2fDWdeNraevWu3qk6IXtcnKs+j/OgLyWoit5/++tq EasFRG/2DDb/GIkmYONRvIeS9iYaLU4V5GoYATo88G3oErwU4Sz+PTg2zIZqaJ69e4os K0eA== X-Forwarded-Encrypted: i=1; AJvYcCVNNb2dZCY2yNpOKZwP2LMBhYNdFlERD7zASXoCql0ySZuy59Xw3Jx/YONBAbkJgQSW3jPqNsgBZg==@kvack.org X-Gm-Message-State: AOJu0YyCgSSm2RwfO1mzGGZ+x6ACKWlrvptdzKLooZ5W/YrI7TBuy2ch ORU3AXM8bXI7n2tHk8L8UbeB5YqBdn9K2UYkvjeN7ZgoUozuw+eeWLu3PFqZPTA= X-Google-Smtp-Source: AGHT+IFff016Dc/UvJnFaAb1uH5IeOSLPpio1EAHCPI04vBCz6X3EEqRQxY7cO8q2vilVk8FL6IgYg== X-Received: by 2002:a17:90a:2c0f:b0:2d8:25ce:e6e3 with SMTP id 98e67ed59e1d1-2db6718d8b4mr19220198a91.5.1726183100307; Thu, 12 Sep 2024 16:18:20 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2db6c1ac69asm3157591a91.0.2024.09.12.16.18.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 12 Sep 2024 16:18:19 -0700 (PDT) From: Deepak Gupta To: paul.walmsley@sifive.com, palmer@sifive.com, conor@kernel.org, linux-doc@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kselftest@vger.kernel.org Cc: corbet@lwn.net, palmer@dabbelt.com, aou@eecs.berkeley.edu, robh@kernel.org, krzk+dt@kernel.org, oleg@redhat.com, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, hpa@zytor.com, peterz@infradead.org, akpm@linux-foundation.org, arnd@arndb.de, ebiederm@xmission.com, kees@kernel.org, Liam.Howlett@oracle.com, vbabka@suse.cz, lorenzo.stoakes@oracle.com, shuah@kernel.org, brauner@kernel.org, samuel.holland@sifive.com, debug@rivosinc.com, andy.chiu@sifive.com, jerry.shih@sifive.com, greentime.hu@sifive.com, charlie@rivosinc.com, evan@rivosinc.com, cleger@rivosinc.com, xiao.w.wang@intel.com, ajones@ventanamicro.com, anup@brainfault.org, mchitale@ventanamicro.com, atishp@rivosinc.com, sameo@rivosinc.com, bjorn@rivosinc.com, alexghiti@rivosinc.com, david@redhat.com, libang.li@antgroup.com, jszhang@kernel.org, leobras@redhat.com, guoren@kernel.org, samitolvanen@google.com, songshuaishuai@tinylab.org, costa.shul@redhat.com, bhe@redhat.com, zong.li@sifive.com, puranjay@kernel.org, namcaov@gmail.com, antonb@tenstorrent.com, sorear@fastmail.com, quic_bjorande@quicinc.com, ancientmodern4@gmail.com, ben.dooks@codethink.co.uk, quic_zhonhan@quicinc.com, cuiyunhui@bytedance.com, yang.lee@linux.alibaba.com, ke.zhao@shingroup.cn, sunilvl@ventanamicro.com, tanzhasanwork@gmail.com, schwab@suse.de, dawei.li@shingroup.cn, rppt@kernel.org, willy@infradead.org, usama.anjum@collabora.com, osalvador@suse.de, ryan.roberts@arm.com, andrii@kernel.org, alx@kernel.org, catalin.marinas@arm.com, broonie@kernel.org, revest@chromium.org, bgray@linux.ibm.com, deller@gmx.de, zev@bewilderbeest.net Subject: [PATCH v4 17/30] prctl: arch-agnostic prctl for shadow stack Date: Thu, 12 Sep 2024 16:16:36 -0700 Message-ID: <20240912231650.3740732-18-debug@rivosinc.com> X-Mailer: git-send-email 2.45.0 In-Reply-To: <20240912231650.3740732-1-debug@rivosinc.com> References: <20240912231650.3740732-1-debug@rivosinc.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Queue-Id: ABC561A0015 X-Rspamd-Server: rspam01 X-Stat-Signature: 5ccesrxb8gsjmrrdczhcn4n18fm7om6y X-HE-Tag: 1726183101-637016 X-HE-Meta: U2FsdGVkX1+rqNavJgxE99a/HF79cy3FNno+tCKR64f4gpIe3pz15loJoaH6Q8YHTjf04Pb9t0GDREwKvo1xLEbk8bqXq2vpSTCuf2xz5HBGGcLCERjf+97FyEqlbgBD6ErVf3Lmh09cPTKwXRdhI/eoaErTMm0d3Ogu/ur3XyTYvfaSQkyU1qJU4x8oSmqeGmCKGQiMiFoCuIBtvEvdpx9FB4Zjqx9YGEX9imgsrbo0kWxlDdf2bmqghCBC1dt8l0QbBXfo9ANLh33LRt4EGIRQB6/F1rUZdGbLDi52aMAyyr9FgcGWT0PRAF55eejD1oqpB8GRU/MpFN7W8G7OpFJlnHB82tVFMqQrwr8PwFsS9tRqLUrDLki8jwDC5bgAlybvKHwXPE7RZX1piJjvi8zvHImJx7FSPzW+ZYaEWRPhKbidRoflcigiz9/xneEGBbC7gUq/ODOvpACBrEB9TgXyUIMIUMd9SMmmgJ/Ztf53XffhO6qhgNr8yzqXgtV54YRpmiyS/Fn90pu+pww8p+v5J/Mvy0BhXVqcJeX3Jy+x7HUxT6dAaHUz6aVzCuln4AdDbMcbNgjEtkmIHeyT9lcBaaB0JfPLcu3ssteLCe6aBscP/OPmIph7cpW8WfY+oX4oh8VulwN/RcjRqhY9k6P2AlJN1gYW7bNbImtBs9R35jRSIAB9bxCIs5CCFXgbNGgtWbVpdqDFGrm3UWNhgtOKRiwJKuyaGgWvzw234/JGfoGTnsvjPVqqz1p6080/hFoSsSC2VwDngx+jI5Y3QSco03xIcm7u/9R6FUyL/md+XmAojDkVTwXaQyveKVu49OwwqiaJOHvvhb8sFzFsDERilJhhs3IHGp1XB9Ixon/goMTuK5UuAgdIiRSkci2sqXf/Ol82Gb99CdR67PlZ/cfQmw/ndz/T9+urAJJ/w4qlx871gGBucwgrP0gsYAtAySBEoKy9kCDbnpdSPBw auINqy1x XMbmuqX4F1CagO+ZhPTn0dVVBmyE0psgm4ILvv5HSh9s+7SSBp1iOf8ZC6dzObpjosESezthzHpLJHRUpqmamdWC9fQzas1cuyc4OUpQobUJsCrKn0Aqpwe0dRJwNucYrTeYt40xdKDAW/9qdJA21f0SSLWXGaWJqftTZeUSle0hqINRIZbmg8jTeh+ZGocUXOURk/VIuIsrrjZvrIvEPjYdIkeQZSURXAlF//49l7+vRiNQGu9iHtHGy+rFlDVMOrtiDvpRenNiKA5t3QAIA2m7GGVEwOZ+sOaZ/A+/e6ZE4IDpPi7oNHde9De3s0xODO0G5DDd26SuNKPICeRxzq64PzIzXQcf6jcBWzgCRWqUnvn8Df0NbjofrrvsNu/mO6uYLejYe4bLUJ+LtkMWxp1nMeJoZ9GpA32JV9x+XIMjg75UK9slKI63TNwf3DqifrAZ9hkLTkVR+2YNsphMYPmgqCZaH1bFUZfz6 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: Mark Brown Three architectures (x86, aarch64, riscv) have announced support for shadow stacks with fairly similar functionality. While x86 is using arch_prctl() to control the functionality neither arm64 nor riscv uses that interface so this patch adds arch-agnostic prctl() support to get and set status of shadow stacks and lock the current configuration to prevent further changes, with support for turning on and off individual subfeatures so applications can limit their exposure to features that they do not need. The features are: - PR_SHADOW_STACK_ENABLE: Tracking and enforcement of shadow stacks, including allocation of a shadow stack if one is not already allocated. - PR_SHADOW_STACK_WRITE: Writes to specific addresses in the shadow stack. - PR_SHADOW_STACK_PUSH: Push additional values onto the shadow stack. - PR_SHADOW_STACK_DISABLE: Allow to disable shadow stack. Note once locked, disable must fail. These features are expected to be inherited by new threads and cleared on exec(), unknown features should be rejected for enable but accepted for locking (in order to allow for future proofing). This is based on a patch originally written by Deepak Gupta but later modified by Mark Brown for arm's GCS patch series. Signed-off-by: Mark Brown Co-developed-by: Deepak Gupta Signed-off-by: Deepak Gupta --- include/linux/mm.h | 3 +++ include/uapi/linux/prctl.h | 21 +++++++++++++++++++++ kernel/sys.c | 30 ++++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+) diff --git a/include/linux/mm.h b/include/linux/mm.h index f0dc94fb782a..e3acb4297877 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -4215,6 +4215,9 @@ static inline bool pfn_is_unaccepted_memory(unsigned long pfn) return range_contains_unaccepted_memory(paddr, paddr + PAGE_SIZE); } +int arch_get_shadow_stack_status(struct task_struct *t, unsigned long __user *status); +int arch_set_shadow_stack_status(struct task_struct *t, unsigned long status); +int arch_lock_shadow_stack_status(struct task_struct *t, unsigned long status); void vma_pgtable_walk_begin(struct vm_area_struct *vma); void vma_pgtable_walk_end(struct vm_area_struct *vma); diff --git a/include/uapi/linux/prctl.h b/include/uapi/linux/prctl.h index 35791791a879..b8d7b6361754 100644 --- a/include/uapi/linux/prctl.h +++ b/include/uapi/linux/prctl.h @@ -327,5 +327,26 @@ struct prctl_mm_map { # define PR_PPC_DEXCR_CTRL_SET_ONEXEC 0x8 /* Set the aspect on exec */ # define PR_PPC_DEXCR_CTRL_CLEAR_ONEXEC 0x10 /* Clear the aspect on exec */ # define PR_PPC_DEXCR_CTRL_MASK 0x1f +/* + * Get the current shadow stack configuration for the current thread, + * this will be the value configured via PR_SET_SHADOW_STACK_STATUS. + */ +#define PR_GET_SHADOW_STACK_STATUS 74 + +/* + * Set the current shadow stack configuration. Enabling the shadow + * stack will cause a shadow stack to be allocated for the thread. + */ +#define PR_SET_SHADOW_STACK_STATUS 75 +# define PR_SHADOW_STACK_ENABLE (1UL << 0) +# define PR_SHADOW_STACK_WRITE (1UL << 1) +# define PR_SHADOW_STACK_PUSH (1UL << 2) + +/* + * Prevent further changes to the specified shadow stack + * configuration. All bits may be locked via this call, including + * undefined bits. + */ +#define PR_LOCK_SHADOW_STACK_STATUS 76 #endif /* _LINUX_PRCTL_H */ diff --git a/kernel/sys.c b/kernel/sys.c index 3a2df1bd9f64..7e0c10e867cf 100644 --- a/kernel/sys.c +++ b/kernel/sys.c @@ -2324,6 +2324,21 @@ int __weak arch_prctl_spec_ctrl_set(struct task_struct *t, unsigned long which, return -EINVAL; } +int __weak arch_get_shadow_stack_status(struct task_struct *t, unsigned long __user *status) +{ + return -EINVAL; +} + +int __weak arch_set_shadow_stack_status(struct task_struct *t, unsigned long status) +{ + return -EINVAL; +} + +int __weak arch_lock_shadow_stack_status(struct task_struct *t, unsigned long status) +{ + return -EINVAL; +} + #define PR_IO_FLUSHER (PF_MEMALLOC_NOIO | PF_LOCAL_THROTTLE) #ifdef CONFIG_ANON_VMA_NAME @@ -2782,6 +2797,21 @@ SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3, case PR_RISCV_SET_ICACHE_FLUSH_CTX: error = RISCV_SET_ICACHE_FLUSH_CTX(arg2, arg3); break; + case PR_GET_SHADOW_STACK_STATUS: + if (arg3 || arg4 || arg5) + return -EINVAL; + error = arch_get_shadow_stack_status(me, (unsigned long __user *) arg2); + break; + case PR_SET_SHADOW_STACK_STATUS: + if (arg3 || arg4 || arg5) + return -EINVAL; + error = arch_set_shadow_stack_status(me, arg2); + break; + case PR_LOCK_SHADOW_STACK_STATUS: + if (arg3 || arg4 || arg5) + return -EINVAL; + error = arch_lock_shadow_stack_status(me, arg2); + break; default: error = -EINVAL; break; From patchwork Thu Sep 12 23:16:37 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13802841 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 31CB1EEE270 for ; Thu, 12 Sep 2024 23:18:29 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B22A06B00B2; Thu, 12 Sep 2024 19:18:28 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id AD1416B00B4; Thu, 12 Sep 2024 19:18:28 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 924816B00B5; Thu, 12 Sep 2024 19:18:28 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 72AF26B00B2 for ; Thu, 12 Sep 2024 19:18:28 -0400 (EDT) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id EF7911A0CD1 for ; Thu, 12 Sep 2024 23:18:27 +0000 (UTC) X-FDA: 82557652254.04.317ED5D Received: from mail-pj1-f43.google.com (mail-pj1-f43.google.com [209.85.216.43]) by imf17.hostedemail.com (Postfix) with ESMTP id 1F33D40005 for ; Thu, 12 Sep 2024 23:18:25 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=GmURDLO5; dmarc=none; spf=pass (imf17.hostedemail.com: domain of debug@rivosinc.com designates 209.85.216.43 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1726183077; a=rsa-sha256; cv=none; b=HurBN2pfYRIf5RbiOGn+HN4Q0nBceIrw+CrwLJRgSlAxqYx3ELEIbM19yjbeiTn/ocBbmg ks764eW+5KnvWEkQQJQ+4nzOOlflQnhwGmSFimVIDt1C+1ga6YOu4LADr+QxW/KorPpgIH HoAe8UTD6DeJGSM3e8Pr05vLi/m1rGI= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=GmURDLO5; dmarc=none; spf=pass (imf17.hostedemail.com: domain of debug@rivosinc.com designates 209.85.216.43 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1726183077; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=Tr63COK+U+RGXsHvnwpQo9tUUF56AGnmex8wAUb47lI=; b=E6hhwuDljGtf9JL7UvJsjJe0WVfFSCHLtwBs6u/OS5w6VIoGvhZ9zHbsirpT08F5LbaDbk 6C+9jTNkGIAgy6I2YHOSsK+PsB2uDj/OPAN+6JyCQmE4ClC7oCybz/yyAvTO8RBQyKG2WI 0eZJl0EMGmeBOINNXqkvx7bb/o+TSb8= Received: by mail-pj1-f43.google.com with SMTP id 98e67ed59e1d1-2d87176316eso1958177a91.0 for ; Thu, 12 Sep 2024 16:18:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1726183105; x=1726787905; darn=kvack.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=Tr63COK+U+RGXsHvnwpQo9tUUF56AGnmex8wAUb47lI=; b=GmURDLO5q4m7uKVIiMHCmnuoYAbWNrGx4gXjlzpcjIa5RCQ+FL8JFBO5aZK+XI6M2L gtIgkyfd/EeKKwxS39D3siTXOn5eh+l8Yi/neqtvMnWjnAksJFThy0uFo5HOIV0vmTPw 21m7wDZEDdqEMFcpoNxcEpSNi9DWR2Yq1wAda4eraidxXeLfZ7q4zoW+DeJbrZT6mRA4 AYH3VGArve3po8lt4KuHuGb8cPiWcnFBX2I1ngHctix0CW0bSEzFYVXe64qF9Eq2Qsdm 94dNvyVk/88PvqmNq+RMZhRES3x7DGsWn/BEJwdaCBTSJIhZwppnnivkUYldXjuzn34p VEEg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726183105; x=1726787905; 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=Tr63COK+U+RGXsHvnwpQo9tUUF56AGnmex8wAUb47lI=; b=UnzGAafy2T+jb6nkvEIz2pylarNjeyBP6XFrtCz8YNLVJDrXh/OULAaU1RFT0paWN2 RxMsZpNjCsRtAsTlKp8+lGT09zv9e9rODcOWmgARoJtsm7GN10n+FgmFBXObTDn0FofZ 4gTZW3SpT/0n/Y45sytDDidfV4Zza/7gSYFPgztMH18aeyAg//eC77tcMwcQHqQvNF40 gk+xHW58g2n5u6jTwbldUQLBd/TVOspIErLp3JB96FHV6tSevq/4qKPF6X6VNKRGj3cJ rJNILArzPgl4Ux53dvoK4pGFQD1FMNJZXOFPbXg018653dRVD2SxI/ImVagJzdUECtrB 3Uzg== X-Forwarded-Encrypted: i=1; AJvYcCVQ9R2qzbrtXdA8GnmtcT7vEDM7CjkwnGzMBJ/WEsL8wyzOQd/p1VrOdbLZo0J8lmVKRxuaIfru5Q==@kvack.org X-Gm-Message-State: AOJu0YxBFkaVocGsYihQq+X4ladQWO4AyN4UOG7H8S8Z/GtU14r8Nm01 IOTzfPxelSXms7e2ptQybRRfHhJQK527Wn82ZpbQik4L8tCceit2LO0hXK9aKbE= X-Google-Smtp-Source: AGHT+IEDlbanVmMoBHXIDtdzRNuiWJ0Gl4K78KpM2NYaBdsrPCDFJdZTucI/ImbEr5mpj8z5f/pv1g== X-Received: by 2002:a17:90a:62ca:b0:2d8:9fbe:6727 with SMTP id 98e67ed59e1d1-2db9fc1adabmr6645974a91.4.1726183104748; Thu, 12 Sep 2024 16:18:24 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2db6c1ac69asm3157591a91.0.2024.09.12.16.18.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 12 Sep 2024 16:18:24 -0700 (PDT) From: Deepak Gupta To: paul.walmsley@sifive.com, palmer@sifive.com, conor@kernel.org, linux-doc@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kselftest@vger.kernel.org Cc: corbet@lwn.net, palmer@dabbelt.com, aou@eecs.berkeley.edu, robh@kernel.org, krzk+dt@kernel.org, oleg@redhat.com, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, hpa@zytor.com, peterz@infradead.org, akpm@linux-foundation.org, arnd@arndb.de, ebiederm@xmission.com, kees@kernel.org, Liam.Howlett@oracle.com, vbabka@suse.cz, lorenzo.stoakes@oracle.com, shuah@kernel.org, brauner@kernel.org, samuel.holland@sifive.com, debug@rivosinc.com, andy.chiu@sifive.com, jerry.shih@sifive.com, greentime.hu@sifive.com, charlie@rivosinc.com, evan@rivosinc.com, cleger@rivosinc.com, xiao.w.wang@intel.com, ajones@ventanamicro.com, anup@brainfault.org, mchitale@ventanamicro.com, atishp@rivosinc.com, sameo@rivosinc.com, bjorn@rivosinc.com, alexghiti@rivosinc.com, david@redhat.com, libang.li@antgroup.com, jszhang@kernel.org, leobras@redhat.com, guoren@kernel.org, samitolvanen@google.com, songshuaishuai@tinylab.org, costa.shul@redhat.com, bhe@redhat.com, zong.li@sifive.com, puranjay@kernel.org, namcaov@gmail.com, antonb@tenstorrent.com, sorear@fastmail.com, quic_bjorande@quicinc.com, ancientmodern4@gmail.com, ben.dooks@codethink.co.uk, quic_zhonhan@quicinc.com, cuiyunhui@bytedance.com, yang.lee@linux.alibaba.com, ke.zhao@shingroup.cn, sunilvl@ventanamicro.com, tanzhasanwork@gmail.com, schwab@suse.de, dawei.li@shingroup.cn, rppt@kernel.org, willy@infradead.org, usama.anjum@collabora.com, osalvador@suse.de, ryan.roberts@arm.com, andrii@kernel.org, alx@kernel.org, catalin.marinas@arm.com, broonie@kernel.org, revest@chromium.org, bgray@linux.ibm.com, deller@gmx.de, zev@bewilderbeest.net Subject: [PATCH v4 18/30] prctl: arch-agnostic prctl for indirect branch tracking Date: Thu, 12 Sep 2024 16:16:37 -0700 Message-ID: <20240912231650.3740732-19-debug@rivosinc.com> X-Mailer: git-send-email 2.45.0 In-Reply-To: <20240912231650.3740732-1-debug@rivosinc.com> References: <20240912231650.3740732-1-debug@rivosinc.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Queue-Id: 1F33D40005 X-Rspamd-Server: rspam01 X-Stat-Signature: 45jrzh7tbm69gixhacw7pfepkaumry63 X-HE-Tag: 1726183105-182856 X-HE-Meta: U2FsdGVkX1+Dcr183h6MoYU5ieV37L8CmncqCjZR/UBcI5RcPsTYLQl1lrCfX8yns+WzCzIVV0isFW9irJ7v4ILhm2NjmmOUfTZB8FHNYucB96DsU9NoJChGhyeAHbWMgVQL70SUq6w0sM7FIhYzE8vg7RNUgTgL50CpLTL06GELAYjfKSKXOK3FFOON9njKb8rl9RjExYAgw7E8E4lXcarLFS7RI09JkgWNYZk3h83sI372rTtulAeBRr948vt9rnPt/7k7MM6hM+jBB5r7iB6RrNB4T03GTih6IUzs6NuqK3mU14hae8H6qyPcF7NJ5yjDqJunZ2oQGIav2N7NTgVVqwUuugwqHfLVTZur7PDQqva+FeXOzbcNXkSZpm8vU2hpVog4I1Egl/YzfMxNkmO1I1fx52rfb8mMqB0EGHvYwTNCv8M9slsbfcg5m0xLpWMSmkyp6thmC6RAa60DgUODc9BQpZqxIlePX2vJJItId4UnaFXAzDG/3VQoL5rM3JjEnYtQP7SfwPGm85wVJ6qIjY55FwRP3gz4YfzdN2aLMBuaxveeP8vV+CTwDQBVr9Bs71FYywvCnKocM77fSy9tFntNtlE95G7CTr2L1KrSaaGjsBBy9aUjQFtTNVJM5sSan1cQkgjjnjRE6cxvTLIcAYF/KMeO80GBnBXIIhAF4VU0ulhFSqVzENtacZ6F70Sy+Fj7KPN/xSfPBhmJCN2E3wwTb6TBQdVl3fQ9PCyYOA53RYh8KyflB/PckkzrSYV7Fq0oq+y2JbdCTt/iSZlxTdyhX5kynFNK9iNCzA9TK8wlcyONzkLo68bqIHxa6kk5kFUEEGv4FCYIuZ9rpgyafVwdax02OWTNwZgPG3U0hOAa8VRwLNGVP1JvZtylRuIBe50yG9e6JFdgBRyNMFZin2ZmTXpDKyxuDJu0WqhKUHIbWq8bPOTBD0xtMygwxQCW56CR//2wBwMgfnG g7W9pfAD sd3FhxOLRYPFf1TWxH8cY48ayysKvUJUBT6oMVcRUYpubTVV5HUbCjFTZdEua9VONgBwZ9jMF47Xt5Qw2WKinGoPwbdJZD1VITtl/BeKyzWoHKJRrOsNEdyabOaYYCUGg0R2VEd1MyCHPiri4rZ644T0VAS3nPX/zpjwemiOoCYnuzWw96lNqJ3pLmMCgWrUBlDwMR43YJUauCZt3Kg09c9a9NI2rrAUAlQnoBnvoAOHckQWyo/NAckiOufEnPJo0O1bRoFmRSOtPCRPvTNTz/4RVAb9A6/OfP4JJbIYQya9dn9JYvpr6AGLDPXsNiFY1/eXeedrZy2NajYnloOkZL9RTeapqmqI7otBJD4MWYde4e9MnNyFEWYT3hECKr7QRJ4pFMuD/SesnTYDtQaHZ6HqKwal2ftsZYzC5+lx9N8UoIvXzREFz6+hy1t8sTFLATaxx 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: Three architectures (x86, aarch64, riscv) have support for indirect branch tracking feature in a very similar fashion. On a very high level, indirect branch tracking is a CPU feature where CPU tracks branches which uses memory operand to perform control transfer in program. As part of this tracking on indirect branches, CPU goes in a state where it expects a landing pad instr on target and if not found then CPU raises some fault (architecture dependent) x86 landing pad instr - `ENDBRANCH` aarch64 landing pad instr - `BTI` riscv landing instr - `lpad` Given that three major arches have support for indirect branch tracking, This patch makes `prctl` for indirect branch tracking arch agnostic. To allow userspace to enable this feature for itself, following prtcls are defined: - PR_GET_INDIR_BR_LP_STATUS: Gets current configured status for indirect branch tracking. - PR_SET_INDIR_BR_LP_STATUS: Sets a configuration for indirect branch tracking. Following status options are allowed - PR_INDIR_BR_LP_ENABLE: Enables indirect branch tracking on user thread. - PR_INDIR_BR_LP_DISABLE; Disables indirect branch tracking on user thread. - PR_LOCK_INDIR_BR_LP_STATUS: Locks configured status for indirect branch tracking for user thread. Signed-off-by: Deepak Gupta --- include/linux/cpu.h | 4 ++++ include/uapi/linux/prctl.h | 27 +++++++++++++++++++++++++++ kernel/sys.c | 30 ++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+) diff --git a/include/linux/cpu.h b/include/linux/cpu.h index bdcec1732445..eff56aae05d7 100644 --- a/include/linux/cpu.h +++ b/include/linux/cpu.h @@ -203,4 +203,8 @@ static inline bool cpu_mitigations_auto_nosmt(void) } #endif +int arch_get_indir_br_lp_status(struct task_struct *t, unsigned long __user *status); +int arch_set_indir_br_lp_status(struct task_struct *t, unsigned long status); +int arch_lock_indir_br_lp_status(struct task_struct *t, unsigned long status); + #endif /* _LINUX_CPU_H_ */ diff --git a/include/uapi/linux/prctl.h b/include/uapi/linux/prctl.h index b8d7b6361754..41ffb53490a4 100644 --- a/include/uapi/linux/prctl.h +++ b/include/uapi/linux/prctl.h @@ -349,4 +349,31 @@ struct prctl_mm_map { */ #define PR_LOCK_SHADOW_STACK_STATUS 76 +/* + * Get the current indirect branch tracking configuration for the current + * thread, this will be the value configured via PR_SET_INDIR_BR_LP_STATUS. + */ +#define PR_GET_INDIR_BR_LP_STATUS 77 + +/* + * Set the indirect branch tracking configuration. PR_INDIR_BR_LP_ENABLE will + * enable cpu feature for user thread, to track all indirect branches and ensure + * they land on arch defined landing pad instruction. + * x86 - If enabled, an indirect branch must land on `ENDBRANCH` instruction. + * arch64 - If enabled, an indirect branch must land on `BTI` instruction. + * riscv - If enabled, an indirect branch must land on `lpad` instruction. + * PR_INDIR_BR_LP_DISABLE will disable feature for user thread and indirect + * branches will no more be tracked by cpu to land on arch defined landing pad + * instruction. + */ +#define PR_SET_INDIR_BR_LP_STATUS 78 +# define PR_INDIR_BR_LP_ENABLE (1UL << 0) + +/* + * Prevent further changes to the specified indirect branch tracking + * configuration. All bits may be locked via this call, including + * undefined bits. + */ +#define PR_LOCK_INDIR_BR_LP_STATUS 79 + #endif /* _LINUX_PRCTL_H */ diff --git a/kernel/sys.c b/kernel/sys.c index 7e0c10e867cf..5f88d358066d 100644 --- a/kernel/sys.c +++ b/kernel/sys.c @@ -2339,6 +2339,21 @@ int __weak arch_lock_shadow_stack_status(struct task_struct *t, unsigned long st return -EINVAL; } +int __weak arch_get_indir_br_lp_status(struct task_struct *t, unsigned long __user *status) +{ + return -EINVAL; +} + +int __weak arch_set_indir_br_lp_status(struct task_struct *t, unsigned long status) +{ + return -EINVAL; +} + +int __weak arch_lock_indir_br_lp_status(struct task_struct *t, unsigned long status) +{ + return -EINVAL; +} + #define PR_IO_FLUSHER (PF_MEMALLOC_NOIO | PF_LOCAL_THROTTLE) #ifdef CONFIG_ANON_VMA_NAME @@ -2812,6 +2827,21 @@ SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3, return -EINVAL; error = arch_lock_shadow_stack_status(me, arg2); break; + case PR_GET_INDIR_BR_LP_STATUS: + if (arg3 || arg4 || arg5) + return -EINVAL; + error = arch_get_indir_br_lp_status(me, (unsigned long __user *) arg2); + break; + case PR_SET_INDIR_BR_LP_STATUS: + if (arg3 || arg4 || arg5) + return -EINVAL; + error = arch_set_indir_br_lp_status(me, arg2); + break; + case PR_LOCK_INDIR_BR_LP_STATUS: + if (arg3 || arg4 || arg5) + return -EINVAL; + error = arch_lock_indir_br_lp_status(me, arg2); + break; default: error = -EINVAL; break; From patchwork Thu Sep 12 23:16:38 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13802842 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 6C450EEE270 for ; Thu, 12 Sep 2024 23:18:33 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id F17916B00B4; Thu, 12 Sep 2024 19:18:32 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id EC74E6B00B6; Thu, 12 Sep 2024 19:18:32 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CCC666B00B7; Thu, 12 Sep 2024 19:18:32 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id AA5ED6B00B4 for ; Thu, 12 Sep 2024 19:18:32 -0400 (EDT) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 67B68A94A7 for ; Thu, 12 Sep 2024 23:18:32 +0000 (UTC) X-FDA: 82557652464.04.452ADCA Received: from mail-pj1-f52.google.com (mail-pj1-f52.google.com [209.85.216.52]) by imf04.hostedemail.com (Postfix) with ESMTP id 8CCFB40013 for ; Thu, 12 Sep 2024 23:18:30 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b="E//2XvPI"; spf=pass (imf04.hostedemail.com: domain of debug@rivosinc.com designates 209.85.216.52 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1726183057; a=rsa-sha256; cv=none; b=TstWSFDrWH/bhk4+CaqfQF7JTSy/DkadGGvlEy57F4sN5NhVXsdJBlUIQ/5q1+jzAZ4DEs e3wrMakGgb2/mjCsZ6Agdkb6ZOg08pGABPMTkfM5Z+/BsXAuDPKEBBW4Ra1FZgH1s/nbvz zDcd2SHfmwamneO9QOy6gBugt8ERU2M= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b="E//2XvPI"; spf=pass (imf04.hostedemail.com: domain of debug@rivosinc.com designates 209.85.216.52 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=1726183057; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=dxl+asrYZyMA5EObtActYQsQI9/7SQp4qXUW6BzcbN8=; b=EMPxRzTDd654XxyJM0cFwvadyJGuABy+s6OKtRZnSrpUnV931ne7leyZjTKPIHXGkGnyuG ssOtPhcwNsE/LuDUUqp2hoFXh/WxPDQLLGVYpiEsBmxLLacuKjtASwxc1Z/254rv8RvOTu d4qHq3rxOQBqBcfQM3MqNPXCuF9L79Q= Received: by mail-pj1-f52.google.com with SMTP id 98e67ed59e1d1-2da4ea973bdso1267721a91.1 for ; Thu, 12 Sep 2024 16:18:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1726183109; x=1726787909; darn=kvack.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=dxl+asrYZyMA5EObtActYQsQI9/7SQp4qXUW6BzcbN8=; b=E//2XvPIPpyMUUTu7ibZXpkcYmYTl4PKbXLYDc5kMA13IECR6WZBOAcxgfRjPQeN3n VKWe8KIiT0Irvhm7bi6+VLJ61DzpSMo/D3DUqhiMDvlvxRgeMQbLFlB5e3BiY2uvwEPH lhSSyaxIfCgidNHw/8PPqzyh4eDLHlPhz8Y1XrDB6T0vMrxOLXn/bU16OL6RFX6Q9PUM pAo8M1SlQFQ7sBLPAGQwPw0L0b1g6zL9ffDmy1Xm8vH1KlYHeDMo06VMLDArKxJc2CTq L54iH1VXNNwQEd+w+98k8rBXHqo56Mp+YmLozxSWKF18hp0ZxUZNTs2JX3KtsuhnJjUT cgoA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726183109; x=1726787909; 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=dxl+asrYZyMA5EObtActYQsQI9/7SQp4qXUW6BzcbN8=; b=G+YESBaijXfy+COYa0+3zyr9b1ZUM5ec4MSoNBSvkxRwWkDfh2SKlDf0kGPt44Mf3x Vr5qpofL1J3lTPR0Qd4PBvykkxrnOkhg62K1wxUU1YG8iYJgRv6pW4eEBkT8nxB+C/EY ePGkuMRITBX68M9OpaWbqXPNVlpX/ce847exVq18ooRaa8g1XyRed1gtvc7OILSRqOs0 DaoI1CYJCEVHfI4DGyBvqDmeural8npN55jkI7cKyuOxUoTkOMne1fU4Pmk/JGnDvCkb hDNUS3k8oBIfqKM8WCdr198aqGtfv+ecGKn6KOhVay7zSX8M/MZWO8fDIbG+mRxN2mh2 08KQ== X-Forwarded-Encrypted: i=1; AJvYcCUxNzas8Vq2QA6v64WeoKEVUw1ZGvCnBB3aZURx7ZMVFxcexTdwZQjB9uy8i52g/1R/OKiPI+ZC1Q==@kvack.org X-Gm-Message-State: AOJu0YynG6PWK+vi9NSHOVQF5jWGLlx8OHOYYcNn16MuD4V7Bs9U5OUK cLuV8x5HJQa2po8Yjv7Y677NTZulCTiBWi1/aKP2F/Co8vIjt4btcJPMX/bh2/8= X-Google-Smtp-Source: AGHT+IEa4TPR9k56uhVbKEFp8flznEgSz0stUKrf5zCJaY7PljtKC23HwZsb6NuQgT/7g1yg03P/9Q== X-Received: by 2002:a17:90b:716:b0:2d8:92c7:d336 with SMTP id 98e67ed59e1d1-2db9fff0ebamr5713630a91.22.1726183109149; Thu, 12 Sep 2024 16:18:29 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2db6c1ac69asm3157591a91.0.2024.09.12.16.18.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 12 Sep 2024 16:18:28 -0700 (PDT) From: Deepak Gupta To: paul.walmsley@sifive.com, palmer@sifive.com, conor@kernel.org, linux-doc@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kselftest@vger.kernel.org Cc: corbet@lwn.net, palmer@dabbelt.com, aou@eecs.berkeley.edu, robh@kernel.org, krzk+dt@kernel.org, oleg@redhat.com, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, hpa@zytor.com, peterz@infradead.org, akpm@linux-foundation.org, arnd@arndb.de, ebiederm@xmission.com, kees@kernel.org, Liam.Howlett@oracle.com, vbabka@suse.cz, lorenzo.stoakes@oracle.com, shuah@kernel.org, brauner@kernel.org, samuel.holland@sifive.com, debug@rivosinc.com, andy.chiu@sifive.com, jerry.shih@sifive.com, greentime.hu@sifive.com, charlie@rivosinc.com, evan@rivosinc.com, cleger@rivosinc.com, xiao.w.wang@intel.com, ajones@ventanamicro.com, anup@brainfault.org, mchitale@ventanamicro.com, atishp@rivosinc.com, sameo@rivosinc.com, bjorn@rivosinc.com, alexghiti@rivosinc.com, david@redhat.com, libang.li@antgroup.com, jszhang@kernel.org, leobras@redhat.com, guoren@kernel.org, samitolvanen@google.com, songshuaishuai@tinylab.org, costa.shul@redhat.com, bhe@redhat.com, zong.li@sifive.com, puranjay@kernel.org, namcaov@gmail.com, antonb@tenstorrent.com, sorear@fastmail.com, quic_bjorande@quicinc.com, ancientmodern4@gmail.com, ben.dooks@codethink.co.uk, quic_zhonhan@quicinc.com, cuiyunhui@bytedance.com, yang.lee@linux.alibaba.com, ke.zhao@shingroup.cn, sunilvl@ventanamicro.com, tanzhasanwork@gmail.com, schwab@suse.de, dawei.li@shingroup.cn, rppt@kernel.org, willy@infradead.org, usama.anjum@collabora.com, osalvador@suse.de, ryan.roberts@arm.com, andrii@kernel.org, alx@kernel.org, catalin.marinas@arm.com, broonie@kernel.org, revest@chromium.org, bgray@linux.ibm.com, deller@gmx.de, zev@bewilderbeest.net Subject: [PATCH v4 19/30] riscv: Implements arch agnostic shadow stack prctls Date: Thu, 12 Sep 2024 16:16:38 -0700 Message-ID: <20240912231650.3740732-20-debug@rivosinc.com> X-Mailer: git-send-email 2.45.0 In-Reply-To: <20240912231650.3740732-1-debug@rivosinc.com> References: <20240912231650.3740732-1-debug@rivosinc.com> MIME-Version: 1.0 X-Stat-Signature: k68bifqszus77jrecfozqtfk63urcnob X-Rspamd-Queue-Id: 8CCFB40013 X-Rspam-User: X-Rspamd-Server: rspam10 X-HE-Tag: 1726183110-937665 X-HE-Meta: U2FsdGVkX1/IFvQyKqzkG4cGH4w5sFccqNE/yZSHqkBNIiz10hjrpuE+kiYfs4/Hp85ZGVqwRpal8gMYPkE8Bgf/q4gzZrngegpEHC22gHx5gjmOhuFFoqSiZMauS/E7/bnYNIgH8TmWG4xcUpgU5SGZuIEszpCEbBKT69Qj2HI3RM2vvwtf/pV515IHydlDpQ0ma9aYTbUM1zoK6CMfkNus4CI4KU/DWr3GFbkD87Fqrwu0K/s5PaaflNZgp1Li3S2FXYFPMHxB5tGWTree+3H9XgcsJ7oejPLd2xHTvZ2QDRGFH+7OkAJxqiC8jzc1uq3X+uqgL0Q2A4/xb95w2ZlF3ZhI067rzE9SuCctV6AwMznpD5xd12UTh89EEBT82fApuY/gPYnk2PUTcIEn3dfjIbrJEQKB6JDXNPFYz4sYfVz1zD2eIb+eXiY37Lp7sBmmWYtNJUIcR5onK6VlJOn207s8/67Ks8DHeCupz3b1RuOqLMcalJoKNJGMHq2aJweQfXzn+G+Q6yDc3CtJWNs7WyWOzKfreyOS0t3g0+A5uazoscvMLhYOpkN3UGmAK+wXdiIdqobwZlp9fPnFpUd/kYA1WJwRuRZ3BasFESi8Lqzc1WdQ8XjGbnTjT6MVqiZNEr8mNaFyHW4sO7plMi1BDD47Do1bBRGih1BCQkLxRjxALUbuQWwFsJ4GRV0luSj1jR49QDxIF5iTtOP5TrZi/cZXqi/ukN4vP35Ikzb3WpXsBrgYWvNKrjSSGjH+qfgBWxctj+qw/dtPoVkaI2LFVXkkihRnUmh5QWUS4WOEQ1KUBfFocMvv9kIbc04hQKzQsImY5Gkk/be+m/eqWoivx66N+SIP5VqG8FDUvisUPc0RYQ56tfCsTm0v8ZkCYbYMvPT+Y0yDeDCzEvpQ9YDCqdGEUgeAdt3fSXlLeecUyheXbGzpidT71o7domwBGZ9X/ZMmUxC73oasc12 xXOKnKUA 64eFreL4gBbHxWlL32GG0rsi5Pu2pTfslLl2f/CqFvUlx2ozTBAnreaT4SOLg1iWF9QRbwkSkjS6PsjYi28AIXmNg47Z9Ow3W+AuZPGFtUlddomGTeLcKJnSS7DmnX/tMnk0dajOje/SGdLKoOyaA3LHflCoC/TURV7PBve6GNqivtpfKkebQKedEi/JYb9tb0Psa353t4ZwEIHxfWeywSUVrLPtzLJhQt5FEIecai16tPanqOTsHvLkBb+HygZfWBPOElGMpZt6TGHkpmLMwMcneDDNaaxCkmlZyrr6Msxh8ip0I6f+Cl1IElQOHCDr6p0rrcQMlLuo07F2TvRZlZcPr8/sK7ZCIY5DvEeV29SXh8HB8Mj5oD8ELyvhSbM8I/in3pOkaXxj31C5b6z0s0eQuevoCVskQxXjVBaHSzdjbHn0V6m/dMU+8tdyRXChonmRM 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: Implement architecture agnostic prctls() interface for setting and getting shadow stack status. prctls implemented are PR_GET_SHADOW_STACK_STATUS, PR_SET_SHADOW_STACK_STATUS and PR_LOCK_SHADOW_STACK_STATUS. As part of PR_SET_SHADOW_STACK_STATUS/PR_GET_SHADOW_STACK_STATUS, only PR_SHADOW_STACK_ENABLE is implemented because RISCV allows each mode to write to their own shadow stack using `sspush` or `ssamoswap`. PR_LOCK_SHADOW_STACK_STATUS locks current configuration of shadow stack enabling. Signed-off-by: Deepak Gupta --- arch/riscv/include/asm/usercfi.h | 30 ++++++++- arch/riscv/kernel/process.c | 8 +++ arch/riscv/kernel/usercfi.c | 107 +++++++++++++++++++++++++++++++ 3 files changed, 144 insertions(+), 1 deletion(-) diff --git a/arch/riscv/include/asm/usercfi.h b/arch/riscv/include/asm/usercfi.h index a6974307ca6c..6d806fbb283d 100644 --- a/arch/riscv/include/asm/usercfi.h +++ b/arch/riscv/include/asm/usercfi.h @@ -7,6 +7,7 @@ #ifndef __ASSEMBLY__ #include +#include struct task_struct; struct kernel_clone_args; @@ -14,7 +15,8 @@ struct kernel_clone_args; #ifdef CONFIG_RISCV_USER_CFI struct cfi_status { unsigned long ubcfi_en : 1; /* Enable for backward cfi. */ - unsigned long rsvd : ((sizeof(unsigned long)*8) - 1); + unsigned long ubcfi_locked : 1; + unsigned long rsvd : ((sizeof(unsigned long)*8) - 2); unsigned long user_shdw_stk; /* Current user shadow stack pointer */ unsigned long shdw_stk_base; /* Base address of shadow stack */ unsigned long shdw_stk_size; /* size of shadow stack */ @@ -27,6 +29,12 @@ void set_shstk_base(struct task_struct *task, unsigned long shstk_addr, unsigned unsigned long get_shstk_base(struct task_struct *task, unsigned long *size); void set_active_shstk(struct task_struct *task, unsigned long shstk_addr); bool is_shstk_enabled(struct task_struct *task); +bool is_shstk_locked(struct task_struct *task); +bool is_shstk_allocated(struct task_struct *task); +void set_shstk_lock(struct task_struct *task); +void set_shstk_status(struct task_struct *task, bool enable); + +#define PR_SHADOW_STACK_SUPPORTED_STATUS_MASK (PR_SHADOW_STACK_ENABLE) #else @@ -62,6 +70,26 @@ static inline bool is_shstk_enabled(struct task_struct *task) return false; } +static inline bool is_shstk_locked(struct task_struct *task) +{ + return false; +} + +bool is_shstk_allocated(struct task_struct *task) +{ + return false; +} + +void set_shstk_lock(struct task_struct *task) +{ + +} + +static inline void set_shstk_status(struct task_struct *task, bool enable) +{ + +} + #endif /* CONFIG_RISCV_USER_CFI */ #endif /* __ASSEMBLY__ */ diff --git a/arch/riscv/kernel/process.c b/arch/riscv/kernel/process.c index 6747db72435a..b64baa0235ea 100644 --- a/arch/riscv/kernel/process.c +++ b/arch/riscv/kernel/process.c @@ -143,6 +143,14 @@ void start_thread(struct pt_regs *regs, unsigned long pc, regs->epc = pc; regs->sp = sp; + /* + * clear shadow stack state on exec. + * libc will set it later via prctl. + */ + set_shstk_status(current, false); + set_shstk_base(current, 0, 0); + set_active_shstk(current, 0); + #ifdef CONFIG_64BIT regs->status &= ~SR_UXL; diff --git a/arch/riscv/kernel/usercfi.c b/arch/riscv/kernel/usercfi.c index 7a7f0b57b2d4..c77abe552c88 100644 --- a/arch/riscv/kernel/usercfi.c +++ b/arch/riscv/kernel/usercfi.c @@ -24,6 +24,16 @@ bool is_shstk_enabled(struct task_struct *task) return task->thread_info.user_cfi_state.ubcfi_en ? true : false; } +bool is_shstk_allocated(struct task_struct *task) +{ + return task->thread_info.user_cfi_state.shdw_stk_base ? true : false; +} + +bool is_shstk_locked(struct task_struct *task) +{ + return task->thread_info.user_cfi_state.ubcfi_locked ? true : false; +} + void set_shstk_base(struct task_struct *task, unsigned long shstk_addr, unsigned long size) { task->thread_info.user_cfi_state.shdw_stk_base = shstk_addr; @@ -42,6 +52,23 @@ void set_active_shstk(struct task_struct *task, unsigned long shstk_addr) task->thread_info.user_cfi_state.user_shdw_stk = shstk_addr; } +void set_shstk_status(struct task_struct *task, bool enable) +{ + task->thread_info.user_cfi_state.ubcfi_en = enable ? 1 : 0; + + if (enable) + task->thread_info.envcfg |= ENVCFG_SSE; + else + task->thread_info.envcfg &= ~ENVCFG_SSE; + + csr_write(CSR_ENVCFG, task->thread_info.envcfg); +} + +void set_shstk_lock(struct task_struct *task) +{ + task->thread_info.user_cfi_state.ubcfi_locked = 1; +} + /* * If size is 0, then to be compatible with regular stack we want it to be as big as * regular stack. Else PAGE_ALIGN it and return back @@ -264,3 +291,83 @@ void shstk_release(struct task_struct *tsk) vm_munmap(base, size); set_shstk_base(tsk, 0, 0); } + +int arch_get_shadow_stack_status(struct task_struct *t, unsigned long __user *status) +{ + unsigned long bcfi_status = 0; + + if (!cpu_supports_shadow_stack()) + return -EINVAL; + + /* this means shadow stack is enabled on the task */ + bcfi_status |= (is_shstk_enabled(t) ? PR_SHADOW_STACK_ENABLE : 0); + + return copy_to_user(status, &bcfi_status, sizeof(bcfi_status)) ? -EFAULT : 0; +} + +int arch_set_shadow_stack_status(struct task_struct *t, unsigned long status) +{ + unsigned long size = 0, addr = 0; + bool enable_shstk = false; + + if (!cpu_supports_shadow_stack()) + return -EINVAL; + + /* Reject unknown flags */ + if (status & ~PR_SHADOW_STACK_SUPPORTED_STATUS_MASK) + return -EINVAL; + + /* bcfi status is locked and further can't be modified by user */ + if (is_shstk_locked(t)) + return -EINVAL; + + enable_shstk = status & PR_SHADOW_STACK_ENABLE; + /* Request is to enable shadow stack and shadow stack is not enabled already */ + if (enable_shstk && !is_shstk_enabled(t)) { + /* shadow stack was allocated and enable request again + * no need to support such usecase and return EINVAL. + */ + if (is_shstk_allocated(t)) + return -EINVAL; + + size = calc_shstk_size(0); + addr = allocate_shadow_stack(0, size, 0, false); + if (IS_ERR_VALUE(addr)) + return -ENOMEM; + set_shstk_base(t, addr, size); + set_active_shstk(t, addr + size); + } + + /* + * If a request to disable shadow stack happens, let's go ahead and release it + * Although, if CLONE_VFORKed child did this, then in that case we will end up + * not releasing the shadow stack (because it might be needed in parent). Although + * we will disable it for VFORKed child. And if VFORKed child tries to enable again + * then in that case, it'll get entirely new shadow stack because following condition + * are true + * - shadow stack was not enabled for vforked child + * - shadow stack base was anyways pointing to 0 + * This shouldn't be a big issue because we want parent to have availability of shadow + * stack whenever VFORKed child releases resources via exit or exec but at the same + * time we want VFORKed child to break away and establish new shadow stack if it desires + * + */ + if (!enable_shstk) + shstk_release(t); + + set_shstk_status(t, enable_shstk); + return 0; +} + +int arch_lock_shadow_stack_status(struct task_struct *task, + unsigned long arg) +{ + /* If shtstk not supported or not enabled on task, nothing to lock here */ + if (!cpu_supports_shadow_stack() || + !is_shstk_enabled(task)) + return -EINVAL; + + set_shstk_lock(task); + + return 0; +} From patchwork Thu Sep 12 23:16:39 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13802843 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 B1569EEE271 for ; Thu, 12 Sep 2024 23:18:37 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 41CA76B00B6; Thu, 12 Sep 2024 19:18:37 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 3CB2B6B00B8; Thu, 12 Sep 2024 19:18:37 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1D1038D0002; Thu, 12 Sep 2024 19:18:37 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id F23046B00B6 for ; Thu, 12 Sep 2024 19:18:36 -0400 (EDT) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id AB96E80DA1 for ; Thu, 12 Sep 2024 23:18:36 +0000 (UTC) X-FDA: 82557652632.27.B3CA878 Received: from mail-pl1-f178.google.com (mail-pl1-f178.google.com [209.85.214.178]) by imf03.hostedemail.com (Postfix) with ESMTP id D8EEB20011 for ; Thu, 12 Sep 2024 23:18:34 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=kNjve1Tr; dmarc=none; spf=pass (imf03.hostedemail.com: domain of debug@rivosinc.com designates 209.85.214.178 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1726182997; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=KwRJ4ewhEVAckpzni0eOC4W3kOUznT5QSphy/YvwFF4=; b=2F5CfpLH4L0DtGZ62aHDCAvEZj8+p/+f5+OOtzHCXJmvPDKYxuvdHhVcY2K/hfFV8LXtuh 78R/2TTJf6v3fqnmVlaHYT7jqUXxKFp7bD1b0O3Wn/7PWuf/u+KqaWbUO1CtB/XQzT6CQW hzwkhul32ZNr2mQA0KIeeo9w/z57dj8= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1726182997; a=rsa-sha256; cv=none; b=wnBCQDITItoMYzSSLQh9I45JdUdFVLLtfx2vEt7jRgfprMd8fKTUwxEpsPmjgiVMxy9xl7 wOQ62gL87N56LJ4P8PgKfAiY/cbPtOlMPMRgVBFLF25S1SoyWhjsqyN+hDQnqvyASFdpeq HoifF8bZ0UCrHdGa1zl8FUeTXoInjEI= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=kNjve1Tr; dmarc=none; spf=pass (imf03.hostedemail.com: domain of debug@rivosinc.com designates 209.85.214.178 as permitted sender) smtp.mailfrom=debug@rivosinc.com Received: by mail-pl1-f178.google.com with SMTP id d9443c01a7336-20570b42f24so17948735ad.1 for ; Thu, 12 Sep 2024 16:18:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1726183114; x=1726787914; darn=kvack.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=KwRJ4ewhEVAckpzni0eOC4W3kOUznT5QSphy/YvwFF4=; b=kNjve1Tr5hYbPgEtkJKYJaVZrrxDLK++ozxV2cNqCxpZBxkEtipFmqQ3DlCHepvnYR p5jglkbtyvkNiCViPhwz/O0LWyaegQRnwvsZv7xJPBg1sburkySpgT31ByLSZwotPc3x rCv+wGv5H/XsFOLJDhq3j0awiN/Chhl0TbVVoa9t2ch5iYG3BGk5F0kbr6dsFuvo9yqb o3twgn+Z+a+p6KxxdqimqoOU4vr8nARPD3gmYn9P/KfaUpgywm2haUu1GDEhFLhiv4/Q V6mIrrcQrk/LfrH00A3FKM/mXvTfINdNVNZpwM21lkOr7MTCDdAkpwsZxLi10o0XvNdu VJLw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726183114; x=1726787914; 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=KwRJ4ewhEVAckpzni0eOC4W3kOUznT5QSphy/YvwFF4=; b=kx7hwMjzKpnwg4OmhcMedVXqBZJTA7g61O65oGec9YhW7pS0g3AVThF/Lpw/xGZ+9+ T6/U5gbzb1XFuXMfJFjWWdaAn6RNgc2i0sO+gvxxUA5NV4Cd/PXpNOsVzYmi2XUFIn/y zQQkmk8C3Fd4F56FYnmbH9X6O4k2mxExcN0Ak4uu/GqSq70LY8aSyMuYIhSV+IxpjHmr LgAyqT/CaEoGt/T4Uexo+2m8HyahacPFMRwhmdpNDBOBVIeSH2RvRCuWKt2xmy2KOxNd 7wZKxC8GYc1pW2weWY+OoEmraFr6eKHnKOjVuv7eFIf8syD7Qa5fm1vSnynqS/dP2xHz 27oQ== X-Forwarded-Encrypted: i=1; AJvYcCUvHPNp8Yb37dj371zCnak+UPQ0IgNebPlqn8+LmcAbmYcOzF0SBQpm0J0Mh2W8ZLDhwKyH49long==@kvack.org X-Gm-Message-State: AOJu0YzZmz46RigNsXD4Sr2ZAMoMEjjlc06jumI7CBbtDq5wdegnWTv0 h3oxZn+/fjSUeqpWL/qpJ6UE8VILO1M0lCR9aDNaWnjIsR6aeNtCllKDcM6iblo= X-Google-Smtp-Source: AGHT+IG1JMYVnuZDDv5vrKY0Ap6nzQ8EAROaZEkaQsrbR4Aftj/1uKtP8Rx6b8tiDt3qNH/8ZNKatg== X-Received: by 2002:a17:90b:4c0c:b0:2d3:c0e5:cbac with SMTP id 98e67ed59e1d1-2db9ffa11e4mr4871259a91.7.1726183113476; Thu, 12 Sep 2024 16:18:33 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2db6c1ac69asm3157591a91.0.2024.09.12.16.18.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 12 Sep 2024 16:18:33 -0700 (PDT) From: Deepak Gupta To: paul.walmsley@sifive.com, palmer@sifive.com, conor@kernel.org, linux-doc@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kselftest@vger.kernel.org Cc: corbet@lwn.net, palmer@dabbelt.com, aou@eecs.berkeley.edu, robh@kernel.org, krzk+dt@kernel.org, oleg@redhat.com, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, hpa@zytor.com, peterz@infradead.org, akpm@linux-foundation.org, arnd@arndb.de, ebiederm@xmission.com, kees@kernel.org, Liam.Howlett@oracle.com, vbabka@suse.cz, lorenzo.stoakes@oracle.com, shuah@kernel.org, brauner@kernel.org, samuel.holland@sifive.com, debug@rivosinc.com, andy.chiu@sifive.com, jerry.shih@sifive.com, greentime.hu@sifive.com, charlie@rivosinc.com, evan@rivosinc.com, cleger@rivosinc.com, xiao.w.wang@intel.com, ajones@ventanamicro.com, anup@brainfault.org, mchitale@ventanamicro.com, atishp@rivosinc.com, sameo@rivosinc.com, bjorn@rivosinc.com, alexghiti@rivosinc.com, david@redhat.com, libang.li@antgroup.com, jszhang@kernel.org, leobras@redhat.com, guoren@kernel.org, samitolvanen@google.com, songshuaishuai@tinylab.org, costa.shul@redhat.com, bhe@redhat.com, zong.li@sifive.com, puranjay@kernel.org, namcaov@gmail.com, antonb@tenstorrent.com, sorear@fastmail.com, quic_bjorande@quicinc.com, ancientmodern4@gmail.com, ben.dooks@codethink.co.uk, quic_zhonhan@quicinc.com, cuiyunhui@bytedance.com, yang.lee@linux.alibaba.com, ke.zhao@shingroup.cn, sunilvl@ventanamicro.com, tanzhasanwork@gmail.com, schwab@suse.de, dawei.li@shingroup.cn, rppt@kernel.org, willy@infradead.org, usama.anjum@collabora.com, osalvador@suse.de, ryan.roberts@arm.com, andrii@kernel.org, alx@kernel.org, catalin.marinas@arm.com, broonie@kernel.org, revest@chromium.org, bgray@linux.ibm.com, deller@gmx.de, zev@bewilderbeest.net Subject: [PATCH v4 20/30] riscv: Implements arch agnostic indirect branch tracking prctls Date: Thu, 12 Sep 2024 16:16:39 -0700 Message-ID: <20240912231650.3740732-21-debug@rivosinc.com> X-Mailer: git-send-email 2.45.0 In-Reply-To: <20240912231650.3740732-1-debug@rivosinc.com> References: <20240912231650.3740732-1-debug@rivosinc.com> MIME-Version: 1.0 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: D8EEB20011 X-Stat-Signature: 4npadq14uw3sig8t4zztxks88ac8fogg X-Rspam-User: X-HE-Tag: 1726183114-777805 X-HE-Meta: U2FsdGVkX1/NLZxn5zciP5nwINE7QJMC8cspeXHghSevbckrbn529M4TcSIjZnOLCzoI3ZKfzI7IhVCo5Qh8q8SUutKrdqREA5bZg2vCyF+2QWCNZCLSneQyPPOHAbniEECxI1erpIGxJ3KOMDfVGv24UyNnSgtjS+uX5WOTrbPlBkTLwvuzjy5zWFI91QS2+rVIPx+QGJh/v8XrqbdXgq8BB80ziZjs5zf6Yu6hGg5/cOsN0CQIRSUjHLTe4l6fDmsReWn+elzAPnKLmxFEh/zd92XSjnvLiQtVWPM7kEukv71pJRDEHcWFX3REtkv9zHnFHIJoxEAxQVJbNBPrHJhXILlbh1NO3+6g477Drcpg3ESOixeUgRCiqozDd2f5ktpmgefuyDdKVAjux/54qUNVIPebrHWJraNe/KLfE/IL9Ni2d93CHZc+MPmno7XqXpUrQLMR+M9khOXTrR9ZBl+kAZ8Lm/C29rLryqzPJFtO5igIv91emMbSDJTJyk/SH3OPzL8L8w9FQDDzU26qIyiRNrypKzyyYR+AT3umSfcc4TCtxqhezg07B0GLQ3vr5xhF8Il1nmoUjdBZAPtfQAOiXnX0kegXCLXirWEhhChlcGo6PZHpK+mSIHFUQoWKb/t+EP6rvN0AQNlGw8Cj8hGP8UeKaXDVSsBSpE79T4pMsmul/cY6R7xpp8bzME8U+goA4BlfpXkTB1etbdZkG0dmbdJwTHMEJf//sEOx2RAaFCCUkRV1SCtjrhLrtOSwh4NJIU3QkwcvZ6B7B/ZSvp7O5ffA1k08Kmbd6DBr5YjMSjj5j/qtntmkux+b+411nw0bUpQcOAy4i4HXZ2gR6jydgthkopzTftNOUO4SqAO4ssAN/yPV85VAlTsMNDe3XX/jseTUjvPV7ZbE+Ae20n2QBeQp32PR4mJaS/1Lr+KlrU4oakRsZAKImsmSeia2H8pHOqNEMPcs/+WmqKa Jt8Btv0l oLfj4/2vMlwmwgS4Nyi6tPSq1poD38unJ1NTu22bt6K1NYcC/yKg96x4SBc98LAskNVaFy8Qa5AwuNtSnAGROftJU8U7kffeHut2EZSwHdFWJ+VWflJEP6oRwK9lBb4Ga7mC8nNMJQWZZacpwf5opksQ6cE/hol77Lvyqc27RxNP/mvTayhTEIrm2yOYtrq1SzRoo75zG2ZkHeSSlY4yk98Y2jSTLm5o7unt5rqQ8S1C0cnXsvv2vN458Rg7vj936mQzJ1s5Pgu48cH2EpcdBUsXe3XqGLiyXoHR2nPWetbNx4lR74BnOEBnSjv62BQbaz9Zr3vuUH7HDbN7/J3ftnDj+N+TSG84KUedq1hpl5KL0m6kVNy3oXqipmgpLvFteXQnS22K883MJEo3hG7ZP/Narx3S14ec0fTRNTD219zaGvFSQnmymIwKKrXFSGEjv3jeu 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: prctls implemented are: PR_SET_INDIR_BR_LP_STATUS, PR_GET_INDIR_BR_LP_STATUS and PR_LOCK_INDIR_BR_LP_STATUS. Signed-off-by: Deepak Gupta --- arch/riscv/include/asm/usercfi.h | 28 +++++++++++- arch/riscv/kernel/process.c | 5 +++ arch/riscv/kernel/usercfi.c | 76 ++++++++++++++++++++++++++++++++ 3 files changed, 108 insertions(+), 1 deletion(-) diff --git a/arch/riscv/include/asm/usercfi.h b/arch/riscv/include/asm/usercfi.h index 6d806fbb283d..20a9102cce51 100644 --- a/arch/riscv/include/asm/usercfi.h +++ b/arch/riscv/include/asm/usercfi.h @@ -16,7 +16,9 @@ struct kernel_clone_args; struct cfi_status { unsigned long ubcfi_en : 1; /* Enable for backward cfi. */ unsigned long ubcfi_locked : 1; - unsigned long rsvd : ((sizeof(unsigned long)*8) - 2); + unsigned long ufcfi_en : 1; /* Enable for forward cfi. Note that ELP goes in sstatus */ + unsigned long ufcfi_locked : 1; + unsigned long rsvd : ((sizeof(unsigned long)*8) - 4); unsigned long user_shdw_stk; /* Current user shadow stack pointer */ unsigned long shdw_stk_base; /* Base address of shadow stack */ unsigned long shdw_stk_size; /* size of shadow stack */ @@ -33,6 +35,10 @@ bool is_shstk_locked(struct task_struct *task); bool is_shstk_allocated(struct task_struct *task); void set_shstk_lock(struct task_struct *task); void set_shstk_status(struct task_struct *task, bool enable); +bool is_indir_lp_enabled(struct task_struct *task); +bool is_indir_lp_locked(struct task_struct *task); +void set_indir_lp_status(struct task_struct *task, bool enable); +void set_indir_lp_lock(struct task_struct *task); #define PR_SHADOW_STACK_SUPPORTED_STATUS_MASK (PR_SHADOW_STACK_ENABLE) @@ -90,6 +96,26 @@ static inline void set_shstk_status(struct task_struct *task, bool enable) } +static inline bool is_indir_lp_enabled(struct task_struct *task) +{ + return false; +} + +static inline bool is_indir_lp_locked(struct task_struct *task) +{ + return false; +} + +static inline void set_indir_lp_status(struct task_struct *task, bool enable) +{ + +} + +void set_indir_lp_lock(struct task_struct *task) +{ + +} + #endif /* CONFIG_RISCV_USER_CFI */ #endif /* __ASSEMBLY__ */ diff --git a/arch/riscv/kernel/process.c b/arch/riscv/kernel/process.c index b64baa0235ea..f3c5b8f2c869 100644 --- a/arch/riscv/kernel/process.c +++ b/arch/riscv/kernel/process.c @@ -150,6 +150,11 @@ void start_thread(struct pt_regs *regs, unsigned long pc, set_shstk_status(current, false); set_shstk_base(current, 0, 0); set_active_shstk(current, 0); + /* + * disable indirect branch tracking on exec. + * libc will enable it later via prctl. + */ + set_indir_lp_status(current, false); #ifdef CONFIG_64BIT regs->status &= ~SR_UXL; diff --git a/arch/riscv/kernel/usercfi.c b/arch/riscv/kernel/usercfi.c index c77abe552c88..8da509afdbe9 100644 --- a/arch/riscv/kernel/usercfi.c +++ b/arch/riscv/kernel/usercfi.c @@ -69,6 +69,32 @@ void set_shstk_lock(struct task_struct *task) task->thread_info.user_cfi_state.ubcfi_locked = 1; } +bool is_indir_lp_enabled(struct task_struct *task) +{ + return task->thread_info.user_cfi_state.ufcfi_en ? true : false; +} + +bool is_indir_lp_locked(struct task_struct *task) +{ + return task->thread_info.user_cfi_state.ufcfi_locked ? true : false; +} + +void set_indir_lp_status(struct task_struct *task, bool enable) +{ + task->thread_info.user_cfi_state.ufcfi_en = enable ? 1 : 0; + + if (enable) + task->thread_info.envcfg |= ENVCFG_LPE; + else + task->thread_info.envcfg &= ~ENVCFG_LPE; + + csr_write(CSR_ENVCFG, task->thread_info.envcfg); +} + +void set_indir_lp_lock(struct task_struct *task) +{ + task->thread_info.user_cfi_state.ufcfi_locked = 1; +} /* * If size is 0, then to be compatible with regular stack we want it to be as big as * regular stack. Else PAGE_ALIGN it and return back @@ -371,3 +397,53 @@ int arch_lock_shadow_stack_status(struct task_struct *task, return 0; } + +int arch_get_indir_br_lp_status(struct task_struct *t, unsigned long __user *status) +{ + unsigned long fcfi_status = 0; + + if (!cpu_supports_indirect_br_lp_instr()) + return -EINVAL; + + /* indirect branch tracking is enabled on the task or not */ + fcfi_status |= (is_indir_lp_enabled(t) ? PR_INDIR_BR_LP_ENABLE : 0); + + return copy_to_user(status, &fcfi_status, sizeof(fcfi_status)) ? -EFAULT : 0; +} + +int arch_set_indir_br_lp_status(struct task_struct *t, unsigned long status) +{ + bool enable_indir_lp = false; + + if (!cpu_supports_indirect_br_lp_instr()) + return -EINVAL; + + /* indirect branch tracking is locked and further can't be modified by user */ + if (is_indir_lp_locked(t)) + return -EINVAL; + + /* Reject unknown flags */ + if (status & ~PR_INDIR_BR_LP_ENABLE) + return -EINVAL; + + enable_indir_lp = (status & PR_INDIR_BR_LP_ENABLE) ? true : false; + set_indir_lp_status(t, enable_indir_lp); + + return 0; +} + +int arch_lock_indir_br_lp_status(struct task_struct *task, + unsigned long arg) +{ + /* + * If indirect branch tracking is not supported or not enabled on task, + * nothing to lock here + */ + if (!cpu_supports_indirect_br_lp_instr() || + !is_indir_lp_enabled(task)) + return -EINVAL; + + set_indir_lp_lock(task); + + return 0; +} From patchwork Thu Sep 12 23:16:40 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13802844 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 130DAEEE272 for ; Thu, 12 Sep 2024 23:18:42 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8FDBA8D0002; Thu, 12 Sep 2024 19:18:41 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 8ACE06B00B9; Thu, 12 Sep 2024 19:18:41 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6D8D58D0002; Thu, 12 Sep 2024 19:18:41 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 4E41E6B00B8 for ; Thu, 12 Sep 2024 19:18:41 -0400 (EDT) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 0CBEAA504C for ; Thu, 12 Sep 2024 23:18:41 +0000 (UTC) X-FDA: 82557652842.16.4DA66B0 Received: from mail-pg1-f170.google.com (mail-pg1-f170.google.com [209.85.215.170]) by imf15.hostedemail.com (Postfix) with ESMTP id 3938CA0003 for ; Thu, 12 Sep 2024 23:18:38 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=HWKFFMJ0; dmarc=none; spf=pass (imf15.hostedemail.com: domain of debug@rivosinc.com designates 209.85.215.170 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1726183041; a=rsa-sha256; cv=none; b=ElWbpU5Ixhr1ugt5slDRxOnEinUeKWyrQRH5sKroTtnROLYHABYd4x3s9JYWCkPOt6Pv2E vBdH0He3u4GAVvkHNWVHzGVr71eKYE1uCcK/SRypc/b5vtIWwFGAOnzMDqt6oCjsn1wiHv DnXK1SQSpga6dN+0iuIIWNsmpRuI9RI= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=HWKFFMJ0; dmarc=none; spf=pass (imf15.hostedemail.com: domain of debug@rivosinc.com designates 209.85.215.170 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1726183041; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=yXfnoEa3i1wyQd1F0z4P7UzjOQKWsjZf9fduhQBzJq4=; b=MrQIhWXgzRkotma2XZFN2x0R2RtZjQONgBRHOvLtCZSoFhvkjrcGPdXHHarVblsmlMA8mS hsRoHgpzP1WyOTA0aPH0om75/34aED5VXjHN0IIKUwLtOM452vDeX8CydtNeXtEy4lyKjE FhdflVdzvTvR/1ke4N64RTwSeiWLntw= Received: by mail-pg1-f170.google.com with SMTP id 41be03b00d2f7-7db238d07b3so1127276a12.2 for ; Thu, 12 Sep 2024 16:18:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1726183118; x=1726787918; darn=kvack.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=yXfnoEa3i1wyQd1F0z4P7UzjOQKWsjZf9fduhQBzJq4=; b=HWKFFMJ0ow1fVCqXDxUKwzTElMumvAKs7qLqkp2NH3g7kJV9Sndb1eCUVu+LnlRWCm +OKi118GZeGVEXE2ut78SKvLqJh9joLJwX9beLd1x0iGZfqM3YCwy1Rtt8l5M7MtxrBh WrRpnYOL1Qr73WhkNV/nm1LJPPKx4HE953pqCerx02eYwucAr8dzHIDchLHSbupUCbOs 8X+PiuwEyQl0NUJNKyuj0D/HsICCZw4yrvDPK7+OJNdCO8TRyfFrPBGNu+vf4G56w4jO MaYQ7G6/2RjGXuWOOBqaR5bD6t8sRr1hvc4psIWPpdRUTMFjQ2fsDmaSDIkRL5Zlm7HQ t7qQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726183118; x=1726787918; 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=yXfnoEa3i1wyQd1F0z4P7UzjOQKWsjZf9fduhQBzJq4=; b=jX8vRhvO9bXW/pslIKXEFCEJm4qkS2979fsqXrcJ+Kd6HWwA85nzti+tcSv5LPq5Ds TGy2ngX2yaEEAfwBtynrGaXkZSVhOsHtoNa+d0Kk0D2BL2+NAyJpRHDbim9JFRqjWELQ sGw0Po/Y8BQqB1CEMqVDGMv8qA54ST5IVz8obamBi5xDxIjDrjrw+ClpTbDLWZJ6+1kL Kb0CpppL7gnzHo0zySJyWhJN/mYel8PASrqqnR37YKsisgHI0u23WOmAsS2iWOney5T3 PkpzziNCBp0bVUDcMQ1LKa6q3dN3FATNT5IB+DB4dFrEAYkb928bLU+rPyLDFdZDiikR SG6Q== X-Forwarded-Encrypted: i=1; AJvYcCUNpbUE6TFEplN6r/Edh3IStF92P0eXhHG5xLCFSCagllTOqMMcr6GiCAG4n2OqX+VfilUelUiP1g==@kvack.org X-Gm-Message-State: AOJu0YwDUV7WFi1lf0dX1ofo24pkGdn6JA5I43JVud25K66AYhqAh/QL 6pP2dEohn6Xs37pI68nN8+RjdE2MP9MtuGejxuZxFH9yogSc9jc2xedpBhgtxxs= X-Google-Smtp-Source: AGHT+IFuYD8h65+1U6P2mbP/p4Fa/VUP3aMNny81RffweMyQhp3hs7nPg1eND2jqWBezKJj0ChZXLw== X-Received: by 2002:a17:90b:1043:b0:2d8:aa9c:e386 with SMTP id 98e67ed59e1d1-2db9ff90b77mr5493054a91.14.1726183117828; Thu, 12 Sep 2024 16:18:37 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2db6c1ac69asm3157591a91.0.2024.09.12.16.18.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 12 Sep 2024 16:18:37 -0700 (PDT) From: Deepak Gupta To: paul.walmsley@sifive.com, palmer@sifive.com, conor@kernel.org, linux-doc@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kselftest@vger.kernel.org Cc: corbet@lwn.net, palmer@dabbelt.com, aou@eecs.berkeley.edu, robh@kernel.org, krzk+dt@kernel.org, oleg@redhat.com, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, hpa@zytor.com, peterz@infradead.org, akpm@linux-foundation.org, arnd@arndb.de, ebiederm@xmission.com, kees@kernel.org, Liam.Howlett@oracle.com, vbabka@suse.cz, lorenzo.stoakes@oracle.com, shuah@kernel.org, brauner@kernel.org, samuel.holland@sifive.com, debug@rivosinc.com, andy.chiu@sifive.com, jerry.shih@sifive.com, greentime.hu@sifive.com, charlie@rivosinc.com, evan@rivosinc.com, cleger@rivosinc.com, xiao.w.wang@intel.com, ajones@ventanamicro.com, anup@brainfault.org, mchitale@ventanamicro.com, atishp@rivosinc.com, sameo@rivosinc.com, bjorn@rivosinc.com, alexghiti@rivosinc.com, david@redhat.com, libang.li@antgroup.com, jszhang@kernel.org, leobras@redhat.com, guoren@kernel.org, samitolvanen@google.com, songshuaishuai@tinylab.org, costa.shul@redhat.com, bhe@redhat.com, zong.li@sifive.com, puranjay@kernel.org, namcaov@gmail.com, antonb@tenstorrent.com, sorear@fastmail.com, quic_bjorande@quicinc.com, ancientmodern4@gmail.com, ben.dooks@codethink.co.uk, quic_zhonhan@quicinc.com, cuiyunhui@bytedance.com, yang.lee@linux.alibaba.com, ke.zhao@shingroup.cn, sunilvl@ventanamicro.com, tanzhasanwork@gmail.com, schwab@suse.de, dawei.li@shingroup.cn, rppt@kernel.org, willy@infradead.org, usama.anjum@collabora.com, osalvador@suse.de, ryan.roberts@arm.com, andrii@kernel.org, alx@kernel.org, catalin.marinas@arm.com, broonie@kernel.org, revest@chromium.org, bgray@linux.ibm.com, deller@gmx.de, zev@bewilderbeest.net Subject: [PATCH v4 21/30] riscv/traps: Introduce software check exception Date: Thu, 12 Sep 2024 16:16:40 -0700 Message-ID: <20240912231650.3740732-22-debug@rivosinc.com> X-Mailer: git-send-email 2.45.0 In-Reply-To: <20240912231650.3740732-1-debug@rivosinc.com> References: <20240912231650.3740732-1-debug@rivosinc.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 3938CA0003 X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: jc6hk7c1cam1ic8a9us9cgundtxgqc1a X-HE-Tag: 1726183118-970747 X-HE-Meta: U2FsdGVkX1/fg+qT1EF5bNXSF5ujlTDjyBhp6QLdgMXqsBk+VDHnsNftJtL4mWkUhxBWRAF6vSPEWLcJTYUQTDQmURmze2oHvAaKmUXhTXcHsdrtzRZHI0g7YC257G+k4TK25lFrTIeXH6aHL6WpKoyfO/fzEm9qC+DvhJ0uTILdVOICdTV72ZwVrnIrpmMDMnc2GH6WZRzHaAfIxhuKQlsvO8HvLaS1EKkBWOIYOWCAf47sGfQ8eMGSJMtDrG/CYOcVjkdwO6Jshk84TWVtxF6diCU+xDvXukLgSQsFGOPXZAAVpjxQYQx+2ZThyo04hFaCcu//FEANPHU4pco9ukleY9faPnlbUjOmuIigoV2iAaT7f4YaEMr13sz8X9Jo6c9ieLUftQEgrFI+Zxyquh3KDoUbpoxrU2WsPCM8EFeLAhIoPs7Ke3ATHWzhT/fl3gnKYci6Z0Dj7IC3R5G7iMDNvH6klLezIkc2JJpLDWcDHRYU++VvZ6Uuf96sAH0JCBxqwr5pvBS7kXW2GxDjKR+Wx7nr0W4A34nC/9JRaRimFfY6fGOI/M4sAWdZV4ULqJEP/bBdlSEUEkg0RGWi2DvYK78FX0CdloUSzEEftVEDFOypyJ8l9borRO6CWLqGkibeTTtfwpSBvZg3bSJfpWNS5AQGm2/Om7rhux0VMBwfQ45M4vHz14SaFuRYWNfLceazep3tMJ830Z603y2pyZpTSIQ/UC8GHMG/1Zq4qBPIlUJB/afkrlDtZBZ5vp9G05vUKQFkrZxoo7xlHdTdEjogkFfdkkabzoQEAdaOlgdno8k+03sXDEl734Qb6e/U7/FvjqpsIx4EC/Bod75mVclX4GFB70uL6emVKraFnj3gVWz7KPM6DhR02VzqT/I6qwM7TUEmCDvCAu6mCRJ2sQJvBiu62GbeH75QBui+nGYX3k7oTUwDlV1jUQ8mD8on/RrjdtVLpaDhQ950ROV HHdpsnIR /+7g6jhSUc0p1R2Q85d9OPrY1ZtTxxFHDhhQMym91qJsZ/BLXtrI7Ds/QsqqBpjCdrn07fAp7+THEc0UMA3ajexZnhNlGw0Pqpy1BLycaNsRBMW6IkKyAdJLJAZEcXYj6dCJA07iaE7HSeEl9i8YZB0dDpuKoY01lSUdKmXB4xyQLbxYIta4eYL+rJBgvt1yAAI5gdonN3q2TmZKyxyhJFWW5Uc8gkpGIpqnsn6vOOZLtktEYFY1blDIzbVHyQVMRZt/Ofp/9FJ2RN0jV34w+eI1WyGR7VnLjrqqKd330hoYyIEdvLSCWlwT0tpVMvR2Pktc9Evf67oJVDFlB+3TgFn8At+jaWvejkCSxHaYcZ5pr+ZCbkhw98IeFphCQNs7v8OdJknnuRVI10mb234qZmhEfs+0iPWCELwiT4uJhXepT2b107/Tcd5sz5E88dhdMQ9GR 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: zicfiss / zicfilp introduces a new exception to priv isa `software check exception` with cause code = 18. This patch implements software check exception. Additionally it implements a cfi violation handler which checks for code in xtval. If xtval=2, it means that sw check exception happened because of an indirect branch not landing on 4 byte aligned PC or not landing on `lpad` instruction or label value embedded in `lpad` not matching label value setup in `x7`. If xtval=3, it means that sw check exception happened because of mismatch between link register (x1 or x5) and top of shadow stack (on execution of `sspopchk`). In case of cfi violation, SIGSEGV is raised with code=SEGV_CPERR. SEGV_CPERR was introduced by x86 shadow stack patches. Signed-off-by: Deepak Gupta --- arch/riscv/include/asm/asm-prototypes.h | 1 + arch/riscv/include/asm/entry-common.h | 2 ++ arch/riscv/kernel/entry.S | 3 ++ arch/riscv/kernel/traps.c | 38 +++++++++++++++++++++++++ 4 files changed, 44 insertions(+) diff --git a/arch/riscv/include/asm/asm-prototypes.h b/arch/riscv/include/asm/asm-prototypes.h index cd627ec289f1..5a27cefd7805 100644 --- a/arch/riscv/include/asm/asm-prototypes.h +++ b/arch/riscv/include/asm/asm-prototypes.h @@ -51,6 +51,7 @@ DECLARE_DO_ERROR_INFO(do_trap_ecall_u); DECLARE_DO_ERROR_INFO(do_trap_ecall_s); DECLARE_DO_ERROR_INFO(do_trap_ecall_m); DECLARE_DO_ERROR_INFO(do_trap_break); +DECLARE_DO_ERROR_INFO(do_trap_software_check); asmlinkage void handle_bad_stack(struct pt_regs *regs); asmlinkage void do_page_fault(struct pt_regs *regs); diff --git a/arch/riscv/include/asm/entry-common.h b/arch/riscv/include/asm/entry-common.h index 2293e535f865..4068c7e5452a 100644 --- a/arch/riscv/include/asm/entry-common.h +++ b/arch/riscv/include/asm/entry-common.h @@ -39,4 +39,6 @@ static inline int handle_misaligned_store(struct pt_regs *regs) } #endif +bool handle_user_cfi_violation(struct pt_regs *regs); + #endif /* _ASM_RISCV_ENTRY_COMMON_H */ diff --git a/arch/riscv/kernel/entry.S b/arch/riscv/kernel/entry.S index ca9203e6d76d..2ec75ba864a8 100644 --- a/arch/riscv/kernel/entry.S +++ b/arch/riscv/kernel/entry.S @@ -384,6 +384,9 @@ SYM_DATA_START_LOCAL(excp_vect_table) RISCV_PTR do_page_fault /* load page fault */ RISCV_PTR do_trap_unknown RISCV_PTR do_page_fault /* store page fault */ + RISCV_PTR do_trap_unknown /* cause=16 */ + RISCV_PTR do_trap_unknown /* cause=17 */ + RISCV_PTR do_trap_software_check /* cause=18 is sw check exception */ SYM_DATA_END_LABEL(excp_vect_table, SYM_L_LOCAL, excp_vect_table_end) #ifndef CONFIG_MMU diff --git a/arch/riscv/kernel/traps.c b/arch/riscv/kernel/traps.c index 51ebfd23e007..32d1453bed72 100644 --- a/arch/riscv/kernel/traps.c +++ b/arch/riscv/kernel/traps.c @@ -354,6 +354,44 @@ void do_trap_ecall_u(struct pt_regs *regs) } +#define CFI_TVAL_FCFI_CODE 2 +#define CFI_TVAL_BCFI_CODE 3 +/* handle cfi violations */ +bool handle_user_cfi_violation(struct pt_regs *regs) +{ + bool ret = false; + unsigned long tval = csr_read(CSR_TVAL); + + if (((tval == CFI_TVAL_FCFI_CODE) && cpu_supports_indirect_br_lp_instr()) || + ((tval == CFI_TVAL_BCFI_CODE) && cpu_supports_shadow_stack())) { + do_trap_error(regs, SIGSEGV, SEGV_CPERR, regs->epc, + "Oops - control flow violation"); + ret = true; + } + + return ret; +} +/* + * software check exception is defined with risc-v cfi spec. Software check + * exception is raised when:- + * a) An indirect branch doesn't land on 4 byte aligned PC or `lpad` + * instruction or `label` value programmed in `lpad` instr doesn't + * match with value setup in `x7`. reported code in `xtval` is 2. + * b) `sspopchk` instruction finds a mismatch between top of shadow stack (ssp) + * and x1/x5. reported code in `xtval` is 3. + */ +asmlinkage __visible __trap_section void do_trap_software_check(struct pt_regs *regs) +{ + if (user_mode(regs)) { + /* not a cfi violation, then merge into flow of unknown trap handler */ + if (!handle_user_cfi_violation(regs)) + do_trap_unknown(regs); + } else { + /* sw check exception coming from kernel is a bug in kernel */ + die(regs, "Kernel BUG"); + } +} + #ifdef CONFIG_MMU asmlinkage __visible noinstr void do_page_fault(struct pt_regs *regs) { From patchwork Thu Sep 12 23:16:41 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13802845 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 AB5CEEEE271 for ; Thu, 12 Sep 2024 23:18:46 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 41CDD6B00B8; Thu, 12 Sep 2024 19:18:46 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 3A5826B00B9; Thu, 12 Sep 2024 19:18:46 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1A8EB8D0003; Thu, 12 Sep 2024 19:18:46 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id EE9CB6B00B8 for ; Thu, 12 Sep 2024 19:18:45 -0400 (EDT) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id A7AE0120CCE for ; Thu, 12 Sep 2024 23:18:45 +0000 (UTC) X-FDA: 82557653010.20.2BEC944 Received: from mail-pj1-f43.google.com (mail-pj1-f43.google.com [209.85.216.43]) by imf05.hostedemail.com (Postfix) with ESMTP id BC238100002 for ; Thu, 12 Sep 2024 23:18:43 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=LEcJbfYF; spf=pass (imf05.hostedemail.com: domain of debug@rivosinc.com designates 209.85.216.43 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=1726183017; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=+38J+w59/fsg+wZPwkpHjWKQt6YBaFGJt3wtFmX9cIM=; b=c62VK1zBghxgSMdk40c0YIB4bgGz4wkk7sgHHvHJCcZ/+vnFItipVZ3tek7Idi3D5vwEyx AOnYV3FDHrIOGPrjy/gywSnYl6cV2FDIiuYlfMiqRWY3ps+weN6/CW3rVg9CFdxd3ws9cu DN2cAB0Jvs1Deh36fiKMAy9geK7MfQ4= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1726183017; a=rsa-sha256; cv=none; b=XobwDZdlYiwYB1woE7EIv1WM8/3KZo4OdMib2ubh7g6SJ6Q7vRNzOG1fshW36S+UK0Ve9x Fam1eDJdcGJSFASKDLqtlNXhx2kCMyEOPDgCtJJ/4UcthZAtE+ql89To2KoQX3k8M6Pa/g HA1BqmvC75g1nDaWb/rY3mPu7KPsjy4= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=LEcJbfYF; spf=pass (imf05.hostedemail.com: domain of debug@rivosinc.com designates 209.85.216.43 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none Received: by mail-pj1-f43.google.com with SMTP id 98e67ed59e1d1-2d86f71353dso1108634a91.2 for ; Thu, 12 Sep 2024 16:18:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1726183122; x=1726787922; darn=kvack.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=+38J+w59/fsg+wZPwkpHjWKQt6YBaFGJt3wtFmX9cIM=; b=LEcJbfYF23TCwemd2S51vn91guuPGjkucSXxB14VjqZTbzm2JOl3vQ1YtO/ZImhwwT CRZu3JAg4FZcqO/aCn40WoaVAWxCBe42qtXNCd5fNXf+swq94MlrJdIjAdF9LV7+XExc fcC+q6rocpbBs54AKkLR1TeKvzFWRzIM21W+a9EIRG5sdtgjGj8McLIu6z/xPkB7cEio XJQ4OFB77MerYXLXweP+GG28xFMoTDOQnvHPGghjJBgzHj/CMz4uxJt3WSFr+9nikvVn d3+q05IJpw4Zr/EScrWw2e+eYiZwX+RIBCNSm20E+IEXzSxh+urB1nIAvRV4tbgdvKe0 c7Vw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726183122; x=1726787922; 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=+38J+w59/fsg+wZPwkpHjWKQt6YBaFGJt3wtFmX9cIM=; b=H6L97qx+wEe8t5WGysKaLA3S68becaMOWD2Njhzk6D+ihB4tmugtsA2JN9b6khLi8D Xkzcl0OT5+roHrOtjW10gmWVovStl1gGC1SitODKypl+TVGgJPeZmaHWEP/ALLhyI/C7 M9RUbdEyhShHAX36S4YKZkwjdHVsPZcapDcR6xPDslcVOQ1+79Jh0bh1fvZggQjEzJ1V MUlaEStML0QojOh9MFoe7In7JkOsolhe9KD2948vsNMj1LGxC7ExMt8G+rFjbkhlT7ZX 2SvOmRGNlsNz7fj1GJBB0KuzA8BGRgCgFt6S7She/lwxRr9F4T4M6koOiUxVIFHU2icK 8Rlw== X-Forwarded-Encrypted: i=1; AJvYcCWsVFa36c5ApXXSejnzT8RT25GR3AXGJ/NuV5s/hzL78U3hBKLAWCjb3XwmXBnKR2m1L8O9E6riig==@kvack.org X-Gm-Message-State: AOJu0Yx7OB0wo/6bSuqslDrivzsVCGAPkEwg12BCtfklxpkeOuhUFkm4 9h4tgv8xJVp4+jqDgavYZQmrMok65oQnP2P2SjdOHnOkCX+Kp5sDuunWByG6KDM= X-Google-Smtp-Source: AGHT+IHfT/S6FiX6u36xFuQAdjiVXSnBTlg8zgoeNqcnzuIIMjt0legeBCv+aVmkm3X/I1OJo91O6Q== X-Received: by 2002:a17:90a:4b44:b0:2da:9115:15ce with SMTP id 98e67ed59e1d1-2db9ff94036mr4324100a91.15.1726183122337; Thu, 12 Sep 2024 16:18:42 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2db6c1ac69asm3157591a91.0.2024.09.12.16.18.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 12 Sep 2024 16:18:42 -0700 (PDT) From: Deepak Gupta To: paul.walmsley@sifive.com, palmer@sifive.com, conor@kernel.org, linux-doc@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kselftest@vger.kernel.org Cc: corbet@lwn.net, palmer@dabbelt.com, aou@eecs.berkeley.edu, robh@kernel.org, krzk+dt@kernel.org, oleg@redhat.com, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, hpa@zytor.com, peterz@infradead.org, akpm@linux-foundation.org, arnd@arndb.de, ebiederm@xmission.com, kees@kernel.org, Liam.Howlett@oracle.com, vbabka@suse.cz, lorenzo.stoakes@oracle.com, shuah@kernel.org, brauner@kernel.org, samuel.holland@sifive.com, debug@rivosinc.com, andy.chiu@sifive.com, jerry.shih@sifive.com, greentime.hu@sifive.com, charlie@rivosinc.com, evan@rivosinc.com, cleger@rivosinc.com, xiao.w.wang@intel.com, ajones@ventanamicro.com, anup@brainfault.org, mchitale@ventanamicro.com, atishp@rivosinc.com, sameo@rivosinc.com, bjorn@rivosinc.com, alexghiti@rivosinc.com, david@redhat.com, libang.li@antgroup.com, jszhang@kernel.org, leobras@redhat.com, guoren@kernel.org, samitolvanen@google.com, songshuaishuai@tinylab.org, costa.shul@redhat.com, bhe@redhat.com, zong.li@sifive.com, puranjay@kernel.org, namcaov@gmail.com, antonb@tenstorrent.com, sorear@fastmail.com, quic_bjorande@quicinc.com, ancientmodern4@gmail.com, ben.dooks@codethink.co.uk, quic_zhonhan@quicinc.com, cuiyunhui@bytedance.com, yang.lee@linux.alibaba.com, ke.zhao@shingroup.cn, sunilvl@ventanamicro.com, tanzhasanwork@gmail.com, schwab@suse.de, dawei.li@shingroup.cn, rppt@kernel.org, willy@infradead.org, usama.anjum@collabora.com, osalvador@suse.de, ryan.roberts@arm.com, andrii@kernel.org, alx@kernel.org, catalin.marinas@arm.com, broonie@kernel.org, revest@chromium.org, bgray@linux.ibm.com, deller@gmx.de, zev@bewilderbeest.net Subject: [PATCH v4 22/30] riscv sigcontext: cfi state struct definition for sigcontext Date: Thu, 12 Sep 2024 16:16:41 -0700 Message-ID: <20240912231650.3740732-23-debug@rivosinc.com> X-Mailer: git-send-email 2.45.0 In-Reply-To: <20240912231650.3740732-1-debug@rivosinc.com> References: <20240912231650.3740732-1-debug@rivosinc.com> MIME-Version: 1.0 X-Stat-Signature: 9fekpc7cqfrxno7oyrutaemfpe6oia3y X-Rspamd-Queue-Id: BC238100002 X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1726183123-965896 X-HE-Meta: U2FsdGVkX18bmG69LK+b35UoSkl7qWCk/HXUaHz3KZI/vh7cKSF/nBh18jSTXsTKIG4Hw1lBTSikT4C5pmtudvlQIUIUT3Nge1MLl9rUqoVg0BtixtxHhsCH85yi2PTbAox8BKr3W6fkfH01Ih+QB1AaXeMZCPKA3OFyTWFylrnkZ/UWVr1nCykb6AyjA3oElM8wyRmldTq1R2us0RQiW/dSZ2GwXcs7ZAugMdfOKMpjGUgjBvH54xwPs6gfwPkK0ML4awNNYso6FN9IOlxiS3432LcqLg7bBrT3y62bcf2LRPUnlemsLDO240TTotpqpNJbvgL8B8XJIbjzcpJVLVeHzRW9goI2xK+UQQxU60WvN4ZUfQUiN7OMBgk3/5osGVuMTj8GSZsnRHSCMIwnuReR8nJyilYnQyK+cYTzOiStxMA+VDcr/MXjUEOa67WoEdNIluOIJWIdpB4ytzCWMgrjzZCnFV74eXA35hpAg9GFMSLyufQlvskKBtQt0e8gDDrQlSmcFYzAGS3uSQoRS0OKcI6M1JEJWdtaC2FqN3MAwlbvL7DTN+VqtD/khd73VBHcmlaHSAHjLbnW8dqukTIisRcMAD6YK/w2YJ9b92q1k2SevHH8NO+gbWzFwfP8Cd6KkBLfQ9daUxjwuVPQ0Qt2Gip7NSYFxjlnkXEVKWoczV+8HLCGKqv28Mqun4apOizfROjjTDMvQgb3GHrytgdc8o6EhMSAhDTXRNM1YO0U+dG19LOxM1As+5JPCtHstA1JvLLLTzuO0ijhqo2cwHQg4/Y3HZUFGsh/118GjMf3IoQHCQJjAflsw6arPjMMzwjvK15/wcsVZdkFYAVestLQRx+rbmnwi087ryndLBklm0AKxTs6knm88O9xEthiJf/ftlNMRGQBZ6Kn6ILVbJEgTIzzxxK8dudgKzLQdfAu3scUsAk8lc+ngcPXxyH5RYeIIQ5GKsjqn3Vs8bX nc/LHLbS LKlAgZFchuIspsmxR0IoWNoWFY/Lg6paAiVkUGsBmlKHfrbqYxCC8bfLiUT2nhfRA0DvbS0UAYckiNDKcYSsUsgRLTcqeNqfgzzOH32Xg8XCVCShCyLIXPDTWYIauiEmn2b9zf/JXan46Co5kJMvcDFeogU2u+pZT2I+sAaVMMWgkb8nqYKe02Z4fLB+MnYGX+aedzMv5oo6L+LLZZIPG/aI0MIhjUrsF8jn8paPMfWt40EgzRuJ6+dk9KtoNFWTOTClxYvkJEHgAXL6glgS2eP/r2KxYS4QIcFQWsD522EqYOfgDwWciOKjuFIyZRkzeBEK/rSgc5iViFW3aO4o3aupK8W/2/cxvelAA5VVFjUTp0+/l/dDqfhDJquYeITKUTq0TvgGwOm1ASmnHQuBDKf4XnU2+LHuUbS3Qmsusvkm1sLI8f6yOd53/0pHBmRaqHCz2 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000839, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Shadow stack needs to be saved and restored on signal delivery and signal return. sigcontext embedded in ucontext is extendible. Defining cfi state in there which can be used to save cfi state before signal delivery and restore cfi state on sigreturn Signed-off-by: Deepak Gupta --- arch/riscv/include/uapi/asm/sigcontext.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/arch/riscv/include/uapi/asm/sigcontext.h b/arch/riscv/include/uapi/asm/sigcontext.h index cd4f175dc837..c4d19a10651d 100644 --- a/arch/riscv/include/uapi/asm/sigcontext.h +++ b/arch/riscv/include/uapi/asm/sigcontext.h @@ -21,6 +21,9 @@ struct __sc_riscv_v_state { struct __riscv_v_ext_state v_state; } __attribute__((aligned(16))); +struct __sc_riscv_cfi_state { + unsigned long ss_ptr; /* shadow stack pointer */ +}; /* * Signal context structure * From patchwork Thu Sep 12 23:16:42 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13802846 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 2F499EEE271 for ; Thu, 12 Sep 2024 23:18:51 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id AD4116B00B9; Thu, 12 Sep 2024 19:18:50 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id A82036B00BA; Thu, 12 Sep 2024 19:18:50 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 887FC6B00BB; Thu, 12 Sep 2024 19:18:50 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 691916B00B9 for ; Thu, 12 Sep 2024 19:18:50 -0400 (EDT) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 20FB91A0D45 for ; Thu, 12 Sep 2024 23:18:50 +0000 (UTC) X-FDA: 82557653220.03.9C11F3F Received: from mail-pg1-f176.google.com (mail-pg1-f176.google.com [209.85.215.176]) by imf10.hostedemail.com (Postfix) with ESMTP id 3778CC0002 for ; Thu, 12 Sep 2024 23:18:47 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=cE+rVR06; spf=pass (imf10.hostedemail.com: domain of debug@rivosinc.com designates 209.85.215.176 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=1726183075; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=QuNLZoEwAoxFOzNtpFEIpmCklLXVvlyHizGcnsawnko=; b=aoEjgMJ9PyfHZMzA6d0wFJf8LTDiUWH6jYCyS1Tt9GO9W2n4R4Sdv5mQmH5dXbnx2QHSrR YgU0AxX0UVfb88j+nV+wAf76yAu0S0kSXKcZAF7ZzJl5NLvFxZNDW6I0aJQ/cdgMYpEV8d BsbPSTwyF75ED7L2F90VCgPuWF2sf+w= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=cE+rVR06; spf=pass (imf10.hostedemail.com: domain of debug@rivosinc.com designates 209.85.215.176 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1726183075; a=rsa-sha256; cv=none; b=DX71E8XceJ+o8HjrhhlmRxnfR9Z0gaPnNhzIrdbCCccAsIrH6LdNSZ2ipUI3PXWP1dFNtr 9I34emWbDnOBW+DTUJshdPPirDbQumZ2NB5KZHS4UzvCX+zY65+blLN0SSLHAxZpl/0Ihu k719lY+2/4Xi6OeTP63mp38m9a0K71I= Received: by mail-pg1-f176.google.com with SMTP id 41be03b00d2f7-7db2e3fa438so231057a12.1 for ; Thu, 12 Sep 2024 16:18:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1726183127; x=1726787927; darn=kvack.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=QuNLZoEwAoxFOzNtpFEIpmCklLXVvlyHizGcnsawnko=; b=cE+rVR06xQYD87RkDxKvMZb+FvrlT2FAMyDMnAkw9fOxk786klpMOChRfhR3L0z1Ua kx2fzKDWUCgzt9hnONVomYnGBhARuUscySz/FhDTtketNPCbvMP+bGt6JHnwl3DqcKDT XKpp2XTRk/M011HpChohX4oBfYFHUy22w88rBh85YqMhL+jE2l8cQu9LUMriC4ADkSGE RqhctF2Tmjy90JW9rr6MgF/lZyTpGzqNPZyz+xNr/IucFnLMCGB9rE98f8aum5052/70 10jYZk3ZeAJOELcaTI1sRMDC+9wZ2hLM+QH01KK8HzTknkWR1YtDOd5/KpyAiKbfhlIY 2GMA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726183127; x=1726787927; 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=QuNLZoEwAoxFOzNtpFEIpmCklLXVvlyHizGcnsawnko=; b=ZFU4Y6sEw/Lt9ZKJBCzl/GSUCwXghnv/wv1brp4MyPY75KhTnaxmjD0rDwGBmohFc6 6naS1xFRHYoquBLC7HDe2CS7hZf1ZSpzsQprB48Yo4fxbzFSPkD3JOFLs/V7zW0Coavc isokZOXR83TBA6p8vzU/BbJGOebTdJsr3nAPd0VJDwu/jO21+sLa0J/nhqmOQQMaiZW+ 3z5HLiRZ/Vx8E5ct4rfGH1R56jEwJoO9Ai6F6uWlxdqlfrtJf0JdgH+UbRxGjOt8GBPW z3ilFPZgSZ7Izk/nTLo05u71ciRyQIWzITJYpkV93Spsw0EMPXavB4doOUVd5QAptEeF eidA== X-Forwarded-Encrypted: i=1; AJvYcCV6k9oKfUuuVU87LmfLFMh++J/DQ+TeHx72cUItLKB6Cuf0oxhQvDr19j2Mm1CqP3CNnl6zbOJt3A==@kvack.org X-Gm-Message-State: AOJu0YwB5MhW5ZoDdhc9cximhsTscUW69/V+IKz6LX8OA/6GOBDquO7t JA/QtBv1xVYICIOgsEszleT3Jy/mgqSuAtVpbUWkWq3+tZWv2Wc7p0T7YH4qKY8= X-Google-Smtp-Source: AGHT+IFe2b+wfH4HddVQdWnBa2Cv1PojGiau8/JID+xbbETXDST/X3AAL06qD2zWAK34gSRESgSPDg== X-Received: by 2002:a17:90a:4b82:b0:2c9:8105:483 with SMTP id 98e67ed59e1d1-2db9ffca8a1mr4592061a91.14.1726183126845; Thu, 12 Sep 2024 16:18:46 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2db6c1ac69asm3157591a91.0.2024.09.12.16.18.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 12 Sep 2024 16:18:46 -0700 (PDT) From: Deepak Gupta To: paul.walmsley@sifive.com, palmer@sifive.com, conor@kernel.org, linux-doc@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kselftest@vger.kernel.org Cc: corbet@lwn.net, palmer@dabbelt.com, aou@eecs.berkeley.edu, robh@kernel.org, krzk+dt@kernel.org, oleg@redhat.com, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, hpa@zytor.com, peterz@infradead.org, akpm@linux-foundation.org, arnd@arndb.de, ebiederm@xmission.com, kees@kernel.org, Liam.Howlett@oracle.com, vbabka@suse.cz, lorenzo.stoakes@oracle.com, shuah@kernel.org, brauner@kernel.org, samuel.holland@sifive.com, debug@rivosinc.com, andy.chiu@sifive.com, jerry.shih@sifive.com, greentime.hu@sifive.com, charlie@rivosinc.com, evan@rivosinc.com, cleger@rivosinc.com, xiao.w.wang@intel.com, ajones@ventanamicro.com, anup@brainfault.org, mchitale@ventanamicro.com, atishp@rivosinc.com, sameo@rivosinc.com, bjorn@rivosinc.com, alexghiti@rivosinc.com, david@redhat.com, libang.li@antgroup.com, jszhang@kernel.org, leobras@redhat.com, guoren@kernel.org, samitolvanen@google.com, songshuaishuai@tinylab.org, costa.shul@redhat.com, bhe@redhat.com, zong.li@sifive.com, puranjay@kernel.org, namcaov@gmail.com, antonb@tenstorrent.com, sorear@fastmail.com, quic_bjorande@quicinc.com, ancientmodern4@gmail.com, ben.dooks@codethink.co.uk, quic_zhonhan@quicinc.com, cuiyunhui@bytedance.com, yang.lee@linux.alibaba.com, ke.zhao@shingroup.cn, sunilvl@ventanamicro.com, tanzhasanwork@gmail.com, schwab@suse.de, dawei.li@shingroup.cn, rppt@kernel.org, willy@infradead.org, usama.anjum@collabora.com, osalvador@suse.de, ryan.roberts@arm.com, andrii@kernel.org, alx@kernel.org, catalin.marinas@arm.com, broonie@kernel.org, revest@chromium.org, bgray@linux.ibm.com, deller@gmx.de, zev@bewilderbeest.net Subject: [PATCH v4 23/30] riscv signal: save and restore of shadow stack for signal Date: Thu, 12 Sep 2024 16:16:42 -0700 Message-ID: <20240912231650.3740732-24-debug@rivosinc.com> X-Mailer: git-send-email 2.45.0 In-Reply-To: <20240912231650.3740732-1-debug@rivosinc.com> References: <20240912231650.3740732-1-debug@rivosinc.com> MIME-Version: 1.0 X-Rspamd-Server: rspam03 X-Rspam-User: X-Rspamd-Queue-Id: 3778CC0002 X-Stat-Signature: f3gddrb7cuz1cyhbzo5dyyddfu9fowps X-HE-Tag: 1726183127-363915 X-HE-Meta: U2FsdGVkX1+l4Agzh4w6fxTuBUx3lBQKuCDY7sDjJQi2ciRu2PSRP53dSizKFhiAGV/s6Zga8I53gJESrBvjAbTkQnp8SBgEaFCDvFx9eSUsFDRcSc5zzwSieGc0KQfLCsfNLMSXk9jaN8/crL+BLiaAI+2p8Tj3v8sgwhUtp+iAoCIKj/ZA4k2nPr/G6D9H61f+K3wbPfcwwwPpXblO+9LH5948GRGfSUQpNg6yiVAvg45VMMHzQ1Uvezrsj5yYG2RjtjZZK2H25QqPlLUlzuSgkTPHe7JabBXe38YC5le2utbgjkpbTdUHHK7pPCJIC+3VJkym/WfG+VQrvBHnMSnwknZL9Acwkhsmb9h75Y643dqaDZnUQ8yAB/GaT1NrF/RKPpSF9f+MEQLiFpT4hfnKvMZwqHQeXgk3hGwU6JLny6jQrEjiknoLYGU+YmeNwkQt3e8wwK2Kk1aiA4uW9nnTvRfZQSgcY403Q9np9FwX3ADl4Sdascock20aLiI3mgf5pBIvCaLeQXF4pMXe3gYWeNoNwVKgvyQnoHo8T5d8euOj+mz7B1U+IsiSgEJiuEu3CgPlJRStSMK5PbVVJgkjQVyGLwAobd6HngRkGMGN1hky54lBe6+Nv0GEHs2l70zC0A7ls4VSATxSUoz60NChgYg7DGOCQBMC6GmMGss9zsGkMfoX2lTHwGR3p69V4haFL27I1rpoq8NI96OLQmQXb3GrYL158FezvlEUp2R/9Bd32GxbPOEbZTzKDQCvwzdJFA9doQqykNkb7UYWKN65peZV5ftk5ysmEVoWHywPUEogUknd2lAkpzVt2fqNB7MUncHFBvtrXPGP0TQclbCs/mM2d3uJAYlAztEEOj0I980tczSo7ttxMK0YSj50m5YKRyvWOJSD+7U0HVrvN2sWM22jqSpnvTzP7birTNlX7OKXY4DozV8Akk76JwzDf1nO9gR9FNqCoxKKPJF Hp4b5GPX mCVu4/2P+DxYZjGkJucZlhCeElH5x4Jqh4/v2Hf/+MND3WiHmgDtAY54aGmZmA9lOe4obJekV/CUopBUxIh3R9Wjw8d9lkZ9EfgcyEfa28ZJ+fwC9xdSZGyVcCNCAUDfu7Qt6CaoYwwPMuGLmouGxcHXg+0dszFdAg36+GSUKeqeJYueHN+x2t3h7ZEQUGob4lBklV3vXPREr+bkqlJ80AqwKQmmeFpugPuyTOhbtcRvlSQgFo7m4ql9v3elePrDpfitZPsubEubMJRJlrgT715npvTqjg3Wyi4YDayC6JwxHB4F7BhQVEHNtcHAyMIFpR+FzjyINxpvwPqj/0yJfiVLSJxCk0sish8KtYS6QN2IsXfXJgx+AKAkMTKgx1i0AZyqLRnh/nC0mFd8S2Cy7/Li+9O2cVmLA4+RzDzx7mPQbyuOoS2bd9orCC/ZkWQrv3TsAolQa1iiaz+itUXpERWNiF+GUSumjOp2MZtCxvxC4KZU5zuPUepCHiqSXWhdfILX7 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: Save shadow stack pointer in sigcontext structure while delivering signal. Restore shadow stack pointer from sigcontext on sigreturn. As part of save operation, kernel uses `ssamoswap` to save snapshot of current shadow stack on shadow stack itself (can be called as a save token). During restore on sigreturn, kernel retrieves token from top of shadow stack and validates it. This allows that user mode can't arbitrary pivot to any shadow stack address without having a token and thus provide strong security assurance between signaly delivery and sigreturn window. Signed-off-by: Deepak Gupta Suggested-by: Andy Chiu --- arch/riscv/include/asm/usercfi.h | 19 ++++++++++ arch/riscv/kernel/signal.c | 62 +++++++++++++++++++++++++++++++- arch/riscv/kernel/usercfi.c | 57 +++++++++++++++++++++++++++++ 3 files changed, 137 insertions(+), 1 deletion(-) diff --git a/arch/riscv/include/asm/usercfi.h b/arch/riscv/include/asm/usercfi.h index 20a9102cce51..d5050a5df26c 100644 --- a/arch/riscv/include/asm/usercfi.h +++ b/arch/riscv/include/asm/usercfi.h @@ -8,6 +8,7 @@ #ifndef __ASSEMBLY__ #include #include +#include struct task_struct; struct kernel_clone_args; @@ -35,6 +36,9 @@ bool is_shstk_locked(struct task_struct *task); bool is_shstk_allocated(struct task_struct *task); void set_shstk_lock(struct task_struct *task); void set_shstk_status(struct task_struct *task, bool enable); +unsigned long get_active_shstk(struct task_struct *task); +int restore_user_shstk(struct task_struct *tsk, unsigned long shstk_ptr); +int save_user_shstk(struct task_struct *tsk, unsigned long *saved_shstk_ptr); bool is_indir_lp_enabled(struct task_struct *task); bool is_indir_lp_locked(struct task_struct *task); void set_indir_lp_status(struct task_struct *task, bool enable); @@ -96,6 +100,21 @@ static inline void set_shstk_status(struct task_struct *task, bool enable) } +static inline int restore_user_shstk(struct task_struct *tsk, unsigned long shstk_ptr) +{ + return -EINVAL; +} + +static inline int save_user_shstk(struct task_struct *tsk, unsigned long *saved_shstk_ptr) +{ + return -EINVAL; +} + +static inline unsigned long get_active_shstk(struct task_struct *task) +{ + return 0; +} + static inline bool is_indir_lp_enabled(struct task_struct *task) { return false; diff --git a/arch/riscv/kernel/signal.c b/arch/riscv/kernel/signal.c index dcd282419456..7d5c1825650f 100644 --- a/arch/riscv/kernel/signal.c +++ b/arch/riscv/kernel/signal.c @@ -22,6 +22,7 @@ #include #include #include +#include unsigned long signal_minsigstksz __ro_after_init; @@ -153,6 +154,16 @@ static long restore_sigcontext(struct pt_regs *regs, void __user *sc_ext_ptr = &sc->sc_extdesc.hdr; __u32 rsvd; long err; + unsigned long ss_ptr = 0; + struct __sc_riscv_cfi_state __user *sc_cfi = NULL; + + sc_cfi = (struct __sc_riscv_cfi_state *) + ((unsigned long) sc_ext_ptr + sizeof(struct __riscv_ctx_hdr)); + + if (has_vector() && riscv_v_vstate_query(regs)) + sc_cfi = (struct __sc_riscv_cfi_state *) + ((unsigned long) sc_cfi + riscv_v_sc_size); + /* sc_regs is structured the same as the start of pt_regs */ err = __copy_from_user(regs, &sc->sc_regs, sizeof(sc->sc_regs)); if (unlikely(err)) @@ -172,6 +183,24 @@ static long restore_sigcontext(struct pt_regs *regs, if (unlikely(rsvd)) return -EINVAL; + /* + * Restore shadow stack as a form of token stored on shadow stack itself as a safe + * way to restore. + * A token on shadow gives following properties + * - Safe save and restore for shadow stack switching. Any save of shadow stack + * must have had saved a token on shadow stack. Similarly any restore of shadow + * stack must check the token before restore. Since writing to shadow stack with + * address of shadow stack itself is not easily allowed. A restore without a save + * is quite difficult for an attacker to perform. + * - A natural break. A token in shadow stack provides a natural break in shadow stack + * So a single linear range can be bucketed into different shadow stack segments. + * sspopchk will detect the condition and fault to kernel as sw check exception. + */ + if (is_shstk_enabled(current)) { + err |= __copy_from_user(&ss_ptr, &sc_cfi->ss_ptr, sizeof(unsigned long)); + err |= restore_user_shstk(current, ss_ptr); + } + while (!err) { __u32 magic, size; struct __riscv_ctx_hdr __user *head = sc_ext_ptr; @@ -215,6 +244,10 @@ static size_t get_rt_frame_size(bool cal_all) if (cal_all || riscv_v_vstate_query(task_pt_regs(current))) total_context_size += riscv_v_sc_size; } + + if (is_shstk_enabled(current)) + total_context_size += sizeof(struct __sc_riscv_cfi_state); + /* * Preserved a __riscv_ctx_hdr for END signal context header if an * extension uses __riscv_extra_ext_header @@ -276,18 +309,40 @@ static long setup_sigcontext(struct rt_sigframe __user *frame, { struct sigcontext __user *sc = &frame->uc.uc_mcontext; struct __riscv_ctx_hdr __user *sc_ext_ptr = &sc->sc_extdesc.hdr; + unsigned long ss_ptr = 0; + struct __sc_riscv_cfi_state __user *sc_cfi = NULL; long err; + sc_cfi = (struct __sc_riscv_cfi_state *) (sc_ext_ptr + 1); + /* sc_regs is structured the same as the start of pt_regs */ err = __copy_to_user(&sc->sc_regs, regs, sizeof(sc->sc_regs)); /* Save the floating-point state. */ if (has_fpu()) err |= save_fp_state(regs, &sc->sc_fpregs); /* Save the vector state. */ - if (has_vector() && riscv_v_vstate_query(regs)) + if (has_vector() && riscv_v_vstate_query(regs)) { err |= save_v_state(regs, (void __user **)&sc_ext_ptr); + sc_cfi = (struct __sc_riscv_cfi_state *) ((unsigned long) sc_cfi + riscv_v_sc_size); + } /* Write zero to fp-reserved space and check it on restore_sigcontext */ err |= __put_user(0, &sc->sc_extdesc.reserved); + /* + * Save a pointer to shadow stack itself on shadow stack as a form of token. + * A token on shadow gives following properties + * - Safe save and restore for shadow stack switching. Any save of shadow stack + * must have had saved a token on shadow stack. Similarly any restore of shadow + * stack must check the token before restore. Since writing to shadow stack with + * address of shadow stack itself is not easily allowed. A restore without a save + * is quite difficult for an attacker to perform. + * - A natural break. A token in shadow stack provides a natural break in shadow stack + * So a single linear range can be bucketed into different shadow stack segments. Any + * sspopchk will detect the condition and fault to kernel as sw check exception. + */ + if (is_shstk_enabled(current)) { + err |= save_user_shstk(current, &ss_ptr); + err |= __put_user(ss_ptr, &sc_cfi->ss_ptr); + } /* And put END __riscv_ctx_hdr at the end. */ err |= __put_user(END_MAGIC, &sc_ext_ptr->magic); err |= __put_user(END_HDR_SIZE, &sc_ext_ptr->size); @@ -345,6 +400,11 @@ static int setup_rt_frame(struct ksignal *ksig, sigset_t *set, #ifdef CONFIG_MMU regs->ra = (unsigned long)VDSO_SYMBOL( current->mm->context.vdso, rt_sigreturn); + + /* if bcfi is enabled x1 (ra) and x5 (t0) must match. not sure if we need this? */ + if (is_shstk_enabled(current)) + regs->t0 = regs->ra; + #else /* * For the nommu case we don't have a VDSO. Instead we push two diff --git a/arch/riscv/kernel/usercfi.c b/arch/riscv/kernel/usercfi.c index 8da509afdbe9..40c32258b6ec 100644 --- a/arch/riscv/kernel/usercfi.c +++ b/arch/riscv/kernel/usercfi.c @@ -52,6 +52,11 @@ void set_active_shstk(struct task_struct *task, unsigned long shstk_addr) task->thread_info.user_cfi_state.user_shdw_stk = shstk_addr; } +unsigned long get_active_shstk(struct task_struct *task) +{ + return task->thread_info.user_cfi_state.user_shdw_stk; +} + void set_shstk_status(struct task_struct *task, bool enable) { task->thread_info.user_cfi_state.ubcfi_en = enable ? 1 : 0; @@ -164,6 +169,58 @@ static int create_rstor_token(unsigned long ssp, unsigned long *token_addr) return 0; } +/* + * Save user shadow stack pointer on shadow stack itself and return pointer to saved location + * returns -EFAULT if operation was unsuccessful + */ +int save_user_shstk(struct task_struct *tsk, unsigned long *saved_shstk_ptr) +{ + unsigned long ss_ptr = 0; + unsigned long token_loc = 0; + int ret = 0; + + if (saved_shstk_ptr == NULL) + return -EINVAL; + + ss_ptr = get_active_shstk(tsk); + ret = create_rstor_token(ss_ptr, &token_loc); + + if (!ret) { + *saved_shstk_ptr = token_loc; + set_active_shstk(tsk, token_loc); + } + + return ret; +} + +/* + * Restores user shadow stack pointer from token on shadow stack for task `tsk` + * returns -EFAULT if operation was unsuccessful + */ +int restore_user_shstk(struct task_struct *tsk, unsigned long shstk_ptr) +{ + unsigned long token = 0; + + token = amo_user_shstk((unsigned long __user *)shstk_ptr, 0); + + if (token == -1) + return -EFAULT; + + /* invalid token, return EINVAL */ + if ((token - shstk_ptr) != SHSTK_ENTRY_SIZE) { + pr_info_ratelimited( + "%s[%d]: bad restore token in %s: pc=%p sp=%p, token=%p, shstk_ptr=%p\n", + tsk->comm, task_pid_nr(tsk), __func__, + (void *)(task_pt_regs(tsk)->epc), (void *)(task_pt_regs(tsk)->sp), + (void *)token, (void *)shstk_ptr); + return -EINVAL; + } + + /* all checks passed, set active shstk and return success */ + set_active_shstk(tsk, token); + return 0; +} + static unsigned long allocate_shadow_stack(unsigned long addr, unsigned long size, unsigned long token_offset, bool set_tok) From patchwork Thu Sep 12 23:16:43 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13802847 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 5FDFBEEE270 for ; Thu, 12 Sep 2024 23:18:55 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DE0D26B00BA; Thu, 12 Sep 2024 19:18:54 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D90A06B00BC; Thu, 12 Sep 2024 19:18:54 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B94736B00BD; Thu, 12 Sep 2024 19:18:54 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 9A5286B00BA for ; Thu, 12 Sep 2024 19:18:54 -0400 (EDT) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 5AC8EA0E2A for ; Thu, 12 Sep 2024 23:18:54 +0000 (UTC) X-FDA: 82557653388.13.F10FC87 Received: from mail-pj1-f54.google.com (mail-pj1-f54.google.com [209.85.216.54]) by imf06.hostedemail.com (Postfix) with ESMTP id 7AB31180014 for ; Thu, 12 Sep 2024 23:18:52 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=Gg9tqVvP; spf=pass (imf06.hostedemail.com: domain of debug@rivosinc.com designates 209.85.216.54 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=1726183103; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=vRpTmBOo4f9wGNdjTiRRTPN5pl3eVu/w/Wzy47xL6pg=; b=PP2nEON3lwai6Z0oYAVeoUNSWOf61fwx/NETalBj8ErdOGS6K8Fezmu4t+OolUl7v0oNZ1 xEa1MQcpk6oFGcDgMv1myiKg6tuj4x7A7RmufNcSwNMV0Rk9ZhihY6fDnfh+Yzf06M3kvV LzjBeuGHXOrPa0gGu0tktogmUeG+pnI= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=Gg9tqVvP; spf=pass (imf06.hostedemail.com: domain of debug@rivosinc.com designates 209.85.216.54 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1726183103; a=rsa-sha256; cv=none; b=AqOmWZYfWeKKLc8On0oJlkvR9t5rfsVWO7et0dIomM2UmTZKrY1qSHuV/xnwcAWDXyh51M 3YFO1fMsiCEYqVWx3nEgtkWbALzZQb1xwaemSb3RBBo+ZVWshSZOwj9USa51I56XUFwWVb 1SsZCd8URrbmZpTYjMBvAjWTI9l02Ck= Received: by mail-pj1-f54.google.com with SMTP id 98e67ed59e1d1-2d873dc644dso1259975a91.3 for ; Thu, 12 Sep 2024 16:18:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1726183131; x=1726787931; darn=kvack.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=vRpTmBOo4f9wGNdjTiRRTPN5pl3eVu/w/Wzy47xL6pg=; b=Gg9tqVvPsHnJDtR8mmJBTDuMYB207SlK9UaptVAWsxHXc0mh90BYccVpFfhagRdaN+ V8zzD17V2wDuEAzP7jFdCIpG7L5O7Y8vZPxgUU8ZYuD1LvJHPvOqDNNiBIn/bg/hiqCa IJTQGjYRW42+8TEN/Kxut9iXX0IFTsvHOM8a9t2nyQlW/Qe2SVYv4orVi9u+hkz1NwBn CXZU9qBr2XXR5R09YqOcfL3oJSi/C1mOTyD5NENAsNk5qtzsBE2Uqfs+Fcr6yeMF5s65 E0H7OhY9X1CfAfHvAZEuSmnBz9siQQIsRmGQsTVVwRT0aU1IpxmNvHBEVGgQfqQtg7cP eWBA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726183131; x=1726787931; 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=vRpTmBOo4f9wGNdjTiRRTPN5pl3eVu/w/Wzy47xL6pg=; b=Me3P5ReaCx9iowAJYfOLMPoNlvuzBuuBp1an1I1fnqQJo6Q18X3FjN6pnKmEEOdvZY Yah/7w9saM6/9O8sJCHdzQmNzTc28MMVPabO5AEprVo9vdytuFYwl69jLf4RVg2iUg5c aYUHwcK+J9SBnZwcxTUgvUM8RT2iB1wNOidESuRYGRIJXEhRJCmF3zmENPsBcYLmGPk7 S4gKjP2WeeizbiMCTyucjY2C8K6EINS8WzEyxqU1zz76X3Vvrj+OdeLBD/VvCG8tLlZG S8Gwl+jd12eVBK3Go3KRFjMppgwSOW00NVvzs6afxCv1/sSQ7vhQkh42+mxGFqjcDc+i uIxw== X-Forwarded-Encrypted: i=1; AJvYcCUtTzFVVkOskgJsObKvRKtD9MnddoCkctrxvyq9UhB75RmNFKJnxJ4arvmlW0dkH3+E8Ch5AwCcSA==@kvack.org X-Gm-Message-State: AOJu0YziFm03Wa4qU+iyEQybm/Q2broKzO2/wXsQr1+k47TtB5MDADqp CoPR9FVUDXIa2qRe/4UapGMDd4B66mU5G/pwq/q0XH88sohBfz7QQzih0NhlX/w= X-Google-Smtp-Source: AGHT+IGSBIHvNtnBIsR7GeLeQfebNSrrsHlC0GHK9qkrAjtXKK+cIqA47d5wbXfoZYS+dksrkXz4mA== X-Received: by 2002:a17:90a:1641:b0:2d3:bc5e:8452 with SMTP id 98e67ed59e1d1-2dba00681b2mr5466829a91.32.1726183131166; Thu, 12 Sep 2024 16:18:51 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2db6c1ac69asm3157591a91.0.2024.09.12.16.18.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 12 Sep 2024 16:18:50 -0700 (PDT) From: Deepak Gupta To: paul.walmsley@sifive.com, palmer@sifive.com, conor@kernel.org, linux-doc@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kselftest@vger.kernel.org Cc: corbet@lwn.net, palmer@dabbelt.com, aou@eecs.berkeley.edu, robh@kernel.org, krzk+dt@kernel.org, oleg@redhat.com, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, hpa@zytor.com, peterz@infradead.org, akpm@linux-foundation.org, arnd@arndb.de, ebiederm@xmission.com, kees@kernel.org, Liam.Howlett@oracle.com, vbabka@suse.cz, lorenzo.stoakes@oracle.com, shuah@kernel.org, brauner@kernel.org, samuel.holland@sifive.com, debug@rivosinc.com, andy.chiu@sifive.com, jerry.shih@sifive.com, greentime.hu@sifive.com, charlie@rivosinc.com, evan@rivosinc.com, cleger@rivosinc.com, xiao.w.wang@intel.com, ajones@ventanamicro.com, anup@brainfault.org, mchitale@ventanamicro.com, atishp@rivosinc.com, sameo@rivosinc.com, bjorn@rivosinc.com, alexghiti@rivosinc.com, david@redhat.com, libang.li@antgroup.com, jszhang@kernel.org, leobras@redhat.com, guoren@kernel.org, samitolvanen@google.com, songshuaishuai@tinylab.org, costa.shul@redhat.com, bhe@redhat.com, zong.li@sifive.com, puranjay@kernel.org, namcaov@gmail.com, antonb@tenstorrent.com, sorear@fastmail.com, quic_bjorande@quicinc.com, ancientmodern4@gmail.com, ben.dooks@codethink.co.uk, quic_zhonhan@quicinc.com, cuiyunhui@bytedance.com, yang.lee@linux.alibaba.com, ke.zhao@shingroup.cn, sunilvl@ventanamicro.com, tanzhasanwork@gmail.com, schwab@suse.de, dawei.li@shingroup.cn, rppt@kernel.org, willy@infradead.org, usama.anjum@collabora.com, osalvador@suse.de, ryan.roberts@arm.com, andrii@kernel.org, alx@kernel.org, catalin.marinas@arm.com, broonie@kernel.org, revest@chromium.org, bgray@linux.ibm.com, deller@gmx.de, zev@bewilderbeest.net Subject: [PATCH v4 24/30] riscv/kernel: update __show_regs to print shadow stack register Date: Thu, 12 Sep 2024 16:16:43 -0700 Message-ID: <20240912231650.3740732-25-debug@rivosinc.com> X-Mailer: git-send-email 2.45.0 In-Reply-To: <20240912231650.3740732-1-debug@rivosinc.com> References: <20240912231650.3740732-1-debug@rivosinc.com> MIME-Version: 1.0 X-Rspam-User: X-Stat-Signature: crfzuk175adhs3pbj51sgbrx4wdutoim X-Rspamd-Queue-Id: 7AB31180014 X-Rspamd-Server: rspam11 X-HE-Tag: 1726183132-846391 X-HE-Meta: U2FsdGVkX1+qllGt9iOHJQXf3QEszrKbFXqU7ToFMtNJMGsfgMOcq1Aha8MSZueAvvH5GwVa3b3B/Zx9FGD63ohHJDaXpXr3awXLaLth0fzipLx7DOZyw5BzqdigZNJw+f0b7aBtJZewBBlX9WXfK8do6dDxwYIgx7W3pjNWQikqfGvrZoAJVv07Yy4nl+SwQ4BkIJAhG1RetPY3Q5lsuAouHPNL5hqqkGbK6LiJ+sVfZU+l8MFtRPs9UTnUZwzxg8id+26qyCrhpmWkyl/oRiQ1b13WpuccVdA2to1hnXy0bOfJIOiQmFHSWcmsy8XPadPr3sxbWb6XBFQbNE/BaWDNxwCZbDScIHuQpQ1MqadUB93p923N2jrabmMlPybJYhVuw062Cz7NJWUqiZRspS4+E6wwLFydbFbhtKyOjGFAP2HgGarjDglW+kOusE7LrZYjABMMqV56bMAHYOAKaiJR8nVI6dM4AaNqjmkOStdbixofuOpS7IjIbP+xpHFoZ2ss9H8EeEG3JPYnJjMLbFeDUXxtpDP3vtNGy/Rw8RCFKoIC50OUjq+rW7UQNuZ9Oxc5Hp+yIk8hpQybDgQdvI3D7uftZzf8n4UNkX0q4nh99sOuksSFwuly3CNuumw94rXucA8sS3Etx/EobWCcHLEa1ciFcRF2aLljmDl0fZxOPd1MDnse6CNZZwYxlZXSVpQFFo5o8z0A2LwrI1ixDu46MZR0Z0fLBLwLeVHFPNnb47RzFhCFDwZvhbONmcOX/qqCT3mCtvpr9vk2ljSAmBTzf8uNoPNkBSGKYMwvVNB3Jt9NkBi3IGgVrr0vOqR9qiIKo2dW7s86O2PEn7EYl0fi57jeJ4IyoLPOoXRKN8ru+nODkslcW+mGO211Gct3J91iv2Wd64t4A0KWMXGF4YjBn0gavMUwyElaTvnBBkNsPaKcpFkRyVY51syxO5b845xtPmpvO8ZDlYSfaJz F60e5rpr CsiPswLvEkSBf9IR8L3inxfzT0cFC5qLrG7MYCIxt3CoMY6HtUD177hZTiJMOud4qKLYA3HYb9xZEaI2Fs/1KQqsWObjWso3VTPtaBZIGk7zoM6uJgGDtR06itd5Bne2ns9rMcEopWPHqFejDUUPntUCYRFgLrE2eKlAVhDRo8NiEAVyp2GHPPSurg7uMfxe90kyL7LwMEqxCG7m06ZHlgwqyIPKe9RqpVXueg6ASah8okc8IR+AxCDhjsTuVweKSr8WL11dbYuqEzhCceXrRPOY2fOuGvW2+4Pc0q5fIKvnLzT4V0fWQb2B9i98w5ngj2Oq8Y/jRpGO9PRv+F1gO3MBKWB2zDE7zFL2nJrKzaHGV4az3dHGv6Wtngcn6RhE4aXZAeBqmJOBdQp30EUGQyd+KljQ8zIyQAoHHvcusF2J9ulIxnFX2fgtiuoFTRCxUTX0Ce78nX4NNHyJZv8zw4hgxm0uqMEjEbZa0FJ2QHG2XHxMMwRfHUVbqfA== 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: Updating __show_regs to print captured shadow stack pointer as well. On tasks where shadow stack is disabled, it'll simply print 0. Signed-off-by: Deepak Gupta Reviewed-by: Alexandre Ghiti --- arch/riscv/kernel/process.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/arch/riscv/kernel/process.c b/arch/riscv/kernel/process.c index f3c5b8f2c869..1a5d70977e5a 100644 --- a/arch/riscv/kernel/process.c +++ b/arch/riscv/kernel/process.c @@ -87,8 +87,8 @@ void __show_regs(struct pt_regs *regs) regs->s8, regs->s9, regs->s10); pr_cont(" s11: " REG_FMT " t3 : " REG_FMT " t4 : " REG_FMT "\n", regs->s11, regs->t3, regs->t4); - pr_cont(" t5 : " REG_FMT " t6 : " REG_FMT "\n", - regs->t5, regs->t6); + pr_cont(" t5 : " REG_FMT " t6 : " REG_FMT " ssp : " REG_FMT "\n", + regs->t5, regs->t6, get_active_shstk(current)); pr_cont("status: " REG_FMT " badaddr: " REG_FMT " cause: " REG_FMT "\n", regs->status, regs->badaddr, regs->cause); From patchwork Thu Sep 12 23:16:44 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13802848 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 DFADDEEE271 for ; Thu, 12 Sep 2024 23:18:59 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 61AF06B00BC; Thu, 12 Sep 2024 19:18:59 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 5C7B46B00BE; Thu, 12 Sep 2024 19:18:59 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3CB736B00BF; Thu, 12 Sep 2024 19:18:59 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 1DA9B6B00BC for ; Thu, 12 Sep 2024 19:18:59 -0400 (EDT) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id BC568803A2 for ; Thu, 12 Sep 2024 23:18:58 +0000 (UTC) X-FDA: 82557653556.27.F75AA2E Received: from mail-pj1-f47.google.com (mail-pj1-f47.google.com [209.85.216.47]) by imf02.hostedemail.com (Postfix) with ESMTP id DE12780014 for ; Thu, 12 Sep 2024 23:18:56 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=O1P+KRLP; spf=pass (imf02.hostedemail.com: domain of debug@rivosinc.com designates 209.85.216.47 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=1726183031; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=KHeNsbqGcGeU+Pxax1Cmop/4grbmqlxqcgYalDE4FUU=; b=q7Dy5CQD8AfTU8K9HTpF3boyjJxYjs8kBkZxULBKFI5AbdaCO0FkEvsph+TwnJq6JlATsO FgFv68hV+v02LePzc4B7QkkqzSJURDHRWe0B4s2BxSwDf4/97DdA52Kvcx+XtAL4Obb/0C W5AFx5LH0VnJnJZ475Xg7YEm0/8je6w= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1726183031; a=rsa-sha256; cv=none; b=R2Tb9Zp7PNXh8SAmBAoR/tCTaXjjuRuk5V49eogRc95/eTDIJ7KVk2yd+y0S9Fb1UyP0Ag 2R2tGQzOR5TecWl7EgPVczuRXsRcEsXzaF/ZAS3sOf2oRUKDpKfWXvL7XyQLNMzv2PifJb zDqG0s7LfpcwAnpZKftPINxQzbcllC0= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=O1P+KRLP; spf=pass (imf02.hostedemail.com: domain of debug@rivosinc.com designates 209.85.216.47 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none Received: by mail-pj1-f47.google.com with SMTP id 98e67ed59e1d1-2d8b679d7f2so1272302a91.1 for ; Thu, 12 Sep 2024 16:18:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1726183136; x=1726787936; darn=kvack.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=KHeNsbqGcGeU+Pxax1Cmop/4grbmqlxqcgYalDE4FUU=; b=O1P+KRLP7PkAD+16B7Zus4akuC04J9+KIuLmBTEoUgpLNQhzY6450GO1GaJtcNk76K j9CMiqbCg8/iaBzfkkzpHn7waG/EEhEnDXnAFlGAj0h9G7xeL4XTnzdYcL3TmkQ0LRGv BhRJN1OXLHZm3K7RD6MszsN7EMppOPBcRevlenyRp0rYg3xoOQGOOLpqlO9AgwV3Nc4l zXe3bcKpZFsCAIsLbmcdZY9epC6fuqNqt/p7HTeymd02ahz6xNPc6243Bgy4Ban6zSA9 0WxSApeNLaV0/noP43AjdlwkU+d69POsg1b10GNN7GzJEV58rw8dNsoWgyq8TfjdY3CY gbdA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726183136; x=1726787936; 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=KHeNsbqGcGeU+Pxax1Cmop/4grbmqlxqcgYalDE4FUU=; b=pdtgLOGjzoGs97l3rG2a0lM//j+tQ1CIs8CEfMahlSO476pM/HjBeDPkYxYsrE6k1Y 065vBxcNr340HNwS/dhniPwanadbt2kj7sax865c8Sfnl7QfP81wWwheSYu/pvZW5Apq FJSMSjDvTvLIt49BV+9MMsKPpIzHzH7e1jwS6OqjQBDJ1zJ0jsT0X4vMpsCr7Uv98JFY +9btajezCDyNEqnTYUHbAtgIK6SrCn0YptxLEyQYjQHolHkAktBo0YfzZl1U+jLnSuD2 IKqfSs0DJ9bfz3ELiK44+7HKBnhhiUYHerJSeCT0I4dAuurLqwwRuCFpqr8/kbX1Wytq 5GDw== X-Forwarded-Encrypted: i=1; AJvYcCUvTYWTT/ghOjyzYOAl5+I3Vvbp72n+8ytesb4c5qFBcKSemNeWW6vw4gNC+V9HtptewqVPYQqtuQ==@kvack.org X-Gm-Message-State: AOJu0YwQnyN63xCsCraiFEOvmPsD4nX81QVTrzYMOX2wplPIEpwVUI8F zChPfXFxEtIpAPNi0mzbnq9p9L4QXOpT5mfcFx1s/k47mSBkFdneiM9fafufPmo= X-Google-Smtp-Source: AGHT+IF7CFCu96gm/xkq1N6i1yXTt9iSzasTjNEzOjllTvd3QLn/Kdw4NOydn5m2u4mcpVddzMAc9Q== X-Received: by 2002:a17:90b:3808:b0:2cf:def1:d1eb with SMTP id 98e67ed59e1d1-2db9ff74692mr5469461a91.8.1726183135597; Thu, 12 Sep 2024 16:18:55 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2db6c1ac69asm3157591a91.0.2024.09.12.16.18.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 12 Sep 2024 16:18:55 -0700 (PDT) From: Deepak Gupta To: paul.walmsley@sifive.com, palmer@sifive.com, conor@kernel.org, linux-doc@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kselftest@vger.kernel.org Cc: corbet@lwn.net, palmer@dabbelt.com, aou@eecs.berkeley.edu, robh@kernel.org, krzk+dt@kernel.org, oleg@redhat.com, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, hpa@zytor.com, peterz@infradead.org, akpm@linux-foundation.org, arnd@arndb.de, ebiederm@xmission.com, kees@kernel.org, Liam.Howlett@oracle.com, vbabka@suse.cz, lorenzo.stoakes@oracle.com, shuah@kernel.org, brauner@kernel.org, samuel.holland@sifive.com, debug@rivosinc.com, andy.chiu@sifive.com, jerry.shih@sifive.com, greentime.hu@sifive.com, charlie@rivosinc.com, evan@rivosinc.com, cleger@rivosinc.com, xiao.w.wang@intel.com, ajones@ventanamicro.com, anup@brainfault.org, mchitale@ventanamicro.com, atishp@rivosinc.com, sameo@rivosinc.com, bjorn@rivosinc.com, alexghiti@rivosinc.com, david@redhat.com, libang.li@antgroup.com, jszhang@kernel.org, leobras@redhat.com, guoren@kernel.org, samitolvanen@google.com, songshuaishuai@tinylab.org, costa.shul@redhat.com, bhe@redhat.com, zong.li@sifive.com, puranjay@kernel.org, namcaov@gmail.com, antonb@tenstorrent.com, sorear@fastmail.com, quic_bjorande@quicinc.com, ancientmodern4@gmail.com, ben.dooks@codethink.co.uk, quic_zhonhan@quicinc.com, cuiyunhui@bytedance.com, yang.lee@linux.alibaba.com, ke.zhao@shingroup.cn, sunilvl@ventanamicro.com, tanzhasanwork@gmail.com, schwab@suse.de, dawei.li@shingroup.cn, rppt@kernel.org, willy@infradead.org, usama.anjum@collabora.com, osalvador@suse.de, ryan.roberts@arm.com, andrii@kernel.org, alx@kernel.org, catalin.marinas@arm.com, broonie@kernel.org, revest@chromium.org, bgray@linux.ibm.com, deller@gmx.de, zev@bewilderbeest.net Subject: [PATCH v4 25/30] riscv/ptrace: riscv cfi status and state via ptrace and in core files Date: Thu, 12 Sep 2024 16:16:44 -0700 Message-ID: <20240912231650.3740732-26-debug@rivosinc.com> X-Mailer: git-send-email 2.45.0 In-Reply-To: <20240912231650.3740732-1-debug@rivosinc.com> References: <20240912231650.3740732-1-debug@rivosinc.com> MIME-Version: 1.0 X-Stat-Signature: qhnkepxyreqyx7ji4gfiafpttux7jyng X-Rspamd-Queue-Id: DE12780014 X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1726183136-666024 X-HE-Meta: U2FsdGVkX19P/DgewVmREb5nJNYBH+Xlqh7cuv2oYKWpIdFMoeRG8rSNic2U05zYFk4uv12CNI4xzCw5nR33CH1o6smfljJ8tF8pSvo3jpWdMPxrg3u4rv5r3Q6NmMr8H5Mm5AA7dap9oLNSUwk1NgS/lVTFgbnU9GzkTyACR6mw6bRttFghs0GbPZebTcrxea1WA4yYi0D2GYilQHc93LUSDV+muwl/fofXXqATkqZ8UiGoQQkDfUw7KW52pqyPpBaNX5AMGQl4KgxMcnDOBeaycK7GZPlvq3AIRjqo+dgiTIFYelkWn/JUPNYFKo+HNGXVDh/ltrWwVHUr6t7UboQBmhhISCqlxN8268gr86o+w2gLmlIeh2LTbP/kSTn7GV0xoqfPxANBYBw7dw1aoTopJJyDumR8hoh3G9wZ3amE5FY+7AI8R2ktOFjZbyZdNFI4pR9a5Rofg37Vv5IyO6Xj4KEMdpSGCjTjs+xJnR3sz8/q+7kcRAv0QDNeZ+uNuCG9BipNZ+5nD+sJs4AXHo0Iu/FxQL9vPH1NHTQlbj78m7dwUdUPATPbusTR42Kw1ku4sWXBj1oSxzu1s/vnrDxbCnyq220eMooJ3aYjFQWB1gsfPktly12iILr8EKbkTHeVLNMbx62R3PS6Bry3Clm5biQHYv67PknJ0rQ6Zkbsr0fGcG888rnGYW45+JsbzNVPgjZQMAkYE3/RkcLZ7OG6iik29bbjk+8y3wSX3xx3tqlwNLwOzXrvLzrrAvz1HPoI8R47C+x2Uh2y7nego1kVW5THViS3fhU9vCdm8Nq3lD1ng51sjc3poEMJ6U3M0rlLixeb2GIoPOYx+arGQf1NG7z5wAFS9uGyfBZT1VsZWV+Xmrl9AU5vW5C6robfXHZLwxrLWSnlxa1UPMNBohoBbeWBMmMBQZqV79OZrTtUTZTJY8pEzWYxKwn527uoDGuqeTCDlVC8zjqAmEB DW8ZQfUb otNFVAiZUiYsY33+ZdLcJRF+g9KG97d2RlycJG2oAnDSsUWFSZWTL60xTtwaCNaUgNQfRkawuRwvS/mKg21qWVAtBMnv1HJ2Gi7bB3pJFp+XcPinS1vRCLGCroUcQoxxi+32g0CsNSi/V60HIo2pxwc7v1pGskGFczdupTiMuA3gAjQQdivDqWFfT0NuK+lYR4tJEhq7xnE1a+thbL47OfhNsLQzsQhSIiIrsBzNg18Z7NQcvtanc1Q65gh5ZjyBKOffYvMHQ9lXrQNWA72WmemY0fd6Br/YOUlu0vg30tlRSzU/ljhXoDBHW0Jzf+h6wDReUDrYWEnsnYSbJTCd6SABiDC1gW8I5gGkVTqq7uQ1Ku91vTT6WH9ti0QQOGSv+LByr/hqWLHzKxw3/2oslK7n3+VCLHZXMOFTNpSqWxV2zt3U= 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: Expose a new register type NT_RISCV_USER_CFI for risc-v cfi status and state. Intentionally both landing pad and shadow stack status and state are rolled into cfi state. Creating two different NT_RISCV_USER_XXX would not be useful and wastage of a note type. Enabling or disabling of feature is not allowed via ptrace set interface. However setting `elp` state or setting shadow stack pointer are allowed via ptrace set interface. It is expected `gdb` might have use to fixup `elp` state or `shadow stack` pointer. Signed-off-by: Deepak Gupta --- arch/riscv/include/uapi/asm/ptrace.h | 18 ++++++ arch/riscv/kernel/ptrace.c | 83 ++++++++++++++++++++++++++++ include/uapi/linux/elf.h | 1 + 3 files changed, 102 insertions(+) diff --git a/arch/riscv/include/uapi/asm/ptrace.h b/arch/riscv/include/uapi/asm/ptrace.h index a38268b19c3d..512be06a8661 100644 --- a/arch/riscv/include/uapi/asm/ptrace.h +++ b/arch/riscv/include/uapi/asm/ptrace.h @@ -127,6 +127,24 @@ struct __riscv_v_regset_state { */ #define RISCV_MAX_VLENB (8192) +struct __cfi_status { + /* indirect branch tracking state */ + __u64 lp_en : 1; + __u64 lp_lock : 1; + __u64 elp_state : 1; + + /* shadow stack status */ + __u64 shstk_en : 1; + __u64 shstk_lock : 1; + + __u64 rsvd : sizeof(__u64) - 5; +}; + +struct user_cfi_state { + struct __cfi_status cfi_status; + __u64 shstk_ptr; +}; + #endif /* __ASSEMBLY__ */ #endif /* _UAPI_ASM_RISCV_PTRACE_H */ diff --git a/arch/riscv/kernel/ptrace.c b/arch/riscv/kernel/ptrace.c index 92731ff8c79a..c69b20ea6e79 100644 --- a/arch/riscv/kernel/ptrace.c +++ b/arch/riscv/kernel/ptrace.c @@ -19,6 +19,7 @@ #include #include #include +#include enum riscv_regset { REGSET_X, @@ -28,6 +29,9 @@ enum riscv_regset { #ifdef CONFIG_RISCV_ISA_V REGSET_V, #endif +#ifdef CONFIG_RISCV_USER_CFI + REGSET_CFI, +#endif }; static int riscv_gpr_get(struct task_struct *target, @@ -152,6 +156,75 @@ static int riscv_vr_set(struct task_struct *target, } #endif +#ifdef CONFIG_RISCV_USER_CFI +static int riscv_cfi_get(struct task_struct *target, + const struct user_regset *regset, + struct membuf to) +{ + struct user_cfi_state user_cfi; + struct pt_regs *regs; + + regs = task_pt_regs(target); + + user_cfi.cfi_status.lp_en = is_indir_lp_enabled(target); + user_cfi.cfi_status.lp_lock = is_indir_lp_locked(target); + user_cfi.cfi_status.elp_state = (regs->status & SR_ELP); + + user_cfi.cfi_status.shstk_en = is_shstk_enabled(target); + user_cfi.cfi_status.shstk_lock = is_shstk_locked(target); + user_cfi.shstk_ptr = get_active_shstk(target); + + return membuf_write(&to, &user_cfi, sizeof(user_cfi)); +} + +/* + * Does it make sense to allowing enable / disable of cfi via ptrace? + * Not allowing enable / disable / locking control via ptrace for now. + * Setting shadow stack pointer is allowed. GDB might use it to unwind or + * some other fixup. Similarly gdb might want to suppress elp and may want + * to reset elp state. + */ +static int riscv_cfi_set(struct task_struct *target, + const struct user_regset *regset, + unsigned int pos, unsigned int count, + const void *kbuf, const void __user *ubuf) +{ + int ret; + struct user_cfi_state user_cfi; + struct pt_regs *regs; + + regs = task_pt_regs(target); + + ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &user_cfi, 0, -1); + if (ret) + return ret; + + /* + * Not allowing enabling or locking shadow stack or landing pad + * There is no disabling of shadow stack or landing pad via ptrace + * rsvd field should be set to zero so that if those fields are needed in future + */ + if (user_cfi.cfi_status.lp_en || user_cfi.cfi_status.lp_lock || + user_cfi.cfi_status.shstk_en || user_cfi.cfi_status.shstk_lock || + !user_cfi.cfi_status.rsvd) + return -EINVAL; + + /* If lpad is enabled on target and ptrace requests to set / clear elp, do that */ + if (is_indir_lp_enabled(target)) { + if (user_cfi.cfi_status.elp_state) /* set elp state */ + regs->status |= SR_ELP; + else + regs->status &= ~SR_ELP; /* clear elp state */ + } + + /* If shadow stack enabled on target, set new shadow stack pointer */ + if (is_shstk_enabled(target)) + set_active_shstk(target, user_cfi.shstk_ptr); + + return 0; +} +#endif + static const struct user_regset riscv_user_regset[] = { [REGSET_X] = { .core_note_type = NT_PRSTATUS, @@ -182,6 +255,16 @@ static const struct user_regset riscv_user_regset[] = { .set = riscv_vr_set, }, #endif +#ifdef CONFIG_RISCV_USER_CFI + [REGSET_CFI] = { + .core_note_type = NT_RISCV_USER_CFI, + .align = sizeof(__u64), + .n = sizeof(struct user_cfi_state) / sizeof(__u64), + .size = sizeof(__u64), + .regset_get = riscv_cfi_get, + .set = riscv_cfi_set, + } +#endif }; static const struct user_regset_view riscv_user_native_view = { diff --git a/include/uapi/linux/elf.h b/include/uapi/linux/elf.h index b54b313bcf07..390732883fd7 100644 --- a/include/uapi/linux/elf.h +++ b/include/uapi/linux/elf.h @@ -448,6 +448,7 @@ typedef struct elf64_shdr { #define NT_MIPS_MSA 0x802 /* MIPS SIMD registers */ #define NT_RISCV_CSR 0x900 /* RISC-V Control and Status Registers */ #define NT_RISCV_VECTOR 0x901 /* RISC-V vector registers */ +#define NT_RISCV_USER_CFI 0x902 /* RISC-V shadow stack state */ #define NT_LOONGARCH_CPUCFG 0xa00 /* LoongArch CPU config registers */ #define NT_LOONGARCH_CSR 0xa01 /* LoongArch control and status registers */ #define NT_LOONGARCH_LSX 0xa02 /* LoongArch Loongson SIMD Extension registers */ From patchwork Thu Sep 12 23:16:45 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13802849 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 E16F2EEE270 for ; Thu, 12 Sep 2024 23:19:03 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7029A6B00BE; Thu, 12 Sep 2024 19:19:03 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 68B7C6B00C0; Thu, 12 Sep 2024 19:19:03 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4B6116B00C1; Thu, 12 Sep 2024 19:19:03 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 2C8846B00BE for ; Thu, 12 Sep 2024 19:19:03 -0400 (EDT) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id E42E9120CA8 for ; Thu, 12 Sep 2024 23:19:02 +0000 (UTC) X-FDA: 82557653724.14.2196D9E Received: from mail-pj1-f42.google.com (mail-pj1-f42.google.com [209.85.216.42]) by imf05.hostedemail.com (Postfix) with ESMTP id 25D0B10000C for ; Thu, 12 Sep 2024 23:19:00 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=OBefazCi; dmarc=none; spf=pass (imf05.hostedemail.com: domain of debug@rivosinc.com designates 209.85.216.42 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1726183063; a=rsa-sha256; cv=none; b=OZ6hrE4jwJmK+G//8Ee21ZBG1o7K9Fojd5Aj3LHZNuZf9SYpZyPOwM27zLyUBY7pl8GUqv 0hXO/dzD1hl2/RURaFcqS9Zc3PmL2TleGfyM+BOIt3hixCSNUHzr9CCO2LTCzI5bgm7N7k yVro3tJrucIdu/UjzQf1klYAThhpb10= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=OBefazCi; dmarc=none; spf=pass (imf05.hostedemail.com: domain of debug@rivosinc.com designates 209.85.216.42 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1726183063; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=d4D/20yGykkpsrImg7wGRo0iFbIK8mOpJga3H67yqrM=; b=wwDt/raZRI53LUWmTV/Bzx32wrdQiWQD41vmYVrOfs//KQjuSaP2m93ZpgGwR215hUIuLr BD+AGpF4NdawQY3snQULGV3xrYbERhHZNfvn7K+wfPhRpwSQq1jxOAjRJekiNrG5kG5LGo SDInUQDqW2osnPXBm7/84/U9vube/9s= Received: by mail-pj1-f42.google.com with SMTP id 98e67ed59e1d1-2d877e9054eso1107255a91.3 for ; Thu, 12 Sep 2024 16:19:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1726183140; x=1726787940; darn=kvack.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=d4D/20yGykkpsrImg7wGRo0iFbIK8mOpJga3H67yqrM=; b=OBefazCisoDvw95BI1A3O5iaBqoiDB/ORUxZYCm6YVJrIePO2T1ilMK7wwFyF83R52 eVcG3Dmj0KevGhhNvW2gXHnU/vowzkqlFBQ6wD13ZP96KuKIzC33j0KTkcg1aBI9V2gd d36iiIZgxYWzWV7p1zcZBBL+bbU+/nUcA+oViwsJ2gQbgXWH1eD9mVUsskngi05iog4w CBqDK5E6GzTyGYNndFor8Bm2c8up6o0SDS2S9oFgRM2ucJSx1OSW85fF35uWwoQ/sESC AnnyweYk5ilNGtlBJgC7OqZIXn8ZQU+mWhvX1fOj4F74Ru7FzkT2V6/O9IAoL6/pGoPk lOnQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726183140; x=1726787940; 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=d4D/20yGykkpsrImg7wGRo0iFbIK8mOpJga3H67yqrM=; b=hm5LM1jM3aUckCjY/mg0JabU3ZCORvT58LsIDH4cZZ60rDJK4sPp/tLcW0nPlEu1eE xkzmaX0wssf+8GyfO5OfZQJZ5DmG+tuyGXBLCq0dF9QaECznAjYcb23lSsbGpzw1cq8W Q+O8NeQ1v8Z2QAjyp7IW7L3x5/VV/GToDiC5xCcmlq02HFp8LBqaOGvb9JnXzNR8pNBH 4CgzUXpanC0CbuPMoozn8C1gBuG5S/INHZs+qNReutdUlrK/1NeGuk2sjS6zHNZZA+W6 9n/tx8cyj81PhtBCGZUQFXdkz97aLk0a4ynxjXwmxv6de9zeiw8OLizdbmuVHmCKmLyg GZvg== X-Forwarded-Encrypted: i=1; AJvYcCVbwJ3FHJgODeu/D/KtB1GpUUljF/iM9UhuCOZnBpBzwsb+s+HQZuDk4jr5tQM65mOpzEKs5kokZg==@kvack.org X-Gm-Message-State: AOJu0Yx/eOpdyxht6K03RAX1wl6pvu0bJ1gxvWW9GNXAIIfGrHn8jt5e fcDBVV8bKgssWS4iB5am6RVqvzTfgL4btTzpNb19k/oFBd1X8PWaU3HMzksTCdQ= X-Google-Smtp-Source: AGHT+IEQ4DyvIPot4wX3TaeOhc9igc9k0ddrlo9rAO0eC0Cbq71JlaUH+P/ak8XpjpWlABFI12/jIA== X-Received: by 2002:a17:90a:4b88:b0:2da:8c28:6561 with SMTP id 98e67ed59e1d1-2db9ffb41ccmr4730379a91.22.1726183139830; Thu, 12 Sep 2024 16:18:59 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2db6c1ac69asm3157591a91.0.2024.09.12.16.18.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 12 Sep 2024 16:18:59 -0700 (PDT) From: Deepak Gupta To: paul.walmsley@sifive.com, palmer@sifive.com, conor@kernel.org, linux-doc@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kselftest@vger.kernel.org Cc: corbet@lwn.net, palmer@dabbelt.com, aou@eecs.berkeley.edu, robh@kernel.org, krzk+dt@kernel.org, oleg@redhat.com, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, hpa@zytor.com, peterz@infradead.org, akpm@linux-foundation.org, arnd@arndb.de, ebiederm@xmission.com, kees@kernel.org, Liam.Howlett@oracle.com, vbabka@suse.cz, lorenzo.stoakes@oracle.com, shuah@kernel.org, brauner@kernel.org, samuel.holland@sifive.com, debug@rivosinc.com, andy.chiu@sifive.com, jerry.shih@sifive.com, greentime.hu@sifive.com, charlie@rivosinc.com, evan@rivosinc.com, cleger@rivosinc.com, xiao.w.wang@intel.com, ajones@ventanamicro.com, anup@brainfault.org, mchitale@ventanamicro.com, atishp@rivosinc.com, sameo@rivosinc.com, bjorn@rivosinc.com, alexghiti@rivosinc.com, david@redhat.com, libang.li@antgroup.com, jszhang@kernel.org, leobras@redhat.com, guoren@kernel.org, samitolvanen@google.com, songshuaishuai@tinylab.org, costa.shul@redhat.com, bhe@redhat.com, zong.li@sifive.com, puranjay@kernel.org, namcaov@gmail.com, antonb@tenstorrent.com, sorear@fastmail.com, quic_bjorande@quicinc.com, ancientmodern4@gmail.com, ben.dooks@codethink.co.uk, quic_zhonhan@quicinc.com, cuiyunhui@bytedance.com, yang.lee@linux.alibaba.com, ke.zhao@shingroup.cn, sunilvl@ventanamicro.com, tanzhasanwork@gmail.com, schwab@suse.de, dawei.li@shingroup.cn, rppt@kernel.org, willy@infradead.org, usama.anjum@collabora.com, osalvador@suse.de, ryan.roberts@arm.com, andrii@kernel.org, alx@kernel.org, catalin.marinas@arm.com, broonie@kernel.org, revest@chromium.org, bgray@linux.ibm.com, deller@gmx.de, zev@bewilderbeest.net Subject: [PATCH v4 26/30] riscv/hwprobe: zicfilp / zicfiss enumeration in hwprobe Date: Thu, 12 Sep 2024 16:16:45 -0700 Message-ID: <20240912231650.3740732-27-debug@rivosinc.com> X-Mailer: git-send-email 2.45.0 In-Reply-To: <20240912231650.3740732-1-debug@rivosinc.com> References: <20240912231650.3740732-1-debug@rivosinc.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 25D0B10000C X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: j57subgonwkw8t1coxddztdscyn6hzqd X-HE-Tag: 1726183140-36785 X-HE-Meta: U2FsdGVkX1+fsrVvLubAcX10TafPDxDd4yEtsKVhhvvFaG6BuphpvXiTMBkk7bjdS7KFNw8kEKQ9T40o5K5dt63Az9f/0OV1P8fFPmcYf9rdfZXMyM0LUS5c3mo9iWpHVq/2Oyg0qnXZ242+J2tfkice2n+FgVzQv2yNuKee5dgU7S0U58DrYkMs1bVO6vh8DiCVYjJwNenQtj7g5m76HS3EEurCqdKXIGSZbzfyO9qfQUbp/bgsO9h+fZmKtTZbBvYZktcm+DTeyQdXx/DqsCc9R/uWEzQXsoEpeF5W4xeqrDK/+js/o86oVCDUCVqhp9b82Kg+Wepi6n9uwwKQSMlNu4X1Rt9NHCyDIOc3vK0WPsRaQ3uF+OIhWA56lfSeNDpSv5fw7EsgJ89gryH4nMc9/uP34TK5BGFwsSk8i3uR0O7TLTKj82nDeR4KWtORelR5P6syxe5JVsGS2Y7MJZJtO7yuduc7VvqyvpN1Wso2lRaH4SlfM+Clucy+FedzqEHOiBOWCqkLm5FB7hExRWwa6sVBJGd2RffTzmu+9b7svoM4HB5HJUEJmeoUEBMaVZ1tjLzACddJ76buk5p8CWZX5BRFz1/yQKQCRgYGSc5wYccd9kVvVBBRrBE5pzCoOW47u1ugNdafky5oBUVedx6eHk2aro7/Xt9YUuO5gwcyK0Xhbsiwhud+JjmqSdGED59UNitVCMwdfRRIWIMTIWvWom08zzCbag3xsYgiBNUg2dBzeRMIhWgyivc1WrAH5glKtnazgl2cQQR1FKF4gO45oM/IYf/HBe3x1un/brgcxG8eCdfvqvLdVCBOxsil2+2TBUMh77UzGw8ToNNsQ5nLxH2DEi8VSh1aZnnOVAHLNme220mBYuKrJi+j3bxapxcYKxejHmrg46MdW2+8npPv3XCQ9uYjH8Lc39q8wZV8oFKRDDTuhmokunCFLD3l0BdCGMSCcaWGGKfHTAG 7BjqaNo6 iNTmpB2b6yUC3X6VsDXuF9IiLin0KoNr4e3kGin+SC9g/3M3PSdYE75UZ7JpQ3xXU0y8L0HK46jtx17loB1DfyVk9ujOGO84r09wuzdS9BA4t0shv1EXVAfpdQIVjcQWrZvKDt7uGpE0SWsDQfAeghtY1tKxGSP/CK91O5FkuCj8/y3F4z1VMRqpOvaCs3SYmFNTQ5sMSdGEORiEWss0wVeXav84VWNXi36+H2ScSaJYytiVvJVb56Ll6n0+ykNErCn0nNdHkq0dJLJvzA/XmbzznrKR+BBSYyaZJI6h8cFifl+SY4hOGYhvyZCtJDKeLyDUA8/7LiqktoUxUc/Tzw46L/OJKPyQQkf9RsKrtnpy1gcZFfBSF6otXZFj3WxOxAkI2UUQ76QZFcDFi9rwkYpR3H3NJ9rg91j5rwxxTjXlWA9ikeLrcSJkRKKNqQxawMho1 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000003, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Adding enumeration of zicfilp and zicfiss extensions in hwprobe syscall. Signed-off-by: Deepak Gupta --- arch/riscv/include/uapi/asm/hwprobe.h | 2 ++ arch/riscv/kernel/sys_hwprobe.c | 2 ++ 2 files changed, 4 insertions(+) diff --git a/arch/riscv/include/uapi/asm/hwprobe.h b/arch/riscv/include/uapi/asm/hwprobe.h index 1e153cda57db..d5c5dec9ae6c 100644 --- a/arch/riscv/include/uapi/asm/hwprobe.h +++ b/arch/riscv/include/uapi/asm/hwprobe.h @@ -72,6 +72,8 @@ struct riscv_hwprobe { #define RISCV_HWPROBE_EXT_ZCF (1ULL << 46) #define RISCV_HWPROBE_EXT_ZCMOP (1ULL << 47) #define RISCV_HWPROBE_EXT_ZAWRS (1ULL << 48) +#define RISCV_HWPROBE_EXT_ZICFILP (1ULL << 49) +#define RISCV_HWPROBE_EXT_ZICFISS (1ULL << 50) #define RISCV_HWPROBE_KEY_CPUPERF_0 5 #define RISCV_HWPROBE_MISALIGNED_UNKNOWN (0 << 0) #define RISCV_HWPROBE_MISALIGNED_EMULATED (1 << 0) diff --git a/arch/riscv/kernel/sys_hwprobe.c b/arch/riscv/kernel/sys_hwprobe.c index cea0ca2bf2a2..98f72ad7124f 100644 --- a/arch/riscv/kernel/sys_hwprobe.c +++ b/arch/riscv/kernel/sys_hwprobe.c @@ -107,6 +107,8 @@ static void hwprobe_isa_ext0(struct riscv_hwprobe *pair, EXT_KEY(ZCB); EXT_KEY(ZCMOP); EXT_KEY(ZICBOZ); + EXT_KEY(ZICFILP); + EXT_KEY(ZICFISS); EXT_KEY(ZICOND); EXT_KEY(ZIHINTNTL); EXT_KEY(ZIHINTPAUSE); From patchwork Thu Sep 12 23:16:46 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13802850 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 5314AEEE272 for ; Thu, 12 Sep 2024 23:19:08 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D1DE46B00C0; Thu, 12 Sep 2024 19:19:07 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id CA5D58D0003; Thu, 12 Sep 2024 19:19:07 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id AA9EB6B00C3; Thu, 12 Sep 2024 19:19:07 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 872B36B00C0 for ; Thu, 12 Sep 2024 19:19:07 -0400 (EDT) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 3A0D040E84 for ; Thu, 12 Sep 2024 23:19:07 +0000 (UTC) X-FDA: 82557653934.24.A3E8EA8 Received: from mail-pj1-f46.google.com (mail-pj1-f46.google.com [209.85.216.46]) by imf05.hostedemail.com (Postfix) with ESMTP id 6C68C10000B for ; Thu, 12 Sep 2024 23:19:05 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=HQYNVKBs; dmarc=none; spf=pass (imf05.hostedemail.com: domain of debug@rivosinc.com designates 209.85.216.46 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1726183116; a=rsa-sha256; cv=none; b=YcAQQUUsx+18X46EtD4dB8151r0SeJMGKlDY6ERfSzFL8yK3Nf4cBK0Df5VrK/iNYghDj5 SturpxJ+vSQmYKqd42P7uFWFquUdU4rpiU6tm7YYLb+f+pF1iBaUNRwkBTqZ9jEGSOT35A CDRw+zXG+LvoCAC5J2/qN4K5BWWuB4I= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=HQYNVKBs; dmarc=none; spf=pass (imf05.hostedemail.com: domain of debug@rivosinc.com designates 209.85.216.46 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1726183116; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=CbpKVNKRMfAtkqYXzA8w+8eVro8K48y9vmHyffj7Qao=; b=mYchrT0fmigd5ha/EctZa7HIoo4mnB3CdcVj6IDzNWy2FTZbn2nQRQ8Y/ygT0XmiZ2y3Vl KZtbiJEd43fSbZH7t7prmiX+Khub2Wpgc7M0D5i3XzbEsHC3mYCeBLjvy1MQXnz/w/6Z+B f82qkwKTZH2wI3TaSCLguPNL8KF3zGU= Received: by mail-pj1-f46.google.com with SMTP id 98e67ed59e1d1-2d86f71353dso1108836a91.2 for ; Thu, 12 Sep 2024 16:19:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1726183144; x=1726787944; darn=kvack.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=CbpKVNKRMfAtkqYXzA8w+8eVro8K48y9vmHyffj7Qao=; b=HQYNVKBs+J/oE1N/Mn0EtGfY0NLDMWeEHcGelFoZoBcKHBeD9yH+XiXPRILCtWAL+F 57PqJOpahbeFgOxYi5O0icnxyN9mYwgwXihkI2lRy7Bg0nKnaeO+uG3He6sidd17mtjh +kCHBveIWP1t8bRjX/GEEq1W1n8Ld0xbMatMQP0bzkseHHaCQZV69r3QxrhZPJclS4SI vqanOdUwMoYNqn+dD8owxj2GpZI7UcXxYFJbnKx5N5ojiByC5inZtcJmVjxwgNZKjNjm PKbw20sNkFevwj1JD5V4SORdYwTjqaD3gNBOhPfcxVcs5J+kWOZLZis/Mb/9G2FY2AST n2OQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726183144; x=1726787944; 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=CbpKVNKRMfAtkqYXzA8w+8eVro8K48y9vmHyffj7Qao=; b=SNi0lxmQgJZvCcuAli4RiT0OR2JbixbUezM0mCXluxuJ8Tct77yEGzWEBvBSIYkCFK fVGMr7JDhpBGJ4G+O7n4DJni4eq3QBgjoEHUxWUEId9fUiQ5qUFeH6GPm0bBoL6AsvQM GdcQZrS+IVSzT2GnPvqKT1VhC3XRKEZWAAuLvj9ocWSQFhCEJOiK6YelnYiFSEBZVy+J dok5WZBmKzd2QvYm6rG0Fb3OrTXxnp+rfyoYYj2ompXt/7ZAQuCdjrSiGqUl4urVFqNc h3qW4SGVqJ56RevUlhAQeM3I5MSoGSIIoRQkmTSKL91pqGlqhi3v0xaV6V9AoSosJku/ Vo9w== X-Forwarded-Encrypted: i=1; AJvYcCWOgA0a6PheC/AFf9iPvsF4uBeWA9M398jqfqVn+Y1iNfG7oUhS4mUiJIonW79lyWLhKcJ9M+VxfA==@kvack.org X-Gm-Message-State: AOJu0Yw0uGTSq0NHQ1kXkjqkTkjPXck3+k78WMcJwtP4zdb2czLO9ciQ 7AMs+l7AYHV+Vgo47oxa53ARk9OS3bpNjshsO2zcpAFcFulkQxMAADrS0k/AbhY= X-Google-Smtp-Source: AGHT+IERJQSRxOQYUrcoeWqKKFDuWvirFZC93eScxDPtDLfWGyU08Qp8o+SGLVlkTYVlfOG74jZpnw== X-Received: by 2002:a17:90b:1043:b0:2d3:da6d:8330 with SMTP id 98e67ed59e1d1-2db9ff79ba2mr4907026a91.4.1726183144244; Thu, 12 Sep 2024 16:19:04 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2db6c1ac69asm3157591a91.0.2024.09.12.16.19.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 12 Sep 2024 16:19:03 -0700 (PDT) From: Deepak Gupta To: paul.walmsley@sifive.com, palmer@sifive.com, conor@kernel.org, linux-doc@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kselftest@vger.kernel.org Cc: corbet@lwn.net, palmer@dabbelt.com, aou@eecs.berkeley.edu, robh@kernel.org, krzk+dt@kernel.org, oleg@redhat.com, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, hpa@zytor.com, peterz@infradead.org, akpm@linux-foundation.org, arnd@arndb.de, ebiederm@xmission.com, kees@kernel.org, Liam.Howlett@oracle.com, vbabka@suse.cz, lorenzo.stoakes@oracle.com, shuah@kernel.org, brauner@kernel.org, samuel.holland@sifive.com, debug@rivosinc.com, andy.chiu@sifive.com, jerry.shih@sifive.com, greentime.hu@sifive.com, charlie@rivosinc.com, evan@rivosinc.com, cleger@rivosinc.com, xiao.w.wang@intel.com, ajones@ventanamicro.com, anup@brainfault.org, mchitale@ventanamicro.com, atishp@rivosinc.com, sameo@rivosinc.com, bjorn@rivosinc.com, alexghiti@rivosinc.com, david@redhat.com, libang.li@antgroup.com, jszhang@kernel.org, leobras@redhat.com, guoren@kernel.org, samitolvanen@google.com, songshuaishuai@tinylab.org, costa.shul@redhat.com, bhe@redhat.com, zong.li@sifive.com, puranjay@kernel.org, namcaov@gmail.com, antonb@tenstorrent.com, sorear@fastmail.com, quic_bjorande@quicinc.com, ancientmodern4@gmail.com, ben.dooks@codethink.co.uk, quic_zhonhan@quicinc.com, cuiyunhui@bytedance.com, yang.lee@linux.alibaba.com, ke.zhao@shingroup.cn, sunilvl@ventanamicro.com, tanzhasanwork@gmail.com, schwab@suse.de, dawei.li@shingroup.cn, rppt@kernel.org, willy@infradead.org, usama.anjum@collabora.com, osalvador@suse.de, ryan.roberts@arm.com, andrii@kernel.org, alx@kernel.org, catalin.marinas@arm.com, broonie@kernel.org, revest@chromium.org, bgray@linux.ibm.com, deller@gmx.de, zev@bewilderbeest.net Subject: [PATCH v4 27/30] riscv: create a config for shadow stack and landing pad instr support Date: Thu, 12 Sep 2024 16:16:46 -0700 Message-ID: <20240912231650.3740732-28-debug@rivosinc.com> X-Mailer: git-send-email 2.45.0 In-Reply-To: <20240912231650.3740732-1-debug@rivosinc.com> References: <20240912231650.3740732-1-debug@rivosinc.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Queue-Id: 6C68C10000B X-Rspamd-Server: rspam01 X-Stat-Signature: 1znf1bau1or18g1b17ty7raj9hdf7zz6 X-HE-Tag: 1726183145-532798 X-HE-Meta: U2FsdGVkX1/RWZGj9WuS+5a2aSrD0ZuHB7Nmy6j9Am8L3UX54f9EwnoQ3jt84pQjYXXDEFCrxsP+cypT+9mh+RaXZE+QwXDClRKyqEysIc2WYAwuS7GLmOXzD3Pc2n84/w9uqAiqsbFXnehzn/mDjTxyPdBdNNmqtW865zUYs29BUqPDn0dJfxIZW87x0g2FHiUjF+nH5fYLUMqumXU84AzwD9Y2IT2KImTCwAtjMkiz/7LEz3iK8mk/KtTxPOJFrd7qFxNjQ9LeAiiqVU8EWnIp++yQoESl32YkMcoSjMkiYsRUAgoEefilsYjWRRwyUX2/flPr5X5HIkGgO/UwW4TzGkt9UgrU59ejPnG+g3LwaLV9iZwn7DBK+8glhX8ZyF2JxU9oTjfmWOdNkM86NSW5w1TW9LSLBwslZk5Lswer0ReOzMG72u0Fd0IK7HuOkYG7H1Qd9tJq3qVL4WO2b5mVYwjXcraDlLE5GyjZbQOJFs4n71YMUQ1IgvQ72Tm9afiOUcsYL1cKb8WGsiu8Yddj2jQSxG9KF2Jk6bgt1jjcQJuG8mjqGBQaDPaAam0FvhksC0iHfdQRyer79xSzIUVSBI6aWZLcMQ70PN2wawBk+U/3S2vFB03rzf4lS5Y1hpGe4m/RhO4JbruCryVd5BkCJdxuFF+KuKd7N2wa2imfNbD+dgpTtNPWe02xaTpALZJn3CcZUTHHRF1PmOnJzQuLw1YU/dlmKFIcHzmc1siYrI+cn5znCgFPLv3yG5avt9ZkTDo3iGPIeS8+ku4hHvrKPVSmfFRFdKRBjdaI4j0nNMskXZMpjC+kdsMUYrdhTaiD/3VpwY1/yJC+k9zsZ91jYzhK+ENmLmrUTkdQMfwDUD9wYPVNyVVP+3xyxgR+7gpUQme3STB5oKCo2s2Q/jSSI3UhsQMZR08sJ7JJ1fg8ZoMyNxKEtkToCEdQUwJTkrZcsIMrQtrMCz1UnP4 vB7rCnLx ILdYalUqQJgcyrV3MjCYvYOKtOXLy9eubCWQujYTfOT7c+pD07Co+ca3yWkiNMuGOaevh6/ZpbQnc6VeGaGr/MmeHP03Imvy/vlU+vtP+2EK8/pHRVcrWY7mX0OpgsWZsVjYE+fyldnAOo+jV7VDOYmzcE1hPERnnU3xKAWPyXIOHRc8pEQg+D9BocHSIxFcuv0Wr5CoBSmQ7FEzPnCu474rkpGKsuax/QaM1h5ZHkXNiDA2h6aP84o1DOaiE0kGNmAL6qGeM9At2ctqG4qboZADw8URzoGQZo1QYBKRxkj0WBMj4QoWySbDh/d40b5TEmz3u6XSOWNJNol6V8YnAFxqDeV0WZWzDFECvYFbNTIZgWfD5QUeEJ+WVZ6tWchqsu19rtiN6eMb9j7jJBmjg6DsrecU8Qx/8bOaMruoJi4ihPNRgnwfIzE6BZ8kycxkp0e8SVdFNlYTdNHt4yfY7bqwUQA== X-Bogosity: Ham, tests=bogofilter, spamicity=0.024304, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: This patch creates a config for shadow stack support and landing pad instr support. Shadow stack support and landing instr support can be enabled by selecting `CONFIG_RISCV_USER_CFI`. Selecting `CONFIG_RISCV_USER_CFI` wires up path to enumerate CPU support and if cpu support exists, kernel will support cpu assisted user mode cfi. If CONFIG_RISCV_USER_CFI is selected, select `ARCH_USES_HIGH_VMA_FLAGS` and `ARCH_HAS_USER_SHADOW_STACK` for riscv. Signed-off-by: Deepak Gupta --- arch/riscv/Kconfig | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/arch/riscv/Kconfig b/arch/riscv/Kconfig index d1d629a3eb91..24bf08c905d2 100644 --- a/arch/riscv/Kconfig +++ b/arch/riscv/Kconfig @@ -231,6 +231,25 @@ config ARCH_HAS_BROKEN_DWARF5 # https://github.com/llvm/llvm-project/commit/7ffabb61a5569444b5ac9322e22e5471cc5e4a77 depends on LD_IS_LLD && LLD_VERSION < 180000 +config RISCV_USER_CFI + def_bool y + bool "riscv userspace control flow integrity" + depends on 64BIT && $(cc-option,-mabi=lp64 -march=rv64ima_zicfiss) + depends on RISCV_ALTERNATIVE + select ARCH_HAS_USER_SHADOW_STACK + select ARCH_USES_HIGH_VMA_FLAGS + help + Provides CPU assisted control flow integrity to userspace tasks. + Control flow integrity is provided by implementing shadow stack for + backward edge and indirect branch tracking for forward edge in program. + Shadow stack protection is a hardware feature that detects function + return address corruption. This helps mitigate ROP attacks. + Indirect branch tracking enforces that all indirect branches must land + on a landing pad instruction else CPU will fault. This mitigates against + JOP / COP attacks. Applications must be enabled to use it, and old user- + space does not get protection "for free". + default y + config ARCH_MMAP_RND_BITS_MIN default 18 if 64BIT default 8 From patchwork Thu Sep 12 23:16:47 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13802851 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 E7ADCEEE271 for ; Thu, 12 Sep 2024 23:19:12 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 79E586B00C3; Thu, 12 Sep 2024 19:19:12 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 74DA16B00C4; Thu, 12 Sep 2024 19:19:12 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 57A436B00C5; Thu, 12 Sep 2024 19:19:12 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 340C86B00C3 for ; Thu, 12 Sep 2024 19:19:12 -0400 (EDT) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id E0FC9140C8B for ; Thu, 12 Sep 2024 23:19:11 +0000 (UTC) X-FDA: 82557654102.18.01308C8 Received: from mail-pl1-f175.google.com (mail-pl1-f175.google.com [209.85.214.175]) by imf12.hostedemail.com (Postfix) with ESMTP id 169204000A for ; Thu, 12 Sep 2024 23:19:09 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=buAzX6He; spf=pass (imf12.hostedemail.com: domain of debug@rivosinc.com designates 209.85.214.175 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=1726183097; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=Iyonew7njYYUDgOFlccFcLDkYmWknDQSdFdIbsgjEzE=; b=n6LuS33vzDK/1LAYDuNYJxTx+qjqwqu+7iclvvhSvNuwjazpmW3Sp5cfoxnEcftovuTTp8 Ckavo2xTzKhA6ewgjcucRn+HIo7yKfyIXgnKWjQ1SR5FEsC1WEJfoX0i12RmyMsbblEpkG CRUBqaRyeYE/r+O8Bg2FdzMrLfVWST8= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=buAzX6He; spf=pass (imf12.hostedemail.com: domain of debug@rivosinc.com designates 209.85.214.175 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1726183097; a=rsa-sha256; cv=none; b=Gw9zjXunpARi+Rg8nD6z51TfFemTuUd1HHqbn5ORbau8sezCNJVebYmoOwTItMYYE6NBC1 lO+H9hZOvhmfGRPHiIedhQUgcib1KpNZIdMPCXbamG9k6OHjZbggBAU4Z5tFK89H7uxZ76 ZgMSwqZPv8yfaEmJO28X47FjE1HiF80= Received: by mail-pl1-f175.google.com with SMTP id d9443c01a7336-206aee40676so2682205ad.0 for ; Thu, 12 Sep 2024 16:19:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1726183149; x=1726787949; darn=kvack.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=Iyonew7njYYUDgOFlccFcLDkYmWknDQSdFdIbsgjEzE=; b=buAzX6HeVU8OkldaXeUdFdVpP2jyfgvyxtEt//bN490O3JFR8q3KtVxbIw0blUr8Ll 4/WgPRLnIAMMwlViMeihsFkcLFSwNBCQ/hRsyL8jnmFzv3V/p3zlUER2G4R5VK+3f14o IdwuKU1YFxHyFJzCpklQvWqA6eCJwwav2aallBHmZ9psQR69pNEMgEWQs8z7WK8fZMdL ldVPl9NMk/qNdveG0uXKrxv8kWtPS7/pvpI7OJxoOeZT2jTg+BPmPd+dGqZ9xcx6d2Ri oCqYum/sLxxOU7JJlM2y4mn35Tc5D6m52j5ZwjHlZoqaE1INuEU4sJIVD4+0fg9s3GvY suQA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726183149; x=1726787949; 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=Iyonew7njYYUDgOFlccFcLDkYmWknDQSdFdIbsgjEzE=; b=ZeOncmFJBRtE1zNTc8NLHU+dvACcc+oUqoQu6udgXbYFpy2HsGW3GOvvvLd/0ApqiI VhdF7kpTLtvsTsJaInZxj4X/rlQN2Rg2Ao3gzaJ0hE3qZhI4886AsbmLMgPxX6p6TlRy plnWzpXJvbXZLlYX5lu80EVs5H8lJNdxxonl4uq8tlAFrVvZ06Ov0RVPp78szG9GOtRy O/pXuA+zIVSOvHF+y0OeMPC0kITQaugbnKyB0zAWGGY3Vn7SX+sUvecKs7K+qIJhQvUQ xG/NgYG4OXQlyhbQPmpK1d4Lg/lJILyF8QPOsGq36FQwtyJZCnXY3q90MfyIVSDW9pi7 lkWA== X-Forwarded-Encrypted: i=1; AJvYcCXUZb+QYSXtoSezhAQjfRrv5PxOmH0FbLNykZXB4JWZ3SrQ5EKjD7mkBU+Asxqm5RDpRk7krHDlCQ==@kvack.org X-Gm-Message-State: AOJu0YzKasn2HAXzGJG3ms4ZH0GA5P0DvGaxO1f+zNGagO0tfATAZdz2 zsd5pOL3M3QUy0Kd81ClE7Ow3yjAVYqynDtEvIBkmHnGQ/HjyfSGiXAiadP/jto= X-Google-Smtp-Source: AGHT+IFQFXeegIq20jMre9WaWvSrfHhLDrwK4rARP/3T0r/5M87gFpvdLVA9mkxvLCR99S6cpVQrqw== X-Received: by 2002:a17:90a:4b87:b0:2d8:f0b4:9acb with SMTP id 98e67ed59e1d1-2dbb9f08179mr1017556a91.34.1726183148685; Thu, 12 Sep 2024 16:19:08 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2db6c1ac69asm3157591a91.0.2024.09.12.16.19.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 12 Sep 2024 16:19:08 -0700 (PDT) From: Deepak Gupta To: paul.walmsley@sifive.com, palmer@sifive.com, conor@kernel.org, linux-doc@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kselftest@vger.kernel.org Cc: corbet@lwn.net, palmer@dabbelt.com, aou@eecs.berkeley.edu, robh@kernel.org, krzk+dt@kernel.org, oleg@redhat.com, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, hpa@zytor.com, peterz@infradead.org, akpm@linux-foundation.org, arnd@arndb.de, ebiederm@xmission.com, kees@kernel.org, Liam.Howlett@oracle.com, vbabka@suse.cz, lorenzo.stoakes@oracle.com, shuah@kernel.org, brauner@kernel.org, samuel.holland@sifive.com, debug@rivosinc.com, andy.chiu@sifive.com, jerry.shih@sifive.com, greentime.hu@sifive.com, charlie@rivosinc.com, evan@rivosinc.com, cleger@rivosinc.com, xiao.w.wang@intel.com, ajones@ventanamicro.com, anup@brainfault.org, mchitale@ventanamicro.com, atishp@rivosinc.com, sameo@rivosinc.com, bjorn@rivosinc.com, alexghiti@rivosinc.com, david@redhat.com, libang.li@antgroup.com, jszhang@kernel.org, leobras@redhat.com, guoren@kernel.org, samitolvanen@google.com, songshuaishuai@tinylab.org, costa.shul@redhat.com, bhe@redhat.com, zong.li@sifive.com, puranjay@kernel.org, namcaov@gmail.com, antonb@tenstorrent.com, sorear@fastmail.com, quic_bjorande@quicinc.com, ancientmodern4@gmail.com, ben.dooks@codethink.co.uk, quic_zhonhan@quicinc.com, cuiyunhui@bytedance.com, yang.lee@linux.alibaba.com, ke.zhao@shingroup.cn, sunilvl@ventanamicro.com, tanzhasanwork@gmail.com, schwab@suse.de, dawei.li@shingroup.cn, rppt@kernel.org, willy@infradead.org, usama.anjum@collabora.com, osalvador@suse.de, ryan.roberts@arm.com, andrii@kernel.org, alx@kernel.org, catalin.marinas@arm.com, broonie@kernel.org, revest@chromium.org, bgray@linux.ibm.com, deller@gmx.de, zev@bewilderbeest.net Subject: [PATCH v4 28/30] riscv: Documentation for landing pad / indirect branch tracking Date: Thu, 12 Sep 2024 16:16:47 -0700 Message-ID: <20240912231650.3740732-29-debug@rivosinc.com> X-Mailer: git-send-email 2.45.0 In-Reply-To: <20240912231650.3740732-1-debug@rivosinc.com> References: <20240912231650.3740732-1-debug@rivosinc.com> MIME-Version: 1.0 X-Rspamd-Server: rspam03 X-Rspam-User: X-Rspamd-Queue-Id: 169204000A X-Stat-Signature: 5c68xjzb9tuhc7f1o1fmy3opj8mcbdrs X-HE-Tag: 1726183149-527378 X-HE-Meta: U2FsdGVkX18T2ZGzgbTHvy730fnmSqtuCtp0K18AFrwdgI6NWBgzjb7R3mQc2s449c8mqoZPpI/sir3Z+DDctE1kgZDDvpAX8tSxW468Cckwuv3CIhC9KwVkDbOu9J/Czg7z1vTOTFrSA0ToCkkskWuSs6PYlvoewKBn2EqLrKECv4LxvTMGGgBz/h2w/qkNn4tDwSZZhF8bxYjPEsZ523M1O5zE+3xc2I9Z2XCi7zArivf20TjeDvCwmeyaT1OSxq7j+3FNzZKs8Ih8+T+RQ61ST66SbWdfbd51wDimXE0cdmap08v5QwRRlkbrlDNFUupoJ8Tqrw5I8EEaRPTz249j9ez8S9CtvOf6tAP6gFwPYIFzhal6puNokEnLqaMQEB8IhvAT8O6iq9mlbg+daXxRjUJowr6auOQ95l2OM1BhRcmb3j6qdXvMPKgZ2N4WJposBZdELpCwuIdDddHmb9B07ZSyySjQlxuLRa/s4870XzVZVIx2OrMGzM0WnlGhmVT2JFdL+jFxHz6rcgQI7CsAqQhPMpEaJGFlrb3RwjvgzHw0tb8WxEzGH6UnSbrzSbL2P61iXcFTypB2bLZV8kSMoUidOPWMAwXbHESM1Hb0PO/QBUsfirZOZhhZ5J6HibNBwPvLTfePPKje80k2GLChXFQ1gd7UCurPoN2DHiBywkU+A8Lcv4hWGLQ3vQk1zRyH1+y3QGBccXwvX+yWrNiD7w6nkz9jLGlEqzI+1tfmmz1R+Tsty07mGpEv3xcrsYopnBJt6cVGE4+9y1IQZQbktPTXutC6KJ+X6HQxTeRdRSwwpsXre9x2G3lPc71V8g24gNLw1k8hxFLELajt0LOeBPllZoARbWhPC+Sw7+Ni+DmjM0/ts7pXbZm7oOtj6cBftwE/Ans6en4k4l0KJelV2mPj7NUsyCoECC+aAwSgObiUZo7tYyDbA+Nh/HDDYWEV55iA5rV8gH3LxiX Fgpei466 rDQRzNkP6v1Zp5xr4omJ8glWepXma8nhdSHyS8ZlRUM88Dd6vZwJVPa6rsi8S/IlZLzcSIrRbSJVw0QQBtkWJGPRadOY7LlhgD8QA9kjFpOQK5Qsy7Sv2aaBJwVbz95nPeiQp7/t3mOxsG+pziA/NrPGjok8IIp8CtWX28UDADLnw5fWWKUQTe8NF9BpaKXpC4t9y4ZDGu0f3I5bf/ulM/+PB3P32gtWOy9enWrhAqOk4SX3qQw7KvZF3c+zOa/xRI5aXHgGwRmx34/6FUPLhgk/H8RZMKLv6p31QMYZerTr1rKs+22+XRFCvviNAfENVt+R13O3zSEfZWy9kb6sSVJNSekGWRFd2Ax8JSfRpCgzLvvyp0XcRG/coSTBwEBpTt9q2D6+d5kXQhtjXplGnWZQK3URTNTQC/RmwGdgYJJIjVoH3RQ8L0AhGFBY/IdNkQLzeDGiQfzwgJuo= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000499, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Adding documentation on landing pad aka indirect branch tracking on riscv and kernel interfaces exposed so that user tasks can enable it. Signed-off-by: Deepak Gupta --- Documentation/arch/riscv/zicfilp.rst | 104 +++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 Documentation/arch/riscv/zicfilp.rst diff --git a/Documentation/arch/riscv/zicfilp.rst b/Documentation/arch/riscv/zicfilp.rst new file mode 100644 index 000000000000..23013ee711ac --- /dev/null +++ b/Documentation/arch/riscv/zicfilp.rst @@ -0,0 +1,104 @@ +.. SPDX-License-Identifier: GPL-2.0 + +:Author: Deepak Gupta +:Date: 12 January 2024 + +==================================================== +Tracking indirect control transfers on RISC-V Linux +==================================================== + +This document briefly describes the interface provided to userspace by Linux +to enable indirect branch tracking for user mode applications on RISV-V + +1. Feature Overview +-------------------- + +Memory corruption issues usually result in to crashes, however when in hands of +an adversary and if used creatively can result into variety security issues. + +One of those security issues can be code re-use attacks on program where adversary +can use corrupt function pointers and chain them together to perform jump oriented +programming (JOP) or call oriented programming (COP) and thus compromising control +flow integrity (CFI) of the program. + +Function pointers live in read-write memory and thus are susceptible to corruption +and allows an adversary to reach any program counter (PC) in address space. On +RISC-V zicfilp extension enforces a restriction on such indirect control transfers + + - indirect control transfers must land on a landing pad instruction `lpad`. + There are two exception to this rule + - rs1 = x1 or rs1 = x5, i.e. a return from a function and returns are + protected using shadow stack (see zicfiss.rst) + + - rs1 = x7. On RISC-V compiler usually does below to reach function + which is beyond the offset possible J-type instruction. + + "auipc x7, " + "jalr (x7)" + + Such form of indirect control transfer are still immutable and don't rely + on memory and thus rs1=x7 is exempted from tracking and considered software + guarded jumps. + +`lpad` instruction is pseudo of `auipc rd, ` with `rd=x0`` and is a HINT +nop. `lpad` instruction must be aligned on 4 byte boundary and compares 20 bit +immediate withx7. If `imm_20bit` == 0, CPU don't perform any comparision with x7. If +`imm_20bit` != 0, then `imm_20bit` must match x7 else CPU will raise +`software check exception` (cause=18)with `*tval = 2`. + +Compiler can generate a hash over function signatures and setup them (truncated +to 20bit) in x7 at callsites and function prologues can have `lpad` with same +function hash. This further reduces number of program counters a call site can +reach. + +2. ELF and psABI +----------------- + +Toolchain sets up `GNU_PROPERTY_RISCV_FEATURE_1_FCFI` for property +`GNU_PROPERTY_RISCV_FEATURE_1_AND` in notes section of the object file. + +3. Linux enabling +------------------ + +User space programs can have multiple shared objects loaded in its address space +and it's a difficult task to make sure all the dependencies have been compiled +with support of indirect branch. Thus it's left to dynamic loader to enable +indirect branch tracking for the program. + +4. prctl() enabling +-------------------- + +`PR_SET_INDIR_BR_LP_STATUS` / `PR_GET_INDIR_BR_LP_STATUS` / +`PR_LOCK_INDIR_BR_LP_STATUS` are three prctls added to manage indirect branch +tracking. prctls are arch agnostic and returns -EINVAL on other arches. + +`PR_SET_INDIR_BR_LP_STATUS`: If arg1 `PR_INDIR_BR_LP_ENABLE` and if CPU supports +`zicfilp` then kernel will enabled indirect branch tracking for the task. +Dynamic loader can issue this `prctl` once it has determined that all the objects +loaded in address space support indirect branch tracking. Additionally if there is +a `dlopen` to an object which wasn't compiled with `zicfilp`, dynamic loader can +issue this prctl with arg1 set to 0 (i.e. `PR_INDIR_BR_LP_ENABLE` being clear) + +`PR_GET_INDIR_BR_LP_STATUS`: Returns current status of indirect branch tracking. +If enabled it'll return `PR_INDIR_BR_LP_ENABLE` + +`PR_LOCK_INDIR_BR_LP_STATUS`: Locks current status of indirect branch tracking on +the task. User space may want to run with strict security posture and wouldn't want +loading of objects without `zicfilp` support in it and thus would want to disallow +disabling of indirect branch tracking. In that case user space can use this prctl +to lock current settings. + +5. violations related to indirect branch tracking +-------------------------------------------------- + +Pertaining to indirect branch tracking, CPU raises software check exception in +following conditions + - missing `lpad` after indirect call / jmp + - `lpad` not on 4 byte boundary + - `imm_20bit` embedded in `lpad` instruction doesn't match with `x7` + +In all 3 cases, `*tval = 2` is captured and software check exception is raised +(cause=18) + +Linux kernel will treat this as `SIGSEV`` with code = `SEGV_CPERR` and follow +normal course of signal delivery. From patchwork Thu Sep 12 23:16:48 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13802852 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 0A2E0EEE270 for ; Thu, 12 Sep 2024 23:19:17 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 97EC26B00C4; Thu, 12 Sep 2024 19:19:16 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 9232A6B00C6; Thu, 12 Sep 2024 19:19:16 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 701016B00C7; Thu, 12 Sep 2024 19:19:16 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 52C986B00C4 for ; Thu, 12 Sep 2024 19:19:16 -0400 (EDT) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 103651C52D6 for ; Thu, 12 Sep 2024 23:19:16 +0000 (UTC) X-FDA: 82557654312.03.B314D4A Received: from mail-pj1-f54.google.com (mail-pj1-f54.google.com [209.85.216.54]) by imf10.hostedemail.com (Postfix) with ESMTP id 37C76C000D for ; Thu, 12 Sep 2024 23:19:14 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=veRik2Mw; spf=pass (imf10.hostedemail.com: domain of debug@rivosinc.com designates 209.85.216.54 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=1726183048; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=dA4GtqjPLbFcczpyYhfz2PBDm+Kw3MITc0s5Cuc65RY=; b=E5h2fIx8i1rB7NUrDBYpy8gbWwKzkKoNxktrnRu9DKYODblK7BwKKNLQH36/kEXOS7kWX+ KoC1SRiOAPRNpiRwaoQagCBkYbbi87ZHyex+GnoudVtS4nepl7bD25LeLyL3fW6oftPCI0 H5Fv29hXgUMVEYp2fi4Ef8xe165tkJ4= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1726183048; a=rsa-sha256; cv=none; b=pHazCDrB5X4XymbkmncbQqgyVzdTZ2E9OQY/QQ8muWuDZh4kzWDfZFaPAqxg8v01MUU11P Sdk7WorJHYK7GV5b08llrc4NOD+8SP3aaPk/qC58txk1wbEoG8sOzMwK44b02N9f3HJfOs RsaBAKYOcCIBxfj9NuGbCS8mgrUr4m4= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=veRik2Mw; spf=pass (imf10.hostedemail.com: domain of debug@rivosinc.com designates 209.85.216.54 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none Received: by mail-pj1-f54.google.com with SMTP id 98e67ed59e1d1-2d87176316eso1958438a91.0 for ; Thu, 12 Sep 2024 16:19:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1726183153; x=1726787953; darn=kvack.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=dA4GtqjPLbFcczpyYhfz2PBDm+Kw3MITc0s5Cuc65RY=; b=veRik2MwtGw7Yn44e4dEgc4T2r79q5JLjeSF2KzKbxnOK+LEGpDt5TUr8dy4mxGax0 8wQ2DOhBA11irTKBunMWGydfg6K2wCYCuVVTxhTnwpeF0gDlHhQRdtrOVnnwmav6s8aT PtiOpJpHct1j12CrBBNbmlXxC2REWYqGwkyPJHEWBPXNVPPXKHpefD5eaWnV90wueGoy 3SG5Elr1QGeWc6Cj9x7G9cNMuubfn9vWEhnyFPY0lx6HRK3JLhEz/o5wTSDCqDGEPp5L AYv3DMIGj7e7kOSmFyxMGVF20TaY9Q5RW4AxiG8nt3/vXEJGLowIAu4Cu131W5/45kz0 EEKg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726183153; x=1726787953; 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=dA4GtqjPLbFcczpyYhfz2PBDm+Kw3MITc0s5Cuc65RY=; b=kj3s9v7mMZPcn6diqx184NIhmxteQiLjn4D04S5d39+8egx33kK0RMyBd/sdgiFJzm cszkwVnzVcqBh049y64O7zKDv9idDdWHLGrBJjNFOvi3sDxJHjW9rxhRd9BJOobq8qvb ZHEO+0tnAyEXLSlzzr+I3vFiLHu5p0P9PNl3+v/N8C7BVsC6YwKNHz5iAbKOz1COObNb m2BXDruWRUTNhfVC0kC+cB7p/nZ0L/jQqJHBmBFJMLQxDPfyonIFtYZBRkBKNkRF7bEW CmrgyOSzUjPGUmGEQgs1qirEUnuKH2clyDImb7p8kqAz2/Xo2m1PYwrMzvmrji2infkC p+Og== X-Forwarded-Encrypted: i=1; AJvYcCVUnJwCFl6Loy3pR0fVPQJMZ4P0zVpGfDdV0Kie8a6wfGf3TK7J3LugD0+c3m4+5Fex8iL0+ZsHeQ==@kvack.org X-Gm-Message-State: AOJu0YxmYObBgG6PIhfNIE2pWsNHbWN8e6viy0xmXRsBhHJDnB5tphVb YOgHFP2m18PRuwT8WHRuq/P0CObbqYqo63zpufNrDBGi3G9h6AIZGikg4M53f7Y= X-Google-Smtp-Source: AGHT+IFssDwDGbEBDsElM2G4SM2jHhtYY+v60zMZaes5wDvpv3V2oV9g/OYP3KitPGsedCggtBiS7g== X-Received: by 2002:a17:90a:8a04:b0:2d8:aa5d:5856 with SMTP id 98e67ed59e1d1-2db9fc6e916mr7388901a91.11.1726183152820; Thu, 12 Sep 2024 16:19:12 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2db6c1ac69asm3157591a91.0.2024.09.12.16.19.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 12 Sep 2024 16:19:12 -0700 (PDT) From: Deepak Gupta To: paul.walmsley@sifive.com, palmer@sifive.com, conor@kernel.org, linux-doc@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kselftest@vger.kernel.org Cc: corbet@lwn.net, palmer@dabbelt.com, aou@eecs.berkeley.edu, robh@kernel.org, krzk+dt@kernel.org, oleg@redhat.com, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, hpa@zytor.com, peterz@infradead.org, akpm@linux-foundation.org, arnd@arndb.de, ebiederm@xmission.com, kees@kernel.org, Liam.Howlett@oracle.com, vbabka@suse.cz, lorenzo.stoakes@oracle.com, shuah@kernel.org, brauner@kernel.org, samuel.holland@sifive.com, debug@rivosinc.com, andy.chiu@sifive.com, jerry.shih@sifive.com, greentime.hu@sifive.com, charlie@rivosinc.com, evan@rivosinc.com, cleger@rivosinc.com, xiao.w.wang@intel.com, ajones@ventanamicro.com, anup@brainfault.org, mchitale@ventanamicro.com, atishp@rivosinc.com, sameo@rivosinc.com, bjorn@rivosinc.com, alexghiti@rivosinc.com, david@redhat.com, libang.li@antgroup.com, jszhang@kernel.org, leobras@redhat.com, guoren@kernel.org, samitolvanen@google.com, songshuaishuai@tinylab.org, costa.shul@redhat.com, bhe@redhat.com, zong.li@sifive.com, puranjay@kernel.org, namcaov@gmail.com, antonb@tenstorrent.com, sorear@fastmail.com, quic_bjorande@quicinc.com, ancientmodern4@gmail.com, ben.dooks@codethink.co.uk, quic_zhonhan@quicinc.com, cuiyunhui@bytedance.com, yang.lee@linux.alibaba.com, ke.zhao@shingroup.cn, sunilvl@ventanamicro.com, tanzhasanwork@gmail.com, schwab@suse.de, dawei.li@shingroup.cn, rppt@kernel.org, willy@infradead.org, usama.anjum@collabora.com, osalvador@suse.de, ryan.roberts@arm.com, andrii@kernel.org, alx@kernel.org, catalin.marinas@arm.com, broonie@kernel.org, revest@chromium.org, bgray@linux.ibm.com, deller@gmx.de, zev@bewilderbeest.net Subject: [PATCH v4 29/30] riscv: Documentation for shadow stack on riscv Date: Thu, 12 Sep 2024 16:16:48 -0700 Message-ID: <20240912231650.3740732-30-debug@rivosinc.com> X-Mailer: git-send-email 2.45.0 In-Reply-To: <20240912231650.3740732-1-debug@rivosinc.com> References: <20240912231650.3740732-1-debug@rivosinc.com> MIME-Version: 1.0 X-Stat-Signature: 3pxy6dqcwqgwrrudj6cog3dtfwgp8h8m X-Rspamd-Queue-Id: 37C76C000D X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1726183154-348121 X-HE-Meta: U2FsdGVkX18lW4i2NA0Yu/7mWXBx/VawPxj0DG6XwrJru+CGn9lygAvBHGwKCuf9mxTR0JFnsizTUU/lukbXRyXzACv38a9sQdOX3Vtlj1dwghwznGhqNdcBn1MIBZYIDyroU0Vw8h+EibxvKaiBr7SDQnO74Fq2dKubhRRi1Xb1ZbK/AYvz17R2NZcUV2MOa1yPdPnUyl5kPRgaPQyGMes9JLJDBnPBLV6ZgJIiGg8N4n01wy76lKQhGiSf003m7reNn012+UwTa76oDz6emqEc5EwVcTiUMf4gBsCdeMaG/VcfPKIbf5IoMmy1PUtRGu4/BCiCdbx5LlGb1i2tgU6f/dJkxVZfKNu0AwydRPH/jZLEcW8fX5C3HKTj/3evHV4ATblVgOoRC5iHLq6GxbzBzjrj/O395grTkyggeJDBtMOqdeDBh4TIin+/I/GvtPhjAM/kuUWIpEqXsA//q/jqjakUdcyC4bLvejfNLj0iUALYtnoasCir+sXE43Mu/+dvy5Xa5es5HqA8dNkG4T8jm+GYgpiGgljYxI5fjnnN/AtFCNyCpo8y78D0n/7Ejw3jvXUP9qOI85IuqSW1ue/uetWZERV3F8g4NlLwGZKidHZ7KA+K+tYrae80UABjKjUF/GINLDl9/S6geCjNT8OAd5dD/brOykoVh3WrZEKlf08L538aIsKI2K/cv/72XXvzETC1eYZy5PnKg9WaujHAhdMTXLPnO6GWWWPYjIPVFy5WtqavwsJ6EBEqDgAeiFnwLz6BZ5IJ4HUyzo7fJP/SUIvhv2v4Kx26RhLGSYuLlg/PN9Uln5cN2JyqZWM8b2n/hiUFII+U+SxuV5W4qRIXboz5t12gpO4k6tIpYY+JLcrpNw7CxVhzKkt0/J9Vqu7R5crpi9vnMhHN0luN2nB9dVOvBh+D19mCQT6TwH2DHl5UAdVXvK95LP2nXL9qSArZX6n6jCn93ZSCkpu s0gPPECX JgAlBoaJrOLPtivRV0iU9UKKs8ibZnhKIaAbiBaPDQqBdPIlRzxBUQu6H/1FdVdCuSiJn3HKA+FIaLXXuP357GZzmqsEYoX661jPrH3cRsQMQ1Sqk6a/qHiJD32Vs7o6PwJXpSRqZkFusbUdt5PJrx2Za9lzZTgckWWZp71hJgnMm7Bz/Fc3EDAk0hqzupsOhS4gGFXrG2DDGreRpJXt9g/H1LGffDNWyRpcsUxIGBPcYsrcbL+JRRShTpQlX2ntU1FvEyhvgtAPjo8nylSmMOsc6GHF2+DinIXka7F4mj90MDxhJd6qb9RmVVe8jgYyrLZUwy+aNq6RdQdX2xhtTBS3P3WH3BAwK9n2C41hWfF39YRNsyjqQlezuAgzSYi8p+KYc7DghbMkafwfZjiRty8dDNODo/UJLRYID1DqVRed4tU0r4UvxMfWgCzxCr5z5rDU5 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: Adding documentation on shadow stack for user mode on riscv and kernel interfaces exposed so that user tasks can enable it. Signed-off-by: Deepak Gupta --- Documentation/arch/riscv/zicfiss.rst | 169 +++++++++++++++++++++++++++ 1 file changed, 169 insertions(+) create mode 100644 Documentation/arch/riscv/zicfiss.rst diff --git a/Documentation/arch/riscv/zicfiss.rst b/Documentation/arch/riscv/zicfiss.rst new file mode 100644 index 000000000000..f133b6af9c15 --- /dev/null +++ b/Documentation/arch/riscv/zicfiss.rst @@ -0,0 +1,169 @@ +.. SPDX-License-Identifier: GPL-2.0 + +:Author: Deepak Gupta +:Date: 12 January 2024 + +========================================================= +Shadow stack to protect function returns on RISC-V Linux +========================================================= + +This document briefly describes the interface provided to userspace by Linux +to enable shadow stack for user mode applications on RISV-V + +1. Feature Overview +-------------------- + +Memory corruption issues usually result in to crashes, however when in hands of +an adversary and if used creatively can result into variety security issues. + +One of those security issues can be code re-use attacks on program where adversary +can use corrupt return addresses present on stack and chain them together to perform +return oriented programming (ROP) and thus compromising control flow integrity (CFI) +of the program. + +Return addresses live on stack and thus in read-write memory and thus are +susceptible to corruption and allows an adversary to reach any program counter +(PC) in address space. On RISC-V `zicfiss` extension provides an alternate stack +`shadow stack` on which return addresses can be safely placed in prolog of the +function and retrieved in epilog. `zicfiss` extension makes following changes + + - PTE encodings for shadow stack virtual memory + An earlier reserved encoding in first stage translation i.e. + PTE.R=0, PTE.W=1, PTE.X=0 becomes PTE encoding for shadow stack pages. + + - `sspush x1/x5` instruction pushes (stores) `x1/x5` to shadow stack. + + - `sspopchk x1/x5` instruction pops (loads) from shadow stack and compares + with `x1/x5` and if un-equal, CPU raises `software check exception` with + `*tval = 3` + +Compiler toolchain makes sure that function prologs have `sspush x1/x5` to save return +address on shadow stack in addition to regular stack. Similarly function epilogs have +`ld x5, offset(x2)`; `sspopchk x5` to ensure that popped value from regular stack +matches with popped value from shadow stack. + +2. Shadow stack protections and linux memory manager +----------------------------------------------------- + +As mentioned earlier, shadow stack get new page table encodings and thus have some +special properties assigned to them and instructions that operate on them as below + + - Regular stores to shadow stack memory raises access store faults. + This way shadow stack memory is protected from stray inadvertant + writes + + - Regular loads to shadow stack memory are allowed. + This allows stack trace utilities or backtrace functions to read + true callstack (not tampered) + + - Only shadow stack instructions can generate shadow stack load or + shadow stack store. + + - Shadow stack load / shadow stack store on read-only memory raises + AMO/store page fault. Thus both `sspush x1/x5` and `sspopchk x1/x5` + will raise AMO/store page fault. This simplies COW handling in kernel + During fork, kernel can convert shadow stack pages into read-only + memory (as it does for regular read-write memory) and as soon as + subsequent `sspush` or `sspopchk` in userspace is encountered, then + kernel can perform COW. + + - Shadow stack load / shadow stack store on read-write, read-write- + execute memory raises an access fault. This is a fatal condition + because shadow stack should never be operating on read-write, read- + write-execute memory. + +3. ELF and psABI +----------------- + +Toolchain sets up `GNU_PROPERTY_RISCV_FEATURE_1_BCFI` for property +`GNU_PROPERTY_RISCV_FEATURE_1_AND` in notes section of the object file. + +4. Linux enabling +------------------ + +User space programs can have multiple shared objects loaded in its address space +and it's a difficult task to make sure all the dependencies have been compiled +with support of shadow stack. Thus it's left to dynamic loader to enable +shadow stack for the program. + +5. prctl() enabling +-------------------- + +`PR_SET_SHADOW_STACK_STATUS` / `PR_GET_SHADOW_STACK_STATUS` / +`PR_LOCK_SHADOW_STACK_STATUS` are three prctls added to manage shadow stack +enabling for tasks. prctls are arch agnostic and returns -EINVAL on other arches. + +`PR_SET_SHADOW_STACK_STATUS`: If arg1 `PR_SHADOW_STACK_ENABLE` and if CPU supports +`zicfiss` then kernel will enable shadow stack for the task. Dynamic loader can +issue this `prctl` once it has determined that all the objects loaded in address +space have support for shadow stack. Additionally if there is a `dlopen` to an +object which wasn't compiled with `zicfiss`, dynamic loader can issue this prctl +with arg1 set to 0 (i.e. `PR_SHADOW_STACK_ENABLE` being clear) + +`PR_GET_SHADOW_STACK_STATUS`: Returns current status of indirect branch tracking. +If enabled it'll return `PR_SHADOW_STACK_ENABLE` + +`PR_LOCK_SHADOW_STACK_STATUS`: Locks current status of shadow stack enabling on the +task. User space may want to run with strict security posture and wouldn't want +loading of objects without `zicfiss` support in it and thus would want to disallow +disabling of shadow stack on current task. In that case user space can use this prctl +to lock current settings. + +5. violations related to returns with shadow stack enabled +----------------------------------------------------------- + +Pertaining to shadow stack, CPU raises software check exception in following +condition + + - On execution of `sspopchk x1/x5`, x1/x5 didn't match top of shadow stack. + If mismatch happens then cpu does `*tval = 3` and raise software check + exception + +Linux kernel will treat this as `SIGSEV`` with code = `SEGV_CPERR` and follow +normal course of signal delivery. + +6. Shadow stack tokens +----------------------- +Regular stores on shadow stacks are not allowed and thus can't be tampered with via +arbitrary stray writes due to bugs. Method of pivoting / switching to shadow stack +is simply writing to csr `CSR_SSP` changes active shadow stack. This can be problematic +because usually value to be written to `CSR_SSP` will be loaded somewhere in writeable +memory and thus allows an adversary to corruption bug in software to pivot to an any +address in shadow stack range. Shadow stack tokens can help mitigate this problem by +making sure that: + + - When software is switching away from a shadow stack, shadow stack pointer should be + saved on shadow stack itself and call it `shadow stack token` + + - When software is switching to a shadow stack, it should read the `shadow stack token` + from shadow stack pointer and verify that `shadow stack token` itself is pointer to + shadow stack itself. + + - Once the token verification is done, software can perform the write to `CSR_SSP` to + switch shadow stack. + +Here software can be user mode task runtime itself which is managing various contexts +as part of single thread. Software can be kernel as well when kernel has to deliver a +signal to user task and must save shadow stack pointer. Kernel can perform similar +procedure by saving a token on user shadow stack itself. This way whenever sigreturn +happens, kernel can read the token and verify the token and then switch to shadow stack. +Using this mechanism, kernel helps user task so that any corruption issue in user task +is not exploited by adversary by arbitrarily using `sigreturn`. Adversary will have to +make sure that there is a `shadow stack token` in addition to invoking `sigreturn` + +7. Signal shadow stack +----------------------- +Following structure has been added to sigcontext for RISC-V. `rsvd` field has been kept +in case we need some extra information in future for landing pads / indirect branch +tracking. It has been kept today in order to allow backward compatibility in future. + +struct __sc_riscv_cfi_state { + unsigned long ss_ptr; + unsigned long rsvd; +}; + +As part of signal delivery, shadow stack token is saved on current shadow stack itself and +updated pointer is saved away in `ss_ptr` field in `__sc_riscv_cfi_state` under `sigcontext` +Existing shadow stack allocation is used for signal delivery. During `sigreturn`, kernel will +obtain `ss_ptr` from `sigcontext` and verify the saved token on shadow stack itself and switch +shadow stack. From patchwork Thu Sep 12 23:16:49 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13802853 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 807ECEEE270 for ; Thu, 12 Sep 2024 23:19:21 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0E8B86B00C9; Thu, 12 Sep 2024 19:19:21 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 0714A6B00C8; Thu, 12 Sep 2024 19:19:21 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DB8A56B00C9; Thu, 12 Sep 2024 19:19:20 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id B6B746B00C6 for ; Thu, 12 Sep 2024 19:19:20 -0400 (EDT) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 6A04C40E10 for ; Thu, 12 Sep 2024 23:19:20 +0000 (UTC) X-FDA: 82557654480.17.2502BE4 Received: from mail-pg1-f174.google.com (mail-pg1-f174.google.com [209.85.215.174]) by imf21.hostedemail.com (Postfix) with ESMTP id 86DC81C000E for ; Thu, 12 Sep 2024 23:19:18 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=aUbZeW2v; dmarc=none; spf=pass (imf21.hostedemail.com: domain of debug@rivosinc.com designates 209.85.215.174 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1726183041; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=IVdPJDxrp4GQLJ67J34Jd5yxy8wiSM/nJ+ybt6ixXzo=; b=iIpC3FEmhXsEurJG5TBmiCpX4kbQENun07XetWYjUVsca51vBqOfa+1Uws1IgjTS1YJHZS yCzoDRiOHHlqZ6JQEbHtc38bbKufrv0pISEJ/DWqwROaUGZ3WRXL+Av9930LoNo2ayQs8E LgLgHZ8Ned6Ie1yQQu61zjj85+EfQJ4= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1726183041; a=rsa-sha256; cv=none; b=H4xpTjvVr9nHc6Ko1FMNJRV6oD8D3lNg8UJIAoD/Z5evqKXXi94V9Yv9rc3B+dQKQHVs+P u7oL0lRJCHTQVOlKRpyGggugM3P+EQU8wq5aptXznbdwhiPYr4G3xf7tkfmHdzpu9YAO5d bl+YryWQsYZAmE0qLQchrECpNRmXg34= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=aUbZeW2v; dmarc=none; spf=pass (imf21.hostedemail.com: domain of debug@rivosinc.com designates 209.85.215.174 as permitted sender) smtp.mailfrom=debug@rivosinc.com Received: by mail-pg1-f174.google.com with SMTP id 41be03b00d2f7-7c6b4222fe3so1017972a12.3 for ; Thu, 12 Sep 2024 16:19:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1726183157; x=1726787957; darn=kvack.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=IVdPJDxrp4GQLJ67J34Jd5yxy8wiSM/nJ+ybt6ixXzo=; b=aUbZeW2vjppGrtDKkWAl/j2XYKKMnb9ON+t6n88xgTQaDKJB33vdOWdBaWkTypBmMm rCyx0ouNIdpSfop9yAocNIFu3F17/ZeSnBFd9tDgDs7vFVuGkUFJ7J6mBHRaas0v6cy+ 1tWHipJvxM/hpS2o3Aht9flj3KU/Dpg7ohbyoU8HQiN/kdWMkvAQ2lK8smQXRidvPycw 8zAWtRivZgoMdWSuVlM0C6WqH9bgkWv5C83QheBxISsTFCmuiWJI3+pH/mLx/v6yNrzw 9EG4JUcudz+SwXmM5a4nO7OySwBXMGxlN3H8DU9o1rS7vEBYQYCXzGAPmmsUkOJC9Bx6 JJQg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726183157; x=1726787957; 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=IVdPJDxrp4GQLJ67J34Jd5yxy8wiSM/nJ+ybt6ixXzo=; b=AniDd+9SB355UhzPhEqugUys2ruUWOZtWWCWlzti6pEkicnhK4iG3mju+RPZKdG1Cv mFPL1GxmClcETInP889ViP5D6hBGS+1OhvR7F5phieGe2pzifbBUyI3PPlAWda+2YKiG /2N0oDqogEosEwDKkW9MJuZ4SGi0KNWEbhX56/A8rp5mXZGtQnaVYKC8iZXi429Cm3L9 cKAxxm37Qwg9R4tqHm8SvWZOBVCvuMeBjKRPyiKNDjWiz9ZV0ea0R+UkxXa88fLoyOGz IjKYJjY391lmsO3qUUPOFp2LVOfL4OVLQw2+f5dAnSzSg02d7eRR7kZsXHskBjxCHeD5 oEng== X-Forwarded-Encrypted: i=1; AJvYcCV/WSjSTFuNVCOJD8eG9tHmEVYjbNxtCJKXb3JJoylucIvdHeFs8/VkW0bwbw8X2lfhpNUc7YNXUg==@kvack.org X-Gm-Message-State: AOJu0Ywv2Etp9IeYaefHltDDLqtTPgeTXqSmPdUwQ7ZsE95rt+GrDC7k Ms8awu6WViJWqge0nxgmy1ei/WDJ7SNnwmxgBJc55gTWPnn1Tc4XWaGBeKsJbro= X-Google-Smtp-Source: AGHT+IFa1E6JbsxaJrJsFmbVg23swDSS4CmQmqqMr+KiY/4/NtDGDjZvIx2bH3fN7YatZbXcXm2b+w== X-Received: by 2002:a17:90a:684c:b0:2d3:d414:4511 with SMTP id 98e67ed59e1d1-2db9ffefa37mr4941681a91.24.1726183157074; Thu, 12 Sep 2024 16:19:17 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2db6c1ac69asm3157591a91.0.2024.09.12.16.19.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 12 Sep 2024 16:19:16 -0700 (PDT) From: Deepak Gupta To: paul.walmsley@sifive.com, palmer@sifive.com, conor@kernel.org, linux-doc@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kselftest@vger.kernel.org Cc: corbet@lwn.net, palmer@dabbelt.com, aou@eecs.berkeley.edu, robh@kernel.org, krzk+dt@kernel.org, oleg@redhat.com, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, hpa@zytor.com, peterz@infradead.org, akpm@linux-foundation.org, arnd@arndb.de, ebiederm@xmission.com, kees@kernel.org, Liam.Howlett@oracle.com, vbabka@suse.cz, lorenzo.stoakes@oracle.com, shuah@kernel.org, brauner@kernel.org, samuel.holland@sifive.com, debug@rivosinc.com, andy.chiu@sifive.com, jerry.shih@sifive.com, greentime.hu@sifive.com, charlie@rivosinc.com, evan@rivosinc.com, cleger@rivosinc.com, xiao.w.wang@intel.com, ajones@ventanamicro.com, anup@brainfault.org, mchitale@ventanamicro.com, atishp@rivosinc.com, sameo@rivosinc.com, bjorn@rivosinc.com, alexghiti@rivosinc.com, david@redhat.com, libang.li@antgroup.com, jszhang@kernel.org, leobras@redhat.com, guoren@kernel.org, samitolvanen@google.com, songshuaishuai@tinylab.org, costa.shul@redhat.com, bhe@redhat.com, zong.li@sifive.com, puranjay@kernel.org, namcaov@gmail.com, antonb@tenstorrent.com, sorear@fastmail.com, quic_bjorande@quicinc.com, ancientmodern4@gmail.com, ben.dooks@codethink.co.uk, quic_zhonhan@quicinc.com, cuiyunhui@bytedance.com, yang.lee@linux.alibaba.com, ke.zhao@shingroup.cn, sunilvl@ventanamicro.com, tanzhasanwork@gmail.com, schwab@suse.de, dawei.li@shingroup.cn, rppt@kernel.org, willy@infradead.org, usama.anjum@collabora.com, osalvador@suse.de, ryan.roberts@arm.com, andrii@kernel.org, alx@kernel.org, catalin.marinas@arm.com, broonie@kernel.org, revest@chromium.org, bgray@linux.ibm.com, deller@gmx.de, zev@bewilderbeest.net Subject: [PATCH v4 30/30] kselftest/riscv: kselftest for user mode cfi Date: Thu, 12 Sep 2024 16:16:49 -0700 Message-ID: <20240912231650.3740732-31-debug@rivosinc.com> X-Mailer: git-send-email 2.45.0 In-Reply-To: <20240912231650.3740732-1-debug@rivosinc.com> References: <20240912231650.3740732-1-debug@rivosinc.com> MIME-Version: 1.0 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 86DC81C000E X-Stat-Signature: 9z3hriszr3epjxkigb7utfuppptc3oar X-Rspam-User: X-HE-Tag: 1726183158-382881 X-HE-Meta: U2FsdGVkX19ToxbgN/xPnUT1BtBVhmGv9cVGk+fKzHYkKoxC51J81On5014NVYhjv6P6lOH98JXdOFyvAYfrhHd54cqvdtptCEYFW65xhiiPQRkAv3wdbOcN9vW0pbeubQFQBoYMHr4dk1mEz5RmdVuHwM2BTB1yN+Lu1c5A3LWCUfF6uv76h/WO+Hm7fb6kXdCxBiO4K3M/JhCtwHHneBEVjte+1/26n25DkrEwuhxw3/FDglK30bWLbIusVrD5FLJgr6Rro8NsD6f5gIJ4hCw+PV6P3wQPR9RcBSiFBOd06u0ASd7NreJgudSk3NQ8dm/mV+sVCv03TPhZHfGYuNYvBpyUXWlyT0oLHH+iDhV6MzhHB0gR8wWnRt5At9pIyBWTpK7NQKrHIX9+cbrBY2e8n9gpWeBuHNqO/wQMZ4Sm14gx0PTPFefHUnnw3M6ce8d9jSZLBSrK8lohQSqWFIJyI8++aW+4Q54cglH1EWoSJPPY+Ao2M1/HmOyKWkQ9jHntq5p8nyLy1KuwJtJoxHHZMfe0DDI39HQzf9oPveY2bx7PliRXf+FAg69jRY6dOJXSLtr6lsQcUDsewDZLSnrt0x1hcTfxLqhCx/nFhor19MvysPApvLIKKbogvdtqlJlo5PhZoHV0E+29+bAon7FVUMtwpVuJlDP9tLbesYrfJvC2Q62rN/JkxQtJVlra0SW9huLM0MtMbs88Wiis+KIb/y8RZJOK1JF8BEe+m87kCBxDGfd38dhMBd4OePrqE0oyQR5VozzpaCHSBDFvxvX4JlyrIAWZZ7xqjk1Ky4am0+zk97+z4jxH2dtFH2IhbZ4u0fjZ+8B4gocpSjDsJQEcrDjdNMGy3vlfRhbSbS5V1beSYG6m4vMInq1LLGbJHZOXwQggHPY9b2dsMsTXr+o4z/tRUnBXwl7t24Ep/3qEsQoe/6YGGqGDGUZp3jEvuQURTfM6mcsFLL4eUGX Q1G+0w46 CY/cKK3LTijrrz7wEYaIH0ceY5DxysU1tqDUv02BLE5fClO5C/I1dep403QgsY1IWM1ENJ9RU/Yvdk/NyFz7DIHo70J54/T7BSz0F4JbGkkElwyAownFC4daeHH2RvGdUU35SQtrr2nHN0Qvae2PNgwjlbt+n3OhUNbbg9l5RgY533adMQIwKcOmWZWAuzByRuyZvV4Tpnmr5D/rrZOuJiqBRktWrU0wWvI0A+DMpHLiSsOCtktfVOokwmi99b7G+ASDiZ/9+MSvLHVqhacj3/Eyd2ZO1KQ7MHdYTQ3ISN2wU5ifevkyJSfjQMBTQmoQRU9iQvjr+fW4jCQJYwK7Nt3IX2fhEd0qhPiI2kjdof9fmb1QPxImbRs+dMXKABhXqnXuyPsuTTanW8QnPV6AKcBgCBjXISbnymArm4cm+psKkh5/XfbLpHXyYZeVWqsoy9NuX 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: Adds kselftest for RISC-V control flow integrity implementation for user mode. There is not a lot going on in kernel for enabling landing pad for user mode. cfi selftest are intended to be compiled with zicfilp and zicfiss enabled compiler. Thus kselftest simply checks if landing pad and shadow stack for the binary and process are enabled or not. selftest then register a signal handler for SIGSEGV. Any control flow violation are reported as SIGSEGV with si_code = SEGV_CPERR. Test will fail on receiving any SEGV_CPERR. Shadow stack part has more changes in kernel and thus there are separate tests for that - Exercise `map_shadow_stack` syscall - `fork` test to make sure COW works for shadow stack pages - gup tests As of today kernel uses FOLL_FORCE when access happens to memory via /proc//mem. Not breaking that for shadow stack - signal test. Make sure signal delivery results in token creation on shadow stack and consumes (and verifies) token on sigreturn - shadow stack protection test. attempts to write using regular store instruction on shadow stack memory must result in access faults Signed-off-by: Deepak Gupta --- tools/testing/selftests/riscv/Makefile | 2 +- tools/testing/selftests/riscv/cfi/.gitignore | 3 + tools/testing/selftests/riscv/cfi/Makefile | 10 + .../testing/selftests/riscv/cfi/cfi_rv_test.h | 83 ++++ .../selftests/riscv/cfi/riscv_cfi_test.c | 82 ++++ .../testing/selftests/riscv/cfi/shadowstack.c | 362 ++++++++++++++++++ .../testing/selftests/riscv/cfi/shadowstack.h | 37 ++ 7 files changed, 578 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/riscv/cfi/.gitignore create mode 100644 tools/testing/selftests/riscv/cfi/Makefile create mode 100644 tools/testing/selftests/riscv/cfi/cfi_rv_test.h create mode 100644 tools/testing/selftests/riscv/cfi/riscv_cfi_test.c create mode 100644 tools/testing/selftests/riscv/cfi/shadowstack.c create mode 100644 tools/testing/selftests/riscv/cfi/shadowstack.h diff --git a/tools/testing/selftests/riscv/Makefile b/tools/testing/selftests/riscv/Makefile index 7ce03d832b64..6e142fe004ab 100644 --- a/tools/testing/selftests/riscv/Makefile +++ b/tools/testing/selftests/riscv/Makefile @@ -5,7 +5,7 @@ ARCH ?= $(shell uname -m 2>/dev/null || echo not) ifneq (,$(filter $(ARCH),riscv)) -RISCV_SUBTARGETS ?= hwprobe vector mm sigreturn +RISCV_SUBTARGETS ?= hwprobe vector mm sigreturn cfi else RISCV_SUBTARGETS := endif diff --git a/tools/testing/selftests/riscv/cfi/.gitignore b/tools/testing/selftests/riscv/cfi/.gitignore new file mode 100644 index 000000000000..ce7623f9da28 --- /dev/null +++ b/tools/testing/selftests/riscv/cfi/.gitignore @@ -0,0 +1,3 @@ +cfitests +riscv_cfi_test +shadowstack \ No newline at end of file diff --git a/tools/testing/selftests/riscv/cfi/Makefile b/tools/testing/selftests/riscv/cfi/Makefile new file mode 100644 index 000000000000..b65f7ff38a32 --- /dev/null +++ b/tools/testing/selftests/riscv/cfi/Makefile @@ -0,0 +1,10 @@ +CFLAGS += -I$(top_srcdir)/tools/include + +CFLAGS += -march=rv64gc_zicfilp_zicfiss + +TEST_GEN_PROGS := cfitests + +include ../../lib.mk + +$(OUTPUT)/cfitests: riscv_cfi_test.c shadowstack.c + $(CC) -o$@ $(CFLAGS) $(LDFLAGS) $^ diff --git a/tools/testing/selftests/riscv/cfi/cfi_rv_test.h b/tools/testing/selftests/riscv/cfi/cfi_rv_test.h new file mode 100644 index 000000000000..fa1cf7183672 --- /dev/null +++ b/tools/testing/selftests/riscv/cfi/cfi_rv_test.h @@ -0,0 +1,83 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ + +#ifndef SELFTEST_RISCV_CFI_H +#define SELFTEST_RISCV_CFI_H +#include +#include +#include "shadowstack.h" + +#define RISCV_CFI_SELFTEST_COUNT RISCV_SHADOW_STACK_TESTS + +#define CHILD_EXIT_CODE_SSWRITE 10 +#define CHILD_EXIT_CODE_SIG_TEST 11 + +#define my_syscall5(num, arg1, arg2, arg3, arg4, arg5) \ +({ \ + register long _num __asm__ ("a7") = (num); \ + register long _arg1 __asm__ ("a0") = (long)(arg1); \ + register long _arg2 __asm__ ("a1") = (long)(arg2); \ + register long _arg3 __asm__ ("a2") = (long)(arg3); \ + register long _arg4 __asm__ ("a3") = (long)(arg4); \ + register long _arg5 __asm__ ("a4") = (long)(arg5); \ + \ + __asm__ volatile ( \ + "ecall\n" \ + : "+r"(_arg1) \ + : "r"(_arg2), "r"(_arg3), "r"(_arg4), "r"(_arg5), \ + "r"(_num) \ + : "memory", "cc" \ + ); \ + _arg1; \ +}) + +#define my_syscall3(num, arg1, arg2, arg3) \ +({ \ + register long _num __asm__ ("a7") = (num); \ + register long _arg1 __asm__ ("a0") = (long)(arg1); \ + register long _arg2 __asm__ ("a1") = (long)(arg2); \ + register long _arg3 __asm__ ("a2") = (long)(arg3); \ + \ + __asm__ volatile ( \ + "ecall\n" \ + : "+r"(_arg1) \ + : "r"(_arg2), "r"(_arg3), \ + "r"(_num) \ + : "memory", "cc" \ + ); \ + _arg1; \ +}) + +#ifndef __NR_prctl +#define __NR_prctl 167 +#endif + +#ifndef __NR_map_shadow_stack +#define __NR_map_shadow_stack 453 +#endif + +#define CSR_SSP 0x011 + +#ifdef __ASSEMBLY__ +#define __ASM_STR(x) x +#else +#define __ASM_STR(x) #x +#endif + +#define csr_read(csr) \ +({ \ + register unsigned long __v; \ + __asm__ __volatile__ ("csrr %0, " __ASM_STR(csr) \ + : "=r" (__v) : \ + : "memory"); \ + __v; \ +}) + +#define csr_write(csr, val) \ +({ \ + unsigned long __v = (unsigned long) (val); \ + __asm__ __volatile__ ("csrw " __ASM_STR(csr) ", %0" \ + : : "rK" (__v) \ + : "memory"); \ +}) + +#endif diff --git a/tools/testing/selftests/riscv/cfi/riscv_cfi_test.c b/tools/testing/selftests/riscv/cfi/riscv_cfi_test.c new file mode 100644 index 000000000000..f22b3f0f24de --- /dev/null +++ b/tools/testing/selftests/riscv/cfi/riscv_cfi_test.c @@ -0,0 +1,82 @@ +// SPDX-License-Identifier: GPL-2.0-only + +#include "../../kselftest.h" +#include +#include +#include +#include "cfi_rv_test.h" + +/* do not optimize cfi related test functions */ +#pragma GCC push_options +#pragma GCC optimize("O0") + +void sigsegv_handler(int signum, siginfo_t *si, void *uc) +{ + struct ucontext *ctx = (struct ucontext *) uc; + + if (si->si_code == SEGV_CPERR) { + printf("Control flow violation happened somewhere\n"); + printf("pc where violation happened %lx\n", ctx->uc_mcontext.gregs[0]); + exit(-1); + } + + printf("In sigsegv handler\n"); + /* all other cases are expected to be of shadow stack write case */ + exit(CHILD_EXIT_CODE_SSWRITE); +} + +bool register_signal_handler(void) +{ + struct sigaction sa = {}; + + sa.sa_sigaction = sigsegv_handler; + sa.sa_flags = SA_SIGINFO; + if (sigaction(SIGSEGV, &sa, NULL)) { + printf("registering signal handler for landing pad violation failed\n"); + return false; + } + + return true; +} + +int main(int argc, char *argv[]) +{ + int ret = 0; + unsigned long lpad_status = 0, ss_status = 0; + + ksft_print_header(); + + ksft_set_plan(RISCV_CFI_SELFTEST_COUNT); + + ksft_print_msg("starting risc-v tests\n"); + + /* + * Landing pad test. Not a lot of kernel changes to support landing + * pad for user mode except lighting up a bit in senvcfg via a prctl + * Enable landing pad through out the execution of test binary + */ + ret = my_syscall5(__NR_prctl, PR_GET_INDIR_BR_LP_STATUS, &lpad_status, 0, 0, 0); + if (ret) + ksft_exit_skip("Get landing pad status failed with %d\n", ret); + + if (!(lpad_status & PR_INDIR_BR_LP_ENABLE)) + ksft_exit_skip("landing pad is not enabled, should be enabled via glibc\n"); + + ret = my_syscall5(__NR_prctl, PR_GET_SHADOW_STACK_STATUS, &ss_status, 0, 0, 0); + if (ret) + ksft_exit_skip("Get shadow stack failed with %d\n", ret); + + if (!(ss_status & PR_SHADOW_STACK_ENABLE)) + ksft_exit_skip("shadow stack is not enabled, should be enabled via glibc\n"); + + if (!register_signal_handler()) + ksft_exit_skip("registering signal handler for SIGSEGV failed\n"); + + ksft_print_msg("landing pad and shadow stack are enabled for binary\n"); + ksft_print_msg("starting risc-v shadow stack tests\n"); + execute_shadow_stack_tests(); + + ksft_finished(); +} + +#pragma GCC pop_options diff --git a/tools/testing/selftests/riscv/cfi/shadowstack.c b/tools/testing/selftests/riscv/cfi/shadowstack.c new file mode 100644 index 000000000000..2f65eb970c44 --- /dev/null +++ b/tools/testing/selftests/riscv/cfi/shadowstack.c @@ -0,0 +1,362 @@ +// SPDX-License-Identifier: GPL-2.0-only + +#include "../../kselftest.h" +#include +#include +#include +#include +#include +#include "shadowstack.h" +#include "cfi_rv_test.h" + +/* do not optimize shadow stack related test functions */ +#pragma GCC push_options +#pragma GCC optimize("O0") + +void zar(void) +{ + unsigned long ssp = 0; + + ssp = csr_read(CSR_SSP); + printf("inside %s and shadow stack ptr is %lx\n", __func__, ssp); +} + +void bar(void) +{ + printf("inside %s\n", __func__); + zar(); +} + +void foo(void) +{ + printf("inside %s\n", __func__); + bar(); +} + +void zar_child(void) +{ + unsigned long ssp = 0; + + ssp = csr_read(CSR_SSP); + printf("inside %s and shadow stack ptr is %lx\n", __func__, ssp); +} + +void bar_child(void) +{ + printf("inside %s\n", __func__); + zar_child(); +} + +void foo_child(void) +{ + printf("inside %s\n", __func__); + bar_child(); +} + +typedef void (call_func_ptr)(void); +/* + * call couple of functions to test push pop. + */ +int shadow_stack_call_tests(call_func_ptr fn_ptr, bool parent) +{ + if (parent) + printf("call test for parent\n"); + else + printf("call test for child\n"); + + (fn_ptr)(); + + return 0; +} + +/* forks a thread, and ensure shadow stacks fork out */ +bool shadow_stack_fork_test(unsigned long test_num, void *ctx) +{ + int pid = 0, child_status = 0, parent_pid = 0, ret = 0; + unsigned long ss_status = 0; + + printf("exercising shadow stack fork test\n"); + + ret = my_syscall5(__NR_prctl, PR_GET_SHADOW_STACK_STATUS, &ss_status, 0, 0, 0); + if (ret) { + printf("shadow stack get status prctl failed with errorcode %d\n", ret); + return false; + } + + if (!(ss_status & PR_SHADOW_STACK_ENABLE)) + ksft_exit_skip("shadow stack is not enabled, should be enabled via glibc\n"); + + parent_pid = getpid(); + pid = fork(); + + if (pid) { + printf("Parent pid %d and child pid %d\n", parent_pid, pid); + shadow_stack_call_tests(&foo, true); + } else + shadow_stack_call_tests(&foo_child, false); + + if (pid) { + printf("waiting on child to finish\n"); + wait(&child_status); + } else { + /* exit child gracefully */ + exit(0); + } + + if (pid && WIFSIGNALED(child_status)) { + printf("child faulted"); + return false; + } + + return true; +} + +/* exercise `map_shadow_stack`, pivot to it and call some functions to ensure it works */ +#define SHADOW_STACK_ALLOC_SIZE 4096 +bool shadow_stack_map_test(unsigned long test_num, void *ctx) +{ + unsigned long shdw_addr; + int ret = 0; + + shdw_addr = my_syscall3(__NR_map_shadow_stack, NULL, SHADOW_STACK_ALLOC_SIZE, 0); + + if (((long) shdw_addr) <= 0) { + printf("map_shadow_stack failed with error code %d\n", (int) shdw_addr); + return false; + } + + ret = munmap((void *) shdw_addr, SHADOW_STACK_ALLOC_SIZE); + + if (ret) { + printf("munmap failed with error code %d\n", ret); + return false; + } + + return true; +} + +/* + * shadow stack protection tests. map a shadow stack and + * validate all memory protections work on it + */ +bool shadow_stack_protection_test(unsigned long test_num, void *ctx) +{ + unsigned long shdw_addr; + unsigned long *write_addr = NULL; + int ret = 0, pid = 0, child_status = 0; + + shdw_addr = my_syscall3(__NR_map_shadow_stack, NULL, SHADOW_STACK_ALLOC_SIZE, 0); + + if (((long) shdw_addr) <= 0) { + printf("map_shadow_stack failed with error code %d\n", (int) shdw_addr); + return false; + } + + write_addr = (unsigned long *) shdw_addr; + pid = fork(); + + /* no child was created, return false */ + if (pid == -1) + return false; + + /* + * try to perform a store from child on shadow stack memory + * it should result in SIGSEGV + */ + if (!pid) { + /* below write must lead to SIGSEGV */ + *write_addr = 0xdeadbeef; + } else { + wait(&child_status); + } + + /* test fail, if 0xdeadbeef present on shadow stack address */ + if (*write_addr == 0xdeadbeef) { + printf("write suceeded\n"); + return false; + } + + /* if child reached here, then fail */ + if (!pid) { + printf("child reached unreachable state\n"); + return false; + } + + /* if child exited via signal handler but not for write on ss */ + if (WIFEXITED(child_status) && + WEXITSTATUS(child_status) != CHILD_EXIT_CODE_SSWRITE) { + printf("child wasn't signaled for write on shadow stack\n"); + return false; + } + + ret = munmap(write_addr, SHADOW_STACK_ALLOC_SIZE); + if (ret) { + printf("munmap failed with error code %d\n", ret); + return false; + } + + return true; +} + +#define SS_MAGIC_WRITE_VAL 0xbeefdead + +int gup_tests(int mem_fd, unsigned long *shdw_addr) +{ + unsigned long val = 0; + + lseek(mem_fd, (unsigned long)shdw_addr, SEEK_SET); + if (read(mem_fd, &val, sizeof(val)) < 0) { + printf("reading shadow stack mem via gup failed\n"); + return 1; + } + + val = SS_MAGIC_WRITE_VAL; + lseek(mem_fd, (unsigned long)shdw_addr, SEEK_SET); + if (write(mem_fd, &val, sizeof(val)) < 0) { + printf("writing shadow stack mem via gup failed\n"); + return 1; + } + + if (*shdw_addr != SS_MAGIC_WRITE_VAL) { + printf("GUP write to shadow stack memory didn't happen\n"); + return 1; + } + + return 0; +} + +bool shadow_stack_gup_tests(unsigned long test_num, void *ctx) +{ + unsigned long shdw_addr = 0; + unsigned long *write_addr = NULL; + int fd = 0; + bool ret = false; + + shdw_addr = my_syscall3(__NR_map_shadow_stack, NULL, SHADOW_STACK_ALLOC_SIZE, 0); + + if (((long) shdw_addr) <= 0) { + printf("map_shadow_stack failed with error code %d\n", (int) shdw_addr); + return false; + } + + write_addr = (unsigned long *) shdw_addr; + + fd = open("/proc/self/mem", O_RDWR); + if (fd == -1) + return false; + + if (gup_tests(fd, write_addr)) { + printf("gup tests failed\n"); + goto out; + } + + ret = true; +out: + if (shdw_addr && munmap(write_addr, SHADOW_STACK_ALLOC_SIZE)) { + printf("munmap failed with error code %d\n", ret); + ret = false; + } + + return ret; +} + +volatile bool break_loop; + +void sigusr1_handler(int signo) +{ + printf("In sigusr1 handler\n"); + break_loop = true; +} + +bool sigusr1_signal_test(void) +{ + struct sigaction sa = {}; + + sa.sa_handler = sigusr1_handler; + sa.sa_flags = 0; + sigemptyset(&sa.sa_mask); + if (sigaction(SIGUSR1, &sa, NULL)) { + printf("registering signal handler for SIGUSR1 failed\n"); + return false; + } + + return true; +} +/* + * shadow stack signal test. shadow stack must be enabled. + * register a signal, fork another thread which is waiting + * on signal. Send a signal from parent to child, verify + * that signal was received by child. If not test fails + */ +bool shadow_stack_signal_test(unsigned long test_num, void *ctx) +{ + int pid = 0, child_status = 0, ret = 0; + unsigned long ss_status = 0; + + ret = my_syscall5(__NR_prctl, PR_GET_SHADOW_STACK_STATUS, &ss_status, 0, 0, 0); + if (ret) { + printf("shadow stack get status prctl failed with errorcode %d\n", ret); + return false; + } + + if (!(ss_status & PR_SHADOW_STACK_ENABLE)) + ksft_exit_skip("shadow stack is not enabled, should be enabled via glibc\n"); + + /* this should be caught by signal handler and do an exit */ + if (!sigusr1_signal_test()) { + printf("registering sigusr1 handler failed\n"); + exit(-1); + } + + pid = fork(); + + if (pid == -1) { + printf("signal test: fork failed\n"); + goto out; + } + + if (pid == 0) { + while (!break_loop) + sleep(1); + + exit(11); + /* child shouldn't go beyond here */ + } + + /* send SIGUSR1 to child */ + kill(pid, SIGUSR1); + wait(&child_status); + +out: + + return (WIFEXITED(child_status) && + WEXITSTATUS(child_status) == 11); +} + +int execute_shadow_stack_tests(void) +{ + int ret = 0; + unsigned long test_count = 0; + unsigned long shstk_status = 0; + + printf("Executing RISC-V shadow stack self tests\n"); + + ret = my_syscall5(__NR_prctl, PR_GET_SHADOW_STACK_STATUS, &shstk_status, 0, 0, 0); + + if (ret != 0) + ksft_exit_skip("Get shadow stack status failed with %d\n", ret); + + /* + * If we are here that means get shadow stack status succeeded and + * thus shadow stack support is baked in the kernel. + */ + while (test_count < ARRAY_SIZE(shstk_tests)) { + ksft_test_result((*shstk_tests[test_count].t_func)(test_count, NULL), + shstk_tests[test_count].name); + test_count++; + } + + return 0; +} + +#pragma GCC pop_options diff --git a/tools/testing/selftests/riscv/cfi/shadowstack.h b/tools/testing/selftests/riscv/cfi/shadowstack.h new file mode 100644 index 000000000000..b43e74136a26 --- /dev/null +++ b/tools/testing/selftests/riscv/cfi/shadowstack.h @@ -0,0 +1,37 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ + +#ifndef SELFTEST_SHADOWSTACK_TEST_H +#define SELFTEST_SHADOWSTACK_TEST_H +#include +#include + +/* + * a cfi test returns true for success or false for fail + * takes a number for test number to index into array and void pointer. + */ +typedef bool (*shstk_test_func)(unsigned long test_num, void *); + +struct shadow_stack_tests { + char *name; + shstk_test_func t_func; +}; + +bool shadow_stack_fork_test(unsigned long test_num, void *ctx); +bool shadow_stack_map_test(unsigned long test_num, void *ctx); +bool shadow_stack_protection_test(unsigned long test_num, void *ctx); +bool shadow_stack_gup_tests(unsigned long test_num, void *ctx); +bool shadow_stack_signal_test(unsigned long test_num, void *ctx); + +static struct shadow_stack_tests shstk_tests[] = { + { "shstk fork test\n", shadow_stack_fork_test }, + { "map shadow stack syscall\n", shadow_stack_map_test }, + { "shadow stack gup tests\n", shadow_stack_gup_tests }, + { "shadow stack signal tests\n", shadow_stack_signal_test}, + { "memory protections of shadow stack memory\n", shadow_stack_protection_test } +}; + +#define RISCV_SHADOW_STACK_TESTS ARRAY_SIZE(shstk_tests) + +int execute_shadow_stack_tests(void); + +#endif