Skip to contentSkip to conversation

KVM: PPC: Book3S HV: Enable on little endian hosts

PATCH In review Alexander Graf 3 participants 16 messages Jun 11, 2014 – Jun 17, 2014

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

cover KVM: PPC: Book3S HV: Enable on little endian hosts

So far we've been able to successfully run HV KVM on big endian hosts, but once you dive into little endian land things start to fall apart.

This patch set enables HV KVM for little endian hosts. This should be the final piece left missing to get little endian systems fully en par with big endian ones in the KVM world.

Alex

Alexander Graf (6):
PPC: Add asm helpers for BE 32bit load/store
KVM: PPC: Book3S HV: Make HTAB code LE host aware
KVM: PPC: Book3S HV: Access guest VPA in BE
KVM: PPC: Book3S HV: Access host lppaca and shadow slb in BE
KVM: PPC: Book3S HV: Access XICS in BE
KVM: PPC: Book3S HV: Enable for little endian hosts

 arch/powerpc/include/asm/asm-compat.h    |   4 ++
 arch/powerpc/include/asm/kvm_book3s_64.h |  14 +++-
 arch/powerpc/kvm/Kconfig                 |   1 -
 arch/powerpc/kvm/book3s_64_mmu_hv.c      |  95 +++++++++++++-------------
 arch/powerpc/kvm/book3s_hv.c             |  16 ++---
 arch/powerpc/kvm/book3s_hv_ras.c         |   6 +-
 arch/powerpc/kvm/book3s_hv_rm_mmu.c      | 111 ++++++++++++++++++-------------
 arch/powerpc/kvm/book3s_hv_rmhandlers.S  |  37 +++++++----
 8 files changed, 166 insertions(+), 118 deletions(-)
Signature · 1 line
1.8.1.4
#1 lore raw

PATCH 1/6 PPC: Add asm helpers for BE 32bit load/store

Quoted 1 line

From assembly code we might not only have to explicitly BE access 64bit values,

but sometimes also 32bit ones. Add helpers that allow for easy use of lwzx/stwx in their respective byte-reverse or native form.

CC Benjamin Herrenschmidt <[email protected]>
---
 arch/powerpc/include/asm/asm-compat.h | 4 ++++
 1 file changed, 4 insertions(+)
arch/powerpc/include/asm/asm-compat.h +4 -0
index 4b237aa..21164ff 100644
@@ -34,10 +34,14 @@
 #define PPC_MIN_STKFRM	112
 
 #ifdef __BIG_ENDIAN__
+#define LWZX_BE	stringify_in_c(lwzx)
 #define LDX_BE	stringify_in_c(ldx)
+#define STDX_BE	stringify_in_c(stwx)
 #define STDX_BE	stringify_in_c(stdx)
 #else
+#define LWZX_BE	stringify_in_c(lwbrx)
 #define LDX_BE	stringify_in_c(ldbrx)
+#define STWX_BE	stringify_in_c(stwbrx)
 #define STDX_BE	stringify_in_c(stdbrx)
 #endif
 
Signature · 1 line
1.8.1.4
#4 lore raw

PATCH 2/6 KVM: PPC: Book3S HV: Make HTAB code LE host aware

In reply to Alexander Graf

When running on an LE host all data structures are kept in little endian byte order. However, the HTAB still needs to be maintained in big endian.

So every time we access any HTAB we need to make sure we do so in the right byte order. Fix up all accesses to manually byte swap.

---
 arch/powerpc/include/asm/kvm_book3s_64.h |  14 +++-
 arch/powerpc/kvm/book3s_64_mmu_hv.c      |  95 +++++++++++++-------------
 arch/powerpc/kvm/book3s_hv_rm_mmu.c      | 111 ++++++++++++++++++-------------
 3 files changed, 126 insertions(+), 94 deletions(-)
