Skip to content

Commit fab1991

Browse files
ouptonMarc Zyngier
authored andcommitted
KVM: selftests: Add a helper for SMCCC calls with SMC instruction
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
1 parent 37c8e49 commit fab1991

2 files changed

Lines changed: 46 additions & 19 deletions

File tree

tools/testing/selftests/kvm/include/aarch64/processor.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,19 @@ void smccc_hvc(uint32_t function_id, uint64_t arg0, uint64_t arg1,
214214
uint64_t arg2, uint64_t arg3, uint64_t arg4, uint64_t arg5,
215215
uint64_t arg6, struct arm_smccc_res *res);
216216

217+
/**
218+
* smccc_smc - Invoke a SMCCC function using the smc conduit
219+
* @function_id: the SMCCC function to be called
220+
* @arg0-arg6: SMCCC function arguments, corresponding to registers x1-x7
221+
* @res: pointer to write the return values from registers x0-x3
222+
*
223+
*/
224+
void smccc_smc(uint32_t function_id, uint64_t arg0, uint64_t arg1,
225+
uint64_t arg2, uint64_t arg3, uint64_t arg4, uint64_t arg5,
226+
uint64_t arg6, struct arm_smccc_res *res);
227+
228+
229+
217230
uint32_t guest_get_vcpuid(void);
218231

219232
#endif /* SELFTEST_KVM_PROCESSOR_H */

tools/testing/selftests/kvm/lib/aarch64/processor.c

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -508,29 +508,43 @@ void aarch64_get_supported_page_sizes(uint32_t ipa,
508508
close(kvm_fd);
509509
}
510510

511+
#define __smccc_call(insn, function_id, arg0, arg1, arg2, arg3, arg4, arg5, \
512+
arg6, res) \
513+
asm volatile("mov w0, %w[function_id]\n" \
514+
"mov x1, %[arg0]\n" \
515+
"mov x2, %[arg1]\n" \
516+
"mov x3, %[arg2]\n" \
517+
"mov x4, %[arg3]\n" \
518+
"mov x5, %[arg4]\n" \
519+
"mov x6, %[arg5]\n" \
520+
"mov x7, %[arg6]\n" \
521+
#insn "#0\n" \
522+
"mov %[res0], x0\n" \
523+
"mov %[res1], x1\n" \
524+
"mov %[res2], x2\n" \
525+
"mov %[res3], x3\n" \
526+
: [res0] "=r"(res->a0), [res1] "=r"(res->a1), \
527+
[res2] "=r"(res->a2), [res3] "=r"(res->a3) \
528+
: [function_id] "r"(function_id), [arg0] "r"(arg0), \
529+
[arg1] "r"(arg1), [arg2] "r"(arg2), [arg3] "r"(arg3), \
530+
[arg4] "r"(arg4), [arg5] "r"(arg5), [arg6] "r"(arg6) \
531+
: "x0", "x1", "x2", "x3", "x4", "x5", "x6", "x7")
532+
533+
511534
void smccc_hvc(uint32_t function_id, uint64_t arg0, uint64_t arg1,
512535
uint64_t arg2, uint64_t arg3, uint64_t arg4, uint64_t arg5,
513536
uint64_t arg6, struct arm_smccc_res *res)
514537
{
515-
asm volatile("mov w0, %w[function_id]\n"
516-
"mov x1, %[arg0]\n"
517-
"mov x2, %[arg1]\n"
518-
"mov x3, %[arg2]\n"
519-
"mov x4, %[arg3]\n"
520-
"mov x5, %[arg4]\n"
521-
"mov x6, %[arg5]\n"
522-
"mov x7, %[arg6]\n"
523-
"hvc #0\n"
524-
"mov %[res0], x0\n"
525-
"mov %[res1], x1\n"
526-
"mov %[res2], x2\n"
527-
"mov %[res3], x3\n"
528-
: [res0] "=r"(res->a0), [res1] "=r"(res->a1),
529-
[res2] "=r"(res->a2), [res3] "=r"(res->a3)
530-
: [function_id] "r"(function_id), [arg0] "r"(arg0),
531-
[arg1] "r"(arg1), [arg2] "r"(arg2), [arg3] "r"(arg3),
532-
[arg4] "r"(arg4), [arg5] "r"(arg5), [arg6] "r"(arg6)
533-
: "x0", "x1", "x2", "x3", "x4", "x5", "x6", "x7");
538+
__smccc_call(hvc, function_id, arg0, arg1, arg2, arg3, arg4, arg5,
539+
arg6, res);
540+
}
541+
542+
void smccc_smc(uint32_t function_id, uint64_t arg0, uint64_t arg1,
543+
uint64_t arg2, uint64_t arg3, uint64_t arg4, uint64_t arg5,
544+
uint64_t arg6, struct arm_smccc_res *res)
545+
{
546+
__smccc_call(smc, function_id, arg0, arg1, arg2, arg3, arg4, arg5,
547+
arg6, res);
534548
}
535549

536550
void kvm_selftest_arch_init(void)

0 commit comments

Comments
 (0)