| 1 |
/* |
| 2 |
* kexec: Linux boots Linux |
| 3 |
* |
| 4 |
* Copyright (C) 2003,2004,2005 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 |
.equ MSR_K6_EFER, 0xC0000080 |
| 21 |
.equ EFER_LME, 0x00000100 |
| 22 |
.equ X86_CR4_PAE, 0x00000020 |
| 23 |
.equ CR0_PG, 0x80000000 |
| 24 |
|
| 25 |
.globl compat_x86_64, compat_x86_64_entry32 |
| 26 |
.text |
| 27 |
.code64 |
| 28 |
.balign 16 |
| 29 |
compat_x86_64: |
| 30 |
/* Setup a temporary gdt */ |
| 31 |
/* This also acts as a serializing instruction ensuring |
| 32 |
* my self modifying code works. |
| 33 |
*/ |
| 34 |
lgdt gdt(%rip) |
| 35 |
|
| 36 |
/* Switch to 32bit compatiblity mode */ |
| 37 |
ljmp *lm_exit_addr(%rip) |
| 38 |
lm_exit: |
| 39 |
.code32 |
| 40 |
|
| 41 |
/* Disable paging */ |
| 42 |
movl %cr0, %eax |
| 43 |
andl $~CR0_PG, %eax |
| 44 |
movl %eax, %cr0 |
| 45 |
|
| 46 |
/* Disable long mode */ |
| 47 |
movl $MSR_K6_EFER, %ecx |
| 48 |
rdmsr |
| 49 |
andl $~EFER_LME, %eax |
| 50 |
wrmsr |
| 51 |
|
| 52 |
/* Disable PAE */ |
| 53 |
xorl %eax, %eax |
| 54 |
movl %eax, %cr4 |
| 55 |
|
| 56 |
/* load the data segments */ |
| 57 |
movl $0x18, %eax /* data segment */ |
| 58 |
movl %eax, %ds |
| 59 |
movl %eax, %es |
| 60 |
movl %eax, %ss |
| 61 |
movl %eax, %fs |
| 62 |
movl %eax, %gs |
| 63 |
|
| 64 |
/* set all of the registers to known values */ |
| 65 |
/* leave %esp alone */ |
| 66 |
|
| 67 |
xorl %eax, %eax |
| 68 |
xorl %ebx, %ebx |
| 69 |
xorl %ecx, %ecx |
| 70 |
xorl %edx, %edx |
| 71 |
xorl %esi, %esi |
| 72 |
xorl %edi, %edi |
| 73 |
xorl %ebp, %ebp |
| 74 |
|
| 75 |
jmp *compat_x86_64_entry32 |
| 76 |
|
| 77 |
.section ".rodata" |
| 78 |
.balign 16 |
| 79 |
gdt: /* 0x00 unusable segment |
| 80 |
* 0x08 unused |
| 81 |
* so use them as the gdt ptr |
| 82 |
*/ |
| 83 |
.word gdt_end - gdt - 1 |
| 84 |
# A quad word pointer to the gdt with the high 32bits 0 |
| 85 |
.long gdt, 0 |
| 86 |
.word 0, 0, 0 |
| 87 |
|
| 88 |
/* 0x10 4GB flat code segment */ |
| 89 |
.word 0xFFFF, 0x0000, 0x9A00, 0x00CF |
| 90 |
/* 0x18 4GB flat data segment */ |
| 91 |
.word 0xFFFF, 0x0000, 0x9200, 0x00CF |
| 92 |
gdt_end: |
| 93 |
|
| 94 |
lm_exit_addr: |
| 95 |
.long lm_exit |
| 96 |
.long 0x10 |
| 97 |
|
| 98 |
compat_x86_64_entry32: |
| 99 |
.long 0 |
| 100 |
.size compat_x86_64_entry32, . - compat_x86_64_entry32 |