From patchwork Wed Nov 22 09:42:11 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Brown X-Patchwork-Id: 13464395 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 56345C61D9C for ; Wed, 22 Nov 2023 09:43:05 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E78726B058C; Wed, 22 Nov 2023 04:43:04 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id E28616B058D; Wed, 22 Nov 2023 04:43:04 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CF19B6B058E; Wed, 22 Nov 2023 04:43:04 -0500 (EST) 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 C05496B058C for ; Wed, 22 Nov 2023 04:43:04 -0500 (EST) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 9B8F7A0492 for ; Wed, 22 Nov 2023 09:43:04 +0000 (UTC) X-FDA: 81485101488.07.25711E8 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by imf17.hostedemail.com (Postfix) with ESMTP id AC06B4000B for ; Wed, 22 Nov 2023 09:43:02 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=q2GNnt1V; spf=pass (imf17.hostedemail.com: domain of broonie@kernel.org designates 145.40.68.75 as permitted sender) smtp.mailfrom=broonie@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1700646182; 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=0md5lKSrfFF/+d67+dSWGz9Xqa/Mx3egB9cc5ypcmdw=; b=bALhfbDYdUUmBSet15HG4LTn90gRAyEKoflYdyM3sg6DY4qeCsGkOopNH9kROK6anWMEMf E+dfjXRk+RUmLkTOhoy30TLFet4LeGtbwrb+IBuUrAIMW0KLVS05qjnRBwaiEBI3fa7Swq vNTNStueOWjDXAwalhM+T3GAW1IJ1yA= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=q2GNnt1V; spf=pass (imf17.hostedemail.com: domain of broonie@kernel.org designates 145.40.68.75 as permitted sender) smtp.mailfrom=broonie@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1700646182; a=rsa-sha256; cv=none; b=SzoiBnXkqBRudGENsuhZ2KFx1fc/Smvvcel+qfCRMEsZ2OVjZA06RZsuQBOzI/A9mGAGV7 BSDiD7miYQakRMmVG7EMB5iqGMJHrNbRSHT9uRcw6m2FjnOdl6mjn8aOiHmYJk8ZEG4tnG buuXu/S2Agu2+0b6xnzb5hrdUqUu40I= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by ams.source.kernel.org (Postfix) with ESMTP id 8085CB8091E; Wed, 22 Nov 2023 09:43:00 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 141ADC433C7; Wed, 22 Nov 2023 09:42:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1700646179; bh=LBKDiUG7NBwWsYEeStbCS35c5iJdRmlcOY1N9hBogGM=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=q2GNnt1Vg0QJQRjesae4sFYhYpcYrw8xuFx8kU7s+OytKR4wzd1yCHi8+32jxqUC1 AWXRxXmwzXvoYtmicILUtYBc+WxAtCvvhpW99BVlem8s8EkD4oOaigl455Hy4ZYMx3 iZ58lbARbqthj3Fz/zCsx8LDPev0F8etIRFBxbkiJ/7XbkI11XtDhT+FZCdf/Y/fS8 JPsY05zr1HGkfkKAUW1l44mtjfkf1MS4IEFL6TYiUmj3frTdbz5owGw53mqr56wpEY ia86O0YKKUQGxM+0JZoc3SjjbemxCBARirVY0+Wq88KNiqBT29fwGRPlkuuiA9heEW AQSiIbLnkjP/Q== From: Mark Brown Date: Wed, 22 Nov 2023 09:42:11 +0000 Subject: [PATCH v7 01/39] arm64/mm: Restructure arch_validate_flags() for extensibility MIME-Version: 1.0 Message-Id: <20231122-arm64-gcs-v7-1-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=1303; i=broonie@kernel.org; h=from:subject:message-id; bh=LBKDiUG7NBwWsYEeStbCS35c5iJdRmlcOY1N9hBogGM=; b=owEBbQGS/pANAwAKASTWi3JdVIfQAcsmYgBlXcz3ufb4BdT3G27Wye1QtqLYalH9gsoNYB6OX 7EyWk64VmOJATMEAAEKAB0WIQSt5miqZ1cYtZ/in+ok1otyXVSH0AUCZV3M9wAKCRAk1otyXVSH 0HxEB/9DTjmmFbNFvHu6r1ymZm8lbbHpqTlnIGPujl+4mFYJWSjhEaVe181wljq4nhtxPsm0u3a dtx7lvzR0Vo0RuGiNEprMGOGHjwlvyFifKfYoG4Hx4DMRzjmHYt9ADFh8JeBcEB7b6VU+8Q54LP choSLtIxGB9bEyLaaFCHrXHn4bsHJebYUBC4hxEk6shMvhPPnKS5YH4sDQVR8xrEqqwA/0e0Nzc 5tg52h+rCta72nz4wUxtrmsCeWCqAeEWxTWayPeXLN4EyGQuAeIcLmLfXs+iQZGDJHimnFgJB+Q IZrJy/B0h5hSoxkO3lXClEw8RP6mm5v2VaonL/9R49GDeD3t X-Developer-Key: i=broonie@kernel.org; a=openpgp; fpr=3F2568AAC26998F9E813A1C5C3F436CA30F5D8EB X-Rspamd-Queue-Id: AC06B4000B X-Rspam-User: X-Stat-Signature: pxz3pzqg5urrrdkjfmfd3ny8m3tyz6us X-Rspamd-Server: rspam01 X-HE-Tag: 1700646182-988372 X-HE-Meta: U2FsdGVkX1+UWBfhi6/illhGiRaMrWk6uGBnnrWMVw8cV7wcBmptUyweIOFgeeVmgfALmONMSWrL9gV08ayj2D+aUmcyitcRKb0VeEV2riJK7tbewGeocApin/40mfQ066pf8jWskI965fYTp+s0DkeYnWSGfENGl32MsZzP824LLcXyikagV79cwKq+XfCttrFKrLYvJMqBKj/fNeWHPs53Tr2hhOm0f9OCmYr+nIrw1z2e5NazYugW8DOL/p1BOEyPDYoaPcDZcj4aslUF9nocBQTbCMz4LjJH+OmDbqmxPMO6qYLl5RLP/KOZP8krWwExEoGNAkcLEhU7ixqfKEGrdPSRJirB6eBFKyyexcGx4khaNgzocPBd/w4K1ChYJm5VcdUBFbwi8GjdyLjjFwzkINGW6DBRAohpqUViVYddSx1+sVEBImE/qwjdP1SZMyNJB6A5eXg/aT8fxujKkKMSXkZ6VO+4Af389h+Puqyzp79dVyWkIil2MzKe3ihy5R8f3gF1Nx1OK46MH+48GhPKDah47fYWU8VSJz05eDkhW3622PmrCJAB+V/PT5pLxr/vh7O0v8sq7RGEFE8n+l5Lrlb+xEjSIv6xPgTLODAqyoLWjVpsFscn0qUqWmQSqOklzxQ+DY6dOfnlc28/+N/pVLVbY9ZHZ5Nl4wtpDVC3I0rDZ1TJcbdJ7MRo/KSoCt4oZrm5GeojpyVcWbfUz5cw7/E0+nBQI29Wbm9UgizIjOjCQG5q9o1Xeh7KnbaCtvg6xHIkKB2T+qm/PG1PJ1U3vggLZwav6+OVurZS2iU0XUMDhTbqE5AeWRHKF98+xwHpayiXl4dF8WCvi/Qr1gX3Y/eUF5PU/4XhZ2ZJjxavD4aHErI0AIH5Jg9Xm9K/ApkcF6auu0e1qnrmYJYm6cNk17isEfirxegvZJ3+LHAk+8X7WFPlNgouAJ9zCvCtZ3dJPk2vzB1Y/jWenLa M72TAFTP p6gI6xIyupV187A5fWWddaC+PvMblXs/J5nIWJRGBkh1bxrYPCBBX7ODOiuduJebXRb5e5a9ZRvFwzlMRrbWnwDsxwbUT6Itx6hJzOME/3yY3QpmbIh+aPZ9Nl8BCOGyUe/t2o3vj1cD+j/wknhECzsFIUulpt5zWa2Rl5PvOEdqoyanEYQ+4kq4Bteh+CYc06CSyL46W85/NEikFdFUYL/Nd21JH0k7fxlz8IXT9CdGEQgnDs3m65Wjhdw5Kw0Gpn3romZvlZKFXvh1IalkqDaVZpseZ3fHAlwNOxRprqnzvlbKnZJR69GAqZ2wRAG4MwYOrh52o6ivArwkhguyg8LWSjsnh7Mklrzxfek/iSJamNk4= 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: Currently arch_validate_flags() is written in a very non-extensible fashion, returning immediately if MTE is not supported and writing the MTE check as a direct return. Since we will want to add more checks for GCS refactor the existing code to be more extensible, no functional change intended. Signed-off-by: Mark Brown --- arch/arm64/include/asm/mman.h | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/arch/arm64/include/asm/mman.h b/arch/arm64/include/asm/mman.h index 5966ee4a6154..c21849ffdd88 100644 --- a/arch/arm64/include/asm/mman.h +++ b/arch/arm64/include/asm/mman.h @@ -52,11 +52,17 @@ static inline bool arch_validate_prot(unsigned long prot, static inline bool arch_validate_flags(unsigned long vm_flags) { - if (!system_supports_mte()) - return true; + if (system_supports_mte()) { + /* + * only allow VM_MTE if VM_MTE_ALLOWED has been set + * previously + */ + if ((vm_flags & VM_MTE) && !(vm_flags & VM_MTE_ALLOWED)) + return false; + } + + return true; - /* only allow VM_MTE if VM_MTE_ALLOWED has been set previously */ - return !(vm_flags & VM_MTE) || (vm_flags & VM_MTE_ALLOWED); } #define arch_validate_flags(vm_flags) arch_validate_flags(vm_flags) From patchwork Wed Nov 22 09:42:12 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Brown X-Patchwork-Id: 13464396 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 9C4E5C61D9D for ; Wed, 22 Nov 2023 09:43:16 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 355F16B058E; Wed, 22 Nov 2023 04:43:16 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 305216B058F; Wed, 22 Nov 2023 04:43:16 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 180226B0590; Wed, 22 Nov 2023 04:43:16 -0500 (EST) 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 0881F6B058E for ; Wed, 22 Nov 2023 04:43:16 -0500 (EST) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id DB64B1A011E for ; Wed, 22 Nov 2023 09:43:15 +0000 (UTC) X-FDA: 81485101950.21.D21C290 Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by imf13.hostedemail.com (Postfix) with ESMTP id 84B0020020 for ; Wed, 22 Nov 2023 09:43:12 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=FleuH5So; spf=pass (imf13.hostedemail.com: domain of broonie@kernel.org designates 145.40.73.55 as permitted sender) smtp.mailfrom=broonie@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1700646193; 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=x/Cd06F2WL0upBscy50EDRzeuCtnVTQqbaxiU8EnLH8=; b=awLyH74Kzp0NqVuW/h5moZOkiXxUUJ9Kfn9iAGoSwLPwh8veDwxF1vukjsu2/mWRt61nOh jW+Nf2wqgxk470fymNye0VfKBBpiIibZMn3v29RzTlTLcxz5vjYFMqpuwdHUX+6nNvxlHe Ny58Ffgw9cVC7t1f2eF0heEd56sh8Ig= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1700646193; a=rsa-sha256; cv=none; b=6KfJhAzItK376V+FOsTuNmL4rHFjPDGXDMIfcBG3YIGp+Hqtepsa5Ca02yRBDh9oKNhNk0 9acKEX5B2Au6YtpDaU7Y9e2xAWMlJcBy2l0jS60SgXjwPE/9Pl372/iUz+w6hex5PA9fY+ DrBt2Sd6yLkOdaikjq9pz978hvCcpbY= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=FleuH5So; spf=pass (imf13.hostedemail.com: domain of broonie@kernel.org designates 145.40.73.55 as permitted sender) smtp.mailfrom=broonie@kernel.org; dmarc=pass (policy=none) header.from=kernel.org Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sin.source.kernel.org (Postfix) with ESMTP id 06DC9CE1EC2; Wed, 22 Nov 2023 09:43:08 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 57205C4339A; Wed, 22 Nov 2023 09:43:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1700646187; bh=m4jPC43T5RlVTTuFtgUTBLQ8flgL5CIMr0+I83wmp9I=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=FleuH5SorImAIbxUWn9gIWuXUzmHWtYH3b9xowe//GV3/74yObmNXfogVNve7sIMj WMccD4ck5U20K2khawd6OtRpGq5YYPh3DYluwm4oekpP+X5W1afPKhEcncJWsyokZT QuxFrZI6uGQHtD0B0H/NFs2v5M7obOC2y4z5qEvGaQcTlaGHI8gFJy8cINt6HOpNBp K3bLtzIeWHw19VB3+FfJUr8zsIUKPktHiO5WM6O/c+xCF/0Utvl1doa8s1wEEXWxf9 xz2MNNlEFg9Dt/5gSfJ0hfI1ZBoWYrrLiIr0R/772tncZSu8t3aGve5BSKnAjX0/Sx mqfMHDktPFbdw== From: Mark Brown Date: Wed, 22 Nov 2023 09:42:12 +0000 Subject: [PATCH v7 02/39] prctl: arch-agnostic prctl for shadow stack MIME-Version: 1.0 Message-Id: <20231122-arm64-gcs-v7-2-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=4768; i=broonie@kernel.org; h=from:subject:message-id; bh=m4jPC43T5RlVTTuFtgUTBLQ8flgL5CIMr0+I83wmp9I=; b=owEBbQGS/pANAwAKASTWi3JdVIfQAcsmYgBlXcz4jLDgXMldTx3cCVXtLYFoKWIKDnTUBfBl4 r79IyY18TGJATMEAAEKAB0WIQSt5miqZ1cYtZ/in+ok1otyXVSH0AUCZV3M+AAKCRAk1otyXVSH 0GGQB/4zpkMXgM4OIAIz0Opw5FgAenwHSga7889ghXvOsN574GY+2TGEUJbteagx1wJvOFXTp8j PGhBMiC5YswNbSMKRdHSQt7Hqlm0Xas/t3WyU7ijF9MgF1wtwunYkT8KOhMfqGGx1UD3LbbE9Kt SimxpwSP1rEIK5OLL6wyhewP+ds+FXd2X+Nq1lWtHDhm9wlB0EFBP7seUU1/d9QTejFvXQPFORs VKEkOsozSdF0M0xoLnRAm+22dflTFx4JIBq9YUZrUiN3NAS7GfwF1EPVRfADSMvKB2SudfaQxZh 6EegY7jED3NiJ6kFufmWOYnsKiRWrS9MmOlCBYd/02cPAtSs X-Developer-Key: i=broonie@kernel.org; a=openpgp; fpr=3F2568AAC26998F9E813A1C5C3F436CA30F5D8EB X-Stat-Signature: idab6npyiotzwhsipy714b6wf5rcdseu X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 84B0020020 X-Rspam-User: X-HE-Tag: 1700646192-534091 X-HE-Meta: U2FsdGVkX1/g6SoKZ4iHetM3Q1LiNBTE5ChivIY7+9LrASu1063QTdCMZCFWmIo3jL5XZWvwq7q0JrDQYMqMHsomwx19KFxiPEpxG9bQVNBFeQot9QLDfnXbxUK6IGNgQMTD2QbZH4uzo10CjQq64lZIyA7q2unb6OTBjzw3ea8v+GfHtDOQ81j9XB+C6tGNf1PZ9iDeP47ZMHVsQoJEIv9cEOaPVx/sEcHSMHqyzn6cDipWNQtP93yNU8QQIDid3bbxhClHIqBTFp90x6dMzJ7S2j26ZiUtWmrdbw7owsipERU2Ks25M4d8q49W5FHMH+wTVLDTUwElLCwR2AHHjStqhDVzAnw5McGMXXyVld8EkoAotgA6BRBCaOQGnzLgJuyEajqYXOBDOWh0JP7cf/2uh2t2tYMY3sC+FFyKmIC0FXLmvLEloLNYR8fPs5FOVT/yXJFzh/Sndf9Wm7ddfPwGLmhMy9pcbv+Iymyp+4PXRvt++hkLvbykfDIOpniCq1wT0Er4gDvKNpBgw3vLDjWzO1swyL1EGiHBycQt2uGNZzb61N7LpIkjx1qt+UAjOwJEWTGiZ9L7Eeg7iM9LGpIhxB/m7C7VNk7XK8SaAHAtK1Rjp20YcXKcY4ZDQceLoaL9X//TZXtYITadi0e0iGf+NoSY2URCmMr1BUZemtHuAIFkQ7WNNFqjMdLHdR9YSwzhySpXYNfqYnoP/5lTzasdiAhKxt6ppYTkZlWPOCWP4YkAP+nMm5i9i7EbPZmoZtW/j1VZoLu7oGzXpEWcA7fyfAaie3v4elXjITmyRSRbpGnic3cdPg3dMATZWKuqOG3EwDhTZ3+HylqtZx0raeiV/wwuCOksAQvJsZq3zk+CKY6UZf2PBsP2HwiqrwyDsX2lwpuwV9BWttWFZFXxJWGczq/BSQB264SQACUC6Y88Ib/JNXWXZW338Sk/uwf64RWFny/y+/hm757/BIi U+3UWQWl BXdnOgH4U/W7OjFbONnYRTM/hvFJRxFFF3sRqDRP0CUEgmS5FJiG7dfIR3C/3Z/KVkYG/Q4psMMBg/rtnJvLb6aLvYxAsn8w3yivzaBuOPrwa1a6e4tL4+cH5HjCPLiU35uFz6jV2EiacfCT0xI/VIXcd2G6kK4xFxkWRZdmzfdBZyavBMtSZIb+s3srjtNCCn0shOoE/siZdXQk7mfaeeHjb/WG44maPctsxzErKVIBlTgKNETu8blL/QaRRbONeB6Qe+GQbDnaJ8eqX4PAvw1R8GMyWKDaZxyDf3SmZS6CGmblpYxRP/b4i1p9dhNn2ejMA 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 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 configuation 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. 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 modified fairly heavily, support for indirect landing pads is removed, additional modes added and the locking interface reworked. The set status prctl() is also reworked to just set flags, if setting/reading the shadow stack pointer is required this could be a separate prctl. Signed-off-by: Mark Brown --- include/linux/mm.h | 4 ++++ include/uapi/linux/prctl.h | 22 ++++++++++++++++++++++ kernel/sys.c | 30 ++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+) diff --git a/include/linux/mm.h b/include/linux/mm.h index 10462f354614..8b28483b4afa 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -4143,4 +4143,8 @@ 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); + #endif /* _LINUX_MM_H */ diff --git a/include/uapi/linux/prctl.h b/include/uapi/linux/prctl.h index 370ed14b1ae0..3c66ed8f46d8 100644 --- a/include/uapi/linux/prctl.h +++ b/include/uapi/linux/prctl.h @@ -306,4 +306,26 @@ struct prctl_mm_map { # define PR_RISCV_V_VSTATE_CTRL_NEXT_MASK 0xc # define PR_RISCV_V_VSTATE_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 71 + +/* + * 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 72 +# 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 73 + #endif /* _LINUX_PRCTL_H */ diff --git a/kernel/sys.c b/kernel/sys.c index e219fcfa112d..96e8a6b5993a 100644 --- a/kernel/sys.c +++ b/kernel/sys.c @@ -2301,6 +2301,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 @@ -2743,6 +2758,21 @@ SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3, case PR_RISCV_V_GET_CONTROL: error = RISCV_V_GET_CONTROL(); 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 Wed Nov 22 09:42:13 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Brown X-Patchwork-Id: 13464397 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 292BBC61D9D for ; Wed, 22 Nov 2023 09:43:21 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id BBAC46B0590; Wed, 22 Nov 2023 04:43:20 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id B6BAF6B0591; Wed, 22 Nov 2023 04:43:20 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A59086B0592; Wed, 22 Nov 2023 04:43:20 -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 9554E6B0590 for ; Wed, 22 Nov 2023 04:43:20 -0500 (EST) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 6BA828052F for ; Wed, 22 Nov 2023 09:43:20 +0000 (UTC) X-FDA: 81485102160.23.EE82A66 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by imf11.hostedemail.com (Postfix) with ESMTP id DCC9C40016 for ; Wed, 22 Nov 2023 09:43:17 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=VS4V1mmE; spf=pass (imf11.hostedemail.com: domain of broonie@kernel.org designates 145.40.68.75 as permitted sender) smtp.mailfrom=broonie@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1700646198; 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=pNe3T1oxurIRBMv7xR3XahEt98MWmkplNge008iSYbQ=; b=mkZiVvjYUGrkYDcJ8Z1I5gvhSnaYP8bKR1TOfkXguGep84L86z0qzoFMtX/u+MRAXb9Bgv 40ohYSdR3FSKrU8h0WmGd1F+PA6vjqkxZg/Zefg/+F7VH0+jMvk6Z5fcYosudpqqDQO0wg Qjz9UISNDm4oPhCEFDbThacamgZtrGo= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1700646198; a=rsa-sha256; cv=none; b=GEVjK5ZAcKoqX792fx4YWIMG88fSaaGxDUXvs4EI3QV8dmwi5/Q1SezFQaPeeXm1UOWajQ UdMGj8gG5R54jOlVKNO4howlzyxIKCc5Apq/dLmJaC3Orw64d/4Me4RFpphGfbbzSHXmK6 jaMV7LL8YJt3Yighz1e++P7vtlB7mBc= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=VS4V1mmE; spf=pass (imf11.hostedemail.com: domain of broonie@kernel.org designates 145.40.68.75 as permitted sender) smtp.mailfrom=broonie@kernel.org; dmarc=pass (policy=none) header.from=kernel.org Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by ams.source.kernel.org (Postfix) with ESMTP id 999C5B8117B; Wed, 22 Nov 2023 09:43:15 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id BB115C433C7; Wed, 22 Nov 2023 09:43:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1700646195; bh=EkyJ2sjNqguwXqxmxIIfJqgpolmeU80tDQ1mr2D5TfQ=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=VS4V1mmE0ZeLoutxYILjpbDNn60e5Tw0OcoSbTasgmO/VejZ//vSk5A4xO+WKFwqr Vt+kO1vC6TXvQBTVbFHHuAp1E9yemr98c7HwHxCb8pBTuq2ba5qEzAzV28dH5jUvlU zMV7HFvQUN1V1bnbNkCq15iyICNQRqCvaWVMvZx4cPo7LnC/W6QUANmM/9t8FXhwrZ eQ+p2PRpxpv2DgKFpKGxSozrGbwpYTwkR7GsL9szoTE6zP6Poc7ZZozJj+u3Frk+Ot uAG2FJIIdF6HfISncjYll7FQ2wapnt8Bc+ndxxZBDEI5t7GlqrUHlXf3Jwmo1mtA3k w60pzdRod63Lw== From: Mark Brown Date: Wed, 22 Nov 2023 09:42:13 +0000 Subject: [PATCH v7 03/39] mman: Add map_shadow_stack() flags MIME-Version: 1.0 Message-Id: <20231122-arm64-gcs-v7-3-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=1759; i=broonie@kernel.org; h=from:subject:message-id; bh=EkyJ2sjNqguwXqxmxIIfJqgpolmeU80tDQ1mr2D5TfQ=; b=owEBbQGS/pANAwAKASTWi3JdVIfQAcsmYgBlXcz5ctjongA13l+3s8rDr6lCcYzCiV/EZ+w01 Sjq0p1+agmJATMEAAEKAB0WIQSt5miqZ1cYtZ/in+ok1otyXVSH0AUCZV3M+QAKCRAk1otyXVSH 0BmeB/0fdILWv3xnmoh3iIUnb+EGSP88bn3NvTdnqSR6EOCHL4XrJPUgwQteJL4EkrLBwM7IXDO phEWSI0/nmWusNr3v9UW6kZ1NmYF+253MWc4v54TjgzG9k4mafDzYYfcZzuGeMZK6JCCAWkGLr0 wv9DrCEa/2jyyNb5n2y+K8nYdNHuJuRrS+1Upl0ZSZ1w9M1JxGUsJgLvnPmoIDgcox7Wg6uxeHf BzbRTK0693utYKeBvr3gTPU/c80PO0X47jTSfpB+WRA2FLQW9FCrDBwc3Z6WFvN2+z7guWL/86q nFREV+64zb5OuzopYwcn7fjb3cADnfSnvSBqlAmHmHfk5T6+ X-Developer-Key: i=broonie@kernel.org; a=openpgp; fpr=3F2568AAC26998F9E813A1C5C3F436CA30F5D8EB X-Rspamd-Queue-Id: DCC9C40016 X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: kx4hrps958uikty3odyuiakjhbhh5ejw X-HE-Tag: 1700646197-729808 X-HE-Meta: U2FsdGVkX1+VuFer3jaYHBqepNuFhoTa1fQhed6WkxCA6qZ4uCZg3BMN/z2yPdr5PIETI/drFLhDwRm+rUHXdPd2budnRaSmsMZL68KeaWH3aIYP3GH72XS0qfwAL3epmumSj4vpLo6lhuBsXTxshm9fvy+AhO2wAd60ii1b4G79ZwltPeeQTnwrkwA/Uz5WoD+zJaMrUCs5hcADQk07ZhEoQnu1H3wuRMainkJIAZI/A3XRVMy+SBgiML7dbt4/p5eO+qF6ukQt/00dINJMf80xv/SuioNL1d7ttWhXIIBdf9LtlMhR7kexqKGZilt3WODGuSgX+J5V8nlo7u5kKXS7wT/+siaqHnzalLr4SLIRHBOsi2QB97PsSTqOX1KY5lPq8DesCstU0l692r6AEqQMFmJ9NHo6lK83Uw5sEyM3ITBH0iSZunyTSb6Lm7+4fRaaBVU7YDRBKkwJgpj/NTIfMhZe+vxAqqxXQ8Lj4P0vFQhIYNVZj5vtFMda1B5yZVUq8DIqBKFOninwKTdyGHur7ah8vSjzWgT1rP0tHoGHTMtn263e6D4YkU876mkOOtDpJTE54JSoqXmkvogx7nY1TDQrNDkJMKAzPOhJ54XTroZx0WFiSQ42Hd06y1diRyXWnHrIPweZDBI+6VgZEuZKwaQSNReVgQv/NaETB0+MtrpQ+qI9OfAM7OikuTFXBuUkkdXMNAPFJW/jtUkMSc2RkIeKif//oou/4KWSeyhj+i9Bqc5O3lLIurGhvEiP9V7JrK6WNaELxqu1Zvw8mQnyMr9noZNbDYy6BUR/Elyp+Npup23eMPMqalwotXiTOmLNFnTrL4cH9m6cIqxTVv7tL7wM0I+OPGp1+aBCFWn9Byw2Zqy+wQSTSdbHGp2O7WT6XEYx+6Zh68TyqglMG6LMY0/QMeDajam/AqYfV9nR5rDwcbQeoy/cH61HQm3NLnkRN3oDEJkHD3XHNrB TiGk9AqF z8hyJNhcZn+FJ9ZH88IxcA5gcwf76TTOPgMMsiwB+z99+mRNa5LVUVGLtTjNlSSH/OFvCfrtl8RbakSrlyTm4Y6lYbw0ToZA2e0QrjpOmmC+wfI1Cx23k7gf9H4Z02ilRNwMrenWxm/nIHaA4uF65AdTGZK+0Vmv2RcPNtoebOefuW8yJY+LTw7CQewodx4L2nQoiZVVFHD4mPJHKlBKt8HFl+BGr1jLsTXXwBxu25/hu7mpJXm5rdWTNI3xIfKr0/FaEbWlo2et+gX1UxZFuLrxgIxPlGc7IGBrATEEITFF6wNyWeeYSYQL/4Byx6UM+BDzZc8loLioGl3WnEiKPCmp6ZYIs8fZ39RynnT1gF9fOOuo= 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: In preparation for adding arm64 GCS support make the map_shadow_stack() SHADOW_STACK_SET_TOKEN flag generic and add _SET_MARKER. The existing flag indicats that a token usable for stack switch should be added to the top of the newly mapped GCS region while the new flag indicates that a top of stack marker suitable for use by unwinders should be added above that. For arm64 the top of stack marker is all bits 0. Signed-off-by: Mark Brown --- arch/x86/include/uapi/asm/mman.h | 3 --- include/uapi/asm-generic/mman.h | 4 ++++ 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/arch/x86/include/uapi/asm/mman.h b/arch/x86/include/uapi/asm/mman.h index 46cdc941f958..ac1e6277212b 100644 --- a/arch/x86/include/uapi/asm/mman.h +++ b/arch/x86/include/uapi/asm/mman.h @@ -5,9 +5,6 @@ #define MAP_32BIT 0x40 /* only give out 32bit addresses */ #define MAP_ABOVE4G 0x80 /* only map above 4GB */ -/* Flags for map_shadow_stack(2) */ -#define SHADOW_STACK_SET_TOKEN (1ULL << 0) /* Set up a restore token in the shadow stack */ - #include #endif /* _ASM_X86_MMAN_H */ diff --git a/include/uapi/asm-generic/mman.h b/include/uapi/asm-generic/mman.h index 57e8195d0b53..d6a282687af5 100644 --- a/include/uapi/asm-generic/mman.h +++ b/include/uapi/asm-generic/mman.h @@ -19,4 +19,8 @@ #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 */ +#define SHADOW_STACK_SET_MARKER (1ULL << 1) /* Set up a top of stack merker in the shadow stack */ + + #endif /* __ASM_GENERIC_MMAN_H */ From patchwork Wed Nov 22 09:42:14 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Brown X-Patchwork-Id: 13464398 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 86FC0C61D9C for ; Wed, 22 Nov 2023 09:43:29 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 21EA46B0592; Wed, 22 Nov 2023 04:43:29 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 1CD996B0593; Wed, 22 Nov 2023 04:43:29 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 06E406B0594; Wed, 22 Nov 2023 04:43:28 -0500 (EST) 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 E70646B0592 for ; Wed, 22 Nov 2023 04:43:28 -0500 (EST) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id BAC6116019A for ; Wed, 22 Nov 2023 09:43:28 +0000 (UTC) X-FDA: 81485102496.19.0E69D77 Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by imf07.hostedemail.com (Postfix) with ESMTP id 63BE940004 for ; Wed, 22 Nov 2023 09:43:25 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=iIJ0MfSf; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf07.hostedemail.com: domain of broonie@kernel.org designates 145.40.73.55 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=1700646207; 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=7UoFm5SVDocDJ2RGIB9LLgBQwfP1e0bUEXl3ZRm4uhs=; b=LZUTqDJLHfVfTcI7d4zv16na6bNiVPE+M5NXXOcZD760oEOor5awWGmf2cGIQrYykS0AmQ p+vU2c83NQSXR9qcDChlX5fZnYsTCuIHsRnwSndQhtO1fsNjxcQrRBxWV/HG/W9jWiloSQ D4A9wfM0/Ov/o8r/Y1LpA9vFmeS2QTo= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=iIJ0MfSf; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf07.hostedemail.com: domain of broonie@kernel.org designates 145.40.73.55 as permitted sender) smtp.mailfrom=broonie@kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1700646207; a=rsa-sha256; cv=none; b=Pm7oo+XMWLL5vJUoW9G8nYpFa5TrLxR6xKjxrtmEX/W+ylAB9UQyN1lQem50/2bu8y65v+ VUMw2Omwg3dN5yY5oFRkNsjNdJQ1x7cug/LOI4K8KoCTKDk7q+N+/PQbGWRBdsB/PlYD30 DVIJW+iXec2QPCjGpmoQotlNSX6KA90= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sin.source.kernel.org (Postfix) with ESMTP id 80875CE1ECD; Wed, 22 Nov 2023 09:43:23 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8DEF8C433AD; Wed, 22 Nov 2023 09:43:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1700646202; bh=v46aUsbCUkfVZOkEZ7v0IxSRHU/syZrce+2sfpMy+L8=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=iIJ0MfSfGFe4BnLUwdW5xpDU2/xqYSBGq5Cj51zuM4YqUFdMnvqeDyfxsUIisnU6b wUw0U8Wv0OtRwL9eJwl7JUSwkLZOKIYuD0AugCVQlgzdKZYYavos+tMW7NTLsmFKcC fUTaeAlnathNNURAxXfaZCeWFV55cs7UylMWhaFfEfZ8KPnWdlZD6rgrcgdbSK7hJg WbfctrcjTBpV2PpOorXe++LBEsLPqSRTj7lS0IvdWzi2ge+pX7iP5nGGrqwFD3lC2V BENoY33CIdapQ9DEX5gbQnb9HvHmnMnFes7cCOy49y2tQGmmB3gDv/79PJIL2Tb+ak 6A2RQiwxSADUA== From: Mark Brown Date: Wed, 22 Nov 2023 09:42:14 +0000 Subject: [PATCH v7 04/39] arm64: Document boot requirements for Guarded Control Stacks MIME-Version: 1.0 Message-Id: <20231122-arm64-gcs-v7-4-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=1640; i=broonie@kernel.org; h=from:subject:message-id; bh=v46aUsbCUkfVZOkEZ7v0IxSRHU/syZrce+2sfpMy+L8=; b=owEBbQGS/pANAwAKASTWi3JdVIfQAcsmYgBlXcz58KUrsysjlaWSzO1jYK55QY/WSRM85IGvg PxdQ5ZK0xyJATMEAAEKAB0WIQSt5miqZ1cYtZ/in+ok1otyXVSH0AUCZV3M+QAKCRAk1otyXVSH 0B0qCACCvoOJm105lKv2ZPLjhkUmi7fDdR0EajV9UTvtTmDuiS9RoPv6qew3skKRJ2x99xSPM6Z MtWo+pWCObUZ/Em3l5ZBxXdf54PqlfguR3WeFcjJxgyWZFoYUbPGvfavfrfotOZEl8YyAR60lkT CE8NmFmf/pIXk4tmlokj6+AB4XXUAcDtuWo7w3VyW/4oC8t5jYKXNeusb7+/D03X7WNhzaqtsD8 E7hAKjI9aCqrTPVhf4fOkVUbCxisy/HohyaibsbUO56DDwyDFqhYNMmUsjPSZSKQXDMaY7nzsiX E2K+UxJ8O8oozTh0n/yiPavYQ+94mTSwGH/LNau3iMTKY0f+ X-Developer-Key: i=broonie@kernel.org; a=openpgp; fpr=3F2568AAC26998F9E813A1C5C3F436CA30F5D8EB X-Rspamd-Queue-Id: 63BE940004 X-Rspam-User: X-Rspamd-Server: rspam02 X-Stat-Signature: g4yy6t1gec14xkz8gwat8naj479cg9yf X-HE-Tag: 1700646205-539107 X-HE-Meta: U2FsdGVkX1+K1qXypGGd29+YwQ4xNKjLcuS2UhklzHgfWPGvQxjrpL5NoI09oWA24CvRCQFY+bjPPoAN2QXtY/5hIG4DvAJTD+C2fnYPNNOzdqwJc1ezej+vJ98cCercqTURnRYGpEcHHPqNyLn38vlrV70midllU/darOaIz4TFaNNKQ0tQMY8FGqxm9UDhw69SGOytf8TtOT1xtyLKooaA5xVOT+4JCRff1HCrWlf8ftqUCsomhSuviKkfYlaclzFDPeu0T2rrZmv1z4qmy76aBiItbc9uy+uKVITgla4EatjiUIfDM5Em9Rr4YZWZuKHVJ3zRrpBqnDDg+s1uMCa8MHjkRkwFKy/VKwGoBa6gm9oZRLsSf88cMhLlw4HJ2QqgM294Ww92C50fj1zjs4DfdAcHXPlrTq5B9RvTpfzo2GyoYdnPkN5i4/bodhfHLEiAszyxU2otfMbay+vZh8vzbVsE1tm8tZE8F4RxFGtnelrH4Vr9KVfuBT2y0tRqlfElPfZsJjMmsZzL7z4MZ5GtihUUH6rsu7ipgiu1W6UguLp9W29/hlnNHBgugJWBCa9dwAqIbWQFg6P7m5X4LfEYF+wNAkQjX5gEB1xe4KMbvPaDrpZpt+oQqsCn2/Ke0CLesNZWbeYyNqSEe60W1H+EVFEFr5XIrBK2SAh+8LK/bpAd5ftPDObBBJ2CKFTNBOUebbihfq6+PArIptSn2mEVYKLyTI/oAWLT+PZs3Ql91+9nzJxqiFr82gyUUTfADHJ4XbmOdRTXLA1Ci7gE+RNN3rqB3kXiyw/89Q014nJIUkHVZeHTJ55it64hhG1YBEv7QULiDE32d400WEHdwJXgsxEL+O0Em1MYYQuXQsAoiJbH1MKkC//fleENEYgyfKBctRJKFVO3tzhuZQAIzuk3D/PhpfqZFOey4P0nTHMeQBBiLmuPmzCyvJ18QSsPOKchwkYufKDtiSBH0bZ LpCzTqzl QAjyOlenhayZM41bEXiRqxj7WThsmG1X7e607azO8FygOTKgbsvUUSXCedhvF03+/Ora3yBGzZU5svs50wMYITdXOXUmEADkUfDp2YnxIx5eqc84HhHCRU5ZW5BdGNzVxBgiaA+kK5jcqlfqJm+SmyH6T9Ye9cIENJZCpOP765V2lKATJyiNIieDx7F4b+Nmtb2jxe+r7Nlq2ni2MsYhWdoUfmsN3AqCR7CfQze4JOxeksnJ38wjg5e96czWSGSz1+Ej+VSmkjT4Q5Pgoi4gNj+r7JFgsw6DF6ZfyM/VESkTI/VzLZtQrzpqm3sjgbk9mHLW6 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: FEAT_GCS introduces a number of new system registers, we require that access to these registers is not trapped when we identify that the feature is detected. Signed-off-by: Mark Brown --- Documentation/arch/arm64/booting.rst | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/Documentation/arch/arm64/booting.rst b/Documentation/arch/arm64/booting.rst index b57776a68f15..de3679770c64 100644 --- a/Documentation/arch/arm64/booting.rst +++ b/Documentation/arch/arm64/booting.rst @@ -411,6 +411,28 @@ Before jumping into the kernel, the following conditions must be met: - HFGRWR_EL2.nPIRE0_EL1 (bit 57) must be initialised to 0b1. + - For features with Guarded Control Stacks (FEAT_GCS): + + - If EL3 is present: + + - SCR_EL3.GCSEn (bit 39) must be initialised to 0b1. + + - If the kernel is entered at EL1 and EL2 is present: + + - HFGITR_EL2.nGCSEPP (bit 59) must be initialised to 0b1. + + - HFGITR_EL2.nGCSSTR_EL1 (bit 58) must be initialised to 0b1. + + - HFGITR_EL2.nGCSPUSHM_EL1 (bit 57) must be initialised to 0b1. + + - HFGRTR_EL2.nGCS_EL1 (bit 53) must be initialised to 0b1. + + - HFGRTR_EL2.nGCS_EL0 (bit 52) must be initialised to 0b1. + + - HFGWTR_EL2.nGCS_EL1 (bit 53) must be initialised to 0b1. + + - HFGWTR_EL2.nGCS_EL0 (bit 52) must be initialised to 0b1. + The requirements described above for CPU mode, caches, MMUs, architected timers, coherency and system registers apply to all CPUs. All CPUs must enter the kernel in the same exception level. Where the values documented From patchwork Wed Nov 22 09:42:15 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Brown X-Patchwork-Id: 13464399 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 B65A9C61D9C for ; Wed, 22 Nov 2023 09:43:35 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 579536B0594; Wed, 22 Nov 2023 04:43:35 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 529156B0595; Wed, 22 Nov 2023 04:43:35 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3F0EF6B0596; Wed, 22 Nov 2023 04:43:35 -0500 (EST) 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 2F1546B0594 for ; Wed, 22 Nov 2023 04:43:35 -0500 (EST) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id F27141204B5 for ; Wed, 22 Nov 2023 09:43:34 +0000 (UTC) X-FDA: 81485102748.11.7E2531E Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by imf05.hostedemail.com (Postfix) with ESMTP id F19E710000C for ; Wed, 22 Nov 2023 09:43:32 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=Qv4wreSI; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf05.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=1700646213; a=rsa-sha256; cv=none; b=M/MLTzuyq0vl0NyyzDCV/uc1N+tGxK5+NH109gX76BtVpTRODAIbkaXgntZPE7H/v63ExM u4xgrwO791hF8sXvOINX1jf6DImahsTFx7U+COEs1akJWeNs+pcQ6djZVOk5mv36fPCyWL Oh3ijIQ+MkOfOznKPQs40WJRDqldZk4= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=Qv4wreSI; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf05.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=1700646213; 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=KbiCLrmvf7bjj9gb8AgsNSQUXCXzZRVASFj0Bv2Jn2A=; b=YUvhZlCeRHgljJOnLvPVddw+0NZswYSHU+P0TYPBosd0Wi32egM6oPHGfEyUl5N++GlesM AlonV7Cn+VshwSW9e3NFSZduUcl8t/ri7v0mlXF/FfioKr7BytNsWnx5PjLsakTSv3scJM lk5pSYViCrFGoGXR0Il76FPqTnABq64= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by ams.source.kernel.org (Postfix) with ESMTP id 3DB33B8118C; Wed, 22 Nov 2023 09:43:31 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 37A19C433C9; Wed, 22 Nov 2023 09:43:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1700646210; bh=L9ZRGf4mNC+FkwLFIfy9nFVoaeaPEXiAYS+7gHUi1aM=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=Qv4wreSIHv5Ype3rrIep2FDZ7oa0JFgTAurBBMARGex0o4XvAcaDvVDagnPxYpBs8 S0dZwlyIztWNnmtN29WH5IFf3Z4N5THAbBxDrk3ea8UARZshQREWPxgVR7Wv+igHTp M4Dd41SuepuOCJKUMQZRg3SP3Dlid7y4kz5nzFsXWlzTe5qHw1NvB7Cq67vDWXIc/1 ziE5HMgNniJ1C0aps5k2QVT1nyg5FKCSD9+DzweQF5qsd9Cm8/uxx+vVc85FFY1mC8 Dpg8R0WYdDuXT31mv3nxtJ11Fmfx3OguZUhTcNmu3wv5jvR0QBPbwLkT8ZPKRp1EKV 8SiewU3eO5sPw== From: Mark Brown Date: Wed, 22 Nov 2023 09:42:15 +0000 Subject: [PATCH v7 05/39] arm64/gcs: Document the ABI for Guarded Control Stacks MIME-Version: 1.0 Message-Id: <20231122-arm64-gcs-v7-5-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=10660; i=broonie@kernel.org; h=from:subject:message-id; bh=L9ZRGf4mNC+FkwLFIfy9nFVoaeaPEXiAYS+7gHUi1aM=; b=owEBbQGS/pANAwAKASTWi3JdVIfQAcsmYgBlXcz6rEW3Yum0dqw0RdOxkewLl5v2bk8+GVDh6 TDhRy8sFkeJATMEAAEKAB0WIQSt5miqZ1cYtZ/in+ok1otyXVSH0AUCZV3M+gAKCRAk1otyXVSH 0A3WB/9GAbpWImDGZqMaksFSfDV0vvm48cRi9UQP9h3li2xxngiotbFbNoY/F2rH7KBYTWmqEn/ hVUSdxoYyrkr2+FOtj2FGeqZ6i1NzhUhfn2FB8aQRHueO6Hmres3zdyKTON/0ZBnlxGFdX5QMMH S0b7vDZL1VNwR+2bfJJGv1PBvTDiGih/85wN9kG5J3YtMzVxlDlzdzsFNFwtgPm3CHIqo5Gkqm1 zHdfoXStP+WXM2PsCNcej+/PeTsPUigGoMSuG40U+vtMCtd75DrLgwrL5Kaz6fjvtaOJkvM7aar x6OErRhAEPoY63b68qVZC7SoZUTHnzKJSsj4+3yTty/wK7dv X-Developer-Key: i=broonie@kernel.org; a=openpgp; fpr=3F2568AAC26998F9E813A1C5C3F436CA30F5D8EB X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: F19E710000C X-Stat-Signature: hn1mggn8woqitg14wid15znbef3xzc8m X-HE-Tag: 1700646212-729111 X-HE-Meta: U2FsdGVkX1/sn4kjIsyt6rQkCDet78lbM9fcYCMR+NjZa/iGeXIgto6y6c5/2kI6smEncTT0pEDLd9Ndqat2OkRaQw9cBNY+X0E/SgCEW8HTiv0ZlFdBPef04mpgj72GWqyPSfJaKe4xI0IeHENxSDSBfF57KNsxywSQyTfWaBSz/jAuHdYP2KM6RS0P/H2h22Q+Q82OilQ7Yro0K8xuN0LtFEbxco7811WrBfY/qLVQSEQ4azNcvosKEwgE7sIyWKTW77vDqlNLBXfzzO/7vpOMNUs+kMJQI8hOoz+XhFRbYBW8YWRWmo+VCdaKH+HVmkSkxJ0gDy4VyD6MUnwAYAjEqz9PvxGE5n6B+7Dl5F/nORn4g+YSj2WmwQAdiSnPMHiRBiibrXyssq9enahw5U7WZKzHqsBSzCBenyxewldFpFaqg+YAD5TQfxEUZnbSZn3g6+/aiWM1UKGLsfFTsy9Ms06NpoiSx73vlpG4RRrTJwOYOTl0TjWWckqfVfzQCq8e3OxtGeIpVSy9MzEGvVwt5nViJORDEjB8cBTG7bItJdDUzwK96HwPEMpnCJDzqH8R8dOpYlfL2xY4YtR/m8nEZdpYbqDnnev/xFhccEqij1b/ioUePEnqaphTjp0k+JGwReUYrFKkPdTmiUAkd58KO4SutTxFC9LO1/P48u+uEiPxFTrRN+WPlzYfPmEksb0Ks9iznJ1HWYx795vAntyrDkY1n5MPamn/hjyoybLxL9cZWHDEreYEdnclAb5hiemUc3ICwVI0lTNJfi9g5A5o55LDbl/aTSGANdChzXKB8eO2LQ/9XiX8sCY747fiTTaY6AVOj76Iin8QqrUnKGDanwkTK581nMhibUsXnxmIt8oBTwNFmKqjA822Xbit48CtwkYzTMleF7LpEWwpSWDrNdX2ifN+eoyFURLGEPWxLSX4Tx5cmrPkH6Oz6KaL/u6AEU8ym4Pte9u059d DTsp/h4d VUSvBvf+Dca3r2vCqmAPwI23nnJze5OduDinmF7T2aY+FfL3CyeeIm7Cpg+HKprtxELw/O0F3AFxwjYD6roiAmfZMkJX2tCNMIJgkn4qdK8+7HiXeEDN1/cmToFYYEVM2NNNSl1lcEWnkT4oaPnTSlsWKNvtnNwTmoA3gUghvst1GQy04yZPlW9ZXBZWTgXAYtjtkkZA0Ggf6K7uFlfbPE+y+/FXgq/+V+u/XqLkVlpAQR2bXzG/15lFqrq4okPjufk4G98byGqmbHWgmec37+pA0d0n9alYQlZpj3u7AtudI+sqzmoLSY8mQcEe3yOwdI9f/ 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: Add some documentation of the userspace ABI for Guarded Control Stacks. Signed-off-by: Mark Brown --- Documentation/arch/arm64/gcs.rst | 233 +++++++++++++++++++++++++++++++++++++ Documentation/arch/arm64/index.rst | 1 + 2 files changed, 234 insertions(+) diff --git a/Documentation/arch/arm64/gcs.rst b/Documentation/arch/arm64/gcs.rst new file mode 100644 index 000000000000..c45c0326836a --- /dev/null +++ b/Documentation/arch/arm64/gcs.rst @@ -0,0 +1,233 @@ +=============================================== +Guarded Control Stack support for AArch64 Linux +=============================================== + +This document outlines briefly the interface provided to userspace by Linux in +order to support use of the ARM Guarded Control Stack (GCS) feature. + +This is an outline of the most important features and issues only and not +intended to be exhaustive. + + + +1. General +----------- + +* GCS is an architecture feature intended to provide greater protection + against return oriented programming (ROP) attacks and to simplify the + implementation of features that need to collect stack traces such as + profiling. + +* When GCS is enabled a separate guarded control stack is maintained by the + PE which is writeable only through specific GCS operations. This + stores the call stack only, when a procedure call instruction is + performed the current PC is pushed onto the GCS and on RET the + address in the LR is verified against that on the top of the GCS. + +* When active current GCS pointer is stored in the system register + GCSPR_EL0. This is readable by userspace but can only be updated + via specific GCS instructions. + +* The architecture provides instructions for switching between guarded + control stacks with checks to ensure that the new stack is a valid + target for switching. + +* The functionality of GCS is similar to that provided by the x86 Shadow + Stack feature, due to sharing of userspace interfaces the ABI refers to + shadow stacks rather than GCS. + +* Support for GCS is reported to userspace via HWCAP2_GCS in the aux vector + AT_HWCAP2 entry. + +* GCS is enabled per thread. While there is support for disabling GCS + at runtime this should be done with great care. + +* GCS memory access faults are reported as normal memory access faults. + +* GCS specific errors (those reported with EC 0x2d) will be reported as + SIGSEGV with a si_code of SEGV_CPERR (control protection error). + +* GCS is supported only for AArch64. + +* On systems where GCS is supported GCSPR_EL0 is always readable by EL0 + regardless of the GCS configuration for the thread. + +* The architecture supports enabling GCS without verifying that return values + in LR match those in the GCS, the LR will be ignored. This is not supported + by Linux. + +* EL0 GCS entries with bit 63 set are reserved for use, one such use is defined + below for signals and should be ignored when parsing the stack if not + understood. + + +2. Enabling and disabling Guarded Control Stacks +------------------------------------------------- + +* GCS is enabled and disabled for a thread via the PR_SET_SHADOW_STACK_STATUS + prctl(), this takes a single flags argument specifying which GCS features + should be used. + +* When set PR_SHADOW_STACK_ENABLE flag allocates a Guarded Control Stack + and enables GCS for the thread, enabling the functionality controlled by + GCSCRE0_EL1.{nTR, RVCHKEN, PCRSEL}. + +* When set the PR_SHADOW_STACK_PUSH flag enables the functionality controlled + by GCSCRE0_EL1.PUSHMEn, allowing explicit GCS pushes. + +* When set the PR_SHADOW_STACK_WRITE flag enables the functionality controlled + by GCSCRE0_EL1.STREn, allowing explicit stores to the Guarded Control Stack. + +* Any unknown flags will cause PR_SET_SHADOW_STACK_STATUS to return -EINVAL. + +* PR_LOCK_SHADOW_STACK_STATUS is passed a bitmask of features with the same + values as used for PR_SET_SHADOW_STACK_STATUS. Any future changes to the + status of the specified GCS mode bits will be rejected. + +* PR_LOCK_SHADOW_STACK_STATUS allows any bit to be locked, this allows + userspace to prevent changes to any future features. + +* There is no support for a process to remove a lock that has been set for + it. + +* PR_SET_SHADOW_STACK_STATUS and PR_LOCK_SHADOW_STACK_STATUS affect only the + thread that called them, any other running threads will be unaffected. + +* New threads inherit the GCS configuration of the thread that created them. + +* GCS is disabled on exec(). + +* The current GCS configuration for a thread may be read with the + PR_GET_SHADOW_STACK_STATUS prctl(), this returns the same flags that + are passed to PR_SET_SHADOW_STACK_STATUS. + +* If GCS is disabled for a thread after having previously been enabled then + the stack will remain allocated for the lifetime of the thread. At present + any attempt to reenable GCS for the thread will be rejected, this may be + revisited in future. + +* It should be noted that since enabling GCS will result in GCS becoming + active immediately it is not normally possible to return from the function + that invoked the prctl() that enabled GCS. It is expected that the normal + usage will be that GCS is enabled very early in execution of a program. + + + +3. Allocation of Guarded Control Stacks +---------------------------------------- + +* When GCS is enabled for a thread a new Guarded Control Stack will be + allocated for it of size RLIMIT_STACK or 4 gigabytes, whichever is + smaller. + +* When a new thread is created by a thread which has GCS enabled then a + new Guarded Control Stack will be allocated for the new thread with + half the size of the standard stack. + +* When a stack is allocated by enabling GCS or during thread creation then + the top 8 bytes of the stack will be initialised to 0 and GCSPR_EL0 will + be set to point to the address of this 0 value, this can be used to + detect the top of the stack. + +* Additional Guarded Control Stacks can be allocated using the + map_shadow_stack() system call. + +* Stacks allocated using map_shadow_stack() can optionally have an end of + stack marker and cap placed at the top of the stack. If the flag + SHADOW_STACK_SET_TOKEN is specified a cap will be placed on the stack, + if SHADOW_STACK_SET_MARKER is not specified the cap will be the top 8 + bytes of the stack and if it is specified then the cap will be the next + 8 bytes. While specifying just SHADOW_STACK_SET_MARKER by itself is + valid since the marker is all bits 0 it has no observable effect. + +* Stacks allocated using map_shadow_stack() must have a size which is a + multiple of 8 bytes larger than 8 bytes and must be 8 bytes aligned. + +* An address can be specified to map_shadow_stack(), if one is provided then + it must be aligned to a page boundary. + +* When a thread is freed the Guarded Control Stack initially allocated for + that thread will be freed. Note carefully that if the stack has been + switched this may not be the stack currently in use by the thread. + + +4. Signal handling +-------------------- + +* A new signal frame record gcs_context encodes the current GCS mode and + pointer for the interrupted context on signal delivery. This will always + be present on systems that support GCS. + +* The record contains a flag field which reports the current GCS configuration + for the interrupted context as PR_GET_SHADOW_STACK_STATUS would. + +* The signal handler is run with the same GCS configuration as the interrupted + context. + +* When GCS is enabled for the interrupted thread a signal handling specific + GCS cap token will be written to the GCS, this is an architectural GCS cap + token with bit 63 set and the token type (bits 0..11) all clear. The + GCSPR_EL0 reported in the signal frame will point to this cap token. + +* The signal handler will use the same GCS as the interrupted context. + +* When GCS is enabled on signal entry a frame with the address of the signal + return handler will be pushed onto the GCS, allowing return from the signal + handler via RET as normal. This will not be reported in the gcs_context in + the signal frame. + + +5. Signal return +----------------- + +When returning from a signal handler: + +* If there is a gcs_context record in the signal frame then the GCS flags + and GCSPR_EL0 will be restored from that context prior to further + validation. + +* If there is no gcs_context record in the signal frame then the GCS + configuration will be unchanged. + +* If GCS is enabled on return from a signal handler then GCSPR_EL0 must + point to a valid GCS signal cap record, this will be popped from the + GCS prior to signal return. + +* If the GCS configuration is locked when returning from a signal then any + attempt to change the GCS configuration will be treated as an error. This + is true even if GCS was not enabled prior to signal entry. + +* GCS may be disabled via signal return but any attempt to enable GCS via + signal return will be rejected. + + +6. ptrace extensions +--------------------- + +* A new regset NT_ARM_GCS is defined for use with PTRACE_GETREGSET and + PTRACE_SETREGSET. + +* Due to the complexity surrounding allocation and deallocation of stacks and + lack of practical application it is not possible to enable GCS via ptrace. + GCS may be disabled via the ptrace interface. + +* Other GCS modes may be configured via ptrace. + +* Configuration via ptrace ignores locking of GCS mode bits. + + +7. ELF coredump extensions +--------------------------- + +* NT_ARM_GCS notes will be added to each coredump for each thread of the + dumped process. The contents will be equivalent to the data that would + have been read if a PTRACE_GETREGSET of the corresponding type were + executed for each thread when the coredump was generated. + + + +8. /proc extensions +-------------------- + +* Guarded Control Stack pages will include "ss" in their VmFlags in + /proc//smaps. diff --git a/Documentation/arch/arm64/index.rst b/Documentation/arch/arm64/index.rst index d08e924204bf..dcf3ee3eb8c0 100644 --- a/Documentation/arch/arm64/index.rst +++ b/Documentation/arch/arm64/index.rst @@ -14,6 +14,7 @@ ARM64 Architecture booting cpu-feature-registers elf_hwcaps + gcs hugetlbpage kdump legacy_instructions From patchwork Wed Nov 22 09:42:16 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Brown X-Patchwork-Id: 13464400 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 DE8C9C61D9C for ; Wed, 22 Nov 2023 09:43:42 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7751C6B0596; Wed, 22 Nov 2023 04:43:42 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 723D06B0597; Wed, 22 Nov 2023 04:43:42 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5C5BB6B0598; Wed, 22 Nov 2023 04:43:42 -0500 (EST) 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 49DC26B0596 for ; Wed, 22 Nov 2023 04:43:42 -0500 (EST) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 24F8880574 for ; Wed, 22 Nov 2023 09:43:42 +0000 (UTC) X-FDA: 81485103084.26.B3CB729 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by imf25.hostedemail.com (Postfix) with ESMTP id 2A174A0019 for ; Wed, 22 Nov 2023 09:43:39 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=IkXRbg5b; 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=1700646220; a=rsa-sha256; cv=none; b=xDZ8OM2fll4s3aPpQ2bT5ZqPUNoHgpAYDqmGRmOBmBX2ZCVQjIbBt998v1VYyKcfruATRv YpxxvxuSN4SMNJW1VO55OqHJi/9GgFUTCYvARqTCAYIy//CVBzeR4EJfIcKgQo5htFUFNa Iu/lHPcOeFBICdbeQZctElHq2Qidhvs= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=IkXRbg5b; 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=1700646220; 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=ICticieoVdVv9tpuonXHXGMhklRlVeNH6zs4ecRw8u4=; b=h+XMQB46W/lL6oTEsDargyPhg9Yt7DuZFqz8ZqpTZI1Jge4jHYm9vSMb9kiE6aDq8qt9Uo 1XvTCCyVpgEHiBHDOvqChg8Ck5GjUkgxHJERbJ2nPaEqExD411UOqS9VGaTNIlodjqP64M Tyd99QKWGhLdih2dbSmDdpGhhzwgq9o= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by ams.source.kernel.org (Postfix) with ESMTP id A5BC5B8118F; Wed, 22 Nov 2023 09:43:38 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 130AEC4339A; Wed, 22 Nov 2023 09:43:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1700646218; bh=sAPCPg94+tnQw67CJNTbw1zQ3juZmRAe+Eh6kRT4gRc=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=IkXRbg5bBWSUR+v3gfhiN3UWClgOifXCfJYdSSioSWvlRRW0PeQTS3840LnJeHTZ4 VoP/bZznXx/D8Zmd8/wg3SHiiXpdq9G7qXu0SHuGSzY3E9t8R3OaZw8DlZc41Hl497 kdEEsWXJpHK1VK2rDYLui9+R7Uo9Mlb9NwLVVUh7IldVFd9eFzYERwTx68oGWAaxwD 4cHNbjRbM4lBZ8/5G5l4KIIIyrrEi3vmyEqksXhSzmqSPbiTjemvRhc3jDYyfjIY4f tzU5DbkQd+AXeKkMRQ8qtIW90WHtI6KFwV0/j7Om0fl0b/6+HV2Eid1Lr4gKKzkPUb iKqusnHn1iNIg== From: Mark Brown Date: Wed, 22 Nov 2023 09:42:16 +0000 Subject: [PATCH v7 06/39] arm64/sysreg: Add new system registers for GCS MIME-Version: 1.0 Message-Id: <20231122-arm64-gcs-v7-6-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=1873; i=broonie@kernel.org; h=from:subject:message-id; bh=sAPCPg94+tnQw67CJNTbw1zQ3juZmRAe+Eh6kRT4gRc=; b=owEBbQGS/pANAwAKASTWi3JdVIfQAcsmYgBlXcz7ZHl1qts3vCkTyLrvdIYGWOcI1zxCo5W7w MWcYuLMMqmJATMEAAEKAB0WIQSt5miqZ1cYtZ/in+ok1otyXVSH0AUCZV3M+wAKCRAk1otyXVSH 0Ni7B/4s8EgRGzCroRrqSGQDamkc26auatPOVjhjcDAF4UzqAGfqte6xSPGtlM654rV6oxw4S74 ScqJVKLmOfCAkvCwX5BxxAlOSdeMqOK/FgKj9J+cM2W5+0AVlvUZv8vAGZIelYeutlHEFoMLD9P r+ONekQALp7T/WiFNBAEbhIqQK7O3EUizX/yHODMJPOzkRMgvLifseFZkrbV5bTKSKVr0pCB18m MCcrHY/O0TwRVMvKL5AYsOCdHTbLkn+Bj+t4hMu50SoZTrAN/9sJa+XlLNgT2Sm5DtgJNxunb1g ownRsis25uUJ0pSZSxZzqvPmmuSJZVa/rIVWAMDmhxRNHGC4 X-Developer-Key: i=broonie@kernel.org; a=openpgp; fpr=3F2568AAC26998F9E813A1C5C3F436CA30F5D8EB X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 2A174A0019 X-Stat-Signature: 7pbcnur5akyinm5rkoiu3hrpcbjyf9sd X-HE-Tag: 1700646219-55593 X-HE-Meta: U2FsdGVkX1+X+s/z2d6FuedH3sVw1eH5TTjXKnnBAWi5ACLRbL7WwYIZYMbTw4GnVLRCciQ4wyxiwqFoOJHVgSqx7PRm6O4+rbNSneyp7szAvTpL0bG80VyEYbyUBjFFOVzV3chfbjaBF54DQg5TvRAPDgOSVU3E4YwSFKVrNMFSEP2ECyHK2YGIHKGDCLYdL9ItWUeZIxH6o88UjeMQW+HdxDwx1ZgkMDVcdBciaJYgy4aymUyWo5BSbNOq+IlD7pMYOdf4Mf+mEKl32Ys8cBuuoZcW+05Pxr/4vVlpBOlTBcxeQzyuuwf1lRzQVPha4nZRIqpv+cNCNXaGHMj7V5nY0K8PYWSBUsOMKVOes31ZtWS0PiVraPsAcd4fno98x90mwWnBX3zAN47x59wVpj8WjzxE/bgE6qdcBixm15dzLNesiuQKlaZoRwQ5Nl1hZa+KaSNCsCtVOh2376dFgWv1ls2UXtapEZaWTwpXzMjSfXMi75jfhpuXtQJoatPcSnzDnG8ZJG66jtinYjdp4skftrerMUgC9Cx7C7l0+Icjz5vVupv4w8B9DL2bvXqXKgCk27wNwYuTEj+aNDOY5jluS+5R2nhkEAVUWFn8qvWpW+yXXOFriBrTBTTp7Wdi9xglnU3rn0Do61AOEfr50Ssvy0Ki5SWJPNyGX34lfje6xz45lVsCi8PYY4qeBtQ6U8GYn67Q9K94c9YatbMVwjWOadYNG0mU71tuD6ta06EeAMwLXdmWrnf75SywgcO9ijo8I37Svi9KicH6WnNy6Ez5TxEf+g8pBca2SK6X9TzuXduW/XWtHEK0/AIqcm+/YAolYPXMKyMBB5hAokCZcyFb3T4hIcISZf5ozjQlK9uQHNeSZhpnb6EJ0JZDHy5bWTP+zFiCn+yd8VTzhkpzEmBp/vBdO1w8zbiKewO9ENZwXytQFKJW1pEnNc0CYPtD+tD4zDZreyVvL81YaCl Z0hjy6OX 25UY7VP108lyUnbj+ujJrlVD8HSJPr6urrj05vzkDMKvoBkQ/Fytp0SJDQydbYHu0A19O8A4U/XbljCnUZ4AUjMO2SO78NVkmUnF74AcQ9bwy38xahNzh9C3llQXlaiIYjGSHBfImFgTHr9OXcpcWOTM3JOXL7Ze2xxHfr3vgsBZYAqye9osVlZ4UDq9fL+acEij1FUDq+Pn8KZFL57sNSMmuHYIFTuWbTVWd6YLQB02orlyOMu+MRAZ5gpsazcmt6/JYTDiXQFJ2TzvAyxIC2fzF4tamEwSYKiRFNawzpGgoD5mTO06RccqnS0nDbQKSZTCp 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: FEAT_GCS introduces a number of new system registers. Add the registers available up to EL2 to sysreg as per DDI0601 2022-12. Signed-off-by: Mark Brown --- arch/arm64/tools/sysreg | 55 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/arch/arm64/tools/sysreg b/arch/arm64/tools/sysreg index 96cbeeab4eec..d2ee46b63482 100644 --- a/arch/arm64/tools/sysreg +++ b/arch/arm64/tools/sysreg @@ -1790,6 +1790,41 @@ Sysreg SMCR_EL1 3 0 1 2 6 Fields SMCR_ELx EndSysreg +SysregFields GCSCR_ELx +Res0 63:10 +Field 9 STREn +Field 8 PUSHMEn +Res0 7 +Field 6 EXLOCKEN +Field 5 RVCHKEN +Res0 4:1 +Field 0 PCRSEL +EndSysregFields + +Sysreg GCSCR_EL1 3 0 2 5 0 +Fields GCSCR_ELx +EndSysreg + +SysregFields GCSPR_ELx +Field 63:3 PTR +Res0 2:0 +EndSysregFields + +Sysreg GCSPR_EL1 3 0 2 5 1 +Fields GCSPR_ELx +EndSysreg + +Sysreg GCSCRE0_EL1 3 0 2 5 2 +Res0 63:11 +Field 10 nTR +Field 9 STREn +Field 8 PUSHMEn +Res0 7:6 +Field 5 RVCHKEN +Res0 4:1 +Field 0 PCRSEL +EndSysreg + Sysreg ALLINT 3 0 4 3 0 Res0 63:14 Field 13 ALLINT @@ -2020,6 +2055,10 @@ Field 4 DZP Field 3:0 BS EndSysreg +Sysreg GCSPR_EL0 3 3 2 5 1 +Fields GCSPR_ELx +EndSysreg + Sysreg SVCR 3 3 4 2 2 Res0 63:2 Field 1 ZA @@ -2348,6 +2387,14 @@ Sysreg SMCR_EL2 3 4 1 2 6 Fields SMCR_ELx EndSysreg +Sysreg GCSCR_EL2 3 4 2 5 0 +Fields GCSCR_ELx +EndSysreg + +Sysreg GCSPR_EL2 3 4 2 5 1 +Fields GCSPR_ELx +EndSysreg + Sysreg DACR32_EL2 3 4 3 0 0 Res0 63:32 Field 31:30 D15 @@ -2407,6 +2454,14 @@ Sysreg SMCR_EL12 3 5 1 2 6 Fields SMCR_ELx EndSysreg +Sysreg GCSCR_EL12 3 5 2 5 0 +Fields GCSCR_ELx +EndSysreg + +Sysreg GCSPR_EL12 3 5 2 5 1 +Fields GCSPR_ELx +EndSysreg + Sysreg FAR_EL12 3 5 6 0 0 Field 63:0 ADDR EndSysreg From patchwork Wed Nov 22 09:42:17 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Brown X-Patchwork-Id: 13464401 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 5BEE2C61D9C for ; Wed, 22 Nov 2023 09:43:49 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id EA14B6B0598; Wed, 22 Nov 2023 04:43:48 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id E50606B0599; Wed, 22 Nov 2023 04:43:48 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CF1F26B059A; Wed, 22 Nov 2023 04:43:48 -0500 (EST) 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 BCCF16B0598 for ; Wed, 22 Nov 2023 04:43:48 -0500 (EST) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 96129805C0 for ; Wed, 22 Nov 2023 09:43:48 +0000 (UTC) X-FDA: 81485103336.15.AA332A5 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf10.hostedemail.com (Postfix) with ESMTP id C4C5DC0021 for ; Wed, 22 Nov 2023 09:43:46 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=qs5YB6BE; spf=pass (imf10.hostedemail.com: domain of broonie@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=broonie@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1700646226; 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=TZ0ZwEmlv4C4jxhX8WtZ7aOr+nW8SCVOLuCCiD9LGas=; b=VbzS7p2fYcDijDwvRoEL5vIEDwAgiRrEeTiMD2gkI/XauayV9cEIYjgRicKwbzPWZVQZn0 w0Xjy/XQZ0R42hd1Fh52wMgDdrDRgCLKH6upHlHgRVXtZIjFy2wmaO4P/C3mFtoHK6lgUq QM7md8FPa42xY8rH9pJ93wELuGaBWfs= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1700646226; a=rsa-sha256; cv=none; b=M4HaAgQHf9TPKzjEP39zVCy3Ixmcff14FmXe1VKpqZF64741JgkFx7cKLFIImQ+sk19bdC nUMl5ee9Ngks+PEnot8nvR3h53b5sMgXSBra677jbvton8OBJl+jAuRdpjJKUe2nADL2CT LLDZImp0Z7RaUP9PUryrBhB26obBdpM= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=qs5YB6BE; spf=pass (imf10.hostedemail.com: domain of broonie@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=broonie@kernel.org; dmarc=pass (policy=none) header.from=kernel.org Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id D868161CD1; Wed, 22 Nov 2023 09:43:45 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 77F1AC433C9; Wed, 22 Nov 2023 09:43:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1700646225; bh=AdenxNKaKLfO1zzZHzslCIJebdi1YPs1wuXZd9U1dO4=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=qs5YB6BECnMAmghk+5k7QLjQ4iTf/O6w9uwKHheCTbxYsU4dCielXXbKpJWvVJbOO eHvstQrw3PY/tc2Itmd0DGJ7a9XLXgveUYEreP0NdC2bZwRKN8Csxmok8dA98yK6jJ pc/VhN++Iddv1n65+uxrwhQVBNX4wTnb4RntXV6p1LG9HjaGajgFeuYEg7d8W2vnS6 DsVLe11iXrOgM+nfF8hv0a4E3fBTkDuQgmYeEb5x+merErzYBI1UBOYtqum9DhkAEu iz1H6w7wU93+McmzZK2pTguofmFSA0eW8JcDbFbW+BkqVIf9s/MphpA8RcRNk9/QCg +DKBkAjfQe8QQ== From: Mark Brown Date: Wed, 22 Nov 2023 09:42:17 +0000 Subject: [PATCH v7 07/39] arm64/sysreg: Add definitions for architected GCS caps MIME-Version: 1.0 Message-Id: <20231122-arm64-gcs-v7-7-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=1363; i=broonie@kernel.org; h=from:subject:message-id; bh=AdenxNKaKLfO1zzZHzslCIJebdi1YPs1wuXZd9U1dO4=; b=owEBbQGS/pANAwAKASTWi3JdVIfQAcsmYgBlXcz8bmHxgHWCFRqVN8uz2M3iReNk3BZizcael fYUhMZ5DoWJATMEAAEKAB0WIQSt5miqZ1cYtZ/in+ok1otyXVSH0AUCZV3M/AAKCRAk1otyXVSH 0F7RB/4ql9JuD5+zDZkKa3IqDccJ/whQ1MzEyDazvd5D9iSOO2uFVWKf/mgBe42Co9F7iJXvawY fnM4HLJ+2I6Jpps5E2t0pRb5MPSbFPYrmiDpnpwyH+gCNdD/ISBWGGKi19hQLMzotC2usKxe7n/ ZPPgZlodquDSXAvzd6yBK/QAT+KqARuuUYjttCxdlJOeU0BHNEbhz8/owbin6JfrC1GTlcGGFai aASFR2cqBA+/sxYNdN9NjVEQv2zxSBdFJ+zVZ1qt29sVNQq4+UVEYWYK5MXwadGBn1rZoKRfQA/ I2UNt33vEzbJko8U/aAkwKFHIOuyMBeaY7xivvSZlmH0QBVz X-Developer-Key: i=broonie@kernel.org; a=openpgp; fpr=3F2568AAC26998F9E813A1C5C3F436CA30F5D8EB X-Rspamd-Queue-Id: C4C5DC0021 X-Rspam-User: X-Stat-Signature: abzpx7cdz9i1x11uxr7po14q3z71h3y1 X-Rspamd-Server: rspam03 X-HE-Tag: 1700646226-732047 X-HE-Meta: U2FsdGVkX19RnM82PjYFzlpGNBKVTHXCTCHy3it13owcnEdW/KHB5BFRzPqO+mnd9AG04IUqdLjsesOV1rxm8jesgAcTA9X3B9VKEWuYxtmA3X1RM7Ku5fbSKx1AFxGJ8XFGJITx12JutQAiHcbAaxSMvltTmTLrYJXrpLevfYrDCFBq77KvMv12CDsIKGcK5eTpLlbI27D0aCq75yVp0aBrtJ6td1kPdMsTCn/jFbDOeBxA5ATmYZE1oy2qUx35ljpf7tlZjGt/smZ9qdMe7Va7B/56m3J4Fo6ZSL8rzduPaFV7yf24dZVEDDs++jBlkewNA1hw7+JOkRIN5pdWhW/hfJj2LHirvY6NSqZfrf1/PjXJnC3kS2nRvTJ4mxccCi91D8OjtZD0tnwSnVebpLPpzGsEkSl4T/wQujz859ff+6eG8Ek9GhqUUiUyrk0uQf4J5y4ehO3nMTFfqnPqX3RgbbMtqnzkZQTxoLKo3klYYECkoPgiU9MZ3LMEjb3xL7y0zcKmurt6rqFSp4xivwpSnAnI3Af8LNP5gvWDXuwZ6KGf3MhuzgAcQYC+ZX+fqp6OupzBZzrtY4p3PkFGQpWdFrUV/Z7bOug1FgI8eQEGtv1j8Kys4U669OR9xvDjdei3wgTXrWeHHV/W1Lc1sTy9mMXIv5aemBFpQ5qrU8EDJr7rh1mtX5bD9RrCphqlGzlAH60s4dwrJdXfEMfiKJG360WWiq11uXABbbYR2BouzdKZAAYAsATlw0QEaylTZAw+SXDJ07rl8qEkX/yJi/ZQqi5TNYKEmzsNiVkE5RDOFzzd2tY2ACK3LpmIZfwl7qhTyrSjGom30LADajQIDgxK0jBq4c3UAcvHxiPrOFSawxlqv3652D0auXh3CH5HOOoNwl6Ut5yzeg5ZUPcB3tEdGw9uFRwwzft4fopnhErhb8EOQFOqMVZzQ3D14mqUBmRnVVJLpOfbZx+1pxl RPB742sl 5LVcJJNCHDCcgHB33/etrHJopck8FnpZo35v01WfwTNtm8zlODWmbJN2dLhmbYgX4oP1UP4y8OpBnrQfvXQ3bGqQFGXF0PK6fs/TC1LulQxlY02SJM7ezhvKRStd5F6DxKKuKOETfZ1C2eoVbINdGw7Ytg2FQFXmjaw6j6uX7Po2KDalozcCCkQ6NNb0vfzR0KJnyB5NnkvVtt9cSQqxSDWQxb09DrloKYzQkE1bWPWatVuYLXvuPyPqawn2fOAkNIMfIKBjeZ7I+ddMlvmjCwAqGfLWPOlN/L5wL/4IGwz70+w4X0JI3+guUPytpSmIkx+J6 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: The architecture defines a format for guarded control stack caps, used to mark the top of an unused GCS in order to limit the potential for exploitation via stack switching. Add definitions associated with these. Signed-off-by: Mark Brown --- arch/arm64/include/asm/sysreg.h | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/arch/arm64/include/asm/sysreg.h b/arch/arm64/include/asm/sysreg.h index 5e65f51c10d2..75a49b41fa93 100644 --- a/arch/arm64/include/asm/sysreg.h +++ b/arch/arm64/include/asm/sysreg.h @@ -1039,6 +1039,26 @@ #define PIRx_ELx_PERM(idx, perm) ((perm) << ((idx) * 4)) +/* + * Definitions for Guarded Control Stack + */ + +#define GCS_CAP_ADDR_MASK GENMASK(63, 12) +#define GCS_CAP_ADDR_SHIFT 12 +#define GCS_CAP_ADDR_WIDTH 52 +#define GCS_CAP_ADDR(x) FIELD_GET(GCS_CAP_ADDR_MASK, x) + +#define GCS_CAP_TOKEN_MASK GENMASK(11, 0) +#define GCS_CAP_TOKEN_SHIFT 0 +#define GCS_CAP_TOKEN_WIDTH 12 +#define GCS_CAP_TOKEN(x) FIELD_GET(GCS_CAP_TOKEN_MASK, x) + +#define GCS_CAP_VALID_TOKEN 0x1 +#define GCS_CAP_IN_PROGRESS_TOKEN 0x5 + +#define GCS_CAP(x) ((((unsigned long)x) & GCS_CAP_ADDR_MASK) | \ + GCS_CAP_VALID_TOKEN) + #define ARM64_FEATURE_FIELD_BITS 4 /* Defined for compatibility only, do not add new users. */ From patchwork Wed Nov 22 09:42:18 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Brown X-Patchwork-Id: 13464402 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 BBAC4C61D9C for ; Wed, 22 Nov 2023 09:43:57 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 552956B059A; Wed, 22 Nov 2023 04:43:57 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 501896B059B; Wed, 22 Nov 2023 04:43:57 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3A3FA6B059C; Wed, 22 Nov 2023 04:43:57 -0500 (EST) 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 28B3E6B059A for ; Wed, 22 Nov 2023 04:43:57 -0500 (EST) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 0787214012B for ; Wed, 22 Nov 2023 09:43:57 +0000 (UTC) X-FDA: 81485103714.15.7063AD0 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by imf10.hostedemail.com (Postfix) with ESMTP id 0D4E9C001C for ; Wed, 22 Nov 2023 09:43:54 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=Jo0eqnhF; spf=pass (imf10.hostedemail.com: domain of broonie@kernel.org designates 145.40.68.75 as permitted sender) smtp.mailfrom=broonie@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1700646235; 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=oHZQJerEJ1fGH3Iz56dN3F6DX5L5AytuCbA413DTvB0=; b=KzQG0KkcyH2b9wy9yCO8HLETreYNYOZBuTLAd9To94xU5T9T24/4Lq/gOLiddvx4zdAb3V XbyYyTKPe8Ije4t8uUJDcJNB7lbj7Vffd9A5PH2Z4u0lb9a652xC0aAnYqzfAAcy848VRE JPvveIGgZkQjwfIOUtOR+NmvV5n2QgY= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=Jo0eqnhF; spf=pass (imf10.hostedemail.com: domain of broonie@kernel.org designates 145.40.68.75 as permitted sender) smtp.mailfrom=broonie@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1700646235; a=rsa-sha256; cv=none; b=lEnKoAcwbCujinnPq9LFMS3rWCCWE5NqXkr0Y/UiWZ+u8XMwXFK88V63R49t+soltTIWFa iuzYMwjnAHObg4AxuC3wcwhhOz2lW5mrN/J9rWHIesQ3EzZDZAAwNGHjHovGavvyLiu1nX QNlGouHbedVwjHx1GzfiuVomyEfuGTQ= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by ams.source.kernel.org (Postfix) with ESMTP id 67E5DB81190; Wed, 22 Nov 2023 09:43:53 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1CF86C433CC; Wed, 22 Nov 2023 09:43:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1700646233; bh=Kj5ys8EZzcB9lauTa/2n/TuM+KU70CYN6cCdyA5umEU=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=Jo0eqnhFka9a7dffiKyy/h01WlRwGEogNuM4GOLoxUdOw9GTlY8Y8IsRwkGQAzVps ZejwMBmDzIrkQh7K7q2pQDPrZXsbBtCixxWp/qAS7BsVPKK22IOAiUXVCd05mRyTBF 4sHtlCXV5S5ajmQ08Ha0/qmbnTXoNEuvYf8Rpl3kx5lqfIJwozNKfyvlN0ixlabSCs z0tRYzkYowakVqbhA+GWa06AGpTmKfBv+oJbb2Y21eWQZergE/uRiRxQ0pVmATJcWM 102wKL6RaMwQ1C6yALuvXYKN5CI7cvjxWoE2cPJirA2ZviLOEi3nlbI0j8VIjMydt6 JEaT3JTOFHOeg== From: Mark Brown Date: Wed, 22 Nov 2023 09:42:18 +0000 Subject: [PATCH v7 08/39] arm64/gcs: Add manual encodings of GCS instructions MIME-Version: 1.0 Message-Id: <20231122-arm64-gcs-v7-8-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=2577; i=broonie@kernel.org; h=from:subject:message-id; bh=Kj5ys8EZzcB9lauTa/2n/TuM+KU70CYN6cCdyA5umEU=; b=owEBbQGS/pANAwAKASTWi3JdVIfQAcsmYgBlXcz8bXL6mSlClcxo2LLkqYujwDOa49MNEAxac 4wcHFzLkiKJATMEAAEKAB0WIQSt5miqZ1cYtZ/in+ok1otyXVSH0AUCZV3M/AAKCRAk1otyXVSH 0GQIB/9o9cqptsu1ZwBDibIQWSTMXx2jyDsfuRCayo0AmyyLwhAIo2lXL771m3LtQpEJubjNSO3 PVjPlRoT2ipPWpt735Fg5NkUgjzEcT6xeT/oG5/YpHULuKMwSq9zvHV+woRV+lYiOZItIrNcgu+ GvMa1MuPADfJzXfw7QgtoClkc+6OfPC5vTsygXo0CEhRXV9O2659pBW0fQZPRmIa42fXbtTlpNK j3l29HNk16rpf+JFot2wdynC8PLWnTH38WB0LF2d+f+czqx+mOQs2STMWDPRpTF43MOm3QEIIJi SJNNHgxgi99OxRRLxuunS9Cf4qkX9bBRfInK56jnN8VLQHMW X-Developer-Key: i=broonie@kernel.org; a=openpgp; fpr=3F2568AAC26998F9E813A1C5C3F436CA30F5D8EB X-Rspamd-Queue-Id: 0D4E9C001C X-Rspam-User: X-Stat-Signature: kwwme8wp9z9k8zs678kd4a5nohm1b7qc X-Rspamd-Server: rspam01 X-HE-Tag: 1700646234-236846 X-HE-Meta: U2FsdGVkX19iFjY++x8tgZ92MmKF6E0yZeNy0j/wqZT3HmmBqeTbdwRzf4mTHkoo2B8ZFJFH7fpbPHiEdfiB8XvnEXXK5icQaCTT8xPWUaRWVV2lD3O2ZzwZPXK/BxzBoj/LOHpqFOYpEqote7bbNo9BIjmr4htLONKOtfwvTlB8aVKBaohQVMCDJhrdmx/RbxSMcucX4TaQORZzrm/z5D4Uy3nEMPEFIM82LGOXgO5DZlx22Qsjfk5PRGIBA6HEEMxqBdboKNff4kNEdpzxdXNu/uag8unLN1U2STB4LSHNZIPylWhcwwXXi9dKztMKtyphv1IufGo+9/ef24sG1fNoncXVUht5YjyWLPnzI7OhwjPAJkNUJieo5Jea5HySNAyw2uD/4GbRLq8MY6DV05WKDNcjx0At8p12bPR3TZFb6ZtQYRknmDrWMwD6SWtaO8zL4mmhDUE/+awu5Va2zUTkHYt0kgF6iFkiLY5uNbz3Ien32s8dSDIVinao+Xm9CU/d2yJ3s83BrDo1DvAP1O508UZ2wD4Bql2H3V34s6V/8922/JjSTXDS5/tzh993OW6IQnXlCMRwDRbBa3Fg7rHYMtAjKGQMm6wVQAlKfF/WuwohGj8uqhqq4nHDKLqE7emmn7hO5HsPxzQoN/HjejU1U+YiPuSpzyi+TA40qJLz5aHkxCv+lTV86UOZytQkmKx1fVyaOTNIg/hpSr8Z0PXA5MH3ZPLcO7xg8RvYpsdBq9uMva4+WrlZK8OnJ1OBmmFp8GVivXEbk8jFVY+Do0EqmWGfAq0DsR3FREG4E+iD3Mfn0bOgZNbvxWUkqZHdbi7TIAAswyJVtrGTb8MpvG+fBiI4UIrLoRDrnG21xeaT8MpIyjopoWIcdQ9x9JOMTnulCP2e/wSCScUOZvkpboaA02pKIrxOUX+p3d/ipjRs4imgUFFgmBsKMzFY19AS9T8Nifd02lMSC8brOQg bNh+W4Su 4L8XJkfbPG+9cfyIsemUV1/dxtZET6Lu5TYa7AFejpqLmX2bzF+7vFP2bEvg2+mIAjnhTc2snGtoO1wFooF2Q2w/8zgoRuCRVRgNwoST7MoI7nJJR7n1H4eeBNgX9qE2kggfm+F9ShqM7EDU7V6mA22uQtMbO0u7okS2Lync0+1P3QT0rCOPfZkuMVPyPYCfeCHh1rUdCuTI5EiZVDUmeF9SOTOmqgbKevmNtCzFA8xNY2F89jCU1lhhbDxNN0DE8xiCkgpbTwmCOTjlJPognZCsH3dNN2iUCn2oSk7gAIgkjhxnHUh9U0AnmStUgL2cd1/NX 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: Define C callable functions for GCS instructions used by the kernel. In order to avoid ambitious toolchain requirements for GCS support these are manually encoded, this means we have fixed register numbers which will be a bit limiting for the compiler but none of these should be used in sufficiently fast paths for this to be a problem. Note that GCSSTTR is used to store to EL0. Signed-off-by: Mark Brown --- arch/arm64/include/asm/gcs.h | 51 ++++++++++++++++++++++++++++++++++++++++ arch/arm64/include/asm/uaccess.h | 22 +++++++++++++++++ 2 files changed, 73 insertions(+) diff --git a/arch/arm64/include/asm/gcs.h b/arch/arm64/include/asm/gcs.h new file mode 100644 index 000000000000..7c5e95218db6 --- /dev/null +++ b/arch/arm64/include/asm/gcs.h @@ -0,0 +1,51 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * Copyright (C) 2023 ARM Ltd. + */ +#ifndef __ASM_GCS_H +#define __ASM_GCS_H + +#include +#include + +static inline void gcsb_dsync(void) +{ + asm volatile(".inst 0xd503227f" : : : "memory"); +} + +static inline void gcsstr(u64 *addr, u64 val) +{ + register u64 *_addr __asm__ ("x0") = addr; + register long _val __asm__ ("x1") = val; + + /* GCSSTTR x1, x0 */ + asm volatile( + ".inst 0xd91f1c01\n" + : + : "rZ" (_val), "r" (_addr) + : "memory"); +} + +static inline void gcsss1(u64 Xt) +{ + asm volatile ( + "sys #3, C7, C7, #2, %0\n" + : + : "rZ" (Xt) + : "memory"); +} + +static inline u64 gcsss2(void) +{ + u64 Xt; + + asm volatile( + "SYSL %0, #3, C7, C7, #3\n" + : "=r" (Xt) + : + : "memory"); + + return Xt; +} + +#endif diff --git a/arch/arm64/include/asm/uaccess.h b/arch/arm64/include/asm/uaccess.h index 14be5000c5a0..22e10e79f56a 100644 --- a/arch/arm64/include/asm/uaccess.h +++ b/arch/arm64/include/asm/uaccess.h @@ -425,4 +425,26 @@ static inline size_t probe_subpage_writeable(const char __user *uaddr, #endif /* CONFIG_ARCH_HAS_SUBPAGE_FAULTS */ +#ifdef CONFIG_ARM64_GCS + +static inline int gcssttr(unsigned long __user *addr, unsigned long val) +{ + register unsigned long __user *_addr __asm__ ("x0") = addr; + register unsigned long _val __asm__ ("x1") = val; + int err = 0; + + /* GCSSTTR x1, x0 */ + asm volatile( + "1: .inst 0xd91f1c01\n" + "2: \n" + _ASM_EXTABLE_UACCESS_ERR(1b, 2b, %w0) + : "+r" (err) + : "rZ" (_val), "r" (_addr) + : "memory"); + + return err; +} + +#endif /* CONFIG_ARM64_GCS */ + #endif /* __ASM_UACCESS_H */ From patchwork Wed Nov 22 09:42:19 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Brown X-Patchwork-Id: 13464403 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 2D3B6C61D9C for ; Wed, 22 Nov 2023 09:44:05 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B95366B059C; Wed, 22 Nov 2023 04:44:04 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id B44206B059D; Wed, 22 Nov 2023 04:44:04 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9E4B16B059E; Wed, 22 Nov 2023 04:44:04 -0500 (EST) 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 89DB46B059C for ; Wed, 22 Nov 2023 04:44:04 -0500 (EST) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 4CC3DB5AB8 for ; Wed, 22 Nov 2023 09:44:04 +0000 (UTC) X-FDA: 81485104008.01.E8FBA0A Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by imf25.hostedemail.com (Postfix) with ESMTP id 68135A001B for ; Wed, 22 Nov 2023 09:44:02 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=skgEBYvx; 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=1700646242; 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=DV5bVXvTe0o/ha9GRNIs1yhe3QvFX1UsR7szIYiQiBI=; b=Ca1NVlYMigI8/870ZHrBpQCPDsC7hLTLR8wJzvhgB6tUzMKEKQ4JAMecJ+oV3YiN+RgTqL QJmjLRdx1xFcw6iJaXjbz7yUvLtA3ICa6x4rdUXUPJAzCFPYQBq+W69PnDgGljog9k98DL +27SraCZeq+3OI71KYU1TdNf30QdQ6s= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=skgEBYvx; 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=1700646242; a=rsa-sha256; cv=none; b=sMeHpXKYOKHDGTR8YE2mLO0RsSfzlhVdImjktv5eGVQUY2bT9jzBn3G6Rfev7uF8TMwT+Z GZcXHa8ALXw8tQlFdr/HpbW8It3xSet4ctqd40AsYwN4IKBA/IfsjMpFXA/iSltwB5/5pO LFrvy+m4zY+7A0RaQvggB8zIyuWhJrg= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by ams.source.kernel.org (Postfix) with ESMTP id 2DFF1B81199; Wed, 22 Nov 2023 09:44:01 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 96852C433C8; Wed, 22 Nov 2023 09:43:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1700646240; bh=vd1vSvagODzOx6kbpI7IvwSkS4eBsjmFRyLwRdBrVrA=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=skgEBYvx/fAYU83PbKMGhZ+jFRWnelXbJMhtlbDO94lZzbLAQu2e6sHPZRgWHd+sx gtvmAgwjSpvlSEQjQzBmlxxsXWS82g6bxtvPaFkQ8pvd5rEGx1NUa6mmsajYrS2HXJ 9SUa7yYSQ4By+Yl/Qgm1N6yHb1EuBPq31sojdUVFxdN/L6m2Rc5/En1jfZfcN6fVd0 1Hue8SThE2M1637XnM8B3AjO9hFNOP+uQE1W0maFQM8JCIjJ7WlFtq4sCyHcLk/Da8 60C8qJmM22SVHPuFPEY+HS4Qp3f7l5E4nEP4L2Ac7hl6Qa7XDz7Xjm83GZP/ILzYcJ 5IDYaXeS3YwZg== From: Mark Brown Date: Wed, 22 Nov 2023 09:42:19 +0000 Subject: [PATCH v7 09/39] arm64/gcs: Provide put_user_gcs() MIME-Version: 1.0 Message-Id: <20231122-arm64-gcs-v7-9-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=1037; i=broonie@kernel.org; h=from:subject:message-id; bh=vd1vSvagODzOx6kbpI7IvwSkS4eBsjmFRyLwRdBrVrA=; b=owEBbQGS/pANAwAKASTWi3JdVIfQAcsmYgBlXcz9ntfDGYRfW3xISuNKutZJXpSjiIRaViJ0k n4sUnukccCJATMEAAEKAB0WIQSt5miqZ1cYtZ/in+ok1otyXVSH0AUCZV3M/QAKCRAk1otyXVSH 0N3bB/44FbabVeFeFQXmXvNQGbQr73EvxPiLmtFhVunzGXVexn0MqHJIPQxDC2ugEGWeKqK+7nz mHcpRXkSvzsDxa0SVk2G9hz7TrXNC6LMK87Ap6mUxpUx/sl71tnM08rKVQzi+iw4Ye5ZPtSiDWW L5diQOrA4wDNpimxkSurVohp7umfjFe/leUGxJGrezbj+MEWgxqb/endM8PgCOg4sdzQBljGFdX WMXW9ZlcwjjEOdBjomF3WAbeNIdpvDJQHFW9t9p9cLOLrxovGepwQ/qOzb7pt0bnZZ/ely3LOX/ 7IrP/ur/Ss2aXoZkMeS2okOUMjg3vSIb9e5S9aaS+RcHJC2B X-Developer-Key: i=broonie@kernel.org; a=openpgp; fpr=3F2568AAC26998F9E813A1C5C3F436CA30F5D8EB X-Rspamd-Queue-Id: 68135A001B X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: 4uqy4m3u3o7uu7sfu47jfp6ubhjnf7kw X-HE-Tag: 1700646242-630863 X-HE-Meta: U2FsdGVkX1/oRLwJtIGQ0fKcDxU5eLDLCze+6xGJzT+bzB478b/AaKY+aWNq+aQY+yfl7lidYtcsuFF2kLjNXnH9xiw5rSNH8jpR6ouhQap9DfMupXKebtDmbvorWJsLb8kRmzEOokD4WQ6e+gW/NYezrS0g0XqG9BnXrTXZLR45+Xe9Tpv6ORLRr/Bv7FNEFN7yv6vqUo/O4c7kWOb0Eai0CnJS5RQeLQavdwYeClydjh7Zsm7S5Av0nwmgEPrrKokhYJOhI/nIv0ekpa87Ntp2WQa4GKV4fTj2Cz6D3QO4iFMj2Ri9somKMnjrZAg6CTx6DrRi+7mHjJW7zp2t+dmsSbrpNy4cjAlsstOqqn7UxYbm4vf113z6UjbfSolplAcZ4d50zIhZ2J+8z8o0rcziaa/l358QW9sMuPpXBY6Zw2P+l546LebcFELhjhBnNvw0bLETDaNT3nuWwAOdLisF6n5J0M68omyKTzxDNeUgwPpEquW+tf9b+LSNYCvd6QzJaFmZRoEwtbsuzrZF3F/hcyzdaCS6rh4c96E6iTkTPCHkld3xDPLM1+Emk8uQo3BFRKtahQqjJyETSrG1IpGK3cpJ9PqFo6K5hlnjCldWRRqeRJ6J8mTw9upp0m//JelaiwHTrBniW9qPacW1PHAx7z0THkqzcb+Sbnx9BtZOPy6fXiMRmVbJm1SWWGg+A2DS0DgOI5j4k2MdXAznCzNSPqsg4GD0FvjFVa8iPzmppI7O6ebipL1d+DtJYend4yYUdOR0xm/kvk4DJzwCX180EWNQ+9A+yBtso015uGVlIV1K4YO8TrMapri9J5ZWwlYxVBLKyw++bDQsBePdLe7cC0+jcaV6piCeN3ogHiCzl8J474x4DNsnB1XJMZscyN43aRGp83BWd1kjHo8J1ouIjMw9JjNRFmTwMPE2quhiCDDa5eJuto7aNL1bnxwC5BZXppb+002WMXuiMzu s1IrckYE blCzBR8mt+xcA7dMTn92I3V9ZwLIandJDGAVQwarQiRRAdeYY8pXrGOCL27cQeQNUfI13Onf5yKC0JKX63CumR7tOqpdf1hxkLs7be+/+L0BaoU6a8tMod04RKRiDcMlABurEWKYf7afzEjDdLZVjer+ftzS5QbH5cI75N5AWHLytMI3fTBxsF6Pi45wbsq5kHOs4pWbNCcY4CKZN85JX6zPKHYNk0SkHJ6IoKRevgxOVw+9shrdjDn/fXxh+htUWK+NTqSD/ozYn455RztNq6XR19CA+xCom+FbkOm21K0S11l+XfqCiQxP1C/K3u29y8R8qEL+axjCybIvkMj6Ced0ACS+RgZKs0kBTEwzAL/bjB8NvweX4xcsrySZQGrOhnYo74OvQ0cVA6IM= 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: In order for EL1 to write to an EL0 GCS it must use the GCSSTTR instruction rather than a normal STTR. Provide a put_user_gcs() which does this. Signed-off-by: Mark Brown --- arch/arm64/include/asm/uaccess.h | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/arch/arm64/include/asm/uaccess.h b/arch/arm64/include/asm/uaccess.h index 22e10e79f56a..e118c3d772c8 100644 --- a/arch/arm64/include/asm/uaccess.h +++ b/arch/arm64/include/asm/uaccess.h @@ -445,6 +445,24 @@ static inline int gcssttr(unsigned long __user *addr, unsigned long val) return err; } +static inline void put_user_gcs(unsigned long val, unsigned long __user *addr, + int *err) +{ + int ret; + + if (!access_ok((char __user *)addr, sizeof(u64))) { + *err = -EFAULT; + return; + } + + uaccess_ttbr0_enable(); + ret = gcssttr(addr, val); + if (ret != 0) + *err = ret; + uaccess_ttbr0_disable(); +} + + #endif /* CONFIG_ARM64_GCS */ #endif /* __ASM_UACCESS_H */ From patchwork Wed Nov 22 09:42:20 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Brown X-Patchwork-Id: 13464404 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 E3B65C61D9D for ; Wed, 22 Nov 2023 09:44:11 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 887B56B059E; Wed, 22 Nov 2023 04:44:11 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 835B76B059F; Wed, 22 Nov 2023 04:44:11 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6D7896B05A0; Wed, 22 Nov 2023 04:44:11 -0500 (EST) 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 59B8D6B059E for ; Wed, 22 Nov 2023 04:44:11 -0500 (EST) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 26AF2140567 for ; Wed, 22 Nov 2023 09:44:11 +0000 (UTC) X-FDA: 81485104302.15.B5C56DB Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf09.hostedemail.com (Postfix) with ESMTP id 5C576140013 for ; Wed, 22 Nov 2023 09:44:09 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=OvKVQHJl; spf=pass (imf09.hostedemail.com: domain of broonie@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=broonie@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1700646249; 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=cEnjzLtEtU2/qgYRbmQ8ScupbG36KHsvEqwwkTuL5DM=; b=hJd27Vro5RhPfqsm2CFlVuCc9905ovecQ9So7a0yIjCLn7rMdT4ERH1OWGh6wB+KsTLqgn SjDDWGL5MXUBW/eDHCfUaisXzRf0q5A+mVsHPxdslK5sK/J37V38KmyUmvD2DmOjeihhBQ 6kDK5D+10gDmIxBCz8ubwoSu2zASBpA= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1700646249; a=rsa-sha256; cv=none; b=Pl/Ou01BBeMIgU2R4Tya/LbXCpf/yfB0cD1fiG+P4FbZN/Z/tRyaQiXq2PMXIe4uU2JSa6 8mEV03aSGqoDtQdv+jxdIzYjf8NXcLV9Zlqe8o3tn7434j27mePPG1B4Yh29HSyPZFNZNK y9VpUN7ycRhVDnkRd1bklCoer7ioN+w= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=OvKVQHJl; spf=pass (imf09.hostedemail.com: domain of broonie@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=broonie@kernel.org; dmarc=pass (policy=none) header.from=kernel.org Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 6160A619E9; Wed, 22 Nov 2023 09:44:08 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 22E86C4339A; Wed, 22 Nov 2023 09:44:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1700646248; bh=i9QyE7h/VNfEY/fQWg1KcUCsnEaqWFpKvbtjF57JZIU=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=OvKVQHJlaRBJIrr/EqVvB1Vhp4g6DB+5CWEBUGNM3o4IyXrYXjYMV9aL349rfMduV iC1sUc8GEMqhX9H+WRYy595ExaJkKlI4MNhh8kzV2HwPjOacNIQul8xtmQvxy/xNWy bJyZ92pIRTGziqyBTlQVmhpRcHecvfbBV8wVxA1Jl3BIzkpyZ6s/ZovvVa/P4DuGKH mxVnXURPLXu73B5K45nsYhJhGwSGJGkfi7MoHRbDJvnCRGQ5ZNTznbOFNqjlxfBHPi GJ1mJL/sYWm4X7XtGJckFaZSRb6p2wAJFNIMhy8vDr+jKDW1Wp5g6OdIf+KPnIfmWR oSzOQyZR1Z4qQ== From: Mark Brown Date: Wed, 22 Nov 2023 09:42:20 +0000 Subject: [PATCH v7 10/39] arm64/cpufeature: Runtime detection of Guarded Control Stack (GCS) MIME-Version: 1.0 Message-Id: <20231122-arm64-gcs-v7-10-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=2922; i=broonie@kernel.org; h=from:subject:message-id; bh=i9QyE7h/VNfEY/fQWg1KcUCsnEaqWFpKvbtjF57JZIU=; b=owEBbQGS/pANAwAKASTWi3JdVIfQAcsmYgBlXcz+s2txHvZTDwLAf6SOVjsd+5BN63pi8i51Z FULlftCXzOJATMEAAEKAB0WIQSt5miqZ1cYtZ/in+ok1otyXVSH0AUCZV3M/gAKCRAk1otyXVSH 0LSmCACGoUb2lLCDHWabC23R3Nx8VwtQ0tQQHq3bVsr9lWag6ifOZORAusKIlhzB1Oof9Iv9cBG 7FBO3D5ZhNs6EMIXSMOy38ifF/x/csaQnxTcH0SQxUa0yxqQabljmKvoALa4i6z2Sae8FCyOu5E bYUX0M3PHsUF5jPmg/O+j9TkHroXSXFGeQgLHN7x67h3d/QTmpeg1SC6AwlkZYnH/dUUQusDF4r mj6E+PFeikOSBCVgHbO/jkc02tozpz97gUJGsZ9H4glsVXGtiH+3gPm2IGliCtYTvx2Jwl0/Mu+ 7ZPEOpq55NzT3FgzhsBiZfL6DeYDKnu1Tu30JsJ4LpYGSAQ+ X-Developer-Key: i=broonie@kernel.org; a=openpgp; fpr=3F2568AAC26998F9E813A1C5C3F436CA30F5D8EB X-Rspamd-Queue-Id: 5C576140013 X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: pfx3hqfb43amh46tbzge8f5g5tjbmpct X-HE-Tag: 1700646249-472828 X-HE-Meta: U2FsdGVkX19J+38x6y4Yct5q/kJFhygbGJjUE0vw3iQIOd95x+aZGLQ8GwCRolAc7V0Axd8Dg9ngt0DPm5s7kqaFZc5STiyL2NB9pWCTWzjlrvYAnLDIMSPQTRY+PN3GkTflbNXWOKI8/XgrAOV0DWAAoiJ3OsdZRSDZ3uXntfdmkFqQ2IFWKkQ7OB6xTXc4LB24C1C7SJNcoU5fA5MjOizas/osn+irljrUAQIQEfi+JciLNgc8twGS47ZRwrdvPw80R6DPdZ13u8CtbfKvodZOBTG/VDjERiuAoytMnZvURNSiTFXawl7X7TWJD/xp2fgS8Lkm1CYrbTtTCPaia04A3NGEB2wTtztZyUhiQnFkTiqfmqSEqOUHLqgKlHHZq6bF7jsdWzvEOpKL3dZBuzFnh2fz2QdsnQruRzQy0WjKnrzQSZ8Ccp9PecpZJfHzzxbXbLggioFcfSarX9cnGSJMnhJyQoLaagLGim5zwjTVkxG6Qo6ofsG56QYXmH5sxHcnH9C3Z0TZvsCNfRelAy0BjXh2POAxpf//ONJ+9WaGNzYtfauhzwkJuI5uDRAgi9IS4USUXvb9OyzzwSzfLsahvQBZBOCsJjspE2QsmIJ5LkqEZkzCfVxjbXg9funKH+n/oq2OSqCG9cFYokH3jLYVGAGHoegBYX+7wtQQyZ7bgVZgUlEJZBhOOzWFjRkeffN57J+r1w4FI5+10E8w5yN8/sfKkEqFPNKS2PO2pV9dbvcr2lPSn5hz80iQnhjht57jdZxFz/mVBs2l2GBrV7ejwXPMtIny6RmBIvss+p6qkFHXJ4uZ+KH2J0c/07QUMgk5QwR3HV6K6tfC5pO+hKddh7uLY3wlse67rWtj1sR4SQxeQUDjN4r31dDzfU58BYM22OQT9BsSSwsyw1vCLlt8aQICV+xAQo/TaZHmsILWsx1RlydfwVeKRJHzBiIzqofW/xWmV42QBX22Z0L wOTxu0Pf wzEqDg6Ue32yBuqVgd4SWYqoYNOYHU2UsSE5iCS7cpYE5LATOctU2emdqqYKfZ5j79x9OKNSikqswwgLaEGhpL8leG2KxuRRjMFC4ituQeQ84oy4glGUY6YKC71tkZFRgSRSje43PSCDfvsn8BrueflbHPDPMQ8Zw2P5fseKI5emj0H5Otsj7CGCPmzVJd4dS0QSyNbf8iVYgCpcE1/CTB+zU73e/fJpXFU6t/ukmnXdR0cD0Mx6YSJ9LJ00RY8yF2mwnoWOMfDXZqOIqcXKWb0Jl+2Lj2a4e+wuNQlcCLKfKthJpRRHca5Kf2hkiYxGrGLxD 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: Add a cpufeature for GCS, allowing other code to conditionally support it at runtime. Signed-off-by: Mark Brown --- arch/arm64/include/asm/cpufeature.h | 6 ++++++ arch/arm64/kernel/cpufeature.c | 16 ++++++++++++++++ arch/arm64/tools/cpucaps | 1 + 3 files changed, 23 insertions(+) diff --git a/arch/arm64/include/asm/cpufeature.h b/arch/arm64/include/asm/cpufeature.h index f6d416fe49b0..5696a55cc1f8 100644 --- a/arch/arm64/include/asm/cpufeature.h +++ b/arch/arm64/include/asm/cpufeature.h @@ -819,6 +819,12 @@ static inline bool system_supports_tlb_range(void) return alternative_has_cap_unlikely(ARM64_HAS_TLB_RANGE); } +static inline bool system_supports_gcs(void) +{ + return IS_ENABLED(CONFIG_ARM64_GCS) && + alternative_has_cap_unlikely(ARM64_HAS_GCS); +} + int do_emulate_mrs(struct pt_regs *regs, u32 sys_reg, u32 rt); bool try_emulate_mrs(struct pt_regs *regs, u32 isn); diff --git a/arch/arm64/kernel/cpufeature.c b/arch/arm64/kernel/cpufeature.c index 646591c67e7a..5477451cefa1 100644 --- a/arch/arm64/kernel/cpufeature.c +++ b/arch/arm64/kernel/cpufeature.c @@ -255,6 +255,8 @@ static const struct arm64_ftr_bits ftr_id_aa64pfr0[] = { }; static const struct arm64_ftr_bits ftr_id_aa64pfr1[] = { + ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_GCS), + FTR_STRICT, FTR_LOWER_SAFE, ID_AA64PFR1_EL1_GCS_SHIFT, 4, 0), ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_SME), FTR_STRICT, FTR_LOWER_SAFE, ID_AA64PFR1_EL1_SME_SHIFT, 4, 0), ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64PFR1_EL1_MPAM_frac_SHIFT, 4, 0), @@ -2242,6 +2244,12 @@ static void cpu_enable_mops(const struct arm64_cpu_capabilities *__unused) sysreg_clear_set(sctlr_el1, 0, SCTLR_EL1_MSCEn); } +static void cpu_enable_gcs(const struct arm64_cpu_capabilities *__unused) +{ + /* GCS is not currently used at EL1 */ + write_sysreg_s(0, SYS_GCSCR_EL1); +} + /* Internal helper functions to match cpu capability type */ static bool cpucap_late_cpu_optional(const struct arm64_cpu_capabilities *cap) @@ -2731,6 +2739,14 @@ static const struct arm64_cpu_capabilities arm64_features[] = { .matches = has_cpuid_feature, ARM64_CPUID_FIELDS(ID_AA64MMFR2_EL1, EVT, IMP) }, + { + .desc = "Guarded Control Stack (GCS)", + .capability = ARM64_HAS_GCS, + .type = ARM64_CPUCAP_SYSTEM_FEATURE, + .cpu_enable = cpu_enable_gcs, + .matches = has_cpuid_feature, + ARM64_CPUID_FIELDS(ID_AA64PFR1_EL1, GCS, IMP) + }, {}, }; diff --git a/arch/arm64/tools/cpucaps b/arch/arm64/tools/cpucaps index b98c38288a9d..30d27f6301fd 100644 --- a/arch/arm64/tools/cpucaps +++ b/arch/arm64/tools/cpucaps @@ -28,6 +28,7 @@ HAS_EPAN HAS_EVT HAS_FGT HAS_FPSIMD +HAS_GCS HAS_GENERIC_AUTH HAS_GENERIC_AUTH_ARCH_QARMA3 HAS_GENERIC_AUTH_ARCH_QARMA5 From patchwork Wed Nov 22 09:42:21 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Brown X-Patchwork-Id: 13464405 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 5A4F5C61D9C for ; Wed, 22 Nov 2023 09:44:19 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id EA1D86B05A0; Wed, 22 Nov 2023 04:44:18 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id E524E6B05A1; Wed, 22 Nov 2023 04:44:18 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CF22E6B05A2; Wed, 22 Nov 2023 04:44:18 -0500 (EST) 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 BBAFD6B05A0 for ; Wed, 22 Nov 2023 04:44:18 -0500 (EST) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 925F6A04B0 for ; Wed, 22 Nov 2023 09:44:18 +0000 (UTC) X-FDA: 81485104596.30.86044DB Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf12.hostedemail.com (Postfix) with ESMTP id AF9F94000B for ; Wed, 22 Nov 2023 09:44:16 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=h210R7jL; spf=pass (imf12.hostedemail.com: domain of broonie@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=broonie@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1700646256; 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=NSorrFaz1NAT3W8+gjPP65V3WERq3K/Abd+JMiBMrFQ=; b=GL/IXoab6GmmgMfkRg/1c7vFznGdi5HwDo9XaJiW3QuAVowVmMiT3U5hvriuqUPgKueEPx wLiqmsj3GYFjEHfmKY7rudfw0aFWeO5mv8j6PaKfiE3SyzReqv7y38HEX10bOh1HzsUj5f sJD32tSUEcAdHN0QPFP8nOpWhSRlbrw= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1700646256; a=rsa-sha256; cv=none; b=m6y/UVVWtjd93z4p3whXFEEqx9mjL5wwvARlgLP5ltDq4IpyVCgAvWTDtsv+YtZoflW3nU pC6GbjWrE1g/0q30QQu3E2chx9A3QfqAnIPh/Z+BRJ4EL57tJrNERqqVbd29Bm+ZxSLTs9 Zsg6Pt57/Js21PVsqSnW+QzQiw8X0h4= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=h210R7jL; spf=pass (imf12.hostedemail.com: domain of broonie@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=broonie@kernel.org; dmarc=pass (policy=none) header.from=kernel.org Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id D3BF261B74; Wed, 22 Nov 2023 09:44:15 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9F7A2C433CA; Wed, 22 Nov 2023 09:44:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1700646255; bh=gCkTJXN9hgWCfubwLDLAmQpOXrRFEWNkqe7LcsfqPUI=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=h210R7jLnqkzHFNW2i+bNC/uW8728czTflygATeqPHl26mTG4iNTO/3gkMXGYPXFf PH8vpoZOA7k8ZL9aFvlCjOOCziT6vy26FbZL1HE665xm4hvAUJvhE2zp6t9hzCfeqo t3IJNIHcA7tQBbVdWXAS2/RmKl+4V3XS5f8pVjvm5p17xaH9rKDG8KJPCnyHe4rKv0 HxFWNWYXflOIzbRkGZ3oNzHRXJweodM+ntU8HkYYfwHN9YnMhlU/AiZ+ctbxSLTi3K 3WwpCNuPtS29zMJyc0EWpf9s7RymWtpI9ltp4+horOAkwOTgBysp6m7F9DquRI7H+/ 0znHAiLzjXVdg== From: Mark Brown Date: Wed, 22 Nov 2023 09:42:21 +0000 Subject: [PATCH v7 11/39] arm64/mm: Allocate PIE slots for EL0 guarded control stack MIME-Version: 1.0 Message-Id: <20231122-arm64-gcs-v7-11-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=2936; i=broonie@kernel.org; h=from:subject:message-id; bh=gCkTJXN9hgWCfubwLDLAmQpOXrRFEWNkqe7LcsfqPUI=; b=owGbwMvMwMWocq27KDak/QLjabUkhtTYM/8lHrpkfRDKsNrcv+HM4ftX0yZd2fNToL57XXlv5 UOvmIi3nYzGLAyMXAyyYoosa59lrEoPl9g6/9H8VzCDWJlApjBwcQrARDYpsP937Bf6G8WaH8wS YTJz+hT7PkttgeLI1xVXNCfMl/p82eTBruI2duXO/qP+f9YFX1rxK9B2h3NLW5GFu63oJoVFOc7 nV3j7lmhOOmrZMmGVgMOGJ4cce5+8qrfkvBvQJLSi70Sdr71Os2Jxf7PPrIgIzU8e9Wx3HhrOE3 ysuznx3ozqD5f5a+2631l8OJr1ce1l7+Soja23YtudX1UYLvRvdFzOZWPi92LpBmcVmZ5psxr/6 r3u15w538Em9ti7wptlYtEt+X7dAtEnsv4v5Hn4b9vTpHNKzlr3+ULlnBjm/GI4y7naUo9PZJX0 9sqNSfN2StnU8k4oPOfcdSVw4iMT1+awP0b8OSEmRlssAQ== X-Developer-Key: i=broonie@kernel.org; a=openpgp; fpr=3F2568AAC26998F9E813A1C5C3F436CA30F5D8EB X-Stat-Signature: 5mnjp94s6cyghhnfosp8oqyzhn7briaj X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: AF9F94000B X-Rspam-User: X-HE-Tag: 1700646256-340321 X-HE-Meta: U2FsdGVkX19QCq4QhjGKqE9DQB04bmngwQfqGgGIx6NP5PDxNWWSOst5/E0hVOUFlBUyb7GfcAmUHtN0GvjmMwb+o5+2r+/s3zEnU3vWyQeXlqHUyUhmc77dAVtgwcOYd7QLEgwMMbToiI4oky7UuXV616blHj+X1sFFMMaKTpFV70+PPr++cLb5oC8rwMAlzfCiag+GAF99VCWFHuHNh9mj7O3e1XX/xC8mnCZzODBEdKWDPUwW5r50j0JDFp5WRT/uOpI/RWbWloQoRe6fn8kUzrWTrHV5sG7Muqhmu/ILc4sKJxFPxSAKORndBpf1lLBDgAX7kL9XfR6AQk7kt/Ae/sbgDDAOEnsdi8f0VcTQWffjFWJ76YYSpXmI+6wJY2KJsrGcimnM/hLKwCE4wMAx0s6hOH3dJKgdx7NRSBsKymy1j/GRrhiSndxLfQYecLwU/d1WvfIeLJSXxlBTesvBy/dKNJAxmBAq2GlWKsF81qm3OjHjkQvUTm205y2ZBmdmK7UJr3cc2Rk8lGYlt0ddX+nfyP/HrXqhOewOTsegLSPlZq5wPN805iFtLohwUXeRcUh+p920IunRgekYlhyPpb3DVMpekcWE68E89iCQGk231IAhzh4Z5mzeukuwS2KEwjBaxXf26A7c44VxW5JCBKHstYXYIT2d5nJRNekAv8qqhBanxkkb+9630oAvG7EmAUyhYmGRaJ68fDgJtJfukDLcQjhK8se+4DOkGkOl3KbdjVi9OUN6l3eXGmMBtu74nFiAtr1W0jVgU6UXloEMsMz76MUREE+2OoutoXnbWOXcqNi+ITnBl8nKdhSzIRYo8rMvLu98pjJwAJ6PSZLQa0ccgwIBKr9+8ownCHeszEqKJbwPlqY4t4ObCSzUenXYK3/q2tZZzlAyhWv97iTmNC78m/Pt3LYpD3VYXZypZ13M/fPkAdmzzkB8WuSiY/BXpjh9Sc35VBFz8Lj ywYzeDtD suLhJ3Xs4VQ8T6+1ZMAKA/iS4jRZOhQ+fH7HsTWGqTHXjmHEBGZieUx0954wDB6ivbndL8Hyws82wfrWx1L1ITa1oAnnIrTtcSAX+ZKKJIbfiyd9INVfuEutKskxfpenilF0p+wfpgeRRgBPg56++eBirxlh3xGMUuWVWXlPO1s7wV6VnCnuz5kmrCFcRsssrXMlBOraRHJSZHMUbvyAuzw2vkVrSJc91BnMEIa2jBHujGR1tHd8MCVGDLg/9yrnwpqIa7gjUn8ShiIB4ypaPeg4zMQ+9Whc6QSDJNc/8jUUMhydpDDPzvw5jTJ0ZXO0Jphkc 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: Pages used for guarded control stacks need to be described to the hardware using the Permission Indirection Extension, GCS is not supported without PIE. In order to support copy on write for guarded stacks we allocate two values, one for active GCSs and one for GCS pages marked as read only prior to copy. Since the actual effect is defined using PIE the specific bit pattern used does not matter to the hardware but we choose two values which differ only in PTE_WRITE in order to help share code with non-PIE cases. Signed-off-by: Mark Brown --- arch/arm64/include/asm/pgtable-prot.h | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/arch/arm64/include/asm/pgtable-prot.h b/arch/arm64/include/asm/pgtable-prot.h index e9624f6326dd..297a645e8a0f 100644 --- a/arch/arm64/include/asm/pgtable-prot.h +++ b/arch/arm64/include/asm/pgtable-prot.h @@ -127,15 +127,23 @@ extern bool arm64_use_ng_mappings; /* 6: PTE_PXN | PTE_WRITE */ /* 7: PAGE_SHARED_EXEC PTE_PXN | PTE_WRITE | PTE_USER */ /* 8: PAGE_KERNEL_ROX PTE_UXN */ -/* 9: PTE_UXN | PTE_USER */ +/* 9: PAGE_GCS_RO PTE_UXN | PTE_USER */ /* a: PAGE_KERNEL_EXEC PTE_UXN | PTE_WRITE */ -/* b: PTE_UXN | PTE_WRITE | PTE_USER */ +/* b: PAGE_GCS PTE_UXN | PTE_WRITE | PTE_USER */ /* c: PAGE_KERNEL_RO PTE_UXN | PTE_PXN */ /* d: PAGE_READONLY PTE_UXN | PTE_PXN | PTE_USER */ /* e: PAGE_KERNEL PTE_UXN | PTE_PXN | PTE_WRITE */ /* f: PAGE_SHARED PTE_UXN | PTE_PXN | PTE_WRITE | PTE_USER */ +#define _PAGE_GCS (_PAGE_DEFAULT | PTE_NG | PTE_UXN | PTE_WRITE | PTE_USER) +#define _PAGE_GCS_RO (_PAGE_DEFAULT | PTE_NG | PTE_UXN | PTE_USER) + +#define PAGE_GCS __pgprot(_PAGE_GCS) +#define PAGE_GCS_RO __pgprot(_PAGE_GCS_RO) + #define PIE_E0 ( \ + PIRx_ELx_PERM(pte_pi_index(_PAGE_GCS), PIE_GCS) | \ + PIRx_ELx_PERM(pte_pi_index(_PAGE_GCS_RO), PIE_R) | \ PIRx_ELx_PERM(pte_pi_index(_PAGE_EXECONLY), PIE_X_O) | \ PIRx_ELx_PERM(pte_pi_index(_PAGE_READONLY_EXEC), PIE_RX) | \ PIRx_ELx_PERM(pte_pi_index(_PAGE_SHARED_EXEC), PIE_RWX) | \ @@ -143,6 +151,8 @@ extern bool arm64_use_ng_mappings; PIRx_ELx_PERM(pte_pi_index(_PAGE_SHARED), PIE_RW)) #define PIE_E1 ( \ + PIRx_ELx_PERM(pte_pi_index(_PAGE_GCS), PIE_NONE_O) | \ + PIRx_ELx_PERM(pte_pi_index(_PAGE_GCS_RO), PIE_NONE_O) | \ PIRx_ELx_PERM(pte_pi_index(_PAGE_EXECONLY), PIE_NONE_O) | \ PIRx_ELx_PERM(pte_pi_index(_PAGE_READONLY_EXEC), PIE_R) | \ PIRx_ELx_PERM(pte_pi_index(_PAGE_SHARED_EXEC), PIE_RW) | \ From patchwork Wed Nov 22 09:42:22 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Brown X-Patchwork-Id: 13464406 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 6A740C61D9C for ; Wed, 22 Nov 2023 09:44:26 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 049706B05A2; Wed, 22 Nov 2023 04:44:26 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id F3B876B05A3; Wed, 22 Nov 2023 04:44:25 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DDC736B05A4; Wed, 22 Nov 2023 04:44:25 -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 CAC056B05A2 for ; Wed, 22 Nov 2023 04:44:25 -0500 (EST) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id A52EBA03B8 for ; Wed, 22 Nov 2023 09:44:25 +0000 (UTC) X-FDA: 81485104890.11.B710344 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf19.hostedemail.com (Postfix) with ESMTP id D40841A0018 for ; Wed, 22 Nov 2023 09:44:23 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=DhF0C8ob; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf19.hostedemail.com: domain of broonie@kernel.org designates 139.178.84.217 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=1700646263; 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=ibX+h2ew3ywDs3y5/V0WozUSvEovuEkFpmJ1Sw1wP7g=; b=akaPi+hFo23vl2CrXcN1c7R1cmhJS3dc/LOzIGiMOCmhVg2FC2a9VOyA9/afUbdRMyalWo ZUjSZ3S15CnIpqHK5492dhFTem7lacSxkPuvFZthi76YhH0o016vIwDOPXI/q6MpIp8Pfn I2IZT+Miwgy/bMseq2otnYvJI5dkwio= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=DhF0C8ob; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf19.hostedemail.com: domain of broonie@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=broonie@kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1700646263; a=rsa-sha256; cv=none; b=cBCke74IVSLpnsRSTmnn3zUGJ1e4bHQlopWCW2S04poleJj7aAoUTwPCuoECd5MlSL7yqM X37AZX/gRQN/NzB7NjWnSFIhrWzqlqeKDZyjdV5yV6Rr+LOxMTMIrLYaNzXFpgk1xbESUo gtMpWC9SKd737kBMOJnt8EvX+8xpIXg= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 0CC0161CCC; Wed, 22 Nov 2023 09:44:23 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 041B7C43397; Wed, 22 Nov 2023 09:44:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1700646262; bh=Df+HY0dV5R5n1ih4MqR1JvyrE5qdgwZR/dZq7PG88vE=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=DhF0C8obSjt9Nfr4iuhMF+MfrBzvOMHkq9LmVgyYKZnO9fIVLqs7HPAS+n3JPEKJ0 dLQEv4IqHz985M/9vF5RA3TEblITjIIzbNMwRKx3W4AeIrzRpwLIpvKWycDupEWd4k HDnsgVbJ2E80hUb0HTiGD+TfDklzJH8Ma+jZ6ZDSObz510LSGSdIjpW/3G98VBnzUv DxrYCGKBYiovBFq7e5dPgwWVI3DxTMzU2LE/Db2CvFUUPnnfx59dYtoZlztgKFgZdC Xnv3q15QP1P/yyGxaz73jzcQ0IOLjraOoSEdX2zUMo2fgiDmta5EVVRM0eoUCmP1lx wbCpNkVqokZ9Q== From: Mark Brown Date: Wed, 22 Nov 2023 09:42:22 +0000 Subject: [PATCH v7 12/39] mm: Define VM_SHADOW_STACK for arm64 when we support GCS MIME-Version: 1.0 Message-Id: <20231122-arm64-gcs-v7-12-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=2038; i=broonie@kernel.org; h=from:subject:message-id; bh=Df+HY0dV5R5n1ih4MqR1JvyrE5qdgwZR/dZq7PG88vE=; b=owEBbQGS/pANAwAKASTWi3JdVIfQAcsmYgBlXcz/qzN33RRp1sMMsdDeIWGemytfZkpPWLo4a cwQK9wJLbWJATMEAAEKAB0WIQSt5miqZ1cYtZ/in+ok1otyXVSH0AUCZV3M/wAKCRAk1otyXVSH 0CJlB/42DNRC926QZda7ZgVHwYfL0gTMCpMI6gcouqu0xgA1HTtwNrwD613VnSOoyxtfTGVzCMY tsVAy8UOqNqRN6Dz4Zh0A+WmkLl1I5GdvGmF54We0wD2eSV5cLIVOIbdhyzcHObAzfkNwcd8LZV xX6x0wVfcf5dhIKnwCSx7NL3qmoP/2RuVklEW90UpGYkcYj/L3FIb/kydQwjinq8/NYa8ybu2FI iPgL35MV+Ppu4QMvQxgI4+09gl+0XIjyeFDoigRWiF/oIFAz8r4t9jdHMwTU+YpNFLdNthAWQ/S faocMmU8q7Y6bz+9r3UGqczI7+EUjPo4aNc0Gwlh0vqeaAX/ X-Developer-Key: i=broonie@kernel.org; a=openpgp; fpr=3F2568AAC26998F9E813A1C5C3F436CA30F5D8EB X-Rspamd-Queue-Id: D40841A0018 X-Rspam-User: X-Rspamd-Server: rspam02 X-Stat-Signature: o8qh16hy19twtofwm6g4nm6copycf6xh X-HE-Tag: 1700646263-179690 X-HE-Meta: U2FsdGVkX1/yfBmJpgQHOUm35DfugdmJ19FeJ8BviT2aqNXa/ZL+zWV0sgULytTzBul4M5j/O6HOLGBwO2Ixnp8akVFPWERqTsVEz8DQmjlr+eiaYWoHqMSLD5RxnzeY9IPCT8V1Kf9qFbQLkE7OyDHInFStSLcMUIn0eZeYjP4VTOPuucJL4FJil/mlsuRa0qmUcKapMc3vio/ZPmgQGjO/wGcTuBxYOM4vr9gKEQIQDrlw6ObKGSklNgoX9D0AWvuNppDNm+bgiy1Ap/z8DbakxcrNmA2aGrx18q0M658n3jws3x6TeuZaEqUUhmEIB6PJ3k5v+50kezAzDTwa70iZtRmfTDRod78LXvJgEmK3ts9IwGp91wp6rXHx5OexYUAqoG+Wu18+rod/yF5RoOMpTEdZ8phhVXJ9AYH4mA5bWgjl+pYPBKUG192PQtZMDtNeERUPTYrs6M88/6WtI+W6GwGsO/AFyoRwDFMQMu/K25BRcNuZD5DtL2NDvGF6GE9Bgg1Qas8GWZnm5wYpfdtzQHgLojouA9thGvYnHoYbjYL+gx3YrHQuugD4RjEVdolE5c+eKiQanV02WbkzVkiT6yKWfyF1LjS2LiJD+BfyHRWtQqZvlKtfkFJ5lBbpq1K+gf+jaVIhNaGuO/9p4q+coh3sjh+HypyNBTgh5HHajEUbib26DxvjDnrI/vZSoZ3oYyOaUYEbke8R4tNYOzsWI8LpEG0tMz+zEWSjFc6I2z5OIo9hlT/2N4nMP7MFdtMoVQUCtXvSjlifqIoUIatsbcg/Q1WEC4IyahT6wVaYVNvrEofMA7GNsQAf+7A7bLfsPVZgcfxt+IsO/cy60IsoEJYXLJ3mTAblk8VOUv0ZThdpnJ+KnhkciTVZLqpNBMDv+OCQtJseRmbzZlDELcxN3r5EBJd7QEkIcXGT5OQbwIMpPrwOFL5+HSoWPKi6hA0l4ZNGv+sPR0KKyaS HdkGlDS4 oGjEfThMlaJIplXUmx2H8MF8Oa3EQxqlKvEK9p78ximKzjgqgV9CWTRHiEkB92QZDXtxj+A95bAu4wAq4LjDrrxGEfXB607jne1q5yoj+dJrUgevd+cNXJ/uQMgJvuPRf1FZ211qcN7MmhyEGu8/XxnnuvO4MzpBR9z/dlN8pqqbSA3E+wJ9rEO3ljzAHnqI0Jiotqpj67+nVnb31ror8nb4fKT/im81UfJEDqG4xwQaXNT5Z9VgCsRqTAVVZlTFKtPk7bA53jytWvDKlcjGYns+0oNgswo3yPSd6t8Pa5Fe+ZmDqkxvBDio01a4Em2PMrSdnVEGkLjN0yZwlHE/TwUY5L/lbGwgIT4poNVmYqC0rSWsIvXjUvUyZqQ== 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: Use VM_HIGH_ARCH_5 for guarded control stack pages. Signed-off-by: Mark Brown --- Documentation/filesystems/proc.rst | 2 +- fs/proc/task_mmu.c | 3 +++ include/linux/mm.h | 12 +++++++++++- 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/Documentation/filesystems/proc.rst b/Documentation/filesystems/proc.rst index 49ef12df631b..1dfc5cb20f28 100644 --- a/Documentation/filesystems/proc.rst +++ b/Documentation/filesystems/proc.rst @@ -570,7 +570,7 @@ encoded manner. The codes are the following: mt arm64 MTE allocation tags are enabled um userfaultfd missing tracking uw userfaultfd wr-protect tracking - ss shadow stack page + ss shadow/guarded control stack page == ======================================= Note that there is no guarantee that every flag and associated mnemonic will diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c index f0a904aeee8e..29fd7d6fbeb0 100644 --- a/fs/proc/task_mmu.c +++ b/fs/proc/task_mmu.c @@ -701,6 +701,9 @@ static void show_smap_vma_flags(struct seq_file *m, struct vm_area_struct *vma) #endif /* CONFIG_HAVE_ARCH_USERFAULTFD_MINOR */ #ifdef CONFIG_ARCH_HAS_USER_SHADOW_STACK [ilog2(VM_SHADOW_STACK)] = "ss", +#endif +#ifdef CONFIG_ARM64_GCS + [ilog2(VM_SHADOW_STACK)] = "ss", #endif }; size_t i; diff --git a/include/linux/mm.h b/include/linux/mm.h index 8b28483b4afa..3c96fc6c1128 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -352,7 +352,17 @@ extern unsigned int kobjsize(const void *objp); * for more details on the guard size. */ # define VM_SHADOW_STACK VM_HIGH_ARCH_5 -#else +#endif + +#if defined(CONFIG_ARM64_GCS) +/* + * arm64's Guarded Control Stack implements similar functionality and + * has similar constraints to shadow stacks. + */ +# define VM_SHADOW_STACK VM_HIGH_ARCH_5 +#endif + +#ifndef VM_SHADOW_STACK # define VM_SHADOW_STACK VM_NONE #endif From patchwork Wed Nov 22 09:42:23 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Brown X-Patchwork-Id: 13464407 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 F144DC61D9B for ; Wed, 22 Nov 2023 09:44:36 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8FB596B05A4; Wed, 22 Nov 2023 04:44:36 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 8ABC06B05A5; Wed, 22 Nov 2023 04:44:36 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 79AD96B05A6; Wed, 22 Nov 2023 04:44:36 -0500 (EST) 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 6A0B66B05A4 for ; Wed, 22 Nov 2023 04:44:36 -0500 (EST) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 36958A0228 for ; Wed, 22 Nov 2023 09:44:36 +0000 (UTC) X-FDA: 81485105352.27.F981C16 Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by imf21.hostedemail.com (Postfix) with ESMTP id CF7B31C0007 for ; Wed, 22 Nov 2023 09:44:33 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=rTp3IgyI; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf21.hostedemail.com: domain of broonie@kernel.org designates 145.40.73.55 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=1700646274; 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=Arbjk0aSXvp932aR/l2/MuAVze6JtdDBpxWCzpTeg2M=; b=azviwIrRkKlZJ49RhV9TmP75w889xge6uia2GqJNA51dbqwxp1IoE3Zy43Q4g/3d7ryNGK DOKk9dxOOhKxT98PW95t67r4xk6QUJ6hbNk1PipaZ1NPg8+oXOjZkT6eZp0E6Z292bgPtM FWidzuUeDKx5mQ0yJGgYvYMDEG+nYtA= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=rTp3IgyI; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf21.hostedemail.com: domain of broonie@kernel.org designates 145.40.73.55 as permitted sender) smtp.mailfrom=broonie@kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1700646274; a=rsa-sha256; cv=none; b=jJtOnOBrQsbjit87JdEcAmoIqoXVdzP5wR/42xpI8Jr8tvzCGnC5oPfJ/T6Chg2XgX1wNw IQrzbS9GaYGJh8SkgHL+Iu+XGvgjA4lkveJLng6ydRLetiY3jiMUXt+AowM171at/hHhH0 0jBbEVQal83VmKuKaLathB0M7FneHtk= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sin.source.kernel.org (Postfix) with ESMTP id F0A42CE1ED6; Wed, 22 Nov 2023 09:44:30 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 50C67C433C7; Wed, 22 Nov 2023 09:44:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1700646270; bh=qEFrCNf3UEgHGW1T9wtS9jRJz/KS9KwPB3lxI2GEsTI=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=rTp3IgyIvQfGD4I+L4TFOAbCJv9NbsiNeIDD4RtpVFyB4VfP3fsoai+tC4z4rivNB 8csRCTYL8ZxeIFyOEnSQA4auqXLMjPB0TV4q9PD4M8mgIYeKJidyttb6RB1TqgN3n/ 7KFCMpd6Y97LNcgEUgDa3TuB2NG38oyO3WYbYyT4ZlzJStWJg4OpYtI1zx1GNbFLcQ EqvGVx0LQC1NgAEZKJhwgqD7BpUehJeQAuBogQ81mPWkWennLwbFNmzi6M9MU9ePt7 EfPhNaYflYB6bWpv0JVKbnZGesMDPr2M3n8dHIwjEE+6mu+EB7/vi4iVvbD9/uCSfp qfBU5xs5TJplw== From: Mark Brown Date: Wed, 22 Nov 2023 09:42:23 +0000 Subject: [PATCH v7 13/39] arm64/mm: Map pages for guarded control stack MIME-Version: 1.0 Message-Id: <20231122-arm64-gcs-v7-13-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=1837; i=broonie@kernel.org; h=from:subject:message-id; bh=qEFrCNf3UEgHGW1T9wtS9jRJz/KS9KwPB3lxI2GEsTI=; b=owEBbQGS/pANAwAKASTWi3JdVIfQAcsmYgBlXc0A24D56zutL8bLQvd52fo0wC43SRULlWAh1 +rU/4PE4q6JATMEAAEKAB0WIQSt5miqZ1cYtZ/in+ok1otyXVSH0AUCZV3NAAAKCRAk1otyXVSH 0HxeB/wMjuD6MVRsjymmm5q0PFo3mPpIDfrsn5bfPM93xyIkhexzp3o8MKMLZJR6AyWc5SU9ANA 9VNb9/Gq2SqulwM+T75NERQoIaX5QN0rnERoUsbZEovn2OVcl1rKgSR9V3pSg6Qe7bTHQI1RXba 52pV6lnw7K6QWG67Rs68SOCjwuPipie41cnu7hHYrKAVPUJgJ3xAaTF3zvDCHGppy2hPtemSDue xvPJt2OEUG/I7wwXpBQXAIdu64425sWrMATEdrAKaBbTDiRIX3QHYitwt4URVvOPC092qkbtgUq ocdLzNoIZ+MRZVauVM6J+z8pX5L1QoZViXQ6EXKLKo+5igsQ X-Developer-Key: i=broonie@kernel.org; a=openpgp; fpr=3F2568AAC26998F9E813A1C5C3F436CA30F5D8EB X-Rspam-User: X-Stat-Signature: 6gxf8riecusznit161mun663zo48zn87 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: CF7B31C0007 X-HE-Tag: 1700646273-196334 X-HE-Meta: U2FsdGVkX1/WZDdqBmJTc7XYy1ye9iDjrBjQytWlFYW0pddZSzHTLjs0kAsMu+QNrcMCotG7ZZpw0jWRyuJMRpzWZp0wSCvPt4IAu1Bi7arhzND+vfXUjPdusBLi1S1OvsaFR5rmJuqlDjBfbe7ShYDEAOfmJ+ku2FOrOVcNWJO/83UH8qvlEX/HMqT1kAVm19Ndyv5N+Xm1jutxLHA3/3MArLopQI+S0zG6TrqkuZ9qvxuuMGGGvWIplcGW1MvhT/Pt33gwnXuMi0gfgMcO2pY0pgY4fbQaC8AztGbhd//yQey6UJp+sTCfk0wpmfoiwWA72BZ6c7+e6nwzHqv0BRXa2JfcelX4Ja/3ppjt1qvkfPRQyENwAQeXp+Vnh+RZqBBRwxykl0LtB8ZbASdpd95wXpg1/D1c5YHZSsu1LUS4qptcDEt52+QaTc4WsCu43SRJAJPFm5ZcT+/QhhXX8cfCg8e4wqDLLOxthmYat0q6k1OcXyVrPvwcLnMjEmci63/o7bVfZ2+3+b/2g8IZulkQeVJFtajvAYzkSgcTUb5HivU6PuWLqk1LxaIf74ieYY3iIDZsJUAFjLQhuIWVI0nr974mR1gQCQK3z5eo0MjFsS2LbxGS3xy90GPTLLNJtv65aoj9AH5wDesShou1xKhhEA39ttGWulKlWLSw6I63IFFComSpp+hIsFaRLw/EvU/Sn071R8lr3aSHdh4y9/94SdJu9rremFvycvcIO3gdkHrnlZ9UsU94nzmAYUWqusAR5RjYcj2U/fEZnX8bVxr7O/qeMaet6bWLFEQcc1w86VNGg24fvLZtxM8j8hnI8KdSbnzIq9LPjqLGH2j+RtvXSKEOkRGjTZnuG8a3i3DZXfKO6jX8wmlvO7RPzi7KHmnn64yWg0Nmc5g/dzwyC4d3nD+qNnPf1cQoGtnHBmyaabhFw5qSS2wpEIu+el95qHLqAg8mZcPwJcNOAdR Lp64NMoe qCxw4SxIfg3wNRxnGI4wj4CqxOCEQndHH6t4NtO1SukwXmnriSkw4+h6O2NrMWVIej/nthRInKsid0s3GVWJWOTzYV+/C+ZYrZ3XZiEhKIWQBWhSHQpIVy8jUTFncwIrsRnpKUliaNto12xtPwmn6/06GHqi3XkY4iziQF5imO+ZI3C/f+5YA4I69ZzJtcYVpbJ3m/R6+Frje5MfZ/UDH9TMh7d+vCmJqx/qhyI0EU3RPP5Kt2pZt7VngqG4sdtdYvPiQApBAYLQlwnu55dipo/DqcWc7E5/SFLOk8IljoKmdFZyN9srRKzgBi8XOVvZYPnL7 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: Map pages flagged as being part of a GCS as such rather than using the full set of generic VM flags. This is done using a conditional rather than extending the size of protection_map since that would make for a very sparse array. Signed-off-by: Mark Brown --- arch/arm64/include/asm/mman.h | 9 +++++++++ arch/arm64/mm/mmap.c | 13 ++++++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/arch/arm64/include/asm/mman.h b/arch/arm64/include/asm/mman.h index c21849ffdd88..6d3fe6433a62 100644 --- a/arch/arm64/include/asm/mman.h +++ b/arch/arm64/include/asm/mman.h @@ -61,6 +61,15 @@ static inline bool arch_validate_flags(unsigned long vm_flags) return false; } + if (system_supports_gcs() && (vm_flags & VM_SHADOW_STACK)) { + /* + * An executable GCS isn't a good idea, and the mm + * core can't cope with a shared GCS. + */ + if (vm_flags & (VM_EXEC | VM_ARM64_BTI | VM_SHARED)) + return false; + } + return true; } diff --git a/arch/arm64/mm/mmap.c b/arch/arm64/mm/mmap.c index 645fe60d000f..605d4e6edc1d 100644 --- a/arch/arm64/mm/mmap.c +++ b/arch/arm64/mm/mmap.c @@ -79,9 +79,20 @@ arch_initcall(adjust_protection_map); pgprot_t vm_get_page_prot(unsigned long vm_flags) { - pteval_t prot = pgprot_val(protection_map[vm_flags & + pteval_t prot; + + /* If this is a GCS then only interpret VM_WRITE. */ + if (system_supports_gcs() && (vm_flags & VM_SHADOW_STACK)) { + if (vm_flags & VM_WRITE) + prot = _PAGE_GCS; + else + prot = _PAGE_GCS_RO; + } else { + prot = pgprot_val(protection_map[vm_flags & (VM_READ|VM_WRITE|VM_EXEC|VM_SHARED)]); + } + /* VM_ARM64_BTI on a GCS is rejected in arch_valdiate_flags() */ if (vm_flags & VM_ARM64_BTI) prot |= PTE_GP; From patchwork Wed Nov 22 09:42:24 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Brown X-Patchwork-Id: 13464408 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 3A3BBC61D9B for ; Wed, 22 Nov 2023 09:44:44 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id CA68A6B05A6; Wed, 22 Nov 2023 04:44:43 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id C55166B05A7; Wed, 22 Nov 2023 04:44:43 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id AF6776B05A8; Wed, 22 Nov 2023 04:44:43 -0500 (EST) 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 9AB946B05A6 for ; Wed, 22 Nov 2023 04:44:43 -0500 (EST) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 73DFBC05DF for ; Wed, 22 Nov 2023 09:44:43 +0000 (UTC) X-FDA: 81485105646.09.FFB2201 Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by imf26.hostedemail.com (Postfix) with ESMTP id 164A5140009 for ; Wed, 22 Nov 2023 09:44:40 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=lBBiPkfs; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf26.hostedemail.com: domain of broonie@kernel.org designates 145.40.73.55 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=1700646281; 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=n3malbSPeu/Q5hS55bgQ6p7fHqRVphzv9TdTOkJrikw=; b=eRy90z/b5KHfvvqRUxJruVt/sgFD61qxKr5tACc/SjtEapL3trR+9QmlDXf5brY904gap/ fmpICeWIAb9GtjenMt22ywqU2BK04z8ueN9bZm91m6k8DRohUvc+xvqZSdnt8YOgJkInUP ZVNKsK+6ozGwgCMum+shNHnonH/8qsM= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=lBBiPkfs; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf26.hostedemail.com: domain of broonie@kernel.org designates 145.40.73.55 as permitted sender) smtp.mailfrom=broonie@kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1700646281; a=rsa-sha256; cv=none; b=M9wVXIN+uGCBezswUNQdbHju8+/mMjBnOzUXlHD+iO4wY4OdwSTJBdQpqH5Lk0IRFtjipx K7y1WGNNGYyfDwYMX2p5Ci4fSD8CRWt2KVQ4XGJkfNHRoQzG+AqhMGh1shjeZ2juFKF41v QZALDxMRRLdN3lWZ1OZo7C8dtFZ+ASw= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sin.source.kernel.org (Postfix) with ESMTP id 9B187CE1EC3; Wed, 22 Nov 2023 09:44:38 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id AB45FC43142; Wed, 22 Nov 2023 09:44:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1700646277; bh=701JTF9Jvt7MHb4vzhbZhtT7BBJBuznNuW3VQeP4Dqk=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=lBBiPkfs1bDvDwjHk4OkCK/FLfl/DjbD4UXXRmnc6yFzF5zap9dV3IJ72+bcXHLdR tUb7tOWPLx3jyiV+kFx1gYyjX76ecQ9jUUhXAFPxP3MT9oDHjvRaYtVWbg7PEzj2xv 35tvDg3WO/auArFeFCucrYnv98XyP7Q9zZVWus1BTU9i5jNDTO6JUUSp7efT6qfpp4 kNbXvyIW8LgU9/jn09Puy6NA0jHcD+6ZD9V3T6NpokLSw8zrlewKqnApU8DlaGUI11 e86Pgu50Yp02rHLvXaKF0MTkm15a2boO2lGqfFFeQz6uJtO+40YZewfERRgzR+47r4 AeAJJBAkAlg+A== From: Mark Brown Date: Wed, 22 Nov 2023 09:42:24 +0000 Subject: [PATCH v7 14/39] KVM: arm64: Manage GCS registers for guests MIME-Version: 1.0 Message-Id: <20231122-arm64-gcs-v7-14-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=5364; i=broonie@kernel.org; h=from:subject:message-id; bh=701JTF9Jvt7MHb4vzhbZhtT7BBJBuznNuW3VQeP4Dqk=; b=owEBbQGS/pANAwAKASTWi3JdVIfQAcsmYgBlXc0BfEgkkRq/SykifBOr7DH7c9OzUQTfc3qa1 neFhSoEgA+JATMEAAEKAB0WIQSt5miqZ1cYtZ/in+ok1otyXVSH0AUCZV3NAQAKCRAk1otyXVSH 0MFTB/9Zui5cCedvHBD+Vs/u3hLqGSWuJf2lHM51ej/6t6eS1XaHSrFKXdLgTCQYE9smlvcGNQo r4+cJ9fLfz1oQl0jGvRlS6Bnx2jTNrWGGdnFhMgZDfO5xrvSpRjjmXpk3+SRnO4OjVVSGfdAnRS yHzmj/yQQSFe0bfTWBWitkGKeJJcQe/vtDdswlJP2slDDHoCFndFuD21dGsTA+oBvEwSrtNAK0b chDLJDM+svT3/5T0sbV2cWltxUQ5VLZFcpuat5k1Z3WG1YZbg5zH+e+q2CPy36NMutNgtGWpmOV 9/jMEPsWOhkDS2qH1SdZl7RFJHJybmOLGPChu233YdOu3sod X-Developer-Key: i=broonie@kernel.org; a=openpgp; fpr=3F2568AAC26998F9E813A1C5C3F436CA30F5D8EB X-Rspamd-Queue-Id: 164A5140009 X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: w4cc6mt9xgzj9noy4it387guuupmgj7f X-HE-Tag: 1700646280-199261 X-HE-Meta: U2FsdGVkX19VOy0RMiKGHPSSe+F0p1uc2znVEf0AwK9WAiXwh8zUzxFQXaXGRqvVsh3++JuDcaJvJOgylA1OKk/ncPykA8Evy0lkhuYRy1kIFHOyFiypxxvMXrkyyQ2Bz5E9zRKFAsaVw5+WgpcwjIk0xglXo8tevDPxGoCSMlgbWqomPAElc7NdgnPXpaeFLVjR4UNlCasMt2DYOlk7yVfkUganXFwaaHGHo1pPkDdjCc9e2666VdyS3r0wSazAhDpWQGIsb7WQyAZvzYPWtbNSibes06655w2/uyggJ2oXOFTdDAY0G7NLT9INTGDJvLwH56WjNpJkz/3L+87V560kBST8rnTJ3wiw0BSjWrXXzfNZhUFpLbQpT5R99v+5DKUmq46Eci4L59GORBK/+RrPQMc77AA+mUp92NtSvyvlhhqPAGgvi/OVmEoDBoQzjkQ1IS6p6agFLq8UYV8uA8FPdPsCsQXqLVFvCry2jyicrUV+V4VelT5sxyPmKOUAFQ5Y06w5av59Ud0eDmPrfFUCCuISy6GE6GURVqlBqg+Ouq3hOg5DcGAi+N8W4IimwV6zVzjtX44vqegDMzIoSRFS/Gcq/E9hFrNsniY1ZKT+eaL36xaC7U1F9TeMzACNz+XnUqDVgZ3ZrHjKlbMOznqIPYzLCiExwYpAEUK+1p5C68Q7PvxcyF7qbTnyxfn3ywuPyzm5xXD+NTnb2oeYnUvdVsiLyVwKwCBXzd7w2e3GwYJgZFFoBs98OmY+my19bq/FHi9ynp9kRK3v6b6HEQD8ypriu1/ByEujWYWMt5/ehhm6mKqboULawIO0pbe2nWUYVvBbSHTW5uYvL9TUiu8SW0Zwt+T89UIqQwxtS1KGx5dtHZG4VtAJrU/Ia6H9nA7ac6BDNfa7gSn/VAE5z1XzqHPCiImrXVieIZ5xljOwKkQ76Lwe/31tV7CqGSA6xQsYECv2h2NIu6tdSiH Pdy1Mv4K T7M14A46pLZDSzg9n7owFZXcIkBIskFDIFFB94h1mhrNG+tovW7mxSI3zYiUmXJrW6nwC/mnxuFOtUzrjpXTK3C2a/dMWi7ch4sPLdj0IBlypIRRJ+yiz4vPM+ng5/OE3nkYyYCsl7sU0TwDeDg020NTCWvWsKI9lGrcP5oVApsqsXu0VoV8oc+BNTFUvO+yfrCW9G3nZ/MSD+pHk6gai6WHCt36qEDmikQpgq0vB5nMShYneWkmWfrxUfsi+oPg2SuMwaXCQDaWXcVkdX5IS0n60n5420nZxyaxOWOxbXnULwy5r3Sw+csVEZNd4HxcntBg2 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: GCS introduces a number of system registers for EL1 and EL0, on systems with GCS we need to context switch them and expose them to VMMs to allow guests to use GCS. Traps are already disabled. Signed-off-by: Mark Brown --- arch/arm64/include/asm/kvm_host.h | 12 ++++++++++++ arch/arm64/kvm/hyp/include/hyp/sysreg-sr.h | 17 +++++++++++++++++ arch/arm64/kvm/sys_regs.c | 22 ++++++++++++++++++++++ 3 files changed, 51 insertions(+) diff --git a/arch/arm64/include/asm/kvm_host.h b/arch/arm64/include/asm/kvm_host.h index 824f29f04916..2b09805da26a 100644 --- a/arch/arm64/include/asm/kvm_host.h +++ b/arch/arm64/include/asm/kvm_host.h @@ -401,6 +401,12 @@ enum vcpu_sysreg { PIR_EL1, /* Permission Indirection Register 1 (EL1) */ PIRE0_EL1, /* Permission Indirection Register 0 (EL1) */ + /* Guarded Control Stack registers */ + GCSCRE0_EL1, /* Guarded Control Stack Control (EL0) */ + GCSCR_EL1, /* Guarded Control Stack Control (EL1) */ + GCSPR_EL0, /* Guarded Control Stack Pointer (EL0) */ + GCSPR_EL1, /* Guarded Control Stack Pointer (EL1) */ + /* 32bit specific registers. */ DACR32_EL2, /* Domain Access Control Register */ IFSR32_EL2, /* Instruction Fault Status Register */ @@ -1177,6 +1183,12 @@ bool kvm_arm_vcpu_is_finalized(struct kvm_vcpu *vcpu); #define kvm_vm_has_ran_once(kvm) \ (test_bit(KVM_ARCH_FLAG_HAS_RAN_ONCE, &(kvm)->arch.flags)) +static inline bool has_gcs(void) +{ + return IS_ENABLED(CONFIG_ARM64_GCS) && + cpus_have_final_cap(ARM64_HAS_GCS); +} + int kvm_trng_call(struct kvm_vcpu *vcpu); #ifdef CONFIG_KVM extern phys_addr_t hyp_mem_base; diff --git a/arch/arm64/kvm/hyp/include/hyp/sysreg-sr.h b/arch/arm64/kvm/hyp/include/hyp/sysreg-sr.h index bb6b571ec627..ec34d4a90717 100644 --- a/arch/arm64/kvm/hyp/include/hyp/sysreg-sr.h +++ b/arch/arm64/kvm/hyp/include/hyp/sysreg-sr.h @@ -25,6 +25,8 @@ static inline void __sysreg_save_user_state(struct kvm_cpu_context *ctxt) { ctxt_sys_reg(ctxt, TPIDR_EL0) = read_sysreg(tpidr_el0); ctxt_sys_reg(ctxt, TPIDRRO_EL0) = read_sysreg(tpidrro_el0); + if (has_gcs()) + ctxt_sys_reg(ctxt, GCSPR_EL0) = read_sysreg_s(SYS_GCSPR_EL0); } static inline bool ctxt_has_mte(struct kvm_cpu_context *ctxt) @@ -62,6 +64,12 @@ static inline void __sysreg_save_el1_state(struct kvm_cpu_context *ctxt) ctxt_sys_reg(ctxt, PAR_EL1) = read_sysreg_par(); ctxt_sys_reg(ctxt, TPIDR_EL1) = read_sysreg(tpidr_el1); + if (has_gcs()) { + ctxt_sys_reg(ctxt, GCSPR_EL1) = read_sysreg_el1(SYS_GCSPR); + ctxt_sys_reg(ctxt, GCSCR_EL1) = read_sysreg_el1(SYS_GCSCR); + ctxt_sys_reg(ctxt, GCSCRE0_EL1) = read_sysreg_s(SYS_GCSCRE0_EL1); + } + if (ctxt_has_mte(ctxt)) { ctxt_sys_reg(ctxt, TFSR_EL1) = read_sysreg_el1(SYS_TFSR); ctxt_sys_reg(ctxt, TFSRE0_EL1) = read_sysreg_s(SYS_TFSRE0_EL1); @@ -95,6 +103,8 @@ static inline void __sysreg_restore_user_state(struct kvm_cpu_context *ctxt) { write_sysreg(ctxt_sys_reg(ctxt, TPIDR_EL0), tpidr_el0); write_sysreg(ctxt_sys_reg(ctxt, TPIDRRO_EL0), tpidrro_el0); + if (has_gcs()) + write_sysreg_s(ctxt_sys_reg(ctxt, GCSPR_EL0), SYS_GCSPR_EL0); } static inline void __sysreg_restore_el1_state(struct kvm_cpu_context *ctxt) @@ -138,6 +148,13 @@ static inline void __sysreg_restore_el1_state(struct kvm_cpu_context *ctxt) write_sysreg(ctxt_sys_reg(ctxt, PAR_EL1), par_el1); write_sysreg(ctxt_sys_reg(ctxt, TPIDR_EL1), tpidr_el1); + if (has_gcs()) { + write_sysreg_el1(ctxt_sys_reg(ctxt, GCSPR_EL1), SYS_GCSPR); + write_sysreg_el1(ctxt_sys_reg(ctxt, GCSCR_EL1), SYS_GCSCR); + write_sysreg_s(ctxt_sys_reg(ctxt, GCSCRE0_EL1), + SYS_GCSCRE0_EL1); + } + if (ctxt_has_mte(ctxt)) { write_sysreg_el1(ctxt_sys_reg(ctxt, TFSR_EL1), SYS_TFSR); write_sysreg_s(ctxt_sys_reg(ctxt, TFSRE0_EL1), SYS_TFSRE0_EL1); diff --git a/arch/arm64/kvm/sys_regs.c b/arch/arm64/kvm/sys_regs.c index 4735e1b37fb3..300719f82dd1 100644 --- a/arch/arm64/kvm/sys_regs.c +++ b/arch/arm64/kvm/sys_regs.c @@ -1876,6 +1876,23 @@ static unsigned int mte_visibility(const struct kvm_vcpu *vcpu, .visibility = mte_visibility, \ } +static unsigned int gcs_visibility(const struct kvm_vcpu *vcpu, + const struct sys_reg_desc *rd) +{ + if (has_gcs()) + return 0; + + return REG_HIDDEN; +} + +#define GCS_REG(name) { \ + SYS_DESC(SYS_##name), \ + .access = undef_access, \ + .reset = reset_unknown, \ + .reg = name, \ + .visibility = gcs_visibility, \ +} + static unsigned int el2_visibility(const struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd) { @@ -2223,6 +2240,10 @@ static const struct sys_reg_desc sys_reg_descs[] = { PTRAUTH_KEY(APDB), PTRAUTH_KEY(APGA), + GCS_REG(GCSCR_EL1), + GCS_REG(GCSPR_EL1), + GCS_REG(GCSCRE0_EL1), + { SYS_DESC(SYS_SPSR_EL1), access_spsr}, { SYS_DESC(SYS_ELR_EL1), access_elr}, @@ -2309,6 +2330,7 @@ static const struct sys_reg_desc sys_reg_descs[] = { { SYS_DESC(SYS_SMIDR_EL1), undef_access }, { SYS_DESC(SYS_CSSELR_EL1), access_csselr, reset_unknown, CSSELR_EL1 }, { SYS_DESC(SYS_CTR_EL0), access_ctr }, + GCS_REG(GCSPR_EL0), { SYS_DESC(SYS_SVCR), undef_access }, { PMU_SYS_REG(PMCR_EL0), .access = access_pmcr, .reset = reset_pmcr, From patchwork Wed Nov 22 09:42:25 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Brown X-Patchwork-Id: 13464409 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 D52E7C61D9B for ; Wed, 22 Nov 2023 09:44:50 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6DAEB6B05A8; Wed, 22 Nov 2023 04:44:50 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 688F76B05A9; Wed, 22 Nov 2023 04:44:50 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 503246B05AA; Wed, 22 Nov 2023 04:44:50 -0500 (EST) 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 417336B05A8 for ; Wed, 22 Nov 2023 04:44:50 -0500 (EST) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 2194F8014D for ; Wed, 22 Nov 2023 09:44:50 +0000 (UTC) X-FDA: 81485105940.18.AFA9F35 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by imf03.hostedemail.com (Postfix) with ESMTP id 2465C2001C for ; Wed, 22 Nov 2023 09:44:47 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=SyPO5tjn; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf03.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=1700646288; 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=u9Kh223n1sw2bA4JKVyqpsNW0iXZWBnYpCmPJVjaA/w=; b=mehDeRdZ1vO05jrs2VzZu932SPGptUJPqotdZfTg3I6XRjb045/xoIdh9i8k85U2oTmTu7 BvZMVgdi1q6wKOdwTxsNKVrCqASgpIC19RmPfJK0pn0LUbzlIo765LW5NLrXfJkgnfqt6u lIEhUg9MDQV0hGccdTU18XwY/l5ihes= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=SyPO5tjn; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf03.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=1700646288; a=rsa-sha256; cv=none; b=XEYXSsFkIucAIIzx6ghsoTJj14liBgfJLEEgvDkH/wGVhXtVz/c1o2x/E0N3EKhLXqNywe vzeDLnfpJF5KSXCw+n2p6B0hvzH94/RUPU1ObEAUspZB+caeIskaurs+kq5qkbxj7yoWGW xBSj3mXP8/HvKPGpTuHor1C9X2/ghDY= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by ams.source.kernel.org (Postfix) with ESMTP id DED0CB81183; Wed, 22 Nov 2023 09:44:45 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 70B4AC433C7; Wed, 22 Nov 2023 09:44:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1700646285; bh=eAZyVctWUIW822C6a4XspzvFhF8YmdJ2KmfW+KTTZkg=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=SyPO5tjneJ1lWQ6vMvro8FogjGi2iuc2b+pcmTXuO8VKhRqYlvw00yJlvvbHsdh84 TjGzB+y7+/Y3Q920f0JAUO7oIDuDC/YZstqwF38A6ixh909kP+lzJHULCdJGX+fIzu q2JxSOEaKpH+uJ3EXxGW9/9dWeXbeAXUDeWofNO1W6RlsAf2V1MwnpzjgSB/ogU4X+ iQxSL+7QlIiwyGcC1yZ1M5CuYrxppxx40R+51v4wstppfqJqFFJl8enKcPtvvp49Ck 5eg7e8+cZV57vDaFzG3Pv8smXqbDeUH6q2VouWbCf5uw9sS0ZDl4rykCqfkbJEE4hX ADwfS0/JxHqSg== From: Mark Brown Date: Wed, 22 Nov 2023 09:42:25 +0000 Subject: [PATCH v7 15/39] arm64/gcs: Allow GCS usage at EL0 and EL1 MIME-Version: 1.0 Message-Id: <20231122-arm64-gcs-v7-15-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=2246; i=broonie@kernel.org; h=from:subject:message-id; bh=eAZyVctWUIW822C6a4XspzvFhF8YmdJ2KmfW+KTTZkg=; b=owEBbQGS/pANAwAKASTWi3JdVIfQAcsmYgBlXc0CgBXsZyH7UmX+GJUnolHom10FZwrrBFtz2 fm0irI1LESJATMEAAEKAB0WIQSt5miqZ1cYtZ/in+ok1otyXVSH0AUCZV3NAgAKCRAk1otyXVSH 0DSKB/0YQg4ct+cd7LzPnbzosVtkcpo9NOAazn/sUsJ2DtI354warKGrRY6PxciWlxzrMDgiIus HiJme7H4zGCw0IDbOmwwTfwCBkzLrvskL6mtcVElBOwwj0p22FxA2z06bnEyl+hOGCVOvm6HJGr S2L6/PMBzs/qmDYtEl04cqXyrEjy7B073aCQ39bj4OWb8e9pd1x45oG1v1BG9nEsklqU/iYgKZl +W/7rPeizJfM/yQ1ZEYuaJ73f8isMI/FMN+vA1i0JVuceYiVSaL7GypOFOYgDGwoP/Xmt6S2aUk PoVEJ298MhJe8H7+mrvEZOIqa4+3U3qcVTURWXFcrBZzuVbh X-Developer-Key: i=broonie@kernel.org; a=openpgp; fpr=3F2568AAC26998F9E813A1C5C3F436CA30F5D8EB X-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 2465C2001C X-Stat-Signature: gsw9eb3beoraa15i3fny8s6g1twgfiao X-HE-Tag: 1700646287-608891 X-HE-Meta: U2FsdGVkX18DHc1igdN17+ERylzs1Fv6Wwz3f6uvZESR+QSeAqVPUHtqyHMC++CSazPhDKebwY9603uUOzezvXcQINoCj6rxDb8gQVQ1OTJeA+k711exrl1NElhfOxIqhuiJDEf5R9HIubGGEqDUdo91ucFgohfPFDALHOKE+xIGPq/fPzcSpJsPoe8eg87N0rAK0wPf1UezS34gVIhU7WjSpmr68VlYvpC8S+Z+JZ0HZNofWpdgqfJiRp/Bo6Y7wul6sEHbRbcGkoYdn8X5WozBG2dptz/gpe4+OKI7pMcU7SsCk/wsNgyw8QHudipoJ+3dh3yF5Tgz84xYrlEt49Y7S8NutyQfKsHsg8VXXO44iwtCR8Q0ZP2kmlPWWtOYLaBDvtfj+RmEMi/xMnCV0Gqcha4aJ4DE/IjyOGg9j7Aa18b3j9zE73NdU4F5FrtwlyRt6QKIidBp8IcncpIaxeZhSEV54EsljxJCQJOTQeLkqQpgFawOuAgYMZa2tMiiyYPbi9OOC1jlDZze0ucDbRY6wgwrHbS0rZirOQY62E9oQ8rwbxSk49e1ScKmhItjVzZp1uZnOg3mMd6vRFOdmlRE94YGk4bsVAp51ICJlqFexKf/zHaUQt4i35jIqfjMnFK1FpzUjUChxUDQnvQvnqE7UHvcU+/i7N4b9BRTFctOLnGJ1eqRJBqWQ0o1SrpotqcBQ3w7OiOTgUYn/2xBkRG82kQ++YW9dsOTrGTu9k505Adpxopj0Ous+8Ms5I8fnI0gXUQTyHxdTC4torIKvQrMXeaBleVDRHRvqAz+TD0DYjKrek9ovn18k+ac19YvTCmg0ZtnhDwFqUy9/FKm/GIfIzXwiVOv1Q++JJ1JSxA64jIUlLz7EU5MTCJe3otQMAJziRxQJBkCdBqDHF6zIUO41Zzfdi4YC66eq9VPgGAUxsEBEMtz2j/TNrxrk+LChUGFHnu5Pat6Dcy40NV L/JszYSC i7V8hpzRGnAx5PkWf0tbfdSTCCQg/dNJska505Eq2B1BW4kyx5ta51Zgo1CQG4oNjN32CTEj2mZpNsd4TKoWyZySROl3vFL2v7xVypekoGFu6TRCXVYd/aHIhEnrC0hyVPj+7GsN6ETml9kIKXAIEXSJs34jtmx9wZR5yipWo3/yGu9nv4dvchnFERUKHv5uqgDDxVD2yiSxAFMVICEAOrHysdE9cvIN31WgIUddR09vZEvohz5zIdGLLOhi1E5ouriA+XOyCqACC5bn4d1i5cLkAAGrs6MQ36QMKrmbVzoLmocPNU4MYvKS4WNmXhVk2BJV+ 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: There is a control HCRX_EL2.GCSEn which must be set to allow GCS features to take effect at lower ELs and also fine grained traps for GCS usage at EL0 and EL1. Configure all these to allow GCS usage by EL0 and EL1. Signed-off-by: Mark Brown --- arch/arm64/include/asm/el2_setup.h | 17 +++++++++++++++++ arch/arm64/include/asm/kvm_arm.h | 4 ++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/arch/arm64/include/asm/el2_setup.h b/arch/arm64/include/asm/el2_setup.h index b7afaa026842..17672563e333 100644 --- a/arch/arm64/include/asm/el2_setup.h +++ b/arch/arm64/include/asm/el2_setup.h @@ -27,6 +27,14 @@ ubfx x0, x0, #ID_AA64MMFR1_EL1_HCX_SHIFT, #4 cbz x0, .Lskip_hcrx_\@ mov_q x0, HCRX_HOST_FLAGS + + /* Enable GCS if supported */ + mrs_s x1, SYS_ID_AA64PFR1_EL1 + ubfx x1, x1, #ID_AA64PFR1_EL1_GCS_SHIFT, #4 + cbz x1, .Lset_hcrx_\@ + orr x0, x0, #HCRX_EL2_GCSEn + +.Lset_hcrx_\@: msr_s SYS_HCRX_EL2, x0 .Lskip_hcrx_\@: .endm @@ -190,6 +198,15 @@ orr x0, x0, #HFGxTR_EL2_nPIR_EL1 orr x0, x0, #HFGxTR_EL2_nPIRE0_EL1 + /* GCS depends on PIE so we don't check it if PIE is absent */ + mrs_s x1, SYS_ID_AA64PFR1_EL1 + ubfx x1, x1, #ID_AA64PFR1_EL1_GCS_SHIFT, #4 + cbz x1, .Lset_fgt_\@ + + /* Disable traps of access to GCS registers at EL0 and EL1 */ + orr x0, x0, #HFGxTR_EL2_nGCS_EL1_MASK + orr x0, x0, #HFGxTR_EL2_nGCS_EL0_MASK + .Lset_fgt_\@: msr_s SYS_HFGRTR_EL2, x0 msr_s SYS_HFGWTR_EL2, x0 diff --git a/arch/arm64/include/asm/kvm_arm.h b/arch/arm64/include/asm/kvm_arm.h index b85f46a73e21..e4b6a29879b7 100644 --- a/arch/arm64/include/asm/kvm_arm.h +++ b/arch/arm64/include/asm/kvm_arm.h @@ -103,9 +103,9 @@ #define HCR_HOST_VHE_FLAGS (HCR_RW | HCR_TGE | HCR_E2H) #define HCRX_GUEST_FLAGS \ - (HCRX_EL2_SMPME | HCRX_EL2_TCR2En | \ + (HCRX_EL2_SMPME | HCRX_EL2_TCR2En | HCRX_EL2_GCSEn |\ (cpus_have_final_cap(ARM64_HAS_MOPS) ? (HCRX_EL2_MSCEn | HCRX_EL2_MCE2) : 0)) -#define HCRX_HOST_FLAGS (HCRX_EL2_MSCEn | HCRX_EL2_TCR2En) +#define HCRX_HOST_FLAGS (HCRX_EL2_MSCEn | HCRX_EL2_TCR2En | HCRX_EL2_GCSEn) /* TCR_EL2 Registers bits */ #define TCR_EL2_RES1 ((1U << 31) | (1 << 23)) From patchwork Wed Nov 22 09:42:26 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Brown X-Patchwork-Id: 13464410 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 34A54C61D9D for ; Wed, 22 Nov 2023 09:44:56 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C0A886B05AA; Wed, 22 Nov 2023 04:44:55 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id BB9F66B05AB; Wed, 22 Nov 2023 04:44:55 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A5C0D6B05AC; Wed, 22 Nov 2023 04:44:55 -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 9179C6B05AA for ; Wed, 22 Nov 2023 04:44:55 -0500 (EST) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 6E65940614 for ; Wed, 22 Nov 2023 09:44:55 +0000 (UTC) X-FDA: 81485106150.09.68C9772 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf02.hostedemail.com (Postfix) with ESMTP id AF35480017 for ; Wed, 22 Nov 2023 09:44:53 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b="p0Z/U7xM"; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf02.hostedemail.com: domain of broonie@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=broonie@kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1700646293; a=rsa-sha256; cv=none; b=jVcac4uDLB/gJmguGUJpqJqu3IB6jo5Ux6jguyyzqFL64Vml+U2a8DidjgXSYbwcG7Bv0W l8AeWpe2RlnROodG+iHZC+mBJv39ZfylSoG7TLEH1A5HJLNnhHr907CDF4MK0I/FWL4QP7 /l4chSV6K8ITLw9FQ1/V7lUYv04Imoo= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b="p0Z/U7xM"; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf02.hostedemail.com: domain of broonie@kernel.org designates 139.178.84.217 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=1700646293; 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=V005LwtjUfEy1OzahQ56WzeSFrnTdi98gEeViD70LKc=; b=1AvF7/gl2fcB4I3eYIktbBHqPVw/R936nHGrleBGQkqWhbx1AT7QnJuTjygqgpDfBQWNsD JVSEPTbZeQhtxBF0HhHfn+dNA7M13YS+r/CHj2tPUJGbuQFyn9zpzxuulWRji+fj84rWUY pFWIgQ22ji355yP5Gtb4t3Qk4qmF/+I= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id E168361CCC; Wed, 22 Nov 2023 09:44:52 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id B6165C433CD; Wed, 22 Nov 2023 09:44:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1700646292; bh=HrTZdiDMDN2AL5+FFpvaO5U1EqVinVUtyfSdqXL4wYA=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=p0Z/U7xMPKgM32RdtDRReCBHmtw+qLgWMkmr9fAlX9sTPpSTX1kzT1b4Ax8iNZTQ1 SDpqnAv/m3xI6cskZjeIJXtgnHudh+xvZe8lSAZLVs7RjUByzMaLt84cVni6atFfi1 +X3DScY2qReqW23WxMSFy7bEE8mBc2v/XGgaKns+PNuVAPlMnGMDRcv9OEpAI4D/om CpDnm2ALUXUkqaRpqSUp+bCmc6Y3gqo06ZtP0UHlplrxt73e4QcDOclNWVf+LnjHaN QXl+y5Ql95Ai80nNQ7l/b37t+jrxyuqXFBUhUW9i2y6qZpxx91LF6ySTkMQLR3is6Y lXvqWl1Km6fLA== From: Mark Brown Date: Wed, 22 Nov 2023 09:42:26 +0000 Subject: [PATCH v7 16/39] arm64/idreg: Add overrride for GCS MIME-Version: 1.0 Message-Id: <20231122-arm64-gcs-v7-16-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=1992; i=broonie@kernel.org; h=from:subject:message-id; bh=HrTZdiDMDN2AL5+FFpvaO5U1EqVinVUtyfSdqXL4wYA=; b=owEBbQGS/pANAwAKASTWi3JdVIfQAcsmYgBlXc0CojGZj7s7XLMcN8F+/cIfToo5TSu1/o7Jx iRDev9LKKeJATMEAAEKAB0WIQSt5miqZ1cYtZ/in+ok1otyXVSH0AUCZV3NAgAKCRAk1otyXVSH 0IzPCACBRiVUTa+NHQT8XYai1y7/yLDxXJtjiAwGWCackHesESEtvValhKUigkGVJTRmqHLXptp cfwVGt2mctgc4HJtz8X8e6HP2UNCAeeD3OzagUJnZaDRGgKuFx6WxtnBaxjDKorTWBOhf6WqttU iV+LqXArWC0PbuMC2DgqoQwv6zSNVIeG0//Fhbwj7qd/pve5AUJV5aFS8zHE0FBOSbKfDl5y15z Xn+yX4gbWJIf0xIN4DDjLjwN3Jl0uM2gMo9nMACXRJvrQLhHqw3BjMF7ida4CqfIiVvU/BTHlpI jlGGSl6tFko0XV6J3tsrAJW+uVFoylFuxbpJ/cMFFFqzZs5R X-Developer-Key: i=broonie@kernel.org; a=openpgp; fpr=3F2568AAC26998F9E813A1C5C3F436CA30F5D8EB X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: AF35480017 X-Stat-Signature: rcyhokzwx7i3g6cf6dkfjuj7gukqmzdw X-HE-Tag: 1700646293-900307 X-HE-Meta: U2FsdGVkX1/WmSaTV83J6D/nDgnPB2r9lTiccvyK1JHhxoG6X/uFCd3GGOlUcOMywsLAkdGvBeXoBehBuOzJVuOMduG9KJBYBXm5z4N8cmra9mwOzBFM6WwMj3fFKmB4ddumYoj2ceWj8yELgZYCpjJzhHTyoD9kLdGZNNQEJnqGMATPrQSGwu77e8GNyvZezQmvoDxSULzPx/qmgiHY2PurIoLzTvNIECe+EaqII3ka31S+uPWRLIXZv0x8ArwjgEZkdxgX7audzXOny75c2WZ9oaWP6v8Qiygzt6ImFQ7C85oamou+GQmaR5LvOLn7sHO5H5K0q0f8QXP4Ltrp5hIuspiYB0aatFHyKcq4y6vBCnYXSpGambyKafXhoCaAFz08tHAKgJHj5+y+QGnCcQ8cQwPZlescXUpBpOlVxEKGdHR5LpjOL1oKKRu3zWwBgv3SjkBQIMlucRYUxwzDQUz7zuYzAlqoKkRx6t1w06XaXBgcW/T7HcdH19Qe5I21KRod6sfD/M/nRunDaVXIY9UIFeyzOEsWybwjvAuWs5+Bu7kcV1LbeHoboEc7mYIvq9V/vp3TYtxnDY05KgFQ8e0VfJMY8uFxOs48x02WPrDLCOKQwgZWsmEdxJRnpOXIWnAXDAx7Nqs0jKXvd0u4arPL3BLTOZA9qC9KgloD1fc0lJl44WXtnNb7JSoHokf/rakeGOqHziKZqyoWBaKfBqSRLaANvaiErg1O8eWCCYxCUogGgqkemdQm0MD9A5W/+YRagXSe1JHwVklXUrA97sdjgobSJQP6Gjl70RDNLUu0JYVZf9rFig3Iwc5DCv+y1Y+kZLpzDmgmz48LrbNsUKSU/BSbYN1kkBl9Dmff9t46zNkd/oyaFWuRCiSIZZPoeqJgwstgXI6Uw599dhZvC9UpMis2dg9OR6NJTUm5TZgRCk9MhlGWaA/zW7hFekyuYp/f1NQIUTK/WbR+Yv1 P6zUw/2S ZahF3fSpcJfVnLXnIDMCknBuRJJiMNWa1BJUw/YQrjOmIMDvMNKVQYxazMN8pcxxObnyJPiAkATMiL/elovZpmY2rBSzqB4u8tgtrh9FQxiGgBb0vMNhUXIx2XwVLg/5IpavE4MXqGm0/hd2qdLLf1q9uG+UCiTSLcAxob6Zh1jTrYuBCS/RrqndhSbbAmT07Cw9O14TARwHZ+ZQbNrr/a2SLCiOB/oTjDS7nvj51OvjAGIGOhXr0zY9GtFDRKrENjU4fVW8V3+922EYtuaiA2hYEvoFITvm7dXB2smYSPdkDMasDM7Gl1jZGg9cWlvx9tNEc 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: Hook up an override for GCS, allowing it to be disabled from the command line by specifying arm64.nogcs in case there are problems. Signed-off-by: Mark Brown --- Documentation/admin-guide/kernel-parameters.txt | 6 ++++++ arch/arm64/kernel/idreg-override.c | 2 ++ 2 files changed, 8 insertions(+) diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt index 65731b060e3f..e2282f310ad7 100644 --- a/Documentation/admin-guide/kernel-parameters.txt +++ b/Documentation/admin-guide/kernel-parameters.txt @@ -418,9 +418,15 @@ arm64.nobti [ARM64] Unconditionally disable Branch Target Identification support + arm64.nogcs [ARM64] Unconditionally disable Guarded Control Stack + support + arm64.nomops [ARM64] Unconditionally disable Memory Copy and Memory Set instructions support + arm64.nopauth [ARM64] Unconditionally disable Pointer Authentication + support + arm64.nomte [ARM64] Unconditionally disable Memory Tagging Extension support diff --git a/arch/arm64/kernel/idreg-override.c b/arch/arm64/kernel/idreg-override.c index 3addc09f8746..cc059ba39d58 100644 --- a/arch/arm64/kernel/idreg-override.c +++ b/arch/arm64/kernel/idreg-override.c @@ -99,6 +99,7 @@ static const struct ftr_set_desc pfr1 __initconst = { .override = &id_aa64pfr1_override, .fields = { FIELD("bt", ID_AA64PFR1_EL1_BT_SHIFT, NULL ), + FIELD("gcs", ID_AA64PFR1_EL1_GCS_SHIFT, NULL), FIELD("mte", ID_AA64PFR1_EL1_MTE_SHIFT, NULL), FIELD("sme", ID_AA64PFR1_EL1_SME_SHIFT, pfr1_sme_filter), {} @@ -178,6 +179,7 @@ static const struct { { "arm64.nosve", "id_aa64pfr0.sve=0" }, { "arm64.nosme", "id_aa64pfr1.sme=0" }, { "arm64.nobti", "id_aa64pfr1.bt=0" }, + { "arm64.nogcs", "id_aa64pfr1.gcs=0" }, { "arm64.nopauth", "id_aa64isar1.gpi=0 id_aa64isar1.gpa=0 " "id_aa64isar1.api=0 id_aa64isar1.apa=0 " From patchwork Wed Nov 22 09:42:27 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Brown X-Patchwork-Id: 13464411 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 C6119C61D9C for ; Wed, 22 Nov 2023 09:45:04 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5C7AA6B05AC; Wed, 22 Nov 2023 04:45:04 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 579746B05AD; Wed, 22 Nov 2023 04:45:04 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 419196B05AE; Wed, 22 Nov 2023 04:45:04 -0500 (EST) 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 2D6A46B05AC for ; Wed, 22 Nov 2023 04:45:04 -0500 (EST) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 0074EB5067 for ; Wed, 22 Nov 2023 09:45:03 +0000 (UTC) X-FDA: 81485106528.03.ACACE13 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by imf22.hostedemail.com (Postfix) with ESMTP id E54ADC001D for ; Wed, 22 Nov 2023 09:45:01 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=hzQJt+Qj; spf=pass (imf22.hostedemail.com: domain of broonie@kernel.org designates 145.40.68.75 as permitted sender) smtp.mailfrom=broonie@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1700646302; 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=urIKzgF+gbFci+9gR98y0hlMG49q807Mv9quL2usDL0=; b=RLLAeuFXk0yMuWNb1x9rftAWbUDaw3nwCaTktrTsVbXhTznxkM2bjeoNGQe10d3KLA8NS5 Kr/vlf9O95FJUK8Md3Z3Z9uFAtaxki2R5C4o7+F8DVlR1r72eX53mRz04SApCjLJ/z1a5V Ga+1zsNRjrh7iQPmUuMvLyeh9upZ2OQ= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1700646302; a=rsa-sha256; cv=none; b=QUWD0g78yIHStPUD5zE3w7Nc87gWt8mDenuYCDb+nm1sV4ZgM+ykgH3NVijZ+sO+zFVRcO EZx9NLToQfoz+/pbET/VMsbTQYWIlWf9QJNyZpVr2lPOR0JhXSNw7Bl4in6Mc1TamrgZuq PM+nH4zuxP7v9YsswO2nL9+U17OBNoI= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=hzQJt+Qj; spf=pass (imf22.hostedemail.com: domain of broonie@kernel.org designates 145.40.68.75 as permitted sender) smtp.mailfrom=broonie@kernel.org; dmarc=pass (policy=none) header.from=kernel.org Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by ams.source.kernel.org (Postfix) with ESMTP id 806E6B8118C; Wed, 22 Nov 2023 09:45:00 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 11890C433C9; Wed, 22 Nov 2023 09:44:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1700646299; bh=aU3xh1nd0j/0DloWdWCWjw7vV0cVDgM+Blqu3VdYPtQ=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=hzQJt+QjotGyzdW5aJXId+R762os1ez45wKIR4uvXUTtbTXvvCKCEaxjLQyE5+11O K/3A3ODSi956zsBNcLEf8f9eLMdYm4TfOH9+z7tMTrIVtOdP9B4+I+wuRvMERobQ0h JUCP4jebtXbiUqppMsA8G+rhZvXVKolcxtB4XUuTV/t0oWGtgR4VblnkO4cK75mYwx 9qI5OOQOQdgWtzvzwMesWgtsn9CBkG0LqBT1uOptR+3dcOUP2In7IV76hwF2BkIhlS 3YE7/eFIr9cmqTmfXYxf0PZPbAKrclY8V1Irgn/qaq2Sr3mVtzvKuhhfqIj/o2sm81 2gp9m8bABLqRQ== From: Mark Brown Date: Wed, 22 Nov 2023 09:42:27 +0000 Subject: [PATCH v7 17/39] arm64/hwcap: Add hwcap for GCS MIME-Version: 1.0 Message-Id: <20231122-arm64-gcs-v7-17-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=2995; i=broonie@kernel.org; h=from:subject:message-id; bh=aU3xh1nd0j/0DloWdWCWjw7vV0cVDgM+Blqu3VdYPtQ=; b=owGbwMvMwMWocq27KDak/QLjabUkhtTYs8yBxYdLZ52e+l5Oe09GrZNLak6ww3nOF9bCn5eUz FSfv86zk9GYhYGRi0FWTJFl7bOMVenhElvnP5r/CmYQKxPIFAYuTgGYiBcP+z8LRdmId5Olin8b 5BuLuhe2H8zc9ODU6aXpE8zOPVi9ocN05a+2aUysdeZfqnTPtFgnF7PO8Qh455/rUsuYdEXCoWZ e5IkWn8z3tRp6xy/cEp5xxCrjXWG54XKjD0tzVhQL8U0tviwncOXjLeGMSaYXTx7ZMyv5ZSe/z9 KE6susi0KynUof7X5r/u7qHfdQVpVtXS8614fxdCQcYuox5Nz0pPapaL5lp2gH9/Zn+zrrWY6+C l5S0Cl4egfbyxm98Wl8aS1hDzNipDcaelg8K3/9QGndFfevh306TfLYk3wcJffkljH++LlJwvkP m6kTi1VGgv/ioufK5joTf4rHJIo9vjD7mFjODZHj5Qq/AA== X-Developer-Key: i=broonie@kernel.org; a=openpgp; fpr=3F2568AAC26998F9E813A1C5C3F436CA30F5D8EB X-Rspamd-Queue-Id: E54ADC001D X-Rspam-User: X-Stat-Signature: ffrdwir6mta89k56ifzeenayktayjin3 X-Rspamd-Server: rspam03 X-HE-Tag: 1700646301-252185 X-HE-Meta: U2FsdGVkX1896xkg/Xnh5paOg8HNm5JE+VOU19Gvv5ekk3rsAokDGBJC+mgAFYieC8a2VelKftkLbE0QhLLZ4ads/Etk5SGQWgXRb6rZ6wMq6jv935auT9W8eb04gmtfxaCrBPGh/PTDMGF9XySbbqngUwwXUrSsB+qwVa7P8Zo16pK02AWEkbssIyCRRtV/4ooL4Xo2zHK0jrxfjRJFZPocMT8kVZsjuD0XviZtl22li3Sx+a1iL4nxAF6+FrpUdcgOe0kIyR6g7QTGspacIKju/KQ8Mac4eo2EDwadYfNEW9L7Rzl01ZaPqTSp5dtaPUmBB7lZb+4lEFDk4t14TIrRjTuoRnwC8RCSlqCHrJJxpx5dfUPFnX5wEYDel7X1gmUVYEb86gPoEdUucv4gR9UMZtc1CTOkn1wQFX9Ad/gX0P420K0q4pYReo6bV/iptUoohfiG4FI/BHcLF+9z9ZwBC108+rm73ov3iZ/3lyWOoN0eU69w5ANC5Xt61MdcwQhiDVNSfwMq9yHVv0wQv3cTy2ZTEDrdg8X5NrEhDLdrZ59OFPSIDhplQPxKgCi6hIAFcxshqlrhPu25WwQbflHywSi1xkuLtAl/xClThr57mtm8Qof50/QAskjlm1uFxVRpGFU0S32wRcInLNhe3EtD3Sue5jwcF1GHnZo889lmmZL/9PYTOsMCYVGmkwQ3Nr7nrurnlk2AUBXGfLpnC5jbAawfqGlEOVjAnpT4HecwfbXUpLBZLSwwBJJTOMvv9NA8iS4/Whn72kys5jaUF2d2J6V/pOY8D42uxgsr7YPq4JlS4OBsgUs9YWNm+iwltbp82yKVv5Pl/lv5alLrHNB7K/0tGTCzK+lB8IVjV4vr/dhZPBGnmsVLS1MaLJu+XSh15kmMgyVvQpCEdaCCQTVO00JJ822CN+d0ePtWSzf6EMCJya79foBFKR606ycSt5+2Lkm0og+fmccQ969 g9D0GmaS uPLC4rhenBfWhNbDFFr7Y3HFzyTmJeuC2lK4yy2Wfjmf48CCDevInjUs2iNRcCzoiVPQwjlZX8tiIwBhA4HEme1fCb4S/5Nk0N/qn8GqGpHlrQkhyqfVhgB7hZ0BL302eW02C4t/cevg7Qf/wdA0XYeSXTPj7Nl7ia0Gn3R5al318Z7dywD6aAFQ9OodZ1RGesSOtBI17Vl4uUmukUvCFS5jre0V4/JBA1bOuMafxS4xH7eP0fOYR/uEauwDv6sSNZyZmMH2KFos3hnVc1VY3eaKbJxaRk9ztekFL39f409SFapJmtNTJbDUy1lBYANWxplxa 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: Provide a hwcap to enable userspace to detect support for GCS. Signed-off-by: Mark Brown --- Documentation/arch/arm64/elf_hwcaps.rst | 3 +++ arch/arm64/include/asm/hwcap.h | 1 + arch/arm64/include/uapi/asm/hwcap.h | 1 + arch/arm64/kernel/cpufeature.c | 3 +++ arch/arm64/kernel/cpuinfo.c | 1 + 5 files changed, 9 insertions(+) diff --git a/Documentation/arch/arm64/elf_hwcaps.rst b/Documentation/arch/arm64/elf_hwcaps.rst index ced7b335e2e0..86d4ace9c75c 100644 --- a/Documentation/arch/arm64/elf_hwcaps.rst +++ b/Documentation/arch/arm64/elf_hwcaps.rst @@ -317,6 +317,9 @@ HWCAP2_LRCPC3 HWCAP2_LSE128 Functionality implied by ID_AA64ISAR0_EL1.Atomic == 0b0011. +HWCAP2_GCS + Functionality implied by ID_AA64PFR1_EL1.GCS == 0b1 + 4. Unused AT_HWCAP bits ----------------------- diff --git a/arch/arm64/include/asm/hwcap.h b/arch/arm64/include/asm/hwcap.h index cd71e09ea14d..e01e6b72a839 100644 --- a/arch/arm64/include/asm/hwcap.h +++ b/arch/arm64/include/asm/hwcap.h @@ -142,6 +142,7 @@ #define KERNEL_HWCAP_SVE_B16B16 __khwcap2_feature(SVE_B16B16) #define KERNEL_HWCAP_LRCPC3 __khwcap2_feature(LRCPC3) #define KERNEL_HWCAP_LSE128 __khwcap2_feature(LSE128) +#define KERNEL_HWCAP_GCS __khwcap2_feature(GCS) /* * This yields a mask that user programs can use to figure out what diff --git a/arch/arm64/include/uapi/asm/hwcap.h b/arch/arm64/include/uapi/asm/hwcap.h index 5023599fa278..996b5b5d4c4e 100644 --- a/arch/arm64/include/uapi/asm/hwcap.h +++ b/arch/arm64/include/uapi/asm/hwcap.h @@ -107,5 +107,6 @@ #define HWCAP2_SVE_B16B16 (1UL << 45) #define HWCAP2_LRCPC3 (1UL << 46) #define HWCAP2_LSE128 (1UL << 47) +#define HWCAP2_GCS (1UL << 48) #endif /* _UAPI__ASM_HWCAP_H */ diff --git a/arch/arm64/kernel/cpufeature.c b/arch/arm64/kernel/cpufeature.c index 5477451cefa1..dab80391543f 100644 --- a/arch/arm64/kernel/cpufeature.c +++ b/arch/arm64/kernel/cpufeature.c @@ -2859,6 +2859,9 @@ static const struct arm64_cpu_capabilities arm64_elf_hwcaps[] = { HWCAP_CAP(ID_AA64ZFR0_EL1, I8MM, IMP, CAP_HWCAP, KERNEL_HWCAP_SVEI8MM), HWCAP_CAP(ID_AA64ZFR0_EL1, F32MM, IMP, CAP_HWCAP, KERNEL_HWCAP_SVEF32MM), HWCAP_CAP(ID_AA64ZFR0_EL1, F64MM, IMP, CAP_HWCAP, KERNEL_HWCAP_SVEF64MM), +#endif +#ifdef CONFIG_ARM64_GCS + HWCAP_CAP(ID_AA64PFR1_EL1, GCS, IMP, CAP_HWCAP, KERNEL_HWCAP_GCS), #endif HWCAP_CAP(ID_AA64PFR1_EL1, SSBS, SSBS2, CAP_HWCAP, KERNEL_HWCAP_SSBS), #ifdef CONFIG_ARM64_BTI diff --git a/arch/arm64/kernel/cpuinfo.c b/arch/arm64/kernel/cpuinfo.c index a257da7b56fe..d557ee301464 100644 --- a/arch/arm64/kernel/cpuinfo.c +++ b/arch/arm64/kernel/cpuinfo.c @@ -130,6 +130,7 @@ static const char *const hwcap_str[] = { [KERNEL_HWCAP_SVE_B16B16] = "sveb16b16", [KERNEL_HWCAP_LRCPC3] = "lrcpc3", [KERNEL_HWCAP_LSE128] = "lse128", + [KERNEL_HWCAP_GCS] = "gcs", }; #ifdef CONFIG_COMPAT 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)", From patchwork Wed Nov 22 09:42:29 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Brown X-Patchwork-Id: 13464413 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 16A37C61D9D for ; Wed, 22 Nov 2023 09:45:19 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A83F66B05AF; Wed, 22 Nov 2023 04:45:18 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id A34376B05B0; Wed, 22 Nov 2023 04:45:18 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 921F16B05B1; Wed, 22 Nov 2023 04:45:18 -0500 (EST) 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 8232C6B05AF for ; Wed, 22 Nov 2023 04:45:18 -0500 (EST) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 4FA46405FB for ; Wed, 22 Nov 2023 09:45:18 +0000 (UTC) X-FDA: 81485107116.01.BC849D7 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by imf05.hostedemail.com (Postfix) with ESMTP id 5B764100004 for ; Wed, 22 Nov 2023 09:45:16 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b="l/tPHDZN"; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf05.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=1700646316; a=rsa-sha256; cv=none; b=wKf7OdPRs9TzLsKIB/fVHnJTvVgyQGlKRKk6x9ty2dXTEtTmW4ka368TNJPpnLaVyX3NcK mQHa7OTfCDpLz1/I/0ndxlpgPl1eFXvCZ5NG96OwRubZy3mgnDml8Oc0Y5KaKiU8GbnPIX DTLUkfTus2jTssgLy+jgVwKtsXURuVA= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b="l/tPHDZN"; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf05.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=1700646316; 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=I9GWmG1KSAG6j6NmhLXBB8A12JAWTxVIAAxeTQpcgyE=; b=0R02M3osn4ow7o8yfGunN1Ei/ZPLt0+ddmfx0hkdHEfnm9ZmDBJ4+MUZKPcrH5SwRIlmG1 WoGMhpCVMbQGn0doWFfXOxasQ/fqdfB3By1nGDBsNOYC9YTxaw7h1t0AxPbrH5VeJACK8Q ZAJmyw224oNwBoAiRI6RbQcHpvXAUY4= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by ams.source.kernel.org (Postfix) with ESMTP id 18A5DB81183; Wed, 22 Nov 2023 09:45:15 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id BEE1DC433C8; Wed, 22 Nov 2023 09:45:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1700646314; bh=BzV/RIjxnxMn9VbcnDWXuF4bpk5mXr34eQJiBtwP4NE=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=l/tPHDZNixS3bQk2n+SQhubpzTfcvaboniqmwUh/PTbCtGaJTfzsMFW7xRRZgI7QS iDE7cilI1DbJeAIwmDbOwO1mCCxOaIuTdR27BQhkBBc96Q7dT+6aLYUnAWSz4LUfqS wca9ympps1BSiJrViPZBxwqOIYjvs+Cu2pjPu8UHjcTdLxe209jcyhOb8S+92tP93r azGWWAvwK+SVC3z+7QS1CM2oNASn21FY+PMbJs3GDyS8RPf8e/bhbklg09GkGiJIcg hdpXy/Qq3ZPD9ODrfB7CvUWAmiZpuaUPoVHyapkCxoEFHvcrqWe74qnkqE/a3ZRzPR Yb5KCzU8Uh2Xg== From: Mark Brown Date: Wed, 22 Nov 2023 09:42:29 +0000 Subject: [PATCH v7 19/39] arm64/mm: Handle GCS data aborts MIME-Version: 1.0 Message-Id: <20231122-arm64-gcs-v7-19-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=5688; i=broonie@kernel.org; h=from:subject:message-id; bh=BzV/RIjxnxMn9VbcnDWXuF4bpk5mXr34eQJiBtwP4NE=; b=owEBbQGS/pANAwAKASTWi3JdVIfQAcsmYgBlXc0FZrStz9Xr8Prvr2S0JFwovux2nNPRmOouZ p8eOCwtpimJATMEAAEKAB0WIQSt5miqZ1cYtZ/in+ok1otyXVSH0AUCZV3NBQAKCRAk1otyXVSH 0NmMB/9iOfF48aW2DH8GfqhVA2m6OttDsfi8udYamecnJPcdti7OK5IEBs6O/7ccnIxI6+Dt8H3 Dm4IDlD9qdSD04W5mGrRffn6p3plw6PmIuzC7gd4iqjAG+6QDVAnQ2vRWkISWgXJ8VojIpu5cJg jETt5f7+meyZpX7Dz6KDygBxHgMpOs4Ua9UmN/yeDU6ZMIpCMGqOljcXee/8Xg5yn2itZwWOs0b MM2K35WGayANmXha1Z1vIME/dVrDqVqaF81BflwSn+dVaBejDxkNnVaECEDo4Y6qvCWsBs1OrYL OSJsoHrfhXZPAVAeHEiCu4/SYLssCMlwH/7uyX4yQ17OTMQx X-Developer-Key: i=broonie@kernel.org; a=openpgp; fpr=3F2568AAC26998F9E813A1C5C3F436CA30F5D8EB X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 5B764100004 X-Stat-Signature: uhdemnitsoihzjbt4wytnkg6fjwfqnzf X-HE-Tag: 1700646316-442429 X-HE-Meta: U2FsdGVkX1+IzpdrdnCXLlSo1Tve0mcPWl5rgxTFHH6czz3CHeujCS5klAkzGUUT1dc7ul33L7skykftRyFwp1rH+1jJeD9pCSAgqu8tXuoFgLMK/Agx5NVUnrK1Z+evOeAW2m0Z26vDBX3QoPNfVn4LrTZGjlvjdkUGZoXlsDiU2n8EXu/YdV5uSfpeu1aEVqRL56qz/e2CtrFUUixczT6BnOyjm5C/Sat0jhUtkg4iRb7O3zHhknGDus8wEZ10b0Wi6JevIezGvFWziXQkp2UAay++pG5EuTqzXIoIu7EKH/cnBST2rkFkAb02ZKhNrXtvPlG92KpE0noK1+ObLvxGYjJrJ7s5plfLjtHTpLNRJEEkYRl0s0tVfZA00LzTGL+e8/J316F4CRN5Qgdwfz1z/to8rXFvVoyRUNLeJFtkGIODLgC9b5w35V9tBdSQEMaydYud5OBZyI77LZ4b91m3JBCvpdRi/TkFV+zjBQQV4t6sZpVVNFDwcz2WkAGAR2f5eOms5TpYbYOhrgJhv251DEKrXmZBIv1Dblzm4TTN9V06uZNYvknq4PP1nJPl4EBIrmUXh8XbLJuzkLfv/f6/09hc+WpLdR1Zo/MKN5Xob5uwPrch8JsHcohOvZNDsc4pqqLIBF8eSZzoh5l8bCJQJElEWXa62HUDfvR8aSWgsZ3zlAkSvYLktAYDaO8rh5JXVoMFq+dph9iZM3fUp57RGlHUvG3rK0/BdbCKX0yrhT4kBQAzUv/YTPFgxshOBpUMCXSGQyymOyEA/FBQMvArk+IaU0HpB5f2YBle/4g+adG1TeaLw8uZRovQZPVjAbE3SXoD1PoxTqyO386k13plr4jgwuBDO8FTwkJS58Tc9PJ9EtVmzF6DSpHATXQmI2Gre11joOL1fS3gvLx7jucbThDkyFq0IFYgzOYQht02jkDw39MkUkyd4J5H9c37lKTVmEGzkjj/Y8ofURl MvXg9M4i /bwHEYX+MmQx3/K9d1+/mdazkcL7ng3p+7h/eZd6rsxriWmtExZCRa5kWmXSuCqXg8FAiEx7/VPdWWjABLNkmSLxJ4XPItyUR3M+0OezOgxhlotytb6dn3qC4gZ5bvAc8ky+YmBFK0D6P9neE0Ae/cOhVGyh8fhItpB/6z8iws6nYvaVMsEN3nJssvi6Oo1vRzu2Bh1ZRjyjA4odyDAn55fpA56vuY9L8fAGyS1wiYCJn8Iz9Os1fIyKyEWoX+aPof6vW7f5s/s4wfJc2hazWAblNjRmDNiKEo6M4gM28z1LtWDqmeOrGf5A2q3wI0P1PVvGj 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: All GCS operations at EL0 must happen on a page which is marked as having UnprivGCS access, including read operations. If a GCS operation attempts to access a page without this then it will generate a data abort with the GCS bit set in ESR_EL1.ISS2. EL0 may validly generate such faults, for example due to copy on write which will cause the GCS data to be stored in a read only page with no GCS permissions until the actual copy happens. Since UnprivGCS allows both reads and writes to the GCS (though only through GCS operations) we need to ensure that the memory management subsystem handles GCS accesses as writes at all times. Do this by adding FAULT_FLAG_WRITE to any GCS page faults, adding handling to ensure that invalid cases are identfied as such early so the memory management core does not think they will succeed. The core cannot distinguish between VMAs which are generally writeable and VMAs which are only writeable through GCS operations. EL1 may validly write to EL0 GCS for management purposes (eg, while initialising with cap tokens). We also report any GCS faults in VMAs not marked as part of a GCS as access violations, causing a fault to be delivered to userspace if it attempts to do GCS operations outside a GCS. Signed-off-by: Mark Brown --- arch/arm64/mm/fault.c | 79 +++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 71 insertions(+), 8 deletions(-) diff --git a/arch/arm64/mm/fault.c b/arch/arm64/mm/fault.c index 460d799e1296..28de0807b4a1 100644 --- a/arch/arm64/mm/fault.c +++ b/arch/arm64/mm/fault.c @@ -494,13 +494,30 @@ static void do_bad_area(unsigned long far, unsigned long esr, } } +/* + * Note: not valid for EL1 DC IVAC, but we never use that such that it + * should fault. EL0 cannot issue DC IVAC (undef). + */ +static bool is_write_abort(unsigned long esr) +{ + return (esr & ESR_ELx_WNR) && !(esr & ESR_ELx_CM); +} + +static bool is_gcs_fault(unsigned long esr) +{ + if (!esr_is_data_abort(esr)) + return false; + + return ESR_ELx_ISS2(esr) & ESR_ELx_GCS; +} + #define VM_FAULT_BADMAP ((__force vm_fault_t)0x010000) #define VM_FAULT_BADACCESS ((__force vm_fault_t)0x020000) static vm_fault_t __do_page_fault(struct mm_struct *mm, struct vm_area_struct *vma, unsigned long addr, unsigned int mm_flags, unsigned long vm_flags, - struct pt_regs *regs) + unsigned long esr, struct pt_regs *regs) { /* * Ok, we have a good vm_area for this memory access, so we can handle @@ -510,6 +527,26 @@ static vm_fault_t __do_page_fault(struct mm_struct *mm, */ if (!(vma->vm_flags & vm_flags)) return VM_FAULT_BADACCESS; + + if (vma->vm_flags & VM_SHADOW_STACK) { + /* + * Writes to a GCS must either be generated by a GCS + * operation or be from EL1. + */ + if (is_write_abort(esr) && + !(is_gcs_fault(esr) || is_el1_data_abort(esr))) + return VM_FAULT_BADACCESS; + } else { + /* + * GCS faults should never happen for pages that are + * not part of a GCS and the operation being attempted + * can never succeed. + */ + if (is_gcs_fault(esr)) + return VM_FAULT_BADACCESS; + } + + return handle_mm_fault(vma, addr, mm_flags, regs); } @@ -518,13 +555,18 @@ static bool is_el0_instruction_abort(unsigned long esr) return ESR_ELx_EC(esr) == ESR_ELx_EC_IABT_LOW; } -/* - * Note: not valid for EL1 DC IVAC, but we never use that such that it - * should fault. EL0 cannot issue DC IVAC (undef). - */ -static bool is_write_abort(unsigned long esr) +static bool is_invalid_el0_gcs_access(struct vm_area_struct *vma, u64 esr) { - return (esr & ESR_ELx_WNR) && !(esr & ESR_ELx_CM); + if (!system_supports_gcs()) + return false; + if (likely(!(vma->vm_flags & VM_SHADOW_STACK))) { + if (is_gcs_fault(esr)) + return true; + return false; + } + if (is_gcs_fault(esr)) + return false; + return is_write_abort(esr); } static int __kprobes do_page_fault(unsigned long far, unsigned long esr, @@ -573,6 +615,13 @@ static int __kprobes do_page_fault(unsigned long far, unsigned long esr, /* If EPAN is absent then exec implies read */ if (!alternative_has_cap_unlikely(ARM64_HAS_EPAN)) vm_flags |= VM_EXEC; + /* + * Upgrade read faults to write faults, GCS reads must + * occur on a page marked as GCS so we need to trigger + * copy on write always. + */ + if (is_gcs_fault(esr)) + mm_flags |= FAULT_FLAG_WRITE; } if (is_ttbr0_addr(addr) && is_el1_permission_fault(addr, esr, regs)) { @@ -594,6 +643,20 @@ static int __kprobes do_page_fault(unsigned long far, unsigned long esr, if (!vma) goto lock_mmap; + /* + * We get legitimate write faults for GCS pages from GCS + * operations, even when the initial operation was a read, as + * a result of upgrading GCS accesses to writes for CoW but + * GCS acceses outside of a GCS must fail. Specifically check + * for this since the mm core isn't able to distinguish + * invalid GCS access from valid ones and will try to resolve + * the fault. + */ + if (is_invalid_el0_gcs_access(vma, esr)) { + vma_end_read(vma); + goto lock_mmap; + } + if (!(vma->vm_flags & vm_flags)) { vma_end_read(vma); goto lock_mmap; @@ -623,7 +686,7 @@ static int __kprobes do_page_fault(unsigned long far, unsigned long esr, goto done; } - fault = __do_page_fault(mm, vma, addr, mm_flags, vm_flags, regs); + fault = __do_page_fault(mm, vma, addr, mm_flags, vm_flags, esr, regs); /* Quick path to respond to signals */ if (fault_signal_pending(fault, regs)) { From patchwork Wed Nov 22 09:42:30 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Brown X-Patchwork-Id: 13464414 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 D3F19C61D9C for ; Wed, 22 Nov 2023 09:45:25 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 63DC66B05B2; Wed, 22 Nov 2023 04:45:25 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 5ECB46B05B3; Wed, 22 Nov 2023 04:45:25 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 468B16B05B4; Wed, 22 Nov 2023 04:45:25 -0500 (EST) 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 3227F6B05B2 for ; Wed, 22 Nov 2023 04:45:25 -0500 (EST) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 0B9231A06FA for ; Wed, 22 Nov 2023 09:45:25 +0000 (UTC) X-FDA: 81485107410.12.36EAB2D Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf16.hostedemail.com (Postfix) with ESMTP id 25328180012 for ; Wed, 22 Nov 2023 09:45:22 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=HdxWHgkS; spf=pass (imf16.hostedemail.com: domain of broonie@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=broonie@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1700646323; 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=OMVGeJMiw8fMaWxcNSDT95DTRXpFwkThaBAazRFpyyM=; b=Qpx6yGGe82E8PejxR6MewQBIieLWRpA4rMhLunTHOJfNJY5ofa8uvOAYaeh9fICkpp5t/8 UQ2BS9GnJAmA+hcq+CwEt6VAcrUPLO0hxN3rpBFWoubkCp/Px3Qz23YpOLsFvEKmglXS+q QL0Xw6HN5Ef4X43hAJp/qYuhpyijKJQ= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=HdxWHgkS; spf=pass (imf16.hostedemail.com: domain of broonie@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=broonie@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1700646323; a=rsa-sha256; cv=none; b=fEJ8IQJXGvCOxk+B1Hb/y4LN61JbVPIw61moEzJFc4r7eYHH0TXcZAuqGPXIjh6MIOexJ8 QPlnkjj2z0q4SmZNHlSbJ10LORgRQS8llsVNhL87OOwf0nTjzv5BA/BuGu0dLxqkC2yWQq EOYLJxlublc7wsvNMRX9psVhAStzP+Y= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 44F1461B74; Wed, 22 Nov 2023 09:45:22 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3C129C433CB; Wed, 22 Nov 2023 09:45:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1700646322; bh=xkbuSU3P8z04kIlYSUCRz7O7y1/tSlbAvMwotpjCBPk=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=HdxWHgkSs4SFLRZkLxTS8ApdtxYDE6ESk+4DkzcS0pHqww1toP/eM7xIeE51YKf7C EzB1y2Os8mFSF+wDsb+uqyAg/66WmovXFrqOoptTDEKFyb1gySVSXEoCi5vrCGSwiI eRbT7RoAhPw/YjlpIgZEN8fB1asMEPGD85s5jEW827GiRPesCDVkdMjCuO9lFOZsEf iiD8sK9QKZ8u9RCc7VI0dR7/dDfYEX031t1GbBgRAnjSjFQswZxxbb+FbpQK5yz43v wH5hwTd93/S6DZPtfY3i4+PBSiRrrHqm2TL3rXgRSuvKAMry3V4l9nqY3YN6nfIL6O u7krLo9/TeqsQ== From: Mark Brown Date: Wed, 22 Nov 2023 09:42:30 +0000 Subject: [PATCH v7 20/39] arm64/gcs: Context switch GCS state for EL0 MIME-Version: 1.0 Message-Id: <20231122-arm64-gcs-v7-20-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=6699; i=broonie@kernel.org; h=from:subject:message-id; bh=xkbuSU3P8z04kIlYSUCRz7O7y1/tSlbAvMwotpjCBPk=; b=owGbwMvMwMWocq27KDak/QLjabUkhtTYs6zTqwzUVtu96LM3b9AszJB6vyrzRWA+m/qdvhb9e u2mPQs7GY1ZGBi5GGTFFFnWPstYlR4usXX+o/mvYAaxMoFMYeDiFICJ2B9l/8Pp6iqWpimsUvR4 bUSxv+CVpd/X8V9P8ZV25uqddOa655a5eb+7VuVyPy9PSfQ1dY2Rz9RpNapJ5JjX9Fn1tZqbfGZ 5kJlmofvZqxzV86vzp7gz/p9WsobfUmKG2QWjI2zKyh89K8587T4X+3apRG7wzW13s2sOKy7I1k /vMVogkX2j59CRJc6m6guEvY5+lSrv3HfWJ7pzuc5135zCLddiY8sz/sZlNypdOhF5pUz6o9fDY xP6U2/wPhOfaamiYDPHf+3sriO1J+fLl1ffOLyBy5vpHH+wv8fuDzozzrGl735+wGR9eb3hlssb r//8/qul1mVj0fwG/z2ph7/WT3iyLjRyXxzfPMGy/GnzAA== X-Developer-Key: i=broonie@kernel.org; a=openpgp; fpr=3F2568AAC26998F9E813A1C5C3F436CA30F5D8EB X-Rspamd-Queue-Id: 25328180012 X-Rspam-User: X-Stat-Signature: do4hy1kz6h683p4ya3hh6yarj1418af3 X-Rspamd-Server: rspam01 X-HE-Tag: 1700646322-693723 X-HE-Meta: U2FsdGVkX18zPxo1th3hTDPfadkEWLtIXPpWFZQAREezlAmd9+Btg3kY/8QDXDt7Vzyd/P1XhIRK2by7gW/z3wZ1EakL2CuetouVFXDB807bB6/QFz4qnrPz0GN96MpuVNX5iArnIj4OTquXWHB+P17fCfe7u14mGCMghQUKaKqebUN/tC5vp+FqROL0Owdvsq9bpazpguezzgX4forYspQI64Q1W7nAG3oJy69bdQ8J1Oa5IriUBQAC22M2HIS8X8PMenvQwWt9Dvq4NmMj9s9FwrZbXkoRcyG5tOoHCU8IxR6s+npEsNIA1XB8bL59x4N9KUmgqtXoVegV05UbPYgwhxDQxSxo/OVBZ4W1BDAogVCr5yB49aYuxjqiCoutuWHIzq7QVZ+Xh1LhpuhXWlonwxoNKoPaA/o2/PUWPAZZxBrQxmkTA3rMb8eLSakZl/aLrUiW1HhtqFjHC+AQIKervYp4/L+Mers24S8jZp1EDgX8Q05Har+1xLL+4AE4n680ikTSM3cHQ8VYQundZlAM/xiU7w47caHYpnhn/64hKStY4CquRt/5UTrj7yNxE7DlCfKbbtQxYv6zKm5dZYfYEOIwhIOnehIQ0R8nhF6mLYoqnq4YygE7erESlMXqqP5dkBNYfnHcQy52Ce6VUvZKfXI1ExDgTAu1b4emMF0/htptFS4LPxOfBM1dgYMk3NAsEzN1s8YPg6UyMh3dmGWggr0ykaiL9g8Gmv6ZAeqnXQC7k0pNmZ/GNLG5MvBmKm5RXlhjbC9Hxu5VEHS0fF6OlZ6+ftV4CRfj1hTQVg/KdiJP1ZvivS6KY6MU5oUeLJ7hJ4AX6mFoPs+dzmdA097KVITKEyKOxOWK++FJJMj7wok+aPWAw7ckVDjjhpoxovwIkiKqqg/Em3Yw8FYi1ev/oO+bxCBeYBRk+BUMpKhys2dAkXyddONHX2G0bdpx5baUiVtuU/ksiqImYkG UW/httUy RoAX1GsnCccvxDKwsaqROj5qe6ceMAfcmNg7GR2vtP7oxU6DP+wH7M4ewtAqZpEovyTjMimRfJWahjXjV9Z3pd8PyJQp1hnjI2x40JsKf+wIHhhGo883+zbxVLFD0NWIYOzoNMp+XU3by4tD/O27DTr1Vr9+C3MiC9EC9uZsdl2vmJ95bDj5v+bOiLA8naQ8tsyncoTV6tU3jiloMe4TYZ27eneBHScNgFtqPb02jPJtR57JhU5prejEmO0/MXoSstHnEIB689nDTZZoDF5+1117BTa19Uwi1KoIvxklCauN17K1JFNSNxGEkYD9apY43gedidNqpV7M6FHKVN0EObfDTatGjXuuEql4nEqaJw6nNmOM= 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: There are two registers controlling the GCS state of EL0, GCSPR_EL0 which is the current GCS pointer and GCSCRE0_EL1 which has enable bits for the specific GCS functionality enabled for EL0. Manage these on context switch and process lifetime events, GCS is reset on exec(). Also ensure that any changes to the GCS memory are visible to other PEs and that changes from other PEs are visible on this one by issuing a GCSB DSYNC when moving to or from a thread with GCS. Since the current GCS configuration of a thread will be visible to userspace we store the configuration in the format used with userspace and provide a helper which configures the system register as needed. On systems that support GCS we always allow access to GCSPR_EL0, this facilitates reporting of GCS faults if userspace implements disabling of GCS on error - the GCS can still be discovered and examined even if GCS has been disabled. Signed-off-by: Mark Brown --- arch/arm64/include/asm/gcs.h | 24 ++++++++++++++++ arch/arm64/include/asm/processor.h | 6 ++++ arch/arm64/kernel/process.c | 56 ++++++++++++++++++++++++++++++++++++++ arch/arm64/mm/Makefile | 1 + arch/arm64/mm/gcs.c | 39 ++++++++++++++++++++++++++ 5 files changed, 126 insertions(+) diff --git a/arch/arm64/include/asm/gcs.h b/arch/arm64/include/asm/gcs.h index 7c5e95218db6..04594ef59dad 100644 --- a/arch/arm64/include/asm/gcs.h +++ b/arch/arm64/include/asm/gcs.h @@ -48,4 +48,28 @@ static inline u64 gcsss2(void) return Xt; } +#ifdef CONFIG_ARM64_GCS + +static inline bool task_gcs_el0_enabled(struct task_struct *task) +{ + return current->thread.gcs_el0_mode & PR_SHADOW_STACK_ENABLE; +} + +void gcs_set_el0_mode(struct task_struct *task); +void gcs_free(struct task_struct *task); +void gcs_preserve_current_state(void); + +#else + +static inline bool task_gcs_el0_enabled(struct task_struct *task) +{ + return false; +} + +static inline void gcs_set_el0_mode(struct task_struct *task) { } +static inline void gcs_free(struct task_struct *task) { } +static inline void gcs_preserve_current_state(void) { } + +#endif + #endif diff --git a/arch/arm64/include/asm/processor.h b/arch/arm64/include/asm/processor.h index e5bc54522e71..c28681cf9721 100644 --- a/arch/arm64/include/asm/processor.h +++ b/arch/arm64/include/asm/processor.h @@ -179,6 +179,12 @@ struct thread_struct { u64 sctlr_user; u64 svcr; u64 tpidr2_el0; +#ifdef CONFIG_ARM64_GCS + unsigned int gcs_el0_mode; + u64 gcspr_el0; + u64 gcs_base; + u64 gcs_size; +#endif }; static inline unsigned int thread_get_vl(struct thread_struct *thread, diff --git a/arch/arm64/kernel/process.c b/arch/arm64/kernel/process.c index 7387b68c745b..fd80b43c2969 100644 --- a/arch/arm64/kernel/process.c +++ b/arch/arm64/kernel/process.c @@ -48,6 +48,7 @@ #include #include #include +#include #include #include #include @@ -271,12 +272,32 @@ static void flush_tagged_addr_state(void) clear_thread_flag(TIF_TAGGED_ADDR); } +#ifdef CONFIG_ARM64_GCS + +static void flush_gcs(void) +{ + if (!system_supports_gcs()) + return; + + gcs_free(current); + current->thread.gcs_el0_mode = 0; + write_sysreg_s(0, SYS_GCSCRE0_EL1); + write_sysreg_s(0, SYS_GCSPR_EL0); +} + +#else + +static void flush_gcs(void) { } + +#endif + void flush_thread(void) { fpsimd_flush_thread(); tls_thread_flush(); flush_ptrace_hw_breakpoint(current); flush_tagged_addr_state(); + flush_gcs(); } void arch_release_task_struct(struct task_struct *tsk) @@ -474,6 +495,40 @@ static void entry_task_switch(struct task_struct *next) __this_cpu_write(__entry_task, next); } +#ifdef CONFIG_ARM64_GCS + +void gcs_preserve_current_state(void) +{ + if (task_gcs_el0_enabled(current)) + current->thread.gcspr_el0 = read_sysreg_s(SYS_GCSPR_EL0); +} + +static void gcs_thread_switch(struct task_struct *next) +{ + if (!system_supports_gcs()) + return; + + gcs_preserve_current_state(); + + gcs_set_el0_mode(next); + write_sysreg_s(next->thread.gcspr_el0, SYS_GCSPR_EL0); + + /* + * Ensure that GCS changes are observable by/from other PEs in + * case of migration. + */ + if (task_gcs_el0_enabled(current) || task_gcs_el0_enabled(next)) + gcsb_dsync(); +} + +#else + +static void gcs_thread_switch(struct task_struct *next) +{ +} + +#endif + /* * ARM erratum 1418040 handling, affecting the 32bit view of CNTVCT. * Ensure access is disabled when switching to a 32bit task, ensure @@ -533,6 +588,7 @@ struct task_struct *__switch_to(struct task_struct *prev, ssbs_thread_switch(next); erratum_1418040_thread_switch(next); ptrauth_thread_switch_user(next); + gcs_thread_switch(next); /* * Complete any pending TLB or cache maintenance on this CPU in case diff --git a/arch/arm64/mm/Makefile b/arch/arm64/mm/Makefile index dbd1bc95967d..4e7cb2f02999 100644 --- a/arch/arm64/mm/Makefile +++ b/arch/arm64/mm/Makefile @@ -10,6 +10,7 @@ obj-$(CONFIG_TRANS_TABLE) += trans_pgd.o obj-$(CONFIG_TRANS_TABLE) += trans_pgd-asm.o obj-$(CONFIG_DEBUG_VIRTUAL) += physaddr.o obj-$(CONFIG_ARM64_MTE) += mteswap.o +obj-$(CONFIG_ARM64_GCS) += gcs.o KASAN_SANITIZE_physaddr.o += n obj-$(CONFIG_KASAN) += kasan_init.o diff --git a/arch/arm64/mm/gcs.c b/arch/arm64/mm/gcs.c new file mode 100644 index 000000000000..b0a67efc522b --- /dev/null +++ b/arch/arm64/mm/gcs.c @@ -0,0 +1,39 @@ +// SPDX-License-Identifier: GPL-2.0-only + +#include +#include +#include +#include + +#include +#include + +/* + * Apply the GCS mode configured for the specified task to the + * hardware. + */ +void gcs_set_el0_mode(struct task_struct *task) +{ + u64 gcscre0_el1 = GCSCRE0_EL1_nTR; + + if (task->thread.gcs_el0_mode & PR_SHADOW_STACK_ENABLE) + gcscre0_el1 |= GCSCRE0_EL1_RVCHKEN | GCSCRE0_EL1_PCRSEL; + + if (task->thread.gcs_el0_mode & PR_SHADOW_STACK_WRITE) + gcscre0_el1 |= GCSCRE0_EL1_STREn; + + if (task->thread.gcs_el0_mode & PR_SHADOW_STACK_PUSH) + gcscre0_el1 |= GCSCRE0_EL1_PUSHMEn; + + write_sysreg_s(gcscre0_el1, SYS_GCSCRE0_EL1); +} + +void gcs_free(struct task_struct *task) +{ + if (task->thread.gcs_base) + vm_munmap(task->thread.gcs_base, task->thread.gcs_size); + + task->thread.gcspr_el0 = 0; + task->thread.gcs_base = 0; + task->thread.gcs_size = 0; +} From patchwork Wed Nov 22 09:42:31 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Brown X-Patchwork-Id: 13464415 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 7F531C61D9C for ; Wed, 22 Nov 2023 09:45:36 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1D08C6B05B4; Wed, 22 Nov 2023 04:45:36 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 181236B05B5; Wed, 22 Nov 2023 04:45:36 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 048B26B05B6; Wed, 22 Nov 2023 04:45:35 -0500 (EST) 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 E97CE6B05B4 for ; Wed, 22 Nov 2023 04:45:35 -0500 (EST) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id B9981805C0 for ; Wed, 22 Nov 2023 09:45:35 +0000 (UTC) X-FDA: 81485107830.26.1E918A8 Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by imf21.hostedemail.com (Postfix) with ESMTP id 473151C0010 for ; Wed, 22 Nov 2023 09:45:32 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b="J/t21EOt"; spf=pass (imf21.hostedemail.com: domain of broonie@kernel.org designates 145.40.73.55 as permitted sender) smtp.mailfrom=broonie@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1700646334; 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=vPP0ivvZvNMMnbhuwiLOgwkFhM81YmO5+/+Uo6KwZAo=; b=Cwd5CAfKlS4ex0T4MMzEcFbfIIgvonlLmA/PlIgRtKcO2OnEOk1y9nyeqf8Jj+GWXNNkOp KQC/IgV1QAihraTXRmI78gYxDqcsVzj/FmmrFDkMHoyTU4xtkwzHIw0/BzdJVGsVrDUxrr Reyv6xc6KD8X/g32gf2qm96sn36zczo= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1700646334; a=rsa-sha256; cv=none; b=Nzqh+BCMi/u/U8vuivaOJ4/wRNbDhxWUkAkY2FejEycfZcd6/nGhNbzAL3Ilc1pYjBzgwJ DmFWvv7wNGy+361IK+qlPm4eObHP4+B4HdoM2TmfCIrK5AKPjnnRLUrcfeBJMe7CagnZBq xf4FPoHUKRTFDadgp96d/E/F+jq/4HY= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b="J/t21EOt"; spf=pass (imf21.hostedemail.com: domain of broonie@kernel.org designates 145.40.73.55 as permitted sender) smtp.mailfrom=broonie@kernel.org; dmarc=pass (policy=none) header.from=kernel.org Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sin.source.kernel.org (Postfix) with ESMTP id 28E91CE1EA4; Wed, 22 Nov 2023 09:45:30 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8BBB3C433C7; Wed, 22 Nov 2023 09:45:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1700646329; bh=p7vhwNE+CJYbXpRAkTVE4uNTVeLjV9vl6fV2mHCV5l8=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=J/t21EOtX9LkHO6S2pMhuc938Wc3YdgkYmn0ooGJB+tFn7kn2xaS1fJa9v/tWKi43 ZTDDKJiFXArP+8mEXvmx2sBLcyO6KjFUwilZJ9QSpTmZBHex9DHtV7uGhhjFfTOdHA BFtt+5Y6vqCpuXIYKBUJbZqMwDI8e9aBheS5w/j6wc1DsyC3Z2ID5HtMpFHCoHTcJo rWLQurPTEuIvboKnpuVymwk7QeZNeC8YHWb609HHt82+32TM1oD3/3qoKdGeGwAAaa xy2l0tOR5Nb7hVNhSMufPGx6/Ar5BoHrwMGH9nsGTb2eLlGyS1xBLrlgKhaomAQEKx tYJH4Vzj89TFg== From: Mark Brown Date: Wed, 22 Nov 2023 09:42:31 +0000 Subject: [PATCH v7 21/39] arm64/gcs: Allocate a new GCS for threads with GCS enabled MIME-Version: 1.0 Message-Id: <20231122-arm64-gcs-v7-21-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=5979; i=broonie@kernel.org; h=from:subject:message-id; bh=p7vhwNE+CJYbXpRAkTVE4uNTVeLjV9vl6fV2mHCV5l8=; b=owEBbQGS/pANAwAKASTWi3JdVIfQAcsmYgBlXc0GtL0+c11OyZaFaNeVlqO8pwcT9Q3SQtGvl DiVL+0NZkSJATMEAAEKAB0WIQSt5miqZ1cYtZ/in+ok1otyXVSH0AUCZV3NBgAKCRAk1otyXVSH 0I5ZB/9ZoJk5A/i0hsPYxTWiFmHCmO/wYkYfWs9TeOH7TaQylRIdkTSuSPmzgHhApbE5QeBZyaU uS997l8TNA33lJJ2js6ybFa7dhO+PFXo/WWqrdncps5Mt7P8IC0Zd/Z4jyBPVQumNeLPc2oJD5D uW83f704kQiGeBYcxmPTNph0Iy2NoFCK2tjIHAQ/zeTosrqitio0MFnrAvbvWNl5t/K3F67Tp6B Bf7jfpqYvIL/b83Z9wbP3/iID7f1dllpQQH5JL/Ig4/pVRu10RPXKm8ia5gAOIBI4b2tXMldcdb 2y5Sqo6qdVr9SvMj0mxKxslcvS7VqlDB4ZvxXeja7O8Ko7EQ X-Developer-Key: i=broonie@kernel.org; a=openpgp; fpr=3F2568AAC26998F9E813A1C5C3F436CA30F5D8EB X-Rspamd-Queue-Id: 473151C0010 X-Rspam-User: X-Stat-Signature: mg8rxhz633o1yg8bfzxuf5t543e41q3m X-Rspamd-Server: rspam03 X-HE-Tag: 1700646332-769595 X-HE-Meta: U2FsdGVkX1+qDaXuffGL8TxLEITANAPqH/W2Ny2eMrWmVo0pznO0i9bpO9/n6Bcv2S1YinDeM/bVDZMLdWUPKzzZmtCB73KWCPBEKNQBjbX5cOWIvMCnjqDJ9by41qdirsQ1dk2SRm/XMcT7Gl8pT//ztD0QQp0++2NRpOxm8g6ut3R7B7MQT5XaynrBiYCTLedaWYGoY6GiV7t35xdJFIuWIy51M7dEFMass6+qNmTK8ZYBj9KrXC+0NEKP7+iumaauPanq9kv5yjw/Rdey0VDZNOwtBDARXbIPbi6MyiQyI4VMoyRT42nNyyViEaCLoO9Yg6LIMSQnffcwDEIGKj+DeueWNNP/WXajYmHbj8uJ/hk6CoPAbhKpVgBj0WTbt0y2qqeQjADatN3ZB7NKXIg2jK0RGRoNepmzFEaJ8LgOysWDFDgWDpJAACqgPXV+k0iv0nQdTl2msoVtbjiZQkZNeSPbVfoTfAJ7wu+mWApb9m3/dPxEECAIRrRm2j5QEfci3YWW0gBo09nLoXYiRcaxH3TS/lQipJoqSHqKS4868K4fkubqWcImfLorTV7rjWCPBTPc6jfcQVGDfhrlWqZXUUg/EBzLIsMed5bVNOwdQWUgxso29tQjAw0DfA2LdDScA76+3cQM3L+985Ai/k2M0eSMsJO/QjAop298I1F/6aRYqtmxEuEgKYPqYaQn8Mlt6RBDpGrh3n/DgAR2pliwFL1XEoAE2Re6IbWvewH9nlFuYWztvDQnoWt6pByA7mHKHIvkY0v9UJFfEc8vr1xB/cCt4n4lwvd9vbTZqFHIN2+xvgD5FEfP/btzOUOIKY4vJxwWwsZ8FllFF0wTazMPJp84CF1Dtl15SiCLNkhyoHjcf2ZluCF/LNnqJVQGkwWuOu1S8qUWRLhCtGkm+z279muuvwgSC0rHiD61KttMYcy9sezwU0CFzJnj9sCxSRU4wSbhooOZG87sm7W ddMY9gON 9vyMwdIYU+nFhrBwTutSjpdu0WVh1gKJMCh7c/w5rIJN0Ur4t9ncai0VUfnqCK6DvzKm3UXH6EyxpaEm6VB89sDNprFRulTk6hvmJYylR4b1z3cBvQY8jsNBFUvxb+1bpzQd31NiMNWrN1jeV+JAvZNLMdOOUkCT7frKkWISLYsQzyOxrST9mhhVAPlg0o3TVe4mJ8kS5XLCmgMKtOUEaytE+BlOANjZ4Lxu4NGtoNuRSIPazyP0A6O6c8otZvxogACA30jZY0h4SXXGExSTqNq6EfvSU0VhLLyhFGKctovvCTxdbQOVEueh6ql/emVWwnRrK 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: When a new thread is created by a thread with GCS enabled the GCS needs to be specified along with the regular stack. clone3() has been extended to support this case, allowing userspace to explicitly request the size for the GCS to be created, but plain clone() is not extensible and existing clone3() users will not specify a size. For compatibility with these cases and also x86 (which did not initially implement clone3() support for shadow stacks) if no GCS is specified we will allocate one thread so when a thread is created which has GCS enabled allocate one for it. We follow the extensively discussed x86 implementation and allocate min(RLIMIT_STACK, 4G). Since the GCS only stores the call stack and not any variables this should be more than sufficient for most applications. GCSs allocated via this mechanism then it will be freed when the thread exits. Signed-off-by: Mark Brown --- arch/arm64/include/asm/gcs.h | 9 ++++++ arch/arm64/kernel/process.c | 25 +++++++++++++++ arch/arm64/mm/gcs.c | 76 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 110 insertions(+) diff --git a/arch/arm64/include/asm/gcs.h b/arch/arm64/include/asm/gcs.h index 04594ef59dad..c1f274fdb9c0 100644 --- a/arch/arm64/include/asm/gcs.h +++ b/arch/arm64/include/asm/gcs.h @@ -8,6 +8,8 @@ #include #include +struct kernel_clone_args; + static inline void gcsb_dsync(void) { asm volatile(".inst 0xd503227f" : : : "memory"); @@ -58,6 +60,8 @@ static inline bool task_gcs_el0_enabled(struct task_struct *task) void gcs_set_el0_mode(struct task_struct *task); void gcs_free(struct task_struct *task); void gcs_preserve_current_state(void); +unsigned long gcs_alloc_thread_stack(struct task_struct *tsk, + const struct kernel_clone_args *args); #else @@ -69,6 +73,11 @@ static inline bool task_gcs_el0_enabled(struct task_struct *task) static inline void gcs_set_el0_mode(struct task_struct *task) { } static inline void gcs_free(struct task_struct *task) { } static inline void gcs_preserve_current_state(void) { } +static inline unsigned long gcs_alloc_thread_stack(struct task_struct *tsk, + const struct kernel_clone_args *args) +{ + return -ENOTSUPP; +} #endif diff --git a/arch/arm64/kernel/process.c b/arch/arm64/kernel/process.c index fd80b43c2969..588d2bc4b329 100644 --- a/arch/arm64/kernel/process.c +++ b/arch/arm64/kernel/process.c @@ -285,9 +285,29 @@ static void flush_gcs(void) write_sysreg_s(0, SYS_GCSPR_EL0); } +static int copy_thread_gcs(struct task_struct *p, + const struct kernel_clone_args *args) +{ + unsigned long gcs; + + gcs = gcs_alloc_thread_stack(p, args); + if (IS_ERR_VALUE(gcs)) + return PTR_ERR((void *)gcs); + + /* Ensure the current state of the GCS is seen by CoW */ + gcsb_dsync(); + + return 0; +} + #else static void flush_gcs(void) { } +static int copy_thread_gcs(struct task_struct *p, + const struct kernel_clone_args *args) +{ + return 0; +} #endif @@ -369,6 +389,7 @@ int copy_thread(struct task_struct *p, const struct kernel_clone_args *args) unsigned long stack_start = args->stack; unsigned long tls = args->tls; struct pt_regs *childregs = task_pt_regs(p); + int ret; memset(&p->thread.cpu_context, 0, sizeof(struct cpu_context)); @@ -410,6 +431,10 @@ int copy_thread(struct task_struct *p, const struct kernel_clone_args *args) p->thread.uw.tp_value = tls; p->thread.tpidr2_el0 = 0; } + + ret = copy_thread_gcs(p, args); + if (ret != 0) + return ret; } else { /* * A kthread has no context to ERET to, so ensure any buggy diff --git a/arch/arm64/mm/gcs.c b/arch/arm64/mm/gcs.c index b0a67efc522b..0d4881173b5f 100644 --- a/arch/arm64/mm/gcs.c +++ b/arch/arm64/mm/gcs.c @@ -8,6 +8,82 @@ #include #include +static unsigned long alloc_gcs(unsigned long addr, unsigned long size, + unsigned long token_offset, bool set_res_tok) +{ + int flags = MAP_ANONYMOUS | MAP_PRIVATE; + struct mm_struct *mm = current->mm; + unsigned long mapped_addr, unused; + + if (addr) + flags |= MAP_FIXED_NOREPLACE; + + mmap_write_lock(mm); + mapped_addr = do_mmap(NULL, addr, size, PROT_READ | PROT_WRITE, flags, + VM_SHADOW_STACK, 0, &unused, NULL); + mmap_write_unlock(mm); + + return mapped_addr; +} + +static unsigned long gcs_size(unsigned long size) +{ + if (size) + return PAGE_ALIGN(size); + + /* Allocate RLIMIT_STACK/2 with limits of PAGE_SIZE..2G */ + size = PAGE_ALIGN(min_t(unsigned long long, + rlimit(RLIMIT_STACK) / 2, SZ_2G)); + return max(PAGE_SIZE, size); +} + +unsigned long gcs_alloc_thread_stack(struct task_struct *tsk, + const struct kernel_clone_args *args) +{ + unsigned long addr, size; + + /* If the user specified a GCS use it. */ + if (args->shadow_stack_size) { + if (!system_supports_gcs()) + return (unsigned long)ERR_PTR(-EINVAL); + + size = args->shadow_stack_size; + + if (size < 8) + return (unsigned long)ERR_PTR(-EINVAL); + + } else { + /* + * Otherwise fall back to legacy clone() support and + * implicitly allocate a GCS if we need a new one. + */ + + if (!system_supports_gcs()) + return 0; + + if (!task_gcs_el0_enabled(tsk)) + return 0; + + if ((args->flags & (CLONE_VFORK | CLONE_VM)) != CLONE_VM) { + tsk->thread.gcspr_el0 = read_sysreg_s(SYS_GCSPR_EL0); + return 0; + } + + size = args->stack_size; + } + + size = gcs_size(size); + addr = alloc_gcs(0, size, 0, 0); + if (IS_ERR_VALUE(addr)) + return addr; + + tsk->thread.gcs_base = addr; + tsk->thread.gcs_size = size; + tsk->thread.gcspr_el0 = addr + size - sizeof(u64); + + return addr; +} + /* * Apply the GCS mode configured for the specified task to the * hardware. From patchwork Wed Nov 22 09:42:32 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Brown X-Patchwork-Id: 13464416 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 9BDCCC61D9B for ; Wed, 22 Nov 2023 09:45:40 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3F32F6B05B6; Wed, 22 Nov 2023 04:45:40 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 3A3826B05B7; Wed, 22 Nov 2023 04:45:40 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 26C056B05B8; Wed, 22 Nov 2023 04:45:40 -0500 (EST) 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 124166B05B6 for ; Wed, 22 Nov 2023 04:45:40 -0500 (EST) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id E1C5B80556 for ; Wed, 22 Nov 2023 09:45:39 +0000 (UTC) X-FDA: 81485107998.24.867D020 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf25.hostedemail.com (Postfix) with ESMTP id 127F1A0011 for ; Wed, 22 Nov 2023 09:45:37 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=EpTv0RoK; spf=pass (imf25.hostedemail.com: domain of broonie@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=broonie@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1700646338; 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=Sfm+zGm8gQVD9hEtJsdOIJdLxVtR9XcyUBct7dLRVTw=; b=pihSzIxYOOSiV1XPlviQ3FPNy5+FCx9waQntfqDbfEFWZUUNxJZAs56eNpBGVMfqevzkXT lFSj41JEcHe3hiJrMpj4995Z7HIlOQAb3zbWtSF4ATHDGmSm4ZoHeHvTQOizBcJLbEyCDC FqYkz0hyPZ9ldsuIV0imerLV1h6OGho= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1700646338; a=rsa-sha256; cv=none; b=qrT2tE/bk/Hsyprgoj1FMi2ZC0re1ef0sKXPRcx4fByEK1j9iO2CD6ULvSXLsHmrboH9C4 EIqoBhJDDcFUDVjOSbz8TSeZUj90rISM8F6ay8ZDL+fCQE7Bx9v8pZAIDjRfMjosyaxvod yEblQc5PIabnpXyvc1E1NNfzutOm764= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=EpTv0RoK; spf=pass (imf25.hostedemail.com: domain of broonie@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=broonie@kernel.org; dmarc=pass (policy=none) header.from=kernel.org Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 3AA0361CCC; Wed, 22 Nov 2023 09:45:37 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id D60BAC433C9; Wed, 22 Nov 2023 09:45:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1700646336; bh=x72fc+VK4oqJgPWdK4g1pRbvmyi52iEXFShOF15RYHs=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=EpTv0RoKA17/YgCmkJvV8UuVMOQQW4ty21K7jgH8A/zDrtvbdpHZTrkYfvOzn+Jrc PH9eCQwcx73OSxmNFKy6ELZOWGHDJDHp0DJAay+QyWiC3TQTN3uXMeyeM6Ok7/BnDh QqagZTyt/OwGcuBf3c5ln+69zT4wXopP+IhTN341xkPTlZ+qaoT1p+o9IVqjmGFiCh Q2mpgFpunOaHVub8T0um/ENHsTvoE9G7IVDz4lazbW0UPSQxbUJXEMd6NA/7iB/2k6 rs4/3VYRT42yIe5Zv6PcFG1LwXPMzzULY2KwPyMGGSbntuGWCJu60ncwjmLo15AQCa BBo0vEZz1QLog== From: Mark Brown Date: Wed, 22 Nov 2023 09:42:32 +0000 Subject: [PATCH v7 22/39] arm64/gcs: Implement shadow stack prctl() interface MIME-Version: 1.0 Message-Id: <20231122-arm64-gcs-v7-22-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=5675; i=broonie@kernel.org; h=from:subject:message-id; bh=x72fc+VK4oqJgPWdK4g1pRbvmyi52iEXFShOF15RYHs=; b=owEBbQGS/pANAwAKASTWi3JdVIfQAcsmYgBlXc0HgY785xSQh4xaC5GmEKN/hUf0aNGwZWzsN J+sB9i9FraJATMEAAEKAB0WIQSt5miqZ1cYtZ/in+ok1otyXVSH0AUCZV3NBwAKCRAk1otyXVSH 0AVEB/9YxdfnJWe4XxsXvdgrnhh9yBgTzfPpy2Cfm+j5T5DjzF64dE8vzpwqEryRhKs6H0GDUgS f3Jbjko6vejw/6rhQCpSZ8l5czU1WZGMsl/06vE7gRABuHVq5MwmtZvHXeUsAHT0lRk7eSUotPZ C/ZwzmQLUgbPJovfzpfOAsLW5P0b/EapVNA2oFIdHZtMM//qhB7pyw6q51/cZ90L6yAuUYN7fyS 0pJ8ll7w0v7j6BQ5KvRe+p08dRCuinUdbs2R5crhSIdKxhW15xbYoFkFVL33HR2qasw0LE/AlOu /tAyjhhLeY0t8V+jk4kMFBDWkGOb4SlNjRsZuVJeFVbOOcUy X-Developer-Key: i=broonie@kernel.org; a=openpgp; fpr=3F2568AAC26998F9E813A1C5C3F436CA30F5D8EB X-Stat-Signature: 1xdrmu4qqrtpkaxgcmktjfki6fziw6gi X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 127F1A0011 X-Rspam-User: X-HE-Tag: 1700646337-384260 X-HE-Meta: U2FsdGVkX188+qEoSGx0G92dB/Z28J8WsGTwZ6QA6k+3ycq2ym2vEWcszWq2Zc4OSpzihqKE7APR9cgOvQB92lP2K0MC4M9m8hyiEo9oKvkHezkcH9i9mNysewNVpjZqao6h8LLRoZ6N9bJXUDGX9JQjEuW9YWvU/w4I2vT5BeYTPG2oLWCzZPPTNChK2gssMa/g6/dougt24R3Rn3HWI+NRPuGrkyjxTG5BJp88qb6XpCFuBZ29Snuv9iVGziI4URgWU1QDF10g0s00M2v3YuoVTRtxp3MPh07QmAkvZwNwlMFZleKu49m63XvyIRQKoYCxh9tGErEyFvVnTvcT7/PQNcYYyx1a4pSDqf25jUJoOuHFLZPTuGjFw5JpyULXb9TMckfoQqsxnAdBV+3nkcUWRDRtvz+Q1KeHOQUCBPn2bifm+OEAQpbdnBk8EMAng5Er5YBexPKDVc1PRVMGaACk3CsclIiP8y+FAX8rvRmEFP7ZRXwThZvvWzWsG0QRrWMRbDQFcGT6UKdXT4/C6Ggt8kXedJG7qmYxqTPIECXEBFZWSvcWptTTXzFsb7t8hBv3EKgPvquqA54Z9ccf1pVaE3hteedOqlqV37QCfXWcXOk53JyqRCX4JHB2a0i31U5LdhAfZeTdZ7VZ6HuKe4tU/RiOgA8O2YfKHoxMv4RhIqJ4rKetCX3lTwFJs3cccyiHaI+oEcFMSLe9H5UN9PQ+V7x9hEQtzoBizqekw6h2IVWFpxWn+xV/yfycKm1LLLbr/AZibJn0AWK4D9CO35V0DSaNQsVEgHP/Xj1JVKo9Pa1tnkVNXIqpM6PZTprmfOfSnqDkI9LeR8r4KLkaEZmcHzV6FrVGncNC1NCWKgOREwS2P0tGtK+BwyKSZ9l8xzijCZvyhbwhoSg2y60OIMkZ54GJEtO2yZHl6tNBOnCSjgiRUjAXImsZTkRth+usJMgRm+2vJg7Aq5jFI3+ oWiog6do 2Wn15Ju/qiFWzkXDzGefktlhgx4aROWcSLxEXTRhcQSOCyxZLUNbBTP0jF2B8vX9Q5UbZCChoJKpWMlsJLkh93SMopSowq6wBADipHHScSVnWDKwwS45xZ3oYS1b7VHmcATxtscsxSQA0D3Ld2Lviw/SwPzGZLgKDrzDtEteiKinPBwl6at4d5U7hap6s2gkzppHYOSieT8JQARqd5UMH3UpM56xfXCYVzRgb/aKru1qPRB1Cibnb1IdTEvwwcCJm6xFAcWHCI/O6q79XTefVm5yAAqN7y190CLkG/+K5b+eR6wGcRIk/FOqmGZQ4ohwQ2FOf 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 the architecture neutral prtctl() interface for setting the shadow stack status, this supports setting and reading the current GCS configuration for the current thread. Userspace can enable basic GCS functionality and additionally also support for GCS pushes and arbatrary GCS stores. It is expected that this prctl() will be called very early in application startup, for example by the dynamic linker, and not subsequently adjusted during normal operation. Users should carefully note that after enabling GCS for a thread GCS will become active with no call stack so it is not normally possible to return from the function that invoked the prctl(). State is stored per thread, enabling GCS for a thread causes a GCS to be allocated for that thread. Userspace may lock the current GCS configuration by specifying PR_SHADOW_STACK_ENABLE_LOCK, this prevents any further changes to the GCS configuration via any means. If GCS is not being enabled then all flags other than _LOCK are ignored, it is not possible to enable stores or pops without enabling GCS. When disabling the GCS we do not free the allocated stack, this allows for inspection of the GCS after disabling as part of fault reporting. Since it is not an expected use case and since it presents some complications in determining what to do with previously initialsed data on the GCS attempts to reenable GCS after this are rejected. This can be revisted if a use case arises. Signed-off-by: Mark Brown --- arch/arm64/include/asm/gcs.h | 22 ++++++++++ arch/arm64/include/asm/processor.h | 1 + arch/arm64/mm/gcs.c | 82 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 105 insertions(+) diff --git a/arch/arm64/include/asm/gcs.h b/arch/arm64/include/asm/gcs.h index c1f274fdb9c0..48c97e63e56a 100644 --- a/arch/arm64/include/asm/gcs.h +++ b/arch/arm64/include/asm/gcs.h @@ -50,6 +50,9 @@ static inline u64 gcsss2(void) return Xt; } +#define PR_SHADOW_STACK_SUPPORTED_STATUS_MASK \ + (PR_SHADOW_STACK_ENABLE | PR_SHADOW_STACK_WRITE | PR_SHADOW_STACK_PUSH) + #ifdef CONFIG_ARM64_GCS static inline bool task_gcs_el0_enabled(struct task_struct *task) @@ -63,6 +66,20 @@ void gcs_preserve_current_state(void); unsigned long gcs_alloc_thread_stack(struct task_struct *tsk, const struct kernel_clone_args *args); +static inline int gcs_check_locked(struct task_struct *task, + unsigned long new_val) +{ + unsigned long cur_val = task->thread.gcs_el0_mode; + + cur_val &= task->thread.gcs_el0_locked; + new_val &= task->thread.gcs_el0_locked; + + if (cur_val != new_val) + return -EBUSY; + + return 0; +} + #else static inline bool task_gcs_el0_enabled(struct task_struct *task) @@ -78,6 +95,11 @@ static inline unsigned long gcs_alloc_thread_stack(struct task_struct *tsk, { return -ENOTSUPP; } +static inline int gcs_check_locked(struct task_struct *task, + unsigned long new_val) +{ + return 0; +} #endif diff --git a/arch/arm64/include/asm/processor.h b/arch/arm64/include/asm/processor.h index c28681cf9721..029d20ea7878 100644 --- a/arch/arm64/include/asm/processor.h +++ b/arch/arm64/include/asm/processor.h @@ -181,6 +181,7 @@ struct thread_struct { u64 tpidr2_el0; #ifdef CONFIG_ARM64_GCS unsigned int gcs_el0_mode; + unsigned int gcs_el0_locked; u64 gcspr_el0; u64 gcs_base; u64 gcs_size; diff --git a/arch/arm64/mm/gcs.c b/arch/arm64/mm/gcs.c index 0d4881173b5f..315bd698de86 100644 --- a/arch/arm64/mm/gcs.c +++ b/arch/arm64/mm/gcs.c @@ -113,3 +113,85 @@ void gcs_free(struct task_struct *task) task->thread.gcs_base = 0; task->thread.gcs_size = 0; } + +int arch_set_shadow_stack_status(struct task_struct *task, unsigned long arg) +{ + unsigned long gcs, size; + int ret; + + if (!system_supports_gcs()) + return -EINVAL; + + if (is_compat_thread(task_thread_info(task))) + return -EINVAL; + + /* Reject unknown flags */ + if (arg & ~PR_SHADOW_STACK_SUPPORTED_STATUS_MASK) + return -EINVAL; + + ret = gcs_check_locked(task, arg); + if (ret != 0) + return ret; + + /* If we are enabling GCS then make sure we have a stack */ + if (arg & PR_SHADOW_STACK_ENABLE) { + if (!task_gcs_el0_enabled(task)) { + /* Do not allow GCS to be reenabled */ + if (task->thread.gcs_base) + return -EINVAL; + + if (task != current) + return -EBUSY; + + size = gcs_size(0); + gcs = alloc_gcs(task->thread.gcspr_el0, size, + 0, 0); + if (!gcs) + return -ENOMEM; + + task->thread.gcspr_el0 = gcs + size - sizeof(u64); + task->thread.gcs_base = gcs; + task->thread.gcs_size = size; + if (task == current) + write_sysreg_s(task->thread.gcspr_el0, + SYS_GCSPR_EL0); + + } + } + + task->thread.gcs_el0_mode = arg; + if (task == current) + gcs_set_el0_mode(task); + + return 0; +} + +int arch_get_shadow_stack_status(struct task_struct *task, + unsigned long __user *arg) +{ + if (!system_supports_gcs()) + return -EINVAL; + + if (is_compat_thread(task_thread_info(task))) + return -EINVAL; + + return put_user(task->thread.gcs_el0_mode, arg); +} + +int arch_lock_shadow_stack_status(struct task_struct *task, + unsigned long arg) +{ + if (!system_supports_gcs()) + return -EINVAL; + + if (is_compat_thread(task_thread_info(task))) + return -EINVAL; + + /* + * We support locking unknown bits so applications can prevent + * any changes in a future proof manner. + */ + task->thread.gcs_el0_locked |= arg; + + return 0; +} From patchwork Wed Nov 22 09:42:33 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Brown X-Patchwork-Id: 13464417 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 090BDC61D9B for ; Wed, 22 Nov 2023 09:45:50 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9C0AC6B05B8; Wed, 22 Nov 2023 04:45:49 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 970366B05B9; Wed, 22 Nov 2023 04:45:49 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 85F0C6B05BA; Wed, 22 Nov 2023 04:45:49 -0500 (EST) 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 765E06B05B8 for ; Wed, 22 Nov 2023 04:45:49 -0500 (EST) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 44DF31A011E for ; Wed, 22 Nov 2023 09:45:49 +0000 (UTC) X-FDA: 81485108418.26.81BEB40 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by imf07.hostedemail.com (Postfix) with ESMTP id 4E7C44001A for ; Wed, 22 Nov 2023 09:45:47 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b="K/G00O1W"; spf=pass (imf07.hostedemail.com: domain of broonie@kernel.org designates 145.40.68.75 as permitted sender) smtp.mailfrom=broonie@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1700646347; a=rsa-sha256; cv=none; b=Z7GgKO1WxI82uWoy/cnWLKeqdfPiRw59InKxn0JZNMNT8asXp1JsCdB5/ZgLs7n+Rs5w5b 9I1sNo8zyNuiyQ5OvOB+bQxLFY4V8i0CdNB3D4VtAg3jMPJ/NKzGN+hDkRaPIFYjd0Dzwf 11CLPT+0XI9/cUZO3e33j3IVX7kWibU= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b="K/G00O1W"; spf=pass (imf07.hostedemail.com: domain of broonie@kernel.org designates 145.40.68.75 as permitted sender) smtp.mailfrom=broonie@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1700646347; 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=AyBaOQ3NkTpOcB9oET87q4a6TvzX+i0b1HDg+2aunOo=; b=fYr9PYXeya0VzFE64r4ZrhbkI7/SrV9A4N1IM0UCoEjWewdA5ybi/+OYxTJtkRFAzHJPdF wherPAhSaPFATcGdptLGegnzt2Zq3lMBSt3IdcGlcEvnUb581hrhyE2a2AhoB5AYNeizm+ w5OBWEaX49oiaaasC2WyA6B/zlBZoZE= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by ams.source.kernel.org (Postfix) with ESMTP id E388EB8118A; Wed, 22 Nov 2023 09:45:45 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5FF98C43395; Wed, 22 Nov 2023 09:45:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1700646345; bh=NWVJpdky41RR5z+76XsSxPAX8JBO81FQgE/78Pibz4I=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=K/G00O1WsBVKAEIul9z7M9wwqUCctNvCWPIaRc94+Gvlyf4ztcyShJLjmynNGk+0B 7hq8h2uawUA22gE15dEZ51ZTQ925UaN/8M9vgcTDnOwWQsuUaff39e7A4k0MYaYQei kbeJ26pPSy+RavFC5jiQzP68OxdewcWNTDUx+97/iR+Wod7dBZrpJhL+zFSllgaOU4 tTAwtGqFpeCW/XVRsAzFRAm4U0v25Q+Vt+EYrKl876crUoM8lp3JjUPcemuqRNmigp osHPuhMKVym0baNytxZlIId5GP3XNo+tBA2/HwMdSZQ13zyKoep+bol7KRP3sZl1Y6 5c5nQ4gyCyVog== From: Mark Brown Date: Wed, 22 Nov 2023 09:42:33 +0000 Subject: [PATCH v7 23/39] arm64/mm: Implement map_shadow_stack() MIME-Version: 1.0 Message-Id: <20231122-arm64-gcs-v7-23-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=3103; i=broonie@kernel.org; h=from:subject:message-id; bh=NWVJpdky41RR5z+76XsSxPAX8JBO81FQgE/78Pibz4I=; b=owEBbQGS/pANAwAKASTWi3JdVIfQAcsmYgBlXc0Ip6PqHo/mfAIYxGDeH30hQhlkTVEtnfk4p U/mjwHwk4uJATMEAAEKAB0WIQSt5miqZ1cYtZ/in+ok1otyXVSH0AUCZV3NCAAKCRAk1otyXVSH 0MvzB/9fzWnM1qf2rhce2NWjEOKbFvvKGljLcOVUTHACXD9yQBR9F7owkcySZ9VXjzQbfvkKGXP 9KBhY9vBnEONt/apYyW01/mhfN+uI9680+VgwQRrwPP2yyMoTRFej/ZISyD4X7zqhOohmgBMTls we4TRIcF7x/2ZIRovz3jyjHZW4De+AalqERXlrJXgUEBHNiMQyi97JE+muLMn0kqMhzS0KM0fqo EUqWgO/phoFoU4p6c8gieIGuKOJ8elT3uUiZAeB+kK2rCc7bZbL9koFjnhS4mYcFlRDToQhQpFv GD6HdRG8hNRI9z4hGkH/MB88HcPqkexmwToLVqmqilFiuS8b X-Developer-Key: i=broonie@kernel.org; a=openpgp; fpr=3F2568AAC26998F9E813A1C5C3F436CA30F5D8EB X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: 4E7C44001A X-Stat-Signature: drqxgfxe3djf5kyheyi75rxyjfzo87f6 X-Rspam-User: X-HE-Tag: 1700646347-152028 X-HE-Meta: U2FsdGVkX1/9dAMvkQOzoqS9B3UqVyg3apfDscB1rTJo0YozjrfwUxaCDHmS8N4qKZPyyv0UJnGs8MJPhCIV5qa57BzkaVEKXG0Z7zhpAiLRypR+8ZSfPoPwU16clXOBj3/FrxaWXAX6IiPlBkXVwyi73d8de0AxAIEpewTR1cpbFFetDBI5aC3eW7U0g8Pkvz7JxYkbTDgumojR0Ve8PZN6mSzg986tNVRJI6W4Q/iazcDDXiDK7kT6G2eu/6S339BINMnEW6vzAyjgkc1J/GU7gzred0kvg8pqQdiEvc1ALBJGQWDkqC7NgiIzGDlXn60Gaw/HQG85fheJNQAz9dss6N+pYH1OuY+VpCF9/QJfjFiMF04OZoK7AfspZRF6HnJx4WsFKNtoe2Ji/A0T5YFq8qaSrna+0YlHfCQof6PM4x+Ji8B0Vv92fme0NSF4ua87OhABh95/CKl/EiX7/dR5S2APqRLA32VPxRvKCi8705MHgWgOZP+byjoDTSRSx6OeycVfwpIE04brDNk0znVn5RxavMGXztgh0BTQKD5f5f7SBRVX5aBLyqgWadZA2yOTnKhZzt6pOYYPC0yeTNLBSc2u0i8vBwQsV8T3hEg696LP5KO/9G7Z2lqbhxhqWerHxu66XRqlHqmGEYNZ8NvS36Z6BQoANBcPP7CKWn2ZJgOBcRtEzr35zR2ILHHqsTowzfPim6EGB73ctZQGPTifFKA7lYkiGYH+6vGno7X89bj/oIak2dBKGSNJXh7SOMCzzzhBOdFUSMkbL8lVVzBJ2DmIjrZ7O0oB93BPxWXqCHiqZQ+85lGlmmW1xfxjxL4Juzv8YA0MJOKVdGGsByZ/DMC7wRrs5rrTv4bi9Hu2Zxshvvckm08rDFd/VwiUh4Wi+FwzlOS2HFVSixJy3bjya7ZJybIZ2AvJqSyMUOEiCfLPexRn5c5bUlTKBP65QfZNwMGWZtbuMD+RIuh RvkcsMkP valfbYggGz31IR+vETXCTahQhaoayGoZ0isRmSPG1bHy6Nv5s3YbJq7TtpphAoU1aEQ9hsLb+iq0SJkFCg3Orbt5TnVlL+N3FE+tH2dNY35CYEtmMx0x868OgUCN9EM+QWgRxJpjaHzpViISXQ8emMORkiAq2ZGeTFigfB1Wl+z4/APHFmry0MZNNWK4rvF/lEPYOLZf3cq9A2YsS0lBHVrx//NNkSctWIQSw1lIFzOZ6b8SCNlHAiKYxYK6LXHUShzj8ZvkUVuNC8JPAEV5s1Fx9CS8375D4fXbgO21kYfStMD6KeDjSPjE3x+BltCswY4nF7Y0k1MVO5IPm81FI3GtT+pg93FJP0dXfcEzLlFl8VqE= 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 guarded control stacks 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. Implement this for arm64. Two flags are provided, allowing applications to request that the stack be initialised with a valid cap token at the top of the stack and optionally also an end of stack marker above that. We support requesting an end of stack marker alone but since this is a NULL pointer it is indistinguishable from not initialising anything by itself. Since the x86 code has not yet been rebased to v6.5-rc1 this includes the architecture neutral parts of Rick Edgecmbe's "x86/shstk: Introduce map_shadow_stack syscall". Signed-off-by: Mark Brown --- arch/arm64/mm/gcs.c | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/arch/arm64/mm/gcs.c b/arch/arm64/mm/gcs.c index 315bd698de86..02f8f6046c10 100644 --- a/arch/arm64/mm/gcs.c +++ b/arch/arm64/mm/gcs.c @@ -84,6 +84,67 @@ unsigned long gcs_alloc_thread_stack(struct task_struct *tsk, return addr; } +SYSCALL_DEFINE3(map_shadow_stack, unsigned long, addr, unsigned long, size, unsigned int, flags) +{ + unsigned long alloc_size; + unsigned long __user *cap_ptr; + unsigned long cap_val; + int ret = 0; + int cap_offset; + + if (!system_supports_gcs()) + return -EOPNOTSUPP; + + if (flags & ~(SHADOW_STACK_SET_TOKEN | SHADOW_STACK_SET_MARKER)) + return -EINVAL; + + if (addr && (addr % PAGE_SIZE)) + return -EINVAL; + + if (size == 8 || size % 8) + return -EINVAL; + + /* + * An overflow would result in attempting to write the restore token + * to the wrong location. Not catastrophic, but just return the right + * error code and block it. + */ + alloc_size = PAGE_ALIGN(size); + if (alloc_size < size) + return -EOVERFLOW; + + addr = alloc_gcs(addr, alloc_size, 0, false); + if (IS_ERR_VALUE(addr)) + return addr; + + /* + * Put a cap token at the end of the allocated region so it + * can be switched to. + */ + if (flags & SHADOW_STACK_SET_TOKEN) { + /* Leave an extra empty frame as a top of stack marker? */ + if (flags & SHADOW_STACK_SET_MARKER) + cap_offset = 2; + else + cap_offset = 1; + + cap_ptr = (unsigned long __user *)(addr + size - + (cap_offset * sizeof(unsigned long))); + cap_val = GCS_CAP(cap_ptr); + + put_user_gcs(cap_val, cap_ptr, &ret); + if (ret != 0) { + vm_munmap(addr, size); + return -EFAULT; + } + + /* Ensure the new cap is viaible for GCS */ + gcsb_dsync(); + } + + return addr; +} + /* * Apply the GCS mode configured for the specified task to the * hardware. From patchwork Wed Nov 22 09:42:34 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Brown X-Patchwork-Id: 13464418 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 048F5C61D9B for ; Wed, 22 Nov 2023 09:45:59 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9BFAF6B05BA; Wed, 22 Nov 2023 04:45:58 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 970916B05BB; Wed, 22 Nov 2023 04:45:58 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8383B6B05BC; Wed, 22 Nov 2023 04:45:58 -0500 (EST) 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 737436B05BA for ; Wed, 22 Nov 2023 04:45:58 -0500 (EST) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 509ADB5792 for ; Wed, 22 Nov 2023 09:45:58 +0000 (UTC) X-FDA: 81485108796.13.AB2F491 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf19.hostedemail.com (Postfix) with ESMTP id 80DA81A001B for ; Wed, 22 Nov 2023 09:45:56 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=U4bwvIok; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf19.hostedemail.com: domain of broonie@kernel.org designates 139.178.84.217 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=1700646356; 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=TQXMdHVoTmLA5Q3fomDDtlfqXWGUedrm0qb0+CB2cYg=; b=7cU5ZR07onZo9xVv7RQbqYB3TqxClIiLRGaYsEl98Eb6taA5ZzXySNjbw+E/2pQdtmGceI a6VTAU9Ocj0gAUAPHuxpQh4WCld6AplN0oNt0ddrG4bsIcdW9r8uvst4h91Gcq6OaSsJcK rU4agVXmGJeW6dvaYTcZxhTrwsAXLRk= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=U4bwvIok; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf19.hostedemail.com: domain of broonie@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=broonie@kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1700646356; a=rsa-sha256; cv=none; b=t77MvMpl+tWTRKWmnjq6DMKCRWPbj58yJLlFwssJ4D8KsXAtlIt2ra649X1BapeRlVBkpb 0WXmegVUtlcwfoGS3aYOlO0/DL22Da+sfViuMUrDKFf7Z8pMpI6Vu29dxjBYgeORMsogjH IwNRNf/SCcjcma7mZ8n2WfXG91ZZIiU= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id C109360EA0; Wed, 22 Nov 2023 09:45:55 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id C8647C433C7; Wed, 22 Nov 2023 09:45:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1700646355; bh=JSMKZVTb8W/3p+ZC0+L/d3j69dhWO4dnKUlZwbrdLaI=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=U4bwvIokG8EBOx6vV6+qI7PoxqEUT6THnfDrDq1mSrjFlRQnyk3mldrsV93A4RKJa Ab8+infWBXCxq3HWoxF+Rgts7qb5Vr9GbzdBdzX/QP4QpKQkQ/2QSTT6x6sIwYWirn BLgVCFQxL/e9dcHNNRJqWIJv1XpTSq+v9BRpNLM63JBxA/EL4GPQZ+FSB1yvjDF/tE jRAhvJCYRVY545coOpYO4b6zCpWJhzoa76at3Ag5n/yOKCf71oXBG5yOJ99685CWe+ jTWgch4JvU7mTRWUWV7ZCIyXqDxDJmYJXJkS661Flns6yYwdYHYwa9X38FpoqqL1K+ j2c9XuOOQDc9Q== From: Mark Brown Date: Wed, 22 Nov 2023 09:42:34 +0000 Subject: [PATCH v7 24/39] arm64/signal: Set up and restore the GCS context for signal handlers MIME-Version: 1.0 Message-Id: <20231122-arm64-gcs-v7-24-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=7224; i=broonie@kernel.org; h=from:subject:message-id; bh=JSMKZVTb8W/3p+ZC0+L/d3j69dhWO4dnKUlZwbrdLaI=; b=owEBbQGS/pANAwAKASTWi3JdVIfQAcsmYgBlXc0JNdS11sr/Hjge1PGfZqER4xUAoTpU9oiaf FJ+aEPK/b+JATMEAAEKAB0WIQSt5miqZ1cYtZ/in+ok1otyXVSH0AUCZV3NCQAKCRAk1otyXVSH 0BhgB/9wTBjvZo5XENLoyEPM8e1KArN74mpxK9+U6onYgUXq7Hkos33gETCBKyt4RZgN1HezEtY KP/9WhVc+tZTxnGd6ycY8s+k7os236QxAWzAgt/ivN+S9uYY0GTGs0l9VE9Nz9kHLzFLIy0SttB w2sViEpIqU8+ewskH7BJA6S4qgEFx4XmvWi/sr1O3Opj6EqosGlFI4l2VkWKqWEvBydESKE9HQB 1pTNOoiYU+pTtqjt/ghPMIK0/A/yUfohxaYdvnuvApLKrxOk2qwBGsNyvzT4aNk/qMgbw5OZ/JV aX1ofsQjx23b5yhe9sX8DOrhH8LfB7WGT1kAxw1at/EmK8q0 X-Developer-Key: i=broonie@kernel.org; a=openpgp; fpr=3F2568AAC26998F9E813A1C5C3F436CA30F5D8EB X-Rspamd-Queue-Id: 80DA81A001B X-Rspam-User: X-Rspamd-Server: rspam02 X-Stat-Signature: jkiuafgh1px8wzgpq37jgy5d7dzaj4h5 X-HE-Tag: 1700646356-139813 X-HE-Meta: U2FsdGVkX18SedpflA5IwP/WdSrogzl6IyrZ/EAIES7yjykNr6BckmOENQs0khLJVGvYQA4yMtbGXQKHGQq7/3mx/rgYjQs1JravKQDBAx9YLGHoJ3A4FT8oVQ80uITnWnZmu92GJeTLCsJsjrcXHKS7ZJuRxJDRGSGBCLzL9CunuHaC/aJ+Mu82K93p+dtKz/yyhKm9F34Tibidbq7t5I68WIMua7pmxZht5PplCD+sZ3BQc0GRvyqZBHgjX42IAP5+XITstMZiEzNOxSF5gFmjdsnr6bz92d54/9C2FEr4ODGp7IslOR7hg3OkvHi86MA9oNAJwShqTEIvbmk8fC2iWUh7ywIGocfuh97I1A4GKmuB8wENrpWJGMwhYGdzD+kWpbzZg4bWa4l7BCf5c+Lfp1cWrbmpz60WeXBGbpxjkfYWtJgYqBrmX+G5lj44XKBT1MVfNmwLdIohQi3rgrhWKesE+MBAEkEWuUoLWyyUl+439GOAe2YCjzuAhUhrj7OuJe/CLDwCVe7YCxb5qbsbyoo4pA+dt5c2W4TvIQEouHN254Sut+o9a43LR4dy4SGPbv4+Ox19GkkSfCifBBUrZPHXg9VK4zxTDgoSNkVRZrqLSLt3/+8hFldtCUDkoTnMs+nOeK8wqe9lKWjp05l5Om9KJvHi0IqAstzE7GBeUg/e0ZlNTD+Sdzx7XuVOI1kZDzjV6cXG43uB+AHCwD3uMP6y9pFBQCo3sQeDISowNyKkkRRF9e6m0b6RJuDq9lvlZEHFmseMJc93nGNDB1LRb5quIC6oPqC4uT5tCaBrMNApaMaagLMqbssL3jSO/Y1UBYMCCmBDl+IGbXOSMrefJgz/gyeegHBxTqQnY/GIhct0hh+Ud6pBw4a/LGu5ZGa3xnUvwypAdayA93G0kxVsZjD034kWuP7j6RCcMiVTxzNh+tdic5eiHOVZ/kvtlEdPfzNdllIXWIQPAH8 DLtGhg+Y YZ6vs2Y1BCNGZbAW05v/6GWOBaY1VGbalIWx7YErz/v01UEnWDpfJB5BRfWDdmaR0JkamUfG2tplDUPHvuBdrx+IoLmfkwF+55GRj+oeZeeNwW/79Jk42EwIhqN+XslJICEAFdUT/XmXjh+LACZ4StL4moZYrNtt//yu6hIodTJkaSCM7cL0wPDJKdZyFq+3+zYPfQ7vb9rMZ7kQXoU6MW82mPQZCgzy2di9456r41rQG+sIRiPXn6d54neBPf47UHsqh+fk3aMx16kMKEpOez/2z9R8EdocprYNjUqV9a+bBextiNx9zp5iPRZEAgFNdmaPj 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: When invoking a signal handler we use the GCS configuration and stack for the current thread. Since we implement signal return by calling the signal handler with a return address set up pointing to a trampoline in the vDSO we need to also configure any active GCS for this by pushing a frame for the trampoline onto the GCS. If we do not do this then signal return will generate a GCS protection fault. In order to guard against attempts to bypass GCS protections via signal return we only allow returning with GCSPR_EL0 pointing to an address where it was previously preempted by a signal. We do this by pushing a cap onto the GCS, this takes the form of an architectural GCS cap token with the top bit set and token type of 0 which we add on signal entry and validate and pop off on signal return. The combination of the top bit being set and the token type mean that this can't be interpreted as a valid token or address. Signed-off-by: Mark Brown --- arch/arm64/include/asm/gcs.h | 1 + arch/arm64/kernel/signal.c | 129 +++++++++++++++++++++++++++++++++++++++++-- arch/arm64/mm/gcs.c | 1 + 3 files changed, 126 insertions(+), 5 deletions(-) diff --git a/arch/arm64/include/asm/gcs.h b/arch/arm64/include/asm/gcs.h index 48c97e63e56a..f50660603ecf 100644 --- a/arch/arm64/include/asm/gcs.h +++ b/arch/arm64/include/asm/gcs.h @@ -9,6 +9,7 @@ #include struct kernel_clone_args; +struct ksignal; static inline void gcsb_dsync(void) { diff --git a/arch/arm64/kernel/signal.c b/arch/arm64/kernel/signal.c index 0e8beb3349ea..b5dff91274fb 100644 --- a/arch/arm64/kernel/signal.c +++ b/arch/arm64/kernel/signal.c @@ -25,6 +25,7 @@ #include #include #include +#include #include #include #include @@ -34,6 +35,37 @@ #include #include +#ifdef CONFIG_ARM64_GCS +/* Extra bit set in the address distinguishing a signal cap token. */ +#define GCS_SIGNAL_CAP_FLAG BIT(63) + +#define GCS_SIGNAL_CAP(addr) ((((unsigned long)addr) & GCS_CAP_ADDR_MASK) | \ + GCS_SIGNAL_CAP_FLAG) + +static bool gcs_signal_cap_valid(u64 addr, u64 val) +{ + /* + * The top bit should be set, this is an invalid address for + * EL0 and will only be set for caps created by signals. + */ + if (!(val & GCS_SIGNAL_CAP_FLAG)) + return false; + + /* The rest should be a standard architectural cap token. */ + val &= ~GCS_SIGNAL_CAP_FLAG; + + /* The cap must have the low bits set to a token value */ + if (GCS_CAP_TOKEN(val) != 0) + return false; + + /* The cap must store the VA the cap was stored at */ + if (GCS_CAP_ADDR(addr) != GCS_CAP_ADDR(val)) + return false; + + return true; +} +#endif + /* * Do a signal return; undo the signal stack. These are aligned to 128-bit. */ @@ -815,6 +847,45 @@ static int restore_sigframe(struct pt_regs *regs, return err; } +#ifdef CONFIG_ARM64_GCS +static int gcs_restore_signal(void) +{ + u64 gcspr_el0, cap; + int ret; + + if (!system_supports_gcs()) + return 0; + + if (!(current->thread.gcs_el0_mode & PR_SHADOW_STACK_ENABLE)) + return 0; + + gcspr_el0 = read_sysreg_s(SYS_GCSPR_EL0); + + /* + * GCSPR_EL0 should be pointing at a capped GCS, read the cap... + */ + gcsb_dsync(); + ret = copy_from_user(&cap, (__user void*)gcspr_el0, sizeof(cap)); + if (ret) + return -EFAULT; + + /* + * ...then check that the cap is the actual GCS before + * restoring it. + */ + if (!gcs_signal_cap_valid(gcspr_el0, cap)) + return -EINVAL; + + current->thread.gcspr_el0 = gcspr_el0 + sizeof(cap); + write_sysreg_s(current->thread.gcspr_el0, SYS_GCSPR_EL0); + + return 0; +} + +#else +static int gcs_restore_signal(void) { return 0; } +#endif + SYSCALL_DEFINE0(rt_sigreturn) { struct pt_regs *regs = current_pt_regs(); @@ -841,6 +912,9 @@ SYSCALL_DEFINE0(rt_sigreturn) if (restore_altstack(&frame->uc.uc_stack)) goto badframe; + if (gcs_restore_signal()) + goto badframe; + return regs->regs[0]; badframe: @@ -1071,7 +1145,50 @@ static int get_sigframe(struct rt_sigframe_user_layout *user, return 0; } -static void setup_return(struct pt_regs *regs, struct k_sigaction *ka, +#ifdef CONFIG_ARM64_GCS + +static int gcs_signal_entry(__sigrestore_t sigtramp, struct ksignal *ksig) +{ + unsigned long __user *gcspr_el0; + int ret = 0; + + if (!system_supports_gcs()) + return 0; + + if (!task_gcs_el0_enabled(current)) + return 0; + + /* + * We are entering a signal handler, current register state is + * active. + */ + gcspr_el0 = (unsigned long __user *)read_sysreg_s(SYS_GCSPR_EL0); + + /* + * Push a cap and the GCS entry for the trampoline onto the GCS. + */ + put_user_gcs((unsigned long)sigtramp, gcspr_el0 - 2, &ret); + put_user_gcs(GCS_SIGNAL_CAP(gcspr_el0 - 1), gcspr_el0 - 1, &ret); + if (ret != 0) + return ret; + + gcsb_dsync(); + + gcspr_el0 -= 2; + write_sysreg_s((unsigned long)gcspr_el0, SYS_GCSPR_EL0); + + return 0; +} +#else + +static int gcs_signal_entry(__sigrestore_t sigtramp, struct ksignal *ksig) +{ + return 0; +} + +#endif + +static int setup_return(struct pt_regs *regs, struct ksignal *ksig, struct rt_sigframe_user_layout *user, int usig) { __sigrestore_t sigtramp; @@ -1079,7 +1196,7 @@ static void setup_return(struct pt_regs *regs, struct k_sigaction *ka, regs->regs[0] = usig; regs->sp = (unsigned long)user->sigframe; regs->regs[29] = (unsigned long)&user->next_frame->fp; - regs->pc = (unsigned long)ka->sa.sa_handler; + regs->pc = (unsigned long)ksig->ka.sa.sa_handler; /* * Signal delivery is a (wacky) indirect function call in @@ -1119,12 +1236,14 @@ static void setup_return(struct pt_regs *regs, struct k_sigaction *ka, sme_smstop(); } - if (ka->sa.sa_flags & SA_RESTORER) - sigtramp = ka->sa.sa_restorer; + if (ksig->ka.sa.sa_flags & SA_RESTORER) + sigtramp = ksig->ka.sa.sa_restorer; else sigtramp = VDSO_SYMBOL(current->mm->context.vdso, sigtramp); regs->regs[30] = (unsigned long)sigtramp; + + return gcs_signal_entry(sigtramp, ksig); } static int setup_rt_frame(int usig, struct ksignal *ksig, sigset_t *set, @@ -1147,7 +1266,7 @@ static int setup_rt_frame(int usig, struct ksignal *ksig, sigset_t *set, err |= __save_altstack(&frame->uc.uc_stack, regs->sp); err |= setup_sigframe(&user, regs, set); if (err == 0) { - setup_return(regs, &ksig->ka, &user, usig); + err = setup_return(regs, ksig, &user, usig); if (ksig->ka.sa.sa_flags & SA_SIGINFO) { err |= copy_siginfo_to_user(&frame->info, &ksig->info); regs->regs[1] = (unsigned long)&frame->info; diff --git a/arch/arm64/mm/gcs.c b/arch/arm64/mm/gcs.c index 02f8f6046c10..6f51429c5a46 100644 --- a/arch/arm64/mm/gcs.c +++ b/arch/arm64/mm/gcs.c @@ -6,6 +6,7 @@ #include #include +#include #include static unsigned long alloc_gcs(unsigned long addr, unsigned long size, From patchwork Wed Nov 22 09:42:35 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Brown X-Patchwork-Id: 13464420 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 76B64C61D9B for ; Wed, 22 Nov 2023 09:46:26 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 10F9D6B05BE; Wed, 22 Nov 2023 04:46:26 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 0BECB6B05BF; Wed, 22 Nov 2023 04:46:26 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E7AFE6B05C0; Wed, 22 Nov 2023 04:46:25 -0500 (EST) 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 D61EF6B05BE for ; Wed, 22 Nov 2023 04:46:25 -0500 (EST) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id AFDCA1402E7 for ; Wed, 22 Nov 2023 09:46:25 +0000 (UTC) X-FDA: 81485109930.14.3D11928 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by imf26.hostedemail.com (Postfix) with ESMTP id BE471140009 for ; Wed, 22 Nov 2023 09:46:23 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=fKnWI82w; spf=pass (imf26.hostedemail.com: domain of broonie@kernel.org designates 145.40.68.75 as permitted sender) smtp.mailfrom=broonie@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1700646384; a=rsa-sha256; cv=none; b=fhP4tlfGP7JmTzPDCoW642amBIZYvie7rLsoc2ktMsc2GUGOIJY0XdHbyB5HQbbwK1iIEu wZx25kwfsUYW+hNiN4UET0c7CEm9Af31ITyGam7qHIIiEuDpPh6vskHBrcEHXI10Nz9dL+ ff2/lt5zk6iV87LEqjpINvXbmfRyBno= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=fKnWI82w; spf=pass (imf26.hostedemail.com: domain of broonie@kernel.org designates 145.40.68.75 as permitted sender) smtp.mailfrom=broonie@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1700646384; 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=kUdewf5TeEI+0UiBru498KG2Xvb/Y79qEfeBgcbbr8Y=; b=fUcJ4/HKimMSVL0AT03rtRJlM+DLL7Ljf8I8N6ytJZQ7wSyYR+HQfJXWvNA7WrDm/iwCLa Hx9amZVIqdQ1iQn+vG9leVp2ztZHHZX0qgVGOdmFkf5EXh9HH6OVWMDagRNE6LunH+KrDU d4zFPytfCgirmN+fpUx11L4nnMajGIw= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by ams.source.kernel.org (Postfix) with ESMTP id C12F9B81F71; Wed, 22 Nov 2023 09:46:05 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0329AC433C9; Wed, 22 Nov 2023 09:45:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1700646365; bh=OmIISmzme0R4+ZZhAdBzRBNSULPhQIu1MUcvbR0SztM=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=fKnWI82whn0XtZdXkPreiufY8bmPTbg/EhFaJf6C45E2ClZ6Xv2Zhoc7zg0gpoeKF brXoGKtIf3FI21RXrD8eJdIK6zfz+RvJtBibK7z+vO32SdMZw0j02YzIsdu8hFM69t k5vymDvbbKl+So6XOiIp2P1QrhzMC8diEO4SkAXIaJT+8b/RFjhLN8r5ZCcg8ozxox s3ru+GLB8ogDtroFfmq7CVBeUd55pS39+ibUDo4S0B5qsBQA+/XTrb4PzFS9Fcq58F qp9aZp7TCaLSSPxRfKqFXEemhscbubzUQLAS4H3J2bluTM/10+9X4yKtBNgwLSoSrv DEQ1osdYiB55g== From: Mark Brown Date: Wed, 22 Nov 2023 09:42:35 +0000 Subject: [PATCH v7 25/39] arm64/signal: Expose GCS state in signal frames MIME-Version: 1.0 Message-Id: <20231122-arm64-gcs-v7-25-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=5977; i=broonie@kernel.org; h=from:subject:message-id; bh=OmIISmzme0R4+ZZhAdBzRBNSULPhQIu1MUcvbR0SztM=; b=owEBbQGS/pANAwAKASTWi3JdVIfQAcsmYgBlXc0JHIYxL2UrB1tx7MSl6aRe7ZhUp/W9tRbtj gsL6qbyLg6JATMEAAEKAB0WIQSt5miqZ1cYtZ/in+ok1otyXVSH0AUCZV3NCQAKCRAk1otyXVSH 0ItlB/97EBZweJSTZu8YX5Q8kOJfn4Y40tfmiBjCNZTMEzx/rxeR5DJ2uOALUItLzFzYwbr7xXt wlujHSBFfUEgz0xA9ogYtQECmdVjCE/1BTflw4B4J9zHnKw44v3FiJJJ4dLxTsH7E2+7s250vqt YKDkAz/HyNgSwVgfXy1zwlwovNqebe6+TENHaZPYUSn5eb5Yfm/ZX1+3AZQ35aDuvaAvvw5w0H9 EOdFTsc5+LnecbmGHXGtOqbJCISKXhAQDlvu6GklYlCoex0A0rt9J1XZJz3zxYcZvNiJUG45SgD WDFtGSLC6tHpbS+rC3B+RRoHZwnU+qXse8UzLqUFOyrB50aY X-Developer-Key: i=broonie@kernel.org; a=openpgp; fpr=3F2568AAC26998F9E813A1C5C3F436CA30F5D8EB X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: BE471140009 X-Stat-Signature: 7a6m4s1tdzczi9a7459nx3ht9ra5epew X-Rspam-User: X-HE-Tag: 1700646383-766157 X-HE-Meta: U2FsdGVkX1/EMu1eCsl8dhZ8NVDxSYcqQiRosiP57VkKAM/QqemFgLKRTxjHoaEFhax/Svv/0M8S7NYtT7dk6Y+/BU5WzvLVC6UB9v8QC4JPvKaofNAnzxj104ebx/2IGM1TA3/S3Kxuh4dbuxVXuIW1NuYEduTFg/KR44S2AmDdNCmRPg/DiJo/rDmOmIPa+RrBlMAPJfER3Ne/WSWnQkyAg14mro7OckfXPw9Pb8Gdur+62rYzgpHnkG6cLLcmqSicDJXuPVsAc8/zzIymG4+w4+WEACa4TVtkSefKh6XtWxhrVbaOBnT9P8/So1Ehy1QhV0dXb/102mXCF6AIMVF8CuUc0QsQjjhwZlSjxUK1l90GgRtgGuehLPhjjPXc4NlSBcl6i4m7pg87vDBUrdDjjDY/B1S7whEZxtA+3XKwOCJCi6Crwx9EufgF+V9eQUkhDIGMBc6TLDmPtF7LfVm2GNnaO+2Q6h+EDWIWcNjfIq0DILafdGPa3ujiAyjXfjqwZ1vGTDzVSxB3JZyCbfxMKmglB35io9LuZjQx3aOTf25sEtFlK/PGbgqb0tgzX06R45cb6ShgtfxUhux59zHRHdTxWmI1hFByJ8JxIUH74IruhtPL8dZPum084n6FabLEFWaIyPOcobh/3W1+BAJ9ReDN9Spl45CtvLS1SM4obmnVcFfB+3yDu1MQbg/PIKjlZ/4TUolKUSih20FLa6mh7CN/KfFGYYd4XQqzIukAx8LBaYgNYe/+QZvq+M6UoOVpDkH5jmfkQSRxIiWfSEwql2RT0dw4EyVtBgETn14zeL85t7+ESyN74xsmMMTZ1BCsQeq0ncp3TTF8xRHJo3lPuw8PVelHWaNJOSzxIkbQvKJ35rKGUCoDwrpmJzt6FlObSo1RiEvLTEt2ttxLnavIGwCslcmS6z/i/RHhVbrlkt5P5TybHXR5mopTqmZ1lh0E/N+Jc3mz9/Gihfm EGyKerRx LiaY0qAhi2x0EXnrhtNg0hTDb0CtE7E9kEVwEKgPiCf9heinIHwgygznerMjkb6ah4dEV0v24asZbTTdMexq7stw7YG/Tq/Z/uxooWvr/DbksMyMRirKmDPRCTQddowKmV+h0BYZ/nkyg/KY95yb+2GiEU/1EQL6z4+f+DYu/o5vPfSBr2m6UOUqd9BwoDIqNkPOdWTv9vdzUd8cfbmhPpfX+w6F2QjCi41eFEIEOBONzsfUUyTs6NkRfAN/gSt1U+y8dD1Z4tIXEirtzqYSWF10ZwpQS4NUEEDdgRXy4V7kVnVZE7WMc++S/BKRuLOu6GFjd 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: Add a context for the GCS state and include it in the signal context when running on a system that supports GCS. We reuse the same flags that the prctl() uses to specify which GCS features are enabled and also provide the current GCS pointer. We do not support enabling GCS via signal return, there is a conflict between specifying GCSPR_EL0 and allocation of a new GCS and this is not an ancticipated use case. We also enforce GCS configuration locking on signal return. Signed-off-by: Mark Brown --- arch/arm64/include/uapi/asm/sigcontext.h | 9 +++ arch/arm64/kernel/signal.c | 107 +++++++++++++++++++++++++++++++ 2 files changed, 116 insertions(+) diff --git a/arch/arm64/include/uapi/asm/sigcontext.h b/arch/arm64/include/uapi/asm/sigcontext.h index f23c1dc3f002..7b66d245f2d2 100644 --- a/arch/arm64/include/uapi/asm/sigcontext.h +++ b/arch/arm64/include/uapi/asm/sigcontext.h @@ -168,6 +168,15 @@ struct zt_context { __u16 __reserved[3]; }; +#define GCS_MAGIC 0x47435300 + +struct gcs_context { + struct _aarch64_ctx head; + __u64 gcspr; + __u64 features_enabled; + __u64 reserved; +}; + #endif /* !__ASSEMBLY__ */ #include diff --git a/arch/arm64/kernel/signal.c b/arch/arm64/kernel/signal.c index b5dff91274fb..7ff775495f3e 100644 --- a/arch/arm64/kernel/signal.c +++ b/arch/arm64/kernel/signal.c @@ -88,6 +88,7 @@ struct rt_sigframe_user_layout { unsigned long fpsimd_offset; unsigned long esr_offset; + unsigned long gcs_offset; unsigned long sve_offset; unsigned long tpidr2_offset; unsigned long za_offset; @@ -214,6 +215,8 @@ struct user_ctxs { u32 za_size; struct zt_context __user *zt; u32 zt_size; + struct gcs_context __user *gcs; + u32 gcs_size; }; static int preserve_fpsimd_context(struct fpsimd_context __user *ctx) @@ -606,6 +609,82 @@ extern int restore_zt_context(struct user_ctxs *user); #endif /* ! CONFIG_ARM64_SME */ +#ifdef CONFIG_ARM64_GCS + +static int preserve_gcs_context(struct gcs_context __user *ctx) +{ + int err = 0; + u64 gcspr; + + /* + * We will add a cap token to the frame, include it in the + * GCSPR_EL0 we report to support stack switching via + * sigreturn. + */ + gcs_preserve_current_state(); + gcspr = current->thread.gcspr_el0; + if (task_gcs_el0_enabled(current)) + gcspr -= 8; + + __put_user_error(GCS_MAGIC, &ctx->head.magic, err); + __put_user_error(sizeof(*ctx), &ctx->head.size, err); + __put_user_error(gcspr, &ctx->gcspr, err); + __put_user_error(current->thread.gcs_el0_mode, + &ctx->features_enabled, err); + + return err; +} + +static int restore_gcs_context(struct user_ctxs *user) +{ + u64 gcspr, enabled; + int err = 0; + + if (user->gcs_size != sizeof(*user->gcs)) + return -EINVAL; + + __get_user_error(gcspr, &user->gcs->gcspr, err); + __get_user_error(enabled, &user->gcs->features_enabled, err); + if (err) + return err; + + /* Don't allow unknown modes */ + if (enabled & ~PR_SHADOW_STACK_SUPPORTED_STATUS_MASK) + return -EINVAL; + + err = gcs_check_locked(current, enabled); + if (err != 0) + return err; + + /* Don't allow enabling */ + if (!task_gcs_el0_enabled(current) && + (enabled & PR_SHADOW_STACK_ENABLE)) + return -EINVAL; + + /* If we are disabling disable everything */ + if (!(enabled & PR_SHADOW_STACK_ENABLE)) + enabled = 0; + + current->thread.gcs_el0_mode = enabled; + + /* + * We let userspace set GCSPR_EL0 to anything here, we will + * validate later in gcs_restore_signal(). + */ + current->thread.gcspr_el0 = gcspr; + write_sysreg_s(current->thread.gcspr_el0, SYS_GCSPR_EL0); + + return 0; +} + +#else /* ! CONFIG_ARM64_GCS */ + +/* Turn any non-optimised out attempts to use these into a link error: */ +extern int preserve_gcs_context(void __user *ctx); +extern int restore_gcs_context(struct user_ctxs *user); + +#endif /* ! CONFIG_ARM64_GCS */ + static int parse_user_sigframe(struct user_ctxs *user, struct rt_sigframe __user *sf) { @@ -622,6 +701,7 @@ static int parse_user_sigframe(struct user_ctxs *user, user->tpidr2 = NULL; user->za = NULL; user->zt = NULL; + user->gcs = NULL; if (!IS_ALIGNED((unsigned long)base, 16)) goto invalid; @@ -716,6 +796,17 @@ static int parse_user_sigframe(struct user_ctxs *user, user->zt_size = size; break; + case GCS_MAGIC: + if (!system_supports_gcs()) + goto invalid; + + if (user->gcs) + goto invalid; + + user->gcs = (struct gcs_context __user *)head; + user->gcs_size = size; + break; + case EXTRA_MAGIC: if (have_extra_context) goto invalid; @@ -835,6 +926,9 @@ static int restore_sigframe(struct pt_regs *regs, err = restore_fpsimd_context(&user); } + if (err == 0 && system_supports_gcs() && user.gcs) + err = restore_gcs_context(&user); + if (err == 0 && system_supports_tpidr2() && user.tpidr2) err = restore_tpidr2_context(&user); @@ -949,6 +1043,13 @@ static int setup_sigframe_layout(struct rt_sigframe_user_layout *user, return err; } + if (system_supports_gcs()) { + err = sigframe_alloc(user, &user->gcs_offset, + sizeof(struct gcs_context)); + if (err) + return err; + } + if (system_supports_sve() || system_supports_sme()) { unsigned int vq = 0; @@ -1042,6 +1143,12 @@ static int setup_sigframe(struct rt_sigframe_user_layout *user, __put_user_error(current->thread.fault_code, &esr_ctx->esr, err); } + if (system_supports_gcs() && err == 0 && user->gcs_offset) { + struct gcs_context __user *gcs_ctx = + apply_user_offset(user, user->gcs_offset); + err |= preserve_gcs_context(gcs_ctx); + } + /* Scalable Vector Extension state (including streaming), if present */ if ((system_supports_sve() || system_supports_sme()) && err == 0 && user->sve_offset) { From patchwork Wed Nov 22 09:42:36 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Brown X-Patchwork-Id: 13464419 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 07857C61D9D for ; Wed, 22 Nov 2023 09:46:20 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A35206B05BC; Wed, 22 Nov 2023 04:46:19 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 9E5776B05BD; Wed, 22 Nov 2023 04:46:19 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8D4C56B05BE; Wed, 22 Nov 2023 04:46:19 -0500 (EST) 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 7EBCE6B05BC for ; Wed, 22 Nov 2023 04:46:19 -0500 (EST) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 421FD1A0295 for ; Wed, 22 Nov 2023 09:46:19 +0000 (UTC) X-FDA: 81485109678.28.21BD96A Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by imf05.hostedemail.com (Postfix) with ESMTP id D9CF9100011 for ; Wed, 22 Nov 2023 09:46:16 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=bO+EDdYB; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf05.hostedemail.com: domain of broonie@kernel.org designates 145.40.73.55 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=1700646377; 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=nGowsrdBIlCMrQN6IvwN6mQelvaNZeDEi5rXdHlD8sI=; b=Nqan2a1QOjSpT/4WpwolB9HCYVUBueEkbq3B5FtQAmJKhN0KErS/1sXqOPoqwLjXBUYiuo ORinLyVjZumTXaO4WLpb6s/SJTM5jC6pRXK/TAQqjfBcsJW2y3nqcR0E35OmAA1InmaM/4 mYmUsDPfR/fi1hF2yoc0AMYgkkKTcDQ= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=bO+EDdYB; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf05.hostedemail.com: domain of broonie@kernel.org designates 145.40.73.55 as permitted sender) smtp.mailfrom=broonie@kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1700646377; a=rsa-sha256; cv=none; b=R8rcx507EsDx6+QqY6BB/xn4SF6dk7c/6Wmd71W3GKUOijz3f+39FsIOCFwo38zPKmkGB9 1Oslb21DasMeuUHzMb+tYg/jpox2FqSxKpKo8OkN0pUzW/aNPe14cFUOgUviKiqg+ALW5Q x2I3vxwqZZBcYeF0YwOwwInWNqtly6Y= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sin.source.kernel.org (Postfix) with ESMTP id 58C93CE1E77; Wed, 22 Nov 2023 09:46:14 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id BF243C433CC; Wed, 22 Nov 2023 09:46:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1700646374; bh=bFlT5/awvXTof78KkT1d+gL2ROO+ZrM5pYa3JBpSrio=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=bO+EDdYB5VGZElKlGYPJaeuv0B0743DFAhHLmHDfS9M0pJeWV2FZLMZufVNyS02Pn 3CVj+UIACeUO0f8m2Lax3vNWcBPC8/DXe9LIx5KIVvKh55IiqceB7ZxMI+nUQmZ7OC 9NSrb2VB37RIVzuPiya49btFNT5kYwkg9iWQhaTiLYeLBPnfJa9WNUXAxK6tCfuIuG /U7dh9bY2lHlTjRUkVkSUempfu0xOSDHy3z8PJ8jM0rf6MWntWPo6zSBbbaP2v7EzY fu1GvOiR2woyzqDToJtuvf52Q5ny48mcXnET+shaTjYgo1uLZ/+ZJOVvrWLFKxw8uB 8F6iv99DT2QAw== From: Mark Brown Date: Wed, 22 Nov 2023 09:42:36 +0000 Subject: [PATCH v7 26/39] arm64/ptrace: Expose GCS via ptrace and core files MIME-Version: 1.0 Message-Id: <20231122-arm64-gcs-v7-26-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=4259; i=broonie@kernel.org; h=from:subject:message-id; bh=bFlT5/awvXTof78KkT1d+gL2ROO+ZrM5pYa3JBpSrio=; b=owEBbQGS/pANAwAKASTWi3JdVIfQAcsmYgBlXc0KraBhWo8Yu9TsgTVu/cTEFTnLR7d01WQ+/ 8pSUITq4fKJATMEAAEKAB0WIQSt5miqZ1cYtZ/in+ok1otyXVSH0AUCZV3NCgAKCRAk1otyXVSH 0E7dB/9HiqI+MMrMcBA7CHdbJrqryaQT8IGaG8oX639MqYezbjknfbchZggQZNm1U2FGnXbkzku R87sUnYn3bidqmXKt+kRRarvJZZQ8COkGq8aRjZid/yhSg7PXgkZx3llJdei8n5JgyhBt1KVlIv bSltKZCWQZdAXrB8J6PGZJGxfOqwbBkIIQZIEF+IoBgpwOHcag9PjYwiQ5e1xIKqtcYTzkqIUuI 0Ob0Q9V/CR3tpxTzo766EQy7Y7M+yQ3nijDLSrpclfVRXwydQMDiiFr8+psvaol5E43QTIEpwpF YFDWWKN2iQqVHZm3hxklP8V2dgmJ6RHM8XFM5BCIkCQa89Qv X-Developer-Key: i=broonie@kernel.org; a=openpgp; fpr=3F2568AAC26998F9E813A1C5C3F436CA30F5D8EB X-Rspamd-Queue-Id: D9CF9100011 X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: deb567usfzn7qdzdnt77x4p9j75bnot8 X-HE-Tag: 1700646376-381145 X-HE-Meta: U2FsdGVkX1+iqEvhR1lhDqQaNrGSL5UnVb38Z7/AKyJ8fEEjh85NAgyR2jDeB+380OtZt0V5x5MZnuVAcrd0XcA45hr7Nqtgc8r38ZzuX+yKomB95cGKi43QBfgZxp/5+nzFsQ5VAY1f2IlwC2CaG/ijyIe76chnlOolkk49yu1OJPje32WPpCRqhZY7bRnDLMHqmaKvdA32LJvarxQ80EAsOzUtntWIfcUGTLj8Fk+FJZnXJW0k3Nzd5x2F6pQydii+wXXWKMwNpe1saVgq4WvL60HW8AEBL0yT+8XBYcUZYiLMb5LqCjY9bCFnZ2equEV3Ys6nNYuGWD4hDJg3vambI8mJJ+PS/5WQPXp6CyxLZ+LVMSjIVu8fQQ1uE/bg9lzp0i7VR8rYtFg4tNupKpTUsn/i41lsZSDB/RXnDo9mfbZ2mNZJOJhKZ2AAFtwpl82wm2QS0EMF1/et0HF5GEcsTqYsCZDNivqyDnSLB8gZ1XD9uRTG8OCBFtdIkX+3EFeLjJii80tdZYLsQdL/asHuIGlkLojfFzsRFhQaOiWQovSYum1DKK/SQu2A/RWCTvsyoqL2tBsT3oSd1xwJskDsYOQJOFjtRrDG6tGlRzaGEUg4ZlMZySZeE7VQKObtQPotqSTo1E1+upp43ypapUMglxhf2H/svcjaXqkAuKLDdtWePRbIuchLaui+OHShhpY7L2zU5ur6jZDESXnj8y2EuQo0vubJfLfmJcyF2U3RAG0QIF3XntEMF8O6AXxG/0HDOnPXNXM+g/pNPTs9NGDL2zJI+pLMihVLXTEJbLwMtsjYCjOEfUq7bcCcpcxnG0NDkrRuMo2QKi7P0x34f/U4rfSi+unozTb2YkEZWQz4NKsNnyhi6+P8BuaObzZhVkf8Z3iwX/BX+8oquVcGCkY9SXg5p4aFlfCEgBj9bRQIA2TcWmvGt8RINZ7eGoyUKkYYsOjgDgIe+8Db5iA saZuha/F pmp3SLRN3f+KuYyAWLCaGxw5/rdddWd/J+wJLOXqluxZ89XG8TzZz3dFkTfGKt+/Z4PPhcPAgfTqCRRWJV6bYkt+XijW7mfVD9g4DD93o4H1CPEIT68UA4XM2Y8HNA2Q+oRCLQnK9sR0nsDO+IBfVq6wYc0tJ1vGpy4PnIvWEKzt4Ivg9bidQCbwREVLVugEdsTGvBPqpvI1glS5CcvWeQy8Q9rKAA+9q5+043dqtueAL5GNfEX+RmpL/sTC4YkhuyPmWmXUx5UYcwwNSmCGr8kA3B6+tInHncgTsXGbThv/OjMAFSCH7PgQja1kAV0KSimRPqnnhBUyMwPYYb8blGe/AAlJIYYhJ3ywu87DyxkDcUYY= 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: Provide a new register type NT_ARM_GCS reporting the current GCS mode and pointer for EL0. Due to the interactions with allocation and deallocation of Guarded Control Stacks we do not permit any changes to the GCS mode via ptrace, only GCSPR_EL0 may be changed. Signed-off-by: Mark Brown --- arch/arm64/include/uapi/asm/ptrace.h | 8 +++++ arch/arm64/kernel/ptrace.c | 59 ++++++++++++++++++++++++++++++++++++ include/uapi/linux/elf.h | 1 + 3 files changed, 68 insertions(+) diff --git a/arch/arm64/include/uapi/asm/ptrace.h b/arch/arm64/include/uapi/asm/ptrace.h index 7fa2f7036aa7..0f39ba4f3efd 100644 --- a/arch/arm64/include/uapi/asm/ptrace.h +++ b/arch/arm64/include/uapi/asm/ptrace.h @@ -324,6 +324,14 @@ struct user_za_header { #define ZA_PT_SIZE(vq) \ (ZA_PT_ZA_OFFSET + ZA_PT_ZA_SIZE(vq)) +/* GCS state (NT_ARM_GCS) */ + +struct user_gcs { + __u64 features_enabled; + __u64 features_locked; + __u64 gcspr_el0; +}; + #endif /* __ASSEMBLY__ */ #endif /* _UAPI__ASM_PTRACE_H */ diff --git a/arch/arm64/kernel/ptrace.c b/arch/arm64/kernel/ptrace.c index 20d7ef82de90..f15b8e33561e 100644 --- a/arch/arm64/kernel/ptrace.c +++ b/arch/arm64/kernel/ptrace.c @@ -33,6 +33,7 @@ #include #include #include +#include #include #include #include @@ -1409,6 +1410,51 @@ static int tagged_addr_ctrl_set(struct task_struct *target, const struct } #endif +#ifdef CONFIG_ARM64_GCS +static int gcs_get(struct task_struct *target, + const struct user_regset *regset, + struct membuf to) +{ + struct user_gcs user_gcs; + + if (target == current) + gcs_preserve_current_state(); + + user_gcs.features_enabled = target->thread.gcs_el0_mode; + user_gcs.features_locked = target->thread.gcs_el0_locked; + user_gcs.gcspr_el0 = target->thread.gcspr_el0; + + return membuf_write(&to, &user_gcs, sizeof(user_gcs)); +} + +static int gcs_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_gcs user_gcs; + + ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &user_gcs, 0, -1); + if (ret) + return ret; + + if (user_gcs.features_enabled & ~PR_SHADOW_STACK_SUPPORTED_STATUS_MASK) + return -EINVAL; + + /* Do not allow enable via ptrace */ + if ((user_gcs.features_enabled & PR_SHADOW_STACK_ENABLE) && + !!(target->thread.gcs_el0_mode & PR_SHADOW_STACK_ENABLE)) + return -EBUSY; + + target->thread.gcs_el0_mode = user_gcs.features_enabled; + target->thread.gcs_el0_locked = user_gcs.features_locked; + target->thread.gcspr_el0 = user_gcs.gcspr_el0; + + return 0; +} +#endif + enum aarch64_regset { REGSET_GPR, REGSET_FPR, @@ -1437,6 +1483,9 @@ enum aarch64_regset { #ifdef CONFIG_ARM64_TAGGED_ADDR_ABI REGSET_TAGGED_ADDR_CTRL, #endif +#ifdef CONFIG_ARM64_GCS + REGSET_GCS, +#endif }; static const struct user_regset aarch64_regsets[] = { @@ -1587,6 +1636,16 @@ static const struct user_regset aarch64_regsets[] = { .set = tagged_addr_ctrl_set, }, #endif +#ifdef CONFIG_ARM64_GCS + [REGSET_GCS] = { + .core_note_type = NT_ARM_GCS, + .n = sizeof(struct user_gcs) / sizeof(u64), + .size = sizeof(u64), + .align = sizeof(u64), + .regset_get = gcs_get, + .set = gcs_set, + }, +#endif }; static const struct user_regset_view user_aarch64_view = { diff --git a/include/uapi/linux/elf.h b/include/uapi/linux/elf.h index 9417309b7230..436dfc359f61 100644 --- a/include/uapi/linux/elf.h +++ b/include/uapi/linux/elf.h @@ -440,6 +440,7 @@ typedef struct elf64_shdr { #define NT_ARM_SSVE 0x40b /* ARM Streaming SVE registers */ #define NT_ARM_ZA 0x40c /* ARM SME ZA registers */ #define NT_ARM_ZT 0x40d /* ARM SME ZT registers */ +#define NT_ARM_GCS 0x40e /* ARM GCS state */ #define NT_ARC_V2 0x600 /* ARCv2 accumulator/extra registers */ #define NT_VMCOREDD 0x700 /* Vmcore Device Dump Note */ #define NT_MIPS_DSP 0x800 /* MIPS DSP ASE registers */ From patchwork Wed Nov 22 09:42:37 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Brown X-Patchwork-Id: 13464421 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 20EBEC61D9B for ; Wed, 22 Nov 2023 09:46:29 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B211A6B05C0; Wed, 22 Nov 2023 04:46:28 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id ACFF86B05C1; Wed, 22 Nov 2023 04:46:28 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9710D6B05C2; Wed, 22 Nov 2023 04:46:28 -0500 (EST) 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 860BB6B05C0 for ; Wed, 22 Nov 2023 04:46:28 -0500 (EST) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 6DAA9B58C8 for ; Wed, 22 Nov 2023 09:46:28 +0000 (UTC) X-FDA: 81485110056.22.AE70695 Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by imf10.hostedemail.com (Postfix) with ESMTP id F044CC0008 for ; Wed, 22 Nov 2023 09:46:25 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b="ZNt/mhOl"; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf10.hostedemail.com: domain of broonie@kernel.org designates 145.40.73.55 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=1700646386; 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=fzbo3KR4MOr1ZmKxTJQohTxFt954ex0BmbuXA1ZQ+UM=; b=tCG9Kjc9jpLEoOqFbq38EnXWZBXwVRiQc8tpwxzwMTVw6TdEXbWffhgdLnAoFpJRLwRLdj ZiEKjWzCfoqHqlel3NQZeguCTJ03Bs6eKJDO3Lq/dqmHNgT1dVtiaauuov+SPyWwNZzOCJ 2hV4lp3ED7d0IPyJUp16McEdQCoCG64= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b="ZNt/mhOl"; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf10.hostedemail.com: domain of broonie@kernel.org designates 145.40.73.55 as permitted sender) smtp.mailfrom=broonie@kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1700646386; a=rsa-sha256; cv=none; b=HWkRrEQvGJdjFznKbwcxqirFOqmnIDk3R4nOWZ85VvetR3id7HO2BXPP5SR99rFnn8HkhZ dQLXD9AukFG6/6NmU7DcSOiI+feJ9Er/jjwyGxufl7MTixonBbUijI3LxTVuboJisiypoQ NR8OFeUCRMLdryW3yE0oAtMvDF48P7c= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sin.source.kernel.org (Postfix) with ESMTP id 7A13ECE1EC1; Wed, 22 Nov 2023 09:46:23 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 866E8C43395; Wed, 22 Nov 2023 09:46:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1700646383; bh=0y6nS2SVdzl2qxV5TcDCnpKi8YQvhWLC/gR5WsuQggw=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=ZNt/mhOldQGyAGrFdcd7zCoYi9c4Cpbfl611N1nCbvre1FYvH7Zl1qxYMHlLUXK/R oTr4pGgCl3ZqkdfLg9vxV823aJQE0eCLlAhRLaW1gT0pmdwjk38uP6NXqfphyDxggd Nce0uiHdxAcfp2N1Y9laenaA7HV7sOUFODNyqrBMy7XcrOzKnRNQTKmHrYboi3WFf4 QOpE3cQlVvo2KE6NieOOuSEronhIGHfX5fucm1ZO0NVH0NxleNUmMK8+wdtaJLpRNh bN4Wz6vhiC+V3XP1enBFhlP/PZm5aE6IiSJnXGkBC62BqC2W7W+tQNNElkgCPc4NEY Pt1AESbQWlpmQ== From: Mark Brown Date: Wed, 22 Nov 2023 09:42:37 +0000 Subject: [PATCH v7 27/39] arm64: Add Kconfig for Guarded Control Stack (GCS) MIME-Version: 1.0 Message-Id: <20231122-arm64-gcs-v7-27-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=1372; i=broonie@kernel.org; h=from:subject:message-id; bh=0y6nS2SVdzl2qxV5TcDCnpKi8YQvhWLC/gR5WsuQggw=; b=owEBbQGS/pANAwAKASTWi3JdVIfQAcsmYgBlXc0LjTRQMrZZBOJgPLB48tbG4uiml7CZIDEV5 uQD798eCNqJATMEAAEKAB0WIQSt5miqZ1cYtZ/in+ok1otyXVSH0AUCZV3NCwAKCRAk1otyXVSH 0Iq3B/9HlE8pfPFk2nuyqu1U5wC2TDnfo30tlZoqa72rjxWoZMeOZIt03d7ifiDGT60BAhV7DR/ X2q4ijl0gxsqAdoQTECIT09I17i0jhkg4sOrFnUM9S88Dc3B+OIQORdu8k6AozeCurJMkcd/ICY snWt5N0Nh2ox8T7U6IwsCVSrOZfctm8ylWDVAVHxsy53/DBhuI0FtT+rlbYGseRcYj1zRLPXpf+ BVttx7Ntz2AelFYWVTuV1+GEKIwFIHMxKR/QSrU8uS9CzItwTUAOZKN30cLz7YqjsJMsYAnSVKN Bd8Eqt+f6s9NrIoUWZmOF8FfRH4sNLQwPl0XXWRCxkdsmJNx X-Developer-Key: i=broonie@kernel.org; a=openpgp; fpr=3F2568AAC26998F9E813A1C5C3F436CA30F5D8EB X-Rspamd-Queue-Id: F044CC0008 X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: ueyjxseag5sqf1q9jsuctfnhr3ajyjoz X-HE-Tag: 1700646385-814049 X-HE-Meta: U2FsdGVkX1/bFgP7PMjxOnINBdPCiMelg6pQyfSGPmtgCMnxKhboRIrWkUknxnduMXqGvZ/bdf60LdATTC604NvDEKWX2voaeq6ogICcCnMVWEpocVe9TNkLaO3WmOaDmPdBXml9Zutm0EAsiGf2t3HjNNJfcdw+ft3iW09Ai10GcVvRC3szdu3G3t0X9rUtq+RD3etM8/3p6YxufSSsj2FrFF44UWIrJZmuTycjUvfpOd8TGOb25N3oeOnug+9B+oeIax61/8tgDmo0sfB/fldnFZZSTSAVghTOo/uQZ92ufeLkh1zOUPMmPlXwsmtJ9PjMuCTXFAQdoEGDkHxT3XacVDB3QBP0KPq0QYkGlR5JY15Lh4c7X5VsEhwch0tQDWr8Tx4czS7yuiFW3cr6ioy8fi7+iPU66mpAg+pmQhg0drPM19dmZ7fl7TaGspOmlkLmSEfHn9BCFv/0lRA5ILKErQ/5RPR+QOvmQHbwNRiAq3VuA3dP3qqhof1MbfR1IU5/llVNl9LGTbkn69iHbrDjrMOhflE3qyS4Pjm7keOLxcIft9CmIpeAbcY0Cb7gXayiVKU+pdJ1QefvxQTIBvGzsr6TJ55HlFsTY62vVY6m5FVUp97k09CkbwbRG2RO2TgvzcRGCExETEwEnMYUX4nMlsRJP4tLJcicZb20LTTOQnaR20o12zY1Atgr+y6uhDrGkYm+40N6G6fSFzKocIPn/pcj3X+l9KGWCZuJf64mHFf53QRjzwshLF7bEjsctM3dxYOWfNjboWsLnzyHqx6B7fi7ZCfOfpSyWnoQ++z6YO9YpQFH8WpTi85wT9R4A/TG7UagmjewE0vS757RBW90gi5nzNp3DD/Iceb4k8Bqn5zYfSdy+Gl3xfdy67dRXxa55V8bUbe7wk+9Ht2DblrnCW95P3Xj2R+8fvKkzDUDzKGfXjSBrgCQ4tjpgfHDvLIgbSiPtWacuA4/ieV 8pk77+mc +pHjJ9px+0eOcPezF5TuUHEw5Nw5PytKFKxVk6hAfWiRL+77trAq/urALs03sk0x0ejHq0UrabOZuOdV5TDfoHhiTd2vJJiwKdwGJmCYQCeeFdfK1HcRACU6tFSQ6Q76f3/tXeeMPaYJzEtS7OoB5BBTr3LvP7HJqu8FTgAxE/lzAWaW6undtpMEA35iCcwOx8zSG4BFGlJ+54ZXrT3Df5xVPQsLuQMjnEdX31Gu4KY7xEgwdrnyPPxf9vvLv7ajc5KqUxfWAF/sK3xoJeWhIafom2IijE7kZGU76eWRhzd0eAq9tcsktOjhfH+KEf5jE6jb1 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: Provide a Kconfig option allowing the user to select if GCS support is built into the kernel. Signed-off-by: Mark Brown --- arch/arm64/Kconfig | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/arch/arm64/Kconfig b/arch/arm64/Kconfig index 7b071a00425d..05cffc0f5786 100644 --- a/arch/arm64/Kconfig +++ b/arch/arm64/Kconfig @@ -2078,6 +2078,26 @@ config ARM64_EPAN if the cpu does not implement the feature. endmenu # "ARMv8.7 architectural features" +menu "v9.4 architectural features" + +config ARM64_GCS + bool "Enable support for Guarded Control Stack (GCS)" + default y + select ARCH_HAS_USER_SHADOW_STACK + select ARCH_USES_HIGH_VMA_FLAGS + help + Guarded Control Stack (GCS) provides support for a separate + stack with restricted access which contains only return + addresses. This can be used to harden against some attacks + by comparing return address used by the program with what is + stored in the GCS, and may also be used to efficiently obtain + the call stack for applications such as profiling. + + The feature is detected at runtime, and will remain disabled + if the system does not implement the feature. + +endmenu # "v9.4 architectural features" + config ARM64_SVE bool "ARM Scalable Vector Extension support" default y From patchwork Wed Nov 22 09:42:38 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Brown X-Patchwork-Id: 13464422 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 BCB52C61D9C for ; Wed, 22 Nov 2023 09:46:38 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 24A146B05C2; Wed, 22 Nov 2023 04:46:38 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 1F69A6B05C3; Wed, 22 Nov 2023 04:46:38 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0982F6B05C4; Wed, 22 Nov 2023 04:46:38 -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 EAF3E6B05C2 for ; Wed, 22 Nov 2023 04:46:37 -0500 (EST) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id C0AEF803F3 for ; Wed, 22 Nov 2023 09:46:37 +0000 (UTC) X-FDA: 81485110434.25.CF4DB12 Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by imf04.hostedemail.com (Postfix) with ESMTP id 48A6E40004 for ; Wed, 22 Nov 2023 09:46:34 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=fUa44Bi6; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf04.hostedemail.com: domain of broonie@kernel.org designates 145.40.73.55 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=1700646396; 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=ca9++bFNJIk1cLKn1TH2rm1ajrTk7LHzLeyWpvr3bRM=; b=taZbN9gLME6kfk2uHAfntfYD6JZCoI0VUe/8OHjMg32M58KIid6B4BDY3LMZLAhO3qbrKZ wjH8hoJvB/aELzvdS3u+MBlVvXa8gXpeDNJE+dM82wI0Bj+90svt5iXsK9XkLuBHhZm22/ H1UDDI3+gAry5NTv13TOOzU0Ee92MBo= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=fUa44Bi6; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf04.hostedemail.com: domain of broonie@kernel.org designates 145.40.73.55 as permitted sender) smtp.mailfrom=broonie@kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1700646396; a=rsa-sha256; cv=none; b=f5/9hSdEvt285nvmtYV9y4wEKir+GhQUBu0lUu4MFqAGPjkVcpKInCpZaw05TBwVQ+1Upb cKpiK4Sght1iVFab5I5pP2p/rNoTcstGMi6ZHYjaVxsUbYIsi1SMKo1ZH3t++LJ/OPz2ZA UuroQir0GQtdcPc8rPNfQCFkYTJfG30= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sin.source.kernel.org (Postfix) with ESMTP id 80C5ECE1EA4; Wed, 22 Nov 2023 09:46:32 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9A99AC43391; Wed, 22 Nov 2023 09:46:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1700646391; bh=I+uVw8aipGa+8Ns9k+b3dq26kNHNXYgyRv4lZpoE8Nc=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=fUa44Bi64x8jXoE/MvI8BIoiZzNypKXZeeiprmYPWGJR3lX5FGydI1vtuAYWadks7 UasgH5m/KtJQsDRJFu9SVTzRRZ09vZBslyb0Ho2ASTSMbx7DP6p+CS8+ianT5KLmas tJXDc07OIjutHkKJCdOQ9rbHFcS+M1Xk9x0/2ZDaXRW8l9gWOsMXGxNr64kYyyP2wN wkARHPE4CLrm5Ok5GqnIBpxAl47axEAxsh9fxCbSQqsYrNum0vfC6FHddFrleyQ0jj 1fispSHLot6aPOIBz5WXtqFTgm3jtICwL5ybtt6OQ3R735RlkVitJiiQ8Rd9rwhnj7 yf08/Sy6l7Hug== From: Mark Brown Date: Wed, 22 Nov 2023 09:42:38 +0000 Subject: [PATCH v7 28/39] kselftest/arm64: Verify the GCS hwcap MIME-Version: 1.0 Message-Id: <20231122-arm64-gcs-v7-28-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=1190; i=broonie@kernel.org; h=from:subject:message-id; bh=I+uVw8aipGa+8Ns9k+b3dq26kNHNXYgyRv4lZpoE8Nc=; b=owEBbQGS/pANAwAKASTWi3JdVIfQAcsmYgBlXc0MyzVIJO4Kid9gCsCQr45ifThlTEHkARV/j UTsXVxwqbuJATMEAAEKAB0WIQSt5miqZ1cYtZ/in+ok1otyXVSH0AUCZV3NDAAKCRAk1otyXVSH 0PTmCACERewHlO0lUDAx7VBob22qNqbBIi4nI0finRcfRAMHGdVUVIxWAqs+6UQtEQWTanwoxxh RHNYkE5AnkhBIVqgo9rPkkciwxmqsPMafm9s1H0t/nysi4FdiMxIMpg6pQt0D3ydQ1Psg/yCml7 nbLsDLWDnrT2PZBbwMSZdbxpf6TLjfDdFnf2PqTp7lJXdovzdfaZM83h4amYvIT+snl3MZDg+DE yO8Tc24qt+aAP5Fbjm4kLsSFx2poUdmSadKGczZTZxfOnsCSNiQjkj2FshEGptZPCYDyN1vmU0z I1pxYkitO980tsJqitFcBPPgoo+CvzMYooor7SZAL9CXXBHj X-Developer-Key: i=broonie@kernel.org; a=openpgp; fpr=3F2568AAC26998F9E813A1C5C3F436CA30F5D8EB X-Rspamd-Queue-Id: 48A6E40004 X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: gk1mt3c5guhtspp56zuk51geth5t49st X-HE-Tag: 1700646394-149097 X-HE-Meta: U2FsdGVkX1/2xvMghFZuNkA0y+llJiA76LGDsWGqr2ACmwup2KdMJdXHbhcUlV/CMN1RlpdLEI0VY1x5Vu7Sd0Q/ASm8jO/kl0gwTphUjPlNU2iTNdpwJ8TW9TRqJCu+02EMQwAUtvuAyI+xunclNcyceVRkWlUDkiMsnrxDGU/hCZbdp50K+CoAkA7pC8VoO7Lr7TmvY/tRYwqlLVOp94P3udIq4YQWZ1Q6+o9t8JyYTwT2FxCzhUV1WAaS/WMU6izIOTFDXJOtsbpEH7XXLnwzpsH16bCP+btrb9eyRMl4DyFvvlMEvMregkWegho0zl3Brss/KrsgIIJse2ISPn2VpRdQ/L1Ff6S6aQhPwkrZl/pL4Yt651wG83EvqQswEtGbFHjgLoZsyDboAHEKwfNyCogMXeugj5TYGa5zQav3l9MLKQ7bwblxbEn5wOL6DxqBCB9y9YS/xCobbAaNu3rrMZ6y9vVtcyF+H9uL7ifA/DOO/M3M6EkHcXasbAjLCF0uUxdYSpT1xgA0dnyycfF8vLYASvRaUE0bVMiIAMR3XS8khGdlXlplm5cDtADbLxvklAotXrNWe3y6GBSrWkmnNPPp72/i6YtGejs0D6uQGrtyqxQAL50h78a9TD5yStt7AZckY8VBL47LligFu1AZKLp2s4RkUBord9CP8asUue4GrNNuvTV3e0CPYyu86RyyVCTWglXa3UAdRNag7HTTROYiHVI3saqQIhK5F3MIjD+UTtCmf6c8Pz1tTNL9Oo/fIepuhcApCOSYkykNQ5eCs8Ngedn/sullkqvRNEmJfCfAORtTC4ABvhrq9hLpvfqJT0rBIq2egR1OdW7ytu/NHaRv5TH0rBDWs0p7JiKztB2VDydwDEMrkZQFkl22IK2aa4Cn0Zl0SffdJCxgqRGtyXAu948RdBSAwb5RKcFkggYTqx9v2AB7SSht7Hqr7NtplqrxZfiOlOphe5S 4YV2CqWy t8AqyNugRHA0gy7G2L6xSfairWc0zKU4s1O/LUnp+emUFTeFDfNPapCX2RO6BYgQxVDxyETD/V305llJcF391nGom8387EwvHNIHcisY6JP0gkWuzQY4eR9KYLIrDpsS5VPWyPVZw2QIE5G8zVQ/VNCDvl/3jguSxGtNboZZ7iyGko2Y2SoNMF6T70qULC5JxjzIzJgC0ZJ7qIKELVezkdnxiwPi4d0luVDP9W8aGlhyzKllUPMK60GXecM5/5evWN9AUncgXDhI58YXyfbM4EsHkvZcAQYaVNaojVkl8iykzVR76XW+SI57mUH1sj8F3W2zj 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: Add coverage of the GCS hwcap to the hwcap selftest, using a read of GCSPR_EL0 to generate SIGILL without having to worry about enabling GCS. Signed-off-by: Mark Brown --- tools/testing/selftests/arm64/abi/hwcap.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/tools/testing/selftests/arm64/abi/hwcap.c b/tools/testing/selftests/arm64/abi/hwcap.c index 1189e77c8152..bc9e3250a9df 100644 --- a/tools/testing/selftests/arm64/abi/hwcap.c +++ b/tools/testing/selftests/arm64/abi/hwcap.c @@ -63,6 +63,17 @@ static void fp_sigill(void) asm volatile("fmov s0, #1"); } +static void gcs_sigill(void) +{ + unsigned long *gcspr; + + asm volatile( + "mrs %0, S3_3_C2_C5_1" + : "=r" (gcspr) + : + : "cc"); +} + static void ilrcpc_sigill(void) { /* LDAPUR W0, [SP, #8] */ @@ -360,6 +371,14 @@ static const struct hwcap_data { .cpuinfo = "fp", .sigill_fn = fp_sigill, }, + { + .name = "GCS", + .at_hwcap = AT_HWCAP2, + .hwcap_bit = HWCAP2_GCS, + .cpuinfo = "gcs", + .sigill_fn = gcs_sigill, + .sigill_reliable = true, + }, { .name = "JSCVT", .at_hwcap = AT_HWCAP, From patchwork Wed Nov 22 09:42:39 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Brown X-Patchwork-Id: 13464423 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 3B681C61D9B for ; Wed, 22 Nov 2023 09:46:48 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id CCF796B05C4; Wed, 22 Nov 2023 04:46:47 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id C7E416B05C5; Wed, 22 Nov 2023 04:46:47 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B6D2C6B05C6; Wed, 22 Nov 2023 04:46:47 -0500 (EST) 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 A7B766B05C4 for ; Wed, 22 Nov 2023 04:46:47 -0500 (EST) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 7FEA9A0656 for ; Wed, 22 Nov 2023 09:46:47 +0000 (UTC) X-FDA: 81485110854.21.098C7A7 Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by imf27.hostedemail.com (Postfix) with ESMTP id 0BCDE4001F for ; Wed, 22 Nov 2023 09:46:44 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=p3JwdAwl; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf27.hostedemail.com: domain of broonie@kernel.org designates 145.40.73.55 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=1700646405; 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=+yPK72IDWPizGwnzq/hEJFw0qTsKEBkl+c/fPfZYSgc=; b=bKd6yclXLYo2QPD3HvMM3MvaymnbQbYqcoBEhyCPpjR6SV3uHj87bwOe9oVOQ5gyihpTpU KQ3r5nBvRNHSvDvhNzXDI7+Ahf7mce0S6N55iAAOhNJjEyncfP7AT/Wl848CT8+V69PlGb gAhvzMS+LznXuLZLLqJN5KsN0Znj41k= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=p3JwdAwl; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf27.hostedemail.com: domain of broonie@kernel.org designates 145.40.73.55 as permitted sender) smtp.mailfrom=broonie@kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1700646405; a=rsa-sha256; cv=none; b=xp7Q4oJnq/DX0qebuqxWvWhMIdQxpsCc1zjo3Lud8Pmp+JlBcv+qSbffUExRkMwQfp+Cmo 0FeLTJAgPR9+ZTQ5OPSc2gXnvdnEBbqndFmdeWOwNBAMqlRcpVylbMYPoFj3NxLF3jdJgw 4RtxVhM2FkTaq19tfXy8gbuSX9jKJDk= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sin.source.kernel.org (Postfix) with ESMTP id 1E66FCE1EC3; Wed, 22 Nov 2023 09:46:42 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 495C6C433C8; Wed, 22 Nov 2023 09:46:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1700646401; bh=eI3VK56kyhvq7JOnBJXgl6j+EIFmz1Cd350CxZrMcNA=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=p3JwdAwl1IwDl1cRu279YeAAcZ/jDDNBFvD6epk9Cj3NAtBjknB4UKuxychR75RtB FhjbV32JXVmGGIo7RvOYvZI21X1Va108tpxqaoXhAlxRto69QxeD4jrYLKfxzRH+xS fS4SsKyIPwP7gr6HZsK/zCT+FGosjhk9bH6qPcKXRN5/Gg8IcmfmTi+fnplKR0d3NB d4XtyugCFtLBNbW2QMhR75JadHd3ITyQBaQ4BS9ZMhBgxqaRG6GWK5XglKVU3qBEyH 51/2dg7wqDRYv0qDxXDHavJlTBhDl1MK0iQvFuQaTnqc0cUJPWw7BCj4c2ii5isa1m 5uI1oYMi+XrMQ== From: Mark Brown Date: Wed, 22 Nov 2023 09:42:39 +0000 Subject: [PATCH v7 29/39] kselftest/arm64: Add GCS as a detected feature in the signal tests MIME-Version: 1.0 Message-Id: <20231122-arm64-gcs-v7-29-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=1828; i=broonie@kernel.org; h=from:subject:message-id; bh=eI3VK56kyhvq7JOnBJXgl6j+EIFmz1Cd350CxZrMcNA=; b=owEBbQGS/pANAwAKASTWi3JdVIfQAcsmYgBlXc0MpoaH+B7so44Op51dKDPnZ+HR/liFSB4lO rxQeP/wL7iJATMEAAEKAB0WIQSt5miqZ1cYtZ/in+ok1otyXVSH0AUCZV3NDAAKCRAk1otyXVSH 0OPrB/0bYwxvcTBgjII2I1FWsRdgrDk0RAdSe1MhLGxQngmZhESoEneo2Hq5C+22QiDRyqPOBWG NBhs+DcJn2N8QakBKktW1j2hZa/f2Fq4KvqQXWzEzaozqttPg3+lRVUc/+5XW2a+SMylEt1YziP D2jfrKJj4dayCnvjh7KLRR9I6EXK8+UH3eNDRgBoO3xugNgtV822vuRQkc43QiV5uAvpfJ3I5ZM ddSrXmzHk2QU0QffGhU8k1hfn4I2IqmpsZW99HiDapofjAKTjk9jUeKd4kbu3X+PH/DOibREYO3 VHJNr+Al5/4t9DoAy8UzphVB0ymspKLRJlwLJbX6p/eOOAfu X-Developer-Key: i=broonie@kernel.org; a=openpgp; fpr=3F2568AAC26998F9E813A1C5C3F436CA30F5D8EB X-Rspamd-Queue-Id: 0BCDE4001F X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: 1esohh63z9yto33g8y4gzzna6kg8h43y X-HE-Tag: 1700646404-415273 X-HE-Meta: U2FsdGVkX1/+3kkn17i3U37W3Cc4/VyprOqUejNzwZ/QwtYrU3w3Sc8zcQ0PMLb8VS2NwLNOTtSpsvRBcAoG/Stab/xl2+3kcJ1FtKByfBoWYLELEZOgLhiWbqxLCIlvWrSFXyNrhRrzIBKQEdgBsz3StVKaynTCrGMTlqLPKg+mhFY104adiOfmCva1jmQsMCFzCii3ULdgTqQ1A96RbHbjzvrewqexqOpVKd7ZW0Y0Aiz6Lh3t+2ki6lXqWT9Vm7mR25WSmCk1QtZqe7s4msJEbEt2g2OJZphnlUh65Hz/RVxuN+i2JFlk/JSPGU4dxOOdmN/gsJDukVMvOuL+PAgFTM43FoI/NeR3FI6yvTaGQuYfpTCg9QqCRgMDJ1kU/ITn1p6h1cIABARBEOyQ1pjpk/pUuP7sO5FeadxAuXfrqLIpsi3B2GF7P8AVLMfJoPYKq1xm4Xnl0Oo09ZQXkTHlxK1yYUor+u8lDNjoF+CTt9nokLQcrnWpiT+89/ai0YFwZKBn4FcVxP1NqOOFg+ajUn3/Ap4sJ6wGV1HEtik/q6e0Es7bJpCdzym+ZX+v8YHEzTevRfo6vrYlB1jLaH1jf3m94gXrFkvBwg3zFd+CxO+akXmw/LwVumkL2JWrRTwBTZJks0GtFYCqWAiItyA3jp8CbHqymLOopjxFlWWR3sF6Kk3rZDN8LOgiVymyugWAjz7xlXyqGcosvkXssfxv4LOOYJVPkzbABerpYYx2A0bXSYY9RhSoeGlikQ9b2Zw4+LNob3+aYe2VU832MhH5iZ2ZAEdF/dmIA2tZUbvTtDXR23OYkiy8HmfRP+g3scyEuex29gjG7WUFrt5WPs3uY9xG2jm/52hK6UWNblpiM7XRsyivRjvxxx02yR5xSRDAr6xFxqgX2C/ozxRz4ul8M5Nhu8Nea6g7fR6Fu/ioFZqSJ5GttUTyKIH0D/7gkhPQ65Mgnv17alwDS9K YDdgDuN9 oouR1G9nzsKcjaNUGf6mQj1XZ5hDI8iuDk67qBA+xEpIUxj2vh8yqod5KFJKXPkNuyOQPE16FlEpypRWIztBiCEznW77truYuMIpEofXUxo2VLGgifppmdnkpPrbFGGRdA4sQBpcj7MME/wsqyIy0lJ738FEneuW/CNclL0+PkOiELwBQGqkzzhbilPGT4aceI/ljRm3LeUxmxjod9fXSx4jSVqg91Nmpk7tnzQJA96QPfM719GPsM7TwwbWUGx+oBvhS7q6DtsY6GhU5DT7BxwamuzzCfI57n33TtTEn2aGfVoWpJjGB6VmF3Gau2xZefQoC 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: In preparation for testing GCS related signal handling add it as a feature we check for in the signal handling support code. Signed-off-by: Mark Brown --- tools/testing/selftests/arm64/signal/test_signals.h | 2 ++ tools/testing/selftests/arm64/signal/test_signals_utils.c | 3 +++ 2 files changed, 5 insertions(+) diff --git a/tools/testing/selftests/arm64/signal/test_signals.h b/tools/testing/selftests/arm64/signal/test_signals.h index 1e6273d81575..7ada43688c02 100644 --- a/tools/testing/selftests/arm64/signal/test_signals.h +++ b/tools/testing/selftests/arm64/signal/test_signals.h @@ -35,6 +35,7 @@ enum { FSME_BIT, FSME_FA64_BIT, FSME2_BIT, + FGCS_BIT, FMAX_END }; @@ -43,6 +44,7 @@ enum { #define FEAT_SME (1UL << FSME_BIT) #define FEAT_SME_FA64 (1UL << FSME_FA64_BIT) #define FEAT_SME2 (1UL << FSME2_BIT) +#define FEAT_GCS (1UL << FGCS_BIT) /* * A descriptor used to describe and configure a test case. diff --git a/tools/testing/selftests/arm64/signal/test_signals_utils.c b/tools/testing/selftests/arm64/signal/test_signals_utils.c index 0dc948db3a4a..89ef95c1af0e 100644 --- a/tools/testing/selftests/arm64/signal/test_signals_utils.c +++ b/tools/testing/selftests/arm64/signal/test_signals_utils.c @@ -30,6 +30,7 @@ static char const *const feats_names[FMAX_END] = { " SME ", " FA64 ", " SME2 ", + " GCS ", }; #define MAX_FEATS_SZ 128 @@ -329,6 +330,8 @@ int test_init(struct tdescr *td) td->feats_supported |= FEAT_SME_FA64; if (getauxval(AT_HWCAP2) & HWCAP2_SME2) td->feats_supported |= FEAT_SME2; + if (getauxval(AT_HWCAP2) & HWCAP2_GCS) + td->feats_supported |= FEAT_GCS; if (feats_ok(td)) { if (td->feats_required & td->feats_supported) fprintf(stderr, From patchwork Wed Nov 22 09:42:40 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Brown X-Patchwork-Id: 13464424 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 1A70EC61D9D for ; Wed, 22 Nov 2023 09:46:55 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B48736B05C6; Wed, 22 Nov 2023 04:46:54 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id AF7E06B05C7; Wed, 22 Nov 2023 04:46:54 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9C02C6B05C8; Wed, 22 Nov 2023 04:46:54 -0500 (EST) 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 8CB546B05C6 for ; Wed, 22 Nov 2023 04:46:54 -0500 (EST) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 6B7401206DE for ; Wed, 22 Nov 2023 09:46:54 +0000 (UTC) X-FDA: 81485111148.09.B708139 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf04.hostedemail.com (Postfix) with ESMTP id 87ABC40007 for ; Wed, 22 Nov 2023 09:46:52 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=PZ1Wx9Tk; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf04.hostedemail.com: domain of broonie@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=broonie@kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1700646412; a=rsa-sha256; cv=none; b=4nu6CQrKomHakB7ncDgotEak7UvHU/UEj+KjWLlhbM8wotZOY2ZO8/gPO+RnthB2k4Gm4U 1kk2PlE6gpq8Qrv/JUjjsO0ME7d7sdFwh3DW0ntF/We7hI7W8FwAevtbJ0bdAhOZT7x3hn H+j/Exxk2HkZiabhNSA2pPGCYaCIGck= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=PZ1Wx9Tk; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf04.hostedemail.com: domain of broonie@kernel.org designates 139.178.84.217 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=1700646412; 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=y26ddMOg9K/y/tJ2sKEQf/YeocVRTVypBE9Q+/+9A7M=; b=Vn2aL+K8nZa37NMURmPIw4l+WC27br+2sgITc1ttUuCpEOM3+i05ht14HkNYjeZs14yIxA S3shqfYp1Ax/Zn7kJlP9p9o69LmYT57BT1WAM26DvaE1ekE6rwQKpNoOpOqsRQAp8LF5US glhD+35U3KcjopjYziqb9lfNXCSi4cg= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id CEE1961CD1; Wed, 22 Nov 2023 09:46:51 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id E8857C433CA; Wed, 22 Nov 2023 09:46:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1700646410; bh=T2ruSLpA9W494zUv5Qepx8+aOeVctWYAUnjtvSwySvY=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=PZ1Wx9TkLoV/X+IrvGpvBxm3p6Izmd9GqK9jyOJc9sy1HNqn/hpBB9IDGY/YnItPC FxNsykRf6mVt0YMlfyr+sN6Ms9T7RVQaFkNsTC97fEZO+fUPVL3W+m9G0jzYB6qJvU fcOq4KiCnX3OIM7tYWZAgS1Dfpg9op6/746si+Kwinx4ZMTj3QHxm/4lY+A9etL2tm 3tDVaZcBXqr6AloxwT6VSntqHTUxAhWdYRr3+uHHBcI2CVZGpeWf/WO77bzijAhQMP IgOKUS9jtIu55IdkUoQbVxx5C6B0g0nizVlIJwBO2KFoLMsKjJTn1z7CT9vjorguIK mbuF/n9+xD0Xw== From: Mark Brown Date: Wed, 22 Nov 2023 09:42:40 +0000 Subject: [PATCH v7 30/39] kselftest/arm64: Add framework support for GCS to signal handling tests MIME-Version: 1.0 Message-Id: <20231122-arm64-gcs-v7-30-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=1590; i=broonie@kernel.org; h=from:subject:message-id; bh=T2ruSLpA9W494zUv5Qepx8+aOeVctWYAUnjtvSwySvY=; b=owEBbQGS/pANAwAKASTWi3JdVIfQAcsmYgBlXc0NR2aczxjlGYq03aKOVjt4W10C6p38XrK+J oS87f9vTxSJATMEAAEKAB0WIQSt5miqZ1cYtZ/in+ok1otyXVSH0AUCZV3NDQAKCRAk1otyXVSH 0Ad/B/0VsxBXosT03kzkufFFapLPj6qQjPHPpPO24L3muhblnM2KlRjG3UB0MkKu0OEUIMVyUho SY91EGTkHNCVXdHw/Lpc1koXFKbfRMmvqbBknEVQQu1QuaxEHVqSeecnRQ8PvGmxep+HblX3Qgb KR3G5zVXW9eN7I2W5giFJfKIIVQ8LQDfzz89bwRkq7w/kCa7nvvQSOYd0aY7jokuRUL7OrvZL6W jk+8SBvujVBd0652MR+Sut6kI5bVnfQMcKoXiaAFeUEuhmIbkk7sG3L2Kd3VY6lOxsTMibSGZL+ UgM9uLhNy73cMheqzSwzPPmyYw652KL+eUp2gxUIYbYB5b4J X-Developer-Key: i=broonie@kernel.org; a=openpgp; fpr=3F2568AAC26998F9E813A1C5C3F436CA30F5D8EB X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 87ABC40007 X-Stat-Signature: 8p6seq8ducwtog6mycum7wxqnewh7tcf X-HE-Tag: 1700646412-778904 X-HE-Meta: U2FsdGVkX1+SgqlGN0cgW3Q9NSIPzWT30gPuWwX2Anzty8ZD6bZUZjY8z8BACjv5GO0oYDKChS95fKcjdUf3eUMBg0LKmTAfvmzFLOZfVCFWRURLou5zedIECCkwA7dXvxcJi0wv4LChN+SFlZ4dWGPsmSEoKqfK2tvO60bkahjhiM/N7gX92hQTqpi8XJmky/4l7lx/QXpTN/DaUt0Ox1WNXf/Vih3ROSmyJxIG6zhWE4cfrKX8xgIF4ZhYMKHGyLDCqOkhKImv+scwo5Hy+YYt4xszHRoQdZoLS4K586cZqzajM0EX2Wnp4Ns8PHRzX4klvpNxIgKwnn9sijikht02LGONKyA8S0ooR+CkpJqhpEcwctIJhjKZHbKKtutpqs5YjBJB9XnoE9GdxapKb7eLcHQ7Bxo/iZRPwSi+I4IrHwJRsNmJZgwfw1zi6Azg3zGd9incyZMFOnQ09Vd6zeWSw04rsynXoo/mMz1Y4k31IZPO7Wo6zC8rCWiiiJ2PMoBOP9DdsPeXNso1VzwiBl7UV3D7389cYAMD5Jf/5axF4Nqb6DCyojoW/KQpJrMAk9laCNZfYj61qYDmRcRhiWk9wHgu8MuAeuuJeMZvYk0ZOrVAc8MByazYwPzSs9rIBAsPQJN3euz85oIaboWjsuKnDee2i2ad0MD3Fve2B1LaDoo/ka+vm/k6S1JZkadX9VjaJG/pDUyehn1Zan4QFNQO2q9ZW5/Ka7Ulxg+q7mGjDw+G9Zsx34rFrx2xq8bN2Ujggt+9FcxzhRqyXD8fKrCTZ/OUC1D9f43JyJNLiRhUvyPZZBEl2pA0KPYLXG9AbBqay7g6sgDslUvCxd24dkg5/Mr5dTPAHfWx9sFmj3OfsHLehaly4vYmszIkYoQ0YkK2/z0EpaJL0NHofND8vGbV2C1QKMbKePfWkwU+jgpT2P7VOUJXF4O9cW5OhZkd2ZAQ2b+yCz1nkeK84/K 5SCshj7Q zMZO1jDQn00Fzi/LDtUsc0exe8skh/1DZ4R1JatB2+m0tuAS1BGIAK4UbSryLez1QSpmXwKkLpXlkR6I5fe55kNOPh5shyQ+5WJ3YHYXLLIWciKlNrlnAVDA3s2OCd/DNPm2OfGPl0gH2IRwwSp97ZTzG5GugzXQiFPpiX5vNesYJ96l3IbbFXliSaI/wjdZBP0Mpdh9mgi2MwjX5YJnFOtZ2WfPE5RmCSkveMarpwQ4jQ5wUt/2Y6MoEdCSIgRFffh/z66ZeVLIG2aM5qkTIpRvkSckDCkXhqyqq4LZnU/LiMqVaLLgn7hsFpgKuxINLn3fJ 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: Teach the framework about the GCS signal context, avoiding warnings on the unknown context. Signed-off-by: Mark Brown --- tools/testing/selftests/arm64/signal/testcases/testcases.c | 7 +++++++ tools/testing/selftests/arm64/signal/testcases/testcases.h | 1 + 2 files changed, 8 insertions(+) diff --git a/tools/testing/selftests/arm64/signal/testcases/testcases.c b/tools/testing/selftests/arm64/signal/testcases/testcases.c index 9f580b55b388..1cd124732be4 100644 --- a/tools/testing/selftests/arm64/signal/testcases/testcases.c +++ b/tools/testing/selftests/arm64/signal/testcases/testcases.c @@ -209,6 +209,13 @@ bool validate_reserved(ucontext_t *uc, size_t resv_sz, char **err) zt = (struct zt_context *)head; new_flags |= ZT_CTX; break; + case GCS_MAGIC: + if (flags & GCS_CTX) + *err = "Multiple GCS_MAGIC"; + if (head->size != sizeof(struct gcs_context)) + *err = "Bad size for gcs_context"; + new_flags |= GCS_CTX; + break; case EXTRA_MAGIC: if (flags & EXTRA_CTX) *err = "Multiple EXTRA_MAGIC"; diff --git a/tools/testing/selftests/arm64/signal/testcases/testcases.h b/tools/testing/selftests/arm64/signal/testcases/testcases.h index a08ab0d6207a..9b2599745c29 100644 --- a/tools/testing/selftests/arm64/signal/testcases/testcases.h +++ b/tools/testing/selftests/arm64/signal/testcases/testcases.h @@ -19,6 +19,7 @@ #define ZA_CTX (1 << 2) #define EXTRA_CTX (1 << 3) #define ZT_CTX (1 << 4) +#define GCS_CTX (1 << 5) #define KSFT_BAD_MAGIC 0xdeadbeef From patchwork Wed Nov 22 09:42:41 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Brown X-Patchwork-Id: 13464425 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 800B7C61D9B for ; Wed, 22 Nov 2023 09:47:04 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 26DA86B05C8; Wed, 22 Nov 2023 04:47:04 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 21E486B05C9; Wed, 22 Nov 2023 04:47:04 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 10D526B05CA; Wed, 22 Nov 2023 04:47:04 -0500 (EST) 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 01C296B05C8 for ; Wed, 22 Nov 2023 04:47:03 -0500 (EST) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id CBA5DC0580 for ; Wed, 22 Nov 2023 09:47:03 +0000 (UTC) X-FDA: 81485111526.06.70AF0C8 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf13.hostedemail.com (Postfix) with ESMTP id F3AEC20027 for ; Wed, 22 Nov 2023 09:47:01 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=syOMsnFW; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf13.hostedemail.com: domain of broonie@kernel.org designates 139.178.84.217 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=1700646422; 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=2ozZbRaYs/+0MGSubF2558RGu/iy7kUlYOSh8sIY7uI=; b=h8SortpNejjIfZLMUrkHfqZZWpT1jgZ40LNKRIf1/y3c5yatmWZbHi8gHBy8xFR1EMHJC1 2sX7H6jwBSmM87UAaiQV+L9mlpPYVHSAqkCiMKLbsRYbgwDD6/yqvHoiaRvEMmBEtH8awF TJydRb/EQTQxZvSJ8ftNyd2BWx8fFoY= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=syOMsnFW; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf13.hostedemail.com: domain of broonie@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=broonie@kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1700646422; a=rsa-sha256; cv=none; b=nKSV/LVpn9DGDY08rzwKqQAykyyMa6ItX4XCWqbigT5vSj/zipg+LX5hLNsKnx2SjMVN4n jZ4lXmRxRVrwx6Vs7HwYi0MHzvMi0Gxdfy/W+dpJ3shhlSV3nERQSONa1VsZpFITc6qvL8 sAQhpT/hvqTZpzBs9g/cGqRjC6tJCzk= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 3817D61CCB; Wed, 22 Nov 2023 09:47:01 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6C568C4167D; Wed, 22 Nov 2023 09:46:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1700646421; bh=qxIqlAUMc2bBTJ1O51B6Tb6fnmxFBkD4x3sOYOI7QNE=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=syOMsnFWzOG+xmHUkIsaKs6NTpc6AaYK4nG1MDHbqXgrSkOqjN7up45LrEl5p7Kj8 Ym73t13eE56T+NFw9qfvMT6cKIN/58qjCmJqzZxAJ1kGvbKFLul5nd9dC6SQUz1jrA EXxSfZQgZcLsl/twjdlg0vE6CSiRLXxi/NK/28aJYlwlXlwyWmtSXRBJT/xwFsHoxv cnjdJbcrEHQku+7vy1h9M37JgKh9Q68/onZlKJEqYCHDVnv0PkCs/RRlNmSkgV+RF4 oOhzEu7XmVtkSX4RB82dUstRQ0AKsXe8scyUjoFXpHdqZZGidv6d7sY3xjsf23P0T0 9QQcGPMdtp0aw== From: Mark Brown Date: Wed, 22 Nov 2023 09:42:41 +0000 Subject: [PATCH v7 31/39] kselftest/arm64: Allow signals tests to specify an expected si_code MIME-Version: 1.0 Message-Id: <20231122-arm64-gcs-v7-31-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=2627; i=broonie@kernel.org; h=from:subject:message-id; bh=qxIqlAUMc2bBTJ1O51B6Tb6fnmxFBkD4x3sOYOI7QNE=; b=owEBbQGS/pANAwAKASTWi3JdVIfQAcsmYgBlXc0OiKQHWj96U6Iv+vrEb7owlLDF7A+UeVAJW s3BN30QZ7mJATMEAAEKAB0WIQSt5miqZ1cYtZ/in+ok1otyXVSH0AUCZV3NDgAKCRAk1otyXVSH 0Cj/B/4v4VwoMeU53pDf1fNeG//YL1WSnuk6c3A7i2dDESTIut5NK5DCoQTU6va5PSrNx6bHcHQ U209ci3F/Zr+o/E5MK0xtlUtuT0eeijo8MCPj04SF2s9C7Dh86AMZ5nTAvggAIsFDYLgQgOmNM+ XlQhib+aoBQStnNJmu+VdxLsNvBqIxdMk0E5RP7S8lMT9FNHmPXIsAfuYt4IpvN/kglnDyFWP2g AlBAMocbBXbx6N9D+rWLO3KOz7hiQ9AVkz/OUwp5hFzfb6hy96kQW529H3py/8jX/rxRH/DfOlw srt4yM6xjmvQ1TSj6Nvsge/pgI2bILXYvrzKkwYSC4BlkQsu X-Developer-Key: i=broonie@kernel.org; a=openpgp; fpr=3F2568AAC26998F9E813A1C5C3F436CA30F5D8EB X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: F3AEC20027 X-Stat-Signature: oem6hb4rj1tgxxerdaaauswbth5qoy41 X-Rspam-User: X-HE-Tag: 1700646421-963693 X-HE-Meta: U2FsdGVkX192YjwJ2g2/hI8tmCQYUOKM6NdjWK3BgD6YUN6GHvNiQ6RRteQCFKPFVjGeSj6vdbCrfqFpjGJZMeK0X+w+YWM3tIZzacViwbZwLorkqLgYF6Llh9eGaRBX5aG6JGj2281TyUU6+Dutu9boSdQqnamXgg/oqFNBJ1+vLZ322TiYTxdfQLrTLemTcS533VnM0Ky7MCPaPq47SsyJjhpCYDH/wGfrI/KeC86fN5B0Pu776Nz5CirvGBo5zp2+Z60cvs1TbFGLxfomkBxDO3AnpgMfXvSctOKZSm4lGZXj5BRC0+QI7c1CKvq9zCXHrP6MRqQz+QecORebbCO2qwM2mz62XoYN4WTQRuC677lGZ1QuzRcQccv7+nmZ/M2AxJcghm5mjcx9F9GWh4RfnAHxlJldmmy3VKLaG6lq3orxrl7HrMHc2ufU9Cp9aUBM+FRYNArDiertUu595jeWBr0GVxcTtKytIBsmx2N4Fgbkd5QSVJIPQ/llbShAm00aJldBsxQ2GJCBO0eFbd0Hk8sQNhMKkqPS3iVKMXh3bYz7iGYQhSdMPq5pjbmWR034EVKUzPD+ni9NCafh8oGhGxVhjObB8e1AiiAWM75sNV4vYMYvxLnI88rzZw1jzsgXaiWgT2J7LW6/RldWDgGEU/3zLvToIARHFHFmceDt6Kd5WZO03fEdldS81biOdjGEM+SV3p7onfx26lf6Prqiu8bchKT6q6d0v0ej7BA51LgAiOREvniAzWO9u1bHPOPEmM8amUUeZn2oRcSsvF32X6UinhFBYC81qopF5e5XWHrDwtNsH6q2YMXUEDh4f91WPjSsviddUrbXkZHoFGWo2TbFhBUOMGI1/r8kx+layNQikOKB6RVkCUgH/uP4hPvN59d2xQnYf4SG2DLbjXgYJ/GvlugCCgIQ6pRgBS1mmnTjZ+lSq2KY91tEKGROMyNFMOcvEYNsUPMnwMP UnGXiW0F zbq2kFxTamjOjln7T2F6Md8ISMKjKpwolXnj2+pZ/XyQBoLQgO7Tgh8IxVpgm28Rq9hf1EGvRVQC+FW+fZloHH7RtNq9pp54VAMNDU+kgUnPxaZSBdFbTveoyVlSttFmone2ocrGeUyI4XYIrSk1kEWEgJclbluDqfVvknoPeEbtX7SHq6CDlDq/Bub+7IduqsgoOu8KT4hSzASWtPBk2Eeg8lLkjh0XVZ6aAWyStsQ5otU1DdBQO0R9c1Q+wJAfh46uI3KGpXqeh4EBB+AI6PvuOzUgwP4YeOCgSUzDduGiwLZvwKqc53rLiilNIUwcss8Nb929E3xIe0Uormw6dqR/pbnnNuPOKRh3y4snoDg+ogR6c50cD6jRFTA== 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: Currently we ignore si_code unless the expected signal is a SIGSEGV, in which case we enforce it being SEGV_ACCERR. Allow test cases to specify exactly which si_code should be generated so we can validate this, and test for other segfault codes. Signed-off-by: Mark Brown --- .../testing/selftests/arm64/signal/test_signals.h | 4 +++ .../selftests/arm64/signal/test_signals_utils.c | 29 ++++++++++++++-------- 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/tools/testing/selftests/arm64/signal/test_signals.h b/tools/testing/selftests/arm64/signal/test_signals.h index 7ada43688c02..ee75a2c25ce7 100644 --- a/tools/testing/selftests/arm64/signal/test_signals.h +++ b/tools/testing/selftests/arm64/signal/test_signals.h @@ -71,6 +71,10 @@ struct tdescr { * Zero when no signal is expected on success */ int sig_ok; + /* + * expected si_code for sig_ok, or 0 to not check + */ + int sig_ok_code; /* signum expected on unsupported CPU features. */ int sig_unsupp; /* a timeout in second for test completion */ diff --git a/tools/testing/selftests/arm64/signal/test_signals_utils.c b/tools/testing/selftests/arm64/signal/test_signals_utils.c index 89ef95c1af0e..63deca32b0df 100644 --- a/tools/testing/selftests/arm64/signal/test_signals_utils.c +++ b/tools/testing/selftests/arm64/signal/test_signals_utils.c @@ -143,16 +143,25 @@ static bool handle_signal_ok(struct tdescr *td, "current->token ZEROED...test is probably broken!\n"); abort(); } - /* - * Trying to narrow down the SEGV to the ones generated by Kernel itself - * via arm64_notify_segfault(). This is a best-effort check anyway, and - * the si_code check may need to change if this aspect of the kernel - * ABI changes. - */ - if (td->sig_ok == SIGSEGV && si->si_code != SEGV_ACCERR) { - fprintf(stdout, - "si_code != SEGV_ACCERR...test is probably broken!\n"); - abort(); + if (td->sig_ok_code) { + if (si->si_code != td->sig_ok_code) { + fprintf(stdout, "si_code is %d not %d\n", + si->si_code, td->sig_ok_code); + abort(); + } + } else { + /* + * Trying to narrow down the SEGV to the ones + * generated by Kernel itself via + * arm64_notify_segfault(). This is a best-effort + * check anyway, and the si_code check may need to + * change if this aspect of the kernel ABI changes. + */ + if (td->sig_ok == SIGSEGV && si->si_code != SEGV_ACCERR) { + fprintf(stdout, + "si_code != SEGV_ACCERR...test is probably broken!\n"); + abort(); + } } td->pass = 1; /* From patchwork Wed Nov 22 09:42:42 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Brown X-Patchwork-Id: 13464426 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 E3C52C61D9B for ; Wed, 22 Nov 2023 09:47:13 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 888D26B05CA; Wed, 22 Nov 2023 04:47:13 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 839286B05CB; Wed, 22 Nov 2023 04:47:13 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 701416B05CC; Wed, 22 Nov 2023 04:47:13 -0500 (EST) 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 60CEE6B05CA for ; Wed, 22 Nov 2023 04:47:13 -0500 (EST) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 47D86B5B00 for ; Wed, 22 Nov 2023 09:47:13 +0000 (UTC) X-FDA: 81485111946.07.6ED838F Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by imf18.hostedemail.com (Postfix) with ESMTP id 508D11C001B for ; Wed, 22 Nov 2023 09:47:11 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=Y9kSWTAV; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf18.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=1700646431; 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=WdLZmli9vg8t6fe84vUBdKw/nM+D1l+ZQgyfXgsT10E=; b=OhqqU5/LWpdtULel67Ak4J2qp5XZp/nxmYsELjJfSc344jWuSOwdYdnhTVymCLH7BYqTs6 bCXb9Ki1nxdU4Cy5lQ4wCnMiRiulosvgcafVjPZyTfX4DGLlNI7N6YCle6RgZDfJi3F3z0 RfyFcgqRssSlBwP0j7TogThaOfzLfcQ= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=Y9kSWTAV; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf18.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=1700646431; a=rsa-sha256; cv=none; b=dw0FPkeKG3CmQpyQv5FxLRnoJAIpLtBRE4tQJFa4M5ovyXM1LdhdW6xTobjSp7rxTm5NjE BUjP/Zd7IFXpWLBw5Pcf4J6WQxVruytRV0iCtIxVKjFwa6V4QUI7Vwn4dKEcnlo6gdFxf4 6lfe2CRR9y9tcrK96pFREzjD+/lIvWQ= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by ams.source.kernel.org (Postfix) with ESMTP id CE428B81674; Wed, 22 Nov 2023 09:47:09 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 88DA2C433CB; Wed, 22 Nov 2023 09:47:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1700646429; bh=b3DUElLZ+Soh0Lccqy2ZSImkcM7zO9jMiCyb9nDJSwQ=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=Y9kSWTAVK98zhtVsyhDnKInGWdD30T07Ake/yR4jhC/+XLRShZE6i/o1V6NRBUhLz Dxrc74iwZWGjxvodTdGkItzPGoLKXLdxFS2Kgf26ynO1y5UO6mFbuGEYFRezPYZ3SN BJ6+Cpt7eYespf4hYmoUEvmoRFweNTPbyCFCCMewFKq5ZNXVlAgNurJYmvFMgcKAoz 6A5VkqVZkQux3aS2rdDhZOMzrDvsDBVF7gmWW8Wtj1s1MwxG9PDWzmbqkEHXCrQaYg nTZt+W8U2etS7jdzyurIciWhdn9ueX+xYQm+Qiv9r0ECdsUBfiYa9tDRxTCLuOOVc8 ssLs3BiYaclDw== From: Mark Brown Date: Wed, 22 Nov 2023 09:42:42 +0000 Subject: [PATCH v7 32/39] kselftest/arm64: Always run signals tests with GCS enabled MIME-Version: 1.0 Message-Id: <20231122-arm64-gcs-v7-32-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=3590; i=broonie@kernel.org; h=from:subject:message-id; bh=b3DUElLZ+Soh0Lccqy2ZSImkcM7zO9jMiCyb9nDJSwQ=; b=owEBbQGS/pANAwAKASTWi3JdVIfQAcsmYgBlXc0Pq69NiV0DZvuQOBOHTJq4Hskdu8ueWsh0A OXrPDovxWCJATMEAAEKAB0WIQSt5miqZ1cYtZ/in+ok1otyXVSH0AUCZV3NDwAKCRAk1otyXVSH 0AvKB/4gIm6EQumGj0Sww0FN0GocO/C+BLqTbwD6sziQA+cF0OfObYvSHOKFbMlhgZBDcbxRj+H 28uewK7t1SSYel2V8C+ykywrGEfBsUBjsriI5E5CfKOsPUhqtYwfEvHNoVySVobx5Rc1uodWehE wD4kxoZug4GzX0A1HdT4HPvZzCYUzCDXeyINi68DPqWrwit2CvDnkhjf8gGKqotnS4KnvHmhtcG C29Newlq2zS23qzht35JcPjfcn8Y1kR1IcN6GlS81iqTMkIEXYrjUAOl11+nDynG8e7kSOrapK7 71JfVueE4JJPVZeyyV05a7VJNsMwlN+yEcRjJzeJj1TAN/mD X-Developer-Key: i=broonie@kernel.org; a=openpgp; fpr=3F2568AAC26998F9E813A1C5C3F436CA30F5D8EB X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: 508D11C001B X-Stat-Signature: 6466a5qf4pubirksbc8inaoyq1dm5nu9 X-Rspam-User: X-HE-Tag: 1700646431-286131 X-HE-Meta: U2FsdGVkX180SLhwsj1Ug1TRGrbThn598Imzgg/guTzNCNzQqupskJO7lrpRL5uDC5GIWhqZ17OHl+AliRN8s1etO59jSo9sm7z2VBA3Kg20nGASTQM/9fu4iy9oEDxR73i/98QS9o3Akcum88r7PWpObHZ/ls8TkCgwTizwvt/rbOxjDiRHkmwUQ5Q2nccauILuTu7Dtkj4gqUTPcUvGsDAsQ5TXozE/i2fg7E2mlQy/CqcOm5LzcyDUC45NNbJY169lYJpV0DMWGXNMJ1QbWikGuDXaxy7PBKkHxt6JlBJH06MgK1op7A59rOUVVui2DYRJKo+3kpNRvLdJfCaQ+zAlHPx4beExlrTGSveOcHikSB+qDYsCVB2Y8oeQ1r5oTULP0irPhdtrcnJE512yKMRt9Sv7TfAasZOvczxmJHmM7iqvucH8baYQYeQ/5Uy4xUUdoduc6PjOAQ6GpH0mObqG9Gww5uYFCXKs+7/5Rc7esDJi7ScmRcQY5qn+guHuNemDiUDrb8NlMggsA6OVLH4bcV9vv+Loi9/gU0wIYqgNrNRuaSQfXMtmMlap6RJzymGLeYREFF04ZonaRMXS/guG2rdB3arfydFH2GDwS3Qk6jMh3mtYNUPnVHsP1GThXtEu3EBY7a1iPRqKQDplk2s2OoipsvHZ/N+bozUl0/lGdyNd3B+ox6A5FNCVmkf49+ZBMmgaD56sq+QZ/Y8L3Z1dfv9ebFx1O+AhToPHrR8Bzm7GmNJ+77Irmf0iagRE5SyPhJoXkM+7RzJg6uwfebOm0+ZBIKLLokwY2ysDfDuHA0GrKlrjKh2EJa0StF6rJiepFTxs43r/qGG0Nl/XODH2tg49MaO8EH1kqM7pIod3p2NJZXg1mBGc99tHz5gT/CEqjCTBhQMdOsi9nj9D/8BpbAhN5RVJ3SZL+HN9VWQoQpko3to3RuXF0riz5SdbQdkR45Z0JGxmxDFzLX q4ICUA0s 6TxUlPr4X88X4WlT00hK1tpdsdPDKfTvN0UpyKDlAgbUOsDsINjuYO6Z2BZAo/tHBE8aHChj++1kU04xebAWhFx47bkrBJum91V6XkqyJpJhkwzT9YDA4cJSvajkZkLjPHUJjbKmjx+Fyo4LLYPPcHtT0IZOuGAtK4UHPAiCOUtCfkCJcbUWbojxgmhdwS3Si/dMolrWExhfZuhebKngO9aGEhCsxN2CH5JWvLS/qkXrvfhfFPcf2RHNbgQTiK+eTetFE4B22VlRWMl8jMumjiy/t5uzb6cmTaAqjG/r8dwrHRDHkoh2wggqqSzHY1aKqZ2R2 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: Since it is not possible to return from the function that enabled GCS without disabling GCS it is very inconvenient to use the signal handling tests to cover GCS when GCS is not enabled by the toolchain and runtime, something that no current distribution does. Since none of the testcases do anything with stacks that would cause problems with GCS we can sidestep this issue by unconditionally enabling GCS on startup and exiting with a call to exit() rather than a return from main(). Signed-off-by: Mark Brown --- .../testing/selftests/arm64/signal/test_signals.c | 17 ++++++++++++- .../selftests/arm64/signal/test_signals_utils.h | 29 ++++++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/arm64/signal/test_signals.c b/tools/testing/selftests/arm64/signal/test_signals.c index 00051b40d71e..30e95f50db19 100644 --- a/tools/testing/selftests/arm64/signal/test_signals.c +++ b/tools/testing/selftests/arm64/signal/test_signals.c @@ -7,6 +7,10 @@ * Each test provides its own tde struct tdescr descriptor to link with * this wrapper. Framework provides common helpers. */ + +#include +#include + #include #include "test_signals.h" @@ -16,6 +20,16 @@ struct tdescr *current = &tde; int main(int argc, char *argv[]) { + /* + * Ensure GCS is at least enabled throughout the tests if + * supported, otherwise the inability to return from the + * function that enabled GCS makes it very inconvenient to set + * up test cases. The prctl() may fail if GCS was locked by + * libc setup code. + */ + if (getauxval(AT_HWCAP2) & HWCAP2_GCS) + gcs_set_state(PR_SHADOW_STACK_ENABLE); + ksft_print_msg("%s :: %s\n", current->name, current->descr); if (test_setup(current) && test_init(current)) { test_run(current); @@ -23,5 +37,6 @@ int main(int argc, char *argv[]) } test_result(current); - return current->result; + /* Do not return in case GCS was enabled */ + exit(current->result); } diff --git a/tools/testing/selftests/arm64/signal/test_signals_utils.h b/tools/testing/selftests/arm64/signal/test_signals_utils.h index 762c8fe9c54a..1e80808ee105 100644 --- a/tools/testing/selftests/arm64/signal/test_signals_utils.h +++ b/tools/testing/selftests/arm64/signal/test_signals_utils.h @@ -18,6 +18,35 @@ void test_cleanup(struct tdescr *td); int test_run(struct tdescr *td); void test_result(struct tdescr *td); +#ifndef __NR_prctl +#define __NR_prctl 167 +#endif + +/* + * The prctl takes 1 argument but we need to ensure that the other + * values passed in registers to the syscall are zero since the kernel + * validates them. + */ +#define gcs_set_state(state) \ + ({ \ + register long _num __asm__ ("x8") = __NR_prctl; \ + register long _arg1 __asm__ ("x0") = PR_SET_SHADOW_STACK_STATUS; \ + register long _arg2 __asm__ ("x1") = (long)(state); \ + register long _arg3 __asm__ ("x2") = 0; \ + register long _arg4 __asm__ ("x3") = 0; \ + register long _arg5 __asm__ ("x4") = 0; \ + \ + __asm__ volatile ( \ + "svc #0\n" \ + : "=r"(_arg1) \ + : "r"(_arg1), "r"(_arg2), \ + "r"(_arg3), "r"(_arg4), \ + "r"(_arg5), "r"(_num) \ + : "memory", "cc" \ + ); \ + _arg1; \ + }) + static inline bool feats_ok(struct tdescr *td) { if (td->feats_incompatible & td->feats_supported) From patchwork Wed Nov 22 09:42:43 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Brown X-Patchwork-Id: 13464427 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 392A4C61D9C for ; Wed, 22 Nov 2023 09:47:23 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id CCE516B05CC; Wed, 22 Nov 2023 04:47:22 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id C7F4D6B05CD; Wed, 22 Nov 2023 04:47:22 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id AF9AF6B05CE; Wed, 22 Nov 2023 04:47:22 -0500 (EST) 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 9F61A6B05CC for ; Wed, 22 Nov 2023 04:47:22 -0500 (EST) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 71AE7405F0 for ; Wed, 22 Nov 2023 09:47:22 +0000 (UTC) X-FDA: 81485112324.30.6D656D8 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by imf17.hostedemail.com (Postfix) with ESMTP id 65F894001B for ; Wed, 22 Nov 2023 09:47:20 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=mrQEru6U; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf17.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=1700646440; 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=IyLuedUWxIuVW+hBuiGvQQhpvyZuGLxwcGsJToJTFpw=; b=Owy+xEynQ/spkeXWjlHLyu6BRMRPp2T7uYKhMx319LNzbOY5gPBNTk0nazXdFjGqGcdi33 r2SCXGT26Fx/tg2FT8DkaA3kNMrm7YAcqSX3adblWUA/nikJCqw5CBG0v8KAJQoYtAwFH2 TVg/P/7RsoOQQ7A9M0X8FsFbF/+XCAQ= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=mrQEru6U; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf17.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=1700646440; a=rsa-sha256; cv=none; b=Bc16b0SmpzTSan4vxG4tPh3Fk8BY2pqSVT1hCVlczskplvw6nLdpc5+gWWarpjvzmlVcv9 XuThDu3SXgj7gjtgolw2x8YAVXEpQ57BQFcLkUj3v4EAngKGAtHA6pBy7o2UMeWIQMs36P BXz6I6/1JQS22As3/A2hmaPp4C+YJYI= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by ams.source.kernel.org (Postfix) with ESMTP id 0EE09B81183; Wed, 22 Nov 2023 09:47:19 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id EF758C433C9; Wed, 22 Nov 2023 09:47:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1700646438; bh=fd8/cfgrFfzpt2lwLCcC40w5n1WmV3tDKMJNBUAz6S0=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=mrQEru6UqcQG3kmh3zGDnmQlXjA0eIKg8zenwv1l/XR3Dg7szS339bGIZh9BFm1Yk P1AIfRE2XzOMbX/PuCKYsMmKkpi2972srVgCpJYYBG3wITIgz9Zyw55f9UNoj77FhE FzIlVixUqKmjSUPDH60z5vYW+0+kVgeP+7yW8Ul3Y4XOxJjc0HX38hmrh61CxuN9To ZUirmXZ7+WbL1G8x1dT/R0iZK00g1pWAkMjvt82ZhuLi5OmwZ6AK6JYpTsrEGrCZuv j4FgEzgFyZxCnExeVsUpmq0HzZcim0g8I5vplJmnHGwMsRA/3ohMGUGCyLiM9KN3Si TYzFXlBNsH5cQ== From: Mark Brown Date: Wed, 22 Nov 2023 09:42:43 +0000 Subject: [PATCH v7 33/39] kselftest/arm64: Add very basic GCS test program MIME-Version: 1.0 Message-Id: <20231122-arm64-gcs-v7-33-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=14970; i=broonie@kernel.org; h=from:subject:message-id; bh=fd8/cfgrFfzpt2lwLCcC40w5n1WmV3tDKMJNBUAz6S0=; b=owEBbQGS/pANAwAKASTWi3JdVIfQAcsmYgBlXc0PQh7pjrRYnlrLrpuindC5JLbPPt4Kx5awS WgIPTusXEuJATMEAAEKAB0WIQSt5miqZ1cYtZ/in+ok1otyXVSH0AUCZV3NDwAKCRAk1otyXVSH 0JY1B/96J7GYvZ9NL4qhsuoGQz6SomkCN+gDSk3QczolnszCDJVS+6leV5AOmBbCVzF+XJCMa9X XhHVTECyEGkrduwrTmdUcl+MsqxBPFKzuO44CkzP4dNWWUEWJNACeA9hYFM8WWi+8/2M36TX+zI y2bw/NJhTabfRyceyD9rZ7Tw1+7U1ipIJTeqU5P/BZxkWpCkMeWAPiXyQwA5UcBkINsbpQXX8lK 6K0P8pa9VNQlO4nUe6SFHNauimmxzM+v7U/+JdLFYghzg6kAm9fJbBOmZ2s0YsInb1RPrPOi/Uf +eb+bOFAbNltdxaTf3G6NqzvvTuI5vUBE0TrbZZgECFs3ICa X-Developer-Key: i=broonie@kernel.org; a=openpgp; fpr=3F2568AAC26998F9E813A1C5C3F436CA30F5D8EB X-Rspamd-Queue-Id: 65F894001B X-Rspam-User: X-Rspamd-Server: rspam02 X-Stat-Signature: 4u8u5q5rtyx3ismc1tj4o4ayzz1ahecp X-HE-Tag: 1700646440-208062 X-HE-Meta: U2FsdGVkX19e+ELu5m3+Mo73LOts6wJiUjPY4jyFRljZsVYs8n2csK8f3kRN1dBxUGAQ8OhbXto4N/1RWHcQBpZc3gaVXKD94FYZWRzIDNk7eVQKIndY0+EPJokBf+4q9WVPfUq4RTQJQ27CbsD3g7DrFPI3MtmAaCMlI3287L7d7x3TQW41xM1RGB1WeXcI2KWrMgioPWxXMLy0Mgqcg3wkyyQtmFq4qTwYP1eRlluKbCNrPpl+/RHd5H0AQIXr6i1Iz2jMKBPorn7wiZ4TSzp9UWHcq/1id1fJV9J4QfvxJW3B2vqKX0VfXej/j2SfsHHKcB0C4uZ5YbjqFM7Jww+SLKWrYUttkKtUYt1KELrhdEXqexinY2qObfhJJUuyVX86wXSklDPoWgM571LhaGPkEYcWCN/4Pxr3T+fuwNy09QKhaY4uwQsfYwuUCH5ckgQo0rB64G2ydqtRqDnyXt4imUFHd1fNHihnHAQypVjRe3p43/1SXbJTeOAEr5y+SuNwZY/5KbuWyY7CqirGtGdi1b9b1avAqy6P/MpiZvKCVp5mAUxmkwMKaWk643O2rhMNDxa8uCF2+z4/QcPqNrP9O8+rzC4iDcBSbSXv67Yslf26NAIiCMqpRwojhmDIMB/LokYezgDprtXHITAer+oBjtIPQGDWKuQ5TIQYlUwHdOM1ztAUfkURdfAVv8oWIIDw1oGtt/B++LG+bkc56ZlyhxhAY8ChEpo9kMB0kFom/i+6B6CkuQzq4hk1FYLdKH/iS/kC2Mgj573dqnWhvVhnoNBuDfwD7z2cUKj39oxddQQZQTaAcPbhKUAB7NE8DmDZkoD/DiCcEYz9O3lBAw8gjV/2kRjH1Bfs5k+M8hVbeDYAiw1jFEI3Ob7eDa8unSWPkL+lifJkggUuCbZ9Pej6+1OX6VAG5rfDQmTGSsq6loWR/LtnpHlEKXBi8GNRzqSzfCtFZLURu1DwCZh nsMAqg/M JSNygX6fJ+q74Q3jnqVh5UEB/vaqiKjYVVul4176BCfrJa/Bh+WCSn26fKGkI19g7bVTAnQBXwMv4jXba+TmqnXKaGMSK2KwbfX1OAuqYFkUUQbAxc7NKWRMiAr+2Hv8a2v5JwYvG2Jr77ist5BAGK/pb2c//v6o8fo3M991+NYHwwS2axpqO7+2Lm6e7kiUVR4Cq8ivt7SNPAI7NuS086V/mDDByXgcxQpOzaze8EqKJ+46S15GpIOAmYQEnilUusFBAD9BTJb/395WzF9Kjo0+nGfmUhbqybXpNadyD3PW8xqgzWfziMPu+1gmlpq0GDO/g 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 test program just covers the basic GCS ABI, covering aspects of the ABI as standalone features without attempting to integrate things. Signed-off-by: Mark Brown --- tools/testing/selftests/arm64/Makefile | 2 +- tools/testing/selftests/arm64/gcs/.gitignore | 1 + tools/testing/selftests/arm64/gcs/Makefile | 18 ++ tools/testing/selftests/arm64/gcs/basic-gcs.c | 428 ++++++++++++++++++++++++++ tools/testing/selftests/arm64/gcs/gcs-util.h | 90 ++++++ 5 files changed, 538 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/arm64/Makefile b/tools/testing/selftests/arm64/Makefile index 28b93cab8c0d..22029e60eff3 100644 --- a/tools/testing/selftests/arm64/Makefile +++ b/tools/testing/selftests/arm64/Makefile @@ -4,7 +4,7 @@ ARCH ?= $(shell uname -m 2>/dev/null || echo not) ifneq (,$(filter $(ARCH),aarch64 arm64)) -ARM64_SUBTARGETS ?= tags signal pauth fp mte bti abi +ARM64_SUBTARGETS ?= tags signal pauth fp mte bti abi gcs else ARM64_SUBTARGETS := endif diff --git a/tools/testing/selftests/arm64/gcs/.gitignore b/tools/testing/selftests/arm64/gcs/.gitignore new file mode 100644 index 000000000000..0e5e695ecba5 --- /dev/null +++ b/tools/testing/selftests/arm64/gcs/.gitignore @@ -0,0 +1 @@ +basic-gcs diff --git a/tools/testing/selftests/arm64/gcs/Makefile b/tools/testing/selftests/arm64/gcs/Makefile new file mode 100644 index 000000000000..61a30f483429 --- /dev/null +++ b/tools/testing/selftests/arm64/gcs/Makefile @@ -0,0 +1,18 @@ +# SPDX-License-Identifier: GPL-2.0 +# Copyright (C) 2023 ARM Limited +# +# In order to avoid interaction with the toolchain and dynamic linker the +# portions of these tests that interact with the GCS are implemented using +# nolibc. +# + +TEST_GEN_PROGS := basic-gcs + +include ../../lib.mk + +$(OUTPUT)/basic-gcs: basic-gcs.c + $(CC) -g -fno-asynchronous-unwind-tables -fno-ident -s -Os -nostdlib \ + -static -include ../../../../include/nolibc/nolibc.h \ + -I../../../../../usr/include \ + -std=gnu99 -I../.. -g \ + -ffreestanding -Wall $^ -o $@ -lgcc diff --git a/tools/testing/selftests/arm64/gcs/basic-gcs.c b/tools/testing/selftests/arm64/gcs/basic-gcs.c new file mode 100644 index 000000000000..3ef957e0065f --- /dev/null +++ b/tools/testing/selftests/arm64/gcs/basic-gcs.c @@ -0,0 +1,428 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright (C) 2023 ARM Limited. + */ + +#include +#include + +#include + +#include +#include +#include + +#include "kselftest.h" +#include "gcs-util.h" + +/* nolibc doesn't have sysconf(), just hard code the maximum */ +static size_t page_size = 65536; + +static __attribute__((noinline)) void valid_gcs_function(void) +{ + /* Do something the compiler can't optimise out */ + my_syscall1(__NR_prctl, PR_SVE_GET_VL); +} + +static inline int gcs_set_status(unsigned long mode) +{ + bool enabling = mode & PR_SHADOW_STACK_ENABLE; + int ret; + unsigned long new_mode; + + /* + * The prctl takes 1 argument but we need to ensure that the + * other 3 values passed in registers to the syscall are zero + * since the kernel validates them. + */ + ret = my_syscall5(__NR_prctl, PR_SET_SHADOW_STACK_STATUS, mode, + 0, 0, 0); + + if (ret == 0) { + ret = my_syscall5(__NR_prctl, PR_GET_SHADOW_STACK_STATUS, + &new_mode, 0, 0, 0); + if (ret == 0) { + if (new_mode != mode) { + ksft_print_msg("Mode set to %x not %x\n", + new_mode, mode); + ret = -EINVAL; + } + } else { + ksft_print_msg("Failed to validate mode: %d\n", ret); + } + + if (enabling != chkfeat_gcs()) { + ksft_print_msg("%senabled by prctl but %senabled in CHKFEAT\n", + enabling ? "" : "not ", + chkfeat_gcs() ? "" : "not "); + ret = -EINVAL; + } + } + + return ret; +} + +/* Try to read the status */ +static bool read_status(void) +{ + unsigned long state; + int ret; + + ret = my_syscall5(__NR_prctl, PR_GET_SHADOW_STACK_STATUS, + &state, 0, 0, 0); + if (ret != 0) { + ksft_print_msg("Failed to read state: %d\n", ret); + return false; + } + + return state & PR_SHADOW_STACK_ENABLE; +} + +/* Just a straight enable */ +static bool base_enable(void) +{ + int ret; + + ret = gcs_set_status(PR_SHADOW_STACK_ENABLE); + if (ret) { + ksft_print_msg("PR_SHADOW_STACK_ENABLE failed %d\n", ret); + return false; + } + + return true; +} + +/* Check we can read GCSPR_EL0 when GCS is enabled */ +static bool read_gcspr_el0(void) +{ + unsigned long *gcspr_el0; + + ksft_print_msg("GET GCSPR\n"); + gcspr_el0 = get_gcspr(); + ksft_print_msg("GCSPR_EL0 is %p\n", gcspr_el0); + + return true; +} + +/* Also allow writes to stack */ +static bool enable_writeable(void) +{ + int ret; + + ret = gcs_set_status(PR_SHADOW_STACK_ENABLE | PR_SHADOW_STACK_WRITE); + if (ret) { + ksft_print_msg("PR_SHADOW_STACK_ENABLE writeable failed: %d\n", ret); + return false; + } + + ret = gcs_set_status(PR_SHADOW_STACK_ENABLE); + if (ret) { + ksft_print_msg("failed to restore plain enable %d\n", ret); + return false; + } + + return true; +} + +/* Also allow writes to stack */ +static bool enable_push_pop(void) +{ + int ret; + + ret = gcs_set_status(PR_SHADOW_STACK_ENABLE | PR_SHADOW_STACK_PUSH); + if (ret) { + ksft_print_msg("PR_SHADOW_STACK_ENABLE with push failed: %d\n", + ret); + return false; + } + + ret = gcs_set_status(PR_SHADOW_STACK_ENABLE); + if (ret) { + ksft_print_msg("failed to restore plain enable %d\n", ret); + return false; + } + + return true; +} + +/* Enable GCS and allow everything */ +static bool enable_all(void) +{ + int ret; + + ret = gcs_set_status(PR_SHADOW_STACK_ENABLE | PR_SHADOW_STACK_PUSH | + PR_SHADOW_STACK_WRITE); + if (ret) { + ksft_print_msg("PR_SHADOW_STACK_ENABLE with everything failed: %d\n", + ret); + return false; + } + + ret = gcs_set_status(PR_SHADOW_STACK_ENABLE); + if (ret) { + ksft_print_msg("failed to restore plain enable %d\n", ret); + return false; + } + + return true; +} + +static bool enable_invalid(void) +{ + int ret = gcs_set_status(ULONG_MAX); + if (ret == 0) { + ksft_print_msg("GCS_SET_STATUS %lx succeeded\n", ULONG_MAX); + return false; + } + + return true; +} + +/* Map a GCS */ +static bool map_guarded_stack(void) +{ + int ret; + uint64_t *buf; + uint64_t expected_cap; + int elem; + bool pass = true; + + buf = (void *)my_syscall3(__NR_map_shadow_stack, 0, page_size, + SHADOW_STACK_SET_MARKER | + SHADOW_STACK_SET_TOKEN); + if (buf == MAP_FAILED) { + ksft_print_msg("Failed to map %d byte GCS: %d\n", + page_size, errno); + return false; + } + ksft_print_msg("Mapped GCS at %p-%p\n", buf, + (uint64_t)buf + page_size); + + /* The top of the newly allocated region should be 0 */ + elem = (page_size / sizeof(uint64_t)) - 1; + if (buf[elem]) { + ksft_print_msg("Last entry is 0x%lx not 0x0\n", buf[elem]); + pass = false; + } + + /* Then a valid cap token */ + elem--; + expected_cap = ((uint64_t)buf + page_size - 16); + expected_cap &= GCS_CAP_ADDR_MASK; + expected_cap |= GCS_CAP_VALID_TOKEN; + if (buf[elem] != expected_cap) { + ksft_print_msg("Cap entry is 0x%lx not 0x%lx\n", + buf[elem], expected_cap); + pass = false; + } + ksft_print_msg("cap token is 0x%lx\n", buf[elem]); + + /* The rest should be zeros */ + for (elem = 0; elem < page_size / sizeof(uint64_t) - 2; elem++) { + if (!buf[elem]) + continue; + ksft_print_msg("GCS slot %d is 0x%lx not 0x0\n", + elem, buf[elem]); + pass = false; + } + + ret = munmap(buf, page_size); + if (ret != 0) { + ksft_print_msg("Failed to unmap %d byte GCS: %d\n", + page_size, errno); + pass = false; + } + + return pass; +} + +/* A fork()ed process can run */ +static bool test_fork(void) +{ + unsigned long child_mode; + int ret, status; + pid_t pid; + bool pass = true; + + pid = fork(); + if (pid == -1) { + ksft_print_msg("fork() failed: %d\n", errno); + pass = false; + goto out; + } + if (pid == 0) { + /* In child, make sure we can call a function, read + * the GCS pointer and status and then exit */ + valid_gcs_function(); + get_gcspr(); + + ret = my_syscall5(__NR_prctl, PR_GET_SHADOW_STACK_STATUS, + &child_mode, 0, 0, 0); + if (ret == 0 && !(child_mode & PR_SHADOW_STACK_ENABLE)) { + ksft_print_msg("GCS not enabled in child\n"); + ret = -EINVAL; + } + + exit(ret); + } + + /* + * In parent, check we can still do function calls then block + * for the child. + */ + valid_gcs_function(); + + ksft_print_msg("Waiting for child %d\n", pid); + + ret = waitpid(pid, &status, 0); + if (ret == -1) { + ksft_print_msg("Failed to wait for child: %d\n", + errno); + return false; + } + + if (!WIFEXITED(status)) { + ksft_print_msg("Child exited due to signal %d\n", + WTERMSIG(status)); + pass = false; + } else { + if (WEXITSTATUS(status)) { + ksft_print_msg("Child exited with status %d\n", + WEXITSTATUS(status)); + pass = false; + } + } + +out: + + return pass; +} + +/* Check that we can explicitly specify a GCS via clone3() */ +static bool test_clone3(void) +{ + struct clone_args args; + unsigned long child_mode; + pid_t pid = -1; + int status, ret; + bool pass; + + memset(&args, 0, sizeof(args)); + args.flags = CLONE_VM; + args.shadow_stack_size = page_size; + + pid = my_syscall2(__NR_clone3, &args, sizeof(args)); + if (pid < 0) { + ksft_print_msg("clone3() failed: %d\n", errno); + pass = false; + goto out; + } + + /* In child? */ + if (pid == 0) { + /* Do we have GCS enabled? */ + ret = my_syscall5(__NR_prctl, PR_GET_SHADOW_STACK_STATUS, + &child_mode, 0, 0, 0); + if (ret != 0) { + ksft_print_msg("PR_GET_SHADOW_STACK_STATUS failed: %d\n", + ret); + exit(EXIT_FAILURE); + } + + if (!(child_mode & PR_SHADOW_STACK_ENABLE)) { + ksft_print_msg("GCS not enabled in child\n"); + exit(EXIT_FAILURE); + } + + ksft_print_msg("GCS enabled in child\n"); + + /* We've probably already called a function but make sure */ + valid_gcs_function(); + + exit(EXIT_SUCCESS); + } + + if (waitpid(-1, &status, __WALL) < 0) { + ksft_print_msg("waitpid() failed %d\n", errno); + pass = false; + goto out; + } + if (WIFEXITED(status)) { + if (WEXITSTATUS(status) == EXIT_SUCCESS) { + pass = true; + } else { + ksft_print_msg("Child returned status %d\n", + WEXITSTATUS(status)); + pass = false; + } + } else if (WIFSIGNALED(status)) { + ksft_print_msg("Child exited due to signal %d\n", + WTERMSIG(status)); + pass = false; + } else { + ksft_print_msg("Child exited uncleanly\n"); + pass = false; + } + +out: + return pass; +} + +typedef bool (*gcs_test)(void); + +static struct { + char *name; + gcs_test test; + bool needs_enable; +} tests[] = { + { "read_status", read_status }, + { "base_enable", base_enable, true }, + { "read_gcspr_el0", read_gcspr_el0 }, + { "enable_writeable", enable_writeable, true }, + { "enable_push_pop", enable_push_pop, true }, + { "enable_all", enable_all, true }, + { "enable_invalid", enable_invalid, true }, + { "map_guarded_stack", map_guarded_stack }, + { "fork", test_fork }, + { "clone3", test_clone3 }, +}; + +int main(void) +{ + int i, ret; + unsigned long gcs_mode; + + ksft_print_header(); + + /* + * We don't have getauxval() with nolibc so treat a failure to + * read GCS state as a lack of support and skip. + */ + ret = my_syscall5(__NR_prctl, PR_GET_SHADOW_STACK_STATUS, + &gcs_mode, 0, 0, 0); + if (ret != 0) + ksft_exit_skip("Failed to read GCS state: %d\n", ret); + + if (!(gcs_mode & PR_SHADOW_STACK_ENABLE)) { + gcs_mode = PR_SHADOW_STACK_ENABLE; + ret = my_syscall5(__NR_prctl, PR_SET_SHADOW_STACK_STATUS, + gcs_mode, 0, 0, 0); + if (ret != 0) + ksft_exit_fail_msg("Failed to enable GCS: %d\n", ret); + } + + ksft_set_plan(ARRAY_SIZE(tests)); + + for (i = 0; i < ARRAY_SIZE(tests); i++) { + ksft_test_result((*tests[i].test)(), "%s\n", tests[i].name); + } + + /* One last test: disable GCS, we can do this one time */ + my_syscall5(__NR_prctl, PR_SET_SHADOW_STACK_STATUS, 0, 0, 0, 0); + if (ret != 0) + ksft_print_msg("Failed to disable GCS: %d\n", ret); + + ksft_finished(); + + return 0; +} diff --git a/tools/testing/selftests/arm64/gcs/gcs-util.h b/tools/testing/selftests/arm64/gcs/gcs-util.h new file mode 100644 index 000000000000..b37801c95604 --- /dev/null +++ b/tools/testing/selftests/arm64/gcs/gcs-util.h @@ -0,0 +1,90 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * Copyright (C) 2023 ARM Limited. + */ + +#ifndef GCS_UTIL_H +#define GCS_UTIL_H + +#include + +#ifndef __NR_map_shadow_stack +#define __NR_map_shadow_stack 453 +#endif + +#ifndef __NR_prctl +#define __NR_prctl 167 +#endif + +/* Shadow Stack/Guarded Control Stack interface */ +#define PR_GET_SHADOW_STACK_STATUS 71 +#define PR_SET_SHADOW_STACK_STATUS 72 +#define PR_LOCK_SHADOW_STACK_STATUS 73 + +# define PR_SHADOW_STACK_ENABLE (1UL << 0) +# define PR_SHADOW_STACK_WRITE (1UL << 1) +# define PR_SHADOW_STACK_PUSH (1UL << 2) + +#define PR_SHADOW_STACK_ALL_MODES \ + PR_SHADOW_STACK_ENABLE | PR_SHADOW_STACK_WRITE | PR_SHADOW_STACK_PUSH + +#define SHADOW_STACK_SET_TOKEN (1ULL << 0) /* Set up a restore token in the shadow stack */ +#define SHADOW_STACK_SET_MARKER (1ULL << 1) /* Set up a top of stack merker in the shadow stack */ + +#define GCS_CAP_ADDR_MASK (0xfffffffffffff000UL) +#define GCS_CAP_TOKEN_MASK (0x0000000000000fffUL) +#define GCS_CAP_VALID_TOKEN 1 +#define GCS_CAP_IN_PROGRESS_TOKEN 5 + +#define GCS_CAP(x) (((unsigned long)(x) & GCS_CAP_ADDR_MASK) | \ + GCS_CAP_VALID_TOKEN) + +static inline unsigned long *get_gcspr(void) +{ + unsigned long *gcspr; + + asm volatile( + "mrs %0, S3_3_C2_C5_1" + : "=r" (gcspr) + : + : "cc"); + + return gcspr; +} + +static inline void __attribute__((always_inline)) gcsss1(unsigned long *Xt) +{ + asm volatile ( + "sys #3, C7, C7, #2, %0\n" + : + : "rZ" (Xt) + : "memory"); +} + +static inline unsigned long __attribute__((always_inline)) *gcsss2(void) +{ + unsigned long *Xt; + + asm volatile( + "SYSL %0, #3, C7, C7, #3\n" + : "=r" (Xt) + : + : "memory"); + + return Xt; +} + +static inline bool chkfeat_gcs(void) +{ + register long val __asm__ ("x16") = 1; + + /* CHKFEAT x16 */ + asm volatile( + "hint #0x28\n" + : "=r" (val) + : "r" (val)); + + return val != 1; +} + +#endif From patchwork Wed Nov 22 09:42:44 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Brown X-Patchwork-Id: 13464428 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 2F916C61D9C for ; Wed, 22 Nov 2023 09:47:34 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C81266B05CE; Wed, 22 Nov 2023 04:47:33 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id C30EA6B05CF; Wed, 22 Nov 2023 04:47:33 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id AD17E6B05D0; Wed, 22 Nov 2023 04:47:33 -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 996596B05CE for ; Wed, 22 Nov 2023 04:47:33 -0500 (EST) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 6B63F80645 for ; Wed, 22 Nov 2023 09:47:33 +0000 (UTC) X-FDA: 81485112786.16.8777AC4 Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by imf06.hostedemail.com (Postfix) with ESMTP id 09B2F180018 for ; Wed, 22 Nov 2023 09:47:30 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=fUwhc6da; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf06.hostedemail.com: domain of broonie@kernel.org designates 145.40.73.55 as permitted sender) smtp.mailfrom=broonie@kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1700646451; a=rsa-sha256; cv=none; b=1Ce/1zxt22z84hdZYtooFlNl+XrHasLFz/Bi8j7xK6oQV8vI3pWSZ2eSqaPc65F9kJrjDk hkCD1/esq7OqVJkmaztrT10tXlgSjg1n9tFBV9lIrMbNzYCatSN7nQ8mOKZaFf4JvCNrvp dDaKo19L6uNmjxe+qxiJGSuq2Sei6O8= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=fUwhc6da; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf06.hostedemail.com: domain of broonie@kernel.org designates 145.40.73.55 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=1700646451; 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=Lzalqc9txhhm+/4hWLLdvtEyxOk3CueDCg1LBC6Rb2M=; b=HjcSpBRyKiN511snc4KWKvk1v72kijxCU77h49nd8+BdWDiTqXvrFwLMnob6uCMson2V6S POzQSNQTkmeQ/UvElmePTf4ASAC9JTpXJzJ3BjoHxA/pGeh3ira4c4KKD1dW9WdjYGiFGA 2rsBGsvXVfdTKgfJrycdufDjK67QvHo= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sin.source.kernel.org (Postfix) with ESMTP id 1579FCE1EC1; Wed, 22 Nov 2023 09:47:28 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id DBF92C433C8; Wed, 22 Nov 2023 09:47:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1700646447; bh=sEl9MC/DNin35BZFtSi9ZbFCRBERfcoa8BWyWFkgIWs=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=fUwhc6da4HCUQ1JOyKirZiHK2/ANZrrzC4GfdOdjuxw1++Bvu/NPAks/8twja0DQR zws3cwSCHEfo0kOz55Xzp1swY+xP4eN3m+Ns6NbaN4/z+fOyFV+SoPWdjiHhEkrwpp azekmZi61JlG9VeBGnD62iVW7AgIJQjpTighx6gj8h/hTue2+sajCxlYF4qCbyElfq OaucMa/GBhKQxAm0BuhuivNyfu/CA0/OnXwPIF2B4Vd376Khc2TmSbIiBAM4MKgguf Zf8kR1LYqAO2J8F3+rK2G4OBWeBr+NMzRJmGzVXLgdGjkR3rGqfnRwiaDGbsJLUQBk GfGj5EqPEaYsg== From: Mark Brown Date: Wed, 22 Nov 2023 09:42:44 +0000 Subject: [PATCH v7 34/39] kselftest/arm64: Add a GCS test program built with the system libc MIME-Version: 1.0 Message-Id: <20231122-arm64-gcs-v7-34-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=21534; i=broonie@kernel.org; h=from:subject:message-id; bh=sEl9MC/DNin35BZFtSi9ZbFCRBERfcoa8BWyWFkgIWs=; b=owEBbQGS/pANAwAKASTWi3JdVIfQAcsmYgBlXc0QU2t12cNjneFFrmYQPVNXh0mD+p4w3pRe5 sa6Q9N29buJATMEAAEKAB0WIQSt5miqZ1cYtZ/in+ok1otyXVSH0AUCZV3NEAAKCRAk1otyXVSH 0FR1B/9dCNVB8gGgvRug+3mlZdIZTvcfwrzGaPPt/CkX/qYvH4UWG3/jz+yNQkgi38N4lNoBqxC CTvEmVhbj2r8hZY7pWEg3TvWDH88a4ppv8L4muH07peqmUxeeaxE6WtNDv07+1j6UaOtNGkbGet F4Bdh2Xr/vTMfohH/OLbEWCHgmgPRyWVvvry2CqVmnRZhHHbFHD2TWey93+K4I1umx/xXwg9wU/ Gssvk355I2czO73nbnCYPi3BxFbVYih4w/ItEed0/WPHV57XCJ29i/X1FuURaw5F6iMP5pXSpHH yV3nhxLQMIgnq1VPC0zDQrksrApM9DmcMbHX403nXGON2CTG X-Developer-Key: i=broonie@kernel.org; a=openpgp; fpr=3F2568AAC26998F9E813A1C5C3F436CA30F5D8EB X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 09B2F180018 X-Stat-Signature: gojja74zsefngur1nsgqnh1j14yngymd X-HE-Tag: 1700646450-575198 X-HE-Meta: U2FsdGVkX1/Ab4NLn4Ja3Zzhzf1+116tDeSpHFidJmdFuAmvRjYgzOnCYIJOE2P0HiMyRozNc4vZr066obbleix7fSmx8SBJp3cEUc3wy1YnGBBb3Ibh/hu9Ohk+g72/d0QP7aDP+7oHwgGj5pCVlceL8MRvf0ttz3MFIZu387tBj7FQsIl2qRXQhB8cflS+F4LvtrFhMq9E3PPYHO8TK78lqomNWrSOL9FkYajg+H77CMJwmtNCoXhfk8FvbjQ1UM+Ph7kBxrlC/agmGL+9NdlJvZX+mHOu1bCs1V1wwLFH+aZFS252uQWT18kFYvmK8sSfi272bEasaxclmYs+AQ601CcrVbLndeJAtq42g6KUnTCeYBML/Oqn2D82S6Bz8g6UcVGIK9TXspeY00j6O3rRxJeFFA4QoY2Lib94hZ4e2+WZxoo9VHCu5xouCpgS/9DX/C00hYXlSK2la4teBM5xVR6rg8h7Ut+BmpXuxFA/zdXJFywxNYlRJ+sIPz3XYu1MB/ZeEahoG86bX4P0MPtojA1h7mrDIlTY+sw+xkuTVJqEjJuoNtE0EUeYlxqZTYMot4QtnkU6y8yartdXZLjPh44ChJOZXjSzfPAlQdPr9h8yr9qpJROTQVchVh69LFrlgcxRE0xAIYaE5kmk1bRRmo5BDQEfisToT5kZaVzM7nO2rPTsKfsfyQ+fbv/cfFOzm8h/S5ZqqZrmEhdUafxrqJdYj2jphhHyO9KV9Ik0F8TqeYmkkedntEk1Q+oaEnIA8HhB5GYn1FF5LXHBMQND220EiduPn/j8iVa8lNnd0NlPhOV7vEW6jarUrNQsMt4TxHY/5eEI8+3njFTKykt/pL8gy8XtQe3fZSMtGjwNXdkCbFqC/9clOGfZ8qrO8nlj3FpZaHnWVJt0p/za7Q6ktpceRuXuy5EyfLMMtO84kYMjcRIduWR9ZEKZjTLFOnX9z9k2gQc+AlSWJe+ uUDVWtjG s6FJGRhwvSonoDIg2dyS0wJMUe40Mqlue4S0HoxiINU/vFU9PnAGenk9qmzcZEDblbqR9u1PNbersHx9W7gHrxp8cxZ1kD2S3O7StX158jeCLKaOntnYP+sislwUNetweghFhHhWxczyxUJlxWz+6eO/m8VBdAIppoaOWc0A9q6ncNrE+922glSA/AktfrONLS37S2NuxJt2orNgu7pQDf7Ug7BLGZQklig7wbl5XsOSaaNryVy8veMRuYzMWAqhN87iFJmWcKBQhWBibn5APyYXiIayjBGqgiM4J/RggGLEiNniiXDh6qa3uAE6DZ9c82ziG 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: There are things like threads which nolibc struggles with which we want to add coverage for, and the ABI allows us to test most of these even if libc itself does not understand GCS so add a test application built using the system libc. Signed-off-by: Mark Brown --- tools/testing/selftests/arm64/gcs/.gitignore | 1 + tools/testing/selftests/arm64/gcs/Makefile | 4 +- tools/testing/selftests/arm64/gcs/gcs-util.h | 10 + tools/testing/selftests/arm64/gcs/libc-gcs.c | 742 +++++++++++++++++++++++++++ 4 files changed, 756 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/arm64/gcs/.gitignore b/tools/testing/selftests/arm64/gcs/.gitignore index 0e5e695ecba5..5810c4a163d4 100644 --- a/tools/testing/selftests/arm64/gcs/.gitignore +++ b/tools/testing/selftests/arm64/gcs/.gitignore @@ -1 +1,2 @@ basic-gcs +libc-gcs diff --git a/tools/testing/selftests/arm64/gcs/Makefile b/tools/testing/selftests/arm64/gcs/Makefile index 61a30f483429..a8fdf21e9a47 100644 --- a/tools/testing/selftests/arm64/gcs/Makefile +++ b/tools/testing/selftests/arm64/gcs/Makefile @@ -6,7 +6,9 @@ # nolibc. # -TEST_GEN_PROGS := basic-gcs +TEST_GEN_PROGS := basic-gcs libc-gcs + +LDLIBS+=-lpthread include ../../lib.mk diff --git a/tools/testing/selftests/arm64/gcs/gcs-util.h b/tools/testing/selftests/arm64/gcs/gcs-util.h index b37801c95604..4bafd1d7feb5 100644 --- a/tools/testing/selftests/arm64/gcs/gcs-util.h +++ b/tools/testing/selftests/arm64/gcs/gcs-util.h @@ -16,6 +16,16 @@ #define __NR_prctl 167 #endif +#ifndef NT_ARM_GCS +#define NT_ARM_GCS 0x40e + +struct user_gcs { + __u64 features_enabled; + __u64 features_locked; + __u64 gcspr_el0; +}; +#endif + /* Shadow Stack/Guarded Control Stack interface */ #define PR_GET_SHADOW_STACK_STATUS 71 #define PR_SET_SHADOW_STACK_STATUS 72 diff --git a/tools/testing/selftests/arm64/gcs/libc-gcs.c b/tools/testing/selftests/arm64/gcs/libc-gcs.c new file mode 100644 index 000000000000..58141d46f861 --- /dev/null +++ b/tools/testing/selftests/arm64/gcs/libc-gcs.c @@ -0,0 +1,742 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright (C) 2023 ARM Limited. + */ + +#define _GNU_SOURCE + +#include +#include + +#include +#include +#include +#include +#include + +#include +#include + +#include + +#include "kselftest_harness.h" + +#include "gcs-util.h" + +#define my_syscall2(num, arg1, arg2) \ +({ \ + register long _num __asm__ ("x8") = (num); \ + register long _arg1 __asm__ ("x0") = (long)(arg1); \ + register long _arg2 __asm__ ("x1") = (long)(arg2); \ + register long _arg3 __asm__ ("x2") = 0; \ + register long _arg4 __asm__ ("x3") = 0; \ + register long _arg5 __asm__ ("x4") = 0; \ + \ + __asm__ volatile ( \ + "svc #0\n" \ + : "=r"(_arg1) \ + : "r"(_arg1), "r"(_arg2), \ + "r"(_arg3), "r"(_arg4), \ + "r"(_arg5), "r"(_num) \ + : "memory", "cc" \ + ); \ + _arg1; \ +}) + +static noinline void gcs_recurse(int depth) +{ + if (depth) + gcs_recurse(depth - 1); + + /* Prevent tail call optimization so we actually recurse */ + asm volatile("dsb sy" : : : "memory"); +} + +/* Smoke test that a function call and return works*/ +TEST(can_call_function) +{ + gcs_recurse(0); +} + +static void *gcs_test_thread(void *arg) +{ + int ret; + unsigned long mode; + + /* + * Some libcs don't seem to fill unused arguments with 0 but + * the kernel validates this so we supply all 5 arguments. + */ + ret = prctl(PR_GET_SHADOW_STACK_STATUS, &mode, 0, 0, 0); + if (ret != 0) { + ksft_print_msg("PR_GET_SHADOW_STACK_STATUS failed: %d\n", ret); + return NULL; + } + + if (!(mode & PR_SHADOW_STACK_ENABLE)) { + ksft_print_msg("GCS not enabled in thread, mode is %u\n", + mode); + return NULL; + } + + /* Just in case... */ + gcs_recurse(0); + + /* Use a non-NULL value to indicate a pass */ + return &gcs_test_thread; +} + +/* Verify that if we start a new thread it has GCS enabled */ +TEST(gcs_enabled_thread) +{ + pthread_t thread; + void *thread_ret; + int ret; + + ret = pthread_create(&thread, NULL, gcs_test_thread, NULL); + ASSERT_TRUE(ret == 0); + if (ret != 0) + return; + + ret = pthread_join(thread, &thread_ret); + ASSERT_TRUE(ret == 0); + if (ret != 0) + return; + + ASSERT_TRUE(thread_ret != NULL); +} + +/* Read the GCS until we find the terminator */ +TEST(gcs_find_terminator) +{ + unsigned long *gcs, *cur; + + gcs = get_gcspr(); + cur = gcs; + while (*cur) + cur++; + + ksft_print_msg("GCS in use from %p-%p\n", gcs, cur); + + /* + * We should have at least whatever called into this test so + * the two pointer should differ. + */ + ASSERT_TRUE(gcs != cur); +} + +/* + * We can access a GCS via ptrace + * + * This could usefully have a fixture but note that each test is + * fork()ed into a new child whcih causes issues. Might be better to + * lift at least some of this out into a separate, non-harness, test + * program. + */ +TEST(ptrace_read_write) +{ + pid_t child, pid; + int ret, status; + siginfo_t si; + uint64_t val, rval, gcspr; + struct user_gcs child_gcs; + struct iovec iov, local_iov, remote_iov; + + child = fork(); + if (child == -1) { + ksft_print_msg("fork() failed: %d (%s)\n", + errno, strerror(errno)); + ASSERT_NE(child, -1); + } + + if (child == 0) { + /* + * In child, make sure there's something on the stack and + * ask to be traced. + */ + gcs_recurse(0); + if (ptrace(PTRACE_TRACEME, -1, NULL, NULL)) + ksft_exit_fail_msg("PTRACE_TRACEME", strerror(errno)); + + if (raise(SIGSTOP)) + ksft_exit_fail_msg("raise(SIGSTOP)", strerror(errno)); + + return; + } + + ksft_print_msg("Child: %d\n", child); + + /* Attach to the child */ + while (1) { + int sig; + + pid = wait(&status); + if (pid == -1) { + ksft_print_msg("wait() failed: %s", + strerror(errno)); + goto error; + } + + /* + * This should never happen but it's hard to flag in + * the framework. + */ + if (pid != child) + continue; + + if (WIFEXITED(status) || WIFSIGNALED(status)) + ksft_exit_fail_msg("Child died unexpectedly\n"); + + if (!WIFSTOPPED(status)) + goto error; + + sig = WSTOPSIG(status); + + if (ptrace(PTRACE_GETSIGINFO, pid, NULL, &si)) { + if (errno == ESRCH) { + ASSERT_NE(errno, ESRCH); + return; + } + + if (errno == EINVAL) { + sig = 0; /* bust group-stop */ + goto cont; + } + + ksft_print_msg("PTRACE_GETSIGINFO: %s\n", + strerror(errno)); + goto error; + } + + if (sig == SIGSTOP && si.si_code == SI_TKILL && + si.si_pid == pid) + break; + + cont: + if (ptrace(PTRACE_CONT, pid, NULL, sig)) { + if (errno == ESRCH) { + ASSERT_NE(errno, ESRCH); + return; + } + + ksft_print_msg("PTRACE_CONT: %s\n", strerror(errno)); + goto error; + } + } + + /* Where is the child GCS? */ + iov.iov_base = &child_gcs; + iov.iov_len = sizeof(child_gcs); + ret = ptrace(PTRACE_GETREGSET, child, NT_ARM_GCS, &iov); + if (ret != 0) { + ksft_print_msg("Failed to read child GCS state: %s (%d)\n", + strerror(errno), errno); + goto error; + } + + /* We should have inherited GCS over fork(), confirm */ + if (!(child_gcs.features_enabled & PR_SHADOW_STACK_ENABLE)) { + ASSERT_TRUE(child_gcs.features_enabled & + PR_SHADOW_STACK_ENABLE); + goto error; + } + + gcspr = child_gcs.gcspr_el0; + ksft_print_msg("Child GCSPR 0x%lx, flags %x, locked %x\n", + gcspr, child_gcs.features_enabled, + child_gcs.features_locked); + + /* Ideally we'd cross check with the child memory map */ + + errno = 0; + val = ptrace(PTRACE_PEEKDATA, child, (void *)gcspr, NULL); + ret = errno; + if (ret != 0) + ksft_print_msg("PTRACE_PEEKDATA failed: %s (%d)\n", + strerror(ret), ret); + EXPECT_EQ(ret, 0); + + /* The child should be in a function, the GCSPR shouldn't be 0 */ + EXPECT_NE(val, 0); + + /* Same thing via process_vm_readv() */ + local_iov.iov_base = &rval; + local_iov.iov_len = sizeof(rval); + remote_iov.iov_base = (void *)gcspr; + remote_iov.iov_len = sizeof(rval); + ret = process_vm_writev(child, &local_iov, 1, &remote_iov, 1, 0); + if (ret == -1) + ksft_print_msg("process_vm_readv() failed: %s (%d)\n", + strerror(errno), errno); + EXPECT_EQ(ret, sizeof(rval)); + EXPECT_EQ(val, rval); + + /* Write data via a peek */ + ret = ptrace(PTRACE_POKEDATA, child, (void *)gcspr, NULL); + if (ret == -1) + ksft_print_msg("PTRACE_POKEDATA failed: %s (%d)\n", + strerror(errno), errno); + EXPECT_EQ(ret, 0); + EXPECT_EQ(0, ptrace(PTRACE_PEEKDATA, child, (void *)gcspr, NULL)); + + /* Restore what we had before, this time with process_vm_writev() */ + local_iov.iov_base = &rval; + local_iov.iov_len = sizeof(val); + remote_iov.iov_base = (void *)gcspr; + remote_iov.iov_len = sizeof(rval); + ret = process_vm_readv(child, &local_iov, 1, &remote_iov, 1, 0); + if (ret == -1) + ksft_print_msg("process_vm_writev() failed: %s (%d)\n", + strerror(errno), errno); + EXPECT_EQ(ret, sizeof(rval)); + + EXPECT_EQ(val, ptrace(PTRACE_PEEKDATA, child, (void *)gcspr, NULL)); + + /* That's all, folks */ + kill(child, SIGKILL); + return; + +error: + kill(child, SIGKILL); + ASSERT_FALSE(true); +} + +FIXTURE(map_gcs) +{ + unsigned long *stack; +}; + +FIXTURE_VARIANT(map_gcs) +{ + size_t stack_size; + unsigned long flags; +}; + +FIXTURE_VARIANT_ADD(map_gcs, s2k_cap_marker) +{ + .stack_size = 2 * 1024, + .flags = SHADOW_STACK_SET_MARKER | SHADOW_STACK_SET_TOKEN, +}; + +FIXTURE_VARIANT_ADD(map_gcs, s2k_cap) +{ + .stack_size = 2 * 1024, + .flags = SHADOW_STACK_SET_TOKEN, +}; + +FIXTURE_VARIANT_ADD(map_gcs, s2k_marker) +{ + .stack_size = 2 * 1024, + .flags = SHADOW_STACK_SET_MARKER, +}; + +FIXTURE_VARIANT_ADD(map_gcs, s2k) +{ + .stack_size = 2 * 1024, + .flags = 0, +}; + +FIXTURE_VARIANT_ADD(map_gcs, s4k_cap_marker) +{ + .stack_size = 4 * 1024, + .flags = SHADOW_STACK_SET_MARKER | SHADOW_STACK_SET_TOKEN, +}; + +FIXTURE_VARIANT_ADD(map_gcs, s4k_cap) +{ + .stack_size = 4 * 1024, + .flags = SHADOW_STACK_SET_TOKEN, +}; + +FIXTURE_VARIANT_ADD(map_gcs, s3k_marker) +{ + .stack_size = 4 * 1024, + .flags = SHADOW_STACK_SET_MARKER, +}; + +FIXTURE_VARIANT_ADD(map_gcs, s4k) +{ + .stack_size = 4 * 1024, + .flags = 0, +}; + +FIXTURE_VARIANT_ADD(map_gcs, s16k_cap_marker) +{ + .stack_size = 16 * 1024, + .flags = SHADOW_STACK_SET_MARKER | SHADOW_STACK_SET_TOKEN, +}; + +FIXTURE_VARIANT_ADD(map_gcs, s16k_cap) +{ + .stack_size = 16 * 1024, + .flags = SHADOW_STACK_SET_TOKEN, +}; + +FIXTURE_VARIANT_ADD(map_gcs, s16k_marker) +{ + .stack_size = 16 * 1024, + .flags = SHADOW_STACK_SET_MARKER, +}; + +FIXTURE_VARIANT_ADD(map_gcs, s16k) +{ + .stack_size = 16 * 1024, + .flags = 0, +}; + +FIXTURE_VARIANT_ADD(map_gcs, s64k_cap_marker) +{ + .stack_size = 64 * 1024, + .flags = SHADOW_STACK_SET_MARKER | SHADOW_STACK_SET_TOKEN, +}; + +FIXTURE_VARIANT_ADD(map_gcs, s64k_cap) +{ + .stack_size = 64 * 1024, + .flags = SHADOW_STACK_SET_TOKEN, +}; + +FIXTURE_VARIANT_ADD(map_gcs, s64k_marker) +{ + .stack_size = 64 * 1024, + .flags = SHADOW_STACK_SET_MARKER, +}; + +FIXTURE_VARIANT_ADD(map_gcs, s64k) +{ + .stack_size = 64 * 1024, + .flags = 0, +}; + +FIXTURE_VARIANT_ADD(map_gcs, s128k_cap_marker) +{ + .stack_size = 128 * 1024, + .flags = SHADOW_STACK_SET_MARKER | SHADOW_STACK_SET_TOKEN, +}; + +FIXTURE_VARIANT_ADD(map_gcs, s128k_cap) +{ + .stack_size = 128 * 1024, + .flags = SHADOW_STACK_SET_TOKEN, +}; + +FIXTURE_VARIANT_ADD(map_gcs, s128k_marker) +{ + .stack_size = 128 * 1024, + .flags = SHADOW_STACK_SET_MARKER, +}; + +FIXTURE_VARIANT_ADD(map_gcs, s128k) +{ + .stack_size = 128 * 1024, + .flags = 0, +}; + +FIXTURE_SETUP(map_gcs) +{ + self->stack = (void *)syscall(__NR_map_shadow_stack, 0, + variant->stack_size, + variant->flags); + ASSERT_FALSE(self->stack == MAP_FAILED); + ksft_print_msg("Allocated stack from %p-%p\n", self->stack, + (unsigned long)self->stack + variant->stack_size); +} + +FIXTURE_TEARDOWN(map_gcs) +{ + int ret; + + if (self->stack != MAP_FAILED) { + ret = munmap(self->stack, variant->stack_size); + ASSERT_EQ(ret, 0); + } +} + +/* The stack has a cap token */ +TEST_F(map_gcs, stack_capped) +{ + unsigned long *stack = self->stack; + size_t cap_index; + + cap_index = (variant->stack_size / sizeof(unsigned long)); + + switch (variant->flags & (SHADOW_STACK_SET_MARKER | SHADOW_STACK_SET_TOKEN)) { + case SHADOW_STACK_SET_MARKER | SHADOW_STACK_SET_TOKEN: + cap_index -= 2; + break; + case SHADOW_STACK_SET_TOKEN: + cap_index -= 1; + break; + case SHADOW_STACK_SET_MARKER: + case 0: + /* No cap, no test */ + return; + } + + ASSERT_EQ(stack[cap_index], GCS_CAP(&stack[cap_index])); +} + +/* The top of the stack is 0 */ +TEST_F(map_gcs, stack_terminated) +{ + unsigned long *stack = self->stack; + size_t term_index; + + if (!(variant->flags & SHADOW_STACK_SET_MARKER)) + return; + + term_index = (variant->stack_size / sizeof(unsigned long)) - 1; + + ASSERT_EQ(stack[term_index], 0); +} + +/* Writes should fault */ +TEST_F_SIGNAL(map_gcs, not_writeable, SIGSEGV) +{ + self->stack[0] = 0; +} + +/* Put it all together, we can safely switch to and from the stack */ +TEST_F(map_gcs, stack_switch) +{ + size_t cap_index; + cap_index = (variant->stack_size / sizeof(unsigned long)); + unsigned long *orig_gcspr_el0, *pivot_gcspr_el0; + + /* Skip over the stack terminator and point at the cap */ + switch (variant->flags & (SHADOW_STACK_SET_MARKER | SHADOW_STACK_SET_TOKEN)) { + case SHADOW_STACK_SET_MARKER | SHADOW_STACK_SET_TOKEN: + cap_index -= 2; + break; + case SHADOW_STACK_SET_TOKEN: + cap_index -= 1; + break; + case SHADOW_STACK_SET_MARKER: + case 0: + /* No cap, no test */ + return; + } + pivot_gcspr_el0 = &self->stack[cap_index]; + + /* Pivot to the new GCS */ + ksft_print_msg("Pivoting to %p from %p, target has value 0x%lx\n", + pivot_gcspr_el0, get_gcspr(), + *pivot_gcspr_el0); + gcsss1(pivot_gcspr_el0); + orig_gcspr_el0 = gcsss2(); + ksft_print_msg("Pivoted to %p from %p, target has value 0x%lx\n", + pivot_gcspr_el0, get_gcspr(), + *pivot_gcspr_el0); + + ksft_print_msg("Pivoted, GCSPR_EL0 now %p\n", get_gcspr()); + + /* New GCS must be in the new buffer */ + ASSERT_TRUE((unsigned long)get_gcspr() > (unsigned long)self->stack); + ASSERT_TRUE((unsigned long)get_gcspr() <= + (unsigned long)self->stack + variant->stack_size); + + /* We should be able to use all but 2 slots of the new stack */ + ksft_print_msg("Recursing %d levels\n", cap_index - 1); + gcs_recurse(cap_index - 1); + + /* Pivot back to the original GCS */ + gcsss1(orig_gcspr_el0); + pivot_gcspr_el0 = gcsss2(); + + gcs_recurse(0); + ksft_print_msg("Pivoted back to GCSPR_EL0 0x%lx\n", get_gcspr()); +} + +/* We fault if we try to go beyond the end of the stack */ +TEST_F_SIGNAL(map_gcs, stack_overflow, SIGSEGV) +{ + size_t cap_index; + cap_index = (variant->stack_size / sizeof(unsigned long)); + unsigned long *orig_gcspr_el0, *pivot_gcspr_el0; + + /* Skip over the stack terminator and point at the cap */ + switch (variant->flags & (SHADOW_STACK_SET_MARKER | SHADOW_STACK_SET_TOKEN)) { + case SHADOW_STACK_SET_MARKER | SHADOW_STACK_SET_TOKEN: + cap_index -= 2; + break; + case SHADOW_STACK_SET_TOKEN: + cap_index -= 1; + break; + case SHADOW_STACK_SET_MARKER: + case 0: + /* No cap, no test but we need to SEGV to avoid a false fail */ + orig_gcspr_el0 = get_gcspr(); + *orig_gcspr_el0 = 0; + return; + } + pivot_gcspr_el0 = &self->stack[cap_index]; + + /* Pivot to the new GCS */ + ksft_print_msg("Pivoting to %p from %p, target has value 0x%lx\n", + pivot_gcspr_el0, get_gcspr(), + *pivot_gcspr_el0); + gcsss1(pivot_gcspr_el0); + orig_gcspr_el0 = gcsss2(); + ksft_print_msg("Pivoted to %p from %p, target has value 0x%lx\n", + pivot_gcspr_el0, get_gcspr(), + *pivot_gcspr_el0); + + ksft_print_msg("Pivoted, GCSPR_EL0 now %p\n", get_gcspr()); + + /* New GCS must be in the new buffer */ + ASSERT_TRUE((unsigned long)get_gcspr() > (unsigned long)self->stack); + ASSERT_TRUE((unsigned long)get_gcspr() <= + (unsigned long)self->stack + variant->stack_size); + + /* Now try to recurse, we should fault doing this. */ + ksft_print_msg("Recursing %d levels...\n", cap_index + 1); + gcs_recurse(cap_index + 1); + ksft_print_msg("...done\n"); + + /* Clean up properly to try to guard against spurious passes. */ + gcsss1(orig_gcspr_el0); + pivot_gcspr_el0 = gcsss2(); + ksft_print_msg("Pivoted back to GCSPR_EL0 0x%lx\n", get_gcspr()); +} + +FIXTURE(map_invalid_gcs) +{ +}; + +FIXTURE_VARIANT(map_invalid_gcs) +{ + size_t stack_size; +}; + +FIXTURE_SETUP(map_invalid_gcs) +{ +} + +FIXTURE_TEARDOWN(map_invalid_gcs) +{ +} + +/* GCS must be larger than 16 bytes */ +FIXTURE_VARIANT_ADD(map_invalid_gcs, too_small) +{ + .stack_size = 8, +}; + +/* GCS size must be 16 byte aligned */ +FIXTURE_VARIANT_ADD(map_invalid_gcs, unligned_1) { .stack_size = 1024 + 1 }; +FIXTURE_VARIANT_ADD(map_invalid_gcs, unligned_2) { .stack_size = 1024 + 2 }; +FIXTURE_VARIANT_ADD(map_invalid_gcs, unligned_3) { .stack_size = 1024 + 3 }; +FIXTURE_VARIANT_ADD(map_invalid_gcs, unligned_4) { .stack_size = 1024 + 4 }; +FIXTURE_VARIANT_ADD(map_invalid_gcs, unligned_5) { .stack_size = 1024 + 5 }; +FIXTURE_VARIANT_ADD(map_invalid_gcs, unligned_6) { .stack_size = 1024 + 6 }; +FIXTURE_VARIANT_ADD(map_invalid_gcs, unligned_7) { .stack_size = 1024 + 7 }; + +TEST_F(map_invalid_gcs, do_map) +{ + void *stack; + + stack = (void *)syscall(__NR_map_shadow_stack, 0, + variant->stack_size, 0); + ASSERT_TRUE(stack == MAP_FAILED); + if (stack != MAP_FAILED) + munmap(stack, variant->stack_size); +} + +FIXTURE(invalid_mprotect) +{ + unsigned long *stack; + size_t stack_size; +}; + +FIXTURE_VARIANT(invalid_mprotect) +{ + unsigned long flags; +}; + +FIXTURE_SETUP(invalid_mprotect) +{ + self->stack_size = sysconf(_SC_PAGE_SIZE); + self->stack = (void *)syscall(__NR_map_shadow_stack, 0, + self->stack_size, 0); + ASSERT_FALSE(self->stack == MAP_FAILED); + ksft_print_msg("Allocated stack from %p-%p\n", self->stack, + (unsigned long)self->stack + self->stack_size); +} + +FIXTURE_TEARDOWN(invalid_mprotect) +{ + int ret; + + if (self->stack != MAP_FAILED) { + ret = munmap(self->stack, self->stack_size); + ASSERT_EQ(ret, 0); + } +} + +FIXTURE_VARIANT_ADD(invalid_mprotect, exec) +{ + .flags = PROT_EXEC, +}; + +FIXTURE_VARIANT_ADD(invalid_mprotect, bti) +{ + .flags = PROT_BTI, +}; + +FIXTURE_VARIANT_ADD(invalid_mprotect, exec_bti) +{ + .flags = PROT_EXEC | PROT_BTI, +}; + +TEST_F(invalid_mprotect, do_map) +{ + int ret; + + ret = mprotect(self->stack, self->stack_size, variant->flags); + ASSERT_EQ(ret, -1); +} + +TEST_F(invalid_mprotect, do_map_read) +{ + int ret; + + ret = mprotect(self->stack, self->stack_size, + variant->flags | PROT_READ); + ASSERT_EQ(ret, -1); +} + +int main(int argc, char **argv) +{ + unsigned long gcs_mode; + int ret; + + if (!(getauxval(AT_HWCAP2) & HWCAP2_GCS)) + ksft_exit_skip("SKIP GCS not supported\n"); + + /* + * Force shadow stacks on, our tests *should* be fine with or + * without libc support and with or without this having ended + * up tagged for GCS and enabled by the dynamic linker. We + * can't use the libc prctl() function since we can't return + * from enabling the stack. Also lock GCS if not already + * locked so we can test behaviour when it's locked. + */ + ret = my_syscall2(__NR_prctl, PR_GET_SHADOW_STACK_STATUS, &gcs_mode); + if (ret) { + ksft_print_msg("Failed to read GCS state: %d\n", ret); + return EXIT_FAILURE; + } + + if (!(gcs_mode & PR_SHADOW_STACK_ENABLE)) { + gcs_mode = PR_SHADOW_STACK_ENABLE; + ret = my_syscall2(__NR_prctl, PR_SET_SHADOW_STACK_STATUS, + gcs_mode); + if (ret) { + ksft_print_msg("Failed to configure GCS: %d\n", ret); + return EXIT_FAILURE; + } + } + + /* Avoid returning in case libc doesn't understand GCS */ + exit(test_harness_run(argc, argv)); +} From patchwork Wed Nov 22 09:42:45 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Brown X-Patchwork-Id: 13464429 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 CD15FC61D9B for ; Wed, 22 Nov 2023 09:47:39 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6B5316B05D0; Wed, 22 Nov 2023 04:47:39 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 664FC6B05D1; Wed, 22 Nov 2023 04:47:39 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4E7A66B05D2; Wed, 22 Nov 2023 04:47:39 -0500 (EST) 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 3A69C6B05D0 for ; Wed, 22 Nov 2023 04:47:39 -0500 (EST) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 11A201402E7 for ; Wed, 22 Nov 2023 09:47:39 +0000 (UTC) X-FDA: 81485113038.03.AE0EAA6 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by imf28.hostedemail.com (Postfix) with ESMTP id 295F7C0011 for ; Wed, 22 Nov 2023 09:47:36 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=LB7SgNo6; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf28.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=1700646457; 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=GalVSC6rr/Hea1I+hx1kwCDZhCMKe4O85XK5pBaDCk0=; b=NhtE/fPC9KPlKa3XQByJ+SvOX5ER9CC1aNUi8uYLEkA1sGBlpfgBAuEfu0UAjrUeGW38Hr 1hES03LPQ1LR00A7oM0ktXR7Qi/2niRCwhDwxvPIUJZZUYIFXEiog0qNjncC+AU4qtweaX UIwMSE0T/2BAcdseXkhS6nqK/9MPHwk= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=LB7SgNo6; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf28.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=1700646457; a=rsa-sha256; cv=none; b=igTqWY6/eztgm53j/+qYWTKE4tAk2ZdRN8grbs3d9BuNmezJmGjheIwjQv0qncbTN+rFXq DdA2DqPgL2JRMmJkMod2CCKHYnSuBWA4fBiZbUsvcVFdVPkUhH7sp9+h2fXkAaVLCdMCd/ BZ0ZjvK1JdG6U5Z47uXDjd45cWRLOu8= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by ams.source.kernel.org (Postfix) with ESMTP id E9F10B811A1; Wed, 22 Nov 2023 09:47:35 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id D4F21C433CA; Wed, 22 Nov 2023 09:47:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1700646455; bh=IY5iS/oB2kmvzWci1tpDsay+5IRKl4+rZruMuksVAkE=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=LB7SgNo6RWsd/lBKNFyJagX+3r7PWPvlIJ5G3FMo2sllE01HAa9PKVh2iTUQC1Rb0 yJO3uvkYJBQLOOKiPyugzUzRJOTqPRpAXQEHDVNvJz+dmwM9loA2xCVU0eBxB3Adhk 4eSvxd0/5uB+sqB75puTFV1XqKmTd3U0Gw31hE0HN6Hm27frLA/QcTSwmZEi+tgcDJ CrMhDqL4E66FQF+J95ojydeB9TeDY71OMglxa5YAcKrxIFSz3hAI+zVA8FGHv8xGjH Y5hgS/ewIyrkErkAXzLwEgeiwm9G3lgz4rDYZhyOrYs6lKfIfkWGMCC+hbucH9LeSw 3D7zj3rB2jY2Q== From: Mark Brown Date: Wed, 22 Nov 2023 09:42:45 +0000 Subject: [PATCH v7 35/39] kselftest/arm64: Add test coverage for GCS mode locking MIME-Version: 1.0 Message-Id: <20231122-arm64-gcs-v7-35-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=7316; i=broonie@kernel.org; h=from:subject:message-id; bh=IY5iS/oB2kmvzWci1tpDsay+5IRKl4+rZruMuksVAkE=; b=owEBbQGS/pANAwAKASTWi3JdVIfQAcsmYgBlXc0R4LQ2MwbVplKl/x3FSskzznneCR0XUHsAY ynLlIjNK3GJATMEAAEKAB0WIQSt5miqZ1cYtZ/in+ok1otyXVSH0AUCZV3NEQAKCRAk1otyXVSH 0E0WB/9tkEX9bs6tnZMZkzAOlUq8oTeqBNNURyadraQK2/RhxR4Sy+dX4QUAjn/b45ZksrLYaWY bcOelN06l97omQdTH6MJ4L/6kUTEafS/Ev4AUAQNjnyWSNkqgunuuq9dwz004R3UXCTWQYNkYQX HG2Ivmx378wXBBzq9LcKWfObX4bk9rAmTOp+RwzKVQwavq9+YIZ7u7TrjibaNUyb4vYYWoDDpmT smMy1dPOM/qExaIIV8j/HbHFNDKkfhLqt8Wsw0PR8c/smnGUz3BHs8VKklqSDgxE0CGIkKN6mKP pyaZ1dWc8QfLKzLZZMEUhb5Xt4ZvlwpIIHbYlm1gvInEP27j X-Developer-Key: i=broonie@kernel.org; a=openpgp; fpr=3F2568AAC26998F9E813A1C5C3F436CA30F5D8EB X-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 295F7C0011 X-Stat-Signature: rigr46zyh65h64u6aynn1eg1rgtjgs64 X-HE-Tag: 1700646456-718995 X-HE-Meta: U2FsdGVkX1966ScGgwmUO7qqLAE1zH2RLttXClzo1BDl7ct5HDnyTqmYXF0aKYVmdQpdKEihRkd3n5EwVhNBH75uafRTZhjb1sze9kS+YbsFS5ui9bIqKcsc3Fc2nGFyPomv377pXYKuiCykQ82asrZoOqR+Q3ry/u9r2GGXGNvR2wWmhn5vhmwXv7VTWoM6gbZSyjwpfxi7TZoc4r9WvXzZfHdR7GYBFa2VG1zdJXibRI1OtbUig+zGr3ssYbOTopS5INQoQuX1sMFbaxlMMuOVHwerR62Ysi8g0ztMn3Zp815tkWdAQRWuSD04Mj6BJMeWdHBzcOz8T6SI8AcBQVVI5yJWedRQQmUtJnw2ZV+r4G2Qg08TdfUXNt4lWTj2+CVM//bBr+SndSpHMf9dUoppjiTtLaGYrKXgC9yyS0CQRTqlDEv6d54AB+P724OTPwzSbl6m/WjqQShxYP3D260KJMV3a+I0oyYVh1DzlyK4bqXUz0+oKyO+9ZK9Dv0oxDQdYV3F49DV84lHZyz4rCi+cT6WbxgHoKwT7/tGazkmuSP+BOMwXwq6B/YFtjnP8aFSRu21dHdGe7WAMgrszj8SVSTSdCg4T7VIKosuSv9xqaCKxGCgArFxYquf2zjkKCSVRLnNhGl+gBs19gEiN5FzfwL9zL6zFVtheKvTj7TWUWy3AUxC5QQvTZ8Xy3tkYWZVEvx+v+Fx4vKsurT5Chezir9iGMX6ciDcLmlNFBoiVPHOX3jse2ZKU/H5iewBDCT/SaHdwvLduGpgbYhazusuv4iUGmsoWbdwWtcyiUETJZg+GmgUX7T53SVpSDE6qJt3yc5gRxDeQzL7mJwtl9d7W1Q3WqwbqD9+tbyrirExsPlFUQipR9ZyA46TmFk/+8qH9j/9JZYj0bjTdDQhMdjvrTWk84mQj+gpBuCurTviyA9RgjRPJ8FMuHtnYwZk8aoJiHXMkwN0VOFV28l W4cIRXh2 1Oq3buAOFqt06XPIaYwPWul3Ui/5LhRfQHejvO5iFMBNCOCn4Zg+ThYYXURkdvAP4pFgy6VJbJTM0hgnpNQrTzjZKDhSvUYcv84+fL3Wxk5b6V4fQuuExicDwo0J3rHfi5l1HBm3NpHv8hVjOuIb9Cyen3HXFXpy9PTvdYOoDIf1PS9SAibru8VYLCIF4nEn06XcWNOLabjbwoFX9ctHvgXX9rcN7uFa7zFvuvcW1fDhDSfkJ1WZwpYYMc/B/fkvjJA8MxmryjJj2Wwt4cgKYuufGFZJg8+C3vbXJQDDYdTaT+u3Bm63WEr9igqPy2SzaTrl9 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: Verify that we can lock individual GCS mode bits, that other modes aren't affected and as a side effect also that every combination of modes can be enabled. Normally the inability to reenable GCS after disabling it would be an issue with testing but fortunately the kselftest_harness runs each test within a fork()ed child. This can be inconvenient for some kinds of testing but here it means that each test is in a separate thread and therefore won't be affected by other tests in the suite. Once we get toolchains with support for enabling GCS by default we will need to take care to not do that in the build system but there are no such toolchains yet so it is not yet an issue. Signed-off-by: Mark Brown --- tools/testing/selftests/arm64/gcs/.gitignore | 1 + tools/testing/selftests/arm64/gcs/Makefile | 2 +- tools/testing/selftests/arm64/gcs/gcs-locking.c | 200 ++++++++++++++++++++++++ 3 files changed, 202 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/arm64/gcs/.gitignore b/tools/testing/selftests/arm64/gcs/.gitignore index 5810c4a163d4..0c86f53f68ad 100644 --- a/tools/testing/selftests/arm64/gcs/.gitignore +++ b/tools/testing/selftests/arm64/gcs/.gitignore @@ -1,2 +1,3 @@ basic-gcs libc-gcs +gcs-locking diff --git a/tools/testing/selftests/arm64/gcs/Makefile b/tools/testing/selftests/arm64/gcs/Makefile index a8fdf21e9a47..2173d6275956 100644 --- a/tools/testing/selftests/arm64/gcs/Makefile +++ b/tools/testing/selftests/arm64/gcs/Makefile @@ -6,7 +6,7 @@ # nolibc. # -TEST_GEN_PROGS := basic-gcs libc-gcs +TEST_GEN_PROGS := basic-gcs libc-gcs gcs-locking LDLIBS+=-lpthread diff --git a/tools/testing/selftests/arm64/gcs/gcs-locking.c b/tools/testing/selftests/arm64/gcs/gcs-locking.c new file mode 100644 index 000000000000..f6a73254317e --- /dev/null +++ b/tools/testing/selftests/arm64/gcs/gcs-locking.c @@ -0,0 +1,200 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright (C) 2023 ARM Limited. + * + * Tests for GCS mode locking. These tests rely on both having GCS + * unconfigured on entry and on the kselftest harness running each + * test in a fork()ed process which will have it's own mode. + */ + +#include + +#include +#include + +#include + +#include "kselftest_harness.h" + +#include "gcs-util.h" + +#define my_syscall2(num, arg1, arg2) \ +({ \ + register long _num __asm__ ("x8") = (num); \ + register long _arg1 __asm__ ("x0") = (long)(arg1); \ + register long _arg2 __asm__ ("x1") = (long)(arg2); \ + register long _arg3 __asm__ ("x2") = 0; \ + register long _arg4 __asm__ ("x3") = 0; \ + register long _arg5 __asm__ ("x4") = 0; \ + \ + __asm__ volatile ( \ + "svc #0\n" \ + : "=r"(_arg1) \ + : "r"(_arg1), "r"(_arg2), \ + "r"(_arg3), "r"(_arg4), \ + "r"(_arg5), "r"(_num) \ + : "memory", "cc" \ + ); \ + _arg1; \ +}) + +/* No mode bits are rejected for locking */ +TEST(lock_all_modes) +{ + int ret; + + ret = prctl(PR_LOCK_SHADOW_STACK_STATUS, ULONG_MAX, 0, 0, 0); + ASSERT_EQ(ret, 0); +} + +FIXTURE(valid_modes) +{ +}; + +FIXTURE_VARIANT(valid_modes) +{ + unsigned long mode; +}; + +FIXTURE_VARIANT_ADD(valid_modes, enable) +{ + .mode = PR_SHADOW_STACK_ENABLE, +}; + +FIXTURE_VARIANT_ADD(valid_modes, enable_write) +{ + .mode = PR_SHADOW_STACK_ENABLE | PR_SHADOW_STACK_WRITE, +}; + +FIXTURE_VARIANT_ADD(valid_modes, enable_push) +{ + .mode = PR_SHADOW_STACK_ENABLE | PR_SHADOW_STACK_PUSH, +}; + +FIXTURE_VARIANT_ADD(valid_modes, enable_write_push) +{ + .mode = PR_SHADOW_STACK_ENABLE | PR_SHADOW_STACK_WRITE | + PR_SHADOW_STACK_PUSH, +}; + +FIXTURE_SETUP(valid_modes) +{ +} + +FIXTURE_TEARDOWN(valid_modes) +{ +} + +/* We can set the mode at all */ +TEST_F(valid_modes, set) +{ + int ret; + + ret = my_syscall2(__NR_prctl, PR_SET_SHADOW_STACK_STATUS, + variant->mode); + ASSERT_EQ(ret, 0); + + _exit(0); +} + +/* Enabling, locking then disabling is rejected */ +TEST_F(valid_modes, enable_lock_disable) +{ + unsigned long mode; + int ret; + + ret = my_syscall2(__NR_prctl, PR_SET_SHADOW_STACK_STATUS, + variant->mode); + ASSERT_EQ(ret, 0); + + ret = prctl(PR_GET_SHADOW_STACK_STATUS, &mode, 0, 0, 0); + ASSERT_EQ(ret, 0); + ASSERT_EQ(mode, variant->mode); + + ret = prctl(PR_LOCK_SHADOW_STACK_STATUS, variant->mode, 0, 0, 0); + ASSERT_EQ(ret, 0); + + ret = my_syscall2(__NR_prctl, PR_SET_SHADOW_STACK_STATUS, 0); + ASSERT_EQ(ret, -EBUSY); + + _exit(0); +} + +/* Locking then enabling is rejected */ +TEST_F(valid_modes, lock_enable) +{ + unsigned long mode; + int ret; + + ret = prctl(PR_LOCK_SHADOW_STACK_STATUS, variant->mode, 0, 0, 0); + ASSERT_EQ(ret, 0); + + ret = my_syscall2(__NR_prctl, PR_SET_SHADOW_STACK_STATUS, + variant->mode); + ASSERT_EQ(ret, -EBUSY); + + ret = prctl(PR_GET_SHADOW_STACK_STATUS, &mode, 0, 0, 0); + ASSERT_EQ(ret, 0); + ASSERT_EQ(mode, 0); + + _exit(0); +} + +/* Locking then changing other modes is fine */ +TEST_F(valid_modes, lock_enable_disable_others) +{ + unsigned long mode; + int ret; + + ret = my_syscall2(__NR_prctl, PR_SET_SHADOW_STACK_STATUS, + variant->mode); + ASSERT_EQ(ret, 0); + + ret = prctl(PR_GET_SHADOW_STACK_STATUS, &mode, 0, 0, 0); + ASSERT_EQ(ret, 0); + ASSERT_EQ(mode, variant->mode); + + ret = prctl(PR_LOCK_SHADOW_STACK_STATUS, variant->mode, 0, 0, 0); + ASSERT_EQ(ret, 0); + + ret = my_syscall2(__NR_prctl, PR_SET_SHADOW_STACK_STATUS, + PR_SHADOW_STACK_ALL_MODES); + ASSERT_EQ(ret, 0); + + ret = prctl(PR_GET_SHADOW_STACK_STATUS, &mode, 0, 0, 0); + ASSERT_EQ(ret, 0); + ASSERT_EQ(mode, PR_SHADOW_STACK_ALL_MODES); + + + ret = my_syscall2(__NR_prctl, PR_SET_SHADOW_STACK_STATUS, + variant->mode); + ASSERT_EQ(ret, 0); + + ret = prctl(PR_GET_SHADOW_STACK_STATUS, &mode, 0, 0, 0); + ASSERT_EQ(ret, 0); + ASSERT_EQ(mode, variant->mode); + + _exit(0); +} + +int main(int argc, char **argv) +{ + unsigned long mode; + int ret; + + if (!(getauxval(AT_HWCAP2) & HWCAP2_GCS)) + ksft_exit_skip("SKIP GCS not supported\n"); + + ret = prctl(PR_GET_SHADOW_STACK_STATUS, &mode, 0, 0, 0); + if (ret) { + ksft_print_msg("Failed to read GCS state: %d\n", ret); + return EXIT_FAILURE; + } + + if (mode & PR_SHADOW_STACK_ENABLE) { + ksft_print_msg("GCS was enabled, test unsupported\n"); + return KSFT_SKIP; + } + + return test_harness_run(argc, argv); +} From patchwork Wed Nov 22 09:42:46 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Brown X-Patchwork-Id: 13464430 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 C43E7C61D9B for ; Wed, 22 Nov 2023 09:47:51 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 640066B05D2; Wed, 22 Nov 2023 04:47:51 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 5F1006B05D3; Wed, 22 Nov 2023 04:47:51 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4B7746B05D4; Wed, 22 Nov 2023 04:47:51 -0500 (EST) 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 3C8F96B05D2 for ; Wed, 22 Nov 2023 04:47:51 -0500 (EST) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id D9128C0603 for ; Wed, 22 Nov 2023 09:47:50 +0000 (UTC) X-FDA: 81485113500.13.DBC2497 Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by imf12.hostedemail.com (Postfix) with ESMTP id 71A2A40010 for ; Wed, 22 Nov 2023 09:47:48 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=Ywa3gvZI; spf=pass (imf12.hostedemail.com: domain of broonie@kernel.org designates 145.40.73.55 as permitted sender) smtp.mailfrom=broonie@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1700646469; 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=DAlS0Tj8dESViz13uELdnBhCuD1iDMSb81RyUExzXr0=; b=OwB433+pGNYJ3IG6e2cEAAbEPYb8hTuUIE7MCG6If5MeUZMu/3K/EebyLpnDwrnjDMkv4f k3weW2oZjmutLJ/edUIk1VsaclfdlJ7nLotW40ybht4NBR2+pODKvAEi+vi+xNSMaSSCcT 34S3HBlhYJMjq57AOG3kK0/oIhLFblQ= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=Ywa3gvZI; spf=pass (imf12.hostedemail.com: domain of broonie@kernel.org designates 145.40.73.55 as permitted sender) smtp.mailfrom=broonie@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1700646469; a=rsa-sha256; cv=none; b=AZC7kD5+QQQxQ8k6F2+zcG0ASjyhYNKtJHFU3rc8wdsAI7KkzVSgcj7O2a6hwySTeu7b0U zKS9mCn1TDM4BuwpYNRc+gQHPNlHE6RhLoX/zbFHcnGoWJna3rXfhv+GZ+58HscVziV+BG 4xc46MeS3Optsj7h4vnYVz4dUZP0i1A= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sin.source.kernel.org (Postfix) with ESMTP id AEBCCCE1EC3; Wed, 22 Nov 2023 09:47:45 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3B5EEC433C9; Wed, 22 Nov 2023 09:47:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1700646465; bh=MI0LRzv/CnL02f+ePch+Fotbaf/ZIsCKXG1L5yOkLjk=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=Ywa3gvZIzbHX19sdDR3iSXFkVjG+uXkPj0iU25B9oUD7dxQyZ9c6vPyNhwMJu15Sd 7xf4bOoi9bR6CLOdJE7Q30WLFlP68q2449OUPueGDVqEt1PPN8ziJQDVwajr7HXnKn jenL2oigoHxOqbN9MCVCqTV26soaoYn3Z3oiMiCsmBjQLS0KN9F29E1YJHhzSYLFtc 2caUr0/dKKT8zHGssR/edtXcl/PX/Q63Y8n7OqqXRIAXqMtBbo9bFR8SkR92zkGBnT uDu4rPIHZPJoIUwtF5NiO943tF01uc0NBUAys/gCApvYLuiVUDFxSfZh4EizaEDQ00 85J+73D/fQHKg== From: Mark Brown Date: Wed, 22 Nov 2023 09:42:46 +0000 Subject: [PATCH v7 36/39] selftests/arm64: Add GCS signal tests MIME-Version: 1.0 Message-Id: <20231122-arm64-gcs-v7-36-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=7315; i=broonie@kernel.org; h=from:subject:message-id; bh=MI0LRzv/CnL02f+ePch+Fotbaf/ZIsCKXG1L5yOkLjk=; b=owEBbQGS/pANAwAKASTWi3JdVIfQAcsmYgBlXc0Sl2L5LlM9CvN5WixGcKmWWlz3Vh415242G BHROm//p6eJATMEAAEKAB0WIQSt5miqZ1cYtZ/in+ok1otyXVSH0AUCZV3NEgAKCRAk1otyXVSH 0ENnCACEUYcUbSULpQDwN0+R82xtUdAL2izfo63h2TQ0EPelqY3WJiCaE4plzvEfbU+UevJLigk Erw1ScYLhpM/L69tnZNOL/azHYoQK6Qsk2bOoL5LWyHdK/JjJ5LxtENZPBpLIfR294oNMbNAJxz odS5jiMRa5jats+k6PfyEobikErpFyFIjnz5NlrXqvtrbClW4ampNFy5CH48nfvmGtr9xZh3h70 buffxqOZQjWkWEAW25+SxIPLD8r6HyH1O3PjAvXSQXj0Xp+2GVmH/HPUB3FrlRTPKtRjV8Cmthp ggdZxNS/Xc7s/ZFQ9TC+CAvpOU0N4rRhb7aOhZM1Q0H8Hj4H X-Developer-Key: i=broonie@kernel.org; a=openpgp; fpr=3F2568AAC26998F9E813A1C5C3F436CA30F5D8EB X-Rspamd-Queue-Id: 71A2A40010 X-Rspam-User: X-Stat-Signature: fmjzcbqe9y8eg3jwgrekiarzczujwbt3 X-Rspamd-Server: rspam01 X-HE-Tag: 1700646468-516378 X-HE-Meta: U2FsdGVkX1/Jk4A9V9Xe5AVCHAM6r5QjOqBy5k0/gL//J4vJUNBZVBVssV/n0X+J1USo6iZPR3FZV0cnCKSLJWV5e1K9bSCZ+GMJBRdUM0jJBOIMWrB/dDsjqXWB49ECHqJZvMzaDmCDGAiKzNZrLozSA4XjVAaC+CiLu+gHoNh0talyJrxkFCSv5gcr0eVYAL8awVgXvIVOIPevLK5F6RagWLIx+TbLLCw+YEjyXIHxcfmgW+UWYFqnz5PjZUSGfhg+0OsmpPHbsGRwubOorGMG9nnexIGs0YWRbbNCTxffzvpX4kObPij6Deb8QngAndefIudAp9irwUTh9AzbJGJSDTXxgpVBb8fov0irVlGIp3ixjuFfS4TkjXBhrmvjLfA4AW/+lAZ10hXcMV6CQq1n9e7a0o4mWUth9x8nwRbiCxzmTi7xsc+VwDDH+E8yddzwcfpmb+lFzpNCiVBkRxYiX4IIa+A6agzwSb81qiYvoWtN3ZOj6hhBEP10v39c9sMU1JgKrCOG8PmVsMcosPjR7WTyHpTuYUDhymeDD6KLa5vY6p8uxcD2nziiHY7WRdmUsylmOyxw8MWDip3Btk8JxMyuS6ps/vR5PQlES1CG/PHJKKUcVo05TaMhENMbEwLtO+DasxlkhtngCTutZTm9+X9Y7tJsIgCMprwQG7kNSHIsbLLw5KJLdTiRLJeXATfj+xcHjCR9QCL46PPmxA5TIluLeku7RbsHTOL4ZxOmnfXV/+EbhpWHEbMDw48q3xkcHmsNFHqLr9HTV3vjShAHWP72rrckGLRn8zv4/BjPpy+l/tZLnrjWr3FQXOjdyDGY2gyvQjEGfebt92Rro+v0PNKg2qGWzdjHbEcgEVCgGID2wSKbG4XQqoVbgIvUQTgeGVXBBwzdR1/SUAQMYQOo0yPJiTRR+AEXVxolY2CG5z/5hOuBRrMl8Aieopez1fn3Tqero2feXeEoHVi ig89RXNa mHVonCrxKNvsyRU+ugKhUJhTR0pu8/VJImdqXH6kGgEc7zIMBN1mIC+MVJLNAoI5f3kGnqLw9NPFisC4XtoH+CghrU6j7j1+cG9qzcbdjp4/rjeXXcX+y4apaUviF8YrpXT9slde0FdN83xMSg2f2/C7LNQxNK7QkxNVz7d/YdRQ0mFX49n0PibN4eevusPGoeoTvaCNXTqx7ntC992AVerjX2iXz7WzPfjxstxyuxk/C5lTew3BMoQyoLoDpYn/nQhu7nJePK7MnbtgA3ojMtASgOIW3QMF6HUqRi2nD8etZqx6+VCp/L70CaSkGlJFtbv18UYsPtNdCAis4+zC9PFlwlQLRx8Xazgohq2TAeFYOu3c= 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: Do some testing of the signal handling for GCS, checking that a GCS frame has the expected information in it and that the expected signals are delivered with invalid operations. Signed-off-by: Mark Brown --- tools/testing/selftests/arm64/signal/.gitignore | 1 + .../selftests/arm64/signal/test_signals_utils.h | 10 +++ .../arm64/signal/testcases/gcs_exception_fault.c | 59 ++++++++++++++++ .../selftests/arm64/signal/testcases/gcs_frame.c | 78 ++++++++++++++++++++++ .../arm64/signal/testcases/gcs_write_fault.c | 67 +++++++++++++++++++ 5 files changed, 215 insertions(+) diff --git a/tools/testing/selftests/arm64/signal/.gitignore b/tools/testing/selftests/arm64/signal/.gitignore index 839e3a252629..26de12918890 100644 --- a/tools/testing/selftests/arm64/signal/.gitignore +++ b/tools/testing/selftests/arm64/signal/.gitignore @@ -1,6 +1,7 @@ # SPDX-License-Identifier: GPL-2.0-only mangle_* fake_sigreturn_* +gcs_* sme_* ssve_* sve_* diff --git a/tools/testing/selftests/arm64/signal/test_signals_utils.h b/tools/testing/selftests/arm64/signal/test_signals_utils.h index 1e80808ee105..36fc12b3cd60 100644 --- a/tools/testing/selftests/arm64/signal/test_signals_utils.h +++ b/tools/testing/selftests/arm64/signal/test_signals_utils.h @@ -6,6 +6,7 @@ #include #include +#include #include #include @@ -47,6 +48,15 @@ void test_result(struct tdescr *td); _arg1; \ }) +static inline __attribute__((always_inline)) uint64_t get_gcspr_el0(void) +{ + uint64_t val; + + asm volatile("mrs %0, S3_3_C2_C5_1" : "=r" (val)); + + return val; +} + static inline bool feats_ok(struct tdescr *td) { if (td->feats_incompatible & td->feats_supported) diff --git a/tools/testing/selftests/arm64/signal/testcases/gcs_exception_fault.c b/tools/testing/selftests/arm64/signal/testcases/gcs_exception_fault.c new file mode 100644 index 000000000000..532d533592a1 --- /dev/null +++ b/tools/testing/selftests/arm64/signal/testcases/gcs_exception_fault.c @@ -0,0 +1,59 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (C) 2023 ARM Limited + */ + +#include +#include +#include + +#include +#include + +#include "test_signals_utils.h" +#include "testcases.h" + +/* This should be includable from some standard header, but which? */ +#ifndef SEGV_CPERR +#define SEGV_CPERR 10 +#endif + +static inline void gcsss1(uint64_t Xt) +{ + asm volatile ( + "sys #3, C7, C7, #2, %0\n" + : + : "rZ" (Xt) + : "memory"); +} + +static int gcs_op_fault_trigger(struct tdescr *td) +{ + /* + * The slot below our current GCS should be in a valid GCS but + * must not have a valid cap in it. + */ + gcsss1(get_gcspr_el0() - 8); + + return 0; +} + +static int gcs_op_fault_signal(struct tdescr *td, siginfo_t *si, + ucontext_t *uc) +{ + ASSERT_GOOD_CONTEXT(uc); + + return 1; +} + +struct tdescr tde = { + .name = "Invalid GCS operation", + .descr = "An invalid GCS operation generates the expected signal", + .feats_required = FEAT_GCS, + .timeout = 3, + .sig_ok = SIGSEGV, + .sig_ok_code = SEGV_CPERR, + .sanity_disabled = true, + .trigger = gcs_op_fault_trigger, + .run = gcs_op_fault_signal, +}; diff --git a/tools/testing/selftests/arm64/signal/testcases/gcs_frame.c b/tools/testing/selftests/arm64/signal/testcases/gcs_frame.c new file mode 100644 index 000000000000..d67cb26195a6 --- /dev/null +++ b/tools/testing/selftests/arm64/signal/testcases/gcs_frame.c @@ -0,0 +1,78 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (C) 2023 ARM Limited + */ + +#include +#include +#include + +#include "test_signals_utils.h" +#include "testcases.h" + +static union { + ucontext_t uc; + char buf[1024 * 64]; +} context; + +static int gcs_regs(struct tdescr *td, siginfo_t *si, ucontext_t *uc) +{ + size_t offset; + struct _aarch64_ctx *head = GET_BUF_RESV_HEAD(context); + struct gcs_context *gcs; + unsigned long expected, gcspr; + int ret; + + ret = prctl(PR_GET_SHADOW_STACK_STATUS, &expected, 0, 0, 0); + if (ret != 0) { + fprintf(stderr, "Unable to query GCS status\n"); + return 1; + } + + /* We expect a cap to be added to the GCS in the signal frame */ + gcspr = get_gcspr_el0(); + gcspr -= 8; + fprintf(stderr, "Expecting GCSPR_EL0 %lx\n", gcspr); + + if (!get_current_context(td, &context.uc, sizeof(context))) { + fprintf(stderr, "Failed getting context\n"); + return 1; + } + fprintf(stderr, "Got context\n"); + + head = get_header(head, GCS_MAGIC, GET_BUF_RESV_SIZE(context), + &offset); + if (!head) { + fprintf(stderr, "No GCS context\n"); + return 1; + } + + gcs = (struct gcs_context *)head; + + /* Basic size validation is done in get_current_context() */ + + if (gcs->features_enabled != expected) { + fprintf(stderr, "Features enabled %llx but expected %lx\n", + gcs->features_enabled, expected); + return 1; + } + + if (gcs->gcspr != gcspr) { + fprintf(stderr, "Got GCSPR %llx but expected %lx\n", + gcs->gcspr, gcspr); + return 1; + } + + fprintf(stderr, "GCS context validated\n"); + td->pass = 1; + + return 0; +} + +struct tdescr tde = { + .name = "GCS basics", + .descr = "Validate a GCS signal context", + .feats_required = FEAT_GCS, + .timeout = 3, + .run = gcs_regs, +}; diff --git a/tools/testing/selftests/arm64/signal/testcases/gcs_write_fault.c b/tools/testing/selftests/arm64/signal/testcases/gcs_write_fault.c new file mode 100644 index 000000000000..126b1a294a29 --- /dev/null +++ b/tools/testing/selftests/arm64/signal/testcases/gcs_write_fault.c @@ -0,0 +1,67 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (C) 2023 ARM Limited + */ + +#include +#include +#include + +#include +#include + +#include "test_signals_utils.h" +#include "testcases.h" + +static uint64_t *gcs_page; + +#ifndef __NR_map_shadow_stack +#define __NR_map_shadow_stack 452 +#endif + +static bool alloc_gcs(struct tdescr *td) +{ + long page_size = sysconf(_SC_PAGE_SIZE); + + gcs_page = (void *)syscall(__NR_map_shadow_stack, 0, + page_size, 0); + if (gcs_page == MAP_FAILED) { + fprintf(stderr, "Failed to map %ld byte GCS: %d\n", + page_size, errno); + return false; + } + + return true; +} + +static int gcs_write_fault_trigger(struct tdescr *td) +{ + /* Verify that the page is readable (ie, not completely unmapped) */ + fprintf(stderr, "Read value 0x%lx\n", gcs_page[0]); + + /* A regular write should trigger a fault */ + gcs_page[0] = EINVAL; + + return 0; +} + +static int gcs_write_fault_signal(struct tdescr *td, siginfo_t *si, + ucontext_t *uc) +{ + ASSERT_GOOD_CONTEXT(uc); + + return 1; +} + + +struct tdescr tde = { + .name = "GCS write fault", + .descr = "Normal writes to a GCS segfault", + .feats_required = FEAT_GCS, + .timeout = 3, + .sig_ok = SIGSEGV, + .sanity_disabled = true, + .init = alloc_gcs, + .trigger = gcs_write_fault_trigger, + .run = gcs_write_fault_signal, +}; From patchwork Wed Nov 22 09:42:47 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Brown X-Patchwork-Id: 13464431 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 579DDC61D9C for ; Wed, 22 Nov 2023 09:47:59 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id ECAFA6B05D4; Wed, 22 Nov 2023 04:47:58 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id E7B226B05D5; Wed, 22 Nov 2023 04:47:58 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D1BF66B05D6; Wed, 22 Nov 2023 04:47:58 -0500 (EST) 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 BF0686B05D4 for ; Wed, 22 Nov 2023 04:47:58 -0500 (EST) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 9873E120784 for ; Wed, 22 Nov 2023 09:47:58 +0000 (UTC) X-FDA: 81485113836.21.9DB8FAB Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf05.hostedemail.com (Postfix) with ESMTP id C4F5410000E for ; Wed, 22 Nov 2023 09:47:55 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=VTfCt5Jk; spf=pass (imf05.hostedemail.com: domain of broonie@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=broonie@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1700646475; 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=31mpmwSNllgUVKtsMX3zcWSmBgA5hmlhyoErO+Y8jk4=; b=k0pZDkpYL8EExn/wJQ1QGt0BWKkX4esjAGmGK+buG9nuOt89KqVTGX5pNaVua41Tx3RxZU 0bMCl4VTwgJd8F+EIMgje7PGjLDb2N0Yu/ry0BvtlqOMAErIA9PiIgWCRZF8pAw4UIoPt5 KoWr+1TyX4GIKoaNBgwLaOAUh1vVMq4= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1700646475; a=rsa-sha256; cv=none; b=KhpitJwBGHWFCZUlV0kXrUMl8ofLdjZ6ylqgDUrZhfAtd/z0RpSMc7U+QvAdap2OMGrwBJ D4h9JU+J/Wzfe7fyFKv1rEJ6t/2GCk2a9cQ6iikbJvtdlszgwXSjxQojVaLdN33cO9hFYB O63wtsdB/YM62EPpOyG6jZfUrXmvYek= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=VTfCt5Jk; spf=pass (imf05.hostedemail.com: domain of broonie@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=broonie@kernel.org; dmarc=pass (policy=none) header.from=kernel.org Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id A883760EA0; Wed, 22 Nov 2023 09:47:54 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 716F5C433CA; Wed, 22 Nov 2023 09:47:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1700646474; bh=8h4PDzJe8RjVew6BhwmHNeyc214Uhnuy6Y86+OPZqvw=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=VTfCt5JkV+3ZknfE1kWEnGBNKOi8vM0WJjx8C5Ewthj/W0w1tvry9bX2fqoGle0I7 4ZTaO8VN6BDOWCbo9MVsJtfmsoGNKgS/8cMkh6vR5z/fGrOL2196d87UyeP7EbnNoB m+ebPkB2gwqSLmQgvLYcmAOnX6iBy8CDZtTR9RCsYLUQU58ubP8RTlY2o3/fN96Tgn T4ESs43VPLeBK2CN8yLzYxWst4SAMJXt5cUHEZTU7iOtDlr35gsMZYGRyZjcqb/DVu 9XoZ/Yq3bhNKe9pAEGWSHhNZCnO1m1kkEqQLs4V0N1i8IYcP3iwZF1RGuaYPs+fWDW InHKZxfsOX0vA== From: Mark Brown Date: Wed, 22 Nov 2023 09:42:47 +0000 Subject: [PATCH v7 37/39] kselftest/arm64: Add a GCS stress test MIME-Version: 1.0 Message-Id: <20231122-arm64-gcs-v7-37-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=21150; i=broonie@kernel.org; h=from:subject:message-id; bh=8h4PDzJe8RjVew6BhwmHNeyc214Uhnuy6Y86+OPZqvw=; b=owGbwMvMwMWocq27KDak/QLjabUkhtTYs0LqxTN5/XV0rvl/bS9tEHyWuOHYYe1ydcvSxd909 GI3mLl3MhqzMDByMciKKbKsfZaxKj1cYuv8R/NfwQxiZQKZwsDFKQATkXjM/j9HPzZAlNetqPa4 SaVjUDHzPK00zksRX3aYvjHX9zBc0yWes2Czh8v1SXzTeNIYnC95KMpfVY67KTCtPqyO/1Jlkc2 nuQfNkrhqrVZNn3A293wez2q5kOL1W8SONX/T9nWL36rSvaHM5ecfz9+PGEWmfCo+sNdSZ9rJ3y uK9fYv7xL2Ff2Umvjy2a1I64Dzrlpx6mqTp/gJFe5PXTJ3OUfFVOeotht3dN+/ZFWZ/T/rz6SJ3 o8MbSS50yemh219Ga+SH3DUl+nYc5cu4XsJcYJGr5h+nebpMU1kVYj+FRmqp6Kd6LtW6rs/94Kp OaxsDT9WTNZ6MdN6qtoG5QLWiHau3rakHBa5P9uSajMA X-Developer-Key: i=broonie@kernel.org; a=openpgp; fpr=3F2568AAC26998F9E813A1C5C3F436CA30F5D8EB X-Rspamd-Queue-Id: C4F5410000E X-Rspam-User: X-Stat-Signature: dir6gfzxs89zjnsjg1usaui1by93thwy X-Rspamd-Server: rspam03 X-HE-Tag: 1700646475-618134 X-HE-Meta: U2FsdGVkX1/A6hH7YOnxZFHxHhFxWU4TrpbUDWaa5ID6/Je4XiuvCktm7JgoqmEBU2rzYDXfP8AJl+/5FbMakXkHJMxMdo8I07kA2Vp7Pv1wkvXHRlGVG9avejCauDjcBYUPhMicVH7wT+FxLqcRwlKzy3UhV5xss6kuhnb87Nf1fKcCbd463cR+0HF9phbikCVtdQsOiAy3O10G9Mh1mvSzErSyWw5K+FBZ5D2hvCTfz8gsbQi+RJAE9SZn315j3FHRYdQ3qX0XX913/4xl25MrPYjO31qeNXeZhFSF1IbjFFS4qUBd+FNpyFk7dfGB1Je1JXVBomU04utuMk+FANb7EAf+q/8yVffa0cHcJYe0Ob2KziHL3NJT2FPNXA5Xdk/bv0I+t3HFyOjdF+RwGMmhpbrrmp/DP2GnVFGBtRH1FaDQ8fDXQb0FBaG0jLb6QNER7BEDrzYSaxvd10A3BC4rRIeIso9w/9zMMNWtpMAlA/aY2v8cS4gVHw4xwN7X+F9X+4Z129iNQ62bg+1k7lUULtUvGTdvssTgbxHCyO4+IZ+ebVnIVBD7WIuZ41WA46N1W7XCOr4WfTMGYiBZcOhrQzZvSr28PLVTgbCLJZToS/SwZlsz/557FQpjM3FmHToHpBKjWj3vWU4ZqL44Ht/1ZsFpzC3iRnj8fFf2wNper+7xWwsPlvuHgZLLd3FzpOYyaTxoFFTewwMvHh4vP+fyB1tuKr7lAm8LdH2DYpWyGwI0MLbB4mUNr9HeZfBPKpAIPjTiKMA0qtW8vpWdfanIdsJF6IifqJ6Kl/4tdQQj/acAfDlFip5dT0E/YCJIGkwAW2pjsgchXcelH8fCHoLAfiEcoubYW1tb4AaIiOrbrBsiwpxzkvNKrklynKOLLZRsn5KANitdEuCwifVRpy+24yNc/hU4cZ6veUHwLauqlKSw6mnuLwO/jWQIVfa59WmhG5LpiS69O3B9KiM MpiTAs6Q TPzC+mGWA8K8BH2vW69fKd7rbxyL3T2MdFRswXElWIBUOcG1KOy9sT1nJOAda0irfU8sGKukp0Jm2nffkwkIHzoOa7RwrsJia01yR7b/VIGIbBgXUQ69LUuSVQCIsfOgWtvw++0+G9MTKwbFEeMoxZHL3BEGSZVjUCkBfwru3cobGwCdDZDhwO8QzToJ1F3RVWOVa6jzd0xOSJrDMiIo85n1iwpHLOSuTWzXZXrq5R6Q856E/iXHEFlWtxyExUGNmhzHCS672eX/uI+RDc/6aSuWnhoaijrmZ1EXajFChzKcmdWmjabhsw+VmX1F/L0vFPymi 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: Add a stress test which runs one more process than we have CPUs spinning through a very recursive function with frequent syscalls immediately prior to return and signals being injected every 100ms. The goal is to flag up any scheduling related issues, for example failure to ensure that barriers are inserted when moving a GCS using task to another CPU. The test runs for a configurable amount of time, defaulting to 10 seconds. Signed-off-by: Mark Brown --- tools/testing/selftests/arm64/gcs/.gitignore | 2 + tools/testing/selftests/arm64/gcs/Makefile | 6 +- tools/testing/selftests/arm64/gcs/asm-offsets.h | 0 .../selftests/arm64/gcs/gcs-stress-thread.S | 311 ++++++++++++ tools/testing/selftests/arm64/gcs/gcs-stress.c | 532 +++++++++++++++++++++ 5 files changed, 850 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/arm64/gcs/.gitignore b/tools/testing/selftests/arm64/gcs/.gitignore index 0c86f53f68ad..1e8d1f6b27f2 100644 --- a/tools/testing/selftests/arm64/gcs/.gitignore +++ b/tools/testing/selftests/arm64/gcs/.gitignore @@ -1,3 +1,5 @@ basic-gcs libc-gcs gcs-locking +gcs-stress +gcs-stress-thread diff --git a/tools/testing/selftests/arm64/gcs/Makefile b/tools/testing/selftests/arm64/gcs/Makefile index 2173d6275956..d8b06ca51e22 100644 --- a/tools/testing/selftests/arm64/gcs/Makefile +++ b/tools/testing/selftests/arm64/gcs/Makefile @@ -6,7 +6,8 @@ # nolibc. # -TEST_GEN_PROGS := basic-gcs libc-gcs gcs-locking +TEST_GEN_PROGS := basic-gcs libc-gcs gcs-locking gcs-stress +TEST_GEN_PROGS_EXTENDED := gcs-stress-thread LDLIBS+=-lpthread @@ -18,3 +19,6 @@ $(OUTPUT)/basic-gcs: basic-gcs.c -I../../../../../usr/include \ -std=gnu99 -I../.. -g \ -ffreestanding -Wall $^ -o $@ -lgcc + +$(OUTPUT)/gcs-stress-thread: gcs-stress-thread.S + $(CC) -nostdlib $^ -o $@ diff --git a/tools/testing/selftests/arm64/gcs/asm-offsets.h b/tools/testing/selftests/arm64/gcs/asm-offsets.h new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/tools/testing/selftests/arm64/gcs/gcs-stress-thread.S b/tools/testing/selftests/arm64/gcs/gcs-stress-thread.S new file mode 100644 index 000000000000..4fe8695333e5 --- /dev/null +++ b/tools/testing/selftests/arm64/gcs/gcs-stress-thread.S @@ -0,0 +1,311 @@ +// Program that loops for ever doing lots of recursions and system calls, +// intended to be used as part of a stress test for GCS context switching. +// +// Copyright 2015-2023 Arm Ltd + +#include + +#define sa_sz 32 +#define sa_flags 8 +#define sa_handler 0 +#define sa_mask_sz 8 + +#define si_code 8 + +#define SIGINT 2 +#define SIGABRT 6 +#define SIGUSR1 10 +#define SIGSEGV 11 +#define SIGUSR2 12 +#define SIGTERM 15 +#define SEGV_CPERR 10 + +#define SA_NODEFER 1073741824 +#define SA_SIGINFO 4 +#define ucontext_regs 184 + +#define PR_SET_SHADOW_STACK_STATUS 72 +# define PR_SHADOW_STACK_ENABLE (1UL << 0) + +#define GCSPR_EL0 S3_3_C2_C5_1 + +.macro function name + .macro endfunction + .type \name, @function + .purgem endfunction + .endm +\name: +.endm + +// Print a single character x0 to stdout +// Clobbers x0-x2,x8 +function putc + str x0, [sp, #-16]! + + mov x0, #1 // STDOUT_FILENO + mov x1, sp + mov x2, #1 + mov x8, #__NR_write + svc #0 + + add sp, sp, #16 + ret +endfunction +.globl putc + +// Print a NUL-terminated string starting at address x0 to stdout +// Clobbers x0-x3,x8 +function puts + mov x1, x0 + + mov x2, #0 +0: ldrb w3, [x0], #1 + cbz w3, 1f + add x2, x2, #1 + b 0b + +1: mov w0, #1 // STDOUT_FILENO + mov x8, #__NR_write + svc #0 + + ret +endfunction +.globl puts + +// Utility macro to print a literal string +// Clobbers x0-x4,x8 +.macro puts string + .pushsection .rodata.str1.1, "aMS", @progbits, 1 +.L__puts_literal\@: .string "\string" + .popsection + + ldr x0, =.L__puts_literal\@ + bl puts +.endm + +// Print an unsigned decimal number x0 to stdout +// Clobbers x0-x4,x8 +function putdec + mov x1, sp + str x30, [sp, #-32]! // Result can't be > 20 digits + + mov x2, #0 + strb w2, [x1, #-1]! // Write the NUL terminator + + mov x2, #10 +0: udiv x3, x0, x2 // div-mod loop to generate the digits + msub x0, x3, x2, x0 + add w0, w0, #'0' + strb w0, [x1, #-1]! + mov x0, x3 + cbnz x3, 0b + + ldrb w0, [x1] + cbnz w0, 1f + mov w0, #'0' // Print "0" for 0, not "" + strb w0, [x1, #-1]! + +1: mov x0, x1 + bl puts + + ldr x30, [sp], #32 + ret +endfunction +.globl putdec + +// Print an unsigned decimal number x0 to stdout, followed by a newline +// Clobbers x0-x5,x8 +function putdecn + mov x5, x30 + + bl putdec + mov x0, #'\n' + bl putc + + ret x5 +endfunction +.globl putdecn + +// Fill x1 bytes starting at x0 with 0. +// Clobbers x1, x2. +function memclr + mov w2, #0 +endfunction +.globl memclr + // fall through to memfill + +// Trivial memory fill: fill x1 bytes starting at address x0 with byte w2 +// Clobbers x1 +function memfill + cmp x1, #0 + b.eq 1f + +0: strb w2, [x0], #1 + subs x1, x1, #1 + b.ne 0b + +1: ret +endfunction +.globl memfill + +// w0: signal number +// x1: sa_action +// w2: sa_flags +// Clobbers x0-x6,x8 +function setsignal + str x30, [sp, #-((sa_sz + 15) / 16 * 16 + 16)]! + + mov w4, w0 + mov x5, x1 + mov w6, w2 + + add x0, sp, #16 + mov x1, #sa_sz + bl memclr + + mov w0, w4 + add x1, sp, #16 + str w6, [x1, #sa_flags] + str x5, [x1, #sa_handler] + mov x2, #0 + mov x3, #sa_mask_sz + mov x8, #__NR_rt_sigaction + svc #0 + + cbz w0, 1f + + puts "sigaction failure\n" + b abort + +1: ldr x30, [sp], #((sa_sz + 15) / 16 * 16 + 16) + ret +endfunction + + +function tickle_handler + // Perhaps collect GCSPR_EL0 here in future? + ret +endfunction + +function terminate_handler + mov w21, w0 + mov x20, x2 + + puts "Terminated by signal " + mov w0, w21 + bl putdec + puts ", no error\n" + + mov x0, #0 + mov x8, #__NR_exit + svc #0 +endfunction + +function segv_handler + // stash the siginfo_t * + mov x20, x1 + + // Disable GCS, we don't want additional faults logging things + mov x0, PR_SET_SHADOW_STACK_STATUS + mov x1, xzr + mov x2, xzr + mov x3, xzr + mov x4, xzr + mov x5, xzr + mov x8, #__NR_prctl + svc #0 + + puts "Got SIGSEGV code " + + ldr x21, [x20, #si_code] + mov x0, x21 + bl putdec + + // GCS faults should have si_code SEGV_CPERR + cmp x21, #SEGV_CPERR + bne 1f + + puts " (GCS violation)" +1: + mov x0, '\n' + bl putc + b abort +endfunction + +// Recurse x20 times +.macro recurse id +function recurse\id + stp x29, x30, [sp, #-16]! + mov x29, sp + + cmp x20, 0 + beq 1f + sub x20, x20, 1 + bl recurse\id + +1: + ldp x29, x30, [sp], #16 + + // Do a syscall immediately prior to returning to try to provoke + // scheduling and migration at a point where coherency issues + // might trigger. + mov x8, #__NR_getpid + svc #0 + + ret +endfunction +.endmacro + +// Generate and use two copies so we're changing the GCS contents +recurse 1 +recurse 2 + +.globl _start +function _start + // Run with GCS + mov x0, PR_SET_SHADOW_STACK_STATUS + mov x1, PR_SHADOW_STACK_ENABLE + mov x2, xzr + mov x3, xzr + mov x4, xzr + mov x5, xzr + mov x8, #__NR_prctl + svc #0 + cbz x0, 1f + puts "Failed to enable GCS\n" + b abort +1: + + mov w0, #SIGTERM + adr x1, terminate_handler + mov w2, #SA_SIGINFO + bl setsignal + + mov w0, #SIGUSR1 + adr x1, tickle_handler + mov w2, #SA_SIGINFO + orr w2, w2, #SA_NODEFER + bl setsignal + + mov w0, #SIGSEGV + adr x1, segv_handler + mov w2, #SA_SIGINFO + orr w2, w2, #SA_NODEFER + bl setsignal + + puts "Running\n" + +loop: + // Small recursion depth so we're frequently flipping between + // the two recursors and changing what's on the stack + mov x20, #5 + bl recurse1 + mov x20, #5 + bl recurse2 + b loop +endfunction + +abort: + mov x0, #255 + mov x8, #__NR_exit + svc #0 diff --git a/tools/testing/selftests/arm64/gcs/gcs-stress.c b/tools/testing/selftests/arm64/gcs/gcs-stress.c new file mode 100644 index 000000000000..23fd8ec37bdc --- /dev/null +++ b/tools/testing/selftests/arm64/gcs/gcs-stress.c @@ -0,0 +1,532 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright (C) 2022-3 ARM Limited. + */ + +#define _GNU_SOURCE +#define _POSIX_C_SOURCE 199309L + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "../../kselftest.h" + +struct child_data { + char *name, *output; + pid_t pid; + int stdout; + bool output_seen; + bool exited; + int exit_status; + int exit_signal; +}; + +static int epoll_fd; +static struct child_data *children; +static struct epoll_event *evs; +static int tests; +static int num_children; +static bool terminate; + +static int startup_pipe[2]; + +static int num_processors(void) +{ + long nproc = sysconf(_SC_NPROCESSORS_CONF); + if (nproc < 0) { + perror("Unable to read number of processors\n"); + exit(EXIT_FAILURE); + } + + return nproc; +} + +static void start_thread(struct child_data *child) +{ + int ret, pipefd[2], i; + struct epoll_event ev; + + ret = pipe(pipefd); + if (ret != 0) + ksft_exit_fail_msg("Failed to create stdout pipe: %s (%d)\n", + strerror(errno), errno); + + child->pid = fork(); + if (child->pid == -1) + ksft_exit_fail_msg("fork() failed: %s (%d)\n", + strerror(errno), errno); + + if (!child->pid) { + /* + * In child, replace stdout with the pipe, errors to + * stderr from here as kselftest prints to stdout. + */ + ret = dup2(pipefd[1], 1); + if (ret == -1) { + fprintf(stderr, "dup2() %d\n", errno); + exit(EXIT_FAILURE); + } + + /* + * Duplicate the read side of the startup pipe to + * FD 3 so we can close everything else. + */ + ret = dup2(startup_pipe[0], 3); + if (ret == -1) { + fprintf(stderr, "dup2() %d\n", errno); + exit(EXIT_FAILURE); + } + + /* + * Very dumb mechanism to clean open FDs other than + * stdio. We don't want O_CLOEXEC for the pipes... + */ + for (i = 4; i < 8192; i++) + close(i); + + /* + * Read from the startup pipe, there should be no data + * and we should block until it is closed. We just + * carry on on error since this isn't super critical. + */ + ret = read(3, &i, sizeof(i)); + if (ret < 0) + fprintf(stderr, "read(startp pipe) failed: %s (%d)\n", + strerror(errno), errno); + if (ret > 0) + fprintf(stderr, "%d bytes of data on startup pipe\n", + ret); + close(3); + + ret = execl("gcs-stress-thread", "gcs-stress-thread", NULL); + fprintf(stderr, "execl(gcs-stress-thread) failed: %d (%s)\n", + errno, strerror(errno)); + + exit(EXIT_FAILURE); + } else { + /* + * In parent, remember the child and close our copy of the + * write side of stdout. + */ + close(pipefd[1]); + child->stdout = pipefd[0]; + child->output = NULL; + child->exited = false; + child->output_seen = false; + + ev.events = EPOLLIN | EPOLLHUP; + ev.data.ptr = child; + + ret = asprintf(&child->name, "Thread-%d", child->pid); + if (ret == -1) + ksft_exit_fail_msg("asprintf() failed\n"); + + ret = epoll_ctl(epoll_fd, EPOLL_CTL_ADD, child->stdout, &ev); + if (ret < 0) { + ksft_exit_fail_msg("%s EPOLL_CTL_ADD failed: %s (%d)\n", + child->name, strerror(errno), errno); + } + } + + ksft_print_msg("Started %s\n", child->name); + num_children++; +} + +static bool child_output_read(struct child_data *child) +{ + char read_data[1024]; + char work[1024]; + int ret, len, cur_work, cur_read; + + ret = read(child->stdout, read_data, sizeof(read_data)); + if (ret < 0) { + if (errno == EINTR) + return true; + + ksft_print_msg("%s: read() failed: %s (%d)\n", + child->name, strerror(errno), + errno); + return false; + } + len = ret; + + child->output_seen = true; + + /* Pick up any partial read */ + if (child->output) { + strncpy(work, child->output, sizeof(work) - 1); + cur_work = strnlen(work, sizeof(work)); + free(child->output); + child->output = NULL; + } else { + cur_work = 0; + } + + cur_read = 0; + while (cur_read < len) { + work[cur_work] = read_data[cur_read++]; + + if (work[cur_work] == '\n') { + work[cur_work] = '\0'; + ksft_print_msg("%s: %s\n", child->name, work); + cur_work = 0; + } else { + cur_work++; + } + } + + if (cur_work) { + work[cur_work] = '\0'; + ret = asprintf(&child->output, "%s", work); + if (ret == -1) + ksft_exit_fail_msg("Out of memory\n"); + } + + return false; +} + +static void child_output(struct child_data *child, uint32_t events, + bool flush) +{ + bool read_more; + + if (events & EPOLLIN) { + do { + read_more = child_output_read(child); + } while (read_more); + } + + if (events & EPOLLHUP) { + close(child->stdout); + child->stdout = -1; + flush = true; + } + + if (flush && child->output) { + ksft_print_msg("%s: %s\n", child->name, child->output); + free(child->output); + child->output = NULL; + } +} + +static void child_tickle(struct child_data *child) +{ + if (child->output_seen && !child->exited) + kill(child->pid, SIGUSR1); +} + +static void child_stop(struct child_data *child) +{ + if (!child->exited) + kill(child->pid, SIGTERM); +} + +static void child_cleanup(struct child_data *child) +{ + pid_t ret; + int status; + bool fail = false; + + if (!child->exited) { + do { + ret = waitpid(child->pid, &status, 0); + if (ret == -1 && errno == EINTR) + continue; + + if (ret == -1) { + ksft_print_msg("waitpid(%d) failed: %s (%d)\n", + child->pid, strerror(errno), + errno); + fail = true; + break; + } + + if (WIFEXITED(status)) { + child->exit_status = WEXITSTATUS(status); + child->exited = true; + } + + if (WIFSIGNALED(status)) { + child->exit_signal = WTERMSIG(status); + ksft_print_msg("%s: Exited due to signal %d\n", + child->name); + fail = true; + child->exited = true; + } + } while (!child->exited); + } + + if (!child->output_seen) { + ksft_print_msg("%s no output seen\n", child->name); + fail = true; + } + + if (child->exit_status != 0) { + ksft_print_msg("%s exited with error code %d\n", + child->name, child->exit_status); + fail = true; + } + + ksft_test_result(!fail, "%s\n", child->name); +} + +static void handle_child_signal(int sig, siginfo_t *info, void *context) +{ + int i; + bool found = false; + + for (i = 0; i < num_children; i++) { + if (children[i].pid == info->si_pid) { + children[i].exited = true; + children[i].exit_status = info->si_status; + found = true; + break; + } + } + + if (!found) + ksft_print_msg("SIGCHLD for unknown PID %d with status %d\n", + info->si_pid, info->si_status); +} + +static void handle_exit_signal(int sig, siginfo_t *info, void *context) +{ + int i; + + /* If we're already exiting then don't signal again */ + if (terminate) + return; + + ksft_print_msg("Got signal, exiting...\n"); + + terminate = true; + + /* + * This should be redundant, the main loop should clean up + * after us, but for safety stop everything we can here. + */ + for (i = 0; i < num_children; i++) + child_stop(&children[i]); +} + +/* Handle any pending output without blocking */ +static void drain_output(bool flush) +{ + int ret = 1; + int i; + + while (ret > 0) { + ret = epoll_wait(epoll_fd, evs, tests, 0); + if (ret < 0) { + if (errno == EINTR) + continue; + ksft_print_msg("epoll_wait() failed: %s (%d)\n", + strerror(errno), errno); + } + + for (i = 0; i < ret; i++) + child_output(evs[i].data.ptr, evs[i].events, flush); + } +} + +static const struct option options[] = { + { "timeout", required_argument, NULL, 't' }, + { } +}; + +int main(int argc, char **argv) +{ + int seen_children; + bool all_children_started = false; + int gcs_threads; + int timeout = 10; + int ret, cpus, i, c; + struct sigaction sa; + + while ((c = getopt_long(argc, argv, "t:", options, NULL)) != -1) { + switch (c) { + case 't': + ret = sscanf(optarg, "%d", &timeout); + if (ret != 1) + ksft_exit_fail_msg("Failed to parse timeout %s\n", + optarg); + break; + default: + ksft_exit_fail_msg("Unknown argument\n"); + } + } + + cpus = num_processors(); + tests = 0; + + if (getauxval(AT_HWCAP2) & HWCAP2_GCS) { + /* One extra thread, trying to trigger migrations */ + gcs_threads = cpus + 1; + tests += gcs_threads; + } else { + gcs_threads = 0; + } + + ksft_print_header(); + ksft_set_plan(tests); + + ksft_print_msg("%d CPUs, %d GCS threads\n", + cpus, gcs_threads); + + if (!tests) + ksft_exit_skip("No tests scheduled\n"); + + if (timeout > 0) + ksft_print_msg("Will run for %ds\n", timeout); + else + ksft_print_msg("Will run until terminated\n"); + + children = calloc(sizeof(*children), tests); + if (!children) + ksft_exit_fail_msg("Unable to allocate child data\n"); + + ret = epoll_create1(EPOLL_CLOEXEC); + if (ret < 0) + ksft_exit_fail_msg("epoll_create1() failed: %s (%d)\n", + strerror(errno), ret); + epoll_fd = ret; + + /* Create a pipe which children will block on before execing */ + ret = pipe(startup_pipe); + if (ret != 0) + ksft_exit_fail_msg("Failed to create startup pipe: %s (%d)\n", + strerror(errno), errno); + + /* Get signal handers ready before we start any children */ + memset(&sa, 0, sizeof(sa)); + sa.sa_sigaction = handle_exit_signal; + sa.sa_flags = SA_RESTART | SA_SIGINFO; + sigemptyset(&sa.sa_mask); + ret = sigaction(SIGINT, &sa, NULL); + if (ret < 0) + ksft_print_msg("Failed to install SIGINT handler: %s (%d)\n", + strerror(errno), errno); + ret = sigaction(SIGTERM, &sa, NULL); + if (ret < 0) + ksft_print_msg("Failed to install SIGTERM handler: %s (%d)\n", + strerror(errno), errno); + sa.sa_sigaction = handle_child_signal; + ret = sigaction(SIGCHLD, &sa, NULL); + if (ret < 0) + ksft_print_msg("Failed to install SIGCHLD handler: %s (%d)\n", + strerror(errno), errno); + + evs = calloc(tests, sizeof(*evs)); + if (!evs) + ksft_exit_fail_msg("Failed to allocated %d epoll events\n", + tests); + + for (i = 0; i < gcs_threads; i++) + start_thread(&children[i]); + + /* + * All children started, close the startup pipe and let them + * run. + */ + close(startup_pipe[0]); + close(startup_pipe[1]); + + timeout *= 10; + for (;;) { + /* Did we get a signal asking us to exit? */ + if (terminate) + break; + + /* + * Timeout is counted in 100ms with no output, the + * tests print during startup then are silent when + * running so this should ensure they all ran enough + * to install the signal handler, this is especially + * useful in emulation where we will both be slow and + * likely to have a large set of VLs. + */ + ret = epoll_wait(epoll_fd, evs, tests, 100); + if (ret < 0) { + if (errno == EINTR) + continue; + ksft_exit_fail_msg("epoll_wait() failed: %s (%d)\n", + strerror(errno), errno); + } + + /* Output? */ + if (ret > 0) { + for (i = 0; i < ret; i++) { + child_output(evs[i].data.ptr, evs[i].events, + false); + } + continue; + } + + /* Otherwise epoll_wait() timed out */ + + /* + * If the child processes have not produced output they + * aren't actually running the tests yet. + */ + if (!all_children_started) { + seen_children = 0; + + for (i = 0; i < num_children; i++) + if (children[i].output_seen || + children[i].exited) + seen_children++; + + if (seen_children != num_children) { + ksft_print_msg("Waiting for %d children\n", + num_children - seen_children); + continue; + } + + all_children_started = true; + } + + ksft_print_msg("Sending signals, timeout remaining: %d00ms\n", + timeout); + + for (i = 0; i < num_children; i++) + child_tickle(&children[i]); + + /* Negative timeout means run indefinitely */ + if (timeout < 0) + continue; + if (--timeout == 0) + break; + } + + ksft_print_msg("Finishing up...\n"); + terminate = true; + + for (i = 0; i < tests; i++) + child_stop(&children[i]); + + drain_output(false); + + for (i = 0; i < tests; i++) + child_cleanup(&children[i]); + + drain_output(true); + + ksft_print_cnts(); + + return 0; +} From patchwork Wed Nov 22 09:42:48 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Brown X-Patchwork-Id: 13464432 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 201AFC61D9D for ; Wed, 22 Nov 2023 09:48:07 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id AAC686B05D6; Wed, 22 Nov 2023 04:48:06 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id A5CB36B05D7; Wed, 22 Nov 2023 04:48:06 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8FD146B05D8; Wed, 22 Nov 2023 04:48:06 -0500 (EST) 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 7C0586B05D6 for ; Wed, 22 Nov 2023 04:48:06 -0500 (EST) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 576991CB40F for ; Wed, 22 Nov 2023 09:48:06 +0000 (UTC) X-FDA: 81485114172.28.64586E9 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf06.hostedemail.com (Postfix) with ESMTP id 80E32180014 for ; Wed, 22 Nov 2023 09:48:04 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=ky+pdXFq; spf=pass (imf06.hostedemail.com: domain of broonie@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=broonie@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1700646484; 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=lARH0LXYpPyl6/xq0Q87ae+WghUwtBsjqC5c6hCRo2I=; b=e6lYZa4Mz/GzQ2m+OvK4IiVXFl4D+kRO6r33shtJpahapkUxp7jP+aANcM7BJ7iEGj6hdH FwpcNpRoBpPUp7fTc7R9Bhh4oUz7ZkJ30DpGZYrid7w6mvYE6ZYcbNvOHUe3t3KFc6ipxi B6xVbTuz7JjD4BwG0j7gavgY9E26uyA= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1700646484; a=rsa-sha256; cv=none; b=u+jca3lpRC/6t1xEHc62r4liu4nVeywtT6eQHtMENJP08mLfgWhqRHEQ7oer++YQ2n0n68 I/qkXSUfaBcAHiWOo7vL4leDLHsXR7FhrhTDYJVbNnvmd2tTX17xOhMVV6voUGFSwYy0s0 NOWDeqVO4I8Lh8xoTl6H/tssAZ3QVrU= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=ky+pdXFq; spf=pass (imf06.hostedemail.com: domain of broonie@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=broonie@kernel.org; dmarc=pass (policy=none) header.from=kernel.org Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id BDF1361CCB; Wed, 22 Nov 2023 09:48:03 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 07F11C433C8; Wed, 22 Nov 2023 09:47:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1700646483; bh=eSCNvWDUi17fRk35d8u5Mk3lnX0Qn/saq0Me9ODMcog=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=ky+pdXFqPVvbwLOQUIF5KSUAApd5yTfp+EINHRoVxbFevJpHoyzBoj2b888ngusWk yaqfFa8QVfN25G/u7QdOYZUJScjAIgS0W5IYixhIMpxpcucqib0pQ39x4ovpU/4ei1 gVrQagiEjXeCrqjKfim7+VoJe9JtEaWSBTdRJi46N1fKsiwXaGv2WafWlU+6dHq8lN uzmYGPrRen1M+iAo+Yib/uD572QUkvKLXEUJXCTK8GbUTZNFIdU2G16bmX0oplRg+W IAO1TThYj30cSZq36jN5aITacV3zTcxa2zJi6aLLFWvZTAsyIT/vGN0hdk74aRrUiq hXdb6F28yuOyA== From: Mark Brown Date: Wed, 22 Nov 2023 09:42:48 +0000 Subject: [PATCH v7 38/39] kselftest/arm64: Enable GCS for the FP stress tests MIME-Version: 1.0 Message-Id: <20231122-arm64-gcs-v7-38-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=3085; i=broonie@kernel.org; h=from:subject:message-id; bh=eSCNvWDUi17fRk35d8u5Mk3lnX0Qn/saq0Me9ODMcog=; b=owEBbQGS/pANAwAKASTWi3JdVIfQAcsmYgBlXc0TxVaHhiADHB3bt1cHAI20VA1kqzEuj0fpj P2J54lQhOyJATMEAAEKAB0WIQSt5miqZ1cYtZ/in+ok1otyXVSH0AUCZV3NEwAKCRAk1otyXVSH 0JlxB/9FubKE8WWTJdmyYLHbycLew6cc6wkr4QsEGDYSbvsH6yUlHMaOFlF8K4bCuYzdet4kVjB djl3tnOX8toJbJGgztTuvNDASojBPp75eEaXXBNPtkn8zEZOzMvgXvd09/n0HmXffwhqFr2N495 TLue+HbFeVDNc0fCIhADVuCrpK+5PEiISx974vYRYvwsGyqLh59DxFuqqJgWVatzyni7JXJWWmJ 6wgvUBPx9mGzmUZGFi5H5tD93ZTl3+/CXDEPsk4m3sH7wOkaH9ohDus3PfpTRW42Vdad9uAvR4I A8kL/AC9KhHYrCxP1U84b4R0Y0z+oLl66/wYlFmPpOJWkdaU X-Developer-Key: i=broonie@kernel.org; a=openpgp; fpr=3F2568AAC26998F9E813A1C5C3F436CA30F5D8EB X-Stat-Signature: jo6jdfrjwwxsuyxubcq7dqxkhhosyksg X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 80E32180014 X-Rspam-User: X-HE-Tag: 1700646484-449353 X-HE-Meta: U2FsdGVkX1+ySzUljNezSlnMpPWqbu5ab4njrBt3htTZDqDxcc6TaltBCOhapcme5bT5c9Wus1Fw0QcPsQlIbJcWV4W9wcCac2I5+3SRqagumcBnVvW5mnXTmDikdjFpCMyMCysQcC+eZy2QD+Nlu/8Ra6XLdd/yT09td18zAgd8mBnnw7Zg4ZswfhnDJtPd5geXGBiG3/0phA1YoLRLYMeapxkG5Udl936ZMirU1SmPyl07L/TXHd/Itx65/J1CRbP3U96gvu34ni/tZO2RbVuj5X17gVTzhqh+fOIeJ5NkQtR8jG+28jTHzo0viKgfiXNtOuVLK2acMOTxVw/TiZF0sRYponQfCGRHFJ98tqWUIV3Kd72UjVM71XsUvI7Tv5GgMRIppBV9MtuPAzR7SWvWecwIZZkKHXLbomXllRCrrkNp1rpmMF537iz2SY5p9pqu/n755oiDqqzKPc30SWS0zrJzTajAeE8n2CVuw0SAuy6CcPPvllhI8pfYL0bumYhp9WnSgc/AufkTfwlt9aV3U4FZVAu7ugCJ+8Buok4ZgKtfuIL6dpmagbuHHMWiTJKPe36j3WjHQa0gHPNwB74/IPvic3grApWH1JTyQJJFP30/zkXSOMFU1ThGuUEBg0u6J+TicWOcoMTptZROG5KTZkJM5pDo1VbL5fIabdJFHccUIRNC7tB0It4K4KpZfBo+O88cZWvELEWnvAQryGsCGjYs6MQhRXPiX48ySBxF8Ggm1Ty66TqoBmpsi4xCP7b1mJUgWx3R7YjqgHKSMybb0XENZ74QU4qgk8ZUAR4W6EcQROu6v94bPI/dRmBsns+f6bGxhwDOoqdNcE0ayklcVmh0LxEdCZE9rrnwAj21HL6ticqLowGNLQv0l/mW7/lGSOxMLgT6jsbwfByhTZ20+YguhPeZlmoMvqt/uq+HvINlDLBYmpZ9nU2NmMCw4YBIW/vMSq+yTrGX5QP eItqvhyL LzmnOXIIaJz1HxwJB+Qpt1lfPQAkPy/OAvyOkfrAK2wwa/L6VMuQzQOsjp2Q0/vsCtah5vrvqfkPjB/A1LVeorLG2gKD4m7uH/reOJCjb7tyHIYr7s7m5RZbyRdfcAgCir7gjACWFXQQWfJGQtucSUKEYhQdTHZrWHrRT92iaQn/Yk6hQzr0Qc/NckT8caxRrA/Vn2d6U0DXbHmMg7a9k4t22iH65pbcjRDHVVXKox20KUgCDfJoWtzcITWglG5u761564B8v1jHWMqKbb9U2oLPcNnesxQGnM8RdXVLJog03cXkaiEg6o6AyBOqJvY+7XX6g 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: While it's a bit off topic for them the floating point stress tests do give us some coverage of context thrashing cases, and also of active signal delivery separate to the relatively complicated framework in the actual signals tests. Have the tests enable GCS on startup, ignoring failures so they continue to work as before on systems without GCS. Signed-off-by: Mark Brown --- tools/testing/selftests/arm64/fp/assembler.h | 15 +++++++++++++++ tools/testing/selftests/arm64/fp/fpsimd-test.S | 2 ++ tools/testing/selftests/arm64/fp/sve-test.S | 2 ++ tools/testing/selftests/arm64/fp/za-test.S | 2 ++ tools/testing/selftests/arm64/fp/zt-test.S | 2 ++ 5 files changed, 23 insertions(+) diff --git a/tools/testing/selftests/arm64/fp/assembler.h b/tools/testing/selftests/arm64/fp/assembler.h index 9b38a0da407d..7012f9f796de 100644 --- a/tools/testing/selftests/arm64/fp/assembler.h +++ b/tools/testing/selftests/arm64/fp/assembler.h @@ -65,4 +65,19 @@ endfunction bl puts .endm +#define PR_SET_SHADOW_STACK_STATUS 72 +# define PR_SHADOW_STACK_ENABLE (1UL << 0) + +.macro enable_gcs + // Run with GCS + mov x0, PR_SET_SHADOW_STACK_STATUS + mov x1, PR_SHADOW_STACK_ENABLE + mov x2, xzr + mov x3, xzr + mov x4, xzr + mov x5, xzr + mov x8, #__NR_prctl + svc #0 +.endm + #endif /* ! ASSEMBLER_H */ diff --git a/tools/testing/selftests/arm64/fp/fpsimd-test.S b/tools/testing/selftests/arm64/fp/fpsimd-test.S index 8b960d01ed2e..b16fb7f42e3e 100644 --- a/tools/testing/selftests/arm64/fp/fpsimd-test.S +++ b/tools/testing/selftests/arm64/fp/fpsimd-test.S @@ -215,6 +215,8 @@ endfunction // Main program entry point .globl _start function _start + enable_gcs + mov x23, #0 // signal count mov w0, #SIGINT diff --git a/tools/testing/selftests/arm64/fp/sve-test.S b/tools/testing/selftests/arm64/fp/sve-test.S index 547d077e3517..e29ab7c4e824 100644 --- a/tools/testing/selftests/arm64/fp/sve-test.S +++ b/tools/testing/selftests/arm64/fp/sve-test.S @@ -378,6 +378,8 @@ endfunction // Main program entry point .globl _start function _start + enable_gcs + mov x23, #0 // Irritation signal count mov w0, #SIGINT diff --git a/tools/testing/selftests/arm64/fp/za-test.S b/tools/testing/selftests/arm64/fp/za-test.S index 9dcd70911397..f789694fa3ea 100644 --- a/tools/testing/selftests/arm64/fp/za-test.S +++ b/tools/testing/selftests/arm64/fp/za-test.S @@ -231,6 +231,8 @@ endfunction // Main program entry point .globl _start function _start + enable_gcs + mov x23, #0 // signal count mov w0, #SIGINT diff --git a/tools/testing/selftests/arm64/fp/zt-test.S b/tools/testing/selftests/arm64/fp/zt-test.S index d63286397638..ea5e55310705 100644 --- a/tools/testing/selftests/arm64/fp/zt-test.S +++ b/tools/testing/selftests/arm64/fp/zt-test.S @@ -200,6 +200,8 @@ endfunction // Main program entry point .globl _start function _start + enable_gcs + mov x23, #0 // signal count mov w0, #SIGINT From patchwork Wed Nov 22 09:42:49 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Brown X-Patchwork-Id: 13464433 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 DCC9EC61D9D for ; Wed, 22 Nov 2023 09:48:15 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7C7676B05D8; Wed, 22 Nov 2023 04:48:15 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 776B36B05D9; Wed, 22 Nov 2023 04:48:15 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 63EDF6B05DA; Wed, 22 Nov 2023 04:48:15 -0500 (EST) 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 5247E6B05D8 for ; Wed, 22 Nov 2023 04:48:15 -0500 (EST) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 2F9CA1A0839 for ; Wed, 22 Nov 2023 09:48:15 +0000 (UTC) X-FDA: 81485114550.30.28DAAA9 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf23.hostedemail.com (Postfix) with ESMTP id 601C514000E for ; Wed, 22 Nov 2023 09:48:13 +0000 (UTC) Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=uZVDPcKF; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf23.hostedemail.com: domain of broonie@kernel.org designates 139.178.84.217 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=1700646493; 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=GQzGkKqFdDjtW47t1neXVo9WE7wwJKuc9BPZVSbCuv0=; b=7zKi14zO2kELnwjO1Y9BTF6PtOG3z8BEO1pJ2rQkUgfe3b5N4gey4WSvP7ajI7n6ZvKr/P AxkvKShnDiQ0LPofCfSAfDmSnm4KUl1xbbp6dTsxt8RQ5IK095uGyZBnbYbJT+yQpP6I7F TJCN6UlSElOm/RD7GNum9U/VKBS6xAQ= ARC-Authentication-Results: i=1; imf23.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=uZVDPcKF; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf23.hostedemail.com: domain of broonie@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=broonie@kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1700646493; a=rsa-sha256; cv=none; b=LmfbVGh9CseE0GjZ+r07O2FOV/gn2E4+PgN35QUAeG4z3II7nlj1pmhWGTfNdJD2/SVpSh ZBT/V9lBmTc+pcpsmXtta4siNXFpkJ16wL7LBqjcPTSNuitTzFaTUJWAoSLNABLMLdltI1 szna6bkHxC1lUuNZtr3V24PfRmJatKc= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 78B2761CAF; Wed, 22 Nov 2023 09:48:12 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id EB795C433C9; Wed, 22 Nov 2023 09:48:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1700646492; bh=+DcIGbRVvw60QaNepPSRFBnETXstp9aOb5bVveLOaFg=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=uZVDPcKFka+wlMJXjfzajobqpM6EaIscRoqp8YtTGuiZLIBclLh/18h+beyzfCCak O611N3cFEGc9ooTue7jvmAjHduLzlohXVD9CMIjCWn79H/TlnFbuXsNc8sX94PgzpD CziDJmtJS4Lxr7Q90BN45O6K7RZDp60OhWbeVYOizHCAv+pOxqIKTxJVflyNZ+J6XH /WFEwQWOydbHD+eyzieR8s/u37khmup0ORNwiLjGPrd5KdyWHwd+12K9tNSjVXsVMU h4yc9Yk3hd0sUoKwmILtXr0+4rKmOlfPSNZJELuR9deSXlQR1au1DbSTFSMH3s0Dw3 UjGBYMzpNCEaQ== From: Mark Brown Date: Wed, 22 Nov 2023 09:42:49 +0000 Subject: [PATCH v7 39/39] kselftest/clone3: Enable GCS in the clone3 selftests MIME-Version: 1.0 Message-Id: <20231122-arm64-gcs-v7-39-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=2416; i=broonie@kernel.org; h=from:subject:message-id; bh=+DcIGbRVvw60QaNepPSRFBnETXstp9aOb5bVveLOaFg=; b=owEBbQGS/pANAwAKASTWi3JdVIfQAcsmYgBlXc0Uf5JbJdKOG4lKmXzlRMK5DRWagk6L/dGG+ 23SAtCf39uJATMEAAEKAB0WIQSt5miqZ1cYtZ/in+ok1otyXVSH0AUCZV3NFAAKCRAk1otyXVSH 0HKwB/44qmjC2meQurJ74cEJ5LyDpr1HY6Y1uWSWnrnkSyRi8ifcJAyyiipIeWWT2m2p19Mpf+V 5qrOSTOZdbGPeoAgIKaULvFGqHP/ksUE8h92lW7xsOkWWWvpcMBPOCkmhlsIYyaWpVOSMobVII8 PBDQf7mV3cJuvY8YfCMIkBz3Ul6mwtxdu9vNby4YUtXDRU9U3cGAcL6XtW3KntS2xI0+aDTcs7i YX48MJ+hv3H+CYavScUNHVuNSx8y1Q3eta1i1xV3wGLOaAAxBPAvtoCuwkfsLCOAy6jNqbmbBPU 2P7pVrY78UsvDTA8mvMcJrJSnASVS4HY2NdgEMkZbCxKMCbn X-Developer-Key: i=broonie@kernel.org; a=openpgp; fpr=3F2568AAC26998F9E813A1C5C3F436CA30F5D8EB X-Rspamd-Queue-Id: 601C514000E X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: q9mj8kchu43xqwupfcmnmf6kyukoccau X-HE-Tag: 1700646493-846361 X-HE-Meta: U2FsdGVkX18HPBpuiX7ny0fbprQwRaiRUpLZNOhxRUQ+fHAFz1BRSz760OCW3P08q5hcw5G9TeISxOiT+HGRcXSPUjDDreUVkvj5oAnfDC5bSvIN26W8yKCjtWdF2IB8S45uUNSxN++7W70FLQxnz3vd2MxiwAo17AvZMpEwfZGPnrAt0NYX/jLkVO0Ycat98d3LKFjq2viuWEQb3d3q6QxcEU4CFfNuHaNMKI1NbjslsxKwsrdbHO4KkOtUweGlh7MtCSU8majlMpp9MdEDir7cWwt0CDZ/2rnK7fzu2jFc5xBVc9B8/sTRMl1kFY6e8FdIIH8o9+WgAmJoULF74FGQdLltTNtUyV/cjA4jjCVqt+k8dqfUapVB0Uq10/iYFMJVa9e+HTWnXvYZ0MrS3VGxADO7DwdxbT2I/plWD7S+X323kG+CcngvQn3cIk3IuhiUDFQnPEnlVqcNLfe9sMxZysDOn9pWHgOhX5Y+7xu7KokzjdJ3ygI8gO3MG4lOyue0DLWdyy/Ijt/TS66age0dXwG3e0SBahFxhe2oTWcCh0HsfKN5OewJbt5WKHkvMCzIEDJmycbiB0XZQtKByqfXMwcD93ePRSgylIc2yIwC1F913E3CrPHEKyra1+gswMQGSxAxQ8xlWU6/t/QXYjMmE0PkPIlU2tW4KxZDw9DqsjVxcnh5RyUb9gFfhSjDlOHXDoo8GxSigvQ9meF04VtNr903DU9V7Xk2a0Bgo4lv21d07jpYvntAS7nnmFQGmlIwVOmtO1oRJ0wR4jVht1kqmhH/StQPvr8IbiEJL6Y8qU8GEuh5/+JSzeRQOKUAzDXfJw/3JP5tH2gT8i+vGdCLo4NQitiUH7ZvWo4Bb6I/0W2hORuINlvYuyuPXNQCukwxXtLEgBrksauhLfSMuWbcKzC55b3Ymk3yE07aDn+z7i0qrQemSe0xcY575qjgyy4Km1fB/qcmpF2dt7C jaRwYPfx yKKJ8cIvhUv3JzD8q8Azcxc/bNPzHy8JeBWhzhX8jW142Uy9T+2luHn+1Lh4YakQTz434+Jw7kxwr1LRLOAhICWUlDzyiymyTXIn3018Mvj9ESJ/OFJOn86Y9AxmZs3rHzYgqReT9eTT2zG3TROrsS9uMed2Lp250ZUoXL15XkfiNJDhtzApxG/bX6ybGsHcpJCtZODDr3rQ0WoRCT2SDgu/SQYABj6l6dqyIcsTrC43kL8ZTHbvSs+Z+Dhp0PY8VkEtRSmpu62z6xpmK1UKvng08wplY1x5k4Sc3ZNP3qU+ZvgCsBLqHJZHetyfErCXCG/0f 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: In order to allow testing without full userspace support for shadow stacks the clone3() selftests open code enabling them at runtime, hook up arm64 support. Signed-off-by: Mark Brown --- tools/testing/selftests/clone3/clone3.c | 37 +++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/tools/testing/selftests/clone3/clone3.c b/tools/testing/selftests/clone3/clone3.c index dbe52582573c..6114a33d6ec2 100644 --- a/tools/testing/selftests/clone3/clone3.c +++ b/tools/testing/selftests/clone3/clone3.c @@ -432,6 +432,43 @@ static inline void enable_shadow_stack(void) #endif +#ifdef __aarc64__ +#define PR_SET_SHADOW_STACK_STATUS 72 +# define PR_SHADOW_STACK_ENABLE (1UL << 0) + +#define my_syscall2(num, arg1, arg2) \ +({ \ + register long _num __asm__ ("x8") = (num); \ + register long _arg1 __asm__ ("x0") = (long)(arg1); \ + register long _arg2 __asm__ ("x1") = (long)(arg2); \ + register long _arg3 __asm__ ("x2") = 0; \ + register long _arg4 __asm__ ("x3") = 0; \ + register long _arg5 __asm__ ("x4") = 0; \ + \ + __asm__ volatile ( \ + "svc #0\n" \ + : "=r"(_arg1) \ + : "r"(_arg1), "r"(_arg2), \ + "r"(_arg3), "r"(_arg4), \ + "r"(_arg5), "r"(_num) \ + : "memory", "cc" \ + ); \ + _arg1; \ +}) + +#define ENABLE_SHADOW_STACK +static inline void enable_shadow_stack(void) +{ + int ret; + + ret = my_syscall2(__NR_prctl, PR_SET_SHADOW_STACK_STATUS, + PR_SHADOW_STACK_ENABLE); + if (ret == 0) + shadow_stack_enabled = true; +} + +#endif + #ifndef ENABLE_SHADOW_STACK static void enable_shadow_stack(void) {