From patchwork Wed May 8 16:46:37 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Burgess X-Patchwork-Id: 89795 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 3F88E3845BE1 for ; Wed, 8 May 2024 16:48:42 +0000 (GMT) X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id B41283845BE1 for ; Wed, 8 May 2024 16:46:52 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org B41283845BE1 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org B41283845BE1 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1715186815; cv=none; b=jRs6IHEI9wBhhJQexWhxOBSkjo52B1VN8rH4H7cQs1LrBGWQKrct0JAPzJAhr+xzq1WunYyd8Ym5993kZFkbywjJsL7PcqJcOM1+Cg+8lWxoM4bsOd5WJLEMHJRV/jSyxymeFf8cTM014cgBcA+bFNhDcGPpx8f12qQIUXedufw= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1715186815; c=relaxed/simple; bh=5Y9PSUVforz4BLmMbTrrt8YKPD4as050r2cByA0tXIc=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=Pl2zdxcjS6uxmbftGKNG8guptkf9uBWQaVbYHCzZufQClgYYCvGnrVXZEgyxXc5N85rhv4VX5luk1GoPL73nagOynFjvUcfoME9u7PCI//FjFuZet2Hf4Htn108e2sVpt8e7jvpbPCqbzs5pJLr8lwvwjFvjuFjqJlimbFuaK28= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1715186812; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=hocNIUxR0fvxLrV49+tg7LKoOh/JA3kk9vmMDD5k8eo=; b=aBhR/AR8IBG1mIQ4sbSOfdSuHWG+CDAk1IlU6wKnkuYVYKzrXmwORyWuTwdxfr/lfn55u1 u3JQGLbOIcZIAd6f4Qntj5hP93kDpvsaLu8oB2z6MnIuzc1JJl5jXEyEbdFfdtzGbhfTqx fPYC3KNgihdAepnffNF2My9IzQI3pO4= Received: from mail-wr1-f71.google.com (mail-wr1-f71.google.com [209.85.221.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-173-SiRuONPeNUq7qXMP-HPi8g-1; Wed, 08 May 2024 12:46:50 -0400 X-MC-Unique: SiRuONPeNUq7qXMP-HPi8g-1 Received: by mail-wr1-f71.google.com with SMTP id ffacd0b85a97d-34cca3de393so1912074f8f.3 for ; Wed, 08 May 2024 09:46:50 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715186809; x=1715791609; 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=2lQ87tPQQIbOqF1OMYg2i/duuBDkXwFbOIKl0VMRBKY=; b=B8eUwScckUub2F1H8Xt4RUKWNxuAAGyiFQlvrIvo1tWA7lYrpv5nS/+FMiuWbZCbop e6kFIk0yXaf+bIKmXkLa+4xaDpRelvRG7niLUvR08Tx/TDw6ym/ry3RLGJaD9nsUQ1tZ FPX7T4k0G77UXiWatOETvm7XYnSv2y6pH7Ox8BAoD75H2PvDHmatZbSNnFQV1XzmfMB1 VSKO6c5eE0NAaqtC38HUFH0aAHsIFX0DHpPnvlzxmzlQyLWp6yeeZnttA1sbzK2JyIzT whunSJ3T7VbX/LnGei2VHw6W3iHlL07ESfi8oxz0pIcPET3auSo7pt7bRQr+RZWPW0Pb zb2A== X-Gm-Message-State: AOJu0YxvFxgQOkR6256cfHd5v5/y6Z4t/JKAbdR4IybawYsQmp6s4Jur C2WRn6QXCx8KAiFiIXbc7RghAgZMt3OdJc4e4XXoCPCOGAARuj0OM6AZtYYU1KkPt8fbbsfhbrm fRzs4O446KHFyiBH2/6+A1Jw13k9cBJAbkfWKPqXfpW6LNmQ3IeQ/bOV26SU8yWfF1hZZI52axm ePfqYCYEhRv+T6v9MX499RL09JpXnWYwl2uvGjI1rwNbo= X-Received: by 2002:adf:e787:0:b0:34c:c1c:8413 with SMTP id ffacd0b85a97d-34fca80d3dbmr2346192f8f.58.1715186808689; Wed, 08 May 2024 09:46:48 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGSha2BWnh2VHDkanQtaif5UgjT1D5Qxc0gjgUCUOA6uA+kvj0pklK1Uxv8qdpqK/XQ2SMJEg== X-Received: by 2002:adf:e787:0:b0:34c:c1c:8413 with SMTP id ffacd0b85a97d-34fca80d3dbmr2346174f8f.58.1715186808180; Wed, 08 May 2024 09:46:48 -0700 (PDT) Received: from localhost ([31.111.84.240]) by smtp.gmail.com with ESMTPSA id g18-20020a5d5412000000b00346bda84bf9sm15785101wrv.78.2024.05.08.09.46.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 May 2024 09:46:47 -0700 (PDT) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess , felix.willgerodt@intel.com, jhb@FreeBSD.org Subject: [PATCHv6 1/9] gdb/gdbserver: share I386_LINUX_XSAVE_XCR0_OFFSET definition Date: Wed, 8 May 2024 17:46:37 +0100 Message-Id: X-Mailer: git-send-email 2.25.4 In-Reply-To: References: MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.5 required=5.0 tests=BAYES_00, DKIM_INVALID, DKIM_SIGNED, GIT_PATCH_0, KAM_DMARC_NONE, KAM_DMARC_STATUS, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, 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+patchwork=sourceware.org@sourceware.org Share the definition of I386_LINUX_XSAVE_XCR0_OFFSET between GDB and gdbserver. This commit moves the definition into gdbsupport/x86-xstate.h, which allows the #define to be shared. There should be no user visible changes after this commit. Approved-By: Felix Willgerodt --- gdb/i386-linux-tdep.h | 20 -------------------- gdbserver/linux-x86-low.cc | 21 --------------------- gdbsupport/x86-xstate.h | 20 ++++++++++++++++++++ 3 files changed, 20 insertions(+), 41 deletions(-) diff --git a/gdb/i386-linux-tdep.h b/gdb/i386-linux-tdep.h index 5891747572b..07593c6a8ec 100644 --- a/gdb/i386-linux-tdep.h +++ b/gdb/i386-linux-tdep.h @@ -58,26 +58,6 @@ extern void i386_linux_report_signal_info (struct gdbarch *gdbarch, /* Return the target description according to XCR0. */ extern const struct target_desc *i386_linux_read_description (uint64_t xcr0); -/* Format of XSAVE extended state is: - struct - { - fxsave_bytes[0..463] - sw_usable_bytes[464..511] - xstate_hdr_bytes[512..575] - extended state regions (AVX, MPX, AVX512, PKRU, etc.) - }; - - Same memory layout will be used for the coredump NT_X86_XSTATE - representing the XSAVE extended state registers. - - The first 8 bytes of the sw_usable_bytes[464..467] is the OS enabled - extended state mask, which is the same as the extended control register - 0 (the XFEATURE_ENABLED_MASK register), XCR0. We can use this mask - together with the mask saved in the xstate_hdr_bytes to determine what - states the processor/OS supports and what state, used or initialized, - the process/thread is in. */ -#define I386_LINUX_XSAVE_XCR0_OFFSET 464 - extern int i386_linux_gregset_reg_offset[]; /* Return x86 siginfo type. */ diff --git a/gdbserver/linux-x86-low.cc b/gdbserver/linux-x86-low.cc index 2603fb2ac5d..6de44aeb2d9 100644 --- a/gdbserver/linux-x86-low.cc +++ b/gdbserver/linux-x86-low.cc @@ -831,27 +831,6 @@ x86_target::low_siginfo_fixup (siginfo_t *ptrace, gdb_byte *inf, int direction) static int use_xml; -/* Format of XSAVE extended state is: - struct - { - fxsave_bytes[0..463] - sw_usable_bytes[464..511] - xstate_hdr_bytes[512..575] - avx_bytes[576..831] - future_state etc - }; - - Same memory layout will be used for the coredump NT_X86_XSTATE - representing the XSAVE extended state registers. - - The first 8 bytes of the sw_usable_bytes[464..467] is the OS enabled - extended state mask, which is the same as the extended control register - 0 (the XFEATURE_ENABLED_MASK register), XCR0. We can use this mask - together with the mask saved in the xstate_hdr_bytes to determine what - states the processor/OS supports and what state, used or initialized, - the process/thread is in. */ -#define I386_LINUX_XSAVE_XCR0_OFFSET 464 - /* Does the current host support the GETFPXREGS request? The header file may or may not define it, and even if it is defined, the kernel will return EIO if it's running on a pre-SSE processor. */ diff --git a/gdbsupport/x86-xstate.h b/gdbsupport/x86-xstate.h index 89c1143fbe1..11b37544aa3 100644 --- a/gdbsupport/x86-xstate.h +++ b/gdbsupport/x86-xstate.h @@ -120,4 +120,24 @@ constexpr bool operator!= (const x86_xsave_layout &lhs, #define I387_MXCSR_INIT_VAL 0x1f80 +/* Format of XSAVE extended state is: + struct + { + fxsave_bytes[0..463] + sw_usable_bytes[464..511] + xstate_hdr_bytes[512..575] + extended state regions (AVX, MPX, AVX512, PKRU, etc.) + }; + + Same memory layout will be used for the coredump NT_X86_XSTATE + representing the XSAVE extended state registers. + + The first 8 bytes of the sw_usable_bytes[464..467] is the OS enabled + extended state mask, which is the same as the extended control register + 0 (the XFEATURE_ENABLED_MASK register), XCR0. We can use this mask + together with the mask saved in the xstate_hdr_bytes to determine what + states the processor/OS supports and what state, used or initialized, + the process/thread is in. */ +#define I386_LINUX_XSAVE_XCR0_OFFSET 464 + #endif /* COMMON_X86_XSTATE_H */ From patchwork Wed May 8 16:46:38 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Burgess X-Patchwork-Id: 89791 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 BE7BA3870C17 for ; Wed, 8 May 2024 16:47:47 +0000 (GMT) X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id ED51D3846043 for ; Wed, 8 May 2024 16:46:52 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org ED51D3846043 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org ED51D3846043 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1715186816; cv=none; b=MjtuQN+0zROy+uxuo6QAzwGuHcD3yuitPlRv+7xdiHWotCOF/48Do20jfIJC9bNiawZrxmKv/efsQpe1Z7LLr5QLVRB8aUw5vZSAWyNEpOozH5VEmcgIHeY5VxJXjXfatEZTxloBca3NFNrxtiy8EB6wHmyZeu48sJIZ+RpV/y8= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1715186816; c=relaxed/simple; bh=Yvst0bcnmtYFoAay+PFgd60W17CeqjrTARDMF5hgSzg=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=YkHZVq31rrmO6/26e9Fyne8+6rc9mdCl6QiCC0DnhZFMbwU+EjC8GaC2Arfp+l82Ez6CPwqIW7b6AbkZuo4uIa9cLA4+P3vUmhmwkTauxKCCMYfI8KSKViYhtUvNv//EruDWEZNx4SgoNyQSaFIZRlQ25NNsqyyNgjPMfPU3BIs= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1715186812; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=aPhdQkVUz+dr3FxKW6fZsG6q3nZxo7a/XW73wZ/ewvQ=; b=CbPXPLZm7pBTi6O+DrjE8ME2m8/qBEB//JFQfH9b4pzRGKyu2C50YTMz/Wbf//zNCsVoYq P5w1vFJEysViEP0whYvEYPfiOksgT+o67gaCQ/jKj3dQ5Lm9KYKX8isE4R4oOqyMbcsDFD SWwbmGa3z4Wt2UlB8UAVqX2rW0/gVvo= Received: from mail-wr1-f71.google.com (mail-wr1-f71.google.com [209.85.221.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-183-5xINmCrTMmCg4nROwQnLrA-1; Wed, 08 May 2024 12:46:51 -0400 X-MC-Unique: 5xINmCrTMmCg4nROwQnLrA-1 Received: by mail-wr1-f71.google.com with SMTP id ffacd0b85a97d-34da4921505so2673783f8f.3 for ; Wed, 08 May 2024 09:46:51 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715186810; x=1715791610; 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=aPhdQkVUz+dr3FxKW6fZsG6q3nZxo7a/XW73wZ/ewvQ=; b=oARrzOFwAAkm9bK8/+zJK4eSgWT+sM1c+qoN49NIkwPw7jPpKa4/FUn5i6yVkC9J5x 0C4w2w4EMr5mqCeF18LVOLxAggTzw/SQGSd8ECAJ7Og5RprfIDKPHkLtYQoxlq9RMXUG +F4kdCU+dgImHrrMiVm6BRf4S3FKxiCeENwYSVHTTr/notXB2huTDmsvzTQBijy9nY1z Nq/mI+oZ++pDo5upUpRPgsvjGh5LbkV70I01eB+anN0du13RdtDhIQhmLc9qgaQcHNjz cEfHAaCmIQMTUSL2CIwchJlh67Tx1uG+/SRatll4/fVW6TyWoKwuoHQ9IRIi4fJyLPJ6 f7+A== X-Gm-Message-State: AOJu0YycYINnAR9Oj2VfasOhNu8kvcNdrAe4Nvff1YC2gHdJ/noof/o9 st8zGRsd3LjZ1QegIFe1W3c+fB9OUKSNCvlXuWMJhMZzgbPfiSbDx9w6M1yz5hgI52s0wVa7l7Q 18oGIvAPCHBCOH3TVpzZ8eatyZwPAWK4/L/9FrYXwTCW0beTEOM2urubduVN9lS8QzzJLIXZOzS qKYoHZZ//hAKHs4/LwkOEcxXkAa47yqOLes4jyOfhir/c= X-Received: by 2002:a5d:4b82:0:b0:34b:44d7:f3eb with SMTP id ffacd0b85a97d-34fca433f79mr2904410f8f.3.1715186809780; Wed, 08 May 2024 09:46:49 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEnVFMFee1cuFpNZGyahEtAhDUS0y9kSiI6aHEE4ECOfqB6GMOMXnOu4KAVnBxlUWk8S9IQSg== X-Received: by 2002:a5d:4b82:0:b0:34b:44d7:f3eb with SMTP id ffacd0b85a97d-34fca433f79mr2904382f8f.3.1715186809153; Wed, 08 May 2024 09:46:49 -0700 (PDT) Received: from localhost ([31.111.84.240]) by smtp.gmail.com with ESMTPSA id l18-20020adffe92000000b00350141ea310sm245874wrr.90.2024.05.08.09.46.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 May 2024 09:46:48 -0700 (PDT) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess , felix.willgerodt@intel.com, jhb@FreeBSD.org Subject: [PATCHv6 2/9] gdbserver/x86: move no-xml code earlier in x86_linux_read_description Date: Wed, 8 May 2024 17:46:38 +0100 Message-Id: <090291eeebe4ea47f86ae85fedbb8d3e449805f8.1715185683.git.aburgess@redhat.com> X-Mailer: git-send-email 2.25.4 In-Reply-To: References: MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.0 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, 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+patchwork=sourceware.org@sourceware.org This commit is part of a series that aims to share more of the x86 target description reading/generation code between GDB and gdbserver. There are a huge number of similarities between the code in gdbserver's x86_linux_read_description function and GDB's x86_linux_nat_target::read_description function, and it is this similarity that I plan, in a later commit, to share between GDB and gdbserver. However, one thing that is different in x86_linux_read_description is the code inside the '!use_xml' block. This is the code that handles the case where gdbserver is not allowed to send an XML target description back to GDB. In this case gdbserver uses some predefined, fixed, target descriptions. First, it's worth noting that I suspect this code is not tested any more. I couldn't find anything in the testsuite that tries to disable XML target description support. And the idea of having a single "fixed" target description really doesn't work well when we think about all the various x86 extensions that exist. Part of me would like to rip out the no-xml support in gdbserver (at least for x86), and if a GDB connects that doesn't support XML target descriptions, gdbserver can just give an error and drop the connection. GDB has supported XML target descriptions for 16 years now, I think it would be reasonable for our shipped gdbserver to drop support for the old way of doing things. Anyway.... this commit doesn't do that. What I did notice was that, over time, the '!use_xml' block appears to have "drifted" within the x86_linux_read_description function; it's now not the first check we do. Instead we make some ptrace calls and return a target description generated based on the result of these ptrace calls. Surely it only makes sense to generate variable target descriptions if we can send these back to GDB? So in this commit I propose to move the '!use_xml' block earlier in the x86_linux_read_description function. The benefit of this is that this leaves the later half of x86_linux_read_description much more similar to the GDB function x86_linux_nat_target::read_description and sets us up for potentially sharing code between GDB and gdbserver in a later commit. Approved-By: John Baldwin Approved-By: Felix Willgerodt --- gdbserver/linux-x86-low.cc | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/gdbserver/linux-x86-low.cc b/gdbserver/linux-x86-low.cc index 6de44aeb2d9..e8ef3667eb4 100644 --- a/gdbserver/linux-x86-low.cc +++ b/gdbserver/linux-x86-low.cc @@ -869,6 +869,22 @@ x86_linux_read_description (void) #endif } + /* If we are not allowed to send an XML target description then we need + to use the hard-wired target descriptions. This corresponds to GDB's + default machine for x86. + + This check needs to occur before any returns statements that might + generate some alternative target descriptions. */ + if (!use_xml) + { +#ifdef __x86_64__ + if (machine == EM_X86_64) + return tdesc_amd64_linux_no_xml.get (); + else +#endif + return tdesc_i386_linux_no_xml.get (); + } + #if !defined __x86_64__ && defined HAVE_PTRACE_GETFPXREGS if (machine == EM_386 && have_ptrace_getfpxregs == -1) { @@ -885,17 +901,6 @@ x86_linux_read_description (void) } #endif - if (!use_xml) - { - /* Don't use XML. */ -#ifdef __x86_64__ - if (machine == EM_X86_64) - return tdesc_amd64_linux_no_xml.get (); - else -#endif - return tdesc_i386_linux_no_xml.get (); - } - if (have_ptrace_getregset == TRIBOOL_UNKNOWN) { uint64_t xstateregs[(X86_XSTATE_SSE_SIZE / sizeof (uint64_t))]; From patchwork Wed May 8 16:46:39 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Burgess X-Patchwork-Id: 89792 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 330703884575 for ; Wed, 8 May 2024 16:47:49 +0000 (GMT) X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 1DC673849AC8 for ; Wed, 8 May 2024 16:46:55 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 1DC673849AC8 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 1DC673849AC8 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1715186821; cv=none; b=gIGoMPud5rmIojsXh0lLV0RBvOwSxT8QbY9Xrtde8EMFwSr8vH8xmx+5K/OF+wrYSUIJPRseVRvgYA/aP/dGSURH0vPLb8QkqvlvVRsEEYuYoX7zUbmpIpiENMfy9AKOQWxHaOkrw3min6PLfTbb/dhbWIe4CNO+Ee8ZxJRGbOI= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1715186821; c=relaxed/simple; bh=a+qwqSX4FMxkjOzasN2GMuO1h/74ybrHSsGL/NEpaiw=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=o6sCltGenzZTs+YL6wfOiEHSz1qhN3f440m9OG0XxkoxpIAMnA2t4yEeMQkPZmVC9ulev+borL2ieOWxbybIT+ttTncvxcLaMrBody+tNGvRJqPalpn4p48s6QBxi6RtAt1+BuyzpcTDxrNgN8AhWHfx8dTonWFSNFduxOfTIhU= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1715186814; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=7AGyzN7u3UjZVQM4OmdGkgyIj3Yn7Gr0RFwDUu+Q/40=; b=dI5825J4aJGkVH+iBUA8D2ECYFJLmB9GdD6EoF5iDNeSfJhwCorpUFUfmV0GmhY6Ax/XGu Ughp+9Kft9eNS9FYyTo4dz+lEOvwLy9BNDRQeyf63PXN3WMRISiMljgUXYF3Y8maxOHnK8 rTyEN+Lvvcy+Plevh30JzFoRISHFqts= Received: from mail-wr1-f71.google.com (mail-wr1-f71.google.com [209.85.221.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-172-anqr64oQMYWpucX_-L0agw-1; Wed, 08 May 2024 12:46:53 -0400 X-MC-Unique: anqr64oQMYWpucX_-L0agw-1 Received: by mail-wr1-f71.google.com with SMTP id ffacd0b85a97d-34d9b4d3a4fso2651114f8f.1 for ; Wed, 08 May 2024 09:46:53 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715186812; x=1715791612; 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=hJzgxLGHtv31az+AlwvWJlHvMa4zPRAIlUCcOb5XP8Q=; b=lpSBe8tNwncPGQ3j+vAYGaJv+kOBw9A8FWmhbggfN6uWozUOd5MrRO4I3ouuKCWg+F LMWYS1iCHY9TRuh1oqpvzwxnoL1T3qsf1HUyu1iFAuGtEykXPwtrZHccrg5EC8lFoOcz 5VJy37qpmXTAngluDc8Ogm8WUJifv/LrGRLePiPkPSz1/pvqsIb/vAWY5RDBLo7BFWxA ugofessYK0ETCr/GQ1LOwE4MyiCnF3pYDHuHv2Oaz8L1dZIGHtyWr3CbErBEvmZ2MG/9 K7Vs6CqzDEwpWzfJZi2tSUosgATcN31450+bxSFShR7EGZXpbHsTjU313W/Rx44qpQ+h PyWw== X-Gm-Message-State: AOJu0Yxx6UX4zqE97tK4rRUVMhCXHLzEiXAmSoojF+uyRt5ngbSdnbBp oGhnjsdUAXvXaY6C9eCqGLJ03OVreIp/BTbyWwP3OwZWzFdaeSOBzQMwDrHZeIXW3LL2fCIGj0O k3kesoZpPe09wku+6rfhOhw9MYXAe9nTF3gmQc7IzZ9l2ZLnFEgItrqgCOvbREzeaw0x+1GGOTc R84z4bCUHq3akm5KqNSrBOklO9GXovfTjz/8Hq4kNPt2M= X-Received: by 2002:adf:e612:0:b0:34f:9b4:43d0 with SMTP id ffacd0b85a97d-34fcade2dffmr2536881f8f.26.1715186811416; Wed, 08 May 2024 09:46:51 -0700 (PDT) X-Google-Smtp-Source: AGHT+IF7aFOYOZGrAzZy306I11QECN77aK0V6RA99g8Bo+8Sb3U9HLK8JNX3Zq8rBxrywhXIH8oHzg== X-Received: by 2002:adf:e612:0:b0:34f:9b4:43d0 with SMTP id ffacd0b85a97d-34fcade2dffmr2536860f8f.26.1715186810829; Wed, 08 May 2024 09:46:50 -0700 (PDT) Received: from localhost ([31.111.84.240]) by smtp.gmail.com with ESMTPSA id c16-20020adffb10000000b0034f0633e322sm8652857wrr.38.2024.05.08.09.46.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 May 2024 09:46:49 -0700 (PDT) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess , felix.willgerodt@intel.com, jhb@FreeBSD.org Subject: [PATCHv6 3/9] gdb/x86: move have_ptrace_getfpxregs global into gdb/nat directory Date: Wed, 8 May 2024 17:46:39 +0100 Message-Id: <9efce46803ede508ac0fd0cf3f9d1497117e3aae.1715185683.git.aburgess@redhat.com> X-Mailer: git-send-email 2.25.4 In-Reply-To: References: MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.5 required=5.0 tests=BAYES_00, DKIM_INVALID, DKIM_SIGNED, GIT_PATCH_0, KAM_DMARC_NONE, KAM_DMARC_STATUS, KAM_SHORT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, 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+patchwork=sourceware.org@sourceware.org The have_ptrace_getfpxregs global tracks whether GDB or gdbserver is running on a kernel that supports the GETFPXREGS ptrace request. Currently this global is declared twice (once in GDB and once in gdbserver), but it makes sense to move this global into the nat/ directory, and have a single declaration and definition. While moving this variable I have converted it to a tribool, as that was what it really was, if even used the same numbering as the tribool enum (-1, 0, 1). Where have_ptrace_getfpxregs was used I have updated in the obvious way. However, while making this change I noticed what I think is a bug in x86_linux_nat_target::read_description and x86_linux_read_description, both of these functions can be called multiple times, but in both cases we only end up calling i386_linux_read_description the first time through in the event that PTRACE_GETFPXREGS is not supported. This is because initially have_ptrace_getfpxregs will be TRIBOOL_UNKNOWN, but after the ptrace call fails we set have_ptrace_getfpxregs to TRIBOOL_FALSE. The next time we attempt to read the target description we'll skip the ptrace call, and so skip the call to i386_linux_read_description. I've not tried to address this preexisting bug in this commit, this is purely a refactor, there should be no user visible changes after this commit. In later commits I'll merge the gdbserver and GDB code together into the nat/ directory, and after that I'll try to address this bug. Reviewed-By: Felix Willgerodt --- gdb/Makefile.in | 1 - gdb/i386-linux-nat.c | 26 +++++--------------------- gdb/i386-linux-nat.h | 26 -------------------------- gdb/nat/x86-linux.c | 9 +++++++++ gdb/nat/x86-linux.h | 14 ++++++++++++++ gdb/x86-linux-nat.c | 7 ++----- gdbserver/linux-x86-low.cc | 17 +++-------------- 7 files changed, 33 insertions(+), 67 deletions(-) delete mode 100644 gdb/i386-linux-nat.h diff --git a/gdb/Makefile.in b/gdb/Makefile.in index 234d0051f49..d1c9c2f2859 100644 --- a/gdb/Makefile.in +++ b/gdb/Makefile.in @@ -1397,7 +1397,6 @@ HFILES_NO_SRCDIR = \ hppa-tdep.h \ i386-bsd-nat.h \ i386-darwin-tdep.h \ - i386-linux-nat.h \ i386-linux-tdep.h \ i386-tdep.h \ i387-tdep.h \ diff --git a/gdb/i386-linux-nat.c b/gdb/i386-linux-nat.c index 7278dd91ff0..c54c9b8f9c1 100644 --- a/gdb/i386-linux-nat.c +++ b/gdb/i386-linux-nat.c @@ -26,7 +26,7 @@ #include "gregset.h" #include "gdb_proc_service.h" -#include "i386-linux-nat.h" +#include "nat/x86-linux.h" #include "i387-tdep.h" #include "i386-tdep.h" #include "i386-linux-tdep.h" @@ -80,22 +80,6 @@ int have_ptrace_getregs = 0 #endif ; - -/* Does the current host support the GETFPXREGS request? The header - file may or may not define it, and even if it is defined, the - kernel will return EIO if it's running on a pre-SSE processor. - - My instinct is to attach this to some architecture- or - target-specific data structure, but really, a particular GDB - process can only run on top of one kernel at a time. So it's okay - for this to be a simple variable. */ -int have_ptrace_getfpxregs = -#ifdef HAVE_PTRACE_GETFPXREGS - -1 -#else - 0 -#endif -; /* Accessing registers through the U area, one at a time. */ @@ -388,14 +372,14 @@ fetch_fpxregs (struct regcache *regcache, int tid) { elf_fpxregset_t fpxregs; - if (! have_ptrace_getfpxregs) + if (have_ptrace_getfpxregs == TRIBOOL_FALSE) return 0; if (ptrace (PTRACE_GETFPXREGS, tid, 0, (int) &fpxregs) < 0) { if (errno == EIO) { - have_ptrace_getfpxregs = 0; + have_ptrace_getfpxregs = TRIBOOL_FALSE; return 0; } @@ -415,14 +399,14 @@ store_fpxregs (const struct regcache *regcache, int tid, int regno) { elf_fpxregset_t fpxregs; - if (! have_ptrace_getfpxregs) + if (have_ptrace_getfpxregs == TRIBOOL_FALSE) return 0; if (ptrace (PTRACE_GETFPXREGS, tid, 0, &fpxregs) == -1) { if (errno == EIO) { - have_ptrace_getfpxregs = 0; + have_ptrace_getfpxregs = TRIBOOL_FALSE; return 0; } diff --git a/gdb/i386-linux-nat.h b/gdb/i386-linux-nat.h deleted file mode 100644 index c3d32aff1be..00000000000 --- a/gdb/i386-linux-nat.h +++ /dev/null @@ -1,26 +0,0 @@ -/* Native-dependent code for GNU/Linux i386. - - Copyright (C) 1999-2024 Free Software Foundation, Inc. - - This file is part of GDB. - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . */ - -#ifndef I386_LINUX_NAT_H -#define I386_LINUX_NAT_H 1 - -/* Does the current host support the GETFPXREGS request? */ -extern int have_ptrace_getfpxregs; - -#endif diff --git a/gdb/nat/x86-linux.c b/gdb/nat/x86-linux.c index 7a21c8f2c26..f394dc5d5e1 100644 --- a/gdb/nat/x86-linux.c +++ b/gdb/nat/x86-linux.c @@ -20,6 +20,15 @@ #include "x86-linux.h" #include "x86-linux-dregs.h" +/* See nat/x86-linux.h. */ +tribool have_ptrace_getfpxregs = +#ifdef HAVE_PTRACE_GETFPXREGS + TRIBOOL_UNKNOWN +#else + TRIBOOL_FALSE +#endif +; + /* Per-thread arch-specific data we want to keep. */ struct arch_lwp_info diff --git a/gdb/nat/x86-linux.h b/gdb/nat/x86-linux.h index 822882173f9..936e20154a0 100644 --- a/gdb/nat/x86-linux.h +++ b/gdb/nat/x86-linux.h @@ -22,6 +22,20 @@ #include "nat/linux-nat.h" +/* Does the current host support the GETFPXREGS request? The system header + file may or may not define it, but even if it is defined, the kernel + will return EIO if it's running on a pre-SSE processor. + + Initially this will be TRIBOOL_UNKNOWN and should be changed to + TRIBOOL_FALSE if the ptrace call is attempted and fails or changed to + TRIBOOL_TRUE if the ptrace call is attempted and succeeds. + + My instinct is to attach this to some architecture- or target-specific + data structure, but really, a particular GDB process can only run on top + of one kernel at a time. So it's okay - for this to be a global + variable. */ +extern tribool have_ptrace_getfpxregs; + /* Set whether our local mirror of LWP's debug registers has been changed since the values were last written to the thread. Nonzero indicates that a change has been made, zero indicates no change. */ diff --git a/gdb/x86-linux-nat.c b/gdb/x86-linux-nat.c index a3d8ffb60f1..b42884ad57f 100644 --- a/gdb/x86-linux-nat.c +++ b/gdb/x86-linux-nat.c @@ -26,9 +26,6 @@ #include #include "x86-nat.h" -#ifndef __x86_64__ -#include "i386-linux-nat.h" -#endif #include "x86-linux-nat.h" #include "i386-linux-tdep.h" #ifdef __x86_64__ @@ -147,13 +144,13 @@ x86_linux_nat_target::read_description () error (_("Can't debug 64-bit process with 32-bit GDB")); } #elif HAVE_PTRACE_GETFPXREGS - if (have_ptrace_getfpxregs == -1) + if (have_ptrace_getfpxregs == TRIBOOL_UNKNOWN) { elf_fpxregset_t fpxregs; if (ptrace (PTRACE_GETFPXREGS, tid, 0, (int) &fpxregs) < 0) { - have_ptrace_getfpxregs = 0; + have_ptrace_getfpxregs = TRIBOOL_FALSE; have_ptrace_getregset = TRIBOOL_FALSE; return i386_linux_read_description (X86_XSTATE_X87_MASK); } diff --git a/gdbserver/linux-x86-low.cc b/gdbserver/linux-x86-low.cc index e8ef3667eb4..7937055c060 100644 --- a/gdbserver/linux-x86-low.cc +++ b/gdbserver/linux-x86-low.cc @@ -831,17 +831,6 @@ x86_target::low_siginfo_fixup (siginfo_t *ptrace, gdb_byte *inf, int direction) static int use_xml; -/* Does the current host support the GETFPXREGS request? The header - file may or may not define it, and even if it is defined, the - kernel will return EIO if it's running on a pre-SSE processor. */ -int have_ptrace_getfpxregs = -#ifdef HAVE_PTRACE_GETFPXREGS - -1 -#else - 0 -#endif -; - /* Get Linux/x86 target description from running target. */ static const struct target_desc * @@ -886,18 +875,18 @@ x86_linux_read_description (void) } #if !defined __x86_64__ && defined HAVE_PTRACE_GETFPXREGS - if (machine == EM_386 && have_ptrace_getfpxregs == -1) + if (machine == EM_386 && have_ptrace_getfpxregs == TRIBOOL_UNKNOWN) { elf_fpxregset_t fpxregs; if (ptrace (PTRACE_GETFPXREGS, tid, 0, (long) &fpxregs) < 0) { - have_ptrace_getfpxregs = 0; + have_ptrace_getfpxregs = TRIBOOL_FALSE; have_ptrace_getregset = TRIBOOL_FALSE; return i386_linux_read_description (X86_XSTATE_X87); } else - have_ptrace_getfpxregs = 1; + have_ptrace_getfpxregs = TRIBOOL_TRUE; } #endif From patchwork Wed May 8 16:46:40 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Burgess X-Patchwork-Id: 89797 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 73DD83883006 for ; Wed, 8 May 2024 16:49:13 +0000 (GMT) X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id B975C384AB64 for ; Wed, 8 May 2024 16:46:55 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org B975C384AB64 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org B975C384AB64 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1715186821; cv=none; b=vZ6wbWIYV2K+oRcB0T+B6M07mW3MYTLNTGddyq9ogqziV2tRPku+/GPeWhiILIeqtfDJ8+sOCzGRDQoDk3F74xCB4ydCoB68RwUl0vaof+PrQs24u20SfSGdPmF8SEtAbhuiFffUSvR52LebkHIj5wPEYLzolOAHWcO7/MqHUYE= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1715186821; c=relaxed/simple; bh=Cc9RY17pLyzo1U/P1qA1KCLDIjwMfNvEKJJzNgnV3Ks=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=dbIn2vTmNqLLDp3w4s9tW5q2f08HmITx4IulEYKLo/YqZv+kUDnJFwLpr3kFhsu4tzwRO4f+Y9riavC2o4QcqVGchmbj9HEXzbdSGnp5fkojvCtbp0RmUcHGVS9zIkOqyFBVhOA2LcLzLdHpre4lZlpg9TCRwe+dMe+7NaB6uU4= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1715186815; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=XBhV9X8GKy7XGYDixSkd0KphFA109YWEI0EQGs7UQbI=; b=jA+tJ/8gDR3iDwtF+Ki1gVFgZHubtPTn0173t8Uyfcumza2zF36rqBmK2vNkjXut4AXpMs 0iOBLH/5P4BADY+/0zv/gGgPBvcvw7jILPknFje4UEB3iwkwZ6HVKEnngJ4Iyhv5YmPeHj Gq4XRTDH63jPq4ksyf77SAd2GGOeyKI= Received: from mail-wr1-f70.google.com (mail-wr1-f70.google.com [209.85.221.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-441-jksxdXYtOiK7muU-53RvYA-1; Wed, 08 May 2024 12:46:54 -0400 X-MC-Unique: jksxdXYtOiK7muU-53RvYA-1 Received: by mail-wr1-f70.google.com with SMTP id ffacd0b85a97d-34da03e19beso3287834f8f.1 for ; Wed, 08 May 2024 09:46:53 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715186812; x=1715791612; 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=XBhV9X8GKy7XGYDixSkd0KphFA109YWEI0EQGs7UQbI=; b=hN5iJumNGOhFWeFgIdTbwXfjV+0OUNjD9xR1ROCDSWRQQ93feaVrvVRn0lrba1JrzY qOJGenXtVmSAHJhLwbyg7dWMEgdXrsPYlxa1BurllpJ1rcnWbc/26UHqhJZQPiJe8+6/ ZKVqMJrL3o+E4sxcdsDamzdqMjkPoR7+EGYp760gBxjzzzrOu2JMW/nrtiB5o+yVezE+ 3VPeEGoP48TfpTk8iYa8Stlzs5EK3964VeUbZMn1BPQ3cL6KSO/HYGLyaSShpSP4SlNH ZkuOglWiJhENRkyXRNLTLFljx1xzID3NcmqzRm/qivmYRkHZKTkC5UBIlgnygNFnO68q TpvA== X-Gm-Message-State: AOJu0YwbvXw8s/E5sURi1LB02atUTxzYuvWFyOKnTrrrPtooHkqxNGMh AJDFlN6Qk55wR3GPyuQx1QVsXwISHGudjvKSr/aTO8Yq/Z1dLBadgzLcuB7eCndeR+uKRR2jnfV fO8zWqle25/OPAI3ci2Jdr5iRny/yUhEZmUxB/wPHPzlphZG1Ls8MUM3mv5nxoE6jzvsV4B7OcW CLJOdfixc13bWLTYtWMlzHbBk9jF4seDuSIy2r5tDdgPA= X-Received: by 2002:adf:fa8a:0:b0:34c:ab55:bf1 with SMTP id ffacd0b85a97d-34fca05485dmr2393455f8f.2.1715186812438; Wed, 08 May 2024 09:46:52 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFVh+B+nW8D/bgVkbEsiJqeOm+hhENrytHp7jfQd+aH1Oo3eNykNPxZS1LY8ysRYJCRSNh0dw== X-Received: by 2002:adf:fa8a:0:b0:34c:ab55:bf1 with SMTP id ffacd0b85a97d-34fca05485dmr2393435f8f.2.1715186811928; Wed, 08 May 2024 09:46:51 -0700 (PDT) Received: from localhost ([31.111.84.240]) by smtp.gmail.com with ESMTPSA id h18-20020a056000001200b0034c78001f6asm15731056wrx.109.2024.05.08.09.46.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 May 2024 09:46:51 -0700 (PDT) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess , felix.willgerodt@intel.com, jhb@FreeBSD.org Subject: [PATCHv6 4/9] gdb/x86: move have_ptrace_getregset global into gdb/nat directory Date: Wed, 8 May 2024 17:46:40 +0100 Message-Id: X-Mailer: git-send-email 2.25.4 In-Reply-To: References: MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.0 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, 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+patchwork=sourceware.org@sourceware.org Just like the previous commit, this commit moves have_ptrace_getregset into the nat/ directory. This global is currently declared and defined in both GDB and gdbserver, but this can be merged and shared. There should be no user visible changes after this commit. --- gdb/linux-nat.c | 3 --- gdb/linux-nat.h | 3 --- gdb/nat/x86-linux.c | 3 +++ gdb/nat/x86-linux.h | 3 +++ gdbserver/linux-low.cc | 3 --- gdbserver/linux-low.h | 2 -- 6 files changed, 6 insertions(+), 11 deletions(-) diff --git a/gdb/linux-nat.c b/gdb/linux-nat.c index 48ecd3627ca..38d3af56f39 100644 --- a/gdb/linux-nat.c +++ b/gdb/linux-nat.c @@ -243,9 +243,6 @@ PTRACE_PEEKTEXT/PTRACE_POKETEXT or process_vm_readv/process_vm_writev: struct linux_nat_target *linux_target; -/* Does the current host support PTRACE_GETREGSET? */ -enum tribool have_ptrace_getregset = TRIBOOL_UNKNOWN; - /* When true, print debug messages relating to the linux native target. */ static bool debug_linux_nat; diff --git a/gdb/linux-nat.h b/gdb/linux-nat.h index 4dcbe9e170a..fd393df5020 100644 --- a/gdb/linux-nat.h +++ b/gdb/linux-nat.h @@ -299,9 +299,6 @@ lwp_info_range all_lwps (); lwp_info_safe_range all_lwps_safe (); -/* Does the current host support PTRACE_GETREGSET? */ -extern enum tribool have_ptrace_getregset; - /* Called from the LWP layer to inform the thread_db layer that PARENT spawned CHILD. Both LWPs are currently stopped. This function does whatever is required to have the child LWP under the diff --git a/gdb/nat/x86-linux.c b/gdb/nat/x86-linux.c index f394dc5d5e1..ed1d06bd623 100644 --- a/gdb/nat/x86-linux.c +++ b/gdb/nat/x86-linux.c @@ -29,6 +29,9 @@ tribool have_ptrace_getfpxregs = #endif ; +/* See nat/x86-linux.h. */ +tribool have_ptrace_getregset = TRIBOOL_UNKNOWN; + /* Per-thread arch-specific data we want to keep. */ struct arch_lwp_info diff --git a/gdb/nat/x86-linux.h b/gdb/nat/x86-linux.h index 936e20154a0..cb5f44f4b49 100644 --- a/gdb/nat/x86-linux.h +++ b/gdb/nat/x86-linux.h @@ -36,6 +36,9 @@ variable. */ extern tribool have_ptrace_getfpxregs; +/* Does the current host support PTRACE_GETREGSET? */ +extern tribool have_ptrace_getregset; + /* Set whether our local mirror of LWP's debug registers has been changed since the values were last written to the thread. Nonzero indicates that a change has been made, zero indicates no change. */ diff --git a/gdbserver/linux-low.cc b/gdbserver/linux-low.cc index ac7f9807ecc..4ba56cd34ff 100644 --- a/gdbserver/linux-low.cc +++ b/gdbserver/linux-low.cc @@ -133,9 +133,6 @@ typedef struct } Elf64_auxv_t; #endif -/* Does the current host support PTRACE_GETREGSET? */ -enum tribool have_ptrace_getregset = TRIBOOL_UNKNOWN; - /* Return TRUE if THREAD is the leader thread of the process. */ static bool diff --git a/gdbserver/linux-low.h b/gdbserver/linux-low.h index eaf87527338..273c04626b8 100644 --- a/gdbserver/linux-low.h +++ b/gdbserver/linux-low.h @@ -951,8 +951,6 @@ void thread_db_notice_clone (struct thread_info *parent_thr, ptid_t child_ptid); bool thread_db_thread_handle (ptid_t ptid, gdb_byte **handle, int *handle_len); -extern enum tribool have_ptrace_getregset; - /* Search for the value with type MATCH in the auxv vector, with entries of length WORDSIZE bytes, of process with pid PID. If found, store the value in *VALP and return 1. If not found or if there is an error, From patchwork Wed May 8 16:46:41 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Burgess X-Patchwork-Id: 89793 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 21908388300C for ; Wed, 8 May 2024 16:48:03 +0000 (GMT) X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id D85E4387088C for ; Wed, 8 May 2024 16:46:56 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D85E4387088C Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org D85E4387088C Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1715186821; cv=none; b=jy5yI1PjWBRrH8jwvWyxK4w0tZ4Mp0ang6V+0aixuTvoNQ3ydUQ0mpXm4PPS1uFH4pqatjz6eehpWsm9E32aqb/L+lLSsMozKiwpPa1MJ2dAtclfCG0wBWpbvSoQkGhMPBJmCcZnM7TMJYdjl1sksGwjrEIGYRv46eCYDXg6YEo= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1715186821; c=relaxed/simple; bh=aekI+ZyY0Iih3Hruqd8K01lpSO1nJiy9lcezDkSRm3o=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=RMdp6mTXAkM2ISpY1+Fco4xq7Ifay4TJBgF2UyxZPvstMal8NY3Uj2JubopDZuP8nd0Nf405s9fHsWZQ8zqxJQ7wTyTGQgnWOoJ78v9wNhDH2BJOzwNdRM6uUbWdlWbHpJrodAaaETRt/jyPJplHGmAmxkBHvDcvf/2upjTHKk8= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1715186816; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=4+R3nbdzCnPabaBzt5qri338sh3vC6nAjMFAbER7sZo=; b=da96BdFv5IBMjqPOP8nb7qzRl7fLxk50it8wNJ2MJuO4hZ/TrSAlxrMea2EtHh054l/Ijf VzsaChZFYOrf5kk1S/zoYR2cQlanlKpgB7Bzj6ysI6CK2LA9fD2YZD+2MGhYQ7iilfS75O nKVdSw0SdxJlBqnY//V43Q7SKzbBi6Y= Received: from mail-wr1-f71.google.com (mail-wr1-f71.google.com [209.85.221.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-501-ToIhlZKMMyGzSpioPjr6Gw-1; Wed, 08 May 2024 12:46:55 -0400 X-MC-Unique: ToIhlZKMMyGzSpioPjr6Gw-1 Received: by mail-wr1-f71.google.com with SMTP id ffacd0b85a97d-34dc66313b3so3012836f8f.2 for ; Wed, 08 May 2024 09:46:55 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715186814; x=1715791614; 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=4+R3nbdzCnPabaBzt5qri338sh3vC6nAjMFAbER7sZo=; b=bpgPQ6mZrGFFRpP8TPFkINFIR0AA53trUB3hYVYLnVuiVlWuHGKjBjQo7NbeZB1+SX TTV8F1TEB1t/OgNdC2HkGKKfx0PoFv2nZJStc/5FSAm8ngN76jpDn78NzUo41XBSaaL1 s/2HX/2O3AvOpp4kgD+f4YbP0sbmYjT7CyCa77mRH+2Tj1WLtsz/YMhurPjaZIhpIrGJ +L4UzN0nT4DhH0+svsp8+nA909nHBWBYZkCSu4xAgk+9ztJPlJizqSwmlEYqw29UMrjK rXCOmiStsU4RCDFdu8GOEOuc+t3DeMBf2maRsAiznZEcMZgHXCOv2Ubpvqortq1ajH/i 7nsA== X-Gm-Message-State: AOJu0YwtgPwXggHnz/yMgO5E4js56FJUBySDPPYnCjoY0zL9G3mwzDFg XGbxlGNYCYhY8H4PwY7L2Q/P2rI/+6Ood+JfNGiJhMMKGQg1CrOIUn5XZVwC8gXum7jbhy7sJF/ ZwbRetRTrJUlfPTl/E6jVWzg5matU66OORlTKMjGxMeLyZMkmN/Pt3r5xwpgorFNkndsjpHaJvW ll+gCDnGvyL86VJFiYSNcUaM3BAQ+eU449nWAm5oB7Xu4= X-Received: by 2002:a5d:488b:0:b0:343:751e:8ef7 with SMTP id ffacd0b85a97d-34fcaa048b9mr2606229f8f.51.1715186813956; Wed, 08 May 2024 09:46:53 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEeckAlFycVqxKDckePBzZXtaVJrkNqLrckVAmZ3J7fbqR+sOKHrWVpsV9I5JMtGZ28G8NX/w== X-Received: by 2002:a5d:488b:0:b0:343:751e:8ef7 with SMTP id ffacd0b85a97d-34fcaa048b9mr2606208f8f.51.1715186813422; Wed, 08 May 2024 09:46:53 -0700 (PDT) Received: from localhost ([31.111.84.240]) by smtp.gmail.com with ESMTPSA id k7-20020a5d6287000000b0034f3f756293sm7118672wru.6.2024.05.08.09.46.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 May 2024 09:46:52 -0700 (PDT) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess , felix.willgerodt@intel.com, jhb@FreeBSD.org Subject: [PATCHv6 5/9] gdb/x86: move reading of cs and ds state into gdb/nat directory Date: Wed, 8 May 2024 17:46:41 +0100 Message-Id: X-Mailer: git-send-email 2.25.4 In-Reply-To: References: MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.1 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, 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+patchwork=sourceware.org@sourceware.org This patch is part of a series that has the aim sharing the x86 Linux target description creation code between GDB and gdbserver. Within GDB part of this process involves reading the cs and ds state from the 'struct user_regs_struct' using a ptrace call. This isn't done by gdbserver, which is part of the motivation for this whole series; the approach gdbserver takes is inferior to the approach GDB takes (gdbserver relies on reading the file being debugged, and extracting similar information from the file headers). This commit moves the reading of cs and ds, which is used to figure out if a thread is 32-bit or 64-bit (or in x32 mode), into the gdb/nat directory so that the code can be shared with gdbserver, but at this point I'm not actually using the code in gdbserver, that will come later. As such there should be no user visible changes after this commit, GDB continues to do things as it did before (reading cs/ds), while gdbserver continues to use its own approach (which doesn't require reading cs/ds). Approved-By: John Baldwin Reviewed-By: Felix Willgerodt --- gdb/nat/x86-linux.c | 47 +++++++++++++++++++++++++++++++++++++++++++++ gdb/nat/x86-linux.h | 28 +++++++++++++++++++++++++++ gdb/x86-linux-nat.c | 45 ++++++++----------------------------------- 3 files changed, 83 insertions(+), 37 deletions(-) diff --git a/gdb/nat/x86-linux.c b/gdb/nat/x86-linux.c index ed1d06bd623..145684367d7 100644 --- a/gdb/nat/x86-linux.c +++ b/gdb/nat/x86-linux.c @@ -19,6 +19,8 @@ #include "x86-linux.h" #include "x86-linux-dregs.h" +#include "nat/gdb_ptrace.h" +#include /* See nat/x86-linux.h. */ tribool have_ptrace_getfpxregs = @@ -91,3 +93,48 @@ x86_linux_prepare_to_resume (struct lwp_info *lwp) { x86_linux_update_debug_registers (lwp); } + +#ifdef __x86_64__ +/* Value of CS segment register: + 64bit process: 0x33 + 32bit process: 0x23 */ +#define AMD64_LINUX_USER64_CS 0x33 + +/* Value of DS segment register: + LP64 process: 0x0 + X32 process: 0x2b */ +#define AMD64_LINUX_X32_DS 0x2b +#endif + +/* See nat/x86-linux.h. */ + +x86_linux_arch_size +x86_linux_ptrace_get_arch_size (int tid) +{ +#ifdef __x86_64__ + unsigned long cs; + unsigned long ds; + + /* Get CS register. */ + errno = 0; + cs = ptrace (PTRACE_PEEKUSER, tid, + offsetof (struct user_regs_struct, cs), 0); + if (errno != 0) + perror_with_name (_("Couldn't get CS register")); + + bool is_64bit = cs == AMD64_LINUX_USER64_CS; + + /* Get DS register. */ + errno = 0; + ds = ptrace (PTRACE_PEEKUSER, tid, + offsetof (struct user_regs_struct, ds), 0); + if (errno != 0) + perror_with_name (_("Couldn't get DS register")); + + bool is_x32 = ds == AMD64_LINUX_X32_DS; + + return x86_linux_arch_size (is_64bit, is_x32); +#else + return x86_linux_arch_size (false, false); +#endif +} diff --git a/gdb/nat/x86-linux.h b/gdb/nat/x86-linux.h index cb5f44f4b49..a76c5092f52 100644 --- a/gdb/nat/x86-linux.h +++ b/gdb/nat/x86-linux.h @@ -64,4 +64,32 @@ extern void x86_linux_delete_thread (struct arch_lwp_info *arch_lwp); extern void x86_linux_prepare_to_resume (struct lwp_info *lwp); +/* Return value from x86_linux_ptrace_get_arch_size function. Indicates if + a thread is 32-bit, 64-bit, or x32. */ + +struct x86_linux_arch_size +{ + explicit x86_linux_arch_size (bool is_64bit, bool is_x32) + : m_is_64bit (is_64bit), + m_is_x32 (is_x32) + { + /* Nothing. */ + } + + bool is_64bit () const + { return m_is_64bit; } + + bool is_x32 () const + { return m_is_x32; } + +private: + bool m_is_64bit = false; + bool m_is_x32 = false; +}; + +/* Use ptrace calls to figure out if thread TID is 32-bit, 64-bit, or + 64-bit running in x32 mode. */ + +extern x86_linux_arch_size x86_linux_ptrace_get_arch_size (int tid); + #endif /* NAT_X86_LINUX_H */ diff --git a/gdb/x86-linux-nat.c b/gdb/x86-linux-nat.c index b42884ad57f..285a4880103 100644 --- a/gdb/x86-linux-nat.c +++ b/gdb/x86-linux-nat.c @@ -87,18 +87,6 @@ x86_linux_nat_target::post_startup_inferior (ptid_t ptid) linux_nat_target::post_startup_inferior (ptid); } -#ifdef __x86_64__ -/* Value of CS segment register: - 64bit process: 0x33 - 32bit process: 0x23 */ -#define AMD64_LINUX_USER64_CS 0x33 - -/* Value of DS segment register: - LP64 process: 0x0 - X32 process: 0x2b */ -#define AMD64_LINUX_X32_DS 0x2b -#endif - /* Get Linux/x86 target description from running target. */ const struct target_desc * @@ -118,31 +106,14 @@ x86_linux_nat_target::read_description () tid = inferior_ptid.pid (); #ifdef __x86_64__ - { - unsigned long cs; - unsigned long ds; - - /* Get CS register. */ - errno = 0; - cs = ptrace (PTRACE_PEEKUSER, tid, - offsetof (struct user_regs_struct, cs), 0); - if (errno != 0) - perror_with_name (_("Couldn't get CS register")); - - is_64bit = cs == AMD64_LINUX_USER64_CS; - - /* Get DS register. */ - errno = 0; - ds = ptrace (PTRACE_PEEKUSER, tid, - offsetof (struct user_regs_struct, ds), 0); - if (errno != 0) - perror_with_name (_("Couldn't get DS register")); - - is_x32 = ds == AMD64_LINUX_X32_DS; - - if (sizeof (void *) == 4 && is_64bit && !is_x32) - error (_("Can't debug 64-bit process with 32-bit GDB")); - } + + x86_linux_arch_size arch_size = x86_linux_ptrace_get_arch_size (tid); + is_64bit = arch_size.is_64bit (); + is_x32 = arch_size.is_x32 (); + + if (sizeof (void *) == 4 && is_64bit && !is_x32) + error (_("Can't debug 64-bit process with 32-bit GDB")); + #elif HAVE_PTRACE_GETFPXREGS if (have_ptrace_getfpxregs == TRIBOOL_UNKNOWN) { From patchwork Wed May 8 16:46:42 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Burgess X-Patchwork-Id: 89799 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 891513849AD1 for ; Wed, 8 May 2024 16:49:42 +0000 (GMT) X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 6DD41384AB63 for ; Wed, 8 May 2024 16:46:58 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 6DD41384AB63 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 6DD41384AB63 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1715186821; cv=none; b=dIsdfFgmXz5YA5596CMYQUIRam4rgorN+708lV6yVak5ofsqXAczTttjqwojKsTSdAXy9wWRDfXnMaFbew1GzYOdNzwyhIjj4kOp/obll8D8X9Y/kB75nuBCZYUqyiThHIzS2q7XecKovwb/Aj2ubxqQuP81R/l8mhfnXCFsDl0= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1715186821; c=relaxed/simple; bh=ETkbBlaj1w9W1Anc108RQLOnCCo0Rww+Xx/8mgadGyo=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=NR2pLphZdC3UEXcYyfRlogib2N+tN2UnHpURrXf0atZ5YABo49t0gnSkXP2wC6rVzQVhZI+tfl8pjze6/CRuSgz+ZSNcTLF2IXS58OJPJiH6TbzoW/BKg7FB0clCExpftPHLhVAswRDaD9y3e7dJPsUJzNcdjyDFKCpnuziAaNY= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1715186818; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=cqElmtZbv7aSTtDT5mJGICUe0+iVjYJmDgq6nHCvAyU=; b=T5J9ahivlrn5EN4aYUxC2ZGAL5hw+lUZhjHrNZWiUXc2FYchwISNp0pWD5uoz0chWogK04 1qDeaA/ORdjJ1Y6YblVlTSGtU/VlNTcLk+FMvoqREYkvxh1+Wczg4SJi8x2ktVlrwkPYqs E8T6Q1zs3bQyy7YZboPbl/pY4ySZQbI= Received: from mail-wm1-f70.google.com (mail-wm1-f70.google.com [209.85.128.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-374-EH26NWk2MAKqNGI9cKT8tQ-1; Wed, 08 May 2024 12:46:56 -0400 X-MC-Unique: EH26NWk2MAKqNGI9cKT8tQ-1 Received: by mail-wm1-f70.google.com with SMTP id 5b1f17b1804b1-41ec1528d83so27378665e9.1 for ; Wed, 08 May 2024 09:46:56 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715186815; x=1715791615; 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=cqElmtZbv7aSTtDT5mJGICUe0+iVjYJmDgq6nHCvAyU=; b=a4DuInHamouELPPQVAR7puuNypSIb9/bIEW1XNdIP3dUIyx6z9N3DsHpbsgRiMy+5Y 5cWA2aQUs0Lvgjg6DUAKfHp1tStutxi/sZChSGlpuIQVjn6EAcGxBWFBUR7JmCvILmdD nDMeQiZiwCNGEHOgEpRSxUav5degXLHQtVAixr5aKPzd+lElLN+Q2Qk+MdD0mYYJ8OZl kglISJTGhUTCzC2miXoTC6Dzj558nJwBks2sekQLkWAvUrYtUyHmt/ceGiCge8qtOrbn ydhCTQX2IcSWWSOC0HODxO6dgkgFJUZgnmGV17sB0b+Ebclu0koTWHmqPENDFMOXcXrJ DKjQ== X-Gm-Message-State: AOJu0YymYV7cP6J1Q1Rw0OwlKpMfjACPNBCXLkP2JlUTr9Z4LhOih/AH J0sWXrPPVh1e+Mo23Z0Gs69rTZphN7JYiIFjsp+LVX0jg/i6k8IDQ+UpSgTpVjQURAlV81OYpZ+ Fq1kottuwOAwjQMTb2TbkW8QatbLyG+XPgiiaZXWfPck42Bv9woTUY7x+F9tBSUngQIxLeuBRBP q7m8Ib0vxMMbDV2WllFz/1MK+1zFxiwF0l7O34Og6+pbY= X-Received: by 2002:a05:600c:314a:b0:41b:eaf2:f7e6 with SMTP id 5b1f17b1804b1-41f71302e3emr34091215e9.2.1715186814760; Wed, 08 May 2024 09:46:54 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHP14hr9lGdosembSX1i7UMVHFzfEUiY2QZ2XYsL4826th9vqelzrwDnE3cjwNPpUKITYXTHA== X-Received: by 2002:a05:600c:314a:b0:41b:eaf2:f7e6 with SMTP id 5b1f17b1804b1-41f71302e3emr34090925e9.2.1715186814342; Wed, 08 May 2024 09:46:54 -0700 (PDT) Received: from localhost ([31.111.84.240]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-41f87b265dasm29459505e9.3.2024.05.08.09.46.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 May 2024 09:46:54 -0700 (PDT) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess , felix.willgerodt@intel.com, jhb@FreeBSD.org Subject: [PATCHv6 6/9] gdb: move xcr0 == 0 check into i386_linux_core_read_description Date: Wed, 8 May 2024 17:46:42 +0100 Message-Id: X-Mailer: git-send-email 2.25.4 In-Reply-To: References: MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.1 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, 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+patchwork=sourceware.org@sourceware.org Currently, in i386_linux_core_read_description, if GDB fails to extract an xcr0 value from the core file, then we will have a default zero value for the xcr0 variable, we still call the i386_linux_read_description function, which checks for this zero value and returns nullptr. Back in i386_linux_core_read_description we spot the nullptr return value from i386_linux_read_description and call i386_linux_read_description again, but this time passing a default value for xcr0. In the next commit I plan to rework i386_linux_read_description, and in so doing I will remove the check for xcr0 == 0, this is inline with how the amd64 code is written. However, this means that the 'xcr0 == 0' check needs to move up the stack to i386_linux_core_read_description, again, this brings the i386 code into line with the amd64 code. This is just a refactor in preparation for the next commit, there should be no user visible changes after this commit. Approved-By: Felix Willgerodt --- gdb/i386-linux-tdep.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/gdb/i386-linux-tdep.c b/gdb/i386-linux-tdep.c index a78f03fac8d..511e43f3b6f 100644 --- a/gdb/i386-linux-tdep.c +++ b/gdb/i386-linux-tdep.c @@ -714,15 +714,16 @@ i386_linux_core_read_description (struct gdbarch *gdbarch, /* Linux/i386. */ x86_xsave_layout layout; uint64_t xcr0 = i386_linux_core_read_xsave_info (abfd, layout); - const struct target_desc *tdesc = i386_linux_read_description (xcr0); - if (tdesc != NULL) - return tdesc; + if (xcr0 == 0) + { + if (bfd_get_section_by_name (abfd, ".reg-xfp") != nullptr) + xcr0 = X86_XSTATE_SSE_MASK; + else + xcr0 = X86_XSTATE_X87_MASK; + } - if (bfd_get_section_by_name (abfd, ".reg-xfp") != NULL) - return i386_linux_read_description (X86_XSTATE_SSE_MASK); - else - return i386_linux_read_description (X86_XSTATE_X87_MASK); + return i386_linux_read_description (xcr0); } /* Similar to i386_supply_fpregset, but use XSAVE extended state. */ From patchwork Wed May 8 16:46:43 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Burgess X-Patchwork-Id: 89796 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 07AFE3870876 for ; Wed, 8 May 2024 16:48:46 +0000 (GMT) X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 229293849AD1 for ; Wed, 8 May 2024 16:47:02 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 229293849AD1 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 229293849AD1 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1715186833; cv=none; b=qn4ewoFnBvscO9MAxtT6Kau0zDEVa83Ojp0DvSB7IemQc5vXs/eeQKRvHSLhWBWq32ejno0hTzr51SihZSyS8SAuxI61AJfSNPA1Cy5EUFR8fb+msPRoYDdJyhyXzfBNPN2rK3cVb6rP96UoQ4u4zcN5g02Xd4uVJrttPOwy7Ww= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1715186833; c=relaxed/simple; bh=h72QAxFEO1GQUnduVFHJ+1edYFWdNH6tJalju96v2nU=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=cFWr8ydVJycZKM8U3zhPdFGGewXxnhzMURzA6tEmq6jATv/uxb71voLzZ3EBIP+Zx0yaw5Lb4qz/WukddexxbyYzIX9PgzbKekZPE08cXRTN9bLRuaomPt2eFe1y57h9Qga8tiptgj444zcMP7ENcl+NaFK9DKFuIfcGnSEb0Q0= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1715186822; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=bjxkI/4qOAPJ7aITKZ0fceCUsFQ9puBm1f3njOMxKkE=; b=K+2QtLPcXZ2XBugLWuznhkZ4ExNZWjsDAphueZJUdP7no6wDvB3OEGQqjpbmYPRQniKNh1 cu1BnAgUDTkI5QdcQshiP88I+h4FkjWgqE2lG2NsitC1YPnj3UqgpKpF2AJwPVpytUorgm HUfm6PJCsRjN4wrEEWiJOb/+TpjN7zc= Received: from mail-wm1-f70.google.com (mail-wm1-f70.google.com [209.85.128.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-638-aOp2j1Y9OCeST_NOZ9nOeQ-1; Wed, 08 May 2024 12:47:01 -0400 X-MC-Unique: aOp2j1Y9OCeST_NOZ9nOeQ-1 Received: by mail-wm1-f70.google.com with SMTP id 5b1f17b1804b1-41d979ae1b8so17373285e9.3 for ; Wed, 08 May 2024 09:47:01 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715186817; x=1715791617; 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=6U+Z9aUQXtn4c6MRdStPmBSNqgGp6EQA/BufLX6xTy4=; b=ilmDAaO+QF6NDh6PbM41843z9SCDHBQMo3hEWgTLCwmFGUJc2hoHQ81b43zG0eKpqg UowATUVDewBCjKYEao9OJDnk37nkqbOj8zf7heBwkwqpP28AOrbgD1DgslRZZ4moT1mi UtMQbdsSVUu287CHQhAzUErUxwHXnTbg4H5IeQv4mt/c/VGEUCsBLRpgNk/i3W/0nQol rHhVXsbBJhOPA36jMdNtsuJJoHz5S7EkVCkav6WiigTfNODhOESppxqIYSt1nZz7p7YN cB/sb71al5CakEni9DPVzDnlZK8f0MhvbrAjerYMkpNfRVKsHaeCiL4REWYknQXUZ2oB Edsw== X-Gm-Message-State: AOJu0Yw+trRLMqc5iCIcc5w74Dk+/xuXQSLHUFkNsAvlp7yKnO6jw3iw ZKt75lXvNQxUiUXwUimF3DnsxdHi/eH4wuHwjgEl74TEfZYye/5FxCe5Fvrk0SA0sqpsVToTWmR qvBz6fQKrlZMVocza94a8AQWAPyr3gMpIvTEttCklP8cliLQAq2mORcxlnjTzKerMVkA5l4Uqw+ ILtienAsQTWliBs8nR9szmSVgYdNHQNvU8s+98sIKHD4k= X-Received: by 2002:a05:6000:1141:b0:34d:e252:b57d with SMTP id ffacd0b85a97d-34fca242165mr2235569f8f.17.1715186816563; Wed, 08 May 2024 09:46:56 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFBe19P0BQKgfxRb4Q4wyt0arK2VoX0/mZkVZLQVYpiHRsB2+quRMoh50aCseQjYYw9B+j9bg== X-Received: by 2002:a05:6000:1141:b0:34d:e252:b57d with SMTP id ffacd0b85a97d-34fca242165mr2235549f8f.17.1715186815542; Wed, 08 May 2024 09:46:55 -0700 (PDT) Received: from localhost ([31.111.84.240]) by smtp.gmail.com with ESMTPSA id o10-20020a5d47ca000000b0034d9e5411ebsm15832912wrc.45.2024.05.08.09.46.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 May 2024 09:46:55 -0700 (PDT) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess , felix.willgerodt@intel.com, jhb@FreeBSD.org Subject: [PATCHv6 7/9] gdb/gdbserver: share some code relating to target description creation Date: Wed, 8 May 2024 17:46:43 +0100 Message-Id: X-Mailer: git-send-email 2.25.4 In-Reply-To: References: MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.5 required=5.0 tests=BAYES_00, DKIM_INVALID, DKIM_SIGNED, GIT_PATCH_0, KAM_DMARC_NONE, KAM_DMARC_STATUS, KAM_SHORT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, 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+patchwork=sourceware.org@sourceware.org This commit is part of a series to share more of the x86 target description creation code between GDB and gdbserver. Unlike previous commits which were mostly refactoring, this commit is the first that makes a real change, though that change should mostly be for gdbserver; I've largely adopted the "GDB" way of doing things for gdbserver, and this fixes a real gdbserver bug. On a x86-64 Linux target, running the test: gdb.server/connect-with-no-symbol-file.exp results in two core files being created. Both of these core files are from the inferior process, created after gdbserver has detached. In this test a gdbserver process is started and then, after gdbserver has started, but before GDB attaches, we either delete the inferior executable, or change its permissions so it can't be read. Only after doing this do we attempt to connect with GDB. As GDB connects to gdbserver, gdbserver attempts to figure out the target description so that it can send the description to GDB, this involves a call to x86_linux_read_description. In x86_linux_read_description one of the first things we do is try to figure out if the process is 32-bit or 64-bit. To do this we look up the executable via the thread-id, and then attempt to read the architecture size from the executable. This isn't going to work if the executable has been deleted, or is no longer readable. And so, as we can't read the executable, we default to an i386 target and use an i386 target description. A consequence of using an i386 target description is that addresses are assumed to be 32-bits. Here's an example session that shows the problems this causes. This is run on an x86-64 machine, and the test binary (xx.x) is a standard 64-bit x86-64 binary: shell_1$ gdbserver --once localhost :54321 /tmp/xx.x shell_2$ gdb -q (gdb) set sysroot (gdb) shell chmod 000 /tmp/xx.x (gdb) target remote :54321 Remote debugging using :54321 warning: /tmp/xx.x: Permission denied. 0xf7fd3110 in ?? () (gdb) show architecture The target architecture is set to "auto" (currently "i386"). (gdb) p/x $pc $1 = 0xf7fd3110 (gdb) info proc mappings process 2412639 Mapped address spaces: Start Addr End Addr Size Offset Perms objfile 0x400000 0x401000 0x1000 0x0 r--p /tmp/xx.x 0x401000 0x402000 0x1000 0x1000 r-xp /tmp/xx.x 0x402000 0x403000 0x1000 0x2000 r--p /tmp/xx.x 0x403000 0x405000 0x2000 0x2000 rw-p /tmp/xx.x 0xf7fcb000 0xf7fcf000 0x4000 0x0 r--p [vvar] 0xf7fcf000 0xf7fd1000 0x2000 0x0 r-xp [vdso] 0xf7fd1000 0xf7fd3000 0x2000 0x0 r--p /usr/lib64/ld-2.30.so 0xf7fd3000 0xf7ff3000 0x20000 0x2000 r-xp /usr/lib64/ld-2.30.so 0xf7ff3000 0xf7ffb000 0x8000 0x22000 r--p /usr/lib64/ld-2.30.so 0xf7ffc000 0xf7ffe000 0x2000 0x2a000 rw-p /usr/lib64/ld-2.30.so 0xf7ffe000 0xf7fff000 0x1000 0x0 rw-p 0xfffda000 0xfffff000 0x25000 0x0 rw-p [stack] 0xff600000 0xff601000 0x1000 0x0 r-xp [vsyscall] (gdb) info inferiors Num Description Connection Executable * 1 process 2412639 1 (remote :54321) (gdb) shell cat /proc/2412639/maps 00400000-00401000 r--p 00000000 fd:03 45907133 /tmp/xx.x 00401000-00402000 r-xp 00001000 fd:03 45907133 /tmp/xx.x 00402000-00403000 r--p 00002000 fd:03 45907133 /tmp/xx.x 00403000-00405000 rw-p 00002000 fd:03 45907133 /tmp/xx.x 7ffff7fcb000-7ffff7fcf000 r--p 00000000 00:00 0 [vvar] 7ffff7fcf000-7ffff7fd1000 r-xp 00000000 00:00 0 [vdso] 7ffff7fd1000-7ffff7fd3000 r--p 00000000 fd:00 143904 /usr/lib64/ld-2.30.so 7ffff7fd3000-7ffff7ff3000 r-xp 00002000 fd:00 143904 /usr/lib64/ld-2.30.so 7ffff7ff3000-7ffff7ffb000 r--p 00022000 fd:00 143904 /usr/lib64/ld-2.30.so 7ffff7ffc000-7ffff7ffe000 rw-p 0002a000 fd:00 143904 /usr/lib64/ld-2.30.so 7ffff7ffe000-7ffff7fff000 rw-p 00000000 00:00 0 7ffffffda000-7ffffffff000 rw-p 00000000 00:00 0 [stack] ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall] (gdb) Notice the difference between the mappings reported via GDB and those reported directly from the kernel via /proc/PID/maps, the addresses of every mapping is clamped to 32-bits for GDB, while the kernel reports real 64-bit addresses. Notice also that the $pc value is a 32-bit value. It appears to be within one of the mappings reported by GDB, but is outside any of the mappings reported from the kernel. And this is where the problem arises. When gdbserver detaches from the inferior we pass the inferior the address from which it should resume. Due to the 32/64 bit confusion we tell the inferior to resume from the 32-bit $pc value, which is not within any valid mapping, and so, as soon as the inferior resumes, it segfaults. If we look at how GDB (not gdbserver) figures out its target description then we see an interesting difference. GDB doesn't try to read the executable. Instead GDB uses ptrace to query the thread's state, and uses this to figure out the if the thread is 32 or 64 bit. If we update gdbserver to do it the "GDB" way then the above problem is resolved, gdbserver now sees the process as 64-bit, and when we detach from the inferior we give it the correct 64-bit address, and the inferior no longer segfaults. Now, I could just update the gdbserver code, but better, I think, to share one copy of the code between GDB and gdbserver in gdb/nat/. That is what this commit does. The cores of x86_linux_read_description from gdbserver and x86_linux_nat_target::read_description from GDB are moved into a new file gdb/nat/x86-linux-tdesc.c and combined into a single function x86_linux_tdesc_for_tid which is called from each location. This new function does things mostly the GDB way, some changes are needed to allow for the sharing; we now take some pointers for where the shared code can cache the xcr0 and xsave layout values. Another thing to note about this commit is how the functions i386_linux_read_description and amd64_linux_read_description are handled. For now I've left these function as implemented separately in GDB and gdbserver. I've moved the declarations of these functions into gdb/arch/{i386,amd64}-linux-tdesc.h, but the implementations are left where they are. A later commit in this series will make these functions shared too, but doing this is not trivial, so I've left that for a separate commit. Merging the declarations as I've done here ensures that everyone implements the function to the same API, and once these functions are shared (in a later commit) we'll want a shared declaration anyway. Reviewed-By: Felix Willgerodt Acked-By: John Baldwin --- gdb/Makefile.in | 3 + gdb/amd64-linux-tdep.c | 1 + gdb/amd64-linux-tdep.h | 6 -- gdb/arch/amd64-linux-tdesc.h | 30 +++++++ gdb/arch/i386-linux-tdesc.h | 29 +++++++ gdb/configure.nat | 4 +- gdb/i386-linux-tdep.c | 1 + gdb/i386-linux-tdep.h | 3 - gdb/nat/x86-linux-tdesc.c | 128 ++++++++++++++++++++++++++++++ gdb/nat/x86-linux-tdesc.h | 54 +++++++++++++ gdb/x86-linux-nat.c | 88 ++------------------- gdbserver/configure.srv | 2 + gdbserver/i387-fp.cc | 9 +-- gdbserver/i387-fp.h | 4 +- gdbserver/linux-amd64-ipa.cc | 1 + gdbserver/linux-i386-ipa.cc | 1 + gdbserver/linux-x86-low.cc | 147 ++++++++++------------------------- gdbserver/linux-x86-tdesc.cc | 2 + gdbserver/linux-x86-tdesc.h | 7 -- 19 files changed, 310 insertions(+), 210 deletions(-) create mode 100644 gdb/arch/amd64-linux-tdesc.h create mode 100644 gdb/arch/i386-linux-tdesc.h create mode 100644 gdb/nat/x86-linux-tdesc.c create mode 100644 gdb/nat/x86-linux-tdesc.h diff --git a/gdb/Makefile.in b/gdb/Makefile.in index d1c9c2f2859..9c2c69b02ab 100644 --- a/gdb/Makefile.in +++ b/gdb/Makefile.in @@ -1550,8 +1550,10 @@ HFILES_NO_SRCDIR = \ arch/aarch64-insn.h \ arch/aarch64-mte-linux.h \ arch/aarch64-scalable-linux.h \ + arch/amd64-linux-tdesc.h \ arch/arc.h \ arch/arm.h \ + arch/i386-linux-tdesc.h \ arch/i386.h \ arch/loongarch.h \ arch/ppc-linux-common.h \ @@ -1607,6 +1609,7 @@ HFILES_NO_SRCDIR = \ nat/x86-gcc-cpuid.h \ nat/x86-linux.h \ nat/x86-linux-dregs.h \ + nat/x86-linux-tdesc.h \ python/py-event.h \ python/py-events.h \ python/py-stopevent.h \ diff --git a/gdb/amd64-linux-tdep.c b/gdb/amd64-linux-tdep.c index c52b0436872..f9647dce1e6 100644 --- a/gdb/amd64-linux-tdep.c +++ b/gdb/amd64-linux-tdep.c @@ -42,6 +42,7 @@ #include "arch/amd64.h" #include "target-descriptions.h" #include "expop.h" +#include "arch/amd64-linux-tdesc.h" /* The syscall's XML filename for i386. */ #define XML_SYSCALL_FILENAME_AMD64 "syscalls/amd64-linux.xml" diff --git a/gdb/amd64-linux-tdep.h b/gdb/amd64-linux-tdep.h index 2003dcda78f..0ec49e7fe03 100644 --- a/gdb/amd64-linux-tdep.h +++ b/gdb/amd64-linux-tdep.h @@ -43,12 +43,6 @@ extern struct target_desc *tdesc_x32_linux; extern struct target_desc *tdesc_x32_avx_linux; extern struct target_desc *tdesc_x32_avx_avx512_linux; -/* Return the right amd64-linux target descriptions according to - XCR0_FEATURES_BIT and IS_X32. */ - -const target_desc *amd64_linux_read_description (uint64_t xcr0_features_bit, - bool is_x32); - /* Enum that defines the syscall identifiers for amd64 linux. Used for process record/replay, these will be translated into a gdb-canonical set of syscall ids in linux-record.c. */ diff --git a/gdb/arch/amd64-linux-tdesc.h b/gdb/arch/amd64-linux-tdesc.h new file mode 100644 index 00000000000..db425b60df6 --- /dev/null +++ b/gdb/arch/amd64-linux-tdesc.h @@ -0,0 +1,30 @@ +/* Target description related code for GNU/Linux x86-64. + + Copyright (C) 2024 Free Software Foundation, Inc. + + This file is part of GDB. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + +#ifndef ARCH_AMD64_LINUX_TDESC_H +#define ARCH_AMD64_LINUX_TDESC_H + +struct target_desc; + +/* Return the AMD64 target descriptions corresponding to XCR0 and IS_X32. */ + +extern const target_desc *amd64_linux_read_description (uint64_t xcr0, + bool is_x32); + +#endif /* ARCH_AMD64_LINUX_TDESC_H */ diff --git a/gdb/arch/i386-linux-tdesc.h b/gdb/arch/i386-linux-tdesc.h new file mode 100644 index 00000000000..0b736337a75 --- /dev/null +++ b/gdb/arch/i386-linux-tdesc.h @@ -0,0 +1,29 @@ +/* Target description related code for GNU/Linux i386. + + Copyright (C) 2024 Free Software Foundation, Inc. + + This file is part of GDB. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + +#ifndef ARCH_I386_LINUX_TDESC_H +#define ARCH_I386_LINUX_TDESC_H + +struct target_desc; + +/* Return the i386 target description corresponding to XCR0. */ + +extern const struct target_desc *i386_linux_read_description (uint64_t xcr0); + +#endif /* ARCH_I386_LINUX_TDESC_H */ diff --git a/gdb/configure.nat b/gdb/configure.nat index 8b98511cef7..4bcc0696027 100644 --- a/gdb/configure.nat +++ b/gdb/configure.nat @@ -256,7 +256,7 @@ case ${gdb_host} in NATDEPFILES="${NATDEPFILES} x86-nat.o nat/x86-dregs.o \ nat/x86-xstate.o \ i386-linux-nat.o x86-linux-nat.o nat/linux-btrace.o \ - nat/x86-linux.o nat/x86-linux-dregs.o" + nat/x86-linux.o nat/x86-linux-dregs.o nat/x86-linux-tdesc.o" ;; ia64) # Host: Intel IA-64 running GNU/Linux @@ -322,7 +322,7 @@ case ${gdb_host} in NATDEPFILES="${NATDEPFILES} x86-nat.o nat/x86-dregs.o \ nat/x86-xstate.o amd64-nat.o amd64-linux-nat.o x86-linux-nat.o \ nat/linux-btrace.o \ - nat/x86-linux.o nat/x86-linux-dregs.o \ + nat/x86-linux.o nat/x86-linux-dregs.o nat/x86-linux-tdesc.o \ nat/amd64-linux-siginfo.o" ;; sparc) diff --git a/gdb/i386-linux-tdep.c b/gdb/i386-linux-tdep.c index 511e43f3b6f..6a7490680c1 100644 --- a/gdb/i386-linux-tdep.c +++ b/gdb/i386-linux-tdep.c @@ -40,6 +40,7 @@ #include "i387-tdep.h" #include "gdbsupport/x86-xstate.h" +#include "arch/i386-linux-tdesc.h" /* The syscall's XML filename for i386. */ #define XML_SYSCALL_FILENAME_I386 "syscalls/i386-linux.xml" diff --git a/gdb/i386-linux-tdep.h b/gdb/i386-linux-tdep.h index 07593c6a8ec..e8691cd778e 100644 --- a/gdb/i386-linux-tdep.h +++ b/gdb/i386-linux-tdep.h @@ -55,9 +55,6 @@ extern void i386_linux_report_signal_info (struct gdbarch *gdbarch, struct ui_out *uiout, enum gdb_signal siggnal); -/* Return the target description according to XCR0. */ -extern const struct target_desc *i386_linux_read_description (uint64_t xcr0); - extern int i386_linux_gregset_reg_offset[]; /* Return x86 siginfo type. */ diff --git a/gdb/nat/x86-linux-tdesc.c b/gdb/nat/x86-linux-tdesc.c new file mode 100644 index 00000000000..ebe94d475ce --- /dev/null +++ b/gdb/nat/x86-linux-tdesc.c @@ -0,0 +1,128 @@ +/* Target description related code for GNU/Linux x86 (i386 and x86-64). + + Copyright (C) 2024 Free Software Foundation, Inc. + + This file is part of GDB. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + +#include "nat/x86-linux-tdesc.h" +#ifdef __x86_64__ +#include "arch/amd64.h" +#include "arch/amd64-linux-tdesc.h" +#endif +#include "arch/i386.h" +#include "arch/i386-linux-tdesc.h" + +#include "nat/x86-linux.h" +#include "nat/gdb_ptrace.h" +#include "nat/x86-xstate.h" +#include "gdbsupport/x86-xstate.h" + +#ifndef __x86_64__ +#include +#endif + +#include +#include + +#ifndef IN_PROCESS_AGENT + +/* See nat/x86-linux-tdesc.h. */ + +const target_desc * +x86_linux_tdesc_for_tid (int tid, const char *error_msg, + uint64_t *xcr0_storage, + x86_xsave_layout *xsave_layout_storage) +{ +#ifdef __x86_64__ + + x86_linux_arch_size arch_size = x86_linux_ptrace_get_arch_size (tid); + bool is_64bit = arch_size.is_64bit (); + bool is_x32 = arch_size.is_x32 (); + + if (sizeof (void *) == 4 && is_64bit && !is_x32) + error ("%s", error_msg); + +#elif HAVE_PTRACE_GETFPXREGS + if (have_ptrace_getfpxregs == TRIBOOL_UNKNOWN) + { + elf_fpxregset_t fpxregs; + + if (ptrace (PTRACE_GETFPXREGS, tid, 0, (int) &fpxregs) < 0) + { + have_ptrace_getfpxregs = TRIBOOL_FALSE; + have_ptrace_getregset = TRIBOOL_FALSE; + } + else + have_ptrace_getfpxregs = TRIBOOL_TRUE; + } + + if (have_ptrace_getfpxregs == TRIBOOL_FALSE) + return i386_linux_read_description (X86_XSTATE_X87_MASK); +#endif + + if (have_ptrace_getregset == TRIBOOL_UNKNOWN) + { + uint64_t xstateregs[(X86_XSTATE_SSE_SIZE / sizeof (uint64_t))]; + struct iovec iov; + + iov.iov_base = xstateregs; + iov.iov_len = sizeof (xstateregs); + + /* Check if PTRACE_GETREGSET works. */ + if (ptrace (PTRACE_GETREGSET, tid, + (unsigned int) NT_X86_XSTATE, &iov) < 0) + { + have_ptrace_getregset = TRIBOOL_FALSE; + *xcr0_storage = 0; + } + else + { + have_ptrace_getregset = TRIBOOL_TRUE; + + /* Get XCR0 from XSAVE extended state. */ + *xcr0_storage = xstateregs[(I386_LINUX_XSAVE_XCR0_OFFSET + / sizeof (uint64_t))]; + +#ifdef __x86_64__ + /* No MPX on x32. */ + if (is_64bit && is_x32) + *xcr0_storage &= ~X86_XSTATE_MPX; +#endif /* __x86_64__ */ + + *xsave_layout_storage + = x86_fetch_xsave_layout (*xcr0_storage, x86_xsave_length ()); + } + } + + /* Check the native XCR0 only if PTRACE_GETREGSET is available. If + PTRACE_GETREGSET is not available then set xcr0_features_bits to + zero so that the "no-features" descriptions are returned by the + code below. */ + uint64_t xcr0_features_bits; + if (have_ptrace_getregset == TRIBOOL_TRUE) + xcr0_features_bits = *xcr0_storage & X86_XSTATE_ALL_MASK; + else + xcr0_features_bits = 0; + +#ifdef __x86_64__ + if (is_64bit) + return amd64_linux_read_description (xcr0_features_bits, is_x32); + else +#endif + return i386_linux_read_description (xcr0_features_bits); +} + +#endif /* !IN_PROCESS_AGENT */ diff --git a/gdb/nat/x86-linux-tdesc.h b/gdb/nat/x86-linux-tdesc.h new file mode 100644 index 00000000000..4035c49fc06 --- /dev/null +++ b/gdb/nat/x86-linux-tdesc.h @@ -0,0 +1,54 @@ +/* Target description related code for GNU/Linux x86 (i386 and x86-64). + + Copyright (C) 2024 Free Software Foundation, Inc. + + This file is part of GDB. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + +#ifndef NAT_X86_LINUX_TDESC_H +#define NAT_X86_LINUX_TDESC_H + +#include "gdbsupport/function-view.h" + +struct target_desc; +struct x86_xsave_layout; + +/* Return the target description for Linux thread TID. + + The storage pointed to by XCR0_STORAGE AND XSAVE_LAYOUT_STORAGE must + exist until the program (GDB or gdbserver) terminates, this storage is + used to cache the xcr0 and xsave layout values. The values pointed to + by these arguments are only updated at most once, the first time this + function is called. + + This function returns a target description based on the extracted xcr0 + value along with other characteristics of the thread identified by TID. + + This function can return nullptr if we encounter a machine configuration + for which a target_desc cannot be created. Ideally this would not be + the case, we should be able to create a target description for every + possible machine configuration. See amd64_linux_read_description and + i386_linux_read_description for cases when nullptr might be + returned. + + ERROR_MSG is using in an error() call if we try to create a target + description for a 64-bit process but this is a 32-bit build of GDB. */ + +extern const target_desc * +x86_linux_tdesc_for_tid (int tid, const char *error_msg, + uint64_t *xcr0_storage, + x86_xsave_layout *xsave_layout_storage); + +#endif /* NAT_X86_LINUX_TDESC_H */ diff --git a/gdb/x86-linux-nat.c b/gdb/x86-linux-nat.c index 285a4880103..43a72893562 100644 --- a/gdb/x86-linux-nat.c +++ b/gdb/x86-linux-nat.c @@ -38,6 +38,7 @@ #include "nat/x86-linux.h" #include "nat/x86-linux-dregs.h" #include "nat/linux-ptrace.h" +#include "nat/x86-linux-tdesc.h" /* linux_nat_target::low_new_fork implementation. */ @@ -92,93 +93,18 @@ x86_linux_nat_target::post_startup_inferior (ptid_t ptid) const struct target_desc * x86_linux_nat_target::read_description () { - int tid; - int is_64bit = 0; -#ifdef __x86_64__ - int is_x32; -#endif - static uint64_t xcr0; - uint64_t xcr0_features_bits; + static uint64_t xcr0_storage; if (inferior_ptid == null_ptid) return this->beneath ()->read_description (); - tid = inferior_ptid.pid (); - -#ifdef __x86_64__ - - x86_linux_arch_size arch_size = x86_linux_ptrace_get_arch_size (tid); - is_64bit = arch_size.is_64bit (); - is_x32 = arch_size.is_x32 (); - - if (sizeof (void *) == 4 && is_64bit && !is_x32) - error (_("Can't debug 64-bit process with 32-bit GDB")); - -#elif HAVE_PTRACE_GETFPXREGS - if (have_ptrace_getfpxregs == TRIBOOL_UNKNOWN) - { - elf_fpxregset_t fpxregs; - - if (ptrace (PTRACE_GETFPXREGS, tid, 0, (int) &fpxregs) < 0) - { - have_ptrace_getfpxregs = TRIBOOL_FALSE; - have_ptrace_getregset = TRIBOOL_FALSE; - return i386_linux_read_description (X86_XSTATE_X87_MASK); - } - } -#endif - - if (have_ptrace_getregset == TRIBOOL_UNKNOWN) - { - uint64_t xstateregs[(X86_XSTATE_SSE_SIZE / sizeof (uint64_t))]; - struct iovec iov; - - iov.iov_base = xstateregs; - iov.iov_len = sizeof (xstateregs); - - /* Check if PTRACE_GETREGSET works. */ - if (ptrace (PTRACE_GETREGSET, tid, - (unsigned int) NT_X86_XSTATE, &iov) < 0) - have_ptrace_getregset = TRIBOOL_FALSE; - else - { - have_ptrace_getregset = TRIBOOL_TRUE; - - /* Get XCR0 from XSAVE extended state. */ - xcr0 = xstateregs[(I386_LINUX_XSAVE_XCR0_OFFSET - / sizeof (uint64_t))]; - - m_xsave_layout = x86_fetch_xsave_layout (xcr0, x86_xsave_length ()); - } - } - - /* Check the native XCR0 only if PTRACE_GETREGSET is available. If - PTRACE_GETREGSET is not available then set xcr0_features_bits to - zero so that the "no-features" descriptions are returned by the - switches below. */ - if (have_ptrace_getregset == TRIBOOL_TRUE) - xcr0_features_bits = xcr0 & X86_XSTATE_ALL_MASK; - else - xcr0_features_bits = 0; + int tid = inferior_ptid.pid (); - if (is_64bit) - { -#ifdef __x86_64__ - return amd64_linux_read_description (xcr0_features_bits, is_x32); -#endif - } - else - { - const struct target_desc * tdesc - = i386_linux_read_description (xcr0_features_bits); - - if (tdesc == NULL) - tdesc = i386_linux_read_description (X86_XSTATE_SSE_MASK); - - return tdesc; - } + const char *error_msg + = _("Can't debug 64-bit process with 32-bit GDB"); - gdb_assert_not_reached ("failed to return tdesc"); + return x86_linux_tdesc_for_tid (tid, error_msg, &xcr0_storage, + &this->m_xsave_layout); } diff --git a/gdbserver/configure.srv b/gdbserver/configure.srv index 9e861a75088..7a2702d78bf 100644 --- a/gdbserver/configure.srv +++ b/gdbserver/configure.srv @@ -109,6 +109,7 @@ case "${gdbserver_host}" in srv_tgtobj="${srv_tgtobj} nat/linux-btrace.o" srv_tgtobj="${srv_tgtobj} nat/x86-linux.o" srv_tgtobj="${srv_tgtobj} nat/x86-linux-dregs.o" + srv_tgtobj="${srv_tgtobj} nat/x86-linux-tdesc.o" srv_linux_usrregs=yes srv_linux_regsets=yes srv_linux_thread_db=yes @@ -371,6 +372,7 @@ case "${gdbserver_host}" in srv_tgtobj="${srv_tgtobj} nat/linux-btrace.o" srv_tgtobj="${srv_tgtobj} nat/x86-linux.o" srv_tgtobj="${srv_tgtobj} nat/x86-linux-dregs.o" + srv_tgtobj="${srv_tgtobj} nat/x86-linux-tdesc.o" srv_tgtobj="${srv_tgtobj} nat/amd64-linux-siginfo.o" srv_linux_usrregs=yes # This is for i386 progs. srv_linux_regsets=yes diff --git a/gdbserver/i387-fp.cc b/gdbserver/i387-fp.cc index 62cafd87204..4d8bcb5edfa 100644 --- a/gdbserver/i387-fp.cc +++ b/gdbserver/i387-fp.cc @@ -21,7 +21,7 @@ #include "nat/x86-xstate.h" /* Default to SSE. */ -static unsigned long long x86_xcr0 = X86_XSTATE_SSE_MASK; +static uint64_t x86_xcr0 = X86_XSTATE_SSE_MASK; static const int num_mpx_bnd_registers = 4; static const int num_mpx_cfg_registers = 2; @@ -944,9 +944,8 @@ i387_xsave_to_cache (struct regcache *regcache, const void *buf) /* See i387-fp.h. */ -void -i387_set_xsave_mask (uint64_t xcr0, int len) +std::pair +i387_get_xsave_storage () { - x86_xcr0 = xcr0; - xsave_layout = x86_fetch_xsave_layout (xcr0, len); + return { &x86_xcr0, &xsave_layout }; } diff --git a/gdbserver/i387-fp.h b/gdbserver/i387-fp.h index 450466efb75..4ee21da8461 100644 --- a/gdbserver/i387-fp.h +++ b/gdbserver/i387-fp.h @@ -19,6 +19,8 @@ #ifndef GDBSERVER_I387_FP_H #define GDBSERVER_I387_FP_H +struct x86_xsave_layout; + void i387_cache_to_fsave (struct regcache *regcache, void *buf); void i387_fsave_to_cache (struct regcache *regcache, const void *buf); @@ -30,6 +32,6 @@ void i387_xsave_to_cache (struct regcache *regcache, const void *buf); /* Set the XSAVE mask and fetch the XSAVE layout via CPUID. */ -void i387_set_xsave_mask (uint64_t xcr0, int len); +std::pair i387_get_xsave_storage (); #endif /* GDBSERVER_I387_FP_H */ diff --git a/gdbserver/linux-amd64-ipa.cc b/gdbserver/linux-amd64-ipa.cc index a6346750f49..df5e6aca081 100644 --- a/gdbserver/linux-amd64-ipa.cc +++ b/gdbserver/linux-amd64-ipa.cc @@ -22,6 +22,7 @@ #include "tracepoint.h" #include "linux-x86-tdesc.h" #include "gdbsupport/x86-xstate.h" +#include "arch/amd64-linux-tdesc.h" /* fast tracepoints collect registers. */ diff --git a/gdbserver/linux-i386-ipa.cc b/gdbserver/linux-i386-ipa.cc index 8f14e0937d4..aa346fc9bc3 100644 --- a/gdbserver/linux-i386-ipa.cc +++ b/gdbserver/linux-i386-ipa.cc @@ -22,6 +22,7 @@ #include "tracepoint.h" #include "linux-x86-tdesc.h" #include "gdbsupport/x86-xstate.h" +#include "arch/i386-linux-tdesc.h" /* GDB register numbers. */ diff --git a/gdbserver/linux-x86-low.cc b/gdbserver/linux-x86-low.cc index 7937055c060..5d60f6b6fe1 100644 --- a/gdbserver/linux-x86-low.cc +++ b/gdbserver/linux-x86-low.cc @@ -29,8 +29,11 @@ #ifdef __x86_64__ #include "nat/amd64-linux-siginfo.h" +#include "arch/amd64-linux-tdesc.h" #endif +#include "arch/i386-linux-tdesc.h" + #include "gdb_proc_service.h" /* Don't include elf/common.h if linux/elf.h got included by gdb_proc_service.h. */ @@ -46,6 +49,7 @@ #include "nat/x86-linux.h" #include "nat/x86-linux-dregs.h" #include "linux-x86-tdesc.h" +#include "nat/x86-linux-tdesc.h" #ifdef __x86_64__ static target_desc_up tdesc_amd64_linux_no_xml; @@ -834,29 +838,12 @@ static int use_xml; /* Get Linux/x86 target description from running target. */ static const struct target_desc * -x86_linux_read_description (void) +x86_linux_read_description () { - unsigned int machine; - int is_elf64; - int xcr0_features; - int tid; - static uint64_t xcr0; - static int xsave_len; - struct regset_info *regset; - - tid = lwpid_of (current_thread); + int tid = lwpid_of (current_thread); - is_elf64 = linux_pid_exe_is_elf_64_file (tid, &machine); - - if (sizeof (void *) == 4) - { - if (is_elf64 > 0) - error (_("Can't debug 64-bit process with 32-bit GDBserver")); -#ifndef __x86_64__ - else if (machine == EM_X86_64) - error (_("Can't debug x86-64 process with 32-bit GDBserver")); -#endif - } + const char *error_msg + = _("Can't debug 64-bit process with 32-bit GDBserver"); /* If we are not allowed to send an XML target description then we need to use the hard-wired target descriptions. This corresponds to GDB's @@ -866,103 +853,53 @@ x86_linux_read_description (void) generate some alternative target descriptions. */ if (!use_xml) { + x86_linux_arch_size arch_size = x86_linux_ptrace_get_arch_size (tid); + bool is_64bit = arch_size.is_64bit (); + bool is_x32 = arch_size.is_x32 (); + + if (sizeof (void *) == 4 && is_64bit && !is_x32) + error ("%s", error_msg); + #ifdef __x86_64__ - if (machine == EM_X86_64) + if (is_64bit && !is_x32) return tdesc_amd64_linux_no_xml.get (); else #endif return tdesc_i386_linux_no_xml.get (); } -#if !defined __x86_64__ && defined HAVE_PTRACE_GETFPXREGS - if (machine == EM_386 && have_ptrace_getfpxregs == TRIBOOL_UNKNOWN) - { - elf_fpxregset_t fpxregs; - - if (ptrace (PTRACE_GETFPXREGS, tid, 0, (long) &fpxregs) < 0) - { - have_ptrace_getfpxregs = TRIBOOL_FALSE; - have_ptrace_getregset = TRIBOOL_FALSE; - return i386_linux_read_description (X86_XSTATE_X87); - } - else - have_ptrace_getfpxregs = TRIBOOL_TRUE; - } -#endif - - if (have_ptrace_getregset == TRIBOOL_UNKNOWN) - { - uint64_t xstateregs[(X86_XSTATE_SSE_SIZE / sizeof (uint64_t))]; - struct iovec iov; - - iov.iov_base = xstateregs; - iov.iov_len = sizeof (xstateregs); - - /* Check if PTRACE_GETREGSET works. */ - if (ptrace (PTRACE_GETREGSET, tid, - (unsigned int) NT_X86_XSTATE, (long) &iov) < 0) - have_ptrace_getregset = TRIBOOL_FALSE; - else - { - have_ptrace_getregset = TRIBOOL_TRUE; - - /* Get XCR0 from XSAVE extended state. */ - xcr0 = xstateregs[(I386_LINUX_XSAVE_XCR0_OFFSET - / sizeof (uint64_t))]; - - /* No MPX on x32. */ - if (machine == EM_X86_64 && !is_elf64) - xcr0 &= ~X86_XSTATE_MPX; - - xsave_len = x86_xsave_length (); - - /* Use PTRACE_GETREGSET if it is available. */ - for (regset = x86_regsets; - regset->fill_function != NULL; regset++) - if (regset->get_request == PTRACE_GETREGSET) - regset->size = xsave_len; - else if (regset->type != GENERAL_REGS) - regset->size = 0; - } - } - - /* Check the native XCR0 only if PTRACE_GETREGSET is available. */ - xcr0_features = (have_ptrace_getregset == TRIBOOL_TRUE - && (xcr0 & X86_XSTATE_ALL_MASK)); + /* If have_ptrace_getregset is changed to true by calling + x86_linux_tdesc_for_tid then we will perform some additional + initialisation. */ + bool have_ptrace_getregset_is_unknown + = have_ptrace_getregset == TRIBOOL_UNKNOWN; - if (xcr0_features) - i387_set_xsave_mask (xcr0, xsave_len); + /* Get pointers to where we should store the xcr0 and xsave_layout + values. These will be filled in by x86_linux_tdesc_for_tid the first + time that the function is called. Subsequent calls will not modify + the stored values. */ + std::pair storage + = i387_get_xsave_storage (); - if (machine == EM_X86_64) - { -#ifdef __x86_64__ - const target_desc *tdesc = NULL; + const target_desc *tdesc + = x86_linux_tdesc_for_tid (tid, error_msg, storage.first, storage.second); - if (xcr0_features) - { - tdesc = amd64_linux_read_description (xcr0 & X86_XSTATE_ALL_MASK, - !is_elf64); - } - - if (tdesc == NULL) - tdesc = amd64_linux_read_description (X86_XSTATE_SSE_MASK, !is_elf64); - return tdesc; -#endif - } - else + if (have_ptrace_getregset_is_unknown + && have_ptrace_getregset == TRIBOOL_TRUE) { - const target_desc *tdesc = NULL; - - if (xcr0_features) - tdesc = i386_linux_read_description (xcr0 & X86_XSTATE_ALL_MASK); - - if (tdesc == NULL) - tdesc = i386_linux_read_description (X86_XSTATE_SSE); - - return tdesc; + int xsave_len = x86_xsave_length (); + + /* Use PTRACE_GETREGSET if it is available. */ + for (regset_info *regset = x86_regsets; + regset->fill_function != nullptr; + regset++) + if (regset->get_request == PTRACE_GETREGSET) + regset->size = xsave_len; + else if (regset->type != GENERAL_REGS) + regset->size = 0; } - gdb_assert_not_reached ("failed to return tdesc"); + return tdesc; } /* Update all the target description of all processes; a new GDB diff --git a/gdbserver/linux-x86-tdesc.cc b/gdbserver/linux-x86-tdesc.cc index cd3b5d80e37..af3735aa895 100644 --- a/gdbserver/linux-x86-tdesc.cc +++ b/gdbserver/linux-x86-tdesc.cc @@ -23,8 +23,10 @@ #include "gdbsupport/x86-xstate.h" #ifdef __x86_64__ #include "arch/amd64.h" +#include "arch/amd64-linux-tdesc.h" #endif #include "x86-tdesc.h" +#include "arch/i386-linux-tdesc.h" /* Return the right x86_linux_tdesc index for a given XCR0. Return X86_TDESC_LAST if can't find a match. */ diff --git a/gdbserver/linux-x86-tdesc.h b/gdbserver/linux-x86-tdesc.h index f9561b129ae..576aaf5e165 100644 --- a/gdbserver/linux-x86-tdesc.h +++ b/gdbserver/linux-x86-tdesc.h @@ -46,11 +46,4 @@ int amd64_get_ipa_tdesc_idx (const struct target_desc *tdesc); const struct target_desc *i386_get_ipa_tdesc (int idx); -#ifdef __x86_64__ -const struct target_desc *amd64_linux_read_description (uint64_t xcr0, - bool is_x32); -#endif - -const struct target_desc *i386_linux_read_description (uint64_t xcr0); - #endif /* GDBSERVER_LINUX_X86_TDESC_H */ From patchwork Wed May 8 16:46:44 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Burgess X-Patchwork-Id: 89794 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 D8264382E82D for ; Wed, 8 May 2024 16:48:30 +0000 (GMT) X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 9F810384AB5D for ; Wed, 8 May 2024 16:47:05 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 9F810384AB5D Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 9F810384AB5D Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1715186832; cv=none; b=Q1tn+g0UJxLNpguPop63epdtFN41uRCPjtWCXF1eyGWaklRZs1v1pxj7J7CSHFEW8aqLhZQGcOE6xu4yVQ+f/Y3CLI1VqzweAHMZd/nXaPxlS1afCuxpuf+HW27stYdn9ovJulMghmMCTgepD+GvXORNGLMlKdXxx9c2M9/HcRc= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1715186832; c=relaxed/simple; bh=LFPcJBob+a0kPfJchJx7jWScuVR0kE5gkqoq10JXi+c=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=EarFnkqnYuVoV2NV4w8wbi6aPeZA6NFP0FKT/VR5hgWa0tisyC8oDj8MlTKLdhmv/D+Nq82vu0qAHqhUhYJjlhH/qUBZtdXW6lL96LjtIi2nFQNaH2T32rBuJD81aWrCRnva0wsc/9vaAF7fGEedClY20g34k4cRf9hCQCWiHQE= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1715186825; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=4QMTLf13dCguFA4ZsukvehhhfPVR4wj6J0t3euGgloE=; b=d/T/YF5OyCaKrXqbAA9rOzE4li9OZaBlJzQs5ho6M77l/cyMasot30HYhs9v1VsXtp5JIi VHQJ/4GW0EDdsxE6JM+i9V2lPq8Sm+rIRyquWS0vTJxlFcVylW2nskEvVrTLY8ssy9uB0x vD3yIlZa0Wg8olI9J0+yOhiG0pXXulo= Received: from mail-lj1-f200.google.com (mail-lj1-f200.google.com [209.85.208.200]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-287-gSS6dhqIMJWghBhpNqYNQw-1; Wed, 08 May 2024 12:47:03 -0400 X-MC-Unique: gSS6dhqIMJWghBhpNqYNQw-1 Received: by mail-lj1-f200.google.com with SMTP id 38308e7fff4ca-2e2da49e82aso45629921fa.3 for ; Wed, 08 May 2024 09:47:03 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715186819; x=1715791619; 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=4QMTLf13dCguFA4ZsukvehhhfPVR4wj6J0t3euGgloE=; b=SUggSe/ZzR4zIqyW/zoKr31za/jufLrEU10QcKqDSn0KfkNhLZP57SGMYj6hBpBXcw Rn7sG3ORnwSAqo4/UGB82wBLTQbIeeC0L7J03ocIrzEIi0hspJBvkEAyLXmZbNr4ABo6 ZMWpYeUk5cNEsJ/xhbndR3+3J78HZNfhb8iopv+NF0O6umazqUQxt4HNcR/tIKh9V5am s/gjB60C9Zo0AxeLBnGnhre9PEf9S7/X2I457lmLnW7QCqw6fm8LRnoNxoWhVK7kq3Sq 3x3Cm5w1czJIRn8JvQgVYzZbxgEvh3aPzSVzTexLx8kt7dlvf/0SeMoTi2U7J/d9I+YT VkyQ== X-Gm-Message-State: AOJu0Yyeh7ZIhIrTBWSXTVLHIkkAeuu1d+cF9KjBx8PmRGmItT7iUvIN KiHLSMkPIVgymqCsovqulTCJZZ3LQGFaI0TANBC4ccSevGH9HVlYSwi4coIWCNawuFRuKcKTHv0 JjVlag2v1n15aFgTNXVMXmxLlX0Pn9o3pKfcnmaJiDR5SDFKZiZkw/8AUa2vVEOsDvjzCQtfzVg l4uU/ER7Dr+rszNhZIgGQzFxfsEi7eiauc/MN///L5PvQ= X-Received: by 2002:a05:651c:2115:b0:2e3:3b4e:43ee with SMTP id 38308e7fff4ca-2e44738d29fmr25953681fa.19.1715186818031; Wed, 08 May 2024 09:46:58 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFpJPpe4/gDAfPoeVwSoTQze500dChsHWf6G+1pX0JzEk6QcqNthqH43PPPLmJyOGBaEAc6Hw== X-Received: by 2002:a05:651c:2115:b0:2e3:3b4e:43ee with SMTP id 38308e7fff4ca-2e44738d29fmr25953331fa.19.1715186817121; Wed, 08 May 2024 09:46:57 -0700 (PDT) Received: from localhost ([31.111.84.240]) by smtp.gmail.com with ESMTPSA id a7-20020adfed07000000b0034d839bed92sm15724079wro.64.2024.05.08.09.46.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 May 2024 09:46:56 -0700 (PDT) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess , felix.willgerodt@intel.com, jhb@FreeBSD.org Subject: [PATCHv6 8/9] gdbserver: update target description creation for x86/linux Date: Wed, 8 May 2024 17:46:44 +0100 Message-Id: <7aca2b7b9ccd5ea782a4be22f8f965678ddad38a.1715185683.git.aburgess@redhat.com> X-Mailer: git-send-email 2.25.4 In-Reply-To: References: MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.1 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, 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+patchwork=sourceware.org@sourceware.org This commit is part of a series which aims to share more of the target description creation between GDB and gdbserver for x86/Linux. After some refactoring earlier in this series the shared x86_linux_tdesc_for_tid function was added into nat/x86-linux-tdesc.c. However, this function still relies on amd64_linux_read_description and i386_linux_read_description which are implemented separately for both gdbserver and GDB. Given that at their core, all these functions do is: 1. take an xcr0 value as input, 2. mask out some feature bits, 3. look for a cached pre-generated target description and return it if found, 4. if no cached target description is found then call either amd64_create_target_description or i386_create_target_description to create a new target description, which is then added to the cache. Return the newly created target description. The inner functions amd64_create_target_description and i386_create_target_description are already shared between GDB and gdbserver (in the gdb/arch/ directory), so the only thing that the *_read_description functions really do is add the caching layer, and it feels like this really could be shared. However, we have a small problem. Despite using the same {amd64,i386}_create_target_description functions in both GDB and gdbserver to create the target descriptions, on the gdbserver side we cache target descriptions based on a reduced set of xcr0 feature bits. What this means is that, in theory, different xcr0 values can map to the same cache entry, which could result in the wrong target description being used. However, I'm not sure if this can actually happen in reality. Within gdbserver we already split the target description cache based on i386, amd64, and x32. I suspect within a given gdbserver session we'll only see at most one target description for each of these. The cache conflicting problem is caused by xcr0_to_tdesc_idx, which maps an xcr0 value to a enum x86_linux_tdesc value, and there are only 7 usable values in enum x86_linux_tdesc. In contrast, on the GDB side there are 64, 32, and 16 cache slots for i386, amd64, and x32 respectively. On the GDB side it is much more important to cache things correctly as a single GDB session might connect to multiple different remote targets, each of which might have slightly different x86 architectures. And so, if we want to merge the target description caching between GDB and gdbserver, then we need to first update gdbserver so that it caches in the same way as GDB, that is, it needs to adopt a mechanism that allows for the same number of cache slots of each of i386, amd64, and x32. In this way, when the caching is shared, GDB's behaviour will not change. Unfortunately it's a little more complex than that due to the in process agent (IPA). When the IPA is in use, gdbserver sends a target description index to the IPA, and the IPA uses this to find the correct target description to use, the IPA having first generated every possible target description. Interestingly, there is certainly a bug here which results from only having 7 values in enum x86_linux_tdesc. As multiple possible target descriptions in gdbserver map to the same enum x86_linux_tdesc value, then, when the enum x86_linux_tdesc value is sent to the IPA there is no way for gdbserver to know that the IPA will select the correct target description. This bug will get fixed by this commit. ** START OF AN ASIDE ** Back in the day I suspect this approach of sending a target description index made perfect sense. However since this commit: commit a8806230241d201f808d856eaae4d44088117b0c Date: Thu Dec 7 17:07:01 2017 +0000 Initialize target description early in IPA I think that passing an index was probably a bad idea. We used to pass the index, and then use that index to lookup which target description to instantiate and use, the target description was not generated until the index arrived. However, the above commit fixed an issue where we can't call malloc() within (certain parts of) the IPA (apparently), so instead we now pre-compute _every_ possible target description within the IPA. The index is only used to lookup which of the (many) pre-computed target descriptions to use. It would (I think) have been easier all around if the IPA just self-inspected, figured out its own xcr0 value, and used that to create the one target description that is required. So long as the xcr0 to target description code is shared (at compile time) with gdbserver, then we can be sure that the IPA will derive the same target description as gdbserver, and we would avoid all this index passing business, which has made this commit so very, very painful. I did look at how a process might derive its own xcr0 value, but I don't believe this is actually that simple, so for now I've just doubled down on the index passing approach. While reviewing earlier iterations of this patch there has been discussion about the possibility of removing the IPA from GDB. That would certainly make all of the code touched in this patch much simpler, but I don't really want to do that as part of this series. ** END OF AN ASIDE ** Currently then for x86/linux, gdbserver sends a number between 0 and 7 to the IPA, and the IPA uses this to create a target description. However, I am proposing that gdbserver should now create one of (up to) 64 different target descriptions for i386, so this 0 to 7 index isn't going to be good enough any more (amd64 and x32 have slightly fewer possible target descriptions, but still more than 8, so the problem is the same). For a while I wondered if I was going to have to try and find some backward compatible solution for this mess. But after seeing how lightly the IPA is actually documented, I wonder if it is not the case that there is a tight coupling between a version of gdbserver and a version of the IPA? At least I'm hoping so, and that's what I've assumed in this commit. In this commit I have thrown out the old IPA target description index numbering scheme, and switched to a completely new numbering scheme. Instead of the index that is passed being arbitrary, the index is instead calculated from the set of xcr0 features that are present on the target. Within the IPA we can then reverse this logic to recreate the xcr0 value based on the index, and from the xcr0 value we can choose the correct target description. With the gdbserver to IPA numbering scheme issue resolved I have then update the gdbserver versions of amd64_linux_read_description and i386_linux_read_description so that they cache target descriptions using the same set of xcr0 features as GDB itself. After this gdbserver should now always come up with the same target description as GDB does on any x86/Linux target. This commit does not introduce any new code sharing between GDB and gdbserver as previous commits in this series have done. Instead this commit is all about bringing GDB and gdbserver into alignment functionally so that the next commit(s) can merge the GDB and gdbserver versions of these functions. Notes On The Implementation --------------------------- Previously, within gdbserver, target descriptions were cached within arrays. These arrays were sized based on enum x86_linux_tdesc and xcr0_to_tdesc_idx returned the array (cache) index. Now we need different array lengths for each of i386, amd64, and x32. And the index to use within each array is calculated based on which xcr0 bits are set and valid for a particular target type. I really wanted to avoid having fixed array sizes, or having the set of relevant xcr0 bits encoded in multiple places. The solution I came up with was to create a single data structure which would contain a list of xcr0 bits along with flags to indicate which of the i386, amd64, and x32 targets the bit is relevant for. By making the table constexpr, and adding some constexpr helper functions, it is possible to calculate the sizes for the cache arrays at compile time, as well as the bit masks needed to each target type. During review it was pointed out[1] that possibly the failure to check the SSE and X87 bits for amd64/x32 targets might be an error, however, if this is the case then this is an issue that existed long before this patch. I'd really like to keep this patch focused on reworking the existing code and try to avoid changing how target descriptions are actually created, mostly out of fear that I'll break something. [1] https://inbox.sourceware.org/gdb-patches/MN2PR11MB4566070607318EE7E669A5E28E1B2@MN2PR11MB4566.namprd11.prod.outlook.com Approved-By: John Baldwin --- gdbserver/linux-amd64-ipa.cc | 43 +---- gdbserver/linux-i386-ipa.cc | 23 +-- gdbserver/linux-x86-low.cc | 24 ++- gdbserver/linux-x86-tdesc.cc | 335 ++++++++++++++++++++++++++--------- gdbserver/linux-x86-tdesc.h | 48 ++--- 5 files changed, 306 insertions(+), 167 deletions(-) diff --git a/gdbserver/linux-amd64-ipa.cc b/gdbserver/linux-amd64-ipa.cc index df5e6aca081..0c80812cc6f 100644 --- a/gdbserver/linux-amd64-ipa.cc +++ b/gdbserver/linux-amd64-ipa.cc @@ -164,47 +164,21 @@ supply_static_tracepoint_registers (struct regcache *regcache, #endif /* HAVE_UST */ -#if !defined __ILP32__ -/* Map the tdesc index to xcr0 mask. */ -static uint64_t idx2mask[X86_TDESC_LAST] = { - X86_XSTATE_X87_MASK, - X86_XSTATE_SSE_MASK, - X86_XSTATE_AVX_MASK, - X86_XSTATE_MPX_MASK, - X86_XSTATE_AVX_MPX_MASK, - X86_XSTATE_AVX_AVX512_MASK, - X86_XSTATE_AVX_MPX_AVX512_PKU_MASK, -}; -#endif - /* Return target_desc to use for IPA, given the tdesc index passed by gdbserver. */ const struct target_desc * get_ipa_tdesc (int idx) { - if (idx >= X86_TDESC_LAST) - { - internal_error ("unknown ipa tdesc index: %d", idx); - } + uint64_t xcr0 = x86_linux_tdesc_idx_to_xcr0 (idx); #if defined __ILP32__ - switch (idx) - { - case X86_TDESC_SSE: - return amd64_linux_read_description (X86_XSTATE_SSE_MASK, true); - case X86_TDESC_AVX: - return amd64_linux_read_description (X86_XSTATE_AVX_MASK, true); - case X86_TDESC_AVX_AVX512: - return amd64_linux_read_description (X86_XSTATE_AVX_AVX512_MASK, true); - default: - break; - } + bool is_x32 = true; #else - return amd64_linux_read_description (idx2mask[idx], false); + bool is_x32 = false; #endif - internal_error ("unknown ipa tdesc index: %d", idx); + return amd64_linux_read_description (xcr0, is_x32); } /* Allocate buffer for the jump pads. The branch instruction has a @@ -272,11 +246,10 @@ void initialize_low_tracepoint (void) { #if defined __ILP32__ - amd64_linux_read_description (X86_XSTATE_SSE_MASK, true); - amd64_linux_read_description (X86_XSTATE_AVX_MASK, true); - amd64_linux_read_description (X86_XSTATE_AVX_AVX512_MASK, true); + for (auto i = 0; i < x86_linux_x32_tdesc_count (); i++) + amd64_linux_read_description (x86_linux_tdesc_idx_to_xcr0 (i), true); #else - for (auto i = 0; i < X86_TDESC_LAST; i++) - amd64_linux_read_description (idx2mask[i], false); + for (auto i = 0; i < x86_linux_amd64_tdesc_count (); i++) + amd64_linux_read_description (x86_linux_tdesc_idx_to_xcr0 (i), false); #endif } diff --git a/gdbserver/linux-i386-ipa.cc b/gdbserver/linux-i386-ipa.cc index aa346fc9bc3..c1c3152fb04 100644 --- a/gdbserver/linux-i386-ipa.cc +++ b/gdbserver/linux-i386-ipa.cc @@ -245,28 +245,15 @@ initialize_fast_tracepoint_trampoline_buffer (void) } } -/* Map the tdesc index to xcr0 mask. */ -static uint64_t idx2mask[X86_TDESC_LAST] = { - X86_XSTATE_X87_MASK, - X86_XSTATE_SSE_MASK, - X86_XSTATE_AVX_MASK, - X86_XSTATE_MPX_MASK, - X86_XSTATE_AVX_MPX_MASK, - X86_XSTATE_AVX_AVX512_MASK, - X86_XSTATE_AVX_MPX_AVX512_PKU_MASK, -}; - /* Return target_desc to use for IPA, given the tdesc index passed by gdbserver. */ const struct target_desc * get_ipa_tdesc (int idx) { - if (idx >= X86_TDESC_LAST) - { - internal_error ("unknown ipa tdesc index: %d", idx); - } - return i386_linux_read_description (idx2mask[idx]); + uint64_t xcr0 = x86_linux_tdesc_idx_to_xcr0 (idx); + + return i386_linux_read_description (xcr0); } /* Allocate buffer for the jump pads. On i386, we can reach an arbitrary @@ -288,6 +275,6 @@ void initialize_low_tracepoint (void) { initialize_fast_tracepoint_trampoline_buffer (); - for (auto i = 0; i < X86_TDESC_LAST; i++) - i386_linux_read_description (idx2mask[i]); + for (auto i = 0; i < x86_linux_i386_tdesc_count (); i++) + i386_linux_read_description (x86_linux_tdesc_idx_to_xcr0 (i)); } diff --git a/gdbserver/linux-x86-low.cc b/gdbserver/linux-x86-low.cc index 5d60f6b6fe1..86dbe165eca 100644 --- a/gdbserver/linux-x86-low.cc +++ b/gdbserver/linux-x86-low.cc @@ -2876,14 +2876,28 @@ x86_target::get_ipa_tdesc_idx () struct regcache *regcache = get_thread_regcache (current_thread, 0); const struct target_desc *tdesc = regcache->tdesc; + if (!use_xml) + { + /* If USE_XML is false then we should be using one of these target + descriptions, see x86_linux_read_description for where we choose + one of these. Both of these descriptions are created from this + fixed xcr0 value X86_XSTATE_SSE_MASK. */ + gdb_assert (tdesc == tdesc_i386_linux_no_xml.get () #ifdef __x86_64__ - return amd64_get_ipa_tdesc_idx (tdesc); -#endif + || tdesc == tdesc_amd64_linux_no_xml.get () +#endif /* __x86_64__ */ + ); + return x86_linux_xcr0_to_tdesc_idx (X86_XSTATE_SSE_MASK); + } - if (tdesc == tdesc_i386_linux_no_xml.get ()) - return X86_TDESC_SSE; + /* The xcr0 value and xsave layout value are cached when the target + description is read. Grab their cache location, and use the cached + value to calculate a tdesc index. */ + std::pair storage + = i387_get_xsave_storage (); + uint64_t xcr0 = *storage.first; - return i386_get_ipa_tdesc_idx (tdesc); + return x86_linux_xcr0_to_tdesc_idx (xcr0); } /* The linux target ops object. */ diff --git a/gdbserver/linux-x86-tdesc.cc b/gdbserver/linux-x86-tdesc.cc index af3735aa895..f5636898d71 100644 --- a/gdbserver/linux-x86-tdesc.cc +++ b/gdbserver/linux-x86-tdesc.cc @@ -28,96 +28,297 @@ #include "x86-tdesc.h" #include "arch/i386-linux-tdesc.h" -/* Return the right x86_linux_tdesc index for a given XCR0. Return - X86_TDESC_LAST if can't find a match. */ +/* A structure used to describe a single xstate feature bit that might, or + might not, be checked for when creating a target description for one of + i386, amd64, or x32. -static enum x86_linux_tdesc -xcr0_to_tdesc_idx (uint64_t xcr0, bool is_x32) + The different CPU/ABI types check for different xstate features when + creating a target description. + + We want to cache target descriptions, and this is currently done in + three separate caches, one each for i386, amd64, and x32. Additionally, + the caching we're discussing here is Linux only, and for Linux, the only + thing that has an impact on target description creation is the xcr0 + value. + + In order to ensure the cache functions correctly we need to filter out + only those xcr0 feature bits that are relevant, we can then cache target + descriptions based on the relevant feature bits. Two xcr0 values might + be different, but have the same relevant feature bits. In this case we + would expect the two xcr0 values to map to the same cache entry. */ + +struct x86_xstate_feature { + /* The xstate feature mask. This is a mask against an xcr0 value. */ + uint64_t feature; + + /* Is this feature checked when creating an i386 target description. */ + bool is_i386; + + /* Is this feature checked when creating an amd64 target description. */ + bool is_amd64; + + /* Is this feature checked when creating an x32 target description. */ + bool is_x32; +}; + +/* A constant table that describes all of the xstate features that are + checked when building a target description for i386, amd64, or x32. + + If in the future, due to simplifications or refactoring, this table ever + ends up with 'true' for every xcr0 feature on every target type, then this + is an indication that this table should probably be removed, and that the + rest of the code in this file can be simplified. */ + +static constexpr x86_xstate_feature x86_linux_all_xstate_features[] = { + /* Feature, i386, amd64, x32. */ + { X86_XSTATE_PKRU, true, true, true }, + { X86_XSTATE_AVX512, true, true, true }, + { X86_XSTATE_AVX, true, true, true }, + { X86_XSTATE_MPX, true, true, false }, + { X86_XSTATE_SSE, true, false, false }, + { X86_XSTATE_X87, true, false, false } +}; + +/* Return a compile time constant which is a mask of all the xstate features + that are checked for when building an i386 target description. */ + +static constexpr uint64_t +x86_linux_i386_xcr0_feature_mask () { - if (xcr0 & X86_XSTATE_PKRU) - { - if (is_x32) - { - /* No x32 MPX and PKU, fall back to avx_avx512. */ - return X86_TDESC_AVX_AVX512; - } - else - return X86_TDESC_AVX_MPX_AVX512_PKU; - } - else if (xcr0 & X86_XSTATE_AVX512) - return X86_TDESC_AVX_AVX512; - else if ((xcr0 & X86_XSTATE_AVX_MPX_MASK) == X86_XSTATE_AVX_MPX_MASK) + uint64_t mask = 0; + + for (const auto &entry : x86_linux_all_xstate_features) + if (entry.is_i386) + mask |= entry.feature; + + return mask; +} + +/* Return a compile time constant which is a mask of all the xstate features + that are checked for when building an amd64 target description. */ + +static constexpr uint64_t +x86_linux_amd64_xcr0_feature_mask () +{ + uint64_t mask = 0; + + for (const auto &entry : x86_linux_all_xstate_features) + if (entry.is_amd64) + mask |= entry.feature; + + return mask; +} + +/* Return a compile time constant which is a mask of all the xstate features + that are checked for when building an x32 target description. */ + +static constexpr uint64_t +x86_linux_x32_xcr0_feature_mask () +{ + uint64_t mask = 0; + + for (const auto &entry : x86_linux_all_xstate_features) + if (entry.is_x32) + mask |= entry.feature; + + return mask; +} + +/* Return a compile time constant which is a count of the number of xstate + features that are checked for when building an i386 target description. */ + +static constexpr int +x86_linux_i386_tdesc_count_1 () +{ + uint64_t count = 0; + + for (const auto &entry : x86_linux_all_xstate_features) + if (entry.is_i386) + ++count; + + gdb_assert (count > 0); + + return (1 << count); +} + +/* Return a compile time constant which is a count of the number of xstate + features that are checked for when building an amd64 target description. */ + +static constexpr int +x86_linux_amd64_tdesc_count_1 () +{ + uint64_t count = 0; + + for (const auto &entry : x86_linux_all_xstate_features) + if (entry.is_amd64) + ++count; + + gdb_assert (count > 0); + + return (1 << count); +} + +/* Return a compile time constant which is a count of the number of xstate + features that are checked for when building an x32 target description. */ + +static constexpr int +x86_linux_x32_tdesc_count_1 () +{ + uint64_t count = 0; + + for (const auto &entry : x86_linux_all_xstate_features) + if (entry.is_x32) + ++count; + + gdb_assert (count > 0); + + return (1 << count); +} + +#ifdef IN_PROCESS_AGENT + +/* See linux-x86-tdesc.h. */ + +int +x86_linux_amd64_tdesc_count () +{ + return x86_linux_amd64_tdesc_count_1 (); +} + +/* See linux-x86-tdesc.h. */ + +int +x86_linux_x32_tdesc_count () +{ + return x86_linux_x32_tdesc_count_1 (); +} + +/* See linux-x86-tdesc.h. */ + +int +x86_linux_i386_tdesc_count () +{ + return x86_linux_i386_tdesc_count_1 (); +} + +#endif /* IN_PROCESS_AGENT */ + +/* Convert an xcr0 value into an integer. The integer will be passed to + the in-process-agent where it will then be passed to + x86_linux_tdesc_idx_to_xcr0 to get back the xcr0 value. */ + +int +x86_linux_xcr0_to_tdesc_idx (uint64_t xcr0) +{ + /* The following table shows which features are checked for when creating + the target descriptions (see nat/x86-linux-tdesc.c), the feature order + represents the bit order within the generated index number. + + i386 | x87 sse mpx avx avx512 pkru + amd64 | mpx avx avx512 pkru + i32 | avx avx512 pkru + + The features are ordered so that for each mode (i386, amd64, i32) the + generated index will form a continuous range. */ + + int idx = 0; + + for (int i = 0; i < ARRAY_SIZE (x86_linux_all_xstate_features); ++i) { - if (is_x32) /* No MPX on x32. */ - return X86_TDESC_AVX; - else - return X86_TDESC_AVX_MPX; + if ((xcr0 & x86_linux_all_xstate_features[i].feature) + == x86_linux_all_xstate_features[i].feature) + idx |= (1 << i); } - else if (xcr0 & X86_XSTATE_MPX) + + return idx; +} + +#ifdef IN_PROCESS_AGENT + +/* Convert an index number (as returned from x86_linux_xcr0_to_tdesc_idx) + into an xcr0 value which can then be used to create a target + description. */ + +uint64_t +x86_linux_tdesc_idx_to_xcr0 (int idx) +{ + uint64_t xcr0 = 0; + + for (int i = 0; i < ARRAY_SIZE (x86_linux_all_xstate_features); ++i) { - if (is_x32) /* No MPX on x32. */ - return X86_TDESC_AVX; - else - return X86_TDESC_MPX; + if ((idx & (1 << i)) != 0) + xcr0 |= x86_linux_all_xstate_features[i].feature; } - else if (xcr0 & X86_XSTATE_AVX) - return X86_TDESC_AVX; - else if (xcr0 & X86_XSTATE_SSE) - return X86_TDESC_SSE; - else if (xcr0 & X86_XSTATE_X87) - return X86_TDESC_MMX; - else - return X86_TDESC_LAST; + + return xcr0; } +#endif /* IN_PROCESS_AGENT */ + #if defined __i386__ || !defined IN_PROCESS_AGENT -static struct target_desc *i386_tdescs[X86_TDESC_LAST] = { }; +/* A cache of all possible i386 target descriptions. */ -/* Return the target description according to XCR0. */ +static struct target_desc *i386_tdescs[x86_linux_i386_tdesc_count_1 ()] = { }; + +/* See nat/x86-linux-tdesc.h. */ const struct target_desc * i386_linux_read_description (uint64_t xcr0) { - enum x86_linux_tdesc idx = xcr0_to_tdesc_idx (xcr0, false); + xcr0 &= x86_linux_i386_xcr0_feature_mask (); + int idx = x86_linux_xcr0_to_tdesc_idx (xcr0); - if (idx == X86_TDESC_LAST) - return NULL; + gdb_assert (idx >= 0 && idx < x86_linux_i386_tdesc_count_1 ()); - struct target_desc **tdesc = &i386_tdescs[idx]; + target_desc **tdesc = &i386_tdescs[idx]; - if (*tdesc == NULL) + if (*tdesc == nullptr) { *tdesc = i386_create_target_description (xcr0, true, false); init_target_desc (*tdesc, i386_expedite_regs); } - return *tdesc;; + return *tdesc; } #endif #ifdef __x86_64__ -static target_desc *amd64_tdescs[X86_TDESC_LAST] = { }; -static target_desc *x32_tdescs[X86_TDESC_LAST] = { }; +/* A cache of all possible amd64 target descriptions. */ + +static target_desc *amd64_tdescs[x86_linux_amd64_tdesc_count_1 ()] = { }; + +/* A cache of all possible x32 target descriptions. */ + +static target_desc *x32_tdescs[x86_linux_x32_tdesc_count_1 ()] = { }; + +/* See nat/x86-linux-tdesc.h. */ const struct target_desc * amd64_linux_read_description (uint64_t xcr0, bool is_x32) { - enum x86_linux_tdesc idx = xcr0_to_tdesc_idx (xcr0, is_x32); + if (is_x32) + xcr0 &= x86_linux_x32_xcr0_feature_mask (); + else + xcr0 &= x86_linux_amd64_xcr0_feature_mask (); + + int idx = x86_linux_xcr0_to_tdesc_idx (xcr0); - if (idx == X86_TDESC_LAST) - return NULL; + if (is_x32) + gdb_assert (idx >= 0 && idx < x86_linux_x32_tdesc_count_1 ()); + else + gdb_assert (idx >= 0 && idx < x86_linux_amd64_tdesc_count_1 ()); - struct target_desc **tdesc = NULL; + target_desc **tdesc = nullptr; if (is_x32) tdesc = &x32_tdescs[idx]; else tdesc = &amd64_tdescs[idx]; - if (*tdesc == NULL) + if (*tdesc == nullptr) { *tdesc = amd64_create_target_description (xcr0, is_x32, true, true); @@ -127,39 +328,3 @@ amd64_linux_read_description (uint64_t xcr0, bool is_x32) } #endif - -#ifndef IN_PROCESS_AGENT - -int -i386_get_ipa_tdesc_idx (const struct target_desc *tdesc) -{ - for (int i = 0; i < X86_TDESC_LAST; i++) - { - if (tdesc == i386_tdescs[i]) - return i; - } - - /* If none tdesc is found, return the one with minimum features. */ - return X86_TDESC_MMX; -} - -#if defined __x86_64__ -int -amd64_get_ipa_tdesc_idx (const struct target_desc *tdesc) -{ - for (int i = 0; i < X86_TDESC_LAST; i++) - { - if (tdesc == amd64_tdescs[i]) - return i; - } - for (int i = 0; i < X86_TDESC_LAST; i++) - { - if (tdesc == x32_tdescs[i]) - return i; - } - - return X86_TDESC_SSE; -} - -#endif -#endif diff --git a/gdbserver/linux-x86-tdesc.h b/gdbserver/linux-x86-tdesc.h index 576aaf5e165..c06af21da48 100644 --- a/gdbserver/linux-x86-tdesc.h +++ b/gdbserver/linux-x86-tdesc.h @@ -21,29 +21,29 @@ #ifndef GDBSERVER_LINUX_X86_TDESC_H #define GDBSERVER_LINUX_X86_TDESC_H -/* Note: since IPA obviously knows what ABI it's running on (i386 vs x86_64 - vs x32), it's sufficient to pass only the register set here. This, - together with the ABI known at IPA compile time, maps to a tdesc. */ - -enum x86_linux_tdesc { - X86_TDESC_MMX = 0, - X86_TDESC_SSE = 1, - X86_TDESC_AVX = 2, - X86_TDESC_MPX = 3, - X86_TDESC_AVX_MPX = 4, - X86_TDESC_AVX_AVX512 = 5, - X86_TDESC_AVX_MPX_AVX512_PKU = 6, - X86_TDESC_LAST = 7, -}; - -#if defined __i386__ || !defined IN_PROCESS_AGENT -int i386_get_ipa_tdesc_idx (const struct target_desc *tdesc); -#endif - -#if defined __x86_64__ && !defined IN_PROCESS_AGENT -int amd64_get_ipa_tdesc_idx (const struct target_desc *tdesc); -#endif - -const struct target_desc *i386_get_ipa_tdesc (int idx); +/* Convert an xcr0 value into an integer. The integer will be passed to + the in-process-agent where it will then be passed to + x86_linux_tdesc_idx_to_xcr0 to get back the xcr0 value. */ + +extern int x86_linux_xcr0_to_tdesc_idx (uint64_t xcr0); + +#ifdef IN_PROCESS_AGENT + +/* Convert an index number (as returned from x86_linux_xcr0_to_tdesc_idx) + into an xcr0 value which can then be used to create a target + description. */ + +extern uint64_t x86_linux_tdesc_idx_to_xcr0 (int idx); + +/* Within the in-process-agent we need to pre-initialise all of the target + descriptions, to do this we need to know how many target descriptions + there are for each different target type. These functions return the + target description count for the relevant target. */ + +extern int x86_linux_amd64_tdesc_count (); +extern int x86_linux_x32_tdesc_count (); +extern int x86_linux_i386_tdesc_count (); + +#endif /* IN_PROCESS_AGENT */ #endif /* GDBSERVER_LINUX_X86_TDESC_H */ From patchwork Wed May 8 16:46:45 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Burgess X-Patchwork-Id: 89798 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 8AD33384AB63 for ; Wed, 8 May 2024 16:49:17 +0000 (GMT) X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 33E943883006 for ; Wed, 8 May 2024 16:47:07 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 33E943883006 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 33E943883006 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1715186833; cv=none; b=kYU3XqMIfS+RjhZhhM6gw1rDeajwHnZJd1x7ApVREVjeIr0jjgMB1yOB7IMXUwjm1DjUim+ssMJVG3cYOZq//pRs8Zti5M0X1LFK5l8ghAyQWctWgZX8H4E67346votPKSmne3cmP5TudxuiO37xy8AmvDKcMD+p8/zpnSahtUg= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1715186833; c=relaxed/simple; bh=stgmAMy1kyWoUddnaRRrsI2kELxzc788SdWku1m1fMo=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=YT+UA2qVFxjhUbw/E8OoGKlho9VqF93biUl6DsP2q4ARZjdmqwJXovIe1IHDZDhrlp4bV3JxIjLAjNWPbVa33pqRG0VndhtNcU0M/W3C32HtKJvHUnIxiLj6wkuiUEtaKDTcwmjd48V/JHVLpSODw1ZUHJraml9UiDykOvmnn3w= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1715186826; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=8J/XrCC/NbeUz6i2LZSCX+JHVT1eWmaDty+rG1dws30=; b=OWm91IHqEnYP0DDrLV/ivEcHwvqGen6a1rEifscm5fhGZMUWUr9IxxyCmJdaWdgyjXsuGj hyaLeyqT8HQ0aP/UIH5OrP8RIlhUVoBZO1PyP86lCEzfc6IuKL3ZBUubOoCySiBp4sNmwR KoQUi2A4AtvjWkuMJYtPixFsRWIW8zg= Received: from mail-lj1-f197.google.com (mail-lj1-f197.google.com [209.85.208.197]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-434-B21wpauINHK4PELtaM82Yw-1; Wed, 08 May 2024 12:47:04 -0400 X-MC-Unique: B21wpauINHK4PELtaM82Yw-1 Received: by mail-lj1-f197.google.com with SMTP id 38308e7fff4ca-2e482d3d8b3so6587491fa.2 for ; Wed, 08 May 2024 09:47:04 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715186823; x=1715791623; 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=8J/XrCC/NbeUz6i2LZSCX+JHVT1eWmaDty+rG1dws30=; b=ixjXdvduYJtzeS4leXubDqYiXYaCLnuXWmVuKLLyaoDL/8i9nvrdvuiTXEbd/6Ya36 +eXIukQ9dDjDPrDB+PX7FJODCY28uQ+fCML2CwpBzHgEFac/ig26AXra2zTzhkGRfqm+ Yu5qSyQvUPdH8KtLwFaGqvK4pv6NWwzc9kLaOqsrMxOKLngJOoGZA2J6QlI+uUfSVUH1 1WcltZ/qI41PPZ/zgu8HvgKZ5IcefYXSzKLwDzmYPMkyL5bPfKM5SrTM40jfvKLjqAbm UafSp58SEdbOYi8MzN33F2Ndyhmcc8A//0yILsO/sozlhbjVkBnM1sh/ntideUvfFBGn L3AA== X-Gm-Message-State: AOJu0YxGRZ593qjOCmqj1mfvA0obGXcRzV/t23i79nmqOD5dKHIPeTa/ WkDrHp3OIbWyzarrZ+cTqncjSLWuuenYM1FEfaK/rQaVTvT07Eq/DhRqGgFGyrsKoEcqTV31jKe w6mMcpb7zxsNeeXSBE6IWxoIIbhpaoAld0XuhRO0jEKvhy+K8Vaji/LD+cWIDwcb7vAQXluur9i YSWqca015pdTSW2Cdk6v3AkXm0CYyOuvbcOkRI43Qsogk= X-Received: by 2002:a2e:9496:0:b0:2e1:a15b:b504 with SMTP id 38308e7fff4ca-2e4476afa8fmr19904701fa.37.1715186822244; Wed, 08 May 2024 09:47:02 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEKxs7esXn7fvsorz5TIRUnVB4wmtFbgmLGITY8BED9t3VIrH/VM1iKZ8w8O7hk4bNuPFXvpQ== X-Received: by 2002:a2e:9496:0:b0:2e1:a15b:b504 with SMTP id 38308e7fff4ca-2e4476afa8fmr19904331fa.37.1715186821104; Wed, 08 May 2024 09:47:01 -0700 (PDT) Received: from localhost ([31.111.84.240]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-41f87d2045asm28894585e9.27.2024.05.08.09.46.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 May 2024 09:46:58 -0700 (PDT) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess , felix.willgerodt@intel.com, jhb@FreeBSD.org Subject: [PATCHv6 9/9] gdb/gdbserver: share x86/linux tdesc caching Date: Wed, 8 May 2024 17:46:45 +0100 Message-Id: <6c060cb9063a9e83a07b49000a666cdb6698be9e.1715185683.git.aburgess@redhat.com> X-Mailer: git-send-email 2.25.4 In-Reply-To: References: MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.1 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, 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+patchwork=sourceware.org@sourceware.org This commit builds on the previous series of commits to share the target description caching code between GDB and gdbserver for x86/Linux targets. The objective of this commit is to move the four functions (2 each of) i386_linux_read_description and amd64_linux_read_description into the gdb/arch/ directory and combine them so we have just a single copy of each. Then GDB, gdbserver, and the in-process-agent (IPA) will link against these shared functions. One curiosity with this patch is the function x86_linux_post_init_tdesc. On the gdbserver side the two functions amd64_linux_read_description and i386_linux_read_description have some functionality that is not present on the GDB side, there is some additional configuration that is performed as each target description is created, to setup the expedited registers. To support this I've added the function x86_linux_post_init_tdesc. This function is called from the two *_linux_read_description functions, but is implemented separately for GDB and gdbserver. An alternative approach that avoids adding x86_linux_post_init_tdesc would be to have x86_linux_tdesc_for_tid return a non-const target description, then in x86_target::low_arch_setup we could inspect the target description to figure out if it is 64-bit or not, and modify the target description as needed. In the end I think that adding the x86_linux_post_init_tdesc function is the simpler solution. The contents of gdbserver/linux-x86-low.cc have moved to gdb/arch/x86-linux-tdesc-features.c, and gdbserver/linux-x86-tdesc.h has moved to gdb/arch/x86-linux-tdesc-features.h, this change leads to some updates in the #includes in the gdbserver/ directory. This commit also changes how target descriptions are cached. Previously both GDB and gdbserver used static C-style arrays to act as the tdesc cache. This was fine, except for two problems. Either the C-style arrays would need to be placed in x86-linux-tdesc-features.c, which would allow us to use the x86_linux_*_tdesc_count_1() functions to size the arrays for us, or we'd need to hard code the array sizes using separate #defines, which we'd then have to keep in sync with the rest of the code in x86-linux-tdesc-features.c. Given both of these problems I decided a better solution would be to just switch to using a std::unordered_map to act as the cache. This will resize automatically, and we can use the xcr0 value as the key. At first inspection, using xcr0 might seem to be a problem; after all the {i386,amd64}_create_target_description functions take more than just the xcr0 value. However, this patch is only for x86/Linux targets, and for x86/Linux all of the other flags passed to the tdesc creation functions have constant values and so are irrelevant when we consider tdesc caching. For testing I've done the following: - Built on x86-64 GNU/Linux for all targets, and just for the native target, - Build on i386 GNU/Linux for all targets, and just for the native target, - Build on a 64-bit, non-x86 GNU/Linux for all targets, just for the native target, and for targets x86_64-*-linux and i386-*-linux. --- gdb/Makefile.in | 5 + gdb/amd64-linux-tdep.c | 31 -- gdb/arch/amd64-linux-tdesc.c | 61 ++++ gdb/arch/i386-linux-tdesc.c | 51 +++ gdb/arch/x86-linux-tdesc-features.c | 267 +++++++++++++++ .../arch/x86-linux-tdesc-features.h | 49 ++- gdb/arch/x86-linux-tdesc.h | 37 +++ gdb/configure.nat | 6 +- gdb/configure.tgt | 11 +- gdb/i386-linux-tdep.c | 26 +- gdbserver/configure.srv | 9 + gdbserver/linux-amd64-ipa.cc | 2 +- gdbserver/linux-i386-ipa.cc | 2 +- gdbserver/linux-x86-low.cc | 2 +- gdbserver/linux-x86-tdesc.cc | 311 +----------------- 15 files changed, 489 insertions(+), 381 deletions(-) create mode 100644 gdb/arch/amd64-linux-tdesc.c create mode 100644 gdb/arch/i386-linux-tdesc.c create mode 100644 gdb/arch/x86-linux-tdesc-features.c rename gdbserver/linux-x86-tdesc.h => gdb/arch/x86-linux-tdesc-features.h (52%) create mode 100644 gdb/arch/x86-linux-tdesc.h diff --git a/gdb/Makefile.in b/gdb/Makefile.in index 9c2c69b02ab..800021551b9 100644 --- a/gdb/Makefile.in +++ b/gdb/Makefile.in @@ -748,6 +748,7 @@ ALL_64_TARGET_OBS = \ arch/aarch64-insn.o \ arch/aarch64-mte-linux.o \ arch/aarch64-scalable-linux.o \ + arch/amd64-linux-tdesc.o \ arch/amd64.o \ arch/riscv.o \ bpf-tdep.o \ @@ -788,9 +789,11 @@ ALL_TARGET_OBS = \ arch/arm.o \ arch/arm-get-next-pcs.o \ arch/arm-linux.o \ + arch/i386-linux-tdesc.o \ arch/i386.o \ arch/loongarch.o \ arch/ppc-linux-common.o \ + arch/x86-linux-tdesc-features.o \ arm-bsd-tdep.o \ arm-fbsd-tdep.o \ arm-linux-tdep.o \ @@ -1559,6 +1562,8 @@ HFILES_NO_SRCDIR = \ arch/ppc-linux-common.h \ arch/ppc-linux-tdesc.h \ arch/riscv.h \ + arch/x86-linux-tdesc-features.h \ + arch/x86-linux-tdesc.h \ cli/cli-cmds.h \ cli/cli-decode.h \ cli/cli-script.h \ diff --git a/gdb/amd64-linux-tdep.c b/gdb/amd64-linux-tdep.c index f9647dce1e6..d7662cac572 100644 --- a/gdb/amd64-linux-tdep.c +++ b/gdb/amd64-linux-tdep.c @@ -1578,37 +1578,6 @@ amd64_linux_record_signal (struct gdbarch *gdbarch, return 0; } -const target_desc * -amd64_linux_read_description (uint64_t xcr0_features_bit, bool is_x32) -{ - static target_desc *amd64_linux_tdescs \ - [2/*AVX*/][2/*MPX*/][2/*AVX512*/][2/*PKRU*/] = {}; - static target_desc *x32_linux_tdescs \ - [2/*AVX*/][2/*AVX512*/][2/*PKRU*/] = {}; - - target_desc **tdesc; - - if (is_x32) - { - tdesc = &x32_linux_tdescs[(xcr0_features_bit & X86_XSTATE_AVX) ? 1 : 0 ] - [(xcr0_features_bit & X86_XSTATE_AVX512) ? 1 : 0] - [(xcr0_features_bit & X86_XSTATE_PKRU) ? 1 : 0]; - } - else - { - tdesc = &amd64_linux_tdescs[(xcr0_features_bit & X86_XSTATE_AVX) ? 1 : 0] - [(xcr0_features_bit & X86_XSTATE_MPX) ? 1 : 0] - [(xcr0_features_bit & X86_XSTATE_AVX512) ? 1 : 0] - [(xcr0_features_bit & X86_XSTATE_PKRU) ? 1 : 0]; - } - - if (*tdesc == NULL) - *tdesc = amd64_create_target_description (xcr0_features_bit, is_x32, - true, true); - - return *tdesc; -} - /* Get Linux/x86 target description from core dump. */ static const struct target_desc * diff --git a/gdb/arch/amd64-linux-tdesc.c b/gdb/arch/amd64-linux-tdesc.c new file mode 100644 index 00000000000..794cd5c815d --- /dev/null +++ b/gdb/arch/amd64-linux-tdesc.c @@ -0,0 +1,61 @@ +/* Target description related code for GNU/Linux x86-64. + + Copyright (C) 2024 Free Software Foundation, Inc. + + This file is part of GDB. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + +#include "arch/x86-linux-tdesc.h" +#include "arch/amd64-linux-tdesc.h" +#include "arch/amd64.h" +#include "arch/x86-linux-tdesc-features.h" + + +/* See arch/amd64-linux-tdesc.h. */ + +const struct target_desc * +amd64_linux_read_description (uint64_t xcr0, bool is_x32) +{ + /* The type used for the amd64 and x32 target description caches. */ + using tdesc_cache_type = std::unordered_map; + + /* Caches for the previously seen amd64 and x32 target descriptions, + indexed by the xcr0 value that created the target description. These + need to be static within this function to ensure they are initialised + before first use. */ + static tdesc_cache_type amd64_tdesc_cache, x32_tdesc_cache; + + tdesc_cache_type &tdesc_cache = is_x32 ? x32_tdesc_cache : amd64_tdesc_cache; + + xcr0 &= is_x32 + ? x86_linux_x32_xcr0_feature_mask () + : x86_linux_amd64_xcr0_feature_mask (); + + const auto it = tdesc_cache.find (xcr0); + if (it != tdesc_cache.end ()) + return it->second.get (); + + /* Create the previously unseen target description. */ + target_desc_up tdesc (amd64_create_target_description (xcr0, is_x32, + true, true)); + x86_linux_post_init_tdesc (tdesc.get (), true); + + /* Add to the cache, and return a pointer borrowed from the + target_desc_up. This is safe as the cache (and the pointers contained + within it) are not deleted until GDB exits. */ + target_desc *ptr = tdesc.get (); + tdesc_cache.emplace (xcr0, std::move (tdesc)); + return ptr; +} diff --git a/gdb/arch/i386-linux-tdesc.c b/gdb/arch/i386-linux-tdesc.c new file mode 100644 index 00000000000..78b0cef841e --- /dev/null +++ b/gdb/arch/i386-linux-tdesc.c @@ -0,0 +1,51 @@ +/* Target description related code for GNU/Linux i386. + + Copyright (C) 2024 Free Software Foundation, Inc. + + This file is part of GDB. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + +#include "arch/x86-linux-tdesc.h" +#include "arch/i386-linux-tdesc.h" +#include "arch/i386.h" +#include "arch/x86-linux-tdesc-features.h" + +/* See arch/i386-linux-tdesc.h. */ + +const target_desc * +i386_linux_read_description (uint64_t xcr0) +{ + /* Cache of previously seen i386 target descriptions, indexed by the xcr0 + value that created the target description. This needs to be static + within this function to ensure it is initialised before first use. */ + static std::unordered_map i386_tdesc_cache; + + xcr0 &= x86_linux_i386_xcr0_feature_mask (); + + const auto it = i386_tdesc_cache.find (xcr0); + if (it != i386_tdesc_cache.end ()) + return it->second.get (); + + /* Create the previously unseen target description. */ + target_desc_up tdesc (i386_create_target_description (xcr0, true, false)); + x86_linux_post_init_tdesc (tdesc.get (), false); + + /* Add to the cache, and return a pointer borrowed from the + target_desc_up. This is safe as the cache (and the pointers contained + within it) are not deleted until GDB exits. */ + target_desc *ptr = tdesc.get (); + i386_tdesc_cache.emplace (xcr0, std::move (tdesc)); + return ptr; +} diff --git a/gdb/arch/x86-linux-tdesc-features.c b/gdb/arch/x86-linux-tdesc-features.c new file mode 100644 index 00000000000..55444d91bd5 --- /dev/null +++ b/gdb/arch/x86-linux-tdesc-features.c @@ -0,0 +1,267 @@ +/* Target description related code for GNU/Linux x86 (i386 and x86-64). + + Copyright (C) 2024 Free Software Foundation, Inc. + + This file is part of GDB. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + +#include "arch/x86-linux-tdesc-features.h" + +/* A structure used to describe a single xstate feature bit that might, or + might not, be checked for when creating a target description for one of + i386, amd64, or x32. + + The different CPU/ABI types check for different xstate features when + creating a target description. + + We want to cache target descriptions, and this is currently done in + three separate caches, one each for i386, amd64, and x32. Additionally, + the caching we're discussing here is Linux only, and for Linux, the only + thing that has an impact on target description creation is the xcr0 + value. + + In order to ensure the cache functions correctly we need to filter out + only those xcr0 feature bits that are relevant, we can then cache target + descriptions based on the relevant feature bits. Two xcr0 values might + be different, but have the same relevant feature bits. In this case we + would expect the two xcr0 values to map to the same cache entry. */ + +struct x86_xstate_feature { + /* The xstate feature mask. This is a mask against an xcr0 value. */ + uint64_t feature; + + /* Is this feature checked when creating an i386 target description. */ + bool is_i386; + + /* Is this feature checked when creating an amd64 target description. */ + bool is_amd64; + + /* Is this feature checked when creating an x32 target description. */ + bool is_x32; +}; + +/* A constant table that describes all of the xstate features that are + checked when building a target description for i386, amd64, or x32. + + If in the future, due to simplifications or refactoring, this table ever + ends up with 'true' for every xcr0 feature on every target type, then this + is an indication that this table should probably be removed, and that the + rest of the code in this file can be simplified. */ + +static constexpr x86_xstate_feature x86_linux_all_xstate_features[] = { + /* Feature, i386, amd64, x32. */ + { X86_XSTATE_PKRU, true, true, true }, + { X86_XSTATE_AVX512, true, true, true }, + { X86_XSTATE_AVX, true, true, true }, + { X86_XSTATE_MPX, true, true, false }, + { X86_XSTATE_SSE, true, false, false }, + { X86_XSTATE_X87, true, false, false } +}; + +/* Return a compile time constant which is a mask of all the xstate features + that are checked for when building an i386 target description. */ + +static constexpr uint64_t +x86_linux_i386_xcr0_feature_mask_1 () +{ + uint64_t mask = 0; + + for (const auto &entry : x86_linux_all_xstate_features) + if (entry.is_i386) + mask |= entry.feature; + + return mask; +} + +/* Return a compile time constant which is a mask of all the xstate features + that are checked for when building an amd64 target description. */ + +static constexpr uint64_t +x86_linux_amd64_xcr0_feature_mask_1 () +{ + uint64_t mask = 0; + + for (const auto &entry : x86_linux_all_xstate_features) + if (entry.is_amd64) + mask |= entry.feature; + + return mask; +} + +/* Return a compile time constant which is a mask of all the xstate features + that are checked for when building an x32 target description. */ + +static constexpr uint64_t +x86_linux_x32_xcr0_feature_mask_1 () +{ + uint64_t mask = 0; + + for (const auto &entry : x86_linux_all_xstate_features) + if (entry.is_x32) + mask |= entry.feature; + + return mask; +} + +/* See arch/x86-linux-tdesc-features.h. */ + +uint64_t +x86_linux_amd64_xcr0_feature_mask () +{ + return x86_linux_amd64_xcr0_feature_mask_1 (); +} + +/* See arch/x86-linux-tdesc-features.h. */ + +uint64_t +x86_linux_x32_xcr0_feature_mask () +{ + return x86_linux_x32_xcr0_feature_mask_1 (); +} + +/* See arch/x86-linux-tdesc-features.h. */ + +uint64_t +x86_linux_i386_xcr0_feature_mask () +{ + return x86_linux_i386_xcr0_feature_mask_1 (); +} + +#ifdef GDBSERVER + +/* See arch/x86-linux-tdesc-features.h. */ + +int +x86_linux_xcr0_to_tdesc_idx (uint64_t xcr0) +{ + /* The following table shows which features are checked for when creating + the target descriptions (see nat/x86-linux-tdesc.c), the feature order + represents the bit order within the generated index number. + + i386 | x87 sse mpx avx avx512 pkru + amd64 | mpx avx avx512 pkru + i32 | avx avx512 pkru + + The features are ordered so that for each mode (i386, amd64, i32) the + generated index will form a continuous range. */ + + int idx = 0; + + for (int i = 0; i < ARRAY_SIZE (x86_linux_all_xstate_features); ++i) + { + if ((xcr0 & x86_linux_all_xstate_features[i].feature) + == x86_linux_all_xstate_features[i].feature) + idx |= (1 << i); + } + + return idx; +} + +#endif /* GDBSERVER */ + +#ifdef IN_PROCESS_AGENT + +/* Return a compile time constant which is a count of the number of xstate + features that are checked for when building an i386 target description. */ + +static constexpr int +x86_linux_i386_tdesc_count_1 () +{ + uint64_t count = 0; + + for (const auto &entry : x86_linux_all_xstate_features) + if (entry.is_i386) + ++count; + + gdb_assert (count > 0); + + return (1 << count); +} + +/* Return a compile time constant which is a count of the number of xstate + features that are checked for when building an amd64 target description. */ + +static constexpr int +x86_linux_amd64_tdesc_count_1 () +{ + uint64_t count = 0; + + for (const auto &entry : x86_linux_all_xstate_features) + if (entry.is_amd64) + ++count; + + gdb_assert (count > 0); + + return (1 << count); +} + +/* Return a compile time constant which is a count of the number of xstate + features that are checked for when building an x32 target description. */ + +static constexpr int +x86_linux_x32_tdesc_count_1 () +{ + uint64_t count = 0; + + for (const auto &entry : x86_linux_all_xstate_features) + if (entry.is_x32) + ++count; + + gdb_assert (count > 0); + + return (1 << count); +} + +/* See arch/x86-linux-tdesc-features.h. */ + +int +x86_linux_amd64_tdesc_count () +{ + return x86_linux_amd64_tdesc_count_1 (); +} + +/* See arch/x86-linux-tdesc-features.h. */ + +int +x86_linux_x32_tdesc_count () +{ + return x86_linux_x32_tdesc_count_1 (); +} + +/* See arch/x86-linux-tdesc-features.h. */ + +int +x86_linux_i386_tdesc_count () +{ + return x86_linux_i386_tdesc_count_1 (); +} + +/* See arch/x86-linux-tdesc-features.h. */ + +uint64_t +x86_linux_tdesc_idx_to_xcr0 (int idx) +{ + uint64_t xcr0 = 0; + + for (int i = 0; i < ARRAY_SIZE (x86_linux_all_xstate_features); ++i) + { + if ((idx & (1 << i)) != 0) + xcr0 |= x86_linux_all_xstate_features[i].feature; + } + + return xcr0; +} + +#endif /* IN_PROCESS_AGENT */ diff --git a/gdbserver/linux-x86-tdesc.h b/gdb/arch/x86-linux-tdesc-features.h similarity index 52% rename from gdbserver/linux-x86-tdesc.h rename to gdb/arch/x86-linux-tdesc-features.h index c06af21da48..fb0017e46db 100644 --- a/gdbserver/linux-x86-tdesc.h +++ b/gdb/arch/x86-linux-tdesc-features.h @@ -1,7 +1,6 @@ -/* Low level support for x86 (i386 and x86-64), shared between gdbserver - and IPA. +/* Target description related code for GNU/Linux x86 (i386 and x86-64). - Copyright (C) 2016-2024 Free Software Foundation, Inc. + Copyright (C) 2024 Free Software Foundation, Inc. This file is part of GDB. @@ -18,32 +17,46 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . */ -#ifndef GDBSERVER_LINUX_X86_TDESC_H -#define GDBSERVER_LINUX_X86_TDESC_H +#ifndef ARCH_X86_LINUX_TDESC_FEATURES_H +#define ARCH_X86_LINUX_TDESC_FEATURES_H -/* Convert an xcr0 value into an integer. The integer will be passed to - the in-process-agent where it will then be passed to - x86_linux_tdesc_idx_to_xcr0 to get back the xcr0 value. */ +#include "gdbsupport/x86-xstate.h" +#include "gdbsupport/gdb_assert.h" + +/* Return a mask of X86_STATE_* feature flags. The returned mask indicates + the set of features which are checked for when creating the target + description for each of amd64, x32, and i386. */ + +extern uint64_t x86_linux_amd64_xcr0_feature_mask (); +extern uint64_t x86_linux_x32_xcr0_feature_mask (); +extern uint64_t x86_linux_i386_xcr0_feature_mask (); + +#ifdef GDBSERVER + +/* Convert an xcr0 value into an integer. The integer will be passed from + gdbserver to the in-process-agent where it will then be passed through + x86_linux_tdesc_idx_to_xcr0 to get back the original xcr0 value. */ extern int x86_linux_xcr0_to_tdesc_idx (uint64_t xcr0); +#endif /* GDBSERVER */ + #ifdef IN_PROCESS_AGENT +/* Return the maximum possible number of target descriptions for each of + amd64, x32, and i386. These are used by the in-process-agent to + generate every possible target description. */ + +extern int x86_linux_amd64_tdesc_count (); +extern int x86_linux_x32_tdesc_count (); +extern int x86_linux_i386_tdesc_count (); + /* Convert an index number (as returned from x86_linux_xcr0_to_tdesc_idx) into an xcr0 value which can then be used to create a target description. */ extern uint64_t x86_linux_tdesc_idx_to_xcr0 (int idx); -/* Within the in-process-agent we need to pre-initialise all of the target - descriptions, to do this we need to know how many target descriptions - there are for each different target type. These functions return the - target description count for the relevant target. */ - -extern int x86_linux_amd64_tdesc_count (); -extern int x86_linux_x32_tdesc_count (); -extern int x86_linux_i386_tdesc_count (); - #endif /* IN_PROCESS_AGENT */ -#endif /* GDBSERVER_LINUX_X86_TDESC_H */ +#endif /* ARCH_X86_LINUX_TDESC_FEATURES_H */ diff --git a/gdb/arch/x86-linux-tdesc.h b/gdb/arch/x86-linux-tdesc.h new file mode 100644 index 00000000000..152592fcf76 --- /dev/null +++ b/gdb/arch/x86-linux-tdesc.h @@ -0,0 +1,37 @@ +/* Target description related code for GNU/Linux x86 (i386 and x86-64). + + Copyright (C) 2024 Free Software Foundation, Inc. + + This file is part of GDB. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + +#ifndef ARCH_X86_LINUX_TDESC_H +#define ARCH_X86_LINUX_TDESC_H + +struct target_desc; + +/* This function is called from amd64_linux_read_description and + i386_linux_read_description after a new target description has been + created, TDESC is the new target description, IS_64BIT will be true + when called from amd64_linux_read_description, otherwise IS_64BIT will + be false. If the *_linux_read_description functions found a cached + target description then this function will not be called. + + Both GDB and gdbserver have their own implementations of this + function. */ + +extern void x86_linux_post_init_tdesc (target_desc *tdesc, bool is_64bit); + +#endif /* ARCH_X86_LINUX_TDESC_H */ diff --git a/gdb/configure.nat b/gdb/configure.nat index 4bcc0696027..24e1824b01c 100644 --- a/gdb/configure.nat +++ b/gdb/configure.nat @@ -256,7 +256,8 @@ case ${gdb_host} in NATDEPFILES="${NATDEPFILES} x86-nat.o nat/x86-dregs.o \ nat/x86-xstate.o \ i386-linux-nat.o x86-linux-nat.o nat/linux-btrace.o \ - nat/x86-linux.o nat/x86-linux-dregs.o nat/x86-linux-tdesc.o" + nat/x86-linux.o nat/x86-linux-dregs.o nat/x86-linux-tdesc.o \ + arch/i386-linux-tdesc.o arch/x86-linux-tdesc-features.o" ;; ia64) # Host: Intel IA-64 running GNU/Linux @@ -323,7 +324,8 @@ case ${gdb_host} in nat/x86-xstate.o amd64-nat.o amd64-linux-nat.o x86-linux-nat.o \ nat/linux-btrace.o \ nat/x86-linux.o nat/x86-linux-dregs.o nat/x86-linux-tdesc.o \ - nat/amd64-linux-siginfo.o" + nat/amd64-linux-siginfo.o arch/x86-linux-tdesc-features.o \ + arch/i386-linux-tdesc.o arch/amd64-linux-tdesc.o" ;; sparc) # Host: GNU/Linux UltraSPARC diff --git a/gdb/configure.tgt b/gdb/configure.tgt index 47a674201f9..8326c458eb1 100644 --- a/gdb/configure.tgt +++ b/gdb/configure.tgt @@ -320,10 +320,13 @@ i[34567]86-*-linux*) gdb_target_obs="i386-linux-tdep.o \ glibc-tdep.o \ solib-svr4.o symfile-mem.o \ - linux-tdep.o linux-record.o" + linux-tdep.o linux-record.o \ + arch/i386-linux-tdesc.o \ + arch/x86-linux-tdesc-features.o" if test "x$enable_64_bit_bfd" = "xyes"; then # Target: GNU/Linux x86-64 - gdb_target_obs="amd64-linux-tdep.o ${gdb_target_obs}" + gdb_target_obs="amd64-linux-tdep.o \ + arch/amd64-linux-tdesc.o ${gdb_target_obs}" fi ;; i[34567]86-*-gnu*) @@ -718,7 +721,9 @@ x86_64-*-linux*) # Target: GNU/Linux x86-64 gdb_target_obs="amd64-linux-tdep.o ${i386_tobjs} \ i386-linux-tdep.o glibc-tdep.o \ - solib-svr4.o symfile-mem.o linux-tdep.o linux-record.o" + solib-svr4.o symfile-mem.o linux-tdep.o linux-record.o \ + arch/i386-linux-tdesc.o arch/amd64-linux-tdesc.o \ + arch/x86-linux-tdesc-features.o" ;; x86_64-*-freebsd* | x86_64-*-kfreebsd*-gnu) # Target: FreeBSD/amd64 diff --git a/gdb/i386-linux-tdep.c b/gdb/i386-linux-tdep.c index 6a7490680c1..6debed21b89 100644 --- a/gdb/i386-linux-tdep.c +++ b/gdb/i386-linux-tdep.c @@ -41,6 +41,7 @@ #include "i387-tdep.h" #include "gdbsupport/x86-xstate.h" #include "arch/i386-linux-tdesc.h" +#include "arch/x86-linux-tdesc.h" /* The syscall's XML filename for i386. */ #define XML_SYSCALL_FILENAME_I386 "syscalls/i386-linux.xml" @@ -680,29 +681,12 @@ i386_linux_core_read_x86_xsave_layout (struct gdbarch *gdbarch, layout) != 0; } -/* See i386-linux-tdep.h. */ +/* See arch/x86-linux-tdesc.h. */ -const struct target_desc * -i386_linux_read_description (uint64_t xcr0) +void +x86_linux_post_init_tdesc (target_desc *tdesc, bool is_64bit) { - if (xcr0 == 0) - return NULL; - - static struct target_desc *i386_linux_tdescs \ - [2/*X87*/][2/*SSE*/][2/*AVX*/][2/*MPX*/][2/*AVX512*/][2/*PKRU*/] = {}; - struct target_desc **tdesc; - - tdesc = &i386_linux_tdescs[(xcr0 & X86_XSTATE_X87) ? 1 : 0] - [(xcr0 & X86_XSTATE_SSE) ? 1 : 0] - [(xcr0 & X86_XSTATE_AVX) ? 1 : 0] - [(xcr0 & X86_XSTATE_MPX) ? 1 : 0] - [(xcr0 & X86_XSTATE_AVX512) ? 1 : 0] - [(xcr0 & X86_XSTATE_PKRU) ? 1 : 0]; - - if (*tdesc == NULL) - *tdesc = i386_create_target_description (xcr0, true, false); - - return *tdesc; + /* Nothing. */ } /* Get Linux/x86 target description from core dump. */ diff --git a/gdbserver/configure.srv b/gdbserver/configure.srv index 7a2702d78bf..e17b5cf280c 100644 --- a/gdbserver/configure.srv +++ b/gdbserver/configure.srv @@ -110,12 +110,16 @@ case "${gdbserver_host}" in srv_tgtobj="${srv_tgtobj} nat/x86-linux.o" srv_tgtobj="${srv_tgtobj} nat/x86-linux-dregs.o" srv_tgtobj="${srv_tgtobj} nat/x86-linux-tdesc.o" + srv_tgtobj="${srv_tgtobj} arch/x86-linux-tdesc-features.o" + srv_tgtobj="${srv_tgtobj} arch/i386-linux-tdesc.o" srv_linux_usrregs=yes srv_linux_regsets=yes srv_linux_thread_db=yes srv_linux_btrace=yes ipa_obj="linux-i386-ipa.o linux-x86-tdesc-ipa.o" ipa_obj="${ipa_obj} arch/i386-ipa.o" + ipa_obj="${ipa_obj} arch/x86-linux-tdesc-features-ipa.o" + ipa_obj="${ipa_obj} arch/i386-linux-tdesc-ipa.o" ;; i[34567]86-*-mingw*) srv_regobj="" srv_tgtobj="x86-low.o nat/x86-dregs.o win32-low.o" @@ -374,12 +378,17 @@ case "${gdbserver_host}" in srv_tgtobj="${srv_tgtobj} nat/x86-linux-dregs.o" srv_tgtobj="${srv_tgtobj} nat/x86-linux-tdesc.o" srv_tgtobj="${srv_tgtobj} nat/amd64-linux-siginfo.o" + srv_tgtobj="${srv_tgtobj} arch/x86-linux-tdesc-features.o" + srv_tgtobj="${srv_tgtobj} arch/amd64-linux-tdesc.o" + srv_tgtobj="${srv_tgtobj} arch/i386-linux-tdesc.o" srv_linux_usrregs=yes # This is for i386 progs. srv_linux_regsets=yes srv_linux_thread_db=yes srv_linux_btrace=yes ipa_obj="linux-amd64-ipa.o linux-x86-tdesc-ipa.o" ipa_obj="${ipa_obj} arch/amd64-ipa.o" + ipa_obj="${ipa_obj} arch/x86-linux-tdesc-features-ipa.o" + ipa_obj="${ipa_obj} arch/amd64-linux-tdesc-ipa.o" ;; x86_64-*-mingw*) srv_regobj="" srv_tgtobj="x86-low.o nat/x86-dregs.o" diff --git a/gdbserver/linux-amd64-ipa.cc b/gdbserver/linux-amd64-ipa.cc index 0c80812cc6f..89857f208b8 100644 --- a/gdbserver/linux-amd64-ipa.cc +++ b/gdbserver/linux-amd64-ipa.cc @@ -20,9 +20,9 @@ #include #include "tracepoint.h" -#include "linux-x86-tdesc.h" #include "gdbsupport/x86-xstate.h" #include "arch/amd64-linux-tdesc.h" +#include "arch/x86-linux-tdesc-features.h" /* fast tracepoints collect registers. */ diff --git a/gdbserver/linux-i386-ipa.cc b/gdbserver/linux-i386-ipa.cc index c1c3152fb04..8100c9f9840 100644 --- a/gdbserver/linux-i386-ipa.cc +++ b/gdbserver/linux-i386-ipa.cc @@ -20,9 +20,9 @@ #include #include "tracepoint.h" -#include "linux-x86-tdesc.h" #include "gdbsupport/x86-xstate.h" #include "arch/i386-linux-tdesc.h" +#include "arch/x86-linux-tdesc-features.h" /* GDB register numbers. */ diff --git a/gdbserver/linux-x86-low.cc b/gdbserver/linux-x86-low.cc index 86dbe165eca..5c28fec6df6 100644 --- a/gdbserver/linux-x86-low.cc +++ b/gdbserver/linux-x86-low.cc @@ -33,6 +33,7 @@ #endif #include "arch/i386-linux-tdesc.h" +#include "arch/x86-linux-tdesc-features.h" #include "gdb_proc_service.h" /* Don't include elf/common.h if linux/elf.h got included by @@ -48,7 +49,6 @@ #include "nat/linux-nat.h" #include "nat/x86-linux.h" #include "nat/x86-linux-dregs.h" -#include "linux-x86-tdesc.h" #include "nat/x86-linux-tdesc.h" #ifdef __x86_64__ diff --git a/gdbserver/linux-x86-tdesc.cc b/gdbserver/linux-x86-tdesc.cc index f5636898d71..13c80762605 100644 --- a/gdbserver/linux-x86-tdesc.cc +++ b/gdbserver/linux-x86-tdesc.cc @@ -17,314 +17,19 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . */ +#include "arch/x86-linux-tdesc.h" #include "tdesc.h" -#include "linux-x86-tdesc.h" -#include "arch/i386.h" -#include "gdbsupport/x86-xstate.h" -#ifdef __x86_64__ -#include "arch/amd64.h" -#include "arch/amd64-linux-tdesc.h" -#endif #include "x86-tdesc.h" -#include "arch/i386-linux-tdesc.h" - -/* A structure used to describe a single xstate feature bit that might, or - might not, be checked for when creating a target description for one of - i386, amd64, or x32. - - The different CPU/ABI types check for different xstate features when - creating a target description. - - We want to cache target descriptions, and this is currently done in - three separate caches, one each for i386, amd64, and x32. Additionally, - the caching we're discussing here is Linux only, and for Linux, the only - thing that has an impact on target description creation is the xcr0 - value. - - In order to ensure the cache functions correctly we need to filter out - only those xcr0 feature bits that are relevant, we can then cache target - descriptions based on the relevant feature bits. Two xcr0 values might - be different, but have the same relevant feature bits. In this case we - would expect the two xcr0 values to map to the same cache entry. */ - -struct x86_xstate_feature { - /* The xstate feature mask. This is a mask against an xcr0 value. */ - uint64_t feature; - - /* Is this feature checked when creating an i386 target description. */ - bool is_i386; - - /* Is this feature checked when creating an amd64 target description. */ - bool is_amd64; - - /* Is this feature checked when creating an x32 target description. */ - bool is_x32; -}; - -/* A constant table that describes all of the xstate features that are - checked when building a target description for i386, amd64, or x32. - - If in the future, due to simplifications or refactoring, this table ever - ends up with 'true' for every xcr0 feature on every target type, then this - is an indication that this table should probably be removed, and that the - rest of the code in this file can be simplified. */ - -static constexpr x86_xstate_feature x86_linux_all_xstate_features[] = { - /* Feature, i386, amd64, x32. */ - { X86_XSTATE_PKRU, true, true, true }, - { X86_XSTATE_AVX512, true, true, true }, - { X86_XSTATE_AVX, true, true, true }, - { X86_XSTATE_MPX, true, true, false }, - { X86_XSTATE_SSE, true, false, false }, - { X86_XSTATE_X87, true, false, false } -}; - -/* Return a compile time constant which is a mask of all the xstate features - that are checked for when building an i386 target description. */ - -static constexpr uint64_t -x86_linux_i386_xcr0_feature_mask () -{ - uint64_t mask = 0; - - for (const auto &entry : x86_linux_all_xstate_features) - if (entry.is_i386) - mask |= entry.feature; - - return mask; -} - -/* Return a compile time constant which is a mask of all the xstate features - that are checked for when building an amd64 target description. */ - -static constexpr uint64_t -x86_linux_amd64_xcr0_feature_mask () -{ - uint64_t mask = 0; - - for (const auto &entry : x86_linux_all_xstate_features) - if (entry.is_amd64) - mask |= entry.feature; - - return mask; -} - -/* Return a compile time constant which is a mask of all the xstate features - that are checked for when building an x32 target description. */ - -static constexpr uint64_t -x86_linux_x32_xcr0_feature_mask () -{ - uint64_t mask = 0; - - for (const auto &entry : x86_linux_all_xstate_features) - if (entry.is_x32) - mask |= entry.feature; - - return mask; -} - -/* Return a compile time constant which is a count of the number of xstate - features that are checked for when building an i386 target description. */ - -static constexpr int -x86_linux_i386_tdesc_count_1 () -{ - uint64_t count = 0; - - for (const auto &entry : x86_linux_all_xstate_features) - if (entry.is_i386) - ++count; - - gdb_assert (count > 0); - - return (1 << count); -} - -/* Return a compile time constant which is a count of the number of xstate - features that are checked for when building an amd64 target description. */ - -static constexpr int -x86_linux_amd64_tdesc_count_1 () -{ - uint64_t count = 0; - - for (const auto &entry : x86_linux_all_xstate_features) - if (entry.is_amd64) - ++count; - - gdb_assert (count > 0); - - return (1 << count); -} - -/* Return a compile time constant which is a count of the number of xstate - features that are checked for when building an x32 target description. */ - -static constexpr int -x86_linux_x32_tdesc_count_1 () -{ - uint64_t count = 0; - - for (const auto &entry : x86_linux_all_xstate_features) - if (entry.is_x32) - ++count; - - gdb_assert (count > 0); - - return (1 << count); -} - -#ifdef IN_PROCESS_AGENT - -/* See linux-x86-tdesc.h. */ - -int -x86_linux_amd64_tdesc_count () -{ - return x86_linux_amd64_tdesc_count_1 (); -} - -/* See linux-x86-tdesc.h. */ - -int -x86_linux_x32_tdesc_count () -{ - return x86_linux_x32_tdesc_count_1 (); -} - -/* See linux-x86-tdesc.h. */ - -int -x86_linux_i386_tdesc_count () -{ - return x86_linux_i386_tdesc_count_1 (); -} - -#endif /* IN_PROCESS_AGENT */ - -/* Convert an xcr0 value into an integer. The integer will be passed to - the in-process-agent where it will then be passed to - x86_linux_tdesc_idx_to_xcr0 to get back the xcr0 value. */ - -int -x86_linux_xcr0_to_tdesc_idx (uint64_t xcr0) -{ - /* The following table shows which features are checked for when creating - the target descriptions (see nat/x86-linux-tdesc.c), the feature order - represents the bit order within the generated index number. - - i386 | x87 sse mpx avx avx512 pkru - amd64 | mpx avx avx512 pkru - i32 | avx avx512 pkru - - The features are ordered so that for each mode (i386, amd64, i32) the - generated index will form a continuous range. */ - - int idx = 0; - - for (int i = 0; i < ARRAY_SIZE (x86_linux_all_xstate_features); ++i) - { - if ((xcr0 & x86_linux_all_xstate_features[i].feature) - == x86_linux_all_xstate_features[i].feature) - idx |= (1 << i); - } - - return idx; -} - -#ifdef IN_PROCESS_AGENT - -/* Convert an index number (as returned from x86_linux_xcr0_to_tdesc_idx) - into an xcr0 value which can then be used to create a target - description. */ - -uint64_t -x86_linux_tdesc_idx_to_xcr0 (int idx) -{ - uint64_t xcr0 = 0; - - for (int i = 0; i < ARRAY_SIZE (x86_linux_all_xstate_features); ++i) - { - if ((idx & (1 << i)) != 0) - xcr0 |= x86_linux_all_xstate_features[i].feature; - } - - return xcr0; -} - -#endif /* IN_PROCESS_AGENT */ - -#if defined __i386__ || !defined IN_PROCESS_AGENT -/* A cache of all possible i386 target descriptions. */ +/* See arch/x86-linux-tdesc.h. */ -static struct target_desc *i386_tdescs[x86_linux_i386_tdesc_count_1 ()] = { }; - -/* See nat/x86-linux-tdesc.h. */ - -const struct target_desc * -i386_linux_read_description (uint64_t xcr0) +void +x86_linux_post_init_tdesc (target_desc *tdesc, bool is_64bit) { - xcr0 &= x86_linux_i386_xcr0_feature_mask (); - int idx = x86_linux_xcr0_to_tdesc_idx (xcr0); - - gdb_assert (idx >= 0 && idx < x86_linux_i386_tdesc_count_1 ()); - - target_desc **tdesc = &i386_tdescs[idx]; - - if (*tdesc == nullptr) - { - *tdesc = i386_create_target_description (xcr0, true, false); - - init_target_desc (*tdesc, i386_expedite_regs); - } - - return *tdesc; -} -#endif - #ifdef __x86_64__ - -/* A cache of all possible amd64 target descriptions. */ - -static target_desc *amd64_tdescs[x86_linux_amd64_tdesc_count_1 ()] = { }; - -/* A cache of all possible x32 target descriptions. */ - -static target_desc *x32_tdescs[x86_linux_x32_tdesc_count_1 ()] = { }; - -/* See nat/x86-linux-tdesc.h. */ - -const struct target_desc * -amd64_linux_read_description (uint64_t xcr0, bool is_x32) -{ - if (is_x32) - xcr0 &= x86_linux_x32_xcr0_feature_mask (); - else - xcr0 &= x86_linux_amd64_xcr0_feature_mask (); - - int idx = x86_linux_xcr0_to_tdesc_idx (xcr0); - - if (is_x32) - gdb_assert (idx >= 0 && idx < x86_linux_x32_tdesc_count_1 ()); - else - gdb_assert (idx >= 0 && idx < x86_linux_amd64_tdesc_count_1 ()); - - target_desc **tdesc = nullptr; - - if (is_x32) - tdesc = &x32_tdescs[idx]; + if (is_64bit) + init_target_desc (tdesc, amd64_expedite_regs); else - tdesc = &amd64_tdescs[idx]; - - if (*tdesc == nullptr) - { - *tdesc = amd64_create_target_description (xcr0, is_x32, true, true); - - init_target_desc (*tdesc, amd64_expedite_regs); - } - return *tdesc; -} - #endif + init_target_desc (tdesc, i386_expedite_regs); +}