Skip to contentSkip to conversation

target-ppc: ppc64 target's virtio can be either endian.

PATCH Superseded Greg Kurz 3 participants 15 messages Nov 25, 2013 – Jan 20, 2014

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Greg Kurz

author
#0 lore raw

PATCH 1/1 target-ppc: ppc64 target's virtio can be either endian.

We base it on the OS endian, as reflected by the endianness of the interrupt vectors (handled through the ILE bit in the LPCR register).

This patch does two things:
- make LPCR a KVM register
- implement virtio_get_byteswap() over LPCR

This patch requires to have the following defined in the linux headers:

$ grep LPCR linux-headers/asm-powerpc/kvm.h
#define KVM_REG_PPC_LPCR        (KVM_REG_PPC | KVM_REG_SIZE_U32 | 0xb5)
---
 target-ppc/kvm.c         |    4 ++++
 target-ppc/misc_helper.c |   14 ++++++++++++++
 2 files changed, 18 insertions(+)
target-ppc/kvm.c +4 -0
index 10d0cd9..b450a22 100644
@@ -869,6 +869,8 @@ int kvm_arch_put_registers(CPUState *cs, int level)
                 DPRINTF("Warning: Unable to set VPA information to KVM\n");
             }
         }
+
+        kvm_put_one_spr(cs, KVM_REG_PPC_LPCR, SPR_LPCR);
 #endif /* TARGET_PPC64 */
     }
 
@@ -1091,6 +1093,8 @@ int kvm_arch_get_registers(CPUState *cs)
                 DPRINTF("Warning: Unable to get VPA information from KVM\n");
             }
         }
+
+        kvm_get_one_spr(cs, KVM_REG_PPC_LPCR, SPR_LPCR);
 #endif
     }
 
target-ppc/misc_helper.c +14 -0
index 616aab6..0e0743a 100644
@@ -20,6 +20,8 @@
 #include "helper.h"
 
 #include "helper_regs.h"
+#include "hw/virtio/virtio.h"
+#include "sysemu/kvm.h"
 
 /*****************************************************************************/
 /* SPR accesses */
@@ -116,3 +118,15 @@ void ppc_store_msr(CPUPPCState *env, target_ulong value)
 {
     hreg_store_msr(env, value, 0);
 }
+
+bool virtio_get_byteswap(void)
+{
+    PowerPCCPU *cp = POWERPC_CPU(first_cpu);
+    CPUPPCState *env = &cp->env;
+
+    if (kvm_enabled()) { 
+        kvm_arch_get_registers(first_cpu);
+    }
+
+    return env->spr[SPR_LPCR] & LPCR_ILE;
+}
#1 lore raw
Quoted 57 lines · Greg Kurz

We base it on the OS endian, as reflected by the endianness of the interrupt vectors (handled through the ILE bit in the LPCR register).

This patch does two things:
- make LPCR a KVM register
- implement virtio_get_byteswap() over LPCR

This patch requires to have the following defined in the linux headers:

$ grep LPCR linux-headers/asm-powerpc/kvm.h
#define KVM_REG_PPC_LPCR        (KVM_REG_PPC | KVM_REG_SIZE_U32 | 0xb5)

---
target-ppc/kvm.c | 4 ++++
target-ppc/misc_helper.c | 14 ++++++++++++++

2 files changed, 18 insertions(+)
target-ppc/kvm.c +4 -0
index 10d0cd9..b450a22 100644
@@ -869,6 +869,8 @@ int kvm_arch_put_registers(CPUState *cs, int level)
                DPRINTF("Warning: Unable to set VPA information to KVM\n");
            }
        }
+
+        kvm_put_one_spr(cs, KVM_REG_PPC_LPCR, SPR_LPCR);
 #endif /* TARGET_PPC64 */
    }
 
@@ -1091,6 +1093,8 @@ int kvm_arch_get_registers(CPUState *cs)
                DPRINTF("Warning: Unable to get VPA information from KVM\n");
            }
        }
+
+        kvm_get_one_spr(cs, KVM_REG_PPC_LPCR, SPR_LPCR);
 #endif
    }
 
target-ppc/misc_helper.c +2 -0
index 616aab6..0e0743a 100644
@@ -20,6 +20,8 @@
 #include "helper.h"
 
 #include "helper_regs.h"
