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 <asm/div64.h>
31 #include <asm/x86_init.h>
32 #include <asm/hypervisor.h>
33 #include <asm/timer.h>
35 #include <asm/vmware.h>
39 #define pr_fmt(fmt) "vmware: " fmt
41 #define CPUID_VMWARE_INFO_LEAF 0x40000000
42 #define CPUID_VMWARE_FEATURES_LEAF 0x40000010
43 #define CPUID_VMWARE_FEATURES_ECX_VMMCALL BIT(0)
44 #define CPUID_VMWARE_FEATURES_ECX_VMCALL BIT(1)
46 #define VMWARE_HYPERVISOR_MAGIC 0x564D5868
48 #define VMWARE_CMD_GETVERSION 10
49 #define VMWARE_CMD_GETHZ 45
50 #define VMWARE_CMD_GETVCPU_INFO 68
51 #define VMWARE_CMD_LEGACY_X2APIC 3
52 #define VMWARE_CMD_VCPU_RESERVED 31
53 #define VMWARE_CMD_STEALCLOCK 91
55 #define STEALCLOCK_NOT_AVAILABLE (-1)
56 #define STEALCLOCK_DISABLED 0
57 #define STEALCLOCK_ENABLED 1
59 #define VMWARE_PORT(cmd, eax, ebx, ecx, edx) \
60 __asm__("inl (%%dx), %%eax" : \
61 "=a"(eax), "=c"(ecx), "=d"(edx), "=b"(ebx) : \
62 "a"(VMWARE_HYPERVISOR_MAGIC), \
63 "c"(VMWARE_CMD_##cmd), \
64 "d"(VMWARE_HYPERVISOR_PORT), "b"(UINT_MAX) : \
67 #define VMWARE_VMCALL(cmd, eax, ebx, ecx, edx) \
69 "=a"(eax), "=c"(ecx), "=d"(edx), "=b"(ebx) : \
70 "a"(VMWARE_HYPERVISOR_MAGIC), \
71 "c"(VMWARE_CMD_##cmd), \
72 "d"(0), "b"(UINT_MAX) : \
75 #define VMWARE_VMMCALL(cmd, eax, ebx, ecx, edx) \
77 "=a"(eax), "=c"(ecx), "=d"(edx), "=b"(ebx) : \
78 "a"(VMWARE_HYPERVISOR_MAGIC), \
79 "c"(VMWARE_CMD_##cmd), \
80 "d"(0), "b"(UINT_MAX) : \
83 #define VMWARE_CMD(cmd, eax, ebx, ecx, edx) do { \
84 switch (vmware_hypercall_mode) { \
85 case CPUID_VMWARE_FEATURES_ECX_VMCALL: \
86 VMWARE_VMCALL(cmd, eax, ebx, ecx, edx); \
88 case CPUID_VMWARE_FEATURES_ECX_VMMCALL: \
89 VMWARE_VMMCALL(cmd, eax, ebx, ecx, edx); \
92 VMWARE_PORT(cmd, eax, ebx, ecx, edx); \
97 struct vmware_steal_time {
99 uint64_t clock; /* stolen time counter in units of vtsc */
101 /* only for little-endian */
106 uint64_t reserved[7];
109 static unsigned long vmware_tsc_khz __ro_after_init;
110 static u8 vmware_hypercall_mode __ro_after_init;
112 static inline int __vmware_platform(void)
114 uint32_t eax, ebx, ecx, edx;
115 VMWARE_CMD(GETVERSION, eax, ebx, ecx, edx);
116 return eax != (uint32_t)-1 && ebx == VMWARE_HYPERVISOR_MAGIC;
119 static unsigned long vmware_get_tsc_khz(void)
121 return vmware_tsc_khz;
124 #ifdef CONFIG_PARAVIRT
125 static struct cyc2ns_data vmware_cyc2ns __ro_after_init;
126 static bool vmw_sched_clock __initdata = true;
127 static DEFINE_PER_CPU_DECRYPTED(struct vmware_steal_time, vmw_steal_time) __aligned(64);
128 static bool has_steal_clock;
129 static bool steal_acc __initdata = true; /* steal time accounting */
131 static __init int setup_vmw_sched_clock(char *s)
133 vmw_sched_clock = false;
136 early_param("no-vmw-sched-clock", setup_vmw_sched_clock);
138 static __init int parse_no_stealacc(char *arg)
143 early_param("no-steal-acc", parse_no_stealacc);
145 static unsigned long long notrace vmware_sched_clock(void)
147 unsigned long long ns;
149 ns = mul_u64_u32_shr(rdtsc(), vmware_cyc2ns.cyc2ns_mul,
150 vmware_cyc2ns.cyc2ns_shift);
151 ns -= vmware_cyc2ns.cyc2ns_offset;
155 static void __init vmware_cyc2ns_setup(void)
157 struct cyc2ns_data *d = &vmware_cyc2ns;
158 unsigned long long tsc_now = rdtsc();
160 clocks_calc_mult_shift(&d->cyc2ns_mul, &d->cyc2ns_shift,
161 vmware_tsc_khz, NSEC_PER_MSEC, 0);
162 d->cyc2ns_offset = mul_u64_u32_shr(tsc_now, d->cyc2ns_mul,
165 pr_info("using clock offset of %llu ns\n", d->cyc2ns_offset);
168 static int vmware_cmd_stealclock(uint32_t arg1, uint32_t arg2)
170 uint32_t result, info;
172 asm volatile (VMWARE_HYPERCALL :
175 "a"(VMWARE_HYPERVISOR_MAGIC),
177 "c"(VMWARE_CMD_STEALCLOCK),
185 static bool stealclock_enable(phys_addr_t pa)
187 return vmware_cmd_stealclock(upper_32_bits(pa),
188 lower_32_bits(pa)) == STEALCLOCK_ENABLED;
191 static int __stealclock_disable(void)
193 return vmware_cmd_stealclock(0, 1);
196 static void stealclock_disable(void)
198 __stealclock_disable();
201 static bool vmware_is_stealclock_available(void)
203 return __stealclock_disable() != STEALCLOCK_NOT_AVAILABLE;
207 * vmware_steal_clock() - read the per-cpu steal clock
208 * @cpu: the cpu number whose steal clock we want to read
210 * The function reads the steal clock if we are on a 64-bit system, otherwise
211 * reads it in parts, checking that the high part didn't change in the
215 * The steal clock reading in ns.
217 static uint64_t vmware_steal_clock(int cpu)
219 struct vmware_steal_time *steal = &per_cpu(vmw_steal_time, cpu);
222 if (IS_ENABLED(CONFIG_64BIT))
223 clock = READ_ONCE(steal->clock);
225 uint32_t initial_high, low, high;
228 initial_high = READ_ONCE(steal->clock_high);
229 /* Do not reorder initial_high and high readings */
231 low = READ_ONCE(steal->clock_low);
232 /* Keep low reading in between */
234 high = READ_ONCE(steal->clock_high);
235 } while (initial_high != high);
237 clock = ((uint64_t)high << 32) | low;
240 return mul_u64_u32_shr(clock, vmware_cyc2ns.cyc2ns_mul,
241 vmware_cyc2ns.cyc2ns_shift);
244 static void vmware_register_steal_time(void)
246 int cpu = smp_processor_id();
247 struct vmware_steal_time *st = &per_cpu(vmw_steal_time, cpu);
249 if (!has_steal_clock)
252 if (!stealclock_enable(slow_virt_to_phys(st))) {
253 has_steal_clock = false;
257 pr_info("vmware-stealtime: cpu %d, pa %llx\n",
258 cpu, (unsigned long long) slow_virt_to_phys(st));
261 static void vmware_disable_steal_time(void)
263 if (!has_steal_clock)
266 stealclock_disable();
269 static void vmware_guest_cpu_init(void)
272 vmware_register_steal_time();
275 static void vmware_pv_guest_cpu_reboot(void *unused)
277 vmware_disable_steal_time();
280 static int vmware_pv_reboot_notify(struct notifier_block *nb,
281 unsigned long code, void *unused)
283 if (code == SYS_RESTART)
284 on_each_cpu(vmware_pv_guest_cpu_reboot, NULL, 1);
288 static struct notifier_block vmware_pv_reboot_nb = {
289 .notifier_call = vmware_pv_reboot_notify,
293 static void __init vmware_smp_prepare_boot_cpu(void)
295 vmware_guest_cpu_init();
296 native_smp_prepare_boot_cpu();
299 static int vmware_cpu_online(unsigned int cpu)
302 vmware_guest_cpu_init();
307 static int vmware_cpu_down_prepare(unsigned int cpu)
310 vmware_disable_steal_time();
316 static __init int activate_jump_labels(void)
318 if (has_steal_clock) {
319 static_key_slow_inc(¶virt_steal_enabled);
321 static_key_slow_inc(¶virt_steal_rq_enabled);
326 arch_initcall(activate_jump_labels);
328 static void __init vmware_paravirt_ops_setup(void)
330 pv_info.name = "VMware hypervisor";
331 pv_ops.cpu.io_delay = paravirt_nop;
333 if (vmware_tsc_khz == 0)
336 vmware_cyc2ns_setup();
339 pv_ops.time.sched_clock = vmware_sched_clock;
341 if (vmware_is_stealclock_available()) {
342 has_steal_clock = true;
343 pv_ops.time.steal_clock = vmware_steal_clock;
345 /* We use reboot notifier only to disable steal clock */
346 register_reboot_notifier(&vmware_pv_reboot_nb);
349 smp_ops.smp_prepare_boot_cpu =
350 vmware_smp_prepare_boot_cpu;
351 if (cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN,
354 vmware_cpu_down_prepare) < 0)
355 pr_err("vmware_guest: Failed to install cpu hotplug callbacks\n");
357 vmware_guest_cpu_init();
362 #define vmware_paravirt_ops_setup() do {} while (0)
366 * VMware hypervisor takes care of exporting a reliable TSC to the guest.
367 * Still, due to timing difference when running on virtual cpus, the TSC can
368 * be marked as unstable in some cases. For example, the TSC sync check at
369 * bootup can fail due to a marginal offset between vcpus' TSCs (though the
370 * TSCs do not drift from each other). Also, the ACPI PM timer clocksource
371 * is not suitable as a watchdog when running on a hypervisor because the
372 * kernel may miss a wrap of the counter if the vcpu is descheduled for a
373 * long time. To skip these checks at runtime we set these capability bits,
374 * so that the kernel could just trust the hypervisor with providing a
375 * reliable virtual TSC that is suitable for timekeeping.
377 static void __init vmware_set_capabilities(void)
379 setup_force_cpu_cap(X86_FEATURE_CONSTANT_TSC);
380 setup_force_cpu_cap(X86_FEATURE_TSC_RELIABLE);
381 if (vmware_hypercall_mode == CPUID_VMWARE_FEATURES_ECX_VMCALL)
382 setup_force_cpu_cap(X86_FEATURE_VMCALL);
383 else if (vmware_hypercall_mode == CPUID_VMWARE_FEATURES_ECX_VMMCALL)
384 setup_force_cpu_cap(X86_FEATURE_VMW_VMMCALL);
387 static void __init vmware_platform_setup(void)
389 uint32_t eax, ebx, ecx, edx;
390 uint64_t lpj, tsc_khz;
392 VMWARE_CMD(GETHZ, eax, ebx, ecx, edx);
394 if (ebx != UINT_MAX) {
395 lpj = tsc_khz = eax | (((uint64_t)ebx) << 32);
396 do_div(tsc_khz, 1000);
397 WARN_ON(tsc_khz >> 32);
398 pr_info("TSC freq read from hypervisor : %lu.%03lu MHz\n",
399 (unsigned long) tsc_khz / 1000,
400 (unsigned long) tsc_khz % 1000);
407 vmware_tsc_khz = tsc_khz;
408 x86_platform.calibrate_tsc = vmware_get_tsc_khz;
409 x86_platform.calibrate_cpu = vmware_get_tsc_khz;
411 #ifdef CONFIG_X86_LOCAL_APIC
412 /* Skip lapic calibration since we know the bus frequency. */
413 lapic_timer_period = ecx / HZ;
414 pr_info("Host bus clock speed read from hypervisor : %u Hz\n",
418 pr_warn("Failed to get TSC freq from the hypervisor\n");
421 vmware_paravirt_ops_setup();
423 #ifdef CONFIG_X86_IO_APIC
427 vmware_set_capabilities();
430 static u8 __init vmware_select_hypercall(void)
432 int eax, ebx, ecx, edx;
434 cpuid(CPUID_VMWARE_FEATURES_LEAF, &eax, &ebx, &ecx, &edx);
435 return (ecx & (CPUID_VMWARE_FEATURES_ECX_VMMCALL |
436 CPUID_VMWARE_FEATURES_ECX_VMCALL));
440 * While checking the dmi string information, just checking the product
441 * serial key should be enough, as this will always have a VMware
442 * specific string when running under VMware hypervisor.
443 * If !boot_cpu_has(X86_FEATURE_HYPERVISOR), vmware_hypercall_mode
444 * intentionally defaults to 0.
446 static uint32_t __init vmware_platform(void)
448 if (boot_cpu_has(X86_FEATURE_HYPERVISOR)) {
450 unsigned int hyper_vendor_id[3];
452 cpuid(CPUID_VMWARE_INFO_LEAF, &eax, &hyper_vendor_id[0],
453 &hyper_vendor_id[1], &hyper_vendor_id[2]);
454 if (!memcmp(hyper_vendor_id, "VMwareVMware", 12)) {
455 if (eax >= CPUID_VMWARE_FEATURES_LEAF)
456 vmware_hypercall_mode =
457 vmware_select_hypercall();
459 pr_info("hypercall mode: 0x%02x\n",
460 (unsigned int) vmware_hypercall_mode);
462 return CPUID_VMWARE_INFO_LEAF;
464 } else if (dmi_available && dmi_name_in_serial("VMware") &&
471 /* Checks if hypervisor supports x2apic without VT-D interrupt remapping. */
472 static bool __init vmware_legacy_x2apic_available(void)
474 uint32_t eax, ebx, ecx, edx;
475 VMWARE_CMD(GETVCPU_INFO, eax, ebx, ecx, edx);
476 return (eax & (1 << VMWARE_CMD_VCPU_RESERVED)) == 0 &&
477 (eax & (1 << VMWARE_CMD_LEGACY_X2APIC)) != 0;
480 #ifdef CONFIG_AMD_MEM_ENCRYPT
481 static void vmware_sev_es_hcall_prepare(struct ghcb *ghcb,
482 struct pt_regs *regs)
484 /* Copy VMWARE specific Hypercall parameters to the GHCB */
485 ghcb_set_rip(ghcb, regs->ip);
486 ghcb_set_rbx(ghcb, regs->bx);
487 ghcb_set_rcx(ghcb, regs->cx);
488 ghcb_set_rdx(ghcb, regs->dx);
489 ghcb_set_rsi(ghcb, regs->si);
490 ghcb_set_rdi(ghcb, regs->di);
491 ghcb_set_rbp(ghcb, regs->bp);
494 static bool vmware_sev_es_hcall_finish(struct ghcb *ghcb, struct pt_regs *regs)
496 if (!(ghcb_rbx_is_valid(ghcb) &&
497 ghcb_rcx_is_valid(ghcb) &&
498 ghcb_rdx_is_valid(ghcb) &&
499 ghcb_rsi_is_valid(ghcb) &&
500 ghcb_rdi_is_valid(ghcb) &&
501 ghcb_rbp_is_valid(ghcb)))
504 regs->bx = ghcb->save.rbx;
505 regs->cx = ghcb->save.rcx;
506 regs->dx = ghcb->save.rdx;
507 regs->si = ghcb->save.rsi;
508 regs->di = ghcb->save.rdi;
509 regs->bp = ghcb->save.rbp;
515 const __initconst struct hypervisor_x86 x86_hyper_vmware = {
517 .detect = vmware_platform,
518 .type = X86_HYPER_VMWARE,
519 .init.init_platform = vmware_platform_setup,
520 .init.x2apic_available = vmware_legacy_x2apic_available,
521 #ifdef CONFIG_AMD_MEM_ENCRYPT
522 .runtime.sev_es_hcall_prepare = vmware_sev_es_hcall_prepare,
523 .runtime.sev_es_hcall_finish = vmware_sev_es_hcall_finish,