24 #include <linux/export.h>
25 #include <linux/kvm_para.h>
26 #include <linux/slab.h>
30 #include <asm/sections.h>
31 #include <asm/cacheflush.h>
36 #define KVM_MAGIC_PAGE (-4096L)
37 #define magic_var(x) KVM_MAGIC_PAGE + offsetof(struct kvm_vcpu_arch_shared, x)
39 #define KVM_INST_LWZ 0x80000000
40 #define KVM_INST_STW 0x90000000
41 #define KVM_INST_LD 0xe8000000
42 #define KVM_INST_STD 0xf8000000
43 #define KVM_INST_NOP 0x60000000
44 #define KVM_INST_B 0x48000000
45 #define KVM_INST_B_MASK 0x03ffffff
46 #define KVM_INST_B_MAX 0x01ffffff
47 #define KVM_INST_LI 0x38000000
49 #define KVM_MASK_RT 0x03e00000
50 #define KVM_RT_30 0x03c00000
51 #define KVM_MASK_RB 0x0000f800
52 #define KVM_INST_MFMSR 0x7c0000a6
57 #define KVM_INST_SPR(sprn, moveto) (0x7c0002a6 | \
58 (((sprn) & 0x1f) << 16) | \
59 (((sprn) & 0x3e0) << 6) | \
62 #define KVM_INST_MFSPR(sprn) KVM_INST_SPR(sprn, SPR_FROM)
63 #define KVM_INST_MTSPR(sprn) KVM_INST_SPR(sprn, SPR_TO)
65 #define KVM_INST_TLBSYNC 0x7c00046c
66 #define KVM_INST_MTMSRD_L0 0x7c000164
67 #define KVM_INST_MTMSRD_L1 0x7c010164
68 #define KVM_INST_MTMSR 0x7c000124
70 #define KVM_INST_WRTEE 0x7c000106
71 #define KVM_INST_WRTEEI_0 0x7c000146
72 #define KVM_INST_WRTEEI_1 0x7c008146
74 #define KVM_INST_MTSRIN 0x7c0001e4
76 static bool kvm_patching_worked =
true;
77 static char kvm_tmp[1024 * 1024];
78 static int kvm_tmp_index;
80 static inline void kvm_patch_ins(
u32 *inst,
u32 new_inst)
86 static void kvm_patch_ins_ll(
u32 *inst,
long addr,
u32 rt)
89 kvm_patch_ins(inst,
KVM_INST_LD | rt | (addr & 0x0000fffc));
91 kvm_patch_ins(inst,
KVM_INST_LWZ | rt | (addr & 0x0000fffc));
95 static void kvm_patch_ins_ld(
u32 *inst,
long addr,
u32 rt)
98 kvm_patch_ins(inst,
KVM_INST_LD | rt | (addr & 0x0000fffc));
100 kvm_patch_ins(inst,
KVM_INST_LWZ | rt | ((addr + 4) & 0x0000fffc));
104 static void kvm_patch_ins_lwz(
u32 *inst,
long addr,
u32 rt)
106 kvm_patch_ins(inst,
KVM_INST_LWZ | rt | (addr & 0x0000ffff));
109 static void kvm_patch_ins_std(
u32 *inst,
long addr,
u32 rt)
112 kvm_patch_ins(inst,
KVM_INST_STD | rt | (addr & 0x0000fffc));
114 kvm_patch_ins(inst,
KVM_INST_STW | rt | ((addr + 4) & 0x0000fffc));
118 static void kvm_patch_ins_stw(
u32 *inst,
long addr,
u32 rt)
120 kvm_patch_ins(inst,
KVM_INST_STW | rt | (addr & 0x0000fffc));
123 static void kvm_patch_ins_nop(
u32 *inst)
128 static void kvm_patch_ins_b(
u32 *inst,
int addr)
130 #if defined(CONFIG_RELOCATABLE) && defined(CONFIG_PPC_BOOK3S)
141 static u32 *kvm_alloc(
int len)
145 if ((kvm_tmp_index + len) >
ARRAY_SIZE(kvm_tmp)) {
148 kvm_patching_worked =
false;
152 p = (
void*)&kvm_tmp[kvm_tmp_index];
153 kvm_tmp_index += len;
164 static void kvm_patch_ins_mtmsrd(
u32 *inst,
u32 rt)
171 p = kvm_alloc(kvm_emulate_mtmsrd_len * 4);
177 next_inst = ((
ulong)inst + 4);
178 distance_end = next_inst - (
ulong)&p[kvm_emulate_mtmsrd_branch_offs];
182 kvm_patching_worked =
false;
187 memcpy(p, kvm_emulate_mtmsrd, kvm_emulate_mtmsrd_len * 4);
189 switch (get_rt(rt)) {
191 kvm_patch_ins_ll(&p[kvm_emulate_mtmsrd_reg_offs],
195 kvm_patch_ins_ll(&p[kvm_emulate_mtmsrd_reg_offs],
199 p[kvm_emulate_mtmsrd_reg_offs] |= rt;
203 p[kvm_emulate_mtmsrd_orig_ins_offs] = *inst;
207 kvm_patch_ins_b(inst, distance_start);
217 static void kvm_patch_ins_mtmsr(
u32 *inst,
u32 rt)
224 p = kvm_alloc(kvm_emulate_mtmsr_len * 4);
230 next_inst = ((
ulong)inst + 4);
231 distance_end = next_inst - (
ulong)&p[kvm_emulate_mtmsr_branch_offs];
235 kvm_patching_worked =
false;
240 memcpy(p, kvm_emulate_mtmsr, kvm_emulate_mtmsr_len * 4);
244 switch (get_rt(rt)) {
246 kvm_patch_ins_ll(&p[kvm_emulate_mtmsr_reg1_offs],
248 kvm_patch_ins_ll(&p[kvm_emulate_mtmsr_reg2_offs],
252 kvm_patch_ins_ll(&p[kvm_emulate_mtmsr_reg1_offs],
254 kvm_patch_ins_ll(&p[kvm_emulate_mtmsr_reg2_offs],
258 p[kvm_emulate_mtmsr_reg1_offs] |= rt;
259 p[kvm_emulate_mtmsr_reg2_offs] |= rt;
263 p[kvm_emulate_mtmsr_orig_ins_offs] = *inst;
267 kvm_patch_ins_b(inst, distance_start);
272 extern u32 kvm_emulate_wrtee_branch_offs;
273 extern u32 kvm_emulate_wrtee_reg_offs;
274 extern u32 kvm_emulate_wrtee_orig_ins_offs;
275 extern u32 kvm_emulate_wrtee_len;
276 extern u32 kvm_emulate_wrtee[];
278 static void kvm_patch_ins_wrtee(
u32 *inst,
u32 rt,
int imm_one)
285 p = kvm_alloc(kvm_emulate_wrtee_len * 4);
291 next_inst = ((
ulong)inst + 4);
292 distance_end = next_inst - (
ulong)&p[kvm_emulate_wrtee_branch_offs];
296 kvm_patching_worked =
false;
301 memcpy(p, kvm_emulate_wrtee, kvm_emulate_wrtee_len * 4);
305 p[kvm_emulate_wrtee_reg_offs] =
309 switch (get_rt(rt)) {
311 kvm_patch_ins_ll(&p[kvm_emulate_wrtee_reg_offs],
315 kvm_patch_ins_ll(&p[kvm_emulate_wrtee_reg_offs],
319 p[kvm_emulate_wrtee_reg_offs] |= rt;
324 p[kvm_emulate_wrtee_orig_ins_offs] = *inst;
328 kvm_patch_ins_b(inst, distance_start);
331 extern u32 kvm_emulate_wrteei_0_branch_offs;
332 extern u32 kvm_emulate_wrteei_0_len;
333 extern u32 kvm_emulate_wrteei_0[];
335 static void kvm_patch_ins_wrteei_0(
u32 *inst)
342 p = kvm_alloc(kvm_emulate_wrteei_0_len * 4);
348 next_inst = ((
ulong)inst + 4);
349 distance_end = next_inst - (
ulong)&p[kvm_emulate_wrteei_0_branch_offs];
353 kvm_patching_worked =
false;
357 memcpy(p, kvm_emulate_wrteei_0, kvm_emulate_wrteei_0_len * 4);
362 kvm_patch_ins_b(inst, distance_start);
367 #ifdef CONFIG_PPC_BOOK3S_32
369 extern u32 kvm_emulate_mtsrin_branch_offs;
370 extern u32 kvm_emulate_mtsrin_reg1_offs;
371 extern u32 kvm_emulate_mtsrin_reg2_offs;
372 extern u32 kvm_emulate_mtsrin_orig_ins_offs;
373 extern u32 kvm_emulate_mtsrin_len;
374 extern u32 kvm_emulate_mtsrin[];
376 static void kvm_patch_ins_mtsrin(
u32 *inst,
u32 rt,
u32 rb)
383 p = kvm_alloc(kvm_emulate_mtsrin_len * 4);
389 next_inst = ((
ulong)inst + 4);
390 distance_end = next_inst - (
ulong)&p[kvm_emulate_mtsrin_branch_offs];
394 kvm_patching_worked =
false;
399 memcpy(p, kvm_emulate_mtsrin, kvm_emulate_mtsrin_len * 4);
401 p[kvm_emulate_mtsrin_reg1_offs] |= (rb << 10);
402 p[kvm_emulate_mtsrin_reg2_offs] |= rt;
403 p[kvm_emulate_mtsrin_orig_ins_offs] = *inst;
407 kvm_patch_ins_b(inst, distance_start);
412 static void kvm_map_magic_page(
void *
data)
433 switch (inst_no_rt) {
436 kvm_patch_ins_ld(inst,
magic_var(msr), inst_rt);
439 kvm_patch_ins_ld(inst,
magic_var(sprg0), inst_rt);
442 kvm_patch_ins_ld(inst,
magic_var(sprg1), inst_rt);
445 kvm_patch_ins_ld(inst,
magic_var(sprg2), inst_rt);
448 kvm_patch_ins_ld(inst,
magic_var(sprg3), inst_rt);
451 kvm_patch_ins_ld(inst,
magic_var(srr0), inst_rt);
454 kvm_patch_ins_ld(inst,
magic_var(srr1), inst_rt);
464 kvm_patch_ins_lwz(inst,
magic_var(dsisr), inst_rt);
467 #ifdef CONFIG_PPC_BOOK3E_MMU
470 kvm_patch_ins_lwz(inst,
magic_var(mas0), inst_rt);
474 kvm_patch_ins_lwz(inst,
magic_var(mas1), inst_rt);
478 kvm_patch_ins_ld(inst,
magic_var(mas2), inst_rt);
482 kvm_patch_ins_lwz(inst,
magic_var(mas7_3) + 4, inst_rt);
486 kvm_patch_ins_lwz(inst,
magic_var(mas4), inst_rt);
490 kvm_patch_ins_lwz(inst,
magic_var(mas6), inst_rt);
494 kvm_patch_ins_lwz(inst,
magic_var(mas7_3), inst_rt);
503 kvm_patch_ins_ld(inst,
magic_var(sprg4), inst_rt);
510 kvm_patch_ins_ld(inst,
magic_var(sprg5), inst_rt);
517 kvm_patch_ins_ld(inst,
magic_var(sprg6), inst_rt);
524 kvm_patch_ins_ld(inst,
magic_var(sprg7), inst_rt);
536 kvm_patch_ins_lwz(inst,
magic_var(pir), inst_rt);
542 kvm_patch_ins_std(inst,
magic_var(sprg0), inst_rt);
545 kvm_patch_ins_std(inst,
magic_var(sprg1), inst_rt);
548 kvm_patch_ins_std(inst,
magic_var(sprg2), inst_rt);
551 kvm_patch_ins_std(inst,
magic_var(sprg3), inst_rt);
554 kvm_patch_ins_std(inst,
magic_var(srr0), inst_rt);
557 kvm_patch_ins_std(inst,
magic_var(srr1), inst_rt);
567 kvm_patch_ins_stw(inst,
magic_var(dsisr), inst_rt);
569 #ifdef CONFIG_PPC_BOOK3E_MMU
572 kvm_patch_ins_stw(inst,
magic_var(mas0), inst_rt);
576 kvm_patch_ins_stw(inst,
magic_var(mas1), inst_rt);
580 kvm_patch_ins_std(inst,
magic_var(mas2), inst_rt);
584 kvm_patch_ins_stw(inst,
magic_var(mas7_3) + 4, inst_rt);
588 kvm_patch_ins_stw(inst,
magic_var(mas4), inst_rt);
592 kvm_patch_ins_stw(inst,
magic_var(mas6), inst_rt);
596 kvm_patch_ins_stw(inst,
magic_var(mas7_3), inst_rt);
602 kvm_patch_ins_std(inst,
magic_var(sprg4), inst_rt);
606 kvm_patch_ins_std(inst,
magic_var(sprg5), inst_rt);
610 kvm_patch_ins_std(inst,
magic_var(sprg6), inst_rt);
614 kvm_patch_ins_std(inst,
magic_var(sprg7), inst_rt);
626 kvm_patch_ins_nop(inst);
631 kvm_patch_ins_mtmsrd(inst, inst_rt);
635 kvm_patch_ins_mtmsr(inst, inst_rt);
639 kvm_patch_ins_wrtee(inst, inst_rt, 0);
645 #ifdef CONFIG_PPC_BOOK3S_32
649 kvm_patch_ins_mtsrin(inst, inst_rt, inst_rb);
659 kvm_patch_ins_wrteei_0(inst);
663 kvm_patch_ins_wrtee(inst, 0, 1);
672 static void kvm_use_magic_page(
void)
684 kvm_patching_worked =
false;
689 start = (
void*)_stext;
699 for (p = start; p <
end; p++) {
701 if (p >= kvm_template_start && p < kvm_template_end) {
702 p = kvm_template_end - 1;
705 kvm_check_ins(p, features);
711 kvm_patching_worked ?
"worked" :
"failed");
718 unsigned long register r0 asm(
"r0");
719 unsigned long register r3 asm(
"r3") = in[0];
720 unsigned long register r4 asm(
"r4") = in[1];
721 unsigned long register r5 asm(
"r5") = in[2];
722 unsigned long register r6 asm(
"r6") = in[3];
723 unsigned long register r7 asm(
"r7") = in[4];
724 unsigned long register r8 asm(
"r8") = in[5];
725 unsigned long register r9 asm(
"r9") = in[6];
726 unsigned long register r10 asm(
"r10") = in[7];
727 unsigned long register r11 asm(
"r11") = nr;
728 unsigned long register r12 asm(
"r12");
730 asm volatile(
"bl epapr_hypercall_start"
731 :
"=r"(
r0),
"=r"(
r3),
"=r"(
r4),
"=r"(
r5),
"=r"(
r6),
736 :
"memory",
"cc",
"xer",
"ctr",
"lr");
751 static __init void kvm_free_tmp(
void)
776 kvm_use_magic_page();
778 #ifdef CONFIG_PPC_BOOK3S_64