kvm.c 11 KB

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