From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id DCADAC4332F for ; Fri, 23 Dec 2022 00:37:26 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229843AbiLWAhZ (ORCPT ); Thu, 22 Dec 2022 19:37:25 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52352 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229506AbiLWAhV (ORCPT ); Thu, 22 Dec 2022 19:37:21 -0500 Received: from mail-pj1-x102f.google.com (mail-pj1-x102f.google.com [IPv6:2607:f8b0:4864:20::102f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DAC48DF3A for ; Thu, 22 Dec 2022 16:37:20 -0800 (PST) Received: by mail-pj1-x102f.google.com with SMTP id p4so3528359pjk.2 for ; Thu, 22 Dec 2022 16:37:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=5qpVu3sN2GuUThZ9RB+4eYZTG5/t0SB6qmR3AbS+l70=; b=B8R3XSgXnZA0zY5ZIdpmzNALRNnKsAfqFMLI/F+HF96zJYmqg1RNfhTugB8bwKI8A2 JS/Y6GRPNC3bxfTQCrdpS/9K/kg2Lr6+RHAiBOrwY5KMWHe7ixr5p9ST9I6V2CdjBoQW utBDn9+m+81ByW1uwDLF2LCDyCILvdtt27LVTPhR5xnYw17Qhp8OzxxUN+OC0WbfgeyF OJHSL9IpNLDgSNZ8DTQRpKT2MgorAXNG7vnzvJERj00cpKqFnRlol438XrI30+88e00T aTN9PmkIoWMfS2sV12MkOTVjfYk520RDEMd6Mi5dYYe4rhkOYnXeilicT614LDtZ6Pug UAHA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=5qpVu3sN2GuUThZ9RB+4eYZTG5/t0SB6qmR3AbS+l70=; b=WUvwyK9Fz7901TW4Y+vCt8wAjcqAwJAcn3t7UZevCUH52N1goiBvIGV1pVPs/+4i4u l8KA/+w7pQEzJi23X38rtkvvtJxntm5XNw1V0dRU5m07NMJLI6N03Q4y9TYvwC1TbIxb w6vTPB+QXp+MBl2/6yMbFnMlOh242zKNBZkvEL22oMeaYhXm/55g1KUQLtcWo4K/p0Oy R0GQk+6tXHoFs5OJUHdtUNcr+BeAnn6CNRt4TwqDPfuQ4qugfI0Rm4nUGU9JwzcQunPt bB8idmYhU9t3DpLBTVxI6HFjH8Qwgme4m4Km2jGP8faz7Jn9jHS/KxWin/4nWGkgH/64 o0Dw== X-Gm-Message-State: AFqh2kpBGE6V3NyKCeXyrV9CvulBjZri8u5W88zzVROFFMYFYh6/w039 lfyHk3S7Nz5aJ/USdqDLVzjyxg== X-Google-Smtp-Source: AMrXdXsjr7g6zJgmvJf2nNP86FhM3dZ2aK3tAps43yzHeFgEfsriOphjFvjgXLdFPCH3uTD/vS0vNQ== X-Received: by 2002:a17:90a:408d:b0:223:e2df:8030 with SMTP id l13-20020a17090a408d00b00223e2df8030mr1083496pjg.2.1671755840213; Thu, 22 Dec 2022 16:37:20 -0800 (PST) Received: from google.com (7.104.168.34.bc.googleusercontent.com. [34.168.104.7]) by smtp.gmail.com with ESMTPSA id l7-20020a17090b078700b00218fb3bec27sm1103723pjz.56.2022.12.22.16.37.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Dec 2022 16:37:19 -0800 (PST) Date: Fri, 23 Dec 2022 00:37:16 +0000 From: Sean Christopherson To: Vishal Annapurve Cc: x86@kernel.org, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, pbonzini@redhat.com, shuah@kernel.org, bgardon@google.com, oupton@google.com, peterx@redhat.com, vkuznets@redhat.com, dmatlack@google.com, pgonda@google.com, andrew.jones@linux.dev Subject: Re: [V3 PATCH 2/2] KVM: selftests: x86: Add native hypercall support Message-ID: References: <20221222230458.3828342-1-vannapurve@google.com> <20221222230458.3828342-3-vannapurve@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20221222230458.3828342-3-vannapurve@google.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Dec 22, 2022, Vishal Annapurve wrote: > Add an API to execute hypercall as per the cpu type by checking the > underlying CPU. KVM emulates vmcall/vmmcall instruction by modifying > guest memory contents with hypercall instruction as per the cpu type. > > Confidential VMs need to execute hypercall instruction without it being > emulated by KVM as KVM can not modify guest memory contents. > > Signed-off-by: Vishal Annapurve > --- > .../selftests/kvm/include/x86_64/processor.h | 3 +++ > .../selftests/kvm/lib/x86_64/processor.c | 18 ++++++++++++++++++ > 2 files changed, 21 insertions(+) > > diff --git a/tools/testing/selftests/kvm/include/x86_64/processor.h b/tools/testing/selftests/kvm/include/x86_64/processor.h > index 4d5dd9a467e1..3617f83bb2e5 100644 > --- a/tools/testing/selftests/kvm/include/x86_64/processor.h > +++ b/tools/testing/selftests/kvm/include/x86_64/processor.h > @@ -1039,6 +1039,9 @@ uint64_t *vm_get_page_table_entry(struct kvm_vm *vm, uint64_t vaddr); > uint64_t kvm_hypercall(uint64_t nr, uint64_t a0, uint64_t a1, uint64_t a2, > uint64_t a3); > > +uint64_t kvm_native_hypercall(uint64_t nr, uint64_t a0, uint64_t a1, uint64_t a2, > + uint64_t a3); > + > void __vm_xsave_require_permission(int bit, const char *name); > > #define vm_xsave_require_permission(perm) \ > diff --git a/tools/testing/selftests/kvm/lib/x86_64/processor.c b/tools/testing/selftests/kvm/lib/x86_64/processor.c > index 1e93bb3cb058..429e55f2609f 100644 > --- a/tools/testing/selftests/kvm/lib/x86_64/processor.c > +++ b/tools/testing/selftests/kvm/lib/x86_64/processor.c > @@ -1202,6 +1202,24 @@ uint64_t kvm_hypercall(uint64_t nr, uint64_t a0, uint64_t a1, uint64_t a2, > return r; > } > > +uint64_t kvm_native_hypercall(uint64_t nr, uint64_t a0, uint64_t a1, uint64_t a2, Just do this in kvm_hypercall(). David didn't say "don't do that", he said "don't do that in a single patch". Except for fix_hypercall_test, selftests should always use the native hypercall instruction and not rely on KVM's patching, e.g. patching will go sideways if someone gets clever and makes guest code not-writable. > + uint64_t a3) Align parameters. > +{ > + uint64_t r; > + > + if (is_amd_cpu()) { Curly brace is unnecessary. Though I think I'd prefer to not duplicate the asm blob (too many darn inputs). It's a little ugly, but I prefer it over duplicating the entire blob. The approach could also likely be macrofied for other hypercall usage (future problem). asm volatile("test %[use_vmmcall], %[use_vmmcall]\n\t" "jnz 1f\n\t" "vmcall\n\t" "jmp 2f\n\t" "1: vmmcall\n\t" "2:" : "=a"(r) : "a"(nr), "b"(a0), "c"(a1), "d"(a2), "S"(a3), [use_vmmcall] "r" (is_amd_cpu())); return r;