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