merge mainline into intwrap

This commit is contained in:
Vladimir 'phcoder' Serbinenko 2010-08-29 23:21:21 +02:00
commit 069c9c5fda
137 changed files with 8057 additions and 3591 deletions

View file

@ -0,0 +1,104 @@
/*
* 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/>.
*/
#include <grub/relocator.h>
#include <grub/relocator_private.h>
#include <grub/memory.h>
#include <grub/efi/efi.h>
#include <grub/efi/api.h>
#include <grub/term.h>
#define NEXT_MEMORY_DESCRIPTOR(desc, size) \
((grub_efi_memory_descriptor_t *) ((char *) (desc) + (size)))
unsigned
grub_relocator_firmware_get_max_events (void)
{
grub_efi_uintn_t mmapsize = 0, descriptor_size = 0;
grub_efi_uint32_t descriptor_version = 0;
grub_efi_uintn_t key;
grub_efi_get_memory_map (&mmapsize, NULL, &key, &descriptor_size,
&descriptor_version);
/* Since grub_relocator_firmware_fill_events uses malloc
we need some reserve. Hence +10. */
return 2 * (mmapsize / descriptor_size + 10);
}
unsigned
grub_relocator_firmware_fill_events (struct grub_relocator_mmap_event *events)
{
grub_efi_uintn_t mmapsize = 0, desc_size = 0;
grub_efi_uint32_t descriptor_version = 0;
grub_efi_memory_descriptor_t *descs = NULL;
grub_efi_uintn_t key;
int counter = 0;
grub_efi_memory_descriptor_t *desc;
grub_efi_get_memory_map (&mmapsize, NULL, &key, &desc_size,
&descriptor_version);
descs = grub_malloc (mmapsize);
if (!descs)
return 0;
grub_efi_get_memory_map (&mmapsize, descs, &key, &desc_size,
&descriptor_version);
for (desc = descs;
(char *) desc < ((char *) descs + mmapsize);
desc = NEXT_MEMORY_DESCRIPTOR (desc, desc_size))
{
if (desc->type != GRUB_EFI_CONVENTIONAL_MEMORY)
continue;
events[counter].type = REG_FIRMWARE_START;
events[counter].pos = desc->physical_start;
counter++;
events[counter].type = REG_FIRMWARE_END;
events[counter].pos = desc->physical_start + (desc->num_pages << 12);
counter++;
}
return counter;
}
int
grub_relocator_firmware_alloc_region (grub_addr_t start, grub_size_t size)
{
grub_efi_boot_services_t *b;
grub_efi_physical_address_t address = start;
grub_efi_status_t status;
if (grub_efi_is_finished)
return 1;
b = grub_efi_system_table->boot_services;
status = efi_call_4 (b->allocate_pages, GRUB_EFI_ALLOCATE_ADDRESS,
GRUB_EFI_LOADER_DATA, size >> 12, &address);
return (status == GRUB_EFI_SUCCESS);
}
void
grub_relocator_firmware_free_region (grub_addr_t start, grub_size_t size)
{
grub_efi_boot_services_t *b;
if (grub_efi_is_finished)
return;
b = grub_efi_system_table->boot_services;
efi_call_2 (b->free_pages, start, size >> 12);
}

View file

