public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: "Kirill A. Shutemov" <kirill.shutemov@linux.intel.com>
To: Dmitry Malkin <d.malkin@real-time-systems.com>
Cc: linux-x86_64@vger.kernel.org, linux-kernel@vger.kernel.org,
	Ingo Molnar <mingo@kernel.org>,
	Thomas Gleixner <tglx@linutronix.de>,
	"H. Peter Anvin" <hpa@zytor.com>,
	x86@kernel.org
Subject: Re: 4.17.x won't boot due to "x86/boot/compressed/64: Handle 5-level paging boot if kernel is above 4G"
Date: Fri, 27 Jul 2018 16:46:22 +0300	[thread overview]
Message-ID: <20180727134622.5htfsqa3aciultyf@black.fi.intel.com> (raw)
In-Reply-To: <9e22ee9b-0ada-9863-32aa-db3cacd25862@real-time-systems.com>

On Thu, Jul 26, 2018 at 04:21:11PM +0000, Dmitry Malkin wrote:
> On 07/26/2018 04:50 PM, Kirill A. Shutemov wrote:
> > > > > 2. reading from memory which may be reserved in case of EFI systems:
> > > > > > >      ebda_start = *(unsigned short *)0x40e << 4;
> > > > > > >      bios_start = *(unsigned short *)0x413 << 10;
> > > > > Also, on EFI system without CSM it will results in all zeros. Which will
> > > > > place trampoline_start to 0x9d000. And it also may be reserved memory. In
> > > > > fact I have such system and it is causes instant reboot (when code starts
> > > > > copying to "trampoline_start").
> > > > Could you show dmesg from such system?
> > > Sure, here it is (please note than not both pages are reserved but only
> > > second one: 0x9e000-0x9ffff):
> > Well. That's bad.
> > 
> > I don't see much options but parse e820 in decompression code. I hoped to
> > avoid this.
> > 
> > Let me see what I can do there.
> Just in case of UEFI (I don't know much about BIOS and kexec):
> register RSI (right before call paging_prepare) will contains pointer to
> "struct boot_params" (returned by function efi_main() in eboot.c).
> There are fields e820_table and e820_entries.
> 

Could you check if this makes a difference for you?

diff --git a/arch/x86/boot/compressed/pgtable_64.c b/arch/x86/boot/compressed/pgtable_64.c
index 8c5107545251..9e2157371491 100644
--- a/arch/x86/boot/compressed/pgtable_64.c
+++ b/arch/x86/boot/compressed/pgtable_64.c
@@ -1,3 +1,4 @@
+#include <asm/e820/types.h>
 #include <asm/processor.h>
 #include "pgtable.h"
 #include "../string.h"
@@ -34,10 +35,62 @@ unsigned long *trampoline_32bit __section(.data);
 extern struct boot_params *boot_params;
 int cmdline_find_option_bool(const char *option);
 
+static unsigned long find_trampoline_placement(void)
+{
+	unsigned long bios_start, ebda_start;
+	unsigned long trampoline_start;
+	struct boot_e820_entry *entry;
+	int i;
+
+	/*
+	 * Find a suitable spot for the trampoline.
+	 * This code is based on reserve_bios_regions().
+	 */
+
+	ebda_start = *(unsigned short *)0x40e << 4;
+	bios_start = *(unsigned short *)0x413 << 10;
+
+	if (bios_start < BIOS_START_MIN || bios_start > BIOS_START_MAX)
+		bios_start = BIOS_START_MAX;
+
+	if (ebda_start > BIOS_START_MIN && ebda_start < bios_start)
+		bios_start = ebda_start;
+
+	bios_start = round_down(bios_start, PAGE_SIZE);
+
+	/* Find the first usable memory region under bios_start. */
+	for (i = boot_params->e820_entries - 1; i >= 0; i--) {
+		entry = &boot_params->e820_table[i];
+
+		/* Skip all entries above bios_start. */
+		if (bios_start <= entry->addr)
+			continue;
+
+		/* Skip non-RAM entries. */
+		if (entry->type != E820_TYPE_RAM)
+			continue;
+
+		/* Adjust bios_start to the end of the entry if needed. */
+		if (bios_start > entry->addr + entry->size)
+			bios_start = entry->addr + entry->size;
+
+		/* Keep bios_start page-aligned. */
+		bios_start = round_down(bios_start, PAGE_SIZE);
+
+		/* Skip the entry if it's too small. */
+		if (bios_start - TRAMPOLINE_32BIT_SIZE < entry->addr)
+			continue;
+
+		break;
+	}
+
+	/* Place the trampoline just below the end of low memory */
+	return bios_start - TRAMPOLINE_32BIT_SIZE;
+}
+
 struct paging_config paging_prepare(void *rmode)
 {
 	struct paging_config paging_config = {};
-	unsigned long bios_start, ebda_start;
 
 	/* Initialize boot_params. Required for cmdline_find_option_bool(). */
 	boot_params = rmode;
@@ -61,23 +114,7 @@ struct paging_config paging_prepare(void *rmode)
 		paging_config.l5_required = 1;
 	}
 
-	/*
-	 * Find a suitable spot for the trampoline.
-	 * This code is based on reserve_bios_regions().
-	 */
-
-	ebda_start = *(unsigned short *)0x40e << 4;
-	bios_start = *(unsigned short *)0x413 << 10;
-
-	if (bios_start < BIOS_START_MIN || bios_start > BIOS_START_MAX)
-		bios_start = BIOS_START_MAX;
-
-	if (ebda_start > BIOS_START_MIN && ebda_start < bios_start)
-		bios_start = ebda_start;
-
-	/* Place the trampoline just below the end of low memory, aligned to 4k */
-	paging_config.trampoline_start = bios_start - TRAMPOLINE_32BIT_SIZE;
-	paging_config.trampoline_start = round_down(paging_config.trampoline_start, PAGE_SIZE);
+	paging_config.trampoline_start = find_trampoline_placement();
 
 	trampoline_32bit = (unsigned long *)paging_config.trampoline_start;
 
-- 
 Kirill A. Shutemov

  reply	other threads:[~2018-07-27 13:46 UTC|newest]

Thread overview: 39+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-07-25 17:26 4.17.x won't boot due to "x86/boot/compressed/64: Handle 5-level paging boot if kernel is above 4G" Dmitry Malkin
2018-07-25 21:21 ` Kirill A. Shutemov
2018-07-26  8:10   ` Dmitry Malkin
2018-07-26 14:50     ` Kirill A. Shutemov
2018-07-26 16:21       ` Dmitry Malkin
2018-07-27 13:46         ` Kirill A. Shutemov [this message]
     [not found] <CAF=P+=5c-baQp-CK1ViG8h=mMRv6d3EgEsN_U4rbBx-Pwv_Krw@mail.gmail.com>
2018-07-01 21:32 ` Benjamin Gilbert
2018-07-02  9:34   ` Kirill A. Shutemov
2018-07-02 19:01     ` Benjamin Gilbert
2018-07-03  8:30       ` Kirill A. Shutemov
2018-07-03  8:59         ` Thomas Gleixner
2018-07-03 11:01           ` Kirill A. Shutemov
2018-07-03 11:24   ` Gabriel C
2018-07-03 12:44     ` Kirill A. Shutemov
2018-07-03 14:02       ` Thomas Gleixner
2018-07-03 14:07         ` Bernhard Rosenkraenzer
2018-07-03 14:19           ` Thomas Gleixner
2018-07-03 14:21       ` Kirill A. Shutemov
2018-07-03 14:27         ` Thomas Gleixner
2018-07-03 18:03         ` Andi Kleen
2018-07-03 20:26           ` Kirill A. Shutemov
2018-07-03 21:00             ` Andi Kleen
2018-07-04  3:10         ` Benjamin Gilbert
2018-07-04 13:21           ` Kirill A. Shutemov
2018-07-04 15:08         ` Kirill A. Shutemov
2018-07-04 20:42           ` Benjamin Gilbert
2018-07-06  6:37           ` Masahiro Yamada
2018-07-06 10:41             ` Kirill A. Shutemov
2018-07-06 14:13               ` Masahiro Yamada
2018-07-06 14:39                 ` Gabriel C
2018-07-06 16:33                   ` Kirill A. Shutemov
2018-07-06 17:31                     ` Gabriel C
2018-07-07  0:55                   ` Masahiro Yamada
2018-07-06 16:29                 ` Kirill A. Shutemov
2018-07-06 18:11                   ` Andi Kleen
2018-07-06 19:34                     ` Benjamin Gilbert
2018-07-07  1:21                   ` Masahiro Yamada
2018-07-09 10:10                     ` Kirill A. Shutemov
2018-07-09 10:37                       ` Masahiro Yamada

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20180727134622.5htfsqa3aciultyf@black.fi.intel.com \
    --to=kirill.shutemov@linux.intel.com \
    --cc=d.malkin@real-time-systems.com \
    --cc=hpa@zytor.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-x86_64@vger.kernel.org \
    --cc=mingo@kernel.org \
    --cc=tglx@linutronix.de \
    --cc=x86@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox