kvm.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * KVM paravirt_ops implementation
  4. *
  5. * Copyright (C) 2007, Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
  6. * Copyright IBM Corporation, 2007
  7. * Authors: Anthony Liguori <aliguori@us.ibm.com>
  8. */
  9. #define pr_fmt(fmt) "kvm-guest: " fmt
  10. #include <linux/context_tracking.h>
  11. #include <linux/init.h>
  12. #include <linux/irq.h>
  13. #include <linux/kernel.h>
  14. #include <linux/kvm_para.h>
  15. #include <linux/cpu.h>
  16. #include <linux/mm.h>
  17. #include <linux/highmem.h>
  18. #include <linux/hardirq.h>
  19. #include <linux/notifier.h>
  20. #include <linux/reboot.h>
  21. #include <linux/hash.h>
  22. #include <linux/sched.h>
  23. #include <linux/slab.h>
  24. #include <linux/kprobes.h>
  25. #include <linux/nmi.h>
  26. #include <linux/swait.h>
  27. #include <linux/syscore_ops.h>
  28. #include <asm/timer.h>
  29. #include <asm/cpu.h>
  30. #include <asm/traps.h>
  31. #include <asm/desc.h>
  32. #include <asm/tlbflush.h>
  33. #include <asm/apic.h>
  34. #include <asm/apicdef.h>
  35. #include <asm/hypervisor.h>
  36. #include <asm/tlb.h>
  37. #include <asm/cpuidle_haltpoll.h>
  38. #include <asm/ptrace.h>
  39. #include <asm/reboot.h>
  40. #include <asm/svm.h>
  41. DEFINE_STATIC_KEY_FALSE(kvm_async_pf_enabled);
  42. static int kvmapf = 1;
  43. static int __init parse_no_kvmapf(char *arg)
  44. {
  45. kvmapf = 0;
  46. return 0;
  47. }
  48. early_param("no-kvmapf", parse_no_kvmapf);
  49. static int steal_acc = 1;
  50. static int __init parse_no_stealacc(char *arg)
  51. {
  52. steal_acc = 0;
  53. return 0;
  54. }
  55. early_param("no-steal-acc", parse_no_stealacc);
  56. static DEFINE_PER_CPU_DECRYPTED(struct kvm_vcpu_pv_apf_data, apf_reason) __aligned(64);
  57. DEFINE_PER_CPU_DECRYPTED(struct kvm_steal_time, steal_time) __aligned(64) __visible;
  58. static int has_steal_clock = 0;
  59. /*
  60. * No need for any "IO delay" on KVM
  61. */
  62. static void kvm_io_delay(void)
  63. {
  64. }
  65. #define KVM_TASK_SLEEP_HASHBITS 8
  66. #define KVM_TASK_SLEEP_HASHSIZE (1<<KVM_TASK_SLEEP_HASHBITS)
  67. struct kvm_task_sleep_node {
  68. struct hlist_node link;
  69. struct swait_queue_head wq;
  70. u32 token;
  71. int cpu;
  72. };
  73. static struct kvm_task_sleep_head {
  74. raw_spinlock_t lock;
  75. struct hlist_head list;
  76. } async_pf_sleepers[KVM_TASK_SLEEP_HASHSIZE];
  77. static struct kvm_task_sleep_node *_find_apf_task(struct kvm_task_sleep_head *b,
  78. u32 token)
  79. {
  80. struct hlist_node *p;
  81. hlist_for_each(p, &b->list) {
  82. struct kvm_task_sleep_node *n =
  83. hlist_entry(p, typeof(*n), link);
  84. if (n->token == token)
  85. return n;
  86. }
  87. return NULL;
  88. }
  89. static bool kvm_async_pf_queue_task(u32 token, struct kvm_task_sleep_node *n)
  90. {
  91. u32 key = hash_32(token, KVM_TASK_SLEEP_HASHBITS);
  92. struct kvm_task_sleep_head *b = &async_pf_sleepers[key];
  93. struct kvm_task_sleep_node *e;
  94. raw_spin_lock(&b->lock);
  95. e = _find_apf_task(b, token);
  96. if (e) {
  97. /* dummy entry exist -> wake up was delivered ahead of PF */
  98. hlist_del(&e->link);
  99. raw_spin_unlock(&b->lock);
  100. kfree(e);
  101. return false;
  102. }
  103. n->token = token;
  104. n->cpu = smp_processor_id();
  105. init_swait_queue_head(&n->wq);
  106. hlist_add_head(&n->link, &b->list);
  107. raw_spin_unlock(&b->lock);
  108. return true;
  109. }
  110. /*
  111. * kvm_async_pf_task_wait_schedule - Wait for pagefault to be handled
  112. * @token: Token to identify the sleep node entry
  113. *
  114. * Invoked from the async pagefault handling code or from the VM exit page
  115. * fault handler. In both cases RCU is watching.
  116. */
  117. void kvm_async_pf_task_wait_schedule(u32 token)
  118. {
  119. struct kvm_task_sleep_node n;
  120. DECLARE_SWAITQUEUE(wait);
  121. lockdep_assert_irqs_disabled();
  122. if (!kvm_async_pf_queue_task(token, &n))
  123. return;
  124. for (;;) {
  125. prepare_to_swait_exclusive(&n.wq, &wait, TASK_UNINTERRUPTIBLE);
  126. if (hlist_unhashed(&n.link))
  127. break;
  128. local_irq_enable();
  129. schedule();
  130. local_irq_disable();
  131. }
  132. finish_swait(&n.wq, &wait);
  133. }
  134. EXPORT_SYMBOL_GPL(kvm_async_pf_task_wait_schedule);
  135. static void apf_task_wake_one(struct kvm_task_sleep_node *n)
  136. {
  137. hlist_del_init(&n->link);
  138. if (swq_has_sleeper(&n->wq))
  139. swake_up_one(&n->wq);
  140. }
  141. static void apf_task_wake_all(void)
  142. {
  143. int i;
  144. for (i = 0; i < KVM_TASK_SLEEP_HASHSIZE; i++) {
  145. struct kvm_task_sleep_head *b = &async_pf_sleepers[i];
  146. struct kvm_task_sleep_node *n;
  147. struct hlist_node *p, *next;
  148. raw_spin_lock(&b->lock);
  149. hlist_for_each_safe(p, next, &b->list) {
  150. n = hlist_entry(p, typeof(*n), link);
  151. if (n->cpu == smp_processor_id())
  152. apf_task_wake_one(n);
  153. }
  154. raw_spin_unlock(&b->lock);
  155. }
  156. }
  157. void kvm_async_pf_task_wake(u32 token)
  158. {
  159. u32 key = hash_32(token, KVM_TASK_SLEEP_HASHBITS);
  160. struct kvm_task_sleep_head *b = &async_pf_sleepers[key];
  161. struct kvm_task_sleep_node *n;
  162. if (token == ~0) {
  163. apf_task_wake_all();
  164. return;
  165. }
  166. again:
  167. raw_spin_lock(&b->lock);
  168. n = _find_apf_task(b, token);
  169. if (!n) {
  170. /*
  171. * async PF was not yet handled.
  172. * Add dummy entry for the token.
  173. */
  174. n = kzalloc(sizeof(*n), GFP_ATOMIC);
  175. if (!n) {
  176. /*
  177. * Allocation failed! Busy wait while other cpu
  178. * handles async PF.
  179. */
  180. raw_spin_unlock(&b->lock);
  181. cpu_relax();
  182. goto again;
  183. }
  184. n->token = token;
  185. n->cpu = smp_processor_id();
  186. init_swait_queue_head(&n->wq);
  187. hlist_add_head(&n->link, &b->list);
  188. } else {
  189. apf_task_wake_one(n);
  190. }
  191. raw_spin_unlock(&b->lock);
  192. return;
  193. }
  194. EXPORT_SYMBOL_GPL(kvm_async_pf_task_wake);
  195. noinstr u32 kvm_read_and_reset_apf_flags(void)
  196. {
  197. u32 flags = 0;
  198. if (__this_cpu_read(apf_reason.enabled)) {
  199. flags = __this_cpu_read(apf_reason.flags);
  200. __this_cpu_write(apf_reason.flags, 0);
  201. }
  202. return flags;
  203. }
  204. EXPORT_SYMBOL_GPL(kvm_read_and_reset_apf_flags);
  205. noinstr bool __kvm_handle_async_pf(struct pt_regs *regs, u32 token)
  206. {
  207. u32 flags = kvm_read_and_reset_apf_flags();
  208. irqentry_state_t state;
  209. if (!flags)
  210. return false;
  211. state = irqentry_enter(regs);
  212. instrumentation_begin();
  213. /*
  214. * If the host managed to inject an async #PF into an interrupt
  215. * disabled region, then die hard as this is not going to end well
  216. * and the host side is seriously broken.
  217. */
  218. if (unlikely(!(regs->flags & X86_EFLAGS_IF)))
  219. panic("Host injected async #PF in interrupt disabled region\n");
  220. if (flags & KVM_PV_REASON_PAGE_NOT_PRESENT) {
  221. if (unlikely(!(user_mode(regs))))
  222. panic("Host injected async #PF in kernel mode\n");
  223. /* Page is swapped out by the host. */
  224. kvm_async_pf_task_wait_schedule(token);
  225. } else {
  226. WARN_ONCE(1, "Unexpected async PF flags: %x\n", flags);
  227. }
  228. instrumentation_end();
  229. irqentry_exit(regs, state);
  230. return true;
  231. }
  232. DEFINE_IDTENTRY_SYSVEC(sysvec_kvm_asyncpf_interrupt)
  233. {
  234. struct pt_regs *old_regs = set_irq_regs(regs);
  235. u32 token;
  236. ack_APIC_irq();
  237. inc_irq_stat(irq_hv_callback_count);
  238. if (__this_cpu_read(apf_reason.enabled)) {
  239. token = __this_cpu_read(apf_reason.token);
  240. kvm_async_pf_task_wake(token);
  241. __this_cpu_write(apf_reason.token, 0);
  242. wrmsrl(MSR_KVM_ASYNC_PF_ACK, 1);
  243. }
  244. set_irq_regs(old_regs);
  245. }
  246. static void __init paravirt_ops_setup(void)
  247. {
  248. pv_info.name = "KVM";
  249. if (kvm_para_has_feature(KVM_FEATURE_NOP_IO_DELAY))
  250. pv_ops.cpu.io_delay = kvm_io_delay;
  251. #ifdef CONFIG_X86_IO_APIC
  252. no_timer_check = 1;
  253. #endif
  254. }
  255. static void kvm_register_steal_time(void)
  256. {
  257. int cpu = smp_processor_id();
  258. struct kvm_steal_time *st = &per_cpu(steal_time, cpu);
  259. if (!has_steal_clock)
  260. return;
  261. wrmsrl(MSR_KVM_STEAL_TIME, (slow_virt_to_phys(st) | KVM_MSR_ENABLED));
  262. pr_info("stealtime: cpu %d, msr %llx\n", cpu,
  263. (unsigned long long) slow_virt_to_phys(st));
  264. }
  265. static DEFINE_PER_CPU_DECRYPTED(unsigned long, kvm_apic_eoi) = KVM_PV_EOI_DISABLED;
  266. static notrace void kvm_guest_apic_eoi_write(u32 reg, u32 val)
  267. {
  268. /**
  269. * This relies on __test_and_clear_bit to modify the memory
  270. * in a way that is atomic with respect to the local CPU.
  271. * The hypervisor only accesses this memory from the local CPU so
  272. * there's no need for lock or memory barriers.
  273. * An optimization barrier is implied in apic write.
  274. */
  275. if (__test_and_clear_bit(KVM_PV_EOI_BIT, this_cpu_ptr(&kvm_apic_eoi)))
  276. return;
  277. apic->native_eoi_write(APIC_EOI, APIC_EOI_ACK);
  278. }
  279. static void kvm_guest_cpu_init(void)
  280. {
  281. if (kvm_para_has_feature(KVM_FEATURE_ASYNC_PF_INT) && kvmapf) {
  282. u64 pa = slow_virt_to_phys(this_cpu_ptr(&apf_reason));
  283. WARN_ON_ONCE(!static_branch_likely(&kvm_async_pf_enabled));
  284. pa = slow_virt_to_phys(this_cpu_ptr(&apf_reason));
  285. pa |= KVM_ASYNC_PF_ENABLED | KVM_ASYNC_PF_DELIVERY_AS_INT;
  286. if (kvm_para_has_feature(KVM_FEATURE_ASYNC_PF_VMEXIT))
  287. pa |= KVM_ASYNC_PF_DELIVERY_AS_PF_VMEXIT;
  288. wrmsrl(MSR_KVM_ASYNC_PF_INT, HYPERVISOR_CALLBACK_VECTOR);
  289. wrmsrl(MSR_KVM_ASYNC_PF_EN, pa);
  290. __this_cpu_write(apf_reason.enabled, 1);
  291. pr_info("KVM setup async PF for cpu %d\n", smp_processor_id());
  292. }
  293. if (kvm_para_has_feature(KVM_FEATURE_PV_EOI)) {
  294. unsigned long pa;
  295. /* Size alignment is implied but just to make it explicit. */
  296. BUILD_BUG_ON(__alignof__(kvm_apic_eoi) < 4);
  297. __this_cpu_write(kvm_apic_eoi, 0);
  298. pa = slow_virt_to_phys(this_cpu_ptr(&kvm_apic_eoi))
  299. | KVM_MSR_ENABLED;
  300. wrmsrl(MSR_KVM_PV_EOI_EN, pa);
  301. }
  302. if (has_steal_clock)
  303. kvm_register_steal_time();
  304. }
  305. static void kvm_pv_disable_apf(void)
  306. {
  307. if (!__this_cpu_read(apf_reason.enabled))
  308. return;
  309. wrmsrl(MSR_KVM_ASYNC_PF_EN, 0);
  310. __this_cpu_write(apf_reason.enabled, 0);
  311. pr_info("Unregister pv shared memory for cpu %d\n", smp_processor_id());
  312. }
  313. static void kvm_disable_steal_time(void)
  314. {
  315. if (!has_steal_clock)
  316. return;
  317. wrmsr(MSR_KVM_STEAL_TIME, 0, 0);
  318. }
  319. static void kvm_pv_guest_cpu_reboot(void *unused)
  320. {
  321. /*
  322. * We disable PV EOI before we load a new kernel by kexec,
  323. * since MSR_KVM_PV_EOI_EN stores a pointer into old kernel's memory.
  324. * New kernel can re-enable when it boots.
  325. */
  326. if (kvm_para_has_feature(KVM_FEATURE_PV_EOI))
  327. wrmsrl(MSR_KVM_PV_EOI_EN, 0);
  328. kvm_pv_disable_apf();
  329. kvm_disable_steal_time();
  330. }
  331. static int kvm_pv_reboot_notify(struct notifier_block *nb,
  332. unsigned long code, void *unused)
  333. {
  334. if (code == SYS_RESTART)
  335. on_each_cpu(kvm_pv_guest_cpu_reboot, NULL, 1);
  336. return NOTIFY_DONE;
  337. }
  338. static struct notifier_block kvm_pv_reboot_nb = {
  339. .notifier_call = kvm_pv_reboot_notify,
  340. };
  341. static u64 kvm_steal_clock(int cpu)
  342. {
  343. u64 steal;
  344. struct kvm_steal_time *src;
  345. int version;
  346. src = &per_cpu(steal_time, cpu);
  347. do {
  348. version = src->version;
  349. virt_rmb();
  350. steal = src->steal;
  351. virt_rmb();
  352. } while ((version & 1) || (version != src->version));
  353. return steal;
  354. }
  355. static inline void __set_percpu_decrypted(void *ptr, unsigned long size)
  356. {
  357. early_set_memory_decrypted((unsigned long) ptr, size);
  358. }
  359. /*
  360. * Iterate through all possible CPUs and map the memory region pointed
  361. * by apf_reason, steal_time and kvm_apic_eoi as decrypted at once.
  362. *
  363. * Note: we iterate through all possible CPUs to ensure that CPUs
  364. * hotplugged will have their per-cpu variable already mapped as
  365. * decrypted.
  366. */
  367. static void __init sev_map_percpu_data(void)
  368. {
  369. int cpu;
  370. if (!sev_active())
  371. return;
  372. for_each_possible_cpu(cpu) {
  373. __set_percpu_decrypted(&per_cpu(apf_reason, cpu), sizeof(apf_reason));
  374. __set_percpu_decrypted(&per_cpu(steal_time, cpu), sizeof(steal_time));
  375. __set_percpu_decrypted(&per_cpu(kvm_apic_eoi, cpu), sizeof(kvm_apic_eoi));
  376. }
  377. }
  378. static bool pv_tlb_flush_supported(void)
  379. {
  380. return (kvm_para_has_feature(KVM_FEATURE_PV_TLB_FLUSH) &&
  381. !kvm_para_has_hint(KVM_HINTS_REALTIME) &&
  382. kvm_para_has_feature(KVM_FEATURE_STEAL_TIME));
  383. }
  384. static DEFINE_PER_CPU(cpumask_var_t, __pv_cpu_mask);
  385. static void kvm_guest_cpu_offline(bool shutdown)
  386. {
  387. kvm_disable_steal_time();
  388. if (kvm_para_has_feature(KVM_FEATURE_PV_EOI))
  389. wrmsrl(MSR_KVM_PV_EOI_EN, 0);
  390. kvm_pv_disable_apf();
  391. if (!shutdown)
  392. apf_task_wake_all();
  393. kvmclock_disable();
  394. }
  395. static int kvm_cpu_online(unsigned int cpu)
  396. {
  397. unsigned long flags;
  398. local_irq_save(flags);
  399. kvm_guest_cpu_init();
  400. local_irq_restore(flags);
  401. return 0;
  402. }
  403. #ifdef CONFIG_SMP
  404. static bool pv_ipi_supported(void)
  405. {
  406. return kvm_para_has_feature(KVM_FEATURE_PV_SEND_IPI);
  407. }
  408. static bool pv_sched_yield_supported(void)
  409. {
  410. return (kvm_para_has_feature(KVM_FEATURE_PV_SCHED_YIELD) &&
  411. !kvm_para_has_hint(KVM_HINTS_REALTIME) &&
  412. kvm_para_has_feature(KVM_FEATURE_STEAL_TIME));
  413. }
  414. #define KVM_IPI_CLUSTER_SIZE (2 * BITS_PER_LONG)
  415. static void __send_ipi_mask(const struct cpumask *mask, int vector)
  416. {
  417. unsigned long flags;
  418. int cpu, apic_id, icr;
  419. int min = 0, max = 0;
  420. #ifdef CONFIG_X86_64
  421. __uint128_t ipi_bitmap = 0;
  422. #else
  423. u64 ipi_bitmap = 0;
  424. #endif
  425. long ret;
  426. if (cpumask_empty(mask))
  427. return;
  428. local_irq_save(flags);
  429. switch (vector) {
  430. default:
  431. icr = APIC_DM_FIXED | vector;
  432. break;
  433. case NMI_VECTOR:
  434. icr = APIC_DM_NMI;
  435. break;
  436. }
  437. for_each_cpu(cpu, mask) {
  438. apic_id = per_cpu(x86_cpu_to_apicid, cpu);
  439. if (!ipi_bitmap) {
  440. min = max = apic_id;
  441. } else if (apic_id < min && max - apic_id < KVM_IPI_CLUSTER_SIZE) {
  442. ipi_bitmap <<= min - apic_id;
  443. min = apic_id;
  444. } else if (apic_id > min && apic_id < min + KVM_IPI_CLUSTER_SIZE) {
  445. max = apic_id < max ? max : apic_id;
  446. } else {
  447. ret = kvm_hypercall4(KVM_HC_SEND_IPI, (unsigned long)ipi_bitmap,
  448. (unsigned long)(ipi_bitmap >> BITS_PER_LONG), min, icr);
  449. WARN_ONCE(ret < 0, "kvm-guest: failed to send PV IPI: %ld",
  450. ret);
  451. min = max = apic_id;
  452. ipi_bitmap = 0;
  453. }
  454. __set_bit(apic_id - min, (unsigned long *)&ipi_bitmap);
  455. }
  456. if (ipi_bitmap) {
  457. ret = kvm_hypercall4(KVM_HC_SEND_IPI, (unsigned long)ipi_bitmap,
  458. (unsigned long)(ipi_bitmap >> BITS_PER_LONG), min, icr);
  459. WARN_ONCE(ret < 0, "kvm-guest: failed to send PV IPI: %ld",
  460. ret);
  461. }
  462. local_irq_restore(flags);
  463. }
  464. static void kvm_send_ipi_mask(const struct cpumask *mask, int vector)
  465. {
  466. __send_ipi_mask(mask, vector);
  467. }
  468. static void kvm_send_ipi_mask_allbutself(const struct cpumask *mask, int vector)
  469. {
  470. unsigned int this_cpu = smp_processor_id();
  471. struct cpumask *new_mask = this_cpu_cpumask_var_ptr(__pv_cpu_mask);
  472. const struct cpumask *local_mask;
  473. cpumask_copy(new_mask, mask);
  474. cpumask_clear_cpu(this_cpu, new_mask);
  475. local_mask = new_mask;
  476. __send_ipi_mask(local_mask, vector);
  477. }
  478. /*
  479. * Set the IPI entry points
  480. */
  481. static void kvm_setup_pv_ipi(void)
  482. {
  483. apic->send_IPI_mask = kvm_send_ipi_mask;
  484. apic->send_IPI_mask_allbutself = kvm_send_ipi_mask_allbutself;
  485. pr_info("setup PV IPIs\n");
  486. }
  487. static void kvm_smp_send_call_func_ipi(const struct cpumask *mask)
  488. {
  489. int cpu;
  490. native_send_call_func_ipi(mask);
  491. /* Make sure other vCPUs get a chance to run if they need to. */
  492. for_each_cpu(cpu, mask) {
  493. if (vcpu_is_preempted(cpu)) {
  494. kvm_hypercall1(KVM_HC_SCHED_YIELD, per_cpu(x86_cpu_to_apicid, cpu));
  495. break;
  496. }
  497. }
  498. }
  499. static void __init kvm_smp_prepare_boot_cpu(void)
  500. {
  501. /*
  502. * Map the per-cpu variables as decrypted before kvm_guest_cpu_init()
  503. * shares the guest physical address with the hypervisor.
  504. */
  505. sev_map_percpu_data();
  506. kvm_guest_cpu_init();
  507. native_smp_prepare_boot_cpu();
  508. kvm_spinlock_init();
  509. }
  510. static int kvm_cpu_down_prepare(unsigned int cpu)
  511. {
  512. unsigned long flags;
  513. local_irq_save(flags);
  514. kvm_guest_cpu_offline(false);
  515. local_irq_restore(flags);
  516. return 0;
  517. }
  518. #endif
  519. static int kvm_suspend(void)
  520. {
  521. kvm_guest_cpu_offline(false);
  522. return 0;
  523. }
  524. static void kvm_resume(void)
  525. {
  526. kvm_cpu_online(raw_smp_processor_id());
  527. }
  528. static struct syscore_ops kvm_syscore_ops = {
  529. .suspend = kvm_suspend,
  530. .resume = kvm_resume,
  531. };
  532. /*
  533. * After a PV feature is registered, the host will keep writing to the
  534. * registered memory location. If the guest happens to shutdown, this memory
  535. * won't be valid. In cases like kexec, in which you install a new kernel, this
  536. * means a random memory location will be kept being written.
  537. */
  538. #ifdef CONFIG_KEXEC_CORE
  539. static void kvm_crash_shutdown(struct pt_regs *regs)
  540. {
  541. kvm_guest_cpu_offline(true);
  542. native_machine_crash_shutdown(regs);
  543. }
  544. #endif
  545. static void kvm_flush_tlb_others(const struct cpumask *cpumask,
  546. const struct flush_tlb_info *info)
  547. {
  548. u8 state;
  549. int cpu;
  550. struct kvm_steal_time *src;
  551. struct cpumask *flushmask = this_cpu_cpumask_var_ptr(__pv_cpu_mask);
  552. cpumask_copy(flushmask, cpumask);
  553. /*
  554. * We have to call flush only on online vCPUs. And
  555. * queue flush_on_enter for pre-empted vCPUs
  556. */
  557. for_each_cpu(cpu, flushmask) {
  558. src = &per_cpu(steal_time, cpu);
  559. state = READ_ONCE(src->preempted);
  560. if ((state & KVM_VCPU_PREEMPTED)) {
  561. if (try_cmpxchg(&src->preempted, &state,
  562. state | KVM_VCPU_FLUSH_TLB))
  563. __cpumask_clear_cpu(cpu, flushmask);
  564. }
  565. }
  566. native_flush_tlb_others(flushmask, info);
  567. }
  568. static void __init kvm_guest_init(void)
  569. {
  570. int i;
  571. paravirt_ops_setup();
  572. register_reboot_notifier(&kvm_pv_reboot_nb);
  573. for (i = 0; i < KVM_TASK_SLEEP_HASHSIZE; i++)
  574. raw_spin_lock_init(&async_pf_sleepers[i].lock);
  575. if (kvm_para_has_feature(KVM_FEATURE_STEAL_TIME)) {
  576. has_steal_clock = 1;
  577. pv_ops.time.steal_clock = kvm_steal_clock;
  578. }
  579. if (pv_tlb_flush_supported()) {
  580. pv_ops.mmu.flush_tlb_others = kvm_flush_tlb_others;
  581. pv_ops.mmu.tlb_remove_table = tlb_remove_table;
  582. pr_info("KVM setup pv remote TLB flush\n");
  583. }
  584. if (kvm_para_has_feature(KVM_FEATURE_PV_EOI))
  585. apic_set_eoi_write(kvm_guest_apic_eoi_write);
  586. if (kvm_para_has_feature(KVM_FEATURE_ASYNC_PF_INT) && kvmapf) {
  587. static_branch_enable(&kvm_async_pf_enabled);
  588. alloc_intr_gate(HYPERVISOR_CALLBACK_VECTOR, asm_sysvec_kvm_asyncpf_interrupt);
  589. }
  590. #ifdef CONFIG_SMP
  591. smp_ops.smp_prepare_boot_cpu = kvm_smp_prepare_boot_cpu;
  592. if (pv_sched_yield_supported()) {
  593. smp_ops.send_call_func_ipi = kvm_smp_send_call_func_ipi;
  594. pr_info("setup PV sched yield\n");
  595. }
  596. if (cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN, "x86/kvm:online",
  597. kvm_cpu_online, kvm_cpu_down_prepare) < 0)
  598. pr_err("failed to install cpu hotplug callbacks\n");
  599. #else
  600. sev_map_percpu_data();
  601. kvm_guest_cpu_init();
  602. #endif
  603. #ifdef CONFIG_KEXEC_CORE
  604. machine_ops.crash_shutdown = kvm_crash_shutdown;
  605. #endif
  606. register_syscore_ops(&kvm_syscore_ops);
  607. /*
  608. * Hard lockup detection is enabled by default. Disable it, as guests
  609. * can get false positives too easily, for example if the host is
  610. * overcommitted.
  611. */
  612. hardlockup_detector_disable();
  613. }
  614. static noinline uint32_t __kvm_cpuid_base(void)
  615. {
  616. if (boot_cpu_data.cpuid_level < 0)
  617. return 0; /* So we don't blow up on old processors */
  618. if (boot_cpu_has(X86_FEATURE_HYPERVISOR))
  619. return hypervisor_cpuid_base("KVMKVMKVM\0\0\0", 0);
  620. return 0;
  621. }
  622. static inline uint32_t kvm_cpuid_base(void)
  623. {
  624. static int kvm_cpuid_base = -1;
  625. if (kvm_cpuid_base == -1)
  626. kvm_cpuid_base = __kvm_cpuid_base();
  627. return kvm_cpuid_base;
  628. }
  629. bool kvm_para_available(void)
  630. {
  631. return kvm_cpuid_base() != 0;
  632. }
  633. EXPORT_SYMBOL_GPL(kvm_para_available);
  634. unsigned int kvm_arch_para_features(void)
  635. {
  636. return cpuid_eax(kvm_cpuid_base() | KVM_CPUID_FEATURES);
  637. }
  638. unsigned int kvm_arch_para_hints(void)
  639. {
  640. return cpuid_edx(kvm_cpuid_base() | KVM_CPUID_FEATURES);
  641. }
  642. EXPORT_SYMBOL_GPL(kvm_arch_para_hints);
  643. static uint32_t __init kvm_detect(void)
  644. {
  645. return kvm_cpuid_base();
  646. }
  647. static void __init kvm_apic_init(void)
  648. {
  649. #if defined(CONFIG_SMP)
  650. if (pv_ipi_supported())
  651. kvm_setup_pv_ipi();
  652. #endif
  653. }
  654. static void __init kvm_init_platform(void)
  655. {
  656. kvmclock_init();
  657. x86_platform.apic_post_init = kvm_apic_init;
  658. }
  659. #if defined(CONFIG_AMD_MEM_ENCRYPT)
  660. static void kvm_sev_es_hcall_prepare(struct ghcb *ghcb, struct pt_regs *regs)
  661. {
  662. /* RAX and CPL are already in the GHCB */
  663. ghcb_set_rbx(ghcb, regs->bx);
  664. ghcb_set_rcx(ghcb, regs->cx);
  665. ghcb_set_rdx(ghcb, regs->dx);
  666. ghcb_set_rsi(ghcb, regs->si);
  667. }
  668. static bool kvm_sev_es_hcall_finish(struct ghcb *ghcb, struct pt_regs *regs)
  669. {
  670. /* No checking of the return state needed */
  671. return true;
  672. }
  673. #endif
  674. const __initconst struct hypervisor_x86 x86_hyper_kvm = {
  675. .name = "KVM",
  676. .detect = kvm_detect,
  677. .type = X86_HYPER_KVM,
  678. .init.guest_late_init = kvm_guest_init,
  679. .init.x2apic_available = kvm_para_available,
  680. .init.init_platform = kvm_init_platform,
  681. #if defined(CONFIG_AMD_MEM_ENCRYPT)
  682. .runtime.sev_es_hcall_prepare = kvm_sev_es_hcall_prepare,
  683. .runtime.sev_es_hcall_finish = kvm_sev_es_hcall_finish,
  684. #endif
  685. };
  686. static __init int activate_jump_labels(void)
  687. {
  688. if (has_steal_clock) {
  689. static_key_slow_inc(&paravirt_steal_enabled);
  690. if (steal_acc)
  691. static_key_slow_inc(&paravirt_steal_rq_enabled);
  692. }
  693. return 0;
  694. }
  695. arch_initcall(activate_jump_labels);
  696. static __init int kvm_alloc_cpumask(void)
  697. {
  698. int cpu;
  699. bool alloc = false;
  700. if (!kvm_para_available() || nopv)
  701. return 0;
  702. if (pv_tlb_flush_supported())
  703. alloc = true;
  704. #if defined(CONFIG_SMP)
  705. if (pv_ipi_supported())
  706. alloc = true;
  707. #endif
  708. if (alloc)
  709. for_each_possible_cpu(cpu) {
  710. zalloc_cpumask_var_node(per_cpu_ptr(&__pv_cpu_mask, cpu),
  711. GFP_KERNEL, cpu_to_node(cpu));
  712. }
  713. return 0;
  714. }
  715. arch_initcall(kvm_alloc_cpumask);
  716. #ifdef CONFIG_PARAVIRT_SPINLOCKS
  717. /* Kick a cpu by its apicid. Used to wake up a halted vcpu */
  718. static void kvm_kick_cpu(int cpu)
  719. {
  720. int apicid;
  721. unsigned long flags = 0;
  722. apicid = per_cpu(x86_cpu_to_apicid, cpu);
  723. kvm_hypercall2(KVM_HC_KICK_CPU, flags, apicid);
  724. }
  725. #include <asm/qspinlock.h>
  726. static void kvm_wait(u8 *ptr, u8 val)
  727. {
  728. unsigned long flags;
  729. if (in_nmi())
  730. return;
  731. local_irq_save(flags);
  732. if (READ_ONCE(*ptr) != val)
  733. goto out;
  734. /*
  735. * halt until it's our turn and kicked. Note that we do safe halt
  736. * for irq enabled case to avoid hang when lock info is overwritten
  737. * in irq spinlock slowpath and no spurious interrupt occur to save us.
  738. */
  739. if (arch_irqs_disabled_flags(flags))
  740. halt();
  741. else
  742. safe_halt();
  743. out:
  744. local_irq_restore(flags);
  745. }
  746. #ifdef CONFIG_X86_32
  747. __visible bool __kvm_vcpu_is_preempted(long cpu)
  748. {
  749. struct kvm_steal_time *src = &per_cpu(steal_time, cpu);
  750. return !!(src->preempted & KVM_VCPU_PREEMPTED);
  751. }
  752. PV_CALLEE_SAVE_REGS_THUNK(__kvm_vcpu_is_preempted);
  753. #else
  754. #include <asm/asm-offsets.h>
  755. extern bool __raw_callee_save___kvm_vcpu_is_preempted(long);
  756. /*
  757. * Hand-optimize version for x86-64 to avoid 8 64-bit register saving and
  758. * restoring to/from the stack.
  759. */
  760. asm(
  761. ".pushsection .text;"
  762. ".global __raw_callee_save___kvm_vcpu_is_preempted;"
  763. ".type __raw_callee_save___kvm_vcpu_is_preempted, @function;"
  764. "__raw_callee_save___kvm_vcpu_is_preempted:"
  765. "movq __per_cpu_offset(,%rdi,8), %rax;"
  766. "cmpb $0, " __stringify(KVM_STEAL_TIME_preempted) "+steal_time(%rax);"
  767. "setne %al;"
  768. "ret;"
  769. ".size __raw_callee_save___kvm_vcpu_is_preempted, .-__raw_callee_save___kvm_vcpu_is_preempted;"
  770. ".popsection");
  771. #endif
  772. /*
  773. * Setup pv_lock_ops to exploit KVM_FEATURE_PV_UNHALT if present.
  774. */
  775. void __init kvm_spinlock_init(void)
  776. {
  777. /*
  778. * In case host doesn't support KVM_FEATURE_PV_UNHALT there is still an
  779. * advantage of keeping virt_spin_lock_key enabled: virt_spin_lock() is
  780. * preferred over native qspinlock when vCPU is preempted.
  781. */
  782. if (!kvm_para_has_feature(KVM_FEATURE_PV_UNHALT)) {
  783. pr_info("PV spinlocks disabled, no host support\n");
  784. return;
  785. }
  786. /*
  787. * Disable PV spinlocks and use native qspinlock when dedicated pCPUs
  788. * are available.
  789. */
  790. if (kvm_para_has_hint(KVM_HINTS_REALTIME)) {
  791. pr_info("PV spinlocks disabled with KVM_HINTS_REALTIME hints\n");
  792. goto out;
  793. }
  794. if (num_possible_cpus() == 1) {
  795. pr_info("PV spinlocks disabled, single CPU\n");
  796. goto out;
  797. }
  798. if (nopvspin) {
  799. pr_info("PV spinlocks disabled, forced by \"nopvspin\" parameter\n");
  800. goto out;
  801. }
  802. pr_info("PV spinlocks enabled\n");
  803. __pv_init_lock_hash();
  804. pv_ops.lock.queued_spin_lock_slowpath = __pv_queued_spin_lock_slowpath;
  805. pv_ops.lock.queued_spin_unlock =
  806. PV_CALLEE_SAVE(__pv_queued_spin_unlock);
  807. pv_ops.lock.wait = kvm_wait;
  808. pv_ops.lock.kick = kvm_kick_cpu;
  809. if (kvm_para_has_feature(KVM_FEATURE_STEAL_TIME)) {
  810. pv_ops.lock.vcpu_is_preempted =
  811. PV_CALLEE_SAVE(__kvm_vcpu_is_preempted);
  812. }
  813. /*
  814. * When PV spinlock is enabled which is preferred over
  815. * virt_spin_lock(), virt_spin_lock_key's value is meaningless.
  816. * Just disable it anyway.
  817. */
  818. out:
  819. static_branch_disable(&virt_spin_lock_key);
  820. }
  821. #endif /* CONFIG_PARAVIRT_SPINLOCKS */
  822. #ifdef CONFIG_ARCH_CPUIDLE_HALTPOLL
  823. static void kvm_disable_host_haltpoll(void *i)
  824. {
  825. wrmsrl(MSR_KVM_POLL_CONTROL, 0);
  826. }
  827. static void kvm_enable_host_haltpoll(void *i)
  828. {
  829. wrmsrl(MSR_KVM_POLL_CONTROL, 1);
  830. }
  831. void arch_haltpoll_enable(unsigned int cpu)
  832. {
  833. if (!kvm_para_has_feature(KVM_FEATURE_POLL_CONTROL)) {
  834. pr_err_once("host does not support poll control\n");
  835. pr_err_once("host upgrade recommended\n");
  836. return;
  837. }
  838. /* Enable guest halt poll disables host halt poll */
  839. smp_call_function_single(cpu, kvm_disable_host_haltpoll, NULL, 1);
  840. }
  841. EXPORT_SYMBOL_GPL(arch_haltpoll_enable);
  842. void arch_haltpoll_disable(unsigned int cpu)
  843. {
  844. if (!kvm_para_has_feature(KVM_FEATURE_POLL_CONTROL))
  845. return;
  846. /* Disable guest halt poll enables host halt poll */
  847. smp_call_function_single(cpu, kvm_enable_host_haltpoll, NULL, 1);
  848. }
  849. EXPORT_SYMBOL_GPL(arch_haltpoll_disable);
  850. #endif