+#include "hw/virtio/virtio.h"
+#include "sysemu/kvm.h"
 
 /*****************************************************************************/
 /* SPR accesses */
@@ -116,3 +118,15 @@ void ppc_store_msr(CPUPPCState *env, target_ulong value)
 {

target-ppc/misc_helper.c void ppc_store_msr(CPUPPCState *env, target_ulong value) · line 128

Quoted 10 lines
target-ppc/misc_helper.c +8 -0
    hreg_store_msr(env, value, 0);
 }
+
+bool virtio_get_byteswap(void)
+{
+    PowerPCCPU *cp = POWERPC_CPU(first_cpu);
+    CPUPPCState *env = &cp->env;
+
+    if (kvm_enabled()) { 
+        kvm_arch_get_registers(first_cpu);

This function is not defined when CONFIG_KVM is disabled.

Please do

    cpu_synchronize_state(first_cpu);

instead.

Also can't virtio_get_byteswap pass in the CPU pointer of the CPU that's calling in this moment? I'm not sure how racy it is to synchronize the first cpu while we're not in the first cpu's execution thread.

Either way, if we do this "right" we don't even have to jump through these hoops, as little endian setting simply happens steered from QEMU, so QEMU will have all knowledge about the guest's little endian mode without the need to synchronize any state.

Alex

author
#2 lore raw
Quoted 73 lines · On Mon, 9 Dec 2013 16:33:59 +0100 Alexander Graf <[email protected]> wrote:
Quoted 57 lines · Greg Kurz

We base it on the OS endian, as reflected by the endianness of the interrupt vectors (handled through the ILE bit in the LPCR register).

This patch does two things:
- make LPCR a KVM register
- implement virtio_get_byteswap() over LPCR

This patch requires to have the following defined in the linux headers:

$ grep LPCR linux-headers/asm-powerpc/kvm.h
#define KVM_REG_PPC_LPCR        (KVM_REG_PPC | KVM_REG_SIZE_U32 | 0xb5)

---
target-ppc/kvm.c | 4 ++++
target-ppc/misc_helper.c | 14 ++++++++++++++

2 files changed, 18 insertions(+)
target-ppc/kvm.c +4 -0
index 10d0cd9..b450a22 100644
@@ -869,6 +869,8 @@ int kvm_arch_put_registers(CPUState *cs, int level)
                DPRINTF("Warning: Unable to set VPA information to
 KVM\n"); }
        }
+
+        kvm_put_one_spr(cs, KVM_REG_PPC_LPCR, SPR_LPCR);
 #endif /* TARGET_PPC64 */
    }
 
@@ -1091,6 +1093,8 @@ int kvm_arch_get_registers(CPUState *cs)
                DPRINTF("Warning: Unable to get VPA information from
 KVM\n"); }
        }
+
+        kvm_get_one_spr(cs, KVM_REG_PPC_LPCR, SPR_LPCR);
 #endif
    }
 
target-ppc/misc_helper.c +2 -0
index 616aab6..0e0743a 100644
@@ -20,6 +20,8 @@
 #include "helper.h"
 
 #include "helper_regs.h"
+#include "hw/virtio/virtio.h"
+#include "sysemu/kvm.h"
 
 /*****************************************************************************/
 /* SPR accesses */
