reboot.S 4.2 KB

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