Skip to contentSkip to conversation

LASS KVM virtualization support

PATCH Superseded Zeng Guang 6 participants 36 messages Jun 1, 2023 – Aug 16, 2023

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Zeng Guang

author
#0 lore raw

cover LASS KVM virtualization support

Linear Address Space Separation (LASS)[1] is an independent mechanism that enforces the mode-based protections on any access to a linear address.

Based on a linear-address organization, the 64-bit canonical linear address space is partitioned into two halves: all linear addresses whose most significant bit is 0 are user space addresses, while linear addresses whose most significant bit is 1 are supervisor space address.

LASS aims to prevent any attempt to probe supervisor space addresses by user mode, and likewise stop any attempt to access (if SMAP enabled) or execute user space addresses from supervisor mode.

When platform has LASS capability, KVM requires to expose this feature to guest VM enumerated by CPUID.(EAX=07H.ECX=1):EAX.LASS[bit 6], and allow guest to enable it via CR4.LASS[bit 27] on demand. For instruction executed in the guest directly, hardware will perform the check. But KVM also needs to behave same as hardware to apply LASS to kinds of guest memory accesses when emulating privileged instructions by software.

KVM will take following LASS voilations check on emulation path. User-mode access to supervisor space address:

        LA[bit 63] && (CPL == 3)
Supervisor-mode access to user space address:
        Instruction fetch: !LA[bit 63] && (CPL < 3)
        Data access: !LA[bit 63] && (CR4.SMAP==1) && ((RFLAGS.AC == 0 &&
                     CPL < 3) || Implicit supervisor access)

This patch series provide a LASS KVM solution.

We tested the basic function of LASS virtualization including LASS enumeration and enabling in non-root and nested environment. As KVM unittest framework is not compatible to LASS rule, we use kernel module and application test to emulate LASS violation instead. With KVM forced emulation mechanism, we also verified the LASS functionality on some emulation path with instruction fetch and data access to have same behavior as hardware.

[1] Intel ISE https://cdrdv2.intel.com/v1/dl/getContent/671368
Chapter Linear Address Space Separation (LASS)

------------------------------------------------------

v0->v1
1. Adapt to new __linearize() API
2. Function refactor of vmx_check_lass()
3. Refine commit message to be more precise
4. Drop LASS kvm cap detection depending
on hardware capability

Binbin Wu (1):
KVM: x86: Consolidate flags for __linearize()

Zeng Guang (5):
KVM: x86: Virtualize CR4.LASS
KVM: VMX: Add new ops in kvm_x86_ops for LASS violation check
KVM: x86: Add emulator helper for LASS violation check
KVM: x86: LASS protection on KVM emulation
KVM: x86: Advertise LASS CPUID to user space

 arch/x86/include/asm/kvm-x86-ops.h |  3 +-
 arch/x86/include/asm/kvm_host.h    |  4 ++-
 arch/x86/kvm/cpuid.c               |  5 ++-
 arch/x86/kvm/emulate.c             | 47 +++++++++++++++++++++++-----
 arch/x86/kvm/kvm_emulate.h         |  6 ++++
 arch/x86/kvm/vmx/nested.c          |  3 ++
 arch/x86/kvm/vmx/sgx.c             |  4 +++
 arch/x86/kvm/vmx/vmx.c             | 50 ++++++++++++++++++++++++++++++
 arch/x86/kvm/vmx/vmx.h             |  2 ++
 arch/x86/kvm/x86.c                 | 12 +++++++
 arch/x86/kvm/x86.h                 |  2 ++
 11 files changed, 126 insertions(+), 12 deletions(-)
Signature · 1 line
2.27.0
author
#1 lore raw

PATCH 1/6 KVM: x86: Consolidate flags for __linearize()

From: Binbin Wu <[email protected]>

Define a 32-bit parameter and consolidate the two bools into it.

__linearize() has two bool parameters write and fetch. And new flag will be needed to support new feature (e.g. LAM needs a flag to skip address untag under some conditions).

No functional change intended.

---
 arch/x86/kvm/emulate.c     | 19 +++++++++++++------
 arch/x86/kvm/kvm_emulate.h |  4 ++++
 2 files changed, 17 insertions(+), 6 deletions(-)
