All of lore.kernel.org
 help / color / mirror / Atom feed
From: Andre Przywara <andre.przywara@arm.com>
To: will.deacon@arm.com, kvm@vger.kernel.org
Cc: marc.zyngier@arm.com, kvmarm@lists.cs.columbia.edu,
	kvm-ppc@vger.kernel.org
Subject: [PATCH 04/14] x86: support loading flat binary kernel images from a pipe
Date: Thu, 30 Jul 2015 11:52:21 +0100	[thread overview]
Message-ID: <1438253551-2378-5-git-send-email-andre.przywara@arm.com> (raw)
In-Reply-To: <1438253551-2378-1-git-send-email-andre.przywara@arm.com>

With the latest patches we allow loading bzImage kernels from a pipe,
but we still fail on "flat binary" images.
Rework the loading routines to take memory buffers for the beginning
of the file, so we don't need to rewind the image.
This allows to fall back to flat binary loading if bzImage fails
without using a seek, so kvmtool will happily accept any file
descriptor (including pipes) for the image file.

Signed-off-by: Andre Przywara <andre.przywara@arm.com>
---
 x86/kvm.c | 48 +++++++++++++++++++++++++-----------------------
 1 file changed, 25 insertions(+), 23 deletions(-)

diff --git a/x86/kvm.c b/x86/kvm.c
index 8fe5585..9817953 100644
--- a/x86/kvm.c
+++ b/x86/kvm.c
@@ -206,16 +206,16 @@ static inline void *guest_real_to_host(struct kvm *kvm, u16 selector, u16 offset
 	return guest_flat_to_host(kvm, flat);
 }
 
