]> asedeno.scripts.mit.edu Git - linux.git/blob - arch/powerpc/kernel/machine_kexec_file_64.c
Merge tag 'powerpc-4.17-1' of git://git.kernel.org/pub/scm/linux/kernel/git/powerpc/linux
[linux.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 #include <asm/ima.h>
31
32 #define SLAVE_CODE_SIZE         256
33
34 static struct kexec_file_ops *kexec_file_loaders[] = {
35         &kexec_elf64_ops,
36 };
37
38 int arch_kexec_kernel_image_probe(struct kimage *image, void *buf,
39                                   unsigned long buf_len)
40 {
41         int i, ret = -ENOEXEC;
42         struct kexec_file_ops *fops;
43
44         /* We don't support crash kernels yet. */
45         if (image->type == KEXEC_TYPE_CRASH)
46                 return -EOPNOTSUPP;
47
48         for (i = 0; i < ARRAY_SIZE(kexec_file_loaders); i++) {
49                 fops = kexec_file_loaders[i];
50                 if (!fops || !fops->probe)
51                         continue;
52
53                 ret = fops->probe(buf, buf_len);
54                 if (!ret) {
55                         image->fops = fops;
56                         return ret;
57                 }
58         }
59
60         return ret;
61 }
62
63 void *arch_kexec_kernel_image_load(struct kimage *image)
64 {
65         if (!image->fops || !image->fops->load)
66                 return ERR_PTR(-ENOEXEC);
67
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);
72 }
73
74 int arch_kimage_file_post_load_cleanup(struct kimage *image)
75 {
76         if (!image->fops || !image->fops->cleanup)
77                 return 0;
78
79         return image->fops->cleanup(image->image_loader_data);
80 }
81
82 /**
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.
86  *
87  * This function is used by kexec_add_buffer and kexec_locate_mem_hole
88  * to find unreserved memory to load kexec segments into.
89  *
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.
93  */
94 int arch_kexec_walk_mem(struct kexec_buf *kbuf,
95                         int (*func)(struct resource *, void *))
96 {
97         int ret = 0;
98         u64 i;
99         phys_addr_t mstart, mend;
100         struct resource res = { };
101
102         if (kbuf->top_down) {
103                 for_each_free_mem_range_reverse(i, NUMA_NO_NODE, 0,
104                                                 &mstart, &mend, NULL) {
105                         /*
106                          * In memblock, end points to the first byte after the
107                          * range while in kexec, end points to the last byte
108                          * in the range.
109                          */
110                         res.start = mstart;
111                         res.end = mend - 1;
112                         ret = func(&res, kbuf);
113                         if (ret)
114                                 break;
115                 }
116         } else {
117                 for_each_free_mem_range(i, NUMA_NO_NODE, 0, &mstart, &mend,
118                                         NULL) {
119                         /*
120                          * In memblock, end points to the first byte after the
121                          * range while in kexec, end points to the last byte
122                          * in the range.
123                          */
124                         res.start = mstart;
125                         res.end = mend - 1;
126                         ret = func(&res, kbuf);
127                         if (ret)
128                                 break;
129                 }
130         }
131
132         return ret;
133 }
134
135 /**
136  * setup_purgatory - initialize the purgatory's global variables
137  * @image:              kexec image.
138  * @slave_code:         Slave code for the purgatory.
139  * @fdt:                Flattened device tree for the next kernel.
140  * @kernel_load_addr:   Address where the kernel is loaded.
141  * @fdt_load_addr:      Address where the flattened device tree is loaded.
142  *
143  * Return: 0 on success, or negative errno on error.
144  */
145 int setup_purgatory(struct kimage *image, const void *slave_code,
146                     const void *fdt, unsigned long kernel_load_addr,
147                     unsigned long fdt_load_addr)
148 {
149         unsigned int *slave_code_buf, master_entry;
150         int ret;
151
152         slave_code_buf = kmalloc(SLAVE_CODE_SIZE, GFP_KERNEL);
153         if (!slave_code_buf)
154                 return -ENOMEM;
155
156         /* Get the slave code from the new kernel and put it in purgatory. */
157         ret = kexec_purgatory_get_set_symbol(image, "purgatory_start",
158                                              slave_code_buf, SLAVE_CODE_SIZE,
159                                              true);
160         if (ret) {
161                 kfree(slave_code_buf);
162                 return ret;
163         }
164
165         master_entry = slave_code_buf[0];
166         memcpy(slave_code_buf, slave_code, SLAVE_CODE_SIZE);
167         slave_code_buf[0] = master_entry;
168         ret = kexec_purgatory_get_set_symbol(image, "purgatory_start",
169                                              slave_code_buf, SLAVE_CODE_SIZE,
170                                              false);
171         kfree(slave_code_buf);
172
173         ret = kexec_purgatory_get_set_symbol(image, "kernel", &kernel_load_addr,
174                                              sizeof(kernel_load_addr), false);
175         if (ret)
176                 return ret;
177         ret = kexec_purgatory_get_set_symbol(image, "dt_offset", &fdt_load_addr,
178                                              sizeof(fdt_load_addr), false);
179         if (ret)
180                 return ret;
181
182         return 0;
183 }
184
185 /**
186  * delete_fdt_mem_rsv - delete memory reservation with given address and size
187  *
188  * Return: 0 on success, or negative errno on error.
189  */
190 int delete_fdt_mem_rsv(void *fdt, unsigned long start, unsigned long size)
191 {
192         int i, ret, num_rsvs = fdt_num_mem_rsv(fdt);
193
194         for (i = 0; i < num_rsvs; i++) {
195                 uint64_t rsv_start, rsv_size;
196
197                 ret = fdt_get_mem_rsv(fdt, i, &rsv_start, &rsv_size);
198                 if (ret) {
199                         pr_err("Malformed device tree.\n");
200                         return -EINVAL;
201                 }
202
203                 if (rsv_start == start && rsv_size == size) {
204                         ret = fdt_del_mem_rsv(fdt, i);
205                         if (ret) {
206                                 pr_err("Error deleting device tree reservation.\n");
207                                 return -EINVAL;
208                         }
209
210                         return 0;
211                 }
212         }
213
214         return -ENOENT;
215 }
216
217 /*
218  * setup_new_fdt - modify /chosen and memory reservation for the next kernel
219  * @image:              kexec image being loaded.
220  * @fdt:                Flattened device tree for the next kernel.
221  * @initrd_load_addr:   Address where the next initrd will be loaded.
222  * @initrd_len:         Size of the next initrd, or 0 if there will be none.
223  * @cmdline:            Command line for the next kernel, or NULL if there will
224  *                      be none.
225  *
226  * Return: 0 on success, or negative errno on error.
227  */
228 int setup_new_fdt(const struct kimage *image, void *fdt,
229                   unsigned long initrd_load_addr, unsigned long initrd_len,
230                   const char *cmdline)
231 {
232         int ret, chosen_node;
233         const void *prop;
234
235         /* Remove memory reservation for the current device tree. */
236         ret = delete_fdt_mem_rsv(fdt, __pa(initial_boot_params),
237                                  fdt_totalsize(initial_boot_params));
238         if (ret == 0)
239                 pr_debug("Removed old device tree reservation.\n");
240         else if (ret != -ENOENT)
241                 return ret;
242
243         chosen_node = fdt_path_offset(fdt, "/chosen");
244         if (chosen_node == -FDT_ERR_NOTFOUND) {
245                 chosen_node = fdt_add_subnode(fdt, fdt_path_offset(fdt, "/"),
246                                               "chosen");
247                 if (chosen_node < 0) {
248                         pr_err("Error creating /chosen.\n");
249                         return -EINVAL;
250                 }
251         } else if (chosen_node < 0) {
252                 pr_err("Malformed device tree: error reading /chosen.\n");
253                 return -EINVAL;
254         }
255
256         /* Did we boot using an initrd? */
257         prop = fdt_getprop(fdt, chosen_node, "linux,initrd-start", NULL);
258         if (prop) {
259                 uint64_t tmp_start, tmp_end, tmp_size;
260
261                 tmp_start = fdt64_to_cpu(*((const fdt64_t *) prop));
262
263                 prop = fdt_getprop(fdt, chosen_node, "linux,initrd-end", NULL);
264                 if (!prop) {
265                         pr_err("Malformed device tree.\n");
266                         return -EINVAL;
267                 }
268                 tmp_end = fdt64_to_cpu(*((const fdt64_t *) prop));
269
270                 /*
271                  * kexec reserves exact initrd size, while firmware may
272                  * reserve a multiple of PAGE_SIZE, so check for both.
273                  */
274                 tmp_size = tmp_end - tmp_start;
275                 ret = delete_fdt_mem_rsv(fdt, tmp_start, tmp_size);
276                 if (ret == -ENOENT)
277                         ret = delete_fdt_mem_rsv(fdt, tmp_start,
278                                                  round_up(tmp_size, PAGE_SIZE));
279                 if (ret == 0)
280                         pr_debug("Removed old initrd reservation.\n");
281                 else if (ret != -ENOENT)
282                         return ret;
283
284                 /* If there's no new initrd, delete the old initrd's info. */
285                 if (initrd_len == 0) {
286                         ret = fdt_delprop(fdt, chosen_node,
287                                           "linux,initrd-start");
288                         if (ret) {
289                                 pr_err("Error deleting linux,initrd-start.\n");
290                                 return -EINVAL;
291                         }
292
293                         ret = fdt_delprop(fdt, chosen_node, "linux,initrd-end");
294                         if (ret) {
295                                 pr_err("Error deleting linux,initrd-end.\n");
296                                 return -EINVAL;
297                         }
298                 }
299         }
300
301         if (initrd_len) {
302                 ret = fdt_setprop_u64(fdt, chosen_node,
303                                       "linux,initrd-start",
304                                       initrd_load_addr);
305                 if (ret < 0) {
306                         pr_err("Error setting up the new device tree.\n");
307                         return -EINVAL;
308                 }
309
310                 /* initrd-end is the first address after the initrd image. */
311                 ret = fdt_setprop_u64(fdt, chosen_node, "linux,initrd-end",
312                                       initrd_load_addr + initrd_len);
313                 if (ret < 0) {
314                         pr_err("Error setting up the new device tree.\n");
315                         return -EINVAL;
316                 }
317
318                 ret = fdt_add_mem_rsv(fdt, initrd_load_addr, initrd_len);
319                 if (ret) {
320                         pr_err("Error reserving initrd memory: %s\n",
321                                fdt_strerror(ret));
322                         return -EINVAL;
323                 }
324         }
325
326         if (cmdline != NULL) {
327                 ret = fdt_setprop_string(fdt, chosen_node, "bootargs", cmdline);
328                 if (ret < 0) {
329                         pr_err("Error setting up the new device tree.\n");
330                         return -EINVAL;
331                 }
332         } else {
333                 ret = fdt_delprop(fdt, chosen_node, "bootargs");
334                 if (ret && ret != -FDT_ERR_NOTFOUND) {
335                         pr_err("Error deleting bootargs.\n");
336                         return -EINVAL;
337                 }
338         }
339
340         ret = setup_ima_buffer(image, fdt, chosen_node);
341         if (ret) {
342                 pr_err("Error setting up the new device tree.\n");
343                 return ret;
344         }
345
346         ret = fdt_setprop(fdt, chosen_node, "linux,booted-from-kexec", NULL, 0);
347         if (ret) {
348                 pr_err("Error setting up the new device tree.\n");
349                 return -EINVAL;
350         }
351
352         return 0;
353 }