kexec.c 7.01 KB
Newer Older
1
/*
2
 * kexec.c - kexec_load system call
3 4 5 6 7 8
 * Copyright (C) 2002-2004 Eric Biederman  <ebiederm@xmission.com>
 *
 * This source code is licensed under the GNU General Public License,
 * Version 2.  See the file COPYING for more details.
 */

9 10
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

11
#include <linux/capability.h>
12 13 14
#include <linux/mm.h>
#include <linux/file.h>
#include <linux/kexec.h>
15
#include <linux/mutex.h>
16 17
#include <linux/list.h>
#include <linux/syscalls.h>
18
#include <linux/vmalloc.h>
19
#include <linux/slab.h>
20

21 22
#include "kexec_internal.h"

23 24 25
static int copy_user_segment_list(struct kimage *image,
				  unsigned long nr_segments,
				  struct kexec_segment __user *segments)
26
{
27
	int ret;
28 29 30 31 32
	size_t segment_bytes;

	/* Read in the segments */
	image->nr_segments = nr_segments;
	segment_bytes = nr_segments * sizeof(*segments);
33 34 35 36 37 38 39
	ret = copy_from_user(image->segment, segments, segment_bytes);
	if (ret)
		ret = -EFAULT;

	return ret;
}

40 41 42 43
static int kimage_alloc_init(struct kimage **rimage, unsigned long entry,
			     unsigned long nr_segments,
			     struct kexec_segment __user *segments,
			     unsigned long flags)
44
{
45
	int ret;
46
	struct kimage *image;
47 48 49 50
	bool kexec_on_panic = flags & KEXEC_ON_CRASH;

	if (kexec_on_panic) {
		/* Verify we have a valid entry point */
51 52
		if ((entry < phys_to_boot_phys(crashk_res.start)) ||
		    (entry > phys_to_boot_phys(crashk_res.end)))
53 54
			return -EADDRNOTAVAIL;
	}
55 56

	/* Allocate and initialize a controlling structure */
57 58 59 60 61 62
	image = do_kimage_alloc_init();
	if (!image)
		return -ENOMEM;

	image->start = entry;

63 64
	ret = copy_user_segment_list(image, nr_segments, segments);
	if (ret)
65 66
		goto out_free_image;

67
	if (kexec_on_panic) {
68
		/* Enable special crash kernel control page alloc policy. */
69 70 71 72
		image->control_page = crashk_res.start;
		image->type = KEXEC_TYPE_CRASH;
	}

73 74 75 76
	ret = sanity_check_segment_list(image);
	if (ret)
		goto out_free_image;

77 78 79 80 81
	/*
	 * Find a location for the control code buffer, and add it
	 * the vector of segments so that it's pages will also be
	 * counted as destination pages.
	 */
82
	ret = -ENOMEM;
83
	image->control_code_page = kimage_alloc_control_pages(image,
84
					   get_order(KEXEC_CONTROL_PAGE_SIZE));
85
	if (!image->control_code_page) {
86
		pr_err("Could not allocate control_code_buffer\n");
87
		goto out_free_image;
88 89
	}

90 91 92 93 94 95
	if (!kexec_on_panic) {
		image->swap_page = kimage_alloc_control_pages(image, 0);
		if (!image->swap_page) {
			pr_err("Could not allocate swap buffer\n");
			goto out_free_control_pages;
		}
Huang Ying's avatar
Huang Ying committed
96 97
	}

98 99
	*rimage = image;
	return 0;
100
out_free_control_pages:
101
	kimage_free_page_list(&image->control_pages);
102
out_free_image:
103
	kfree(image);
104
	return ret;
105 106
}

107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
static int do_kexec_load(unsigned long entry, unsigned long nr_segments,
		struct kexec_segment __user *segments, unsigned long flags)
{
	struct kimage **dest_image, *image;
	unsigned long i;
	int ret;

	if (flags & KEXEC_ON_CRASH) {
		dest_image = &kexec_crash_image;
		if (kexec_crash_image)
			arch_kexec_unprotect_crashkres();
	} else {
		dest_image = &kexec_image;
	}

	if (nr_segments == 0) {
		/* Uninstall image */
		kimage_free(xchg(dest_image, NULL));
		return 0;
	}
	if (flags & KEXEC_ON_CRASH) {
		/*
		 * Loading another kernel to switch to if this one
		 * crashes.  Free any current crash dump kernel before
		 * we corrupt it.
		 */
		kimage_free(xchg(&kexec_crash_image, NULL));
	}

	ret = kimage_alloc_init(&image, entry, nr_segments, segments, flags);
	if (ret)
		return ret;

	if (flags & KEXEC_PRESERVE_CONTEXT)
		image->preserve_context = 1;

