Skip to contentSkip to conversation

Fix two crashes in decomression code

PATCH Superseded Kirill A. Shutemov 2 participants 8 messages May 10, 2018 – May 13, 2018 x86

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover Fix two crashes in decomression code

Here's two fixes in early boot code that leads to instant reboot.

Kirill A. Shutemov (2):
x86/boot/compressed/64: Set up GOT for paging_prepare() and
cleanup_trampoline()
x86/boot/compressed/64: Fix moving page table out of trampoline memory

 arch/x86/boot/compressed/head_64.S    | 77 ++++++++++++++++++++++-----
 arch/x86/boot/compressed/pgtable_64.c | 14 ++---
 2 files changed, 67 insertions(+), 24 deletions(-)
Signature · 1 line
2.17.0
#1 lore raw

PATCH 1/2 x86/boot/compressed/64: Set up GOT for paging_prepare() and cleanup_trampoline()

Eric and Hugh have reported instant reboot due to my recent changes in decompression code.

The root cause is that I didn't realize that we need to adjust GOT to be able to run C code that early.

The problem is only visible with old toolchain. Binutils >= 2.24 is able to eliminate GOT references by replacing them with RIP-relative address loads[1].

We need to adjust GOT two times:
- before calling paging_prepare() with address the binary was loaded by bootloader
- before relocating the kernel to the new place with relocation address

[1] https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;a=commitdiff;h=80d873266dec

Fixes 194a9749c73d ("x86/boot/compressed/64: Handle 5-level paging boot if kernel is above 4G")
---
 arch/x86/boot/compressed/head_64.S | 66 ++++++++++++++++++++++++------
 1 file changed, 53 insertions(+), 13 deletions(-)
arch/x86/boot/compressed/head_64.S +53 -13
index fca012baba19..6cbb2d64c91e 100644
@@ -305,6 +305,21 @@ ENTRY(startup_64)
 	/* Set up the stack */
 	leaq	boot_stack_end(%rbx), %rsp
 
+	/*
+	 * paging_prepare() and cleanup_trampoline() below can have GOT
+	 * references. Adjust the table with address we are running at.
+	 */
+
+	/* The GOP was not adjusted before */
+	xorq	%rax, %rax
+
+	/* Calculate the address the binary is loaded at. */
+	call	1f
+1:	popq	%rdi
+	subq	$1b, %rdi
+
+	call	adjust_gop
+
 	/*
 	 * At this point we are in long mode with 4-level paging enabled,
 	 * but we might want to enable 5-level paging or vice versa.
@@ -381,6 +396,24 @@ 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.
+	 * This address was used to adjust the table before and we need to
+	 * undo the change.
+	 */
+	call	1f
+1:	popq	%rax
+	subq	$1b, %rax
+
+	/* The new adjustment is relocation address */
+	movq	%rbx, %rdi
+	call	adjust_gop
+
 /*
  * Copy the compressed kernel to the end of our buffer
  * where decompression in place becomes safe.
@@ -481,19 +514,6 @@ relocated:
 	shrq	$3, %rcx
 	rep	stosq
 
-/*
- * Adjust our own GOT
- */
-	leaq	_got(%rip), %rdx
-	leaq	_egot(%rip), %rcx
-1:
-	cmpq	%rcx, %rdx
-	jae	2f
-	addq	%rbx, (%rdx)
-	addq	$8, %rdx
-	jmp	1b
-2:
-	
 /*
  * Do the extraction, and jump to the new kernel..
  */
@@ -512,6 +532,26 @@ relocated:
  */
 	jmp	*%rax
 
+/*
+ * Adjust global offest table
+ *
+ * RAX is previous adjustment of the table to undo (0 if it's the first time we touch GOP).
+ * RDI is the new adjustment to apply.
+ */
+adjust_gop:
+	/* 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	$8, %rdx
+	jmp	1b
+2:
+	ret
+
 	.code32
 /*
  * This is the 32-bit trampoline that will be copied over to low memory.
Signature · 1 line
2.17.0
#2 lore raw

Re: [PATCH 1/2] x86/boot/compressed/64: Set up GOT for paging_prepare() and cleanup_trampoline()

arch/x86/boot/compressed/head_64.S ENTRY(startup_64) · line 313

Quoted 6 lines · Kirill A. Shutemov
arch/x86/boot/compressed/head_64.S +6 -0
+	/*
+	 * paging_prepare() and cleanup_trampoline() below can have GOT
+	 * references. Adjust the table with address we are running at.
+	 */
+
+	/* The GOP was not adjusted before */

