machine_kexec.c 7.1 KB

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