From patchwork Fri Jul 19 23:43:38 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13737559 Received: from mail-pf1-f202.google.com (mail-pf1-f202.google.com [209.85.210.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 6049C1494A0 for ; Fri, 19 Jul 2024 23:43:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721432632; cv=none; b=mizh1dlOKlzbmH5/OQ/YaJ3RfrAM+hYv/8+EhfLgJ3ENLuNMRSkjt+8m75yPcOa+Jgt8g7ymFC9hTQx9ZsBr8jY7y2U4vm3PlxztGnbA/CyFfogEvGBheBS8fmuT44Jfk5irLo4BMGwVePAQIh7k75YGGMUnM8YyTn8qj0yhKxQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721432632; c=relaxed/simple; bh=9VC1o23EyPnmCsIUXN13mbYo4nMy/PcXMeGVi57HKHs=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=XTSVuIyrwKuS6JKiOeHXJZmgd24ibc9BmE16fBdwZJI5excavu/ptjsABKELGTUpIUvtxQ5Da531ruBavhYtSySHfvKWvUZszvy8/fFla8JSgLqZW5PjD+0/a7ji/6D3cUpRHekg/uRPiQVs/dJ+TqpvodTRbvSmLHC/pwUR4gs= 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=fqtAqOen; arc=none smtp.client-ip=209.85.210.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="fqtAqOen" Received: by mail-pf1-f202.google.com with SMTP id d2e1a72fcca58-70af5f8def2so1324886b3a.2 for ; Fri, 19 Jul 2024 16:43:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1721432630; x=1722037430; 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=fqtAqOenea+jug6vhkcKDsRqx79hWEdz8f73cYIH2iI9nSsvRBd5IAQbwG7NtpzlSC b+l8enHTY862ktDxs+ACVJIfNz6nCYuWLWyP/D8rGk+cvAEYkRtgYcxe9U1BhnHqYGy8 VGaGWGr7lFKj9gTMbaVj2S5gJpeREVaWp5ofet9c1FP5oC1+9XwHnQGPxXWSzeaJoJZZ hploaa4T5/NjXb8dyjkSGQdqNDVO+EUcCe+OAk/WcXDkUb9YXzlHBMgL/YHtJyeabY6U Z6J66QlZaLgVpxEb/gsw2t4xfTp3jYESuZOQIMdWTwcNcSUr38jeP4VL85AW9Bc9ZNht YQfg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721432630; x=1722037430; 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=ooRGoq8k98CHkAV7xgUPbyKJjEg0GaF+cFcCIj8Z85PDz0vUYbol5JYy5jsDaFIiGU qcPwF0CWfS4E0cEv4pbQXLlDNgN7wqA7YzeesiTQ9fw64BARld6GhdwRcE5uEjjn/Jrr /9KFJIubympM6zfJHCkUK8Pk0+Xo924V8KUuIthDThXlXRF25dXxtrB/dZkols1aFUkt EMj226c7Ox2Lsm8O5AbmMoKE2lraGhHNbv6DdJvBSDsal1scQ6lmn0j+AaBX4BQ/9oKZ JKWYGAxo2jMaL+YovhbumXIyMD2JDGi0jcPUS4Rh8vQfKkeDzMywan5eQA+W289BR85X zsAQ== X-Gm-Message-State: AOJu0Yzv7mWM2lqKbDJ5iU1WZ9cMLCzLiAUHC47R3moOYypG7U4SvJlg nises0koqeDELYcDnyI6YWTX0vYVPx/z75olmIomMdVu0d+fJjnejd7J2gs66GrZ6mn4WrCGBWJ UBw== X-Google-Smtp-Source: AGHT+IEiuM3Z91qDjQNlWIAL1YtO0VXEuZifNLzsyyzV/85gD4e0QNR3MJvKRnBVzqGPUl26Tn7sPfzVK4g= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6a00:3991:b0:706:71b3:d7cf with SMTP id d2e1a72fcca58-70d0845c0d5mr6033b3a.0.1721432630110; Fri, 19 Jul 2024 16:43:50 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 19 Jul 2024 16:43:38 -0700 In-Reply-To: <20240719234346.3020464-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240719234346.3020464-1-seanjc@google.com> X-Mailer: git-send-email 2.45.2.1089.g2a221341d9-goog Message-ID: <20240719234346.3020464-2-seanjc@google.com> Subject: [PATCH 1/8] 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:43:39 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13737560 Received: from mail-pf1-f202.google.com (mail-pf1-f202.google.com [209.85.210.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 63D2C1494D7 for ; Fri, 19 Jul 2024 23:43:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721432634; cv=none; b=W0HPsn2z5RI3Lb0NQPpSKsPgQwg0EPJLqihlNkgabEx6tLETcmFIl9EBZ2JKO5GrifDJe34gWe2onBdradKDRVpjslwq9EIrjRayjISmoKqWLStF5CjROoxp2W8i4EJYnVmISZ8RnMoYcQaqPNn7KxlW5TwhHMX08ennK+qvuHg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721432634; c=relaxed/simple; bh=Z1cH1V9K2pPOjjICaFF7xPr76fow0RyXynIXQuiRywA=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=tAvBG54vgguRl1VCzXnxzvlPnfQTL0Y5ww7xmBUJ0YV6JyhZigumxKG0gMIZQ5mx/2x6E7wxwZ1+sUTEj7wk77cEs6vhCi3KXcQ0XnlmJ+70ar0EAagH2jK5TAC4WCU8BmccDKzXbQj2qeC1MJBuPBZPmD6KALUxMhYn7VqFSf0= 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=TPPHYkBy; arc=none smtp.client-ip=209.85.210.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="TPPHYkBy" Received: by mail-pf1-f202.google.com with SMTP id d2e1a72fcca58-70b0bb79c49so1211730b3a.0 for ; Fri, 19 Jul 2024 16:43:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1721432633; x=1722037433; 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=TPPHYkBy3TiUELDRlwMuEx+qBxW/BuO8KYkUDyHRSf5MRNaGhFPDHXmDgsmVwaNDIJ h03JcUYfpPEroZmy0kWXwF2IhAvoCKaKYp1tatz34XiYTzXqudOIWGVsqlgOH1BOJMHP DRV19vp/2yVwlm5/dOIMoynZG9OGr8jYTLrjEvOcGsKb7nGIVzrjUpF0Nlg9g8AX3IKh LPT/cWKLZHsPOa7cV3sTWdOv+zjK6GyAmce0HRb0FbGNyZTycFsqSAYdJeFgy1k8cTK8 TzrICGeUx3icxZ6SBMFaYOx2/VTKm+aKWaBbCWL7EO/3/cjusTZrV0cB7XMxqgurea7e wu9Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721432633; x=1722037433; 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=jnyVY7wWG/KV1EWuRyRbAzF5+7/hryShhy1tpzikoYLfPfLJ6dlLt4NyRYOMnUX5pi mznPgbnWvNPqeYR8le9HwbZtcXvPiZmyjWet8VkGgfDfoI5QKiadvN9eDTe/5J1dFRYA rdYjbpR4F9AHGKn6DzRiztGfQyKWS6vUqwVl9Ija/wZDDQ14LeZJUMD/hsqsRxkMpbU+ kvjKukYiGVVklTILv37q2FW7092BhlqNVYrE6f06iTJec0+FWnDDnnp5j5RtXqFaU8O6 mvaodK2QJWWVaKWM8XMnhPvRIpDkR1pO3/ZX8tujwdsjrHi5P/Zxr8ug7MVZfzV5sBdX ANHg== X-Gm-Message-State: AOJu0Yx7TveC3dh4sA6OUMzX2zD+3qCarvgSVcOKnCx1pT8ZCMPO0f9k IwYHdP5Zt+NmV+P2veErBBlIQi7FLqFSdbdMpysrE5iTIUcMnXTO4oJV8zgBqSsPGA0+h3JS767 idQ== X-Google-Smtp-Source: AGHT+IGrqTi7r+bD33iXiOlKe24I4fRBWbe1ifwKtqgnwHLxRGmTs+zh59kEsCtnf3DhIB+XwN5wBZPlmi0= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:aa7:87d8:0:b0:70a:ffc2:ba with SMTP id d2e1a72fcca58-70d0845b84dmr4399b3a.0.1721432632396; Fri, 19 Jul 2024 16:43:52 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 19 Jul 2024 16:43:39 -0700 In-Reply-To: <20240719234346.3020464-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240719234346.3020464-1-seanjc@google.com> X-Mailer: git-send-email 2.45.2.1089.g2a221341d9-goog Message-ID: <20240719234346.3020464-3-seanjc@google.com> Subject: [PATCH 2/8] 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:43:40 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13737561 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 1D811149C60 for ; Fri, 19 Jul 2024 23:43:55 +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=1721432636; cv=none; b=STdLXUT7LLmeRilHmDSVpLRzel7Ik3pM6ZIQfbdgU6WLzIb35F7dJOQXteqCuyjoSBKq2jlBh1rLkzKbqvrrIeJqIaHl30L6CgI3Os6yCpw3n8W3nrZh9O46LeJr+cKNnnI51kLCoBIvOPrpptiOxNXCdHGatqkD29XawYyrvKI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721432636; c=relaxed/simple; bh=r48XGddCN80zIaPaynZ1yaj/hfmpfp63eVWhm7RV+0o=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=bgg4bLAu03C/pLOUt+cKInDtIWkZgS1UGoghlNYAPwe/veZpUFhcP1DR9lUgCZFocBVCHI+nO/gFKkSh2pupvOGNw4fJgC8qoqF1eEmHNqpmbMOOYzgzx8kVmF3q0NA1RE5J1xg7TUET6DQtRgUzg9z6cgK3Z3riXj5JkOU7bJI= 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=WWB0WDFu; 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="WWB0WDFu" Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-1fc6db23c74so16877425ad.0 for ; Fri, 19 Jul 2024 16:43:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1721432634; x=1722037434; 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=WWB0WDFu/9NtJG3zC2CirGtv825hVJZS0vzaQUEyJSO12S2N9LzwuBaJpSO61fxnxD 3f7q1NZkLo99LUsyXFe5Ph5ZewJx/9EaAI24BKCA2V7KfUXMdGJDY6HYpQc1pgCwPmXr J6rMAycphwPWV7hkbu04bSgT7L4bXjsigiPFZ+oEwn2jBpplBOj0dwthLMHnp6bVLXwq 9wLCYDGKSmd11PnDTEMw7NMPI7gtJDj58Xe+Pnc65X9RA4EA7jFqfyfMWLH4u7w0Cw7B iEG4D1eaMx+WPgrH9s5q+4NWlCKRAZ9Gu/SA3RxGXmeXoTCc1gyF4z0EiHwKdssA+ZP2 nepA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721432634; x=1722037434; 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=DANyHavNjwp/o/wJ+Yx2gm0w9/sLrptudYB1QmzuhwJp3COSDgiEmuuZXhW6TLu03H BGYU/uoLUkhFhjisA3VsdV/Z56ddENIKZNta2YiZsp2IPSApAiIeYh4+fCTDzbP0xR3k viirUB37ycg+ImPS2aBWKWWG8giONo2LUfssIMFKn5H1fiKDGwcFpJBzyKp+uevBybCg 9fqXpiGUZS7hGhtKn1c2VCR6iySP4Dst0ON+K5a7ngMlSf6LSJtTCQS99vCiixdBiz11 PZiXW/EShs/JT1TXLBUs/288wVXx5n1Tb9uTPOcXsWQ6gi5JNApchtUGyhIPz0b24Om0 3AgQ== X-Gm-Message-State: AOJu0YxdVOwncPP/8jtwdac0cONEiuhbNW0sMG1rms9Mz3nGJEP1AXHT VOYjyagRZtGOgr8s6eKRB1Niu+HnZxfvawdSZBAIXxVto5tEZGb5N8rkrznYOGFx6aOskADBeyf ScA== X-Google-Smtp-Source: AGHT+IGaaLuLfDMyX2Ck/Rh10pNVbyLR+y67SoJ0fL3JQMAp89Fz1Hzg3nnGOd+xkFsesKHNd/kOpG2I7nY= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:902:c404:b0:1fb:56c8:f273 with SMTP id d9443c01a7336-1fd74534000mr872005ad.1.1721432634374; Fri, 19 Jul 2024 16:43:54 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 19 Jul 2024 16:43:40 -0700 In-Reply-To: <20240719234346.3020464-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240719234346.3020464-1-seanjc@google.com> X-Mailer: git-send-email 2.45.2.1089.g2a221341d9-goog Message-ID: <20240719234346.3020464-4-seanjc@google.com> Subject: [PATCH 3/8] 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:43:41 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13737562 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 79EA2149E14 for ; Fri, 19 Jul 2024 23:43:57 +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=1721432639; cv=none; b=I5FERSeVX3JTfEbLn5qsnPZ/50OVooHwKvOjYnK5YXDdEqXv/WDz49YOrfGtSLbEjbYSrzQO8jYB2XhcC3jJVWxs22yL4rQqFf3PEERTv1DiyTRkJzynVp1+4BQaBnNXGfzpOKm5ma/11n7tDU0Bvr1SFqBcpaItAyWMx5Y0NFY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721432639; c=relaxed/simple; bh=L5K4LhEvErOvub+DyS3BuApRHh5XgLwLfLGmErCpdIg=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=BBI55JNYoM21nifnWVVRBr06PcDjEkfCRS3Wv4958kOk8HA3978HI5uoS2yenNOIXFw9ox+l0sKllHpuCMM673jvSBvCKD6rQ1TCxYp2ZSrsV+K+OHP2yny/E18yJ6jJOAwOgUXwSBawTRGCSiUbPPodBSP07wUyFP6zgDq0Sbw= 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=cKRfBVO9; 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="cKRfBVO9" Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-650ab31aabdso62151687b3.3 for ; Fri, 19 Jul 2024 16:43:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1721432636; x=1722037436; 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=cKRfBVO9uW3KH4IWeqnJv1Q6J/rkUKH7lgTsQiOSqXHvbkCSXQGD4g0fwOwKT05jeU YBUO2xk1cEPW+ulo5QCgatStBgQkw5CqJUeoch4ZP0PWgohQ5VpFxpmvngbFOW6w8Aol O44pfnUI7xAu5LJ39PI3M1QdiKVA63wMUd2HvedKjR2evyQOKGA9ubfY2p+NSPnrpyvw v552avnLc7skp2ON9z/Qb0HWEw5XE+y/cK/FbMpWf5L2N7TPOP4xCfI0btsEuNu2MGZs bIMrXgOQ2tKd1hk2Aux213mMT7eI1Ce5K9vW19CarrzOrsR165jBBZnKT/tVMCfCyITT awPw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721432636; x=1722037436; 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=fNcaDMSzEb815tHR/x1IYv70IEnfI2Iv6XP5lRBsfMxVD5E3SBKk7gGZxoSoP4oacA I/+GpzSmqD5tcnGnrVGZHglVSE5ae4SwKis40hDhXcHAJgkG1uMgTJ8XWhBBmuwhJ+0O zJYN8l8TGR7FIB5yfrr1STfOGjXHf1w0zaQghpC9USInQXC/yGReZ8ETIUWb8vW2AVHm T9fZk4SxQmJNHlIz6kvs3Lq1o6GJH//KxBn1q8D73s67DjdUMr8vqXPBTYh7ZBD9Pfhh yC8P3xCP1SN21SQuucUWKQgoFrxXlB4ToD4Z9VA10nSKeHYw8Wrv0JuOs3RlVMcbrNyV vgIA== X-Gm-Message-State: AOJu0YyJIkingQkuD64GHSXtOwXgh84mqbRDP0/mk7J9A6hwL8iP5k7V Y2F38ewr+YrgBjtfVGo2hafBbCSxx/7Kzb6Cp9z4WJ9eT7cdfJLGwzP4M/T7pseWtfcpMGkbA3u 0Vg== X-Google-Smtp-Source: AGHT+IHyklNc/kciJMRQw56X5UOMRuQVOpb5WnfAPHuAp+SM9Knp643EKgohR9My7VTvqkjf450OIUMnKJ0= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a25:804e:0:b0:e03:53a4:1a7 with SMTP id 3f1490d57ef6-e087046cademr8377276.10.1721432636436; Fri, 19 Jul 2024 16:43:56 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 19 Jul 2024 16:43:41 -0700 In-Reply-To: <20240719234346.3020464-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240719234346.3020464-1-seanjc@google.com> X-Mailer: git-send-email 2.45.2.1089.g2a221341d9-goog Message-ID: <20240719234346.3020464-5-seanjc@google.com> Subject: [PATCH 4/8] 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:43:42 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13737563 Received: from mail-pf1-f202.google.com (mail-pf1-f202.google.com [209.85.210.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 3096514A092 for ; Fri, 19 Jul 2024 23:43:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721432640; cv=none; b=CbxOY2t1/ebAVge/aimSkDxB05NqhtW8XmLvL23rJKseHp54ydRFVlY4oQA+7B5RgWfoD7BldU86LOm02SSohPhObarvYG14edF7WrwYHEBu8+n2D8345Sh4x7W5WDQUjd8YojHIOUiuC+BrZM7gcLge6ns/PIgQYxjbs/B3BJg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721432640; c=relaxed/simple; bh=PPn7G+f4jddXFxEDGFar00uqs+OIt5lJ4PTBlh2FQQA=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=TB139tiwqSKbNXG1McyzxmWrPngLI+K7e49zDpcjSQyVFP7V0Elob/qE+EGfV8SXCbQqeG94e5QaixmIUB6LYRWBoqAGjCe9uhS3iQHhdZyj12SqwmFZTsoH11CTyLFeFv5LcWeVpHQUMvZvbGmfsRTLFZReJmdX/VcACWCAyG8= 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=3Vq3fpFf; arc=none smtp.client-ip=209.85.210.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="3Vq3fpFf" Received: by mail-pf1-f202.google.com with SMTP id d2e1a72fcca58-70af7dc9780so1325201b3a.1 for ; Fri, 19 Jul 2024 16:43:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1721432638; x=1722037438; 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=3Vq3fpFfznOlU1PmOXKDg2z39TyQyHZf9wYfhsx1zuLWJcaUMGN33VlX40g+DSYnnc wul05y62UHZoao/I1r6FAIBNtEDOQAABOrq1+djwvsWpE8kSU55ZL+NaDZxV3k+HZh0U MabqoUJLcTZ0WskElfDW5SlrJ07bkVVSJxMiOxPHmMRTqizMjQs4Y7T/Ej++VKOp7aVB IkJyGJNY1PCtKTHkRKkC+jAa+3/JY3EBVSBVFdIQDzgoqKh2x2g1wl2iSa4+qJIOdKkK Kgqb7Fd2YZM1XX/ePz6Wcbl8bokbGLJd18u7c40jcDyei6Uk6NwAyTZL7oPouTBREXRN +1Vg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721432638; x=1722037438; 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=HsJoDrJlDMGnF5ulSq77qJTKdmmzVpczwYzPUMmYvFshTcv66Zy30wNgD85piXDfXq uzm+si2TAPDFcQxzNxUv22/MF7QjK4L5hiyb4oI23z4rBooWLp4uu15fIIOib/qaYb/B rVmYGC0meMUcgWg6qeiHUfYpTeGgNcuXKHVvexcJ4oFeuOoBVzsWRnbJLYeJleVg3A0l tqcX+K5sNYKXHNNZmb7dEf2CAQ9YgC5ikOJsUqJfuL/w5F11gAbf9S/voXUzlMooP/wL S6g3rt9wQE10nX46VSP0KHYBLJYj+/6SkXQC2x0HmNqdS+CUXtbsc4VybF/lrUBW9ejv RaoA== X-Gm-Message-State: AOJu0YwT/55c+xk5f4kB3w5yQBqFDbN2IRibi8Jda0cnX6otHxZpCi2K 3wI/bk3NXbfycpsLh7pmqCHgB8mAV6Gsq+n7h/zUlqwIIXUVIk+jEngBg85Zg1FkEBdmtZ4qxuQ Eqw== X-Google-Smtp-Source: AGHT+IH0kpH/UFJ9YnQJ29R7jW5YI6r3QaHGi0uazNzGckCKBUeuNmJ/e1teSmk32abSP1lzY/T/0UsK7vk= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6a00:1314:b0:706:3433:bf21 with SMTP id d2e1a72fcca58-70cfd58f8f9mr47559b3a.3.1721432638145; Fri, 19 Jul 2024 16:43:58 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 19 Jul 2024 16:43:42 -0700 In-Reply-To: <20240719234346.3020464-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240719234346.3020464-1-seanjc@google.com> X-Mailer: git-send-email 2.45.2.1089.g2a221341d9-goog Message-ID: <20240719234346.3020464-6-seanjc@google.com> Subject: [PATCH 5/8] 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:43:43 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13737564 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 057CC14A4DB for ; Fri, 19 Jul 2024 23:44:00 +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=1721432643; cv=none; b=WIi2HNFRi05GWRwlfDLarV7DR8EgQbzQL3FWKbKIGP4qhvbR5WLufkNshQwxXRO/buoVqtVhwAR6EiT3Xle0PGmZ/U2x8sIkhZC3WHWe4Oar5v1A6QoKSWcZvLCt7To28RB+fGh86vlxOEn95eohd3NA1RgpyALASKzESK2nhC8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721432643; c=relaxed/simple; bh=avuIVrOvbdJuGswu1VBc5QXgq1mmaRUWopHFZ+/wLdQ=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=sBsQB467z//q1oXK2l8GyhAfHZ3olmo/2DO4L5/wzkc3v3RosiHW3sUPlAyHPh1V1Xunz3/xMKUuRFnKV9462qE+EO8yjJcFVi3JXrAPh3ff/oruaOeXx63j+hgDBIXfc0osCR5d9p2639Kc/7BZ5hF6sS+VfT4/vIft4xprJZE= 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=0r3601bv; 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="0r3601bv" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-2cb7364bac9so1833523a91.0 for ; Fri, 19 Jul 2024 16:44:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1721432640; x=1722037440; 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=0r3601bvHPZl17MhLs39ASHfYqDDVZLF/ZdoHoubWqtS08h5jfdX3wo6h4JvqIGu0J tYIe8VuuYIr8sgplw+OSabkUbgzNxEIIwY6LXA3uzUyBgnTPL29CoKfMHewytkSl9/Fx OBCJvXZNXggsMqp/Z0+cfUiQ592ek+H0v5pNnAdkFOER2YjnNTOpnZ+bMyl4KVCH8oak qJ2RcM5VLMOInvxks/wg/2rvKU1dUyJyxbkdBl8mHESEbjtI3qvjMpnyR6QePVGywUCX 1tcWhBlbps4P26N9sqTtmwr0V785nb8yqDaIWRCuicYB3DSkR3148sZNdT0+u/T/8A6e gEng== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721432640; x=1722037440; 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=eCtl9wv2s6iia5YqHr3ivSG58Sw3HLKTI6L4iHdWByFDLhr8uFbNaKCYIC2GmUpkSE wsR9saFfs3PPO5OxQyS2s5YRoud8T97uOyueBBvdpetwXLIgYFJLExKiV/SW3kk/pksS Kdl4LDkcpw6Q6BAGLUzA0LOXlgo0aZ2NExHPsqQCiLeOSvcr0+170L8gfIa6U/EnYv4a uqbyFrWQ/QXZsVddV53V5WngHiCyxpKrxekqY/Hve/7drJJ9RM8u2fCQ38i0pH1MNTs1 1BIMYmnGyTe0bbgfgsfcHM22SuZVJQhwt8XImy5las3svX1eFpzwxQ59odbMEr/9Uw4r G8aA== X-Gm-Message-State: AOJu0YylbWl0bAQCllySW4MXwEhA0erPgJ7ROLRflfaE1YcVs4QoMF6q wGo2gDJ4JRqUWRCccvBYA3yMSvDSCRCqNx+fEXxCmLDbRZWlKYxdGsy+IpiLk6MfAtFfAwqmR4H z8Q== X-Google-Smtp-Source: AGHT+IGPvuABTRGzUn95rBWDL/2eBHyGq/tmVyD1jWvRffYkunDVRYZslMTOmS1p0lIemoM5bFBHW+JX+M4= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:90a:7081:b0:2c8:632:7efe with SMTP id 98e67ed59e1d1-2cb7761c6bbmr21715a91.4.1721432640200; Fri, 19 Jul 2024 16:44:00 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 19 Jul 2024 16:43:43 -0700 In-Reply-To: <20240719234346.3020464-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240719234346.3020464-1-seanjc@google.com> X-Mailer: git-send-email 2.45.2.1089.g2a221341d9-goog Message-ID: <20240719234346.3020464-7-seanjc@google.com> Subject: [PATCH 6/8] 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:43:44 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13737565 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 14CE514A606 for ; Fri, 19 Jul 2024 23:44:02 +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=1721432644; cv=none; b=GgOpjD0qCIDH/U+7XOxjdoOOCcqAy/pOghUdIZ+lJsZzsjKQ1Gab3n9VXZGrLhzMLbPxA8Gt9KADBXhX7VSJNBtg4H7/PweOVmiPu5b20b3kOL2A9xPsg1kbmW2C6JSJ5vMijeZtdX8xKZHg1YzOK46irecZ7nNxHPWtgXARAuc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721432644; 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=Thu58ggMUXmCnP8imtGtx/dDYkSOQFn3NlKrp9gp2qZ50ZZaa78EfN7zkRY6iQg3aCIv26Catuzxrgcu6Bx+X11l0Vkx4VzANdM0jenMKt4ABsp89xsPaDj8uaKHhEST/NoDy6C9+bimucTOY0OYIDGVPxCdmpFrlXEttN1dREk= 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=IgFEX+ii; 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="IgFEX+ii" Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-1fd774c3b8eso1750045ad.0 for ; Fri, 19 Jul 2024 16:44:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1721432642; x=1722037442; 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=IgFEX+iim849Wzc++CNUkvAjvUB6Xb6zPl8eSF8bOwiDDAZkZZqS39BN9fNQ5y/btR StMXYkjGqLpS1nq47f1nDQBoDneL22/ZQDYjDnluYzgKINVZyPNjH2tLe85lN0uhafvS yS1O0IrDeOdzrCmP5T9gVSNHhkAXk9SSN6z4GZkKT0EKmVwkRPbEQj1Cr87W9jIyJFKV WNPyFFCG5Efqs34trfgO0FeJXI8p61CqMbzD2tuQYrcx/Xwkm5Pea9QUjljngZ5zdNv6 IELnPCCL03fRlTONGDJxlb1ul75lZ0p9e1I81oGnizxC6fYpWwlOae5QIsEtkv8H+o9a FfvA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721432642; x=1722037442; 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=TKCi+8nioWp79/sMrVUO/r+IVs7NhhOuj+IwSPw91646QPZZeJv0UZJWjszliBBvlw XZa2FepFqxUnLmUKaudX0kc8VoWIKWkb9X78LMWD+XeFaUEpTiiSBdJj8CsaknvsQ5d7 qJ6d152clthBUD9JSGYZlIfPXKoFMiwLCJDQsDQwvKAZGL2WkO1zPKF7xOorY5YYnCPi 8wv014qcs6lwN9+C+fSg8SWByQjqj7hUTTeb9tp2slE3LWqkoe8Ozzoqi6hCDulVJgFd 4Nzcl0l7tO+ABM22c2iVJdIe0qpTJONWbjPMIIFtd8glthnjtb0dZfbMCkcdornVKQty +/jQ== X-Gm-Message-State: AOJu0YyY+s9PvR48l8enJR84Rvd+02lHO/t7PQBUxKvzQ88aBtl5B6mn KWR8HQoH7oJ6sr6TF8EBiZ8glxOC85ZH1q2MLPxaT4/j+jSifJYecXFdwKF5fWGPAg5tepGG5HV N9A== X-Google-Smtp-Source: AGHT+IGnhKiwC1lWgStU0irxOJLtvwWl6nqop+oW+igV346cVb8SlQ4Ca/NDQl0jX64yTdSTKHgW0Kp/9hM= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:902:ea10:b0:1fb:526a:5d60 with SMTP id d9443c01a7336-1fd74d87f6dmr880325ad.4.1721432642189; Fri, 19 Jul 2024 16:44:02 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 19 Jul 2024 16:43:44 -0700 In-Reply-To: <20240719234346.3020464-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240719234346.3020464-1-seanjc@google.com> X-Mailer: git-send-email 2.45.2.1089.g2a221341d9-goog Message-ID: <20240719234346.3020464-8-seanjc@google.com> Subject: [PATCH 7/8] 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:43:45 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13737566 Received: from mail-pl1-f202.google.com (mail-pl1-f202.google.com [209.85.214.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 CBA581494A7 for ; Fri, 19 Jul 2024 23:44:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721432646; cv=none; b=RmAB2uoMjTGI37lAS4nV1neHmcA6KkD27AmAGO54gi+5lBX5dTBr3LU0H2K9FNZE8gQ9gqUVoFbCr02CtYQTi9/ld1AebyJ6bPJm0Xf/CfvxbwaXeQpz2iATAVj6gdkNUV9L3cpJD/Uj9vZOCuVEF/gZj0Mixki/bTLfv+c/5Ko= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721432646; c=relaxed/simple; bh=M/NyQgjGXBaNreFjHyLA402m2eagNzOE5eYuTkfJG8U=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=N8epdSwTuyvcWrPMAfF6bRYO4iXlkKZw1ZZE1RnB7iZFIVotr24xBZTwuvsJHaOSUy7tfvFnRl7t8Ojr6Np8Mj0n9kjRpI74nPnaV/ratYS3Aq/O5lmdFCZQxcMptdQBdjddLwZp+wflg6JhNZWY4vJ43miBlIy/J8DxV49XjnY= 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=VWJ7ayf8; arc=none smtp.client-ip=209.85.214.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="VWJ7ayf8" Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-1fc54c57a92so20469215ad.3 for ; Fri, 19 Jul 2024 16:44:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1721432644; x=1722037444; 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=VWJ7ayf8QKyjGG/QhRF+XCbRmHMnELh3uwSBUOVWI4Rdi+8KdaW3smSE9UWhwMkJll 0vr1QBG4vQSn3cdA96fYAuq7WUAHVH8VtiYgHdAGKB6PgdIxGZ7SD1hpsQQaBFMINpiF GQCS9RqHLpcv7HpIbPncUM3fUkdIIO4savj7NjOZwm0V1ClwDUj5yJ4/YD+vgV9CJSH7 SgooQscw90CRiSdx9b8oj5axGstm5P0S+MYv2+TMvEzr9DI9MdLTqB7Im/uxtYGpWFjd uVIIp49Ml1YrpJ3XmjLTwR5p1eOw0GWLDnWhyGlPoPzt/MS7hUBZeOvS428RB/hvmPjt JJoQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721432644; x=1722037444; 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=sQ3Fw6cAnu2KGCGHULzvuBCNPFCSFFXDDgUgu5/gxAOO9X4QC52d/AV+EWwTJjTIwn P75DweYTZRVnpzshHWtGyn/qmBaiGLd4snM/B1feKvkOld6Lv/cEl1RXb3wwXVtiBh0t H8zSlxDnHZE5PexWwMftFwKLnggUUb/xeOkm6xLlS+fLlxny/Weul2h7LAZO2yJPa2kP NZSv0RSHp2DBJCLdo08gY/7T9w4fW1TMosuIZr4Rh9ab2qw2YnALyiUK7MkHXGnUY7Cd g1J8KVgZdiAh3zjXl4/XCLLc/JJjRiGIjT+hTx8/1FwvQ1uNSqY6J1hyG5RpD8FB82BX 9jHg== X-Gm-Message-State: AOJu0Yz54saspLMjxudaJ4CAVjrK3citwnFxEQambvqeJNXvbo5BDoK2 emOrQJ9HjNF3GVHAtmyYhexEC9IH+13zjXBHbx6ca5rjXakqI5IIWh8Fr9E/F1d2M8WJSLz+EgM Pjw== X-Google-Smtp-Source: AGHT+IHqLL4iTsWdNjx6HxdWSeX/rS7x3vjnktjhHaCJPZN8EdE2np/68Z7gXEq3b6USSD8ByBeBiZlEqhE= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:903:32cf:b0:1f9:ddfe:fdde with SMTP id d9443c01a7336-1fd745c27admr911935ad.9.1721432644115; Fri, 19 Jul 2024 16:44:04 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 19 Jul 2024 16:43:45 -0700 In-Reply-To: <20240719234346.3020464-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240719234346.3020464-1-seanjc@google.com> X-Mailer: git-send-email 2.45.2.1089.g2a221341d9-goog Message-ID: <20240719234346.3020464-9-seanjc@google.com> Subject: [PATCH 8/8] 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