2784 lines · 68.2 KB · C
RawDownload
/* * kexec.c - kexec system call * 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. */ #define pr_fmt(fmt)	"kexec: " fmt #include <linux/capability.h>#include <linux/mm.h>#include <linux/file.h>#include <linux/slab.h>#include <linux/fs.h>#include <linux/kexec.h>#include <linux/mutex.h>#include <linux/list.h>#include <linux/highmem.h>#include <linux/syscalls.h>#include <linux/reboot.h>#include <linux/ioport.h>#include <linux/hardirq.h>#include <linux/elf.h>#include <linux/elfcore.h>#include <linux/utsname.h>#include <linux/numa.h>#include <linux/suspend.h>#include <linux/device.h>#include <linux/freezer.h>#include <linux/pm.h>#include <linux/cpu.h>#include <linux/console.h>#include <linux/vmalloc.h>#include <linux/swap.h>#include <linux/syscore_ops.h>#include <linux/compiler.h>#include <linux/hugetlb.h> #include <asm/page.h>#include <asm/uaccess.h>#include <asm/io.h>#include <asm/sections.h> #include <crypto/hash.h>#include <crypto/sha.h> /* Per cpu memory for storing cpu states in case of system crash. */note_buf_t __percpu *crash_notes; /* vmcoreinfo stuff */static unsigned char vmcoreinfo_data[VMCOREINFO_BYTES];u32 vmcoreinfo_note[VMCOREINFO_NOTE_SIZE/4];size_t vmcoreinfo_size;size_t vmcoreinfo_max_size = sizeof(vmcoreinfo_data); /* Flag to indicate we are going to kexec a new kernel */bool kexec_in_progress = false; /* * Declare these symbols weak so that if architecture provides a purgatory, * these will be overridden. */char __weak kexec_purgatory[0];size_t __weak kexec_purgatory_size = 0; #ifdef CONFIG_KEXEC_FILEstatic int kexec_calculate_store_digests(struct kimage *image);#endif /* Location of the reserved area for the crash kernel */struct resource crashk_res = {	.name  = "Crash kernel",	.start = 0,	.end   = 0,	.flags = IORESOURCE_BUSY | IORESOURCE_MEM};struct resource crashk_low_res = {	.name  = "Crash kernel",	.start = 0,	.end   = 0,	.flags = IORESOURCE_BUSY | IORESOURCE_MEM}; int kexec_should_crash(struct task_struct *p){	if (in_interrupt() || !p->pid || is_global_init(p) || panic_on_oops)		return 1;	return 0;} /* * When kexec transitions to the new kernel there is a one-to-one * mapping between physical and virtual addresses.  On processors * where you can disable the MMU this is trivial, and easy.  For * others it is still a simple predictable page table to setup. * * In that environment kexec copies the new kernel to its final * resting place.  This means I can only support memory whose * physical address can fit in an unsigned long.  In particular * addresses where (pfn << PAGE_SHIFT) > ULONG_MAX cannot be handled. * If the assembly stub has more restrictive requirements * KEXEC_SOURCE_MEMORY_LIMIT and KEXEC_DEST_MEMORY_LIMIT can be * defined more restrictively in <asm/kexec.h>. * * The code for the transition from the current kernel to the * the new kernel is placed in the control_code_buffer, whose size * is given by KEXEC_CONTROL_PAGE_SIZE.  In the best case only a single * page of memory is necessary, but some architectures require more. * Because this memory must be identity mapped in the transition from * virtual to physical addresses it must live in the range * 0 - TASK_SIZE, as only the user space mappings are arbitrarily * modifiable. * * The assembly stub in the control code buffer is passed a linked list * of descriptor pages detailing the source pages of the new kernel, * and the destination addresses of those source pages.  As this data * structure is not used in the context of the current OS, it must * be self-contained. * * The code has been made to work with highmem pages and will use a * destination page in its final resting place (if it happens * to allocate it).  The end product of this is that most of the * physical address space, and most of RAM can be used. * * Future directions include: *  - allocating a page table with the control code buffer identity *    mapped, to simplify machine_kexec and make kexec_on_panic more *    reliable. */ /* * KIMAGE_NO_DEST is an impossible destination address..., for * allocating pages whose destination address we do not care about. */#define KIMAGE_NO_DEST (-1UL) static int kimage_is_destination_range(struct kimage *image,				       unsigned long start, unsigned long end);static struct page *kimage_alloc_page(struct kimage *image,				       gfp_t gfp_mask,				       unsigned long dest); static int copy_user_segment_list(struct kimage *image,				  unsigned long nr_segments,				  struct kexec_segment __user *segments){	int ret;	size_t segment_bytes; 	/* Read in the segments */	image->nr_segments = nr_segments;	segment_bytes = nr_segments * sizeof(*segments);	ret = copy_from_user(image->segment, segments, segment_bytes);	if (ret)		ret = -EFAULT; 	return ret;} static int sanity_check_segment_list(struct kimage *image){	int result, i;	unsigned long nr_segments = image->nr_segments; 	/*	 * Verify we have good destination addresses.  The caller is	 * responsible for making certain we don't attempt to load	 * the new image into invalid or reserved areas of RAM.  This	 * just verifies it is an address we can use.	 *	 * Since the kernel does everything in page size chunks ensure	 * the destination addresses are page aligned.  Too many	 * special cases crop of when we don't do this.  The most	 * insidious is getting overlapping destination addresses	 * simply because addresses are changed to page size	 * granularity.	 */	result = -EADDRNOTAVAIL;	for (i = 0; i < nr_segments; i++) {		unsigned long mstart, mend; 		mstart = image->segment[i].mem;		mend   = mstart + image->segment[i].memsz;		if ((mstart & ~PAGE_MASK) || (mend & ~PAGE_MASK))			return result;		if (mend >= KEXEC_DESTINATION_MEMORY_LIMIT)			return result;	} 	/* Verify our destination addresses do not overlap.	 * If we alloed overlapping destination addresses	 * through very weird things can happen with no	 * easy explanation as one segment stops on another.	 */	result = -EINVAL;	for (i = 0; i < nr_segments; i++) {		unsigned long mstart, mend;		unsigned long j; 		mstart = image->segment[i].mem;		mend   = mstart + image->segment[i].memsz;		for (j = 0; j < i; j++) {			unsigned long pstart, pend;			pstart = image->segment[j].mem;			pend   = pstart + image->segment[j].memsz;			/* Do the segments overlap ? */			if ((mend > pstart) && (mstart < pend))				return result;		}	} 	/* Ensure our buffer sizes are strictly less than	 * our memory sizes.  This should always be the case,	 * and it is easier to check up front than to be surprised	 * later on.	 */	result = -EINVAL;	for (i = 0; i < nr_segments; i++) {		if (image->segment[i].bufsz > image->segment[i].memsz)			return result;	} 	/*	 * Verify we have good destination addresses.  Normally	 * the caller is responsible for making certain we don't	 * attempt to load the new image into invalid or reserved	 * areas of RAM.  But crash kernels are preloaded into a	 * reserved area of ram.  We must ensure the addresses	 * are in the reserved area otherwise preloading the	 * kernel could corrupt things.	 */ 	if (image->type == KEXEC_TYPE_CRASH) {		result = -EADDRNOTAVAIL;		for (i = 0; i < nr_segments; i++) {			unsigned long mstart, mend; 			mstart = image->segment[i].mem;			mend = mstart + image->segment[i].memsz - 1;			/* Ensure we are within the crash kernel limits */			if ((mstart < crashk_res.start) ||			    (mend > crashk_res.end))				return result;		}	} 	return 0;} static struct kimage *do_kimage_alloc_init(void){	struct kimage *image; 	/* Allocate a controlling structure */	image = kzalloc(sizeof(*image), GFP_KERNEL);	if (!image)		return NULL; 	image->head = 0;	image->entry = &image->head;	image->last_entry = &image->head;	image->control_page = ~0; /* By default this does not apply */	image->type = KEXEC_TYPE_DEFAULT; 	/* Initialize the list of control pages */	INIT_LIST_HEAD(&image->control_pages); 	/* Initialize the list of destination pages */	INIT_LIST_HEAD(&image->dest_pages); 	/* Initialize the list of unusable pages */	INIT_LIST_HEAD(&image->unusable_pages); 	return image;} static void kimage_free_page_list(struct list_head *list); static int kimage_alloc_init(struct kimage **rimage, unsigned long entry,			     unsigned long nr_segments,			     struct kexec_segment __user *segments,			     unsigned long flags){	int ret;	struct kimage *image;	bool kexec_on_panic = flags & KEXEC_ON_CRASH; 	if (kexec_on_panic) {		/* Verify we have a valid entry point */		if ((entry < crashk_res.start) || (entry > crashk_res.end))			return -EADDRNOTAVAIL;	} 	/* Allocate and initialize a controlling structure */	image = do_kimage_alloc_init();	if (!image)		return -ENOMEM; 	image->start = entry; 	ret = copy_user_segment_list(image, nr_segments, segments);	if (ret)		goto out_free_image; 	ret = sanity_check_segment_list(image);	if (ret)		goto out_free_image; 	 /* Enable the special crash kernel control page allocation policy. */	if (kexec_on_panic) {		image->control_page = crashk_res.start;		image->type = KEXEC_TYPE_CRASH;	} 	/*	 * 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.	 */	ret = -ENOMEM;	image->control_code_page = kimage_alloc_control_pages(image,					   get_order(KEXEC_CONTROL_PAGE_SIZE));	if (!image->control_code_page) {		pr_err("Could not allocate control_code_buffer\n");		goto out_free_image;	} 	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;		}	} 	*rimage = image;	return 0;out_free_control_pages:	kimage_free_page_list(&image->control_pages);out_free_image:	kfree(image);	return ret;} #ifdef CONFIG_KEXEC_FILEstatic int copy_file_from_fd(int fd, void **buf, unsigned long *buf_len){	struct fd f = fdget(fd);	int ret;	struct kstat stat;	loff_t pos;	ssize_t bytes = 0; 	if (!f.file)		return -EBADF; 	ret = vfs_getattr(&f.file->f_path, &stat);	if (ret)		goto out; 	if (stat.size > INT_MAX) {		ret = -EFBIG;		goto out;	} 	/* Don't hand 0 to vmalloc, it whines. */	if (stat.size == 0) {		ret = -EINVAL;		goto out;	} 	*buf = vmalloc(stat.size);	if (!*buf) {		ret = -ENOMEM;		goto out;	} 	pos = 0;	while (pos < stat.size) {		bytes = kernel_read(f.file, pos, (char *)(*buf) + pos,				    stat.size - pos);		if (bytes < 0) {			vfree(*buf);			ret = bytes;			goto out;		} 		if (bytes == 0)			break;		pos += bytes;	} 	if (pos != stat.size) {		ret = -EBADF;		vfree(*buf);		goto out;	} 	*buf_len = pos;out:	fdput(f);	return ret;} /* Architectures can provide this probe function */int __weak arch_kexec_kernel_image_probe(struct kimage *image, void *buf,					 unsigned long buf_len){	return -ENOEXEC;} void * __weak arch_kexec_kernel_image_load(struct kimage *image){	return ERR_PTR(-ENOEXEC);} void __weak arch_kimage_file_post_load_cleanup(struct kimage *image){} int __weak arch_kexec_kernel_verify_sig(struct kimage *image, void *buf,					unsigned long buf_len){	return -EKEYREJECTED;} /* Apply relocations of type RELA */int __weakarch_kexec_apply_relocations_add(const Elf_Ehdr *ehdr, Elf_Shdr *sechdrs,				 unsigned int relsec){	pr_err("RELA relocation unsupported.\n");	return -ENOEXEC;} /* Apply relocations of type REL */int __weakarch_kexec_apply_relocations(const Elf_Ehdr *ehdr, Elf_Shdr *sechdrs,			     unsigned int relsec){	pr_err("REL relocation unsupported.\n");	return -ENOEXEC;} /* * Free up memory used by kernel, initrd, and comand line. This is temporary * memory allocation which is not needed any more after these buffers have * been loaded into separate segments and have been copied elsewhere. */static void kimage_file_post_load_cleanup(struct kimage *image){	struct purgatory_info *pi = &image->purgatory_info; 	vfree(image->kernel_buf);	image->kernel_buf = NULL; 	vfree(image->initrd_buf);	image->initrd_buf = NULL; 	kfree(image->cmdline_buf);	image->cmdline_buf = NULL; 	vfree(pi->purgatory_buf);	pi->purgatory_buf = NULL; 	vfree(pi->sechdrs);	pi->sechdrs = NULL; 	/* See if architecture has anything to cleanup post load */	arch_kimage_file_post_load_cleanup(image); 	/*	 * Above call should have called into bootloader to free up	 * any data stored in kimage->image_loader_data. It should	 * be ok now to free it up.	 */	kfree(image->image_loader_data);	image->image_loader_data = NULL;} /* * In file mode list of segments is prepared by kernel. Copy relevant * data from user space, do error checking, prepare segment list */static intkimage_file_prepare_segments(struct kimage *image, int kernel_fd, int initrd_fd,			     const char __user *cmdline_ptr,			     unsigned long cmdline_len, unsigned flags){	int ret = 0;	void *ldata; 	ret = copy_file_from_fd(kernel_fd, &image->kernel_buf,				&image->kernel_buf_len);	if (ret)		return ret; 	/* Call arch image probe handlers */	ret = arch_kexec_kernel_image_probe(image, image->kernel_buf,

Lines 1–500 of 2784.