@@ -116,3 +118,15 @@ void ppc_store_msr(CPUPPCState *env, target_ulong
 value) {

target-ppc/misc_helper.c void ppc_store_msr(CPUPPCState *env, target_ulong · line 128

Quoted 10 lines
target-ppc/misc_helper.c +8 -0
    hreg_store_msr(env, value, 0);
 }
+
+bool virtio_get_byteswap(void)
+{
+    PowerPCCPU *cp = POWERPC_CPU(first_cpu);
+    CPUPPCState *env = &cp->env;
+
+    if (kvm_enabled()) { 
+        kvm_arch_get_registers(first_cpu);

This function is not defined when CONFIG_KVM is disabled.

Quoted 6 lines

Please do

    cpu_synchronize_state(first_cpu);

instead.

Oups... I'll fix that.

Quoted 4 lines

Also can't virtio_get_byteswap pass in the CPU pointer of the CPU that's calling in this moment? I'm not sure how racy it is to synchronize the first cpu while we're not in the first cpu's execution thread.

I kept the choices made by Rusty in the original serie. According to these, I am not sure if we can use current_cpu:

https://lists.gnu.org/archive/html/qemu-devel/2013-08/msg01156.html
https://lists.gnu.org/archive/html/qemu-devel/2013-08/msg01504.html

Quoted 5 lines

Either way, if we do this "right" we don't even have to jump through these hoops, as little endian setting simply happens steered from QEMU, so QEMU will have all knowledge about the guest's little endian mode without the need to synchronize any state.

Sure. This is just primary work to get virtio working for cross-endian cases. :)

Thanks for the review.

Cheers.

--
Greg

#3 lore raw
Quoted 95 lines · Greg Kurz
Quoted 73 lines · On Mon, 9 Dec 2013 16:33:59 +0100 Alexander Graf <[email protected]> wrote:
Quoted 57 lines · Greg Kurz

We base it on the OS endian, as reflected by the endianness of the interrupt vectors (handled through the ILE bit in the LPCR register).

This patch does two things:
- make LPCR a KVM register
- implement virtio_get_byteswap() over LPCR

This patch requires to have the following defined in the linux headers:

$ grep LPCR linux-headers/asm-powerpc/kvm.h
#define KVM_REG_PPC_LPCR        (KVM_REG_PPC | KVM_REG_SIZE_U32 | 0xb5)

---
target-ppc/kvm.c | 4 ++++
target-ppc/misc_helper.c | 14 ++++++++++++++

2 files changed, 18 insertions(+)
target-ppc/kvm.c +4 -0
index 10d0cd9..b450a22 100644
@@ -869,6 +869,8 @@ int kvm_arch_put_registers(CPUState *cs, int level)
               DPRINTF("Warning: Unable to set VPA information to
 KVM\n"); }
       }
+
+        kvm_put_one_spr(cs, KVM_REG_PPC_LPCR, SPR_LPCR);
 #endif /* TARGET_PPC64 */
   }
 
@@ -1091,6 +1093,8 @@ int kvm_arch_get_registers(CPUState *cs)
               DPRINTF("Warning: Unable to get VPA information from
 KVM\n"); }
       }
+
+        kvm_get_one_spr(cs, KVM_REG_PPC_LPCR, SPR_LPCR);
 #endif
   }
 
target-ppc/misc_helper.c +2 -0
index 616aab6..0e0743a 100644
@@ -20,6 +20,8 @@
 #include "helper.h"
 
 #include "helper_regs.h"
+#include "hw/virtio/virtio.h"
+#include "sysemu/kvm.h"
 
 /*****************************************************************************/
 /* SPR accesses */
@@ -116,3 +118,15 @@ void ppc_store_msr(CPUPPCState *env, target_ulong
 value) {

target-ppc/misc_helper.c void ppc_store_msr(CPUPPCState *env, target_ulong · line 128

Quoted 10 lines
target-ppc/misc_helper.c +8 -0
   hreg_store_msr(env, value, 0);
 }
+
+bool virtio_get_byteswap(void)
+{
+    PowerPCCPU *cp = POWERPC_CPU(first_cpu);
+    CPUPPCState *env = &cp->env;
+
+    if (kvm_enabled()) { 
+        kvm_arch_get_registers(first_cpu);

This function is not defined when CONFIG_KVM is disabled.

Quoted 6 lines

Please do

   cpu_synchronize_state(first_cpu);

instead.

Oups... I'll fix that.

Quoted 4 lines

Also can't virtio_get_byteswap pass in the CPU pointer of the CPU that's calling in this moment? I'm not sure how racy it is to synchronize the first cpu while we're not in the first cpu's execution thread.

I kept the choices made by Rusty in the original serie. According to these, I am not sure if we can use current_cpu:

https://lists.gnu.org/archive/html/qemu-devel/2013-08/msg01156.html
https://lists.gnu.org/archive/html/qemu-devel/2013-08/msg01504.html

Quoted 8 lines
Quoted 5 lines

Either way, if we do this "right" we don't even have to jump through these hoops, as little endian setting simply happens steered from QEMU, so QEMU will have all knowledge about the guest's little endian mode without the need to synchronize any state.

Sure. This is just primary work to get virtio working for cross-endian cases. :)

Yeah, but so far LE switching code is not upstream in KVM - and for TCG we don't need the cpu_synchronize_state() call at all.

Alex

author
#4 lore raw
Quoted 107 lines · On Mon, 9 Dec 2013 18:18:26 +0100 Alexander Graf <[email protected]> wrote:
Quoted 104 lines · Greg Kurz
Quoted 75 lines · On Mon, 9 Dec 2013 16:33:59 +0100 Alexander Graf <[email protected]> wrote:
Quoted 59 lines · Greg Kurz

We base it on the OS endian, as reflected by the endianness of the interrupt vectors (handled through the ILE bit in the LPCR register).

This patch does two things:
- make LPCR a KVM register
- implement virtio_get_byteswap() over LPCR

This patch requires to have the following defined in the linux headers:

$ grep LPCR linux-headers/asm-powerpc/kvm.h
#define KVM_REG_PPC_LPCR        (KVM_REG_PPC | KVM_REG_SIZE_U32 |
0xb5)

---
target-ppc/kvm.c | 4 ++++
target-ppc/misc_helper.c | 14 ++++++++++++++

2 files changed, 18 insertions(+)
target-ppc/kvm.c +0 -0
index 10d0cd9..b450a22 100644
@@ -869,6 +869,8 @@ int kvm_arch_put_registers(CPUState *cs, int

level) DPRINTF("Warning: Unable to set VPA information to KVM\n"); }

       }
