kvm.c 12 KB

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