@ -19,84 +19,248 @@
#include <grub/mm.h>
#include <grub/misc.h>
#include <grub/types.h>
#include <grub/types.h>
#include <grub/err.h>
#include <grub/term.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_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_chunk_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_chunk_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_uint8_t grub_relocator16_start;
extern grub_uint8_t grub_relocator16_end;
extern grub_uint16_t grub_relocator16_cs;
extern grub_uint16_t grub_relocator16_ip;
extern grub_uint16_t grub_relocator16_ds;
extern grub_uint16_t grub_relocator16_es;
extern grub_uint16_t grub_relocator16_fs;
extern grub_uint16_t grub_relocator16_gs;
extern grub_uint16_t grub_relocator16_ss;
extern grub_uint16_t grub_relocator16_sp;
extern grub_uint32_t grub_relocator16_edx;
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;
extern grub_uint8_t grub_relocator32_start;
extern grub_uint8_t grub_relocator32_end;
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_esi;
#define RELOCATOR_SIZEOF(x) (&grub_relocator32_##x##_end - &grub_relocator32_##x##_start)
#define RELOCATOR_ALIGN 16
#define PREFIX(x) grub_relocator32_ ## x
extern grub_uint8_t grub_relocator64_start;
extern grub_uint8_t grub_relocator64_end;
extern grub_uint64_t grub_relocator64_rax;
extern grub_uint64_t grub_relocator64_rbx;
extern grub_uint64_t grub_relocator64_rcx;
extern grub_uint64_t grub_relocator64_rdx;
extern grub_uint64_t grub_relocator64_rip;
extern grub_uint64_t grub_relocator64_rip_addr;
extern grub_uint64_t grub_relocator64_rsp;
extern grub_uint64_t grub_relocator64_rsi;
extern grub_addr_t grub_relocator64_cr3;
static void
write_call_relocator_bw (void *ptr, void *src, grub_uint32_t dest,
grub_size_t size, struct grub_relocator32_state state)
#define RELOCATOR_SIZEOF(x) (&grub_relocator##x##_end - &grub_relocator##x##_start)
grub_size_t grub_relocator_align = 1;
grub_size_t grub_relocator_forward_size;
grub_size_t grub_relocator_backward_size;
#ifdef __x86_64__
grub_size_t grub_relocator_jumper_size = 12;
#else
grub_size_t grub_relocator_jumper_size = 7;
#endif
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;
#ifdef __x86_64__
/* movq imm64, %rax (for relocator) */
*(grub_uint8_t *) ptr = 0x48;
ptr++;
*(grub_uint8_t *) ptr = 0xb8;
ptr++;
*(grub_uint64_t *) ptr = addr;
ptr += sizeof (grub_uint64_t);
#else
/* movl imm32, %eax (for relocator) */
*(grub_uint8_t *) ptr = 0xb8;
ptr++;
*(grub_uint32_t *) ptr = addr;
ptr += sizeof (grub_uint32_t);
#endif
/* jmp $eax/$rax */
*(grub_uint8_t *) ptr = 0xff;
ptr++;
*(grub_uint8_t *) ptr = 0xe0;
ptr++;
}
#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_chunk_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_chunk_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_err_t err;
void *relst;
grub_relocator_chunk_t ch;
err = grub_relocator_alloc_chunk_align (rel, &ch, 0,
(0xffffffff - RELOCATOR_SIZEOF (32))
+ 1, RELOCATOR_SIZEOF (32), 16,
GRUB_RELOCATOR_PREFERENCE_NONE);
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_relocator32_esi = state.esi;
grub_memmove (get_virtual_current_address (ch), &grub_relocator32_start,
RELOCATOR_SIZEOF (32));
err = grub_relocator_prepare_relocs (rel, get_physical_target_address (ch),
&relst, NULL);
if (err)
return err;
asm volatile ("cli");
((void (*) (void)) relst) ();
/* Not reached. */
return GRUB_ERR_NONE;
}
grub_err_t
grub_relocator16_boot (struct grub_relocator *rel,
struct grub_relocator16_state state)
{
grub_err_t err;
void *relst;
grub_relocator_chunk_t ch;
err = grub_relocator_alloc_chunk_align (rel, &ch, 0,
0xa0000 - RELOCATOR_SIZEOF (16),
RELOCATOR_SIZEOF (16), 16,
GRUB_RELOCATOR_PREFERENCE_NONE);
if (err)
return err;
grub_relocator16_cs = state.cs;
grub_relocator16_ip = state.ip;
grub_relocator16_ds = state.ds;
grub_relocator16_es = state.es;
grub_relocator16_fs = state.fs;
grub_relocator16_gs = state.gs;
grub_relocator16_ss = state.ss;
grub_relocator16_sp = state.sp;
grub_relocator16_edx = state.edx;
grub_memmove (get_virtual_current_address (ch), &grub_relocator16_start,
RELOCATOR_SIZEOF (16));
err = grub_relocator_prepare_relocs (rel, get_physical_target_address (ch),
&relst, NULL);
if (err)
return err;
asm volatile ("cli");
((void (*) (void)) relst) ();
/* Not reached. */
return GRUB_ERR_NONE;
}
grub_err_t
grub_relocator64_boot (struct grub_relocator *rel,
struct grub_relocator64_state state,
grub_addr_t min_addr, grub_addr_t max_addr)
{
grub_err_t err;
void *relst;
grub_relocator_chunk_t ch;
err = grub_relocator_alloc_chunk_align (rel, &ch, min_addr,
max_addr - RELOCATOR_SIZEOF (64),
RELOCATOR_SIZEOF (64), 16,
GRUB_RELOCATOR_PREFERENCE_NONE);
if (err)
return err;
grub_relocator64_rax = state.rax;
grub_relocator64_rbx = state.rbx;
grub_relocator64_rcx = state.rcx;
grub_relocator64_rdx = state.rdx;
grub_relocator64_rip = state.rip;
grub_relocator64_rsp = state.rsp;
grub_relocator64_rsi = state.rsi;
grub_relocator64_cr3 = state.cr3;
grub_memmove (get_virtual_current_address (ch), &grub_relocator64_start,
RELOCATOR_SIZEOF (64));
err = grub_relocator_prepare_relocs (rel, get_physical_target_address (ch),
&relst, NULL);
if (err)
return err;
asm volatile ("cli");
((void (*) (void)) relst) ();
/* Not reached. */
return GRUB_ERR_NONE;
}

