Received: by 2002:a05:6512:3d0e:0:0:0:0 with SMTP id d14csp58431lfv; Tue, 12 Apr 2022 17:10:57 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxRWjl1kbiOii6AXrPBP/YBOgysfLZfK1YsMKXZ0uS9ZjH/l6TVRd+lpXshq9FPxEzWBrJ/ X-Received: by 2002:a17:902:76ca:b0:157:1c6:5660 with SMTP id j10-20020a17090276ca00b0015701c65660mr30227574plt.105.1649808657659; Tue, 12 Apr 2022 17:10:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649808657; cv=none; d=google.com; s=arc-20160816; b=ysXc26W9Rq38KuBNuDhZ6TO8Ls7F6PmMFV9VNT/fixJ1587EMm2NDFqeeAwmthrtzO vt3m5Xj8ZwgeMbVgJf6bK/o9dgqXZbyqYqzm45VFu/Ls22Ax3yW/dp5LijQjhr9So+g0 u2n1oKqfZTZLf+WIPIW9piPIbC+GBN4kPodwwctiDVFROOYJuEQjaVq6vKfYNmANXycK bietZphL6OjeNDBsB0tF8tijdjdxSRP144QRd6J/uCFS6MWImAgnXIVWFgTbGLyeccrp CPIdIRwP7MMJuznz8UhN8Wh4Dn+PCWWfeO1IcTvkOm/NrTSy8zYRgyZHVu2znaHu/c7E tcoA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:references:in-reply-to:message-id:date:subject :cc:to:from:dkim-signature; bh=ammEMva+SuB0Ma6+z1yRQtv1I3GaDTNABx4ZN6TVltE=; b=ELaWTVF+IfeCvJEx64BcD221fZdk/swfO3tAQZ6Ue0rLQbrmEpAcutKEKauU7Ak8nZ Mq9mNjMAEDue1HKWBjek/tepazNnGmCzCdRUSQN82FN7S3ChdsmVDfOnftqZwkqGrODe lrNZl7H1BY2kj50B8HJonm8NkCKUOPCl4fEq3T7Y9L6wBb/g72bGe69gnRvcymnj9R3r 6fVzzreb3jCbE89vrW9OKWb8UCFAbqOcrt6ZhADtdmOduL0zzG2STorvMlJm6k18SI4A fpWZAWbLXrtS3Sd7wFlq9s2wiPi/fkV07P7mUfEqFKnhMdvOsxe0bzVAu0+7XFnGx1EX rD0A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=f4xp2pqa; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id k63-20020a628442000000b004fabe29f82bsi2491559pfd.316.2022.04.12.17.10.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Apr 2022 17:10:57 -0700 (PDT) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=f4xp2pqa; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 1A065148677; Tue, 12 Apr 2022 15:25:47 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S244000AbiDKJjc (ORCPT + 99 others); Mon, 11 Apr 2022 05:39:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38192 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344529AbiDKJjW (ORCPT ); Mon, 11 Apr 2022 05:39:22 -0400 Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 585B9403FC; Mon, 11 Apr 2022 02:36:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1649669818; x=1681205818; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=RSripfRDPX6q0oBFdAW/Me0piSvmrss3ymZMZey5RrI=; b=f4xp2pqaPyrSRFvqXgQHk6aICo+JkFu5BB6H6TaO9tScgtag7YwfW7u2 OU/CUe/nEA/CgKuSflIPxO2utD13OEf5dBYMZxX+0/J0jsPL70OZJCt/L ZylIFpx9fgEgPE+a1mQi6RlvHcqOEA5QDoc6y++4S9Hd0ntTfUDyYVV36 9tl6/M2xoBL/Hrpnk6jwqFGgVJvYi5tXl4IToNbOKd+sqLDe/jTyHMpxI Y2VYlwebAWJp1fF4f04RvQziOVhOafEucfJQKqajBzWVzce7QYYgAoSR6 jnNKCgXfwOLg0AqYV/uJ6iTkX2hPk6tj/biDb3Dw+AFEoeh7XJ7geQUTb w==; X-IronPort-AV: E=McAfee;i="6400,9594,10313"; a="259671443" X-IronPort-AV: E=Sophos;i="5.90,251,1643702400"; d="scan'208";a="259671443" Received: from orsmga008.jf.intel.com ([10.7.209.65]) by fmsmga102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 11 Apr 2022 02:36:58 -0700 X-IronPort-AV: E=Sophos;i="5.90,251,1643702400"; d="scan'208";a="572050510" Received: from arthur-vostro-3668.sh.intel.com ([10.239.13.120]) by orsmga008-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 11 Apr 2022 02:36:52 -0700 From: Zeng Guang To: Paolo Bonzini , Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , kvm@vger.kernel.org, Dave Hansen , Tony Luck , Kan Liang , Thomas Gleixner , Ingo Molnar , Borislav Petkov , "H. Peter Anvin" , Kim Phillips , Jarkko Sakkinen , Jethro Beekman , Kai Huang Cc: x86@kernel.org, linux-kernel@vger.kernel.org, Robert Hu , Gao Chao , Zeng Guang Subject: [PATCH v8 5/9] KVM: x86: Add support for vICR APIC-write VM-Exits in x2APIC mode Date: Mon, 11 Apr 2022 17:04:43 +0800 Message-Id: <20220411090447.5928-6-guang.zeng@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20220411090447.5928-1-guang.zeng@intel.com> References: <20220411090447.5928-1-guang.zeng@intel.com> X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Upcoming Intel CPUs will support virtual x2APIC MSR writes to the vICR, i.e. will trap and generate an APIC-write VM-Exit instead of intercepting the WRMSR. Add support for handling "nodecode" x2APIC writes, which were previously impossible. Note, x2APIC MSR writes are 64 bits wide. Signed-off-by: Zeng Guang --- arch/x86/kvm/lapic.c | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c index 66b0eb0bda94..137c3a2f5180 100644 --- a/arch/x86/kvm/lapic.c +++ b/arch/x86/kvm/lapic.c @@ -67,6 +67,7 @@ static bool lapic_timer_advance_dynamic __read_mostly; #define LAPIC_TIMER_ADVANCE_NS_MAX 5000 /* step-by-step approximation to mitigate fluctuation */ #define LAPIC_TIMER_ADVANCE_ADJUST_STEP 8 +static int kvm_lapic_msr_read(struct kvm_lapic *apic, u32 reg, u64 *data); static inline void __kvm_lapic_set_reg(char *regs, int reg_off, u32 val) { @@ -2230,10 +2231,27 @@ EXPORT_SYMBOL_GPL(kvm_lapic_set_eoi); /* emulate APIC access in a trap manner */ void kvm_apic_write_nodecode(struct kvm_vcpu *vcpu, u32 offset) { - u32 val = kvm_lapic_get_reg(vcpu->arch.apic, offset); + struct kvm_lapic *apic = vcpu->arch.apic; + u64 val; + + if (apic_x2apic_mode(apic)) { + /* + * When guest APIC is in x2APIC mode and IPI virtualization + * is enabled, accessing APIC_ICR may cause trap-like VM-exit + * on Intel hardware. Other offsets are not possible. + */ + if (WARN_ON_ONCE(offset != APIC_ICR)) + return; - /* TODO: optimize to just emulate side effect w/o one more write */ - kvm_lapic_reg_write(vcpu->arch.apic, offset, val); + kvm_lapic_msr_read(apic, offset, &val); + kvm_apic_send_ipi(apic, (u32)val, (u32)(val >> 32)); + trace_kvm_apic_write(APIC_ICR, val); + } else { + val = kvm_lapic_get_reg(apic, offset); + + /* TODO: optimize to just emulate side effect w/o one more write */ + kvm_lapic_reg_write(apic, offset, (u32)val); + } } EXPORT_SYMBOL_GPL(kvm_apic_write_nodecode); -- 2.27.0