From patchwork Sat Feb 3 00:09: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: 196100 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:9bc1:b0:106:209c:c626 with SMTP id op1csp769271dyc; Fri, 2 Feb 2024 16:10:19 -0800 (PST) X-Google-Smtp-Source: AGHT+IFO4tIorNrEtHj0VuPm9YeCriGEXSuP8XcE3gAQHbnMHI3pwXqZm60CFeA7R8yX+pklcRjo X-Received: by 2002:a17:90a:fd0f:b0:28c:fb86:23ce with SMTP id cv15-20020a17090afd0f00b0028cfb8623cemr3556022pjb.44.1706919019065; Fri, 02 Feb 2024 16:10:19 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706919019; cv=pass; d=google.com; s=arc-20160816; b=s6m8DrLeinZnKMroDGCR7I3/rroqLZNXpKfJdXwEiAHZA8kWdDCBS812ckqsDORddB afjv/XCbLvwY2BP11PoVJFLZTTV7PcPCvVxC6g4rDkLSGOq0NbhbFVHx5Y0Ae5+bPcP0 0JGkz9gOXQjyAF2ITn3IershX09ENpeVjun0J4dYyPI720a9GImc1AxLLGK4xbByhbqu OhXQN8Uu+Kwj8Uwom4ot88H4kaUUskakzRRAxBQ/9ZKPw11KPNT7ClU4JEVwwcJ18Bj2 fkMCMTI8LSgRBEgeTRhPeZXT763QnajQPelLXscAPG7LqfbaqmOaO/EPWJXIb7kiCey7 swhQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:message-id:references:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:in-reply-to:date :reply-to:dkim-signature; bh=OEEHd6q2TChurvVJ9QWQDtWt/fR7D+phxscUNFsCU/w=; fh=D6g3OrUcxGdqOMRlgpvpnlf3nT314G5MaFzXZUCeksc=; b=n2CP+2FYcXcAa2XMkA3zjRQaAcwoRxGs7CD0O9NUpvwr13iLz3S5JlqdKMKHAVDYjD 1w6XbhAvTISxefAjeilcv7t02RUseY+H2BF9NA8/itme8niToO9OyiGKBBPZfFm6Fmh4 7rG3yn2h7622XA/NJOcnZC1z0mp5olh6QZSufQIjJ8sI1WxeGQFrs0SfV3xMmh1awIPK myAoS+4CN8NcpZYtRV/Zfg7u0ve21TWQnJ/e+T98+rQRFFjXvgS0t3tWfWskQrlLkrZ9 rQrlz9DvQPZUQjITVFG/KU03BLdhgtC5y/2LYHn6soGlMKeaOJ2dgbLT4Kz3mI1Liwve RKug==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=NCaTiZpY; arc=pass (i=1 spf=pass spfdomain=flex--seanjc.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-50747-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-50747-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com X-Forwarded-Encrypted: i=1; AJvYcCVSoehB1JP7DmaBzfgPsG+/O7oDqd3V3VdoiY363/E9iyBW6WcNW5rUsol16j0gAfabK5YVy54mNnoH7n3rmHLtzsgQiQ== Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id my5-20020a17090b4c8500b00295d9ec8e60si670853pjb.123.2024.02.02.16.10.18 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 Feb 2024 16:10:19 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-50747-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=NCaTiZpY; arc=pass (i=1 spf=pass spfdomain=flex--seanjc.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-50747-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-50747-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id D3B71281FDD for ; Sat, 3 Feb 2024 00:10:18 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 8C7EF6FCA; Sat, 3 Feb 2024 00:09:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="NCaTiZpY" 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 BC3BBA3D for ; Sat, 3 Feb 2024 00:09:23 +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=1706918965; cv=none; b=VnZTwGcilGVzJAdHQi6K8QtHUNf7aL4HMKLemZAJ7v+4zzVZXuEB5V9xDa9M6sTtJfSjZA4jt2ZS9xWiqEGN4np7nVfDWYTrjvJ8b7bHfHGat0WxIKJ1EoGvlmZVsMPic7NiQpQ61U6j7HXEt5ae6ZJW3VKssBF2ACDbrGYAJHY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706918965; c=relaxed/simple; bh=DQsGPwFy3v7uPs21OAJ9r/tyAnYHLR0BBzOxzUXWZ6A=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=EmLEgVOj9Ug56sjY/bxKEHiEi92edaYEbZkspBMkvl6GhMMkxq20KFIH5ur6oS3m658eTWlSNTxZWqZH2IjLoQqZmc+OumK+O1UiSvMVc1eZIscXl9gGqLnF+5cpaKoHa4FyyWSKMlSdVyWnAUt9QUh5JRMxBVN690lfX/HWvhg= 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=NCaTiZpY; 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 Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-dc6dbdcfd39so4559097276.2 for ; Fri, 02 Feb 2024 16:09:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1706918962; x=1707523762; 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=OEEHd6q2TChurvVJ9QWQDtWt/fR7D+phxscUNFsCU/w=; b=NCaTiZpYfUYV6aEyCd55jbXAuVRLGHmocKLd11khnbFf2d5SITGcQY1/VCKor88P6T 3b1ofVmwetI1rzqzVVhX9zsr0ixBt1+8QA5DkNfal0P9y24cGACiyD0+QNg2rf3iIX2K y0QwynEX1sSrkt5ZVdphd0vqyUU2M40rf5B3GIQMmAfilEss7MmkQYdSMR1XOoTxBu9+ vCO6+der2su51qtCCadahvKTHcYjoA18XH4IGAyg/k6PKdjw16h6G2a/Bhfu7JXKKO+4 jHyCBvzhLs7M2NDnF+eMpyWLdL5BovQm6CW8l3FcjnKmI1mdcLiYjYDm0ipJvfSdJHz/ XCCQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706918962; x=1707523762; 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=OEEHd6q2TChurvVJ9QWQDtWt/fR7D+phxscUNFsCU/w=; b=hBaRGzePA22hVUb+qcsH6hfLZPbzglz8CaDzikIzXrnKzUTAVh9f5NbI8HmPQzx5UC XSBkDG7l3OMe8yWEqvek5MM1PBVBM+OOpynBY7ubAPJ9FLvXlR0WdSzREI1JiOl4OTch PUbe1qATAiW+dYPC8VCMK+93I1uBRU+xFJWe7ngjQvM2tfsUrrWeYgDYNibCK4S9izYK FNajYjygnhnE4e7RWdQK6GQgwLYEY/e8oVWUCXfjy8ztkM8xr9X6ZmmkhuCrCaoKHCgN ff2I4rU8l8//p0MgFkcUM7PJAMmFxJ2nOuXF21APseMJkprcRWOWXvCZgALkXot7ZVL6 hfbg== X-Gm-Message-State: AOJu0YzYgBnFtVBhYVL5UKf965jwm/Pr/gz2jMHP3P1u2bGVHw5+D1qr /4EPmgDzKquFx+NMVi/pwkq0xlGJBzRfD1S8ahSBy2FvLS8ltE3VeNtQ+ttRN+NdkfAYH0WOQAV 9gw== X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6902:2402:b0:dc6:e7e8:caae with SMTP id dr2-20020a056902240200b00dc6e7e8caaemr1059362ybb.0.1706918962763; Fri, 02 Feb 2024 16:09:22 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 2 Feb 2024 16:09:07 -0800 In-Reply-To: <20240203000917.376631-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: <20240203000917.376631-1-seanjc@google.com> X-Mailer: git-send-email 2.43.0.594.gd9cf4e227d-goog Message-ID: <20240203000917.376631-2-seanjc@google.com> Subject: [PATCH v8 01/10] KVM: selftests: Extend VM creation's @shape to allow control of VM subtype From: Sean Christopherson To: Paolo Bonzini , Marc Zyngier , Oliver Upton , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Sean Christopherson Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, Vishal Annapurve , Ackerley Tng , Andrew Jones , Tom Lendacky , Michael Roth , Peter Gonda X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1789834317552869585 X-GMAIL-MSGID: 1789834317552869585 Carve out space in the @shape passed to the various VM creation helpers to allow using the shape to control the subtype of VM, e.g. to identify x86's SEV VMs (which are "regular" VMs as far as KVM is concerned). Cc: Paolo Bonzini Cc: Sean Christopherson Cc: Vishal Annapurve Cc: Ackerley Tng Cc: Andrew Jones Cc: Tom Lendacky Cc: Michael Roth Signed-off-by: Sean Christopherson --- tools/testing/selftests/kvm/include/kvm_util_base.h | 9 +++++++-- tools/testing/selftests/kvm/lib/kvm_util.c | 1 + 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/tools/testing/selftests/kvm/include/kvm_util_base.h b/tools/testing/selftests/kvm/include/kvm_util_base.h index 070f250036fc..d9dc31af2f96 100644 --- a/tools/testing/selftests/kvm/include/kvm_util_base.h +++ b/tools/testing/selftests/kvm/include/kvm_util_base.h @@ -90,6 +90,7 @@ enum kvm_mem_region_type { struct kvm_vm { int mode; unsigned long type; + uint8_t subtype; int kvm_fd; int fd; unsigned int pgtable_levels; @@ -191,10 +192,14 @@ enum vm_guest_mode { }; struct vm_shape { - enum vm_guest_mode mode; - unsigned int type; + uint32_t type; + uint8_t mode; + uint8_t subtype; + uint16_t padding; }; +kvm_static_assert(sizeof(struct vm_shape) == sizeof(uint64_t)); + #define VM_TYPE_DEFAULT 0 #define VM_SHAPE(__mode) \ diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/selftests/kvm/lib/kvm_util.c index 4994afbdab40..a53caf81eb87 100644 --- a/tools/testing/selftests/kvm/lib/kvm_util.c +++ b/tools/testing/selftests/kvm/lib/kvm_util.c @@ -276,6 +276,7 @@ struct kvm_vm *____vm_create(struct vm_shape shape) vm->mode = shape.mode; vm->type = shape.type; + vm->subtype = shape.subtype; vm->pa_bits = vm_guest_mode_params[vm->mode].pa_bits; vm->va_bits = vm_guest_mode_params[vm->mode].va_bits; From patchwork Sat Feb 3 00:09:08 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 196103 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:9bc1:b0:106:209c:c626 with SMTP id op1csp769560dyc; Fri, 2 Feb 2024 16:11:03 -0800 (PST) X-Google-Smtp-Source: AGHT+IE3u7Uzpz/IGjfkHDyAFfKWu7li8mnnx2SpJRTdY9GUqGC/aEJHTy788HDAsYqlrfDLEEGv X-Received: by 2002:ac8:5e4d:0:b0:42b:ef92:945 with SMTP id i13-20020ac85e4d000000b0042bef920945mr3845774qtx.14.1706919063643; Fri, 02 Feb 2024 16:11:03 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706919063; cv=pass; d=google.com; s=arc-20160816; b=dgh5JEQN9LPw26lZcdgHFlTs0ZGORoGFTOZ7YT9VtNT2kUQnZEva01eKY9kZLU13VP 6X9eEnghKXbdu5AnQmUglMVSKje/fR42/oiztQTZ0Kg2wz3uav+AmJ0g+C+93ta780rg 84zceRyGMeJJHO3T2HZrL99VCy0SAf5WjjE721CCnCCjw9oHLNfNI5X5iIlln1wGif0m 6LgW5WYNFXmFD9Sle8p9vkLyBTGYXPI5MKWxpAGaWayKc68YZa1Dl66PeSNeGdKB/0LJ KWg9Kga2Yyk7SBzFBPF36ESyQ7owmGJZefxoURUq+duysAYmTx+tZn4RxOUrWGZovGdK i9MA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:message-id:references:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:in-reply-to:date :reply-to:dkim-signature; bh=Fg/vqH030f9YZARetCg5UZ+D2wOsplctpRVUYVfmsZ0=; fh=JlnlTJ3NSh5kftCRx1NEHOqlWpzz5zx9dhUP3cd+1CY=; b=v75nBOl5e00tb0Bu9ojo2bpBOvOV93Ivao8GgoYwRG70LoS2rN9XHynFEOdR+SjeZ5 O9ZR9O+IrzBD2szVCgEqGYy62JbiEl+E6i59t7TedPP2gPe+qIrzwEQSiTK4Y09XTE0O hLDxkmHwYSEx1KaL2HJHMSEN4ByV1TU9HKYg8YxLvvxEA6ybYxNkpqNWd+m9kkdfOyEn //VjbaD6JhB64n+t3kqr7lqE0HCY9D1zOcaNCvmgrTuQfmlsZ6nQuPJ5WBN/hupYdJW/ 5Nax3CMUUnuRG+N7/ejR6IYZQhd1PPtKFZ+OCFqUwGpzqI4frUVFwmQvwDea9M+ZTCAH VjpA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=cK2RC+av; arc=pass (i=1 spf=pass spfdomain=flex--seanjc.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-50748-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-50748-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com X-Forwarded-Encrypted: i=1; AJvYcCXksXC5rWgvAKE9b4R3ja8OqisjzQoTn6kESSK23WaGAdoLmv0qhsczDLv9kUze8jXa/7OX2qQ9JVJQwMrfiLvpRQuNUw== Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id v20-20020a05622a189400b0042bda762eedsi3149569qtc.759.2024.02.02.16.11.03 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 Feb 2024 16:11:03 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-50748-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=cK2RC+av; arc=pass (i=1 spf=pass spfdomain=flex--seanjc.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-50748-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-50748-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 3D4BF1C20F3C for ; Sat, 3 Feb 2024 00:11:03 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id B65E4CA7C; Sat, 3 Feb 2024 00:09:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="cK2RC+av" 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 702CA1FB2 for ; Sat, 3 Feb 2024 00:09:25 +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=1706918967; cv=none; b=ebJebhaUhdWx6jRA9xnspAaE/OKoqTzPoK9WNrFIX2h5SYhW6+pG81zoeC+lapoaxjwKUKrvrOTG+I3D8VOfjNh9JvBltIdg0rowg5clBjLMAyfcHwENA2vzixudPB20C5cXoWmjR6BoAFAWNWLq4hiy22dNvUS9Ul5fwFBMbaQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706918967; c=relaxed/simple; bh=CNPk1afhnBjcfCeKhJ+dXXQAMfAeimm4XlBq2aq6RHw=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=j1CKuEAELfF96atnuF/5GO/ldfRV7AoV1kAnYefy0O2wyacH8K2XdtOXuRqLvdavJyYIddimuzZbvz+abDMDIEJHYbG424ithrX9n1Hmm6I9+0A4DJ6e9quRaPnpoXHn4hkvXBIo47PbvB2dTQ0uU33ci1H7M3lWsQb/Royg6O8= 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=cK2RC+av; 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 Received: by mail-pg1-f201.google.com with SMTP id 41be03b00d2f7-5d8dc8f906aso2494656a12.2 for ; Fri, 02 Feb 2024 16:09:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1706918965; x=1707523765; 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=Fg/vqH030f9YZARetCg5UZ+D2wOsplctpRVUYVfmsZ0=; b=cK2RC+avJAJ9jOkjhYkgaGPwauQ9rNIT7+b4lLLqyTyDYkgVBM2xqSDpE9OXH5bJgy SR2p+BFGQPaS1svOYUrk9Im3R3/p2h04PXTlx56tom05gPAcV7MCefxNAAQZioDZlFKj H23/DBtfZqWbZJATe6fDsjVzKsmSa3sBNZJJpNLGvnrK6f/TgD4U/lTN5q7msnt8n7pK YX0AkwK3MHkP6GJcqDgWoAp0HgICmc9fIptRjN1lpAGu8s5+9rtakwVkyXt9PR9jyoTB djlM7b0MNylgEEOFwlZejT3/Qo0/Bv420VVTn0bC8fHsmIgyKB1ucDtohxcLGQWFpUB7 7gCQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706918965; x=1707523765; 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=Fg/vqH030f9YZARetCg5UZ+D2wOsplctpRVUYVfmsZ0=; b=inJlsdru6IoOmn7TBmmJ6wo8A/XRlP2QFIKIE7+XT/eaSmwQd221hV4U2V5Zwa4xG5 nlvGfgxk3ZD5PQXhkzuXs6bM2OirvS83JPKt7leDFHOXSoai4u6ZK57GkOwBDeeqpCKZ M9p+CP/feyVBact/FeFMLML2ZIrg1BCVco8VfsWZ21LaUOTf5lKAoMDI6vaEHXgzeHX0 MWJxYKa6QiGCoBfJQCWPEsWMvW0EDaNhRjoHZNE2sSIaPO34zhoqfXh3FzRTN94/H2c9 fO0pLZex5GsDTWG89/KhHsWX9DR2advbAnTA/NB7S2e5PZ5g5I9Km9qfUhMATLKIZO4u h9HA== X-Gm-Message-State: AOJu0YycjlYwDMgalME43bxFFS3zeYgQZoGRWen9O3a7sTVI4rkPPu1d pV2/UqcteSZ7HfGumgpvYxSENYeE9Et3UuXSKzOXh5wfB6RkVJeARFzbUsSIshjlClSg98ZcPkw 7Mw== X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:902:d4c4:b0:1d9:697d:6817 with SMTP id o4-20020a170902d4c400b001d9697d6817mr174835plg.1.1706918964815; Fri, 02 Feb 2024 16:09:24 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 2 Feb 2024 16:09:08 -0800 In-Reply-To: <20240203000917.376631-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: <20240203000917.376631-1-seanjc@google.com> X-Mailer: git-send-email 2.43.0.594.gd9cf4e227d-goog Message-ID: <20240203000917.376631-3-seanjc@google.com> Subject: [PATCH v8 02/10] KVM: selftests: Make sparsebit structs const where appropriate From: Sean Christopherson To: Paolo Bonzini , Marc Zyngier , Oliver Upton , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Sean Christopherson Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, Vishal Annapurve , Ackerley Tng , Andrew Jones , Tom Lendacky , Michael Roth , Peter Gonda X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1789834364311128170 X-GMAIL-MSGID: 1789834364311128170 From: Michael Roth Make all sparsebit struct pointers "const" where appropriate. This will allow adding a bitmap to track protected/encrypted physical memory that tests can access in a read-only fashion. Cc: Paolo Bonzini Cc: Sean Christopherson Cc: Vishal Annapurve Cc: Ackerley Tng Cc: Andrew Jones Cc: Tom Lendacky Cc: Michael Roth Signed-off-by: Michael Roth Signed-off-by: Peter Gonda [sean: massage changelog] Signed-off-by: Sean Christopherson --- .../testing/selftests/kvm/include/sparsebit.h | 36 +++++++------- tools/testing/selftests/kvm/lib/sparsebit.c | 48 +++++++++---------- 2 files changed, 42 insertions(+), 42 deletions(-) diff --git a/tools/testing/selftests/kvm/include/sparsebit.h b/tools/testing/selftests/kvm/include/sparsebit.h index 12a9a4b9cead..fb5170d57fcb 100644 --- a/tools/testing/selftests/kvm/include/sparsebit.h +++ b/tools/testing/selftests/kvm/include/sparsebit.h @@ -30,26 +30,26 @@ typedef uint64_t sparsebit_num_t; struct sparsebit *sparsebit_alloc(void); void sparsebit_free(struct sparsebit **sbitp); -void sparsebit_copy(struct sparsebit *dstp, struct sparsebit *src); +void sparsebit_copy(struct sparsebit *dstp, const struct sparsebit *src); -bool sparsebit_is_set(struct sparsebit *sbit, sparsebit_idx_t idx); -bool sparsebit_is_set_num(struct sparsebit *sbit, +bool sparsebit_is_set(const struct sparsebit *sbit, sparsebit_idx_t idx); +bool sparsebit_is_set_num(const struct sparsebit *sbit, sparsebit_idx_t idx, sparsebit_num_t num); -bool sparsebit_is_clear(struct sparsebit *sbit, sparsebit_idx_t idx); -bool sparsebit_is_clear_num(struct sparsebit *sbit, +bool sparsebit_is_clear(const struct sparsebit *sbit, sparsebit_idx_t idx); +bool sparsebit_is_clear_num(const struct sparsebit *sbit, sparsebit_idx_t idx, sparsebit_num_t num); -sparsebit_num_t sparsebit_num_set(struct sparsebit *sbit); -bool sparsebit_any_set(struct sparsebit *sbit); -bool sparsebit_any_clear(struct sparsebit *sbit); -bool sparsebit_all_set(struct sparsebit *sbit); -bool sparsebit_all_clear(struct sparsebit *sbit); -sparsebit_idx_t sparsebit_first_set(struct sparsebit *sbit); -sparsebit_idx_t sparsebit_first_clear(struct sparsebit *sbit); -sparsebit_idx_t sparsebit_next_set(struct sparsebit *sbit, sparsebit_idx_t prev); -sparsebit_idx_t sparsebit_next_clear(struct sparsebit *sbit, sparsebit_idx_t prev); -sparsebit_idx_t sparsebit_next_set_num(struct sparsebit *sbit, +sparsebit_num_t sparsebit_num_set(const struct sparsebit *sbit); +bool sparsebit_any_set(const struct sparsebit *sbit); +bool sparsebit_any_clear(const struct sparsebit *sbit); +bool sparsebit_all_set(const struct sparsebit *sbit); +bool sparsebit_all_clear(const struct sparsebit *sbit); +sparsebit_idx_t sparsebit_first_set(const struct sparsebit *sbit); +sparsebit_idx_t sparsebit_first_clear(const struct sparsebit *sbit); +sparsebit_idx_t sparsebit_next_set(const struct sparsebit *sbit, sparsebit_idx_t prev); +sparsebit_idx_t sparsebit_next_clear(const struct sparsebit *sbit, sparsebit_idx_t prev); +sparsebit_idx_t sparsebit_next_set_num(const struct sparsebit *sbit, sparsebit_idx_t start, sparsebit_num_t num); -sparsebit_idx_t sparsebit_next_clear_num(struct sparsebit *sbit, +sparsebit_idx_t sparsebit_next_clear_num(const struct sparsebit *sbit, sparsebit_idx_t start, sparsebit_num_t num); void sparsebit_set(struct sparsebit *sbitp, sparsebit_idx_t idx); @@ -62,9 +62,9 @@ void sparsebit_clear_num(struct sparsebit *sbitp, sparsebit_idx_t start, sparsebit_num_t num); void sparsebit_clear_all(struct sparsebit *sbitp); -void sparsebit_dump(FILE *stream, struct sparsebit *sbit, +void sparsebit_dump(FILE *stream, const struct sparsebit *sbit, unsigned int indent); -void sparsebit_validate_internal(struct sparsebit *sbit); +void sparsebit_validate_internal(const struct sparsebit *sbit); #ifdef __cplusplus } diff --git a/tools/testing/selftests/kvm/lib/sparsebit.c b/tools/testing/selftests/kvm/lib/sparsebit.c index 88cb6b84e6f3..cfed9d26cc71 100644 --- a/tools/testing/selftests/kvm/lib/sparsebit.c +++ b/tools/testing/selftests/kvm/lib/sparsebit.c @@ -202,7 +202,7 @@ static sparsebit_num_t node_num_set(struct node *nodep) /* Returns a pointer to the node that describes the * lowest bit index. */ -static struct node *node_first(struct sparsebit *s) +static struct node *node_first(const struct sparsebit *s) { struct node *nodep; @@ -216,7 +216,7 @@ static struct node *node_first(struct sparsebit *s) * lowest bit index > the index of the node pointed to by np. * Returns NULL if no node with a higher index exists. */ -static struct node *node_next(struct sparsebit *s, struct node *np) +static struct node *node_next(const struct sparsebit *s, struct node *np) { struct node *nodep = np; @@ -244,7 +244,7 @@ static struct node *node_next(struct sparsebit *s, struct node *np) * highest index < the index of the node pointed to by np. * Returns NULL if no node with a lower index exists. */ -static struct node *node_prev(struct sparsebit *s, struct node *np) +static struct node *node_prev(const struct sparsebit *s, struct node *np) { struct node *nodep = np; @@ -273,7 +273,7 @@ static struct node *node_prev(struct sparsebit *s, struct node *np) * subtree and duplicates the bit settings to the newly allocated nodes. * Returns the newly allocated copy of subtree. */ -static struct node *node_copy_subtree(struct node *subtree) +static struct node *node_copy_subtree(const struct node *subtree) { struct node *root; @@ -307,7 +307,7 @@ static struct node *node_copy_subtree(struct node *subtree) * index is within the bits described by the mask bits or the number of * contiguous bits set after the mask. Returns NULL if there is no such node. */ -static struct node *node_find(struct sparsebit *s, sparsebit_idx_t idx) +static struct node *node_find(const struct sparsebit *s, sparsebit_idx_t idx) { struct node *nodep; @@ -393,7 +393,7 @@ static struct node *node_add(struct sparsebit *s, sparsebit_idx_t idx) } /* Returns whether all the bits in the sparsebit array are set. */ -bool sparsebit_all_set(struct sparsebit *s) +bool sparsebit_all_set(const struct sparsebit *s) { /* * If any nodes there must be at least one bit set. Only case @@ -775,7 +775,7 @@ static void node_reduce(struct sparsebit *s, struct node *nodep) /* Returns whether the bit at the index given by idx, within the * sparsebit array is set or not. */ -bool sparsebit_is_set(struct sparsebit *s, sparsebit_idx_t idx) +bool sparsebit_is_set(const struct sparsebit *s, sparsebit_idx_t idx) { struct node *nodep; @@ -921,7 +921,7 @@ static inline sparsebit_idx_t node_first_clear(struct node *nodep, int start) * used by test cases after they detect an unexpected condition, as a means * to capture diagnostic information. */ -static void sparsebit_dump_internal(FILE *stream, struct sparsebit *s, +static void sparsebit_dump_internal(FILE *stream, const struct sparsebit *s, unsigned int indent) { /* Dump the contents of s */ @@ -969,7 +969,7 @@ void sparsebit_free(struct sparsebit **sbitp) * sparsebit_alloc(). It can though already have bits set, which * if different from src will be cleared. */ -void sparsebit_copy(struct sparsebit *d, struct sparsebit *s) +void sparsebit_copy(struct sparsebit *d, const struct sparsebit *s) { /* First clear any bits already set in the destination */ sparsebit_clear_all(d); @@ -981,7 +981,7 @@ void sparsebit_copy(struct sparsebit *d, struct sparsebit *s) } /* Returns whether num consecutive bits starting at idx are all set. */ -bool sparsebit_is_set_num(struct sparsebit *s, +bool sparsebit_is_set_num(const struct sparsebit *s, sparsebit_idx_t idx, sparsebit_num_t num) { sparsebit_idx_t next_cleared; @@ -1005,14 +1005,14 @@ bool sparsebit_is_set_num(struct sparsebit *s, } /* Returns whether the bit at the index given by idx. */ -bool sparsebit_is_clear(struct sparsebit *s, +bool sparsebit_is_clear(const struct sparsebit *s, sparsebit_idx_t idx) { return !sparsebit_is_set(s, idx); } /* Returns whether num consecutive bits starting at idx are all cleared. */ -bool sparsebit_is_clear_num(struct sparsebit *s, +bool sparsebit_is_clear_num(const struct sparsebit *s, sparsebit_idx_t idx, sparsebit_num_t num) { sparsebit_idx_t next_set; @@ -1041,13 +1041,13 @@ bool sparsebit_is_clear_num(struct sparsebit *s, * value. Use sparsebit_any_set(), instead of sparsebit_num_set() > 0, * to determine if the sparsebit array has any bits set. */ -sparsebit_num_t sparsebit_num_set(struct sparsebit *s) +sparsebit_num_t sparsebit_num_set(const struct sparsebit *s) { return s->num_set; } /* Returns whether any bit is set in the sparsebit array. */ -bool sparsebit_any_set(struct sparsebit *s) +bool sparsebit_any_set(const struct sparsebit *s) { /* * Nodes only describe set bits. If any nodes then there @@ -1070,20 +1070,20 @@ bool sparsebit_any_set(struct sparsebit *s) } /* Returns whether all the bits in the sparsebit array are cleared. */ -bool sparsebit_all_clear(struct sparsebit *s) +bool sparsebit_all_clear(const struct sparsebit *s) { return !sparsebit_any_set(s); } /* Returns whether all the bits in the sparsebit array are set. */ -bool sparsebit_any_clear(struct sparsebit *s) +bool sparsebit_any_clear(const struct sparsebit *s) { return !sparsebit_all_set(s); } /* Returns the index of the first set bit. Abort if no bits are set. */ -sparsebit_idx_t sparsebit_first_set(struct sparsebit *s) +sparsebit_idx_t sparsebit_first_set(const struct sparsebit *s) { struct node *nodep; @@ -1097,7 +1097,7 @@ sparsebit_idx_t sparsebit_first_set(struct sparsebit *s) /* Returns the index of the first cleared bit. Abort if * no bits are cleared. */ -sparsebit_idx_t sparsebit_first_clear(struct sparsebit *s) +sparsebit_idx_t sparsebit_first_clear(const struct sparsebit *s) { struct node *nodep1, *nodep2; @@ -1151,7 +1151,7 @@ sparsebit_idx_t sparsebit_first_clear(struct sparsebit *s) /* Returns index of next bit set within s after the index given by prev. * Returns 0 if there are no bits after prev that are set. */ -sparsebit_idx_t sparsebit_next_set(struct sparsebit *s, +sparsebit_idx_t sparsebit_next_set(const struct sparsebit *s, sparsebit_idx_t prev) { sparsebit_idx_t lowest_possible = prev + 1; @@ -1244,7 +1244,7 @@ sparsebit_idx_t sparsebit_next_set(struct sparsebit *s, /* Returns index of next bit cleared within s after the index given by prev. * Returns 0 if there are no bits after prev that are cleared. */ -sparsebit_idx_t sparsebit_next_clear(struct sparsebit *s, +sparsebit_idx_t sparsebit_next_clear(const struct sparsebit *s, sparsebit_idx_t prev) { sparsebit_idx_t lowest_possible = prev + 1; @@ -1300,7 +1300,7 @@ sparsebit_idx_t sparsebit_next_clear(struct sparsebit *s, * and returns the index of the first sequence of num consecutively set * bits. Returns a value of 0 of no such sequence exists. */ -sparsebit_idx_t sparsebit_next_set_num(struct sparsebit *s, +sparsebit_idx_t sparsebit_next_set_num(const struct sparsebit *s, sparsebit_idx_t start, sparsebit_num_t num) { sparsebit_idx_t idx; @@ -1335,7 +1335,7 @@ sparsebit_idx_t sparsebit_next_set_num(struct sparsebit *s, * and returns the index of the first sequence of num consecutively cleared * bits. Returns a value of 0 of no such sequence exists. */ -sparsebit_idx_t sparsebit_next_clear_num(struct sparsebit *s, +sparsebit_idx_t sparsebit_next_clear_num(const struct sparsebit *s, sparsebit_idx_t start, sparsebit_num_t num) { sparsebit_idx_t idx; @@ -1583,7 +1583,7 @@ static size_t display_range(FILE *stream, sparsebit_idx_t low, * contiguous bits. This is done because '-' is used to specify command-line * options, and sometimes ranges are specified as command-line arguments. */ -void sparsebit_dump(FILE *stream, struct sparsebit *s, +void sparsebit_dump(FILE *stream, const struct sparsebit *s, unsigned int indent) { size_t current_line_len = 0; @@ -1681,7 +1681,7 @@ void sparsebit_dump(FILE *stream, struct sparsebit *s, * s. On error, diagnostic information is printed to stderr and * abort is called. */ -void sparsebit_validate_internal(struct sparsebit *s) +void sparsebit_validate_internal(const struct sparsebit *s) { bool error_detected = false; struct node *nodep, *prev = NULL; From patchwork Sat Feb 3 00:09:09 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 196102 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:9bc1:b0:106:209c:c626 with SMTP id op1csp769519dyc; Fri, 2 Feb 2024 16:10:57 -0800 (PST) X-Google-Smtp-Source: AGHT+IFI5NOWy1do4CUjQZ0fR+VFYGBext1PxVEl0h8/q91f6rf+Pc2q24qaoFMLzb74bLRG/iuj X-Received: by 2002:a05:622a:181a:b0:42a:5923:b0cc with SMTP id t26-20020a05622a181a00b0042a5923b0ccmr9934720qtc.59.1706919056953; Fri, 02 Feb 2024 16:10:56 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706919056; cv=pass; d=google.com; s=arc-20160816; b=skHlh3nbMlZLruJ1YyXAhcMGHlLz+3tkpfETKlSeccbo3GsNITNoN9+eyA0YUFOI6H wYLjVgiaoWwWafZ2jycistJicEFceoVKKx3ndILKrAzW2PwTVQ3al+qfbS6usRk182e/ rLbomF+C+zA2P5Ljbuj8Gb8oOBIg+YxtCdaagJREOe66XP45tLu2qYTS81CYT0zkwq57 YkPkTpWfP+iQ3PhTlXW0oY07cgwK5zcyBqKKOLCATAAhQjylNGN9L+ZXEvHfJcAaYe0L VCIKDp/4pdnrsmEoL0vo6aT605ezRrk1N9S4jHHlp/so1HS4hXQSVvVFwLlLx9sJS8+C MoZw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:message-id:references:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:in-reply-to:date :reply-to:dkim-signature; bh=EJcVR2aXcmJcYN2R0niM7r+O1TbQOV0XdUcPDJMxbc8=; fh=sEMHsSH/YS1Znc/5csPgpjo29UzyALhBI8Cs+cUtdCo=; b=vl/0Dm6OhnyXIuIimkNYvu7iauA5GUlWA8/TRlYx+8OZKhYpYff8FYj4y/7TJDahr/ 5hvqzau8LDctzCFO2iPObKdFmufYLwEEXWLn6VW4qYDS0ssUq12JpU0P9tzfMJZIgSAz /GrQSLcbgt63xzV8qC1AMjL3OWm+x511Ib5UPMt2w+Dh4c67OzNWspFaLDoE5yf21U9j BC64bNL/wguO6bb1jESKHfN3C4vqnezLTP0J/u/pqgFYHgs9wa9KrX3wqiSSC5kafB5G XL0UoGLsHBVURi+TwBlNhG5nwONJE17+YUThM1zxg2wn/o9Jz1L2GjMQFDLht8Bh83x8 UvFQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=GS2Axujj; arc=pass (i=1 spf=pass spfdomain=flex--seanjc.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-50749-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-50749-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com X-Forwarded-Encrypted: i=1; AJvYcCWU1cGBUWfaS/N+Fq5pVmXEmtpdq6+EGf7oRpF2lCzeS2Z4SwEwd26fvm2LBsTV7En33UDjcKYJa11KOexdt08unUNjsQ== Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id v20-20020a05622a189400b0042bda762eedsi3149569qtc.759.2024.02.02.16.10.56 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 Feb 2024 16:10:56 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-50749-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=GS2Axujj; arc=pass (i=1 spf=pass spfdomain=flex--seanjc.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-50749-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-50749-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id BB83B1C21342 for ; Sat, 3 Feb 2024 00:10:56 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 6B483C135; Sat, 3 Feb 2024 00:09:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="GS2Axujj" 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 B276B63D1 for ; Sat, 3 Feb 2024 00:09:27 +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=1706918969; cv=none; b=HDZeyPDkMZ5hstr5J5hhboP7F9suRCTDRqh9P/vVTM2mhyvWN3CUxE+hR+UCjF9nP2X1j6EerUEvKGAKt1LzZIKoLrAqcutzYUpKc/OfpNxE1OjZQVeWdDfMGllV0WgTennk48tcd2PQIsFxxlAsaxg25l3Ut0cR66I0KjvtSwY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706918969; c=relaxed/simple; bh=5adFsddT99jRoR/vIsu3Y6j5XoRwGq4E/i/qqsJ/xJA=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=lih4ng0N1u0RrmV7fSvhsG7U+B1V2Xcyyz43H9h9RHig1GBuBqF0mJgKGWPPMlDbfdXw4XmcEQuXsSXMxq55ie7iefG24goDREUO3vBIuF7ftC674oj+1CnXlx7kI45LAyoXLQ9qKVjc7xd1ADZsQoIq+YgeVKPkt/XG19xziZo= 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=GS2Axujj; 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 Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-6041bb56dbfso39644147b3.2 for ; Fri, 02 Feb 2024 16:09:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1706918966; x=1707523766; 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=EJcVR2aXcmJcYN2R0niM7r+O1TbQOV0XdUcPDJMxbc8=; b=GS2AxujjAaHn3yveWNxDHDiVD5cIhLg+IDoVBNBCG6toa3XyqVGvPcN5Dd90VnftUZ uMAPrfOrjQAL7BaP8P4/pGlXpBiTrO1nDjnyKN8Ru0RbZml84yaVo1WO3pKcTm1BP4yH grbN7uBD6LnBTIWU+sJwT+nBPKz53ud5rIyGygu9me/8PUrBxHFlBhVShtQ56BH1PzUZ c20+2TFooNzEjhAsyOdGt4Wzrhnov6rtefQdk7oN0bsbmojLQNTkyKc5/a75E+K0PX30 kH9LiAYpkfXkbW8SjI+mDIXbYYrSXzsn7alT5Yc5nD9u2OqbBR3xO89UVPGmlHHr37vX TXVA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706918966; x=1707523766; 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=EJcVR2aXcmJcYN2R0niM7r+O1TbQOV0XdUcPDJMxbc8=; b=XUzLO4WLvR6m8t+NCIDMB+O/G/2J8PiyXDaJIilDNRykx/Pwyv9FCey40m4GgYtRYl VpMsobbBTUyNrEwvsJsokidq/Gu1qHNqq7qDgXcEoLrhCSErNuEAqged62RoxormpkIX OeW+pjk2AXrEky0MqWATZ66H3PhI6acZsfGjPRugaCBF74A+YJ+/vTDGHd8lSeVW7AZR O2NkVNFCq41YQ17AE/nxw993Vn3tWMxbyf7TZnbA/og9ll2ht6SZMZ5gwWfkD8fIz9CU Hdjh2gwfjyE2enICVNCK2qVZttXfBNUtBFHj9FpI9D7ZUdiCpE1p022XgF2ZnEjZSvAY EFUA== X-Gm-Message-State: AOJu0YzkIq0HPvo6uAyQGrB/Qqo0JsX8yQPXyBniJAIUakGNAsEHKghs PAzY8pJMM1lqXr4YSICFUNAr123Hj5zlH0W7juY9t2edp8E+3db8IYhuv4fG6BxgvQ1P77v8T3d i3g== X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a81:9b4b:0:b0:5ff:6041:c17d with SMTP id s72-20020a819b4b000000b005ff6041c17dmr1167011ywg.2.1706918966644; Fri, 02 Feb 2024 16:09:26 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 2 Feb 2024 16:09:09 -0800 In-Reply-To: <20240203000917.376631-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: <20240203000917.376631-1-seanjc@google.com> X-Mailer: git-send-email 2.43.0.594.gd9cf4e227d-goog Message-ID: <20240203000917.376631-4-seanjc@google.com> Subject: [PATCH v8 03/10] KVM: selftests: Add a macro to iterate over a sparsebit range From: Sean Christopherson To: Paolo Bonzini , Marc Zyngier , Oliver Upton , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Sean Christopherson Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, Vishal Annapurve , Ackerley Tng , Andrew Jones , Tom Lendacky , Michael Roth , Peter Gonda X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1789834357244809597 X-GMAIL-MSGID: 1789834357244809597 From: Ackerley Tng Add sparsebit_for_each_set_range() to allow iterator over a range of set bits in a range. This will be used by x86 SEV guests to process protected physical pages (each such page needs to be encrypted _after_ being "added" to the VM). Signed-off-by: Ackerley Tng [sean: split to separate patch] Signed-off-by: Sean Christopherson --- .../testing/selftests/kvm/include/sparsebit.h | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/tools/testing/selftests/kvm/include/sparsebit.h b/tools/testing/selftests/kvm/include/sparsebit.h index fb5170d57fcb..bc760761e1a3 100644 --- a/tools/testing/selftests/kvm/include/sparsebit.h +++ b/tools/testing/selftests/kvm/include/sparsebit.h @@ -66,6 +66,26 @@ void sparsebit_dump(FILE *stream, const struct sparsebit *sbit, unsigned int indent); void sparsebit_validate_internal(const struct sparsebit *sbit); +/* + * Iterate over an inclusive ranges within sparsebit @s. In each iteration, + * @range_begin and @range_end will take the beginning and end of the set + * range, which are of type sparsebit_idx_t. + * + * For example, if the range [3, 7] (inclusive) is set, within the + * iteration,@range_begin will take the value 3 and @range_end will take + * the value 7. + * + * Ensure that there is at least one bit set before using this macro with + * sparsebit_any_set(), because sparsebit_first_set() will abort if none + * are set. + */ +#define sparsebit_for_each_set_range(s, range_begin, range_end) \ + for (range_begin = sparsebit_first_set(s), \ + range_end = sparsebit_next_clear(s, range_begin) - 1; \ + range_begin && range_end; \ + range_begin = sparsebit_next_set(s, range_end), \ + range_end = sparsebit_next_clear(s, range_begin) - 1) + #ifdef __cplusplus } #endif From patchwork Sat Feb 3 00:09:10 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 196105 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:9bc1:b0:106:209c:c626 with SMTP id op1csp769792dyc; Fri, 2 Feb 2024 16:11:40 -0800 (PST) X-Google-Smtp-Source: AGHT+IGotl4Td6JAblKN7P7MV0fEZ+NQgy9jRep2nMwYpvrF2LzyS13M3lDe24gjYN39Zgqzsfcc X-Received: by 2002:a17:90a:e284:b0:296:1195:df00 with SMTP id d4-20020a17090ae28400b002961195df00mr6378834pjz.36.1706919100369; Fri, 02 Feb 2024 16:11:40 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706919100; cv=pass; d=google.com; s=arc-20160816; b=QYPgEu3BvkI2cVfPt2OF6LfydGovCFC2Kf3FRKOiWk/AAS/1SlBQAoNq2z62bMZWhJ cR00d7WxbY+c+7ey7WsRSKg5IqtfSJDKCmw6GQonvZVdAHFl50sOmpFlpcPsqe0/YL+0 /feknKgKSepgVjk0BBy9ESKxLjL51DkteRS2S3RsmX+IgJIeXMNFvFhzgCaygkly+weC HaJNRa7iDS9d1PzE6rUUnyH4mfUS4S1hqirwppxWAHMEU51Mb8znlht/rJjFAGbLu4O6 IJO/AKC2//J74rhTqNfkKGOJLwyvFJkCDbuav0FWmrV1KAdmPUpnxcrmmgxs5YrV1ynM Fvbg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:message-id:references:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:in-reply-to:date :reply-to:dkim-signature; bh=CwvKSfH4xv/NatBnDCESKILLStQmhSJKJagouj3ct64=; fh=kx2p58LX4Ss5pTdrcFTqJaPbV7c3ly9vFOP+bcQGnCI=; b=YMXng0EOpO8st8Fvawh9DffeUlf4ojpHRgjBZ/QhXX3voh2dZ6h7Ddy9drrOnikaHs V/QruzuPssLbrEuKNG4FOvvp6EVoar3r7zjdzy5xmVufiBrtNTsUB6MCN2PRox6YYv6t X6B0EUR6VkezsXKPDIP/5gJISmK8aSS8PNiyO2iTbBH3rliGxUHOhpi8ajFISfmqHlUE FOllLgolnj8pSwk59epQrykyHPgSQjapja1VVztOhIWgCHdrcdNpBbpgiTEsStnQ1MDM Nzx9TMb/9hBQpyObKvk/sMhi+gSQEx0x3CAHMKWcR+c5ZAyu+M9N+ZwzV8HVDkGt6IkN S/wQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=i0u1N5ds; arc=pass (i=1 spf=pass spfdomain=flex--seanjc.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-50750-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-50750-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com X-Forwarded-Encrypted: i=1; AJvYcCVwJL5Zbv91+U8OMR7BRcdjSuA72Fw2gd0PLffI3IFS8wteQpsKo3pUMpn44CNXvIeDNxIdsVrIzirQx6pbsyLHvJOo1A== Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id y18-20020a17090aa41200b0029649e36354si653687pjp.163.2024.02.02.16.11.40 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 Feb 2024 16:11:40 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-50750-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=i0u1N5ds; arc=pass (i=1 spf=pass spfdomain=flex--seanjc.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-50750-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-50750-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 23AE9282F57 for ; Sat, 3 Feb 2024 00:11:40 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id BF58B11181; Sat, 3 Feb 2024 00:09:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="i0u1N5ds" 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 93AE779C8 for ; Sat, 3 Feb 2024 00:09:29 +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=1706918971; cv=none; b=iShYPZgpsjU7gACshcdgaQ1PLy6SSEjQc49eskpnynSfbd9S1PjtUSjYk9ZfeL2YpY1u1MMpE9nl+6U/lZnRSTC5p3sk245Ghz5/P/6/yv13NoYlwjgHxoxRolA5mkqVjurqT0imUQkU+nwde0tEN/VBZy7D387XIpnKuL85ZCk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706918971; c=relaxed/simple; bh=WLkUW0/Unoe62tn0PAI8vXH8dvOJolXpQGzrHPU+8WU=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=rOXY9NMtUQMeB7vISyemwyqqW+InFgaCBLP1uxj270rI4FVnTM8ecYPnimxw5fj125Nj3mV+dpC2mtQMhg2/pkcBvWCvl6yWPnR0C8rfPw7u74cb6pKABQlshdF+BL1eq8NB/CL0lfO6Vfw8mIp2J8V6u3N7bf8n0XMDhFOpvpQ= 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=i0u1N5ds; 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 Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-5f38d676cecso44494287b3.0 for ; Fri, 02 Feb 2024 16:09:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1706918968; x=1707523768; 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=CwvKSfH4xv/NatBnDCESKILLStQmhSJKJagouj3ct64=; b=i0u1N5dsSmRPwUp91mpyA8kVP1ezD7HeFPc3JuIWVmQGwynyCvZ4JU84J0tDJfEjWl JninIDOspfAG03g/KL8GlNV2TfGxGCt88XU6AqE4H5jkubPfhTM02Pc8kCyNi9v7LDW3 H5LwKPD5eg1Bya9/7HHnLCOMDTq3NHZvQyN4Yo19FKMOhPUxzvYE6B1MTE/+6+BjsXPy QM/YkghIzbSa/eh3X1YIF0tOX4jmuZnKYb1jZgqgPGKkKy1Mqfnlp5URG2ub0A0dbglX cP9JKr2pOKCx+6j9aCZhShwMV+Mcp3I6sDiu4vmE4uHVpdBTr3IqE4Pp7siZV2Ze4IpV qSzg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706918968; x=1707523768; 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=CwvKSfH4xv/NatBnDCESKILLStQmhSJKJagouj3ct64=; b=rfcB2tUUYcemFelWv1feQzEvD8hhvDxo1iZ/SFe6WGxNj8zLxzwmAgYhKYt41/QMCw kdhyK6FfBTaQY3Ud3JGP/xT1bmOn9bTk8OsWsh98ED8fGQfZCSvj5xrTmBJv/JHokXfN zGJXa0LeS0AvejU0l7IP/Ex3Bo+pLg9HNBj2Pjv8M2P7ZnEXqHJcOGEgWhtldc9COaAg D7sfhHoGwZJZ7ioqXBFsItVVM9kS4fJ5eeRrIphTx5dYxBj1/lm+Gk5uyUNPX5rQhjDh GQ2CRIEc3/AqwVTTShitfJX59MpfYEprmp0JhBnOfCWSW8Qq00pNBeyaiIiYTzmMBlu3 jKRg== X-Gm-Message-State: AOJu0Yx+d7m6K3HNTzoYIH3e7+You8HzHfkzzEO7T3+zFY6JhOSSqFe0 qFQ3fDejgSTZWjAXNO39SUg8bwvLVGZZ83wBJB7TvsaIN1Y0kWtRZuuII4XVYNansTOgUsmRfIN jaQ== X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a25:ce90:0:b0:dc6:deca:8122 with SMTP id x138-20020a25ce90000000b00dc6deca8122mr35402ybe.5.1706918968697; Fri, 02 Feb 2024 16:09:28 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 2 Feb 2024 16:09:10 -0800 In-Reply-To: <20240203000917.376631-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: <20240203000917.376631-1-seanjc@google.com> X-Mailer: git-send-email 2.43.0.594.gd9cf4e227d-goog Message-ID: <20240203000917.376631-5-seanjc@google.com> Subject: [PATCH v8 04/10] KVM: selftests: Add support for allocating/managing protected guest memory From: Sean Christopherson To: Paolo Bonzini , Marc Zyngier , Oliver Upton , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Sean Christopherson Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, Vishal Annapurve , Ackerley Tng , Andrew Jones , Tom Lendacky , Michael Roth , Peter Gonda X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1789834402332934822 X-GMAIL-MSGID: 1789834402332934822 From: Peter Gonda Add support for differentiating between protected (a.k.a. private, a.k.a. encrypted) memory and normal (a.k.a. shared) memory for VMs that support protected guest memory, e.g. x86's SEV. Provide and manage a common bitmap for tracking whether a given physical page resides in protected memory, as support for protected memory isn't x86 specific, i.e. adding a arch hook would be a net negative now, and in the future. Cc: Paolo Bonzini Cc: Sean Christopherson Cc: Vishal Annapurve Cc: Ackerley Tng cc: Andrew Jones Cc: Tom Lendacky Cc: Michael Roth Originally-by: Michael Roth Signed-off-by: Peter Gonda Co-developed-by: Sean Christopherson Signed-off-by: Sean Christopherson Reviewed-by: Itaru Kitayama --- .../selftests/kvm/include/kvm_util_base.h | 25 +++++++++++++++++-- tools/testing/selftests/kvm/lib/kvm_util.c | 22 +++++++++++++--- 2 files changed, 41 insertions(+), 6 deletions(-) diff --git a/tools/testing/selftests/kvm/include/kvm_util_base.h b/tools/testing/selftests/kvm/include/kvm_util_base.h index d9dc31af2f96..a82149305349 100644 --- a/tools/testing/selftests/kvm/include/kvm_util_base.h +++ b/tools/testing/selftests/kvm/include/kvm_util_base.h @@ -46,6 +46,7 @@ typedef uint64_t vm_vaddr_t; /* Virtual Machine (Guest) virtual address */ struct userspace_mem_region { struct kvm_userspace_memory_region2 region; struct sparsebit *unused_phy_pages; + struct sparsebit *protected_phy_pages; int fd; off_t offset; enum vm_mem_backing_src_type backing_src_type; @@ -573,6 +574,13 @@ void vm_mem_add(struct kvm_vm *vm, enum vm_mem_backing_src_type src_type, uint64_t guest_paddr, uint32_t slot, uint64_t npages, uint32_t flags, int guest_memfd_fd, uint64_t guest_memfd_offset); +#ifndef vm_arch_has_protected_memory +static inline bool vm_arch_has_protected_memory(struct kvm_vm *vm) +{ + return false; +} +#endif + void vm_mem_region_set_flags(struct kvm_vm *vm, uint32_t slot, uint32_t flags); void vm_mem_region_move(struct kvm_vm *vm, uint32_t slot, uint64_t new_gpa); void vm_mem_region_delete(struct kvm_vm *vm, uint32_t slot); @@ -836,10 +844,23 @@ const char *exit_reason_str(unsigned int exit_reason); vm_paddr_t vm_phy_page_alloc(struct kvm_vm *vm, vm_paddr_t paddr_min, uint32_t memslot); -vm_paddr_t vm_phy_pages_alloc(struct kvm_vm *vm, size_t num, - vm_paddr_t paddr_min, uint32_t memslot); +vm_paddr_t __vm_phy_pages_alloc(struct kvm_vm *vm, size_t num, + vm_paddr_t paddr_min, uint32_t memslot, + bool protected); vm_paddr_t vm_alloc_page_table(struct kvm_vm *vm); +static inline vm_paddr_t vm_phy_pages_alloc(struct kvm_vm *vm, size_t num, + vm_paddr_t paddr_min, uint32_t memslot) +{ + /* + * By default, allocate memory as protected for VMs that support + * protected memory, as the majority of memory for such VMs is + * protected, i.e. using shared memory is effectively opt-in. + */ + return __vm_phy_pages_alloc(vm, num, paddr_min, memslot, + vm_arch_has_protected_memory(vm)); +} + /* * ____vm_create() does KVM_CREATE_VM and little else. __vm_create() also * loads the test binary into guest memory and creates an IRQ chip (x86 only). diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/selftests/kvm/lib/kvm_util.c index a53caf81eb87..ea677aa019ef 100644 --- a/tools/testing/selftests/kvm/lib/kvm_util.c +++ b/tools/testing/selftests/kvm/lib/kvm_util.c @@ -717,6 +717,7 @@ static void __vm_mem_region_delete(struct kvm_vm *vm, vm_ioctl(vm, KVM_SET_USER_MEMORY_REGION2, ®ion->region); sparsebit_free(®ion->unused_phy_pages); + sparsebit_free(®ion->protected_phy_pages); ret = munmap(region->mmap_start, region->mmap_size); TEST_ASSERT(!ret, __KVM_SYSCALL_ERROR("munmap()", ret)); if (region->fd >= 0) { @@ -1098,6 +1099,8 @@ void vm_mem_add(struct kvm_vm *vm, enum vm_mem_backing_src_type src_type, } region->unused_phy_pages = sparsebit_alloc(); + if (vm_arch_has_protected_memory(vm)) + region->protected_phy_pages = sparsebit_alloc(); sparsebit_set_num(region->unused_phy_pages, guest_paddr >> vm->page_shift, npages); region->region.slot = slot; @@ -1924,6 +1927,10 @@ void vm_dump(FILE *stream, struct kvm_vm *vm, uint8_t indent) region->host_mem); fprintf(stream, "%*sunused_phy_pages: ", indent + 2, ""); sparsebit_dump(stream, region->unused_phy_pages, 0); + if (region->protected_phy_pages) { + fprintf(stream, "%*sprotected_phy_pages: ", indent + 2, ""); + sparsebit_dump(stream, region->protected_phy_pages, 0); + } } fprintf(stream, "%*sMapped Virtual Pages:\n", indent, ""); sparsebit_dump(stream, vm->vpages_mapped, indent + 2); @@ -2025,6 +2032,7 @@ const char *exit_reason_str(unsigned int exit_reason) * num - number of pages * paddr_min - Physical address minimum * memslot - Memory region to allocate page from + * protected - True if the pages will be used as protected/private memory * * Output Args: None * @@ -2036,8 +2044,9 @@ const char *exit_reason_str(unsigned int exit_reason) * and their base address is returned. A TEST_ASSERT failure occurs if * not enough pages are available at or above paddr_min. */ -vm_paddr_t vm_phy_pages_alloc(struct kvm_vm *vm, size_t num, - vm_paddr_t paddr_min, uint32_t memslot) +vm_paddr_t __vm_phy_pages_alloc(struct kvm_vm *vm, size_t num, + vm_paddr_t paddr_min, uint32_t memslot, + bool protected) { struct userspace_mem_region *region; sparsebit_idx_t pg, base; @@ -2050,8 +2059,10 @@ vm_paddr_t vm_phy_pages_alloc(struct kvm_vm *vm, size_t num, paddr_min, vm->page_size); region = memslot2region(vm, memslot); + TEST_ASSERT(!protected || region->protected_phy_pages, + "Region doesn't support protected memory"); + base = pg = paddr_min >> vm->page_shift; - do { for (; pg < base + num; ++pg) { if (!sparsebit_is_set(region->unused_phy_pages, pg)) { @@ -2070,8 +2081,11 @@ vm_paddr_t vm_phy_pages_alloc(struct kvm_vm *vm, size_t num, abort(); } - for (pg = base; pg < base + num; ++pg) + for (pg = base; pg < base + num; ++pg) { sparsebit_clear(region->unused_phy_pages, pg); + if (protected) + sparsebit_set(region->protected_phy_pages, pg); + } return base * vm->page_size; } From patchwork Sat Feb 3 00:09:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 196106 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:9bc1:b0:106:209c:c626 with SMTP id op1csp769924dyc; Fri, 2 Feb 2024 16:12:02 -0800 (PST) X-Google-Smtp-Source: AGHT+IHWgOXmmX9z5A1swlkHCMGMo4OhrJecuKnnlMv+mNzWDYo8XbRXAAQ7QXK9/NzzmBjh/pvL X-Received: by 2002:aa7:d890:0:b0:560:799:f802 with SMTP id u16-20020aa7d890000000b005600799f802mr713754edq.21.1706919121828; Fri, 02 Feb 2024 16:12:01 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706919121; cv=pass; d=google.com; s=arc-20160816; b=ZZcD+jUg1WAMIre9pYEZ9YTuqhjVglOnCbfYhDjSHbccDT62cK7UefR8mkx1s7SgPU kpZr4a/sOtogYPV0nNEq/ldUn+oRmSkyqKkaDvAfVCpvaTi5t/A63pPTCQWOlbl9su72 RXi3DPUPmqXKlh7iCMfCAfLdqTWa1aaN6/XrUsf5qRAgVqMyBtEGE+GOSktCm5hQJCWV v6x8P1fC0IwPoU0QGcfh5TVjoNhSi1j9H1lwnZystyTPbDhQiqtpFNTl1m6zpf+vAKj9 rpp0a8li9vLvulKu1r8K+fYYSTv4aTDLpnASvQ4meHA98Le4hCO2lb5gHsGl8o6MtrHm zIOQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:message-id:references:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:in-reply-to:date :reply-to:dkim-signature; bh=5qit/ax01cjsi8jn+OF5XBDYnQC7hsp7dQaMdNEMAzo=; fh=77zCzlRNJ7R9RcV+rk/vZ+5U93O2r/1eOCRCthCDPVM=; b=dx72eSWwNlQSpvObPYQiuR3MFZyN23aYOHTJ32ih+fSVwgQUnLcfN+935qj76ydYgW WB3NLTEA+OShWjIVO8zaMFRq8iNQsfrUr9PLQ/4HWJtQXEKxHQFyYQJSBlDTuqHSB+um 4t6kG7/xijn4/1AmzRoCI1smflWPOOSMhX6lmClS1TwQlE0bpAj5uZKZgtRjQu/SpaY5 Ov8u9JjOQBdCwQ4jmM5FRiU587cL1rRhTV5X+6qQWvpME3UjCV3KHCyXs0vGwYhvpYZ1 FMTvnLOrPdOfPN4TH4Y+jGyZ05DSFXEq1pljlpvxbalQ7xSj5R5SD6E6S15UMwuivKVr wnng==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=xBbbsGRc; arc=pass (i=1 spf=pass spfdomain=flex--seanjc.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-50751-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-50751-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com X-Forwarded-Encrypted: i=1; AJvYcCXNfu5Pq3Ozjsr5vuX3Er2ochihw7Qt2cjjbo3G8Me3xZkEnc1mvbNpnE6vZxHYiR528XDRrBdxYI35eJrbsyX9SjJgzw== Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id a8-20020a509b48000000b0055fc426b511si1267329edj.641.2024.02.02.16.12.01 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 Feb 2024 16:12:01 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-50751-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=xBbbsGRc; arc=pass (i=1 spf=pass spfdomain=flex--seanjc.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-50751-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-50751-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 3E2D31F2262A for ; Sat, 3 Feb 2024 00:12:01 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 1B6FB11738; Sat, 3 Feb 2024 00:09:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="xBbbsGRc" 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 CDD628F72 for ; Sat, 3 Feb 2024 00:09:31 +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=1706918973; cv=none; b=BCRwm1tOpCiCuoVqXb//9Rbifd6WHrZycOXKup75BjrTrydz6TSAod1+ZK4nl9LErm9dZawFNi/m5eyxSWiK3i2Lki8+FZ659iyAffeZqQ5HG9/CJxEx4xW1TPv2tVLdNeilwFCD67a+A5rV1yE4WGk8CwuTgtAkQM2BWTs53cM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706918973; c=relaxed/simple; bh=eEsuDAyYhqh0PRIkmb2mwUAqWJ2Pol0Ha6iX400H8fc=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=DzKOSTIUy5qTwO7bUYFfYXLbC7lLz/skpTIbSAa3v+439+NIR8pO0/dM+zlrkX/5ESXGkmrjZLBlM/uAEAX1C5+rAb5Hk6KmLEdPHED3Z892KVPs2/AdYobkQElFFPeQKh7LzSagHWfXHH84yjX1PCasGE82v71iIP99RXlBg1Q= 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=xBbbsGRc; 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 Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-6041e5b4367so38749347b3.3 for ; Fri, 02 Feb 2024 16:09:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1706918971; x=1707523771; 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=5qit/ax01cjsi8jn+OF5XBDYnQC7hsp7dQaMdNEMAzo=; b=xBbbsGRczX9e6fFUaFftX9mbr+YTAnGcrCF0wxijp5Pg0y8c3hL4zKqkKl2xF/++5D Qim+Kmq+PowZDSCqlfAv9A+iXYl/+3ohyQgHXTg/y9sYDBo6CB3PeKihRh2PuFzG/vWM oYN9q1jqfaxZELkUhzKGnfj8+FDEV4lSRKD1GErcojnu3+G42PzdXbHdqk5pY88gZ8ri qtbJNHCkoIzt1l3duR7NYpH9GYgFAPXIlYmODppjNjbFZ5IozElMJgX9jl5+u+XtxyP9 9ysVF4a5cUaYz3xO0pwbtqUDVs0vbIW0AqlkqtN7KdYYWowJqEdkRiO3cSDrDR9iVb4K jEpw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706918971; x=1707523771; 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=5qit/ax01cjsi8jn+OF5XBDYnQC7hsp7dQaMdNEMAzo=; b=FZsZJACWwYeiaSYRfvbwIcW2LdYaq9aQA7l6cMminLAL/lRYD2SWKapPhwrfVLIXIw yj3GLK+gUWIC/SIU0ECuBkoUzee3gOL7QotNWmSJYzhbyC4P4sOkj2KET3W/QyAGGuAg EWdbPQHUJ2rgcR+5EfSxg/o0+CjYvlKgZweCCH55I2Yg68BDIFqLlJgCSbzATVZm1262 /zrQ6Q0QWfg+dSZdzRYbjuiQpZZNcBw21Y4NrKp1GZEYnn7T5vQEDZgGrrVyi9iXsp4m crrV+TmsQ+vD+zuCTOrT53equijy7QbSJDL5SYVSbZIikZW71HMVLLEIq8TE5D8r88X/ l+RA== X-Gm-Message-State: AOJu0YxpCKN3SF/VZS8NQu0ncMzRqOv9UvlnoMmJFzJ+aFXcqm0n4P+O 1Y0F/euo28k3QkImBFGt592tjzDRgXexEp9ABVgBU3mzaqlU4ra6WzmzKleE/nFJk8CD3brMUhl aCQ== X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a81:4cd5:0:b0:602:d83f:bf36 with SMTP id z204-20020a814cd5000000b00602d83fbf36mr1585025ywa.0.1706918970863; Fri, 02 Feb 2024 16:09:30 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 2 Feb 2024 16:09:11 -0800 In-Reply-To: <20240203000917.376631-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: <20240203000917.376631-1-seanjc@google.com> X-Mailer: git-send-email 2.43.0.594.gd9cf4e227d-goog Message-ID: <20240203000917.376631-6-seanjc@google.com> Subject: [PATCH v8 05/10] KVM: selftests: Add support for protected vm_vaddr_* allocations From: Sean Christopherson To: Paolo Bonzini , Marc Zyngier , Oliver Upton , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Sean Christopherson Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, Vishal Annapurve , Ackerley Tng , Andrew Jones , Tom Lendacky , Michael Roth , Peter Gonda X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1789834424988780166 X-GMAIL-MSGID: 1789834424988780166 From: Michael Roth Test programs may wish to allocate shared vaddrs for things like sharing memory with the guest. Since protected vms will have their memory encrypted by default an interface is needed to explicitly request shared pages. Implement this by splitting the common code out from vm_vaddr_alloc() and introducing a new vm_vaddr_alloc_shared(). Cc: Paolo Bonzini Cc: Sean Christopherson Cc: Vishal Annapurve Cc: Ackerly Tng cc: Andrew Jones Cc: Tom Lendacky Cc: Michael Roth Signed-off-by: Michael Roth Signed-off-by: Peter Gonda Signed-off-by: Sean Christopherson --- .../selftests/kvm/include/kvm_util_base.h | 3 +++ tools/testing/selftests/kvm/lib/kvm_util.c | 26 +++++++++++++++---- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/tools/testing/selftests/kvm/include/kvm_util_base.h b/tools/testing/selftests/kvm/include/kvm_util_base.h index a82149305349..cb3159af6db3 100644 --- a/tools/testing/selftests/kvm/include/kvm_util_base.h +++ b/tools/testing/selftests/kvm/include/kvm_util_base.h @@ -590,6 +590,9 @@ vm_vaddr_t vm_vaddr_unused_gap(struct kvm_vm *vm, size_t sz, vm_vaddr_t vaddr_mi vm_vaddr_t vm_vaddr_alloc(struct kvm_vm *vm, size_t sz, vm_vaddr_t vaddr_min); vm_vaddr_t __vm_vaddr_alloc(struct kvm_vm *vm, size_t sz, vm_vaddr_t vaddr_min, enum kvm_mem_region_type type); +vm_vaddr_t vm_vaddr_alloc_shared(struct kvm_vm *vm, size_t sz, + vm_vaddr_t vaddr_min, + enum kvm_mem_region_type type); vm_vaddr_t vm_vaddr_alloc_pages(struct kvm_vm *vm, int nr_pages); vm_vaddr_t __vm_vaddr_alloc_page(struct kvm_vm *vm, enum kvm_mem_region_type type); diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/selftests/kvm/lib/kvm_util.c index ea677aa019ef..e7f4f84f2e68 100644 --- a/tools/testing/selftests/kvm/lib/kvm_util.c +++ b/tools/testing/selftests/kvm/lib/kvm_util.c @@ -1431,15 +1431,17 @@ vm_vaddr_t vm_vaddr_unused_gap(struct kvm_vm *vm, size_t sz, return pgidx_start * vm->page_size; } -vm_vaddr_t __vm_vaddr_alloc(struct kvm_vm *vm, size_t sz, vm_vaddr_t vaddr_min, - enum kvm_mem_region_type type) +static vm_vaddr_t ____vm_vaddr_alloc(struct kvm_vm *vm, size_t sz, + vm_vaddr_t vaddr_min, + enum kvm_mem_region_type type, + bool protected) { uint64_t pages = (sz >> vm->page_shift) + ((sz % vm->page_size) != 0); virt_pgd_alloc(vm); - vm_paddr_t paddr = vm_phy_pages_alloc(vm, pages, - KVM_UTIL_MIN_PFN * vm->page_size, - vm->memslots[type]); + vm_paddr_t paddr = __vm_phy_pages_alloc(vm, pages, + KVM_UTIL_MIN_PFN * vm->page_size, + vm->memslots[type], protected); /* * Find an unused range of virtual page addresses of at least @@ -1459,6 +1461,20 @@ vm_vaddr_t __vm_vaddr_alloc(struct kvm_vm *vm, size_t sz, vm_vaddr_t vaddr_min, return vaddr_start; } +vm_vaddr_t __vm_vaddr_alloc(struct kvm_vm *vm, size_t sz, vm_vaddr_t vaddr_min, + enum kvm_mem_region_type type) +{ + return ____vm_vaddr_alloc(vm, sz, vaddr_min, type, + vm_arch_has_protected_memory(vm)); +} + +vm_vaddr_t vm_vaddr_alloc_shared(struct kvm_vm *vm, size_t sz, + vm_vaddr_t vaddr_min, + enum kvm_mem_region_type type) +{ + return ____vm_vaddr_alloc(vm, sz, vaddr_min, type, false); +} + /* * VM Virtual Address Allocate * From patchwork Sat Feb 3 00:09:12 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 196111 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:9bc1:b0:106:209c:c626 with SMTP id op1csp771672dyc; Fri, 2 Feb 2024 16:16:50 -0800 (PST) X-Google-Smtp-Source: AGHT+IF88JaYpGiC7OaMIldibt2Ixcz//bvLcwHFNuwFCAzOfXtpoPULmMl3Ua8nTwzv1UydyWa2 X-Received: by 2002:a17:90b:1957:b0:296:58cc:2124 with SMTP id nk23-20020a17090b195700b0029658cc2124mr1801858pjb.16.1706919410208; Fri, 02 Feb 2024 16:16:50 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706919410; cv=pass; d=google.com; s=arc-20160816; b=uAtOWedPXx6eXHTwFjPi83W4RgrIjWtHfRPvFvLt4JAzvnBlOsgt7k3OhrmIZ5p7p1 zir1UlQHCuCdceK1PqkmyjwOfp8zA5dHnea6aNqX4IYDJ97HrPJiEu63yKAfgRtik5SH agHmClklM51A0b+QtmDs62J+7Dkb7AL5oFBpaE/MJ/Dve8sbI3AwNzU6Xr4Ul0oWp9Kd u+Wqc9Wp8aDNj429v8AvcNO95CLaK27kOV14b9xiawjcq8YSXOdb3cs5FdSeOmPxQrPi 1qM4AUqId5DwzLKfXVk6WHzoLoTs6lndQGGvzLYPYK9vashRHSteLvDTzj6073bWH42x JT+A== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:message-id:references:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:in-reply-to:date :reply-to:dkim-signature; bh=uGdQAEk26j5OxpYXu3PaMK7EeL8Au0IOP5m2/Y3bZT0=; fh=YuRcvbYNQbydvpu9FYYn7AFWaqOF9ReOpClromqPTxA=; b=bLUPWBcEVOtm94JgAma2WBByb2k9P1lnlib511pz3Ah/GqPUCUYLxcxpQWTGpITkR3 /j+YT/tgtXUs/ZKyb1145hs7rZG4XgT+T2TQ9jgMbjYvGjQh0KOnvmLEOwZoyQAf5t03 kgk3Dr1zJcAp3i7Lm4rXGHcb20SPmCzA9qZTP5eSvZCyAr2NwnFUd1/Q0sqUGjV44Tdh 9Awyld9rT8YLqhcmRYjnDemfWsq322mcXfgb5sljYLdTa9tBscktBCBdXA5qHBLXrcMb 3MQdR/SOhyQtOFncFHsxNcG0MeFL7IImLiZDt9pDaCiY1nSIq1RY8/v22Dw++s1AX7kC BI8w==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="vjb9Ij/6"; arc=pass (i=1 spf=pass spfdomain=flex--seanjc.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-50752-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-50752-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com X-Forwarded-Encrypted: i=1; AJvYcCWQDAx7fYvMCruzfGFvQ54ALwactDVNMWnqiKYhUvIBz18VoKvI5ZHRt25sJbb1DQ5N4OYgYzcLNvyc6Hx/Jtyn5WSwRw== Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id 17-20020a170902ee5100b001d963a3b596si2362967plo.200.2024.02.02.16.16.49 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 Feb 2024 16:16:50 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-50752-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="vjb9Ij/6"; arc=pass (i=1 spf=pass spfdomain=flex--seanjc.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-50752-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-50752-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id 3A4B7B28BEE for ; Sat, 3 Feb 2024 00:12:14 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 07D1612B78; Sat, 3 Feb 2024 00:09:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="vjb9Ij/6" 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 608F4EEAB for ; Sat, 3 Feb 2024 00:09:33 +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=1706918974; cv=none; b=B3VhLB3Jc+9BNEdqpHcrYHvrL8PfOkjML5VeR4aQQlXQ7ovDnSGtMy3WWq+QxOeqU1SvE3JrFGXPK/x6GOB7qazbT9hHxkBze2AUJlUnQ/XMkZyiK+ewo/CaOQ5Wk3ThjqjJklDE+cw/VfRyMFTsVg9AVNE8RPQlqCZg0Ah0HFI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706918974; c=relaxed/simple; bh=D8mb0FRK9gpBnBRXkQEG+PB9Dag5X8x5Fbzx9RXG+Rg=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=SRk4pbz6DUHQ9B1mb7MntX64PU/+Wn6852aQ8zPT9iH191/hgaX9chfJ4eY2cLLCGmwczPU0BwbLxf9elKuGApHoWIPgBde9kSmnuH4A9o72ta9321HpzcPpZ/1uzm2wraG0dQZnjSEdzkHpsRWQi2gsD71kCqDx5u17bX/ot1U= 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=vjb9Ij/6; 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 Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-1d45d23910aso26054245ad.1 for ; Fri, 02 Feb 2024 16:09:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1706918973; x=1707523773; 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=uGdQAEk26j5OxpYXu3PaMK7EeL8Au0IOP5m2/Y3bZT0=; b=vjb9Ij/6tmv+EekvnSYVIlmbw7VBjrmOZp+aU/COej0BFX1gexhugakIc0C53NjjGf TUcpWjHo/jGoTlU8hnPwYl9yQX9h7/J13VZydkPJ1EGR+vSL8Qv0b8/LzSTT/FPCBXCx avmHI0sDDTogW2uBzRhR7LtPj1NUvlqfdkm8mho8NPTd9cKZnMIvMtewqNmPDxTZ6+Wr nXExugYjWuWs3H8fTsWvW1XvYdSgqrQnqvXBPSBZaiJlLzPtnRcp/YkKZ4CxlBraZ3Sr aHHyXufNt4tSytE6stJz1IU5aDrxRRUAdlKig4Iyd24OVyWTq1pxjhT655Vp8Zhp7Q9G 3bdg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706918973; x=1707523773; 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=uGdQAEk26j5OxpYXu3PaMK7EeL8Au0IOP5m2/Y3bZT0=; b=tHYgnBPIV0E6S7J1bcogqYImJUkArQ63WW0XjLsW02Vt1pxi5/QMifZkMS5nfYbo7X 7mznFbRlw6huR0SXjtNY+3REzS8JDeHwmrJU7zTybhRqIwHEezBe9YWlxWSd7uP0ggyU zzcqTiyDZWXRmUXf0UZvTxnxqg9e/zfslDSe2Z3sq5dMbdsG5QYCOsUKiRlfSSNOk1mB clbm15vp55G6xE76Ha2VObxao3GiYCDKyvRVT9mFsYpZun56Cl0zn1K5z9cV5JWMGKxc 5kXarr/K+lNhUuU4NkxQ7PbHC2Jtb2LFbiwFfmL8fDS54MBkX7H0KEqoeb7tT0CCg7qx AF/Q== X-Gm-Message-State: AOJu0YxTkYiZOdWpqbYGnPzu7/RCGYKN1m4CVltfLJxeStZzNYpiMsYI phyY11G1zESkCeROTQuDFFYXmIzNK7RburBNGCoeRN+nogxOjUrXurEpXj3Yvp3sYVYIMi5hKvW yWQ== X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:902:e80c:b0:1d9:855b:629f with SMTP id u12-20020a170902e80c00b001d9855b629fmr3990plg.11.1706918972726; Fri, 02 Feb 2024 16:09:32 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 2 Feb 2024 16:09:12 -0800 In-Reply-To: <20240203000917.376631-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: <20240203000917.376631-1-seanjc@google.com> X-Mailer: git-send-email 2.43.0.594.gd9cf4e227d-goog Message-ID: <20240203000917.376631-7-seanjc@google.com> Subject: [PATCH v8 06/10] KVM: selftests: Explicitly ucall pool from shared memory From: Sean Christopherson To: Paolo Bonzini , Marc Zyngier , Oliver Upton , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Sean Christopherson Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, Vishal Annapurve , Ackerley Tng , Andrew Jones , Tom Lendacky , Michael Roth , Peter Gonda X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1789834727897078149 X-GMAIL-MSGID: 1789834727897078149 From: Peter Gonda Allocate the common ucall pool using vm_vaddr_alloc_shared() so that the ucall structures will be placed in shared (unencrypted) memory for VMs with support for protected (encrypted) memory, e.g. x86's SEV. Cc: Paolo Bonzini Cc: Sean Christopherson Cc: Vishal Annapurve Cc: Ackerly Tng cc: Andrew Jones Cc: Tom Lendacky Cc: Michael Roth Signed-off-by: Peter Gonda [sean: massage changelog] Signed-off-by: Sean Christopherson --- tools/testing/selftests/kvm/lib/ucall_common.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/kvm/lib/ucall_common.c b/tools/testing/selftests/kvm/lib/ucall_common.c index 816a3fa109bf..f5af65a41c29 100644 --- a/tools/testing/selftests/kvm/lib/ucall_common.c +++ b/tools/testing/selftests/kvm/lib/ucall_common.c @@ -29,7 +29,8 @@ void ucall_init(struct kvm_vm *vm, vm_paddr_t mmio_gpa) vm_vaddr_t vaddr; int i; - vaddr = __vm_vaddr_alloc(vm, sizeof(*hdr), KVM_UTIL_MIN_VADDR, MEM_REGION_DATA); + vaddr = vm_vaddr_alloc_shared(vm, sizeof(*hdr), KVM_UTIL_MIN_VADDR, + MEM_REGION_DATA); hdr = (struct ucall_header *)addr_gva2hva(vm, vaddr); memset(hdr, 0, sizeof(*hdr)); From patchwork Sat Feb 3 00:09:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 196114 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:9bc1:b0:106:209c:c626 with SMTP id op1csp771941dyc; Fri, 2 Feb 2024 16:17:35 -0800 (PST) X-Google-Smtp-Source: AGHT+IFV3BOG6qndw3y+x1WyVe8OGP2fJbv+LZzclJMqtvXn064wNRibYY3LwGUnzMloABkC0yED X-Received: by 2002:a05:6a20:9f4e:b0:19e:3a9f:f925 with SMTP id ml14-20020a056a209f4e00b0019e3a9ff925mr9007956pzb.14.1706919454957; Fri, 02 Feb 2024 16:17:34 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706919454; cv=pass; d=google.com; s=arc-20160816; b=QwLc0Jm1N3PAza6ggQpOo3FmVi9wVtw31f/d8j0in3DSJFmG7tjnKNnNwNvbrsPOqj sG7uJlzPt8pziL6hcnIvMRqN6IcLUV1T+koyPVMUCKc2hh92T8IK1YZuvBwwVS2Xolhs GJ0c7WE9d7id93IMZ3vldnNqnHo889hRZx0AUqELI6W/r2ruNoXodW3r6alDZLr5ReKj plovSM3kdTuwXYL6YtNrloqVDo43mAgo2hFmVt/2cFXs+rA386HmxLC3mz3ro3KdHWIs UDELxBhoiBXuAu5j3v7cQHfDs+4mXHg9faFMhAwV8mro2PRIVY66Q9GjNjcJsyI2Nuvr LgMA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:message-id:references:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:in-reply-to:date :reply-to:dkim-signature; bh=kcNwe0n4y+fi75yl+T1k9chorTvmU12VF8T08tNTnrE=; fh=pgBdVIGOhIfkrvTrl1+Y2Bt22l06zHhw+t0t4pavaNY=; b=OUpzHlTwdAPNqxUWS/OXNuvPD4cOveDRNrQpBAzHbwiRRxlkJ9TkYDh+OnlxHBq7Ge ayKpdeGez2vvmVlwJ9RK0xGNQfI1iryKwjMjMs+bp5aVjFG2aODRGar++K2SA0EZQOTV MDgGAepwBE3Dk88RLT/8VSZLv+6dZgSHJ0dNvtU0qIEOJdPHkoBvM2X4ibQBHnBmCHH5 3aK9RsHh4yFilKZhTvft9SdKOxZIr3RlLFJ80uaq18sJ5gECsF6JNbnD24avw7/gewno rJ2MnKv7mL6HoWFKMIWjNTlP4cNHvrX7bMgrpBzQi4r1qbM814n7ja+gxafjZrM0sc/g QPaQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=2sHy4m4y; arc=pass (i=1 spf=pass spfdomain=flex--seanjc.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-50753-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-50753-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com X-Forwarded-Encrypted: i=1; AJvYcCVKSARRr+QH3OGCKTUa1dHZ/x99DBDCZrt3Gofl4GAYLXatLhIk1AEamEn749Y4dgO+YOaGGmSHNkP2K9M/Q9wUxqciWQ== Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id i12-20020a63540c000000b005ceb4a70475si2323453pgb.83.2024.02.02.16.17.34 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 Feb 2024 16:17:34 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-50753-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=2sHy4m4y; arc=pass (i=1 spf=pass spfdomain=flex--seanjc.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-50753-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-50753-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id AD6DBB28FAC for ; Sat, 3 Feb 2024 00:12:45 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 92D701640B; Sat, 3 Feb 2024 00:09:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="2sHy4m4y" 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 5335510A2D for ; Sat, 3 Feb 2024 00:09:35 +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=1706918977; cv=none; b=BohHYQGd4gUnkwwofjQz2Mzl6Wp5DEzpACjRdWzEel6B9mem9ccIQPUAAjo0ykuGEAAYE8bm2L7f62yfBbhnivGfP6VgH3toyGn7H673XPLdL/GTk7haXve1blwsgRFISXcEjX0VaaonmCxI1kgwEm6CMXmns4yflrKit6mA5ts= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706918977; c=relaxed/simple; bh=qQqc0EiIYxFI/LhKRBkJ7kQPJAVfHL4+1rGyNOlKFGs=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=O7UUyJLeBrOUwm7rAbfsBqEOlGWQIudawi/gSHFxlS56THVGY+axysVMHtAczi+VwQWbv1698pvBSL5ql+p/GgZpiHNpvE91J5NKeTqVMB5DVQe1Bgl8tp7aHMwNF9K+vj+7B4neRVilnRtkRdaViRhbrcsgQo7JPhwihiTFO8E= 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=2sHy4m4y; 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 Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-60404484c23so50387387b3.1 for ; Fri, 02 Feb 2024 16:09:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1706918974; x=1707523774; 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=kcNwe0n4y+fi75yl+T1k9chorTvmU12VF8T08tNTnrE=; b=2sHy4m4ySh2fExVNQN0fQDvZe+QQ5G9KOGhkhSjA/tP6AaN4kvadnL7qmnUFqrI/7w i7AEpeDekIdSfcshFnx6qXm3u0iipQvNkw3D0Bjyo0hMnvCYschr/LaCckWL3p1yisz1 XLgMZgyWmfMXN+waiHcdNkmBgOnSJ12g/oO08+pE9G427YEsleOMSwILMvpaBzSGdlmL aR64tKalPv/UFxfklgPLh44/RcjfOjXOsGhfehiYR26LCtkcqRG5f6JMIyihCpWhQwLg hSfX/fsspRqYSmf4RiPd57aWQ7zsUOMf+9ukT+JwxClWC7jKd/vnJA8xDkSx9M6OAPgy EdCg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706918974; x=1707523774; 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=kcNwe0n4y+fi75yl+T1k9chorTvmU12VF8T08tNTnrE=; b=G2EpWC8Jv15pAnl4GPPTnipjwWsE9MEfbTk5/8qde5LvsAS5h7GTvfrxTihWc/p2cx c8ioLaqEHA4iXFbT79g39Uspwh0+nk1epdfH0/LW/JMsOt/IghvUElim/F6OXDeS/9mh +eu4RJM5NSh9yNN0JjOpOWrhcuwAcppGRAdVr53CHdSchGgvV1sMBSnqt5/s/rqGAEjL Ib2bETXDJwAptgy5FWKu930xEjOrCnBvIO4zkskjGqtUFukx7Ed/+nRY8ka2lCbhGylx bwJD0JXXMqpDNko/BtgNQRRrh1XV5desTq3zCmRCerBWreQRimkJ+wygAYYxQ2a5Bs9w pZaA== X-Gm-Message-State: AOJu0YyNgnENsZam+GMEoaddHoEDXPf2akH1DR94/RTWNfzJGbQ/OVqA +Tjs1MT72pgUtl6ZQ8QwYGS2hPfa4OIijwmqdWKI2n2/Tx3qS6RIto8tkXa54fUBwJOTshMm96y 03Q== X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6902:920:b0:dc6:55ac:d08d with SMTP id bu32-20020a056902092000b00dc655acd08dmr2378837ybb.5.1706918974538; Fri, 02 Feb 2024 16:09:34 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 2 Feb 2024 16:09:13 -0800 In-Reply-To: <20240203000917.376631-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: <20240203000917.376631-1-seanjc@google.com> X-Mailer: git-send-email 2.43.0.594.gd9cf4e227d-goog Message-ID: <20240203000917.376631-8-seanjc@google.com> Subject: [PATCH v8 07/10] KVM: selftests: Allow tagging protected memory in guest page tables From: Sean Christopherson To: Paolo Bonzini , Marc Zyngier , Oliver Upton , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Sean Christopherson Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, Vishal Annapurve , Ackerley Tng , Andrew Jones , Tom Lendacky , Michael Roth , Peter Gonda X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1789834774515389816 X-GMAIL-MSGID: 1789834774515389816 From: Peter Gonda Add support for tagging and untagging guest physical address, e.g. to allow x86's SEV and TDX guests to embed shared vs. private information in the GPA. SEV (encryption, a.k.a. C-bit) and TDX (shared, a.k.a. S-bit) steal bits from the guest's physical address space that is consumed by the CPU metadata, i.e. effectively aliases the "real" GPA. Implement generic "tagging" so that the shared vs. private metadata can be managed by x86 without bleeding too many details into common code. Cc: Paolo Bonzini Cc: Sean Christopherson Cc: Vishal Annapurve Cc: Ackerly Tng cc: Andrew Jones Cc: Tom Lendacky Cc: Michael Roth Originally-by: Michael Roth Signed-off-by: Peter Gonda Signed-off-by: Sean Christopherson --- .../kvm/include/aarch64/kvm_util_arch.h | 7 +++++++ .../selftests/kvm/include/kvm_util_base.h | 13 ++++++++++++ .../kvm/include/riscv/kvm_util_arch.h | 7 +++++++ .../kvm/include/s390x/kvm_util_arch.h | 7 +++++++ .../kvm/include/x86_64/kvm_util_arch.h | 21 +++++++++++++++++++ tools/testing/selftests/kvm/lib/kvm_util.c | 17 +++++++++++++++ .../selftests/kvm/lib/x86_64/processor.c | 15 ++++++++++++- 7 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/kvm/include/aarch64/kvm_util_arch.h create mode 100644 tools/testing/selftests/kvm/include/riscv/kvm_util_arch.h create mode 100644 tools/testing/selftests/kvm/include/s390x/kvm_util_arch.h create mode 100644 tools/testing/selftests/kvm/include/x86_64/kvm_util_arch.h diff --git a/tools/testing/selftests/kvm/include/aarch64/kvm_util_arch.h b/tools/testing/selftests/kvm/include/aarch64/kvm_util_arch.h new file mode 100644 index 000000000000..218f5cdf0d86 --- /dev/null +++ b/tools/testing/selftests/kvm/include/aarch64/kvm_util_arch.h @@ -0,0 +1,7 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef _TOOLS_LINUX_ASM_ARM64_KVM_HOST_H +#define _TOOLS_LINUX_ASM_ARM64_KVM_HOST_H + +struct kvm_vm_arch {}; + +#endif // _TOOLS_LINUX_ASM_ARM64_KVM_HOST_H diff --git a/tools/testing/selftests/kvm/include/kvm_util_base.h b/tools/testing/selftests/kvm/include/kvm_util_base.h index cb3159af6db3..4b266dc0c9bd 100644 --- a/tools/testing/selftests/kvm/include/kvm_util_base.h +++ b/tools/testing/selftests/kvm/include/kvm_util_base.h @@ -18,9 +18,11 @@ #include #include +#include #include +#include "kvm_util_arch.h" #include "sparsebit.h" /* @@ -113,6 +115,9 @@ struct kvm_vm { vm_vaddr_t idt; vm_vaddr_t handlers; uint32_t dirty_ring_size; + uint64_t gpa_tag_mask; + + struct kvm_vm_arch arch; /* Cache of information for binary stats interface */ int stats_fd; @@ -605,6 +610,12 @@ void *addr_gva2hva(struct kvm_vm *vm, vm_vaddr_t gva); vm_paddr_t addr_hva2gpa(struct kvm_vm *vm, void *hva); void *addr_gpa2alias(struct kvm_vm *vm, vm_paddr_t gpa); + +static inline vm_paddr_t vm_untag_gpa(struct kvm_vm *vm, vm_paddr_t gpa) +{ + return gpa & ~vm->gpa_tag_mask; +} + void vcpu_run(struct kvm_vcpu *vcpu); int _vcpu_run(struct kvm_vcpu *vcpu); @@ -1114,4 +1125,6 @@ void kvm_selftest_arch_init(void); void kvm_arch_vm_post_create(struct kvm_vm *vm); +bool vm_is_gpa_protected(struct kvm_vm *vm, vm_paddr_t paddr); + #endif /* SELFTEST_KVM_UTIL_BASE_H */ diff --git a/tools/testing/selftests/kvm/include/riscv/kvm_util_arch.h b/tools/testing/selftests/kvm/include/riscv/kvm_util_arch.h new file mode 100644 index 000000000000..c8280d5659ce --- /dev/null +++ b/tools/testing/selftests/kvm/include/riscv/kvm_util_arch.h @@ -0,0 +1,7 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef _TOOLS_LINUX_ASM_RISCV_KVM_HOST_H +#define _TOOLS_LINUX_ASM_RISCV_KVM_HOST_H + +struct kvm_vm_arch {}; + +#endif // _TOOLS_LINUX_ASM_RISCV_KVM_HOST_H diff --git a/tools/testing/selftests/kvm/include/s390x/kvm_util_arch.h b/tools/testing/selftests/kvm/include/s390x/kvm_util_arch.h new file mode 100644 index 000000000000..4c4c1c1e4bf8 --- /dev/null +++ b/tools/testing/selftests/kvm/include/s390x/kvm_util_arch.h @@ -0,0 +1,7 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef _TOOLS_LINUX_ASM_S390_KVM_HOST_H +#define _TOOLS_LINUX_ASM_S390_KVM_HOST_H + +struct kvm_vm_arch {}; + +#endif // _TOOLS_LINUX_ASM_S390_KVM_HOST_H diff --git a/tools/testing/selftests/kvm/include/x86_64/kvm_util_arch.h b/tools/testing/selftests/kvm/include/x86_64/kvm_util_arch.h new file mode 100644 index 000000000000..17bb38236d97 --- /dev/null +++ b/tools/testing/selftests/kvm/include/x86_64/kvm_util_arch.h @@ -0,0 +1,21 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef _TOOLS_LINUX_ASM_X86_KVM_HOST_H +#define _TOOLS_LINUX_ASM_X86_KVM_HOST_H + +#include +#include + +struct kvm_vm_arch { + uint64_t c_bit; + uint64_t s_bit; +}; + +static inline bool __vm_arch_has_protected_memory(struct kvm_vm_arch *arch) +{ + return arch->c_bit || arch->s_bit; +} + +#define vm_arch_has_protected_memory(vm) \ + __vm_arch_has_protected_memory(&(vm)->arch) + +#endif // _TOOLS_LINUX_ASM_X86_KVM_HOST_H diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/selftests/kvm/lib/kvm_util.c index e7f4f84f2e68..19511137d1ae 100644 --- a/tools/testing/selftests/kvm/lib/kvm_util.c +++ b/tools/testing/selftests/kvm/lib/kvm_util.c @@ -1597,6 +1597,8 @@ void *addr_gpa2hva(struct kvm_vm *vm, vm_paddr_t gpa) { struct userspace_mem_region *region; + gpa = vm_untag_gpa(vm, gpa); + region = userspace_mem_region_find(vm, gpa, gpa); if (!region) { TEST_FAIL("No vm physical memory at 0x%lx", gpa); @@ -2305,3 +2307,18 @@ void __attribute((constructor)) kvm_selftest_init(void) kvm_selftest_arch_init(); } + +bool vm_is_gpa_protected(struct kvm_vm *vm, vm_paddr_t paddr) +{ + sparsebit_idx_t pg = 0; + struct userspace_mem_region *region; + + if (!vm_arch_has_protected_memory(vm)) + return false; + + region = userspace_mem_region_find(vm, paddr, paddr); + TEST_ASSERT(region, "No vm physical memory at 0x%lx", paddr); + + pg = paddr >> vm->page_shift; + return sparsebit_is_set(region->protected_phy_pages, pg); +} diff --git a/tools/testing/selftests/kvm/lib/x86_64/processor.c b/tools/testing/selftests/kvm/lib/x86_64/processor.c index 615d05247470..6c1d2c0ec584 100644 --- a/tools/testing/selftests/kvm/lib/x86_64/processor.c +++ b/tools/testing/selftests/kvm/lib/x86_64/processor.c @@ -157,6 +157,8 @@ static uint64_t *virt_create_upper_pte(struct kvm_vm *vm, { uint64_t *pte = virt_get_pte(vm, parent_pte, vaddr, current_level); + paddr = vm_untag_gpa(vm, paddr); + if (!(*pte & PTE_PRESENT_MASK)) { *pte = PTE_PRESENT_MASK | PTE_WRITABLE_MASK; if (current_level == target_level) @@ -200,6 +202,8 @@ void __virt_pg_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr, int level) "Physical address beyond maximum supported,\n" " paddr: 0x%lx vm->max_gfn: 0x%lx vm->page_size: 0x%x", paddr, vm->max_gfn, vm->page_size); + TEST_ASSERT(vm_untag_gpa(vm, paddr) == paddr, + "Unexpected bits in paddr: %lx", paddr); /* * Allocate upper level page tables, if not already present. Return @@ -222,6 +226,15 @@ void __virt_pg_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr, int level) TEST_ASSERT(!(*pte & PTE_PRESENT_MASK), "PTE already present for 4k page at vaddr: 0x%lx", vaddr); *pte = PTE_PRESENT_MASK | PTE_WRITABLE_MASK | (paddr & PHYSICAL_PAGE_MASK); + + /* + * Neither SEV nor TDX supports shared page tables, so only the final + * leaf PTE needs manually set the C/S-bit. + */ + if (vm_is_gpa_protected(vm, paddr)) + *pte |= vm->arch.c_bit; + else + *pte |= vm->arch.s_bit; } void virt_arch_pg_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr) @@ -496,7 +509,7 @@ vm_paddr_t addr_arch_gva2gpa(struct kvm_vm *vm, vm_vaddr_t gva) * No need for a hugepage mask on the PTE, x86-64 requires the "unused" * address bits to be zero. */ - return PTE_GET_PA(*pte) | (gva & ~HUGEPAGE_MASK(level)); + return vm_untag_gpa(vm, PTE_GET_PA(*pte)) | (gva & ~HUGEPAGE_MASK(level)); } static void kvm_setup_gdt(struct kvm_vm *vm, struct kvm_dtable *dt) From patchwork Sat Feb 3 00:09:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 196107 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:9bc1:b0:106:209c:c626 with SMTP id op1csp770369dyc; Fri, 2 Feb 2024 16:13:15 -0800 (PST) X-Google-Smtp-Source: AGHT+IGAHVKwbmZZS1LYe+g7DqOUb8Kl8ps4/pDr5AS6KPohDF6FF4IUlqsXMKWKvRNhGJBt0aN2 X-Received: by 2002:a05:6214:2529:b0:68c:7672:1830 with SMTP id gg9-20020a056214252900b0068c76721830mr4404429qvb.22.1706919195408; Fri, 02 Feb 2024 16:13:15 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706919195; cv=pass; d=google.com; s=arc-20160816; b=NGiu3TDljuLJMT+HjN1EdVSXEQ8XBf4AI5tzB6efFWhHk7+GmkMRub1yW6wsacNmo0 fSHrftdR3fkF+X8wslxHjrQqr9OKmAjlvVtE+XRfc40oRpqi26nzNd2+4nb2rMS4ZECG o9tU23I8xeDKXmCh31bwpxDt0zuxt+zuBqMrEhjqkGRJa13xw8jINidmXn1hgUr4qgP8 CfLkV+IaxUTg0XxDqn21oj0lwYqLqN4YHOGfyy0RHSimX2LUubMTGBmLa4l6lW/LiOBS 58KrjyveCN4hgMD9Jt/brvTaGK4dkkg5/HGdFebDSGJUxhee3E37M00TVkzJqFCuggVA 1BYQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:message-id:references:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:in-reply-to:date :reply-to:dkim-signature; bh=lcHhkIfCfD5wroHUDfoUCPFb/M+i7HRMkWQ8hVkrcXw=; fh=v+Fr6JerKypZA8oaX9ICdZuDZuU19XQ3h0E1rEwZEBw=; b=w29q7PH/58zu6JQMU1B1dzVdmZT4GytI8gBYrSZzT3UuGi8A5OekXvOKz6SHjNglbJ er1BuwEfIqA/MjI5OiOS5ZKrNDMbevo1/m9U1U8BGtIzfNRdSQvShTrsFTltWS18kbi5 DnXhyDUxQJw2Crlwez088avCrn+yCoq7OXTUMSgao7YD+yS6qSpRxd1JZhyd7QMt65Ym PYYoyyXw+26Ihv2UhlGbsWGvD2C1/TL1h5ltYZXTaDhA8ZI4W/wPMTlfwgM8slVCgun8 5J4XEv6FOcupnb5dBysyBEzJ+XJmciJ9KxOHQs8TISQIhLVmqNUIxFDxohHehPMO8hug j0uA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=slwDiIB0; arc=pass (i=1 spf=pass spfdomain=flex--seanjc.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-50754-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-50754-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com X-Forwarded-Encrypted: i=1; AJvYcCV29QwxpJQHFCQ27uXK0Wq2cmAj3QaNxqk/DoREhSRef1rHjyKTQ171jQRhjCw29mCeHSauq5ShgBSpAAdHZr2nzU7c9w== Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id l10-20020ad44bca000000b0068c74c895b1si3169192qvw.426.2024.02.02.16.13.15 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 Feb 2024 16:13:15 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-50754-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=slwDiIB0; arc=pass (i=1 spf=pass spfdomain=flex--seanjc.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-50754-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-50754-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 173521C228FA for ; Sat, 3 Feb 2024 00:13:15 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id AB9B41B5B3; Sat, 3 Feb 2024 00:09:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="slwDiIB0" 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 53D2011CB7 for ; Sat, 3 Feb 2024 00:09:37 +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=1706918979; cv=none; b=T85a4V96gqTvf8t6kARI0MPJQGIPG1UdtzBxyMsMKpkJmUtzXTjt8x0n1I4daE0lQVV9FdkO+h51wPASmiDWGEC/+QpQ233jVC4ZFEJseAYAI60O6slYqM4n5yt20E9yXlLuswjZZa8MyFYe7tN/NU6Dds9S/5PyZZscsUbQOC4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706918979; c=relaxed/simple; bh=51qYoiJXla3fcRiNNoPbF5xjAjxwljCMn92WKnY0s1Q=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=ehyBurljSDLAZyeRiKBLqkBTJyxseEg1Xvw5QBwGzl2DKp2e4BYZtF/QXpIlzh2NFaP06a8PfXegq6hJXtxMxNM03Wr6/gXyITXRqpzDZrHvxneIbGpaRGA+v86/LgeecmrD/PmSUFd4G4oFiO+YymPqritAKUTrr+b/Pf0bZbI= 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=slwDiIB0; 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 Received: by mail-pg1-f201.google.com with SMTP id 41be03b00d2f7-5d8df7c5500so1880452a12.2 for ; Fri, 02 Feb 2024 16:09:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1706918976; x=1707523776; 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=lcHhkIfCfD5wroHUDfoUCPFb/M+i7HRMkWQ8hVkrcXw=; b=slwDiIB0Mgz+fEC9LQCoaJskIKT7i3OP+8NOIvqaOzPDiifsD8CScd/LXyMdfe6Cup 53LEQ7ZnzE6zfTNfThNR2UG5z4itFGPapnkv/qIV6PomfH+Z7QQ7EKkAcIWWt9ovduoH b5QArHVrKszzmIRrhh0IJLH0rDJOrz6YO8/Lg2jmLi4QVZTEgP+VRgPwCI8UEwufJNy/ rcdMPXtIpoQVBN3do/cqMmxnln1zYTiToBCHTE3VbzsEjrQrkX+OMHJSKKVjIkiDzZNT v6FjIly+ppd/MLFbLY0NpsyIBHei72sBgSq6EH3z2+ByBhe1en6kie21yh06BvIm8gnf a1og== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706918976; x=1707523776; 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=lcHhkIfCfD5wroHUDfoUCPFb/M+i7HRMkWQ8hVkrcXw=; b=nZyNA5cMs4S2b8qvnxxqNQwovR/ILAxRc1O4eivBkBkmofbE+wdq2x+70i9FXl00BO InIUpJ+ynsRhlzcWYd3pfQHudubBq+C/JwulbSG/egjPhNJITwLdb0qIHznKA0nINM8L gBg+txyiW4d2ZQvDSVvP28IRBol8BYMCfXAUzA5cgD62/nPfQqT8BMGUIdZr8lQoEvmU K6IK0OEom4mEXKABk1v4m10NFHfBIurssXrZzlxtHMGMkF7b2VigPJ4oIoL24YQ4SsBU BZ8GGW7AG3dj5gSuZUzQUf/sOFVWk4xbLWMGtaDVBhcsHVInJhvLBq5DmlBKb/0I7wyZ CUkQ== X-Gm-Message-State: AOJu0YwBBVsawoTqp/ji4ulN5ZcLJ4Ne3d6DdHGnIh9ITnzj2h/Nu3pq JPrEvFnGem433o2BgkblRjohGYC7LTs5LC6v01j327Su4othEUgEYu3ZkHRfEq/aAw1L7SbVgGq gQQ== X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a65:628e:0:b0:5d8:be12:fa64 with SMTP id f14-20020a65628e000000b005d8be12fa64mr45328pgv.0.1706918976544; Fri, 02 Feb 2024 16:09:36 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 2 Feb 2024 16:09:14 -0800 In-Reply-To: <20240203000917.376631-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: <20240203000917.376631-1-seanjc@google.com> X-Mailer: git-send-email 2.43.0.594.gd9cf4e227d-goog Message-ID: <20240203000917.376631-9-seanjc@google.com> Subject: [PATCH v8 08/10] KVM: selftests: Add library for creating and interacting with SEV guests From: Sean Christopherson To: Paolo Bonzini , Marc Zyngier , Oliver Upton , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Sean Christopherson Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, Vishal Annapurve , Ackerley Tng , Andrew Jones , Tom Lendacky , Michael Roth , Peter Gonda X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1789834502070605272 X-GMAIL-MSGID: 1789834502070605272 From: Peter Gonda Add a library/APIs for creating and interfacing with SEV guests, all of which need some amount of common functionality, e.g. an open file handle for the SEV driver (/dev/sev), ioctl() wrappers to pass said file handle to KVM, tracking of the C-bit, etc. Add an x86-specific hook to initialize address properties, a.k.a. the location of the C-bit. An arch specific hook is rather gross, but x86 already has a dedicated #ifdef-protected kvm_get_cpu_address_width() hook, i.e. the ugliest code already exists. Cc: Paolo Bonzini Cc: Sean Christopherson Cc: Vishal Annapurve Cc: Ackerly Tng cc: Andrew Jones Cc: Tom Lendacky Cc: Michael Roth Originally-by: Michael Roth Signed-off-by: Peter Gonda Co-developed-by: Sean Christopherson Signed-off-by: Sean Christopherson --- tools/testing/selftests/kvm/Makefile | 1 + .../kvm/include/x86_64/kvm_util_arch.h | 2 + .../selftests/kvm/include/x86_64/processor.h | 8 ++ .../selftests/kvm/include/x86_64/sev.h | 110 +++++++++++++++ tools/testing/selftests/kvm/lib/kvm_util.c | 1 + .../selftests/kvm/lib/x86_64/processor.c | 17 +++ tools/testing/selftests/kvm/lib/x86_64/sev.c | 128 ++++++++++++++++++ 7 files changed, 267 insertions(+) create mode 100644 tools/testing/selftests/kvm/include/x86_64/sev.h create mode 100644 tools/testing/selftests/kvm/lib/x86_64/sev.c diff --git a/tools/testing/selftests/kvm/Makefile b/tools/testing/selftests/kvm/Makefile index ce58098d80fd..169b6ee8f733 100644 --- a/tools/testing/selftests/kvm/Makefile +++ b/tools/testing/selftests/kvm/Makefile @@ -38,6 +38,7 @@ LIBKVM_x86_64 += lib/x86_64/hyperv.c LIBKVM_x86_64 += lib/x86_64/memstress.c LIBKVM_x86_64 += lib/x86_64/pmu.c LIBKVM_x86_64 += lib/x86_64/processor.c +LIBKVM_x86_64 += lib/x86_64/sev.c LIBKVM_x86_64 += lib/x86_64/svm.c LIBKVM_x86_64 += lib/x86_64/ucall.c LIBKVM_x86_64 += lib/x86_64/vmx.c diff --git a/tools/testing/selftests/kvm/include/x86_64/kvm_util_arch.h b/tools/testing/selftests/kvm/include/x86_64/kvm_util_arch.h index 17bb38236d97..205ed788aeb8 100644 --- a/tools/testing/selftests/kvm/include/x86_64/kvm_util_arch.h +++ b/tools/testing/selftests/kvm/include/x86_64/kvm_util_arch.h @@ -8,6 +8,8 @@ struct kvm_vm_arch { uint64_t c_bit; uint64_t s_bit; + int sev_fd; + bool is_pt_protected; }; static inline bool __vm_arch_has_protected_memory(struct kvm_vm_arch *arch) diff --git a/tools/testing/selftests/kvm/include/x86_64/processor.h b/tools/testing/selftests/kvm/include/x86_64/processor.h index 0f4792083d01..3bd03b088dda 100644 --- a/tools/testing/selftests/kvm/include/x86_64/processor.h +++ b/tools/testing/selftests/kvm/include/x86_64/processor.h @@ -23,6 +23,12 @@ extern bool host_cpu_is_intel; extern bool host_cpu_is_amd; +enum vm_guest_x86_subtype { + VM_SUBTYPE_NONE = 0, + VM_SUBTYPE_SEV, + VM_SUBTYPE_SEV_ES, +}; + /* Forced emulation prefix, used to invoke the emulator unconditionally. */ #define KVM_FEP "ud2; .byte 'k', 'v', 'm';" @@ -276,6 +282,7 @@ struct kvm_x86_cpu_property { #define X86_PROPERTY_MAX_EXT_LEAF KVM_X86_CPU_PROPERTY(0x80000000, 0, EAX, 0, 31) #define X86_PROPERTY_MAX_PHY_ADDR KVM_X86_CPU_PROPERTY(0x80000008, 0, EAX, 0, 7) #define X86_PROPERTY_MAX_VIRT_ADDR KVM_X86_CPU_PROPERTY(0x80000008, 0, EAX, 8, 15) +#define X86_PROPERTY_SEV_C_BIT KVM_X86_CPU_PROPERTY(0x8000001F, 0, EBX, 0, 5) #define X86_PROPERTY_PHYS_ADDR_REDUCTION KVM_X86_CPU_PROPERTY(0x8000001F, 0, EBX, 6, 11) #define X86_PROPERTY_MAX_CENTAUR_LEAF KVM_X86_CPU_PROPERTY(0xC0000000, 0, EAX, 0, 31) @@ -1093,6 +1100,7 @@ do { \ } while (0) void kvm_get_cpu_address_width(unsigned int *pa_bits, unsigned int *va_bits); +void kvm_init_vm_address_properties(struct kvm_vm *vm); bool vm_is_unrestricted_guest(struct kvm_vm *vm); struct ex_regs { diff --git a/tools/testing/selftests/kvm/include/x86_64/sev.h b/tools/testing/selftests/kvm/include/x86_64/sev.h new file mode 100644 index 000000000000..262b16f574e9 --- /dev/null +++ b/tools/testing/selftests/kvm/include/x86_64/sev.h @@ -0,0 +1,110 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * Helpers used for SEV guests + * + */ +#ifndef SELFTEST_KVM_SEV_H +#define SELFTEST_KVM_SEV_H + +#include +#include + +#include "linux/psp-sev.h" + +#include "kvm_util.h" +#include "svm_util.h" +#include "processor.h" + +#define SEV_FW_REQ_VER_MAJOR 0 +#define SEV_FW_REQ_VER_MINOR 17 + +enum sev_guest_state { + SEV_GUEST_STATE_UNINITIALIZED = 0, + SEV_GUEST_STATE_LAUNCH_UPDATE, + SEV_GUEST_STATE_LAUNCH_SECRET, + SEV_GUEST_STATE_RUNNING, +}; + +#define SEV_POLICY_NO_DBG (1UL << 0) +#define SEV_POLICY_ES (1UL << 2) + +bool is_kvm_sev_supported(void); + +void sev_vm_launch(struct kvm_vm *vm, uint32_t policy); +void sev_vm_launch_measure(struct kvm_vm *vm, uint8_t *measurement); +void sev_vm_launch_finish(struct kvm_vm *vm); + +struct kvm_vm *vm_sev_create_with_one_vcpu(uint32_t policy, void *guest_code, + struct kvm_vcpu **cpu); + +kvm_static_assert(SEV_RET_SUCCESS == 0); + +/* + * The KVM_MEMORY_ENCRYPT_OP uAPI is utter garbage and takes an "unsigned long" + * instead of a proper struct. The size of the parameter is embedded in the + * ioctl number, i.e. is ABI and thus immutable. Hack around the mess by + * creating an overlay to pass in an "unsigned long" without a cast (casting + * will make the compiler unhappy due to dereferencing an aliased pointer). + */ +#define __vm_sev_ioctl(vm, cmd, arg) \ +({ \ + int r; \ + \ + union { \ + struct kvm_sev_cmd c; \ + unsigned long raw; \ + } sev_cmd = { .c = { \ + .id = (cmd), \ + .data = (uint64_t)(arg), \ + .sev_fd = (vm)->arch.sev_fd, \ + } }; \ + \ + r = __vm_ioctl(vm, KVM_MEMORY_ENCRYPT_OP, &sev_cmd.raw); \ + r ?: sev_cmd.c.error; \ +}) + +#define vm_sev_ioctl(vm, cmd, arg) \ +({ \ + int ret = __vm_sev_ioctl(vm, cmd, arg); \ + \ + __TEST_ASSERT_VM_VCPU_IOCTL(!ret, #cmd, ret, vm); \ +}) + +static inline void sev_vm_init(struct kvm_vm *vm) +{ + vm->arch.sev_fd = open_sev_dev_path_or_exit(); + + vm_sev_ioctl(vm, KVM_SEV_INIT, NULL); +} + + +static inline void sev_es_vm_init(struct kvm_vm *vm) +{ + vm->arch.sev_fd = open_sev_dev_path_or_exit(); + + vm_sev_ioctl(vm, KVM_SEV_ES_INIT, NULL); +} + +static inline void sev_register_encrypted_memory(struct kvm_vm *vm, + struct userspace_mem_region *region) +{ + struct kvm_enc_region range = { + .addr = region->region.userspace_addr, + .size = region->region.memory_size, + }; + + vm_ioctl(vm, KVM_MEMORY_ENCRYPT_REG_REGION, &range); +} + +static inline void sev_launch_update_data(struct kvm_vm *vm, vm_paddr_t gpa, + uint64_t size) +{ + struct kvm_sev_launch_update_data update_data = { + .uaddr = (unsigned long)addr_gpa2hva(vm, gpa), + .len = size, + }; + + vm_sev_ioctl(vm, KVM_SEV_LAUNCH_UPDATE_DATA, &update_data); +} + +#endif /* SELFTEST_KVM_SEV_H */ diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/selftests/kvm/lib/kvm_util.c index 19511137d1ae..b2262b5fad9e 100644 --- a/tools/testing/selftests/kvm/lib/kvm_util.c +++ b/tools/testing/selftests/kvm/lib/kvm_util.c @@ -317,6 +317,7 @@ struct kvm_vm *____vm_create(struct vm_shape shape) case VM_MODE_PXXV48_4K: #ifdef __x86_64__ kvm_get_cpu_address_width(&vm->pa_bits, &vm->va_bits); + kvm_init_vm_address_properties(vm); /* * Ignore KVM support for 5-level paging (vm->va_bits == 57), * it doesn't take effect unless a CR4.LA57 is set, which it diff --git a/tools/testing/selftests/kvm/lib/x86_64/processor.c b/tools/testing/selftests/kvm/lib/x86_64/processor.c index 6c1d2c0ec584..aa92220bf5da 100644 --- a/tools/testing/selftests/kvm/lib/x86_64/processor.c +++ b/tools/testing/selftests/kvm/lib/x86_64/processor.c @@ -9,6 +9,7 @@ #include "test_util.h" #include "kvm_util.h" #include "processor.h" +#include "sev.h" #ifndef NUM_INTERRUPTS #define NUM_INTERRUPTS 256 @@ -278,6 +279,9 @@ uint64_t *__vm_get_page_table_entry(struct kvm_vm *vm, uint64_t vaddr, { uint64_t *pml4e, *pdpe, *pde; + TEST_ASSERT(!vm->arch.is_pt_protected, + "Walking page tables of protected guests is impossible"); + TEST_ASSERT(*level >= PG_LEVEL_NONE && *level < PG_LEVEL_NUM, "Invalid PG_LEVEL_* '%d'", *level); @@ -573,6 +577,11 @@ void kvm_arch_vm_post_create(struct kvm_vm *vm) vm_create_irqchip(vm); sync_global_to_guest(vm, host_cpu_is_intel); sync_global_to_guest(vm, host_cpu_is_amd); + + if (vm->subtype == VM_SUBTYPE_SEV) + sev_vm_init(vm); + else if (vm->subtype == VM_SUBTYPE_SEV_ES) + sev_es_vm_init(vm); } struct kvm_vcpu *vm_arch_vcpu_add(struct kvm_vm *vm, uint32_t vcpu_id, @@ -1063,6 +1072,14 @@ void kvm_get_cpu_address_width(unsigned int *pa_bits, unsigned int *va_bits) } } +void kvm_init_vm_address_properties(struct kvm_vm *vm) +{ + if (vm->subtype == VM_SUBTYPE_SEV) { + vm->arch.c_bit = BIT_ULL(this_cpu_property(X86_PROPERTY_SEV_C_BIT)); + vm->gpa_tag_mask = vm->arch.c_bit; + } +} + static void set_idt_entry(struct kvm_vm *vm, int vector, unsigned long addr, int dpl, unsigned short selector) { diff --git a/tools/testing/selftests/kvm/lib/x86_64/sev.c b/tools/testing/selftests/kvm/lib/x86_64/sev.c new file mode 100644 index 000000000000..f37b1a7247ad --- /dev/null +++ b/tools/testing/selftests/kvm/lib/x86_64/sev.c @@ -0,0 +1,128 @@ +// SPDX-License-Identifier: GPL-2.0-only +#define _GNU_SOURCE /* for program_invocation_short_name */ +#include +#include + +#include "sev.h" + +bool is_kvm_sev_supported(void) +{ + int sev_fd = open_sev_dev_path_or_exit(); + struct sev_user_data_status sev_status; + + struct sev_issue_cmd arg = { + .cmd = SEV_PLATFORM_STATUS, + .data = (unsigned long)&sev_status, + }; + + kvm_ioctl(sev_fd, SEV_ISSUE_CMD, &arg); + close(sev_fd); + + return sev_status.api_major > SEV_FW_REQ_VER_MAJOR || + (sev_status.api_major == SEV_FW_REQ_VER_MAJOR && + sev_status.api_minor >= SEV_FW_REQ_VER_MINOR); +} + +/* + * sparsebit_next_clear() can return 0 if [x, 2**64-1] are all set, and the + * -1 would then cause an underflow back to 2**64 - 1. This is expected and + * correct. + * + * If the last range in the sparsebit is [x, y] and we try to iterate, + * sparsebit_next_set() will return 0, and sparsebit_next_clear() will try + * and find the first range, but that's correct because the condition + * expression would cause us to quit the loop. + */ +static void encrypt_region(struct kvm_vm *vm, struct userspace_mem_region *region) +{ + const struct sparsebit *protected_phy_pages = region->protected_phy_pages; + const vm_paddr_t gpa_base = region->region.guest_phys_addr; + const sparsebit_idx_t lowest_page_in_region = gpa_base >> vm->page_shift; + sparsebit_idx_t i, j; + + if (!sparsebit_any_set(protected_phy_pages)) + return; + + sev_register_encrypted_memory(vm, region); + + sparsebit_for_each_set_range(protected_phy_pages, i, j) { + const uint64_t size = (j - i + 1) * vm->page_size; + const uint64_t offset = (i - lowest_page_in_region) * vm->page_size; + + sev_launch_update_data(vm, gpa_base + offset, size); + } +} + +void sev_vm_launch(struct kvm_vm *vm, uint32_t policy) +{ + struct kvm_sev_launch_start launch_start = { + .policy = policy, + }; + struct userspace_mem_region *region; + struct kvm_sev_guest_status status; + int ctr; + + vm_sev_ioctl(vm, KVM_SEV_LAUNCH_START, &launch_start); + vm_sev_ioctl(vm, KVM_SEV_GUEST_STATUS, &status); + + TEST_ASSERT_EQ(status.policy, policy); + TEST_ASSERT_EQ(status.state, SEV_GUEST_STATE_LAUNCH_UPDATE); + + hash_for_each(vm->regions.slot_hash, ctr, region, slot_node) + encrypt_region(vm, region); + + vm->arch.is_pt_protected = true; +} + +void sev_vm_launch_measure(struct kvm_vm *vm, uint8_t *measurement) +{ + struct kvm_sev_launch_measure launch_measure; + struct kvm_sev_guest_status guest_status; + + launch_measure.len = 256; + launch_measure.uaddr = (__u64)measurement; + vm_sev_ioctl(vm, KVM_SEV_LAUNCH_MEASURE, &launch_measure); + + vm_sev_ioctl(vm, KVM_SEV_GUEST_STATUS, &guest_status); + TEST_ASSERT_EQ(guest_status.state, SEV_GUEST_STATE_LAUNCH_SECRET); +} + +void sev_vm_launch_finish(struct kvm_vm *vm) +{ + struct kvm_sev_guest_status status; + + vm_sev_ioctl(vm, KVM_SEV_GUEST_STATUS, &status); + TEST_ASSERT(status.state == SEV_GUEST_STATE_LAUNCH_UPDATE || + status.state == SEV_GUEST_STATE_LAUNCH_SECRET, + "Unexpected guest state: %d", status.state); + + vm_sev_ioctl(vm, KVM_SEV_LAUNCH_FINISH, NULL); + + vm_sev_ioctl(vm, KVM_SEV_GUEST_STATUS, &status); + TEST_ASSERT_EQ(status.state, SEV_GUEST_STATE_RUNNING); +} + +struct kvm_vm *vm_sev_create_with_one_vcpu(uint32_t policy, void *guest_code, + struct kvm_vcpu **cpu) +{ + struct vm_shape shape = { + .type = VM_TYPE_DEFAULT, + .mode = VM_MODE_DEFAULT, + .subtype = VM_SUBTYPE_SEV, + }; + struct kvm_vm *vm; + struct kvm_vcpu *cpus[1]; + uint8_t measurement[512]; + + vm = __vm_create_with_vcpus(shape, 1, 0, guest_code, cpus); + *cpu = cpus[0]; + + sev_vm_launch(vm, policy); + + /* TODO: Validate the measurement is as expected. */ + sev_vm_launch_measure(vm, measurement); + + sev_vm_launch_finish(vm); + + return vm; +} From patchwork Sat Feb 3 00:09:15 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 196109 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:9bc1:b0:106:209c:c626 with SMTP id op1csp770475dyc; Fri, 2 Feb 2024 16:13:39 -0800 (PST) X-Google-Smtp-Source: AGHT+IG+z32c/qQMSJDCgLznhteehAAmwEnjvSvXr1hIcrY8ktZcKngPxQQFAbeYQbeFK/NoJ6HM X-Received: by 2002:a0c:f284:0:b0:68c:7faa:b256 with SMTP id k4-20020a0cf284000000b0068c7faab256mr6099272qvl.54.1706919219043; Fri, 02 Feb 2024 16:13:39 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706919219; cv=pass; d=google.com; s=arc-20160816; b=nxZVDeBWij0A3FCe9dJJ++vY9CSZTrtGss6tXPiLqdBrUQG8NRy54/3/BfUdLtY3mv T/sF7XdbWLAaZgKZwVk434S2gzFKZQoR/JqcBD5HvIXElhzWGh3pUC4PZ4GPd5jLr89a aFCGuZxaD5K2Dg/ogStykOKDvKSJRIS+4wyzWGnh7iBIeZo3sg1R1yY+aRynoiAo3cl2 79bHpvPNltMI6bwiHuuf46qMEzj/ePmdBKxXRzXwzmgt5f+uhPheDM5vyBAJ+pvm94dn h3eUrr7puI//l79DsK3qkqq+n0ThJa0nrFVd1IpoASl9+fiYuYfv0GqCSXUHNGuiKccI sZGg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:message-id:references:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:in-reply-to:date :reply-to:dkim-signature; bh=XX/qPWiFzcrNc98rmsyPZwe0j7yaMHMPDZjofeW64Pk=; fh=szOu55n6wHF1++LTW8RkahAvd2OUNFeH3qUAkarig5k=; b=kuOX+v+u8ArgAJRUnZ0hfBXgAfLbFA+6d4IvtAiasH+iyrnx+Vik9JNG0vgjUe7JgB 8watAXfvov2VZchiZGlTThjr6y1WG9IUMlpgJLTte5hRhSDYBLQUrpfsZ/Yyg9wg5i4E TXVY8MeCQMnmftk4ARvqNwoJ7erdJCTBdb6Fls5x1sMY6ERxDk2uWTaiHe3Z8exESJnc 1usTLwUttYUM6fnwrfJeye82BE+inBLzUFXa3HCN2+IIhWW3ReATzaQ39NWohZDfRl6d ZeRwykrZJ7aurOhlH+P+xS186BbcQcVYmXzI01JUftRjVQdoQH3FroAHpuMcuQ8cvEpu SLlw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=eF5KUrtV; arc=pass (i=1 spf=pass spfdomain=flex--seanjc.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-50756-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-50756-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com X-Forwarded-Encrypted: i=1; AJvYcCUoY0Ly++ZA7gGaEo65Dwk3IaiFbFCQqn77DxFIT53f96O6CQ+BJjxhIOe281vzko+0a6gggtmC2L5irtW8/AeDgDSaZw== Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id f7-20020a0ccc87000000b0068c7004907dsi3176555qvl.502.2024.02.02.16.13.38 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 Feb 2024 16:13:39 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-50756-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=eF5KUrtV; arc=pass (i=1 spf=pass spfdomain=flex--seanjc.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-50756-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-50756-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id C27381C22825 for ; Sat, 3 Feb 2024 00:13:38 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 496D61BC22; Sat, 3 Feb 2024 00:09:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="eF5KUrtV" Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.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 8662012E58 for ; Sat, 3 Feb 2024 00:09:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706918984; cv=none; b=rrelGszTY0/KSUjlLNyzPjTrCgIlqzX2PxtOOl+EjhH8kaPYkByPaPXZwbYtORKJm6nTMPLNtUPxEDJ764UHyGuaiyMY6lFUJetWJ045PVM412/bkoey6HF0kiaD7yPVrMuIDELHFPUVvo9ajEa/sAFqr06FIaaEL+N0ELlitNs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706918984; c=relaxed/simple; bh=WErIq3K7OOqOd3xHpYtlDNEPs0UpZWxiYWZgTo/MP4I=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=IwCg1NwaxPV69xySt+sWGoWZS+XOeHDQeGCH1gXpgsWMm1dzjD9UyvIhcnvMF2jlAFLbjJSEEuX8ihv4JX3f3tq8PEIfpuvb2WYuIb+IggR+OHB4fz5QD7JgFAo/64KZJczBvEywezAwD/iHNH9RTQuzRFiCKQV7SuNBPvRY/dM= 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=eF5KUrtV; arc=none smtp.client-ip=209.85.219.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 Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-dc6c2643a07so4778994276.3 for ; Fri, 02 Feb 2024 16:09:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1706918978; x=1707523778; 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=XX/qPWiFzcrNc98rmsyPZwe0j7yaMHMPDZjofeW64Pk=; b=eF5KUrtVovEJ4MDY6oGzaFPy1rOPDlgm8O18gNe4qm2R10VKwzOXH/clL8xMTRKe5t MX5BWFW4o57RNP9UcLtjUpz8pr8XFiA/E9xjW1WoKd8KDE6LumUWVADgMckxLDk+j/Qb Rv+L/R8v05AMTl+1NuGlDw5BYyOKEY3PW+S2VEHYFPkKsMt2V8YgY1iMgqLbH0CiEK3E rgmRXMQoq2hI1X1OzUGRWDMuU6Kf+kG30Z+OUZ35FrVZ1Ko7+6hXWfd/fDhnVoZ45Jg6 FTQXsOQFpoLeNscsRsX4jkaaDRvlJZB4yMiseOvl/E4lcKBbSNP0hFFbh25WVwufR4E5 d8Jw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706918978; x=1707523778; 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=XX/qPWiFzcrNc98rmsyPZwe0j7yaMHMPDZjofeW64Pk=; b=nD5g9nBVK7NMO1AqqfIzBmFXU644aJ7/0RABtdqkSwc0/gR2n+L6wU7Oo343bFLTDM 6JHLZ9jNiAalrZls1zKkbcKmCGThG++c0NBDOxNCUtBnfXiVdGo3khJ7RuIOtJ7yDcxB Z8/hC6AgOWyjZEHQgTr6K6/xpMutqH9fyKRnhjWluIqtTZb8c9UAIlXeApH+AvpbYM9S eGG99b6rXNVmLl1xlOm82PFNQ48ZjTVquIHaBqkEHHOOUlAqDyDTM40ioXbqls8xA1E3 zXhMRMNT9Ul4WtRM73SMxGXnO+JASc2gWyi0eaMNUyTSwDhOK8xBXX+NpLZKOHistuXC 4H0A== X-Gm-Message-State: AOJu0Yy6aKU5lWYsX+abtiaaCGRTimltFFSQClCzO2m9xCwsDR7G3L/I CZgC2ECnY9VGifMmlOlU299/PVOwo8xzO4R14+nNwohKwSF2ZAibFQPEy0hauVJJ1ngeAZHym1/ Oaw== X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6902:1085:b0:dc6:e5e9:f3af with SMTP id v5-20020a056902108500b00dc6e5e9f3afmr1242765ybu.9.1706918978596; Fri, 02 Feb 2024 16:09:38 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 2 Feb 2024 16:09:15 -0800 In-Reply-To: <20240203000917.376631-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: <20240203000917.376631-1-seanjc@google.com> X-Mailer: git-send-email 2.43.0.594.gd9cf4e227d-goog Message-ID: <20240203000917.376631-10-seanjc@google.com> Subject: [PATCH v8 09/10] KVM: selftests: Use the SEV library APIs in the intra-host migration test From: Sean Christopherson To: Paolo Bonzini , Marc Zyngier , Oliver Upton , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Sean Christopherson Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, Vishal Annapurve , Ackerley Tng , Andrew Jones , Tom Lendacky , Michael Roth , Peter Gonda X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1789834527163508286 X-GMAIL-MSGID: 1789834527163508286 Port the existing intra-host SEV(-ES) migration test to the recently added SEV library, which handles much of the boilerplate needed to create and configure SEV guests. Signed-off-by: Sean Christopherson --- .../selftests/kvm/x86_64/sev_migrate_tests.c | 67 ++++++------------- 1 file changed, 21 insertions(+), 46 deletions(-) diff --git a/tools/testing/selftests/kvm/x86_64/sev_migrate_tests.c b/tools/testing/selftests/kvm/x86_64/sev_migrate_tests.c index a49828adf294..d6f7428e42c6 100644 --- a/tools/testing/selftests/kvm/x86_64/sev_migrate_tests.c +++ b/tools/testing/selftests/kvm/x86_64/sev_migrate_tests.c @@ -10,11 +10,9 @@ #include "test_util.h" #include "kvm_util.h" #include "processor.h" -#include "svm_util.h" +#include "sev.h" #include "kselftest.h" -#define SEV_POLICY_ES 0b100 - #define NR_MIGRATE_TEST_VCPUS 4 #define NR_MIGRATE_TEST_VMS 3 #define NR_LOCK_TESTING_THREADS 3 @@ -22,46 +20,24 @@ bool have_sev_es; -static int __sev_ioctl(int vm_fd, int cmd_id, void *data, __u32 *fw_error) -{ - struct kvm_sev_cmd cmd = { - .id = cmd_id, - .data = (uint64_t)data, - .sev_fd = open_sev_dev_path_or_exit(), - }; - int ret; - - ret = ioctl(vm_fd, KVM_MEMORY_ENCRYPT_OP, &cmd); - *fw_error = cmd.error; - return ret; -} - -static void sev_ioctl(int vm_fd, int cmd_id, void *data) -{ - int ret; - __u32 fw_error; - - ret = __sev_ioctl(vm_fd, cmd_id, data, &fw_error); - TEST_ASSERT(ret == 0 && fw_error == SEV_RET_SUCCESS, - "%d failed: return code: %d, errno: %d, fw error: %d", - cmd_id, ret, errno, fw_error); -} - static struct kvm_vm *sev_vm_create(bool es) { struct kvm_vm *vm; - struct kvm_sev_launch_start start = { 0 }; int i; vm = vm_create_barebones(); - sev_ioctl(vm->fd, es ? KVM_SEV_ES_INIT : KVM_SEV_INIT, NULL); + if (!es) + sev_vm_init(vm); + else + sev_es_vm_init(vm); + for (i = 0; i < NR_MIGRATE_TEST_VCPUS; ++i) __vm_vcpu_add(vm, i); + + sev_vm_launch(vm, es ? SEV_POLICY_ES : 0); + if (es) - start.policy |= SEV_POLICY_ES; - sev_ioctl(vm->fd, KVM_SEV_LAUNCH_START, &start); - if (es) - sev_ioctl(vm->fd, KVM_SEV_LAUNCH_UPDATE_VMSA, NULL); + vm_sev_ioctl(vm, KVM_SEV_LAUNCH_UPDATE_VMSA, NULL); return vm; } @@ -181,7 +157,7 @@ static void test_sev_migrate_parameters(void) sev_vm = sev_vm_create(/* es= */ false); sev_es_vm = sev_vm_create(/* es= */ true); sev_es_vm_no_vmsa = vm_create_barebones(); - sev_ioctl(sev_es_vm_no_vmsa->fd, KVM_SEV_ES_INIT, NULL); + sev_es_vm_init(sev_es_vm_no_vmsa); __vm_vcpu_add(sev_es_vm_no_vmsa, 1); ret = __sev_migrate_from(sev_vm, sev_es_vm); @@ -230,13 +206,13 @@ static void sev_mirror_create(struct kvm_vm *dst, struct kvm_vm *src) TEST_ASSERT(!ret, "Copying context failed, ret: %d, errno: %d", ret, errno); } -static void verify_mirror_allowed_cmds(int vm_fd) +static void verify_mirror_allowed_cmds(struct kvm_vm *vm) { struct kvm_sev_guest_status status; + int cmd_id; - for (int cmd_id = KVM_SEV_INIT; cmd_id < KVM_SEV_NR_MAX; ++cmd_id) { + for (cmd_id = KVM_SEV_INIT; cmd_id < KVM_SEV_NR_MAX; ++cmd_id) { int ret; - __u32 fw_error; /* * These commands are allowed for mirror VMs, all others are @@ -256,14 +232,13 @@ static void verify_mirror_allowed_cmds(int vm_fd) * These commands should be disallowed before the data * parameter is examined so NULL is OK here. */ - ret = __sev_ioctl(vm_fd, cmd_id, NULL, &fw_error); - TEST_ASSERT( - ret == -1 && errno == EINVAL, - "Should not be able call command: %d. ret: %d, errno: %d", - cmd_id, ret, errno); + ret = __vm_sev_ioctl(vm, cmd_id, NULL); + TEST_ASSERT(ret == -1 && errno == EINVAL, + "Should not be able call command: %d. ret: %d, errno: %d", + cmd_id, ret, errno); } - sev_ioctl(vm_fd, KVM_SEV_GUEST_STATUS, &status); + vm_sev_ioctl(vm, KVM_SEV_GUEST_STATUS, &status); } static void test_sev_mirror(bool es) @@ -281,9 +256,9 @@ static void test_sev_mirror(bool es) __vm_vcpu_add(dst_vm, i); if (es) - sev_ioctl(dst_vm->fd, KVM_SEV_LAUNCH_UPDATE_VMSA, NULL); + vm_sev_ioctl(dst_vm, KVM_SEV_LAUNCH_UPDATE_VMSA, NULL); - verify_mirror_allowed_cmds(dst_vm->fd); + verify_mirror_allowed_cmds(dst_vm); kvm_vm_free(src_vm); kvm_vm_free(dst_vm); From patchwork Sat Feb 3 00:09:16 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 196108 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:9bc1:b0:106:209c:c626 with SMTP id op1csp770435dyc; Fri, 2 Feb 2024 16:13:29 -0800 (PST) X-Google-Smtp-Source: AGHT+IFMwvX58e2sqy5462V8hlSmuEQVEs3HPuuWg9zGTw+NZbCCldvnCO3RXlzFn7/2Y3xHz6oa X-Received: by 2002:a05:6402:5248:b0:560:1372:d950 with SMTP id t8-20020a056402524800b005601372d950mr561293edd.22.1706919209345; Fri, 02 Feb 2024 16:13:29 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706919209; cv=pass; d=google.com; s=arc-20160816; b=GyopnejWMM+Mo09Tl6AT8eV8DezM1wh0QwZnutMGooXMDZgaH14XujPghvTCMombHB zvZazzGTRxB0hikIVOu4gUrnY7wt+QCkjTn36kFMSY7CeeuF8FJbt27rLGJIRcFrPDqM 9kH6TicGksMTyey/mJbCkDjru2jcPLhnPI+BQmOfmKh3EiqxHpZ/BaWNpHbXGIn2qx+c PcYlYyNjAZpeaMZLjesOyKI6Ajzx+7rlHG5LhDf3HT0E/bN2UmAqNJlvQ9EXzHXTC79R INR3Vf7oaPvD1MeR0oIx/dPrGcw0qAy1o7e0MMXXpLw8f2eSQrcAF5jBfpe/wc+GoZfA n6nA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:message-id:references:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:in-reply-to:date :reply-to:dkim-signature; bh=VzhuhwHBOVTPe3ek7qdXtFf4wVMa0yBW9i03fvIkD6g=; fh=Dd1IfGRW1muX1NS2R3mh8Dtohf9olMCyDSM5MxIBL80=; b=MfX4yb40KsDefV+FAPMctQhupCVmcFabwS7uz3y7+8Og+erzQgKxeGRXjgUPm9LjfC UJvc4ftnJ9/5L5V8VrGvuLR3g36Vb6skz7u2dd3IttwJ1vxOzCsHQKJNoG9ekqNzlhzJ QEeCZlisNEd9nVNDTz7z2IdENWfK5Y/F/TDtdmqkuVXtMwsaZoZB91ANLjBnCtUBqMvz RWF6NxfsbbTH3Bqxh/0ZM9rxChaIkhXyRBa5jNLKuhvQ/cEpLGFENdM9B+SWGSuELOb8 RFFHR0gmi/dua1BF4tC0yOFBCZNK0VF+xjVTMe1M9adY81UxJ2iPKoAChGOOgSyng4SM Arbg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=bnt6gnw2; arc=pass (i=1 spf=pass spfdomain=flex--seanjc.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-50755-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-50755-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com X-Forwarded-Encrypted: i=1; AJvYcCUBXpknROpD5pYzidKFNlqyhFC4s7kK0dpeSIHVlu0jtGWnCO1oMP9SbltztZaNjouilKoKdMIvr7gyly2O2EUPPiZENg== Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id b18-20020a50b412000000b0055a61376aeesi1259044edh.240.2024.02.02.16.13.29 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 Feb 2024 16:13:29 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-50755-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=bnt6gnw2; arc=pass (i=1 spf=pass spfdomain=flex--seanjc.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-50755-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-50755-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id B38191F25FBA for ; Sat, 3 Feb 2024 00:13:28 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 262C11B96B; Sat, 3 Feb 2024 00:09:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="bnt6gnw2" 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 7156013FF1 for ; Sat, 3 Feb 2024 00:09:41 +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=1706918983; cv=none; b=Hrux55hNm+rWgllrA0pipFv2yz2hVOkokknLQcX7016530P1P8oKOHWFaewZR9tsxhRyfXYyVnmb6K+QYVgBy+pqLnsdTjR/ok7TdEwPREAb84VfeCQqRzUs9DDKLbgYrlB5TYFeALCCnQaZd5Kmq+pxvIVo56ZnWws2nMdnMHw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706918983; c=relaxed/simple; bh=dxFDbsAQMhSueKOp2cttxeSOtYuXNHRkrePZ7bjyZ1U=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=mxaN/tHkl3hRQO3jN2BT0uUwq8YE1c1gHWH6yRVIjxY1lQTftqP5lnpTLSR/Z22sJxALyo1/xBxPBFpJzYZoZ+7pAwBwpGBXyx4f0oTps0WZwf/Nk+ZQrg58gEyYgcohHQ8HSo8c8QhQpF/l3vtaw+tw/gQYnY9zxlSmV0yCBbI= 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=bnt6gnw2; 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 Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-1d968aebbd1so17556115ad.1 for ; Fri, 02 Feb 2024 16:09:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1706918981; x=1707523781; 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=VzhuhwHBOVTPe3ek7qdXtFf4wVMa0yBW9i03fvIkD6g=; b=bnt6gnw2arROnor157+UAWxYa02ZsadT4zbEncIYG1BpJmmwPx3eKR0fMf6BoTeaa+ im0SIt34/wbwKyGJQFpoZ58msCstKa5ohg9dE2iFJYZjwWMIeJv9eVoyzIR5EFo1kPnq o1agCJhmWI1yDdbOnYZ7riV5WVHY3zelYYKBaBsPoaiV5vgfyv9qz0HRw19ETK+/Bcj7 F07UAet363tPsJK0hPUeTy43teKgkl13TL5viArqNF91cD/oav62QkDvzr5VsJ7+Sq8F KcyHc7o59UmfWLJNQK3HOoa0tNgMhqifnBx6Z31ThKC2tzPm8d4XspGqgJt+OaCpsK4W 6mUQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706918981; x=1707523781; 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=VzhuhwHBOVTPe3ek7qdXtFf4wVMa0yBW9i03fvIkD6g=; b=FuXjpUd+tToOagUByXg2eYLWbpHydWCgDHpFAvuIJ8zfwYxGTIwmqiHmQjj9RFa/we 7qoGmtwaaIRQR1z9rNw18LZ4XYw89OEiWCJmT9xh8myFPZV4+lULKsh/3HHagXItjieL VsFKiRTbc0P5MM748lORvK4Fxj0PkjrM3cAe76TMx6olhmVg8jBhrthCvSS9pkql/yne 4PsvZ64EDUrllO5Pv73gXYwMhbx1Xb6HbvUU5XbuhqgbpPop2BweyoulbZaRDYgieZqp Xw8fnR5oyn75dbx0bAbi6mSwB0Fxg9sPlWV5kYHIHlo51zf9xMqaJaTvnf6GCR2Pewqu +I5A== X-Gm-Message-State: AOJu0YyEYkebPSjwwqLQjyaQ2AOWmvrEHRmrQ3UVBhNjiOrjSEFt8d1u /obCzwm70tk6qgNdL5KEeXQsNUhqLviLzIcBtke8rYBToqZn5ZOj7pm1PW5fc4ztGq95iI0zVe5 Pxw== X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:902:e741:b0:1d9:1ca3:1883 with SMTP id p1-20020a170902e74100b001d91ca31883mr51751plf.5.1706918980700; Fri, 02 Feb 2024 16:09:40 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 2 Feb 2024 16:09:16 -0800 In-Reply-To: <20240203000917.376631-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: <20240203000917.376631-1-seanjc@google.com> X-Mailer: git-send-email 2.43.0.594.gd9cf4e227d-goog Message-ID: <20240203000917.376631-11-seanjc@google.com> Subject: [PATCH v8 10/10] KVM: selftests: Add a basic SEV smoke test From: Sean Christopherson To: Paolo Bonzini , Marc Zyngier , Oliver Upton , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Sean Christopherson Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, Vishal Annapurve , Ackerley Tng , Andrew Jones , Tom Lendacky , Michael Roth , Peter Gonda X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1789834516912618550 X-GMAIL-MSGID: 1789834516912618550 From: Peter Gonda Add a basic smoke test for SEV guests to verify that KVM can launch an SEV guest and run a few instructions without exploding. To verify that SEV is indeed enabled, assert that SEV is reported as enabled in MSR_AMD64_SEV, a.k.a. SEV_STATUS, which cannot be intercepted by KVM (architecturally enforced). Cc: Paolo Bonzini Cc: Sean Christopherson Cc: Vishal Annapurve Cc: Ackerly Tng cc: Andrew Jones Cc: Tom Lendacky Cc: Michael Roth Suggested-by: Michael Roth Signed-off-by: Peter Gonda [sean: rename to "sev_smoke_test"] Signed-off-by: Sean Christopherson --- tools/testing/selftests/kvm/Makefile | 1 + .../selftests/kvm/x86_64/sev_smoke_test.c | 58 +++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 tools/testing/selftests/kvm/x86_64/sev_smoke_test.c diff --git a/tools/testing/selftests/kvm/Makefile b/tools/testing/selftests/kvm/Makefile index 169b6ee8f733..da20e6bb43ed 100644 --- a/tools/testing/selftests/kvm/Makefile +++ b/tools/testing/selftests/kvm/Makefile @@ -120,6 +120,7 @@ TEST_GEN_PROGS_x86_64 += x86_64/vmx_pmu_caps_test TEST_GEN_PROGS_x86_64 += x86_64/xen_shinfo_test TEST_GEN_PROGS_x86_64 += x86_64/xen_vmcall_test TEST_GEN_PROGS_x86_64 += x86_64/sev_migrate_tests +TEST_GEN_PROGS_x86_64 += x86_64/sev_smoke_test TEST_GEN_PROGS_x86_64 += x86_64/amx_test TEST_GEN_PROGS_x86_64 += x86_64/max_vcpuid_cap_test TEST_GEN_PROGS_x86_64 += x86_64/triple_fault_event_test diff --git a/tools/testing/selftests/kvm/x86_64/sev_smoke_test.c b/tools/testing/selftests/kvm/x86_64/sev_smoke_test.c new file mode 100644 index 000000000000..c1534efab2be --- /dev/null +++ b/tools/testing/selftests/kvm/x86_64/sev_smoke_test.c @@ -0,0 +1,58 @@ +// SPDX-License-Identifier: GPL-2.0-only +#include +#include +#include +#include +#include + +#include "test_util.h" +#include "kvm_util.h" +#include "processor.h" +#include "svm_util.h" +#include "linux/psp-sev.h" +#include "sev.h" + +static void guest_sev_code(void) +{ + GUEST_ASSERT(this_cpu_has(X86_FEATURE_SEV)); + GUEST_ASSERT(rdmsr(MSR_AMD64_SEV) & MSR_AMD64_SEV_ENABLED); + + GUEST_DONE(); +} + +static void test_sev(void *guest_code, uint64_t policy) +{ + struct kvm_vcpu *vcpu; + struct kvm_vm *vm; + struct ucall uc; + + vm = vm_sev_create_with_one_vcpu(policy, guest_code, &vcpu); + + for (;;) { + vcpu_run(vcpu); + + switch (get_ucall(vcpu, &uc)) { + case UCALL_SYNC: + continue; + case UCALL_DONE: + return; + case UCALL_ABORT: + REPORT_GUEST_ASSERT(uc); + default: + TEST_FAIL("Unexpected exit: %s", + exit_reason_str(vcpu->run->exit_reason)); + } + } + + kvm_vm_free(vm); +} + +int main(int argc, char *argv[]) +{ + TEST_REQUIRE(is_kvm_sev_supported()); + + test_sev(guest_sev_code, SEV_POLICY_NO_DBG); + test_sev(guest_sev_code, 0); + + return 0; +}