View file

@ -0,0 +1,193 @@
/*
* 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/>.
*/
/* The code segment of the protected mode. */
#define CODE_SEGMENT 0x08
/* The data segment of the protected mode. */
#define DATA_SEGMENT 0x10
#define PSEUDO_REAL_CSEG 0x18
#define PSEUDO_REAL_DSEG 0x20
#include "relocator_common.S"
.p2align 4 /* force 16-byte alignment */
VARIABLE(grub_relocator16_start)
PREAMBLE
movl %esi, %eax
movw %ax, (LOCAL (cs_base_bytes12) - LOCAL (base)) (RSI, 1)
shrl $16, %eax
movb %al, (LOCAL (cs_base_byte3) - LOCAL (base)) (RSI, 1)
RELOAD_GDT
.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
#ifdef __x86_64__
/* Disable amd64. */
movl $GRUB_MEMORY_CPU_AMD64_MSR, %ecx
rdmsr
andl $(~GRUB_MEMORY_CPU_AMD64_MSR_ON), %eax
wrmsr
#endif
/* Turn off PAE. */
movl %cr4, %eax
andl $(~GRUB_MEMORY_CPU_CR4_PAE_ON), %eax
movl %eax, %cr4
/* Update other registers. */
movl $PSEUDO_REAL_DSEG, %eax
movl %eax, %ds
movl %eax, %es
movl %eax, %fs
movl %eax, %gs
movl %eax, %ss
movl %esi, %eax
shrl $4, %eax
movw %ax, (LOCAL (segment) - LOCAL (base)) (%esi, 1)
/* jump to a 16 bit segment */
ljmp $PSEUDO_REAL_CSEG, $(LOCAL (cont2) - LOCAL(base))
LOCAL(cont2):
.code16
/* clear the PE bit of CR0 */
movl %cr0, %eax
andl $(~GRUB_MEMORY_CPU_CR0_PE_ON), %eax
movl %eax, %cr0
/* flush prefetch queue, reload %cs */
/* ljmp */
.byte 0xea
.word LOCAL(cont3)-LOCAL(base)
LOCAL(segment):
.word 0
LOCAL(cont3):
/* we are in real mode now
* set up the real mode segment registers : DS, SS, ES
*/
/* movw imm16, %ax. */
.byte 0xb8
VARIABLE(grub_relocator16_ds)
.word 0
movw %ax, %ds
/* movw imm16, %ax. */
.byte 0xb8
VARIABLE(grub_relocator16_es)
.word 0
movw %ax, %es
/* movw imm16, %ax. */
.byte 0xb8
VARIABLE(grub_relocator16_fs)
.word 0
movw %ax, %fs
/* movw imm16, %ax. */
.byte 0xb8
VARIABLE(grub_relocator16_gs)
.word 0
movw %ax, %gs
/* movw imm16, %ax. */
.byte 0xb8
VARIABLE(grub_relocator16_ss)
.word 0
movw %ax, %ss
/* movw imm16, %ax. */
.byte 0xb8
VARIABLE(grub_relocator16_sp)
.word 0
movw %ax, %ss
/* movw imm32, %edx. */
.byte 0x66, 0xba
VARIABLE(grub_relocator16_edx)
.long 0
/* Cleared direction flag is of no problem with any current
payload and makes this implementation easier. */
cld
/* ljmp */
.byte 0xea
VARIABLE(grub_relocator16_ip)
.word 0
VARIABLE(grub_relocator16_cs)
.word 0
.code32
/* GDT. Copied from loader/i386/linux.c. */
.p2align 4
LOCAL(gdt):
.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
LOCAL(cs_base_bytes12):
.word 0
LOCAL(cs_base_byte3):
.byte 0
.byte 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
LOCAL(gdt_end):
VARIABLE(grub_relocator16_end)

