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
44 #define CPUID_VMWARE_FEATURES_ECX_VMMCALL BIT(0)
45 #define CPUID_VMWARE_FEATURES_ECX_VMCALL BIT(1)
47 #define VMWARE_HYPERVISOR_MAGIC 0x564D5868
49 #define VMWARE_CMD_GETVERSION 10
50 #define VMWARE_CMD_GETHZ 45
51 #define VMWARE_CMD_GETVCPU_INFO 68
52 #define VMWARE_CMD_LEGACY_X2APIC 3
53 #define VMWARE_CMD_VCPU_RESERVED 31
54 #define VMWARE_CMD_STEALCLOCK 91
56 #define STEALCLOCK_NOT_AVAILABLE (-1)
57 #define STEALCLOCK_DISABLED 0
58 #define STEALCLOCK_ENABLED 1
60 #define VMWARE_PORT(cmd, eax, ebx, ecx, edx) \
61 __asm__("inl (%%dx), %%eax" : \
62 "=a"(eax), "=c"(ecx), "=d"(edx), "=b"(ebx) : \
63 "a"(VMWARE_HYPERVISOR_MAGIC), \
64 "c"(VMWARE_CMD_##cmd), \
65 "d"(VMWARE_HYPERVISOR_PORT), "b"(UINT_MAX) : \
68 #define VMWARE_VMCALL(cmd, eax, ebx, ecx, edx) \
70 "=a"(eax), "=c"(ecx), "=d"(edx), "=b"(ebx) : \
71 "a"(VMWARE_HYPERVISOR_MAGIC), \
72 "c"(VMWARE_CMD_##cmd), \
73 "d"(0), "b"(UINT_MAX) : \
76 #define VMWARE_VMMCALL(cmd, eax, ebx, ecx, edx) \
78 "=a"(eax), "=c"(ecx), "=d"(edx), "=b"(ebx) : \
79 "a"(VMWARE_HYPERVISOR_MAGIC), \
80 "c"(VMWARE_CMD_##cmd), \
81 "d"(0), "b"(UINT_MAX) : \
84 #define VMWARE_CMD(cmd, eax, ebx, ecx, edx) do { \
85 switch (vmware_hypercall_mode) { \
86 case CPUID_VMWARE_FEATURES_ECX_VMCALL: \
87 VMWARE_VMCALL(cmd, eax, ebx, ecx, edx); \
89 case CPUID_VMWARE_FEATURES_ECX_VMMCALL: \
90 VMWARE_VMMCALL(cmd, eax, ebx, ecx, edx); \
93 VMWARE_PORT(cmd, eax, ebx, ecx, edx); \
98 struct vmware_steal_time {
100 uint64_t clock; /* stolen time counter in units of vtsc */
102 /* only for little-endian */
107 uint64_t reserved[7];
110 static unsigned long vmware_tsc_khz __ro_after_init;
111 static u8 vmware_hypercall_mode __ro_after_init;
113 static inline int __vmware_platform(void)
115 uint32_t eax, ebx, ecx, edx;
116 VMWARE_CMD(GETVERSION, eax, ebx, ecx, edx);
117 return eax != (uint32_t)-1 && ebx == VMWARE_HYPERVISOR_MAGIC;
120 static unsigned long vmware_get_tsc_khz(void)
122 return vmware_tsc_khz;
125 #ifdef CONFIG_PARAVIRT
126 static struct cyc2ns_data vmware_cyc2ns __ro_after_init;
127 static bool vmw_sched_clock __initdata = true;
128 static DEFINE_PER_CPU_DECRYPTED(struct vmware_steal_time, vmw_steal_time) __aligned(64);
129 static bool has_steal_clock;
130 static bool steal_acc __initdata = true; /* steal time accounting */
132 static __init int setup_vmw_sched_clock(char *s)
134 vmw_sched_clock = false;
137 early_param("no-vmw-sched-clock", setup_vmw_sched_clock);
139 static __init int parse_no_stealacc(char *arg)
144 early_param("no-steal-acc", parse_no_stealacc);
146 static unsigned long long notrace vmware_sched_clock(void)
148 unsigned long long ns;
150 ns = mul_u64_u32_shr(rdtsc(), vmware_cyc2ns.cyc2ns_mul,
151 vmware_cyc2ns.cyc2ns_shift);
152 ns -= vmware_cyc2ns.cyc2ns_offset;
156 static void __init vmware_cyc2ns_setup(void)
158 struct cyc2ns_data *d = &vmware_cyc2ns;
159 unsigned long long tsc_now = rdtsc();
161 clocks_calc_mult_shift(&d->cyc2ns_mul, &d->cyc2ns_shift,
162 vmware_tsc_khz, NSEC_PER_MSEC, 0);
163 d->cyc2ns_offset = mul_u64_u32_shr(tsc_now, d->cyc2ns_mul,
166 pr_info("using clock offset of %llu ns\n", d->cyc2ns_offset);
169 static int vmware_cmd_stealclock(uint32_t arg1, uint32_t arg2)
171 uint32_t result, info;
173 asm volatile (VMWARE_HYPERCALL :
176 "a"(VMWARE_HYPERVISOR_MAGIC),
178 "c"(VMWARE_CMD_STEALCLOCK),
186 static bool stealclock_enable(phys_addr_t pa)
188 return vmware_cmd_stealclock(upper_32_bits(pa),
189 lower_32_bits(pa)) == STEALCLOCK_ENABLED;
192 static int __stealclock_disable(void)
194 return vmware_cmd_stealclock(0, 1);
197 static void stealclock_disable(void)
199 __stealclock_disable();
202 static bool vmware_is_stealclock_available(void)
204 return __stealclock_disable() != STEALCLOCK_NOT_AVAILABLE;
208 * vmware_steal_clock() - read the per-cpu steal clock
209 * @cpu: the cpu number whose steal clock we want to read
211 * The function reads the steal clock if we are on a 64-bit system, otherwise
212 * reads it in parts, checking that the high part didn't change in the
216 * The steal clock reading in ns.
218 static uint64_t vmware_steal_clock(int cpu)
220 struct vmware_steal_time *steal = &per_cpu(vmw_steal_time, cpu);
223 if (IS_ENABLED(CONFIG_64BIT))
224 clock = READ_ONCE(steal->clock);
226 uint32_t initial_high, low, high;
229 initial_high = READ_ONCE(steal->clock_high);
230 /* Do not reorder initial_high and high readings */
232 low = READ_ONCE(steal->clock_low);
233 /* Keep low reading in between */
235 high = READ_ONCE(steal->clock_high);
236 } while (initial_high != high);
238 clock = ((uint64_t)high << 32) | low;
241 return mul_u64_u32_shr(clock, vmware_cyc2ns.cyc2ns_mul,
242 vmware_cyc2ns.cyc2ns_shift);
245 static void vmware_register_steal_time(void)
247 int cpu = smp_processor_id();
248 struct vmware_steal_time *st = &per_cpu(vmw_steal_time, cpu);
250 if (!has_steal_clock)
253 if (!stealclock_enable(slow_virt_to_phys(st))) {
254 has_steal_clock = false;
258 pr_info("vmware-stealtime: cpu %d, pa %llx\n",
259 cpu, (unsigned long long) slow_virt_to_phys(st));
262 static void vmware_disable_steal_time(void)
264 if (!has_steal_clock)
267 stealclock_disable();
270 static void vmware_guest_cpu_init(void)
273 vmware_register_steal_time();
276 static void vmware_pv_guest_cpu_reboot(void *unused)
278 vmware_disable_steal_time();
281 static int vmware_pv_reboot_notify(struct notifier_block *nb,
282 unsigned long code, void *unused)
284 if (code == SYS_RESTART)
285 on_each_cpu(vmware_pv_guest_cpu_reboot, NULL, 1);
289 static struct notifier_block vmware_pv_reboot_nb = {
290 .notifier_call = vmware_pv_reboot_notify,
294 static void __init vmware_smp_prepare_boot_cpu(void)
296 vmware_guest_cpu_init();
297 native_smp_prepare_boot_cpu();
300 static int vmware_cpu_online(unsigned int cpu)
303 vmware_guest_cpu_init();
308 static int vmware_cpu_down_prepare(unsigned int cpu)
311 vmware_disable_steal_time();
317 static __init int activate_jump_labels(void)
319 if (has_steal_clock) {
320 static_key_slow_inc(¶virt_steal_enabled);
322 static_key_slow_inc(¶virt_steal_rq_enabled);
327 arch_initcall(activate_jump_labels);
329 static void __init vmware_paravirt_ops_setup(void)
331 pv_info.name = "VMware hypervisor";
332 pv_ops.cpu.io_delay = paravirt_nop;
334 if (vmware_tsc_khz == 0)
337 vmware_cyc2ns_setup();
340 paravirt_set_sched_clock(vmware_sched_clock);
342 if (vmware_is_stealclock_available()) {
343 has_steal_clock = true;
344 static_call_update(pv_steal_clock, vmware_steal_clock);
346 /* We use reboot notifier only to disable steal clock */
347 register_reboot_notifier(&vmware_pv_reboot_nb);
350 smp_ops.smp_prepare_boot_cpu =
351 vmware_smp_prepare_boot_cpu;
352 if (cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN,
355 vmware_cpu_down_prepare) < 0)
356 pr_err("vmware_guest: Failed to install cpu hotplug callbacks\n");
358 vmware_guest_cpu_init();
363 #define vmware_paravirt_ops_setup() do {} while (0)
367 * VMware hypervisor takes care of exporting a reliable TSC to the guest.
368 * Still, due to timing difference when running on virtual cpus, the TSC can
369 * be marked as unstable in some cases. For example, the TSC sync check at
370 * bootup can fail due to a marginal offset between vcpus' TSCs (though the
371 * TSCs do not drift from each other). Also, the ACPI PM timer clocksource
372 * is not suitable as a watchdog when running on a hypervisor because the
373 * kernel may miss a wrap of the counter if the vcpu is descheduled for a
374 * long time. To skip these checks at runtime we set these capability bits,
375 * so that the kernel could just trust the hypervisor with providing a
376 * reliable virtual TSC that is suitable for timekeeping.
378 static void __init vmware_set_capabilities(void)
380 setup_force_cpu_cap(X86_FEATURE_CONSTANT_TSC);
381 setup_force_cpu_cap(X86_FEATURE_TSC_RELIABLE);
383 setup_force_cpu_cap(X86_FEATURE_TSC_KNOWN_FREQ);
384 if (vmware_hypercall_mode == CPUID_VMWARE_FEATURES_ECX_VMCALL)
385 setup_force_cpu_cap(X86_FEATURE_VMCALL);
386 else if (vmware_hypercall_mode == CPUID_VMWARE_FEATURES_ECX_VMMCALL)
387 setup_force_cpu_cap(X86_FEATURE_VMW_VMMCALL);
390 static void __init vmware_platform_setup(void)
392 uint32_t eax, ebx, ecx, edx;
393 uint64_t lpj, tsc_khz;
395 VMWARE_CMD(GETHZ, eax, ebx, ecx, edx);
397 if (ebx != UINT_MAX) {
398 lpj = tsc_khz = eax | (((uint64_t)ebx) << 32);
399 do_div(tsc_khz, 1000);
400 WARN_ON(tsc_khz >> 32);
401 pr_info("TSC freq read from hypervisor : %lu.%03lu MHz\n",
402 (unsigned long) tsc_khz / 1000,
403 (unsigned long) tsc_khz % 1000);
410 vmware_tsc_khz = tsc_khz;
411 x86_platform.calibrate_tsc = vmware_get_tsc_khz;
412 x86_platform.calibrate_cpu = vmware_get_tsc_khz;
414 #ifdef CONFIG_X86_LOCAL_APIC
415 /* Skip lapic calibration since we know the bus frequency. */
416 lapic_timer_period = ecx / HZ;
417 pr_info("Host bus clock speed read from hypervisor : %u Hz\n",
421 pr_warn("Failed to get TSC freq from the hypervisor\n");
424 vmware_paravirt_ops_setup();
426 #ifdef CONFIG_X86_IO_APIC
430 vmware_set_capabilities();
433 static u8 __init vmware_select_hypercall(void)
435 int eax, ebx, ecx, edx;
437 cpuid(CPUID_VMWARE_FEATURES_LEAF, &eax, &ebx, &ecx, &edx);
438 return (ecx & (CPUID_VMWARE_FEATURES_ECX_VMMCALL |
439 CPUID_VMWARE_FEATURES_ECX_VMCALL));
443 * While checking the dmi string information, just checking the product
444 * serial key should be enough, as this will always have a VMware
445 * specific string when running under VMware hypervisor.
446 * If !boot_cpu_has(X86_FEATURE_HYPERVISOR), vmware_hypercall_mode
447 * intentionally defaults to 0.
449 static uint32_t __init vmware_platform(void)
451 if (boot_cpu_has(X86_FEATURE_HYPERVISOR)) {
453 unsigned int hyper_vendor_id[3];
455 cpuid(CPUID_VMWARE_INFO_LEAF, &eax, &hyper_vendor_id[0],
456 &hyper_vendor_id[1], &hyper_vendor_id[2]);
457 if (!memcmp(hyper_vendor_id, "VMwareVMware", 12)) {
458 if (eax >= CPUID_VMWARE_FEATURES_LEAF)
459 vmware_hypercall_mode =
460 vmware_select_hypercall();
462 pr_info("hypercall mode: 0x%02x\n",
463 (unsigned int) vmware_hypercall_mode);
465 return CPUID_VMWARE_INFO_LEAF;
467 } else if (dmi_available && dmi_name_in_serial("VMware") &&
474 /* Checks if hypervisor supports x2apic without VT-D interrupt remapping. */
475 static bool __init vmware_legacy_x2apic_available(void)
477 uint32_t eax, ebx, ecx, edx;
478 VMWARE_CMD(GETVCPU_INFO, eax, ebx, ecx, edx);
479 return (eax & (1 << VMWARE_CMD_VCPU_RESERVED)) == 0 &&
480 (eax & (1 << VMWARE_CMD_LEGACY_X2APIC)) != 0;
483 #ifdef CONFIG_AMD_MEM_ENCRYPT
484 static void vmware_sev_es_hcall_prepare(struct ghcb *ghcb,
485 struct pt_regs *regs)
487 /* Copy VMWARE specific Hypercall parameters to the GHCB */
488 ghcb_set_rip(ghcb, regs->ip);
489 ghcb_set_rbx(ghcb, regs->bx);
490 ghcb_set_rcx(ghcb, regs->cx);
491 ghcb_set_rdx(ghcb, regs->dx);
492 ghcb_set_rsi(ghcb, regs->si);
493 ghcb_set_rdi(ghcb, regs->di);
494 ghcb_set_rbp(ghcb, regs->bp);
497 static bool vmware_sev_es_hcall_finish(struct ghcb *ghcb, struct pt_regs *regs)
499 if (!(ghcb_rbx_is_valid(ghcb) &&
500 ghcb_rcx_is_valid(ghcb) &&
501 ghcb_rdx_is_valid(ghcb) &&
502 ghcb_rsi_is_valid(ghcb) &&
503 ghcb_rdi_is_valid(ghcb) &&
504 ghcb_rbp_is_valid(ghcb)))
507 regs->bx = ghcb_get_rbx(ghcb);
508 regs->cx = ghcb_get_rcx(ghcb);
509 regs->dx = ghcb_get_rdx(ghcb);
510 regs->si = ghcb_get_rsi(ghcb);
511 regs->di = ghcb_get_rdi(ghcb);
512 regs->bp = ghcb_get_rbp(ghcb);
518 const __initconst struct hypervisor_x86 x86_hyper_vmware = {
520 .detect = vmware_platform,
521 .type = X86_HYPER_VMWARE,
522 .init.init_platform = vmware_platform_setup,
523 .init.x2apic_available = vmware_legacy_x2apic_available,
524 #ifdef CONFIG_AMD_MEM_ENCRYPT
525 .runtime.sev_es_hcall_prepare = vmware_sev_es_hcall_prepare,
526 .runtime.sev_es_hcall_finish = vmware_sev_es_hcall_finish,