kvm.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  1. /*
  2. * Copyright (C) 2010 SUSE Linux Products GmbH. All rights reserved.
  3. * Copyright 2010-2011 Freescale Semiconductor, Inc.
  4. *
  5. * Authors:
  6. * Alexander Graf <agraf@suse.de>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License, version 2, as
  10. * published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  20. */
  21. #include <linux/kvm_host.h>
  22. #include <linux/init.h>
  23. #include <linux/export.h>
  24. #include <linux/kvm_para.h>
  25. #include <linux/slab.h>
  26. #include <linux/of.h>
  27. #include <linux/pagemap.h>
  28. #include <asm/reg.h>
  29. #include <asm/sections.h>
  30. #include <asm/cacheflush.h>
  31. #include <asm/disassemble.h>
  32. #include <asm/ppc-opcode.h>
  33. #include <asm/epapr_hcalls.h>
  34. #define KVM_MAGIC_PAGE (-4096L)
  35. #define magic_var(x) KVM_MAGIC_PAGE + offsetof(struct kvm_vcpu_arch_shared, x)
  36. #define KVM_INST_LWZ 0x80000000
  37. #define KVM_INST_STW 0x90000000
  38. #define KVM_INST_LD 0xe8000000
  39. #define KVM_INST_STD 0xf8000000
  40. #define KVM_INST_NOP 0x60000000
  41. #define KVM_INST_B 0x48000000
  42. #define KVM_INST_B_MASK 0x03ffffff
  43. #define KVM_INST_B_MAX 0x01ffffff
  44. #define KVM_INST_LI 0x38000000
  45. #define KVM_MASK_RT 0x03e00000
  46. #define KVM_RT_30 0x03c00000
  47. #define KVM_MASK_RB 0x0000f800
  48. #define KVM_INST_MFMSR 0x7c0000a6
  49. #define SPR_FROM 0
  50. #define SPR_TO 0x100
  51. #define KVM_INST_SPR(sprn, moveto) (0x7c0002a6 | \
  52. (((sprn) & 0x1f) << 16) | \
  53. (((sprn) & 0x3e0) << 6) | \
  54. (moveto))
  55. #define KVM_INST_MFSPR(sprn) KVM_INST_SPR(sprn, SPR_FROM)
  56. #define KVM_INST_MTSPR(sprn) KVM_INST_SPR(sprn, SPR_TO)
  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_WRTEE 0x7c000106
  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. 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_ll(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 & 0x0000fffc));
  79. #endif
  80. }
  81. static void kvm_patch_ins_ld(u32 *inst, long addr, u32 rt)
  82. {
  83. #ifdef CONFIG_64BIT
  84. kvm_patch_ins(inst, KVM_INST_LD | rt | (addr & 0x0000fffc));
  85. #else
  86. kvm_patch_ins(inst, KVM_INST_LWZ | rt | ((addr + 4) & 0x0000fffc));
  87. #endif
  88. }
  89. static void kvm_patch_ins_lwz(u32 *inst, long addr, u32 rt)
  90. {
  91. kvm_patch_ins(inst, KVM_INST_LWZ | rt | (addr & 0x0000ffff));
  92. }
  93. static void kvm_patch_ins_std(u32 *inst, long addr, u32 rt)
  94. {
  95. #ifdef CONFIG_64BIT
  96. kvm_patch_ins(inst, KVM_INST_STD | rt | (addr & 0x0000fffc));
  97. #else
  98. kvm_patch_ins(inst, KVM_INST_STW | rt | ((addr + 4) & 0x0000fffc));
  99. #endif
  100. }
  101. static void kvm_patch_ins_stw(u32 *inst, long addr, u32 rt)
  102. {
  103. kvm_patch_ins(inst, KVM_INST_STW | rt | (addr & 0x0000fffc));
  104. }
  105. static void kvm_patch_ins_nop(u32 *inst)
  106. {
  107. kvm_patch_ins(inst, KVM_INST_NOP);
  108. }
  109. static void kvm_patch_ins_b(u32 *inst, int addr)
  110. {
  111. #if defined(CONFIG_RELOCATABLE) && defined(CONFIG_PPC_BOOK3S)
  112. /* On relocatable kernels interrupts handlers and our code
  113. can be in different regions, so we don't patch them */
  114. if ((ulong)inst < (ulong)&__end_interrupts)
  115. return;
  116. #endif
  117. kvm_patch_ins(inst, KVM_INST_B | (addr & KVM_INST_B_MASK));
  118. }
  119. static u32 *kvm_alloc(int len)
  120. {
  121. u32 *p;
  122. if ((kvm_tmp_index + len) > ARRAY_SIZE(kvm_tmp)) {
  123. printk(KERN_ERR "KVM: No more space (%d + %d)\n",
  124. kvm_tmp_index, len);
  125. kvm_patching_worked = false;
  126. return NULL;
  127. }
  128. p = (void*)&kvm_tmp[kvm_tmp_index];
  129. kvm_tmp_index += len;
  130. return p;
  131. }
  132. extern u32 kvm_emulate_mtmsrd_branch_offs;
  133. extern u32 kvm_emulate_mtmsrd_reg_offs;
  134. extern u32 kvm_emulate_mtmsrd_orig_ins_offs;
  135. extern u32 kvm_emulate_mtmsrd_len;
  136. extern u32 kvm_emulate_mtmsrd[];
  137. static void kvm_patch_ins_mtmsrd(u32 *inst, u32 rt)
  138. {
  139. u32 *p;
  140. int distance_start;
  141. int distance_end;
  142. ulong next_inst;
  143. p = kvm_alloc(kvm_emulate_mtmsrd_len * 4);
  144. if (!p)
  145. return;
  146. /* Find out where we are and put everything there */
  147. distance_start = (ulong)p - (ulong)inst;
  148. next_inst = ((ulong)inst + 4);
  149. distance_end = next_inst - (ulong)&p[kvm_emulate_mtmsrd_branch_offs];
  150. /* Make sure we only write valid b instructions */
  151. if (distance_start > KVM_INST_B_MAX) {
  152. kvm_patching_worked = false;
  153. return;
  154. }
  155. /* Modify the chunk to fit the invocation */
  156. memcpy(p, kvm_emulate_mtmsrd, kvm_emulate_mtmsrd_len * 4);
  157. p[kvm_emulate_mtmsrd_branch_offs] |= distance_end & KVM_INST_B_MASK;
  158. switch (get_rt(rt)) {
  159. case 30:
  160. kvm_patch_ins_ll(&p[kvm_emulate_mtmsrd_reg_offs],
  161. magic_var(scratch2), KVM_RT_30);
  162. break;
  163. case 31:
  164. kvm_patch_ins_ll(&p[kvm_emulate_mtmsrd_reg_offs],
  165. magic_var(scratch1), KVM_RT_30);
  166. break;
  167. default:
  168. p[kvm_emulate_mtmsrd_reg_offs] |= rt;
  169. break;
  170. }
  171. p[kvm_emulate_mtmsrd_orig_ins_offs] = *inst;
  172. flush_icache_range((ulong)p, (ulong)p + kvm_emulate_mtmsrd_len * 4);
  173. /* Patch the invocation */
  174. kvm_patch_ins_b(inst, distance_start);
  175. }
  176. extern u32 kvm_emulate_mtmsr_branch_offs;
  177. extern u32 kvm_emulate_mtmsr_reg1_offs;
  178. extern u32 kvm_emulate_mtmsr_reg2_offs;
  179. extern u32 kvm_emulate_mtmsr_orig_ins_offs;
  180. extern u32 kvm_emulate_mtmsr_len;
  181. extern u32 kvm_emulate_mtmsr[];
  182. static void kvm_patch_ins_mtmsr(u32 *inst, u32 rt)
  183. {
  184. u32 *p;
  185. int distance_start;
  186. int distance_end;
  187. ulong next_inst;
  188. p = kvm_alloc(kvm_emulate_mtmsr_len * 4);
  189. if (!p)
  190. return;
  191. /* Find out where we are and put everything there */
  192. distance_start = (ulong)p - (ulong)inst;
  193. next_inst = ((ulong)inst + 4);
  194. distance_end = next_inst - (ulong)&p[kvm_emulate_mtmsr_branch_offs];
  195. /* Make sure we only write valid b instructions */
  196. if (distance_start > KVM_INST_B_MAX) {
  197. kvm_patching_worked = false;
  198. return;
  199. }
  200. /* Modify the chunk to fit the invocation */
  201. memcpy(p, kvm_emulate_mtmsr, kvm_emulate_mtmsr_len * 4);
  202. p[kvm_emulate_mtmsr_branch_offs] |= distance_end & KVM_INST_B_MASK;
  203. /* Make clobbered registers work too */
  204. switch (get_rt(rt)) {
  205. case 30:
  206. kvm_patch_ins_ll(&p[kvm_emulate_mtmsr_reg1_offs],
  207. magic_var(scratch2), KVM_RT_30);
  208. kvm_patch_ins_ll(&p[kvm_emulate_mtmsr_reg2_offs],
  209. magic_var(scratch2), KVM_RT_30);
  210. break;
  211. case 31:
  212. kvm_patch_ins_ll(&p[kvm_emulate_mtmsr_reg1_offs],
  213. magic_var(scratch1), KVM_RT_30);
  214. kvm_patch_ins_ll(&p[kvm_emulate_mtmsr_reg2_offs],
  215. magic_var(scratch1), KVM_RT_30);
  216. break;
  217. default:
  218. p[kvm_emulate_mtmsr_reg1_offs] |= rt;
  219. p[kvm_emulate_mtmsr_reg2_offs] |= rt;
  220. break;
  221. }
  222. p[kvm_emulate_mtmsr_orig_ins_offs] = *inst;
  223. flush_icache_range((ulong)p, (ulong)p + kvm_emulate_mtmsr_len * 4);
  224. /* Patch the invocation */
  225. kvm_patch_ins_b(inst, distance_start);
  226. }
  227. #ifdef CONFIG_BOOKE
  228. extern u32 kvm_emulate_wrtee_branch_offs;
  229. extern u32 kvm_emulate_wrtee_reg_offs;
  230. extern u32 kvm_emulate_wrtee_orig_ins_offs;
  231. extern u32 kvm_emulate_wrtee_len;
  232. extern u32 kvm_emulate_wrtee[];
  233. static void kvm_patch_ins_wrtee(u32 *inst, u32 rt, int imm_one)
  234. {
  235. u32 *p;
  236. int distance_start;
  237. int distance_end;
  238. ulong next_inst;
  239. p = kvm_alloc(kvm_emulate_wrtee_len * 4);
  240. if (!p)
  241. return;
  242. /* Find out where we are and put everything there */
  243. distance_start = (ulong)p - (ulong)inst;
  244. next_inst = ((ulong)inst + 4);
  245. distance_end = next_inst - (ulong)&p[kvm_emulate_wrtee_branch_offs];
  246. /* Make sure we only write valid b instructions */
  247. if (distance_start > KVM_INST_B_MAX) {
  248. kvm_patching_worked = false;
  249. return;
  250. }
  251. /* Modify the chunk to fit the invocation */
  252. memcpy(p, kvm_emulate_wrtee, kvm_emulate_wrtee_len * 4);
  253. p[kvm_emulate_wrtee_branch_offs] |= distance_end & KVM_INST_B_MASK;
  254. if (imm_one) {
  255. p[kvm_emulate_wrtee_reg_offs] =
  256. KVM_INST_LI | __PPC_RT(R30) | MSR_EE;
  257. } else {
  258. /* Make clobbered registers work too */
  259. switch (get_rt(rt)) {
  260. case 30:
  261. kvm_patch_ins_ll(&p[kvm_emulate_wrtee_reg_offs],
  262. magic_var(scratch2), KVM_RT_30);
  263. break;
  264. case 31:
  265. kvm_patch_ins_ll(&p[kvm_emulate_wrtee_reg_offs],
  266. magic_var(scratch1), KVM_RT_30);
  267. break;
  268. default:
  269. p[kvm_emulate_wrtee_reg_offs] |= rt;
  270. break;
  271. }
  272. }
  273. p[kvm_emulate_wrtee_orig_ins_offs] = *inst;
  274. flush_icache_range((ulong)p, (ulong)p + kvm_emulate_wrtee_len * 4);
  275. /* Patch the invocation */
  276. kvm_patch_ins_b(inst, distance_start);
  277. }
  278. extern u32 kvm_emulate_wrteei_0_branch_offs;
  279. extern u32 kvm_emulate_wrteei_0_len;
  280. extern u32 kvm_emulate_wrteei_0[];
  281. static void kvm_patch_ins_wrteei_0(u32 *inst)
  282. {
  283. u32 *p;
  284. int distance_start;
  285. int distance_end;
  286. ulong next_inst;
  287. p = kvm_alloc(kvm_emulate_wrteei_0_len * 4);
  288. if (!p)
  289. return;
  290. /* Find out where we are and put everything there */
  291. distance_start = (ulong)p - (ulong)inst;
  292. next_inst = ((ulong)inst + 4);
  293. distance_end = next_inst - (ulong)&p[kvm_emulate_wrteei_0_branch_offs];
  294. /* Make sure we only write valid b instructions */
  295. if (distance_start > KVM_INST_B_MAX) {
  296. kvm_patching_worked = false;
  297. return;
  298. }
  299. memcpy(p, kvm_emulate_wrteei_0, kvm_emulate_wrteei_0_len * 4);
  300. p[kvm_emulate_wrteei_0_branch_offs] |= distance_end & KVM_INST_B_MASK;
  301. flush_icache_range((ulong)p, (ulong)p + kvm_emulate_wrteei_0_len * 4);
  302. /* Patch the invocation */
  303. kvm_patch_ins_b(inst, distance_start);
  304. }
  305. #endif
  306. #ifdef CONFIG_PPC_BOOK3S_32
  307. extern u32 kvm_emulate_mtsrin_branch_offs;
  308. extern u32 kvm_emulate_mtsrin_reg1_offs;
  309. extern u32 kvm_emulate_mtsrin_reg2_offs;
  310. extern u32 kvm_emulate_mtsrin_orig_ins_offs;
  311. extern u32 kvm_emulate_mtsrin_len;
  312. extern u32 kvm_emulate_mtsrin[];
  313. static void kvm_patch_ins_mtsrin(u32 *inst, u32 rt, u32 rb)
  314. {
  315. u32 *p;
  316. int distance_start;
  317. int distance_end;
  318. ulong next_inst;
  319. p = kvm_alloc(kvm_emulate_mtsrin_len * 4);
  320. if (!p)
  321. return;
  322. /* Find out where we are and put everything there */
  323. distance_start = (ulong)p - (ulong)inst;
  324. next_inst = ((ulong)inst + 4);
  325. distance_end = next_inst - (ulong)&p[kvm_emulate_mtsrin_branch_offs];
  326. /* Make sure we only write valid b instructions */
  327. if (distance_start > KVM_INST_B_MAX) {
  328. kvm_patching_worked = false;
  329. return;
  330. }
  331. /* Modify the chunk to fit the invocation */
  332. memcpy(p, kvm_emulate_mtsrin, kvm_emulate_mtsrin_len * 4);
  333. p[kvm_emulate_mtsrin_branch_offs] |= distance_end & KVM_INST_B_MASK;
  334. p[kvm_emulate_mtsrin_reg1_offs] |= (rb << 10);
  335. p[kvm_emulate_mtsrin_reg2_offs] |= rt;
  336. p[kvm_emulate_mtsrin_orig_ins_offs] = *inst;
  337. flush_icache_range((ulong)p, (ulong)p + kvm_emulate_mtsrin_len * 4);
  338. /* Patch the invocation */
  339. kvm_patch_ins_b(inst, distance_start);
  340. }
  341. #endif
  342. static void kvm_map_magic_page(void *data)
  343. {
  344. u32 *features = data;
  345. ulong in[8] = {0};
  346. ulong out[8];
  347. in[0] = KVM_MAGIC_PAGE;
  348. in[1] = KVM_MAGIC_PAGE | MAGIC_PAGE_FLAG_NOT_MAPPED_NX;
  349. epapr_hypercall(in, out, KVM_HCALL_TOKEN(KVM_HC_PPC_MAP_MAGIC_PAGE));
  350. *features = out[0];
  351. }
  352. static void kvm_check_ins(u32 *inst, u32 features)
  353. {
  354. u32 _inst = *inst;
  355. u32 inst_no_rt = _inst & ~KVM_MASK_RT;
  356. u32 inst_rt = _inst & KVM_MASK_RT;
  357. switch (inst_no_rt) {
  358. /* Loads */
  359. case KVM_INST_MFMSR:
  360. kvm_patch_ins_ld(inst, magic_var(msr), inst_rt);
  361. break;
  362. case KVM_INST_MFSPR(SPRN_SPRG0):
  363. kvm_patch_ins_ld(inst, magic_var(sprg0), inst_rt);
  364. break;
  365. case KVM_INST_MFSPR(SPRN_SPRG1):
  366. kvm_patch_ins_ld(inst, magic_var(sprg1), inst_rt);
  367. break;
  368. case KVM_INST_MFSPR(SPRN_SPRG2):
  369. kvm_patch_ins_ld(inst, magic_var(sprg2), inst_rt);
  370. break;
  371. case KVM_INST_MFSPR(SPRN_SPRG3):
  372. kvm_patch_ins_ld(inst, magic_var(sprg3), inst_rt);
  373. break;
  374. case KVM_INST_MFSPR(SPRN_SRR0):
  375. kvm_patch_ins_ld(inst, magic_var(srr0), inst_rt);
  376. break;
  377. case KVM_INST_MFSPR(SPRN_SRR1):
  378. kvm_patch_ins_ld(inst, magic_var(srr1), inst_rt);
  379. break;
  380. #ifdef CONFIG_BOOKE
  381. case KVM_INST_MFSPR(SPRN_DEAR):
  382. #else
  383. case KVM_INST_MFSPR(SPRN_DAR):
  384. #endif
  385. kvm_patch_ins_ld(inst, magic_var(dar), inst_rt);
  386. break;
  387. case KVM_INST_MFSPR(SPRN_DSISR):
  388. kvm_patch_ins_lwz(inst, magic_var(dsisr), inst_rt);
  389. break;
  390. #ifdef CONFIG_PPC_BOOK3E_MMU
  391. case KVM_INST_MFSPR(SPRN_MAS0):
  392. if (features & KVM_MAGIC_FEAT_MAS0_TO_SPRG7)
  393. kvm_patch_ins_lwz(inst, magic_var(mas0), inst_rt);
  394. break;
  395. case KVM_INST_MFSPR(SPRN_MAS1):
  396. if (features & KVM_MAGIC_FEAT_MAS0_TO_SPRG7)
  397. kvm_patch_ins_lwz(inst, magic_var(mas1), inst_rt);
  398. break;
  399. case KVM_INST_MFSPR(SPRN_MAS2):
  400. if (features & KVM_MAGIC_FEAT_MAS0_TO_SPRG7)
  401. kvm_patch_ins_ld(inst, magic_var(mas2), inst_rt);
  402. break;
  403. case KVM_INST_MFSPR(SPRN_MAS3):
  404. if (features & KVM_MAGIC_FEAT_MAS0_TO_SPRG7)
  405. kvm_patch_ins_lwz(inst, magic_var(mas7_3) + 4, inst_rt);
  406. break;
  407. case KVM_INST_MFSPR(SPRN_MAS4):
  408. if (features & KVM_MAGIC_FEAT_MAS0_TO_SPRG7)
  409. kvm_patch_ins_lwz(inst, magic_var(mas4), inst_rt);
  410. break;
  411. case KVM_INST_MFSPR(SPRN_MAS6):
  412. if (features & KVM_MAGIC_FEAT_MAS0_TO_SPRG7)
  413. kvm_patch_ins_lwz(inst, magic_var(mas6), inst_rt);
  414. break;
  415. case KVM_INST_MFSPR(SPRN_MAS7):
  416. if (features & KVM_MAGIC_FEAT_MAS0_TO_SPRG7)
  417. kvm_patch_ins_lwz(inst, magic_var(mas7_3), inst_rt);
  418. break;
  419. #endif /* CONFIG_PPC_BOOK3E_MMU */
  420. case KVM_INST_MFSPR(SPRN_SPRG4):
  421. #ifdef CONFIG_BOOKE
  422. case KVM_INST_MFSPR(SPRN_SPRG4R):
  423. #endif
  424. if (features & KVM_MAGIC_FEAT_MAS0_TO_SPRG7)
  425. kvm_patch_ins_ld(inst, magic_var(sprg4), inst_rt);
  426. break;
  427. case KVM_INST_MFSPR(SPRN_SPRG5):
  428. #ifdef CONFIG_BOOKE
  429. case KVM_INST_MFSPR(SPRN_SPRG5R):
  430. #endif
  431. if (features & KVM_MAGIC_FEAT_MAS0_TO_SPRG7)
  432. kvm_patch_ins_ld(inst, magic_var(sprg5), inst_rt);
  433. break;
  434. case KVM_INST_MFSPR(SPRN_SPRG6):
  435. #ifdef CONFIG_BOOKE
  436. case KVM_INST_MFSPR(SPRN_SPRG6R):
  437. #endif
  438. if (features & KVM_MAGIC_FEAT_MAS0_TO_SPRG7)
  439. kvm_patch_ins_ld(inst, magic_var(sprg6), inst_rt);
  440. break;
  441. case KVM_INST_MFSPR(SPRN_SPRG7):
  442. #ifdef CONFIG_BOOKE
  443. case KVM_INST_MFSPR(SPRN_SPRG7R):
  444. #endif
  445. if (features & KVM_MAGIC_FEAT_MAS0_TO_SPRG7)
  446. kvm_patch_ins_ld(inst, magic_var(sprg7), inst_rt);
  447. break;
  448. #ifdef CONFIG_BOOKE
  449. case KVM_INST_MFSPR(SPRN_ESR):
  450. if (features & KVM_MAGIC_FEAT_MAS0_TO_SPRG7)
  451. kvm_patch_ins_lwz(inst, magic_var(esr), inst_rt);
  452. break;
  453. #endif
  454. case KVM_INST_MFSPR(SPRN_PIR):
  455. if (features & KVM_MAGIC_FEAT_MAS0_TO_SPRG7)
  456. kvm_patch_ins_lwz(inst, magic_var(pir), inst_rt);
  457. break;
  458. /* Stores */
  459. case KVM_INST_MTSPR(SPRN_SPRG0):
  460. kvm_patch_ins_std(inst, magic_var(sprg0), inst_rt);
  461. break;
  462. case KVM_INST_MTSPR(SPRN_SPRG1):
  463. kvm_patch_ins_std(inst, magic_var(sprg1), inst_rt);
  464. break;
  465. case KVM_INST_MTSPR(SPRN_SPRG2):
  466. kvm_patch_ins_std(inst, magic_var(sprg2), inst_rt);
  467. break;
  468. case KVM_INST_MTSPR(SPRN_SPRG3):
  469. kvm_patch_ins_std(inst, magic_var(sprg3), inst_rt);
  470. break;
  471. case KVM_INST_MTSPR(SPRN_SRR0):
  472. kvm_patch_ins_std(inst, magic_var(srr0), inst_rt);
  473. break;
  474. case KVM_INST_MTSPR(SPRN_SRR1):
  475. kvm_patch_ins_std(inst, magic_var(srr1), inst_rt);
  476. break;
  477. #ifdef CONFIG_BOOKE
  478. case KVM_INST_MTSPR(SPRN_DEAR):
  479. #else
  480. case KVM_INST_MTSPR(SPRN_DAR):
  481. #endif
  482. kvm_patch_ins_std(inst, magic_var(dar), inst_rt);
  483. break;
  484. case KVM_INST_MTSPR(SPRN_DSISR):
  485. kvm_patch_ins_stw(inst, magic_var(dsisr), inst_rt);
  486. break;
  487. #ifdef CONFIG_PPC_BOOK3E_MMU
  488. case KVM_INST_MTSPR(SPRN_MAS0):
  489. if (features & KVM_MAGIC_FEAT_MAS0_TO_SPRG7)
  490. kvm_patch_ins_stw(inst, magic_var(mas0), inst_rt);
  491. break;
  492. case KVM_INST_MTSPR(SPRN_MAS1):
  493. if (features & KVM_MAGIC_FEAT_MAS0_TO_SPRG7)
  494. kvm_patch_ins_stw(inst, magic_var(mas1), inst_rt);
  495. break;
  496. case KVM_INST_MTSPR(SPRN_MAS2):
  497. if (features & KVM_MAGIC_FEAT_MAS0_TO_SPRG7)
  498. kvm_patch_ins_std(inst, magic_var(mas2), inst_rt);
  499. break;
  500. case KVM_INST_MTSPR(SPRN_MAS3):
  501. if (features & KVM_MAGIC_FEAT_MAS0_TO_SPRG7)
  502. kvm_patch_ins_stw(inst, magic_var(mas7_3) + 4, inst_rt);
  503. break;
  504. case KVM_INST_MTSPR(SPRN_MAS4):
  505. if (features & KVM_MAGIC_FEAT_MAS0_TO_SPRG7)
  506. kvm_patch_ins_stw(inst, magic_var(mas4), inst_rt);
  507. break;
  508. case KVM_INST_MTSPR(SPRN_MAS6):
  509. if (features & KVM_MAGIC_FEAT_MAS0_TO_SPRG7)
  510. kvm_patch_ins_stw(inst, magic_var(mas6), inst_rt);
  511. break;
  512. case KVM_INST_MTSPR(SPRN_MAS7):
  513. if (features & KVM_MAGIC_FEAT_MAS0_TO_SPRG7)
  514. kvm_patch_ins_stw(inst, magic_var(mas7_3), inst_rt);
  515. break;
  516. #endif /* CONFIG_PPC_BOOK3E_MMU */
  517. case KVM_INST_MTSPR(SPRN_SPRG4):
  518. if (features & KVM_MAGIC_FEAT_MAS0_TO_SPRG7)
  519. kvm_patch_ins_std(inst, magic_var(sprg4), inst_rt);
  520. break;
  521. case KVM_INST_MTSPR(SPRN_SPRG5):
  522. if (features & KVM_MAGIC_FEAT_MAS0_TO_SPRG7)
  523. kvm_patch_ins_std(inst, magic_var(sprg5), inst_rt);
  524. break;
  525. case KVM_INST_MTSPR(SPRN_SPRG6):
  526. if (features & KVM_MAGIC_FEAT_MAS0_TO_SPRG7)
  527. kvm_patch_ins_std(inst, magic_var(sprg6), inst_rt);
  528. break;
  529. case KVM_INST_MTSPR(SPRN_SPRG7):
  530. if (features & KVM_MAGIC_FEAT_MAS0_TO_SPRG7)
  531. kvm_patch_ins_std(inst, magic_var(sprg7), inst_rt);
  532. break;
  533. #ifdef CONFIG_BOOKE
  534. case KVM_INST_MTSPR(SPRN_ESR):
  535. if (features & KVM_MAGIC_FEAT_MAS0_TO_SPRG7)
  536. kvm_patch_ins_stw(inst, magic_var(esr), inst_rt);
  537. break;
  538. #endif
  539. /* Nops */
  540. case KVM_INST_TLBSYNC:
  541. kvm_patch_ins_nop(inst);
  542. break;
  543. /* Rewrites */
  544. case KVM_INST_MTMSRD_L1:
  545. kvm_patch_ins_mtmsrd(inst, inst_rt);
  546. break;
  547. case KVM_INST_MTMSR:
  548. case KVM_INST_MTMSRD_L0:
  549. kvm_patch_ins_mtmsr(inst, inst_rt);
  550. break;
  551. #ifdef CONFIG_BOOKE
  552. case KVM_INST_WRTEE:
  553. kvm_patch_ins_wrtee(inst, inst_rt, 0);
  554. break;
  555. #endif
  556. }
  557. switch (inst_no_rt & ~KVM_MASK_RB) {
  558. #ifdef CONFIG_PPC_BOOK3S_32
  559. case KVM_INST_MTSRIN:
  560. if (features & KVM_MAGIC_FEAT_SR) {
  561. u32 inst_rb = _inst & KVM_MASK_RB;
  562. kvm_patch_ins_mtsrin(inst, inst_rt, inst_rb);
  563. }
  564. break;
  565. #endif
  566. }
  567. switch (_inst) {
  568. #ifdef CONFIG_BOOKE
  569. case KVM_INST_WRTEEI_0:
  570. kvm_patch_ins_wrteei_0(inst);
  571. break;
  572. case KVM_INST_WRTEEI_1:
  573. kvm_patch_ins_wrtee(inst, 0, 1);
  574. break;
  575. #endif
  576. }
  577. }
  578. extern u32 kvm_template_start[];
  579. extern u32 kvm_template_end[];
  580. static void kvm_use_magic_page(void)
  581. {
  582. u32 *p;
  583. u32 *start, *end;
  584. u32 features;
  585. /* Tell the host to map the magic page to -4096 on all CPUs */
  586. on_each_cpu(kvm_map_magic_page, &features, 1);
  587. /* Quick self-test to see if the mapping works */
  588. if (!fault_in_pages_readable((const char *)KVM_MAGIC_PAGE, sizeof(u32))) {
  589. kvm_patching_worked = false;
  590. return;
  591. }
  592. /* Now loop through all code and find instructions */
  593. start = (void*)_stext;
  594. end = (void*)_etext;
  595. /*
  596. * Being interrupted in the middle of patching would
  597. * be bad for SPRG4-7, which KVM can't keep in sync
  598. * with emulated accesses because reads don't trap.
  599. */
  600. local_irq_disable();
  601. for (p = start; p < end; p++) {
  602. /* Avoid patching the template code */
  603. if (p >= kvm_template_start && p < kvm_template_end) {
  604. p = kvm_template_end - 1;
  605. continue;
  606. }
  607. kvm_check_ins(p, features);
  608. }
  609. local_irq_enable();
  610. printk(KERN_INFO "KVM: Live patching for a fast VM %s\n",
  611. kvm_patching_worked ? "worked" : "failed");
  612. }
  613. static __init void kvm_free_tmp(void)
  614. {
  615. free_reserved_area(&kvm_tmp[kvm_tmp_index],
  616. &kvm_tmp[ARRAY_SIZE(kvm_tmp)], -1, NULL);
  617. }
  618. static int __init kvm_guest_init(void)
  619. {
  620. if (!kvm_para_available())
  621. goto free_tmp;
  622. if (!epapr_paravirt_enabled)
  623. goto free_tmp;
  624. if (kvm_para_has_feature(KVM_FEATURE_MAGIC_PAGE))
  625. kvm_use_magic_page();
  626. #ifdef CONFIG_PPC_BOOK3S_64
  627. /* Enable napping */
  628. powersave_nap = 1;
  629. #endif
  630. free_tmp:
  631. kvm_free_tmp();
  632. return 0;
  633. }
  634. postcore_initcall(kvm_guest_init);