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: 9449777 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 930666074E for ; Mon, 28 Nov 2016 17:17:36 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 7F08E27F7F for ; Mon, 28 Nov 2016 17:17:36 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 719DD27F82; Mon, 28 Nov 2016 17:17:36 +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=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=unavailable version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 9081927F7F for ; Mon, 28 Nov 2016 17:17:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S933135AbcK1RRb (ORCPT ); Mon, 28 Nov 2016 12:17:31 -0500 Received: from outprodmail02.cc.columbia.edu ([128.59.72.51]:39013 "EHLO outprodmail02.cc.columbia.edu" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754278AbcK1RR3 (ORCPT ); Mon, 28 Nov 2016 12:17:29 -0500 Received: from hazelnut (hazelnut.cc.columbia.edu [128.59.213.250]) by outprodmail02.cc.columbia.edu (8.14.4/8.14.4) with ESMTP id uASGk5Hx056373 for ; Mon, 28 Nov 2016 11:46:23 -0500 Received: from hazelnut (localhost.localdomain [127.0.0.1]) by hazelnut (Postfix) with ESMTP id 8DD5F6D for ; Mon, 28 Nov 2016 11:46:23 -0500 (EST) Received: from sendprodmail01.cc.columbia.edu (sendprodmail01.cc.columbia.edu [128.59.72.13]) by hazelnut (Postfix) with ESMTP id 5ECEB80 for ; Mon, 28 Nov 2016 11:46:23 -0500 (EST) Received: from mail-qt0-f197.google.com (mail-qt0-f197.google.com [209.85.216.197]) by sendprodmail01.cc.columbia.edu (8.14.4/8.14.4) with ESMTP id uASGkNCU057259 (version=TLSv1/SSLv3 cipher=AES128-GCM-SHA256 bits=128 verify=NOT) for ; Mon, 28 Nov 2016 11:46:23 -0500 Received: by mail-qt0-f197.google.com with SMTP id n6so96201831qtd.4 for ; Mon, 28 Nov 2016 08:46:23 -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=jL7i0whPgUhCuZFdMeDvh0AxRDo9wcghBxJchrPV0+0JLbaWmd2FNeXbf3yWGo3sKg Y5ZVpKaJbMT4tJH2gN25lMZP+b1JzorpQvXWVf+vcvzF6kV6ALzit1RgOBY7N56eCRKc zJ1qIATfywMSmqc/ZjWy4Xng2P+w0ynSTXcpMm6Ijz7bzPyxrAdoWlrSkouMkm4Qep4S kyId/IUeWvAuaZATJW+gB37m7/yufFMO47TaTX7EqxObURsgnEiabTe81vLgLMbw5yBw wJ3Sv0DQVNfaILhs46mT9H/ARyQ2SKC5rydeql7NS0kAYmnoYPDns6YVuNUXDFRB1VZI U3+g== X-Gm-Message-State: AKaTC00rPr6gk1gWp/UBlUaa0VJIPUWxUWnNVrqDVyXrlvE7D6H8AHuqXzJpxkz+IKKqJBzeamHV9gxDayyUl6o8Ud0A1QfsG6gfemdVCCLqTJLd1LOAo6RurS6sXBu7IIdB0VuWXfw0Ubk= X-Received: by 10.237.32.228 with SMTP id 91mr19205475qtb.146.1480351581833; 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 Cc: linux-arm-kernel@lists.infradead.org, marc.zyngier@arm.com, christoffer.dall@linaro.org, will.deacon@arm.com, catalin.marinas@arm.com, pbonzini@redhat.com, rkrcmar@redhat.com, linux@armlinux.org.uk, julien.grall@arm.com, andre.przywara@arm.com, kvm@vger.kernel.org, Jintack Lim 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.13 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.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) {