From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from EUR04-HE1-obe.outbound.protection.outlook.com (mail-eopbgr70042.outbound.protection.outlook.com [40.107.7.42]) by sourceware.org (Postfix) with ESMTPS id 822623858427 for ; Thu, 29 Sep 2022 15:46:11 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 822623858427 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=arm.com ARC-Seal: i=2; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=pass; b=MlqLCvfASUJ8tbW4y/xTR+uV0uo77FwsvCtk9iIMAOd7Ad+rG0eBRsE81X7vsH/bJ9/7l+tm1yEWEG2Y0BcTzpjWPL7F6qKR+fogYOi5qzcZqyBdaODjJOl8nwyDD41/yOY2R4/eJ1cM5lm+xhpRNLK1DEt9d2wbz3Kz8YzGWgogy0CRBCvOaMIOrDn1xF0Somr91k5hhKFUCzaxBU2TKHNTWbVr4pUoKLZAbLgHr5UI3t+WWCJJ/8WxMaoEidFkx4wVPI9SLrXkjphUbDzE8ks20rK953SDPnBULEESLl89alQR/NB6KfhQYnpxNLe3JhhPRRAFr40CeZHSCpQhIw== ARC-Message-Signature: i=2; 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=2RALsbsb5U1ZP/PGGHAkUQFeZQ5OE3VQx8Hz0bES/eU=; b=h0pXKL4HRLy4x7LlQocInCWCewr5eTrXxCMLY6gVF7/YY2Wsj6CyFH+Z0/Is/QN1Qc+Lb5HycEtQ3SBGB9536cAPkKSVh2CLkh8FIzdl58EAXOju0CkkKoaeJ7s3Iu8osT5sVTeDO91rt5e4qTksfFygM8P2j7YHL/UQ1EM1aCB+PvNGs+ZBmIdKj97/bF5vHtacX+MeKb+j2QC3yDfRaxSl3bxdwY/x9VwIs48b+0LGm9fVAG6fUypLAAL4bifF0nwfv30RGbKcisgc9qQfren6NiIiJ6ic1XNt80f2bIAFk3GbqU5GTsJ3p4Cy/nVkpYA1iVN3H8qGrDucyQyRAQ== ARC-Authentication-Results: i=2; mx.microsoft.com 1; spf=pass (sender ip is 63.35.35.123) smtp.rcpttodomain=gcc.gnu.org smtp.mailfrom=arm.com; dmarc=pass (p=none sp=none pct=100) action=none header.from=arm.com; dkim=pass (signature was verified) header.d=armh.onmicrosoft.com; arc=pass (0 oda=1 ltdi=1 spf=[1,1,smtp.mailfrom=arm.com] dmarc=[1,1,header.from=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=2RALsbsb5U1ZP/PGGHAkUQFeZQ5OE3VQx8Hz0bES/eU=; b=+DTRcjEoGLFo75Tyqg8zITYbZ2l3jJ2kmCZDSvl8dBaxsqgNQCUDTL0HRJ+/fQY/829gTn4Coj2ax/6CSzQsJcJI6EfGVz9E5StG72tD0KDVVmiQysfs3MKohFpv+/f8fVDo+n/x5JXrnMk47v5GV/3Jjja65i7ARz3u2/6AnME= Received: from AS9PR06CA0551.eurprd06.prod.outlook.com (2603:10a6:20b:485::12) by AM7PR08MB5397.eurprd08.prod.outlook.com (2603:10a6:20b:dd::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5676.20; Thu, 29 Sep 2022 15:46:08 +0000 Received: from VE1EUR03FT057.eop-EUR03.prod.protection.outlook.com (2603:10a6:20b:485:cafe::9e) by AS9PR06CA0551.outlook.office365.com (2603:10a6:20b:485::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5676.20 via Frontend Transport; Thu, 29 Sep 2022 15:46:08 +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 VE1EUR03FT057.mail.protection.outlook.com (10.152.19.123) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5676.17 via Frontend Transport; Thu, 29 Sep 2022 15:46:08 +0000 Received: ("Tessian outbound 7e4a920b87c0:v128"); Thu, 29 Sep 2022 15:46:08 +0000 X-CheckRecipientChecked: true X-CR-MTA-CID: 1f1e0c00dfbbb8ab X-CR-MTA-TID: 64aa7808 Received: from 9a7627e8f975.1 by 64aa7808-outbound-1.mta.getcheckrecipient.com id 702818BF-3C1F-4DD3-9693-C2B086DD420D.1; Thu, 29 Sep 2022 15:46:00 +0000 Received: from EUR05-VI1-obe.outbound.protection.outlook.com by 64aa7808-outbound-1.mta.getcheckrecipient.com with ESMTPS id 9a7627e8f975.1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384); Thu, 29 Sep 2022 15:46:00 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=ZaSV6pIu9fBoAWWswNe125bsEyJfmNWtebBKhjj1IWXRu9QRXBpc9bkDB2c24TjIM64P/2q6UvbAmizJH78lGQ55NVc+6eVKk4I1bqUy6SyJRYHmf78IhIAIzfdTLQyrHD/F4Z7PGb5aE7feTqwQsZ9J1RF+NbChjk1Rs9TdFlT1s8IxjyTwh2AGwH+6ycbxed6hGYpJPRRDO/ArEo/L7FXRzEM5lMiOmTmy1rqpEQB/3oj4dYVHEoRyWmuBR4K5BvZ8tHa+/vfil5kHWbk+4CgU6aU+XNm/82T5+lk3tKsvB/nzrEIOPsDejmtK/sP3ieYrsLDPMgWJu2+mI8R+YA== 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=2RALsbsb5U1ZP/PGGHAkUQFeZQ5OE3VQx8Hz0bES/eU=; b=axHbQ+RPAM/Z5mDbmQk8U51v8lBSZ3wWPIvjFKqYIXjLLbF5iLwXhgPfsV8XeCpzzO6EPvBXBp5me1ik5lnhXh5SYOLd7SIDxIdMoacdE4W2xYQdlur8S4YQFTTB9SdC25ld9+4lG0FIM8mQRS2gxPKN+pErR2jT/ZREhnxN1KEV/RfN2F9rTOKTYK9Cz2G2N+/8Ji6LSNMey3dIMcU9OLDWS3twotTlyO02ETrvneJvKaZUUpsBZChgs0ZrELJYgdIDiNq19L9axuG+Wdq20fKLm+r6uUK6zuaywzGDoOhWUBCqjmF5EBiCfbGyylPs7V/AFwdQB6Fc39r0lEo4rw== 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=2RALsbsb5U1ZP/PGGHAkUQFeZQ5OE3VQx8Hz0bES/eU=; b=+DTRcjEoGLFo75Tyqg8zITYbZ2l3jJ2kmCZDSvl8dBaxsqgNQCUDTL0HRJ+/fQY/829gTn4Coj2ax/6CSzQsJcJI6EfGVz9E5StG72tD0KDVVmiQysfs3MKohFpv+/f8fVDo+n/x5JXrnMk47v5GV/3Jjja65i7ARz3u2/6AnME= Received: from DUZPR01CA0079.eurprd01.prod.exchangelabs.com (2603:10a6:10:46a::16) by AS8PR08MB8995.eurprd08.prod.outlook.com (2603:10a6:20b:5b2::21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5676.15; Thu, 29 Sep 2022 15:45:58 +0000 Received: from DBAEUR03FT032.eop-EUR03.prod.protection.outlook.com (2603:10a6:10:46a:cafe::42) by DUZPR01CA0079.outlook.office365.com (2603:10a6:10:46a::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5676.20 via Frontend Transport; Thu, 29 Sep 2022 15:45:58 +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 DBAEUR03FT032.mail.protection.outlook.com (100.127.142.185) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.5676.17 via Frontend Transport; Thu, 29 Sep 2022 15:45:58 +0000 Received: from AZ-NEU-EX02.Emea.Arm.com (10.251.26.5) 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.9; Thu, 29 Sep 2022 15:45:40 +0000 Received: from AZ-NEU-EX04.Arm.com (10.251.24.32) by AZ-NEU-EX02.Emea.Arm.com (10.251.26.5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.9; Thu, 29 Sep 2022 15:45:39 +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.9 via Frontend Transport; Thu, 29 Sep 2022 15:45:38 +0000 From: Andrea Corallo To: Kyrylo Tkachov CC: Andrea Corallo via Gcc-patches , "Richard Earnshaw" , nd Subject: [PATCH 12/15 V2] arm: implement bti injection References: Date: Thu, 29 Sep 2022 17:45:38 +0200 In-Reply-To: (Kyrylo Tkachov's message of "Tue, 27 Sep 2022 09:18:54 +0000") Message-ID: User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.1.50 (gnu/linux) MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" X-EOPAttributedMessage: 1 X-MS-TrafficTypeDiagnostic: DBAEUR03FT032:EE_|AS8PR08MB8995:EE_|VE1EUR03FT057:EE_|AM7PR08MB5397:EE_ X-MS-Office365-Filtering-Correlation-Id: 5a68a3b6-4fba-4fc6-27dc-08daa231ba2d 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: +0dH5yRiT+ht2J5doMK5dnVPEq47b+gjTsW3PM4etzwZgOaP/66Jtq2c1f81kKBmOpTw/aj36pxCIxmwCmUn+FFgLfnggVe0L9pEyL+tGhPKyHXISJx4uirQT+MKOVPFM1NZ2qab4hYR9Whkcqre/TI8XsiOb+jyqzBeNPPKe0Xcb9EfStkagPJcWsIi320J5mYhSkDCSPAwdiSCuf8bQ9IeeXiooV0RuRgYUaYLbhgSnvB4FDnuciCn0ZLuYTjzGRSesbegjn+ctsU/sPIUfexUBqmI0jXfctpPxK7oZoow7UDR5JU7i1UiEoOWQJiJbaOiio0OKSvZKZU/G59uaW1g86PExyy3VvGZQtiNAXUkhYRznIRWBnL3sqWs2xs5hhLE+ccqggwXsKBRuBlDjnbAO4RiFlmlsxlRUoWdbLXiYx+PNlbCwtPEFYQaXWPP33IuhShumJUuv//pY7aTiNprl44KIGyZbxzoHHUdTPJqjhXk+mN0UHbaqyKm9E+O8pMNwsFRkFsq+EJcqFk6KFjEJRko+JjTlNgB2nN+WR1eB9+DAh+PbopaG7bGwxNvK3Y0M7mob+GBo9vUxGvvxKcZt6Ca+m4b+cpb/A5hXt536wv3UGr7I3/QFUzH3c8WNdy3oFeZ7xi21zcWAayHvtd6ZcAvN9A5k5b5kbm94gzxXZKjHkPDnk10qdeFRBqxMuLBGwCGFIx5PfnuvefS6MkudRkZznOTxkTzXE5RGgLBJqwJFRsX1lAPeM1VkgV2MxzEDccPp/1NsN1IlnLFW1R3tEcPEFHHwLUmtpQZp1McJT2KXI7WlDzzPKbZvQd6 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)(346002)(376002)(39860400002)(136003)(396003)(451199015)(36840700001)(40470700004)(46966006)(6636002)(36860700001)(8936002)(26005)(82310400005)(316002)(41300700001)(54906003)(36756003)(83380400001)(86362001)(47076005)(6862004)(44832011)(40480700001)(5660300002)(82740400003)(235185007)(478600001)(33964004)(2906002)(4326008)(70206006)(70586007)(37006003)(81166007)(40460700003)(356005)(2616005)(186003)(336012)(426003)(8676002)(36900700001);DIR:OUT;SFP:1101; X-MS-Exchange-Transport-CrossTenantHeadersStamped: AS8PR08MB8995 X-MS-Exchange-Transport-CrossTenantHeadersStripped: VE1EUR03FT057.eop-EUR03.prod.protection.outlook.com X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id-Prvs: 4e508beb-a783-4534-344f-08daa231b400 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: BPshmVf+BPvGxfIChBK5JvCo67yFLgBkMIwg31vuaprRcq2dJL2Ye5MViHxM3GUp+ZTHyGIlg6ytdGT3zW9Y2+u8bQzEOIIFwt81ASAOL6L0nqrJorN7pbDifP1BsCTsZxuRC8xk9bsGE/5lsTumh2abjnP9V1YTGYybE7LNbM0UuFn5mc6UQs4nQgozivmm5BH6PHB1aNy6KHvSWDNOPX8aWfj+5GXmWwYifi2s83p28XGAdbW1EgOXoV5bp17De4PqHnO8wcHmPGWtLFuqgMP9Ahdt8UUNjugJAcYuoVoyswjmqxSjUdxg1CXc3M9LSKHdF+BjyCWYhOgmKW69GXySxDnHejT7gwOb+PSeJILIYTADARGbu9X36BDZgsPOvGpczlBT3em3bJ/BuNS9WCvGtLRMlzQY5ZkVZL6dLImjJxv/mxilcTqdioadSNJE4NWUtoiB0HcKs7a6yOHM4ztr8kTIgJp61YnB8O+1Fa6kr9gDtHyAwH0Chj43o+zfRuN6QKFMrwgFpTPrhbQJvu0oCt4J1BVOD9qWoeYvl88C12vCPJjAnpliBglKgfdoDmdpzfYP1McFXIjKBHz+G1R5DsSDSN7VB22kclqzLAA5vaP/x9xBZrLXxJE6lXsa4kbroAX/6fFs6QGyzDJlUlt4Dg9f9Ki8rraEJY3dfYa3RPL7yFRKj8nJgJBkCi1i2Cl0cZdisbBDsdWfEaeK+uIUhJoWztoRALM/WqTNe3K9iXrUhKeW9ongdEC9rfAMY3ektw4SrYlQI+u7+jP1Sw== 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)(136003)(39850400004)(346002)(376002)(396003)(451199015)(40470700004)(46966006)(36840700001)(41300700001)(2906002)(5660300002)(44832011)(235185007)(8676002)(6862004)(8936002)(4326008)(70206006)(70586007)(478600001)(33964004)(47076005)(86362001)(82740400003)(82310400005)(2616005)(186003)(336012)(26005)(6636002)(54906003)(37006003)(316002)(40460700003)(83380400001)(36756003)(81166007)(40480700001)(426003)(36860700001);DIR:OUT;SFP:1101; X-OriginatorOrg: arm.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 29 Sep 2022 15:46:08.5562 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 5a68a3b6-4fba-4fc6-27dc-08daa231ba2d 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: VE1EUR03FT057.eop-EUR03.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM7PR08MB5397 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 Kyrylo Tkachov writes: > Hi Andrea, [...] > diff --git a/gcc/config/arm/aarch-bti-insert.cc b/gcc/config/arm/aarch-bti-insert.cc > index 2d1d2e334a9..8f045c247bf 100644 > --- a/gcc/config/arm/aarch-bti-insert.cc > +++ b/gcc/config/arm/aarch-bti-insert.cc > @@ -41,6 +41,7 @@ > #include "cfgrtl.h" > #include "tree-pass.h" > #include "cgraph.h" > +#include "diagnostic-core.h" > > This change doesn't seem to match what's in the ChangeLog and doesn't make sense to me. Change removed thanks. > @@ -32985,6 +32979,58 @@ 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; > +} > + > +/* 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) == UNSPEC_BTI_NOP; > +} > + > +/* Check if X (or any sub-rtx of X) is a PACIASP/PACIBSP instruction. */ > > The arm instructions are not PACIASP/PACIBSP. > This comment should be rewritten. This hunk belongs to aarch64.cc so it's aarch64 specific. > +bool > +aarch_pac_insn_p (rtx x) > +{ > > .......... > > +rtx > +aarch_gen_bti_c (void) > +{ > + return gen_bti_nop (); > +} > + > +rtx > +aarch_gen_bti_j (void) > +{ > + return gen_bti_nop (); > +} > + > > A reader may be confused for why we have a bti_c and bti_j function that have identical functionality. > Please add function comments explaining the situation. Done > diff --git a/gcc/config/arm/arm.md b/gcc/config/arm/arm.md > index 92269a7819a..90c8c1d66f5 100644 > --- a/gcc/config/arm/arm.md > +++ b/gcc/config/arm/arm.md > @@ -12913,6 +12913,13 @@ > "aut\t%|ip, %|lr, %|sp" > [(set_attr "length" "4")]) > > +(define_insn "bti_nop" > + [(unspec_volatile [(const_int 0)] UNSPEC_BTI_NOP)] > + "arm_arch7 && arm_arch_cmse" > > That seems like a copy-paste mistake. CMSE has nothing to do with this functionality? This is because we don't have arm_arch8m_main, but this is equivalent to arm_arch7 && arm_arch_cmse. IIUC it wasn't added becasue armv8-m is basically just armv7-m + cmse. Any other preferred way to express this? > + "bti" > + [(set_attr "length" "4") > > The length of instructions in the arm backend is 4 by default, this set_attr can be omitted > > + (set_attr "type" "mov_reg")]) > + > Probably better to use the "nop" attribute here? Done Thanks for reviewing, please find attached the updated version. Andrea --=-=-= Content-Type: text/plain; charset="utf-8" Content-Disposition: attachment; filename="0001-PATCH-12-15-arm-implement-bti-injection.patch" >From 42f81b763c3a347f3452cd6ead056748d2830135 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, 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 (UNSPEC_BTI_NOP): New unspec. * config/arm/aarch-bti-insert.cc (rest_of_insert_bti): Update to 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 | 61 +++++++++++++++++++++++++--- gcc/config/arm/arm.md | 6 +++ 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, 166 insertions(+), 7 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 2021bdf9d2f..004e1dfa8d8 100644 --- a/gcc/config.gcc +++ b/gcc/config.gcc @@ -353,7 +353,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 84764bf27ce..6befb6c4445 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 0ba9c0b9960..a37fd20c090 100644 --- a/gcc/config/arm/arm.cc +++ b/gcc/config/arm/arm.cc @@ -23368,12 +23368,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 @@ -32986,6 +32980,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; +} + +/* 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) == UNSPEC_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 + || XINT (tmp, 1) == UNSPEC_PACBTI_NOP)) + return true; + } + + return false; +} + +/* The following two functions are for code compatibility with aarch64 + code, this even if in arm we have only one 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 92269a7819a..70a3d0a5013 100644 --- a/gcc/config/arm/arm.md +++ b/gcc/config/arm/arm.md @@ -12913,6 +12913,12 @@ (define_insn "aut_nop" "aut\t%|ip, %|lr, %|sp" [(set_attr "length" "4")]) +(define_insn "bti_nop" + [(unspec_volatile [(const_int 0)] UNSPEC_BTI_NOP)] + "arm_arch7 && arm_arch_cmse" + "bti" + [(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 dbe243a03f6..78e723a4b3c 100644 --- a/gcc/config/arm/unspecs.md +++ b/gcc/config/arm/unspecs.md @@ -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 ]) 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..230ebb5a1da --- /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 -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..35aef6992e2 --- /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 -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 --=-=-=--