]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blob - arch/powerpc/kernel/machine_kexec_file_64.c
timekeeping: Repair ktime_get_coarse*() granularity
[mirror_ubuntu-jammy-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/of_fdt.h>
28 #include <linux/libfdt.h>
29 #include <asm/ima.h>
30
31 #define SLAVE_CODE_SIZE 256
32
33 const struct kexec_file_ops * const kexec_file_loaders[] = {
34 &kexec_elf64_ops,
35 NULL
36 };
37
38 int arch_kexec_kernel_image_probe(struct kimage *image, void *buf,
39 unsigned long buf_len)
40 {
41 /* We don't support crash kernels yet. */
42 if (image->type == KEXEC_TYPE_CRASH)
43 return -EOPNOTSUPP;
44
45 return kexec_image_probe_default(image, buf, buf_len);
46 }
47
48 /**
49 * setup_purgatory - initialize the purgatory's global variables
50 * @image: kexec image.
51 * @slave_code: Slave code for the purgatory.
52 * @fdt: Flattened device tree for the next kernel.
53 * @kernel_load_addr: Address where the kernel is loaded.
54 * @fdt_load_addr: Address where the flattened device tree is loaded.
55 *
56 * Return: 0 on success, or negative errno on error.
57 */
58 int setup_purgatory(struct kimage *image, const void *slave_code,
59 const void *fdt, unsigned long kernel_load_addr,
60 unsigned long fdt_load_addr)
61 {
62 unsigned int *slave_code_buf, master_entry;
63 int ret;
64
65 slave_code_buf = kmalloc(SLAVE_CODE_SIZE, GFP_KERNEL);
66 if (!slave_code_buf)
67 return -ENOMEM;
68
69 /* Get the slave code from the new kernel and put it in purgatory. */
70 ret = kexec_purgatory_get_set_symbol(image, "purgatory_start",
71 slave_code_buf, SLAVE_CODE_SIZE,
72 true);
73 if (ret) {
74 kfree(slave_code_buf);
75 return ret;
76 }
77
78 master_entry = slave_code_buf[0];
79 memcpy(slave_code_buf, slave_code, SLAVE_CODE_SIZE);
80 slave_code_buf[0] = master_entry;
81 ret = kexec_purgatory_get_set_symbol(image, "purgatory_start",
82 slave_code_buf, SLAVE_CODE_SIZE,
83 false);
84 kfree(slave_code_buf);
85
86 ret = kexec_purgatory_get_set_symbol(image, "kernel", &kernel_load_addr,
87 sizeof(kernel_load_addr), false);
88 if (ret)
89 return ret;
90 ret = kexec_purgatory_get_set_symbol(image, "dt_offset", &fdt_load_addr,
91 sizeof(fdt_load_addr), false);
92 if (ret)
93 return ret;
94
95 return 0;
96 }
97
98 /**
99 * delete_fdt_mem_rsv - delete memory reservation with given address and size
100 *
101 * Return: 0 on success, or negative errno on error.
102 */
103 int delete_fdt_mem_rsv(void *fdt, unsigned long start, unsigned long size)
104 {
105 int i, ret, num_rsvs = fdt_num_mem_rsv(fdt);
106
107 for (i = 0; i < num_rsvs; i++) {
108 uint64_t rsv_start, rsv_size;
109
110 ret = fdt_get_mem_rsv(fdt, i, &rsv_start, &rsv_size);
111 if (ret) {
112 pr_err("Malformed device tree.\n");
113 return -EINVAL;
114 }
115
116 if (rsv_start == start && rsv_size == size) {
117 ret = fdt_del_mem_rsv(fdt, i);
118 if (ret) {
119 pr_err("Error deleting device tree reservation.\n");
120 return -EINVAL;
121 }
122
123 return 0;
124 }
125 }
126
127 return -ENOENT;
128 }
129
130 /*
131 * setup_new_fdt - modify /chosen and memory reservation for the next kernel
132 * @image: kexec image being loaded.
133 * @fdt: Flattened device tree for the next kernel.
134 * @initrd_load_addr: Address where the next initrd will be loaded.
135 * @initrd_len: Size of the next initrd, or 0 if there will be none.
136 * @cmdline: Command line for the next kernel, or NULL if there will
137 * be none.
138 *
139 * Return: 0 on success, or negative errno on error.
140 */
141 int setup_new_fdt(const struct kimage *image, void *fdt,
142 unsigned long initrd_load_addr, unsigned long initrd_len,
143 const char *cmdline)
144 {
145 int ret, chosen_node;
146 const void *prop;
147
148 /* Remove memory reservation for the current device tree. */
149 ret = delete_fdt_mem_rsv(fdt, __pa(initial_boot_params),
150 fdt_totalsize(initial_boot_params));
151 if (ret == 0)
152 pr_debug("Removed old device tree reservation.\n");
153 else if (ret != -ENOENT)
154 return ret;
155
156 chosen_node = fdt_path_offset(fdt, "/chosen");
157 if (chosen_node == -FDT_ERR_NOTFOUND) {
158 chosen_node = fdt_add_subnode(fdt, fdt_path_offset(fdt, "/"),
159 "chosen");
160 if (chosen_node < 0) {
161 pr_err("Error creating /chosen.\n");
162 return -EINVAL;
163 }
164 } else if (chosen_node < 0) {
165 pr_err("Malformed device tree: error reading /chosen.\n");
166 return -EINVAL;
167 }
168
169 /* Did we boot using an initrd? */
170 prop = fdt_getprop(fdt, chosen_node, "linux,initrd-start", NULL);
171 if (prop) {
172 uint64_t tmp_start, tmp_end, tmp_size;
173
174 tmp_start = fdt64_to_cpu(*((const fdt64_t *) prop));
175
176 prop = fdt_getprop(fdt, chosen_node, "linux,initrd-end", NULL);
177 if (!prop) {
178 pr_err("Malformed device tree.\n");
179 return -EINVAL;
180 }
181 tmp_end = fdt64_to_cpu(*((const fdt64_t *) prop));
182
183 /*
184 * kexec reserves exact initrd size, while firmware may
185 * reserve a multiple of PAGE_SIZE, so check for both.
186 */
187 tmp_size = tmp_end - tmp_start;
188 ret = delete_fdt_mem_rsv(fdt, tmp_start, tmp_size);
189 if (ret == -ENOENT)
190 ret = delete_fdt_mem_rsv(fdt, tmp_start,
191 round_up(tmp_size, PAGE_SIZE));
192 if (ret == 0)
193 pr_debug("Removed old initrd reservation.\n");
194 else if (ret != -ENOENT)
195 return ret;
196
197 /* If there's no new initrd, delete the old initrd's info. */
198 if (initrd_len == 0) {
199 ret = fdt_delprop(fdt, chosen_node,
200 "linux,initrd-start");
201 if (ret) {
202 pr_err("Error deleting linux,initrd-start.\n");
203 return -EINVAL;
204 }
205
206 ret = fdt_delprop(fdt, chosen_node, "linux,initrd-end");
207 if (ret) {
208 pr_err("Error deleting linux,initrd-end.\n");
209 return -EINVAL;
210 }
211 }
212 }
213
214 if (initrd_len) {
215 ret = fdt_setprop_u64(fdt, chosen_node,
216 "linux,initrd-start",
217 initrd_load_addr);
218 if (ret < 0)
219 goto err;
220
221 /* initrd-end is the first address after the initrd image. */
222 ret = fdt_setprop_u64(fdt, chosen_node, "linux,initrd-end",
223 initrd_load_addr + initrd_len);
224 if (ret < 0)
225 goto err;
226
227 ret = fdt_add_mem_rsv(fdt, initrd_load_addr, initrd_len);
228 if (ret) {
229 pr_err("Error reserving initrd memory: %s\n",
230 fdt_strerror(ret));
231 return -EINVAL;
232 }
233 }
234
235 if (cmdline != NULL) {
236 ret = fdt_setprop_string(fdt, chosen_node, "bootargs", cmdline);
237 if (ret < 0)
238 goto err;
239 } else {
240 ret = fdt_delprop(fdt, chosen_node, "bootargs");
241 if (ret && ret != -FDT_ERR_NOTFOUND) {
242 pr_err("Error deleting bootargs.\n");
243 return -EINVAL;
244 }
245 }
246
247 ret = setup_ima_buffer(image, fdt, chosen_node);
248 if (ret) {
249 pr_err("Error setting up the new device tree.\n");
250 return ret;
251 }
252
253 ret = fdt_setprop(fdt, chosen_node, "linux,booted-from-kexec", NULL, 0);
254 if (ret)
255 goto err;
256
257 return 0;
258
259 err:
260 pr_err("Error setting up the new device tree.\n");
261 return -EINVAL;
262 }