machine_kexec.c 7.0 KB

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