arch/powerpc/include/asm/kvm_book3s_64.h +11 -3
index fddb72b..2e99a3e 100644
@@ -61,18 +61,26 @@ extern unsigned long kvm_rma_pages;
 
 static inline long try_lock_hpte(unsigned long *hpte, unsigned long bits)
 {
-	unsigned long tmp, old;
+	unsigned long tmp, old, lockbit;
+
+	/*
+	 * We load/store in native endian, but the HTAB is in big endian. If
+	 * we byte swap all data we apply on the PTE we're implicitly correct
+	 * again.
+	 */
+	lockbit = cpu_to_be64(HPTE_V_HVLOCK);
+	bits = cpu_to_be64(bits);
 
 	asm volatile("	ldarx	%0,0,%2\n"
 		     "	and.	%1,%0,%3\n"
 		     "	bne	2f\n"
-		     "	ori	%0,%0,%4\n"
+		     "	or	%0,%0,%4\n"
 		     "  stdcx.	%0,0,%2\n"
 		     "	beq+	2f\n"
 		     "	mr	%1,%3\n"
 		     "2:	isync"
 		     : "=&r" (tmp), "=&r" (old)
-		     : "r" (hpte), "r" (bits), "i" (HPTE_V_HVLOCK)
+		     : "r" (hpte), "r" (bits), "r" (lockbit)
 		     : "cc", "memory");
 	return old == 0;
 }
arch/powerpc/kvm/book3s_64_mmu_hv.c +50 -45

271 lines not on this page · raw message

arch/powerpc/kvm/book3s_hv_rm_mmu.c +65 -46

290 lines not on this page · raw message

Signature · 1 line
1.8.1.4
#5 lore raw

PATCH 3/6 KVM: PPC: Book3S HV: Access guest VPA in BE

In reply to Alexander Graf

There are a few shared data structures between the host and the guest. Most of them get registered through the VPA interface.

These data structures are defined to always be in big endian byte order, so let's make sure we always access them in big endian.

---
 arch/powerpc/kvm/book3s_hv.c     | 16 ++++++++--------
 arch/powerpc/kvm/book3s_hv_ras.c |  6 +++---
 2 files changed, 11 insertions(+), 11 deletions(-)
