Skip to contentSkip to conversation

Add virtio endianness determination in KVM ARM/ARM64

PATCH In review Pranavkumar Sawargaonkar 2 participants 6 messages Feb 5, 2015 – Feb 6, 2015

lore
Share & tools
lore
#0 lore raw

v3 cover Add virtio endianness determination in KVM ARM/ARM64

In KVM ARM/ARM64 one can run cross-endian guset with respect to host kernel. For virtio to work with this we need to make virtio code aware of guest's endianness.
This patchset adds the guest endianness determination for KVM ARM/ARM64 and related small fix in KVM ARM64.

V2->V3:
- Renamed arm_cpu_virtio_endianness() to arm_cpu_is_big_endian()
- Added check for current exception level so that endianness for el0 is determined by looking at sctlr EOE and for others from sctlr EE bit.

V1->V2:
- Change the patch to be used generically for both ARM and ARM64.
- Define TARGET_IS_BIENDIAN for target-arm
- Address the review comments received in v1.
- Rebased code on top of latest qemu code.

V1:
- First patch in this series.
- http://lists.gnu.org/archive/html/qemu-devel/2014-10/msg03321.html

Pranavkumar Sawargaonkar (2):
target-arm: KVM64: Get and Sync up guest register state like kvm32.
target-arm: Guest cpu endianness determination for virtio KVM

    ARM/ARM64
 target-arm/cpu.c   | 24 ++++++++++++++++++++++++
 target-arm/cpu.h   |  2 ++
 target-arm/kvm64.c | 13 ++++++++++++-
 3 files changed, 38 insertions(+), 1 deletion(-)
Signature · 1 line
1.9.1
#1 lore raw

PATCH v3 1/2 target-arm: KVM64: Get and Sync up guest register state like kvm32.

This patch adds:
1. Call write_kvmstate_to_list() and write_list_to_cpustate() in kvm_arch_get_registers() to sync guest register state.
2. Call write_list_to_kvmstate() in kvm_arch_put_registers() to sync guest register state.

These changes are already there for kvm32 in target-arm/kvm32.c.

---
 target-arm/kvm64.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
target-arm/kvm64.c +12 -1
index ba16821..033babf 100644
@@ -193,9 +193,12 @@ int kvm_arch_put_registers(CPUState *cs, int level)
         }
     }
 
+    if (!write_list_to_kvmstate(cpu)) {
+        return EINVAL;
+    }
+
     /* TODO:
      * FP state
-     * system registers
      */
     return ret;
 }
@@ -269,6 +272,14 @@ int kvm_arch_get_registers(CPUState *cs)
         }
     }
 
+    if (!write_kvmstate_to_list(cpu)) {
+        return EINVAL;
+    }
+    /* Note that it's OK to have registers which aren't in CPUState,
+     * so we can ignore a failure return here.
+     */
+    write_list_to_cpustate(cpu);
+
     /* TODO: other registers */
     return ret;
 }
Signature · 1 line
1.9.1
#2 lore raw

PATCH v3 2/2 target-arm: Guest cpu endianness determination for virtio KVM ARM/ARM64

In reply to Pranavkumar Sawargaonkar

This patch implements a fucntion pointer "virtio_is_big_endian" from "CPUClass" structure for arm/arm64.
Function arm_cpu_is_big_endian() is added to determine and return the guest cpu endianness to virtio.
This is required for running cross endian guests with virtio on ARM/ARM64.

---
 target-arm/cpu.c | 24 ++++++++++++++++++++++++
 target-arm/cpu.h |  2 ++
 2 files changed, 26 insertions(+)
target-arm/cpu.c +24 -0
index 285947f..4d9cded 100644
@@ -320,6 +320,29 @@ static void arm_cpu_kvm_set_irq(void *opaque, int irq, int level)
     kvm_set_irq(kvm_state, kvm_irq, level ? 1 : 0);
 #endif
 }
+
+static bool arm_cpu_is_big_endian(CPUState *cs)
+{
+    ARMCPU *cpu = ARM_CPU(cs);
+    CPUARMState *env = &cpu->env;
+    int cur_el;
+
+    cpu_synchronize_state(cs);
+
+    /* In 32bit guest endianess is determined by looking at CPSR's E bit */
+    if (!is_a64(env)) {
+        return (env->pstate & CPSR_E) ? 1 : 0;
+    }
+
+    cur_el = arm_current_el(env);
+
+    if (cur_el == 0) {
+        return (env->cp15.sctlr_el[1] & SCTLR_E0E) != 0;
+    }
+
+    return (env->cp15.sctlr_el[cur_el] & SCTLR_EE) != 0;
+}
+
 #endif
 
 static inline void set_feature(CPUARMState *env, int feature)
@@ -1189,6 +1212,7 @@ static void arm_cpu_class_init(ObjectClass *oc, void *data)
     cc->do_interrupt = arm_cpu_do_interrupt;
     cc->get_phys_page_debug = arm_cpu_get_phys_page_debug;
     cc->vmsd = &vmstate_arm_cpu;
