1534 lines · 38.0 KB · C
RawDownload
/* * kexec.c - kexec system call core code. * 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) KBUILD_MODNAME ": " 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/uaccess.h>#include <linux/io.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/sections.h> #include <crypto/hash.h>#include <crypto/sha.h>#include "kexec_internal.h" DEFINE_MUTEX(kexec_mutex); /* 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;  /* 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 crash_kexec_post_notifiers is enabled, don't run	 * crash_kexec() here yet, which must be run after panic	 * notifiers in panic().	 */	if (crash_kexec_post_notifiers)		return 0;	/*	 * There are 4 panic() calls in do_exit() path, each of which	 * corresponds to each of these 4 conditions.	 */	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 struct page *kimage_alloc_page(struct kimage *image,				       gfp_t gfp_mask,				       unsigned long dest); 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;} 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;} int kimage_is_destination_range(struct kimage *image,					unsigned long start,					unsigned long end){	unsigned long i; 	for (i = 0; i < image->nr_segments; i++) {		unsigned long mstart, mend; 		mstart = image->segment[i].mem;		mend = mstart + image->segment[i].memsz;		if ((end > mstart) && (start < mend))			return 1;	} 	return 0;} static struct page *kimage_alloc_pages(gfp_t gfp_mask, unsigned int order){	struct page *pages; 	pages = alloc_pages(gfp_mask, order);	if (pages) {		unsigned int count, i; 		pages->mapping = NULL;		set_page_private(pages, order);		count = 1 << order;		for (i = 0; i < count; i++)			SetPageReserved(pages + i);	} 	return pages;} static void kimage_free_pages(struct page *page){	unsigned int order, count, i; 	order = page_private(page);	count = 1 << order;	for (i = 0; i < count; i++)		ClearPageReserved(page + i);	__free_pages(page, order);} void kimage_free_page_list(struct list_head *list){	struct list_head *pos, *next; 	list_for_each_safe(pos, next, list) {		struct page *page; 		page = list_entry(pos, struct page, lru);		list_del(&page->lru);		kimage_free_pages(page);	}} static struct page *kimage_alloc_normal_control_pages(struct kimage *image,							unsigned int order){	/* Control pages are special, they are the intermediaries	 * that are needed while we copy the rest of the pages	 * to their final resting place.  As such they must	 * not conflict with either the destination addresses	 * or memory the kernel is already using.	 *	 * The only case where we really need more than one of	 * these are for architectures where we cannot disable	 * the MMU and must instead generate an identity mapped	 * page table for all of the memory.	 *	 * At worst this runs in O(N) of the image size.	 */	struct list_head extra_pages;	struct page *pages;	unsigned int count; 	count = 1 << order;	INIT_LIST_HEAD(&extra_pages); 	/* Loop while I can allocate a page and the page allocated	 * is a destination page.	 */	do {		unsigned long pfn, epfn, addr, eaddr; 		pages = kimage_alloc_pages(KEXEC_CONTROL_MEMORY_GFP, order);		if (!pages)			break;		pfn   = page_to_pfn(pages);		epfn  = pfn + count;		addr  = pfn << PAGE_SHIFT;		eaddr = epfn << PAGE_SHIFT;		if ((epfn >= (KEXEC_CONTROL_MEMORY_LIMIT >> PAGE_SHIFT)) ||			      kimage_is_destination_range(image, addr, eaddr)) {			list_add(&pages->lru, &extra_pages);			pages = NULL;		}	} while (!pages); 	if (pages) {		/* Remember the allocated page... */		list_add(&pages->lru, &image->control_pages); 		/* Because the page is already in it's destination		 * location we will never allocate another page at		 * that address.  Therefore kimage_alloc_pages		 * will not return it (again) and we don't need		 * to give it an entry in image->segment[].		 */	}	/* Deal with the destination pages I have inadvertently allocated.	 *	 * Ideally I would convert multi-page allocations into single	 * page allocations, and add everything to image->dest_pages.	 *	 * For now it is simpler to just free the pages.	 */	kimage_free_page_list(&extra_pages); 	return pages;} static struct page *kimage_alloc_crash_control_pages(struct kimage *image,						      unsigned int order){	/* Control pages are special, they are the intermediaries	 * that are needed while we copy the rest of the pages	 * to their final resting place.  As such they must	 * not conflict with either the destination addresses	 * or memory the kernel is already using.	 *	 * Control pages are also the only pags we must allocate	 * when loading a crash kernel.  All of the other pages	 * are specified by the segments and we just memcpy	 * into them directly.	 *	 * The only case where we really need more than one of	 * these are for architectures where we cannot disable	 * the MMU and must instead generate an identity mapped	 * page table for all of the memory.	 *	 * Given the low demand this implements a very simple	 * allocator that finds the first hole of the appropriate	 * size in the reserved memory region, and allocates all	 * of the memory up to and including the hole.	 */	unsigned long hole_start, hole_end, size;	struct page *pages; 	pages = NULL;	size = (1 << order) << PAGE_SHIFT;	hole_start = (image->control_page + (size - 1)) & ~(size - 1);	hole_end   = hole_start + size - 1;	while (hole_end <= crashk_res.end) {		unsigned long i; 		if (hole_end > KEXEC_CRASH_CONTROL_MEMORY_LIMIT)			break;		/* See if I overlap any of the segments */		for (i = 0; i < image->nr_segments; i++) {			unsigned long mstart, mend; 			mstart = image->segment[i].mem;			mend   = mstart + image->segment[i].memsz - 1;			if ((hole_end >= mstart) && (hole_start <= mend)) {				/* Advance the hole to the end of the segment */				hole_start = (mend + (size - 1)) & ~(size - 1);				hole_end   = hole_start + size - 1;				break;			}		}		/* If I don't overlap any segments I have found my hole! */		if (i == image->nr_segments) {			pages = pfn_to_page(hole_start >> PAGE_SHIFT);			image->control_page = hole_end;			break;		}	} 	return pages;}  struct page *kimage_alloc_control_pages(struct kimage *image,					 unsigned int order){	struct page *pages = NULL; 	switch (image->type) {	case KEXEC_TYPE_DEFAULT:		pages = kimage_alloc_normal_control_pages(image, order);		break;	case KEXEC_TYPE_CRASH:		pages = kimage_alloc_crash_control_pages(image, order);		break;	} 	return pages;} static int kimage_add_entry(struct kimage *image, kimage_entry_t entry){	if (*image->entry != 0)		image->entry++; 	if (image->entry == image->last_entry) {		kimage_entry_t *ind_page;		struct page *page; 		page = kimage_alloc_page(image, GFP_KERNEL, KIMAGE_NO_DEST);		if (!page)			return -ENOMEM; 		ind_page = page_address(page);		*image->entry = virt_to_phys(ind_page) | IND_INDIRECTION;		image->entry = ind_page;		image->last_entry = ind_page +				      ((PAGE_SIZE/sizeof(kimage_entry_t)) - 1);	}	*image->entry = entry;	image->entry++;	*image->entry = 0; 	return 0;} static int kimage_set_destination(struct kimage *image,				   unsigned long destination){	int result; 	destination &= PAGE_MASK;	result = kimage_add_entry(image, destination | IND_DESTINATION);

Lines 1–500 of 1534.