kvm.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  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/module.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 <asm/timer.h>
  37. #include <asm/cpu.h>
  38. #include <asm/traps.h>
  39. #include <asm/desc.h>
  40. #include <asm/tlbflush.h>
  41. #include <asm/idle.h>
  42. #include <asm/apic.h>
  43. #include <asm/apicdef.h>
  44. #include <asm/hypervisor.h>
  45. #include <asm/kvm_guest.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. enum ctx_state prev_state;
  224. switch (kvm_read_and_reset_pf_reason()) {
  225. default:
  226. do_page_fault(regs, error_code);
  227. break;
  228. case KVM_PV_REASON_PAGE_NOT_PRESENT:
  229. /* page is swapped out by the host. */
  230. prev_state = exception_enter();
  231. exit_idle();
  232. kvm_async_pf_task_wait((u32)read_cr2());
  233. exception_exit(prev_state);
  234. break;
  235. case KVM_PV_REASON_PAGE_READY:
  236. rcu_irq_enter();
  237. exit_idle();
  238. kvm_async_pf_task_wake((u32)read_cr2());
  239. rcu_irq_exit();
  240. break;
  241. }
  242. }
  243. static void __init paravirt_ops_setup(void)
  244. {
  245. pv_info.name = "KVM";
  246. pv_info.paravirt_enabled = 1;
  247. if (kvm_para_has_feature(KVM_FEATURE_NOP_IO_DELAY))
  248. pv_cpu_ops.io_delay = kvm_io_delay;
  249. #ifdef CONFIG_X86_IO_APIC
  250. no_timer_check = 1;
  251. #endif
  252. }
  253. static void kvm_register_steal_time(void)
  254. {
  255. int cpu = smp_processor_id();
  256. struct kvm_steal_time *st = &per_cpu(steal_time, cpu);
  257. if (!has_steal_clock)
  258. return;
  259. memset(st, 0, sizeof(*st));
  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(unsigned long, kvm_apic_eoi) = KVM_PV_EOI_DISABLED;
  265. static 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, &__get_cpu_var(kvm_apic_eoi)))
  275. return;
  276. apic_write(APIC_EOI, APIC_EOI_ACK);
  277. }
  278. void __cpuinit 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(&__get_cpu_var(apf_reason));
  284. #ifdef CONFIG_PREEMPT
  285. pa |= KVM_ASYNC_PF_SEND_ALWAYS;
  286. #endif
  287. wrmsrl(MSR_KVM_ASYNC_PF_EN, pa | KVM_ASYNC_PF_ENABLED);
  288. __get_cpu_var(apf_reason).enabled = 1;
  289. printk(KERN_INFO"KVM setup async PF for cpu %d\n",
  290. smp_processor_id());
  291. }
  292. if (kvm_para_has_feature(KVM_FEATURE_PV_EOI)) {
  293. unsigned long pa;
  294. /* Size alignment is implied but just to make it explicit. */
  295. BUILD_BUG_ON(__alignof__(kvm_apic_eoi) < 4);
  296. __get_cpu_var(kvm_apic_eoi) = 0;
  297. pa = slow_virt_to_phys(&__get_cpu_var(kvm_apic_eoi))
  298. | KVM_MSR_ENABLED;
  299. wrmsrl(MSR_KVM_PV_EOI_EN, pa);
  300. }
  301. if (has_steal_clock)
  302. kvm_register_steal_time();
  303. }
  304. static void kvm_pv_disable_apf(void)
  305. {
  306. if (!__get_cpu_var(apf_reason).enabled)
  307. return;
  308. wrmsrl(MSR_KVM_ASYNC_PF_EN, 0);
  309. __get_cpu_var(apf_reason).enabled = 0;
  310. printk(KERN_INFO"Unregister pv shared memory for cpu %d\n",
  311. smp_processor_id());
  312. }
  313. static void kvm_pv_guest_cpu_reboot(void *unused)
  314. {
  315. /*
  316. * We disable PV EOI before we load a new kernel by kexec,
  317. * since MSR_KVM_PV_EOI_EN stores a pointer into old kernel's memory.
  318. * New kernel can re-enable when it boots.
  319. */
  320. if (kvm_para_has_feature(KVM_FEATURE_PV_EOI))
  321. wrmsrl(MSR_KVM_PV_EOI_EN, 0);
  322. kvm_pv_disable_apf();
  323. kvm_disable_steal_time();
  324. }
  325. static int kvm_pv_reboot_notify(struct notifier_block *nb,
  326. unsigned long code, void *unused)
  327. {
  328. if (code == SYS_RESTART)
  329. on_each_cpu(kvm_pv_guest_cpu_reboot, NULL, 1);
  330. return NOTIFY_DONE;
  331. }
  332. static struct notifier_block kvm_pv_reboot_nb = {
  333. .notifier_call = kvm_pv_reboot_notify,
  334. };
  335. static u64 kvm_steal_clock(int cpu)
  336. {
  337. u64 steal;
  338. struct kvm_steal_time *src;
  339. int version;
  340. src = &per_cpu(steal_time, cpu);
  341. do {
  342. version = src->version;
  343. rmb();
  344. steal = src->steal;
  345. rmb();
  346. } while ((version & 1) || (version != src->version));
  347. return steal;
  348. }
  349. void kvm_disable_steal_time(void)
  350. {
  351. if (!has_steal_clock)
  352. return;
  353. wrmsr(MSR_KVM_STEAL_TIME, 0, 0);
  354. }
  355. #ifdef CONFIG_SMP
  356. static void __init kvm_smp_prepare_boot_cpu(void)
  357. {
  358. WARN_ON(kvm_register_clock("primary cpu clock"));
  359. kvm_guest_cpu_init();
  360. native_smp_prepare_boot_cpu();
  361. }
  362. static void __cpuinit kvm_guest_cpu_online(void *dummy)
  363. {
  364. kvm_guest_cpu_init();
  365. }
  366. static void kvm_guest_cpu_offline(void *dummy)
  367. {
  368. kvm_disable_steal_time();
  369. if (kvm_para_has_feature(KVM_FEATURE_PV_EOI))
  370. wrmsrl(MSR_KVM_PV_EOI_EN, 0);
  371. kvm_pv_disable_apf();
  372. apf_task_wake_all();
  373. }
  374. static int __cpuinit kvm_cpu_notify(struct notifier_block *self,
  375. unsigned long action, void *hcpu)
  376. {
  377. int cpu = (unsigned long)hcpu;
  378. switch (action) {
  379. case CPU_ONLINE:
  380. case CPU_DOWN_FAILED:
  381. case CPU_ONLINE_FROZEN:
  382. smp_call_function_single(cpu, kvm_guest_cpu_online, NULL, 0);
  383. break;
  384. case CPU_DOWN_PREPARE:
  385. case CPU_DOWN_PREPARE_FROZEN:
  386. smp_call_function_single(cpu, kvm_guest_cpu_offline, NULL, 1);
  387. break;
  388. default:
  389. break;
  390. }
  391. return NOTIFY_OK;
  392. }
  393. static struct notifier_block __cpuinitdata kvm_cpu_notifier = {
  394. .notifier_call = kvm_cpu_notify,
  395. };
  396. #endif
  397. static void __init kvm_apf_trap_init(void)
  398. {
  399. set_intr_gate(14, &async_page_fault);
  400. }
  401. void __init kvm_guest_init(void)
  402. {
  403. int i;
  404. if (!kvm_para_available())
  405. return;
  406. paravirt_ops_setup();
  407. register_reboot_notifier(&kvm_pv_reboot_nb);
  408. for (i = 0; i < KVM_TASK_SLEEP_HASHSIZE; i++)
  409. spin_lock_init(&async_pf_sleepers[i].lock);
  410. if (kvm_para_has_feature(KVM_FEATURE_ASYNC_PF))
  411. x86_init.irqs.trap_init = kvm_apf_trap_init;
  412. if (kvm_para_has_feature(KVM_FEATURE_STEAL_TIME)) {
  413. has_steal_clock = 1;
  414. pv_time_ops.steal_clock = kvm_steal_clock;
  415. }
  416. if (kvm_para_has_feature(KVM_FEATURE_PV_EOI))
  417. apic_set_eoi_write(kvm_guest_apic_eoi_write);
  418. if (kvmclock_vsyscall)
  419. kvm_setup_vsyscall_timeinfo();
  420. #ifdef CONFIG_SMP
  421. smp_ops.smp_prepare_boot_cpu = kvm_smp_prepare_boot_cpu;
  422. register_cpu_notifier(&kvm_cpu_notifier);
  423. #else
  424. kvm_guest_cpu_init();
  425. #endif
  426. }
  427. static bool __init kvm_detect(void)
  428. {
  429. if (!kvm_para_available())
  430. return false;
  431. return true;
  432. }
  433. const struct hypervisor_x86 x86_hyper_kvm __refconst = {
  434. .name = "KVM",
  435. .detect = kvm_detect,
  436. .x2apic_available = kvm_para_available,
  437. };
  438. EXPORT_SYMBOL_GPL(x86_hyper_kvm);
  439. static __init int activate_jump_labels(void)
  440. {
  441. if (has_steal_clock) {
  442. static_key_slow_inc(&paravirt_steal_enabled);
  443. if (steal_acc)
  444. static_key_slow_inc(&paravirt_steal_rq_enabled);
  445. }
  446. return 0;
  447. }
  448. arch_initcall(activate_jump_labels);