kvm.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865
  1. /*
  2. * KVM paravirt_ops implementation
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. *
  18. * Copyright (C) 2007, Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
  19. * Copyright IBM Corporation, 2007
  20. * Authors: Anthony Liguori <aliguori@us.ibm.com>
  21. */
  22. #include <linux/context_tracking.h>
  23. #include <linux/init.h>
  24. #include <linux/kernel.h>
  25. #include <linux/kvm_para.h>
  26. #include <linux/cpu.h>
  27. #include <linux/mm.h>
  28. #include <linux/highmem.h>
  29. #include <linux/hardirq.h>
  30. #include <linux/notifier.h>
  31. #include <linux/reboot.h>
  32. #include <linux/hash.h>
  33. #include <linux/sched.h>
  34. #include <linux/slab.h>
  35. #include <linux/kprobes.h>
  36. #include <linux/debugfs.h>
  37. #include <linux/nmi.h>
  38. #include <linux/swait.h>
  39. #include <asm/timer.h>
  40. #include <asm/cpu.h>
  41. #include <asm/traps.h>
  42. #include <asm/desc.h>
  43. #include <asm/tlbflush.h>
  44. #include <asm/apic.h>
  45. #include <asm/apicdef.h>
  46. #include <asm/hypervisor.h>
  47. #include <asm/tlb.h>
  48. static int kvmapf = 1;
  49. static int __init parse_no_kvmapf(char *arg)
  50. {
  51. kvmapf = 0;
  52. return 0;
  53. }
  54. early_param("no-kvmapf", parse_no_kvmapf);
  55. static int steal_acc = 1;
  56. static int __init parse_no_stealacc(char *arg)
  57. {
  58. steal_acc = 0;
  59. return 0;
  60. }
  61. early_param("no-steal-acc", parse_no_stealacc);
  62. static DEFINE_PER_CPU_DECRYPTED(struct kvm_vcpu_pv_apf_data, apf_reason) __aligned(64);
  63. static DEFINE_PER_CPU_DECRYPTED(struct kvm_steal_time, steal_time) __aligned(64);
  64. static int has_steal_clock = 0;
  65. /*
  66. * No need for any "IO delay" on KVM
  67. */
  68. static void kvm_io_delay(void)
  69. {
  70. }
  71. #define KVM_TASK_SLEEP_HASHBITS 8
  72. #define KVM_TASK_SLEEP_HASHSIZE (1<<KVM_TASK_SLEEP_HASHBITS)
  73. struct kvm_task_sleep_node {
  74. struct hlist_node link;
  75. struct swait_queue_head wq;
  76. u32 token;
  77. int cpu;
  78. bool halted;
  79. };
  80. static struct kvm_task_sleep_head {
  81. raw_spinlock_t lock;
  82. struct hlist_head list;
  83. } async_pf_sleepers[KVM_TASK_SLEEP_HASHSIZE];
  84. static struct kvm_task_sleep_node *_find_apf_task(struct kvm_task_sleep_head *b,
  85. u32 token)
  86. {
  87. struct hlist_node *p;
  88. hlist_for_each(p, &b->list) {
  89. struct kvm_task_sleep_node *n =
  90. hlist_entry(p, typeof(*n), link);
  91. if (n->token == token)
  92. return n;
  93. }
  94. return NULL;
  95. }
  96. /*
  97. * @interrupt_kernel: Is this called from a routine which interrupts the kernel
  98. * (other than user space)?
  99. */
  100. void kvm_async_pf_task_wait(u32 token, int interrupt_kernel)
  101. {
  102. u32 key = hash_32(token, KVM_TASK_SLEEP_HASHBITS);
  103. struct kvm_task_sleep_head *b = &async_pf_sleepers[key];
  104. struct kvm_task_sleep_node n, *e;
  105. DECLARE_SWAITQUEUE(wait);
  106. rcu_irq_enter();
  107. raw_spin_lock(&b->lock);
  108. e = _find_apf_task(b, token);
  109. if (e) {
  110. /* dummy entry exist -> wake up was delivered ahead of PF */
  111. hlist_del(&e->link);
  112. kfree(e);
  113. raw_spin_unlock(&b->lock);
  114. rcu_irq_exit();
  115. return;
  116. }
  117. n.token = token;
  118. n.cpu = smp_processor_id();
  119. n.halted = is_idle_task(current) ||
  120. (IS_ENABLED(CONFIG_PREEMPT_COUNT)
  121. ? preempt_count() > 1 || rcu_preempt_depth()
  122. : interrupt_kernel);
  123. init_swait_queue_head(&n.wq);
  124. hlist_add_head(&n.link, &b->list);
  125. raw_spin_unlock(&b->lock);
  126. for (;;) {
  127. if (!n.halted)
  128. prepare_to_swait_exclusive(&n.wq, &wait, TASK_UNINTERRUPTIBLE);
  129. if (hlist_unhashed(&n.link))
  130. break;
  131. rcu_irq_exit();
  132. if (!n.halted) {
  133. local_irq_enable();
  134. schedule();
  135. local_irq_disable();
  136. } else {
  137. /*
  138. * We cannot reschedule. So halt.
  139. */
  140. native_safe_halt();
  141. local_irq_disable();
  142. }
  143. rcu_irq_enter();
  144. }
  145. if (!n.halted)
  146. finish_swait(&n.wq, &wait);
  147. rcu_irq_exit();
  148. return;
  149. }
  150. EXPORT_SYMBOL_GPL(kvm_async_pf_task_wait);
  151. static void apf_task_wake_one(struct kvm_task_sleep_node *n)
  152. {
  153. hlist_del_init(&n->link);
  154. if (n->halted)
  155. smp_send_reschedule(n->cpu);
  156. else if (swq_has_sleeper(&n->wq))
  157. swake_up_one(&n->wq);
  158. }
  159. static void apf_task_wake_all(void)
  160. {
  161. int i;
  162. for (i = 0; i < KVM_TASK_SLEEP_HASHSIZE; i++) {
  163. struct hlist_node *p, *next;
  164. struct kvm_task_sleep_head *b = &async_pf_sleepers[i];
  165. raw_spin_lock(&b->lock);
  166. hlist_for_each_safe(p, next, &b->list) {
  167. struct kvm_task_sleep_node *n =
  168. hlist_entry(p, typeof(*n), link);
  169. if (n->cpu == smp_processor_id())
  170. apf_task_wake_one(n);
  171. }
  172. raw_spin_unlock(&b->lock);
  173. }
  174. }
  175. void kvm_async_pf_task_wake(u32 token)
  176. {
  177. u32 key = hash_32(token, KVM_TASK_SLEEP_HASHBITS);
  178. struct kvm_task_sleep_head *b = &async_pf_sleepers[key];
  179. struct kvm_task_sleep_node *n;
  180. if (token == ~0) {
  181. apf_task_wake_all();
  182. return;
  183. }
  184. again:
  185. raw_spin_lock(&b->lock);
  186. n = _find_apf_task(b, token);
  187. if (!n) {
  188. /*
  189. * async PF was not yet handled.
  190. * Add dummy entry for the token.
  191. */
  192. n = kzalloc(sizeof(*n), GFP_ATOMIC);
  193. if (!n) {
  194. /*
  195. * Allocation failed! Busy wait while other cpu
  196. * handles async PF.
  197. */
  198. raw_spin_unlock(&b->lock);
  199. cpu_relax();
  200. goto again;
  201. }
  202. n->token = token;
  203. n->cpu = smp_processor_id();
  204. init_swait_queue_head(&n->wq);
  205. hlist_add_head(&n->link, &b->list);
  206. } else
  207. apf_task_wake_one(n);
  208. raw_spin_unlock(&b->lock);
  209. return;
  210. }
  211. EXPORT_SYMBOL_GPL(kvm_async_pf_task_wake);
  212. u32 kvm_read_and_reset_pf_reason(void)
  213. {
  214. u32 reason = 0;
  215. if (__this_cpu_read(apf_reason.enabled)) {
  216. reason = __this_cpu_read(apf_reason.reason);
  217. __this_cpu_write(apf_reason.reason, 0);
  218. }
  219. return reason;
  220. }
  221. EXPORT_SYMBOL_GPL(kvm_read_and_reset_pf_reason);
  222. NOKPROBE_SYMBOL(kvm_read_and_reset_pf_reason);
  223. dotraplinkage void
  224. do_async_page_fault(struct pt_regs *regs, unsigned long error_code)
  225. {
  226. enum ctx_state prev_state;
  227. switch (kvm_read_and_reset_pf_reason()) {
  228. default:
  229. do_page_fault(regs, error_code);
  230. break;
  231. case KVM_PV_REASON_PAGE_NOT_PRESENT:
  232. /* page is swapped out by the host. */
  233. prev_state = exception_enter();
  234. kvm_async_pf_task_wait((u32)read_cr2(), !user_mode(regs));
  235. exception_exit(prev_state);
  236. break;
  237. case KVM_PV_REASON_PAGE_READY:
  238. rcu_irq_enter();
  239. kvm_async_pf_task_wake((u32)read_cr2());
  240. rcu_irq_exit();
  241. break;
  242. }
  243. }
  244. NOKPROBE_SYMBOL(do_async_page_fault);
  245. static void __init paravirt_ops_setup(void)
  246. {
  247. pv_info.name = "KVM";
  248. if (kvm_para_has_feature(KVM_FEATURE_NOP_IO_DELAY))
  249. pv_ops.cpu.io_delay = kvm_io_delay;
  250. #ifdef CONFIG_X86_IO_APIC
  251. no_timer_check = 1;
  252. #endif
  253. }
  254. static void kvm_register_steal_time(void)
  255. {
  256. int cpu = smp_processor_id();
  257. struct kvm_steal_time *st = &per_cpu(steal_time, cpu);
  258. if (!has_steal_clock)
  259. return;
  260. wrmsrl(MSR_KVM_STEAL_TIME, (slow_virt_to_phys(st) | KVM_MSR_ENABLED));
  261. pr_info("kvm-stealtime: cpu %d, msr %llx\n",
  262. cpu, (unsigned long long) slow_virt_to_phys(st));
  263. }
  264. static DEFINE_PER_CPU_DECRYPTED(unsigned long, kvm_apic_eoi) = KVM_PV_EOI_DISABLED;
  265. static notrace void kvm_guest_apic_eoi_write(u32 reg, u32 val)
  266. {
  267. /**
  268. * This relies on __test_and_clear_bit to modify the memory
  269. * in a way that is atomic with respect to the local CPU.
  270. * The hypervisor only accesses this memory from the local CPU so
  271. * there's no need for lock or memory barriers.
  272. * An optimization barrier is implied in apic write.
  273. */
  274. if (__test_and_clear_bit(KVM_PV_EOI_BIT, this_cpu_ptr(&kvm_apic_eoi)))
  275. return;
  276. apic->native_eoi_write(APIC_EOI, APIC_EOI_ACK);
  277. }
  278. static void kvm_guest_cpu_init(void)
  279. {
  280. if (!kvm_para_available())
  281. return;
  282. if (kvm_para_has_feature(KVM_FEATURE_ASYNC_PF) && kvmapf) {
  283. u64 pa = slow_virt_to_phys(this_cpu_ptr(&apf_reason));
  284. #ifdef CONFIG_PREEMPT
  285. pa |= KVM_ASYNC_PF_SEND_ALWAYS;
  286. #endif
  287. pa |= KVM_ASYNC_PF_ENABLED;
  288. if (kvm_para_has_feature(KVM_FEATURE_ASYNC_PF_VMEXIT))
  289. pa |= KVM_ASYNC_PF_DELIVERY_AS_PF_VMEXIT;
  290. wrmsrl(MSR_KVM_ASYNC_PF_EN, pa);
  291. __this_cpu_write(apf_reason.enabled, 1);
  292. printk(KERN_INFO"KVM setup async PF for cpu %d\n",
  293. smp_processor_id());
  294. }
  295. if (kvm_para_has_feature(KVM_FEATURE_PV_EOI)) {
  296. unsigned long pa;
  297. /* Size alignment is implied but just to make it explicit. */
  298. BUILD_BUG_ON(__alignof__(kvm_apic_eoi) < 4);
  299. __this_cpu_write(kvm_apic_eoi, 0);
  300. pa = slow_virt_to_phys(this_cpu_ptr(&kvm_apic_eoi))
  301. | KVM_MSR_ENABLED;
  302. wrmsrl(MSR_KVM_PV_EOI_EN, pa);
  303. }
  304. if (has_steal_clock)
  305. kvm_register_steal_time();
  306. }
  307. static void kvm_pv_disable_apf(void)
  308. {
  309. if (!__this_cpu_read(apf_reason.enabled))
  310. return;
  311. wrmsrl(MSR_KVM_ASYNC_PF_EN, 0);
  312. __this_cpu_write(apf_reason.enabled, 0);
  313. printk(KERN_INFO"Unregister pv shared memory for cpu %d\n",
  314. smp_processor_id());
  315. }
  316. static void kvm_pv_guest_cpu_reboot(void *unused)
  317. {
  318. /*
  319. * We disable PV EOI before we load a new kernel by kexec,
  320. * since MSR_KVM_PV_EOI_EN stores a pointer into old kernel's memory.
  321. * New kernel can re-enable when it boots.
  322. */
  323. if (kvm_para_has_feature(KVM_FEATURE_PV_EOI))
  324. wrmsrl(MSR_KVM_PV_EOI_EN, 0);
  325. kvm_pv_disable_apf();
  326. kvm_disable_steal_time();
  327. }
  328. static int kvm_pv_reboot_notify(struct notifier_block *nb,
  329. unsigned long code, void *unused)
  330. {
  331. if (code == SYS_RESTART)
  332. on_each_cpu(kvm_pv_guest_cpu_reboot, NULL, 1);
  333. return NOTIFY_DONE;
  334. }
  335. static struct notifier_block kvm_pv_reboot_nb = {
  336. .notifier_call = kvm_pv_reboot_notify,
  337. };
  338. static u64 kvm_steal_clock(int cpu)
  339. {
  340. u64 steal;
  341. struct kvm_steal_time *src;
  342. int version;
  343. src = &per_cpu(steal_time, cpu);
  344. do {
  345. version = src->version;
  346. virt_rmb();
  347. steal = src->steal;
  348. virt_rmb();
  349. } while ((version & 1) || (version != src->version));
  350. return steal;
  351. }
  352. void kvm_disable_steal_time(void)
  353. {
  354. if (!has_steal_clock)
  355. return;
  356. wrmsr(MSR_KVM_STEAL_TIME, 0, 0);
  357. }
  358. static inline void __set_percpu_decrypted(void *ptr, unsigned long size)
  359. {
  360. early_set_memory_decrypted((unsigned long) ptr, size);
  361. }
  362. /*
  363. * Iterate through all possible CPUs and map the memory region pointed
  364. * by apf_reason, steal_time and kvm_apic_eoi as decrypted at once.
  365. *
  366. * Note: we iterate through all possible CPUs to ensure that CPUs
  367. * hotplugged will have their per-cpu variable already mapped as
  368. * decrypted.
  369. */
  370. static void __init sev_map_percpu_data(void)
  371. {
  372. int cpu;
  373. if (!sev_active())
  374. return;
  375. for_each_possible_cpu(cpu) {
  376. __set_percpu_decrypted(&per_cpu(apf_reason, cpu), sizeof(apf_reason));
  377. __set_percpu_decrypted(&per_cpu(steal_time, cpu), sizeof(steal_time));
  378. __set_percpu_decrypted(&per_cpu(kvm_apic_eoi, cpu), sizeof(kvm_apic_eoi));
  379. }
  380. }
  381. #ifdef CONFIG_SMP
  382. #define KVM_IPI_CLUSTER_SIZE (2 * BITS_PER_LONG)
  383. static void __send_ipi_mask(const struct cpumask *mask, int vector)
  384. {
  385. unsigned long flags;
  386. int cpu, apic_id, icr;
  387. int min = 0, max = 0;
  388. #ifdef CONFIG_X86_64
  389. __uint128_t ipi_bitmap = 0;
  390. #else
  391. u64 ipi_bitmap = 0;
  392. #endif
  393. if (cpumask_empty(mask))
  394. return;
  395. local_irq_save(flags);
  396. switch (vector) {
  397. default:
  398. icr = APIC_DM_FIXED | vector;
  399. break;
  400. case NMI_VECTOR:
  401. icr = APIC_DM_NMI;
  402. break;
  403. }
  404. for_each_cpu(cpu, mask) {
  405. apic_id = per_cpu(x86_cpu_to_apicid, cpu);
  406. if (!ipi_bitmap) {
  407. min = max = apic_id;
  408. } else if (apic_id < min && max - apic_id < KVM_IPI_CLUSTER_SIZE) {
  409. ipi_bitmap <<= min - apic_id;
  410. min = apic_id;
  411. } else if (apic_id < min + KVM_IPI_CLUSTER_SIZE) {
  412. max = apic_id < max ? max : apic_id;
  413. } else {
  414. kvm_hypercall4(KVM_HC_SEND_IPI, (unsigned long)ipi_bitmap,
  415. (unsigned long)(ipi_bitmap >> BITS_PER_LONG), min, icr);
  416. min = max = apic_id;
  417. ipi_bitmap = 0;
  418. }
  419. __set_bit(apic_id - min, (unsigned long *)&ipi_bitmap);
  420. }
  421. if (ipi_bitmap) {
  422. kvm_hypercall4(KVM_HC_SEND_IPI, (unsigned long)ipi_bitmap,
  423. (unsigned long)(ipi_bitmap >> BITS_PER_LONG), min, icr);
  424. }
  425. local_irq_restore(flags);
  426. }
  427. static void kvm_send_ipi_mask(const struct cpumask *mask, int vector)
  428. {
  429. __send_ipi_mask(mask, vector);
  430. }
  431. static void kvm_send_ipi_mask_allbutself(const struct cpumask *mask, int vector)
  432. {
  433. unsigned int this_cpu = smp_processor_id();
  434. struct cpumask new_mask;
  435. const struct cpumask *local_mask;
  436. cpumask_copy(&new_mask, mask);
  437. cpumask_clear_cpu(this_cpu, &new_mask);
  438. local_mask = &new_mask;
  439. __send_ipi_mask(local_mask, vector);
  440. }
  441. static void kvm_send_ipi_allbutself(int vector)
  442. {
  443. kvm_send_ipi_mask_allbutself(cpu_online_mask, vector);
  444. }
  445. static void kvm_send_ipi_all(int vector)
  446. {
  447. __send_ipi_mask(cpu_online_mask, vector);
  448. }
  449. /*
  450. * Set the IPI entry points
  451. */
  452. static void kvm_setup_pv_ipi(void)
  453. {
  454. apic->send_IPI_mask = kvm_send_ipi_mask;
  455. apic->send_IPI_mask_allbutself = kvm_send_ipi_mask_allbutself;
  456. apic->send_IPI_allbutself = kvm_send_ipi_allbutself;
  457. apic->send_IPI_all = kvm_send_ipi_all;
  458. pr_info("KVM setup pv IPIs\n");
  459. }
  460. static void __init kvm_smp_prepare_cpus(unsigned int max_cpus)
  461. {
  462. native_smp_prepare_cpus(max_cpus);
  463. if (kvm_para_has_hint(KVM_HINTS_REALTIME))
  464. static_branch_disable(&virt_spin_lock_key);
  465. }
  466. static void __init kvm_smp_prepare_boot_cpu(void)
  467. {
  468. /*
  469. * Map the per-cpu variables as decrypted before kvm_guest_cpu_init()
  470. * shares the guest physical address with the hypervisor.
  471. */
  472. sev_map_percpu_data();
  473. kvm_guest_cpu_init();
  474. native_smp_prepare_boot_cpu();
  475. kvm_spinlock_init();
  476. }
  477. static void kvm_guest_cpu_offline(void)
  478. {
  479. kvm_disable_steal_time();
  480. if (kvm_para_has_feature(KVM_FEATURE_PV_EOI))
  481. wrmsrl(MSR_KVM_PV_EOI_EN, 0);
  482. kvm_pv_disable_apf();
  483. apf_task_wake_all();
  484. }
  485. static int kvm_cpu_online(unsigned int cpu)
  486. {
  487. local_irq_disable();
  488. kvm_guest_cpu_init();
  489. local_irq_enable();
  490. return 0;
  491. }
  492. static int kvm_cpu_down_prepare(unsigned int cpu)
  493. {
  494. local_irq_disable();
  495. kvm_guest_cpu_offline();
  496. local_irq_enable();
  497. return 0;
  498. }
  499. #endif
  500. static void __init kvm_apf_trap_init(void)
  501. {
  502. update_intr_gate(X86_TRAP_PF, async_page_fault);
  503. }
  504. static DEFINE_PER_CPU(cpumask_var_t, __pv_tlb_mask);
  505. static void kvm_flush_tlb_others(const struct cpumask *cpumask,
  506. const struct flush_tlb_info *info)
  507. {
  508. u8 state;
  509. int cpu;
  510. struct kvm_steal_time *src;
  511. struct cpumask *flushmask = this_cpu_cpumask_var_ptr(__pv_tlb_mask);
  512. cpumask_copy(flushmask, cpumask);
  513. /*
  514. * We have to call flush only on online vCPUs. And
  515. * queue flush_on_enter for pre-empted vCPUs
  516. */
  517. for_each_cpu(cpu, flushmask) {
  518. src = &per_cpu(steal_time, cpu);
  519. state = READ_ONCE(src->preempted);
  520. if ((state & KVM_VCPU_PREEMPTED)) {
  521. if (try_cmpxchg(&src->preempted, &state,
  522. state | KVM_VCPU_FLUSH_TLB))
  523. __cpumask_clear_cpu(cpu, flushmask);
  524. }
  525. }
  526. native_flush_tlb_others(flushmask, info);
  527. }
  528. static void __init kvm_guest_init(void)
  529. {
  530. int i;
  531. if (!kvm_para_available())
  532. return;
  533. paravirt_ops_setup();
  534. register_reboot_notifier(&kvm_pv_reboot_nb);
  535. for (i = 0; i < KVM_TASK_SLEEP_HASHSIZE; i++)
  536. raw_spin_lock_init(&async_pf_sleepers[i].lock);
  537. if (kvm_para_has_feature(KVM_FEATURE_ASYNC_PF))
  538. x86_init.irqs.trap_init = kvm_apf_trap_init;
  539. if (kvm_para_has_feature(KVM_FEATURE_STEAL_TIME)) {
  540. has_steal_clock = 1;
  541. pv_ops.time.steal_clock = kvm_steal_clock;
  542. }
  543. if (kvm_para_has_feature(KVM_FEATURE_PV_TLB_FLUSH) &&
  544. !kvm_para_has_hint(KVM_HINTS_REALTIME) &&
  545. kvm_para_has_feature(KVM_FEATURE_STEAL_TIME)) {
  546. pv_ops.mmu.flush_tlb_others = kvm_flush_tlb_others;
  547. pv_ops.mmu.tlb_remove_table = tlb_remove_table;
  548. }
  549. if (kvm_para_has_feature(KVM_FEATURE_PV_EOI))
  550. apic_set_eoi_write(kvm_guest_apic_eoi_write);
  551. #ifdef CONFIG_SMP
  552. smp_ops.smp_prepare_cpus = kvm_smp_prepare_cpus;
  553. smp_ops.smp_prepare_boot_cpu = kvm_smp_prepare_boot_cpu;
  554. if (cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN, "x86/kvm:online",
  555. kvm_cpu_online, kvm_cpu_down_prepare) < 0)
  556. pr_err("kvm_guest: Failed to install cpu hotplug callbacks\n");
  557. #else
  558. sev_map_percpu_data();
  559. kvm_guest_cpu_init();
  560. #endif
  561. /*
  562. * Hard lockup detection is enabled by default. Disable it, as guests
  563. * can get false positives too easily, for example if the host is
  564. * overcommitted.
  565. */
  566. hardlockup_detector_disable();
  567. }
  568. static noinline uint32_t __kvm_cpuid_base(void)
  569. {
  570. if (boot_cpu_data.cpuid_level < 0)
  571. return 0; /* So we don't blow up on old processors */
  572. if (boot_cpu_has(X86_FEATURE_HYPERVISOR))
  573. return hypervisor_cpuid_base("KVMKVMKVM\0\0\0", 0);
  574. return 0;
  575. }
  576. static inline uint32_t kvm_cpuid_base(void)
  577. {
  578. static int kvm_cpuid_base = -1;
  579. if (kvm_cpuid_base == -1)
  580. kvm_cpuid_base = __kvm_cpuid_base();
  581. return kvm_cpuid_base;
  582. }
  583. bool kvm_para_available(void)
  584. {
  585. return kvm_cpuid_base() != 0;
  586. }
  587. EXPORT_SYMBOL_GPL(kvm_para_available);
  588. unsigned int kvm_arch_para_features(void)
  589. {
  590. return cpuid_eax(kvm_cpuid_base() | KVM_CPUID_FEATURES);
  591. }
  592. unsigned int kvm_arch_para_hints(void)
  593. {
  594. return cpuid_edx(kvm_cpuid_base() | KVM_CPUID_FEATURES);
  595. }
  596. static uint32_t __init kvm_detect(void)
  597. {
  598. return kvm_cpuid_base();
  599. }
  600. static void __init kvm_apic_init(void)
  601. {
  602. #if defined(CONFIG_SMP)
  603. if (kvm_para_has_feature(KVM_FEATURE_PV_SEND_IPI))
  604. kvm_setup_pv_ipi();
  605. #endif
  606. }
  607. static void __init kvm_init_platform(void)
  608. {
  609. kvmclock_init();
  610. x86_platform.apic_post_init = kvm_apic_init;
  611. }
  612. const __initconst struct hypervisor_x86 x86_hyper_kvm = {
  613. .name = "KVM",
  614. .detect = kvm_detect,
  615. .type = X86_HYPER_KVM,
  616. .init.guest_late_init = kvm_guest_init,
  617. .init.x2apic_available = kvm_para_available,
  618. .init.init_platform = kvm_init_platform,
  619. };
  620. static __init int activate_jump_labels(void)
  621. {
  622. if (has_steal_clock) {
  623. static_key_slow_inc(&paravirt_steal_enabled);
  624. if (steal_acc)
  625. static_key_slow_inc(&paravirt_steal_rq_enabled);
  626. }
  627. return 0;
  628. }
  629. arch_initcall(activate_jump_labels);
  630. static __init int kvm_setup_pv_tlb_flush(void)
  631. {
  632. int cpu;
  633. if (kvm_para_has_feature(KVM_FEATURE_PV_TLB_FLUSH) &&
  634. !kvm_para_has_hint(KVM_HINTS_REALTIME) &&
  635. kvm_para_has_feature(KVM_FEATURE_STEAL_TIME)) {
  636. for_each_possible_cpu(cpu) {
  637. zalloc_cpumask_var_node(per_cpu_ptr(&__pv_tlb_mask, cpu),
  638. GFP_KERNEL, cpu_to_node(cpu));
  639. }
  640. pr_info("KVM setup pv remote TLB flush\n");
  641. }
  642. return 0;
  643. }
  644. arch_initcall(kvm_setup_pv_tlb_flush);
  645. #ifdef CONFIG_PARAVIRT_SPINLOCKS
  646. /* Kick a cpu by its apicid. Used to wake up a halted vcpu */
  647. static void kvm_kick_cpu(int cpu)
  648. {
  649. int apicid;
  650. unsigned long flags = 0;
  651. apicid = per_cpu(x86_cpu_to_apicid, cpu);
  652. kvm_hypercall2(KVM_HC_KICK_CPU, flags, apicid);
  653. }
  654. #include <asm/qspinlock.h>
  655. static void kvm_wait(u8 *ptr, u8 val)
  656. {
  657. unsigned long flags;
  658. if (in_nmi())
  659. return;
  660. local_irq_save(flags);
  661. if (READ_ONCE(*ptr) != val)
  662. goto out;
  663. /*
  664. * halt until it's our turn and kicked. Note that we do safe halt
  665. * for irq enabled case to avoid hang when lock info is overwritten
  666. * in irq spinlock slowpath and no spurious interrupt occur to save us.
  667. */
  668. if (arch_irqs_disabled_flags(flags))
  669. halt();
  670. else
  671. safe_halt();
  672. out:
  673. local_irq_restore(flags);
  674. }
  675. #ifdef CONFIG_X86_32
  676. __visible bool __kvm_vcpu_is_preempted(long cpu)
  677. {
  678. struct kvm_steal_time *src = &per_cpu(steal_time, cpu);
  679. return !!(src->preempted & KVM_VCPU_PREEMPTED);
  680. }
  681. PV_CALLEE_SAVE_REGS_THUNK(__kvm_vcpu_is_preempted);
  682. #else
  683. #include <asm/asm-offsets.h>
  684. extern bool __raw_callee_save___kvm_vcpu_is_preempted(long);
  685. /*
  686. * Hand-optimize version for x86-64 to avoid 8 64-bit register saving and
  687. * restoring to/from the stack.
  688. */
  689. asm(
  690. ".pushsection .text;"
  691. ".global __raw_callee_save___kvm_vcpu_is_preempted;"
  692. ".type __raw_callee_save___kvm_vcpu_is_preempted, @function;"
  693. "__raw_callee_save___kvm_vcpu_is_preempted:"
  694. "movq __per_cpu_offset(,%rdi,8), %rax;"
  695. "cmpb $0, " __stringify(KVM_STEAL_TIME_preempted) "+steal_time(%rax);"
  696. "setne %al;"
  697. "ret;"
  698. ".popsection");
  699. #endif
  700. /*
  701. * Setup pv_lock_ops to exploit KVM_FEATURE_PV_UNHALT if present.
  702. */
  703. void __init kvm_spinlock_init(void)
  704. {
  705. if (!kvm_para_available())
  706. return;
  707. /* Does host kernel support KVM_FEATURE_PV_UNHALT? */
  708. if (!kvm_para_has_feature(KVM_FEATURE_PV_UNHALT))
  709. return;
  710. if (kvm_para_has_hint(KVM_HINTS_REALTIME))
  711. return;
  712. /* Don't use the pvqspinlock code if there is only 1 vCPU. */
  713. if (num_possible_cpus() == 1)
  714. return;
  715. __pv_init_lock_hash();
  716. pv_ops.lock.queued_spin_lock_slowpath = __pv_queued_spin_lock_slowpath;
  717. pv_ops.lock.queued_spin_unlock =
  718. PV_CALLEE_SAVE(__pv_queued_spin_unlock);
  719. pv_ops.lock.wait = kvm_wait;
  720. pv_ops.lock.kick = kvm_kick_cpu;
  721. if (kvm_para_has_feature(KVM_FEATURE_STEAL_TIME)) {
  722. pv_ops.lock.vcpu_is_preempted =
  723. PV_CALLEE_SAVE(__kvm_vcpu_is_preempted);
  724. }
  725. }
  726. #endif /* CONFIG_PARAVIRT_SPINLOCKS */