kvm.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  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. kvm_async_pf_task_wait((u32)read_cr2());
  220. break;
  221. case KVM_PV_REASON_PAGE_READY:
  222. rcu_irq_enter();
  223. exit_idle();
  224. kvm_async_pf_task_wake((u32)read_cr2());
  225. rcu_irq_exit();
  226. break;
  227. }
  228. }
  229. static void __init paravirt_ops_setup(void)
  230. {
  231. pv_info.name = "KVM";
  232. pv_info.paravirt_enabled = 1;
  233. if (kvm_para_has_feature(KVM_FEATURE_NOP_IO_DELAY))
  234. pv_cpu_ops.io_delay = kvm_io_delay;
  235. #ifdef CONFIG_X86_IO_APIC
  236. no_timer_check = 1;
  237. #endif
  238. }
  239. static void kvm_register_steal_time(void)
  240. {
  241. int cpu = smp_processor_id();
  242. struct kvm_steal_time *st = &per_cpu(steal_time, cpu);
  243. if (!has_steal_clock)
  244. return;
  245. memset(st, 0, sizeof(*st));
  246. wrmsrl(MSR_KVM_STEAL_TIME, (__pa(st) | KVM_MSR_ENABLED));
  247. printk(KERN_INFO "kvm-stealtime: cpu %d, msr %lx\n",
  248. cpu, __pa(st));
  249. }
  250. static DEFINE_PER_CPU(unsigned long, kvm_apic_eoi) = KVM_PV_EOI_DISABLED;
  251. static void kvm_guest_apic_eoi_write(u32 reg, u32 val)
  252. {
  253. /**
  254. * This relies on __test_and_clear_bit to modify the memory
  255. * in a way that is atomic with respect to the local CPU.
  256. * The hypervisor only accesses this memory from the local CPU so
  257. * there's no need for lock or memory barriers.
  258. * An optimization barrier is implied in apic write.
  259. */
  260. if (__test_and_clear_bit(KVM_PV_EOI_BIT, &__get_cpu_var(kvm_apic_eoi)))
  261. return;
  262. apic_write(APIC_EOI, APIC_EOI_ACK);
  263. }
  264. void __cpuinit kvm_guest_cpu_init(void)
  265. {
  266. if (!kvm_para_available())
  267. return;
  268. if (kvm_para_has_feature(KVM_FEATURE_ASYNC_PF) && kvmapf) {
  269. u64 pa = __pa(&__get_cpu_var(apf_reason));
  270. #ifdef CONFIG_PREEMPT
  271. pa |= KVM_ASYNC_PF_SEND_ALWAYS;
  272. #endif
  273. wrmsrl(MSR_KVM_ASYNC_PF_EN, pa | KVM_ASYNC_PF_ENABLED);
  274. __get_cpu_var(apf_reason).enabled = 1;
  275. printk(KERN_INFO"KVM setup async PF for cpu %d\n",
  276. smp_processor_id());
  277. }
  278. if (kvm_para_has_feature(KVM_FEATURE_PV_EOI)) {
  279. unsigned long pa;
  280. /* Size alignment is implied but just to make it explicit. */
  281. BUILD_BUG_ON(__alignof__(kvm_apic_eoi) < 4);
  282. __get_cpu_var(kvm_apic_eoi) = 0;
  283. pa = __pa(&__get_cpu_var(kvm_apic_eoi)) | KVM_MSR_ENABLED;
  284. wrmsrl(MSR_KVM_PV_EOI_EN, pa);
  285. }
  286. if (has_steal_clock)
  287. kvm_register_steal_time();
  288. }
  289. static void kvm_pv_disable_apf(void)
  290. {
  291. if (!__get_cpu_var(apf_reason).enabled)
  292. return;
  293. wrmsrl(MSR_KVM_ASYNC_PF_EN, 0);
  294. __get_cpu_var(apf_reason).enabled = 0;
  295. printk(KERN_INFO"Unregister pv shared memory for cpu %d\n",
  296. smp_processor_id());
  297. }
  298. static void kvm_pv_guest_cpu_reboot(void *unused)
  299. {
  300. /*
  301. * We disable PV EOI before we load a new kernel by kexec,
  302. * since MSR_KVM_PV_EOI_EN stores a pointer into old kernel's memory.
  303. * New kernel can re-enable when it boots.
  304. */
  305. if (kvm_para_has_feature(KVM_FEATURE_PV_EOI))
  306. wrmsrl(MSR_KVM_PV_EOI_EN, 0);
  307. kvm_pv_disable_apf();
  308. kvm_disable_steal_time();
  309. }
  310. static int kvm_pv_reboot_notify(struct notifier_block *nb,
  311. unsigned long code, void *unused)
  312. {
  313. if (code == SYS_RESTART)
  314. on_each_cpu(kvm_pv_guest_cpu_reboot, NULL, 1);
  315. return NOTIFY_DONE;
  316. }
  317. static struct notifier_block kvm_pv_reboot_nb = {
  318. .notifier_call = kvm_pv_reboot_notify,
  319. };
  320. static u64 kvm_steal_clock(int cpu)
  321. {
  322. u64 steal;
  323. struct kvm_steal_time *src;
  324. int version;
  325. src = &per_cpu(steal_time, cpu);
  326. do {
  327. version = src->version;
  328. rmb();
  329. steal = src->steal;
  330. rmb();
  331. } while ((version & 1) || (version != src->version));
  332. return steal;
  333. }
  334. void kvm_disable_steal_time(void)
  335. {
  336. if (!has_steal_clock)
  337. return;
  338. wrmsr(MSR_KVM_STEAL_TIME, 0, 0);
  339. }
  340. #ifdef CONFIG_SMP
  341. static void __init kvm_smp_prepare_boot_cpu(void)
  342. {
  343. WARN_ON(kvm_register_clock("primary cpu clock"));
  344. kvm_guest_cpu_init();
  345. native_smp_prepare_boot_cpu();
  346. }
  347. static void __cpuinit kvm_guest_cpu_online(void *dummy)
  348. {
  349. kvm_guest_cpu_init();
  350. }
  351. static void kvm_guest_cpu_offline(void *dummy)
  352. {
  353. kvm_disable_steal_time();
  354. if (kvm_para_has_feature(KVM_FEATURE_PV_EOI))
  355. wrmsrl(MSR_KVM_PV_EOI_EN, 0);
  356. kvm_pv_disable_apf();
  357. apf_task_wake_all();
  358. }
  359. static int __cpuinit kvm_cpu_notify(struct notifier_block *self,
  360. unsigned long action, void *hcpu)
  361. {
  362. int cpu = (unsigned long)hcpu;
  363. switch (action) {
  364. case CPU_ONLINE:
  365. case CPU_DOWN_FAILED:
  366. case CPU_ONLINE_FROZEN:
  367. smp_call_function_single(cpu, kvm_guest_cpu_online, NULL, 0);
  368. break;
  369. case CPU_DOWN_PREPARE:
  370. case CPU_DOWN_PREPARE_FROZEN:
  371. smp_call_function_single(cpu, kvm_guest_cpu_offline, NULL, 1);
  372. break;
  373. default:
  374. break;
  375. }
  376. return NOTIFY_OK;
  377. }
  378. static struct notifier_block __cpuinitdata kvm_cpu_notifier = {
  379. .notifier_call = kvm_cpu_notify,
  380. };
  381. #endif
  382. static void __init kvm_apf_trap_init(void)
  383. {
  384. set_intr_gate(14, &async_page_fault);
  385. }
  386. void __init kvm_guest_init(void)
  387. {
  388. int i;
  389. if (!kvm_para_available())
  390. return;
  391. paravirt_ops_setup();
  392. register_reboot_notifier(&kvm_pv_reboot_nb);
  393. for (i = 0; i < KVM_TASK_SLEEP_HASHSIZE; i++)
  394. spin_lock_init(&async_pf_sleepers[i].lock);
  395. if (kvm_para_has_feature(KVM_FEATURE_ASYNC_PF))
  396. x86_init.irqs.trap_init = kvm_apf_trap_init;
  397. if (kvm_para_has_feature(KVM_FEATURE_STEAL_TIME)) {
  398. has_steal_clock = 1;
  399. pv_time_ops.steal_clock = kvm_steal_clock;
  400. }
  401. if (kvm_para_has_feature(KVM_FEATURE_PV_EOI))
  402. apic_set_eoi_write(kvm_guest_apic_eoi_write);
  403. #ifdef CONFIG_SMP
  404. smp_ops.smp_prepare_boot_cpu = kvm_smp_prepare_boot_cpu;
  405. register_cpu_notifier(&kvm_cpu_notifier);
  406. #else
  407. kvm_guest_cpu_init();
  408. #endif
  409. }
  410. static bool __init kvm_detect(void)
  411. {
  412. if (!kvm_para_available())
  413. return false;
  414. return true;
  415. }
  416. const struct hypervisor_x86 x86_hyper_kvm __refconst = {
  417. .name = "KVM",
  418. .detect = kvm_detect,
  419. };
  420. EXPORT_SYMBOL_GPL(x86_hyper_kvm);
  421. static __init int activate_jump_labels(void)
  422. {
  423. if (has_steal_clock) {
  424. static_key_slow_inc(&paravirt_steal_enabled);
  425. if (steal_acc)
  426. static_key_slow_inc(&paravirt_steal_rq_enabled);
  427. }
  428. return 0;
  429. }
  430. arch_initcall(activate_jump_labels);