]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - arch/powerpc/kernel/machine_kexec_file_64.c
Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/vfs
[mirror_ubuntu-bionic-kernel.git] / arch / powerpc / kernel / machine_kexec_file_64.c
1 /*
2 * ppc64 code to implement the kexec_file_load syscall
3 *
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
10 *
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>.
14 *
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).
18 *
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.
23 */
24
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>
30
31 #define SLAVE_CODE_SIZE 256
32
33 static struct kexec_file_ops *kexec_file_loaders[] = {
34 &kexec_elf64_ops,
35 };
36
37 int arch_kexec_kernel_image_probe(struct kimage *image, void *buf,
38 unsigned long buf_len)
39 {
40 int i, ret = -ENOEXEC;
41 struct kexec_file_ops *fops;
42
43 /* We don't support crash kernels yet. */
44 if (image->type == KEXEC_TYPE_CRASH)
45 return -ENOTSUPP;
46
47 for (i = 0; i < ARRAY_SIZE(kexec_file_loaders); i++) {
48 fops = kexec_file_loaders[i];
49 if (!fops || !fops->probe)
50 continue;
51
52 ret = fops->probe(buf, buf_len);
53 if (!ret) {
54 image->fops = fops;
55 return ret;
56 }
57 }
58
59 return ret;
60 }
61
62 void *arch_kexec_kernel_image_load(struct kimage *image)
63 {
64 if (!image->fops || !image->fops->load)
65 return ERR_PTR(-ENOEXEC);
66
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);
71 }
72
73 int arch_kimage_file_post_load_cleanup(struct kimage *image)
74 {
75 if (!image->fops || !image->fops->cleanup)
76 return 0;
77
78 return image->fops->cleanup(image->image_loader_data);
79 }
80
81 /**
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.
85 *
86 * This function is used by kexec_add_buffer and kexec_locate_mem_hole
87 * to find unreserved memory to load kexec segments into.
88 *
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.
92 */
93 int arch_kexec_walk_mem(struct kexec_buf *kbuf, int (*func)(u64, u64, void *))
94 {
95 int ret = 0;
96 u64 i;
97 phys_addr_t mstart, mend;
98
99 if (kbuf->top_down) {
100 for_each_free_mem_range_reverse(i, NUMA_NO_NODE, 0,
101 &mstart, &mend, NULL) {
102 /*
103 * In memblock, end points to the first byte after the
104 * range while in kexec, end points to the last byte
105 * in the range.
106 */
107 ret = func(mstart, mend - 1, kbuf);
108 if (ret)
109 break;
110 }
111 } else {
112 for_each_free_mem_range(i, NUMA_NO_NODE, 0, &mstart, &mend,
113 NULL) {
114 /*
115 * In memblock, end points to the first byte after the
116 * range while in kexec, end points to the last byte
117 * in the range.
118 */
119 ret = func(mstart, mend - 1, kbuf);
120 if (ret)
121 break;
122 }
123 }
124
125 return ret;
126 }
127
128 /**
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.
135 *
136 * Return: 0 on success, or negative errno on error.
137 */
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)
141 {
142 unsigned int *slave_code_buf, master_entry;
143 int ret;
144
145 slave_code_buf = kmalloc(SLAVE_CODE_SIZE, GFP_KERNEL);
146 if (!slave_code_buf)
147 return -ENOMEM;
148
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,
152 true);
153 if (ret) {
154 kfree(slave_code_buf);
155 return ret;
156 }
157
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,
163 false);
164 kfree(slave_code_buf);
165
166 ret = kexec_purgatory_get_set_symbol(image, "kernel", &kernel_load_addr,
167 sizeof(kernel_load_addr), false);
168 if (ret)
169 return ret;
170 ret = kexec_purgatory_get_set_symbol(image, "dt_offset", &fdt_load_addr,
171 sizeof(fdt_load_addr), false);
172 if (ret)
173 return ret;
174
175 return 0;
176 }
177
178 /**
179 * delete_fdt_mem_rsv - delete memory reservation with given address and size
180 *
181 * Return: 0 on success, or negative errno on error.
182 */
183 static int delete_fdt_mem_rsv(void *fdt, unsigned long start, unsigned long size)
184 {
185 int i, ret, num_rsvs = fdt_num_mem_rsv(fdt);
186
187 for (i = 0; i < num_rsvs; i++) {
188 uint64_t rsv_start, rsv_size;
189
190 ret = fdt_get_mem_rsv(fdt, i, &rsv_start, &rsv_size);
191 if (ret) {
192 pr_err("Malformed device tree.\n");
193 return -EINVAL;
194 }
195
196 if (rsv_start == start && rsv_size == size) {
197 ret = fdt_del_mem_rsv(fdt, i);
198 if (ret) {
199 pr_err("Error deleting device tree reservation.\n");
200 return -EINVAL;
201 }
202
203 return 0;
204 }
205 }
206
207 return -ENOENT;
208 }
209
210 /*
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
216 * be none.
217 *
218 * Return: 0 on success, or negative errno on error.
219 */
220 int setup_new_fdt(void *fdt, unsigned long initrd_load_addr,
221 unsigned long initrd_len, const char *cmdline)
222 {
223 int ret, chosen_node;
224 const void *prop;
225
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));
229 if (ret == 0)
230 pr_debug("Removed old device tree reservation.\n");
231 else if (ret != -ENOENT)
232 return ret;
233
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, "/"),
237 "chosen");
238 if (chosen_node < 0) {
239 pr_err("Error creating /chosen.\n");
240 return -EINVAL;
241 }
242 } else if (chosen_node < 0) {
243 pr_err("Malformed device tree: error reading /chosen.\n");
244 return -EINVAL;
245 }
246
247 /* Did we boot using an initrd? */
248 prop = fdt_getprop(fdt, chosen_node, "linux,initrd-start", NULL);
249 if (prop) {
250 uint64_t tmp_start, tmp_end, tmp_size;
251
252 tmp_start = fdt64_to_cpu(*((const fdt64_t *) prop));
253
254 prop = fdt_getprop(fdt, chosen_node, "linux,initrd-end", NULL);
255 if (!prop) {
256 pr_err("Malformed device tree.\n");
257 return -EINVAL;
258 }
259 tmp_end = fdt64_to_cpu(*((const fdt64_t *) prop));
260
261 /*
262 * kexec reserves exact initrd size, while firmware may
263 * reserve a multiple of PAGE_SIZE, so check for both.
264 */
265 tmp_size = tmp_end - tmp_start;
266 ret = delete_fdt_mem_rsv(fdt, tmp_start, tmp_size);
267 if (ret == -ENOENT)
268 ret = delete_fdt_mem_rsv(fdt, tmp_start,
269 round_up(tmp_size, PAGE_SIZE));
270 if (ret == 0)
271 pr_debug("Removed old initrd reservation.\n");
272 else if (ret != -ENOENT)
273 return ret;
274
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");
279 if (ret) {
280 pr_err("Error deleting linux,initrd-start.\n");
281 return -EINVAL;
282 }
283
284 ret = fdt_delprop(fdt, chosen_node, "linux,initrd-end");
285 if (ret) {
286 pr_err("Error deleting linux,initrd-end.\n");
287 return -EINVAL;
288 }
289 }
290 }
291
292 if (initrd_len) {
293 ret = fdt_setprop_u64(fdt, chosen_node,
294 "linux,initrd-start",
295 initrd_load_addr);
296 if (ret < 0) {
297 pr_err("Error setting up the new device tree.\n");
298 return -EINVAL;
299 }
300
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);
304 if (ret < 0) {
305 pr_err("Error setting up the new device tree.\n");
306 return -EINVAL;
307 }
308
309 ret = fdt_add_mem_rsv(fdt, initrd_load_addr, initrd_len);
310 if (ret) {
311 pr_err("Error reserving initrd memory: %s\n",
312 fdt_strerror(ret));
313 return -EINVAL;
314 }
315 }
316
317 if (cmdline != NULL) {
318 ret = fdt_setprop_string(fdt, chosen_node, "bootargs", cmdline);
319 if (ret < 0) {
320 pr_err("Error setting up the new device tree.\n");
321 return -EINVAL;
322 }
323 } else {
324 ret = fdt_delprop(fdt, chosen_node, "bootargs");
325 if (ret && ret != -FDT_ERR_NOTFOUND) {
326 pr_err("Error deleting bootargs.\n");
327 return -EINVAL;
328 }
329 }
330
331 ret = fdt_setprop(fdt, chosen_node, "linux,booted-from-kexec", NULL, 0);
332 if (ret) {
333 pr_err("Error setting up the new device tree.\n");
334 return -EINVAL;
335 }
336
337 return 0;
338 }