2009-08-03 12:48:25 +02:00
|
|
|
/*
|
|
|
|
* GRUB -- GRand Unified Bootloader
|
|
|
|
* Copyright (C) 2009 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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <grub/symbol.h>
|
2009-08-05 19:30:43 +02:00
|
|
|
#include <grub/i386/memory.h>
|
2009-08-03 13:52:03 +02:00
|
|
|
|
2009-08-03 12:48:25 +02:00
|
|
|
#ifdef BACKWARD
|
|
|
|
#define RELOCATOR_VARIABLE(x) VARIABLE(grub_relocator32_backward_ ## x)
|
|
|
|
#else
|
|
|
|
#define RELOCATOR_VARIABLE(x) VARIABLE(grub_relocator32_forward_ ## x)
|
2009-08-03 13:52:03 +02:00
|
|
|
#endif
|
2009-08-05 19:30:43 +02:00
|
|
|
#ifdef __x86_64__
|
|
|
|
#define RAX %rax
|
|
|
|
#define RCX %rcx
|
|
|
|
#define RDI %rdi
|
|
|
|
#define RSI %rdi
|
|
|
|
#else
|
|
|
|
#define RAX %eax
|
|
|
|
#define RCX %ecx
|
|
|
|
#define RDI %edi
|
|
|
|
#define RSI %esi
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Apple's linker has a problem with 64-bit relocations. */
|
|
|
|
#if defined (__apple__) || ! defined (__x86_64__)
|
|
|
|
#define RSIA %esi
|
|
|
|
#define RAXA %eax
|
|
|
|
#else
|
|
|
|
#define RSIA %rsi
|
|
|
|
#define RAXA %rax
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2009-08-03 12:48:25 +02:00
|
|
|
.p2align 4 /* force 16-byte alignment */
|
|
|
|
|
|
|
|
RELOCATOR_VARIABLE(start)
|
|
|
|
#ifdef BACKWARD
|
|
|
|
base:
|
|
|
|
#endif
|
|
|
|
cli
|
|
|
|
|
|
|
|
#ifndef __x86_64__
|
|
|
|
/* mov imm32, %eax */
|
|
|
|
.byte 0xb8
|
|
|
|
RELOCATOR_VARIABLE(dest)
|
2009-08-05 19:30:43 +02:00
|
|
|
.long 0
|
|
|
|
movl %eax, %edi
|
2009-08-03 12:48:25 +02:00
|
|
|
|
|
|
|
/* mov imm32, %eax */
|
|
|
|
.byte 0xb8
|
|
|
|
RELOCATOR_VARIABLE(src)
|
2009-08-05 19:30:43 +02:00
|
|
|
.long 0
|
|
|
|
movl %eax, %esi
|
2009-08-03 12:48:25 +02:00
|
|
|
|
|
|
|
/* mov imm32, %ecx */
|
|
|
|
.byte 0xb9
|
|
|
|
RELOCATOR_VARIABLE(size)
|
2009-08-05 19:30:43 +02:00
|
|
|
.long 0
|
2009-08-03 12:48:25 +02:00
|
|
|
#else
|
2009-08-05 19:30:43 +02:00
|
|
|
xorq %rax, %rax
|
2009-08-03 12:48:25 +02:00
|
|
|
|
|
|
|
/* mov imm32, %eax */
|
|
|
|
.byte 0xb8
|
|
|
|
RELOCATOR_VARIABLE(dest)
|
2009-08-05 19:30:43 +02:00
|
|
|
.long 0
|
|
|
|
movq %rax, %rdi
|
2009-08-03 12:48:25 +02:00
|
|
|
|
|
|
|
/* mov imm64, %rax */
|
2009-08-03 13:45:37 +02:00
|
|
|
.byte 0x48
|
2009-08-03 12:48:25 +02:00
|
|
|
.byte 0xb8
|
|
|
|
RELOCATOR_VARIABLE(src)
|
2009-08-05 19:30:43 +02:00
|
|
|
.long 0, 0
|
|
|
|
movq %rax, %rsi
|
2009-08-03 12:48:25 +02:00
|
|
|
|
2009-08-05 19:30:43 +02:00
|
|
|
xorq %rcx, %rcx
|
2009-08-03 12:48:25 +02:00
|
|
|
/* mov imm32, %ecx */
|
|
|
|
.byte 0xb9
|
|
|
|
RELOCATOR_VARIABLE(size)
|
2009-08-05 19:30:43 +02:00
|
|
|
.long 0
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
mov RDI, RAX
|
|
|
|
|
|
|
|
#ifdef BACKWARD
|
|
|
|
add RCX, RSI
|
|
|
|
add RDX, RDI
|
|
|
|
#endif
|
2009-08-03 12:48:25 +02:00
|
|
|
|
|
|
|
#ifndef BACKWARD
|
2009-08-05 19:30:43 +02:00
|
|
|
add RCX, RAX
|
2009-08-03 12:48:25 +02:00
|
|
|
#endif
|
2009-08-05 19:30:43 +02:00
|
|
|
addq $0x3, RCX
|
|
|
|
shrq $2, RCX
|
2009-08-03 12:48:25 +02:00
|
|
|
|
2009-08-05 19:30:43 +02:00
|
|
|
|
2009-08-03 12:48:25 +02:00
|
|
|
#ifdef BACKWARD
|
|
|
|
/* Backward movsl is implicitly off-by-four. compensate that. */
|
2009-08-05 19:30:43 +02:00
|
|
|
subq $4, RSI
|
|
|
|
subq $4, RDI
|
2009-08-03 12:48:25 +02:00
|
|
|
|
|
|
|
/* Backward copy. */
|
|
|
|
std
|
|
|
|
|
|
|
|
rep
|
|
|
|
movsl
|
|
|
|
|
|
|
|
#else
|
|
|
|
/* Forward copy. */
|
|
|
|
cld
|
|
|
|
rep
|
|
|
|
movsl
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* %rax contains now our new 'base'. */
|
2009-08-05 19:30:43 +02:00
|
|
|
mov RAX, RSI
|
|
|
|
add $(cont0 - base), RAXA
|
|
|
|
jmp *RAX
|
2009-08-03 12:48:25 +02:00
|
|
|
cont0:
|
2009-08-05 19:30:43 +02:00
|
|
|
lea (cont1 - base) (RSIA, 1), RAXA
|
|
|
|
movl %eax, (jump_vector - base) (RSIA, 1)
|
2009-08-03 12:48:25 +02:00
|
|
|
|
2009-08-05 19:30:43 +02:00
|
|
|
lea (gdt - base) (RSIA, 1), RAXA
|
|
|
|
mov RAXA, (gdt_addr - base) (RSIA, 1)
|
2009-08-03 12:48:25 +02:00
|
|
|
|
|
|
|
/* Switch to compatibility mode. */
|
|
|
|
|
2009-08-05 19:30:43 +02:00
|
|
|
lgdt (gdtdesc - base) (RSIA, 1)
|
2009-08-03 12:48:25 +02:00
|
|
|
|
|
|
|
/* Update %cs. Thanks to David Miller for pointing this mistake out. */
|
2009-08-05 19:30:43 +02:00
|
|
|
ljmp *(jump_vector - base) (RSIA, 1)
|
2009-08-03 12:48:25 +02:00
|
|
|
|
|
|
|
cont1:
|
|
|
|
.code32
|
|
|
|
|
|
|
|
/* Update other registers. */
|
2009-08-05 19:30:43 +02:00
|
|
|
movl $GRUB_MEMORY_MACHINE_PROT_MODE_DSEG, %eax
|
|
|
|
movl %eax, %ds
|
|
|
|
movl %eax, %es
|
|
|
|
movl %eax, %fs
|
|
|
|
movl %eax, %gs
|
|
|
|
movl %eax, %ss
|
2009-08-03 12:48:25 +02:00
|
|
|
|
|
|
|
/* Disable paging. */
|
2009-08-05 19:30:43 +02:00
|
|
|
movl %cr0, %eax
|
|
|
|
andl $0x7fffffff, %eax
|
|
|
|
movl %eax, %cr0
|
2009-08-03 12:48:25 +02:00
|
|
|
|
|
|
|
/* Disable amd64. */
|
2009-08-05 19:30:43 +02:00
|
|
|
movl $0xc0000080, %ecx
|
2009-08-03 12:48:25 +02:00
|
|
|
rdmsr
|
2009-08-05 19:30:43 +02:00
|
|
|
andl $0xfffffeff, %eax
|
2009-08-03 12:48:25 +02:00
|
|
|
wrmsr
|
|
|
|
|
|
|
|
/* Turn off PAE. */
|
2009-08-05 19:30:43 +02:00
|
|
|
movl %cr4, %eax
|
|
|
|
andl $0xffffffcf, %eax
|
|
|
|
movl %eax, %cr4
|
2009-08-03 12:48:25 +02:00
|
|
|
|
2009-08-05 19:30:43 +02:00
|
|
|
jmp cont2
|
2009-08-03 12:48:25 +02:00
|
|
|
cont2:
|
|
|
|
.code32
|
|
|
|
|
|
|
|
/* mov imm32, %eax */
|
|
|
|
.byte 0xb8
|
|
|
|
RELOCATOR_VARIABLE (esp)
|
2009-08-05 19:30:43 +02:00
|
|
|
.long 0
|
2009-08-03 13:52:03 +02:00
|
|
|
|
2009-08-05 19:30:43 +02:00
|
|
|
movl %eax, %esp
|
2009-08-03 13:52:03 +02:00
|
|
|
|
2009-08-03 12:48:25 +02:00
|
|
|
/* mov imm32, %eax */
|
|
|
|
.byte 0xb8
|
|
|
|
RELOCATOR_VARIABLE (eax)
|
2009-08-05 19:30:43 +02:00
|
|
|
.long 0
|
2009-08-03 12:48:25 +02:00
|
|
|
|
|
|
|
/* mov imm32, %ebx */
|
|
|
|
.byte 0xbb
|
|
|
|
RELOCATOR_VARIABLE (ebx)
|
2009-08-05 19:30:43 +02:00
|
|
|
.long 0
|
2009-08-03 12:48:25 +02:00
|
|
|
|
|
|
|
/* mov imm32, %ecx */
|
|
|
|
.byte 0xb9
|
|
|
|
RELOCATOR_VARIABLE (ecx)
|
2009-08-05 19:30:43 +02:00
|
|
|
.long 0
|
2009-08-03 12:48:25 +02:00
|
|
|
|
|
|
|
/* mov imm32, %edx */
|
|
|
|
.byte 0xba
|
|
|
|
RELOCATOR_VARIABLE (edx)
|
2009-08-05 19:30:43 +02:00
|
|
|
.long 0
|
2009-08-03 12:48:25 +02:00
|
|
|
|
|
|
|
/* Cleared direction flag is of no problem with any current
|
|
|
|
payload and makes this implementation easier. */
|
|
|
|
cld
|
|
|
|
|
2009-08-05 19:30:43 +02:00
|
|
|
.byte 0xea
|
2009-08-03 12:48:25 +02:00
|
|
|
RELOCATOR_VARIABLE (eip)
|
2009-08-05 19:30:43 +02:00
|
|
|
.long 0
|
|
|
|
.word 0x08
|
2009-08-03 12:48:25 +02:00
|
|
|
|
2009-08-05 19:30:43 +02:00
|
|
|
/* GDT. The same as is used in 32-bit GRUB. */
|
|
|
|
.p2align 4
|
2009-08-03 12:48:25 +02:00
|
|
|
gdt:
|
2009-08-05 19:30:43 +02:00
|
|
|
.word 0, 0
|
|
|
|
.byte 0, 0, 0, 0
|
|
|
|
|
|
|
|
/* -- code segment --
|
|
|
|
* base = 0x00000000, limit = 0xFFFFF (4 KiB Granularity), present
|
|
|
|
* type = 32bit code execute/read, DPL = 0
|
|
|
|
*/
|
|
|
|
.word 0xFFFF, 0
|
|
|
|
.byte 0, 0x9A, 0xCF, 0
|
|
|
|
|
|
|
|
/* -- data segment --
|
|
|
|
* base = 0x00000000, limit 0xFFFFF (4 KiB Granularity), present
|
|
|
|
* type = 32 bit data read/write, DPL = 0
|
|
|
|
*/
|
|
|
|
.word 0xFFFF, 0
|
|
|
|
.byte 0, 0x92, 0xCF, 0
|
|
|
|
|
|
|
|
/* -- 16 bit real mode CS --
|
|
|
|
* base = 0x00000000, limit 0x0FFFF (1 B Granularity), present
|
|
|
|
* type = 16 bit code execute/read only/conforming, DPL = 0
|
|
|
|
*/
|
|
|
|
.word 0xFFFF, 0
|
|
|
|
.byte 0, 0x9E, 0, 0
|
|
|
|
|
|
|
|
/* -- 16 bit real mode DS --
|
|
|
|
* base = 0x00000000, limit 0x0FFFF (1 B Granularity), present
|
|
|
|
* type = 16 bit data read/write, DPL = 0
|
|
|
|
*/
|
|
|
|
.word 0xFFFF, 0
|
|
|
|
.byte 0, 0x92, 0, 0
|
|
|
|
|
|
|
|
.p2align 4
|
2009-08-03 12:48:25 +02:00
|
|
|
gdtdesc:
|
2009-08-05 19:30:43 +02:00
|
|
|
.word 0x27
|
2009-08-03 12:48:25 +02:00
|
|
|
gdt_addr:
|
2009-08-05 19:30:43 +02:00
|
|
|
#ifdef __x86_64__
|
2009-08-03 12:48:25 +02:00
|
|
|
/* Filled by the code. */
|
2009-08-05 19:30:43 +02:00
|
|
|
.quad 0
|
|
|
|
#else
|
|
|
|
/* Filled by the code. */
|
|
|
|
.long 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
.p2align 4
|
2009-08-03 12:48:25 +02:00
|
|
|
jump_vector:
|
|
|
|
/* Jump location. Is filled by the code */
|
2009-08-05 19:30:43 +02:00
|
|
|
.long 0
|
|
|
|
.long GRUB_MEMORY_MACHINE_PROT_MODE_CSEG
|
2009-08-03 12:48:25 +02:00
|
|
|
|
|
|
|
#ifndef BACKWARD
|
|
|
|
base:
|
|
|
|
#endif
|
|
|
|
|
|
|
|
RELOCATOR_VARIABLE(end)
|