1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * ppc64 code to implement the kexec_file_load syscall
4  *
5  * Copyright (C) 2004  Adam Litke (agl@us.ibm.com)
6  * Copyright (C) 2004  IBM Corp.
7  * Copyright (C) 2004,2005  Milton D Miller II, IBM Corporation
8  * Copyright (C) 2005  R Sharada (sharada@in.ibm.com)
9  * Copyright (C) 2006  Mohan Kumar M (mohan@in.ibm.com)
10  * Copyright (C) 2020  IBM Corporation
11  *
12  * Based on kexec-tools' kexec-ppc64.c, kexec-elf-rel-ppc64.c, fs2dt.c.
13  * Heavily modified for the kernel by
14  * Hari Bathini, IBM Corporation.
15  */
16 
17 #include <linux/kexec.h>
18 #include <linux/of_fdt.h>
19 #include <linux/libfdt.h>
20 #include <linux/of_device.h>
21 #include <linux/memblock.h>
22 #include <linux/slab.h>
23 #include <linux/vmalloc.h>
24 #include <asm/drmem.h>
25 #include <asm/kexec_ranges.h>
26 #include <asm/crashdump-ppc64.h>
27 
28 struct umem_info {
29 	u64 *buf;		/* data buffer for usable-memory property */
30 	u32 size;		/* size allocated for the data buffer */
31 	u32 max_entries;	/* maximum no. of entries */
32 	u32 idx;		/* index of current entry */
33 
34 	/* usable memory ranges to look up */
35 	unsigned int nr_ranges;
36 	const struct crash_mem_range *ranges;
37 };
38 
39 const struct kexec_file_ops * const kexec_file_loaders[] = {
40 	&kexec_elf64_ops,
41 	NULL
42 };
43 
44 /**
45  * get_exclude_memory_ranges - Get exclude memory ranges. This list includes
46  *                             regions like opal/rtas, tce-table, initrd,
47  *                             kernel, htab which should be avoided while
48  *                             setting up kexec load segments.
49  * @mem_ranges:                Range list to add the memory ranges to.
50  *
51  * Returns 0 on success, negative errno on error.
52  */
53 static int get_exclude_memory_ranges(struct crash_mem **mem_ranges)
54 {
55 	int ret;
56 
57 	ret = add_tce_mem_ranges(mem_ranges);
58 	if (ret)
59 		goto out;
60 
61 	ret = add_initrd_mem_range(mem_ranges);
62 	if (ret)
63 		goto out;
64 
65 	ret = add_htab_mem_range(mem_ranges);
66 	if (ret)
67 		goto out;
68 
69 	ret = add_kernel_mem_range(mem_ranges);
70 	if (ret)
71 		goto out;
72 
73 	ret = add_rtas_mem_range(mem_ranges);
74 	if (ret)
75 		goto out;
76 
77 	ret = add_opal_mem_range(mem_ranges);
78 	if (ret)
79 		goto out;
80 
81 	ret = add_reserved_mem_ranges(mem_ranges);
82 	if (ret)
83 		goto out;
84 
85 	/* exclude memory ranges should be sorted for easy lookup */
86 	sort_memory_ranges(*mem_ranges, true);
87 out:
88 	if (ret)
89 		pr_err("Failed to setup exclude memory ranges\n");
90 	return ret;
91 }
92 
93 /**
94  * get_usable_memory_ranges - Get usable memory ranges. This list includes
95  *                            regions like crashkernel, opal/rtas & tce-table,
96  *                            that kdump kernel could use.
97  * @mem_ranges:               Range list to add the memory ranges to.
98  *
99  * Returns 0 on success, negative errno on error.
100  */
101 static int get_usable_memory_ranges(struct crash_mem **mem_ranges)
102 {
103 	int ret;
104 
105 	/*
106 	 * Early boot failure observed on guests when low memory (first memory
107 	 * block?) is not added to usable memory. So, add [0, crashk_res.end]
108 	 * instead of [crashk_res.start, crashk_res.end] to workaround it.
109 	 * Also, crashed kernel's memory must be added to reserve map to
110 	 * avoid kdump kernel from using it.
111 	 */
112 	ret = add_mem_range(mem_ranges, 0, crashk_res.end + 1);
113 	if (ret)
114 		goto out;
115 
116 	ret = add_rtas_mem_range(mem_ranges);
117 	if (ret)
118 		goto out;
119 
120 	ret = add_opal_mem_range(mem_ranges);
121 	if (ret)
122 		goto out;
123 
124 	ret = add_tce_mem_ranges(mem_ranges);
125 out:
126 	if (ret)
127 		pr_err("Failed to setup usable memory ranges\n");
128 	return ret;
129 }
130 
131 /**
132  * get_crash_memory_ranges - Get crash memory ranges. This list includes
133  *                           first/crashing kernel's memory regions that
134  *                           would be exported via an elfcore.
135  * @mem_ranges:              Range list to add the memory ranges to.
136  *
137  * Returns 0 on success, negative errno on error.
138  */
139 static int get_crash_memory_ranges(struct crash_mem **mem_ranges)
140 {
141 	phys_addr_t base, end;
142 	struct crash_mem *tmem;
143 	u64 i;
144 	int ret;
145 
146 	for_each_mem_range(i, &base, &end) {
147 		u64 size = end - base;
148 
149 		/* Skip backup memory region, which needs a separate entry */
150 		if (base == BACKUP_SRC_START) {
151 			if (size > BACKUP_SRC_SIZE) {
152 				base = BACKUP_SRC_END + 1;
153 				size -= BACKUP_SRC_SIZE;
154 			} else
155 				continue;
156 		}
157 
158 		ret = add_mem_range(mem_ranges, base, size);
159 		if (ret)
160 			goto out;
161 
162 		/* Try merging adjacent ranges before reallocation attempt */
163 		if ((*mem_ranges)->nr_ranges == (*mem_ranges)->max_nr_ranges)
164 			sort_memory_ranges(*mem_ranges, true);
165 	}
166 
167 	/* Reallocate memory ranges if there is no space to split ranges */
168 	tmem = *mem_ranges;
169 	if (tmem && (tmem->nr_ranges == tmem->max_nr_ranges)) {
170 		tmem = realloc_mem_ranges(mem_ranges);
171 		if (!tmem)
172 			goto out;
173 	}
174 
175 	/* Exclude crashkernel region */
176 	ret = crash_exclude_mem_range(tmem, crashk_res.start, crashk_res.end);
177 	if (ret)
178 		goto out;
179 
180 	/*
181 	 * FIXME: For now, stay in parity with kexec-tools but if RTAS/OPAL
182 	 *        regions are exported to save their context at the time of
183 	 *        crash, they should actually be backed up just like the
184 	 *        first 64K bytes of memory.
185 	 */
186 	ret = add_rtas_mem_range(mem_ranges);
187 	if (ret)
188 		goto out;
189 
190 	ret = add_opal_mem_range(mem_ranges);
191 	if (ret)
192 		goto out;
193 
194 	/* create a separate program header for the backup region */
195 	ret = add_mem_range(mem_ranges, BACKUP_SRC_START, BACKUP_SRC_SIZE);
196 	if (ret)
197 		goto out;
198 
199 	sort_memory_ranges(*mem_ranges, false);
200 out:
201 	if (ret)
202 		pr_err("Failed to setup crash memory ranges\n");
203 	return ret;
204 }
205 
206 /**
207  * get_reserved_memory_ranges - Get reserve memory ranges. This list includes
208  *                              memory regions that should be added to the
209  *                              memory reserve map to ensure the region is
210  *                              protected from any mischief.
211  * @mem_ranges:                 Range list to add the memory ranges to.
212  *
213  * Returns 0 on success, negative errno on error.
214  */
215 static int get_reserved_memory_ranges(struct crash_mem **mem_ranges)
216 {
217 	int ret;
218 
219 	ret = add_rtas_mem_range(mem_ranges);
220 	if (ret)
221 		goto out;
222 
223 	ret = add_tce_mem_ranges(mem_ranges);
224 	if (ret)
225 		goto out;
226 
227 	ret = add_reserved_mem_ranges(mem_ranges);
228 out:
229 	if (ret)
230 		pr_err("Failed to setup reserved memory ranges\n");
231 	return ret;
232 }
233 
234 /**
235  * __locate_mem_hole_top_down - Looks top down for a large enough memory hole
236  *                              in the memory regions between buf_min & buf_max
237  *                              for the buffer. If found, sets kbuf->mem.
238  * @kbuf:                       Buffer contents and memory parameters.
239  * @buf_min:                    Minimum address for the buffer.
240  * @buf_max:                    Maximum address for the buffer.
241  *
242  * Returns 0 on success, negative errno on error.
243  */
244 static int __locate_mem_hole_top_down(struct kexec_buf *kbuf,
245 				      u64 buf_min, u64 buf_max)
246 {
247 	int ret = -EADDRNOTAVAIL;
248 	phys_addr_t start, end;
249 	u64 i;
250 
251 	for_each_mem_range_rev(i, &start, &end) {
252 		/*
253 		 * memblock uses [start, end) convention while it is
254 		 * [start, end] here. Fix the off-by-one to have the
255 		 * same convention.
256 		 */
257 		end -= 1;
258 
259 		if (start > buf_max)
260 			continue;
261 
262 		/* Memory hole not found */
263 		if (end < buf_min)
264 			break;
265 
266 		/* Adjust memory region based on the given range */
267 		if (start < buf_min)
268 			start = buf_min;
269 		if (end > buf_max)
270 			end = buf_max;
271 
272 		start = ALIGN(start, kbuf->buf_align);
273 		if (start < end && (end - start + 1) >= kbuf->memsz) {
274 			/* Suitable memory range found. Set kbuf->mem */
275 			kbuf->mem = ALIGN_DOWN(end - kbuf->memsz + 1,
276 					       kbuf->buf_align);
277 			ret = 0;
278 			break;
279 		}
280 	}
281 
282 	return ret;
283 }
284 
285 /**
286  * locate_mem_hole_top_down_ppc64 - Skip special memory regions to find a
287  *                                  suitable buffer with top down approach.
288  * @kbuf:                           Buffer contents and memory parameters.
289  * @buf_min:                        Minimum address for the buffer.
290  * @buf_max:                        Maximum address for the buffer.
291  * @emem:                           Exclude memory ranges.
292  *
293  * Returns 0 on success, negative errno on error.
294  */
295 static int locate_mem_hole_top_down_ppc64(struct kexec_buf *kbuf,
296 					  u64 buf_min, u64 buf_max,
297 					  const struct crash_mem *emem)
298 {
299 	int i, ret = 0, err = -EADDRNOTAVAIL;
300 	u64 start, end, tmin, tmax;
301 
302 	tmax = buf_max;
303 	for (i = (emem->nr_ranges - 1); i >= 0; i--) {
304 		start = emem->ranges[i].start;
305 		end = emem->ranges[i].end;
306 
307 		if (start > tmax)
308 			continue;
309 
310 		if (end < tmax) {
311 			tmin = (end < buf_min ? buf_min : end + 1);
312 			ret = __locate_mem_hole_top_down(kbuf, tmin, tmax);
313 			if (!ret)
314 				return 0;
315 		}
316 
317 		tmax = start - 1;
318 
319 		if (tmax < buf_min) {
320 			ret = err;
321 			break;
322 		}
323 		ret = 0;
324 	}
325 
326 	if (!ret) {
327 		tmin = buf_min;
328 		ret = __locate_mem_hole_top_down(kbuf, tmin, tmax);
329 	}
330 	return ret;
331 }
332 
333 /**
334  * __locate_mem_hole_bottom_up - Looks bottom up for a large enough memory hole
335  *                               in the memory regions between buf_min & buf_max
336  *                               for the buffer. If found, sets kbuf->mem.
337  * @kbuf:                        Buffer contents and memory parameters.
338  * @buf_min:                     Minimum address for the buffer.
339  * @buf_max:                     Maximum address for the buffer.
340  *
341  * Returns 0 on success, negative errno on error.
342  */
343 static int __locate_mem_hole_bottom_up(struct kexec_buf *kbuf,
344 				       u64 buf_min, u64 buf_max)
345 {
346 	int ret = -EADDRNOTAVAIL;
347 	phys_addr_t start, end;
348 	u64 i;
349 
350 	for_each_mem_range(i, &start, &end) {
351 		/*
352 		 * memblock uses [start, end) convention while it is
353 		 * [start, end] here. Fix the off-by-one to have the
354 		 * same convention.
355 		 */
356 		end -= 1;
357 
358 		if (end < buf_min)
359 			continue;
360 
361 		/* Memory hole not found */
362 		if (start > buf_max)
363 			break;
364 
365 		/* Adjust memory region based on the given range */
366 		if (start < buf_min)
367 			start = buf_min;
368 		if (end > buf_max)
369 			end = buf_max;
370 
371 		start = ALIGN(start, kbuf->buf_align);
372 		if (start < end && (end - start + 1) >= kbuf->memsz) {
373 			/* Suitable memory range found. Set kbuf->mem */
374 			kbuf->mem = start;
375 			ret = 0;
376 			break;
377 		}
378 	}
379 
380 	return ret;
381 }
382 
383 /**
384  * locate_mem_hole_bottom_up_ppc64 - Skip special memory regions to find a
385  *                                   suitable buffer with bottom up approach.
386  * @kbuf:                            Buffer contents and memory parameters.
387  * @buf_min:                         Minimum address for the buffer.
388  * @buf_max:                         Maximum address for the buffer.
389  * @emem:                            Exclude memory ranges.
390  *
391  * Returns 0 on success, negative errno on error.
392  */
393 static int locate_mem_hole_bottom_up_ppc64(struct kexec_buf *kbuf,
394 					   u64 buf_min, u64 buf_max,
395 					   const struct crash_mem *emem)
396 {
397 	int i, ret = 0, err = -EADDRNOTAVAIL;
398 	u64 start, end, tmin, tmax;
399 
400 	tmin = buf_min;
401 	for (i = 0; i < emem->nr_ranges; i++) {
402 		start = emem->ranges[i].start;
403 		end = emem->ranges[i].end;
404 
405 		if (end < tmin)
406 			continue;
407 
408 		if (start > tmin) {
409 			tmax = (start > buf_max ? buf_max : start - 1);
410 			ret = __locate_mem_hole_bottom_up(kbuf, tmin, tmax);
411 			if (!ret)
412 				return 0;
413 		}
414 
415 		tmin = end + 1;
416 
417 		if (tmin > buf_max) {
418 			ret = err;
419 			break;
420 		}
421 		ret = 0;
422 	}
423 
424 	if (!ret) {
425 		tmax = buf_max;
426 		ret = __locate_mem_hole_bottom_up(kbuf, tmin, tmax);
427 	}
428 	return ret;
429 }
430 
431 /**
432  * check_realloc_usable_mem - Reallocate buffer if it can't accommodate entries
433  * @um_info:                  Usable memory buffer and ranges info.
434  * @cnt:                      No. of entries to accommodate.
435  *
436  * Frees up the old buffer if memory reallocation fails.
437  *
438  * Returns buffer on success, NULL on error.
439  */
440 static u64 *check_realloc_usable_mem(struct umem_info *um_info, int cnt)
441 {
442 	u32 new_size;
443 	u64 *tbuf;
444 
445 	if ((um_info->idx + cnt) <= um_info->max_entries)
446 		return um_info->buf;
447 
448 	new_size = um_info->size + MEM_RANGE_CHUNK_SZ;
449 	tbuf = krealloc(um_info->buf, new_size, GFP_KERNEL);
450 	if (tbuf) {
451 		um_info->buf = tbuf;
452 		um_info->size = new_size;
453 		um_info->max_entries = (um_info->size / sizeof(u64));
454 	}
455 
456 	return tbuf;
457 }
458 
459 /**
460  * add_usable_mem - Add the usable memory ranges within the given memory range
461  *                  to the buffer
462  * @um_info:        Usable memory buffer and ranges info.
463  * @base:           Base address of memory range to look for.
464  * @end:            End address of memory range to look for.
465  *
466  * Returns 0 on success, negative errno on error.
467  */
468 static int add_usable_mem(struct umem_info *um_info, u64 base, u64 end)
469 {
470 	u64 loc_base, loc_end;
471 	bool add;
472 	int i;
473 
474 	for (i = 0; i < um_info->nr_ranges; i++) {
475 		add = false;
476 		loc_base = um_info->ranges[i].start;
477 		loc_end = um_info->ranges[i].end;
478 		if (loc_base >= base && loc_end <= end)
479 			add = true;
480 		else if (base < loc_end && end > loc_base) {
481 			if (loc_base < base)
482 				loc_base = base;
483 			if (loc_end > end)
484 				loc_end = end;
485 			add = true;
486 		}
487 
488 		if (add) {
489 			if (!check_realloc_usable_mem(um_info, 2))
490 				return -ENOMEM;
491 
492 			um_info->buf[um_info->idx++] = cpu_to_be64(loc_base);
493 			um_info->buf[um_info->idx++] =
494 					cpu_to_be64(loc_end - loc_base + 1);
495 		}
496 	}
497 
498 	return 0;
499 }
500 
501 /**
502  * kdump_setup_usable_lmb - This is a callback function that gets called by
503  *                          walk_drmem_lmbs for every LMB to set its
504  *                          usable memory ranges.
505  * @lmb:                    LMB info.
506  * @usm:                    linux,drconf-usable-memory property value.
507  * @data:                   Pointer to usable memory buffer and ranges info.
508  *
509  * Returns 0 on success, negative errno on error.
510  */
511 static int kdump_setup_usable_lmb(struct drmem_lmb *lmb, const __be32 **usm,
512 				  void *data)
513 {
514 	struct umem_info *um_info;
515 	int tmp_idx, ret;
516 	u64 base, end;
517 
518 	/*
519 	 * kdump load isn't supported on kernels already booted with
520 	 * linux,drconf-usable-memory property.
521 	 */
522 	if (*usm) {
523 		pr_err("linux,drconf-usable-memory property already exists!");
524 		return -EINVAL;
525 	}
526 
527 	um_info = data;
528 	tmp_idx = um_info->idx;
529 	if (!check_realloc_usable_mem(um_info, 1))
530 		return -ENOMEM;
531 
532 	um_info->idx++;
533 	base = lmb->base_addr;
534 	end = base + drmem_lmb_size() - 1;
535 	ret = add_usable_mem(um_info, base, end);
536 	if (!ret) {
537 		/*
538 		 * Update the no. of ranges added. Two entries (base & size)
539 		 * for every range added.
540 		 */
541 		um_info->buf[tmp_idx] =
542 				cpu_to_be64((um_info->idx - tmp_idx - 1) / 2);
543 	}
544 
545 	return ret;
546 }
547 
548 #define NODE_PATH_LEN		256
549 /**
550  * add_usable_mem_property - Add usable memory property for the given
551  *                           memory node.
552  * @fdt:                     Flattened device tree for the kdump kernel.
553  * @dn:                      Memory node.
554  * @um_info:                 Usable memory buffer and ranges info.
555  *
556  * Returns 0 on success, negative errno on error.
557  */
558 static int add_usable_mem_property(void *fdt, struct device_node *dn,
559 				   struct umem_info *um_info)
560 {
561 	int n_mem_addr_cells, n_mem_size_cells, node;
562 	char path[NODE_PATH_LEN];
563 	int i, len, ranges, ret;
564 	const __be32 *prop;
565 	u64 base, end;
566 
567 	of_node_get(dn);
568 
569 	if (snprintf(path, NODE_PATH_LEN, "%pOF", dn) > (NODE_PATH_LEN - 1)) {
570 		pr_err("Buffer (%d) too small for memory node: %pOF\n",
571 		       NODE_PATH_LEN, dn);
572 		return -EOVERFLOW;
573 	}
574 	pr_debug("Memory node path: %s\n", path);
575 
576 	/* Now that we know the path, find its offset in kdump kernel's fdt */
577 	node = fdt_path_offset(fdt, path);
578 	if (node < 0) {
579 		pr_err("Malformed device tree: error reading %s\n", path);
580 		ret = -EINVAL;
581 		goto out;
582 	}
583 
584 	/* Get the address & size cells */
585 	n_mem_addr_cells = of_n_addr_cells(dn);
586 	n_mem_size_cells = of_n_size_cells(dn);
587 	pr_debug("address cells: %d, size cells: %d\n", n_mem_addr_cells,
588 		 n_mem_size_cells);
589 
590 	um_info->idx  = 0;
591 	if (!check_realloc_usable_mem(um_info, 2)) {
592 		ret = -ENOMEM;
593 		goto out;
594 	}
595 
596 	prop = of_get_property(dn, "reg", &len);
597 	if (!prop || len <= 0) {
598 		ret = 0;
599 		goto out;
600 	}
601 
602 	/*
603 	 * "reg" property represents sequence of (addr,size) tuples
604 	 * each representing a memory range.
605 	 */
606 	ranges = (len >> 2) / (n_mem_addr_cells + n_mem_size_cells);
607 
608 	for (i = 0; i < ranges; i++) {
609 		base = of_read_number(prop, n_mem_addr_cells);
610 		prop += n_mem_addr_cells;
611 		end = base + of_read_number(prop, n_mem_size_cells) - 1;
612 		prop += n_mem_size_cells;
613 
614 		ret = add_usable_mem(um_info, base, end);
615 		if (ret)
616 			goto out;
617 	}
618 
619 	/*
620 	 * No kdump kernel usable memory found in this memory node.
621 	 * Write (0,0) tuple in linux,usable-memory property for
622 	 * this region to be ignored.
623 	 */
624 	if (um_info->idx == 0) {
625 		um_info->buf[0] = 0;
626 		um_info->buf[1] = 0;
627 		um_info->idx = 2;
628 	}
629 
630 	ret = fdt_setprop(fdt, node, "linux,usable-memory", um_info->buf,
631 			  (um_info->idx * sizeof(u64)));
632 
633 out:
634 	of_node_put(dn);
635 	return ret;
636 }
637 
638 
639 /**
640  * update_usable_mem_fdt - Updates kdump kernel's fdt with linux,usable-memory
641  *                         and linux,drconf-usable-memory DT properties as
642  *                         appropriate to restrict its memory usage.
643  * @fdt:                   Flattened device tree for the kdump kernel.
644  * @usable_mem:            Usable memory ranges for kdump kernel.
645  *
646  * Returns 0 on success, negative errno on error.
647  */
648 static int update_usable_mem_fdt(void *fdt, struct crash_mem *usable_mem)
649 {
650 	struct umem_info um_info;
651 	struct device_node *dn;
652 	int node, ret = 0;
653 
654 	if (!usable_mem) {
655 		pr_err("Usable memory ranges for kdump kernel not found\n");
656 		return -ENOENT;
657 	}
658 
659 	node = fdt_path_offset(fdt, "/ibm,dynamic-reconfiguration-memory");
660 	if (node == -FDT_ERR_NOTFOUND)
661 		pr_debug("No dynamic reconfiguration memory found\n");
662 	else if (node < 0) {
663 		pr_err("Malformed device tree: error reading /ibm,dynamic-reconfiguration-memory.\n");
664 		return -EINVAL;
665 	}
666 
667 	um_info.buf  = NULL;
668 	um_info.size = 0;
669 	um_info.max_entries = 0;
670 	um_info.idx  = 0;
671 	/* Memory ranges to look up */
672 	um_info.ranges = &(usable_mem->ranges[0]);
673 	um_info.nr_ranges = usable_mem->nr_ranges;
674 
675 	dn = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
676 	if (dn) {
677 		ret = walk_drmem_lmbs(dn, &um_info, kdump_setup_usable_lmb);
678 		of_node_put(dn);
679 
680 		if (ret) {
681 			pr_err("Could not setup linux,drconf-usable-memory property for kdump\n");
682 			goto out;
683 		}
684 
685 		ret = fdt_setprop(fdt, node, "linux,drconf-usable-memory",
686 				  um_info.buf, (um_info.idx * sizeof(u64)));
687 		if (ret) {
688 			pr_err("Failed to update fdt with linux,drconf-usable-memory property");
689 			goto out;
690 		}
691 	}
692 
693 	/*
694 	 * Walk through each memory node and set linux,usable-memory property
695 	 * for the corresponding node in kdump kernel's fdt.
696 	 */
697 	for_each_node_by_type(dn, "memory") {
698 		ret = add_usable_mem_property(fdt, dn, &um_info);
699 		if (ret) {
700 			pr_err("Failed to set linux,usable-memory property for %s node",
701 			       dn->full_name);
702 			goto out;
703 		}
704 	}
705 
706 out:
707 	kfree(um_info.buf);
708 	return ret;
709 }
710 
711 /**
712  * load_backup_segment - Locate a memory hole to place the backup region.
713  * @image:               Kexec image.
714  * @kbuf:                Buffer contents and memory parameters.
715  *
716  * Returns 0 on success, negative errno on error.
717  */
718 static int load_backup_segment(struct kimage *image, struct kexec_buf *kbuf)
719 {
720 	void *buf;
721 	int ret;
722 
723 	/*
724 	 * Setup a source buffer for backup segment.
725 	 *
726 	 * A source buffer has no meaning for backup region as data will
727 	 * be copied from backup source, after crash, in the purgatory.
728 	 * But as load segment code doesn't recognize such segments,
729 	 * setup a dummy source buffer to keep it happy for now.
730 	 */
731 	buf = vzalloc(BACKUP_SRC_SIZE);
732 	if (!buf)
733 		return -ENOMEM;
734 
735 	kbuf->buffer = buf;
736 	kbuf->mem = KEXEC_BUF_MEM_UNKNOWN;
737 	kbuf->bufsz = kbuf->memsz = BACKUP_SRC_SIZE;
738 	kbuf->top_down = false;
739 
740 	ret = kexec_add_buffer(kbuf);
741 	if (ret) {
742 		vfree(buf);
743 		return ret;
744 	}
745 
746 	image->arch.backup_buf = buf;
747 	image->arch.backup_start = kbuf->mem;
748 	return 0;
749 }
750 
751 /**
752  * update_backup_region_phdr - Update backup region's offset for the core to
753  *                             export the region appropriately.
754  * @image:                     Kexec image.
755  * @ehdr:                      ELF core header.
756  *
757  * Assumes an exclusive program header is setup for the backup region
758  * in the ELF headers
759  *
760  * Returns nothing.
761  */
762 static void update_backup_region_phdr(struct kimage *image, Elf64_Ehdr *ehdr)
763 {
764 	Elf64_Phdr *phdr;
765 	unsigned int i;
766 
767 	phdr = (Elf64_Phdr *)(ehdr + 1);
768 	for (i = 0; i < ehdr->e_phnum; i++) {
769 		if (phdr->p_paddr == BACKUP_SRC_START) {
770 			phdr->p_offset = image->arch.backup_start;
771 			pr_debug("Backup region offset updated to 0x%lx\n",
772 				 image->arch.backup_start);
773 			return;
774 		}
775 	}
776 }
777 
778 /**
779  * load_elfcorehdr_segment - Setup crash memory ranges and initialize elfcorehdr
780  *                           segment needed to load kdump kernel.
781  * @image:                   Kexec image.
782  * @kbuf:                    Buffer contents and memory parameters.
783  *
784  * Returns 0 on success, negative errno on error.
785  */
786 static int load_elfcorehdr_segment(struct kimage *image, struct kexec_buf *kbuf)
787 {
788 	struct crash_mem *cmem = NULL;
789 	unsigned long headers_sz;
790 	void *headers = NULL;
791 	int ret;
792 
793 	ret = get_crash_memory_ranges(&cmem);
794 	if (ret)
795 		goto out;
796 
797 	/* Setup elfcorehdr segment */
798 	ret = crash_prepare_elf64_headers(cmem, false, &headers, &headers_sz);
799 	if (ret) {
800 		pr_err("Failed to prepare elf headers for the core\n");
801 		goto out;
802 	}
803 
804 	/* Fix the offset for backup region in the ELF header */
805 	update_backup_region_phdr(image, headers);
806 
807 	kbuf->buffer = headers;
808 	kbuf->mem = KEXEC_BUF_MEM_UNKNOWN;
809 	kbuf->bufsz = kbuf->memsz = headers_sz;
810 	kbuf->top_down = false;
811 
812 	ret = kexec_add_buffer(kbuf);
813 	if (ret) {
814 		vfree(headers);
815 		goto out;
816 	}
817 
818 	image->arch.elfcorehdr_addr = kbuf->mem;
819 	image->arch.elf_headers_sz = headers_sz;
820 	image->arch.elf_headers = headers;
821 out:
822 	kfree(cmem);
823 	return ret;
824 }
825 
826 /**
827  * load_crashdump_segments_ppc64 - Initialize the additional segements needed
828  *                                 to load kdump kernel.
829  * @image:                         Kexec image.
830  * @kbuf:                          Buffer contents and memory parameters.
831  *
832  * Returns 0 on success, negative errno on error.
833  */
834 int load_crashdump_segments_ppc64(struct kimage *image,
835 				  struct kexec_buf *kbuf)
836 {
837 	int ret;
838 
839 	/* Load backup segment - first 64K bytes of the crashing kernel */
840 	ret = load_backup_segment(image, kbuf);
841 	if (ret) {
842 		pr_err("Failed to load backup segment\n");
843 		return ret;
844 	}
845 	pr_debug("Loaded the backup region at 0x%lx\n", kbuf->mem);
846 
847 	/* Load elfcorehdr segment - to export crashing kernel's vmcore */
848 	ret = load_elfcorehdr_segment(image, kbuf);
849 	if (ret) {
850 		pr_err("Failed to load elfcorehdr segment\n");
851 		return ret;
852 	}
853 	pr_debug("Loaded elf core header at 0x%lx, bufsz=0x%lx memsz=0x%lx\n",
854 		 image->arch.elfcorehdr_addr, kbuf->bufsz, kbuf->memsz);
855 
856 	return 0;
857 }
858 
859 /**
860  * setup_purgatory_ppc64 - initialize PPC64 specific purgatory's global
861  *                         variables and call setup_purgatory() to initialize
862  *                         common global variable.
863  * @image:                 kexec image.
864  * @slave_code:            Slave code for the purgatory.
865  * @fdt:                   Flattened device tree for the next kernel.
866  * @kernel_load_addr:      Address where the kernel is loaded.
867  * @fdt_load_addr:         Address where the flattened device tree is loaded.
868  *
869  * Returns 0 on success, negative errno on error.
870  */
871 int setup_purgatory_ppc64(struct kimage *image, const void *slave_code,
872 			  const void *fdt, unsigned long kernel_load_addr,
873 			  unsigned long fdt_load_addr)
874 {
875 	struct device_node *dn = NULL;
876 	int ret;
877 
878 	ret = setup_purgatory(image, slave_code, fdt, kernel_load_addr,
879 			      fdt_load_addr);
880 	if (ret)
881 		goto out;
882 
883 	if (image->type == KEXEC_TYPE_CRASH) {
884 		u32 my_run_at_load = 1;
885 
886 		/*
887 		 * Tell relocatable kernel to run at load address
888 		 * via the word meant for that at 0x5c.
889 		 */
890 		ret = kexec_purgatory_get_set_symbol(image, "run_at_load",
891 						     &my_run_at_load,
892 						     sizeof(my_run_at_load),
893 						     false);
894 		if (ret)
895 			goto out;
896 	}
897 
898 	/* Tell purgatory where to look for backup region */
899 	ret = kexec_purgatory_get_set_symbol(image, "backup_start",
900 					     &image->arch.backup_start,
901 					     sizeof(image->arch.backup_start),
902 					     false);
903 	if (ret)
904 		goto out;
905 
906 	/* Setup OPAL base & entry values */
907 	dn = of_find_node_by_path("/ibm,opal");
908 	if (dn) {
909 		u64 val;
910 
911 		of_property_read_u64(dn, "opal-base-address", &val);
912 		ret = kexec_purgatory_get_set_symbol(image, "opal_base", &val,
913 						     sizeof(val), false);
914 		if (ret)
915 			goto out;
916 
917 		of_property_read_u64(dn, "opal-entry-address", &val);
918 		ret = kexec_purgatory_get_set_symbol(image, "opal_entry", &val,
919 						     sizeof(val), false);
920 	}
921 out:
922 	if (ret)
923 		pr_err("Failed to setup purgatory symbols");
924 	of_node_put(dn);
925 	return ret;
926 }
927 
928 /**
929  * setup_new_fdt_ppc64 - Update the flattend device-tree of the kernel
930  *                       being loaded.
931  * @image:               kexec image being loaded.
932  * @fdt:                 Flattened device tree for the next kernel.
933  * @initrd_load_addr:    Address where the next initrd will be loaded.
934  * @initrd_len:          Size of the next initrd, or 0 if there will be none.
935  * @cmdline:             Command line for the next kernel, or NULL if there will
936  *                       be none.
937  *
938  * Returns 0 on success, negative errno on error.
939  */
940 int setup_new_fdt_ppc64(const struct kimage *image, void *fdt,
941 			unsigned long initrd_load_addr,
942 			unsigned long initrd_len, const char *cmdline)
943 {
944 	struct crash_mem *umem = NULL, *rmem = NULL;
945 	int i, nr_ranges, ret;
946 
947 	ret = setup_new_fdt(image, fdt, initrd_load_addr, initrd_len, cmdline);
948 	if (ret)
949 		goto out;
950 
951 	/*
952 	 * Restrict memory usage for kdump kernel by setting up
953 	 * usable memory ranges and memory reserve map.
954 	 */
955 	if (image->type == KEXEC_TYPE_CRASH) {
956 		ret = get_usable_memory_ranges(&umem);
957 		if (ret)
958 			goto out;
959 
960 		ret = update_usable_mem_fdt(fdt, umem);
961 		if (ret) {
962 			pr_err("Error setting up usable-memory property for kdump kernel\n");
963 			goto out;
964 		}
965 
966 		/*
967 		 * Ensure we don't touch crashed kernel's memory except the
968 		 * first 64K of RAM, which will be backed up.
969 		 */
970 		ret = fdt_add_mem_rsv(fdt, BACKUP_SRC_END + 1,
971 				      crashk_res.start - BACKUP_SRC_SIZE);
972 		if (ret) {
973 			pr_err("Error reserving crash memory: %s\n",
974 			       fdt_strerror(ret));
975 			goto out;
976 		}
977 
978 		/* Ensure backup region is not used by kdump/capture kernel */
979 		ret = fdt_add_mem_rsv(fdt, image->arch.backup_start,
980 				      BACKUP_SRC_SIZE);
981 		if (ret) {
982 			pr_err("Error reserving memory for backup: %s\n",
983 			       fdt_strerror(ret));
984 			goto out;
985 		}
986 	}
987 
988 	/* Update memory reserve map */
989 	ret = get_reserved_memory_ranges(&rmem);
990 	if (ret)
991 		goto out;
992 
993 	nr_ranges = rmem ? rmem->nr_ranges : 0;
994 	for (i = 0; i < nr_ranges; i++) {
995 		u64 base, size;
996 
997 		base = rmem->ranges[i].start;
998 		size = rmem->ranges[i].end - base + 1;
999 		ret = fdt_add_mem_rsv(fdt, base, size);
1000 		if (ret) {
1001 			pr_err("Error updating memory reserve map: %s\n",
1002 			       fdt_strerror(ret));
1003 			goto out;
1004 		}
1005 	}
1006 
1007 out:
1008 	kfree(rmem);
1009 	kfree(umem);
1010 	return ret;
1011 }
1012 
1013 /**
1014  * arch_kexec_locate_mem_hole - Skip special memory regions like rtas, opal,
1015  *                              tce-table, reserved-ranges & such (exclude
1016  *                              memory ranges) as they can't be used for kexec
1017  *                              segment buffer. Sets kbuf->mem when a suitable
1018  *                              memory hole is found.
1019  * @kbuf:                       Buffer contents and memory parameters.
1020  *
1021  * Assumes minimum of PAGE_SIZE alignment for kbuf->memsz & kbuf->buf_align.
1022  *
1023  * Returns 0 on success, negative errno on error.
1024  */
1025 int arch_kexec_locate_mem_hole(struct kexec_buf *kbuf)
1026 {
1027 	struct crash_mem **emem;
1028 	u64 buf_min, buf_max;
1029 	int ret;
1030 
1031 	/* Look up the exclude ranges list while locating the memory hole */
1032 	emem = &(kbuf->image->arch.exclude_ranges);
1033 	if (!(*emem) || ((*emem)->nr_ranges == 0)) {
1034 		pr_warn("No exclude range list. Using the default locate mem hole method\n");
1035 		return kexec_locate_mem_hole(kbuf);
1036 	}
1037 
1038 	buf_min = kbuf->buf_min;
1039 	buf_max = kbuf->buf_max;
1040 	/* Segments for kdump kernel should be within crashkernel region */
1041 	if (kbuf->image->type == KEXEC_TYPE_CRASH) {
1042 		buf_min = (buf_min < crashk_res.start ?
1043 			   crashk_res.start : buf_min);
1044 		buf_max = (buf_max > crashk_res.end ?
1045 			   crashk_res.end : buf_max);
1046 	}
1047 
1048 	if (buf_min > buf_max) {
1049 		pr_err("Invalid buffer min and/or max values\n");
1050 		return -EINVAL;
1051 	}
1052 
1053 	if (kbuf->top_down)
1054 		ret = locate_mem_hole_top_down_ppc64(kbuf, buf_min, buf_max,
1055 						     *emem);
1056 	else
1057 		ret = locate_mem_hole_bottom_up_ppc64(kbuf, buf_min, buf_max,
1058 						      *emem);
1059 
1060 	/* Add the buffer allocated to the exclude list for the next lookup */
1061 	if (!ret) {
1062 		add_mem_range(emem, kbuf->mem, kbuf->memsz);
1063 		sort_memory_ranges(*emem, true);
1064 	} else {
1065 		pr_err("Failed to locate memory buffer of size %lu\n",
1066 		       kbuf->memsz);
1067 	}
1068 	return ret;
1069 }
1070 
1071 /**
1072  * arch_kexec_kernel_image_probe - Does additional handling needed to setup
1073  *                                 kexec segments.
1074  * @image:                         kexec image being loaded.
1075  * @buf:                           Buffer pointing to elf data.
1076  * @buf_len:                       Length of the buffer.
1077  *
1078  * Returns 0 on success, negative errno on error.
1079  */
1080 int arch_kexec_kernel_image_probe(struct kimage *image, void *buf,
1081 				  unsigned long buf_len)
1082 {
1083 	int ret;
1084 
1085 	/* Get exclude memory ranges needed for setting up kexec segments */
1086 	ret = get_exclude_memory_ranges(&(image->arch.exclude_ranges));
1087 	if (ret) {
1088 		pr_err("Failed to setup exclude memory ranges for buffer lookup\n");
1089 		return ret;
1090 	}
1091 
1092 	return kexec_image_probe_default(image, buf, buf_len);
1093 }
1094 
1095 /**
1096  * arch_kimage_file_post_load_cleanup - Frees up all the allocations done
1097  *                                      while loading the image.
1098  * @image:                              kexec image being loaded.
1099  *
1100  * Returns 0 on success, negative errno on error.
1101  */
1102 int arch_kimage_file_post_load_cleanup(struct kimage *image)
1103 {
1104 	kfree(image->arch.exclude_ranges);
1105 	image->arch.exclude_ranges = NULL;
1106 
1107 	vfree(image->arch.backup_buf);
1108 	image->arch.backup_buf = NULL;
1109 
1110 	vfree(image->arch.elf_headers);
1111 	image->arch.elf_headers = NULL;
1112 	image->arch.elf_headers_sz = 0;
1113 
1114 	return kexec_image_post_load_cleanup_default(image);
1115 }
1116