From patchwork Thu Apr 27 21:01:03 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Baldwin X-Patchwork-Id: 68409 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 066653870892 for ; Thu, 27 Apr 2023 21:02:18 +0000 (GMT) X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from mail.baldwin.cx (bigwig.baldwin.cx [IPv6:2607:f138:0:13::2]) by sourceware.org (Postfix) with ESMTPS id 3CF443858C27 for ; Thu, 27 Apr 2023 21:01:34 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 3CF443858C27 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=FreeBSD.org Authentication-Results: sourceware.org; spf=fail smtp.mailfrom=FreeBSD.org Received: from ralph.baldwin.net (c-98-35-126-114.hsd1.ca.comcast.net [98.35.126.114]) by mail.baldwin.cx (Postfix) with ESMTPSA id 931631A84E2F for ; Thu, 27 Apr 2023 17:01:33 -0400 (EDT) From: John Baldwin To: gdb-patches@sourceware.org Subject: [PATCH v5 09/19] gdb: Update x86 FreeBSD architectures to support XSAVE layouts. Date: Thu, 27 Apr 2023 14:01:03 -0700 Message-Id: <20230427210113.45380-10-jhb@FreeBSD.org> X-Mailer: git-send-email 2.40.0 In-Reply-To: <20230427210113.45380-1-jhb@FreeBSD.org> References: <20230427210113.45380-1-jhb@FreeBSD.org> MIME-Version: 1.0 X-Greylist: Sender succeeded SMTP AUTH, not delayed by milter-greylist-4.6.4 (mail.baldwin.cx [0.0.0.0]); Thu, 27 Apr 2023 17:01:33 -0400 (EDT) X-Virus-Scanned: clamav-milter 0.103.1 at mail.baldwin.cx X-Virus-Status: Clean X-Spam-Status: No, score=-11.9 required=5.0 tests=BAYES_00, FORGED_SPF_HELO, GIT_PATCH_0, KAM_DMARC_STATUS, KAM_SHORT, KHOP_HELO_FCRDNS, SPF_HELO_PASS, SPF_SOFTFAIL, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 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 Sender: "Gdb-patches" Refactor i386fbsd_core_read_xcr0 to fetch and return a corresponding x86_xsave_layout as well as xcr0 using the size of an existing NT_X86_XSTATE core dump to determine the offsets via i387_set_xsave_layout. Use this to add an implementation of gdbarch_core_xfer_x86_xsave_layout. Use tdep->xsave_layout.sizeof_xsave as the size of the XSTATE register set and only fetch/store the register set if this size is non-zero. --- gdb/amd64-fbsd-tdep.c | 12 ++++++-- gdb/i386-fbsd-tdep.c | 72 ++++++++++++++++++++++++------------------- gdb/i386-fbsd-tdep.h | 10 ++++-- 3 files changed, 58 insertions(+), 36 deletions(-) diff --git a/gdb/amd64-fbsd-tdep.c b/gdb/amd64-fbsd-tdep.c index 0d5ce004fb1..5dc519d7d6e 100644 --- a/gdb/amd64-fbsd-tdep.c +++ b/gdb/amd64-fbsd-tdep.c @@ -224,7 +224,9 @@ amd64fbsd_core_read_description (struct gdbarch *gdbarch, struct target_ops *target, bfd *abfd) { - return amd64_target_description (i386fbsd_core_read_xcr0 (abfd), true); + x86_xsave_layout layout; + return amd64_target_description (i386_fbsd_core_read_xsave_info (abfd, layout), + true); } /* Similar to amd64_supply_fpregset, but use XSAVE extended state. */ @@ -271,8 +273,10 @@ amd64fbsd_iterate_over_regset_sections (struct gdbarch *gdbarch, cb (".reg-x86-segbases", AMD64_FBSD_SIZEOF_SEGBASES_REGSET, AMD64_FBSD_SIZEOF_SEGBASES_REGSET, &amd64_fbsd_segbases_regset, "segment bases", cb_data); - cb (".reg-xstate", X86_XSTATE_SIZE (tdep->xcr0), X86_XSTATE_SIZE (tdep->xcr0), - &amd64fbsd_xstateregset, "XSAVE extended state", cb_data); + if (tdep->xsave_layout.sizeof_xsave != 0) + cb (".reg-xstate", tdep->xsave_layout.sizeof_xsave, + tdep->xsave_layout.sizeof_xsave, &amd64fbsd_xstateregset, + "XSAVE extended state", cb_data); } /* Implement the get_thread_local_address gdbarch method. */ @@ -313,6 +317,8 @@ amd64fbsd_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch) tramp_frame_prepend_unwinder (gdbarch, &amd64_fbsd_sigframe); tdep->xsave_xcr0_offset = I386_FBSD_XSAVE_XCR0_OFFSET; + set_gdbarch_core_read_x86_xsave_layout + (gdbarch, i386_fbsd_core_read_x86_xsave_layout); /* Iterate over core file register note sections. */ set_gdbarch_iterate_over_regset_sections diff --git a/gdb/i386-fbsd-tdep.c b/gdb/i386-fbsd-tdep.c index fb9030413ab..df842f16bfc 100644 --- a/gdb/i386-fbsd-tdep.c +++ b/gdb/i386-fbsd-tdep.c @@ -18,13 +18,13 @@ along with this program. If not, see . */ #include "defs.h" +#include "gdbcore.h" #include "osabi.h" #include "regcache.h" #include "regset.h" #include "trad-frame.h" #include "tramp-frame.h" #include "i386-fbsd-tdep.h" -#include "gdbsupport/x86-xstate.h" #include "i386-tdep.h" #include "i387-tdep.h" @@ -241,43 +241,49 @@ static const struct tramp_frame i386_fbsd64_sigframe = i386_fbsd_sigframe_init }; -/* Get XSAVE extended state xcr0 from core dump. */ +/* See i386-fbsd-tdep.h. */ uint64_t -i386fbsd_core_read_xcr0 (bfd *abfd) +i386_fbsd_core_read_xsave_info (bfd *abfd, x86_xsave_layout &layout) { asection *xstate = bfd_get_section_by_name (abfd, ".reg-xstate"); - uint64_t xcr0; + if (xstate == nullptr) + return X86_XSTATE_SSE_MASK; - if (xstate) + /* Check extended state size. */ + size_t size = bfd_section_size (xstate); + if (size < X86_XSTATE_AVX_SIZE) + return X86_XSTATE_SSE_MASK; + + char contents[8]; + if (! bfd_get_section_contents (abfd, xstate, contents, + I386_FBSD_XSAVE_XCR0_OFFSET, 8)) { - size_t size = bfd_section_size (xstate); - - /* Check extended state size. */ - if (size < X86_XSTATE_AVX_SIZE) - xcr0 = X86_XSTATE_SSE_MASK; - else - { - char contents[8]; - - if (! bfd_get_section_contents (abfd, xstate, contents, - I386_FBSD_XSAVE_XCR0_OFFSET, - 8)) - { - warning (_("Couldn't read `xcr0' bytes from " - "`.reg-xstate' section in core file.")); - return X86_XSTATE_SSE_MASK; - } - - xcr0 = bfd_get_64 (abfd, contents); - } + warning (_("Couldn't read `xcr0' bytes from " + "`.reg-xstate' section in core file.")); + return X86_XSTATE_SSE_MASK; } - else - xcr0 = X86_XSTATE_SSE_MASK; + + uint64_t xcr0 = bfd_get_64 (abfd, contents); + + if (!i387_set_xsave_layout (xcr0, size, layout)) + return X86_XSTATE_SSE_MASK; return xcr0; } +/* Implement the core_read_x86_xsave_layout gdbarch method. */ + +bool +i386_fbsd_core_read_x86_xsave_layout (struct gdbarch *gdbarch, + x86_xsave_layout &layout) +{ + if (i386_fbsd_core_read_xsave_info (core_bfd, layout) == X86_XSTATE_SSE_MASK) + return false; + + return true; +} + /* Implement the core_read_description gdbarch method. */ static const struct target_desc * @@ -285,7 +291,9 @@ i386fbsd_core_read_description (struct gdbarch *gdbarch, struct target_ops *target, bfd *abfd) { - return i386_target_description (i386fbsd_core_read_xcr0 (abfd), true); + x86_xsave_layout layout; + return i386_target_description (i386_fbsd_core_read_xsave_info (abfd, layout), + true); } /* Similar to i386_supply_fpregset, but use XSAVE extended state. */ @@ -335,9 +343,9 @@ i386fbsd_iterate_over_regset_sections (struct gdbarch *gdbarch, I386_FBSD_SIZEOF_SEGBASES_REGSET, &i386_fbsd_segbases_regset, "segment bases", cb_data); - if (tdep->xcr0 & X86_XSTATE_AVX) - cb (".reg-xstate", X86_XSTATE_SIZE (tdep->xcr0), - X86_XSTATE_SIZE (tdep->xcr0), &i386fbsd_xstateregset, + if (tdep->xsave_layout.sizeof_xsave != 0) + cb (".reg-xstate", tdep->xsave_layout.sizeof_xsave, + tdep->xsave_layout.sizeof_xsave, &i386fbsd_xstateregset, "XSAVE extended state", cb_data); } @@ -386,6 +394,8 @@ i386fbsd_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch) i386_elf_init_abi (info, gdbarch); tdep->xsave_xcr0_offset = I386_FBSD_XSAVE_XCR0_OFFSET; + set_gdbarch_core_read_x86_xsave_layout + (gdbarch, i386_fbsd_core_read_x86_xsave_layout); /* Iterate over core file register note sections. */ set_gdbarch_iterate_over_regset_sections diff --git a/gdb/i386-fbsd-tdep.h b/gdb/i386-fbsd-tdep.h index cb991af9e49..f96c00d45eb 100644 --- a/gdb/i386-fbsd-tdep.h +++ b/gdb/i386-fbsd-tdep.h @@ -20,10 +20,16 @@ #ifndef I386_FBSD_TDEP_H #define I386_FBSD_TDEP_H +#include "gdbsupport/x86-xstate.h" #include "regset.h" -/* Get XSAVE extended state xcr0 from core dump. */ -extern uint64_t i386fbsd_core_read_xcr0 (bfd *abfd); +/* Validate and fetch XSAVE extended state xcr0 and extended area + layout from core dump. */ +uint64_t i386_fbsd_core_read_xsave_info (bfd *abfd, x86_xsave_layout &layout); + +/* Implement the core_read_x86_xsave_layout gdbarch method. */ +bool i386_fbsd_core_read_x86_xsave_layout (struct gdbarch *gdbarch, + x86_xsave_layout &layout); /* The format of the XSAVE extended area is determined by hardware. Cores store the XSAVE extended area in a NT_X86_XSTATE note that