target-ppc/kvm.c +2 -0
+
+        kvm_put_one_spr(cs, KVM_REG_PPC_LPCR, SPR_LPCR);
#endif /* TARGET_PPC64 */
   }
target-ppc/kvm.c +2 -0
@@ -1091,6 +1093,8 @@ int kvm_arch_get_registers(CPUState *cs)
               DPRINTF("Warning: Unable to get VPA information from
 KVM\n"); }
       }
+
+        kvm_get_one_spr(cs, KVM_REG_PPC_LPCR, SPR_LPCR);
 #endif
   }
 
target-ppc/misc_helper.c +2 -0
index 616aab6..0e0743a 100644
@@ -20,6 +20,8 @@
 #include "helper.h"
 
 #include "helper_regs.h"
+#include "hw/virtio/virtio.h"
+#include "sysemu/kvm.h"
 
 /*****************************************************************************/
 /* SPR accesses */
@@ -116,3 +118,15 @@ void ppc_store_msr(CPUPPCState *env, target_ulong
 value) {

target-ppc/misc_helper.c void ppc_store_msr(CPUPPCState *env, target_ulong · line 128

Quoted 10 lines
target-ppc/misc_helper.c +8 -0
   hreg_store_msr(env, value, 0);
 }
+
+bool virtio_get_byteswap(void)
+{
+    PowerPCCPU *cp = POWERPC_CPU(first_cpu);
+    CPUPPCState *env = &cp->env;
+
+    if (kvm_enabled()) { 
+        kvm_arch_get_registers(first_cpu);

This function is not defined when CONFIG_KVM is disabled.

Quoted 6 lines

Please do

   cpu_synchronize_state(first_cpu);

instead.

Oups... I'll fix that.

Quoted 5 lines

Also can't virtio_get_byteswap pass in the CPU pointer of the CPU that's calling in this moment? I'm not sure how racy it is to synchronize the first cpu while we're not in the first cpu's execution thread.

I kept the choices made by Rusty in the original serie. According to these, I am not sure if we can use current_cpu:

https://lists.gnu.org/archive/html/qemu-devel/2013-08/msg01156.html
https://lists.gnu.org/archive/html/qemu-devel/2013-08/msg01504.html

Quoted 5 lines

Either way, if we do this "right" we don't even have to jump through these hoops, as little endian setting simply happens steered from QEMU, so QEMU will have all knowledge about the guest's little endian mode without the need to synchronize any state.

Quoted 9 lines
Quoted 2 lines

Sure. This is just primary work to get virtio working for cross-endian cases. :)

Yeah, but so far LE switching code is not upstream in KVM - and for TCG we don't need the cpu_synchronize_state() call at all.

Alex

Sure, but for TCG, cpu_synchronize_state() does nothing since KVM is not enabled... what would be wrong with that ?

Cheers.

--
Greg

#5 lore raw
Quoted 113 lines

Am 10.12.2013 um 18:37 schrieb Greg Kurz <[email protected]>:

Quoted 107 lines · On Mon, 9 Dec 2013 18:18:26 +0100 Alexander Graf <[email protected]> wrote:
Quoted 106 lines · Greg Kurz
Quoted 75 lines · On Mon, 9 Dec 2013 16:33:59 +0100 Alexander Graf <[email protected]> wrote:
Quoted 62 lines · Greg Kurz

We base it on the OS endian, as reflected by the endianness of the interrupt vectors (handled through the ILE bit in the LPCR register).

This patch does two things:
- make LPCR a KVM register
- implement virtio_get_byteswap() over LPCR

This patch requires to have the following defined in the linux headers:

$ grep LPCR linux-headers/asm-powerpc/kvm.h
#define KVM_REG_PPC_LPCR        (KVM_REG_PPC | KVM_REG_SIZE_U32 |
0xb5)

---
target-ppc/kvm.c | 4 ++++
target-ppc/misc_helper.c | 14 ++++++++++++++

2 files changed, 18 insertions(+)
target-ppc/kvm.c +0 -0
index 10d0cd9..b450a22 100644
@@ -869,6 +869,8 @@ int kvm_arch_put_registers(CPUState *cs, int

level) DPRINTF("Warning: Unable to set VPA information to KVM\n"); }

      }