	ret = machine_kexec_prepare(image);
	if (ret)
		goto out;

147 148 149 150 151 152 153 154
	/*
	 * Some architecture(like S390) may touch the crash memory before
	 * machine_kexec_prepare(), we must copy vmcoreinfo data after it.
	 */
	ret = kimage_crash_copy_vmcoreinfo(image);
	if (ret)
		goto out;

155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
	for (i = 0; i < nr_segments; i++) {
		ret = kimage_load_segment(image, &image->segment[i]);
		if (ret)
			goto out;
	}

	kimage_terminate(image);

	/* Install the new kernel and uninstall the old */
	image = xchg(dest_image, image);

out:
	if ((flags & KEXEC_ON_CRASH) && kexec_crash_image)
		arch_kexec_protect_crashkres();

	kimage_free(image);
	return ret;
}

174 175 176 177 178 179 180 181 182 183 184 185 186 187
/*
 * Exec Kernel system call: for obvious reasons only root may call it.
 *
 * This call breaks up into three pieces.
 * - A generic part which loads the new kernel from the current
 *   address space, and very carefully places the data in the
 *   allocated pages.
 *
 * - A generic part that interacts with the kernel and tells all of
 *   the devices to shut down.  Preventing on-going dmas, and placing
 *   the devices in a consistent state so a later kernel can
 *   reinitialize them.
 *
 * - A machine specific part that includes the syscall number
Geert Uytterhoeven's avatar
Geert Uytterhoeven committed
188
 *   and then copies the image to it's final destination.  And
189 190 191 192 193
 *   jumps into the image at entry.
 *
 * kexec does not sync, or unmount filesystems so if you need
 * that to happen you need to do that yourself.
 */
194

195 196
SYSCALL_DEFINE4(kexec_load, unsigned long, entry, unsigned long, nr_segments,
		struct kexec_segment __user *, segments, unsigned long, flags)
197 198 199 200
{
	int result;

	/* We only trust the superuser with rebooting the system. */
201
	if (!capable(CAP_SYS_BOOT) || kexec_load_disabled)
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
		return -EPERM;

	/*
	 * Verify we have a legal set of flags
	 * This leaves us room for future extensions.
	 */
	if ((flags & KEXEC_FLAGS) != (flags & ~KEXEC_ARCH_MASK))
		return -EINVAL;

	/* Verify we are on the appropriate architecture */
	if (((flags & KEXEC_ARCH_MASK) != KEXEC_ARCH) &&
		((flags & KEXEC_ARCH_MASK) != KEXEC_ARCH_DEFAULT))
		return -EINVAL;

	/* Put an artificial cap on the number
	 * of segments passed to kexec_load.
	 */
	if (nr_segments > KEXEC_SEGMENT_MAX)
		return -EINVAL;

	/* Because we write directly to the reserved memory
	 * region when loading crash kernels we need a mutex here to
	 * prevent multiple crash  kernels from attempting to load
	 * simultaneously, and to prevent a crash kernel from loading
	 * over the top of a in use crash kernel.
	 *
	 * KISS: always take the mutex.
	 */
230
	if (!mutex_trylock(&kexec_mutex))
231
		return -EBUSY;
Maneesh Soni's avatar
Maneesh Soni committed
232

233
	result = do_kexec_load(entry, nr_segments, segments, flags);
234

235
	mutex_unlock(&kexec_mutex);
Maneesh Soni's avatar
Maneesh Soni committed
236

237 238 239 240
	return result;
}

#ifdef CONFIG_COMPAT
241 242 243 244
COMPAT_SYSCALL_DEFINE4(kexec_load, compat_ulong_t, entry,
		       compat_ulong_t, nr_segments,
		       struct compat_kexec_segment __user *, segments,
		       compat_ulong_t, flags)
245 246 247 248 249 250 251 252
{
	struct compat_kexec_segment in;
	struct kexec_segment out, __user *ksegments;
	unsigned long i, result;

	/* Don't allow clients that don't understand the native
	 * architecture to do anything.
	 */
Maneesh Soni's avatar
Maneesh Soni committed
253
	if ((flags & KEXEC_ARCH_MASK) == KEXEC_ARCH_DEFAULT)
254 255
		return -EINVAL;

Maneesh Soni's avatar
Maneesh Soni committed
256
	if (nr_segments > KEXEC_SEGMENT_MAX)
257 258 259
		return -EINVAL;

	ksegments = compat_alloc_user_space(nr_segments * sizeof(out));
260
	for (i = 0; i < nr_segments; i++) {
261
		result = copy_from_user(&in, &segments[i], sizeof(in));
Maneesh Soni's avatar
Maneesh Soni committed
262
		if (result)
263 264 265 266 267 268 269 270
			return -EFAULT;

		out.buf   = compat_ptr(in.buf);
		out.bufsz = in.bufsz;
		out.mem   = in.mem;
		out.memsz = in.memsz;

		result = copy_to_user(&ksegments[i], &out, sizeof(out));
Maneesh Soni's avatar
Maneesh Soni committed
271
		if (result)
272 273 274 275 276 277
			return -EFAULT;
	}

	return sys_kexec_load(entry, nr_segments, ksegments, flags);
}
#endif