arch/powerpc/kvm/book3s_hv.c +8 -8
index c38cf9f..f8640f4 100644
@@ -270,7 +270,7 @@ struct kvm_vcpu *kvmppc_find_vcpu(struct kvm *kvm, int id)
 static void init_vpa(struct kvm_vcpu *vcpu, struct lppaca *vpa)
 {
 	vpa->__old_status |= LPPACA_OLD_SHARED_PROC;
-	vpa->yield_count = 1;
+	vpa->yield_count = cpu_to_be32(1);
 }
 
 static int set_vpa(struct kvm_vcpu *vcpu, struct kvmppc_vpa *v,
@@ -333,9 +333,9 @@ static unsigned long do_h_register_vpa(struct kvm_vcpu *vcpu,
 		if (va = NULL)
 			return H_PARAMETER;
 		if (subfunc = H_VPA_REG_VPA)
-			len = ((struct reg_vpa *)va)->length.hword;
+			len = be16_to_cpu(((struct reg_vpa *)va)->length.hword);
 		else
-			len = ((struct reg_vpa *)va)->length.word;
+			len = be32_to_cpu(((struct reg_vpa *)va)->length.word);
 		kvmppc_unpin_guest_page(kvm, va, vpa, false);
 
 		/* Check length */
@@ -540,11 +540,11 @@ static void kvmppc_create_dtl_entry(struct kvm_vcpu *vcpu,
 		return;
 	memset(dt, 0, sizeof(struct dtl_entry));
 	dt->dispatch_reason = 7;
-	dt->processor_id = vc->pcpu + vcpu->arch.ptid;
-	dt->timebase = now + vc->tb_offset;
-	dt->enqueue_to_dispatch_time = stolen;
-	dt->srr0 = kvmppc_get_pc(vcpu);
-	dt->srr1 = vcpu->arch.shregs.msr;
+	dt->processor_id = cpu_to_be16(vc->pcpu + vcpu->arch.ptid);
+	dt->timebase = cpu_to_be64(now + vc->tb_offset);
+	dt->enqueue_to_dispatch_time = cpu_to_be32(stolen);
+	dt->srr0 = cpu_to_be64(kvmppc_get_pc(vcpu));
+	dt->srr1 = cpu_to_be64(vcpu->arch.shregs.msr);
 	++dt;
 	if (dt = vcpu->arch.dtl.pinned_end)
 		dt = vcpu->arch.dtl.pinned_addr;
arch/powerpc/kvm/book3s_hv_ras.c +3 -3
index 768a9f9..6a3ba8a 100644
@@ -45,14 +45,14 @@ static void reload_slb(struct kvm_vcpu *vcpu)
 		return;
 
 	/* Sanity check */
-	n = min_t(u32, slb->persistent, SLB_MIN_SIZE);
+	n = min_t(u32, be32_to_cpu(slb->persistent), SLB_MIN_SIZE);
 	if ((void *) &slb->save_area[n] > vcpu->arch.slb_shadow.pinned_end)
 		return;
 
 	/* Load up the SLB from that */
 	for (i = 0; i < n; ++i) {
-		unsigned long rb = slb->save_area[i].esid;
-		unsigned long rs = slb->save_area[i].vsid;
+		unsigned long rb = be64_to_cpu(slb->save_area[i].esid);
+		unsigned long rs = be64_to_cpu(slb->save_area[i].vsid);
 
 		rb = (rb & ~0xFFFul) | i;	/* insert entry number */
 		asm volatile("slbmte %0,%1" : : "r" (rs), "r" (rb));
Signature · 1 line
1.8.1.4
#6 lore raw

PATCH 4/6 KVM: PPC: Book3S HV: Access host lppaca and shadow slb in BE

In reply to Alexander Graf

Some data structures are always stored in big endian. Among those are the LPPACA fields as well as the shadow slb. These structures might be shared with a hypervisor.

So whenever we access those fields, make sure we do so in big endian byte order.

---
 arch/powerpc/kvm/book3s_hv_rmhandlers.S | 20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)
arch/powerpc/kvm/book3s_hv_rmhandlers.S +10 -10
index 220aefb..3530d06 100644
@@ -32,10 +32,6 @@
 
 #define VCPU_GPRS_TM(reg) (((reg) * ULONG_SIZE) + VCPU_GPR_TM)
 
-#ifdef __LITTLE_ENDIAN__
-#error Need to fix lppaca and SLB shadow accesses in little endian mode
-#endif
-
 /* Values in HSTATE_NAPPING(r13) */
 #define NAPPING_CEDE	1
 #define NAPPING_NOVCPU	2
@@ -585,9 +581,10 @@ kvmppc_got_guest:
 	ld	r3, VCPU_VPA(r4)
 	cmpdi	r3, 0
 	beq	25f
-	lwz	r5, LPPACA_YIELDCOUNT(r3)
+	li	r6, LPPACA_YIELDCOUNT
+	LWZX_BE	r5, r3, r6
 	addi	r5, r5, 1
-	stw	r5, LPPACA_YIELDCOUNT(r3)
+	STWX_BE	r5, r3, r6
 	li	r6, 1
 	stb	r6, VCPU_VPA_DIRTY(r4)
 25:
@@ -1328,9 +1325,10 @@ END_FTR_SECTION_IFSET(CPU_FTR_ARCH_206)
 	ld	r8, VCPU_VPA(r9)	/* do they have a VPA? */
 	cmpdi	r8, 0
 	beq	25f
-	lwz	r3, LPPACA_YIELDCOUNT(r8)
+	li	r4, LPPACA_YIELDCOUNT
+	LWZX_BE	r3, r8, r4
 	addi	r3, r3, 1
-	stw	r3, LPPACA_YIELDCOUNT(r8)
+	STWX_BE	r3, r8, r4
 	li	r3, 1
 	stb	r3, VCPU_VPA_DIRTY(r9)
 25:
@@ -1643,8 +1641,10 @@ END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
 33:	ld	r8,PACA_SLBSHADOWPTR(r13)
 
 	.rept	SLB_NUM_BOLTED
-	ld	r5,SLBSHADOW_SAVEAREA(r8)
-	ld	r6,SLBSHADOW_SAVEAREA+8(r8)
+	li	r3, SLBSHADOW_SAVEAREA
+	LDX_BE	r5, r8, r3
+	addi	r3, r3, 8
+	LDX_BE	r6, r8, r3
 	andis.	r7,r5,SLB_ESID_V@h
 	beq	1f
 	slbmte	r6,r5
Signature · 1 line
1.8.1.4
#7 lore raw

PATCH 5/6 KVM: PPC: Book3S HV: Access XICS in BE

In reply to Alexander Graf

On the exit path from the guest we check what type of interrupt we received if we received one. This means we're doing hardware access to the XICS interrupt controller.

However, when running on a little endian system, this access is byte reversed.

So let's make sure to swizzle the bytes back again and virtuall make XICS accesses big endian.

---
 arch/powerpc/kvm/book3s_hv_rmhandlers.S | 17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)
arch/powerpc/kvm/book3s_hv_rmhandlers.S +15 -2
index 3530d06..40dec37 100644
@@ -36,6 +36,13 @@
 #define NAPPING_CEDE	1
 #define NAPPING_NOVCPU	2
 
+.macro bswap32 regd, regs
+	srwi	\regd,\regs,24
+	rlwimi	\regd,\regs,24,16,23
+	rlwimi	\regd,\regs,8,8,15
+	rlwimi	\regd,\regs,24,0,7
+.endm
+
 /*
  * Call kvmppc_hv_entry in real mode.
  * Must be called with interrupts hard-disabled.
@@ -2206,7 +2213,12 @@ kvmppc_read_intr:
 	cmpdi	r6, 0
 	beq-	1f
 	lwzcix	r0, r6, r7
-	rlwinm.	r3, r0, 0, 0xffffff
+#ifdef __LITTLE_ENDIAN__
+	bswap32	r3, r0
+#else
+	mr	r3, r0
+#endif
+	rlwinm.	r3, r3, 0, 0xffffff
 	sync
 	beq	1f			/* if nothing pending in the ICP */
 
@@ -2241,7 +2253,8 @@ kvmppc_read_intr:
 42:	/* It's not an IPI and it's for the host, stash it in the PACA
 	 * before exit, it will be picked up by the host ICP driver
 	 */
-	stw	r0, HSTATE_SAVED_XIRR(r13)
+	li	r4, HSTATE_SAVED_XIRR
+	STWX_BE	r0, r13, r4
 	li	r3, 1
 	b	1b
 
Signature · 1 line
1.8.1.4
#13 lore raw

PATCH 6/6 KVM: PPC: Book3S HV: Enable for little endian hosts

In reply to Alexander Graf

Now that we've fixed all the issues that HV KVM code had on little endian hosts, we can enable it in the kernel configuration for users to play with.

---
 arch/powerpc/kvm/Kconfig | 1 -
 1 file changed, 1 deletion(-)
arch/powerpc/kvm/Kconfig +0 -1
index d6a53b9..8aeeda1 100644
@@ -75,7 +75,6 @@ config KVM_BOOK3S_64
 config KVM_BOOK3S_64_HV
 	tristate "KVM support for POWER7 and PPC970 using hypervisor mode in host"
 	depends on KVM_BOOK3S_64
-	depends on !CPU_LITTLE_ENDIAN
 	select KVM_BOOK3S_HV_POSSIBLE
 	select MMU_NOTIFIER
 	select CMA
Signature · 1 line
1.8.1.4
#14 lore raw
In reply to Alexander Graf
Quoted 6 lines · Alexander Graf

So far we've been able to successfully run HV KVM on big endian hosts, but once you dive into little endian land things start to fall apart.

This patch set enables HV KVM for little endian hosts. This should be the final piece left missing to get little endian systems fully en par with big endian ones in the KVM world.

Can we also use types like __be64 where we need them ? That will also make sure that tools like sparse can catch errors ?

-aneesh

#15 lore raw
Quoted 10 lines · Aneesh Kumar K.V
Quoted 6 lines · Alexander Graf

So far we've been able to successfully run HV KVM on big endian hosts, but once you dive into little endian land things start to fall apart.

This patch set enables HV KVM for little endian hosts. This should be the final piece left missing to get little endian systems fully en par with big endian ones in the KVM world.

Can we also use types like __be64 where we need them ? That will also make sure that tools like sparse can catch errors ?

Great idea :). Will do.

Alex

#2 lore raw

Re: [PATCH 1/6] PPC: Add asm helpers for BE 32bit load/store

In reply to Alexander Graf
Quoted 15 lines · Alexander Graf
Quoted 1 line

From assembly code we might not only have to explicitly BE access 64bit values,

but sometimes also 32bit ones. Add helpers that allow for easy use of lwzx/stwx in their respective byte-reverse or native form.

CC Benjamin Herrenschmidt <[email protected]>
---
 arch/powerpc/include/asm/asm-compat.h | 4 ++++
 1 file changed, 4 insertions(+)
arch/powerpc/include/asm/asm-compat.h +0 -0
index 4b237aa..21164ff 100644

arch/powerpc/include/asm/asm-compat.h line 39

Quoted 6 lines
arch/powerpc/include/asm/asm-compat.h +2 -0
@@ -34,10 +34,14 @@
 #define PPC_MIN_STKFRM	112
 
 #ifdef __BIG_ENDIAN__
+#define LWZX_BE	stringify_in_c(lwzx)
 #define LDX_BE	stringify_in_c(ldx)
+#define STDX_BE	stringify_in_c(stwx)

This should be STWX_BE, shouldn't it?

arch/powerpc/include/asm/asm-compat.h line 46

Quoted 7 lines
arch/powerpc/include/asm/asm-compat.h +2 -0
 #define STDX_BE	stringify_in_c(stdx)
 #else
+#define LWZX_BE	stringify_in_c(lwbrx)
 #define LDX_BE	stringify_in_c(ldbrx)
+#define STWX_BE	stringify_in_c(stwbrx)
 #define STDX_BE	stringify_in_c(stdbrx)
 #endif

Paul.

#8 lore raw

Re: [PATCH 5/6] KVM: PPC: Book3S HV: Access XICS in BE

In reply to Alexander Graf
Quoted 8 lines · Alexander Graf

On the exit path from the guest we check what type of interrupt we received if we received one. This means we're doing hardware access to the XICS interrupt controller.

However, when running on a little endian system, this access is byte reversed.

So let's make sure to swizzle the bytes back again and virtuall make XICS accesses big endian.

...

arch/powerpc/kvm/book3s_hv_rmhandlers.S kvmppc_read_intr: · line 2257

Quoted 7 lines
arch/powerpc/kvm/book3s_hv_rmhandlers.S +2 -1
@@ -2241,7 +2253,8 @@ kvmppc_read_intr:
 42:	/* It's not an IPI and it's for the host, stash it in the PACA
 	 * before exit, it will be picked up by the host ICP driver
 	 */
-	stw	r0, HSTATE_SAVED_XIRR(r13)
+	li	r4, HSTATE_SAVED_XIRR
+	STWX_BE	r0, r13, r4

This is a paca field, not something mandated by PAPR or shared with the guest, so why do we need to keep it BE? If you do make it BE, don't you also need to fix kvmppc_get_xics_latch()?

Paul.

#3 lore raw

Re: [PATCH 1/6] PPC: Add asm helpers for BE 32bit load/store

In reply to Paul Mackerras
Quoted 18 lines · Paul Mackerras
Quoted 17 lines · Alexander Graf
Quoted 1 line

From assembly code we might not only have to explicitly BE access 64bit values,

but sometimes also 32bit ones. Add helpers that allow for easy use of lwzx/stwx in their respective byte-reverse or native form.

CC Benjamin Herrenschmidt <[email protected]>
---
  arch/powerpc/include/asm/asm-compat.h | 4 ++++
  1 file changed, 4 insertions(+)
arch/powerpc/include/asm/asm-compat.h +0 -0
index 4b237aa..21164ff 100644
@@ -34,10 +34,14 @@
  #define PPC_MIN_STKFRM	112
 
Quoted 5 lines

arch/powerpc/include/asm/asm-compat.h line 39

Quoted 4 lines
arch/powerpc/include/asm/asm-compat.h +2 -0
  #ifdef __BIG_ENDIAN__
+#define LWZX_BE	stringify_in_c(lwzx)
  #define LDX_BE	stringify_in_c(ldx)
+#define STDX_BE	stringify_in_c(stwx)

This should be STWX_BE, shouldn't it?

And there I thought I would get away without anyone noticing it :). Yes, of course.

