vmware.c 15 KB

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