From patchwork Tue Mar 5 17:00:16 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Burgess X-Patchwork-Id: 86823 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 10F05385800C for ; Tue, 5 Mar 2024 17:01:08 +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 352143858D20 for ; Tue, 5 Mar 2024 17:00:34 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 352143858D20 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 352143858D20 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=1709658036; cv=none; b=SxPApz5BfWmSo0H7KDXiRr+pH8PxWn7LvG5yty/Iye2peU1Jfc56P3qFKGlmlN7AsupD4A1xfHA+w4KC+IcFam2s4bMNlJwgJARc+og3JO/uabU/1SwF2b24yms5OpXLDNA4ShTiF3beBr2IOrOJLJ6XAw6IFu9qpcLQGKIil3A= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1709658036; c=relaxed/simple; bh=DPNramscNL1WdEhJAM9Tt/xtG0cGZN4H3qfwzMKH1ns=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=QYpNbokMUwgzpin/qoRFyQFcGh1MVNMFUYN4vrrwz2/FWMIfmD/nhc3M7Vuwi5IuKrGGe2xzsMYAExtyoXKN41yT7RjNh7x6xR5vu9EPjE3NIz5Y9yOij5wO6lRlD20BRYJQKUbt1RnsoJAvE3xL/aUxIpT0uG+D/Mvx9YK5x88= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1709658033; 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=ceYBdM0LCcpy8bDpXJnPJUnU0rGleVF/tiJ5HyPajLU=; b=FQmWnlAFJ9g5fV4NJYSsZElNrkQ28LsRr6RNwbYC+2rybsMx+dc303xGZRjlOfEG12NIdn zelHwrMpLn0QTNHBrkjom9LmdxM1VZe2J5KKyjwUegeK3qjjHdnxzNb1W+CKtpoT8QPb3a ELoFiptYGYfCDQBpJlVQc7wwCIowLd4= Received: from mail-ej1-f72.google.com (mail-ej1-f72.google.com [209.85.218.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-240-yL37AWjmNzCcvNjfkRO3bw-1; Tue, 05 Mar 2024 12:00:30 -0500 X-MC-Unique: yL37AWjmNzCcvNjfkRO3bw-1 Received: by mail-ej1-f72.google.com with SMTP id a640c23a62f3a-a45b35386bbso9188366b.1 for ; Tue, 05 Mar 2024 09:00:30 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709658028; x=1710262828; 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=ceYBdM0LCcpy8bDpXJnPJUnU0rGleVF/tiJ5HyPajLU=; b=pbFzZ7+AmqEiOZOVqilmuJ1lHaikRm/Y/ZI1oXOyEFKdhwKLjI/FMyJ463G6o/WPl2 ocs/rSfMmSczkexAdcEwOl4TxLnxDkpz9u5EaNu1C4uz1HA1wdux9Rpby5OHI/90PyBZ 1ulkXaqWjbuOUM/4xrCg3rDTA1kicxG4AkkUUIZl6OuKNZHNoxd8vGSUAfJxuBlX5fSe KquNrFUB2XXkyBdlW4aAafhBHxqCX2FU3tdPRxWGqlfEZKAemZ5/2MXOb65k6oDGsbAp RDgZxmSj6vN9Ah37nSPFQouXSL8/wEm14Ws7g8zytjvdGLW8uTcRiwlYbTFDCf8PQq4H hdwQ== X-Gm-Message-State: AOJu0YwGE84yP9oIZYaXnmmP6P1X94uxcix3rBOREvwiphC3FKS06V3u 6EmYDE88hweMpUPJSJQtUGEB+a7ojdiYvMVfu4i0KvKNXd4W3AGUBcJzDzbrRfP1HH6UeVHbeg/ s9rlNrDy7iD3F5/ZzbKQprPfW+BA/bR//pBrSKQuhxaPTYc0+q4GAFdTkVPbDnkrsql4RqaDiWg IK1eCP/xxyysNe1O2JT7MG7IOHMZwajuu2LnqtQYBNJYs= X-Received: by 2002:a17:906:d156:b0:a41:392d:e11c with SMTP id br22-20020a170906d15600b00a41392de11cmr10736832ejb.26.1709658028617; Tue, 05 Mar 2024 09:00:28 -0800 (PST) X-Google-Smtp-Source: AGHT+IGplxxPeFfM0IoXZ9cXqIYBsKPxd3PWObu8kgYT+7pgQWlXHfGuiIPbpiZvaKPdsQB+5LFVbA== X-Received: by 2002:a17:906:d156:b0:a41:392d:e11c with SMTP id br22-20020a170906d15600b00a41392de11cmr10736821ejb.26.1709658028212; Tue, 05 Mar 2024 09:00:28 -0800 (PST) Received: from localhost (185.223.159.143.dyn.plus.net. [143.159.223.185]) by smtp.gmail.com with ESMTPSA id os26-20020a170906af7a00b00a450b817705sm3340867ejb.154.2024.03.05.09.00.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 05 Mar 2024 09:00:26 -0800 (PST) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess Subject: [PATCHv2 1/7] gdbserver: convert have_ptrace_getregset to a tribool Date: Tue, 5 Mar 2024 17:00:16 +0000 Message-Id: <98d3c669f728dd2cfb601d4f09eff13475aac7ca.1709657954.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.3 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, T_SCC_BODY_TEXT_LINE 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 Convert the have_ptrace_getregset global within gdbserver to a tribool. This brings the flag into alignment with the corresponding flag in GDB. The gdbserver have_ptrace_getregset variable is already used as a tribool, it just doesn't have the tribool type. In a future commit I plan to share more code between GDB and gdbserver, and having this variable be the same type in both code bases will make the sharing much easier. There should be no user visible changes after this commit. --- gdbserver/linux-arm-low.cc | 6 +++--- gdbserver/linux-low.cc | 2 +- gdbserver/linux-low.h | 2 +- gdbserver/linux-x86-low.cc | 10 +++++----- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/gdbserver/linux-arm-low.cc b/gdbserver/linux-arm-low.cc index 396ec88081b..b4f0e071c92 100644 --- a/gdbserver/linux-arm-low.cc +++ b/gdbserver/linux-arm-low.cc @@ -1007,9 +1007,9 @@ arm_target::low_arch_setup () /* Check if PTRACE_GETREGSET works. */ if (ptrace (PTRACE_GETREGSET, tid, NT_PRSTATUS, &iov) == 0) - have_ptrace_getregset = 1; + have_ptrace_getregset = TRIBOOL_TRUE; else - have_ptrace_getregset = 0; + have_ptrace_getregset = TRIBOOL_FALSE; } bool @@ -1122,7 +1122,7 @@ arm_target::get_regs_info () { const struct target_desc *tdesc = current_process ()->tdesc; - if (have_ptrace_getregset == 1 + if (have_ptrace_getregset == TRIBOOL_TRUE && (is_aarch32_linux_description (tdesc) || arm_linux_get_tdesc_fp_type (tdesc) == ARM_FP_TYPE_VFPV3)) return ®s_info_aarch32; diff --git a/gdbserver/linux-low.cc b/gdbserver/linux-low.cc index 9d5a6242803..dfa26d451b0 100644 --- a/gdbserver/linux-low.cc +++ b/gdbserver/linux-low.cc @@ -135,7 +135,7 @@ typedef struct #endif /* Does the current host support PTRACE_GETREGSET? */ -int have_ptrace_getregset = -1; +enum tribool have_ptrace_getregset = TRIBOOL_UNKNOWN; /* Return TRUE if THREAD is the leader thread of the process. */ diff --git a/gdbserver/linux-low.h b/gdbserver/linux-low.h index d34d2738238..eaf87527338 100644 --- a/gdbserver/linux-low.h +++ b/gdbserver/linux-low.h @@ -951,7 +951,7 @@ 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 int have_ptrace_getregset; +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 diff --git a/gdbserver/linux-x86-low.cc b/gdbserver/linux-x86-low.cc index 3af0a009052..0a3bd2c8670 100644 --- a/gdbserver/linux-x86-low.cc +++ b/gdbserver/linux-x86-low.cc @@ -899,7 +899,7 @@ x86_linux_read_description (void) if (ptrace (PTRACE_GETFPXREGS, tid, 0, (long) &fpxregs) < 0) { have_ptrace_getfpxregs = 0; - have_ptrace_getregset = 0; + have_ptrace_getregset = TRIBOOL_FALSE; return i386_linux_read_description (X86_XSTATE_X87); } else @@ -918,7 +918,7 @@ x86_linux_read_description (void) return tdesc_i386_linux_no_xml.get (); } - if (have_ptrace_getregset == -1) + if (have_ptrace_getregset == TRIBOOL_UNKNOWN) { uint64_t xstateregs[(X86_XSTATE_SSE_SIZE / sizeof (uint64_t))]; struct iovec iov; @@ -929,10 +929,10 @@ x86_linux_read_description (void) /* Check if PTRACE_GETREGSET works. */ if (ptrace (PTRACE_GETREGSET, tid, (unsigned int) NT_X86_XSTATE, (long) &iov) < 0) - have_ptrace_getregset = 0; + have_ptrace_getregset = TRIBOOL_FALSE; else { - have_ptrace_getregset = 1; + have_ptrace_getregset = TRIBOOL_TRUE; /* Get XCR0 from XSAVE extended state. */ xcr0 = xstateregs[(I386_LINUX_XSAVE_XCR0_OFFSET @@ -951,7 +951,7 @@ x86_linux_read_description (void) } /* Check the native XCR0 only if PTRACE_GETREGSET is available. */ - xcr0_features = (have_ptrace_getregset + xcr0_features = (have_ptrace_getregset == TRIBOOL_TRUE && (xcr0 & X86_XSTATE_ALL_MASK)); if (xcr0_features) From patchwork Tue Mar 5 17:00:17 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Burgess X-Patchwork-Id: 86824 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 62AEC3857C49 for ; Tue, 5 Mar 2024 17:01:09 +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 610F33858C54 for ; Tue, 5 Mar 2024 17:00:34 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 610F33858C54 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 610F33858C54 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=1709658037; cv=none; b=YI98GcRRIA9CDK4aD9uiZVqusMO2cNQ39XrjoKxtZolgiLM0pwP21VmUgRi+c4pIEDACoeAkUfRcWDvtkWqW2OtOJIKmuf5+Ss+6fpsbdFpPUamN6v2mDKZ9SRrrMuhHS6Ac7NRAxjwCoT4e9tqJ5s1rrUUKmrS74B8BJI8P6N0= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1709658037; c=relaxed/simple; bh=jrQjt0KZMclBRfviJh6vdBIqgIZ9DsDinBIGGiJRjV8=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=Ax/cSgC9kebn54WxS6TZ4HDKMJAsPWyJBMN59pokSm3CT8TBIVbtxD/y97IhhG9kGno0lAGSnMthAlmAAXoxBLgOFK76GlmjuLI+RWpEmtKvntYoQMpZsCryagm35nxu4bcxqkXBmBwXJjdZEihKqjIVjwuqv6rK8IN7y0Na3to= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1709658034; 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=n3E/5/Dp/ldR+3ZldUUOQH7Gh19Z9aeEnWLVM19ESq8=; b=gCpQ3KA4GBEUXv3kxDqf+Xr+Y+m65B1vQtN4WkmKV5W9VDoWjnNxesCSIb1TtU+vQSweVR Vvem/5h2tZ/shWIDG/uqXbd4NAH4N7ELTSTRirTBsji9JiG0YgXY/ic+EecH9GG+/5ARHg NybYJqMlEJHA+SLan/pqtd4t9M/taA0= Received: from mail-ej1-f72.google.com (mail-ej1-f72.google.com [209.85.218.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-267-cI-DxcjCMOCJyWq6J9753Q-1; Tue, 05 Mar 2024 12:00:32 -0500 X-MC-Unique: cI-DxcjCMOCJyWq6J9753Q-1 Received: by mail-ej1-f72.google.com with SMTP id a640c23a62f3a-a45acc7f191so48095566b.0 for ; Tue, 05 Mar 2024 09:00:32 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709658030; x=1710262830; 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=n3E/5/Dp/ldR+3ZldUUOQH7Gh19Z9aeEnWLVM19ESq8=; b=a/sKYXTqhcFQI9xNMhQRHHUeiXydp4eljt5Q/Z/caL9RDfo4AYN4cEy28A0tvtx3QX mcmLgdVOvQo/ezWV0o37mMNiM+XN5kIvQesU30Y8RHHdYvHwsdFEpOiuCkxeiYGVaNdV pz1yVm1Fhg1ePp4CNttnDBxnVehu7njqvizwBAXY+kcEPeg+pBjyUa7R11SHWoHhjizw LIoy5ac8dunGrR6BIASOCKZzCxbRK4qdsRTVOwqrf3qkS+WnNaaJnqG3jGv78iqVH5LZ y3sq2pO1L2VaAeX4OSyS1eJ5XbbpMC0QqGWClh9iPatGWg/tJA64X2dVuzQ1SACV5OpP 1jlA== X-Gm-Message-State: AOJu0YxsR9wk9tuvoMdl7uDuXtGqPYy2+XUmlTQLWUntTC3yoLi6R7+h ll+4VmSdHjXQdO3nXryd9ssRY36UzQ+RRuB2cNXJCOqQiDh5asWm0EX/iuv9d6kixfMYPM1gE+y aa8lL2ReWIWjylEe3AsuNyuk8RWIPUIOLK8z5WHXQG0q1pbfb4jfNhjXj5k7/UA+bNUJaGLS4ql TvJFPHPE1zKnbBhFIZVaahYgCuop2IdnTTQ8JKoRh35LI= X-Received: by 2002:a17:906:4342:b0:a45:607c:7bca with SMTP id z2-20020a170906434200b00a45607c7bcamr3675379ejm.31.1709658030536; Tue, 05 Mar 2024 09:00:30 -0800 (PST) X-Google-Smtp-Source: AGHT+IECAohEIZqQngr8e5GU6VLbOLYkl4Zj9H366tZGXu4oMiMl6+k0vHFRBxB4UVKP1bZxKdJKcg== X-Received: by 2002:a17:906:4342:b0:a45:607c:7bca with SMTP id z2-20020a170906434200b00a45607c7bcamr3675367ejm.31.1709658030027; Tue, 05 Mar 2024 09:00:30 -0800 (PST) Received: from localhost (185.223.159.143.dyn.plus.net. [143.159.223.185]) by smtp.gmail.com with ESMTPSA id rp25-20020a170906d97900b00a3d665c6778sm6318746ejb.12.2024.03.05.09.00.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 05 Mar 2024 09:00:29 -0800 (PST) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess Subject: [PATCHv2 2/7] gdb/x86: move reading of cs and ds state into gdb/nat directory Date: Tue, 5 Mar 2024 17:00:17 +0000 Message-Id: <56268fed55672ec3c13401c42d128a27504e99ed.1709657954.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.3 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, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE 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 of making the code that, for x86, reads the target description for a native process shared 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. 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 could 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). --- gdb/nat/x86-linux.c | 47 +++++++++++++++++++++++++++++++++++++++++++++ gdb/nat/x86-linux.h | 28 +++++++++++++++++++++++++++ gdb/x86-linux-nat.c | 42 +++++----------------------------------- 3 files changed, 80 insertions(+), 37 deletions(-) diff --git a/gdb/nat/x86-linux.c b/gdb/nat/x86-linux.c index e61f4d749ba..4242a1baafb 100644 --- a/gdb/nat/x86-linux.c +++ b/gdb/nat/x86-linux.c @@ -20,6 +20,8 @@ #include "gdbsupport/common-defs.h" #include "x86-linux.h" #include "x86-linux-dregs.h" +#include "nat/gdb_ptrace.h" +#include /* Per-thread arch-specific data we want to keep. */ @@ -80,3 +82,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 822882173f9..15153ea277e 100644 --- a/gdb/nat/x86-linux.h +++ b/gdb/nat/x86-linux.h @@ -47,4 +47,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 b93ffca38db..b39d05c401f 100644 --- a/gdb/x86-linux-nat.c +++ b/gdb/x86-linux-nat.c @@ -91,18 +91,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 * @@ -122,31 +110,11 @@ 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 (); + #elif HAVE_PTRACE_GETFPXREGS if (have_ptrace_getfpxregs == -1) { From patchwork Tue Mar 5 17:00:18 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Burgess X-Patchwork-Id: 86826 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 1BEB23857C4A for ; Tue, 5 Mar 2024 17:02:01 +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 E06B0385840A for ; Tue, 5 Mar 2024 17:00:36 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org E06B0385840A 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 E06B0385840A 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=1709658039; cv=none; b=GqXooy1b+8wehRouxquVYnTGgC5h8iQpVSxeQwvFxCuwV5o4hqt5mRe4stsFlmAMfoubejFzmn4KZCY5KlaagbptHQN0y+tPlbzgyjZFeGU8ff8v/xGnW9sdy3xgm7kyBXrPp5ID/GB3oPD3n4sTYPOygaPDQNt06DqAlsVH8yI= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1709658039; c=relaxed/simple; bh=9/HqTaCvTazW1RjeOAOizbbNirCf+b+Oq8ruTP8S+qw=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=UQZ4S4kskbiVXI2xNxi+kQQx4tUlxKoPYRdU/sImt+Lb5pP+TxnKlcJue3aV4xTpLDd3MA1Oux25MzFpIf5+CaNbC6fI67A3aHWUN2LdMve6th3Deb1ULMFgzP8cw1aVsOo8qzSJXYarAcdCeVGNwdXkQ2O2a/ctIgXAmeEAi5w= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1709658036; 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=gIxf/Z9eGBVC+AA09DfGemGgR17B1hKcwD9+MJo0YZ0=; b=g+CW8FI46Hq+kuR31VaDPjK0TwxPmiKbNj1aJo65W8rD06oXSsITGdZPzRluTRYtSb/GJP ZCOJ0a7X36KBtYzPRmuIlIJIVXETrmIMEzP2a+EeIvkI0xXJRpQz91IPwojTsOKzQLOl4k jPwl4wKbaZRPIxF+/vy4qFawaefFlkw= Received: from mail-ed1-f72.google.com (mail-ed1-f72.google.com [209.85.208.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-116-ugFJWvOpP_GX2upnpAXy0A-1; Tue, 05 Mar 2024 12:00:35 -0500 X-MC-Unique: ugFJWvOpP_GX2upnpAXy0A-1 Received: by mail-ed1-f72.google.com with SMTP id 4fb4d7f45d1cf-567d4f5b3d1so80104a12.2 for ; Tue, 05 Mar 2024 09:00:34 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709658031; x=1710262831; 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=gIxf/Z9eGBVC+AA09DfGemGgR17B1hKcwD9+MJo0YZ0=; b=uQT3inHrQcfpreEmaxMTyQelaLqiuVpJqqCjUJQqQYUxqN0JeTS9+MRN6jcDVBPlM2 h0mIwcZ4vMZn7SVatredAYtgZqdFOqWuYcHlz2XMoif3vs7E1c/gUyfYb1h6UcdHdKoQ g3oD1Sb3jO1VEVX8N22ahWCOnizs2mikEk6Tt+8crIuEMlzxbvpUzAhhqbJYb7geS/P1 2yvlzOl8U975NLiF1F1RXEn7krg9tOxY7g3KJB6HPTLelBXeJK+kt70xKzncdKNOBtDN Bnm3l3c/3Vp9XsrkNxOldcbWQE0BdU9Njw4NcLQKLtIPpaeuIXgKRO+wGXx8/hfDhFqt Y4FQ== X-Gm-Message-State: AOJu0Yz9i3ykK0pw8TCReeg20C5ffNM+n6xp4AgegpHD+GjD+SyVq6ul gRTdkDjkWRHCSpkFdYrYZBdoQyOxyoQXbGHCcjQlg31/C+Jbu9mU+rN09/9b7KzV5zuVZknkXwi u227Q92uxNX55oJdViQXN1YTVmGfmb2t8KLipon/InahCvtcrSc0DuTSC0oHSu2nbxDwfO/mx3e IJCKqREXbKMru/2NYQVRQIyd/HmIPPmioM0AgbR1//zr0= X-Received: by 2002:a05:6402:b18:b0:565:bb25:bb7a with SMTP id bm24-20020a0564020b1800b00565bb25bb7amr9067321edb.24.1709658031550; Tue, 05 Mar 2024 09:00:31 -0800 (PST) X-Google-Smtp-Source: AGHT+IHPC9EFCuFle00iTMlO/bSfwa3pkVRwe7Q2/38QV3aP+tLhK2u4DDtztHkMv51o09vkdLHx2w== X-Received: by 2002:a05:6402:b18:b0:565:bb25:bb7a with SMTP id bm24-20020a0564020b1800b00565bb25bb7amr9067296edb.24.1709658030949; Tue, 05 Mar 2024 09:00:30 -0800 (PST) Received: from localhost (185.223.159.143.dyn.plus.net. [143.159.223.185]) by smtp.gmail.com with ESMTPSA id cs4-20020a0564020c4400b00566a4dec01fsm5870501edb.11.2024.03.05.09.00.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 05 Mar 2024 09:00:30 -0800 (PST) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess Subject: [PATCHv2 3/7] gdbserver/x86: move no-xml code earlier in x86_linux_read_description Date: Tue, 5 Mar 2024 17:00:18 +0000 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.3 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, T_SCC_BODY_TEXT_LINE 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. --- 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 0a3bd2c8670..ec80bfe905c 100644 --- a/gdbserver/linux-x86-low.cc +++ b/gdbserver/linux-x86-low.cc @@ -891,6 +891,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) { @@ -907,17 +923,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 Tue Mar 5 17:00:19 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Burgess X-Patchwork-Id: 86825 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 CC09338582A0 for ; Tue, 5 Mar 2024 17:01:35 +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 BF9E9385841E for ; Tue, 5 Mar 2024 17:00:37 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org BF9E9385841E 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 BF9E9385841E 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=1709658039; cv=none; b=a1d24QmsfFIIdRif2Fi3t9CEfy1+HRVkQepdBfyCVMqejlpze+PoFyvFGMIXW2+lS2hAhUP7U25fwNmJuQLxcu4cI+md7WlYmc6dvWwCyuRbRYs+Xcp4mMy+ggdxC+0hNMZ9iGhxFsT894pvLqe06dmy/M69pa9WjTaArzrqykc= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1709658039; c=relaxed/simple; bh=33soRsZkXSgzEHGChzLjEQIdtmxoK62AIEhTozwP8Ss=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=B+sRAtz8qpn2WX0oifceeMLtEl4GKYWi5yYBmtettfScYuq9qM/y+h4uR9BNk1NiREL7B8pYo9EyOC1/bmODhByfpBXuNsrPYntxUEyN4inPRUvp6KfeFdZcZfrN7TRnOavr/UseEQ84fuXpvj4cfSTv3wPN5V/O3DQWVCXm+u0= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1709658037; 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=CJngwrA0jAT15OSINDaXUgDRUESbq+cA5MNEVZHqi6A=; b=cXJt9riY7j2enRSeLRQEqewr7F9mbYujsZhD+sNDxBPTGfu7qXCXj58hJ98lwKkAoy+pJE wCB6RQnuhl1JdV5o79Ru6LyAovvX/53S+tyeQu9D871YE43nqIzA6MB9mFoAUJHHs8wRdK Ih3VbjVzoJkd4qYh9XZR+QPYUhe9CV8= Received: from mail-ej1-f70.google.com (mail-ej1-f70.google.com [209.85.218.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-130-QIGWVH8_M4KPp4Dz694BFw-1; Tue, 05 Mar 2024 12:00:36 -0500 X-MC-Unique: QIGWVH8_M4KPp4Dz694BFw-1 Received: by mail-ej1-f70.google.com with SMTP id a640c23a62f3a-a45851b0030so94250766b.3 for ; Tue, 05 Mar 2024 09:00:36 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709658035; x=1710262835; 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=5l6cZBAwFHCFujU3vwfZPFJhWxmCp2WHrpaQ8yWZrUw=; b=EqCHCV61zx+YiIFaa/N5CsMPp9ISBoVAqDEDsok8R2JecQvNBe78BQ36zGbDdOxacB 2XtKvOqbsUhuudXuxK1VzHELaLyh3Ts4BA0dZt0PFTeDiqFaAkt6mIlvEt/KoCuv8ZjV AdyyLdtXhf+9kND+0aEgtMN1+JTNBWozetpS7jL+ShdomyQ8/2G8Te62kwTlpV36S7Su 1VyfpeR2asjIQgiOU5TSchYeCSuiRamIdhnpkZ3CBDcFYwZdweFpaC5u8Dn1IFjYOC73 K+aXExWgAWt9tPe8M1RMSS9yWMLTqDnjKIXAwCitGUG1tBDpkSGYR8U3lYCrshycTjyz rBkg== X-Gm-Message-State: AOJu0YzhxqiCgq3/Q7YFz7td/b9j5GJQQ1k8xsbEACy/yGeX3S8LHZ/l WS1bdFfNAFRA8eb7PQdcfnmpPKWS8iUBLtxabgn56t0Btgxy5FxxVnkeXDcBNkDBdU7zmPr59a3 l05GgaA+KDNq3+LWFSOThexUg66/xVPPtBSXwU661iGZKRpSCSb05mURvpzyjHGDFoRhlDnFUyp fpX4CZVj11r4f8eWJnTIModQDfKmKx74G4F25yY4cz1T4= X-Received: by 2002:a17:906:6b91:b0:a45:b2d0:87e6 with SMTP id l17-20020a1709066b9100b00a45b2d087e6mr224388ejr.44.1709658034556; Tue, 05 Mar 2024 09:00:34 -0800 (PST) X-Google-Smtp-Source: AGHT+IGHl5PWCpP9MQbFnYd8jq20Cr4eteOwPhMMiszThtdzPjxZY8zTygJQ4/gf0UTKNlVT3nXbqQ== X-Received: by 2002:a17:906:6b91:b0:a45:b2d0:87e6 with SMTP id l17-20020a1709066b9100b00a45b2d087e6mr224364ejr.44.1709658033849; Tue, 05 Mar 2024 09:00:33 -0800 (PST) Received: from localhost (185.223.159.143.dyn.plus.net. [143.159.223.185]) by smtp.gmail.com with ESMTPSA id w18-20020a17090652d200b00a4320e22b31sm6304527ejn.19.2024.03.05.09.00.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 05 Mar 2024 09:00:31 -0800 (PST) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess Subject: [PATCHv2 4/7] gdb/gdbserver: share I386_LINUX_XSAVE_XCR0_OFFSET definition Date: Tue, 5 Mar 2024 17:00:19 +0000 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.7 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, T_SCC_BODY_TEXT_LINE 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 is part of a series that aims to share more of the x86 target description creation code between GDB and gdbserver. The I386_LINUX_XSAVE_XCR0_OFFSET #define is used as part of the target description creation, and I noticed that this constant is defined separately for GDB and gdbserver. This commit moves the definition into gdb/nat/x86-linux.h, which allows the #define to be shared. There should be no user visible changes after this commit. --- gdb/amd64-linux-tdep.c | 1 + gdb/i386-linux-tdep.c | 1 + gdb/i386-linux-tdep.h | 20 -------------------- gdb/nat/x86-linux.h | 20 ++++++++++++++++++++ gdbserver/linux-x86-low.cc | 22 +--------------------- 5 files changed, 23 insertions(+), 41 deletions(-) diff --git a/gdb/amd64-linux-tdep.c b/gdb/amd64-linux-tdep.c index 1deb13b4e9a..a512ec5dd02 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 "nat/x86-linux.h" /* The syscall's XML filename for i386. */ #define XML_SYSCALL_FILENAME_AMD64 "syscalls/amd64-linux.xml" diff --git a/gdb/i386-linux-tdep.c b/gdb/i386-linux-tdep.c index a2f937690c2..8dd7203b6c3 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 "nat/x86-linux.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 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/gdb/nat/x86-linux.h b/gdb/nat/x86-linux.h index 15153ea277e..855a8d14f91 100644 --- a/gdb/nat/x86-linux.h +++ b/gdb/nat/x86-linux.h @@ -22,6 +22,26 @@ #include "nat/linux-nat.h" +/* 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 + /* 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-x86-low.cc b/gdbserver/linux-x86-low.cc index ec80bfe905c..7bade8b3cbf 100644 --- a/gdbserver/linux-x86-low.cc +++ b/gdbserver/linux-x86-low.cc @@ -27,6 +27,7 @@ #include "gdbsupport/x86-xstate.h" #include "nat/x86-xstate.h" #include "nat/gdb_ptrace.h" +#include "nat/x86-linux.h" #ifdef __x86_64__ #include "nat/amd64-linux-siginfo.h" @@ -832,27 +833,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. */ From patchwork Tue Mar 5 17:00:20 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Burgess X-Patchwork-Id: 86829 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 BE931385840F for ; Tue, 5 Mar 2024 17:02:48 +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 7CE4B38582A4 for ; Tue, 5 Mar 2024 17:00:39 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 7CE4B38582A4 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 7CE4B38582A4 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=1709658044; cv=none; b=a1lkeF0I/g5xqVEuc4sdDkbbiQIl8IwcYQe27Ox4tlJYeNzpNIFP5Yp7uagUsHq21L6OxiHuizFkB+sVcgEQP5zbyrR+YQWecLIDkZpQpJ2DaFJuwUppX4BL86ZXHxWu8x1EDCmvAPVz9PrHiusCrb9KyunTFOV0tD5oyJPDIeg= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1709658044; c=relaxed/simple; bh=ENTq4mVsZcFN/Twiv+N5mnSaGEj+fYyt0PbUOXdLx6I=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=i1+szsCO0obiWHhOYaiWUoDcOtuN8XIrNl7dcf7t3X3us0kno5Th8Jur/IGsjraNUyFvk4cPb9oxri5L+sEvaMl0BPNKHoNf3XLeznyg6zG7CiJAHEe9k21sA8rGu/qnnzHyOJud3JyfrKzuXYYwOl04TIRylt3o4D2d5W3EODk= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1709658039; 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=Poa873OW7unQj1lo0e6pKpCRkrWwwVjRRKoKsTSJuL8=; b=Kr6NqfmW21ZVqs2zXK5MC7kSr9kkHpjYagVtrF7Orr1sAv/32RRLjNUS1i9wdErDXXqzUv J1MT4NXZrAJEE1196tr1My4u+cVz6cxzHs46U2xTGX3/QVLPYLuSqltaOrtl0O8LwUvwxd 4hXWiSncA14I3g9VHT7TCZLHMLHe6Xs= Received: from mail-ej1-f70.google.com (mail-ej1-f70.google.com [209.85.218.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-145-SCzad44ENaKaFrbda0_FwA-1; Tue, 05 Mar 2024 12:00:37 -0500 X-MC-Unique: SCzad44ENaKaFrbda0_FwA-1 Received: by mail-ej1-f70.google.com with SMTP id a640c23a62f3a-a4585db429bso98443866b.0 for ; Tue, 05 Mar 2024 09:00:37 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709658036; x=1710262836; 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=HfaKYKx6Nhu7gr0AmuQcW1RGfLdS8CsthgdqKIB1NzE=; b=I0BPIgeIWJntyATHSeUKgQRev/bpRWmqq7KyOZ+SmC3Ii6ZIJbf/ye05zKx9FfxXkG uGXQx9qIKbqGEPMVjwdzGmtrL0rnSQWqUs4gl379e0nYF+EFe0BC8Gjfrm+9kgcKS6v3 f0jV3tbbghRA625fr9BtPQB2GZcp/9GD653RO641pfxr/xSSUILFJZqr5rzbZi3edBSp 8BP4X/UHVv5yZHiij3oVhPEJbegP12l+9PhIpATOwqtVbcqzPcqDtztpAxNWdubZ0ObN Z+MeMVsl+AsqzNz4+zBKVIpDkVtxw7Wyv4Jm64x8HA7aIsuFPp8/Jyd3GASYJD1FvjZX Notg== X-Gm-Message-State: AOJu0YwbWRAOlWxLHrAepRYjNhSQsZulzXd8AtlJViGkRerx/Z1fGVdK HV6Y/VLDB3oHn3NdQ6v3qaBGupkVFdtR4AOwi+kuNn1n+inpkgIX+NE+zx9gP7SGhFkPcbWszih f07IiwMAomyKq+D3y6vBkDbZGkdaPpFaQqNFKhW58j17Uk0HRMQNDPDjmASmW8VR7Ywr5D38kEb AJ15Cn0UtUIPWjZZqDqIo/taz8v9IS4lK8vt7QxIw3wks= X-Received: by 2002:a17:906:39da:b0:a44:e2e0:bda8 with SMTP id i26-20020a17090639da00b00a44e2e0bda8mr6646423eje.0.1709658035677; Tue, 05 Mar 2024 09:00:35 -0800 (PST) X-Google-Smtp-Source: AGHT+IECD2UsJGLbJSVyRaiW+j02bS9IdXjyK5e1/WapSvO/1EgawFqr2lvABvVpZLhQCMjgUq0c2A== X-Received: by 2002:a17:906:39da:b0:a44:e2e0:bda8 with SMTP id i26-20020a17090639da00b00a44e2e0bda8mr6646380eje.0.1709658034800; Tue, 05 Mar 2024 09:00:34 -0800 (PST) Received: from localhost (185.223.159.143.dyn.plus.net. [143.159.223.185]) by smtp.gmail.com with ESMTPSA id a18-20020a1709066d5200b00a458c59b4c4sm1432819ejt.123.2024.03.05.09.00.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 05 Mar 2024 09:00:34 -0800 (PST) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess Subject: [PATCHv2 5/7] gdb/gdbserver: share some code relating to target description creation Date: Tue, 5 Mar 2024 17:00:20 +0000 Message-Id: <34d7aaf5e472af247b1be1abc3eb3432e52e2ad4.1709657954.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.7 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, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE 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 the GDB way, the only changes are to allow for the sharing; we now have a callback function to call the first time that the xcr0 state is read, this allows for GDB and gdbserver to perform their own initialisation as needed, and additionally, the new function takes a pointer for where to cache the xcr0 value, this isn't needed for this commit, but will be useful in a later commit where gdbserver will want to read this cached xcr0 value. 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/nat/x86-linux-tdesc.h, but the implementations are left as separate. 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. --- gdb/Makefile.in | 1 + gdb/amd64-linux-tdep.c | 1 + gdb/amd64-linux-tdep.h | 6 -- gdb/configure.nat | 4 +- gdb/i386-linux-tdep.c | 1 + gdb/i386-linux-tdep.h | 3 - gdb/nat/x86-linux-tdesc.c | 118 ++++++++++++++++++++++++++++ gdb/nat/x86-linux-tdesc.h | 75 ++++++++++++++++++ gdb/x86-linux-nat.c | 91 ++++------------------ gdbserver/configure.srv | 2 + gdbserver/linux-amd64-ipa.cc | 1 + gdbserver/linux-i386-ipa.cc | 1 + gdbserver/linux-x86-low.cc | 144 +++++++++++------------------------ gdbserver/linux-x86-tdesc.cc | 1 + gdbserver/linux-x86-tdesc.h | 7 -- 15 files changed, 261 insertions(+), 195 deletions(-) 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 0e0f19c40c9..76bed92d84e 100644 --- a/gdb/Makefile.in +++ b/gdb/Makefile.in @@ -1586,6 +1586,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 a512ec5dd02..7e0900dc6f9 100644 --- a/gdb/amd64-linux-tdep.c +++ b/gdb/amd64-linux-tdep.c @@ -43,6 +43,7 @@ #include "target-descriptions.h" #include "expop.h" #include "nat/x86-linux.h" +#include "nat/x86-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/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 8dd7203b6c3..f5f7a36bf36 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 "nat/x86-linux.h" +#include "nat/x86-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..78d22f55d67 --- /dev/null +++ b/gdb/nat/x86-linux-tdesc.c @@ -0,0 +1,118 @@ +/* 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 "gdbsupport/common-defs.h" +#include "nat/x86-linux-tdesc.h" +#ifdef __x86_64__ +#include "arch/amd64.h" +#endif +#include "arch/i386.h" + +#include "gdbsupport/common-defs.h" +#include "nat/x86-linux.h" +#include "nat/x86-linux-dregs.h" +#include "nat/gdb_ptrace.h" +#include "nat/x86-xstate.h" +#include "nat/x86-linux-tdesc.h" + +#include +#include +#include +#include +#include +#include + +/* See nat/x86-linux-tdesc.h. */ + +const target_desc * +x86_linux_tdesc_for_tid (int tid, enum tribool *have_ptrace_getregset, + gdb::function_view xcr0_init_cb, + const char *error_msg, uint64_t *xcr0_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 == -1) + { + elf_fpxregset_t fpxregs; + + if (ptrace (PTRACE_GETFPXREGS, tid, 0, (int) &fpxregs) < 0) + { + have_ptrace_getfpxregs = 0; + *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; + *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))]; + + xcr0_init_cb (*xcr0_storage); + } + } + + /* 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. */ + 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); + + gdb_assert_not_reached ("failed to return tdesc"); +} diff --git a/gdb/nat/x86-linux-tdesc.h b/gdb/nat/x86-linux-tdesc.h new file mode 100644 index 00000000000..3727a8bf95e --- /dev/null +++ b/gdb/nat/x86-linux-tdesc.h @@ -0,0 +1,75 @@ +/* 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; + +/* Return the target description for Linux thread TID. + + When *HAVE_PTRACE_GETREGSET is TRIBOOL_UNKNOWN then the current value of + xcr0 is read using ptrace calls and stored into *XCR0_STORAGE. Then + XCR0_INIT_CB is called with the value of *XCR0_STORAGE and + *HAVE_PTRACE_GETREGSET is set to TRIBOOL_TRUE. + + If the attempt to read xcr0 using ptrace fails then *XCR0_STORAGE is set + to zero and *HAVE_PTRACE_GETREGSET is set to TRIBOOL_FALSE. + + The storage pointed to by XCR0_STORAGE must exist until the program + terminates, this storage is used to cache the xcr0 value. As such + XCR0_INIT_CB will only be called once if xcr0 is successfully read using + ptrace, or not at all if the ptrace call fails. + + 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, enum tribool *have_ptrace_getregset, + gdb::function_view xcr0_init_cb, + const char *error_msg, uint64_t *xcr0_storage); + +#ifdef __x86_64__ + +/* Return the right amd64-linux target descriptions according to + XCR0_FEATURES_BIT and IS_X32. This is implemented separately in both + GDB and gdbserver. */ + +extern const target_desc *amd64_linux_read_description + (uint64_t xcr0_features_bit, bool is_x32); + +#endif + +/* Return the target description according to XCR0. This is implemented + separately in both GDB and gdbserver. */ +extern const struct target_desc *i386_linux_read_description (uint64_t xcr0); + +#endif /* NAT_X86_LINUX_TDESC_H */ diff --git a/gdb/x86-linux-nat.c b/gdb/x86-linux-nat.c index b39d05c401f..872e27e739f 100644 --- a/gdb/x86-linux-nat.c +++ b/gdb/x86-linux-nat.c @@ -42,6 +42,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. */ @@ -96,90 +97,26 @@ 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 (); - -#elif HAVE_PTRACE_GETFPXREGS - if (have_ptrace_getfpxregs == -1) - { - elf_fpxregset_t fpxregs; - - if (ptrace (PTRACE_GETFPXREGS, tid, 0, (int) &fpxregs) < 0) - { - have_ptrace_getfpxregs = 0; - 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; - - 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); + int tid = inferior_ptid.pid (); - if (tdesc == NULL) - tdesc = i386_linux_read_description (X86_XSTATE_SSE_MASK); + const char *error_msg + = _("Can't debug 64-bit process with 32-bit GDB"); - return tdesc; - } + /* Callback that is triggered the first time x86_linux_tdesc_for_tid + reads the xcr0 register. Setup other bits of state */ + auto cb = [&] (uint64_t xcr0) + { + this->m_xsave_layout + = x86_fetch_xsave_layout (xcr0, x86_xsave_length ()); + }; - gdb_assert_not_reached ("failed to return tdesc"); + return x86_linux_tdesc_for_tid (tid, &have_ptrace_getregset, cb, + error_msg, &xcr0_storage); } 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/linux-amd64-ipa.cc b/gdbserver/linux-amd64-ipa.cc index 54e4c9812bb..f97b0d6a1d9 100644 --- a/gdbserver/linux-amd64-ipa.cc +++ b/gdbserver/linux-amd64-ipa.cc @@ -23,6 +23,7 @@ #include "tracepoint.h" #include "linux-x86-tdesc.h" #include "gdbsupport/x86-xstate.h" +#include "nat/x86-linux-tdesc.h" /* Defined in auto-generated file amd64-linux.c. */ void init_registers_amd64_linux (void); diff --git a/gdbserver/linux-i386-ipa.cc b/gdbserver/linux-i386-ipa.cc index 2e4646f8c03..459b8055b5c 100644 --- a/gdbserver/linux-i386-ipa.cc +++ b/gdbserver/linux-i386-ipa.cc @@ -23,6 +23,7 @@ #include "tracepoint.h" #include "linux-x86-tdesc.h" #include "gdbsupport/x86-xstate.h" +#include "nat/x86-linux-tdesc.h" /* GDB register numbers. */ diff --git a/gdbserver/linux-x86-low.cc b/gdbserver/linux-x86-low.cc index 7bade8b3cbf..9bf369f8a34 100644 --- a/gdbserver/linux-x86-low.cc +++ b/gdbserver/linux-x86-low.cc @@ -48,6 +48,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; @@ -844,32 +845,20 @@ int have_ptrace_getfpxregs = #endif ; +/* Cached xcr0 value. This is initialised the first time + x86_linux_read_description is called. */ + +static uint64_t xcr0_storage; + /* Get Linux/x86 target description from running target. */ static const struct target_desc * x86_linux_read_description (void) { - 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 @@ -879,99 +868,54 @@ 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 == -1) - { - elf_fpxregset_t fpxregs; - - if (ptrace (PTRACE_GETFPXREGS, tid, 0, (long) &fpxregs) < 0) - { - have_ptrace_getfpxregs = 0; - have_ptrace_getregset = TRIBOOL_FALSE; - return i386_linux_read_description (X86_XSTATE_X87); - } - else - have_ptrace_getfpxregs = 1; - } -#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))]; - - 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 (xcr0_features) - i387_set_xsave_mask (xcr0, xsave_len); + /* Callback that is triggered the first time x86_linux_tdesc_for_tid + reads the xcr0 register. Setup other bits of state */ + auto cb = [] (uint64_t xcr0) + { + i387_set_xsave_mask (xcr0, x86_xsave_length ()); + }; - if (machine == EM_X86_64) - { -#ifdef __x86_64__ - const target_desc *tdesc = NULL; + /* 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) - { - tdesc = amd64_linux_read_description (xcr0 & X86_XSTATE_ALL_MASK, - !is_elf64); - } + const target_desc *tdesc + = x86_linux_tdesc_for_tid (tid, &have_ptrace_getregset, cb, error_msg, + &xcr0_storage); - 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 626207fc477..9fd64d8574b 100644 --- a/gdbserver/linux-x86-tdesc.cc +++ b/gdbserver/linux-x86-tdesc.cc @@ -26,6 +26,7 @@ #include "arch/amd64.h" #endif #include "x86-tdesc.h" +#include "nat/x86-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 Tue Mar 5 17:00:21 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Burgess X-Patchwork-Id: 86830 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 71870385800C for ; Tue, 5 Mar 2024 17:03:27 +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 369443858296 for ; Tue, 5 Mar 2024 17:00:44 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 369443858296 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 369443858296 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=1709658047; cv=none; b=S1vp95knNmK8Vmnoh/bSBG4s0Ahaj1UH52cvDHfnn83O0W2pJyGxIB4ewospbNG/5PuOLQc1OqoO+a5u+uNmvR0KQmHpZ4MA116dOCdTYMDouo4wpQklyIoFB7K2ERgn60dlwhZNGRqOwMZSTW4wd0CaOfQccpoq0hWYBagYpeQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1709658047; c=relaxed/simple; bh=wgiloRsXke7abgItuxIBq3E+KSS/cG10oXAwryZwTNI=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=mttPoqvhn4IQMpHpoIOU5sox8cGyVjHXLkNJlJjhh7Q1kY2K2J2fom5c8daZvUfe/03pUmsSSgZtCQt1iI+hVPRZ0Fvyg5n5yMRNuIprs3Cfgr68fGox3vb2QDfPEChhPNAh7qGosol8mkIaEMSq7GADRnSMOZrT7rDYQq5J2ug= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1709658043; 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=YEV5MEi2jZkO0KFYWFBlC3wMUGYAC7OTBjmm+lfPbqg=; b=NUzp7D8119Vj8DoJs+HlURnmS5qzrvJkxx0E3U+A+em/vMRBOnNPCxzOznwS1K84nBJV7e v/cTPuJdwVmKS9gk0fExNW72PW9KQkPER5UiMIlYOZ7McUwQlSe99+7Gk53INjuRv7NLf9 lN80gO3Y4AdbN1Hy0BQF/Cs04J8jIfE= Received: from mail-ej1-f72.google.com (mail-ej1-f72.google.com [209.85.218.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-383-1u2S3hkUMNedtAZrJdrigg-1; Tue, 05 Mar 2024 12:00:41 -0500 X-MC-Unique: 1u2S3hkUMNedtAZrJdrigg-1 Received: by mail-ej1-f72.google.com with SMTP id a640c23a62f3a-a4488afb812so272811166b.3 for ; Tue, 05 Mar 2024 09:00:38 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709658037; x=1710262837; 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=YEV5MEi2jZkO0KFYWFBlC3wMUGYAC7OTBjmm+lfPbqg=; b=qjzuW8sApxR2EVZJrupUZx11hRDX49b7+ghrX/rw40Rf9s9C9O/38ijaDSP6G54Txd qrL5jZRtWqIc6OTUxLXw5Gst3vwUb/Dvy5tGInZf8dHpIC+vWQH1LrUk3OCryyxniygr cqPWmOxmsYZEQ82GgDUgDqC/AqK1pYNNQLcFg/FKvRrjbvn6F/d+23zpG4EfM99CU44X WAVf6q0c8djqGdODKnNUjixeLeepfRnswUa4A1ZMNlCNwRKFfQXRknJ1FgAu+h4QWRNZ q1aHD6Gv8dMIOfMX3QqPl2Q8B1e3sWRXaV+T0nfrddIHhhqIbrqObGxFqL8bhNyf9mi7 bkPg== X-Gm-Message-State: AOJu0YwQZ1ed+bsVSlgIeQfnyuVH6aZWDtHY881DKJYXDihOrS4RvVwv c0w9EeW0uXRAAvs9Hk94yTOY/gbZew1OnS51aB8qEdZrJe1QObfk/TMb2Rpm32d9sIAfWAt1WvG YmGK0h0tXwYznqihT4RmTGnhTypSA3q5mlIdVD/igKIl4HySugfHOHoBx2cmj9sy/4g43Gzc6om x5d9m+xM5ZQS0P0Xz2pialDbQwoh+Wr7PuC4S+hBIDyT8= X-Received: by 2002:a17:906:4f0a:b0:a45:46f8:a181 with SMTP id t10-20020a1709064f0a00b00a4546f8a181mr4378833eju.46.1709658036922; Tue, 05 Mar 2024 09:00:36 -0800 (PST) X-Google-Smtp-Source: AGHT+IHCzMkOcbEsTQy42veTcyClpSt8ELq7aYeYP+AOVYkEIXz+oKEL7mzcQc/HoX6PfjLX5JKwlA== X-Received: by 2002:a17:906:4f0a:b0:a45:46f8:a181 with SMTP id t10-20020a1709064f0a00b00a4546f8a181mr4378794eju.46.1709658036073; Tue, 05 Mar 2024 09:00:36 -0800 (PST) Received: from localhost (185.223.159.143.dyn.plus.net. [143.159.223.185]) by smtp.gmail.com with ESMTPSA id t15-20020a1709063e4f00b00a42f6d17123sm6134933eji.46.2024.03.05.09.00.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 05 Mar 2024 09:00:35 -0800 (PST) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess Subject: [PATCHv2 6/7] gdbserver: update target description creation for x86/linux Date: Tue, 5 Mar 2024 17:00:21 +0000 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.3 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, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE 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, the previous commit actually started to share some code, we added the shared x86_linux_tdesc_for_tid function 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 to 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. On the GDB side we create target descriptions using a different set of cpu features than on the gdbserver side! This means that for the exact same target, we might get a different target description when using native GDB vs using gdbserver. This surely feels like a mistake, I would expect to get the same target description on each. The table below shows the number of possible different target descriptions that we can create on the GDB side vs on the gdbserver side for each target type: | GDB | gdbserver ------|-----|---------- i386 | 64 | 7 amd64 | 32 | 7 x32 | 16 | 7 So in theory, all I want to do is move the GDB version of *_read_description into the nat/ directory and have gdbserver use that, then both GDB and gdbserver would be able to create any of the possible target descriptions. 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. ** START OF AN ASIDE ** Back in the day I suspect this approach 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 passing the index is now more trouble than its worth. We used to pass the index, and then use that index to lookup which target description to instantiate and use. 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 now 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. ** 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. 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 cpu 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 create 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 create target descriptions using the same set of cpu 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 does. Instead this commit is all about bringing GDB and gdbserver into alignment functionally so that the next commit can merge the GDB and gdbserver versions of these functions. --- gdbserver/linux-amd64-ipa.cc | 43 +---- gdbserver/linux-i386-ipa.cc | 23 +-- gdbserver/linux-x86-low.cc | 15 +- gdbserver/linux-x86-tdesc.cc | 315 +++++++++++++++++++++++++---------- gdbserver/linux-x86-tdesc.h | 49 +++--- 5 files changed, 277 insertions(+), 168 deletions(-) diff --git a/gdbserver/linux-amd64-ipa.cc b/gdbserver/linux-amd64-ipa.cc index f97b0d6a1d9..42de7d2da16 100644 --- a/gdbserver/linux-amd64-ipa.cc +++ b/gdbserver/linux-amd64-ipa.cc @@ -169,47 +169,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 @@ -277,11 +251,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_ipa_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_ipa_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 459b8055b5c..246bcb9813b 100644 --- a/gdbserver/linux-i386-ipa.cc +++ b/gdbserver/linux-i386-ipa.cc @@ -246,28 +246,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 @@ -289,6 +276,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_ipa_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 9bf369f8a34..6dbefdb2f26 100644 --- a/gdbserver/linux-x86-low.cc +++ b/gdbserver/linux-x86-low.cc @@ -2892,14 +2892,17 @@ 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 (tdesc == tdesc_i386_linux_no_xml.get () #ifdef __x86_64__ - return amd64_get_ipa_tdesc_idx (tdesc); -#endif - - if (tdesc == tdesc_i386_linux_no_xml.get ()) - return X86_TDESC_SSE; + || tdesc == tdesc_amd64_linux_no_xml.get () +#endif /* __x86_64__ */ + ) + return x86_linux_xcr0_to_tdesc_idx (X86_XSTATE_SSE_MASK); + } - return i386_get_ipa_tdesc_idx (tdesc); + return x86_linux_xcr0_to_tdesc_idx (xcr0_storage); } /* The linux target ops object. */ diff --git a/gdbserver/linux-x86-tdesc.cc b/gdbserver/linux-x86-tdesc.cc index 9fd64d8574b..87cf368a336 100644 --- a/gdbserver/linux-x86-tdesc.cc +++ b/gdbserver/linux-x86-tdesc.cc @@ -28,96 +28,277 @@ #include "x86-tdesc.h" #include "nat/x86-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 cpu feature 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) +struct x86_tdesc_feature { + /* The cpu 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 cpu features that are + checked when building a target description for i386, amd64, or x32. */ + +static constexpr x86_tdesc_feature x86_linux_all_tdesc_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 cpu features + that are checked for when building an i386 target description. */ + +static constexpr uint64_t +x86_linux_i386_tdesc_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_tdesc_features) + if (entry.is_i386) + mask |= entry.feature; + + return mask; +} + +/* Return a compile time constant which is a mask of all the cpu features + that are checked for when building an amd64 target description. */ + +static constexpr uint64_t +x86_linux_amd64_tdesc_feature_mask () +{ + uint64_t mask = 0; + + for (const auto &entry : x86_linux_all_tdesc_features) + if (entry.is_amd64) + mask |= entry.feature; + + return mask; +} + +/* Return a compile time constant which is a mask of all the cpu features + that are checked for when building an x32 target description. */ + +static constexpr uint64_t +x86_linux_x32_tdesc_feature_mask () +{ + uint64_t mask = 0; + + for (const auto &entry : x86_linux_all_tdesc_features) + if (entry.is_x32) + mask |= entry.feature; + + return mask; +} + +/* Return a compile time constant which is a count of the number of cpu + features that are checked for when building an i386 target description. */ + +static constexpr int +x86_linux_i386_tdesc_count () +{ + uint64_t count = 0; + + for (const auto &entry : x86_linux_all_tdesc_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 cpu + features that are checked for when building an amd64 target description. */ + +static constexpr int +x86_linux_amd64_tdesc_count () +{ + uint64_t count = 0; + + for (const auto &entry : x86_linux_all_tdesc_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 cpu + features that are checked for when building an x32 target description. */ + +static constexpr int +x86_linux_x32_tdesc_count () +{ + uint64_t count = 0; + + for (const auto &entry : x86_linux_all_tdesc_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_ipa_tdesc_count () +{ + return x86_linux_amd64_tdesc_count (); +} + +/* See linux-x86-tdesc.h. */ + +int +x86_linux_x32_ipa_tdesc_count () +{ + return x86_linux_x32_tdesc_count (); +} + +/* See linux-x86-tdesc.h. */ + +int +x86_linux_i386_ipa_tdesc_count () +{ + return x86_linux_i386_tdesc_count (); +} + +#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_tdesc_features); ++i) { - if (is_x32) /* No MPX on x32. */ - return X86_TDESC_AVX; - else - return X86_TDESC_AVX_MPX; + if ((xcr0 & x86_linux_all_tdesc_features[i].feature) != 0) + 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_tdesc_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_tdesc_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 ()] = { }; + +/* 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_tdesc_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 ()); - 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 ()] = { }; + +/* A cache of all possible x32 target descriptions. */ + +static target_desc *x32_tdescs[x86_linux_x32_tdesc_count ()] = { }; + +/* 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_tdesc_feature_mask (); + else + xcr0 &= x86_linux_amd64_tdesc_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 ()); + else + gdb_assert (idx >= 0 && idx < x86_linux_amd64_tdesc_count ()); - 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 +308,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..70456e4be44 100644 --- a/gdbserver/linux-x86-tdesc.h +++ b/gdbserver/linux-x86-tdesc.h @@ -21,29 +21,30 @@ #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_ipa_tdesc_count (); +extern int x86_linux_x32_ipa_tdesc_count (); +extern int x86_linux_i386_ipa_tdesc_count (); + + +#endif /* IN_PROCESS_AGENT */ #endif /* GDBSERVER_LINUX_X86_TDESC_H */ From patchwork Tue Mar 5 17:00:22 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Burgess X-Patchwork-Id: 86833 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 4D9F8385800A for ; Tue, 5 Mar 2024 17:04:24 +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 32F81385843B for ; Tue, 5 Mar 2024 17:00:43 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 32F81385843B 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 32F81385843B 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=1709658047; cv=none; b=DboS/m5/XYDN2pO07VhODW4O0QJu6mxp1T8Cqsu3DwnWrlRa9Yzqd8zTxVO5VU5dgjK638POmsVmIQz19zDpOxR2o650IUOGeBy7mh3pQbKrPHOCCauxj/CFiTsWHstWyGMVHtHmQxmOob1vhh0LgSYdGwXczRg81s73o+TIanU= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1709658047; c=relaxed/simple; bh=+HVjUn1eK5TA7AIc9fr50qOIF8mtL+abalhb6qLzmyE=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=QJuTe07+YJHG1NI3F4ZMQWV2Y7aFVNhMEM4jPL3DcppjYU42Vf8ftYBVzERBB/YrrnF/yXbBb2LCGCYcRsO7w7WKR2D/QPr/ipLl+W6fftdYW93C4mQEcDzigQdGh1Ntw6FTs7wircsA6sYtftIeR937vdltw0pXsnsnlkOU1iY= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1709658042; 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=RAfeTkWm293x+ECS9RWloMwYDzvtqPYDqOtBFsl5awE=; b=EIaOOS1JWICsB3rj5m+AznNx3xYRMnHe+zMkGGHauRU/BAVn5QywGFQHo6t4cTZljn2fCI Sjlm/lSvqZa3UYaVbjU37dRd19HU6rKYuYXBxEozEPcCzkkx+XustalaGwUhhuBV36GqJw 6TSsYr1upwJ7UEzjiwp8UYrE+C46Is0= Received: from mail-lf1-f70.google.com (mail-lf1-f70.google.com [209.85.167.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-151-JO1AaIbuOSmG0Q7YCxUqyA-1; Tue, 05 Mar 2024 12:00:41 -0500 X-MC-Unique: JO1AaIbuOSmG0Q7YCxUqyA-1 Received: by mail-lf1-f70.google.com with SMTP id 2adb3069b0e04-51325a4d003so6536904e87.3 for ; Tue, 05 Mar 2024 09:00:40 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709658039; x=1710262839; 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=RAfeTkWm293x+ECS9RWloMwYDzvtqPYDqOtBFsl5awE=; b=lbB9Jz4ncf+YCtm/LcHSbeVa1hIRg3ofT+Z/lPldZtF6Q5fzzU0Q2Fy+J7UyoHNTzI xeAG21gk6fzHaDP3wSJynh7MalChMLgBGVkDthi/HMqmxMzefjFqqSvhiLFlUEhWbDXd Evis/rJmwip5WiHxpBl6QSOcFqQe4/p829myDOObfamsdDmK8hZKevnIx/lVokyMIuWk a/DyU50gQ84a6u7WVVBqYVRo5uiZr1SYgbKv53IPJVRZf9/cgjNiWvdLkQnaLHq9qzSq Z9ULf2NrSsBfQVj93dF4pik2tjQlT5voulsqNgodc8Tfslck5eIW4L5h/yGXPzFI+GTM Os1w== X-Gm-Message-State: AOJu0YwQs9AtCGAUlwPZwjHmD45k5OYDR7wqUF2u+5qYsQFohoCUFhks hj6heAPClkKYo/ERPM6VTaI4HYUmVJh1ayOHs5jpPgi9kxqliu41ohK7irQVHNUWaOMHmFEOOOV D4psjPsoJxCxBuCBxQVBHqAFGeQtmqyGrnEwyaWGCEWC5+QGmQaTEx7/lykPU9ZpD8lUXl06Hxp vZiV8fJadNIVtPlMnU2EBJgmUmalTVxBEVVnLcGSVviq0= X-Received: by 2002:a19:2d14:0:b0:513:3435:5120 with SMTP id k20-20020a192d14000000b0051334355120mr1510279lfj.32.1709658038328; Tue, 05 Mar 2024 09:00:38 -0800 (PST) X-Google-Smtp-Source: AGHT+IHGPRMjFKRm8rK8VxesdMG1t9cZI2icxc5p1K5HgjHRk8qk9YKQkhegHG+uXmsBSGKNkfbDMA== X-Received: by 2002:a19:2d14:0:b0:513:3435:5120 with SMTP id k20-20020a192d14000000b0051334355120mr1510250lfj.32.1709658037411; Tue, 05 Mar 2024 09:00:37 -0800 (PST) Received: from localhost (185.223.159.143.dyn.plus.net. [143.159.223.185]) by smtp.gmail.com with ESMTPSA id t15-20020a1709063e4f00b00a42f6d17123sm6134953eji.46.2024.03.05.09.00.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 05 Mar 2024 09:00:37 -0800 (PST) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess Subject: [PATCHv2 7/7] gdb/gdbserver: share x86/linux tdesc caching Date: Tue, 5 Mar 2024 17:00:22 +0000 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.3 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, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE 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 gdb/nat/x86-linux-tdesc.c and combine them so we have just a single copy of each. Then both GDB and gdbserver will link against these shared functions. It is worth reading the description of the previous commit to see why this merging is not as simple as it seems: on the gdbserver side we actually have two users of these functions, gdbserver itself, and the in process agent (IPA). However, the previous commit streamlined the gdbserver code, and so now it is simple to move the two functions along with all their support functions from the gdbserver directory into the gdb/nat/ directory, and then GDB is fine to call these functions. One small 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, that 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. This does mean adding back some non-shared code when this whole series has been about sharing code, but now the only non-shared bit is the single line that is actually different between GDB and gdbserver, all the rest, which is identical, is now shared. I did need to add a new rule to the gdbserver Makefile, this is to allow the nat/x86-linux-tdesc.c file to be compiled for the IPA. --- gdb/amd64-linux-tdep.c | 31 ---- gdb/i386-linux-tdep.c | 30 +--- gdb/nat/x86-linux-tdesc.c | 287 +++++++++++++++++++++++++++++++++++ gdb/nat/x86-linux-tdesc.h | 56 ++++++- gdbserver/Makefile.in | 4 + gdbserver/configure.srv | 2 + gdbserver/linux-amd64-ipa.cc | 1 - gdbserver/linux-i386-ipa.cc | 1 - gdbserver/linux-x86-low.cc | 1 - gdbserver/linux-x86-tdesc.cc | 285 +--------------------------------- gdbserver/linux-x86-tdesc.h | 50 ------ 11 files changed, 355 insertions(+), 393 deletions(-) delete mode 100644 gdbserver/linux-x86-tdesc.h diff --git a/gdb/amd64-linux-tdep.c b/gdb/amd64-linux-tdep.c index 7e0900dc6f9..e149034de21 100644 --- a/gdb/amd64-linux-tdep.c +++ b/gdb/amd64-linux-tdep.c @@ -1579,37 +1579,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/i386-linux-tdep.c b/gdb/i386-linux-tdep.c index f5f7a36bf36..ed1442236e8 100644 --- a/gdb/i386-linux-tdep.c +++ b/gdb/i386-linux-tdep.c @@ -681,29 +681,12 @@ i386_linux_core_read_x86_xsave_layout (struct gdbarch *gdbarch, layout) != 0; } -/* See i386-linux-tdep.h. */ +/* 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) { - 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. */ @@ -716,7 +699,10 @@ 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); + + const struct target_desc *tdesc; + if (xcr0 != 0) + tdesc = i386_linux_read_description (xcr0); if (tdesc != NULL) return tdesc; diff --git a/gdb/nat/x86-linux-tdesc.c b/gdb/nat/x86-linux-tdesc.c index 78d22f55d67..297d1873680 100644 --- a/gdb/nat/x86-linux-tdesc.c +++ b/gdb/nat/x86-linux-tdesc.c @@ -38,6 +38,8 @@ #include #include +#ifndef IN_PROCESS_AGENT + /* See nat/x86-linux-tdesc.h. */ const target_desc * @@ -116,3 +118,288 @@ x86_linux_tdesc_for_tid (int tid, enum tribool *have_ptrace_getregset, gdb_assert_not_reached ("failed to return tdesc"); } + +#endif /* !IN_PROCESS_AGENT */ + + + +/* A structure used to describe a single cpu feature that might, or might + not, be checked for when creating a target description for one of i386, + amd64, or x32. */ + +struct x86_tdesc_feature { + /* The cpu 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 cpu features that are + checked when building a target description for i386, amd64, or x32. */ + +static constexpr x86_tdesc_feature x86_linux_all_tdesc_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 cpu features + that are checked for when building an i386 target description. */ + +static constexpr uint64_t +x86_linux_i386_tdesc_feature_mask () +{ + uint64_t mask = 0; + + for (const auto &entry : x86_linux_all_tdesc_features) + if (entry.is_i386) + mask |= entry.feature; + + return mask; +} + +/* Return a compile time constant which is a mask of all the cpu features + that are checked for when building an amd64 target description. */ + +static constexpr uint64_t +x86_linux_amd64_tdesc_feature_mask () +{ + uint64_t mask = 0; + + for (const auto &entry : x86_linux_all_tdesc_features) + if (entry.is_amd64) + mask |= entry.feature; + + return mask; +} + +/* Return a compile time constant which is a mask of all the cpu features + that are checked for when building an x32 target description. */ + +static constexpr uint64_t +x86_linux_x32_tdesc_feature_mask () +{ + uint64_t mask = 0; + + for (const auto &entry : x86_linux_all_tdesc_features) + if (entry.is_x32) + mask |= entry.feature; + + return mask; +} + +/* Return a compile time constant which is a count of the number of cpu + features that are checked for when building an i386 target description. */ + +static constexpr int +x86_linux_i386_tdesc_count () +{ + uint64_t count = 0; + + for (const auto &entry : x86_linux_all_tdesc_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 cpu + features that are checked for when building an amd64 target description. */ + +static constexpr int +x86_linux_amd64_tdesc_count () +{ + uint64_t count = 0; + + for (const auto &entry : x86_linux_all_tdesc_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 cpu + features that are checked for when building an x32 target description. */ + +static constexpr int +x86_linux_x32_tdesc_count () +{ + uint64_t count = 0; + + for (const auto &entry : x86_linux_all_tdesc_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_ipa_tdesc_count () +{ + return x86_linux_amd64_tdesc_count (); +} + +/* See linux-x86-tdesc.h. */ + +int +x86_linux_x32_ipa_tdesc_count () +{ + return x86_linux_x32_tdesc_count (); +} + +/* See linux-x86-tdesc.h. */ + +int +x86_linux_i386_ipa_tdesc_count () +{ + return x86_linux_i386_tdesc_count (); +} + +#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_tdesc_features); ++i) + { + if ((xcr0 & x86_linux_all_tdesc_features[i].feature) != 0) + 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_tdesc_features); ++i) + { + if ((idx & (1 << i)) != 0) + xcr0 |= x86_linux_all_tdesc_features[i].feature; + } + + return xcr0; +} + +#endif /* IN_PROCESS_AGENT */ + +#if defined __i386__ || !defined IN_PROCESS_AGENT + +/* A cache of all possible i386 target descriptions. */ + +static struct target_desc *i386_tdescs[x86_linux_i386_tdesc_count ()] = { }; + +/* See nat/x86-linux-tdesc.h. */ + +const struct target_desc * +i386_linux_read_description (uint64_t xcr0) +{ + xcr0 &= x86_linux_i386_tdesc_feature_mask (); + int idx = x86_linux_xcr0_to_tdesc_idx (xcr0); + + gdb_assert (idx >= 0 && idx < x86_linux_i386_tdesc_count ()); + + target_desc **tdesc = &i386_tdescs[idx]; + + if (*tdesc == nullptr) + { + *tdesc = i386_create_target_description (xcr0, true, false); + + x86_linux_post_init_tdesc (*tdesc, false); + } + + return *tdesc; +} +#endif + +#ifdef __x86_64__ + +/* A cache of all possible amd64 target descriptions. */ + +static target_desc *amd64_tdescs[x86_linux_amd64_tdesc_count ()] = { }; + +/* A cache of all possible x32 target descriptions. */ + +static target_desc *x32_tdescs[x86_linux_x32_tdesc_count ()] = { }; + +/* 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_tdesc_feature_mask (); + else + xcr0 &= x86_linux_amd64_tdesc_feature_mask (); + + int idx = x86_linux_xcr0_to_tdesc_idx (xcr0); + + if (is_x32) + gdb_assert (idx >= 0 && idx < x86_linux_x32_tdesc_count ()); + else + gdb_assert (idx >= 0 && idx < x86_linux_amd64_tdesc_count ()); + + target_desc **tdesc = nullptr; + + if (is_x32) + tdesc = &x32_tdescs[idx]; + else + tdesc = &amd64_tdescs[idx]; + + if (*tdesc == nullptr) + { + *tdesc = amd64_create_target_description (xcr0, is_x32, true, true); + + x86_linux_post_init_tdesc (*tdesc, true); + } + return *tdesc; +} + +#endif diff --git a/gdb/nat/x86-linux-tdesc.h b/gdb/nat/x86-linux-tdesc.h index 3727a8bf95e..648fe0ed0b2 100644 --- a/gdb/nat/x86-linux-tdesc.h +++ b/gdb/nat/x86-linux-tdesc.h @@ -24,6 +24,8 @@ struct target_desc; +#ifndef IN_PROCESS_AGENT + /* Return the target description for Linux thread TID. When *HAVE_PTRACE_GETREGSET is TRIBOOL_UNKNOWN then the current value of @@ -57,19 +59,57 @@ x86_linux_tdesc_for_tid (int tid, enum tribool *have_ptrace_getregset, gdb::function_view xcr0_init_cb, const char *error_msg, uint64_t *xcr0_storage); +#endif /* !IN_PROCESS_AGENT */ + #ifdef __x86_64__ -/* Return the right amd64-linux target descriptions according to - XCR0_FEATURES_BIT and IS_X32. This is implemented separately in both - GDB and gdbserver. */ +/* Return the AMD64 target descriptions corresponding to XCR0 and IS_X32. */ -extern const target_desc *amd64_linux_read_description - (uint64_t xcr0_features_bit, bool is_x32); +extern const target_desc *amd64_linux_read_description (uint64_t xcr0, + bool is_x32); -#endif +#endif /* __x86_64__ */ + +/* Return the i386 target description corresponding to XCR0. */ -/* Return the target description according to XCR0. This is implemented - separately in both GDB and gdbserver. */ extern const struct target_desc *i386_linux_read_description (uint64_t xcr0); +/* 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); + +/* 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_ipa_tdesc_count (); +extern int x86_linux_x32_ipa_tdesc_count (); +extern int x86_linux_i386_ipa_tdesc_count (); + +#endif /* IN_PROCESS_AGENT */ + #endif /* NAT_X86_LINUX_TDESC_H */ diff --git a/gdbserver/Makefile.in b/gdbserver/Makefile.in index d12f8746611..c97649378e8 100644 --- a/gdbserver/Makefile.in +++ b/gdbserver/Makefile.in @@ -517,6 +517,10 @@ gdbsupport/%-ipa.o: ../gdbsupport/%.cc $(IPAGENT_COMPILE) $< $(POSTCOMPILE) +nat/%-ipa.o: ../gdb/nat/%.c + $(IPAGENT_COMPILE) $< + $(POSTCOMPILE) + %-ipa.o: ../gdb/%.c $(IPAGENT_COMPILE) -x c++ $< $(POSTCOMPILE) diff --git a/gdbserver/configure.srv b/gdbserver/configure.srv index 7a2702d78bf..36a457f21ed 100644 --- a/gdbserver/configure.srv +++ b/gdbserver/configure.srv @@ -116,6 +116,7 @@ case "${gdbserver_host}" in 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} nat/x86-linux-tdesc-ipa.o" ;; i[34567]86-*-mingw*) srv_regobj="" srv_tgtobj="x86-low.o nat/x86-dregs.o win32-low.o" @@ -380,6 +381,7 @@ case "${gdbserver_host}" in 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} nat/x86-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 42de7d2da16..afa6aab5bab 100644 --- a/gdbserver/linux-amd64-ipa.cc +++ b/gdbserver/linux-amd64-ipa.cc @@ -21,7 +21,6 @@ #include "server.h" #include #include "tracepoint.h" -#include "linux-x86-tdesc.h" #include "gdbsupport/x86-xstate.h" #include "nat/x86-linux-tdesc.h" diff --git a/gdbserver/linux-i386-ipa.cc b/gdbserver/linux-i386-ipa.cc index 246bcb9813b..f6d26f784af 100644 --- a/gdbserver/linux-i386-ipa.cc +++ b/gdbserver/linux-i386-ipa.cc @@ -21,7 +21,6 @@ #include "server.h" #include #include "tracepoint.h" -#include "linux-x86-tdesc.h" #include "gdbsupport/x86-xstate.h" #include "nat/x86-linux-tdesc.h" diff --git a/gdbserver/linux-x86-low.cc b/gdbserver/linux-x86-low.cc index 6dbefdb2f26..7134d20dd7f 100644 --- a/gdbserver/linux-x86-low.cc +++ b/gdbserver/linux-x86-low.cc @@ -47,7 +47,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 87cf368a336..39d1d51b8c0 100644 --- a/gdbserver/linux-x86-tdesc.cc +++ b/gdbserver/linux-x86-tdesc.cc @@ -19,292 +19,19 @@ #include "server.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" -#endif #include "x86-tdesc.h" #include "nat/x86-linux-tdesc.h" -/* A structure used to describe a single cpu feature that might, or might - not, be checked for when creating a target description for one of i386, - amd64, or x32. */ - -struct x86_tdesc_feature { - /* The cpu 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 cpu features that are - checked when building a target description for i386, amd64, or x32. */ - -static constexpr x86_tdesc_feature x86_linux_all_tdesc_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 cpu features - that are checked for when building an i386 target description. */ - -static constexpr uint64_t -x86_linux_i386_tdesc_feature_mask () -{ - uint64_t mask = 0; - - for (const auto &entry : x86_linux_all_tdesc_features) - if (entry.is_i386) - mask |= entry.feature; - - return mask; -} - -/* Return a compile time constant which is a mask of all the cpu features - that are checked for when building an amd64 target description. */ - -static constexpr uint64_t -x86_linux_amd64_tdesc_feature_mask () -{ - uint64_t mask = 0; - - for (const auto &entry : x86_linux_all_tdesc_features) - if (entry.is_amd64) - mask |= entry.feature; - - return mask; -} - -/* Return a compile time constant which is a mask of all the cpu features - that are checked for when building an x32 target description. */ - -static constexpr uint64_t -x86_linux_x32_tdesc_feature_mask () -{ - uint64_t mask = 0; - - for (const auto &entry : x86_linux_all_tdesc_features) - if (entry.is_x32) - mask |= entry.feature; - - return mask; -} - -/* Return a compile time constant which is a count of the number of cpu - features that are checked for when building an i386 target description. */ - -static constexpr int -x86_linux_i386_tdesc_count () -{ - uint64_t count = 0; - - for (const auto &entry : x86_linux_all_tdesc_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 cpu - features that are checked for when building an amd64 target description. */ - -static constexpr int -x86_linux_amd64_tdesc_count () -{ - uint64_t count = 0; - - for (const auto &entry : x86_linux_all_tdesc_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 cpu - features that are checked for when building an x32 target description. */ - -static constexpr int -x86_linux_x32_tdesc_count () -{ - uint64_t count = 0; - - for (const auto &entry : x86_linux_all_tdesc_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_ipa_tdesc_count () -{ - return x86_linux_amd64_tdesc_count (); -} - -/* See linux-x86-tdesc.h. */ - -int -x86_linux_x32_ipa_tdesc_count () -{ - return x86_linux_x32_tdesc_count (); -} - -/* See linux-x86-tdesc.h. */ - -int -x86_linux_i386_ipa_tdesc_count () -{ - return x86_linux_i386_tdesc_count (); -} - -#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_tdesc_features); ++i) - { - if ((xcr0 & x86_linux_all_tdesc_features[i].feature) != 0) - 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_tdesc_features); ++i) - { - if ((idx & (1 << i)) != 0) - xcr0 |= x86_linux_all_tdesc_features[i].feature; - } - - return xcr0; -} - -#endif /* IN_PROCESS_AGENT */ - -#if defined __i386__ || !defined IN_PROCESS_AGENT - -/* A cache of all possible i386 target descriptions. */ - -static struct target_desc *i386_tdescs[x86_linux_i386_tdesc_count ()] = { }; - /* 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_tdesc_feature_mask (); - int idx = x86_linux_xcr0_to_tdesc_idx (xcr0); - - gdb_assert (idx >= 0 && idx < x86_linux_i386_tdesc_count ()); - - 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 ()] = { }; - -/* A cache of all possible x32 target descriptions. */ - -static target_desc *x32_tdescs[x86_linux_x32_tdesc_count ()] = { }; - -/* 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_tdesc_feature_mask (); + if (is_64bit) + init_target_desc (tdesc, amd64_expedite_regs); else - xcr0 &= x86_linux_amd64_tdesc_feature_mask (); - - int idx = x86_linux_xcr0_to_tdesc_idx (xcr0); - - if (is_x32) - gdb_assert (idx >= 0 && idx < x86_linux_x32_tdesc_count ()); - else - gdb_assert (idx >= 0 && idx < x86_linux_amd64_tdesc_count ()); - - target_desc **tdesc = nullptr; - - if (is_x32) - tdesc = &x32_tdescs[idx]; - 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); } -#endif diff --git a/gdbserver/linux-x86-tdesc.h b/gdbserver/linux-x86-tdesc.h deleted file mode 100644 index 70456e4be44..00000000000 --- a/gdbserver/linux-x86-tdesc.h +++ /dev/null @@ -1,50 +0,0 @@ -/* Low level support for x86 (i386 and x86-64), shared between gdbserver - and IPA. - - Copyright (C) 2016-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 GDBSERVER_LINUX_X86_TDESC_H -#define GDBSERVER_LINUX_X86_TDESC_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. */ - -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_ipa_tdesc_count (); -extern int x86_linux_x32_ipa_tdesc_count (); -extern int x86_linux_i386_ipa_tdesc_count (); - - -#endif /* IN_PROCESS_AGENT */ - -#endif /* GDBSERVER_LINUX_X86_TDESC_H */