Alex

#9 lore raw

Re: [PATCH 5/6] KVM: PPC: Book3S HV: Access XICS in BE

In reply to Paul Mackerras
Quoted 11 lines · Paul Mackerras
Quoted 8 lines · Alexander Graf

On the exit path from the guest we check what type of interrupt we received if we received one. This means we're doing hardware access to the XICS interrupt controller.

However, when running on a little endian system, this access is byte reversed.

So let's make sure to swizzle the bytes back again and virtuall make XICS accesses big endian.

...

Quoted 10 lines

arch/powerpc/kvm/book3s_hv_rmhandlers.S kvmppc_read_intr: · line 2257

Quoted 7 lines
arch/powerpc/kvm/book3s_hv_rmhandlers.S +2 -1
@@ -2241,7 +2253,8 @@ kvmppc_read_intr:
  42:	/* It's not an IPI and it's for the host, stash it in the PACA
  	 * before exit, it will be picked up by the host ICP driver
  	 */
-	stw	r0, HSTATE_SAVED_XIRR(r13)
+	li	r4, HSTATE_SAVED_XIRR
+	STWX_BE	r0, r13, r4

This is a paca field, not something mandated by PAPR or shared with the guest, so why do we need to keep it BE? If you do make it BE, don't you also need to fix kvmppc_get_xics_latch()?

