machine_kexec.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright IBM Corp. 2005, 2011
  4. *
  5. * Author(s): Rolf Adelsberger,
  6. * Heiko Carstens <heiko.carstens@de.ibm.com>
  7. * Michael Holzheu <holzheu@linux.vnet.ibm.com>
  8. */
  9. #include <linux/device.h>
  10. #include <linux/mm.h>
  11. #include <linux/kexec.h>
  12. #include <linux/delay.h>
  13. #include <linux/reboot.h>
  14. #include <linux/ftrace.h>
  15. #include <linux/debug_locks.h>
  16. #include <linux/suspend.h>
  17. #include <asm/cio.h>
  18. #include <asm/setup.h>
  19. #include <asm/pgtable.h>
  20. #include <asm/pgalloc.h>
  21. #include <asm/smp.h>
  22. #include <asm/reset.h>
  23. #include <asm/ipl.h>
  24. #include <asm/diag.h>
  25. #include <asm/elf.h>
  26. #include <asm/asm-offsets.h>
  27. #include <asm/cacheflush.h>
  28. #include <asm/os_info.h>
  29. #include <asm/set_memory.h>
  30. #include <asm/switch_to.h>
  31. #include <asm/nmi.h>
  32. typedef void (*relocate_kernel_t)(kimage_entry_t *, unsigned long);
  33. extern const unsigned char relocate_kernel[];
  34. extern const unsigned long long relocate_kernel_len;
  35. #ifdef CONFIG_CRASH_DUMP
  36. /*
  37. * PM notifier callback for kdump
  38. */
  39. static int machine_kdump_pm_cb(struct notifier_block *nb, unsigned long action,
  40. void *ptr)
  41. {
  42. switch (action) {
  43. case PM_SUSPEND_PREPARE:
  44. case PM_HIBERNATION_PREPARE:
  45. if (kexec_crash_image)
  46. arch_kexec_unprotect_crashkres();
  47. break;
  48. case PM_POST_SUSPEND:
  49. case PM_POST_HIBERNATION:
  50. if (kexec_crash_image)
  51. arch_kexec_protect_crashkres();
  52. break;
  53. default:
  54. return NOTIFY_DONE;
  55. }
  56. return NOTIFY_OK;
  57. }
  58. static int __init machine_kdump_pm_init(void)
  59. {
  60. pm_notifier(machine_kdump_pm_cb, 0);
  61. return 0;
  62. }
  63. arch_initcall(machine_kdump_pm_init);
  64. /*
  65. * Reset the system, copy boot CPU registers to absolute zero,
  66. * and jump to the kdump image
  67. */
  68. static void __do_machine_kdump(void *image)
  69. {
  70. int (*start_kdump)(int);
  71. unsigned long prefix;
  72. /* store_status() saved the prefix register to lowcore */
  73. prefix = (unsigned long) S390_lowcore.prefixreg_save_area;
  74. /* Now do the reset */
  75. s390_reset_system();
  76. /*
  77. * Copy dump CPU store status info to absolute zero.
  78. * This need to be done *after* s390_reset_system set the
  79. * prefix register of this CPU to zero
  80. */
  81. memcpy((void *) __LC_FPREGS_SAVE_AREA,
  82. (void *)(prefix + __LC_FPREGS_SAVE_AREA), 512);
  83. __load_psw_mask(PSW_MASK_BASE | PSW_DEFAULT_KEY | PSW_MASK_EA | PSW_MASK_BA);
  84. start_kdump = (void *)((struct kimage *) image)->start;
  85. start_kdump(1);
  86. /* Die if start_kdump returns */
  87. disabled_wait((unsigned long) __builtin_return_address(0));
  88. }
  89. /*
  90. * Start kdump: create a LGR log entry, store status of all CPUs and
  91. * branch to __do_machine_kdump.
  92. */
  93. static noinline void __machine_kdump(void *image)
  94. {
  95. struct mcesa *mcesa;
  96. union ctlreg2 cr2_old, cr2_new;
  97. int this_cpu, cpu;
  98. lgr_info_log();
  99. /* Get status of the other CPUs */
  100. this_cpu = smp_find_processor_id(stap());
  101. for_each_online_cpu(cpu) {
  102. if (cpu == this_cpu)
  103. continue;
  104. if (smp_store_status(cpu))
  105. continue;
  106. }
  107. /* Store status of the boot CPU */
  108. mcesa = (struct mcesa *)(S390_lowcore.mcesad & MCESA_ORIGIN_MASK);
  109. if (MACHINE_HAS_VX)
  110. save_vx_regs((__vector128 *) mcesa->vector_save_area);
  111. if (MACHINE_HAS_GS) {
  112. __ctl_store(cr2_old.val, 2, 2);
  113. cr2_new = cr2_old;
  114. cr2_new.gse = 1;
  115. __ctl_load(cr2_new.val, 2, 2);
  116. save_gs_cb((struct gs_cb *) mcesa->guarded_storage_save_area);
  117. __ctl_load(cr2_old.val, 2, 2);
  118. }
  119. /*
  120. * To create a good backchain for this CPU in the dump store_status
  121. * is passed the address of a function. The address is saved into
  122. * the PSW save area of the boot CPU and the function is invoked as
  123. * a tail call of store_status. The backchain in the dump will look
  124. * like this:
  125. * restart_int_handler -> __machine_kexec -> __do_machine_kdump
  126. * The call to store_status() will not return.
  127. */
  128. store_status(__do_machine_kdump, image);
  129. }
  130. #endif
  131. /*
  132. * Check if kdump checksums are valid: We call purgatory with parameter "0"
  133. */
  134. static bool kdump_csum_valid(struct kimage *image)
  135. {
  136. #ifdef CONFIG_CRASH_DUMP
  137. int (*start_kdump)(int) = (void *)image->start;
  138. int rc;
  139. __arch_local_irq_stnsm(0xfb); /* disable DAT */
  140. rc = start_kdump(0);
  141. __arch_local_irq_stosm(0x04); /* enable DAT */
  142. return rc == 0;
  143. #else
  144. return false;
  145. #endif
  146. }
  147. #ifdef CONFIG_CRASH_DUMP
  148. void crash_free_reserved_phys_range(unsigned long begin, unsigned long end)
  149. {
  150. unsigned long addr, size;
  151. for (addr = begin; addr < end; addr += PAGE_SIZE)
  152. free_reserved_page(pfn_to_page(addr >> PAGE_SHIFT));
  153. size = begin - crashk_res.start;
  154. if (size)
  155. os_info_crashkernel_add(crashk_res.start, size);
  156. else
  157. os_info_crashkernel_add(0, 0);
  158. }
  159. static void crash_protect_pages(int protect)
  160. {
  161. unsigned long size;
  162. if (!crashk_res.end)
  163. return;
  164. size = resource_size(&crashk_res);
  165. if (protect)
  166. set_memory_ro(crashk_res.start, size >> PAGE_SHIFT);
  167. else
  168. set_memory_rw(crashk_res.start, size >> PAGE_SHIFT);
  169. }
  170. void arch_kexec_protect_crashkres(void)
  171. {
  172. crash_protect_pages(1);
  173. }
  174. void arch_kexec_unprotect_crashkres(void)
  175. {
  176. crash_protect_pages(0);
  177. }
  178. #endif
  179. /*
  180. * Give back memory to hypervisor before new kdump is loaded
  181. */
  182. static int machine_kexec_prepare_kdump(void)
  183. {
  184. #ifdef CONFIG_CRASH_DUMP
  185. if (MACHINE_IS_VM)
  186. diag10_range(PFN_DOWN(crashk_res.start),
  187. PFN_DOWN(crashk_res.end - crashk_res.start + 1));
  188. return 0;
  189. #else
  190. return -EINVAL;
  191. #endif
  192. }
  193. int machine_kexec_prepare(struct kimage *image)
  194. {
  195. void *reboot_code_buffer;
  196. if (image->type == KEXEC_TYPE_CRASH)
  197. return machine_kexec_prepare_kdump();
  198. /* We don't support anything but the default image type for now. */
  199. if (image->type != KEXEC_TYPE_DEFAULT)
  200. return -EINVAL;
  201. /* Get the destination where the assembler code should be copied to.*/
  202. reboot_code_buffer = (void *) page_to_phys(image->control_code_page);
  203. /* Then copy it */
  204. memcpy(reboot_code_buffer, relocate_kernel, relocate_kernel_len);
  205. return 0;
  206. }
  207. void machine_kexec_cleanup(struct kimage *image)
  208. {
  209. }
  210. void arch_crash_save_vmcoreinfo(void)
  211. {
  212. VMCOREINFO_SYMBOL(lowcore_ptr);
  213. VMCOREINFO_SYMBOL(high_memory);
  214. VMCOREINFO_LENGTH(lowcore_ptr, NR_CPUS);
  215. mem_assign_absolute(S390_lowcore.vmcore_info, paddr_vmcoreinfo_note());
  216. }
  217. void machine_shutdown(void)
  218. {
  219. }
  220. void machine_crash_shutdown(struct pt_regs *regs)
  221. {
  222. }
  223. /*
  224. * Do normal kexec
  225. */
  226. static void __do_machine_kexec(void *data)
  227. {
  228. relocate_kernel_t data_mover;
  229. struct kimage *image = data;
  230. s390_reset_system();
  231. data_mover = (relocate_kernel_t) page_to_phys(image->control_code_page);
  232. __arch_local_irq_stnsm(0xfb); /* disable DAT - avoid no-execute */
  233. /* Call the moving routine */
  234. (*data_mover)(&image->head, image->start);
  235. /* Die if kexec returns */
  236. disabled_wait((unsigned long) __builtin_return_address(0));
  237. }
  238. /*
  239. * Reset system and call either kdump or normal kexec
  240. */
  241. static void __machine_kexec(void *data)
  242. {
  243. __arch_local_irq_stosm(0x04); /* enable DAT */
  244. pfault_fini();
  245. tracing_off();
  246. debug_locks_off();
  247. #ifdef CONFIG_CRASH_DUMP
  248. if (((struct kimage *) data)->type == KEXEC_TYPE_CRASH)
  249. __machine_kdump(data);
  250. #endif
  251. __do_machine_kexec(data);
  252. }
  253. /*
  254. * Do either kdump or normal kexec. In case of kdump we first ask
  255. * purgatory, if kdump checksums are valid.
  256. */
  257. void machine_kexec(struct kimage *image)
  258. {
  259. if (image->type == KEXEC_TYPE_CRASH && !kdump_csum_valid(image))
  260. return;
  261. tracer_disable();
  262. smp_send_stop();
  263. smp_call_ipl_cpu(__machine_kexec, image);
  264. }