1 /* SPDX-License-Identifier: GPL-2.0 */
2 #include <linux/linkage.h>
3 #include <asm/desc_defs.h>
4 #include <asm/segment.h>
5 #include <asm/page_types.h>
6 #include <asm/processor-flags.h>
7 #include <asm/msr-index.h>
11 * The following code and data reboots the machine by switching to real
12 * mode and jumping to the BIOS reset entry point, as if the CPU has
13 * really been reset. The previous version asked the keyboard
14 * controller to pulse the CPU reset line, which is more thorough, but
15 * doesn't work with at least one type of 486 motherboard. It is easy
16 * to stop this code working; hence the copious comments.
18 * This code is called with the restart type (0 = BIOS, 1 = APM) in
19 * the primary argument register (%eax for 32 bit, %edi for 64 bit).
21 .section ".text32", "ax"
23 SYM_CODE_START(machine_real_restart_asm)
26 /* Switch to trampoline GDT as it is guaranteed < 4 GiB */
27 movl $__KERNEL_DS, %eax
31 /* Disable paging to drop us out of long mode */
33 andl $~X86_CR0_PG, %eax
35 ljmpl $__KERNEL32_CS, $pa_machine_real_restart_paging_off
37 SYM_INNER_LABEL(machine_real_restart_paging_off, SYM_L_GLOBAL)
45 #endif /* CONFIG_X86_64 */
47 /* Set up the IDT for real mode. */
48 lidtl pa_machine_real_restart_idt
51 * Set up a GDT from which we can load segment descriptors for real
52 * mode. The GDT is not used in real mode; it is just needed here to
53 * prepare the descriptors.
55 lgdtl pa_machine_real_restart_gdt
58 * Load the data segment registers with 16-bit compatible values
67 SYM_CODE_END(machine_real_restart_asm)
70 * This is 16-bit protected mode code to disable paging and the cache,
71 * switch to real mode and jump to the BIOS reset code.
73 * The instruction that switches to real mode by writing to CR0 must be
74 * followed immediately by a far jump instruction, which set CS to a
75 * valid value for real mode, and flushes the prefetch queue to avoid
76 * running instructions that have already been decoded in protected
79 * Clears all the flags except ET, especially PG (paging), PE
80 * (protected-mode enable) and TS (task switch for coprocessor state
81 * save). Flushes the TLB after paging has been disabled. Sets CD and
82 * NW, to disable the cache on a 486, and invalidates the cache. This
83 * is more like the state of a 486 after reset. I don't know if
84 * something else should be done for other chips.
86 * More could be done here to set up the registers as if a CPU reset had
87 * occurred; hopefully real BIOSs don't assume much. This is not the
88 * actual BIOS entry point, anyway (that is at 0xfffffff0).
90 * Most of this work is probably excessive, but it is what is tested.
96 machine_real_restart_asm16:
100 andl $0x00000011, %edx
101 orl $0x60000000, %edx
105 testl $0x60000000, %edx /* If no cache bits -> no wbinvd */
124 /* This should never return... */
127 ljmpw $0xf000, $0xfff0
129 .section ".rodata", "a"
132 SYM_DATA_START(machine_real_restart_idt)
133 .word 0xffff /* Length - real mode default value */
134 .long 0 /* Base - real mode default value */
135 SYM_DATA_END(machine_real_restart_idt)
138 SYM_DATA_START(machine_real_restart_gdt)
140 .word 0xffff /* Length - real mode default value */
141 .long pa_machine_real_restart_gdt
145 * 16-bit code segment pointing to real_mode_seg
148 .word 0xffff /* Limit */
149 .long 0x9b000000 + pa_real_mode_base
153 * 16-bit data segment with the selector value 16 = 0x10 and
154 * base value 0x100; since this is consistent with real mode
155 * semantics we don't have to reload the segments once CR0.PE = 0.
157 .quad GDT_ENTRY(DESC_DATA16, 0x100, 0xffff)
158 SYM_DATA_END(machine_real_restart_gdt)