kvm.c 15 KB

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