From patchwork Fri Oct 8 12:17:35 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tejas Belagod X-Patchwork-Id: 46002 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id E95403857C5D for ; Fri, 8 Oct 2021 12:19:33 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E95403857C5D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1633695574; bh=V/ckW+Ppz1C8A+WjXGa9n+fyWnWVkPV4sPFFK86ye5A=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=AbiAP4CRSoGnBPjsPBTM9k8F+LgiWuxAhLVkte6N2ftZ5W9QYdahLwbEqfoMwb/ZR jw+o47DXhZN8BvAdDSRkrF3AFsn7PZEpgl49GgIEbLr6gOUEpjdcKNjU5TF6OTY26v K5yU8Yapuls08yCdQkUzX4o8rGApDSqZnOyMHa4I= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from EUR05-DB8-obe.outbound.protection.outlook.com (mail-db8eur05on2047.outbound.protection.outlook.com [40.107.20.47]) by sourceware.org (Postfix) with ESMTPS id E91AE3857809 for ; Fri, 8 Oct 2021 12:17:45 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org E91AE3857809 Received: from AM6P191CA0077.EURP191.PROD.OUTLOOK.COM (2603:10a6:209:8a::18) by HE1PR0801MB1945.eurprd08.prod.outlook.com (2603:10a6:3:4f::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4566.19; Fri, 8 Oct 2021 12:17:43 +0000 Received: from AM5EUR03FT051.eop-EUR03.prod.protection.outlook.com (2603:10a6:209:8a:cafe::1) by AM6P191CA0077.outlook.office365.com (2603:10a6:209:8a::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4587.18 via Frontend Transport; Fri, 8 Oct 2021 12:17:43 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 63.35.35.123) smtp.mailfrom=arm.com; gcc.gnu.org; dkim=pass (signature was verified) header.d=armh.onmicrosoft.com;gcc.gnu.org; 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; Received: from 64aa7808-outbound-1.mta.getcheckrecipient.com (63.35.35.123) by AM5EUR03FT051.mail.protection.outlook.com (10.152.16.246) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4587.18 via Frontend Transport; Fri, 8 Oct 2021 12:17:43 +0000 Received: ("Tessian outbound 8e26f7114b75:v103"); Fri, 08 Oct 2021 12:17:43 +0000 X-CR-MTA-TID: 64aa7808 Received: from 35b3b59e696a.1 by 64aa7808-outbound-1.mta.getcheckrecipient.com id DF5A6BE6-2B77-4E90-82B8-BBFD2CB3A8D3.1; Fri, 08 Oct 2021 12:17:36 +0000 Received: from EUR05-DB8-obe.outbound.protection.outlook.com by 64aa7808-outbound-1.mta.getcheckrecipient.com with ESMTPS id 35b3b59e696a.1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384); Fri, 08 Oct 2021 12:17:36 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Iyh0Lklhp3zUlU9h8PpsXZGjKCOMmLTpUs6V7Q2lK+MapIHEbJYMEgLOX5c8VxvAp+zGsJgnD8I9SFl/w298t9QcapnsTSlhncS14ShdXDLRPGNBKKBXQUBT5XvVNtARFAqwpj18B9UhAet4EWvV3u+r5X3googM6ssFIWaLwqm94r/Gwsd6Trvstu/gYDlBT/FPoSIqW53cc8uLlnReOmHip/Dq+jPY3IvKVgpKIPM2M9jr8Z3IkuLO+hcG3be7Y/+Y7R4Kj7+Pw/PXlMI/ohyR7u4U2afkt8dvnNLe9AdSpAhY7QA5VQK1k4A+Jahq1sH6+rYfbrSqIcrhLstvrg== 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=V/ckW+Ppz1C8A+WjXGa9n+fyWnWVkPV4sPFFK86ye5A=; b=TdvfhQ2WtbRmqdP7VGG40gpQPDXus+YpZEjMwU3sU4j2lLrJwMhre4v7DE1lJGvwG3KIv63wk05RUxFAVOMTIRMTtpt+o3ktT2M1uBYGaS8pP/OCDXdDXfJ1NIuLUezeVdkMCg3ChB4iWzt7772Hx/wphk7iJhWbP0LpO9nZElg2N0SuAYfYqWwu5cg7vgIdQYLvN3s6KYrN/8rlErAj/YK0injZK8Aq0EPa+LyGq3UDbczvT9d8Y/959Vbit193sb0JxpASp2on1qPZfiX4TA0ujivRuKVnAORpnDSlIWPalZeAJKiACLA40TjyGG4rLv2n7mPcBtNV3TB+KthvWw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=arm.com; dmarc=pass action=none header.from=arm.com; dkim=pass header.d=arm.com; arc=none Received: from AM5PR0801MB1844.eurprd08.prod.outlook.com (2603:10a6:203:39::22) by AM5PR0801MB1906.eurprd08.prod.outlook.com (2603:10a6:203:48::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4566.19; Fri, 8 Oct 2021 12:17:35 +0000 Received: from AM5PR0801MB1844.eurprd08.prod.outlook.com ([fe80::4cab:e095:5105:b6ec]) by AM5PR0801MB1844.eurprd08.prod.outlook.com ([fe80::4cab:e095:5105:b6ec%9]) with mapi id 15.20.4566.022; Fri, 8 Oct 2021 12:17:35 +0000 To: "gcc-patches@gcc.gnu.org" Subject: [Patch 2/7, Arm, GCC] Add option -mbranch-protection. Thread-Topic: [Patch 2/7, Arm, GCC] Add option -mbranch-protection. Thread-Index: Ade8OvtjhNs8zoVAQ+mvKjXXvoc1kA== Date: Fri, 8 Oct 2021 12:17:35 +0000 Message-ID: Accept-Language: en-GB, en-US Content-Language: en-US X-MS-Has-Attach: yes X-MS-TNEF-Correlator: x-ts-tracking-id: E193116D0AAF87499928C0EFC88D7779.0 x-checkrecipientchecked: true Authentication-Results-Original: gcc.gnu.org; dkim=none (message not signed) header.d=none;gcc.gnu.org; dmarc=none action=none header.from=arm.com; x-ms-publictraffictype: Email X-MS-Office365-Filtering-Correlation-Id: d3733924-2e4c-46ab-97d4-08d98a55a164 x-ms-traffictypediagnostic: AM5PR0801MB1906:|HE1PR0801MB1945: X-Microsoft-Antispam-PRVS: x-checkrecipientrouted: true nodisclaimer: true x-ms-oob-tlc-oobclassifiers: OLM:281;OLM:281; X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam-Untrusted: BCL:0; X-Microsoft-Antispam-Message-Info-Original: xzpzv/kagm1voOyrmPplTnsRGBAHwpCAe/tx+A+93jQqlOrJ/6Mx+tWIx0pNsBXO1BD6l7XShaLlYfRusFEsPPvpmHgJwb9nFdRYjvVkcQHAJW6mZ3+2Rlra+XO+KPFdy83fYKnL1mJdzCaHugL6WM4DdMA0otWw1B7we+sQAzR45KmN8/18dYz1EqRNOa7yb2WLNdcuXm1TjjYvhZ7IfIdAj6MjsX1GTjwUW2XzLAZObj7ub1oWKfz5wRcrz2+cTunc0UrfLdpAtaiHfhFY+wmeUTCn04NnQQNH05m5LcthLtHz/FigmkleEkHYNniKINlBY7JQcW8miVKLFBbVu5Kplo3Rd960DWUhtUMiXUOaYDP35AV5W4uxqZqgmjReRV8XWH5eHHmkQVHzuFPo8f6CFCeaMOeTeqLd5g9oisfVTcUej5AjPcItqmELIpaRcVAU76aMsDQNkAGNLESbil8aOpDla+lQnTKyf7pA7i7ut1LCL8rNsZ5nHJNzW44vM6+J4pDbz/7VFq4mRSiq3hHMridIWDKW9t5CxK5TUTUBL26syg1vkOV6L/oxCamCLk3snv/avIsdGmVvC49jUKBqhwFViPsjF05kXv1uow5e0RxtUk6oqDJHIv4kheBCpsajApoPt9mV9KWWwGZPQ67TYycMoEuMrt3CIVni1/664Pa5MH2o3AsAS84r3+oOj6wmnEHwUr0t7RC3Twy8vA== X-Forefront-Antispam-Report-Untrusted: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:AM5PR0801MB1844.eurprd08.prod.outlook.com; PTR:; CAT:NONE; SFS:(4636009)(366004)(6916009)(52536014)(33656002)(71200400001)(8676002)(122000001)(4744005)(26005)(38100700002)(5660300002)(9686003)(38070700005)(86362001)(55016002)(316002)(508600001)(66446008)(64756008)(66476007)(7696005)(8936002)(2906002)(186003)(66556008)(6506007)(99936003)(76116006)(66946007)(83380400001); DIR:OUT; SFP:1101; x-ms-exchange-transport-forked: True MIME-Version: 1.0 X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM5PR0801MB1906 Original-Authentication-Results: gcc.gnu.org; dkim=none (message not signed) header.d=none;gcc.gnu.org; dmarc=none action=none header.from=arm.com; X-EOPAttributedMessage: 0 X-MS-Exchange-Transport-CrossTenantHeadersStripped: AM5EUR03FT051.eop-EUR03.prod.protection.outlook.com X-MS-Office365-Filtering-Correlation-Id-Prvs: fd40fea2-2056-4d85-859e-08d98a559cdb X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: xe9N58u3bNJeC2XjkjFbi7XmcjxtrJVn7qpn+VMsndHRoBffhXAM0zT3qz1LMMx5DFPhf7i2S9t8Fl+bn74MEmbLDRQIgpfJLk2G5y2B6rKUSvRhXzNVHNhzFrka97h2Y5siMbuJ2eUhsaT3Jiz+FkTunx+NKfjzEq7gRh2e0qNwsAQau62Df9auNWcAvZCkrhl5cHUV0PVEUrTUzu6IDy3jBL4vJYwpDsOIe5fnEdJ1fX1dPC3zG/7KDzNwmlqyManYnj1sj6lnxLDKVg5+/K9tG8cgwoHgcxquTp4QZ/K+Ln5LvYcr7N18Xmk+HqRVNx4F6x518IqSSOpiT83CnjSFqyb5mg8yAlOk9d6SbNsmRXCQyNYeJinJONszsgJb9dp4LWmB82yaQvj0YhylGGfhY+lRiuJ9zZC/jtSWz8B12JoGfcP+0POXudJi/samxC4z9OTexaKO5CXqTxgtFZVmVSBvzWD9ohKjTiISEnRO64vZAUKmv/YWmpsIDhkYYzvnuTHNSV2eXQeLdTb91h1oKnb0VCzlq8AQUo/Tf6j2IjGMguSyyLnnnW04RJN7qc/0L6HcjGRnc/b6mpCotNi250vKGrCiXLhkNFAfHs8OgPpeUPBNKFSDFt23L3/YOLJxhZWrKAwfRQxu3YoSBEYFZIdAVnkeCAPDFQya8cnOxzAYYdzTNdmZ5jETMk98DHb6UkNuGEV56CUPWJIkZw== 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:(4636009)(46966006)(36840700001)(8676002)(86362001)(21480400003)(336012)(83380400001)(6506007)(356005)(99936003)(235185007)(8936002)(81166007)(316002)(70586007)(52536014)(2906002)(36860700001)(5660300002)(7696005)(186003)(6916009)(70206006)(26005)(82310400003)(508600001)(9686003)(47076005)(33656002)(55016002); DIR:OUT; SFP:1101; X-OriginatorOrg: arm.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 08 Oct 2021 12:17:43.3523 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: d3733924-2e4c-46ab-97d4-08d98a55a164 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: AM5EUR03FT051.eop-EUR03.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: HE1PR0801MB1945 X-Spam-Status: No, score=-13.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, GIT_PATCH_0, KAM_LOTSOFHASH, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_PASS, SPF_PASS, TXREP, UNPARSEABLE_RELAY autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Tejas Belagod via Gcc-patches From: Tejas Belagod Reply-To: Tejas Belagod Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" Hi, Add -mbranch-protection option and its associated parsing routines. This option enables the code-generation of pointer signing and authentication instructions in function prologues and epilogues. Tested on arm-none-eabi. OK for trunk? 2021-10-04 Tejas Belagod gcc/ChangeLog: * common/config/arm/arm-common.c (arm_print_hit_for_pacbti_option): New. (arm_progress_next_token): New. (arm_parse_pac_ret_clause): New routine for parsing the pac-ret clause for -mbranch-protection. (arm_parse_pacbti_option): New routine to parse all the options to -mbranch-protection. * config/arm/arm-protos.h (arm_parse_pacbti_option): Export. * config/arm/arm.c (arm_configure)build_target): Handle option to -mbranch-protection. * config/arm/arm.opt (mbranch-protection). New. (arm_enable_pacbti): New. diff --git a/gcc/common/config/arm/arm-common.c b/gcc/common/config/arm/arm-common.c index de898a74165db4d7250aa0097dfab682beb0f99c..188feebb15b52f389d5d0b3ec322be3017efd5a0 100644 --- a/gcc/common/config/arm/arm-common.c +++ b/gcc/common/config/arm/arm-common.c @@ -475,6 +475,156 @@ arm_parse_arch_option_name (const arch_option *list, const char *optname, return NULL; } +static void +arm_print_hint_for_pacbti_option () +{ + const char *s = "pac-ret[+leaf][+b-key][+bti]" + " | bti[+pac-ret[+leaf][+b-key]]"; + inform (input_location, "valid arguments are: %s", s); +} + +/* Progress *E to end of next token delimited by DELIMITER. + Cache old *E in *OE. */ +static void +arm_progress_next_token (const char **oe, const char **e, + size_t *l, const char delimiter) +{ + *oe = *e + 1; + *e = strchr (*oe, delimiter); + *l = *e ? *e - *oe : strlen (*oe); +} + +/* Parse options to -mbranch-protection. */ +static const char* +arm_parse_pac_ret_clause (const char *pacret, const char *optname, + unsigned int *pacbti) +{ + const char *old_end = NULL; + const char *end = strchr (pacret, '+'); + size_t len = end ? end - pacret : strlen (pacret); + if (len == 7 && strncmp (pacret, "pac-ret", len) == 0) + { + *pacbti |= 2; + if (end != NULL) + { + /* pac-ret+... */ + arm_progress_next_token (&old_end, &end, &len, '+'); + if (len == 4 && strncmp (old_end, "leaf", len) == 0) + { + *pacbti |= 8; + if (end != NULL) + { + /* pac-ret+leaf+... */ + arm_progress_next_token (&old_end, &end, &len, '+'); + if (len == 5 && strncmp (old_end, "b-key", len) == 0) + { + /* Clear bit for A-key. */ + *pacbti &= 0xfffffffd; + *pacbti |= 4; + /* A non-NULL end indicates its pointing to a '+'. + Advance it to point to the next option in the string. */ + if (end != NULL) + end++; + } + else + /* This could be 'bti', leave it to caller to parse. */ + end = old_end; + } + } + else if (len == 5 && strncmp (old_end, "b-key", len) == 0) + { + /* Clear bit for A-key. */ + *pacbti &= 0xfffffffd; + *pacbti |= 4; + if (end != NULL) + { + /* pac-ret+b-key+... */ + arm_progress_next_token (&old_end, &end, &len, '+'); + if (len == 4 && strncmp (old_end, "leaf", len) == 0) + { + *pacbti |= 8; + /* A non-NULL end indicates its pointing to a '+'. + Advance it to point to the next option in the string. */ + if (end != NULL) + end++; + } + else + /* This could be 'bti', leave it to caller to parse. */ + end = old_end; + } + } + else + { + /* This could be a 'bti' option, so leave it to the caller to + parse. Fall through to the return. */ + end = old_end; + } + } + } + else + { + error_at (input_location, "unrecognized %s argument: %s", optname, pacret); + arm_print_hint_for_pacbti_option (); + return NULL; + } + + return end; +} + +unsigned int +arm_parse_pacbti_option (const char *pacbti, const char *optname, bool complain) +{ + unsigned int enable_pacbti = 0; + const char *end = strchr (pacbti, '+'); + size_t len = end ? end - pacbti : strlen (pacbti); + + if (strcmp (pacbti, "none") == 0) + return 0; + + if (strcmp (pacbti, "standard") == 0) + return 0x3; + + if (len == 3 && strncmp (pacbti, "bti", len) == 0) + { + /* bti+... */ + enable_pacbti |= 1; + + if (end != NULL + && arm_parse_pac_ret_clause (end + 1, optname, &enable_pacbti) != NULL + && complain == true) + { + /* If the value returned in non-NULL, there's garbage at the end, + so error. */ + error_at (input_location, "unrecognized %s argument: %s", + optname, pacbti); + arm_print_hint_for_pacbti_option (); + } + } + else + { + /* +... */ + end = arm_parse_pac_ret_clause (pacbti, optname, &enable_pacbti); + if (end != NULL) + { + if (strcmp (end, "bti") != 0) + { + if (complain == true) + { + error_at (input_location, "unrecognized %s argument: %s", + optname, pacbti); + arm_print_hint_for_pacbti_option (); + } + } + else + { + enable_pacbti |= 1; + } + } + } + + return enable_pacbti; +} + /* List the permitted architecture option names. If TARGET is a near miss for an entry, print out the suggested alternative. */ static void diff --git a/gcc/config/arm/arm-protos.h b/gcc/config/arm/arm-protos.h index 9b1f61394ad7d778a6c0b84bc6c805f14709f598..1f6984199382848e6d105ba7afd571a49d1ee885 100644 --- a/gcc/config/arm/arm-protos.h +++ b/gcc/config/arm/arm-protos.h @@ -584,6 +584,10 @@ const arch_option *arm_parse_arch_option_name (const arch_option *, void arm_parse_option_features (sbitmap, const cpu_arch_option *, const char *); + +unsigned int arm_parse_pacbti_option (const char *pacbti, const char *optname, + bool complain = true); + void arm_initialize_isa (sbitmap, const enum isa_feature *); const char * arm_gen_far_branch (rtx *, int, const char * , const char *); diff --git a/gcc/config/arm/arm.c b/gcc/config/arm/arm.c index 6c6e77fab666f4aeff023b1f949e3ca0a3545658..1f939a6b79a90430abf120e0aa075dfc1fab29a8 100644 --- a/gcc/config/arm/arm.c +++ b/gcc/config/arm/arm.c @@ -3217,6 +3217,13 @@ arm_configure_build_target (struct arm_build_target *target, tune_opts = strchr (opts->x_arm_tune_string, '+'); } + if (opts->x_arm_branch_protection_string) + { + arm_enable_pacbti + = arm_parse_pacbti_option (opts->x_arm_branch_protection_string, + "-mbranch-protection"); + } + if (arm_selected_arch) { arm_initialize_isa (target->isa, arm_selected_arch->common.isa_bits); diff --git a/gcc/config/arm/arm.opt b/gcc/config/arm/arm.opt index a7677eeb45c805d6a314f4d1d2d6063d8c0b687c..82dd665d327d514d9ad842905ddd3f9bb61e99a4 100644 --- a/gcc/config/arm/arm.opt +++ b/gcc/config/arm/arm.opt @@ -304,6 +304,13 @@ mbranch-cost= Target RejectNegative Joined UInteger Var(arm_branch_cost) Init(-1) Cost to assume for a branch insn. +mbranch-protection= +Target RejectNegative Joined Var(arm_branch_protection_string) Save +Use branch-protection features. + +TargetVariable +unsigned int arm_enable_pacbti = 0x0 + mgeneral-regs-only Target RejectNegative Mask(GENERAL_REGS_ONLY) Save Generate code which uses the core registers only (r0-r14).