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

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

File Facts

System
Linux kernel
Corpus path
tools/testing/selftests/kvm/x86/nested_emulation_test.c
Extension
.c
Size
3906 bytes
Lines
147
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

struct emulated_instruction {
	const char name[32];
	u8 opcode[15];
	u32 exit_reason[NR_VIRTUALIZATION_FLAVORS];
};

static struct emulated_instruction instructions[] = {
	{
		.name = "pause",
		.opcode = { 0xf3, 0x90 },
		.exit_reason = { SVM_EXIT_PAUSE,
				 EXIT_REASON_PAUSE_INSTRUCTION, }
	},
	{
		.name = "hlt",
		.opcode = { 0xf4 },
		.exit_reason = { SVM_EXIT_HLT,
				 EXIT_REASON_HLT, }
	},
};

static u8 kvm_fep[] = { 0x0f, 0x0b, 0x6b, 0x76, 0x6d };	/* ud2 ; .ascii "kvm" */
static u8 l2_guest_code[sizeof(kvm_fep) + 15];
static u8 *l2_instruction = &l2_guest_code[sizeof(kvm_fep)];

static u32 get_instruction_length(struct emulated_instruction *insn)
{
	u32 i;

	for (i = 0; i < ARRAY_SIZE(insn->opcode) && insn->opcode[i]; i++)
		;

	return i;
}

static void guest_code(void *test_data)
{
	int f = this_cpu_has(X86_FEATURE_SVM) ? SVM_F : VMX_F;
	int i;

	memcpy(l2_guest_code, kvm_fep, sizeof(kvm_fep));

	if (f == SVM_F) {
		struct svm_test_data *svm = test_data;
		struct vmcb *vmcb = svm->vmcb;

		generic_svm_setup(svm, NULL, NULL);
		vmcb->save.idtr.limit = 0;
		vmcb->save.rip = (u64)l2_guest_code;

		vmcb->control.intercept |= BIT_ULL(INTERCEPT_SHUTDOWN) |
					   BIT_ULL(INTERCEPT_PAUSE) |
					   BIT_ULL(INTERCEPT_HLT);
		vmcb->control.intercept_exceptions = 0;
	} else {
		GUEST_ASSERT(prepare_for_vmx_operation(test_data));
		GUEST_ASSERT(load_vmcs(test_data));

		prepare_vmcs(test_data, NULL, NULL);
		GUEST_ASSERT(!vmwrite(GUEST_IDTR_LIMIT, 0));
		GUEST_ASSERT(!vmwrite(GUEST_RIP, (u64)l2_guest_code));
		GUEST_ASSERT(!vmwrite(EXCEPTION_BITMAP, 0));

		vmwrite(CPU_BASED_VM_EXEC_CONTROL, vmreadz(CPU_BASED_VM_EXEC_CONTROL) |
						   CPU_BASED_PAUSE_EXITING |
						   CPU_BASED_HLT_EXITING);
	}

	for (i = 0; i < ARRAY_SIZE(instructions); i++) {
		struct emulated_instruction *insn = &instructions[i];
		u32 insn_len = get_instruction_length(insn);
		u32 exit_insn_len;
		u32 exit_reason;

		/*
		 * Copy the target instruction to the L2 code stream, and fill
		 * the remaining bytes with INT3s so that a missed intercept
		 * results in a consistent failure mode (SHUTDOWN).
		 */
		memcpy(l2_instruction, insn->opcode, insn_len);
		memset(l2_instruction + insn_len, 0xcc, sizeof(insn->opcode) - insn_len);

		if (f == SVM_F) {
			struct svm_test_data *svm = test_data;
			struct vmcb *vmcb = svm->vmcb;

			run_guest(vmcb, svm->vmcb_gpa);
			exit_reason = vmcb->control.exit_code;
			exit_insn_len = vmcb->control.next_rip - vmcb->save.rip;
			GUEST_ASSERT_EQ(vmcb->save.rip, (u64)l2_instruction);

Annotation

Implementation Notes