From nobody Sun Feb 8 06:14:00 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7BBA821C16F for ; Fri, 2 Aug 2024 15:16:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722611785; cv=none; b=o/as8+uUlhfwKMsfd3RIPGrSa6xUMohSagaKcNziaUdrJtQPfdm2zZhV/dcE8/VsS2Le4o+mfLrQzqqm4IfCJe6RO46FQ+5U723dcZdeAACWIIFDzF4KWv45bNkPUqoqoLIlgYGDS8tSoy3Qq2Zfj/F5IDoWkPls+oMTQ1iOX60= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722611785; c=relaxed/simple; bh=7RzWm4TVAza6SKjenqIEuJ97Sb8DDWQkvSq/IOSwezw=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=iG+xZjH28Db6O8TpYs1ipFq6c9ZKSPS6w5J77/ctUUgu/tTr51zH/MRW8cpxM98Y6G3628QVnYCA1wCXzfu6XV0LUpCkTWj+t35GAZFbgsUc8E4cPdbfkUmCEL4hkAvZivDQKw+C8HlOOacv9NMhR5JgUPzgzz6XsMFJZ0Fl5YM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=WkwNOPEE; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="WkwNOPEE" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1722611782; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ikT1XcHCFVzQ6kf/Q0zvP0r3BIvuRrjrVEasPutpHds=; b=WkwNOPEEt0fqI/vt6P2GVHjLqRr90k/tquazGDyav5ZniYINIr2PW3MZQJ1xwX8X+xBFKB 0G5ApyGAnAhnCcFmKi4Afm+/2QkiJkYGQZfDGAcQlEw9R6bueMmKC9puJ4SM90SJuW65qy uEwW7k1wk4Zty+7QODpmD4tspU4CNRQ= Received: from mx-prod-mc-02.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-32-a903dtmwMDuLrwaGtfwyTQ-1; Fri, 02 Aug 2024 11:16:21 -0400 X-MC-Unique: a903dtmwMDuLrwaGtfwyTQ-1 Received: from mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.17]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 2CC9B1955D53; Fri, 2 Aug 2024 15:16:19 +0000 (UTC) Received: from intellaptop.redhat.com (unknown [10.47.238.37]) by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 94D441955E8C; Fri, 2 Aug 2024 15:16:14 +0000 (UTC) From: Maxim Levitsky To: kvm@vger.kernel.org Cc: Sean Christopherson , linux-kernel@vger.kernel.org, Borislav Petkov , "H. Peter Anvin" , Paolo Bonzini , Ingo Molnar , x86@kernel.org, Thomas Gleixner , Dave Hansen , Maxim Levitsky , Chao Gao Subject: [PATCH v2 1/2] KVM: x86: relax canonical check for some x86 architectural msrs Date: Fri, 2 Aug 2024 18:16:07 +0300 Message-Id: <20240802151608.72896-2-mlevitsk@redhat.com> In-Reply-To: <20240802151608.72896-1-mlevitsk@redhat.com> References: <20240802151608.72896-1-mlevitsk@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.0 on 10.30.177.17 Content-Type: text/plain; charset="utf-8" Several architectural msrs (e.g MSR_KERNEL_GS_BASE) must contain a canonical address, and according to Intel PRM, this is enforced by a #GP canonical check during MSR write. However as it turns out, the supported address width used for this canonical check is determined only by host cpu model: if CPU *supports* 5 level paging, the width will be 57 regardless of the state of CR4.LA57. Experemental tests on a Sapphire Rapids CPU and on a Zen4 CPU confirm this behavior. In addition to that, the Intel ISA extension manual mentions that this might be the architectural behavior: Architecture Instruction Set Extensions and Future Features Programming Ref= erence [1]. Chapter 6.4: "CANONICALITY CHECKING FOR DATA ADDRESSES WRITTEN TO CONTROL REGISTERS AND MSRS" "In Processors that support LAM continue to require the addresses written to control registers or MSRs to be 57-bit canonical if the processor _supports_ 5-level paging or 48-bit canonical if it supports only 4-level paging" [1]: https://cdrdv2.intel.com/v1/dl/getContent/671368 Suggested-by: Chao Gao Signed-off-by: Maxim Levitsky --- arch/x86/kvm/x86.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index a6968eadd418..3582f0bb7644 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -1844,7 +1844,16 @@ static int __kvm_set_msr(struct kvm_vcpu *vcpu, u32 = index, u64 data, case MSR_KERNEL_GS_BASE: case MSR_CSTAR: case MSR_LSTAR: - if (is_noncanonical_address(data, vcpu)) + + /* + * Both AMD and Intel cpus allow values which + * are canonical in the 5 level paging mode but are not + * canonical in the 4 level paging mode to be written + * to the above MSRs, as long as the host CPU supports + * 5 level paging, regardless of the state of the CR4.LA57. + */ + if (!__is_canonical_address(data, + kvm_cpu_cap_has(X86_FEATURE_LA57) ? 57 : 48)) return 1; break; case MSR_IA32_SYSENTER_EIP: --=20 2.40.1 From nobody Sun Feb 8 06:14:00 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 419B73DABE7 for ; Fri, 2 Aug 2024 15:16:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722611791; cv=none; b=hK3mDyAX0wJUQhMPsUh6MisHbeOP3K8jrmy/C1u5SP6Q9+yHony3IQ4i8GWfpHRE1RmTFzcFnhYJlmtE5E0Gy4qbnN47f7aMYFtrPbrMLKMLN2NwYyds132SXSSVhcIE5tmk4izTbeC39JmW0u7UEj3uGUFtILgwnX20Zm87/8o= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722611791; c=relaxed/simple; bh=Er46L1hp81+g7ncccocxo1kW4uaMS9G+kd29B0+gGLo=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=eBsJrx9tAdMBC6ZzAwHgy84/VYEehIMHMsdSXQjV7dy2ScVMdCqIUWGVUNmcgYTf1Tzuega2a72S0kuycTtKiNlOcU3+TKo9O7HzaGsVEzLYngt5e4wIqlTtOXFEBxzbHDTX5/6JVh8sEYmlNXcNjI46Ul4yMX++bpj35A//oW4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=M+i0tuSC; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="M+i0tuSC" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1722611789; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=TbSpH0vyde4k53/5J0gdQAsE/z1IiL2N27dmroNOMqk=; b=M+i0tuSCDG/VR+M9JW+MDCWwNEcmiVzIHhUdTLJ8KHBpAWFLN+vD326gggr4vhpb+nJtvc 91h2ZIPU7tD97WCeL/At7Q7HfHDsQuaJ4HWli+PGkybzKqu1dmhh+LSGzf40Q+l6JipyMO 70EKtXrJODKnOPJlkn1gzIMb/Wbr7lo= Received: from mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-246-Tt5guUWeOfOm2mobmd6YEA-1; Fri, 02 Aug 2024 11:16:26 -0400 X-MC-Unique: Tt5guUWeOfOm2mobmd6YEA-1 Received: from mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.17]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 84A6A1955EAA; Fri, 2 Aug 2024 15:16:23 +0000 (UTC) Received: from intellaptop.redhat.com (unknown [10.47.238.37]) by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 722C01955E80; Fri, 2 Aug 2024 15:16:19 +0000 (UTC) From: Maxim Levitsky To: kvm@vger.kernel.org Cc: Sean Christopherson , linux-kernel@vger.kernel.org, Borislav Petkov , "H. Peter Anvin" , Paolo Bonzini , Ingo Molnar , x86@kernel.org, Thomas Gleixner , Dave Hansen , Maxim Levitsky Subject: [PATCH v2 2/2] KVM: SVM: fix emulation of msr reads/writes of MSR_FS_BASE and MSR_GS_BASE Date: Fri, 2 Aug 2024 18:16:08 +0300 Message-Id: <20240802151608.72896-3-mlevitsk@redhat.com> In-Reply-To: <20240802151608.72896-1-mlevitsk@redhat.com> References: <20240802151608.72896-1-mlevitsk@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.0 on 10.30.177.17 Content-Type: text/plain; charset="utf-8" If these msrs are read by the emulator (e.g due to 'force emulation' prefix= ), SVM code currently fails to extract the corresponding segment bases, and return them to the emulator. Fix that. Signed-off-by: Maxim Levitsky --- arch/x86/kvm/svm/svm.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index c58da281f14f..3fc01ba2bd4a 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -2875,6 +2875,12 @@ static int svm_get_msr(struct kvm_vcpu *vcpu, struct= msr_data *msr_info) case MSR_CSTAR: msr_info->data =3D svm->vmcb01.ptr->save.cstar; break; + case MSR_GS_BASE: + msr_info->data =3D svm->vmcb01.ptr->save.gs.base; + break; + case MSR_FS_BASE: + msr_info->data =3D svm->vmcb01.ptr->save.fs.base; + break; case MSR_KERNEL_GS_BASE: msr_info->data =3D svm->vmcb01.ptr->save.kernel_gs_base; break; @@ -3100,6 +3106,12 @@ static int svm_set_msr(struct kvm_vcpu *vcpu, struct= msr_data *msr) case MSR_CSTAR: svm->vmcb01.ptr->save.cstar =3D data; break; + case MSR_GS_BASE: + svm->vmcb01.ptr->save.gs.base =3D data; + break; + case MSR_FS_BASE: + svm->vmcb01.ptr->save.fs.base =3D data; + break; case MSR_KERNEL_GS_BASE: svm->vmcb01.ptr->save.kernel_gs_base =3D data; break; --=20 2.40.1