Skip to contentSkip to conversation

arm64: Obtain text offset from kernel image

PATCH Applied Marc Zyngier 3 participants 3 messages Jun 8, 2020 – Jun 9, 2020

Subsystems (2)
lore
Share & tools
lore

Marc Zyngier

author maintainer
#0 lore raw

PATCH v2 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.

While we're at it, check the image magic and default to the text offset to be 0x80000 when image_size is 0, as described in the kernel's booting.rst document.

---

Notes:

    v2: Check magic, assume offset to be 0x8000 if image_size is 0.
 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                  | 43 ++++++++++++++++++++++
 arm/kvm.c                          |  2 +-
 6 files changed, 107 insertions(+), 5 deletions(-)
 create mode 100644 arm/aarch64/include/asm/image.h
 create mode 100644 arm/aarch64/kvm.c
Makefile +1 -0
index d27ff38..35bb118 100644
@@ -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
index cd31e72..a772bb1 100644
@@ -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
index 9de623a..55ef8ed 100644
@@ -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 +43 -0

43 lines not on this page · raw message

arm/kvm.c +1 -1
index 1f85fc6..5aea18f 100644
@@ -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
Applied
#1 lore raw
Quoted 9 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.

[...]

Applied to kvmtool (master), thanks!

[1/1] arm64: Obtain text offset from kernel image

      https://git.kernel.org/will/kvmtool/c/fd0a05bd27dd

Cheers,

Signature · 10 lines
Will

https://fixes.arm64.dev
https://next.arm64.dev
https://will.arm64.dev

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
#2 lore raw
In reply to Marc Zyngier

Hi,

Quoted 15 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.

While we're at it, check the image magic and default to the text offset to be 0x80000 when image_size is 0, as described in the kernel's booting.rst document.

Quoted 6 lines

---

Notes:

    v2: Check magic, assume offset to be 0x8000 if image_size is 0.

[..]

I wanted to review the patch, but then I noticed that Will merged it. So I settled for testing it, to make sure nothing breaks. I was able to reproduce the issue reported on the mailing list - without this patch, when the kernel is compiled with CONFIG_RANDOMIZE_BASE not set, the kernel doesn't boot; with this patch applied, the same kernel boots successfully.

I also tested it with an aarch32 kernel, this is what I got:

$ taskset -c 4,5 ./lkvm run -c2 -m1024 -k ../kvmtool-vms/zImage -d
../kvmtool-vms/debian-10.3.0.img  -p 'earlycon root=/dev/vda2'
  # lkvm run -k ../kvmtool-vms/zImage -m 1024 -c 2 --name guest-1834
  Warning: Kernel image magic not matching
  Warning: unable to translate host address 0x13115a4c82d05a4d to guest
  Fatal: kernel image too big to contain in guest memory.

IMO, works as expected.

Thanks,
Alex
_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm