First compileable newreloc

This commit is contained in:
Vladimir 'phcoder' Serbinenko 2010-01-10 00:30:33 +01:00
parent 5d2c52b8ca
commit 95c7fc3f55
13 changed files with 1007 additions and 405 deletions

View file

@ -64,7 +64,7 @@ kernel_img_HEADERS = boot.h cache.h device.h disk.h dl.h elf.h elfload.h \
partition.h msdos_partition.h reader.h symbol.h term.h time.h types.h \
machine/biosdisk.h machine/boot.h machine/console.h machine/init.h \
machine/memory.h machine/loader.h machine/vga.h machine/vbe.h \
machine/kernel.h machine/pxe.h i386/pit.h list.h handler.h command.h i18n.h
machine/kernel.h machine/pxe.h i386/pit.h list.h handler.h command.h i18n.h mm_private.h
kernel_img_CFLAGS = $(COMMON_CFLAGS) $(TARGET_IMG_CFLAGS)
kernel_img_ASFLAGS = $(COMMON_ASFLAGS)
kernel_img_LDFLAGS = $(COMMON_LDFLAGS) $(TARGET_IMG_LDFLAGS)$(GRUB_KERNEL_MACHINE_LINK_ADDR) $(COMMON_CFLAGS)
@ -116,7 +116,7 @@ bin_SCRIPTS += grub-mkrescue
grub_mkrescue_SOURCES = util/grub-mkrescue.in
pkglib_MODULES = biosdisk.mod chain.mod \
multiboot.mod reboot.mod halt.mod \
reboot.mod halt.mod \
vbe.mod vbetest.mod vbeinfo.mod play.mod serial.mod \
vga.mod memdisk.mod pci.mod lspci.mod \
aout.mod bsd.mod pxe.mod pxecmd.mod datetime.mod date.mod \
@ -179,7 +179,7 @@ linux_mod_SOURCES = loader/i386/linux.c
linux_mod_CFLAGS = $(COMMON_CFLAGS)
linux_mod_LDFLAGS = $(COMMON_LDFLAGS)
pkglib_MODULES += xnu.mod
#pkglib_MODULES += xnu.mod
xnu_mod_SOURCES = loader/xnu_resume.c loader/i386/xnu.c loader/i386/pc/xnu.c \
loader/macho32.c loader/macho64.c loader/macho.c loader/xnu.c
xnu_mod_CFLAGS = $(COMMON_CFLAGS)
@ -202,6 +202,7 @@ serial_mod_CFLAGS = $(COMMON_CFLAGS)
serial_mod_LDFLAGS = $(COMMON_LDFLAGS)
# For multiboot.mod.
#pkglib_MODULES += multiboot.mod
multiboot_mod_SOURCES = loader/i386/multiboot.c \
loader/i386/multiboot_helper.S \
loader/i386/pc/multiboot2.c \

View file

@ -16,7 +16,8 @@ vga_text_mod_CFLAGS = $(COMMON_CFLAGS)
vga_text_mod_LDFLAGS = $(COMMON_LDFLAGS)
pkglib_MODULES += relocator.mod
relocator_mod_SOURCES = lib/i386/relocator.c lib/i386/relocator_asm.S lib/i386/relocator_backward.S
relocator_mod_SOURCES = lib/relocator.c lib/i386/relocator32.S \
lib/i386/relocator_asm.S lib/i386/relocator.c
relocator_mod_CFLAGS = $(COMMON_CFLAGS)
relocator_mod_ASFLAGS = $(COMMON_ASFLAGS)
relocator_mod_LDFLAGS = $(COMMON_LDFLAGS)

View file

