kvm.c 12 KB

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