kvm.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  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/module.h>
  23. #include <linux/kernel.h>
  24. #include <linux/kvm_para.h>
  25. #include <linux/cpu.h>
  26. #include <linux/mm.h>
  27. #include <linux/highmem.h>
  28. #include <linux/hardirq.h>
  29. #include <linux/notifier.h>
  30. #include <linux/reboot.h>
  31. #include <linux/hash.h>
  32. #include <linux/sched.h>
  33. #include <linux/slab.h>
  34. #include <linux/kprobes.h>
  35. #include <asm/timer.h>
  36. #include <asm/cpu.h>
  37. #include <asm/traps.h>
  38. #include <asm/desc.h>
  39. #include <asm/tlbflush.h>
  40. #include <asm/idle.h>
  41. #include <asm/apic.h>
  42. #include <asm/apicdef.h>
  43. #include <asm/hypervisor.h>
  44. #include <asm/kvm_guest.h>
  45. #include <asm/context_tracking.h>
  46. static int kvmapf = 1;
  47. static int parse_no_kvmapf(char *arg)
  48. {
  49. kvmapf = 0;
  50. return 0;
  51. }
  52. early_param("no-kvmapf", parse_no_kvmapf);
  53. static int steal_acc = 1;
  54. static int parse_no_stealacc(char *arg)
  55. {
  56. steal_acc = 0;
  57. return 0;
  58. }
  59. early_param("no-steal-acc", parse_no_stealacc);
  60. static int kvmclock_vsyscall = 1;
  61. static int parse_no_kvmclock_vsyscall(char *arg)
  62. {
  63. kvmclock_vsyscall = 0;
  64. return 0;
  65. }
  66. early_param("no-kvmclock-vsyscall", parse_no_kvmclock_vsyscall);
  67. static DEFINE_PER_CPU(struct kvm_vcpu_pv_apf_data, apf_reason) __aligned(64);
  68. static DEFINE_PER_CPU(struct kvm_steal_time, steal_time) __aligned(64);
  69. static int has_steal_clock = 0;
  70. /*
  71. * No need for any "IO delay" on KVM
  72. */
  73. static void kvm_io_delay(void)
  74. {
  75. }
  76. #define KVM_TASK_SLEEP_HASHBITS 8
  77. #define KVM_TASK_SLEEP_HASHSIZE (1<<KVM_TASK_SLEEP_HASHBITS)
  78. struct kvm_task_sleep_node {
  79. struct hlist_node link;
  80. wait_queue_head_t wq;
  81. u32 token;
  82. int cpu;
  83. bool halted;
  84. };
  85. static struct kvm_task_sleep_head {
  86. spinlock_t lock;
  87. struct hlist_head list;
  88. } async_pf_sleepers[KVM_TASK_SLEEP_HASHSIZE];
  89. static struct kvm_task_sleep_node *_find_apf_task(struct kvm_task_sleep_head *b,
  90. u32 token)
  91. {
  92. struct hlist_node *p;
  93. hlist_for_each(p, &b->list) {
  94. struct kvm_task_sleep_node *n =
  95. hlist_entry(p, typeof(*n), link);
  96. if (n->token == token)
  97. return n;
  98. }
  99. return NULL;
  100. }
  101. void kvm_async_pf_task_wait(u32 token)
  102. {
  103. u32 key = hash_32(token, KVM_TASK_SLEEP_HASHBITS);
  104. struct kvm_task_sleep_head *b = &async_pf_sleepers[key];
  105. struct kvm_task_sleep_node n, *e;
  106. DEFINE_WAIT(wait);
  107. rcu_irq_enter();
  108. spin_lock(&b->lock);
  109. e = _find_apf_task(b, token);
  110. if (e) {
  111. /* dummy entry exist -> wake up was delivered ahead of PF */
  112. hlist_del(&e->link);
  113. kfree(e);
  114. spin_unlock(&b->lock);
  115. rcu_irq_exit();
  116. return;
  117. }
  118. n.token = token;
  119. n.cpu = smp_processor_id();
  120. n.halted = is_idle_task(current) || preempt_count() > 1;
  121. init_waitqueue_head(&n.wq);
  122. hlist_add_head(&n.link, &b->list);
  123. spin_unlock(&b->lock);
  124. for (;;) {
  125. if (!n.halted)
  126. prepare_to_wait(&n.wq, &wait, TASK_UNINTERRUPTIBLE);
  127. if (hlist_unhashed(&n.link))
  128. break;
  129. if (!n.halted) {
  130. local_irq_enable();
  131. schedule();
  132. local_irq_disable();
  133. } else {
  134. /*
  135. * We cannot reschedule. So halt.
  136. */
  137. rcu_irq_exit();
  138. native_safe_halt();
  139. rcu_irq_enter();
  140. local_irq_disable();
  141. }
  142. }
  143. if (!n.halted)
  144. finish_wait(&n.wq, &wait);
  145. rcu_irq_exit();
  146. return;
  147. }
  148. EXPORT_SYMBOL_GPL(kvm_async_pf_task_wait);
  149. static void apf_task_wake_one(struct kvm_task_sleep_node *n)
  150. {
  151. hlist_del_init(&n->link);
  152. if (n->halted)
  153. smp_send_reschedule(n->cpu);
  154. else if (waitqueue_active(&n->wq))
  155. wake_up(&n->wq);
  156. }
  157. static void apf_task_wake_all(void)
  158. {
  159. int i;
  160. for (i = 0; i < KVM_TASK_SLEEP_HASHSIZE; i++) {
  161. struct hlist_node *p, *next;
  162. struct kvm_task_sleep_head *b = &async_pf_sleepers[i];
  163. spin_lock(&b->lock);
  164. hlist_for_each_safe(p, next, &b->list) {
  165. struct kvm_task_sleep_node *n =
  166. hlist_entry(p, typeof(*n), link);
  167. if (n->cpu == smp_processor_id())
  168. apf_task_wake_one(n);
  169. }
  170. spin_unlock(&b->lock);
  171. }
  172. }
  173. void kvm_async_pf_task_wake(u32 token)
  174. {
  175. u32 key = hash_32(token, KVM_TASK_SLEEP_HASHBITS);
  176. struct kvm_task_sleep_head *b = &async_pf_sleepers[key];
  177. struct kvm_task_sleep_node *n;
  178. if (token == ~0) {
  179. apf_task_wake_all();
  180. return;
  181. }
  182. again:
  183. spin_lock(&b->lock);
  184. n = _find_apf_task(b, token);
  185. if (!n) {
  186. /*
  187. * async PF was not yet handled.
  188. * Add dummy entry for the token.
  189. */
  190. n = kzalloc(sizeof(*n), GFP_ATOMIC);
  191. if (!n) {
  192. /*
  193. * Allocation failed! Busy wait while other cpu
  194. * handles async PF.
  195. */
  196. spin_unlock(&b->lock);
  197. cpu_relax();
  198. goto again;
  199. }
  200. n->token = token;
  201. n->cpu = smp_processor_id();
  202. init_waitqueue_head(&n->wq);
  203. hlist_add_head(&n->link, &b->list);
  204. } else
  205. apf_task_wake_one(n);
  206. spin_unlock(&b->lock);
  207. return;
  208. }
  209. EXPORT_SYMBOL_GPL(kvm_async_pf_task_wake);
  210. u32 kvm_read_and_reset_pf_reason(void)
  211. {
  212. u32 reason = 0;
  213. if (__get_cpu_var(apf_reason).enabled) {
  214. reason = __get_cpu_var(apf_reason).reason;
  215. __get_cpu_var(apf_reason).reason = 0;
  216. }
  217. return reason;
  218. }
  219. EXPORT_SYMBOL_GPL(kvm_read_and_reset_pf_reason);
  220. dotraplinkage void __kprobes
  221. do_async_page_fault(struct pt_regs *regs, unsigned long error_code)
  222. {
  223. switch (kvm_read_and_reset_pf_reason()) {
  224. default:
  225. do_page_fault(regs, error_code);
  226. break;
  227. case KVM_PV_REASON_PAGE_NOT_PRESENT:
  228. /* page is swapped out by the host. */
  229. exception_enter(regs);
  230. exit_idle();
  231. kvm_async_pf_task_wait((u32)read_cr2());
  232. exception_exit(regs);
  233. break;
  234. case KVM_PV_REASON_PAGE_READY:
  235. rcu_irq_enter();
  236. exit_idle();
  237. kvm_async_pf_task_wake((u32)read_cr2());
  238. rcu_irq_exit();
  239. break;
  240. }
  241. }
  242. static void __init paravirt_ops_setup(void)
  243. {
  244. pv_info.name = "KVM";
  245. pv_info.paravirt_enabled = 1;
  246. if (kvm_para_has_feature(KVM_FEATURE_NOP_IO_DELAY))
  247. pv_cpu_ops.io_delay = kvm_io_delay;
  248. #ifdef CONFIG_X86_IO_APIC
  249. no_timer_check = 1;
  250. #endif
  251. }
  252. static void kvm_register_steal_time(void)
  253. {
  254. int cpu = smp_processor_id();
  255. struct kvm_steal_time *st = &per_cpu(steal_time, cpu);
  256. if (!has_steal_clock)
  257. return;
  258. memset(st, 0, sizeof(*st));
  259. wrmsrl(MSR_KVM_STEAL_TIME, (slow_virt_to_phys(st) | KVM_MSR_ENABLED));
  260. pr_info("kvm-stealtime: cpu %d, msr %llx\n",
  261. cpu, (unsigned long long) slow_virt_to_phys(st));
  262. }
  263. static DEFINE_PER_CPU(unsigned long, kvm_apic_eoi) = KVM_PV_EOI_DISABLED;
  264. static void kvm_guest_apic_eoi_write(u32 reg, u32 val)
  265. {
  266. /**
  267. * This relies on __test_and_clear_bit to modify the memory
  268. * in a way that is atomic with respect to the local CPU.
  269. * The hypervisor only accesses this memory from the local CPU so
  270. * there's no need for lock or memory barriers.
  271. * An optimization barrier is implied in apic write.
  272. */
  273. if (__test_and_clear_bit(KVM_PV_EOI_BIT, &__get_cpu_var(kvm_apic_eoi)))
  274. return;
  275. apic_write(APIC_EOI, APIC_EOI_ACK);
  276. }
  277. void __cpuinit kvm_guest_cpu_init(void)
  278. {
  279. if (!kvm_para_available())
  280. return;
  281. if (kvm_para_has_feature(KVM_FEATURE_ASYNC_PF) && kvmapf) {
  282. u64 pa = slow_virt_to_phys(&__get_cpu_var(apf_reason));
  283. #ifdef CONFIG_PREEMPT
  284. pa |= KVM_ASYNC_PF_SEND_ALWAYS;
  285. #endif
  286. wrmsrl(MSR_KVM_ASYNC_PF_EN, pa | KVM_ASYNC_PF_ENABLED);
  287. __get_cpu_var(apf_reason).enabled = 1;
  288. printk(KERN_INFO"KVM setup async PF for cpu %d\n",
  289. smp_processor_id());
  290. }
  291. if (kvm_para_has_feature(KVM_FEATURE_PV_EOI)) {
  292. unsigned long pa;
  293. /* Size alignment is implied but just to make it explicit. */
  294. BUILD_BUG_ON(__alignof__(kvm_apic_eoi) < 4);
  295. __get_cpu_var(kvm_apic_eoi) = 0;
  296. pa = slow_virt_to_phys(&__get_cpu_var(kvm_apic_eoi))
  297. | KVM_MSR_ENABLED;
  298. wrmsrl(MSR_KVM_PV_EOI_EN, pa);
  299. }
  300. if (has_steal_clock)
  301. kvm_register_steal_time();
  302. }
  303. static void kvm_pv_disable_apf(void)
  304. {
  305. if (!__get_cpu_var(apf_reason).enabled)
  306. return;
  307. wrmsrl(MSR_KVM_ASYNC_PF_EN, 0);
  308. __get_cpu_var(apf_reason).enabled = 0;
  309. printk(KERN_INFO"Unregister pv shared memory for cpu %d\n",
  310. smp_processor_id());
  311. }
  312. static void kvm_pv_guest_cpu_reboot(void *unused)
  313. {
  314. /*
  315. * We disable PV EOI before we load a new kernel by kexec,
  316. * since MSR_KVM_PV_EOI_EN stores a pointer into old kernel's memory.
  317. * New kernel can re-enable when it boots.
  318. */
  319. if (kvm_para_has_feature(KVM_FEATURE_PV_EOI))
  320. wrmsrl(MSR_KVM_PV_EOI_EN, 0);
  321. kvm_pv_disable_apf();
  322. kvm_disable_steal_time();
  323. }
  324. static int kvm_pv_reboot_notify(struct notifier_block *nb,
  325. unsigned long code, void *unused)
  326. {
  327. if (code == SYS_RESTART)
  328. on_each_cpu(kvm_pv_guest_cpu_reboot, NULL, 1);
  329. return NOTIFY_DONE;
  330. }
  331. static struct notifier_block kvm_pv_reboot_nb = {
  332. .notifier_call = kvm_pv_reboot_notify,
  333. };
  334. static u64 kvm_steal_clock(int cpu)
  335. {
  336. u64 steal;
  337. struct kvm_steal_time *src;
  338. int version;
  339. src = &per_cpu(steal_time, cpu);
  340. do {
  341. version = src->version;
  342. rmb();
  343. steal = src->steal;
  344. rmb();
  345. } while ((version & 1) || (version != src->version));
  346. return steal;
  347. }
  348. void kvm_disable_steal_time(void)
  349. {
  350. if (!has_steal_clock)
  351. return;
  352. wrmsr(MSR_KVM_STEAL_TIME, 0, 0);
  353. }
  354. #ifdef CONFIG_SMP
  355. static void __init kvm_smp_prepare_boot_cpu(void)
  356. {
  357. WARN_ON(kvm_register_clock("primary cpu clock"));
  358. kvm_guest_cpu_init();
  359. native_smp_prepare_boot_cpu();
  360. }
  361. static void __cpuinit kvm_guest_cpu_online(void *dummy)
  362. {
  363. kvm_guest_cpu_init();
  364. }
  365. static void kvm_guest_cpu_offline(void *dummy)
  366. {
  367. kvm_disable_steal_time();
  368. if (kvm_para_has_feature(KVM_FEATURE_PV_EOI))
  369. wrmsrl(MSR_KVM_PV_EOI_EN, 0);
  370. kvm_pv_disable_apf();
  371. apf_task_wake_all();
  372. }
  373. static int __cpuinit kvm_cpu_notify(struct notifier_block *self,
  374. unsigned long action, void *hcpu)
  375. {
  376. int cpu = (unsigned long)hcpu;
  377. switch (action) {
  378. case CPU_ONLINE:
  379. case CPU_DOWN_FAILED:
  380. case CPU_ONLINE_FROZEN:
  381. smp_call_function_single(cpu, kvm_guest_cpu_online, NULL, 0);
  382. break;
  383. case CPU_DOWN_PREPARE:
  384. case CPU_DOWN_PREPARE_FROZEN:
  385. smp_call_function_single(cpu, kvm_guest_cpu_offline, NULL, 1);
  386. break;
  387. default:
  388. break;
  389. }
  390. return NOTIFY_OK;
  391. }
  392. static struct notifier_block __cpuinitdata kvm_cpu_notifier = {
  393. .notifier_call = kvm_cpu_notify,
  394. };
  395. #endif
  396. static void __init kvm_apf_trap_init(void)
  397. {
  398. set_intr_gate(14, &async_page_fault);
  399. }
  400. void __init kvm_guest_init(void)
  401. {
  402. int i;
  403. if (!kvm_para_available())
  404. return;
  405. paravirt_ops_setup();
  406. register_reboot_notifier(&kvm_pv_reboot_nb);
  407. for (i = 0; i < KVM_TASK_SLEEP_HASHSIZE; i++)
  408. spin_lock_init(&async_pf_sleepers[i].lock);
  409. if (kvm_para_has_feature(KVM_FEATURE_ASYNC_PF))
  410. x86_init.irqs.trap_init = kvm_apf_trap_init;
  411. if (kvm_para_has_feature(KVM_FEATURE_STEAL_TIME)) {
  412. has_steal_clock = 1;
  413. pv_time_ops.steal_clock = kvm_steal_clock;
  414. }
  415. if (kvm_para_has_feature(KVM_FEATURE_PV_EOI))
  416. apic_set_eoi_write(kvm_guest_apic_eoi_write);
  417. if (kvmclock_vsyscall)
  418. kvm_setup_vsyscall_timeinfo();
  419. #ifdef CONFIG_SMP
  420. smp_ops.smp_prepare_boot_cpu = kvm_smp_prepare_boot_cpu;
  421. register_cpu_notifier(&kvm_cpu_notifier);
  422. #else
  423. kvm_guest_cpu_init();
  424. #endif
  425. }
  426. static bool __init kvm_detect(void)
  427. {
  428. if (!kvm_para_available())
  429. return false;
  430. return true;
  431. }
  432. const struct hypervisor_x86 x86_hyper_kvm __refconst = {
  433. .name = "KVM",
  434. .detect = kvm_detect,
  435. .x2apic_available = kvm_para_available,
  436. };
  437. EXPORT_SYMBOL_GPL(x86_hyper_kvm);
  438. static __init int activate_jump_labels(void)
  439. {
  440. if (has_steal_clock) {
  441. static_key_slow_inc(&paravirt_steal_enabled);
  442. if (steal_acc)
  443. static_key_slow_inc(&paravirt_steal_rq_enabled);
  444. }
  445. return 0;
  446. }
  447. arch_initcall(activate_jump_labels);