Bill Richardson | 3a5a8d6 | 2010-05-21 11:35:01 -0700 | [diff] [blame] | 1 | /* |
| 2 | * GRUB -- GRand Unified Bootloader |
| 3 | * Copyright (C) 2009 Free Software Foundation, Inc. |
| 4 | * |
| 5 | * GRUB is free software: you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation, either version 3 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * GRUB is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with GRUB. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | |
| 19 | |
| 20 | .p2align 4 /* force 16-byte alignment */ |
| 21 | .globl trampoline |
| 22 | trampoline: |
| 23 | cli |
| 24 | /* %rdi contains protected memory start and %rsi |
| 25 | contains real memory start. */ |
| 26 | |
| 27 | mov %rsi, %rbx |
| 28 | |
| 29 | call base |
| 30 | base: |
| 31 | pop %rsi |
| 32 | |
| 33 | lea (cont1 - base) (%rsi, 1), %rax |
| 34 | mov %eax, (jump_vector - base) (%rsi, 1) |
| 35 | |
| 36 | lea (gdt - base) (%rsi, 1), %rax |
| 37 | mov %rax, (gdtaddr - base) (%rsi, 1) |
| 38 | |
| 39 | /* Switch to compatibility mode. */ |
| 40 | |
| 41 | lidt (idtdesc - base) (%rsi, 1) |
| 42 | lgdt (gdtdesc - base) (%rsi, 1) |
| 43 | |
| 44 | /* Update %cs. Thanks to David Miller for pointing this mistake out. */ |
| 45 | ljmp *(jump_vector - base) (%rsi, 1) |
| 46 | |
| 47 | cont1: |
| 48 | .code32 |
| 49 | |
| 50 | /* Update other registers. */ |
| 51 | mov $0x18, %eax |
| 52 | mov %eax, %ds |
| 53 | mov %eax, %es |
| 54 | mov %eax, %fs |
| 55 | mov %eax, %gs |
| 56 | mov %eax, %ss |
| 57 | |
| 58 | /* Disable paging. */ |
| 59 | mov %cr0, %eax |
| 60 | and $0x7fffffff, %eax |
| 61 | mov %eax, %cr0 |
| 62 | |
| 63 | /* Disable amd64. */ |
| 64 | mov $0xc0000080, %ecx |
| 65 | rdmsr |
| 66 | and $0xfffffeff, %eax |
| 67 | wrmsr |
| 68 | |
| 69 | /* Turn off PAE. */ |
| 70 | movl %cr4, %eax |
| 71 | and $0xffffffcf, %eax |
| 72 | mov %eax, %cr4 |
| 73 | |
| 74 | jmp cont2 |
| 75 | cont2: |
| 76 | .code32 |
| 77 | |
| 78 | mov %ebx, %esi |
| 79 | |
| 80 | jmp *%edi |
| 81 | |
| 82 | /* GDT. */ |
| 83 | .p2align 4 |
| 84 | gdt: |
| 85 | /* NULL. */ |
| 86 | .byte 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 |
| 87 | |
| 88 | /* Reserved. */ |
| 89 | .byte 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 |
| 90 | |
| 91 | /* Code segment. */ |
| 92 | .byte 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x9A, 0xCF, 0x00 |
| 93 | |
| 94 | /* Data segment. */ |
| 95 | .byte 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x92, 0xCF, 0x00 |
| 96 | |
| 97 | gdtdesc: |
| 98 | .word 31 |
| 99 | gdtaddr: |
| 100 | .quad gdt |
| 101 | |
| 102 | idtdesc: |
| 103 | .word 0 |
| 104 | idtaddr: |
| 105 | .quad 0 |
| 106 | |
| 107 | .p2align 4 |
| 108 | jump_vector: |
| 109 | /* Jump location. Is filled by the code */ |
| 110 | .long 0 |
| 111 | .long 0x10 |
| 112 | |