From patchwork Sat Jul 8 21:53:17 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Kiper X-Patchwork-Id: 9831557 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 1F627602BD for ; Sat, 8 Jul 2017 21:56:40 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 117A527C2D for ; Sat, 8 Jul 2017 21:56:40 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 062B227F91; Sat, 8 Jul 2017 21:56:40 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-4.2 required=2.0 tests=BAYES_00, RCVD_IN_DNSWL_MED, UNPARSEABLE_RELAY autolearn=ham version=3.3.1 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.wl.linuxfoundation.org (Postfix) with ESMTPS id 4A32627C2D for ; Sat, 8 Jul 2017 21:56:39 +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 1dTxfh-00048s-M6; Sat, 08 Jul 2017 21:53:49 +0000 Received: from mail6.bemta6.messagelabs.com ([193.109.254.103]) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dTxfg-00048W-So for xen-devel@lists.xenproject.org; Sat, 08 Jul 2017 21:53:49 +0000 Received: from [85.158.143.35] by server-9.bemta-6.messagelabs.com id 84/BA-03406-C6451695; Sat, 08 Jul 2017 21:53:48 +0000 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFupgkeJIrShJLcpLzFFi42LpnVTnqpsdkhh p8OGWssX3LZOZHBg9Dn+4whLAGMWamZeUX5HAmnF+smDB4fCK+91nWBoYHxp2MXJxCAlMYpKY ++8pC4Tzh1GiqXcFE4SzgVFi5tITrBDOBEaJ03eXAzmcHGwCOhIXvzxkB7FFBJQk7q2azARiM wt4SLz/MgssLixgJfGt7TAjiM0ioCpx7tFPsBpeAXeJy99+gcUlBBQlup9NYAOxOYF6G9p/gd lCQDWPn55hhqgxlmh/e5FtAiPfAkaGVYwaxalFZalFukZmeklFmekZJbmJmTm6hgZmermpxcW J6ak5iUnFesn5uZsYgaHCAAQ7GM8sCDzEKMnBpCTKK9abECnEl5SfUpmRWJwRX1Sak1p8iFGG g0NJgpcnODFSSLAoNT21Ii0zBxi0MGkJDh4lEV6rr0CtvMUFibnFmekQqVOMuhyvJvz/xiTEk peflyolzqsBMkMApCijNA9uBCyCLjHKSgnzMgIdJcRTkFqUm1mCKv+KUZyDUUmYtzUIaApPZl 4J3KZXQEcwAR3BVgd2REkiQkqqgfG2yIvqN/Es0jefhgXZXy1JkJ+2U7Hkvcm2tyabA0pVog9 a261b5risS85v+gzrszeP5nx5IPVy08vs5Uv/mTfGqbBNWG2sx/ynJb90RqPXevHPR/xEbe6u UpQP6Jlc37XhF+cS3pSZ+et2LZgq+uPR7/KM3pDtmhtfrNLhOPJ4r7HZRxUbXxclluKMREMt5 qLiRABLyqICmwIAAA== X-Env-Sender: daniel.kiper@oracle.com X-Msg-Ref: server-4.tower-21.messagelabs.com!1499550826!66136846!1 X-Originating-IP: [141.146.126.69] X-SpamReason: No, hits=0.0 required=7.0 tests=sa_preprocessor: VHJ1c3RlZCBJUDogMTQxLjE0Ni4xMjYuNjkgPT4gMjc3MjE4\n X-StarScan-Received: X-StarScan-Version: 9.4.25; banners=-,-,- X-VirusChecked: Checked Received: (qmail 56393 invoked from network); 8 Jul 2017 21:53:47 -0000 Received: from aserp1040.oracle.com (HELO aserp1040.oracle.com) (141.146.126.69) by server-4.tower-21.messagelabs.com with DHE-RSA-AES256-GCM-SHA384 encrypted SMTP; 8 Jul 2017 21:53:47 -0000 Received: from aserv0022.oracle.com (aserv0022.oracle.com [141.146.126.234]) by aserp1040.oracle.com (Sentrion-MTA-4.3.2/Sentrion-MTA-4.3.2) with ESMTP id v68LriXR014895 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Sat, 8 Jul 2017 21:53:44 GMT Received: from aserv0122.oracle.com (aserv0122.oracle.com [141.146.126.236]) by aserv0022.oracle.com (8.14.4/8.14.4) with ESMTP id v68LrhwQ019486 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Sat, 8 Jul 2017 21:53:43 GMT Received: from abhmp0006.oracle.com (abhmp0006.oracle.com [141.146.116.12]) by aserv0122.oracle.com (8.14.4/8.14.4) with ESMTP id v68LrglR019181; Sat, 8 Jul 2017 21:53:43 GMT Received: from olila.local.net-space.pl (/10.175.230.11) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Sat, 08 Jul 2017 14:53:42 -0700 From: Daniel Kiper To: xen-devel@lists.xenproject.org Date: Sat, 8 Jul 2017 23:53:17 +0200 Message-Id: <1499550803-25664-3-git-send-email-daniel.kiper@oracle.com> X-Mailer: git-send-email 1.7.10.4 In-Reply-To: <1499550803-25664-1-git-send-email-daniel.kiper@oracle.com> References: <1499550803-25664-1-git-send-email-daniel.kiper@oracle.com> X-Source-IP: aserv0022.oracle.com [141.146.126.234] Cc: andrew.cooper3@citrix.com, jbeulich@suse.com Subject: [Xen-devel] [PATCH RFC 2/7] xen/x86: Manually build PE header 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-Virus-Scanned: ClamAV using ClamSMTP This is the first step to get: - one binary which can be loaded by the EFI loader, Multiboot and Multiboot2 protocols, - if we wish, in the future we can drop xen/xen.gz and build xen.efi only, - crash dumps generated by the xen.efi loaded from the EFI loader can be analyzed by crash tool, - simpler code, - simpler build, - Xen build will no longer depend on ld i386pep support. Signed-off-by: Daniel Kiper --- xen/arch/x86/Rules.mk | 2 + xen/arch/x86/boot/head.S | 145 ++++++++++++++++++++++++++++++++++++++++++++++ xen/arch/x86/xen.lds.S | 16 ++++- 3 files changed, 162 insertions(+), 1 deletion(-) diff --git a/xen/arch/x86/Rules.mk b/xen/arch/x86/Rules.mk index 568657e..b501c88 100644 --- a/xen/arch/x86/Rules.mk +++ b/xen/arch/x86/Rules.mk @@ -7,6 +7,8 @@ CFLAGS += -I$(BASEDIR)/include CFLAGS += -I$(BASEDIR)/include/asm-x86/mach-generic CFLAGS += -I$(BASEDIR)/include/asm-x86/mach-default CFLAGS += -DXEN_IMG_OFFSET=$(XEN_IMG_OFFSET) +CFLAGS += -DXEN_LOAD_ALIGN=XEN_IMG_OFFSET +CFLAGS += -DXEN_FILE_ALIGN=PAGE_SIZE CFLAGS += '-D__OBJECT_LABEL__=$(subst /,$$,$(subst -,_,$(subst $(BASEDIR)/,,$(CURDIR))/$@))' # Prevent floating-point variables from creeping into Xen. diff --git a/xen/arch/x86/boot/head.S b/xen/arch/x86/boot/head.S index fd6fc33..28bbc04 100644 --- a/xen/arch/x86/boot/head.S +++ b/xen/arch/x86/boot/head.S @@ -1,3 +1,4 @@ +#include #include #include #include @@ -44,6 +45,150 @@ .Lmb2ht_init_end\@: .endm + .section .efi.pe.header, "a", @progbits + +ENTRY(efi_pe_head) + /* + * Legacy EXE header. + * + * Most of it is copied from binutils package, version 2.28, + * include/coff/pe.h:struct external_PEI_filehdr and + * bfd/peXXigen.c:_bfd_XXi_only_swap_filehdr_out(). + * + * Page is equal 512 bytes here. + * Paragraph is equal 16 bytes here. + */ + .short 0x5a4d /* EXE magic number. */ + .short 0x90 /* Bytes on last page of file. */ + .short 0x3 /* Pages in file. */ + .short 0 /* Relocations. */ + .short 0x4 /* Size of header in paragraphs. */ + .short 0 /* Minimum extra paragraphs needed. */ + .short 0xffff /* Maximum extra paragraphs needed. */ + .short 0 /* Initial (relative) SS value. */ + .short 0xb8 /* Initial SP value. */ + .short 0 /* Checksum. */ + .short 0 /* Initial IP value. */ + .short 0 /* Initial (relative) CS value. */ + .short 0x40 /* File address of relocation table. */ + .short 0 /* Overlay number. */ + .fill 4, 2, 0 /* Reserved words. */ + .short 0 /* OEM identifier. */ + .short 0 /* OEM information. */ + .fill 10, 2, 0 /* Reserved words. */ + .long pe_header - efi_pe_head /* File address of the PE header. */ + + /* + * DOS message. + * + * It is copied from binutils package, version 2.28, + * include/coff/pe.h:struct external_PEI_filehdr and + * bfd/peXXigen.c:_bfd_XXi_only_swap_filehdr_out(). + */ + .long 0x0eba1f0e + .long 0xcd09b400 + .long 0x4c01b821 + .long 0x685421cd + .long 0x70207369 + .long 0x72676f72 + .long 0x63206d61 + .long 0x6f6e6e61 + .long 0x65622074 + .long 0x6e757220 + .long 0x206e6920 + .long 0x20534f44 + .long 0x65646f6d + .long 0x0a0d0d2e + .long 0x24 + .long 0 + + /* + * PE/COFF header. + * + * The PE/COFF format is defined by Microsoft, and is available from + * http://www.microsoft.com/whdc/system/platform/firmware/PECOFF.mspx + * + * Some ideas are taken from Linux kernel and Xen ARM64. + */ + +pe_header: + .ascii "PE\0\0" /* PE signature. */ + .short 0x8664 /* Machine: IMAGE_FILE_MACHINE_AMD64. */ + .short 1 /* NumberOfSections. */ + .long XEN_COMPILE_POSIX_TIME /* TimeDateStamp. */ + .long 0 /* PointerToSymbolTable. */ + .long 0 /* NumberOfSymbols. */ + .short section_table - optional_header /* SizeOfOptionalHeader. */ + .short 0x226 /* Characteristics: + * IMAGE_FILE_EXECUTABLE_IMAGE | + * IMAGE_FILE_LARGE_ADDRESS_AWARE | + * IMAGE_FILE_DEBUG_STRIPPED | + * IMAGE_FILE_LINE_NUMS_STRIPPED + */ + +optional_header: + .short 0x20b /* PE format: PE32+ */ + .byte 0x02 /* MajorLinkerVersion. */ + .byte 0x14 /* MinorLinkerVersion. */ + .long __2M_rwdata_end - efi_pe_head_end /* SizeOfCode. */ + .long 0 /* SizeOfInitializedData. */ + .long 0 /* SizeOfUninitializedData. */ + .long sym_offs(efi_start) /* AddressOfEntryPoint. */ + .long sym_offs(start) /* BaseOfCode. */ + .quad sym_offs(__image_base__) /* ImageBase. */ + .long XEN_LOAD_ALIGN /* SectionAlignment. */ + .long XEN_FILE_ALIGN /* FileAlignment. */ + .short 2 /* MajorOperatingSystemVersion. */ + .short 0 /* MinorOperatingSystemVersion. */ + .short XEN_VERSION /* MajorImageVersion. */ + .short XEN_SUBVERSION /* MinorImageVersion. */ + .short 2 /* MajorSubsystemVersion. */ + .short 0 /* MinorSubsystemVersion. */ + .long 0 /* Win32VersionValue. */ + .long __pe_SizeOfImage /* SizeOfImage. */ + .long efi_pe_head_end - efi_pe_head /* SizeOfHeaders. */ + .long 0 /* CheckSum. */ + .short 0xa /* Subsystem: EFI application. */ + .short 0 /* DllCharacteristics. */ + .quad 0 /* SizeOfStackReserve. */ + .quad 0 /* SizeOfStackCommit. */ + .quad 0 /* SizeOfHeapReserve. */ + .quad 0 /* SizeOfHeapCommit. */ + .long 0 /* LoaderFlags. */ + .long 0x6 /* NumberOfRvaAndSizes. */ + + /* Data Directories. */ + .quad 0 /* Export Table. */ + .quad 0 /* Import Table. */ + .quad 0 /* Resource Table. */ + .quad 0 /* Exception Table. */ + .quad 0 /* Certificate Table. */ + .quad 0 /* Base Relocation Table. */ + +section_table: + .ascii ".text\0\0\0" /* Name. */ + .long __2M_rwdata_end - efi_pe_head_end /* VirtualSize. */ + .long sym_offs(start) /* VirtualAddress. */ + .long __bss_start - efi_pe_head_end /* SizeOfRawData. */ + .long efi_pe_head_end - efi_pe_head /* PointerToRawData. */ + .long 0 /* PointerToRelocations. */ + .long 0 /* PointerToLinenumbers. */ + .short 0 /* NumberOfRelocations. */ + .short 0 /* NumberOfLinenumbers. */ + .long 0xe0500020 /* Characteristics: + * IMAGE_SCN_CNT_CODE | + * IMAGE_SCN_ALIGN_16BYTES | + * IMAGE_SCN_MEM_EXECUTE | + * IMAGE_SCN_MEM_READ | + * IMAGE_SCN_MEM_WRITE + */ + + .align XEN_FILE_ALIGN +ENTRY(efi_pe_head_end) + + .text + .code32 + ENTRY(start) jmp __start diff --git a/xen/arch/x86/xen.lds.S b/xen/arch/x86/xen.lds.S index 8289a1b..3c115b9 100644 --- a/xen/arch/x86/xen.lds.S +++ b/xen/arch/x86/xen.lds.S @@ -54,7 +54,12 @@ SECTIONS __2M_text_start = .; /* Start of 2M superpages, mapped RX. */ #endif - . = __XEN_VIRT_START + XEN_IMG_OFFSET; + . = __XEN_VIRT_START + XEN_IMG_OFFSET - efi_pe_head_end + efi_pe_head; + + .efi.pe.header : { + *(.efi.pe.header) + } :NONE + _start = .; .text : { _stext = .; /* Text and read-only data */ @@ -234,6 +239,8 @@ SECTIONS *(.data.rel) *(.data.rel.*) CONSTRUCTORS + /* PE file must end at XEN_FILE_ALIGN boundary. */ + . = ALIGN(XEN_FILE_ALIGN); } :text .bss : { /* BSS */ @@ -259,6 +266,8 @@ SECTIONS #endif __2M_rwdata_end = .; + __pe_SizeOfImage = ALIGN(. - __image_base__, XEN_LOAD_ALIGN); + #ifdef EFI . = ALIGN(4); .reloc : { @@ -337,3 +346,8 @@ ASSERT((trampoline_end - trampoline_start) < TRAMPOLINE_SPACE - MBI_SPACE_MIN, "not enough room for trampoline and mbi data") ASSERT((wakeup_stack - wakeup_stack_start) >= WAKEUP_STACK_MIN, "wakeup stack too small") + +ASSERT(efi_pe_head_end == _start, "PE header does not end at the beginning of .text section") +ASSERT(_start == __XEN_VIRT_START + XEN_IMG_OFFSET, ".text section begins at wrong address") +ASSERT(IS_ALIGNED(_start, XEN_FILE_ALIGN), "_start misaligned") +ASSERT(IS_ALIGNED(__bss_start, XEN_FILE_ALIGN), "__bss_start misaligned")