Yikes. Yes. Thanks a lot for the catch!

Alex

#10 lore raw

Re: [PATCH 5/6] KVM: PPC: Book3S HV: Access XICS in BE

Quoted 24 lines · Alexander Graf
Quoted 11 lines · Paul Mackerras
Quoted 8 lines · Alexander Graf

On the exit path from the guest we check what type of interrupt we received if we received one. This means we're doing hardware access to the XICS interrupt controller.

However, when running on a little endian system, this access is byte reversed.

So let's make sure to swizzle the bytes back again and virtuall make XICS accesses big endian.

...

Quoted 10 lines

arch/powerpc/kvm/book3s_hv_rmhandlers.S kvmppc_read_intr: · line 2257

Quoted 7 lines
arch/powerpc/kvm/book3s_hv_rmhandlers.S +2 -1
@@ -2241,7 +2253,8 @@ kvmppc_read_intr:
  42:    /* It's not an IPI and it's for the host, stash it in the PACA
       * before exit, it will be picked up by the host ICP driver
       */
-    stw    r0, HSTATE_SAVED_XIRR(r13)
+    li    r4, HSTATE_SAVED_XIRR
+    STWX_BE    r0, r13, r4

This is a paca field, not something mandated by PAPR or shared with the guest, so why do we need to keep it BE? If you do make it BE, don't you also need to fix kvmppc_get_xics_latch()?

