KVM: arm64: Use BTI for nvhe
authorMostafa Saleh <smostafa@google.com>
Tue, 30 May 2023 15:08:45 +0000 (15:08 +0000)
committerOliver Upton <oliver.upton@linux.dev>
Tue, 30 May 2023 16:24:28 +0000 (16:24 +0000)
CONFIG_ARM64_BTI_KERNEL compiles the kernel to support ARMv8.5-BTI.
However, the nvhe code doesn't make use of it as it doesn't map any
pages with Guarded Page(GP) bit.

kvm pgtable code is modified to map executable pages with GP bit
if BTI is enabled for the kernel.

At hyp init, SCTLR_EL2.BT is set to 1 to match EL1 configuration
(SCTLR_EL1.BT1) set in bti_enable().

One difference between kernel and nvhe code, is that the kernel maps
.text with GP while nvhe maps all the executable pages, this makes
nvhe code need to deal with special initialization code coming from
other executable sections (.idmap.text).
For this we need to add bti instruction at the beginning of
__kvm_handle_stub_hvc as it can be called by  __host_hvc through
branch instruction(br) and unlike SYM_FUNC_START, SYM_CODE_START
doesn’t add bti instruction at the beginning, and it can’t be modified
to add it as it is used with vector tables.
Another solution which is more intrusive is to convert
__kvm_handle_stub_hvc to a function and inject “bti jc” instead of
“bti c” in SYM_FUNC_START

Signed-off-by: Mostafa Saleh <smostafa@google.com>
Link: https://lore.kernel.org/r/20230530150845.2856828-1-smostafa@google.com
Signed-off-by: Oliver Upton <oliver.upton@linux.dev>
arch/arm64/include/asm/sysreg.h
arch/arm64/kvm/hyp/nvhe/hyp-init.S
arch/arm64/kvm/hyp/pgtable.c

index e72d9aa..204124c 100644 (file)
                         (BIT(18)) | (BIT(22)) | (BIT(23)) | (BIT(28)) | \
                         (BIT(29)))
 
+#define SCTLR_EL2_BT   (BIT(36))
 #ifdef CONFIG_CPU_BIG_ENDIAN
 #define ENDIAN_SET_EL2         SCTLR_ELx_EE
 #else
index a6d67c2..f3ee66a 100644 (file)
@@ -128,6 +128,13 @@ alternative_if ARM64_HAS_ADDRESS_AUTH
                     SCTLR_ELx_ENDA | SCTLR_ELx_ENDB)
        orr     x0, x0, x1
 alternative_else_nop_endif
+
+#ifdef CONFIG_ARM64_BTI_KERNEL
+alternative_if ARM64_BTI
+       orr     x0, x0, #SCTLR_EL2_BT
+alternative_else_nop_endif
+#endif /* CONFIG_ARM64_BTI_KERNEL */
+
        msr     sctlr_el2, x0
        isb
 
@@ -196,6 +203,11 @@ SYM_CODE_START_LOCAL(__kvm_hyp_init_cpu)
 SYM_CODE_END(__kvm_hyp_init_cpu)
 
 SYM_CODE_START(__kvm_handle_stub_hvc)
+       /*
+        * __kvm_handle_stub_hvc called from __host_hvc through branch instruction(br) so
+        * we need bti j at beginning.
+        */
+       bti j
        cmp     x0, #HVC_SOFT_RESTART
        b.ne    1f
 
index 3d61bd3..a79a45f 100644 (file)
@@ -34,7 +34,7 @@
 #define KVM_PTE_LEAF_ATTR_LO_S2_SH_IS  3
 #define KVM_PTE_LEAF_ATTR_LO_S2_AF     BIT(10)
 
-#define KVM_PTE_LEAF_ATTR_HI           GENMASK(63, 51)
+#define KVM_PTE_LEAF_ATTR_HI           GENMASK(63, 50)
 
 #define KVM_PTE_LEAF_ATTR_HI_SW                GENMASK(58, 55)
 
@@ -42,6 +42,8 @@
 
 #define KVM_PTE_LEAF_ATTR_HI_S2_XN     BIT(54)
 
+#define KVM_PTE_LEAF_ATTR_HI_S1_GP     BIT(50)
+
 #define KVM_PTE_LEAF_ATTR_S2_PERMS     (KVM_PTE_LEAF_ATTR_LO_S2_S2AP_R | \
                                         KVM_PTE_LEAF_ATTR_LO_S2_S2AP_W | \
                                         KVM_PTE_LEAF_ATTR_HI_S2_XN)
@@ -371,6 +373,9 @@ static int hyp_set_prot_attr(enum kvm_pgtable_prot prot, kvm_pte_t *ptep)
 
                if (device)
                        return -EINVAL;
+
+               if (IS_ENABLED(CONFIG_ARM64_BTI_KERNEL) && system_supports_bti())
+                       attr |= KVM_PTE_LEAF_ATTR_HI_S1_GP;
        } else {
                attr |= KVM_PTE_LEAF_ATTR_HI_S1_XN;
        }