| 1 |
/* |
| 2 |
* purgatory: setup code |
| 3 |
* |
| 4 |
* Copyright (C) 2003,2004 Eric Biederman ([email protected]) |
| 5 |
* |
| 6 |
* This program is free software; you can redistribute it and/or modify |
| 7 |
* it under the terms of the GNU General Public License as published by |
| 8 |
* the Free Software Foundation (version 2 of the License). |
| 9 |
* |
| 10 |
* This program 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 this program; if not, write to the Free Software |
| 17 |
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 18 |
*/ |
| 19 |
|
| 20 |
.data |
| 21 |
.equ MSR_K6_EFER, 0xC0000080 |
| 22 |
.equ EFER_LME, 0x00000100 |
| 23 |
.equ X86_CR4_PAE, 0x00000020 |
| 24 |
.equ CR0_PG, 0x80000000 |
| 25 |
|
| 26 |
.text |
| 27 |
.balign 16 |
| 28 |
.globl entry32, entry32_regs |
| 29 |
entry32: |
| 30 |
.code64 |
| 31 |
|
| 32 |
/* Setup the 4G offset of entry32 lm_exit code segment */ |
| 33 |
movq $0x00CF9A000000ffff, %rax |
| 34 |
|
| 35 |
leaq entry32(%rip), %rbx /* Low 24 bits */ |
| 36 |
andq $0xffffff, %rbx |
| 37 |
shlq $16, %rbx |
| 38 |
orq %rbx, %rax |
| 39 |
|
| 40 |
leaq entry32(%rip), %rbx /* High 8 bits */ |
| 41 |
movq $0xff000000, %rdx |
| 42 |
andq %rdx, %rbx |
| 43 |
shlq $32, %rbx |
| 44 |
orq %rbx, %rax |
| 45 |
|
| 46 |
movq %rax, (gdt + 0x20)(%rip) |
| 47 |
|
| 48 |
/* Setup a gdt that is generally usefully */ |
| 49 |
lgdt gdt(%rip) |
| 50 |
|
| 51 |
/* Setup the far pointer to the entry point */ |
| 52 |
movl eip(%rip), %eax |
| 53 |
movl %eax, entry32_addr(%rip) |
| 54 |
|
| 55 |
/* Switch to 32bit compatiblity mode */ |
| 56 |
ljmp *lm_exit_addr(%rip) |
| 57 |
lm_exit: |
| 58 |
.code32 |
| 59 |
|
| 60 |
/* Disable paging */ |
| 61 |
movl %cr0, %eax |
| 62 |
andl $~CR0_PG, %eax |
| 63 |
movl %eax, %cr0 |
| 64 |
|
| 65 |
/* Disable long mode */ |
| 66 |
movl $MSR_K6_EFER, %ecx |
| 67 |
rdmsr |
| 68 |
andl $~EFER_LME, %eax |
| 69 |
wrmsr |
| 70 |
|
| 71 |
/* Disable PAE */ |
| 72 |
xorl %eax, %eax |
| 73 |
movl %eax, %cr4 |
| 74 |
|
| 75 |
/* load the data segments */ |
| 76 |
movl $0x18, %eax /* data segment */ |
| 77 |
movl %eax, %ds |
| 78 |
movl %eax, %es |
| 79 |
movl %eax, %ss |
| 80 |
movl %eax, %fs |
| 81 |
movl %eax, %gs |
| 82 |
|
| 83 |
/* Load the registers */ |
| 84 |
movl %cs:eax - entry32, %eax |
| 85 |
movl %cs:ecx - entry32, %ecx |
| 86 |
movl %cs:edx - entry32, %edx |
| 87 |
movl %cs:esi - entry32, %esi |
| 88 |
movl %cs:edi - entry32, %edi |
| 89 |
movl %cs:esp - entry32, %esp |
| 90 |
movl %cs:ebp - entry32, %ebp |
| 91 |
movl %cs:ebx - entry32, %ebx |
| 92 |
|
| 93 |
/* Jump to the loaded image */ |
| 94 |
ljmp *%cs:entry32_addr - entry32 |
| 95 |
|
| 96 |
.section ".data" |
| 97 |
.balign 16 |
| 98 |
gdt: /* 0x00 unusable segment |
| 99 |
* 0x08 unused |
| 100 |
* so use them as the gdt ptr |
| 101 |
*/ |
| 102 |
.word gdt_end - gdt - 1 |
| 103 |
.quad gdt |
| 104 |
.word 0, 0, 0 |
| 105 |
|
| 106 |
/* Documented linux kernel segments */ |
| 107 |
/* 0x10 4GB flat code segment */ |
| 108 |
.word 0xFFFF, 0x0000, 0x9A00, 0x00CF |
| 109 |
/* 0x18 4GB flat data segment */ |
| 110 |
.word 0xFFFF, 0x0000, 0x9200, 0x00CF |
| 111 |
|
| 112 |
/* 0x20 4GB flat code segment base at entry32 */ |
| 113 |
.word 0xFFFF, 0x0000, 0x9A00, 0x0CF |
| 114 |
/* 0x28 dummy */ |
| 115 |
.word 0x0000, 0x0000, 0x0000, 0x000 |
| 116 |
/* 0x30 dummy */ |
| 117 |
.word 0x0000, 0x0000, 0x0000, 0x000 |
| 118 |
/* 0x38 dummy */ |
| 119 |
.word 0x0000, 0x0000, 0x0000, 0x000 |
| 120 |
/* 0x40 dummy */ |
| 121 |
.word 0x0000, 0x0000, 0x0000, 0x000 |
| 122 |
/* 0x48 dummy */ |
| 123 |
.word 0x0000, 0x0000, 0x0000, 0x000 |
| 124 |
/* 0x50 dummy */ |
| 125 |
.word 0x0000, 0x0000, 0x0000, 0x000 |
| 126 |
/* 0x58 dummy */ |
| 127 |
.word 0x0000, 0x0000, 0x0000, 0x000 |
| 128 |
|
| 129 |
/* Segments used by the 2.5.x kernel */ |
| 130 |
/* 0x60 4GB flat code segment */ |
| 131 |
.word 0xFFFF, 0x0000, 0x9A00, 0x00CF |
| 132 |
/* 0x68 4GB flat data segment */ |
| 133 |
.word 0xFFFF, 0x0000, 0x9200, 0x00CF |
| 134 |
gdt_end: |
| 135 |
|
| 136 |
.section ".rodata" |
| 137 |
.balign 4 |
| 138 |
lm_exit_addr: |
| 139 |
.long lm_exit - entry32 |
| 140 |
.long 0x20 |
| 141 |
|
| 142 |
.section ".data" |
| 143 |
.balign 4 |
| 144 |
entry32_addr: |
| 145 |
.long 0x00000000 |
| 146 |
.long 0x10 |
| 147 |
|
| 148 |
.section ".rodata" |
| 149 |
.balign 4 |
| 150 |
entry32_regs: |
| 151 |
eax: .long 0x00000000 |
| 152 |
ebx: .long 0x00000000 |
| 153 |
ecx: .long 0x00000000 |
| 154 |
edx: .long 0x00000000 |
| 155 |
esi: .long 0x00000000 |
| 156 |
edi: .long 0x00000000 |
| 157 |
esp: .long 0x00000000 |
| 158 |
ebp: .long 0x00000000 |
| 159 |
eip: .quad entry16 /* low 32 bits address |
| 160 |
* high 32bits zeros |
| 161 |
* uses 64bit reloc |
| 162 |
*/ |
| 163 |
.size entry32_regs, (. - 4) - entry32_regs |
| 164 |
|