target-ppc/kvm.c +2 -0
+
+        kvm_put_one_spr(cs, KVM_REG_PPC_LPCR, SPR_LPCR);

#endif /* TARGET_PPC64 */
}

target-ppc/kvm.c +2 -0
@@ -1091,6 +1093,8 @@ int kvm_arch_get_registers(CPUState *cs)
              DPRINTF("Warning: Unable to get VPA information from
 KVM\n"); }
      }
+
+        kvm_get_one_spr(cs, KVM_REG_PPC_LPCR, SPR_LPCR);
 #endif
  }
 
target-ppc/misc_helper.c +2 -0
index 616aab6..0e0743a 100644
@@ -20,6 +20,8 @@
 #include "helper.h"
 
 #include "helper_regs.h"
+#include "hw/virtio/virtio.h"
+#include "sysemu/kvm.h"
 
 /*****************************************************************************/
 /* SPR accesses */
@@ -116,3 +118,15 @@ void ppc_store_msr(CPUPPCState *env, target_ulong
 value) {

target-ppc/misc_helper.c void ppc_store_msr(CPUPPCState *env, target_ulong · line 128

Quoted 10 lines
target-ppc/misc_helper.c +8 -0
  hreg_store_msr(env, value, 0);
 }
+
+bool virtio_get_byteswap(void)
+{
+    PowerPCCPU *cp = POWERPC_CPU(first_cpu);
+    CPUPPCState *env = &cp->env;
+
+    if (kvm_enabled()) { 
+        kvm_arch_get_registers(first_cpu);

This function is not defined when CONFIG_KVM is disabled.

Quoted 5 lines

Please do

  cpu_synchronize_state(first_cpu);

instead.

Oups... I'll fix that.

Quoted 4 lines

Also can't virtio_get_byteswap pass in the CPU pointer of the CPU that's calling in this moment? I'm not sure how racy it is to synchronize the first cpu while we're not in the first cpu's execution thread.

I kept the choices made by Rusty in the original serie. According to these, I am not sure if we can use current_cpu:

https://lists.gnu.org/archive/html/qemu-devel/2013-08/msg01156.html
https://lists.gnu.org/archive/html/qemu-devel/2013-08/msg01504.html

Quoted 4 lines

Either way, if we do this "right" we don't even have to jump through these hoops, as little endian setting simply happens steered from QEMU, so QEMU will have all knowledge about the guest's little endian mode without the need to synchronize any state.

Sure. This is just primary work to get virtio working for cross-endian cases. :)

Quoted 8 lines
Quoted 5 lines

Yeah, but so far LE switching code is not upstream in KVM - and for TCG we don't need the cpu_synchronize_state() call at all.

Alex

Sure, but for TCG, cpu_synchronize_state() does nothing since KVM is not enabled... what would be wrong with that ?

That it could potentially break kvm.

Alex

Quoted 6 lines

Cheers.

--
Greg

author
#6 lore raw
Quoted 5 lines · On Tue, 10 Dec 2013 18:48:45 +0100 Alexander Graf <[email protected]> wrote:
Quoted 5 lines
Quoted 5 lines

Yeah, but so far LE switching code is not upstream in KVM - and for TCG we don't need the cpu_synchronize_state() call at all.

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

Alex

Sure, but for TCG, cpu_synchronize_state() does nothing since KVM is not enabled... what would be wrong with that ?

That it could potentially break kvm.

Sorry to bother again but could you elaborate ?

Quoted 2 lines

Alex

--
Greg

#7 lore raw
Quoted 9 lines · Greg Kurz
Quoted 7 lines · On Tue, 10 Dec 2013 18:48:45 +0100 Alexander Graf <[email protected]> wrote:
Quoted 7 lines
Quoted 6 lines

Yeah, but so far LE switching code is not upstream in KVM - and for TCG we don't need the cpu_synchronize_state() call at all.

Alex

Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

Sure, but for TCG, cpu_synchronize_state() does nothing since KVM is not enabled... what would be wrong with that ?

That it could potentially break kvm.

Sorry to bother again but could you elaborate ?

Hrm, looking through the code it seems like it's safe to call cpu_synchronize_state() on a foreign cpu. Sorry for the fuss.

Alex

author
#8 lore raw

PATCH v2 1/1 target-ppc: ppc64 target's virtio can be either endian.

We base it on the OS endian, as reflected by the endianness of the interrupt vectors (handled through the ILE bit in the LPCR register).

This patch does two things:
- make LPCR a KVM register
- implement virtio_get_byteswap() over LPCR

Using first_cpu to fetch the registers from KVM may look arbitrary and awkward, but it is okay because KVM sets/unsets the ILE bit on all CPUs.

Changes in v2:
- call cpu_synchronize_state() instead of kvm_arch_get_registers().

---

Alex,

If you are interested, I can resend the whole virtio+endian serie or setup a git tree for you to pull from.

--
Greg

 target-ppc/kvm.c         |    4 ++++
 target-ppc/misc_helper.c |   12 ++++++++++++
 2 files changed, 16 insertions(+)
target-ppc/kvm.c +4 -0
index 10d0cd9..b450a22 100644
@@ -869,6 +869,8 @@ int kvm_arch_put_registers(CPUState *cs, int level)
                 DPRINTF("Warning: Unable to set VPA information to KVM\n");
             }
         }
