reboot_32.S 3.8 KB

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