arch/x86/kvm/emulate.c +13 -6
index 936a397a08cd..9508836e8a35 100644
@@ -687,8 +687,8 @@ static unsigned insn_alignment(struct x86_emulate_ctxt *ctxt, unsigned size)
 static __always_inline int __linearize(struct x86_emulate_ctxt *ctxt,
 				       struct segmented_address addr,
 				       unsigned *max_size, unsigned size,
-				       bool write, bool fetch,
-				       enum x86emul_mode mode, ulong *linear)
+				       u32 flags, enum x86emul_mode mode,
+				       ulong *linear)
 {
 	struct desc_struct desc;
 	bool usable;
@@ -696,6 +696,8 @@ static __always_inline int __linearize(struct x86_emulate_ctxt *ctxt,
 	u32 lim;
 	u16 sel;
 	u8  va_bits;
+	bool fetch = !!(flags & X86EMUL_F_FETCH);
+	bool write = !!(flags & X86EMUL_F_WRITE);
 
 	la = seg_base(ctxt, addr.seg) + addr.ea;
 	*max_size = 0;
@@ -757,7 +759,11 @@ static int linearize(struct x86_emulate_ctxt *ctxt,
 		     ulong *linear)
 {
 	unsigned max_size;
-	return __linearize(ctxt, addr, &max_size, size, write, false,
+	u32 flags = 0;
+
+	if (write)
+		flags |= X86EMUL_F_WRITE;
+	return __linearize(ctxt, addr, &max_size, size, flags,
 			   ctxt->mode, linear);
 }
 
@@ -768,10 +774,11 @@ static inline int assign_eip(struct x86_emulate_ctxt *ctxt, ulong dst)
 	unsigned max_size;
 	struct segmented_address addr = { .seg = VCPU_SREG_CS,
 					   .ea = dst };
+	u32 flags = X86EMUL_F_FETCH;
 
 	if (ctxt->op_bytes != sizeof(unsigned long))
 		addr.ea = dst & ((1UL << (ctxt->op_bytes << 3)) - 1);
-	rc = __linearize(ctxt, addr, &max_size, 1, false, true, ctxt->mode, &linear);
+	rc = __linearize(ctxt, addr, &max_size, 1, flags, ctxt->mode, &linear);
 	if (rc == X86EMUL_CONTINUE)
 		ctxt->_eip = addr.ea;
 	return rc;
@@ -896,6 +903,7 @@ static int __do_insn_fetch_bytes(struct x86_emulate_ctxt *ctxt, int op_size)
 	int cur_size = ctxt->fetch.end - ctxt->fetch.data;
 	struct segmented_address addr = { .seg = VCPU_SREG_CS,
 					   .ea = ctxt->eip + cur_size };
+	u32 flags = X86EMUL_F_FETCH;
 
 	/*
 	 * We do not know exactly how many bytes will be needed, and
@@ -907,8 +915,7 @@ static int __do_insn_fetch_bytes(struct x86_emulate_ctxt *ctxt, int op_size)
 	 * boundary check itself.  Instead, we use max_size to check
 	 * against op_size.
 	 */
-	rc = __linearize(ctxt, addr, &max_size, 0, false, true, ctxt->mode,
-			 &linear);
+	rc = __linearize(ctxt, addr, &max_size, 0, flags, ctxt->mode, &linear);
 	if (unlikely(rc != X86EMUL_CONTINUE))
 		return rc;
 
arch/x86/kvm/kvm_emulate.h +4 -0
index ab65f3a47dfd..5b9ec610b2cb 100644
@@ -88,6 +88,10 @@ struct x86_instruction_info {
 #define X86EMUL_IO_NEEDED       5 /* IO is needed to complete emulation */
 #define X86EMUL_INTERCEPTED     6 /* Intercepted by nested VMCB/VMCS */
 
+/* x86-specific emulation flags */
+#define X86EMUL_F_FETCH			BIT(0)
+#define X86EMUL_F_WRITE			BIT(1)
+
 struct x86_emulate_ops {
 	void (*vm_bugged)(struct x86_emulate_ctxt *ctxt);
 	/*
Signature · 1 line
2.27.0
#2 lore raw

Re: [PATCH v1 1/6] KVM: x86: Consolidate flags for __linearize()

Quoted 3 lines · Zeng Guang

From: Binbin Wu <[email protected]>

Define a 32-bit parameter and consolidate the two bools into it.

Please write changelogs so that they make sense without the context of the shortlog. In isolation, the above provides zero context. And there's no need to provide a play-by-play description of the change, e.g. this can be:

Consolidate __linearize()'s @write and @fetch into a set of flags so that additional flags can be added without needing more/new boolean parameters, e.g. to precisely identify the access type for LASS.

Quoted 2 lines

__linearize() has two bool parameters write and fetch. And new flag will be needed to support new feature (e.g. LAM needs a flag to skip

s/LAM/LASS

Quoted 1 line

address untag under some conditions).

Looks like this was copy+pasted LAM. AIUI, there is no untagging in LASS. Please, please take the time to proofread what you're posting. To you it's a minor typo, to others, incorrect statements like this can create a lot of confusion.

Quoted 14 lines

No functional change intended.

---
 arch/x86/kvm/emulate.c     | 19 +++++++++++++------
 arch/x86/kvm/kvm_emulate.h |  4 ++++
 2 files changed, 17 insertions(+), 6 deletions(-)
arch/x86/kvm/emulate.c +0 -0
index 936a397a08cd..9508836e8a35 100644

arch/x86/kvm/emulate.c static unsigned insn_alignment(struct x86_emulate_ctxt *ctxt, unsigned size) · line 690

Quoted 6 lines
arch/x86/kvm/emulate.c +1 -2
@@ -687,8 +687,8 @@ static unsigned insn_alignment(struct x86_emulate_ctxt *ctxt, unsigned size)
 static __always_inline int __linearize(struct x86_emulate_ctxt *ctxt,
 				       struct segmented_address addr,
 				       unsigned *max_size, unsigned size,
-				       bool write, bool fetch,
-				       enum x86emul_mode mode, ulong *linear)
+				       u32 flags, enum x86emul_mode mode,

"unsigned int", not "u32". They're obviously the same effective thing, but using "unsigned int" captures that the number of bits doesn't truly matter, e.g. isn't reflected in hardware or anything. This could just as easily be a u16, but there's obviously no reason to squeeze this into a u16.

Quoted 14 lines
arch/x86/kvm/emulate.c +3 -0
+				       ulong *linear)
 {
 	struct desc_struct desc;
 	bool usable;
@@ -696,6 +696,8 @@ static __always_inline int __linearize(struct x86_emulate_ctxt *ctxt,
 	u32 lim;
 	u16 sel;
 	u8  va_bits;
+	bool fetch = !!(flags & X86EMUL_F_FETCH);
+	bool write = !!(flags & X86EMUL_F_WRITE);
 
 	la = seg_base(ctxt, addr.seg) + addr.ea;
 	*max_size = 0;

arch/x86/kvm/emulate.c static int linearize(struct x86_emulate_ctxt *ctxt, · line 767

Quoted 10 lines
arch/x86/kvm/emulate.c +5 -1
@@ -757,7 +759,11 @@ static int linearize(struct x86_emulate_ctxt *ctxt,
 		     ulong *linear)
 {
 	unsigned max_size;
-	return __linearize(ctxt, addr, &max_size, size, write, false,
+	u32 flags = 0;
+
+	if (write)
+		flags |= X86EMUL_F_WRITE;
+	return __linearize(ctxt, addr, &max_size, size, flags,
 			   ctxt->mode, linear);

I'm tempted to have this be:

	return __linearize(ctxt, addr, &max_size, size,
			   write ? X86EMUL_F_WRITE : 0, ctxt->mode, linear);

Mostly so that it's obvious "flags" is constant. The alterntive would e

	const unsigned int flags = write ? X86EMUL_F_WRITE : 0;

But my preference is probably to omit "flags" entirely.

arch/x86/kvm/emulate.c static inline int assign_eip(struct x86_emulate_ctxt *ctxt, ulong dst) · line 781

Quoted 12 lines
arch/x86/kvm/emulate.c +2 -1
 }
 
@@ -768,10 +774,11 @@ static inline int assign_eip(struct x86_emulate_ctxt *ctxt, ulong dst)
 	unsigned max_size;
 	struct segmented_address addr = { .seg = VCPU_SREG_CS,
 					   .ea = dst };
+	u32 flags = X86EMUL_F_FETCH;
 
 	if (ctxt->op_bytes != sizeof(unsigned long))
 		addr.ea = dst & ((1UL << (ctxt->op_bytes << 3)) - 1);
-	rc = __linearize(ctxt, addr, &max_size, 1, false, true, ctxt->mode, &linear);
+	rc = __linearize(ctxt, addr, &max_size, 1, flags, ctxt->mode, &linear);

Meh, just pass X86EMUL_F_FETCH directly, i.e. drop the local "flags".

Quoted 12 lines
arch/x86/kvm/emulate.c +1 -0
 	if (rc == X86EMUL_CONTINUE)
 		ctxt->_eip = addr.ea;
 	return rc;
@@ -896,6 +903,7 @@ static int __do_insn_fetch_bytes(struct x86_emulate_ctxt *ctxt, int op_size)
 	int cur_size = ctxt->fetch.end - ctxt->fetch.data;
 	struct segmented_address addr = { .seg = VCPU_SREG_CS,
 					   .ea = ctxt->eip + cur_size };
+	u32 flags = X86EMUL_F_FETCH;
 
 	/*
 	 * We do not know exactly how many bytes will be needed, and

arch/x86/kvm/emulate.c static int __do_insn_fetch_bytes(struct x86_emulate_ctxt *ctxt, int op_size) · line 918

Quoted 6 lines
arch/x86/kvm/emulate.c +1 -2
@@ -907,8 +915,7 @@ static int __do_insn_fetch_bytes(struct x86_emulate_ctxt *ctxt, int op_size)
 	 * boundary check itself.  Instead, we use max_size to check
 	 * against op_size.
 	 */
-	rc = __linearize(ctxt, addr, &max_size, 0, false, true, ctxt->mode,
-			 &linear);
+	rc = __linearize(ctxt, addr, &max_size, 0, flags, ctxt->mode, &linear);

And here.

Quoted 21 lines
arch/x86/kvm/emulate.c +0 -0
 	if (unlikely(rc != X86EMUL_CONTINUE))
 		return rc;
 
arch/x86/kvm/kvm_emulate.h +4 -0
index ab65f3a47dfd..5b9ec610b2cb 100644
@@ -88,6 +88,10 @@ struct x86_instruction_info {
 #define X86EMUL_IO_NEEDED       5 /* IO is needed to complete emulation */
 #define X86EMUL_INTERCEPTED     6 /* Intercepted by nested VMCB/VMCS */
 
+/* x86-specific emulation flags */
+#define X86EMUL_F_FETCH			BIT(0)
+#define X86EMUL_F_WRITE			BIT(1)
+
 struct x86_emulate_ops {
 	void (*vm_bugged)(struct x86_emulate_ctxt *ctxt);
 	/*
Signature · 1 line
2.27.0
author
#3 lore raw

Re: [PATCH v1 1/6] KVM: x86: Consolidate flags for __linearize()

Quoted 12 lines · Sean Christopherson
Quoted 3 lines · Zeng Guang

From: Binbin Wu <[email protected]>

Define a 32-bit parameter and consolidate the two bools into it.

Please write changelogs so that they make sense without the context of the shortlog. In isolation, the above provides zero context. And there's no need to provide a play-by-play description of the change, e.g. this can be:

Consolidate __linearize()'s @write and @fetch into a set of flags so that additional flags can be added without needing more/new boolean parameters, e.g. to precisely identify the access type for LASS.

Quoted 8 lines
Quoted 2 lines

__linearize() has two bool parameters write and fetch. And new flag will be needed to support new feature (e.g. LAM needs a flag to skip

s/LAM/LASS

Quoted 1 line

address untag under some conditions).

Looks like this was copy+pasted LAM. AIUI, there is no untagging in LASS. Please, please take the time to proofread what you're posting. To you it's a minor typo, to others, incorrect statements like this can create a lot of confusion.

LASS and LAM attempts to apply same emulator framework. This patch as prerequisite directly uses the one of LAM patches. But no excuse, we will modify it to non-feature and hardware specific as you suggested. Thanks.

#4 lore raw

Re: [PATCH v1 1/6] KVM: x86: Consolidate flags for __linearize()

In reply to Sean Christopherson
Quoted 30 lines · Sean Christopherson
Quoted 3 lines · Zeng Guang

From: Binbin Wu <[email protected]>

Define a 32-bit parameter and consolidate the two bools into it.

Please write changelogs so that they make sense without the context of the shortlog. In isolation, the above provides zero context. And there's no need to provide a play-by-play description of the change, e.g. this can be:

Consolidate __linearize()'s @write and @fetch into a set of flags so that additional flags can be added without needing more/new boolean parameters, e.g. to precisely identify the access type for LASS.

Quoted 2 lines

__linearize() has two bool parameters write and fetch. And new flag will be needed to support new feature (e.g. LAM needs a flag to skip

s/LAM/LASS

Quoted 1 line

address untag under some conditions).

Looks like this was copy+pasted LAM. AIUI, there is no untagging in LASS. Please, please take the time to proofread what you're posting. To you it's a minor typo, to others, incorrect statements like this can create a lot of confusion.

Quoted 9 lines

No functional change intended.

---
  arch/x86/kvm/emulate.c     | 19 +++++++++++++------
  arch/x86/kvm/kvm_emulate.h |  4 ++++
  2 files changed, 17 insertions(+), 6 deletions(-)
Quoted 15 lines

arch/x86/kvm/emulate.c static unsigned insn_alignment(struct x86_emulate_ctxt *ctxt, unsigned size) · line 690

Quoted 11 lines
arch/x86/kvm/emulate.c +1 -2
index 936a397a08cd..9508836e8a35 100644
@@ -687,8 +687,8 @@ static unsigned insn_alignment(struct x86_emulate_ctxt *ctxt, unsigned size)
  static __always_inline int __linearize(struct x86_emulate_ctxt *ctxt,
  				       struct segmented_address addr,
  				       unsigned *max_size, unsigned size,
-				       bool write, bool fetch,
-				       enum x86emul_mode mode, ulong *linear)
+				       u32 flags, enum x86emul_mode mode,

"unsigned int", not "u32". They're obviously the same effective thing, but using "unsigned int" captures that the number of bits doesn't truly matter, e.g. isn't reflected in hardware or anything. This could just as easily be a u16, but there's obviously no reason to squeeze this into a u16.

OK. Thanks.

Except for the "u32" / "usigned int" thing, I have updated the patch in KVM LAM enabling patch series v9:
https://lore.kernel.org/kvm/[email protected]/
It has dropped the unnecessary local variables you mentioned below and improved the changelog a bit to be more informative.

Do you have any comment on this version?

Quoted 91 lines
Quoted 15 lines
arch/x86/kvm/emulate.c +3 -0
+				       ulong *linear)
  {
  	struct desc_struct desc;
  	bool usable;
@@ -696,6 +696,8 @@ static __always_inline int __linearize(struct x86_emulate_ctxt *ctxt,
  	u32 lim;
  	u16 sel;
  	u8  va_bits;
+	bool fetch = !!(flags & X86EMUL_F_FETCH);
+	bool write = !!(flags & X86EMUL_F_WRITE);
 
  	la = seg_base(ctxt, addr.seg) + addr.ea;
  	*max_size = 0;

arch/x86/kvm/emulate.c static int linearize(struct x86_emulate_ctxt *ctxt, · line 767

Quoted 10 lines
arch/x86/kvm/emulate.c +5 -1
@@ -757,7 +759,11 @@ static int linearize(struct x86_emulate_ctxt *ctxt,
  		     ulong *linear)
  {
  	unsigned max_size;
-	return __linearize(ctxt, addr, &max_size, size, write, false,
+	u32 flags = 0;
+
+	if (write)
+		flags |= X86EMUL_F_WRITE;
+	return __linearize(ctxt, addr, &max_size, size, flags,
  			   ctxt->mode, linear);

I'm tempted to have this be:

	return __linearize(ctxt, addr, &max_size, size,
			   write ? X86EMUL_F_WRITE : 0, ctxt->mode, linear);

Mostly so that it's obvious "flags" is constant. The alterntive would e

	const unsigned int flags = write ? X86EMUL_F_WRITE : 0;

But my preference is probably to omit "flags" entirely.

arch/x86/kvm/emulate.c static inline int assign_eip(struct x86_emulate_ctxt *ctxt, ulong dst) · line 781

Quoted 12 lines
arch/x86/kvm/emulate.c +2 -1
  }
 
@@ -768,10 +774,11 @@ static inline int assign_eip(struct x86_emulate_ctxt *ctxt, ulong dst)
  	unsigned max_size;
  	struct segmented_address addr = { .seg = VCPU_SREG_CS,
  					   .ea = dst };
+	u32 flags = X86EMUL_F_FETCH;
 
  	if (ctxt->op_bytes != sizeof(unsigned long))
  		addr.ea = dst & ((1UL << (ctxt->op_bytes << 3)) - 1);
-	rc = __linearize(ctxt, addr, &max_size, 1, false, true, ctxt->mode, &linear);
+	rc = __linearize(ctxt, addr, &max_size, 1, flags, ctxt->mode, &linear);

Meh, just pass X86EMUL_F_FETCH directly, i.e. drop the local "flags".

Quoted 12 lines
arch/x86/kvm/emulate.c +1 -0
  	if (rc == X86EMUL_CONTINUE)
  		ctxt->_eip = addr.ea;
  	return rc;
@@ -896,6 +903,7 @@ static int __do_insn_fetch_bytes(struct x86_emulate_ctxt *ctxt, int op_size)
  	int cur_size = ctxt->fetch.end - ctxt->fetch.data;
  	struct segmented_address addr = { .seg = VCPU_SREG_CS,
  					   .ea = ctxt->eip + cur_size };
+	u32 flags = X86EMUL_F_FETCH;
 
  	/*
  	 * We do not know exactly how many bytes will be needed, and

arch/x86/kvm/emulate.c static int __do_insn_fetch_bytes(struct x86_emulate_ctxt *ctxt, int op_size) · line 918

Quoted 6 lines
arch/x86/kvm/emulate.c +1 -2
@@ -907,8 +915,7 @@ static int __do_insn_fetch_bytes(struct x86_emulate_ctxt *ctxt, int op_size)
  	 * boundary check itself.  Instead, we use max_size to check
  	 * against op_size.
  	 */
-	rc = __linearize(ctxt, addr, &max_size, 0, false, true, ctxt->mode,
-			 &linear);
+	rc = __linearize(ctxt, addr, &max_size, 0, flags, ctxt->mode, &linear);

And here.

Quoted 21 lines
arch/x86/kvm/emulate.c +0 -0
  	if (unlikely(rc != X86EMUL_CONTINUE))
  		return rc;
 
arch/x86/kvm/kvm_emulate.h +4 -0
index ab65f3a47dfd..5b9ec610b2cb 100644
@@ -88,6 +88,10 @@ struct x86_instruction_info {
  #define X86EMUL_IO_NEEDED       5 /* IO is needed to complete emulation */
  #define X86EMUL_INTERCEPTED     6 /* Intercepted by nested VMCB/VMCS */
 
+/* x86-specific emulation flags */
+#define X86EMUL_F_FETCH			BIT(0)
+#define X86EMUL_F_WRITE			BIT(1)
+
  struct x86_emulate_ops {
  	void (*vm_bugged)(struct x86_emulate_ctxt *ctxt);
  	/*
Signature · 1 line
2.27.0
author
#5 lore raw

PATCH 2/6 KVM: x86: Virtualize CR4.LASS

In reply to Zeng Guang

Virtualize CR4.LASS[bit 27] under KVM control instead of being guest-owned as CR4.LASS generally set once for each vCPU at boot time and won't be toggled at runtime. Besides, only if VM has LASS capability enumerated with CPUID.(EAX=07H.ECX=1):EAX.LASS[bit 6], KVM allows guest software to be able to set CR4.LASS.

Updating cr4_fixed1 to set CR4.LASS bit in the emulated IA32_VMX_CR4_FIXED1 MSR for guests and allow guests to enable LASS in nested VMX operaion as well.

Notes: Setting CR4.LASS to 1 enable LASS in IA-32e mode. It doesn't take effect in legacy mode even if CR4.LASS is set.

---
 arch/x86/include/asm/kvm_host.h | 2 +-
 arch/x86/kvm/vmx/vmx.c          | 3 +++
 arch/x86/kvm/x86.h              | 2 ++
 3 files changed, 6 insertions(+), 1 deletion(-)
arch/x86/include/asm/kvm_host.h +1 -1
index fb9d1f2d6136..92d8e65fe88c 100644
@@ -125,7 +125,7 @@
 			  | X86_CR4_PGE | X86_CR4_PCE | X86_CR4_OSFXSR | X86_CR4_PCIDE \
 			  | X86_CR4_OSXSAVE | X86_CR4_SMEP | X86_CR4_FSGSBASE \
 			  | X86_CR4_OSXMMEXCPT | X86_CR4_LA57 | X86_CR4_VMXE \
-			  | X86_CR4_SMAP | X86_CR4_PKE | X86_CR4_UMIP))
+			  | X86_CR4_SMAP | X86_CR4_PKE | X86_CR4_UMIP | X86_CR4_LASS))
 
 #define CR8_RESERVED_BITS (~(unsigned long)X86_CR8_TPR)
 
arch/x86/kvm/vmx/vmx.c +3 -0
index 44fb619803b8..a33205ded85c 100644
@@ -7603,6 +7603,9 @@ static void nested_vmx_cr_fixed1_bits_update(struct kvm_vcpu *vcpu)
 	cr4_fixed1_update(X86_CR4_UMIP,       ecx, feature_bit(UMIP));
 	cr4_fixed1_update(X86_CR4_LA57,       ecx, feature_bit(LA57));
 
+	entry = kvm_find_cpuid_entry_index(vcpu, 0x7, 1);
+	cr4_fixed1_update(X86_CR4_LASS,       eax, feature_bit(LASS));
+
 #undef cr4_fixed1_update
 }
 
arch/x86/kvm/x86.h +2 -0
index c544602d07a3..e1295f490308 100644
@@ -529,6 +529,8 @@ bool kvm_msr_allowed(struct kvm_vcpu *vcpu, u32 index, u32 type);
 		__reserved_bits |= X86_CR4_VMXE;        \
 	if (!__cpu_has(__c, X86_FEATURE_PCID))          \
 		__reserved_bits |= X86_CR4_PCIDE;       \
+	if (!__cpu_has(__c, X86_FEATURE_LASS))          \
+		__reserved_bits |= X86_CR4_LASS;        \
 	__reserved_bits;                                \
 })
 
Signature · 1 line
2.27.0
#6 lore raw

Re: [PATCH v1 2/6] KVM: x86: Virtualize CR4.LASS

Quoted 8 lines · Zeng Guang

Virtualize CR4.LASS[bit 27] under KVM control instead of being guest-owned as CR4.LASS generally set once for each vCPU at boot time and won't be toggled at runtime. Besides, only if VM has LASS capability enumerated with CPUID.(EAX=07H.ECX=1):EAX.LASS[bit 6], KVM allows guest software to be able to set CR4.LASS.

Updating cr4_fixed1 to set CR4.LASS bit in the emulated IA32_VMX_CR4_FIXED1 MSR for guests and allow guests to enable LASS in nested VMX operaion as well.

s/operaion/operation

Quoted 17 lines

Notes: Setting CR4.LASS to 1 enable LASS in IA-32e mode. It doesn't take effect in legacy mode even if CR4.LASS is set.

---
  arch/x86/include/asm/kvm_host.h | 2 +-
  arch/x86/kvm/vmx/vmx.c          | 3 +++
  arch/x86/kvm/x86.h              | 2 ++
  3 files changed, 6 insertions(+), 1 deletion(-)
arch/x86/include/asm/kvm_host.h +0 -0
index fb9d1f2d6136..92d8e65fe88c 100644

arch/x86/include/asm/kvm_host.h line 128

Quoted 5 lines
arch/x86/include/asm/kvm_host.h +1 -1
@@ -125,7 +125,7 @@
  			  | X86_CR4_PGE | X86_CR4_PCE | X86_CR4_OSFXSR | X86_CR4_PCIDE \
  			  | X86_CR4_OSXSAVE | X86_CR4_SMEP | X86_CR4_FSGSBASE \
  			  | X86_CR4_OSXMMEXCPT | X86_CR4_LA57 | X86_CR4_VMXE \
-			  | X86_CR4_SMAP | X86_CR4_PKE | X86_CR4_UMIP))
+			  | X86_CR4_SMAP | X86_CR4_PKE | X86_CR4_UMIP | X86_CR4_LASS))

Suppose there is some bare-matel linux patch to define the LASS related macros. May be better to describe the dependent patch(es) in cover letter.

Quoted 30 lines
arch/x86/include/asm/kvm_host.h +0 -0
  #define CR8_RESERVED_BITS (~(unsigned long)X86_CR8_TPR)
 
arch/x86/kvm/vmx/vmx.c +3 -0
index 44fb619803b8..a33205ded85c 100644
@@ -7603,6 +7603,9 @@ static void nested_vmx_cr_fixed1_bits_update(struct kvm_vcpu *vcpu)
  	cr4_fixed1_update(X86_CR4_UMIP,       ecx, feature_bit(UMIP));
  	cr4_fixed1_update(X86_CR4_LA57,       ecx, feature_bit(LA57));
 
+	entry = kvm_find_cpuid_entry_index(vcpu, 0x7, 1);
+	cr4_fixed1_update(X86_CR4_LASS,       eax, feature_bit(LASS));
+
  #undef cr4_fixed1_update
  }
 
arch/x86/kvm/x86.h +2 -0
index c544602d07a3..e1295f490308 100644
@@ -529,6 +529,8 @@ bool kvm_msr_allowed(struct kvm_vcpu *vcpu, u32 index, u32 type);
  		__reserved_bits |= X86_CR4_VMXE;        \
  	if (!__cpu_has(__c, X86_FEATURE_PCID))          \
  		__reserved_bits |= X86_CR4_PCIDE;       \
+	if (!__cpu_has(__c, X86_FEATURE_LASS))          \
+		__reserved_bits |= X86_CR4_LASS;        \
  	__reserved_bits;                                \
  })
 
author
#7 lore raw

Re: [PATCH v1 2/6] KVM: x86: Virtualize CR4.LASS

Quoted 11 lines · Binbin Wu
Quoted 8 lines · Zeng Guang

Virtualize CR4.LASS[bit 27] under KVM control instead of being guest-owned as CR4.LASS generally set once for each vCPU at boot time and won't be toggled at runtime. Besides, only if VM has LASS capability enumerated with CPUID.(EAX=07H.ECX=1):EAX.LASS[bit 6], KVM allows guest software to be able to set CR4.LASS.

Updating cr4_fixed1 to set CR4.LASS bit in the emulated IA32_VMX_CR4_FIXED1 MSR for guests and allow guests to enable LASS in nested VMX operaion as well.

s/operaion/operation

Thanks.

Quoted 12 lines
Quoted 12 lines

Notes: Setting CR4.LASS to 1 enable LASS in IA-32e mode. It doesn't take effect in legacy mode even if CR4.LASS is set.

---
   arch/x86/include/asm/kvm_host.h | 2 +-
   arch/x86/kvm/vmx/vmx.c          | 3 +++
   arch/x86/kvm/x86.h              | 2 ++
   3 files changed, 6 insertions(+), 1 deletion(-)
Quoted 14 lines

arch/x86/include/asm/kvm_host.h line 128

Quoted 10 lines
arch/x86/include/asm/kvm_host.h +1 -1
index fb9d1f2d6136..92d8e65fe88c 100644
@@ -125,7 +125,7 @@
   			  | X86_CR4_PGE | X86_CR4_PCE | X86_CR4_OSFXSR | X86_CR4_PCIDE \
   			  | X86_CR4_OSXSAVE | X86_CR4_SMEP | X86_CR4_FSGSBASE \
   			  | X86_CR4_OSXMMEXCPT | X86_CR4_LA57 | X86_CR4_VMXE \
-			  | X86_CR4_SMAP | X86_CR4_PKE | X86_CR4_UMIP))
+			  | X86_CR4_SMAP | X86_CR4_PKE | X86_CR4_UMIP | X86_CR4_LASS))

Suppose there is some bare-matel linux patch to define the LASS related macros.
May be better to describe the dependent patch(es) in cover letter.

Good suggestion.

#8 lore raw

Re: [PATCH v1 2/6] KVM: x86: Virtualize CR4.LASS

In reply to Zeng Guang
Quoted 8 lines · Zeng Guang

Virtualize CR4.LASS[bit 27] under KVM control instead of being guest-owned as CR4.LASS generally set once for each vCPU at boot time and won't be toggled at runtime. Besides, only if VM has LASS capability enumerated with CPUID.(EAX=07H.ECX=1):EAX.LASS[bit 6], KVM allows guest software to be able to set CR4.LASS.

Updating cr4_fixed1 to set CR4.LASS bit in the emulated IA32_VMX_CR4_FIXED1 MSR for guests and allow guests to enable LASS in nested VMX operaion as well.

s/operaion/operation.

author
#9 lore raw

Re: [PATCH v1 2/6] KVM: x86: Virtualize CR4.LASS

Quoted 10 lines · Sean Christopherson
Quoted 8 lines · Zeng Guang

Virtualize CR4.LASS[bit 27] under KVM control instead of being guest-owned as CR4.LASS generally set once for each vCPU at boot time and won't be toggled at runtime. Besides, only if VM has LASS capability enumerated with CPUID.(EAX=07H.ECX=1):EAX.LASS[bit 6], KVM allows guest software to be able to set CR4.LASS.

Updating cr4_fixed1 to set CR4.LASS bit in the emulated IA32_VMX_CR4_FIXED1 MSR for guests and allow guests to enable LASS in nested VMX operaion as well.

s/operaion/operation.

Will fix in next version. Thanks.

#10 lore raw

Re: [PATCH v1 2/6] KVM: x86: Virtualize CR4.LASS

In reply to Zeng Guang

Belatedly, same complaints as the LAM series[*], this patch doesn't actually virtual LASS. Just squash this with the previous patch, "KVM: VMX: Implement and apply vmx_is_lass_violation() for LASS protection", though I would keep the shortlog from this patch.

[*] https://lore.kernel.org/all/[email protected]

Quoted 14 lines · Zeng Guang

Virtualize CR4.LASS[bit 27] under KVM control instead of being guest-owned as CR4.LASS generally set once for each vCPU at boot time and won't be toggled at runtime. Besides, only if VM has LASS capability enumerated with CPUID.(EAX=07H.ECX=1):EAX.LASS[bit 6], KVM allows guest software to be able to set CR4.LASS.

Updating cr4_fixed1 to set CR4.LASS bit in the emulated IA32_VMX_CR4_FIXED1 MSR for guests and allow guests to enable LASS in nested VMX operaion as well.

Notes: Setting CR4.LASS to 1 enable LASS in IA-32e mode. It doesn't take effect in legacy mode even if CR4.LASS is set.

author
#11 lore raw

PATCH 3/6 KVM: VMX: Add new ops in kvm_x86_ops for LASS violation check

In reply to Zeng Guang

Intel introduces LASS (Linear Address Separation) feature providing an independent mechanism to achieve the mode-based protection.

LASS partitions 64-bit linear address space into two halves, user-mode address (LA[bit 63]=0) and supervisor-mode address (LA[bit 63]=1). It stops any code execution or conditional data access[1]
1. from user mode to supervisor-mode address space
2. from supervisor mode to user-mode address space
and generates LASS violation fault accordingly.

[1]A supervisor mode data access causes a LASS violation only if supervisor mode access protection is enabled (CR4.SMAP = 1) and either RFLAGS.AC = 0 or the access implicitly accesses a system data structure.

Following are the rules of LASS violation check on the linear address(LA). User access to supervisor-mode address space:

    LA[bit 63] && (CPL == 3)
Supervisor access to user-mode address space:
    Instruction fetch: !LA[bit 63] && (CPL < 3)
    Data access: !LA[bit 63] && (CR4.SMAP==1) && ((RFLAGS.AC == 0 &&
                 CPL < 3) || Implicit supervisor access)

Add new ops in kvm_x86_ops to do LASS violation check.

---
 arch/x86/include/asm/kvm-x86-ops.h |  3 +-
 arch/x86/include/asm/kvm_host.h    |  2 ++
 arch/x86/kvm/kvm_emulate.h         |  1 +
 arch/x86/kvm/vmx/vmx.c             | 47 ++++++++++++++++++++++++++++++
 arch/x86/kvm/vmx/vmx.h             |  2 ++
 5 files changed, 54 insertions(+), 1 deletion(-)
arch/x86/include/asm/kvm-x86-ops.h +2 -1
index 13bc212cd4bc..8980a3bfa687 100644
@@ -132,7 +132,8 @@ KVM_X86_OP_OPTIONAL(migrate_timers)
 KVM_X86_OP(msr_filter_changed)
 KVM_X86_OP(complete_emulated_msr)
 KVM_X86_OP(vcpu_deliver_sipi_vector)
-KVM_X86_OP_OPTIONAL_RET0(vcpu_get_apicv_inhibit_reasons);
+KVM_X86_OP_OPTIONAL_RET0(vcpu_get_apicv_inhibit_reasons)
+KVM_X86_OP_OPTIONAL_RET0(check_lass)
 
 #undef KVM_X86_OP
 #undef KVM_X86_OP_OPTIONAL
arch/x86/include/asm/kvm_host.h +2 -0
index 92d8e65fe88c..98666d1e7727 100644
@@ -1731,6 +1731,8 @@ struct kvm_x86_ops {
 	 * Returns vCPU specific APICv inhibit reasons
 	 */
 	unsigned long (*vcpu_get_apicv_inhibit_reasons)(struct kvm_vcpu *vcpu);
+
+	bool (*check_lass)(struct kvm_vcpu *vcpu, u64 access, u64 la, u32 flags);
 };
 
 struct kvm_x86_nested_ops {
arch/x86/kvm/kvm_emulate.h +1 -0
index 5b9ec610b2cb..f1439ab7c14b 100644
@@ -91,6 +91,7 @@ struct x86_instruction_info {
 /* x86-specific emulation flags */
 #define X86EMUL_F_FETCH			BIT(0)
 #define X86EMUL_F_WRITE			BIT(1)
+#define X86EMUL_F_SKIPLASS		BIT(2)
 
 struct x86_emulate_ops {
 	void (*vm_bugged)(struct x86_emulate_ctxt *ctxt);
arch/x86/kvm/vmx/vmx.c +47 -0

59 lines not on this page · raw message

arch/x86/kvm/vmx/vmx.h +2 -0
index 9e66531861cf..f2e775b9849b 100644
@@ -433,6 +433,8 @@ void vmx_enable_intercept_for_msr(struct kvm_vcpu *vcpu, u32 msr, int type);
 u64 vmx_get_l2_tsc_offset(struct kvm_vcpu *vcpu);
 u64 vmx_get_l2_tsc_multiplier(struct kvm_vcpu *vcpu);
 
+bool vmx_check_lass(struct kvm_vcpu *vcpu, u64 access, u64 la, u32 flags);
+
 static inline void vmx_set_intercept_for_msr(struct kvm_vcpu *vcpu, u32 msr,
 					     int type, bool value)
 {
Signature · 1 line
2.27.0
#12 lore raw

Re: [PATCH v1 3/6] KVM: VMX: Add new ops in kvm_x86_ops for LASS violation check

Quoted 1 line · Zeng Guang

Intel introduces LASS (Linear Address Separation) feature providing

                      ^
 missing "Space" here

Quoted 85 lines

an independent mechanism to achieve the mode-based protection.

LASS partitions 64-bit linear address space into two halves, user-mode address (LA[bit 63]=0) and supervisor-mode address (LA[bit 63]=1). It stops any code execution or conditional data access[1]
1. from user mode to supervisor-mode address space
2. from supervisor mode to user-mode address space
and generates LASS violation fault accordingly.

[1]A supervisor mode data access causes a LASS violation only if supervisor mode access protection is enabled (CR4.SMAP = 1) and either RFLAGS.AC = 0 or the access implicitly accesses a system data structure.

Following are the rules of LASS violation check on the linear address(LA). User access to supervisor-mode address space:

     LA[bit 63] && (CPL == 3)
Supervisor access to user-mode address space:
     Instruction fetch: !LA[bit 63] && (CPL < 3)
     Data access: !LA[bit 63] && (CR4.SMAP==1) && ((RFLAGS.AC == 0 &&
                  CPL < 3) || Implicit supervisor access)

Add new ops in kvm_x86_ops to do LASS violation check.

---
  arch/x86/include/asm/kvm-x86-ops.h |  3 +-
  arch/x86/include/asm/kvm_host.h    |  2 ++
  arch/x86/kvm/kvm_emulate.h         |  1 +
  arch/x86/kvm/vmx/vmx.c             | 47 ++++++++++++++++++++++++++++++
  arch/x86/kvm/vmx/vmx.h             |  2 ++
  5 files changed, 54 insertions(+), 1 deletion(-)
arch/x86/include/asm/kvm-x86-ops.h +2 -1
index 13bc212cd4bc..8980a3bfa687 100644
@@ -132,7 +132,8 @@ KVM_X86_OP_OPTIONAL(migrate_timers)
  KVM_X86_OP(msr_filter_changed)
  KVM_X86_OP(complete_emulated_msr)
  KVM_X86_OP(vcpu_deliver_sipi_vector)
-KVM_X86_OP_OPTIONAL_RET0(vcpu_get_apicv_inhibit_reasons);
+KVM_X86_OP_OPTIONAL_RET0(vcpu_get_apicv_inhibit_reasons)
+KVM_X86_OP_OPTIONAL_RET0(check_lass)
 
  #undef KVM_X86_OP
  #undef KVM_X86_OP_OPTIONAL
arch/x86/include/asm/kvm_host.h +2 -0
index 92d8e65fe88c..98666d1e7727 100644
@@ -1731,6 +1731,8 @@ struct kvm_x86_ops {
  	 * Returns vCPU specific APICv inhibit reasons
  	 */
  	unsigned long (*vcpu_get_apicv_inhibit_reasons)(struct kvm_vcpu *vcpu);
+
+	bool (*check_lass)(struct kvm_vcpu *vcpu, u64 access, u64 la, u32 flags);
  };
 
  struct kvm_x86_nested_ops {
arch/x86/kvm/kvm_emulate.h +1 -0
index 5b9ec610b2cb..f1439ab7c14b 100644
@@ -91,6 +91,7 @@ struct x86_instruction_info {
  /* x86-specific emulation flags */
  #define X86EMUL_F_FETCH			BIT(0)
  #define X86EMUL_F_WRITE			BIT(1)
+#define X86EMUL_F_SKIPLASS		BIT(2)
 
  struct x86_emulate_ops {
  	void (*vm_bugged)(struct x86_emulate_ctxt *ctxt);
arch/x86/kvm/vmx/vmx.c +5 -0
index a33205ded85c..876997e8448e 100644
@@ -8130,6 +8130,51 @@ static void vmx_vm_destroy(struct kvm *kvm)
  	free_pages((unsigned long)kvm_vmx->pid_table, vmx_get_pid_table_order(kvm));
  }
 
+/*
+ * Determine whether an access to the linear address causes a LASS violation.
+ * LASS protection is only effective in long mode. As a prerequisite, caller
+ * should make sure vCPU running in long mode and invoke this api to do LASS
+ * violation check.

arch/x86/kvm/vmx/vmx.c static void vmx_vm_destroy(struct kvm *kvm) · line 8147

Quoted 10 lines
arch/x86/kvm/vmx/vmx.c +10 -0
+ */
+bool vmx_check_lass(struct kvm_vcpu *vcpu, u64 access, u64 la, u32 flags)
+{
+	bool user_mode, user_as, rflags_ac;
+
+	if (!!(flags & X86EMUL_F_SKIPLASS) ||
+	    !kvm_is_cr4_bit_set(vcpu, X86_CR4_LASS))
+		return false;
+
+	WARN_ON_ONCE(!is_long_mode(vcpu));

IMHO, it's better to skip the following checks and return false if it is out of long mode.

arch/x86/kvm/vmx/vmx.c static void vmx_vm_destroy(struct kvm *kvm) · line 8149

Quoted 2 lines
arch/x86/kvm/vmx/vmx.c +2 -0
+
+	user_as = !(la >> 63);

It's better to describe how LASS treat linear address in compatibility mode in changelog or/and in comment, i.e. for a linear address with only 32 bits (or 16 bits), the processor treats bit 63 as if it were 0.

Quoted 53 lines
arch/x86/kvm/vmx/vmx.c +30 -0
+
+	/*
+	 * An access is a supervisor-mode access if CPL < 3 or if it implicitly
+	 * accesses a system data structure. For implicit accesses to system
+	 * data structure, the processor acts as if RFLAGS.AC is clear.
+	 */
+	if (access & PFERR_IMPLICIT_ACCESS) {
+		user_mode = false;
+		rflags_ac = false;
+	} else {
+		user_mode = vmx_get_cpl(vcpu) == 3;
+		if (!user_mode)
+			rflags_ac = !!(kvm_get_rflags(vcpu) & X86_EFLAGS_AC);
+	}
+
+	if (user_mode == user_as)
+		return false;
+
+	/*
+	 * Supervisor-mode _data_ accesses to user address space
+	 * cause LASS violations only if SMAP is enabled.
+	 */
+	if (!user_mode && !(access & PFERR_FETCH_MASK))
+		return kvm_is_cr4_bit_set(vcpu, X86_CR4_SMAP) && !rflags_ac;
+
+	return true;
+}
+
  static struct kvm_x86_ops vmx_x86_ops __initdata = {
  	.name = KBUILD_MODNAME,
 
@@ -8269,6 +8314,8 @@ static struct kvm_x86_ops vmx_x86_ops __initdata = {
  	.complete_emulated_msr = kvm_complete_insn_gp,
 
  	.vcpu_deliver_sipi_vector = kvm_vcpu_deliver_sipi_vector,
+
+	.check_lass = vmx_check_lass,
  };
 
  static unsigned int vmx_handle_intel_pt_intr(void)
arch/x86/kvm/vmx/vmx.h +2 -0
index 9e66531861cf..f2e775b9849b 100644
@@ -433,6 +433,8 @@ void vmx_enable_intercept_for_msr(struct kvm_vcpu *vcpu, u32 msr, int type);
  u64 vmx_get_l2_tsc_offset(struct kvm_vcpu *vcpu);
  u64 vmx_get_l2_tsc_multiplier(struct kvm_vcpu *vcpu);
 
+bool vmx_check_lass(struct kvm_vcpu *vcpu, u64 access, u64 la, u32 flags);
+
  static inline void vmx_set_intercept_for_msr(struct kvm_vcpu *vcpu, u32 msr,
  					     int type, bool value)
  {
#13 lore raw

Re: [PATCH v1 3/6] KVM: VMX: Add new ops in kvm_x86_ops for LASS violation check

Quoted 95 lines · On Mon, 5 Jun 2023 11:31:48 +0800 Binbin Wu <[email protected]> wrote:
Quoted 1 line · Zeng Guang

Intel introduces LASS (Linear Address Separation) feature providing

                      ^
 missing "Space" here

Quoted 89 lines

an independent mechanism to achieve the mode-based protection.

LASS partitions 64-bit linear address space into two halves, user-mode address (LA[bit 63]=0) and supervisor-mode address (LA[bit 63]=1). It stops any code execution or conditional data access[1]
1. from user mode to supervisor-mode address space
2. from supervisor mode to user-mode address space
and generates LASS violation fault accordingly.

[1]A supervisor mode data access causes a LASS violation only if supervisor mode access protection is enabled (CR4.SMAP = 1) and either RFLAGS.AC = 0 or the access implicitly accesses a system data structure.

Following are the rules of LASS violation check on the linear address(LA). User access to supervisor-mode address space:

     LA[bit 63] && (CPL == 3)
Supervisor access to user-mode address space:
     Instruction fetch: !LA[bit 63] && (CPL < 3)
     Data access: !LA[bit 63] && (CR4.SMAP==1) && ((RFLAGS.AC == 0 &&
                  CPL < 3) || Implicit supervisor access)

Add new ops in kvm_x86_ops to do LASS violation check.

---
  arch/x86/include/asm/kvm-x86-ops.h |  3 +-
  arch/x86/include/asm/kvm_host.h    |  2 ++
  arch/x86/kvm/kvm_emulate.h         |  1 +
  arch/x86/kvm/vmx/vmx.c             | 47 ++++++++++++++++++++++++++++++
  arch/x86/kvm/vmx/vmx.h             |  2 ++
  5 files changed, 54 insertions(+), 1 deletion(-)
arch/x86/include/asm/kvm-x86-ops.h +2 -1
index 13bc212cd4bc..8980a3bfa687 100644
@@ -132,7 +132,8 @@ KVM_X86_OP_OPTIONAL(migrate_timers)
  KVM_X86_OP(msr_filter_changed)
  KVM_X86_OP(complete_emulated_msr)
  KVM_X86_OP(vcpu_deliver_sipi_vector)
-KVM_X86_OP_OPTIONAL_RET0(vcpu_get_apicv_inhibit_reasons);
+KVM_X86_OP_OPTIONAL_RET0(vcpu_get_apicv_inhibit_reasons)
+KVM_X86_OP_OPTIONAL_RET0(check_lass)
 
  #undef KVM_X86_OP
  #undef KVM_X86_OP_OPTIONAL
arch/x86/include/asm/kvm_host.h +2 -0
index 92d8e65fe88c..98666d1e7727 100644
@@ -1731,6 +1731,8 @@ struct kvm_x86_ops {
  	 * Returns vCPU specific APICv inhibit reasons
  	 */
  	unsigned long (*vcpu_get_apicv_inhibit_reasons)(struct kvm_vcpu *vcpu);
+
+	bool (*check_lass)(struct kvm_vcpu *vcpu, u64 access, u64 la, u32 flags);
  };
 
  struct kvm_x86_nested_ops {
arch/x86/kvm/kvm_emulate.h +1 -0
index 5b9ec610b2cb..f1439ab7c14b 100644
@@ -91,6 +91,7 @@ struct x86_instruction_info {
  /* x86-specific emulation flags */
  #define X86EMUL_F_FETCH			BIT(0)
  #define X86EMUL_F_WRITE			BIT(1)
+#define X86EMUL_F_SKIPLASS		BIT(2)
 
  struct x86_emulate_ops {
  	void (*vm_bugged)(struct x86_emulate_ctxt *ctxt);
arch/x86/kvm/vmx/vmx.c +9 -0
index a33205ded85c..876997e8448e 100644
@@ -8130,6 +8130,51 @@ static void vmx_vm_destroy(struct kvm *kvm)
  	free_pages((unsigned long)kvm_vmx->pid_table, vmx_get_pid_table_order(kvm));
  }
 
+/*
+ * Determine whether an access to the linear address causes a LASS violation.
+ * LASS protection is only effective in long mode. As a prerequisite, caller
+ * should make sure vCPU running in long mode and invoke this api to do LASS
+ * violation check.
+ */
+bool vmx_check_lass(struct kvm_vcpu *vcpu, u64 access, u64 la, u32 flags)
+{
+	bool user_mode, user_as, rflags_ac;
Quoted 9 lines

arch/x86/kvm/vmx/vmx.c static void vmx_vm_destroy(struct kvm *kvm) · line 8147

Quoted 6 lines
arch/x86/kvm/vmx/vmx.c +6 -0
+
+	if (!!(flags & X86EMUL_F_SKIPLASS) ||
+	    !kvm_is_cr4_bit_set(vcpu, X86_CR4_LASS))
+		return false;
+
+	WARN_ON_ONCE(!is_long_mode(vcpu));

IMHO, it's better to skip the following checks and return false if it is out of long mode.

The check of long mode is in the caller implemented in in the next patch. :)

+ if (!is_long_mode(vcpu))
+ return false;

Quoted 62 lines

arch/x86/kvm/vmx/vmx.c static void vmx_vm_destroy(struct kvm *kvm) · line 8149

Quoted 2 lines
arch/x86/kvm/vmx/vmx.c +2 -0
+
+	user_as = !(la >> 63);

It's better to describe how LASS treat linear address in compatibility mode in changelog or/and in comment,
i.e. for a linear address with only 32 bits (or 16 bits), the processor treats bit 63 as if it were 0.

Quoted 53 lines
arch/x86/kvm/vmx/vmx.c +30 -0
+
+	/*
+	 * An access is a supervisor-mode access if CPL < 3 or if it implicitly
+	 * accesses a system data structure. For implicit accesses to system
+	 * data structure, the processor acts as if RFLAGS.AC is clear.
+	 */
+	if (access & PFERR_IMPLICIT_ACCESS) {
+		user_mode = false;
+		rflags_ac = false;
+	} else {
+		user_mode = vmx_get_cpl(vcpu) == 3;
+		if (!user_mode)
+			rflags_ac = !!(kvm_get_rflags(vcpu) & X86_EFLAGS_AC);
+	}
+
+	if (user_mode == user_as)
+		return false;
+
+	/*
+	 * Supervisor-mode _data_ accesses to user address space
+	 * cause LASS violations only if SMAP is enabled.
+	 */
+	if (!user_mode && !(access & PFERR_FETCH_MASK))
+		return kvm_is_cr4_bit_set(vcpu, X86_CR4_SMAP) && !rflags_ac;
+
+	return true;
+}
+
  static struct kvm_x86_ops vmx_x86_ops __initdata = {
  	.name = KBUILD_MODNAME,
 
@@ -8269,6 +8314,8 @@ static struct kvm_x86_ops vmx_x86_ops __initdata = {
  	.complete_emulated_msr = kvm_complete_insn_gp,
 
  	.vcpu_deliver_sipi_vector = kvm_vcpu_deliver_sipi_vector,
+
+	.check_lass = vmx_check_lass,
  };
 
  static unsigned int vmx_handle_intel_pt_intr(void)
arch/x86/kvm/vmx/vmx.h +2 -0
index 9e66531861cf..f2e775b9849b 100644
@@ -433,6 +433,8 @@ void vmx_enable_intercept_for_msr(struct kvm_vcpu *vcpu, u32 msr, int type);
  u64 vmx_get_l2_tsc_offset(struct kvm_vcpu *vcpu);
  u64 vmx_get_l2_tsc_multiplier(struct kvm_vcpu *vcpu);
 
+bool vmx_check_lass(struct kvm_vcpu *vcpu, u64 access, u64 la, u32 flags);
+
  static inline void vmx_set_intercept_for_msr(struct kvm_vcpu *vcpu, u32 msr,
  					     int type, bool value)
  {
#14 lore raw

Re: [PATCH v1 3/6] KVM: VMX: Add new ops in kvm_x86_ops for LASS violation check

Quoted 108 lines · Zhi Wang
Quoted 94 lines · On Mon, 5 Jun 2023 11:31:48 +0800 Binbin Wu <[email protected]> wrote:
Quoted 1 line · Zeng Guang

Intel introduces LASS (Linear Address Separation) feature providing

                      ^
 missing "Space" here

Quoted 89 lines

an independent mechanism to achieve the mode-based protection.

LASS partitions 64-bit linear address space into two halves, user-mode address (LA[bit 63]=0) and supervisor-mode address (LA[bit 63]=1). It stops any code execution or conditional data access[1]
1. from user mode to supervisor-mode address space
2. from supervisor mode to user-mode address space
and generates LASS violation fault accordingly.

[1]A supervisor mode data access causes a LASS violation only if supervisor mode access protection is enabled (CR4.SMAP = 1) and either RFLAGS.AC = 0 or the access implicitly accesses a system data structure.

Following are the rules of LASS violation check on the linear address(LA). User access to supervisor-mode address space:

      LA[bit 63] && (CPL == 3)
Supervisor access to user-mode address space:
      Instruction fetch: !LA[bit 63] && (CPL < 3)
      Data access: !LA[bit 63] && (CR4.SMAP==1) && ((RFLAGS.AC == 0 &&
                   CPL < 3) || Implicit supervisor access)

Add new ops in kvm_x86_ops to do LASS violation check.

---
   arch/x86/include/asm/kvm-x86-ops.h |  3 +-
   arch/x86/include/asm/kvm_host.h    |  2 ++
   arch/x86/kvm/kvm_emulate.h         |  1 +
   arch/x86/kvm/vmx/vmx.c             | 47 ++++++++++++++++++++++++++++++
   arch/x86/kvm/vmx/vmx.h             |  2 ++
   5 files changed, 54 insertions(+), 1 deletion(-)
arch/x86/include/asm/kvm-x86-ops.h +2 -1
index 13bc212cd4bc..8980a3bfa687 100644
@@ -132,7 +132,8 @@ KVM_X86_OP_OPTIONAL(migrate_timers)
   KVM_X86_OP(msr_filter_changed)
   KVM_X86_OP(complete_emulated_msr)
   KVM_X86_OP(vcpu_deliver_sipi_vector)
-KVM_X86_OP_OPTIONAL_RET0(vcpu_get_apicv_inhibit_reasons);
+KVM_X86_OP_OPTIONAL_RET0(vcpu_get_apicv_inhibit_reasons)
+KVM_X86_OP_OPTIONAL_RET0(check_lass)
 
   #undef KVM_X86_OP
   #undef KVM_X86_OP_OPTIONAL
arch/x86/include/asm/kvm_host.h +2 -0
index 92d8e65fe88c..98666d1e7727 100644
@@ -1731,6 +1731,8 @@ struct kvm_x86_ops {
   	 * Returns vCPU specific APICv inhibit reasons
   	 */
   	unsigned long (*vcpu_get_apicv_inhibit_reasons)(struct kvm_vcpu *vcpu);
+
+	bool (*check_lass)(struct kvm_vcpu *vcpu, u64 access, u64 la, u32 flags);
   };
 
   struct kvm_x86_nested_ops {
arch/x86/kvm/kvm_emulate.h +1 -0
index 5b9ec610b2cb..f1439ab7c14b 100644
@@ -91,6 +91,7 @@ struct x86_instruction_info {
   /* x86-specific emulation flags */
   #define X86EMUL_F_FETCH			BIT(0)
   #define X86EMUL_F_WRITE			BIT(1)
+#define X86EMUL_F_SKIPLASS		BIT(2)
 
   struct x86_emulate_ops {
   	void (*vm_bugged)(struct x86_emulate_ctxt *ctxt);
arch/x86/kvm/vmx/vmx.c +9 -0
index a33205ded85c..876997e8448e 100644
@@ -8130,6 +8130,51 @@ static void vmx_vm_destroy(struct kvm *kvm)
   	free_pages((unsigned long)kvm_vmx->pid_table, vmx_get_pid_table_order(kvm));
   }
 
+/*
+ * Determine whether an access to the linear address causes a LASS violation.
+ * LASS protection is only effective in long mode. As a prerequisite, caller
+ * should make sure vCPU running in long mode and invoke this api to do LASS
+ * violation check.
+ */
+bool vmx_check_lass(struct kvm_vcpu *vcpu, u64 access, u64 la, u32 flags)
+{
+	bool user_mode, user_as, rflags_ac;
Quoted 9 lines

arch/x86/kvm/vmx/vmx.c static void vmx_vm_destroy(struct kvm *kvm) · line 8147

Quoted 6 lines
arch/x86/kvm/vmx/vmx.c +6 -0
+
+	if (!!(flags & X86EMUL_F_SKIPLASS) ||
+	    !kvm_is_cr4_bit_set(vcpu, X86_CR4_LASS))
+		return false;
+
+	WARN_ON_ONCE(!is_long_mode(vcpu));

IMHO, it's better to skip the following checks and return false if it is out of long mode.

The check of long mode is in the caller implemented in in the next patch. :)

Review

Quoted 2 lines
(quoted diff) +2 -0
+	if (!is_long_mode(vcpu))
+		return false;

I know the callers have checked the mode, however, IMHO, it's better as following:

+ if (!!(flags & X86EMUL_F_SKIPLASS) ||
+ !kvm_is_cr4_bit_set(vcpu, X86_CR4_LASS) || WARN_ON_ONCE(!is_long_mode(vcpu)))
+ return false;

Quoted 61 lines
Quoted 61 lines

arch/x86/kvm/vmx/vmx.c static void vmx_vm_destroy(struct kvm *kvm) · line 8149

Quoted 2 lines
arch/x86/kvm/vmx/vmx.c +2 -0
+
+	user_as = !(la >> 63);

It's better to describe how LASS treat linear address in compatibility mode in changelog or/and in comment,
i.e. for a linear address with only 32 bits (or 16 bits), the processor treats bit 63 as if it were 0.

Quoted 53 lines
arch/x86/kvm/vmx/vmx.c +30 -0
+
+	/*
+	 * An access is a supervisor-mode access if CPL < 3 or if it implicitly
+	 * accesses a system data structure. For implicit accesses to system
+	 * data structure, the processor acts as if RFLAGS.AC is clear.
+	 */
+	if (access & PFERR_IMPLICIT_ACCESS) {
+		user_mode = false;
+		rflags_ac = false;
+	} else {
+		user_mode = vmx_get_cpl(vcpu) == 3;
+		if (!user_mode)
+			rflags_ac = !!(kvm_get_rflags(vcpu) & X86_EFLAGS_AC);
+	}
+
+	if (user_mode == user_as)
+		return false;
+
+	/*
+	 * Supervisor-mode _data_ accesses to user address space
+	 * cause LASS violations only if SMAP is enabled.
+	 */
+	if (!user_mode && !(access & PFERR_FETCH_MASK))
+		return kvm_is_cr4_bit_set(vcpu, X86_CR4_SMAP) && !rflags_ac;
+
+	return true;
+}
+
   static struct kvm_x86_ops vmx_x86_ops __initdata = {
   	.name = KBUILD_MODNAME,
 
@@ -8269,6 +8314,8 @@ static struct kvm_x86_ops vmx_x86_ops __initdata = {
   	.complete_emulated_msr = kvm_complete_insn_gp,
 
   	.vcpu_deliver_sipi_vector = kvm_vcpu_deliver_sipi_vector,
+
+	.check_lass = vmx_check_lass,
   };
 
   static unsigned int vmx_handle_intel_pt_intr(void)
arch/x86/kvm/vmx/vmx.h +2 -0
index 9e66531861cf..f2e775b9849b 100644
@@ -433,6 +433,8 @@ void vmx_enable_intercept_for_msr(struct kvm_vcpu *vcpu, u32 msr, int type);
   u64 vmx_get_l2_tsc_offset(struct kvm_vcpu *vcpu);
   u64 vmx_get_l2_tsc_multiplier(struct kvm_vcpu *vcpu);
 
+bool vmx_check_lass(struct kvm_vcpu *vcpu, u64 access, u64 la, u32 flags);
+
   static inline void vmx_set_intercept_for_msr(struct kvm_vcpu *vcpu, u32 msr,
   					     int type, bool value)
   {
#15 lore raw

Re: [PATCH v1 3/6] KVM: VMX: Add new ops in kvm_x86_ops for LASS violation check

Quoted 117 lines · On Tue, 6 Jun 2023 10:57:23 +0800 Binbin Wu <[email protected]> wrote:
Quoted 108 lines · Zhi Wang
Quoted 94 lines · On Mon, 5 Jun 2023 11:31:48 +0800 Binbin Wu <[email protected]> wrote:
Quoted 1 line · Zeng Guang

Intel introduces LASS (Linear Address Separation) feature providing

                      ^
 missing "Space" here

Quoted 89 lines

an independent mechanism to achieve the mode-based protection.

LASS partitions 64-bit linear address space into two halves, user-mode address (LA[bit 63]=0) and supervisor-mode address (LA[bit 63]=1). It stops any code execution or conditional data access[1]
1. from user mode to supervisor-mode address space
2. from supervisor mode to user-mode address space
and generates LASS violation fault accordingly.

[1]A supervisor mode data access causes a LASS violation only if supervisor mode access protection is enabled (CR4.SMAP = 1) and either RFLAGS.AC = 0 or the access implicitly accesses a system data structure.

Following are the rules of LASS violation check on the linear address(LA). User access to supervisor-mode address space:

      LA[bit 63] && (CPL == 3)
Supervisor access to user-mode address space:
      Instruction fetch: !LA[bit 63] && (CPL < 3)
      Data access: !LA[bit 63] && (CR4.SMAP==1) && ((RFLAGS.AC == 0 &&
                   CPL < 3) || Implicit supervisor access)

Add new ops in kvm_x86_ops to do LASS violation check.

---
   arch/x86/include/asm/kvm-x86-ops.h |  3 +-
   arch/x86/include/asm/kvm_host.h    |  2 ++
   arch/x86/kvm/kvm_emulate.h         |  1 +
   arch/x86/kvm/vmx/vmx.c             | 47 ++++++++++++++++++++++++++++++
   arch/x86/kvm/vmx/vmx.h             |  2 ++
   5 files changed, 54 insertions(+), 1 deletion(-)
arch/x86/include/asm/kvm-x86-ops.h +2 -1
index 13bc212cd4bc..8980a3bfa687 100644
@@ -132,7 +132,8 @@ KVM_X86_OP_OPTIONAL(migrate_timers)
   KVM_X86_OP(msr_filter_changed)
   KVM_X86_OP(complete_emulated_msr)
   KVM_X86_OP(vcpu_deliver_sipi_vector)
-KVM_X86_OP_OPTIONAL_RET0(vcpu_get_apicv_inhibit_reasons);
+KVM_X86_OP_OPTIONAL_RET0(vcpu_get_apicv_inhibit_reasons)
+KVM_X86_OP_OPTIONAL_RET0(check_lass)
 
   #undef KVM_X86_OP
   #undef KVM_X86_OP_OPTIONAL
arch/x86/include/asm/kvm_host.h +2 -0
index 92d8e65fe88c..98666d1e7727 100644
@@ -1731,6 +1731,8 @@ struct kvm_x86_ops {
   	 * Returns vCPU specific APICv inhibit reasons
   	 */
   	unsigned long (*vcpu_get_apicv_inhibit_reasons)(struct kvm_vcpu *vcpu);
+
+	bool (*check_lass)(struct kvm_vcpu *vcpu, u64 access, u64 la, u32 flags);
   };
 
   struct kvm_x86_nested_ops {
arch/x86/kvm/kvm_emulate.h +1 -0
index 5b9ec610b2cb..f1439ab7c14b 100644
@@ -91,6 +91,7 @@ struct x86_instruction_info {
   /* x86-specific emulation flags */
   #define X86EMUL_F_FETCH			BIT(0)
   #define X86EMUL_F_WRITE			BIT(1)
+#define X86EMUL_F_SKIPLASS		BIT(2)
 
   struct x86_emulate_ops {
   	void (*vm_bugged)(struct x86_emulate_ctxt *ctxt);
arch/x86/kvm/vmx/vmx.c +9 -0
index a33205ded85c..876997e8448e 100644
@@ -8130,6 +8130,51 @@ static void vmx_vm_destroy(struct kvm *kvm)
   	free_pages((unsigned long)kvm_vmx->pid_table, vmx_get_pid_table_order(kvm));
   }
 
+/*
+ * Determine whether an access to the linear address causes a LASS violation.
+ * LASS protection is only effective in long mode. As a prerequisite, caller
+ * should make sure vCPU running in long mode and invoke this api to do LASS
+ * violation check.
+ */
+bool vmx_check_lass(struct kvm_vcpu *vcpu, u64 access, u64 la, u32 flags)
+{
+	bool user_mode, user_as, rflags_ac;
Quoted 9 lines

arch/x86/kvm/vmx/vmx.c static void vmx_vm_destroy(struct kvm *kvm) · line 8147

Quoted 6 lines
arch/x86/kvm/vmx/vmx.c +6 -0
+
+	if (!!(flags & X86EMUL_F_SKIPLASS) ||
+	    !kvm_is_cr4_bit_set(vcpu, X86_CR4_LASS))
+		return false;
+
+	WARN_ON_ONCE(!is_long_mode(vcpu));

IMHO, it's better to skip the following checks and return false if it is out of long mode.

The check of long mode is in the caller implemented in in the next patch. :)

Review

Quoted 2 lines
(quoted diff) +2 -0
+	if (!is_long_mode(vcpu))
+		return false;

I know the callers have checked the mode, however, IMHO, it's better as following:

arch/x86/kvm/vmx/vmx.c static void vmx_vm_destroy(struct kvm *kvm) · line 8145

Quoted 3 lines
arch/x86/kvm/vmx/vmx.c +3 -0
+	if (!!(flags & X86EMUL_F_SKIPLASS) ||
+	    !kvm_is_cr4_bit_set(vcpu, X86_CR4_LASS) || WARN_ON_ONCE(!is_long_mode(vcpu)))
+		return false;

Uh. I see. LGTM.

Quoted 64 lines
Quoted 63 lines
Quoted 63 lines

arch/x86/kvm/vmx/vmx.c static void vmx_vm_destroy(struct kvm *kvm) · line 8149

Quoted 4 lines
arch/x86/kvm/vmx/vmx.c +2 -0
+
+	user_as = !(la >> 63);

It's better to describe how LASS treat linear address in compatibility mode in changelog or/and in comment,
i.e. for a linear address with only 32 bits (or 16 bits), the processor treats bit 63 as if it were 0.

Quoted 53 lines
arch/x86/kvm/vmx/vmx.c +30 -0
+
+	/*
+	 * An access is a supervisor-mode access if CPL < 3 or if it implicitly
+	 * accesses a system data structure. For implicit accesses to system
+	 * data structure, the processor acts as if RFLAGS.AC is clear.
+	 */
+	if (access & PFERR_IMPLICIT_ACCESS) {
+		user_mode = false;
+		rflags_ac = false;
+	} else {
+		user_mode = vmx_get_cpl(vcpu) == 3;
+		if (!user_mode)
+			rflags_ac = !!(kvm_get_rflags(vcpu) & X86_EFLAGS_AC);
+	}
+
+	if (user_mode == user_as)
+		return false;
+
+	/*
+	 * Supervisor-mode _data_ accesses to user address space
+	 * cause LASS violations only if SMAP is enabled.
+	 */
+	if (!user_mode && !(access & PFERR_FETCH_MASK))
+		return kvm_is_cr4_bit_set(vcpu, X86_CR4_SMAP) && !rflags_ac;
+
+	return true;
+}
+
   static struct kvm_x86_ops vmx_x86_ops __initdata = {
   	.name = KBUILD_MODNAME,
 
@@ -8269,6 +8314,8 @@ static struct kvm_x86_ops vmx_x86_ops __initdata = {
   	.complete_emulated_msr = kvm_complete_insn_gp,
 
   	.vcpu_deliver_sipi_vector = kvm_vcpu_deliver_sipi_vector,
+
+	.check_lass = vmx_check_lass,
   };
 
   static unsigned int vmx_handle_intel_pt_intr(void)
arch/x86/kvm/vmx/vmx.h +2 -0
index 9e66531861cf..f2e775b9849b 100644
@@ -433,6 +433,8 @@ void vmx_enable_intercept_for_msr(struct kvm_vcpu *vcpu, u32 msr, int type);
   u64 vmx_get_l2_tsc_offset(struct kvm_vcpu *vcpu);
   u64 vmx_get_l2_tsc_multiplier(struct kvm_vcpu *vcpu);
 
+bool vmx_check_lass(struct kvm_vcpu *vcpu, u64 access, u64 la, u32 flags);
+
   static inline void vmx_set_intercept_for_msr(struct kvm_vcpu *vcpu, u32 msr,
   					     int type, bool value)
   {
author
#16 lore raw

Re: [PATCH v1 3/6] KVM: VMX: Add new ops in kvm_x86_ops for LASS violation check

In reply to Binbin Wu
Quoted 5 lines · Binbin Wu
Quoted 1 line · Zeng Guang

Intel introduces LASS (Linear Address Separation) feature providing

                      ^
 missing "Space" here

Thanks.

Quoted 89 lines
Quoted 89 lines

an independent mechanism to achieve the mode-based protection.

LASS partitions 64-bit linear address space into two halves, user-mode address (LA[bit 63]=0) and supervisor-mode address (LA[bit 63]=1). It stops any code execution or conditional data access[1]
1. from user mode to supervisor-mode address space
2. from supervisor mode to user-mode address space
and generates LASS violation fault accordingly.

[1]A supervisor mode data access causes a LASS violation only if supervisor mode access protection is enabled (CR4.SMAP = 1) and either RFLAGS.AC = 0 or the access implicitly accesses a system data structure.

Following are the rules of LASS violation check on the linear address(LA). User access to supervisor-mode address space:

      LA[bit 63] && (CPL == 3)
Supervisor access to user-mode address space:
      Instruction fetch: !LA[bit 63] && (CPL < 3)
      Data access: !LA[bit 63] && (CR4.SMAP==1) && ((RFLAGS.AC == 0 &&
                   CPL < 3) || Implicit supervisor access)

Add new ops in kvm_x86_ops to do LASS violation check.

---
   arch/x86/include/asm/kvm-x86-ops.h |  3 +-
   arch/x86/include/asm/kvm_host.h    |  2 ++
   arch/x86/kvm/kvm_emulate.h         |  1 +
   arch/x86/kvm/vmx/vmx.c             | 47 ++++++++++++++++++++++++++++++
   arch/x86/kvm/vmx/vmx.h             |  2 ++
   5 files changed, 54 insertions(+), 1 deletion(-)
arch/x86/include/asm/kvm-x86-ops.h +2 -1
index 13bc212cd4bc..8980a3bfa687 100644
@@ -132,7 +132,8 @@ KVM_X86_OP_OPTIONAL(migrate_timers)
   KVM_X86_OP(msr_filter_changed)
   KVM_X86_OP(complete_emulated_msr)
   KVM_X86_OP(vcpu_deliver_sipi_vector)
-KVM_X86_OP_OPTIONAL_RET0(vcpu_get_apicv_inhibit_reasons);
+KVM_X86_OP_OPTIONAL_RET0(vcpu_get_apicv_inhibit_reasons)
+KVM_X86_OP_OPTIONAL_RET0(check_lass)
 
   #undef KVM_X86_OP
   #undef KVM_X86_OP_OPTIONAL
arch/x86/include/asm/kvm_host.h +2 -0
index 92d8e65fe88c..98666d1e7727 100644
@@ -1731,6 +1731,8 @@ struct kvm_x86_ops {
   	 * Returns vCPU specific APICv inhibit reasons
   	 */
   	unsigned long (*vcpu_get_apicv_inhibit_reasons)(struct kvm_vcpu *vcpu);
+
+	bool (*check_lass)(struct kvm_vcpu *vcpu, u64 access, u64 la, u32 flags);
   };
 
   struct kvm_x86_nested_ops {
arch/x86/kvm/kvm_emulate.h +1 -0
index 5b9ec610b2cb..f1439ab7c14b 100644
@@ -91,6 +91,7 @@ struct x86_instruction_info {
   /* x86-specific emulation flags */
   #define X86EMUL_F_FETCH			BIT(0)
   #define X86EMUL_F_WRITE			BIT(1)
+#define X86EMUL_F_SKIPLASS		BIT(2)
 
   struct x86_emulate_ops {
   	void (*vm_bugged)(struct x86_emulate_ctxt *ctxt);
arch/x86/kvm/vmx/vmx.c +9 -0
index a33205ded85c..876997e8448e 100644
@@ -8130,6 +8130,51 @@ static void vmx_vm_destroy(struct kvm *kvm)
   	free_pages((unsigned long)kvm_vmx->pid_table, vmx_get_pid_table_order(kvm));
   }
 
+/*
+ * Determine whether an access to the linear address causes a LASS violation.
+ * LASS protection is only effective in long mode. As a prerequisite, caller
+ * should make sure vCPU running in long mode and invoke this api to do LASS
+ * violation check.
+ */
+bool vmx_check_lass(struct kvm_vcpu *vcpu, u64 access, u64 la, u32 flags)
+{
+	bool user_mode, user_as, rflags_ac;
Quoted 8 lines

arch/x86/kvm/vmx/vmx.c static void vmx_vm_destroy(struct kvm *kvm) · line 8147

Quoted 6 lines
arch/x86/kvm/vmx/vmx.c +6 -0
+
+	if (!!(flags & X86EMUL_F_SKIPLASS) ||
+	    !kvm_is_cr4_bit_set(vcpu, X86_CR4_LASS))
+		return false;
+
+	WARN_ON_ONCE(!is_long_mode(vcpu));

IMHO, it's better to skip the following checks and return false if it is out of long mode.

In some cases , cpu mode check already exists before invoking LASS violation detect, e.g. vmx instruction emulation. So it's designed to make vmx_check_lass() focusing on LASS violation alone, and leave it to caller taking care of cpu mode in advance.

The purpose is to avoid duplicating cpu mode check though the impact seems not significant. :)

Quoted 7 lines

arch/x86/kvm/vmx/vmx.c static void vmx_vm_destroy(struct kvm *kvm) · line 8149

Quoted 2 lines
arch/x86/kvm/vmx/vmx.c +2 -0
+
+	user_as = !(la >> 63);

It's better to describe how LASS treat linear address in compatibility mode in changelog or/and in comment,
i.e. for a linear address with only 32 bits (or 16 bits), the processor treats bit 63 as if it were 0.

OK, will add comments on specific treatment in compatibility mode.

Quoted 53 lines
Quoted 53 lines
arch/x86/kvm/vmx/vmx.c +30 -0
+
+	/*
+	 * An access is a supervisor-mode access if CPL < 3 or if it implicitly
+	 * accesses a system data structure. For implicit accesses to system
+	 * data structure, the processor acts as if RFLAGS.AC is clear.
+	 */
+	if (access & PFERR_IMPLICIT_ACCESS) {
+		user_mode = false;
+		rflags_ac = false;
+	} else {
+		user_mode = vmx_get_cpl(vcpu) == 3;
+		if (!user_mode)
+			rflags_ac = !!(kvm_get_rflags(vcpu) & X86_EFLAGS_AC);
+	}
+
+	if (user_mode == user_as)
+		return false;
+
+	/*
+	 * Supervisor-mode _data_ accesses to user address space
+	 * cause LASS violations only if SMAP is enabled.
+	 */
+	if (!user_mode && !(access & PFERR_FETCH_MASK))
+		return kvm_is_cr4_bit_set(vcpu, X86_CR4_SMAP) && !rflags_ac;
+
+	return true;
+}
+
   static struct kvm_x86_ops vmx_x86_ops __initdata = {
   	.name = KBUILD_MODNAME,
 
@@ -8269,6 +8314,8 @@ static struct kvm_x86_ops vmx_x86_ops __initdata = {
   	.complete_emulated_msr = kvm_complete_insn_gp,
 
   	.vcpu_deliver_sipi_vector = kvm_vcpu_deliver_sipi_vector,
+
+	.check_lass = vmx_check_lass,
   };
 
   static unsigned int vmx_handle_intel_pt_intr(void)
arch/x86/kvm/vmx/vmx.h +2 -0
index 9e66531861cf..f2e775b9849b 100644
@@ -433,6 +433,8 @@ void vmx_enable_intercept_for_msr(struct kvm_vcpu *vcpu, u32 msr, int type);
   u64 vmx_get_l2_tsc_offset(struct kvm_vcpu *vcpu);
   u64 vmx_get_l2_tsc_multiplier(struct kvm_vcpu *vcpu);
 
+bool vmx_check_lass(struct kvm_vcpu *vcpu, u64 access, u64 la, u32 flags);
+
   static inline void vmx_set_intercept_for_msr(struct kvm_vcpu *vcpu, u32 msr,
   					     int type, bool value)
   {
#17 lore raw

Re: [PATCH v1 3/6] KVM: VMX: Add new ops in kvm_x86_ops for LASS violation check

In reply to Zeng Guang

arch/x86/include/asm/kvm_host.h struct kvm_x86_ops { · line 1735

Quoted 10 lines · Zeng Guang
arch/x86/include/asm/kvm_host.h +2 -0
index 92d8e65fe88c..98666d1e7727 100644
@@ -1731,6 +1731,8 @@ struct kvm_x86_ops {
 	 * Returns vCPU specific APICv inhibit reasons
 	 */
 	unsigned long (*vcpu_get_apicv_inhibit_reasons)(struct kvm_vcpu *vcpu);
+
+	bool (*check_lass)(struct kvm_vcpu *vcpu, u64 access, u64 la, u32 flags);

It is better to declare the @la as gva_t since the address is a virtual address.

Both @access and @flags provide additional informaiton about a memory access. I think we can drop one of them e.g. adding a new bit X86EMUL_F_IMPLICIT_ACCESS.

Or maybe in the first place, we can just extend PFERR_? for SKIP_LASS/LAM behavior instead of adding another set of flags (X86EMUL_F_?). The benefit of adding new flags is they won't collide with future hardware extensions. I am not sure.

author
#18 lore raw

Re: [PATCH v1 3/6] KVM: VMX: Add new ops in kvm_x86_ops for LASS violation check

Quoted 13 lines · Chao

arch/x86/include/asm/kvm_host.h struct kvm_x86_ops { · line 1735

Quoted 10 lines · Zeng Guang
arch/x86/include/asm/kvm_host.h +2 -0
index 92d8e65fe88c..98666d1e7727 100644
@@ -1731,6 +1731,8 @@ struct kvm_x86_ops {
 	 * Returns vCPU specific APICv inhibit reasons
 	 */
 	unsigned long (*vcpu_get_apicv_inhibit_reasons)(struct kvm_vcpu *vcpu);
+
+	bool (*check_lass)(struct kvm_vcpu *vcpu, u64 access, u64 la, u32 flags);

It is better to declare the @la as gva_t since the address is a virtual address.

Quoted 7 lines

Both @access and @flags provide additional informaiton about a memory access. I think we can drop one of them e.g. adding a new bit X86EMUL_F_IMPLICIT_ACCESS.

Or maybe in the first place, we can just extend PFERR_? for SKIP_LASS/LAM behavior instead of adding another set of flags (X86EMUL_F_?). The benefit of adding new flags is they won't collide with future hardware extensions. I am not sure.

Make sense. Prefer to adding a new bit of X86EMUL flags. PFERR_ is used for page fault case and actually not proper to be taken for LASS/LAM usage.

#19 lore raw

Re: [PATCH v1 3/6] KVM: VMX: Add new ops in kvm_x86_ops for LASS violation check

In reply to Zeng Guang
Quoted 104 lines · Zeng Guang

Intel introduces LASS (Linear Address Separation) feature providing an independent mechanism to achieve the mode-based protection.

LASS partitions 64-bit linear address space into two halves, user-mode address (LA[bit 63]=0) and supervisor-mode address (LA[bit 63]=1). It stops any code execution or conditional data access[1]
1. from user mode to supervisor-mode address space
2. from supervisor mode to user-mode address space
and generates LASS violation fault accordingly.

[1]A supervisor mode data access causes a LASS violation only if supervisor mode access protection is enabled (CR4.SMAP = 1) and either RFLAGS.AC = 0 or the access implicitly accesses a system data structure.

Following are the rules of LASS violation check on the linear address(LA). User access to supervisor-mode address space:

    LA[bit 63] && (CPL == 3)
Supervisor access to user-mode address space:
    Instruction fetch: !LA[bit 63] && (CPL < 3)
    Data access: !LA[bit 63] && (CR4.SMAP==1) && ((RFLAGS.AC == 0 &&
                 CPL < 3) || Implicit supervisor access)

Add new ops in kvm_x86_ops to do LASS violation check.

---
 arch/x86/include/asm/kvm-x86-ops.h |  3 +-
 arch/x86/include/asm/kvm_host.h    |  2 ++
 arch/x86/kvm/kvm_emulate.h         |  1 +
 arch/x86/kvm/vmx/vmx.c             | 47 ++++++++++++++++++++++++++++++
 arch/x86/kvm/vmx/vmx.h             |  2 ++
 5 files changed, 54 insertions(+), 1 deletion(-)
arch/x86/include/asm/kvm-x86-ops.h +2 -1
index 13bc212cd4bc..8980a3bfa687 100644
@@ -132,7 +132,8 @@ KVM_X86_OP_OPTIONAL(migrate_timers)
 KVM_X86_OP(msr_filter_changed)
 KVM_X86_OP(complete_emulated_msr)
 KVM_X86_OP(vcpu_deliver_sipi_vector)
-KVM_X86_OP_OPTIONAL_RET0(vcpu_get_apicv_inhibit_reasons);
+KVM_X86_OP_OPTIONAL_RET0(vcpu_get_apicv_inhibit_reasons)
+KVM_X86_OP_OPTIONAL_RET0(check_lass)
 
 #undef KVM_X86_OP
 #undef KVM_X86_OP_OPTIONAL
arch/x86/include/asm/kvm_host.h +2 -0
index 92d8e65fe88c..98666d1e7727 100644
@@ -1731,6 +1731,8 @@ struct kvm_x86_ops {
 	 * Returns vCPU specific APICv inhibit reasons
 	 */
 	unsigned long (*vcpu_get_apicv_inhibit_reasons)(struct kvm_vcpu *vcpu);
+
+	bool (*check_lass)(struct kvm_vcpu *vcpu, u64 access, u64 la, u32 flags);
 };
 
 struct kvm_x86_nested_ops {
arch/x86/kvm/kvm_emulate.h +1 -0
index 5b9ec610b2cb..f1439ab7c14b 100644
@@ -91,6 +91,7 @@ struct x86_instruction_info {
 /* x86-specific emulation flags */
 #define X86EMUL_F_FETCH			BIT(0)
 #define X86EMUL_F_WRITE			BIT(1)
+#define X86EMUL_F_SKIPLASS		BIT(2)
 
 struct x86_emulate_ops {
 	void (*vm_bugged)(struct x86_emulate_ctxt *ctxt);
arch/x86/kvm/vmx/vmx.c +23 -0
index a33205ded85c..876997e8448e 100644
@@ -8130,6 +8130,51 @@ static void vmx_vm_destroy(struct kvm *kvm)
 	free_pages((unsigned long)kvm_vmx->pid_table, vmx_get_pid_table_order(kvm));
 }
 
+/*
+ * Determine whether an access to the linear address causes a LASS violation.
+ * LASS protection is only effective in long mode. As a prerequisite, caller
+ * should make sure vCPU running in long mode and invoke this api to do LASS
+ * violation check.
+ */
+bool vmx_check_lass(struct kvm_vcpu *vcpu, u64 access, u64 la, u32 flags)
+{
+	bool user_mode, user_as, rflags_ac;
+
+	if (!!(flags & X86EMUL_F_SKIPLASS) ||
+	    !kvm_is_cr4_bit_set(vcpu, X86_CR4_LASS))
+		return false;
+
+	WARN_ON_ONCE(!is_long_mode(vcpu));
+
+	user_as = !(la >> 63);
+
+	/*
+	 * An access is a supervisor-mode access if CPL < 3 or if it implicitly
+	 * accesses a system data structure. For implicit accesses to system
+	 * data structure, the processor acts as if RFLAGS.AC is clear.
+	 */

arch/x86/kvm/vmx/vmx.c static void vmx_vm_destroy(struct kvm *kvm) · line 8165

Quoted 10 lines
arch/x86/kvm/vmx/vmx.c +10 -0
+	if (access & PFERR_IMPLICIT_ACCESS) {
+		user_mode = false;
+		rflags_ac = false;
+	} else {
+		user_mode = vmx_get_cpl(vcpu) == 3;
+		if (!user_mode)
+			rflags_ac = !!(kvm_get_rflags(vcpu) & X86_EFLAGS_AC);
+	}
+
+	if (user_mode == user_as)

Confused by user_as, it's role of address(U/S) so how about "user_addr" ? "if (user_mode == user_addr)" looks more clear to me.

Quoted 40 lines
arch/x86/kvm/vmx/vmx.c +14 -0
+		return false;
+
+	/*
+	 * Supervisor-mode _data_ accesses to user address space
+	 * cause LASS violations only if SMAP is enabled.
+	 */
+	if (!user_mode && !(access & PFERR_FETCH_MASK))
+		return kvm_is_cr4_bit_set(vcpu, X86_CR4_SMAP) && !rflags_ac;
+
+	return true;
+}
+
 static struct kvm_x86_ops vmx_x86_ops __initdata = {
 	.name = KBUILD_MODNAME,
 
@@ -8269,6 +8314,8 @@ static struct kvm_x86_ops vmx_x86_ops __initdata = {
 	.complete_emulated_msr = kvm_complete_insn_gp,
 
 	.vcpu_deliver_sipi_vector = kvm_vcpu_deliver_sipi_vector,
+
+	.check_lass = vmx_check_lass,
 };
 
 static unsigned int vmx_handle_intel_pt_intr(void)
arch/x86/kvm/vmx/vmx.h +2 -1
index 9e66531861cf..f2e775b9849b 100644
@@ -433,6 +433,8 @@ void vmx_enable_intercept_for_msr(struct kvm_vcpu *vcpu, u32 msr, int type);
 u64 vmx_get_l2_tsc_offset(struct kvm_vcpu *vcpu);
 u64 vmx_get_l2_tsc_multiplier(struct kvm_vcpu *vcpu);
 
+bool vmx_check_lass(struct kvm_vcpu *vcpu, u64 access, u64 la, u32 flags);
+
 static inline void vmx_set_intercept_for_msr(struct kvm_vcpu *vcpu, u32 msr,
 					     int type, bool value)
 {
--

2.27.0

author
#20 lore raw

Re: [PATCH v1 3/6] KVM: VMX: Add new ops in kvm_x86_ops for LASS violation check

Quoted 39 lines · Yuan Yao
Quoted 38 lines · Zeng Guang

Intel introduces LASS (Linear Address Separation) feature providing an independent mechanism to achieve the mode-based protection.

LASS partitions 64-bit linear address space into two halves, user-mode address (LA[bit 63]=0) and supervisor-mode address (LA[bit 63]=1). It stops any code execution or conditional data access[1]
1. from user mode to supervisor-mode address space
2. from supervisor mode to user-mode address space
and generates LASS violation fault accordingly.

arch/x86/kvm/vmx/vmx.c +28 -0
+/*
+ * Determine whether an access to the linear address causes a LASS violation.
+ * LASS protection is only effective in long mode. As a prerequisite, caller
+ * should make sure vCPU running in long mode and invoke this api to do LASS
+ * violation check.
+ */
+bool vmx_check_lass(struct kvm_vcpu *vcpu, u64 access, u64 la, u32 flags)
+{
+	bool user_mode, user_as, rflags_ac;
+
+	if (!!(flags & X86EMUL_F_SKIPLASS) ||
+	    !kvm_is_cr4_bit_set(vcpu, X86_CR4_LASS))
+		return false;
+
+	WARN_ON_ONCE(!is_long_mode(vcpu));
+
+	user_as = !(la >> 63);
+
+	/*
+	 * An access is a supervisor-mode access if CPL < 3 or if it implicitly
+	 * accesses a system data structure. For implicit accesses to system
+	 * data structure, the processor acts as if RFLAGS.AC is clear.
+	 */
+	if (access & PFERR_IMPLICIT_ACCESS) {
+		user_mode = false;
+		rflags_ac = false;
+	} else {
+		user_mode = vmx_get_cpl(vcpu) == 3;
Quoted 9 lines

arch/x86/kvm/vmx/vmx.c static void vmx_vm_destroy(struct kvm *kvm) · line 8165

Quoted 5 lines
arch/x86/kvm/vmx/vmx.c +5 -0
+		if (!user_mode)
+			rflags_ac = !!(kvm_get_rflags(vcpu) & X86_EFLAGS_AC);
+	}
+
+	if (user_mode == user_as)

Confused by user_as, it's role of address(U/S) so how about "user_addr" ? "if (user_mode == user_addr)" looks more clear to me.

Actually "as" stands for "address space". I suppose it more precise. :)

#21 lore raw

Re: [PATCH v1 3/6] KVM: VMX: Add new ops in kvm_x86_ops for LASS violation check

In reply to Zeng Guang

Similar to comments on an earlier patch, don't try to describe the literal code change, e.g. this does far more than just "Add new ops in kvm_x86_ops". Something like

KVM: VMX: Add emulation of LASS violation checks on linear address

Quoted 39 lines · Zeng Guang

Intel introduces LASS (Linear Address Separation) feature providing an independent mechanism to achieve the mode-based protection.

LASS partitions 64-bit linear address space into two halves, user-mode address (LA[bit 63]=0) and supervisor-mode address (LA[bit 63]=1). It stops any code execution or conditional data access[1]
1. from user mode to supervisor-mode address space
2. from supervisor mode to user-mode address space
and generates LASS violation fault accordingly.

[1]A supervisor mode data access causes a LASS violation only if supervisor mode access protection is enabled (CR4.SMAP = 1) and either RFLAGS.AC = 0 or the access implicitly accesses a system data structure.

Following are the rules of LASS violation check on the linear address(LA). User access to supervisor-mode address space:

    LA[bit 63] && (CPL == 3)
Supervisor access to user-mode address space:
    Instruction fetch: !LA[bit 63] && (CPL < 3)
    Data access: !LA[bit 63] && (CR4.SMAP==1) && ((RFLAGS.AC == 0 &&
                 CPL < 3) || Implicit supervisor access)

Add new ops in kvm_x86_ops to do LASS violation check.

---
 arch/x86/include/asm/kvm-x86-ops.h |  3 +-
 arch/x86/include/asm/kvm_host.h    |  2 ++
 arch/x86/kvm/kvm_emulate.h         |  1 +
 arch/x86/kvm/vmx/vmx.c             | 47 ++++++++++++++++++++++++++++++
 arch/x86/kvm/vmx/vmx.h             |  2 ++
 5 files changed, 54 insertions(+), 1 deletion(-)
arch/x86/include/asm/kvm-x86-ops.h +0 -0
index 13bc212cd4bc..8980a3bfa687 100644

arch/x86/include/asm/kvm-x86-ops.h KVM_X86_OP_OPTIONAL(migrate_timers) · line 136

Quoted 6 lines
arch/x86/include/asm/kvm-x86-ops.h +2 -1
@@ -132,7 +132,8 @@ KVM_X86_OP_OPTIONAL(migrate_timers)
 KVM_X86_OP(msr_filter_changed)
 KVM_X86_OP(complete_emulated_msr)
 KVM_X86_OP(vcpu_deliver_sipi_vector)
-KVM_X86_OP_OPTIONAL_RET0(vcpu_get_apicv_inhibit_reasons);
+KVM_X86_OP_OPTIONAL_RET0(vcpu_get_apicv_inhibit_reasons)
+KVM_X86_OP_OPTIONAL_RET0(check_lass)

Use "is_lass_violation" instead of "check_lass" for all function names. "check" doesn't convey that the function is a predicate, i.e. that it returns true/false.

Quoted 20 lines
arch/x86/include/asm/kvm-x86-ops.h +0 -0
 #undef KVM_X86_OP
 #undef KVM_X86_OP_OPTIONAL
arch/x86/include/asm/kvm_host.h +2 -0
index 92d8e65fe88c..98666d1e7727 100644
@@ -1731,6 +1731,8 @@ struct kvm_x86_ops {
 	 * Returns vCPU specific APICv inhibit reasons
 	 */
 	unsigned long (*vcpu_get_apicv_inhibit_reasons)(struct kvm_vcpu *vcpu);
+
+	bool (*check_lass)(struct kvm_vcpu *vcpu, u64 access, u64 la, u32 flags);
 };
 
 struct kvm_x86_nested_ops {
arch/x86/kvm/kvm_emulate.h +0 -0
index 5b9ec610b2cb..f1439ab7c14b 100644

arch/x86/kvm/kvm_emulate.h struct x86_instruction_info { · line 94

Quoted 4 lines
arch/x86/kvm/kvm_emulate.h +1 -0
@@ -91,6 +91,7 @@ struct x86_instruction_info {
 /* x86-specific emulation flags */
 #define X86EMUL_F_FETCH			BIT(0)
 #define X86EMUL_F_WRITE			BIT(1)
+#define X86EMUL_F_SKIPLASS		BIT(2)

This belongs in the patch that integrates LASS into the emulator. And rather than make the flag a command (SKIPLASS), I think it makes sense to make the flag describe the access. It'll mean more flags, but those are free. That way the originators of the accesses, e.g. em_invlpg(), don't need to document how they interact with LASS, e.g. this code is self-documenting, and if someone wants to understand why KVM has a dedicated X86EMUL_F_INVLPG flag, it's easy enough to find the consumer. And in the unlikely scenario that other things care about INVLPG, branch targets, etc., we won't end up with X86EMUL_F_SKIPLASS, X86EMUL_F_SKIPOTHER, etc.

	rc = __linearize(ctxt, ctxt->src.addr.mem, &max_size, 1,
			 X86EMUL_F_INVLPG, ctxt->mode, &linear);

So this?

  #define X86EMUL_F_IMPLICIT
  #define X86EMUL_F_INVLPG
  #define X86EMUL_F_BRANCH_TARGET
Quoted 15 lines
arch/x86/kvm/kvm_emulate.h +0 -0
 struct x86_emulate_ops {
 	void (*vm_bugged)(struct x86_emulate_ctxt *ctxt);
arch/x86/kvm/vmx/vmx.c +5 -0
index a33205ded85c..876997e8448e 100644
@@ -8130,6 +8130,51 @@ static void vmx_vm_destroy(struct kvm *kvm)
 	free_pages((unsigned long)kvm_vmx->pid_table, vmx_get_pid_table_order(kvm));
 }
 
+/*
+ * Determine whether an access to the linear address causes a LASS violation.
+ * LASS protection is only effective in long mode. As a prerequisite, caller
+ * should make sure vCPU running in long mode and invoke this api to do LASS
+ * violation check.

arch/x86/kvm/vmx/vmx.c static void vmx_vm_destroy(struct kvm *kvm) · line 8147

Quoted 10 lines
arch/x86/kvm/vmx/vmx.c +10 -0
+ */
+bool vmx_check_lass(struct kvm_vcpu *vcpu, u64 access, u64 la, u32 flags)
+{
+	bool user_mode, user_as, rflags_ac;
+
+	if (!!(flags & X86EMUL_F_SKIPLASS) ||
+	    !kvm_is_cr4_bit_set(vcpu, X86_CR4_LASS))
+		return false;
+
+	WARN_ON_ONCE(!is_long_mode(vcpu));

This is silly. By making this a preqreq, 2 of the 3 callers are forced to explicitly check for is_long_mode(), *and* it unnecessarily bleeds LASS details outside of VMX.

arch/x86/kvm/vmx/vmx.c static void vmx_vm_destroy(struct kvm *kvm) · line 8156

Quoted 9 lines
arch/x86/kvm/vmx/vmx.c +9 -0
+
+	user_as = !(la >> 63);
+
+	/*
+	 * An access is a supervisor-mode access if CPL < 3 or if it implicitly
+	 * accesses a system data structure. For implicit accesses to system
+	 * data structure, the processor acts as if RFLAGS.AC is clear.
+	 */
+	if (access & PFERR_IMPLICIT_ACCESS) {

Please don't use PFERR_IMPLICIT_ACCESS, just extend the new flags. This is obviously not coming from a page fault. PFERR_IMPLICIT_ACCESS really shouldn't exist, but at least there was reasonable justification for adding it (changing all of the paths that lead to permission_fault() would have require a massive overhaul).

***HOWEVER***

I think the entire approach of hooking __linearize() may be a mistake, and LASS should instead be implemented in a wrapper of ->gva_to_gpa(). The two calls to __linearize() that are escaped with SKIPLASS are escaped *because* they don't actually access memory (branch targets and INVLPG), and so if LASS is enforced only when ->gva_to_gpa() is invoked, all of these new flags go away because the cases that ignore LASS are naturally handled.

That should also make it unnecessary to add one-off checks since e.g. kvm_handle_invpcid() will hit kvm_read_guest_virt() and thus ->gva_to_gpa(), i.e. we won't end up playing an ongoing game of whack-a-mole.

And one question that needs to be answered is what happens when an access rolls over from supervisor to user, e.g. if the kernel access 8 bytes at -1ull and thus reads all Fs => 0x6, does the access get a LASS violation on the access to user memory. User=>supervisor can't happen because non-canonical checks have higher priority, but supervisor=>user can. And that matters because it will impact whether or not KVM needs to check each *page* or just the initial address.

Quoted 7 lines
arch/x86/kvm/vmx/vmx.c +7 -0
+		user_mode = false;
+		rflags_ac = false;
+	} else {
+		user_mode = vmx_get_cpl(vcpu) == 3;
+		if (!user_mode)
+			rflags_ac = !!(kvm_get_rflags(vcpu) & X86_EFLAGS_AC);
+	}

arch/x86/kvm/vmx/vmx.c static void vmx_vm_destroy(struct kvm *kvm) · line 8173

Quoted 10 lines
arch/x86/kvm/vmx/vmx.c +10 -0
+
+	if (user_mode == user_as)
+		return false;
+
+	/*
+	 * Supervisor-mode _data_ accesses to user address space
+	 * cause LASS violations only if SMAP is enabled.
+	 */
+	if (!user_mode && !(access & PFERR_FETCH_MASK))
+		return kvm_is_cr4_bit_set(vcpu, X86_CR4_SMAP) && !rflags_ac;

This is all more complex than it needs to be. Using local bools just so that the "user_mode == user_as" is common is not a good tradeoff. User addresses have *significantly* different behavior, lean into that instead of dancing around it.

bool is_lass_violation(struct kvm_vcpu *vcpu, unsigned long addr,
unsigned int flags)

{
	const bool is_supervisor_access = addr & BIT_ULL(63);
	const bool implicit = flags & X86EMUL_F_IMPLICIT;

	bool user_mode, user_as, rflags_ac;

	if (!kvm_is_cr4_bit_set(vcpu, X86_CR4_LASS) || !is_long_mode(vcpu))
		return false;

	/*
	 * INVLPG isn't subject to LASS, e.g. to allow invalidating userspace
	 * addresses without toggling RFLAGS.AC.  Branch targets aren't subject
	 * to LASS in order to simplifiy far control transfers (the subsequent
	 * fetch will enforce LASS as appropriate).
	 */
	if (flags & (X86EMUL_F_BRANCH_TARGET | X86EMUL_F_INVLPG))
		return false;

	if (!implicit && vmx_get_cpl(vcpu) == 3)
		return is_supervisor_address;

	/* LASS is enforced for supervisor access iff SMAP is enabled. */
	if (!kvm_is_cr4_bit_set(vcpu, X86_CR4_SMAP))
		return false;

	/* Like SMAP, RFLAGS.AC disables LASS checks in supervisor mode. */
	if (!implicit && (kvm_get_rflags(vcpu) & X86_EFLAGS_AC))
		return false;

	return !is_supervisor_address;
}
Quoted 2 lines
arch/x86/kvm/vmx/vmx.c +2 -0
+	return true;
+}
#22 lore raw

Re: [PATCH v1 3/6] KVM: VMX: Add new ops in kvm_x86_ops for LASS violation check

Quoted 22 lines · Sean Christopherson

arch/x86/kvm/vmx/vmx.c static void vmx_vm_destroy(struct kvm *kvm) · line 8156

Quoted 6 lines
arch/x86/kvm/vmx/vmx.c +6 -0
+	/*
+	 * An access is a supervisor-mode access if CPL < 3 or if it implicitly
+	 * accesses a system data structure. For implicit accesses to system
+	 * data structure, the processor acts as if RFLAGS.AC is clear.
+	 */
+	if (access & PFERR_IMPLICIT_ACCESS) {

Please don't use PFERR_IMPLICIT_ACCESS, just extend the new flags. This is obviously not coming from a page fault. PFERR_IMPLICIT_ACCESS really shouldn't exist, but at least there was reasonable justification for adding it (changing all of the paths that lead to permission_fault() would have require a massive overhaul).

***HOWEVER***

I think the entire approach of hooking __linearize() may be a mistake, and LASS should instead be implemented in a wrapper of ->gva_to_gpa(). The two calls to __linearize() that are escaped with SKIPLASS are escaped *because* they don't actually access memory (branch targets and INVLPG), and so if LASS is enforced only when ->gva_to_gpa() is invoked, all of these new flags go away because the cases that ignore LASS are naturally handled.

Quoted 3 lines

That should also make it unnecessary to add one-off checks since e.g. kvm_handle_invpcid() will hit kvm_read_guest_virt() and thus ->gva_to_gpa(), i.e. we won't end up playing an ongoing game of whack-a-mole.

Drat, that won't work, at least not without quite a few more changes.

1. kvm_{read,write,fetch}_guest_virt() are effectively defined to work with a fully resolve linear address, i.e. callers assume failure means #PF

2. Similar to (1), segment information isn't available, i.e. KVM wouldn't know when to inject #SS instead of #GP

And IIUC, LASS violations are higher priority than instruction specific alignment checks, e.g. on CMPXCHG16B.

And looking at LAM, that untagging needs to be done before the canonical checks, which means that we'll need at least X86EMUL_F_INVLPG.

So my idea of shoving this into a ->gva_to_gpa() wrapper won't work well. Bummer.

author
#23 lore raw

Re: [PATCH v1 3/6] KVM: VMX: Add new ops in kvm_x86_ops for LASS violation check

In reply to Sean Christopherson
Quoted 5 lines · Sean Christopherson

Similar to comments on an earlier patch, don't try to describe the literal code change, e.g. this does far more than just "Add new ops in kvm_x86_ops". Something like

KVM: VMX: Add emulation of LASS violation checks on linear address

OK. I will modify the change log to focus on overview of patch implementation and be more concise.

Quoted 36 lines · Zeng Guang
Quoted 34 lines

Intel introduces LASS (Linear Address Separation) feature providing an independent mechanism to achieve the mode-based protection.

LASS partitions 64-bit linear address space into two halves, user-mode address (LA[bit 63]=0) and supervisor-mode address (LA[bit 63]=1). It stops any code execution or conditional data access[1]
1. from user mode to supervisor-mode address space
2. from supervisor mode to user-mode address space
and generates LASS violation fault accordingly.

[1]A supervisor mode data access causes a LASS violation only if supervisor mode access protection is enabled (CR4.SMAP = 1) and either RFLAGS.AC = 0 or the access implicitly accesses a system data structure.

Following are the rules of LASS violation check on the linear address(LA). User access to supervisor-mode address space:

     LA[bit 63] && (CPL == 3)
Supervisor access to user-mode address space:
     Instruction fetch: !LA[bit 63] && (CPL < 3)
     Data access: !LA[bit 63] && (CR4.SMAP==1) && ((RFLAGS.AC == 0 &&
                  CPL < 3) || Implicit supervisor access)

Add new ops in kvm_x86_ops to do LASS violation check.

---
  arch/x86/include/asm/kvm-x86-ops.h |  3 +-
  arch/x86/include/asm/kvm_host.h    |  2 ++
  arch/x86/kvm/kvm_emulate.h         |  1 +
  arch/x86/kvm/vmx/vmx.c             | 47 ++++++++++++++++++++++++++++++
  arch/x86/kvm/vmx/vmx.h             |  2 ++
  5 files changed, 54 insertions(+), 1 deletion(-)
Quoted 13 lines

arch/x86/include/asm/kvm-x86-ops.h KVM_X86_OP_OPTIONAL(migrate_timers) · line 136

Quoted 11 lines
arch/x86/include/asm/kvm-x86-ops.h +2 -1
index 13bc212cd4bc..8980a3bfa687 100644
@@ -132,7 +132,8 @@ KVM_X86_OP_OPTIONAL(migrate_timers)
  KVM_X86_OP(msr_filter_changed)
  KVM_X86_OP(complete_emulated_msr)
  KVM_X86_OP(vcpu_deliver_sipi_vector)
-KVM_X86_OP_OPTIONAL_RET0(vcpu_get_apicv_inhibit_reasons);
+KVM_X86_OP_OPTIONAL_RET0(vcpu_get_apicv_inhibit_reasons)
+KVM_X86_OP_OPTIONAL_RET0(check_lass)

Use "is_lass_violation" instead of "check_lass" for all function names. "check" doesn't convey that the function is a predicate, i.e. that it returns true/false.

That does make sense. Will change it.

Quoted 33 lines
Quoted 20 lines
arch/x86/include/asm/kvm-x86-ops.h +0 -0
  #undef KVM_X86_OP
  #undef KVM_X86_OP_OPTIONAL
arch/x86/include/asm/kvm_host.h +2 -0
index 92d8e65fe88c..98666d1e7727 100644
@@ -1731,6 +1731,8 @@ struct kvm_x86_ops {
  	 * Returns vCPU specific APICv inhibit reasons
  	 */
  	unsigned long (*vcpu_get_apicv_inhibit_reasons)(struct kvm_vcpu *vcpu);
+
+	bool (*check_lass)(struct kvm_vcpu *vcpu, u64 access, u64 la, u32 flags);
  };
 
  struct kvm_x86_nested_ops {
arch/x86/kvm/kvm_emulate.h +0 -0
index 5b9ec610b2cb..f1439ab7c14b 100644

arch/x86/kvm/kvm_emulate.h struct x86_instruction_info { · line 94

Quoted 4 lines
arch/x86/kvm/kvm_emulate.h +1 -0
@@ -91,6 +91,7 @@ struct x86_instruction_info {
  /* x86-specific emulation flags */
  #define X86EMUL_F_FETCH			BIT(0)
  #define X86EMUL_F_WRITE			BIT(1)
+#define X86EMUL_F_SKIPLASS		BIT(2)

This belongs in the patch that integrates LASS into the emulator. And rather than make the flag a command (SKIPLASS), I think it makes sense to make the flag describe the access. It'll mean more flags, but those are free. That way the originators of the accesses, e.g. em_invlpg(), don't need to document how they interact with LASS, e.g. this code is self-documenting, and if someone wants to understand why KVM has a dedicated X86EMUL_F_INVLPG flag, it's easy enough to find the consumer. And in the unlikely scenario that other things care about INVLPG, branch targets, etc., we won't end up with X86EMUL_F_SKIPLASS, X86EMUL_F_SKIPOTHER, etc.

Quoted 8 lines
	rc = __linearize(ctxt, ctxt->src.addr.mem, &max_size, 1,
			 X86EMUL_F_INVLPG, ctxt->mode, &linear);

So this?

   #define X86EMUL_F_IMPLICIT
   #define X86EMUL_F_INVLPG
   #define X86EMUL_F_BRANCH_TARGET

By this way, emulator just provides the type of operation and make it to become common interface open to various platforms. That also conceals vendor specific implementation details. We will follow this idea.
Thanks.

Quoted 19 lines
Quoted 19 lines
arch/x86/kvm/kvm_emulate.h +0 -0
  struct x86_emulate_ops {
  	void (*vm_bugged)(struct x86_emulate_ctxt *ctxt);
arch/x86/kvm/vmx/vmx.c +9 -0
index a33205ded85c..876997e8448e 100644
@@ -8130,6 +8130,51 @@ static void vmx_vm_destroy(struct kvm *kvm)
  	free_pages((unsigned long)kvm_vmx->pid_table, vmx_get_pid_table_order(kvm));
  }
 
+/*
+ * Determine whether an access to the linear address causes a LASS violation.
+ * LASS protection is only effective in long mode. As a prerequisite, caller
+ * should make sure vCPU running in long mode and invoke this api to do LASS
+ * violation check.
+ */
+bool vmx_check_lass(struct kvm_vcpu *vcpu, u64 access, u64 la, u32 flags)
+{
+	bool user_mode, user_as, rflags_ac;
Quoted 8 lines

arch/x86/kvm/vmx/vmx.c static void vmx_vm_destroy(struct kvm *kvm) · line 8147

Quoted 6 lines
arch/x86/kvm/vmx/vmx.c +6 -0
+
+	if (!!(flags & X86EMUL_F_SKIPLASS) ||
+	    !kvm_is_cr4_bit_set(vcpu, X86_CR4_LASS))
+		return false;
+
+	WARN_ON_ONCE(!is_long_mode(vcpu));

This is silly. By making this a preqreq, 2 of the 3 callers are forced to explicitly check for is_long_mode(), *and* it unnecessarily bleeds LASS details outside of VMX.

Right. Will give up this implementation.

Quoted 6 lines
Quoted 6 lines
arch/x86/kvm/vmx/vmx.c +6 -0
+
+	user_as = !(la >> 63);
+
+	/*
+	 * An access is a supervisor-mode access if CPL < 3 or if it implicitly
+	 * accesses a system data structure. For implicit accesses to system
Quoted 8 lines

arch/x86/kvm/vmx/vmx.c static void vmx_vm_destroy(struct kvm *kvm) · line 8156

Quoted 3 lines
arch/x86/kvm/vmx/vmx.c +3 -0
+	 * data structure, the processor acts as if RFLAGS.AC is clear.
+	 */
+	if (access & PFERR_IMPLICIT_ACCESS) {

Please don't use PFERR_IMPLICIT_ACCESS, just extend the new flags. This is obviously not coming from a page fault. PFERR_IMPLICIT_ACCESS really shouldn't exist, but at least there was reasonable justification for adding it (changing all of the paths that lead to permission_fault() would have require a massive overhaul).

I've realized it not proper to use "PFERR_xxx" which was defined for mmu process actually. It easily makes things confuse.

Quoted 9 lines

***HOWEVER***

I think the entire approach of hooking __linearize() may be a mistake, and LASS should instead be implemented in a wrapper of ->gva_to_gpa(). The two calls to __linearize() that are escaped with SKIPLASS are escaped *because* they don't actually access memory (branch targets and INVLPG), and so if LASS is enforced only when ->gva_to_gpa() is invoked, all of these new flags go away because the cases that ignore LASS are naturally handled.

Quoted 3 lines

That should also make it unnecessary to add one-off checks since e.g. kvm_handle_invpcid() will hit kvm_read_guest_virt() and thus ->gva_to_gpa(), i.e. we won't end up playing an ongoing game of whack-a-mole.

We've ever thought about this scheme, i.e. doing LASS violation check before page table walk in ->gva_to_gpa(). But we find it difficult to identify and process the fault of Lass violation. That may need to change the architecture design of mmu. Thus we recommend current implementation.

Quoted 6 lines

And one question that needs to be answered is what happens when an access rolls over from supervisor to user, e.g. if the kernel access 8 bytes at -1ull and thus reads all Fs => 0x6, does the access get a LASS violation on the access to user memory. User=>supervisor can't happen because non-canonical checks have higher priority, but supervisor=>user can. And that matters because it will impact whether or not KVM needs to check each *page* or just the initial address.

By experiment on Simics platform, I verified such kind of access will cause a LASS violation. KVM has to consider lass violation check at both end of address range of instruction fetch and data access in emulation. Currently it doesn't take care of this corner case. Thanks to point out this potential problem.

Quoted 47 lines
Quoted 8 lines
arch/x86/kvm/vmx/vmx.c +7 -0
+		user_mode = false;
+		rflags_ac = false;
+	} else {
+		user_mode = vmx_get_cpl(vcpu) == 3;
+		if (!user_mode)
+			rflags_ac = !!(kvm_get_rflags(vcpu) & X86_EFLAGS_AC);
+	}

arch/x86/kvm/vmx/vmx.c static void vmx_vm_destroy(struct kvm *kvm) · line 8173

Quoted 10 lines
arch/x86/kvm/vmx/vmx.c +10 -0
+
+	if (user_mode == user_as)
+		return false;
+
+	/*
+	 * Supervisor-mode _data_ accesses to user address space
+	 * cause LASS violations only if SMAP is enabled.
+	 */
+	if (!user_mode && !(access & PFERR_FETCH_MASK))
+		return kvm_is_cr4_bit_set(vcpu, X86_CR4_SMAP) && !rflags_ac;

This is all more complex than it needs to be. Using local bools just so that the "user_mode == user_as" is common is not a good tradeoff. User addresses have *significantly* different behavior, lean into that instead of dancing around it.

bool is_lass_violation(struct kvm_vcpu *vcpu, unsigned long addr,
unsigned int flags)

{
	const bool is_supervisor_access = addr & BIT_ULL(63);
	const bool implicit = flags & X86EMUL_F_IMPLICIT;

	bool user_mode, user_as, rflags_ac;

	if (!kvm_is_cr4_bit_set(vcpu, X86_CR4_LASS) || !is_long_mode(vcpu))
		return false;

	/*
	 * INVLPG isn't subject to LASS, e.g. to allow invalidating userspace
	 * addresses without toggling RFLAGS.AC.  Branch targets aren't subject
	 * to LASS in order to simplifiy far control transfers (the subsequent
	 * fetch will enforce LASS as appropriate).
	 */
	if (flags & (X86EMUL_F_BRANCH_TARGET | X86EMUL_F_INVLPG))
		return false;

	if (!implicit && vmx_get_cpl(vcpu) == 3)
		return is_supervisor_address;

	/* LASS is enforced for supervisor access iff SMAP is enabled. */
	if (!kvm_is_cr4_bit_set(vcpu, X86_CR4_SMAP))
Quoted 8 lines
		return false;

	/* Like SMAP, RFLAGS.AC disables LASS checks in supervisor mode. */
	if (!implicit && (kvm_get_rflags(vcpu) & X86_EFLAGS_AC))
		return false;

	return !is_supervisor_address;
}

OK. I will refine the implementation further.
Thanks.

Quoted 2 lines
Quoted 2 lines
arch/x86/kvm/vmx/vmx.c +2 -0
+	return true;
+}
author
#24 lore raw

PATCH 4/6 KVM: x86: Add emulator helper for LASS violation check

In reply to Zeng Guang

When LASS is enabled, KVM need apply LASS violation check to instruction emulations. Add helper for the usage of x86 emulator to perform LASS protection.

---
 arch/x86/kvm/kvm_emulate.h |  1 +
 arch/x86/kvm/x86.c         | 12 ++++++++++++
 2 files changed, 13 insertions(+)
arch/x86/kvm/kvm_emulate.h +1 -0
index f1439ab7c14b..fd1c2b22867e 100644
@@ -230,6 +230,7 @@ struct x86_emulate_ops {
 	int (*leave_smm)(struct x86_emulate_ctxt *ctxt);
 	void (*triple_fault)(struct x86_emulate_ctxt *ctxt);
 	int (*set_xcr)(struct x86_emulate_ctxt *ctxt, u32 index, u64 xcr);
+	bool (*check_lass)(struct x86_emulate_ctxt *ctxt, u64 access, u64 la, u32 flags);
 };
 
 /* Type, address-of, and value of an instruction's operand. */
arch/x86/kvm/x86.c +12 -0
index c0778ca39650..faf01fecc4ca 100644
@@ -8287,6 +8287,17 @@ static void emulator_vm_bugged(struct x86_emulate_ctxt *ctxt)
 		kvm_vm_bugged(kvm);
 }
 
+static bool emulator_check_lass(struct x86_emulate_ctxt *ctxt,
+				u64 access, u64 la, u32 flags)
+{
+	struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
+
+	if (!is_long_mode(vcpu))
+		return false;
+
+	return static_call(kvm_x86_check_lass)(vcpu, access, la, flags);
+}
+
 static const struct x86_emulate_ops emulate_ops = {
 	.vm_bugged           = emulator_vm_bugged,
 	.read_gpr            = emulator_read_gpr,
@@ -8332,6 +8343,7 @@ static const struct x86_emulate_ops emulate_ops = {
 	.leave_smm           = emulator_leave_smm,
 	.triple_fault        = emulator_triple_fault,
 	.set_xcr             = emulator_set_xcr,
+	.check_lass          = emulator_check_lass,
 };
 
 static void toggle_interruptibility(struct kvm_vcpu *vcpu, u32 mask)
Signature · 1 line
2.27.0
#25 lore raw

Re: [PATCH v1 4/6] KVM: x86: Add emulator helper for LASS violation check

Quoted 28 lines · Zeng Guang

When LASS is enabled, KVM need apply LASS violation check to instruction emulations. Add helper for the usage of x86 emulator to perform LASS protection.

---
 arch/x86/kvm/kvm_emulate.h |  1 +
 arch/x86/kvm/x86.c         | 12 ++++++++++++
 2 files changed, 13 insertions(+)
arch/x86/kvm/kvm_emulate.h +1 -0
index f1439ab7c14b..fd1c2b22867e 100644
@@ -230,6 +230,7 @@ struct x86_emulate_ops {
 	int (*leave_smm)(struct x86_emulate_ctxt *ctxt);
 	void (*triple_fault)(struct x86_emulate_ctxt *ctxt);
 	int (*set_xcr)(struct x86_emulate_ctxt *ctxt, u32 index, u64 xcr);
+	bool (*check_lass)(struct x86_emulate_ctxt *ctxt, u64 access, u64 la, u32 flags);
 };
 
 /* Type, address-of, and value of an instruction's operand. */
arch/x86/kvm/x86.c +0 -0
index c0778ca39650..faf01fecc4ca 100644

arch/x86/kvm/x86.c static void emulator_vm_bugged(struct x86_emulate_ctxt *ctxt) · line 8296

Quoted 10 lines
arch/x86/kvm/x86.c +7 -0
@@ -8287,6 +8287,17 @@ static void emulator_vm_bugged(struct x86_emulate_ctxt *ctxt)
 		kvm_vm_bugged(kvm);
 }
 
+static bool emulator_check_lass(struct x86_emulate_ctxt *ctxt,
+				u64 access, u64 la, u32 flags)
+{
+	struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
+
+	if (!is_long_mode(vcpu))
+		return false;

Likely a moot point if we wrap ->gva_to_gpa(), but most this into the vendor implementation.

And if we keep these emulator hooks, massage the patch ordering:

1. Add plumbing to emulator to pass new flags
2. Add kvm_x86_ops definition and invocation from emulator
3. Implement and wire up vmx_is_lass_violation()

That way the changes to each area of KVM are better isolated.

Quoted 17 lines
arch/x86/kvm/x86.c +4 -0
+	return static_call(kvm_x86_check_lass)(vcpu, access, la, flags);
+}
+
 static const struct x86_emulate_ops emulate_ops = {
 	.vm_bugged           = emulator_vm_bugged,
 	.read_gpr            = emulator_read_gpr,
@@ -8332,6 +8343,7 @@ static const struct x86_emulate_ops emulate_ops = {
 	.leave_smm           = emulator_leave_smm,
 	.triple_fault        = emulator_triple_fault,
 	.set_xcr             = emulator_set_xcr,
+	.check_lass          = emulator_check_lass,
 };
 
 static void toggle_interruptibility(struct kvm_vcpu *vcpu, u32 mask)
Signature · 1 line
2.27.0
author
#26 lore raw

Re: [PATCH v1 4/6] KVM: x86: Add emulator helper for LASS violation check

Quoted 32 lines · Sean Christopherson
Quoted 31 lines · Zeng Guang

When LASS is enabled, KVM need apply LASS violation check to instruction emulations. Add helper for the usage of x86 emulator to perform LASS protection.

---
  arch/x86/kvm/kvm_emulate.h |  1 +
  arch/x86/kvm/x86.c         | 12 ++++++++++++
  2 files changed, 13 insertions(+)
arch/x86/kvm/kvm_emulate.h +1 -0
index f1439ab7c14b..fd1c2b22867e 100644
@@ -230,6 +230,7 @@ struct x86_emulate_ops {
  	int (*leave_smm)(struct x86_emulate_ctxt *ctxt);
  	void (*triple_fault)(struct x86_emulate_ctxt *ctxt);
  	int (*set_xcr)(struct x86_emulate_ctxt *ctxt, u32 index, u64 xcr);
+	bool (*check_lass)(struct x86_emulate_ctxt *ctxt, u64 access, u64 la, u32 flags);
  };
 
  /* Type, address-of, and value of an instruction's operand. */
arch/x86/kvm/x86.c +0 -0
index c0778ca39650..faf01fecc4ca 100644
@@ -8287,6 +8287,17 @@ static void emulator_vm_bugged(struct x86_emulate_ctxt *ctxt)
  		kvm_vm_bugged(kvm);
  }
 
Quoted 9 lines

arch/x86/kvm/x86.c static void emulator_vm_bugged(struct x86_emulate_ctxt *ctxt) · line 8296

Quoted 7 lines
arch/x86/kvm/x86.c +7 -0
+static bool emulator_check_lass(struct x86_emulate_ctxt *ctxt,
+				u64 access, u64 la, u32 flags)
+{
+	struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
+
+	if (!is_long_mode(vcpu))
+		return false;

Likely a moot point if we wrap ->gva_to_gpa(), but most this into the vendor implementation.

It's right way to move cpu mode check into is_lass_violation(). Previously I was struggling to expect getting benefit from separate invocation. But it doesn't help much indeed.

Quoted 8 lines

And if we keep these emulator hooks, massage the patch ordering:

1. Add plumbing to emulator to pass new flags
2. Add kvm_x86_ops definition and invocation from emulator
3. Implement and wire up vmx_is_lass_violation()

That way the changes to each area of KVM are better isolated.

OK. Will reorganize the patch accordingly.
Thanks.

Quoted 17 lines
Quoted 17 lines
arch/x86/kvm/x86.c +4 -0
+	return static_call(kvm_x86_check_lass)(vcpu, access, la, flags);
+}
+
  static const struct x86_emulate_ops emulate_ops = {
  	.vm_bugged           = emulator_vm_bugged,
  	.read_gpr            = emulator_read_gpr,
@@ -8332,6 +8343,7 @@ static const struct x86_emulate_ops emulate_ops = {
  	.leave_smm           = emulator_leave_smm,
  	.triple_fault        = emulator_triple_fault,
  	.set_xcr             = emulator_set_xcr,
+	.check_lass          = emulator_check_lass,
  };
 
  static void toggle_interruptibility(struct kvm_vcpu *vcpu, u32 mask)
Signature · 1 line
2.27.0
author
#27 lore raw

PATCH 5/6 KVM: x86: LASS protection on KVM emulation

In reply to Zeng Guang

Do LASS violation check for instructions emulated by KVM. Note that for instructions executed in the guest directly, hardware will perform the check.

Not all instruction emulation leads to accesses to guest linear addresses because 1) some instructions like CPUID, RDMSR, don't take memory as operands 2) instruction fetch in most cases is already done inside the guest.

Four cases in which KVM uses a linear address to access guest memory:
- KVM emulates instruction fetches or data accesses
- KVM emulates implicit data access to a system data structure
- VMX instruction emulation
- SGX ENCLS instruction emulation

LASS violation check applies to these linear addresses so as to enforce mode-based protections as hardware behaves.

As exceptions, the target memory address of emulation of invlpg, branch and call instructions doesn't require LASS violation check.

---
 arch/x86/kvm/emulate.c    | 30 ++++++++++++++++++++++++++++--
 arch/x86/kvm/vmx/nested.c |  3 +++
 arch/x86/kvm/vmx/sgx.c    |  4 ++++
 3 files changed, 35 insertions(+), 2 deletions(-)
arch/x86/kvm/emulate.c +28 -2
index 9508836e8a35..ed5191fa2079 100644
@@ -698,6 +698,7 @@ static __always_inline int __linearize(struct x86_emulate_ctxt *ctxt,
 	u8  va_bits;
 	bool fetch = !!(flags & X86EMUL_F_FETCH);
 	bool write = !!(flags & X86EMUL_F_WRITE);
+	u64 access = fetch ? PFERR_FETCH_MASK : 0;
 
 	la = seg_base(ctxt, addr.seg) + addr.ea;
 	*max_size = 0;
@@ -743,6 +744,10 @@ static __always_inline int __linearize(struct x86_emulate_ctxt *ctxt,
 		}
 		break;
 	}
+
+	if (ctxt->ops->check_lass(ctxt, access, *linear, flags))
+		goto bad;
+
 	if (la & (insn_alignment(ctxt, size) - 1))
 		return emulate_gp(ctxt, 0);
 	return X86EMUL_CONTINUE;
@@ -774,7 +779,11 @@ static inline int assign_eip(struct x86_emulate_ctxt *ctxt, ulong dst)
 	unsigned max_size;
 	struct segmented_address addr = { .seg = VCPU_SREG_CS,
 					   .ea = dst };
-	u32 flags = X86EMUL_F_FETCH;
+	/*
+	 * LASS doesn't apply to addresses that specify the targets of jump and
+	 * call instructions.
+	 */
+	u32 flags = X86EMUL_F_FETCH | X86EMUL_F_SKIPLASS;
 
 	if (ctxt->op_bytes != sizeof(unsigned long))
 		addr.ea = dst & ((1UL << (ctxt->op_bytes << 3)) - 1);
@@ -853,6 +862,13 @@ static inline int jmp_rel(struct x86_emulate_ctxt *ctxt, int rel)
 static int linear_read_system(struct x86_emulate_ctxt *ctxt, ulong linear,
 			      void *data, unsigned size)
 {
+	if (ctxt->ops->check_lass(ctxt, PFERR_IMPLICIT_ACCESS, linear, 0)) {
+		ctxt->exception.vector = GP_VECTOR;
+		ctxt->exception.error_code = 0;
+		ctxt->exception.error_code_valid = true;
+		return X86EMUL_PROPAGATE_FAULT;
+	}
+
 	return ctxt->ops->read_std(ctxt, linear, data, size, &ctxt->exception, true);
 }
 
@@ -860,6 +876,13 @@ static int linear_write_system(struct x86_emulate_ctxt *ctxt,
 			       ulong linear, void *data,
 			       unsigned int size)
 {
+	if (ctxt->ops->check_lass(ctxt, PFERR_IMPLICIT_ACCESS, linear, 0)) {
+		ctxt->exception.vector = GP_VECTOR;
+		ctxt->exception.error_code = 0;
+		ctxt->exception.error_code_valid = true;
+		return X86EMUL_PROPAGATE_FAULT;
+	}
+
 	return ctxt->ops->write_std(ctxt, linear, data, size, &ctxt->exception, true);
 }
 
@@ -3448,8 +3471,11 @@ static int em_invlpg(struct x86_emulate_ctxt *ctxt)
 {
 	int rc;
 	ulong linear;
+	unsigned max_size;
 
-	rc = linearize(ctxt, ctxt->src.addr.mem, 1, false, &linear);
+	/* LASS doesn't apply to the memory address for invlpg */
+	rc = __linearize(ctxt, ctxt->src.addr.mem, &max_size, 1,
+			 X86EMUL_F_SKIPLASS, ctxt->mode, &linear);
 	if (rc == X86EMUL_CONTINUE)
 		ctxt->ops->invlpg(ctxt, linear);
 	/* Disable writeback. */
arch/x86/kvm/vmx/nested.c +3 -0
index e35cf0bd0df9..bb1c3fa13c13 100644
@@ -4986,6 +4986,9 @@ int get_vmx_mem_address(struct kvm_vcpu *vcpu, unsigned long exit_qualification,
 		 * destination for long mode!
 		 */
 		exn = is_noncanonical_address(*ret, vcpu);
+
+		if (!exn)
+			exn = vmx_check_lass(vcpu, 0, *ret, 0);
 	} else {
 		/*
 		 * When not in long mode, the virtual/linear address is
arch/x86/kvm/vmx/sgx.c +4 -0
index 2261b684a7d4..3825275827eb 100644
@@ -46,6 +46,10 @@ static int sgx_get_encls_gva(struct kvm_vcpu *vcpu, unsigned long offset,
 			((s.base != 0 || s.limit != 0xffffffff) &&
 			(((u64)*gva + size - 1) > s.limit + 1));
 	}
+
+	if (!fault && is_long_mode(vcpu))
+		fault = vmx_check_lass(vcpu, 0, *gva, 0);
+
 	if (fault)
 		kvm_inject_gp(vcpu, 0);
 	return fault ? -EINVAL : 0;
Signature · 1 line
2.27.0
#28 lore raw

Re: [PATCH v1 5/6] KVM: x86: LASS protection on KVM emulation

Quoted 15 lines · Zeng Guang

Do LASS violation check for instructions emulated by KVM. Note that for instructions executed in the guest directly, hardware will perform the check.

Not all instruction emulation leads to accesses to guest linear addresses because 1) some instructions like CPUID, RDMSR, don't take memory as operands 2) instruction fetch in most cases is already done inside the guest.

Four cases in which KVM uses a linear address to access guest memory:
- KVM emulates instruction fetches or data accesses
- KVM emulates implicit data access to a system data structure
- VMX instruction emulation
- SGX ENCLS instruction emulation

Quoted 5 lines

LASS violation check applies to these linear addresses so as to enforce mode-based protections as hardware behaves.

As exceptions, the target memory address of emulation of invlpg, branch and call instructions doesn't require LASS violation check.

I think LASS doesn't apply to the target addresses in the descriptors of INVPCID and INVVPID. Although no code change needed, IMHO, it's better to describe it in the changelog or/and comments.

Quoted 9 lines
---
  arch/x86/kvm/emulate.c    | 30 ++++++++++++++++++++++++++++--
  arch/x86/kvm/vmx/nested.c |  3 +++
  arch/x86/kvm/vmx/sgx.c    |  4 ++++
  3 files changed, 35 insertions(+), 2 deletions(-)

[...]

arch/x86/kvm/vmx/nested.c int get_vmx_mem_address(struct kvm_vcpu *vcpu, unsigned long exit_qualification, · line 4991

Quoted 11 lines
arch/x86/kvm/vmx/nested.c +3 -0
index e35cf0bd0df9..bb1c3fa13c13 100644
@@ -4986,6 +4986,9 @@ int get_vmx_mem_address(struct kvm_vcpu *vcpu, unsigned long exit_qualification,
  		 * destination for long mode!
  		 */
  		exn = is_noncanonical_address(*ret, vcpu);
+
+		if (!exn)
+			exn = vmx_check_lass(vcpu, 0, *ret, 0);

Can be simpler by using logical-or:

exn = is_noncanonical_address(*ret, vcpu) || vmx_check_lass(vcpu, 0, *ret, 0);

Quoted 18 lines
arch/x86/kvm/vmx/nested.c +0 -0
  	} else {
  		/*
  		 * When not in long mode, the virtual/linear address is
arch/x86/kvm/vmx/sgx.c +4 -0
index 2261b684a7d4..3825275827eb 100644
@@ -46,6 +46,10 @@ static int sgx_get_encls_gva(struct kvm_vcpu *vcpu, unsigned long offset,
  			((s.base != 0 || s.limit != 0xffffffff) &&
  			(((u64)*gva + size - 1) > s.limit + 1));
  	}
+
+	if (!fault && is_long_mode(vcpu))
+		fault = vmx_check_lass(vcpu, 0, *gva, 0);
+
  	if (fault)
  		kvm_inject_gp(vcpu, 0);
  	return fault ? -EINVAL : 0;
author
#29 lore raw

PATCH 6/6 KVM: x86: Advertise LASS CPUID to user space

In reply to Zeng Guang

LASS (Linear-address space separation) is an independent mechanism to enforce the mode-based protection that can prevent user-mode accesses to supervisor-mode addresses, and vice versa. Because the LASS protections are applied before paging, malicious software can not acquire any paging-based timing information to compromise the security of system.

The CPUID bit definition to support LASS:
CPUID.(EAX=07H.ECX=1):EAX.LASS[bit 6]

Advertise LASS to user space to support LASS virtualization.

---
 arch/x86/kvm/cpuid.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
arch/x86/kvm/cpuid.c +2 -3
index 0c9660a07b23..a7fafe99ffe4 100644
@@ -646,9 +646,8 @@ void kvm_set_cpu_caps(void)
 		kvm_cpu_cap_set(X86_FEATURE_SPEC_CTRL_SSBD);
 
 	kvm_cpu_cap_mask(CPUID_7_1_EAX,
-		F(AVX_VNNI) | F(AVX512_BF16) | F(CMPCCXADD) |
-		F(FZRM) | F(FSRS) | F(FSRC) |
-		F(AMX_FP16) | F(AVX_IFMA)
+		F(AVX_VNNI) | F(AVX512_BF16) | F(LASS) | F(CMPCCXADD) |
+		F(FZRM) | F(FSRS) | F(FSRC) | F(AMX_FP16) | F(AVX_IFMA)
 	);
 
 	kvm_cpu_cap_init_kvm_defined(CPUID_7_1_EDX,
Signature · 1 line
2.27.0
#30 lore raw
In reply to Zeng Guang
Quoted 1 line · Zeng Guang

v0->v1

Heh, the kernel process is a bit of a heathen and starts counting patch versions at '1', not '0'. I.e. this should be v2, not v1. No need to resend, just an FYI for the future.

author
#31 lore raw
Quoted 5 lines · Sean Christopherson
Quoted 1 line · Zeng Guang

v0->v1

Heh, the kernel process is a bit of a heathen and starts counting patch versions at '1', not '0'. I.e. this should be v2, not v1. No need to resend, just an FYI for the future.

Got it !
Thanks.

#32 lore raw
In reply to Zeng Guang
Quoted 26 lines · Zeng Guang

Subject:
[PATCH v1 0/6] LASS KVM virtualization support
From:
Zeng Guang <[email protected]>
Date:
6/1/2023, 10:23 PM

To:
Paolo Bonzini <[email protected]>, Sean Christopherson <[email protected]>, Thomas Gleixner <[email protected]>, Ingo Molnar <[email protected]>, Borislav Petkov <[email protected]>, Dave Hansen <[email protected]>, H Peter Anvin <[email protected]>, [email protected] CC:
[email protected], [email protected], Zeng Guang <[email protected]>

Linear Address Space Separation (LASS)[1] is an independent mechanism that enforces the mode-based protections on any access to a linear address.

Based on a linear-address organization, the 64-bit canonical linear address space is partitioned into two halves: all linear addresses whose most significant bit is 0 are user space addresses, while linear addresses whose most significant bit is 1 are supervisor space address.

LASS aims to prevent any attempt to probe supervisor space addresses by user mode, and likewise stop any attempt to access (if SMAP enabled) or execute user space addresses from supervisor mode.

Quoted 6 lines

When platform has LASS capability, KVM requires to expose this feature to guest VM enumerated by CPUID.(EAX=07H.ECX=1):EAX.LASS[bit 6], and allow guest to enable it via CR4.LASS[bit 27] on demand. For instruction executed in the guest directly, hardware will perform the check. But KVM also needs to behave same as hardware to apply LASS to kinds of guest memory accesses when emulating privileged instructions by software.

Not just privileged instructions, e.g. MMIO access instructions.

Quoted 2 lines

KVM will take following LASS voilations check on emulation path.

/s/voilations/violations

Quoted 54 lines

User-mode access to supervisor space address:

         LA[bit 63] && (CPL == 3)
Supervisor-mode access to user space address:
         Instruction fetch: !LA[bit 63] && (CPL < 3)
         Data access: !LA[bit 63] && (CR4.SMAP==1) && ((RFLAGS.AC == 0 &&
                      CPL < 3) || Implicit supervisor access)

This patch series provide a LASS KVM solution.

We tested the basic function of LASS virtualization including LASS enumeration and enabling in non-root and nested environment. As KVM unittest framework is not compatible to LASS rule, we use kernel module and application test to emulate LASS violation instead. With KVM forced emulation mechanism, we also verified the LASS functionality on some emulation path with instruction fetch and data access to have same behavior as hardware.

[1] Intel ISEhttps://cdrdv2.intel.com/v1/dl/getContent/671368 Chapter Linear Address Space Separation (LASS)

------------------------------------------------------

v0->v1
1. Adapt to new __linearize() API
2. Function refactor of vmx_check_lass()
3. Refine commit message to be more precise
4. Drop LASS kvm cap detection depending
on hardware capability

Binbin Wu (1):
KVM: x86: Consolidate flags for __linearize()

Zeng Guang (5):
KVM: x86: Virtualize CR4.LASS
KVM: VMX: Add new ops in kvm_x86_ops for LASS violation check
KVM: x86: Add emulator helper for LASS violation check
KVM: x86: LASS protection on KVM emulation
KVM: x86: Advertise LASS CPUID to user space

  arch/x86/include/asm/kvm-x86-ops.h |  3 +-
  arch/x86/include/asm/kvm_host.h    |  4 ++-
  arch/x86/kvm/cpuid.c               |  5 ++-
  arch/x86/kvm/emulate.c             | 47 +++++++++++++++++++++++-----
  arch/x86/kvm/kvm_emulate.h         |  6 ++++
  arch/x86/kvm/vmx/nested.c          |  3 ++
  arch/x86/kvm/vmx/sgx.c             |  4 +++
  arch/x86/kvm/vmx/vmx.c             | 50 ++++++++++++++++++++++++++++++
  arch/x86/kvm/vmx/vmx.h             |  2 ++
  arch/x86/kvm/x86.c                 | 12 +++++++
  arch/x86/kvm/x86.h                 |  2 ++
  11 files changed, 126 insertions(+), 12 deletions(-)

-- 2.27.0

author
#33 lore raw
Quoted 33 lines · Binbin Wu
Quoted 31 lines · Zeng Guang

Subject:
[PATCH v1 0/6] LASS KVM virtualization support
From:
Zeng Guang <[email protected]>
Date:
6/1/2023, 10:23 PM

To:
Paolo Bonzini <[email protected]>, Sean Christopherson <[email protected]>, Thomas Gleixner <[email protected]>, Ingo Molnar <[email protected]>, Borislav Petkov <[email protected]>, Dave Hansen <[email protected]>, H Peter Anvin <[email protected]>, [email protected]
CC:
[email protected], [email protected], Zeng Guang <[email protected]>

Linear Address Space Separation (LASS)[1] is an independent mechanism that enforces the mode-based protections on any access to a linear address.

Based on a linear-address organization, the 64-bit canonical linear address space is partitioned into two halves: all linear addresses whose most significant bit is 0 are user space addresses, while linear addresses whose most significant bit is 1 are supervisor space address.

LASS aims to prevent any attempt to probe supervisor space addresses by user mode, and likewise stop any attempt to access (if SMAP enabled) or execute user space addresses from supervisor mode.

Quoted 7 lines
Quoted 6 lines

When platform has LASS capability, KVM requires to expose this feature to guest VM enumerated by CPUID.(EAX=07H.ECX=1):EAX.LASS[bit 6], and allow guest to enable it via CR4.LASS[bit 27] on demand. For instruction executed in the guest directly, hardware will perform the check. But KVM also needs to behave same as hardware to apply LASS to kinds of guest memory accesses when emulating privileged instructions by software.

Not just privileged instructions, e.g. MMIO access instructions.

OK. I'll revise it.

Quoted 2 lines
Quoted 1 line

KVM will take following LASS voilations check on emulation path.

/s/voilations/violations

Thanks.

Quoted 54 lines
Quoted 54 lines

User-mode access to supervisor space address:

          LA[bit 63] && (CPL == 3)
Supervisor-mode access to user space address:
          Instruction fetch: !LA[bit 63] && (CPL < 3)
          Data access: !LA[bit 63] && (CR4.SMAP==1) && ((RFLAGS.AC == 0 &&
                       CPL < 3) || Implicit supervisor access)

This patch series provide a LASS KVM solution.

We tested the basic function of LASS virtualization including LASS enumeration and enabling in non-root and nested environment. As KVM unittest framework is not compatible to LASS rule, we use kernel module and application test to emulate LASS violation instead. With KVM forced emulation mechanism, we also verified the LASS functionality on some emulation path with instruction fetch and data access to have same behavior as hardware.

[1] Intel ISEhttps://cdrdv2.intel.com/v1/dl/getContent/671368 Chapter Linear Address Space Separation (LASS)

------------------------------------------------------

v0->v1
1. Adapt to new __linearize() API
2. Function refactor of vmx_check_lass()
3. Refine commit message to be more precise
4. Drop LASS kvm cap detection depending
on hardware capability

Binbin Wu (1):
KVM: x86: Consolidate flags for __linearize()

Zeng Guang (5):
KVM: x86: Virtualize CR4.LASS
KVM: VMX: Add new ops in kvm_x86_ops for LASS violation check
KVM: x86: Add emulator helper for LASS violation check
KVM: x86: LASS protection on KVM emulation
KVM: x86: Advertise LASS CPUID to user space

   arch/x86/include/asm/kvm-x86-ops.h |  3 +-
   arch/x86/include/asm/kvm_host.h    |  4 ++-
   arch/x86/kvm/cpuid.c               |  5 ++-
   arch/x86/kvm/emulate.c             | 47 +++++++++++++++++++++++-----
   arch/x86/kvm/kvm_emulate.h         |  6 ++++
   arch/x86/kvm/vmx/nested.c          |  3 ++
   arch/x86/kvm/vmx/sgx.c             |  4 +++
   arch/x86/kvm/vmx/vmx.c             | 50 ++++++++++++++++++++++++++++++
   arch/x86/kvm/vmx/vmx.h             |  2 ++
   arch/x86/kvm/x86.c                 | 12 +++++++
   arch/x86/kvm/x86.h                 |  2 ++
   11 files changed, 126 insertions(+), 12 deletions(-)

-- 2.27.0

#34 lore raw
In reply to Zeng Guang
Quoted 1 line · Zeng Guang

This patch series provide a LASS KVM solution.

... and depends on kernel enabling that can be found at

https://lore.kernel.org/all/[email protected]

Quoted 10 lines

We tested the basic function of LASS virtualization including LASS enumeration and enabling in non-root and nested environment. As KVM unittest framework is not compatible to LASS rule, we use kernel module and application test to emulate LASS violation instead. With KVM forced emulation mechanism, we also verified the LASS functionality on some emulation path with instruction fetch and data access to have same behavior as hardware.

[1] Intel ISE https://cdrdv2.intel.com/v1/dl/getContent/671368
Chapter Linear Address Space Separation (LASS)

author
#35 lore raw
Quoted 5 lines · Sean Christopherson
Quoted 1 line · Zeng Guang

This patch series provide a LASS KVM solution.

... and depends on kernel enabling that can be found at

https://lore.kernel.org/all/[email protected]

OK. I will add the dependency statement in next version.

Quoted 10 lines
Quoted 10 lines

We tested the basic function of LASS virtualization including LASS enumeration and enabling in non-root and nested environment. As KVM unittest framework is not compatible to LASS rule, we use kernel module and application test to emulate LASS violation instead. With KVM forced emulation mechanism, we also verified the LASS functionality on some emulation path with instruction fetch and data access to have same behavior as hardware.

[1] Intel ISE https://cdrdv2.intel.com/v1/dl/getContent/671368
Chapter Linear Address Space Separation (LASS)