View file

@ -0,0 +1,120 @@
/*
* 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/>.
*/
/* The code segment of the protected mode. */
#define CODE_SEGMENT 0x10
/* The data segment of the protected mode. */
#define DATA_SEGMENT 0x18
#include "relocator_common.S"
.p2align 4 /* force 16-byte alignment */
VARIABLE(grub_relocator32_start)
PREAMBLE
RELOAD_GDT
.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
#ifdef __x86_64__
/* Disable amd64. */
movl $GRUB_MEMORY_CPU_AMD64_MSR, %ecx
rdmsr
andl $(~GRUB_MEMORY_CPU_AMD64_MSR_ON), %eax
wrmsr
#endif
/* 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_esi)
.long 0
movl %eax, %esi
/* 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
LOCAL(gdt_end):
VARIABLE(grub_relocator32_end)

View file

@ -0,0 +1,160 @@
/*
* 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
#define CODE_SEGMENT 0x08
/* The data segment of the protected mode. */
#define DATA_SEGMENT 0x10
#include "relocator_common.S"
.p2align 4 /* force 16-byte alignment */
VARIABLE(grub_relocator64_start)
PREAMBLE
#ifndef __x86_64__
DISABLE_PAGING
/* 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
RELOAD_GDT
#else
/* mov imm64, %rax */
.byte 0x48
.byte 0xb8
VARIABLE(grub_relocator64_cr3)
.quad 0
movq %rax, %cr3
#endif
.code64
/* mov imm64, %rax */
.byte 0x48
.byte 0xb8
VARIABLE(grub_relocator64_rsp)
.quad 0
movq %rax, %rsp
/* 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
jmp *LOCAL(jump_addr) (%rip)
LOCAL(jump_addr):
VARIABLE(grub_relocator64_rip)
.quad 0
#ifndef __x86_64__
.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. */
LOCAL(gdt_end):
#endif
VARIABLE(grub_relocator64_end)

View file

