Skip to content

Commit 644bfa0

Browse files
agrafavikivity
authored andcommitted
KVM: PPC: PV wrteei
On BookE the preferred way to write the EE bit is the wrteei instruction. It already encodes the EE bit in the instruction. So in order to get BookE some speedups as well, let's also PV'nize thati instruction. Signed-off-by: Alexander Graf <agraf@suse.de> Signed-off-by: Avi Kivity <avi@redhat.com>
1 parent 7810927 commit 644bfa0

2 files changed

Lines changed: 91 additions & 0 deletions

File tree

‎arch/powerpc/kernel/kvm.c‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@
6767
#define KVM_INST_MTMSRD_L1 0x7c010164
6868
#define KVM_INST_MTMSR 0x7c000124
6969

70+
#define KVM_INST_WRTEEI_0 0x7c000146
71+
#define KVM_INST_WRTEEI_1 0x7c008146
72+
7073
static bool kvm_patching_worked = true;
7174
static char kvm_tmp[1024 * 1024];
7275
static int kvm_tmp_index;
@@ -221,6 +224,47 @@ static void kvm_patch_ins_mtmsr(u32 *inst, u32 rt)
221224
kvm_patch_ins_b(inst, distance_start);
222225
}
223226

227+
#ifdef CONFIG_BOOKE
228+
229+
extern u32 kvm_emulate_wrteei_branch_offs;
230+
extern u32 kvm_emulate_wrteei_ee_offs;
231+
extern u32 kvm_emulate_wrteei_len;
232+
extern u32 kvm_emulate_wrteei[];
233+
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+
241+
p = kvm_alloc(kvm_emulate_wrteei_len * 4);
242+
if (!p)
243+
return;
244+
245+
/* Find out where we are and put everything there */
246+
distance_start = (ulong)p - (ulong)inst;
247+
next_inst = ((ulong)inst + 4);
248+
distance_end = next_inst - (ulong)&p[kvm_emulate_wrteei_branch_offs];
249+
250+
/* Make sure we only write valid b instructions */
251+
if (distance_start > KVM_INST_B_MAX) {
252+
kvm_patching_worked = false;
253+
return;
254+
}
255+
256+
/* Modify the chunk to fit the invocation */
257+
memcpy(p, kvm_emulate_wrteei, kvm_emulate_wrteei_len * 4);
258+
p[kvm_emulate_wrteei_branch_offs] |= distance_end & KVM_INST_B_MASK;
259+
p[kvm_emulate_wrteei_ee_offs] |= (*inst & MSR_EE);
260+
flush_icache_range((ulong)p, (ulong)p + kvm_emulate_wrteei_len * 4);
261+
262+
/* Patch the invocation */
263+
kvm_patch_ins_b(inst, distance_start);
264+
}
265+
266+
#endif
267+
224268
static void kvm_map_magic_page(void *data)
225269
{
226270
kvm_hypercall2(KVM_HC_PPC_MAP_MAGIC_PAGE,
@@ -310,6 +354,12 @@ static void kvm_check_ins(u32 *inst)
310354
}
311355

312356
switch (_inst) {
357+
#ifdef CONFIG_BOOKE
358+
case KVM_INST_WRTEEI_0:
359+
case KVM_INST_WRTEEI_1:
360+
kvm_patch_ins_wrteei(inst);
361+
break;
362+
#endif
313363
}
314364
}
315365

‎arch/powerpc/kernel/kvm_emul.S‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,3 +204,44 @@ kvm_emulate_mtmsr_orig_ins_offs:
204204
.global kvm_emulate_mtmsr_len
205205
kvm_emulate_mtmsr_len:
206206
.long (kvm_emulate_mtmsr_end - kvm_emulate_mtmsr) / 4
207+
208+
209+
210+
.global kvm_emulate_wrteei
211+
kvm_emulate_wrteei:
212+
213+
SCRATCH_SAVE
214+
215+
/* Fetch old MSR in r31 */
216+
LL64(r31, KVM_MAGIC_PAGE + KVM_MAGIC_MSR, 0)
217+
218+
/* Remove MSR_EE from old MSR */
219+
li r30, 0
220+
ori r30, r30, MSR_EE
221+
andc r31, r31, r30
222+
223+
/* OR new MSR_EE onto the old MSR */
224+
kvm_emulate_wrteei_ee:
225+
ori r31, r31, 0
226+
227+
/* Write new MSR value back */
228+
STL64(r31, KVM_MAGIC_PAGE + KVM_MAGIC_MSR, 0)
229+
230+
SCRATCH_RESTORE
231+
232+
/* Go back to caller */
233+
kvm_emulate_wrteei_branch:
234+
b .
235+
kvm_emulate_wrteei_end:
236+
237+
.global kvm_emulate_wrteei_branch_offs
238+
kvm_emulate_wrteei_branch_offs:
239+
.long (kvm_emulate_wrteei_branch - kvm_emulate_wrteei) / 4
240+
241+
.global kvm_emulate_wrteei_ee_offs
242+
kvm_emulate_wrteei_ee_offs:
243+
.long (kvm_emulate_wrteei_ee - kvm_emulate_wrteei) / 4
244+
245+
.global kvm_emulate_wrteei_len
246+
kvm_emulate_wrteei_len:
247+
.long (kvm_emulate_wrteei_end - kvm_emulate_wrteei) / 4

0 commit comments

Comments
 (0)