Skip to contentSkip to conversation

target-arm: ARM64: Adding EL1 AARCH32 guest support

PATCH Superseded Greg Bellows 3 participants 17 messages Jan 27, 2015 – Feb 5, 2015

v3 of 8 Superseded Latest is v8 →

lore
Share & tools
lore
#0 lore raw

v3 cover target-arm: ARM64: Adding EL1 AARCH32 guest support

Added support for running an AArch32 guest on a AArch64 KVM host. Support has only been added to the QEMU machvirt machine. The addition of CPU properties specifiable from the command line were added to allow disablement of AArch64 execution state hereby forcing EL1 to be AArch32. The new CPU command line property is "aarch64=on/off" that is specified as follows:

    aarch64-softmmu/qemu-system-aarch64 -M virt -cpu cortex-a57,aarch64=off ...

---

v1 -> v2
- Replaced custom property parsing with use of generic CPU property parser
- Added CPU property registration
- Fixed mulitple property handling in virt.c
- Removed unnecessary kernel load changes

v2 -> v3
- Fix KVM64/AArch64 hang by conditionalizing register sync
- Conditionalize 64-bit interrupt handler setting of aarch64

Greg Bellows (4):
target-arm: Add CPU property to disable AArch64
target-arm: Add feature parsing to virt
target-arm: Add 32/64-bit register sync
target-arm: Add AArch32 guest support to KVM64

 hw/arm/virt.c           | 20 +++++++++--
 target-arm/cpu.c        |  6 +++-
 target-arm/cpu64.c      | 29 ++++++++++++++++
 target-arm/helper-a64.c |  9 +++--
 target-arm/internals.h  | 89 +++++++++++++++++++++++++++++++++++++++++++++++++
 target-arm/kvm64.c      | 33 +++++++++++++++---
 target-arm/op_helper.c  |  6 ++--
 7 files changed, 175 insertions(+), 17 deletions(-)
Signature · 1 line
1.8.3.2
#1 lore raw

PATCH v3 1/4 target-arm: Add CPU property to disable AArch64

Adds registration and get/set functions for enabling/disabling the AArch64 execution state on AArch64 CPUs. By default AArch64 execution state is enabled on AArch64 CPUs, setting the property to off, will disable the execution state. The below QEMU invocation would have AArch64 execution state disabled.

    $ ./qemu-system-aarch64 -machine virt -cpu cortex-a57,aarch64=off

Also adds stripping of features from CPU model string in acquiring the ARM CPU by name.

---

v1 -> v2
- Scrap the custom CPU feature parsing in favor of using the default CPU parsing.
- Add registration of CPU AArch64 property to disable/enable the AArch64 feature.

---
 target-arm/cpu.c   |  6 +++++-
 target-arm/cpu64.c | 29 +++++++++++++++++++++++++++++
 2 files changed, 34 insertions(+), 1 deletion(-)
