machine_kexec.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * kexec for arm64
  4. *
  5. * Copyright (C) Linaro.
  6. * Copyright (C) Huawei Futurewei Technologies.
  7. */
  8. #include <linux/interrupt.h>
  9. #include <linux/irq.h>
  10. #include <linux/kernel.h>
  11. #include <linux/kexec.h>
  12. #include <linux/page-flags.h>
  13. #include <linux/reboot.h>
  14. #include <linux/set_memory.h>
  15. #include <linux/smp.h>
  16. #include <asm/cacheflush.h>
  17. #include <asm/cpu_ops.h>
  18. #include <asm/daifflags.h>
  19. #include <asm/memory.h>
  20. #include <asm/mmu.h>
  21. #include <asm/mmu_context.h>
  22. #include <asm/page.h>
  23. #include <asm/sections.h>
  24. #include <asm/trans_pgd.h>
  25. /**
  26. * kexec_image_info - For debugging output.
  27. */
  28. #define kexec_image_info(_i) _kexec_image_info(__func__, __LINE__, _i)
  29. static void _kexec_image_info(const char *func, int line,
  30. const struct kimage *kimage)
  31. {
  32. kexec_dprintk("%s:%d:\n", func, line);
  33. kexec_dprintk(" kexec kimage info:\n");
  34. kexec_dprintk(" type: %d\n", kimage->type);
  35. kexec_dprintk(" head: %lx\n", kimage->head);
  36. kexec_dprintk(" kern_reloc: %pa\n", &kimage->arch.kern_reloc);
  37. kexec_dprintk(" el2_vectors: %pa\n", &kimage->arch.el2_vectors);
  38. }
  39. void machine_kexec_cleanup(struct kimage *kimage)
  40. {
  41. /* Empty routine needed to avoid build errors. */
  42. }
  43. /**
  44. * machine_kexec_prepare - Prepare for a kexec reboot.
  45. *
  46. * Called from the core kexec code when a kernel image is loaded.
  47. * Forbid loading a kexec kernel if we have no way of hotplugging cpus or cpus
  48. * are stuck in the kernel. This avoids a panic once we hit machine_kexec().
  49. */
  50. int machine_kexec_prepare(struct kimage *kimage)
  51. {
  52. if (kimage->type != KEXEC_TYPE_CRASH && cpus_are_stuck_in_kernel()) {
  53. pr_err("Can't kexec: CPUs are stuck in the kernel.\n");
  54. return -EBUSY;
  55. }
  56. return 0;
  57. }
  58. /**
  59. * kexec_segment_flush - Helper to flush the kimage segments to PoC.
  60. */
  61. static void kexec_segment_flush(const struct kimage *kimage)
  62. {
  63. unsigned long i;
  64. pr_debug("%s:\n", __func__);
  65. for (i = 0; i < kimage->nr_segments; i++) {
  66. pr_debug(" segment[%lu]: %016lx - %016lx, 0x%lx bytes, %lu pages\n",
  67. i,
  68. kimage->segment[i].mem,
  69. kimage->segment[i].mem + kimage->segment[i].memsz,
  70. kimage->segment[i].memsz,
  71. kimage->segment[i].memsz / PAGE_SIZE);
  72. dcache_clean_inval_poc(
  73. (unsigned long)phys_to_virt(kimage->segment[i].mem),
  74. (unsigned long)phys_to_virt(kimage->segment[i].mem) +
  75. kimage->segment[i].memsz);
  76. }
  77. }
  78. /* Allocates pages for kexec page table */
  79. static void *kexec_page_alloc(void *arg)
  80. {
  81. struct kimage *kimage = arg;
  82. struct page *page = kimage_alloc_control_pages(kimage, 0);
  83. void *vaddr = NULL;
  84. if (!page)
  85. return NULL;
  86. vaddr = page_address(page);
  87. memset(vaddr, 0, PAGE_SIZE);
  88. return vaddr;
  89. }
  90. int machine_kexec_post_load(struct kimage *kimage)
  91. {
  92. int rc;
  93. pgd_t *trans_pgd;
  94. void *reloc_code = page_to_virt(kimage->control_code_page);
  95. long reloc_size;
  96. struct trans_pgd_info info = {
  97. .trans_alloc_page = kexec_page_alloc,
  98. .trans_alloc_arg = kimage,
  99. };
  100. /* If in place, relocation is not used, only flush next kernel */
  101. if (kimage->head & IND_DONE) {
  102. kexec_segment_flush(kimage);
  103. kexec_image_info(kimage);
  104. return 0;
  105. }
  106. kimage->arch.el2_vectors = 0;
  107. if (is_hyp_nvhe()) {
  108. rc = trans_pgd_copy_el2_vectors(&info,
  109. &kimage->arch.el2_vectors);
  110. if (rc)
  111. return rc;
  112. }
  113. /* Create a copy of the linear map */
  114. trans_pgd = kexec_page_alloc(kimage);
  115. if (!trans_pgd)
  116. return -ENOMEM;
  117. rc = trans_pgd_create_copy(&info, &trans_pgd, PAGE_OFFSET, PAGE_END);
  118. if (rc)
  119. return rc;
  120. kimage->arch.ttbr1 = __pa(trans_pgd);
  121. kimage->arch.zero_page = __pa_symbol(empty_zero_page);
  122. reloc_size = __relocate_new_kernel_end - __relocate_new_kernel_start;
  123. memcpy(reloc_code, __relocate_new_kernel_start, reloc_size);
  124. kimage->arch.kern_reloc = __pa(reloc_code);
  125. rc = trans_pgd_idmap_page(&info, &kimage->arch.ttbr0,
  126. &kimage->arch.t0sz, reloc_code);
  127. if (rc)
  128. return rc;
  129. kimage->arch.phys_offset = virt_to_phys(kimage) - (long)kimage;
  130. /* Flush the reloc_code in preparation for its execution. */
  131. dcache_clean_inval_poc((unsigned long)reloc_code,
  132. (unsigned long)reloc_code + reloc_size);
  133. icache_inval_pou((uintptr_t)reloc_code,
  134. (uintptr_t)reloc_code + reloc_size);
  135. kexec_image_info(kimage);
  136. return 0;
  137. }
  138. /**
  139. * machine_kexec - Do the kexec reboot.
  140. *
  141. * Called from the core kexec code for a sys_reboot with LINUX_REBOOT_CMD_KEXEC.
  142. */
  143. void machine_kexec(struct kimage *kimage)
  144. {
  145. bool in_kexec_crash = (kimage == kexec_crash_image);
  146. bool stuck_cpus = cpus_are_stuck_in_kernel();
  147. /*
  148. * New cpus may have become stuck_in_kernel after we loaded the image.
  149. */
  150. BUG_ON(!in_kexec_crash && (stuck_cpus || (num_online_cpus() > 1)));
  151. WARN(in_kexec_crash && (stuck_cpus || smp_crash_stop_failed()),
  152. "Some CPUs may be stale, kdump will be unreliable.\n");
  153. pr_info("Bye!\n");
  154. local_daif_mask();
  155. /*
  156. * Both restart and kernel_reloc will shutdown the MMU, disable data
  157. * caches. However, restart will start new kernel or purgatory directly,
  158. * kernel_reloc contains the body of arm64_relocate_new_kernel
  159. * In kexec case, kimage->start points to purgatory assuming that
  160. * kernel entry and dtb address are embedded in purgatory by
  161. * userspace (kexec-tools).
  162. * In kexec_file case, the kernel starts directly without purgatory.
  163. */
  164. if (kimage->head & IND_DONE) {
  165. typeof(cpu_soft_restart) *restart;
  166. cpu_install_idmap();
  167. restart = (void *)__pa_symbol(cpu_soft_restart);
  168. restart(is_hyp_nvhe(), kimage->start, kimage->arch.dtb_mem,
  169. 0, 0);
  170. } else {
  171. void (*kernel_reloc)(struct kimage *kimage);
  172. if (is_hyp_nvhe())
  173. __hyp_set_vectors(kimage->arch.el2_vectors);
  174. cpu_install_ttbr0(kimage->arch.ttbr0, kimage->arch.t0sz);
  175. kernel_reloc = (void *)kimage->arch.kern_reloc;
  176. kernel_reloc(kimage);
  177. }
  178. BUG(); /* Should never get here. */
  179. }
  180. static void machine_kexec_mask_interrupts(void)
  181. {
  182. unsigned int i;
  183. struct irq_desc *desc;
  184. for_each_irq_desc(i, desc) {
  185. struct irq_chip *chip;
  186. int ret;
  187. chip = irq_desc_get_chip(desc);
  188. if (!chip)
  189. continue;
  190. /*
  191. * First try to remove the active state. If this
  192. * fails, try to EOI the interrupt.
  193. */
  194. ret = irq_set_irqchip_state(i, IRQCHIP_STATE_ACTIVE, false);
  195. if (ret && irqd_irq_inprogress(&desc->irq_data) &&
  196. chip->irq_eoi)
  197. chip->irq_eoi(&desc->irq_data);
  198. if (chip->irq_mask)
  199. chip->irq_mask(&desc->irq_data);
  200. if (chip->irq_disable && !irqd_irq_disabled(&desc->irq_data))
  201. chip->irq_disable(&desc->irq_data);
  202. }
  203. }
  204. /**
  205. * machine_crash_shutdown - shutdown non-crashing cpus and save registers
  206. */
  207. void machine_crash_shutdown(struct pt_regs *regs)
  208. {
  209. local_irq_disable();
  210. /* shutdown non-crashing cpus */
  211. crash_smp_send_stop();
  212. /* for crashing cpu */
  213. crash_save_cpu(regs, smp_processor_id());
  214. machine_kexec_mask_interrupts();
  215. pr_info("Starting crashdump kernel...\n");
  216. }
  217. #if defined(CONFIG_CRASH_DUMP) && defined(CONFIG_HIBERNATION)
  218. /*
  219. * To preserve the crash dump kernel image, the relevant memory segments
  220. * should be mapped again around the hibernation.
  221. */
  222. void crash_prepare_suspend(void)
  223. {
  224. if (kexec_crash_image)
  225. arch_kexec_unprotect_crashkres();
  226. }
  227. void crash_post_resume(void)
  228. {
  229. if (kexec_crash_image)
  230. arch_kexec_protect_crashkres();
  231. }
  232. /*
  233. * crash_is_nosave
  234. *
  235. * Return true only if a page is part of reserved memory for crash dump kernel,
  236. * but does not hold any data of loaded kernel image.
  237. *
  238. * Note that all the pages in crash dump kernel memory have been initially
  239. * marked as Reserved as memory was allocated via memblock_reserve().
  240. *
  241. * In hibernation, the pages which are Reserved and yet "nosave" are excluded
  242. * from the hibernation iamge. crash_is_nosave() does thich check for crash
  243. * dump kernel and will reduce the total size of hibernation image.
  244. */
  245. bool crash_is_nosave(unsigned long pfn)
  246. {
  247. int i;
  248. phys_addr_t addr;
  249. if (!crashk_res.end)
  250. return false;
  251. /* in reserved memory? */
  252. addr = __pfn_to_phys(pfn);
  253. if ((addr < crashk_res.start) || (crashk_res.end < addr)) {
  254. if (!crashk_low_res.end)
  255. return false;
  256. if ((addr < crashk_low_res.start) || (crashk_low_res.end < addr))
  257. return false;
  258. }
  259. if (!kexec_crash_image)
  260. return true;
  261. /* not part of loaded kernel image? */
  262. for (i = 0; i < kexec_crash_image->nr_segments; i++)
  263. if (addr >= kexec_crash_image->segment[i].mem &&
  264. addr < (kexec_crash_image->segment[i].mem +
  265. kexec_crash_image->segment[i].memsz))
  266. return false;
  267. return true;
  268. }
  269. void crash_free_reserved_phys_range(unsigned long begin, unsigned long end)
  270. {
  271. unsigned long addr;
  272. struct page *page;
  273. for (addr = begin; addr < end; addr += PAGE_SIZE) {
  274. page = phys_to_page(addr);
  275. free_reserved_page(page);
  276. }
  277. }
  278. #endif /* CONFIG_HIBERNATION */