From nobody Mon Feb 9 21:03:36 2026 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 35FEF2BEC4A for ; Tue, 10 Jun 2025 22:57:56 +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=1749596278; cv=none; b=NPQE0iNFroXiSYPaop5W9EfXVOyNODCzeMzoCp4dQJH5RuRxBMfnrqdNZA8BxWRBD4QZUvxO6SvlR+EK2kujTlLyd4spkBXJYtrk6Z+pMZHEUzVbvBNgnvRwnGSpbYNlsWMFxzlIa7h0Kxpw+lIoxjjLkSjhZWY9EHWrmK4D6Ns= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749596278; c=relaxed/simple; bh=MDOUH3X6wq/mglGD6uhAQsU6Et3gPFf5mZVecvvR/9I=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=oQaxqAt7Ugab83iwiF6PceZ1iJ7eoBjXXVp3GtmVbp+3fZ7jDn+u2s2yWOYYvZO4+NH9JHFGgTpxPd2sYoDMvwprYS/K3+OHg4lj0eVZc+HI+Q+4vZ2MhttUlrUkJSSijPwn3wuFkKSjnD2s8WFsQxEUMfYbABVsJN2FnYz5rTs= 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=HflR/2iq; 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="HflR/2iq" Received: by mail-pf1-f201.google.com with SMTP id d2e1a72fcca58-7377139d8b1so4922227b3a.0 for ; Tue, 10 Jun 2025 15:57:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1749596276; x=1750201076; 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=BWoe3RUxKXBLyv8Si/dsvC7/cgdlk+MDhj94IyKDEDU=; b=HflR/2iq7dL2gImevWzs6jvbe047NoKFWpV4a8B+Nqn5auALDCD+p20jVg9sz5VDwn UTOw3rdfwQxsSFrzf3+wJb8gdg82CxhBbcvghkccK4jULG49o9rSS0Ie+KAsNhsya3nb 7htHN/xDEJRSTf4vMc4wz0Cd2/oore/27fCZcpPFg9Umjpz/eU2xHS+4HztFuNZVNyYb 5HrJO1xp4PHuUZPp1z3dQNkqatBzCy+7FRBiHu10mxcDSV8E+Z1reIfxyW2gB3nRE6ri NdQMczDt2gqesZFuZUbRmBr/xgM9XtPjPbtrcYSAiokq8RzrPPF3dS3Akw6PAnMASYYD 1gyA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1749596276; x=1750201076; 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=BWoe3RUxKXBLyv8Si/dsvC7/cgdlk+MDhj94IyKDEDU=; b=kR6AswbUXx6LWBUfOOcqpIOnbv9GzswLJGnYrMIGdQypyTkOq+TjkG9pU2zPpbpfRc EByO7apWpwkN74sHl8Lgo73XoR1B6JfR1+FdrzSn0UPeo/eqhda1fXaNZqIwD4HE+gDz KiZOoRW8h3SsQaFP57IRVQ5Bdoy3BLgZeaVPDidgc2awKIPKpnfS0yEFKjnLVbHB9Tih f7ykAILvL9aNWrLzXCy8Dj/fobSbTxrh3wzKJXHp1EAJfqJSxqU43Ln5Ru+VXTQBJVbH Z5dtUTGqjgJA4/gN3ArweYmjlrTbllXYCWubkGOCfcfxJu5TqUAHbMx0RZ802kLy4nLZ S92Q== X-Forwarded-Encrypted: i=1; AJvYcCUMtPdPF6M1jL0Is873bCJHxdzl+A+b4t6C01SA50gkZ77GWQ9vxb6R93wZi89UDC498Pr9A2G4GWJeRtE=@vger.kernel.org X-Gm-Message-State: AOJu0Yypt6OAVG7jmA9oNFfMRl0osJGkgJOSYiuM2vXzQVYKT5/l2RVS XBpyn6ILFTBWEwIi4sUqGFrEYTND61JbIaD/0OJBsWqWYo7A9upaVZaGgG5LUxss5CSUooHE1Ao pSLuRtQ== X-Google-Smtp-Source: AGHT+IEg1w75KYPNRmchxzOO8gESU1MbJ8FnfG5EUTzVHn8nGAguHp/iA+cZNRupVdy01no7GpvgeWcwHnk= X-Received: from pgbaz4.prod.google.com ([2002:a05:6a02:44:b0:b2f:9d37:5774]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a20:72a5:b0:1f5:5a0b:4768 with SMTP id adf61e73a8af0-21f86703596mr1655744637.21.1749596276465; Tue, 10 Jun 2025 15:57:56 -0700 (PDT) Reply-To: Sean Christopherson Date: Tue, 10 Jun 2025 15:57:14 -0700 In-Reply-To: <20250610225737.156318-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250610225737.156318-1-seanjc@google.com> X-Mailer: git-send-email 2.50.0.rc0.642.g800a2b2222-goog Message-ID: <20250610225737.156318-10-seanjc@google.com> Subject: [PATCH v2 09/32] KVM: SVM: Clean up macros related to architectural MSRPM definitions From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Chao Gao , Borislav Petkov , Xin Li , Dapeng Mi , Francesco Lavra , Manali Shukla Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Move SVM's MSR Permissions Map macros to svm.h in antipication of adding helpers that are available to SVM code, and opportunistically replace a variety of open-coded literals with (hopefully) informative macros. Opportunistically open code ARRAY_SIZE(msrpm_ranges) instead of wrapping it as NUM_MSR_MAPS, which is an ambiguous name even if it were qualified with "SVM_MSRPM". Deliberately leave the ranges as open coded literals, as using macros to define the ranges actually introduces more potential failure points, since both the definitions and the usage have to be careful to use the correct index. The lack of clear intent behind the ranges will be addressed in future patches. No functional change intended. Signed-off-by: Sean Christopherson --- arch/x86/kvm/svm/svm.c | 12 ++++-------- arch/x86/kvm/svm/svm.h | 13 ++++++++++++- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index 854904a80b7e..a683602cae22 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -268,22 +268,18 @@ static int tsc_aux_uret_slot __read_mostly =3D -1; =20 static const u32 msrpm_ranges[] =3D {0, 0xc0000000, 0xc0010000}; =20 -#define NUM_MSR_MAPS ARRAY_SIZE(msrpm_ranges) -#define MSRS_RANGE_SIZE 2048 -#define MSRS_IN_RANGE (MSRS_RANGE_SIZE * 8 / 2) - u32 svm_msrpm_offset(u32 msr) { u32 offset; int i; =20 - for (i =3D 0; i < NUM_MSR_MAPS; i++) { + for (i =3D 0; i < ARRAY_SIZE(msrpm_ranges); i++) { if (msr < msrpm_ranges[i] || - msr >=3D msrpm_ranges[i] + MSRS_IN_RANGE) + msr >=3D msrpm_ranges[i] + SVM_MSRS_PER_RANGE) continue; =20 - offset =3D (msr - msrpm_ranges[i]) / 4; /* 4 msrs per u8 */ - offset +=3D (i * MSRS_RANGE_SIZE); /* add range offset */ + offset =3D (msr - msrpm_ranges[i]) / SVM_MSRS_PER_BYTE; + offset +=3D (i * SVM_MSRPM_BYTES_PER_RANGE); /* add range offset */ =20 /* Now we have the u8 offset - but need the u32 offset */ return offset / 4; diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index f1e466a10219..086a8c8aae86 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -613,11 +613,22 @@ static inline void svm_vmgexit_no_action(struct vcpu_= svm *svm, u64 data) svm_vmgexit_set_return_code(svm, GHCB_HV_RESP_NO_ACTION, data); } =20 -/* svm.c */ +/* + * The MSRPM is 8KiB in size, divided into four 2KiB ranges (the fourth ra= nge + * is reserved). Each MSR within a range is covered by two bits, one each= for + * read (bit 0) and write (bit 1), where a bit value of '1' means intercep= ted. + */ +#define SVM_MSRPM_BYTES_PER_RANGE 2048 +#define SVM_BITS_PER_MSR 2 +#define SVM_MSRS_PER_BYTE (BITS_PER_BYTE / SVM_BITS_PER_MSR) +#define SVM_MSRS_PER_RANGE (SVM_MSRPM_BYTES_PER_RANGE * SVM_MSRS_PER_BYTE) +static_assert(SVM_MSRS_PER_RANGE =3D=3D 8192); + #define MSR_INVALID 0xffffffffU =20 #define DEBUGCTL_RESERVED_BITS (~DEBUGCTLMSR_LBR) =20 +/* svm.c */ extern bool dump_invalid_vmcb; =20 u32 svm_msrpm_offset(u32 msr); --=20 2.50.0.rc0.642.g800a2b2222-goog