H. Peter Anvin | 3d35ac3 | 2011-02-14 18:36:03 -0800 | [diff] [blame] | 1 | #include <linux/linkage.h> |
| 2 | #include <linux/init.h> |
| 3 | #include <asm/segment.h> |
| 4 | #include <asm/page_types.h> |
H. Peter Anvin | 6505139 | 2012-06-16 21:47:37 -0700 | [diff] [blame^] | 5 | #include <asm/processor-flags.h> |
| 6 | #include <asm/msr-index.h> |
H. Peter Anvin | e5684ec | 2012-05-08 21:22:37 +0300 | [diff] [blame] | 7 | #include "realmode.h" |
H. Peter Anvin | 3d35ac3 | 2011-02-14 18:36:03 -0800 | [diff] [blame] | 8 | |
| 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 Anvin | 6505139 | 2012-06-16 21:47:37 -0700 | [diff] [blame^] | 17 | * 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 Anvin | 3d35ac3 | 2011-02-14 18:36:03 -0800 | [diff] [blame] | 19 | */ |
Jarkko Sakkinen | 5a8c9ae | 2012-05-08 21:22:27 +0300 | [diff] [blame] | 20 | .section ".text32", "ax" |
H. Peter Anvin | 3d35ac3 | 2011-02-14 18:36:03 -0800 | [diff] [blame] | 21 | .code32 |
Jarkko Sakkinen | 8e029fc | 2012-05-08 21:22:40 +0300 | [diff] [blame] | 22 | ENTRY(machine_real_restart_asm) |
H. Peter Anvin | 6505139 | 2012-06-16 21:47:37 -0700 | [diff] [blame^] | 23 | |
| 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 | |
| 32 | 1: |
| 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 Anvin | 3d35ac3 | 2011-02-14 18:36:03 -0800 | [diff] [blame] | 42 | /* Set up the IDT for real mode. */ |
Jarkko Sakkinen | 5a8c9ae | 2012-05-08 21:22:27 +0300 | [diff] [blame] | 43 | lidtl pa_machine_real_restart_idt |
H. Peter Anvin | 3d35ac3 | 2011-02-14 18:36:03 -0800 | [diff] [blame] | 44 | |
| 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 Sakkinen | 5a8c9ae | 2012-05-08 21:22:27 +0300 | [diff] [blame] | 50 | lgdtl pa_machine_real_restart_gdt |
H. Peter Anvin | 3d35ac3 | 2011-02-14 18:36:03 -0800 | [diff] [blame] | 51 | |
| 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 Sakkinen | 5a8c9ae | 2012-05-08 21:22:27 +0300 | [diff] [blame] | 61 | ljmpw $8, $1f |
H. Peter Anvin | 3d35ac3 | 2011-02-14 18:36:03 -0800 | [diff] [blame] | 62 | |
| 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 Sakkinen | 5a8c9ae | 2012-05-08 21:22:27 +0300 | [diff] [blame] | 86 | .text |
H. Peter Anvin | 3d35ac3 | 2011-02-14 18:36:03 -0800 | [diff] [blame] | 87 | .code16 |
Jarkko Sakkinen | 5a8c9ae | 2012-05-08 21:22:27 +0300 | [diff] [blame] | 88 | |
Jarkko Sakkinen | 8e029fc | 2012-05-08 21:22:40 +0300 | [diff] [blame] | 89 | .balign 16 |
Jarkko Sakkinen | 5a8c9ae | 2012-05-08 21:22:27 +0300 | [diff] [blame] | 90 | machine_real_restart_asm16: |
H. Peter Anvin | 3d35ac3 | 2011-02-14 18:36:03 -0800 | [diff] [blame] | 91 | 1: |
| 92 | xorl %ecx, %ecx |
Jarkko Sakkinen | 5a8c9ae | 2012-05-08 21:22:27 +0300 | [diff] [blame] | 93 | movl %cr0, %edx |
| 94 | andl $0x00000011, %edx |
| 95 | orl $0x60000000, %edx |
| 96 | movl %edx, %cr0 |
H. Peter Anvin | 3d35ac3 | 2011-02-14 18:36:03 -0800 | [diff] [blame] | 97 | movl %ecx, %cr3 |
| 98 | movl %cr0, %edx |
Jarkko Sakkinen | 34d0b02 | 2012-05-10 10:11:38 +0300 | [diff] [blame] | 99 | testl $0x60000000, %edx /* If no cache bits -> no wbinvd */ |
H. Peter Anvin | 3d35ac3 | 2011-02-14 18:36:03 -0800 | [diff] [blame] | 100 | jz 2f |
| 101 | wbinvd |
| 102 | 2: |
Jarkko Sakkinen | 5a8c9ae | 2012-05-08 21:22:27 +0300 | [diff] [blame] | 103 | andb $0x10, %dl |
| 104 | movl %edx, %cr0 |
H. Peter Anvin | e5684ec | 2012-05-08 21:22:37 +0300 | [diff] [blame] | 105 | LJMPW_RM(3f) |
Jarkko Sakkinen | 5a8c9ae | 2012-05-08 21:22:27 +0300 | [diff] [blame] | 106 | 3: |
H. Peter Anvin | 6feb592 | 2012-05-08 21:22:39 +0300 | [diff] [blame] | 107 | andw %ax, %ax |
Jarkko Sakkinen | 5a8c9ae | 2012-05-08 21:22:27 +0300 | [diff] [blame] | 108 | jz bios |
H. Peter Anvin | 3d35ac3 | 2011-02-14 18:36:03 -0800 | [diff] [blame] | 109 | |
| 110 | apm: |
| 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 Sakkinen | 5a8c9ae | 2012-05-08 21:22:27 +0300 | [diff] [blame] | 118 | /* This should never return... */ |
H. Peter Anvin | 3d35ac3 | 2011-02-14 18:36:03 -0800 | [diff] [blame] | 119 | |
Jarkko Sakkinen | 5a8c9ae | 2012-05-08 21:22:27 +0300 | [diff] [blame] | 120 | bios: |
| 121 | ljmpw $0xf000, $0xfff0 |
H. Peter Anvin | 3d35ac3 | 2011-02-14 18:36:03 -0800 | [diff] [blame] | 122 | |
Jarkko Sakkinen | 5a8c9ae | 2012-05-08 21:22:27 +0300 | [diff] [blame] | 123 | .section ".rodata", "a" |
H. Peter Anvin | 3d35ac3 | 2011-02-14 18:36:03 -0800 | [diff] [blame] | 124 | |
Jarkko Sakkinen | 8e029fc | 2012-05-08 21:22:40 +0300 | [diff] [blame] | 125 | .balign 16 |
| 126 | GLOBAL(machine_real_restart_idt) |
H. Peter Anvin | 3d35ac3 | 2011-02-14 18:36:03 -0800 | [diff] [blame] | 127 | .word 0xffff /* Length - real mode default value */ |
| 128 | .long 0 /* Base - real mode default value */ |
Jarkko Sakkinen | 8e029fc | 2012-05-08 21:22:40 +0300 | [diff] [blame] | 129 | END(machine_real_restart_idt) |
H. Peter Anvin | 3d35ac3 | 2011-02-14 18:36:03 -0800 | [diff] [blame] | 130 | |
Jarkko Sakkinen | 8e029fc | 2012-05-08 21:22:40 +0300 | [diff] [blame] | 131 | .balign 16 |
| 132 | GLOBAL(machine_real_restart_gdt) |
Jarkko Sakkinen | 5a8c9ae | 2012-05-08 21:22:27 +0300 | [diff] [blame] | 133 | /* 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 Anvin | 3d35ac3 | 2011-02-14 18:36:03 -0800 | [diff] [blame] | 146 | /* |
| 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 Sakkinen | 8e029fc | 2012-05-08 21:22:40 +0300 | [diff] [blame] | 152 | END(machine_real_restart_gdt) |