machine_kexec.c 5.5 KB

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