kvm.c 12 KB

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