Skip to contentSkip to conversation

ARM: add PSCI emulation support

PATCH In review Rob Herring 3 participants 17 messages May 23, 2014 – Jun 4, 2014

lore
Share & tools
lore
#0 lore raw

v2 cover ARM: add PSCI emulation support

From: Rob Herring <[email protected]>

This series adds support for emulating ARM PSCI calls. PSCI or Power State Coordination Interface is an ARM standard for controlling cpu power states. This series supports both AArch32 and AArch64 using HVC or SMC calls.

This is based on version 6 of Pranavkumar Sawargaonkar's series for PSCI 0.2 support in KVM[1].

Rob

[1] http://lists.gnu.org/archive/html/qemu-devel/2014-05/msg04514.html

Rob Herring (6):
target-arm: don't set cpu do_interrupt handler for user mode emulation
target-arm: add powered off cpu state
target-arm: add hvc and smc exception emulation handling
infrastructure
target-arm: add emulation of PSCI calls for system emulation arm/virt: enable PSCI emulation support for system emulation arm/highbank: enable PSCI emulation support

 hw/arm/highbank.c          |   9 +++
 hw/arm/virt.c              |  41 +++++-------
 target-arm/Makefile.objs   |   2 +-
 target-arm/cpu-qom.h       |  11 ++++
 target-arm/cpu.c           |  10 ++-
 target-arm/cpu.h           |   2 +
 target-arm/cpu64.c         |   2 +
 target-arm/helper-a64.c    |  35 ++++++++--
 target-arm/helper.c        |  53 +++++++++++++--
 target-arm/internals.h     |  20 ++++++
 target-arm/kvm-consts.h    |   6 ++
 target-arm/machine.c       |   5 +-
 target-arm/psci.c          | 157 +++++++++++++++++++++++++++++++++++++++++++++
 target-arm/translate-a64.c |  13 +++-
 target-arm/translate.c     |  24 +++++--
 15 files changed, 340 insertions(+), 50 deletions(-)
 create mode 100644 target-arm/psci.c
Signature · 1 line
1.9.1
#1 lore raw

PATCH v2 1/6 target-arm: don't set cpu do_interrupt handler for user mode emulation

From: Rob Herring <[email protected]>

In preparation to add system mode only calls to aarch64_cpu_do_interrupt, compile it for system mode only and don't set the do_interrupt callback for user mode emulation. User mode emulation should never get interrupts and thus should not have a exception handler function. Do the same change from AArch32 to keep them aligned.

---
v2:
- Only set .do_interrupt function for system emulation on A32 and A64.

 target-arm/cpu.c        | 2 +-
 target-arm/cpu64.c      | 2 ++
 target-arm/helper-a64.c | 3 +++
 target-arm/helper.c     | 5 -----
 4 files changed, 6 insertions(+), 6 deletions(-)
target-arm/cpu.c +1 -1
index 589f34d..f46b375 100644
@@ -1038,7 +1038,6 @@ static void arm_cpu_class_init(ObjectClass *oc, void *data)
 
     cc->class_by_name = arm_cpu_class_by_name;
     cc->has_work = arm_cpu_has_work;
-    cc->do_interrupt = arm_cpu_do_interrupt;
     cc->dump_state = arm_cpu_dump_state;
     cc->set_pc = arm_cpu_set_pc;
     cc->gdb_read_register = arm_cpu_gdb_read_register;
@@ -1046,6 +1045,7 @@ static void arm_cpu_class_init(ObjectClass *oc, void *data)
 #ifdef CONFIG_USER_ONLY
     cc->handle_mmu_fault = arm_cpu_handle_mmu_fault;
 #else
+    cc->do_interrupt = arm_cpu_do_interrupt;
     cc->get_phys_page_debug = arm_cpu_get_phys_page_debug;
     cc->vmsd = &vmstate_arm_cpu;
 #endif
target-arm/cpu64.c +2 -0
index 8daa622..b155579 100644
@@ -187,7 +187,9 @@ static void aarch64_cpu_class_init(ObjectClass *oc, void *data)
 {
     CPUClass *cc = CPU_CLASS(oc);
 
+#if !defined(CONFIG_USER_ONLY)
     cc->do_interrupt = aarch64_cpu_do_interrupt;
+#endif
     cc->set_pc = aarch64_cpu_set_pc;
     cc->gdb_read_register = aarch64_cpu_gdb_read_register;
     cc->gdb_write_register = aarch64_cpu_gdb_write_register;
target-arm/helper-a64.c +3 -0
index bf921cc..84411b4 100644
@@ -438,6 +438,8 @@ float32 HELPER(fcvtx_f64_to_f32)(float64 a, CPUARMState *env)
     return r;
 }
 
+#if !defined(CONFIG_USER_ONLY)
+
 /* Handle a CPU exception.  */
 void aarch64_cpu_do_interrupt(CPUState *cs)
 {
@@ -512,3 +514,4 @@ void aarch64_cpu_do_interrupt(CPUState *cs)
     env->pc = addr;
     cs->interrupt_request |= CPU_INTERRUPT_EXITTB;
 }
+#endif
target-arm/helper.c +0 -5
index 417161e..1307473 100644
@@ -3004,11 +3004,6 @@ uint32_t HELPER(rbit)(uint32_t x)
 
 #if defined(CONFIG_USER_ONLY)
 