-static bool load_flat_binary(struct kvm *kvm, int fd_kernel)
+static bool load_flat_binary(struct kvm *kvm, int fd_kernel, void *buf, int len)
 {
 	void *p;
 	int nr;
 
-	if (lseek(fd_kernel, 0, SEEK_SET) < 0)
-		die_perror("lseek");
-
 	p = guest_real_to_host(kvm, BOOT_LOADER_SELECTOR, BOOT_LOADER_IP);
 
+	memcpy(p, buf, len);
+	p += len;
+
 	while ((nr = read(fd_kernel, p, 65536)) > 0)
 		p += nr;
 
@@ -229,11 +229,10 @@ static bool load_flat_binary(struct kvm *kvm, int fd_kernel)
 static const char *BZIMAGE_MAGIC = "HdrS";
 
 static bool load_bzimage(struct kvm *kvm, int fd_kernel, int fd_initrd,
-			 const char *kernel_cmdline)
+			 const char *kernel_cmdline, struct boot_params *boot)
 {
 	struct boot_params *kern_boot;
 	unsigned long setup_sects;
-	struct boot_params boot;
 	size_t cmdline_size;
 	ssize_t setup_size;
 	void *p;
@@ -245,26 +244,23 @@ static bool load_bzimage(struct kvm *kvm, int fd_kernel, int fd_initrd,
 	 * memory layout.
 	 */
 
-	if (read(fd_kernel, &boot, sizeof(boot)) != sizeof(boot))
-		return false;
-
-	if (memcmp(&boot.hdr.header, BZIMAGE_MAGIC, strlen(BZIMAGE_MAGIC)))
+	if (memcmp(&boot->hdr.header, BZIMAGE_MAGIC, strlen(BZIMAGE_MAGIC)))
 		return false;
 
-	if (boot.hdr.version < BOOT_PROTOCOL_REQUIRED)
+	if (boot->hdr.version < BOOT_PROTOCOL_REQUIRED)
 		die("Too old kernel");
 
-	if (!boot.hdr.setup_sects)
-		boot.hdr.setup_sects = BZ_DEFAULT_SETUP_SECTS;
-	setup_sects = boot.hdr.setup_sects + 1;
+	if (!boot->hdr.setup_sects)
+		boot->hdr.setup_sects = BZ_DEFAULT_SETUP_SECTS;
+	setup_sects = boot->hdr.setup_sects + 1;
 
 	setup_size = setup_sects << 9;
 	p = guest_real_to_host(kvm, BOOT_LOADER_SELECTOR, BOOT_LOADER_IP);
 
 	/* copy setup.bin to mem */
-	memcpy(p, &boot, sizeof(boot));
-	p += sizeof(boot);
-	setup_size -= sizeof(boot);
+	memcpy(p, boot, sizeof(struct boot_params));
+	p += sizeof(struct boot_params);
+	setup_size -= sizeof(struct boot_params);
 	if (read(fd_kernel, p, setup_size) != setup_size)
 		die_perror("read");
 
@@ -277,10 +273,10 @@ static bool load_bzimage(struct kvm *kvm, int fd_kernel, int fd_initrd,
 	p = guest_flat_to_host(kvm, BOOT_CMDLINE_OFFSET);
 	if (kernel_cmdline) {
 		cmdline_size = strlen(kernel_cmdline) + 1;
-		if (cmdline_size > boot.hdr.cmdline_size)
-			cmdline_size = boot.hdr.cmdline_size;
+		if (cmdline_size > boot->hdr.cmdline_size)
+			cmdline_size = boot->hdr.cmdline_size;
 
-		memset(p, 0, boot.hdr.cmdline_size);
+		memset(p, 0, boot->hdr.cmdline_size);
 		memcpy(p, kernel_cmdline, cmdline_size - 1);
 	}
 
@@ -313,7 +309,7 @@ static bool load_bzimage(struct kvm *kvm, int fd_kernel, int fd_initrd,
 		if (fstat(fd_initrd, &initrd_stat))
 			die_perror("fstat");
 
-		addr = boot.hdr.initrd_addr_max & ~0xfffff;
+		addr = boot->hdr.initrd_addr_max & ~0xfffff;
 		for (;;) {
 			if (addr < BZ_KERNEL_START)
 				die("Not enough memory for initrd");
@@ -345,15 +341,21 @@ static bool load_bzimage(struct kvm *kvm, int fd_kernel, int fd_initrd,
 bool kvm__arch_load_kernel_image(struct kvm *kvm, int fd_kernel, int fd_initrd,
 				 const char *kernel_cmdline)
 {
-	if (load_bzimage(kvm, fd_kernel, fd_initrd, kernel_cmdline))
+	struct boot_params boot;
+
+	if (read(fd_kernel, &boot, sizeof(boot)) != sizeof(boot))
+		return false;
+
+	if (load_bzimage(kvm, fd_kernel, fd_initrd, kernel_cmdline, &boot))
 		return true;
+
 	pr_warning("Kernel image is not a bzImage.");
 	pr_warning("Trying to load it as a flat binary (no cmdline support)");
 
 	if (fd_initrd != -1)
 		pr_warning("Loading initrd with flat binary not supported.");
 
-	return load_flat_binary(kvm, fd_kernel);
+	return load_flat_binary(kvm, fd_kernel, &boot, sizeof(boot));
 }
 
 /**
-- 
2.3.5

WARNING: multiple messages have this Message-ID (diff)
From: Andre Przywara <andre.przywara@arm.com>
To: will.deacon@arm.com, kvm@vger.kernel.org
Cc: marc.zyngier@arm.com, kvmarm@lists.cs.columbia.edu,
	kvm-ppc@vger.kernel.org
Subject: [PATCH 04/14] x86: support loading flat binary kernel images from a pipe
Date: Thu, 30 Jul 2015 10:52:21 +0000	[thread overview]
Message-ID: <1438253551-2378-5-git-send-email-andre.przywara@arm.com> (raw)
In-Reply-To: <1438253551-2378-1-git-send-email-andre.przywara@arm.com>

With the latest patches we allow loading bzImage kernels from a pipe,
but we still fail on "flat binary" images.
Rework the loading routines to take memory buffers for the beginning
of the file, so we don't need to rewind the image.
This allows to fall back to flat binary loading if bzImage fails
without using a seek, so kvmtool will happily accept any file
descriptor (including pipes) for the image file.

Signed-off-by: Andre Przywara <andre.przywara@arm.com>
---
 x86/kvm.c | 48 +++++++++++++++++++++++++-----------------------
 1 file changed, 25 insertions(+), 23 deletions(-)

diff --git a/x86/kvm.c b/x86/kvm.c
index 8fe5585..9817953 100644
--- a/x86/kvm.c
+++ b/x86/kvm.c
@@ -206,16 +206,16 @@ static inline void *guest_real_to_host(struct kvm *kvm, u16 selector, u16 offset
 	return guest_flat_to_host(kvm, flat);
 }
 
-static bool load_flat_binary(struct kvm *kvm, int fd_kernel)
+static bool load_flat_binary(struct kvm *kvm, int fd_kernel, void *buf, int len)
 {
 	void *p;
 	int nr;
 
-	if (lseek(fd_kernel, 0, SEEK_SET) < 0)
-		die_perror("lseek");
-
 	p = guest_real_to_host(kvm, BOOT_LOADER_SELECTOR, BOOT_LOADER_IP);
 
+	memcpy(p, buf, len);
+	p += len;
+
 	while ((nr = read(fd_kernel, p, 65536)) > 0)
 		p += nr;
 
@@ -229,11 +229,10 @@ static bool load_flat_binary(struct kvm *kvm, int fd_kernel)
 static const char *BZIMAGE_MAGIC = "HdrS";
 
 static bool load_bzimage(struct kvm *kvm, int fd_kernel, int fd_initrd,
-			 const char *kernel_cmdline)
+			 const char *kernel_cmdline, struct boot_params *boot)
 {
 	struct boot_params *kern_boot;
 	unsigned long setup_sects;
-	struct boot_params boot;
 	size_t cmdline_size;
 	ssize_t setup_size;
 	void *p;
@@ -245,26 +244,23 @@ static bool load_bzimage(struct kvm *kvm, int fd_kernel, int fd_initrd,
 	 * memory layout.
 	 */
 
-	if (read(fd_kernel, &boot, sizeof(boot)) != sizeof(boot))
-		return false;
-
-	if (memcmp(&boot.hdr.header, BZIMAGE_MAGIC, strlen(BZIMAGE_MAGIC)))
+	if (memcmp(&boot->hdr.header, BZIMAGE_MAGIC, strlen(BZIMAGE_MAGIC)))
 		return false;
 
-	if (boot.hdr.version < BOOT_PROTOCOL_REQUIRED)
+	if (boot->hdr.version < BOOT_PROTOCOL_REQUIRED)
 		die("Too old kernel");
 
-	if (!boot.hdr.setup_sects)
-		boot.hdr.setup_sects = BZ_DEFAULT_SETUP_SECTS;
-	setup_sects = boot.hdr.setup_sects + 1;
+	if (!boot->hdr.setup_sects)
+		boot->hdr.setup_sects = BZ_DEFAULT_SETUP_SECTS;
+	setup_sects = boot->hdr.setup_sects + 1;
 
 	setup_size = setup_sects << 9;
 	p = guest_real_to_host(kvm, BOOT_LOADER_SELECTOR, BOOT_LOADER_IP);
 
 	/* copy setup.bin to mem */
-	memcpy(p, &boot, sizeof(boot));
-	p += sizeof(boot);
-	setup_size -= sizeof(boot);
+	memcpy(p, boot, sizeof(struct boot_params));
+	p += sizeof(struct boot_params);
+	setup_size -= sizeof(struct boot_params);
 	if (read(fd_kernel, p, setup_size) != setup_size)
 		die_perror("read");
 
@@ -277,10 +273,10 @@ static bool load_bzimage(struct kvm *kvm, int fd_kernel, int fd_initrd,
 	p = guest_flat_to_host(kvm, BOOT_CMDLINE_OFFSET);
 	if (kernel_cmdline) {
 		cmdline_size = strlen(kernel_cmdline) + 1;
-		if (cmdline_size > boot.hdr.cmdline_size)
-			cmdline_size = boot.hdr.cmdline_size;
+		if (cmdline_size > boot->hdr.cmdline_size)
+			cmdline_size = boot->hdr.cmdline_size;
 
-		memset(p, 0, boot.hdr.cmdline_size);
+		memset(p, 0, boot->hdr.cmdline_size);
 		memcpy(p, kernel_cmdline, cmdline_size - 1);
 	}
 
@@ -313,7 +309,7 @@ static bool load_bzimage(struct kvm *kvm, int fd_kernel, int fd_initrd,
 		if (fstat(fd_initrd, &initrd_stat))
 			die_perror("fstat");
 
-		addr = boot.hdr.initrd_addr_max & ~0xfffff;
+		addr = boot->hdr.initrd_addr_max & ~0xfffff;
 		for (;;) {
 			if (addr < BZ_KERNEL_START)
 				die("Not enough memory for initrd");
@@ -345,15 +341,21 @@ static bool load_bzimage(struct kvm *kvm, int fd_kernel, int fd_initrd,
 bool kvm__arch_load_kernel_image(struct kvm *kvm, int fd_kernel, int fd_initrd,
 				 const char *kernel_cmdline)
 {
-	if (load_bzimage(kvm, fd_kernel, fd_initrd, kernel_cmdline))
+	struct boot_params boot;
+
+	if (read(fd_kernel, &boot, sizeof(boot)) != sizeof(boot))
+		return false;
+
+	if (load_bzimage(kvm, fd_kernel, fd_initrd, kernel_cmdline, &boot))
 		return true;
+
 	pr_warning("Kernel image is not a bzImage.");
 	pr_warning("Trying to load it as a flat binary (no cmdline support)");
 
 	if (fd_initrd != -1)
 		pr_warning("Loading initrd with flat binary not supported.");
 
-	return load_flat_binary(kvm, fd_kernel);
+	return load_flat_binary(kvm, fd_kernel, &boot, sizeof(boot));
 }
 
 /**
-- 
2.3.5


  parent reply	other threads:[~2015-07-30 10:52 UTC|newest]

Thread overview: 30+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-07-30 10:52 [PATCH 00/14] kvmtool: Refactor kernel image loading to allow pipes Andre Przywara
2015-07-30 10:52 ` Andre Przywara
2015-07-30 10:52 ` [PATCH 01/14] Refactor kernel image loading Andre Przywara
2015-07-30 10:52   ` Andre Przywara
2015-07-30 10:52 ` [PATCH 02/14] arm/powerpc: remove unneeded seeks in kernel loading Andre Przywara
2015-07-30 10:52   ` Andre Przywara
2015-07-30 10:52 ` [PATCH 03/14] x86: allow pipes for bzImage kernel images Andre Przywara
2015-07-30 10:52   ` Andre Przywara
2015-07-30 10:52 ` Andre Przywara [this message]
2015-07-30 10:52   ` [PATCH 04/14] x86: support loading flat binary kernel images from a pipe Andre Przywara
2015-07-30 10:52 ` [PATCH 05/14] kvmtool: introduce pseek Andre Przywara
2015-07-30 10:52   ` Andre Przywara
2015-07-30 10:52 ` [PATCH 06/14] MIPS: use pseek() in ELF kernel image loading Andre Przywara
2015-07-30 10:52   ` Andre Przywara
2015-07-30 10:52 ` [PATCH 07/14] MIPS: move ELF headers loading outside of load_elf_binary() Andre Przywara
2015-07-30 10:52   ` Andre Przywara
2015-07-30 10:52 ` [PATCH 08/14] MIPS: remove seeks from load_flat_binary() Andre Przywara
2015-07-30 10:52   ` Andre Przywara
2015-07-30 10:52 ` [PATCH 09/14] arm: move kernel loading into arm/kvm.c Andre Przywara
2015-07-30 10:52   ` Andre Przywara
2015-07-30 10:52 ` [PATCH 10/14] provide generic read_file() implementation Andre Przywara
2015-07-30 10:52   ` Andre Przywara
2015-07-30 10:52 ` [PATCH 11/14] arm/arm64: use read_file() in kernel and initrd loading Andre Przywara
2015-07-30 10:52   ` Andre Przywara
2015-07-30 10:52 ` [PATCH 12/14] powerpc: " Andre Przywara
2015-07-30 10:52   ` Andre Przywara
2015-07-30 10:52 ` [PATCH 13/14] MIPS: use read wrappers in kernel loading Andre Przywara
2015-07-30 10:52   ` Andre Przywara
2015-07-30 10:52 ` [PATCH 14/14] x86: " Andre Przywara
2015-07-30 10:52   ` Andre Przywara

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1438253551-2378-5-git-send-email-andre.przywara@arm.com \
    --to=andre.przywara@arm.com \
    --cc=kvm-ppc@vger.kernel.org \
    --cc=kvm@vger.kernel.org \
    --cc=kvmarm@lists.cs.columbia.edu \
    --cc=marc.zyngier@arm.com \
    --cc=will.deacon@arm.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.