Skip to contentSkip to conversation

x86: SEV-ES Guest Support

PATCH Applied Joerg Roedel 10 participants 175 messages Sep 7, 2020 – Jan 28, 2021 1 message not in the archive Page 2 of 2

Subsystems (10)
x86 KVM virtio core KVM x86 +6 more subsystems
lore
Share & tools
lore
#123 lore raw

PATCH v7 55/72 x86/sev-es: Handle RDPMC Events

In reply to Joerg Roedel

From: Tom Lendacky <[email protected]>

Implement a handler for #VC exceptions caused by RDPMC instructions.

[ [email protected]: Adapt to #VC handling infrastructure ]
---
 arch/x86/kernel/sev-es.c | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)
arch/x86/kernel/sev-es.c +22 -0
index 5c155f307129..2ee600f3184b 100644
@@ -853,6 +853,25 @@ static enum es_result vc_handle_wbinvd(struct ghcb *ghcb,
 	return sev_es_ghcb_hv_call(ghcb, ctxt, SVM_EXIT_WBINVD, 0, 0);
 }
 
+static enum es_result vc_handle_rdpmc(struct ghcb *ghcb, struct es_em_ctxt *ctxt)
+{
+	enum es_result ret;
+
+	ghcb_set_rcx(ghcb, ctxt->regs->cx);
+
+	ret = sev_es_ghcb_hv_call(ghcb, ctxt, SVM_EXIT_RDPMC, 0, 0);
+	if (ret != ES_OK)
+		return ret;
+
+	if (!(ghcb_rax_is_valid(ghcb) && ghcb_rdx_is_valid(ghcb)))
+		return ES_VMM_ERROR;
+
+	ctxt->regs->ax = ghcb->save.rax;
+	ctxt->regs->dx = ghcb->save.rdx;
+
+	return ES_OK;
+}
+
 static enum es_result vc_handle_exitcode(struct es_em_ctxt *ctxt,
 					 struct ghcb *ghcb,
 					 unsigned long exit_code)
@@ -870,6 +889,9 @@ static enum es_result vc_handle_exitcode(struct es_em_ctxt *ctxt,
 	case SVM_EXIT_RDTSCP:
 		result = vc_handle_rdtsc(ghcb, ctxt, exit_code);
 		break;
+	case SVM_EXIT_RDPMC:
+		result = vc_handle_rdpmc(ghcb, ctxt);
+		break;
 	case SVM_EXIT_CPUID:
 		result = vc_handle_cpuid(ghcb, ctxt);
 		break;
Signature · 6 lines
2.28.0

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
merge bot Applied
#124 lore raw

[tip: x86/seves] x86/sev-es: Handle RDPMC Events

The following commit has been merged into the x86/seves branch of tip:

The following commit has been merged into the x86/seves branch of tip:

Commit-ID:     5d55cf78a8785a7496919462f3d1a4689f62fb1d
Gitweb:        https://git.kernel.org/tip/5d55cf78a8785a7496919462f3d1a4689f62fb1d
Author:        Tom Lendacky <[email protected]>
AuthorDate:    Mon, 07 Sep 2020 15:15:56 +02:00
Committer:     Borislav Petkov <[email protected]>
CommitterDate: Wed, 09 Sep 2020 11:33:20 +02:00

x86/sev-es: Handle RDPMC Events

Implement a handler for #VC exceptions caused by RDPMC instructions.

---
 arch/x86/kernel/sev-es.c | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)
arch/x86/kernel/sev-es.c +22 -0
index 4d468ec..b2d7287 100644
@@ -853,6 +853,25 @@ static enum es_result vc_handle_wbinvd(struct ghcb *ghcb,
 	return sev_es_ghcb_hv_call(ghcb, ctxt, SVM_EXIT_WBINVD, 0, 0);
 }
 
+static enum es_result vc_handle_rdpmc(struct ghcb *ghcb, struct es_em_ctxt *ctxt)
+{
+	enum es_result ret;
+
+	ghcb_set_rcx(ghcb, ctxt->regs->cx);
+
+	ret = sev_es_ghcb_hv_call(ghcb, ctxt, SVM_EXIT_RDPMC, 0, 0);
+	if (ret != ES_OK)
+		return ret;
+
+	if (!(ghcb_rax_is_valid(ghcb) && ghcb_rdx_is_valid(ghcb)))
+		return ES_VMM_ERROR;
+
+	ctxt->regs->ax = ghcb->save.rax;
+	ctxt->regs->dx = ghcb->save.rdx;
+
+	return ES_OK;
+}
+
 static enum es_result vc_handle_exitcode(struct es_em_ctxt *ctxt,
 					 struct ghcb *ghcb,
 					 unsigned long exit_code)
