machine_kexec.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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 <asm/cio.h>
  16. #include <asm/setup.h>
  17. #include <asm/pgtable.h>
  18. #include <asm/pgalloc.h>
  19. #include <asm/smp.h>
  20. #include <asm/reset.h>
  21. #include <asm/ipl.h>
  22. #include <asm/diag.h>
  23. #include <asm/asm-offsets.h>
  24. #include <asm/os_info.h>
  25. typedef void (*relocate_kernel_t)(kimage_entry_t *, unsigned long);
  26. extern const unsigned char relocate_kernel[];
  27. extern const unsigned long long relocate_kernel_len;
  28. #ifdef CONFIG_CRASH_DUMP
  29. void *fill_cpu_elf_notes(void *ptr, struct save_area *sa);
  30. /*
  31. * Create ELF notes for one CPU
  32. */
  33. static void add_elf_notes(int cpu)
  34. {
  35. struct save_area *sa = (void *) 4608 + store_prefix();
  36. void *ptr;
  37. memcpy((void *) (4608UL + sa->pref_reg), sa, sizeof(*sa));
  38. ptr = (u64 *) per_cpu_ptr(crash_notes, cpu);
  39. ptr = fill_cpu_elf_notes(ptr, sa);
  40. memset(ptr, 0, sizeof(struct elf_note));
  41. }
  42. /*
  43. * Initialize CPU ELF notes
  44. */
  45. void setup_regs(void)
  46. {
  47. unsigned long sa = S390_lowcore.prefixreg_save_area + SAVE_AREA_BASE;
  48. int cpu, this_cpu;
  49. this_cpu = smp_find_processor_id(stap());
  50. add_elf_notes(this_cpu);
  51. for_each_online_cpu(cpu) {
  52. if (cpu == this_cpu)
  53. continue;
  54. if (smp_store_status(cpu))
  55. continue;
  56. add_elf_notes(cpu);
  57. }
  58. /* Copy dump CPU store status info to absolute zero */
  59. memcpy((void *) SAVE_AREA_BASE, (void *) sa, sizeof(struct save_area));
  60. }
  61. #endif
  62. /*
  63. * Start kdump: We expect here that a store status has been done on our CPU
  64. */
  65. static void __do_machine_kdump(void *image)
  66. {
  67. #ifdef CONFIG_CRASH_DUMP
  68. int (*start_kdump)(int) = (void *)((struct kimage *) image)->start;
  69. setup_regs();
  70. __load_psw_mask(PSW_MASK_BASE | PSW_DEFAULT_KEY | PSW_MASK_EA | PSW_MASK_BA);
  71. start_kdump(1);
  72. #endif
  73. }
  74. /*
  75. * Check if kdump checksums are valid: We call purgatory with parameter "0"
  76. */
  77. static int kdump_csum_valid(struct kimage *image)
  78. {
  79. #ifdef CONFIG_CRASH_DUMP
  80. int (*start_kdump)(int) = (void *)image->start;
  81. int rc;
  82. __arch_local_irq_stnsm(0xfb); /* disable DAT */
  83. rc = start_kdump(0);
  84. __arch_local_irq_stosm(0x04); /* enable DAT */
  85. return rc ? 0 : -EINVAL;
  86. #else
  87. return -EINVAL;
  88. #endif
  89. }
  90. /*
  91. * Map or unmap crashkernel memory
  92. */
  93. static void crash_map_pages(int enable)
  94. {
  95. unsigned long size = resource_size(&crashk_res);
  96. BUG_ON(crashk_res.start % KEXEC_CRASH_MEM_ALIGN ||
  97. size % KEXEC_CRASH_MEM_ALIGN);
  98. if (enable)
  99. vmem_add_mapping(crashk_res.start, size);
  100. else {
  101. vmem_remove_mapping(crashk_res.start, size);
  102. if (size)
  103. os_info_crashkernel_add(crashk_res.start, size);
  104. else
  105. os_info_crashkernel_add(0, 0);
  106. }
  107. }
  108. /*
  109. * Map crashkernel memory
  110. */
  111. void crash_map_reserved_pages(void)
  112. {
  113. crash_map_pages(1);
  114. }
  115. /*
  116. * Unmap crashkernel memory
  117. */
  118. void crash_unmap_reserved_pages(void)
  119. {
  120. crash_map_pages(0);
  121. }
  122. /*
  123. * Give back memory to hypervisor before new kdump is loaded
  124. */
  125. static int machine_kexec_prepare_kdump(void)
  126. {
  127. #ifdef CONFIG_CRASH_DUMP
  128. if (MACHINE_IS_VM)
  129. diag10_range(PFN_DOWN(crashk_res.start),
  130. PFN_DOWN(crashk_res.end - crashk_res.start + 1));
  131. return 0;
  132. #else
  133. return -EINVAL;
  134. #endif
  135. }
  136. int machine_kexec_prepare(struct kimage *image)
  137. {
  138. void *reboot_code_buffer;
  139. /* Can't replace kernel image since it is read-only. */
  140. if (ipl_flags & IPL_NSS_VALID)
  141. return -ENOSYS;
  142. if (image->type == KEXEC_TYPE_CRASH)
  143. return machine_kexec_prepare_kdump();
  144. /* We don't support anything but the default image type for now. */
  145. if (image->type != KEXEC_TYPE_DEFAULT)
  146. return -EINVAL;
  147. /* Get the destination where the assembler code should be copied to.*/
  148. reboot_code_buffer = (void *) page_to_phys(image->control_code_page);
  149. /* Then copy it */
  150. memcpy(reboot_code_buffer, relocate_kernel, relocate_kernel_len);
  151. return 0;
  152. }
  153. void machine_kexec_cleanup(struct kimage *image)
  154. {
  155. }
  156. void arch_crash_save_vmcoreinfo(void)
  157. {
  158. VMCOREINFO_SYMBOL(lowcore_ptr);
  159. VMCOREINFO_SYMBOL(high_memory);
  160. VMCOREINFO_LENGTH(lowcore_ptr, NR_CPUS);
  161. }
  162. void machine_shutdown(void)
  163. {
  164. }
  165. /*
  166. * Do normal kexec
  167. */
  168. static void __do_machine_kexec(void *data)
  169. {
  170. relocate_kernel_t data_mover;
  171. struct kimage *image = data;
  172. data_mover = (relocate_kernel_t) page_to_phys(image->control_code_page);
  173. /* Call the moving routine */
  174. (*data_mover)(&image->head, image->start);
  175. }
  176. /*
  177. * Reset system and call either kdump or normal kexec
  178. */
  179. static void __machine_kexec(void *data)
  180. {
  181. struct kimage *image = data;
  182. __arch_local_irq_stosm(0x04); /* enable DAT */
  183. pfault_fini();
  184. tracing_off();
  185. debug_locks_off();
  186. if (image->type == KEXEC_TYPE_CRASH) {
  187. lgr_info_log();
  188. s390_reset_system(__do_machine_kdump, data);
  189. } else {
  190. s390_reset_system(__do_machine_kexec, data);
  191. }
  192. disabled_wait((unsigned long) __builtin_return_address(0));
  193. }
  194. /*
  195. * Do either kdump or normal kexec. In case of kdump we first ask
  196. * purgatory, if kdump checksums are valid.
  197. */
  198. void machine_kexec(struct kimage *image)
  199. {
  200. if (image->type == KEXEC_TYPE_CRASH && !kdump_csum_valid(image))
  201. return;
  202. tracer_disable();
  203. smp_send_stop();
  204. smp_call_ipl_cpu(__machine_kexec, image);
  205. }