blob: 6bf8feac5557d74b210a5f979c90d0554f22d6f6 [file] [log] [blame]
H. Peter Anvin3d35ac32011-02-14 18:36:03 -08001#include <linux/linkage.h>
2#include <linux/init.h>
3#include <asm/segment.h>
4#include <asm/page_types.h>
H. Peter Anvin65051392012-06-16 21:47:37 -07005#include <asm/processor-flags.h>
6#include <asm/msr-index.h>
H. Peter Anvine5684ec2012-05-08 21:22:37 +03007#include "realmode.h"
H. Peter Anvin3d35ac32011-02-14 18:36:03 -08008
9/*
10 * The following code and data reboots the machine by switching to real
11 * mode and jumping to the BIOS reset entry point, as if the CPU has
12 * really been reset. The previous version asked the keyboard
13 * controller to pulse the CPU reset line, which is more thorough, but
14 * doesn't work with at least one type of 486 motherboard. It is easy
15 * to stop this code working; hence the copious comments.
16 *
H. Peter Anvin65051392012-06-16 21:47:37 -070017 * This code is called with the restart type (0 = BIOS, 1 = APM) in
18 * the primary argument register (%eax for 32 bit, %edi for 64 bit).
H. Peter Anvin3d35ac32011-02-14 18:36:03 -080019 */
Jarkko Sakkinen5a8c9ae2012-05-08 21:22:27 +030020 .section ".text32", "ax"
H. Peter Anvin3d35ac32011-02-14 18:36:03 -080021 .code32
Jarkko Sakkinen8e029fc2012-05-08 21:22:40 +030022ENTRY(machine_real_restart_asm)
H. Peter Anvin65051392012-06-16 21:47:37 -070023
24#ifdef CONFIG_X86_64
25
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 jmp 1f /* "A branch" may be needed here, assume near is OK */
31
321:
33 xorl %eax, %eax
34 xorl %edx, %edx
35 movl $MSR_EFER, %ecx
36 wrmsr
37
38 movl %edi, %eax
39
40#endif /* CONFIG_X86_64 */
41
H. Peter Anvin3d35ac32011-02-14 18:36:03 -080042 /* Set up the IDT for real mode. */
Jarkko Sakkinen5a8c9ae2012-05-08 21:22:27 +030043 lidtl pa_machine_real_restart_idt
H. Peter Anvin3d35ac32011-02-14 18:36:03 -080044
45 /*
46 * Set up a GDT from which we can load segment descriptors for real
47 * mode. The GDT is not used in real mode; it is just needed here to
48 * prepare the descriptors.
49 */
Jarkko Sakkinen5a8c9ae2012-05-08 21:22:27 +030050 lgdtl pa_machine_real_restart_gdt
H. Peter Anvin3d35ac32011-02-14 18:36:03 -080051
52 /*
53 * Load the data segment registers with 16-bit compatible values
54 */
55 movl $16, %ecx
56 movl %ecx, %ds
57 movl %ecx, %es
58 movl %ecx, %fs
59 movl %ecx, %gs
60 movl %ecx, %ss
Jarkko Sakkinen5a8c9ae2012-05-08 21:22:27 +030061 ljmpw $8, $1f
H. Peter Anvin3d35ac32011-02-14 18:36:03 -080062
63/*
64 * This is 16-bit protected mode code to disable paging and the cache,
65 * switch to real mode and jump to the BIOS reset code.
66 *
67 * The instruction that switches to real mode by writing to CR0 must be
68 * followed immediately by a far jump instruction, which set CS to a
69 * valid value for real mode, and flushes the prefetch queue to avoid
70 * running instructions that have already been decoded in protected
71 * mode.
72 *
73 * Clears all the flags except ET, especially PG (paging), PE
74 * (protected-mode enable) and TS (task switch for coprocessor state
75 * save). Flushes the TLB after paging has been disabled. Sets CD and
76 * NW, to disable the cache on a 486, and invalidates the cache. This
77 * is more like the state of a 486 after reset. I don't know if
78 * something else should be done for other chips.
79 *
80 * More could be done here to set up the registers as if a CPU reset had
81 * occurred; hopefully real BIOSs don't assume much. This is not the
82 * actual BIOS entry point, anyway (that is at 0xfffffff0).
83 *
84 * Most of this work is probably excessive, but it is what is tested.
85 */
Jarkko Sakkinen5a8c9ae2012-05-08 21:22:27 +030086 .text
H. Peter Anvin3d35ac32011-02-14 18:36:03 -080087 .code16
Jarkko Sakkinen5a8c9ae2012-05-08 21:22:27 +030088
Jarkko Sakkinen8e029fc2012-05-08 21:22:40 +030089 .balign 16
Jarkko Sakkinen5a8c9ae2012-05-08 21:22:27 +030090machine_real_restart_asm16:
H. Peter Anvin3d35ac32011-02-14 18:36:03 -0800911:
92 xorl %ecx, %ecx
Jarkko Sakkinen5a8c9ae2012-05-08 21:22:27 +030093 movl %cr0, %edx
94 andl $0x00000011, %edx
95 orl $0x60000000, %edx
96 movl %edx, %cr0
H. Peter Anvin3d35ac32011-02-14 18:36:03 -080097 movl %ecx, %cr3
98 movl %cr0, %edx
Jarkko Sakkinen34d0b022012-05-10 10:11:38 +030099 testl $0x60000000, %edx /* If no cache bits -> no wbinvd */
H. Peter Anvin3d35ac32011-02-14 18:36:03 -0800100 jz 2f
101 wbinvd
1022:
Jarkko Sakkinen5a8c9ae2012-05-08 21:22:27 +0300103 andb $0x10, %dl
104 movl %edx, %cr0
H. Peter Anvine5684ec2012-05-08 21:22:37 +0300105 LJMPW_RM(3f)
Jarkko Sakkinen5a8c9ae2012-05-08 21:22:27 +03001063:
H. Peter Anvin6feb5922012-05-08 21:22:39 +0300107 andw %ax, %ax
Jarkko Sakkinen5a8c9ae2012-05-08 21:22:27 +0300108 jz bios
H. Peter Anvin3d35ac32011-02-14 18:36:03 -0800109
110apm:
111 movw $0x1000, %ax
112 movw %ax, %ss
113 movw $0xf000, %sp
114 movw $0x5307, %ax
115 movw $0x0001, %bx
116 movw $0x0003, %cx
117 int $0x15
Jarkko Sakkinen5a8c9ae2012-05-08 21:22:27 +0300118 /* This should never return... */
H. Peter Anvin3d35ac32011-02-14 18:36:03 -0800119
Jarkko Sakkinen5a8c9ae2012-05-08 21:22:27 +0300120bios:
121 ljmpw $0xf000, $0xfff0
H. Peter Anvin3d35ac32011-02-14 18:36:03 -0800122
Jarkko Sakkinen5a8c9ae2012-05-08 21:22:27 +0300123 .section ".rodata", "a"
H. Peter Anvin3d35ac32011-02-14 18:36:03 -0800124
Jarkko Sakkinen8e029fc2012-05-08 21:22:40 +0300125 .balign 16
126GLOBAL(machine_real_restart_idt)
H. Peter Anvin3d35ac32011-02-14 18:36:03 -0800127 .word 0xffff /* Length - real mode default value */
128 .long 0 /* Base - real mode default value */
Jarkko Sakkinen8e029fc2012-05-08 21:22:40 +0300129END(machine_real_restart_idt)
H. Peter Anvin3d35ac32011-02-14 18:36:03 -0800130
Jarkko Sakkinen8e029fc2012-05-08 21:22:40 +0300131 .balign 16
132GLOBAL(machine_real_restart_gdt)
Jarkko Sakkinen5a8c9ae2012-05-08 21:22:27 +0300133 /* Self-pointer */
134 .word 0xffff /* Length - real mode default value */
135 .long pa_machine_real_restart_gdt
136 .word 0
137
138 /*
139 * 16-bit code segment pointing to real_mode_seg
140 * Selector value 8
141 */
142 .word 0xffff /* Limit */
143 .long 0x9b000000 + pa_real_mode_base
144 .word 0
145
H. Peter Anvin3d35ac32011-02-14 18:36:03 -0800146 /*
147 * 16-bit data segment with the selector value 16 = 0x10 and
148 * base value 0x100; since this is consistent with real mode
149 * semantics we don't have to reload the segments once CR0.PE = 0.
150 */
151 .quad GDT_ENTRY(0x0093, 0x100, 0xffff)
Jarkko Sakkinen8e029fc2012-05-08 21:22:40 +0300152END(machine_real_restart_gdt)