From: Vivek Goyal <vgoyal@redhat.com>
To: linux-kernel@vger.kernel.org, kexec@lists.infradead.org
Cc: mjg59@srcf.ucam.org, jkosina@suse.cz, hpa@zytor.com,
ebiederm@xmission.com, greg@kroah.com,
Vivek Goyal <vgoyal@redhat.com>
Subject: [PATCH 09/11] kexec: Provide a function to add a segment at fixed address
Date: Mon, 27 Jan 2014 13:57:49 -0500 [thread overview]
Message-ID: <1390849071-21989-10-git-send-email-vgoyal@redhat.com> (raw)
In-Reply-To: <1390849071-21989-1-git-send-email-vgoyal@redhat.com>
kexec_add_buffer() can find a suitable range of memory for user buffer and
add it to list of segments. But ELF loader will require that a buffer can
be loaded at the address it has been compiled for (ET_EXEC type executables).
So we need a helper function which can see if requested memory is valid and
available and add a segment accordiingly. This patch provides that helper
function. It will be used by elf loader in later patch.
Signed-off-by: Vivek Goyal <vgoyal@redhat.com>
---
include/linux/kexec.h | 3 +++
kernel/kexec.c | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 68 insertions(+)
diff --git a/include/linux/kexec.h b/include/linux/kexec.h
index d391ed7..2fb052c 100644
--- a/include/linux/kexec.h
+++ b/include/linux/kexec.h
@@ -208,6 +208,9 @@ extern asmlinkage long sys_kexec_load(unsigned long entry,
struct kexec_segment __user *segments,
unsigned long flags);
extern int kernel_kexec(void);
+extern int kexec_add_segment(struct kimage *image, char *buffer,
+ unsigned long bufsz, unsigned long memsz,
+ unsigned long base);
extern int kexec_add_buffer(struct kimage *image, char *buffer,
unsigned long bufsz, unsigned long memsz,
unsigned long buf_align, unsigned long buf_min,
diff --git a/kernel/kexec.c b/kernel/kexec.c
index 20169a4..9e4718b 100644
--- a/kernel/kexec.c
+++ b/kernel/kexec.c
@@ -2002,6 +2002,71 @@ static int __kexec_add_segment(struct kimage *image, char *buf,
return 0;
}
+static int validate_ram_range_callback(u64 start, u64 end, void *arg)
+{
+ struct kexec_segment *ksegment = arg;
+ u64 mstart = ksegment->mem;
+ u64 mend = ksegment->mem + ksegment->memsz - 1;
+
+ /* Found a valid range. Stop going through more ranges */
+ if (mstart >= start && mend <= end)
+ return 1;
+
+ /* Range did not match. Go to next one */
+ return 0;
+}
+
+/* Add a kexec segment at fixed address provided by caller */
+int kexec_add_segment(struct kimage *image, char *buffer, unsigned long bufsz,
+ unsigned long memsz, unsigned long base)
+{
+ struct kexec_segment ksegment;
+ int ret;
+
+ /* Currently adding segment this way is allowed only in file mode */
+ if (!image->file_mode)
+ return -EINVAL;
+
+ if (image->nr_segments >= KEXEC_SEGMENT_MAX)
+ return -EINVAL;
+
+ /*
+ * Make sure we are not trying to add segment after allocating
+ * control pages. All segments need to be placed first before
+ * any control pages are allocated. As control page allocation
+ * logic goes through list of segments to make sure there are
+ * no destination overlaps.
+ */
+ WARN_ONCE(!list_empty(&image->control_pages), "Adding kexec segment"
+ " after allocating control pages\n");
+
+ if (bufsz > memsz)
+ return -EINVAL;
+ if (memsz == 0)
+ return -EINVAL;
+
+ /* Align memsz to next page boundary */
+ memsz = ALIGN(memsz, PAGE_SIZE);
+
+ /* Make sure base is atleast page size aligned */
+ if (base & (PAGE_SIZE - 1))
+ return -EINVAL;
+
+ memset(&ksegment, 0, sizeof(struct kexec_segment));
+ ksegment.mem = base;
+ ksegment.memsz = memsz;
+
+ /* Validate memory range */
+ ret = walk_system_ram_res(base, base + memsz - 1, &ksegment,
+ validate_ram_range_callback);
+
+ /* If a valid range is found, 1 is returned */
+ if (ret != 1)
+ return -EINVAL;
+
+ return __kexec_add_segment(image, buffer, bufsz, base, memsz);
+}
+
static int locate_mem_hole_top_down(unsigned long start, unsigned long end,
struct kexec_buf *kbuf)
{
--
1.8.4.2
_______________________________________________
kexec mailing list
kexec@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/kexec
next prev parent reply other threads:[~2014-01-27 18:58 UTC|newest]
Thread overview: 53+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-01-27 18:57 [RFC PATCH 00/11][V2] kexec: A new system call to allow in kernel loading Vivek Goyal
2014-01-27 18:57 ` [PATCH 01/11] kexec: Move segment verification code in a separate function Vivek Goyal
2014-01-27 18:57 ` [PATCH 02/11] resource: Provide new functions to walk through resources Vivek Goyal
2014-01-27 18:57 ` [PATCH 03/11] bin2c: Move bin2c in scripts/basic Vivek Goyal
2014-01-27 21:12 ` Michal Marek
2014-01-27 21:18 ` Vivek Goyal
2014-01-27 21:54 ` Michal Marek
2014-01-27 18:57 ` [PATCH 04/11] kernel: Build bin2c based on config option CONFIG_BUILD_BIN2C Vivek Goyal
2014-01-27 18:57 ` [PATCH 05/11] kexec: Make kexec_segment user buffer pointer a union Vivek Goyal
2014-01-27 18:57 ` [PATCH 06/11] kexec: A new system call, kexec_file_load, for in kernel kexec Vivek Goyal
2014-02-21 14:59 ` Borislav Petkov
2014-02-24 16:41 ` Vivek Goyal
2014-02-25 19:35 ` Petr Tesarik
2014-02-25 21:47 ` Borislav Petkov
2014-02-26 15:37 ` Borislav Petkov
2014-02-26 15:46 ` Vivek Goyal
2014-01-27 18:57 ` [PATCH 07/11] kexec: Create a relocatable object called purgatory Vivek Goyal
2014-02-24 19:08 ` H. Peter Anvin
2014-02-25 16:43 ` Vivek Goyal
2014-02-25 16:55 ` H. Peter Anvin
2014-02-25 18:20 ` Vivek Goyal
2014-02-25 21:09 ` H. Peter Anvin
2014-02-26 14:52 ` Vivek Goyal
2014-02-26 16:00 ` Borislav Petkov
2014-02-26 16:32 ` Vivek Goyal
2014-02-27 15:44 ` Borislav Petkov
2014-01-27 18:57 ` [PATCH 08/11] kexec-bzImage: Support for loading bzImage using 64bit entry Vivek Goyal
2014-02-25 18:38 ` H. Peter Anvin
2014-02-25 18:43 ` Vivek Goyal
2014-02-27 21:36 ` Borislav Petkov
2014-02-28 16:31 ` Vivek Goyal
2014-03-05 16:37 ` Borislav Petkov
2014-03-05 16:40 ` H. Peter Anvin
2014-03-05 18:40 ` Vivek Goyal
2014-03-05 19:47 ` Borislav Petkov
2014-01-27 18:57 ` Vivek Goyal [this message]
2014-02-27 21:52 ` [PATCH 09/11] kexec: Provide a function to add a segment at fixed address Borislav Petkov
2014-02-28 16:56 ` Vivek Goyal
2014-03-10 10:01 ` Borislav Petkov
2014-03-10 15:35 ` Vivek Goyal
2014-01-27 18:57 ` [PATCH 10/11] kexec: Support for loading ELF x86_64 images Vivek Goyal
2014-02-28 14:58 ` Borislav Petkov
2014-02-28 17:11 ` Vivek Goyal
2014-03-07 17:12 ` Borislav Petkov
2014-03-07 18:39 ` Borislav Petkov
2014-03-10 14:42 ` Vivek Goyal
2014-03-12 16:19 ` Borislav Petkov
2014-03-12 17:24 ` Vivek Goyal
2014-01-27 18:57 ` [PATCH 11/11] kexec: Support for Kexec on panic using new system call Vivek Goyal
2014-02-28 17:28 ` Borislav Petkov
2014-02-28 21:06 ` Vivek Goyal
2014-05-26 8:25 ` [RFC PATCH 00/11][V2] kexec: A new system call to allow in kernel loading Borislav Petkov
2014-05-27 12:34 ` Vivek Goyal
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=1390849071-21989-10-git-send-email-vgoyal@redhat.com \
--to=vgoyal@redhat.com \
--cc=ebiederm@xmission.com \
--cc=greg@kroah.com \
--cc=hpa@zytor.com \
--cc=jkosina@suse.cz \
--cc=kexec@lists.infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mjg59@srcf.ucam.org \
/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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).