+    cc->virtio_is_big_endian = arm_cpu_is_big_endian;
 #endif
     cc->gdb_num_core_regs = 26;
     cc->gdb_core_xml_file = "arm-core.xml";
target-arm/cpu.h +2 -0
index cd7a9e8..317e801 100644
@@ -32,6 +32,8 @@
 #  define ELF_MACHINE EM_ARM
 #endif
 
+#define TARGET_IS_BIENDIAN 1
+
 #define CPUArchState struct CPUARMState
 
 #include "qemu-common.h"
Signature · 1 line
1.9.1
#3 lore raw

Re: [Qemu-devel] [PATCH V3 2/2] target-arm: Guest cpu endianness determination for virtio KVM ARM/ARM64

Quoted 22 lines · Pranavkumar Sawargaonkar

This patch implements a fucntion pointer "virtio_is_big_endian" from "CPUClass" structure for arm/arm64.
Function arm_cpu_is_big_endian() is added to determine and return the guest cpu endianness to virtio.
This is required for running cross endian guests with virtio on ARM/ARM64.

---
 target-arm/cpu.c | 24 ++++++++++++++++++++++++
 target-arm/cpu.h |  2 ++
 2 files changed, 26 insertions(+)
target-arm/cpu.c +2 -0
index 285947f..4d9cded 100644
@@ -320,6 +320,29 @@ static void arm_cpu_kvm_set_irq(void *opaque, int irq, int level)
     kvm_set_irq(kvm_state, kvm_irq, level ? 1 : 0);
 #endif
 }
+
+static bool arm_cpu_is_big_endian(CPUState *cs)

target-arm/cpu.c static void arm_cpu_kvm_set_irq(void *opaque, int irq, int level) · line 334

Quoted 10 lines
target-arm/cpu.c +10 -0
+{
+    ARMCPU *cpu = ARM_CPU(cs);
+    CPUARMState *env = &cpu->env;
+    int cur_el;
+
+    cpu_synchronize_state(cs);
+
+    /* In 32bit guest endianess is determined by looking at CPSR's E bit */
+    if (!is_a64(env)) {
+        return (env->pstate & CPSR_E) ? 1 : 0;

This is wrong, because if we're not 32-bit then the CPSR isn't in env->pstate but in env->cpsr_uncached. (I'm guessing you didn't test 32-bit guests.)

Since this is the only error in this patch, I'll just fix it as I put the series into target-arm.next rather than forcing you to respin it.

thanks
-- PMM

#4 lore raw

Re: [Qemu-devel] [PATCH V3 2/2] target-arm: Guest cpu endianness determination for virtio KVM ARM/ARM64

Quoted 10 lines · Peter Maydell

target-arm/cpu.c static void arm_cpu_kvm_set_irq(void *opaque, int irq, int level) · line 334

Quoted 4 lines · Pranavkumar Sawargaonkar
target-arm/cpu.c +4 -0
+
+    /* In 32bit guest endianess is determined by looking at CPSR's E bit */
+    if (!is_a64(env)) {
+        return (env->pstate & CPSR_E) ? 1 : 0;

This is wrong, because if we're not 32-bit then the CPSR isn't in env->pstate but in env->cpsr_uncached. (I'm guessing you didn't test 32-bit guests.)

Actually thinking about it your code would have worked for the common 32-bit guest case, since if we fall through to looking at SCTLR then (assuming the guest is at EL1 which it will be when it's messing with the virtio device) we'll end up checking the 32-bit SCTLR EE bit, which will be the same as the current guest endianness for any sane guest kernel. So I apologise for suggesting you didn't test that case.

-- PMM

#5 lore raw

Re: [Qemu-devel] [PATCH V3 2/2] target-arm: Guest cpu endianness determination for virtio KVM ARM/ARM64

Hi PMM,

Quoted 12 lines · Peter Maydell
Quoted 10 lines · Peter Maydell

target-arm/cpu.c static void arm_cpu_kvm_set_irq(void *opaque, int irq, int level) · line 334

Quoted 4 lines · Pranavkumar Sawargaonkar
target-arm/cpu.c +4 -0
+
+    /* In 32bit guest endianess is determined by looking at CPSR's E bit */
+    if (!is_a64(env)) {
+        return (env->pstate & CPSR_E) ? 1 : 0;

This is wrong, because if we're not 32-bit then the CPSR isn't in env->pstate but in env->cpsr_uncached. (I'm guessing you didn't test 32-bit guests.)

Quoted 7 lines

Actually thinking about it your code would have worked for the common 32-bit guest case, since if we fall through to looking at SCTLR then (assuming the guest is at EL1 which it will be when it's messing with the virtio device) we'll end up checking the 32-bit SCTLR EE bit, which will be the same as the current guest endianness for any sane guest kernel. So I apologise for suggesting you didn't test that case.

Actually I have not not tested patch with 32bit guests. Thanks for pulling the patches and 32bit guest case fix.

Thanks,
Pranav

Quoted 2 lines

-- PMM