grub/lib/i386/relocator_asm.S

251 lines
4.4 KiB
ArmAsm
Raw Normal View History

2009-08-03 10:48:25 +00: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 17:30:43 +00:00
#include <grub/i386/memory.h>
2009-08-03 11:52:03 +00:00
2009-08-03 10:48:25 +00: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 11:52:03 +00:00
#endif
2009-08-05 17:30:43 +00:00
#ifdef __x86_64__
#define RAX %rax
#define RCX %rcx
2009-08-05 17:58:05 +00:00
#define RDX %rdx
2009-08-05 17:30:43 +00:00
#define RDI %rdi
#define RSI %rdi
#else
#define RAX %eax
#define RCX %ecx
2009-08-05 17:58:05 +00:00
#define RDX %edx
2009-08-05 17:30:43 +00:00
#define RDI %edi
#define RSI %esi
#endif
2009-08-05 17:58:05 +00:00
/* The code segment of the protected mode. */
#define CODE_SEGMENT 0x10
2009-08-05 17:30:43 +00:00
2009-08-05 17:58:05 +00:00
/* The data segment of the protected mode. */
#define DATA_SEGMENT 0x18
2009-08-05 17:30:43 +00:00
2009-08-03 10:48:25 +00:00
.p2align 4 /* force 16-byte alignment */
RELOCATOR_VARIABLE(start)
#ifdef BACKWARD
2009-08-05 17:58:05 +00:00
L_base:
2009-08-03 10:48:25 +00:00
#endif
cli
#ifndef __x86_64__
/* mov imm32, %eax */
.byte 0xb8
RELOCATOR_VARIABLE(dest)
2009-08-05 17:30:43 +00:00
.long 0
movl %eax, %edi
2009-08-03 10:48:25 +00:00
/* mov imm32, %eax */
.byte 0xb8
RELOCATOR_VARIABLE(src)
2009-08-05 17:30:43 +00:00
.long 0
movl %eax, %esi
2009-08-03 10:48:25 +00:00
/* mov imm32, %ecx */
.byte 0xb9
RELOCATOR_VARIABLE(size)
2009-08-05 17:30:43 +00:00
.long 0
2009-08-03 10:48:25 +00:00
#else
2009-08-05 17:30:43 +00:00
xorq %rax, %rax
2009-08-03 10:48:25 +00:00
/* mov imm32, %eax */
.byte 0xb8
RELOCATOR_VARIABLE(dest)
2009-08-05 17:30:43 +00:00
.long 0
movq %rax, %rdi
2009-08-03 10:48:25 +00:00
/* mov imm64, %rax */
2009-08-03 11:45:37 +00:00
.byte 0x48
2009-08-03 10:48:25 +00:00
.byte 0xb8
RELOCATOR_VARIABLE(src)
2009-08-05 17:30:43 +00:00
.long 0, 0
movq %rax, %rsi
2009-08-03 10:48:25 +00:00
2009-08-05 17:30:43 +00:00
xorq %rcx, %rcx
2009-08-03 10:48:25 +00:00
/* mov imm32, %ecx */
.byte 0xb9
RELOCATOR_VARIABLE(size)
2009-08-05 17:30:43 +00:00
.long 0
#endif
mov RDI, RAX
#ifdef BACKWARD
add RCX, RSI
add RDX, RDI
#endif
2009-08-03 10:48:25 +00:00
#ifndef BACKWARD
2009-08-05 17:30:43 +00:00
add RCX, RAX
2009-08-03 10:48:25 +00:00
#endif
2009-08-05 17:58:05 +00:00
add $0x3, RCX
shr $2, RCX
2009-08-03 10:48:25 +00:00
2009-08-05 17:30:43 +00:00
2009-08-03 10:48:25 +00:00
#ifdef BACKWARD
/* Backward movsl is implicitly off-by-four. compensate that. */
2009-08-05 17:58:05 +00:00
sub $4, RSI
sub $4, RDI
2009-08-03 10:48:25 +00:00
/* Backward copy. */
std
rep
movsl
#else
/* Forward copy. */
cld
rep
movsl
#endif
/* %rax contains now our new 'base'. */
2009-08-05 17:30:43 +00:00
mov RAX, RSI
2009-08-05 17:58:05 +00:00
add $(L_cont0 - L_base), RAX
2009-08-05 17:30:43 +00:00
jmp *RAX
2009-08-05 17:58:05 +00:00
L_cont0:
lea (L_cont1 - L_base) (RSI, 1), RAX
movl %eax, (L_jump_vector - L_base) (RSI, 1)
2009-08-03 10:48:25 +00:00
2009-08-05 17:58:05 +00:00
lea (L_gdt - L_base) (RSI, 1), RAX
mov RAX, (L_gdt_addr - L_base) (RSI, 1)
2009-08-03 10:48:25 +00:00
/* Switch to compatibility mode. */
2009-08-05 17:58:05 +00:00
lgdt (L_gdtdesc - L_base) (RSI, 1)
2009-08-03 10:48:25 +00:00
/* Update %cs. Thanks to David Miller for pointing this mistake out. */
2009-08-05 17:58:05 +00:00
ljmp *(L_jump_vector - L_base) (RSI, 1)
2009-08-03 10:48:25 +00:00
2009-08-05 17:58:05 +00:00
L_cont1:
2009-08-03 10:48:25 +00:00
.code32
/* Update other registers. */
2009-08-05 17:58:05 +00:00
movl $DATA_SEGMENT, %eax
2009-08-05 17:30:43 +00:00
movl %eax, %ds
movl %eax, %es
movl %eax, %fs
movl %eax, %gs
movl %eax, %ss
2009-08-03 10:48:25 +00:00
/* Disable paging. */
2009-08-05 17:30:43 +00:00
movl %cr0, %eax
2009-08-05 17:58:05 +00:00
andl $(~GRUB_MEMORY_CPU_CR0_PAGING_ON), %eax
2009-08-05 17:30:43 +00:00
movl %eax, %cr0
2009-08-03 10:48:25 +00:00
/* Disable amd64. */
2009-08-05 17:58:05 +00:00
movl $GRUB_MEMORY_CPU_AMD64_MSR, %ecx
2009-08-03 10:48:25 +00:00
rdmsr
2009-08-05 17:58:05 +00:00
andl $(~GRUB_MEMORY_CPU_AMD64_MSR_ON), %eax
2009-08-03 10:48:25 +00:00
wrmsr
/* Turn off PAE. */
2009-08-05 17:30:43 +00:00
movl %cr4, %eax
2009-08-05 17:58:05 +00:00
andl $GRUB_MEMORY_CPU_CR4_PAE_ON, %eax
2009-08-05 17:30:43 +00:00
movl %eax, %cr4
2009-08-03 10:48:25 +00:00
2009-08-05 17:58:05 +00:00
jmp L_cont2
L_cont2:
2009-08-03 10:48:25 +00:00
.code32
/* mov imm32, %eax */
.byte 0xb8
RELOCATOR_VARIABLE (esp)
2009-08-05 17:30:43 +00:00
.long 0
2009-08-03 11:52:03 +00:00
2009-08-05 17:30:43 +00:00
movl %eax, %esp
2009-08-03 11:52:03 +00:00
2009-08-03 10:48:25 +00:00
/* mov imm32, %eax */
.byte 0xb8
RELOCATOR_VARIABLE (eax)
2009-08-05 17:30:43 +00:00
.long 0
2009-08-03 10:48:25 +00:00
/* mov imm32, %ebx */
.byte 0xbb
RELOCATOR_VARIABLE (ebx)
2009-08-05 17:30:43 +00:00
.long 0
2009-08-03 10:48:25 +00:00
/* mov imm32, %ecx */
.byte 0xb9
RELOCATOR_VARIABLE (ecx)
2009-08-05 17:30:43 +00:00
.long 0
2009-08-03 10:48:25 +00:00
/* mov imm32, %edx */
.byte 0xba
RELOCATOR_VARIABLE (edx)
2009-08-05 17:30:43 +00:00
.long 0
2009-08-03 10:48:25 +00:00
/* Cleared direction flag is of no problem with any current
payload and makes this implementation easier. */
cld
2009-08-05 17:30:43 +00:00
.byte 0xea
2009-08-03 10:48:25 +00:00
RELOCATOR_VARIABLE (eip)
2009-08-05 17:30:43 +00:00
.long 0
.word 0x08
2009-08-03 10:48:25 +00:00
2009-08-05 17:58:05 +00:00
/* GDT. Copied from loader/i386/linux.c. */
2009-08-05 17:30:43 +00:00
.p2align 4
2009-08-05 17:58:05 +00:00
L_gdt:
/* NULL. */
.byte 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
/* Reserved. */
.byte 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
/* Code segment. */
.byte 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x9A, 0xCF, 0x00
/* Data segment. */
.byte 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x92, 0xCF, 0x00
2009-08-05 17:30:43 +00:00
.p2align 4
2009-08-05 17:58:05 +00:00
L_gdtdesc:
2009-08-05 17:30:43 +00:00
.word 0x27
2009-08-05 17:58:05 +00:00
L_gdt_addr:
2009-08-05 17:30:43 +00:00
#ifdef __x86_64__
2009-08-03 10:48:25 +00:00
/* Filled by the code. */
2009-08-05 17:30:43 +00:00
.quad 0
#else
/* Filled by the code. */
.long 0
#endif
.p2align 4
2009-08-05 17:58:05 +00:00
L_jump_vector:
2009-08-03 10:48:25 +00:00
/* Jump location. Is filled by the code */
2009-08-05 17:30:43 +00:00
.long 0
2009-08-05 17:58:05 +00:00
.long CODE_SEGMENT
2009-08-03 10:48:25 +00:00
#ifndef BACKWARD
2009-08-05 17:58:05 +00:00
L_base:
2009-08-03 10:48:25 +00:00
#endif
RELOCATOR_VARIABLE(end)