From patchwork Tue Mar 15 15:25:58 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Kiper X-Patchwork-Id: 8589631 Return-Path: X-Original-To: patchwork-xen-devel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id D421A9F6E1 for ; Tue, 15 Mar 2016 15:29:41 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 678A3202C8 for ; Tue, 15 Mar 2016 15:29:40 +0000 (UTC) Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 9550B2022D for ; Tue, 15 Mar 2016 15:29:38 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1afqs3-00009D-Ua; Tue, 15 Mar 2016 15:26:55 +0000 Received: from mail6.bemta3.messagelabs.com ([195.245.230.39]) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1afqs2-00008v-5O for xen-devel@lists.xenproject.org; Tue, 15 Mar 2016 15:26:54 +0000 Received: from [85.158.137.68] by server-16.bemta-3.messagelabs.com id ED/F5-02994-DB928E65; Tue, 15 Mar 2016 15:26:53 +0000 X-Env-Sender: daniel.kiper@oracle.com X-Msg-Ref: server-16.tower-31.messagelabs.com!1458055610!21757336!1 X-Originating-IP: [156.151.31.81] X-SpamReason: No, hits=0.0 required=7.0 tests=sa_preprocessor: VHJ1c3RlZCBJUDogMTU2LjE1MS4zMS44MSA9PiAyODgzMzk=\n X-StarScan-Received: X-StarScan-Version: 8.11; banners=-,-,- X-VirusChecked: Checked Received: (qmail 15626 invoked from network); 15 Mar 2016 15:26:52 -0000 Received: from userp1040.oracle.com (HELO userp1040.oracle.com) (156.151.31.81) by server-16.tower-31.messagelabs.com with DHE-RSA-AES256-GCM-SHA384 encrypted SMTP; 15 Mar 2016 15:26:52 -0000 Received: from aserv0022.oracle.com (aserv0022.oracle.com [141.146.126.234]) by userp1040.oracle.com (Sentrion-MTA-4.3.2/Sentrion-MTA-4.3.2) with ESMTP id u2FFQSvU026705 (version=TLSv1 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Tue, 15 Mar 2016 15:26:30 GMT Received: from aserv0121.oracle.com (aserv0121.oracle.com [141.146.126.235]) by aserv0022.oracle.com (8.13.8/8.13.8) with ESMTP id u2FFQSFY000836 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=FAIL); Tue, 15 Mar 2016 15:26:28 GMT Received: from abhmp0019.oracle.com (abhmp0019.oracle.com [141.146.116.25]) by aserv0121.oracle.com (8.13.8/8.13.8) with ESMTP id u2FFQRSS013412; Tue, 15 Mar 2016 15:26:28 GMT Received: from olila.local.net-space.pl (/10.175.228.51) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Tue, 15 Mar 2016 08:26:27 -0700 From: Daniel Kiper To: xen-devel@lists.xenproject.org, grub-devel@gnu.org Date: Tue, 15 Mar 2016 16:25:58 +0100 Message-Id: <1458055562-24950-2-git-send-email-daniel.kiper@oracle.com> X-Mailer: git-send-email 1.7.10.4 In-Reply-To: <1458055562-24950-1-git-send-email-daniel.kiper@oracle.com> References: <1458055562-24950-1-git-send-email-daniel.kiper@oracle.com> X-Source-IP: aserv0022.oracle.com [141.146.126.234] Cc: jgross@suse.com, eric.snowberg@oracle.com, arvidjaar@gmail.com, andrew.cooper3@citrix.com, stefano.stabellini@eu.citrix.com, cardoe@cardoe.com, pgnet.dev@gmail.com, roy.franz@linaro.org, ning.sun@intel.com, david.vrabel@citrix.com, jbeulich@suse.com, phcoder@gmail.com, qiaowei.ren@intel.com, richard.l.maliszewski@intel.com, gang.wei@intel.com, fu.wei@linaro.org, seth.goldberg@oracle.com Subject: [Xen-devel] [GRUB2 PATCH v4 1/4] i386/relocator: Add grub_relocator64_efi relocator X-BeenThere: xen-devel@lists.xen.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: xen-devel-bounces@lists.xen.org Sender: "Xen-devel" X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Add grub_relocator64_efi relocator. It will be used on EFI 64-bit platforms when multiboot2 compatible image requests MULTIBOOT_TAG_TYPE_EFI_BS. Relocator will set lower parts of %rax and %rbx accordingly to multiboot2 specification. On the other hand processor mode, just before jumping into loaded image, will be set accordingly to Unified Extensible Firmware Interface Specification, Version 2.4 Errata B, section 2.3.4, x64 Platforms, boot services. This way loaded image will be able to use EFI boot services without any issues. Signed-off-by: Daniel Kiper --- v4 - suggestions/fixes: - move EFI platform specific C code to separate file (suggested by Vladimir 'phcoder' Serbinenko), - add some comments (suggested by Vladimir 'phcoder' Serbinenko). v3 - suggestions/fixes: - reuse grub-core/lib/i386/relocator64.S code instead of creating separate assembly file (suggested by Vladimir 'phcoder' Serbinenko), - grub_multiboot_boot() cleanup (suggested by Vladimir 'phcoder' Serbinenko), - reuse multiboot_header_tag_entry_address struct instead of creating new one for EFI 64-bit entry point (suggested by Vladimir 'phcoder' Serbinenko). --- grub-core/Makefile.core.def | 1 + grub-core/lib/i386/relocator64.S | 11 +++++ grub-core/lib/x86_64/efi/relocator.c | 76 ++++++++++++++++++++++++++++++++++ grub-core/loader/multiboot.c | 51 ++++++++++++++++++++--- grub-core/loader/multiboot_mbi2.c | 19 +++++++-- include/grub/i386/multiboot.h | 11 +++++ include/grub/i386/relocator.h | 21 ++++++++++ include/multiboot2.h | 1 + 8 files changed, 182 insertions(+), 9 deletions(-) create mode 100644 grub-core/lib/x86_64/efi/relocator.c diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def index 58b4208..21ad0dd 100644 --- a/grub-core/Makefile.core.def +++ b/grub-core/Makefile.core.def @@ -1531,6 +1531,7 @@ module = { i386_xen = lib/i386/xen/relocator.S; x86_64_xen = lib/x86_64/xen/relocator.S; xen = lib/i386/relocator_common_c.c; + x86_64_efi = lib/x86_64/efi/relocator.c; extra_dist = lib/i386/relocator_common.S; extra_dist = kern/powerpc/cache_flush.S; diff --git a/grub-core/lib/i386/relocator64.S b/grub-core/lib/i386/relocator64.S index e4648d8..75725cf 100644 --- a/grub-core/lib/i386/relocator64.S +++ b/grub-core/lib/i386/relocator64.S @@ -73,6 +73,14 @@ VARIABLE(grub_relocator64_rsp) movq %rax, %rsp + /* + * Here is grub_relocator64_efi_start() entry point. + * Following code is shared between grub_relocator64_efi_start() + * and grub_relocator64_start(). + * + * Think twice before changing anything below!!! + */ +VARIABLE(grub_relocator64_efi_start) /* mov imm64, %rax */ .byte 0x48 .byte 0xb8 @@ -120,6 +128,9 @@ LOCAL(jump_addr): VARIABLE(grub_relocator64_rip) .quad 0 + /* Here grub_relocator64_efi_start() ends. Ufff... */ +VARIABLE(grub_relocator64_efi_end) + #ifndef __x86_64__ .p2align 4 LOCAL(gdt): 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..c93d061 --- /dev/null +++ b/grub-core/lib/x86_64/efi/relocator.c @@ -0,0 +1,76 @@ +/* + * 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 . + */ + +#include +#include + +#include +#include + +#include +#include + +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; + + err = grub_relocator_alloc_chunk_align (rel, &ch, 0, + 0x40000000 - RELOCATOR_SIZEOF (64_efi), + 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/loader/multiboot.c b/grub-core/loader/multiboot.c index 73aa0aa..18038fd 100644 --- a/grub-core/loader/multiboot.c +++ b/grub-core/loader/multiboot.c @@ -118,6 +118,48 @@ grub_multiboot_set_video_mode (void) return err; } +#ifdef GRUB_MACHINE_EFI +#ifdef __x86_64__ +#define grub_relocator_efi_boot grub_relocator64_efi_boot +#define grub_relocator_efi_state grub_relocator64_efi_state +#endif +#endif + +#ifdef grub_relocator_efi_boot +static void +efi_boot (struct grub_relocator *rel, + grub_uint32_t target) +{ + struct grub_relocator_efi_state state_efi = MULTIBOOT_EFI_INITIAL_STATE; + + state_efi.MULTIBOOT_EFI_ENTRY_REGISTER = grub_multiboot_payload_eip; + state_efi.MULTIBOOT_EFI_MBI_REGISTER = target; + + grub_relocator_efi_boot (rel, state_efi); +} +#else +#define grub_efi_is_finished 1 +static void +efi_boot (struct grub_relocator *rel __attribute__ ((unused)), + grub_uint32_t target __attribute__ ((unused))) +{ +} +#endif + +#if defined (__i386__) || defined (__x86_64__) +static void +normal_boot (struct grub_relocator *rel, struct grub_relocator32_state state) +{ + grub_relocator32_boot (rel, state, 0); +} +#else +static void +normal_boot (struct grub_relocator *rel, struct grub_relocator32_state state) +{ + grub_relocator32_boot (rel, state); +} +#endif + static grub_err_t grub_multiboot_boot (void) { @@ -131,11 +173,10 @@ grub_multiboot_boot (void) if (err) return err; -#if defined (__i386__) || defined (__x86_64__) - grub_relocator32_boot (grub_multiboot_relocator, state, 0); -#else - grub_relocator32_boot (grub_multiboot_relocator, state); -#endif + if (grub_efi_is_finished) + normal_boot (grub_multiboot_relocator, state); + else + efi_boot (grub_multiboot_relocator, state.MULTIBOOT_MBI_REGISTER); /* Not reached. */ return GRUB_ERR_NONE; diff --git a/grub-core/loader/multiboot_mbi2.c b/grub-core/loader/multiboot_mbi2.c index f147d67..a3dca90 100644 --- a/grub-core/loader/multiboot_mbi2.c +++ b/grub-core/loader/multiboot_mbi2.c @@ -107,8 +107,8 @@ grub_multiboot_load (grub_file_t file, const char *filename) grub_err_t err; struct multiboot_header_tag *tag; struct multiboot_header_tag_address *addr_tag = NULL; - int entry_specified = 0; - grub_addr_t entry = 0; + int entry_specified = 0, efi_entry_specified = 0; + grub_addr_t entry = 0, efi_entry = 0; grub_uint32_t console_required = 0; struct multiboot_header_tag_framebuffer *fbtag = NULL; int accepted_consoles = GRUB_MULTIBOOT_CONSOLE_EGA_TEXT; @@ -192,6 +192,13 @@ grub_multiboot_load (grub_file_t file, const char *filename) entry = ((struct multiboot_header_tag_entry_address *) tag)->entry_addr; break; + case MULTIBOOT_HEADER_TAG_ENTRY_ADDRESS_EFI64: +#if defined (GRUB_MACHINE_EFI) && defined (__x86_64__) + efi_entry_specified = 1; + efi_entry = ((struct multiboot_header_tag_entry_address *) tag)->entry_addr; +#endif + break; + case MULTIBOOT_HEADER_TAG_CONSOLE_FLAGS: if (!(((struct multiboot_header_tag_console_flags *) tag)->console_flags & MULTIBOOT_CONSOLE_FLAGS_EGA_TEXT_SUPPORTED)) @@ -211,7 +218,9 @@ grub_multiboot_load (grub_file_t file, const char *filename) break; case MULTIBOOT_HEADER_TAG_EFI_BS: +#ifdef GRUB_MACHINE_EFI keep_bs = 1; +#endif break; default: @@ -224,7 +233,7 @@ grub_multiboot_load (grub_file_t file, const char *filename) break; } - if (addr_tag && !entry_specified) + if (addr_tag && !entry_specified && !(keep_bs && efi_entry_specified)) { grub_free (buffer); return grub_error (GRUB_ERR_UNKNOWN_OS, @@ -287,7 +296,9 @@ grub_multiboot_load (grub_file_t file, const char *filename) } } - if (entry_specified) + if (keep_bs && efi_entry_specified) + grub_multiboot_payload_eip = efi_entry; + else if (entry_specified) grub_multiboot_payload_eip = entry; if (fbtag) diff --git a/include/grub/i386/multiboot.h b/include/grub/i386/multiboot.h index a1b9488..807a1de 100644 --- a/include/grub/i386/multiboot.h +++ b/include/grub/i386/multiboot.h @@ -30,6 +30,17 @@ #define MULTIBOOT_MBI_REGISTER ebx #define MULTIBOOT_ARCHITECTURE_CURRENT MULTIBOOT_ARCHITECTURE_I386 +#ifdef GRUB_MACHINE_EFI +#ifdef __x86_64__ +#define MULTIBOOT_EFI_INITIAL_STATE { .rax = MULTIBOOT_BOOTLOADER_MAGIC, \ + .rcx = 0, \ + .rdx = 0, \ + } +#define MULTIBOOT_EFI_ENTRY_REGISTER rip +#define MULTIBOOT_EFI_MBI_REGISTER rbx +#endif +#endif + #define MULTIBOOT_ELF32_MACHINE EM_386 #define MULTIBOOT_ELF64_MACHINE EM_X86_64 diff --git a/include/grub/i386/relocator.h b/include/grub/i386/relocator.h index 5f89a7e..2a56c3b 100644 --- a/include/grub/i386/relocator.h +++ b/include/grub/i386/relocator.h @@ -65,6 +65,20 @@ struct grub_relocator64_state grub_addr_t cr3; }; +#ifdef GRUB_MACHINE_EFI +#ifdef __x86_64__ +struct grub_relocator64_efi_state +{ + grub_uint64_t rax; + grub_uint64_t rbx; + grub_uint64_t rcx; + grub_uint64_t rdx; + grub_uint64_t rip; + grub_uint64_t rsi; +}; +#endif +#endif + grub_err_t grub_relocator16_boot (struct grub_relocator *rel, struct grub_relocator16_state state); @@ -76,4 +90,11 @@ 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); +#ifdef GRUB_MACHINE_EFI +#ifdef __x86_64__ +grub_err_t grub_relocator64_efi_boot (struct grub_relocator *rel, + struct grub_relocator64_efi_state state); +#endif +#endif + #endif /* ! GRUB_RELOCATOR_CPU_HEADER */ diff --git a/include/multiboot2.h b/include/multiboot2.h index 8b8c156..46e7b71 100644 --- a/include/multiboot2.h +++ b/include/multiboot2.h @@ -69,6 +69,7 @@ #define MULTIBOOT_HEADER_TAG_FRAMEBUFFER 5 #define MULTIBOOT_HEADER_TAG_MODULE_ALIGN 6 #define MULTIBOOT_HEADER_TAG_EFI_BS 7 +#define MULTIBOOT_HEADER_TAG_ENTRY_ADDRESS_EFI64 9 #define MULTIBOOT_ARCHITECTURE_I386 0 #define MULTIBOOT_ARCHITECTURE_MIPS32 4