machine_kexec.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. /*
  2. * kexec for arm64
  3. *
  4. * Copyright (C) Linaro.
  5. * Copyright (C) Huawei Futurewei Technologies.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/interrupt.h>
  12. #include <linux/irq.h>
  13. #include <linux/kernel.h>
  14. #include <linux/kexec.h>
  15. #include <linux/page-flags.h>
  16. #include <linux/smp.h>
  17. #include <asm/cacheflush.h>
  18. #include <asm/cpu_ops.h>
  19. #include <asm/memory.h>
  20. #include <asm/mmu.h>
  21. #include <asm/mmu_context.h>
  22. #include <asm/page.h>
  23. #include "cpu-reset.h"
  24. /* Global variables for the arm64_relocate_new_kernel routine. */
  25. extern const unsigned char arm64_relocate_new_kernel[];
  26. extern const unsigned long arm64_relocate_new_kernel_size;
  27. /**
  28. * kexec_image_info - For debugging output.
  29. */
  30. #define kexec_image_info(_i) _kexec_image_info(__func__, __LINE__, _i)
  31. static void _kexec_image_info(const char *func, int line,
  32. const struct kimage *kimage)
  33. {
  34. unsigned long i;
  35. pr_debug("%s:%d:\n", func, line);
  36. pr_debug(" kexec kimage info:\n");
  37. pr_debug(" type: %d\n", kimage->type);
  38. pr_debug(" start: %lx\n", kimage->start);
  39. pr_debug(" head: %lx\n", kimage->head);
  40. pr_debug(" nr_segments: %lu\n", kimage->nr_segments);
  41. for (i = 0; i < kimage->nr_segments; i++) {
  42. pr_debug(" segment[%lu]: %016lx - %016lx, 0x%lx bytes, %lu pages\n",
  43. i,
  44. kimage->segment[i].mem,
  45. kimage->segment[i].mem + kimage->segment[i].memsz,
  46. kimage->segment[i].memsz,
  47. kimage->segment[i].memsz / PAGE_SIZE);
  48. }
  49. }
  50. void machine_kexec_cleanup(struct kimage *kimage)
  51. {
  52. /* Empty routine needed to avoid build errors. */
  53. }
  54. /**
  55. * machine_kexec_prepare - Prepare for a kexec reboot.
  56. *
  57. * Called from the core kexec code when a kernel image is loaded.
  58. * Forbid loading a kexec kernel if we have no way of hotplugging cpus or cpus
  59. * are stuck in the kernel. This avoids a panic once we hit machine_kexec().
  60. */
  61. int machine_kexec_prepare(struct kimage *kimage)
  62. {
  63. kexec_image_info(kimage);
  64. if (kimage->type != KEXEC_TYPE_CRASH && cpus_are_stuck_in_kernel()) {
  65. pr_err("Can't kexec: CPUs are stuck in the kernel.\n");
  66. return -EBUSY;
  67. }
  68. return 0;
  69. }
  70. /**
  71. * kexec_list_flush - Helper to flush the kimage list and source pages to PoC.
  72. */
  73. static void kexec_list_flush(struct kimage *kimage)
  74. {
  75. kimage_entry_t *entry;
  76. for (entry = &kimage->head; ; entry++) {
  77. unsigned int flag;
  78. void *addr;
  79. /* flush the list entries. */
  80. __flush_dcache_area(entry, sizeof(kimage_entry_t));
  81. flag = *entry & IND_FLAGS;
  82. if (flag == IND_DONE)
  83. break;
  84. addr = phys_to_virt(*entry & PAGE_MASK);
  85. switch (flag) {
  86. case IND_INDIRECTION:
  87. /* Set entry point just before the new list page. */
  88. entry = (kimage_entry_t *)addr - 1;
  89. break;
  90. case IND_SOURCE:
  91. /* flush the source pages. */
  92. __flush_dcache_area(addr, PAGE_SIZE);
  93. break;
  94. case IND_DESTINATION:
  95. break;
  96. default:
  97. BUG();
  98. }
  99. }
  100. }
  101. /**
  102. * kexec_segment_flush - Helper to flush the kimage segments to PoC.
  103. */
  104. static void kexec_segment_flush(const struct kimage *kimage)
  105. {
  106. unsigned long i;
  107. pr_debug("%s:\n", __func__);
  108. for (i = 0; i < kimage->nr_segments; i++) {
  109. pr_debug(" segment[%lu]: %016lx - %016lx, 0x%lx bytes, %lu pages\n",
  110. i,
  111. kimage->segment[i].mem,
  112. kimage->segment[i].mem + kimage->segment[i].memsz,
  113. kimage->segment[i].memsz,
  114. kimage->segment[i].memsz / PAGE_SIZE);
  115. __flush_dcache_area(phys_to_virt(kimage->segment[i].mem),
  116. kimage->segment[i].memsz);
  117. }
  118. }
  119. /**
  120. * machine_kexec - Do the kexec reboot.
  121. *
  122. * Called from the core kexec code for a sys_reboot with LINUX_REBOOT_CMD_KEXEC.
  123. */
  124. void machine_kexec(struct kimage *kimage)
  125. {
  126. phys_addr_t reboot_code_buffer_phys;
  127. void *reboot_code_buffer;
  128. bool in_kexec_crash = (kimage == kexec_crash_image);
  129. bool stuck_cpus = cpus_are_stuck_in_kernel();
  130. /*
  131. * New cpus may have become stuck_in_kernel after we loaded the image.
  132. */
  133. BUG_ON(!in_kexec_crash && (stuck_cpus || (num_online_cpus() > 1)));
  134. WARN(in_kexec_crash && (stuck_cpus || smp_crash_stop_failed()),
  135. "Some CPUs may be stale, kdump will be unreliable.\n");
  136. reboot_code_buffer_phys = page_to_phys(kimage->control_code_page);
  137. reboot_code_buffer = phys_to_virt(reboot_code_buffer_phys);
  138. kexec_image_info(kimage);
  139. pr_debug("%s:%d: control_code_page: %p\n", __func__, __LINE__,
  140. kimage->control_code_page);
  141. pr_debug("%s:%d: reboot_code_buffer_phys: %pa\n", __func__, __LINE__,
  142. &reboot_code_buffer_phys);
  143. pr_debug("%s:%d: reboot_code_buffer: %p\n", __func__, __LINE__,
  144. reboot_code_buffer);
  145. pr_debug("%s:%d: relocate_new_kernel: %p\n", __func__, __LINE__,
  146. arm64_relocate_new_kernel);
  147. pr_debug("%s:%d: relocate_new_kernel_size: 0x%lx(%lu) bytes\n",
  148. __func__, __LINE__, arm64_relocate_new_kernel_size,
  149. arm64_relocate_new_kernel_size);
  150. /*
  151. * Copy arm64_relocate_new_kernel to the reboot_code_buffer for use
  152. * after the kernel is shut down.
  153. */
  154. memcpy(reboot_code_buffer, arm64_relocate_new_kernel,
  155. arm64_relocate_new_kernel_size);
  156. /* Flush the reboot_code_buffer in preparation for its execution. */
  157. __flush_dcache_area(reboot_code_buffer, arm64_relocate_new_kernel_size);
  158. flush_icache_range((uintptr_t)reboot_code_buffer,
  159. arm64_relocate_new_kernel_size);
  160. /* Flush the kimage list and its buffers. */
  161. kexec_list_flush(kimage);
  162. /* Flush the new image if already in place. */
  163. if ((kimage != kexec_crash_image) && (kimage->head & IND_DONE))
  164. kexec_segment_flush(kimage);
  165. pr_info("Bye!\n");
  166. /* Disable all DAIF exceptions. */
  167. asm volatile ("msr daifset, #0xf" : : : "memory");
  168. /*
  169. * cpu_soft_restart will shutdown the MMU, disable data caches, then
  170. * transfer control to the reboot_code_buffer which contains a copy of
  171. * the arm64_relocate_new_kernel routine. arm64_relocate_new_kernel
  172. * uses physical addressing to relocate the new image to its final
  173. * position and transfers control to the image entry point when the
  174. * relocation is complete.
  175. */
  176. cpu_soft_restart(kimage != kexec_crash_image,
  177. reboot_code_buffer_phys, kimage->head, kimage->start, 0);
  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. smp_send_crash_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. void arch_kexec_protect_crashkres(void)
  218. {
  219. int i;
  220. kexec_segment_flush(kexec_crash_image);
  221. for (i = 0; i < kexec_crash_image->nr_segments; i++)
  222. set_memory_valid(
  223. __phys_to_virt(kexec_crash_image->segment[i].mem),
  224. kexec_crash_image->segment[i].memsz >> PAGE_SHIFT, 0);
  225. }
  226. void arch_kexec_unprotect_crashkres(void)
  227. {
  228. int i;
  229. for (i = 0; i < kexec_crash_image->nr_segments; i++)
  230. set_memory_valid(
  231. __phys_to_virt(kexec_crash_image->segment[i].mem),
  232. kexec_crash_image->segment[i].memsz >> PAGE_SHIFT, 1);
  233. }
  234. #ifdef CONFIG_HIBERNATION
  235. /*
  236. * To preserve the crash dump kernel image, the relevant memory segments
  237. * should be mapped again around the hibernation.
  238. */
  239. void crash_prepare_suspend(void)
  240. {
  241. if (kexec_crash_image)
  242. arch_kexec_unprotect_crashkres();
  243. }
  244. void crash_post_resume(void)
  245. {
  246. if (kexec_crash_image)
  247. arch_kexec_protect_crashkres();
  248. }
  249. /*
  250. * crash_is_nosave
  251. *
  252. * Return true only if a page is part of reserved memory for crash dump kernel,
  253. * but does not hold any data of loaded kernel image.
  254. *
  255. * Note that all the pages in crash dump kernel memory have been initially
  256. * marked as Reserved in kexec_reserve_crashkres_pages().
  257. *
  258. * In hibernation, the pages which are Reserved and yet "nosave" are excluded
  259. * from the hibernation iamge. crash_is_nosave() does thich check for crash
  260. * dump kernel and will reduce the total size of hibernation image.
  261. */
  262. bool crash_is_nosave(unsigned long pfn)
  263. {
  264. int i;
  265. phys_addr_t addr;
  266. if (!crashk_res.end)
  267. return false;
  268. /* in reserved memory? */
  269. addr = __pfn_to_phys(pfn);
  270. if ((addr < crashk_res.start) || (crashk_res.end < addr))
  271. return false;
  272. if (!kexec_crash_image)
  273. return true;
  274. /* not part of loaded kernel image? */
  275. for (i = 0; i < kexec_crash_image->nr_segments; i++)
  276. if (addr >= kexec_crash_image->segment[i].mem &&
  277. addr < (kexec_crash_image->segment[i].mem +
  278. kexec_crash_image->segment[i].memsz))
  279. return false;
  280. return true;
  281. }
  282. void crash_free_reserved_phys_range(unsigned long begin, unsigned long end)
  283. {
  284. unsigned long addr;
  285. struct page *page;
  286. for (addr = begin; addr < end; addr += PAGE_SIZE) {
  287. page = phys_to_page(addr);
  288. ClearPageReserved(page);
  289. free_reserved_page(page);
  290. }
  291. }
  292. #endif /* CONFIG_HIBERNATION */
  293. void arch_crash_save_vmcoreinfo(void)
  294. {
  295. VMCOREINFO_NUMBER(VA_BITS);
  296. /* Please note VMCOREINFO_NUMBER() uses "%d", not "%x" */
  297. vmcoreinfo_append_str("NUMBER(kimage_voffset)=0x%llx\n",
  298. kimage_voffset);
  299. vmcoreinfo_append_str("NUMBER(PHYS_OFFSET)=0x%llx\n",
  300. PHYS_OFFSET);
  301. }