-void arm_cpu_do_interrupt(CPUState *cs)
-{
-    cs->exception_index = -1;
-}
-
 int arm_cpu_handle_mmu_fault(CPUState *cs, vaddr address, int rw,
                              int mmu_idx)
 {

--
1.9.1

Reviewed-by on v2 1/6
#2 lore raw

Re: [Qemu-devel] [PATCH v2 1/6] target-arm: don't set cpu do_interrupt handler for user mode emulation

Quoted 9 lines · Rob Herring

From: Rob Herring <[email protected]>

In preparation to add system mode only calls to aarch64_cpu_do_interrupt, compile it for system mode only and don't set the do_interrupt callback for user mode emulation. User mode emulation should never get interrupts and thus should not have a exception handler function. Do the same change from AArch32 to keep them aligned.

-- PMM

#3 lore raw

PATCH v2 2/6 target-arm: add powered off cpu state

In reply to Rob Herring

From: Rob Herring <[email protected]>

Add tracking of cpu power state in order to support powering off of cores in system emulation. The initial state is determined by the start-powered-off QOM property.

---
v2:
- Add vmstate for powered_off

 target-arm/cpu-qom.h | 2 ++
 target-arm/cpu.c     | 7 ++++++-
 target-arm/machine.c | 5 +++--
 3 files changed, 11 insertions(+), 3 deletions(-)
target-arm/cpu-qom.h +2 -0
index eaee944..f835900 100644
@@ -102,6 +102,8 @@ typedef struct ARMCPU {
 
     /* Should CPU start in PSCI powered-off state? */
     bool start_powered_off;
+    /* CPU currently in PSCI powered-off state */
+    bool powered_off;
 
     /* [QEMU_]KVM_ARM_TARGET_* constant for this CPU, or
      * QEMU_KVM_ARM_TARGET_NONE if the kernel doesn't support this CPU type.
target-arm/cpu.c +6 -1
index f46b375..a0f9916 100644
@@ -40,7 +40,9 @@ static void arm_cpu_set_pc(CPUState *cs, vaddr value)
 
 static bool arm_cpu_has_work(CPUState *cs)
 {
-    return cs->interrupt_request &
+    ARMCPU *cpu = ARM_CPU(cs);
+
+    return !cpu->powered_off && cs->interrupt_request &
         (CPU_INTERRUPT_FIQ | CPU_INTERRUPT_HARD | CPU_INTERRUPT_EXITTB);
 }
 
@@ -91,6 +93,9 @@ static void arm_cpu_reset(CPUState *s)
     env->vfp.xregs[ARM_VFP_MVFR1] = cpu->mvfr1;
     env->vfp.xregs[ARM_VFP_MVFR2] = cpu->mvfr2;
 
+    cpu->powered_off = cpu->start_powered_off;
+    s->halted = cpu->start_powered_off;
+
     if (arm_feature(env, ARM_FEATURE_IWMMXT)) {
         env->iwmmxt.cregs[ARM_IWMMXT_wCID] = 0x69051000 | 'Q';
     }
target-arm/machine.c +3 -2
index 5092dcd..c8aa1d6 100644
@@ -218,8 +218,8 @@ static int cpu_post_load(void *opaque, int version_id)
 
 const VMStateDescription vmstate_arm_cpu = {
     .name = "cpu",
-    .version_id = 17,
-    .minimum_version_id = 17,
+    .version_id = 18,
+    .minimum_version_id = 18,
     .pre_save = cpu_pre_save,
     .post_load = cpu_post_load,
     .fields = (VMStateField[]) {
@@ -259,6 +259,7 @@ const VMStateDescription vmstate_arm_cpu = {
         VMSTATE_UINT64(env.exception.vaddress, ARMCPU),
         VMSTATE_TIMER(gt_timer[GTIMER_PHYS], ARMCPU),
         VMSTATE_TIMER(gt_timer[GTIMER_VIRT], ARMCPU),
+        VMSTATE_BOOL(powered_off, ARMCPU),
         VMSTATE_END_OF_LIST()
     },
     .subsections = (VMStateSubsection[]) {

--
1.9.1

#5 lore raw

PATCH v2 3/6 target-arm: add hvc and smc exception emulation handling infrastructure

In reply to Rob Herring

From: Rob Herring <[email protected]>

Add the infrastructure to handle and emulate hvc and smc exceptions. This will enable emulation of things such as PSCI calls. This commit does not change the behavior and will exit with unknown exception.

---
v2:
- add syn_aa32_smc
- add missing syndrome calls
- properly handle unhandled SMC/HVC emulation as undefined instruction exception
- Move PSCI bits in arm_cpu_do_hvc/smc to next patch
- fix immediate value for thumb hvc

 target-arm/cpu-qom.h       |  3 +++
 target-arm/cpu.h           |  2 ++
 target-arm/helper-a64.c    | 32 ++++++++++++++++++++++++++++----
 target-arm/helper.c        | 30 ++++++++++++++++++++++++++++++
 target-arm/internals.h     | 20 ++++++++++++++++++++
 target-arm/translate-a64.c | 13 ++++++++++---
 target-arm/translate.c     | 24 +++++++++++++++++-------
 7 files changed, 110 insertions(+), 14 deletions(-)
target-arm/cpu-qom.h +3 -0
index f835900..d2ff087 100644
@@ -195,6 +195,9 @@ extern const struct VMStateDescription vmstate_arm_cpu;
 void register_cp_regs_for_features(ARMCPU *cpu);
 void init_cpreg_list(ARMCPU *cpu);
 
+bool arm_cpu_do_hvc(CPUState *cs);
+bool arm_cpu_do_smc(CPUState *cs);
+
 void arm_cpu_do_interrupt(CPUState *cpu);
 void arm_v7m_cpu_do_interrupt(CPUState *cpu);
 
target-arm/cpu.h +2 -0
index c83f249..905ba02 100644
@@ -51,6 +51,8 @@
 #define EXCP_EXCEPTION_EXIT  8   /* Return from v7M exception.  */
 #define EXCP_KERNEL_TRAP     9   /* Jumped to kernel code page.  */
 #define EXCP_STREX          10
+#define EXCP_HVC            11
+#define EXCP_SMC            12
 
 #define ARMV7M_EXCP_RESET   1
 #define ARMV7M_EXCP_NMI     2
target-arm/helper-a64.c +28 -4
index 84411b4..e3dcca8 100644
@@ -466,14 +466,11 @@ void aarch64_cpu_do_interrupt(CPUState *cs)
                       env->exception.syndrome);
     }
 
