From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from EUR01-HE1-obe.outbound.protection.outlook.com (mail-he1eur01on2041.outbound.protection.outlook.com [40.107.13.41]) by sourceware.org (Postfix) with ESMTPS id 05CC838417E8 for ; Wed, 14 Dec 2022 16:40:55 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 05CC838417E8 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=arm.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=armh.onmicrosoft.com; s=selector2-armh-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=utvY/WTHtUXD67TQnAFhtOTncHbJ8LguA8+ClM3LMjE=; b=qlg7EMsimrIhSEK+3eG3WjWkoWaHiQ1Ysmo5eR+kVTYOIccsi5t5hyExZlmKZPG09txwJuDgbo2dYrS1XNtjTa/cavgm1DfbvyXfztKT1PA4H3K/PWLdqoxcCuMoV2jdoshyXOTEsu0tToACKuIGu/vFilgrPKSNAzFZYNS+YVc= Received: from AS9PR06CA0056.eurprd06.prod.outlook.com (2603:10a6:20b:463::28) by AS8PR08MB7909.eurprd08.prod.outlook.com (2603:10a6:20b:52a::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5924.11; Wed, 14 Dec 2022 16:40:49 +0000 Received: from AM7EUR03FT008.eop-EUR03.prod.protection.outlook.com (2603:10a6:20b:463:cafe::c) by AS9PR06CA0056.outlook.office365.com (2603:10a6:20b:463::28) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5924.11 via Frontend Transport; Wed, 14 Dec 2022 16:40:49 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 63.35.35.123) smtp.mailfrom=arm.com; dkim=pass (signature was verified) header.d=armh.onmicrosoft.com;dmarc=pass action=none header.from=arm.com; Received-SPF: Pass (protection.outlook.com: domain of arm.com designates 63.35.35.123 as permitted sender) receiver=protection.outlook.com; client-ip=63.35.35.123; helo=64aa7808-outbound-1.mta.getcheckrecipient.com; pr=C Received: from 64aa7808-outbound-1.mta.getcheckrecipient.com (63.35.35.123) by AM7EUR03FT008.mail.protection.outlook.com (100.127.141.25) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5924.11 via Frontend Transport; Wed, 14 Dec 2022 16:40:49 +0000 Received: ("Tessian outbound aeae1c7b66fd:v130"); Wed, 14 Dec 2022 16:40:49 +0000 X-CheckRecipientChecked: true X-CR-MTA-CID: b10c597c9c853159 X-CR-MTA-TID: 64aa7808 Received: from febad7f31e4d.1 by 64aa7808-outbound-1.mta.getcheckrecipient.com id FAD02A66-3718-447D-8040-60E1A0B418E0.1; Wed, 14 Dec 2022 16:40:42 +0000 Received: from EUR01-VE1-obe.outbound.protection.outlook.com by 64aa7808-outbound-1.mta.getcheckrecipient.com with ESMTPS id febad7f31e4d.1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384); Wed, 14 Dec 2022 16:40:42 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=DtXjdLTAVhc3D2aC3W4zTFpCbzCfl58zpGIvPnMSenjeCR0Tbb1adRZtFmqwRF4bx6LKwGUIabgHOskcqfVssXrODYq6J2Kp4BBgpF1J4JZbKUBMSw8BQr+qk/HK4nz3u5tVvEkupsO+BRgzznaadPBnhoVbF6B8EJKAcRKduoIm9jqvSm+f9J/cFeo/puhj/zu37IZGeg+fgRAFerL6gQQew2D8OjvGglOi38KpLnQ++3ZphDSz5UVhM+BhHvAwe280NS9DeNnNJiBotqTipzPvuUAeyeFosa6zc1+/ilnI/cNTLHlIQYsSgRu8QWN/BT6lLBRW55z7fL6V1nkWRg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=utvY/WTHtUXD67TQnAFhtOTncHbJ8LguA8+ClM3LMjE=; b=aPKiy/IgWlny/m+omej4yvTphsOirJcVPuV8faCrOGtTeaCCODiYXxEdyETS4LdAuFi+sh1n6ufnNwhJ24idhV/fQlLdQdk16kGt61nGCIdBf09SqikA3+YDIQbiYIxzNYI5VjlK8UDYaP9wAeQKwRDymX10rENGLdFpcPI6HItpCqLMq7jqo49BRXQci6vzkA6RwabpQyAAlR/MRqbF3XlpqbYstA3qfjGnpTFVDZPSBkpBIyYBY8eLZmXjJnOMC+lk0d3dVCS9M9DOJOLi8yIoMP+8xLR0qwYxpORVrxLWrM4sdjRM5Su6xpOxNxeMja2Q2fgqhSkpJqsQOZR6WQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 40.67.248.234) smtp.rcpttodomain=gcc.gnu.org smtp.mailfrom=arm.com; dmarc=pass (p=none sp=none pct=100) action=none header.from=arm.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=armh.onmicrosoft.com; s=selector2-armh-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=utvY/WTHtUXD67TQnAFhtOTncHbJ8LguA8+ClM3LMjE=; b=qlg7EMsimrIhSEK+3eG3WjWkoWaHiQ1Ysmo5eR+kVTYOIccsi5t5hyExZlmKZPG09txwJuDgbo2dYrS1XNtjTa/cavgm1DfbvyXfztKT1PA4H3K/PWLdqoxcCuMoV2jdoshyXOTEsu0tToACKuIGu/vFilgrPKSNAzFZYNS+YVc= Received: from FR3P281CA0078.DEUP281.PROD.OUTLOOK.COM (2603:10a6:d10:1f::19) by DU0PR08MB7464.eurprd08.prod.outlook.com (2603:10a6:10:357::21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5924.9; Wed, 14 Dec 2022 16:40:39 +0000 Received: from VI1EUR03FT028.eop-EUR03.prod.protection.outlook.com (2603:10a6:d10:1f:cafe::63) by FR3P281CA0078.outlook.office365.com (2603:10a6:d10:1f::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5924.11 via Frontend Transport; Wed, 14 Dec 2022 16:40:39 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 40.67.248.234) smtp.mailfrom=arm.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=arm.com; Received-SPF: Pass (protection.outlook.com: domain of arm.com designates 40.67.248.234 as permitted sender) receiver=protection.outlook.com; client-ip=40.67.248.234; helo=nebula.arm.com; pr=C Received: from nebula.arm.com (40.67.248.234) by VI1EUR03FT028.mail.protection.outlook.com (100.127.144.83) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.5924.11 via Frontend Transport; Wed, 14 Dec 2022 16:40:39 +0000 Received: from AZ-NEU-EX04.Arm.com (10.251.24.32) by AZ-NEU-EX04.Arm.com (10.251.24.32) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.16; Wed, 14 Dec 2022 16:40:35 +0000 Received: from e124257 (10.34.105.24) by mail.arm.com (10.251.24.32) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.16 via Frontend Transport; Wed, 14 Dec 2022 16:40:35 +0000 From: Andrea Corallo To: Richard Earnshaw CC: Kyrylo Tkachov , Richard Earnshaw , nd , Andrea Corallo via Gcc-patches Subject: [PATCH 12/15 V4] arm: implement bti injection References: <30990d91-0c32-a3b0-7954-846bf1eddc8d@foss.arm.com> Date: Wed, 14 Dec 2022 17:40:33 +0100 In-Reply-To: <30990d91-0c32-a3b0-7954-846bf1eddc8d@foss.arm.com> (Richard Earnshaw's message of "Mon, 5 Dec 2022 17:02:25 +0000") Message-ID: User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.2.50 (gnu/linux) MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" X-EOPAttributedMessage: 1 X-MS-TrafficTypeDiagnostic: VI1EUR03FT028:EE_|DU0PR08MB7464:EE_|AM7EUR03FT008:EE_|AS8PR08MB7909:EE_ X-MS-Office365-Filtering-Correlation-Id: 9ec360de-52e4-42e4-3300-08daddf1f4f7 x-checkrecipientrouted: true NoDisclaimer: true X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam-Untrusted: BCL:0; X-Microsoft-Antispam-Message-Info-Original: UXc2dwGsiOOUZA6rC6TOAzPi8vsdhdow9yxXiTRR4TQTjuFP/EvNro6jD0DVHd7y/QXHPvcSDiNYXcMw1AtKqehDOfWpubCvdi/99DTIKGoITteDUvFpvnjmJylmLKw0SH1M0XqE4rJakOEPXaM+RZlIZ9+5FpAkuURi5r1vesGFeEHny0NVAP53hKSJba+0J4W4Cnms70xQsrCvlnXBCLC0vbvcq+4YL/PuU0lcdm+lobDFI7rcHlssUDQPWIldt1UvHFnRBRXaZZl1BP2jdDFFLY1KBzjaEjOwT3t8BkklSvV2Ng3RufspT9rV32aTX234RHODFc4SBXXBzVzF4h+UEZnn7SbSR684RGrce6TBlxxpXeawBeGZzDqL1ke4R/E6yNC1RCCDoF2Qt6s50ibrFfYMoUTa1TKkvKpka0eoFsaoU88LKOn7P7NWEDrJKpdBy3kAjRyA9y3591BOiPwEXj2PF0wZ7O+PrhQX3qzeR1zBZKsaCb1VFozakX0URKzAvDqRRBTVv7s3aCXUcxq1bowwD4GHXt2jZ2RdXXRn0KdhaDdswcQS+CD8LsesR7Kq3lGp45tTA7eD+LUzGgQTVb43yPbbUgAY/RCweoEdboCwWdTZsp1wR+/fUMjT2Y84MmvRGzhoHaMRBK4cQZH7sI5uQUlgHUCrrFCUrx0o0nRuVq7fRiaPAAFP8JMoeLiXuEVOIQuHJE+iReLsZA== X-Forefront-Antispam-Report-Untrusted: CIP:40.67.248.234;CTRY:IE;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:nebula.arm.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230022)(4636009)(376002)(396003)(136003)(39860400002)(346002)(451199015)(46966006)(36840700001)(336012)(2906002)(426003)(2616005)(82740400003)(478600001)(26005)(186003)(53546011)(36756003)(82310400005)(86362001)(81166007)(40480700001)(356005)(70586007)(83380400001)(47076005)(36860700001)(33964004)(41300700001)(8676002)(70206006)(4326008)(5660300002)(235185007)(6862004)(54906003)(8936002)(316002)(44832011)(36900700001);DIR:OUT;SFP:1101; X-MS-Exchange-Transport-CrossTenantHeadersStamped: DU0PR08MB7464 X-MS-Exchange-Transport-CrossTenantHeadersStripped: AM7EUR03FT008.eop-EUR03.prod.protection.outlook.com X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id-Prvs: af652de0-8f17-4d20-c6bc-08daddf1ef02 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: A7gTL3JDmnPZAKkkspxyM9FpRKDGiZut8owyae3xfNJNZ0bVlmb6/ORRQJgz5rftdk/kAIdcog66z0p+0JxUGU/chdO3/X0p4lQzJ5JCxqlVs0NtH300Stu3UKIAXRCZaSNshG2m60ECqazTq5Mpmy4oL30ITti0UFtQv9o00MyJF4ydonbEpBNO1DY2bUVkBBxz15QsPjo8y/rBR1khq5+tYGCLLlC1EyeR0oDuDu8sPgpkhDLSCwcw3V+akmmyaatl8P7i/oN7+1KdY+rNh2kIo4BRyES3c2PJmbQpsQcsb+w0AZeHJsB3hV3njrEB06Oj2ZNH6rEVbpvI9xa0d8MBHdoLNUozehLoKm5q87KqhbNcAk8aajytG2M6yazWIq/PG7mJgBXqSXBZhku3m16oIRyYJTBCRDECF7NxEWFDE6lxEbBNSimaNhNhdNTyZpg5Sua9vJHkUSASz38PZgEUd/pDj6hCH0T3TFUgKPiQ1CyjatJAvn+Ayu4tJqcU1Q1ihv7iHD/nXtoOGidRgaHuHehYdvIWcmV0oXlNrViKBLHLvlPXFf282B9RkcfZEPegdqNSzmrVM6xrKdJn4vLKFca4SLZxlk645JkxAyjHDq4nzCwpqUZayWoktDYKP19DLpD6xLZbAolNd6ODoV2Gcnu6KUYeC8EJzXrH0gdWCtttVhmawmDaMee6n9vx0eY/oBILxgWII7FGcV9nkQ== X-Forefront-Antispam-Report: CIP:63.35.35.123;CTRY:IE;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:64aa7808-outbound-1.mta.getcheckrecipient.com;PTR:ec2-63-35-35-123.eu-west-1.compute.amazonaws.com;CAT:NONE;SFS:(13230022)(4636009)(39860400002)(376002)(346002)(396003)(136003)(451199015)(36840700001)(40470700004)(46966006)(316002)(47076005)(2616005)(336012)(426003)(54906003)(5660300002)(2906002)(86362001)(8936002)(82740400003)(6862004)(40480700001)(36860700001)(82310400005)(40460700003)(36756003)(83380400001)(44832011)(235185007)(81166007)(8676002)(4326008)(186003)(478600001)(70206006)(41300700001)(70586007)(26005)(33964004)(53546011);DIR:OUT;SFP:1101; X-OriginatorOrg: arm.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Dec 2022 16:40:49.2263 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 9ec360de-52e4-42e4-3300-08daddf1f4f7 X-MS-Exchange-CrossTenant-Id: f34e5979-57d9-4aaa-ad4d-b122a662184d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=f34e5979-57d9-4aaa-ad4d-b122a662184d;Ip=[63.35.35.123];Helo=[64aa7808-outbound-1.mta.getcheckrecipient.com] X-MS-Exchange-CrossTenant-AuthSource: AM7EUR03FT008.eop-EUR03.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: AS8PR08MB7909 X-Spam-Status: No, score=-11.9 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,FORGED_SPF_HELO,GIT_PATCH_0,KAM_DMARC_NONE,KAM_SHORT,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_PASS,SPF_NONE,TXREP,UNPARSEABLE_RELAY autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: --=-=-= Content-Type: text/plain Hi Richard, thanks for reviewing. Richard Earnshaw writes: > On 28/10/2022 17:40, Andrea Corallo via Gcc-patches wrote: >> Hi all, >> please find attached the third iteration of this patch addresing >> review >> comments. >> Thanks >> Andrea >> > > @@ -23374,12 +23374,6 @@ output_probe_stack_range (rtx reg1, rtx reg2) > return ""; > } > > -static bool > -aarch_bti_enabled () > -{ > - return false; > -} > - > /* Generate the prologue instructions for entry into an ARM or Thumb-2 > function. */ > void > @@ -32992,6 +32986,61 @@ arm_current_function_pac_enabled_p (void) > && !crtl->is_leaf)); > } > > +/* Return TRUE if Branch Target Identification Mechanism is enabled. */ > +bool > +aarch_bti_enabled (void) > +{ > + return aarch_enable_bti == 1; > +} > > See comment in earlier patch about the location of this function > moving. Can aarch_enable_bti take values other than 0 and 1? Yes default is 2. [...] > + return GET_CODE (pat) == UNSPEC_VOLATILE && XINT (pat, 1) == > UNSPEC_BTI_NOP; > > I'm not sure where this crept in, but UNSPEC and UNSPEC_VOLATILE have > separate enums in the backend, so UNSPEC_BIT_NOP should really be > VUNSPEC_BTI_NOP and defined in the enum "unspecv". Done > +aarch_pac_insn_p (rtx x) > +{ > + if (!x || !INSN_P (x)) > + return false; > + > + rtx pat = PATTERN (x); > + > + if (GET_CODE (pat) == SET) > + { > + rtx tmp = XEXP (pat, 1); > + if (tmp > + && GET_CODE (tmp) == UNSPEC > + && (XINT (tmp, 1) == UNSPEC_PAC_NOP > + || XINT (tmp, 1) == UNSPEC_PACBTI_NOP)) > + return true; > + } > + > > This will also need updating (see review on earlier patch) because > PACBTI needs to be unspec_volatile, while PAC doesn't. Done > +/* The following two functions are for code compatibility with aarch64 > + code, this even if in arm we have only one bti instruction. */ > + > > I'd just write > /* Target specific mapping for aarch_gen_bti_c and > aarch_gen_bti_j. For Arm, both of these map to a simple BTI > instruction. */ Done > > @@ -162,6 +162,7 @@ (define_c_enum "unspec" [ > UNSPEC_PAC_NOP ; Represents PAC signing LR > UNSPEC_PACBTI_NOP ; Represents PAC signing LR + valid landing pad > UNSPEC_AUT_NOP ; Represents PAC verifying LR > + UNSPEC_BTI_NOP ; Represent BTI > ]) > > BTI is an unspec volatile, so this should be in the "vunspec" enum and > renamed accordingly (see above). Done. Please find attached the updated version of this patch. BR Andrea --=-=-= Content-Type: text/plain; charset="utf-8" Content-Disposition: attachment; filename="0001-PATCH-12-15-arm-implement-bti-injection.patch" >From 5823333b5e4e4fe089f6865cc3e0360afd1ff168 Mon Sep 17 00:00:00 2001 From: Andrea Corallo Date: Thu, 7 Apr 2022 11:51:56 +0200 Subject: [PATCH] [PATCH 12/15] arm: implement bti injection Hi all, this patch enables Branch Target Identification Armv8.1-M Mechanism [1]. This is achieved by using the bti pass made common with Aarch64. The pass iterates through the instructions and adds the necessary BTI instructions at the beginning of every function and at every landing pads targeted by indirect jumps. Best Regards Andrea [1] gcc/ChangeLog 2022-04-07 Andrea Corallo * config.gcc (arm*-*-*): Add 'aarch-bti-insert.o' object. * config/arm/arm-protos.h: Update. * config/arm/arm.cc (aarch_bti_enabled) Update. (aarch_bti_j_insn_p, aarch_pac_insn_p, aarch_gen_bti_c) (aarch_gen_bti_j): New functions. * config/arm/arm.md (bti_nop): New insn. * config/arm/t-arm (PASSES_EXTRA): Add 'arm-passes.def'. (aarch-bti-insert.o): New target. * config/arm/unspecs.md (VUNSPEC_BTI_NOP): New unspec. * config/arm/aarch-bti-insert.cc (rest_of_insert_bti): Verify arch compatibility. * config/arm/arm-passes.def: New file. gcc/testsuite/ChangeLog 2022-04-07 Andrea Corallo * gcc.target/arm/bti-1.c: New testcase. * gcc.target/arm/bti-2.c: Likewise. --- gcc/config.gcc | 2 +- gcc/config/arm/arm-passes.def | 21 ++++++++++ gcc/config/arm/arm-protos.h | 2 + gcc/config/arm/arm.cc | 53 ++++++++++++++++++++++++- gcc/config/arm/arm.md | 7 ++++ gcc/config/arm/t-arm | 10 +++++ gcc/config/arm/unspecs.md | 1 + gcc/testsuite/gcc.target/arm/bti-1.c | 12 ++++++ gcc/testsuite/gcc.target/arm/bti-2.c | 58 ++++++++++++++++++++++++++++ 9 files changed, 163 insertions(+), 3 deletions(-) create mode 100644 gcc/config/arm/arm-passes.def create mode 100644 gcc/testsuite/gcc.target/arm/bti-1.c create mode 100644 gcc/testsuite/gcc.target/arm/bti-2.c diff --git a/gcc/config.gcc b/gcc/config.gcc index 86abcd26185..f578b88dd49 100644 --- a/gcc/config.gcc +++ b/gcc/config.gcc @@ -351,7 +351,7 @@ arc*-*-*) ;; arm*-*-*) cpu_type=arm - extra_objs="arm-builtins.o arm-mve-builtins.o aarch-common.o" + extra_objs="arm-builtins.o arm-mve-builtins.o aarch-common.o aarch-bti-insert.o" extra_headers="mmintrin.h arm_neon.h arm_acle.h arm_fp16.h arm_cmse.h arm_bf16.h arm_mve_types.h arm_mve.h arm_cde.h" target_type_format_char='%' c_target_objs="arm-c.o" diff --git a/gcc/config/arm/arm-passes.def b/gcc/config/arm/arm-passes.def new file mode 100644 index 00000000000..71d6b563640 --- /dev/null +++ b/gcc/config/arm/arm-passes.def @@ -0,0 +1,21 @@ +/* Arm-specific passes declarations. + Copyright (C) 2022 Free Software Foundation, Inc. + Contributed by Arm Ltd. + + This file is part of GCC. + + GCC is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3, or (at your option) + any later version. + + GCC is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with GCC; see the file COPYING3. If not see + . */ + +INSERT_PASS_BEFORE (pass_shorten_branches, 1, pass_insert_bti); diff --git a/gcc/config/arm/arm-protos.h b/gcc/config/arm/arm-protos.h index d97a1c3bf56..61c2bb7b526 100644 --- a/gcc/config/arm/arm-protos.h +++ b/gcc/config/arm/arm-protos.h @@ -24,6 +24,8 @@ #include "sbitmap.h" +rtl_opt_pass *make_pass_insert_bti (gcc::context *ctxt); + extern enum unwind_info_type arm_except_unwind_info (struct gcc_options *); extern int use_return_insn (int, rtx); extern bool use_simple_return_p (void); diff --git a/gcc/config/arm/arm.cc b/gcc/config/arm/arm.cc index 4af809373e4..e6ee9f433aa 100644 --- a/gcc/config/arm/arm.cc +++ b/gcc/config/arm/arm.cc @@ -33109,12 +33109,61 @@ arm_current_function_pac_enabled_p (void) } /* Return TRUE if Branch Target Identification Mechanism is enabled. */ -static bool -aarch_bti_enabled () +bool +aarch_bti_enabled (void) +{ + return aarch_enable_bti == 1; +} + +/* Check if INSN is a BTI J insn. */ +bool +aarch_bti_j_insn_p (rtx_insn *insn) { + if (!insn || !INSN_P (insn)) + return false; + + rtx pat = PATTERN (insn); + return GET_CODE (pat) == UNSPEC_VOLATILE && XINT (pat, 1) == VUNSPEC_BTI_NOP; +} + +/* Check if X (or any sub-rtx of X) is a PACIASP/PACIBSP instruction. */ +bool +aarch_pac_insn_p (rtx x) +{ + if (!x || !INSN_P (x)) + return false; + + rtx pat = PATTERN (x); + + if (GET_CODE (pat) == SET) + { + rtx tmp = XEXP (pat, 1); + if (tmp + && ((GET_CODE (tmp) == UNSPEC + && XINT (tmp, 1) == UNSPEC_PAC_NOP) + || (GET_CODE (tmp) == UNSPEC_VOLATILE + && XINT (tmp, 1) == VUNSPEC_PACBTI_NOP))) + return true; + } + return false; } + /* Target specific mapping for aarch_gen_bti_c and aarch_gen_bti_j. + For Arm, both of these map to a simple BTI instruction. */ + +rtx +aarch_gen_bti_c (void) +{ + return gen_bti_nop (); +} + +rtx +aarch_gen_bti_j (void) +{ + return gen_bti_nop (); +} + /* Implement TARGET_SCHED_CAN_SPECULATE_INSN. Return true if INSN can be scheduled for speculative execution. Reject the long-running division and square-root instructions. */ diff --git a/gcc/config/arm/arm.md b/gcc/config/arm/arm.md index c7a20e94139..35c6be22eb1 100644 --- a/gcc/config/arm/arm.md +++ b/gcc/config/arm/arm.md @@ -13001,6 +13001,13 @@ (define_insn "aut_nop" "aut\t%|ip, %|lr, %|sp" [(set_attr "conds" "unconditional")]) +(define_insn "bti_nop" + [(unspec_volatile [(const_int 0)] VUNSPEC_BTI_NOP)] + "arm_arch8m_main" + "bti" + [(set_attr "conds" "unconditional") + (set_attr "type" "nop")]) + ;; Vector bits common to IWMMXT, Neon and MVE (include "vec-common.md") ;; Load the Intel Wireless Multimedia Extension patterns diff --git a/gcc/config/arm/t-arm b/gcc/config/arm/t-arm index 041cc6ec045..683342cb528 100644 --- a/gcc/config/arm/t-arm +++ b/gcc/config/arm/t-arm @@ -175,3 +175,13 @@ arm-d.o: $(srcdir)/config/arm/arm-d.cc arm-common.o: arm-cpu-cdata.h driver-arm.o: arm-native.h + +PASSES_EXTRA += $(srcdir)/config/arm/arm-passes.def + +aarch-bti-insert.o: $(srcdir)/config/arm/aarch-bti-insert.cc \ + $(CONFIG_H) $(SYSTEM_H) $(TM_H) $(REGS_H) insn-config.h $(RTL_BASE_H) \ + dominance.h cfg.h cfganal.h $(BASIC_BLOCK_H) $(INSN_ATTR_H) $(RECOG_H) \ + output.h hash-map.h $(DF_H) $(OBSTACK_H) $(TARGET_H) $(RTL_H) \ + $(CONTEXT_H) $(TREE_PASS_H) regrename.h + $(COMPILER) -c $(ALL_COMPILERFLAGS) $(ALL_CPPFLAGS) $(INCLUDES) \ + $(srcdir)/config/arm/aarch-bti-insert.cc diff --git a/gcc/config/arm/unspecs.md b/gcc/config/arm/unspecs.md index 370f0b12da1..93447a8ce9d 100644 --- a/gcc/config/arm/unspecs.md +++ b/gcc/config/arm/unspecs.md @@ -257,6 +257,7 @@ (define_c_enum "unspecv" [ ; instruction. VUNSPEC_PACBTI_NOP ; Represents PAC signing LR + valid landing pad VUNSPEC_AUT_NOP ; Represents PAC verifying LR + VUNSPEC_BTI_NOP ; Represent BTI ]) ;; Enumerators for NEON unspecs. diff --git a/gcc/testsuite/gcc.target/arm/bti-1.c b/gcc/testsuite/gcc.target/arm/bti-1.c new file mode 100644 index 00000000000..79dd8010d2d --- /dev/null +++ b/gcc/testsuite/gcc.target/arm/bti-1.c @@ -0,0 +1,12 @@ +/* Check that GCC does bti instruction. */ +/* { dg-do compile } */ +/* { dg-skip-if "avoid conflicting multilib options" { *-*-* } { "-marm" "-mcpu=*" } } */ +/* { dg-options "-march=armv8.1-m.main -mthumb -mfloat-abi=softfp -mbranch-protection=bti --save-temps" } */ + +int +main (void) +{ + return 0; +} + +/* { dg-final { scan-assembler "bti" } } */ diff --git a/gcc/testsuite/gcc.target/arm/bti-2.c b/gcc/testsuite/gcc.target/arm/bti-2.c new file mode 100644 index 00000000000..33910563849 --- /dev/null +++ b/gcc/testsuite/gcc.target/arm/bti-2.c @@ -0,0 +1,58 @@ +/* { dg-do compile } */ +/* -Os to create jump table. */ +/* { dg-options "-Os" } */ +/* { dg-skip-if "avoid conflicting multilib options" { *-*-* } { "-marm" "-mcpu=*" } } */ +/* { dg-options "-march=armv8.1-m.main -mthumb -mfloat-abi=softfp -mbranch-protection=bti --save-temps" } */ + +extern int f1 (void); +extern int f2 (void); +extern int f3 (void); +extern int f4 (void); +extern int f5 (void); +extern int f6 (void); +extern int f7 (void); +extern int f8 (void); +extern int f9 (void); +extern int f10 (void); + +int (*ptr) (void); + +int +f_jump_table (int y, int n) +{ + int i; + for (i = 0; i < n ;i ++) + { + switch (y) + { + case 0 : ptr = f1; break; + case 1 : ptr = f2; break; + case 2 : ptr = f3; break; + case 3 : ptr = f4; break; + case 4 : ptr = f5; break; + case 5 : ptr = f6; break; + case 6 : ptr = f7; break; + case 7 : ptr = f8; break; + case 8 : ptr = f9; break; + case 9 : ptr = f10; break; + default: break; + } + y += ptr (); + } + return (y == 0)? y+1:4; +} + +int +f_label_address () +{ + static void * addr = &&lab1; + goto *addr; +lab1: + addr = &&lab2; + return 1; +lab2: + addr = &&lab1; + return 2; +} + +/* { dg-final { scan-assembler-times "bti" 15 } } */ -- 2.25.1 --=-=-=--