Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 1 | /* |
| 2 | * kexec.c - kexec system call |
| 3 | * Copyright (C) 2002-2004 Eric Biederman <ebiederm@xmission.com> |
| 4 | * |
| 5 | * This source code is licensed under the GNU General Public License, |
| 6 | * Version 2. See the file COPYING for more details. |
| 7 | */ |
| 8 | |
Randy.Dunlap | c59ede7 | 2006-01-11 12:17:46 -0800 | [diff] [blame] | 9 | #include <linux/capability.h> |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 10 | #include <linux/mm.h> |
| 11 | #include <linux/file.h> |
| 12 | #include <linux/slab.h> |
| 13 | #include <linux/fs.h> |
| 14 | #include <linux/kexec.h> |
Andrew Morton | 8c5a1cf | 2008-08-15 00:40:27 -0700 | [diff] [blame] | 15 | #include <linux/mutex.h> |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 16 | #include <linux/list.h> |
| 17 | #include <linux/highmem.h> |
| 18 | #include <linux/syscalls.h> |
| 19 | #include <linux/reboot.h> |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 20 | #include <linux/ioport.h> |
Alexander Nyberg | 6e274d1 | 2005-06-25 14:58:26 -0700 | [diff] [blame] | 21 | #include <linux/hardirq.h> |
Magnus Damm | 85916f8 | 2006-12-06 20:40:41 -0800 | [diff] [blame] | 22 | #include <linux/elf.h> |
| 23 | #include <linux/elfcore.h> |
Ken'ichi Ohmichi | fd59d23 | 2007-10-16 23:27:27 -0700 | [diff] [blame] | 24 | #include <linux/utsname.h> |
| 25 | #include <linux/numa.h> |
Huang Ying | 3ab8352 | 2008-07-25 19:45:07 -0700 | [diff] [blame] | 26 | #include <linux/suspend.h> |
| 27 | #include <linux/device.h> |
Huang Ying | 89081d1 | 2008-07-25 19:45:10 -0700 | [diff] [blame] | 28 | #include <linux/freezer.h> |
| 29 | #include <linux/pm.h> |
| 30 | #include <linux/cpu.h> |
| 31 | #include <linux/console.h> |
Luck, Tony | 5f41b8c | 2008-10-20 15:23:40 -0700 | [diff] [blame] | 32 | #include <linux/vmalloc.h> |
Amerigo Wang | 06a7f71 | 2009-12-15 16:47:46 -0800 | [diff] [blame] | 33 | #include <linux/swap.h> |
Rafael J. Wysocki | 19234c0 | 2011-04-20 00:36:11 +0200 | [diff] [blame] | 34 | #include <linux/syscore_ops.h> |
Gideon Israel Dsouza | 52f5684c | 2014-04-07 15:39:20 -0700 | [diff] [blame] | 35 | #include <linux/compiler.h> |
Atsushi Kumagai | 8f1d26d | 2014-07-30 16:08:39 -0700 | [diff] [blame] | 36 | #include <linux/hugetlb.h> |
Alexander Nyberg | 6e274d1 | 2005-06-25 14:58:26 -0700 | [diff] [blame] | 37 | |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 38 | #include <asm/page.h> |
| 39 | #include <asm/uaccess.h> |
| 40 | #include <asm/io.h> |
Ken'ichi Ohmichi | fd59d23 | 2007-10-16 23:27:27 -0700 | [diff] [blame] | 41 | #include <asm/sections.h> |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 42 | |
Vivek Goyal | cc57165 | 2006-01-09 20:51:41 -0800 | [diff] [blame] | 43 | /* Per cpu memory for storing cpu states in case of system crash. */ |
Tejun Heo | 43cf38e | 2010-02-02 14:38:57 +0900 | [diff] [blame] | 44 | note_buf_t __percpu *crash_notes; |
Vivek Goyal | cc57165 | 2006-01-09 20:51:41 -0800 | [diff] [blame] | 45 | |
Ken'ichi Ohmichi | fd59d23 | 2007-10-16 23:27:27 -0700 | [diff] [blame] | 46 | /* vmcoreinfo stuff */ |
Dmitri Vorobiev | edb79a2 | 2009-04-02 16:58:58 -0700 | [diff] [blame] | 47 | static unsigned char vmcoreinfo_data[VMCOREINFO_BYTES]; |
Ken'ichi Ohmichi | fd59d23 | 2007-10-16 23:27:27 -0700 | [diff] [blame] | 48 | u32 vmcoreinfo_note[VMCOREINFO_NOTE_SIZE/4]; |
Ken'ichi Ohmichi | d768281 | 2007-10-16 23:27:28 -0700 | [diff] [blame] | 49 | size_t vmcoreinfo_size; |
| 50 | size_t vmcoreinfo_max_size = sizeof(vmcoreinfo_data); |
Ken'ichi Ohmichi | fd59d23 | 2007-10-16 23:27:27 -0700 | [diff] [blame] | 51 | |
Khalid Aziz | 4fc9bbf | 2013-11-27 15:19:25 -0700 | [diff] [blame] | 52 | /* Flag to indicate we are going to kexec a new kernel */ |
| 53 | bool kexec_in_progress = false; |
| 54 | |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 55 | /* Location of the reserved area for the crash kernel */ |
| 56 | struct resource crashk_res = { |
| 57 | .name = "Crash kernel", |
| 58 | .start = 0, |
| 59 | .end = 0, |
| 60 | .flags = IORESOURCE_BUSY | IORESOURCE_MEM |
| 61 | }; |
Yinghai Lu | 0212f91 | 2013-01-24 12:20:11 -0800 | [diff] [blame] | 62 | struct resource crashk_low_res = { |
Yinghai Lu | 157752d | 2013-04-15 22:23:46 -0700 | [diff] [blame] | 63 | .name = "Crash kernel", |
Yinghai Lu | 0212f91 | 2013-01-24 12:20:11 -0800 | [diff] [blame] | 64 | .start = 0, |
| 65 | .end = 0, |
| 66 | .flags = IORESOURCE_BUSY | IORESOURCE_MEM |
| 67 | }; |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 68 | |
Alexander Nyberg | 6e274d1 | 2005-06-25 14:58:26 -0700 | [diff] [blame] | 69 | int kexec_should_crash(struct task_struct *p) |
| 70 | { |
Serge E. Hallyn | b460cbc | 2007-10-18 23:39:52 -0700 | [diff] [blame] | 71 | if (in_interrupt() || !p->pid || is_global_init(p) || panic_on_oops) |
Alexander Nyberg | 6e274d1 | 2005-06-25 14:58:26 -0700 | [diff] [blame] | 72 | return 1; |
| 73 | return 0; |
| 74 | } |
| 75 | |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 76 | /* |
| 77 | * When kexec transitions to the new kernel there is a one-to-one |
| 78 | * mapping between physical and virtual addresses. On processors |
| 79 | * where you can disable the MMU this is trivial, and easy. For |
| 80 | * others it is still a simple predictable page table to setup. |
| 81 | * |
| 82 | * In that environment kexec copies the new kernel to its final |
| 83 | * resting place. This means I can only support memory whose |
| 84 | * physical address can fit in an unsigned long. In particular |
| 85 | * addresses where (pfn << PAGE_SHIFT) > ULONG_MAX cannot be handled. |
| 86 | * If the assembly stub has more restrictive requirements |
| 87 | * KEXEC_SOURCE_MEMORY_LIMIT and KEXEC_DEST_MEMORY_LIMIT can be |
| 88 | * defined more restrictively in <asm/kexec.h>. |
| 89 | * |
| 90 | * The code for the transition from the current kernel to the |
| 91 | * the new kernel is placed in the control_code_buffer, whose size |
Huang Ying | 163f687 | 2008-08-15 00:40:22 -0700 | [diff] [blame] | 92 | * is given by KEXEC_CONTROL_PAGE_SIZE. In the best case only a single |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 93 | * page of memory is necessary, but some architectures require more. |
| 94 | * Because this memory must be identity mapped in the transition from |
| 95 | * virtual to physical addresses it must live in the range |
| 96 | * 0 - TASK_SIZE, as only the user space mappings are arbitrarily |
| 97 | * modifiable. |
| 98 | * |
| 99 | * The assembly stub in the control code buffer is passed a linked list |
| 100 | * of descriptor pages detailing the source pages of the new kernel, |
| 101 | * and the destination addresses of those source pages. As this data |
| 102 | * structure is not used in the context of the current OS, it must |
| 103 | * be self-contained. |
| 104 | * |
| 105 | * The code has been made to work with highmem pages and will use a |
| 106 | * destination page in its final resting place (if it happens |
| 107 | * to allocate it). The end product of this is that most of the |
| 108 | * physical address space, and most of RAM can be used. |
| 109 | * |
| 110 | * Future directions include: |
| 111 | * - allocating a page table with the control code buffer identity |
| 112 | * mapped, to simplify machine_kexec and make kexec_on_panic more |
| 113 | * reliable. |
| 114 | */ |
| 115 | |
| 116 | /* |
| 117 | * KIMAGE_NO_DEST is an impossible destination address..., for |
| 118 | * allocating pages whose destination address we do not care about. |
| 119 | */ |
| 120 | #define KIMAGE_NO_DEST (-1UL) |
| 121 | |
Maneesh Soni | 72414d3 | 2005-06-25 14:58:28 -0700 | [diff] [blame] | 122 | static int kimage_is_destination_range(struct kimage *image, |
| 123 | unsigned long start, unsigned long end); |
| 124 | static struct page *kimage_alloc_page(struct kimage *image, |
Al Viro | 9796fdd | 2005-10-21 03:22:03 -0400 | [diff] [blame] | 125 | gfp_t gfp_mask, |
Maneesh Soni | 72414d3 | 2005-06-25 14:58:28 -0700 | [diff] [blame] | 126 | unsigned long dest); |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 127 | |
Vivek Goyal | dabe786 | 2014-08-08 14:25:45 -0700 | [diff] [blame] | 128 | static int copy_user_segment_list(struct kimage *image, |
| 129 | unsigned long nr_segments, |
| 130 | struct kexec_segment __user *segments) |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 131 | { |
Vivek Goyal | dabe786 | 2014-08-08 14:25:45 -0700 | [diff] [blame] | 132 | int ret; |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 133 | size_t segment_bytes; |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 134 | |
| 135 | /* Read in the segments */ |
| 136 | image->nr_segments = nr_segments; |
| 137 | segment_bytes = nr_segments * sizeof(*segments); |
Vivek Goyal | dabe786 | 2014-08-08 14:25:45 -0700 | [diff] [blame] | 138 | ret = copy_from_user(image->segment, segments, segment_bytes); |
| 139 | if (ret) |
| 140 | ret = -EFAULT; |
| 141 | |
| 142 | return ret; |
| 143 | } |
| 144 | |
| 145 | static int sanity_check_segment_list(struct kimage *image) |
| 146 | { |
| 147 | int result, i; |
| 148 | unsigned long nr_segments = image->nr_segments; |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 149 | |
| 150 | /* |
| 151 | * Verify we have good destination addresses. The caller is |
| 152 | * responsible for making certain we don't attempt to load |
| 153 | * the new image into invalid or reserved areas of RAM. This |
| 154 | * just verifies it is an address we can use. |
| 155 | * |
| 156 | * Since the kernel does everything in page size chunks ensure |
Uwe Kleine-König | b595076 | 2010-11-01 15:38:34 -0400 | [diff] [blame] | 157 | * the destination addresses are page aligned. Too many |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 158 | * special cases crop of when we don't do this. The most |
| 159 | * insidious is getting overlapping destination addresses |
| 160 | * simply because addresses are changed to page size |
| 161 | * granularity. |
| 162 | */ |
| 163 | result = -EADDRNOTAVAIL; |
| 164 | for (i = 0; i < nr_segments; i++) { |
| 165 | unsigned long mstart, mend; |
Maneesh Soni | 72414d3 | 2005-06-25 14:58:28 -0700 | [diff] [blame] | 166 | |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 167 | mstart = image->segment[i].mem; |
| 168 | mend = mstart + image->segment[i].memsz; |
| 169 | if ((mstart & ~PAGE_MASK) || (mend & ~PAGE_MASK)) |
Vivek Goyal | dabe786 | 2014-08-08 14:25:45 -0700 | [diff] [blame] | 170 | return result; |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 171 | if (mend >= KEXEC_DESTINATION_MEMORY_LIMIT) |
Vivek Goyal | dabe786 | 2014-08-08 14:25:45 -0700 | [diff] [blame] | 172 | return result; |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | /* Verify our destination addresses do not overlap. |
| 176 | * If we alloed overlapping destination addresses |
| 177 | * through very weird things can happen with no |
| 178 | * easy explanation as one segment stops on another. |
| 179 | */ |
| 180 | result = -EINVAL; |
Maneesh Soni | 72414d3 | 2005-06-25 14:58:28 -0700 | [diff] [blame] | 181 | for (i = 0; i < nr_segments; i++) { |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 182 | unsigned long mstart, mend; |
| 183 | unsigned long j; |
Maneesh Soni | 72414d3 | 2005-06-25 14:58:28 -0700 | [diff] [blame] | 184 | |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 185 | mstart = image->segment[i].mem; |
| 186 | mend = mstart + image->segment[i].memsz; |
Maneesh Soni | 72414d3 | 2005-06-25 14:58:28 -0700 | [diff] [blame] | 187 | for (j = 0; j < i; j++) { |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 188 | unsigned long pstart, pend; |
| 189 | pstart = image->segment[j].mem; |
| 190 | pend = pstart + image->segment[j].memsz; |
| 191 | /* Do the segments overlap ? */ |
| 192 | if ((mend > pstart) && (mstart < pend)) |
Vivek Goyal | dabe786 | 2014-08-08 14:25:45 -0700 | [diff] [blame] | 193 | return result; |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 194 | } |
| 195 | } |
| 196 | |
| 197 | /* Ensure our buffer sizes are strictly less than |
| 198 | * our memory sizes. This should always be the case, |
| 199 | * and it is easier to check up front than to be surprised |
| 200 | * later on. |
| 201 | */ |
| 202 | result = -EINVAL; |
Maneesh Soni | 72414d3 | 2005-06-25 14:58:28 -0700 | [diff] [blame] | 203 | for (i = 0; i < nr_segments; i++) { |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 204 | if (image->segment[i].bufsz > image->segment[i].memsz) |
Vivek Goyal | dabe786 | 2014-08-08 14:25:45 -0700 | [diff] [blame] | 205 | return result; |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 206 | } |
| 207 | |
Vivek Goyal | dabe786 | 2014-08-08 14:25:45 -0700 | [diff] [blame] | 208 | /* |
| 209 | * Verify we have good destination addresses. Normally |
| 210 | * the caller is responsible for making certain we don't |
| 211 | * attempt to load the new image into invalid or reserved |
| 212 | * areas of RAM. But crash kernels are preloaded into a |
| 213 | * reserved area of ram. We must ensure the addresses |
| 214 | * are in the reserved area otherwise preloading the |
| 215 | * kernel could corrupt things. |
| 216 | */ |
Maneesh Soni | 72414d3 | 2005-06-25 14:58:28 -0700 | [diff] [blame] | 217 | |
Vivek Goyal | dabe786 | 2014-08-08 14:25:45 -0700 | [diff] [blame] | 218 | if (image->type == KEXEC_TYPE_CRASH) { |
| 219 | result = -EADDRNOTAVAIL; |
| 220 | for (i = 0; i < nr_segments; i++) { |
| 221 | unsigned long mstart, mend; |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 222 | |
Vivek Goyal | dabe786 | 2014-08-08 14:25:45 -0700 | [diff] [blame] | 223 | mstart = image->segment[i].mem; |
| 224 | mend = mstart + image->segment[i].memsz - 1; |
| 225 | /* Ensure we are within the crash kernel limits */ |
| 226 | if ((mstart < crashk_res.start) || |
| 227 | (mend > crashk_res.end)) |
| 228 | return result; |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | return 0; |
| 233 | } |
| 234 | |
| 235 | static struct kimage *do_kimage_alloc_init(void) |
| 236 | { |
| 237 | struct kimage *image; |
| 238 | |
| 239 | /* Allocate a controlling structure */ |
| 240 | image = kzalloc(sizeof(*image), GFP_KERNEL); |
| 241 | if (!image) |
| 242 | return NULL; |
| 243 | |
| 244 | image->head = 0; |
| 245 | image->entry = &image->head; |
| 246 | image->last_entry = &image->head; |
| 247 | image->control_page = ~0; /* By default this does not apply */ |
| 248 | image->type = KEXEC_TYPE_DEFAULT; |
| 249 | |
| 250 | /* Initialize the list of control pages */ |
| 251 | INIT_LIST_HEAD(&image->control_pages); |
| 252 | |
| 253 | /* Initialize the list of destination pages */ |
| 254 | INIT_LIST_HEAD(&image->dest_pages); |
| 255 | |
| 256 | /* Initialize the list of unusable pages */ |
| 257 | INIT_LIST_HEAD(&image->unusable_pages); |
| 258 | |
| 259 | return image; |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 260 | } |
| 261 | |
Zhang Yanfei | b92e7e0 | 2013-02-27 17:03:29 -0800 | [diff] [blame] | 262 | static void kimage_free_page_list(struct list_head *list); |
| 263 | |
Vivek Goyal | 255aedd | 2014-08-08 14:25:48 -0700 | [diff] [blame^] | 264 | static int kimage_alloc_init(struct kimage **rimage, unsigned long entry, |
| 265 | unsigned long nr_segments, |
| 266 | struct kexec_segment __user *segments, |
| 267 | unsigned long flags) |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 268 | { |
Vivek Goyal | 255aedd | 2014-08-08 14:25:48 -0700 | [diff] [blame^] | 269 | int ret; |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 270 | struct kimage *image; |
Vivek Goyal | 255aedd | 2014-08-08 14:25:48 -0700 | [diff] [blame^] | 271 | bool kexec_on_panic = flags & KEXEC_ON_CRASH; |
| 272 | |
| 273 | if (kexec_on_panic) { |
| 274 | /* Verify we have a valid entry point */ |
| 275 | if ((entry < crashk_res.start) || (entry > crashk_res.end)) |
| 276 | return -EADDRNOTAVAIL; |
| 277 | } |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 278 | |
| 279 | /* Allocate and initialize a controlling structure */ |
Vivek Goyal | dabe786 | 2014-08-08 14:25:45 -0700 | [diff] [blame] | 280 | image = do_kimage_alloc_init(); |
| 281 | if (!image) |
| 282 | return -ENOMEM; |
| 283 | |
| 284 | image->start = entry; |
| 285 | |
Vivek Goyal | 255aedd | 2014-08-08 14:25:48 -0700 | [diff] [blame^] | 286 | ret = copy_user_segment_list(image, nr_segments, segments); |
| 287 | if (ret) |
Vivek Goyal | dabe786 | 2014-08-08 14:25:45 -0700 | [diff] [blame] | 288 | goto out_free_image; |
| 289 | |
Vivek Goyal | 255aedd | 2014-08-08 14:25:48 -0700 | [diff] [blame^] | 290 | ret = sanity_check_segment_list(image); |
| 291 | if (ret) |
Vivek Goyal | dabe786 | 2014-08-08 14:25:45 -0700 | [diff] [blame] | 292 | goto out_free_image; |
Maneesh Soni | 72414d3 | 2005-06-25 14:58:28 -0700 | [diff] [blame] | 293 | |
Vivek Goyal | 255aedd | 2014-08-08 14:25:48 -0700 | [diff] [blame^] | 294 | /* Enable the special crash kernel control page allocation policy. */ |
| 295 | if (kexec_on_panic) { |
| 296 | image->control_page = crashk_res.start; |
| 297 | image->type = KEXEC_TYPE_CRASH; |
| 298 | } |
| 299 | |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 300 | /* |
| 301 | * Find a location for the control code buffer, and add it |
| 302 | * the vector of segments so that it's pages will also be |
| 303 | * counted as destination pages. |
| 304 | */ |
Vivek Goyal | 255aedd | 2014-08-08 14:25:48 -0700 | [diff] [blame^] | 305 | ret = -ENOMEM; |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 306 | image->control_code_page = kimage_alloc_control_pages(image, |
Huang Ying | 163f687 | 2008-08-15 00:40:22 -0700 | [diff] [blame] | 307 | get_order(KEXEC_CONTROL_PAGE_SIZE)); |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 308 | if (!image->control_code_page) { |
Fabian Frederick | e1bebcf | 2014-06-06 14:37:09 -0700 | [diff] [blame] | 309 | pr_err("Could not allocate control_code_buffer\n"); |
Vivek Goyal | dabe786 | 2014-08-08 14:25:45 -0700 | [diff] [blame] | 310 | goto out_free_image; |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 311 | } |
| 312 | |
Vivek Goyal | 255aedd | 2014-08-08 14:25:48 -0700 | [diff] [blame^] | 313 | if (!kexec_on_panic) { |
| 314 | image->swap_page = kimage_alloc_control_pages(image, 0); |
| 315 | if (!image->swap_page) { |
| 316 | pr_err("Could not allocate swap buffer\n"); |
| 317 | goto out_free_control_pages; |
| 318 | } |
Huang Ying | 3ab8352 | 2008-07-25 19:45:07 -0700 | [diff] [blame] | 319 | } |
| 320 | |
Zhang Yanfei | b92e7e0 | 2013-02-27 17:03:29 -0800 | [diff] [blame] | 321 | *rimage = image; |
| 322 | return 0; |
Vivek Goyal | dabe786 | 2014-08-08 14:25:45 -0700 | [diff] [blame] | 323 | out_free_control_pages: |
Zhang Yanfei | b92e7e0 | 2013-02-27 17:03:29 -0800 | [diff] [blame] | 324 | kimage_free_page_list(&image->control_pages); |
Vivek Goyal | dabe786 | 2014-08-08 14:25:45 -0700 | [diff] [blame] | 325 | out_free_image: |
Zhang Yanfei | b92e7e0 | 2013-02-27 17:03:29 -0800 | [diff] [blame] | 326 | kfree(image); |
Vivek Goyal | 255aedd | 2014-08-08 14:25:48 -0700 | [diff] [blame^] | 327 | return ret; |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 328 | } |
| 329 | |
Maneesh Soni | 72414d3 | 2005-06-25 14:58:28 -0700 | [diff] [blame] | 330 | static int kimage_is_destination_range(struct kimage *image, |
| 331 | unsigned long start, |
| 332 | unsigned long end) |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 333 | { |
| 334 | unsigned long i; |
| 335 | |
| 336 | for (i = 0; i < image->nr_segments; i++) { |
| 337 | unsigned long mstart, mend; |
Maneesh Soni | 72414d3 | 2005-06-25 14:58:28 -0700 | [diff] [blame] | 338 | |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 339 | mstart = image->segment[i].mem; |
Maneesh Soni | 72414d3 | 2005-06-25 14:58:28 -0700 | [diff] [blame] | 340 | mend = mstart + image->segment[i].memsz; |
| 341 | if ((end > mstart) && (start < mend)) |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 342 | return 1; |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 343 | } |
Maneesh Soni | 72414d3 | 2005-06-25 14:58:28 -0700 | [diff] [blame] | 344 | |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 345 | return 0; |
| 346 | } |
| 347 | |
Al Viro | 9796fdd | 2005-10-21 03:22:03 -0400 | [diff] [blame] | 348 | static struct page *kimage_alloc_pages(gfp_t gfp_mask, unsigned int order) |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 349 | { |
| 350 | struct page *pages; |
Maneesh Soni | 72414d3 | 2005-06-25 14:58:28 -0700 | [diff] [blame] | 351 | |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 352 | pages = alloc_pages(gfp_mask, order); |
| 353 | if (pages) { |
| 354 | unsigned int count, i; |
| 355 | pages->mapping = NULL; |
Hugh Dickins | 4c21e2f | 2005-10-29 18:16:40 -0700 | [diff] [blame] | 356 | set_page_private(pages, order); |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 357 | count = 1 << order; |
Maneesh Soni | 72414d3 | 2005-06-25 14:58:28 -0700 | [diff] [blame] | 358 | for (i = 0; i < count; i++) |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 359 | SetPageReserved(pages + i); |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 360 | } |
Maneesh Soni | 72414d3 | 2005-06-25 14:58:28 -0700 | [diff] [blame] | 361 | |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 362 | return pages; |
| 363 | } |
| 364 | |
| 365 | static void kimage_free_pages(struct page *page) |
| 366 | { |
| 367 | unsigned int order, count, i; |
Maneesh Soni | 72414d3 | 2005-06-25 14:58:28 -0700 | [diff] [blame] | 368 | |
Hugh Dickins | 4c21e2f | 2005-10-29 18:16:40 -0700 | [diff] [blame] | 369 | order = page_private(page); |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 370 | count = 1 << order; |
Maneesh Soni | 72414d3 | 2005-06-25 14:58:28 -0700 | [diff] [blame] | 371 | for (i = 0; i < count; i++) |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 372 | ClearPageReserved(page + i); |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 373 | __free_pages(page, order); |
| 374 | } |
| 375 | |
| 376 | static void kimage_free_page_list(struct list_head *list) |
| 377 | { |
| 378 | struct list_head *pos, *next; |
Maneesh Soni | 72414d3 | 2005-06-25 14:58:28 -0700 | [diff] [blame] | 379 | |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 380 | list_for_each_safe(pos, next, list) { |
| 381 | struct page *page; |
| 382 | |
| 383 | page = list_entry(pos, struct page, lru); |
| 384 | list_del(&page->lru); |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 385 | kimage_free_pages(page); |
| 386 | } |
| 387 | } |
| 388 | |
Maneesh Soni | 72414d3 | 2005-06-25 14:58:28 -0700 | [diff] [blame] | 389 | static struct page *kimage_alloc_normal_control_pages(struct kimage *image, |
| 390 | unsigned int order) |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 391 | { |
| 392 | /* Control pages are special, they are the intermediaries |
| 393 | * that are needed while we copy the rest of the pages |
| 394 | * to their final resting place. As such they must |
| 395 | * not conflict with either the destination addresses |
| 396 | * or memory the kernel is already using. |
| 397 | * |
| 398 | * The only case where we really need more than one of |
| 399 | * these are for architectures where we cannot disable |
| 400 | * the MMU and must instead generate an identity mapped |
| 401 | * page table for all of the memory. |
| 402 | * |
| 403 | * At worst this runs in O(N) of the image size. |
| 404 | */ |
| 405 | struct list_head extra_pages; |
| 406 | struct page *pages; |
| 407 | unsigned int count; |
| 408 | |
| 409 | count = 1 << order; |
| 410 | INIT_LIST_HEAD(&extra_pages); |
| 411 | |
| 412 | /* Loop while I can allocate a page and the page allocated |
| 413 | * is a destination page. |
| 414 | */ |
| 415 | do { |
| 416 | unsigned long pfn, epfn, addr, eaddr; |
Maneesh Soni | 72414d3 | 2005-06-25 14:58:28 -0700 | [diff] [blame] | 417 | |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 418 | pages = kimage_alloc_pages(GFP_KERNEL, order); |
| 419 | if (!pages) |
| 420 | break; |
| 421 | pfn = page_to_pfn(pages); |
| 422 | epfn = pfn + count; |
| 423 | addr = pfn << PAGE_SHIFT; |
| 424 | eaddr = epfn << PAGE_SHIFT; |
| 425 | if ((epfn >= (KEXEC_CONTROL_MEMORY_LIMIT >> PAGE_SHIFT)) || |
Maneesh Soni | 72414d3 | 2005-06-25 14:58:28 -0700 | [diff] [blame] | 426 | kimage_is_destination_range(image, addr, eaddr)) { |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 427 | list_add(&pages->lru, &extra_pages); |
| 428 | pages = NULL; |
| 429 | } |
Maneesh Soni | 72414d3 | 2005-06-25 14:58:28 -0700 | [diff] [blame] | 430 | } while (!pages); |
| 431 | |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 432 | if (pages) { |
| 433 | /* Remember the allocated page... */ |
| 434 | list_add(&pages->lru, &image->control_pages); |
| 435 | |
| 436 | /* Because the page is already in it's destination |
| 437 | * location we will never allocate another page at |
| 438 | * that address. Therefore kimage_alloc_pages |
| 439 | * will not return it (again) and we don't need |
| 440 | * to give it an entry in image->segment[]. |
| 441 | */ |
| 442 | } |
| 443 | /* Deal with the destination pages I have inadvertently allocated. |
| 444 | * |
| 445 | * Ideally I would convert multi-page allocations into single |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 446 | * page allocations, and add everything to image->dest_pages. |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 447 | * |
| 448 | * For now it is simpler to just free the pages. |
| 449 | */ |
| 450 | kimage_free_page_list(&extra_pages); |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 451 | |
Maneesh Soni | 72414d3 | 2005-06-25 14:58:28 -0700 | [diff] [blame] | 452 | return pages; |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 453 | } |
| 454 | |
Maneesh Soni | 72414d3 | 2005-06-25 14:58:28 -0700 | [diff] [blame] | 455 | static struct page *kimage_alloc_crash_control_pages(struct kimage *image, |
| 456 | unsigned int order) |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 457 | { |
| 458 | /* Control pages are special, they are the intermediaries |
| 459 | * that are needed while we copy the rest of the pages |
| 460 | * to their final resting place. As such they must |
| 461 | * not conflict with either the destination addresses |
| 462 | * or memory the kernel is already using. |
| 463 | * |
| 464 | * Control pages are also the only pags we must allocate |
| 465 | * when loading a crash kernel. All of the other pages |
| 466 | * are specified by the segments and we just memcpy |
| 467 | * into them directly. |
| 468 | * |
| 469 | * The only case where we really need more than one of |
| 470 | * these are for architectures where we cannot disable |
| 471 | * the MMU and must instead generate an identity mapped |
| 472 | * page table for all of the memory. |
| 473 | * |
| 474 | * Given the low demand this implements a very simple |
| 475 | * allocator that finds the first hole of the appropriate |
| 476 | * size in the reserved memory region, and allocates all |
| 477 | * of the memory up to and including the hole. |
| 478 | */ |
| 479 | unsigned long hole_start, hole_end, size; |
| 480 | struct page *pages; |
Maneesh Soni | 72414d3 | 2005-06-25 14:58:28 -0700 | [diff] [blame] | 481 | |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 482 | pages = NULL; |
| 483 | size = (1 << order) << PAGE_SHIFT; |
| 484 | hole_start = (image->control_page + (size - 1)) & ~(size - 1); |
| 485 | hole_end = hole_start + size - 1; |
Maneesh Soni | 72414d3 | 2005-06-25 14:58:28 -0700 | [diff] [blame] | 486 | while (hole_end <= crashk_res.end) { |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 487 | unsigned long i; |
Maneesh Soni | 72414d3 | 2005-06-25 14:58:28 -0700 | [diff] [blame] | 488 | |
Michael Holzheu | 3d214fa | 2011-10-30 15:16:36 +0100 | [diff] [blame] | 489 | if (hole_end > KEXEC_CRASH_CONTROL_MEMORY_LIMIT) |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 490 | break; |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 491 | /* See if I overlap any of the segments */ |
Maneesh Soni | 72414d3 | 2005-06-25 14:58:28 -0700 | [diff] [blame] | 492 | for (i = 0; i < image->nr_segments; i++) { |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 493 | unsigned long mstart, mend; |
Maneesh Soni | 72414d3 | 2005-06-25 14:58:28 -0700 | [diff] [blame] | 494 | |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 495 | mstart = image->segment[i].mem; |
| 496 | mend = mstart + image->segment[i].memsz - 1; |
| 497 | if ((hole_end >= mstart) && (hole_start <= mend)) { |
| 498 | /* Advance the hole to the end of the segment */ |
| 499 | hole_start = (mend + (size - 1)) & ~(size - 1); |
| 500 | hole_end = hole_start + size - 1; |
| 501 | break; |
| 502 | } |
| 503 | } |
| 504 | /* If I don't overlap any segments I have found my hole! */ |
| 505 | if (i == image->nr_segments) { |
| 506 | pages = pfn_to_page(hole_start >> PAGE_SHIFT); |
| 507 | break; |
| 508 | } |
| 509 | } |
Maneesh Soni | 72414d3 | 2005-06-25 14:58:28 -0700 | [diff] [blame] | 510 | if (pages) |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 511 | image->control_page = hole_end; |
Maneesh Soni | 72414d3 | 2005-06-25 14:58:28 -0700 | [diff] [blame] | 512 | |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 513 | return pages; |
| 514 | } |
| 515 | |
| 516 | |
Maneesh Soni | 72414d3 | 2005-06-25 14:58:28 -0700 | [diff] [blame] | 517 | struct page *kimage_alloc_control_pages(struct kimage *image, |
| 518 | unsigned int order) |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 519 | { |
| 520 | struct page *pages = NULL; |
Maneesh Soni | 72414d3 | 2005-06-25 14:58:28 -0700 | [diff] [blame] | 521 | |
| 522 | switch (image->type) { |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 523 | case KEXEC_TYPE_DEFAULT: |
| 524 | pages = kimage_alloc_normal_control_pages(image, order); |
| 525 | break; |
| 526 | case KEXEC_TYPE_CRASH: |
| 527 | pages = kimage_alloc_crash_control_pages(image, order); |
| 528 | break; |
| 529 | } |
Maneesh Soni | 72414d3 | 2005-06-25 14:58:28 -0700 | [diff] [blame] | 530 | |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 531 | return pages; |
| 532 | } |
| 533 | |
| 534 | static int kimage_add_entry(struct kimage *image, kimage_entry_t entry) |
| 535 | { |
Maneesh Soni | 72414d3 | 2005-06-25 14:58:28 -0700 | [diff] [blame] | 536 | if (*image->entry != 0) |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 537 | image->entry++; |
Maneesh Soni | 72414d3 | 2005-06-25 14:58:28 -0700 | [diff] [blame] | 538 | |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 539 | if (image->entry == image->last_entry) { |
| 540 | kimage_entry_t *ind_page; |
| 541 | struct page *page; |
Maneesh Soni | 72414d3 | 2005-06-25 14:58:28 -0700 | [diff] [blame] | 542 | |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 543 | page = kimage_alloc_page(image, GFP_KERNEL, KIMAGE_NO_DEST); |
Maneesh Soni | 72414d3 | 2005-06-25 14:58:28 -0700 | [diff] [blame] | 544 | if (!page) |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 545 | return -ENOMEM; |
Maneesh Soni | 72414d3 | 2005-06-25 14:58:28 -0700 | [diff] [blame] | 546 | |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 547 | ind_page = page_address(page); |
| 548 | *image->entry = virt_to_phys(ind_page) | IND_INDIRECTION; |
| 549 | image->entry = ind_page; |
Maneesh Soni | 72414d3 | 2005-06-25 14:58:28 -0700 | [diff] [blame] | 550 | image->last_entry = ind_page + |
| 551 | ((PAGE_SIZE/sizeof(kimage_entry_t)) - 1); |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 552 | } |
| 553 | *image->entry = entry; |
| 554 | image->entry++; |
| 555 | *image->entry = 0; |
Maneesh Soni | 72414d3 | 2005-06-25 14:58:28 -0700 | [diff] [blame] | 556 | |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 557 | return 0; |
| 558 | } |
| 559 | |
Maneesh Soni | 72414d3 | 2005-06-25 14:58:28 -0700 | [diff] [blame] | 560 | static int kimage_set_destination(struct kimage *image, |
| 561 | unsigned long destination) |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 562 | { |
| 563 | int result; |
| 564 | |
| 565 | destination &= PAGE_MASK; |
| 566 | result = kimage_add_entry(image, destination | IND_DESTINATION); |
Maneesh Soni | 72414d3 | 2005-06-25 14:58:28 -0700 | [diff] [blame] | 567 | if (result == 0) |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 568 | image->destination = destination; |
Maneesh Soni | 72414d3 | 2005-06-25 14:58:28 -0700 | [diff] [blame] | 569 | |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 570 | return result; |
| 571 | } |
| 572 | |
| 573 | |
| 574 | static int kimage_add_page(struct kimage *image, unsigned long page) |
| 575 | { |
| 576 | int result; |
| 577 | |
| 578 | page &= PAGE_MASK; |
| 579 | result = kimage_add_entry(image, page | IND_SOURCE); |
Maneesh Soni | 72414d3 | 2005-06-25 14:58:28 -0700 | [diff] [blame] | 580 | if (result == 0) |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 581 | image->destination += PAGE_SIZE; |
Maneesh Soni | 72414d3 | 2005-06-25 14:58:28 -0700 | [diff] [blame] | 582 | |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 583 | return result; |
| 584 | } |
| 585 | |
| 586 | |
| 587 | static void kimage_free_extra_pages(struct kimage *image) |
| 588 | { |
| 589 | /* Walk through and free any extra destination pages I may have */ |
| 590 | kimage_free_page_list(&image->dest_pages); |
| 591 | |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 592 | /* Walk through and free any unusable pages I have cached */ |
Vivek Goyal | 7d3e2bc | 2014-08-08 14:25:43 -0700 | [diff] [blame] | 593 | kimage_free_page_list(&image->unusable_pages); |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 594 | |
| 595 | } |
WANG Cong | 7fccf03 | 2008-07-25 19:45:02 -0700 | [diff] [blame] | 596 | static void kimage_terminate(struct kimage *image) |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 597 | { |
Maneesh Soni | 72414d3 | 2005-06-25 14:58:28 -0700 | [diff] [blame] | 598 | if (*image->entry != 0) |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 599 | image->entry++; |
Maneesh Soni | 72414d3 | 2005-06-25 14:58:28 -0700 | [diff] [blame] | 600 | |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 601 | *image->entry = IND_DONE; |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 602 | } |
| 603 | |
| 604 | #define for_each_kimage_entry(image, ptr, entry) \ |
| 605 | for (ptr = &image->head; (entry = *ptr) && !(entry & IND_DONE); \ |
Fabian Frederick | e1bebcf | 2014-06-06 14:37:09 -0700 | [diff] [blame] | 606 | ptr = (entry & IND_INDIRECTION) ? \ |
| 607 | phys_to_virt((entry & PAGE_MASK)) : ptr + 1) |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 608 | |
| 609 | static void kimage_free_entry(kimage_entry_t entry) |
| 610 | { |
| 611 | struct page *page; |
| 612 | |
| 613 | page = pfn_to_page(entry >> PAGE_SHIFT); |
| 614 | kimage_free_pages(page); |
| 615 | } |
| 616 | |
| 617 | static void kimage_free(struct kimage *image) |
| 618 | { |
| 619 | kimage_entry_t *ptr, entry; |
| 620 | kimage_entry_t ind = 0; |
| 621 | |
| 622 | if (!image) |
| 623 | return; |
Maneesh Soni | 72414d3 | 2005-06-25 14:58:28 -0700 | [diff] [blame] | 624 | |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 625 | kimage_free_extra_pages(image); |
| 626 | for_each_kimage_entry(image, ptr, entry) { |
| 627 | if (entry & IND_INDIRECTION) { |
| 628 | /* Free the previous indirection page */ |
Maneesh Soni | 72414d3 | 2005-06-25 14:58:28 -0700 | [diff] [blame] | 629 | if (ind & IND_INDIRECTION) |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 630 | kimage_free_entry(ind); |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 631 | /* Save this indirection page until we are |
| 632 | * done with it. |
| 633 | */ |
| 634 | ind = entry; |
Fabian Frederick | e1bebcf | 2014-06-06 14:37:09 -0700 | [diff] [blame] | 635 | } else if (entry & IND_SOURCE) |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 636 | kimage_free_entry(entry); |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 637 | } |
| 638 | /* Free the final indirection page */ |
Maneesh Soni | 72414d3 | 2005-06-25 14:58:28 -0700 | [diff] [blame] | 639 | if (ind & IND_INDIRECTION) |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 640 | kimage_free_entry(ind); |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 641 | |
| 642 | /* Handle any machine specific cleanup */ |
| 643 | machine_kexec_cleanup(image); |
| 644 | |
| 645 | /* Free the kexec control pages... */ |
| 646 | kimage_free_page_list(&image->control_pages); |
| 647 | kfree(image); |
| 648 | } |
| 649 | |
Maneesh Soni | 72414d3 | 2005-06-25 14:58:28 -0700 | [diff] [blame] | 650 | static kimage_entry_t *kimage_dst_used(struct kimage *image, |
| 651 | unsigned long page) |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 652 | { |
| 653 | kimage_entry_t *ptr, entry; |
| 654 | unsigned long destination = 0; |
| 655 | |
| 656 | for_each_kimage_entry(image, ptr, entry) { |
Maneesh Soni | 72414d3 | 2005-06-25 14:58:28 -0700 | [diff] [blame] | 657 | if (entry & IND_DESTINATION) |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 658 | destination = entry & PAGE_MASK; |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 659 | else if (entry & IND_SOURCE) { |
Maneesh Soni | 72414d3 | 2005-06-25 14:58:28 -0700 | [diff] [blame] | 660 | if (page == destination) |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 661 | return ptr; |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 662 | destination += PAGE_SIZE; |
| 663 | } |
| 664 | } |
Maneesh Soni | 72414d3 | 2005-06-25 14:58:28 -0700 | [diff] [blame] | 665 | |
Alexey Dobriyan | 314b6a4 | 2005-06-27 22:29:33 -0700 | [diff] [blame] | 666 | return NULL; |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 667 | } |
| 668 | |
Maneesh Soni | 72414d3 | 2005-06-25 14:58:28 -0700 | [diff] [blame] | 669 | static struct page *kimage_alloc_page(struct kimage *image, |
Al Viro | 9796fdd | 2005-10-21 03:22:03 -0400 | [diff] [blame] | 670 | gfp_t gfp_mask, |
Maneesh Soni | 72414d3 | 2005-06-25 14:58:28 -0700 | [diff] [blame] | 671 | unsigned long destination) |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 672 | { |
| 673 | /* |
| 674 | * Here we implement safeguards to ensure that a source page |
| 675 | * is not copied to its destination page before the data on |
| 676 | * the destination page is no longer useful. |
| 677 | * |
| 678 | * To do this we maintain the invariant that a source page is |
| 679 | * either its own destination page, or it is not a |
| 680 | * destination page at all. |
| 681 | * |
| 682 | * That is slightly stronger than required, but the proof |
| 683 | * that no problems will not occur is trivial, and the |
| 684 | * implementation is simply to verify. |
| 685 | * |
| 686 | * When allocating all pages normally this algorithm will run |
| 687 | * in O(N) time, but in the worst case it will run in O(N^2) |
| 688 | * time. If the runtime is a problem the data structures can |
| 689 | * be fixed. |
| 690 | */ |
| 691 | struct page *page; |
| 692 | unsigned long addr; |
| 693 | |
| 694 | /* |
| 695 | * Walk through the list of destination pages, and see if I |
| 696 | * have a match. |
| 697 | */ |
| 698 | list_for_each_entry(page, &image->dest_pages, lru) { |
| 699 | addr = page_to_pfn(page) << PAGE_SHIFT; |
| 700 | if (addr == destination) { |
| 701 | list_del(&page->lru); |
| 702 | return page; |
| 703 | } |
| 704 | } |
| 705 | page = NULL; |
| 706 | while (1) { |
| 707 | kimage_entry_t *old; |
| 708 | |
| 709 | /* Allocate a page, if we run out of memory give up */ |
| 710 | page = kimage_alloc_pages(gfp_mask, 0); |
Maneesh Soni | 72414d3 | 2005-06-25 14:58:28 -0700 | [diff] [blame] | 711 | if (!page) |
Alexey Dobriyan | 314b6a4 | 2005-06-27 22:29:33 -0700 | [diff] [blame] | 712 | return NULL; |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 713 | /* If the page cannot be used file it away */ |
Maneesh Soni | 72414d3 | 2005-06-25 14:58:28 -0700 | [diff] [blame] | 714 | if (page_to_pfn(page) > |
| 715 | (KEXEC_SOURCE_MEMORY_LIMIT >> PAGE_SHIFT)) { |
Vivek Goyal | 7d3e2bc | 2014-08-08 14:25:43 -0700 | [diff] [blame] | 716 | list_add(&page->lru, &image->unusable_pages); |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 717 | continue; |
| 718 | } |
| 719 | addr = page_to_pfn(page) << PAGE_SHIFT; |
| 720 | |
| 721 | /* If it is the destination page we want use it */ |
| 722 | if (addr == destination) |
| 723 | break; |
| 724 | |
| 725 | /* If the page is not a destination page use it */ |
Maneesh Soni | 72414d3 | 2005-06-25 14:58:28 -0700 | [diff] [blame] | 726 | if (!kimage_is_destination_range(image, addr, |
| 727 | addr + PAGE_SIZE)) |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 728 | break; |
| 729 | |
| 730 | /* |
| 731 | * I know that the page is someones destination page. |
| 732 | * See if there is already a source page for this |
| 733 | * destination page. And if so swap the source pages. |
| 734 | */ |
| 735 | old = kimage_dst_used(image, addr); |
| 736 | if (old) { |
| 737 | /* If so move it */ |
| 738 | unsigned long old_addr; |
| 739 | struct page *old_page; |
| 740 | |
| 741 | old_addr = *old & PAGE_MASK; |
| 742 | old_page = pfn_to_page(old_addr >> PAGE_SHIFT); |
| 743 | copy_highpage(page, old_page); |
| 744 | *old = addr | (*old & ~PAGE_MASK); |
| 745 | |
| 746 | /* The old page I have found cannot be a |
Jonathan Steel | f9092f3 | 2008-09-22 13:57:45 -0700 | [diff] [blame] | 747 | * destination page, so return it if it's |
| 748 | * gfp_flags honor the ones passed in. |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 749 | */ |
Jonathan Steel | f9092f3 | 2008-09-22 13:57:45 -0700 | [diff] [blame] | 750 | if (!(gfp_mask & __GFP_HIGHMEM) && |
| 751 | PageHighMem(old_page)) { |
| 752 | kimage_free_pages(old_page); |
| 753 | continue; |
| 754 | } |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 755 | addr = old_addr; |
| 756 | page = old_page; |
| 757 | break; |
Fabian Frederick | e1bebcf | 2014-06-06 14:37:09 -0700 | [diff] [blame] | 758 | } else { |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 759 | /* Place the page on the destination list I |
| 760 | * will use it later. |
| 761 | */ |
| 762 | list_add(&page->lru, &image->dest_pages); |
| 763 | } |
| 764 | } |
Maneesh Soni | 72414d3 | 2005-06-25 14:58:28 -0700 | [diff] [blame] | 765 | |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 766 | return page; |
| 767 | } |
| 768 | |
| 769 | static int kimage_load_normal_segment(struct kimage *image, |
Maneesh Soni | 72414d3 | 2005-06-25 14:58:28 -0700 | [diff] [blame] | 770 | struct kexec_segment *segment) |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 771 | { |
| 772 | unsigned long maddr; |
Zhang Yanfei | 310faaa | 2013-04-30 15:28:21 -0700 | [diff] [blame] | 773 | size_t ubytes, mbytes; |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 774 | int result; |
Alexey Dobriyan | 314b6a4 | 2005-06-27 22:29:33 -0700 | [diff] [blame] | 775 | unsigned char __user *buf; |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 776 | |
| 777 | result = 0; |
| 778 | buf = segment->buf; |
| 779 | ubytes = segment->bufsz; |
| 780 | mbytes = segment->memsz; |
| 781 | maddr = segment->mem; |
| 782 | |
| 783 | result = kimage_set_destination(image, maddr); |
Maneesh Soni | 72414d3 | 2005-06-25 14:58:28 -0700 | [diff] [blame] | 784 | if (result < 0) |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 785 | goto out; |
Maneesh Soni | 72414d3 | 2005-06-25 14:58:28 -0700 | [diff] [blame] | 786 | |
| 787 | while (mbytes) { |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 788 | struct page *page; |
| 789 | char *ptr; |
| 790 | size_t uchunk, mchunk; |
Maneesh Soni | 72414d3 | 2005-06-25 14:58:28 -0700 | [diff] [blame] | 791 | |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 792 | page = kimage_alloc_page(image, GFP_HIGHUSER, maddr); |
Stephen Hemminger | c80544d | 2007-10-18 03:07:05 -0700 | [diff] [blame] | 793 | if (!page) { |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 794 | result = -ENOMEM; |
| 795 | goto out; |
| 796 | } |
Maneesh Soni | 72414d3 | 2005-06-25 14:58:28 -0700 | [diff] [blame] | 797 | result = kimage_add_page(image, page_to_pfn(page) |
| 798 | << PAGE_SHIFT); |
| 799 | if (result < 0) |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 800 | goto out; |
Maneesh Soni | 72414d3 | 2005-06-25 14:58:28 -0700 | [diff] [blame] | 801 | |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 802 | ptr = kmap(page); |
| 803 | /* Start with a clear page */ |
Jan Beulich | 3ecb01d | 2010-10-26 14:22:27 -0700 | [diff] [blame] | 804 | clear_page(ptr); |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 805 | ptr += maddr & ~PAGE_MASK; |
Zhang Yanfei | 31c3a3f | 2013-04-30 15:28:23 -0700 | [diff] [blame] | 806 | mchunk = min_t(size_t, mbytes, |
| 807 | PAGE_SIZE - (maddr & ~PAGE_MASK)); |
| 808 | uchunk = min(ubytes, mchunk); |
Maneesh Soni | 72414d3 | 2005-06-25 14:58:28 -0700 | [diff] [blame] | 809 | |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 810 | result = copy_from_user(ptr, buf, uchunk); |
| 811 | kunmap(page); |
| 812 | if (result) { |
Dan Carpenter | f65a03f | 2010-08-10 18:03:31 -0700 | [diff] [blame] | 813 | result = -EFAULT; |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 814 | goto out; |
| 815 | } |
| 816 | ubytes -= uchunk; |
| 817 | maddr += mchunk; |
| 818 | buf += mchunk; |
| 819 | mbytes -= mchunk; |
| 820 | } |
Maneesh Soni | 72414d3 | 2005-06-25 14:58:28 -0700 | [diff] [blame] | 821 | out: |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 822 | return result; |
| 823 | } |
| 824 | |
| 825 | static int kimage_load_crash_segment(struct kimage *image, |
Maneesh Soni | 72414d3 | 2005-06-25 14:58:28 -0700 | [diff] [blame] | 826 | struct kexec_segment *segment) |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 827 | { |
| 828 | /* For crash dumps kernels we simply copy the data from |
| 829 | * user space to it's destination. |
| 830 | * We do things a page at a time for the sake of kmap. |
| 831 | */ |
| 832 | unsigned long maddr; |
Zhang Yanfei | 310faaa | 2013-04-30 15:28:21 -0700 | [diff] [blame] | 833 | size_t ubytes, mbytes; |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 834 | int result; |
Alexey Dobriyan | 314b6a4 | 2005-06-27 22:29:33 -0700 | [diff] [blame] | 835 | unsigned char __user *buf; |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 836 | |
| 837 | result = 0; |
| 838 | buf = segment->buf; |
| 839 | ubytes = segment->bufsz; |
| 840 | mbytes = segment->memsz; |
| 841 | maddr = segment->mem; |
Maneesh Soni | 72414d3 | 2005-06-25 14:58:28 -0700 | [diff] [blame] | 842 | while (mbytes) { |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 843 | struct page *page; |
| 844 | char *ptr; |
| 845 | size_t uchunk, mchunk; |
Maneesh Soni | 72414d3 | 2005-06-25 14:58:28 -0700 | [diff] [blame] | 846 | |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 847 | page = pfn_to_page(maddr >> PAGE_SHIFT); |
Stephen Hemminger | c80544d | 2007-10-18 03:07:05 -0700 | [diff] [blame] | 848 | if (!page) { |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 849 | result = -ENOMEM; |
| 850 | goto out; |
| 851 | } |
| 852 | ptr = kmap(page); |
| 853 | ptr += maddr & ~PAGE_MASK; |
Zhang Yanfei | 31c3a3f | 2013-04-30 15:28:23 -0700 | [diff] [blame] | 854 | mchunk = min_t(size_t, mbytes, |
| 855 | PAGE_SIZE - (maddr & ~PAGE_MASK)); |
| 856 | uchunk = min(ubytes, mchunk); |
| 857 | if (mchunk > uchunk) { |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 858 | /* Zero the trailing part of the page */ |
| 859 | memset(ptr + uchunk, 0, mchunk - uchunk); |
| 860 | } |
| 861 | result = copy_from_user(ptr, buf, uchunk); |
Zou Nan hai | a7956113 | 2006-12-07 09:51:35 -0800 | [diff] [blame] | 862 | kexec_flush_icache_page(page); |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 863 | kunmap(page); |
| 864 | if (result) { |
Dan Carpenter | f65a03f | 2010-08-10 18:03:31 -0700 | [diff] [blame] | 865 | result = -EFAULT; |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 866 | goto out; |
| 867 | } |
| 868 | ubytes -= uchunk; |
| 869 | maddr += mchunk; |
| 870 | buf += mchunk; |
| 871 | mbytes -= mchunk; |
| 872 | } |
Maneesh Soni | 72414d3 | 2005-06-25 14:58:28 -0700 | [diff] [blame] | 873 | out: |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 874 | return result; |
| 875 | } |
| 876 | |
| 877 | static int kimage_load_segment(struct kimage *image, |
Maneesh Soni | 72414d3 | 2005-06-25 14:58:28 -0700 | [diff] [blame] | 878 | struct kexec_segment *segment) |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 879 | { |
| 880 | int result = -ENOMEM; |
Maneesh Soni | 72414d3 | 2005-06-25 14:58:28 -0700 | [diff] [blame] | 881 | |
| 882 | switch (image->type) { |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 883 | case KEXEC_TYPE_DEFAULT: |
| 884 | result = kimage_load_normal_segment(image, segment); |
| 885 | break; |
| 886 | case KEXEC_TYPE_CRASH: |
| 887 | result = kimage_load_crash_segment(image, segment); |
| 888 | break; |
| 889 | } |
Maneesh Soni | 72414d3 | 2005-06-25 14:58:28 -0700 | [diff] [blame] | 890 | |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 891 | return result; |
| 892 | } |
| 893 | |
| 894 | /* |
| 895 | * Exec Kernel system call: for obvious reasons only root may call it. |
| 896 | * |
| 897 | * This call breaks up into three pieces. |
| 898 | * - A generic part which loads the new kernel from the current |
| 899 | * address space, and very carefully places the data in the |
| 900 | * allocated pages. |
| 901 | * |
| 902 | * - A generic part that interacts with the kernel and tells all of |
| 903 | * the devices to shut down. Preventing on-going dmas, and placing |
| 904 | * the devices in a consistent state so a later kernel can |
| 905 | * reinitialize them. |
| 906 | * |
| 907 | * - A machine specific part that includes the syscall number |
Geert Uytterhoeven | 002ace7 | 2013-09-15 11:35:37 +0200 | [diff] [blame] | 908 | * and then copies the image to it's final destination. And |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 909 | * jumps into the image at entry. |
| 910 | * |
| 911 | * kexec does not sync, or unmount filesystems so if you need |
| 912 | * that to happen you need to do that yourself. |
| 913 | */ |
Jeff Moyer | c330dda | 2006-06-23 02:05:07 -0700 | [diff] [blame] | 914 | struct kimage *kexec_image; |
| 915 | struct kimage *kexec_crash_image; |
Kees Cook | 7984754 | 2014-01-23 15:55:59 -0800 | [diff] [blame] | 916 | int kexec_load_disabled; |
Andrew Morton | 8c5a1cf | 2008-08-15 00:40:27 -0700 | [diff] [blame] | 917 | |
| 918 | static DEFINE_MUTEX(kexec_mutex); |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 919 | |
Heiko Carstens | 754fe8d | 2009-01-14 14:14:09 +0100 | [diff] [blame] | 920 | SYSCALL_DEFINE4(kexec_load, unsigned long, entry, unsigned long, nr_segments, |
| 921 | struct kexec_segment __user *, segments, unsigned long, flags) |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 922 | { |
| 923 | struct kimage **dest_image, *image; |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 924 | int result; |
| 925 | |
| 926 | /* We only trust the superuser with rebooting the system. */ |
Kees Cook | 7984754 | 2014-01-23 15:55:59 -0800 | [diff] [blame] | 927 | if (!capable(CAP_SYS_BOOT) || kexec_load_disabled) |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 928 | return -EPERM; |
| 929 | |
| 930 | /* |
| 931 | * Verify we have a legal set of flags |
| 932 | * This leaves us room for future extensions. |
| 933 | */ |
| 934 | if ((flags & KEXEC_FLAGS) != (flags & ~KEXEC_ARCH_MASK)) |
| 935 | return -EINVAL; |
| 936 | |
| 937 | /* Verify we are on the appropriate architecture */ |
| 938 | if (((flags & KEXEC_ARCH_MASK) != KEXEC_ARCH) && |
| 939 | ((flags & KEXEC_ARCH_MASK) != KEXEC_ARCH_DEFAULT)) |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 940 | return -EINVAL; |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 941 | |
| 942 | /* Put an artificial cap on the number |
| 943 | * of segments passed to kexec_load. |
| 944 | */ |
| 945 | if (nr_segments > KEXEC_SEGMENT_MAX) |
| 946 | return -EINVAL; |
| 947 | |
| 948 | image = NULL; |
| 949 | result = 0; |
| 950 | |
| 951 | /* Because we write directly to the reserved memory |
| 952 | * region when loading crash kernels we need a mutex here to |
| 953 | * prevent multiple crash kernels from attempting to load |
| 954 | * simultaneously, and to prevent a crash kernel from loading |
| 955 | * over the top of a in use crash kernel. |
| 956 | * |
| 957 | * KISS: always take the mutex. |
| 958 | */ |
Andrew Morton | 8c5a1cf | 2008-08-15 00:40:27 -0700 | [diff] [blame] | 959 | if (!mutex_trylock(&kexec_mutex)) |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 960 | return -EBUSY; |
Maneesh Soni | 72414d3 | 2005-06-25 14:58:28 -0700 | [diff] [blame] | 961 | |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 962 | dest_image = &kexec_image; |
Maneesh Soni | 72414d3 | 2005-06-25 14:58:28 -0700 | [diff] [blame] | 963 | if (flags & KEXEC_ON_CRASH) |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 964 | dest_image = &kexec_crash_image; |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 965 | if (nr_segments > 0) { |
| 966 | unsigned long i; |
Maneesh Soni | 72414d3 | 2005-06-25 14:58:28 -0700 | [diff] [blame] | 967 | |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 968 | /* Loading another kernel to reboot into */ |
Maneesh Soni | 72414d3 | 2005-06-25 14:58:28 -0700 | [diff] [blame] | 969 | if ((flags & KEXEC_ON_CRASH) == 0) |
Vivek Goyal | 255aedd | 2014-08-08 14:25:48 -0700 | [diff] [blame^] | 970 | result = kimage_alloc_init(&image, entry, nr_segments, |
| 971 | segments, flags); |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 972 | /* Loading another kernel to switch to if this one crashes */ |
| 973 | else if (flags & KEXEC_ON_CRASH) { |
| 974 | /* Free any current crash dump kernel before |
| 975 | * we corrupt it. |
| 976 | */ |
| 977 | kimage_free(xchg(&kexec_crash_image, NULL)); |
Vivek Goyal | 255aedd | 2014-08-08 14:25:48 -0700 | [diff] [blame^] | 978 | result = kimage_alloc_init(&image, entry, nr_segments, |
| 979 | segments, flags); |
Michael Holzheu | 558df72 | 2011-10-30 15:16:43 +0100 | [diff] [blame] | 980 | crash_map_reserved_pages(); |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 981 | } |
Maneesh Soni | 72414d3 | 2005-06-25 14:58:28 -0700 | [diff] [blame] | 982 | if (result) |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 983 | goto out; |
Maneesh Soni | 72414d3 | 2005-06-25 14:58:28 -0700 | [diff] [blame] | 984 | |
Huang Ying | 3ab8352 | 2008-07-25 19:45:07 -0700 | [diff] [blame] | 985 | if (flags & KEXEC_PRESERVE_CONTEXT) |
| 986 | image->preserve_context = 1; |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 987 | result = machine_kexec_prepare(image); |
Maneesh Soni | 72414d3 | 2005-06-25 14:58:28 -0700 | [diff] [blame] | 988 | if (result) |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 989 | goto out; |
Maneesh Soni | 72414d3 | 2005-06-25 14:58:28 -0700 | [diff] [blame] | 990 | |
| 991 | for (i = 0; i < nr_segments; i++) { |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 992 | result = kimage_load_segment(image, &image->segment[i]); |
Maneesh Soni | 72414d3 | 2005-06-25 14:58:28 -0700 | [diff] [blame] | 993 | if (result) |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 994 | goto out; |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 995 | } |
WANG Cong | 7fccf03 | 2008-07-25 19:45:02 -0700 | [diff] [blame] | 996 | kimage_terminate(image); |
Michael Holzheu | 558df72 | 2011-10-30 15:16:43 +0100 | [diff] [blame] | 997 | if (flags & KEXEC_ON_CRASH) |
| 998 | crash_unmap_reserved_pages(); |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 999 | } |
| 1000 | /* Install the new kernel, and Uninstall the old */ |
| 1001 | image = xchg(dest_image, image); |
| 1002 | |
Maneesh Soni | 72414d3 | 2005-06-25 14:58:28 -0700 | [diff] [blame] | 1003 | out: |
Andrew Morton | 8c5a1cf | 2008-08-15 00:40:27 -0700 | [diff] [blame] | 1004 | mutex_unlock(&kexec_mutex); |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 1005 | kimage_free(image); |
Maneesh Soni | 72414d3 | 2005-06-25 14:58:28 -0700 | [diff] [blame] | 1006 | |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 1007 | return result; |
| 1008 | } |
| 1009 | |
Michael Holzheu | 558df72 | 2011-10-30 15:16:43 +0100 | [diff] [blame] | 1010 | /* |
| 1011 | * Add and remove page tables for crashkernel memory |
| 1012 | * |
| 1013 | * Provide an empty default implementation here -- architecture |
| 1014 | * code may override this |
| 1015 | */ |
| 1016 | void __weak crash_map_reserved_pages(void) |
| 1017 | {} |
| 1018 | |
| 1019 | void __weak crash_unmap_reserved_pages(void) |
| 1020 | {} |
| 1021 | |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 1022 | #ifdef CONFIG_COMPAT |
Heiko Carstens | ca2c405 | 2014-03-04 17:13:42 +0100 | [diff] [blame] | 1023 | COMPAT_SYSCALL_DEFINE4(kexec_load, compat_ulong_t, entry, |
| 1024 | compat_ulong_t, nr_segments, |
| 1025 | struct compat_kexec_segment __user *, segments, |
| 1026 | compat_ulong_t, flags) |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 1027 | { |
| 1028 | struct compat_kexec_segment in; |
| 1029 | struct kexec_segment out, __user *ksegments; |
| 1030 | unsigned long i, result; |
| 1031 | |
| 1032 | /* Don't allow clients that don't understand the native |
| 1033 | * architecture to do anything. |
| 1034 | */ |
Maneesh Soni | 72414d3 | 2005-06-25 14:58:28 -0700 | [diff] [blame] | 1035 | if ((flags & KEXEC_ARCH_MASK) == KEXEC_ARCH_DEFAULT) |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 1036 | return -EINVAL; |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 1037 | |
Maneesh Soni | 72414d3 | 2005-06-25 14:58:28 -0700 | [diff] [blame] | 1038 | if (nr_segments > KEXEC_SEGMENT_MAX) |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 1039 | return -EINVAL; |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 1040 | |
| 1041 | ksegments = compat_alloc_user_space(nr_segments * sizeof(out)); |
Fabian Frederick | e1bebcf | 2014-06-06 14:37:09 -0700 | [diff] [blame] | 1042 | for (i = 0; i < nr_segments; i++) { |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 1043 | result = copy_from_user(&in, &segments[i], sizeof(in)); |
Maneesh Soni | 72414d3 | 2005-06-25 14:58:28 -0700 | [diff] [blame] | 1044 | if (result) |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 1045 | return -EFAULT; |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 1046 | |
| 1047 | out.buf = compat_ptr(in.buf); |
| 1048 | out.bufsz = in.bufsz; |
| 1049 | out.mem = in.mem; |
| 1050 | out.memsz = in.memsz; |
| 1051 | |
| 1052 | result = copy_to_user(&ksegments[i], &out, sizeof(out)); |
Maneesh Soni | 72414d3 | 2005-06-25 14:58:28 -0700 | [diff] [blame] | 1053 | if (result) |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 1054 | return -EFAULT; |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 1055 | } |
| 1056 | |
| 1057 | return sys_kexec_load(entry, nr_segments, ksegments, flags); |
| 1058 | } |
| 1059 | #endif |
| 1060 | |
Alexander Nyberg | 6e274d1 | 2005-06-25 14:58:26 -0700 | [diff] [blame] | 1061 | void crash_kexec(struct pt_regs *regs) |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 1062 | { |
Andrew Morton | 8c5a1cf | 2008-08-15 00:40:27 -0700 | [diff] [blame] | 1063 | /* Take the kexec_mutex here to prevent sys_kexec_load |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 1064 | * running on one cpu from replacing the crash kernel |
| 1065 | * we are using after a panic on a different cpu. |
| 1066 | * |
| 1067 | * If the crash kernel was not located in a fixed area |
| 1068 | * of memory the xchg(&kexec_crash_image) would be |
| 1069 | * sufficient. But since I reuse the memory... |
| 1070 | */ |
Andrew Morton | 8c5a1cf | 2008-08-15 00:40:27 -0700 | [diff] [blame] | 1071 | if (mutex_trylock(&kexec_mutex)) { |
David Wilder | c0ce7d0 | 2006-06-23 15:29:34 -0700 | [diff] [blame] | 1072 | if (kexec_crash_image) { |
Vivek Goyal | e996e58 | 2006-01-09 20:51:44 -0800 | [diff] [blame] | 1073 | struct pt_regs fixed_regs; |
KOSAKI Motohiro | 0f4bd46 | 2009-12-22 03:15:43 +0000 | [diff] [blame] | 1074 | |
Vivek Goyal | e996e58 | 2006-01-09 20:51:44 -0800 | [diff] [blame] | 1075 | crash_setup_regs(&fixed_regs, regs); |
Ken'ichi Ohmichi | fd59d23 | 2007-10-16 23:27:27 -0700 | [diff] [blame] | 1076 | crash_save_vmcoreinfo(); |
Vivek Goyal | e996e58 | 2006-01-09 20:51:44 -0800 | [diff] [blame] | 1077 | machine_crash_shutdown(&fixed_regs); |
David Wilder | c0ce7d0 | 2006-06-23 15:29:34 -0700 | [diff] [blame] | 1078 | machine_kexec(kexec_crash_image); |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 1079 | } |
Andrew Morton | 8c5a1cf | 2008-08-15 00:40:27 -0700 | [diff] [blame] | 1080 | mutex_unlock(&kexec_mutex); |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 1081 | } |
| 1082 | } |
Vivek Goyal | cc57165 | 2006-01-09 20:51:41 -0800 | [diff] [blame] | 1083 | |
Amerigo Wang | 06a7f71 | 2009-12-15 16:47:46 -0800 | [diff] [blame] | 1084 | size_t crash_get_memory_size(void) |
| 1085 | { |
Pavan Naregundi | e05bd33 | 2010-06-29 15:05:28 -0700 | [diff] [blame] | 1086 | size_t size = 0; |
Amerigo Wang | 06a7f71 | 2009-12-15 16:47:46 -0800 | [diff] [blame] | 1087 | mutex_lock(&kexec_mutex); |
Pavan Naregundi | e05bd33 | 2010-06-29 15:05:28 -0700 | [diff] [blame] | 1088 | if (crashk_res.end != crashk_res.start) |
Joe Perches | 28f65c11 | 2011-06-09 09:13:32 -0700 | [diff] [blame] | 1089 | size = resource_size(&crashk_res); |
Amerigo Wang | 06a7f71 | 2009-12-15 16:47:46 -0800 | [diff] [blame] | 1090 | mutex_unlock(&kexec_mutex); |
| 1091 | return size; |
| 1092 | } |
| 1093 | |
Anton Blanchard | c0bb9e4 | 2010-08-25 10:22:58 +1000 | [diff] [blame] | 1094 | void __weak crash_free_reserved_phys_range(unsigned long begin, |
| 1095 | unsigned long end) |
Amerigo Wang | 06a7f71 | 2009-12-15 16:47:46 -0800 | [diff] [blame] | 1096 | { |
| 1097 | unsigned long addr; |
| 1098 | |
Jiang Liu | e07cee2 | 2013-04-29 15:06:58 -0700 | [diff] [blame] | 1099 | for (addr = begin; addr < end; addr += PAGE_SIZE) |
| 1100 | free_reserved_page(pfn_to_page(addr >> PAGE_SHIFT)); |
Amerigo Wang | 06a7f71 | 2009-12-15 16:47:46 -0800 | [diff] [blame] | 1101 | } |
| 1102 | |
| 1103 | int crash_shrink_memory(unsigned long new_size) |
| 1104 | { |
| 1105 | int ret = 0; |
| 1106 | unsigned long start, end; |
Michael Holzheu | bec013c | 2012-01-12 17:20:15 -0800 | [diff] [blame] | 1107 | unsigned long old_size; |
Michael Holzheu | 6480e5a | 2012-01-12 17:20:14 -0800 | [diff] [blame] | 1108 | struct resource *ram_res; |
Amerigo Wang | 06a7f71 | 2009-12-15 16:47:46 -0800 | [diff] [blame] | 1109 | |
| 1110 | mutex_lock(&kexec_mutex); |
| 1111 | |
| 1112 | if (kexec_crash_image) { |
| 1113 | ret = -ENOENT; |
| 1114 | goto unlock; |
| 1115 | } |
| 1116 | start = crashk_res.start; |
| 1117 | end = crashk_res.end; |
Michael Holzheu | bec013c | 2012-01-12 17:20:15 -0800 | [diff] [blame] | 1118 | old_size = (end == 0) ? 0 : end - start + 1; |
| 1119 | if (new_size >= old_size) { |
| 1120 | ret = (new_size == old_size) ? 0 : -EINVAL; |
Amerigo Wang | 06a7f71 | 2009-12-15 16:47:46 -0800 | [diff] [blame] | 1121 | goto unlock; |
| 1122 | } |
| 1123 | |
Michael Holzheu | 6480e5a | 2012-01-12 17:20:14 -0800 | [diff] [blame] | 1124 | ram_res = kzalloc(sizeof(*ram_res), GFP_KERNEL); |
| 1125 | if (!ram_res) { |
| 1126 | ret = -ENOMEM; |
| 1127 | goto unlock; |
| 1128 | } |
| 1129 | |
Michael Holzheu | 558df72 | 2011-10-30 15:16:43 +0100 | [diff] [blame] | 1130 | start = roundup(start, KEXEC_CRASH_MEM_ALIGN); |
| 1131 | end = roundup(start + new_size, KEXEC_CRASH_MEM_ALIGN); |
Amerigo Wang | 06a7f71 | 2009-12-15 16:47:46 -0800 | [diff] [blame] | 1132 | |
Michael Holzheu | 558df72 | 2011-10-30 15:16:43 +0100 | [diff] [blame] | 1133 | crash_map_reserved_pages(); |
Anton Blanchard | c0bb9e4 | 2010-08-25 10:22:58 +1000 | [diff] [blame] | 1134 | crash_free_reserved_phys_range(end, crashk_res.end); |
Amerigo Wang | 06a7f71 | 2009-12-15 16:47:46 -0800 | [diff] [blame] | 1135 | |
Pavan Naregundi | e05bd33 | 2010-06-29 15:05:28 -0700 | [diff] [blame] | 1136 | if ((start == end) && (crashk_res.parent != NULL)) |
Amerigo Wang | 06a7f71 | 2009-12-15 16:47:46 -0800 | [diff] [blame] | 1137 | release_resource(&crashk_res); |
Michael Holzheu | 6480e5a | 2012-01-12 17:20:14 -0800 | [diff] [blame] | 1138 | |
| 1139 | ram_res->start = end; |
| 1140 | ram_res->end = crashk_res.end; |
| 1141 | ram_res->flags = IORESOURCE_BUSY | IORESOURCE_MEM; |
| 1142 | ram_res->name = "System RAM"; |
| 1143 | |
Vitaly Mayatskikh | 475f9aa | 2010-05-11 14:06:51 -0700 | [diff] [blame] | 1144 | crashk_res.end = end - 1; |
Michael Holzheu | 6480e5a | 2012-01-12 17:20:14 -0800 | [diff] [blame] | 1145 | |
| 1146 | insert_resource(&iomem_resource, ram_res); |
Michael Holzheu | 558df72 | 2011-10-30 15:16:43 +0100 | [diff] [blame] | 1147 | crash_unmap_reserved_pages(); |
Amerigo Wang | 06a7f71 | 2009-12-15 16:47:46 -0800 | [diff] [blame] | 1148 | |
| 1149 | unlock: |
| 1150 | mutex_unlock(&kexec_mutex); |
| 1151 | return ret; |
| 1152 | } |
| 1153 | |
Magnus Damm | 85916f8 | 2006-12-06 20:40:41 -0800 | [diff] [blame] | 1154 | static u32 *append_elf_note(u32 *buf, char *name, unsigned type, void *data, |
| 1155 | size_t data_len) |
| 1156 | { |
| 1157 | struct elf_note note; |
| 1158 | |
| 1159 | note.n_namesz = strlen(name) + 1; |
| 1160 | note.n_descsz = data_len; |
| 1161 | note.n_type = type; |
| 1162 | memcpy(buf, ¬e, sizeof(note)); |
| 1163 | buf += (sizeof(note) + 3)/4; |
| 1164 | memcpy(buf, name, note.n_namesz); |
| 1165 | buf += (note.n_namesz + 3)/4; |
| 1166 | memcpy(buf, data, note.n_descsz); |
| 1167 | buf += (note.n_descsz + 3)/4; |
| 1168 | |
| 1169 | return buf; |
| 1170 | } |
| 1171 | |
| 1172 | static void final_note(u32 *buf) |
| 1173 | { |
| 1174 | struct elf_note note; |
| 1175 | |
| 1176 | note.n_namesz = 0; |
| 1177 | note.n_descsz = 0; |
| 1178 | note.n_type = 0; |
| 1179 | memcpy(buf, ¬e, sizeof(note)); |
| 1180 | } |
| 1181 | |
| 1182 | void crash_save_cpu(struct pt_regs *regs, int cpu) |
| 1183 | { |
| 1184 | struct elf_prstatus prstatus; |
| 1185 | u32 *buf; |
| 1186 | |
Rusty Russell | 4f4b6c1 | 2009-01-01 10:12:15 +1030 | [diff] [blame] | 1187 | if ((cpu < 0) || (cpu >= nr_cpu_ids)) |
Magnus Damm | 85916f8 | 2006-12-06 20:40:41 -0800 | [diff] [blame] | 1188 | return; |
| 1189 | |
| 1190 | /* Using ELF notes here is opportunistic. |
| 1191 | * I need a well defined structure format |
| 1192 | * for the data I pass, and I need tags |
| 1193 | * on the data to indicate what information I have |
| 1194 | * squirrelled away. ELF notes happen to provide |
| 1195 | * all of that, so there is no need to invent something new. |
| 1196 | */ |
Fabian Frederick | e1bebcf | 2014-06-06 14:37:09 -0700 | [diff] [blame] | 1197 | buf = (u32 *)per_cpu_ptr(crash_notes, cpu); |
Magnus Damm | 85916f8 | 2006-12-06 20:40:41 -0800 | [diff] [blame] | 1198 | if (!buf) |
| 1199 | return; |
| 1200 | memset(&prstatus, 0, sizeof(prstatus)); |
| 1201 | prstatus.pr_pid = current->pid; |
Tejun Heo | 6cd61c0 | 2009-02-09 22:17:39 +0900 | [diff] [blame] | 1202 | elf_core_copy_kernel_regs(&prstatus.pr_reg, regs); |
Simon Horman | 6672f76 | 2007-05-08 00:28:22 -0700 | [diff] [blame] | 1203 | buf = append_elf_note(buf, KEXEC_CORE_NOTE_NAME, NT_PRSTATUS, |
Fabian Frederick | e1bebcf | 2014-06-06 14:37:09 -0700 | [diff] [blame] | 1204 | &prstatus, sizeof(prstatus)); |
Magnus Damm | 85916f8 | 2006-12-06 20:40:41 -0800 | [diff] [blame] | 1205 | final_note(buf); |
| 1206 | } |
| 1207 | |
Vivek Goyal | cc57165 | 2006-01-09 20:51:41 -0800 | [diff] [blame] | 1208 | static int __init crash_notes_memory_init(void) |
| 1209 | { |
| 1210 | /* Allocate memory for saving cpu registers. */ |
| 1211 | crash_notes = alloc_percpu(note_buf_t); |
| 1212 | if (!crash_notes) { |
Fabian Frederick | e1bebcf | 2014-06-06 14:37:09 -0700 | [diff] [blame] | 1213 | pr_warn("Kexec: Memory allocation for saving cpu register states failed\n"); |
Vivek Goyal | cc57165 | 2006-01-09 20:51:41 -0800 | [diff] [blame] | 1214 | return -ENOMEM; |
| 1215 | } |
| 1216 | return 0; |
| 1217 | } |
Paul Gortmaker | c96d666 | 2014-04-03 14:48:35 -0700 | [diff] [blame] | 1218 | subsys_initcall(crash_notes_memory_init); |
Ken'ichi Ohmichi | fd59d23 | 2007-10-16 23:27:27 -0700 | [diff] [blame] | 1219 | |
Bernhard Walle | cba63c3 | 2007-10-18 23:40:58 -0700 | [diff] [blame] | 1220 | |
| 1221 | /* |
| 1222 | * parsing the "crashkernel" commandline |
| 1223 | * |
| 1224 | * this code is intended to be called from architecture specific code |
| 1225 | */ |
| 1226 | |
| 1227 | |
| 1228 | /* |
| 1229 | * This function parses command lines in the format |
| 1230 | * |
| 1231 | * crashkernel=ramsize-range:size[,...][@offset] |
| 1232 | * |
| 1233 | * The function returns 0 on success and -EINVAL on failure. |
| 1234 | */ |
Fabian Frederick | e1bebcf | 2014-06-06 14:37:09 -0700 | [diff] [blame] | 1235 | static int __init parse_crashkernel_mem(char *cmdline, |
| 1236 | unsigned long long system_ram, |
| 1237 | unsigned long long *crash_size, |
| 1238 | unsigned long long *crash_base) |
Bernhard Walle | cba63c3 | 2007-10-18 23:40:58 -0700 | [diff] [blame] | 1239 | { |
| 1240 | char *cur = cmdline, *tmp; |
| 1241 | |
| 1242 | /* for each entry of the comma-separated list */ |
| 1243 | do { |
| 1244 | unsigned long long start, end = ULLONG_MAX, size; |
| 1245 | |
| 1246 | /* get the start of the range */ |
| 1247 | start = memparse(cur, &tmp); |
| 1248 | if (cur == tmp) { |
Fabian Frederick | e1bebcf | 2014-06-06 14:37:09 -0700 | [diff] [blame] | 1249 | pr_warn("crashkernel: Memory value expected\n"); |
Bernhard Walle | cba63c3 | 2007-10-18 23:40:58 -0700 | [diff] [blame] | 1250 | return -EINVAL; |
| 1251 | } |
| 1252 | cur = tmp; |
| 1253 | if (*cur != '-') { |
Fabian Frederick | e1bebcf | 2014-06-06 14:37:09 -0700 | [diff] [blame] | 1254 | pr_warn("crashkernel: '-' expected\n"); |
Bernhard Walle | cba63c3 | 2007-10-18 23:40:58 -0700 | [diff] [blame] | 1255 | return -EINVAL; |
| 1256 | } |
| 1257 | cur++; |
| 1258 | |
| 1259 | /* if no ':' is here, than we read the end */ |
| 1260 | if (*cur != ':') { |
| 1261 | end = memparse(cur, &tmp); |
| 1262 | if (cur == tmp) { |
Fabian Frederick | e1bebcf | 2014-06-06 14:37:09 -0700 | [diff] [blame] | 1263 | pr_warn("crashkernel: Memory value expected\n"); |
Bernhard Walle | cba63c3 | 2007-10-18 23:40:58 -0700 | [diff] [blame] | 1264 | return -EINVAL; |
| 1265 | } |
| 1266 | cur = tmp; |
| 1267 | if (end <= start) { |
Fabian Frederick | e1bebcf | 2014-06-06 14:37:09 -0700 | [diff] [blame] | 1268 | pr_warn("crashkernel: end <= start\n"); |
Bernhard Walle | cba63c3 | 2007-10-18 23:40:58 -0700 | [diff] [blame] | 1269 | return -EINVAL; |
| 1270 | } |
| 1271 | } |
| 1272 | |
| 1273 | if (*cur != ':') { |
Fabian Frederick | e1bebcf | 2014-06-06 14:37:09 -0700 | [diff] [blame] | 1274 | pr_warn("crashkernel: ':' expected\n"); |
Bernhard Walle | cba63c3 | 2007-10-18 23:40:58 -0700 | [diff] [blame] | 1275 | return -EINVAL; |
| 1276 | } |
| 1277 | cur++; |
| 1278 | |
| 1279 | size = memparse(cur, &tmp); |
| 1280 | if (cur == tmp) { |
Fabian Frederick | e1bebcf | 2014-06-06 14:37:09 -0700 | [diff] [blame] | 1281 | pr_warn("Memory value expected\n"); |
Bernhard Walle | cba63c3 | 2007-10-18 23:40:58 -0700 | [diff] [blame] | 1282 | return -EINVAL; |
| 1283 | } |
| 1284 | cur = tmp; |
| 1285 | if (size >= system_ram) { |
Fabian Frederick | e1bebcf | 2014-06-06 14:37:09 -0700 | [diff] [blame] | 1286 | pr_warn("crashkernel: invalid size\n"); |
Bernhard Walle | cba63c3 | 2007-10-18 23:40:58 -0700 | [diff] [blame] | 1287 | return -EINVAL; |
| 1288 | } |
| 1289 | |
| 1290 | /* match ? */ |
Michael Ellerman | be089d79 | 2008-05-01 04:34:49 -0700 | [diff] [blame] | 1291 | if (system_ram >= start && system_ram < end) { |
Bernhard Walle | cba63c3 | 2007-10-18 23:40:58 -0700 | [diff] [blame] | 1292 | *crash_size = size; |
| 1293 | break; |
| 1294 | } |
| 1295 | } while (*cur++ == ','); |
| 1296 | |
| 1297 | if (*crash_size > 0) { |
Hidetoshi Seto | 11c7da4 | 2009-07-29 15:02:08 -0700 | [diff] [blame] | 1298 | while (*cur && *cur != ' ' && *cur != '@') |
Bernhard Walle | cba63c3 | 2007-10-18 23:40:58 -0700 | [diff] [blame] | 1299 | cur++; |
| 1300 | if (*cur == '@') { |
| 1301 | cur++; |
| 1302 | *crash_base = memparse(cur, &tmp); |
| 1303 | if (cur == tmp) { |
Fabian Frederick | e1bebcf | 2014-06-06 14:37:09 -0700 | [diff] [blame] | 1304 | pr_warn("Memory value expected after '@'\n"); |
Bernhard Walle | cba63c3 | 2007-10-18 23:40:58 -0700 | [diff] [blame] | 1305 | return -EINVAL; |
| 1306 | } |
| 1307 | } |
| 1308 | } |
| 1309 | |
| 1310 | return 0; |
| 1311 | } |
| 1312 | |
| 1313 | /* |
| 1314 | * That function parses "simple" (old) crashkernel command lines like |
| 1315 | * |
Fabian Frederick | e1bebcf | 2014-06-06 14:37:09 -0700 | [diff] [blame] | 1316 | * crashkernel=size[@offset] |
Bernhard Walle | cba63c3 | 2007-10-18 23:40:58 -0700 | [diff] [blame] | 1317 | * |
| 1318 | * It returns 0 on success and -EINVAL on failure. |
| 1319 | */ |
Fabian Frederick | e1bebcf | 2014-06-06 14:37:09 -0700 | [diff] [blame] | 1320 | static int __init parse_crashkernel_simple(char *cmdline, |
| 1321 | unsigned long long *crash_size, |
| 1322 | unsigned long long *crash_base) |
Bernhard Walle | cba63c3 | 2007-10-18 23:40:58 -0700 | [diff] [blame] | 1323 | { |
| 1324 | char *cur = cmdline; |
| 1325 | |
| 1326 | *crash_size = memparse(cmdline, &cur); |
| 1327 | if (cmdline == cur) { |
Fabian Frederick | e1bebcf | 2014-06-06 14:37:09 -0700 | [diff] [blame] | 1328 | pr_warn("crashkernel: memory value expected\n"); |
Bernhard Walle | cba63c3 | 2007-10-18 23:40:58 -0700 | [diff] [blame] | 1329 | return -EINVAL; |
| 1330 | } |
| 1331 | |
| 1332 | if (*cur == '@') |
| 1333 | *crash_base = memparse(cur+1, &cur); |
Zhenzhong Duan | eaa3be6 | 2012-03-28 14:42:47 -0700 | [diff] [blame] | 1334 | else if (*cur != ' ' && *cur != '\0') { |
Fabian Frederick | e1bebcf | 2014-06-06 14:37:09 -0700 | [diff] [blame] | 1335 | pr_warn("crashkernel: unrecognized char\n"); |
Zhenzhong Duan | eaa3be6 | 2012-03-28 14:42:47 -0700 | [diff] [blame] | 1336 | return -EINVAL; |
| 1337 | } |
Bernhard Walle | cba63c3 | 2007-10-18 23:40:58 -0700 | [diff] [blame] | 1338 | |
| 1339 | return 0; |
| 1340 | } |
| 1341 | |
Yinghai Lu | adbc742 | 2013-04-15 22:23:48 -0700 | [diff] [blame] | 1342 | #define SUFFIX_HIGH 0 |
| 1343 | #define SUFFIX_LOW 1 |
| 1344 | #define SUFFIX_NULL 2 |
| 1345 | static __initdata char *suffix_tbl[] = { |
| 1346 | [SUFFIX_HIGH] = ",high", |
| 1347 | [SUFFIX_LOW] = ",low", |
| 1348 | [SUFFIX_NULL] = NULL, |
| 1349 | }; |
| 1350 | |
Bernhard Walle | cba63c3 | 2007-10-18 23:40:58 -0700 | [diff] [blame] | 1351 | /* |
Yinghai Lu | adbc742 | 2013-04-15 22:23:48 -0700 | [diff] [blame] | 1352 | * That function parses "suffix" crashkernel command lines like |
| 1353 | * |
| 1354 | * crashkernel=size,[high|low] |
| 1355 | * |
| 1356 | * It returns 0 on success and -EINVAL on failure. |
Bernhard Walle | cba63c3 | 2007-10-18 23:40:58 -0700 | [diff] [blame] | 1357 | */ |
Yinghai Lu | adbc742 | 2013-04-15 22:23:48 -0700 | [diff] [blame] | 1358 | static int __init parse_crashkernel_suffix(char *cmdline, |
| 1359 | unsigned long long *crash_size, |
| 1360 | unsigned long long *crash_base, |
| 1361 | const char *suffix) |
| 1362 | { |
| 1363 | char *cur = cmdline; |
| 1364 | |
| 1365 | *crash_size = memparse(cmdline, &cur); |
| 1366 | if (cmdline == cur) { |
| 1367 | pr_warn("crashkernel: memory value expected\n"); |
| 1368 | return -EINVAL; |
| 1369 | } |
| 1370 | |
| 1371 | /* check with suffix */ |
| 1372 | if (strncmp(cur, suffix, strlen(suffix))) { |
| 1373 | pr_warn("crashkernel: unrecognized char\n"); |
| 1374 | return -EINVAL; |
| 1375 | } |
| 1376 | cur += strlen(suffix); |
| 1377 | if (*cur != ' ' && *cur != '\0') { |
| 1378 | pr_warn("crashkernel: unrecognized char\n"); |
| 1379 | return -EINVAL; |
| 1380 | } |
| 1381 | |
| 1382 | return 0; |
| 1383 | } |
| 1384 | |
| 1385 | static __init char *get_last_crashkernel(char *cmdline, |
| 1386 | const char *name, |
| 1387 | const char *suffix) |
| 1388 | { |
| 1389 | char *p = cmdline, *ck_cmdline = NULL; |
| 1390 | |
| 1391 | /* find crashkernel and use the last one if there are more */ |
| 1392 | p = strstr(p, name); |
| 1393 | while (p) { |
| 1394 | char *end_p = strchr(p, ' '); |
| 1395 | char *q; |
| 1396 | |
| 1397 | if (!end_p) |
| 1398 | end_p = p + strlen(p); |
| 1399 | |
| 1400 | if (!suffix) { |
| 1401 | int i; |
| 1402 | |
| 1403 | /* skip the one with any known suffix */ |
| 1404 | for (i = 0; suffix_tbl[i]; i++) { |
| 1405 | q = end_p - strlen(suffix_tbl[i]); |
| 1406 | if (!strncmp(q, suffix_tbl[i], |
| 1407 | strlen(suffix_tbl[i]))) |
| 1408 | goto next; |
| 1409 | } |
| 1410 | ck_cmdline = p; |
| 1411 | } else { |
| 1412 | q = end_p - strlen(suffix); |
| 1413 | if (!strncmp(q, suffix, strlen(suffix))) |
| 1414 | ck_cmdline = p; |
| 1415 | } |
| 1416 | next: |
| 1417 | p = strstr(p+1, name); |
| 1418 | } |
| 1419 | |
| 1420 | if (!ck_cmdline) |
| 1421 | return NULL; |
| 1422 | |
| 1423 | return ck_cmdline; |
| 1424 | } |
| 1425 | |
Yinghai Lu | 0212f91 | 2013-01-24 12:20:11 -0800 | [diff] [blame] | 1426 | static int __init __parse_crashkernel(char *cmdline, |
Bernhard Walle | cba63c3 | 2007-10-18 23:40:58 -0700 | [diff] [blame] | 1427 | unsigned long long system_ram, |
| 1428 | unsigned long long *crash_size, |
Yinghai Lu | 0212f91 | 2013-01-24 12:20:11 -0800 | [diff] [blame] | 1429 | unsigned long long *crash_base, |
Yinghai Lu | adbc742 | 2013-04-15 22:23:48 -0700 | [diff] [blame] | 1430 | const char *name, |
| 1431 | const char *suffix) |
Bernhard Walle | cba63c3 | 2007-10-18 23:40:58 -0700 | [diff] [blame] | 1432 | { |
Bernhard Walle | cba63c3 | 2007-10-18 23:40:58 -0700 | [diff] [blame] | 1433 | char *first_colon, *first_space; |
Yinghai Lu | adbc742 | 2013-04-15 22:23:48 -0700 | [diff] [blame] | 1434 | char *ck_cmdline; |
Bernhard Walle | cba63c3 | 2007-10-18 23:40:58 -0700 | [diff] [blame] | 1435 | |
| 1436 | BUG_ON(!crash_size || !crash_base); |
| 1437 | *crash_size = 0; |
| 1438 | *crash_base = 0; |
| 1439 | |
Yinghai Lu | adbc742 | 2013-04-15 22:23:48 -0700 | [diff] [blame] | 1440 | ck_cmdline = get_last_crashkernel(cmdline, name, suffix); |
Bernhard Walle | cba63c3 | 2007-10-18 23:40:58 -0700 | [diff] [blame] | 1441 | |
| 1442 | if (!ck_cmdline) |
| 1443 | return -EINVAL; |
| 1444 | |
Yinghai Lu | 0212f91 | 2013-01-24 12:20:11 -0800 | [diff] [blame] | 1445 | ck_cmdline += strlen(name); |
Bernhard Walle | cba63c3 | 2007-10-18 23:40:58 -0700 | [diff] [blame] | 1446 | |
Yinghai Lu | adbc742 | 2013-04-15 22:23:48 -0700 | [diff] [blame] | 1447 | if (suffix) |
| 1448 | return parse_crashkernel_suffix(ck_cmdline, crash_size, |
| 1449 | crash_base, suffix); |
Bernhard Walle | cba63c3 | 2007-10-18 23:40:58 -0700 | [diff] [blame] | 1450 | /* |
| 1451 | * if the commandline contains a ':', then that's the extended |
| 1452 | * syntax -- if not, it must be the classic syntax |
| 1453 | */ |
| 1454 | first_colon = strchr(ck_cmdline, ':'); |
| 1455 | first_space = strchr(ck_cmdline, ' '); |
| 1456 | if (first_colon && (!first_space || first_colon < first_space)) |
| 1457 | return parse_crashkernel_mem(ck_cmdline, system_ram, |
| 1458 | crash_size, crash_base); |
Bernhard Walle | cba63c3 | 2007-10-18 23:40:58 -0700 | [diff] [blame] | 1459 | |
Xishi Qiu | 80c74f6 | 2013-09-11 14:24:47 -0700 | [diff] [blame] | 1460 | return parse_crashkernel_simple(ck_cmdline, crash_size, crash_base); |
Bernhard Walle | cba63c3 | 2007-10-18 23:40:58 -0700 | [diff] [blame] | 1461 | } |
| 1462 | |
Yinghai Lu | adbc742 | 2013-04-15 22:23:48 -0700 | [diff] [blame] | 1463 | /* |
| 1464 | * That function is the entry point for command line parsing and should be |
| 1465 | * called from the arch-specific code. |
| 1466 | */ |
Yinghai Lu | 0212f91 | 2013-01-24 12:20:11 -0800 | [diff] [blame] | 1467 | int __init parse_crashkernel(char *cmdline, |
| 1468 | unsigned long long system_ram, |
| 1469 | unsigned long long *crash_size, |
| 1470 | unsigned long long *crash_base) |
| 1471 | { |
| 1472 | return __parse_crashkernel(cmdline, system_ram, crash_size, crash_base, |
Yinghai Lu | adbc742 | 2013-04-15 22:23:48 -0700 | [diff] [blame] | 1473 | "crashkernel=", NULL); |
Yinghai Lu | 0212f91 | 2013-01-24 12:20:11 -0800 | [diff] [blame] | 1474 | } |
| 1475 | |
Yinghai Lu | 55a20ee | 2013-04-15 22:23:47 -0700 | [diff] [blame] | 1476 | int __init parse_crashkernel_high(char *cmdline, |
| 1477 | unsigned long long system_ram, |
| 1478 | unsigned long long *crash_size, |
| 1479 | unsigned long long *crash_base) |
| 1480 | { |
| 1481 | return __parse_crashkernel(cmdline, system_ram, crash_size, crash_base, |
Yinghai Lu | adbc742 | 2013-04-15 22:23:48 -0700 | [diff] [blame] | 1482 | "crashkernel=", suffix_tbl[SUFFIX_HIGH]); |
Yinghai Lu | 55a20ee | 2013-04-15 22:23:47 -0700 | [diff] [blame] | 1483 | } |
| 1484 | |
Yinghai Lu | 0212f91 | 2013-01-24 12:20:11 -0800 | [diff] [blame] | 1485 | int __init parse_crashkernel_low(char *cmdline, |
| 1486 | unsigned long long system_ram, |
| 1487 | unsigned long long *crash_size, |
| 1488 | unsigned long long *crash_base) |
| 1489 | { |
| 1490 | return __parse_crashkernel(cmdline, system_ram, crash_size, crash_base, |
Yinghai Lu | adbc742 | 2013-04-15 22:23:48 -0700 | [diff] [blame] | 1491 | "crashkernel=", suffix_tbl[SUFFIX_LOW]); |
Yinghai Lu | 0212f91 | 2013-01-24 12:20:11 -0800 | [diff] [blame] | 1492 | } |
Bernhard Walle | cba63c3 | 2007-10-18 23:40:58 -0700 | [diff] [blame] | 1493 | |
Michael Holzheu | fa8ff29 | 2011-10-30 15:16:41 +0100 | [diff] [blame] | 1494 | static void update_vmcoreinfo_note(void) |
Ken'ichi Ohmichi | fd59d23 | 2007-10-16 23:27:27 -0700 | [diff] [blame] | 1495 | { |
Michael Holzheu | fa8ff29 | 2011-10-30 15:16:41 +0100 | [diff] [blame] | 1496 | u32 *buf = vmcoreinfo_note; |
Ken'ichi Ohmichi | fd59d23 | 2007-10-16 23:27:27 -0700 | [diff] [blame] | 1497 | |
| 1498 | if (!vmcoreinfo_size) |
| 1499 | return; |
Ken'ichi Ohmichi | fd59d23 | 2007-10-16 23:27:27 -0700 | [diff] [blame] | 1500 | buf = append_elf_note(buf, VMCOREINFO_NOTE_NAME, 0, vmcoreinfo_data, |
| 1501 | vmcoreinfo_size); |
Ken'ichi Ohmichi | fd59d23 | 2007-10-16 23:27:27 -0700 | [diff] [blame] | 1502 | final_note(buf); |
| 1503 | } |
| 1504 | |
Michael Holzheu | fa8ff29 | 2011-10-30 15:16:41 +0100 | [diff] [blame] | 1505 | void crash_save_vmcoreinfo(void) |
| 1506 | { |
Vivek Goyal | 63dca8d | 2012-07-30 14:42:36 -0700 | [diff] [blame] | 1507 | vmcoreinfo_append_str("CRASHTIME=%ld\n", get_seconds()); |
Michael Holzheu | fa8ff29 | 2011-10-30 15:16:41 +0100 | [diff] [blame] | 1508 | update_vmcoreinfo_note(); |
| 1509 | } |
| 1510 | |
Ken'ichi Ohmichi | fd59d23 | 2007-10-16 23:27:27 -0700 | [diff] [blame] | 1511 | void vmcoreinfo_append_str(const char *fmt, ...) |
| 1512 | { |
| 1513 | va_list args; |
| 1514 | char buf[0x50]; |
Zhang Yanfei | 310faaa | 2013-04-30 15:28:21 -0700 | [diff] [blame] | 1515 | size_t r; |
Ken'ichi Ohmichi | fd59d23 | 2007-10-16 23:27:27 -0700 | [diff] [blame] | 1516 | |
| 1517 | va_start(args, fmt); |
Chen Gang | a19428e | 2014-01-27 17:07:13 -0800 | [diff] [blame] | 1518 | r = vscnprintf(buf, sizeof(buf), fmt, args); |
Ken'ichi Ohmichi | fd59d23 | 2007-10-16 23:27:27 -0700 | [diff] [blame] | 1519 | va_end(args); |
| 1520 | |
Zhang Yanfei | 31c3a3f | 2013-04-30 15:28:23 -0700 | [diff] [blame] | 1521 | r = min(r, vmcoreinfo_max_size - vmcoreinfo_size); |
Ken'ichi Ohmichi | fd59d23 | 2007-10-16 23:27:27 -0700 | [diff] [blame] | 1522 | |
| 1523 | memcpy(&vmcoreinfo_data[vmcoreinfo_size], buf, r); |
| 1524 | |
| 1525 | vmcoreinfo_size += r; |
| 1526 | } |
| 1527 | |
| 1528 | /* |
| 1529 | * provide an empty default implementation here -- architecture |
| 1530 | * code may override this |
| 1531 | */ |
Gideon Israel Dsouza | 52f5684c | 2014-04-07 15:39:20 -0700 | [diff] [blame] | 1532 | void __weak arch_crash_save_vmcoreinfo(void) |
Ken'ichi Ohmichi | fd59d23 | 2007-10-16 23:27:27 -0700 | [diff] [blame] | 1533 | {} |
| 1534 | |
Gideon Israel Dsouza | 52f5684c | 2014-04-07 15:39:20 -0700 | [diff] [blame] | 1535 | unsigned long __weak paddr_vmcoreinfo_note(void) |
Ken'ichi Ohmichi | fd59d23 | 2007-10-16 23:27:27 -0700 | [diff] [blame] | 1536 | { |
| 1537 | return __pa((unsigned long)(char *)&vmcoreinfo_note); |
| 1538 | } |
| 1539 | |
| 1540 | static int __init crash_save_vmcoreinfo_init(void) |
| 1541 | { |
Ken'ichi Ohmichi | bba1f60 | 2008-02-07 00:15:22 -0800 | [diff] [blame] | 1542 | VMCOREINFO_OSRELEASE(init_uts_ns.name.release); |
| 1543 | VMCOREINFO_PAGESIZE(PAGE_SIZE); |
Ken'ichi Ohmichi | fd59d23 | 2007-10-16 23:27:27 -0700 | [diff] [blame] | 1544 | |
Ken'ichi Ohmichi | bcbba6c | 2007-10-16 23:27:30 -0700 | [diff] [blame] | 1545 | VMCOREINFO_SYMBOL(init_uts_ns); |
| 1546 | VMCOREINFO_SYMBOL(node_online_map); |
Will Deacon | d034cfa | 2012-03-28 14:42:47 -0700 | [diff] [blame] | 1547 | #ifdef CONFIG_MMU |
Ken'ichi Ohmichi | bcbba6c | 2007-10-16 23:27:30 -0700 | [diff] [blame] | 1548 | VMCOREINFO_SYMBOL(swapper_pg_dir); |
Will Deacon | d034cfa | 2012-03-28 14:42:47 -0700 | [diff] [blame] | 1549 | #endif |
Ken'ichi Ohmichi | bcbba6c | 2007-10-16 23:27:30 -0700 | [diff] [blame] | 1550 | VMCOREINFO_SYMBOL(_stext); |
Joonsoo Kim | f1c4069 | 2013-04-29 15:07:37 -0700 | [diff] [blame] | 1551 | VMCOREINFO_SYMBOL(vmap_area_list); |
Ken'ichi Ohmichi | fd59d23 | 2007-10-16 23:27:27 -0700 | [diff] [blame] | 1552 | |
| 1553 | #ifndef CONFIG_NEED_MULTIPLE_NODES |
Ken'ichi Ohmichi | bcbba6c | 2007-10-16 23:27:30 -0700 | [diff] [blame] | 1554 | VMCOREINFO_SYMBOL(mem_map); |
| 1555 | VMCOREINFO_SYMBOL(contig_page_data); |
Ken'ichi Ohmichi | fd59d23 | 2007-10-16 23:27:27 -0700 | [diff] [blame] | 1556 | #endif |
| 1557 | #ifdef CONFIG_SPARSEMEM |
Ken'ichi Ohmichi | bcbba6c | 2007-10-16 23:27:30 -0700 | [diff] [blame] | 1558 | VMCOREINFO_SYMBOL(mem_section); |
| 1559 | VMCOREINFO_LENGTH(mem_section, NR_SECTION_ROOTS); |
Ken'ichi Ohmichi | c76f860 | 2008-02-07 00:15:20 -0800 | [diff] [blame] | 1560 | VMCOREINFO_STRUCT_SIZE(mem_section); |
Ken'ichi Ohmichi | bcbba6c | 2007-10-16 23:27:30 -0700 | [diff] [blame] | 1561 | VMCOREINFO_OFFSET(mem_section, section_mem_map); |
Ken'ichi Ohmichi | fd59d23 | 2007-10-16 23:27:27 -0700 | [diff] [blame] | 1562 | #endif |
Ken'ichi Ohmichi | c76f860 | 2008-02-07 00:15:20 -0800 | [diff] [blame] | 1563 | VMCOREINFO_STRUCT_SIZE(page); |
| 1564 | VMCOREINFO_STRUCT_SIZE(pglist_data); |
| 1565 | VMCOREINFO_STRUCT_SIZE(zone); |
| 1566 | VMCOREINFO_STRUCT_SIZE(free_area); |
| 1567 | VMCOREINFO_STRUCT_SIZE(list_head); |
| 1568 | VMCOREINFO_SIZE(nodemask_t); |
Ken'ichi Ohmichi | bcbba6c | 2007-10-16 23:27:30 -0700 | [diff] [blame] | 1569 | VMCOREINFO_OFFSET(page, flags); |
| 1570 | VMCOREINFO_OFFSET(page, _count); |
| 1571 | VMCOREINFO_OFFSET(page, mapping); |
| 1572 | VMCOREINFO_OFFSET(page, lru); |
Atsushi Kumagai | 8d67091 | 2013-02-27 17:03:25 -0800 | [diff] [blame] | 1573 | VMCOREINFO_OFFSET(page, _mapcount); |
| 1574 | VMCOREINFO_OFFSET(page, private); |
Ken'ichi Ohmichi | bcbba6c | 2007-10-16 23:27:30 -0700 | [diff] [blame] | 1575 | VMCOREINFO_OFFSET(pglist_data, node_zones); |
| 1576 | VMCOREINFO_OFFSET(pglist_data, nr_zones); |
Ken'ichi Ohmichi | fd59d23 | 2007-10-16 23:27:27 -0700 | [diff] [blame] | 1577 | #ifdef CONFIG_FLAT_NODE_MEM_MAP |
Ken'ichi Ohmichi | bcbba6c | 2007-10-16 23:27:30 -0700 | [diff] [blame] | 1578 | VMCOREINFO_OFFSET(pglist_data, node_mem_map); |
Ken'ichi Ohmichi | fd59d23 | 2007-10-16 23:27:27 -0700 | [diff] [blame] | 1579 | #endif |
Ken'ichi Ohmichi | bcbba6c | 2007-10-16 23:27:30 -0700 | [diff] [blame] | 1580 | VMCOREINFO_OFFSET(pglist_data, node_start_pfn); |
| 1581 | VMCOREINFO_OFFSET(pglist_data, node_spanned_pages); |
| 1582 | VMCOREINFO_OFFSET(pglist_data, node_id); |
| 1583 | VMCOREINFO_OFFSET(zone, free_area); |
| 1584 | VMCOREINFO_OFFSET(zone, vm_stat); |
| 1585 | VMCOREINFO_OFFSET(zone, spanned_pages); |
| 1586 | VMCOREINFO_OFFSET(free_area, free_list); |
| 1587 | VMCOREINFO_OFFSET(list_head, next); |
| 1588 | VMCOREINFO_OFFSET(list_head, prev); |
Atsushi Kumagai | 13ba3fc | 2013-04-29 15:07:40 -0700 | [diff] [blame] | 1589 | VMCOREINFO_OFFSET(vmap_area, va_start); |
| 1590 | VMCOREINFO_OFFSET(vmap_area, list); |
Ken'ichi Ohmichi | bcbba6c | 2007-10-16 23:27:30 -0700 | [diff] [blame] | 1591 | VMCOREINFO_LENGTH(zone.free_area, MAX_ORDER); |
Neil Horman | 04d491a | 2009-04-02 16:58:57 -0700 | [diff] [blame] | 1592 | log_buf_kexec_setup(); |
Ken'ichi Ohmichi | 83a08e7 | 2008-01-08 15:33:05 -0800 | [diff] [blame] | 1593 | VMCOREINFO_LENGTH(free_area.free_list, MIGRATE_TYPES); |
Ken'ichi Ohmichi | bcbba6c | 2007-10-16 23:27:30 -0700 | [diff] [blame] | 1594 | VMCOREINFO_NUMBER(NR_FREE_PAGES); |
Ken'ichi Ohmichi | 122c7a5 | 2008-04-28 02:13:04 -0700 | [diff] [blame] | 1595 | VMCOREINFO_NUMBER(PG_lru); |
| 1596 | VMCOREINFO_NUMBER(PG_private); |
| 1597 | VMCOREINFO_NUMBER(PG_swapcache); |
Atsushi Kumagai | 8d67091 | 2013-02-27 17:03:25 -0800 | [diff] [blame] | 1598 | VMCOREINFO_NUMBER(PG_slab); |
Mitsuhiro Tanino | 0d0bf66 | 2013-02-27 17:03:27 -0800 | [diff] [blame] | 1599 | #ifdef CONFIG_MEMORY_FAILURE |
| 1600 | VMCOREINFO_NUMBER(PG_hwpoison); |
| 1601 | #endif |
Petr Tesarik | b3acc56 | 2014-06-23 13:22:03 -0700 | [diff] [blame] | 1602 | VMCOREINFO_NUMBER(PG_head_mask); |
Atsushi Kumagai | 8d67091 | 2013-02-27 17:03:25 -0800 | [diff] [blame] | 1603 | VMCOREINFO_NUMBER(PAGE_BUDDY_MAPCOUNT_VALUE); |
David Rientjes | 3a1122d | 2014-07-30 19:05:55 -0700 | [diff] [blame] | 1604 | #ifdef CONFIG_HUGETLBFS |
Atsushi Kumagai | 8f1d26d | 2014-07-30 16:08:39 -0700 | [diff] [blame] | 1605 | VMCOREINFO_SYMBOL(free_huge_page); |
David Rientjes | 3a1122d | 2014-07-30 19:05:55 -0700 | [diff] [blame] | 1606 | #endif |
Ken'ichi Ohmichi | fd59d23 | 2007-10-16 23:27:27 -0700 | [diff] [blame] | 1607 | |
| 1608 | arch_crash_save_vmcoreinfo(); |
Michael Holzheu | fa8ff29 | 2011-10-30 15:16:41 +0100 | [diff] [blame] | 1609 | update_vmcoreinfo_note(); |
Ken'ichi Ohmichi | fd59d23 | 2007-10-16 23:27:27 -0700 | [diff] [blame] | 1610 | |
| 1611 | return 0; |
| 1612 | } |
| 1613 | |
Paul Gortmaker | c96d666 | 2014-04-03 14:48:35 -0700 | [diff] [blame] | 1614 | subsys_initcall(crash_save_vmcoreinfo_init); |
Huang Ying | 3ab8352 | 2008-07-25 19:45:07 -0700 | [diff] [blame] | 1615 | |
Huang Ying | 7ade3fc | 2008-08-15 00:40:21 -0700 | [diff] [blame] | 1616 | /* |
| 1617 | * Move into place and start executing a preloaded standalone |
| 1618 | * executable. If nothing was preloaded return an error. |
Huang Ying | 3ab8352 | 2008-07-25 19:45:07 -0700 | [diff] [blame] | 1619 | */ |
| 1620 | int kernel_kexec(void) |
| 1621 | { |
| 1622 | int error = 0; |
| 1623 | |
Andrew Morton | 8c5a1cf | 2008-08-15 00:40:27 -0700 | [diff] [blame] | 1624 | if (!mutex_trylock(&kexec_mutex)) |
Huang Ying | 3ab8352 | 2008-07-25 19:45:07 -0700 | [diff] [blame] | 1625 | return -EBUSY; |
| 1626 | if (!kexec_image) { |
| 1627 | error = -EINVAL; |
| 1628 | goto Unlock; |
| 1629 | } |
| 1630 | |
Huang Ying | 3ab8352 | 2008-07-25 19:45:07 -0700 | [diff] [blame] | 1631 | #ifdef CONFIG_KEXEC_JUMP |
Huang Ying | 7ade3fc | 2008-08-15 00:40:21 -0700 | [diff] [blame] | 1632 | if (kexec_image->preserve_context) { |
Srivatsa S. Bhat | bcda53f | 2011-12-07 22:29:54 +0100 | [diff] [blame] | 1633 | lock_system_sleep(); |
Huang Ying | 89081d1 | 2008-07-25 19:45:10 -0700 | [diff] [blame] | 1634 | pm_prepare_console(); |
| 1635 | error = freeze_processes(); |
| 1636 | if (error) { |
| 1637 | error = -EBUSY; |
| 1638 | goto Restore_console; |
| 1639 | } |
| 1640 | suspend_console(); |
Alan Stern | d161630 | 2009-05-24 22:05:42 +0200 | [diff] [blame] | 1641 | error = dpm_suspend_start(PMSG_FREEZE); |
Huang Ying | 89081d1 | 2008-07-25 19:45:10 -0700 | [diff] [blame] | 1642 | if (error) |
| 1643 | goto Resume_console; |
Alan Stern | d161630 | 2009-05-24 22:05:42 +0200 | [diff] [blame] | 1644 | /* At this point, dpm_suspend_start() has been called, |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 1645 | * but *not* dpm_suspend_end(). We *must* call |
| 1646 | * dpm_suspend_end() now. Otherwise, drivers for |
Huang Ying | 89081d1 | 2008-07-25 19:45:10 -0700 | [diff] [blame] | 1647 | * some devices (e.g. interrupt controllers) become |
| 1648 | * desynchronized with the actual state of the |
| 1649 | * hardware at resume time, and evil weirdness ensues. |
| 1650 | */ |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 1651 | error = dpm_suspend_end(PMSG_FREEZE); |
Huang Ying | 89081d1 | 2008-07-25 19:45:10 -0700 | [diff] [blame] | 1652 | if (error) |
Rafael J. Wysocki | 749b0af | 2009-03-16 22:34:35 +0100 | [diff] [blame] | 1653 | goto Resume_devices; |
| 1654 | error = disable_nonboot_cpus(); |
| 1655 | if (error) |
| 1656 | goto Enable_cpus; |
Rafael J. Wysocki | 2ed8d2b | 2009-03-16 22:34:06 +0100 | [diff] [blame] | 1657 | local_irq_disable(); |
Rafael J. Wysocki | 2e711c0 | 2011-04-26 19:15:07 +0200 | [diff] [blame] | 1658 | error = syscore_suspend(); |
Rafael J. Wysocki | 770824b | 2009-02-22 18:38:50 +0100 | [diff] [blame] | 1659 | if (error) |
Rafael J. Wysocki | 749b0af | 2009-03-16 22:34:35 +0100 | [diff] [blame] | 1660 | goto Enable_irqs; |
Huang Ying | 7ade3fc | 2008-08-15 00:40:21 -0700 | [diff] [blame] | 1661 | } else |
Huang Ying | 3ab8352 | 2008-07-25 19:45:07 -0700 | [diff] [blame] | 1662 | #endif |
Huang Ying | 7ade3fc | 2008-08-15 00:40:21 -0700 | [diff] [blame] | 1663 | { |
Khalid Aziz | 4fc9bbf | 2013-11-27 15:19:25 -0700 | [diff] [blame] | 1664 | kexec_in_progress = true; |
Huang Ying | ca195b7 | 2008-08-15 00:40:24 -0700 | [diff] [blame] | 1665 | kernel_restart_prepare(NULL); |
Vivek Goyal | c97102b | 2013-12-18 17:08:31 -0800 | [diff] [blame] | 1666 | migrate_to_reboot_cpu(); |
Srivatsa S. Bhat | 011e4b0 | 2014-05-27 16:25:34 +0530 | [diff] [blame] | 1667 | |
| 1668 | /* |
| 1669 | * migrate_to_reboot_cpu() disables CPU hotplug assuming that |
| 1670 | * no further code needs to use CPU hotplug (which is true in |
| 1671 | * the reboot case). However, the kexec path depends on using |
| 1672 | * CPU hotplug again; so re-enable it here. |
| 1673 | */ |
| 1674 | cpu_hotplug_enable(); |
Fabian Frederick | e1bebcf | 2014-06-06 14:37:09 -0700 | [diff] [blame] | 1675 | pr_emerg("Starting new kernel\n"); |
Huang Ying | 3ab8352 | 2008-07-25 19:45:07 -0700 | [diff] [blame] | 1676 | machine_shutdown(); |
| 1677 | } |
| 1678 | |
| 1679 | machine_kexec(kexec_image); |
| 1680 | |
Huang Ying | 3ab8352 | 2008-07-25 19:45:07 -0700 | [diff] [blame] | 1681 | #ifdef CONFIG_KEXEC_JUMP |
Huang Ying | 7ade3fc | 2008-08-15 00:40:21 -0700 | [diff] [blame] | 1682 | if (kexec_image->preserve_context) { |
Rafael J. Wysocki | 19234c0 | 2011-04-20 00:36:11 +0200 | [diff] [blame] | 1683 | syscore_resume(); |
Rafael J. Wysocki | 749b0af | 2009-03-16 22:34:35 +0100 | [diff] [blame] | 1684 | Enable_irqs: |
Huang Ying | 3ab8352 | 2008-07-25 19:45:07 -0700 | [diff] [blame] | 1685 | local_irq_enable(); |
Rafael J. Wysocki | 749b0af | 2009-03-16 22:34:35 +0100 | [diff] [blame] | 1686 | Enable_cpus: |
Huang Ying | 89081d1 | 2008-07-25 19:45:10 -0700 | [diff] [blame] | 1687 | enable_nonboot_cpus(); |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 1688 | dpm_resume_start(PMSG_RESTORE); |
Huang Ying | 89081d1 | 2008-07-25 19:45:10 -0700 | [diff] [blame] | 1689 | Resume_devices: |
Alan Stern | d161630 | 2009-05-24 22:05:42 +0200 | [diff] [blame] | 1690 | dpm_resume_end(PMSG_RESTORE); |
Huang Ying | 89081d1 | 2008-07-25 19:45:10 -0700 | [diff] [blame] | 1691 | Resume_console: |
| 1692 | resume_console(); |
| 1693 | thaw_processes(); |
| 1694 | Restore_console: |
| 1695 | pm_restore_console(); |
Srivatsa S. Bhat | bcda53f | 2011-12-07 22:29:54 +0100 | [diff] [blame] | 1696 | unlock_system_sleep(); |
Huang Ying | 3ab8352 | 2008-07-25 19:45:07 -0700 | [diff] [blame] | 1697 | } |
Huang Ying | 7ade3fc | 2008-08-15 00:40:21 -0700 | [diff] [blame] | 1698 | #endif |
Huang Ying | 3ab8352 | 2008-07-25 19:45:07 -0700 | [diff] [blame] | 1699 | |
| 1700 | Unlock: |
Andrew Morton | 8c5a1cf | 2008-08-15 00:40:27 -0700 | [diff] [blame] | 1701 | mutex_unlock(&kexec_mutex); |
Huang Ying | 3ab8352 | 2008-07-25 19:45:07 -0700 | [diff] [blame] | 1702 | return error; |
| 1703 | } |