Received: by 2002:ab2:60d1:0:b0:1f7:5705:b850 with SMTP id i17csp774878lqm; Wed, 1 May 2024 15:50:31 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXRH6f2MRdJoQNLFgZTuo4lTRPEOr6ZDzWqGYkHUZqTW4XL2H/E4ffPwpYk0Oc+BM6RLMLlyzcB8HOoRCAX6K3H5LGD7Dlm3wdgdcFnWQ== X-Google-Smtp-Source: AGHT+IEp9Y7gxYwiUFUP02RCK4+Q2nqqYqMw8HZhESOo0G+0JY2ORyrqfFjG/D9o197NZYFX6D6F X-Received: by 2002:a2e:9e52:0:b0:2e0:c6ec:bcc2 with SMTP id g18-20020a2e9e52000000b002e0c6ecbcc2mr2261565ljk.33.1714603829769; Wed, 01 May 2024 15:50:29 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1714603829; cv=pass; d=google.com; s=arc-20160816; b=FybjXRx7/ptyb+9bO3kJZCAAFSsc3e8Ist0IUZmDW+9rrr1L9QVWgw1mkhx5Aw4JZk gUYSDdZiMpk5CGCSk4eJQiQkkeX3PZccgOo4pytFgjNfcd2t1RhH/wMlX3ZCDWAot1nz Rj/vUNCextTZoDry8NrIR2BpYUAxaNYbcGd9tOSrrSvImgSFIfgNSW3ySSHfFdGxsKkq 46qRUQZg4bCUq7g0fRoKfjf096jmXdV5c5kp5qBitNMgr3TCr5mEk25hbXKsDFkLRGo4 JJc/3fhC8nbwQemZDO2Q0Pj8+V3pFF85Z/V8Z8GUk10F5GrwguNPopwlxSWZZdl0YSVF CoXA== 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 :dkim-signature; bh=1P/Mv3chyaRaiyKE52xANGAdVdVRLjPvU/jVzwzR+Xg=; fh=+oQwrBEXsCXLEMpZ6rQmnSBfVnoA/VEr2k7eOFv4Q38=; b=Tq8eZnk05jxsnPyhtWRqaa+ifgd3VQNQMZ4ikH0CjLs5vAwUqgAGrrlWBHMwOB2VHL HWKXgJpBSe9a2UgyhpA41KGBqEFqfs+H8+OwyYb774qNodykQ4dWLEP7GL4OQJQyQdIr 6PPoWxs0FX334VvoRIj9zjRPXM0+anr6T5TC8894C0fDTMiZpAr8lvDUwx/EO6knTCLS 8p8BTpkmVNRduJLeY20KWHtIDPZhvSiPvO8MqTqoVLu+vBLLZahGsmxWO942xnBwhZGl 9UgOBkCCEyLc3S7YzqRvZ4D/RdYEvH3qx2r5JaxWwanFGVWUqMJX+fUtaIF8TCqkVXSu M9qQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="kRUKb/kc"; 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-165796-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-165796-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id e26-20020a50d4da000000b005729db4cadfsi1945726edj.440.2024.05.01.15.50.29 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 May 2024 15:50:29 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-165796-linux.lists.archive=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="kRUKb/kc"; 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-165796-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-165796-linux.lists.archive=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 42BF21F218FF for ; Wed, 1 May 2024 22:50:29 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id F007F168AE4; Wed, 1 May 2024 22:50:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="kRUKb/kc" Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7B9CD2EB11 for ; Wed, 1 May 2024 22:50:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714603817; cv=none; b=pmfUGVuRZSItox9+ztLJFsKXpxYP5gkHvCMgVNTF/zfHwOhM67boxIH5TvsbE0XkbP9ehId5gujIFwaIuCKHJ6mogJDnt15wanKbNoGLW+lPFBvFlmXQHoxyaiQrDrYZW0ntkmJNGiRGyQjrbZkMuBfmg5iBEMUQQL6fwt+1H/w= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714603817; c=relaxed/simple; bh=YNNf0XLM+4ChwsAjeHwf7iXcDfwaWcrjgejEJs7VaII=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=UMoW+7qCMpvgexd8E52ysFqrjEqCHma30uCcCa3PwP9jbuFafiPmcspUpruIo/fDQ++uPkT3Ul3oOCFtAIpJ4ivf9FjqCpkngamAfUJ3tO1yMqAsjgWxtYgKe+aTFhkV2uohu86WrQGCqY0v9bxWAwQkaPFBfnlN8wnJ7Vq8m74= 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=kRUKb/kc; arc=none smtp.client-ip=209.85.128.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-61be452c62bso52768377b3.2 for ; Wed, 01 May 2024 15:50:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1714603813; x=1715208613; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=1P/Mv3chyaRaiyKE52xANGAdVdVRLjPvU/jVzwzR+Xg=; b=kRUKb/kcIS6ExXDqVIfmC8J5dFc9ZnH2bTKtfdDklUQFp8+UZrkhyAQBUW6I8pIjDK iGnSUY91tdSuEwsfELYmKwtjEQ3mB1q35MRLjedsHzvB/s88mgBiHYS7ikBTr7/g6bXo BgBtyYwsDbuVSw7y4vNzB0GdyCJSDrLv4zJ41/bYhxAU/UUtASo+/RMLjDIAAzzQvgts q/eIE8tRHExiVHo59CPgzKoyxtLK8ZvmpfJhhL4Gmsyqy4kx44c99MlO69NHFvGOJhrn b6pCCcDcpVSr0cUvymVl6qBDtzOAFT5SVdPRiS5JuuLB/H0Gukvs6zj08gooMu9tc6u+ JcMw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714603813; x=1715208613; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=1P/Mv3chyaRaiyKE52xANGAdVdVRLjPvU/jVzwzR+Xg=; b=tKHXtDSqhAsPuS9aKWYcicYEfI10pt1fuyGwJ2AlpIuRssD0+yjW2o6PHLwJVJCcx0 PwCgmg3oPCJYNGlZDfcRoSpc5ebqdQkBhZF1Eiu5QWzwLC3HrG724hS09Uh2RkX068Mk HyQiZlfhk6uq9vlj0U76ZLTR6DlO18tR2s7G2f+vjuWVbLz7PU6wa0aYL23NS6gPhccB copHDDZgmAFTlX/ZSrHUT9zZd96Q1GcFHmFwj+ng23mMsTuPfu5xf+p6Cr19Qb3hKuIR daO6XzbESsLDKGKzcNpCY9KsCiyNiBr0b+WCp1WI8w805E1W7XQxrfR1AyhHResTQQWr qhAA== X-Forwarded-Encrypted: i=1; AJvYcCXYd3SiSKubXszoJDjvQGt9HTpFzRG+dwnW8q2QQdWUw4HoxT3KBiJmC+N5bQbagO5V4y/3GaSx79m7nqpLuy19Z74APIGlSRtTMCof X-Gm-Message-State: AOJu0YzAg44S20mUZVEp+DGXQOfsKWaUUI5VHE/JgK2ikcaTG6GNAJJh 52zhiEIbkRBZRr0hc2hkCQ0aMygNTo1+SkCNRRGSkeLXDuh579c4mkS9pOanHgOmqtNcBH/mV5N G5Q== X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6902:1243:b0:dc6:b813:5813 with SMTP id t3-20020a056902124300b00dc6b8135813mr321284ybu.9.1714603813461; Wed, 01 May 2024 15:50:13 -0700 (PDT) Date: Wed, 1 May 2024 15:50:11 -0700 In-Reply-To: <20240219074733.122080-22-weijiang.yang@intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240219074733.122080-1-weijiang.yang@intel.com> <20240219074733.122080-22-weijiang.yang@intel.com> Message-ID: Subject: Re: [PATCH v10 21/27] KVM: x86: Save and reload SSP to/from SMRAM From: Sean Christopherson To: Yang Weijiang Cc: pbonzini@redhat.com, dave.hansen@intel.com, x86@kernel.org, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, peterz@infradead.org, chao.gao@intel.com, rick.p.edgecombe@intel.com, mlevitsk@redhat.com, john.allen@amd.com Content-Type: text/plain; charset="us-ascii" On Sun, Feb 18, 2024, Yang Weijiang wrote: > Save CET SSP to SMRAM on SMI and reload it on RSM. KVM emulates HW arch > behavior when guest enters/leaves SMM mode,i.e., save registers to SMRAM > at the entry of SMM and reload them at the exit to SMM. Per SDM, SSP is > one of such registers on 64-bit Arch, and add the support for SSP. Note, > on 32-bit Arch, SSP is not defined in SMRAM, so fail 32-bit CET guest > launch. > > Suggested-by: Sean Christopherson > Signed-off-by: Yang Weijiang > Reviewed-by: Maxim Levitsky > --- > arch/x86/kvm/cpuid.c | 11 +++++++++++ > arch/x86/kvm/smm.c | 8 ++++++++ > arch/x86/kvm/smm.h | 2 +- > 3 files changed, 20 insertions(+), 1 deletion(-) > > diff --git a/arch/x86/kvm/cpuid.c b/arch/x86/kvm/cpuid.c > index 2bb1931103ad..c0e13040e35b 100644 > --- a/arch/x86/kvm/cpuid.c > +++ b/arch/x86/kvm/cpuid.c > @@ -149,6 +149,17 @@ static int kvm_check_cpuid(struct kvm_vcpu *vcpu, > if (vaddr_bits != 48 && vaddr_bits != 57 && vaddr_bits != 0) > return -EINVAL; > } > + /* > + * Prevent 32-bit guest launch if shadow stack is exposed as SSP > + * state is not defined for 32-bit SMRAM. Why? Lack of save/restore for SSP on 32-bit guests is a gap in Intel's architecture, I don't see why KVM should diverge from hardware. I.e. just do nothing for SSP on SMI/RSM, because that's exactly what the architecture says will happen. > + */ > + best = cpuid_entry2_find(entries, nent, 0x80000001, > + KVM_CPUID_INDEX_NOT_SIGNIFICANT); > + if (best && !(best->edx & F(LM))) { > + best = cpuid_entry2_find(entries, nent, 0x7, 0); > + if (best && (best->ecx & F(SHSTK))) > + return -EINVAL; > + } > > /* > * Exposing dynamic xfeatures to the guest requires additional > diff --git a/arch/x86/kvm/smm.c b/arch/x86/kvm/smm.c > index 45c855389ea7..7aac9c54c353 100644 > --- a/arch/x86/kvm/smm.c > +++ b/arch/x86/kvm/smm.c > @@ -275,6 +275,10 @@ static void enter_smm_save_state_64(struct kvm_vcpu *vcpu, > enter_smm_save_seg_64(vcpu, &smram->gs, VCPU_SREG_GS); > > smram->int_shadow = static_call(kvm_x86_get_interrupt_shadow)(vcpu); > + > + if (guest_can_use(vcpu, X86_FEATURE_SHSTK)) > + KVM_BUG_ON(kvm_msr_read(vcpu, MSR_KVM_SSP, &smram->ssp), > + vcpu->kvm); > } > #endif > > @@ -564,6 +568,10 @@ static int rsm_load_state_64(struct x86_emulate_ctxt *ctxt, > static_call(kvm_x86_set_interrupt_shadow)(vcpu, 0); > ctxt->interruptibility = (u8)smstate->int_shadow; > > + if (guest_can_use(vcpu, X86_FEATURE_SHSTK)) > + KVM_BUG_ON(kvm_msr_write(vcpu, MSR_KVM_SSP, smstate->ssp), > + vcpu->kvm); This should synthesize triple-fault, not WARN and kill the VM, as the value to be restored is guest controlled (the guest can scribble SMRAM from within the SMI handler). At that point, I would just synthesize triple-fault for the read path too.