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 07/14] MIPS: move ELF headers loading outside of load_elf_binary()
Date: Thu, 30 Jul 2015 11:52:24 +0100	[thread overview]
Message-ID: <1438253551-2378-8-git-send-email-andre.przywara@arm.com> (raw)
In-Reply-To: <1438253551-2378-1-git-send-email-andre.przywara@arm.com>

Refactor MIPS' load_elf_binary() implementation by not reading the
ELF header itself, but using a pointer to a memory buffer instead.
This prepares for removing the need to rewind the image file later.

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

diff --git a/mips/kvm.c b/mips/kvm.c
index 4d08b20..ed81a02 100644
--- a/mips/kvm.c
+++ b/mips/kvm.c
@@ -282,46 +282,39 @@ static bool kvm__arch_get_elf_32_info(Elf32_Ehdr *ehdr, int fd_kernel,
 	return true;
 }
 
-static bool load_elf_binary(struct kvm *kvm, int fd_kernel)
-{
-	union {
-		Elf64_Ehdr ehdr;
-		Elf32_Ehdr ehdr32;
-	} eh;
+union ElfHeaders {
+	Elf64_Ehdr ehdr;
+	Elf32_Ehdr ehdr32;
+};
 
+static bool load_elf_binary(struct kvm *kvm, int fd_kernel,
+			    union ElfHeaders *eh)
+{
 	size_t nr;
 	char *p;
 	struct kvm__arch_elf_info ei;
 
-	if (lseek(fd_kernel, 0, SEEK_SET) < 0)
-		die_perror("lseek");
-
-	nr = read(fd_kernel, &eh, sizeof(eh));
-	if (nr != sizeof(eh)) {
-		pr_info("Couldn't read %d bytes for ELF header.", (int)sizeof(eh));
-		return false;
-	}
-
-	if (eh.ehdr.e_ident[EI_MAG0] != ELFMAG0 ||
-	    eh.ehdr.e_ident[EI_MAG1] != ELFMAG1 ||
-	    eh.ehdr.e_ident[EI_MAG2] != ELFMAG2 ||
-	    eh.ehdr.e_ident[EI_MAG3] != ELFMAG3 ||
-	    (eh.ehdr.e_ident[EI_CLASS] != ELFCLASS64 && eh.ehdr.e_ident[EI_CLASS] != ELFCLASS32) ||
-	    eh.ehdr.e_ident[EI_VERSION] != EV_CURRENT) {
+	if (eh->ehdr.e_ident[EI_MAG0] != ELFMAG0 ||
+	    eh->ehdr.e_ident[EI_MAG1] != ELFMAG1 ||
+	    eh->ehdr.e_ident[EI_MAG2] != ELFMAG2 ||
+	    eh->ehdr.e_ident[EI_MAG3] != ELFMAG3 ||
+	    (eh->ehdr.e_ident[EI_CLASS] != ELFCLASS64 &&
+		eh->ehdr.e_ident[EI_CLASS] != ELFCLASS32) ||
+	    eh->ehdr.e_ident[EI_VERSION] != EV_CURRENT) {
 		pr_info("Incompatible ELF header.");
 		return false;
 	}
-	if (eh.ehdr.e_type != ET_EXEC || eh.ehdr.e_machine != EM_MIPS) {
+	if (eh->ehdr.e_type != ET_EXEC || eh->ehdr.e_machine != EM_MIPS) {
 		pr_info("Incompatible ELF not MIPS EXEC.");
 		return false;
 	}
 
-	if (eh.ehdr.e_ident[EI_CLASS] == ELFCLASS64) {
-		if (!kvm__arch_get_elf_64_info(&eh.ehdr, fd_kernel, &ei))
+	if (eh->ehdr.e_ident[EI_CLASS] == ELFCLASS64) {
+		if (!kvm__arch_get_elf_64_info(&eh->ehdr, fd_kernel, &ei))
 			return false;
 		kvm->arch.is64bit = true;
 	} else {
-		if (!kvm__arch_get_elf_32_info(&eh.ehdr32, fd_kernel, &ei))
+		if (!kvm__arch_get_elf_32_info(&eh->ehdr32, fd_kernel, &ei))
 			return false;
 		kvm->arch.is64bit = false;
 	}
@@ -334,7 +327,8 @@ static bool load_elf_binary(struct kvm *kvm, int fd_kernel)
 	p = guest_flat_to_host(kvm, ei.load_addr);
 
 	pr_info("ELF Loading 0x%lx bytes from 0x%llx to 0x%llx",
-		(unsigned long)ei.len, (unsigned long long)ei.offset, (unsigned long long)ei.load_addr);
+		(unsigned long)ei.len, (unsigned long long)ei.offset,
+		(unsigned long long)ei.load_addr);
 	do {
 		nr = read(fd_kernel, p, ei.len);
 		if (nr < 0)
@@ -349,12 +343,16 @@ static bool load_elf_binary(struct kvm *kvm, int fd_kernel)
 bool kvm__arch_load_kernel_image(struct kvm *kvm, int fd_kernel, int fd_initrd,
 				 const char *kernel_cmdline)
 {
+	union ElfHeaders eh;
+
 	if (fd_initrd != -1) {
 		pr_err("Initrd not supported on MIPS.");
 		return false;
 	}
 
-	if (load_elf_binary(kvm, fd_kernel)) {
+	read_in_full(fd_kernel, &eh, sizeof(eh));
+
+	if (load_elf_binary(kvm, fd_kernel, &eh)) {
 		kvm__mips_install_cmdline(kvm);
 		return true;
 	}
-- 
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 07/14] MIPS: move ELF headers loading outside of load_elf_binary()
Date: Thu, 30 Jul 2015 10:52:24 +0000	[thread overview]
Message-ID: <1438253551-2378-8-git-send-email-andre.przywara@arm.com> (raw)
In-Reply-To: <1438253551-2378-1-git-send-email-andre.przywara@arm.com>

Refactor MIPS' load_elf_binary() implementation by not reading the
ELF header itself, but using a pointer to a memory buffer instead.
This prepares for removing the need to rewind the image file later.

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

diff --git a/mips/kvm.c b/mips/kvm.c
index 4d08b20..ed81a02 100644
--- a/mips/kvm.c
+++ b/mips/kvm.c
@@ -282,46 +282,39 @@ static bool kvm__arch_get_elf_32_info(Elf32_Ehdr *ehdr, int fd_kernel,
 	return true;
 }
 
-static bool load_elf_binary(struct kvm *kvm, int fd_kernel)
-{
-	union {
-		Elf64_Ehdr ehdr;
-		Elf32_Ehdr ehdr32;
-	} eh;
+union ElfHeaders {
+	Elf64_Ehdr ehdr;
+	Elf32_Ehdr ehdr32;
+};
 
+static bool load_elf_binary(struct kvm *kvm, int fd_kernel,
+			    union ElfHeaders *eh)
+{
 	size_t nr;
 	char *p;
 	struct kvm__arch_elf_info ei;
 
-	if (lseek(fd_kernel, 0, SEEK_SET) < 0)
-		die_perror("lseek");
-
-	nr = read(fd_kernel, &eh, sizeof(eh));
-	if (nr != sizeof(eh)) {
-		pr_info("Couldn't read %d bytes for ELF header.", (int)sizeof(eh));
-		return false;
-	}
-
-	if (eh.ehdr.e_ident[EI_MAG0] != ELFMAG0 ||
-	    eh.ehdr.e_ident[EI_MAG1] != ELFMAG1 ||
-	    eh.ehdr.e_ident[EI_MAG2] != ELFMAG2 ||
-	    eh.ehdr.e_ident[EI_MAG3] != ELFMAG3 ||
-	    (eh.ehdr.e_ident[EI_CLASS] != ELFCLASS64 && eh.ehdr.e_ident[EI_CLASS] != ELFCLASS32) ||
-	    eh.ehdr.e_ident[EI_VERSION] != EV_CURRENT) {
+	if (eh->ehdr.e_ident[EI_MAG0] != ELFMAG0 ||
+	    eh->ehdr.e_ident[EI_MAG1] != ELFMAG1 ||
+	    eh->ehdr.e_ident[EI_MAG2] != ELFMAG2 ||
+	    eh->ehdr.e_ident[EI_MAG3] != ELFMAG3 ||
+	    (eh->ehdr.e_ident[EI_CLASS] != ELFCLASS64 &&
+		eh->ehdr.e_ident[EI_CLASS] != ELFCLASS32) ||
+	    eh->ehdr.e_ident[EI_VERSION] != EV_CURRENT) {
 		pr_info("Incompatible ELF header.");
 		return false;
 	}
-	if (eh.ehdr.e_type != ET_EXEC || eh.ehdr.e_machine != EM_MIPS) {
+	if (eh->ehdr.e_type != ET_EXEC || eh->ehdr.e_machine != EM_MIPS) {
 		pr_info("Incompatible ELF not MIPS EXEC.");
 		return false;
 	}
 
-	if (eh.ehdr.e_ident[EI_CLASS] = ELFCLASS64) {
-		if (!kvm__arch_get_elf_64_info(&eh.ehdr, fd_kernel, &ei))
+	if (eh->ehdr.e_ident[EI_CLASS] = ELFCLASS64) {
+		if (!kvm__arch_get_elf_64_info(&eh->ehdr, fd_kernel, &ei))
 			return false;
 		kvm->arch.is64bit = true;
 	} else {
-		if (!kvm__arch_get_elf_32_info(&eh.ehdr32, fd_kernel, &ei))
+		if (!kvm__arch_get_elf_32_info(&eh->ehdr32, fd_kernel, &ei))
 			return false;
 		kvm->arch.is64bit = false;
 	}
@@ -334,7 +327,8 @@ static bool load_elf_binary(struct kvm *kvm, int fd_kernel)
 	p = guest_flat_to_host(kvm, ei.load_addr);
 
 	pr_info("ELF Loading 0x%lx bytes from 0x%llx to 0x%llx",
-		(unsigned long)ei.len, (unsigned long long)ei.offset, (unsigned long long)ei.load_addr);
+		(unsigned long)ei.len, (unsigned long long)ei.offset,
+		(unsigned long long)ei.load_addr);
 	do {
 		nr = read(fd_kernel, p, ei.len);
 		if (nr < 0)
@@ -349,12 +343,16 @@ static bool load_elf_binary(struct kvm *kvm, int fd_kernel)
 bool kvm__arch_load_kernel_image(struct kvm *kvm, int fd_kernel, int fd_initrd,
 				 const char *kernel_cmdline)
 {
+	union ElfHeaders eh;
+
 	if (fd_initrd != -1) {
 		pr_err("Initrd not supported on MIPS.");
 		return false;
 	}
 
-	if (load_elf_binary(kvm, fd_kernel)) {
+	read_in_full(fd_kernel, &eh, sizeof(eh));
+
+	if (load_elf_binary(kvm, fd_kernel, &eh)) {
 		kvm__mips_install_cmdline(kvm);
 		return true;
 	}
-- 
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 ` [PATCH 04/14] x86: support loading flat binary kernel images from a pipe Andre Przywara
2015-07-30 10:52   ` 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 ` Andre Przywara [this message]
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 ` [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-8-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.