@ -19,232 +19,62 @@
#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
.p2align 2
/* 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)
VARIABLE(grub_relocator_backward_chunk_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)
.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
#ifdef __x86_64__
/* Disable amd64. */
movl $GRUB_MEMORY_CPU_AMD64_MSR, %ecx
rdmsr
andl $(~GRUB_MEMORY_CPU_AMD64_MSR_ON), %eax
wrmsr
#endif
/* 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_chunk_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

@ -0,0 +1,82 @@
/*
* 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 %rsi
#else
#define RAX %eax
#define RSI %esi
#endif
.macro DISABLE_PAGING
#ifdef GRUB_MACHINE_IEEE1275
#endif
movl %cr0, %eax
andl $(~GRUB_MEMORY_CPU_CR0_PAGING_ON), %eax
movl %eax, %cr0
.endm
.macro PREAMBLE
LOCAL(base):
/* %rax contains now our new 'base'. */
mov RAX, RSI
add $(LOCAL(cont0) - LOCAL(base)), RAX
jmp *RAX
LOCAL(cont0):
.endm
.macro RELOAD_GDT
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)
.p2align 4
LOCAL(gdtdesc):
.word LOCAL(gdt_end) - LOCAL(gdt)
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
LOCAL(cont1):
.endm

View file

@ -0,0 +1,95 @@
/*
* 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/>.
*/
#include <grub/relocator.h>
#include <grub/relocator_private.h>
#include <grub/memory.h>
#include <grub/ieee1275/ieee1275.h>
unsigned
grub_relocator_firmware_get_max_events (void)
{
int counter = 0;
auto int NESTED_FUNC_ATTR count (grub_uint64_t addr __attribute__ ((unused)),
grub_uint64_t len __attribute__ ((unused)),
grub_uint32_t type __attribute__ ((unused)));
int NESTED_FUNC_ATTR count (grub_uint64_t addr __attribute__ ((unused)),
grub_uint64_t len __attribute__ ((unused)),
grub_uint32_t type __attribute__ ((unused)))
{
counter++;
return 0;
}
if (grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_CANNOT_INTERPRET))
return 0;
grub_machine_mmap_iterate (count);
return 2 * counter;
}
unsigned
grub_relocator_firmware_fill_events (struct grub_relocator_mmap_event *events)
{
int counter = 0;
auto int NESTED_FUNC_ATTR fill (grub_uint64_t addr, grub_uint64_t len,
grub_uint32_t type);
int NESTED_FUNC_ATTR fill (grub_uint64_t addr, grub_uint64_t len,
grub_uint32_t type)
{
if (type != GRUB_MACHINE_MEMORY_AVAILABLE)
return 0;
if (grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_NO_PRE1_5M_CLAIM))
{
if (addr + len <= 0x180000)
return 0;
if (addr < 0x180000)
{
len = addr + len - 0x180000;
addr = 0x180000;
}
}
events[counter].type = REG_FIRMWARE_START;
events[counter].pos = addr;
counter++;
events[counter].type = REG_FIRMWARE_END;
events[counter].pos = addr + len;
counter++;
return 0;
}
if (grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_CANNOT_INTERPRET))
return 0;
grub_machine_mmap_iterate (fill);
return counter;
}
int
grub_relocator_firmware_alloc_region (grub_addr_t start, grub_size_t size)
{
return (grub_claimmap (start, size) >= 0);
}
void
grub_relocator_firmware_free_region (grub_addr_t start, grub_size_t size)
{
grub_ieee1275_release (start, size);
}

View file

