Received: by 2002:a05:7412:8521:b0:e2:908c:2ebd with SMTP id t33csp2337272rdf; Mon, 6 Nov 2023 11:01:30 -0800 (PST) X-Google-Smtp-Source: AGHT+IFz/sehWhMUgDSV11VWhuGL4aAHAdZ+sj+HmJ1rsr0bLUekPwMMdQlagV2OoC7X+8FiE9UW X-Received: by 2002:a05:6870:ff0b:b0:1e9:9989:33a5 with SMTP id qn11-20020a056870ff0b00b001e9998933a5mr597099oab.5.1699297290502; Mon, 06 Nov 2023 11:01:30 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1699297290; cv=none; d=google.com; s=arc-20160816; b=aTROguEeN//8wdukSLPvhItONN+0VGVV5R5ewgxlEf6Y8FaGbmwLaHfOJHHeVm1maj DZ+oaRttMJOwciIloGwca8MYHH93B0gM2VrYo1jCNGjZvk/lZEv+x2JSkPr6mY/mYP7s pU8jUXc7tehxiFmz2L1m2oJXndjvDyHIj4rtdgq1pnxaMQbzsASWh0EF+cfPtKRLQ/YF UtbASUfgH3ZubAPeOTyGcttSwjWvo5cS/iNoBxOV+4K0v5kWO1TN3zONDpAat5d144R3 AUXep7+td6mQatGe2vypx4Fqa/qcqTQNqQpFrFEtk38CB56NoGf7fUoClzK71zeOLjVr HiuQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:from:subject :message-id:references:mime-version:in-reply-to:date:dkim-signature; bh=PGkM+GRz/6fYflQxsLrTt3gzPDlcKqRYPaToqANSy+c=; fh=AEB/Qw9IonDhjFU/1n2f/mT5c/qVgyTY2b2Iv+zVo5Y=; b=IHpawjEaAt/e83D5GPnh1jtJyOGELRLmK7gCGGWIaANegQKefElJJgEJRHL+dNrjp6 TCRHzQ0sb/9TYC3DaIUJo9LmhZmkEaCi9wGi8uOLaB0hiSsA1bzn+T4Kms5yipLxMRAY 6vajOLs2HHVbRGCl2Qjo0owOxcUIh1i1V7MAhlNn0MbGg5mRQx/uTfBfvdwNEPm4131Y 4j4s4EJ+gntHJ1QopyhcSIlaBBxRGUbMH9o92sEerrpAyv9zh8M5ScnXd1Hoh2CPM5I6 vD8GSCPkWl8LTHUrSkbZQ9OW5F1DdVE92xpWo8d07Mwr80mSZzJhdMOynYvX0mIJUph5 Mt1g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=2+OIpzdY; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from lipwig.vger.email (lipwig.vger.email. [2620:137:e000::3:3]) by mx.google.com with ESMTPS id w20-20020a056870a2d400b001e11adaa9ecsi3539746oak.246.2023.11.06.11.01.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 Nov 2023 11:01:30 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) client-ip=2620:137:e000::3:3; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=2+OIpzdY; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id D84568032EF1; Mon, 6 Nov 2023 11:01:23 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232266AbjKFTBP (ORCPT + 99 others); Mon, 6 Nov 2023 14:01:15 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59004 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232041AbjKFTBN (ORCPT ); Mon, 6 Nov 2023 14:01:13 -0500 Received: from mail-pl1-x64a.google.com (mail-pl1-x64a.google.com [IPv6:2607:f8b0:4864:20::64a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 29E11D8 for ; Mon, 6 Nov 2023 11:01:09 -0800 (PST) Received: by mail-pl1-x64a.google.com with SMTP id d9443c01a7336-1cc3ad55c75so31266275ad.0 for ; Mon, 06 Nov 2023 11:01:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1699297268; x=1699902068; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:from:to:cc:subject:date:message-id :reply-to; bh=PGkM+GRz/6fYflQxsLrTt3gzPDlcKqRYPaToqANSy+c=; b=2+OIpzdYdxqDj04JgzfNtedMgHDoURt26Pdyn3DhZnXLqalChyShHj1tM11pDsKmNf 3tirJqFkUo/eezyxQpKXC0pBCXGlDImuabh5fTuZYCOvdbQHu9ZPLZtRedJGAV6ldTrn xr4xEfcqdpl0Gdw+Bfi20uLKW3PEUTc87XXL/bkV/puol379wwbPJs9AG4drYU2eFGEu nEIowM4lnXqC636o6DX91HlQzDIrVKepU4FoASjgKr5zyky0AkWh++oNrOdgs9m+ozRX 4XQh4MgnpU3Hf5DXONlq5uB7Um46cLt2cByfh0hBBvi3D4QuHFOQUhZTGuqxpFfe8heG mTiQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1699297268; x=1699902068; h=content-transfer-encoding:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:x-gm-message-state:from:to:cc:subject :date:message-id:reply-to; bh=PGkM+GRz/6fYflQxsLrTt3gzPDlcKqRYPaToqANSy+c=; b=WFhjxXo6bApEgxzePS2JYj9Q5hjhqEFGVcGElA6DSjqTpnSHjhwAQf1ruqIVujG+gT mgQY1tNaIhtv4bDJI7pTe26xgnoqQ+3gZKXBASVQ+kMPbbVXP0GUmxSiFIaflIX0lJFZ RYCGNuD1RK+Go+kxzFszLEs1XZGNZId93C/iGKOZXEeGAEeVxle15Z6vVPgRb5o1HUWj WFLbU7NLWrSVzJ+0O6hjDRpL3dLcex/druLF3mClOlsx3vkyBJywac2hliaeSsZiyyIN vDCmKZ0BFpzg/Gvb2lAWqHnJc6LLgXSGJh3yPv8Uk9hDxeW6k+JM/qM4cQ/1dwDFjv4V wQYA== X-Gm-Message-State: AOJu0Yy/s9QGuDK4H3PytiDDjCpQOw05HYQxOkB6mLmhjGK4Gx7Zgk8a 8p5z38ftfFxpU231mfmviUDWSuali2U= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:902:ee14:b0:1ca:b952:f5fa with SMTP id z20-20020a170902ee1400b001cab952f5famr504750plb.5.1699297268653; Mon, 06 Nov 2023 11:01:08 -0800 (PST) Date: Mon, 6 Nov 2023 11:01:07 -0800 In-Reply-To: Mime-Version: 1.0 References: <20231104000239.367005-1-seanjc@google.com> <20231104000239.367005-7-seanjc@google.com> Message-ID: Subject: Re: [PATCH v6 06/20] KVM: selftests: Add vcpu_set_cpuid_property() to set properties From: Sean Christopherson To: Jim Mattson Cc: Paolo Bonzini , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Kan Liang , Dapeng Mi , Jinrong Liang , Like Xu , Aaron Lewis Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (lipwig.vger.email [0.0.0.0]); Mon, 06 Nov 2023 11:01:24 -0800 (PST) On Sat, Nov 04, 2023, Jim Mattson wrote: > On Fri, Nov 3, 2023 at 5:02=E2=80=AFPM Sean Christopherson wrote: > > > > From: Jinrong Liang > > > > Add vcpu_set_cpuid_property() helper function for setting properties, a= nd > > use it instead of open coding an equivalent for MAX_PHY_ADDR. Future v= PMU > > testcases will also need to stuff various CPUID properties. > > > > Signed-off-by: Jinrong Liang > > Co-developed-by: Sean Christopherson > > Signed-off-by: Sean Christopherson > > --- > > .../testing/selftests/kvm/include/x86_64/processor.h | 4 +++- > > tools/testing/selftests/kvm/lib/x86_64/processor.c | 12 +++++++++--- > > .../kvm/x86_64/smaller_maxphyaddr_emulation_test.c | 2 +- > > 3 files changed, 13 insertions(+), 5 deletions(-) > > > > diff --git a/tools/testing/selftests/kvm/include/x86_64/processor.h b/t= ools/testing/selftests/kvm/include/x86_64/processor.h > > index 25bc61dac5fb..a01931f7d954 100644 > > --- a/tools/testing/selftests/kvm/include/x86_64/processor.h > > +++ b/tools/testing/selftests/kvm/include/x86_64/processor.h > > @@ -994,7 +994,9 @@ static inline void vcpu_set_cpuid(struct kvm_vcpu *= vcpu) > > vcpu_ioctl(vcpu, KVM_GET_CPUID2, vcpu->cpuid); > > } > > > > -void vcpu_set_cpuid_maxphyaddr(struct kvm_vcpu *vcpu, uint8_t maxphyad= dr); > > +void vcpu_set_cpuid_property(struct kvm_vcpu *vcpu, > > + struct kvm_x86_cpu_property property, > > + uint32_t value); > > > > void vcpu_clear_cpuid_entry(struct kvm_vcpu *vcpu, uint32_t function); > > void vcpu_set_or_clear_cpuid_feature(struct kvm_vcpu *vcpu, > > diff --git a/tools/testing/selftests/kvm/lib/x86_64/processor.c b/tools= /testing/selftests/kvm/lib/x86_64/processor.c > > index d8288374078e..9e717bc6bd6d 100644 > > --- a/tools/testing/selftests/kvm/lib/x86_64/processor.c > > +++ b/tools/testing/selftests/kvm/lib/x86_64/processor.c > > @@ -752,11 +752,17 @@ void vcpu_init_cpuid(struct kvm_vcpu *vcpu, const= struct kvm_cpuid2 *cpuid) > > vcpu_set_cpuid(vcpu); > > } > > > > -void vcpu_set_cpuid_maxphyaddr(struct kvm_vcpu *vcpu, uint8_t maxphyad= dr) > > +void vcpu_set_cpuid_property(struct kvm_vcpu *vcpu, > > + struct kvm_x86_cpu_property property, > > + uint32_t value) > > { > > - struct kvm_cpuid_entry2 *entry =3D vcpu_get_cpuid_entry(vcpu, 0= x80000008); > > + struct kvm_cpuid_entry2 *entry; > > + > > + entry =3D __vcpu_get_cpuid_entry(vcpu, property.function, prope= rty.index); > > + > > + (&entry->eax)[property.reg] &=3D ~GENMASK(property.hi_bit, prop= erty.lo_bit); > > + (&entry->eax)[property.reg] |=3D value << (property.lo_bit); >=20 > What if 'value' is too large? >=20 > Perhaps: > value <<=3D property.lo_bit; > TEST_ASSERT(!(value & ~GENMASK(property.hi_bit, > property.lo_bit)), "value is too large"); Heh, if the mask is something like bits 31:24, this would miss the case whe= re shifting value would drop bits.=20 Rather than explicitly detecting edge cases, I think the simplest approach = is to assert that kvm_cpuid_property() reads back @value, e.g. struct kvm_cpuid_entry2 *entry; entry =3D __vcpu_get_cpuid_entry(vcpu, property.function, property.index); (&entry->eax)[property.reg] &=3D ~GENMASK(property.hi_bit, property.lo_bit= ); (&entry->eax)[property.reg] |=3D value << property.lo_bit; vcpu_set_cpuid(vcpu); /* Sanity check that @value doesn't exceed the bounds in any way. */ TEST_ASSERT_EQ(kvm_cpuid_property(vcpu->cpuid, property), value);