reboot.S 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. #include <linux/linkage.h>
  2. #include <asm/segment.h>
  3. #include <asm/page_types.h>
  4. #include <asm/processor-flags.h>
  5. #include <asm/msr-index.h>
  6. #include "realmode.h"
  7. /*
  8. * The following code and data reboots the machine by switching to real
  9. * mode and jumping to the BIOS reset entry point, as if the CPU has
  10. * really been reset. The previous version asked the keyboard
  11. * controller to pulse the CPU reset line, which is more thorough, but
  12. * doesn't work with at least one type of 486 motherboard. It is easy
  13. * to stop this code working; hence the copious comments.
  14. *
  15. * This code is called with the restart type (0 = BIOS, 1 = APM) in
  16. * the primary argument register (%eax for 32 bit, %edi for 64 bit).
  17. */
  18. .section ".text32", "ax"
  19. .code32
  20. ENTRY(machine_real_restart_asm)
  21. #ifdef CONFIG_X86_64
  22. /* Switch to trampoline GDT as it is guaranteed < 4 GiB */
  23. movl $__KERNEL_DS, %eax
  24. movl %eax, %ds
  25. lgdtl pa_tr_gdt
  26. /* Disable paging to drop us out of long mode */
  27. movl %cr0, %eax
  28. andl $~X86_CR0_PG, %eax
  29. movl %eax, %cr0
  30. ljmpl $__KERNEL32_CS, $pa_machine_real_restart_paging_off
  31. GLOBAL(machine_real_restart_paging_off)
  32. xorl %eax, %eax
  33. xorl %edx, %edx
  34. movl $MSR_EFER, %ecx
  35. wrmsr
  36. movl %edi, %eax
  37. #endif /* CONFIG_X86_64 */
  38. /* Set up the IDT for real mode. */
  39. lidtl pa_machine_real_restart_idt
  40. /*
  41. * Set up a GDT from which we can load segment descriptors for real
  42. * mode. The GDT is not used in real mode; it is just needed here to
  43. * prepare the descriptors.
  44. */
  45. lgdtl pa_machine_real_restart_gdt
  46. /*
  47. * Load the data segment registers with 16-bit compatible values
  48. */
  49. movl $16, %ecx
  50. movl %ecx, %ds
  51. movl %ecx, %es
  52. movl %ecx, %fs
  53. movl %ecx, %gs
  54. movl %ecx, %ss
  55. ljmpw $8, $1f
  56. /*
  57. * This is 16-bit protected mode code to disable paging and the cache,
  58. * switch to real mode and jump to the BIOS reset code.
  59. *
  60. * The instruction that switches to real mode by writing to CR0 must be
  61. * followed immediately by a far jump instruction, which set CS to a
  62. * valid value for real mode, and flushes the prefetch queue to avoid
  63. * running instructions that have already been decoded in protected
  64. * mode.
  65. *
  66. * Clears all the flags except ET, especially PG (paging), PE
  67. * (protected-mode enable) and TS (task switch for coprocessor state
  68. * save). Flushes the TLB after paging has been disabled. Sets CD and
  69. * NW, to disable the cache on a 486, and invalidates the cache. This
  70. * is more like the state of a 486 after reset. I don't know if
  71. * something else should be done for other chips.
  72. *
  73. * More could be done here to set up the registers as if a CPU reset had
  74. * occurred; hopefully real BIOSs don't assume much. This is not the
  75. * actual BIOS entry point, anyway (that is at 0xfffffff0).
  76. *
  77. * Most of this work is probably excessive, but it is what is tested.
  78. */
  79. .text
  80. .code16
  81. .balign 16
  82. machine_real_restart_asm16:
  83. 1:
  84. xorl %ecx, %ecx
  85. movl %cr0, %edx
  86. andl $0x00000011, %edx
  87. orl $0x60000000, %edx
  88. movl %edx, %cr0
  89. movl %ecx, %cr3
  90. movl %cr0, %edx
  91. testl $0x60000000, %edx /* If no cache bits -> no wbinvd */
  92. jz 2f
  93. wbinvd
  94. 2:
  95. andb $0x10, %dl
  96. movl %edx, %cr0
  97. LJMPW_RM(3f)
  98. 3:
  99. andw %ax, %ax
  100. jz bios
  101. apm:
  102. movw $0x1000, %ax
  103. movw %ax, %ss
  104. movw $0xf000, %sp
  105. movw $0x5307, %ax
  106. movw $0x0001, %bx
  107. movw $0x0003, %cx
  108. int $0x15
  109. /* This should never return... */
  110. bios:
  111. ljmpw $0xf000, $0xfff0
  112. .section ".rodata", "a"
  113. .balign 16
  114. GLOBAL(machine_real_restart_idt)
  115. .word 0xffff /* Length - real mode default value */
  116. .long 0 /* Base - real mode default value */
  117. END(machine_real_restart_idt)
  118. .balign 16
  119. GLOBAL(machine_real_restart_gdt)
  120. /* Self-pointer */
  121. .word 0xffff /* Length - real mode default value */
  122. .long pa_machine_real_restart_gdt
  123. .word 0
  124. /*
  125. * 16-bit code segment pointing to real_mode_seg
  126. * Selector value 8
  127. */
  128. .word 0xffff /* Limit */
  129. .long 0x9b000000 + pa_real_mode_base
  130. .word 0
  131. /*
  132. * 16-bit data segment with the selector value 16 = 0x10 and
  133. * base value 0x100; since this is consistent with real mode
  134. * semantics we don't have to reload the segments once CR0.PE = 0.
  135. */
  136. .quad GDT_ENTRY(0x0093, 0x100, 0xffff)
  137. END(machine_real_restart_gdt)