-    env->cp15.esr_el1 = env->exception.syndrome;
-    env->cp15.far_el1 = env->exception.vaddress;
-
     switch (cs->exception_index) {
     case EXCP_PREFETCH_ABORT:
     case EXCP_DATA_ABORT:
         qemu_log_mask(CPU_LOG_INT, "...with FAR 0x%" PRIx64 "\n",
-                      env->cp15.far_el1);
+                      env->exception.vaddress);
         break;
     case EXCP_BKPT:
     case EXCP_UDEF:
@@ -485,10 +482,37 @@ void aarch64_cpu_do_interrupt(CPUState *cs)
     case EXCP_FIQ:
         addr += 0x100;
         break;
+    case EXCP_HVC:
+        if (arm_cpu_do_hvc(cs)) {
+            return;
+        }
+        qemu_log_mask(LOG_GUEST_ERROR, "Unhandled HVC exception\n");
+        env->exception.syndrome = syn_uncategorized();
+        if (is_a64(env)) {
+            env->pc -= 4;
+        } else {
+            env->regs[15] -= 4;
+        }
+        break;
+    case EXCP_SMC:
+        if (arm_cpu_do_smc(cs)) {
+            return;
+        }
+        qemu_log_mask(LOG_GUEST_ERROR, "Unhandled SMC exception\n");
+        env->exception.syndrome = syn_uncategorized();
+        if (is_a64(env)) {
+            env->pc -= 4;
+        } else {
+            env->regs[15] -= 4;
+        }
+        break;
     default:
         cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
     }
 
+    env->cp15.esr_el1 = env->exception.syndrome;
+    env->cp15.far_el1 = env->exception.vaddress;
+
     if (is_a64(env)) {
         env->banked_spsr[0] = pstate_read(env);
         env->sp_el[arm_current_pl(env)] = env->xregs[31];
target-arm/helper.c +30 -0
index 1307473..552e601 100644
@@ -3255,6 +3255,16 @@ void arm_v7m_cpu_do_interrupt(CPUState *cs)
     env->thumb = addr & 1;
 }
 
+bool arm_cpu_do_hvc(CPUState *cs)
+{
+    return false;
+}
+
+bool arm_cpu_do_smc(CPUState *cs)
+{
+    return false;
+}
+
 /* Handle a CPU exception.  */
 void arm_cpu_do_interrupt(CPUState *cs)
 {
@@ -3357,6 +3367,26 @@ void arm_cpu_do_interrupt(CPUState *cs)
         mask = CPSR_A | CPSR_I | CPSR_F;
         offset = 4;
         break;
+    case EXCP_HVC:
+        if (arm_cpu_do_hvc(cs)) {
+            return;
+        }
+        qemu_log_mask(LOG_GUEST_ERROR, "Unhandled HVC exception\n");
+        new_mode = ARM_CPU_MODE_UND;
+        addr = 0x04;
+        mask = CPSR_A | CPSR_I;
+        offset = 4;
+       break;
+    case EXCP_SMC:
+        if (arm_cpu_do_smc(cs)) {
+            return;
+        }
+        qemu_log_mask(LOG_GUEST_ERROR, "Unhandled SMC exception\n");
+        new_mode = ARM_CPU_MODE_UND;
+        addr = 0x04;
+        mask = CPSR_A | CPSR_I;
+        offset = 4;
+        break;
     default:
         cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
         return; /* Never happens.  Keep compiler happy.  */
target-arm/internals.h +20 -0
index d63a975..52a284f 100644
@@ -184,6 +184,26 @@ static inline uint32_t syn_aa32_svc(uint32_t imm16, bool is_thumb)
         | (is_thumb ? 0 : ARM_EL_IL);
 }
 
