From patchwork Sun Jun 8 01:03:15 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thiago Jung Bauermann X-Patchwork-Id: 894804 Delivered-To: patch@linaro.org Received: by 2002:a05:6000:ecd:b0:3a4:ee3f:8f15 with SMTP id ea13csp1175957wrb; Sat, 7 Jun 2025 18:07:22 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCX4Janhm4yAdc/7N7F82BQ6YS5ZdsCHSLpB6IT2tnSJUA7bvC3I5ldMCv7M0fYbY9Imo3UkWw==@linaro.org X-Google-Smtp-Source: AGHT+IFTccWTjb9qzXUs/gdYkGR3H7tm67pWWYd1ljkUJfklALan2ih7x3ngOKfSm+vk2l6oLRjq X-Received: by 2002:ac8:6f10:0:b0:4a5:9993:ede8 with SMTP id d75a77b69052e-4a6f0527c7cmr69589171cf.15.1749344842529; Sat, 07 Jun 2025 18:07:22 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1749344842; cv=pass; d=google.com; s=arc-20240605; b=T5MMNlWRG8/30x91elXWpDvfUtDiTxO93GQboFlEf0j0oWic6LSl886+TzZAPlSmhn JSdeE84dx+nXpfL27CorjwP4GKJT6ySNamX3AD7TYfRNTrpplHsed2/7l91vW7wtx/fK HS+78SshSwtBGMh2Sy1kDwXBO9vclVpNb6kVxbE3r8ywK1kCqynv07V/I/gu73iXF0zh 3Vy76jYFf1p1lgG6nT4N4xMR9CwJLCZhP/2jtZguHniZv666YbHwEJ7yU4G+lyJOJZE5 pa1uN7NZbA8EqIe3NTRS9W6cS1sstt2LNeLFlEZjpA9PCV8Z3XrfrKJ/8FKswUYf0o80 48/Q== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; 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:to:from :dkim-signature:dkim-filter:arc-filter:dmarc-filter:delivered-to :dkim-filter; bh=+P25Ol1rrL9RXQ+IIwq9Mq3FFZED8KMAP6S2By2GSvM=; fh=72kqq0iQhigvR9Vv/oqX5ebs3Yyyw7XhzWxOOEPdupI=; b=Tk1ajCKXSviWTPpFQin1BdL2puPQHbEQ9Cz+e9vORvlF80E3uxtxLrxleziHMDaL6P eW8tBgBLhO3UylhETEILTASOgegLLNS2NIVaGBdvbZGl+3Zdj0u7sUzc/hzP2PGJtW3Z IHZLruU74wIVsmEcfir6ZoqgKK/LYfLVDSo+PDE4RucwN6RkIbR/60fnhdTAUpNJkfPK eYDzMhmoVH9ZQjR3++tLZZhCEXVwS0dNuRlbDCKyu8VIE9TzJGfmJXvnwmfMI4j7P5PU RLuqe59UsY1hggSkxxZEMV1LrpoAuybB0I0NPRffcVHia16HyB9ylZazcNUDc+Mvq4O0 8pIw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=KBYEyQBG; arc=pass (i=1); spf=pass (google.com: domain of gdb-patches-bounces~patch=linaro.org@sourceware.org designates 8.43.85.97 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. [8.43.85.97]) by mx.google.com with ESMTPS id d75a77b69052e-4a6198903ddsi49295381cf.369.2025.06.07.18.07.22 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 07 Jun 2025 18:07:22 -0700 (PDT) Received-SPF: pass (google.com: domain of gdb-patches-bounces~patch=linaro.org@sourceware.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=KBYEyQBG; arc=pass (i=1); spf=pass (google.com: domain of gdb-patches-bounces~patch=linaro.org@sourceware.org designates 8.43.85.97 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 08C6B3857B94 for ; Sun, 8 Jun 2025 01:07:22 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 08C6B3857B94 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=linaro.org header.i=@linaro.org header.a=rsa-sha256 header.s=google header.b=KBYEyQBG X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from mail-vs1-xe2e.google.com (mail-vs1-xe2e.google.com [IPv6:2607:f8b0:4864:20::e2e]) by sourceware.org (Postfix) with ESMTPS id C27153858039 for ; Sun, 8 Jun 2025 01:03:55 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C27153858039 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 C27153858039 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::e2e ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1749344635; cv=none; b=XsM3QuKZisq/RwWo+YDxBd1RBByi/bey+nS+e9naHUtX4XLaPHhHE0xq9MC3Zv3lqmvIzHEdZ4/M+j6E8dGXQWsZGQ2KD/UQ81c/iO1YJfceiuzbt8/7wRPEU0rRSfHJyl58f0oi5ufWDl1CsdLgKs3I7FOWsef46ra5gO5tXFM= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1749344635; c=relaxed/simple; bh=6NHOUl68GW8sxmUEBViX/0cRp/rWd32691vjp/X7dyI=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=S7mPsWnnxzssWB5CchSJXEdEt3e5bZjRFz7WYk86/PZExtBYfiqfcv4T2tDNx/W/1j8ZhnBBPd12WCDV5iW/hUEjg1Ce2UPSTIZuP6DIYpnPiYrflzzt4MdXy22vBx3+mvTbawUzkTWumWNscz4CUpkUNE7kDBo2g+5RlcX/RtI= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C27153858039 Received: by mail-vs1-xe2e.google.com with SMTP id ada2fe7eead31-4e4564178bcso2954148137.1 for ; Sat, 07 Jun 2025 18:03:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1749344635; x=1749949435; darn=sourceware.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=+P25Ol1rrL9RXQ+IIwq9Mq3FFZED8KMAP6S2By2GSvM=; b=KBYEyQBGhlN9NJsUATugn9eGhAQIcv+I0YTwCmgqMBpqTQWShGYuWK/fXSo49ycNIu abrmrgyRS56t+SKW/bwHs03WYkFI+esUKC6DzwjPTaw4dOaxrag5bEZZaIvL3j9vUHR0 tNdj9dOuvAuj6iEMse/vJr+LKjYs7QlLU0LvyFAiToIaAW3yT90NGzKFpOiXNdBUR7p0 rHkaq6LK9+t6yblA92BLkIqsP0eNpMSEa1tvJxFsn572SBtc5yl0tJYHYM6/aYF0CwuV ViF6iFhIx9SlOH7+vbedLCSL9YFXofYiSIUmaj7xVKrJs/fMubCwzflEhtRozt1n2sab zEMw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1749344635; x=1749949435; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=+P25Ol1rrL9RXQ+IIwq9Mq3FFZED8KMAP6S2By2GSvM=; b=IE5MXruhiPH/6I/6UNngQLP0dYG0hm8CtQJi7pIUdGcZGODX7M+M3w16cve5KDJDqI 3qDAm1M913URU51/VMImCaDloy/K+n3LI5r4Ctplc3Jx34YAzicKCziJ3TSYZzP5UHnP chLNj6VdBRywbKayVVPn9Onue7Ney1XSRp4nbR3JRcBLd9rKsBMutRpj3StbvEJNRvju kK2fP1SGPOXffc+Td3vl7pqk7/hwgtTvLiFIbmpuD9nVT36tkc2YmiU5NOVr+d8gXifc 7wtl2H3syg/gxwELBYoNgdUsdC0yfclgHg7XvV4zq5op7gT4ARP1qc5FJ7BGZN66/ojI 3jaA== X-Gm-Message-State: AOJu0Yzf2Wm7KW53pTAKGaE4lY1HSRWxERW5YxYgsXnSBgQzqbvmHZSX laJzAihpZlQRm8ePZinFNl9jG7y5m0g9/NQ3PV9NmIgXy6PTWylFXt6a7wSJ6CLci8g58OJhsB/ J8xeJ X-Gm-Gg: ASbGncuY50KAJhfm0gVIOzI7DtWNb8zwIkMjKWeraHVvRs4CXnpnrS7rD49v65lB5jr zT8pImM+dEbcxAHoGpRNZW/i+FgS3qeKPrWDIAjXse7Ebye38CmVND68pE+h7loyci29hF0ek81 wkcNpvmqSHk1jPABD+TRgcPfln8jDqROdJkS9WlSbAJiggw5WdgorJz8LUeaij/naO26hU+tsID Gehs2TDS0M47lC78uvORhYvGdJBxSCuaT42fw5jrQQ184eghY0mUj93qgMfoIPxBKSixoACejml Zy8wTP4h75vvVLHVkvy4wWZobrL9v8ocCSIr4ah0kwgBCLks2vXKaomBh4n+pA== X-Received: by 2002:a05:6102:a4d:b0:4e2:e5ec:fa09 with SMTP id ada2fe7eead31-4e772bce81emr7724094137.6.1749344634980; Sat, 07 Jun 2025 18:03:54 -0700 (PDT) Received: from localhost ([2804:14d:7e39:88d6:8cb1:1e8e:e951:f687]) by smtp.gmail.com with ESMTPSA id ada2fe7eead31-4e7735e5172sm3248876137.0.2025.06.07.18.03.54 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 07 Jun 2025 18:03:54 -0700 (PDT) From: Thiago Jung Bauermann To: gdb-patches@sourceware.org Subject: [PATCH 4/8] GDB: aarch64-linux: GCS support in Linux signals Date: Sat, 7 Jun 2025 22:03:15 -0300 Message-ID: <20250608010338.2234530-5-thiago.bauermann@linaro.org> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250608010338.2234530-1-thiago.bauermann@linaro.org> References: <20250608010338.2234530-1-thiago.bauermann@linaro.org> MIME-Version: 1.0 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 The signal frame can have a GCS context, so teach GDB how to use it. Also, there's a new SEGV sigcode when the inferior does an illegal memory access in the Guarded Control Stack, so display a message when that is the case. --- gdb/aarch64-linux-tdep.c | 83 ++++++++++++++++++++++++++++++++---- gdb/arch/aarch64-gcs-linux.h | 4 ++ gdb/doc/gdb.texinfo | 8 ++++ 3 files changed, 86 insertions(+), 9 deletions(-) diff --git a/gdb/aarch64-linux-tdep.c b/gdb/aarch64-linux-tdep.c index ce213bb482b9..24fb151311c4 100644 --- a/gdb/aarch64-linux-tdep.c +++ b/gdb/aarch64-linux-tdep.c @@ -165,6 +165,7 @@ #define AARCH64_ZA_MAGIC 0x54366345 #define AARCH64_TPIDR2_MAGIC 0x54504902 #define AARCH64_ZT_MAGIC 0x5a544e01 +#define AARCH64_GCS_MAGIC 0x47435300 /* Defines for the extra_context that follows an AARCH64_EXTRA_MAGIC. */ #define AARCH64_EXTRA_DATAP_OFFSET 8 @@ -206,6 +207,11 @@ the signal context state. */ #define AARCH64_SME2_CONTEXT_REGS_OFFSET 16 +/* GCSPR register value offset in the GCS signal frame context. */ +#define AARCH64_GCS_CONTEXT_GCSPR_OFFSET 8 +/* features_enabled value offset in the GCS signal frame context. */ +#define AARCH64_GCS_CONTEXT_FEATURES_ENABLED_OFFSET 16 + /* Holds information about the signal frame. */ struct aarch64_linux_sigframe { @@ -246,6 +252,13 @@ struct aarch64_linux_sigframe bool za_payload = false; /* True if we have a ZT entry in the signal context, false otherwise. */ bool zt_available = false; + + /* True if we have a GCS entry in the signal context, false otherwise. */ + bool gcs_availabe = false; + /* The Guarded Control Stack Pointer Register. */ + uint64_t gcspr; + /* Flags indicating which GCS features are enabled for the thread. */ + uint64_t gcs_features_enabled; }; /* Read an aarch64_ctx, returning the magic value, and setting *SIZE to the @@ -526,6 +539,39 @@ aarch64_linux_read_signal_frame_info (const frame_info_ptr &this_frame, signal_frame.zt_section = section; signal_frame.zt_available = true; + section += size; + break; + } + case AARCH64_GCS_MAGIC: + { + gdb_byte buf[8]; + + /* Extract the GCSPR. */ + if (target_read_memory (section + AARCH64_GCS_CONTEXT_GCSPR_OFFSET, + buf, 8) != 0) + { + warning (_("Failed to read the GCSPR from the GCS signal frame" + " context.")); + section += size; + break; + } + + signal_frame.gcspr = extract_unsigned_integer (buf, byte_order); + + /* Extract the features_enabled field. */ + if (target_read_memory (section + + AARCH64_GCS_CONTEXT_FEATURES_ENABLED_OFFSET, + buf, sizeof (buf)) != 0) + { + warning (_("Failed to read the enabled features from the GCS" + " signal frame context.")); + section += size; + break; + } + + signal_frame.gcs_features_enabled + = extract_unsigned_integer (buf, byte_order); + signal_frame.gcs_availabe = true; section += size; break; } @@ -703,6 +749,19 @@ aarch64_linux_sigframe_init (const struct tramp_frame *self, + AARCH64_TPIDR2_CONTEXT_TPIDR2_OFFSET); } + /* Restore the GCS registers, if the target supports it and if there is + an entry for them. */ + if (signal_frame.gcs_availabe && tdep->has_gcs ()) + { + /* Restore GCSPR. */ + trad_frame_set_reg_value (this_cache, tdep->gcs_reg_base, + signal_frame.gcspr); + /* Restore gcs_features_enabled. */ + trad_frame_set_reg_value (this_cache, tdep->gcs_linux_reg_base, + signal_frame.gcs_features_enabled); + /* gcs_features_locked isn't present in the GCS signal context. */ + } + trad_frame_set_id (this_cache, frame_id_build (signal_frame.sp, func)); } @@ -2486,17 +2545,18 @@ aarch64_linux_report_signal_info (struct gdbarch *gdbarch, { aarch64_gdbarch_tdep *tdep = gdbarch_tdep (gdbarch); - if (!tdep->has_mte () || siggnal != GDB_SIGNAL_SEGV) + if (!(tdep->has_mte () || tdep->has_gcs ()) || siggnal != GDB_SIGNAL_SEGV) return; CORE_ADDR fault_addr = 0; - long si_code = 0; + long si_code = 0, si_errno = 0; try { /* Sigcode tells us if the segfault is actually a memory tag violation. */ si_code = parse_and_eval_long ("$_siginfo.si_code"); + si_errno = parse_and_eval_long ("$_siginfo.si_errno"); fault_addr = parse_and_eval_long ("$_siginfo._sifields._sigfault.si_addr"); @@ -2507,13 +2567,18 @@ aarch64_linux_report_signal_info (struct gdbarch *gdbarch, return; } - /* If this is not a memory tag violation, just return. */ - if (si_code != SEGV_MTEAERR && si_code != SEGV_MTESERR) + const char *meaning; + + if (si_code == SEGV_MTEAERR || si_code == SEGV_MTESERR) + meaning = _("Memory tag violation"); + else if (si_code == SEGV_CPERR && si_errno == 0) + meaning = _("Guarded Control Stack error"); + else return; uiout->text ("\n"); - uiout->field_string ("sigcode-meaning", _("Memory tag violation")); + uiout->field_string ("sigcode-meaning", meaning); /* For synchronous faults, show additional information. */ if (si_code == SEGV_MTESERR) @@ -2539,7 +2604,7 @@ aarch64_linux_report_signal_info (struct gdbarch *gdbarch, uiout->field_string ("logical-tag", hex_string (ltag)); } } - else + else if (si_code != SEGV_CPERR) { uiout->text ("\n"); uiout->text (_("Fault address unavailable")); @@ -2838,9 +2903,6 @@ aarch64_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch) /* Register a hook for checking if an address is tagged or not. */ set_gdbarch_tagged_address_p (gdbarch, aarch64_linux_tagged_address_p); - set_gdbarch_report_signal_info (gdbarch, - aarch64_linux_report_signal_info); - /* Core file helpers. */ /* Core file helper to create a memory tag section for a particular @@ -2857,6 +2919,9 @@ aarch64_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch) aarch64_linux_decode_memtag_section); } + if (tdep->has_mte () || tdep->has_gcs ()) + set_gdbarch_report_signal_info (gdbarch, aarch64_linux_report_signal_info); + /* Initialize the aarch64_linux_record_tdep. */ /* These values are the size of the type that will be used in a system call. They are obtained from Linux Kernel source. */ diff --git a/gdb/arch/aarch64-gcs-linux.h b/gdb/arch/aarch64-gcs-linux.h index 9366caa7289a..853e748faf2d 100644 --- a/gdb/arch/aarch64-gcs-linux.h +++ b/gdb/arch/aarch64-gcs-linux.h @@ -41,4 +41,8 @@ struct user_gcs #endif /* GCS_MAGIC */ +#ifndef SEGV_CPERR +#define SEGV_CPERR 10 /* Control protection error. */ +#endif + #endif /* ARCH_AARCH64_GCS_LINUX_H */ diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo index 03f419e90436..1b35fa029884 100644 --- a/gdb/doc/gdb.texinfo +++ b/gdb/doc/gdb.texinfo @@ -26994,6 +26994,14 @@ information, see the @uref{https://www.kernel.org/doc/html/latest/arch/arm64/gcs.html,ignored, documentation} in the Linux kernel. +To aid debugging, @value{GDBN} will note when SIGSEGV signals are generated +as a result of a Guarded Control Stack error: + +@smallexample +Program received signal SIGSEGV, Segmentation fault +Guarded Control Stack error. +@end smallexample + @node x86 @subsection x86