+
+        kvm_put_one_spr(cs, KVM_REG_PPC_LPCR, SPR_LPCR);
 #endif /* TARGET_PPC64 */
     }
 
@@ -1091,6 +1093,8 @@ int kvm_arch_get_registers(CPUState *cs)
                 DPRINTF("Warning: Unable to get VPA information from KVM\n");
             }
         }
+
+        kvm_get_one_spr(cs, KVM_REG_PPC_LPCR, SPR_LPCR);
 #endif
     }
 
target-ppc/misc_helper.c +12 -0
index 616aab6..e8fc8a3 100644
@@ -20,6 +20,8 @@
 #include "helper.h"
 
 #include "helper_regs.h"
+#include "hw/virtio/virtio.h"
+#include "sysemu/kvm.h"
 
 /*****************************************************************************/
 /* SPR accesses */
@@ -116,3 +118,13 @@ void ppc_store_msr(CPUPPCState *env, target_ulong value)
 {
     hreg_store_msr(env, value, 0);
 }
+
+bool virtio_get_byteswap(void)
+{
+    PowerPCCPU *cp = POWERPC_CPU(first_cpu);
+    CPUPPCState *env = &cp->env;
+
+    cpu_synchronize_state(first_cpu);
+
+    return env->spr[SPR_LPCR] & LPCR_ILE;
+}
Reviewed-by on v2 1/1
#9 lore raw
Quoted 17 lines · Greg Kurz

We base it on the OS endian, as reflected by the endianness of the interrupt vectors (handled through the ILE bit in the LPCR register).

This patch does two things:
- make LPCR a KVM register
- implement virtio_get_byteswap() over LPCR

Using first_cpu to fetch the registers from KVM may look arbitrary and awkward, but it is okay because KVM sets/unsets the ILE bit on all CPUs.

