diff options
Diffstat (limited to 'grub-core/kern/ia64')
-rw-r--r-- | grub-core/kern/ia64/cache.c | 35 | ||||
-rw-r--r-- | grub-core/kern/ia64/dl.c | 150 | ||||
-rw-r--r-- | grub-core/kern/ia64/dl_helper.c | 241 | ||||
-rw-r--r-- | grub-core/kern/ia64/efi/init.c | 80 | ||||
-rw-r--r-- | grub-core/kern/ia64/efi/startup.S | 44 |
5 files changed, 550 insertions, 0 deletions
diff --git a/grub-core/kern/ia64/cache.c b/grub-core/kern/ia64/cache.c new file mode 100644 index 0000000..13efd30 --- /dev/null +++ b/grub-core/kern/ia64/cache.c @@ -0,0 +1,35 @@ +/* init.c - initialize an ia64-based EFI system */ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2008 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/types.h> +#include <grub/cache.h> + +void +grub_arch_sync_caches (void *address, grub_size_t len) +{ + /* Cache line length is at least 32. */ + len += (grub_uint64_t)address & 0x1f; + grub_uint64_t a = (grub_uint64_t)address & ~0x1f; + + /* Flush data. */ + for (len = (len + 31) & ~0x1f; len > 0; len -= 0x20, a += 0x20) + asm volatile ("fc.i %0" : : "r" (a)); + /* Sync and serialize. Maybe extra. */ + asm volatile (";; sync.i;; srlz.i;;"); +} diff --git a/grub-core/kern/ia64/dl.c b/grub-core/kern/ia64/dl.c new file mode 100644 index 0000000..db59300 --- /dev/null +++ b/grub-core/kern/ia64/dl.c @@ -0,0 +1,150 @@ +/* dl.c - arch-dependent part of loadable module support */ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2002,2004,2005,2007,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/dl.h> +#include <grub/elf.h> +#include <grub/misc.h> +#include <grub/err.h> +#include <grub/mm.h> +#include <grub/i18n.h> +#include <grub/ia64/reloc.h> + +#define MASK19 ((1 << 19) - 1) +#define MASK20 ((1 << 20) - 1) + +/* Check if EHDR is a valid ELF header. */ +grub_err_t +grub_arch_dl_check_header (void *ehdr) +{ + Elf_Ehdr *e = ehdr; + + /* Check the magic numbers. */ + if (e->e_ident[EI_CLASS] != ELFCLASS64 + || e->e_ident[EI_DATA] != ELFDATA2LSB + || e->e_machine != EM_IA_64) + return grub_error (GRUB_ERR_BAD_OS, N_("invalid arch-dependent ELF magic")); + + return GRUB_ERR_NONE; +} + +#pragma GCC diagnostic ignored "-Wcast-align" + +/* Relocate symbols. */ +grub_err_t +grub_arch_dl_relocate_symbols (grub_dl_t mod, void *ehdr, + Elf_Shdr *s, grub_dl_segment_t seg) +{ + Elf_Rela *rel, *max; + + for (rel = (Elf_Rela *) ((char *) ehdr + s->sh_offset), + max = (Elf_Rela *) ((char *) rel + s->sh_size); + rel < max; + rel = (Elf_Rela *) ((char *) rel + s->sh_entsize)) + { + grub_addr_t addr; + Elf_Sym *sym; + grub_uint64_t value; + + if (seg->size < (rel->r_offset & ~3)) + return grub_error (GRUB_ERR_BAD_MODULE, + "reloc offset is out of the segment"); + + addr = (grub_addr_t) seg->addr + rel->r_offset; + sym = (Elf_Sym *) ((char *) mod->symtab + + mod->symsize * ELF_R_SYM (rel->r_info)); + + /* On the PPC the value does not have an explicit + addend, add it. */ + value = sym->st_value + rel->r_addend; + + switch (ELF_R_TYPE (rel->r_info)) + { + case R_IA64_PCREL21B: + { + grub_int64_t noff; + if (ELF_ST_TYPE (sym->st_info) == STT_FUNC) + { + struct grub_ia64_trampoline *tr = mod->trampptr; + grub_ia64_make_trampoline (tr, value); + noff = ((char *) tr - (char *) (addr & ~3)) >> 4; + mod->trampptr = tr + 1; + } + else + noff = ((char *) value - (char *) (addr & ~3)) >> 4; + + if ((noff & ~MASK19) && ((-noff) & ~MASK19)) + return grub_error (GRUB_ERR_BAD_MODULE, + "jump offset too big (%lx)", noff); + grub_ia64_add_value_to_slot_20b (addr, noff); + } + break; + case R_IA64_SEGREL64LSB: + *(grub_uint64_t *) addr += value - (grub_addr_t) seg->addr; + break; + case R_IA64_FPTR64LSB: + case R_IA64_DIR64LSB: + *(grub_uint64_t *) addr += value; + break; + case R_IA64_PCREL64LSB: + *(grub_uint64_t *) addr += value - addr; + break; + case R_IA64_GPREL64I: + grub_ia64_set_immu64 (addr, value - (grub_addr_t) mod->base); + break; + case R_IA64_GPREL22: + if ((value - (grub_addr_t) mod->base) & ~MASK20) + return grub_error (GRUB_ERR_BAD_MODULE, + "gprel offset too big (%lx)", + value - (grub_addr_t) mod->base); + grub_ia64_add_value_to_slot_21 (addr, value - (grub_addr_t) mod->base); + break; + + case R_IA64_LTOFF22X: + case R_IA64_LTOFF22: + if (ELF_ST_TYPE (sym->st_info) == STT_FUNC) + value = *(grub_uint64_t *) sym->st_value + rel->r_addend; + /* Fallthrough. */ + case R_IA64_LTOFF_FPTR22: + { + grub_uint64_t *gpptr = mod->gotptr; + *gpptr = value; + if (((grub_addr_t) gpptr - (grub_addr_t) mod->base) & ~MASK20) + return grub_error (GRUB_ERR_BAD_MODULE, + "gprel offset too big (%lx)", + (grub_addr_t) gpptr - (grub_addr_t) mod->base); + grub_ia64_add_value_to_slot_21 (addr, (grub_addr_t) gpptr - (grub_addr_t) mod->base); + mod->gotptr = gpptr + 1; + break; + } + /* We treat LTOFF22X as LTOFF22, so we can ignore LDXMOV. */ + case R_IA64_LDXMOV: + break; + default: + { + char rel_info[17]; /* log16(2^64) = 16, plus NUL. */ + + grub_snprintf (rel_info, sizeof (rel_info) - 1, "%" PRIxGRUB_UINT64_T, + ELF_R_TYPE (rel->r_info)); + return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET, + N_("relocation 0x%s is not implemented yet"), rel_info); + } + } + } + return GRUB_ERR_NONE; +} diff --git a/grub-core/kern/ia64/dl_helper.c b/grub-core/kern/ia64/dl_helper.c new file mode 100644 index 0000000..c601595 --- /dev/null +++ b/grub-core/kern/ia64/dl_helper.c @@ -0,0 +1,241 @@ +/* dl.c - arch-dependent part of loadable module support */ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2002,2004,2005,2007,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/dl.h> +#include <grub/elf.h> +#include <grub/misc.h> +#include <grub/err.h> +#include <grub/mm.h> +#include <grub/i18n.h> +#include <grub/ia64/reloc.h> + +#pragma GCC diagnostic ignored "-Wcast-align" + +#define MASK20 ((1 << 20) - 1) +#define MASK3 (~(grub_addr_t) 3) + +void +grub_ia64_set_immu64 (grub_addr_t addr, grub_uint64_t val) +{ + /* Copied from binutils. */ + grub_uint64_t *ptr = ((grub_uint64_t *) (addr & MASK3)); + grub_uint64_t t0, t1; + + t0 = grub_le_to_cpu64 (ptr[0]); + t1 = grub_le_to_cpu64 (ptr[1]); + + /* tmpl/s: bits 0.. 5 in t0 + slot 0: bits 5..45 in t0 + slot 1: bits 46..63 in t0, bits 0..22 in t1 + slot 2: bits 23..63 in t1 */ + + /* First, clear the bits that form the 64 bit constant. */ + t0 &= ~(0x3ffffLL << 46); + t1 &= ~(0x7fffffLL + | (( (0x07fLL << 13) | (0x1ffLL << 27) + | (0x01fLL << 22) | (0x001LL << 21) + | (0x001LL << 36)) << 23)); + + t0 |= ((val >> 22) & 0x03ffffLL) << 46; /* 18 lsbs of imm41 */ + t1 |= ((val >> 40) & 0x7fffffLL) << 0; /* 23 msbs of imm41 */ + t1 |= ( (((val >> 0) & 0x07f) << 13) /* imm7b */ + | (((val >> 7) & 0x1ff) << 27) /* imm9d */ + | (((val >> 16) & 0x01f) << 22) /* imm5c */ + | (((val >> 21) & 0x001) << 21) /* ic */ + | (((val >> 63) & 0x001) << 36)) << 23; /* i */ + + ptr[0] = t0; + ptr[1] = t1; +} + +void +grub_ia64_add_value_to_slot_20b (grub_addr_t addr, grub_uint32_t value) +{ + grub_uint32_t val; + switch (addr & 3) + { + case 0: + val = grub_le_to_cpu32 (grub_get_unaligned32 (((grub_uint8_t *) + (addr & MASK3) + 2))); + val = (((((val & MASK20) + value) & MASK20) << 2) + | (val & ~(MASK20 << 2))); + grub_set_unaligned32 (((grub_uint8_t *) (addr & MASK3) + 2), + grub_cpu_to_le32 (val)); + break; + case 1: + val = grub_le_to_cpu32 (grub_get_unaligned32 (((grub_uint8_t *) + (addr & MASK3) + 7))); + val = ((((((val >> 3) & MASK20) + value) & MASK20) << 3) + | (val & ~(MASK20 << 3))); + grub_set_unaligned32 (((grub_uint8_t *) (addr & MASK3) + 7), + grub_cpu_to_le32 (val)); + break; + case 2: + val = grub_le_to_cpu32 (grub_get_unaligned32 (((grub_uint8_t *) + (addr & MASK3) + 12))); + val = ((((((val >> 4) & MASK20) + value) & MASK20) << 4) + | (val & ~(MASK20 << 4))); + grub_set_unaligned32 (((grub_uint8_t *) (addr & MASK3) + 12), + grub_cpu_to_le32 (val)); + break; + } +} + +#define MASKF21 ( ((1 << 23) - 1) & ~((1 << 7) | (1 << 8)) ) + +static grub_uint32_t +add_value_to_slot_21_real (grub_uint32_t a, grub_uint32_t value) +{ + grub_uint32_t high, mid, low, c; + low = (a & 0x00007f); + mid = (a & 0x7fc000) >> 7; + high = (a & 0x003e00) << 7; + c = (low | mid | high) + value; + return (c & 0x7f) | ((c << 7) & 0x7fc000) | ((c >> 7) & 0x0003e00); //0x003e00 +} + +void +grub_ia64_add_value_to_slot_21 (grub_addr_t addr, grub_uint32_t value) +{ + grub_uint32_t val; + switch (addr & 3) + { + case 0: + val = grub_le_to_cpu32 (grub_get_unaligned32 (((grub_uint8_t *) + (addr & MASK3) + 2))); + val = ((add_value_to_slot_21_real (((val >> 2) & MASKF21), value) + & MASKF21) << 2) | (val & ~(MASKF21 << 2)); + grub_set_unaligned32 (((grub_uint8_t *) (addr & MASK3) + 2), + grub_cpu_to_le32 (val)); + break; + case 1: + val = grub_le_to_cpu32 (grub_get_unaligned32 (((grub_uint8_t *) + (addr & MASK3) + 7))); + val = ((add_value_to_slot_21_real (((val >> 3) & MASKF21), value) + & MASKF21) << 3) | (val & ~(MASKF21 << 3)); + grub_set_unaligned32 (((grub_uint8_t *) (addr & MASK3) + 7), + grub_cpu_to_le32 (val)); + break; + case 2: + val = grub_le_to_cpu32 (grub_get_unaligned32 (((grub_uint8_t *) + (addr & MASK3) + 12))); + val = ((add_value_to_slot_21_real (((val >> 4) & MASKF21), value) + & MASKF21) << 4) | (val & ~(MASKF21 << 4)); + grub_set_unaligned32 (((grub_uint8_t *) (addr & MASK3) + 12), + grub_cpu_to_le32 (val)); + break; + } +} + +static const grub_uint8_t nopm[5] = + { + /* [MLX] nop.m 0x0 */ + 0x05, 0x00, 0x00, 0x00, 0x01 + }; + +#ifdef GRUB_UTIL +static grub_uint8_t jump[0x20] = + { + /* [MMI] add r15=r15,r1;; */ + 0x0b, 0x78, 0x3c, 0x02, 0x00, 0x20, + /* ld8 r16=[r15],8 */ + 0x00, 0x41, 0x3c, 0x30, 0x28, 0xc0, + /* mov r14=r1;; */ + 0x01, 0x08, 0x00, 0x84, + /* [MIB] ld8 r1=[r15] */ + 0x11, 0x08, 0x00, 0x1e, 0x18, 0x10, + /* mov b6=r16 */ + 0x60, 0x80, 0x04, 0x80, 0x03, 0x00, + /* br.few b6;; */ + 0x60, 0x00, 0x80, 0x00 + }; +#else +static const grub_uint8_t jump[0x20] = + { + /* ld8 r16=[r15],8 */ + 0x02, 0x80, 0x20, 0x1e, 0x18, 0x14, + /* mov r14=r1;; */ + 0xe0, 0x00, 0x04, 0x00, 0x42, 0x00, + /* nop.i 0x0 */ + 0x00, 0x00, 0x04, 0x00, + /* ld8 r1=[r15] */ + 0x11, 0x08, 0x00, 0x1e, 0x18, 0x10, + /* mov b6=r16 */ + 0x60, 0x80, 0x04, 0x80, 0x03, 0x00, + /* br.few b6;; */ + 0x60, 0x00, 0x80, 0x00 + }; +#endif + +void +grub_ia64_make_trampoline (struct grub_ia64_trampoline *tr, grub_uint64_t addr) +{ + grub_memcpy (tr->nop, nopm, sizeof (tr->nop)); + tr->addr_hi[0] = ((addr & 0xc00000) >> 16); + tr->addr_hi[1] = (addr >> 24) & 0xff; + tr->addr_hi[2] = (addr >> 32) & 0xff; + tr->addr_hi[3] = (addr >> 40) & 0xff; + tr->addr_hi[4] = (addr >> 48) & 0xff; + tr->addr_hi[5] = (addr >> 56) & 0xff; + tr->e0 = 0xe0; + tr->addr_lo[0] = ((addr & 0x000f) << 4) | 0x01; + tr->addr_lo[1] = (((addr & 0x0070) >> 4) | ((addr & 0x070000) >> 11) + | ((addr & 0x200000) >> 17)); + tr->addr_lo[2] = ((addr & 0x1f80) >> 5) | ((addr & 0x180000) >> 19); + tr->addr_lo[3] = ((addr & 0xe000) >> 13) | 0x60; + grub_memcpy (tr->jump, jump, sizeof (tr->jump)); +} + +grub_err_t +grub_ia64_dl_get_tramp_got_size (const void *ehdr, grub_size_t *tramp, + grub_size_t *got) +{ + const Elf64_Ehdr *e = ehdr; + grub_size_t cntt = 0, cntg = 0; + const Elf64_Shdr *s; + unsigned i; + + for (i = 0, s = (Elf64_Shdr *) ((char *) e + grub_le_to_cpu64 (e->e_shoff)); + i < grub_le_to_cpu16 (e->e_shnum); + i++, s = (Elf64_Shdr *) ((char *) s + grub_le_to_cpu16 (e->e_shentsize))) + if (s->sh_type == grub_cpu_to_le32_compile_time (SHT_RELA)) + { + const Elf64_Rela *rel, *max; + + for (rel = (Elf64_Rela *) ((char *) e + grub_le_to_cpu64 (s->sh_offset)), + max = (const Elf64_Rela *) ((char *) rel + grub_le_to_cpu64 (s->sh_size)); + rel < max; rel = (const Elf64_Rela *) ((char *) rel + grub_le_to_cpu64 (s->sh_entsize))) + switch (ELF64_R_TYPE (grub_le_to_cpu64 (rel->r_info))) + { + case R_IA64_PCREL21B: + cntt++; + break; + case R_IA64_LTOFF_FPTR22: + case R_IA64_LTOFF22X: + case R_IA64_LTOFF22: + cntg++; + break; + } + } + *tramp = cntt * sizeof (struct grub_ia64_trampoline); + *got = cntg * sizeof (grub_uint64_t); + + return GRUB_ERR_NONE; +} + diff --git a/grub-core/kern/ia64/efi/init.c b/grub-core/kern/ia64/efi/init.c new file mode 100644 index 0000000..f196557 --- /dev/null +++ b/grub-core/kern/ia64/efi/init.c @@ -0,0 +1,80 @@ +/* init.c - initialize an ia64-based EFI system */ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2008 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/types.h> +#include <grub/misc.h> +#include <grub/mm.h> +#include <grub/time.h> +#include <grub/err.h> +#include <grub/dl.h> +#include <grub/kernel.h> +#include <grub/efi/efi.h> +#include <grub/loader.h> + +static grub_uint64_t divisor = 1; + +static grub_uint64_t +get_itc (void) +{ + grub_uint64_t ret; + asm volatile ("mov %0=ar.itc" : "=r" (ret)); + return ret; +} + +grub_uint64_t +grub_rtc_get_time_ms (void) +{ + return get_itc () / divisor; +} + +void +grub_machine_init (void) +{ + grub_efi_event_t event; + grub_uint64_t before, after; + grub_efi_uintn_t idx; + grub_efi_init (); + + efi_call_5 (grub_efi_system_table->boot_services->create_event, + GRUB_EFI_EVT_TIMER, GRUB_EFI_TPL_CALLBACK, 0, 0, &event); + + before = get_itc (); + efi_call_3 (grub_efi_system_table->boot_services->set_timer, event, + GRUB_EFI_TIMER_RELATIVE, 200000); + efi_call_3 (grub_efi_system_table->boot_services->wait_for_event, 1, + &event, &idx); + after = get_itc (); + efi_call_1 (grub_efi_system_table->boot_services->close_event, event); + divisor = (after - before + 5) / 20; + if (divisor == 0) + divisor = 800000; + grub_install_get_time_ms (grub_rtc_get_time_ms); +} + +void +grub_machine_fini (int flags) +{ + if (!(flags & GRUB_LOADER_FLAG_NORETURN)) + return; + + grub_efi_fini (); + + if (!(flags & GRUB_LOADER_FLAG_EFI_KEEP_ALLOCATED_MEMORY)) + grub_efi_memory_fini (); +} diff --git a/grub-core/kern/ia64/efi/startup.S b/grub-core/kern/ia64/efi/startup.S new file mode 100644 index 0000000..d75c6d7 --- /dev/null +++ b/grub-core/kern/ia64/efi/startup.S @@ -0,0 +1,44 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2008 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 <config.h> +#include <grub/symbol.h> +#include <grub/offsets.h> + + .text + .psr abi64 + .psr lsb + .lsb + + .global _start + .proc _start +_start: + alloc loc0=ar.pfs,2,4,0,0 + mov loc1=rp + addl loc2=@gprel(grub_efi_image_handle),gp + addl loc3=@gprel(grub_efi_system_table),gp + ;; + st8 [loc2]=in0 + st8 [loc3]=in1 + br.call.sptk.few rp=grub_main + ;; + mov ar.pfs=loc0 + mov rp=loc1 + ;; + br.ret.sptk.few rp + + .endp _start |