From patchwork Mon Oct 9 18:36:08 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Baldwin X-Patchwork-Id: 77305 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 14C0538560AA for ; Mon, 9 Oct 2023 18:37:28 +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 7FDA83858414 for ; Mon, 9 Oct 2023 18:36:45 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 7FDA83858414 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 6CDBF1A84E2D; Mon, 9 Oct 2023 14:36:44 -0400 (EDT) From: John Baldwin To: gdb-patches@sourceware.org Cc: Willgerodt@sourceware.org, Felix , George@sourceware.org, Jini Susan , Simon Marchi Subject: [RFC 06/13] nat/x86-cpuid: Add a function to build the contents of a NT_X86_CPUID note Date: Mon, 9 Oct 2023 11:36:08 -0700 Message-ID: <20231009183617.24862-7-jhb@FreeBSD.org> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20231009183617.24862-1-jhb@FreeBSD.org> References: <20231009183617.24862-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]); Mon, 09 Oct 2023 14:36:45 -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 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 --- gdb/nat/x86-cpuid.c | 91 +++++++++++++++++++++++++++++++++++++++++++++ gdb/nat/x86-cpuid.h | 7 ++++ 2 files changed, 98 insertions(+) create mode 100644 gdb/nat/x86-cpuid.c diff --git a/gdb/nat/x86-cpuid.c b/gdb/nat/x86-cpuid.c new file mode 100644 index 00000000000..a0fc9c2b192 --- /dev/null +++ b/gdb/nat/x86-cpuid.c @@ -0,0 +1,91 @@ +/* x86 CPUID functions. + + Copyright (C) 2023 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-cpuid.h" + +/* Data stored in the note for a single CPUID leaf. */ + +struct cpuid_leaf +{ + uint32_t leaf; + uint32_t subleaf; + uint32_t eax; + uint32_t ebx; + uint32_t ecx; + uint32_t edx; +}; + +/* Append a single CPUID leaf. */ + +static void +append_cpuid_note (gdb::unique_xmalloc_ptr &buf, size_t &len, + uint32_t leaf, uint32_t subleaf) +{ + struct cpuid_leaf data; + if (!x86_cpuid_count (leaf, subleaf, &data.eax, &data.ebx, &data.ecx, + &data.edx)) + return; + + data.leaf = leaf; + data.subleaf = subleaf; + + buf.reset ((gdb_byte *) xrealloc (buf.release (), len + sizeof (data))); + memcpy (buf.get() + len, &data, sizeof (data)); + len += sizeof (data); +} + +/* See x86-cpuid.h. */ + +void +x86_cpuid_note (gdb::unique_xmalloc_ptr &buf, size_t &len) +{ + buf.reset (nullptr); + len = 0; + + /* Include 0xd sub-leaves that describe the XSAVE extended state. */ + uint32_t eax, edx; + if (x86_cpuid_count (0xd, 0, &eax, nullptr, nullptr, &edx) + && (eax != 0 || edx != 0)) + { + /* Main leaf and sub-leaf 1. */ + append_cpuid_note (buf, len, 0xd, 0); + append_cpuid_note (buf, len, 0xd, 1); + + /* Sub-leaves for each enabled feature. */ + eax >>= 2; + uint32_t i = 2; + while (eax != 0) + { + if ((eax & 1) == 1) + append_cpuid_note (buf, len, 0xd, i); + eax >>= 1; + i++; + } + + i = 0; + while (edx != 0) + { + if ((edx & 1) == 1) + append_cpuid_note (buf, len, 0xd, i + 32); + edx >>= 1; + i++; + } + } +} diff --git a/gdb/nat/x86-cpuid.h b/gdb/nat/x86-cpuid.h index 9401705c44d..6b0561dfcf4 100644 --- a/gdb/nat/x86-cpuid.h +++ b/gdb/nat/x86-cpuid.h @@ -82,4 +82,11 @@ x86_cpuid_count (unsigned int __level, unsigned int __sublevel, #undef nullptr #endif +#ifdef __cplusplus +/* Store data for the NT_X86_CPUID core dump note in BUF for the current + host. The size of the data is stored in LEN. */ + +void x86_cpuid_note (gdb::unique_xmalloc_ptr &buf, size_t &len); +#endif + #endif /* NAT_X86_CPUID_H */