GOP == EFI speak for Graphics Output Protocol. What the heck?

arch/x86/boot/compressed/head_64.S ENTRY(startup_64) · line 314

Quoted 1 line
arch/x86/boot/compressed/head_64.S +1 -0
+	xorq	%rax, %rax

And this clearing of RAX is related to this because? Sure you need it for adjust_got() but adding a comment to that is too much asked for, right?

Quoted 11 lines
arch/x86/boot/compressed/head_64.S +7 -0
+	/* Calculate the address the binary is loaded at. */
+	call	1f
+1:	popq	%rdi
+	subq	$1b, %rdi
+
+	call	adjust_gop
+
 	/*
 	 * At this point we are in long mode with 4-level paging enabled,
 	 * but we might want to enable 5-level paging or vice versa.

arch/x86/boot/compressed/head_64.S trampoline_return: · line 402

Quoted 7 lines
arch/x86/boot/compressed/head_64.S +4 -0
@@ -381,6 +396,24 @@ 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.
+	 */

Breaking up those comments makes it more readable, right?

arch/x86/boot/compressed/head_64.S trampoline_return: · line 413

Quoted 10 lines
arch/x86/boot/compressed/head_64.S +10 -0
+	/*
+	 * Calculate the address the binary is loaded at.
+	 * This address was used to adjust the table before and we need to
+	 * undo the change.
+	 */
+	call	1f
+1:	popq	%rax
+	subq	$1b, %rax
+
+	/* The new adjustment is relocation address */

is the relocation address

Quoted 27 lines
arch/x86/boot/compressed/head_64.S +3 -13
+	movq	%rbx, %rdi
+	call	adjust_gop
+
 /*
  * Copy the compressed kernel to the end of our buffer
  * where decompression in place becomes safe.
@@ -481,19 +514,6 @@ relocated:
 	shrq	$3, %rcx
 	rep	stosq
 
-/*
- * Adjust our own GOT
- */
-	leaq	_got(%rip), %rdx
-	leaq	_egot(%rip), %rcx
-1:
-	cmpq	%rcx, %rdx
-	jae	2f
-	addq	%rbx, (%rdx)
-	addq	$8, %rdx
-	jmp	1b
-2:
-	
 /*
  * Do the extraction, and jump to the new kernel..
  */

arch/x86/boot/compressed/head_64.S relocated: · line 536

Quoted 5 lines
arch/x86/boot/compressed/head_64.S +2 -0
@@ -512,6 +532,26 @@ relocated:
  */
 	jmp	*%rax
 
+/*
+ * Adjust global offest table

offest?

arch/x86/boot/compressed/head_64.S relocated: · line 538

Quoted 2 lines
arch/x86/boot/compressed/head_64.S +2 -0
+ *
+ * RAX is previous adjustment of the table to undo (0 if it's the first time we touch GOP).

is the previous

And there is no reason to make that line overly long.

Quoted 5 lines
arch/x86/boot/compressed/head_64.S +5 -0
+ * RDI is the new adjustment to apply.
+ */
+adjust_gop:
+	/* Walk through the GOT adding the address to the entries */
+	leaq	_got(%rip), %rdx

arch/x86/boot/compressed/head_64.S relocated: · line 553

Quoted 10 lines
arch/x86/boot/compressed/head_64.S +10 -0
+	leaq	_egot(%rip), %rcx
+1:
+	cmpq	%rcx, %rdx
+	jae	2f
+	subq	%rax, (%rdx)	/* Undo previous adjustment */
+	addq	%rdi, (%rdx)	/* Apply the new adjustment */
+	addq	$8, %rdx
+	jmp	1b
+2:
+	ret

I'm really tired of your carelessness. The amount of half baken stuff you submit is way above the tolerance level by now. I asked you several times to be more careful, but you simply do not care at all. Get your act together finally.

Thanks,

tglx

#3 lore raw

Re: [PATCH 1/2] x86/boot/compressed/64: Set up GOT for paging_prepare() and cleanup_trampoline()

Quoted 10 lines · Thomas Gleixner

arch/x86/boot/compressed/head_64.S ENTRY(startup_64) · line 313

Quoted 6 lines · Kirill A. Shutemov
arch/x86/boot/compressed/head_64.S +6 -0
+	/*
+	 * paging_prepare() and cleanup_trampoline() below can have GOT
+	 * references. Adjust the table with address we are running at.
+	 */
+
+	/* The GOP was not adjusted before */