Quoted 1 line

Yikes. Yes. Thanks a lot for the catch!

Eh, no. What we do is we read (good on BE, byte reversed) into r0. Then we swab32() from r0 to r3 on LE, mr from r0 to r3 on BE.

r3 gets truncated along the way.

The reason we maintain r0 as wrong-endian is that we write it back using the cache inhibited stwcix instruction:

Quoted 1 line

stwcix r0, r6, r7 /* EOI it */

So during the lifetime of r0 as XIRR it's always byte-reversed on LE. That's why we store it using STWX_BE into hstate, because that's the time when we actually swab32() it for further interpretation.

Alternatively I could clobber a different register and maintain the byte swapped variant in there if you prefer.

Alex

#11 lore raw

Re: [PATCH 5/6] KVM: PPC: Book3S HV: Access XICS in BE

Quoted 6 lines · Alexander Graf

Eh, no. What we do is we read (good on BE, byte reversed) into r0. Then we swab32() from r0 to r3 on LE, mr from r0 to r3 on BE.

r3 gets truncated along the way.

Quoted 8 lines

The reason we maintain r0 as wrong-endian is that we write it back using the cache inhibited stwcix instruction:

Quoted 1 line

stwcix r0, r6, r7 /* EOI it */

So during the lifetime of r0 as XIRR it's always byte-reversed on LE. That's why we store it using STWX_BE into hstate, because that's the time when we actually swab32() it for further interpretation.

