Received: from list by lists.gnu.org with archive (Exim 4.71) id 1fIfLl-0006qb-8A for mharc-grub-devel@gnu.org; Tue, 15 May 2018 15:11:05 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:36917) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fIfLi-0006qR-Iz for grub-devel@gnu.org; Tue, 15 May 2018 15:11:04 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fIfLe-0003ZM-9Q for grub-devel@gnu.org; Tue, 15 May 2018 15:11:02 -0400 Received: from genode.genode-labs.com ([94.130.141.228]:48108) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1fIfLd-0003YU-R8 for grub-devel@gnu.org; Tue, 15 May 2018 15:10:58 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=genode-labs.com; s=2018-03; h=Content-Transfer-Encoding:Content-Type: In-Reply-To:MIME-Version:Date:Message-ID:From:References:To:Subject:Sender: Reply-To:Cc:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help: List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=mOdeBdcju7tEuRrLCgHedcwr7jJofAgOt1GQBAaJKbE=; b=P/mJTQwXkIGRWdApf8QmvS72JR pYwOIo8yCXCPgeyRg1kCYObyx2RyzpMXNA9fVPmBDPOgdtFgiCoR/ESeK4dDpWgB4ybDBuJ9FIaU6 cUs+nZuZKrpRKUiBqmi7/i4nmvUE6VR4f4MS9m9AL9WcwxIqmWzheqtORolhknp+w3zz8gSfvjz6n xinSG5lbW+AMI+ljkWVUO0x2CcIv8j3RNFVPM1k0ZLSmh1jxJDYnvrRpQtJqL4aW8W+OUexWUM9Y1 gjKoYaQXNUE+L8bmEBjw5c/6JlyEZaZA+zxYgLF1r08C30rcJ9sLRlzjtQDSbTbHtMBjmMDXKfQG9 DFsQqRkA==; Received: by genode.genode-labs.com (Exim 4.89) with esmtpsa (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (envelope-from ) id 1fIfLa-0003lA-EC ; Tue, 15 May 2018 21:10:55 +0200 Subject: Re: [PATCH] mbi: use per segment a separate relocator chunk To: The development of GNU GRUB , Daniel Kiper References: <20180515134229.GC16845@router-fw-old.local.net-space.pl> From: Alexander Boettcher Message-ID: Date: Tue, 15 May 2018 21:10:48 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.7.0 MIME-Version: 1.0 In-Reply-To: <20180515134229.GC16845@router-fw-old.local.net-space.pl> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit X-Spam_score: 0.0 X-Spam_score_int: 0 X-Spam_bar: / X-Spam_report: NO_RELAYS=-0.001,URIBL_BLOCKED=0.001 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 94.130.141.228 X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 15 May 2018 19:11:04 -0000 On 15.05.2018 15:42, Daniel Kiper wrote: > On Mon, May 14, 2018 at 09:02:00PM +0200, Alexander Boettcher wrote: >> >From 8a0296d040a42950cd64e733f7997203975bc184 Mon Sep 17 00:00:00 2001 >> From: Alexander Boettcher >> Date: Fri, 13 Apr 2018 23:37:01 +0200 >> Subject: [PATCH] mbi: use per segment a separate relocator chunk >> >> if elf is non relocatable. > > s/elf/ELF file/ > >> If the segments are not dense packed, the original code set up a huge >> relocator chunk comprising all segments. >> >> During the final relocation in grub_relocator_prepare_relocs, the chunk >> is moved to its desired place and overrides memory which are actually not >> covered/touched by the specified segments. >> >> The overriden memory may contain device memory (vga text mode e.g.), which >> leads to strange boot behaviour. > > I assume that a given ELF PHDR address/size does not cover VGA memory or > anything like that, No. > so, I am not sure what exactly overwrites this region. > grub_memset() in current line 161 at some point? No. During grub_relocator_prepare_reloc the overwrite happens, if i'm not wrong. An (artificial) example, imagine two ELF PHDRs, e.g. [0x8000-0x9000) and [0x2000000-0x2100000). Without this patch grub calculates one relocator chunk of size 0x20f8000 (0x2100000 - 0x8000) and places it at some higher memory, e.g. [0x3000000 - 0x30f8000). During the invocation of grub_relocator_prepare_reloc the memory gets copied from [0x3000000-0x30f8000) to [0x8000-0x2100000) and now the VGA memory area got overwritten and you have a lot of blinking symbols on the screen. > >> Signed-off-by: Alexander Boettcher >> --- >> grub-core/loader/multiboot_elfxx.c | 57 +++++++++++++++++++++++--------------- >> 1 file changed, 35 insertions(+), 22 deletions(-) >> >> diff --git a/grub-core/loader/multiboot_elfxx.c b/grub-core/loader/multiboot_elfxx.c >> index 67daf59..539c94a 100644 >> --- a/grub-core/loader/multiboot_elfxx.c >> +++ b/grub-core/loader/multiboot_elfxx.c >> @@ -57,9 +57,9 @@ CONCAT(grub_multiboot_load_elf, XX) (mbi_load_data_t *mld) >> char *phdr_base; >> grub_err_t err; >> grub_relocator_chunk_t ch; >> - grub_uint32_t load_offset, load_size; >> + grub_uint32_t load_offset = 0, load_size; >> int i; >> - void *source; >> + void *source = NULL; > > It seems to me that this change is not needed. > I am thinking about "void *source = NULL;". > >> if (ehdr->e_ident[EI_MAG0] != ELFMAG0 >> || ehdr->e_ident[EI_MAG1] != ELFMAG1 >> @@ -83,6 +83,7 @@ CONCAT(grub_multiboot_load_elf, XX) (mbi_load_data_t *mld) >> #define phdr(i) ((Elf_Phdr *) (phdr_base + (i) * ehdr->e_phentsize)) >> >> mld->link_base_addr = ~0; >> + mld->load_base_addr = ~0; >> >> /* Calculate lowest and highest load address. */ >> for (i = 0; i < ehdr->e_phnum; i++) >> @@ -97,10 +98,14 @@ CONCAT(grub_multiboot_load_elf, XX) (mbi_load_data_t *mld) >> return grub_error (GRUB_ERR_BAD_OS, "segment crosses 4 GiB border"); >> #endif >> >> - load_size = highest_load - mld->link_base_addr; >> + grub_dprintf ("multiboot_loader", "link_base_addr=0x%x, load_base_addr=0x%x, " >> + "relocatable=%d\n", mld->link_base_addr, >> + mld->load_base_addr, mld->relocatable); > > mld->load_base_addr is always ~0 here, so, it does not make sense > to display it here. Though I think that mld->link_base_addr and > mld->relocatable should be shown here. Maybe other values from mld > which are know here, i.e. align, preference, avoid_efi_boot_services > too... > >> if (mld->relocatable) >> { >> + load_size = highest_load - mld->link_base_addr; >> + >> if (load_size > mld->max_addr || mld->min_addr > mld->max_addr - load_size) >> return grub_error (GRUB_ERR_BAD_OS, "invalid min/max address and/or load size"); >> >> @@ -108,27 +113,19 @@ CONCAT(grub_multiboot_load_elf, XX) (mbi_load_data_t *mld) >> mld->min_addr, mld->max_addr - load_size, >> load_size, mld->align ? mld->align : 1, >> mld->preference, mld->avoid_efi_boot_services); >> - } >> - else >> - err = grub_relocator_alloc_chunk_addr (GRUB_MULTIBOOT (relocator), &ch, >> - mld->link_base_addr, load_size); >> >> - if (err) >> - { >> - grub_dprintf ("multiboot_loader", "Cannot allocate memory for OS image\n"); >> - return err; >> - } >> - >> - mld->load_base_addr = get_physical_target_address (ch); >> - source = get_virtual_current_address (ch); >> + if (err) >> + { >> + grub_dprintf ("multiboot_loader", "Cannot allocate memory for OS image\n"); >> + return err; >> + } >> >> - grub_dprintf ("multiboot_loader", "link_base_addr=0x%x, load_base_addr=0x%x, " >> - "load_size=0x%x, relocatable=%d\n", mld->link_base_addr, >> - mld->load_base_addr, load_size, mld->relocatable); >> + mld->load_base_addr = get_physical_target_address (ch); >> + source = get_virtual_current_address (ch); >> >> - if (mld->relocatable) >> - grub_dprintf ("multiboot_loader", "align=0x%lx, preference=0x%x, avoid_efi_boot_services=%d\n", >> - (long) mld->align, mld->preference, mld->avoid_efi_boot_services); >> + grub_dprintf ("multiboot_loader", "align=0x%lx, preference=0x%x, avoid_efi_boot_services=%d\n", >> + (long) mld->align, mld->preference, mld->avoid_efi_boot_services); >> + } > > I think that this grub_dprintf() should be moved... > >> /* Load every loadable segment in memory. */ >> for (i = 0; i < ehdr->e_phnum; i++) >> @@ -139,7 +136,23 @@ CONCAT(grub_multiboot_load_elf, XX) (mbi_load_data_t *mld) >> grub_dprintf ("multiboot_loader", "segment %d: paddr=0x%lx, memsz=0x%lx, vaddr=0x%lx\n", >> i, (long) phdr(i)->p_paddr, (long) phdr(i)->p_memsz, (long) phdr(i)->p_vaddr); >> >> - load_offset = phdr(i)->p_paddr - mld->link_base_addr; >> + if (mld->relocatable) >> + load_offset = phdr(i)->p_paddr - mld->link_base_addr; >> + else >> + { >> + err = grub_relocator_alloc_chunk_addr (GRUB_MULTIBOOT (relocator), &ch, >> + phdr(i)->p_paddr, phdr(i)->p_memsz); >> + >> + if (err) >> + { >> + grub_dprintf ("multiboot_loader", "Cannot allocate memory for OS image\n"); >> + return err; >> + } >> + >> + mld->load_base_addr = grub_min (mld->load_base_addr, get_physical_target_address (ch)); >> + >> + source = get_virtual_current_address (ch); >> + } >> >> if (phdr(i)->p_filesz != 0) >> { > > ... further below, just behind this loop. And mld->load_base_addr should be > shown here too. > > Daniel > > _______________________________________________ > Grub-devel mailing list > Grub-devel@gnu.org > https://lists.gnu.org/mailman/listinfo/grub-devel > -- Alexander Boettcher Genode Labs http://www.genode-labs.com - http://www.genode.org Genode Labs GmbH - Amtsgericht Dresden - HRB 28424 - Sitz Dresden Geschäftsführer: Dr.-Ing. Norman Feske, Christian Helmuth