From patchwork Wed Jun 18 05:54:42 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: 897543 Delivered-To: patch@linaro.org Received: by 2002:a5d:5f54:0:b0:3a4:ee3f:8f15 with SMTP id cm20csp136279wrb; Tue, 17 Jun 2025 23:01:01 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVvaJP1t0yd0HhH0SAj70fPMJ7RfasHEPUYVxuWlAdqz+26Cayx1VWyFypV+rEIBxCRKTOnxg==@linaro.org X-Google-Smtp-Source: AGHT+IGaHWO+NfJMnajuQnlEmLvAwO8DS6NV8nD049J3Ocs+QBu0Ypn7TnGJvaHHT9WSPoMTZRL2 X-Received: by 2002:a05:6122:469a:b0:530:7e05:3839 with SMTP id 71dfb90a1353d-53149c0d1ddmr11566566e0c.11.1750226461244; Tue, 17 Jun 2025 23:01:01 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1750226461; cv=pass; d=google.com; s=arc-20240605; b=lqTAKnGXLU6BCrntY9LROYtgolaB4nl6XsubsezIj/pKspco1cdUCj7xSdZl873XgM 1Hy6eUD3fx9FU9azU4BnBoA2pf9avDBQ3Ohxr+ac/7vxcMJdiVoOOTXieybRbjrZBWm3 bTC6pDsxhXS/5E00GzF3f0EA7EuBGTFrqcXZ2M0QmHIHDhFlTol79Cd2VqKp+JY3Vtu5 Eg6Fc8GhefZQSEornvm54ez3kqA9OlR2XRHgdNBFgA7V5duYiiDMPy/53ZF39gXAnx36 lN2r6sFjo/Wn5GhHf5wT+oQVxzgc+7VzHH9Xx/GEPL7P5CPU6UEbw2LDtlB3wEDNk3C3 dNLQ== 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:cc:to :from:dkim-signature:dkim-filter:arc-filter:dmarc-filter :delivered-to:dkim-filter; bh=7gMHT+QuBNqwx8wE3rHEKapX7dO5bAysWX7HkETHjFY=; fh=v0Swrqm/HGzmFfs2Lyn4KGEK39u4NVWMm4qGXeDZh/s=; b=gHw+u6XzU3bJQ6QXI6UwlANy2fUhC30czfIPOVzodzuRJzlDM2hKwDmd8NgOQKYyhu D7LeYuftST7e/NXGObsBypHRkLq9R3iejHSCrhpimhvhhcNZROWA+Yvn6J7zAJ1AJPdH 63U+EzWN6yqMdLI9+PuotZgBlzm6Qh4rmn6j5n2nhykoTlskO3n1o9EIEYPeNKnQ9vFs sqgYwb+4RxZOWp/2SbdJc7npRHCWTQo6hhOzAcy+r8FpNmDQEl/rujE89n+UiXow4z+g 1lykbl+75pi+XZu48Fn/mCSjvCO//mlsJs66Dnp97ZOJgA4bHAIHIcyMX/9hkWB1F7gg XsWA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b="saT/XPoT"; 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 71dfb90a1353d-5313e0e9281si2746526e0c.213.2025.06.17.23.01.01 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 17 Jun 2025 23:01:01 -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="saT/XPoT"; 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 B35473816969 for ; Wed, 18 Jun 2025 06:01:00 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org B35473816969 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=saT/XPoT X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from mail-pf1-x42c.google.com (mail-pf1-x42c.google.com [IPv6:2607:f8b0:4864:20::42c]) by sourceware.org (Postfix) with ESMTPS id 6D442388BA6C for ; Wed, 18 Jun 2025 05:55:07 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 6D442388BA6C 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 6D442388BA6C Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::42c ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1750226107; cv=none; b=sHkng606jnoG8GS84jShoxVRoGAGM0YrgwcdaZCbfeBqyDkMxLX/XpxoIp4OWRS8i5AGN4nWhIENal+zgcTq1bMZoIMJGtDlDt0S7lV5BSdpIyDZZ0L86zZKkZEZ6agqPXrePVrhGl1axdhbua9kjfXUkG7k9PZuym0KH7rCrLo= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1750226107; c=relaxed/simple; bh=3FmdXal1N5zP73/YIMAC9gqv9TIhRjPnpIf8VthEjOI=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=Q+ZOyZgwfD/v7GlmTT9ecY+SspIGtesDusY2FfDEY48XnUJFBcEZDXyi8lDQazHVT8ZSQfXv4H9GOtCL6W4wkdEcTz061wbT71z3JBCw7slWkunF9bijApe8+olPObTsUFE5lOMSeGYVI+LeS2xo+6Q3ItsoG+nlAQ1FWrqGmk0= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 6D442388BA6C Received: by mail-pf1-x42c.google.com with SMTP id d2e1a72fcca58-73c17c770a7so7240292b3a.2 for ; Tue, 17 Jun 2025 22:55:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1750226106; x=1750830906; 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=7gMHT+QuBNqwx8wE3rHEKapX7dO5bAysWX7HkETHjFY=; b=saT/XPoTakXC/0/ppKpfNRIotzEnetXFurL+m3UJzX2YZE8v11CmV2/nZ6moe30Dw0 eChHvgkFPWxC765UK9KsPkkzL/4+P86L+BqFSuBcSs9NoxU8JxyltDm6eEOpLpSY01vg FYjwGo8qq/BhOKxXRVSlPJ7HmL8abiIsk0DGD6UMrh7jW3bPBlBVWP8DRR60hAdugrOA ViOqv+pY5+YgJl56kd3lW06cO/FMeZWFTVz1JgZhcQAnPVD85RIBhkkIELrEMmXBChrL CSMMi/799mT55Bbjgs/CxCzWDW1reRE9DVASVc4GI/V2RKpjBUdwYoJDX6lvyV+uitRc XxeA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1750226106; x=1750830906; 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=7gMHT+QuBNqwx8wE3rHEKapX7dO5bAysWX7HkETHjFY=; b=vm1FGgVrS91/kPvgHz4Bxe16uqU3+PAvVBejEtDF4uH8y2w5+PZRlrzMYo30+QGI6e voZ2IDuqWPX/0P3rKjJUEvsnQUMYYyhUOebTp0t+VWONq9plXCdcm3HXaFFOmdiTIDn5 M6Yzp+reDOC0y9JMHVgags0mdyrIrBRWZReMSHNOj9+8XdF/wnFv7ziTUHi2fn/hWXfC yJjRzm/B54vjvfF006ODDSlbLXR5V3cJqjnDG3PCz3yUw9llbqv8NPRbehSZ+Ok4V6ef LOYI0RVEjdoZjaasgd4dw669qfW+N25nQ9PCBqlqOcuVvPS68VUWj3UgAC/1HapCmEvm iF4w== X-Gm-Message-State: AOJu0Yyfw0M57nuen1YFjPQqL89LY73oAbvURs4k9OQdZIJJ5RXyLp6X HaEVIv9MeuF2yJTHCBoNDpm3+38k5vCcCKiu/07wLm8q4sv4ey8ZgW7IhxUt8lxeHAaMqGxp2cH eMPRp X-Gm-Gg: ASbGncvOvlp6Bs54nEnxUASRLppr1h5TEIJk6MHnYI4QWhOnQlvV+SmQf7j8ycZvlBd WrBQSypGNsRgVAWDMbXpotWBfmgvQjL9RP0AjSvuYeiVd0Y79gjsslSasawcWwzPMbSPOo7NB/y vdJ+gk285RfkUv961N4EVyxjDHZDJDaI4Zgy8ctqbe6oqj3B6i6r5EguXtiJITTyli16BnSNOdq M4IqlwgOn3jSAyymPPcR4C1pfGSx2JIlFvzPSQeadkdawjANlEWZO592Jp4Vg7Xz9KCAstaYsfh asRo+d5zj9q6NsGm/TEKr0TNeodMjTskYaLv5DX1FQGwdTB6LUbR5AC1SPILgF+bywHZ X-Received: by 2002:a05:6a00:8510:b0:748:2cbb:be51 with SMTP id d2e1a72fcca58-7489cfde777mr12745488b3a.17.1750226106277; Tue, 17 Jun 2025 22:55:06 -0700 (PDT) Received: from localhost ([2804:14d:7e39:88d6:ae12:97ab:a9:43c]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-748900d7340sm10434935b3a.179.2025.06.17.22.55.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 17 Jun 2025 22:55:05 -0700 (PDT) From: Thiago Jung Bauermann To: gdb-patches@sourceware.org Cc: Eli Zaretskii , "Schimpe, Christina" , Luis Machado Subject: [PATCH v2 6/9] GDB: aarch64-linux: GCS support in Linux signals Date: Wed, 18 Jun 2025 02:54:42 -0300 Message-ID: <20250618055445.709416-7-thiago.bauermann@linaro.org> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250618055445.709416-1-thiago.bauermann@linaro.org> References: <20250618055445.709416-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/linux-tdep.h | 4 + gdb/testsuite/gdb.arch/aarch64-gcs-core.exp | 6 +- gdb/testsuite/gdb.arch/aarch64-gcs.exp | 7 +- 4 files changed, 88 insertions(+), 12 deletions(-) Changes since v1: - Adopt Luis' suggested wording for warning about error reading GCSPR from the signal frame context. - Use aarch64_gdbarch_tdep::has_gcs_linux in aarch64_linux_sigframe_init instead of has_gcs. - Moved SEGV_CPERR definition from gdb/arch/aarch64-gcs-linux.h to gdb/linux-tdep.h (suggested by Christina). - Moved documentation changes to its own patch. - Add the testcase changes related to the code in this patch. diff --git a/gdb/aarch64-linux-tdep.c b/gdb/aarch64-linux-tdep.c index ee3f7bfd9928..6bd9dfc47d81 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 GCS pointer 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_linux ()) + { + /* 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")); @@ -2841,9 +2906,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 @@ -2860,6 +2922,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/linux-tdep.h b/gdb/linux-tdep.h index 7083635b976c..0bee4b34845d 100644 --- a/gdb/linux-tdep.h +++ b/gdb/linux-tdep.h @@ -26,6 +26,10 @@ struct inferior; struct regcache; +#ifndef SEGV_CPERR +#define SEGV_CPERR 10 /* Control protection error. */ +#endif + /* Enum used to define the extra fields of the siginfo type used by an architecture. */ enum linux_siginfo_extra_field_values diff --git a/gdb/testsuite/gdb.arch/aarch64-gcs-core.exp b/gdb/testsuite/gdb.arch/aarch64-gcs-core.exp index ef7e507407a4..581abf2e06e9 100644 --- a/gdb/testsuite/gdb.arch/aarch64-gcs-core.exp +++ b/gdb/testsuite/gdb.arch/aarch64-gcs-core.exp @@ -37,7 +37,8 @@ gdb_test "continue" \ [multi_line \ "Continuing\\." \ "($hex\r\n)?" \ - "Program received signal SIGSEGV, Segmentation fault\\." \ + "Program received signal SIGSEGV, Segmentation fault" \ + "Guarded Control Stack error\\." \ "function \\(gcspr=$hex\\) at .*aarch64-gcs-core.c:$decimal" \ {.*__asm__ volatile \("ret\\n"\);}] \ "continue to SIGSEGV" @@ -67,7 +68,8 @@ proc check_core_file {core_filename saved_gcspr} { if [gdb_test "core $core_filename" \ [multi_line \ "Core was generated by .*\\." \ - "Program terminated with signal SIGSEGV, Segmentation fault\\." \ + "Program terminated with signal SIGSEGV, Segmentation fault" \ + "Guarded Control Stack error\\." \ "#0 function \\(gcspr=$hex\\) at .*aarch64-gcs-core.c:$decimal" \ "$decimal.*__asm__ volatile \\(\"ret\\\\n\"\\);"] \ "load core file"] { diff --git a/gdb/testsuite/gdb.arch/aarch64-gcs.exp b/gdb/testsuite/gdb.arch/aarch64-gcs.exp index ea70cc6ef2e9..907dcb43e78b 100644 --- a/gdb/testsuite/gdb.arch/aarch64-gcs.exp +++ b/gdb/testsuite/gdb.arch/aarch64-gcs.exp @@ -51,17 +51,22 @@ gdb_test "continue" \ ".*\r\nBreakpoint \[0-9\]+, handler \\(sig=10\\) at .*aarch64-gcs.c.*handler_gcspr = get_gcspr \\(\\);" \ "continue to signal handler" +gdb_test_no_output "set \$gcspr_in_handler = \$gcspr" \ + "save gcspr value in handler for later" # Select the frame above the frame, which makes GDB # unwind the gcspr from the signal frame GCS context. gdb_test "frame 2" "#2 ($hex in )?\\S+ \\(.*\\) (at|from) \\S+.*" \ "reached frame 2" gdb_test "print \$gcspr" ". = \\(void \\*\\) $hex" "gcspr in frame level 2" +gdb_test "print \$gcspr == \$gcspr_in_handler + 8" ". = 1" \ + "gcspr unwound from signal context is correct" gdb_test "continue" \ [multi_line \ "Continuing\\." \ "" \ - "Program received signal SIGSEGV, Segmentation fault\\." \ + "Program received signal SIGSEGV, Segmentation fault" \ + "Guarded Control Stack error\\." \ "normal_function2 \\(\\) at .*aarch64-gcs.c:$decimal" \ "${decimal}\\s+__asm__ volatile \\(\"ret\\\\n\"\\);"] \ "continue to SIGSEGV"