target-arm/cpu.c +5 -1
index 285947f..29ed691 100644
@@ -514,13 +514,17 @@ static ObjectClass *arm_cpu_class_by_name(const char *cpu_model)
 {
     ObjectClass *oc;
     char *typename;
+    char *cpuname;
 
     if (!cpu_model) {
         return NULL;
     }
 
-    typename = g_strdup_printf("%s-" TYPE_ARM_CPU, cpu_model);
+    cpuname = g_strdup(cpu_model);
+    cpuname = strtok(cpuname, ",");
+    typename = g_strdup_printf("%s-" TYPE_ARM_CPU, cpuname);
     oc = object_class_by_name(typename);
+    g_free(cpuname);
     g_free(typename);
     if (!oc || !object_class_dynamic_cast(oc, TYPE_ARM_CPU) ||
         object_class_is_abstract(oc)) {
target-arm/cpu64.c +29 -0
index bb778b3..5a59280 100644
@@ -32,6 +32,11 @@ static inline void set_feature(CPUARMState *env, int feature)
     env->features |= 1ULL << feature;
 }
 
+static inline void unset_feature(CPUARMState *env, int feature)
+{
+    env->features &= ~(1ULL << feature);
+}
+
 #ifndef CONFIG_USER_ONLY
 static uint64_t a57_l2ctlr_read(CPUARMState *env, const ARMCPRegInfo *ri)
 {
@@ -170,8 +175,32 @@ static const ARMCPUInfo aarch64_cpus[] = {
     { .name = NULL }
 };
 
+static bool aarch64_cpu_get_aarch64(Object *obj, Error **errp)
+{
+    ARMCPU *cpu = ARM_CPU(obj);
+
+    return arm_feature(&cpu->env, ARM_FEATURE_AARCH64);
+}
+
+static void aarch64_cpu_set_aarch64(Object *obj, bool value, Error **errp)
+{
+    ARMCPU *cpu = ARM_CPU(obj);
+
+    if (value == false) {
+        unset_feature(&cpu->env, ARM_FEATURE_AARCH64);
+    } else {
+        set_feature(&cpu->env, ARM_FEATURE_AARCH64);
+    }
+}
+
 static void aarch64_cpu_initfn(Object *obj)
 {
+    object_property_add_bool(obj, "aarch64", aarch64_cpu_get_aarch64,
+                             aarch64_cpu_set_aarch64, NULL);
+    object_property_set_description(obj, "aarch64",
+                                    "Set on/off to enable/disable aarch64 "
+                                    "execution state ",
+                                    NULL);
 }
 
 static void aarch64_cpu_finalizefn(Object *obj)
Signature · 1 line
1.8.3.2
#2 lore raw

Re: [Qemu-devel] [PATCH v3 1/4] target-arm: Add CPU property to disable AArch64

Quoted 31 lines · Greg Bellows

Adds registration and get/set functions for enabling/disabling the AArch64 execution state on AArch64 CPUs. By default AArch64 execution state is enabled on AArch64 CPUs, setting the property to off, will disable the execution state. The below QEMU invocation would have AArch64 execution state disabled.

    $ ./qemu-system-aarch64 -machine virt -cpu cortex-a57,aarch64=off

Also adds stripping of features from CPU model string in acquiring the ARM CPU by name.

---

v1 -> v2
- Scrap the custom CPU feature parsing in favor of using the default CPU parsing.
- Add registration of CPU AArch64 property to disable/enable the AArch64 feature.

---
 target-arm/cpu.c   |  6 +++++-
 target-arm/cpu64.c | 29 +++++++++++++++++++++++++++++
 2 files changed, 34 insertions(+), 1 deletion(-)
target-arm/cpu.c +0 -0
index 285947f..29ed691 100644
@@ -514,13 +514,17 @@ static ObjectClass *arm_cpu_class_by_name(const char *cpu_model)
 {
     ObjectClass *oc;

target-arm/cpu.c static ObjectClass *arm_cpu_class_by_name(const char *cpu_model) · line 524

Quoted 10 lines
target-arm/cpu.c +3 -1
     char *typename;
+    char *cpuname;
 
     if (!cpu_model) {
         return NULL;
     }
 
-    typename = g_strdup_printf("%s-" TYPE_ARM_CPU, cpu_model);
+    cpuname = g_strdup(cpu_model);
+    cpuname = strtok(cpuname, ",");

strtok isn't thread safe. Let's just use g_strsplit like we did in virt.c and then we don't have to worry about whether or not multiple threads could ever end up here at the same time.

Quoted 43 lines
target-arm/cpu.c +2 -0
+    typename = g_strdup_printf("%s-" TYPE_ARM_CPU, cpuname);
     oc = object_class_by_name(typename);
+    g_free(cpuname);
     g_free(typename);
     if (!oc || !object_class_dynamic_cast(oc, TYPE_ARM_CPU) ||
         object_class_is_abstract(oc)) {
target-arm/cpu64.c +22 -0
index bb778b3..5a59280 100644
@@ -32,6 +32,11 @@ static inline void set_feature(CPUARMState *env, int feature)
     env->features |= 1ULL << feature;
 }
 
+static inline void unset_feature(CPUARMState *env, int feature)
+{
+    env->features &= ~(1ULL << feature);
+}
+
 #ifndef CONFIG_USER_ONLY
 static uint64_t a57_l2ctlr_read(CPUARMState *env, const ARMCPRegInfo *ri)
 {
@@ -170,8 +175,32 @@ static const ARMCPUInfo aarch64_cpus[] = {
     { .name = NULL }
 };
 
+static bool aarch64_cpu_get_aarch64(Object *obj, Error **errp)
+{
+    ARMCPU *cpu = ARM_CPU(obj);
+
+    return arm_feature(&cpu->env, ARM_FEATURE_AARCH64);
+}
+
+static void aarch64_cpu_set_aarch64(Object *obj, bool value, Error **errp)
+{
+    ARMCPU *cpu = ARM_CPU(obj);
+
+    if (value == false) {
+        unset_feature(&cpu->env, ARM_FEATURE_AARCH64);
+    } else {
+        set_feature(&cpu->env, ARM_FEATURE_AARCH64);
+    }
+}

target-arm/cpu64.c static const ARMCPUInfo aarch64_cpus[] = { · line 204

Quoted 10 lines
target-arm/cpu64.c +7 -0
+
 static void aarch64_cpu_initfn(Object *obj)
 {
+    object_property_add_bool(obj, "aarch64", aarch64_cpu_get_aarch64,
+                             aarch64_cpu_set_aarch64, NULL);
+    object_property_set_description(obj, "aarch64",
+                                    "Set on/off to enable/disable aarch64 "
+                                    "execution state ",
+                                    NULL);
 }

This all looks OK code-wise. Still need to think about whether we can manage to end up with a nicer interface to the user than cpuname,-aarch64, though.

-- PMM

#3 lore raw

Re: [Qemu-devel] [PATCH v3 1/4] target-arm: Add CPU property to disable AArch64

Quoted 11 lines · Peter Maydell

target-arm/cpu64.c static const ARMCPUInfo aarch64_cpus[] = { · line 204

Quoted 9 lines · Greg Bellows

static void aarch64_cpu_initfn(Object *obj)
{

target-arm/cpu64.c +6 -0
+    object_property_add_bool(obj, "aarch64", aarch64_cpu_get_aarch64,
+                             aarch64_cpu_set_aarch64, NULL);
+    object_property_set_description(obj, "aarch64",
+                                    "Set on/off to enable/disable aarch64 "
+                                    "execution state ",
+                                    NULL);
 }
Quoted 3 lines

This all looks OK code-wise. Still need to think about whether we can manage to end up with a nicer interface to the user than cpuname,-aarch64, though.

[I meant "-cpu cpuname,aarch64=off", hadn't processed that we've updated to the new style syntax.]

I had a think about this as I was cycling home, and I (re)convinced myself that modelling this as "remove the AArch64 feature flag" is the right way to do it, which then just leaves us with "what's the best user-facing UI we can manage to expose that?".

Suppose that we had two properties/feature flag switches that do the same thing but have inverse sense:

aarch64=off
aarch32-only=on

Would that help, or just be more confusing?

-- PMM

#4 lore raw

Re: [Qemu-devel] [PATCH v3 1/4] target-arm: Add CPU property to disable AArch64

Quoted 24 lines · Peter Maydell
Quoted 11 lines · Peter Maydell

target-arm/cpu64.c static const ARMCPUInfo aarch64_cpus[] = { · line 204

Quoted 9 lines · Greg Bellows

static void aarch64_cpu_initfn(Object *obj)
{

target-arm/cpu64.c +6 -0
+    object_property_add_bool(obj, "aarch64", aarch64_cpu_get_aarch64,
+                             aarch64_cpu_set_aarch64, NULL);
+    object_property_set_description(obj, "aarch64",
+                                    "Set on/off to enable/disable aarch64 "
+                                    "execution state ",
+                                    NULL);
 }
Quoted 3 lines

This all looks OK code-wise. Still need to think about whether we can manage to end up with a nicer interface to the user than cpuname,-aarch64, though.

[I meant "-cpu cpuname,aarch64=off", hadn't processed that we've updated to the new style syntax.]

I had a think about this as I was cycling home, and I (re)convinced myself that modelling this as "remove the AArch64 feature flag" is the right way to do it, which then just leaves us with "what's the best user-facing UI we can manage to expose that?".

Quoted 8 lines

Suppose that we had two properties/feature flag switches that do the same thing but have inverse sense:

aarch64=off
aarch32-only=on

Would that help, or just be more confusing?

that would help, imho.

-Christoffer

#5 lore raw

Re: [Qemu-devel] [PATCH v3 1/4] target-arm: Add CPU property to disable AArch64

Quoted 32 lines · Christoffer Dall
Quoted 1 line · Peter Maydell

On 3 February 2015 at 19:14, Peter Maydell <[email protected]>

Quoted 1 line · wrote:
Quoted 1 line

On 27 January 2015 at 23:58, Greg Bellows <[email protected]>

Quoted 6 lines · wrote:
Quoted 6 lines

target-arm/cpu64.c static const ARMCPUInfo aarch64_cpus[] = {

Quoted 6 lines

static void aarch64_cpu_initfn(Object *obj)
{

target-arm/cpu64.c +4 -0
+    object_property_add_bool(obj, "aarch64", aarch64_cpu_get_aarch64,
+                             aarch64_cpu_set_aarch64, NULL);
+    object_property_set_description(obj, "aarch64",
+                                    "Set on/off to enable/disable

aarch64 "

Quoted 19 lines
Quoted 7 lines

target-arm/cpu64.c static const ARMCPUInfo aarch64_cpus[] = { · line 204

Quoted 3 lines
target-arm/cpu64.c +2 -0
+                                    "execution state ",
+                                    NULL);
 }

This all looks OK code-wise. Still need to think about whether we can manage to end up with a nicer interface to the user than cpuname,-aarch64, though.

[I meant "-cpu cpuname,aarch64=off", hadn't processed that we've updated to the new style syntax.]

I had a think about this as I was cycling home, and I (re)convinced myself that modelling this as "remove the AArch64 feature flag" is the right way to do it, which then just leaves us with "what's the best user-facing UI we can manage to expose that?".

Suppose that we had two properties/feature flag switches that do the same thing but have inverse sense:

Quoted 8 lines
Quoted 5 lines

aarch64=off
aarch32-only=on

Would that help, or just be more confusing?

that would help, imho.

​Hmmm..., while it is trivial to add the second option I think it would be confusing for users and begs for issues of having both specified. If we prefer the second name lets go with it otherwise my vote is against having two. If the behavior of the flag is "remove the AArch64 flag" it seems most natural to stick with what we have as it conveys just that. The other option seems counter-intuitive.

Quoted 2 lines

-Christoffer

#6 lore raw

Re: [Qemu-devel] [PATCH v3 1/4] target-arm: Add CPU property to disable AArch64

In reply to Peter Maydell
Quoted 44 lines · Peter Maydell
Quoted 1 line · Greg Bellows

Adds registration and get/set functions for enabling/disabling the

AArch64

Quoted 1 line

execution state on AArch64 CPUs. By default AArch64 execution state is

enabled

Quoted 1 line

on AArch64 CPUs, setting the property to off, will disable the execution

state.

Quoted 5 lines

The below QEMU invocation would have AArch64 execution state disabled.

    $ ./qemu-system-aarch64 -machine virt -cpu cortex-a57,aarch64=off

Also adds stripping of features from CPU model string in acquiring the

ARM CPU

target-arm/cpu.c static ObjectClass *arm_cpu_class_by_name(const

Quoted 21 lines

by name.

---

v1 -> v2
- Scrap the custom CPU feature parsing in favor of using the default CPU parsing.
- Add registration of CPU AArch64 property to disable/enable the AArch64 feature.

---
 target-arm/cpu.c   |  6 +++++-
 target-arm/cpu64.c | 29 +++++++++++++++++++++++++++++
 2 files changed, 34 insertions(+), 1 deletion(-)
target-arm/cpu.c +0 -0
index 285947f..29ed691 100644
@@ -514,13 +514,17 @@ static ObjectClass *arm_cpu_class_by_name(const

char *cpu_model)

Quoted 9 lines
target-arm/cpu.c +1 -0
 {
     ObjectClass *oc;
     char *typename;
+    char *cpuname;
 
     if (!cpu_model) {
         return NULL;
     }
 
Quoted 9 lines

target-arm/cpu.c static ObjectClass *arm_cpu_class_by_name(const char *cpu_model) · line 524

Quoted 3 lines
target-arm/cpu.c +2 -1
-    typename = g_strdup_printf("%s-" TYPE_ARM_CPU, cpu_model);
+    cpuname = g_strdup(cpu_model);
+    cpuname = strtok(cpuname, ",");

strtok isn't thread safe. Let's just use g_strsplit like we did in virt.c and then we don't have to worry about whether or not multiple threads could ever end up here at the same time.

​Sure, I'll change it.​

Quoted 64 lines

target-arm/cpu64.c static inline void set_feature(CPUARMState *env, int

Quoted 11 lines
target-arm/cpu.c +2 -0
+    typename = g_strdup_printf("%s-" TYPE_ARM_CPU, cpuname);
     oc = object_class_by_name(typename);
+    g_free(cpuname);
     g_free(typename);
     if (!oc || !object_class_dynamic_cast(oc, TYPE_ARM_CPU) ||
         object_class_is_abstract(oc)) {
target-arm/cpu64.c +0 -0
index bb778b3..5a59280 100644
@@ -32,6 +32,11 @@ static inline void set_feature(CPUARMState *env, int

feature)

target-arm/cpu64.c static inline void set_feature(CPUARMState *env, int feature)

Quoted 10 lines
target-arm/cpu64.c +5 -0
     env->features |= 1ULL << feature;
 }
 
+static inline void unset_feature(CPUARMState *env, int feature)
+{
+    env->features &= ~(1ULL << feature);
+}
+
 #ifndef CONFIG_USER_ONLY
 static uint64_t a57_l2ctlr_read(CPUARMState *env, const ARMCPRegInfo

*ri)

target-arm/cpu64.c static const ARMCPUInfo aarch64_cpus[] = { · line 185

Quoted 13 lines
target-arm/cpu64.c +8 -0
 {
@@ -170,8 +175,32 @@ static const ARMCPUInfo aarch64_cpus[] = {
     { .name = NULL }
 };
 
+static bool aarch64_cpu_get_aarch64(Object *obj, Error **errp)
+{
+    ARMCPU *cpu = ARM_CPU(obj);
+
+    return arm_feature(&cpu->env, ARM_FEATURE_AARCH64);
+}
+
+static void aarch64_cpu_set_aarch64(Object *obj, bool value, Error

**errp)

Quoted 6 lines
target-arm/cpu64.c +6 -0
+{
+    ARMCPU *cpu = ARM_CPU(obj);
+
+    if (value == false) {
+        unset_feature(&cpu->env, ARM_FEATURE_AARCH64);
+    } else {

target-arm/cpu64.c static const ARMCPUInfo aarch64_cpus[] = {

Quoted 10 lines
target-arm/cpu64.c +8 -0
+        set_feature(&cpu->env, ARM_FEATURE_AARCH64);
+    }
+}
+
 static void aarch64_cpu_initfn(Object *obj)
 {
+    object_property_add_bool(obj, "aarch64", aarch64_cpu_get_aarch64,
+                             aarch64_cpu_set_aarch64, NULL);
+    object_property_set_description(obj, "aarch64",
+                                    "Set on/off to enable/disable

aarch64 "

target-arm/cpu64.c static const ARMCPUInfo aarch64_cpus[] = { · line 204

Quoted 3 lines
target-arm/cpu64.c +2 -0
+                                    "execution state ",
+                                    NULL);
 }

This all looks OK code-wise. Still need to think about whether we can manage to end up with a nicer interface to the user than cpuname,-aarch64, though.

-- PMM

#7 lore raw

PATCH v3 2/4 target-arm: Add feature parsing to virt

In reply to Greg Bellows

Added machvirt parsing of feature keywords added to the -cpu command line option. Parsing occurs during machine initialization.

---

v1 -> v2
- Fix multiple property handling

---
 hw/arm/virt.c | 20 +++++++++++++++++---
 1 file changed, 17 insertions(+), 3 deletions(-)
hw/arm/virt.c +17 -3
index 2353440..9b34e94 100644
@@ -573,15 +573,19 @@ static void machvirt_init(MachineState *machine)
     MemoryRegion *ram = g_new(MemoryRegion, 1);
     const char *cpu_model = machine->cpu_model;
     VirtBoardInfo *vbi;
+    char **cpustr;
 
     if (!cpu_model) {
         cpu_model = "cortex-a15";
     }
 
-    vbi = find_machine_info(cpu_model);
+    /* Separate the actual CPU model name from any appended features */
+    cpustr = g_strsplit(cpu_model, ",", 2);
+
+    vbi = find_machine_info(cpustr[0]);
 
     if (!vbi) {
-        error_report("mach-virt: CPU %s not supported", cpu_model);
+        error_report("mach-virt: CPU %s not supported", cpustr[0]);
         exit(1);
     }
 
@@ -595,8 +599,10 @@ static void machvirt_init(MachineState *machine)
     create_fdt(vbi);
 
     for (n = 0; n < smp_cpus; n++) {
-        ObjectClass *oc = cpu_class_by_name(TYPE_ARM_CPU, cpu_model);
+        ObjectClass *oc = cpu_class_by_name(TYPE_ARM_CPU, cpustr[0]);
+        CPUClass *cc = CPU_CLASS(oc);
         Object *cpuobj;
+        Error *err = NULL;
 
         if (!oc) {
             fprintf(stderr, "Unable to find CPU definition\n");
@@ -604,6 +610,13 @@ static void machvirt_init(MachineState *machine)
         }
         cpuobj = object_new(object_class_get_name(oc));
 
+        /* Handle any CPU options specfied by the user */
+        cc->parse_features(CPU(cpuobj), cpustr[1], &err);
+        if (err) {
+            error_report("%s", error_get_pretty(err));
+            exit(1);
+        }
+
         if (!vms->secure) {
             object_property_set_bool(cpuobj, false, "has_el3", NULL);
         }
@@ -623,6 +636,7 @@ static void machvirt_init(MachineState *machine)
 
         object_property_set_bool(cpuobj, true, "realized", NULL);
     }
+    g_strfreev(cpustr);
     fdt_add_timer_nodes(vbi);
     fdt_add_cpu_nodes(vbi);
     fdt_add_psci_node(vbi);
Signature · 1 line
1.8.3.2
Reviewed-by on v3 2/4
#8 lore raw

Re: [Qemu-devel] [PATCH v3 2/4] target-arm: Add feature parsing to virt

hw/arm/virt.c static void machvirt_init(MachineState *machine) · line 585

Quoted 8 lines · Greg Bellows

Added machvirt parsing of feature keywords added to the -cpu command line option. Parsing occurs during machine initialization.

hw/arm/virt.c +4 -1
-    vbi = find_machine_info(cpu_model);
+    /* Separate the actual CPU model name from any appended features */
+    cpustr = g_strsplit(cpu_model, ",", 2);
+
+    vbi = find_machine_info(cpustr[0]);

It seems a shame we have to do this kind of manual string fiddling in every board model, but that is definitely not a problem to address in this patchset.

hw/arm/virt.c static void machvirt_init(MachineState *machine) · line 613

Quoted 5 lines
hw/arm/virt.c +1 -0
@@ -604,6 +610,13 @@ static void machvirt_init(MachineState *machine)
         }
         cpuobj = object_new(object_class_get_name(oc));
 
+        /* Handle any CPU options specfied by the user */

"specified"

hw/arm/virt.c static void machvirt_init(MachineState *machine) · line 618

Quoted 5 lines
hw/arm/virt.c +5 -0
+        cc->parse_features(CPU(cpuobj), cpustr[1], &err);
+        if (err) {
+            error_report("%s", error_get_pretty(err));
+            exit(1);
+        }

Otherwise

thanks
-- PMM

#9 lore raw

PATCH v3 3/4 target-arm: Add 32/64-bit register sync

In reply to Greg Bellows

Add AArch32 to AArch64 register sychronization functions. Replace manual register synchronization with new functions in aarch64_cpu_do_interrupt() and HELPER(exception_return)().

---

v2 -> v3
- Conditionalize interrupt handler update of aarch64.

---
 target-arm/helper-a64.c |  9 +++--
 target-arm/internals.h  | 89 +++++++++++++++++++++++++++++++++++++++++++++++++
 target-arm/op_helper.c  |  6 ++--
 3 files changed, 95 insertions(+), 9 deletions(-)
target-arm/helper-a64.c +4 -5
index 81066ca..8c6a100 100644
@@ -448,7 +448,6 @@ void aarch64_cpu_do_interrupt(CPUState *cs)
     unsigned int new_el = arm_excp_target_el(cs, cs->exception_index);
     target_ulong addr = env->cp15.vbar_el[new_el];
     unsigned int new_mode = aarch64_pstate_mode(new_el, true);
-    int i;
 
     if (arm_current_el(env) < new_el) {
         if (env->aarch64) {
@@ -512,15 +511,15 @@ void aarch64_cpu_do_interrupt(CPUState *cs)
         }
         env->elr_el[new_el] = env->regs[15];
 
-        for (i = 0; i < 15; i++) {
-            env->xregs[i] = env->regs[i];
-        }
+        aarch64_sync_32_to_64(env);
 
         env->condexec_bits = 0;
     }
 
     pstate_write(env, PSTATE_DAIF | new_mode);
-    env->aarch64 = 1;
+    if (arm_feature(env, ARM_FEATURE_AARCH64)) {
+        env->aarch64 = 1;
+    }
     aarch64_restore_sp(env, new_el);
 
     env->pc = addr;
target-arm/internals.h +89 -0

95 lines not on this page · raw message

target-arm/op_helper.c +2 -4
index 2bed914..7713022 100644
@@ -465,7 +465,7 @@ void HELPER(exception_return)(CPUARMState *env)
     int cur_el = arm_current_el(env);
     unsigned int spsr_idx = aarch64_banked_spsr_index(cur_el);
     uint32_t spsr = env->banked_spsr[spsr_idx];
-    int new_el, i;
+    int new_el;
 
     aarch64_save_sp(env, cur_el);
 
@@ -491,9 +491,7 @@ void HELPER(exception_return)(CPUARMState *env)
         if (!arm_singlestep_active(env)) {
             env->uncached_cpsr &= ~PSTATE_SS;
         }
-        for (i = 0; i < 15; i++) {
-            env->regs[i] = env->xregs[i];
-        }
+        aarch64_sync_64_to_32(env);
 
         env->regs[15] = env->elr_el[1] & ~0x1;
     } else {
Signature · 1 line
1.8.3.2
#10 lore raw

Re: [Qemu-devel] [PATCH v3 3/4] target-arm: Add 32/64-bit register sync

Quoted 35 lines · Greg Bellows

Add AArch32 to AArch64 register sychronization functions. Replace manual register synchronization with new functions in aarch64_cpu_do_interrupt() and HELPER(exception_return)().

---

v2 -> v3
- Conditionalize interrupt handler update of aarch64.

---
 target-arm/helper-a64.c |  9 +++--
 target-arm/internals.h  | 89 +++++++++++++++++++++++++++++++++++++++++++++++++
 target-arm/op_helper.c  |  6 ++--
 3 files changed, 95 insertions(+), 9 deletions(-)
target-arm/helper-a64.c +0 -4
index 81066ca..8c6a100 100644
@@ -448,7 +448,6 @@ void aarch64_cpu_do_interrupt(CPUState *cs)
     unsigned int new_el = arm_excp_target_el(cs, cs->exception_index);
     target_ulong addr = env->cp15.vbar_el[new_el];
     unsigned int new_mode = aarch64_pstate_mode(new_el, true);
-    int i;
 
     if (arm_current_el(env) < new_el) {
         if (env->aarch64) {
@@ -512,15 +511,15 @@ void aarch64_cpu_do_interrupt(CPUState *cs)
         }
         env->elr_el[new_el] = env->regs[15];
 
-        for (i = 0; i < 15; i++) {
-            env->xregs[i] = env->regs[i];
-        }

target-arm/helper-a64.c void aarch64_cpu_do_interrupt(CPUState *cs) · line 522

Quoted 10 lines
target-arm/helper-a64.c +4 -1
+        aarch64_sync_32_to_64(env);
 
         env->condexec_bits = 0;
     }
 
     pstate_write(env, PSTATE_DAIF | new_mode);
-    env->aarch64 = 1;
+    if (arm_feature(env, ARM_FEATURE_AARCH64)) {
+        env->aarch64 = 1;
+    }

This doesn't look correct. If this CPU doesn't have the AArch64 feature then pretty much all of what this function does is wrong, because it is the "take exception to an EL that is using AArch64" function. We need to ensure that CPUs without the feature bit never call this, either by setting the CPU class method pointer correctly (which is what happens at the moment for the traditional 32-bit-only CPUs) or else we need to arrange that we dynamically call the right function depending on the register width of the EL we're about to take the exception to. We'll definitely need to handle that latter case for 64-bit EL3 or EL2 support (since suddenly EL1 can be 32-bit even in a CPU with 64-bit support enabled).

target-arm/internals.h static inline void aarch64_restore_sp(CPUARMState *env, int el) · line 131

Quoted 12 lines
target-arm/helper-a64.c +0 -0
     aarch64_restore_sp(env, new_el);
 
     env->pc = addr;
target-arm/internals.h +1 -0
index bb171a7..626ea7d 100644
@@ -128,6 +128,95 @@ static inline void aarch64_restore_sp(CPUARMState *env, int el)
     }
 }
 
+static inline void aarch64_sync_32_to_64(CPUARMState *env)

These functions are pretty big; I would just put them in a suitable .c file rather than have them be static inline in a .h file. A few lines of doc comment describing the purpose of the functions would be nice too.

Quoted 24 lines
target-arm/internals.h +24 -0
+{
+    int i;
+
+    /* We can blanket copy R[0:7] to X[0:7] */
+    for (i = 0; i < 8; i++) {
+        env->xregs[i] = env->regs[i];
+    }
+
+    /* If we are in USR mode then we just want to complete the above blanket
+     * copy so we get the accurate register values.  If not, then we have to go
+     * to the saved and banked user regs.
+     */
+    if ((env->uncached_cpsr & 0x1f) == ARM_CPU_MODE_USR) {
+        for (i = 8; i < 15; i++) {
+            env->xregs[i] = env->regs[i];
+        }
+    } else {
+        for (i = 8; i < 13; i++) {
+            env->xregs[i] = env->usr_regs[i-8];
+        }
+        env->xregs[13] = env->banked_r13[bank_number(ARM_CPU_MODE_USR)];
+        env->xregs[14] = env->banked_r14[bank_number(ARM_CPU_MODE_USR)];
+    }
+    env->pc = env->regs[15];

target-arm/internals.h static inline void aarch64_restore_sp(CPUARMState *env, int el) · line 165

Quoted 10 lines
target-arm/internals.h +10 -0
+
+    env->xregs[15] = env->banked_r13[bank_number(ARM_CPU_MODE_HYP)];
+    env->xregs[16] = env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)];
+    env->xregs[17] = env->banked_r14[bank_number(ARM_CPU_MODE_IRQ)];
+    env->xregs[18] = env->banked_r13[bank_number(ARM_CPU_MODE_SVC)];
+    env->xregs[19] = env->banked_r14[bank_number(ARM_CPU_MODE_SVC)];
+    env->xregs[20] = env->banked_r13[bank_number(ARM_CPU_MODE_ABT)];
+    env->xregs[21] = env->banked_r14[bank_number(ARM_CPU_MODE_ABT)];
+    env->xregs[22] = env->banked_r13[bank_number(ARM_CPU_MODE_UND)];
+    env->xregs[23] = env->banked_r14[bank_number(ARM_CPU_MODE_UND)];

This isn't going to give the right answers if we happen to be in one of these modes. For instance if we're in SVC mode then SVC's r13 (which needs to be copied into xregs[18]) is going to be in env->regs[13], not in the banked_r13 slot.

target-arm/internals.h static inline void aarch64_restore_sp(CPUARMState *env, int el) · line 168

Quoted 3 lines
target-arm/internals.h +3 -0
+
+    for (i = 0; i < 5; i++) {
+        env->xregs[24+i] = env->fiq_regs[i];

Similarly if we're in FIQ mode then the remainder of the FIQ registers are in env->regs[8] to [14], not in the fiq_regs[] or banked_r13[] or banked_r14[].

PS: spaces round operators like '+', please.

Quoted 8 lines
target-arm/internals.h +8 -0
+    }
+    env->xregs[29] = env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)];
+    env->xregs[30] = env->banked_r14[bank_number(ARM_CPU_MODE_FIQ)];
+}
+
+static inline void aarch64_sync_64_to_32(CPUARMState *env)
+{
+    int i;

target-arm/internals.h static inline void aarch64_restore_sp(CPUARMState *env, int el) · line 186

Quoted 10 lines
target-arm/internals.h +10 -0
+
+    /* We can blanket copy R[0:7] to X[0:7] */
+    for (i = 0; i < 8; i++) {
+        env->regs[i] = env->xregs[i];
+    }
+
+    /* If we are in USR mode then we want to complete the above blanket
+     * copy as the XREGs will contain the most recent value.
+     */
+    if ((env->uncached_cpsr & 0x1f) == ARM_CPU_MODE_USR) {

use the CPSR_M mask, not 0x1f.

target-arm/internals.h static inline void aarch64_restore_sp(CPUARMState *env, int el) · line 189

Quoted 4 lines
target-arm/internals.h +4 -0
+        for (i = 8; i < 15; i++) {
+            env->regs[i] = env->xregs[i];
+        }
+    }

This is missing the update of regs[8..14] for modes other than USR (which is sort of the inverse of the problem in the preceding function).

target-arm/internals.h static inline void aarch64_restore_sp(CPUARMState *env, int el) · line 201

Quoted 11 lines
target-arm/internals.h +11 -0
+
+    /* Update the user copies and banked registers so they are also up to
+     * date.
+     */
+    for (i = 8; i < 13; i++) {
+        env->usr_regs[i-8] = env->xregs[i];
+    }
+    env->banked_r13[bank_number(ARM_CPU_MODE_USR)] = env->xregs[13];
+    env->banked_r14[bank_number(ARM_CPU_MODE_USR)] = env->xregs[14];
+
+    env->regs[15] = env->pc;

This feels a bit out of place -- I'd put it up with the rest of the updates of env->regs[].

Quoted 34 lines
target-arm/internals.h +17 -0
+    env->banked_r13[bank_number(ARM_CPU_MODE_HYP)] = env->xregs[15];
+    env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[16];
+    env->banked_r14[bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[17];
+    env->banked_r13[bank_number(ARM_CPU_MODE_SVC)] = env->xregs[18];
+    env->banked_r14[bank_number(ARM_CPU_MODE_SVC)] = env->xregs[19];
+    env->banked_r13[bank_number(ARM_CPU_MODE_ABT)] = env->xregs[20];
+    env->banked_r14[bank_number(ARM_CPU_MODE_ABT)] = env->xregs[21];
+    env->banked_r13[bank_number(ARM_CPU_MODE_UND)] = env->xregs[22];
+    env->banked_r14[bank_number(ARM_CPU_MODE_UND)] = env->xregs[23];
+
+    for (i = 0; i < 5; i++) {
+        env->fiq_regs[i] = env->xregs[24+i];
+    }
+    env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[29];
+    env->banked_r14[bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[30];
+}
+
 static inline void update_spsel(CPUARMState *env, uint32_t imm)
 {
     unsigned int cur_el = arm_current_el(env);
target-arm/op_helper.c +1 -1
index 2bed914..7713022 100644
@@ -465,7 +465,7 @@ void HELPER(exception_return)(CPUARMState *env)
     int cur_el = arm_current_el(env);
     unsigned int spsr_idx = aarch64_banked_spsr_index(cur_el);
     uint32_t spsr = env->banked_spsr[spsr_idx];
-    int new_el, i;
+    int new_el;
 
     aarch64_save_sp(env, cur_el);
 

target-arm/op_helper.c void HELPER(exception_return)(CPUARMState *env) · line 497

Quoted 10 lines
target-arm/op_helper.c +1 -3
@@ -491,9 +491,7 @@ void HELPER(exception_return)(CPUARMState *env)
         if (!arm_singlestep_active(env)) {
             env->uncached_cpsr &= ~PSTATE_SS;
         }
-        for (i = 0; i < 15; i++) {
-            env->regs[i] = env->xregs[i];
-        }
+        aarch64_sync_64_to_32(env);
 
         env->regs[15] = env->elr_el[1] & ~0x1;
     } else {

thanks
-- PMM

#11 lore raw

Re: [Qemu-devel] [PATCH v3 3/4] target-arm: Add 32/64-bit register sync

Quoted 73 lines · Peter Maydell
Quoted 6 lines · Greg Bellows

Add AArch32 to AArch64 register sychronization functions. Replace manual register synchronization with new functions in aarch64_cpu_do_interrupt() and HELPER(exception_return)().

Quoted 7 lines

---

v2 -> v3
- Conditionalize interrupt handler update of aarch64.

---
 target-arm/helper-a64.c |  9 +++--
 target-arm/internals.h  | 89

+++++++++++++++++++++++++++++++++++++++++++++++++

Quoted 22 lines
 target-arm/op_helper.c  |  6 ++--
 3 files changed, 95 insertions(+), 9 deletions(-)
target-arm/helper-a64.c +0 -4
index 81066ca..8c6a100 100644
@@ -448,7 +448,6 @@ void aarch64_cpu_do_interrupt(CPUState *cs)
     unsigned int new_el = arm_excp_target_el(cs, cs->exception_index);
     target_ulong addr = env->cp15.vbar_el[new_el];
     unsigned int new_mode = aarch64_pstate_mode(new_el, true);
-    int i;
 
     if (arm_current_el(env) < new_el) {
         if (env->aarch64) {
@@ -512,15 +511,15 @@ void aarch64_cpu_do_interrupt(CPUState *cs)
         }
         env->elr_el[new_el] = env->regs[15];
 
-        for (i = 0; i < 15; i++) {
-            env->xregs[i] = env->regs[i];
-        }

target-arm/helper-a64.c void aarch64_cpu_do_interrupt(CPUState *cs) · line 522

Quoted 10 lines
target-arm/helper-a64.c +4 -1
+        aarch64_sync_32_to_64(env);
 
         env->condexec_bits = 0;
     }
 
     pstate_write(env, PSTATE_DAIF | new_mode);
-    env->aarch64 = 1;
+    if (arm_feature(env, ARM_FEATURE_AARCH64)) {
+        env->aarch64 = 1;
+    }

This doesn't look correct. If this CPU doesn't have the AArch64 feature then pretty much all of what this function does is wrong, because it is the "take exception to an EL that is using AArch64" function. We need to ensure that CPUs without the feature bit never call this, either by setting the CPU class method pointer correctly (which is what happens at the moment for the traditional 32-bit-only CPUs) or else we need to arrange that we dynamically call the right function depending on the register width of the EL we're about to take the exception to. We'll definitely need to handle that latter case for 64-bit EL3 or EL2 support (since suddenly EL1 can be 32-bit even in a CPU with 64-bit support enabled).

target-arm/internals.h static inline void aarch64_restore_sp(CPUARMState

Quoted 8 lines
target-arm/helper-a64.c +0 -0
     aarch64_restore_sp(env, new_el);
 
     env->pc = addr;
target-arm/internals.h +0 -0
index bb171a7..626ea7d 100644
@@ -128,6 +128,95 @@ static inline void aarch64_restore_sp(CPUARMState

*env, int el)

Quoted 3 lines
target-arm/internals.h +0 -0
     }
 }
 
Quoted 7 lines

target-arm/internals.h static inline void aarch64_restore_sp(CPUARMState *env, int el) · line 131

Quoted 1 line
target-arm/internals.h +1 -0
+static inline void aarch64_sync_32_to_64(CPUARMState *env)

These functions are pretty big; I would just put them in a suitable .c file rather than have them be static inline in a .h file. A few lines of doc comment describing the purpose of the functions would be nice too.

​Moved and commented in v4
​

Quoted 38 lines

target-arm/internals.h static inline void aarch64_restore_sp(CPUARMState *env, int el)

Quoted 10 lines
target-arm/internals.h +9 -0
+{
+    int i;
+
+    /* We can blanket copy R[0:7] to X[0:7] */
+    for (i = 0; i < 8; i++) {
+        env->xregs[i] = env->regs[i];
+    }
+
+    /* If we are in USR mode then we just want to complete the above

blanket

target-arm/internals.h static inline void aarch64_restore_sp(CPUARMState · line 141

Quoted 1 line
target-arm/internals.h +1 -0
+     * copy so we get the accurate register values.  If not, then we

have to go

Quoted 14 lines
target-arm/internals.h +14 -0
+     * to the saved and banked user regs.
+     */
+    if ((env->uncached_cpsr & 0x1f) == ARM_CPU_MODE_USR) {
+        for (i = 8; i < 15; i++) {
+            env->xregs[i] = env->regs[i];
+        }
+    } else {
+        for (i = 8; i < 13; i++) {
+            env->xregs[i] = env->usr_regs[i-8];
+        }
+        env->xregs[13] = env->banked_r13[bank_number(ARM_CPU_MODE_USR)];
+        env->xregs[14] = env->banked_r14[bank_number(ARM_CPU_MODE_USR)];
+    }
+    env->pc = env->regs[15];

target-arm/internals.h static inline void aarch64_restore_sp(CPUARMState *env, int el) · line 165

Quoted 10 lines
target-arm/internals.h +10 -0
+
+    env->xregs[15] = env->banked_r13[bank_number(ARM_CPU_MODE_HYP)];
+    env->xregs[16] = env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)];
+    env->xregs[17] = env->banked_r14[bank_number(ARM_CPU_MODE_IRQ)];
+    env->xregs[18] = env->banked_r13[bank_number(ARM_CPU_MODE_SVC)];
+    env->xregs[19] = env->banked_r14[bank_number(ARM_CPU_MODE_SVC)];
+    env->xregs[20] = env->banked_r13[bank_number(ARM_CPU_MODE_ABT)];
+    env->xregs[21] = env->banked_r14[bank_number(ARM_CPU_MODE_ABT)];
+    env->xregs[22] = env->banked_r13[bank_number(ARM_CPU_MODE_UND)];
+    env->xregs[23] = env->banked_r14[bank_number(ARM_CPU_MODE_UND)];
Quoted 5 lines

This isn't going to give the right answers if we happen to be in one of these modes. For instance if we're in SVC mode then SVC's r13 (which needs to be copied into xregs[18]) is going to be in env->regs[13], not in the banked_r13 slot.

​Yes, you are correct and I dealt with this for USR mode but for some reason failed to do so for the other modes. Fixed in v4.​

Quoted 11 lines

target-arm/internals.h static inline void aarch64_restore_sp(CPUARMState *env, int el) · line 168

Quoted 4 lines
target-arm/internals.h +3 -0
+
+    for (i = 0; i < 5; i++) {
+        env->xregs[24+i] = env->fiq_regs[i];

Similarly if we're in FIQ mode then the remainder of the FIQ registers are in env->regs[8] to [14], not in the fiq_regs[] or banked_r13[] or banked_r14[].

PS: spaces round operators like '+', please.

​Fixed in v4

Quoted 22 lines
Quoted 9 lines
target-arm/internals.h +8 -0
+    }
+    env->xregs[29] = env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)];
+    env->xregs[30] = env->banked_r14[bank_number(ARM_CPU_MODE_FIQ)];
+}
+
+static inline void aarch64_sync_64_to_32(CPUARMState *env)
+{
+    int i;

target-arm/internals.h static inline void aarch64_restore_sp(CPUARMState *env, int el) · line 186

Quoted 10 lines
target-arm/internals.h +10 -0
+
+    /* We can blanket copy R[0:7] to X[0:7] */
+    for (i = 0; i < 8; i++) {
+        env->regs[i] = env->xregs[i];
+    }
+
+    /* If we are in USR mode then we want to complete the above blanket
+     * copy as the XREGs will contain the most recent value.
+     */
+    if ((env->uncached_cpsr & 0x1f) == ARM_CPU_MODE_USR) {

use the CPSR_M mask, not 0x1f.

Quoted 8 lines

target-arm/internals.h static inline void aarch64_restore_sp(CPUARMState *env, int el) · line 189

Quoted 4 lines
target-arm/internals.h +4 -0
+        for (i = 8; i < 15; i++) {
+            env->regs[i] = env->xregs[i];
+        }
+    }

This is missing the update of regs[8..14] for modes other than USR (which is sort of the inverse of the problem in the preceding function).

Yes, this is due to the same omission you pointed out above. I should also point out that r8:r12 only get sync'd when in USR or FIQ mode.

Quoted 14 lines

target-arm/internals.h static inline void aarch64_restore_sp(CPUARMState · line 192

Quoted 3 lines
target-arm/internals.h +2 -0
+
+    /* Update the user copies and banked registers so they are also up

to

target-arm/internals.h static inline void aarch64_restore_sp(CPUARMState *env, int el) · line 201

Quoted 9 lines
target-arm/internals.h +9 -0
+     * date.
+     */
+    for (i = 8; i < 13; i++) {
+        env->usr_regs[i-8] = env->xregs[i];
+    }
+    env->banked_r13[bank_number(ARM_CPU_MODE_USR)] = env->xregs[13];
+    env->banked_r14[bank_number(ARM_CPU_MODE_USR)] = env->xregs[14];
+
+    env->regs[15] = env->pc;
Quoted 3 lines

This feels a bit out of place -- I'd put it up with the rest of the updates of env->regs[].

​I originally put this at the end because regs[15] was the last register numerically to be updated. I think the end makes even more sense given my v4 changes. Let me know if you agree.​

Quoted 49 lines
Quoted 35 lines
target-arm/internals.h +17 -0
+    env->banked_r13[bank_number(ARM_CPU_MODE_HYP)] = env->xregs[15];
+    env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[16];
+    env->banked_r14[bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[17];
+    env->banked_r13[bank_number(ARM_CPU_MODE_SVC)] = env->xregs[18];
+    env->banked_r14[bank_number(ARM_CPU_MODE_SVC)] = env->xregs[19];
+    env->banked_r13[bank_number(ARM_CPU_MODE_ABT)] = env->xregs[20];
+    env->banked_r14[bank_number(ARM_CPU_MODE_ABT)] = env->xregs[21];
+    env->banked_r13[bank_number(ARM_CPU_MODE_UND)] = env->xregs[22];
+    env->banked_r14[bank_number(ARM_CPU_MODE_UND)] = env->xregs[23];
+
+    for (i = 0; i < 5; i++) {
+        env->fiq_regs[i] = env->xregs[24+i];
+    }
+    env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[29];
+    env->banked_r14[bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[30];
+}
+
 static inline void update_spsel(CPUARMState *env, uint32_t imm)
 {
     unsigned int cur_el = arm_current_el(env);
target-arm/op_helper.c +1 -1
index 2bed914..7713022 100644
@@ -465,7 +465,7 @@ void HELPER(exception_return)(CPUARMState *env)
     int cur_el = arm_current_el(env);
     unsigned int spsr_idx = aarch64_banked_spsr_index(cur_el);
     uint32_t spsr = env->banked_spsr[spsr_idx];
-    int new_el, i;
+    int new_el;
 
     aarch64_save_sp(env, cur_el);
 

target-arm/op_helper.c void HELPER(exception_return)(CPUARMState *env) · line 497

Quoted 10 lines
target-arm/op_helper.c +1 -3
@@ -491,9 +491,7 @@ void HELPER(exception_return)(CPUARMState *env)
         if (!arm_singlestep_active(env)) {
             env->uncached_cpsr &= ~PSTATE_SS;
         }
-        for (i = 0; i < 15; i++) {
-            env->regs[i] = env->xregs[i];
-        }
+        aarch64_sync_64_to_32(env);
 
         env->regs[15] = env->elr_el[1] & ~0x1;
     } else {

thanks
-- PMM

#12 lore raw

Re: [Qemu-devel] [PATCH v3 3/4] target-arm: Add 32/64-bit register sync

In reply to Peter Maydell
Quoted 52 lines · Peter Maydell
Quoted 6 lines · Greg Bellows

Add AArch32 to AArch64 register sychronization functions. Replace manual register synchronization with new functions in aarch64_cpu_do_interrupt() and HELPER(exception_return)().

Quoted 7 lines

---

v2 -> v3
- Conditionalize interrupt handler update of aarch64.

---
 target-arm/helper-a64.c |  9 +++--
 target-arm/internals.h  | 89

+++++++++++++++++++++++++++++++++++++++++++++++++

Quoted 22 lines
 target-arm/op_helper.c  |  6 ++--
 3 files changed, 95 insertions(+), 9 deletions(-)
target-arm/helper-a64.c +0 -4
index 81066ca..8c6a100 100644
@@ -448,7 +448,6 @@ void aarch64_cpu_do_interrupt(CPUState *cs)
     unsigned int new_el = arm_excp_target_el(cs, cs->exception_index);
     target_ulong addr = env->cp15.vbar_el[new_el];
     unsigned int new_mode = aarch64_pstate_mode(new_el, true);
-    int i;
 
     if (arm_current_el(env) < new_el) {
         if (env->aarch64) {
@@ -512,15 +511,15 @@ void aarch64_cpu_do_interrupt(CPUState *cs)
         }
         env->elr_el[new_el] = env->regs[15];
 
-        for (i = 0; i < 15; i++) {
-            env->xregs[i] = env->regs[i];
-        }

target-arm/helper-a64.c void aarch64_cpu_do_interrupt(CPUState *cs) · line 522

Quoted 10 lines
target-arm/helper-a64.c +4 -1
+        aarch64_sync_32_to_64(env);
 
         env->condexec_bits = 0;
     }
 
     pstate_write(env, PSTATE_DAIF | new_mode);
-    env->aarch64 = 1;
+    if (arm_feature(env, ARM_FEATURE_AARCH64)) {
+        env->aarch64 = 1;
+    }

This doesn't look correct. If this CPU doesn't have the AArch64 feature then pretty much all of what this function does is wrong, because it is the "take exception to an EL that is using AArch64" function. We need to ensure that CPUs without the feature

Quoted 9 lines

bit never call this, either by setting the CPU class method pointer correctly (which is what happens at the moment for the traditional 32-bit-only CPUs) or else we need to arrange that we dynamically call the right function depending on the register width of the EL we're about to take the exception to. We'll definitely need to handle that latter case for 64-bit EL3 or EL2 support (since suddenly EL1 can be 32-bit even in a CPU with 64-bit support enabled).

​​I think this is deeper than the one routine. Essentially, we have to convert the AArch64 CPU object into an ARM CPU object on the fly. This would have to happen after class instantiation because we don't have the properties until after. From looking at the code, I'm not convinced it is safe to override the fields, but I need to look a bit more.

I wanted to understand more, so I ran without my changes to see if aarch64 ever gets cleared and sure enough it does. Is this expected?

I wasn't convinced this should occur, so I investigated further. The only place I see us explicitly set it to 0 is in exception return when spsr has nRW set. So I set a watch point on the spsr, and sure enough I see an update occur setting nRW which means aarch64 gets set to 0.

I realize it is possible for the PSR to get set but are we prepared to handle it?

Quoted 164 lines

target-arm/internals.h static inline void aarch64_restore_sp(CPUARMState

Quoted 8 lines
target-arm/helper-a64.c +0 -0
     aarch64_restore_sp(env, new_el);
 
     env->pc = addr;
target-arm/internals.h +0 -0
index bb171a7..626ea7d 100644
@@ -128,6 +128,95 @@ static inline void aarch64_restore_sp(CPUARMState

*env, int el)

target-arm/internals.h static inline void aarch64_restore_sp(CPUARMState *env, int el) · line 131

Quoted 4 lines
target-arm/internals.h +1 -0
     }
 }
 
+static inline void aarch64_sync_32_to_64(CPUARMState *env)

These functions are pretty big; I would just put them in a suitable .c file rather than have them be static inline in a .h file. A few lines of doc comment describing the purpose of the functions would be nice too.

target-arm/internals.h static inline void aarch64_restore_sp(CPUARMState *env, int el)

Quoted 9 lines
target-arm/internals.h +9 -0
+{
+    int i;
+
+    /* We can blanket copy R[0:7] to X[0:7] */
+    for (i = 0; i < 8; i++) {
+        env->xregs[i] = env->regs[i];
+    }
+
+    /* If we are in USR mode then we just want to complete the above

blanket

target-arm/internals.h static inline void aarch64_restore_sp(CPUARMState · line 141

Quoted 1 line
target-arm/internals.h +1 -0
+     * copy so we get the accurate register values.  If not, then we

have to go

Quoted 14 lines
target-arm/internals.h +14 -0
+     * to the saved and banked user regs.
+     */
+    if ((env->uncached_cpsr & 0x1f) == ARM_CPU_MODE_USR) {
+        for (i = 8; i < 15; i++) {
+            env->xregs[i] = env->regs[i];
+        }
+    } else {
+        for (i = 8; i < 13; i++) {
+            env->xregs[i] = env->usr_regs[i-8];
+        }
+        env->xregs[13] = env->banked_r13[bank_number(ARM_CPU_MODE_USR)];
+        env->xregs[14] = env->banked_r14[bank_number(ARM_CPU_MODE_USR)];
+    }
+    env->pc = env->regs[15];

target-arm/internals.h static inline void aarch64_restore_sp(CPUARMState *env, int el) · line 165

Quoted 10 lines
target-arm/internals.h +10 -0
+
+    env->xregs[15] = env->banked_r13[bank_number(ARM_CPU_MODE_HYP)];
+    env->xregs[16] = env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)];
+    env->xregs[17] = env->banked_r14[bank_number(ARM_CPU_MODE_IRQ)];
+    env->xregs[18] = env->banked_r13[bank_number(ARM_CPU_MODE_SVC)];
+    env->xregs[19] = env->banked_r14[bank_number(ARM_CPU_MODE_SVC)];
+    env->xregs[20] = env->banked_r13[bank_number(ARM_CPU_MODE_ABT)];
+    env->xregs[21] = env->banked_r14[bank_number(ARM_CPU_MODE_ABT)];
+    env->xregs[22] = env->banked_r13[bank_number(ARM_CPU_MODE_UND)];
+    env->xregs[23] = env->banked_r14[bank_number(ARM_CPU_MODE_UND)];

This isn't going to give the right answers if we happen to be in one of these modes. For instance if we're in SVC mode then SVC's r13 (which needs to be copied into xregs[18]) is going to be in env->regs[13], not in the banked_r13 slot.

target-arm/internals.h static inline void aarch64_restore_sp(CPUARMState *env, int el) · line 168

Quoted 3 lines
target-arm/internals.h +3 -0
+
+    for (i = 0; i < 5; i++) {
+        env->xregs[24+i] = env->fiq_regs[i];

Similarly if we're in FIQ mode then the remainder of the FIQ registers are in env->regs[8] to [14], not in the fiq_regs[] or banked_r13[] or banked_r14[].

PS: spaces round operators like '+', please.

Quoted 8 lines
target-arm/internals.h +8 -0
+    }
+    env->xregs[29] = env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)];
+    env->xregs[30] = env->banked_r14[bank_number(ARM_CPU_MODE_FIQ)];
+}
+
+static inline void aarch64_sync_64_to_32(CPUARMState *env)
+{
+    int i;

target-arm/internals.h static inline void aarch64_restore_sp(CPUARMState *env, int el) · line 186

Quoted 10 lines
target-arm/internals.h +10 -0
+
+    /* We can blanket copy R[0:7] to X[0:7] */
+    for (i = 0; i < 8; i++) {
+        env->regs[i] = env->xregs[i];
+    }
+
+    /* If we are in USR mode then we want to complete the above blanket
+     * copy as the XREGs will contain the most recent value.
+     */
+    if ((env->uncached_cpsr & 0x1f) == ARM_CPU_MODE_USR) {

use the CPSR_M mask, not 0x1f.

target-arm/internals.h static inline void aarch64_restore_sp(CPUARMState *env, int el) · line 189

Quoted 4 lines
target-arm/internals.h +4 -0
+        for (i = 8; i < 15; i++) {
+            env->regs[i] = env->xregs[i];
+        }
+    }

This is missing the update of regs[8..14] for modes other than USR (which is sort of the inverse of the problem in the preceding function).

target-arm/internals.h static inline void aarch64_restore_sp(CPUARMState · line 192

Quoted 2 lines
target-arm/internals.h +2 -0
+
+    /* Update the user copies and banked registers so they are also up

to

target-arm/internals.h static inline void aarch64_restore_sp(CPUARMState *env, int el) · line 201

Quoted 9 lines
target-arm/internals.h +9 -0
+     * date.
+     */
+    for (i = 8; i < 13; i++) {
+        env->usr_regs[i-8] = env->xregs[i];
+    }
+    env->banked_r13[bank_number(ARM_CPU_MODE_USR)] = env->xregs[13];
+    env->banked_r14[bank_number(ARM_CPU_MODE_USR)] = env->xregs[14];
+
+    env->regs[15] = env->pc;

This feels a bit out of place -- I'd put it up with the rest of the updates of env->regs[].

Quoted 34 lines
target-arm/internals.h +17 -0
+    env->banked_r13[bank_number(ARM_CPU_MODE_HYP)] = env->xregs[15];
+    env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[16];
+    env->banked_r14[bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[17];
+    env->banked_r13[bank_number(ARM_CPU_MODE_SVC)] = env->xregs[18];
+    env->banked_r14[bank_number(ARM_CPU_MODE_SVC)] = env->xregs[19];
+    env->banked_r13[bank_number(ARM_CPU_MODE_ABT)] = env->xregs[20];
+    env->banked_r14[bank_number(ARM_CPU_MODE_ABT)] = env->xregs[21];
+    env->banked_r13[bank_number(ARM_CPU_MODE_UND)] = env->xregs[22];
+    env->banked_r14[bank_number(ARM_CPU_MODE_UND)] = env->xregs[23];
+
+    for (i = 0; i < 5; i++) {
+        env->fiq_regs[i] = env->xregs[24+i];
+    }
+    env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[29];
+    env->banked_r14[bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[30];
+}
+
 static inline void update_spsel(CPUARMState *env, uint32_t imm)
 {
     unsigned int cur_el = arm_current_el(env);
target-arm/op_helper.c +1 -1
index 2bed914..7713022 100644
@@ -465,7 +465,7 @@ void HELPER(exception_return)(CPUARMState *env)
     int cur_el = arm_current_el(env);
     unsigned int spsr_idx = aarch64_banked_spsr_index(cur_el);
     uint32_t spsr = env->banked_spsr[spsr_idx];
-    int new_el, i;
+    int new_el;
 
     aarch64_save_sp(env, cur_el);
 

target-arm/op_helper.c void HELPER(exception_return)(CPUARMState *env) · line 497

Quoted 10 lines
target-arm/op_helper.c +1 -3
@@ -491,9 +491,7 @@ void HELPER(exception_return)(CPUARMState *env)
         if (!arm_singlestep_active(env)) {
             env->uncached_cpsr &= ~PSTATE_SS;
         }
-        for (i = 0; i < 15; i++) {
-            env->regs[i] = env->xregs[i];
-        }
+        aarch64_sync_64_to_32(env);
 
         env->regs[15] = env->elr_el[1] & ~0x1;
     } else {

thanks
-- PMM

#13 lore raw

Re: [Qemu-devel] [PATCH v3 3/4] target-arm: Add 32/64-bit register sync

Quoted 11 lines · Greg Bellows
Quoted 4 lines · Peter Maydell

This doesn't look correct. If this CPU doesn't have the AArch64 feature then pretty much all of what this function does is wrong, because it is the "take exception to an EL that is using AArch64" function.

I think this is deeper than the one routine. Essentially, we have to convert the AArch64 CPU object into an ARM CPU object on the fly. This would have to happen after class instantiation because we don't have the properties until after. From looking at the code, I'm not convinced it is safe to override the fields, but I need to look a bit more.

I think we probably want the other approach -- leave the object being the class it is, but the functions have to cope with the CPU possibly being in 32 bit mode. We need that anyway for interworking between 64 bit EL3 and 32 bit EL1.

Quoted 2 lines

I wanted to understand more, so I ran without my changes to see if aarch64 ever gets cleared and sure enough it does. Is this expected?

I'm not sure what you mean here.

Quoted 4 lines

I wasn't convinced this should occur, so I investigated further. The only place I see us explicitly set it to 0 is in exception return when spsr has nRW set. So I set a watch point on the spsr, and sure enough I see an update occur setting nRW which means aarch64 gets set to 0.

This sounds like "exception return to a 32-bit EL0", which obviously clears the aarch64 flag.

Quoted 2 lines

I realize it is possible for the PSR to get set but are we prepared to handle it?

To handle what? At the moment the code assumes that if we're a 64 bit CPU then we can only be in 32 bit mode for an AArch32 EL0. This obviously needs to be fixed to support both 32-bit EL1 under 64-bit EL3 and also in this case to support a 64-bit CPU that only has 32-bit EL1. My original point is that the change to aarch64_cpu_do_interrupt() as you have it in this patch can't possibly work, because it is not actually doing what you need to do to handle taking exceptions to a 32 bit EL.

-- PMM

#14 lore raw

PATCH v3 4/4 target-arm: Add AArch32 guest support to KVM64

In reply to Greg Bellows

Add 32-bit to/from 64-bit register synchronization on register gets and puts. Set EL1_32BIT feature flag passed to KVM

---

v2 -> v3
- Conditionalize sync of 32-bit and 64-bit registers

---
 target-arm/kvm64.c | 33 +++++++++++++++++++++++++++++----
 1 file changed, 29 insertions(+), 4 deletions(-)
target-arm/kvm64.c +29 -4
index ba16821..924b423 100644
@@ -81,8 +81,7 @@ int kvm_arch_init_vcpu(CPUState *cs)
     int ret;
     ARMCPU *cpu = ARM_CPU(cs);
 
-    if (cpu->kvm_target == QEMU_KVM_ARM_TARGET_NONE ||
-        !arm_feature(&cpu->env, ARM_FEATURE_AARCH64)) {
+    if (cpu->kvm_target == QEMU_KVM_ARM_TARGET_NONE) {
         fprintf(stderr, "KVM is not supported for this guest CPU type\n");
         return -EINVAL;
     }
@@ -96,6 +95,9 @@ int kvm_arch_init_vcpu(CPUState *cs)
         cpu->psci_version = 2;
         cpu->kvm_init_features[0] |= 1 << KVM_ARM_VCPU_PSCI_0_2;
     }
+    if (!arm_feature(&cpu->env, ARM_FEATURE_AARCH64)) {
+        cpu->kvm_init_features[0] |= 1 << KVM_ARM_VCPU_EL1_32BIT;
+    }
 
     /* Do KVM_ARM_VCPU_INIT ioctl */
     ret = kvm_arm_vcpu_init(cs);
@@ -133,6 +135,13 @@ int kvm_arch_put_registers(CPUState *cs, int level)
     ARMCPU *cpu = ARM_CPU(cs);
     CPUARMState *env = &cpu->env;
 
+    /* If we are in AArch32 mode then we need to sync the AArch64 regs with the
+     * AArch32 regs before pushing them out 64-bit KVM.
+     */
+    if (!is_a64(env)) {
+        aarch64_sync_32_to_64(env);
+    }
+
     for (i = 0; i < 31; i++) {
         reg.id = AARCH64_CORE_REG(regs.regs[i]);
         reg.addr = (uintptr_t) &env->xregs[i];
@@ -162,7 +171,11 @@ int kvm_arch_put_registers(CPUState *cs, int level)
     }
 
     /* Note that KVM thinks pstate is 64 bit but we use a uint32_t */
-    val = pstate_read(env);
+    if (is_a64(env)) {
+        val = pstate_read(env);
+    } else {
+        val = cpsr_read(env);
+    }
     reg.id = AARCH64_CORE_REG(regs.pstate);
     reg.addr = (uintptr_t) &val;
     ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
@@ -219,6 +232,13 @@ int kvm_arch_get_registers(CPUState *cs)
         }
     }
 
+    /* If we are in AArch32 mode then we need to sync the AArch32 regs with the
+     * incoming AArch64 regs received from 64-bit KVM.
+     */
+    if (!is_a64(env)) {
+        aarch64_sync_64_to_32(env);
+    }
+
     reg.id = AARCH64_CORE_REG(regs.sp);
     reg.addr = (uintptr_t) &env->sp_el[0];
     ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
@@ -239,7 +259,12 @@ int kvm_arch_get_registers(CPUState *cs)
     if (ret) {
         return ret;
     }
-    pstate_write(env, val);
+    if (is_a64(env)) {
+        pstate_write(env, val);
+    } else {
+        env->uncached_cpsr = val & CPSR_M;
+        cpsr_write(env, val, 0xffffffff);
+    }
 
     /* KVM puts SP_EL0 in regs.sp and SP_EL1 in regs.sp_el1. On the
      * QEMU side we keep the current SP in xregs[31] as well.
Signature · 1 line
1.8.3.2
#15 lore raw

Re: [Qemu-devel] [PATCH v3 4/4] target-arm: Add AArch32 guest support to KVM64

Quoted 18 lines · Greg Bellows

Add 32-bit to/from 64-bit register synchronization on register gets and puts. Set EL1_32BIT feature flag passed to KVM

---

v2 -> v3
- Conditionalize sync of 32-bit and 64-bit registers

---
 target-arm/kvm64.c | 33 +++++++++++++++++++++++++++++----
 1 file changed, 29 insertions(+), 4 deletions(-)
target-arm/kvm64.c +0 -0
index ba16821..924b423 100644

target-arm/kvm64.c int kvm_arch_init_vcpu(CPUState *cs) · line 85

Quoted 7 lines
target-arm/kvm64.c +1 -2
@@ -81,8 +81,7 @@ int kvm_arch_init_vcpu(CPUState *cs)
     int ret;
     ARMCPU *cpu = ARM_CPU(cs);
 
-    if (cpu->kvm_target == QEMU_KVM_ARM_TARGET_NONE ||
-        !arm_feature(&cpu->env, ARM_FEATURE_AARCH64)) {
+    if (cpu->kvm_target == QEMU_KVM_ARM_TARGET_NONE) {
         fprintf(stderr, "KVM is not supported for this guest CPU type\n");

This change will let you try to run with KVM enabled and -cpu cortex-a15 on a 64-bit host kernel, which is not right. Worse, because the kernel's KVM_ARM_TARGET_CORTEX_A15 and KVM_ARM_TARGET_AEM_V8 are the same value the VCPU init ioctl may succeed but instantiate the wrong kind of CPU. It probably needs to turn into a check whether cpu is a subclass of TYPE_AARCH64_CPU.

Quoted 40 lines
target-arm/kvm64.c +15 -1
         return -EINVAL;
     }
@@ -96,6 +95,9 @@ int kvm_arch_init_vcpu(CPUState *cs)
         cpu->psci_version = 2;
         cpu->kvm_init_features[0] |= 1 << KVM_ARM_VCPU_PSCI_0_2;
     }
+    if (!arm_feature(&cpu->env, ARM_FEATURE_AARCH64)) {
+        cpu->kvm_init_features[0] |= 1 << KVM_ARM_VCPU_EL1_32BIT;
+    }
 
     /* Do KVM_ARM_VCPU_INIT ioctl */
     ret = kvm_arm_vcpu_init(cs);
@@ -133,6 +135,13 @@ int kvm_arch_put_registers(CPUState *cs, int level)
     ARMCPU *cpu = ARM_CPU(cs);
     CPUARMState *env = &cpu->env;
 
+    /* If we are in AArch32 mode then we need to sync the AArch64 regs with the
+     * AArch32 regs before pushing them out 64-bit KVM.
+     */
+    if (!is_a64(env)) {
+        aarch64_sync_32_to_64(env);
+    }
+
     for (i = 0; i < 31; i++) {
         reg.id = AARCH64_CORE_REG(regs.regs[i]);
         reg.addr = (uintptr_t) &env->xregs[i];
@@ -162,7 +171,11 @@ int kvm_arch_put_registers(CPUState *cs, int level)
     }
 
     /* Note that KVM thinks pstate is 64 bit but we use a uint32_t */
-    val = pstate_read(env);
+    if (is_a64(env)) {
+        val = pstate_read(env);
+    } else {
+        val = cpsr_read(env);
+    }
     reg.id = AARCH64_CORE_REG(regs.pstate);
     reg.addr = (uintptr_t) &val;
     ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);

target-arm/kvm64.c int kvm_arch_get_registers(CPUState *cs) · line 240

Quoted 9 lines
target-arm/kvm64.c +6 -0
@@ -219,6 +232,13 @@ int kvm_arch_get_registers(CPUState *cs)
         }
     }
 
+    /* If we are in AArch32 mode then we need to sync the AArch32 regs with the
+     * incoming AArch64 regs received from 64-bit KVM.
+     */
+    if (!is_a64(env)) {
+        aarch64_sync_64_to_32(env);
+    }

This is happening too early, for two reasons:
(1) this is_a64() call needs to operate on the state we read from the kernel, and we don't read the PSTATE from the kernel til later
(2) aarch64_sync_64_to_32() reads env->pc, which again we haven't read from the kernel yet

target-arm/kvm64.c int kvm_arch_get_registers(CPUState *cs) · line 262

Quoted 10 lines
target-arm/kvm64.c +2 -1
+
     reg.id = AARCH64_CORE_REG(regs.sp);
     reg.addr = (uintptr_t) &env->sp_el[0];
     ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
@@ -239,7 +259,12 @@ int kvm_arch_get_registers(CPUState *cs)
     if (ret) {
         return ret;
     }
-    pstate_write(env, val);
+    if (is_a64(env)) {

The value currently in env->aarch64 is junk left over from the last time execution stopped. What you need to do first is
env->aarch64 = ((val & PSTATE_nRW) == 0);

(compare target-arm/machine.c:get_cpsr()).

target-arm/kvm64.c int kvm_arch_get_registers(CPUState *cs) · line 270

Quoted 8 lines
target-arm/kvm64.c +5 -0
+        pstate_write(env, val);
+    } else {
+        env->uncached_cpsr = val & CPSR_M;
+        cpsr_write(env, val, 0xffffffff);
+    }
 
     /* KVM puts SP_EL0 in regs.sp and SP_EL1 in regs.sp_el1. On the
      * QEMU side we keep the current SP in xregs[31] as well.

thanks
-- PMM

Tested-by on the v3 series
#16 lore raw
In reply to Greg Bellows
Quoted 8 lines · Greg Bellows

Added support for running an AArch32 guest on a AArch64 KVM host. Support has only been added to the QEMU machvirt machine. The addition of CPU properties specifiable from the command line were added to allow disablement of AArch64 execution state hereby forcing EL1 to be AArch32. The new CPU command line property is "aarch64=on/off" that is specified as follows:

    aarch64-softmmu/qemu-system-aarch64 -M virt -cpu cortex-a57,aarch64=off ...