Changes in v2:
- call cpu_synchronize_state() instead of kvm_arch_get_registers().

Quoted 6 lines

---

Alex,

If you are interested, I can resend the whole virtio+endian serie or setup a git tree for you to pull from.

I think this patch should really go in after / with the virtio-endian series, since as it is it would be dead code. So I'll just give you my ack here and hope whoever gets the virtio-endianness bits in also includes that with my reviewed-by line.

Alex

Quoted 58 lines

--
Greg

target-ppc/kvm.c | 4 ++++
target-ppc/misc_helper.c | 12 ++++++++++++

2 files changed, 16 insertions(+)
target-ppc/kvm.c +4 -0
index 10d0cd9..b450a22 100644
@@ -869,6 +869,8 @@ int kvm_arch_put_registers(CPUState *cs, int level)
                DPRINTF("Warning: Unable to set VPA information to KVM\n");
            }
        }
+
+        kvm_put_one_spr(cs, KVM_REG_PPC_LPCR, SPR_LPCR);
 #endif /* TARGET_PPC64 */
    }
 
@@ -1091,6 +1093,8 @@ int kvm_arch_get_registers(CPUState *cs)
                DPRINTF("Warning: Unable to get VPA information from KVM\n");
            }
        }
+
+        kvm_get_one_spr(cs, KVM_REG_PPC_LPCR, SPR_LPCR);
 #endif
    }
 
target-ppc/misc_helper.c +12 -0
index 616aab6..e8fc8a3 100644
@@ -20,6 +20,8 @@
 #include "helper.h"
 
 #include "helper_regs.h"
+#include "hw/virtio/virtio.h"
+#include "sysemu/kvm.h"
 
 /*****************************************************************************/
 /* SPR accesses */
@@ -116,3 +118,13 @@ void ppc_store_msr(CPUPPCState *env, target_ulong value)
 {
    hreg_store_msr(env, value, 0);
 }
+
+bool virtio_get_byteswap(void)
+{
+    PowerPCCPU *cp = POWERPC_CPU(first_cpu);
+    CPUPPCState *env = &cp->env;
+
+    cpu_synchronize_state(first_cpu);
+
+    return env->spr[SPR_LPCR] & LPCR_ILE;
+}
author
#10 lore raw
Quoted 11 lines · On Wed, 11 Dec 2013 17:11:49 +0100 Alexander Graf <[email protected]> wrote:

I think this patch should really go in after / with the virtio-endian series, since as it is it would be dead code. So I'll just give you my ack here and hope whoever gets the virtio-endianness bits in also includes that with my reviewed-by line.

Alex

Thanks for your time Alex.

Anthony,

Would you have time to review the virtio-endian patchset from Rusty and I ? I have made a bundle out of the patches that were sent to qemu-devel@:

http://patchwork.ozlabs.org/bundle/groug/qemu-virtio-endian/?order=date

