kvm.c 12 KB

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