kvm.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  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. #define MMU_QUEUE_SIZE 1024
  40. static int kvmapf = 1;
  41. static int parse_no_kvmapf(char *arg)
  42. {
  43. kvmapf = 0;
  44. return 0;
  45. }
  46. early_param("no-kvmapf", parse_no_kvmapf);
  47. struct kvm_para_state {
  48. u8 mmu_queue[MMU_QUEUE_SIZE];
  49. int mmu_queue_len;
  50. };
  51. static DEFINE_PER_CPU(struct kvm_para_state, para_state);
  52. static DEFINE_PER_CPU(struct kvm_vcpu_pv_apf_data, apf_reason) __aligned(64);
  53. static struct kvm_para_state *kvm_para_state(void)
  54. {
  55. return &per_cpu(para_state, raw_smp_processor_id());
  56. }
  57. /*
  58. * No need for any "IO delay" on KVM
  59. */
  60. static void kvm_io_delay(void)
  61. {
  62. }
  63. #define KVM_TASK_SLEEP_HASHBITS 8
  64. #define KVM_TASK_SLEEP_HASHSIZE (1<<KVM_TASK_SLEEP_HASHBITS)
  65. struct kvm_task_sleep_node {
  66. struct hlist_node link;
  67. wait_queue_head_t wq;
  68. u32 token;
  69. int cpu;
  70. };
  71. static struct kvm_task_sleep_head {
  72. spinlock_t lock;
  73. struct hlist_head list;
  74. } async_pf_sleepers[KVM_TASK_SLEEP_HASHSIZE];
  75. static struct kvm_task_sleep_node *_find_apf_task(struct kvm_task_sleep_head *b,
  76. u32 token)
  77. {
  78. struct hlist_node *p;
  79. hlist_for_each(p, &b->list) {
  80. struct kvm_task_sleep_node *n =
  81. hlist_entry(p, typeof(*n), link);
  82. if (n->token == token)
  83. return n;
  84. }
  85. return NULL;
  86. }
  87. void kvm_async_pf_task_wait(u32 token)
  88. {
  89. u32 key = hash_32(token, KVM_TASK_SLEEP_HASHBITS);
  90. struct kvm_task_sleep_head *b = &async_pf_sleepers[key];
  91. struct kvm_task_sleep_node n, *e;
  92. DEFINE_WAIT(wait);
  93. spin_lock(&b->lock);
  94. e = _find_apf_task(b, token);
  95. if (e) {
  96. /* dummy entry exist -> wake up was delivered ahead of PF */
  97. hlist_del(&e->link);
  98. kfree(e);
  99. spin_unlock(&b->lock);
  100. return;
  101. }
  102. n.token = token;
  103. n.cpu = smp_processor_id();
  104. init_waitqueue_head(&n.wq);
  105. hlist_add_head(&n.link, &b->list);
  106. spin_unlock(&b->lock);
  107. for (;;) {
  108. prepare_to_wait(&n.wq, &wait, TASK_UNINTERRUPTIBLE);
  109. if (hlist_unhashed(&n.link))
  110. break;
  111. local_irq_enable();
  112. schedule();
  113. local_irq_disable();
  114. }
  115. finish_wait(&n.wq, &wait);
  116. return;
  117. }
  118. EXPORT_SYMBOL_GPL(kvm_async_pf_task_wait);
  119. static void apf_task_wake_one(struct kvm_task_sleep_node *n)
  120. {
  121. hlist_del_init(&n->link);
  122. if (waitqueue_active(&n->wq))
  123. wake_up(&n->wq);
  124. }
  125. static void apf_task_wake_all(void)
  126. {
  127. int i;
  128. for (i = 0; i < KVM_TASK_SLEEP_HASHSIZE; i++) {
  129. struct hlist_node *p, *next;
  130. struct kvm_task_sleep_head *b = &async_pf_sleepers[i];
  131. spin_lock(&b->lock);
  132. hlist_for_each_safe(p, next, &b->list) {
  133. struct kvm_task_sleep_node *n =
  134. hlist_entry(p, typeof(*n), link);
  135. if (n->cpu == smp_processor_id())
  136. apf_task_wake_one(n);
  137. }
  138. spin_unlock(&b->lock);
  139. }
  140. }
  141. void kvm_async_pf_task_wake(u32 token)
  142. {
  143. u32 key = hash_32(token, KVM_TASK_SLEEP_HASHBITS);
  144. struct kvm_task_sleep_head *b = &async_pf_sleepers[key];
  145. struct kvm_task_sleep_node *n;
  146. if (token == ~0) {
  147. apf_task_wake_all();
  148. return;
  149. }
  150. again:
  151. spin_lock(&b->lock);
  152. n = _find_apf_task(b, token);
  153. if (!n) {
  154. /*
  155. * async PF was not yet handled.
  156. * Add dummy entry for the token.
  157. */
  158. n = kmalloc(sizeof(*n), GFP_ATOMIC);
  159. if (!n) {
  160. /*
  161. * Allocation failed! Busy wait while other cpu
  162. * handles async PF.
  163. */
  164. spin_unlock(&b->lock);
  165. cpu_relax();
  166. goto again;
  167. }
  168. n->token = token;
  169. n->cpu = smp_processor_id();
  170. init_waitqueue_head(&n->wq);
  171. hlist_add_head(&n->link, &b->list);
  172. } else
  173. apf_task_wake_one(n);
  174. spin_unlock(&b->lock);
  175. return;
  176. }
  177. EXPORT_SYMBOL_GPL(kvm_async_pf_task_wake);
  178. u32 kvm_read_and_reset_pf_reason(void)
  179. {
  180. u32 reason = 0;
  181. if (__get_cpu_var(apf_reason).enabled) {
  182. reason = __get_cpu_var(apf_reason).reason;
  183. __get_cpu_var(apf_reason).reason = 0;
  184. }
  185. return reason;
  186. }
  187. EXPORT_SYMBOL_GPL(kvm_read_and_reset_pf_reason);
  188. dotraplinkage void __kprobes
  189. do_async_page_fault(struct pt_regs *regs, unsigned long error_code)
  190. {
  191. switch (kvm_read_and_reset_pf_reason()) {
  192. default:
  193. do_page_fault(regs, error_code);
  194. break;
  195. case KVM_PV_REASON_PAGE_NOT_PRESENT:
  196. /* page is swapped out by the host. */
  197. kvm_async_pf_task_wait((u32)read_cr2());
  198. break;
  199. case KVM_PV_REASON_PAGE_READY:
  200. kvm_async_pf_task_wake((u32)read_cr2());
  201. break;
  202. }
  203. }
  204. static void kvm_mmu_op(void *buffer, unsigned len)
  205. {
  206. int r;
  207. unsigned long a1, a2;
  208. do {
  209. a1 = __pa(buffer);
  210. a2 = 0; /* on i386 __pa() always returns <4G */
  211. r = kvm_hypercall3(KVM_HC_MMU_OP, len, a1, a2);
  212. buffer += r;
  213. len -= r;
  214. } while (len);
  215. }
  216. static void mmu_queue_flush(struct kvm_para_state *state)
  217. {
  218. if (state->mmu_queue_len) {
  219. kvm_mmu_op(state->mmu_queue, state->mmu_queue_len);
  220. state->mmu_queue_len = 0;
  221. }
  222. }
  223. static void kvm_deferred_mmu_op(void *buffer, int len)
  224. {
  225. struct kvm_para_state *state = kvm_para_state();
  226. if (paravirt_get_lazy_mode() != PARAVIRT_LAZY_MMU) {
  227. kvm_mmu_op(buffer, len);
  228. return;
  229. }
  230. if (state->mmu_queue_len + len > sizeof state->mmu_queue)
  231. mmu_queue_flush(state);
  232. memcpy(state->mmu_queue + state->mmu_queue_len, buffer, len);
  233. state->mmu_queue_len += len;
  234. }
  235. static void kvm_mmu_write(void *dest, u64 val)
  236. {
  237. __u64 pte_phys;
  238. struct kvm_mmu_op_write_pte wpte;
  239. #ifdef CONFIG_HIGHPTE
  240. struct page *page;
  241. unsigned long dst = (unsigned long) dest;
  242. page = kmap_atomic_to_page(dest);
  243. pte_phys = page_to_pfn(page);
  244. pte_phys <<= PAGE_SHIFT;
  245. pte_phys += (dst & ~(PAGE_MASK));
  246. #else
  247. pte_phys = (unsigned long)__pa(dest);
  248. #endif
  249. wpte.header.op = KVM_MMU_OP_WRITE_PTE;
  250. wpte.pte_val = val;
  251. wpte.pte_phys = pte_phys;
  252. kvm_deferred_mmu_op(&wpte, sizeof wpte);
  253. }
  254. /*
  255. * We only need to hook operations that are MMU writes. We hook these so that
  256. * we can use lazy MMU mode to batch these operations. We could probably
  257. * improve the performance of the host code if we used some of the information
  258. * here to simplify processing of batched writes.
  259. */
  260. static void kvm_set_pte(pte_t *ptep, pte_t pte)
  261. {
  262. kvm_mmu_write(ptep, pte_val(pte));
  263. }
  264. static void kvm_set_pte_at(struct mm_struct *mm, unsigned long addr,
  265. pte_t *ptep, pte_t pte)
  266. {
  267. kvm_mmu_write(ptep, pte_val(pte));
  268. }
  269. static void kvm_set_pmd(pmd_t *pmdp, pmd_t pmd)
  270. {
  271. kvm_mmu_write(pmdp, pmd_val(pmd));
  272. }
  273. #if PAGETABLE_LEVELS >= 3
  274. #ifdef CONFIG_X86_PAE
  275. static void kvm_set_pte_atomic(pte_t *ptep, pte_t pte)
  276. {
  277. kvm_mmu_write(ptep, pte_val(pte));
  278. }
  279. static void kvm_pte_clear(struct mm_struct *mm,
  280. unsigned long addr, pte_t *ptep)
  281. {
  282. kvm_mmu_write(ptep, 0);
  283. }
  284. static void kvm_pmd_clear(pmd_t *pmdp)
  285. {
  286. kvm_mmu_write(pmdp, 0);
  287. }
  288. #endif
  289. static void kvm_set_pud(pud_t *pudp, pud_t pud)
  290. {
  291. kvm_mmu_write(pudp, pud_val(pud));
  292. }
  293. #if PAGETABLE_LEVELS == 4
  294. static void kvm_set_pgd(pgd_t *pgdp, pgd_t pgd)
  295. {
  296. kvm_mmu_write(pgdp, pgd_val(pgd));
  297. }
  298. #endif
  299. #endif /* PAGETABLE_LEVELS >= 3 */
  300. static void kvm_flush_tlb(void)
  301. {
  302. struct kvm_mmu_op_flush_tlb ftlb = {
  303. .header.op = KVM_MMU_OP_FLUSH_TLB,
  304. };
  305. kvm_deferred_mmu_op(&ftlb, sizeof ftlb);
  306. }
  307. static void kvm_release_pt(unsigned long pfn)
  308. {
  309. struct kvm_mmu_op_release_pt rpt = {
  310. .header.op = KVM_MMU_OP_RELEASE_PT,
  311. .pt_phys = (u64)pfn << PAGE_SHIFT,
  312. };
  313. kvm_mmu_op(&rpt, sizeof rpt);
  314. }
  315. static void kvm_enter_lazy_mmu(void)
  316. {
  317. paravirt_enter_lazy_mmu();
  318. }
  319. static void kvm_leave_lazy_mmu(void)
  320. {
  321. struct kvm_para_state *state = kvm_para_state();
  322. mmu_queue_flush(state);
  323. paravirt_leave_lazy_mmu();
  324. }
  325. static void __init paravirt_ops_setup(void)
  326. {
  327. pv_info.name = "KVM";
  328. pv_info.paravirt_enabled = 1;
  329. if (kvm_para_has_feature(KVM_FEATURE_NOP_IO_DELAY))
  330. pv_cpu_ops.io_delay = kvm_io_delay;
  331. if (kvm_para_has_feature(KVM_FEATURE_MMU_OP)) {
  332. pv_mmu_ops.set_pte = kvm_set_pte;
  333. pv_mmu_ops.set_pte_at = kvm_set_pte_at;
  334. pv_mmu_ops.set_pmd = kvm_set_pmd;
  335. #if PAGETABLE_LEVELS >= 3
  336. #ifdef CONFIG_X86_PAE
  337. pv_mmu_ops.set_pte_atomic = kvm_set_pte_atomic;
  338. pv_mmu_ops.pte_clear = kvm_pte_clear;
  339. pv_mmu_ops.pmd_clear = kvm_pmd_clear;
  340. #endif
  341. pv_mmu_ops.set_pud = kvm_set_pud;
  342. #if PAGETABLE_LEVELS == 4
  343. pv_mmu_ops.set_pgd = kvm_set_pgd;
  344. #endif
  345. #endif
  346. pv_mmu_ops.flush_tlb_user = kvm_flush_tlb;
  347. pv_mmu_ops.release_pte = kvm_release_pt;
  348. pv_mmu_ops.release_pmd = kvm_release_pt;
  349. pv_mmu_ops.release_pud = kvm_release_pt;
  350. pv_mmu_ops.lazy_mode.enter = kvm_enter_lazy_mmu;
  351. pv_mmu_ops.lazy_mode.leave = kvm_leave_lazy_mmu;
  352. }
  353. #ifdef CONFIG_X86_IO_APIC
  354. no_timer_check = 1;
  355. #endif
  356. }
  357. void __cpuinit kvm_guest_cpu_init(void)
  358. {
  359. if (!kvm_para_available())
  360. return;
  361. if (kvm_para_has_feature(KVM_FEATURE_ASYNC_PF) && kvmapf) {
  362. u64 pa = __pa(&__get_cpu_var(apf_reason));
  363. wrmsrl(MSR_KVM_ASYNC_PF_EN, pa | KVM_ASYNC_PF_ENABLED);
  364. __get_cpu_var(apf_reason).enabled = 1;
  365. printk(KERN_INFO"KVM setup async PF for cpu %d\n",
  366. smp_processor_id());
  367. }
  368. }
  369. static void kvm_pv_disable_apf(void *unused)
  370. {
  371. if (!__get_cpu_var(apf_reason).enabled)
  372. return;
  373. wrmsrl(MSR_KVM_ASYNC_PF_EN, 0);
  374. __get_cpu_var(apf_reason).enabled = 0;
  375. printk(KERN_INFO"Unregister pv shared memory for cpu %d\n",
  376. smp_processor_id());
  377. }
  378. static int kvm_pv_reboot_notify(struct notifier_block *nb,
  379. unsigned long code, void *unused)
  380. {
  381. if (code == SYS_RESTART)
  382. on_each_cpu(kvm_pv_disable_apf, NULL, 1);
  383. return NOTIFY_DONE;
  384. }
  385. static struct notifier_block kvm_pv_reboot_nb = {
  386. .notifier_call = kvm_pv_reboot_notify,
  387. };
  388. #ifdef CONFIG_SMP
  389. static void __init kvm_smp_prepare_boot_cpu(void)
  390. {
  391. WARN_ON(kvm_register_clock("primary cpu clock"));
  392. kvm_guest_cpu_init();
  393. native_smp_prepare_boot_cpu();
  394. }
  395. static void kvm_guest_cpu_online(void *dummy)
  396. {
  397. kvm_guest_cpu_init();
  398. }
  399. static void kvm_guest_cpu_offline(void *dummy)
  400. {
  401. kvm_pv_disable_apf(NULL);
  402. apf_task_wake_all();
  403. }
  404. static int __cpuinit kvm_cpu_notify(struct notifier_block *self,
  405. unsigned long action, void *hcpu)
  406. {
  407. int cpu = (unsigned long)hcpu;
  408. switch (action) {
  409. case CPU_ONLINE:
  410. case CPU_DOWN_FAILED:
  411. case CPU_ONLINE_FROZEN:
  412. smp_call_function_single(cpu, kvm_guest_cpu_online, NULL, 0);
  413. break;
  414. case CPU_DOWN_PREPARE:
  415. case CPU_DOWN_PREPARE_FROZEN:
  416. smp_call_function_single(cpu, kvm_guest_cpu_offline, NULL, 1);
  417. break;
  418. default:
  419. break;
  420. }
  421. return NOTIFY_OK;
  422. }
  423. static struct notifier_block __cpuinitdata kvm_cpu_notifier = {
  424. .notifier_call = kvm_cpu_notify,
  425. };
  426. #endif
  427. static void __init kvm_apf_trap_init(void)
  428. {
  429. set_intr_gate(14, &async_page_fault);
  430. }
  431. void __init kvm_guest_init(void)
  432. {
  433. int i;
  434. if (!kvm_para_available())
  435. return;
  436. paravirt_ops_setup();
  437. register_reboot_notifier(&kvm_pv_reboot_nb);
  438. for (i = 0; i < KVM_TASK_SLEEP_HASHSIZE; i++)
  439. spin_lock_init(&async_pf_sleepers[i].lock);
  440. if (kvm_para_has_feature(KVM_FEATURE_ASYNC_PF))
  441. x86_init.irqs.trap_init = kvm_apf_trap_init;
  442. #ifdef CONFIG_SMP
  443. smp_ops.smp_prepare_boot_cpu = kvm_smp_prepare_boot_cpu;
  444. register_cpu_notifier(&kvm_cpu_notifier);
  445. #else
  446. kvm_guest_cpu_init();
  447. #endif
  448. }