Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
torvalds
GitHub Repository: torvalds/linux
Path: blob/master/arch/x86/kernel/cpu/vmware.c
26493 views
1
/*
2
* VMware Detection code.
3
*
4
* Copyright (C) 2008, VMware, Inc.
5
* Author : Alok N Kataria <[email protected]>
6
*
7
* This program is free software; you can redistribute it and/or modify
8
* it under the terms of the GNU General Public License as published by
9
* the Free Software Foundation; either version 2 of the License, or
10
* (at your option) any later version.
11
*
12
* This program is distributed in the hope that it will be useful, but
13
* WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
15
* NON INFRINGEMENT. See the GNU General Public License for more
16
* details.
17
*
18
* You should have received a copy of the GNU General Public License
19
* along with this program; if not, write to the Free Software
20
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
21
*
22
*/
23
24
#include <linux/dmi.h>
25
#include <linux/init.h>
26
#include <linux/export.h>
27
#include <linux/clocksource.h>
28
#include <linux/cpu.h>
29
#include <linux/efi.h>
30
#include <linux/reboot.h>
31
#include <linux/static_call.h>
32
#include <asm/div64.h>
33
#include <asm/x86_init.h>
34
#include <asm/hypervisor.h>
35
#include <asm/timer.h>
36
#include <asm/apic.h>
37
#include <asm/vmware.h>
38
#include <asm/svm.h>
39
40
#undef pr_fmt
41
#define pr_fmt(fmt) "vmware: " fmt
42
43
#define CPUID_VMWARE_INFO_LEAF 0x40000000
44
#define CPUID_VMWARE_FEATURES_LEAF 0x40000010
45
46
#define GETVCPU_INFO_LEGACY_X2APIC BIT(3)
47
#define GETVCPU_INFO_VCPU_RESERVED BIT(31)
48
49
#define STEALCLOCK_NOT_AVAILABLE (-1)
50
#define STEALCLOCK_DISABLED 0
51
#define STEALCLOCK_ENABLED 1
52
53
struct vmware_steal_time {
54
union {
55
u64 clock; /* stolen time counter in units of vtsc */
56
struct {
57
/* only for little-endian */
58
u32 clock_low;
59
u32 clock_high;
60
};
61
};
62
u64 reserved[7];
63
};
64
65
static unsigned long vmware_tsc_khz __ro_after_init;
66
static u8 vmware_hypercall_mode __ro_after_init;
67
68
unsigned long vmware_hypercall_slow(unsigned long cmd,
69
unsigned long in1, unsigned long in3,
70
unsigned long in4, unsigned long in5,
71
u32 *out1, u32 *out2, u32 *out3,
72
u32 *out4, u32 *out5)
73
{
74
unsigned long out0, rbx, rcx, rdx, rsi, rdi;
75
76
switch (vmware_hypercall_mode) {
77
case CPUID_VMWARE_FEATURES_ECX_VMCALL:
78
asm_inline volatile ("vmcall"
79
: "=a" (out0), "=b" (rbx), "=c" (rcx),
80
"=d" (rdx), "=S" (rsi), "=D" (rdi)
81
: "a" (VMWARE_HYPERVISOR_MAGIC),
82
"b" (in1),
83
"c" (cmd),
84
"d" (in3),
85
"S" (in4),
86
"D" (in5)
87
: "cc", "memory");
88
break;
89
case CPUID_VMWARE_FEATURES_ECX_VMMCALL:
90
asm_inline volatile ("vmmcall"
91
: "=a" (out0), "=b" (rbx), "=c" (rcx),
92
"=d" (rdx), "=S" (rsi), "=D" (rdi)
93
: "a" (VMWARE_HYPERVISOR_MAGIC),
94
"b" (in1),
95
"c" (cmd),
96
"d" (in3),
97
"S" (in4),
98
"D" (in5)
99
: "cc", "memory");
100
break;
101
default:
102
asm_inline volatile ("movw %[port], %%dx; inl (%%dx), %%eax"
103
: "=a" (out0), "=b" (rbx), "=c" (rcx),
104
"=d" (rdx), "=S" (rsi), "=D" (rdi)
105
: [port] "i" (VMWARE_HYPERVISOR_PORT),
106
"a" (VMWARE_HYPERVISOR_MAGIC),
107
"b" (in1),
108
"c" (cmd),
109
"d" (in3),
110
"S" (in4),
111
"D" (in5)
112
: "cc", "memory");
113
break;
114
}
115
116
if (out1)
117
*out1 = rbx;
118
if (out2)
119
*out2 = rcx;
120
if (out3)
121
*out3 = rdx;
122
if (out4)
123
*out4 = rsi;
124
if (out5)
125
*out5 = rdi;
126
127
return out0;
128
}
129
130
static inline int __vmware_platform(void)
131
{
132
u32 eax, ebx, ecx;
133
134
eax = vmware_hypercall3(VMWARE_CMD_GETVERSION, 0, &ebx, &ecx);
135
return eax != UINT_MAX && ebx == VMWARE_HYPERVISOR_MAGIC;
136
}
137
138
static unsigned long vmware_get_tsc_khz(void)
139
{
140
return vmware_tsc_khz;
141
}
142
143
#ifdef CONFIG_PARAVIRT
144
static struct cyc2ns_data vmware_cyc2ns __ro_after_init;
145
static bool vmw_sched_clock __initdata = true;
146
static DEFINE_PER_CPU_DECRYPTED(struct vmware_steal_time, vmw_steal_time) __aligned(64);
147
static bool has_steal_clock;
148
static bool steal_acc __initdata = true; /* steal time accounting */
149
150
static __init int setup_vmw_sched_clock(char *s)
151
{
152
vmw_sched_clock = false;
153
return 0;
154
}
155
early_param("no-vmw-sched-clock", setup_vmw_sched_clock);
156
157
static __init int parse_no_stealacc(char *arg)
158
{
159
steal_acc = false;
160
return 0;
161
}
162
early_param("no-steal-acc", parse_no_stealacc);
163
164
static noinstr u64 vmware_sched_clock(void)
165
{
166
unsigned long long ns;
167
168
ns = mul_u64_u32_shr(rdtsc(), vmware_cyc2ns.cyc2ns_mul,
169
vmware_cyc2ns.cyc2ns_shift);
170
ns -= vmware_cyc2ns.cyc2ns_offset;
171
return ns;
172
}
173
174
static void __init vmware_cyc2ns_setup(void)
175
{
176
struct cyc2ns_data *d = &vmware_cyc2ns;
177
unsigned long long tsc_now = rdtsc();
178
179
clocks_calc_mult_shift(&d->cyc2ns_mul, &d->cyc2ns_shift,
180
vmware_tsc_khz, NSEC_PER_MSEC, 0);
181
d->cyc2ns_offset = mul_u64_u32_shr(tsc_now, d->cyc2ns_mul,
182
d->cyc2ns_shift);
183
184
pr_info("using clock offset of %llu ns\n", d->cyc2ns_offset);
185
}
186
187
static int vmware_cmd_stealclock(u32 addr_hi, u32 addr_lo)
188
{
189
u32 info;
190
191
return vmware_hypercall5(VMWARE_CMD_STEALCLOCK, 0, 0, addr_hi, addr_lo,
192
&info);
193
}
194
195
static bool stealclock_enable(phys_addr_t pa)
196
{
197
return vmware_cmd_stealclock(upper_32_bits(pa),
198
lower_32_bits(pa)) == STEALCLOCK_ENABLED;
199
}
200
201
static int __stealclock_disable(void)
202
{
203
return vmware_cmd_stealclock(0, 1);
204
}
205
206
static void stealclock_disable(void)
207
{
208
__stealclock_disable();
209
}
210
211
static bool vmware_is_stealclock_available(void)
212
{
213
return __stealclock_disable() != STEALCLOCK_NOT_AVAILABLE;
214
}
215
216
/**
217
* vmware_steal_clock() - read the per-cpu steal clock
218
* @cpu: the cpu number whose steal clock we want to read
219
*
220
* The function reads the steal clock if we are on a 64-bit system, otherwise
221
* reads it in parts, checking that the high part didn't change in the
222
* meantime.
223
*
224
* Return:
225
* The steal clock reading in ns.
226
*/
227
static u64 vmware_steal_clock(int cpu)
228
{
229
struct vmware_steal_time *steal = &per_cpu(vmw_steal_time, cpu);
230
u64 clock;
231
232
if (IS_ENABLED(CONFIG_64BIT))
233
clock = READ_ONCE(steal->clock);
234
else {
235
u32 initial_high, low, high;
236
237
do {
238
initial_high = READ_ONCE(steal->clock_high);
239
/* Do not reorder initial_high and high readings */
240
virt_rmb();
241
low = READ_ONCE(steal->clock_low);
242
/* Keep low reading in between */
243
virt_rmb();
244
high = READ_ONCE(steal->clock_high);
245
} while (initial_high != high);
246
247
clock = ((u64)high << 32) | low;
248
}
249
250
return mul_u64_u32_shr(clock, vmware_cyc2ns.cyc2ns_mul,
251
vmware_cyc2ns.cyc2ns_shift);
252
}
253
254
static void vmware_register_steal_time(void)
255
{
256
int cpu = smp_processor_id();
257
struct vmware_steal_time *st = &per_cpu(vmw_steal_time, cpu);
258
259
if (!has_steal_clock)
260
return;
261
262
if (!stealclock_enable(slow_virt_to_phys(st))) {
263
has_steal_clock = false;
264
return;
265
}
266
267
pr_info("vmware-stealtime: cpu %d, pa %llx\n",
268
cpu, (unsigned long long) slow_virt_to_phys(st));
269
}
270
271
static void vmware_disable_steal_time(void)
272
{
273
if (!has_steal_clock)
274
return;
275
276
stealclock_disable();
277
}
278
279
static void vmware_guest_cpu_init(void)
280
{
281
if (has_steal_clock)
282
vmware_register_steal_time();
283
}
284
285
static void vmware_pv_guest_cpu_reboot(void *unused)
286
{
287
vmware_disable_steal_time();
288
}
289
290
static int vmware_pv_reboot_notify(struct notifier_block *nb,
291
unsigned long code, void *unused)
292
{
293
if (code == SYS_RESTART)
294
on_each_cpu(vmware_pv_guest_cpu_reboot, NULL, 1);
295
return NOTIFY_DONE;
296
}
297
298
static struct notifier_block vmware_pv_reboot_nb = {
299
.notifier_call = vmware_pv_reboot_notify,
300
};
301
302
#ifdef CONFIG_SMP
303
static void __init vmware_smp_prepare_boot_cpu(void)
304
{
305
vmware_guest_cpu_init();
306
native_smp_prepare_boot_cpu();
307
}
308
309
static int vmware_cpu_online(unsigned int cpu)
310
{
311
local_irq_disable();
312
vmware_guest_cpu_init();
313
local_irq_enable();
314
return 0;
315
}
316
317
static int vmware_cpu_down_prepare(unsigned int cpu)
318
{
319
local_irq_disable();
320
vmware_disable_steal_time();
321
local_irq_enable();
322
return 0;
323
}
324
#endif
325
326
static __init int activate_jump_labels(void)
327
{
328
if (has_steal_clock) {
329
static_key_slow_inc(&paravirt_steal_enabled);
330
if (steal_acc)
331
static_key_slow_inc(&paravirt_steal_rq_enabled);
332
}
333
334
return 0;
335
}
336
arch_initcall(activate_jump_labels);
337
338
static void __init vmware_paravirt_ops_setup(void)
339
{
340
pv_info.name = "VMware hypervisor";
341
pv_ops.cpu.io_delay = paravirt_nop;
342
343
if (vmware_tsc_khz == 0)
344
return;
345
346
vmware_cyc2ns_setup();
347
348
if (vmw_sched_clock)
349
paravirt_set_sched_clock(vmware_sched_clock);
350
351
if (vmware_is_stealclock_available()) {
352
has_steal_clock = true;
353
static_call_update(pv_steal_clock, vmware_steal_clock);
354
355
/* We use reboot notifier only to disable steal clock */
356
register_reboot_notifier(&vmware_pv_reboot_nb);
357
358
#ifdef CONFIG_SMP
359
smp_ops.smp_prepare_boot_cpu =
360
vmware_smp_prepare_boot_cpu;
361
if (cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN,
362
"x86/vmware:online",
363
vmware_cpu_online,
364
vmware_cpu_down_prepare) < 0)
365
pr_err("vmware_guest: Failed to install cpu hotplug callbacks\n");
366
#else
367
vmware_guest_cpu_init();
368
#endif
369
}
370
}
371
#else
372
#define vmware_paravirt_ops_setup() do {} while (0)
373
#endif
374
375
/*
376
* VMware hypervisor takes care of exporting a reliable TSC to the guest.
377
* Still, due to timing difference when running on virtual cpus, the TSC can
378
* be marked as unstable in some cases. For example, the TSC sync check at
379
* bootup can fail due to a marginal offset between vcpus' TSCs (though the
380
* TSCs do not drift from each other). Also, the ACPI PM timer clocksource
381
* is not suitable as a watchdog when running on a hypervisor because the
382
* kernel may miss a wrap of the counter if the vcpu is descheduled for a
383
* long time. To skip these checks at runtime we set these capability bits,
384
* so that the kernel could just trust the hypervisor with providing a
385
* reliable virtual TSC that is suitable for timekeeping.
386
*/
387
static void __init vmware_set_capabilities(void)
388
{
389
setup_force_cpu_cap(X86_FEATURE_CONSTANT_TSC);
390
setup_force_cpu_cap(X86_FEATURE_TSC_RELIABLE);
391
if (vmware_tsc_khz)
392
setup_force_cpu_cap(X86_FEATURE_TSC_KNOWN_FREQ);
393
if (vmware_hypercall_mode == CPUID_VMWARE_FEATURES_ECX_VMCALL)
394
setup_force_cpu_cap(X86_FEATURE_VMCALL);
395
else if (vmware_hypercall_mode == CPUID_VMWARE_FEATURES_ECX_VMMCALL)
396
setup_force_cpu_cap(X86_FEATURE_VMW_VMMCALL);
397
}
398
399
static void __init vmware_platform_setup(void)
400
{
401
u32 eax, ebx, ecx;
402
u64 lpj, tsc_khz;
403
404
eax = vmware_hypercall3(VMWARE_CMD_GETHZ, UINT_MAX, &ebx, &ecx);
405
406
if (ebx != UINT_MAX) {
407
lpj = tsc_khz = eax | (((u64)ebx) << 32);
408
do_div(tsc_khz, 1000);
409
WARN_ON(tsc_khz >> 32);
410
pr_info("TSC freq read from hypervisor : %lu.%03lu MHz\n",
411
(unsigned long) tsc_khz / 1000,
412
(unsigned long) tsc_khz % 1000);
413
414
if (!preset_lpj) {
415
do_div(lpj, HZ);
416
preset_lpj = lpj;
417
}
418
419
vmware_tsc_khz = tsc_khz;
420
x86_platform.calibrate_tsc = vmware_get_tsc_khz;
421
x86_platform.calibrate_cpu = vmware_get_tsc_khz;
422
423
#ifdef CONFIG_X86_LOCAL_APIC
424
/* Skip lapic calibration since we know the bus frequency. */
425
lapic_timer_period = ecx / HZ;
426
pr_info("Host bus clock speed read from hypervisor : %u Hz\n",
427
ecx);
428
#endif
429
} else {
430
pr_warn("Failed to get TSC freq from the hypervisor\n");
431
}
432
433
if (cc_platform_has(CC_ATTR_GUEST_SEV_SNP) && !efi_enabled(EFI_BOOT))
434
x86_init.mpparse.find_mptable = mpparse_find_mptable;
435
436
vmware_paravirt_ops_setup();
437
438
#ifdef CONFIG_X86_IO_APIC
439
no_timer_check = 1;
440
#endif
441
442
vmware_set_capabilities();
443
}
444
445
static u8 __init vmware_select_hypercall(void)
446
{
447
int eax, ebx, ecx, edx;
448
449
cpuid(CPUID_VMWARE_FEATURES_LEAF, &eax, &ebx, &ecx, &edx);
450
return (ecx & (CPUID_VMWARE_FEATURES_ECX_VMMCALL |
451
CPUID_VMWARE_FEATURES_ECX_VMCALL));
452
}
453
454
/*
455
* While checking the dmi string information, just checking the product
456
* serial key should be enough, as this will always have a VMware
457
* specific string when running under VMware hypervisor.
458
* If !boot_cpu_has(X86_FEATURE_HYPERVISOR), vmware_hypercall_mode
459
* intentionally defaults to 0.
460
*/
461
static u32 __init vmware_platform(void)
462
{
463
if (boot_cpu_has(X86_FEATURE_HYPERVISOR)) {
464
unsigned int eax;
465
unsigned int hyper_vendor_id[3];
466
467
cpuid(CPUID_VMWARE_INFO_LEAF, &eax, &hyper_vendor_id[0],
468
&hyper_vendor_id[1], &hyper_vendor_id[2]);
469
if (!memcmp(hyper_vendor_id, "VMwareVMware", 12)) {
470
if (eax >= CPUID_VMWARE_FEATURES_LEAF)
471
vmware_hypercall_mode =
472
vmware_select_hypercall();
473
474
pr_info("hypercall mode: 0x%02x\n",
475
(unsigned int) vmware_hypercall_mode);
476
477
return CPUID_VMWARE_INFO_LEAF;
478
}
479
} else if (dmi_available && dmi_name_in_serial("VMware") &&
480
__vmware_platform())
481
return 1;
482
483
return 0;
484
}
485
486
/* Checks if hypervisor supports x2apic without VT-D interrupt remapping. */
487
static bool __init vmware_legacy_x2apic_available(void)
488
{
489
u32 eax;
490
491
eax = vmware_hypercall1(VMWARE_CMD_GETVCPU_INFO, 0);
492
return !(eax & GETVCPU_INFO_VCPU_RESERVED) &&
493
(eax & GETVCPU_INFO_LEGACY_X2APIC);
494
}
495
496
#ifdef CONFIG_INTEL_TDX_GUEST
497
/*
498
* TDCALL[TDG.VP.VMCALL] uses %rax (arg0) and %rcx (arg2). Therefore,
499
* we remap those registers to %r12 and %r13, respectively.
500
*/
501
unsigned long vmware_tdx_hypercall(unsigned long cmd,
502
unsigned long in1, unsigned long in3,
503
unsigned long in4, unsigned long in5,
504
u32 *out1, u32 *out2, u32 *out3,
505
u32 *out4, u32 *out5)
506
{
507
struct tdx_module_args args = {};
508
509
if (!hypervisor_is_type(X86_HYPER_VMWARE)) {
510
pr_warn_once("Incorrect usage\n");
511
return ULONG_MAX;
512
}
513
514
if (cmd & ~VMWARE_CMD_MASK) {
515
pr_warn_once("Out of range command %lx\n", cmd);
516
return ULONG_MAX;
517
}
518
519
args.rbx = in1;
520
args.rdx = in3;
521
args.rsi = in4;
522
args.rdi = in5;
523
args.r10 = VMWARE_TDX_VENDOR_LEAF;
524
args.r11 = VMWARE_TDX_HCALL_FUNC;
525
args.r12 = VMWARE_HYPERVISOR_MAGIC;
526
args.r13 = cmd;
527
/* CPL */
528
args.r15 = 0;
529
530
__tdx_hypercall(&args);
531
532
if (out1)
533
*out1 = args.rbx;
534
if (out2)
535
*out2 = args.r13;
536
if (out3)
537
*out3 = args.rdx;
538
if (out4)
539
*out4 = args.rsi;
540
if (out5)
541
*out5 = args.rdi;
542
543
return args.r12;
544
}
545
EXPORT_SYMBOL_GPL(vmware_tdx_hypercall);
546
#endif
547
548
#ifdef CONFIG_AMD_MEM_ENCRYPT
549
static void vmware_sev_es_hcall_prepare(struct ghcb *ghcb,
550
struct pt_regs *regs)
551
{
552
/* Copy VMWARE specific Hypercall parameters to the GHCB */
553
ghcb_set_rip(ghcb, regs->ip);
554
ghcb_set_rbx(ghcb, regs->bx);
555
ghcb_set_rcx(ghcb, regs->cx);
556
ghcb_set_rdx(ghcb, regs->dx);
557
ghcb_set_rsi(ghcb, regs->si);
558
ghcb_set_rdi(ghcb, regs->di);
559
ghcb_set_rbp(ghcb, regs->bp);
560
}
561
562
static bool vmware_sev_es_hcall_finish(struct ghcb *ghcb, struct pt_regs *regs)
563
{
564
if (!(ghcb_rbx_is_valid(ghcb) &&
565
ghcb_rcx_is_valid(ghcb) &&
566
ghcb_rdx_is_valid(ghcb) &&
567
ghcb_rsi_is_valid(ghcb) &&
568
ghcb_rdi_is_valid(ghcb) &&
569
ghcb_rbp_is_valid(ghcb)))
570
return false;
571
572
regs->bx = ghcb_get_rbx(ghcb);
573
regs->cx = ghcb_get_rcx(ghcb);
574
regs->dx = ghcb_get_rdx(ghcb);
575
regs->si = ghcb_get_rsi(ghcb);
576
regs->di = ghcb_get_rdi(ghcb);
577
regs->bp = ghcb_get_rbp(ghcb);
578
579
return true;
580
}
581
#endif
582
583
const __initconst struct hypervisor_x86 x86_hyper_vmware = {
584
.name = "VMware",
585
.detect = vmware_platform,
586
.type = X86_HYPER_VMWARE,
587
.init.init_platform = vmware_platform_setup,
588
.init.x2apic_available = vmware_legacy_x2apic_available,
589
#ifdef CONFIG_AMD_MEM_ENCRYPT
590
.runtime.sev_es_hcall_prepare = vmware_sev_es_hcall_prepare,
591
.runtime.sev_es_hcall_finish = vmware_sev_es_hcall_finish,
592
#endif
593
};
594
595