From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id EF095C47DD9 for ; Fri, 22 Mar 2024 18:12:37 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rnjN9-0006KP-Ck; Fri, 22 Mar 2024 14:12:07 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rnjN7-0006KE-Jt for qemu-devel@nongnu.org; Fri, 22 Mar 2024 14:12:05 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rnjN6-000726-4U for qemu-devel@nongnu.org; Fri, 22 Mar 2024 14:12:05 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1711131123; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ybGTeJ15j23uaLcwCaC0bXlA3wGJbYUuGHTm8RBxdns=; b=T2zRmUyHA3VUhzmyDrWZzONgcezAbWCxYpL68N5YiNZ90W/JLcDqCmwx/My2Vg51f1s4Hk 8wZG56xkKO5wKAarq0XKdMYTamCi69e4peEHzIPRH6VVep6P34SWdza3t29aPnY0uYzPpx IESpdxTiHAnk2rcRPxV+fkkfd+VSOUw= Received: from mail-ed1-f72.google.com (mail-ed1-f72.google.com [209.85.208.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-596-x0NdHLI-N42n0hB3yKGLTA-1; Fri, 22 Mar 2024 14:12:00 -0400 X-MC-Unique: x0NdHLI-N42n0hB3yKGLTA-1 Received: by mail-ed1-f72.google.com with SMTP id 4fb4d7f45d1cf-56b829a3b41so1372145a12.3 for ; Fri, 22 Mar 2024 11:12:00 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711131119; x=1711735919; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ybGTeJ15j23uaLcwCaC0bXlA3wGJbYUuGHTm8RBxdns=; b=jUxjfneoawOupOKF5UxfLhyiy2z0LuqDCJawNHA9tU3WgB35XlADBZYn1nNVA2rBEt rFeR+bUkyjh4FbvV3RNpw+M4ewYZno8iBjRPsc32uvNfh7Zx9As/AQMPGNyBAaJeOJCT 5I+J1DJCSVD5AMZ2/cSzM3SvmGep2v6thf2Y40vjavxaA5yPzRVW1gKAA/4A141XItpM 3Ng9Pi0GdU8wFXV7Quw7x7zmOP+lBqpJf8R9ziwiB0jaNm1HDlW0YXG2W77Ylvae0ZjQ wWcHkodO+ie+ZehTinXm0Oh8T8kXEM2LK3HP0PH4qIj/ZzIdvGQZtHqQ0L+7Emz6gOT+ +zyA== X-Gm-Message-State: AOJu0Yz8xFh4WRHXBuEWEk3NyHC0gsvf1nGVOjQjHt+f+qoPjpGcCYZD nGgo/SKLnDjaXUugxJok0Wm9yTCv/0fiaNYFYVlFxlGBjS6TN4ZBU8Bc33LEHwayyF0OLhz5A0W KpHtydMu7TIhDRxmwDNjmAongN11TIOOQjDgfjuO7y6kMByRYrVjMePP3z9u4Jfuvksb25rjsD7 //HndFfZ4Rcbr/SJd5fgEJ6EUauB0nCGfhJCgt X-Received: by 2002:a50:d594:0:b0:56b:902e:b144 with SMTP id v20-20020a50d594000000b0056b902eb144mr192619edi.12.1711131118930; Fri, 22 Mar 2024 11:11:58 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGKOHI4Z9v+6MSVTyAgMg95WsgYfetbNPWZowO9qwc+UeYAk0Gh4Y2YLlm2Ospf8Dcyb1c7bw== X-Received: by 2002:a50:d594:0:b0:56b:902e:b144 with SMTP id v20-20020a50d594000000b0056b902eb144mr192607edi.12.1711131118656; Fri, 22 Mar 2024 11:11:58 -0700 (PDT) Received: from [192.168.10.118] ([151.95.49.219]) by smtp.gmail.com with ESMTPSA id h26-20020a50ed9a000000b005689baaae61sm74449edr.26.2024.03.22.11.11.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 22 Mar 2024 11:11:56 -0700 (PDT) From: Paolo Bonzini To: qemu-devel@nongnu.org Cc: xiaoyao.li@intel.com, michael.roth@amd.com, david@redhat.com Subject: [PATCH 12/26] KVM: track whether guest state is encrypted Date: Fri, 22 Mar 2024 19:11:02 +0100 Message-ID: <20240322181116.1228416-13-pbonzini@redhat.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240322181116.1228416-1-pbonzini@redhat.com> References: <20240322181116.1228416-1-pbonzini@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Received-SPF: pass client-ip=170.10.133.124; envelope-from=pbonzini@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -22 X-Spam_score: -2.3 X-Spam_bar: -- X-Spam_report: (-2.3 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.222, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org So far, KVM has allowed KVM_GET/SET_* ioctls to execute even if the guest state is encrypted, in which case they do nothing. For the new API using VM types, instead, the ioctls will fail which is a safer and more robust approach. The new API will be the only one available for SEV-SNP and TDX, but it is also usable for SEV and SEV-ES. In preparation for that, require architecture-specific KVM code to communicate the point at which guest state is protected (which must be after kvm_cpu_synchronize_post_init(), though that might change in the future in order to suppor migration). >From that point, skip reading registers so that cpu->vcpu_dirty is never true: if it ever becomes true, kvm_arch_put_registers() will fail miserably. Signed-off-by: Paolo Bonzini --- include/sysemu/kvm.h | 2 ++ include/sysemu/kvm_int.h | 1 + accel/kvm/kvm-all.c | 14 ++++++++++++-- target/i386/sev.c | 1 + 4 files changed, 16 insertions(+), 2 deletions(-) diff --git a/include/sysemu/kvm.h b/include/sysemu/kvm.h index fad9a7e8ff3..302e8f6f1e5 100644 --- a/include/sysemu/kvm.h +++ b/include/sysemu/kvm.h @@ -539,6 +539,8 @@ bool kvm_dirty_ring_enabled(void); uint32_t kvm_dirty_ring_size(void); +void kvm_mark_guest_state_protected(void); + /** * kvm_hwpoisoned_mem - indicate if there is any hwpoisoned page * reported for the VM. diff --git a/include/sysemu/kvm_int.h b/include/sysemu/kvm_int.h index 882e37e12c5..3496be7997a 100644 --- a/include/sysemu/kvm_int.h +++ b/include/sysemu/kvm_int.h @@ -87,6 +87,7 @@ struct KVMState bool kernel_irqchip_required; OnOffAuto kernel_irqchip_split; bool sync_mmu; + bool guest_state_protected; uint64_t manual_dirty_log_protect; /* The man page (and posix) say ioctl numbers are signed int, but * they're not. Linux, glibc and *BSD all treat ioctl numbers as diff --git a/accel/kvm/kvm-all.c b/accel/kvm/kvm-all.c index a8cecd040eb..05fa3533c66 100644 --- a/accel/kvm/kvm-all.c +++ b/accel/kvm/kvm-all.c @@ -2698,7 +2698,7 @@ bool kvm_cpu_check_are_resettable(void) static void do_kvm_cpu_synchronize_state(CPUState *cpu, run_on_cpu_data arg) { - if (!cpu->vcpu_dirty) { + if (!cpu->vcpu_dirty && !kvm_state->guest_state_protected) { int ret = kvm_arch_get_registers(cpu); if (ret) { error_report("Failed to get registers: %s", strerror(-ret)); @@ -2712,7 +2712,7 @@ static void do_kvm_cpu_synchronize_state(CPUState *cpu, run_on_cpu_data arg) void kvm_cpu_synchronize_state(CPUState *cpu) { - if (!cpu->vcpu_dirty) { + if (!cpu->vcpu_dirty && !kvm_state->guest_state_protected) { run_on_cpu(cpu, do_kvm_cpu_synchronize_state, RUN_ON_CPU_NULL); } } @@ -2747,6 +2747,11 @@ static void do_kvm_cpu_synchronize_post_init(CPUState *cpu, run_on_cpu_data arg) void kvm_cpu_synchronize_post_init(CPUState *cpu) { + /* + * This runs before the machine_init_done notifiers, and is the last + * opportunity to synchronize the state of confidential guests. + */ + assert(!kvm_state->guest_state_protected); run_on_cpu(cpu, do_kvm_cpu_synchronize_post_init, RUN_ON_CPU_NULL); } @@ -4094,3 +4099,8 @@ void query_stats_schemas_cb(StatsSchemaList **result, Error **errp) query_stats_schema_vcpu(first_cpu, &stats_args); } } + +void kvm_mark_guest_state_protected(void) +{ + kvm_state->guest_state_protected = true; +} diff --git a/target/i386/sev.c b/target/i386/sev.c index b8f79d34d19..c49a8fd55eb 100644 --- a/target/i386/sev.c +++ b/target/i386/sev.c @@ -755,6 +755,7 @@ sev_launch_get_measure(Notifier *notifier, void *unused) if (ret) { exit(1); } + kvm_mark_guest_state_protected(); } /* query the measurement blob length */ -- 2.44.0