arm64: Obtain text offset from kernel image
Marc Zyngier
maintainerPATCH 1/1 arm64: Obtain text offset from kernel image
Recent changes made to Linux 5.8 have outlined that kvmtool hardcodes the text offset instead of reading it from the arm64 image itself.
To address this, import the image header structure into kvmtool and do the right thing. 32bit guests are still loaded to their usual locations.
--- Makefile | 1 + arm/aarch32/include/kvm/kvm-arch.h | 2 +- arm/aarch64/include/asm/image.h | 59 ++++++++++++++++++++++++++++++ arm/aarch64/include/kvm/kvm-arch.h | 5 +-- arm/aarch64/kvm.c | 30 +++++++++++++++ arm/kvm.c | 2 +- 6 files changed, 94 insertions(+), 5 deletions(-) create mode 100644 arm/aarch64/include/asm/image.h create mode 100644 arm/aarch64/kvm.c
Makefile +1 -0
@@ -179,6 +179,7 @@ ifeq ($(ARCH), arm64) OBJS += $(OBJS_ARM_COMMON) OBJS += arm/aarch64/arm-cpu.o OBJS += arm/aarch64/kvm-cpu.o + OBJS += arm/aarch64/kvm.o ARCH_INCLUDE := $(HDRS_ARM_COMMON) ARCH_INCLUDE += -Iarm/aarch64/include
arm/aarch32/include/kvm/kvm-arch.h +1 -1
@@ -1,7 +1,7 @@#ifndef KVM__KVM_ARCH_H #define KVM__KVM_ARCH_H -#define ARM_KERN_OFFSET (...) 0x8000 +#define kvm__arch_get_kern_offset (...) 0x8000#define ARM_MAX_MEMORY (...) ARM_LOMAP_MAX_MEMORY
arm/aarch64/include/asm/image.h new file +59 -0
59 lines not on this page · raw message
arm/aarch64/include/kvm/kvm-arch.h +2 -3
@@ -1,9 +1,8 @@#ifndef KVM__KVM_ARCH_H #define KVM__KVM_ARCH_H -#define ARM_KERN_OFFSET (kvm) ((kvm)->cfg.arch.aarch32_guest ? \ - 0x8000 : \ - 0x80000) +struct kvm ; +unsigned long long kvm__arch_get_kern_offset (struct kvm *kvm,int fd);#define ARM_MAX_MEMORY (kvm) ((kvm)->cfg.arch.aarch32_guest ? \ ARM_LOMAP_MAX_MEMORY : \
arm/aarch64/kvm.c new file +30 -0
@@ -0,0 +1,30 @@ +#include "kvm/kvm.h" + +#include <asm/image.h> + +#include <linux/byteorder.h> + +unsigned long long kvm__arch_get_kern_offset (struct kvm *kvm,int fd) +{ +struct arm64_image_header header; +off_t cur_offset; +ssize_t size; + +/* the 32bit kernel offset is a well known value */ +if (kvm->cfg.arch.aarch32_guest) +return 0x8000 ; + + cur_offset =lseek (fd,0 ,SEEK_CUR ); +if (cur_offset == (off_t)-1 || +lseek (fd,0 ,SEEK_SET ) == (off_t)-1 ) +die ("Failed to seek in image file" ); + + size =xread (fd, &header,sizeof (header)); +if (size <0 || (size_t )size <sizeof (header)) +die ("Failed to read kernel image header" ); + +lseek (fd, cur_offset,SEEK_SET ); + +return le64_to_cpu (header.text_offset); +} +
arm/kvm.c +1 -1
@@ -103,7 +103,7 @@ bool kvm__arch_load_kernel_image(struct kvm *kvm, int fd_kernel, int fd_initrd,*/ limit = kvm->ram_start +min (kvm->ram_size, (u64 )SZ_256M ) -1 ; - pos = kvm->ram_start +ARM_KERN_OFFSET (kvm); + pos = kvm->ram_start +kvm__arch_get_kern_offset (kvm, fd_kernel); kvm->arch.kern_guest_start =host_to_guest_flat (kvm, pos); file_size =read_file (fd_kernel, pos, limit - pos);if (file_size <0 ) {
Signature · 6 lines
2.26.2 _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
Hi Marc,
Quoted 21 lines · Marc Zyngier
Recent changes made to Linux 5.8 have outlined that kvmtool hardcodes the text offset instead of reading it from the arm64 image itself.
To address this, import the image header structure into kvmtool and do the right thing. 32bit guests are still loaded to their usual locations.
Reported-by Ard Biesheuvel <[email protected]>Signed-off-by Marc Zyngier <[email protected]>--- Makefile | 1 + arm/aarch32/include/kvm/kvm-arch.h | 2 +- arm/aarch64/include/asm/image.h | 59 ++++++++++++++++++++++++++++++ arm/aarch64/include/kvm/kvm-arch.h | 5 +-- arm/aarch64/kvm.c | 30 +++++++++++++++ arm/kvm.c | 2 +- 6 files changed, 94 insertions(+), 5 deletions(-) create mode 100644 arm/aarch64/include/asm/image.h create mode 100644 arm/aarch64/kvm.c
Quoted 1 line
[..]
This is a great addition to kvmtool, thank you! Before I do a more in-depth review, I have some general questions.
Regarding the actual value of text_offset, the booting.rst document says:
"Prior to v3.17, the endianness of text_offset was not specified. In these cases image_size is zero and text_offset is 0x80000 in the endianness of the kernel. Where image_size is non-zero image_size is little-endian and must be respected. Where image_size is zero, text_offset can be assumed to be 0x80000".
All header fields are declared little-endian, which looks to me like it would break kernels older than 3.17. If that was intentional, I think it's worth documenting somewhere, or at least a comment for the kvm__arch_get_kern_offset function.
Now that we are parsing the kernel header, have you considered checking the magic number to make sure the user specified a valid kernel image? It might save someone some time debugging why the kernel isn't booting, if, for example, they are booting an armv7 kernel, but they forgot to specify --aarch32.
Thanks,
Alex
_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
Marc Zyngier
maintainerHi Alex,
Quoted 38 lines · Alexandru Elisei
Hi Marc,
Quoted 22 lines · Marc Zyngier
Recent changes made to Linux 5.8 have outlined that kvmtool hardcodes the text offset instead of reading it from the arm64 image itself.
To address this, import the image header structure into kvmtool and do the right thing. 32bit guests are still loaded to their usual locations.
Reported-by Ard Biesheuvel <[email protected]>Signed-off-by Marc Zyngier <[email protected]>--- Makefile | 1 + arm/aarch32/include/kvm/kvm-arch.h | 2 +- arm/aarch64/include/asm/image.h | 59 ++++++++++++++++++++++++++++++ arm/aarch64/include/kvm/kvm-arch.h | 5 +-- arm/aarch64/kvm.c | 30 +++++++++++++++ arm/kvm.c | 2 +- 6 files changed, 94 insertions(+), 5 deletions(-) create mode 100644 arm/aarch64/include/asm/image.h create mode 100644 arm/aarch64/kvm.cQuoted 1 line
[..]
This is a great addition to kvmtool, thank you! Before I do a more in-depth review, I have some general questions.
Regarding the actual value of
text_offset, thebooting.rstdocument says:"Prior to v3.17, the endianness of
text_offsetwas not specified. In these casesimage_sizeis zero andtext_offsetis0x80000in the endianness of the kernel. Whereimage_sizeis non-zeroimage_sizeis little-endian and must be respected. Whereimage_sizeis zero,text_offsetcan be assumed to be0x80000".
Quoted 4 lines
All header fields are declared little-endian, which looks to me like it would break kernels older than 3.17. If that was intentional, I think it's worth documenting somewhere, or at least a comment for the
kvm__arch_get_kern_offsetfunction.
TBH, I didn't give pre-3.17 *big-endian* much thought. Happy to document it though.
Quoted 7 lines
Now that we are parsing the kernel header, have you considered checking the magic
number to make sure the user specified a valid kernel image? It might save someone
some time debugging why the kernel isn't booting, if, for example, they are booting an armv7 kernel, but they forgot to specify --aarch32.
That'd be interesting. I'd be reluctant to prevent it from booting altogether, but I can definitely detect the lack of signature and print a warning.
Thanks,
M.
Signature · 6 lines
Jazz is not dead. It just smells funny... _______________________________________________ linux-arm-kernel mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-arm-kernel