So the STWX_BE is more like a be32_to_cpu than a cpu_to_be32, which is what the name STWX_BE would suggest. Sounds like it at least deserves a comment, or (as you suggest) rearrange the register usage so a normal store works.

Paul.

#12 lore raw

Re: [PATCH 5/6] KVM: PPC: Book3S HV: Access XICS in BE

Quoted 9 lines · Paul Mackerras
Quoted 8 lines · Alexander Graf

Eh, no. What we do is we read (good on BE, byte reversed) into r0. Then we swab32() from r0 to r3 on LE, mr from r0 to r3 on BE.

r3 gets truncated along the way.

The reason we maintain r0 as wrong-endian is that we write it back using the cache inhibited stwcix instruction:

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

stwcix r0, r6, r7 /* EOI it */

So during the lifetime of r0 as XIRR it's always byte-reversed on LE. That's why we store it using STWX_BE into hstate, because that's the time when we actually swab32() it for further interpretation.

So the STWX_BE is more like a be32_to_cpu than a cpu_to_be32, which is what the name STWX_BE would suggest. Sounds like it at least deserves a comment, or (as you suggest) rearrange the register usage so a normal store works.

Yes, I have this now:

From a94a66437ec714ec5650f6d8fec050a33e4477ca Mon Sep 17 00:00:00 2001
From: Alexander Graf <[email protected]>
Date: Wed, 11 Jun 2014 10:37:52 +0200
Subject: [PATCH] KVM: PPC: Book3S HV: Access XICS in BE

On the exit path from the guest we check what type of interrupt we received if we received one. This means we're doing hardware access to the XICS interrupt controller.

However, when running on a little endian system, this access is byte reversed.

So let's make sure to swizzle the bytes back again and virtually make XICS accesses big endian.

---

v1 -> v2:

- Make code easier to follow

arch/powerpc/kvm/book3s_hv_rmhandlers.S +14 -2
index 1a2f471..9829e18 100644
@@ -36,6 +36,13 @@
 #define NAPPING_CEDE    1
 #define NAPPING_NOVCPU    2
 
+.macro bswap32 regd, regs
+    srwi    \regd,\regs,24
+    rlwimi    \regd,\regs,24,16,23
+    rlwimi    \regd,\regs,8,8,15
+    rlwimi    \regd,\regs,24,0,7
+.endm
+
 /*
  * Call kvmppc_hv_entry in real mode.
  * Must be called with interrupts hard-disabled.
@@ -2325,7 +2332,12 @@ kvmppc_read_intr:
     cmpdi    r6, 0
     beq-    1f
     lwzcix    r0, r6, r7
-    rlwinm.    r3, r0, 0, 0xffffff
+#ifdef __LITTLE_ENDIAN__
+    bswap32    r4, r0
+#else
+    mr    r4, r0
+#endif
+    rlwinm.    r3, r4, 0, 0xffffff
     sync
     beq    1f            /* if nothing pending in the ICP */
 
@@ -2360,7 +2372,7 @@ kvmppc_read_intr:
 42:    /* It's not an IPI and it's for the host, stash it in the PACA
      * before exit, it will be picked up by the host ICP driver
      */
-    stw    r0, HSTATE_SAVED_XIRR(r13)
+    stw    r4, HSTATE_SAVED_XIRR(r13)
     li    r3, 1
     b    1b
 

Alex