tools/testing/selftests/kvm/x86/fastops_test.c

Source file repositories/reference/linux-study-clean/tools/testing/selftests/kvm/x86/fastops_test.c

File Facts

System
Linux kernel
Corpus path
tools/testing/selftests/kvm/x86/fastops_test.c
Extension
.c
Size
7702 bytes
Lines
210
Domain
Support Tooling And Documentation
Bucket
tools
Inferred role
Support Tooling And Documentation: implementation source
Status
source implementation candidate

Why This File Exists

Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.

Dependency Surface

Detected Declarations

Annotated Snippet

// SPDX-License-Identifier: GPL-2.0-only
#include "test_util.h"
#include "kvm_util.h"
#include "processor.h"

/*
 * Execute a fastop() instruction, with or without forced emulation.  BT bit 0
 * to set RFLAGS.CF based on whether or not the input is even or odd, so that
 * instructions like ADC and SBB are deterministic.
 */
#define fastop(__insn)									\
	"bt $0, %[bt_val]\n\t"								\
	__insn "\n\t"									\
	"pushfq\n\t"									\
	"pop %[flags]\n\t"

#define flags_constraint(flags_val) [flags]"=r"(flags_val)
#define bt_constraint(__bt_val) [bt_val]"rm"((u32)__bt_val)

#define guest_execute_fastop_1(FEP, insn, __val, __flags)				\
({											\
	__asm__ __volatile__(fastop(FEP insn " %[val]")					\
			     : [val]"+r"(__val), flags_constraint(__flags)		\
			     : bt_constraint(__val)					\
			     : "cc", "memory");						\
})

#define guest_test_fastop_1(insn, type_t, __val)					\
({											\
	type_t val = __val, ex_val = __val, input = __val;				\
	u64 flags, ex_flags;								\
											\
	guest_execute_fastop_1("", insn, ex_val, ex_flags);				\
	guest_execute_fastop_1(KVM_FEP, insn, val, flags);				\
											\
	__GUEST_ASSERT(val == ex_val,							\
		       "Wanted 0x%lx for '%s 0x%lx', got 0x%lx",			\
		       (u64)ex_val, insn, (u64)input, (u64)val);			\
	__GUEST_ASSERT(flags == ex_flags,						\
			"Wanted flags 0x%lx for '%s 0x%lx', got 0x%lx",			\
			ex_flags, insn, (u64)input, flags);				\
})

#define guest_execute_fastop_2(FEP, insn, __input, __output, __flags)			\
({											\
	__asm__ __volatile__(fastop(FEP insn " %[input], %[output]")			\
			     : [output]"+r"(__output), flags_constraint(__flags)	\
			     : [input]"r"(__input), bt_constraint(__output)		\
			     : "cc", "memory");						\
})

#define guest_test_fastop_2(insn, type_t, __val1, __val2)				\
({											\
	type_t input = __val1, input2 = __val2, output = __val2, ex_output = __val2;	\
	u64 flags, ex_flags;								\
											\
	guest_execute_fastop_2("", insn, input, ex_output, ex_flags);			\
	guest_execute_fastop_2(KVM_FEP, insn, input, output, flags);			\
											\
	__GUEST_ASSERT(output == ex_output,						\
		       "Wanted 0x%lx for '%s 0x%lx 0x%lx', got 0x%lx",			\
		       (u64)ex_output, insn, (u64)input,				\
		       (u64)input2, (u64)output);					\
	__GUEST_ASSERT(flags == ex_flags,						\
			"Wanted flags 0x%lx for '%s 0x%lx, 0x%lx', got 0x%lx",		\
			ex_flags, insn, (u64)input, (u64)input2, flags);		\
})

#define guest_execute_fastop_cl(FEP, insn, __shift, __output, __flags)			\
({											\
	__asm__ __volatile__(fastop(FEP insn " %%cl, %[output]")			\
			     : [output]"+r"(__output), flags_constraint(__flags)	\
			     : "c"(__shift), bt_constraint(__output)			\
			     : "cc", "memory");						\
})

#define guest_test_fastop_cl(insn, type_t, __val1, __val2)				\
({											\
	type_t output = __val2, ex_output = __val2, input = __val2;			\
	u8 shift = __val1;								\
	u64 flags, ex_flags;								\
											\
	guest_execute_fastop_cl("", insn, shift, ex_output, ex_flags);			\
	guest_execute_fastop_cl(KVM_FEP, insn, shift, output, flags);			\
											\
	__GUEST_ASSERT(output == ex_output,						\
		       "Wanted 0x%lx for '%s 0x%x, 0x%lx', got 0x%lx",			\
		       (u64)ex_output, insn, shift, (u64)input,				\
		       (u64)output);							\
	__GUEST_ASSERT(flags == ex_flags,						\

Annotation

Implementation Notes