KVM: selftests: Add a helper for SMCCC calls with SMC instruction
authorOliver Upton <oliver.upton@linux.dev>
Tue, 4 Apr 2023 15:40:49 +0000 (15:40 +0000)
committerMarc Zyngier <maz@kernel.org>
Wed, 5 Apr 2023 11:07:42 +0000 (12:07 +0100)
Build a helper for doing SMCs in selftests by macro-izing the current
HVC implementation and taking the conduit instruction as an argument.

Signed-off-by: Oliver Upton <oliver.upton@linux.dev>
Signed-off-by: Marc Zyngier <maz@kernel.org>
Link: https://lore.kernel.org/r/20230404154050.2270077-13-oliver.upton@linux.dev
tools/testing/selftests/kvm/include/aarch64/processor.h
tools/testing/selftests/kvm/lib/aarch64/processor.c

index 5f97752..cb53725 100644 (file)
@@ -214,6 +214,19 @@ void smccc_hvc(uint32_t function_id, uint64_t arg0, uint64_t arg1,
               uint64_t arg2, uint64_t arg3, uint64_t arg4, uint64_t arg5,
               uint64_t arg6, struct arm_smccc_res *res);
 
+/**
+ * smccc_smc - Invoke a SMCCC function using the smc conduit
+ * @function_id: the SMCCC function to be called
+ * @arg0-arg6: SMCCC function arguments, corresponding to registers x1-x7
+ * @res: pointer to write the return values from registers x0-x3
+ *
+ */
+void smccc_smc(uint32_t function_id, uint64_t arg0, uint64_t arg1,
+              uint64_t arg2, uint64_t arg3, uint64_t arg4, uint64_t arg5,
+              uint64_t arg6, struct arm_smccc_res *res);
+
+
+
 uint32_t guest_get_vcpuid(void);
 
 #endif /* SELFTEST_KVM_PROCESSOR_H */
index 5972a23..24e8122 100644 (file)
@@ -508,29 +508,43 @@ void aarch64_get_supported_page_sizes(uint32_t ipa,
        close(kvm_fd);
 }
 
+#define __smccc_call(insn, function_id, arg0, arg1, arg2, arg3, arg4, arg5,    \
+                    arg6, res)                                                 \
+       asm volatile("mov   w0, %w[function_id]\n"                              \
+                    "mov   x1, %[arg0]\n"                                      \
+                    "mov   x2, %[arg1]\n"                                      \
+                    "mov   x3, %[arg2]\n"                                      \
+                    "mov   x4, %[arg3]\n"                                      \
+                    "mov   x5, %[arg4]\n"                                      \
+                    "mov   x6, %[arg5]\n"                                      \
+                    "mov   x7, %[arg6]\n"                                      \
+                    #insn  "#0\n"                                              \
+                    "mov   %[res0], x0\n"                                      \
+                    "mov   %[res1], x1\n"                                      \
+                    "mov   %[res2], x2\n"                                      \
+                    "mov   %[res3], x3\n"                                      \
+                    : [res0] "=r"(res->a0), [res1] "=r"(res->a1),              \
+                      [res2] "=r"(res->a2), [res3] "=r"(res->a3)               \
+                    : [function_id] "r"(function_id), [arg0] "r"(arg0),        \
+                      [arg1] "r"(arg1), [arg2] "r"(arg2), [arg3] "r"(arg3),    \
+                      [arg4] "r"(arg4), [arg5] "r"(arg5), [arg6] "r"(arg6)     \
+                    : "x0", "x1", "x2", "x3", "x4", "x5", "x6", "x7")
+
+
 void smccc_hvc(uint32_t function_id, uint64_t arg0, uint64_t arg1,
               uint64_t arg2, uint64_t arg3, uint64_t arg4, uint64_t arg5,
               uint64_t arg6, struct arm_smccc_res *res)
 {
-       asm volatile("mov   w0, %w[function_id]\n"
-                    "mov   x1, %[arg0]\n"
-                    "mov   x2, %[arg1]\n"
-                    "mov   x3, %[arg2]\n"
-                    "mov   x4, %[arg3]\n"
-                    "mov   x5, %[arg4]\n"
-                    "mov   x6, %[arg5]\n"
-                    "mov   x7, %[arg6]\n"
-                    "hvc   #0\n"
-                    "mov   %[res0], x0\n"
-                    "mov   %[res1], x1\n"
-                    "mov   %[res2], x2\n"
-                    "mov   %[res3], x3\n"
-                    : [res0] "=r"(res->a0), [res1] "=r"(res->a1),
-                      [res2] "=r"(res->a2), [res3] "=r"(res->a3)
-                    : [function_id] "r"(function_id), [arg0] "r"(arg0),
-                      [arg1] "r"(arg1), [arg2] "r"(arg2), [arg3] "r"(arg3),
-                      [arg4] "r"(arg4), [arg5] "r"(arg5), [arg6] "r"(arg6)
-                    : "x0", "x1", "x2", "x3", "x4", "x5", "x6", "x7");
+       __smccc_call(hvc, function_id, arg0, arg1, arg2, arg3, arg4, arg5,
+                    arg6, res);
+}
+
+void smccc_smc(uint32_t function_id, uint64_t arg0, uint64_t arg1,
+              uint64_t arg2, uint64_t arg3, uint64_t arg4, uint64_t arg5,
+              uint64_t arg6, struct arm_smccc_res *res)
+{
+       __smccc_call(smc, function_id, arg0, arg1, arg2, arg3, arg4, arg5,
+                    arg6, res);
 }
 
 void kvm_selftest_arch_init(void)