2 * ppc64 code to implement the kexec_file_load syscall
4 * Copyright (C) 2004 Adam Litke (agl@us.ibm.com)
5 * Copyright (C) 2004 IBM Corp.
6 * Copyright (C) 2004,2005 Milton D Miller II, IBM Corporation
7 * Copyright (C) 2005 R Sharada (sharada@in.ibm.com)
8 * Copyright (C) 2006 Mohan Kumar M (mohan@in.ibm.com)
9 * Copyright (C) 2016 IBM Corporation
11 * Based on kexec-tools' kexec-elf-ppc64.c, fs2dt.c.
12 * Heavily modified for the kernel by
13 * Thiago Jung Bauermann <bauerman@linux.vnet.ibm.com>.
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation (version 2 of the License).
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
25 #include <linux/slab.h>
26 #include <linux/kexec.h>
27 #include <linux/memblock.h>
28 #include <linux/of_fdt.h>
29 #include <linux/libfdt.h>
32 #define SLAVE_CODE_SIZE 256
34 static struct kexec_file_ops
*kexec_file_loaders
[] = {
38 int arch_kexec_kernel_image_probe(struct kimage
*image
, void *buf
,
39 unsigned long buf_len
)
41 int i
, ret
= -ENOEXEC
;
42 struct kexec_file_ops
*fops
;
44 /* We don't support crash kernels yet. */
45 if (image
->type
== KEXEC_TYPE_CRASH
)
48 for (i
= 0; i
< ARRAY_SIZE(kexec_file_loaders
); i
++) {
49 fops
= kexec_file_loaders
[i
];
50 if (!fops
|| !fops
->probe
)
53 ret
= fops
->probe(buf
, buf_len
);
63 void *arch_kexec_kernel_image_load(struct kimage
*image
)
65 if (!image
->fops
|| !image
->fops
->load
)
66 return ERR_PTR(-ENOEXEC
);
68 return image
->fops
->load(image
, image
->kernel_buf
,
69 image
->kernel_buf_len
, image
->initrd_buf
,
70 image
->initrd_buf_len
, image
->cmdline_buf
,
71 image
->cmdline_buf_len
);
74 int arch_kimage_file_post_load_cleanup(struct kimage
*image
)
76 if (!image
->fops
|| !image
->fops
->cleanup
)
79 return image
->fops
->cleanup(image
->image_loader_data
);
83 * arch_kexec_walk_mem - call func(data) for each unreserved memory block
84 * @kbuf: Context info for the search. Also passed to @func.
85 * @func: Function to call for each memory block.
87 * This function is used by kexec_add_buffer and kexec_locate_mem_hole
88 * to find unreserved memory to load kexec segments into.
90 * Return: The memory walk will stop when func returns a non-zero value
91 * and that value will be returned. If all free regions are visited without
92 * func returning non-zero, then zero will be returned.
94 int arch_kexec_walk_mem(struct kexec_buf
*kbuf
, int (*func
)(u64
, u64
, void *))
98 phys_addr_t mstart
, mend
;
100 if (kbuf
->top_down
) {
101 for_each_free_mem_range_reverse(i
, NUMA_NO_NODE
, 0,
102 &mstart
, &mend
, NULL
) {
104 * In memblock, end points to the first byte after the
105 * range while in kexec, end points to the last byte
108 ret
= func(mstart
, mend
- 1, kbuf
);
113 for_each_free_mem_range(i
, NUMA_NO_NODE
, 0, &mstart
, &mend
,
116 * In memblock, end points to the first byte after the
117 * range while in kexec, end points to the last byte
120 ret
= func(mstart
, mend
- 1, kbuf
);
130 * setup_purgatory - initialize the purgatory's global variables
131 * @image: kexec image.
132 * @slave_code: Slave code for the purgatory.
133 * @fdt: Flattened device tree for the next kernel.
134 * @kernel_load_addr: Address where the kernel is loaded.
135 * @fdt_load_addr: Address where the flattened device tree is loaded.
137 * Return: 0 on success, or negative errno on error.
139 int setup_purgatory(struct kimage
*image
, const void *slave_code
,
140 const void *fdt
, unsigned long kernel_load_addr
,
141 unsigned long fdt_load_addr
)
143 unsigned int *slave_code_buf
, master_entry
;
146 slave_code_buf
= kmalloc(SLAVE_CODE_SIZE
, GFP_KERNEL
);
150 /* Get the slave code from the new kernel and put it in purgatory. */
151 ret
= kexec_purgatory_get_set_symbol(image
, "purgatory_start",
152 slave_code_buf
, SLAVE_CODE_SIZE
,
155 kfree(slave_code_buf
);
159 master_entry
= slave_code_buf
[0];
160 memcpy(slave_code_buf
, slave_code
, SLAVE_CODE_SIZE
);
161 slave_code_buf
[0] = master_entry
;
162 ret
= kexec_purgatory_get_set_symbol(image
, "purgatory_start",
163 slave_code_buf
, SLAVE_CODE_SIZE
,
165 kfree(slave_code_buf
);
167 ret
= kexec_purgatory_get_set_symbol(image
, "kernel", &kernel_load_addr
,
168 sizeof(kernel_load_addr
), false);
171 ret
= kexec_purgatory_get_set_symbol(image
, "dt_offset", &fdt_load_addr
,
172 sizeof(fdt_load_addr
), false);
180 * delete_fdt_mem_rsv - delete memory reservation with given address and size
182 * Return: 0 on success, or negative errno on error.
184 int delete_fdt_mem_rsv(void *fdt
, unsigned long start
, unsigned long size
)
186 int i
, ret
, num_rsvs
= fdt_num_mem_rsv(fdt
);
188 for (i
= 0; i
< num_rsvs
; i
++) {
189 uint64_t rsv_start
, rsv_size
;
191 ret
= fdt_get_mem_rsv(fdt
, i
, &rsv_start
, &rsv_size
);
193 pr_err("Malformed device tree.\n");
197 if (rsv_start
== start
&& rsv_size
== size
) {
198 ret
= fdt_del_mem_rsv(fdt
, i
);
200 pr_err("Error deleting device tree reservation.\n");
212 * setup_new_fdt - modify /chosen and memory reservation for the next kernel
213 * @image: kexec image being loaded.
214 * @fdt: Flattened device tree for the next kernel.
215 * @initrd_load_addr: Address where the next initrd will be loaded.
216 * @initrd_len: Size of the next initrd, or 0 if there will be none.
217 * @cmdline: Command line for the next kernel, or NULL if there will
220 * Return: 0 on success, or negative errno on error.
222 int setup_new_fdt(const struct kimage
*image
, void *fdt
,
223 unsigned long initrd_load_addr
, unsigned long initrd_len
,
226 int ret
, chosen_node
;
229 /* Remove memory reservation for the current device tree. */
230 ret
= delete_fdt_mem_rsv(fdt
, __pa(initial_boot_params
),
231 fdt_totalsize(initial_boot_params
));
233 pr_debug("Removed old device tree reservation.\n");
234 else if (ret
!= -ENOENT
)
237 chosen_node
= fdt_path_offset(fdt
, "/chosen");
238 if (chosen_node
== -FDT_ERR_NOTFOUND
) {
239 chosen_node
= fdt_add_subnode(fdt
, fdt_path_offset(fdt
, "/"),
241 if (chosen_node
< 0) {
242 pr_err("Error creating /chosen.\n");
245 } else if (chosen_node
< 0) {
246 pr_err("Malformed device tree: error reading /chosen.\n");
250 /* Did we boot using an initrd? */
251 prop
= fdt_getprop(fdt
, chosen_node
, "linux,initrd-start", NULL
);
253 uint64_t tmp_start
, tmp_end
, tmp_size
;
255 tmp_start
= fdt64_to_cpu(*((const fdt64_t
*) prop
));
257 prop
= fdt_getprop(fdt
, chosen_node
, "linux,initrd-end", NULL
);
259 pr_err("Malformed device tree.\n");
262 tmp_end
= fdt64_to_cpu(*((const fdt64_t
*) prop
));
265 * kexec reserves exact initrd size, while firmware may
266 * reserve a multiple of PAGE_SIZE, so check for both.
268 tmp_size
= tmp_end
- tmp_start
;
269 ret
= delete_fdt_mem_rsv(fdt
, tmp_start
, tmp_size
);
271 ret
= delete_fdt_mem_rsv(fdt
, tmp_start
,
272 round_up(tmp_size
, PAGE_SIZE
));
274 pr_debug("Removed old initrd reservation.\n");
275 else if (ret
!= -ENOENT
)
278 /* If there's no new initrd, delete the old initrd's info. */
279 if (initrd_len
== 0) {
280 ret
= fdt_delprop(fdt
, chosen_node
,
281 "linux,initrd-start");
283 pr_err("Error deleting linux,initrd-start.\n");
287 ret
= fdt_delprop(fdt
, chosen_node
, "linux,initrd-end");
289 pr_err("Error deleting linux,initrd-end.\n");
296 ret
= fdt_setprop_u64(fdt
, chosen_node
,
297 "linux,initrd-start",
300 pr_err("Error setting up the new device tree.\n");
304 /* initrd-end is the first address after the initrd image. */
305 ret
= fdt_setprop_u64(fdt
, chosen_node
, "linux,initrd-end",
306 initrd_load_addr
+ initrd_len
);
308 pr_err("Error setting up the new device tree.\n");
312 ret
= fdt_add_mem_rsv(fdt
, initrd_load_addr
, initrd_len
);
314 pr_err("Error reserving initrd memory: %s\n",
320 if (cmdline
!= NULL
) {
321 ret
= fdt_setprop_string(fdt
, chosen_node
, "bootargs", cmdline
);
323 pr_err("Error setting up the new device tree.\n");
327 ret
= fdt_delprop(fdt
, chosen_node
, "bootargs");
328 if (ret
&& ret
!= -FDT_ERR_NOTFOUND
) {
329 pr_err("Error deleting bootargs.\n");
334 ret
= setup_ima_buffer(image
, fdt
, chosen_node
);
336 pr_err("Error setting up the new device tree.\n");
340 ret
= fdt_setprop(fdt
, chosen_node
, "linux,booted-from-kexec", NULL
, 0);
342 pr_err("Error setting up the new device tree.\n");