kvm.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. /*
  2. * Copyright (C) 2010 SUSE Linux Products GmbH. All rights reserved.
  3. *
  4. * Authors:
  5. * Alexander Graf <agraf@suse.de>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License, version 2, as
  9. * published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. #include <linux/kvm_host.h>
  21. #include <linux/init.h>
  22. #include <linux/kvm_para.h>
  23. #include <linux/slab.h>
  24. #include <linux/of.h>
  25. #include <asm/reg.h>
  26. #include <asm/kvm_ppc.h>
  27. #include <asm/sections.h>
  28. #include <asm/cacheflush.h>
  29. #include <asm/disassemble.h>
  30. #define KVM_MAGIC_PAGE (-4096L)
  31. #define magic_var(x) KVM_MAGIC_PAGE + offsetof(struct kvm_vcpu_arch_shared, x)
  32. #define KVM_INST_LWZ 0x80000000
  33. #define KVM_INST_STW 0x90000000
  34. #define KVM_INST_LD 0xe8000000
  35. #define KVM_INST_STD 0xf8000000
  36. #define KVM_INST_NOP 0x60000000
  37. #define KVM_INST_B 0x48000000
  38. #define KVM_INST_B_MASK 0x03ffffff
  39. #define KVM_INST_B_MAX 0x01ffffff
  40. #define KVM_MASK_RT 0x03e00000
  41. #define KVM_INST_MFMSR 0x7c0000a6
  42. #define KVM_INST_MFSPR_SPRG0 0x7c1042a6
  43. #define KVM_INST_MFSPR_SPRG1 0x7c1142a6
  44. #define KVM_INST_MFSPR_SPRG2 0x7c1242a6
  45. #define KVM_INST_MFSPR_SPRG3 0x7c1342a6
  46. #define KVM_INST_MFSPR_SRR0 0x7c1a02a6
  47. #define KVM_INST_MFSPR_SRR1 0x7c1b02a6
  48. #define KVM_INST_MFSPR_DAR 0x7c1302a6
  49. #define KVM_INST_MFSPR_DSISR 0x7c1202a6
  50. #define KVM_INST_MTSPR_SPRG0 0x7c1043a6
  51. #define KVM_INST_MTSPR_SPRG1 0x7c1143a6
  52. #define KVM_INST_MTSPR_SPRG2 0x7c1243a6
  53. #define KVM_INST_MTSPR_SPRG3 0x7c1343a6
  54. #define KVM_INST_MTSPR_SRR0 0x7c1a03a6
  55. #define KVM_INST_MTSPR_SRR1 0x7c1b03a6
  56. #define KVM_INST_MTSPR_DAR 0x7c1303a6
  57. #define KVM_INST_MTSPR_DSISR 0x7c1203a6
  58. #define KVM_INST_TLBSYNC 0x7c00046c
  59. #define KVM_INST_MTMSRD_L0 0x7c000164
  60. #define KVM_INST_MTMSRD_L1 0x7c010164
  61. #define KVM_INST_MTMSR 0x7c000124
  62. static bool kvm_patching_worked = true;
  63. static char kvm_tmp[1024 * 1024];
  64. static int kvm_tmp_index;
  65. static inline void kvm_patch_ins(u32 *inst, u32 new_inst)
  66. {
  67. *inst = new_inst;
  68. flush_icache_range((ulong)inst, (ulong)inst + 4);
  69. }
  70. static void kvm_patch_ins_ld(u32 *inst, long addr, u32 rt)
  71. {
  72. #ifdef CONFIG_64BIT
  73. kvm_patch_ins(inst, KVM_INST_LD | rt | (addr & 0x0000fffc));
  74. #else
  75. kvm_patch_ins(inst, KVM_INST_LWZ | rt | ((addr + 4) & 0x0000fffc));
  76. #endif
  77. }
  78. static void kvm_patch_ins_lwz(u32 *inst, long addr, u32 rt)
  79. {
  80. kvm_patch_ins(inst, KVM_INST_LWZ | rt | (addr & 0x0000ffff));
  81. }
  82. static void kvm_patch_ins_std(u32 *inst, long addr, u32 rt)
  83. {
  84. #ifdef CONFIG_64BIT
  85. kvm_patch_ins(inst, KVM_INST_STD | rt | (addr & 0x0000fffc));
  86. #else
  87. kvm_patch_ins(inst, KVM_INST_STW | rt | ((addr + 4) & 0x0000fffc));
  88. #endif
  89. }
  90. static void kvm_patch_ins_stw(u32 *inst, long addr, u32 rt)
  91. {
  92. kvm_patch_ins(inst, KVM_INST_STW | rt | (addr & 0x0000fffc));
  93. }
  94. static void kvm_patch_ins_nop(u32 *inst)
  95. {
  96. kvm_patch_ins(inst, KVM_INST_NOP);
  97. }
  98. static void kvm_patch_ins_b(u32 *inst, int addr)
  99. {
  100. #ifdef CONFIG_RELOCATABLE
  101. /* On relocatable kernels interrupts handlers and our code
  102. can be in different regions, so we don't patch them */
  103. extern u32 __end_interrupts;
  104. if ((ulong)inst < (ulong)&__end_interrupts)
  105. return;
  106. #endif
  107. kvm_patch_ins(inst, KVM_INST_B | (addr & KVM_INST_B_MASK));
  108. }
  109. static u32 *kvm_alloc(int len)
  110. {
  111. u32 *p;
  112. if ((kvm_tmp_index + len) > ARRAY_SIZE(kvm_tmp)) {
  113. printk(KERN_ERR "KVM: No more space (%d + %d)\n",
  114. kvm_tmp_index, len);
  115. kvm_patching_worked = false;
  116. return NULL;
  117. }
  118. p = (void*)&kvm_tmp[kvm_tmp_index];
  119. kvm_tmp_index += len;
  120. return p;
  121. }
  122. extern u32 kvm_emulate_mtmsrd_branch_offs;
  123. extern u32 kvm_emulate_mtmsrd_reg_offs;
  124. extern u32 kvm_emulate_mtmsrd_len;
  125. extern u32 kvm_emulate_mtmsrd[];
  126. static void kvm_patch_ins_mtmsrd(u32 *inst, u32 rt)
  127. {
  128. u32 *p;
  129. int distance_start;
  130. int distance_end;
  131. ulong next_inst;
  132. p = kvm_alloc(kvm_emulate_mtmsrd_len * 4);
  133. if (!p)
  134. return;
  135. /* Find out where we are and put everything there */
  136. distance_start = (ulong)p - (ulong)inst;
  137. next_inst = ((ulong)inst + 4);
  138. distance_end = next_inst - (ulong)&p[kvm_emulate_mtmsrd_branch_offs];
  139. /* Make sure we only write valid b instructions */
  140. if (distance_start > KVM_INST_B_MAX) {
  141. kvm_patching_worked = false;
  142. return;
  143. }
  144. /* Modify the chunk to fit the invocation */
  145. memcpy(p, kvm_emulate_mtmsrd, kvm_emulate_mtmsrd_len * 4);
  146. p[kvm_emulate_mtmsrd_branch_offs] |= distance_end & KVM_INST_B_MASK;
  147. p[kvm_emulate_mtmsrd_reg_offs] |= rt;
  148. flush_icache_range((ulong)p, (ulong)p + kvm_emulate_mtmsrd_len * 4);
  149. /* Patch the invocation */
  150. kvm_patch_ins_b(inst, distance_start);
  151. }
  152. extern u32 kvm_emulate_mtmsr_branch_offs;
  153. extern u32 kvm_emulate_mtmsr_reg1_offs;
  154. extern u32 kvm_emulate_mtmsr_reg2_offs;
  155. extern u32 kvm_emulate_mtmsr_reg3_offs;
  156. extern u32 kvm_emulate_mtmsr_orig_ins_offs;
  157. extern u32 kvm_emulate_mtmsr_len;
  158. extern u32 kvm_emulate_mtmsr[];
  159. static void kvm_patch_ins_mtmsr(u32 *inst, u32 rt)
  160. {
  161. u32 *p;
  162. int distance_start;
  163. int distance_end;
  164. ulong next_inst;
  165. p = kvm_alloc(kvm_emulate_mtmsr_len * 4);
  166. if (!p)
  167. return;
  168. /* Find out where we are and put everything there */
  169. distance_start = (ulong)p - (ulong)inst;
  170. next_inst = ((ulong)inst + 4);
  171. distance_end = next_inst - (ulong)&p[kvm_emulate_mtmsr_branch_offs];
  172. /* Make sure we only write valid b instructions */
  173. if (distance_start > KVM_INST_B_MAX) {
  174. kvm_patching_worked = false;
  175. return;
  176. }
  177. /* Modify the chunk to fit the invocation */
  178. memcpy(p, kvm_emulate_mtmsr, kvm_emulate_mtmsr_len * 4);
  179. p[kvm_emulate_mtmsr_branch_offs] |= distance_end & KVM_INST_B_MASK;
  180. p[kvm_emulate_mtmsr_reg1_offs] |= rt;
  181. p[kvm_emulate_mtmsr_reg2_offs] |= rt;
  182. p[kvm_emulate_mtmsr_reg3_offs] |= rt;
  183. p[kvm_emulate_mtmsr_orig_ins_offs] = *inst;
  184. flush_icache_range((ulong)p, (ulong)p + kvm_emulate_mtmsr_len * 4);
  185. /* Patch the invocation */
  186. kvm_patch_ins_b(inst, distance_start);
  187. }
  188. static void kvm_map_magic_page(void *data)
  189. {
  190. kvm_hypercall2(KVM_HC_PPC_MAP_MAGIC_PAGE,
  191. KVM_MAGIC_PAGE, /* Physical Address */
  192. KVM_MAGIC_PAGE); /* Effective Address */
  193. }
  194. static void kvm_check_ins(u32 *inst)
  195. {
  196. u32 _inst = *inst;
  197. u32 inst_no_rt = _inst & ~KVM_MASK_RT;
  198. u32 inst_rt = _inst & KVM_MASK_RT;
  199. switch (inst_no_rt) {
  200. /* Loads */
  201. case KVM_INST_MFMSR:
  202. kvm_patch_ins_ld(inst, magic_var(msr), inst_rt);
  203. break;
  204. case KVM_INST_MFSPR_SPRG0:
  205. kvm_patch_ins_ld(inst, magic_var(sprg0), inst_rt);
  206. break;
  207. case KVM_INST_MFSPR_SPRG1:
  208. kvm_patch_ins_ld(inst, magic_var(sprg1), inst_rt);
  209. break;
  210. case KVM_INST_MFSPR_SPRG2:
  211. kvm_patch_ins_ld(inst, magic_var(sprg2), inst_rt);
  212. break;
  213. case KVM_INST_MFSPR_SPRG3:
  214. kvm_patch_ins_ld(inst, magic_var(sprg3), inst_rt);
  215. break;
  216. case KVM_INST_MFSPR_SRR0:
  217. kvm_patch_ins_ld(inst, magic_var(srr0), inst_rt);
  218. break;
  219. case KVM_INST_MFSPR_SRR1:
  220. kvm_patch_ins_ld(inst, magic_var(srr1), inst_rt);
  221. break;
  222. case KVM_INST_MFSPR_DAR:
  223. kvm_patch_ins_ld(inst, magic_var(dar), inst_rt);
  224. break;
  225. case KVM_INST_MFSPR_DSISR:
  226. kvm_patch_ins_lwz(inst, magic_var(dsisr), inst_rt);
  227. break;
  228. /* Stores */
  229. case KVM_INST_MTSPR_SPRG0:
  230. kvm_patch_ins_std(inst, magic_var(sprg0), inst_rt);
  231. break;
  232. case KVM_INST_MTSPR_SPRG1:
  233. kvm_patch_ins_std(inst, magic_var(sprg1), inst_rt);
  234. break;
  235. case KVM_INST_MTSPR_SPRG2:
  236. kvm_patch_ins_std(inst, magic_var(sprg2), inst_rt);
  237. break;
  238. case KVM_INST_MTSPR_SPRG3:
  239. kvm_patch_ins_std(inst, magic_var(sprg3), inst_rt);
  240. break;
  241. case KVM_INST_MTSPR_SRR0:
  242. kvm_patch_ins_std(inst, magic_var(srr0), inst_rt);
  243. break;
  244. case KVM_INST_MTSPR_SRR1:
  245. kvm_patch_ins_std(inst, magic_var(srr1), inst_rt);
  246. break;
  247. case KVM_INST_MTSPR_DAR:
  248. kvm_patch_ins_std(inst, magic_var(dar), inst_rt);
  249. break;
  250. case KVM_INST_MTSPR_DSISR:
  251. kvm_patch_ins_stw(inst, magic_var(dsisr), inst_rt);
  252. break;
  253. /* Nops */
  254. case KVM_INST_TLBSYNC:
  255. kvm_patch_ins_nop(inst);
  256. break;
  257. /* Rewrites */
  258. case KVM_INST_MTMSRD_L1:
  259. /* We use r30 and r31 during the hook */
  260. if (get_rt(inst_rt) < 30)
  261. kvm_patch_ins_mtmsrd(inst, inst_rt);
  262. break;
  263. case KVM_INST_MTMSR:
  264. case KVM_INST_MTMSRD_L0:
  265. /* We use r30 and r31 during the hook */
  266. if (get_rt(inst_rt) < 30)
  267. kvm_patch_ins_mtmsr(inst, inst_rt);
  268. break;
  269. }
  270. switch (_inst) {
  271. }
  272. }
  273. static void kvm_use_magic_page(void)
  274. {
  275. u32 *p;
  276. u32 *start, *end;
  277. u32 tmp;
  278. /* Tell the host to map the magic page to -4096 on all CPUs */
  279. on_each_cpu(kvm_map_magic_page, NULL, 1);
  280. /* Quick self-test to see if the mapping works */
  281. if (__get_user(tmp, (u32*)KVM_MAGIC_PAGE)) {
  282. kvm_patching_worked = false;
  283. return;
  284. }
  285. /* Now loop through all code and find instructions */
  286. start = (void*)_stext;
  287. end = (void*)_etext;
  288. for (p = start; p < end; p++)
  289. kvm_check_ins(p);
  290. printk(KERN_INFO "KVM: Live patching for a fast VM %s\n",
  291. kvm_patching_worked ? "worked" : "failed");
  292. }
  293. unsigned long kvm_hypercall(unsigned long *in,
  294. unsigned long *out,
  295. unsigned long nr)
  296. {
  297. unsigned long register r0 asm("r0");
  298. unsigned long register r3 asm("r3") = in[0];
  299. unsigned long register r4 asm("r4") = in[1];
  300. unsigned long register r5 asm("r5") = in[2];
  301. unsigned long register r6 asm("r6") = in[3];
  302. unsigned long register r7 asm("r7") = in[4];
  303. unsigned long register r8 asm("r8") = in[5];
  304. unsigned long register r9 asm("r9") = in[6];
  305. unsigned long register r10 asm("r10") = in[7];
  306. unsigned long register r11 asm("r11") = nr;
  307. unsigned long register r12 asm("r12");
  308. asm volatile("bl kvm_hypercall_start"
  309. : "=r"(r0), "=r"(r3), "=r"(r4), "=r"(r5), "=r"(r6),
  310. "=r"(r7), "=r"(r8), "=r"(r9), "=r"(r10), "=r"(r11),
  311. "=r"(r12)
  312. : "r"(r3), "r"(r4), "r"(r5), "r"(r6), "r"(r7), "r"(r8),
  313. "r"(r9), "r"(r10), "r"(r11)
  314. : "memory", "cc", "xer", "ctr", "lr");
  315. out[0] = r4;
  316. out[1] = r5;
  317. out[2] = r6;
  318. out[3] = r7;
  319. out[4] = r8;
  320. out[5] = r9;
  321. out[6] = r10;
  322. out[7] = r11;
  323. return r3;
  324. }
  325. EXPORT_SYMBOL_GPL(kvm_hypercall);
  326. static int kvm_para_setup(void)
  327. {
  328. extern u32 kvm_hypercall_start;
  329. struct device_node *hyper_node;
  330. u32 *insts;
  331. int len, i;
  332. hyper_node = of_find_node_by_path("/hypervisor");
  333. if (!hyper_node)
  334. return -1;
  335. insts = (u32*)of_get_property(hyper_node, "hcall-instructions", &len);
  336. if (len % 4)
  337. return -1;
  338. if (len > (4 * 4))
  339. return -1;
  340. for (i = 0; i < (len / 4); i++)
  341. kvm_patch_ins(&(&kvm_hypercall_start)[i], insts[i]);
  342. return 0;
  343. }
  344. static __init void kvm_free_tmp(void)
  345. {
  346. unsigned long start, end;
  347. start = (ulong)&kvm_tmp[kvm_tmp_index + (PAGE_SIZE - 1)] & PAGE_MASK;
  348. end = (ulong)&kvm_tmp[ARRAY_SIZE(kvm_tmp)] & PAGE_MASK;
  349. /* Free the tmp space we don't need */
  350. for (; start < end; start += PAGE_SIZE) {
  351. ClearPageReserved(virt_to_page(start));
  352. init_page_count(virt_to_page(start));
  353. free_page(start);
  354. totalram_pages++;
  355. }
  356. }
  357. static int __init kvm_guest_init(void)
  358. {
  359. if (!kvm_para_available())
  360. goto free_tmp;
  361. if (kvm_para_setup())
  362. goto free_tmp;
  363. if (kvm_para_has_feature(KVM_FEATURE_MAGIC_PAGE))
  364. kvm_use_magic_page();
  365. free_tmp:
  366. kvm_free_tmp();
  367. return 0;
  368. }
  369. postcore_initcall(kvm_guest_init);