@ -25,26 +25,33 @@
#include <grub/cache.h>
#include <grub/mips/relocator.h>
#include <grub/relocator_private.h>
/* Remark: doesn't work with source outside of 4G.
Use relocator64 in this case.
*/
/* Do we need mips64? */
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_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;
#define REGW_SIZEOF (2 * sizeof (grub_uint32_t))
#define JUMP_SIZEOF (2 * sizeof (grub_uint32_t))
#define RELOCATOR_SRC_SIZEOF(x) (&grub_relocator32_##x##_end \
- &grub_relocator32_##x##_start)
#define RELOCATOR_SRC_SIZEOF(x) (&grub_relocator_##x##_end \
- &grub_relocator_##x##_start)
#define RELOCATOR_SIZEOF(x) (RELOCATOR_SRC_SIZEOF(x) \
+ REGW_SIZEOF * (31 + 3) + JUMP_SIZEOF)
#define RELOCATOR_ALIGN 16
+ REGW_SIZEOF * 3)
grub_size_t grub_relocator_align = sizeof (grub_uint32_t);
grub_size_t grub_relocator_forward_size;
grub_size_t grub_relocator_backward_size;
grub_size_t grub_relocator_jumper_size = JUMP_SIZEOF + REGW_SIZEOF;
#define PREFIX(x) grub_relocator32_ ## x
void
grub_cpu_relocator_init (void)
{
grub_relocator_forward_size = RELOCATOR_SIZEOF(forward);
grub_relocator_backward_size = RELOCATOR_SIZEOF(backward);
}
static void
write_reg (int regn, grub_uint32_t val, void **target)
@ -69,44 +76,74 @@ write_jump (int regn, void **target)
*target = ((grub_uint32_t *) *target) + 1;
}
static void
write_call_relocator_bw (void *ptr0, 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)
{
write_reg (1, addr, &rels);
write_jump (1, &rels);
}
void
grub_cpu_relocator_backward (void *ptr0, void *src, void *dest,
grub_size_t size)
{
void *ptr = ptr0;
int i;
write_reg (8, (grub_uint32_t) src, &ptr);
write_reg (9, dest, &ptr);
write_reg (10, size, &ptr);
grub_memcpy (ptr, &grub_relocator32_backward_start,
write_reg (9, (grub_uint32_t) dest, &ptr);
write_reg (10, (grub_uint32_t) size, &ptr);
grub_memcpy (ptr, &grub_relocator_backward_start,
RELOCATOR_SRC_SIZEOF (backward));
ptr = (grub_uint8_t *) ptr + RELOCATOR_SRC_SIZEOF (backward);
for (i = 1; i < 32; i++)
write_reg (i, state.gpr[i], &ptr);
write_jump (state.jumpreg, &ptr);
grub_arch_sync_caches (ptr0, (grub_uint8_t *) ptr - (grub_uint8_t *) ptr0);
grub_dprintf ("relocator", "Backward relocator: about to jump to %p\n", ptr0);
((void (*) (void)) ptr0) ();
}
static void
write_call_relocator_fw (void *ptr0, void *src, grub_uint32_t dest,
grub_size_t size, struct grub_relocator32_state state)
void
grub_cpu_relocator_forward (void *ptr0, void *src, void *dest,
grub_size_t size)
{
void *ptr = ptr0;
int i;
write_reg (8, (grub_uint32_t) src, &ptr);
write_reg (9, dest, &ptr);
write_reg (10, size, &ptr);
grub_memcpy (ptr, &grub_relocator32_forward_start,
write_reg (9, (grub_uint32_t) dest, &ptr);
write_reg (10, (grub_uint32_t) size, &ptr);
grub_memcpy (ptr, &grub_relocator_forward_start,
RELOCATOR_SRC_SIZEOF (forward));
ptr = (grub_uint8_t *) ptr + RELOCATOR_SRC_SIZEOF (forward);
}
grub_err_t
grub_relocator32_boot (struct grub_relocator *rel,
struct grub_relocator32_state state)
{
grub_relocator_chunk_t ch;
void *ptr;
grub_err_t err;
void *relst;
grub_size_t relsize;
grub_size_t stateset_size = 31 * REGW_SIZEOF + JUMP_SIZEOF;
unsigned i;
grub_addr_t vtarget;
err = grub_relocator_alloc_chunk_align (rel, &ch, 0,
(0xffffffff - stateset_size)
+ 1, stateset_size,
sizeof (grub_uint32_t),
GRUB_RELOCATOR_PREFERENCE_NONE);
if (err)
return err;
ptr = get_virtual_current_address (ch);
for (i = 1; i < 32; i++)
write_reg (i, state.gpr[i], &ptr);
write_jump (state.jumpreg, &ptr);
grub_arch_sync_caches (ptr0, (grub_uint8_t *) ptr - (grub_uint8_t *) ptr0);
grub_dprintf ("relocator", "Forward relocator: about to jump to %p\n", ptr0);
((void (*) (void)) ptr0) ();
}
#include "../relocator.c"
vtarget = (grub_addr_t) grub_map_memory (get_physical_target_address (ch),
stateset_size);
err = grub_relocator_prepare_relocs (rel, vtarget, &relst, &relsize);
if (err)
return err;
grub_arch_sync_caches ((void *) relst, relsize);
((void (*) (void)) relst) ();
/* Not reached. */
return GRUB_ERR_NONE;
}

View file

@ -20,39 +20,39 @@
.p2align 4 /* force 16-byte alignment */
VARIABLE (grub_relocator32_forward_start)
VARIABLE (grub_relocator_forward_start)
move $a0, $9
move $a1, $10
copycont1:
lb $11,0($8)
sb $11,0($9)
addiu $8, $8, 0x1
addiu $9, $9, 0x1
addiu $10, $10, 0xffff
addiu $8, $8, 1
addiu $9, $9, 1
addiu $10, $10, -1
bne $10, $0, copycont1
#include "../../kern/mips/cache_flush.S"
VARIABLE (grub_relocator32_forward_end)
VARIABLE (grub_relocator_forward_end)
VARIABLE (grub_relocator32_backward_start)
VARIABLE (grub_relocator_backward_start)
move $a0, $9
move $a1, $10
addu $9, $9, $10
addu $8, $8, $10
/* Backward movsl is implicitly off-by-one. compensate that. */
addiu $9, $9, 0xffff
addiu $8, $8, 0xffff
addiu $9, $9, -1
addiu $8, $8, -1
copycont2:
lb $11,0($8)
sb $11,0($9)
addiu $8, $8, 0xffff
addiu $9, $9, 0xffff
addiu $10, 0xffff
addiu $8, $8, -1
addiu $9, $9, -1
addiu $10, $10, -1
bne $10, $0, copycont2
#include "../../kern/mips/cache_flush.S"
VARIABLE (grub_relocator32_backward_end)
VARIABLE (grub_relocator_backward_end)

View file

@ -0,0 +1,142 @@
/*
* 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/mm.h>
#include <grub/misc.h>
#include <grub/types.h>
#include <grub/types.h>
#include <grub/err.h>
#include <grub/cache.h>
#include <grub/powerpc/relocator.h>
#include <grub/relocator_private.h>
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;
#define REGW_SIZEOF (2 * sizeof (grub_uint32_t))
#define JUMP_SIZEOF (sizeof (grub_uint32_t))
#define RELOCATOR_SRC_SIZEOF(x) (&grub_relocator_##x##_end \
- &grub_relocator_##x##_start)
#define RELOCATOR_SIZEOF(x) (RELOCATOR_SRC_SIZEOF(x) \
+ REGW_SIZEOF * 3)
grub_size_t grub_relocator_align = sizeof (grub_uint32_t);
grub_size_t grub_relocator_forward_size;
grub_size_t grub_relocator_backward_size;
grub_size_t grub_relocator_jumper_size = JUMP_SIZEOF + REGW_SIZEOF;
void
grub_cpu_relocator_init (void)
{
grub_relocator_forward_size = RELOCATOR_SIZEOF(forward);
grub_relocator_backward_size = RELOCATOR_SIZEOF(backward);
}
static void
write_reg (int regn, grub_uint32_t val, void **target)
{
/* lis r, val >> 16 */
*(grub_uint32_t *) *target =
((0x3c00 | (regn << 5)) << 16) | (val >> 16);
*target = ((grub_uint32_t *) *target) + 1;
/* ori r, r, val & 0xffff. */
*(grub_uint32_t *) *target = (((0x6000 | regn << 5 | regn) << 16)
| (val & 0xffff));
*target = ((grub_uint32_t *) *target) + 1;
}
static void
write_jump (void **target)
{
/* blr. */
*(grub_uint32_t *) *target = 0x4e800020;
*target = ((grub_uint32_t *) *target) + 1;
}
void
grub_cpu_relocator_jumper (void *rels, grub_addr_t addr)
{
write_reg (GRUB_PPC_JUMP_REGISTER, addr, &rels);
write_jump (&rels);
}
void
grub_cpu_relocator_backward (void *ptr0, void *src, void *dest,
grub_size_t size)
{
void *ptr = ptr0;
write_reg (8, (grub_uint32_t) src, &ptr);
write_reg (9, (grub_uint32_t) dest, &ptr);
write_reg (10, (grub_uint32_t) size, &ptr);
grub_memcpy (ptr, &grub_relocator_backward_start,
RELOCATOR_SRC_SIZEOF (backward));
}
void
grub_cpu_relocator_forward (void *ptr0, void *src, void *dest,
grub_size_t size)
{
void *ptr = ptr0;
write_reg (8, (grub_uint32_t) src, &ptr);
write_reg (9, (grub_uint32_t) dest, &ptr);
write_reg (10, (grub_uint32_t) size, &ptr);
grub_memcpy (ptr, &grub_relocator_forward_start,
RELOCATOR_SRC_SIZEOF (forward));
}
grub_err_t
grub_relocator32_boot (struct grub_relocator *rel,
struct grub_relocator32_state state)
{
void *ptr;
grub_err_t err;
void *relst;
grub_size_t relsize;
grub_size_t stateset_size = 32 * REGW_SIZEOF + JUMP_SIZEOF;
unsigned i;
grub_relocator_chunk_t ch;
err = grub_relocator_alloc_chunk_align (rel, &ch, 0,
(0xffffffff - stateset_size)
+ 1, stateset_size,
sizeof (grub_uint32_t),
GRUB_RELOCATOR_PREFERENCE_NONE);
if (err)
return err;
ptr = get_virtual_current_address (ch);
for (i = 0; i < 32; i++)
write_reg (i, state.gpr[i], &ptr);
write_jump (&ptr);
err = grub_relocator_prepare_relocs (rel, get_physical_target_address (ch),
&relst, &relsize);
if (err)
return err;
grub_arch_sync_caches ((void *) relst, relsize);
((void (*) (void)) relst) ();
/* Not reached. */
return GRUB_ERR_NONE;
}

