Skip to content

Commit 73bbf3b

Browse files
nikunjadbp3tk0v
authored andcommitted
x86/tsc: Init the TSC for Secure TSC guests
Use the GUEST_TSC_FREQ MSR to discover the TSC frequency instead of relying on kvm-clock based frequency calibration. Override both CPU and TSC frequency calibration callbacks with securetsc_get_tsc_khz(). Since the difference between CPU base and TSC frequency does not apply in this case, the same callback is being used. [ bp: Carve out from https://lore.kernel.org/r/[email protected] ] Signed-off-by: Nikunj A Dadhania <[email protected]> Signed-off-by: Borislav Petkov (AMD) <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 0a2a98f commit 73bbf3b

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

arch/x86/coco/sev/core.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ static u64 secrets_pa __ro_after_init;
103103
*/
104104
static u64 snp_tsc_scale __ro_after_init;
105105
static u64 snp_tsc_offset __ro_after_init;
106+
static u64 snp_tsc_freq_khz __ro_after_init;
106107

107108
/* #VC handler runtime per-CPU data */
108109
struct sev_es_runtime_data {
@@ -3278,3 +3279,23 @@ void __init snp_secure_tsc_prepare(void)
32783279

32793280
pr_debug("SecureTSC enabled");
32803281
}
3282+
3283+
static unsigned long securetsc_get_tsc_khz(void)
3284+
{
3285+
return snp_tsc_freq_khz;
3286+
}
3287+
3288+
void __init snp_secure_tsc_init(void)
3289+
{
3290+
unsigned long long tsc_freq_mhz;
3291+
3292+
if (!cc_platform_has(CC_ATTR_GUEST_SNP_SECURE_TSC))
3293+
return;
3294+
3295+
setup_force_cpu_cap(X86_FEATURE_TSC_KNOWN_FREQ);
3296+
rdmsrl(MSR_AMD64_GUEST_TSC_FREQ, tsc_freq_mhz);
3297+
snp_tsc_freq_khz = (unsigned long)(tsc_freq_mhz * 1000);
3298+
3299+
x86_platform.calibrate_cpu = securetsc_get_tsc_khz;
3300+
x86_platform.calibrate_tsc = securetsc_get_tsc_khz;
3301+
}

arch/x86/include/asm/sev.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,7 @@ int snp_send_guest_request(struct snp_msg_desc *mdesc, struct snp_guest_req *req
482482
struct snp_guest_request_ioctl *rio);
483483

484484
void __init snp_secure_tsc_prepare(void);
485+
void __init snp_secure_tsc_init(void);
485486

486487
#else /* !CONFIG_AMD_MEM_ENCRYPT */
487488

@@ -524,6 +525,7 @@ static inline void snp_msg_free(struct snp_msg_desc *mdesc) { }
524525
static inline int snp_send_guest_request(struct snp_msg_desc *mdesc, struct snp_guest_req *req,
525526
struct snp_guest_request_ioctl *rio) { return -ENODEV; }
526527
static inline void __init snp_secure_tsc_prepare(void) { }
528+
static inline void __init snp_secure_tsc_init(void) { }
527529

528530
#endif /* CONFIG_AMD_MEM_ENCRYPT */
529531

arch/x86/kernel/tsc.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include <asm/i8259.h>
3131
#include <asm/topology.h>
3232
#include <asm/uv/uv.h>
33+
#include <asm/sev.h>
3334

3435
unsigned int __read_mostly cpu_khz; /* TSC clocks / usec, not used here */
3536
EXPORT_SYMBOL(cpu_khz);
@@ -1515,6 +1516,9 @@ void __init tsc_early_init(void)
15151516
/* Don't change UV TSC multi-chassis synchronization */
15161517
if (is_early_uv_system())
15171518
return;
1519+
1520+
snp_secure_tsc_init();
1521+
15181522
if (!determine_cpu_tsc_frequencies(true))
15191523
return;
15201524
tsc_enable_sched_clock();

0 commit comments

Comments
 (0)