From patchwork Wed Nov 22 09:42:28 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Brown X-Patchwork-Id: 13464412 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 5E774C61D9B for ; Wed, 22 Nov 2023 09:45:17 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id F3D7B6B05AE; Wed, 22 Nov 2023 04:45:16 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id EEE706B05AF; Wed, 22 Nov 2023 04:45:16 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DB5AE6B05B0; Wed, 22 Nov 2023 04:45:16 -0500 (EST) 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 CCCAA6B05AE for ; Wed, 22 Nov 2023 04:45:16 -0500 (EST) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id B3CEC1CAED6 for ; Wed, 22 Nov 2023 09:45:16 +0000 (UTC) X-FDA: 81485107032.15.96DC53D Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by imf25.hostedemail.com (Postfix) with ESMTP id AEA0EA0017 for ; Wed, 22 Nov 2023 09:45:14 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=CCDro7Jo; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf25.hostedemail.com: domain of broonie@kernel.org designates 145.40.68.75 as permitted sender) smtp.mailfrom=broonie@kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1700646314; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=dTfoVphkMmVvpDJF2+yr5qTfG4ThDXv19nEGC0eJiI4=; b=FjP5K5e25fJc2vH2odoGkVwiaRNYaMnCZYs3lV8WNgpU/bvnqgPbeZISkyuXQLDE5nWti+ LQvIKz3jrPWt5mhN3Xllsd0zMMIQDomMNt9KW7QBCLhoAqqAejyaNQrsSniohXlh4vPGfZ lvgXK2jExw4/SLBNtWAejS1ZDxtYCmY= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=CCDro7Jo; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf25.hostedemail.com: domain of broonie@kernel.org designates 145.40.68.75 as permitted sender) smtp.mailfrom=broonie@kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1700646314; a=rsa-sha256; cv=none; b=Zc03uiuhtwVp9iEIzZxqDeOKigEsjHAz3StZ2+bSkH/2bgSrHlFQlui6cbEiBUHMxDXA4P fQsvANS3JQAWXgtvi6gl31dCc0+z7ogPwGbUj2+t4yOPyeieWWvVQIVoR0FjFDfsA8hoR1 VJsOnue8Wy7PZ/sXXuQBaM93XFgBT+I= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by ams.source.kernel.org (Postfix) with ESMTP id E8923B81180; Wed, 22 Nov 2023 09:45:07 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5952CC43395; Wed, 22 Nov 2023 09:45:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1700646307; bh=wVOlZmCKBUn1jhAhbimvszXVMnUi4v3Lr4IoEItDOBs=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=CCDro7JouQ3Wb7ftL82yuN3B5sn74lazOO3cAMtOK6GH2SXVkLKS+Ls4+euuZ/BZ9 d0Itf5FF7PBzbZNWp1TOl5glUhjk7qsABIUeecVHaajClACNOB4FXPUfDBG4QmMxQr t6ITemYbCzbCSH9f+1Gwaz4hI0MGFv6kG4swHP5mlAAw5T4CWQaZJr3TWYl08fVmK4 n7rkJNNHIoDZf6vWLnpGDczTKD09XOfpL3/BAhQcWr1iSGTEId5z52kCD00ZLmbM4O ff2/7uIIOpZ69Ftmi0srAFO8J8ZcjVhhhF2FuvFtXG5uFA/VlpdhES5TGYqk4zULSy J+gI8bXr3IVxQ== From: Mark Brown Date: Wed, 22 Nov 2023 09:42:28 +0000 Subject: [PATCH v7 18/39] arm64/traps: Handle GCS exceptions MIME-Version: 1.0 Message-Id: <20231122-arm64-gcs-v7-18-201c483bd775@kernel.org> References: <20231122-arm64-gcs-v7-0-201c483bd775@kernel.org> In-Reply-To: <20231122-arm64-gcs-v7-0-201c483bd775@kernel.org> To: Catalin Marinas , Will Deacon , Jonathan Corbet , Andrew Morton , Marc Zyngier , Oliver Upton , James Morse , Suzuki K Poulose , Arnd Bergmann , Oleg Nesterov , Eric Biederman , Kees Cook , Shuah Khan , "Rick P. Edgecombe" , Deepak Gupta , Ard Biesheuvel , Szabolcs Nagy Cc: "H.J. Lu" , Paul Walmsley , Palmer Dabbelt , Albert Ou , Florian Weimer , Christian Brauner , Thiago Jung Bauermann , linux-arm-kernel@lists.infradead.org, linux-doc@vger.kernel.org, kvmarm@lists.linux.dev, linux-fsdevel@vger.kernel.org, linux-arch@vger.kernel.org, linux-mm@kvack.org, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, linux-riscv@lists.infradead.org, Mark Brown X-Mailer: b4 0.13-dev-0438c X-Developer-Signature: v=1; a=openpgp-sha256; l=6026; i=broonie@kernel.org; h=from:subject:message-id; bh=wVOlZmCKBUn1jhAhbimvszXVMnUi4v3Lr4IoEItDOBs=; b=owEBbQGS/pANAwAKASTWi3JdVIfQAcsmYgBlXc0E3ICrmSHwaAjqaVf1FJF/zyEXU93LK20k7 YokMTr6WAKJATMEAAEKAB0WIQSt5miqZ1cYtZ/in+ok1otyXVSH0AUCZV3NBAAKCRAk1otyXVSH 0KxzB/4gPmBtGMVYhHYiouneb0CaF9tVHpcoWEOMsDM5SAo4ONxsRNygZjO8ze0HGXstBrRQ1r4 GHNRMG0fOupLravoefTRskl4NTos7JFpQIqzCDXUFL7jhyVUQqvCO0zrXloM3LHzXSYuaAwA9+n r1JDfXrk8MYDd3W1aeizdTj6qmJsOsQWkzx0f3LzaJ55XYKtMUIQ3ud476aDDIKEx+x756F6X52 m0LXzmpBtHt7wzhYOTYRBOFb7gfQfBhCTf5LICgyFMQBOfX8qSe3MLats9o2ORh3F29flpP8xE+ vkuOX1VnQDO6I9IVjpBimFsiRvjMYdX8JDF6N9ru9NR2hV4P X-Developer-Key: i=broonie@kernel.org; a=openpgp; fpr=3F2568AAC26998F9E813A1C5C3F436CA30F5D8EB X-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: AEA0EA0017 X-Stat-Signature: 3mhffcuq1ps1df7f7y4ixcjczctmwfza X-HE-Tag: 1700646314-839516 X-HE-Meta: U2FsdGVkX1+P1EgVXkrswSzZspfu8z1kyQHf6VISz1hN2IscNbVyI5oxZhKRJZ+TLbrk7cHJmSLhS/nqZLLbSmY8bnhv7BTmnCHJQ0VfprXuJkm43Qeu9PlAIz1yOmSACuj7FTI5BFFz3x/jpBiWsAWFYQ7pCr/Mt4qG0GhxJ9rUgyUtzYBQqM/5zSD/oLaAd2SmGPxUEe0H8yuS9M9FiUx017BNaNnu7YnJc/3beXEN2Rekl7dZlSqxmJws2LJNvqv7KBOrVErP9nYG1RKx0KIZvykylYHRQ/lZ73kR4nzeGDS42lGeCcjyRFw5IjyaZ6OSN9zyOz/QlAvnWBiuUJK+kibuqOLDmJHnSHLXh+jracWooBP47xj4NC0vZTYSpODznD/4Hwt94yxX+VDCgyrJUmR5HouFme8wTX1mNqzEwDjg/tDr60Ltea9eHE3JEbuSn6TFXpCdb5je8JKOkkyXKH1dNOxWTnS6uYYnE6egD/kmaUNW+yH3LsH36HgxzF6bNn2nccuBXQsX79/YFMNEj9pX5JnwJqBfX7rDoSw++O97H3So8OSGjCt2Q2G5LFEZLzPFsw/W9AmjC8++YtidQPiqrJvH52QQ+JqlRwIk1QcWoUtthy0N0xir8Td+ZgnuA0G7ZpvhUCnH6Xc0arqLEG+P5f1UoWZCnwAks2TTb4BT1gWFTo+YnmBBOABrH50f+phfpuNpikYRV+T8IuE1iLH9FF+VU/R6szftOka7AjtIFiXU3iDJSvrkpOMWSYobBooaDwm7fdmtW85/4ROENl5ABabV00yW0Eh7XZ+lOSld9l6rBokTQAy4FR/V2Tn+BpBBGMWZlukRx6TByBgsnYRuesOcY2lDJP4VBbeq6ZrctRG4paxMZmcZOxL/LwIDUV9FIqbCHUNetTWRIeoz61sFLEL1f3ko6fh8QmntftK9lYsYVg+tF1l38j3/UuINLwblQw8Y5P088NX nDV5CRj8 JF5SqIHlZFAUeXqpgH2/niokJPu0MTYN97j+7v1eaojtFgxhy9pL4xsQawGfDm6gGv4vcNljFHDboT95/9zAXAJITBVBfuFZS07TE0Zc+sRi1VONl54w3BBG59ICOEgqDh9n1SRSm4v2KIj2jgQflvIivyPlxKIzziSx+akJlO6LPyOja7psgY+DY8eVgbh2s/9wInNako3sf4Yxx3HEfHzMWrd4mrzYRtafUtvJJJ8MPW9ncuj7HzlGJmaqfY5G3jn1UH2Q56GdVX1jArgGjYqAgz9SizzlyfR2u3xrG+XmFV1Fy8bksHZ9gdWDutpQi8++s 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: A new exception code is defined for GCS specific faults other than standard load/store faults, for example GCS token validation failures, add handling for this. These faults are reported to userspace as segfaults with code SEGV_CPERR (protection error), mirroring the reporting for x86 shadow stack errors. GCS faults due to memory load/store operations generate data aborts with a flag set, these will be handled separately as part of the data abort handling. Since we do not currently enable GCS for EL1 we should not get any faults there but while we're at it we wire things up there, treating any GCS fault as fatal. Signed-off-by: Mark Brown --- arch/arm64/include/asm/esr.h | 28 +++++++++++++++++++++++++++- arch/arm64/include/asm/exception.h | 2 ++ arch/arm64/kernel/entry-common.c | 23 +++++++++++++++++++++++ arch/arm64/kernel/traps.c | 11 +++++++++++ 4 files changed, 63 insertions(+), 1 deletion(-) diff --git a/arch/arm64/include/asm/esr.h b/arch/arm64/include/asm/esr.h index ae35939f395b..a87a8305051f 100644 --- a/arch/arm64/include/asm/esr.h +++ b/arch/arm64/include/asm/esr.h @@ -51,7 +51,8 @@ #define ESR_ELx_EC_FP_EXC32 (0x28) /* Unallocated EC: 0x29 - 0x2B */ #define ESR_ELx_EC_FP_EXC64 (0x2C) -/* Unallocated EC: 0x2D - 0x2E */ +#define ESR_ELx_EC_GCS (0x2D) +/* Unallocated EC: 0x2E */ #define ESR_ELx_EC_SERROR (0x2F) #define ESR_ELx_EC_BREAKPT_LOW (0x30) #define ESR_ELx_EC_BREAKPT_CUR (0x31) @@ -382,6 +383,31 @@ #define ESR_ELx_MOPS_ISS_SRCREG(esr) (((esr) & (UL(0x1f) << 5)) >> 5) #define ESR_ELx_MOPS_ISS_SIZEREG(esr) (((esr) & (UL(0x1f) << 0)) >> 0) +/* ISS field definitions for GCS */ +#define ESR_ELx_ExType_SHIFT (20) +#define ESR_ELx_ExType_MASK GENMASK(23, 20) +#define ESR_ELx_Raddr_SHIFT (10) +#define ESR_ELx_Raddr_MASK GENMASK(14, 10) +#define ESR_ELx_Rn_SHIFT (5) +#define ESR_ELx_Rn_MASK GENMASK(9, 5) +#define ESR_ELx_Rvalue_SHIFT 5 +#define ESR_ELx_Rvalue_MASK GENMASK(9, 5) +#define ESR_ELx_IT_SHIFT (0) +#define ESR_ELx_IT_MASK GENMASK(4, 0) + +#define ESR_ELx_ExType_DATA_CHECK 0 +#define ESR_ELx_ExType_EXLOCK 1 +#define ESR_ELx_ExType_STR 2 + +#define ESR_ELx_IT_RET 0 +#define ESR_ELx_IT_GCSPOPM 1 +#define ESR_ELx_IT_RET_KEYA 2 +#define ESR_ELx_IT_RET_KEYB 3 +#define ESR_ELx_IT_GCSSS1 4 +#define ESR_ELx_IT_GCSSS2 5 +#define ESR_ELx_IT_GCSPOPCX 6 +#define ESR_ELx_IT_GCSPOPX 7 + #ifndef __ASSEMBLY__ #include diff --git a/arch/arm64/include/asm/exception.h b/arch/arm64/include/asm/exception.h index ad688e157c9b..99caff458e20 100644 --- a/arch/arm64/include/asm/exception.h +++ b/arch/arm64/include/asm/exception.h @@ -57,6 +57,8 @@ void do_el0_undef(struct pt_regs *regs, unsigned long esr); void do_el1_undef(struct pt_regs *regs, unsigned long esr); void do_el0_bti(struct pt_regs *regs); void do_el1_bti(struct pt_regs *regs, unsigned long esr); +void do_el0_gcs(struct pt_regs *regs, unsigned long esr); +void do_el1_gcs(struct pt_regs *regs, unsigned long esr); void do_debug_exception(unsigned long addr_if_watchpoint, unsigned long esr, struct pt_regs *regs); void do_fpsimd_acc(unsigned long esr, struct pt_regs *regs); diff --git a/arch/arm64/kernel/entry-common.c b/arch/arm64/kernel/entry-common.c index 0fc94207e69a..52d78ce63a4e 100644 --- a/arch/arm64/kernel/entry-common.c +++ b/arch/arm64/kernel/entry-common.c @@ -429,6 +429,15 @@ static void noinstr el1_bti(struct pt_regs *regs, unsigned long esr) exit_to_kernel_mode(regs); } +static void noinstr el1_gcs(struct pt_regs *regs, unsigned long esr) +{ + enter_from_kernel_mode(regs); + local_daif_inherit(regs); + do_el1_gcs(regs, esr); + local_daif_mask(); + exit_to_kernel_mode(regs); +} + static void noinstr el1_dbg(struct pt_regs *regs, unsigned long esr) { unsigned long far = read_sysreg(far_el1); @@ -471,6 +480,9 @@ asmlinkage void noinstr el1h_64_sync_handler(struct pt_regs *regs) case ESR_ELx_EC_BTI: el1_bti(regs, esr); break; + case ESR_ELx_EC_GCS: + el1_gcs(regs, esr); + break; case ESR_ELx_EC_BREAKPT_CUR: case ESR_ELx_EC_SOFTSTP_CUR: case ESR_ELx_EC_WATCHPT_CUR: @@ -650,6 +662,14 @@ static void noinstr el0_mops(struct pt_regs *regs, unsigned long esr) exit_to_user_mode(regs); } +static void noinstr el0_gcs(struct pt_regs *regs, unsigned long esr) +{ + enter_from_user_mode(regs); + local_daif_restore(DAIF_PROCCTX); + do_el0_gcs(regs, esr); + exit_to_user_mode(regs); +} + static void noinstr el0_inv(struct pt_regs *regs, unsigned long esr) { enter_from_user_mode(regs); @@ -732,6 +752,9 @@ asmlinkage void noinstr el0t_64_sync_handler(struct pt_regs *regs) case ESR_ELx_EC_MOPS: el0_mops(regs, esr); break; + case ESR_ELx_EC_GCS: + el0_gcs(regs, esr); + break; case ESR_ELx_EC_BREAKPT_LOW: case ESR_ELx_EC_SOFTSTP_LOW: case ESR_ELx_EC_WATCHPT_LOW: diff --git a/arch/arm64/kernel/traps.c b/arch/arm64/kernel/traps.c index 215e6d7f2df8..fb867c6526a6 100644 --- a/arch/arm64/kernel/traps.c +++ b/arch/arm64/kernel/traps.c @@ -500,6 +500,16 @@ void do_el1_bti(struct pt_regs *regs, unsigned long esr) die("Oops - BTI", regs, esr); } +void do_el0_gcs(struct pt_regs *regs, unsigned long esr) +{ + force_signal_inject(SIGSEGV, SEGV_CPERR, regs->pc, 0); +} + +void do_el1_gcs(struct pt_regs *regs, unsigned long esr) +{ + die("Oops - GCS", regs, esr); +} + void do_el0_fpac(struct pt_regs *regs, unsigned long esr) { force_signal_inject(SIGILL, ILL_ILLOPN, regs->pc, esr); @@ -838,6 +848,7 @@ static const char *esr_class_str[] = { [ESR_ELx_EC_MOPS] = "MOPS", [ESR_ELx_EC_FP_EXC32] = "FP (AArch32)", [ESR_ELx_EC_FP_EXC64] = "FP (AArch64)", + [ESR_ELx_EC_GCS] = "Guarded Control Stack", [ESR_ELx_EC_SERROR] = "SError", [ESR_ELx_EC_BREAKPT_LOW] = "Breakpoint (lower EL)", [ESR_ELx_EC_BREAKPT_CUR] = "Breakpoint (current EL)",