From patchwork Thu May 8 09:32:48 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marc Zyngier X-Patchwork-Id: 4134201 Return-Path: X-Original-To: patchwork-kvm@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 6FFC1C0ACC for ; Thu, 8 May 2014 09:33:14 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 8503E20266 for ; Thu, 8 May 2014 09:33:13 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 768B82017E for ; Thu, 8 May 2014 09:33:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753634AbaEHJdA (ORCPT ); Thu, 8 May 2014 05:33:00 -0400 Received: from fw-tnat.austin.arm.com ([217.140.110.23]:35434 "EHLO collaborate-mta1.arm.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1753638AbaEHJc6 (ORCPT ); Thu, 8 May 2014 05:32:58 -0400 Received: from e102391-lin.cambridge.arm.com (e102391-lin.cambridge.arm.com [10.1.209.166]) by collaborate-mta1.arm.com (Postfix) with ESMTP id 5D24D13FA3E; Thu, 8 May 2014 04:32:57 -0500 (CDT) From: Marc Zyngier To: kvmarm@lists.cs.columbia.edu, kvm@vger.kernel.org Cc: Pekka Enberg , Will Deacon Subject: [PATCH v4 9/9] kvmtool: virtio: enable arm/arm64 support for bi-endianness Date: Thu, 8 May 2014 10:32:48 +0100 Message-Id: <1399541568-13803-10-git-send-email-marc.zyngier@arm.com> X-Mailer: git-send-email 1.8.3.4 In-Reply-To: <1399541568-13803-1-git-send-email-marc.zyngier@arm.com> References: <1399541568-13803-1-git-send-email-marc.zyngier@arm.com> Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Spam-Status: No, score=-7.5 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Implement the kvm_cpu__get_endianness call for both AArch32 and AArch64, and advertise the bi-endianness support. Signed-off-by: Marc Zyngier --- tools/kvm/arm/aarch32/kvm-cpu.c | 14 ++++++++ tools/kvm/arm/aarch64/include/kvm/kvm-cpu-arch.h | 2 ++ tools/kvm/arm/aarch64/kvm-cpu.c | 43 ++++++++++++++++++++++++ tools/kvm/arm/include/arm-common/kvm-arch.h | 2 ++ 4 files changed, 61 insertions(+) diff --git a/tools/kvm/arm/aarch32/kvm-cpu.c b/tools/kvm/arm/aarch32/kvm-cpu.c index bd71037..464b473 100644 --- a/tools/kvm/arm/aarch32/kvm-cpu.c +++ b/tools/kvm/arm/aarch32/kvm-cpu.c @@ -1,5 +1,6 @@ #include "kvm/kvm-cpu.h" #include "kvm/kvm.h" +#include "kvm/virtio.h" #include @@ -76,6 +77,19 @@ void kvm_cpu__reset_vcpu(struct kvm_cpu *vcpu) die_perror("KVM_SET_ONE_REG failed (pc)"); } +int kvm_cpu__get_endianness(struct kvm_cpu *vcpu) +{ + struct kvm_one_reg reg; + u32 data; + + reg.id = ARM_CORE_REG(usr_regs.ARM_cpsr); + reg.addr = (u64)(unsigned long)&data; + if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, ®) < 0) + die("KVM_GET_ONE_REG failed (cpsr)"); + + return (data & PSR_E_BIT) ? VIRTIO_ENDIAN_BE : VIRTIO_ENDIAN_LE; +} + void kvm_cpu__show_code(struct kvm_cpu *vcpu) { struct kvm_one_reg reg; diff --git a/tools/kvm/arm/aarch64/include/kvm/kvm-cpu-arch.h b/tools/kvm/arm/aarch64/include/kvm/kvm-cpu-arch.h index 7d70c3b..2ffa7ad 100644 --- a/tools/kvm/arm/aarch64/include/kvm/kvm-cpu-arch.h +++ b/tools/kvm/arm/aarch64/include/kvm/kvm-cpu-arch.h @@ -13,5 +13,7 @@ #define ARM_MPIDR_HWID_BITMASK 0xFF00FFFFFFUL #define ARM_CPU_ID 3, 0, 0, 0 #define ARM_CPU_ID_MPIDR 5 +#define ARM_CPU_CTRL 3, 0, 1, 0 +#define ARM_CPU_CTRL_SCTLR_EL1 0 #endif /* KVM__KVM_CPU_ARCH_H */ diff --git a/tools/kvm/arm/aarch64/kvm-cpu.c b/tools/kvm/arm/aarch64/kvm-cpu.c index 059e42c..71a2a3a 100644 --- a/tools/kvm/arm/aarch64/kvm-cpu.c +++ b/tools/kvm/arm/aarch64/kvm-cpu.c @@ -1,12 +1,17 @@ #include "kvm/kvm-cpu.h" #include "kvm/kvm.h" +#include "kvm/virtio.h" #include #define COMPAT_PSR_F_BIT 0x00000040 #define COMPAT_PSR_I_BIT 0x00000080 +#define COMPAT_PSR_E_BIT 0x00000200 #define COMPAT_PSR_MODE_SVC 0x00000013 +#define SCTLR_EL1_E0E_MASK (1 << 24) +#define SCTLR_EL1_EE_MASK (1 << 25) + #define ARM64_CORE_REG(x) (KVM_REG_ARM64 | KVM_REG_SIZE_U64 | \ KVM_REG_ARM_CORE | KVM_REG_ARM_CORE_REG(x)) @@ -133,6 +138,44 @@ void kvm_cpu__reset_vcpu(struct kvm_cpu *vcpu) return reset_vcpu_aarch64(vcpu); } +int kvm_cpu__get_endianness(struct kvm_cpu *vcpu) +{ + struct kvm_one_reg reg; + u64 psr; + u64 sctlr; + + /* + * Quoting the definition given by Peter Maydell: + * + * "Endianness of the CPU which does the virtio reset at the + * point when it does that reset" + * + * We first check for an AArch32 guest: its endianness can + * change when using SETEND, which affects the CPSR.E bit. + * + * If we're AArch64, use SCTLR_EL1.E0E if access comes from + * EL0, and SCTLR_EL1.EE if access comes from EL1. + */ + reg.id = ARM64_CORE_REG(regs.pstate); + reg.addr = (u64)&psr; + if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, ®) < 0) + die("KVM_GET_ONE_REG failed (spsr[EL1])"); + + if (psr & PSR_MODE32_BIT) + return (psr & COMPAT_PSR_E_BIT) ? VIRTIO_ENDIAN_BE : VIRTIO_ENDIAN_LE; + + reg.id = ARM64_SYS_REG(ARM_CPU_CTRL, ARM_CPU_CTRL_SCTLR_EL1); + reg.addr = (u64)&sctlr; + if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, ®) < 0) + die("KVM_GET_ONE_REG failed (SCTLR_EL1)"); + + if ((psr & PSR_MODE_MASK) == PSR_MODE_EL0t) + sctlr &= SCTLR_EL1_E0E_MASK; + else + sctlr &= SCTLR_EL1_EE_MASK; + return sctlr ? VIRTIO_ENDIAN_BE : VIRTIO_ENDIAN_LE; +} + void kvm_cpu__show_code(struct kvm_cpu *vcpu) { struct kvm_one_reg reg; diff --git a/tools/kvm/arm/include/arm-common/kvm-arch.h b/tools/kvm/arm/include/arm-common/kvm-arch.h index b6c4bf8..5d2fab2 100644 --- a/tools/kvm/arm/include/arm-common/kvm-arch.h +++ b/tools/kvm/arm/include/arm-common/kvm-arch.h @@ -35,6 +35,8 @@ #define VIRTIO_DEFAULT_TRANS(kvm) \ ((kvm)->cfg.arch.virtio_trans_pci ? VIRTIO_PCI : VIRTIO_MMIO) +#define VIRTIO_RING_ENDIAN (VIRTIO_ENDIAN_LE | VIRTIO_ENDIAN_BE) + static inline bool arm_addr_in_ioport_region(u64 phys_addr) { u64 limit = KVM_IOPORT_AREA + ARM_IOPORT_SIZE;