GOP == EFI speak for Graphics Output Protocol. What the heck?

I was not aware about Graphics Output Protocol.

Quoted 5 lines

arch/x86/boot/compressed/head_64.S ENTRY(startup_64) · line 314

Quoted 2 lines
arch/x86/boot/compressed/head_64.S +1 -0
+	xorq	%rax, %rax

And this clearing of RAX is related to this because? Sure you need it for adjust_got() but adding a comment to that is too much asked for, right?

Huh? The comment just above the line describes why it's needed.

Quoted 14 lines
Quoted 14 lines
arch/x86/boot/compressed/head_64.S +7 -0
+	/* Calculate the address the binary is loaded at. */
+	call	1f
+1:	popq	%rdi
+	subq	$1b, %rdi
+
+	call	adjust_gop
+
 	/*
 	 * At this point we are in long mode with 4-level paging enabled,
 	 * but we might want to enable 5-level paging or vice versa.
@@ -381,6 +396,24 @@ trampoline_return:
 	pushq	$0
 	popfq
 
Quoted 6 lines

arch/x86/boot/compressed/head_64.S trampoline_return: · line 402

Quoted 4 lines
arch/x86/boot/compressed/head_64.S +4 -0
+	/*
+	 * Previously we've adjusted the GOT with address the binary was
+	 * loaded at. Now we need to re-adjust for relocation address.
+	 */

Breaking up those comments makes it more readable, right?

Yes, I think so.

The first comment is for the whole block of code below. The second is the comment for the first step.

Quoted 67 lines

arch/x86/boot/compressed/head_64.S trampoline_return: · line 413

Quoted 10 lines
arch/x86/boot/compressed/head_64.S +10 -0
+	/*
+	 * Calculate the address the binary is loaded at.
+	 * This address was used to adjust the table before and we need to
+	 * undo the change.
+	 */
+	call	1f
+1:	popq	%rax
+	subq	$1b, %rax
+
+	/* The new adjustment is relocation address */

is the relocation address

Quoted 27 lines
arch/x86/boot/compressed/head_64.S +3 -13
+	movq	%rbx, %rdi
+	call	adjust_gop
+
 /*
  * Copy the compressed kernel to the end of our buffer
  * where decompression in place becomes safe.
@@ -481,19 +514,6 @@ relocated:
 	shrq	$3, %rcx
 	rep	stosq
 
-/*
- * Adjust our own GOT
- */
-	leaq	_got(%rip), %rdx
-	leaq	_egot(%rip), %rcx
-1:
-	cmpq	%rcx, %rdx
-	jae	2f
-	addq	%rbx, (%rdx)
-	addq	$8, %rdx
-	jmp	1b
-2:
-	
 /*
  * Do the extraction, and jump to the new kernel..
  */

arch/x86/boot/compressed/head_64.S relocated: · line 536

Quoted 5 lines
arch/x86/boot/compressed/head_64.S +2 -0
@@ -512,6 +532,26 @@ relocated:
  */
 	jmp	*%rax
 
+/*
+ * Adjust global offest table

offest?

arch/x86/boot/compressed/head_64.S relocated: · line 538

Quoted 2 lines
arch/x86/boot/compressed/head_64.S +2 -0
+ *
+ * RAX is previous adjustment of the table to undo (0 if it's the first time we touch GOP).

is the previous

And there is no reason to make that line overly long.

Quoted 12 lines
arch/x86/boot/compressed/head_64.S +12 -0
+ * RDI is the new adjustment to apply.
+ */
+adjust_gop:
+	/* 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	$8, %rdx
Quoted 8 lines

arch/x86/boot/compressed/head_64.S relocated: · line 553

Quoted 3 lines
arch/x86/boot/compressed/head_64.S +3 -0
+	jmp	1b
+2:
+	ret

I'm really tired of your carelessness. The amount of half baken stuff you submit is way above the tolerance level by now. I asked you several times to be more careful, but you simply do not care at all. Get your act together finally.

I don't think it's fair.

Yes, I have hard time write correctly, even in my native languages. I'm blind to mistakes I do. I'm sorry about them.

But I do care about bugs in my code and I do my best addressing them. It took a lot of effort to find root cause of the bug and your statement about my carelessness doesn't match my assessment.

Have a nice day.

Signature · 1 line
 Kirill A. Shutemov
#4 lore raw

Re: [PATCH 1/2] x86/boot/compressed/64: Set up GOT for paging_prepare() and cleanup_trampoline()

Quoted 12 lines · Kirill A. Shutemov
Quoted 10 lines · Thomas Gleixner

arch/x86/boot/compressed/head_64.S ENTRY(startup_64) · line 313

Quoted 6 lines · Kirill A. Shutemov
arch/x86/boot/compressed/head_64.S +6 -0
+	/*
+	 * paging_prepare() and cleanup_trampoline() below can have GOT
+	 * references. Adjust the table with address we are running at.
+	 */
+
+	/* The GOP was not adjusted before */

