summaryrefslogtreecommitdiffstats
path: root/grub-core/lib/x86_64
diff options
context:
space:
mode:
Diffstat (limited to 'grub-core/lib/x86_64')
-rw-r--r--grub-core/lib/x86_64/efi/relocator.c79
-rw-r--r--grub-core/lib/x86_64/relocator_asm.S85
-rw-r--r--grub-core/lib/x86_64/setjmp.S68
-rw-r--r--grub-core/lib/x86_64/xen/relocator.S133
4 files changed, 365 insertions, 0 deletions
diff --git a/grub-core/lib/x86_64/efi/relocator.c b/grub-core/lib/x86_64/efi/relocator.c
new file mode 100644
index 0000000..7d200a1
--- /dev/null
+++ b/grub-core/lib/x86_64/efi/relocator.c
@@ -0,0 +1,79 @@
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2009 Free Software Foundation, Inc.
+ * Copyright (C) 2016 Oracle and/or its affiliates. All rights reserved.
+ *
+ * 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/err.h>
+
+#include <grub/i386/relocator.h>
+#include <grub/relocator_private.h>
+
+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_rsi;
+
+extern grub_uint8_t grub_relocator64_efi_start;
+extern grub_uint8_t grub_relocator64_efi_end;
+
+#define RELOCATOR_SIZEOF(x) (&grub_relocator##x##_end - &grub_relocator##x##_start)
+
+grub_err_t
+grub_relocator64_efi_boot (struct grub_relocator *rel,
+ struct grub_relocator64_efi_state state)
+{
+ grub_err_t err;
+ void *relst;
+ grub_relocator_chunk_t ch;
+
+ /*
+ * 64-bit relocator code may live above 4 GiB quite well.
+ * However, I do not want ask for problems. Just in case.
+ */
+ err = grub_relocator_alloc_chunk_align_safe (rel, &ch, 0, 0x100000000,
+ RELOCATOR_SIZEOF (64_efi), 16,
+ GRUB_RELOCATOR_PREFERENCE_NONE, 1);
+ if (err)
+ return err;
+
+ /* Do not touch %rsp! It points to EFI created stack. */
+ 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_rsi = state.rsi;
+
+ grub_memmove (get_virtual_current_address (ch), &grub_relocator64_efi_start,
+ RELOCATOR_SIZEOF (64_efi));
+
+ err = grub_relocator_prepare_relocs (rel, get_physical_target_address (ch),
+ &relst, NULL);
+ if (err)
+ return err;
+
+ ((void (*) (void)) relst) ();
+
+ /* Not reached. */
+ return GRUB_ERR_NONE;
+}
diff --git a/grub-core/lib/x86_64/relocator_asm.S b/grub-core/lib/x86_64/relocator_asm.S
new file mode 100644
index 0000000..2ab6d8c
--- /dev/null
+++ b/grub-core/lib/x86_64/relocator_asm.S
@@ -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)
diff --git a/grub-core/lib/x86_64/setjmp.S b/grub-core/lib/x86_64/setjmp.S
new file mode 100644
index 0000000..6b151bc
--- /dev/null
+++ b/grub-core/lib/x86_64/setjmp.S
@@ -0,0 +1,68 @@
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2003,2007 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/dl.h>
+
+ .file "setjmp.S"
+
+GRUB_MOD_LICENSE "GPLv3+"
+
+ .text
+
+/*
+ * jmp_buf:
+ * rbx rsp rbp r12 r13 r14 r15 rip
+ * 0 8 16 24 32 40 48 56
+ */
+
+/*
+ * int grub_setjmp (grub_jmp_buf env)
+ */
+FUNCTION(grub_setjmp)
+ pop %rsi /* Return address, and adjust the stack */
+ xorq %rax, %rax
+ movq %rbx, 0(%rdi) /* RBX */
+ movq %rsp, 8(%rdi) /* RSP */
+ push %rsi
+ movq %rbp, 16(%rdi) /* RBP */
+ movq %r12, 24(%rdi) /* R12 */
+ movq %r13, 32(%rdi) /* R13 */
+ movq %r14, 40(%rdi) /* R14 */
+ movq %r15, 48(%rdi) /* R15 */
+ movq %rsi, 56(%rdi) /* RSI */
+ ret
+
+/*
+ * int grub_longjmp (grub_jmp_buf env, int val)
+ */
+FUNCTION(grub_longjmp)
+ movl %esi, %eax
+ orl %eax, %eax
+ jnz 1f
+ incl %eax
+1:
+
+ movq (%rdi), %rbx
+ movq 8(%rdi), %rsp
+ movq 16(%rdi), %rbp
+ movq 24(%rdi), %r12
+ movq 32(%rdi), %r13
+ movq 40(%rdi), %r14
+ movq 48(%rdi), %r15
+ jmp *56(%rdi)
diff --git a/grub-core/lib/x86_64/xen/relocator.S b/grub-core/lib/x86_64/xen/relocator.S
new file mode 100644
index 0000000..f5364ed
--- /dev/null
+++ b/grub-core/lib/x86_64/xen/relocator.S
@@ -0,0 +1,133 @@
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 2013 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/x86_64/memory.h>
+#include <grub/x86_64/types.h>
+#include <grub/symbol.h>
+#include <grub/xen.h>
+
+/* Macro to load an imm64 value stored by the C-part into %rax: */
+#define MOV_IMM64_RAX(var) .byte 0x48, 0xb8; VARIABLE(var); .quad 0
+
+ .p2align 4 /* force 16-byte alignment */
+
+VARIABLE(grub_relocator_xen_remap_start)
+LOCAL(base):
+ /* Remap the remapper to it's new address. */
+ MOV_IMM64_RAX(grub_relocator_xen_remapper_virt)
+
+ movq %rax, %rdi /* %rdi: new virtual address of remapper */
+ movq %rax, %rbx /* Remember new virtual address */
+
+ MOV_IMM64_RAX(grub_relocator_xen_remapper_map)
+
+ movq %rax, %rsi /* %rsi: page table entry */
+
+ movq $UVMF_INVLPG, %rdx /* %rdx: flags (inv. single entry) */
+ movq $__HYPERVISOR_update_va_mapping, %rax
+ syscall /* Do the remap operation */
+
+ addq $(LOCAL(cont) - LOCAL(base)), %rbx
+
+ jmp *%rbx /* Continue with new virtual address */
+
+LOCAL(cont):
+ /* Modify mappings of new page tables to be read-only. */
+ MOV_IMM64_RAX(grub_relocator_xen_mfn_list)
+
+ movq %rax, %rbx /* %rbx is the base of the p2m list */
+ leaq EXT_C(grub_relocator_xen_paging_areas) (%rip), %r8
+
+1:
+ movq 0(%r8), %r12 /* Get start pfn of the current area */
+ movq GRUB_TARGET_SIZEOF_LONG(%r8), %rcx /* Get # of pg tables */
+ testq %rcx, %rcx /* 0 -> last area reached */
+ jz 3f
+2:
+ movq %r12, %rdi
+ shlq $PAGE_SHIFT, %rdi /* virtual address (1:1 mapping) */
+ movq (%rbx, %r12, 8), %rsi /* mfn */
+ shlq $PAGE_SHIFT, %rsi
+ orq $(GRUB_PAGE_PRESENT | GRUB_PAGE_USER), %rsi /* Build pte */
+ movq $UVMF_INVLPG, %rdx
+ movq %rcx, %r9 /* %rcx clobbered by hypercall */
+ movq $__HYPERVISOR_update_va_mapping, %rax
+ syscall
+
+ movq %r9, %rcx
+ incq %r12 /* next pfn */
+
+ loop 2b
+
+ addq $(2 * GRUB_TARGET_SIZEOF_LONG), %r8 /* next pg table area */
+ jmp 1b
+
+3:
+ /* Switch page tables: pin new L4 pt, load cr3, unpin old L4. */
+ leaq EXT_C(grub_relocator_xen_mmu_op) (%rip), %rdi
+ movq $3, %rsi /* 3 mmu ops */
+ movq $0, %rdx /* pdone (not used) */
+ movq $DOMID_SELF, %r10
+ movq $__HYPERVISOR_mmuext_op, %rax
+ syscall
+
+ /* Continue in virtual kernel mapping. */
+ MOV_IMM64_RAX(grub_relocator_xen_remap_continue)
+
+ jmp *%rax
+
+VARIABLE(grub_relocator_xen_paging_areas)
+ /* array of start, size pairs, size 0 is end marker */
+ .quad 0, 0, 0, 0, 0, 0, 0, 0
+
+VARIABLE(grub_relocator_xen_mmu_op)
+ .space 256
+
+VARIABLE(grub_relocator_xen_remap_end)
+
+
+VARIABLE(grub_relocator_xen_start)
+ /* Unmap old remapper area. */
+ MOV_IMM64_RAX(grub_relocator_xen_remapper_virt2)
+
+ movq %rax, %rdi
+
+ xorq %rax, %rax /* Invalid pte */
+ movq %rax, %rsi
+
+ movq $UVMF_INVLPG, %rdx
+ movq $__HYPERVISOR_update_va_mapping, %rax
+ syscall
+
+ /* Prepare registers for starting kernel. */
+ MOV_IMM64_RAX(grub_relocator_xen_stack)
+
+ movq %rax, %rsp
+
+ MOV_IMM64_RAX(grub_relocator_xen_start_info)
+
+ movq %rax, %rsi
+
+ cld
+
+ MOV_IMM64_RAX(grub_relocator_xen_entry_point)
+
+ /* Now start the new kernel. */
+ jmp *%rax
+
+VARIABLE(grub_relocator_xen_end)