From patchwork Wed Apr 17 21:04:22 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gustavo Romero X-Patchwork-Id: 789343 Delivered-To: patch@linaro.org Received: by 2002:adf:e6ca:0:b0:346:15ad:a2a with SMTP id y10csp233775wrm; Wed, 17 Apr 2024 14:06:39 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUQ9UE7sRUag0wCE++bf87HwEz2tJh/+ApkPlQj+GBcFImOAgfQyGgr0CTduliOXImZhY8qLgb8cAKvq/Q1/6sw X-Google-Smtp-Source: AGHT+IFO7GTar1deST1ofTYp6ctCBNu63Ef8lf5vXaHnvI9LeHxOQoTsxnmqG96aWDMup8IvfCqY X-Received: by 2002:ac8:7f16:0:b0:437:79db:d662 with SMTP id f22-20020ac87f16000000b0043779dbd662mr615024qtk.15.1713387998828; Wed, 17 Apr 2024 14:06:38 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1713387998; cv=pass; d=google.com; s=arc-20160816; b=r7HnH3rcS8sXNrbkEcIubZsvm9S0bi7o4BxpkS+rGie2iVvppLkltE10vNNKv/I5Ia +cOiPSgHNRYSe6ndAp0EGKCEJDypNV+tn0Ss7YHhFeJmKQN8lMtILrD+SqlS9BIAJAXB MWIQmufAUdIL/lUWpxB5vMh72JUUd3DZXHKT12jl9HEg2NTKrAFtGWOlGgTS32f+BoMs Yrbn63Bkh68yd2EYz//4IFYD2we2mHXANxiKV28SlMiTUXL1zH/uB+f2jsyMMzl9I5ZM DQqy/xPDopITdj6IPC5wYxQQm2Au57CR4obQR7YcpV5XGU8t5CSpVE4F3NTy63IY6Ynx XqJA== 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=jJMnCAS7JhNlNLrR5zkoIjLZBdkOSQn5g55tAEWRYyc=; fh=PHP0XrDyWzrRvU96cs6sxWXpzsZ2PqUxQqbJpAaSNaA=; b=vcPenFqDVLT4eUGTjpUt2oc8Cs9ff5uVtK9TzydyCEFbuOqbbtVzdXA5WnJ+UikTNF SffcrHt6GXWrQkm1pT3Nuxtfl3zSCY5r2UcuHNwmx35aTL74/uwrbl2dvH9WOcJhGgsF sTwVU5jZlJ4Dc4VsHI3PbxadHmAUGwp9ac180A4oWMN/Yve13N1UH1732fhaGV3rCPLB q2upvGkyV7rgUBOzMx3F/TunVsPTdoJcPPdpHaf8+GXzMJC4jHmS4Irt6CTpeP+a588V B2A0hZtg9Y/5byo398SInaucpJuS6nzZfcJ60N9ujE8Db2LD2448GVzDIfjcFQMwPeCC g+NQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b="sWpjcs/2"; 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 l15-20020a05622a050f00b004365858c699si34147qtx.138.2024.04.17.14.06.38 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 17 Apr 2024 14:06:38 -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="sWpjcs/2"; 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 784D23861870 for ; Wed, 17 Apr 2024 21:06:38 +0000 (GMT) X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from mail-pl1-x634.google.com (mail-pl1-x634.google.com [IPv6:2607:f8b0:4864:20::634]) by sourceware.org (Postfix) with ESMTPS id 7054B3858427 for ; Wed, 17 Apr 2024 21:04:59 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 7054B3858427 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 7054B3858427 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::634 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1713387902; cv=none; b=u2ywucCmPt30ILiY17LssOQLyPAsqkrIu6g1ZhuTo5NfMtktML2UWONwb+CQykSkaTAFTgRu/9nRVzQYb9TtvEzSlINoHEk3gqB8Cx9tuKszTiUsqdoPVAd8ouBRyDPQiCYmRJ7PxU1SN6tmOwpzBlkEfSN/EQSKn/ZH8a04UUo= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1713387902; c=relaxed/simple; bh=vop3zeMlI32VNl+L7d6SvD68a1TBDsSzmZxY05GPZLg=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=TwHJp6loHr+8dBPEurpWGkyDRSCK2KR99fTsvw/K8K+bLab6VJ+kdNgSpdz1mEUWF8lQdDsPEKdi2pY3pe2KYSZhEB+CQ5+tNPEX0jyF+vpMnzMBfAg9ohg35poh4y129NU8GoMflIPnUAwX7U/DDUvlSpv78QkhKqgJJdcTTsc= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pl1-x634.google.com with SMTP id d9443c01a7336-1e3ff14f249so1536405ad.1 for ; Wed, 17 Apr 2024 14:04:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1713387898; x=1713992698; 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=jJMnCAS7JhNlNLrR5zkoIjLZBdkOSQn5g55tAEWRYyc=; b=sWpjcs/2rqXFuZXSHyOQzXhctzK+bi7/T1VHFz/KP37nLLGTXrFilq+nFBAG/PCB8k X1zO97hSdLrVCYaM+EDVVPiGN2MTtnfaWfE/xEIZIE7RvOsnA3px1de6Lo7w0zCCes3x wVzeUhexust6/vm0I1A6M4l5q7QQCwuiiatJ/BX4XnkpWMTT7oQklbqYSF5Yma5+i6Fx XnSivBl0zk9KNVFY0ITt0TcZZtDqW5yaK5jbbdQwg61k8f64PgGHlfaoBW0F4RDt9HSp HMX/mcEAnm+Uik3nuWKM4ISzhrxBTbYckyBDP2dh83uYWUn67pC6oz1lVzZ16xjpVoKZ XHkA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713387898; x=1713992698; 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=jJMnCAS7JhNlNLrR5zkoIjLZBdkOSQn5g55tAEWRYyc=; b=lZvGjnjeduskTI9XmE7oj0oBxklQbnGmNy0/qqsJBQUBEKoP61k0Ut4aqdnuEB9kLd nklxzkPUvThdZm2guBGf9mPY4PAkPyCVFotnuwnD65zQyD+4vVkuG4YFdADQ4pgN9QZY 0kuO+gVnc3C5xVxwGvBJImUzxXG7XWIBwhodBxJC7OeUzpUQMVGpIVFNZx0nL239vfJj KBjysILKZf+w2q6FrAyW3pPTKfuTj7FEr14ZtdruCdYSYcyCDoVTWqobXDNuQ1UTeoHg w4Lx8v/s2XRT3b1X0oZeuSXXXeraWEsaewG3j53BSONo4SB4bW17XCRi1OefluYI6Esc 91Ww== X-Gm-Message-State: AOJu0Yy90r+mq9x6dVqcIRkOAos9BciqMQWMxfUMDGrtKTnpve3H1dSR eusyGLyEEEipMfBcskHHbmLUJfzMHfvoKrbBGTToi7cM2SKhuXTfJ2hvGqGi4UM6nOc+qL/adNx B X-Received: by 2002:a17:902:e54c:b0:1e4:53b6:6be with SMTP id n12-20020a170902e54c00b001e453b606bemr409471plf.1.1713387897935; Wed, 17 Apr 2024 14:04:57 -0700 (PDT) Received: from amd.. ([2804:7f0:b403:ad57:3e7c:3fff:fe7a:e83b]) by smtp.gmail.com with ESMTPSA id l9-20020a170903120900b001e3e0aa9776sm80494plh.27.2024.04.17.14.04.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 17 Apr 2024 14:04:57 -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 v5 6/8] gdb: Add qIsAddressTagged packet Date: Wed, 17 Apr 2024 21:04:22 +0000 Message-Id: <20240417210424.216374-7-gustavo.romero@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240417210424.216374-1-gustavo.romero@linaro.org> References: <20240417210424.216374-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 --- gdb/remote.c | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) diff --git a/gdb/remote.c b/gdb/remote.c index cd271c4b493..2bb962955b5 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,49 @@ 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) or empty reply (packet not supported). */ + if (result.status () != PACKET_OK) + 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 +15627,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 +16141,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. */ {