machine_kexec_32.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. * handle transition of Linux booting another kernel
  3. * Copyright (C) 2002-2005 Eric Biederman <ebiederm@xmission.com>
  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/mm.h>
  9. #include <linux/kexec.h>
  10. #include <linux/delay.h>
  11. #include <linux/init.h>
  12. #include <asm/pgtable.h>
  13. #include <asm/pgalloc.h>
  14. #include <asm/tlbflush.h>
  15. #include <asm/mmu_context.h>
  16. #include <asm/io.h>
  17. #include <asm/apic.h>
  18. #include <asm/cpufeature.h>
  19. #include <asm/desc.h>
  20. #include <asm/system.h>
  21. #define PAGE_ALIGNED __attribute__ ((__aligned__(PAGE_SIZE)))
  22. static u32 kexec_pgd[1024] PAGE_ALIGNED;
  23. #ifdef CONFIG_X86_PAE
  24. static u32 kexec_pmd0[1024] PAGE_ALIGNED;
  25. static u32 kexec_pmd1[1024] PAGE_ALIGNED;
  26. #endif
  27. static u32 kexec_pte0[1024] PAGE_ALIGNED;
  28. static u32 kexec_pte1[1024] PAGE_ALIGNED;
  29. static void set_idt(void *newidt, __u16 limit)
  30. {
  31. struct Xgt_desc_struct curidt;
  32. /* ia32 supports unaliged loads & stores */
  33. curidt.size = limit;
  34. curidt.address = (unsigned long)newidt;
  35. load_idt(&curidt);
  36. };
  37. static void set_gdt(void *newgdt, __u16 limit)
  38. {
  39. struct Xgt_desc_struct curgdt;
  40. /* ia32 supports unaligned loads & stores */
  41. curgdt.size = limit;
  42. curgdt.address = (unsigned long)newgdt;
  43. load_gdt(&curgdt);
  44. };
  45. static void load_segments(void)
  46. {
  47. #define __STR(X) #X
  48. #define STR(X) __STR(X)
  49. __asm__ __volatile__ (
  50. "\tljmp $"STR(__KERNEL_CS)",$1f\n"
  51. "\t1:\n"
  52. "\tmovl $"STR(__KERNEL_DS)",%%eax\n"
  53. "\tmovl %%eax,%%ds\n"
  54. "\tmovl %%eax,%%es\n"
  55. "\tmovl %%eax,%%fs\n"
  56. "\tmovl %%eax,%%gs\n"
  57. "\tmovl %%eax,%%ss\n"
  58. ::: "eax", "memory");
  59. #undef STR
  60. #undef __STR
  61. }
  62. /*
  63. * A architecture hook called to validate the
  64. * proposed image and prepare the control pages
  65. * as needed. The pages for KEXEC_CONTROL_CODE_SIZE
  66. * have been allocated, but the segments have yet
  67. * been copied into the kernel.
  68. *
  69. * Do what every setup is needed on image and the
  70. * reboot code buffer to allow us to avoid allocations
  71. * later.
  72. *
  73. * Currently nothing.
  74. */
  75. int machine_kexec_prepare(struct kimage *image)
  76. {
  77. return 0;
  78. }
  79. /*
  80. * Undo anything leftover by machine_kexec_prepare
  81. * when an image is freed.
  82. */
  83. void machine_kexec_cleanup(struct kimage *image)
  84. {
  85. }
  86. /*
  87. * Do not allocate memory (or fail in any way) in machine_kexec().
  88. * We are past the point of no return, committed to rebooting now.
  89. */
  90. NORET_TYPE void machine_kexec(struct kimage *image)
  91. {
  92. unsigned long page_list[PAGES_NR];
  93. void *control_page;
  94. /* Interrupts aren't acceptable while we reboot */
  95. local_irq_disable();
  96. control_page = page_address(image->control_code_page);
  97. memcpy(control_page, relocate_kernel, PAGE_SIZE);
  98. page_list[PA_CONTROL_PAGE] = __pa(control_page);
  99. page_list[VA_CONTROL_PAGE] = (unsigned long)relocate_kernel;
  100. page_list[PA_PGD] = __pa(kexec_pgd);
  101. page_list[VA_PGD] = (unsigned long)kexec_pgd;
  102. #ifdef CONFIG_X86_PAE
  103. page_list[PA_PMD_0] = __pa(kexec_pmd0);
  104. page_list[VA_PMD_0] = (unsigned long)kexec_pmd0;
  105. page_list[PA_PMD_1] = __pa(kexec_pmd1);
  106. page_list[VA_PMD_1] = (unsigned long)kexec_pmd1;
  107. #endif
  108. page_list[PA_PTE_0] = __pa(kexec_pte0);
  109. page_list[VA_PTE_0] = (unsigned long)kexec_pte0;
  110. page_list[PA_PTE_1] = __pa(kexec_pte1);
  111. page_list[VA_PTE_1] = (unsigned long)kexec_pte1;
  112. /* The segment registers are funny things, they have both a
  113. * visible and an invisible part. Whenever the visible part is
  114. * set to a specific selector, the invisible part is loaded
  115. * with from a table in memory. At no other time is the
  116. * descriptor table in memory accessed.
  117. *
  118. * I take advantage of this here by force loading the
  119. * segments, before I zap the gdt with an invalid value.
  120. */
  121. load_segments();
  122. /* The gdt & idt are now invalid.
  123. * If you want to load them you must set up your own idt & gdt.
  124. */
  125. set_gdt(phys_to_virt(0),0);
  126. set_idt(phys_to_virt(0),0);
  127. /* now call it */
  128. relocate_kernel((unsigned long)image->head, (unsigned long)page_list,
  129. image->start, cpu_has_pae);
  130. }
  131. /* crashkernel=size@addr specifies the location to reserve for
  132. * a crash kernel. By reserving this memory we guarantee
  133. * that linux never sets it up as a DMA target.
  134. * Useful for holding code to do something appropriate
  135. * after a kernel panic.
  136. */
  137. static int __init parse_crashkernel(char *arg)
  138. {
  139. unsigned long size, base;
  140. size = memparse(arg, &arg);
  141. if (*arg == '@') {
  142. base = memparse(arg+1, &arg);
  143. /* FIXME: Do I want a sanity check
  144. * to validate the memory range?
  145. */
  146. crashk_res.start = base;
  147. crashk_res.end = base + size - 1;
  148. }
  149. return 0;
  150. }
  151. early_param("crashkernel", parse_crashkernel);