machine_kexec.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * machine_kexec.c for kexec
  4. *
  5. * Copyright (C) 2022 Loongson Technology Corporation Limited
  6. */
  7. #include <linux/compiler.h>
  8. #include <linux/cpu.h>
  9. #include <linux/kexec.h>
  10. #include <linux/crash_dump.h>
  11. #include <linux/delay.h>
  12. #include <linux/irq.h>
  13. #include <linux/libfdt.h>
  14. #include <linux/mm.h>
  15. #include <linux/of_fdt.h>
  16. #include <linux/reboot.h>
  17. #include <linux/sched.h>
  18. #include <linux/sched/task_stack.h>
  19. #include <asm/bootinfo.h>
  20. #include <asm/cacheflush.h>
  21. #include <asm/page.h>
  22. /* 0x100000 ~ 0x200000 is safe */
  23. #define KEXEC_CONTROL_CODE TO_CACHE(0x100000UL)
  24. #define KEXEC_CMDLINE_ADDR TO_CACHE(0x108000UL)
  25. static unsigned long reboot_code_buffer;
  26. static cpumask_t cpus_in_crash = CPU_MASK_NONE;
  27. #ifdef CONFIG_SMP
  28. static void (*relocated_kexec_smp_wait)(void *);
  29. atomic_t kexec_ready_to_reboot = ATOMIC_INIT(0);
  30. #endif
  31. static unsigned long efi_boot;
  32. static unsigned long cmdline_ptr;
  33. static unsigned long systable_ptr;
  34. static unsigned long start_addr;
  35. static unsigned long first_ind_entry;
  36. static void kexec_image_info(const struct kimage *kimage)
  37. {
  38. unsigned long i;
  39. pr_debug("kexec kimage info:\n");
  40. pr_debug("\ttype: %d\n", kimage->type);
  41. pr_debug("\tstart: %lx\n", kimage->start);
  42. pr_debug("\thead: %lx\n", kimage->head);
  43. pr_debug("\tnr_segments: %lu\n", kimage->nr_segments);
  44. for (i = 0; i < kimage->nr_segments; i++) {
  45. pr_debug("\t segment[%lu]: %016lx - %016lx", i,
  46. kimage->segment[i].mem,
  47. kimage->segment[i].mem + kimage->segment[i].memsz);
  48. pr_debug("\t\t0x%lx bytes, %lu pages\n",
  49. (unsigned long)kimage->segment[i].memsz,
  50. (unsigned long)kimage->segment[i].memsz / PAGE_SIZE);
  51. }
  52. }
  53. int machine_kexec_prepare(struct kimage *kimage)
  54. {
  55. int i;
  56. char *bootloader = "kexec";
  57. void *cmdline_ptr = (void *)KEXEC_CMDLINE_ADDR;
  58. kexec_image_info(kimage);
  59. kimage->arch.efi_boot = fw_arg0;
  60. kimage->arch.systable_ptr = fw_arg2;
  61. /* Find the command line */
  62. for (i = 0; i < kimage->nr_segments; i++) {
  63. if (!strncmp(bootloader, (char __user *)kimage->segment[i].buf, strlen(bootloader))) {
  64. if (!copy_from_user(cmdline_ptr, kimage->segment[i].buf, COMMAND_LINE_SIZE))
  65. kimage->arch.cmdline_ptr = (unsigned long)cmdline_ptr;
  66. break;
  67. }
  68. }
  69. if (!kimage->arch.cmdline_ptr) {
  70. pr_err("Command line not included in the provided image\n");
  71. return -EINVAL;
  72. }
  73. /* kexec/kdump need a safe page to save reboot_code_buffer */
  74. kimage->control_code_page = virt_to_page((void *)KEXEC_CONTROL_CODE);
  75. reboot_code_buffer = (unsigned long)page_address(kimage->control_code_page);
  76. memcpy((void *)reboot_code_buffer, relocate_new_kernel, relocate_new_kernel_size);
  77. #ifdef CONFIG_SMP
  78. /* All secondary cpus now may jump to kexec_smp_wait cycle */
  79. relocated_kexec_smp_wait = reboot_code_buffer + (void *)(kexec_smp_wait - relocate_new_kernel);
  80. #endif
  81. return 0;
  82. }
  83. void machine_kexec_cleanup(struct kimage *kimage)
  84. {
  85. }
  86. void kexec_reboot(void)
  87. {
  88. do_kexec_t do_kexec = NULL;
  89. /*
  90. * We know we were online, and there will be no incoming IPIs at
  91. * this point. Mark online again before rebooting so that the crash
  92. * analysis tool will see us correctly.
  93. */
  94. set_cpu_online(smp_processor_id(), true);
  95. /* Ensure remote CPUs observe that we're online before rebooting. */
  96. smp_mb__after_atomic();
  97. /*
  98. * Make sure we get correct instructions written by the
  99. * machine_kexec_prepare() CPU.
  100. */
  101. __asm__ __volatile__ ("\tibar 0\n"::);
  102. #ifdef CONFIG_SMP
  103. /* All secondary cpus go to kexec_smp_wait */
  104. if (smp_processor_id() > 0) {
  105. relocated_kexec_smp_wait(NULL);
  106. unreachable();
  107. }
  108. #endif
  109. do_kexec = (void *)reboot_code_buffer;
  110. do_kexec(efi_boot, cmdline_ptr, systable_ptr, start_addr, first_ind_entry);
  111. unreachable();
  112. }
  113. #ifdef CONFIG_SMP
  114. static void kexec_shutdown_secondary(void *regs)
  115. {
  116. int cpu = smp_processor_id();
  117. if (!cpu_online(cpu))
  118. return;
  119. /* We won't be sent IPIs any more. */
  120. set_cpu_online(cpu, false);
  121. local_irq_disable();
  122. while (!atomic_read(&kexec_ready_to_reboot))
  123. cpu_relax();
  124. kexec_reboot();
  125. }
  126. static void crash_shutdown_secondary(void *passed_regs)
  127. {
  128. int cpu = smp_processor_id();
  129. struct pt_regs *regs = passed_regs;
  130. /*
  131. * If we are passed registers, use those. Otherwise get the
  132. * regs from the last interrupt, which should be correct, as
  133. * we are in an interrupt. But if the regs are not there,
  134. * pull them from the top of the stack. They are probably
  135. * wrong, but we need something to keep from crashing again.
  136. */
  137. if (!regs)
  138. regs = get_irq_regs();
  139. if (!regs)
  140. regs = task_pt_regs(current);
  141. if (!cpu_online(cpu))
  142. return;
  143. /* We won't be sent IPIs any more. */
  144. set_cpu_online(cpu, false);
  145. local_irq_disable();
  146. if (!cpumask_test_cpu(cpu, &cpus_in_crash))
  147. crash_save_cpu(regs, cpu);
  148. cpumask_set_cpu(cpu, &cpus_in_crash);
  149. while (!atomic_read(&kexec_ready_to_reboot))
  150. cpu_relax();
  151. kexec_reboot();
  152. }
  153. void crash_smp_send_stop(void)
  154. {
  155. unsigned int ncpus;
  156. unsigned long timeout;
  157. static int cpus_stopped;
  158. /*
  159. * This function can be called twice in panic path, but obviously
  160. * we should execute this only once.
  161. */
  162. if (cpus_stopped)
  163. return;
  164. cpus_stopped = 1;
  165. /* Excluding the panic cpu */
  166. ncpus = num_online_cpus() - 1;
  167. smp_call_function(crash_shutdown_secondary, NULL, 0);
  168. smp_wmb();
  169. /*
  170. * The crash CPU sends an IPI and wait for other CPUs to
  171. * respond. Delay of at least 10 seconds.
  172. */
  173. timeout = MSEC_PER_SEC * 10;
  174. pr_emerg("Sending IPI to other cpus...\n");
  175. while ((cpumask_weight(&cpus_in_crash) < ncpus) && timeout--) {
  176. mdelay(1);
  177. cpu_relax();
  178. }
  179. }
  180. #endif /* defined(CONFIG_SMP) */
  181. void machine_shutdown(void)
  182. {
  183. int cpu;
  184. /* All CPUs go to reboot_code_buffer */
  185. for_each_possible_cpu(cpu)
  186. if (!cpu_online(cpu))
  187. cpu_device_up(get_cpu_device(cpu));
  188. #ifdef CONFIG_SMP
  189. smp_call_function(kexec_shutdown_secondary, NULL, 0);
  190. #endif
  191. }
  192. void machine_crash_shutdown(struct pt_regs *regs)
  193. {
  194. int crashing_cpu;
  195. local_irq_disable();
  196. crashing_cpu = smp_processor_id();
  197. crash_save_cpu(regs, crashing_cpu);
  198. #ifdef CONFIG_SMP
  199. crash_smp_send_stop();
  200. #endif
  201. cpumask_set_cpu(crashing_cpu, &cpus_in_crash);
  202. pr_info("Starting crashdump kernel...\n");
  203. }
  204. void machine_kexec(struct kimage *image)
  205. {
  206. unsigned long entry, *ptr;
  207. struct kimage_arch *internal = &image->arch;
  208. efi_boot = internal->efi_boot;
  209. cmdline_ptr = internal->cmdline_ptr;
  210. systable_ptr = internal->systable_ptr;
  211. start_addr = (unsigned long)phys_to_virt(image->start);
  212. first_ind_entry = (image->type == KEXEC_TYPE_DEFAULT) ?
  213. (unsigned long)phys_to_virt(image->head & PAGE_MASK) : 0;
  214. /*
  215. * The generic kexec code builds a page list with physical
  216. * addresses. they are directly accessible through XKPRANGE
  217. * hence the phys_to_virt() call.
  218. */
  219. for (ptr = &image->head; (entry = *ptr) && !(entry & IND_DONE);
  220. ptr = (entry & IND_INDIRECTION) ?
  221. phys_to_virt(entry & PAGE_MASK) : ptr + 1) {
  222. if (*ptr & IND_SOURCE || *ptr & IND_INDIRECTION ||
  223. *ptr & IND_DESTINATION)
  224. *ptr = (unsigned long) phys_to_virt(*ptr);
  225. }
  226. /* Mark offline before disabling local irq. */
  227. set_cpu_online(smp_processor_id(), false);
  228. /* We do not want to be bothered. */
  229. local_irq_disable();
  230. pr_notice("EFI boot flag 0x%lx\n", efi_boot);
  231. pr_notice("Command line at 0x%lx\n", cmdline_ptr);
  232. pr_notice("System table at 0x%lx\n", systable_ptr);
  233. pr_notice("We will call new kernel at 0x%lx\n", start_addr);
  234. pr_notice("Bye ...\n");
  235. /* Make reboot code buffer available to the boot CPU. */
  236. flush_cache_all();
  237. #ifdef CONFIG_SMP
  238. atomic_set(&kexec_ready_to_reboot, 1);
  239. #endif
  240. kexec_reboot();
  241. }