]>
git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - arch/powerpc/kernel/machine_kexec_file_64.c
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>
31 #define SLAVE_CODE_SIZE 256
33 static struct kexec_file_ops
*kexec_file_loaders
[] = {
37 int arch_kexec_kernel_image_probe(struct kimage
*image
, void *buf
,
38 unsigned long buf_len
)
40 int i
, ret
= -ENOEXEC
;
41 struct kexec_file_ops
*fops
;
43 /* We don't support crash kernels yet. */
44 if (image
->type
== KEXEC_TYPE_CRASH
)
47 for (i
= 0; i
< ARRAY_SIZE(kexec_file_loaders
); i
++) {
48 fops
= kexec_file_loaders
[i
];
49 if (!fops
|| !fops
->probe
)
52 ret
= fops
->probe(buf
, buf_len
);
62 void *arch_kexec_kernel_image_load(struct kimage
*image
)
64 if (!image
->fops
|| !image
->fops
->load
)
65 return ERR_PTR(-ENOEXEC
);
67 return image
->fops
->load(image
, image
->kernel_buf
,
68 image
->kernel_buf_len
, image
->initrd_buf
,
69 image
->initrd_buf_len
, image
->cmdline_buf
,
70 image
->cmdline_buf_len
);
73 int arch_kimage_file_post_load_cleanup(struct kimage
*image
)
75 if (!image
->fops
|| !image
->fops
->cleanup
)
78 return image
->fops
->cleanup(image
->image_loader_data
);
82 * arch_kexec_walk_mem - call func(data) for each unreserved memory block
83 * @kbuf: Context info for the search. Also passed to @func.
84 * @func: Function to call for each memory block.
86 * This function is used by kexec_add_buffer and kexec_locate_mem_hole
87 * to find unreserved memory to load kexec segments into.
89 * Return: The memory walk will stop when func returns a non-zero value
90 * and that value will be returned. If all free regions are visited without
91 * func returning non-zero, then zero will be returned.
93 int arch_kexec_walk_mem(struct kexec_buf
*kbuf
, int (*func
)(u64
, u64
, void *))
97 phys_addr_t mstart
, mend
;
100 for_each_free_mem_range_reverse(i
, NUMA_NO_NODE
, 0,
101 &mstart
, &mend
, NULL
) {
103 * In memblock, end points to the first byte after the
104 * range while in kexec, end points to the last byte
107 ret
= func(mstart
, mend
- 1, kbuf
);
112 for_each_free_mem_range(i
, NUMA_NO_NODE
, 0, &mstart
, &mend
,
115 * In memblock, end points to the first byte after the
116 * range while in kexec, end points to the last byte
119 ret
= func(mstart
, mend
- 1, kbuf
);
129 * setup_purgatory - initialize the purgatory's global variables
130 * @image: kexec image.
131 * @slave_code: Slave code for the purgatory.
132 * @fdt: Flattened device tree for the next kernel.
133 * @kernel_load_addr: Address where the kernel is loaded.
134 * @fdt_load_addr: Address where the flattened device tree is loaded.
136 * Return: 0 on success, or negative errno on error.
138 int setup_purgatory(struct kimage
*image
, const void *slave_code
,
139 const void *fdt
, unsigned long kernel_load_addr
,
140 unsigned long fdt_load_addr
)
142 unsigned int *slave_code_buf
, master_entry
;
145 slave_code_buf
= kmalloc(SLAVE_CODE_SIZE
, GFP_KERNEL
);
149 /* Get the slave code from the new kernel and put it in purgatory. */
150 ret
= kexec_purgatory_get_set_symbol(image
, "purgatory_start",
151 slave_code_buf
, SLAVE_CODE_SIZE
,
154 kfree(slave_code_buf
);
158 master_entry
= slave_code_buf
[0];
159 memcpy(slave_code_buf
, slave_code
, SLAVE_CODE_SIZE
);
160 slave_code_buf
[0] = master_entry
;
161 ret
= kexec_purgatory_get_set_symbol(image
, "purgatory_start",
162 slave_code_buf
, SLAVE_CODE_SIZE
,
164 kfree(slave_code_buf
);
166 ret
= kexec_purgatory_get_set_symbol(image
, "kernel", &kernel_load_addr
,
167 sizeof(kernel_load_addr
), false);
170 ret
= kexec_purgatory_get_set_symbol(image
, "dt_offset", &fdt_load_addr
,
171 sizeof(fdt_load_addr
), false);
179 * delete_fdt_mem_rsv - delete memory reservation with given address and size
181 * Return: 0 on success, or negative errno on error.
183 static int delete_fdt_mem_rsv(void *fdt
, unsigned long start
, unsigned long size
)
185 int i
, ret
, num_rsvs
= fdt_num_mem_rsv(fdt
);
187 for (i
= 0; i
< num_rsvs
; i
++) {
188 uint64_t rsv_start
, rsv_size
;
190 ret
= fdt_get_mem_rsv(fdt
, i
, &rsv_start
, &rsv_size
);
192 pr_err("Malformed device tree.\n");
196 if (rsv_start
== start
&& rsv_size
== size
) {
197 ret
= fdt_del_mem_rsv(fdt
, i
);
199 pr_err("Error deleting device tree reservation.\n");
211 * setup_new_fdt - modify /chosen and memory reservation for the next kernel
212 * @fdt: Flattened device tree for the next kernel.
213 * @initrd_load_addr: Address where the next initrd will be loaded.
214 * @initrd_len: Size of the next initrd, or 0 if there will be none.
215 * @cmdline: Command line for the next kernel, or NULL if there will
218 * Return: 0 on success, or negative errno on error.
220 int setup_new_fdt(void *fdt
, unsigned long initrd_load_addr
,
221 unsigned long initrd_len
, const char *cmdline
)
223 int ret
, chosen_node
;
226 /* Remove memory reservation for the current device tree. */
227 ret
= delete_fdt_mem_rsv(fdt
, __pa(initial_boot_params
),
228 fdt_totalsize(initial_boot_params
));
230 pr_debug("Removed old device tree reservation.\n");
231 else if (ret
!= -ENOENT
)
234 chosen_node
= fdt_path_offset(fdt
, "/chosen");
235 if (chosen_node
== -FDT_ERR_NOTFOUND
) {
236 chosen_node
= fdt_add_subnode(fdt
, fdt_path_offset(fdt
, "/"),
238 if (chosen_node
< 0) {
239 pr_err("Error creating /chosen.\n");
242 } else if (chosen_node
< 0) {
243 pr_err("Malformed device tree: error reading /chosen.\n");
247 /* Did we boot using an initrd? */
248 prop
= fdt_getprop(fdt
, chosen_node
, "linux,initrd-start", NULL
);
250 uint64_t tmp_start
, tmp_end
, tmp_size
;
252 tmp_start
= fdt64_to_cpu(*((const fdt64_t
*) prop
));
254 prop
= fdt_getprop(fdt
, chosen_node
, "linux,initrd-end", NULL
);
256 pr_err("Malformed device tree.\n");
259 tmp_end
= fdt64_to_cpu(*((const fdt64_t
*) prop
));
262 * kexec reserves exact initrd size, while firmware may
263 * reserve a multiple of PAGE_SIZE, so check for both.
265 tmp_size
= tmp_end
- tmp_start
;
266 ret
= delete_fdt_mem_rsv(fdt
, tmp_start
, tmp_size
);
268 ret
= delete_fdt_mem_rsv(fdt
, tmp_start
,
269 round_up(tmp_size
, PAGE_SIZE
));
271 pr_debug("Removed old initrd reservation.\n");
272 else if (ret
!= -ENOENT
)
275 /* If there's no new initrd, delete the old initrd's info. */
276 if (initrd_len
== 0) {
277 ret
= fdt_delprop(fdt
, chosen_node
,
278 "linux,initrd-start");
280 pr_err("Error deleting linux,initrd-start.\n");
284 ret
= fdt_delprop(fdt
, chosen_node
, "linux,initrd-end");
286 pr_err("Error deleting linux,initrd-end.\n");
293 ret
= fdt_setprop_u64(fdt
, chosen_node
,
294 "linux,initrd-start",
297 pr_err("Error setting up the new device tree.\n");
301 /* initrd-end is the first address after the initrd image. */
302 ret
= fdt_setprop_u64(fdt
, chosen_node
, "linux,initrd-end",
303 initrd_load_addr
+ initrd_len
);
305 pr_err("Error setting up the new device tree.\n");
309 ret
= fdt_add_mem_rsv(fdt
, initrd_load_addr
, initrd_len
);
311 pr_err("Error reserving initrd memory: %s\n",
317 if (cmdline
!= NULL
) {
318 ret
= fdt_setprop_string(fdt
, chosen_node
, "bootargs", cmdline
);
320 pr_err("Error setting up the new device tree.\n");
324 ret
= fdt_delprop(fdt
, chosen_node
, "bootargs");
325 if (ret
&& ret
!= -FDT_ERR_NOTFOUND
) {
326 pr_err("Error deleting bootargs.\n");
331 ret
= fdt_setprop(fdt
, chosen_node
, "linux,booted-from-kexec", NULL
, 0);
333 pr_err("Error setting up the new device tree.\n");