GOP == EFI speak for Graphics Output Protocol. What the heck?

Quoted 1 line

I was not aware about Graphics Output Protocol.

That does not matter. GOP what ever you think it is has nothing to do here. This is about GOT or am I missing something?

Quoted 6 lines
Quoted 4 lines

arch/x86/boot/compressed/head_64.S ENTRY(startup_64) · line 314

Quoted 1 line
arch/x86/boot/compressed/head_64.S +1 -0
+	xorq	%rax, %rax

And this clearing of RAX is related to this because? Sure you need it for adjust_got() but adding a comment to that is too much asked for, right?

Huh? The comment just above the line describes why it's needed.

No it does not. It is a comment which has only value when you first read the comment above the function which is called 5 lines later. Comments should make sense on their own. To be honest I did not even make the connection when I read the function later.

Quoted 21 lines
Quoted 14 lines
Quoted 14 lines
arch/x86/boot/compressed/head_64.S +7 -0
+	/* Calculate the address the binary is loaded at. */
+	call	1f
+1:	popq	%rdi
+	subq	$1b, %rdi
+
+	call	adjust_gop
+
 	/*
 	 * At this point we are in long mode with 4-level paging enabled,
 	 * but we might want to enable 5-level paging or vice versa.
@@ -381,6 +396,24 @@ trampoline_return:
 	pushq	$0
 	popfq
 
Quoted 6 lines

arch/x86/boot/compressed/head_64.S trampoline_return: · line 402

Quoted 4 lines
arch/x86/boot/compressed/head_64.S +4 -0
+	/*
+	 * Previously we've adjusted the GOT with address the binary was
+	 * loaded at. Now we need to re-adjust for relocation address.
+	 */

Breaking up those comments makes it more readable, right?

Quoted 4 lines

Yes, I think so.

The first comment is for the whole block of code below. The second is the comment for the first step.

Sorry no. It's just confusing as hell and a few weeks down the road it looks like somebody removed the code between the comments and forgot to update them. You can put an empty line into one comment block to separate paragraphs.

Quoted 2 lines

Yes, I have hard time write correctly, even in my native languages. I'm blind to mistakes I do. I'm sorry about them.

Sorry, that you have a problem with that, but you could have told me offlist long ago and we would have found a solution for this.

Not knowing that, it just looks like being careless to the other side.

Thanks,

tglx

#5 lore raw

PATCH 2/2 x86/boot/compressed/64: Fix moving page table out of trampoline memory

In reply to Kirill A. Shutemov

top_pgtable address has to be calculated relative to where the kernel image will be relocated for decompression, not relative to position of kernel is running at the moment. We do the same for the rest of page table we use the stage. It makes them safe from being overwritten during decompression.

Calculate the address of top_pgtable in assembly and pass down to cleanup_trampoline().

Move the page table to .pgtable section where the rest of page tables are. The section is @nobits so we save 4k in kernel image.

Fixes e9d0e6330eb8 ("x86/boot/compressed/64: Prepare new top-level page table for trampoline")
---
 arch/x86/boot/compressed/head_64.S    | 11 +++++++++++
 arch/x86/boot/compressed/pgtable_64.c | 14 +++-----------
 2 files changed, 14 insertions(+), 11 deletions(-)
arch/x86/boot/compressed/head_64.S +11 -0
index 6cbb2d64c91e..1acf7b5d16cf 100644
@@ -385,10 +385,14 @@ trampoline_return:
 	/*
 	 * cleanup_trampoline() would restore trampoline memory.
 	 *
+	 * RDI is address of the page table to use instead of page table
+	 * in trampoline memory (if required).
+	 *
 	 * RSI holds real mode data and needs to be preserved across
 	 * this function call.
 	 */
 	pushq	%rsi
+	leaq	top_pgtable(%rbx), %rdi
 	call	cleanup_trampoline
 	popq	%rsi
 
