machine_kexec_file_64.c 8.6 KB

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