@ -21,6 +21,7 @@
#include <grub/types.h>
#include <grub/err.h>
#include <grub/relocator.h>
struct grub_relocator32_state
{
@ -32,10 +33,7 @@ struct grub_relocator32_state
grub_uint32_t eip;
};
void *grub_relocator32_alloc (grub_size_t size);
grub_err_t grub_relocator32_boot (void *relocator, grub_uint32_t dest,
grub_err_t grub_relocator32_boot (struct grub_relocator *rel,
struct grub_relocator32_state state);
void *grub_relocator32_realloc (void *relocator, grub_size_t size);
void grub_relocator32_free (void *relocator);
#endif /* ! GRUB_RELOCATOR_CPU_HEADER */

View file

@ -43,6 +43,8 @@
#define ALIGN_UP(addr, align) \
((addr + (typeof (addr)) align - 1) & ~((typeof (addr)) align - 1))
#define ALIGN_DOWN(addr, align) \
((addr) & ~((typeof (addr)) align - 1))
#define ARRAY_SIZE(array) (sizeof (array) / sizeof (array[0]))
#define COMPILE_TIME_ASSERT(cond) switch (0) { case 1: case !(cond): ; }

62
include/grub/mm_private.h Normal file
View file

@ -0,0 +1,62 @@
/*
* GRUB -- GRand Unified Bootloader
* Copyright (C) 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/>.
*/
#ifndef GRUB_MM_PRIVATE_H
#define GRUB_MM_PRIVATE_H 1
#include <grub/mm.h>
/* Magic words. */
#define GRUB_MM_FREE_MAGIC 0x2d3c2808
#define GRUB_MM_ALLOC_MAGIC 0x6db08fa4
typedef struct grub_mm_header
{
struct grub_mm_header *next;
grub_size_t size;
grub_size_t magic;
#if GRUB_CPU_SIZEOF_VOID_P == 4
char padding[4];
#elif GRUB_CPU_SIZEOF_VOID_P == 8
char padding[8];
#else
# error "unknown word size"
#endif
}
*grub_mm_header_t;
#if GRUB_CPU_SIZEOF_VOID_P == 4
# define GRUB_MM_ALIGN_LOG2 4
#elif GRUB_CPU_SIZEOF_VOID_P == 8
# define GRUB_MM_ALIGN_LOG2 5
#endif
#define GRUB_MM_ALIGN (1 << GRUB_MM_ALIGN_LOG2)
typedef struct grub_mm_region
{
struct grub_mm_header *first;
struct grub_mm_region *next;
grub_size_t pre_size;
grub_size_t size;
}
*grub_mm_region_t;
extern grub_mm_region_t EXPORT_VAR (grub_mm_base);
#endif

42
include/grub/relocator.h Normal file
View file

@ -0,0 +1,42 @@
/*
* 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/>.
*/
#ifndef GRUB_RELOCATOR_HEADER
#define GRUB_RELOCATOR_HEADER 1
#include <grub/types.h>
#include <grub/err.h>
struct grub_relocator;
struct grub_relocator *grub_relocator_new (void);
grub_err_t
grub_relocator_alloc_chunk_addr (struct grub_relocator *rel, void **src,
grub_addr_t target, grub_size_t size);
grub_err_t
grub_relocator_alloc_chunk_align (struct grub_relocator *rel, void **src,
grub_addr_t *target,
grub_addr_t min_addr, grub_addr_t max_addr,
grub_size_t size, grub_size_t align);
void
grub_relocator_unload (struct grub_relocator *rel);
#endif

View file

@ -0,0 +1,58 @@
/*
* 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/>.
*/
#ifndef GRUB_RELOCATOR_PRIVATE_HEADER
#define GRUB_RELOCATOR_PRIVATE_HEADER 1
#include <grub/types.h>
#include <grub/err.h>
extern grub_size_t grub_relocator_align;
extern grub_size_t grub_relocator_forward_size;
extern grub_size_t grub_relocator_backward_size;
extern grub_size_t grub_relocator_jumper_size;
struct grub_relocator
{
struct grub_relocator_chunk *chunks;
grub_addr_t postchunks;
grub_addr_t highestaddr;
grub_addr_t highestnonpostaddr;
grub_size_t relocators_size;
};
struct grub_relocator_chunk
{
struct grub_relocator_chunk *next;
grub_addr_t src;
grub_addr_t target;
grub_size_t size;
};
void
grub_cpu_relocator_init (void);
grub_err_t
grub_relocator_prepare_relocs (struct grub_relocator *rel, grub_addr_t addr,
grub_addr_t *relstart);
void grub_cpu_relocator_forward (void *rels, void *src, void *tgt,
grub_size_t size);
void grub_cpu_relocator_backward (void *rels, void *src, void *tgt,
grub_size_t size);
void grub_cpu_relocator_jumper (void *rels, grub_addr_t addr);
#endif

View file

@ -65,6 +65,7 @@
#include <grub/types.h>
#include <grub/disk.h>
#include <grub/dl.h>
#include <grub/mm_private.h>
#ifdef MM_DEBUG
# undef grub_malloc
@ -74,45 +75,9 @@
# undef grub_memalign
#endif
/* Magic words. */
#define GRUB_MM_FREE_MAGIC 0x2d3c2808
#define GRUB_MM_ALLOC_MAGIC 0x6db08fa4
typedef struct grub_mm_header
{
struct grub_mm_header *next;
grub_size_t size;
grub_size_t magic;
#if GRUB_CPU_SIZEOF_VOID_P == 4
char padding[4];
#elif GRUB_CPU_SIZEOF_VOID_P == 8
char padding[8];
#else
# error "unknown word size"
#endif
}
*grub_mm_header_t;
#if GRUB_CPU_SIZEOF_VOID_P == 4
# define GRUB_MM_ALIGN_LOG2 4
#elif GRUB_CPU_SIZEOF_VOID_P == 8
# define GRUB_MM_ALIGN_LOG2 5
#endif
#define GRUB_MM_ALIGN (1 << GRUB_MM_ALIGN_LOG2)
typedef struct grub_mm_region
{
struct grub_mm_header *first;
struct grub_mm_region *next;
grub_addr_t addr;
grub_size_t size;
}
*grub_mm_region_t;
static grub_mm_region_t base;
grub_mm_region_t grub_mm_base;
/* Get a header from the pointer PTR, and set *P and *R to a pointer
to the header and a pointer to its region, respectively. PTR must
@ -123,9 +88,9 @@ get_header_from_pointer (void *ptr, grub_mm_header_t *p, grub_mm_region_t *r)
if ((grub_addr_t) ptr & (GRUB_MM_ALIGN - 1))
grub_fatal ("unaligned pointer %p", ptr);
for (*r = base; *r; *r = (*r)->next)
if ((grub_addr_t) ptr > (*r)->addr
&& (grub_addr_t) ptr <= (*r)->addr + (*r)->size)
for (*r = grub_mm_base; *r; *r = (*r)->next)
if ((grub_addr_t) ptr > (grub_addr_t) ((*r) + 1)
&& (grub_addr_t) ptr <= (grub_addr_t) ((*r) + 1) + (*r)->size)
break;
if (! *r)
@ -153,22 +118,21 @@ grub_mm_init_region (void *addr, grub_size_t size)
return;
/* Allocate a region from the head. */
r = (grub_mm_region_t) (((grub_addr_t) addr + GRUB_MM_ALIGN - 1)
& (~(GRUB_MM_ALIGN - 1)));
r = (grub_mm_region_t) ALIGN_UP ((grub_addr_t) addr, GRUB_MM_ALIGN);
size -= (char *) r - (char *) addr + sizeof (*r);
h = (grub_mm_header_t) ((char *) r + GRUB_MM_ALIGN);
h = (grub_mm_header_t) (r + 1);
h->next = h;
h->magic = GRUB_MM_FREE_MAGIC;
h->size = (size >> GRUB_MM_ALIGN_LOG2);
r->first = h;
r->addr = (grub_addr_t) h;
r->pre_size = (grub_addr_t) r - (grub_addr_t) addr;
r->size = (h->size << GRUB_MM_ALIGN_LOG2);
/* Find where to insert this region. Put a smaller one before bigger ones,
to prevent fragmentation. */
for (p = &base, q = *p; q; p = &(q->next), q = *p)
for (p = &grub_mm_base, q = *p; q; p = &(q->next), q = *p)
if (q->size > r->size)
break;
@ -268,13 +232,14 @@ grub_real_malloc (grub_mm_header_t *first, grub_size_t n, grub_size_t align)
*/
grub_mm_header_t r;
extra += (p->size - extra - n) & (~(align - 1));
r = p + extra + n;
r->magic = GRUB_MM_FREE_MAGIC;
r->size = p->size - extra - n;
r->next = p->next;
r->next = p;
p->size = extra;
p->next = r;
q->next = r;
p += extra;
p->size = n;
p->magic = GRUB_MM_ALLOC_MAGIC;
@ -310,7 +275,7 @@ grub_memalign (grub_size_t align, grub_size_t size)
again:
for (r = base; r; r = r->next)
for (r = grub_mm_base; r; r = r->next)
{
void *p;
@ -471,7 +436,7 @@ grub_mm_dump_free (void)
{
grub_mm_region_t r;
for (r = base; r; r = r->next)
for (r = grub_mm_base; r; r = r->next)
{
grub_mm_header_t p;
@ -498,13 +463,13 @@ grub_mm_dump (unsigned lineno)
grub_mm_region_t r;
grub_printf ("called at line %u\n", lineno);
for (r = base; r; r = r->next)
for (r = grub_mm_base; r; r = r->next)
{
grub_mm_header_t p;
for (p = (grub_mm_header_t) ((r->addr + GRUB_MM_ALIGN - 1)
& (~(GRUB_MM_ALIGN - 1)));
(grub_addr_t) p < r->addr + r->size;
for (p = (grub_mm_header_t) ALIGN_UP ((grub_addr_t) (r + 1),
GRUB_MM_ALIGN);
(grub_addr_t) p < (grub_addr_t) (r+1) + r->size;
p++)
{
switch (p->magic)

View file

@ -24,79 +24,115 @@
#include <grub/err.h>
#include <grub/i386/relocator.h>
#include <grub/relocator_private.h>
extern grub_uint8_t grub_relocator32_forward_start;
extern grub_uint8_t grub_relocator32_forward_end;
extern grub_uint8_t grub_relocator32_backward_start;
extern grub_uint8_t grub_relocator32_backward_end;
extern grub_uint8_t grub_relocator32_start;
extern grub_uint8_t grub_relocator32_end;
extern grub_uint8_t grub_relocator_forward_start;
extern grub_uint8_t grub_relocator_forward_end;
extern grub_uint8_t grub_relocator_backward_start;
extern grub_uint8_t grub_relocator_backward_end;
extern grub_uint32_t grub_relocator32_backward_dest;
extern grub_uint32_t grub_relocator32_backward_size;
extern grub_addr_t grub_relocator32_backward_src;
extern void *grub_relocator_backward_dest;
extern void *grub_relocator_backward_src;
extern grub_size_t grub_relocator_backward_size;
extern grub_uint32_t grub_relocator32_forward_dest;
extern grub_uint32_t grub_relocator32_forward_size;
extern grub_addr_t grub_relocator32_forward_src;
extern void *grub_relocator_forward_dest;
extern void *grub_relocator_forward_src;
extern grub_size_t grub_relocator_forward_size;
extern grub_uint32_t grub_relocator32_forward_eax;
extern grub_uint32_t grub_relocator32_forward_ebx;
extern grub_uint32_t grub_relocator32_forward_ecx;
extern grub_uint32_t grub_relocator32_forward_edx;
extern grub_uint32_t grub_relocator32_forward_eip;
extern grub_uint32_t grub_relocator32_forward_esp;
extern grub_uint32_t grub_relocator32_eax;
extern grub_uint32_t grub_relocator32_ebx;
extern grub_uint32_t grub_relocator32_ecx;
extern grub_uint32_t grub_relocator32_edx;
extern grub_uint32_t grub_relocator32_eip;
extern grub_uint32_t grub_relocator32_esp;
extern grub_uint32_t grub_relocator32_backward_eax;
extern grub_uint32_t grub_relocator32_backward_ebx;
extern grub_uint32_t grub_relocator32_backward_ecx;
extern grub_uint32_t grub_relocator32_backward_edx;
extern grub_uint32_t grub_relocator32_backward_eip;
extern grub_uint32_t grub_relocator32_backward_esp;
#define RELOCATOR_SIZEOF(x) (&grub_relocator##x##_end - &grub_relocator##x##_start)
#define RELOCATOR_SIZEOF(x) (&grub_relocator32_##x##_end - &grub_relocator32_##x##_start)
#define RELOCATOR_ALIGN 16
#define PREFIX(x) grub_relocator32_ ## x
grub_size_t grub_relocator_align = 1;
grub_size_t grub_relocator_forward_size;
grub_size_t grub_relocator_backward_size;
grub_size_t grub_relocator_jumper_size = 10;
static void
write_call_relocator_bw (void *ptr, void *src, grub_uint32_t dest,
grub_size_t size, struct grub_relocator32_state state)
void
grub_cpu_relocator_init (void)
{
grub_relocator32_backward_dest = dest;
grub_relocator32_backward_src = PTR_TO_UINT64 (src);
grub_relocator32_backward_size = size;
grub_relocator32_backward_eax = state.eax;
grub_relocator32_backward_ebx = state.ebx;
grub_relocator32_backward_ecx = state.ecx;
grub_relocator32_backward_edx = state.edx;
grub_relocator32_backward_eip = state.eip;
grub_relocator32_backward_esp = state.esp;
grub_memmove (ptr,
&grub_relocator32_backward_start,
RELOCATOR_SIZEOF (backward));
((void (*) (void)) ptr) ();
grub_relocator_forward_size = RELOCATOR_SIZEOF(_forward);
grub_relocator_backward_size = RELOCATOR_SIZEOF(_backward);
}
static void
write_call_relocator_fw (void *ptr, void *src, grub_uint32_t dest,
grub_size_t size, struct grub_relocator32_state state)
void
grub_cpu_relocator_jumper (void *rels, grub_addr_t addr)
{
grub_relocator32_forward_dest = dest;
grub_relocator32_forward_src = PTR_TO_UINT64 (src);
grub_relocator32_forward_size = size;
grub_relocator32_forward_eax = state.eax;
grub_relocator32_forward_ebx = state.ebx;
grub_relocator32_forward_ecx = state.ecx;
grub_relocator32_forward_edx = state.edx;
grub_relocator32_forward_eip = state.eip;
grub_relocator32_forward_esp = state.esp;
grub_memmove (ptr,
&grub_relocator32_forward_start,
RELOCATOR_SIZEOF (forward));
((void (*) (void)) ptr) ();
grub_uint8_t *ptr;
ptr = rels;
/* jmp $addr */
*(grub_uint8_t *) ptr = 0xe9;
ptr++;
*(grub_uint32_t *) ptr = addr - (grub_uint32_t) (ptr + 4);
ptr += 4;
/* movl $addr, %eax (for relocator) */
*(grub_uint8_t *) ptr = 0xb8;
ptr++;
*(grub_uint32_t *) ptr = addr;
}
#include "../relocator.c"
void
grub_cpu_relocator_backward (void *ptr, void *src, void *dest,
grub_size_t size)
{
grub_relocator_backward_dest = dest;
grub_relocator_backward_src = src;
grub_relocator_backward_size = size;
grub_memmove (ptr,
&grub_relocator_backward_start,
RELOCATOR_SIZEOF (_backward));
}
void
grub_cpu_relocator_forward (void *ptr, void *src, void *dest,
grub_size_t size)
{
grub_relocator_forward_dest = dest;
grub_relocator_forward_src = src;
grub_relocator_forward_size = size;
grub_memmove (ptr,
&grub_relocator_forward_start,
RELOCATOR_SIZEOF (_forward));
}
grub_err_t
grub_relocator32_boot (struct grub_relocator *rel,
struct grub_relocator32_state state)
{
grub_addr_t target;
void *src;
grub_err_t err;
grub_addr_t relst;
err = grub_relocator_alloc_chunk_align (rel, &src, &target, 0,
(0xffffffff - RELOCATOR_SIZEOF (32))
+ 1, RELOCATOR_SIZEOF (32), 16);
if (err)
return err;
grub_relocator32_eax = state.eax;
grub_relocator32_ebx = state.ebx;
grub_relocator32_ecx = state.ecx;
grub_relocator32_edx = state.edx;
grub_relocator32_eip = state.eip;
grub_relocator32_esp = state.esp;
grub_memmove (src, &grub_relocator32_start, RELOCATOR_SIZEOF (32));
err = grub_relocator_prepare_relocs (rel, target, &relst);
if (err)
return err;
asm volatile ("cli");
((void (*) (void)) relst) ();
/* Not reached. */
return GRUB_ERR_NONE;
}

158
lib/i386/relocator32.S Normal file
View file

@ -0,0 +1,158 @@
/*
* 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/>.
*/
#include <grub/symbol.h>
#include <grub/i386/memory.h>
#ifdef __x86_64__
#define RAX %rax
#define RSI %rdi
#else
#define RAX %eax
#define RSI %esi
#endif
/* The code segment of the protected mode. */
#define CODE_SEGMENT 0x10
/* The data segment of the protected mode. */
#define DATA_SEGMENT 0x18
.p2align 4 /* force 16-byte alignment */
VARIABLE(grub_relocator32_start)
LOCAL(base):
/* %rax contains now our new 'base'. */
mov RAX, RSI
add $(LOCAL(cont0) - LOCAL(base)), RAX
jmp *RAX
LOCAL(cont0):
lea (LOCAL(cont1) - LOCAL(base)) (RSI, 1), RAX
movl %eax, (LOCAL(jump_vector) - LOCAL(base)) (RSI, 1)
lea (LOCAL(gdt) - LOCAL(base)) (RSI, 1), RAX
mov RAX, (LOCAL(gdt_addr) - LOCAL(base)) (RSI, 1)
/* Switch to compatibility mode. */
lgdt (LOCAL(gdtdesc) - LOCAL(base)) (RSI, 1)
/* Update %cs. */
ljmp *(LOCAL(jump_vector) - LOCAL(base)) (RSI, 1)
LOCAL(cont1):
.code32
/* Update other registers. */
movl $DATA_SEGMENT, %eax
movl %eax, %ds
movl %eax, %es
movl %eax, %fs
movl %eax, %gs
movl %eax, %ss
/* Disable paging. */
movl %cr0, %eax
andl $(~GRUB_MEMORY_CPU_CR0_PAGING_ON), %eax
movl %eax, %cr0
/* Disable amd64. */
movl $GRUB_MEMORY_CPU_AMD64_MSR, %ecx
rdmsr
andl $(~GRUB_MEMORY_CPU_AMD64_MSR_ON), %eax
wrmsr
/* Turn off PAE. */
movl %cr4, %eax
andl $GRUB_MEMORY_CPU_CR4_PAE_ON, %eax
movl %eax, %cr4
jmp LOCAL(cont2)
LOCAL(cont2):
.code32
/* mov imm32, %eax */
.byte 0xb8
VARIABLE(grub_relocator32_esp)
.long 0
movl %eax, %esp
/* mov imm32, %eax */
.byte 0xb8
VARIABLE(grub_relocator32_eax)
.long 0
/* mov imm32, %ebx */
.byte 0xbb
VARIABLE(grub_relocator32_ebx)
.long 0
/* mov imm32, %ecx */
.byte 0xb9
VARIABLE(grub_relocator32_ecx)
.long 0
/* mov imm32, %edx */
.byte 0xba
VARIABLE(grub_relocator32_edx)
.long 0
/* Cleared direction flag is of no problem with any current
payload and makes this implementation easier. */
cld
.byte 0xea
VARIABLE(grub_relocator32_eip)
.long 0
.word CODE_SEGMENT
/* GDT. Copied from loader/i386/linux.c. */
.p2align 4
LOCAL(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
.p2align 4
LOCAL(gdtdesc):
.word 0x27
LOCAL(gdt_addr):
#ifdef __x86_64__
/* Filled by the code. */
.quad 0
#else
/* Filled by the code. */
.long 0
#endif
.p2align 4
LOCAL(jump_vector):
/* Jump location. Is filled by the code */
.long 0
.long CODE_SEGMENT
VARIABLE(grub_relocator32_end)

View file

@ -19,230 +19,60 @@
#include <grub/symbol.h>
#include <grub/i386/memory.h>
#ifdef BACKWARD
#define RELOCATOR_VARIABLE(x) VARIABLE(grub_relocator32_backward_ ## x)
#else
#define RELOCATOR_VARIABLE(x) VARIABLE(grub_relocator32_forward_ ## x)
#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
/* The code segment of the protected mode. */
#define CODE_SEGMENT 0x10
/* The data segment of the protected mode. */
#define DATA_SEGMENT 0x18
.p2align 4 /* force 16-byte alignment */
RELOCATOR_VARIABLE(start)
#ifdef BACKWARD
LOCAL(base):
#endif
cli
#ifndef __x86_64__
VARIABLE(grub_relocator_backward_start)
/* mov imm32, %eax */
.byte 0xb8
RELOCATOR_VARIABLE(dest)
VARIABLE(grub_relocator_backward_dest)
.long 0
movl %eax, %edi
/* mov imm32, %eax */
.byte 0xb8
RELOCATOR_VARIABLE(src)
VARIABLE(grub_relocator_backward_src)
.long 0
movl %eax, %esi
/* mov imm32, %ecx */
.byte 0xb9
RELOCATOR_VARIABLE(size)
.long 0
#else
xorq %rax, %rax
/* mov imm32, %eax */
.byte 0xb8
RELOCATOR_VARIABLE(dest)
.long 0
movq %rax, %rdi
/* mov imm64, %rax */
.byte 0x48
.byte 0xb8
RELOCATOR_VARIABLE(src)
.long 0, 0
movq %rax, %rsi
xorq %rcx, %rcx
/* mov imm32, %ecx */
.byte 0xb9
RELOCATOR_VARIABLE(size)
VARIABLE(grub_relocator_backward_size)
.long 0
#endif
mov RDI, RAX
#ifdef BACKWARD
add RCX, RSI
add RCX, RDI
#endif
#ifndef BACKWARD
add RCX, RAX
#endif
add $0x3, RCX
shr $2, RCX
add %ecx, %esi
add %ecx, %edi
#ifdef BACKWARD
/* Backward movsl is implicitly off-by-four. compensate that. */
sub $4, RSI
sub $4, RDI
/* Backward movsb is implicitly off-by-one. compensate that. */
sub $1, %esi
sub $1, %edi
/* Backward copy. */
std
rep
movsl
movsb
VARIABLE(grub_relocator_backward_end)
#else
/* Forward copy. */
cld
rep
movsl
#endif
/* %rax contains now our new 'base'. */
mov RAX, RSI
add $(LOCAL(cont0) - LOCAL(base)), RAX
jmp *RAX
LOCAL(cont0):
lea (LOCAL(cont1) - LOCAL(base)) (RSI, 1), RAX
movl %eax, (LOCAL(jump_vector) - LOCAL(base)) (RSI, 1)
lea (LOCAL(gdt) - LOCAL(base)) (RSI, 1), RAX
mov RAX, (LOCAL(gdt_addr) - LOCAL(base)) (RSI, 1)
/* Switch to compatibility mode. */
lgdt (LOCAL(gdtdesc) - LOCAL(base)) (RSI, 1)
/* Update %cs. */
ljmp *(LOCAL(jump_vector) - LOCAL(base)) (RSI, 1)
LOCAL(cont1):
.code32
/* Update other registers. */
movl $DATA_SEGMENT, %eax
movl %eax, %ds
movl %eax, %es
movl %eax, %fs
movl %eax, %gs
movl %eax, %ss
/* Disable paging. */
movl %cr0, %eax
andl $(~GRUB_MEMORY_CPU_CR0_PAGING_ON), %eax
movl %eax, %cr0
/* Disable amd64. */
movl $GRUB_MEMORY_CPU_AMD64_MSR, %ecx
rdmsr
andl $(~GRUB_MEMORY_CPU_AMD64_MSR_ON), %eax
wrmsr
/* Turn off PAE. */
movl %cr4, %eax
andl $GRUB_MEMORY_CPU_CR4_PAE_ON, %eax
movl %eax, %cr4
jmp LOCAL(cont2)
LOCAL(cont2):
.code32
VARIABLE(grub_relocator_forward_start)
/* mov imm32, %eax */
.byte 0xb8
RELOCATOR_VARIABLE (esp)
VARIABLE(grub_relocator_forward_dest)
.long 0
movl %eax, %edi
movl %eax, %esp
/* mov imm32, %eax */
/* mov imm32, %rax */
.byte 0xb8
RELOCATOR_VARIABLE (eax)
.long 0
/* mov imm32, %ebx */
.byte 0xbb
RELOCATOR_VARIABLE (ebx)
VARIABLE(grub_relocator_forward_src)
.long 0
movl %eax, %esi
/* mov imm32, %ecx */
.byte 0xb9
RELOCATOR_VARIABLE (ecx)
VARIABLE(grub_relocator_forward_size)
.long 0
/* mov imm32, %edx */
.byte 0xba
RELOCATOR_VARIABLE (edx)
.long 0
/* Cleared direction flag is of no problem with any current
payload and makes this implementation easier. */
/* Forward copy. */
cld
.byte 0xea
RELOCATOR_VARIABLE (eip)
.long 0
.word CODE_SEGMENT
/* GDT. Copied from loader/i386/linux.c. */
.p2align 4
LOCAL(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
.p2align 4
LOCAL(gdtdesc):
.word 0x27
LOCAL(gdt_addr):
#ifdef __x86_64__
/* Filled by the code. */
.quad 0
#else
/* Filled by the code. */
.long 0
#endif
.p2align 4
LOCAL(jump_vector):
/* Jump location. Is filled by the code */
.long 0
.long CODE_SEGMENT
#ifndef BACKWARD
LOCAL(base):
#endif
RELOCATOR_VARIABLE(end)
rep
movsb
VARIABLE(grub_relocator_forward_end)

View file

@ -16,122 +16,486 @@
* along with GRUB. If not, see <http://www.gnu.org/licenses/>.
*/
#define MAX_OVERHEAD ((RELOCATOR_SIZEOF (forward) + RELOCATOR_ALIGN) \
+ (RELOCATOR_SIZEOF (backward) + RELOCATOR_ALIGN) \
+ (RELOCATOR_SIZEOF (forward) + RELOCATOR_ALIGN) \
+ (RELOCATOR_SIZEOF (backward) + RELOCATOR_ALIGN))
#define PRE_REGION_SIZE (RELOCATOR_SIZEOF (backward) + RELOCATOR_ALIGN)
#include <grub/relocator.h>
#include <grub/relocator_private.h>
#include <grub/mm_private.h>
#include <grub/misc.h>
void *
PREFIX (alloc) (grub_size_t size)
/* TODO: use more efficient data structures if necessary. */
struct grub_relocator *
grub_relocator_new (void)
{
char *playground;
struct grub_relocator *ret;
playground = grub_malloc (size + MAX_OVERHEAD);
if (!playground)
return 0;
grub_cpu_relocator_init ();
*(grub_size_t *) playground = size;
ret = grub_zalloc (sizeof (struct grub_relocator));
if (!ret)
return NULL;
return playground + PRE_REGION_SIZE;
ret->postchunks = ~(grub_addr_t) 0;
ret->relocators_size = grub_relocator_jumper_size;
return ret;
}
void *
PREFIX (realloc) (void *relocator, grub_size_t size)
static grub_mm_header_t
get_best_header (struct grub_relocator *rel,
grub_addr_t start, grub_addr_t end, grub_addr_t align,
grub_size_t size,
grub_mm_region_t rb, grub_mm_header_t *prev,
grub_addr_t *best_addr, int from_low_priv, int collisioncheck)
{
char *playground;
grub_mm_header_t h, hp;
grub_mm_header_t hb = NULL, hbp = NULL;
if (!relocator)
return PREFIX (alloc) (size);
auto void try_addr (grub_addr_t allowable_start, grub_addr_t allowable_end);
void try_addr (grub_addr_t allowable_start, grub_addr_t allowable_end)
{
if (from_low_priv)
{
grub_addr_t addr;
playground = (char *) relocator - PRE_REGION_SIZE;
addr = ALIGN_UP (allowable_start, align);
playground = grub_realloc (playground, size + MAX_OVERHEAD);
if (!playground)
return 0;
if (addr < start)
addr = ALIGN_UP (start, align);
*(grub_size_t *) playground = size;
return playground + PRE_REGION_SIZE;
if (collisioncheck)
while (1)
{
struct grub_relocator_chunk *chunk;
for (chunk = rel->chunks; chunk; chunk = chunk->next)
if ((chunk->target <= addr
&& addr < chunk->target + chunk->size)
|| (chunk->target <= addr + size
&& addr + size < chunk->target + chunk->size)
|| (addr <= chunk->target && chunk->target < addr + size)
|| (addr <= chunk->target + chunk->size
&& chunk->target + chunk->size < addr + size))
{
addr = ALIGN_UP (chunk->target + chunk->size, align);
break;
}
if (!chunk)
break;
}
void
PREFIX(free) (void *relocator)
if (allowable_end <= addr + size)
return;
if (addr > end)
return;
if (hb == NULL || *best_addr > addr)
{
if (relocator)
grub_free ((char *) relocator - PRE_REGION_SIZE);
hb = h;
hbp = hp;
*best_addr = addr;
}
grub_err_t
PREFIX (boot) (void *relocator, grub_uint32_t dest,
struct grub_relocator32_state state)
{
grub_size_t size;
char *playground;
playground = (char *) relocator - PRE_REGION_SIZE;
size = *(grub_size_t *) playground;
grub_dprintf ("relocator",
"Relocator: source: %p, destination: 0x%x, size: 0x%lx\n",
relocator, (unsigned) dest, (unsigned long) size);
/* Very unlikely condition: Relocator may risk overwrite itself.
Just move it a bit up. */
if ((grub_addr_t) dest < (grub_addr_t) relocator
+ (RELOCATOR_SIZEOF (backward) + RELOCATOR_ALIGN)
&& (grub_addr_t) dest + (RELOCATOR_SIZEOF (forward) + RELOCATOR_ALIGN)
> (grub_addr_t) relocator)
{
void *relocator_new = ((grub_uint8_t *) relocator)
+ (RELOCATOR_SIZEOF (forward) + RELOCATOR_ALIGN)
+ (RELOCATOR_SIZEOF (backward) + RELOCATOR_ALIGN);
grub_dprintf ("relocator", "Overwrite condition detected moving "
"relocator from %p to %p\n", relocator, relocator_new);
grub_memmove (relocator_new, relocator,
(RELOCATOR_SIZEOF (forward) + RELOCATOR_ALIGN)
+ size
+ (RELOCATOR_SIZEOF (backward) + RELOCATOR_ALIGN));
relocator = relocator_new;
}
if ((grub_addr_t) dest >= (grub_addr_t) relocator)
{
int overhead;
overhead = dest -
ALIGN_UP (dest - RELOCATOR_SIZEOF (backward) - RELOCATOR_ALIGN,
RELOCATOR_ALIGN);
grub_dprintf ("relocator",
"Backward relocator: code %p, source: %p, "
"destination: 0x%x, size: 0x%lx\n",
(char *) relocator - overhead,
(char *) relocator - overhead,
(unsigned) dest - overhead,
(unsigned long) size + overhead);
write_call_relocator_bw ((char *) relocator - overhead,
(char *) relocator - overhead,
dest - overhead, size + overhead, state);
}
else
{
int overhead;
grub_addr_t addr;
overhead = ALIGN_UP (dest + size, RELOCATOR_ALIGN)
+ RELOCATOR_SIZEOF (forward) - (dest + size);
grub_dprintf ("relocator",
"Forward relocator: code %p, source: %p, "
"destination: 0x%x, size: 0x%lx\n",
(char *) relocator + size + overhead
- RELOCATOR_SIZEOF (forward),
relocator, (unsigned) dest,
(unsigned long) size + overhead);
addr = ALIGN_DOWN (allowable_end - size, align);
write_call_relocator_fw ((char *) relocator + size + overhead
- RELOCATOR_SIZEOF (forward),
relocator, dest, size + overhead, state);
if (addr > end)
addr = ALIGN_DOWN (end, align);
if (collisioncheck)
while (1)
{
struct grub_relocator_chunk *chunk;
for (chunk = rel->chunks; chunk; chunk = chunk->next)
if ((chunk->target <= addr
&& addr < chunk->target + chunk->size)
|| (chunk->target <= addr + size
&& addr + size < chunk->target + chunk->size)
|| (addr <= chunk->target && chunk->target < addr + size)
|| (addr <= chunk->target + chunk->size
&& chunk->target + chunk->size < addr + size))
{
addr = ALIGN_DOWN (chunk->target - size, align);
break;
}
if (!chunk)
break;
}
/* Not reached. */
if (allowable_start > addr)
return;
if (addr < start)
return;
if (hb == NULL || *best_addr < addr)
{
hb = h;
hbp = hp;
*best_addr = addr;
}
}
}
for (hp = NULL, h = rb->first; h; hp = h, h = h->next)
{
grub_addr_t allowable_start, allowable_end;
allowable_start = (grub_addr_t) h;
allowable_end = (grub_addr_t) (h + 1 + h->size);
try_addr (allowable_start, allowable_end);
if ((grub_addr_t) h == (grub_addr_t) (rb + 1))
try_addr (allowable_start - sizeof (*rb) - rb->pre_size,
allowable_end - sizeof (*rb));
}
*prev = hbp;
return hb;
}
static int
malloc_in_range (struct grub_relocator *rel,
grub_addr_t start, grub_addr_t end, grub_addr_t align,
grub_size_t size, grub_addr_t *res, int from_low_priv,
int collisioncheck)
{
grub_mm_region_t rb = NULL, rbp = NULL;
grub_mm_header_t hb = NULL, hbp = NULL;
grub_addr_t best_addr;
again:
{
grub_mm_region_t r, rp;
for (rp = NULL, r = grub_mm_base; r; rp = r, r = r->next)
{
if ((grub_addr_t) r + r->size + sizeof (*r) > start
&& (grub_addr_t) r <= end && r->size + sizeof (*r) >= size
&& (rb == NULL || from_low_priv ? rb > r : rb < r))
{
rb = r;
rbp = rp;
}
}
}
if (!rb)
return 0;
hb = get_best_header (rel, start, end, align, size, rb, &hbp, &best_addr,
from_low_priv, collisioncheck);
if (!hb)
{
if (from_low_priv)
start = (grub_addr_t) (rb + rb->size + sizeof (*rb));
else
end = (grub_addr_t) rb - 1;
goto again;
}
/* Special case: relocating region start. */
if (best_addr < (grub_addr_t) hbp)
{
grub_addr_t newreg_start, newreg_raw_start = best_addr + size;
grub_addr_t newreg_size, newreg_presize;
grub_mm_header_t new_header;
newreg_start = ALIGN_UP (newreg_raw_start, GRUB_MM_ALIGN);
newreg_presize = newreg_start - newreg_raw_start;
newreg_size = rb->size - (newreg_start - (grub_addr_t) rb);
if ((hb->size << GRUB_MM_ALIGN_LOG2) >= newreg_start
+ (grub_addr_t) rb)
{
grub_mm_header_t newhnext = hb->next;
grub_size_t newhsize = ((hb->size << GRUB_MM_ALIGN_LOG2)
- newreg_start
- (grub_addr_t) rb) >> GRUB_MM_ALIGN_LOG2;
new_header = (void *) (newreg_start + sizeof (*rb));
new_header->next = newhnext;
new_header->size = newhsize;
new_header->magic = GRUB_MM_FREE_MAGIC;
}
else
{
new_header = hb->next;
}
if (hbp || new_header)
{
struct grub_mm_header *newregfirst = rb->first;
struct grub_mm_region *newregnext = rb->next;
struct grub_mm_region *newreg = (void *) newreg_start;
if (hbp)
hbp->next = new_header;
else
newregfirst = new_header;
newreg->first = newregfirst;
newreg->next = newregnext;
newreg->pre_size = newreg_presize;
newreg->size = newreg_size;
if (rbp)
rbp->next = newreg;
else
grub_mm_base = newreg;
}
else
{
if (rbp)
rbp->next = rb->next;
else
grub_mm_base = rb->next;
}
*res = best_addr;
return 1;
}
{
struct grub_mm_header *foll = NULL;
if (best_addr + size <= (grub_addr_t) (hb + hb->size))
{
foll = (void *) ALIGN_UP (best_addr + size, GRUB_MM_ALIGN);
foll->magic = GRUB_MM_FREE_MAGIC;
foll->size = hb->size - (foll - hb);
}
if (best_addr - (grub_addr_t) hb >= sizeof (*hb))
{
hb->size = (best_addr - (grub_addr_t) hb) >> GRUB_MM_ALIGN_LOG2;
if (foll)
{
foll->next = hb;
if (hbp)
hbp->next = foll;
else
rb->first = foll;
}
}
else
{
if (foll)
foll->next = hb->next;
else
foll = hb->next;
if (hbp)
hbp->next = foll;
else
rb->first = foll;
}
*res = best_addr;
return 1;
}
}
grub_err_t
grub_relocator_alloc_chunk_addr (struct grub_relocator *rel, void **src,
grub_addr_t target, grub_size_t size)
{
struct grub_relocator_chunk *chunk;
grub_addr_t start;
grub_addr_t min_addr = 0, max_addr;
max_addr = rel->postchunks;
/* Keep chunks in memory in the same order as they'll be after relocation. */
for (chunk = rel->chunks; chunk; chunk = chunk->next)
{
if (chunk->target > target && chunk->src > max_addr)
max_addr = chunk->src;
if (chunk->target + chunk->size <= target
&& chunk->src + chunk->size < min_addr
&& chunk->src < rel->postchunks)
min_addr = chunk->src + chunk->size;
if ((chunk->target <= target && target < chunk->target + chunk->size)
|| (target <= chunk->target && chunk->target < target + size))
{
return grub_error (GRUB_ERR_BAD_ARGUMENT, "overlap detected");
}
}
chunk = grub_malloc (sizeof (struct grub_relocator_chunk));
if (!chunk)
return grub_errno;
do
{
/* A trick to improve Linux allocation. */
#if defined (__i386__) || defined (__x86_64__)
if (target < 0x100000)
if (malloc_in_range (rel, rel->highestnonpostaddr, ~(grub_addr_t)0, 0,
size, &start, 1, 0))
{
if (rel->postchunks < start)
rel->postchunks = start;
break;
}
#endif
if (malloc_in_range (rel, target, max_addr, 1, size, &start, 1, 0))
break;
if (malloc_in_range (rel, min_addr, target, 0, size, &start, 1, 0))
break;
grub_free (chunk);
return grub_error (GRUB_ERR_OUT_OF_MEMORY, "out of memory");
}
while (0);
if (rel->highestaddr < target + size)
rel->highestaddr = target + size;
if (rel->highestaddr < start + size)
rel->highestaddr = start + size;
if (start < rel->postchunks)
{
if (rel->highestnonpostaddr < target + size)
rel->highestnonpostaddr = target + size;
if (rel->highestnonpostaddr < start + size)
rel->highestnonpostaddr = start + size;
}
if (start < target)
rel->relocators_size += grub_relocator_backward_size;
if (start > target)
rel->relocators_size += grub_relocator_forward_size;
chunk->src = start;
chunk->target = target;
chunk->size = size;
chunk->next = rel->chunks;
rel->chunks = chunk;
*src = (void *) start;
return GRUB_ERR_NONE;
}
grub_err_t
grub_relocator_alloc_chunk_align (struct grub_relocator *rel, void **src,
grub_addr_t *target,
grub_addr_t min_addr, grub_addr_t max_addr,
grub_size_t size, grub_size_t align)
{
grub_addr_t min_addr2 = 0, max_addr2;
struct grub_relocator_chunk *chunk;
grub_addr_t start;
chunk = grub_malloc (sizeof (struct grub_relocator_chunk));
if (!chunk)
return grub_errno;
if (malloc_in_range (rel, min_addr, max_addr, align,
size, &start, 1, 1))
{
chunk->src = start;
chunk->target = start;
chunk->size = size;
chunk->next = rel->chunks;
rel->chunks = chunk;
*src = (void *) start;
*target = start;
return GRUB_ERR_NONE;
}
max_addr2 = rel->postchunks;
/* Keep chunks in memory in the same order as they'll be after
relocation. */
for (chunk = rel->chunks; chunk; chunk = chunk->next)
{
if (chunk->target > max_addr && chunk->src > max_addr2)
max_addr2 = chunk->src;
if (chunk->target + chunk->size <= min_addr
&& chunk->src + chunk->size < min_addr2
&& chunk->src < rel->postchunks)
min_addr2 = chunk->src + chunk->size;
}
if (!malloc_in_range (rel, min_addr2, max_addr2, align,
size, &start, 1, 1))
{
grub_free (chunk);
return grub_error (GRUB_ERR_OUT_OF_MEMORY, "out of memory");
}
chunk->target = ALIGN_UP (min_addr, align);
while (1)
{
struct grub_relocator_chunk *chunk2;
for (chunk2 = rel->chunks; chunk2; chunk2 = chunk2->next)
if ((chunk2->target <= chunk->target
&& chunk->target < chunk2->target + chunk2->size)
|| (chunk2->target <= chunk->target + size
&& chunk->target + size < chunk2->target + chunk2->size)
|| (chunk->target <= chunk2->target && chunk2->target
< chunk->target + size)
|| (chunk->target <= chunk2->target + chunk2->size
&& chunk2->target + chunk2->size < chunk->target + size))
{
chunk->target = ALIGN_UP (chunk2->target + chunk2->size, align);
break;
}
if (!chunk2)
break;
}
if (start < chunk->target)
rel->relocators_size += grub_relocator_backward_size;
if (start > chunk->target)
rel->relocators_size += grub_relocator_forward_size;
chunk->src = start;
chunk->size = size;
chunk->next = rel->chunks;
rel->chunks = chunk;
*src = (void *) start;
*target = chunk->target;
return GRUB_ERR_NONE;
}
void
grub_relocator_unload (struct grub_relocator *rel)
{
struct grub_relocator_chunk *chunk, *next;
for (chunk = rel->chunks; chunk; chunk = next)
{
grub_fatal ("Relocator unloading isn't implemented yet");
next = chunk->next;
grub_free (chunk);
}
}
grub_err_t
grub_relocator_prepare_relocs (struct grub_relocator *rel, grub_addr_t addr,
grub_addr_t *relstart)
{
struct grub_relocator_chunk *chunk;
grub_addr_t rels;
grub_addr_t rels0;
if (!malloc_in_range (rel, 0, ~(grub_addr_t)0, grub_relocator_align,
rel->relocators_size, &rels0, 1, 1))
return grub_error (GRUB_ERR_OUT_OF_MEMORY, "out of memory");
rels = rels0;
for (chunk = rel->chunks; chunk; chunk = chunk->next)
{
if (chunk->src < chunk->target)
{
grub_cpu_relocator_backward ((void *) rels,
(void *) chunk->src,
(void *) chunk->target,
chunk->size);
rels += grub_relocator_backward_size;
}
if (chunk->src > chunk->target)
{
grub_cpu_relocator_forward ((void *) rels,
(void *) chunk->src,
(void *) chunk->target,
chunk->size);
rels += grub_relocator_forward_size;
}
}
grub_cpu_relocator_jumper ((void *) rels, addr);
*relstart = rels0;
return GRUB_ERR_NONE;
}

View file

@ -0,0 +1,85 @@
/*
* 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>
#include <grub/i386/memory.h>
VARIABLE(grub_relocator_backward_start)
/* mov imm32, %rax */
.byte 0x48
.byte 0xb8
RELOCATOR_VARIABLE(dest)
.long 0, 0
movq %rax, %rdi
/* mov imm64, %rax */
.byte 0x48
.byte 0xb8
RELOCATOR_VARIABLE(src)
.long 0, 0
movq %rax, %rsi
/* mov imm32, %ecx */
.byte 0x48
.byte 0xb9
RELOCATOR_VARIABLE(size)
.long 0, 0
add %rcx, %rsi
add %rcx, %rdi
/* Backward movsb is implicitly off-by-one. compensate that. */
sub $1, %rsi
sub $1, %rdi
/* Backward copy. */
std
rep
movsb
VARIABLE(grub_relocator_backward_end)
VARIABLE(grub_relocator_forward_start)
/* mov imm64, %rax */
.byte 0x48
.byte 0xb8
VARIABLE(grub_relocator_forward_dest)
.long 0, 0
movq %rax, %rdi
/* mov imm64, %rax */
.byte 0x48
.byte 0xb8
VARIABLE(grub_relocator_forward_src)
.long 0, 0
movq %rax, %rsi
xorq %rcx, %rcx
/* mov imm64, %rcx */
.byte 0x48
.byte 0xb9
VARIABLE(grub_relocator_forward_size)
.long 0, 0
/* Forward copy. */
cld
rep
movsb
VARIABLE(grub_relocator_forward_end)