From patchwork Mon Sep 1 11:55:27 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 384798 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3DC101401B5 for ; Mon, 1 Sep 2014 21:58:00 +1000 (EST) Received: from localhost ([::1]:59689 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XOQFC-00007C-9H for incoming@patchwork.ozlabs.org; Mon, 01 Sep 2014 07:57:58 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:45697) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XOQD8-0004zp-HA for qemu-devel@nongnu.org; Mon, 01 Sep 2014 07:55:56 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1XOQD2-0001hn-Fn for qemu-devel@nongnu.org; Mon, 01 Sep 2014 07:55:50 -0400 Received: from mail-wg0-f44.google.com ([74.125.82.44]:38306) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XOQD2-0001hb-6C for qemu-devel@nongnu.org; Mon, 01 Sep 2014 07:55:44 -0400 Received: by mail-wg0-f44.google.com with SMTP id m15so5263568wgh.15 for ; Mon, 01 Sep 2014 04:55:43 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=F2XkPM8qCIghFJ8qdyc99D7txxXDEIR3e2Hu4BC3R84=; b=iodk89mp55u4KFGwyuxremZalmcH1twy2nL/8jpoQ+qXeFhGlNUmFMfLyR3rWQIVwE DSp8vZh1Nms/HhWkNEymPy15z7+DJD7E77gJyx1VMCtoncZhHGMxkz/vST2eSZ2BfcRB h31mj/fbi7NgRil+Nrx2Kn+pAIrfie8giKLdqgNd3Sz+Ha6eljnhwx5OtcZ9owc5XPSu aP0RjStlVpY1YzuAPZQ0y8bDCpRXUMYlHaDqhBV4kdXXVRTosKPJkPPc4fIILMdPnVqt p2r/QAyzteSvQi2SUaLZXZ/36TX7C9EfVuqVfFCbBNF/wrB7riLSqV4O5i0CSIZq5B9n F5jA== X-Gm-Message-State: ALoCoQkXzxfPLe2Gy5PmTOcw8jHx04p6NkhLK1gQ6K+YgXPfAo5XqD7XIkbI+nILTZ4girIH8T6s X-Received: by 10.194.90.233 with SMTP id bz9mr2551205wjb.94.1409572543428; Mon, 01 Sep 2014 04:55:43 -0700 (PDT) Received: from ards-macbook-pro.local (cag06-7-83-153-85-71.fbx.proxad.net. [83.153.85.71]) by mx.google.com with ESMTPSA id mz16sm23997986wic.13.2014.09.01.04.55.42 for (version=TLSv1.1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Mon, 01 Sep 2014 04:55:42 -0700 (PDT) From: Ard Biesheuvel To: qemu-devel@nongnu.org, peter.maydell@linaro.org, rob.herring@linaro.org Date: Mon, 1 Sep 2014 13:55:27 +0200 Message-Id: <1409572529-14897-4-git-send-email-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 1.8.3.2 In-Reply-To: <1409572529-14897-1-git-send-email-ard.biesheuvel@linaro.org> References: <1409572529-14897-1-git-send-email-ard.biesheuvel@linaro.org> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 74.125.82.44 Cc: Ard Biesheuvel Subject: [Qemu-devel] [PATCH v2 3/5] target-arm: add hvc and smc exception emulation handling infrastructure X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org From: Rob Herring 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. Signed-off-by: Rob Herring Signed-off-by: Ard Biesheuvel --- target-arm/cpu-qom.h | 3 +++ target-arm/cpu.h | 2 ++ target-arm/helper-a64.c | 16 ++++++++++++++++ target-arm/helper.c | 32 ++++++++++++++++++++++++++++++++ target-arm/internals.h | 20 ++++++++++++++++++++ target-arm/translate-a64.c | 26 +++++++++++++++++--------- target-arm/translate.c | 24 +++++++++++++++++------- 7 files changed, 107 insertions(+), 16 deletions(-) diff --git a/target-arm/cpu-qom.h b/target-arm/cpu-qom.h index eae0a7b9c908..104cc67e82d2 100644 --- a/target-arm/cpu-qom.h +++ b/target-arm/cpu-qom.h @@ -192,6 +192,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); diff --git a/target-arm/cpu.h b/target-arm/cpu.h index 51bedc826299..d235929f4c12 100644 --- a/target-arm/cpu.h +++ b/target-arm/cpu.h @@ -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 diff --git a/target-arm/helper-a64.c b/target-arm/helper-a64.c index 89b913ee9396..1f8072ab141b 100644 --- a/target-arm/helper-a64.c +++ b/target-arm/helper-a64.c @@ -485,6 +485,22 @@ void aarch64_cpu_do_interrupt(CPUState *cs) case EXCP_FIQ: addr += 0x100; break; + case EXCP_HVC: + if (arm_cpu_do_hvc(cs)) { + return; + } + /* Treat as unallocated encoding */ + qemu_log_mask(LOG_GUEST_ERROR, "HVC not implemented on this CPU\n"); + env->exception.syndrome = syn_uncategorized(); + break; + case EXCP_SMC: + if (arm_cpu_do_smc(cs)) { + return; + } + /* Treat as unallocated encoding */ + qemu_log_mask(LOG_GUEST_ERROR, "SMC not implemented on this CPU\n"); + env->exception.syndrome = syn_uncategorized(); + break; default: cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index); } diff --git a/target-arm/helper.c b/target-arm/helper.c index 2b95f33872cb..51a01a815b7b 100644 --- a/target-arm/helper.c +++ b/target-arm/helper.c @@ -3497,6 +3497,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) { @@ -3599,6 +3609,28 @@ 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, "HVC not implemented on this CPU\n"); + goto hvc_unallocated; + case EXCP_SMC: + if (arm_cpu_do_smc(cs)) { + return; + } + qemu_log_mask(LOG_GUEST_ERROR, "SMC not implemented on this CPU\n"); + hvc_unallocated: + /* Treat as unallocated encoding */ + new_mode = ARM_CPU_MODE_UND; + addr = 0x04; + mask = CPSR_I; + if (env->thumb) { + offset = 2; + } else { + offset = 4; + } + break; default: cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index); return; /* Never happens. Keep compiler happy. */ diff --git a/target-arm/internals.h b/target-arm/internals.h index 53c2e3cf3e7e..caab98e6b508 100644 --- a/target-arm/internals.h +++ b/target-arm/internals.h @@ -210,6 +210,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); diff --git a/target-arm/translate-a64.c b/target-arm/translate-a64.c index 8e66b6c97282..65e35e3aaec0 100644 --- a/target-arm/translate-a64.c +++ b/target-arm/translate-a64.c @@ -1473,20 +1473,28 @@ static void disas_exc(DisasContext *s, uint32_t insn) switch (opc) { case 0: - /* 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); - break; - } /* For SVC, HVC and SMC we advance the single-step state * machine before taking the exception. This is architecturally * mandated, to ensure that single-stepping a system call * instruction works properly. */ - gen_ss_advance(s); - gen_exception_insn(s, 0, EXCP_SWI, syn_aa64_svc(imm16)); + switch (op2_ll) { + case 1: + gen_ss_advance(s); + gen_exception_insn(s, 0, EXCP_SWI, syn_aa64_svc(imm16)); + break; + case 2: + gen_ss_advance(s); + gen_exception_insn(s, 0, EXCP_HVC, syn_aa64_hvc(imm16)); + break; + case 3: + gen_ss_advance(s); + gen_exception_insn(s, 0, EXCP_SMC, syn_aa64_smc(imm16)); + break; + default: + unallocated_encoding(s); + break; + } break; case 1: if (op2_ll != 0) { diff --git a/target-arm/translate.c b/target-arm/translate.c index 2c0b1deaea81..a4545ed2bc40 100644 --- a/target-arm/translate.c +++ b/target-arm/translate.c @@ -7871,9 +7871,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; } @@ -9709,10 +9714,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, 16, 4) << 12; + imm16 |= extract32(insn, 0, 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) {