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