The last patch (ppc64 target's virtio can be either endian) is the enablement for ppc64: it supports both TCG and KVM.

Thanks.

--
Greg

author
#11 lore raw

PATCH 1/1 target-ppc: ppc64 target's virtio can be either endian. (v3)

In reply to Greg Kurz

We base it on the OS endian, as reflected by the endianness of the interrupt vectors (handled through the ILE bit in the LPCR register).

This patch implements virtio_get_byteswap() over LPCR.

Using first_cpu to fetch the registers from KVM may look arbitrary and awkward, but it is okay because KVM sets/unsets the ILE bit on all CPUs.

Changes in v3:
- dropped the explicit calls to kvm_[get|put]_one_reg as LPCR is now properly handled by the generic SPR code.
Changes in v2:
- call cpu_synchronize_state() instead of kvm_arch_get_registers().

---
 target-ppc/misc_helper.c |   12 ++++++++++++
 1 file changed, 12 insertions(+)
target-ppc/misc_helper.c +12 -0
index 616aab6..e8fc8a3 100644
@@ -20,6 +20,8 @@
 #include "helper.h"
 
 #include "helper_regs.h"
+#include "hw/virtio/virtio.h"
+#include "sysemu/kvm.h"
 
 /*****************************************************************************/
 /* SPR accesses */
@@ -116,3 +118,13 @@ void ppc_store_msr(CPUPPCState *env, target_ulong value)
 {
     hreg_store_msr(env, value, 0);
 }
+
+bool virtio_get_byteswap(void)
+{
+    PowerPCCPU *cp = POWERPC_CPU(first_cpu);
+    CPUPPCState *env = &cp->env;
+
+    cpu_synchronize_state(first_cpu);
+
+    return env->spr[SPR_LPCR] & LPCR_ILE;
+}
#12 lore raw

Re: [Qemu-devel] [PATCH] target-ppc: ppc64 target's virtio can be either endian. (v3)

Quoted 37 lines · Greg Kurz

We base it on the OS endian, as reflected by the endianness of the interrupt vectors (handled through the ILE bit in the LPCR register).

This patch implements virtio_get_byteswap() over LPCR.

Using first_cpu to fetch the registers from KVM may look arbitrary and awkward, but it is okay because KVM sets/unsets the ILE bit on all CPUs.

Changes in v3:
- dropped the explicit calls to kvm_[get|put]_one_reg as LPCR is now properly handled by the generic SPR code.
Changes in v2:
- call cpu_synchronize_state() instead of kvm_arch_get_registers().

---
target-ppc/misc_helper.c | 12 ++++++++++++

1 file changed, 12 insertions(+)
target-ppc/misc_helper.c +2 -0
index 616aab6..e8fc8a3 100644
@@ -20,6 +20,8 @@
 #include "helper.h"
 
 #include "helper_regs.h"
+#include "hw/virtio/virtio.h"
+#include "sysemu/kvm.h"
 
 /*****************************************************************************/
 /* SPR accesses */

target-ppc/misc_helper.c void ppc_store_msr(CPUPPCState *env, target_ulong value) · line 127

Quoted 10 lines
target-ppc/misc_helper.c +7 -0
@@ -116,3 +118,13 @@ void ppc_store_msr(CPUPPCState *env, target_ulong value)
 {
    hreg_store_msr(env, value, 0);
 }
+
+bool virtio_get_byteswap(void)
+{
+    PowerPCCPU *cp = POWERPC_CPU(first_cpu);
+    CPUPPCState *env = &cp->env;
+
+    cpu_synchronize_state(first_cpu);

LPCR_ILE only gets modified by QEMU, so you can even get rid of the synchronize call :).

Either way, this can only go in after the generic virtio byteswap mechanics are merged.

Alex

Quoted 4 lines
target-ppc/misc_helper.c +3 -0
+
+    return env->spr[SPR_LPCR] & LPCR_ILE;
+}
#13 lore raw
In reply to Alexander Graf
Quoted 5 lines · Alexander Graf

Also can't virtio_get_byteswap pass in the CPU pointer of the CPU that's calling in this moment? I'm not sure how racy it is to synchronize the first cpu while we're not in the first cpu's execution thread.

The LPCR bit is always set on all CPUs.

Quoted 4 lines

Either way, if we do this "right" we don't even have to jump through these hoops, as little endian setting simply happens steered from QEMU, so QEMU will have all knowledge about the guest's little endian mode without the need to synchronize any state.

Ben.

#14 lore raw
Quoted 8 lines · Benjamin Herrenschmidt
Quoted 5 lines · Alexander Graf

Also can't virtio_get_byteswap pass in the CPU pointer of the CPU that's calling in this moment? I'm not sure how racy it is to synchronize the first cpu while we're not in the first cpu's execution thread.

The LPCR bit is always set on all CPUs.

Yes and no. In HV KVM, yes. With sPAPR, yes. If we want to emulate a host machine, LPCR is a per-core register and may be different.

But that's not the point. The point is that I don't think it's good practice to call cpu_synchronize_state() of a foreign cpu, since it gives me headaches to imagine in what twisted race conditions things could go wrong there.

What happens when the first cpu is executing code while we call the synchronize call? It should lock in vcpu_load(), but that means we're not stuck. So we may have a secondary cpu that waits for the first cpu to exit. But why would it exit at all? It doesn't have to. Maybe it waits inside of the guest to see the virtio probing complete first.

As I said, I don't even want to start imagining what can go wrong.

So instead, I'd prefer if we model it sPAPR'ish: Treat VM-wide hypercalls as such. Handle it in QEMU, save the "LE" state in some machine state and query it from there. Then we're all safe.

Alex