machine_kexec.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * machine_kexec.c for kexec
  4. * Created by <nschichan@corp.free.fr> on Thu Oct 12 15:15:06 2006
  5. */
  6. #include <linux/compiler.h>
  7. #include <linux/kexec.h>
  8. #include <linux/mm.h>
  9. #include <linux/delay.h>
  10. #include <linux/libfdt.h>
  11. #include <linux/reboot.h>
  12. #include <asm/cacheflush.h>
  13. #include <asm/page.h>
  14. extern const unsigned char relocate_new_kernel[];
  15. extern const size_t relocate_new_kernel_size;
  16. extern unsigned long kexec_start_address;
  17. extern unsigned long kexec_indirection_page;
  18. static unsigned long reboot_code_buffer;
  19. #ifdef CONFIG_SMP
  20. static void (*relocated_kexec_smp_wait)(void *);
  21. atomic_t kexec_ready_to_reboot = ATOMIC_INIT(0);
  22. void (*_crash_smp_send_stop)(void) = NULL;
  23. #endif
  24. void (*_machine_kexec_shutdown)(void) = NULL;
  25. void (*_machine_crash_shutdown)(struct pt_regs *regs) = NULL;
  26. static void kexec_image_info(const struct kimage *kimage)
  27. {
  28. unsigned long i;
  29. pr_debug("kexec kimage info:\n");
  30. pr_debug(" type: %d\n", kimage->type);
  31. pr_debug(" start: %lx\n", kimage->start);
  32. pr_debug(" head: %lx\n", kimage->head);
  33. pr_debug(" nr_segments: %lu\n", kimage->nr_segments);
  34. for (i = 0; i < kimage->nr_segments; i++) {
  35. pr_debug(" segment[%lu]: %016lx - %016lx, 0x%lx bytes, %lu pages\n",
  36. i,
  37. kimage->segment[i].mem,
  38. kimage->segment[i].mem + kimage->segment[i].memsz,
  39. (unsigned long)kimage->segment[i].memsz,
  40. (unsigned long)kimage->segment[i].memsz / PAGE_SIZE);
  41. }
  42. }
  43. #ifdef CONFIG_UHI_BOOT
  44. static int uhi_machine_kexec_prepare(struct kimage *kimage)
  45. {
  46. int i;
  47. /*
  48. * In case DTB file is not passed to the new kernel, a flat device
  49. * tree will be created by kexec tool. It holds modified command
  50. * line for the new kernel.
  51. */
  52. for (i = 0; i < kimage->nr_segments; i++) {
  53. struct fdt_header fdt;
  54. if (kimage->segment[i].memsz <= sizeof(fdt))
  55. continue;
  56. if (copy_from_user(&fdt, kimage->segment[i].buf, sizeof(fdt)))
  57. continue;
  58. if (fdt_check_header(&fdt))
  59. continue;
  60. kexec_args[0] = -2;
  61. kexec_args[1] = (unsigned long)
  62. phys_to_virt((unsigned long)kimage->segment[i].mem);
  63. break;
  64. }
  65. return 0;
  66. }
  67. int (*_machine_kexec_prepare)(struct kimage *) = uhi_machine_kexec_prepare;
  68. #else
  69. int (*_machine_kexec_prepare)(struct kimage *) = NULL;
  70. #endif /* CONFIG_UHI_BOOT */
  71. int
  72. machine_kexec_prepare(struct kimage *kimage)
  73. {
  74. #ifdef CONFIG_SMP
  75. if (!kexec_nonboot_cpu_func())
  76. return -EINVAL;
  77. #endif
  78. kexec_image_info(kimage);
  79. if (_machine_kexec_prepare)
  80. return _machine_kexec_prepare(kimage);
  81. return 0;
  82. }
  83. void
  84. machine_kexec_cleanup(struct kimage *kimage)
  85. {
  86. }
  87. #ifdef CONFIG_SMP
  88. static void kexec_shutdown_secondary(void *param)
  89. {
  90. int cpu = smp_processor_id();
  91. if (!cpu_online(cpu))
  92. return;
  93. /* We won't be sent IPIs any more. */
  94. set_cpu_online(cpu, false);
  95. local_irq_disable();
  96. while (!atomic_read(&kexec_ready_to_reboot))
  97. cpu_relax();
  98. kexec_reboot();
  99. /* NOTREACHED */
  100. }
  101. #endif
  102. void
  103. machine_shutdown(void)
  104. {
  105. if (_machine_kexec_shutdown)
  106. _machine_kexec_shutdown();
  107. #ifdef CONFIG_SMP
  108. smp_call_function(kexec_shutdown_secondary, NULL, 0);
  109. while (num_online_cpus() > 1) {
  110. cpu_relax();
  111. mdelay(1);
  112. }
  113. #endif
  114. }
  115. void
  116. machine_crash_shutdown(struct pt_regs *regs)
  117. {
  118. if (_machine_crash_shutdown)
  119. _machine_crash_shutdown(regs);
  120. else
  121. default_machine_crash_shutdown(regs);
  122. }
  123. #ifdef CONFIG_SMP
  124. void kexec_nonboot_cpu_jump(void)
  125. {
  126. local_flush_icache_range((unsigned long)relocated_kexec_smp_wait,
  127. reboot_code_buffer + relocate_new_kernel_size);
  128. relocated_kexec_smp_wait(NULL);
  129. }
  130. #endif
  131. void kexec_reboot(void)
  132. {
  133. void (*do_kexec)(void) __noreturn;
  134. /*
  135. * We know we were online, and there will be no incoming IPIs at
  136. * this point. Mark online again before rebooting so that the crash
  137. * analysis tool will see us correctly.
  138. */
  139. set_cpu_online(smp_processor_id(), true);
  140. /* Ensure remote CPUs observe that we're online before rebooting. */
  141. smp_mb__after_atomic();
  142. #ifdef CONFIG_SMP
  143. if (smp_processor_id() > 0) {
  144. /*
  145. * Instead of cpu_relax() or wait, this is needed for kexec
  146. * smp reboot. Kdump usually doesn't require an smp new
  147. * kernel, but kexec may do.
  148. */
  149. kexec_nonboot_cpu();
  150. /* NOTREACHED */
  151. }
  152. #endif
  153. /*
  154. * Make sure we get correct instructions written by the
  155. * machine_kexec() CPU.
  156. */
  157. local_flush_icache_range(reboot_code_buffer,
  158. reboot_code_buffer + relocate_new_kernel_size);
  159. do_kexec = (void *)reboot_code_buffer;
  160. do_kexec();
  161. }
  162. void
  163. machine_kexec(struct kimage *image)
  164. {
  165. unsigned long entry;
  166. unsigned long *ptr;
  167. reboot_code_buffer =
  168. (unsigned long)page_address(image->control_code_page);
  169. kexec_start_address =
  170. (unsigned long) phys_to_virt(image->start);
  171. if (image->type == KEXEC_TYPE_DEFAULT) {
  172. kexec_indirection_page =
  173. (unsigned long) phys_to_virt(image->head & PAGE_MASK);
  174. } else {
  175. kexec_indirection_page = (unsigned long)&image->head;
  176. }
  177. memcpy((void*)reboot_code_buffer, relocate_new_kernel,
  178. relocate_new_kernel_size);
  179. /*
  180. * The generic kexec code builds a page list with physical
  181. * addresses. they are directly accessible through KSEG0 (or
  182. * CKSEG0 or XPHYS if on 64bit system), hence the
  183. * phys_to_virt() call.
  184. */
  185. for (ptr = &image->head; (entry = *ptr) && !(entry &IND_DONE);
  186. ptr = (entry & IND_INDIRECTION) ?
  187. phys_to_virt(entry & PAGE_MASK) : ptr + 1) {
  188. if (*ptr & IND_SOURCE || *ptr & IND_INDIRECTION ||
  189. *ptr & IND_DESTINATION)
  190. *ptr = (unsigned long) phys_to_virt(*ptr);
  191. }
  192. /* Mark offline BEFORE disabling local irq. */
  193. set_cpu_online(smp_processor_id(), false);
  194. /*
  195. * we do not want to be bothered.
  196. */
  197. local_irq_disable();
  198. printk("Will call new kernel at %08lx\n", image->start);
  199. printk("Bye ...\n");
  200. /* Make reboot code buffer available to the boot CPU. */
  201. __flush_cache_all();
  202. #ifdef CONFIG_SMP
  203. /* All secondary cpus now may jump to kexec_wait cycle */
  204. relocated_kexec_smp_wait = reboot_code_buffer +
  205. (void *)(kexec_smp_wait - relocate_new_kernel);
  206. smp_wmb();
  207. atomic_set(&kexec_ready_to_reboot, 1);
  208. #endif
  209. kexec_reboot();
  210. }