Skip to content

Commit ba38677

Browse files
vigbalubp3tk0v
authored andcommitted
x86/elf: Add a new FPU buffer layout info to x86 core files
Add a new .note section containing type, size, offset and flags of every xfeature that is present. This information will be used by debuggers to understand the XSAVE layout of the machine where the core file has been dumped, and to read XSAVE registers, especially during cross-platform debugging. The XSAVE layouts of modern AMD and Intel CPUs differ, especially since Memory Protection Keys and the AVX-512 features have been inculcated into the AMD CPUs. Since AMD never adopted (and hence never left room in the XSAVE layout for) the Intel MPX feature, tools like GDB had assumed a fixed XSAVE layout matching that of Intel (based on the XCR0 mask). Hence, core dumps from AMD CPUs didn't match the known size for the XCR0 mask. This resulted in GDB and other tools not being able to access the values of the AVX-512 and PKRU registers on AMD CPUs. To solve this, an interim solution has been accepted into GDB, and is already a part of GDB 14, see https://sourceware.org/pipermail/gdb-patches/2023-March/198081.html. But it depends on heuristics based on the total XSAVE register set size and the XCR0 mask to infer the layouts of the various register blocks for core dumps, and hence, is not a foolproof mechanism to determine the layout of the XSAVE area. Therefore, add a new core dump note in order to allow GDB/LLDB and other relevant tools to determine the layout of the XSAVE area of the machine where the corefile was dumped. The new core dump note (which is being proposed as a per-process .note section), NT_X86_XSAVE_LAYOUT (0x205) contains an array of structures. Each structure describes an individual extended feature containing offset, size and flags in this format: struct x86_xfeat_component { u32 type; u32 size; u32 offset; u32 flags; }; and in an independent manner, allowing for future extensions without depending on hw arch specifics like CPUID etc. [ bp: Massage commit message, zap trailing whitespace. ] Co-developed-by: Jini Susan George <[email protected]> Signed-off-by: Jini Susan George <[email protected]> Co-developed-by: Borislav Petkov (AMD) <[email protected]> Signed-off-by: Borislav Petkov (AMD) <[email protected]> Signed-off-by: Vignesh Balasubramanian <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 8400291 commit ba38677

File tree

5 files changed

+109
-2
lines changed

5 files changed

+109
-2
lines changed

arch/x86/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ config X86
107107
select ARCH_HAS_DEBUG_WX
108108
select ARCH_HAS_ZONE_DMA_SET if EXPERT
109109
select ARCH_HAVE_NMI_SAFE_CMPXCHG
110+
select ARCH_HAVE_EXTRA_ELF_NOTES
110111
select ARCH_MHP_MEMMAP_ON_MEMORY_ENABLE
111112
select ARCH_MIGHT_HAVE_ACPI_PDC if ACPI
112113
select ARCH_MIGHT_HAVE_PC_PARPORT

arch/x86/include/uapi/asm/elf.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
2+
#ifndef _UAPI_ASM_X86_ELF_H
3+
#define _UAPI_ASM_X86_ELF_H
4+
5+
#include <linux/types.h>
6+
7+
struct x86_xfeat_component {
8+
__u32 type;
9+
__u32 size;
10+
__u32 offset;
11+
__u32 flags;
12+
} __packed;
13+
14+
_Static_assert(sizeof(struct x86_xfeat_component) % 4 == 0, "x86_xfeat_component is not aligned");
15+
16+
#endif /* _UAPI_ASM_X86_ELF_H */

arch/x86/kernel/fpu/xstate.c

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <linux/seq_file.h>
1414
#include <linux/proc_fs.h>
1515
#include <linux/vmalloc.h>
16+
#include <linux/coredump.h>
1617

1718
#include <asm/fpu/api.h>
1819
#include <asm/fpu/regset.h>
@@ -23,6 +24,8 @@
2324
#include <asm/prctl.h>
2425
#include <asm/elf.h>
2526

