Improvements
This commit is contained in:
parent
7c8f178d02
commit
5c29f4d962
4 changed files with 130 additions and 152 deletions
1
THANKS
1
THANKS
|
@ -8,6 +8,7 @@ generally assist in the GRUB 2 maintainership process:
|
||||||
|
|
||||||
Andrey Shuvikov <mr_hyro@yahoo.com>
|
Andrey Shuvikov <mr_hyro@yahoo.com>
|
||||||
Bibo Mao <bibo.mao@intel.com>
|
Bibo Mao <bibo.mao@intel.com>
|
||||||
|
David Miller <davem@davemloft.net>
|
||||||
Guillem Jover <guillem@hadrons.org>
|
Guillem Jover <guillem@hadrons.org>
|
||||||
Harley D. Eades III <hde@foobar-qux.org>
|
Harley D. Eades III <hde@foobar-qux.org>
|
||||||
Hitoshi Ozeki <h-ozeki@ck2.so-net.ne.jp>
|
Hitoshi Ozeki <h-ozeki@ck2.so-net.ne.jp>
|
||||||
|
|
|
@ -28,6 +28,8 @@
|
||||||
#include <grub/memory.h>
|
#include <grub/memory.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#include <grub/i386/memory.h>
|
||||||
|
|
||||||
/* The scratch buffer used in real mode code. */
|
/* The scratch buffer used in real mode code. */
|
||||||
#define GRUB_MEMORY_MACHINE_SCRATCH_ADDR 0x68000
|
#define GRUB_MEMORY_MACHINE_SCRATCH_ADDR 0x68000
|
||||||
#define GRUB_MEMORY_MACHINE_SCRATCH_SEG (GRUB_MEMORY_MACHINE_SCRATCH_ADDR >> 4)
|
#define GRUB_MEMORY_MACHINE_SCRATCH_SEG (GRUB_MEMORY_MACHINE_SCRATCH_ADDR >> 4)
|
||||||
|
@ -63,21 +65,6 @@
|
||||||
/* The address where another boot loader is loaded. */
|
/* The address where another boot loader is loaded. */
|
||||||
#define GRUB_MEMORY_MACHINE_BOOT_LOADER_ADDR 0x7c00
|
#define GRUB_MEMORY_MACHINE_BOOT_LOADER_ADDR 0x7c00
|
||||||
|
|
||||||
/* The flag for protected mode. */
|
|
||||||
#define GRUB_MEMORY_MACHINE_CR0_PE_ON 0x1
|
|
||||||
|
|
||||||
/* The code segment of the protected mode. */
|
|
||||||
#define GRUB_MEMORY_MACHINE_PROT_MODE_CSEG 0x8
|
|
||||||
|
|
||||||
/* The data segment of the protected mode. */
|
|
||||||
#define GRUB_MEMORY_MACHINE_PROT_MODE_DSEG 0x10
|
|
||||||
|
|
||||||
/* The code segment of the pseudo real mode. */
|
|
||||||
#define GRUB_MEMORY_MACHINE_PSEUDO_REAL_CSEG 0x18
|
|
||||||
|
|
||||||
/* The data segment of the pseudo real mode. */
|
|
||||||
#define GRUB_MEMORY_MACHINE_PSEUDO_REAL_DSEG 0x20
|
|
||||||
|
|
||||||
#ifndef ASM_FILE
|
#ifndef ASM_FILE
|
||||||
|
|
||||||
struct grub_machine_mmap_entry
|
struct grub_machine_mmap_entry
|
||||||
|
|
|
@ -32,19 +32,11 @@ extern grub_uint8_t grub_relocator32_backward_end;
|
||||||
|
|
||||||
extern grub_uint32_t grub_relocator32_backward_dest;
|
extern grub_uint32_t grub_relocator32_backward_dest;
|
||||||
extern grub_uint32_t grub_relocator32_backward_size;
|
extern grub_uint32_t grub_relocator32_backward_size;
|
||||||
#ifdef __x86_64__
|
extern grub_addr_t grub_relocator32_backward_src;
|
||||||
extern grub_uint64_t grub_relocator32_backward_src;
|
|
||||||
#else
|
|
||||||
extern grub_uint32_t grub_relocator32_backward_src;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
extern grub_uint32_t grub_relocator32_forward_dest;
|
extern grub_uint32_t grub_relocator32_forward_dest;
|
||||||
extern grub_uint32_t grub_relocator32_forward_size;
|
extern grub_uint32_t grub_relocator32_forward_size;
|
||||||
#ifdef __x86_64__
|
extern grub_addr_t grub_relocator32_forward_src;
|
||||||
extern grub_uint64_t grub_relocator32_forward_src;
|
|
||||||
#else
|
|
||||||
extern grub_uint32_t grub_relocator32_forward_src;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
extern grub_uint32_t grub_relocator32_forward_eax;
|
extern grub_uint32_t grub_relocator32_forward_eax;
|
||||||
extern grub_uint32_t grub_relocator32_forward_ebx;
|
extern grub_uint32_t grub_relocator32_forward_ebx;
|
||||||
|
|
|
@ -17,12 +17,35 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <grub/symbol.h>
|
#include <grub/symbol.h>
|
||||||
|
#include <grub/i386/memory.h>
|
||||||
|
|
||||||
#ifdef BACKWARD
|
#ifdef BACKWARD
|
||||||
#define RELOCATOR_VARIABLE(x) VARIABLE(grub_relocator32_backward_ ## x)
|
#define RELOCATOR_VARIABLE(x) VARIABLE(grub_relocator32_backward_ ## x)
|
||||||
#else
|
#else
|
||||||
#define RELOCATOR_VARIABLE(x) VARIABLE(grub_relocator32_forward_ ## x)
|
#define RELOCATOR_VARIABLE(x) VARIABLE(grub_relocator32_forward_ ## x)
|
||||||
#endif
|
#endif
|
||||||
|
#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
|
||||||
|
|
||||||
|
|
||||||
.p2align 4 /* force 16-byte alignment */
|
.p2align 4 /* force 16-byte alignment */
|
||||||
|
|
||||||
RELOCATOR_VARIABLE(start)
|
RELOCATOR_VARIABLE(start)
|
||||||
|
@ -35,88 +58,64 @@ base:
|
||||||
/* mov imm32, %eax */
|
/* mov imm32, %eax */
|
||||||
.byte 0xb8
|
.byte 0xb8
|
||||||
RELOCATOR_VARIABLE(dest)
|
RELOCATOR_VARIABLE(dest)
|
||||||
.long 0
|
.long 0
|
||||||
mov %eax, %edi
|
movl %eax, %edi
|
||||||
|
|
||||||
/* mov imm32, %eax */
|
/* mov imm32, %eax */
|
||||||
.byte 0xb8
|
.byte 0xb8
|
||||||
RELOCATOR_VARIABLE(src)
|
RELOCATOR_VARIABLE(src)
|
||||||
.long 0
|
.long 0
|
||||||
mov %eax, %esi
|
movl %eax, %esi
|
||||||
|
|
||||||
/* mov imm32, %ecx */
|
/* mov imm32, %ecx */
|
||||||
.byte 0xb9
|
.byte 0xb9
|
||||||
RELOCATOR_VARIABLE(size)
|
RELOCATOR_VARIABLE(size)
|
||||||
.long 0
|
.long 0
|
||||||
mov %edi, %eax
|
|
||||||
#ifndef BACKWARD
|
|
||||||
add %ecx, %eax
|
|
||||||
#endif
|
|
||||||
add $0x3, %ecx
|
|
||||||
shr $2, %ecx
|
|
||||||
|
|
||||||
#ifdef BACKWARD
|
|
||||||
/* Backward movsl is implicitly off-by-four. compensate that. */
|
|
||||||
subl $4, %esi
|
|
||||||
subl $4, %edi
|
|
||||||
|
|
||||||
/* Backward copy. */
|
|
||||||
std
|
|
||||||
addl %ecx, %esi
|
|
||||||
addl %ecx, %edi
|
|
||||||
|
|
||||||
rep
|
|
||||||
movsl
|
|
||||||
|
|
||||||
#else
|
#else
|
||||||
/* Forward copy. */
|
xorq %rax, %rax
|
||||||
cld
|
|
||||||
rep
|
|
||||||
movsl
|
|
||||||
#endif
|
|
||||||
/* %eax contains now our new base. */
|
|
||||||
mov %eax, %esi
|
|
||||||
add $(cont0 - base), %eax
|
|
||||||
jmp *%eax
|
|
||||||
cont0:
|
|
||||||
#else
|
|
||||||
xorq %rax, %rax
|
|
||||||
|
|
||||||
/* mov imm32, %eax */
|
/* mov imm32, %eax */
|
||||||
.byte 0xb8
|
.byte 0xb8
|
||||||
RELOCATOR_VARIABLE(dest)
|
RELOCATOR_VARIABLE(dest)
|
||||||
.long 0
|
.long 0
|
||||||
mov %eax, %edi
|
movq %rax, %rdi
|
||||||
|
|
||||||
/* mov imm64, %rax */
|
/* mov imm64, %rax */
|
||||||
.byte 0x48
|
.byte 0x48
|
||||||
.byte 0xb8
|
.byte 0xb8
|
||||||
RELOCATOR_VARIABLE(src)
|
RELOCATOR_VARIABLE(src)
|
||||||
.long 0, 0
|
.long 0, 0
|
||||||
mov %rax, %rsi
|
movq %rax, %rsi
|
||||||
|
|
||||||
xorq %rcx, %rcx
|
xorq %rcx, %rcx
|
||||||
/* mov imm32, %ecx */
|
/* mov imm32, %ecx */
|
||||||
.byte 0xb9
|
.byte 0xb9
|
||||||
RELOCATOR_VARIABLE(size)
|
RELOCATOR_VARIABLE(size)
|
||||||
.long 0
|
.long 0
|
||||||
|
|
||||||
mov %rdi, %rax
|
|
||||||
#ifndef BACKWARD
|
|
||||||
add %rcx, %rax
|
|
||||||
#endif
|
#endif
|
||||||
add $0x3, %rcx
|
|
||||||
shr $2, %rcx
|
|
||||||
|
|
||||||
|
mov RDI, RAX
|
||||||
|
|
||||||
|
#ifdef BACKWARD
|
||||||
|
add RCX, RSI
|
||||||
|
add RDX, RDI
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef BACKWARD
|
||||||
|
add RCX, RAX
|
||||||
|
#endif
|
||||||
|
addq $0x3, RCX
|
||||||
|
shrq $2, RCX
|
||||||
|
|
||||||
|
|
||||||
#ifdef BACKWARD
|
#ifdef BACKWARD
|
||||||
/* Backward movsl is implicitly off-by-four. compensate that. */
|
/* Backward movsl is implicitly off-by-four. compensate that. */
|
||||||
subq $4, %rsi
|
subq $4, RSI
|
||||||
subq $4, %rdi
|
subq $4, RDI
|
||||||
|
|
||||||
/* Backward copy. */
|
/* Backward copy. */
|
||||||
std
|
std
|
||||||
addq %rcx, %rsi
|
|
||||||
addq %rcx, %rdi
|
|
||||||
|
|
||||||
rep
|
rep
|
||||||
movsl
|
movsl
|
||||||
|
@ -129,142 +128,141 @@ RELOCATOR_VARIABLE(size)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* %rax contains now our new 'base'. */
|
/* %rax contains now our new 'base'. */
|
||||||
mov %rax, %rsi
|
mov RAX, RSI
|
||||||
#ifdef APPLE_CC
|
add $(cont0 - base), RAXA
|
||||||
add $(cont0 - base), %eax
|
jmp *RAX
|
||||||
#else
|
|
||||||
add $(cont0 - base), %rax
|
|
||||||
#endif
|
|
||||||
jmp *%rax
|
|
||||||
cont0:
|
cont0:
|
||||||
#ifdef APPLE_CC
|
lea (cont1 - base) (RSIA, 1), RAXA
|
||||||
lea (cont1 - base) (%esi, 1), %eax
|
movl %eax, (jump_vector - base) (RSIA, 1)
|
||||||
mov %eax, (jump_vector - base) (%esi, 1)
|
|
||||||
|
|
||||||
lea (gdt - base) (%esi, 1), %eax
|
lea (gdt - base) (RSIA, 1), RAXA
|
||||||
mov %eax, (gdt_addr - base) (%esi, 1)
|
mov RAXA, (gdt_addr - base) (RSIA, 1)
|
||||||
|
|
||||||
/* Switch to compatibility mode. */
|
/* Switch to compatibility mode. */
|
||||||
|
|
||||||
lgdt (gdtdesc - base) (%esi, 1)
|
lgdt (gdtdesc - base) (RSIA, 1)
|
||||||
|
|
||||||
/* Update %cs. Thanks to David Miller for pointing this mistake out. */
|
/* Update %cs. Thanks to David Miller for pointing this mistake out. */
|
||||||
ljmp *(jump_vector - base) (%esi,1)
|
ljmp *(jump_vector - base) (RSIA, 1)
|
||||||
#else
|
|
||||||
lea (cont1 - base) (%rsi, 1), %rax
|
|
||||||
mov %eax, (jump_vector - base) (%rsi, 1)
|
|
||||||
|
|
||||||
lea (gdt - base) (%rsi, 1), %rax
|
|
||||||
mov %rax, (gdt_addr - base) (%rsi, 1)
|
|
||||||
|
|
||||||
/* Switch to compatibility mode. */
|
|
||||||
|
|
||||||
lgdt (gdtdesc - base) (%rsi, 1)
|
|
||||||
|
|
||||||
/* Update %cs. Thanks to David Miller for pointing this mistake out. */
|
|
||||||
ljmp *(jump_vector - base) (%rsi, 1)
|
|
||||||
#endif
|
|
||||||
|
|
||||||
cont1:
|
cont1:
|
||||||
.code32
|
.code32
|
||||||
|
|
||||||
/* Update other registers. */
|
/* Update other registers. */
|
||||||
mov $0x18, %eax
|
movl $GRUB_MEMORY_MACHINE_PROT_MODE_DSEG, %eax
|
||||||
mov %eax, %ds
|
movl %eax, %ds
|
||||||
mov %eax, %es
|
movl %eax, %es
|
||||||
mov %eax, %fs
|
movl %eax, %fs
|
||||||
mov %eax, %gs
|
movl %eax, %gs
|
||||||
mov %eax, %ss
|
movl %eax, %ss
|
||||||
|
|
||||||
/* Disable paging. */
|
/* Disable paging. */
|
||||||
mov %cr0, %eax
|
movl %cr0, %eax
|
||||||
and $0x7fffffff, %eax
|
andl $0x7fffffff, %eax
|
||||||
mov %eax, %cr0
|
movl %eax, %cr0
|
||||||
|
|
||||||
/* Disable amd64. */
|
/* Disable amd64. */
|
||||||
mov $0xc0000080, %ecx
|
movl $0xc0000080, %ecx
|
||||||
rdmsr
|
rdmsr
|
||||||
and $0xfffffeff, %eax
|
andl $0xfffffeff, %eax
|
||||||
wrmsr
|
wrmsr
|
||||||
|
|
||||||
/* Turn off PAE. */
|
/* Turn off PAE. */
|
||||||
movl %cr4, %eax
|
movl %cr4, %eax
|
||||||
and $0xffffffcf, %eax
|
andl $0xffffffcf, %eax
|
||||||
mov %eax, %cr4
|
movl %eax, %cr4
|
||||||
|
|
||||||
jmp cont2
|
jmp cont2
|
||||||
cont2:
|
cont2:
|
||||||
#endif
|
|
||||||
.code32
|
.code32
|
||||||
|
|
||||||
/* mov imm32, %eax */
|
/* mov imm32, %eax */
|
||||||
.byte 0xb8
|
.byte 0xb8
|
||||||
RELOCATOR_VARIABLE (esp)
|
RELOCATOR_VARIABLE (esp)
|
||||||
.long 0
|
.long 0
|
||||||
|
|
||||||
movl %eax, %esp
|
movl %eax, %esp
|
||||||
|
|
||||||
/* mov imm32, %eax */
|
/* mov imm32, %eax */
|
||||||
.byte 0xb8
|
.byte 0xb8
|
||||||
RELOCATOR_VARIABLE (eax)
|
RELOCATOR_VARIABLE (eax)
|
||||||
.long 0
|
.long 0
|
||||||
|
|
||||||
/* mov imm32, %ebx */
|
/* mov imm32, %ebx */
|
||||||
.byte 0xbb
|
.byte 0xbb
|
||||||
RELOCATOR_VARIABLE (ebx)
|
RELOCATOR_VARIABLE (ebx)
|
||||||
.long 0
|
.long 0
|
||||||
|
|
||||||
/* mov imm32, %ecx */
|
/* mov imm32, %ecx */
|
||||||
.byte 0xb9
|
.byte 0xb9
|
||||||
RELOCATOR_VARIABLE (ecx)
|
RELOCATOR_VARIABLE (ecx)
|
||||||
.long 0
|
.long 0
|
||||||
|
|
||||||
/* mov imm32, %edx */
|
/* mov imm32, %edx */
|
||||||
.byte 0xba
|
.byte 0xba
|
||||||
RELOCATOR_VARIABLE (edx)
|
RELOCATOR_VARIABLE (edx)
|
||||||
.long 0
|
.long 0
|
||||||
|
|
||||||
/* Cleared direction flag is of no problem with any current
|
/* Cleared direction flag is of no problem with any current
|
||||||
payload and makes this implementation easier. */
|
payload and makes this implementation easier. */
|
||||||
cld
|
cld
|
||||||
|
|
||||||
.byte 0xea
|
.byte 0xea
|
||||||
RELOCATOR_VARIABLE (eip)
|
RELOCATOR_VARIABLE (eip)
|
||||||
.long 0
|
.long 0
|
||||||
#ifdef __x86_64__
|
.word 0x08
|
||||||
.word 0x10
|
|
||||||
#else
|
|
||||||
.word 0x08
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifdef __x86_64__
|
/* GDT. The same as is used in 32-bit GRUB. */
|
||||||
/* GDT. Copied from loader/i386/linux.c. */
|
.p2align 4
|
||||||
.p2align 4
|
|
||||||
gdt:
|
gdt:
|
||||||
/* NULL. */
|
.word 0, 0
|
||||||
.byte 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
|
.byte 0, 0, 0, 0
|
||||||
|
|
||||||
/* Reserved. */
|
/* -- code segment --
|
||||||
.byte 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
|
* base = 0x00000000, limit = 0xFFFFF (4 KiB Granularity), present
|
||||||
|
* type = 32bit code execute/read, DPL = 0
|
||||||
|
*/
|
||||||
|
.word 0xFFFF, 0
|
||||||
|
.byte 0, 0x9A, 0xCF, 0
|
||||||
|
|
||||||
/* Code segment. */
|
/* -- data segment --
|
||||||
.byte 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x9A, 0xCF, 0x00
|
* base = 0x00000000, limit 0xFFFFF (4 KiB Granularity), present
|
||||||
|
* type = 32 bit data read/write, DPL = 0
|
||||||
|
*/
|
||||||
|
.word 0xFFFF, 0
|
||||||
|
.byte 0, 0x92, 0xCF, 0
|
||||||
|
|
||||||
/* Data segment. */
|
/* -- 16 bit real mode CS --
|
||||||
.byte 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x92, 0xCF, 0x00
|
* 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
|
||||||
gdtdesc:
|
gdtdesc:
|
||||||
.word 31
|
.word 0x27
|
||||||
gdt_addr:
|
gdt_addr:
|
||||||
|
#ifdef __x86_64__
|
||||||
/* Filled by the code. */
|
/* Filled by the code. */
|
||||||
.quad 0
|
.quad 0
|
||||||
|
#else
|
||||||
.p2align 4
|
/* Filled by the code. */
|
||||||
|
.long 0
|
||||||
|
#endif
|
||||||
|
|
||||||
|
.p2align 4
|
||||||
jump_vector:
|
jump_vector:
|
||||||
/* Jump location. Is filled by the code */
|
/* Jump location. Is filled by the code */
|
||||||
.long 0
|
.long 0
|
||||||
.long 0x10
|
.long GRUB_MEMORY_MACHINE_PROT_MODE_CSEG
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifndef BACKWARD
|
#ifndef BACKWARD
|
||||||
base:
|
base:
|
||||||
|
|
Loading…
Reference in a new issue