Skip to content

Commit 819a63d

Browse files
agrafavikivity
authored andcommitted
KVM: PPC: PV mtmsrd L=1
The PowerPC ISA has a special instruction for mtmsr that only changes the EE and RI bits, namely the L=1 form. Since that one is reasonably often occuring and simple to implement, let's go with this first. Writing EE=0 is always just a store. Doing EE=1 also requires us to check for pending interrupts and if necessary exit back to the hypervisor. Signed-off-by: Alexander Graf <agraf@suse.de> Signed-off-by: Avi Kivity <avi@redhat.com>
1 parent 9223472 commit 819a63d

2 files changed

Lines changed: 101 additions & 0 deletions

File tree

‎arch/powerpc/kernel/kvm.c‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
#define KVM_INST_MTSPR_DSISR 0x7c1203a6
6464

6565
#define KVM_INST_TLBSYNC 0x7c00046c
66+
#define KVM_INST_MTMSRD_L1 0x7c010164
6667

6768
static bool kvm_patching_worked = true;
6869
static char kvm_tmp[1024 * 1024];
@@ -138,6 +139,43 @@ static u32 *kvm_alloc(int len)
138139
return p;
139140
}
140141

142+
extern u32 kvm_emulate_mtmsrd_branch_offs;
143+
extern u32 kvm_emulate_mtmsrd_reg_offs;
144+
extern u32 kvm_emulate_mtmsrd_len;
145+
extern u32 kvm_emulate_mtmsrd[];
146+
147+
static void kvm_patch_ins_mtmsrd(u32 *inst, u32 rt)
148+
{
149+
u32 *p;
150+
int distance_start;
151+
int distance_end;
152+
ulong next_inst;
153+
154+
p = kvm_alloc(kvm_emulate_mtmsrd_len * 4);
155+
if (!p)
156+
return;
157+
158+
/* Find out where we are and put everything there */
159+
distance_start = (ulong)p - (ulong)inst;
160+
next_inst = ((ulong)inst + 4);
161+
distance_end = next_inst - (ulong)&p[kvm_emulate_mtmsrd_branch_offs];
162+
163+
/* Make sure we only write valid b instructions */
164+
if (distance_start > KVM_INST_B_MAX) {
165+
kvm_patching_worked = false;
166+
return;
167+
}
168+
169+
/* Modify the chunk to fit the invocation */
170+
memcpy(p, kvm_emulate_mtmsrd, kvm_emulate_mtmsrd_len * 4);
171+
p[kvm_emulate_mtmsrd_branch_offs] |= distance_end & KVM_INST_B_MASK;
172+
p[kvm_emulate_mtmsrd_reg_offs] |= rt;
173+
flush_icache_range((ulong)p, (ulong)p + kvm_emulate_mtmsrd_len * 4);
174+
175+
/* Patch the invocation */
176+
kvm_patch_ins_b(inst, distance_start);
177+
}
178+
141179
static void kvm_map_magic_page(void *data)
142180
{
143181
kvm_hypercall2(KVM_HC_PPC_MAP_MAGIC_PAGE,
@@ -211,6 +249,13 @@ static void kvm_check_ins(u32 *inst)
211249
case KVM_INST_TLBSYNC:
212250
kvm_patch_ins_nop(inst);
213251
break;
252+
253+
/* Rewrites */
254+
case KVM_INST_MTMSRD_L1:
255+
/* We use r30 and r31 during the hook */
256+
if (get_rt(inst_rt) < 30)
257+
kvm_patch_ins_mtmsrd(inst, inst_rt);
258+
break;
214259
}
215260

216261
switch (_inst) {

‎arch/powerpc/kernel/kvm_emul.S‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,59 @@ kvm_hypercall_start:
6464
/* Disable critical section. We are critical if \
6565
shared->critical == r1 and r2 is always != r1 */ \
6666
STL64(r2, KVM_MAGIC_PAGE + KVM_MAGIC_CRITICAL, 0);
67+
68+
.global kvm_emulate_mtmsrd
69+
kvm_emulate_mtmsrd:
70+
71+
SCRATCH_SAVE
72+
73+
/* Put MSR & ~(MSR_EE|MSR_RI) in r31 */
74+
LL64(r31, KVM_MAGIC_PAGE + KVM_MAGIC_MSR, 0)
75+
lis r30, (~(MSR_EE | MSR_RI))@h
76+
ori r30, r30, (~(MSR_EE | MSR_RI))@l
77+
and r31, r31, r30
78+
79+
/* OR the register's (MSR_EE|MSR_RI) on MSR */
80+
kvm_emulate_mtmsrd_reg:
81+
andi. r30, r0, (MSR_EE|MSR_RI)
82+
or r31, r31, r30
83+
84+
/* Put MSR back into magic page */
85+
STL64(r31, KVM_MAGIC_PAGE + KVM_MAGIC_MSR, 0)
86+
87+
/* Check if we have to fetch an interrupt */
88+
lwz r31, (KVM_MAGIC_PAGE + KVM_MAGIC_INT)(0)
89+
cmpwi r31, 0
90+
beq+ no_check
91+
92+
/* Check if we may trigger an interrupt */
93+
andi. r30, r30, MSR_EE
94+
beq no_check
95+
96+
SCRATCH_RESTORE
97+
98+
/* Nag hypervisor */
99+
tlbsync
100+
101+
b kvm_emulate_mtmsrd_branch
102+
103+
no_check:
104+
105+
SCRATCH_RESTORE
106+
107+
/* Go back to caller */
108+
kvm_emulate_mtmsrd_branch:
109+
b .
110+
kvm_emulate_mtmsrd_end:
111+
112+
.global kvm_emulate_mtmsrd_branch_offs
113+
kvm_emulate_mtmsrd_branch_offs:
114+
.long (kvm_emulate_mtmsrd_branch - kvm_emulate_mtmsrd) / 4
115+
116+
.global kvm_emulate_mtmsrd_reg_offs
117+
kvm_emulate_mtmsrd_reg_offs:
118+
.long (kvm_emulate_mtmsrd_reg - kvm_emulate_mtmsrd) / 4
119+
120+
.global kvm_emulate_mtmsrd_len
121+
kvm_emulate_mtmsrd_len:
122+
.long (kvm_emulate_mtmsrd_end - kvm_emulate_mtmsrd) / 4

0 commit comments

Comments
 (0)