Skip to content

Commit 0f0502b

Browse files
nikunjadbp3tk0v
authored andcommitted
x86/sev: Change TSC MSR behavior for Secure TSC enabled guests
Secure TSC enabled guests should not write to the MSR_IA32_TSC (0x10) register as the subsequent TSC value reads are undefined. On AMD, MSR_IA32_TSC is intercepted by the hypervisor by default. MSR_IA32_TSC read/write accesses should not exit to the hypervisor for such guests. Accesses to MSR_IA32_TSC need special handling in the #VC handler for the guests with Secure TSC enabled. Writes to MSR_IA32_TSC should be ignored and flagged once with a warning, and reads of MSR_IA32_TSC should return the result of the RDTSC instruction. [ bp: Massage commit message. ] Suggested-by: Borislav Petkov (AMD) <[email protected]> Signed-off-by: Nikunj A Dadhania <[email protected]> Signed-off-by: Borislav Petkov (AMD) <[email protected]> Reviewed-by: Tom Lendacky <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 85b60ca commit 0f0502b

File tree

1 file changed

+38
-1
lines changed

1 file changed

+38
-1
lines changed

arch/x86/coco/sev/core.c

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1433,6 +1433,34 @@ static enum es_result __vc_handle_msr_caa(struct pt_regs *regs, bool write)
14331433
return ES_OK;
14341434
}
14351435

1436+
/*
1437+
* TSC related accesses should not exit to the hypervisor when a guest is
1438+
* executing with Secure TSC enabled, so special handling is required for
1439+
* accesses of MSR_IA32_TSC.
1440+
*/
1441+
static enum es_result __vc_handle_secure_tsc_msrs(struct pt_regs *regs, bool write)
1442+
{
1443+
u64 tsc;
1444+
1445+
/*
1446+
* Writes: Writing to MSR_IA32_TSC can cause subsequent reads of the TSC
1447+
* to return undefined values, so ignore all writes.
1448+
*
1449+
* Reads: Reads of MSR_IA32_TSC should return the current TSC value, use
1450+
* the value returned by rdtsc_ordered().
1451+
*/
1452+
if (write) {
1453+
WARN_ONCE(1, "TSC MSR writes are verboten!\n");
1454+
return ES_OK;
1455+
}
1456+
1457+
tsc = rdtsc_ordered();
1458+
regs->ax = lower_32_bits(tsc);
1459+
regs->dx = upper_32_bits(tsc);
1460+
1461+
return ES_OK;
1462+
}
1463+
14361464
static enum es_result vc_handle_msr(struct ghcb *ghcb, struct es_em_ctxt *ctxt)
14371465
{
14381466
struct pt_regs *regs = ctxt->regs;
@@ -1442,8 +1470,17 @@ static enum es_result vc_handle_msr(struct ghcb *ghcb, struct es_em_ctxt *ctxt)
14421470
/* Is it a WRMSR? */
14431471
write = ctxt->insn.opcode.bytes[1] == 0x30;
14441472

1445-
if (regs->cx == MSR_SVSM_CAA)
1473+
switch (regs->cx) {
1474+
case MSR_SVSM_CAA:
14461475
return __vc_handle_msr_caa(regs, write);
1476+
case MSR_IA32_TSC:
1477+
if (sev_status & MSR_AMD64_SNP_SECURE_TSC)
1478+
return __vc_handle_secure_tsc_msrs(regs, write);
1479+
else
1480+
break;
1481+
default:
1482+
break;
1483+
}
14471484

14481485
ghcb_set_rcx(ghcb, regs->cx);
14491486
if (write) {

0 commit comments

Comments
 (0)