kvm.c 14 KB

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