From patchwork Mon Oct 9 18:36:02 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Baldwin X-Patchwork-Id: 56217 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 1A83038618A6 for ; Mon, 9 Oct 2023 18:37:14 +0000 (GMT) X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from mail.baldwin.cx (bigwig.baldwin.cx [66.216.25.90]) by sourceware.org (Postfix) with ESMTPS id 18EBA3858C5E for ; Mon, 9 Oct 2023 18:36:42 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 18EBA3858C5E 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 (unknown [98.47.15.113]) by mail.baldwin.cx (Postfix) with ESMTPSA id 8C5AC1A84BA9; Mon, 9 Oct 2023 14:36:38 -0400 (EDT) From: John Baldwin To: gdb-patches@sourceware.org Cc: Willgerodt@sourceware.org, Felix , George@sourceware.org, Jini Susan , Simon Marchi Subject: [RFC 00/13] Proposal for a new NT_X86_CPUID core dump note Date: Mon, 9 Oct 2023 11:36:02 -0700 Message-ID: <20231009183617.24862-1-jhb@FreeBSD.org> X-Mailer: git-send-email 2.41.0 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]); Mon, 09 Oct 2023 14:36:39 -0400 (EDT) X-Virus-Scanned: clamav-milter 0.103.1 at mail.baldwin.cx X-Virus-Status: Clean X-Spam-Status: No, score=-1.4 required=5.0 tests=BAYES_00, FORGED_SPF_HELO, KAM_DMARC_STATUS, KHOP_HELO_FCRDNS, SPF_HELO_PASS, SPF_SOFTFAIL, TXREP autolearn=no 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 One of the shortcomings of the previous XSAVE patch series is that it depends on heuristics based on the total XSAVE register set size and XCR0 mask to infer layouts of the various register blocks for core dumps. This series introduces a new x86-specific core dump note intended to supplant these heuristics by storing the raw CPUID leaves describing the XSAVE layout in core dumps. This series proposes a new core dump note, NT_X86_CPUID (0x205), which contains an array of structures. Each structure describes an invidual CPUID sub-leaf containing both the inputs to CPUID (%eax and %ecx) and the outputs (%eax, %ebx, %ecx, and %edx) in a format roughly matching the follow C structure: struct cpuid_leaf { uint32_t leaf; uint32_t subleaf; uint32_t eax; uint32_t ebx; uint32_t ecx; uint32_t edx; }; This format is not XSAVE-specific and implementations could choose to add additional CPUID leaves to this structure if needed in the future. Consumers of this note should lookup the value of required leaves and ignore any unneeded leaves. An alternate approach might be to write out a more XSAVE-specific note that is an array containing the offset and size of each XSAVE region. Note that either approach would enable storing XSAVE notes in the "compact" format at some point in the future. This series adds support for reading/writing the note to binutils as well as suport for parsing and generating the note in GDB. It also hooks this into both the FreeBSD and Linux x86 architectures in GDB to read the XSAVE layout from this note when present, and to write out a note when generating a core via `gcore'. I've done some limited testing on FreeBSD/amd64 and Linux/x86-64, but it could probably use some more testing on Linux in particular. (I know Simon has an AMD machine with a layout not handled by the current heuristics for example.) For the gcore side, a new TARGET_OBJECT_X86_CPUID is used to fetch the current note contents from a native target. There is still one gap even with this patch series which is that if you are connected to a remote target (e.g. gdbserver), we currently do not have a known XSAVE layout to use when writing out a core via `gcore'. One option that would close this gap would be to extend the remote protocol to permit reading this new object from a debug server. The remote target could then implement fetching this object and also make use of this object to implement the target::fetch_x86_xsave_layout method which would close that gap. Another possibility would be to just pick a "known" XSAVE format that matches one of the heuristics. The series is available from git@github.com:bsdjhb/gdb.git on the `nt_x86_cpuid' branch. I also have an implementation of this core dump note available for FreeBSD's kernel, though I won't merge it until we've collectively settled on the format: https://reviews.freebsd.org/D42136 Things I have not done and could use help with: - Implementation for the Linux kernel - Coordination with folks from LLDB John Baldwin (13): binutils: Support for the NT_X86_CPUID core dump note i387-tdep: Add function to read XSAVE layout from NT_X86_CPUID gdb: Use NT_X86_CPUID in x86 FreeBSD architectures to read XSAVE layouts gdb: Use NT_X86_CPUID in x86 FreeBSD architectures to read XSAVE layouts nat/x86-cpuid.h: Remove non-x86 fallbacks nat/x86-cpuid: Add a function to build the contents of a NT_X86_CPUID note x86_elf_make_cpuid_note: Helper routine to build NT_X86_CPUID ELF note x86-fbsd-nat: Support fetching TARGET_OBJECT_X86_CPUID objects fbsd-tdep: Export fbsd_make_corefile_notes {amd64,i386}-fbsd-tdep: Include NT_X86_CPUID notes in core dumps from gcore x86-linux-nat: Support fetching TARGET_OBJECT_X86_CPUID objects linux-tdep: Export linux_make_corefile_notes {amd64,i386}-linux-tdep: Include NT_X86_CPUID notes in core dumps from gcore bfd/elf-bfd.h | 2 + bfd/elf.c | 35 +++++++++++ binutils/readelf.c | 2 + gdb/amd64-fbsd-tdep.c | 1 + gdb/amd64-linux-tdep.c | 1 + gdb/configure.nat | 13 ++-- gdb/fbsd-tdep.c | 5 +- gdb/fbsd-tdep.h | 7 +++ gdb/i386-fbsd-tdep.c | 18 +++++- gdb/i386-fbsd-tdep.h | 7 +++ gdb/i386-linux-tdep.c | 18 +++++- gdb/i386-linux-tdep.h | 7 +++ gdb/i387-tdep.c | 132 +++++++++++++++++++++++++++++++++++++++++ gdb/i387-tdep.h | 8 +++ gdb/linux-tdep.c | 5 +- gdb/linux-tdep.h | 7 +++ gdb/nat/x86-cpuid.c | 91 ++++++++++++++++++++++++++++ gdb/nat/x86-cpuid.h | 29 +++------ gdb/target.h | 2 + gdb/x86-fbsd-nat.c | 37 ++++++++++++ gdb/x86-fbsd-nat.h | 9 +++ gdb/x86-linux-nat.c | 37 ++++++++++++ gdb/x86-linux-nat.h | 9 +++ gdb/x86-tdep.c | 22 +++++++ gdb/x86-tdep.h | 9 +++ include/elf/common.h | 2 + 26 files changed, 480 insertions(+), 35 deletions(-) create mode 100644 gdb/nat/x86-cpuid.c