+static inline uint32_t syn_aa64_hvc(uint32_t imm16)
+{
+    return (EC_AA64_HVC << ARM_EL_EC_SHIFT) | ARM_EL_IL | (imm16 & 0xffff);
+}
+
+static inline uint32_t syn_aa32_hvc(uint32_t imm16)
+{
+    return (EC_AA32_HVC << ARM_EL_EC_SHIFT) | ARM_EL_IL | (imm16 & 0xffff);
+}
+
+static inline uint32_t syn_aa64_smc(uint32_t imm16)
+{
+    return (EC_AA64_SMC << ARM_EL_EC_SHIFT) | ARM_EL_IL | (imm16 & 0xffff);
+}
+
+static inline uint32_t syn_aa32_smc(void)
+{
+    return (EC_AA32_SMC << ARM_EL_EC_SHIFT) | ARM_EL_IL;
+}
+
 static inline uint32_t syn_aa64_bkpt(uint32_t imm16)
 {
     return (EC_AA64_BKPT << ARM_EL_EC_SHIFT) | ARM_EL_IL | (imm16 & 0xffff);
target-arm/translate-a64.c +10 -3
index b62db4d..d5e3624 100644
@@ -1449,11 +1449,18 @@ static void disas_exc(DisasContext *s, uint32_t insn)
         /* SVC, HVC, SMC; since we don't support the Virtualization
          * or TrustZone extensions these all UNDEF except SVC.
          */
-        if (op2_ll != 1) {
-            unallocated_encoding(s);
+        switch (op2_ll) {
+        case 1:
+            gen_exception_insn(s, 0, EXCP_SWI, syn_aa64_svc(imm16));
+            break;
+        case 2:
+            gen_exception_insn(s, 0, EXCP_HVC, syn_aa64_hvc(imm16));
+            break;
+        case 3:
+            gen_exception_insn(s, 0, EXCP_SMC, syn_aa64_smc(imm16));
             break;
         }
-        gen_exception_insn(s, 0, EXCP_SWI, syn_aa64_svc(imm16));
+        unallocated_encoding(s);
         break;
     case 1:
         if (op2_ll != 0) {
target-arm/translate.c +17 -7
index a4d920b..c108bc7 100644
@@ -7727,9 +7727,14 @@ static void disas_arm_insn(CPUARMState * env, DisasContext *s)
         case 7:
         {
             int imm16 = extract32(insn, 0, 4) | (extract32(insn, 8, 12) << 4);
-            /* SMC instruction (op1 == 3)
-               and undefined instructions (op1 == 0 || op1 == 2)
-               will trap */
+            /* HVC and SMC instructions */
+            if (op1 == 2) {
+                gen_exception_insn(s, 0, EXCP_HVC, syn_aa32_hvc(imm16));
+                break;
+            } else if (op1 == 3) {
+                gen_exception_insn(s, 0, EXCP_SMC, syn_aa32_smc());
+                break;
+            }
             if (op1 != 1) {
                 goto illegal_op;
             }
@@ -9555,10 +9560,15 @@ static int disas_thumb2_insn(CPUARMState *env, DisasContext *s, uint16_t insn_hw
                     goto illegal_op;
 
                 if (insn & (1 << 26)) {
-                    /* Secure monitor call (v6Z) */
-                    qemu_log_mask(LOG_UNIMP,
-                                  "arm: unimplemented secure monitor call\n");
-                    goto illegal_op; /* not implemented.  */
+                    if (!(insn & (1 << 20))) {
+                        /* Hypervisor call (v7) */
+                        uint32_t imm16 = extract32(insn, 0, 12);
+                        imm16 |= extract32(insn, 16, 4) << 12;
+                        gen_exception_insn(s, 0, EXCP_HVC, syn_aa32_hvc(imm16));
+                    } else {
+                        /* Secure monitor call (v6+) */
+                        gen_exception_insn(s, 0, EXCP_SMC, syn_aa32_smc());
+                    }
                 } else {
                     op = (insn >> 20) & 7;
                     switch (op) {

--
1.9.1

#6 lore raw

Re: [Qemu-devel] [PATCH v2 3/6] target-arm: add hvc and smc exception emulation handling infrastructure

Quoted 5 lines · Rob Herring

From: Rob Herring <[email protected]>

Add the infrastructure to handle and emulate hvc and smc exceptions. This will enable emulation of things such as PSCI calls. This commit does not change the behavior and will exit with unknown exception.

Hi Rob,

I've got some conflicting work but am happy to rebase on top of this. A few comments inline.

Quoted 39 lines

---
v2:
- add syn_aa32_smc
- add missing syndrome calls
- properly handle unhandled SMC/HVC emulation as undefined instruction exception
- Move PSCI bits in arm_cpu_do_hvc/smc to next patch
- fix immediate value for thumb hvc

 target-arm/cpu-qom.h       |  3 +++
 target-arm/cpu.h           |  2 ++
 target-arm/helper-a64.c    | 32 ++++++++++++++++++++++++++++----
 target-arm/helper.c        | 30 ++++++++++++++++++++++++++++++
 target-arm/internals.h     | 20 ++++++++++++++++++++
 target-arm/translate-a64.c | 13 ++++++++++---
 target-arm/translate.c     | 24 +++++++++++++++++-------
 7 files changed, 110 insertions(+), 14 deletions(-)
target-arm/cpu-qom.h +3 -0
index f835900..d2ff087 100644
@@ -195,6 +195,9 @@ extern const struct VMStateDescription vmstate_arm_cpu;
 void register_cp_regs_for_features(ARMCPU *cpu);
 void init_cpreg_list(ARMCPU *cpu);
 
+bool arm_cpu_do_hvc(CPUState *cs);
+bool arm_cpu_do_smc(CPUState *cs);
+
 void arm_cpu_do_interrupt(CPUState *cpu);
 void arm_v7m_cpu_do_interrupt(CPUState *cpu);
 
target-arm/cpu.h +0 -0
index c83f249..905ba02 100644

target-arm/cpu.h line 55

Quoted 5 lines
target-arm/cpu.h +2 -0
@@ -51,6 +51,8 @@
 #define EXCP_EXCEPTION_EXIT  8   /* Return from v7M exception.  */
 #define EXCP_KERNEL_TRAP     9   /* Jumped to kernel code page.  */
 #define EXCP_STREX          10
+#define EXCP_HVC            11
+#define EXCP_SMC            12

Can you please also populate excnames[] in internals.h?

Quoted 46 lines
target-arm/cpu.h +0 -0
 #define ARMV7M_EXCP_RESET   1
 #define ARMV7M_EXCP_NMI     2
target-arm/helper-a64.c +21 -4
index 84411b4..e3dcca8 100644
@@ -466,14 +466,11 @@ void aarch64_cpu_do_interrupt(CPUState *cs)
                       env->exception.syndrome);
     }
 
-    env->cp15.esr_el1 = env->exception.syndrome;
-    env->cp15.far_el1 = env->exception.vaddress;
-
     switch (cs->exception_index) {
     case EXCP_PREFETCH_ABORT:
     case EXCP_DATA_ABORT:
         qemu_log_mask(CPU_LOG_INT, "...with FAR 0x%" PRIx64 "\n",
-                      env->cp15.far_el1);
+                      env->exception.vaddress);
         break;
     case EXCP_BKPT:
     case EXCP_UDEF:
@@ -485,10 +482,37 @@ void aarch64_cpu_do_interrupt(CPUState *cs)
     case EXCP_FIQ:
         addr += 0x100;
         break;
+    case EXCP_HVC:
+        if (arm_cpu_do_hvc(cs)) {
+            return;
+        }
+        qemu_log_mask(LOG_GUEST_ERROR, "Unhandled HVC exception\n");
+        env->exception.syndrome = syn_uncategorized();
+        if (is_a64(env)) {
+            env->pc -= 4;
+        } else {
+            env->regs[15] -= 4;
+        }
+        break;
+    case EXCP_SMC:
+        if (arm_cpu_do_smc(cs)) {
+            return;
+        }
+        qemu_log_mask(LOG_GUEST_ERROR, "Unhandled SMC exception\n");
+        env->exception.syndrome = syn_uncategorized();
+        if (is_a64(env)) {
+            env->pc -= 4;

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

Quoted 10 lines
target-arm/helper-a64.c +6 -0
+        } else {
+            env->regs[15] -= 4;
+        }
+        break;
     default:
         cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
     }
 
+    env->cp15.esr_el1 = env->exception.syndrome;
+    env->cp15.far_el1 = env->exception.vaddress;

I've had to change the unconditional updates of ESR and FAR, not all exception kinds update both or any. Don't know if you want to fix up all the combos in this series but SMC and HVC should not update FAR AFAICT.

Cheers,
Edgar

Quoted 155 lines
target-arm/helper-a64.c +1 -0
+
     if (is_a64(env)) {
         env->banked_spsr[0] = pstate_read(env);
         env->sp_el[arm_current_pl(env)] = env->xregs[31];
target-arm/helper.c +30 -0
index 1307473..552e601 100644
@@ -3255,6 +3255,16 @@ void arm_v7m_cpu_do_interrupt(CPUState *cs)
     env->thumb = addr & 1;
 }
 
+bool arm_cpu_do_hvc(CPUState *cs)
+{
+    return false;
+}
+
+bool arm_cpu_do_smc(CPUState *cs)
+{
+    return false;
+}
+
 /* Handle a CPU exception.  */
 void arm_cpu_do_interrupt(CPUState *cs)
 {
@@ -3357,6 +3367,26 @@ void arm_cpu_do_interrupt(CPUState *cs)
         mask = CPSR_A | CPSR_I | CPSR_F;
         offset = 4;
         break;
+    case EXCP_HVC:
+        if (arm_cpu_do_hvc(cs)) {
+            return;
+        }
+        qemu_log_mask(LOG_GUEST_ERROR, "Unhandled HVC exception\n");
+        new_mode = ARM_CPU_MODE_UND;
+        addr = 0x04;
+        mask = CPSR_A | CPSR_I;
+        offset = 4;
+       break;
+    case EXCP_SMC:
+        if (arm_cpu_do_smc(cs)) {
+            return;
+        }
+        qemu_log_mask(LOG_GUEST_ERROR, "Unhandled SMC exception\n");
+        new_mode = ARM_CPU_MODE_UND;
+        addr = 0x04;
+        mask = CPSR_A | CPSR_I;
+        offset = 4;
+        break;
     default:
         cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
         return; /* Never happens.  Keep compiler happy.  */
target-arm/internals.h +20 -0
index d63a975..52a284f 100644
@@ -184,6 +184,26 @@ static inline uint32_t syn_aa32_svc(uint32_t imm16, bool is_thumb)
         | (is_thumb ? 0 : ARM_EL_IL);
 }
 
+static inline uint32_t syn_aa64_hvc(uint32_t imm16)
+{
+    return (EC_AA64_HVC << ARM_EL_EC_SHIFT) | ARM_EL_IL | (imm16 & 0xffff);
+}
+
+static inline uint32_t syn_aa32_hvc(uint32_t imm16)
+{
+    return (EC_AA32_HVC << ARM_EL_EC_SHIFT) | ARM_EL_IL | (imm16 & 0xffff);
+}
+
+static inline uint32_t syn_aa64_smc(uint32_t imm16)
+{
+    return (EC_AA64_SMC << ARM_EL_EC_SHIFT) | ARM_EL_IL | (imm16 & 0xffff);
+}
+
+static inline uint32_t syn_aa32_smc(void)
+{
+    return (EC_AA32_SMC << ARM_EL_EC_SHIFT) | ARM_EL_IL;
+}
+
 static inline uint32_t syn_aa64_bkpt(uint32_t imm16)
 {
     return (EC_AA64_BKPT << ARM_EL_EC_SHIFT) | ARM_EL_IL | (imm16 & 0xffff);
target-arm/translate-a64.c +10 -3
index b62db4d..d5e3624 100644
@@ -1449,11 +1449,18 @@ static void disas_exc(DisasContext *s, uint32_t insn)
         /* SVC, HVC, SMC; since we don't support the Virtualization
          * or TrustZone extensions these all UNDEF except SVC.
          */
-        if (op2_ll != 1) {
-            unallocated_encoding(s);
+        switch (op2_ll) {
+        case 1:
+            gen_exception_insn(s, 0, EXCP_SWI, syn_aa64_svc(imm16));
+            break;
+        case 2:
+            gen_exception_insn(s, 0, EXCP_HVC, syn_aa64_hvc(imm16));
+            break;
+        case 3:
+            gen_exception_insn(s, 0, EXCP_SMC, syn_aa64_smc(imm16));
             break;
         }
-        gen_exception_insn(s, 0, EXCP_SWI, syn_aa64_svc(imm16));
+        unallocated_encoding(s);
         break;
     case 1:
         if (op2_ll != 0) {
target-arm/translate.c +17 -8
index a4d920b..c108bc7 100644
@@ -7727,9 +7727,14 @@ static void disas_arm_insn(CPUARMState * env, DisasContext *s)
         case 7:
         {
             int imm16 = extract32(insn, 0, 4) | (extract32(insn, 8, 12) << 4);
-            /* SMC instruction (op1 == 3)
-               and undefined instructions (op1 == 0 || op1 == 2)
-               will trap */
+            /* HVC and SMC instructions */
+            if (op1 == 2) {
+                gen_exception_insn(s, 0, EXCP_HVC, syn_aa32_hvc(imm16));
+                break;
+            } else if (op1 == 3) {
+                gen_exception_insn(s, 0, EXCP_SMC, syn_aa32_smc());
+                break;
+            }
             if (op1 != 1) {
                 goto illegal_op;
             }
@@ -9555,10 +9560,15 @@ static int disas_thumb2_insn(CPUARMState *env, DisasContext *s, uint16_t insn_hw
                     goto illegal_op;
 
                 if (insn & (1 << 26)) {
-                    /* Secure monitor call (v6Z) */
-                    qemu_log_mask(LOG_UNIMP,
-                                  "arm: unimplemented secure monitor call\n");
-                    goto illegal_op; /* not implemented.  */
+                    if (!(insn & (1 << 20))) {
+                        /* Hypervisor call (v7) */
+                        uint32_t imm16 = extract32(insn, 0, 12);
+                        imm16 |= extract32(insn, 16, 4) << 12;
+                        gen_exception_insn(s, 0, EXCP_HVC, syn_aa32_hvc(imm16));
+                    } else {
+                        /* Secure monitor call (v6+) */
+                        gen_exception_insn(s, 0, EXCP_SMC, syn_aa32_smc());
+                    }
                 } else {
                     op = (insn >> 20) & 7;
                     switch (op) {
--

1.9.1

#7 lore raw

Re: [Qemu-devel] [PATCH v2 3/6] target-arm: add hvc and smc exception emulation handling infrastructure

Quoted 11 lines · Edgar E. Iglesias
Quoted 5 lines · Rob Herring

From: Rob Herring <[email protected]>

Add the infrastructure to handle and emulate hvc and smc exceptions. This will enable emulation of things such as PSCI calls. This commit does not change the behavior and will exit with unknown exception.

Hi Rob,

I've got some conflicting work but am happy to rebase on top of this. A few comments inline.

Since this series is dependent on the PSCI patchset which includes a header update to headers which haven't hit the kernel kvm-next branch yet, I'm currently expecting that your initial EL2/EL3 patchset will get in first and this will be the set that needs to rebase...

thanks
-- PMM

Reviewed-by on v2 3/6
#8 lore raw

Re: [Qemu-devel] [PATCH v2 3/6] target-arm: add hvc and smc exception emulation handling infrastructure

In reply to Rob Herring
Quoted 7 lines · Rob Herring

From: Rob Herring <[email protected]>

Add the infrastructure to handle and emulate hvc and smc exceptions. This will enable emulation of things such as PSCI calls. This commit does not change the behavior and will exit with unknown exception.

(As Edgar notes, there is probably some conflict resolution work required after rebasing.)

thanks
-- PMM

#9 lore raw

PATCH v2 4/6 target-arm: add emulation of PSCI calls for system emulation

In reply to Rob Herring

From: Rob Herring <[email protected]>

Add support for handling PSCI calls in system emulation. Both version 0.1 and 0.2 of the PSCI spec are supported. Platforms can enable support by setting "psci-method" QOM property on the cpus to SMC or HVC emulation and having PSCI binding in their dtb.

---
v2:
- Add reference to PSCI spec
- Add target_cs instead of cs when doing operations on a different core.
- Move arm_cpu_do_hvc/smc contents from previous patch.
- Set context_id on both x0 and r0.

 target-arm/Makefile.objs |   2 +-
 target-arm/cpu-qom.h     |   6 ++
 target-arm/cpu.c         |   1 +
 target-arm/helper.c      |  12 ++++
 target-arm/kvm-consts.h  |   6 ++
 target-arm/psci.c        | 157 +++++++++++++++++++++++++++++++++++++++++++++++
 6 files changed, 183 insertions(+), 1 deletion(-)
 create mode 100644 target-arm/psci.c
target-arm/Makefile.objs +1 -1
index dcd167e..50f6c43 100644
@@ -1,5 +1,5 @@
 obj-y += arm-semi.o
-obj-$(CONFIG_SOFTMMU) += machine.o
+obj-$(CONFIG_SOFTMMU) += machine.o psci.o
 obj-$(CONFIG_KVM) += kvm.o
 obj-$(call land,$(CONFIG_KVM),$(call lnot,$(TARGET_AARCH64))) += kvm32.o
 obj-$(call land,$(CONFIG_KVM),$(TARGET_AARCH64)) += kvm64.o
target-arm/cpu-qom.h +6 -0
index d2ff087..56999f1 100644
@@ -105,6 +105,11 @@ typedef struct ARMCPU {
     /* CPU currently in PSCI powered-off state */
     bool powered_off;
 
+    /* PSCI emulation state
+     * 0 - disabled, 1 - smc, 2 - hvc
+     */
+    uint32_t psci_method;
+
     /* [QEMU_]KVM_ARM_TARGET_* constant for this CPU, or
      * QEMU_KVM_ARM_TARGET_NONE if the kernel doesn't support this CPU type.
      */
@@ -195,6 +200,7 @@ extern const struct VMStateDescription vmstate_arm_cpu;
 void register_cp_regs_for_features(ARMCPU *cpu);
 void init_cpreg_list(ARMCPU *cpu);
 
+bool arm_handle_psci(CPUState *cs);
 bool arm_cpu_do_hvc(CPUState *cs);
 bool arm_cpu_do_smc(CPUState *cs);
 
target-arm/cpu.c +1 -0
index a0f9916..64f1157 100644
@@ -1024,6 +1024,7 @@ static const ARMCPUInfo arm_cpus[] = {
 
 static Property arm_cpu_properties[] = {
     DEFINE_PROP_BOOL("start-powered-off", ARMCPU, start_powered_off, false),
+    DEFINE_PROP_UINT32("psci-method", ARMCPU, psci_method, 0),
     DEFINE_PROP_UINT32("midr", ARMCPU, midr, 0),
     DEFINE_PROP_END_OF_LIST()
 };
target-arm/helper.c +12 -0
index 552e601..c1be201 100644
@@ -3257,11 +3257,23 @@ void arm_v7m_cpu_do_interrupt(CPUState *cs)
 
 bool arm_cpu_do_hvc(CPUState *cs)
 {
+    ARMCPU *cpu = ARM_CPU(cs);
+
+    if (cpu->psci_method == QEMU_PSCI_METHOD_HVC) {
+        return arm_handle_psci(cs);
+    }
+
     return false;
 }
 
 bool arm_cpu_do_smc(CPUState *cs)
 {
+    ARMCPU *cpu = ARM_CPU(cs);
+
+    if (cpu->psci_method == QEMU_PSCI_METHOD_SMC) {
+        return arm_handle_psci(cs);
+    }
+
     return false;
 }
 
target-arm/kvm-consts.h +6 -0
index 6009a33..d25b9b8 100644
@@ -50,6 +50,12 @@ MISMATCH_CHECK(PSCI_FN_CPU_OFF, KVM_PSCI_FN_CPU_OFF)
 MISMATCH_CHECK(PSCI_FN_CPU_ON, KVM_PSCI_FN_CPU_ON)
 MISMATCH_CHECK(PSCI_FN_MIGRATE, KVM_PSCI_FN_MIGRATE)
 
+enum {
+    QEMU_PSCI_METHOD_DISABLED = 0,
+    QEMU_PSCI_METHOD_SMC = 1,
+    QEMU_PSCI_METHOD_HVC = 2,
+};
+
 /* Note that KVM uses overlapping values for AArch32 and AArch64
  * target CPU numbers. AArch32 targets:
  */
target-arm/psci.c new file +157 -0

157 lines not on this page · raw message

--
1.9.1

#10 lore raw

Re: [Qemu-devel] [PATCH v2 4/6] target-arm: add emulation of PSCI calls for system emulation

Quoted 25 lines · Rob Herring

From: Rob Herring <[email protected]>

Add support for handling PSCI calls in system emulation. Both version 0.1 and 0.2 of the PSCI spec are supported. Platforms can enable support by setting "psci-method" QOM property on the cpus to SMC or HVC emulation and having PSCI binding in their dtb.

target-arm/psci.c new file +12 -0
new file mode 100644
index 0000000..88af3f9
@@ -0,0 +1,157 @@
+/*
+ * Copyright (C) 2014 - Linaro
+ * Author: Rob Herring <[email protected]>
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the

target-arm/psci.c line 22

Quoted 10 lines
target-arm/psci.c new file +10 -0
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, see <http://www.gnu.org/licenses/>.
+ */
+#include <cpu.h>
+#include <cpu-qom.h>
+#include <kvm-consts.h>
+#include <sysemu/sysemu.h>
+#include <linux/psci.h>

This won't build on non-Linux hosts: you can't assume the linux-headers/ includes are available except within code guarded by CONFIG_KVM.

thanks
-- PMM

#11 lore raw

Re: [Qemu-devel] [PATCH v2 4/6] target-arm: add emulation of PSCI calls for system emulation

Quoted 32 lines · Peter Maydell
Quoted 31 lines · Rob Herring

From: Rob Herring <[email protected]>

Add support for handling PSCI calls in system emulation. Both version 0.1 and 0.2 of the PSCI spec are supported. Platforms can enable support by setting "psci-method" QOM property on the cpus to SMC or HVC emulation and having PSCI binding in their dtb.

target-arm/psci.c new file +18 -0
new file mode 100644
index 0000000..88af3f9
@@ -0,0 +1,157 @@
+/*
+ * Copyright (C) 2014 - Linaro
+ * Author: Rob Herring <[email protected]>
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, see <http://www.gnu.org/licenses/>.
+ */
+#include <cpu.h>
Quoted 8 lines

target-arm/psci.c line 22

Quoted 4 lines
target-arm/psci.c new file +4 -0
+#include <cpu-qom.h>
+#include <kvm-consts.h>
+#include <sysemu/sysemu.h>
+#include <linux/psci.h>

This won't build on non-Linux hosts: you can't assume the linux-headers/ includes are available except within code guarded by CONFIG_KVM.

What do you suggest doing here then. Really, psci.h is OS independent. Can we copy it to somewhere else in the qemu tree?

Rob

#12 lore raw

Re: [Qemu-devel] [PATCH v2 4/6] target-arm: add emulation of PSCI calls for system emulation

Quoted 7 lines · Rob Herring
Quoted 3 lines · Peter Maydell

This won't build on non-Linux hosts: you can't assume the linux-headers/ includes are available except within code guarded by CONFIG_KVM.

What do you suggest doing here then. Really, psci.h is OS independent. Can we copy it to somewhere else in the qemu tree?

Only if you can guarantee that it won't clash with the one that is in the linux-headers/ tree, or the one in /usr/include, ie that it doesn't matter which of those three we pick up. That pretty much means "kernel KVM headers can't ever rely on new PSCI constants being added to psci.h" so that doesn't seem like it's going to work. Otherwise it would need to be copy-and-rename-symbols, same as we do for anything else where we need them outside the KVM code. I agree that's pretty ugly.

thanks
-- PMM

#13 lore raw

PATCH v2 5/6 arm/virt: enable PSCI emulation support for system emulation

In reply to Rob Herring

From: Rob Herring <[email protected]>

Now that we have PSCI emulation, enable it for the virt platform. This simplifies the virt machine a bit now that PSCI and SMP no longer need to be KVM only features.

---
v2:
- Rebased. Mostly a whitespace change removing the kvm_enabled() check.

 hw/arm/virt.c | 41 +++++++++++++++--------------------------
 1 file changed, 15 insertions(+), 26 deletions(-)
hw/arm/virt.c +15 -26
index 442363c..e58a07a 100644
@@ -187,23 +187,19 @@ static void fdt_add_psci_node(const VirtBoardInfo *vbi)
     void *fdt = vbi->fdt;
     ARMCPU *armcpu = ARM_CPU(qemu_get_cpu(0));
 
-    /* No PSCI for TCG yet */
-    if (kvm_enabled()) {
-        qemu_fdt_add_subnode(fdt, "/psci");
-        if (armcpu->psci_version == 2) {
-            const char comp[] = "arm,psci-0.2\0arm,psci";
-            qemu_fdt_setprop(fdt, "/psci", "compatible", comp, sizeof(comp));
-        } else {
-            qemu_fdt_setprop_string(fdt, "/psci", "compatible", "arm,psci");
-        }
-
-        qemu_fdt_setprop_string(fdt, "/psci", "method", "hvc");
-        qemu_fdt_setprop_cell(fdt, "/psci", "cpu_suspend",
-                                  PSCI_FN_CPU_SUSPEND);
-        qemu_fdt_setprop_cell(fdt, "/psci", "cpu_off", PSCI_FN_CPU_OFF);
-        qemu_fdt_setprop_cell(fdt, "/psci", "cpu_on", PSCI_FN_CPU_ON);
-        qemu_fdt_setprop_cell(fdt, "/psci", "migrate", PSCI_FN_MIGRATE);
+    qemu_fdt_add_subnode(fdt, "/psci");
+    if (armcpu->psci_version == 2) {
+        const char comp[] = "arm,psci-0.2\0arm,psci";
+        qemu_fdt_setprop(fdt, "/psci", "compatible", comp, sizeof(comp));
+    } else {
+        qemu_fdt_setprop_string(fdt, "/psci", "compatible", "arm,psci");
     }
+
+    qemu_fdt_setprop_string(fdt, "/psci", "method", "hvc");
+    qemu_fdt_setprop_cell(fdt, "/psci", "cpu_suspend", PSCI_FN_CPU_SUSPEND);
+    qemu_fdt_setprop_cell(fdt, "/psci", "cpu_off", PSCI_FN_CPU_OFF);
+    qemu_fdt_setprop_cell(fdt, "/psci", "cpu_on", PSCI_FN_CPU_ON);
+    qemu_fdt_setprop_cell(fdt, "/psci", "migrate", PSCI_FN_MIGRATE);
 }
 
 static void fdt_add_timer_nodes(const VirtBoardInfo *vbi)
@@ -418,16 +414,6 @@ static void machvirt_init(QEMUMachineInitArgs *args)
 
     vbi->smp_cpus = smp_cpus;
 
-    /*
-     * Only supported method of starting secondary CPUs is PSCI and
-     * PSCI is not yet supported with TCG, so limit smp_cpus to 1
-     * if we're not using KVM.
-     */
-    if (!kvm_enabled() && smp_cpus > 1) {
-        error_report("mach-virt: must enable KVM to use multiple CPUs");
-        exit(1);
-    }
-
     if (args->ram_size > vbi->memmap[VIRT_MEM].size) {
         error_report("mach-virt: cannot model more than 30GB RAM");
         exit(1);
@@ -446,6 +432,9 @@ static void machvirt_init(QEMUMachineInitArgs *args)
         }
         cpuobj = object_new(object_class_get_name(oc));
 
+        object_property_set_int(cpuobj, QEMU_PSCI_METHOD_HVC, "psci-method",
+                                NULL);
+
         /* Secondary CPUs start in PSCI powered-down state */
         if (n > 0) {
             object_property_set_bool(cpuobj, true, "start-powered-off", NULL);

--
1.9.1

Reviewed-by on v2 5/6
#14 lore raw

Re: [Qemu-devel] [PATCH v2 5/6] arm/virt: enable PSCI emulation support for system emulation

Quoted 12 lines · Rob Herring

From: Rob Herring <[email protected]>

Now that we have PSCI emulation, enable it for the virt platform. This simplifies the virt machine a bit now that PSCI and SMP no longer need to be KVM only features.

---
v2:
- Rebased. Mostly a whitespace change removing the kvm_enabled() check.

hw/arm/virt.c static void machvirt_init(QEMUMachineInitArgs *args) · line 436

Quoted 5 lines
hw/arm/virt.c +2 -0
@@ -446,6 +432,9 @@ static void machvirt_init(QEMUMachineInitArgs *args)
         }
         cpuobj = object_new(object_class_get_name(oc));
 
+        object_property_set_int(cpuobj, QEMU_PSCI_METHOD_HVC, "psci-method",
+                                NULL);

Similarly to patch 6, this should use &error_abort, not NULL. Otherwise

thanks
-- PMM

#15 lore raw

PATCH v2 6/6 arm/highbank: enable PSCI emulation support

In reply to Rob Herring

From: Rob Herring <[email protected]>

Enable PSCI emulation on highbank and midway platforms.

Note that this requires fixing the PSCI function IDs in the DTB to match what QEMU is using. This should get fixed.

---
v2:
- Add error_abort on setting of start-powered-off.

 hw/arm/highbank.c | 9 +++++++++
 1 file changed, 9 insertions(+)
hw/arm/highbank.c +9 -0
index 24231e5..0cbc865 100644
@@ -241,6 +241,15 @@ static void calxeda_init(QEMUMachineInitArgs *args, enum cxmachines machine)
         cpuobj = object_new(object_class_get_name(oc));
         cpu = ARM_CPU(cpuobj);
 
+        object_property_set_int(cpuobj, QEMU_PSCI_METHOD_SMC, "psci-method",
+                                &error_abort);
+
+        /* Secondary CPUs start in PSCI powered-down state */
+        if (n > 0) {
+            object_property_set_bool(cpuobj, true, "start-powered-off",
+                                     &error_abort);
+        }
+
         if (object_property_find(cpuobj, "reset-cbar", NULL)) {
             object_property_set_int(cpuobj, MPCORE_PERIPHBASE,
                                     "reset-cbar", &error_abort);

--
1.9.1