From patchwork Thu Feb 19 10:54:45 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 5850871 Return-Path: X-Original-To: patchwork-kvm@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 92C9F9F30C for ; Thu, 19 Feb 2015 10:55:20 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id B4D6820268 for ; Thu, 19 Feb 2015 10:55:19 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id D94592027D for ; Thu, 19 Feb 2015 10:55:17 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752886AbbBSKzL (ORCPT ); Thu, 19 Feb 2015 05:55:11 -0500 Received: from mail-we0-f171.google.com ([74.125.82.171]:38374 "EHLO mail-we0-f171.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752462AbbBSKzH (ORCPT ); Thu, 19 Feb 2015 05:55:07 -0500 Received: by wesw55 with SMTP id w55so6524583wes.5 for ; Thu, 19 Feb 2015 02:55:06 -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:in-reply-to :references; bh=10NFbSURp5ZL4qqanN0mZYbXoOpPvnAuFw67i6yF6dM=; b=Uh738olL+JRHf/p1MKv7nloW/gAhwK2iWMqmnpOZec7I6oMiyKg8D0zyRUhQarD82Z xjHZ7oUBHNrPD8D8VzYgc//Q4wMrw0ADeOgE9ayc0MOfZ26O7L7TeTLfZEbKvXJSZX/o kjR5O2RKf+pDo9hI+5s5vrxY49gKbUqmRF7OcGDYTQfkaT/A8PqrvDHAQEE271kmYAeS ++KiwgUfj4X7O8djb8jj2MUe2T1v1j247wirOy/JZLvUAEknCkpTRNhYh+kWL/norw6H D05ssoONEk4yPHmmVfgW+i1qA2hFR52ONoMO1nHkKoS7ehhzjsTdVwYEs1MiI/UmsYaY OD+g== X-Gm-Message-State: ALoCoQm1yWXeKgeKIjN24aOS2ZsPKoUU1NDBf0DmcQeKEouVd2FD/+6zQ50TmFIZeqm9PpgLCWGW X-Received: by 10.194.122.233 with SMTP id lv9mr7499619wjb.95.1424343306243; Thu, 19 Feb 2015 02:55:06 -0800 (PST) Received: from ards-macbook-pro.local (237.102.108.93.rev.vodafone.pt. [93.108.102.237]) by mx.google.com with ESMTPSA id a1sm29642361wiy.10.2015.02.19.02.55.03 (version=TLSv1.1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Thu, 19 Feb 2015 02:55:05 -0800 (PST) From: Ard Biesheuvel To: lersek@redhat.com, christoffer.dall@linaro.org, marc.zyngier@arm.com, linux-arm-kernel@lists.infradead.org, peter.maydell@linaro.org Cc: kvm@vger.kernel.org, kvmarm@lists.cs.columbia.edu, agraf@suse.de, pbonzini@redhat.com, Ard Biesheuvel Subject: [RFC/RFT PATCH 2/3] arm64: KVM: mangle MAIR register to prevent uncached guest mappings Date: Thu, 19 Feb 2015 10:54:45 +0000 Message-Id: <1424343286-6792-3-git-send-email-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 1.8.3.2 In-Reply-To: <1424343286-6792-1-git-send-email-ard.biesheuvel@linaro.org> References: <1424343286-6792-1-git-send-email-ard.biesheuvel@linaro.org> Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, T_RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=ham 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 Mangle the memory attribute register values at each write to MAIR_EL1 so that regions that the guest intends to map as device or uncached are in fact mapped as cached instead. This avoids incoherency issues when the guest bypassed the caches to access memory that the host has mapped as cached. Signed-off-by: Ard Biesheuvel --- arch/arm64/kvm/sys_regs.c | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/arch/arm64/kvm/sys_regs.c b/arch/arm64/kvm/sys_regs.c index 1e170eab6603..bde2b49a7cd8 100644 --- a/arch/arm64/kvm/sys_regs.c +++ b/arch/arm64/kvm/sys_regs.c @@ -110,6 +110,39 @@ static bool access_vm_reg(struct kvm_vcpu *vcpu, return true; } +static bool access_mair(struct kvm_vcpu *vcpu, + const struct sys_reg_params *p, + const struct sys_reg_desc *r) +{ + unsigned long val, mask; + + BUG_ON(!p->is_write); + + val = *vcpu_reg(vcpu, p->Rt); + + if (!p->is_aarch32) { + /* + * Mangle val so that all device and uncached attributes are + * replaced with cached attributes. + * For each attribute, check whether any of bit 7, bit 5 or bit + * 4 are set. If not, it is a device or outer non-cacheable + * mapping and we override it with inner, outer write-through, + * read+write-allocate (0xbb). + * TODO: handle outer cacheable inner non-cacheable + */ + mask = ~(val >> 7 | val >> 5 | val >> 4) & 0x0101010101010101UL; + val = (val & ~(mask * 0xff)) | (mask * 0xbb); + + vcpu_sys_reg(vcpu, r->reg) = val; + } else { + if (!p->is_32bit) + vcpu_cp15_64_high(vcpu, r->reg) = val >> 32; + vcpu_cp15_64_low(vcpu, r->reg) = val & 0xffffffffUL; + } + + return true; +} + static bool trap_raz_wi(struct kvm_vcpu *vcpu, const struct sys_reg_params *p, const struct sys_reg_desc *r) @@ -371,7 +404,7 @@ static const struct sys_reg_desc sys_reg_descs[] = { /* MAIR_EL1 */ { Op0(0b11), Op1(0b000), CRn(0b1010), CRm(0b0010), Op2(0b000), - access_vm_reg, reset_unknown, MAIR_EL1 }, + access_mair, reset_unknown, MAIR_EL1 }, /* AMAIR_EL1 */ { Op0(0b11), Op1(0b000), CRn(0b1010), CRm(0b0011), Op2(0b000), access_handled_at_el2, reset_amair_el1, AMAIR_EL1 },