View file

@ -0,0 +1,60 @@
/*
* 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>
.p2align 4 /* force 16-byte alignment */
VARIABLE (grub_relocator_forward_start)
mr 3, 9
mr 4, 10
copycont1:
lbz 11,0(8)
stb 11,0(9)
addi 8, 8, 0x1
addi 9, 9, 0x1
addi 10, 10, -1
cmpwi 10, 0
bne copycont1
#include "../../kern/powerpc/cache_flush.S"
VARIABLE (grub_relocator_forward_end)
VARIABLE (grub_relocator_backward_start)
mr 3, 9
mr 4, 10
add 9, 9, 10
add 8, 8, 10
/* Backward movsl is implicitly off-by-one. compensate that. */
addi 9, 9, -1
addi 8, 8, -1
copycont2:
lbz 11,0(8)
stb 11,0(9)
addi 8, 8, -1
addi 9, 9, -1
addi 10, 10, -1
cmpwi 10, 0
bne copycont2
#include "../../kern/powerpc/cache_flush.S"
VARIABLE (grub_relocator_backward_end)

File diff suppressed because it is too large Load diff

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>
.p2align 2
VARIABLE(grub_relocator_backward_start)
/* mov imm32, %rax */
.byte 0x48
.byte 0xb8
VARIABLE(grub_relocator_backward_dest)
.long 0, 0
movq %rax, %rdi
/* mov imm64, %rax */
.byte 0x48
.byte 0xb8
VARIABLE(grub_relocator_backward_src)
.long 0, 0
movq %rax, %rsi
/* mov imm64, %rcx */
.byte 0x48
.byte 0xb9
VARIABLE(grub_relocator_backward_chunk_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
/* mov imm64, %rcx */
.byte 0x48
.byte 0xb9
VARIABLE(grub_relocator_forward_chunk_size)
.long 0, 0
/* Forward copy. */
cld
rep
movsb
VARIABLE(grub_relocator_forward_end)