machine_kexec.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * machine_kexec.c for kexec
  3. * Created by <nschichan@corp.free.fr> on Thu Oct 12 15:15:06 2006
  4. *
  5. * This source code is licensed under the GNU General Public License,
  6. * Version 2. See the file COPYING for more details.
  7. */
  8. #include <linux/compiler.h>
  9. #include <linux/kexec.h>
  10. #include <linux/mm.h>
  11. #include <linux/delay.h>
  12. #include <asm/cacheflush.h>
  13. #include <asm/page.h>
  14. extern const unsigned char relocate_new_kernel[];
  15. extern const size_t relocate_new_kernel_size;
  16. extern unsigned long kexec_start_address;
  17. extern unsigned long kexec_indirection_page;
  18. int (*_machine_kexec_prepare)(struct kimage *) = NULL;
  19. void (*_machine_kexec_shutdown)(void) = NULL;
  20. void (*_machine_crash_shutdown)(struct pt_regs *regs) = NULL;
  21. #ifdef CONFIG_SMP
  22. void (*relocated_kexec_smp_wait) (void *);
  23. atomic_t kexec_ready_to_reboot = ATOMIC_INIT(0);
  24. void (*_crash_smp_send_stop)(void) = NULL;
  25. #endif
  26. static void kexec_image_info(const struct kimage *kimage)
  27. {
  28. unsigned long i;
  29. pr_debug("kexec kimage info:\n");
  30. pr_debug(" type: %d\n", kimage->type);
  31. pr_debug(" start: %lx\n", kimage->start);
  32. pr_debug(" head: %lx\n", kimage->head);
  33. pr_debug(" nr_segments: %lu\n", kimage->nr_segments);
  34. for (i = 0; i < kimage->nr_segments; i++) {
  35. pr_debug(" segment[%lu]: %016lx - %016lx, 0x%lx bytes, %lu pages\n",
  36. i,
  37. kimage->segment[i].mem,
  38. kimage->segment[i].mem + kimage->segment[i].memsz,
  39. (unsigned long)kimage->segment[i].memsz,
  40. (unsigned long)kimage->segment[i].memsz / PAGE_SIZE);
  41. }
  42. }
  43. int
  44. machine_kexec_prepare(struct kimage *kimage)
  45. {
  46. kexec_image_info(kimage);
  47. if (_machine_kexec_prepare)
  48. return _machine_kexec_prepare(kimage);
  49. return 0;
  50. }
  51. void
  52. machine_kexec_cleanup(struct kimage *kimage)
  53. {
  54. }
  55. void
  56. machine_shutdown(void)
  57. {
  58. if (_machine_kexec_shutdown)
  59. _machine_kexec_shutdown();
  60. }
  61. void
  62. machine_crash_shutdown(struct pt_regs *regs)
  63. {
  64. if (_machine_crash_shutdown)
  65. _machine_crash_shutdown(regs);
  66. else
  67. default_machine_crash_shutdown(regs);
  68. }
  69. typedef void (*noretfun_t)(void) __noreturn;
  70. void
  71. machine_kexec(struct kimage *image)
  72. {
  73. unsigned long reboot_code_buffer;
  74. unsigned long entry;
  75. unsigned long *ptr;
  76. reboot_code_buffer =
  77. (unsigned long)page_address(image->control_code_page);
  78. kexec_start_address =
  79. (unsigned long) phys_to_virt(image->start);
  80. if (image->type == KEXEC_TYPE_DEFAULT) {
  81. kexec_indirection_page =
  82. (unsigned long) phys_to_virt(image->head & PAGE_MASK);
  83. } else {
  84. kexec_indirection_page = (unsigned long)&image->head;
  85. }
  86. memcpy((void*)reboot_code_buffer, relocate_new_kernel,
  87. relocate_new_kernel_size);
  88. /*
  89. * The generic kexec code builds a page list with physical
  90. * addresses. they are directly accessible through KSEG0 (or
  91. * CKSEG0 or XPHYS if on 64bit system), hence the
  92. * phys_to_virt() call.
  93. */
  94. for (ptr = &image->head; (entry = *ptr) && !(entry &IND_DONE);
  95. ptr = (entry & IND_INDIRECTION) ?
  96. phys_to_virt(entry & PAGE_MASK) : ptr + 1) {
  97. if (*ptr & IND_SOURCE || *ptr & IND_INDIRECTION ||
  98. *ptr & IND_DESTINATION)
  99. *ptr = (unsigned long) phys_to_virt(*ptr);
  100. }
  101. /* Mark offline BEFORE disabling local irq. */
  102. set_cpu_online(smp_processor_id(), false);
  103. /*
  104. * we do not want to be bothered.
  105. */
  106. local_irq_disable();
  107. printk("Will call new kernel at %08lx\n", image->start);
  108. printk("Bye ...\n");
  109. __flush_cache_all();
  110. #ifdef CONFIG_SMP
  111. /* All secondary cpus now may jump to kexec_wait cycle */
  112. relocated_kexec_smp_wait = reboot_code_buffer +
  113. (void *)(kexec_smp_wait - relocate_new_kernel);
  114. smp_wmb();
  115. atomic_set(&kexec_ready_to_reboot, 1);
  116. #endif
  117. ((noretfun_t) reboot_code_buffer)();
  118. }