27+
#include <uapi/asm/elf.h>
28+
2629
#include "context.h"
2730
#include "internal.h"
2831
#include "legacy.h"
@@ -1838,3 +1841,89 @@ int proc_pid_arch_status(struct seq_file *m, struct pid_namespace *ns,
18381841
return 0;
18391842
}
18401843
#endif /* CONFIG_PROC_PID_ARCH_STATUS */
1844+
1845+
#ifdef CONFIG_COREDUMP
1846+
static const char owner_name[] = "LINUX";
1847+
1848+
/*
1849+
* Dump type, size, offset and flag values for every xfeature that is present.
1850+
*/
1851+
static int dump_xsave_layout_desc(struct coredump_params *cprm)
1852+
{
1853+
int num_records = 0;
1854+
int i;
1855+
1856+
for_each_extended_xfeature(i, fpu_user_cfg.max_features) {
1857+
struct x86_xfeat_component xc = {
1858+
.type = i,
1859+
.size = xstate_sizes[i],
1860+
.offset = xstate_offsets[i],
1861+
/* reserved for future use */
1862+
.flags = 0,
1863+
};
1864+
1865+
if (!dump_emit(cprm, &xc, sizeof(xc)))
1866+
return 0;
1867+
1868+
num_records++;
1869+
}
1870+
return num_records;
1871+
}
1872+
1873+
static u32 get_xsave_desc_size(void)
1874+
{
1875+
u32 cnt = 0;
1876+
u32 i;
1877+
1878+
for_each_extended_xfeature(i, fpu_user_cfg.max_features)
1879+
cnt++;
1880+
1881+
return cnt * (sizeof(struct x86_xfeat_component));
1882+
}
1883+
1884+
int elf_coredump_extra_notes_write(struct coredump_params *cprm)
1885+
{
1886+
int num_records = 0;
1887+
struct elf_note en;
1888+
1889+
if (!fpu_user_cfg.max_features)
1890+
return 0;
1891+
1892+
en.n_namesz = sizeof(owner_name);
1893+
en.n_descsz = get_xsave_desc_size();
1894+
en.n_type = NT_X86_XSAVE_LAYOUT;
1895+
1896+
if (!dump_emit(cprm, &en, sizeof(en)))
1897+
return 1;
1898+
if (!dump_emit(cprm, owner_name, en.n_namesz))
1899+
return 1;
1900+
if (!dump_align(cprm, 4))
1901+
return 1;
1902+
1903+
num_records = dump_xsave_layout_desc(cprm);
1904+
if (!num_records)
1905+
return 1;
1906+
1907+
/* Total size should be equal to the number of records */
1908+
if ((sizeof(struct x86_xfeat_component) * num_records) != en.n_descsz)
1909+
return 1;
1910+
1911+
return 0;
1912+
}
1913+
1914+
int elf_coredump_extra_notes_size(void)
1915+
{
1916+
int size;
1917+
1918+
if (!fpu_user_cfg.max_features)
1919+
return 0;
1920+
1921+
/* .note header */
1922+
size = sizeof(struct elf_note);
1923+
/* Name plus alignment to 4 bytes */
1924+
size += roundup(sizeof(owner_name), 4);
1925+
size += get_xsave_desc_size();
1926+
1927+
return size;
1928+
}
1929+
#endif /* CONFIG_COREDUMP */

fs/binfmt_elf.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2039,7 +2039,7 @@ static int elf_core_dump(struct coredump_params *cprm)
20392039
{
20402040
size_t sz = info.size;
20412041

2042-
/* For cell spufs */
2042+
/* For cell spufs and x86 xstate */
20432043
sz += elf_coredump_extra_notes_size();
20442044

20452045
phdr4note = kmalloc(sizeof(*phdr4note), GFP_KERNEL);
@@ -2103,7 +2103,7 @@ static int elf_core_dump(struct coredump_params *cprm)
21032103
if (!write_note_info(&info, cprm))
21042104
goto end_coredump;
21052105

2106-
/* For cell spufs */
2106+
/* For cell spufs and x86 xstate */
21072107
if (elf_coredump_extra_notes_write(cprm))
21082108
goto end_coredump;
21092109

include/uapi/linux/elf.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,7 @@ typedef struct elf64_shdr {
411411
#define NT_X86_XSTATE 0x202 /* x86 extended state using xsave */
412412
/* Old binutils treats 0x203 as a CET state */
413413
#define NT_X86_SHSTK 0x204 /* x86 SHSTK state */
414+
#define NT_X86_XSAVE_LAYOUT 0x205 /* XSAVE layout description */
414415
#define NT_S390_HIGH_GPRS 0x300 /* s390 upper register halves */
415416
#define NT_S390_TIMER 0x301 /* s390 timer register */
416417
#define NT_S390_TODCMP 0x302 /* s390 TOD clock comparator register */

0 commit comments

Comments
 (0)