kvm.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  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, (__pa(st) | KVM_MSR_ENABLED));
  260. printk(KERN_INFO "kvm-stealtime: cpu %d, msr %lx\n",
  261. cpu, __pa(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 = __pa(&__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 = __pa(&__get_cpu_var(kvm_apic_eoi)) | KVM_MSR_ENABLED;
  297. wrmsrl(MSR_KVM_PV_EOI_EN, pa);
  298. }
  299. if (has_steal_clock)
  300. kvm_register_steal_time();
  301. }
  302. static void kvm_pv_disable_apf(void)
  303. {
  304. if (!__get_cpu_var(apf_reason).enabled)
  305. return;
  306. wrmsrl(MSR_KVM_ASYNC_PF_EN, 0);
  307. __get_cpu_var(apf_reason).enabled = 0;
  308. printk(KERN_INFO"Unregister pv shared memory for cpu %d\n",
  309. smp_processor_id());
  310. }
  311. static void kvm_pv_guest_cpu_reboot(void *unused)
  312. {
  313. /*
  314. * We disable PV EOI before we load a new kernel by kexec,
  315. * since MSR_KVM_PV_EOI_EN stores a pointer into old kernel's memory.
  316. * New kernel can re-enable when it boots.
  317. */
  318. if (kvm_para_has_feature(KVM_FEATURE_PV_EOI))
  319. wrmsrl(MSR_KVM_PV_EOI_EN, 0);
  320. kvm_pv_disable_apf();
  321. kvm_disable_steal_time();
  322. }
  323. static int kvm_pv_reboot_notify(struct notifier_block *nb,
  324. unsigned long code, void *unused)
  325. {
  326. if (code == SYS_RESTART)
  327. on_each_cpu(kvm_pv_guest_cpu_reboot, NULL, 1);
  328. return NOTIFY_DONE;
  329. }
  330. static struct notifier_block kvm_pv_reboot_nb = {
  331. .notifier_call = kvm_pv_reboot_notify,
  332. };
  333. static u64 kvm_steal_clock(int cpu)
  334. {
  335. u64 steal;
  336. struct kvm_steal_time *src;
  337. int version;
  338. src = &per_cpu(steal_time, cpu);
  339. do {
  340. version = src->version;
  341. rmb();
  342. steal = src->steal;
  343. rmb();
  344. } while ((version & 1) || (version != src->version));
  345. return steal;
  346. }
  347. void kvm_disable_steal_time(void)
  348. {
  349. if (!has_steal_clock)
  350. return;
  351. wrmsr(MSR_KVM_STEAL_TIME, 0, 0);
  352. }
  353. #ifdef CONFIG_SMP
  354. static void __init kvm_smp_prepare_boot_cpu(void)
  355. {
  356. WARN_ON(kvm_register_clock("primary cpu clock"));
  357. kvm_guest_cpu_init();
  358. native_smp_prepare_boot_cpu();
  359. }
  360. static void __cpuinit kvm_guest_cpu_online(void *dummy)
  361. {
  362. kvm_guest_cpu_init();
  363. }
  364. static void kvm_guest_cpu_offline(void *dummy)
  365. {
  366. kvm_disable_steal_time();
  367. if (kvm_para_has_feature(KVM_FEATURE_PV_EOI))
  368. wrmsrl(MSR_KVM_PV_EOI_EN, 0);
  369. kvm_pv_disable_apf();
  370. apf_task_wake_all();
  371. }
  372. static int __cpuinit kvm_cpu_notify(struct notifier_block *self,
  373. unsigned long action, void *hcpu)
  374. {
  375. int cpu = (unsigned long)hcpu;
  376. switch (action) {
  377. case CPU_ONLINE:
  378. case CPU_DOWN_FAILED:
  379. case CPU_ONLINE_FROZEN:
  380. smp_call_function_single(cpu, kvm_guest_cpu_online, NULL, 0);
  381. break;
  382. case CPU_DOWN_PREPARE:
  383. case CPU_DOWN_PREPARE_FROZEN:
  384. smp_call_function_single(cpu, kvm_guest_cpu_offline, NULL, 1);
  385. break;
  386. default:
  387. break;
  388. }
  389. return NOTIFY_OK;
  390. }
  391. static struct notifier_block __cpuinitdata kvm_cpu_notifier = {
  392. .notifier_call = kvm_cpu_notify,
  393. };
  394. #endif
  395. static void __init kvm_apf_trap_init(void)
  396. {
  397. set_intr_gate(14, &async_page_fault);
  398. }
  399. void __init kvm_guest_init(void)
  400. {
  401. int i;
  402. if (!kvm_para_available())
  403. return;
  404. paravirt_ops_setup();
  405. register_reboot_notifier(&kvm_pv_reboot_nb);
  406. for (i = 0; i < KVM_TASK_SLEEP_HASHSIZE; i++)
  407. spin_lock_init(&async_pf_sleepers[i].lock);
  408. if (kvm_para_has_feature(KVM_FEATURE_ASYNC_PF))
  409. x86_init.irqs.trap_init = kvm_apf_trap_init;
  410. if (kvm_para_has_feature(KVM_FEATURE_STEAL_TIME)) {
  411. has_steal_clock = 1;
  412. pv_time_ops.steal_clock = kvm_steal_clock;
  413. }
  414. if (kvm_para_has_feature(KVM_FEATURE_PV_EOI))
  415. apic_set_eoi_write(kvm_guest_apic_eoi_write);
  416. if (kvmclock_vsyscall)
  417. kvm_setup_vsyscall_timeinfo();
  418. #ifdef CONFIG_SMP
  419. smp_ops.smp_prepare_boot_cpu = kvm_smp_prepare_boot_cpu;
  420. register_cpu_notifier(&kvm_cpu_notifier);
  421. #else
  422. kvm_guest_cpu_init();
  423. #endif
  424. }
  425. static bool __init kvm_detect(void)
  426. {
  427. if (!kvm_para_available())
  428. return false;
  429. return true;
  430. }
  431. const struct hypervisor_x86 x86_hyper_kvm __refconst = {
  432. .name = "KVM",
  433. .detect = kvm_detect,
  434. };
  435. EXPORT_SYMBOL_GPL(x86_hyper_kvm);
  436. static __init int activate_jump_labels(void)
  437. {
  438. if (has_steal_clock) {
  439. static_key_slow_inc(&paravirt_steal_enabled);
  440. if (steal_acc)
  441. static_key_slow_inc(&paravirt_steal_rq_enabled);
  442. }
  443. return 0;
  444. }
  445. arch_initcall(activate_jump_labels);