kexec-tools/arm64: Add support to read PHYS_OFFSET from vmcoreinfo inside '/proc/kcore'
Subsystems (2)
cover kexec-tools/arm64: Add support to read PHYS_OFFSET from vmcoreinfo inside '/proc/kcore'
Since kernel version 4.19-rc5 (Commit 23c85094fe1895caefdd ["proc/kcore: add vmcoreinfo note to /proc/kcore"]), '/proc/kcore' contains a new PT_NOTE which carries the VMCOREINFO information.
If the same is available, one should prefer the same to retrieve 'PHYS_OFFSET' value exported by the kernel as this is now the standard interface exposed by kernel for sharing machine specific details with the user-land as per the arm64 kernel maintainers (see [0]).
Also on certain arm64 platforms, it has been noticed that due to a hole at the start of physical ram exposed to kernel (i.e. it doesn't start from address 0), the kernel still calculates the 'memstart_addr' kernel variable as 0.
Whereas the SYSTEM_RAM or IOMEM_RESERVED range in '/proc/iomem' would carry a first entry whose start address is non-zero (as the physical ram exposed to the kernel starts from a non-zero address).
In such cases, if we rely on '/proc/iomem' entries to calculate the phys_offset, then we will have mismatch between the user-space and kernel space 'PHYS_OFFSET' value. The present 'kexec-tools' code does the same in 'get_memory_ranges_iomem_cb()' function when it makes a call to 'set_phys_offset()'. This can cause the vmcore generated via 'kexec-tools' to miss the last few bytes as the first '/proc/iomem' starts from a non-zero address.
Details about the patches in this series:
----------------------------------------
PATCH 1/2 ->
- Tries to move the elf read functionality from 'vmcore-dmesg.c' to a new utility library named 'elf_info.c', so that subsequent patches can use the same.
- See the patch log for more details.
PATCH 2/2 ->
- Implements the actual functionality of reading the PHYS_OFFSET for arm64 platforms from the vmcoreinfo inside '/proc/kcore' (if available).
- See the patch log for more details.
Testing:
--------
I tested this patchset on qualcomm-amberwing and huawei-taishan arm64 boards for non-KALSR boot mode. Both display the same PHYS_OFFSET in user-space as the value used by kernel after this patch.
I will send a separate followup patch to fix the KASLR boot cases (as the discussion on the same is in progress with the arm64 kernel maintainers).
[0] https://www.mail-archive.com/[email protected]/msg20300.html
Bhupesh Sharma (2):
util_lib: Add functionality to read elf notes
arm64: Add support to read PHYS_OFFSET from '/proc/kcore' (if
available)
kexec/arch/arm64/kexec-arm64.c | 73 ++ util_lib/Makefile | 4 +- vmcore-dmesg/vmcore-dmesg.c => util_lib/elf_info.c | 157 +++-- util_lib/include/elf_info.h | 30 + vmcore-dmesg/Makefile | 4 +- vmcore-dmesg/vmcore-dmesg.c | 742 +-------------------- 6 files changed, 219 insertions(+), 791 deletions(-) copy vmcore-dmesg/vmcore-dmesg.c => util_lib/elf_info.c (88%) create mode 100644 util_lib/include/elf_info.h
Signature · 7 lines
2.7.4 _______________________________________________ kexec mailing list [email protected] http://lists.infradead.org/mailman/listinfo/kexec
PATCH 1/2 util_lib: Add functionality to read elf notes
'vmcore-dmesg.c' already implements functionality to read 'vmcoreinfo' from vmcore file. The same can be used in other features as well (one of which is described below), so there is merit in moving this to the utility libraries (util_lib).
Since kernel version 4.19-rc5 (Commit 23c85094fe1895caefdd ["proc/kcore: add vmcoreinfo note to /proc/kcore"]), '/proc/kcore' contains a new PT_NOTE which carries the VMCOREINFO information.
If the same is available, we can benefit by using it in 'kexec-tools'. This is especially useful for architectures like arm64 as we can get kernel symbols like 'PHYS_OFFSET' from the '/proc/kcore' itself and use it to calculate 'phys_offset' before we make a call to 'set_phys_offset()'.
Subsequent patch(es) in this series will use the same feature to read the 'vmcoreinfo' from '/proc/kcore'.
This patch also makes some of the functions like 'read_elf64()' which were present in 'vmcore-dmesg.c' non-static, so as to allow future patches to use them as library functions.
Also we add the capability to read 'NUMBER(PHYS_OFFSET)' from vmcoreinfo to the already present 'scan_vmcoreinfo()' code.
Future patches can look at reading more vmcoreinfo information (for e.g. 'kaslr_offset()' for x86_64 and arm64) by using the same framework.
--- util_lib/Makefile | 4 +- vmcore-dmesg/vmcore-dmesg.c => util_lib/elf_info.c | 157 +++-- util_lib/include/elf_info.h | 30 + vmcore-dmesg/Makefile | 4 +- vmcore-dmesg/vmcore-dmesg.c | 742 +-------------------- 5 files changed, 146 insertions(+), 791 deletions(-) copy vmcore-dmesg/vmcore-dmesg.c => util_lib/elf_info.c (88%) create mode 100644 util_lib/include/elf_info.h
util_lib/Makefile +3 -1
@@ -3,6 +3,7 @@ # UTIL_LIB_SRCS += UTIL_LIB_SRCS += util_lib/compute_ip_checksum.c +UTIL_LIB_SRCS += util_lib/elf_info.c UTIL_LIB_SRCS += util_lib/sha256.c UTIL_LIB_OBJS =$(call objify, $(UTIL_LIB_SRCS)) UTIL_LIB_DEPS =$(call depify, $(UTIL_LIB_OBJS))
@@ -11,7 +12,8 @@ UTIL_LIB = libutil.a -include $(UTIL_LIB_DEPS) dist += util_lib/Makefile $(UTIL_LIB_SRCS) \ - util_lib/include/sha256.h util_lib/include/ip_checksum.h + util_lib/include/elf_info.h util_lib/include/sha256.h \ + util_lib/include/ip_checksum.h clean += $(UTIL_LIB_OBJS) $(UTIL_LIB_DEPS) $(UTIL_LIB) $(UTIL_LIB): CPPFLAGS += -I$(srcdir)/util_lib/include
util_lib/elf_info.c renamed from vmcore-dmesg/vmcore-dmesg.c +107 -50
268 lines not on this page · raw message
util_lib/include/elf_info.h new file +30 -0
@@ -0,0 +1,30 @@ +#ifndef ELF_INFO_H +#define ELF_INFO_H + +#define _XOPEN_SOURCE 700 +#define _GNU_SOURCE +#define _LARGEFILE_SOURCE 1 +#define _FILE_OFFSET_BITS 64 + +#include <endian.h> +#include <byteswap.h> +#include <stdio.h> +#include <stdint.h> +#include <errno.h> +#include <stdlib.h> +#include <unistd.h> +#include <string.h> +#include <limits.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <fcntl.h> +#include <elf.h> +#include <stdbool.h> +#include <inttypes.h> +#include <ctype.h> + +int read_phys_offset_elf_kcore (int fd,unsigned long *phys_off); +int read_elf_kcore (int fd); +int read_elf_vmcore (int fd); + +#endif /* ELF_INFO_H */
vmcore-dmesg/Makefile +2 -2
@@ -15,9 +15,9 @@ clean += $(VMCORE_DMESG_OBJS) $(VMCORE_DMESG_DEPS) $(VMCORE_DMESG) $(VMCORE_DMES -include $(VMCORE_DMESG_DEPS) -$(VMCORE_DMESG): $(VMCORE_DMESG_OBJS) +$(VMCORE_DMESG): $(VMCORE_DMESG_OBJS) $(UTIL_LIB) @$(MKDIR) -p $(@D) - $(LINK.o) -o $@ $^ $(CFLAGS) + $(LINK.o) -o $@ $^ $(CFLAGS) $(LIBS) $(VMCORE_DMESG_MANPAGE): vmcore-dmesg/vmcore-dmesg.8 $(MKDIR) -p $(MANDIR)/man8
vmcore-dmesg/vmcore-dmesg.c +4 -738
757 lines not on this page · raw message
Signature · 7 lines
2.7.4 _______________________________________________ kexec mailing list [email protected] http://lists.infradead.org/mailman/listinfo/kexec
PATCH 2/2 arm64: Add support to read PHYS_OFFSET from '/proc/kcore' (if available)
In reply to Bhupesh SharmaSince kernel version 4.19-rc5 (Commit 23c85094fe1895caefdd ["proc/kcore: add vmcoreinfo note to /proc/kcore"]), '/proc/kcore' contains a new PT_NOTE which carries the VMCOREINFO information.
If the same is available, one should prefer the same to retrieve 'PHYS_OFFSET' value exported by the kernel as this is now the standard interface exposed by kernel for sharing machine specific details with the user-land as per the arm64 kernel maintainers (see [0]) .
Also on certain arm64 platforms, it has been noticed that due to a hole at the start of physical ram exposed to kernel (i.e. it doesn't start from address 0), the kernel still calculates the 'memstart_addr' kernel variable as 0.
Whereas the SYSTEM_RAM or IOMEM_RESERVED range in '/proc/iomem' would carry a first entry whose start address is non-zero (as the physical ram exposed to the kernel starts from a non-zero address).
In such cases, if we rely on '/proc/iomem' entries to calculate the phys_offset, then we will have mismatch between the user-space and kernel space 'PHYS_OFFSET' value. The present 'kexec-tools' code does the same in 'get_memory_ranges_iomem_cb()' function when it makes a call to 'set_phys_offset()'. This can cause the vmcore generated via 'kexec-tools' to miss the last few bytes as the first '/proc/iomem' starts from a non-zero address.
One such case was reported by Yanjiang Jin (which I was also able to reproduce on my qualcomm-amberwing boards). Please see [1] for the detailed discussion on the same.
Here is some background on that issue:
1. The EFI firmware on the qualcomm amberwing board can set the first EFI block as EfiReservedMemType:
Region1: 0x000000000000-0x000000200000 [EfiReservedMemType]
Region2: 0x000000200000-0x00000021fffff [EfiRuntimeServiceData]
But EFI API won't return the "EfiReservedMemType" memory to Linux kernel for security reasons, so kernel can't get any info about the first mem block, and kernel can only see region2 as below:
efi: Processing EFI memory map:
efi: 0x000000200000-0x00000021ffff [Runtime Data |RUN| | |
| | | | |WB|WT|WC|UC]
00200000-0021ffff : reserved
2a. If we add debug prints to kernel file 'arch/arm64/mm/init.c' to print the kernel Virtual map we can see that the memory node is set to:
..........
memory : 0xffff800000200000 - 0xffff801800000000
2b. Now if we use kdump (kexec -p) to obtain a crash vmcore we can see that if we use 'readelf' to get the last program Header from vmcore (logs below are for the non-kaslr case):
ELF Header:
........................
Program Headers:
Type Offset VirtAddr PhysAddr
FileSiz MemSiz Flags Align
..............................................................
LOAD 0x0000000076d40000 0xffff80017fe00000
0x0000000180000000 0x0000001680000000
0x0000001680000000 RWE 03. So if we do a simple calculation:
(VirtAddr + MemSiz) = 0xffff80017fe00000 + 0x0000001680000000 =
0xffff8017ffe00000
which is _not_ equal to 0xffff801800000000.
This indicates that the end virtual memory nodes are not the same between vmlinux and vmcore. This would eventually cause 'vmcore-dmesg' to fail while trying to read the vmcore, with an error message:
"No program header covering vaddr 0xXXXX found kexec bug?"
Note:
-----
This patch fixes the issue for non-KASLR boot cases on arm64 platforms, I will send a separate followup patch to fix the KASLR boot cases (as the discussion on the same is in progress with the arm64 kernel maintainers).
References:
-----------
[0] https://www.mail-archive.com/[email protected]/msg20300.html
[1] https://www.spinics.net/lists/kexec/msg20618.html
--- kexec/arch/arm64/kexec-arm64.c | 73 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+)
kexec/arch/arm64/kexec-arm64.c +73 -0
@@ -14,6 +14,7 @@#include <sys/stat.h> #include <linux/elf-em.h> #include <elf.h> +#include <elf_info.h> #include <unistd.h> #include <syscall.h>
@@ -38,6 +39,11 @@#define PROP_ELFCOREHDR "linux,elfcorehdr"#define PROP_USABLE_MEM_RANGE "linux,usable-memory-range" +/* Global flag which indicates that we have tried reading vmcoreinfo +* from '/proc/kcore' already. +*/ +static bool flag_read_vmcoreinfo_from_kcore = false; +/* Global varables the core kexec routines expect. */ unsigned char reuse_initrd;
@@ -740,17 +746,84 @@ void add_segment(struct kexec_info *info, const void *buf, size_t bufsz, }/** +* get_phys_offset_from_kcore - Helper for getting PHYS_OFFSET from kcore. +* +* Since kernel version 4.19, '/proc/kcore' contains a new +* PT_NOTE which carries the VMCOREINFO information. +* +* If the same is available, use it to retrieve 'PHYS_OFFSET' +* from the VMCOREINFO PT_NOTE present in '/proc/kcore'. +*/ + +static int get_phys_offset_from_kcore (unsigned long *phys_offset) +{ +int fd, ret; + +if ((fd =open ("/proc/kcore" ,O_RDONLY )) <0 ) { +dbgprintf ("Can't open (%s).\n" ,"/proc/kcore" ); +return EFAILED ; + } + + ret =read_phys_offset_elf_kcore (fd, phys_offset); +if (ret !=0 ) { +dbgprintf ("Can't find VMCOREINFO in '/proc/kcore'\n" ); +close (fd); +return ret; + } + +close (fd); +return 0 ; +} + +/** * get_memory_ranges_iomem_cb - Helper for get_memory_ranges_iomem. */ static int get_memory_ranges_iomem_cb (void *data,int nr,char *str,unsigned long long base,unsigned long long length) { +int ret; +unsigned long phys_offset =UINT64_MAX ;struct memory_range *r;if (nr >=KEXEC_SEGMENT_MAX )return -1 ; +/* Since kernel version 4.19, '/proc/kcore' contains a new +* PT_NOTE which carries the VMCOREINFO information. +* +* If the same is available, one should prefer the same to +* retrieve 'PHYS_OFFSET' value exported by the kernel as this +* is now the standard interface exposed by kernel for sharing +* machine specific details with the userland. +* +* Also on certain arm64 platforms, it has been noticed that due +* to a hole at the start of physical ram exposed to kernel +* (i.e. it doesn't start from address 0), the kernel still +* calculates the 'memstart_addr' kernel variable as 0. +* +* Whereas the SYSTEM_RAM or IOMEM_RESERVED range in '/proc/iomem' +* would carry a first entry whose start address is non-zero +* (as the physical ram exposed to the kernel starts from a +* non-zero address). +* +* In such cases, if we rely on '/proc/iomem' entries to +* calculate the phys_offset, then we will have mismatch +* between the user-space and kernel space 'PHYS_OFFSET' +* value. +*/ + +if (!flag_read_vmcoreinfo_from_kcore) { + ret =get_phys_offset_from_kcore (&phys_offset); +if (!ret) { +if (phys_offset !=UINT64_MAX ) +set_phys_offset (phys_offset); + + } + + flag_read_vmcoreinfo_from_kcore = true; + } + r = (struct memory_range *)data + nr;if (!strncmp (str,SYSTEM_RAM ,strlen (SYSTEM_RAM )))
Signature · 7 lines
2.7.4 _______________________________________________ kexec mailing list [email protected] http://lists.infradead.org/mailman/listinfo/kexec
Quoted 60 lines · Bhupesh Sharma
Since kernel version 4.19-rc5 (Commit
23c85094fe1895caefdd["proc/kcore: add vmcoreinfo note to/proc/kcore"]), '/proc/kcore' contains a newPT_NOTEwhich carries the VMCOREINFO information.If the same is available, one should prefer the same to retrieve '
PHYS_OFFSET' value exported by the kernel as this is now the standard interface exposed by kernel for sharing machine specific details with the user-land as per the arm64 kernel maintainers (see [0]).Also on certain arm64 platforms, it has been noticed that due to a hole at the start of physical ram exposed to kernel (i.e. it doesn't start from address 0), the kernel still calculates the '
memstart_addr' kernel variable as 0.Whereas the
SYSTEM_RAMorIOMEM_RESERVEDrange in '/proc/iomem' would carry a first entry whose start address is non-zero (as the physical ram exposed to the kernel starts from a non-zero address).In such cases, if we rely on '
/proc/iomem' entries to calculate thephys_offset, then we will have mismatch between the user-space and kernel space 'PHYS_OFFSET' value. The present 'kexec-tools' code does the same in 'get_memory_ranges_iomem_cb()' function when it makes a call to 'set_phys_offset()'. This can cause the vmcore generated via 'kexec-tools' to miss the last few bytes as the first '/proc/iomem' starts from a non-zero address.Details about the patches in this series:
----------------------------------------
PATCH 1/2 ->- Tries to move the elf read functionality from '
vmcore-dmesg.c' to a new utility library named 'elf_info.c', so that subsequent patches can use the same.
- See the patch log for more details.PATCH 2/2 ->
- Implements the actual functionality of reading thePHYS_OFFSETfor arm64 platforms from the vmcoreinfo inside '/proc/kcore' (if available).
- See the patch log for more details.Testing:
--------
I tested this patchset on qualcomm-amberwing and huawei-taishan arm64 boards for non-KALSR boot mode. Both display the samePHYS_OFFSETin user-space as the value used by kernel after this patch.I will send a separate followup patch to fix the KASLR boot cases (as the discussion on the same is in progress with the arm64 kernel maintainers).
[0] https://www.mail-archive.com/[email protected]/msg20300.html
Bhupesh Sharma (2):
util_lib: Add functionality to read elf notes
arm64: Add support to readPHYS_OFFSETfrom '/proc/kcore' (if
available)
Quoted 9 lines
kexec/arch/arm64/kexec-arm64.c | 73 ++ util_lib/Makefile | 4 +- vmcore-dmesg/vmcore-dmesg.c => util_lib/elf_info.c | 157 +++-- util_lib/include/elf_info.h | 30 + vmcore-dmesg/Makefile | 4 +- vmcore-dmesg/vmcore-dmesg.c | 742 +-------------------- 6 files changed, 219 insertions(+), 791 deletions(-) copy vmcore-dmesg/vmcore-dmesg.c => util_lib/elf_info.c (88%) create mode 100644 util_lib/include/elf_info.h
Sorry for letting this slip through the cracks.
Please let me know if this is still relevant.
_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec