From patchwork Mon Nov 28 16:46:10 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jintack Lim X-Patchwork-Id: 9449765 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 875CA600CB for ; Mon, 28 Nov 2016 16:48:25 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 78DB627BFF for ; Mon, 28 Nov 2016 16:48:25 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 6D92E27EE9; Mon, 28 Nov 2016 16:48:25 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-3.7 required=2.0 tests=BAYES_00, RCVD_IN_DNSWL_MED, RCVD_IN_SORBS_SPAM autolearn=ham version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.9]) (using TLSv1.2 with cipher AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 4767027BFF for ; Mon, 28 Nov 2016 16:48:24 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.85_2 #1 (Red Hat Linux)) id 1cBP4w-000588-K3; Mon, 28 Nov 2016 16:46:54 +0000 Received: from outprodmail01.cc.columbia.edu ([128.59.72.39]) by bombadil.infradead.org with esmtps (Exim 4.85_2 #1 (Red Hat Linux)) id 1cBP4l-0004pB-9Z for linux-arm-kernel@lists.infradead.org; Mon, 28 Nov 2016 16:46:46 +0000 Received: from hazelnut (hazelnut.cc.columbia.edu [128.59.213.250]) by outprodmail01.cc.columbia.edu (8.14.4/8.14.4) with ESMTP id uASGiTBC009523 for ; Mon, 28 Nov 2016 11:46:22 -0500 Received: from hazelnut (localhost.localdomain [127.0.0.1]) by hazelnut (Postfix) with ESMTP id 602177E for ; Mon, 28 Nov 2016 11:46:22 -0500 (EST) Received: from sendprodmail03.cc.columbia.edu (sendprodmail03.cc.columbia.edu [128.59.72.15]) by hazelnut (Postfix) with ESMTP id 4787A7E for ; Mon, 28 Nov 2016 11:46:22 -0500 (EST) Received: from mail-qk0-f200.google.com (mail-qk0-f200.google.com [209.85.220.200]) by sendprodmail03.cc.columbia.edu (8.14.4/8.14.4) with ESMTP id uASGkMqc020566 (version=TLSv1/SSLv3 cipher=AES128-GCM-SHA256 bits=128 verify=NOT) for ; Mon, 28 Nov 2016 11:46:22 -0500 Received: by mail-qk0-f200.google.com with SMTP id i34so114350936qkh.1 for ; Mon, 28 Nov 2016 08:46:22 -0800 (PST) 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; bh=+pRGMck+SmRglaAmsyZ2h1fsgzN00nqLcozzV3y+c6U=; b=TND7EzQHvEzNpytOcET87b6OMTYPkmJxTPx6AxjOe/NqkS143xOz++nj+Z/ud74qoP 0JOoru4FW4tcQzeHRLrfbggs+27VtH2w8QqwjcbfX5H5jL1h3U37sI9sn+zqaCBAzp0B cHZFVBfKL7cExSOOiOV/mEQdQ/ZGyVSuDnEKy/WLMfhp2x1PrYSUrtZlnqvFPP01iJJT ug71CBgk1v3doeFwR/55ZeinrwWlbbGZxymHAqwlGZCj5SJWeJFOs05fpwQhzXAsGLGt bW7U1gMacVWmUIKS4Z26xbzyEKrxyoDgRvsILf+m3qk40nLMZ2NGA0vL3hpyAEcG1pG8 CMfQ== X-Gm-Message-State: AKaTC01rptRstIqyLstqjRowF9tZxDCgd6DJjLBKRIe5rgmzirYGzedbgMv3urK/GtXMGCiubtDXZS0HzXh/pXq6QmqSJguopH51oNg73D8lqGNC/pX4yFihlYNVaRSTjNRECG87EBFXPbtV6Xt0Sx2uLzCvMJ6puF0bhg== X-Received: by 10.237.32.228 with SMTP id 91mr19205464qtb.146.1480351581829; Mon, 28 Nov 2016 08:46:21 -0800 (PST) X-Received: by 10.237.32.228 with SMTP id 91mr19205441qtb.146.1480351581593; Mon, 28 Nov 2016 08:46:21 -0800 (PST) Received: from jintack.cs.columbia.edu ([2001:18d8:ffff:16:21a:4aff:feaa:f900]) by smtp.gmail.com with ESMTPSA id b63sm28603027qka.39.2016.11.28.08.46.20 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 28 Nov 2016 08:46:20 -0800 (PST) From: Jintack Lim To: kvmarm@lists.cs.columbia.edu Subject: [PATCH] KVM: arm/arm64: Access CNTHCTL_EL2 bit fields correctly Date: Mon, 28 Nov 2016 11:46:10 -0500 Message-Id: <1480351570-11648-1-git-send-email-jintack@cs.columbia.edu> X-Mailer: git-send-email 1.9.1 X-No-Spam-Score: Local X-Scanned-By: MIMEDefang 2.78 on 128.59.72.15 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20161128_084643_647179_F96F746F X-CRM114-Status: GOOD ( 15.52 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kvm@vger.kernel.org, rkrcmar@redhat.com, marc.zyngier@arm.com, catalin.marinas@arm.com, will.deacon@arm.com, linux@armlinux.org.uk, julien.grall@arm.com, linux-arm-kernel@lists.infradead.org, andre.przywara@arm.com, pbonzini@redhat.com, Jintack Lim , christoffer.dall@linaro.org MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP Bit positions of CNTHCTL_EL2 are changing depending on HCR_EL2.E2H bit. EL1PCEN and EL1PCTEN are 1st and 0th bits when E2H is not set, but they are 11th and 10th bits respectively when E2H is set. Current code is unintentionally setting wrong bits to CNTHCTL_EL2 with E2H set, which may allow guest OS to access physical timer. So, fix it. Signed-off-by: Jintack Lim --- arch/arm/include/asm/kvm_timer.h | 33 +++++++++++++++++++ arch/arm64/include/asm/kvm_timer.h | 62 ++++++++++++++++++++++++++++++++++++ include/clocksource/arm_arch_timer.h | 6 ++-- virt/kvm/arm/hyp/timer-sr.c | 8 ++--- 4 files changed, 103 insertions(+), 6 deletions(-) create mode 100644 arch/arm/include/asm/kvm_timer.h create mode 100644 arch/arm64/include/asm/kvm_timer.h diff --git a/arch/arm/include/asm/kvm_timer.h b/arch/arm/include/asm/kvm_timer.h new file mode 100644 index 0000000..d19d4b3 --- /dev/null +++ b/arch/arm/include/asm/kvm_timer.h @@ -0,0 +1,33 @@ +/* + * Copyright (C) 2016 - Columbia University + * Author: Jintack Lim + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * 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 . + */ + +#ifndef __ARM_KVM_TIMER_H__ +#define __ARM_KVM_TIMER_H__ + +#include + +static inline u32 __hyp_text get_el1pcten(void) +{ + return CNTHCTL_EL1PCTEN_NVHE; +} + +static inline u32 __hyp_text get_el1pcen(void) +{ + return CNTHCTL_EL1PCEN_NVHE; +} + +#endif /* __ARM_KVM_TIMER_H__ */ diff --git a/arch/arm64/include/asm/kvm_timer.h b/arch/arm64/include/asm/kvm_timer.h new file mode 100644 index 0000000..153f3da --- /dev/null +++ b/arch/arm64/include/asm/kvm_timer.h @@ -0,0 +1,62 @@ +/* + * Copyright (C) 2016 - Columbia University + * Author: Jintack Lim + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * 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 . + */ + +#ifndef __ARM64_KVM_TIMER_H__ +#define __ARM64_KVM_TIMER_H__ + +#include +#include + +static inline u32 __hyp_text get_el1pcten_vhe(void) +{ + return CNTHCTL_EL1PCTEN_VHE; +} + +static inline u32 __hyp_text get_el1pcten_nvhe(void) +{ + return CNTHCTL_EL1PCTEN_NVHE; +} + +static hyp_alternate_select(get_el1pcten_arch, + get_el1pcten_nvhe, get_el1pcten_vhe, + ARM64_HAS_VIRT_HOST_EXTN); + +static inline u32 __hyp_text get_el1pten_vhe(void) +{ + return CNTHCTL_EL1PTEN_VHE; +} + +static inline u32 __hyp_text get_el1pcen_nvhe(void) +{ + return CNTHCTL_EL1PCEN_NVHE; +} + +static hyp_alternate_select(get_el1pcen_arch, + get_el1pcen_nvhe, get_el1pten_vhe, + ARM64_HAS_VIRT_HOST_EXTN); + +static inline u32 __hyp_text get_el1pcten(void) +{ + return get_el1pcten_arch()(); +} + +static inline u32 __hyp_text get_el1pcen(void) +{ + return get_el1pcen_arch()(); +} + +#endif /* __ARM64_KVM_TIMER_H__ */ diff --git a/include/clocksource/arm_arch_timer.h b/include/clocksource/arm_arch_timer.h index caedb74..4094529 100644 --- a/include/clocksource/arm_arch_timer.h +++ b/include/clocksource/arm_arch_timer.h @@ -23,8 +23,10 @@ #define ARCH_TIMER_CTRL_IT_MASK (1 << 1) #define ARCH_TIMER_CTRL_IT_STAT (1 << 2) -#define CNTHCTL_EL1PCTEN (1 << 0) -#define CNTHCTL_EL1PCEN (1 << 1) +#define CNTHCTL_EL1PCTEN_NVHE (1 << 0) +#define CNTHCTL_EL1PCEN_NVHE (1 << 1) +#define CNTHCTL_EL1PCTEN_VHE (1 << 10) +#define CNTHCTL_EL1PTEN_VHE (1 << 11) #define CNTHCTL_EVNTEN (1 << 2) #define CNTHCTL_EVNTDIR (1 << 3) #define CNTHCTL_EVNTI (0xF << 4) diff --git a/virt/kvm/arm/hyp/timer-sr.c b/virt/kvm/arm/hyp/timer-sr.c index 798866a..f3feee0 100644 --- a/virt/kvm/arm/hyp/timer-sr.c +++ b/virt/kvm/arm/hyp/timer-sr.c @@ -15,11 +15,11 @@ * along with this program. If not, see . */ -#include #include #include #include +#include /* vcpu is already in the HYP VA space */ void __hyp_text __timer_save_state(struct kvm_vcpu *vcpu) @@ -37,7 +37,7 @@ void __hyp_text __timer_save_state(struct kvm_vcpu *vcpu) /* Allow physical timer/counter access for the host */ val = read_sysreg(cnthctl_el2); - val |= CNTHCTL_EL1PCTEN | CNTHCTL_EL1PCEN; + val |= get_el1pcten() | get_el1pcen(); write_sysreg(val, cnthctl_el2); /* Clear cntvoff for the host */ @@ -55,8 +55,8 @@ void __hyp_text __timer_restore_state(struct kvm_vcpu *vcpu) * Physical counter access is allowed */ val = read_sysreg(cnthctl_el2); - val &= ~CNTHCTL_EL1PCEN; - val |= CNTHCTL_EL1PCTEN; + val &= ~get_el1pcen(); + val |= get_el1pcten(); write_sysreg(val, cnthctl_el2); if (timer->enabled) {