kvm.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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 <asm/timer.h>
  32. #include <asm/cpu.h>
  33. #define MMU_QUEUE_SIZE 1024
  34. static int kvmapf = 1;
  35. static int parse_no_kvmapf(char *arg)
  36. {
  37. kvmapf = 0;
  38. return 0;
  39. }
  40. early_param("no-kvmapf", parse_no_kvmapf);
  41. struct kvm_para_state {
  42. u8 mmu_queue[MMU_QUEUE_SIZE];
  43. int mmu_queue_len;
  44. };
  45. static DEFINE_PER_CPU(struct kvm_para_state, para_state);
  46. static DEFINE_PER_CPU(struct kvm_vcpu_pv_apf_data, apf_reason) __aligned(64);
  47. static struct kvm_para_state *kvm_para_state(void)
  48. {
  49. return &per_cpu(para_state, raw_smp_processor_id());
  50. }
  51. /*
  52. * No need for any "IO delay" on KVM
  53. */
  54. static void kvm_io_delay(void)
  55. {
  56. }
  57. static void kvm_mmu_op(void *buffer, unsigned len)
  58. {
  59. int r;
  60. unsigned long a1, a2;
  61. do {
  62. a1 = __pa(buffer);
  63. a2 = 0; /* on i386 __pa() always returns <4G */
  64. r = kvm_hypercall3(KVM_HC_MMU_OP, len, a1, a2);
  65. buffer += r;
  66. len -= r;
  67. } while (len);
  68. }
  69. static void mmu_queue_flush(struct kvm_para_state *state)
  70. {
  71. if (state->mmu_queue_len) {
  72. kvm_mmu_op(state->mmu_queue, state->mmu_queue_len);
  73. state->mmu_queue_len = 0;
  74. }
  75. }
  76. static void kvm_deferred_mmu_op(void *buffer, int len)
  77. {
  78. struct kvm_para_state *state = kvm_para_state();
  79. if (paravirt_get_lazy_mode() != PARAVIRT_LAZY_MMU) {
  80. kvm_mmu_op(buffer, len);
  81. return;
  82. }
  83. if (state->mmu_queue_len + len > sizeof state->mmu_queue)
  84. mmu_queue_flush(state);
  85. memcpy(state->mmu_queue + state->mmu_queue_len, buffer, len);
  86. state->mmu_queue_len += len;
  87. }
  88. static void kvm_mmu_write(void *dest, u64 val)
  89. {
  90. __u64 pte_phys;
  91. struct kvm_mmu_op_write_pte wpte;
  92. #ifdef CONFIG_HIGHPTE
  93. struct page *page;
  94. unsigned long dst = (unsigned long) dest;
  95. page = kmap_atomic_to_page(dest);
  96. pte_phys = page_to_pfn(page);
  97. pte_phys <<= PAGE_SHIFT;
  98. pte_phys += (dst & ~(PAGE_MASK));
  99. #else
  100. pte_phys = (unsigned long)__pa(dest);
  101. #endif
  102. wpte.header.op = KVM_MMU_OP_WRITE_PTE;
  103. wpte.pte_val = val;
  104. wpte.pte_phys = pte_phys;
  105. kvm_deferred_mmu_op(&wpte, sizeof wpte);
  106. }
  107. /*
  108. * We only need to hook operations that are MMU writes. We hook these so that
  109. * we can use lazy MMU mode to batch these operations. We could probably
  110. * improve the performance of the host code if we used some of the information
  111. * here to simplify processing of batched writes.
  112. */
  113. static void kvm_set_pte(pte_t *ptep, pte_t pte)
  114. {
  115. kvm_mmu_write(ptep, pte_val(pte));
  116. }
  117. static void kvm_set_pte_at(struct mm_struct *mm, unsigned long addr,
  118. pte_t *ptep, pte_t pte)
  119. {
  120. kvm_mmu_write(ptep, pte_val(pte));
  121. }
  122. static void kvm_set_pmd(pmd_t *pmdp, pmd_t pmd)
  123. {
  124. kvm_mmu_write(pmdp, pmd_val(pmd));
  125. }
  126. #if PAGETABLE_LEVELS >= 3
  127. #ifdef CONFIG_X86_PAE
  128. static void kvm_set_pte_atomic(pte_t *ptep, pte_t pte)
  129. {
  130. kvm_mmu_write(ptep, pte_val(pte));
  131. }
  132. static void kvm_pte_clear(struct mm_struct *mm,
  133. unsigned long addr, pte_t *ptep)
  134. {
  135. kvm_mmu_write(ptep, 0);
  136. }
  137. static void kvm_pmd_clear(pmd_t *pmdp)
  138. {
  139. kvm_mmu_write(pmdp, 0);
  140. }
  141. #endif
  142. static void kvm_set_pud(pud_t *pudp, pud_t pud)
  143. {
  144. kvm_mmu_write(pudp, pud_val(pud));
  145. }
  146. #if PAGETABLE_LEVELS == 4
  147. static void kvm_set_pgd(pgd_t *pgdp, pgd_t pgd)
  148. {
  149. kvm_mmu_write(pgdp, pgd_val(pgd));
  150. }
  151. #endif
  152. #endif /* PAGETABLE_LEVELS >= 3 */
  153. static void kvm_flush_tlb(void)
  154. {
  155. struct kvm_mmu_op_flush_tlb ftlb = {
  156. .header.op = KVM_MMU_OP_FLUSH_TLB,
  157. };
  158. kvm_deferred_mmu_op(&ftlb, sizeof ftlb);
  159. }
  160. static void kvm_release_pt(unsigned long pfn)
  161. {
  162. struct kvm_mmu_op_release_pt rpt = {
  163. .header.op = KVM_MMU_OP_RELEASE_PT,
  164. .pt_phys = (u64)pfn << PAGE_SHIFT,
  165. };
  166. kvm_mmu_op(&rpt, sizeof rpt);
  167. }
  168. static void kvm_enter_lazy_mmu(void)
  169. {
  170. paravirt_enter_lazy_mmu();
  171. }
  172. static void kvm_leave_lazy_mmu(void)
  173. {
  174. struct kvm_para_state *state = kvm_para_state();
  175. mmu_queue_flush(state);
  176. paravirt_leave_lazy_mmu();
  177. }
  178. static void __init paravirt_ops_setup(void)
  179. {
  180. pv_info.name = "KVM";
  181. pv_info.paravirt_enabled = 1;
  182. if (kvm_para_has_feature(KVM_FEATURE_NOP_IO_DELAY))
  183. pv_cpu_ops.io_delay = kvm_io_delay;
  184. if (kvm_para_has_feature(KVM_FEATURE_MMU_OP)) {
  185. pv_mmu_ops.set_pte = kvm_set_pte;
  186. pv_mmu_ops.set_pte_at = kvm_set_pte_at;
  187. pv_mmu_ops.set_pmd = kvm_set_pmd;
  188. #if PAGETABLE_LEVELS >= 3
  189. #ifdef CONFIG_X86_PAE
  190. pv_mmu_ops.set_pte_atomic = kvm_set_pte_atomic;
  191. pv_mmu_ops.pte_clear = kvm_pte_clear;
  192. pv_mmu_ops.pmd_clear = kvm_pmd_clear;
  193. #endif
  194. pv_mmu_ops.set_pud = kvm_set_pud;
  195. #if PAGETABLE_LEVELS == 4
  196. pv_mmu_ops.set_pgd = kvm_set_pgd;
  197. #endif
  198. #endif
  199. pv_mmu_ops.flush_tlb_user = kvm_flush_tlb;
  200. pv_mmu_ops.release_pte = kvm_release_pt;
  201. pv_mmu_ops.release_pmd = kvm_release_pt;
  202. pv_mmu_ops.release_pud = kvm_release_pt;
  203. pv_mmu_ops.lazy_mode.enter = kvm_enter_lazy_mmu;
  204. pv_mmu_ops.lazy_mode.leave = kvm_leave_lazy_mmu;
  205. }
  206. #ifdef CONFIG_X86_IO_APIC
  207. no_timer_check = 1;
  208. #endif
  209. }
  210. void __cpuinit kvm_guest_cpu_init(void)
  211. {
  212. if (!kvm_para_available())
  213. return;
  214. if (kvm_para_has_feature(KVM_FEATURE_ASYNC_PF) && kvmapf) {
  215. u64 pa = __pa(&__get_cpu_var(apf_reason));
  216. wrmsrl(MSR_KVM_ASYNC_PF_EN, pa | KVM_ASYNC_PF_ENABLED);
  217. __get_cpu_var(apf_reason).enabled = 1;
  218. printk(KERN_INFO"KVM setup async PF for cpu %d\n",
  219. smp_processor_id());
  220. }
  221. }
  222. static void kvm_pv_disable_apf(void *unused)
  223. {
  224. if (!__get_cpu_var(apf_reason).enabled)
  225. return;
  226. wrmsrl(MSR_KVM_ASYNC_PF_EN, 0);
  227. __get_cpu_var(apf_reason).enabled = 0;
  228. printk(KERN_INFO"Unregister pv shared memory for cpu %d\n",
  229. smp_processor_id());
  230. }
  231. static int kvm_pv_reboot_notify(struct notifier_block *nb,
  232. unsigned long code, void *unused)
  233. {
  234. if (code == SYS_RESTART)
  235. on_each_cpu(kvm_pv_disable_apf, NULL, 1);
  236. return NOTIFY_DONE;
  237. }
  238. static struct notifier_block kvm_pv_reboot_nb = {
  239. .notifier_call = kvm_pv_reboot_notify,
  240. };
  241. #ifdef CONFIG_SMP
  242. static void __init kvm_smp_prepare_boot_cpu(void)
  243. {
  244. WARN_ON(kvm_register_clock("primary cpu clock"));
  245. kvm_guest_cpu_init();
  246. native_smp_prepare_boot_cpu();
  247. }
  248. static void kvm_guest_cpu_online(void *dummy)
  249. {
  250. kvm_guest_cpu_init();
  251. }
  252. static void kvm_guest_cpu_offline(void *dummy)
  253. {
  254. kvm_pv_disable_apf(NULL);
  255. }
  256. static int __cpuinit kvm_cpu_notify(struct notifier_block *self,
  257. unsigned long action, void *hcpu)
  258. {
  259. int cpu = (unsigned long)hcpu;
  260. switch (action) {
  261. case CPU_ONLINE:
  262. case CPU_DOWN_FAILED:
  263. case CPU_ONLINE_FROZEN:
  264. smp_call_function_single(cpu, kvm_guest_cpu_online, NULL, 0);
  265. break;
  266. case CPU_DOWN_PREPARE:
  267. case CPU_DOWN_PREPARE_FROZEN:
  268. smp_call_function_single(cpu, kvm_guest_cpu_offline, NULL, 1);
  269. break;
  270. default:
  271. break;
  272. }
  273. return NOTIFY_OK;
  274. }
  275. static struct notifier_block __cpuinitdata kvm_cpu_notifier = {
  276. .notifier_call = kvm_cpu_notify,
  277. };
  278. #endif
  279. void __init kvm_guest_init(void)
  280. {
  281. if (!kvm_para_available())
  282. return;
  283. paravirt_ops_setup();
  284. register_reboot_notifier(&kvm_pv_reboot_nb);
  285. #ifdef CONFIG_SMP
  286. smp_ops.smp_prepare_boot_cpu = kvm_smp_prepare_boot_cpu;
  287. register_cpu_notifier(&kvm_cpu_notifier);
  288. #else
  289. kvm_guest_cpu_init();
  290. #endif
  291. }