2010-01-12 16:48:51 +00:00
|
|
|
/*
|
|
|
|
* GRUB -- GRand Unified Bootloader
|
|
|
|
* Copyright (C) 2009,2010 Free Software Foundation, Inc.
|
|
|
|
*
|
|
|
|
* GRUB is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* GRUB is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with GRUB. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define CODE32_SEGMENT 0x18
|
2010-08-24 06:57:18 +00:00
|
|
|
#define CODE_SEGMENT 0x08
|
2010-01-12 16:48:51 +00:00
|
|
|
|
|
|
|
/* The data segment of the protected mode. */
|
|
|
|
#define DATA_SEGMENT 0x10
|
|
|
|
|
2010-08-24 06:57:18 +00:00
|
|
|
#include "relocator_common.S"
|
|
|
|
|
2010-01-12 16:48:51 +00:00
|
|
|
.p2align 4 /* force 16-byte alignment */
|
|
|
|
|
|
|
|
VARIABLE(grub_relocator64_start)
|
2010-08-24 06:57:18 +00:00
|
|
|
PREAMBLE
|
2010-01-13 22:17:12 +00:00
|
|
|
#ifndef __x86_64__
|
2010-08-24 06:57:18 +00:00
|
|
|
DISABLE_PAGING
|
2010-01-12 16:48:51 +00:00
|
|
|
|
|
|
|
/* Turn on PAE. */
|
|
|
|
movl %cr4, %eax
|
|
|
|
orl $(GRUB_MEMORY_CPU_CR4_PAE_ON | GRUB_MEMORY_CPU_CR4_PSE_ON), %eax
|
|
|
|
movl %eax, %cr4
|
|
|
|
|
|
|
|
/* mov imm32, %eax */
|
|
|
|
.byte 0xb8
|
|
|
|
VARIABLE(grub_relocator64_cr3)
|
|
|
|
.long 0
|
|
|
|
movl %eax, %cr3
|
|
|
|
|
|
|
|
/* Turn on amd64. */
|
|
|
|
movl $GRUB_MEMORY_CPU_AMD64_MSR, %ecx
|
|
|
|
rdmsr
|
|
|
|
orl $GRUB_MEMORY_CPU_AMD64_MSR_ON, %eax
|
|
|
|
wrmsr
|
|
|
|
|
|
|
|
/* Enable paging. */
|
|
|
|
movl %cr0, %eax
|
|
|
|
orl $GRUB_MEMORY_CPU_CR0_PAGING_ON, %eax
|
|
|
|
movl %eax, %cr0
|
2010-01-13 22:17:12 +00:00
|
|
|
|
2010-08-24 06:57:18 +00:00
|
|
|
RELOAD_GDT
|
2010-01-12 16:48:51 +00:00
|
|
|
#else
|
|
|
|
/* mov imm64, %rax */
|
|
|
|
.byte 0x48
|
|
|
|
.byte 0xb8
|
|
|
|
VARIABLE(grub_relocator64_cr3)
|
|
|
|
.quad 0
|
2010-01-12 22:30:52 +00:00
|
|
|
movq %rax, %cr3
|
2010-01-12 16:48:51 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
.code64
|
|
|
|
|
|
|
|
/* mov imm64, %rax */
|
|
|
|
.byte 0x48
|
|
|
|
.byte 0xb8
|
|
|
|
VARIABLE(grub_relocator64_rsp)
|
|
|
|
.quad 0
|
|
|
|
|
|
|
|
movq %rax, %rsp
|
|
|
|
|
2015-07-17 17:43:42 +00:00
|
|
|
/*
|
|
|
|
* Here is grub_relocator64_efi_start() entry point.
|
|
|
|
* Following code is shared between grub_relocator64_efi_start()
|
|
|
|
* and grub_relocator64_start().
|
|
|
|
*
|
|
|
|
* Think twice before changing anything below!!!
|
|
|
|
*/
|
|
|
|
VARIABLE(grub_relocator64_efi_start)
|
2010-01-12 16:48:51 +00:00
|
|
|
/* mov imm64, %rax */
|
|
|
|
.byte 0x48
|
|
|
|
.byte 0xb8
|
|
|
|
VARIABLE(grub_relocator64_rsi)
|
|
|
|
.quad 0
|
|
|
|
|
|
|
|
movq %rax, %rsi
|
|
|
|
|
|
|
|
/* mov imm64, %rax */
|
|
|
|
.byte 0x48
|
|
|
|
.byte 0xb8
|
|
|
|
VARIABLE(grub_relocator64_rax)
|
|
|
|
.quad 0
|
|
|
|
|
|
|
|
/* mov imm64, %rbx */
|
|
|
|
.byte 0x48
|
|
|
|
.byte 0xbb
|
|
|
|
VARIABLE(grub_relocator64_rbx)
|
|
|
|
.quad 0
|
|
|
|
|
|
|
|
/* mov imm64, %rcx */
|
|
|
|
.byte 0x48
|
|
|
|
.byte 0xb9
|
|
|
|
VARIABLE(grub_relocator64_rcx)
|
|
|
|
.quad 0
|
|
|
|
|
|
|
|
/* mov imm64, %rdx */
|
|
|
|
.byte 0x48
|
|
|
|
.byte 0xba
|
|
|
|
VARIABLE(grub_relocator64_rdx)
|
|
|
|
.quad 0
|
|
|
|
|
|
|
|
/* Cleared direction flag is of no problem with any current
|
|
|
|
payload and makes this implementation easier. */
|
|
|
|
cld
|
|
|
|
|
2012-06-26 12:56:28 +00:00
|
|
|
#ifdef __APPLE__
|
|
|
|
.byte 0xff, 0x25
|
|
|
|
.quad 0
|
|
|
|
#else
|
2010-01-12 16:48:51 +00:00
|
|
|
jmp *LOCAL(jump_addr) (%rip)
|
2012-06-26 12:56:28 +00:00
|
|
|
#endif
|
2010-01-12 16:48:51 +00:00
|
|
|
|
|
|
|
LOCAL(jump_addr):
|
|
|
|
VARIABLE(grub_relocator64_rip)
|
|
|
|
.quad 0
|
|
|
|
|
2015-07-17 17:43:42 +00:00
|
|
|
/* Here grub_relocator64_efi_start() ends. Ufff... */
|
|
|
|
VARIABLE(grub_relocator64_efi_end)
|
|
|
|
|
2010-01-13 22:17:12 +00:00
|
|
|
#ifndef __x86_64__
|
2010-01-12 16:48:51 +00:00
|
|
|
.p2align 4
|
|
|
|
LOCAL(gdt):
|
|
|
|
/* NULL. */
|
|
|
|
.byte 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
|
|
|
|
|
|
|
|
/* 64-bit segment. */
|
|
|
|
.word 0xffff /* Limit xffff. */
|
|
|
|
.word 0x0000 /* Base xxxx0000. */
|
|
|
|
.byte 0x00 /* Base xx00xxxx. */
|
|
|
|
.byte (0x8 /* Type 8. */ | (1 << 4) /* Code. */ \
|
|
|
|
| (0 << 5) /* Ring 0. */ | (1 << 7) /* Present. */)
|
|
|
|
.byte (0xf /* Limit fxxxx. */ | (0 << 4) /* AVL flag. */ \
|
|
|
|
| (1 << 5) /* 64-bit. */ | (0 << 6) \
|
|
|
|
| (1 << 7) /* 4K granular. */)
|
|
|
|
.byte 0x00 /* Base 00xxxxxx. */
|
|
|
|
|
|
|
|
/* Data segment*/
|
|
|
|
.word 0xffff /* Limit xffff. */
|
|
|
|
.word 0x0000 /* Base xxxx0000. */
|
|
|
|
.byte 0x00 /* Base xx00xxxx. */
|
|
|
|
.byte (0x0 /* Type 0. */ | (0 << 4) /* Data. */ \
|
|
|
|
| (0 << 5) /* Ring 0. */ | (1 << 7) /* Present. */)
|
|
|
|
.byte (0xf /* Limit fxxxx. */ | (0 << 4) /* AVL flag. */ \
|
|
|
|
| (0 << 5) /* Data. */ | (0 << 6) \
|
|
|
|
| (1 << 7) /* 4K granular. */)
|
|
|
|
.byte 0x00 /* Base 00xxxxxx. */
|
|
|
|
|
|
|
|
/* Compatibility segment. */
|
|
|
|
.word 0xffff /* Limit xffff. */
|
|
|
|
.word 0x0000 /* Base xxxx0000. */
|
|
|
|
.byte 0x00 /* Base xx00xxxx. */
|
|
|
|
.byte (0x8 /* Type 8. */ | (1 << 4) /* Code. */ \
|
|
|
|
| (0 << 5) /* Ring 0. */ | (1 << 7) /* Present. */)
|
|
|
|
.byte (0xf /* Limit fxxxx. */ | (0 << 4) /* AVL flag. */ \
|
|
|
|
| (0 << 5) /* 32-bit. */ | (1 << 6) /* 32-bit. */ \
|
|
|
|
| (1 << 7) /* 4K granular. */)
|
|
|
|
.byte 0x00 /* Base 00xxxxxx. */
|
|
|
|
|
2010-08-24 06:57:18 +00:00
|
|
|
LOCAL(gdt_end):
|
2010-01-13 22:17:12 +00:00
|
|
|
#endif
|
2010-01-12 16:48:51 +00:00
|
|
|
|
|
|
|
VARIABLE(grub_relocator64_end)
|