All of lore.kernel.org
 help / color / mirror / Atom feed
From: Arvind Sankar <nivedita@alum.mit.edu>
To: Ard Biesheuvel <ardb@kernel.org>
Cc: linux-efi@vger.kernel.org
Subject: [PATCH 1/3] x86/boot/compressed/64: Make adjust_got easier to use repeatedly
Date: Tue,  7 Jan 2020 08:54:58 -0500	[thread overview]
Message-ID: <20200107135500.644618-2-nivedita@alum.mit.edu> (raw)
In-Reply-To: <20200107135500.644618-1-nivedita@alum.mit.edu>

Get the new relocation base address inside adjust_got, and save it so
that the caller doesn't have to figure it out.

Signed-off-by: Arvind Sankar <nivedita@alum.mit.edu>
---
 arch/x86/boot/compressed/head_64.S | 58 +++++++++++++-----------------
 1 file changed, 24 insertions(+), 34 deletions(-)

diff --git a/arch/x86/boot/compressed/head_64.S b/arch/x86/boot/compressed/head_64.S
index 1f1f6c8139b3..1464d8d0ec66 100644
--- a/arch/x86/boot/compressed/head_64.S
+++ b/arch/x86/boot/compressed/head_64.S
@@ -312,20 +312,7 @@ SYM_CODE_START(startup_64)
 	/*
 	 * paging_prepare() and cleanup_trampoline() below can have GOT
 	 * references. Adjust the table with address we are running at.
-	 *
-	 * Zero RAX for adjust_got: the GOT was not adjusted before;
-	 * there's no adjustment to undo.
 	 */
-	xorq	%rax, %rax
-
-	/*
-	 * Calculate the address the binary is loaded at and use it as
-	 * a GOT adjustment.
-	 */
-	call	1f
-1:	popq	%rdi
-	subq	$1b, %rdi
-
 	call	.Ladjust_got
 
 	/*
@@ -412,21 +399,6 @@ trampoline_return:
 	pushq	$0
 	popfq
 
-	/*
-	 * Previously we've adjusted the GOT with address the binary was
-	 * loaded at. Now we need to re-adjust for relocation address.
-	 *
-	 * Calculate the address the binary is loaded at, so that we can
-	 * undo the previous GOT adjustment.
-	 */
-	call	1f
-1:	popq	%rax
-	subq	$1b, %rax
-
-	/* The new adjustment is the relocation address */
-	movq	%rbx, %rdi
-	call	.Ladjust_got
-
 /*
  * Copy the compressed kernel to the end of our buffer
  * where decompression in place becomes safe.
@@ -475,6 +447,12 @@ SYM_FUNC_START_LOCAL_NOALIGN(.Lrelocated)
 	shrq	$3, %rcx
 	rep	stosq
 
+/*
+ * Previously we've adjusted the GOT with address the binary was
+ * loaded at. Now we need to re-adjust for relocation address.
+ */
+	call	.Ladjust_got
+
 /*
  * Do the extraction, and jump to the new kernel..
  */
@@ -497,23 +475,33 @@ SYM_FUNC_END(.Lrelocated)
 /*
  * Adjust the global offset table
  *
- * RAX is the previous adjustment of the table to undo (use 0 if it's the
- * first time we touch GOT).
- * RDI is the new adjustment to apply.
+ * The relocation base address calculation uses RIP-relative addressing, so if
+ * the kernel is being relocated to a new address, this function must be called
+ * after execution has been passed to the new location. We keep track of the
+ * relocation address so that it can be backed out if this function is called
+ * repeatedly.
  */
-.Ladjust_got:
+
+SYM_FUNC_START_LOCAL(.Ladjust_got)
+	/* Get the new relocation base address */
+	leaq	startup_32(%rip), %rax
+	/* Backout the previous relocation address if any */
+	subq	got_relocation_base(%rip), %rax
+	/* Store the relocation base address for future reference */
+	addq	%rax, got_relocation_base(%rip)
+
 	/* Walk through the GOT adding the address to the entries */
 	leaq	_got(%rip), %rdx
 	leaq	_egot(%rip), %rcx
 1:
 	cmpq	%rcx, %rdx
 	jae	2f
-	subq	%rax, (%rdx)	/* Undo previous adjustment */
-	addq	%rdi, (%rdx)	/* Apply the new adjustment */
+	addq	%rax, (%rdx)	/* Apply the incremental adjustment */
 	addq	$8, %rdx
 	jmp	1b
 2:
 	ret
+SYM_FUNC_END(.Ladjust_got)
 
 	.code32
 /*
@@ -628,6 +616,8 @@ SYM_DATA_START_LOCAL(gdt)
 	.quad   0x0000000000000000	/* TS continued */
 SYM_DATA_END_LABEL(gdt, SYM_L_LOCAL, gdt_end)
 
+SYM_DATA_LOCAL(got_relocation_base, .quad 0)
+
 #ifdef CONFIG_EFI_MIXED
 SYM_DATA_LOCAL(efi32_boot_args, .long 0, 0)
 SYM_DATA(efi_is64, .byte 1)
-- 
2.24.1


  reply	other threads:[~2020-01-07 13:55 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-01-07 13:54 [PATCH 0/3] Relocate GOT before calling EFI stub Arvind Sankar
2020-01-07 13:54 ` Arvind Sankar [this message]
2020-01-07 13:54 ` [PATCH 2/3] x86/boot/compressed/32: Allow adjust_got to be called repeatedly Arvind Sankar
2020-01-07 13:55 ` [PATCH 3/3] x86/boot: Perform GOT relocation before calling EFI stub Arvind Sankar
2020-01-07 14:01 ` [PATCH 0/3] Relocate GOT " Ard Biesheuvel
2020-01-07 14:13   ` Ard Biesheuvel
2020-01-07 14:21     ` Arvind Sankar
2020-01-07 14:24       ` Ard Biesheuvel
2020-01-07 14:27         ` Arvind Sankar
2020-01-07 14:28           ` Ard Biesheuvel
2020-01-07 17:58             ` Arvind Sankar
2020-01-07 17:59               ` Ard Biesheuvel
2020-01-07 18:08                 ` Arvind Sankar
2020-01-07 18:10                   ` Ard Biesheuvel
2020-01-07 18:32                     ` Arvind Sankar
2020-01-07 19:03                       ` Ard Biesheuvel
2020-01-07 19:14                         ` Arvind Sankar
2020-01-07 19:23                           ` Ard Biesheuvel
2020-01-07 19:51                             ` Ard Biesheuvel
2020-01-07 20:00                               ` Arvind Sankar

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=20200107135500.644618-2-nivedita@alum.mit.edu \
    --to=nivedita@alum.mit.edu \
    --cc=ardb@kernel.org \
    --cc=linux-efi@vger.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.