kvm.c 15 KB

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