2 * VMware Detection code.
4 * Copyright (C) 2008, VMware, Inc.
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.
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
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.
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/reboot.h>
30 #include <linux/static_call.h>
31 #include <asm/div64.h>
32 #include <asm/x86_init.h>
33 #include <asm/hypervisor.h>
34 #include <asm/timer.h>
36 #include <asm/vmware.h>
40 #define pr_fmt(fmt) "vmware: " fmt
42 #define CPUID_VMWARE_INFO_LEAF 0x40000000
43 #define CPUID_VMWARE_FEATURES_LEAF 0x40000010
45 #define GETVCPU_INFO_LEGACY_X2APIC BIT(3)
46 #define GETVCPU_INFO_VCPU_RESERVED BIT(31)
48 #define STEALCLOCK_NOT_AVAILABLE (-1)
49 #define STEALCLOCK_DISABLED 0
50 #define STEALCLOCK_ENABLED 1
52 struct vmware_steal_time {
54 u64 clock; /* stolen time counter in units of vtsc */
56 /* only for little-endian */
64 static unsigned long vmware_tsc_khz __ro_after_init;
65 static u8 vmware_hypercall_mode __ro_after_init;
67 unsigned long vmware_hypercall_slow(unsigned long cmd,
68 unsigned long in1, unsigned long in3,
69 unsigned long in4, unsigned long in5,
70 u32 *out1, u32 *out2, u32 *out3,
73 unsigned long out0, rbx, rcx, rdx, rsi, rdi;
75 switch (vmware_hypercall_mode) {
76 case CPUID_VMWARE_FEATURES_ECX_VMCALL:
77 asm_inline volatile ("vmcall"
78 : "=a" (out0), "=b" (rbx), "=c" (rcx),
79 "=d" (rdx), "=S" (rsi), "=D" (rdi)
80 : "a" (VMWARE_HYPERVISOR_MAGIC),
88 case CPUID_VMWARE_FEATURES_ECX_VMMCALL:
89 asm_inline volatile ("vmmcall"
90 : "=a" (out0), "=b" (rbx), "=c" (rcx),
91 "=d" (rdx), "=S" (rsi), "=D" (rdi)
92 : "a" (VMWARE_HYPERVISOR_MAGIC),
101 asm_inline volatile ("movw %[port], %%dx; inl (%%dx), %%eax"
102 : "=a" (out0), "=b" (rbx), "=c" (rcx),
103 "=d" (rdx), "=S" (rsi), "=D" (rdi)
104 : [port] "i" (VMWARE_HYPERVISOR_PORT),
105 "a" (VMWARE_HYPERVISOR_MAGIC),
129 static inline int __vmware_platform(void)
133 eax = vmware_hypercall3(VMWARE_CMD_GETVERSION, 0, &ebx, &ecx);
134 return eax != UINT_MAX && ebx == VMWARE_HYPERVISOR_MAGIC;
137 static unsigned long vmware_get_tsc_khz(void)
139 return vmware_tsc_khz;
142 #ifdef CONFIG_PARAVIRT
143 static struct cyc2ns_data vmware_cyc2ns __ro_after_init;
144 static bool vmw_sched_clock __initdata = true;
145 static DEFINE_PER_CPU_DECRYPTED(struct vmware_steal_time, vmw_steal_time) __aligned(64);
146 static bool has_steal_clock;
147 static bool steal_acc __initdata = true; /* steal time accounting */
149 static __init int setup_vmw_sched_clock(char *s)
151 vmw_sched_clock = false;
154 early_param("no-vmw-sched-clock", setup_vmw_sched_clock);
156 static __init int parse_no_stealacc(char *arg)
161 early_param("no-steal-acc", parse_no_stealacc);
163 static noinstr u64 vmware_sched_clock(void)
165 unsigned long long ns;
167 ns = mul_u64_u32_shr(rdtsc(), vmware_cyc2ns.cyc2ns_mul,
168 vmware_cyc2ns.cyc2ns_shift);
169 ns -= vmware_cyc2ns.cyc2ns_offset;
173 static void __init vmware_cyc2ns_setup(void)
175 struct cyc2ns_data *d = &vmware_cyc2ns;
176 unsigned long long tsc_now = rdtsc();
178 clocks_calc_mult_shift(&d->cyc2ns_mul, &d->cyc2ns_shift,
179 vmware_tsc_khz, NSEC_PER_MSEC, 0);
180 d->cyc2ns_offset = mul_u64_u32_shr(tsc_now, d->cyc2ns_mul,
183 pr_info("using clock offset of %llu ns\n", d->cyc2ns_offset);
186 static int vmware_cmd_stealclock(u32 addr_hi, u32 addr_lo)
190 return vmware_hypercall5(VMWARE_CMD_STEALCLOCK, 0, 0, addr_hi, addr_lo,
194 static bool stealclock_enable(phys_addr_t pa)
196 return vmware_cmd_stealclock(upper_32_bits(pa),
197 lower_32_bits(pa)) == STEALCLOCK_ENABLED;
200 static int __stealclock_disable(void)
202 return vmware_cmd_stealclock(0, 1);
205 static void stealclock_disable(void)
207 __stealclock_disable();
210 static bool vmware_is_stealclock_available(void)
212 return __stealclock_disable() != STEALCLOCK_NOT_AVAILABLE;
216 * vmware_steal_clock() - read the per-cpu steal clock
217 * @cpu: the cpu number whose steal clock we want to read
219 * The function reads the steal clock if we are on a 64-bit system, otherwise
220 * reads it in parts, checking that the high part didn't change in the
224 * The steal clock reading in ns.
226 static u64 vmware_steal_clock(int cpu)
228 struct vmware_steal_time *steal = &per_cpu(vmw_steal_time, cpu);
231 if (IS_ENABLED(CONFIG_64BIT))
232 clock = READ_ONCE(steal->clock);
234 u32 initial_high, low, high;
237 initial_high = READ_ONCE(steal->clock_high);
238 /* Do not reorder initial_high and high readings */
240 low = READ_ONCE(steal->clock_low);
241 /* Keep low reading in between */
243 high = READ_ONCE(steal->clock_high);
244 } while (initial_high != high);
246 clock = ((u64)high << 32) | low;
249 return mul_u64_u32_shr(clock, vmware_cyc2ns.cyc2ns_mul,
250 vmware_cyc2ns.cyc2ns_shift);
253 static void vmware_register_steal_time(void)
255 int cpu = smp_processor_id();
256 struct vmware_steal_time *st = &per_cpu(vmw_steal_time, cpu);
258 if (!has_steal_clock)
261 if (!stealclock_enable(slow_virt_to_phys(st))) {
262 has_steal_clock = false;
266 pr_info("vmware-stealtime: cpu %d, pa %llx\n",
267 cpu, (unsigned long long) slow_virt_to_phys(st));
270 static void vmware_disable_steal_time(void)
272 if (!has_steal_clock)
275 stealclock_disable();
278 static void vmware_guest_cpu_init(void)
281 vmware_register_steal_time();
284 static void vmware_pv_guest_cpu_reboot(void *unused)
286 vmware_disable_steal_time();
289 static int vmware_pv_reboot_notify(struct notifier_block *nb,
290 unsigned long code, void *unused)
292 if (code == SYS_RESTART)
293 on_each_cpu(vmware_pv_guest_cpu_reboot, NULL, 1);
297 static struct notifier_block vmware_pv_reboot_nb = {
298 .notifier_call = vmware_pv_reboot_notify,
302 static void __init vmware_smp_prepare_boot_cpu(void)
304 vmware_guest_cpu_init();
305 native_smp_prepare_boot_cpu();
308 static int vmware_cpu_online(unsigned int cpu)
311 vmware_guest_cpu_init();
316 static int vmware_cpu_down_prepare(unsigned int cpu)
319 vmware_disable_steal_time();
325 static __init int activate_jump_labels(void)
327 if (has_steal_clock) {
328 static_key_slow_inc(¶virt_steal_enabled);
330 static_key_slow_inc(¶virt_steal_rq_enabled);
335 arch_initcall(activate_jump_labels);
337 static void __init vmware_paravirt_ops_setup(void)
339 pv_info.name = "VMware hypervisor";
340 pv_ops.cpu.io_delay = paravirt_nop;
342 if (vmware_tsc_khz == 0)
345 vmware_cyc2ns_setup();
348 paravirt_set_sched_clock(vmware_sched_clock);
350 if (vmware_is_stealclock_available()) {
351 has_steal_clock = true;
352 static_call_update(pv_steal_clock, vmware_steal_clock);
354 /* We use reboot notifier only to disable steal clock */
355 register_reboot_notifier(&vmware_pv_reboot_nb);
358 smp_ops.smp_prepare_boot_cpu =
359 vmware_smp_prepare_boot_cpu;
360 if (cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN,
363 vmware_cpu_down_prepare) < 0)
364 pr_err("vmware_guest: Failed to install cpu hotplug callbacks\n");
366 vmware_guest_cpu_init();
371 #define vmware_paravirt_ops_setup() do {} while (0)
375 * VMware hypervisor takes care of exporting a reliable TSC to the guest.
376 * Still, due to timing difference when running on virtual cpus, the TSC can
377 * be marked as unstable in some cases. For example, the TSC sync check at
378 * bootup can fail due to a marginal offset between vcpus' TSCs (though the
379 * TSCs do not drift from each other). Also, the ACPI PM timer clocksource
380 * is not suitable as a watchdog when running on a hypervisor because the
381 * kernel may miss a wrap of the counter if the vcpu is descheduled for a
382 * long time. To skip these checks at runtime we set these capability bits,
383 * so that the kernel could just trust the hypervisor with providing a
384 * reliable virtual TSC that is suitable for timekeeping.
386 static void __init vmware_set_capabilities(void)
388 setup_force_cpu_cap(X86_FEATURE_CONSTANT_TSC);
389 setup_force_cpu_cap(X86_FEATURE_TSC_RELIABLE);
391 setup_force_cpu_cap(X86_FEATURE_TSC_KNOWN_FREQ);
392 if (vmware_hypercall_mode == CPUID_VMWARE_FEATURES_ECX_VMCALL)
393 setup_force_cpu_cap(X86_FEATURE_VMCALL);
394 else if (vmware_hypercall_mode == CPUID_VMWARE_FEATURES_ECX_VMMCALL)
395 setup_force_cpu_cap(X86_FEATURE_VMW_VMMCALL);
398 static void __init vmware_platform_setup(void)
403 eax = vmware_hypercall3(VMWARE_CMD_GETHZ, UINT_MAX, &ebx, &ecx);
405 if (ebx != UINT_MAX) {
406 lpj = tsc_khz = eax | (((u64)ebx) << 32);
407 do_div(tsc_khz, 1000);
408 WARN_ON(tsc_khz >> 32);
409 pr_info("TSC freq read from hypervisor : %lu.%03lu MHz\n",
410 (unsigned long) tsc_khz / 1000,
411 (unsigned long) tsc_khz % 1000);
418 vmware_tsc_khz = tsc_khz;
419 x86_platform.calibrate_tsc = vmware_get_tsc_khz;
420 x86_platform.calibrate_cpu = vmware_get_tsc_khz;
422 #ifdef CONFIG_X86_LOCAL_APIC
423 /* Skip lapic calibration since we know the bus frequency. */
424 lapic_timer_period = ecx / HZ;
425 pr_info("Host bus clock speed read from hypervisor : %u Hz\n",
429 pr_warn("Failed to get TSC freq from the hypervisor\n");
432 vmware_paravirt_ops_setup();
434 #ifdef CONFIG_X86_IO_APIC
438 vmware_set_capabilities();
441 static u8 __init vmware_select_hypercall(void)
443 int eax, ebx, ecx, edx;
445 cpuid(CPUID_VMWARE_FEATURES_LEAF, &eax, &ebx, &ecx, &edx);
446 return (ecx & (CPUID_VMWARE_FEATURES_ECX_VMMCALL |
447 CPUID_VMWARE_FEATURES_ECX_VMCALL));
451 * While checking the dmi string information, just checking the product
452 * serial key should be enough, as this will always have a VMware
453 * specific string when running under VMware hypervisor.
454 * If !boot_cpu_has(X86_FEATURE_HYPERVISOR), vmware_hypercall_mode
455 * intentionally defaults to 0.
457 static u32 __init vmware_platform(void)
459 if (boot_cpu_has(X86_FEATURE_HYPERVISOR)) {
461 unsigned int hyper_vendor_id[3];
463 cpuid(CPUID_VMWARE_INFO_LEAF, &eax, &hyper_vendor_id[0],
464 &hyper_vendor_id[1], &hyper_vendor_id[2]);
465 if (!memcmp(hyper_vendor_id, "VMwareVMware", 12)) {
466 if (eax >= CPUID_VMWARE_FEATURES_LEAF)
467 vmware_hypercall_mode =
468 vmware_select_hypercall();
470 pr_info("hypercall mode: 0x%02x\n",
471 (unsigned int) vmware_hypercall_mode);
473 return CPUID_VMWARE_INFO_LEAF;
475 } else if (dmi_available && dmi_name_in_serial("VMware") &&
482 /* Checks if hypervisor supports x2apic without VT-D interrupt remapping. */
483 static bool __init vmware_legacy_x2apic_available(void)
487 eax = vmware_hypercall1(VMWARE_CMD_GETVCPU_INFO, 0);
488 return !(eax & GETVCPU_INFO_VCPU_RESERVED) &&
489 (eax & GETVCPU_INFO_LEGACY_X2APIC);
492 #ifdef CONFIG_INTEL_TDX_GUEST
494 * TDCALL[TDG.VP.VMCALL] uses %rax (arg0) and %rcx (arg2). Therefore,
495 * we remap those registers to %r12 and %r13, respectively.
497 unsigned long vmware_tdx_hypercall(unsigned long cmd,
498 unsigned long in1, unsigned long in3,
499 unsigned long in4, unsigned long in5,
500 u32 *out1, u32 *out2, u32 *out3,
501 u32 *out4, u32 *out5)
503 struct tdx_module_args args = {};
505 if (!hypervisor_is_type(X86_HYPER_VMWARE)) {
506 pr_warn_once("Incorrect usage\n");
510 if (cmd & ~VMWARE_CMD_MASK) {
511 pr_warn_once("Out of range command %lx\n", cmd);
519 args.r10 = VMWARE_TDX_VENDOR_LEAF;
520 args.r11 = VMWARE_TDX_HCALL_FUNC;
521 args.r12 = VMWARE_HYPERVISOR_MAGIC;
526 __tdx_hypercall(&args);
541 EXPORT_SYMBOL_GPL(vmware_tdx_hypercall);
544 #ifdef CONFIG_AMD_MEM_ENCRYPT
545 static void vmware_sev_es_hcall_prepare(struct ghcb *ghcb,
546 struct pt_regs *regs)
548 /* Copy VMWARE specific Hypercall parameters to the GHCB */
549 ghcb_set_rip(ghcb, regs->ip);
550 ghcb_set_rbx(ghcb, regs->bx);
551 ghcb_set_rcx(ghcb, regs->cx);
552 ghcb_set_rdx(ghcb, regs->dx);
553 ghcb_set_rsi(ghcb, regs->si);
554 ghcb_set_rdi(ghcb, regs->di);
555 ghcb_set_rbp(ghcb, regs->bp);
558 static bool vmware_sev_es_hcall_finish(struct ghcb *ghcb, struct pt_regs *regs)
560 if (!(ghcb_rbx_is_valid(ghcb) &&
561 ghcb_rcx_is_valid(ghcb) &&
562 ghcb_rdx_is_valid(ghcb) &&
563 ghcb_rsi_is_valid(ghcb) &&
564 ghcb_rdi_is_valid(ghcb) &&
565 ghcb_rbp_is_valid(ghcb)))
568 regs->bx = ghcb_get_rbx(ghcb);
569 regs->cx = ghcb_get_rcx(ghcb);
570 regs->dx = ghcb_get_rdx(ghcb);
571 regs->si = ghcb_get_rsi(ghcb);
572 regs->di = ghcb_get_rdi(ghcb);
573 regs->bp = ghcb_get_rbp(ghcb);
579 const __initconst struct hypervisor_x86 x86_hyper_vmware = {
581 .detect = vmware_platform,
582 .type = X86_HYPER_VMWARE,
583 .init.init_platform = vmware_platform_setup,
584 .init.x2apic_available = vmware_legacy_x2apic_available,
585 #ifdef CONFIG_AMD_MEM_ENCRYPT
586 .runtime.sev_es_hcall_prepare = vmware_sev_es_hcall_prepare,
587 .runtime.sev_es_hcall_finish = vmware_sev_es_hcall_finish,