@@ -870,6 +889,9 @@ static enum es_result vc_handle_exitcode(struct es_em_ctxt *ctxt,
 	case SVM_EXIT_RDTSCP:
 		result = vc_handle_rdtsc(ghcb, ctxt, exit_code);
 		break;
+	case SVM_EXIT_RDPMC:
+		result = vc_handle_rdpmc(ghcb, ctxt);
+		break;
 	case SVM_EXIT_CPUID:
 		result = vc_handle_cpuid(ghcb, ctxt);
 		break;
#125 lore raw

PATCH v7 56/72 x86/sev-es: Handle INVD Events

In reply to Joerg Roedel

From: Tom Lendacky <[email protected]>

Implement a handler for #VC exceptions caused by INVD instructions. Since Linux should never use INVD, just mark it as unsupported.

[ [email protected]: Adapt to #VC handling infrastructure ]
---
 arch/x86/kernel/sev-es.c | 4 ++++
 1 file changed, 4 insertions(+)
arch/x86/kernel/sev-es.c +4 -0
index 2ee600f3184b..7fb17e71ff05 100644
@@ -892,6 +892,10 @@ static enum es_result vc_handle_exitcode(struct es_em_ctxt *ctxt,
 	case SVM_EXIT_RDPMC:
 		result = vc_handle_rdpmc(ghcb, ctxt);
 		break;
+	case SVM_EXIT_INVD:
+		pr_err_ratelimited("#VC exception for INVD??? Seriously???\n");
+		result = ES_UNSUPPORTED;
+		break;
 	case SVM_EXIT_CPUID:
 		result = vc_handle_cpuid(ghcb, ctxt);
 		break;
Signature · 6 lines
2.28.0

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
merge bot Applied
#126 lore raw

[tip: x86/seves] x86/sev-es: Handle INVD Events

The following commit has been merged into the x86/seves branch of tip:

The following commit has been merged into the x86/seves branch of tip:

Commit-ID:     8b4ce83707cb2cff5f6d175ea38f751ac4456096
Gitweb:        https://git.kernel.org/tip/8b4ce83707cb2cff5f6d175ea38f751ac4456096
Author:        Tom Lendacky <[email protected]>
AuthorDate:    Mon, 07 Sep 2020 15:15:57 +02:00
Committer:     Borislav Petkov <[email protected]>
CommitterDate: Wed, 09 Sep 2020 11:33:20 +02:00

x86/sev-es: Handle INVD Events

Implement a handler for #VC exceptions caused by INVD instructions. Since Linux should never use INVD, just mark it as unsupported.

---
 arch/x86/kernel/sev-es.c | 4 ++++
 1 file changed, 4 insertions(+)
arch/x86/kernel/sev-es.c +4 -0
index b2d7287..236cfc1 100644
@@ -892,6 +892,10 @@ static enum es_result vc_handle_exitcode(struct es_em_ctxt *ctxt,
 	case SVM_EXIT_RDPMC:
 		result = vc_handle_rdpmc(ghcb, ctxt);
 		break;
+	case SVM_EXIT_INVD:
+		pr_err_ratelimited("#VC exception for INVD??? Seriously???\n");
+		result = ES_UNSUPPORTED;
+		break;
 	case SVM_EXIT_CPUID:
 		result = vc_handle_cpuid(ghcb, ctxt);
 		break;
#127 lore raw

PATCH v7 57/72 x86/sev-es: Handle MONITOR/MONITORX Events

In reply to Joerg Roedel

From: Tom Lendacky <[email protected]>

Implement a handler for #VC exceptions caused by MONITOR and MONITORX instructions.

[ [email protected]: Adapt to #VC handling infrastructure ]
---
 arch/x86/kernel/sev-es.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)
arch/x86/kernel/sev-es.c +13 -0
index 7fb17e71ff05..847a578d5158 100644
@@ -872,6 +872,16 @@ static enum es_result vc_handle_rdpmc(struct ghcb *ghcb, struct es_em_ctxt *ctxt
 	return ES_OK;
 }
 
+static enum es_result vc_handle_monitor(struct ghcb *ghcb,
+					struct es_em_ctxt *ctxt)
+{
+	/*
+	 * Treat it as a NOP and do not leak a physical address to the
+	 * hypervisor
+	 */
+	return ES_OK;
+}
+
 static enum es_result vc_handle_exitcode(struct es_em_ctxt *ctxt,
 					 struct ghcb *ghcb,
 					 unsigned long exit_code)
@@ -908,6 +918,9 @@ static enum es_result vc_handle_exitcode(struct es_em_ctxt *ctxt,
 	case SVM_EXIT_WBINVD:
 		result = vc_handle_wbinvd(ghcb, ctxt);
 		break;
+	case SVM_EXIT_MONITOR:
+		result = vc_handle_monitor(ghcb, ctxt);
+		break;
 	case SVM_EXIT_NPF:
 		result = vc_handle_mmio(ghcb, ctxt);
 		break;
Signature · 6 lines
2.28.0

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
merge bot Applied
#128 lore raw

[tip: x86/seves] x86/sev-es: Handle MONITOR/MONITORX Events

The following commit has been merged into the x86/seves branch of tip:

The following commit has been merged into the x86/seves branch of tip:

Commit-ID:     0c2fd2ef64ef1a91d81c2f61309735ac438b68a4
Gitweb:        https://git.kernel.org/tip/0c2fd2ef64ef1a91d81c2f61309735ac438b68a4
Author:        Tom Lendacky <[email protected]>
AuthorDate:    Mon, 07 Sep 2020 15:15:58 +02:00
Committer:     Borislav Petkov <[email protected]>
CommitterDate: Wed, 09 Sep 2020 11:33:20 +02:00

x86/sev-es: Handle MONITOR/MONITORX Events

Implement a handler for #VC exceptions caused by MONITOR and MONITORX instructions.

---
 arch/x86/kernel/sev-es.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)
arch/x86/kernel/sev-es.c +13 -0
index 236cfc1..b9976e7 100644
@@ -872,6 +872,16 @@ static enum es_result vc_handle_rdpmc(struct ghcb *ghcb, struct es_em_ctxt *ctxt
 	return ES_OK;
 }
 
+static enum es_result vc_handle_monitor(struct ghcb *ghcb,
+					struct es_em_ctxt *ctxt)
+{
+	/*
+	 * Treat it as a NOP and do not leak a physical address to the
+	 * hypervisor.
+	 */
+	return ES_OK;
+}
+
 static enum es_result vc_handle_exitcode(struct es_em_ctxt *ctxt,
 					 struct ghcb *ghcb,
 					 unsigned long exit_code)
@@ -908,6 +918,9 @@ static enum es_result vc_handle_exitcode(struct es_em_ctxt *ctxt,
 	case SVM_EXIT_WBINVD:
 		result = vc_handle_wbinvd(ghcb, ctxt);
 		break;
+	case SVM_EXIT_MONITOR:
+		result = vc_handle_monitor(ghcb, ctxt);
+		break;
 	case SVM_EXIT_NPF:
 		result = vc_handle_mmio(ghcb, ctxt);
 		break;
#129 lore raw

PATCH v7 58/72 x86/sev-es: Handle MWAIT/MWAITX Events

In reply to Joerg Roedel

From: Tom Lendacky <[email protected]>

Implement a handler for #VC exceptions caused by MWAIT and MWAITX instructions.

[ [email protected]: Adapt to #VC handling infrastructure ]
---
 arch/x86/kernel/sev-es.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
arch/x86/kernel/sev-es.c +10 -0
index 847a578d5158..0d0b6e15f7d9 100644
@@ -882,6 +882,13 @@ static enum es_result vc_handle_monitor(struct ghcb *ghcb,
 	return ES_OK;
 }
 
+static enum es_result vc_handle_mwait(struct ghcb *ghcb,
+				      struct es_em_ctxt *ctxt)
+{
+	/* Treat the same as MONITOR/MONITORX */
+	return ES_OK;
+}
+
 static enum es_result vc_handle_exitcode(struct es_em_ctxt *ctxt,
 					 struct ghcb *ghcb,
 					 unsigned long exit_code)
@@ -921,6 +928,9 @@ static enum es_result vc_handle_exitcode(struct es_em_ctxt *ctxt,
 	case SVM_EXIT_MONITOR:
 		result = vc_handle_monitor(ghcb, ctxt);
 		break;
+	case SVM_EXIT_MWAIT:
+		result = vc_handle_mwait(ghcb, ctxt);
+		break;
 	case SVM_EXIT_NPF:
 		result = vc_handle_mmio(ghcb, ctxt);
 		break;
Signature · 6 lines
2.28.0

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
merge bot Applied
#130 lore raw

[tip: x86/seves] x86/sev-es: Handle MWAIT/MWAITX Events

The following commit has been merged into the x86/seves branch of tip:

The following commit has been merged into the x86/seves branch of tip:

Commit-ID:     ded476bbe203e59cb571d6c576f680efe4a0ddff
Gitweb:        https://git.kernel.org/tip/ded476bbe203e59cb571d6c576f680efe4a0ddff
Author:        Tom Lendacky <[email protected]>
AuthorDate:    Mon, 07 Sep 2020 15:15:59 +02:00
Committer:     Borislav Petkov <[email protected]>
CommitterDate: Wed, 09 Sep 2020 11:33:20 +02:00

x86/sev-es: Handle MWAIT/MWAITX Events

Implement a handler for #VC exceptions caused by MWAIT and MWAITX instructions.

---
 arch/x86/kernel/sev-es.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
arch/x86/kernel/sev-es.c +10 -0
index b9976e7..2aea903 100644
@@ -882,6 +882,13 @@ static enum es_result vc_handle_monitor(struct ghcb *ghcb,
 	return ES_OK;
 }
 
+static enum es_result vc_handle_mwait(struct ghcb *ghcb,
+				      struct es_em_ctxt *ctxt)
+{
+	/* Treat the same as MONITOR/MONITORX */
+	return ES_OK;
+}
+
 static enum es_result vc_handle_exitcode(struct es_em_ctxt *ctxt,
 					 struct ghcb *ghcb,
 					 unsigned long exit_code)
@@ -921,6 +928,9 @@ static enum es_result vc_handle_exitcode(struct es_em_ctxt *ctxt,
 	case SVM_EXIT_MONITOR:
 		result = vc_handle_monitor(ghcb, ctxt);
 		break;
+	case SVM_EXIT_MWAIT:
+		result = vc_handle_mwait(ghcb, ctxt);
+		break;
 	case SVM_EXIT_NPF:
 		result = vc_handle_mmio(ghcb, ctxt);
 		break;
#131 lore raw

PATCH v7 59/72 x86/sev-es: Handle VMMCALL Events

In reply to Joerg Roedel

From: Tom Lendacky <[email protected]>

Implement a handler for #VC exceptions caused by VMMCALL instructions. This patch is only a starting point, VMMCALL emulation under SEV-ES needs further hypervisor-specific changes to provide additional state.

[ [email protected]: Adapt to #VC handling infrastructure ]
---
 arch/x86/kernel/sev-es.c | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)
arch/x86/kernel/sev-es.c +23 -0
index 0d0b6e15f7d9..db8e33280f7a 100644
@@ -889,6 +889,26 @@ static enum es_result vc_handle_mwait(struct ghcb *ghcb,
 	return ES_OK;
 }
 
+static enum es_result vc_handle_vmmcall(struct ghcb *ghcb,
+					struct es_em_ctxt *ctxt)
+{
+	enum es_result ret;
+
+	ghcb_set_rax(ghcb, ctxt->regs->ax);
+	ghcb_set_cpl(ghcb, user_mode(ctxt->regs) ? 3 : 0);
+
+	ret = sev_es_ghcb_hv_call(ghcb, ctxt, SVM_EXIT_VMMCALL, 0, 0);
+	if (ret != ES_OK)
+		return ret;
+
+	if (!ghcb_rax_is_valid(ghcb))
+		return ES_VMM_ERROR;
+
+	ctxt->regs->ax = ghcb->save.rax;
+
+	return ES_OK;
+}
+
 static enum es_result vc_handle_exitcode(struct es_em_ctxt *ctxt,
 					 struct ghcb *ghcb,
 					 unsigned long exit_code)
@@ -922,6 +942,9 @@ static enum es_result vc_handle_exitcode(struct es_em_ctxt *ctxt,
 	case SVM_EXIT_MSR:
 		result = vc_handle_msr(ghcb, ctxt);
 		break;
+	case SVM_EXIT_VMMCALL:
+		result = vc_handle_vmmcall(ghcb, ctxt);
+		break;
 	case SVM_EXIT_WBINVD:
 		result = vc_handle_wbinvd(ghcb, ctxt);
 		break;
Signature · 6 lines
2.28.0

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
merge bot Applied
#132 lore raw

[tip: x86/seves] x86/sev-es: Handle VMMCALL Events

The following commit has been merged into the x86/seves branch of tip:

The following commit has been merged into the x86/seves branch of tip:

Commit-ID:     2eb7dcf0ccc40ad3f39b000becf16661abf98102
Gitweb:        https://git.kernel.org/tip/2eb7dcf0ccc40ad3f39b000becf16661abf98102
Author:        Tom Lendacky <[email protected]>
AuthorDate:    Mon, 07 Sep 2020 15:16:00 +02:00
Committer:     Borislav Petkov <[email protected]>
CommitterDate: Wed, 09 Sep 2020 11:33:20 +02:00

x86/sev-es: Handle VMMCALL Events

Implement a handler for #VC exceptions caused by VMMCALL instructions. This is only a starting point, VMMCALL emulation under SEV-ES needs further hypervisor-specific changes to provide additional state.

[ bp: Drop "this patch". ]

---
 arch/x86/kernel/sev-es.c | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)
arch/x86/kernel/sev-es.c +23 -0
index 2aea903..86cb4c5 100644
@@ -889,6 +889,26 @@ static enum es_result vc_handle_mwait(struct ghcb *ghcb,
 	return ES_OK;
 }
 
+static enum es_result vc_handle_vmmcall(struct ghcb *ghcb,
+					struct es_em_ctxt *ctxt)
+{
+	enum es_result ret;
+
+	ghcb_set_rax(ghcb, ctxt->regs->ax);
+	ghcb_set_cpl(ghcb, user_mode(ctxt->regs) ? 3 : 0);
+
+	ret = sev_es_ghcb_hv_call(ghcb, ctxt, SVM_EXIT_VMMCALL, 0, 0);
+	if (ret != ES_OK)
+		return ret;
+
+	if (!ghcb_rax_is_valid(ghcb))
+		return ES_VMM_ERROR;
+
+	ctxt->regs->ax = ghcb->save.rax;
+
+	return ES_OK;
+}
+
 static enum es_result vc_handle_exitcode(struct es_em_ctxt *ctxt,
 					 struct ghcb *ghcb,
 					 unsigned long exit_code)
@@ -922,6 +942,9 @@ static enum es_result vc_handle_exitcode(struct es_em_ctxt *ctxt,
 	case SVM_EXIT_MSR:
 		result = vc_handle_msr(ghcb, ctxt);
 		break;
+	case SVM_EXIT_VMMCALL:
+		result = vc_handle_vmmcall(ghcb, ctxt);
+		break;
 	case SVM_EXIT_WBINVD:
 		result = vc_handle_wbinvd(ghcb, ctxt);
 		break;
#133 lore raw

PATCH v7 60/72 x86/sev-es: Handle #AC Events

In reply to Joerg Roedel

From: Joerg Roedel <[email protected]>

Implement a handler for #VC exceptions caused by #AC exceptions. The #AC exception is just forwarded to do_alignment_check() and not pushed down to the hypervisor, as requested by the SEV-ES GHCB Standardization Specification.

---
 arch/x86/kernel/sev-es.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
arch/x86/kernel/sev-es.c +19 -0
index db8e33280f7a..f78ab9369b9c 100644
@@ -909,6 +909,19 @@ static enum es_result vc_handle_vmmcall(struct ghcb *ghcb,
 	return ES_OK;
 }
 
+static enum es_result vc_handle_trap_ac(struct ghcb *ghcb,
+					struct es_em_ctxt *ctxt)
+{
+	/*
+	 * Calling ecx_alignment_check() directly does not work, because it
+	 * enables IRQs and the GHCB is active. Forward the exception and call
+	 * it later from vc_forward_exception().
+	 */
+	ctxt->fi.vector = X86_TRAP_AC;
+	ctxt->fi.error_code = 0;
+	return ES_EXCEPTION;
+}
+
 static enum es_result vc_handle_exitcode(struct es_em_ctxt *ctxt,
 					 struct ghcb *ghcb,
 					 unsigned long exit_code)
@@ -922,6 +935,9 @@ static enum es_result vc_handle_exitcode(struct es_em_ctxt *ctxt,
 	case SVM_EXIT_WRITE_DR7:
 		result = vc_handle_dr7_write(ghcb, ctxt);
 		break;
+	case SVM_EXIT_EXCP_BASE + X86_TRAP_AC:
+		result = vc_handle_trap_ac(ghcb, ctxt);
+		break;
 	case SVM_EXIT_RDTSC:
 	case SVM_EXIT_RDTSCP:
 		result = vc_handle_rdtsc(ghcb, ctxt, exit_code);
@@ -981,6 +997,9 @@ static __always_inline void vc_forward_exception(struct es_em_ctxt *ctxt)
 	case X86_TRAP_UD:
 		exc_invalid_op(ctxt->regs);
 		break;
+	case X86_TRAP_AC:
+		exc_alignment_check(ctxt->regs, error_code);
+		break;
 	default:
 		pr_emerg("Unsupported exception in #VC instruction emulation - can't continue\n");
 		BUG();
Signature · 6 lines
2.28.0

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
merge bot Applied
#134 lore raw

[tip: x86/seves] x86/sev-es: Handle #AC Events

The following commit has been merged into the x86/seves branch of tip:

The following commit has been merged into the x86/seves branch of tip:

Commit-ID:     a2d0171a9cf59637411281a929900fde80e6c1cb
Gitweb:        https://git.kernel.org/tip/a2d0171a9cf59637411281a929900fde80e6c1cb
Author:        Joerg Roedel <[email protected]>
AuthorDate:    Mon, 07 Sep 2020 15:16:01 +02:00
Committer:     Borislav Petkov <[email protected]>
CommitterDate: Wed, 09 Sep 2020 11:33:20 +02:00

x86/sev-es: Handle #AC Events

Implement a handler for #VC exceptions caused by #AC exceptions. The #AC exception is just forwarded to do_alignment_check() and not pushed down to the hypervisor, as requested by the SEV-ES GHCB Standardization Specification.

---
 arch/x86/kernel/sev-es.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
arch/x86/kernel/sev-es.c +19 -0
index 86cb4c5..8867c48 100644
@@ -909,6 +909,19 @@ static enum es_result vc_handle_vmmcall(struct ghcb *ghcb,
 	return ES_OK;
 }
 
+static enum es_result vc_handle_trap_ac(struct ghcb *ghcb,
+					struct es_em_ctxt *ctxt)
+{
+	/*
+	 * Calling ecx_alignment_check() directly does not work, because it
+	 * enables IRQs and the GHCB is active. Forward the exception and call
+	 * it later from vc_forward_exception().
+	 */
+	ctxt->fi.vector = X86_TRAP_AC;
+	ctxt->fi.error_code = 0;
+	return ES_EXCEPTION;
+}
+
 static enum es_result vc_handle_exitcode(struct es_em_ctxt *ctxt,
 					 struct ghcb *ghcb,
 					 unsigned long exit_code)
@@ -922,6 +935,9 @@ static enum es_result vc_handle_exitcode(struct es_em_ctxt *ctxt,
 	case SVM_EXIT_WRITE_DR7:
 		result = vc_handle_dr7_write(ghcb, ctxt);
 		break;
+	case SVM_EXIT_EXCP_BASE + X86_TRAP_AC:
+		result = vc_handle_trap_ac(ghcb, ctxt);
+		break;
 	case SVM_EXIT_RDTSC:
 	case SVM_EXIT_RDTSCP:
 		result = vc_handle_rdtsc(ghcb, ctxt, exit_code);
@@ -981,6 +997,9 @@ static __always_inline void vc_forward_exception(struct es_em_ctxt *ctxt)
 	case X86_TRAP_UD:
 		exc_invalid_op(ctxt->regs);
 		break;
+	case X86_TRAP_AC:
+		exc_alignment_check(ctxt->regs, error_code);
+		break;
 	default:
 		pr_emerg("Unsupported exception in #VC instruction emulation - can't continue\n");
 		BUG();
#135 lore raw

PATCH v7 61/72 x86/sev-es: Handle #DB Events

In reply to Joerg Roedel

From: Joerg Roedel <[email protected]>

Handle #VC exceptions caused by #DB exceptions in the guest. Those must be handled outside of instrumentation_begin()/end() so that the handler will not be raised recursively.

Handle them by calling the kernels debug exception handler.

---
 arch/x86/kernel/sev-es.c | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)
arch/x86/kernel/sev-es.c +17 -0
index f78ab9369b9c..3eea4e910fc1 100644
@@ -922,6 +922,14 @@ static enum es_result vc_handle_trap_ac(struct ghcb *ghcb,
 	return ES_EXCEPTION;
 }
 
+static __always_inline void vc_handle_trap_db(struct pt_regs *regs)
+{
+	if (user_mode(regs))
+		noist_exc_debug(regs);
+	else
+		exc_debug(regs);
+}
+
 static enum es_result vc_handle_exitcode(struct es_em_ctxt *ctxt,
 					 struct ghcb *ghcb,
 					 unsigned long exit_code)
@@ -1033,6 +1041,15 @@ DEFINE_IDTENTRY_VC_SAFE_STACK(exc_vmm_communication)
 	struct ghcb *ghcb;
 
 	lockdep_assert_irqs_disabled();
+
+	/*
+	 * Handle #DB before calling into !noinstr code to avoid recursive #DB.
+	 */
+	if (error_code == SVM_EXIT_EXCP_BASE + X86_TRAP_DB) {
+		vc_handle_trap_db(regs);
+		return;
+	}
+
 	instrumentation_begin();
 
 	/*
Signature · 6 lines
2.28.0

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
merge bot Applied
#136 lore raw

[tip: x86/seves] x86/sev-es: Handle #DB Events

The following commit has been merged into the x86/seves branch of tip:

The following commit has been merged into the x86/seves branch of tip:

Commit-ID:     cb1ad3ecea959593400dfac4f027dbc005e62c39
Gitweb:        https://git.kernel.org/tip/cb1ad3ecea959593400dfac4f027dbc005e62c39
Author:        Joerg Roedel <[email protected]>
AuthorDate:    Mon, 07 Sep 2020 15:16:02 +02:00
Committer:     Borislav Petkov <[email protected]>
CommitterDate: Wed, 09 Sep 2020 11:33:20 +02:00

x86/sev-es: Handle #DB Events

Handle #VC exceptions caused by #DB exceptions in the guest. Those must be handled outside of instrumentation_begin()/end() so that the handler will not be raised recursively.

Handle them by calling the kernel's debug exception handler.

---
 arch/x86/kernel/sev-es.c | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)
arch/x86/kernel/sev-es.c +17 -0
index 8867c48..79d5190 100644
@@ -922,6 +922,14 @@ static enum es_result vc_handle_trap_ac(struct ghcb *ghcb,
 	return ES_EXCEPTION;
 }
 
+static __always_inline void vc_handle_trap_db(struct pt_regs *regs)
+{
+	if (user_mode(regs))
+		noist_exc_debug(regs);
+	else
+		exc_debug(regs);
+}
+
 static enum es_result vc_handle_exitcode(struct es_em_ctxt *ctxt,
 					 struct ghcb *ghcb,
 					 unsigned long exit_code)
@@ -1033,6 +1041,15 @@ DEFINE_IDTENTRY_VC_SAFE_STACK(exc_vmm_communication)
 	struct ghcb *ghcb;
 
 	lockdep_assert_irqs_disabled();
+
+	/*
+	 * Handle #DB before calling into !noinstr code to avoid recursive #DB.
+	 */
+	if (error_code == SVM_EXIT_EXCP_BASE + X86_TRAP_DB) {
+		vc_handle_trap_db(regs);
+		return;
+	}
+
 	instrumentation_begin();
 
 	/*
#137 lore raw

PATCH v7 62/72 x86/paravirt: Allow hypervisor specific VMMCALL handling under SEV-ES

In reply to Joerg Roedel

From: Joerg Roedel <[email protected]>

Add two new paravirt callbacks to provide hypervisor specific processor state in the GHCB and to copy state from the hypervisor back to the processor.

---
 arch/x86/include/asm/x86_init.h | 16 +++++++++++++++-
 arch/x86/kernel/sev-es.c        | 12 ++++++++++++
 2 files changed, 27 insertions(+), 1 deletion(-)
arch/x86/include/asm/x86_init.h +15 -1
index 6807153c0410..0304e2931cd3 100644
@@ -4,8 +4,10 @@
 
 #include <asm/bootparam.h>
 
+struct ghcb;
 struct mpc_bus;
 struct mpc_cpu;
+struct pt_regs;
 struct mpc_table;
 struct cpuinfo_x86;
 
@@ -236,10 +238,22 @@ struct x86_legacy_features {
 /**
  * struct x86_hyper_runtime - x86 hypervisor specific runtime callbacks
  *
- * @pin_vcpu:		pin current vcpu to specified physical cpu (run rarely)
+ * @pin_vcpu:			pin current vcpu to specified physical
+ *				cpu (run rarely)
+ * @sev_es_hcall_prepare:	Load additional hypervisor-specific
+ *				state into the GHCB when doing a VMMCALL under
+ *				SEV-ES. Called from the #VC exception handler.
+ * @sev_es_hcall_finish:	Copies state from the GHCB back into the
+ *				processor (or pt_regs). Also runs checks on the
+ *				state returned from the hypervisor after a
+ *				VMMCALL under SEV-ES.  Needs to return 'false'
+ *				if the checks fail.  Called from the #VC
+ *				exception handler.
  */
 struct x86_hyper_runtime {
 	void (*pin_vcpu)(int cpu);
+	void (*sev_es_hcall_prepare)(struct ghcb *ghcb, struct pt_regs *regs);
+	bool (*sev_es_hcall_finish)(struct ghcb *ghcb, struct pt_regs *regs);
 };
 
 /**
arch/x86/kernel/sev-es.c +12 -0
index 3eea4e910fc1..9814ac99aea1 100644
@@ -897,6 +897,9 @@ static enum es_result vc_handle_vmmcall(struct ghcb *ghcb,
 	ghcb_set_rax(ghcb, ctxt->regs->ax);
 	ghcb_set_cpl(ghcb, user_mode(ctxt->regs) ? 3 : 0);
 
+	if (x86_platform.hyper.sev_es_hcall_prepare)
+		x86_platform.hyper.sev_es_hcall_prepare(ghcb, ctxt->regs);
+
 	ret = sev_es_ghcb_hv_call(ghcb, ctxt, SVM_EXIT_VMMCALL, 0, 0);
 	if (ret != ES_OK)
 		return ret;
@@ -906,6 +909,15 @@ static enum es_result vc_handle_vmmcall(struct ghcb *ghcb,
 
 	ctxt->regs->ax = ghcb->save.rax;
 
+	/*
+	 * Call sev_es_hcall_finish() after regs->ax is already set.
+	 * This allows the hypervisor handler to overwrite it again if
+	 * necessary.
+	 */
+	if (x86_platform.hyper.sev_es_hcall_finish &&
+	    !x86_platform.hyper.sev_es_hcall_finish(ghcb, ctxt->regs))
+		return ES_VMM_ERROR;
+
 	return ES_OK;
 }
 
Signature · 6 lines
2.28.0

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
merge bot Applied
#138 lore raw

[tip: x86/seves] x86/paravirt: Allow hypervisor-specific VMMCALL handling under SEV-ES

The following commit has been merged into the x86/seves branch of tip:

The following commit has been merged into the x86/seves branch of tip:

Commit-ID:     f6a9f8a45810d2914ea422ff39bfe2e0251c50f2
Gitweb:        https://git.kernel.org/tip/f6a9f8a45810d2914ea422ff39bfe2e0251c50f2
Author:        Joerg Roedel <[email protected]>
AuthorDate:    Mon, 07 Sep 2020 15:16:03 +02:00
Committer:     Borislav Petkov <[email protected]>
CommitterDate: Wed, 09 Sep 2020 11:33:20 +02:00

x86/paravirt: Allow hypervisor-specific VMMCALL handling under SEV-ES

Add two new paravirt callbacks to provide hypervisor-specific processor state in the GHCB and to copy state from the hypervisor back to the processor.

---
 arch/x86/include/asm/x86_init.h | 16 +++++++++++++++-
 arch/x86/kernel/sev-es.c        | 12 ++++++++++++
 2 files changed, 27 insertions(+), 1 deletion(-)
arch/x86/include/asm/x86_init.h +15 -1
index 6807153..0304e29 100644
@@ -4,8 +4,10 @@
 
 #include <asm/bootparam.h>
 
+struct ghcb;
 struct mpc_bus;
 struct mpc_cpu;
+struct pt_regs;
 struct mpc_table;
 struct cpuinfo_x86;
 
@@ -236,10 +238,22 @@ struct x86_legacy_features {
 /**
  * struct x86_hyper_runtime - x86 hypervisor specific runtime callbacks
  *
- * @pin_vcpu:		pin current vcpu to specified physical cpu (run rarely)
+ * @pin_vcpu:			pin current vcpu to specified physical
+ *				cpu (run rarely)
+ * @sev_es_hcall_prepare:	Load additional hypervisor-specific
+ *				state into the GHCB when doing a VMMCALL under
+ *				SEV-ES. Called from the #VC exception handler.
+ * @sev_es_hcall_finish:	Copies state from the GHCB back into the
+ *				processor (or pt_regs). Also runs checks on the
+ *				state returned from the hypervisor after a
+ *				VMMCALL under SEV-ES.  Needs to return 'false'
+ *				if the checks fail.  Called from the #VC
+ *				exception handler.
  */
 struct x86_hyper_runtime {
 	void (*pin_vcpu)(int cpu);
+	void (*sev_es_hcall_prepare)(struct ghcb *ghcb, struct pt_regs *regs);
+	bool (*sev_es_hcall_finish)(struct ghcb *ghcb, struct pt_regs *regs);
 };
 
 /**
arch/x86/kernel/sev-es.c +12 -0
index 79d5190..6d89df9 100644
@@ -897,6 +897,9 @@ static enum es_result vc_handle_vmmcall(struct ghcb *ghcb,
 	ghcb_set_rax(ghcb, ctxt->regs->ax);
 	ghcb_set_cpl(ghcb, user_mode(ctxt->regs) ? 3 : 0);
 
+	if (x86_platform.hyper.sev_es_hcall_prepare)
+		x86_platform.hyper.sev_es_hcall_prepare(ghcb, ctxt->regs);
+
 	ret = sev_es_ghcb_hv_call(ghcb, ctxt, SVM_EXIT_VMMCALL, 0, 0);
 	if (ret != ES_OK)
 		return ret;
@@ -906,6 +909,15 @@ static enum es_result vc_handle_vmmcall(struct ghcb *ghcb,
 
 	ctxt->regs->ax = ghcb->save.rax;
 
+	/*
+	 * Call sev_es_hcall_finish() after regs->ax is already set.
+	 * This allows the hypervisor handler to overwrite it again if
+	 * necessary.
+	 */
+	if (x86_platform.hyper.sev_es_hcall_finish &&
+	    !x86_platform.hyper.sev_es_hcall_finish(ghcb, ctxt->regs))
+		return ES_VMM_ERROR;
+
 	return ES_OK;
 }
 
#139 lore raw

PATCH v7 63/72 x86/kvm: Add KVM specific VMMCALL handling under SEV-ES

In reply to Joerg Roedel

From: Tom Lendacky <[email protected]>

Implement the callbacks to copy the processor state required by KVM to the GHCB.

[ [email protected]: - Split out of a larger patch
- Adapt to different callback functions ]

---
 arch/x86/kernel/kvm.c | 35 +++++++++++++++++++++++++++++------
 1 file changed, 29 insertions(+), 6 deletions(-)
arch/x86/kernel/kvm.c +29 -6
index 08320b0b2b27..0f9597275e9c 100644
@@ -36,6 +36,8 @@
 #include <asm/hypervisor.h>
 #include <asm/tlb.h>
 #include <asm/cpuidle_haltpoll.h>
+#include <asm/ptrace.h>
+#include <asm/svm.h>
 
 DEFINE_STATIC_KEY_FALSE(kvm_async_pf_enabled);
 
@@ -746,13 +748,34 @@ static void __init kvm_init_platform(void)
 	x86_platform.apic_post_init = kvm_apic_init;
 }
 
+#if defined(CONFIG_AMD_MEM_ENCRYPT)
+static void kvm_sev_es_hcall_prepare(struct ghcb *ghcb, struct pt_regs *regs)
+{
+	/* RAX and CPL are already in the GHCB */
+	ghcb_set_rbx(ghcb, regs->bx);
+	ghcb_set_rcx(ghcb, regs->cx);
+	ghcb_set_rdx(ghcb, regs->dx);
+	ghcb_set_rsi(ghcb, regs->si);
+}
+
+static bool kvm_sev_es_hcall_finish(struct ghcb *ghcb, struct pt_regs *regs)
+{
+	/* No checking of the return state needed */
+	return true;
+}
+#endif
+
 const __initconst struct hypervisor_x86 x86_hyper_kvm = {
-	.name			= "KVM",
-	.detect			= kvm_detect,
-	.type			= X86_HYPER_KVM,
-	.init.guest_late_init	= kvm_guest_init,
-	.init.x2apic_available	= kvm_para_available,
-	.init.init_platform	= kvm_init_platform,
+	.name				= "KVM",
+	.detect				= kvm_detect,
+	.type				= X86_HYPER_KVM,
+	.init.guest_late_init		= kvm_guest_init,
+	.init.x2apic_available		= kvm_para_available,
+	.init.init_platform		= kvm_init_platform,
+#if defined(CONFIG_AMD_MEM_ENCRYPT)
+	.runtime.sev_es_hcall_prepare	= kvm_sev_es_hcall_prepare,
+	.runtime.sev_es_hcall_finish	= kvm_sev_es_hcall_finish,
+#endif
 };
 
 static __init int activate_jump_labels(void)
Signature · 6 lines
2.28.0

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
merge bot Applied
#140 lore raw

[tip: x86/seves] x86/kvm: Add KVM-specific VMMCALL handling under SEV-ES

The following commit has been merged into the x86/seves branch of tip:

The following commit has been merged into the x86/seves branch of tip:

Commit-ID:     99419b251e5427b89dbfae103d8a2f469efaa4b2
Gitweb:        https://git.kernel.org/tip/99419b251e5427b89dbfae103d8a2f469efaa4b2
Author:        Tom Lendacky <[email protected]>
AuthorDate:    Mon, 07 Sep 2020 15:16:04 +02:00
Committer:     Borislav Petkov <[email protected]>
CommitterDate: Wed, 09 Sep 2020 11:33:20 +02:00

x86/kvm: Add KVM-specific VMMCALL handling under SEV-ES

Implement the callbacks to copy the processor state required by KVM to the GHCB.

[ [email protected]: - Split out of a larger patch
- Adapt to different callback functions ]

---
 arch/x86/kernel/kvm.c | 35 +++++++++++++++++++++++++++++------
 1 file changed, 29 insertions(+), 6 deletions(-)
arch/x86/kernel/kvm.c +29 -6
index 08320b0..0f95972 100644
@@ -36,6 +36,8 @@
 #include <asm/hypervisor.h>
 #include <asm/tlb.h>
 #include <asm/cpuidle_haltpoll.h>
+#include <asm/ptrace.h>
+#include <asm/svm.h>
 
 DEFINE_STATIC_KEY_FALSE(kvm_async_pf_enabled);
 
@@ -746,13 +748,34 @@ static void __init kvm_init_platform(void)
 	x86_platform.apic_post_init = kvm_apic_init;
 }
 
+#if defined(CONFIG_AMD_MEM_ENCRYPT)
+static void kvm_sev_es_hcall_prepare(struct ghcb *ghcb, struct pt_regs *regs)
+{
+	/* RAX and CPL are already in the GHCB */
+	ghcb_set_rbx(ghcb, regs->bx);
+	ghcb_set_rcx(ghcb, regs->cx);
+	ghcb_set_rdx(ghcb, regs->dx);
+	ghcb_set_rsi(ghcb, regs->si);
+}
+
+static bool kvm_sev_es_hcall_finish(struct ghcb *ghcb, struct pt_regs *regs)
+{
+	/* No checking of the return state needed */
+	return true;
+}
+#endif
+
 const __initconst struct hypervisor_x86 x86_hyper_kvm = {
-	.name			= "KVM",
-	.detect			= kvm_detect,
-	.type			= X86_HYPER_KVM,
-	.init.guest_late_init	= kvm_guest_init,
-	.init.x2apic_available	= kvm_para_available,
-	.init.init_platform	= kvm_init_platform,
+	.name				= "KVM",
+	.detect				= kvm_detect,
+	.type				= X86_HYPER_KVM,
+	.init.guest_late_init		= kvm_guest_init,
+	.init.x2apic_available		= kvm_para_available,
+	.init.init_platform		= kvm_init_platform,
+#if defined(CONFIG_AMD_MEM_ENCRYPT)
+	.runtime.sev_es_hcall_prepare	= kvm_sev_es_hcall_prepare,
+	.runtime.sev_es_hcall_finish	= kvm_sev_es_hcall_finish,
+#endif
 };
 
 static __init int activate_jump_labels(void)

A message that is not in the archive (sent off-list or not yet received): CAAYXXYx=Eq4gYfUqdO7u37VRD_GpPYFQgN=GZySmAMcDc2AM=g@mail.gmail.com

#142 lore raw

Re: [tip: x86/seves] x86/kvm: Add KVM-specific VMMCALL handling under SEV-ES

[resending in plain/text, sorry for double sending]

It seems to me that the kvm_sev_es_hcall_prepare is leaking more information than it is needed. Is this an expected behavior? -Erdem

Quoted 82 lines · On Thu, Sep 10, 2020 at 2:39 AM tip-bot2 for Tom Lendacky…
Quoted 80 lines

The following commit has been merged into the x86/seves branch of tip:

Commit-ID:     99419b251e5427b89dbfae103d8a2f469efaa4b2
Gitweb:        https://git.kernel.org/tip/99419b251e5427b89dbfae103d8a2f469efaa4b2
Author:        Tom Lendacky <[email protected]>
AuthorDate:    Mon, 07 Sep 2020 15:16:04 +02:00
Committer:     Borislav Petkov <[email protected]>
CommitterDate: Wed, 09 Sep 2020 11:33:20 +02:00

x86/kvm: Add KVM-specific VMMCALL handling under SEV-ES

Implement the callbacks to copy the processor state required by KVM to the GHCB.

[ [email protected]: - Split out of a larger patch
- Adapt to different callback functions ]

---
 arch/x86/kernel/kvm.c | 35 +++++++++++++++++++++++++++++------
 1 file changed, 29 insertions(+), 6 deletions(-)
arch/x86/kernel/kvm.c +29 -6
index 08320b0..0f95972 100644
@@ -36,6 +36,8 @@
 #include <asm/hypervisor.h>
 #include <asm/tlb.h>
 #include <asm/cpuidle_haltpoll.h>
+#include <asm/ptrace.h>
+#include <asm/svm.h>
 
 DEFINE_STATIC_KEY_FALSE(kvm_async_pf_enabled);
 
@@ -746,13 +748,34 @@ static void __init kvm_init_platform(void)
        x86_platform.apic_post_init = kvm_apic_init;
 }
 
+#if defined(CONFIG_AMD_MEM_ENCRYPT)
+static void kvm_sev_es_hcall_prepare(struct ghcb *ghcb, struct pt_regs *regs)
+{
+       /* RAX and CPL are already in the GHCB */
+       ghcb_set_rbx(ghcb, regs->bx);
+       ghcb_set_rcx(ghcb, regs->cx);
+       ghcb_set_rdx(ghcb, regs->dx);
+       ghcb_set_rsi(ghcb, regs->si);
+}
+
+static bool kvm_sev_es_hcall_finish(struct ghcb *ghcb, struct pt_regs *regs)
+{
+       /* No checking of the return state needed */
+       return true;
+}
+#endif
+
 const __initconst struct hypervisor_x86 x86_hyper_kvm = {
-       .name                   = "KVM",
-       .detect                 = kvm_detect,
-       .type                   = X86_HYPER_KVM,
-       .init.guest_late_init   = kvm_guest_init,
-       .init.x2apic_available  = kvm_para_available,
-       .init.init_platform     = kvm_init_platform,
+       .name                           = "KVM",
+       .detect                         = kvm_detect,
+       .type                           = X86_HYPER_KVM,
+       .init.guest_late_init           = kvm_guest_init,
+       .init.x2apic_available          = kvm_para_available,
+       .init.init_platform             = kvm_init_platform,
+#if defined(CONFIG_AMD_MEM_ENCRYPT)
+       .runtime.sev_es_hcall_prepare   = kvm_sev_es_hcall_prepare,
+       .runtime.sev_es_hcall_finish    = kvm_sev_es_hcall_finish,
+#endif
 };
 
 static __init int activate_jump_labels(void)
#143 lore raw

Re: [tip: x86/seves] x86/kvm: Add KVM-specific VMMCALL handling under SEV-ES

Quoted 2 lines · Erdem Aktas

It seems to me that the kvm_sev_es_hcall_prepare is leaking more information than it is needed. Is this an expected behavior?

What exactly is leaked? The kvm hypercall uses RAX, RBX, RCX, RDX and RSI for parameters.

Regards,

Joerg

#144 lore raw

Re: [tip: x86/seves] x86/kvm: Add KVM-specific VMMCALL handling under SEV-ES

I might be missing something here but I think what you say is only correct for the kvm_hypercall4 cases. All other functions use a smaller number of registers. #VC blindly assumes that all those registers are used in the vmcall and exposes them. Here are some examples:

For example in the kvm_hypercall2 only rax, rbx, and rcx should be exposed. apic_id address is leaked with rdx when this hypercall is used in kvm_kick_cpu function. RSI is never used. I am not sure what value will be exposed to VMM in this case:

54 static inline long kvm_hypercall2(unsigned int nr, unsigned long p1,
55 unsigned long p2)
56 {
57 long ret;
58 asm volatile(KVM_HYPERCALL

 59                      : "=a"(ret)
 60                      : "a"(nr), "b"(p1), "c"(p2)
 61                      : "memory");

62 return ret;
63 }
And this function is called in :

820 static void kvm_kick_cpu(int cpu)
821 {
822 int apicid;
823 unsigned long flags = 0;
824
825 apicid = per_cpu(x86_cpu_to_apicid, cpu);
826 kvm_hypercall2(KVM_HC_KICK_CPU, flags, apicid);
827 }

looking to what it is compiled in my machine :

151215 ffffffff8105def0 <kvm_kick_cpu>:
151216 {
151217 ffffffff8105def0: e8 fb 9e ff ff callq
ffffffff81057df0 <__fentry__>
151218 apicid = per_cpu(x86_cpu_to_apicid, cpu);
151219 ffffffff8105def5: 48 63 ff movslq %edi,%rdi
151220 {
151221 ffffffff8105def8: 53 push %rbx
151222 apicid = per_cpu(x86_cpu_to_apicid, cpu);
151223 ffffffff8105def9: 48 c7 c0 58 16 01 00 mov $0x11658,%rax
151224
151225 static inline long kvm_hypercall2(unsigned int nr, unsigned long p1,
151226 unsigned long p2)
151227 {
151228 long ret;
151229 asm volatile(KVM_HYPERCALL

 151230 ffffffff8105df00:       31 db                   xor    %ebx,%ebx
 151231 ffffffff8105df02:       48 8b 14 fd 00 19 cb    mov

-0x7e34e700(,%rdi,8),%rdx
151232 ffffffff8105df09: 81
151233 kvm_hypercall2(KVM_HC_KICK_CPU, flags, apicid);
151234 ffffffff8105df0a: 0f b7 0c 02 movzwl
(%rdx,%rax,1),%ecx

 151235 ffffffff8105df0e:       b8 05 00 00 00          mov    $0x5,%eax
 151236 ffffffff8105df13:       0f 01 c1                vmcall
 151237 }
 151238 ffffffff8105df16:       5b                      pop    %rbx
 151239 ffffffff8105df17:       c3                      retq

Similarly kvm_hypercall1 only need 2 registers to expose:

44 static inline long kvm_hypercall1(unsigned int nr, unsigned long p1) 45 {
46 long ret;
47 asm volatile(KVM_HYPERCALL

 48                      : "=a"(ret)
 49                      : "a"(nr), "b"(p1)
 50                      : "memory");

51 return ret;
52 }

And an example where it is used:

562 static void kvm_smp_send_call_func_ipi(const struct cpumask *mask) 563 {
564 int cpu;
565
566 native_send_call_func_ipi(mask);
567
568 /* Make sure other vCPUs get a chance to run if they need to. */
569 for_each_cpu(cpu, mask) {
570 if (vcpu_is_preempted(cpu)) {
571 kvm_hypercall1(KVM_HC_SCHED_YIELD,
per_cpu(x86_cpu_to_apicid, cpu));
572 break;
573 }
574 }
575 }

If we look at the function decompiled in my platform, here x86_cpu_to_apicid address is leaked in rdx. RSI also leaks some information from kvm_smp_send_call_function_ipi function. RCX is not used so it might include something from a higher caller.

151243 ffffffff8105df20 <kvm_smp_send_call_func_ipi>:
151244 {
151245 ffffffff8105df20: e8 cb 9e ff ff callq
ffffffff81057df0 <__fentry__>

 151246 ffffffff8105df25:       53                      push   %rbx
 151247 ffffffff8105df26:       48 89 fb                mov    %rdi,%rbx

151248 native_send_call_func_ipi(mask);
151249 ffffffff8105df29: e8 a2 45 ff ff callq
ffffffff810524d0 <native_send_call_func_ipi>
151250 for_each_cpu(cpu, mask) {

 151251 ffffffff8105df2e:       41 b8 ff ff ff ff       mov    $0xffffffff,%r8d
 151252 ffffffff8105df34:       eb 0e                   jmp
ffffffff8105df44 <kvm_smp_send_call_func_ipi+0x24>
 151253         return PVOP_CALLEE1(bool, lock.vcpu_is_preempted, cpu);
 151254 ffffffff8105df36:       49 63 f8                movslq %r8d,%rdi
 151255 ffffffff8105df39:       ff 14 25 90 93 02 82    callq
*0xffffffff82029390
 151256                 if (vcpu_is_preempted(cpu)) {
 151257 ffffffff8105df40:       84 c0                   test   %al,%al
 151258 ffffffff8105df42:       75 18                   jne
ffffffff8105df5c <kvm_smp_send_call_func_ipi+0x3c>
 151259         for_each_cpu(cpu, mask) {
 151260 ffffffff8105df44:       44 89 c7                mov    %r8d,%edi
 151261 ffffffff8105df47:       48 89 de                mov    %rbx,%rsi
 151262 ffffffff8105df4a:       e8 61 44 39 00          callq
ffffffff813f23b0 <cpumask_next>
 151263 ffffffff8105df4f:       3b 05 2f 7e 12 01       cmp
0x1127e2f(%rip),%eax        # ffffffff82185d84 <nr_cpu_ids>
 151264 ffffffff8105df55:       41 89 c0                mov    %eax,%r8d
 151265 ffffffff8105df58:       72 dc                   jb
ffffffff8105df36 <kvm_smp_send_call_func_ipi+0x16>
 151266 }
 151267 ffffffff8105df5a:       5b                      pop    %rbx
 151268 ffffffff8105df5b:       c3                      retq
 151269                         kvm_hypercall1(KVM_HC_SCHED_YIELD,
per_cpu(x86_cpu_to_apicid, cpu));
 151270 ffffffff8105df5c:       48 8b 14 fd 00 19 cb    mov
-0x7e34e700(,%rdi,8),%rdx
 151271 ffffffff8105df63:       81
 151272 ffffffff8105df64:       48 c7 c0 58 16 01 00    mov    $0x11658,%rax
 151273 ffffffff8105df6b:       0f b7 1c 02             movzwl
(%rdx,%rax,1),%ebx
 151274         asm volatile(KVM_HYPERCALL
 151275 ffffffff8105df6f:       b8 0b 00 00 00          mov    $0xb,%eax
 151276 ffffffff8105df74:       0f 01 c1                vmcall

151277 }

I am not sure how those leaked registers can be used, but depending on which function call hypercall[0-3], there will be some leak.

-Erdem

Quoted 11 lines · Joerg Roedel
Quoted 2 lines · Erdem Aktas

It seems to me that the kvm_sev_es_hcall_prepare is leaking more information than it is needed. Is this an expected behavior?

What exactly is leaked? The kvm hypercall uses RAX, RBX, RCX, RDX and RSI for parameters.

Regards,

Joerg

#145 lore raw

Re: [tip: x86/seves] x86/kvm: Add KVM-specific VMMCALL handling under SEV-ES

Quoted 4 lines · Erdem Aktas

I might be missing something here but I think what you say is only correct for the kvm_hypercall4 cases. All other functions use a smaller number of registers. #VC blindly assumes that all those registers are used in the vmcall and exposes them.

Right, I think we should fix that in the guest and zero out the unused registers. VMMCALL can come from userspace after all, and the #VC handler does not look at the hypercall numbers.

Further, on the host side KVM will unconditionally read out all 4 registers too, which requires us to set them valid in the GHCB.

Regards,

Joerg

#146 lore raw

PATCH v7 64/72 x86/vmware: Add VMware specific handling for VMMCALL under SEV-ES

In reply to Joerg Roedel

From: Doug Covelli <[email protected]>

Add VMware specific handling for #VC faults caused by VMMCALL instructions.

[ [email protected]: - Adapt to different paravirt interface ]
---
 arch/x86/kernel/cpu/vmware.c | 50 ++++++++++++++++++++++++++++++++----
 1 file changed, 45 insertions(+), 5 deletions(-)
arch/x86/kernel/cpu/vmware.c +45 -5
index 9b6fafa69be9..924571fe5864 100644
@@ -33,6 +33,7 @@
 #include <asm/timer.h>
 #include <asm/apic.h>
 #include <asm/vmware.h>
+#include <asm/svm.h>
 
 #undef pr_fmt
 #define pr_fmt(fmt)	"vmware: " fmt
@@ -476,10 +477,49 @@ static bool __init vmware_legacy_x2apic_available(void)
 	       (eax & (1 << VMWARE_CMD_LEGACY_X2APIC)) != 0;
 }
 
+#ifdef CONFIG_AMD_MEM_ENCRYPT
+static void vmware_sev_es_hcall_prepare(struct ghcb *ghcb,
+					struct pt_regs *regs)
+{
+	/* Copy VMWARE specific Hypercall parameters to the GHCB */
+	ghcb_set_rip(ghcb, regs->ip);
+	ghcb_set_rbx(ghcb, regs->bx);
+	ghcb_set_rcx(ghcb, regs->cx);
+	ghcb_set_rdx(ghcb, regs->dx);
+	ghcb_set_rsi(ghcb, regs->si);
+	ghcb_set_rdi(ghcb, regs->di);
+	ghcb_set_rbp(ghcb, regs->bp);
+}
+
+static bool vmware_sev_es_hcall_finish(struct ghcb *ghcb, struct pt_regs *regs)
+{
+	if (!(ghcb_rbx_is_valid(ghcb) &&
+	      ghcb_rcx_is_valid(ghcb) &&
+	      ghcb_rdx_is_valid(ghcb) &&
+	      ghcb_rsi_is_valid(ghcb) &&
+	      ghcb_rdi_is_valid(ghcb) &&
+	      ghcb_rbp_is_valid(ghcb)))
+		return false;
+
+	regs->bx = ghcb->save.rbx;
+	regs->cx = ghcb->save.rcx;
+	regs->dx = ghcb->save.rdx;
+	regs->si = ghcb->save.rsi;
+	regs->di = ghcb->save.rdi;
+	regs->bp = ghcb->save.rbp;
+
+	return true;
+}
+#endif
+
 const __initconst struct hypervisor_x86 x86_hyper_vmware = {
-	.name			= "VMware",
-	.detect			= vmware_platform,
-	.type			= X86_HYPER_VMWARE,
-	.init.init_platform	= vmware_platform_setup,
-	.init.x2apic_available	= vmware_legacy_x2apic_available,
+	.name				= "VMware",
+	.detect				= vmware_platform,
+	.type				= X86_HYPER_VMWARE,
+	.init.init_platform		= vmware_platform_setup,
+	.init.x2apic_available		= vmware_legacy_x2apic_available,
+#ifdef CONFIG_AMD_MEM_ENCRYPT
+	.runtime.sev_es_hcall_prepare	= vmware_sev_es_hcall_prepare,
+	.runtime.sev_es_hcall_finish	= vmware_sev_es_hcall_finish,
+#endif
 };
Signature · 6 lines
2.28.0

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
merge bot Applied
#147 lore raw

[tip: x86/seves] x86/vmware: Add VMware-specific handling for VMMCALL under SEV-ES

The following commit has been merged into the x86/seves branch of tip:

The following commit has been merged into the x86/seves branch of tip:

Commit-ID:     1a222de8dcfb903d039810b0823570ee0be4e6c6
Gitweb:        https://git.kernel.org/tip/1a222de8dcfb903d039810b0823570ee0be4e6c6
Author:        Doug Covelli <[email protected]>
AuthorDate:    Mon, 07 Sep 2020 15:16:05 +02:00
Committer:     Borislav Petkov <[email protected]>
CommitterDate: Wed, 09 Sep 2020 11:33:20 +02:00

x86/vmware: Add VMware-specific handling for VMMCALL under SEV-ES

Add VMware-specific handling for #VC faults caused by VMMCALL instructions.

---
 arch/x86/kernel/cpu/vmware.c | 50 +++++++++++++++++++++++++++++++----
 1 file changed, 45 insertions(+), 5 deletions(-)
arch/x86/kernel/cpu/vmware.c +45 -5
index 9b6fafa..924571f 100644
@@ -33,6 +33,7 @@
 #include <asm/timer.h>
 #include <asm/apic.h>
 #include <asm/vmware.h>
+#include <asm/svm.h>
 
 #undef pr_fmt
 #define pr_fmt(fmt)	"vmware: " fmt
@@ -476,10 +477,49 @@ static bool __init vmware_legacy_x2apic_available(void)
 	       (eax & (1 << VMWARE_CMD_LEGACY_X2APIC)) != 0;
 }
 
+#ifdef CONFIG_AMD_MEM_ENCRYPT
+static void vmware_sev_es_hcall_prepare(struct ghcb *ghcb,
+					struct pt_regs *regs)
+{
+	/* Copy VMWARE specific Hypercall parameters to the GHCB */
+	ghcb_set_rip(ghcb, regs->ip);
+	ghcb_set_rbx(ghcb, regs->bx);
+	ghcb_set_rcx(ghcb, regs->cx);
+	ghcb_set_rdx(ghcb, regs->dx);
+	ghcb_set_rsi(ghcb, regs->si);
+	ghcb_set_rdi(ghcb, regs->di);
+	ghcb_set_rbp(ghcb, regs->bp);
+}
+
+static bool vmware_sev_es_hcall_finish(struct ghcb *ghcb, struct pt_regs *regs)
+{
+	if (!(ghcb_rbx_is_valid(ghcb) &&
+	      ghcb_rcx_is_valid(ghcb) &&
+	      ghcb_rdx_is_valid(ghcb) &&
+	      ghcb_rsi_is_valid(ghcb) &&
+	      ghcb_rdi_is_valid(ghcb) &&
+	      ghcb_rbp_is_valid(ghcb)))
+		return false;
+
+	regs->bx = ghcb->save.rbx;
+	regs->cx = ghcb->save.rcx;
+	regs->dx = ghcb->save.rdx;
+	regs->si = ghcb->save.rsi;
+	regs->di = ghcb->save.rdi;
+	regs->bp = ghcb->save.rbp;
+
+	return true;
+}
+#endif
+
 const __initconst struct hypervisor_x86 x86_hyper_vmware = {
-	.name			= "VMware",
-	.detect			= vmware_platform,
-	.type			= X86_HYPER_VMWARE,
-	.init.init_platform	= vmware_platform_setup,
-	.init.x2apic_available	= vmware_legacy_x2apic_available,
+	.name				= "VMware",
+	.detect				= vmware_platform,
+	.type				= X86_HYPER_VMWARE,
+	.init.init_platform		= vmware_platform_setup,
+	.init.x2apic_available		= vmware_legacy_x2apic_available,
+#ifdef CONFIG_AMD_MEM_ENCRYPT
+	.runtime.sev_es_hcall_prepare	= vmware_sev_es_hcall_prepare,
+	.runtime.sev_es_hcall_finish	= vmware_sev_es_hcall_finish,
+#endif
 };
#148 lore raw

Re: [tip: x86/seves] x86/vmware: Add VMware-specific handling for VMMCALL under SEV-ES

Looking at the VMWARE_VMCALL(cmd, eax, ebx, ecx, edx) definition, it seems to me only 4 registers are required to be shared with hypervisor. I don't know much about vmware but is not vmware_sev_es_hcall_prepare expose more registers than needed and also vmware_sev_es_hcall_finish might let the hypvervisor to modify additional registers which are not used?

Just checking if this is intentional and what I am missing here.

Thanks
-Erdem

Quoted 93 lines · On Thu, Sep 10, 2020 at 2:23 AM tip-bot2 for Doug Covelli…

The following commit has been merged into the x86/seves branch of tip:

Commit-ID:     1a222de8dcfb903d039810b0823570ee0be4e6c6
Gitweb:        https://git.kernel.org/tip/1a222de8dcfb903d039810b0823570ee0be4e6c6
Author:        Doug Covelli <[email protected]>
AuthorDate:    Mon, 07 Sep 2020 15:16:05 +02:00
Committer:     Borislav Petkov <[email protected]>
CommitterDate: Wed, 09 Sep 2020 11:33:20 +02:00

x86/vmware: Add VMware-specific handling for VMMCALL under SEV-ES

Add VMware-specific handling for #VC faults caused by VMMCALL instructions.

---
 arch/x86/kernel/cpu/vmware.c | 50 +++++++++++++++++++++++++++++++----
 1 file changed, 45 insertions(+), 5 deletions(-)
arch/x86/kernel/cpu/vmware.c +45 -5
index 9b6fafa..924571f 100644
@@ -33,6 +33,7 @@
 #include <asm/timer.h>
 #include <asm/apic.h>
 #include <asm/vmware.h>
+#include <asm/svm.h>
 
 #undef pr_fmt
 #define pr_fmt(fmt)    "vmware: " fmt
@@ -476,10 +477,49 @@ static bool __init vmware_legacy_x2apic_available(void)
               (eax & (1 << VMWARE_CMD_LEGACY_X2APIC)) != 0;
 }
 
+#ifdef CONFIG_AMD_MEM_ENCRYPT
+static void vmware_sev_es_hcall_prepare(struct ghcb *ghcb,
+                                       struct pt_regs *regs)
+{
+       /* Copy VMWARE specific Hypercall parameters to the GHCB */
+       ghcb_set_rip(ghcb, regs->ip);
+       ghcb_set_rbx(ghcb, regs->bx);
+       ghcb_set_rcx(ghcb, regs->cx);
+       ghcb_set_rdx(ghcb, regs->dx);
+       ghcb_set_rsi(ghcb, regs->si);
+       ghcb_set_rdi(ghcb, regs->di);
+       ghcb_set_rbp(ghcb, regs->bp);
+}
+
+static bool vmware_sev_es_hcall_finish(struct ghcb *ghcb, struct pt_regs *regs)
+{
+       if (!(ghcb_rbx_is_valid(ghcb) &&
+             ghcb_rcx_is_valid(ghcb) &&
+             ghcb_rdx_is_valid(ghcb) &&
+             ghcb_rsi_is_valid(ghcb) &&
+             ghcb_rdi_is_valid(ghcb) &&
+             ghcb_rbp_is_valid(ghcb)))
+               return false;
+
+       regs->bx = ghcb->save.rbx;
+       regs->cx = ghcb->save.rcx;
+       regs->dx = ghcb->save.rdx;
+       regs->si = ghcb->save.rsi;
+       regs->di = ghcb->save.rdi;
+       regs->bp = ghcb->save.rbp;
+
+       return true;
+}
+#endif
+
 const __initconst struct hypervisor_x86 x86_hyper_vmware = {
-       .name                   = "VMware",
-       .detect                 = vmware_platform,
-       .type                   = X86_HYPER_VMWARE,
-       .init.init_platform     = vmware_platform_setup,
-       .init.x2apic_available  = vmware_legacy_x2apic_available,
+       .name                           = "VMware",
+       .detect                         = vmware_platform,
+       .type                           = X86_HYPER_VMWARE,
+       .init.init_platform             = vmware_platform_setup,
+       .init.x2apic_available          = vmware_legacy_x2apic_available,
+#ifdef CONFIG_AMD_MEM_ENCRYPT
+       .runtime.sev_es_hcall_prepare   = vmware_sev_es_hcall_prepare,
+       .runtime.sev_es_hcall_finish    = vmware_sev_es_hcall_finish,
+#endif
 };
#149 lore raw

Re: [tip: x86/seves] x86/vmware: Add VMware-specific handling for VMMCALL under SEV-ES

Quoted 8 lines · Erdem Aktas

Looking at the VMWARE_VMCALL(cmd, eax, ebx, ecx, edx) definition, it seems to me only 4 registers are required to be shared with hypervisor. I don't know much about vmware but is not vmware_sev_es_hcall_prepare expose more registers than needed and also vmware_sev_es_hcall_finish might let the hypvervisor to modify additional registers which are not used?

Just checking if this is intentional and what I am missing here.

Original patch is from Doug, maybe he can clarify what needs to be shared. I only adapted it to the call-backs.

Doug?

Regards,

Joerg

#150 lore raw

PATCH v7 65/72 x86/realmode: Add SEV-ES specific trampoline entry point

In reply to Joerg Roedel

From: Joerg Roedel <[email protected]>

The code at the trampoline entry point is executed in real-mode. In real-mode #VC exceptions can't be handled, so anything that might cause such an exception must be avoided.

In the standard trampoline entry code this is the WBINVD instruction and the call to verify_cpu(), which are both not needed anyway when running as an SEV-ES guest.

---
 arch/x86/include/asm/realmode.h      |  3 +++
 arch/x86/realmode/rm/header.S        |  3 +++
 arch/x86/realmode/rm/trampoline_64.S | 20 ++++++++++++++++++++
 3 files changed, 26 insertions(+)
arch/x86/include/asm/realmode.h +3 -0
index 96118fb041b8..4d4d853f6841 100644
@@ -21,6 +21,9 @@ struct real_mode_header {
 	/* SMP trampoline */
 	u32	trampoline_start;
 	u32	trampoline_header;
+#ifdef CONFIG_AMD_MEM_ENCRYPT
+	u32	sev_es_trampoline_start;
+#endif
 #ifdef CONFIG_X86_64
 	u32	trampoline_pgd;
 #endif
arch/x86/realmode/rm/header.S +3 -0
index af04512c02d9..8c1db5bf5d78 100644
@@ -20,6 +20,9 @@ SYM_DATA_START(real_mode_header)
 	/* SMP trampoline */
 	.long	pa_trampoline_start
 	.long	pa_trampoline_header
+#ifdef CONFIG_AMD_MEM_ENCRYPT
+	.long	pa_sev_es_trampoline_start
+#endif
 #ifdef CONFIG_X86_64
 	.long	pa_trampoline_pgd;
 #endif
arch/x86/realmode/rm/trampoline_64.S +20 -0
index 251758ed7443..84c5d1b33d10 100644
@@ -56,6 +56,7 @@ SYM_CODE_START(trampoline_start)
 	testl   %eax, %eax		# Check for return code
 	jnz	no_longmode
 
+.Lswitch_to_protected:
 	/*
 	 * GDT tables in non default location kernel can be beyond 16MB and
 	 * lgdt will not be able to load the address as in real mode default
@@ -80,6 +81,25 @@ no_longmode:
 	jmp no_longmode
 SYM_CODE_END(trampoline_start)
 
+#ifdef CONFIG_AMD_MEM_ENCRYPT
+/* SEV-ES supports non-zero IP for entry points - no alignment needed */
+SYM_CODE_START(sev_es_trampoline_start)
+	cli			# We should be safe anyway
+
+	LJMPW_RM(1f)
+1:
+	mov	%cs, %ax	# Code and data in the same place
+	mov	%ax, %ds
+	mov	%ax, %es
+	mov	%ax, %ss
+
+	# Setup stack
+	movl	$rm_stack_end, %esp
+
+	jmp	.Lswitch_to_protected
+SYM_CODE_END(sev_es_trampoline_start)
+#endif	/* CONFIG_AMD_MEM_ENCRYPT */
+
 #include "../kernel/verify_cpu.S"
 
 	.section ".text32","ax"
Signature · 6 lines
2.28.0

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
merge bot Applied
#151 lore raw

[tip: x86/seves] x86/realmode: Add SEV-ES specific trampoline entry point

The following commit has been merged into the x86/seves branch of tip:

The following commit has been merged into the x86/seves branch of tip:

Commit-ID:     bf5ff276448f64f1f9ef9ffc9e231026e3887d3d
Gitweb:        https://git.kernel.org/tip/bf5ff276448f64f1f9ef9ffc9e231026e3887d3d
Author:        Joerg Roedel <[email protected]>
AuthorDate:    Mon, 07 Sep 2020 15:16:06 +02:00
Committer:     Borislav Petkov <[email protected]>
CommitterDate: Wed, 09 Sep 2020 11:33:20 +02:00

x86/realmode: Add SEV-ES specific trampoline entry point

The code at the trampoline entry point is executed in real-mode. In real-mode, #VC exceptions can't be handled so anything that might cause such an exception must be avoided.

In the standard trampoline entry code this is the WBINVD instruction and the call to verify_cpu(), which are both not needed anyway when running as an SEV-ES guest.

---
 arch/x86/include/asm/realmode.h      |  3 +++
 arch/x86/realmode/rm/header.S        |  3 +++
 arch/x86/realmode/rm/trampoline_64.S | 20 ++++++++++++++++++++
 3 files changed, 26 insertions(+)
arch/x86/include/asm/realmode.h +3 -0
index 96118fb..4d4d853 100644
@@ -21,6 +21,9 @@ struct real_mode_header {
 	/* SMP trampoline */
 	u32	trampoline_start;
 	u32	trampoline_header;
+#ifdef CONFIG_AMD_MEM_ENCRYPT
+	u32	sev_es_trampoline_start;
+#endif
 #ifdef CONFIG_X86_64
 	u32	trampoline_pgd;
 #endif
arch/x86/realmode/rm/header.S +3 -0
index af04512..8c1db5b 100644
@@ -20,6 +20,9 @@ SYM_DATA_START(real_mode_header)
 	/* SMP trampoline */
 	.long	pa_trampoline_start
 	.long	pa_trampoline_header
+#ifdef CONFIG_AMD_MEM_ENCRYPT
+	.long	pa_sev_es_trampoline_start
+#endif
 #ifdef CONFIG_X86_64
 	.long	pa_trampoline_pgd;
 #endif
arch/x86/realmode/rm/trampoline_64.S +20 -0
index 251758e..84c5d1b 100644
@@ -56,6 +56,7 @@ SYM_CODE_START(trampoline_start)
 	testl   %eax, %eax		# Check for return code
 	jnz	no_longmode
 
+.Lswitch_to_protected:
 	/*
 	 * GDT tables in non default location kernel can be beyond 16MB and
 	 * lgdt will not be able to load the address as in real mode default
@@ -80,6 +81,25 @@ no_longmode:
 	jmp no_longmode
 SYM_CODE_END(trampoline_start)
 
+#ifdef CONFIG_AMD_MEM_ENCRYPT
+/* SEV-ES supports non-zero IP for entry points - no alignment needed */
+SYM_CODE_START(sev_es_trampoline_start)
+	cli			# We should be safe anyway
+
+	LJMPW_RM(1f)
+1:
+	mov	%cs, %ax	# Code and data in the same place
+	mov	%ax, %ds
+	mov	%ax, %es
+	mov	%ax, %ss
+
+	# Setup stack
+	movl	$rm_stack_end, %esp
+
+	jmp	.Lswitch_to_protected
+SYM_CODE_END(sev_es_trampoline_start)
+#endif	/* CONFIG_AMD_MEM_ENCRYPT */
+
 #include "../kernel/verify_cpu.S"
 
 	.section ".text32","ax"
#152 lore raw

PATCH v7 66/72 x86/realmode: Setup AP jump table

In reply to Joerg Roedel

From: Tom Lendacky <[email protected]>

As part of the GHCB specification, the booting of APs under SEV-ES requires an AP jump table when transitioning from one layer of code to another (e.g. when going from UEFI to the OS). As a result, each layer that parks an AP must provide the physical address of an AP jump table to the next layer via the hypervisor.

Upon booting of the kernel, read the AP jump table address from the hypervisor. Under SEV-ES, APs are started using the INIT-SIPI-SIPI sequence. Before issuing the first SIPI request for an AP, the start CS and IP is programmed into the AP jump table. Upon issuing the SIPI request, the AP will awaken and jump to that start CS:IP address.

[ [email protected]: - Adapted to different code base
- Moved AP table setup from SIPI sending path to real-mode setup code
- Fix sparse warnings ]

---
 arch/x86/include/asm/sev-es.h   |  5 +++
 arch/x86/include/uapi/asm/svm.h |  3 ++
 arch/x86/kernel/sev-es.c        | 69 +++++++++++++++++++++++++++++++++
 arch/x86/realmode/init.c        | 18 ++++++++-
 4 files changed, 93 insertions(+), 2 deletions(-)
arch/x86/include/asm/sev-es.h +5 -0
index 59176e8c6b81..db88e1c3442d 100644
@@ -73,6 +73,9 @@ static inline u64 lower_bits(u64 val, unsigned int bits)
 	return (val & mask);
 }
 
+struct real_mode_header;
+enum stack_type;
+
 /* Early IDT entry points for #VC handler */
 extern void vc_no_ghcb(void);
 extern void vc_boot_ghcb(void);
@@ -92,9 +95,11 @@ static __always_inline void sev_es_ist_exit(void)
 	if (static_branch_unlikely(&sev_es_enable_key))
 		__sev_es_ist_exit();
 }
+extern int sev_es_setup_ap_jump_table(struct real_mode_header *rmh);
 #else
 static inline void sev_es_ist_enter(struct pt_regs *regs) { }
 static inline void sev_es_ist_exit(void) { }
+static inline int sev_es_setup_ap_jump_table(struct real_mode_header *rmh) { return 0; }
 #endif
 
 #endif
arch/x86/include/uapi/asm/svm.h +3 -0
index 8f36ae021a7f..346b8a7155e8 100644
@@ -84,6 +84,9 @@
 /* SEV-ES software-defined VMGEXIT events */
 #define SVM_VMGEXIT_MMIO_READ			0x80000001
 #define SVM_VMGEXIT_MMIO_WRITE			0x80000002
+#define SVM_VMGEXIT_AP_JUMP_TABLE		0x80000005
+#define SVM_VMGEXIT_SET_AP_JUMP_TABLE		0
+#define SVM_VMGEXIT_GET_AP_JUMP_TABLE		1
 #define SVM_VMGEXIT_UNSUPPORTED_EVENT		0x8000ffff
 
 #define SVM_EXIT_ERR           -1
arch/x86/kernel/sev-es.c +69 -0

87 lines not on this page · raw message

arch/x86/realmode/init.c +16 -2
index 1ed1208931e0..3fb9b60be07a 100644
@@ -9,6 +9,7 @@
 #include <asm/realmode.h>
 #include <asm/tlbflush.h>
 #include <asm/crash.h>
+#include <asm/sev-es.h>
 
 struct real_mode_header *real_mode_header;
 u32 *trampoline_cr4_features;
@@ -38,6 +39,19 @@ void __init reserve_real_mode(void)
 	crash_reserve_low_1M();
 }
 
+static void sme_sev_setup_real_mode(struct trampoline_header *th)
+{
+#ifdef CONFIG_AMD_MEM_ENCRYPT
+	if (sme_active())
+		th->flags |= TH_FLAGS_SME_ACTIVE;
+
+	if (sev_es_active()) {
+		if (sev_es_setup_ap_jump_table(real_mode_header))
+			panic("Failed to get/update SEV-ES AP Jump Table");
+	}
+#endif
+}
+
 static void __init setup_real_mode(void)
 {
 	u16 real_mode_seg;
@@ -104,13 +118,13 @@ static void __init setup_real_mode(void)
 	*trampoline_cr4_features = mmu_cr4_features;
 
 	trampoline_header->flags = 0;
-	if (sme_active())
-		trampoline_header->flags |= TH_FLAGS_SME_ACTIVE;
 
 	trampoline_pgd = (u64 *) __va(real_mode_header->trampoline_pgd);
 	trampoline_pgd[0] = trampoline_pgd_entry.pgd;
 	trampoline_pgd[511] = init_top_pgt[511].pgd;
 #endif
+
+	sme_sev_setup_real_mode(trampoline_header);
 }
 
 /*
Signature · 6 lines
2.28.0

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
merge bot Applied
#153 lore raw

[tip: x86/seves] x86/realmode: Setup AP jump table

The following commit has been merged into the x86/seves branch of tip:

The following commit has been merged into the x86/seves branch of tip:

Commit-ID:     8940ac9ced8bc1c48c4e28b0784e3234c9d14469
Gitweb:        https://git.kernel.org/tip/8940ac9ced8bc1c48c4e28b0784e3234c9d14469
Author:        Tom Lendacky <[email protected]>
AuthorDate:    Mon, 07 Sep 2020 15:16:07 +02:00
Committer:     Borislav Petkov <[email protected]>
CommitterDate: Wed, 09 Sep 2020 11:33:20 +02:00

x86/realmode: Setup AP jump table

As part of the GHCB specification, the booting of APs under SEV-ES requires an AP jump table when transitioning from one layer of code to another (e.g. when going from UEFI to the OS). As a result, each layer that parks an AP must provide the physical address of an AP jump table to the next layer via the hypervisor.

Upon booting of the kernel, read the AP jump table address from the hypervisor. Under SEV-ES, APs are started using the INIT-SIPI-SIPI sequence. Before issuing the first SIPI request for an AP, the start CS and IP is programmed into the AP jump table. Upon issuing the SIPI request, the AP will awaken and jump to that start CS:IP address.

[ [email protected]: - Adapted to different code base
- Moved AP table setup from SIPI sending path to real-mode setup code
- Fix sparse warnings ]

---
 arch/x86/include/asm/sev-es.h   |  5 ++-
 arch/x86/include/uapi/asm/svm.h |  3 +-
 arch/x86/kernel/sev-es.c        | 69 ++++++++++++++++++++++++++++++++-
 arch/x86/realmode/init.c        | 18 +++++++-
 4 files changed, 93 insertions(+), 2 deletions(-)
arch/x86/include/asm/sev-es.h +5 -0
index 59176e8..db88e1c 100644
@@ -73,6 +73,9 @@ static inline u64 lower_bits(u64 val, unsigned int bits)
 	return (val & mask);
 }
 
+struct real_mode_header;
+enum stack_type;
+
 /* Early IDT entry points for #VC handler */
 extern void vc_no_ghcb(void);
 extern void vc_boot_ghcb(void);
@@ -92,9 +95,11 @@ static __always_inline void sev_es_ist_exit(void)
 	if (static_branch_unlikely(&sev_es_enable_key))
 		__sev_es_ist_exit();
 }
+extern int sev_es_setup_ap_jump_table(struct real_mode_header *rmh);
 #else
 static inline void sev_es_ist_enter(struct pt_regs *regs) { }
 static inline void sev_es_ist_exit(void) { }
+static inline int sev_es_setup_ap_jump_table(struct real_mode_header *rmh) { return 0; }
 #endif
 
 #endif
arch/x86/include/uapi/asm/svm.h +3 -0
index 8f36ae0..346b8a7 100644
@@ -84,6 +84,9 @@
 /* SEV-ES software-defined VMGEXIT events */
 #define SVM_VMGEXIT_MMIO_READ			0x80000001
 #define SVM_VMGEXIT_MMIO_WRITE			0x80000002
+#define SVM_VMGEXIT_AP_JUMP_TABLE		0x80000005
+#define SVM_VMGEXIT_SET_AP_JUMP_TABLE		0
+#define SVM_VMGEXIT_GET_AP_JUMP_TABLE		1
 #define SVM_VMGEXIT_UNSUPPORTED_EVENT		0x8000ffff
 
 #define SVM_EXIT_ERR           -1
arch/x86/kernel/sev-es.c +69 -0

87 lines not on this page · raw message

arch/x86/realmode/init.c +16 -2
index 1ed1208..3fb9b60 100644
@@ -9,6 +9,7 @@
 #include <asm/realmode.h>
 #include <asm/tlbflush.h>
 #include <asm/crash.h>
+#include <asm/sev-es.h>
 
 struct real_mode_header *real_mode_header;
 u32 *trampoline_cr4_features;
@@ -38,6 +39,19 @@ void __init reserve_real_mode(void)
 	crash_reserve_low_1M();
 }
 
+static void sme_sev_setup_real_mode(struct trampoline_header *th)
+{
+#ifdef CONFIG_AMD_MEM_ENCRYPT
+	if (sme_active())
+		th->flags |= TH_FLAGS_SME_ACTIVE;
+
+	if (sev_es_active()) {
+		if (sev_es_setup_ap_jump_table(real_mode_header))
+			panic("Failed to get/update SEV-ES AP Jump Table");
+	}
+#endif
+}
+
 static void __init setup_real_mode(void)
 {
 	u16 real_mode_seg;
@@ -104,13 +118,13 @@ static void __init setup_real_mode(void)
 	*trampoline_cr4_features = mmu_cr4_features;
 
 	trampoline_header->flags = 0;
-	if (sme_active())
-		trampoline_header->flags |= TH_FLAGS_SME_ACTIVE;
 
 	trampoline_pgd = (u64 *) __va(real_mode_header->trampoline_pgd);
 	trampoline_pgd[0] = trampoline_pgd_entry.pgd;
 	trampoline_pgd[511] = init_top_pgt[511].pgd;
 #endif
+
+	sme_sev_setup_real_mode(trampoline_header);
 }
 
 /*
#154 lore raw

PATCH v7 67/72 x86/smpboot: Load TSS and getcpu GDT entry before loading IDT

In reply to Joerg Roedel

From: Joerg Roedel <[email protected]>

The IDT on 64bit contains vectors which use paranoid_entry() and/or IST stacks. To make these vectors work the TSS and the getcpu GDT entry need to be set up before the IDT is loaded.

---
 arch/x86/include/asm/processor.h |  1 +
 arch/x86/kernel/cpu/common.c     | 23 +++++++++++++++++++++++
 arch/x86/kernel/smpboot.c        |  2 +-
 3 files changed, 25 insertions(+), 1 deletion(-)
arch/x86/include/asm/processor.h +1 -0
index d8a82e650810..5ac507586769 100644
@@ -696,6 +696,7 @@ extern void load_direct_gdt(int);
 extern void load_fixmap_gdt(int);
 extern void load_percpu_segment(int);
 extern void cpu_init(void);
+extern void cpu_init_exception_handling(void);
 extern void cr4_init(void);
 
 static inline unsigned long get_debugctlmsr(void)
arch/x86/kernel/cpu/common.c +23 -0
index 1d65365363a1..a9527c0c38fb 100644
@@ -1854,6 +1854,29 @@ static inline void tss_setup_io_bitmap(struct tss_struct *tss)
 #endif
 }
 
+/*
+ * Setup everything needed to handle exceptions from the IDT, including the IST
+ * exceptions which use paranoid_entry()
+ */
+void cpu_init_exception_handling(void)
+{
+	struct tss_struct *tss = this_cpu_ptr(&cpu_tss_rw);
+	int cpu = raw_smp_processor_id();
+
+	/* paranoid_entry() gets the CPU number from the GDT */
+	setup_getcpu(cpu);
+
+	/* IST vectors need TSS to be set up. */
+	tss_setup_ist(tss);
+	tss_setup_io_bitmap(tss);
+	set_tss_desc(cpu, &get_cpu_entry_area(cpu)->tss.x86_tss);
+
+	load_TR_desc();
+
+	/* Finally load the IDT */
+	load_current_idt();
+}
+
 /*
  * cpu_init() initializes state that is per-CPU. Some data is already
  * initialized (naturally) in the bootstrap process, such as the GDT
arch/x86/kernel/smpboot.c +1 -1
index f5ef689dd62a..de776b2e6046 100644
@@ -227,7 +227,7 @@ static void notrace start_secondary(void *unused)
 	load_cr3(swapper_pg_dir);
 	__flush_tlb_all();
 #endif
-	load_current_idt();
+	cpu_init_exception_handling();
 	cpu_init();
 	x86_cpuinit.early_percpu_clock_init();
 	preempt_disable();
Signature · 6 lines
2.28.0

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
maintainer
#155 lore raw

Re: [PATCH v7 67/72] x86/smpboot: Load TSS and getcpu GDT entry before loading IDT

Quoted 41 lines · Joerg Roedel

From: Joerg Roedel <[email protected]>

The IDT on 64bit contains vectors which use paranoid_entry() and/or IST stacks. To make these vectors work the TSS and the getcpu GDT entry need to be set up before the IDT is loaded.

---
 arch/x86/include/asm/processor.h |  1 +
 arch/x86/kernel/cpu/common.c     | 23 +++++++++++++++++++++++
 arch/x86/kernel/smpboot.c        |  2 +-
 3 files changed, 25 insertions(+), 1 deletion(-)
arch/x86/include/asm/processor.h +1 -0
index d8a82e650810..5ac507586769 100644
@@ -696,6 +696,7 @@ extern void load_direct_gdt(int);
 extern void load_fixmap_gdt(int);
 extern void load_percpu_segment(int);
 extern void cpu_init(void);
+extern void cpu_init_exception_handling(void);
 extern void cr4_init(void);
 
 static inline unsigned long get_debugctlmsr(void)
arch/x86/kernel/cpu/common.c +8 -0
index 1d65365363a1..a9527c0c38fb 100644
@@ -1854,6 +1854,29 @@ static inline void tss_setup_io_bitmap(struct tss_struct *tss)
 #endif
 }
 
+/*
+ * Setup everything needed to handle exceptions from the IDT, including the IST
+ * exceptions which use paranoid_entry()
+ */
+void cpu_init_exception_handling(void)
+{
+	struct tss_struct *tss = this_cpu_ptr(&cpu_tss_rw);
+	int cpu = raw_smp_processor_id();

arch/x86/kernel/cpu/common.c static inline void tss_setup_io_bitmap(struct tss_struct *tss) · line 1874

Quoted 10 lines
arch/x86/kernel/cpu/common.c +10 -0
+
+	/* paranoid_entry() gets the CPU number from the GDT */
+	setup_getcpu(cpu);
+
+	/* IST vectors need TSS to be set up. */
+	tss_setup_ist(tss);
+	tss_setup_io_bitmap(tss);
+	set_tss_desc(cpu, &get_cpu_entry_area(cpu)->tss.x86_tss);
+
+	load_TR_desc();

Aha, this is what you mean here in your 0th message. I'm guessing it is ok to do those things twice in start_secondary...

Signature · 8 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
#156 lore raw

Re: [PATCH v7 67/72] x86/smpboot: Load TSS and getcpu GDT entry before loading IDT

Quoted 10 lines · Borislav Petkov
Quoted 9 lines · Joerg Roedel
arch/x86/kernel/cpu/common.c +9 -0
+void cpu_init_exception_handling(void)
+{
+	struct tss_struct *tss = this_cpu_ptr(&cpu_tss_rw);
+	int cpu = raw_smp_processor_id();
+
+	/* paranoid_entry() gets the CPU number from the GDT */
+	setup_getcpu(cpu);
+
+	/* IST vectors need TSS to be set up. */
Quoted 8 lines

arch/x86/kernel/cpu/common.c static inline void tss_setup_io_bitmap(struct tss_struct *tss) · line 1874

Quoted 5 lines
arch/x86/kernel/cpu/common.c +5 -0
+	tss_setup_ist(tss);
+	tss_setup_io_bitmap(tss);
+	set_tss_desc(cpu, &get_cpu_entry_area(cpu)->tss.x86_tss);
+
+	load_TR_desc();

Aha, this is what you mean here in your 0th message. I'm guessing it is ok to do those things twice in start_secondary...

Yes, I think its best to do it twice, so that cpu_init() stays the CPU state barrier it should be, independent of what happens before.

Joerg
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

merge bot Applied
#157 lore raw

[tip: x86/seves] x86/smpboot: Load TSS and getcpu GDT entry before loading IDT

In reply to Joerg Roedel
The following commit has been merged into the x86/seves branch of tip:

The following commit has been merged into the x86/seves branch of tip:

Commit-ID:     520d030852b4c9babfce9a79d8b5320b6b5545e6
Gitweb:        https://git.kernel.org/tip/520d030852b4c9babfce9a79d8b5320b6b5545e6
Author:        Joerg Roedel <[email protected]>
AuthorDate:    Mon, 07 Sep 2020 15:16:08 +02:00
Committer:     Borislav Petkov <[email protected]>
CommitterDate: Wed, 09 Sep 2020 11:33:20 +02:00

x86/smpboot: Load TSS and getcpu GDT entry before loading IDT

The IDT on 64-bit contains vectors which use paranoid_entry() and/or IST stacks. To make these vectors work, the TSS and the getcpu GDT entry need to be set up before the IDT is loaded.

---
 arch/x86/include/asm/processor.h |  1 +
 arch/x86/kernel/cpu/common.c     | 23 +++++++++++++++++++++++
 arch/x86/kernel/smpboot.c        |  2 +-
 3 files changed, 25 insertions(+), 1 deletion(-)
arch/x86/include/asm/processor.h +1 -0
index 97143d8..615dd44 100644
@@ -696,6 +696,7 @@ extern void load_direct_gdt(int);
 extern void load_fixmap_gdt(int);
 extern void load_percpu_segment(int);
 extern void cpu_init(void);
+extern void cpu_init_exception_handling(void);
 extern void cr4_init(void);
 
 static inline unsigned long get_debugctlmsr(void)
arch/x86/kernel/cpu/common.c +23 -0
index 81fba4d..beffea2 100644
@@ -1863,6 +1863,29 @@ static inline void tss_setup_io_bitmap(struct tss_struct *tss)
 }
 
 /*
+ * Setup everything needed to handle exceptions from the IDT, including the IST
+ * exceptions which use paranoid_entry().
+ */
+void cpu_init_exception_handling(void)
+{
+	struct tss_struct *tss = this_cpu_ptr(&cpu_tss_rw);
+	int cpu = raw_smp_processor_id();
+
+	/* paranoid_entry() gets the CPU number from the GDT */
+	setup_getcpu(cpu);
+
+	/* IST vectors need TSS to be set up. */
+	tss_setup_ist(tss);
+	tss_setup_io_bitmap(tss);
+	set_tss_desc(cpu, &get_cpu_entry_area(cpu)->tss.x86_tss);
+
+	load_TR_desc();
+
+	/* Finally load the IDT */
+	load_current_idt();
+}
+
+/*
  * cpu_init() initializes state that is per-CPU. Some data is already
  * initialized (naturally) in the bootstrap process, such as the GDT
  * and IDT. We reload them nevertheless, this function acts as a
arch/x86/kernel/smpboot.c +1 -1
index f5ef689..de776b2 100644
@@ -227,7 +227,7 @@ static void notrace start_secondary(void *unused)
 	load_cr3(swapper_pg_dir);
 	__flush_tlb_all();
 #endif
-	load_current_idt();
+	cpu_init_exception_handling();
 	cpu_init();
 	x86_cpuinit.early_percpu_clock_init();
 	preempt_disable();
#158 lore raw

PATCH v7 68/72 x86/head/64: Don't call verify_cpu() on starting APs

In reply to Joerg Roedel

From: Joerg Roedel <[email protected]>

The APs are not ready to handle exceptions when verify_cpu() is called in secondary_startup_64.

---
 arch/x86/include/asm/realmode.h |  1 +
 arch/x86/kernel/head_64.S       | 12 ++++++++++++
 arch/x86/realmode/init.c        |  6 ++++++
 3 files changed, 19 insertions(+)
arch/x86/include/asm/realmode.h +1 -0
index 4d4d853f6841..5db5d083c873 100644
@@ -72,6 +72,7 @@ extern unsigned char startup_32_smp[];
 extern unsigned char boot_gdt[];
 #else
 extern unsigned char secondary_startup_64[];
+extern unsigned char secondary_startup_64_no_verify[];
 #endif
 
 static inline size_t real_mode_size_needed(void)
arch/x86/kernel/head_64.S +12 -0
index 41057ff79284..d976a9e6fcba 100644
@@ -125,6 +125,18 @@ SYM_CODE_START(secondary_startup_64)
 	/* Sanitize CPU configuration */
 	call verify_cpu
 
+	/*
+	 * The secondary_startup_64_no_verify entry point is only used by
+	 * SEV-ES guests. In those guests the call to verify_cpu() would cause
+	 * #VC exceptions which can not be handled at this stage of secondary
+	 * CPU bringup.
+	 *
+	 * All non SEV-ES systems, especially Intel systems, need to execute
+	 * verify_cpu() above to make sure NX is enabled.
+	 */
+SYM_INNER_LABEL(secondary_startup_64_no_verify, SYM_L_GLOBAL)
+	UNWIND_HINT_EMPTY
+
 	/*
 	 * Retrieve the modifier (SME encryption mask if SME is active) to be
 	 * added to the initial pgdir entry that will be programmed into CR3.
arch/x86/realmode/init.c +6 -0
index 3fb9b60be07a..22fda7d99159 100644
@@ -46,6 +46,12 @@ static void sme_sev_setup_real_mode(struct trampoline_header *th)
 		th->flags |= TH_FLAGS_SME_ACTIVE;
 
 	if (sev_es_active()) {
+		/*
+		 * Skip the call to verify_cpu() in secondary_startup_64 as it
+		 * will cause #VC exceptions when the AP can't handle them yet.
+		 */
+		th->start = (u64) secondary_startup_64_no_verify;
+
 		if (sev_es_setup_ap_jump_table(real_mode_header))
 			panic("Failed to get/update SEV-ES AP Jump Table");
 	}
Signature · 6 lines
2.28.0

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
merge bot Applied
#159 lore raw

[tip: x86/seves] x86/head/64: Don't call verify_cpu() on starting APs

The following commit has been merged into the x86/seves branch of tip:

The following commit has been merged into the x86/seves branch of tip:

Commit-ID:     3ecacdbd23956a549d93023f86adc87b4a9d6520
Gitweb:        https://git.kernel.org/tip/3ecacdbd23956a549d93023f86adc87b4a9d6520
Author:        Joerg Roedel <[email protected]>
AuthorDate:    Mon, 07 Sep 2020 15:16:09 +02:00
Committer:     Borislav Petkov <[email protected]>
CommitterDate: Wed, 09 Sep 2020 11:33:20 +02:00

x86/head/64: Don't call verify_cpu() on starting APs

The APs are not ready to handle exceptions when verify_cpu() is called in secondary_startup_64().

---
 arch/x86/include/asm/realmode.h |  1 +
 arch/x86/kernel/head_64.S       | 12 ++++++++++++
 arch/x86/realmode/init.c        |  6 ++++++
 3 files changed, 19 insertions(+)
arch/x86/include/asm/realmode.h +1 -0
index 4d4d853..5db5d08 100644
@@ -72,6 +72,7 @@ extern unsigned char startup_32_smp[];
 extern unsigned char boot_gdt[];
 #else
 extern unsigned char secondary_startup_64[];
+extern unsigned char secondary_startup_64_no_verify[];
 #endif
 
 static inline size_t real_mode_size_needed(void)
arch/x86/kernel/head_64.S +12 -0
index 1a71d0d..7eb2a1c 100644
@@ -126,6 +126,18 @@ SYM_CODE_START(secondary_startup_64)
 	call verify_cpu
 
 	/*
+	 * The secondary_startup_64_no_verify entry point is only used by
+	 * SEV-ES guests. In those guests the call to verify_cpu() would cause
+	 * #VC exceptions which can not be handled at this stage of secondary
+	 * CPU bringup.
+	 *
+	 * All non SEV-ES systems, especially Intel systems, need to execute
+	 * verify_cpu() above to make sure NX is enabled.
+	 */
+SYM_INNER_LABEL(secondary_startup_64_no_verify, SYM_L_GLOBAL)
+	UNWIND_HINT_EMPTY
+
+	/*
 	 * Retrieve the modifier (SME encryption mask if SME is active) to be
 	 * added to the initial pgdir entry that will be programmed into CR3.
 	 */
arch/x86/realmode/init.c +6 -0
index 3fb9b60..22fda7d 100644
@@ -46,6 +46,12 @@ static void sme_sev_setup_real_mode(struct trampoline_header *th)
 		th->flags |= TH_FLAGS_SME_ACTIVE;
 
 	if (sev_es_active()) {
+		/*
+		 * Skip the call to verify_cpu() in secondary_startup_64 as it
+		 * will cause #VC exceptions when the AP can't handle them yet.
+		 */
+		th->start = (u64) secondary_startup_64_no_verify;
+
 		if (sev_es_setup_ap_jump_table(real_mode_header))
 			panic("Failed to get/update SEV-ES AP Jump Table");
 	}
#160 lore raw

PATCH v7 69/72 x86/sev-es: Support CPU offline/online

In reply to Joerg Roedel

From: Joerg Roedel <[email protected]>

Add a play_dead handler when running under SEV-ES. This is needed because the hypervisor can't deliver an SIPI request to restart the AP. Instead the kernel has to issue a VMGEXIT to halt the VCPU until the hypervisor wakes it up again.

---
 arch/x86/include/uapi/asm/svm.h |  1 +
 arch/x86/kernel/sev-es.c        | 63 +++++++++++++++++++++++++++++++++
 2 files changed, 64 insertions(+)
arch/x86/include/uapi/asm/svm.h +1 -0
index 346b8a7155e8..c1dcf3e114e4 100644
@@ -84,6 +84,7 @@
 /* SEV-ES software-defined VMGEXIT events */
 #define SVM_VMGEXIT_MMIO_READ			0x80000001
 #define SVM_VMGEXIT_MMIO_WRITE			0x80000002
+#define SVM_VMGEXIT_AP_HLT_LOOP			0x80000004
 #define SVM_VMGEXIT_AP_JUMP_TABLE		0x80000005
 #define SVM_VMGEXIT_SET_AP_JUMP_TABLE		0
 #define SVM_VMGEXIT_GET_AP_JUMP_TABLE		1
arch/x86/kernel/sev-es.c +63 -0

81 lines not on this page · raw message

Signature · 6 lines
2.28.0

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
merge bot Applied
#161 lore raw

[tip: x86/seves] x86/sev-es: Support CPU offline/online

The following commit has been merged into the x86/seves branch of tip:

The following commit has been merged into the x86/seves branch of tip:

Commit-ID:     094794f59720d7e877a1eeb372ecedeed6b441ab
Gitweb:        https://git.kernel.org/tip/094794f59720d7e877a1eeb372ecedeed6b441ab
Author:        Joerg Roedel <[email protected]>
AuthorDate:    Mon, 07 Sep 2020 15:16:10 +02:00
Committer:     Borislav Petkov <[email protected]>
CommitterDate: Wed, 09 Sep 2020 11:33:20 +02:00

x86/sev-es: Support CPU offline/online

Add a play_dead handler when running under SEV-ES. This is needed because the hypervisor can't deliver an SIPI request to restart the AP. Instead, the kernel has to issue a VMGEXIT to halt the VCPU until the hypervisor wakes it up again.

---
 arch/x86/include/uapi/asm/svm.h |  1 +-
 arch/x86/kernel/sev-es.c        | 63 ++++++++++++++++++++++++++++++++-
 2 files changed, 64 insertions(+)
arch/x86/include/uapi/asm/svm.h +1 -0
index 346b8a7..c1dcf3e 100644
@@ -84,6 +84,7 @@
 /* SEV-ES software-defined VMGEXIT events */
 #define SVM_VMGEXIT_MMIO_READ			0x80000001
 #define SVM_VMGEXIT_MMIO_WRITE			0x80000002
+#define SVM_VMGEXIT_AP_HLT_LOOP			0x80000004
 #define SVM_VMGEXIT_AP_JUMP_TABLE		0x80000005
 #define SVM_VMGEXIT_SET_AP_JUMP_TABLE		0
 #define SVM_VMGEXIT_GET_AP_JUMP_TABLE		1
arch/x86/kernel/sev-es.c +63 -0

81 lines not on this page · raw message

#162 lore raw

PATCH v7 70/72 x86/sev-es: Handle NMI State

In reply to Joerg Roedel

From: Joerg Roedel <[email protected]>

When running under SEV-ES the kernel has to tell the hypervisor when to open the NMI window again after an NMI was injected. This is done with an NMI-complete message to the hypervisor.

Add code to the kernels NMI handler to send this message right at the beginning of do_nmi(). This always allows nesting NMIs.

---
 arch/x86/include/asm/sev-es.h   |  7 +++++++
 arch/x86/include/uapi/asm/svm.h |  1 +
 arch/x86/kernel/nmi.c           |  6 ++++++
 arch/x86/kernel/sev-es.c        | 18 ++++++++++++++++++
 4 files changed, 32 insertions(+)
arch/x86/include/asm/sev-es.h +7 -0
index db88e1c3442d..e919f09ae33c 100644
@@ -96,10 +96,17 @@ static __always_inline void sev_es_ist_exit(void)
 		__sev_es_ist_exit();
 }
 extern int sev_es_setup_ap_jump_table(struct real_mode_header *rmh);
+extern void __sev_es_nmi_complete(void);
+static __always_inline void sev_es_nmi_complete(void)
+{
+	if (static_branch_unlikely(&sev_es_enable_key))
+		__sev_es_nmi_complete();
+}
 #else
 static inline void sev_es_ist_enter(struct pt_regs *regs) { }
 static inline void sev_es_ist_exit(void) { }
 static inline int sev_es_setup_ap_jump_table(struct real_mode_header *rmh) { return 0; }
+static inline void sev_es_nmi_complete(void) { }
 #endif
 
 #endif
arch/x86/include/uapi/asm/svm.h +1 -0
index c1dcf3e114e4..a7a3403645e5 100644
@@ -84,6 +84,7 @@
 /* SEV-ES software-defined VMGEXIT events */
 #define SVM_VMGEXIT_MMIO_READ			0x80000001
 #define SVM_VMGEXIT_MMIO_WRITE			0x80000002
+#define SVM_VMGEXIT_NMI_COMPLETE		0x80000003
 #define SVM_VMGEXIT_AP_HLT_LOOP			0x80000004
 #define SVM_VMGEXIT_AP_JUMP_TABLE		0x80000005
 #define SVM_VMGEXIT_SET_AP_JUMP_TABLE		0
arch/x86/kernel/nmi.c +6 -0
index 5859cec774a4..d961f1b3e251 100644
@@ -478,6 +478,12 @@ DEFINE_IDTENTRY_RAW(exc_nmi)
 {
 	bool irq_state;
 
+	/*
+	 * Re-enable NMIs right here when running as an SEV-ES guest. This might
+	 * cause nested NMIs, but those can be handled safely.
+	 */
+	sev_es_nmi_complete();
+
 	if (IS_ENABLED(CONFIG_SMP) && arch_cpu_is_offline(smp_processor_id()))
 		return;
 
arch/x86/kernel/sev-es.c +18 -0
index 0b6c4f46ddfc..9ab3a4dfecd8 100644
@@ -408,6 +408,24 @@ static bool vc_slow_virt_to_phys(struct ghcb *ghcb, struct es_em_ctxt *ctxt,
 /* Include code shared with pre-decompression boot stage */
 #include "sev-es-shared.c"
 
+void __sev_es_nmi_complete(void)
+{
+	struct ghcb_state state;
+	struct ghcb *ghcb;
+
+	ghcb = sev_es_get_ghcb(&state);
+
+	vc_ghcb_invalidate(ghcb);
+	ghcb_set_sw_exit_code(ghcb, SVM_VMGEXIT_NMI_COMPLETE);
+	ghcb_set_sw_exit_info_1(ghcb, 0);
+	ghcb_set_sw_exit_info_2(ghcb, 0);
+
+	sev_es_wr_ghcb_msr(__pa(ghcb));
+	VMGEXIT();
+
+	sev_es_put_ghcb(&state);
+}
+
 static u64 get_jump_table_addr(void)
 {
 	struct ghcb_state state;
Signature · 6 lines
2.28.0

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
merge bot Applied
#163 lore raw

[tip: x86/seves] x86/sev-es: Handle NMI State

The following commit has been merged into the x86/seves branch of tip:

The following commit has been merged into the x86/seves branch of tip:

Commit-ID:     4ca68e023b11e4d5908bf9ee326fab01111d77d5
Gitweb:        https://git.kernel.org/tip/4ca68e023b11e4d5908bf9ee326fab01111d77d5
Author:        Joerg Roedel <[email protected]>
AuthorDate:    Mon, 07 Sep 2020 15:16:11 +02:00
Committer:     Borislav Petkov <[email protected]>
CommitterDate: Wed, 09 Sep 2020 18:02:35 +02:00

x86/sev-es: Handle NMI State

When running under SEV-ES, the kernel has to tell the hypervisor when to open the NMI window again after an NMI was injected. This is done with an NMI-complete message to the hypervisor.

Add code to the kernel's NMI handler to send this message right at the beginning of do_nmi(). This always allows nesting NMIs.

[ bp: Mark __sev_es_nmi_complete() noinstr:
vmlinux.o: warning: objtool: exc_nmi()+0x17: call to __sev_es_nmi_complete()
leaves .noinstr.text section
While at it, use __pa_nodebug() for the same reason due to CONFIG_DEBUG_VIRTUAL=y:
vmlinux.o: warning: objtool: __sev_es_nmi_complete()+0xd9: call to __phys_addr()
leaves .noinstr.text section ]

---
 arch/x86/include/asm/sev-es.h   |  7 +++++++
 arch/x86/include/uapi/asm/svm.h |  1 +
 arch/x86/kernel/nmi.c           |  6 ++++++
 arch/x86/kernel/sev-es.c        | 18 ++++++++++++++++++
 4 files changed, 32 insertions(+)
arch/x86/include/asm/sev-es.h +7 -0
index db88e1c..e919f09 100644
@@ -96,10 +96,17 @@ static __always_inline void sev_es_ist_exit(void)
 		__sev_es_ist_exit();
 }
 extern int sev_es_setup_ap_jump_table(struct real_mode_header *rmh);
+extern void __sev_es_nmi_complete(void);
+static __always_inline void sev_es_nmi_complete(void)
+{
+	if (static_branch_unlikely(&sev_es_enable_key))
+		__sev_es_nmi_complete();
+}
 #else
 static inline void sev_es_ist_enter(struct pt_regs *regs) { }
 static inline void sev_es_ist_exit(void) { }
 static inline int sev_es_setup_ap_jump_table(struct real_mode_header *rmh) { return 0; }
+static inline void sev_es_nmi_complete(void) { }
 #endif
 
 #endif
arch/x86/include/uapi/asm/svm.h +1 -0
index c1dcf3e..a7a3403 100644
@@ -84,6 +84,7 @@
 /* SEV-ES software-defined VMGEXIT events */
 #define SVM_VMGEXIT_MMIO_READ			0x80000001
 #define SVM_VMGEXIT_MMIO_WRITE			0x80000002
+#define SVM_VMGEXIT_NMI_COMPLETE		0x80000003
 #define SVM_VMGEXIT_AP_HLT_LOOP			0x80000004
 #define SVM_VMGEXIT_AP_JUMP_TABLE		0x80000005
 #define SVM_VMGEXIT_SET_AP_JUMP_TABLE		0
arch/x86/kernel/nmi.c +6 -0
index 4c89c4d..56b64d7 100644
@@ -478,6 +478,12 @@ DEFINE_IDTENTRY_RAW(exc_nmi)
 {
 	bool irq_state;
 
+	/*
+	 * Re-enable NMIs right here when running as an SEV-ES guest. This might
+	 * cause nested NMIs, but those can be handled safely.
+	 */
+	sev_es_nmi_complete();
+
 	if (IS_ENABLED(CONFIG_SMP) && arch_cpu_is_offline(smp_processor_id()))
 		return;
 
arch/x86/kernel/sev-es.c +18 -0
index d1bcd0d..b6518e9 100644
@@ -408,6 +408,24 @@ static bool vc_slow_virt_to_phys(struct ghcb *ghcb, struct es_em_ctxt *ctxt,
 /* Include code shared with pre-decompression boot stage */
 #include "sev-es-shared.c"
 
+void noinstr __sev_es_nmi_complete(void)
+{
+	struct ghcb_state state;
+	struct ghcb *ghcb;
+
+	ghcb = sev_es_get_ghcb(&state);
+
+	vc_ghcb_invalidate(ghcb);
+	ghcb_set_sw_exit_code(ghcb, SVM_VMGEXIT_NMI_COMPLETE);
+	ghcb_set_sw_exit_info_1(ghcb, 0);
+	ghcb_set_sw_exit_info_2(ghcb, 0);
+
+	sev_es_wr_ghcb_msr(__pa_nodebug(ghcb));
+	VMGEXIT();
+
+	sev_es_put_ghcb(&state);
+}
+
 static u64 get_jump_table_addr(void)
 {
 	struct ghcb_state state;
#164 lore raw

PATCH v7 71/72 x86/efi: Add GHCB mappings when SEV-ES is active

In reply to Joerg Roedel

From: Tom Lendacky <[email protected]>

Calling down to EFI runtime services can result in the firmware performing VMGEXIT calls. The firmware is likely to use the GHCB of the OS (e.g., for setting EFI variables), so each GHCB in the system needs to be identity mapped in the EFI page tables, as unencrypted, to avoid page faults.

[ [email protected]: Moved GHCB mapping loop to sev-es.c ]
---
 arch/x86/boot/compressed/sev-es.c |  1 +
 arch/x86/include/asm/sev-es.h     |  2 ++
 arch/x86/kernel/sev-es.c          | 30 ++++++++++++++++++++++++++++++
 arch/x86/platform/efi/efi_64.c    | 10 ++++++++++
 4 files changed, 43 insertions(+)
arch/x86/boot/compressed/sev-es.c +1 -0
index 45702b866c33..0a9a248ca33d 100644
@@ -12,6 +12,7 @@
  */
 #include "misc.h"
 
+#include <asm/pgtable_types.h>
 #include <asm/sev-es.h>
 #include <asm/trapnr.h>
 #include <asm/trap_pf.h>
arch/x86/include/asm/sev-es.h +2 -0
index e919f09ae33c..cf1d957c7091 100644
@@ -102,11 +102,13 @@ static __always_inline void sev_es_nmi_complete(void)
 	if (static_branch_unlikely(&sev_es_enable_key))
 		__sev_es_nmi_complete();
 }
+extern int __init sev_es_efi_map_ghcbs(pgd_t *pgd);
 #else
 static inline void sev_es_ist_enter(struct pt_regs *regs) { }
 static inline void sev_es_ist_exit(void) { }
 static inline int sev_es_setup_ap_jump_table(struct real_mode_header *rmh) { return 0; }
 static inline void sev_es_nmi_complete(void) { }
+static inline int sev_es_efi_map_ghcbs(pgd_t *pgd) { return 0; }
 #endif
 
 #endif
arch/x86/kernel/sev-es.c +30 -0
index 9ab3a4dfecd8..4e2b7e4d9b87 100644
@@ -491,6 +491,36 @@ int sev_es_setup_ap_jump_table(struct real_mode_header *rmh)
 	return 0;
 }
 
+/*
+ * This is needed by the OVMF UEFI firmware which will use whatever it finds in
+ * the GHCB MSR as its GHCB to talk to the hypervisor. So make sure the per-cpu
+ * runtime GHCBs used by the kernel are also mapped in the EFI page-table.
+ */
+int __init sev_es_efi_map_ghcbs(pgd_t *pgd)
+{
+	struct sev_es_runtime_data *data;
+	unsigned long address, pflags;
+	int cpu;
+	u64 pfn;
+
+	if (!sev_es_active())
+		return 0;
+
+	pflags = _PAGE_NX | _PAGE_RW;
+
+	for_each_possible_cpu(cpu) {
+		data = per_cpu(runtime_data, cpu);
+
+		address = __pa(&data->ghcb_page);
+		pfn = address >> PAGE_SHIFT;
+
+		if (kernel_map_pages_in_pgd(pgd, pfn, address, 1, pflags))
+			return 1;
+	}
+
+	return 0;
+}
+
 static enum es_result vc_handle_msr(struct ghcb *ghcb, struct es_em_ctxt *ctxt)
 {
 	struct pt_regs *regs = ctxt->regs;
arch/x86/platform/efi/efi_64.c +10 -0
index 6af4da1149ba..8f5759df7776 100644
@@ -47,6 +47,7 @@
 #include <asm/realmode.h>
 #include <asm/time.h>
 #include <asm/pgalloc.h>
+#include <asm/sev-es.h>
 
 /*
  * We allocate runtime services regions top-down, starting from -4G, i.e.
@@ -229,6 +230,15 @@ int __init efi_setup_page_tables(unsigned long pa_memmap, unsigned num_pages)
 		return 1;
 	}
 
+	/*
+	 * When SEV-ES is active, the GHCB as set by the kernel will be used
+	 * by firmware. Create a 1:1 unencrypted mapping for each GHCB.
+	 */
+	if (sev_es_efi_map_ghcbs(pgd)) {
+		pr_err("Failed to create 1:1 mapping for the GHCBs!\n");
+		return 1;
+	}
+
 	/*
 	 * When making calls to the firmware everything needs to be 1:1
 	 * mapped and addressable with 32-bit pointers. Map the kernel
Signature · 6 lines
2.28.0

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
maintainer
#165 lore raw

Re: [PATCH v7 71/72] x86/efi: Add GHCB mappings when SEV-ES is active

+ Ard so that he can ack the efi bits.

Quoted 119 lines · Joerg Roedel

From: Tom Lendacky <[email protected]>

Calling down to EFI runtime services can result in the firmware performing VMGEXIT calls. The firmware is likely to use the GHCB of the OS (e.g., for setting EFI variables), so each GHCB in the system needs to be identity mapped in the EFI page tables, as unencrypted, to avoid page faults.

[ [email protected]: Moved GHCB mapping loop to sev-es.c ]
---
 arch/x86/boot/compressed/sev-es.c |  1 +
 arch/x86/include/asm/sev-es.h     |  2 ++
 arch/x86/kernel/sev-es.c          | 30 ++++++++++++++++++++++++++++++
 arch/x86/platform/efi/efi_64.c    | 10 ++++++++++
 4 files changed, 43 insertions(+)
arch/x86/boot/compressed/sev-es.c +1 -0
index 45702b866c33..0a9a248ca33d 100644
@@ -12,6 +12,7 @@
  */
 #include "misc.h"
 
+#include <asm/pgtable_types.h>
 #include <asm/sev-es.h>
 #include <asm/trapnr.h>
 #include <asm/trap_pf.h>
arch/x86/include/asm/sev-es.h +2 -0
index e919f09ae33c..cf1d957c7091 100644
@@ -102,11 +102,13 @@ static __always_inline void sev_es_nmi_complete(void)
 	if (static_branch_unlikely(&sev_es_enable_key))
 		__sev_es_nmi_complete();
 }
+extern int __init sev_es_efi_map_ghcbs(pgd_t *pgd);
 #else
 static inline void sev_es_ist_enter(struct pt_regs *regs) { }
 static inline void sev_es_ist_exit(void) { }
 static inline int sev_es_setup_ap_jump_table(struct real_mode_header *rmh) { return 0; }
 static inline void sev_es_nmi_complete(void) { }
+static inline int sev_es_efi_map_ghcbs(pgd_t *pgd) { return 0; }
 #endif
 
 #endif
arch/x86/kernel/sev-es.c +30 -0
index 9ab3a4dfecd8..4e2b7e4d9b87 100644
@@ -491,6 +491,36 @@ int sev_es_setup_ap_jump_table(struct real_mode_header *rmh)
 	return 0;
 }
 
+/*
+ * This is needed by the OVMF UEFI firmware which will use whatever it finds in
+ * the GHCB MSR as its GHCB to talk to the hypervisor. So make sure the per-cpu
+ * runtime GHCBs used by the kernel are also mapped in the EFI page-table.
+ */
+int __init sev_es_efi_map_ghcbs(pgd_t *pgd)
+{
+	struct sev_es_runtime_data *data;
+	unsigned long address, pflags;
+	int cpu;
+	u64 pfn;
+
+	if (!sev_es_active())
+		return 0;
+
+	pflags = _PAGE_NX | _PAGE_RW;
+
+	for_each_possible_cpu(cpu) {
+		data = per_cpu(runtime_data, cpu);
+
+		address = __pa(&data->ghcb_page);
+		pfn = address >> PAGE_SHIFT;
+
+		if (kernel_map_pages_in_pgd(pgd, pfn, address, 1, pflags))
+			return 1;
+	}
+
+	return 0;
+}
+
 static enum es_result vc_handle_msr(struct ghcb *ghcb, struct es_em_ctxt *ctxt)
 {
 	struct pt_regs *regs = ctxt->regs;
arch/x86/platform/efi/efi_64.c +10 -0
index 6af4da1149ba..8f5759df7776 100644
@@ -47,6 +47,7 @@
 #include <asm/realmode.h>
 #include <asm/time.h>
 #include <asm/pgalloc.h>
+#include <asm/sev-es.h>
 
 /*
  * We allocate runtime services regions top-down, starting from -4G, i.e.
@@ -229,6 +230,15 @@ int __init efi_setup_page_tables(unsigned long pa_memmap, unsigned num_pages)
 		return 1;
 	}
 
+	/*
+	 * When SEV-ES is active, the GHCB as set by the kernel will be used
+	 * by firmware. Create a 1:1 unencrypted mapping for each GHCB.
+	 */
+	if (sev_es_efi_map_ghcbs(pgd)) {
+		pr_err("Failed to create 1:1 mapping for the GHCBs!\n");
+		return 1;
+	}
+
 	/*
 	 * When making calls to the firmware everything needs to be 1:1
 	 * mapped and addressable with 32-bit pointers. Map the kernel
Signature · 1 line
2.28.0
Signature · 8 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
maintainer
#166 lore raw

Re: [PATCH v7 71/72] x86/efi: Add GHCB mappings when SEV-ES is active

(adding Laszlo and Brijesh)

Quoted 14 lines · Borislav Petkov

+ Ard so that he can ack the efi bits.

Quoted 10 lines · Joerg Roedel

From: Tom Lendacky <[email protected]>

Calling down to EFI runtime services can result in the firmware performing VMGEXIT calls. The firmware is likely to use the GHCB of the OS (e.g., for setting EFI variables), so each GHCB in the system needs to be identity mapped in the EFI page tables, as unencrypted, to avoid page faults.

[ [email protected]: Moved GHCB mapping loop to sev-es.c ]

This looks like it is papering over a more fundamental issue: any memory region that the firmware itself needs to access during the execution of runtime services needs to be described in the UEFI memory map, with the appropriate annotations so that the OS knows it should include these in the EFI runtime page tables. So why has this been omitted in this case?

Quoted 115 lines
Quoted 107 lines
---
 arch/x86/boot/compressed/sev-es.c |  1 +
 arch/x86/include/asm/sev-es.h     |  2 ++
 arch/x86/kernel/sev-es.c          | 30 ++++++++++++++++++++++++++++++
 arch/x86/platform/efi/efi_64.c    | 10 ++++++++++
 4 files changed, 43 insertions(+)
arch/x86/boot/compressed/sev-es.c +1 -0
index 45702b866c33..0a9a248ca33d 100644
@@ -12,6 +12,7 @@
  */
 #include "misc.h"
 
+#include <asm/pgtable_types.h>
 #include <asm/sev-es.h>
 #include <asm/trapnr.h>
 #include <asm/trap_pf.h>
arch/x86/include/asm/sev-es.h +2 -0
index e919f09ae33c..cf1d957c7091 100644
@@ -102,11 +102,13 @@ static __always_inline void sev_es_nmi_complete(void)
      if (static_branch_unlikely(&sev_es_enable_key))
              __sev_es_nmi_complete();
 }
+extern int __init sev_es_efi_map_ghcbs(pgd_t *pgd);
 #else
 static inline void sev_es_ist_enter(struct pt_regs *regs) { }
 static inline void sev_es_ist_exit(void) { }
 static inline int sev_es_setup_ap_jump_table(struct real_mode_header *rmh) { return 0; }
 static inline void sev_es_nmi_complete(void) { }
+static inline int sev_es_efi_map_ghcbs(pgd_t *pgd) { return 0; }
 #endif
 
 #endif
arch/x86/kernel/sev-es.c +30 -0
index 9ab3a4dfecd8..4e2b7e4d9b87 100644
@@ -491,6 +491,36 @@ int sev_es_setup_ap_jump_table(struct real_mode_header *rmh)
      return 0;
 }
 
+/*
+ * This is needed by the OVMF UEFI firmware which will use whatever it finds in
+ * the GHCB MSR as its GHCB to talk to the hypervisor. So make sure the per-cpu
+ * runtime GHCBs used by the kernel are also mapped in the EFI page-table.
+ */
+int __init sev_es_efi_map_ghcbs(pgd_t *pgd)
+{
+     struct sev_es_runtime_data *data;
+     unsigned long address, pflags;
+     int cpu;
+     u64 pfn;
+
+     if (!sev_es_active())
+             return 0;
+
+     pflags = _PAGE_NX | _PAGE_RW;
+
+     for_each_possible_cpu(cpu) {
+             data = per_cpu(runtime_data, cpu);
+
+             address = __pa(&data->ghcb_page);
+             pfn = address >> PAGE_SHIFT;
+
+             if (kernel_map_pages_in_pgd(pgd, pfn, address, 1, pflags))
+                     return 1;
+     }
+
+     return 0;
+}
+
 static enum es_result vc_handle_msr(struct ghcb *ghcb, struct es_em_ctxt *ctxt)
 {
      struct pt_regs *regs = ctxt->regs;
arch/x86/platform/efi/efi_64.c +10 -1
index 6af4da1149ba..8f5759df7776 100644
@@ -47,6 +47,7 @@
 #include <asm/realmode.h>
 #include <asm/time.h>
 #include <asm/pgalloc.h>
+#include <asm/sev-es.h>
 
 /*
  * We allocate runtime services regions top-down, starting from -4G, i.e.
@@ -229,6 +230,15 @@ int __init efi_setup_page_tables(unsigned long pa_memmap, unsigned num_pages)
              return 1;
      }
 
+     /*
+      * When SEV-ES is active, the GHCB as set by the kernel will be used
+      * by firmware. Create a 1:1 unencrypted mapping for each GHCB.
+      */
+     if (sev_es_efi_map_ghcbs(pgd)) {
+             pr_err("Failed to create 1:1 mapping for the GHCBs!\n");
+             return 1;
+     }
+
      /*
       * When making calls to the firmware everything needs to be 1:1
       * mapped and addressable with 32-bit pointers. Map the kernel
--
Quoted 2 lines

2.28.0

--
Regards/Gruss,
Boris.

https://people.kernel.org/tglx/notes-about-netiquette

#167 lore raw

Re: [PATCH v7 71/72] x86/efi: Add GHCB mappings when SEV-ES is active

Quoted 12 lines · Ard Biesheuvel

(adding Laszlo and Brijesh)

Quoted 9 lines · Borislav Petkov

+ Ard so that he can ack the efi bits.

Quoted 5 lines · Joerg Roedel

From: Tom Lendacky <[email protected]>

Calling down to EFI runtime services can result in the firmware performing VMGEXIT calls. The firmware is likely to use the GHCB of the OS (e.g., for setting EFI variables),

I've had to stare at this for a while.

Because, normally a VMGEXIT is supposed to occur like this:

- guest does something privileged
- resultant non-automatic exit (NAE) injects a #VC exception
- exception handler figures out what that privileged thing was
- exception handler submits request to hypervisor via GHCB contents plus VMGEXIT instruction

Point being, the agent that "owns" the exception handler is supposed to pre-allocate or otherwise provide the GHCB too, for information passing.

So... what is the particular NAE that occurs during the execution of UEFI runtime services (at OS runtime)?

And assuming it occurs, I'm unsure if the exception handler (IDT) at that point is owned (temporarily?) by the firmware.

- If the #VC handler comes from the firmware, then I don't know why it would use the OS's GHCB.

- If the #VC handler comes from the OS, then I don't understand why the commit message says "firmware performing VMGEXIT", given that in this case it would be the OS's #VC handler executing VMGEXIT.

So, I think the above commit message implies a VMGEXIT *without* a NAE / #VC context. (Because, I fail to interpret the commit message in a NAE / #VC context in any way; see above.)

OK, so let's see where the firmware performs a VMGEXIT *outside* of an exception handler, *while* at OS runtime. There seems to be one, in file "OvmfPkg/QemuFlashFvbServicesRuntimeDxe/QemuFlashDxe.c":

Quoted 27 lines

VOID
QemuFlashPtrWrite (

  IN        volatile UINT8    *Ptr,
  IN        UINT8             Value
  )
{
  if (MemEncryptSevEsIsEnabled ()) {
    MSR_SEV_ES_GHCB_REGISTER  Msr;
    GHCB                      *Ghcb;

    Msr.GhcbPhysicalAddress = AsmReadMsr64 (MSR_SEV_ES_GHCB);
    Ghcb = Msr.Ghcb;

    //
    // Writing to flash is emulated by the hypervisor through the use of write
    // protection. This won't work for an SEV-ES guest because the write won't
    // be recognized as a true MMIO write, which would result in the required
    // #VC exception. Instead, use the the VMGEXIT MMIO write support directly
    // to perform the update.
    //
    VmgInit (Ghcb);
    Ghcb->SharedBuffer[0] = Value;
    Ghcb->SaveArea.SwScratch = (UINT64) (UINTN) Ghcb->SharedBuffer;
    VmgExit (Ghcb, SVM_EXIT_MMIO_WRITE, (UINT64) (UINTN) Ptr, 1);
    VmgDone (Ghcb);
  } else {
    *Ptr = Value;
Quoted 2 lines

}
}

This function *does* run at OS runtime (as a part of non-volatile UEFI variable writes).

And note that, wherever MSR_SEV_ES_GHCB points to at the moment, is used as GHCB.

If the guest kernel allocates its own GHCB and writes the allocation address to MSR_SEV_ES_GHCB, then indeed the firmware will use the GHCB of the OS.

I reviewed edk2 commit 437eb3f7a8db ("OvmfPkg/QemuFlashFvbServicesRuntimeDxe: Bypass flash detection with SEV-ES", 2020-08-17), but I admit I never thought of the guest OS changing MSR_SEV_ES_GHCB. I'm sorry about that.

As long as this driver is running before OS runtime (i.e., during the DXE and BDS phases), MSR_SEV_ES_GHCB is supposed to carry the value we set in "OvmfPkg/PlatformPei/AmdSev.c":

Quoted 53 lines

STATIC
VOID
AmdSevEsInitialize (
VOID
)
{

  VOID              *GhcbBase;
  PHYSICAL_ADDRESS  GhcbBasePa;
  UINTN             GhcbPageCount, PageCount;
  RETURN_STATUS     PcdStatus, DecryptStatus;
  IA32_DESCRIPTOR   Gdtr;
  VOID              *Gdt;
  if (!MemEncryptSevEsIsEnabled ()) {
    return;
  }

  PcdStatus = PcdSetBoolS (PcdSevEsIsEnabled, TRUE);
  ASSERT_RETURN_ERROR (PcdStatus);

//
// Allocate GHCB and per-CPU variable pages.
// Since the pages must survive across the UEFI to OS transition
// make them reserved.
//
GhcbPageCount = mMaxCpuCount * 2; GhcbBase = AllocateReservedPages (GhcbPageCount); ASSERT (GhcbBase != NULL);

  GhcbBasePa = (PHYSICAL_ADDRESS)(UINTN) GhcbBase;

//
// Each vCPU gets two consecutive pages, the first is the GHCB and the // second is the per-CPU variable page. Loop through the allocation and // only clear the encryption mask for the GHCB pages.
//

  for (PageCount = 0; PageCount < GhcbPageCount; PageCount += 2) {
    DecryptStatus = MemEncryptSevClearPageEncMask (
      0,
      GhcbBasePa + EFI_PAGES_TO_SIZE (PageCount),
      1,
      TRUE
      );
    ASSERT_RETURN_ERROR (DecryptStatus);

}

  ZeroMem (GhcbBase, EFI_PAGES_TO_SIZE (GhcbPageCount));

  PcdStatus = PcdSet64S (PcdGhcbBase, GhcbBasePa);
  ASSERT_RETURN_ERROR (PcdStatus);
  PcdStatus = PcdSet64S (PcdGhcbSize, EFI_PAGES_TO_SIZE (GhcbPageCount));
  ASSERT_RETURN_ERROR (PcdStatus);
Quoted 5 lines

DEBUG ((DEBUG_INFO,

    "SEV-ES is enabled, %lu GHCB pages allocated starting at 0x%p\n",
    (UINT64)GhcbPageCount, GhcbBase));
  AsmWriteMsr64 (MSR_SEV_ES_GHCB, GhcbBasePa);

So what is the *actual* problem at OS runtime:

- Is it that MSR_SEV_ES_GHCB still points at this PEI-phase *reserved* memory allocation (and so when QemuFlashPtrWrite() tries to access it during OS runtime, it doesn't have a runtime mapping for it)?

- Or is it that the OS actively changes MSR_SEV_ES_GHCB, pointing to a memory area that the OS owns -- and *that* area is what QemuFlashPtrWrite() cannot access at OS runtime?

The first problem statement does *not* seem to apply, given -- again -- that the commit message says, "firmware is likely to use the GHCB of the OS".

So I think the second problem statement must apply.

(I think the "reserved allocation" above is "reserved" only because we want to keep the OS out of it around the ExitBootServices() transition.)

Back to the email:

Quoted 4 lines · Ard Biesheuvel
Quoted 3 lines · Borislav Petkov
Quoted 2 lines · Joerg Roedel

so each GHCB in the system needs to be identity
mapped in the EFI page tables, as unencrypted, to avoid page faults.

Not sure I agree about this, but at least it seems to confirm my understanding -- apparently the idea is, for the OS, to satisfy QemuFlashPtrWrite() in the firmware, by putting the "expected" mapping -- for wherever MSR_SEV_ES_GHCB is going to point to -- in place.

Quoted 6 lines
Quoted 4 lines
Quoted 4 lines
[ [email protected]: Moved GHCB mapping loop to sev-es.c ]
Quoted 6 lines

This looks like it is papering over a more fundamental issue: any memory region that the firmware itself needs to access during the execution of runtime services needs to be described in the UEFI memory map, with the appropriate annotations so that the OS knows it should include these in the EFI runtime page tables. So why has this been omitted in this case?

So yeah, the issue seems to be that the QemuFlashFvbServicesRuntimeDxe driver does not *own* the GHCB that it attempts to use at OS runtime. It doesn't know where MSR_SEV_ES_GHCB is going to point.

Is QemuFlashFvbServicesRuntimeDxe permitted to change MSR_SEV_ES_GHCB *temporarily* at OS runtime?

Because, in that case:

- QemuFlashFvbServicesRuntimeDxe should allocate a Runtime Services Data block for GHCB when it starts up (if SEV-ES is active),

- QemuFlashFvbServicesRuntimeDxe should register a SetVirtualAddressMap handler, and use EfiConvertPointer() from UefiRuntimeLib to convert the "runtime GHCB" address to virtual address, in that handler,

- QemuFlashPtrWrite() should call EfiAtRuntime() from UefiRuntimeLib, and if the latter returns TRUE, then (a) use the runtime-converted address for populating the GHCB, and (b) temporarily swap MSR_SEV_ES_GHCB with the address of the self-allocated GHCB. (The MSR needs a *physical* address, so QemuFlashFvbServicesRuntimeDxe would have to remember / retain the original (physical) allocation address too.)

If QemuFlashFvbServicesRuntimeDxe is not permitted to change MSR_SEV_ES_GHCB even temporarily (at OS runtime), then I think the approach proposed in this (guest kernel) patch is valid.

Let me skim the code below...

Quoted 48 lines
Quoted 48 lines
Quoted 48 lines


---
 arch/x86/boot/compressed/sev-es.c |  1 +
 arch/x86/include/asm/sev-es.h     |  2 ++
 arch/x86/kernel/sev-es.c          | 30 ++++++++++++++++++++++++++++++
 arch/x86/platform/efi/efi_64.c    | 10 ++++++++++
 4 files changed, 43 insertions(+)
arch/x86/boot/compressed/sev-es.c +1 -0
index 45702b866c33..0a9a248ca33d 100644
@@ -12,6 +12,7 @@
  */
 #include "misc.h"
 
+#include <asm/pgtable_types.h>
 #include <asm/sev-es.h>
 #include <asm/trapnr.h>
 #include <asm/trap_pf.h>
arch/x86/include/asm/sev-es.h +2 -0
index e919f09ae33c..cf1d957c7091 100644
@@ -102,11 +102,13 @@ static __always_inline void sev_es_nmi_complete(void)
      if (static_branch_unlikely(&sev_es_enable_key))
              __sev_es_nmi_complete();
 }
+extern int __init sev_es_efi_map_ghcbs(pgd_t *pgd);
 #else
 static inline void sev_es_ist_enter(struct pt_regs *regs) { }
 static inline void sev_es_ist_exit(void) { }
 static inline int sev_es_setup_ap_jump_table(struct real_mode_header *rmh) { return 0; }
 static inline void sev_es_nmi_complete(void) { }
+static inline int sev_es_efi_map_ghcbs(pgd_t *pgd) { return 0; }
 #endif
 
 #endif
arch/x86/kernel/sev-es.c +0 -0
index 9ab3a4dfecd8..4e2b7e4d9b87 100644
@@ -491,6 +491,36 @@ int sev_es_setup_ap_jump_table(struct real_mode_header *rmh)
      return 0;
 }
 
Quoted 4 lines
Quoted 4 lines

arch/x86/kernel/sev-es.c int sev_es_setup_ap_jump_table(struct real_mode_header *rmh) · line 497

Quoted 4 lines
arch/x86/kernel/sev-es.c +4 -0
+/*
+ * This is needed by the OVMF UEFI firmware which will use whatever it finds in
+ * the GHCB MSR as its GHCB to talk to the hypervisor. So make sure the per-cpu
+ * runtime GHCBs used by the kernel are also mapped in the EFI page-table.

Yup, this pretty much confirms my suspicion that QemuFlashPtrWrite() is at the center of this.

(BTW, I don't think that the runtime services data allocation, in QemuFlashFvbServicesRuntimeDxe, for OS runtime GHCB purposes, would have to be "per CPU". Refer to "Table 35. Rules for Reentry Into Runtime Services" in the UEFI spec -- if one processor is executing SetVariable(), then no other processor must enter SetVariable(). And so we'll have *at most* one VCPU in QemuFlashPtrWrite(), at any time.)

Quoted 45 lines
Quoted 45 lines
Quoted 45 lines
arch/x86/kernel/sev-es.c +26 -0
+ */
+int __init sev_es_efi_map_ghcbs(pgd_t *pgd)
+{
+     struct sev_es_runtime_data *data;
+     unsigned long address, pflags;
+     int cpu;
+     u64 pfn;
+
+     if (!sev_es_active())
+             return 0;
+
+     pflags = _PAGE_NX | _PAGE_RW;
+
+     for_each_possible_cpu(cpu) {
+             data = per_cpu(runtime_data, cpu);
+
+             address = __pa(&data->ghcb_page);
+             pfn = address >> PAGE_SHIFT;
+
+             if (kernel_map_pages_in_pgd(pgd, pfn, address, 1, pflags))
+                     return 1;
+     }
+
+     return 0;
+}
+
 static enum es_result vc_handle_msr(struct ghcb *ghcb, struct es_em_ctxt *ctxt)
 {
      struct pt_regs *regs = ctxt->regs;
arch/x86/platform/efi/efi_64.c +1 -0
index 6af4da1149ba..8f5759df7776 100644
@@ -47,6 +47,7 @@
 #include <asm/realmode.h>
 #include <asm/time.h>
 #include <asm/pgalloc.h>
+#include <asm/sev-es.h>
 
 /*
  * We allocate runtime services regions top-down, starting from -4G, i.e.
@@ -229,6 +230,15 @@ int __init efi_setup_page_tables(unsigned long pa_memmap, unsigned num_pages)
              return 1;
      }
 
Quoted 12 lines
Quoted 4 lines
Quoted 4 lines
arch/x86/platform/efi/efi_64.c +4 -0
+     /*
+      * When SEV-ES is active, the GHCB as set by the kernel will be used
+      * by firmware. Create a 1:1 unencrypted mapping for each GHCB.
+      */
Quoted 8 lines

arch/x86/platform/efi/efi_64.c int __init efi_setup_page_tables(unsigned long pa_memmap, unsigned num_pages) · line 244

Quoted 8 lines
arch/x86/platform/efi/efi_64.c +5 -0
+     if (sev_es_efi_map_ghcbs(pgd)) {
+             pr_err("Failed to create 1:1 mapping for the GHCBs!\n");
+             return 1;
+     }
+
      /*
       * When making calls to the firmware everything needs to be 1:1
       * mapped and addressable with 32-bit pointers. Map the kernel

Good point!

And it even makes me wonder if the QemuFlashFvbServicesRuntimeDxe approach, with the runtime services data type memory allocation, is feasible at all. Namely, a page's encryption status, under SEV, is controlled through the PTE.

And for this particular UEFI runtime area, it would *not* suffice for the OS to just virt-map it. The OS would also have to *decrypt* the area (mark the PTE as "plaintext").

In other words, it would be an "unprecedented" PTE for the OS to set up:
the PTE would not only map the GVA to GPA, but also mark the area as "plaintext".

Otherwise -- if the OS covers *just* the virt-mapping --, QemuFlashFvbServicesRuntimeDxe would populate its own "runtime GHCB" area just fine, but the actual data hitting the host RAM would be encrypted. And so the hypervisor could not interpret the GHCB.

*If* QemuFlashFvbServicesRuntimeDxe should not change the kernel-owned PTE at runtime, even temporarily, for marking the GHCB as "plaintext", then the problem is indeed only solvable in the guest kernel, in my opinion.

There simply isn't an "architected annotation" for telling the kernel, "virt-map this runtime services data type memory range, *and* mark it as plaintext at the same time".

This would be necessary, as both actions affect the exact same PTE, and the firmware is not really allowed to touch the PTE at runtime. But we don't have such a hint.

To summarize: for QemuFlashFvbServicesRuntimeDxe to allocate UEFI Runtime Services Data type memory, for its own runtime GHCB, two permissions are necessary (together), at OS runtime:

- QemuFlashFvbServicesRuntimeDxe must be allowed to swap MSR_SEV_ES_GHCB temporarily (before executing VMGEXIT),

- QemuFlashFvbServicesRuntimeDxe must be allowed to change the OS-owned PTE temporarily (for remapping the GHCB as plaintext, before writing to it).

Thanks
Laszlo

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

#168 lore raw

Re: [PATCH v7 71/72] x86/efi: Add GHCB mappings when SEV-ES is active

Quoted 10 lines · Laszlo Ersek

To summarize: for QemuFlashFvbServicesRuntimeDxe to allocate UEFI Runtime Services Data type memory, for its own runtime GHCB, two permissions are necessary (together), at OS runtime:

- QemuFlashFvbServicesRuntimeDxe must be allowed to swap MSR_SEV_ES_GHCB temporarily (before executing VMGEXIT),

- QemuFlashFvbServicesRuntimeDxe must be allowed to change the OS-owned PTE temporarily (for remapping the GHCB as plaintext, before writing to it).

Condition#2 gets worse:

If the firmware-allocated runtime GHCB happens to be virt-mapped by the OS using a 2MB page (covering other UEFI runtime data areas, perhaps?), then simply flipping the encryption bit in QemuFlashFvbServicesRuntimeDxe would mark more runtime memory than just the GHCB as "plaintext". (2MB-4KB specifically.)

This could result in:
- firmware read accesses outside of the GHCB returning garbage
- firmware write accesses ouside of the GHCB leaking information to the hypervisor, and reading back later as garbage

In order to prevent those symptoms, the firmware would have to split the 2MB page to 4KB pages, and decrypt just the one (GHCB) page.

But page splitting requires additional memory (for the finer granularity page table), and fw memory cannot be allocated at OS runtime. So that extra memory too would have to be pre-allocated by the firmware. Nasty.

I'd recommend sticking with this kernel patch.

Thanks,
Laszlo

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

#169 lore raw

Re: [PATCH v7 71/72] x86/efi: Add GHCB mappings when SEV-ES is active

In reply to Laszlo Ersek
Quoted 36 lines · Laszlo Ersek
Quoted 12 lines · Ard Biesheuvel

(adding Laszlo and Brijesh)

Quoted 9 lines · Borislav Petkov

+ Ard so that he can ack the efi bits.

Quoted 5 lines · Joerg Roedel

From: Tom Lendacky <[email protected]>

Calling down to EFI runtime services can result in the firmware performing VMGEXIT calls. The firmware is likely to use the GHCB of the OS (e.g., for setting EFI variables),

I've had to stare at this for a while.

Because, normally a VMGEXIT is supposed to occur like this:

- guest does something privileged
- resultant non-automatic exit (NAE) injects a #VC exception
- exception handler figures out what that privileged thing was
- exception handler submits request to hypervisor via GHCB contents plus VMGEXIT instruction

Point being, the agent that "owns" the exception handler is supposed to pre-allocate or otherwise provide the GHCB too, for information passing.

So... what is the particular NAE that occurs during the execution of UEFI runtime services (at OS runtime)?

And assuming it occurs, I'm unsure if the exception handler (IDT) at that point is owned (temporarily?) by the firmware.

- If the #VC handler comes from the firmware, then I don't know why it would use the OS's GHCB.

Quoted 7 lines

- If the #VC handler comes from the OS, then I don't understand why the commit message says "firmware performing VMGEXIT", given that in this case it would be the OS's #VC handler executing VMGEXIT.

So, I think the above commit message implies a VMGEXIT *without* a NAE / #VC context. (Because, I fail to interpret the commit message in a NAE / #VC context in any way; see above.)

Correct.

Quoted 4 lines

OK, so let's see where the firmware performs a VMGEXIT *outside* of an exception handler, *while* at OS runtime. There seems to be one, in file "OvmfPkg/QemuFlashFvbServicesRuntimeDxe/QemuFlashDxe.c":

Again, correct. Basically this is what is invoked when setting UEFI variables.

Quoted 107 lines
Quoted 22 lines

VOID
QemuFlashPtrWrite (

   IN        volatile UINT8    *Ptr,
   IN        UINT8             Value
   )
{
   if (MemEncryptSevEsIsEnabled ()) {
     MSR_SEV_ES_GHCB_REGISTER  Msr;
     GHCB                      *Ghcb;

     Msr.GhcbPhysicalAddress = AsmReadMsr64 (MSR_SEV_ES_GHCB);
     Ghcb = Msr.Ghcb;

     //
     // Writing to flash is emulated by the hypervisor through the use of write
     // protection. This won't work for an SEV-ES guest because the write won't
     // be recognized as a true MMIO write, which would result in the required
     // #VC exception. Instead, use the the VMGEXIT MMIO write support directly
     // to perform the update.
     //
     VmgInit (Ghcb);
Quoted 8 lines
     Ghcb->SharedBuffer[0] = Value;
     Ghcb->SaveArea.SwScratch = (UINT64) (UINTN) Ghcb->SharedBuffer;
     VmgExit (Ghcb, SVM_EXIT_MMIO_WRITE, (UINT64) (UINTN) Ptr, 1);
     VmgDone (Ghcb);
   } else {
     *Ptr = Value;
   }
}

This function *does* run at OS runtime (as a part of non-volatile UEFI variable writes).

And note that, wherever MSR_SEV_ES_GHCB points to at the moment, is used as GHCB.

If the guest kernel allocates its own GHCB and writes the allocation address to MSR_SEV_ES_GHCB, then indeed the firmware will use the GHCB of the OS.

I reviewed edk2 commit 437eb3f7a8db ("OvmfPkg/QemuFlashFvbServicesRuntimeDxe: Bypass flash detection with SEV-ES", 2020-08-17), but I admit I never thought of the guest OS changing MSR_SEV_ES_GHCB. I'm sorry about that.

As long as this driver is running before OS runtime (i.e., during the DXE and BDS phases), MSR_SEV_ES_GHCB is supposed to carry the value we set in "OvmfPkg/PlatformPei/AmdSev.c":

Quoted 57 lines

STATIC
VOID
AmdSevEsInitialize (
VOID
)

{
   VOID              *GhcbBase;
   PHYSICAL_ADDRESS  GhcbBasePa;
   UINTN             GhcbPageCount, PageCount;
   RETURN_STATUS     PcdStatus, DecryptStatus;
   IA32_DESCRIPTOR   Gdtr;
   VOID              *Gdt;

   if (!MemEncryptSevEsIsEnabled ()) {
     return;
   }

   PcdStatus = PcdSetBoolS (PcdSevEsIsEnabled, TRUE);
   ASSERT_RETURN_ERROR (PcdStatus);

   //
   // Allocate GHCB and per-CPU variable pages.
   //   Since the pages must survive across the UEFI to OS transition
   //   make them reserved.
   //
   GhcbPageCount = mMaxCpuCount * 2;
   GhcbBase = AllocateReservedPages (GhcbPageCount);
   ASSERT (GhcbBase != NULL);

   GhcbBasePa = (PHYSICAL_ADDRESS)(UINTN) GhcbBase;

   //
   // Each vCPU gets two consecutive pages, the first is the GHCB and the
   // second is the per-CPU variable page. Loop through the allocation and
   // only clear the encryption mask for the GHCB pages.
   //
   for (PageCount = 0; PageCount < GhcbPageCount; PageCount += 2) {
     DecryptStatus = MemEncryptSevClearPageEncMask (
       0,
       GhcbBasePa + EFI_PAGES_TO_SIZE (PageCount),
       1,
       TRUE
       );
     ASSERT_RETURN_ERROR (DecryptStatus);
   }

   ZeroMem (GhcbBase, EFI_PAGES_TO_SIZE (GhcbPageCount));

   PcdStatus = PcdSet64S (PcdGhcbBase, GhcbBasePa);
   ASSERT_RETURN_ERROR (PcdStatus);
   PcdStatus = PcdSet64S (PcdGhcbSize, EFI_PAGES_TO_SIZE (GhcbPageCount));
   ASSERT_RETURN_ERROR (PcdStatus);

   DEBUG ((DEBUG_INFO,
     "SEV-ES is enabled, %lu GHCB pages allocated starting at 0x%p\n",
     (UINT64)GhcbPageCount, GhcbBase));
Quoted 7 lines
Quoted 1 line
   AsmWriteMsr64 (MSR_SEV_ES_GHCB, GhcbBasePa);

So what is the *actual* problem at OS runtime:

- Is it that MSR_SEV_ES_GHCB still points at this PEI-phase *reserved* memory allocation (and so when QemuFlashPtrWrite() tries to access it during OS runtime, it doesn't have a runtime mapping for it)?

At this point the GHCB MSR points to the OS GHCB, which isn't mapped by the page tables supplied by the OS and used by UEFI.

Quoted 4 lines

- Or is it that the OS actively changes MSR_SEV_ES_GHCB, pointing to a memory area that the OS owns -- and *that* area is what QemuFlashPtrWrite() cannot access at OS runtime?

Correct.

Quoted 225 lines

The first problem statement does *not* seem to apply, given -- again -- that the commit message says, "firmware is likely to use the GHCB of the OS".

So I think the second problem statement must apply.

(I think the "reserved allocation" above is "reserved" only because we want to keep the OS out of it around the ExitBootServices() transition.)

Back to the email:

Quoted 4 lines · Ard Biesheuvel
Quoted 3 lines · Borislav Petkov
Quoted 2 lines · Joerg Roedel

so each GHCB in the system needs to be identity
mapped in the EFI page tables, as unencrypted, to avoid page faults.

Not sure I agree about this, but at least it seems to confirm my understanding -- apparently the idea is, for the OS, to satisfy QemuFlashPtrWrite() in the firmware, by putting the "expected" mapping -- for wherever MSR_SEV_ES_GHCB is going to point to -- in place.

Quoted 6 lines
Quoted 4 lines
Quoted 4 lines
[ [email protected]: Moved GHCB mapping loop to sev-es.c ]
Quoted 6 lines

This looks like it is papering over a more fundamental issue: any memory region that the firmware itself needs to access during the execution of runtime services needs to be described in the UEFI memory map, with the appropriate annotations so that the OS knows it should include these in the EFI runtime page tables. So why has this been omitted in this case?

So yeah, the issue seems to be that the QemuFlashFvbServicesRuntimeDxe driver does not *own* the GHCB that it attempts to use at OS runtime. It doesn't know where MSR_SEV_ES_GHCB is going to point.

Is QemuFlashFvbServicesRuntimeDxe permitted to change MSR_SEV_ES_GHCB *temporarily* at OS runtime?

Because, in that case:

- QemuFlashFvbServicesRuntimeDxe should allocate a Runtime Services Data block for GHCB when it starts up (if SEV-ES is active),

- QemuFlashFvbServicesRuntimeDxe should register a SetVirtualAddressMap handler, and use EfiConvertPointer() from UefiRuntimeLib to convert the "runtime GHCB" address to virtual address, in that handler,

- QemuFlashPtrWrite() should call EfiAtRuntime() from UefiRuntimeLib, and if the latter returns TRUE, then (a) use the runtime-converted address for populating the GHCB, and (b) temporarily swap MSR_SEV_ES_GHCB with the address of the self-allocated GHCB. (The MSR needs a *physical* address, so QemuFlashFvbServicesRuntimeDxe would have to remember / retain the original (physical) allocation address too.)

If QemuFlashFvbServicesRuntimeDxe is not permitted to change MSR_SEV_ES_GHCB even temporarily (at OS runtime), then I think the approach proposed in this (guest kernel) patch is valid.

Let me skim the code below...

Quoted 48 lines
Quoted 48 lines
Quoted 48 lines


---
  arch/x86/boot/compressed/sev-es.c |  1 +
  arch/x86/include/asm/sev-es.h     |  2 ++
  arch/x86/kernel/sev-es.c          | 30 ++++++++++++++++++++++++++++++
  arch/x86/platform/efi/efi_64.c    | 10 ++++++++++
  4 files changed, 43 insertions(+)
arch/x86/boot/compressed/sev-es.c +1 -0
index 45702b866c33..0a9a248ca33d 100644
@@ -12,6 +12,7 @@
   */
  #include "misc.h"
 
+#include <asm/pgtable_types.h>
  #include <asm/sev-es.h>
  #include <asm/trapnr.h>
  #include <asm/trap_pf.h>
arch/x86/include/asm/sev-es.h +2 -0
index e919f09ae33c..cf1d957c7091 100644
@@ -102,11 +102,13 @@ static __always_inline void sev_es_nmi_complete(void)
       if (static_branch_unlikely(&sev_es_enable_key))
               __sev_es_nmi_complete();
  }
+extern int __init sev_es_efi_map_ghcbs(pgd_t *pgd);
  #else
  static inline void sev_es_ist_enter(struct pt_regs *regs) { }
  static inline void sev_es_ist_exit(void) { }
  static inline int sev_es_setup_ap_jump_table(struct real_mode_header *rmh) { return 0; }
  static inline void sev_es_nmi_complete(void) { }
+static inline int sev_es_efi_map_ghcbs(pgd_t *pgd) { return 0; }
  #endif
 
  #endif
arch/x86/kernel/sev-es.c +0 -0
index 9ab3a4dfecd8..4e2b7e4d9b87 100644
@@ -491,6 +491,36 @@ int sev_es_setup_ap_jump_table(struct real_mode_header *rmh)
       return 0;
  }
 
Quoted 4 lines
Quoted 4 lines

arch/x86/kernel/sev-es.c int sev_es_setup_ap_jump_table(struct real_mode_header *rmh) · line 497

Quoted 4 lines
arch/x86/kernel/sev-es.c +4 -0
+/*
+ * This is needed by the OVMF UEFI firmware which will use whatever it finds in
+ * the GHCB MSR as its GHCB to talk to the hypervisor. So make sure the per-cpu
+ * runtime GHCBs used by the kernel are also mapped in the EFI page-table.

Yup, this pretty much confirms my suspicion that QemuFlashPtrWrite() is at the center of this.

(BTW, I don't think that the runtime services data allocation, in QemuFlashFvbServicesRuntimeDxe, for OS runtime GHCB purposes, would have to be "per CPU". Refer to "Table 35. Rules for Reentry Into Runtime Services" in the UEFI spec -- if one processor is executing SetVariable(), then no other processor must enter SetVariable(). And so we'll have *at most* one VCPU in QemuFlashPtrWrite(), at any time.)

Quoted 45 lines
Quoted 45 lines
Quoted 45 lines
arch/x86/kernel/sev-es.c +26 -0
+ */
+int __init sev_es_efi_map_ghcbs(pgd_t *pgd)
+{
+     struct sev_es_runtime_data *data;
+     unsigned long address, pflags;
+     int cpu;
+     u64 pfn;
+
+     if (!sev_es_active())
+             return 0;
+
+     pflags = _PAGE_NX | _PAGE_RW;
+
+     for_each_possible_cpu(cpu) {
+             data = per_cpu(runtime_data, cpu);
+
+             address = __pa(&data->ghcb_page);
+             pfn = address >> PAGE_SHIFT;
+
+             if (kernel_map_pages_in_pgd(pgd, pfn, address, 1, pflags))
+                     return 1;
+     }
+
+     return 0;
+}
+
  static enum es_result vc_handle_msr(struct ghcb *ghcb, struct es_em_ctxt *ctxt)
  {
       struct pt_regs *regs = ctxt->regs;
arch/x86/platform/efi/efi_64.c +1 -0
index 6af4da1149ba..8f5759df7776 100644
@@ -47,6 +47,7 @@
  #include <asm/realmode.h>
  #include <asm/time.h>
  #include <asm/pgalloc.h>
+#include <asm/sev-es.h>
 
  /*
   * We allocate runtime services regions top-down, starting from -4G, i.e.
@@ -229,6 +230,15 @@ int __init efi_setup_page_tables(unsigned long pa_memmap, unsigned num_pages)
               return 1;
       }
 
Quoted 12 lines
Quoted 4 lines
Quoted 4 lines
arch/x86/platform/efi/efi_64.c +4 -0
+     /*
+      * When SEV-ES is active, the GHCB as set by the kernel will be used
+      * by firmware. Create a 1:1 unencrypted mapping for each GHCB.
+      */
Quoted 8 lines

arch/x86/platform/efi/efi_64.c int __init efi_setup_page_tables(unsigned long pa_memmap, unsigned num_pages) · line 244

Quoted 8 lines
arch/x86/platform/efi/efi_64.c +5 -0
+     if (sev_es_efi_map_ghcbs(pgd)) {
+             pr_err("Failed to create 1:1 mapping for the GHCBs!\n");
+             return 1;
+     }
+
       /*
        * When making calls to the firmware everything needs to be 1:1
        * mapped and addressable with 32-bit pointers. Map the kernel

Good point!

And it even makes me wonder if the QemuFlashFvbServicesRuntimeDxe approach, with the runtime services data type memory allocation, is feasible at all. Namely, a page's encryption status, under SEV, is controlled through the PTE.

And for this particular UEFI runtime area, it would *not* suffice for the OS to just virt-map it. The OS would also have to *decrypt* the area (mark the PTE as "plaintext").

In other words, it would be an "unprecedented" PTE for the OS to set up:
the PTE would not only map the GVA to GPA, but also mark the area as "plaintext".

Otherwise -- if the OS covers *just* the virt-mapping --, QemuFlashFvbServicesRuntimeDxe would populate its own "runtime GHCB" area just fine, but the actual data hitting the host RAM would be encrypted. And so the hypervisor could not interpret the GHCB.

*If* QemuFlashFvbServicesRuntimeDxe should not change the kernel-owned PTE at runtime, even temporarily, for marking the GHCB as "plaintext", then the problem is indeed only solvable in the guest kernel, in my opinion.

There simply isn't an "architected annotation" for telling the kernel, "virt-map this runtime services data type memory range, *and* mark it as plaintext at the same time".

This would be necessary, as both actions affect the exact same PTE, and the firmware is not really allowed to touch the PTE at runtime. But we don't have such a hint.

To summarize: for QemuFlashFvbServicesRuntimeDxe to allocate UEFI Runtime Services Data type memory, for its own runtime GHCB, two permissions are necessary (together), at OS runtime:

Quoted 7 lines

- QemuFlashFvbServicesRuntimeDxe must be allowed to swap MSR_SEV_ES_GHCB temporarily (before executing VMGEXIT),

- QemuFlashFvbServicesRuntimeDxe must be allowed to change the OS-owned PTE temporarily (for remapping the GHCB as plaintext, before writing to it).

Amazing summarization Laszlo!

Thanks,
Tom

Quoted 3 lines

Thanks
Laszlo

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

maintainer Acked-by
#170 lore raw

Re: [PATCH v7 71/72] x86/efi: Add GHCB mappings when SEV-ES is active

Quoted 408 lines · Tom Lendacky
Quoted 36 lines · Laszlo Ersek
Quoted 12 lines · Ard Biesheuvel

(adding Laszlo and Brijesh)

Quoted 9 lines · Borislav Petkov

+ Ard so that he can ack the efi bits.

Quoted 5 lines · Joerg Roedel

From: Tom Lendacky <[email protected]>

Calling down to EFI runtime services can result in the firmware performing VMGEXIT calls. The firmware is likely to use the GHCB of the OS (e.g., for setting EFI variables),

I've had to stare at this for a while.

Because, normally a VMGEXIT is supposed to occur like this:

- guest does something privileged
- resultant non-automatic exit (NAE) injects a #VC exception
- exception handler figures out what that privileged thing was
- exception handler submits request to hypervisor via GHCB contents plus VMGEXIT instruction

Point being, the agent that "owns" the exception handler is supposed to pre-allocate or otherwise provide the GHCB too, for information passing.

So... what is the particular NAE that occurs during the execution of UEFI runtime services (at OS runtime)?

And assuming it occurs, I'm unsure if the exception handler (IDT) at that point is owned (temporarily?) by the firmware.

- If the #VC handler comes from the firmware, then I don't know why it would use the OS's GHCB.

Quoted 7 lines

- If the #VC handler comes from the OS, then I don't understand why the commit message says "firmware performing VMGEXIT", given that in this case it would be the OS's #VC handler executing VMGEXIT.

So, I think the above commit message implies a VMGEXIT *without* a NAE / #VC context. (Because, I fail to interpret the commit message in a NAE / #VC context in any way; see above.)

Correct.

Quoted 4 lines

OK, so let's see where the firmware performs a VMGEXIT *outside* of an exception handler, *while* at OS runtime. There seems to be one, in file "OvmfPkg/QemuFlashFvbServicesRuntimeDxe/QemuFlashDxe.c":

Again, correct. Basically this is what is invoked when setting UEFI variables.

Quoted 107 lines
Quoted 22 lines

VOID
QemuFlashPtrWrite (

   IN        volatile UINT8    *Ptr,
   IN        UINT8             Value
   )
{
   if (MemEncryptSevEsIsEnabled ()) {
     MSR_SEV_ES_GHCB_REGISTER  Msr;
     GHCB                      *Ghcb;

     Msr.GhcbPhysicalAddress = AsmReadMsr64 (MSR_SEV_ES_GHCB);
     Ghcb = Msr.Ghcb;

     //
     // Writing to flash is emulated by the hypervisor through the use of write
     // protection. This won't work for an SEV-ES guest because the write won't
     // be recognized as a true MMIO write, which would result in the required
     // #VC exception. Instead, use the the VMGEXIT MMIO write support directly
     // to perform the update.
     //
     VmgInit (Ghcb);
Quoted 8 lines
     Ghcb->SharedBuffer[0] = Value;
     Ghcb->SaveArea.SwScratch = (UINT64) (UINTN) Ghcb->SharedBuffer;
     VmgExit (Ghcb, SVM_EXIT_MMIO_WRITE, (UINT64) (UINTN) Ptr, 1);
     VmgDone (Ghcb);
   } else {
     *Ptr = Value;
   }
}

This function *does* run at OS runtime (as a part of non-volatile UEFI variable writes).

And note that, wherever MSR_SEV_ES_GHCB points to at the moment, is used as GHCB.

If the guest kernel allocates its own GHCB and writes the allocation address to MSR_SEV_ES_GHCB, then indeed the firmware will use the GHCB of the OS.

I reviewed edk2 commit 437eb3f7a8db ("OvmfPkg/QemuFlashFvbServicesRuntimeDxe: Bypass flash detection with SEV-ES", 2020-08-17), but I admit I never thought of the guest OS changing MSR_SEV_ES_GHCB. I'm sorry about that.

As long as this driver is running before OS runtime (i.e., during the DXE and BDS phases), MSR_SEV_ES_GHCB is supposed to carry the value we set in "OvmfPkg/PlatformPei/AmdSev.c":

Quoted 57 lines

STATIC
VOID
AmdSevEsInitialize (
VOID
)

{
   VOID              *GhcbBase;
   PHYSICAL_ADDRESS  GhcbBasePa;
   UINTN             GhcbPageCount, PageCount;
   RETURN_STATUS     PcdStatus, DecryptStatus;
   IA32_DESCRIPTOR   Gdtr;
   VOID              *Gdt;

   if (!MemEncryptSevEsIsEnabled ()) {
     return;
   }

   PcdStatus = PcdSetBoolS (PcdSevEsIsEnabled, TRUE);
   ASSERT_RETURN_ERROR (PcdStatus);

   //
   // Allocate GHCB and per-CPU variable pages.
   //   Since the pages must survive across the UEFI to OS transition
   //   make them reserved.
   //
   GhcbPageCount = mMaxCpuCount * 2;
   GhcbBase = AllocateReservedPages (GhcbPageCount);
   ASSERT (GhcbBase != NULL);

   GhcbBasePa = (PHYSICAL_ADDRESS)(UINTN) GhcbBase;

   //
   // Each vCPU gets two consecutive pages, the first is the GHCB and the
   // second is the per-CPU variable page. Loop through the allocation and
   // only clear the encryption mask for the GHCB pages.
   //
   for (PageCount = 0; PageCount < GhcbPageCount; PageCount += 2) {
     DecryptStatus = MemEncryptSevClearPageEncMask (
       0,
       GhcbBasePa + EFI_PAGES_TO_SIZE (PageCount),
       1,
       TRUE
       );
     ASSERT_RETURN_ERROR (DecryptStatus);
   }

   ZeroMem (GhcbBase, EFI_PAGES_TO_SIZE (GhcbPageCount));

   PcdStatus = PcdSet64S (PcdGhcbBase, GhcbBasePa);
   ASSERT_RETURN_ERROR (PcdStatus);
   PcdStatus = PcdSet64S (PcdGhcbSize, EFI_PAGES_TO_SIZE (GhcbPageCount));
   ASSERT_RETURN_ERROR (PcdStatus);

   DEBUG ((DEBUG_INFO,
     "SEV-ES is enabled, %lu GHCB pages allocated starting at 0x%p\n",
     (UINT64)GhcbPageCount, GhcbBase));
Quoted 7 lines
Quoted 1 line
   AsmWriteMsr64 (MSR_SEV_ES_GHCB, GhcbBasePa);

So what is the *actual* problem at OS runtime:

- Is it that MSR_SEV_ES_GHCB still points at this PEI-phase *reserved* memory allocation (and so when QemuFlashPtrWrite() tries to access it during OS runtime, it doesn't have a runtime mapping for it)?

At this point the GHCB MSR points to the OS GHCB, which isn't mapped by the page tables supplied by the OS and used by UEFI.

Quoted 4 lines

- Or is it that the OS actively changes MSR_SEV_ES_GHCB, pointing to a memory area that the OS owns -- and *that* area is what QemuFlashPtrWrite() cannot access at OS runtime?

Correct.

Quoted 228 lines

The first problem statement does *not* seem to apply, given -- again -- that the commit message says, "firmware is likely to use the GHCB of the OS".

So I think the second problem statement must apply.

(I think the "reserved allocation" above is "reserved" only because we want to keep the OS out of it around the ExitBootServices() transition.)

Back to the email:

Quoted 4 lines · Ard Biesheuvel
Quoted 3 lines · Borislav Petkov
Quoted 2 lines · Joerg Roedel

so each GHCB in the system needs to be identity
mapped in the EFI page tables, as unencrypted, to avoid page faults.

Not sure I agree about this, but at least it seems to confirm my understanding -- apparently the idea is, for the OS, to satisfy QemuFlashPtrWrite() in the firmware, by putting the "expected" mapping -- for wherever MSR_SEV_ES_GHCB is going to point to -- in place.

Quoted 6 lines
Quoted 4 lines
Quoted 4 lines
[ [email protected]: Moved GHCB mapping loop to sev-es.c ]
Quoted 6 lines

This looks like it is papering over a more fundamental issue: any memory region that the firmware itself needs to access during the execution of runtime services needs to be described in the UEFI memory map, with the appropriate annotations so that the OS knows it should include these in the EFI runtime page tables. So why has this been omitted in this case?

So yeah, the issue seems to be that the QemuFlashFvbServicesRuntimeDxe driver does not *own* the GHCB that it attempts to use at OS runtime. It doesn't know where MSR_SEV_ES_GHCB is going to point.

Is QemuFlashFvbServicesRuntimeDxe permitted to change MSR_SEV_ES_GHCB *temporarily* at OS runtime?

Because, in that case:

- QemuFlashFvbServicesRuntimeDxe should allocate a Runtime Services Data block for GHCB when it starts up (if SEV-ES is active),

- QemuFlashFvbServicesRuntimeDxe should register a SetVirtualAddressMap handler, and use EfiConvertPointer() from UefiRuntimeLib to convert the "runtime GHCB" address to virtual address, in that handler,

- QemuFlashPtrWrite() should call EfiAtRuntime() from UefiRuntimeLib, and if the latter returns TRUE, then (a) use the runtime-converted address for populating the GHCB, and (b) temporarily swap MSR_SEV_ES_GHCB with the address of the self-allocated GHCB. (The MSR needs a *physical* address, so QemuFlashFvbServicesRuntimeDxe would have to remember / retain the original (physical) allocation address too.)

If QemuFlashFvbServicesRuntimeDxe is not permitted to change MSR_SEV_ES_GHCB even temporarily (at OS runtime), then I think the approach proposed in this (guest kernel) patch is valid.

Let me skim the code below...

Quoted 48 lines
Quoted 48 lines
Quoted 48 lines


---
  arch/x86/boot/compressed/sev-es.c |  1 +
  arch/x86/include/asm/sev-es.h     |  2 ++
  arch/x86/kernel/sev-es.c          | 30 ++++++++++++++++++++++++++++++
  arch/x86/platform/efi/efi_64.c    | 10 ++++++++++
  4 files changed, 43 insertions(+)
arch/x86/boot/compressed/sev-es.c +1 -0
index 45702b866c33..0a9a248ca33d 100644
@@ -12,6 +12,7 @@
   */
  #include "misc.h"
 
+#include <asm/pgtable_types.h>
  #include <asm/sev-es.h>
  #include <asm/trapnr.h>
  #include <asm/trap_pf.h>
arch/x86/include/asm/sev-es.h +2 -0
index e919f09ae33c..cf1d957c7091 100644
@@ -102,11 +102,13 @@ static __always_inline void sev_es_nmi_complete(void)
       if (static_branch_unlikely(&sev_es_enable_key))
               __sev_es_nmi_complete();
  }
+extern int __init sev_es_efi_map_ghcbs(pgd_t *pgd);
  #else
  static inline void sev_es_ist_enter(struct pt_regs *regs) { }
  static inline void sev_es_ist_exit(void) { }
  static inline int sev_es_setup_ap_jump_table(struct real_mode_header *rmh) { return 0; }
  static inline void sev_es_nmi_complete(void) { }
+static inline int sev_es_efi_map_ghcbs(pgd_t *pgd) { return 0; }
  #endif
 
  #endif
arch/x86/kernel/sev-es.c +0 -0
index 9ab3a4dfecd8..4e2b7e4d9b87 100644
@@ -491,6 +491,36 @@ int sev_es_setup_ap_jump_table(struct real_mode_header *rmh)
       return 0;
  }
 
Quoted 4 lines
Quoted 4 lines

arch/x86/kernel/sev-es.c int sev_es_setup_ap_jump_table(struct real_mode_header *rmh) · line 497

Quoted 4 lines
arch/x86/kernel/sev-es.c +4 -0
+/*
+ * This is needed by the OVMF UEFI firmware which will use whatever it finds in
+ * the GHCB MSR as its GHCB to talk to the hypervisor. So make sure the per-cpu
+ * runtime GHCBs used by the kernel are also mapped in the EFI page-table.

Yup, this pretty much confirms my suspicion that QemuFlashPtrWrite() is at the center of this.

(BTW, I don't think that the runtime services data allocation, in QemuFlashFvbServicesRuntimeDxe, for OS runtime GHCB purposes, would have to be "per CPU". Refer to "Table 35. Rules for Reentry Into Runtime Services" in the UEFI spec -- if one processor is executing SetVariable(), then no other processor must enter SetVariable(). And so we'll have *at most* one VCPU in QemuFlashPtrWrite(), at any time.)

Quoted 45 lines
Quoted 45 lines
Quoted 45 lines
arch/x86/kernel/sev-es.c +26 -0
+ */
+int __init sev_es_efi_map_ghcbs(pgd_t *pgd)
+{
+     struct sev_es_runtime_data *data;
+     unsigned long address, pflags;
+     int cpu;
+     u64 pfn;
+
+     if (!sev_es_active())
+             return 0;
+
+     pflags = _PAGE_NX | _PAGE_RW;
+
+     for_each_possible_cpu(cpu) {
+             data = per_cpu(runtime_data, cpu);
+
+             address = __pa(&data->ghcb_page);
+             pfn = address >> PAGE_SHIFT;
+
+             if (kernel_map_pages_in_pgd(pgd, pfn, address, 1, pflags))
+                     return 1;
+     }
+
+     return 0;
+}
+
  static enum es_result vc_handle_msr(struct ghcb *ghcb, struct es_em_ctxt *ctxt)
  {
       struct pt_regs *regs = ctxt->regs;
arch/x86/platform/efi/efi_64.c +1 -0
index 6af4da1149ba..8f5759df7776 100644
@@ -47,6 +47,7 @@
  #include <asm/realmode.h>
  #include <asm/time.h>
  #include <asm/pgalloc.h>
+#include <asm/sev-es.h>
 
  /*
   * We allocate runtime services regions top-down, starting from -4G, i.e.
@@ -229,6 +230,15 @@ int __init efi_setup_page_tables(unsigned long pa_memmap, unsigned num_pages)
               return 1;
       }
 
Quoted 12 lines
Quoted 4 lines
Quoted 4 lines
arch/x86/platform/efi/efi_64.c +4 -0
+     /*
+      * When SEV-ES is active, the GHCB as set by the kernel will be used
+      * by firmware. Create a 1:1 unencrypted mapping for each GHCB.
+      */
Quoted 8 lines

arch/x86/platform/efi/efi_64.c int __init efi_setup_page_tables(unsigned long pa_memmap, unsigned num_pages) · line 244

Quoted 8 lines
arch/x86/platform/efi/efi_64.c +5 -0
+     if (sev_es_efi_map_ghcbs(pgd)) {
+             pr_err("Failed to create 1:1 mapping for the GHCBs!\n");
+             return 1;
+     }
+
       /*
        * When making calls to the firmware everything needs to be 1:1
        * mapped and addressable with 32-bit pointers. Map the kernel

Good point!

And it even makes me wonder if the QemuFlashFvbServicesRuntimeDxe approach, with the runtime services data type memory allocation, is feasible at all. Namely, a page's encryption status, under SEV, is controlled through the PTE.

And for this particular UEFI runtime area, it would *not* suffice for the OS to just virt-map it. The OS would also have to *decrypt* the area (mark the PTE as "plaintext").

In other words, it would be an "unprecedented" PTE for the OS to set up:
the PTE would not only map the GVA to GPA, but also mark the area as "plaintext".

Otherwise -- if the OS covers *just* the virt-mapping --, QemuFlashFvbServicesRuntimeDxe would populate its own "runtime GHCB" area just fine, but the actual data hitting the host RAM would be encrypted. And so the hypervisor could not interpret the GHCB.

*If* QemuFlashFvbServicesRuntimeDxe should not change the kernel-owned PTE at runtime, even temporarily, for marking the GHCB as "plaintext", then the problem is indeed only solvable in the guest kernel, in my opinion.

There simply isn't an "architected annotation" for telling the kernel, "virt-map this runtime services data type memory range, *and* mark it as plaintext at the same time".

This would be necessary, as both actions affect the exact same PTE, and the firmware is not really allowed to touch the PTE at runtime. But we don't have such a hint.

To summarize: for QemuFlashFvbServicesRuntimeDxe to allocate UEFI Runtime Services Data type memory, for its own runtime GHCB, two permissions are necessary (together), at OS runtime:

- QemuFlashFvbServicesRuntimeDxe must be allowed to swap MSR_SEV_ES_GHCB temporarily (before executing VMGEXIT),

Quoted 7 lines
Quoted 4 lines

- QemuFlashFvbServicesRuntimeDxe must be allowed to change the OS-owned PTE temporarily (for remapping the GHCB as plaintext, before writing to it).

Amazing summarization Laszlo!

Agreed. And thanks a lot for taking the time to do the analysis.

Based on the above,

Thanks,

merge bot Applied
#171 lore raw

[tip: x86/seves] x86/efi: Add GHCB mappings when SEV-ES is active

In reply to Joerg Roedel
The following commit has been merged into the x86/seves branch of tip:

The following commit has been merged into the x86/seves branch of tip:

Commit-ID:     35dcb1ebaf43230637abd2428b9d1fe6c915a78b
Gitweb:        https://git.kernel.org/tip/35dcb1ebaf43230637abd2428b9d1fe6c915a78b
Author:        Tom Lendacky <[email protected]>
AuthorDate:    Mon, 07 Sep 2020 15:16:12 +02:00
Committer:     Borislav Petkov <[email protected]>
CommitterDate: Wed, 09 Sep 2020 18:03:48 +02:00

x86/efi: Add GHCB mappings when SEV-ES is active

Calling down to EFI runtime services can result in the firmware performing VMGEXIT calls. The firmware is likely to use the GHCB of the OS (e.g., for setting EFI variables), so each GHCB in the system needs to be identity-mapped in the EFI page tables, as unencrypted, to avoid page faults.

---
 arch/x86/boot/compressed/sev-es.c |  1 +-
 arch/x86/include/asm/sev-es.h     |  2 ++-
 arch/x86/kernel/sev-es.c          | 30 ++++++++++++++++++++++++++++++-
 arch/x86/platform/efi/efi_64.c    | 10 ++++++++++-
 4 files changed, 43 insertions(+)
arch/x86/boot/compressed/sev-es.c +1 -0
index 5f15e58..2a6c7c3 100644
@@ -12,6 +12,7 @@
  */
 #include "misc.h"
 
+#include <asm/pgtable_types.h>
 #include <asm/sev-es.h>
 #include <asm/trapnr.h>
 #include <asm/trap_pf.h>
arch/x86/include/asm/sev-es.h +2 -0
index e919f09..cf1d957 100644
@@ -102,11 +102,13 @@ static __always_inline void sev_es_nmi_complete(void)
 	if (static_branch_unlikely(&sev_es_enable_key))
 		__sev_es_nmi_complete();
 }
+extern int __init sev_es_efi_map_ghcbs(pgd_t *pgd);
 #else
 static inline void sev_es_ist_enter(struct pt_regs *regs) { }
 static inline void sev_es_ist_exit(void) { }
 static inline int sev_es_setup_ap_jump_table(struct real_mode_header *rmh) { return 0; }
 static inline void sev_es_nmi_complete(void) { }
+static inline int sev_es_efi_map_ghcbs(pgd_t *pgd) { return 0; }
 #endif
 
 #endif
arch/x86/kernel/sev-es.c +30 -0
index b6518e9..8cac9f8 100644
@@ -491,6 +491,36 @@ int sev_es_setup_ap_jump_table(struct real_mode_header *rmh)
 	return 0;
 }
 
+/*
+ * This is needed by the OVMF UEFI firmware which will use whatever it finds in
+ * the GHCB MSR as its GHCB to talk to the hypervisor. So make sure the per-cpu
+ * runtime GHCBs used by the kernel are also mapped in the EFI page-table.
+ */
+int __init sev_es_efi_map_ghcbs(pgd_t *pgd)
+{
+	struct sev_es_runtime_data *data;
+	unsigned long address, pflags;
+	int cpu;
+	u64 pfn;
+
+	if (!sev_es_active())
+		return 0;
+
+	pflags = _PAGE_NX | _PAGE_RW;
+
+	for_each_possible_cpu(cpu) {
+		data = per_cpu(runtime_data, cpu);
+
+		address = __pa(&data->ghcb_page);
+		pfn = address >> PAGE_SHIFT;
+
+		if (kernel_map_pages_in_pgd(pgd, pfn, address, 1, pflags))
+			return 1;
+	}
+
+	return 0;
+}
+
 static enum es_result vc_handle_msr(struct ghcb *ghcb, struct es_em_ctxt *ctxt)
 {
 	struct pt_regs *regs = ctxt->regs;
arch/x86/platform/efi/efi_64.c +10 -0
index 6af4da1..8f5759d 100644
@@ -47,6 +47,7 @@
 #include <asm/realmode.h>
 #include <asm/time.h>
 #include <asm/pgalloc.h>
+#include <asm/sev-es.h>
 
 /*
  * We allocate runtime services regions top-down, starting from -4G, i.e.
@@ -230,6 +231,15 @@ int __init efi_setup_page_tables(unsigned long pa_memmap, unsigned num_pages)
 	}
 
 	/*
+	 * When SEV-ES is active, the GHCB as set by the kernel will be used
+	 * by firmware. Create a 1:1 unencrypted mapping for each GHCB.
+	 */
+	if (sev_es_efi_map_ghcbs(pgd)) {
+		pr_err("Failed to create 1:1 mapping for the GHCBs!\n");
+		return 1;
+	}
+
+	/*
 	 * When making calls to the firmware everything needs to be 1:1
 	 * mapped and addressable with 32-bit pointers. Map the kernel
 	 * text and allocate a new stack because we can't rely on the
merge bot Applied
#172 lore raw

[tip: x86/seves] x86/efi: Add GHCB mappings when SEV-ES is active

In reply to Joerg Roedel
The following commit has been merged into the x86/seves branch of tip:

The following commit has been merged into the x86/seves branch of tip:

Commit-ID:     39336f4ffb2478ad384075cf4ba7ef2e5db2bbd7
Gitweb:        https://git.kernel.org/tip/39336f4ffb2478ad384075cf4ba7ef2e5db2bbd7
Author:        Tom Lendacky <[email protected]>
AuthorDate:    Mon, 07 Sep 2020 15:16:12 +02:00
Committer:     Borislav Petkov <[email protected]>
CommitterDate: Thu, 10 Sep 2020 21:48:50 +02:00

x86/efi: Add GHCB mappings when SEV-ES is active

Calling down to EFI runtime services can result in the firmware performing VMGEXIT calls. The firmware is likely to use the GHCB of the OS (e.g., for setting EFI variables), so each GHCB in the system needs to be identity-mapped in the EFI page tables, as unencrypted, to avoid page faults.

---
 arch/x86/boot/compressed/sev-es.c |  1 +-
 arch/x86/include/asm/sev-es.h     |  2 ++-
 arch/x86/kernel/sev-es.c          | 30 ++++++++++++++++++++++++++++++-
 arch/x86/platform/efi/efi_64.c    | 10 ++++++++++-
 4 files changed, 43 insertions(+)
arch/x86/boot/compressed/sev-es.c +1 -0
index 5f15e58..2a6c7c3 100644
@@ -12,6 +12,7 @@
  */
 #include "misc.h"
 
+#include <asm/pgtable_types.h>
 #include <asm/sev-es.h>
 #include <asm/trapnr.h>
 #include <asm/trap_pf.h>
arch/x86/include/asm/sev-es.h +2 -0
index e919f09..cf1d957 100644
@@ -102,11 +102,13 @@ static __always_inline void sev_es_nmi_complete(void)
 	if (static_branch_unlikely(&sev_es_enable_key))
 		__sev_es_nmi_complete();
 }
+extern int __init sev_es_efi_map_ghcbs(pgd_t *pgd);
 #else
 static inline void sev_es_ist_enter(struct pt_regs *regs) { }
 static inline void sev_es_ist_exit(void) { }
 static inline int sev_es_setup_ap_jump_table(struct real_mode_header *rmh) { return 0; }
 static inline void sev_es_nmi_complete(void) { }
+static inline int sev_es_efi_map_ghcbs(pgd_t *pgd) { return 0; }
 #endif
 
 #endif
arch/x86/kernel/sev-es.c +30 -0
index b6518e9..8cac9f8 100644
@@ -491,6 +491,36 @@ int sev_es_setup_ap_jump_table(struct real_mode_header *rmh)
 	return 0;
 }
 
+/*
+ * This is needed by the OVMF UEFI firmware which will use whatever it finds in
+ * the GHCB MSR as its GHCB to talk to the hypervisor. So make sure the per-cpu
+ * runtime GHCBs used by the kernel are also mapped in the EFI page-table.
+ */
+int __init sev_es_efi_map_ghcbs(pgd_t *pgd)
+{
+	struct sev_es_runtime_data *data;
+	unsigned long address, pflags;
+	int cpu;
+	u64 pfn;
+
+	if (!sev_es_active())
+		return 0;
+
+	pflags = _PAGE_NX | _PAGE_RW;
+
+	for_each_possible_cpu(cpu) {
+		data = per_cpu(runtime_data, cpu);
+
+		address = __pa(&data->ghcb_page);
+		pfn = address >> PAGE_SHIFT;
+
+		if (kernel_map_pages_in_pgd(pgd, pfn, address, 1, pflags))
+			return 1;
+	}
+
+	return 0;
+}
+
 static enum es_result vc_handle_msr(struct ghcb *ghcb, struct es_em_ctxt *ctxt)
 {
 	struct pt_regs *regs = ctxt->regs;
arch/x86/platform/efi/efi_64.c +10 -0
index 6af4da1..8f5759d 100644
@@ -47,6 +47,7 @@
 #include <asm/realmode.h>
 #include <asm/time.h>
 #include <asm/pgalloc.h>
+#include <asm/sev-es.h>
 
 /*
  * We allocate runtime services regions top-down, starting from -4G, i.e.
@@ -230,6 +231,15 @@ int __init efi_setup_page_tables(unsigned long pa_memmap, unsigned num_pages)
 	}
 
 	/*
+	 * When SEV-ES is active, the GHCB as set by the kernel will be used
+	 * by firmware. Create a 1:1 unencrypted mapping for each GHCB.
+	 */
+	if (sev_es_efi_map_ghcbs(pgd)) {
+		pr_err("Failed to create 1:1 mapping for the GHCBs!\n");
+		return 1;
+	}
+
+	/*
 	 * When making calls to the firmware everything needs to be 1:1
 	 * mapped and addressable with 32-bit pointers. Map the kernel
 	 * text and allocate a new stack because we can't rely on the
#173 lore raw

PATCH v7 72/72 x86/sev-es: Check required CPU features for SEV-ES

In reply to Joerg Roedel

From: Martin Radev <[email protected]>

Make sure the machine supports RDRAND, otherwise there is no trusted source of of randomness in the system.

To also check this in the pre-decompression stage, make has_cpuflag not depend on CONFIG_RANDOMIZE_BASE anymore.

---
 arch/x86/boot/compressed/cpuflags.c |  4 ----
 arch/x86/boot/compressed/misc.h     |  5 +++--
 arch/x86/boot/compressed/sev-es.c   |  3 +++
 arch/x86/kernel/sev-es-shared.c     | 15 +++++++++++++++
 arch/x86/kernel/sev-es.c            |  3 +++
 5 files changed, 24 insertions(+), 6 deletions(-)
arch/x86/boot/compressed/cpuflags.c +0 -4
index 6448a8196d32..0cc1323896d1 100644
@@ -1,6 +1,4 @@
 // SPDX-License-Identifier: GPL-2.0
-#ifdef CONFIG_RANDOMIZE_BASE
-
 #include "../cpuflags.c"
 
 bool has_cpuflag(int flag)
@@ -9,5 +7,3 @@ bool has_cpuflag(int flag)
 
 	return test_bit(flag, cpu.flags);
 }
-
-#endif
arch/x86/boot/compressed/misc.h +3 -2
index c0e0ffeee50a..6d31f1b4c4d1 100644
@@ -85,8 +85,6 @@ void choose_random_location(unsigned long input,
 			    unsigned long *output,
 			    unsigned long output_size,
 			    unsigned long *virt_addr);
-/* cpuflags.c */
-bool has_cpuflag(int flag);
 #else
 static inline void choose_random_location(unsigned long input,
 					  unsigned long input_size,
@@ -97,6 +95,9 @@ static inline void choose_random_location(unsigned long input,
 }
 #endif
 
+/* cpuflags.c */
+bool has_cpuflag(int flag);
+
 #ifdef CONFIG_X86_64
 extern int set_page_decrypted(unsigned long address);
 extern int set_page_encrypted(unsigned long address);
arch/x86/boot/compressed/sev-es.c +3 -0
index 0a9a248ca33d..3c66dad4e62e 100644
@@ -145,6 +145,9 @@ void sev_es_shutdown_ghcb(void)
 	if (!boot_ghcb)
 		return;
 
+	if (!sev_es_check_cpu_features())
+		error("SEV-ES CPU Features missing.");
+
 	/*
 	 * GHCB Page must be flushed from the cache and mapped encrypted again.
 	 * Otherwise the running kernel will see strange cache effects when
arch/x86/kernel/sev-es-shared.c +15 -0
index 92d77b725ccb..ce86d2c9ca7b 100644
@@ -9,6 +9,21 @@
  * and is included directly into both code-bases.
  */
 
+#ifndef __BOOT_COMPRESSED
+#define error(v)	pr_err(v)
+#define has_cpuflag(f)	boot_cpu_has(f)
+#endif
+
+static bool __init sev_es_check_cpu_features(void)
+{
+	if (!has_cpuflag(X86_FEATURE_RDRAND)) {
+		error("RDRAND instruction not supported - no trusted source of randomness available\n");
+		return false;
+	}
+
+	return true;
+}
+
 static void sev_es_terminate(unsigned int reason)
 {
 	u64 val = GHCB_SEV_TERMINATE;
arch/x86/kernel/sev-es.c +3 -0
index 4e2b7e4d9b87..70623bbce062 100644
@@ -665,6 +665,9 @@ void __init sev_es_init_vc_handling(void)
 	if (!sev_es_active())
 		return;
 
+	if (!sev_es_check_cpu_features())
+		panic("SEV-ES CPU Features missing");
+
 	/* Enable SEV-ES special handling */
 	static_branch_enable(&sev_es_enable_key);
 
Signature · 6 lines
2.28.0

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
merge bot Applied
#174 lore raw

[tip: x86/seves] x86/sev-es: Check required CPU features for SEV-ES

The following commit has been merged into the x86/seves branch of tip:

The following commit has been merged into the x86/seves branch of tip:

Commit-ID:     1ac0dc494a9796d6f94ff392f16e0c9a8cea7021
Gitweb:        https://git.kernel.org/tip/1ac0dc494a9796d6f94ff392f16e0c9a8cea7021
Author:        Martin Radev <[email protected]>
AuthorDate:    Mon, 07 Sep 2020 15:16:13 +02:00
Committer:     Borislav Petkov <[email protected]>
CommitterDate: Wed, 09 Sep 2020 18:03:48 +02:00

x86/sev-es: Check required CPU features for SEV-ES

Make sure the machine supports RDRAND, otherwise there is no trusted source of randomness in the system.

To also check this in the pre-decompression stage, make has_cpuflag() not depend on CONFIG_RANDOMIZE_BASE anymore.

---
 arch/x86/boot/compressed/cpuflags.c |  4 ----
 arch/x86/boot/compressed/misc.h     |  5 +++--
 arch/x86/boot/compressed/sev-es.c   |  3 +++
 arch/x86/kernel/sev-es-shared.c     | 15 +++++++++++++++
 arch/x86/kernel/sev-es.c            |  3 +++
 5 files changed, 24 insertions(+), 6 deletions(-)
arch/x86/boot/compressed/cpuflags.c +0 -4
index 6448a81..0cc1323 100644
@@ -1,6 +1,4 @@
 // SPDX-License-Identifier: GPL-2.0
-#ifdef CONFIG_RANDOMIZE_BASE
-
 #include "../cpuflags.c"
 
 bool has_cpuflag(int flag)
@@ -9,5 +7,3 @@ bool has_cpuflag(int flag)
 
 	return test_bit(flag, cpu.flags);
 }
-
-#endif
arch/x86/boot/compressed/misc.h +3 -2
index c0e0ffe..6d31f1b 100644
@@ -85,8 +85,6 @@ void choose_random_location(unsigned long input,
 			    unsigned long *output,
 			    unsigned long output_size,
 			    unsigned long *virt_addr);
-/* cpuflags.c */
-bool has_cpuflag(int flag);
 #else
 static inline void choose_random_location(unsigned long input,
 					  unsigned long input_size,
@@ -97,6 +95,9 @@ static inline void choose_random_location(unsigned long input,
 }
 #endif
 
+/* cpuflags.c */
+bool has_cpuflag(int flag);
+
 #ifdef CONFIG_X86_64
 extern int set_page_decrypted(unsigned long address);
 extern int set_page_encrypted(unsigned long address);
arch/x86/boot/compressed/sev-es.c +3 -0
index 2a6c7c3..954cb27 100644
@@ -145,6 +145,9 @@ void sev_es_shutdown_ghcb(void)
 	if (!boot_ghcb)
 		return;
 
+	if (!sev_es_check_cpu_features())
+		error("SEV-ES CPU Features missing.");
+
 	/*
 	 * GHCB Page must be flushed from the cache and mapped encrypted again.
 	 * Otherwise the running kernel will see strange cache effects when
arch/x86/kernel/sev-es-shared.c +15 -0
index 4be8af2..5f83cca 100644
@@ -9,6 +9,21 @@
  * and is included directly into both code-bases.
  */
 
+#ifndef __BOOT_COMPRESSED
+#define error(v)	pr_err(v)
+#define has_cpuflag(f)	boot_cpu_has(f)
+#endif
+
+static bool __init sev_es_check_cpu_features(void)
+{
+	if (!has_cpuflag(X86_FEATURE_RDRAND)) {
+		error("RDRAND instruction not supported - no trusted source of randomness available\n");
+		return false;
+	}
+
+	return true;
+}
+
 static void sev_es_terminate(unsigned int reason)
 {
 	u64 val = GHCB_SEV_TERMINATE;
arch/x86/kernel/sev-es.c +3 -0
index 8cac9f8..6fcfdd3 100644
@@ -665,6 +665,9 @@ void __init sev_es_init_vc_handling(void)
 	if (!sev_es_active())
 		return;
 
+	if (!sev_es_check_cpu_features())
+		panic("SEV-ES CPU Features missing");
+
 	/* Enable SEV-ES special handling */
 	static_branch_enable(&sev_es_enable_key);
 
merge bot Applied
#175 lore raw

[tip: x86/seves] x86/sev-es: Check required CPU features for SEV-ES

In reply to Joerg Roedel
The following commit has been merged into the x86/seves branch of tip:

The following commit has been merged into the x86/seves branch of tip:

Commit-ID:     f5ed777586e08e09c4b6f1e87161a145ee1431cf
Gitweb:        https://git.kernel.org/tip/f5ed777586e08e09c4b6f1e87161a145ee1431cf
Author:        Martin Radev <[email protected]>
AuthorDate:    Mon, 07 Sep 2020 15:16:13 +02:00
Committer:     Borislav Petkov <[email protected]>
CommitterDate: Thu, 10 Sep 2020 21:49:25 +02:00

x86/sev-es: Check required CPU features for SEV-ES

Make sure the machine supports RDRAND, otherwise there is no trusted source of randomness in the system.

To also check this in the pre-decompression stage, make has_cpuflag() not depend on CONFIG_RANDOMIZE_BASE anymore.

---
 arch/x86/boot/compressed/cpuflags.c |  4 ----
 arch/x86/boot/compressed/misc.h     |  5 +++--
 arch/x86/boot/compressed/sev-es.c   |  3 +++
 arch/x86/kernel/sev-es-shared.c     | 15 +++++++++++++++
 arch/x86/kernel/sev-es.c            |  3 +++
 5 files changed, 24 insertions(+), 6 deletions(-)
arch/x86/boot/compressed/cpuflags.c +0 -4
index 6448a81..0cc1323 100644
@@ -1,6 +1,4 @@
 // SPDX-License-Identifier: GPL-2.0
-#ifdef CONFIG_RANDOMIZE_BASE
-
 #include "../cpuflags.c"
 
 bool has_cpuflag(int flag)
@@ -9,5 +7,3 @@ bool has_cpuflag(int flag)
 
 	return test_bit(flag, cpu.flags);
 }
-
-#endif
arch/x86/boot/compressed/misc.h +3 -2
index c0e0ffe..6d31f1b 100644
@@ -85,8 +85,6 @@ void choose_random_location(unsigned long input,
 			    unsigned long *output,
 			    unsigned long output_size,
 			    unsigned long *virt_addr);
-/* cpuflags.c */
-bool has_cpuflag(int flag);
 #else
 static inline void choose_random_location(unsigned long input,
 					  unsigned long input_size,
@@ -97,6 +95,9 @@ static inline void choose_random_location(unsigned long input,
 }
 #endif
 
+/* cpuflags.c */
+bool has_cpuflag(int flag);
+
 #ifdef CONFIG_X86_64
 extern int set_page_decrypted(unsigned long address);
 extern int set_page_encrypted(unsigned long address);
arch/x86/boot/compressed/sev-es.c +3 -0
index 2a6c7c3..954cb27 100644
@@ -145,6 +145,9 @@ void sev_es_shutdown_ghcb(void)
 	if (!boot_ghcb)
 		return;
 
+	if (!sev_es_check_cpu_features())
+		error("SEV-ES CPU Features missing.");
+
 	/*
 	 * GHCB Page must be flushed from the cache and mapped encrypted again.
 	 * Otherwise the running kernel will see strange cache effects when
arch/x86/kernel/sev-es-shared.c +15 -0
index 4be8af2..5f83cca 100644
@@ -9,6 +9,21 @@
  * and is included directly into both code-bases.
  */
 
+#ifndef __BOOT_COMPRESSED
+#define error(v)	pr_err(v)
+#define has_cpuflag(f)	boot_cpu_has(f)
+#endif
+
+static bool __init sev_es_check_cpu_features(void)
+{
+	if (!has_cpuflag(X86_FEATURE_RDRAND)) {
+		error("RDRAND instruction not supported - no trusted source of randomness available\n");
+		return false;
+	}
+
+	return true;
+}
+
 static void sev_es_terminate(unsigned int reason)
 {
 	u64 val = GHCB_SEV_TERMINATE;
arch/x86/kernel/sev-es.c +3 -0
index 8cac9f8..6fcfdd3 100644
@@ -665,6 +665,9 @@ void __init sev_es_init_vc_handling(void)
 	if (!sev_es_active())
 		return;
 
+	if (!sev_es_check_cpu_features())
+		panic("SEV-ES CPU Features missing");
+
 	/* Enable SEV-ES special handling */
 	static_branch_enable(&sev_es_enable_key);