git: d5d2aee87319 - stable/15 - amd64 resumectx(): restore FRED state before loading %cr4

From: Konstantin Belousov <kib_at_FreeBSD.org>
Date: Tue, 03 Nov 2026 09:02:12 UTC
The branch stable/15 has been updated by kib:

URL: https://cgit.FreeBSD.org/src/commit/?id=d5d2aee87319a677896277a994ee4f57f665a94d

commit d5d2aee87319a677896277a994ee4f57f665a94d
Author:     Konstantin Belousov <kib@FreeBSD.org>
AuthorDate: 2026-10-28 14:05:50 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2026-11-03 09:01:15 +0000

    amd64 resumectx(): restore FRED state before loading %cr4
    
    (cherry picked from commit 2fd12bf2dd9c1cf997ce2505bc2af10572ca70e2)
---
 sys/amd64/amd64/cpu_switch.S | 35 +++++++++++++++++++----------------
 1 file changed, 19 insertions(+), 16 deletions(-)

diff --git a/sys/amd64/amd64/cpu_switch.S b/sys/amd64/amd64/cpu_switch.S
index a9a8ad3da872..5c76114b747e 100644
--- a/sys/amd64/amd64/cpu_switch.S
+++ b/sys/amd64/amd64/cpu_switch.S
@@ -467,22 +467,10 @@ ENTRY(resumectx)
 	movl	PCB_SFMASK(%rdi),%eax
 	wrmsr
 
-	/* Restore CR0, CR2, CR4 and CR3. */
-	movq	PCB_CR0(%rdi),%rax
-	movq	%rax,%cr0
-	movq	PCB_CR2(%rdi),%rax
-	movq	%rax,%cr2
-	movq	PCB_CR4(%rdi),%rax
-	movq	%rax,%cr4
-	movq	PCB_CR3(%rdi),%rax
-	movq	%rax,%cr3
-
-	/* Restore descriptor tables. */
+	/* Restore FRED state before reloading %cr4. */
 	cmpb	$0,fred
-	jne	1f
-	lidt	PCB_IDT(%rdi)
-	jmp	2f
-1:	movl	$MSR_FRED_RSP0,%ecx
+	je	1f
+	movl	$MSR_FRED_RSP0,%ecx
 	movl	PCB_FRED_RSP0(%rdi),%eax
 	movl	4 + PCB_FRED_RSP0(%rdi),%edx
 	wrmsr
@@ -506,7 +494,22 @@ ENTRY(resumectx)
 	movl	PCB_FRED_CONFIG(%rdi),%eax
 	movl	4 + PCB_FRED_CONFIG(%rdi),%edx
 	wrmsr
-2:	lldt	PCB_LDT(%rdi)
+1:
+	/* Restore CR0, CR2, CR4 and CR3. */
+	movq	PCB_CR0(%rdi),%rax
+	movq	%rax,%cr0
+	movq	PCB_CR2(%rdi),%rax
+	movq	%rax,%cr2
+	movq	PCB_CR4(%rdi),%rax
+	movq	%rax,%cr4
+	movq	PCB_CR3(%rdi),%rax
+	movq	%rax,%cr3
+
+	/* Restore descriptor tables. */
+	cmpb	$0,fred
+	jne	1f
+	lidt	PCB_IDT(%rdi)
+1:	lldt	PCB_LDT(%rdi)
 
 #define	SDT_SYSTSS	9
 #define	SDT_SYSBSY	11