kvm.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763
  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/kvm_guest.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 int kvmclock_vsyscall = 1;
  63. static int __init parse_no_kvmclock_vsyscall(char *arg)
  64. {
  65. kvmclock_vsyscall = 0;
  66. return 0;
  67. }
  68. early_param("no-kvmclock-vsyscall", parse_no_kvmclock_vsyscall);
  69. static DEFINE_PER_CPU_DECRYPTED(struct kvm_vcpu_pv_apf_data, apf_reason) __aligned(64);
  70. static DEFINE_PER_CPU_DECRYPTED(struct kvm_steal_time, steal_time) __aligned(64);
  71. static int has_steal_clock = 0;
  72. /*
  73. * No need for any "IO delay" on KVM
  74. */
  75. static void kvm_io_delay(void)
  76. {
  77. }
  78. #define KVM_TASK_SLEEP_HASHBITS 8
  79. #define KVM_TASK_SLEEP_HASHSIZE (1<<KVM_TASK_SLEEP_HASHBITS)
  80. struct kvm_task_sleep_node {
  81. struct hlist_node link;
  82. struct swait_queue_head wq;
  83. u32 token;
  84. int cpu;
  85. bool halted;
  86. };
  87. static struct kvm_task_sleep_head {
  88. raw_spinlock_t lock;
  89. struct hlist_head list;
  90. } async_pf_sleepers[KVM_TASK_SLEEP_HASHSIZE];
  91. static struct kvm_task_sleep_node *_find_apf_task(struct kvm_task_sleep_head *b,
  92. u32 token)
  93. {
  94. struct hlist_node *p;
  95. hlist_for_each(p, &b->list) {
  96. struct kvm_task_sleep_node *n =
  97. hlist_entry(p, typeof(*n), link);
  98. if (n->token == token)
  99. return n;
  100. }
  101. return NULL;
  102. }
  103. /*
  104. * @interrupt_kernel: Is this called from a routine which interrupts the kernel
  105. * (other than user space)?
  106. */
  107. void kvm_async_pf_task_wait(u32 token, int interrupt_kernel)
  108. {
  109. u32 key = hash_32(token, KVM_TASK_SLEEP_HASHBITS);
  110. struct kvm_task_sleep_head *b = &async_pf_sleepers[key];
  111. struct kvm_task_sleep_node n, *e;
  112. DECLARE_SWAITQUEUE(wait);
  113. rcu_irq_enter();
  114. raw_spin_lock(&b->lock);
  115. e = _find_apf_task(b, token);
  116. if (e) {
  117. /* dummy entry exist -> wake up was delivered ahead of PF */
  118. hlist_del(&e->link);
  119. kfree(e);
  120. raw_spin_unlock(&b->lock);
  121. rcu_irq_exit();
  122. return;
  123. }
  124. n.token = token;
  125. n.cpu = smp_processor_id();
  126. n.halted = is_idle_task(current) ||
  127. (IS_ENABLED(CONFIG_PREEMPT_COUNT)
  128. ? preempt_count() > 1 || rcu_preempt_depth()
  129. : interrupt_kernel);
  130. init_swait_queue_head(&n.wq);
  131. hlist_add_head(&n.link, &b->list);
  132. raw_spin_unlock(&b->lock);
  133. for (;;) {
  134. if (!n.halted)
  135. prepare_to_swait_exclusive(&n.wq, &wait, TASK_UNINTERRUPTIBLE);
  136. if (hlist_unhashed(&n.link))
  137. break;
  138. rcu_irq_exit();
  139. if (!n.halted) {
  140. local_irq_enable();
  141. schedule();
  142. local_irq_disable();
  143. } else {
  144. /*
  145. * We cannot reschedule. So halt.
  146. */
  147. native_safe_halt();
  148. local_irq_disable();
  149. }
  150. rcu_irq_enter();
  151. }
  152. if (!n.halted)
  153. finish_swait(&n.wq, &wait);
  154. rcu_irq_exit();
  155. return;
  156. }
  157. EXPORT_SYMBOL_GPL(kvm_async_pf_task_wait);
  158. static void apf_task_wake_one(struct kvm_task_sleep_node *n)
  159. {
  160. hlist_del_init(&n->link);
  161. if (n->halted)
  162. smp_send_reschedule(n->cpu);
  163. else if (swq_has_sleeper(&n->wq))
  164. swake_up_one(&n->wq);
  165. }
  166. static void apf_task_wake_all(void)
  167. {
  168. int i;
  169. for (i = 0; i < KVM_TASK_SLEEP_HASHSIZE; i++) {
  170. struct hlist_node *p, *next;
  171. struct kvm_task_sleep_head *b = &async_pf_sleepers[i];
  172. raw_spin_lock(&b->lock);
  173. hlist_for_each_safe(p, next, &b->list) {
  174. struct kvm_task_sleep_node *n =
  175. hlist_entry(p, typeof(*n), link);
  176. if (n->cpu == smp_processor_id())
  177. apf_task_wake_one(n);
  178. }
  179. raw_spin_unlock(&b->lock);
  180. }
  181. }
  182. void kvm_async_pf_task_wake(u32 token)
  183. {
  184. u32 key = hash_32(token, KVM_TASK_SLEEP_HASHBITS);
  185. struct kvm_task_sleep_head *b = &async_pf_sleepers[key];
  186. struct kvm_task_sleep_node *n;
  187. if (token == ~0) {
  188. apf_task_wake_all();
  189. return;
  190. }
  191. again:
  192. raw_spin_lock(&b->lock);
  193. n = _find_apf_task(b, token);
  194. if (!n) {
  195. /*
  196. * async PF was not yet handled.
  197. * Add dummy entry for the token.
  198. */
  199. n = kzalloc(sizeof(*n), GFP_ATOMIC);
  200. if (!n) {
  201. /*
  202. * Allocation failed! Busy wait while other cpu
  203. * handles async PF.
  204. */
  205. raw_spin_unlock(&b->lock);
  206. cpu_relax();
  207. goto again;
  208. }
  209. n->token = token;
  210. n->cpu = smp_processor_id();
  211. init_swait_queue_head(&n->wq);
  212. hlist_add_head(&n->link, &b->list);
  213. } else
  214. apf_task_wake_one(n);
  215. raw_spin_unlock(&b->lock);
  216. return;
  217. }
  218. EXPORT_SYMBOL_GPL(kvm_async_pf_task_wake);
  219. u32 kvm_read_and_reset_pf_reason(void)
  220. {
  221. u32 reason = 0;
  222. if (__this_cpu_read(apf_reason.enabled)) {
  223. reason = __this_cpu_read(apf_reason.reason);
  224. __this_cpu_write(apf_reason.reason, 0);
  225. }
  226. return reason;
  227. }
  228. EXPORT_SYMBOL_GPL(kvm_read_and_reset_pf_reason);
  229. NOKPROBE_SYMBOL(kvm_read_and_reset_pf_reason);
  230. dotraplinkage void
  231. do_async_page_fault(struct pt_regs *regs, unsigned long error_code)
  232. {
  233. enum ctx_state prev_state;
  234. switch (kvm_read_and_reset_pf_reason()) {
  235. default:
  236. do_page_fault(regs, error_code);
  237. break;
  238. case KVM_PV_REASON_PAGE_NOT_PRESENT:
  239. /* page is swapped out by the host. */
  240. prev_state = exception_enter();
  241. kvm_async_pf_task_wait((u32)read_cr2(), !user_mode(regs));
  242. exception_exit(prev_state);
  243. break;
  244. case KVM_PV_REASON_PAGE_READY:
  245. rcu_irq_enter();
  246. kvm_async_pf_task_wake((u32)read_cr2());
  247. rcu_irq_exit();
  248. break;
  249. }
  250. }
  251. NOKPROBE_SYMBOL(do_async_page_fault);
  252. static void __init paravirt_ops_setup(void)
  253. {
  254. pv_info.name = "KVM";
  255. if (kvm_para_has_feature(KVM_FEATURE_NOP_IO_DELAY))
  256. pv_cpu_ops.io_delay = kvm_io_delay;
  257. #ifdef CONFIG_X86_IO_APIC
  258. no_timer_check = 1;
  259. #endif
  260. }
  261. static void kvm_register_steal_time(void)
  262. {
  263. int cpu = smp_processor_id();
  264. struct kvm_steal_time *st = &per_cpu(steal_time, cpu);
  265. if (!has_steal_clock)
  266. return;
  267. wrmsrl(MSR_KVM_STEAL_TIME, (slow_virt_to_phys(st) | KVM_MSR_ENABLED));
  268. pr_info("kvm-stealtime: cpu %d, msr %llx\n",
  269. cpu, (unsigned long long) slow_virt_to_phys(st));
  270. }
  271. static DEFINE_PER_CPU_DECRYPTED(unsigned long, kvm_apic_eoi) = KVM_PV_EOI_DISABLED;
  272. static notrace void kvm_guest_apic_eoi_write(u32 reg, u32 val)
  273. {
  274. /**
  275. * This relies on __test_and_clear_bit to modify the memory
  276. * in a way that is atomic with respect to the local CPU.
  277. * The hypervisor only accesses this memory from the local CPU so
  278. * there's no need for lock or memory barriers.
  279. * An optimization barrier is implied in apic write.
  280. */
  281. if (__test_and_clear_bit(KVM_PV_EOI_BIT, this_cpu_ptr(&kvm_apic_eoi)))
  282. return;
  283. apic->native_eoi_write(APIC_EOI, APIC_EOI_ACK);
  284. }
  285. static void kvm_guest_cpu_init(void)
  286. {
  287. if (!kvm_para_available())
  288. return;
  289. if (kvm_para_has_feature(KVM_FEATURE_ASYNC_PF) && kvmapf) {
  290. u64 pa = slow_virt_to_phys(this_cpu_ptr(&apf_reason));
  291. #ifdef CONFIG_PREEMPT
  292. pa |= KVM_ASYNC_PF_SEND_ALWAYS;
  293. #endif
  294. pa |= KVM_ASYNC_PF_ENABLED;
  295. if (kvm_para_has_feature(KVM_FEATURE_ASYNC_PF_VMEXIT))
  296. pa |= KVM_ASYNC_PF_DELIVERY_AS_PF_VMEXIT;
  297. wrmsrl(MSR_KVM_ASYNC_PF_EN, pa);
  298. __this_cpu_write(apf_reason.enabled, 1);
  299. printk(KERN_INFO"KVM setup async PF for cpu %d\n",
  300. smp_processor_id());
  301. }
  302. if (kvm_para_has_feature(KVM_FEATURE_PV_EOI)) {
  303. unsigned long pa;
  304. /* Size alignment is implied but just to make it explicit. */
  305. BUILD_BUG_ON(__alignof__(kvm_apic_eoi) < 4);
  306. __this_cpu_write(kvm_apic_eoi, 0);
  307. pa = slow_virt_to_phys(this_cpu_ptr(&kvm_apic_eoi))
  308. | KVM_MSR_ENABLED;
  309. wrmsrl(MSR_KVM_PV_EOI_EN, pa);
  310. }
  311. if (has_steal_clock)
  312. kvm_register_steal_time();
  313. }
  314. static void kvm_pv_disable_apf(void)
  315. {
  316. if (!__this_cpu_read(apf_reason.enabled))
  317. return;
  318. wrmsrl(MSR_KVM_ASYNC_PF_EN, 0);
  319. __this_cpu_write(apf_reason.enabled, 0);
  320. printk(KERN_INFO"Unregister pv shared memory for cpu %d\n",
  321. smp_processor_id());
  322. }
  323. static void kvm_pv_guest_cpu_reboot(void *unused)
  324. {
  325. /*
  326. * We disable PV EOI before we load a new kernel by kexec,
  327. * since MSR_KVM_PV_EOI_EN stores a pointer into old kernel's memory.
  328. * New kernel can re-enable when it boots.
  329. */
  330. if (kvm_para_has_feature(KVM_FEATURE_PV_EOI))
  331. wrmsrl(MSR_KVM_PV_EOI_EN, 0);
  332. kvm_pv_disable_apf();
  333. kvm_disable_steal_time();
  334. }
  335. static int kvm_pv_reboot_notify(struct notifier_block *nb,
  336. unsigned long code, void *unused)
  337. {
  338. if (code == SYS_RESTART)
  339. on_each_cpu(kvm_pv_guest_cpu_reboot, NULL, 1);
  340. return NOTIFY_DONE;
  341. }
  342. static struct notifier_block kvm_pv_reboot_nb = {
  343. .notifier_call = kvm_pv_reboot_notify,
  344. };
  345. static u64 kvm_steal_clock(int cpu)
  346. {
  347. u64 steal;
  348. struct kvm_steal_time *src;
  349. int version;
  350. src = &per_cpu(steal_time, cpu);
  351. do {
  352. version = src->version;
  353. virt_rmb();
  354. steal = src->steal;
  355. virt_rmb();
  356. } while ((version & 1) || (version != src->version));
  357. return steal;
  358. }
  359. void kvm_disable_steal_time(void)
  360. {
  361. if (!has_steal_clock)
  362. return;
  363. wrmsr(MSR_KVM_STEAL_TIME, 0, 0);
  364. }
  365. static inline void __set_percpu_decrypted(void *ptr, unsigned long size)
  366. {
  367. early_set_memory_decrypted((unsigned long) ptr, size);
  368. }
  369. /*
  370. * Iterate through all possible CPUs and map the memory region pointed
  371. * by apf_reason, steal_time and kvm_apic_eoi as decrypted at once.
  372. *
  373. * Note: we iterate through all possible CPUs to ensure that CPUs
  374. * hotplugged will have their per-cpu variable already mapped as
  375. * decrypted.
  376. */
  377. static void __init sev_map_percpu_data(void)
  378. {
  379. int cpu;
  380. if (!sev_active())
  381. return;
  382. for_each_possible_cpu(cpu) {
  383. __set_percpu_decrypted(&per_cpu(apf_reason, cpu), sizeof(apf_reason));
  384. __set_percpu_decrypted(&per_cpu(steal_time, cpu), sizeof(steal_time));
  385. __set_percpu_decrypted(&per_cpu(kvm_apic_eoi, cpu), sizeof(kvm_apic_eoi));
  386. }
  387. }
  388. #ifdef CONFIG_SMP
  389. static void __init kvm_smp_prepare_cpus(unsigned int max_cpus)
  390. {
  391. native_smp_prepare_cpus(max_cpus);
  392. if (kvm_para_has_hint(KVM_HINTS_REALTIME))
  393. static_branch_disable(&virt_spin_lock_key);
  394. }
  395. static void __init kvm_smp_prepare_boot_cpu(void)
  396. {
  397. /*
  398. * Map the per-cpu variables as decrypted before kvm_guest_cpu_init()
  399. * shares the guest physical address with the hypervisor.
  400. */
  401. sev_map_percpu_data();
  402. kvm_guest_cpu_init();
  403. native_smp_prepare_boot_cpu();
  404. kvm_spinlock_init();
  405. }
  406. static void kvm_guest_cpu_offline(void)
  407. {
  408. kvm_disable_steal_time();
  409. if (kvm_para_has_feature(KVM_FEATURE_PV_EOI))
  410. wrmsrl(MSR_KVM_PV_EOI_EN, 0);
  411. kvm_pv_disable_apf();
  412. apf_task_wake_all();
  413. }
  414. static int kvm_cpu_online(unsigned int cpu)
  415. {
  416. local_irq_disable();
  417. kvm_guest_cpu_init();
  418. local_irq_enable();
  419. return 0;
  420. }
  421. static int kvm_cpu_down_prepare(unsigned int cpu)
  422. {
  423. local_irq_disable();
  424. kvm_guest_cpu_offline();
  425. local_irq_enable();
  426. return 0;
  427. }
  428. #endif
  429. static void __init kvm_apf_trap_init(void)
  430. {
  431. update_intr_gate(X86_TRAP_PF, async_page_fault);
  432. }
  433. static DEFINE_PER_CPU(cpumask_var_t, __pv_tlb_mask);
  434. static void kvm_flush_tlb_others(const struct cpumask *cpumask,
  435. const struct flush_tlb_info *info)
  436. {
  437. u8 state;
  438. int cpu;
  439. struct kvm_steal_time *src;
  440. struct cpumask *flushmask = this_cpu_cpumask_var_ptr(__pv_tlb_mask);
  441. cpumask_copy(flushmask, cpumask);
  442. /*
  443. * We have to call flush only on online vCPUs. And
  444. * queue flush_on_enter for pre-empted vCPUs
  445. */
  446. for_each_cpu(cpu, flushmask) {
  447. src = &per_cpu(steal_time, cpu);
  448. state = READ_ONCE(src->preempted);
  449. if ((state & KVM_VCPU_PREEMPTED)) {
  450. if (try_cmpxchg(&src->preempted, &state,
  451. state | KVM_VCPU_FLUSH_TLB))
  452. __cpumask_clear_cpu(cpu, flushmask);
  453. }
  454. }
  455. native_flush_tlb_others(flushmask, info);
  456. }
  457. static void __init kvm_guest_init(void)
  458. {
  459. int i;
  460. if (!kvm_para_available())
  461. return;
  462. paravirt_ops_setup();
  463. register_reboot_notifier(&kvm_pv_reboot_nb);
  464. for (i = 0; i < KVM_TASK_SLEEP_HASHSIZE; i++)
  465. raw_spin_lock_init(&async_pf_sleepers[i].lock);
  466. if (kvm_para_has_feature(KVM_FEATURE_ASYNC_PF))
  467. x86_init.irqs.trap_init = kvm_apf_trap_init;
  468. if (kvm_para_has_feature(KVM_FEATURE_STEAL_TIME)) {
  469. has_steal_clock = 1;
  470. pv_time_ops.steal_clock = kvm_steal_clock;
  471. }
  472. if (kvm_para_has_feature(KVM_FEATURE_PV_TLB_FLUSH) &&
  473. !kvm_para_has_hint(KVM_HINTS_REALTIME) &&
  474. kvm_para_has_feature(KVM_FEATURE_STEAL_TIME))
  475. pv_mmu_ops.flush_tlb_others = kvm_flush_tlb_others;
  476. if (kvm_para_has_feature(KVM_FEATURE_PV_EOI))
  477. apic_set_eoi_write(kvm_guest_apic_eoi_write);
  478. if (kvmclock_vsyscall)
  479. kvm_setup_vsyscall_timeinfo();
  480. #ifdef CONFIG_SMP
  481. smp_ops.smp_prepare_cpus = kvm_smp_prepare_cpus;
  482. smp_ops.smp_prepare_boot_cpu = kvm_smp_prepare_boot_cpu;
  483. if (cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN, "x86/kvm:online",
  484. kvm_cpu_online, kvm_cpu_down_prepare) < 0)
  485. pr_err("kvm_guest: Failed to install cpu hotplug callbacks\n");
  486. #else
  487. sev_map_percpu_data();
  488. kvm_guest_cpu_init();
  489. #endif
  490. /*
  491. * Hard lockup detection is enabled by default. Disable it, as guests
  492. * can get false positives too easily, for example if the host is
  493. * overcommitted.
  494. */
  495. hardlockup_detector_disable();
  496. }
  497. static noinline uint32_t __kvm_cpuid_base(void)
  498. {
  499. if (boot_cpu_data.cpuid_level < 0)
  500. return 0; /* So we don't blow up on old processors */
  501. if (boot_cpu_has(X86_FEATURE_HYPERVISOR))
  502. return hypervisor_cpuid_base("KVMKVMKVM\0\0\0", 0);
  503. return 0;
  504. }
  505. static inline uint32_t kvm_cpuid_base(void)
  506. {
  507. static int kvm_cpuid_base = -1;
  508. if (kvm_cpuid_base == -1)
  509. kvm_cpuid_base = __kvm_cpuid_base();
  510. return kvm_cpuid_base;
  511. }
  512. bool kvm_para_available(void)
  513. {
  514. return kvm_cpuid_base() != 0;
  515. }
  516. EXPORT_SYMBOL_GPL(kvm_para_available);
  517. unsigned int kvm_arch_para_features(void)
  518. {
  519. return cpuid_eax(kvm_cpuid_base() | KVM_CPUID_FEATURES);
  520. }
  521. unsigned int kvm_arch_para_hints(void)
  522. {
  523. return cpuid_edx(kvm_cpuid_base() | KVM_CPUID_FEATURES);
  524. }
  525. static uint32_t __init kvm_detect(void)
  526. {
  527. return kvm_cpuid_base();
  528. }
  529. const __initconst struct hypervisor_x86 x86_hyper_kvm = {
  530. .name = "KVM",
  531. .detect = kvm_detect,
  532. .type = X86_HYPER_KVM,
  533. .init.guest_late_init = kvm_guest_init,
  534. .init.x2apic_available = kvm_para_available,
  535. };
  536. static __init int activate_jump_labels(void)
  537. {
  538. if (has_steal_clock) {
  539. static_key_slow_inc(&paravirt_steal_enabled);
  540. if (steal_acc)
  541. static_key_slow_inc(&paravirt_steal_rq_enabled);
  542. }
  543. return 0;
  544. }
  545. arch_initcall(activate_jump_labels);
  546. static __init int kvm_setup_pv_tlb_flush(void)
  547. {
  548. int cpu;
  549. if (kvm_para_has_feature(KVM_FEATURE_PV_TLB_FLUSH) &&
  550. !kvm_para_has_hint(KVM_HINTS_REALTIME) &&
  551. kvm_para_has_feature(KVM_FEATURE_STEAL_TIME)) {
  552. for_each_possible_cpu(cpu) {
  553. zalloc_cpumask_var_node(per_cpu_ptr(&__pv_tlb_mask, cpu),
  554. GFP_KERNEL, cpu_to_node(cpu));
  555. }
  556. pr_info("KVM setup pv remote TLB flush\n");
  557. }
  558. return 0;
  559. }
  560. arch_initcall(kvm_setup_pv_tlb_flush);
  561. #ifdef CONFIG_PARAVIRT_SPINLOCKS
  562. /* Kick a cpu by its apicid. Used to wake up a halted vcpu */
  563. static void kvm_kick_cpu(int cpu)
  564. {
  565. int apicid;
  566. unsigned long flags = 0;
  567. apicid = per_cpu(x86_cpu_to_apicid, cpu);
  568. kvm_hypercall2(KVM_HC_KICK_CPU, flags, apicid);
  569. }
  570. #include <asm/qspinlock.h>
  571. static void kvm_wait(u8 *ptr, u8 val)
  572. {
  573. unsigned long flags;
  574. if (in_nmi())
  575. return;
  576. local_irq_save(flags);
  577. if (READ_ONCE(*ptr) != val)
  578. goto out;
  579. /*
  580. * halt until it's our turn and kicked. Note that we do safe halt
  581. * for irq enabled case to avoid hang when lock info is overwritten
  582. * in irq spinlock slowpath and no spurious interrupt occur to save us.
  583. */
  584. if (arch_irqs_disabled_flags(flags))
  585. halt();
  586. else
  587. safe_halt();
  588. out:
  589. local_irq_restore(flags);
  590. }
  591. #ifdef CONFIG_X86_32
  592. __visible bool __kvm_vcpu_is_preempted(long cpu)
  593. {
  594. struct kvm_steal_time *src = &per_cpu(steal_time, cpu);
  595. return !!(src->preempted & KVM_VCPU_PREEMPTED);
  596. }
  597. PV_CALLEE_SAVE_REGS_THUNK(__kvm_vcpu_is_preempted);
  598. #else
  599. #include <asm/asm-offsets.h>
  600. extern bool __raw_callee_save___kvm_vcpu_is_preempted(long);
  601. /*
  602. * Hand-optimize version for x86-64 to avoid 8 64-bit register saving and
  603. * restoring to/from the stack.
  604. */
  605. asm(
  606. ".pushsection .text;"
  607. ".global __raw_callee_save___kvm_vcpu_is_preempted;"
  608. ".type __raw_callee_save___kvm_vcpu_is_preempted, @function;"
  609. "__raw_callee_save___kvm_vcpu_is_preempted:"
  610. "movq __per_cpu_offset(,%rdi,8), %rax;"
  611. "cmpb $0, " __stringify(KVM_STEAL_TIME_preempted) "+steal_time(%rax);"
  612. "setne %al;"
  613. "ret;"
  614. ".popsection");
  615. #endif
  616. /*
  617. * Setup pv_lock_ops to exploit KVM_FEATURE_PV_UNHALT if present.
  618. */
  619. void __init kvm_spinlock_init(void)
  620. {
  621. if (!kvm_para_available())
  622. return;
  623. /* Does host kernel support KVM_FEATURE_PV_UNHALT? */
  624. if (!kvm_para_has_feature(KVM_FEATURE_PV_UNHALT))
  625. return;
  626. if (kvm_para_has_hint(KVM_HINTS_REALTIME))
  627. return;
  628. __pv_init_lock_hash();
  629. pv_lock_ops.queued_spin_lock_slowpath = __pv_queued_spin_lock_slowpath;
  630. pv_lock_ops.queued_spin_unlock = PV_CALLEE_SAVE(__pv_queued_spin_unlock);
  631. pv_lock_ops.wait = kvm_wait;
  632. pv_lock_ops.kick = kvm_kick_cpu;
  633. if (kvm_para_has_feature(KVM_FEATURE_STEAL_TIME)) {
  634. pv_lock_ops.vcpu_is_preempted =
  635. PV_CALLEE_SAVE(__kvm_vcpu_is_preempted);
  636. }
  637. }
  638. #endif /* CONFIG_PARAVIRT_SPINLOCKS */