machine_kexec.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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/ipl.h>
  23. #include <asm/diag.h>
  24. #include <asm/elf.h>
  25. #include <asm/asm-offsets.h>
  26. #include <asm/cacheflush.h>
  27. #include <asm/os_info.h>
  28. #include <asm/set_memory.h>
  29. #include <asm/switch_to.h>
  30. #include <asm/nmi.h>
  31. typedef void (*relocate_kernel_t)(kimage_entry_t *, unsigned long);
  32. extern const unsigned char relocate_kernel[];
  33. extern const unsigned long long relocate_kernel_len;
  34. #ifdef CONFIG_CRASH_DUMP
  35. /*
  36. * PM notifier callback for kdump
  37. */
  38. static int machine_kdump_pm_cb(struct notifier_block *nb, unsigned long action,
  39. void *ptr)
  40. {
  41. switch (action) {
  42. case PM_SUSPEND_PREPARE:
  43. case PM_HIBERNATION_PREPARE:
  44. if (kexec_crash_image)
  45. arch_kexec_unprotect_crashkres();
  46. break;
  47. case PM_POST_SUSPEND:
  48. case PM_POST_HIBERNATION:
  49. if (kexec_crash_image)
  50. arch_kexec_protect_crashkres();
  51. break;
  52. default:
  53. return NOTIFY_DONE;
  54. }
  55. return NOTIFY_OK;
  56. }
  57. static int __init machine_kdump_pm_init(void)
  58. {
  59. pm_notifier(machine_kdump_pm_cb, 0);
  60. return 0;
  61. }
  62. arch_initcall(machine_kdump_pm_init);
  63. /*
  64. * Reset the system, copy boot CPU registers to absolute zero,
  65. * and jump to the kdump image
  66. */
  67. static void __do_machine_kdump(void *image)
  68. {
  69. int (*start_kdump)(int);
  70. unsigned long prefix;
  71. /* store_status() saved the prefix register to lowcore */
  72. prefix = (unsigned long) S390_lowcore.prefixreg_save_area;
  73. /* Now do the reset */
  74. s390_reset_system();
  75. /*
  76. * Copy dump CPU store status info to absolute zero.
  77. * This need to be done *after* s390_reset_system set the
  78. * prefix register of this CPU to zero
  79. */
  80. memcpy((void *) __LC_FPREGS_SAVE_AREA,
  81. (void *)(prefix + __LC_FPREGS_SAVE_AREA), 512);
  82. __load_psw_mask(PSW_MASK_BASE | PSW_DEFAULT_KEY | PSW_MASK_EA | PSW_MASK_BA);
  83. start_kdump = (void *)((struct kimage *) image)->start;
  84. start_kdump(1);
  85. /* Die if start_kdump returns */
  86. disabled_wait((unsigned long) __builtin_return_address(0));
  87. }
  88. /*
  89. * Start kdump: create a LGR log entry, store status of all CPUs and
  90. * branch to __do_machine_kdump.
  91. */
  92. static noinline void __machine_kdump(void *image)
  93. {
  94. struct mcesa *mcesa;
  95. union ctlreg2 cr2_old, cr2_new;
  96. int this_cpu, cpu;
  97. lgr_info_log();
  98. /* Get status of the other CPUs */
  99. this_cpu = smp_find_processor_id(stap());
  100. for_each_online_cpu(cpu) {
  101. if (cpu == this_cpu)
  102. continue;
  103. if (smp_store_status(cpu))
  104. continue;
  105. }
  106. /* Store status of the boot CPU */
  107. mcesa = (struct mcesa *)(S390_lowcore.mcesad & MCESA_ORIGIN_MASK);
  108. if (MACHINE_HAS_VX)
  109. save_vx_regs((__vector128 *) mcesa->vector_save_area);
  110. if (MACHINE_HAS_GS) {
  111. __ctl_store(cr2_old.val, 2, 2);
  112. cr2_new = cr2_old;
  113. cr2_new.gse = 1;
  114. __ctl_load(cr2_new.val, 2, 2);
  115. save_gs_cb((struct gs_cb *) mcesa->guarded_storage_save_area);
  116. __ctl_load(cr2_old.val, 2, 2);
  117. }
  118. /*
  119. * To create a good backchain for this CPU in the dump store_status
  120. * is passed the address of a function. The address is saved into
  121. * the PSW save area of the boot CPU and the function is invoked as
  122. * a tail call of store_status. The backchain in the dump will look
  123. * like this:
  124. * restart_int_handler -> __machine_kexec -> __do_machine_kdump
  125. * The call to store_status() will not return.
  126. */
  127. store_status(__do_machine_kdump, image);
  128. }
  129. #endif
  130. /*
  131. * Check if kdump checksums are valid: We call purgatory with parameter "0"
  132. */
  133. static bool kdump_csum_valid(struct kimage *image)
  134. {
  135. #ifdef CONFIG_CRASH_DUMP
  136. int (*start_kdump)(int) = (void *)image->start;
  137. int rc;
  138. __arch_local_irq_stnsm(0xfb); /* disable DAT */
  139. rc = start_kdump(0);
  140. __arch_local_irq_stosm(0x04); /* enable DAT */
  141. return rc == 0;
  142. #else
  143. return false;
  144. #endif
  145. }
  146. #ifdef CONFIG_CRASH_DUMP
  147. void crash_free_reserved_phys_range(unsigned long begin, unsigned long end)
  148. {
  149. unsigned long addr, size;
  150. for (addr = begin; addr < end; addr += PAGE_SIZE)
  151. free_reserved_page(pfn_to_page(addr >> PAGE_SHIFT));
  152. size = begin - crashk_res.start;
  153. if (size)
  154. os_info_crashkernel_add(crashk_res.start, size);
  155. else
  156. os_info_crashkernel_add(0, 0);
  157. }
  158. static void crash_protect_pages(int protect)
  159. {
  160. unsigned long size;
  161. if (!crashk_res.end)
  162. return;
  163. size = resource_size(&crashk_res);
  164. if (protect)
  165. set_memory_ro(crashk_res.start, size >> PAGE_SHIFT);
  166. else
  167. set_memory_rw(crashk_res.start, size >> PAGE_SHIFT);
  168. }
  169. void arch_kexec_protect_crashkres(void)
  170. {
  171. crash_protect_pages(1);
  172. }
  173. void arch_kexec_unprotect_crashkres(void)
  174. {
  175. crash_protect_pages(0);
  176. }
  177. #endif
  178. /*
  179. * Give back memory to hypervisor before new kdump is loaded
  180. */
  181. static int machine_kexec_prepare_kdump(void)
  182. {
  183. #ifdef CONFIG_CRASH_DUMP
  184. if (MACHINE_IS_VM)
  185. diag10_range(PFN_DOWN(crashk_res.start),
  186. PFN_DOWN(crashk_res.end - crashk_res.start + 1));
  187. return 0;
  188. #else
  189. return -EINVAL;
  190. #endif
  191. }
  192. int machine_kexec_prepare(struct kimage *image)
  193. {
  194. void *reboot_code_buffer;
  195. if (image->type == KEXEC_TYPE_CRASH)
  196. return machine_kexec_prepare_kdump();
  197. /* We don't support anything but the default image type for now. */
  198. if (image->type != KEXEC_TYPE_DEFAULT)
  199. return -EINVAL;
  200. /* Get the destination where the assembler code should be copied to.*/
  201. reboot_code_buffer = (void *) page_to_phys(image->control_code_page);
  202. /* Then copy it */
  203. memcpy(reboot_code_buffer, relocate_kernel, relocate_kernel_len);
  204. return 0;
  205. }
  206. void machine_kexec_cleanup(struct kimage *image)
  207. {
  208. }
  209. void arch_crash_save_vmcoreinfo(void)
  210. {
  211. VMCOREINFO_SYMBOL(lowcore_ptr);
  212. VMCOREINFO_SYMBOL(high_memory);
  213. VMCOREINFO_LENGTH(lowcore_ptr, NR_CPUS);
  214. mem_assign_absolute(S390_lowcore.vmcore_info, paddr_vmcoreinfo_note());
  215. }
  216. void machine_shutdown(void)
  217. {
  218. }
  219. void machine_crash_shutdown(struct pt_regs *regs)
  220. {
  221. set_os_info_reipl_block();
  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. }