From patchwork Fri Feb 23 00:42:48 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 205155 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:a81b:b0:108:e6aa:91d0 with SMTP id bq27csp307296dyb; Thu, 22 Feb 2024 16:43:42 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCU1axc2+7gcN7K9TyY4ar5T+wyDP9f88yJJrCFjMT/3ukO7ottOWkIGx8O9HXHs3bRgaO6NJiTDlhAZ8ino/hptjJFHNQ== X-Google-Smtp-Source: AGHT+IEntA6TUFbhDvk50D27L5w2wJSCsJfl7RyEN9miolNDGQAiU+ku9grJFv9SDeJAGsNBJhJb X-Received: by 2002:a05:6402:1acf:b0:564:b823:a78 with SMTP id ba15-20020a0564021acf00b00564b8230a78mr192072edb.37.1708649022137; Thu, 22 Feb 2024 16:43:42 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708649022; cv=pass; d=google.com; s=arc-20160816; b=EeaKB9txAP9VbuAxzjNSov5VJAhxJMeG6DeeiNPeZTFTB1KBxNSnioDesUQ7zGiQq4 IZmtad1ef9JD1+Cpo9nYTKr1CCKWgIhhM4q3Y1QOlFedxKhYA2IPQWXOUMEizpY8Je1p wPfLLA+Ct/6ceAjhgq4zb7WZsDksm8uBHCkoB4xwoN/HOnJnhDdfwlSt+uD9kzJZkCeG 6zGtPNYR5RRSww/EsMgjLACKiTphhNfrHoJeLzT2GuTYiserDtwpoQo9maQ/gf7a5ESI Rd+gDhoUFh+X2JLxjLcNqU/pKqRlWkcdi1NwIdZA8mERtKL88l+5B2KrM07ACQ6q6VV/ d2gA== 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=S8c8bYa8F6WSgYnQg5Ml3mIasYavsfmgJ65i8xz9FOg=; fh=FAAV72/zItnd4/mjua2ekSWY4f2ukRzsDAl4NOK4vss=; b=MjPBmF5Rm3T391+AwJajkxc+ie8ctUecWUAxaaOkhtK1aEcBtERTAUbCWvFDSwSnnc x8IIxA53kQK3eH3uJFOvpUfE7zDHYCJJsjTSPEKE6vNrBksrY4dRSeCTb3hSVB1B/0sB f3f7DeUfGp5msxsYEpXbOmm9ohJ4rC5gCKp+piJHbo+JfDgKMht6+jEEW8qTlvyvJfh7 nmO2IAP8d+XvqHAMBl3uZXswFgOS2Jm2OeBN0jxa63jVMK5OIeD8voZZFNpSUjxX1AbU dP1nNy+hjnrGBdEIv5H3iRS8VKn1lb56rsdgc1GZ9o7+PdHyVHETbRXmQ7d3LjPLGgT/ nqtA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=BAa7me75; 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-77582-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-77582-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id f10-20020a0564021e8a00b005642c002cbfsi5206591edf.22.2024.02.22.16.43.41 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Feb 2024 16:43:42 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-77582-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=BAa7me75; 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-77582-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-77582-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 75CCF1F23245 for ; Fri, 23 Feb 2024 00:43:41 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id BF7F3BA5F; Fri, 23 Feb 2024 00:43:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="BAa7me75" 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 3D43E79DC for ; Fri, 23 Feb 2024 00:43:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708648985; cv=none; b=dXrQwVhKPrMf6SOYDKqatc6Z1JPEJ0WAn6Rp1xdfZJmnd15KJXY6qdJ1ZVjhxoD/Xyn8by052rJxC3HslcUefnzyM6xkre7W5+foj6Q9IZopKj1HsvJYHKXpaQ7nsBkNJbMoS+7QfzVt0Ew/AdAIbCPpMRiy9iyn7tyoN+askc4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708648985; c=relaxed/simple; bh=e0zqvQX4ZlL0fPn4DwqoWoLqJX7b6wznfe/Oj9KjKhM=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=cYJyqCPGH3d+nziXMDp543109k331Nk21Q4n89GElCMbcYHOTAM1OogDlDaYBOWGFKgX0pcSPgecCoRZ32u0N7ts6sRgCGT2kjIkiY8wEGUpexWGRwNRvuhrlat9hG5scxmU1xjL7m4aG9EXszAzxPkMn0TZ5vaggSo6G7AcW80= 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=BAa7me75; 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-1dbc9504a1cso2457515ad.3 for ; Thu, 22 Feb 2024 16:43:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1708648983; x=1709253783; 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=S8c8bYa8F6WSgYnQg5Ml3mIasYavsfmgJ65i8xz9FOg=; b=BAa7me75LRuDqlpTm3X0tdjmN0XFop78UuVTZca+6ISLX+0aGhdMgTCZGQv5VGyCf2 hS0nLEVDmiahO64jlYgZKFjVo40L5HlhTRUvEX1gy5uGfBwVYHmnXZAfdkwMMNRW00Qk NZXW+73OxpUHJj1CZj9evmS+zbjWjHckicZhHf/NXRvjO2U9h4VRzR8bgYUT+cf/EY7s iAyClbozLWKDut/i+b/lmf7imhSasBcn0RQMGAcILNCPFhhQJSLtsexJZRjFbk+ZI++A okD+w5WHIkk42eT3eMupOyKG+PJzToM543jV+/MhxusKHYlMY5Igp3s25aezDaP0vjPF GWJg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708648983; x=1709253783; 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=S8c8bYa8F6WSgYnQg5Ml3mIasYavsfmgJ65i8xz9FOg=; b=pae9NTsEBrEVsbYL+QTmmkFeLvKxtXVZ4DXwroVdaYz7MtfA+w4UUTrG7LCaFsRqLV TKb2nXC3Kn4KypoQnGD3noAsR52LAlNvBMUcuoJXoosNAOLHsEBEGB4GeotRnqaIdRzG tJyk0T32YjoaKxOz+lQSOMLEumhb5gg1Zi0FMyXiWG0h6WteQ7AUJjx8hUAFBQMdsQhd ZMc4H8K5B8u6AzJ/aVrmGeKXDkArtGwtVFcF/rLp95qggrR3K1DWHy6n/X7Uxq3qwrcu ibTahktahMtrGQmr7mKRr2NFPlGIDEgJbiLEGeWVh4auI9BlbEB0JpB7doiT0aBTq2m4 lRVw== X-Forwarded-Encrypted: i=1; AJvYcCW8fqVEcCEPolbKtM9eeWgq3IjCXExAWEKy4tQKzUBFwa91PAgLbCHcEHBJjNoIgvy8N0Bp1eZvrMGeqmNbkwUJrmi8RA7Nk7cGkXl4 X-Gm-Message-State: AOJu0Yzjh1Sl/oL/Vtcu/k99VO2ksG1H2LzIQv1vj3JpbxrzR/bZsevq +h9kXnC66eByyhEiomCDIiKviKH96x0DKVVHnypkLHNUOTb3Uz++CGlbCOLs9zo7P/PIw6ELQTg O7g== X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:902:d2d2:b0:1db:d810:89a5 with SMTP id n18-20020a170902d2d200b001dbd81089a5mr26791plc.0.1708648983559; Thu, 22 Feb 2024 16:43:03 -0800 (PST) Reply-To: Sean Christopherson Date: Thu, 22 Feb 2024 16:42:48 -0800 In-Reply-To: <20240223004258.3104051-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: <20240223004258.3104051-1-seanjc@google.com> X-Mailer: git-send-email 2.44.0.rc0.258.g7320e95886-goog Message-ID: <20240223004258.3104051-2-seanjc@google.com> Subject: [PATCH v9 01/11] 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 , Carlos Bilbao , Peter Gonda , Itaru Kitayama X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1791648357093106071 X-GMAIL-MSGID: 1791648357093106071 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 Tested-by: Carlos Bilbao 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 Fri Feb 23 00:42:49 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 205157 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:a81b:b0:108:e6aa:91d0 with SMTP id bq27csp307555dyb; Thu, 22 Feb 2024 16:44:27 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCW3TOSI8z/5QgjEdE19R2P7k/gs8kyMF5hJ3KvimznkNc5Rws91XaAuJaSIXepAn213CxAV5xCyWwv5A7SNh4tTGBYREg== X-Google-Smtp-Source: AGHT+IFKRSy3gzeq14DJDf5LlXVFi7PNlMDjKJhEeP+ErTaSihw7ELaDihrxBxJIVyvUBuQk+tWt X-Received: by 2002:a17:906:3617:b0:a3f:9971:bf73 with SMTP id q23-20020a170906361700b00a3f9971bf73mr216103ejb.55.1708649067687; Thu, 22 Feb 2024 16:44:27 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708649067; cv=pass; d=google.com; s=arc-20160816; b=E6ucCP0G+Sy2YBDn9hiexU1Tf8cWsmMUJ92dVN3wcns85BmPKO3tm8QQhrY/Rtug+L QU1De4gXxQP5YAnjmQBzHI+T+1KXyWGRoJsdf2Pw9SMflRZyqBMjxAn77f4e3J1QU3Zw zlkrvVMnZ3OioBMOKJ0Dwoh25GF+CvGAmAwISBC94orXhhpY8+Oc+xOQPVUDf4ZV4zrC +AdyPB+aCXda+JKcXAYAXA2fUk8ztdlavIJ1q1tbk0V1NHz83EoQq+AJDf3Kkk6WEXuJ Cul1DFcKE/f7kVB+g/HNQ2ioOcAuyJp9iGjNMLuL9JYM1uwA9MYeqJUawIX5W2JD41ki k5Aw== 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=SI4FhPnJU4nZML1luEnjHNpkKZ26vwmW3AcD0Kd2orU=; fh=xw1VeKCGT5r7mKrcSMbmGVlicCKJFFwidsE4wseK3tE=; b=b9fETjPzj7OKyXwde9zcoxFDv6nl6ezcJ9X7ZkwwYdoHVoTIZGsarZ0VXqZM2SLhje gsV2tXZCeTtz652ow87uAp8LXSzWne938xqqLkk1eu2ELzsOXgw29FRajNM0625vPsYk el7FDSHM30zp9dHRUqpEze5hDoZHjLDJqRhMm70DcYLeHIxgGUsiw1ZtzaTFKus0fDQV SQla0J1zL0NqqScFhpOblbVGRUSx+wVpvw26SEOpRiEvBoU0Ace0EyNXxQYs5uFRzUVm Yf7mkrz4am8NxEf2TgcQOV0d9gnCemjtR9BKeUxNZ73guFsGghe/gpebp+O5o4232oVA jQLQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=EMLfkt5Y; 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-77583-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-77583-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id ay17-20020a170906d29100b00a3f848dbca0si925678ejb.594.2024.02.22.16.44.27 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Feb 2024 16:44:27 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-77583-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=EMLfkt5Y; 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-77583-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-77583-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 061D41F24ECB for ; Fri, 23 Feb 2024 00:44:27 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 9EEF610A1E; Fri, 23 Feb 2024 00:43:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="EMLfkt5Y" 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 841B2AD4B for ; Fri, 23 Feb 2024 00:43:06 +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=1708648988; cv=none; b=qTcELGhdS5oRFGHjl3EPzjktSFMh6b8UX12SSKGXNJMrj0sSXNTpbHc9psWFUdctcIt8uEWPqu6wT1hF2mGApSudTSd0wFE9gP61P+OwRnfR/YhQux3zRTKsO40E/yhBbpdxM+3CZkZi8P7Ck7XitDEw3B9iciJL/2dXr3VVpsY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708648988; c=relaxed/simple; bh=oFbuGlDhb8dmh/0KJBsOV/6K0uAo5ZTZiTDIprSpbKE=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=EYJyudziwuEeceSmCNauJlTf4QBNqG02XglEWaS2wcqDL3yWgFODZfjX/Y49QoEnIGkXs+CRjm04vb3LaNJ2IazVKfp5TjG75eXnsByvQa3cl3JEkgQh9g3bxi+TI2Pn1GMBrGEJBH2t5eRske8JdP0jjJGYZSrZqGPx+UNL340= 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=EMLfkt5Y; 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-d9a541b720aso521435276.0 for ; Thu, 22 Feb 2024 16:43:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1708648985; x=1709253785; 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=SI4FhPnJU4nZML1luEnjHNpkKZ26vwmW3AcD0Kd2orU=; b=EMLfkt5Yhl7pQjU0kW86TPXxXk3mfUKeCGW5ODTdYm/OMY2CqKeYANGfudWqjwA9JR pSbdiFnWvH7317BPCiK0dAL7IxL1U4CEeoPRnQD72cU2D4VwA2YeQCH6VWWG+2YSrC6B p2gAcLFp0Xw1rjipB3WAANUFrAHq8qnibZoKhQuqKQaAXoc8Wfm8VJo1Ocn5sUHk9WJ9 5W0BN7V1IB8Y8iGzHbk+1pCaLOKdDlb4BO21VISnJiCT+Ly0kYRiQpKeZpRXpXMNFn4K onogzqtotk/+Umj7/FBMBlcV/FSXnDuVUuoegG34EZXORjwwjYGav+UKY/c7PuzPvNK2 7uTA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708648985; x=1709253785; 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=SI4FhPnJU4nZML1luEnjHNpkKZ26vwmW3AcD0Kd2orU=; b=ijxcWGz/f5R4mOCYhcSFYdqd9F30gEWcx+r6gISfvT1TanVdhW6pgwZdLx3hjZDPU9 U4VYC4CvGw58RI+dPQVSl20ZkBFmA4AHBfTfpGA3KsfroHKC84kGTBRGxWhvLFAU4vMa uIHeQzfN0gGrnxJGGR8OrL/j013oVH/T/EvR37w+uckPEREQM4Qy75xSCgfyFSh0xhN2 6HqwjHT0nADuxUTzEwPt6jHnSv65Wcxe+uylfgLI9AYGFMGHe0yaahU/xEs/okOiIYA0 n/hdhPgeNOwm8kD3E8e4b+YjcRz2KFuVQU8B0MBQ2qrbuGGnhVZz1LJOpopB5ehM9DtV peFA== X-Forwarded-Encrypted: i=1; AJvYcCXdtIqsc/u+b2+FrBeLYTWPNSmNloAUa4j3OCYk9hAuqI+5HyFMB7i+VwBH+JuHVeIoUlZJN0uYghsRkOr9br7A8VYi0kjD6WBzM+dn X-Gm-Message-State: AOJu0YwPsRftl7VJIMCa0aACVEO/vZk/MqmH4y1PV0HRcZi0fa2LVZYf NsRD7WzGlGhMrOA88wu5seUHuX/SKuLFl+ylQ4eYTIekFlEycjQkTckjswXaPBA5pLQaVYEVc/8 6EQ== X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6902:100b:b0:dc6:207e:e8b1 with SMTP id w11-20020a056902100b00b00dc6207ee8b1mr207570ybt.2.1708648985595; Thu, 22 Feb 2024 16:43:05 -0800 (PST) Reply-To: Sean Christopherson Date: Thu, 22 Feb 2024 16:42:49 -0800 In-Reply-To: <20240223004258.3104051-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: <20240223004258.3104051-1-seanjc@google.com> X-Mailer: git-send-email 2.44.0.rc0.258.g7320e95886-goog Message-ID: <20240223004258.3104051-3-seanjc@google.com> Subject: [PATCH v9 02/11] 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 , Carlos Bilbao , Peter Gonda , Itaru Kitayama X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1791648404998695534 X-GMAIL-MSGID: 1791648404998695534 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 Tested-by: Carlos Bilbao 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 Fri Feb 23 00:42:50 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 205156 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:a81b:b0:108:e6aa:91d0 with SMTP id bq27csp307490dyb; Thu, 22 Feb 2024 16:44:16 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCUiFK3v/eZLerDg2c3zGohjIRSfj5NQc9eTAIjy6YFg0Mhu9BOpYs2b4DfL8gtc7zS8y3O9Uu4eC8xFnVb6J9vuMG7fqw== X-Google-Smtp-Source: AGHT+IHuQnQ+nz4zXSuwqU4dquWvdErHM71zBZtZrkYYvVFkSqyDtdDikjSb0HtO9xr3NgasCZbM X-Received: by 2002:a05:620a:145c:b0:785:daec:4c49 with SMTP id i28-20020a05620a145c00b00785daec4c49mr745429qkl.25.1708649056790; Thu, 22 Feb 2024 16:44:16 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708649056; cv=pass; d=google.com; s=arc-20160816; b=OG18jYwxVSYkia7IzwkTJ+BWDiJCEutYXxw4tlj9hqqkwVoxi2UJ3A22MHMAmm80sV WiUwSXrxjCAxAo3QzLnoTC+ViC94sEXxGDGbCHEE/b467Q9lIR6Dqhv9O8Y0wr1s2+hK Y6EQa5fR2akzYYV+IlZL32igkDc+aOCNW/z9GLcUYG0LNbyRJ4CuOFAbj3UwxJ85/WUh 4JCAuVprHc35qot7y88KKbD3Eac4HhCG6wATgJhgmJdjBBpFouCO2CqSrsOloX1I4BHm 4yQkJTucykIlJZxF9Mc9WqEg6at9IQ+QIgPBBmRyZh2MTrJoCgddPyN/uXfdMmEJs8BB KG9g== 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=jhSBUfusWJ3ofLKWMGpg2YDRgo1XDnuJP2HRidQqwH4=; fh=CZR1vSfjLCk/dtjPotIznUa1isn5nwc2SI3jnMUw7p0=; b=aw3+cunPsKYTO5Fa7ZDJKRNTtSOeRrMZbhCWKDdknoHoJMgsRhq9aprnnMdm2LJjT/ B+H7Bo1G/GiH3zYqgMsIRD+ssUlkLwBmL7LYCBcA3iGJ8fL2q5JOtv5R1GNRlDYDFrEN V9QIIbioI4fFf8IUpmuATtEq8Y5R9969dJD1Crg0nTjn52HX07i2KuPDpJEDaA/gcsfU lZSDkZ2PMwUOjjMa8Jvcizibq28iDrFvRT+h6wpyXwcf8n/xQl4QIbxs5WfTiIwBpmfF hqfb4djU3G7PJLAhqjxELaE9tKhBjsFr+/3l8uFeuqkLbwunqdpyWQL37LWg7ygIlsah Bn3w==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=B9DH6Gm+; 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-77584-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-77584-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id y8-20020ae9f408000000b0078773b7760fsi7457796qkl.751.2024.02.22.16.44.16 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Feb 2024 16:44:16 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-77584-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=B9DH6Gm+; 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-77584-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-77584-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 92D7B1C226DF for ; Fri, 23 Feb 2024 00:44:16 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id AD332101C1; Fri, 23 Feb 2024 00:43:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="B9DH6Gm+" Received: from mail-pf1-f202.google.com (mail-pf1-f202.google.com [209.85.210.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2E66CBA28 for ; Fri, 23 Feb 2024 00:43:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708648989; cv=none; b=L0z4IuV94uiqo/pZY6J1fSS4HlfvtXa0YyOscQ+fmnqfE1deN8S9oRoynnCfFxnlzfXWf/6Fbj66a75A/SYrwm7hPkpzSKMKLh4Zacv874/Ho3Q79mBcf95NMfxMZergdEBAS1wX2jQM+emn3J1IINjq6HK+UmTTgv9FOa4BT0M= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708648989; c=relaxed/simple; bh=LW9qJDJFMZKoAzuMjM4EnMGD9KOMiSIoIkVhiiIE6n0=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=UcAQSLqNONsIb1TQDLsoZ4Uzkk7j9C03ZySOSU0VXSGSWfSeLRh6AXJ6judsgcX6cY17OC306JYIqi7Zhlnlg/XUWqy7WSPOFWTkr8nyqX1AD4I6KKPLhFyMjR2OePQZIpqrGVyNYoOa5LXOnt/wL0nWqU2WeE/lkVDmr/JBp+A= 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=B9DH6Gm+; arc=none smtp.client-ip=209.85.210.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Received: by mail-pf1-f202.google.com with SMTP id d2e1a72fcca58-6e04e1ac036so259698b3a.3 for ; Thu, 22 Feb 2024 16:43:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1708648987; x=1709253787; 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=jhSBUfusWJ3ofLKWMGpg2YDRgo1XDnuJP2HRidQqwH4=; b=B9DH6Gm+C1X6H3ks87B4mfBgcd9wrOAVOYx/XLoU3zt5slGPbI/pgo0jjo+yt4Nwdw Wz85tdd0ojV9EgE3ccGgA5+2lkUmmnmrSYx+tpsSBbmdezCatTEAoP6pwSExWNvIUnQF NQUKEFL8v0FM2Oj1S12koT0uc1vdPpxgJVUK7qSZxdk2yJcrBw5oXcG+jWjRh504m5jg pSncjC1jPFGfNMAo2sZ6hA82ct9ONR47tX+iptXOqSoDjFZwOO2ZNUQ+eU+J6/y+IgZO 9r6QxDfl2C1Je6cforf+10ZtutDNe8LKpAkEm5JyiqFWQ8hsPZjSR20seimLstRlrm5B JZpA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708648987; x=1709253787; 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=jhSBUfusWJ3ofLKWMGpg2YDRgo1XDnuJP2HRidQqwH4=; b=QeXQPh6XtecFGOUK2PzzDdy41YhRzNq+6jX1/s2Gkb3APyNX6T3liyT9+1d/41uzcV cuLpV0vn6v7l9h2l3JB6gYCLaU3yZWI/lWxIwIJw5hoedPkfsroVuOr/Zy3FUGn+l4rL ROeImY/8mYwXNtOociH6yhHWcqxlanumHJR9emJNJGFufI5JRgyw9pYpDNj/xkCA3k1f 8FmupiwZgIZBgvgzLIZm76/x9doVhWJJ7+K+NZR6pt7tPgTB0CigloLhgoFRZVROIEcd 2ov5+RiCzGDiM/b3TGBF0881e2XodHWKyCNxXMfzBWu/D51gF7ZkXcxGYIFMj8QU3piV oM2w== X-Forwarded-Encrypted: i=1; AJvYcCWBgs5dHkLV8NwHVfEysl2YmPQXNYVJduuSYTyXE4IFSE8WdYsCDCs9sNcjnBpUu6hsxQJpY5B7Ap7VN4uh7HIABZ40MLc0uG6GK1vp X-Gm-Message-State: AOJu0YyJe8Kuxo0WcH14E1WrcRKzsCY9BGvgM/0DXGrtrGRjLwAN7LGo 1jdk173F9F4TbYEu+EDuI3XNuVmwohOONynk8tNsYrdTa+OI9X8SVXLqabPm7bRdHscTqCJdAj0 KKQ== X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6a00:22cd:b0:6e4:643e:e215 with SMTP id f13-20020a056a0022cd00b006e4643ee215mr24254pfj.3.1708648987517; Thu, 22 Feb 2024 16:43:07 -0800 (PST) Reply-To: Sean Christopherson Date: Thu, 22 Feb 2024 16:42:50 -0800 In-Reply-To: <20240223004258.3104051-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: <20240223004258.3104051-1-seanjc@google.com> X-Mailer: git-send-email 2.44.0.rc0.258.g7320e95886-goog Message-ID: <20240223004258.3104051-4-seanjc@google.com> Subject: [PATCH v9 03/11] 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 , Carlos Bilbao , Peter Gonda , Itaru Kitayama X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1791648393585029705 X-GMAIL-MSGID: 1791648393585029705 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). Tested-by: Carlos Bilbao 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 Fri Feb 23 00:42:51 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 205158 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:a81b:b0:108:e6aa:91d0 with SMTP id bq27csp307670dyb; Thu, 22 Feb 2024 16:44:52 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCW/HrV/2pr2xA9WlUYoJBVd36MOwxBY6jmA/QwUdUzCy9KNfUELZ/tHcL0DNQ+dOKN5RxwVRcbxsKBEDlvQIRvKmWeYaQ== X-Google-Smtp-Source: AGHT+IED2DXBjat57JHG+bkNr9/PyN7FzVWYKSp7wz4Q9mm5BE9mZsJeHxYpJkeND+ZzMyG0VTBg X-Received: by 2002:a05:6a00:c8d:b0:6e4:64c9:8b6f with SMTP id a13-20020a056a000c8d00b006e464c98b6fmr1039880pfv.14.1708649092523; Thu, 22 Feb 2024 16:44:52 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708649092; cv=pass; d=google.com; s=arc-20160816; b=k1eQNaNZtxZq3zXcUs1k8y3/osQuljtWALEqUZYnqkJrd3Z+ZSYxlfw7BdgDaOoZms xrQYX3+hF5p5zE1odSmvnPIq+qSFneW3FkdRDXMkEO6Ri61uUdUkaaRFe24hfvION/AU 5/eoeDFRBsv5jLbDY0A4uG7OTS7lvXoqnngciMMI5Kke9HziYx4XOTTsvnx+MTv0kyK9 GirxMc9mD6mXMLfJWSAO+802K29iw7NoFBZI8ZXV4JEkllbN7B6ehwrgOt0QSIQwOrHe c67PHjohtbKTNuZsLB48FElBh3LIOLdHY8rXN2KU+8LMUqloAjpnQvUgVssbtJls14FQ ZerA== 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=HqrOgwqdeyKlRqEiiRXs7cvS3neUx0UIcneX8OI5QMg=; fh=VVqTLqkqCfz4AX96k+AzTAjF9WA28GOhWS1HHqaShks=; b=dG10yQEbie3q4z8r1yMmL3uu5M+gORTxMzF3tdtGzZCw4hxKSfCqSchqFy+43BFMMT AqxxNRK7ojyrH+BI0izyxmBEgu9in1CBTjY8cTKGDRTFmuWsU/OvYduUOP2ogOk1uljL gOhJW3J87b7kwApWK/psREjm+kJl7eTA4kM2aj6+SVNaCZNlbZvPUojmzPgGgQkCRfGR 4RmZBakeOaJOIQP/Hqx8El5rqxVhbGutYSSjTsoIir2McxOV+7O2CfcjmVnCuNCd3243 sa7v60sAAyeTnAOKhapI1GuUcXeGIY8ttfoAnAuCRhYpu6jVhGSFMyhg1cOBKN0zVyJ5 RiqA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=aQEWBEBw; 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-77585-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-77585-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id k80-20020a628453000000b006e334a4f9f5si10262125pfd.312.2024.02.22.16.44.52 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Feb 2024 16:44:52 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-77585-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=aQEWBEBw; 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-77585-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-77585-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 4F719285E38 for ; Fri, 23 Feb 2024 00:44:52 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 16CD511738; Fri, 23 Feb 2024 00:43:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="aQEWBEBw" 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 496F9CA73 for ; Fri, 23 Feb 2024 00:43:10 +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=1708648992; cv=none; b=WoEc8Xtub/FN+XZtPoDFGmuKcaIHKF6Uv2534KM6/NY2Ou+S5r8aVSVrzBYNBBZ+B70jLwSxvZIVXKazBy+r70Q/AO2mywUBkPrkpZIdMvslXeN1lWWno0uo6ct9LOJuyL4SUJzjbssS87alOZ9tKkLU2LAeJR1nSN221QsKKLA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708648992; c=relaxed/simple; bh=kOWMpHRm0Jt6rsGNN4rcuUv5pHmG0Bajgke/K8TPQlw=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=R3swIlzlaHM9hU/ZN3Pp9JCG2r7LiWnbFWcYgJQF9oytDHb3oXCZfX8Bd8hRHMufVogWAoySQm9J6dmevnXyBai7wNvpoZ/7+V30g8pkDY2ZJfEd9bKxrPb2u9/I6GBs5OzAnH0cvG5L6BK8lMwmx9oed4UZpkNdh2lpW5fDV48= 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=aQEWBEBw; 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-dc6b26eef6cso425073276.3 for ; Thu, 22 Feb 2024 16:43:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1708648989; x=1709253789; 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=HqrOgwqdeyKlRqEiiRXs7cvS3neUx0UIcneX8OI5QMg=; b=aQEWBEBwkqntI56A32yvU+TzYfrUGEtwR7LHvWnjZTTOCDLtIIfVp+1ArIfc1u8ycu nAWWA1EhGy7NGJlFkIhps/7bNmHhsI5XR0Fa+jh0pIeXHrc2YDXYKMRHEyzYQh1ndUdu lO/ZTN5HSPKgXqylaH86fwmg5304SXqqG/F/L0Iv4x15vle1MWkrNeRLNsCc6fmuRpQH 7RGWMk7EMITNDvtl9slESSt+htJ5vfpxV7qDOF5IvZNWOjRPUBqZhp+u0kV02cS4yaO2 wH3OicR9QYQPqk3zyhFv7IdTCs2A4ea94sCcRR52AWGylv01cQQp2jn04cNHi3yD4Ado Lv4A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708648989; x=1709253789; 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=HqrOgwqdeyKlRqEiiRXs7cvS3neUx0UIcneX8OI5QMg=; b=gJ1rjQGjDzFemARJxAhbB1J5JSYeqWhwLx6iuYoczA9019Z5sCIRmiDIvsO7/xJDAl GRNgWlliwansSm38rzaCvlafdhOjjxBfMuoD3JYTpAXUMemoCqfEUxkH7xxR1w7EhNnS g8Q31pzVE2G+D7V4mtkLBOU3JH7BKRZ/CSGijqxZroiBPdmPWxVhvTwZxuPmDO+3+QXz 6aIuPhW0FSwj6SdSW7URXloVmiZnZBytIJosfHc7mBK9JhsvUYIHi25IckOLqsPqAIor f7/U1DkS1JvE6WpWkq/D/RZ3Lo8fQikE+HOYk0GE1JDPTpwICja4dnF/MRl8kcE8mK+v f7Sw== X-Forwarded-Encrypted: i=1; AJvYcCWbKTYhq+qEmHFWOgSqb2GrPFB8ORQ2oNTiHCr0uIdKlQadtSjAwlKmAc4aqBkN2+mcitedapnqw9bg6QtJDgVMKLfZiKrFZ7/E6SyC X-Gm-Message-State: AOJu0Yyeq9uRbWZ/007A8IiK6dFxTHX/ej5yJwfob/B+zmrRobefqnwa AsHsaLpnwnTWhHA2RFJ2U/kz7rdL4ilTNEwsKuTtOvM5zCgZ2PVz1HZDOXgUF0sy3laxcB4xc60 LKg== X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6902:181f:b0:dcc:94b7:a7a3 with SMTP id cf31-20020a056902181f00b00dcc94b7a7a3mr21587ybb.12.1708648989306; Thu, 22 Feb 2024 16:43:09 -0800 (PST) Reply-To: Sean Christopherson Date: Thu, 22 Feb 2024 16:42:51 -0800 In-Reply-To: <20240223004258.3104051-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: <20240223004258.3104051-1-seanjc@google.com> X-Mailer: git-send-email 2.44.0.rc0.258.g7320e95886-goog Message-ID: <20240223004258.3104051-5-seanjc@google.com> Subject: [PATCH v9 04/11] 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 , Carlos Bilbao , Peter Gonda , Itaru Kitayama X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1791648430721223400 X-GMAIL-MSGID: 1791648430721223400 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 Reviewed-by: Itaru Kitayama Tested-by: Carlos Bilbao Originally-by: Michael Roth Signed-off-by: Peter Gonda Co-developed-by: Sean Christopherson Signed-off-by: Sean Christopherson --- .../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 Fri Feb 23 00:42:52 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 205163 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:a81b:b0:108:e6aa:91d0 with SMTP id bq27csp308261dyb; Thu, 22 Feb 2024 16:46:40 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCU7ytLOwd4cYcEiAHRbzqf+befnhOd1JPGoHM5zr4H5noIBTGdnr9HbEvQ97deEVL25k7S7IwlVMv5Z/ruWAlxPtzI5fw== X-Google-Smtp-Source: AGHT+IEbP3rZOy84aSUvDOch8NTXGviefonXMUdiPbbJ8P643VQzrAaR3ntDmfUKf5CMgYeIyyuf X-Received: by 2002:a05:6a20:e198:b0:1a0:d253:973a with SMTP id ks24-20020a056a20e19800b001a0d253973amr460229pzb.53.1708649200516; Thu, 22 Feb 2024 16:46:40 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708649200; cv=pass; d=google.com; s=arc-20160816; b=g16OvbL4XJQ/S5ss8wlQWVHYSxyeysAwxDCJEkA+KGKEXLNl0ORUar7XjQH+k9VupI OgFHkpT5tfnUl9uLG2ZYl1TF1rgE43KKVsqcFXR+WQxNw6psGamMlSbaselCxtZEXUeS WdmfZl95UDJuTJoGPbBTzibTfC7Ju4FJfCGYJj2hEzNIiFipz34UaA1aJ7YgHiqNnJ9W 0WdXepa2t6qaKCVS4aFgZCM90Vo8T2jQ7C421d9XvH4VZa/8jJY2KjOrHyxa2GXeVDPb /0mPOmnb055+luptNbe6yGhE8edKLZGpoNt22Rc29C5rt/85hVHDltdr2Dhg1eUOT8Qa Uk5A== 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=eb0yDEaAWass5g0hMAU4JrW6cOWG3HVfWM81wBHq82s=; fh=vEy6K41iKgd8YKW0/OLhkSDxP2WcG55bcvh6d0b5cKI=; b=X9NdlUlACLLj45kHyqRDXUOxgccKkhC/unHqbKVz8EU8iUq9wpMkB1P89fU5KXiU6N k6ZkyviXI57JERA8lyiZN9/xlEQG5bJCtjIoRhoDwZI5meZQuGYdZzDXvKMswNWPfPlo aw02MSmf2jZTjyPuu+Rt2ZjIYihHSCVszYnvXkozZp5TVZdozDsJNlYjoe/ByIMhMNc7 xGYyhNk7Y4wY7kjPUWIWgqJBj6quKhp3XCpBHSI7CAmr20+lQLzbQ4rPVUw6wgkGxhRa 95gSzs9TXxjSXjYFtvr/+k9XfnjS454cTUggqPNu38+DSKTxzwhsZJ2VQ7tlO7J+xaEZ +YkA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=Gzjk6sOZ; 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-77586-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-77586-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id i9-20020a170902eb4900b001db8cd74953si11063147pli.112.2024.02.22.16.46.40 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Feb 2024 16:46:40 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-77586-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=Gzjk6sOZ; 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-77586-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-77586-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 49A9CB23C32 for ; Fri, 23 Feb 2024 00:45:09 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 1D01B125BC; Fri, 23 Feb 2024 00:43:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Gzjk6sOZ" 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 2EB34F9DE for ; Fri, 23 Feb 2024 00:43:12 +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=1708648993; cv=none; b=tQWlVTMCVI+yAcx2knzB2zYFSDt1N9+dfko3u7BBQz53w9qV+u1B+mhgWrjlBzf4rBKgbp9o3gewaQs+KJwqmSCUtUb0JHHy/SNPZLrEyaNZjM5u4HZtZ8phG4GP/W9xBKW4xf/TlL+oNsZgFgNzJEdLGoevNdkwuRylRvnOUnw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708648993; c=relaxed/simple; bh=S9zU+8ciTEx2zWEcC5twkkb3JnOr+rQWNF5dvxUiK+o=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=NXE7b7bHg38rDNW/Y+vF73yeX4vsgifL+w8uKBk/5FNVF9SL12KyEtGkjommZSa3XrCtCey5JZ9WTnWCwXE5lnH3dgyFpUsYi4MvnYbCavtQsDVEO9HSbJ5SdfLNKGghnRmGo6aFwYvxUTek8RqmpVMAxXXpMnnwe5roorfYimU= 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=Gzjk6sOZ; 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-dc647f65573so569050276.2 for ; Thu, 22 Feb 2024 16:43:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1708648991; x=1709253791; 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=eb0yDEaAWass5g0hMAU4JrW6cOWG3HVfWM81wBHq82s=; b=Gzjk6sOZiNCZ+gpWeAbNxsPAJmORLYywRjriKDJIVWOFMOePX4r8wPkitBeF85XtUL d2c90JzwSr7L+t/qlEXwOzB7SSMBBM3Uq1ADnSXsK8CSAophyYC37JtIXsP2iud8hyI5 TH6NUdyJvnEm0tT8O1O5EQm3YDn+Zse9HglRcoWGLEXnOTxmJYwM6dEWViDaFqnsiVYa tBItNqS5Yd1WR72MZU+cnBZi5kCSObGKthq3OXFjR5qu/34f4OQFV8wMv5ZPTQs5jiGH 8uIpsiuCLZGQk5BVoFCh/CfaSYq4za3131D3qP84JJIzgXLxR6xT+XYy0kl/An0RB7e9 EnRw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708648991; x=1709253791; 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=eb0yDEaAWass5g0hMAU4JrW6cOWG3HVfWM81wBHq82s=; b=SpUs+dzT8GGK/dRoa9CXI0egpQZKljc56I+K5c6k2lWjYgS4WAYwhZaYzDYVw97IHN 22VG0M2DtEZqKY+8Av/+sAtoYPoT+n7FCOJr1696jrYut1eEm+paS1yQydo1aMv10g7O MgmyE6B8gehVwPg8+lINoOwxulAWXsU1HRg6bS6eJ0CvtsPigFFYZ264XkoHZT9i18jL rbZBGlSyu9j0Jax9O1Z8Zuwgym9DJbVjmKn1GPJWnsxrNIs8MDjSq1mwzWzaRqg8ftin tZ6dty2RNnvMUPcWoAANFz0jUnUDGSISVfLUd+dlhc4M8DndR2B8JyLbxIbH0lnutO1e 3d2g== X-Forwarded-Encrypted: i=1; AJvYcCV2uEpchoPzH6XVCCrXtd9nKIxoXMjWvlFOt7ra687049/Qs5qpfMC1uMZrJFIQJhM1eOs0Pzrf03KMgq9QJbvsE779u3MJeHoxqIhy X-Gm-Message-State: AOJu0Yw4GViqFKSFA3/kt1G8kMHPazC/jI8vQhFSXwioaxQNPUxFQOBE vmbaJJ7htuXKCIFUYhdrZH5353WoD1sl55J1xuREkC0ig3EAvRZqWTJdk6y96Y+C/B6gfoVEAMT gIA== X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6902:1007:b0:dc7:48ce:d17f with SMTP id w7-20020a056902100700b00dc748ced17fmr187858ybt.10.1708648991200; Thu, 22 Feb 2024 16:43:11 -0800 (PST) Reply-To: Sean Christopherson Date: Thu, 22 Feb 2024 16:42:52 -0800 In-Reply-To: <20240223004258.3104051-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: <20240223004258.3104051-1-seanjc@google.com> X-Mailer: git-send-email 2.44.0.rc0.258.g7320e95886-goog Message-ID: <20240223004258.3104051-6-seanjc@google.com> Subject: [PATCH v9 05/11] 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 , Carlos Bilbao , Peter Gonda , Itaru Kitayama X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1791648544206055599 X-GMAIL-MSGID: 1791648544206055599 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 Reviewed-by: Itaru Kitayama Tested-by: Carlos Bilbao 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 Fri Feb 23 00:42:53 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 205159 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:a81b:b0:108:e6aa:91d0 with SMTP id bq27csp307834dyb; Thu, 22 Feb 2024 16:45:22 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCUdo8FMszw71L9hxG/xnLvHODToNK+6C2raZX8zPLA3Q++LP9tcDknc6kZEhSMyBagQcgAMfwSl42VOYvRd0bwj8Jbqxg== X-Google-Smtp-Source: AGHT+IF5Mie4WqBHc8Ut7a1vm8b3X3YmD7cnaOT6kunQ8QBnowksz2MsgQYLlDeSKdHkUj4mM3XS X-Received: by 2002:a05:622a:91:b0:42e:17d9:f8bd with SMTP id o17-20020a05622a009100b0042e17d9f8bdmr908408qtw.24.1708649122711; Thu, 22 Feb 2024 16:45:22 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708649122; cv=pass; d=google.com; s=arc-20160816; b=rZIlF7jigbAQgR90PBNWay3pInwMAhB7GoNw4JF+PvqGYb0nwgzjIWFukTEb5Hja70 qXwcqhB3S+4JTNKc7U0QG3lxdDrS4SZWvF2jWnpYIEjnvc4dKDSP8Kznx7ct8O1XqsDE ZcVEIopHO62tQ8dauemIZA7g0ktBHYvKwgFD/tIoW5A+bPtJNCjRgGPcZ9nB/B5vsdqG zNdiYU91cgYhzoy6uoe0GQfcOMyRiP14oTe3tbabYumMd9SjH4na5xKxHT9pnVHQYBm3 XvtKPiEmAuXbLr3rL3HrU2b/m80IZbrZMb3vnrO6E9UJiVSnFzKJuGvZTtuUTs7Q+pNS 99HQ== 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=VHEHpfFT3fq5ZUYVbA7KHyxL3TWLbo6jAAmdD67Ha7M=; fh=nyySvCLQvy7e7OQK36htqCYTMz2B8MUTXs6SRfmvtwY=; b=TGxLxtqaHCCD7BqUxMM0bJFOG4xAQq+yNCETD/FHZKj+BspeV4XUqzjA/bKZgtBbpj BiNutMSTiT0hwpddD5ppXmWq4/TUreE2OJQgPSnbwXIyOSxhKFbK43mZ5nIpjdJ1DM9u 3J0eaKRV0HRMebEgW4CisAPADKnVj2zVNKZ5H6vXP663V+jMVqM0M+LxjgJ0NIzLcZif EZZLGHgsX78k82ftQSSP0Y2ZFLnYzmmF9Bk+W27LD87vDseGE2dO+VRnw82ylNPmrEwb OThTM0Pv3+SeQXgASKD09COFF5cXyjCjMMIJQhZZNi2UvvqIiKSp2ll/cgieaVKqlJQ5 nm/A==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=vFzrhBBh; 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-77587-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-77587-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id h5-20020ac85145000000b0042c5f480416si13853218qtn.22.2024.02.22.16.45.22 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Feb 2024 16:45:22 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-77587-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=vFzrhBBh; 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-77587-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-77587-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 3939D1C22BF5 for ; Fri, 23 Feb 2024 00:45:21 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 4F08412B6F; Fri, 23 Feb 2024 00:43:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="vFzrhBBh" Received: from mail-pg1-f202.google.com (mail-pg1-f202.google.com [209.85.215.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 126CA10A28 for ; Fri, 23 Feb 2024 00:43:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708648995; cv=none; b=CI6EnF+wL/oxB4A6nqsWk+2mdORa/J/SZlU6RdzQTre6WYvO2/cwO5xovIER0y/Q4URbm2soqTI34f/hvX3GhciWsxr6G/sEVZOe+JB9YHzbtTzFb58QCN3xbd8alZQOevSH+mvIt7zbkqc+hHIgB13hlewE32MQ4m1CCaTMLxY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708648995; c=relaxed/simple; bh=EB42Jl+Du5RKKydkq6dHRGJLTaF7jM5r9fOKhUyQo/g=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=NCODju+FnLP9ax1IQQXUswkCUuZoUv87wnqieudD5ZTFw5Rln3kJwogrbnrW0L826IT1S6o3p06eQ7X/CrQjHjVvKxg1TAjj7saLxX217SBsDhC01jz9WCKo3ZLu6hZXsAEiyTx2nolepu7ap+Q8hLzzh0WJzHcHg2eip2moeDI= 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=vFzrhBBh; arc=none smtp.client-ip=209.85.215.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-pg1-f202.google.com with SMTP id 41be03b00d2f7-5c65e666609so291606a12.1 for ; Thu, 22 Feb 2024 16:43:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1708648993; x=1709253793; 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=VHEHpfFT3fq5ZUYVbA7KHyxL3TWLbo6jAAmdD67Ha7M=; b=vFzrhBBhTezHo0kb7LOE2J0LcyWbanHqcemwq0hIMfhF1i3CfkDnDhX3shykLXn1YL DI79oknmC3xBVYnxnxHJ2EZdaUW3ZwScpkpW2+J4n3Atum54OsDq+1ujx2ecDLYVVICE qJDo4FW0QtJAwXGAGRQF4MXIR8ch3VbngenolC0nTDueFFjuMH6hAceIVKypxK385hHZ YBe0BJdG4TGcF2MCeVtznBHzhacE0wRv2Uf0Yba8CtScjaxd0j3qQPl4MmST/Wok8EON UeYeW+Y9N4lPSlrC09eGE9y419d9svPPc4gTPkIi/ghf7uJkkAipSKBO+mT0RzdanXgA cGKA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708648993; x=1709253793; 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=VHEHpfFT3fq5ZUYVbA7KHyxL3TWLbo6jAAmdD67Ha7M=; b=VMJYIbmEvBtIUtku9UptSfjQAy8S/ckDPtDysO3oUVNCA817nwvp56/9bD3gD56AnT QyCouicwV1mNZu0m+R2ggfuIYn3S9WDmAhwsI00HNeLqapgexFSyDY7xy37qYAOVvCwX qAzfT6E4wtrdMQb9jTGfyYBz2NCRthi+2jH/7CPruJSdQZQcdNZ3D4znhQcH4SscWdaQ MJ3qAUbDzPFiaweSm8uDObn54T//5sAHhdp+vXylBvrMhkPlTQEkKT4yoKKJEBcYhLeG 65POiEavjYMBGy9UR918xk9hF/RRsM40jMICRAOiwNuVX0s/zpfLHdK8vBhQFbpa7Hn5 y8Vg== X-Forwarded-Encrypted: i=1; AJvYcCXJzLt0M4ZR4n7NOq9N51Ct0ghJy7CHrQEXmTv8V7nEqx2Hq2IF145006eYpv+5sl+CBBClcfUJWVprmp7quoaqL+vmZVl3Tm0RHJ+2 X-Gm-Message-State: AOJu0YwyPvG8d2vziQmPMuOorFy6PSuI5jCu3NuEB7f6lJNmZMnNn1De arkL7pDPXP8Wc+GtQZm3TqCYtGKv2+PVMdwMC/m1gYvgRbjiHaEOQc7mFFuBC33JKQkogjM5MnP 6qA== X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a65:6d14:0:b0:5cd:9ea4:c99 with SMTP id bf20-20020a656d14000000b005cd9ea40c99mr900pgb.6.1708648993312; Thu, 22 Feb 2024 16:43:13 -0800 (PST) Reply-To: Sean Christopherson Date: Thu, 22 Feb 2024 16:42:53 -0800 In-Reply-To: <20240223004258.3104051-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: <20240223004258.3104051-1-seanjc@google.com> X-Mailer: git-send-email 2.44.0.rc0.258.g7320e95886-goog Message-ID: <20240223004258.3104051-7-seanjc@google.com> Subject: [PATCH v9 06/11] 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 , Carlos Bilbao , Peter Gonda , Itaru Kitayama X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1791648462587730132 X-GMAIL-MSGID: 1791648462587730132 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 Tested-by: Carlos Bilbao 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 Fri Feb 23 00:42:54 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 205160 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:a81b:b0:108:e6aa:91d0 with SMTP id bq27csp307977dyb; Thu, 22 Feb 2024 16:45:44 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCX2DvUtkPDc6zRspiOAARUEDhPw7VZBVc8WEt3Fd8sMexRxGuSEHte1RSK5/kuAVF0sclGO3F/88s9/dNzsep2C5cEdwA== X-Google-Smtp-Source: AGHT+IEipm5tA2DsfaY1z5Jz9+yRTVqGS66cT8gHBqh7YOKzQDb6ku4QQ7e33R1F/7jXwdcuxSve X-Received: by 2002:a17:902:e84e:b0:1db:f965:471e with SMTP id t14-20020a170902e84e00b001dbf965471emr426370plg.58.1708649144584; Thu, 22 Feb 2024 16:45:44 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708649144; cv=pass; d=google.com; s=arc-20160816; b=tRUGhouZkLFsYMyYlW8aqdn378pa5scH1xKynQdLJfubsxQvgdiFT+GlVKjutgNYjF p3hG5iT2n0U1aVSFMo6aKWVefT53YEVrsy8G2IvFCcgmRQC/BbALE81EC9pODiemfjhj QdMr6kvRt8i/8IYWplnd94AN2s115BNkaiKqP4vYw6LUOdV+v6d22/jA7NBW/pDNd+3g AP37SHQjjHawSiTRxk6LMyp7vOYOJdSz1oQRL5WK7Tx3VlfkuS3QTvENo9FGCaVvnzoO DKtKsALsUzQiLp7qRN+7CgNws2/3v9CV3XDUTrClP5glUWDA+db6FF6/DkK98ez+Igzl JJSg== 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=5xSck0kmkPmdifT3b5IqGmDzhnUFDNPygC9r66kostc=; fh=E+BtDPhSBKYKA/vtrXMZ8DsNYLpjIWE8uAtwHC2CsXg=; b=aU5yEGZQ+z2seHhFW4eD8xafOrxDYZBEZyP7zEANCGl6IZL7yggK1q6PLvTDX3/dLQ PNQX960niLP5Qbm87ycW3FNj6Lbt8YmLgrt14b3vCvgsJ6+2TB4TG6xxaEabuzxeghyo cychvWcOANvqiD9YIjEpkTtyNhXcvGIvnvxQQBJQpwuQP9p0LINSlgEynm1Pyco4Nnvh Tom89EyavdHQQlOoEPOlz9yQbXQQ5r6iu+tO/FV6gfn1S0Z+gyWT/LaTuI4rfS3/BsDO CWjjgYSZMxuQKY+pbmDplKL5nEY3E2HZxgdLYKxKI6Jl98+dhFAKoWiepXcw9lTBUYG2 sh+Q==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=TLHF0+dY; 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-77588-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-77588-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id v5-20020a170902b7c500b001dc4a8b7a98si2298234plz.509.2024.02.22.16.45.44 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Feb 2024 16:45:44 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-77588-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=TLHF0+dY; 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-77588-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-77588-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 639F2283575 for ; Fri, 23 Feb 2024 00:45:44 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id C428412B9E; Fri, 23 Feb 2024 00:43:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="TLHF0+dY" Received: from mail-pf1-f201.google.com (mail-pf1-f201.google.com [209.85.210.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DC94511706 for ; Fri, 23 Feb 2024 00:43:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708648997; cv=none; b=u3+VPBEX/3TOAJxtb2d4TSqFlxXIEYW7IeLejxbnLGqrL+bqJL7KGwUo+iGtxPi1zYBQPgPuiFlkpJLjMc5Z00P6fYTtCqiJrjOf6NuGqyIHxphcNQu8XeoNXqJx1ZUxrFa+DjPcWCxJ8bvIiVHZYyHM0XYqPnJaiiy9i+vXYYU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708648997; c=relaxed/simple; bh=lmObn+99bRFIPg/jYAV295z2lJEvLtzb/rpu32UHeHI=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=rro+XfvARgZtw78pap2n7F2K4uxc3pMi7k0JD7OR6e1rmvaJ6QhOq8mEM1CXoXNNu8miTFFeTj+3to15vPXkmOFAhkmo0+j10lT5eIuISwryhV0yNfni6CtZ51HqgWSFcXAXdxxP1eXIouR9gaB11BtmRbmPGtF2tz1LbgsauNo= 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=TLHF0+dY; arc=none smtp.client-ip=209.85.210.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Received: by mail-pf1-f201.google.com with SMTP id d2e1a72fcca58-6e4cce1df30so150877b3a.2 for ; Thu, 22 Feb 2024 16:43:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1708648995; x=1709253795; 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=5xSck0kmkPmdifT3b5IqGmDzhnUFDNPygC9r66kostc=; b=TLHF0+dY1tlKySZoKw8EtlsyocNA2eHRoxtykNArX4ohS0YBrSeOshMf8QwDEHzRhw 96GawnInAnQH+OQuK4neFpdIitiYrfytJ1fjV799wKbZMjA9iGsN+CJibBU20lyJodkJ WqWah5xB6D71N/vHUa3XKOHl7opo1RbTLEsR1fFTEzUw/03EEdNi3F1dnTkX5/VY4BcQ cdmFX8K+SqJjLLFIc91x1ISVwp8X0SNyK+9Q6TrfcKXbFKRRn0et8hVBKubY3Xv2FwTS TIZjk5QQWpR6iQYjo0RczepaWWp6VmHhoyoMBDildJr6JAV81MpgnWRGy6zpyqPU+mWD IjxQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708648995; x=1709253795; 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=5xSck0kmkPmdifT3b5IqGmDzhnUFDNPygC9r66kostc=; b=kkMPINshM4sxBuW+8cbwLjBotfSkZEdJ3C8SKhJ8S1CS3QBjsMWDh2VerC08etD1vH Tc7KTGKvOM1oAl+qaQpqjbipKoMcrpUeStE+K97lcrpU+jeA6wGWCd8dvKHN8VAIHzqa HqdHobFsCPBlMG7u4841lScS7L/h+6+e86gTN/tqsMaEFqv8z/Ee/eBmHhx3wdGvnCUA 2JcUzEGO46e1DijVuJZpAwRtZ1RlFfozbVH6JDfjJWoproiN9lR4cWITQTgautBXsLrw H2lBtktU7U3THaKJT3R8l97K0KbOybIqTcVDZhLhUsxzM96XOuiPMw+Sviz2BMNK3cPH evIQ== X-Forwarded-Encrypted: i=1; AJvYcCWS6dH+9kj/7hA94oNdTmxmpTiPfwiub7E2btd23O7IsbC+de4zJUGQDQS6YsY8gxKRJv8h9hMqTY8KC7nup3aeUFiJQuX2Kd24D8J9 X-Gm-Message-State: AOJu0Ywuoci7uTccnNqCPQODIXjvvI3ynpgE9L5NzoVv4/zNzIYI9urv 9TJJR4H4BMAX/mQTfelQ7KEd8Ed49ePU//JCzEblZizRKc7jHy5/P18AoxMZmmoYyv6oNuZJikG 9Gg== X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6a00:23c7:b0:6e4:d2ba:b4c4 with SMTP id g7-20020a056a0023c700b006e4d2bab4c4mr40112pfc.3.1708648995172; Thu, 22 Feb 2024 16:43:15 -0800 (PST) Reply-To: Sean Christopherson Date: Thu, 22 Feb 2024 16:42:54 -0800 In-Reply-To: <20240223004258.3104051-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: <20240223004258.3104051-1-seanjc@google.com> X-Mailer: git-send-email 2.44.0.rc0.258.g7320e95886-goog Message-ID: <20240223004258.3104051-8-seanjc@google.com> Subject: [PATCH v9 07/11] 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 , Carlos Bilbao , Peter Gonda , Itaru Kitayama X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1791648485397756136 X-GMAIL-MSGID: 1791648485397756136 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 Tested-by: Carlos Bilbao 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 Fri Feb 23 00:42:55 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 205165 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:a81b:b0:108:e6aa:91d0 with SMTP id bq27csp308665dyb; Thu, 22 Feb 2024 16:48:02 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCVIIMzYCm7z6MQdI0AUmWnlkFAZ7Yp3Keasd+1TBLrel45CM4Bb1ZhUjpdepzy7Nl906rPJ1Aimyd9yxQkXjYucKRzxbA== X-Google-Smtp-Source: AGHT+IFswjWo35wOhceLQXpKf6ZA0Xjs2IK9BeRYL4cp8yJbyLKPA1W6PBDcIgig5xlHIxDV+LW8 X-Received: by 2002:a05:6a20:1441:b0:19e:cf31:6a05 with SMTP id a1-20020a056a20144100b0019ecf316a05mr739506pzi.11.1708649281813; Thu, 22 Feb 2024 16:48:01 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708649281; cv=pass; d=google.com; s=arc-20160816; b=QM6R5rXcTAqprqxgAJApYSl3OyIzTPEt05JvkBPz+ABcr1YBVRu8VVMaswQcxZVrpP NtAi6Uy6Km6MasQIXWpHb71U02Z7npXqKc/W7KBlhLSbV3RSXRPGmTkTVvSiRh2RFYNo +AYJW3s+/t9Kjksbmv9XO3+grN6kxjERBLSqJqlQmSCvnzF+9drL21rumYTZ0TtSLeQD acKmmvV5BIYKf48KIPj2+J1vLvbzyxVz3NRZ2VhKbnVO9jjsK0rF0eYQmEeyx8ftgi0D ZUvJXg6CTz1AlVB5iv4ep1UauCiVN6+R1i2sR/q2aiOTWMhAwqkgXMx8pJNuNhpJBPc4 bdVw== 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=QrUKJwHQx6OZ5c5u7S9Sq3lEDPXCQDh1cyYXS22x5bg=; fh=lkkpqRifTAo6gDCjDjHgV/muL+f68ba+4OVEaLvlEbg=; b=yCrPCEt8igZ6gK6Dupv0WtZy/iZnw0NfX6THzqrcPRpRI7xzWRFGmntFyx/vbBjsJs dmJ+9+iASp2Eu6A//UIsxzcC0F9U+2jBZeu9lCYdHTLlHQOegabppiUyiI6iCOpTA/ku 4yOhtmn6tLfhw14P4AVY3dTQzyD5IS1y8gqC5zuy6xrfLvDfn3EFhaMsLM3AOvFps0xY d1bJ+AX9N2J/o6AfjWZhN4IbD9nJrfhb09kExKvmG6FinQEHnoOhoJmXO1Y0GSKY6fvX L1xb1Fy90aPmDWidffyVzj+FgDWl7FcnNwjmV/Dmw/2QUL14F1HpTid/wtXnNw5J7wro pN5Q==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="F43k/3EB"; 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-77589-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-77589-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id lb12-20020a056a004f0c00b006e376b9203esi9957346pfb.213.2024.02.22.16.48.01 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Feb 2024 16:48:01 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-77589-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="F43k/3EB"; 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-77589-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-77589-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 890FBB230EB for ; Fri, 23 Feb 2024 00:46:19 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 0AA2917560; Fri, 23 Feb 2024 00:43:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="F43k/3EB" Received: from mail-pg1-f202.google.com (mail-pg1-f202.google.com [209.85.215.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 0AE0212B60 for ; Fri, 23 Feb 2024 00:43:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708648999; cv=none; b=ZsDGDEPuG8QZMmQOyXZwq/GjBKJHdph9dnc7PPvAMhrNfDfzzzdDJ4tois7Y+zp14XQFn0ZEw8S6feEfk2VzvSpgKAUse8T9hSJTZcXU71hykdiwVk0lq3gxahM7e6p3uj1LiN82vS+mw9rZ/A9ji+qir4JyhWrsXfz9FPSId0M= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708648999; c=relaxed/simple; bh=3dMbCHQzR52bpjGwYgn8QVji/JNHNDJooagZhaAHL4A=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=IOn/WVMYnlYoq0+W9UeN6vKBzahQNKW5U7ENrmrc6+PA3mxEt89EMHx1pMU7TWqPY5xxS2d33OEWxrcZEwPiI8DLZJB0hejl6V0NxxBHjcBrWXpjyqFwvKBK7xb4wcoH4yQ2zkHf4HRJVI2D3aiaP2/Tym5SYSe0zm3fgcAUvpI= 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=F43k/3EB; arc=none smtp.client-ip=209.85.215.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-pg1-f202.google.com with SMTP id 41be03b00d2f7-5ce63e72bc3so290247a12.0 for ; Thu, 22 Feb 2024 16:43:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1708648997; x=1709253797; 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=QrUKJwHQx6OZ5c5u7S9Sq3lEDPXCQDh1cyYXS22x5bg=; b=F43k/3EB1FyU0k5KVxse9yz12kI+aBYwgTACzj4eXLLnKLAOUDMCiHdffpA5MZ9GEB jJcI5oPGjqMqGWW2T/dtwkxlEKNX35pxU5i6uGVpfD7bXgZhPuIPo7WYo1aEB8zuOqwt v5baZnIB+4GKAIgzyPahht1SPXmQopzHuohtsxaUXD6zOkkJKQugTrNUCHVKriUnQrDN PGAbPEvZEn3GevVqAlZo35dEOOdz1Iw2sJCWNQDz7Yq5NHtJSlN7F3RTnJZD/xmU2avJ wke8iy0HbL/hULQLoEb+dvjC6CNFZ/6INRZMUx01XOJZOD8ww9V4OVJ38NrPHVWpfdii vBeQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708648997; x=1709253797; 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=QrUKJwHQx6OZ5c5u7S9Sq3lEDPXCQDh1cyYXS22x5bg=; b=ljHLFwQC7itn4qeqeKE24v2kIatypP1CVB6zLFdvcqetIt18H8vIQztAPiOjC4+DFm ME40t4evIzOEmCUcMiCh0RhBnJ/Wtk5Yn1ivbvciMRj7MtBJMkjdtTTWPUs+kmaVUb2O TjekWEH05dDCm0AC8Ip5Czr6FyT5qiTa8dvEzHF6MAV7lIcjp8CaoxJql/tmr8M+gVs+ 7Swh40DbPkbYcqvts/RlhqGGzbMXABrgx73rLxOzpqUWjokWSYaG316G+tG8DiDBd3wv XSnaD4ib4PpGNi92wOIKYneVmZwq1n47eDJPFEZso1FcTAszWyp9vN+6MF06RwQs2egh FFrA== X-Forwarded-Encrypted: i=1; AJvYcCUVoc5HETD3wezrD7ShkF8VKUIjvnKR3B0G8ftLYuArCj30dWi9lY3t0eUq/kSijGXhQwigVJrfo+ShOr+RDfniMT0xBaYAy+jkPISK X-Gm-Message-State: AOJu0YxQUysEDoVFKdxb1lgeV5B+uerHBngh8+IkafvxSjUAd+LXSqeH zYwpN0y4SMAFGfNGS6iNQcanVswFhlIg8K3mhXWtk4M5RRq0eSUAyUAksW90PepxfmDuP5JB7UP Lrw== X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6a02:a0e:b0:5dc:85a6:d74d with SMTP id cm14-20020a056a020a0e00b005dc85a6d74dmr962pgb.2.1708648997232; Thu, 22 Feb 2024 16:43:17 -0800 (PST) Reply-To: Sean Christopherson Date: Thu, 22 Feb 2024 16:42:55 -0800 In-Reply-To: <20240223004258.3104051-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: <20240223004258.3104051-1-seanjc@google.com> X-Mailer: git-send-email 2.44.0.rc0.258.g7320e95886-goog Message-ID: <20240223004258.3104051-9-seanjc@google.com> Subject: [PATCH v9 08/11] 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 , Carlos Bilbao , Peter Gonda , Itaru Kitayama X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1791648629538534826 X-GMAIL-MSGID: 1791648629538534826 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 Tested-by: Carlos Bilbao 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 | 105 +++++++++++++++++ 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 | 110 ++++++++++++++++++ 7 files changed, 244 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..de5283bef752 --- /dev/null +++ b/tools/testing/selftests/kvm/include/x86_64/sev.h @@ -0,0 +1,105 @@ +/* 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" + +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) + +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..9f5a3dbb5e65 --- /dev/null +++ b/tools/testing/selftests/kvm/lib/x86_64/sev.c @@ -0,0 +1,110 @@ +// SPDX-License-Identifier: GPL-2.0-only +#define _GNU_SOURCE /* for program_invocation_short_name */ +#include +#include + +#include "sev.h" + +/* + * 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 Fri Feb 23 00:42:56 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 205161 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:a81b:b0:108:e6aa:91d0 with SMTP id bq27csp308171dyb; Thu, 22 Feb 2024 16:46:20 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCV6oSWXluf4dSku28lwrgwqECK3/TqxRPyIfltxA5/nwoucvuf3KHxU683mjjyj/02eE8ZcBTOOlapmjAihUx475+QcHw== X-Google-Smtp-Source: AGHT+IFWz5JVfTytPmKPCSbFNaHIoyAAesK5ktzrjnht1pGek7VJKnvoipFvjVtqfy0EIhURQhVn X-Received: by 2002:a05:6870:414b:b0:21f:bb0:506e with SMTP id r11-20020a056870414b00b0021f0bb0506emr578498oad.42.1708649180635; Thu, 22 Feb 2024 16:46:20 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708649180; cv=pass; d=google.com; s=arc-20160816; b=nHH6bwjRfI7pJf+d2yJfFFE8m0lZGUQ2QaeC7toZ+WJj87UBrZOGZc9HAobSDZtSrH 39MOxRD1fwEjyeBhu2vBBouKeC8uhgXjxwbKdmUD2e8DKgNRsXnI5HO/d2Y5oF0mlCYK TmD0qnSzTn3xUdEWgj/HPkpShCADWkLkMDiZuWrGydn6ehiDSH6yzqqtTEf3IA7NVFXB K7V8e/yt1aW9R8/pDx1W6o+rgiTc1Ize+XuwuFQvMZJyIuxtXEK3atZ8FdrkC0fUEoI3 5jb6Bb3P2SJ7eHKp+8bmjsTp8hKjmSKV91seU5CsNqticm3OALRSXnua8UZtbIjpk60a bOxw== 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=lRpse7Hgpajlk57C2H5vth1Tg0pkPPWc04aEOHzQOwA=; fh=VrNDnL7yfHHULTK68DG6+UrhXoz3/xz2GIJjZHjZS8A=; b=az3AoPCCfUJlgEw3LdDMOnsHeRGSAk9NxeVpgr+9cKee/ajkwRqZ8k+p1lQcEvK7A1 qoMiYxQxvTI9ZvTcQFma/QaHQMlb/U82b6Nl1uLZOXTgcK29CTwBovNv1vemUtrAWS2C zZ0Fi28E+GFBXgNP3sYXTRKR2cVh04X0IWEdM7V9sEvQ839VNH+DN7JbrMIuCY0Z6jVa SCqk1e1iYvhsKtYfZBbeEtPOLEXD2Skf/ZEJJKqver3RHXjtOwaOAHYL1e0hLhqlAARQ UqEgNDye2w/GuYDBkX3kosy1o4XePDqsM5smOwiKT165PqnzaNq1R3fjoWFBm0X47FzN iR+g==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=Z1fqfPJU; 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-77590-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-77590-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id a17-20020a637051000000b005d8bea05154si10989393pgn.619.2024.02.22.16.46.20 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Feb 2024 16:46:20 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-77590-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=Z1fqfPJU; 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-77590-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-77590-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 562C7283C17 for ; Fri, 23 Feb 2024 00:46:20 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 5A1DA1758D; Fri, 23 Feb 2024 00:43:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Z1fqfPJU" Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3D77012E4E for ; Fri, 23 Feb 2024 00:43:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708649001; cv=none; b=XeG3nW7AsOj1a0oBaUjEkECxCLEeYeSgpoP8atdxg3QLm3IX9mDVN9ijVmr3BJUi/X7Wf4Eu643cNlCDp0ztEJDGmPHhMNg3nDt0VKbFxr66Heayyoakxl/+HqOr91cluyxsAjsu/1IAQQWRZbZz295xlbhRX4w30g1NCZAv0zs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708649001; c=relaxed/simple; bh=tyZgfxNkQndxJJOO7ECuOTfeiV340CWZKe8/mOgdQK8=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=SC+vBkJjtM0BFYIdZllebxdfvw/4RwouuyZy1QjaIoOiKY2rrOP1H4KY08joquSsEfw9DpUdZaW520mf+kWCRxX7LAhJwe9YudMpr9ZdgXJkM9ewv66lQ7sgagKA+2S8N3BMNRHuktqwHBUDOHzG+kCB4zh1929ODJFnnk+Yr8A= 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=Z1fqfPJU; arc=none smtp.client-ip=209.85.216.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-299783294a6so299404a91.3 for ; Thu, 22 Feb 2024 16:43:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1708648999; x=1709253799; 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=lRpse7Hgpajlk57C2H5vth1Tg0pkPPWc04aEOHzQOwA=; b=Z1fqfPJUV+2UqYUSJtafJe8vLVB9XizU+aOn9sFXD6S0fm6oaLGS4E1DNI7snLbVs9 8GKrRjRlslYLGAXNMKHL0MeAmThcFnM9ysEI1ICg4MeROyVYJx9uFKIYODGBNq+KiHih S0SxywN7xIQd5JJ32+aNtGdPkWfoc5u9CwHloMZ9CEPiS+YRsXwhMbx2oeg4Es2wsQde yeY9piBAgqRMhK81CGmitNgJbvTiFYEX63ZnjFWPniPd2tCpUlh71hUou7+BUYFHpjc/ 5TMYy6hZY5gJSp5Tmcc9lKwELLwEQAU4yAKKrxJqxTlj+1oKNbxQGjAgtd62YkPOoJ7l PoQA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708648999; x=1709253799; 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=lRpse7Hgpajlk57C2H5vth1Tg0pkPPWc04aEOHzQOwA=; b=OAjZCAh5w18SjFxuPZ291hb8+QLgr12OaxJ8MCu/R1IXNW+DmAMxxvTsI0SF2w8XMa GDXrBa/IwjGh3vDAXYida/05IUucpRcPojQcFHwLtodn8ySEbApff950bzq++x7SXoqR V7FP9KwR/EHg5YFjjLp+8D6+7r5nyLvT9A1EcZW4ztAHbOYtRCrapBS1NZguSQmGVeCl c40AtC77VDhwSddBKIN8qwbZSSdeFv/Zp5jRGpoCp66urwlF2uQP2FgzYenUlE7elSOy D48qZRJGE2JOqzU2co0zhOOs1GOX+KWY+8Lbh0Tih5tAj/Mq58lryrPK6O8ETg4rUsyJ en/Q== X-Forwarded-Encrypted: i=1; AJvYcCXqqjPpRbSuY7Io7FTMZb855KwcZaP2i9OPBHZ2A4JKi+KsiEnspwGEZRfHloUdnOmqDNUZXhunVCiSQ79f4sdSnON1PNVvB3sRefeN X-Gm-Message-State: AOJu0Yxya7146V3rl55XT8Byq6cXdRGo9mIZBuHruq2ZnYrgmfiS7Qt8 bR0E3GT+q+OKtVOXzOh1+87jlCj3kmJ3xhO2kKkVfHf7khZXA5mHcuDRZnC+8+Rk4zwn/uZ7tQq z3w== X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:90b:3d01:b0:299:942e:6634 with SMTP id pt1-20020a17090b3d0100b00299942e6634mr1218pjb.1.1708648999474; Thu, 22 Feb 2024 16:43:19 -0800 (PST) Reply-To: Sean Christopherson Date: Thu, 22 Feb 2024 16:42:56 -0800 In-Reply-To: <20240223004258.3104051-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: <20240223004258.3104051-1-seanjc@google.com> X-Mailer: git-send-email 2.44.0.rc0.258.g7320e95886-goog Message-ID: <20240223004258.3104051-10-seanjc@google.com> Subject: [PATCH v9 09/11] 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 , Carlos Bilbao , Peter Gonda , Itaru Kitayama X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1791648522767445612 X-GMAIL-MSGID: 1791648522767445612 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. Tested-by: Carlos Bilbao 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 Fri Feb 23 00:42:57 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 205162 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:a81b:b0:108:e6aa:91d0 with SMTP id bq27csp308251dyb; Thu, 22 Feb 2024 16:46:38 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCVJyJVdtAwdkeu8LVdWPPxzmvdZV6vYi4A2ZzfWw0W/FuW+g+l0vBMxiJ7WYkQj+Znw2f9sLSLxveXPmuVAAFD9zreESQ== X-Google-Smtp-Source: AGHT+IFP0B9sDVuqKTNYZKhphVWb72S4GqHuf4jzyA5skl/0NzJn5cX1p+KWpfxGwO/UHLjxUR+K X-Received: by 2002:a17:906:1393:b0:a3f:1b3a:1289 with SMTP id f19-20020a170906139300b00a3f1b3a1289mr230315ejc.59.1708649198660; Thu, 22 Feb 2024 16:46:38 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708649198; cv=pass; d=google.com; s=arc-20160816; b=zsnC9JEQZpWQPBBrAQuf2/OFYlZFAwzzFc/0TFXJi6VjWaGviHZQt0+RQ8AxulbV+Y 38vYubXUb6hIPDMqDY/txIrDDMCEBJTamjNpbyCHiPyT3IuNUaII1oAavwbwmULjOrIo /HckoJuJkBUvATIi//E6/fbO3lAGtjId9olwiOYRE1vRd2mzLb5FJQ1++Y1HXnQU5oqc Yu6IQKjEAB3zwXpvpA+nxENAuPfTcIB43WRUoCjg5fe287PKXBFRrZnuLyNXLAqqMp+6 kZO+e7qjpLllRZRwwublYxFiTqE3aSnP+f3Yx3drbKB/FCjYs3lzULtdmZmyIdp5dpPu N5HA== 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=ufdAB/OIgPR6sk1BH6tXU4Lb92iEyKpRUAsMWFh65lo=; fh=x57Njk/8/qNjjgVoglnDyI5N1IE9m6+ijwP/W1Az/1Y=; b=aUdEcx8HLbnEilZMKngmQqlXPNpj5GuxQh7AqgbM6YLqtQDXYFzhWqAD7XtgETa6I2 S7PfpwyxolO4PH+tN8E581dSS1RW5OUjUfk8wdrZ6Hu3SqSvXSDOyjxkl+nn1/rvDwZ2 zYhjI6ehlorDlogF5KSN4gqNgCd5jUPmYtILsoQrKAiWGJS981AQ6G1/EkM7maHTYzAG wzDBuKUysa4Q5xqJlg5mRGG/RuEGsHcQh8AG497ctgdQpjt67DuyUL15hIMxDMtDOHUo Nw4NdamvKZhg6Lw+KRJ7JtCdUG4Gt2nG66U+elIlvcCwMa45zKWnrUItjuSlP8A8HaED 2fNA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=Em36FEgh; 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-77591-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-77591-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id va30-20020a17090711de00b00a3f0b680a63si2896262ejb.48.2024.02.22.16.46.38 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Feb 2024 16:46:38 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-77591-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=Em36FEgh; 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-77591-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-77591-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 F30521F22AE7 for ; Fri, 23 Feb 2024 00:46:37 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id DBB3B18039; Fri, 23 Feb 2024 00:43:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Em36FEgh" 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 10B7713AF9 for ; Fri, 23 Feb 2024 00:43:21 +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=1708649003; cv=none; b=dQQH0acqDRUpNOl1cc+gnEfkBWFPyooOwWhstbSBZPxna/UkwR6a8RHdSFDn6J8E+45cwcCUTNuncjvqr8esexOpj0CDAsKQ1eHfj7GsImP+TW5LxqEHzT1sNvA1Mce4HvvNVpoyOxbmFjMapwoQjnRCvR4pSJO/qu0gJVNDPzY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708649003; c=relaxed/simple; bh=wYG3aBkI4214Z2xd6xHe4uZY8sEWm9j7oKweqpMTgCo=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=CXkJ6dS/AfplNsmwe+6jaapC4EWDj/N9FkBEPkLL74jp8jCRwPPsLqu42vu1r78HRj6bQbc0s6Gg9XDv4dHplCDNnk2Rg6HxOEyotYc3O7FrF9m89ud4ywZba7e05eKfUM2N9LtJDgkPRwxwOT8PbRaqfPpUmpw3TkFEulSvEMY= 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=Em36FEgh; 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-1dbc6ff68ffso2514085ad.1 for ; Thu, 22 Feb 2024 16:43:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1708649001; x=1709253801; 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=ufdAB/OIgPR6sk1BH6tXU4Lb92iEyKpRUAsMWFh65lo=; b=Em36FEgh97nyp76bmotk8WR0avZyxpP/pj5q6LXuqFIyy13uo5sTPBOFJy8Yoivbgm XnVtw53YinNGs/V0PpKvxbhhQlfUvX5ndaOv8cYUraIkVJl8A8vUPRMzF7COJrRwKj0l yOzFbwH47axebAZzevu9Kudd01pSYCwLQgVVBHinKEcciW1d6PpV5V7tlHvd/uoz5Gyt G72ZWP82c7Bm9UMmMGnav+r059S113S7Bpf3fc9+2b6qkIk3nLVhOWQH9cMd5l5IdL2K VultK8yuvFMacqGcZRVLY2w/FEgwWCz7z8Chc2kvzxnBAQUGuJxeg0vOleA7uf/efhiB 3hsw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708649001; x=1709253801; 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=ufdAB/OIgPR6sk1BH6tXU4Lb92iEyKpRUAsMWFh65lo=; b=ZjvHfhyWgQNeXkMSTwygL9jdUeElGjvb1BBKbr352NPwnUYk7lMZcixVkt1GQ2myXi aMOwJ76sHhN7gTK5KLMhHqVyEU2AAnfz2qI7O3qfeVQgHq2mMlBlAA7nqo/7HBwYGhtT BXRFnOfJvPaPxoVmWPtBZ8kQMci7H/87BLX903iaPIeVpPwzZn+eWjJToe25EjrbZfUR tDPaC7akICvwpiPUlIHBzZEIzObvwuY4vKdY8beMiHWEaTSkoKIoavohlWa0x1xxvNXo 0Qy5VNyYVsh913iTD9yWvoGnXRho7nAvSjBzWp0Yd0OKznAWyWtnf3ecpmpPLz05HcUw pJQA== X-Forwarded-Encrypted: i=1; AJvYcCUP8WoZNv8+v4+MxGgy6CttMZjDT5V6fggWg/iDgVHhH/nKjZk3CDj7Z5LtPFbE0aAFBjz16gwI+VAnKI4rDlVsHFiqvGZWVlviLtbA X-Gm-Message-State: AOJu0YxC9Vs0p8I8+dOgp4Z9lrkzEZB5LOYUI7LaMIh3EMZWUS5s+TKb MRJ1Ykr/y1LPrZ88+tmYz+M7468maefyhpyc39NjGOSlAN8B1+AsB25lySqcnoOcXUQj/qLn4+a 29Q== X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:903:234c:b0:1dc:1c81:1b2a with SMTP id c12-20020a170903234c00b001dc1c811b2amr1699plh.1.1708649001454; Thu, 22 Feb 2024 16:43:21 -0800 (PST) Reply-To: Sean Christopherson Date: Thu, 22 Feb 2024 16:42:57 -0800 In-Reply-To: <20240223004258.3104051-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: <20240223004258.3104051-1-seanjc@google.com> X-Mailer: git-send-email 2.44.0.rc0.258.g7320e95886-goog Message-ID: <20240223004258.3104051-11-seanjc@google.com> Subject: [PATCH v9 10/11] 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 , Carlos Bilbao , Peter Gonda , Itaru Kitayama X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1791648542176827420 X-GMAIL-MSGID: 1791648542176827420 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 Tested-by: Carlos Bilbao 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..54d72efd9b4d --- /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(kvm_cpu_has(X86_FEATURE_SEV)); + + test_sev(guest_sev_code, SEV_POLICY_NO_DBG); + test_sev(guest_sev_code, 0); + + return 0; +} From patchwork Fri Feb 23 00:42:58 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 205164 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:a81b:b0:108:e6aa:91d0 with SMTP id bq27csp308322dyb; Thu, 22 Feb 2024 16:46:51 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCWSLvZl5zMpXhaoW0SojIoTYY0PBVTCVMe2x5OS9NK3054dje41EJlwSx+Ai1/Z4+ZEKfSbawceMrKNik1/KLJTInlKPA== X-Google-Smtp-Source: AGHT+IE/h4udW6ab9iOBi/LSffSNB5SGjQmf5IbM4zcWuQ3L2MBGgDYO+j2M0VPiSNlib6dYiGLX X-Received: by 2002:aa7:d692:0:b0:565:9a3:9f44 with SMTP id d18-20020aa7d692000000b0056509a39f44mr570529edr.8.1708649211227; Thu, 22 Feb 2024 16:46:51 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708649211; cv=pass; d=google.com; s=arc-20160816; b=qAcujswdonx1xrubPzgbcO3kCxBibk0Anwj43KIgFMNP68uvXwwG44zIV70HHIyHX2 HGOmdfygnKkplyHybI/8hCjMMvMhX4ohiuPKr1wzcv7cSFUa2C1dSqLYBBhR+FbdCkxb K+GH5w36smvcSg28CAMiDnJrAgdYwxBWqlhUMBxFdlncvKpD9JcPFfIJNU/FKv00kM3/ YMlgGEEi9BOp9yCtSu1unolIyvmPw0VHRundesm3zLjIwHSXOHCZ8yvcnI3tJ28FFk/M UxGjl7M1LROHFZq4YMw05MWbE1IbXYLbGvOLSEpgxsmx6FD8CqzHBZJOicsQFeaa15aU t4Eg== 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=Gc43wBFyEWO0ZO0dpPNQ+d7W+GksBMsxe3wuyqOBBYI=; fh=8gEFShNR74zNfeRd7fLdDhxzq4eCNWl/aVGu059FN+s=; b=x6XMx+kH5y4YYv5eMueFTOGi4ZoYuo/dNf88cEK/6nZHpNQJKKZ7vDUukcDwK7N3ip pYvc354qDyvSYXvHJY2W+QYUjnMH0DN9UKgQW1jR2M2i9Zj8LZlDcOmdwB4KtagnbjHM 15COCUaUO22xJfdFDLrlFhATf3UaNOWzHgLS8bQWCdOos8qcQt434ueiJeW8619hsj6o mxklZo+XmfxSS51Eisbb/UiB7ZlovoKPVWliU+G9Rgeq4CVJ0+S1+Lg6P409QolpRA1M zErHQjS5oOqPTsz1nHHttuXOM07HEA0wv2wp9ynl+YYhgiY4/mw6PTHDg+XzdkNWya0g iMSQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=1WZr5fcJ; 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-77592-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-77592-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id g13-20020a0564021ecd00b005645a231764si4702868edg.138.2024.02.22.16.46.51 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Feb 2024 16:46:51 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-77592-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=1WZr5fcJ; 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-77592-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-77592-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 923C11F21FA3 for ; Fri, 23 Feb 2024 00:46:50 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 5324218E06; Fri, 23 Feb 2024 00:43:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="1WZr5fcJ" Received: from mail-pf1-f201.google.com (mail-pf1-f201.google.com [209.85.210.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 14DDB1643E for ; Fri, 23 Feb 2024 00:43:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708649005; cv=none; b=Vf7chFUyhkkSFLCFyojL2xNW+TPOlwJnTlUwBHldzr22qbMI/u86zCU5/ZlkdIJBC4Nk1acHqm/PHxOkm+pwFMio6PngLiAnOFnb/vOR7APRuHP4oZrlgb5CqAEFLGc8uNwpLk3jLZZDqaOfutU2xWQTV+cyoL3Sz0IlBmCtx8A= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708649005; c=relaxed/simple; bh=IcmW1JJZ2UeOOxj+K1S4spEtX6G5KVw3+hJZz8Cf1dQ=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=I9djLDgeiqEyZDOUVYgbEfsMjy3SwzGTMg9bShpNVGInfvTbVN5u6a3ww4l/aPEdel2bBOxd+p5p6USuIT/8xh3KetheuxzrZUJ1QPWaowmJmHcZsUUAwwAEkd4CDvckos1xpmgdK6J/3UW/4FQMcl6SQcIF0VUA9yhjwyOmPGc= 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=1WZr5fcJ; arc=none smtp.client-ip=209.85.210.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Received: by mail-pf1-f201.google.com with SMTP id d2e1a72fcca58-6e37db1a3a0so226140b3a.3 for ; Thu, 22 Feb 2024 16:43:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1708649003; x=1709253803; 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=Gc43wBFyEWO0ZO0dpPNQ+d7W+GksBMsxe3wuyqOBBYI=; b=1WZr5fcJljD5kVFpHfCf45urVVZ6DrdMukd9tGifpXuZbC1emW1lO0Ho68oHn07hFC FMu6Xl+uog8wlF6XHl84X3am+CstFRbWWpsTpGKLiTAdl2Nf9+odW9zlJiyYP+/OC6PB M2ulsV2IF2numMrrLY4ASiiDFU5Z6MqgGkx+7EoZRriMZ5pXQke7rRXZr5/PdSTNVSTn Xq7TpOFiadoa3Ym6IXIp2Eam5ELOmh4TdtSNMqh7Ha7ELMxNcxV2LzMem6cYPjw4n4uw NyXBj57gi9dbLl+kfVdGemXMdqbJZTsCHrt2x0zdnqq8sYHWnuVERexrK2CGzCcUwQ2d SryQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708649003; x=1709253803; 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=Gc43wBFyEWO0ZO0dpPNQ+d7W+GksBMsxe3wuyqOBBYI=; b=aLMOxsnvuM6aVuo7JjpJFxvQrmYjhvz2SnQMJn6Cu1Bx0UkdgZ0AFQOOUr2j4MkhNP Hbrz6UX24rU3UR9AvL6R5AzAMCCBXS0WuL0DXj1OzxNVNZtqnYYn4R8n7r5uim3MXjV1 skWQJKOCjJ8Jy8uVo6j7TomSILBGwUws1hDv4t+6/e2wLByaj1noCShBvmLiJbGuPB9c nSwiN6NS39F66+BnPoaIiQZQUSi6EKVRvr7GaweuT+s2scAAgX72Hh/MUZT8XO64OMRA ME+e/hQXlO6Et/6yhulB2SAu+1mSPhmrLZriXvmAQNX3LdejKWO6KJ10Dy617AQT69c8 Fo3Q== X-Forwarded-Encrypted: i=1; AJvYcCVGQjo9FezOMnZzyTkfUkU6jVFb6BGr/oOdu9bW39gD9qsy/nsu8zpKLUrHwZXpF1Z7qG0npvTJMhON8V2VVbOW9DJdIG0ikWPZNzK3 X-Gm-Message-State: AOJu0YwkLfKL9EFZyylCwPc5IiJcXFp0nXE4eEXJb21dNZFPdE3i6WhA AqcAevzki2vn24H8vIdIPTvnaoEY8VZyDdQyz2ey2ABYmhnWDaIVpfGKwIMJNxwl3rDPFvjNB0M MSw== X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6a00:188b:b0:6e4:8b79:f5be with SMTP id x11-20020a056a00188b00b006e48b79f5bemr32117pfh.3.1708649003443; Thu, 22 Feb 2024 16:43:23 -0800 (PST) Reply-To: Sean Christopherson Date: Thu, 22 Feb 2024 16:42:58 -0800 In-Reply-To: <20240223004258.3104051-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: <20240223004258.3104051-1-seanjc@google.com> X-Mailer: git-send-email 2.44.0.rc0.258.g7320e95886-goog Message-ID: <20240223004258.3104051-12-seanjc@google.com> Subject: [PATCH v9 11/11] KVM: selftests: Add a basic SEV-ES 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 , Carlos Bilbao , Peter Gonda , Itaru Kitayama X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1791648555235119682 X-GMAIL-MSGID: 1791648555235119682 Extend sev_smoke_test to also run a minimal SEV-ES smoke test so that it's possible to test KVM's unique VMRUN=>#VMEXIT path for SEV-ES guests without needing a full blown SEV-ES capable VM, which requires a rather absurd amount of properly configured collateral. Punt on proper GHCB and ucall support, and instead use the GHCB MSR protocol to signal test completion. The most important thing at this point is to have _any_ kind of testing of KVM's __svm_sev_es_vcpu_run(). Cc: Tom Lendacky Cc: Michael Roth Cc: Peter Gonda Cc: Carlos Bilbao Signed-off-by: Sean Christopherson --- .../selftests/kvm/include/x86_64/sev.h | 2 ++ .../selftests/kvm/lib/x86_64/processor.c | 2 +- tools/testing/selftests/kvm/lib/x86_64/sev.c | 6 +++- .../selftests/kvm/x86_64/sev_smoke_test.c | 30 +++++++++++++++++++ 4 files changed, 38 insertions(+), 2 deletions(-) diff --git a/tools/testing/selftests/kvm/include/x86_64/sev.h b/tools/testing/selftests/kvm/include/x86_64/sev.h index de5283bef752..8a1bf88474c9 100644 --- a/tools/testing/selftests/kvm/include/x86_64/sev.h +++ b/tools/testing/selftests/kvm/include/x86_64/sev.h @@ -25,6 +25,8 @@ enum sev_guest_state { #define SEV_POLICY_NO_DBG (1UL << 0) #define SEV_POLICY_ES (1UL << 2) +#define GHCB_MSR_TERM_REQ 0x100 + 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); diff --git a/tools/testing/selftests/kvm/lib/x86_64/processor.c b/tools/testing/selftests/kvm/lib/x86_64/processor.c index aa92220bf5da..a33289a5b89a 100644 --- a/tools/testing/selftests/kvm/lib/x86_64/processor.c +++ b/tools/testing/selftests/kvm/lib/x86_64/processor.c @@ -1074,7 +1074,7 @@ 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) { + if (vm->subtype == VM_SUBTYPE_SEV || vm->subtype == VM_SUBTYPE_SEV_ES) { vm->arch.c_bit = BIT_ULL(this_cpu_property(X86_PROPERTY_SEV_C_BIT)); vm->gpa_tag_mask = vm->arch.c_bit; } diff --git a/tools/testing/selftests/kvm/lib/x86_64/sev.c b/tools/testing/selftests/kvm/lib/x86_64/sev.c index 9f5a3dbb5e65..e248d3364b9c 100644 --- a/tools/testing/selftests/kvm/lib/x86_64/sev.c +++ b/tools/testing/selftests/kvm/lib/x86_64/sev.c @@ -53,6 +53,9 @@ void sev_vm_launch(struct kvm_vm *vm, uint32_t policy) hash_for_each(vm->regions.slot_hash, ctr, region, slot_node) encrypt_region(vm, region); + if (policy & SEV_POLICY_ES) + vm_sev_ioctl(vm, KVM_SEV_LAUNCH_UPDATE_VMSA, NULL); + vm->arch.is_pt_protected = true; } @@ -90,7 +93,8 @@ struct kvm_vm *vm_sev_create_with_one_vcpu(uint32_t policy, void *guest_code, struct vm_shape shape = { .type = VM_TYPE_DEFAULT, .mode = VM_MODE_DEFAULT, - .subtype = VM_SUBTYPE_SEV, + .subtype = policy & SEV_POLICY_ES ? VM_SUBTYPE_SEV_ES : + VM_SUBTYPE_SEV, }; struct kvm_vm *vm; struct kvm_vcpu *cpus[1]; diff --git a/tools/testing/selftests/kvm/x86_64/sev_smoke_test.c b/tools/testing/selftests/kvm/x86_64/sev_smoke_test.c index 54d72efd9b4d..026779f3ed06 100644 --- a/tools/testing/selftests/kvm/x86_64/sev_smoke_test.c +++ b/tools/testing/selftests/kvm/x86_64/sev_smoke_test.c @@ -12,6 +12,21 @@ #include "linux/psp-sev.h" #include "sev.h" + +static void guest_sev_es_code(void) +{ + /* TODO: Check CPUID after GHCB-based hypercall support is added. */ + GUEST_ASSERT(rdmsr(MSR_AMD64_SEV) & MSR_AMD64_SEV_ENABLED); + GUEST_ASSERT(rdmsr(MSR_AMD64_SEV) & MSR_AMD64_SEV_ES_ENABLED); + + /* + * TODO: Add GHCB and ucall support for SEV-ES guests. For now, simply + * force "termination" to signal "done" via the GHCB MSR protocol. + */ + wrmsr(MSR_AMD64_SEV_ES_GHCB, GHCB_MSR_TERM_REQ); + __asm__ __volatile__("rep; vmmcall"); +} + static void guest_sev_code(void) { GUEST_ASSERT(this_cpu_has(X86_FEATURE_SEV)); @@ -31,6 +46,16 @@ static void test_sev(void *guest_code, uint64_t policy) for (;;) { vcpu_run(vcpu); + if (policy & SEV_POLICY_ES) { + TEST_ASSERT(vcpu->run->exit_reason == KVM_EXIT_SYSTEM_EVENT, + "Wanted SYSTEM_EVENT, got %s", + exit_reason_str(vcpu->run->exit_reason)); + TEST_ASSERT_EQ(vcpu->run->system_event.type, KVM_SYSTEM_EVENT_SEV_TERM); + TEST_ASSERT_EQ(vcpu->run->system_event.ndata, 1); + TEST_ASSERT_EQ(vcpu->run->system_event.data[0], GHCB_MSR_TERM_REQ); + break; + } + switch (get_ucall(vcpu, &uc)) { case UCALL_SYNC: continue; @@ -54,5 +79,10 @@ int main(int argc, char *argv[]) test_sev(guest_sev_code, SEV_POLICY_NO_DBG); test_sev(guest_sev_code, 0); + if (kvm_cpu_has(X86_FEATURE_SEV_ES)) { + test_sev(guest_sev_es_code, SEV_POLICY_ES | SEV_POLICY_NO_DBG); + test_sev(guest_sev_es_code, SEV_POLICY_ES); + } + return 0; }