From patchwork Fri Jul 19 23:50:58 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13737568 Received: from mail-pf1-f201.google.com (mail-pf1-f201.google.com [209.85.210.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AC9F214884F for ; Fri, 19 Jul 2024 23:51:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721433073; cv=none; b=dEm9Uad+OaWaouS7D3N6Y8d73Z/QgUZcadb73SBJazxOyHfPixsEbtSTktWudssSBHwE0LUY2EDaK2qYXvjE+Ig/L1CdvBh/fogsejXPYNzsNFApkawB2XrZhYysGBhBZ7ZYhOnhVxk2CCInXgEBeiydT+7I52+2AjXOwI2VEZE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721433073; c=relaxed/simple; bh=9VC1o23EyPnmCsIUXN13mbYo4nMy/PcXMeGVi57HKHs=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Fc8YA56kHa49YMeILNZl4raDGDRx2I/3U/vUjvIs1nuFQ/FmUe7isqKGMSgmeEnzO6YZ9OgswQ99lqdNQuXyYiCJxQywbDvqKOoHE3PuLzUOSSrcr4WmF0nHXzdji+fQgFNfpq0dTkTT2KCabIdRUXXEAdER8PTC+Oo4o5kf2bo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=dEK8ZuAl; arc=none smtp.client-ip=209.85.210.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="dEK8ZuAl" Received: by mail-pf1-f201.google.com with SMTP id d2e1a72fcca58-70b09eb46e4so1170355b3a.3 for ; Fri, 19 Jul 2024 16:51:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1721433071; x=1722037871; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=Q7GRI70QT0vlqusEkjG3FXqYLsYvXxnP78B7joN4qu0=; b=dEK8ZuAldsDEx2w1w+2j1cDUt0l/POihaDi5wQ0aD7+qbKYHvnyKAMX+/pVkgCVUHl mZvtohjQ72zq7kRuEkiq24Ay8xps3o5wgUpD94PPGlILSRh/2GIcHDG5ju0PvVCpWqF+ oMCR0xW/s0v22yLCxycYwOSICrae0C1eNpEjlmH/ltmgX41GKnY1d5csVk0xNSBnn1ZZ WD7jMoSPrN/zA50JolEwvym5Su5YhLm0GoQVq6BDBGZTk3DrMVIomfHZkMg5fHDFI8rD AN61pF+JFdFWdI48VE0g/NaPukVM8DbL9FEqqRLlgwLK2LiHIkFpMFze5Ib2S7NS7MY/ XoDQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721433071; x=1722037871; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=Q7GRI70QT0vlqusEkjG3FXqYLsYvXxnP78B7joN4qu0=; b=OdbkOYn5FVeW103+itG9ly0y3TfWfWoYJtDlpyruRT420GSsRE0s5pETCwQ+bITsZ/ wvSe8S4eZZqTdJKeApnv1Z2mwZ/a7w8bfOLFv54tJZV1UyP5JIFpfksQnlyOBS880xaO yquUiMurwbWZ0ot3qH2lKkQi/LbR2fTok/2MoUh4GTI1kLozueksQ/fmv7o6c2VJc+qD fxRVQvdA3RFkx8qw6QCovk3d7U0DI3nc/PCPDNW71StM8ncpr5eev2/PxiLpK4JO57Xc R8L7tJlJOtFjrPF2wus7g3r4AlH1PE64txnA9ud1SfviO/HYWx6v3hTYP3zBVAhu97AG d6jQ== X-Gm-Message-State: AOJu0YxjsCxVArb67leFhTZSSQicti8yxWg6q4s4d0e65J0eC0rEIMqn 94/UbVzSkLXqDGrNkTJGWGsQePTEJ8dFu+tQ06yYb9VapEGtptVzaZ9l5Z12KiBto6e0Tx6Jm95 C7A== X-Google-Smtp-Source: AGHT+IGzaPvQY/Gvr64S0cERzYi+DWtBU+TZAqXIrmD6fzYYJUjCv+m+z7gk6KBNOoZyk0taViygByegSz0= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:aa7:8e06:0:b0:706:5ca6:1c6b with SMTP id d2e1a72fcca58-70d08602774mr4333b3a.1.1721433070968; Fri, 19 Jul 2024 16:51:10 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 19 Jul 2024 16:50:58 -0700 In-Reply-To: <20240719235107.3023592-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240719235107.3023592-1-seanjc@google.com> X-Mailer: git-send-email 2.45.2.1089.g2a221341d9-goog Message-ID: <20240719235107.3023592-2-seanjc@google.com> Subject: [PATCH v2 01/10] KVM: x86: Enforce x2APIC's must-be-zero reserved ICR bits From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Maxim Levitsky , Suravee Suthikulpanit Inject a #GP on a WRMSR(ICR) that attempts to set any reserved bits that are must-be-zero on both Intel and AMD, i.e. any reserved bits other than the BUSY bit, which Intel ignores and basically says is undefined. KVM's xapic_state_test selftest has been fudging the bug since commit 4b88b1a518b3 ("KVM: selftests: Enhance handling WRMSR ICR register in x2APIC mode"), which essentially removed the testcase instead of fixing the bug. WARN if the nodecode path triggers a #GP, as the CPU is supposed to check reserved bits for ICR when it's partially virtualized. Cc: stable@vger.kernel.org Signed-off-by: Sean Christopherson --- arch/x86/kvm/lapic.c | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c index a7172ba59ad2..35c4567567a2 100644 --- a/arch/x86/kvm/lapic.c +++ b/arch/x86/kvm/lapic.c @@ -2472,7 +2472,7 @@ void kvm_apic_write_nodecode(struct kvm_vcpu *vcpu, u32 offset) * maybe-unecessary write, and both are in the noise anyways. */ if (apic_x2apic_mode(apic) && offset == APIC_ICR) - kvm_x2apic_icr_write(apic, kvm_lapic_get_reg64(apic, APIC_ICR)); + WARN_ON_ONCE(kvm_x2apic_icr_write(apic, kvm_lapic_get_reg64(apic, APIC_ICR))); else kvm_lapic_reg_write(apic, offset, kvm_lapic_get_reg(apic, offset)); } @@ -3186,8 +3186,21 @@ int kvm_lapic_set_vapic_addr(struct kvm_vcpu *vcpu, gpa_t vapic_addr) return 0; } +#define X2APIC_ICR_RESERVED_BITS (GENMASK_ULL(31, 20) | GENMASK_ULL(17, 16) | BIT(13)) + int kvm_x2apic_icr_write(struct kvm_lapic *apic, u64 data) { + if (data & X2APIC_ICR_RESERVED_BITS) + return 1; + + /* + * The BUSY bit is reserved on both Intel and AMD in x2APIC mode, but + * only AMD requires it to be zero, Intel essentially just ignores the + * bit. And if IPI virtualization (Intel) or x2AVIC (AMD) is enabled, + * the CPU performs the reserved bits checks, i.e. the underlying CPU + * behavior will "win". Arbitrarily clear the BUSY bit, as there is no + * sane way to provide consistent behavior with respect to hardware. + */ data &= ~APIC_ICR_BUSY; kvm_apic_send_ipi(apic, (u32)data, (u32)(data >> 32)); From patchwork Fri Jul 19 23:50:59 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13737569 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8EC871494DB for ; Fri, 19 Jul 2024 23:51:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721433074; cv=none; b=iSy+Yb+JpFofFRVzs7j7Or2a+a9Xe/Wu2c+PA1NorTOvNlF+kQCUv40pSW4p3XR2hwecIVRkgC1i3sLk8X7x2UfoU73ZxSHqPhdI1BlI2MwM1X0u/vuIIXdpz6aAeg6mIPA75RhIzYBaYgfuAoDNSDf4XI7UQyKaMNbdNqGZ5Kw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721433074; c=relaxed/simple; bh=Z1cH1V9K2pPOjjICaFF7xPr76fow0RyXynIXQuiRywA=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=g8+SnH4G3ovzGrxi4SsEPWDsPsxUDx1uHZ3vdO6UJMDVJ2foZUYM7fbIMHeB4DN9g7XRt0r0Ywcuk+Iefb5M18IUe9f81iDt8vgdfjN/fOHPJXMgB1URm/SWDyTmNrkgujhribkh9PeS22w2MUU1UbvMZXHGNvRxT7iohk+M5FM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=OoI3Ey6G; arc=none smtp.client-ip=209.85.216.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="OoI3Ey6G" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-2cb7364bac9so1836715a91.0 for ; Fri, 19 Jul 2024 16:51:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1721433073; x=1722037873; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=RdVO46PMlcBgI+ne7j7UwSAA4lN1OIh/NoMWcCiQ7DM=; b=OoI3Ey6GTgrYVKbH5Dz1xQw/VxYVlAZlUJSuCfbXBYjp4p0lHZj37PYNPQOBrsJoTK 0CZueGELn7K2s9ongLOoh3i5vJ1vPREDEXwtfaSV9kSL1wBf+T5IPgvsqzG4HvoWuPPL GU2FgT41NNicpd9Oi6z1vQDdfQgkqexjcrofT7CoKP0dLIijEESME+2RwNP0+hlBOf7v sZlIxq361CVmwuWeR4rjmBLhBuprwwR9+W1Q8WEamQt0UDG6z2dIbFINFU4i1bYpNZle adFd0vBObsHgwb4AzUQ+kvYIB0An/9C5ZShJvBdOy6s0XVkqSW1O1hQt35RjQ/b0dnAp YJNQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721433073; x=1722037873; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=RdVO46PMlcBgI+ne7j7UwSAA4lN1OIh/NoMWcCiQ7DM=; b=jXSmNxnDoHX/revFmMrtzobv1CqzaVoSq57rCnXk+Aqvspx0aGtFAfX7ALI4pDXit2 gk7GBLCadOb45Y6y10q6r9Fatrref19tnpx0n6yzfCXxY01MZIEUSp47GlJJnkjZqzmr TB8PYFhDTVgrMHpWwgUlgDKqq86Fq69sCgPe0GaRM3uaRQHnPolX8Q2OzLLwTGnvvJuM V0G2G/BwE8rbxpnP9M+Z8h1hrEvT6xH8gddjk4jLOEuBE4LQ/kQs5f48jBwQy3RnEbFx q7owUhsXtFlpkbcil3MNYrXtRhgk+RszmX/uTOHXtpHARrhPnUJ38QZkJckDSqVlyx4X +DZA== X-Gm-Message-State: AOJu0YwZdDTTj8iWPXpTI4cIHaXB3Oy9Y6cGaDtN/NPKyOKTR9hdVpCi bZDTcDMk3BghdSUVox+FdKZhIkdD0nYuraKXOegfO/Bb6xXyEElqdHYX+ZSYYugTqv+AmQSANRa V9Q== X-Google-Smtp-Source: AGHT+IGmFkrrWPx3tuEOZ0FODIaBqubff0bPniwUu0Iu1FG6TzHsKKgJswuzEa2qgvqE403sR3EIJhNUJ+w= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:90b:1b0e:b0:2ca:f1a0:8e74 with SMTP id 98e67ed59e1d1-2cd170a5399mr56982a91.2.1721433072795; Fri, 19 Jul 2024 16:51:12 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 19 Jul 2024 16:50:59 -0700 In-Reply-To: <20240719235107.3023592-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240719235107.3023592-1-seanjc@google.com> X-Mailer: git-send-email 2.45.2.1089.g2a221341d9-goog Message-ID: <20240719235107.3023592-3-seanjc@google.com> Subject: [PATCH v2 02/10] KVM: x86: Move x2APIC ICR helper above kvm_apic_write_nodecode() From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Maxim Levitsky , Suravee Suthikulpanit Hoist kvm_x2apic_icr_write() above kvm_apic_write_nodecode() so that a local helper to _read_ the x2APIC ICR can be added and used in the nodecode path without needing a forward declaration. No functional change intended. Cc: stable@vger.kernel.org Signed-off-by: Sean Christopherson --- arch/x86/kvm/lapic.c | 46 ++++++++++++++++++++++---------------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c index 35c4567567a2..d14ef485b0bd 100644 --- a/arch/x86/kvm/lapic.c +++ b/arch/x86/kvm/lapic.c @@ -2455,6 +2455,29 @@ void kvm_lapic_set_eoi(struct kvm_vcpu *vcpu) } EXPORT_SYMBOL_GPL(kvm_lapic_set_eoi); +#define X2APIC_ICR_RESERVED_BITS (GENMASK_ULL(31, 20) | GENMASK_ULL(17, 16) | BIT(13)) + +int kvm_x2apic_icr_write(struct kvm_lapic *apic, u64 data) +{ + if (data & X2APIC_ICR_RESERVED_BITS) + return 1; + + /* + * The BUSY bit is reserved on both Intel and AMD in x2APIC mode, but + * only AMD requires it to be zero, Intel essentially just ignores the + * bit. And if IPI virtualization (Intel) or x2AVIC (AMD) is enabled, + * the CPU performs the reserved bits checks, i.e. the underlying CPU + * behavior will "win". Arbitrarily clear the BUSY bit, as there is no + * sane way to provide consistent behavior with respect to hardware. + */ + data &= ~APIC_ICR_BUSY; + + kvm_apic_send_ipi(apic, (u32)data, (u32)(data >> 32)); + kvm_lapic_set_reg64(apic, APIC_ICR, data); + trace_kvm_apic_write(APIC_ICR, data); + return 0; +} + /* emulate APIC access in a trap manner */ void kvm_apic_write_nodecode(struct kvm_vcpu *vcpu, u32 offset) { @@ -3186,29 +3209,6 @@ int kvm_lapic_set_vapic_addr(struct kvm_vcpu *vcpu, gpa_t vapic_addr) return 0; } -#define X2APIC_ICR_RESERVED_BITS (GENMASK_ULL(31, 20) | GENMASK_ULL(17, 16) | BIT(13)) - -int kvm_x2apic_icr_write(struct kvm_lapic *apic, u64 data) -{ - if (data & X2APIC_ICR_RESERVED_BITS) - return 1; - - /* - * The BUSY bit is reserved on both Intel and AMD in x2APIC mode, but - * only AMD requires it to be zero, Intel essentially just ignores the - * bit. And if IPI virtualization (Intel) or x2AVIC (AMD) is enabled, - * the CPU performs the reserved bits checks, i.e. the underlying CPU - * behavior will "win". Arbitrarily clear the BUSY bit, as there is no - * sane way to provide consistent behavior with respect to hardware. - */ - data &= ~APIC_ICR_BUSY; - - kvm_apic_send_ipi(apic, (u32)data, (u32)(data >> 32)); - kvm_lapic_set_reg64(apic, APIC_ICR, data); - trace_kvm_apic_write(APIC_ICR, data); - return 0; -} - static int kvm_lapic_msr_read(struct kvm_lapic *apic, u32 reg, u64 *data) { u32 low; From patchwork Fri Jul 19 23:51:00 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13737570 Received: from mail-pf1-f201.google.com (mail-pf1-f201.google.com [209.85.210.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B66AC149DFB for ; Fri, 19 Jul 2024 23:51:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721433078; cv=none; b=opTVoXew4EFyM07rCVAwb3u9Dqv37N/SW4H32YzYHaB2hTRxy9KxeBIg8k44wqYEbwT4K+o99YmO3fBL2sVuRXwTzopxe0/ORjTYlR+GBFe/4g5WMNO2b2EuSXRrR5xMyA2gRVT9et5CZNWWnMUaOST1XdYQgzLywDlgSM55p/g= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721433078; c=relaxed/simple; bh=r48XGddCN80zIaPaynZ1yaj/hfmpfp63eVWhm7RV+0o=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=K8eD9VrwsTVFsIoHmIw6K8W07kwuxQDyXmWrjSK9gjvSCqmIyNutsjxsMp6EwgFs2XbbM148S/vpAz9ZKH8RUD7oh3ZFQB3H14adDLWPALjhVtxRFofVTIweKDVm1O3iTt8jdNhGjk59LzvhkLI2fBLtVAmOVIUmgZkP+gr6Wyw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=I10gYsw1; arc=none smtp.client-ip=209.85.210.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="I10gYsw1" Received: by mail-pf1-f201.google.com with SMTP id d2e1a72fcca58-70af58f79d1so1291378b3a.0 for ; Fri, 19 Jul 2024 16:51:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1721433076; x=1722037876; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=x8igdTZgEeKKRURdE5Y+BIgiNYRFx6FmETZmL9mL1H0=; b=I10gYsw1OmJ9G238f2AVOjK2USeLiu+ugqXft5kU5azCEZLPqD5hd2R3IqeOG+BryS 16ZCWx7wWr3hBpNP2EW1FDWNBA+NCIO/A5edf1wL/xHO/XhdWaX0s9nk0yM4KcRzYymc NJgHsWu1uYYLoXGFWvpjIs9H8QkPpuornuMVI9Su2ZQh5p79vaCXbG05nn9LtJ7teg3t H6UNDRnMICUw6Ez7+LHhQ01JkLeEZW2OL6IyPUHGruYDZ2oJvSm8BhFz2qBhTxo7sXS7 8sHrU3tbiPhX8wXbIClkpHyaDb6mZ0Rnt07iG26qmBoTSWZ0L/BwQQ5DLdWkw44nk4bu M0wQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721433076; x=1722037876; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=x8igdTZgEeKKRURdE5Y+BIgiNYRFx6FmETZmL9mL1H0=; b=Jnlrr8djDWZZAKTBGe5Z99DIREj9upy8Adnlt7k/cQB2PCL5gT09Iw9TUoZfyO9nd9 n8akbbN4kDY+kti3avwon4LbI2oofUUjE/TuZCP//Xx0UqWnCMPBkDIpVL+7njbISIS/ km/sC447P7+e1sqyNGIaUlUX/sBGoer2UNcS7bdzTzpyfQ1ok/U4Nu1qFfhevXgDw92L dVf2s389HzTUSFXRnfTBJc2MHTqkQOgJHBNp20EapmPlHxgipUBs0rL+yaMnXiIWjSI9 ZXrx32GFcZ9pQEKIxSydCkxbZiLT/lK9GL1XYaMefGAsq/o9VknpwK/i2HaJymSqQdO5 fhLQ== X-Gm-Message-State: AOJu0Yw/XSeBAyLdINAMo1aAoRCJyjkrua9NX0Ou8DSyKxm4WrKJxyil o+uSgCw2TQORD+DSGaRUzD3T5f/rVHUvxvyLPgChqtTithM2leitqXCrYQgVXE7Ord/ACpx6Wyi ruA== X-Google-Smtp-Source: AGHT+IHWQ1VfgKKF9QhnVw9NXQr2LiRycoIc4Mz1Fp2W/2qX1ARZMwFUoNwjZVmTkeG0fqWDWFmjmP+JY1U= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6a00:6706:b0:70b:1086:cf8c with SMTP id d2e1a72fcca58-70d087cfaf8mr3891b3a.6.1721433074602; Fri, 19 Jul 2024 16:51:14 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 19 Jul 2024 16:51:00 -0700 In-Reply-To: <20240719235107.3023592-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240719235107.3023592-1-seanjc@google.com> X-Mailer: git-send-email 2.45.2.1089.g2a221341d9-goog Message-ID: <20240719235107.3023592-4-seanjc@google.com> Subject: [PATCH v2 03/10] KVM: x86: Re-split x2APIC ICR into ICR+ICR2 for AMD (x2AVIC) From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Maxim Levitsky , Suravee Suthikulpanit Re-introduce the "split" x2APIC ICR storage that KVM used prior to Intel's IPI virtualization support, but only for AMD. While not stated anywhere in the APM, despite stating the ICR is a single 64-bit register, AMD CPUs store the 64-bit ICR as two separate 32-bit values in ICR and ICR2. When IPI virtualization (IPIv on Intel, all AVIC flavors on AMD) is enabled, KVM needs to match CPU behavior as some ICR ICR writes will be handled by the CPU, not by KVM. Add a kvm_x86_ops knob to control the underlying format used by the CPU to store the x2APIC ICR, and tune it to AMD vs. Intel regardless of whether or not x2AVIC is enabled. If KVM is handling all ICR writes, the storage format for x2APIC mode doesn't matter, and having the behavior follow AMD versus Intel will provide better test coverage and ease debugging. Fixes: 4d1d7942e36a ("KVM: SVM: Introduce logic to (de)activate x2AVIC mode") Cc: stable@vger.kernel.org Cc: Maxim Levitsky Cc: Suravee Suthikulpanit Signed-off-by: Sean Christopherson --- arch/x86/include/asm/kvm_host.h | 2 ++ arch/x86/kvm/lapic.c | 42 +++++++++++++++++++++++---------- arch/x86/kvm/svm/svm.c | 2 ++ arch/x86/kvm/vmx/main.c | 2 ++ 4 files changed, 36 insertions(+), 12 deletions(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index 950a03e0181e..edc235521434 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1726,6 +1726,8 @@ struct kvm_x86_ops { void (*enable_nmi_window)(struct kvm_vcpu *vcpu); void (*enable_irq_window)(struct kvm_vcpu *vcpu); void (*update_cr8_intercept)(struct kvm_vcpu *vcpu, int tpr, int irr); + + const bool x2apic_icr_is_split; const unsigned long required_apicv_inhibits; bool allow_apicv_in_x2apic_without_x2apic_virtualization; void (*refresh_apicv_exec_ctrl)(struct kvm_vcpu *vcpu); diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c index d14ef485b0bd..cc0a1008fae4 100644 --- a/arch/x86/kvm/lapic.c +++ b/arch/x86/kvm/lapic.c @@ -2473,11 +2473,25 @@ int kvm_x2apic_icr_write(struct kvm_lapic *apic, u64 data) data &= ~APIC_ICR_BUSY; kvm_apic_send_ipi(apic, (u32)data, (u32)(data >> 32)); - kvm_lapic_set_reg64(apic, APIC_ICR, data); + if (kvm_x86_ops.x2apic_icr_is_split) { + kvm_lapic_set_reg(apic, APIC_ICR, data); + kvm_lapic_set_reg(apic, APIC_ICR2, data >> 32); + } else { + kvm_lapic_set_reg64(apic, APIC_ICR, data); + } trace_kvm_apic_write(APIC_ICR, data); return 0; } +static u64 kvm_x2apic_icr_read(struct kvm_lapic *apic) +{ + if (kvm_x86_ops.x2apic_icr_is_split) + return (u64)kvm_lapic_get_reg(apic, APIC_ICR) | + (u64)kvm_lapic_get_reg(apic, APIC_ICR2) << 32; + + return kvm_lapic_get_reg64(apic, APIC_ICR); +} + /* emulate APIC access in a trap manner */ void kvm_apic_write_nodecode(struct kvm_vcpu *vcpu, u32 offset) { @@ -2495,7 +2509,7 @@ void kvm_apic_write_nodecode(struct kvm_vcpu *vcpu, u32 offset) * maybe-unecessary write, and both are in the noise anyways. */ if (apic_x2apic_mode(apic) && offset == APIC_ICR) - WARN_ON_ONCE(kvm_x2apic_icr_write(apic, kvm_lapic_get_reg64(apic, APIC_ICR))); + WARN_ON_ONCE(kvm_x2apic_icr_write(apic, kvm_x2apic_icr_read(apic))); else kvm_lapic_reg_write(apic, offset, kvm_lapic_get_reg(apic, offset)); } @@ -3005,18 +3019,22 @@ static int kvm_apic_state_fixup(struct kvm_vcpu *vcpu, /* * In x2APIC mode, the LDR is fixed and based on the id. And - * ICR is internally a single 64-bit register, but needs to be - * split to ICR+ICR2 in userspace for backwards compatibility. + * if the ICR is _not_ split, ICR is internally a single 64-bit + * register, but needs to be split to ICR+ICR2 in userspace for + * backwards compatibility. */ - if (set) { + if (set) *ldr = kvm_apic_calc_x2apic_ldr(*id); - icr = __kvm_lapic_get_reg(s->regs, APIC_ICR) | - (u64)__kvm_lapic_get_reg(s->regs, APIC_ICR2) << 32; - __kvm_lapic_set_reg64(s->regs, APIC_ICR, icr); - } else { - icr = __kvm_lapic_get_reg64(s->regs, APIC_ICR); - __kvm_lapic_set_reg(s->regs, APIC_ICR2, icr >> 32); + if (!kvm_x86_ops.x2apic_icr_is_split) { + if (set) { + icr = __kvm_lapic_get_reg(s->regs, APIC_ICR) | + (u64)__kvm_lapic_get_reg(s->regs, APIC_ICR2) << 32; + __kvm_lapic_set_reg64(s->regs, APIC_ICR, icr); + } else { + icr = __kvm_lapic_get_reg64(s->regs, APIC_ICR); + __kvm_lapic_set_reg(s->regs, APIC_ICR2, icr >> 32); + } } } @@ -3214,7 +3232,7 @@ static int kvm_lapic_msr_read(struct kvm_lapic *apic, u32 reg, u64 *data) u32 low; if (reg == APIC_ICR) { - *data = kvm_lapic_get_reg64(apic, APIC_ICR); + *data = kvm_x2apic_icr_read(apic); return 0; } diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index c115d26844f7..04c113386de6 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -5049,6 +5049,8 @@ static struct kvm_x86_ops svm_x86_ops __initdata = { .enable_nmi_window = svm_enable_nmi_window, .enable_irq_window = svm_enable_irq_window, .update_cr8_intercept = svm_update_cr8_intercept, + + .x2apic_icr_is_split = true, .set_virtual_apic_mode = avic_refresh_virtual_apic_mode, .refresh_apicv_exec_ctrl = avic_refresh_apicv_exec_ctrl, .apicv_post_state_restore = avic_apicv_post_state_restore, diff --git a/arch/x86/kvm/vmx/main.c b/arch/x86/kvm/vmx/main.c index 0bf35ebe8a1b..a70699665e11 100644 --- a/arch/x86/kvm/vmx/main.c +++ b/arch/x86/kvm/vmx/main.c @@ -89,6 +89,8 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .enable_nmi_window = vmx_enable_nmi_window, .enable_irq_window = vmx_enable_irq_window, .update_cr8_intercept = vmx_update_cr8_intercept, + + .x2apic_icr_is_split = false, .set_virtual_apic_mode = vmx_set_virtual_apic_mode, .set_apic_access_page_addr = vmx_set_apic_access_page_addr, .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl, From patchwork Fri Jul 19 23:51:01 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13737571 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B37DC14A086 for ; Fri, 19 Jul 2024 23:51:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721433080; cv=none; b=tavHpfvjbh08eHit9gOjoTEOj8E3VfMdpJJKyGwChC8ZAA7BNnkjPBieWXXNOB1ckEWQxJsStJgk/f2H2k3r8uVpD6FSy1s2P0sQpDygxwxqODKBJhUMfJboAu/320MkBtiovlyifevLiVe1lOtQviuMPj8UwDAg01WJoVF5xZo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721433080; c=relaxed/simple; bh=L5K4LhEvErOvub+DyS3BuApRHh5XgLwLfLGmErCpdIg=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=eJlR9rsipOTh52R/DIIcKS/JdNBHCQEB3PlecKLrDKGjE29uGODdFxDVFOjALOQIso5Cqj/sgbeTP9NbJ75HhOhlyNACHZ4QEO82hSe4vsdLcoiaapTtQjc8KaeSbiFEMXevUt1E8XE6W0Q9CR3Zg/0i4IwZ94i+rtsknq/UgQ8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=flYsAvdj; arc=none smtp.client-ip=209.85.219.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="flYsAvdj" Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-e035949cc4eso5682483276.1 for ; Fri, 19 Jul 2024 16:51:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1721433077; x=1722037877; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=szsmqea3Ly+/pHf0iLcDxQ8R3kri7Zn2BloJwbPXzbQ=; b=flYsAvdjZTwuhmxBu59ntx1ZwFjQitc6jnnLtYYl+YyKVEqYqB7IX1H7eqjJ5SgaGS fJ38h7aamBCwQEOys03wXDBoIdmsJwVuxi/u1MSo9CgNxsxkmwxrNENtVLJHQx04GbiJ uvG0DgV7LByW+/KRKru68mSKbDjFqbrkJMzeaNJVTJ4IruAh7UF4t7AVy3doqVsDA3WI QxXGKXW0tMQiYdMzqHh0Q0vJVtomrfktf7eIUwYbp5VogoZKQ+kirvWg+vRqmcyOde/p 2cQxvVrelQce2ivSrSmId0v+TReJng20MFK6M7JYaNtBOnCgFnZg4sqMcqAhzQcsAwL4 nEQA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721433077; x=1722037877; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=szsmqea3Ly+/pHf0iLcDxQ8R3kri7Zn2BloJwbPXzbQ=; b=kfOSAk6f2zvjAGEaPTg67ma/nch6sol2UwRqO8VFK/TSBAqY48YxqawOWQcD19baQ1 j1O89mvrVyhhmXVrMyd4BmjRsaesfIfGNL9ikeRPMiE5VuQ8mFj06HCPq16ephKFvRFW fLIIIt1ya7nyu7ik4xhlEBZMVN8kqjWRik+oiXvOebSIt50A+VyJagitJMIkJjTrMKA7 BPoxDUofDwC24SfcCHfLyJ0yZh1wSiSWzApBIcPsf4zTgFhaPh0mIrRh3cFOZz30AgbQ +IyTsNXadMPviwYMWvKeQVxnrgzBgIr7zl0sT1veeg7cBtCxGWUNfBsBFmbO+0PEyDsM V2Eg== X-Gm-Message-State: AOJu0Yy75cGhbFEal7gDSaq6HYAGu/Dk6tN6/CQaYfccXUJX4yF1FbQ4 8YRa1Qt27nP/On36pw2DoV6T62NhXxGQl9tNSP1DKXGv0fPxMJOt+5mZ1+HSlVlNbdhHGiuUOqb Z6w== X-Google-Smtp-Source: AGHT+IG+Kf34xxLKIMRA93LE8zHki7PaXFwYp13mgJqswzo6PchKoevDMqejbvrPRDDVtschS9qg6QN+O5I= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6902:1509:b0:e03:5a51:382f with SMTP id 3f1490d57ef6-e087042262bmr9466276.8.1721433077656; Fri, 19 Jul 2024 16:51:17 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 19 Jul 2024 16:51:01 -0700 In-Reply-To: <20240719235107.3023592-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240719235107.3023592-1-seanjc@google.com> X-Mailer: git-send-email 2.45.2.1089.g2a221341d9-goog Message-ID: <20240719235107.3023592-5-seanjc@google.com> Subject: [PATCH v2 04/10] KVM: selftests: Open code vcpu_run() equivalent in guest_printf test From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Maxim Levitsky , Suravee Suthikulpanit Open code a version of vcpu_run() in the guest_printf test in anticipation of adding UCALL_ABORT handling to _vcpu_run(). The guest_printf test intentionally generates asserts to verify the output, and thus needs to bypass common assert handling. Open code a helper in the guest_printf test, as it's not expected that any other test would want to skip _only_ the UCALL_ABORT handling. Signed-off-by: Sean Christopherson --- .../testing/selftests/kvm/guest_print_test.c | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/tools/testing/selftests/kvm/guest_print_test.c b/tools/testing/selftests/kvm/guest_print_test.c index 8092c2d0f5d6..bcf582852db9 100644 --- a/tools/testing/selftests/kvm/guest_print_test.c +++ b/tools/testing/selftests/kvm/guest_print_test.c @@ -107,6 +107,21 @@ static void ucall_abort(const char *assert_msg, const char *expected_assert_msg) expected_assert_msg, &assert_msg[offset]); } +/* + * Open code vcpu_run(), sans the UCALL_ABORT handling, so that intentional + * guest asserts guest can be verified instead of being reported as failures. + */ +static void do_vcpu_run(struct kvm_vcpu *vcpu) +{ + int r; + + do { + r = __vcpu_run(vcpu); + } while (r == -1 && errno == EINTR); + + TEST_ASSERT(!r, KVM_IOCTL_ERROR(KVM_RUN, r)); +} + static void run_test(struct kvm_vcpu *vcpu, const char *expected_printf, const char *expected_assert) { @@ -114,7 +129,7 @@ static void run_test(struct kvm_vcpu *vcpu, const char *expected_printf, struct ucall uc; while (1) { - vcpu_run(vcpu); + do_vcpu_run(vcpu); TEST_ASSERT(run->exit_reason == UCALL_EXIT_REASON, "Unexpected exit reason: %u (%s),", @@ -159,7 +174,7 @@ static void test_limits(void) vm = vm_create_with_one_vcpu(&vcpu, guest_code_limits); run = vcpu->run; - vcpu_run(vcpu); + do_vcpu_run(vcpu); TEST_ASSERT(run->exit_reason == UCALL_EXIT_REASON, "Unexpected exit reason: %u (%s),", From patchwork Fri Jul 19 23:51:02 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13737572 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C194914A0A7 for ; Fri, 19 Jul 2024 23:51:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721433082; cv=none; b=KSLI3OjVyj0rOO1rtI8wQqSzXwkKKBJB8GEA0IDDIz8OGyPmjyrtb46j1CJZZ4XP7uHAL0neEs8CcUNG4BQ6RqO6FA2T8Wj5YE6PMXFmWokv2f5POiA3O925+x9Lc/REEuPsqvRtGMy/3SiEtNBocSmWIUNyKeFZwDAXnjNQm+E= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721433082; c=relaxed/simple; bh=PPn7G+f4jddXFxEDGFar00uqs+OIt5lJ4PTBlh2FQQA=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Qtmwph1CNoyxd6IPTk6/Wl+Sos2rC59JBP3rOn/6LjYEH9vSfbxQh6GcRLGYJvcoj26HD2aC2guPTFuvQe+cZhLP4dVdOqRJ+7oHqlGB253Hcwilku/YtRV9ZCYRer9hnLkJuSjhVtNnbd/iQabZ+LSwLYX5wsdhrIIJz2/WvHc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=pZjBILDz; arc=none smtp.client-ip=209.85.128.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="pZjBILDz" Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-66619cb2d3eso69476167b3.2 for ; Fri, 19 Jul 2024 16:51:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1721433079; x=1722037879; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=nPt/EFV+Nq2FbP/DjJ0bFlMBOKC3xPMXdgZnshJlA9c=; b=pZjBILDzGMLIzTMk4KSIwXeP5e6nq/sdp4wgqzhrdQd96GqoCjJT23WBQmt8lvNeov 2MWeQOssg1MaTIuvHFQd+AfWEwl9QF7lD1XQegKSJO7rr8KILs/oznyMWhLKdHeYbWtp 41N4eeqngBaUh3bNppkfLxf4RfQbjW8FO3faMeR3HobYD+WTXdPYi1CmL2Fey2PvZqCd jYAjiRu8Wg66/AXxJ6O/hLI4oatpRyPCDRGMkukNMD4dkVaOz4ZmhxQSVQHSYNU4OtH/ GX25wdlO5iW5ijvfkr4m4W/zKhFOe/NgqEN5bxoNPFaUEQyN1A04LNlmsC2F5eaV1yI/ 2LUQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721433079; x=1722037879; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=nPt/EFV+Nq2FbP/DjJ0bFlMBOKC3xPMXdgZnshJlA9c=; b=IjEfINuDEgcwpY+KF2VQhVAEIjONb/pU7DRe0QNngdiRG7F/c+GH0A31Za8D9FFM1F Co7m0mCSLOYKjUZuVMK5CM6qh9iTSTJM0vKdp2CyOL1FVQIKV8oktZtXei30s9fVh1Rj 1l3A7IdUn/JJoSo/SEES9YZfj7Y5y9ffSy5EeavQFTmOc0IpTZgnVZmJPx2kcMoliOeT +Y+Sq6lcuHf6w7knYW6XFNwWOX4COoB0I1n3CCaV+ZKyTfDiNu/8TXBw71Div2g3xj8q xMgvKi7+Nk+GHsk4vxJ/LjFaMskW4paWa0/Q2Kz+o4kX140F6YrEkOY5+hUlG13NP2ZZ A2GA== X-Gm-Message-State: AOJu0Yyx1R/bv8VpjXdoEfUuSmvDfoyLAewwJepCxYJxXp9jqhAXv/sn E51gaKXARz5K94qjwzifS16QBWHGtAZ2uXWgtOwLMUafySCSf4eWRCw4QOPxZbxCaFTLFa3ZzrT eLQ== X-Google-Smtp-Source: AGHT+IFZOc2svS1nRDHn9/lIMIWxUAGZrgOUxvS3LRxd2PpdM60KCUus+surZW2V4+iDZWwX226BH+eHTGY= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6902:727:b0:e03:a2f7:72e with SMTP id 3f1490d57ef6-e086fcb6929mr2828276.0.1721433079385; Fri, 19 Jul 2024 16:51:19 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 19 Jul 2024 16:51:02 -0700 In-Reply-To: <20240719235107.3023592-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240719235107.3023592-1-seanjc@google.com> X-Mailer: git-send-email 2.45.2.1089.g2a221341d9-goog Message-ID: <20240719235107.3023592-6-seanjc@google.com> Subject: [PATCH v2 05/10] KVM: selftests: Report unhandled exceptions on x86 as regular guest asserts From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Maxim Levitsky , Suravee Suthikulpanit Now that selftests support printf() in the guest, report unexpected exceptions via the regular assertion framework. Exceptions were special cased purely to provide a better error message. Convert only x86 for now, as it's low-hanging fruit (already formats the assertion in the guest), and converting x86 will allow adding asserts in x86 library code without needing to update multiple tests. Once all other architectures are converted, this will allow moving the reporting to common code, which will in turn allow adding asserts in common library code, and will also allow removing UCALL_UNHANDLED. Signed-off-by: Sean Christopherson --- tools/testing/selftests/kvm/lib/x86_64/processor.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/tools/testing/selftests/kvm/lib/x86_64/processor.c b/tools/testing/selftests/kvm/lib/x86_64/processor.c index 153739f2e201..814a604c0891 100644 --- a/tools/testing/selftests/kvm/lib/x86_64/processor.c +++ b/tools/testing/selftests/kvm/lib/x86_64/processor.c @@ -566,10 +566,8 @@ void route_exception(struct ex_regs *regs) if (kvm_fixup_exception(regs)) return; - ucall_assert(UCALL_UNHANDLED, - "Unhandled exception in guest", __FILE__, __LINE__, - "Unhandled exception '0x%lx' at guest RIP '0x%lx'", - regs->vector, regs->rip); + GUEST_FAIL("Unhandled exception '0x%lx' at guest RIP '0x%lx'", + regs->vector, regs->rip); } static void vm_init_descriptor_tables(struct kvm_vm *vm) @@ -611,7 +609,7 @@ void assert_on_unhandled_exception(struct kvm_vcpu *vcpu) { struct ucall uc; - if (get_ucall(vcpu, &uc) == UCALL_UNHANDLED) + if (get_ucall(vcpu, &uc) == UCALL_ABORT) REPORT_GUEST_ASSERT(uc); } From patchwork Fri Jul 19 23:51:03 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13737573 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 613E914A4D2 for ; Fri, 19 Jul 2024 23:51:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721433084; cv=none; b=FV4B2upnZ2gjN3W2I7U6VvLeT8EePRvok1fanATCDBw8cS+dAIwtdwLCaMYTasQr6bdQtz8Z3oaBwUiaLlaKz9265WnTs594rwZeqMuBxkdCg7mgAGYxXwlIL2DHgsxmYDsL60L5GxkEi0TXrN0DEOOYOKtGuOveitaBA34wnDA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721433084; c=relaxed/simple; bh=avuIVrOvbdJuGswu1VBc5QXgq1mmaRUWopHFZ+/wLdQ=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=RgFVUIO6cG1Srww0BqsrBlYP2s7U1nNxAdxJ9eftlYpyVHaUT0RldLOXuQef+PV4gYfmmgiZ8dzlLHhAaWxTDxBL6Ja/tJ3tB4ku4Oc9xS0oUTPB0/U+TdK6mIk3BiAB0UyF+kTv0dFen71yBcDKX9Ph2i7BrUuHPyFUrXchKW0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=eOAWMULj; arc=none smtp.client-ip=209.85.128.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="eOAWMULj" Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-66890dbb7b8so50143357b3.0 for ; Fri, 19 Jul 2024 16:51:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1721433081; x=1722037881; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=vV3aMaVHf7C/0akufsxFVZJRmuCa+4Ea2SM/3XMiVaU=; b=eOAWMULjI0sxK5+DAGTf7g9lHPawrWWDsxm1ws1SCXQnGgMmZlrpwdvAKK7GIiqYM/ 86KTwrWcZOkwk7sU/2lG9+C+pPS+aOu3skk+15ZSsWbZXh357kpP52cV6LIaXA9WXY6h XZacye6vD82sbm6lh+YBso2VPAnffT8ty12duHvIQ10PyaYDF8jrzeH1NYR+TzbOcrls CLRqv2lUm6tC0DqTkqJeCf1A8mOAz/aX2lzGpAs2KwolKFs9IAeD1hyKRhE2EuBPVrnA hpDs8Nr+nLZVF9W7EJ7QHudRi4WsIGGTcEgbl2OpPSsOCGvuSqXa/kkwlkZi41i08PHI DiDw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721433081; x=1722037881; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=vV3aMaVHf7C/0akufsxFVZJRmuCa+4Ea2SM/3XMiVaU=; b=G8gpzsWTqwx2xqTZUGC/ZMoHyVqEzZYVWAuqGUm/W5Dm+aEdbrlIaCWQi9ZQbxBxhl Og7DjEbV7p6Gved3qfgoJfSdxxcbyeCrgLkB6oPMp3khWTzfwmYS5HQwCDTcDhAPwRnE MRbuCO2HJOVd8/F98fEUToAKSJGrhcC3OV7V/IYRUSgl6HKwf5xmJ7qKipUlSAKSFO9u w7dU3w8tL4ySVBe27y1d4z3gclUW5zBvPBs0sGPTPKDS1chq5xxixk71xrewa6jZ0Bz7 xZCn91O7JPECSlVw/y1UfOJgjvft6EycEgyWrBzJhOBIYx+9KC0cLXnww31iS0m7owOk ubZg== X-Gm-Message-State: AOJu0YxznguP4MzUYkxUSrLR8HpTlpki7U7gg57CogiZ1W+zbrhxgZ1v HY56B8A5CT3/Z/GuM3Pi7C5ddU8RpRu84w9QuKMkRudLK5fPA/fR/5fAY4inUneF2U1i4XBPLr5 tGg== X-Google-Smtp-Source: AGHT+IFx9+7vK2ERpoBYVxnQ2HTxUS6m7HbUD6i5XHwtnqVPJXqKCxVy91eJGzC7dRUx4lbD788BVlCbsAI= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6902:100c:b0:e03:b3e8:f9a1 with SMTP id 3f1490d57ef6-e086fe45084mr25809276.2.1721433081278; Fri, 19 Jul 2024 16:51:21 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 19 Jul 2024 16:51:03 -0700 In-Reply-To: <20240719235107.3023592-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240719235107.3023592-1-seanjc@google.com> X-Mailer: git-send-email 2.45.2.1089.g2a221341d9-goog Message-ID: <20240719235107.3023592-7-seanjc@google.com> Subject: [PATCH v2 06/10] KVM: selftests: Add x86 helpers to play nice with x2APIC MSR #GPs From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Maxim Levitsky , Suravee Suthikulpanit Add helpers to allow and expect #GP on x2APIC MSRs, and opportunistically have the existing helper spit out a more useful error message if an unexpected exception occurs. Signed-off-by: Sean Christopherson --- .../selftests/kvm/include/x86_64/apic.h | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/kvm/include/x86_64/apic.h b/tools/testing/selftests/kvm/include/x86_64/apic.h index 0f268b55fa06..51990094effd 100644 --- a/tools/testing/selftests/kvm/include/x86_64/apic.h +++ b/tools/testing/selftests/kvm/include/x86_64/apic.h @@ -11,6 +11,7 @@ #include #include "processor.h" +#include "ucall_common.h" #define APIC_DEFAULT_GPA 0xfee00000ULL @@ -93,9 +94,27 @@ static inline uint64_t x2apic_read_reg(unsigned int reg) return rdmsr(APIC_BASE_MSR + (reg >> 4)); } +static inline uint8_t x2apic_write_reg_safe(unsigned int reg, uint64_t value) +{ + return wrmsr_safe(APIC_BASE_MSR + (reg >> 4), value); +} + static inline void x2apic_write_reg(unsigned int reg, uint64_t value) { - wrmsr(APIC_BASE_MSR + (reg >> 4), value); + uint8_t fault = x2apic_write_reg_safe(reg, value); + + __GUEST_ASSERT(!fault, "Unexpected fault 0x%x on WRMSR(%x) = %lx\n", + fault, APIC_BASE_MSR + (reg >> 4), value); } +static inline void x2apic_write_reg_fault(unsigned int reg, uint64_t value) +{ + uint8_t fault = x2apic_write_reg_safe(reg, value); + + __GUEST_ASSERT(fault == GP_VECTOR, + "Wanted #GP on WRMSR(%x) = %lx, got 0x%x\n", + APIC_BASE_MSR + (reg >> 4), value, fault); +} + + #endif /* SELFTEST_KVM_APIC_H */ From patchwork Fri Jul 19 23:51:04 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13737574 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F398914A60D for ; Fri, 19 Jul 2024 23:51:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721433085; cv=none; b=IYnQmJIyO5ZDiN/+ucGQFRtO9E6ZDY6NGWejUcwn2k4SX72vHVhldwR0IF8gdfrRzuwhNLOgmUSP6WGLY9d5XS8ekYlKPKnSF2oaKQdzP8zzHeKAsLPmYeG6H7Xb5XCDy4QKdbl64GTOlqURHF3SeLVYqCwgQQjXudkgrhSVSY4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721433085; c=relaxed/simple; bh=vGX+QjydJu3zU32DeuD0uDs+BGgsKqP86RvQO+BJ6uk=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=PzyugtlHWi2B2GjShXctAFPngizTTzElYyXoVZQbduLenaiofpNwpJ5c3vpQd5HG0ANOsUO/ax8Dp335y2KTWJvP0xV/bPM0Xiga8OIMMaWztT7WUcvBelmTaB8Iv0YFOoHUrqQ5SHuQsG1B3bQPhl5atUf3hac5z+xD+2Ks3oM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=sT9zKYe2; arc=none smtp.client-ip=209.85.216.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="sT9zKYe2" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-2cb4c2276b6so1991107a91.1 for ; Fri, 19 Jul 2024 16:51:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1721433083; x=1722037883; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=ZUvOecK3sLTMz6qm67Q0shiPwc10UQD2irey1Qg3WXo=; b=sT9zKYe2AnbCTWsxr2d+fvVP4mKJUgIgXJYnlFGwbN2u1npFeDMpIcc3z3jjWOTQSY H1zxBQ0kXPJL+zhopTFnpLjeoU1Kl0GDPM4hL3UNX+dkgZZShZDl5USm+o4L29nKeCz5 KOyMqlnUxXNiuvLojxG9ZSbEkI0yQZwTZ4NQM5uN0iH78cf1KErmABawHv1CbQbMpsZ4 ZO+VAeLMLsd+Dz8+YMBUWGUXDhplT+ByCmoTGiGSRBjG7eS9Bx2Ve6yGPE2FxApaAvWO IwFBp4Ekb2SBl6EH+fX3jyvtCcgLUNcdVeoOILasvKyMw+NMnMOlyaUrazK60oGQzR5x hlnQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721433083; x=1722037883; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=ZUvOecK3sLTMz6qm67Q0shiPwc10UQD2irey1Qg3WXo=; b=gyJtixgOH8Pv5sXBeOz4MnA4mB1Hco3SnMSMv9oHQtJ7XbJgVJfEarEZEGxg9BGPAJ 4C4F+6Njes2dq99PXqqLOn5eX2PquqIJoZa8R/sBiw08s04WRaDxpTUK01uudOPfILK9 A892N5RMpqyYopuBfWuanL0olwgbELjuVNlfxS/nF87PUtiLrj+mjDXnJVednZDtVinE mHDdBH3kY7tN7WNqdfaEIffXGQWG3pTQ5J60pQRFiCx/Ac3s1tUNsIvkoqpj7tBflHfM 0ccRYJ5Vi1uPMrDMFXs/mZJ2k1UimGQEfK0kSc6qL1z8UQzVBgWlgDpaMX+1I9ocIcD0 sd9Q== X-Gm-Message-State: AOJu0YwBiMr8QLWvewZ1RMQl0Fg2SdPc3dTvDDmTw10+bc0P+894Kfyb pjsyomQIib+0GIcq/8NgUKeShHsRmubwduBfGwgConXla4MINPaGiqLOFo1SPpohxXxht0iIkDK KJg== X-Google-Smtp-Source: AGHT+IH4xR/BOfWKOPUMASMusbY5ub+urdfsnucw/9o+x1nLQ9TwhoA/c/J2omSZr9UBr+XODY5GuQ6L3iI= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:90a:fe8f:b0:2c9:ba2b:42ac with SMTP id 98e67ed59e1d1-2cd1618bd8amr3900a91.4.1721433083102; Fri, 19 Jul 2024 16:51:23 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 19 Jul 2024 16:51:04 -0700 In-Reply-To: <20240719235107.3023592-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240719235107.3023592-1-seanjc@google.com> X-Mailer: git-send-email 2.45.2.1089.g2a221341d9-goog Message-ID: <20240719235107.3023592-8-seanjc@google.com> Subject: [PATCH v2 07/10] KVM: selftests: Skip ICR.BUSY test in xapic_state_test if x2APIC is enabled From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Maxim Levitsky , Suravee Suthikulpanit Don't test the ICR BUSY bit when x2APIC is enabled as AMD and Intel have different behavior (AMD #GPs, Intel ignores), and the fact that the CPU performs the reserved bit checks when IPI virtualization is enabled makes it impossible for KVM to precisely emulate one or the other. Signed-off-by: Sean Christopherson --- .../selftests/kvm/x86_64/xapic_state_test.c | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/tools/testing/selftests/kvm/x86_64/xapic_state_test.c b/tools/testing/selftests/kvm/x86_64/xapic_state_test.c index 69849acd95b0..928d65948c48 100644 --- a/tools/testing/selftests/kvm/x86_64/xapic_state_test.c +++ b/tools/testing/selftests/kvm/x86_64/xapic_state_test.c @@ -70,12 +70,10 @@ static void ____test_icr(struct xapic_vcpu *x, uint64_t val) vcpu_ioctl(vcpu, KVM_GET_LAPIC, &xapic); icr = (u64)(*((u32 *)&xapic.regs[APIC_ICR])) | (u64)(*((u32 *)&xapic.regs[APIC_ICR2])) << 32; - if (!x->is_x2apic) { + if (!x->is_x2apic) val &= (-1u | (0xffull << (32 + 24))); - TEST_ASSERT_EQ(icr, val & ~APIC_ICR_BUSY); - } else { - TEST_ASSERT_EQ(icr & ~APIC_ICR_BUSY, val & ~APIC_ICR_BUSY); - } + + TEST_ASSERT_EQ(icr, val & ~APIC_ICR_BUSY); } #define X2APIC_RSVED_BITS_MASK (GENMASK_ULL(31,20) | \ @@ -91,7 +89,15 @@ static void __test_icr(struct xapic_vcpu *x, uint64_t val) */ val &= ~X2APIC_RSVED_BITS_MASK; } - ____test_icr(x, val | APIC_ICR_BUSY); + + /* + * The BUSY bit is reserved on both AMD and Intel, but only AMD treats + * it is as _must_ be zero. Intel simply ignores the bit. Don't test + * the BUSY bit for x2APIC, as there is no single correct behavior. + */ + if (!x->is_x2apic) + ____test_icr(x, val | APIC_ICR_BUSY); + ____test_icr(x, val & ~(u64)APIC_ICR_BUSY); } From patchwork Fri Jul 19 23:51:05 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13737575 Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B372F14B947 for ; Fri, 19 Jul 2024 23:51:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721433088; cv=none; b=JVCMXFyW2zPIPk2/qmo67Dz0jhxhdpuwc9+j4zkK8SVBfrUNB7Wny7oRScQ/vGWdER4ZOSCicprnuxTs7ZByY4D3F+dxGp2rhqinRa3u/kskFzv7APfO53LmM1NV1ZLKMKJ0973hD+h/Lw+i9LPGpHjFYii2ENLITQ+9kUulUzc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721433088; c=relaxed/simple; bh=M/NyQgjGXBaNreFjHyLA402m2eagNzOE5eYuTkfJG8U=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=jrMttNuwGot/gZCuQNg7yqU6N/MrWNPO6Pjqa9iG8WgItrT/ld1kLAnDTzisRibDDOgqTMZUpO2k2wkttEEV6Rl2ESOLjPECyDUhPktrVUp3K852P8oxFXqyCCFsxsEJKtFqQ9LzQZHhvE1rlGkNOSo6gTs7AlauCbI90TKghhA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=zD+eUP1W; arc=none smtp.client-ip=209.85.214.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="zD+eUP1W" Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-1fc5652f7d4so23106175ad.2 for ; Fri, 19 Jul 2024 16:51:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1721433085; x=1722037885; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=rky9lB8r38gMZ+TqqtRst26vC75RUEgf6EDR/NOmmsM=; b=zD+eUP1WU7wGtbT4m/PC+U/wBpDtGL4qu9fL1n2HR3cPceaYkiEThnGBJOjyLhdv9i WfIwiJmjthRQjNYEdf3X7M1EzFBF2ba+EGTKG+OsJ+DgXPb7Hb/xdmYduFxA0lIQ7G/o aoNhsVBGTz+MWJEKbAIen3NJ3R/d5y+ttABkELN3e4VHR1KIqvtQ3WoVP7fGexIlCNFM vr3W8J7jc5dcsb7RgIP84LmuKwHAPerACUsKAubTLSY5uIlLzhLoMqWNRIsNtOvhLRk3 Z0kwzzK8Kqcu7jwW2Cqu6gDrWpu4iP+B3oVPNslxLMs4YbFECs7A3qAApxDGN+0VnS9J 4+fA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721433085; x=1722037885; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=rky9lB8r38gMZ+TqqtRst26vC75RUEgf6EDR/NOmmsM=; b=BAJqOMK8sFjtOugz5YEEl/RBM3yEtiQr84tjmG/anV7J4eGz1Q9pvXGk/lcSSRlqni oLchRITGpE9dmk35giLU46D3MHfHqU079LPQkCSDK9erzZ8bjV2YhPV9CUTBm7laQMlD PYQK9LlwohEdOsl03O12Oc1M3WUWE/tnOF4pkuRYbihO/qpCz60roQKhEe3yn2FRpRXa IpPuEnKVjfnb+t1v5apKa9dCMaaBOZ8nC+CPqYTAESnOUVyDixxXR9jAxWw+eWMes9IR w5iOsbza0pt0rIB+MQr+RaItmG7DEBBJXYwWJ442oK1uaKtuSradYfJ2ZM+8jQF80MIW uy2g== X-Gm-Message-State: AOJu0Yyw8TD79zZ5UblulbTFA2aDcTdBkjOP7cF9s2OaGGCLotHdxolX goRAB6JZKqy+QcAOi8hfVxVFpVY07EKJYkXknp1kuomonteRb+m38b5AVj3461Q1VndHfXPj2ox VDg== X-Google-Smtp-Source: AGHT+IGSRWf9jMvU/TzDtViOp5LXg8rQIWEJDZtRAQmAnRvU6S8IZvjXlJh7nuKM59TgEcdGyP5wOFgiCak= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:902:cecb:b0:1fa:9149:4979 with SMTP id d9443c01a7336-1fd7453544amr1146075ad.2.1721433085107; Fri, 19 Jul 2024 16:51:25 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 19 Jul 2024 16:51:05 -0700 In-Reply-To: <20240719235107.3023592-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240719235107.3023592-1-seanjc@google.com> X-Mailer: git-send-email 2.45.2.1089.g2a221341d9-goog Message-ID: <20240719235107.3023592-9-seanjc@google.com> Subject: [PATCH v2 08/10] KVM: selftests: Test x2APIC ICR reserved bits From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Maxim Levitsky , Suravee Suthikulpanit Actually test x2APIC ICR reserved bits instead of deliberately skipping them. The behavior that is observed when IPI virtualization is enabled is the architecturally correct behavior, KVM is the one who was wrong, i.e. KVM was missing reserved bit checks. Fixes: 4b88b1a518b3 ("KVM: selftests: Enhance handling WRMSR ICR register in x2APIC mode") Signed-off-by: Sean Christopherson --- .../selftests/kvm/x86_64/xapic_state_test.c | 23 ++++++++----------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/tools/testing/selftests/kvm/x86_64/xapic_state_test.c b/tools/testing/selftests/kvm/x86_64/xapic_state_test.c index 928d65948c48..d701fe9dd686 100644 --- a/tools/testing/selftests/kvm/x86_64/xapic_state_test.c +++ b/tools/testing/selftests/kvm/x86_64/xapic_state_test.c @@ -31,6 +31,10 @@ static void xapic_guest_code(void) } } +#define X2APIC_RSVD_BITS_MASK (GENMASK_ULL(31, 20) | \ + GENMASK_ULL(17, 16) | \ + GENMASK_ULL(13, 13)) + static void x2apic_guest_code(void) { asm volatile("cli"); @@ -41,7 +45,10 @@ static void x2apic_guest_code(void) uint64_t val = x2apic_read_reg(APIC_IRR) | x2apic_read_reg(APIC_IRR + 0x10) << 32; - x2apic_write_reg(APIC_ICR, val); + if (val & X2APIC_RSVD_BITS_MASK) + x2apic_write_reg_fault(APIC_ICR, val); + else + x2apic_write_reg(APIC_ICR, val); GUEST_SYNC(val); } while (1); } @@ -72,24 +79,14 @@ static void ____test_icr(struct xapic_vcpu *x, uint64_t val) (u64)(*((u32 *)&xapic.regs[APIC_ICR2])) << 32; if (!x->is_x2apic) val &= (-1u | (0xffull << (32 + 24))); + else if (val & X2APIC_RSVD_BITS_MASK) + return; TEST_ASSERT_EQ(icr, val & ~APIC_ICR_BUSY); } -#define X2APIC_RSVED_BITS_MASK (GENMASK_ULL(31,20) | \ - GENMASK_ULL(17,16) | \ - GENMASK_ULL(13,13)) - static void __test_icr(struct xapic_vcpu *x, uint64_t val) { - if (x->is_x2apic) { - /* Hardware writing vICR register requires reserved bits 31:20, - * 17:16 and 13 kept as zero to avoid #GP exception. Data value - * written to vICR should mask out those bits above. - */ - val &= ~X2APIC_RSVED_BITS_MASK; - } - /* * The BUSY bit is reserved on both AMD and Intel, but only AMD treats * it is as _must_ be zero. Intel simply ignores the bit. Don't test From patchwork Fri Jul 19 23:51:06 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13737576 Received: from mail-pg1-f201.google.com (mail-pg1-f201.google.com [209.85.215.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CD3A514B967 for ; Fri, 19 Jul 2024 23:51:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721433089; cv=none; b=hml688OGkCs6iuPGWyjiwW05yP08NX/+33CHgzkGPVER4q0v/OdvOfaDMsGKW5QB4ayNBwdC/YL5KC+241Ot1vquDsVok+BZTS2+4dysfFKau/sckxKO52GXgnJAXuPTa3ixq91jeg14CwlP26sCDndypHOFxVq07AGANPhZWxo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721433089; c=relaxed/simple; bh=koF0ilUHAjbeODWkxNP1nhd3CUNaryDa0xwHrMXzgqs=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=hpmv41cLDutR+zYE/X9ctT3Xwlk1Bd6JrO7ArzXoqwt8e86Kpaf0+hYjd8N7PMEDMdHhIAXlkQXW+qgG2AvOIFz+CYhSVHruwo6/t9+FaWYe9kpQHHy0mspjHOGlEU1Hraraz7KZ2NuzxQhBgZwMVQ+HgTe7dtLHSnfkEQIqszg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=UEdMJcA3; arc=none smtp.client-ip=209.85.215.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="UEdMJcA3" Received: by mail-pg1-f201.google.com with SMTP id 41be03b00d2f7-7163489149fso2187956a12.3 for ; Fri, 19 Jul 2024 16:51:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1721433087; x=1722037887; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=8Sk1Boj3nBGrPa6ow6mxcb0YRG2ZhhxIoTDChRs8X2U=; b=UEdMJcA3b4AFw8eYBudHXNKiUiUaVAYX4Fol+5+jc4okUfbiTZUO7vrs5itHHIucEp pcqd97LvKFOlPhls0z72K2oPac9x0XtJRtnlP1/Klmae3nM5VpyaHQRFdVrMCBnVgb3k /vgA+MOTq08XTIXnRkd2WSy7JWMCFOiqTeoFhYdYCKCMq3TQhs5F15CXhtaNl1kwVgwc BEp3Y86QwTWmUqp9pZrjhIYWnqkAfaJ7eT1RUhkoAhgB05cRsX3amnxWDLGKr9nBayqL 4ZKb4+0hdt111qBC+riDjKcjpoScSvM5hnueozuhQ600Ckk3J3tTm/d+MeSKBQNJ7nrY tY2g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721433087; x=1722037887; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=8Sk1Boj3nBGrPa6ow6mxcb0YRG2ZhhxIoTDChRs8X2U=; b=mS5j3nT7A6qdlHXEX2VMk8hocFWQBhDaYrHP5+WTIE3VCLJECBCTJJkhrYuL4E8HoG 5+3D317DWRKLwlTGdcOuyJX3llFgoszm6BgM5U15M6ug+o/ikzy7uaYDqa5WZXaQdWU1 uMFTj3bUuQofg+AUEY+5PWmIJzNIeX18Qg2+FM40bpAinSHl/pLI28IRoRkHmLrpJlTy f+wRfA0FXi8wQRjxAK9Ce6nt1K4ptAWeM2ZkkozZfm0sY3h/d18y55ytubF7Q3WaQoPF 5/MP3bP9WVR9jB4iF7okIRPZBqHKof6wZJl4LnPQRSSs2zNHRJ39HBFhTu8/XuyhfRzK /OXQ== X-Gm-Message-State: AOJu0YxznPtEe88RZUoi1lUFlT1SJn4CQ4PnLXr6b8RL722Rg4q6YaJZ z6Eds4W2XJJftp3LWKvp//TQURD/rnLiQEgzrNIgQ4efkKG3SFV3vhjp0lXv85oliDTahSJIjY8 ueQ== X-Google-Smtp-Source: AGHT+IHS2YxJ7KX/UH9tkB2CnA3jvZqUjqWzd4OIqvGM0Un8cvSzhcEo9bYt2q8f59pE8zSx6ZQrF8NDDIc= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a63:191a:0:b0:6e5:62bf:f905 with SMTP id 41be03b00d2f7-79fa29df2a0mr2918a12.10.1721433087010; Fri, 19 Jul 2024 16:51:27 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 19 Jul 2024 16:51:06 -0700 In-Reply-To: <20240719235107.3023592-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240719235107.3023592-1-seanjc@google.com> X-Mailer: git-send-email 2.45.2.1089.g2a221341d9-goog Message-ID: <20240719235107.3023592-10-seanjc@google.com> Subject: [PATCH v2 09/10] KVM: selftests: Verify the guest can read back the x2APIC ICR it wrote From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Maxim Levitsky , Suravee Suthikulpanit Now that the BUSY bit mess is gone (for x2APIC), verify that the *guest* can read back the ICR value that it wrote. Due to the divergent behavior between AMD and Intel with respect to the backing storage of the ICR in the vAPIC page, emulating a seemingly simple MSR write is quite complex. Signed-off-by: Sean Christopherson --- tools/testing/selftests/kvm/x86_64/xapic_state_test.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tools/testing/selftests/kvm/x86_64/xapic_state_test.c b/tools/testing/selftests/kvm/x86_64/xapic_state_test.c index d701fe9dd686..a940adf429ef 100644 --- a/tools/testing/selftests/kvm/x86_64/xapic_state_test.c +++ b/tools/testing/selftests/kvm/x86_64/xapic_state_test.c @@ -45,10 +45,12 @@ static void x2apic_guest_code(void) uint64_t val = x2apic_read_reg(APIC_IRR) | x2apic_read_reg(APIC_IRR + 0x10) << 32; - if (val & X2APIC_RSVD_BITS_MASK) + if (val & X2APIC_RSVD_BITS_MASK) { x2apic_write_reg_fault(APIC_ICR, val); - else + } else { x2apic_write_reg(APIC_ICR, val); + GUEST_ASSERT_EQ(x2apic_read_reg(APIC_ICR), val); + } GUEST_SYNC(val); } while (1); } From patchwork Fri Jul 19 23:51:07 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13737577 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 740CB1487FF for ; Fri, 19 Jul 2024 23:51:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721433091; cv=none; b=RmCiSCzc6UaCYaKf5wT/OqyQjjlaBrxmEH9EatlGCjXcjjUGnZYEdh40/aqmMPx3zNNNiI+zKLcSLDjBJHhOArBkyhH3BXrZK5bt6LpGQ8F178M+UExaP/Mcz9Aij0uLoEXOOdn5Tdzx7lq2+BrB5dK78aTg8Kml0nN5ot5GTJo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721433091; c=relaxed/simple; bh=LPH8q9W4fShc7MD86NNRowGmzhOW/kmQBwXTLxhGfcE=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=QsG8TjsumNwFCNTKKKekpytcXVxVaXxxyIsSdkz7OdJuVMGRBSzUPdvqc1SEK4wiA7wJDgkeqvDS9UvEtkJBIyGkiH9IsSMwOrR1iZrr0XH3mrCkezVT5yZnwo9a7ZdAM6jVemXJ6XOF1KgPRsocL+jEPPl9FnTpxtknKYYFk6E= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=znOsomye; arc=none smtp.client-ip=209.85.216.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="znOsomye" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-2cb685d5987so2128948a91.2 for ; Fri, 19 Jul 2024 16:51:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1721433089; x=1722037889; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=rEJGCMpMk3RIfu2QMQ4nUitiZAubqCnC3LYp6iaILlU=; b=znOsomyeF6udBTvEt251PI3t3XPmo5EOLU5WVaDq8p1zjZuAkAMAVZ8aKmr/dpbrnl nOy3m/I3SUfeZRX+0bDgC3RUvheiYJe1d2jp85ZZ9Gp+25rmqkAiD5sbBLAyoBTMd9hd SDoJSP56C/Emkq6BdKwsDb2xePyRq+BSP3bYXY1B9GgsmXzDSQhdfVxyz4nd2Dn+CtuO cUIlDIOgUb75OMkQ5Utxr7faP0eZ0EgkwLV13OG4Z3X5K4y7GmRnktkEaADMprFdfCbN mLpW3UsHSVme845xuKp6RoP4zI2NL6U+RQM0GA7WJuqNYVqPMMC2ZQlXc0fClqK4KwRk M4ww== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721433089; x=1722037889; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=rEJGCMpMk3RIfu2QMQ4nUitiZAubqCnC3LYp6iaILlU=; b=aCBVeVZ5HDMsHaPZA/wFeTyVC2tZAHvdixDAA9moKFHQKawIyl28A9TQEdyPg3F0Ot aZbIEzROr2sKKqFTNDcHBOz2Yw3VyyVuluELEt4b4onOt58IohsoTRKbJYNi5KkRCVnZ tMu4AEzlNYtvZQK5v3ZDVgvGthYOd+icnQAo9C7YCfC/YbMQvSMiBmHZbgP2eUfoWh+F s5VfEZG7gNhVikaJmmpRwh1wlcajM34eo7Ooq1hhqPfvEXYWw8UqsTh0ictkExLwY/aV DWeEXkAl+u/61K9TUPb8X3hFmNbu0313KLsUCjfTbYgpC5nBluyn2sum6GgA1bX7I+Aa Jvbw== X-Gm-Message-State: AOJu0Yzl7H8cAZWK/QIfsRkPIHYl5o5pSm+WODH2IQRy4ENPQ9WMi9Ug AHD10lfm5VopvbYCO0E2XSK3RLL3hwhf74TcbKD8FgEmiXDrsqa6IDK2N9JnEPOEnzmouRgxaIh H7g== X-Google-Smtp-Source: AGHT+IFNSTCJ4CN2s2b9vbf+ii17GENMZV/5sakKmq6+fG9OzPne60xcEuzETwhtYlL5iJJ6q//VK1rZGY4= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:90a:7884:b0:2bd:f679:24ac with SMTP id 98e67ed59e1d1-2cd15e5389dmr4232a91.0.1721433088762; Fri, 19 Jul 2024 16:51:28 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 19 Jul 2024 16:51:07 -0700 In-Reply-To: <20240719235107.3023592-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240719235107.3023592-1-seanjc@google.com> X-Mailer: git-send-email 2.45.2.1089.g2a221341d9-goog Message-ID: <20240719235107.3023592-11-seanjc@google.com> Subject: [PATCH v2 10/10] KVM: selftests: Play nice with AMD's AVIC errata From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Maxim Levitsky , Suravee Suthikulpanit When AVIC, and thus IPI virtualization on AMD, is enabled, the CPU will virtualize ICR writes. Unfortunately, the CPU doesn't do a very good job, as it fails to clear the BUSY bit and also allows writing ICR2[23:0], despite them being "RESERVED MBZ". Account for the quirky behavior in the xapic_state test to avoid failures in a configuration that likely has no hope of ever being enabled in production. Signed-off-by: Sean Christopherson --- .../selftests/kvm/x86_64/xapic_state_test.c | 23 +++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/tools/testing/selftests/kvm/x86_64/xapic_state_test.c b/tools/testing/selftests/kvm/x86_64/xapic_state_test.c index a940adf429ef..a72bdc4c5c52 100644 --- a/tools/testing/selftests/kvm/x86_64/xapic_state_test.c +++ b/tools/testing/selftests/kvm/x86_64/xapic_state_test.c @@ -13,6 +13,7 @@ struct xapic_vcpu { struct kvm_vcpu *vcpu; bool is_x2apic; + bool has_xavic_errata; }; static void xapic_guest_code(void) @@ -79,12 +80,17 @@ static void ____test_icr(struct xapic_vcpu *x, uint64_t val) vcpu_ioctl(vcpu, KVM_GET_LAPIC, &xapic); icr = (u64)(*((u32 *)&xapic.regs[APIC_ICR])) | (u64)(*((u32 *)&xapic.regs[APIC_ICR2])) << 32; - if (!x->is_x2apic) - val &= (-1u | (0xffull << (32 + 24))); - else if (val & X2APIC_RSVD_BITS_MASK) + if (!x->is_x2apic) { + if (!x->has_xavic_errata) + val &= (-1u | (0xffull << (32 + 24))); + } else if (val & X2APIC_RSVD_BITS_MASK) { return; + } - TEST_ASSERT_EQ(icr, val & ~APIC_ICR_BUSY); + if (x->has_xavic_errata) + TEST_ASSERT_EQ(icr & ~APIC_ICR_BUSY, val & ~APIC_ICR_BUSY); + else + TEST_ASSERT_EQ(icr, val & ~APIC_ICR_BUSY); } static void __test_icr(struct xapic_vcpu *x, uint64_t val) @@ -209,6 +215,15 @@ int main(int argc, char *argv[]) vm = vm_create_with_one_vcpu(&x.vcpu, xapic_guest_code); x.is_x2apic = false; + /* + * AMD's AVIC implementation is buggy (fails to clear the ICR BUSY bit), + * and also diverges from KVM with respect to ICR2[23:0] (KVM and Intel + * drops writes, AMD does not). Account for the errata when checking + * that KVM reads back what was written. + */ + x.has_xavic_errata = host_cpu_is_amd && + get_kvm_amd_param_bool("avic"); + vcpu_clear_cpuid_feature(x.vcpu, X86_FEATURE_X2APIC); virt_pg_map(vm, APIC_DEFAULT_GPA, APIC_DEFAULT_GPA);