machine_kexec.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. #endif
  25. int
  26. machine_kexec_prepare(struct kimage *kimage)
  27. {
  28. if (_machine_kexec_prepare)
  29. return _machine_kexec_prepare(kimage);
  30. return 0;
  31. }
  32. void
  33. machine_kexec_cleanup(struct kimage *kimage)
  34. {
  35. }
  36. void
  37. machine_shutdown(void)
  38. {
  39. if (_machine_kexec_shutdown)
  40. _machine_kexec_shutdown();
  41. }
  42. void
  43. machine_crash_shutdown(struct pt_regs *regs)
  44. {
  45. if (_machine_crash_shutdown)
  46. _machine_crash_shutdown(regs);
  47. else
  48. default_machine_crash_shutdown(regs);
  49. }
  50. typedef void (*noretfun_t)(void) __noreturn;
  51. void
  52. machine_kexec(struct kimage *image)
  53. {
  54. unsigned long reboot_code_buffer;
  55. unsigned long entry;
  56. unsigned long *ptr;
  57. reboot_code_buffer =
  58. (unsigned long)page_address(image->control_code_page);
  59. kexec_start_address =
  60. (unsigned long) phys_to_virt(image->start);
  61. if (image->type == KEXEC_TYPE_DEFAULT) {
  62. kexec_indirection_page =
  63. (unsigned long) phys_to_virt(image->head & PAGE_MASK);
  64. } else {
  65. kexec_indirection_page = (unsigned long)&image->head;
  66. }
  67. memcpy((void*)reboot_code_buffer, relocate_new_kernel,
  68. relocate_new_kernel_size);
  69. /*
  70. * The generic kexec code builds a page list with physical
  71. * addresses. they are directly accessible through KSEG0 (or
  72. * CKSEG0 or XPHYS if on 64bit system), hence the
  73. * phys_to_virt() call.
  74. */
  75. for (ptr = &image->head; (entry = *ptr) && !(entry &IND_DONE);
  76. ptr = (entry & IND_INDIRECTION) ?
  77. phys_to_virt(entry & PAGE_MASK) : ptr + 1) {
  78. if (*ptr & IND_SOURCE || *ptr & IND_INDIRECTION ||
  79. *ptr & IND_DESTINATION)
  80. *ptr = (unsigned long) phys_to_virt(*ptr);
  81. }
  82. /* Mark offline BEFORE disabling local irq. */
  83. set_cpu_online(smp_processor_id(), false);
  84. /*
  85. * we do not want to be bothered.
  86. */
  87. local_irq_disable();
  88. printk("Will call new kernel at %08lx\n", image->start);
  89. printk("Bye ...\n");
  90. __flush_cache_all();
  91. #ifdef CONFIG_SMP
  92. /* All secondary cpus now may jump to kexec_wait cycle */
  93. relocated_kexec_smp_wait = reboot_code_buffer +
  94. (void *)(kexec_smp_wait - relocate_new_kernel);
  95. smp_wmb();
  96. atomic_set(&kexec_ready_to_reboot, 1);
  97. #endif
  98. ((noretfun_t) reboot_code_buffer)();
  99. }