@@ -689,3 +693,10 @@ boot_stack_end:
 	.balign 4096
 pgtable:
 	.fill BOOT_PGT_SIZE, 1, 0
+
+/*
+ * The page table is going to be used instead of page table in the trampoline
+ * memory.
+ */
+top_pgtable:
+	.fill PAGE_SIZE, 1, 0
arch/x86/boot/compressed/pgtable_64.c +3 -11
index 32af1cbcd903..a362fa0b849c 100644
@@ -22,14 +22,6 @@ struct paging_config {
 /* Buffer to preserve trampoline memory */
 static char trampoline_save[TRAMPOLINE_32BIT_SIZE];
 
-/*
- * The page table is going to be used instead of page table in the trampoline
- * memory.
- *
- * It must not be in BSS as BSS is cleared after cleanup_trampoline().
- */
-static char top_pgtable[PAGE_SIZE] __aligned(PAGE_SIZE) __section(.data);
-
 /*
  * Trampoline address will be printed by extract_kernel() for debugging
  * purposes.
@@ -134,7 +126,7 @@ struct paging_config paging_prepare(void)
 	return paging_config;
 }
 
-void cleanup_trampoline(void)
+void cleanup_trampoline(void *pgtable)
 {
 	void *trampoline_pgtable;
 
@@ -145,8 +137,8 @@ void cleanup_trampoline(void)
 	 * if it's there.
 	 */
 	if ((void *)__native_read_cr3() == trampoline_pgtable) {
-		memcpy(top_pgtable, trampoline_pgtable, PAGE_SIZE);
-		native_write_cr3((unsigned long)top_pgtable);
+		memcpy(pgtable, trampoline_pgtable, PAGE_SIZE);
+		native_write_cr3((unsigned long)pgtable);
 	}
 
 	/* Restore trampoline memory */
Signature · 1 line
2.17.0
#6 lore raw

Re: [PATCH 2/2] x86/boot/compressed/64: Fix moving page table out of trampoline memory

Quoted 11 lines · Kirill A. Shutemov

top_pgtable address has to be calculated relative to where the kernel image will be relocated for decompression, not relative to position of kernel is running at the moment. We do the same for the rest of page table we use the stage. It makes them safe from being overwritten during decompression.

Calculate the address of top_pgtable in assembly and pass down to cleanup_trampoline().

Move the page table to .pgtable section where the rest of page tables are. The section is @nobits so we save 4k in kernel image.

So this is supposed to be a fix, but the whole changelog talks about WHAT the patch does and not WHY. Darn, we need proper description of the failure which is about to be fixed.

It's not that hard and I'm really tired to tell you that over and over.

arch/x86/boot/compressed/head_64.S trampoline_return: · line 389

Quoted 5 lines
arch/x86/boot/compressed/head_64.S +2 -0
 	/*
 	 * cleanup_trampoline() would restore trampoline memory.
 	 *
+	 * RDI is address of the page table to use instead of page table
+	 * in trampoline memory (if required).

Do you really believe that you understand that comment 6 month from now?

Thanks,

tglx

#7 lore raw

Re: [PATCH 2/2] x86/boot/compressed/64: Fix moving page table out of trampoline memory

Quoted 11 lines · Thomas Gleixner
Quoted 9 lines · Kirill A. Shutemov

top_pgtable address has to be calculated relative to where the kernel image will be relocated for decompression, not relative to position of kernel is running at the moment. We do the same for the rest of page table we use the stage. It makes them safe from being overwritten during decompression.

Calculate the address of top_pgtable in assembly and pass down to cleanup_trampoline().

Quoted 6 lines
Quoted 2 lines

Move the page table to .pgtable section where the rest of page tables are. The section is @nobits so we save 4k in kernel image.

So this is supposed to be a fix, but the whole changelog talks about WHAT the patch does and not WHY. Darn, we need proper description of the failure which is about to be fixed.

"It makes them safe from being overwritten during decompression."

Quoted 9 lines

It's not that hard and I'm really tired to tell you that over and over.

arch/x86/boot/compressed/head_64.S trampoline_return: · line 389

Quoted 5 lines
arch/x86/boot/compressed/head_64.S +2 -0
 	/*
 	 * cleanup_trampoline() would restore trampoline memory.
 	 *
+	 * RDI is address of the page table to use instead of page table
+	 * in trampoline memory (if required).

Do you really believe that you understand that comment 6 month from now?

Yes, I think I will.

Signature · 1 line
 Kirill A. Shutemov