From patchwork Thu Apr 18 20:10:37 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gustavo Romero X-Patchwork-Id: 789874 Delivered-To: patch@linaro.org Received: by 2002:adf:e6ca:0:b0:346:15ad:a2a with SMTP id y10csp751137wrm; Thu, 18 Apr 2024 13:13:06 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVdoaP9ZklWzkvY257e2zo1fSzYWI0bmDks2bRusScAg+DOJDekjx1Zzs78DxYzvNFHQeTGHTbxEaQs7hXpjRQj X-Google-Smtp-Source: AGHT+IEk7CewVFm69r8sdoFWN225VQNgtf0E22a6IK2QhSUvn7xp6tZmhZSxDTcROCTxesNKPYuT X-Received: by 2002:a05:690c:c8e:b0:618:8b6c:6c1b with SMTP id cm14-20020a05690c0c8e00b006188b6c6c1bmr83469ywb.4.1713471186358; Thu, 18 Apr 2024 13:13:06 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1713471186; cv=pass; d=google.com; s=arc-20160816; b=RyGtlTEAXSp5VsxHqWIE8yN+F1j8BYX2KjU0Q9n4J9TkOQHF9Vi/AJ27LDBk6ev6L1 vSVofcxVfc8i+5h7rH4seJeCZV3HirC+kC4WoeKC05GKc1cpSng+yIxCvgt+flgLLNy4 DSMomOnPsRD0GpLYaMX+d2P1fjUhXBSjmyF72EnwEbE2yJMeZKsfY8RoCeBQxKl9tFSD iMJaOmFO+ahoHBBIHUO9elhJy+FqAloqVofx1jVdKR4ymgq6YScSFRxCtPvB89NmG4wu LCUaFPs4CmWqsHf+6/TSsB4+MBIwZRbeFgcU9Ufwk+MFN4a7FBTD4KgJud0yLHYkl5f4 W8UA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature:arc-filter:dmarc-filter:delivered-to; bh=Jd4vwtUzOixjUh9lVMq2yIZw/U5QwziDmqPZK7rVgWI=; fh=PHP0XrDyWzrRvU96cs6sxWXpzsZ2PqUxQqbJpAaSNaA=; b=Sg1uhp1Cit50N5AXb8j0mrkIWUJJBxN/3f/RBKyWnWu3DNdV+86i41NV3h4advUpp6 lOzu/Bqhrqmhd0OgMtWbgPBs3cxG+aZPlEKn5whJsdpsNSdERT0g4T67AJzCsRTx8Drc NzOYTU9rqt4q5arr33464flB5A0ePSIlTksHiCmsmCXb6vqd/hWgZWFxwMKK/vymuGsl VxzjobToF9ihRffj8w/AbRuCsulsb//JBHikrE298DuKG6LmZIilshumLACMJBQEivss E2sjZkW47to9Tm1XuLLbUa3peOjnnPtH0CaMpH8HGQToqT1xim8kfQ+y2nKNZklTDNiJ 2n7g==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=yk2yAlnb; arc=pass (i=1); spf=pass (google.com: domain of gdb-patches-bounces+patch=linaro.org@sourceware.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gdb-patches-bounces+patch=linaro.org@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id y14-20020a05622a164e00b0043721c53999si2200782qtj.641.2024.04.18.13.13.06 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 18 Apr 2024 13:13:06 -0700 (PDT) Received-SPF: pass (google.com: domain of gdb-patches-bounces+patch=linaro.org@sourceware.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=yk2yAlnb; arc=pass (i=1); spf=pass (google.com: domain of gdb-patches-bounces+patch=linaro.org@sourceware.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gdb-patches-bounces+patch=linaro.org@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 06CA83858C35 for ; Thu, 18 Apr 2024 20:13:06 +0000 (GMT) X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from mail-pl1-x632.google.com (mail-pl1-x632.google.com [IPv6:2607:f8b0:4864:20::632]) by sourceware.org (Postfix) with ESMTPS id 402A33858401 for ; Thu, 18 Apr 2024 20:11:11 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 402A33858401 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=linaro.org ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 402A33858401 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::632 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1713471073; cv=none; b=NXBDeL1ge1t9AnkRunvVX74Wp8JDZwZDTyN9WubsbV+Tzt6ovwqOKeKy3/kcTqp9IANep+QSZ79ACR4CzU8xDy9RnANET6IHuUMzTW50AWnvKrn9hB7UbeZXJ6xNZtPkiLhmLAdXttWX6/IyI1LVGAGYp34gZr/WipoT/yoEf9A= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1713471073; c=relaxed/simple; bh=rsgmiX0OaaQbQDKYL8666bsdQDx/qa/EJRl98FTp9nY=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=Ab+y/+d1PshTVAi8EbH0IPoeYNeswaIbdW4atARiAOD/DZTWYx21J6BBNo0VvohoBZty911g1nADDrRss0RBnjSXVlt3aef2VIMftC8q840dIOPL0kc81qeVsnE3/5Dr2W0a9za+c/UwjApXb41XMI8OgFXqPA6+MZvMoLHwsgI= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pl1-x632.google.com with SMTP id d9443c01a7336-1e86d56b3bcso12196295ad.1 for ; Thu, 18 Apr 2024 13:11:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1713471070; x=1714075870; darn=sourceware.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Jd4vwtUzOixjUh9lVMq2yIZw/U5QwziDmqPZK7rVgWI=; b=yk2yAlnb1XdboKr0jv88bA2TwJoB3FvXP0r2U8kcgEHVfkJN2i1x8Amq27IRLHTHhH cw/yLZSNOfzIN2POeaNiXeYBgZqG5O52JBeJjo9teQC3xf7UwqMmza3aJVSpWxGeOFDA kP8eYQLhmAYJRttMVX1JHtOzqnF0mai7IJD5Ia1XIMdQ4cxzyZetz7cmCcQTboHUUSb5 ksx1Ds6Hf390ZE+k6CwboiUh6MNJxMkMsUIaHM79+W9bltvE73SYciRUUhi5RRfzZHJj 4/WfIM4L2HaoA6dLPZr7B53WvcKkJqzaV/ZxaxfbA+F856gq/0x2XQr/fiyU3Ru+XnLt P9JA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713471070; x=1714075870; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Jd4vwtUzOixjUh9lVMq2yIZw/U5QwziDmqPZK7rVgWI=; b=j36rYRVHk7O86qXYSRMLeA6gluGUMMRLp6Dgu/TzDd7BvN1AfStdXBQbQX7GWSv6dJ G2FUGg1EJVgfhifCdmjFXUWz+jVRJv5zUM9cyY/ZRcw80bbibYpu1g4b86QbiwZd1307 iqXi2B4l90P0kQGXwmxbYKM1QskzBrRPACG7Z4yZeyeeLfshIqCZknlPP27hxsNMpec4 EIXtCfsR1hnq9HhcwVuayLD4UNHxwE8gmqoECwiqTnMpqK5o6t0CIeQilPapvUnnG/9B 1KZwhB3MKhe5x57HtZdRra/ty8bTFJ1aZhk7ZMFaU+VOf8oa1A2pc3Wdo6djrNmTmqKv XWxQ== X-Gm-Message-State: AOJu0YwT3+DbYKlT4M0xNdvyxHZP5D0062yUVQrSNxbfm/dpJSaPzTAj ItgcV1r7DLxLCPmxlvRV126RCKrFH8uzFsBZJOIj8+om/tHeWVxL74d7JmRf0U8eddD4uBtRWVM a X-Received: by 2002:a17:902:f690:b0:1e2:7ea0:c1f2 with SMTP id l16-20020a170902f69000b001e27ea0c1f2mr155800plg.58.1713471069641; Thu, 18 Apr 2024 13:11:09 -0700 (PDT) Received: from amd.. ([2804:7f0:b403:ad57:3e7c:3fff:fe7a:e83b]) by smtp.gmail.com with ESMTPSA id n18-20020a170903111200b001e0b5eee802sm1925524plh.123.2024.04.18.13.11.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 18 Apr 2024 13:11:09 -0700 (PDT) From: Gustavo Romero To: gdb-patches@sourceware.org Cc: luis.machado@arm.com, thiago.bauermann@linaro.org, eliz@gnu.org, tom@tromey.com, gustavo.romero@linaro.org Subject: [PATCH v6 6/8] gdb: Add qIsAddressTagged packet Date: Thu, 18 Apr 2024 20:10:37 +0000 Message-Id: <20240418201039.236867-7-gustavo.romero@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240418201039.236867-1-gustavo.romero@linaro.org> References: <20240418201039.236867-1-gustavo.romero@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-13.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces+patch=linaro.org@sourceware.org This commit adds a new packet, qIsAddressTagged, allowing GDB remote targets to use it to query the stub if a given address is tagged. Currently, the memory tagging address check is done via a read query, where the contents of /proc//smaps is read and the flags are inspected for memory tagging-related flags that indicate the address is in a memory tagged region. This is not ideal, for example, for QEMU stub and other cases, such as on bare-metal, where there is no notion of an OS file like 'smaps.' Hence, the introduction of qIsAddressTagged packet allows checking if an address is tagged in an agnostic way. The is_address_tagged target hook in remote.c attempts to use the qIsAddressTagged packet first for checking if an address is tagged and if the stub does not support such a packet (reply is empty) it falls back to using the current mechanism that reads the contents of /proc//smaps via vFile requests. Signed-off-by: Gustavo Romero Tested-By: Luis Machado --- gdb/remote.c | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) diff --git a/gdb/remote.c b/gdb/remote.c index cd271c4b493..3d034bb1ef8 100644 --- a/gdb/remote.c +++ b/gdb/remote.c @@ -337,6 +337,9 @@ enum { packets and the tag violation stop replies. */ PACKET_memory_tagging_feature, + /* Support for the qIsAddressTagged packet. */ + PACKET_qIsAddressTagged, + PACKET_MAX }; @@ -15535,6 +15538,50 @@ create_store_memtags_request (gdb::char_vector &packet, CORE_ADDR address, strcpy (packet.data (), request.c_str ()); } +static void +create_is_address_tagged_request (gdbarch *gdbarch, gdb::char_vector &packet, + CORE_ADDR address) +{ + int addr_size; + std::string request; + + addr_size = gdbarch_addr_bit (gdbarch) / 8; + request = string_printf ("qIsAddressTagged:%s", phex_nz (address, addr_size)); + + if (packet.size () < request.length () + 1) + error (_("Contents too big for packet qIsAddressTagged.")); + + strcpy (packet.data (), request.c_str ()); +} + +static bool +check_is_address_tagged_reply (remote_target *remote, gdb::char_vector &packet, + bool &tagged) +{ + gdb_assert (remote != nullptr); + /* Check reply and disable qIsAddressTagged usage if it's not supported. */ + packet_result result = remote->m_features.packet_ok (packet, + PACKET_qIsAddressTagged); + + /* Return false on error (Exx), empty reply (packet not supported), or reply + size doesn't match 2 hex digits. */ + if ((result.status () != PACKET_OK) || (strlen (packet.data ()) != 2)) + return false; + + gdb_byte reply; + /* Convert only 2 hex digits, i.e. 1 byte in hex format. */ + hex2bin (packet.data (), &reply, 1); + + if (reply == 0x00 || reply == 0x01) + { + tagged = !!reply; + return true; + } + + /* Invalid reply. */ + return false; +} + /* Implement the "fetch_memtags" target_ops method. */ bool @@ -15581,6 +15628,31 @@ remote_target::store_memtags (CORE_ADDR address, size_t len, bool remote_target::is_address_tagged (gdbarch *gdbarch, CORE_ADDR address) { + /* Firstly, attempt to check the address using the qIsAddressTagged + packet. */ + if (m_features.packet_support (PACKET_qIsAddressTagged) != PACKET_DISABLE) + { + remote_target *remote = get_current_remote_target (); + struct remote_state *rs = get_remote_state (); + bool is_addr_tagged; + + create_is_address_tagged_request (gdbarch, rs->buf, address); + + putpkt (rs->buf); + getpkt (&rs->buf); + + /* If qIsAddressTagged is not supported PACKET_qIsAddressTagged will be + set to PACKET_DISABLE so no further attempt is made to check addresses + using this packet and the fallback mechanism below will be used + instead. Also, if the check fails due to an error (Exx reply) the + fallback is used too. Otherwise, the qIsAddressTagged query succeeded + and is_addr_tagged is valid. */ + if (check_is_address_tagged_reply (remote, rs->buf, is_addr_tagged)) + return is_addr_tagged; + } + + /* Fallback to arch-specific method of checking whether an address is tagged + in case check via qIsAddressTagged fails. */ return gdbarch_tagged_address_p (gdbarch, address); } @@ -16070,6 +16142,9 @@ Show the maximum size of the address (in bits) in a memory packet."), NULL, add_packet_config_cmd (PACKET_memory_tagging_feature, "memory-tagging-feature", "memory-tagging-feature", 0); + add_packet_config_cmd (PACKET_qIsAddressTagged, + "qIsAddressTagged", "memory-tagging-address-check", 0); + /* Assert that we've registered "set remote foo-packet" commands for all packet configs. */ {