From patchwork Sat Mar 23 16:35: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: 87569 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 C43E7385828F for ; Sat, 23 Mar 2024 16:36:45 +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 904833858D38 for ; Sat, 23 Mar 2024 16:35:38 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 904833858D38 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 904833858D38 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=1711211740; cv=none; b=xLBGDOxPfmFdHo8IbT9vQrcdMwERUUpA9KQXLas8tQlq/BGdgHKxElnwuX7OvtiRTthxmyPL1YnSA80oFt/xCSGm3WYFjyE/8mqfxlO4eKVx2blqdOb2CtO0IY49Lz6qkXQBmh0a5hRQbIjaWL3LsG9Shv39nVkIVqTMHRVh+Yk= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1711211740; c=relaxed/simple; bh=oB82Vpp3nyBGXoOttoDwl9pTznBZiedWVnuzaFQXAJY=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=UwbAaJkrOUPC0AY0lFJ77PdA507xjZD0kNTuO/24u0AmmWKWP2Fjuqfrss6/hzWaSR1ZcW/Jd23zQG3lWZNPPDVMxjzBnacV10X14TovKnaNaeqOcsIJaUoSwddrNNG265j71N1YVzPcwBmfupCdYxM/h0o67GgzoLj63/efsmY= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1711211738; 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=iMPU1cT5JxVhOB2Q2fm/P/aatWjIoYW/BIrU7ZhJ69w=; b=g7PXPanZbPdl0Wphd7mjrKxfS457Vw8ziu3nNER/O9KSJQzIXV99BM98yFxaed91ulpKH7 pwPaH64LNqrs1Bc0yXKWJ+HPVKOnYPSEuCt3WZC+OQ4HwSzKeS+WRtbJ4tQbh6s5XDUwYS rb99Y4rQoAj+Yja5WxL0mLTUsJgzmoM= Received: from mail-lf1-f72.google.com (mail-lf1-f72.google.com [209.85.167.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-154-ZmRO_xKINNSkD8SU0jsXvg-1; Sat, 23 Mar 2024 12:35:36 -0400 X-MC-Unique: ZmRO_xKINNSkD8SU0jsXvg-1 Received: by mail-lf1-f72.google.com with SMTP id 2adb3069b0e04-513e7ff16a8so2371625e87.1 for ; Sat, 23 Mar 2024 09:35:36 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711211734; x=1711816534; 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=iMPU1cT5JxVhOB2Q2fm/P/aatWjIoYW/BIrU7ZhJ69w=; b=aRxEZkHZmT7JAlnyt+aNU+yN2CTfTxBn0/rhyR+aZNoMJy22Mw84hQ5vE3EeoMvOqH s6sl0DwdWbbWPWMc2rT/HyE6okskFLEv0jCDJxYWqL+5RcagJR9S1YeMypcSWRnvMqqi Nd4dtcrjD60x1OdGnIrsfw8CA/BMIW57G0kO9cT6vIwMYbt2jQcTwD21kFNDdhedO1HK g/gOLLONdf5gHBK/8Kmu3/gwfPTXW5eAydTZoqjB4+KDQK6ff0KslM+KP2sXYg690BqY ix/JIVINgz7vwr4y7at4vw7y1HNJaAyjQHpRxLyK6M5n/otk+qrdjfyz5e9SYxBXNiGL Ljsw== X-Gm-Message-State: AOJu0YxmUAn9hSc39yiPVDp5wza8qrfdQVH9hAi5OYSbrNLfttrU+SbH jFiQJi2G+xbUfBtPfTxJAaum74Tmvi7CU2r3zyIxSSf8JRdgIo4b1CMw8QtjD/0gQOrK6jtwjqL Tw8HejjuG4m8InN69MkFThxHP9aunNNK374ihLLOKporPzJdVbNw8V3k14WeV+XQbMFNFfFsa8I AykpD6YNPcHBa2uR0UTWXQpMRFb16xgJJNQBLQX/EZ+vQ= X-Received: by 2002:ac2:4db5:0:b0:513:c146:fc02 with SMTP id h21-20020ac24db5000000b00513c146fc02mr1608830lfe.6.1711211734446; Sat, 23 Mar 2024 09:35:34 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFJL/p/GQpdfk5wRyXzyxJvayDH8XbjGUNtyXv1pcyxEjlM0deFhd8b8obXO+p54Ed9qnWkzQ== X-Received: by 2002:ac2:4db5:0:b0:513:c146:fc02 with SMTP id h21-20020ac24db5000000b00513c146fc02mr1608802lfe.6.1711211733033; Sat, 23 Mar 2024 09:35:33 -0700 (PDT) Received: from localhost (185.223.159.143.dyn.plus.net. [143.159.223.185]) by smtp.gmail.com with ESMTPSA id jw3-20020a170906e94300b00a474e6a5723sm60329ejb.118.2024.03.23.09.35.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 23 Mar 2024 09:35:31 -0700 (PDT) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess , hjl.tools@gmail.com, John Baldwin Subject: [PATCHv3 1/8] gdbserver: convert have_ptrace_getregset to a tribool Date: Sat, 23 Mar 2024 16:35: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=-12.1 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org 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. Approved-By: John Baldwin --- 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 933d1fb012a..04202e355bb 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 @@ -955,7 +955,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 Sat Mar 23 16:35: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: 87573 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 74FB43858410 for ; Sat, 23 Mar 2024 16:37:28 +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 F1C203858C33 for ; Sat, 23 Mar 2024 16:35:39 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org F1C203858C33 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 F1C203858C33 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=1711211741; cv=none; b=WKWBNmedbo9iAHGWOMNSbSESyB6gE3eP+7XSWqFJvkZvE9HTy06/ETSv2tt71pWz+vNIqR1IxfncbzjBqFp1FCD1JMzdwM1EkKAg3akpNwFyf6gE3p+vbqkRxS6frra9ND+4fs6SQcqjwDBkZFImY8Vh3b9eJTEDDkgBMuSN+eU= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1711211741; c=relaxed/simple; bh=Ller8/aehxm4RpXqOfF53+zCGeQwfvrmtztk/3YVc7k=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=QxNV2g7o7gVvqmDQTbyUZGuWCj/jTc0FwADDe6px2+q5XS0dl47DtCuhVDei/qgVq4gO47Z0sZhk87srb6amsVmApm5j+igerDQTP2XVs/jQGxxXMivVllXb9Y+arvXljvjZ32EFlgasbqwrbEirfXYVAyOWPF95JS2gNDgBOWU= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1711211739; 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=XgTm4/kQ9kZ2CUD+KyJ3m1I5WeQCMdGM6y9DiUOPzFA=; b=bTurCBq9TjBU5QJvby53d/hVldObJKjrn7LKYwH6hjsvAl/djTemqE1HkdxSKTBfeOKB8y KejC2OUaNMKsiQIkzMPKfbJambGXQFsutuJ8r8Nku02v9TdipKOYIZuARDuqT1jw76dA8S 5fuMS1zJXTwv+Skximzrx92SIAWj/3w= Received: from mail-ej1-f71.google.com (mail-ej1-f71.google.com [209.85.218.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-164-YW3OzYtjP5WC0fH8IX8d_Q-1; Sat, 23 Mar 2024 12:35:37 -0400 X-MC-Unique: YW3OzYtjP5WC0fH8IX8d_Q-1 Received: by mail-ej1-f71.google.com with SMTP id a640c23a62f3a-a4715d8c761so172653666b.1 for ; Sat, 23 Mar 2024 09:35:37 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711211735; x=1711816535; 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=XgTm4/kQ9kZ2CUD+KyJ3m1I5WeQCMdGM6y9DiUOPzFA=; b=IUQiFEOD7s03PptJdnNflRuuOfk82wSPw0jvUPkR0Sk0tBD8sLKB4JOOljAMO3GBCR 5AS+Hzx+DGzIEQWbLwwhw9t0gDoORQAWfwtJt7AvPySkPrpIXaDg8koGB2ppmsUbZln+ PkoX4IpHycEN25k0MbsVAr34Aohh/tdzRdhTWAX18M/icEtIOBmXQ3yErnY+Ygfx+Ird kLA/fcFJznVfyjPu5xo+QmpM6/swnyW3H28Gse6oyT00gSnlmpeSSfUw+sFe/9J+87Qi wc7vLbkLkTtiKIj2kM+slbtazATsZ4hYsrHwvNnOQrPcUU65RnbW1vA2H1D8fo1h4C86 BOOQ== X-Gm-Message-State: AOJu0YwNkGnw6br263VHIYdYlSrWtXvWmscN7ArRN7KikOu0LXIYovHO wGaTGapLFOArn9KOa1LrQLLZv1pefP8Wf1zR17aL87fAyR9FWBz0F9WEoHuLQ2WFOscBMP89LdE iCd8tC1A+0ka0ByODETFduIOuSViYcVJltREgcDGrx3mXui9IkMQh5Q4MKR0dglUYOSETAlkSBl 3Zv+Z3ja7cCvQSfZQw+446wQ9bATK2qW/0TD2dGHwaC10= X-Received: by 2002:a05:6402:5215:b0:56b:ff5b:c49c with SMTP id s21-20020a056402521500b0056bff5bc49cmr620650edd.33.1711211734787; Sat, 23 Mar 2024 09:35:34 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHzCMuC0cSljpfaWbkstVrHVQhpse2+IhUPxlbpBrnM+fMOhlhOz/pWoAgFAlqsniT+HnHaVQ== X-Received: by 2002:a05:6402:5215:b0:56b:ff5b:c49c with SMTP id s21-20020a056402521500b0056bff5bc49cmr620628edd.33.1711211734207; Sat, 23 Mar 2024 09:35:34 -0700 (PDT) Received: from localhost (185.223.159.143.dyn.plus.net. [143.159.223.185]) by smtp.gmail.com with ESMTPSA id ef3-20020a05640228c300b0056bfb4a94e9sm602000edb.59.2024.03.23.09.35.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 23 Mar 2024 09:35:33 -0700 (PDT) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess , hjl.tools@gmail.com, John Baldwin Subject: [PATCHv3 2/8] gdb/x86: move reading of cs and ds state into gdb/nat directory Date: Sat, 23 Mar 2024 16:35:20 +0000 Message-Id: <35cb0063d1d6ec15f1ecda637e69c2a7248dcb95.1711211528.git.aburgess@redhat.com> X-Mailer: git-send-email 2.25.4 In-Reply-To: References: MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.1 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org This 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). Approved-By: John Baldwin --- 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 Sat Mar 23 16:35: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: 87568 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 CE9663858434 for ; Sat, 23 Mar 2024 16:36:23 +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 1952B385841D for ; Sat, 23 Mar 2024 16:35:41 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 1952B385841D 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 1952B385841D 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=1711211743; cv=none; b=w6OYSrFr8Sj2HLfoXziHK1m0MCTkKjwJmNDtq/97hG7v1orq475bFRmF90O+p+DihCVOJVsZrNrlZ9yOROXi2ISuLJWmkx/SOLOJF+j+JLtCZ5bHGNX36YyUPF+c4W9gTjWdRB0CPHGHcRZSSrxGusz0Gcq32vexPAiBSAI5890= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1711211743; c=relaxed/simple; bh=7t4LmZg+ucPi0FDIQOGKk4lXbxWxFF/sf+7DKEfWFT0=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=NBV7cholx0RnQORjoGj7+n+Vgp4stTrDybYdVfl9+ROSIwWM4wyUk/UW+2XJ4yD19PIzr2CHWjiujT5V0ftearUVFJSwKR7NhKMc5HMLBaE3taKYYq91q5hkA5Ib7ZiAYacvT8Xn9mVl/AeRillPQgRb0Jyeo96LxP2YHkQAZ+w= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1711211740; 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=qhce6W3Po2gQnGr5GFOqJIP7oKiLB2b/gCArYC+U3l4=; b=LvnLSOCFnN9KdRpdepGMGCo0ccsoNPqTjYQbv4m4cmIlDF/fCiDa0G5zE+aqUnqrCnBrJy FE7MoJGTIGyADYYF9gZMnEQRDjDS4NsJde6S44VZ0cNAik2xVRVC9RqW+Zh7zA/o/+7N7X OWoOYNiqdBJ0e3Oex2OAmQbB14+iu1I= Received: from mail-lf1-f71.google.com (mail-lf1-f71.google.com [209.85.167.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-190-AzTNILw3MI66ZVdz1dwDkg-1; Sat, 23 Mar 2024 12:35:39 -0400 X-MC-Unique: AzTNILw3MI66ZVdz1dwDkg-1 Received: by mail-lf1-f71.google.com with SMTP id 2adb3069b0e04-513e7ff16a8so2371629e87.1 for ; Sat, 23 Mar 2024 09:35:38 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711211737; x=1711816537; 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=qhce6W3Po2gQnGr5GFOqJIP7oKiLB2b/gCArYC+U3l4=; b=FBN7Wd82wp7Y/d4leNkNh9almlyYLzZ3noIH97iOKytkFx3iWBZQoo5GzeS7VsaK9O sF28bsiDJovBVa847bH2PnrlFHxZ+fuiAIJQKrKeoH24ZD1K2pDruqmgLdzWLwIsU7Zf 8ERo1UGLXk39ySpPffo/XCdh1e9TRgNRpBQdeHF7Wjdjfa21FMp5OCQRWdP8woZTRpuw hhIHY6DRrxxuYT6BYxbJkzXb/8AbG1ngZOBeZbGXwSMdRPs3SWckDQrZcUIMf+xoWnlw T2Sw957KeHwOyYXap8+vAuEhNAeC6bJv6rfBm9mBtpRDJaIx/VsU2pxqHON79FuMStRt S1nA== X-Gm-Message-State: AOJu0Yysav7H1oXSHYScdcKA6C87SnF0ykkhKo5R2lmHLgz6OYgL8lLh efaPn3qz3IZp2q4CHztFP5A76WTDsh+VzprB2ahwICVF2xUgbypyGja7qELCIBvaopTHKSEDAq6 esX3MbO7MxaD47wVOgFOX3fP8/vH+DfdHvVdmXGisvlnzN8CHSQSyZuW7qmMscjcwRuq2iOd/f9 IR0AdtVkaKiBZp2e6qJatoxdXegkwlVnWLsHbloA6XSsY= X-Received: by 2002:a19:7615:0:b0:513:5a9e:78af with SMTP id c21-20020a197615000000b005135a9e78afmr1692840lff.58.1711211737193; Sat, 23 Mar 2024 09:35:37 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGzZIuuvqkkrfChUsHktsjFH6tmeN+FHd82NIFD1BrIpP1GNBhQ5GanZtxkyjQ7bi49wV22+Q== X-Received: by 2002:a19:7615:0:b0:513:5a9e:78af with SMTP id c21-20020a197615000000b005135a9e78afmr1692826lff.58.1711211736740; Sat, 23 Mar 2024 09:35:36 -0700 (PDT) Received: from localhost (185.223.159.143.dyn.plus.net. [143.159.223.185]) by smtp.gmail.com with ESMTPSA id q3-20020a5085c3000000b005645961ad39sm1019729edh.47.2024.03.23.09.35.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 23 Mar 2024 09:35:35 -0700 (PDT) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess , hjl.tools@gmail.com, John Baldwin Subject: [PATCHv3 3/8] gdbserver/x86: move no-xml code earlier in x86_linux_read_description Date: Sat, 23 Mar 2024 16:35:21 +0000 Message-Id: <7934bb6ffde7ea8608245d9a7f6f380bee50a99f.1711211528.git.aburgess@redhat.com> X-Mailer: git-send-email 2.25.4 In-Reply-To: References: MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.1 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org This commit is part of a series that aims to share more of the x86 target description reading/generation code between GDB and gdbserver. There are a huge number of similarities between the code in gdbserver's x86_linux_read_description function and GDB's x86_linux_nat_target::read_description function, and it is this similarity that I plan, in a later commit, to share between GDB and gdbserver. However, one thing that is different in x86_linux_read_description is the code inside the '!use_xml' block. This is the code that handles the case where gdbserver is not allowed to send an XML target description back to GDB. In this case gdbserver uses some predefined, fixed, target descriptions. First, it's worth noting that I suspect this code is not tested any more. I couldn't find anything in the testsuite that tries to disable XML target description support. And the idea of having a single "fixed" target description really doesn't work well when we think about all the various x86 extensions that exist. Part of me would like to rip out the no-xml support in gdbserver (at least for x86), and if a GDB connects that doesn't support XML target descriptions, gdbserver can just give an error and drop the connection. GDB has supported XML target descriptions for 16 years now, I think it would be reasonable for our shipped gdbserver to drop support for the old way of doing things. Anyway.... this commit doesn't do that. What I did notice was that, over time, the '!use_xml' block appears to have "drifted" within the x86_linux_read_description function; it's now not the first check we do. Instead we make some ptrace calls and return a target description generated based on the result of these ptrace calls. Surely it only makes sense to generate variable target descriptions if we can send these back to GDB? So in this commit I propose to move the '!use_xml' block earlier in the x86_linux_read_description function. The benefit of this is that this leaves the later half of x86_linux_read_description much more similar to the GDB function x86_linux_nat_target::read_description and sets us up for potentially sharing code between GDB and gdbserver in a later commit. Approved-By: John Baldwin --- 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 04202e355bb..872c3fc69c7 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 Sat Mar 23 16:35: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: 87570 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 542923858435 for ; Sat, 23 Mar 2024 16:37:07 +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 F007D385840F for ; Sat, 23 Mar 2024 16:35:42 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org F007D385840F 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 F007D385840F 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=1711211746; cv=none; b=b1IMggTX6g+Yxk5IMe3lrlCFwzQ1b5bkRLsg5fufGuzirelfKUWw/PALm5Cya7HKhKGG6pjfIUuA3C6nerMAFxWb3oPiK3dVDyxuhTQ9endXN8C11frnsegLvZzOh0Wk/KMCAeEv5KyW474Gzj3zoHC1u6NVdmeivZfLD+NrQ1c= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1711211746; c=relaxed/simple; bh=hH3YNgD4XDH5IYKwDO76P82rCRevIlDgzJ0C8HRO9sE=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=Bp6bXBkmuebOr0i8Vnam9yxvultPP3vLvVSANvgMI9rOO36GaFHK3P6vCITzNy62Yf4g6gJc7AYQs53qQDoFqrVZqjEf+6K/Tz+4BdnDdYHMRNrGEbwmn7SwVKTGTZh+NdO8PATpdBTv2g81WzuYnITJ9bHcANGe5D2NTUxEoVg= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1711211742; 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=mPv8wUlIPJiHsLm7VbGVGs4nXwQEddc/JamEPU0vjuY=; b=Iiyft6WwlM3sYzqHSu55SS6juhmydrOM775UKNS1HkwIQ+ClWMVp3wliP0N0HpD5GV6U3Y RIWnLFhiPCJyrSAOcmkNj/bQQmDffT+U2zm148QYtd9YzYMLt+mNfETK1tcwg0h6EzaASo iRMYpBCesUQew6PuIEtuzOBdUFYfGGk= 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-307-HzZiF_6cNcqGgDeEM4hvHg-1; Sat, 23 Mar 2024 12:35:41 -0400 X-MC-Unique: HzZiF_6cNcqGgDeEM4hvHg-1 Received: by mail-ej1-f70.google.com with SMTP id a640c23a62f3a-a4748cfa0f6so23364766b.1 for ; Sat, 23 Mar 2024 09:35:40 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711211739; x=1711816539; 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=MESXLCqT5T4epsKKM7Vjsp8kP6SStrg9yZ2GZQjH1pg=; b=o0GHx9oSZx9hufNL9XRwPCnEsmjhJcbKTarbLqudW5FosyulX7w0jGHk1Ox6qIEuME orEg3DPXbD2f0W5ovxvtWK/aZ+C5qgHKran1CnlqPm+taWET+54e+Pb3MLjc3t5Q28vo ShyTc19Xhgl0WCQMSVb4kZvqqhlz11NfMJavRLww8Pad8YQgW55xq41KEPqfs/sK3znS YSMuR9+Se1ksFrXfYIPzcQ9Wzs8TXMt6cc7V+oCEMoAWbW0Te/e4qtX96olXUDvsjm9X JveQOIOVXmS3oFIxGVv1P4ui6uOC57fXgmcO8HdRz+Fob7Hd3esEqKT5SE0Nyi16veaW u1CA== X-Gm-Message-State: AOJu0Yyurd5ZirFGuBMm+KoxFA0rl7h2T8v7Hzz8f/iyNk7P4wNNYYDZ Xs6Xatx4YcNr49bptQH0aG4ovE2CFusN+5LpeBN15ZCvhyE8gQSA73y+H4YQOWcYFdTkMEmjFqQ vGnyDE+ZkvFA5+/tlUoVPZGq21zhLF6AoniF0o7vpNErU+tfwwlUQc19ftVyENjoDIL97eyiJFi gdNQOPMHDzQkF6uSIlFIZ6VuwbOXEk3d1MTJFbyhYpydY= X-Received: by 2002:a50:f692:0:b0:56b:ff60:3984 with SMTP id d18-20020a50f692000000b0056bff603984mr468219edn.9.1711211738832; Sat, 23 Mar 2024 09:35:38 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHy9/0Sxwe/TjeQe/VTGyouqXeBNrhgpvCIaQslBOKKqLR7J3tRJW7U+xpE9V/acEeVI9Vaug== X-Received: by 2002:a50:f692:0:b0:56b:ff60:3984 with SMTP id d18-20020a50f692000000b0056bff603984mr468206edn.9.1711211738183; Sat, 23 Mar 2024 09:35:38 -0700 (PDT) Received: from localhost (185.223.159.143.dyn.plus.net. [143.159.223.185]) by smtp.gmail.com with ESMTPSA id r10-20020a508d8a000000b0056ba017ca7fsm994935edh.87.2024.03.23.09.35.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 23 Mar 2024 09:35:37 -0700 (PDT) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess , hjl.tools@gmail.com, John Baldwin Subject: [PATCHv3 4/8] gdb/gdbserver: share I386_LINUX_XSAVE_XCR0_OFFSET definition Date: Sat, 23 Mar 2024 16:35:22 +0000 Message-Id: <85d9930527784778e52ddc7f633eb528ba606b41.1711211528.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, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org Share the definition of I386_LINUX_XSAVE_XCR0_OFFSET between GDB and gdbserver. This commit 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. Approved-By: John Baldwin --- 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 872c3fc69c7..30d876efc5d 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 Sat Mar 23 16:35:23 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Burgess X-Patchwork-Id: 87571 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 E9D58385841C for ; Sat, 23 Mar 2024 16:37:07 +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 942BE3858C62 for ; Sat, 23 Mar 2024 16:35:44 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 942BE3858C62 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 942BE3858C62 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=1711211749; cv=none; b=UGQxa7PLen4AVwOjSesNKO5Qx7SpTRo9/5kHvBqZXUgZFB4GlVN1OC8lsGqyxzLpu33bUwPK1eq0Jm/jNCblf+YGpwxe4AQ3CusZJyC4xcmTyTL0i3qdjm1o2kwb4JkDkbi5uSrDSRwmhHTEQBrgeuzi5jdoF7pVuw9jqhk3qA8= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1711211749; c=relaxed/simple; bh=YNT/xKl7wOwHlOxbjxkVUItRkwEfwH8x9fG+m4MYUgo=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=qsa7bWZMl7Gnq1bqGmxRrUMzaEMqblCH+cBk+SGYrGJS/HXVzB8NbF2zWiESIKOEf718Uv3CcdzfjHvxsi2OERONFrtUIiPrudKm2B9RX7ZtVeSW2Jljh1TwZUHsaOTrI2sVd5sY5zOcIOzh5EDvdNnoDCzF0ydONIOqCKGjUW8= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1711211744; 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=uhm0F89XERsQD/D9dHH+I3s8jyttuoGwdac+6HYl5dM=; b=He6lyt/XmqKJ3V93iT/ZOI1BR2NNvRpTzkgtDyAZ+LlxXTxSkbD9w71A2bjW0Lw7rNLXai K6699WglYowcW6O8QXybG0jdsxzxEYzpYzuOkMTikJ6wKdfXV+/D6YLbfuURNbXchdyZWG k8UugnyLU6KUmY2/WwA8iJRGbrWQleA= 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-54-ZIC869XQO0aEKjTFvuaUTg-1; Sat, 23 Mar 2024 12:35:42 -0400 X-MC-Unique: ZIC869XQO0aEKjTFvuaUTg-1 Received: by mail-ej1-f72.google.com with SMTP id a640c23a62f3a-a473769f8d0so74626966b.0 for ; Sat, 23 Mar 2024 09:35:42 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711211740; x=1711816540; 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=hs890BarobdMEHS1xibQ6LJDQwUwMYwnjPmcreIkcTA=; b=bYFtjz7NdW0tB/PdutzLdO/nC6ObFDQ+YHBQVwsOxeDVireIg0x6Le0YMzJDerIFz+ sD2OV84wk6nVWtek4fD1/Ksc/vUhG1l5Dw9rI7thXrFuiFIalOTDk05QQOiGAi1B98zD rtNfp4Ck7uplG8AOvY2aoHJBXo1++pc9IR7+VzbkvgYCU8KSgU2qJw3kwt8LO/jKy71z M2Prgl+pOIhz1mAeArTrDroy3GI+SmVHEwU+d4Ef9xIXN6E0O6k50zFIFrsSj82ggReF lJh4gCY8d42bKxPZ/mstPTqyMxUrGVKbLg1VfZJT/LKqq8hJYDwMgQG15Jje8eO9wt2n YghA== X-Gm-Message-State: AOJu0YwEhapz0lnMHB/lRw4ZA4x14hRl6E8O8/WOgcvjw+KJK6TFnZS8 56VgynxSQsCzEg7NBAu1xYQwcz9DyHONVEHLGfxFnlvP49spOc7gFh9ucFAF0HHKcRAtBWafdez vEs0CwGW25UBOIAA8/G4DC4cADu5mCZpJfhKnfw4SVs9NaFXxmGyhanEFY6FnKbg2joUl8v5HuB ReEB++B+cYnB+Z/KXu0RDfjd487SWLRIcG1hUbLQH7CIo= X-Received: by 2002:a17:907:7859:b0:a46:fa6d:9adc with SMTP id lb25-20020a170907785900b00a46fa6d9adcmr2000592ejc.12.1711211740065; Sat, 23 Mar 2024 09:35:40 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFxPEhxKeM1KO6rPpidJo5uCjuwkYlTlXiY06Aaw4VHdqkn/phAu3Y6yUQes/oYQ0bJi6EQ8Q== X-Received: by 2002:a17:907:7859:b0:a46:fa6d:9adc with SMTP id lb25-20020a170907785900b00a46fa6d9adcmr2000573ejc.12.1711211739357; Sat, 23 Mar 2024 09:35:39 -0700 (PDT) Received: from localhost (185.223.159.143.dyn.plus.net. [143.159.223.185]) by smtp.gmail.com with ESMTPSA id t21-20020a170906179500b00a468bf8586bsm1074951eje.174.2024.03.23.09.35.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 23 Mar 2024 09:35:39 -0700 (PDT) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess , hjl.tools@gmail.com, John Baldwin Subject: [PATCHv3 5/8] gdb/gdbserver: share some code relating to target description creation Date: Sat, 23 Mar 2024 16:35:23 +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, KAM_SHORT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org This commit is part of a series to share more of the x86 target description creation code between GDB and gdbserver. Unlike previous commits which were mostly refactoring, this commit is the first that makes a real change, though that change should mostly be for gdbserver; I've largely adopted the "GDB" way of doing things for gdbserver, and this fixes a real gdbserver bug. On a x86-64 Linux target, running the test: gdb.server/connect-with-no-symbol-file.exp results in two core files being created. Both of these core files are from the inferior process, created after gdbserver has detached. In this test a gdbserver process is started and then, after gdbserver has started, but before GDB attaches, we either delete the inferior executable, or change its permissions so it can't be read. Only after doing this do we attempt to connect with GDB. As GDB connects to gdbserver, gdbserver attempts to figure out the target description so that it can send the description to GDB, this involves a call to x86_linux_read_description. In x86_linux_read_description one of the first things we do is try to figure out if the process is 32-bit or 64-bit. To do this we look up the executable via the thread-id, and then attempt to read the architecture size from the executable. This isn't going to work if the executable has been deleted, or is no longer readable. And so, as we can't read the executable, we default to an i386 target and use an i386 target description. A consequence of using an i386 target description is that addresses are assumed to be 32-bits. Here's an example session that shows the problems this causes. This is run on an x86-64 machine, and the test binary (xx.x) is a standard 64-bit x86-64 binary: shell_1$ gdbserver --once localhost :54321 /tmp/xx.x shell_2$ gdb -q (gdb) set sysroot (gdb) shell chmod 000 /tmp/xx.x (gdb) target remote :54321 Remote debugging using :54321 warning: /tmp/xx.x: Permission denied. 0xf7fd3110 in ?? () (gdb) show architecture The target architecture is set to "auto" (currently "i386"). (gdb) p/x $pc $1 = 0xf7fd3110 (gdb) info proc mappings process 2412639 Mapped address spaces: Start Addr End Addr Size Offset Perms objfile 0x400000 0x401000 0x1000 0x0 r--p /tmp/xx.x 0x401000 0x402000 0x1000 0x1000 r-xp /tmp/xx.x 0x402000 0x403000 0x1000 0x2000 r--p /tmp/xx.x 0x403000 0x405000 0x2000 0x2000 rw-p /tmp/xx.x 0xf7fcb000 0xf7fcf000 0x4000 0x0 r--p [vvar] 0xf7fcf000 0xf7fd1000 0x2000 0x0 r-xp [vdso] 0xf7fd1000 0xf7fd3000 0x2000 0x0 r--p /usr/lib64/ld-2.30.so 0xf7fd3000 0xf7ff3000 0x20000 0x2000 r-xp /usr/lib64/ld-2.30.so 0xf7ff3000 0xf7ffb000 0x8000 0x22000 r--p /usr/lib64/ld-2.30.so 0xf7ffc000 0xf7ffe000 0x2000 0x2a000 rw-p /usr/lib64/ld-2.30.so 0xf7ffe000 0xf7fff000 0x1000 0x0 rw-p 0xfffda000 0xfffff000 0x25000 0x0 rw-p [stack] 0xff600000 0xff601000 0x1000 0x0 r-xp [vsyscall] (gdb) info inferiors Num Description Connection Executable * 1 process 2412639 1 (remote :54321) (gdb) shell cat /proc/2412639/maps 00400000-00401000 r--p 00000000 fd:03 45907133 /tmp/xx.x 00401000-00402000 r-xp 00001000 fd:03 45907133 /tmp/xx.x 00402000-00403000 r--p 00002000 fd:03 45907133 /tmp/xx.x 00403000-00405000 rw-p 00002000 fd:03 45907133 /tmp/xx.x 7ffff7fcb000-7ffff7fcf000 r--p 00000000 00:00 0 [vvar] 7ffff7fcf000-7ffff7fd1000 r-xp 00000000 00:00 0 [vdso] 7ffff7fd1000-7ffff7fd3000 r--p 00000000 fd:00 143904 /usr/lib64/ld-2.30.so 7ffff7fd3000-7ffff7ff3000 r-xp 00002000 fd:00 143904 /usr/lib64/ld-2.30.so 7ffff7ff3000-7ffff7ffb000 r--p 00022000 fd:00 143904 /usr/lib64/ld-2.30.so 7ffff7ffc000-7ffff7ffe000 rw-p 0002a000 fd:00 143904 /usr/lib64/ld-2.30.so 7ffff7ffe000-7ffff7fff000 rw-p 00000000 00:00 0 7ffffffda000-7ffffffff000 rw-p 00000000 00:00 0 [stack] ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall] (gdb) Notice the difference between the mappings reported via GDB and those reported directly from the kernel via /proc/PID/maps, the addresses of every mapping is clamped to 32-bits for GDB, while the kernel reports real 64-bit addresses. Notice also that the $pc value is a 32-bit value. It appears to be within one of the mappings reported by GDB, but is outside any of the mappings reported from the kernel. And this is where the problem arises. When gdbserver detaches from the inferior we pass the inferior the address from which it should resume. Due to the 32/64 bit confusion we tell the inferior to resume from the 32-bit $pc value, which is not within any valid mapping, and so, as soon as the inferior resumes, it segfaults. If we look at how GDB (not gdbserver) figures out its target description then we see an interesting difference. GDB doesn't try to read the executable. Instead GDB uses ptrace to query the thread's state, and uses this to figure out the if the thread is 32 or 64 bit. If we update gdbserver to do it the "GDB" way then the above problem is resolved, gdbserver now sees the process as 64-bit, and when we detach from the inferior we give it the correct 64-bit address, and the inferior no longer segfaults. Now, I could just update the gdbserver code, but better, I think, to share one copy of the code between GDB and gdbserver in gdb/nat/. That is what this commit does. The cores of x86_linux_read_description from gdbserver and x86_linux_nat_target::read_description from GDB are moved into a new file gdb/nat/x86-linux-tdesc.c and combined into a single function x86_linux_tdesc_for_tid which is called from each location. This new function does things 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. Approved-By: John Baldwin --- 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 | 124 +++++++++++++++++++++++++++++ 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 | 148 +++++++++++------------------------ gdbserver/linux-x86-tdesc.cc | 1 + gdbserver/linux-x86-tdesc.h | 7 -- 15 files changed, 267 insertions(+), 199 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 331620375ae..38f4d5fde98 100644 --- a/gdb/Makefile.in +++ b/gdb/Makefile.in @@ -1585,6 +1585,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..be7014d2b5f --- /dev/null +++ b/gdb/nat/x86-linux-tdesc.c @@ -0,0 +1,124 @@ +/* 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))]; + +#ifdef __x86_64__ + /* No MPX on x32. */ + if (is_64bit && is_x32) + *xcr0_storage &= ~X86_XSTATE_MPX; +#endif /* __x86_64__ */ + + 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 30d876efc5d..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); - - is_elf64 = linux_pid_exe_is_elf_64_file (tid, &machine); + int tid = lwpid_of (current_thread); - 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,103 +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))]; - - /* No MPX on x32. */ - if (machine == EM_X86_64 && !is_elf64) - xcr0 &= ~X86_XSTATE_MPX; - - xsave_len = x86_xsave_length (); - - /* Use PTRACE_GETREGSET if it is available. */ - for (regset = x86_regsets; - regset->fill_function != NULL; regset++) - if (regset->get_request == PTRACE_GETREGSET) - regset->size = xsave_len; - else if (regset->type != GENERAL_REGS) - regset->size = 0; - } - } - - /* Check the native XCR0 only if PTRACE_GETREGSET is available. */ - xcr0_features = (have_ptrace_getregset == TRIBOOL_TRUE - && (xcr0 & X86_XSTATE_ALL_MASK)); - - if (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 Sat Mar 23 16:35:24 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Burgess X-Patchwork-Id: 87574 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 10DF43858C33 for ; Sat, 23 Mar 2024 16:37:59 +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 E22613858410 for ; Sat, 23 Mar 2024 16:35:45 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org E22613858410 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 E22613858410 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=1711211747; cv=none; b=LlPttyJ7OIVWa0WmK2WP1axE1tKvfJO9P5v2TufcsmR+vDRT7IZ4xiCsPeT1X4/CRIlpqmfSbGbwFQjKee59VoL90lEhKh9aTjO5NtD1onxeggxNOxufbE+SWs9jEbfLVcHsM259Cw9InK7tYyBJans0T5xJDKRuo/jPoITw8ng= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1711211747; c=relaxed/simple; bh=hkhkZOTjcArqDxOuzOV8/svEVOOdE6+01lFVYNJAGVE=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=rHSQVbETV5Ld4/tw5eMbfYcPgK/4qtJH6XMAAVKXCe1TwtRi+OhN+BK4vMcs3aGgQfCCNtjASrtvfVYWvpXrTgPxQzt1qHFVPrKBTcfpE8ZYsYDox6wMCUk4JrrGvx92jD7tpfSuZ/mWTcbAlTvjykhU0CpG0yNevTwPmiwTXfY= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1711211745; 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=Zo9JmJG5OR/Gi7rzdp87plDihzQNc6+KzAu3HxcYc0s=; b=gqYg+7tIF8GQ2L9w6Vnk17ID++78UuJ271QqiAUtyBZC/R84GL6311Au/Z2EiJeO/ocSJn /hrmOBYKPqHmW+xfv0WruR1nnnGS1VJl3EwFNPIRR2w4rQTaEPRSL0fQL1vhidZNtuktqe mtSLfkJFdq64Iy49pyt+vwF2GQ/zqaA= Received: from mail-ed1-f71.google.com (mail-ed1-f71.google.com [209.85.208.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-106-kftQzEKhOQC9C8mxzJcxTA-1; Sat, 23 Mar 2024 12:35:44 -0400 X-MC-Unique: kftQzEKhOQC9C8mxzJcxTA-1 Received: by mail-ed1-f71.google.com with SMTP id 4fb4d7f45d1cf-5689f1323f5so1036179a12.2 for ; Sat, 23 Mar 2024 09:35:43 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711211742; x=1711816542; 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=Zo9JmJG5OR/Gi7rzdp87plDihzQNc6+KzAu3HxcYc0s=; b=JiyMpXWyYtZUPca52J7Wkb0Jykgm0cqNvMAjBs6vl4AT+rgKVpkYSpT1iXNSln8xEk q4dqMFLp+JgnOXJKqViQv7ZGIYxBt8tq/EzdpGGJ80QrkeYhPURuy/xaDfFs04kwzgTs uGrFmELiiDcW0jAQ3BFDG1pmXy911jPIiY/En9MuAcTg33eBvsdbc0YM5mMQC7FDDh+S aY+JxJhDsGnPAN88VtLgHfjvZhazgm/K2dVejj4ufV+RkReblX+3GHnkG/at4mBi9d6e An/72hCmwg7ntNvAaPO1o8tLNf9WbGWogj1WS+WkLqmWEMEylexhJfcx+8extiZDpbN2 mUGA== X-Gm-Message-State: AOJu0Yxl0WnercGAEKxO+hIgyrnnStnUkzR1Rsi/+jOV3PcUS3qIYfE5 eTGRpSl7+OMMNZ4Z4o1A+d3z/7dLLwmdk57ChPbfH6zmCSg1PheEpjQQaMIz7qe2jBRE2y1TS3J 46TTN5f02gnFobRtAhGGRkY4o+sDokTYNc9KI0ottWLk89qDGGIqFR8I8L5Q6UMPbFDWFqM2w7R GpfpdvUJt31VO9fvvOA3FvhWoTESFO9O4DKlplwOcJHJo= X-Received: by 2002:a50:d593:0:b0:566:ecce:9d3c with SMTP id v19-20020a50d593000000b00566ecce9d3cmr2043414edi.26.1711211742187; Sat, 23 Mar 2024 09:35:42 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGXq7G+pzCC4/RoAIRhkC7wDwWwCO3Xlke0UjQPxxvKiYkN4+j067flq0gOXfy1kT8jnyKDJw== X-Received: by 2002:a50:d593:0:b0:566:ecce:9d3c with SMTP id v19-20020a50d593000000b00566ecce9d3cmr2043406edi.26.1711211741852; Sat, 23 Mar 2024 09:35:41 -0700 (PDT) Received: from localhost (185.223.159.143.dyn.plus.net. [143.159.223.185]) by smtp.gmail.com with ESMTPSA id u8-20020aa7d888000000b0056bbf8898e7sm1059881edq.54.2024.03.23.09.35.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 23 Mar 2024 09:35:40 -0700 (PDT) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess , hjl.tools@gmail.com Subject: [PATCHv3 6/8] gdb/arch: assert that X86_XSTATE_MPX is not set for x32 Date: Sat, 23 Mar 2024 16:35:24 +0000 Message-Id: <159cadba5ba824579cbc6426cb26f45228eda0a7.1711211528.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.8 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_NUMSUBJECT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org While trying to merge this commit: commit 4bb20a6244b7091a9a7a2ae35dfbd7e8db27550a Date: Wed Mar 20 04:13:18 2024 -0700 gdbserver: Clear X86_XSTATE_MPX bits in xcr0 on x32 With this patch series of mine: https://inbox.sourceware.org/gdb-patches/cover.1706801009.git.aburgess@redhat.com I worried that there could be other paths that could result in an xcr0 value that has X86_XSTATE_MPX set in x32 mode. As everyone eventually calls amd64_create_target_description to build their target description, I figured we could assert in here that if X86_XSTATE_MPX is set then we should not be an x32 target, this should uncover any other bugs in this area. I'm not currently able to build/run any x32 binaries, so I have no way to test this. Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=31511 --- gdb/arch/amd64.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/gdb/arch/amd64.c b/gdb/arch/amd64.c index b8ff4270079..f182e686dda 100644 --- a/gdb/arch/amd64.c +++ b/gdb/arch/amd64.c @@ -66,8 +66,12 @@ amd64_create_target_description (uint64_t xcr0, bool is_x32, bool is_linux, if (xcr0 & X86_XSTATE_AVX) regnum = create_feature_i386_64bit_avx (tdesc.get (), regnum); - if ((xcr0 & X86_XSTATE_MPX) && !is_x32) - regnum = create_feature_i386_64bit_mpx (tdesc.get (), regnum); + if (xcr0 & X86_XSTATE_MPX) + { + /* MPX is not available on x32. */ + gdb_assert (!is_x32); + regnum = create_feature_i386_64bit_mpx (tdesc.get (), regnum); + } if (xcr0 & X86_XSTATE_AVX512) regnum = create_feature_i386_64bit_avx512 (tdesc.get (), regnum); From patchwork Sat Mar 23 16:35:25 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Burgess X-Patchwork-Id: 87572 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 701D4385843A for ; Sat, 23 Mar 2024 16:37:12 +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 8EC183858D33 for ; Sat, 23 Mar 2024 16:35:48 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 8EC183858D33 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 8EC183858D33 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=1711211752; cv=none; b=WS3n3F9UvZyAvE2I2jMYl+AB76GEhsxD62sUQOogx7mj8ZI6NW+XVH56VC1V12pIvtl5Dt4Wh6Em+GDdnKZSU4GDeWE0De8H2akzdY7dpjShp74gKnGPc4zwXkgH5VoAZGiWxS/PFtu/QH1FLMZG7pwgLRGBez3Xr6r9OIBI/4E= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1711211752; c=relaxed/simple; bh=vC7jSnnS/ykZ/taX4m7b9k9WbE6efooBsVgNooRZd9Q=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=drhN/wxhWzj1vCGmwcJ87oUkPE1ZGdCJFbglC1mUaFKWv2UifFertccC+iDXIDX3YZD4apW81B2+tMUooS5SOqKuinn7PJin9Za0rXWa30bm9ueHJkUsTEC+UTt8vf5hogsyAZZ9mGSGkus5S2REAYZAIKHKyf+/b5FgA60O1zM= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1711211748; 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=aVPY3EsFoy4KrVZBBnyZHWMO7aPGXIoc1Vc09RMN9IA=; b=Bvi0Kr8ab4Mh6xpQKODny0k700TsG9WVpKYQqTKkokrOgrcvwmffPHpgoNCE1karWcCENf ym22TKtUFTZtUV6RWoNpoSFOtwHKvspQeqGsEz7YPiIV61uEiYwyzOZUb55mwWvpPRqxgm JmVvamh5oDxLbgFFGBwN8pP4CmPKUQA= 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-618-ezqVHufBNo6zUi-1mGQKQg-1; Sat, 23 Mar 2024 12:35:47 -0400 X-MC-Unique: ezqVHufBNo6zUi-1mGQKQg-1 Received: by mail-lf1-f70.google.com with SMTP id 2adb3069b0e04-513d125058eso2794607e87.2 for ; Sat, 23 Mar 2024 09:35:46 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711211744; x=1711816544; 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=aVPY3EsFoy4KrVZBBnyZHWMO7aPGXIoc1Vc09RMN9IA=; b=KfhUIOKzuERw3lIKcyU5uyCvPo2yApwsH9Nh+9RqFvWyY2ChZXL9RL7PzbLQj1mPGE KI7xd7fzfvWjNuLRzNqlnGtndzmVgpOdV50/27dUPOfxT4w9AppOLrqfdrNramnJor7e uSOFD2lXpD5yizCXjvfkaz0CuSz6qAODskwJz5SF/NtAVMuMm5EM0UGlQQHXTdgc1x3O lyBiDVWH7iyg53t4by/uJTnFSqQRjTAG/o8po5v4W+06rdPIK1U6mN66ecukI/vxgFf9 TYXS5Yl46MUVBUnq+o/88I0V5IpjH0diljfuFioNa6YI+PPa04qRu8uWpOlzNsjnhL6Z e8Sg== X-Gm-Message-State: AOJu0YzT9q5An6lyEhHRpIwoqvCZ3FpkoI5TqQKI6BLtUmGMsaQTA9Tx 0J9C/l2CaPAPR/fwc+fvpmIh79vzr2PYm2OECvhEc3QKLZy/57nB25tlnZ9SW+h5ADWJDiQZvBc PM34qgfYvKm2J1QPvc95WYlaCYmX04dJtNJJuRxxR5R/LzmVxrM7yZDfD71zxCLF7MtQImsMTM8 DvkW905Vey47H5hN9xBQA+oYYNPDRFkbMIWjX5MVCph/s= X-Received: by 2002:a19:5e55:0:b0:513:a766:4672 with SMTP id z21-20020a195e55000000b00513a7664672mr2285378lfi.4.1711211744192; Sat, 23 Mar 2024 09:35:44 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGlIkkSwTAER80vjFfzCv2qXraNpjE51sish43DkSDKY1s1OzBqnQkawCMMqKeMIF3zIvzwYQ== X-Received: by 2002:a19:5e55:0:b0:513:a766:4672 with SMTP id z21-20020a195e55000000b00513a7664672mr2285353lfi.4.1711211743546; Sat, 23 Mar 2024 09:35:43 -0700 (PDT) Received: from localhost (185.223.159.143.dyn.plus.net. [143.159.223.185]) by smtp.gmail.com with ESMTPSA id kq1-20020a170906abc100b00a46a04d7dc4sm1066626ejb.61.2024.03.23.09.35.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 23 Mar 2024 09:35:42 -0700 (PDT) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess , hjl.tools@gmail.com, John Baldwin Subject: [PATCHv3 7/8] gdbserver: update target description creation for x86/linux Date: Sat, 23 Mar 2024 16:35:25 +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.1 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org This commit is part of a series which aims to share more of the target description creation between GDB and gdbserver for x86/Linux. After some refactoring, 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. Approved-By: John Baldwin --- 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 Sat Mar 23 16:35:26 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Burgess X-Patchwork-Id: 87575 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 5799D3858403 for ; Sat, 23 Mar 2024 16:38:23 +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 038C43858D39 for ; Sat, 23 Mar 2024 16:35:50 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 038C43858D39 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 038C43858D39 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=1711211755; cv=none; b=ep2Hcz0oxpXNEvPUm61DL0PMffXAaLavIstOhMA3KK7gT0iW+ODWGr5csyoeNZyGTa5oY3/tGn7SZ492JVv9fIC7pDjVfMcXC9yybA+/lzOIWQ8xjXGeZfChYUWZn7UMma1TfB3fmUjcTdBpul3VLvKtvEMvhrX4OMu+2TUxjdE= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1711211755; c=relaxed/simple; bh=QMspMluNdXjqpLlPtca7e2W+x+9Uud4CnKw0VoOwA6k=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=C1Nwb2KQHqKMbZAPSTYpe/gT3FyOmQIxfQndQUG5m4xakkeW/2LR3v0qgs9M6HNcg9poEfEpwKgupdbeCC27FgaAUrXvT7C1DQ7cJj7xvsPzN8THcI3oZWnc+TDKrDcKiKIue5VMgrE2phSbUwLvLOzS/B+V4g+vyCh0s6W8Ccg= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1711211750; 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=zz0S66kzKs5j33k3P9qCXyUJs7wJ68mlLlg+MyA8UVY=; b=dHB2rE7VbK8RMjhMzIJr9d5lmsrl3Zj7ew0DvlqyLPuUg+HRlevUIDwu60Ytn3lAnnqOXQ dMZ0qkfM4hAaFHclGjustmWR0EGxO/qcFsfmcIeCBiZ7xYesHGpKnkM/eia1O37lNlJr+s xInW4bkPEKbBQFXXvZ/clG/WwoT+EKw= Received: from mail-lj1-f197.google.com (mail-lj1-f197.google.com [209.85.208.197]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-255-eS0XCMt1M8CvkaOgm2SZBg-1; Sat, 23 Mar 2024 12:35:48 -0400 X-MC-Unique: eS0XCMt1M8CvkaOgm2SZBg-1 Received: by mail-lj1-f197.google.com with SMTP id 38308e7fff4ca-2d6c82d12b5so3660671fa.1 for ; Sat, 23 Mar 2024 09:35:48 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711211746; x=1711816546; 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=zz0S66kzKs5j33k3P9qCXyUJs7wJ68mlLlg+MyA8UVY=; b=tWef2qBTvaNwbbAHbt/zzlMmlgllDUgR34Emp3kyz5P28SQI/nwMGg+gnv4+hv6iHW 4ZPQdtELgWDsjZ5xi3bYnepwvwGPj2uevOiNbyjMTeH3TFVH6Pw+CpW+sSeOqN9GpO/s xH7pMSYbPfm0ikUcT6w/7zuifgNM1dKC1JVZYZsQGV0xD1hLSwoepxC6xAb1petkN+mk +tfGBrgpSU+hdDbNLxDtPqW8yGdE0Z7C7mf9GMPAVGCdPbAMcrCQEjb+a/cJtnPPQ9ja GyYnPGuy/3xOwEx2oRmat1ETF31u9h2WjK3MGc70BCaWBnVPzFmZhj9qVI6hxp4/GPtv ZLqg== X-Gm-Message-State: AOJu0YzywxZkHccO8sgOtW2SPu+2wkUwW0jEnrY49bIrMeLbJuecTq8g 50WH76jlxdGhkmUWPQdduEqpSe9pPJ5O1FbnvV9H40rp1M5sCWvfakv9Z/WGNTGwp4ihPvbhzfm grSL370xbb79raHUypnjslTzSef9u/Wkk5sC6xzIGJCHz56NfLXk/y3oCp4Fbcge71aOGLT4wA6 rH0XJw1Mh9v4l5BRI3UEAd+d6B8OycUXgaeTyEuOBEK4Q= X-Received: by 2002:a19:c216:0:b0:513:c5a1:a8f6 with SMTP id l22-20020a19c216000000b00513c5a1a8f6mr1583119lfc.17.1711211745702; Sat, 23 Mar 2024 09:35:45 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGuPZt7cP38lT0DQnvZNOdQUTJZ1ZgIwbDIPZz195QVf3CKDjjZik5vkff5nC77HBDDS7ZhVA== X-Received: by 2002:a19:c216:0:b0:513:c5a1:a8f6 with SMTP id l22-20020a19c216000000b00513c5a1a8f6mr1583111lfc.17.1711211744900; Sat, 23 Mar 2024 09:35:44 -0700 (PDT) Received: from localhost (185.223.159.143.dyn.plus.net. [143.159.223.185]) by smtp.gmail.com with ESMTPSA id p2-20020a1709060e8200b00a46cddf1efbsm1075234ejf.29.2024.03.23.09.35.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 23 Mar 2024 09:35:44 -0700 (PDT) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess , hjl.tools@gmail.com, John Baldwin Subject: [PATCHv3 8/8] gdb/gdbserver: share x86/linux tdesc caching Date: Sat, 23 Mar 2024 16:35:26 +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.1 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org This commit builds on the previous series of commits to share the target description caching code between GDB and gdbserver for x86/Linux targets. The objective of this commit is to move the four functions (2 each of) i386_linux_read_description and amd64_linux_read_description into 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. Approved-By: John Baldwin --- 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 be7014d2b5f..c438dfae84f 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 * @@ -122,3 +124,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 c7120895a26..bd6f68e7f2c 100644 --- a/gdbserver/Makefile.in +++ b/gdbserver/Makefile.in @@ -519,6 +519,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 */