kvm.c 14 KB

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