Received: by 2002:a05:6500:2018:b0:1fb:9675:f89d with SMTP id t24csp365235lqh; Fri, 31 May 2024 03:59:27 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCV3EMw5Iu0WN2kIMYj4s6lLFdELGWRgv3gMUEBQXh6CMo0B7er3Ybt2qR20D5AeJnz9g6u5PUKlpZCvc19LLCedH5eRErs87PZGPNpyng== X-Google-Smtp-Source: AGHT+IHhk7W4x2Q4jQANGE6TIH8GdYQ81I6+Pd867E2IrG/En2m0DCl8zVeVekolaZbBzRQhoFew X-Received: by 2002:a05:6808:140b:b0:3d1:d0dc:6024 with SMTP id 5614622812f47-3d1e35c0b72mr1646368b6e.36.1717153167092; Fri, 31 May 2024 03:59:27 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1717153167; cv=pass; d=google.com; s=arc-20160816; b=SAYR8QUWzp+LvFJdelXtv48Ul1UZ/aWUhI8FG/+4Uo3tV5cKEFCX0Y8ACjke0fHTWV v3wAH0mp1u1ybFuuk+P0iKunkO7y0bfebQj7AAtnBv9wdcab3IWFRaR7uPmAdaz7wZhu I4gt6FgK5OPsZHKxak6wwi2nptYIUMIRVKt/fE7IbiviAn7Z2duNGl22JhFVhRkieOFQ 6AK0ACU8kWaYTBaik2JddS/9wDbbX5Hw48EJAUsVz0eUYaeWXh6R7Sl4E+QMBneJtqfC +fYEEj1SO0UB8W6OBDwGCB68yEnOjf7VMgvs12lzIDzVTQeo8ULWSeCeM1UWzp9/Oqig v/Pg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-disposition:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:message-id:subject:cc :to:from:date:dkim-signature; bh=7FLex5LhVIj9BiTaijdudRX6o1hrMt70/hkVTmdb8Jk=; fh=QPfiCWaZpb7cEEmYeyV4Er6yBXh6ShGDfSIOI8SSyCc=; b=xJiLOu0UB7qQWSq70uX4KAsolvtH/fYlRsZunbZrNp3FKLdzBezRfGrn8yWaLVAXlH 2TBiBiO2CRUCGWk7MIldJhisu/qE+uE3JlYXR8Hnio918Ey3vdTHRvcVqdsCk0HADTsL E07oeOcJV40aYlAb7vU5lQ8ONblXYOEecbrYfNooeHl4nmjkvArrI2qkLHrKayKCYPq8 VUy0yfH5cEGoHuqpLjvt9Zlbm9aiIgTatSpFuRtA8Vo5gjf0/I+GvgarxxSgYy3IXjn8 Crek0HEmiK3PpvPf3lUdt7M+tZbPawbw5eSTJCQogI+iCtAf4nJ0z/PczLr0mL5v3UR6 JDdQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@alien8.de header.s=alien8 header.b=Zn0AgiIW; arc=pass (i=1 spf=pass spfdomain=alien8.de dkim=pass dkdomain=alien8.de dmarc=pass fromdomain=alien8.de); spf=pass (google.com: domain of linux-kernel+bounces-196703-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-196703-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=alien8.de Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id d75a77b69052e-43ff2466961si17587361cf.380.2024.05.31.03.59.26 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 31 May 2024 03:59:27 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-196703-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@alien8.de header.s=alien8 header.b=Zn0AgiIW; arc=pass (i=1 spf=pass spfdomain=alien8.de dkim=pass dkdomain=alien8.de dmarc=pass fromdomain=alien8.de); spf=pass (google.com: domain of linux-kernel+bounces-196703-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-196703-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=alien8.de Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id B64BF1C234E6 for ; Fri, 31 May 2024 10:59:26 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 7081E156967; Fri, 31 May 2024 10:59:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (4096-bit key) header.d=alien8.de header.i=@alien8.de header.b="Zn0AgiIW" Received: from mail.alien8.de (mail.alien8.de [65.109.113.108]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 79B8518756A; Fri, 31 May 2024 10:59:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=65.109.113.108 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717153161; cv=none; b=UW0ab3dTzH7F9e3VpBwdah3YWvc6WspVXrVYfU6zT6cPmWVk9+2EhTpE1i8IlxxDTSvKmMg0xquasK7Q+YmlTpRkH8oJSFas4qqgl0TmNlv78+0MxwHQ7jOK3tGQplP5cqOn34tFGYx1G+2Lj0nC86V7vBPkLPUn6gdAK7XT4C0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717153161; c=relaxed/simple; bh=SPCs0ZDo8VHDBpwJ3ERGRu25JrPzeqUHj9s47aVFe9I=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=vB0RCMXksUgIVpzoWsDPmfh0EugLnonRDZNPeWtqmsUiBO8DPrrsOmDDdw6XEIbwzRoRY8y76TI2GEEpBWt6HNpEVMdWPavP64VyzytrrOK9WSYTOxJTfiRIZS3BYVZ7a0LDfQgC0WL5uTDJlhZTaSnhFaB+Y33+/fsGTUw9juo= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=alien8.de; spf=pass smtp.mailfrom=alien8.de; dkim=pass (4096-bit key) header.d=alien8.de header.i=@alien8.de header.b=Zn0AgiIW; arc=none smtp.client-ip=65.109.113.108 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=alien8.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=alien8.de Received: from localhost (localhost.localdomain [127.0.0.1]) by mail.alien8.de (SuperMail on ZX Spectrum 128k) with ESMTP id 3527640E0177; Fri, 31 May 2024 10:59:14 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at mail.alien8.de Authentication-Results: mail.alien8.de (amavisd-new); dkim=pass (4096-bit key) header.d=alien8.de Received: from mail.alien8.de ([127.0.0.1]) by localhost (mail.alien8.de [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id W-c3zNk7YqU9; Fri, 31 May 2024 10:59:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=alien8.de; s=alien8; t=1717153149; bh=7FLex5LhVIj9BiTaijdudRX6o1hrMt70/hkVTmdb8Jk=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=Zn0AgiIWRkxoO/RUtUYVbqMX7ooGQBkzOIgN59QQPz8PMAggO9PVYtwvkCS7d3W0o vpVovhSccTNRqfYrsmvQypdOX6U5pfXGJ5edx3SCiKcS+UakdwTMA4Pmgu/dd8jirn CPSc3oLW8LH6uVtemr7drBpAigIjcP3fXRfk61ViOXLzizYfSCNz865ZDIfe4bTe2J roQV1ljaZoduJKpE5Zx4VSZxDzmE/Ggso5dmNVqWhFgZXuwdv9dQx+DAcUpb9/HFL/ cGxNvTSCit6wvyVErJy49ul5XWDYiWxHstSl/aqghIFpdt8cXQXP2JiJariVPKaO2b gM3TTkpQEQ2v9RCXgVJv3uF1CjoeeTYNo5dMhPrFJoBPmCKg36tVx1OSwB8egKMbEM F5D2mF2SrpUQPwWBS0B+k0x61PCu3xvIpdbqq1w3ECKQ01UPOeLMeea7qH0qPIWa9g 8cEySG/kfFIzBKIAlYTKtN8IjWTGSu3639u9q/g1dnL28UO/i8cfkWJVeAHPBdFcg9 OHUKpenr7RcLnkcLTeklELOXJlnvRJXxS3jFkBkwB506nQZbILPQeoxrFBUDG3PGpf SGV8rCBw+pXrow8f0t+agnSgM3W56MOjlxx02fuO4CKRlJevOeEKk6zgtgTRNIngpg vu+o+UC61KpaYYv+GvSw+Rts= Received: from zn.tnic (p5de8ee85.dip0.t-ipconnect.de [93.232.238.133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature ECDSA (P-256) server-digest SHA256) (No client certificate requested) by mail.alien8.de (SuperMail on ZX Spectrum 128k) with ESMTPSA id C1B8A40E01E8; Fri, 31 May 2024 10:58:48 +0000 (UTC) Date: Fri, 31 May 2024 12:58:43 +0200 From: Borislav Petkov To: "Balasubrmanian, Vignesh" Cc: "Balasubrmanian, Vignesh" , Thomas Gleixner , "linux-kernel@vger.kernel.org" , "linux-toolchains@vger.kernel.org" , "mpe@ellerman.id.au" , "npiggin@gmail.com" , "christophe.leroy@csgroup.eu" , "aneesh.kumar@kernel.org" , "naveen.n.rao@linux.ibm.com" , "ebiederm@xmission.com" , "keescook@chromium.org" , "x86@kernel.org" , "linuxppc-dev@lists.ozlabs.org" , "linux-mm@kvack.org" , "George, Jini Susan" , "matz@suse.de" , "binutils@sourceware.org" , "jhb@FreeBSD.org" , "felix.willgerodt@intel.com" Subject: Re: [PATCH v2 1/1] x86/elf: Add a new .note section containing Xfeatures information to x86 core files Message-ID: <20240531105843.GBZlmtY7j7p8LJfQOh@fat_crate.local> References: <87wmo4o3r4.ffs@tglx> <4a090901-9705-40aa-ac3d-d67c52660f22@amd.com> <20240522153433.GCZk4QiX4Hf0OuI48E@fat_crate.local> <902b1bf0-15e6-42df-8f86-21387deef437@amd.com> <20240523144543.GDZk9WlwKpCKx8I3RE@fat_crate.local> <664e101b-3689-4876-825b-a5aa12b7978f@amd.com> <20240526090554.GAZlL7cpva88mMUbCK@fat_crate.local> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: Ok, I went and worked in tglx's comments. This is what this should look like. Only build-tested. --- From cf23110f5cc24b6072ba7a26f31cff3ed486e6b3 Mon Sep 17 00:00:00 2001 From: Vignesh Balasubramanian Date: Tue, 7 May 2024 15:23:31 +0530 Subject: [PATCH] x86/elf: Add a new .note section containing xfeatures 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 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: Work in tglx' comments from https://lore.kernel.org/r/87wmo4o3r4.ffs@tglx, massage. ] Co-developed-by: Jini Susan George Signed-off-by: Jini Susan George Signed-off-by: Vignesh Balasubramanian Signed-off-by: Borislav Petkov (AMD) --- arch/x86/Kconfig | 1 + arch/x86/include/asm/elf.h | 9 ++++ arch/x86/kernel/fpu/xstate.c | 84 ++++++++++++++++++++++++++++++++++++ fs/binfmt_elf.c | 4 +- include/uapi/linux/elf.h | 1 + 5 files changed, 97 insertions(+), 2 deletions(-) diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig index e30ea4129d2c..46e44b087c94 100644 --- a/arch/x86/Kconfig +++ b/arch/x86/Kconfig @@ -107,6 +107,7 @@ config X86 select ARCH_HAS_DEBUG_WX select ARCH_HAS_ZONE_DMA_SET if EXPERT select ARCH_HAVE_NMI_SAFE_CMPXCHG + select ARCH_HAVE_EXTRA_ELF_NOTES select ARCH_MHP_MEMMAP_ON_MEMORY_ENABLE select ARCH_MIGHT_HAVE_ACPI_PDC if ACPI select ARCH_MIGHT_HAVE_PC_PARPORT diff --git a/arch/x86/include/asm/elf.h b/arch/x86/include/asm/elf.h index 1fb83d47711f..cad37090bbd3 100644 --- a/arch/x86/include/asm/elf.h +++ b/arch/x86/include/asm/elf.h @@ -13,6 +13,15 @@ #include #include +struct xfeat_component { + u32 type; + u32 size; + u32 offset; + u32 flags; +} __packed; + +_Static_assert(sizeof(struct xfeat_component)%4 == 0, "xfeat_component is not aligned"); + typedef unsigned long elf_greg_t; #define ELF_NGREG (sizeof(struct user_regs_struct) / sizeof(elf_greg_t)) diff --git a/arch/x86/kernel/fpu/xstate.c b/arch/x86/kernel/fpu/xstate.c index c5a026fee5e0..4c26f119c0d6 100644 --- a/arch/x86/kernel/fpu/xstate.c +++ b/arch/x86/kernel/fpu/xstate.c @@ -13,6 +13,7 @@ #include #include #include +#include #include #include @@ -1838,3 +1839,86 @@ int proc_pid_arch_status(struct seq_file *m, struct pid_namespace *ns, return 0; } #endif /* CONFIG_PROC_PID_ARCH_STATUS */ + +#ifdef CONFIG_COREDUMP +static const char owner_name[] = "LINUX"; + +/* + * Dump type, size, offset and flag values for every xfeature that is present. + */ +static int dump_xsave_layout_desc(struct coredump_params *cprm) +{ + int num_records = 0; + int i; + + for_each_extended_xfeature(i, fpu_kernel_cfg.max_features) { + struct xfeat_component xc = { + .type = i, + .size = xstate_sizes[i], + .offset = xstate_offsets[i], + /* reserved for future use */ + .flags = 0, + }; + + if (!dump_emit(cprm, &xc, sizeof(xc))) + return 0; + + num_records++; + } + return num_records; +} + +static int get_xsave_desc_size(void) +{ + int cnt = 0; + int i; + + for_each_extended_xfeature(i, fpu_kernel_cfg.max_features) + cnt++; + + return cnt * (sizeof(struct xfeat_component)); +} + +int elf_coredump_extra_notes_write(struct coredump_params *cprm) +{ + int num_records = 0; + struct elf_note en; + + if (!fpu_kernel_cfg.max_features) + return 0; + + en.n_namesz = sizeof(owner_name); + en.n_descsz = get_xsave_desc_size(); + en.n_type = NT_X86_XSAVE_LAYOUT; + + if (!dump_emit(cprm, &en, sizeof(en))) + return 1; + if (!dump_emit(cprm, owner_name, en.n_namesz)) + return 1; + if (!dump_align(cprm, 4)) + return 1; + + num_records = dump_xsave_layout_desc(cprm); + if (!num_records) + return 1; + + /* Total size should be equal to the number of records */ + if ((sizeof(struct xfeat_component) * num_records) != en.n_descsz) + return 1; + + return 0; +} + +int elf_coredump_extra_notes_size(void) +{ + int size; + + /* .note header */ + size = sizeof(struct elf_note); + /* name + align */ + size += roundup(sizeof(owner_name), 4); + size += get_xsave_desc_size(); + + return size; +} +#endif diff --git a/fs/binfmt_elf.c b/fs/binfmt_elf.c index a43897b03ce9..3d15c7369b29 100644 --- a/fs/binfmt_elf.c +++ b/fs/binfmt_elf.c @@ -2006,7 +2006,7 @@ static int elf_core_dump(struct coredump_params *cprm) { size_t sz = info.size; - /* For cell spufs */ + /* For cell spufs and x86 xstate */ sz += elf_coredump_extra_notes_size(); phdr4note = kmalloc(sizeof(*phdr4note), GFP_KERNEL); @@ -2070,7 +2070,7 @@ static int elf_core_dump(struct coredump_params *cprm) if (!write_note_info(&info, cprm)) goto end_coredump; - /* For cell spufs */ + /* For cell spufs and x86 xstate */ if (elf_coredump_extra_notes_write(cprm)) goto end_coredump; diff --git a/include/uapi/linux/elf.h b/include/uapi/linux/elf.h index b54b313bcf07..e30a9b47dc87 100644 --- a/include/uapi/linux/elf.h +++ b/include/uapi/linux/elf.h @@ -411,6 +411,7 @@ typedef struct elf64_shdr { #define NT_X86_XSTATE 0x202 /* x86 extended state using xsave */ /* Old binutils treats 0x203 as a CET state */ #define NT_X86_SHSTK 0x204 /* x86 SHSTK state */ +#define NT_X86_XSAVE_LAYOUT 0x205 /* XSAVE layout description */ #define NT_S390_HIGH_GPRS 0x300 /* s390 upper register halves */ #define NT_S390_TIMER 0x301 /* s390 timer register */ #define NT_S390_TODCMP 0x302 /* s390 TOD clock comparator register */ -- 2.43.0 -- Regards/Gruss, Boris. https://people.kernel.org/tglx/notes-about-netiquette