xref: /openbmc/linux/tools/testing/selftests/kvm/x86_64/hyperv_ipi.c (revision 1ac731c529cd4d6adbce134754b51ff7d822b145)
199848924SVitaly Kuznetsov // SPDX-License-Identifier: GPL-2.0
299848924SVitaly Kuznetsov /*
399848924SVitaly Kuznetsov  * Hyper-V HvCallSendSyntheticClusterIpi{,Ex} tests
499848924SVitaly Kuznetsov  *
599848924SVitaly Kuznetsov  * Copyright (C) 2022, Red Hat, Inc.
699848924SVitaly Kuznetsov  *
799848924SVitaly Kuznetsov  */
899848924SVitaly Kuznetsov 
999848924SVitaly Kuznetsov #define _GNU_SOURCE /* for program_invocation_short_name */
1099848924SVitaly Kuznetsov #include <pthread.h>
1199848924SVitaly Kuznetsov #include <inttypes.h>
1299848924SVitaly Kuznetsov 
1399848924SVitaly Kuznetsov #include "kvm_util.h"
1499848924SVitaly Kuznetsov #include "hyperv.h"
1599848924SVitaly Kuznetsov #include "test_util.h"
1699848924SVitaly Kuznetsov #include "vmx.h"
1799848924SVitaly Kuznetsov 
1899848924SVitaly Kuznetsov #define RECEIVER_VCPU_ID_1 2
1999848924SVitaly Kuznetsov #define RECEIVER_VCPU_ID_2 65
2099848924SVitaly Kuznetsov 
2199848924SVitaly Kuznetsov #define IPI_VECTOR	 0xfe
2299848924SVitaly Kuznetsov 
2399848924SVitaly Kuznetsov static volatile uint64_t ipis_rcvd[RECEIVER_VCPU_ID_2 + 1];
2499848924SVitaly Kuznetsov 
2599848924SVitaly Kuznetsov struct hv_vpset {
2699848924SVitaly Kuznetsov 	u64 format;
2799848924SVitaly Kuznetsov 	u64 valid_bank_mask;
2899848924SVitaly Kuznetsov 	u64 bank_contents[2];
2999848924SVitaly Kuznetsov };
3099848924SVitaly Kuznetsov 
3199848924SVitaly Kuznetsov enum HV_GENERIC_SET_FORMAT {
3299848924SVitaly Kuznetsov 	HV_GENERIC_SET_SPARSE_4K,
3399848924SVitaly Kuznetsov 	HV_GENERIC_SET_ALL,
3499848924SVitaly Kuznetsov };
3599848924SVitaly Kuznetsov 
3699848924SVitaly Kuznetsov /* HvCallSendSyntheticClusterIpi hypercall */
3799848924SVitaly Kuznetsov struct hv_send_ipi {
3899848924SVitaly Kuznetsov 	u32 vector;
3999848924SVitaly Kuznetsov 	u32 reserved;
4099848924SVitaly Kuznetsov 	u64 cpu_mask;
4199848924SVitaly Kuznetsov };
4299848924SVitaly Kuznetsov 
4399848924SVitaly Kuznetsov /* HvCallSendSyntheticClusterIpiEx hypercall */
4499848924SVitaly Kuznetsov struct hv_send_ipi_ex {
4599848924SVitaly Kuznetsov 	u32 vector;
4699848924SVitaly Kuznetsov 	u32 reserved;
4799848924SVitaly Kuznetsov 	struct hv_vpset vp_set;
4899848924SVitaly Kuznetsov };
4999848924SVitaly Kuznetsov 
hv_init(vm_vaddr_t pgs_gpa)5099848924SVitaly Kuznetsov static inline void hv_init(vm_vaddr_t pgs_gpa)
5199848924SVitaly Kuznetsov {
5299848924SVitaly Kuznetsov 	wrmsr(HV_X64_MSR_GUEST_OS_ID, HYPERV_LINUX_OS_ID);
5399848924SVitaly Kuznetsov 	wrmsr(HV_X64_MSR_HYPERCALL, pgs_gpa);
5499848924SVitaly Kuznetsov }
5599848924SVitaly Kuznetsov 
receiver_code(void * hcall_page,vm_vaddr_t pgs_gpa)5699848924SVitaly Kuznetsov static void receiver_code(void *hcall_page, vm_vaddr_t pgs_gpa)
5799848924SVitaly Kuznetsov {
5899848924SVitaly Kuznetsov 	u32 vcpu_id;
5999848924SVitaly Kuznetsov 
6099848924SVitaly Kuznetsov 	x2apic_enable();
6199848924SVitaly Kuznetsov 	hv_init(pgs_gpa);
6299848924SVitaly Kuznetsov 
6399848924SVitaly Kuznetsov 	vcpu_id = rdmsr(HV_X64_MSR_VP_INDEX);
6499848924SVitaly Kuznetsov 
6599848924SVitaly Kuznetsov 	/* Signal sender vCPU we're ready */
6699848924SVitaly Kuznetsov 	ipis_rcvd[vcpu_id] = (u64)-1;
6799848924SVitaly Kuznetsov 
6899848924SVitaly Kuznetsov 	for (;;)
6999848924SVitaly Kuznetsov 		asm volatile("sti; hlt; cli");
7099848924SVitaly Kuznetsov }
7199848924SVitaly Kuznetsov 
guest_ipi_handler(struct ex_regs * regs)7299848924SVitaly Kuznetsov static void guest_ipi_handler(struct ex_regs *regs)
7399848924SVitaly Kuznetsov {
7499848924SVitaly Kuznetsov 	u32 vcpu_id = rdmsr(HV_X64_MSR_VP_INDEX);
7599848924SVitaly Kuznetsov 
7699848924SVitaly Kuznetsov 	ipis_rcvd[vcpu_id]++;
7799848924SVitaly Kuznetsov 	wrmsr(HV_X64_MSR_EOI, 1);
7899848924SVitaly Kuznetsov }
7999848924SVitaly Kuznetsov 
nop_loop(void)8099848924SVitaly Kuznetsov static inline void nop_loop(void)
8199848924SVitaly Kuznetsov {
8299848924SVitaly Kuznetsov 	int i;
8399848924SVitaly Kuznetsov 
8499848924SVitaly Kuznetsov 	for (i = 0; i < 100000000; i++)
8599848924SVitaly Kuznetsov 		asm volatile("nop");
8699848924SVitaly Kuznetsov }
8799848924SVitaly Kuznetsov 
sender_guest_code(void * hcall_page,vm_vaddr_t pgs_gpa)8899848924SVitaly Kuznetsov static void sender_guest_code(void *hcall_page, vm_vaddr_t pgs_gpa)
8999848924SVitaly Kuznetsov {
9099848924SVitaly Kuznetsov 	struct hv_send_ipi *ipi = (struct hv_send_ipi *)hcall_page;
9199848924SVitaly Kuznetsov 	struct hv_send_ipi_ex *ipi_ex = (struct hv_send_ipi_ex *)hcall_page;
9299848924SVitaly Kuznetsov 	int stage = 1, ipis_expected[2] = {0};
9399848924SVitaly Kuznetsov 
9499848924SVitaly Kuznetsov 	hv_init(pgs_gpa);
9599848924SVitaly Kuznetsov 	GUEST_SYNC(stage++);
9699848924SVitaly Kuznetsov 
9799848924SVitaly Kuznetsov 	/* Wait for receiver vCPUs to come up */
9899848924SVitaly Kuznetsov 	while (!ipis_rcvd[RECEIVER_VCPU_ID_1] || !ipis_rcvd[RECEIVER_VCPU_ID_2])
9999848924SVitaly Kuznetsov 		nop_loop();
10099848924SVitaly Kuznetsov 	ipis_rcvd[RECEIVER_VCPU_ID_1] = ipis_rcvd[RECEIVER_VCPU_ID_2] = 0;
10199848924SVitaly Kuznetsov 
10299848924SVitaly Kuznetsov 	/* 'Slow' HvCallSendSyntheticClusterIpi to RECEIVER_VCPU_ID_1 */
10399848924SVitaly Kuznetsov 	ipi->vector = IPI_VECTOR;
10499848924SVitaly Kuznetsov 	ipi->cpu_mask = 1 << RECEIVER_VCPU_ID_1;
10599848924SVitaly Kuznetsov 	hyperv_hypercall(HVCALL_SEND_IPI, pgs_gpa, pgs_gpa + 4096);
10699848924SVitaly Kuznetsov 	nop_loop();
10799848924SVitaly Kuznetsov 	GUEST_ASSERT(ipis_rcvd[RECEIVER_VCPU_ID_1] == ++ipis_expected[0]);
10899848924SVitaly Kuznetsov 	GUEST_ASSERT(ipis_rcvd[RECEIVER_VCPU_ID_2] == ipis_expected[1]);
10999848924SVitaly Kuznetsov 	GUEST_SYNC(stage++);
11099848924SVitaly Kuznetsov 	/* 'Fast' HvCallSendSyntheticClusterIpi to RECEIVER_VCPU_ID_1 */
11199848924SVitaly Kuznetsov 	hyperv_hypercall(HVCALL_SEND_IPI | HV_HYPERCALL_FAST_BIT,
11299848924SVitaly Kuznetsov 			 IPI_VECTOR, 1 << RECEIVER_VCPU_ID_1);
11399848924SVitaly Kuznetsov 	nop_loop();
11499848924SVitaly Kuznetsov 	GUEST_ASSERT(ipis_rcvd[RECEIVER_VCPU_ID_1] == ++ipis_expected[0]);
11599848924SVitaly Kuznetsov 	GUEST_ASSERT(ipis_rcvd[RECEIVER_VCPU_ID_2] == ipis_expected[1]);
11699848924SVitaly Kuznetsov 	GUEST_SYNC(stage++);
11799848924SVitaly Kuznetsov 
11899848924SVitaly Kuznetsov 	/* 'Slow' HvCallSendSyntheticClusterIpiEx to RECEIVER_VCPU_ID_1 */
11999848924SVitaly Kuznetsov 	memset(hcall_page, 0, 4096);
12099848924SVitaly Kuznetsov 	ipi_ex->vector = IPI_VECTOR;
12199848924SVitaly Kuznetsov 	ipi_ex->vp_set.format = HV_GENERIC_SET_SPARSE_4K;
12299848924SVitaly Kuznetsov 	ipi_ex->vp_set.valid_bank_mask = 1 << 0;
12399848924SVitaly Kuznetsov 	ipi_ex->vp_set.bank_contents[0] = BIT(RECEIVER_VCPU_ID_1);
12499848924SVitaly Kuznetsov 	hyperv_hypercall(HVCALL_SEND_IPI_EX | (1 << HV_HYPERCALL_VARHEAD_OFFSET),
12599848924SVitaly Kuznetsov 			 pgs_gpa, pgs_gpa + 4096);
12699848924SVitaly Kuznetsov 	nop_loop();
12799848924SVitaly Kuznetsov 	GUEST_ASSERT(ipis_rcvd[RECEIVER_VCPU_ID_1] == ++ipis_expected[0]);
12899848924SVitaly Kuznetsov 	GUEST_ASSERT(ipis_rcvd[RECEIVER_VCPU_ID_2] == ipis_expected[1]);
12999848924SVitaly Kuznetsov 	GUEST_SYNC(stage++);
13099848924SVitaly Kuznetsov 	/* 'XMM Fast' HvCallSendSyntheticClusterIpiEx to RECEIVER_VCPU_ID_1 */
13199848924SVitaly Kuznetsov 	hyperv_write_xmm_input(&ipi_ex->vp_set.valid_bank_mask, 1);
13299848924SVitaly Kuznetsov 	hyperv_hypercall(HVCALL_SEND_IPI_EX | HV_HYPERCALL_FAST_BIT |
13399848924SVitaly Kuznetsov 			 (1 << HV_HYPERCALL_VARHEAD_OFFSET),
13499848924SVitaly Kuznetsov 			 IPI_VECTOR, HV_GENERIC_SET_SPARSE_4K);
13599848924SVitaly Kuznetsov 	nop_loop();
13699848924SVitaly Kuznetsov 	GUEST_ASSERT(ipis_rcvd[RECEIVER_VCPU_ID_1] == ++ipis_expected[0]);
13799848924SVitaly Kuznetsov 	GUEST_ASSERT(ipis_rcvd[RECEIVER_VCPU_ID_2] == ipis_expected[1]);
13899848924SVitaly Kuznetsov 	GUEST_SYNC(stage++);
13999848924SVitaly Kuznetsov 
14099848924SVitaly Kuznetsov 	/* 'Slow' HvCallSendSyntheticClusterIpiEx to RECEIVER_VCPU_ID_2 */
14199848924SVitaly Kuznetsov 	memset(hcall_page, 0, 4096);
14299848924SVitaly Kuznetsov 	ipi_ex->vector = IPI_VECTOR;
14399848924SVitaly Kuznetsov 	ipi_ex->vp_set.format = HV_GENERIC_SET_SPARSE_4K;
14499848924SVitaly Kuznetsov 	ipi_ex->vp_set.valid_bank_mask = 1 << 1;
14599848924SVitaly Kuznetsov 	ipi_ex->vp_set.bank_contents[0] = BIT(RECEIVER_VCPU_ID_2 - 64);
14699848924SVitaly Kuznetsov 	hyperv_hypercall(HVCALL_SEND_IPI_EX | (1 << HV_HYPERCALL_VARHEAD_OFFSET),
14799848924SVitaly Kuznetsov 			 pgs_gpa, pgs_gpa + 4096);
14899848924SVitaly Kuznetsov 	nop_loop();
14999848924SVitaly Kuznetsov 	GUEST_ASSERT(ipis_rcvd[RECEIVER_VCPU_ID_1] == ipis_expected[0]);
15099848924SVitaly Kuznetsov 	GUEST_ASSERT(ipis_rcvd[RECEIVER_VCPU_ID_2] == ++ipis_expected[1]);
15199848924SVitaly Kuznetsov 	GUEST_SYNC(stage++);
15299848924SVitaly Kuznetsov 	/* 'XMM Fast' HvCallSendSyntheticClusterIpiEx to RECEIVER_VCPU_ID_2 */
15399848924SVitaly Kuznetsov 	hyperv_write_xmm_input(&ipi_ex->vp_set.valid_bank_mask, 1);
15499848924SVitaly Kuznetsov 	hyperv_hypercall(HVCALL_SEND_IPI_EX | HV_HYPERCALL_FAST_BIT |
15599848924SVitaly Kuznetsov 			 (1 << HV_HYPERCALL_VARHEAD_OFFSET),
15699848924SVitaly Kuznetsov 			 IPI_VECTOR, HV_GENERIC_SET_SPARSE_4K);
15799848924SVitaly Kuznetsov 	nop_loop();
15899848924SVitaly Kuznetsov 	GUEST_ASSERT(ipis_rcvd[RECEIVER_VCPU_ID_1] == ipis_expected[0]);
15999848924SVitaly Kuznetsov 	GUEST_ASSERT(ipis_rcvd[RECEIVER_VCPU_ID_2] == ++ipis_expected[1]);
16099848924SVitaly Kuznetsov 	GUEST_SYNC(stage++);
16199848924SVitaly Kuznetsov 
16299848924SVitaly Kuznetsov 	/* 'Slow' HvCallSendSyntheticClusterIpiEx to both RECEIVER_VCPU_ID_{1,2} */
16399848924SVitaly Kuznetsov 	memset(hcall_page, 0, 4096);
16499848924SVitaly Kuznetsov 	ipi_ex->vector = IPI_VECTOR;
16599848924SVitaly Kuznetsov 	ipi_ex->vp_set.format = HV_GENERIC_SET_SPARSE_4K;
16699848924SVitaly Kuznetsov 	ipi_ex->vp_set.valid_bank_mask = 1 << 1 | 1;
16799848924SVitaly Kuznetsov 	ipi_ex->vp_set.bank_contents[0] = BIT(RECEIVER_VCPU_ID_1);
16899848924SVitaly Kuznetsov 	ipi_ex->vp_set.bank_contents[1] = BIT(RECEIVER_VCPU_ID_2 - 64);
16999848924SVitaly Kuznetsov 	hyperv_hypercall(HVCALL_SEND_IPI_EX | (2 << HV_HYPERCALL_VARHEAD_OFFSET),
17099848924SVitaly Kuznetsov 			 pgs_gpa, pgs_gpa + 4096);
17199848924SVitaly Kuznetsov 	nop_loop();
17299848924SVitaly Kuznetsov 	GUEST_ASSERT(ipis_rcvd[RECEIVER_VCPU_ID_1] == ++ipis_expected[0]);
17399848924SVitaly Kuznetsov 	GUEST_ASSERT(ipis_rcvd[RECEIVER_VCPU_ID_2] == ++ipis_expected[1]);
17499848924SVitaly Kuznetsov 	GUEST_SYNC(stage++);
17599848924SVitaly Kuznetsov 	/* 'XMM Fast' HvCallSendSyntheticClusterIpiEx to both RECEIVER_VCPU_ID_{1, 2} */
17699848924SVitaly Kuznetsov 	hyperv_write_xmm_input(&ipi_ex->vp_set.valid_bank_mask, 2);
17799848924SVitaly Kuznetsov 	hyperv_hypercall(HVCALL_SEND_IPI_EX | HV_HYPERCALL_FAST_BIT |
17899848924SVitaly Kuznetsov 			 (2 << HV_HYPERCALL_VARHEAD_OFFSET),
17999848924SVitaly Kuznetsov 			 IPI_VECTOR, HV_GENERIC_SET_SPARSE_4K);
18099848924SVitaly Kuznetsov 	nop_loop();
18199848924SVitaly Kuznetsov 	GUEST_ASSERT(ipis_rcvd[RECEIVER_VCPU_ID_1] == ++ipis_expected[0]);
18299848924SVitaly Kuznetsov 	GUEST_ASSERT(ipis_rcvd[RECEIVER_VCPU_ID_2] == ++ipis_expected[1]);
18399848924SVitaly Kuznetsov 	GUEST_SYNC(stage++);
18499848924SVitaly Kuznetsov 
18599848924SVitaly Kuznetsov 	/* 'Slow' HvCallSendSyntheticClusterIpiEx to HV_GENERIC_SET_ALL */
18699848924SVitaly Kuznetsov 	memset(hcall_page, 0, 4096);
18799848924SVitaly Kuznetsov 	ipi_ex->vector = IPI_VECTOR;
18899848924SVitaly Kuznetsov 	ipi_ex->vp_set.format = HV_GENERIC_SET_ALL;
18999848924SVitaly Kuznetsov 	hyperv_hypercall(HVCALL_SEND_IPI_EX, pgs_gpa, pgs_gpa + 4096);
19099848924SVitaly Kuznetsov 	nop_loop();
19199848924SVitaly Kuznetsov 	GUEST_ASSERT(ipis_rcvd[RECEIVER_VCPU_ID_1] == ++ipis_expected[0]);
19299848924SVitaly Kuznetsov 	GUEST_ASSERT(ipis_rcvd[RECEIVER_VCPU_ID_2] == ++ipis_expected[1]);
19399848924SVitaly Kuznetsov 	GUEST_SYNC(stage++);
19499848924SVitaly Kuznetsov 	/*
19599848924SVitaly Kuznetsov 	 * 'XMM Fast' HvCallSendSyntheticClusterIpiEx to HV_GENERIC_SET_ALL.
19699848924SVitaly Kuznetsov 	 */
19753800f88SSean Christopherson 	ipi_ex->vp_set.valid_bank_mask = 0;
19853800f88SSean Christopherson 	hyperv_write_xmm_input(&ipi_ex->vp_set.valid_bank_mask, 2);
19999848924SVitaly Kuznetsov 	hyperv_hypercall(HVCALL_SEND_IPI_EX | HV_HYPERCALL_FAST_BIT,
20099848924SVitaly Kuznetsov 			 IPI_VECTOR, HV_GENERIC_SET_ALL);
20199848924SVitaly Kuznetsov 	nop_loop();
20299848924SVitaly Kuznetsov 	GUEST_ASSERT(ipis_rcvd[RECEIVER_VCPU_ID_1] == ++ipis_expected[0]);
20399848924SVitaly Kuznetsov 	GUEST_ASSERT(ipis_rcvd[RECEIVER_VCPU_ID_2] == ++ipis_expected[1]);
20499848924SVitaly Kuznetsov 	GUEST_SYNC(stage++);
20599848924SVitaly Kuznetsov 
20699848924SVitaly Kuznetsov 	GUEST_DONE();
20799848924SVitaly Kuznetsov }
20899848924SVitaly Kuznetsov 
vcpu_thread(void * arg)20999848924SVitaly Kuznetsov static void *vcpu_thread(void *arg)
21099848924SVitaly Kuznetsov {
21199848924SVitaly Kuznetsov 	struct kvm_vcpu *vcpu = (struct kvm_vcpu *)arg;
21299848924SVitaly Kuznetsov 	int old, r;
21399848924SVitaly Kuznetsov 
21499848924SVitaly Kuznetsov 	r = pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &old);
21599848924SVitaly Kuznetsov 	TEST_ASSERT(!r, "pthread_setcanceltype failed on vcpu_id=%u with errno=%d",
21699848924SVitaly Kuznetsov 		    vcpu->id, r);
21799848924SVitaly Kuznetsov 
21899848924SVitaly Kuznetsov 	vcpu_run(vcpu);
21999848924SVitaly Kuznetsov 
22099848924SVitaly Kuznetsov 	TEST_FAIL("vCPU %u exited unexpectedly", vcpu->id);
22199848924SVitaly Kuznetsov 
22299848924SVitaly Kuznetsov 	return NULL;
22399848924SVitaly Kuznetsov }
22499848924SVitaly Kuznetsov 
cancel_join_vcpu_thread(pthread_t thread,struct kvm_vcpu * vcpu)22599848924SVitaly Kuznetsov static void cancel_join_vcpu_thread(pthread_t thread, struct kvm_vcpu *vcpu)
22699848924SVitaly Kuznetsov {
22799848924SVitaly Kuznetsov 	void *retval;
22899848924SVitaly Kuznetsov 	int r;
22999848924SVitaly Kuznetsov 
23099848924SVitaly Kuznetsov 	r = pthread_cancel(thread);
23199848924SVitaly Kuznetsov 	TEST_ASSERT(!r, "pthread_cancel on vcpu_id=%d failed with errno=%d",
23299848924SVitaly Kuznetsov 		    vcpu->id, r);
23399848924SVitaly Kuznetsov 
23499848924SVitaly Kuznetsov 	r = pthread_join(thread, &retval);
23599848924SVitaly Kuznetsov 	TEST_ASSERT(!r, "pthread_join on vcpu_id=%d failed with errno=%d",
23699848924SVitaly Kuznetsov 		    vcpu->id, r);
23799848924SVitaly Kuznetsov 	TEST_ASSERT(retval == PTHREAD_CANCELED,
23899848924SVitaly Kuznetsov 		    "expected retval=%p, got %p", PTHREAD_CANCELED,
23999848924SVitaly Kuznetsov 		    retval);
24099848924SVitaly Kuznetsov }
24199848924SVitaly Kuznetsov 
main(int argc,char * argv[])24299848924SVitaly Kuznetsov int main(int argc, char *argv[])
24399848924SVitaly Kuznetsov {
24499848924SVitaly Kuznetsov 	struct kvm_vm *vm;
24599848924SVitaly Kuznetsov 	struct kvm_vcpu *vcpu[3];
24699848924SVitaly Kuznetsov 	vm_vaddr_t hcall_page;
24799848924SVitaly Kuznetsov 	pthread_t threads[2];
24899848924SVitaly Kuznetsov 	int stage = 1, r;
24999848924SVitaly Kuznetsov 	struct ucall uc;
25099848924SVitaly Kuznetsov 
25199848924SVitaly Kuznetsov 	vm = vm_create_with_one_vcpu(&vcpu[0], sender_guest_code);
25299848924SVitaly Kuznetsov 
25399848924SVitaly Kuznetsov 	/* Hypercall input/output */
25499848924SVitaly Kuznetsov 	hcall_page = vm_vaddr_alloc_pages(vm, 2);
25599848924SVitaly Kuznetsov 	memset(addr_gva2hva(vm, hcall_page), 0x0, 2 * getpagesize());
25699848924SVitaly Kuznetsov 
25799848924SVitaly Kuznetsov 	vm_init_descriptor_tables(vm);
25899848924SVitaly Kuznetsov 
25999848924SVitaly Kuznetsov 	vcpu[1] = vm_vcpu_add(vm, RECEIVER_VCPU_ID_1, receiver_code);
26099848924SVitaly Kuznetsov 	vcpu_init_descriptor_tables(vcpu[1]);
26199848924SVitaly Kuznetsov 	vcpu_args_set(vcpu[1], 2, hcall_page, addr_gva2gpa(vm, hcall_page));
26299848924SVitaly Kuznetsov 	vcpu_set_msr(vcpu[1], HV_X64_MSR_VP_INDEX, RECEIVER_VCPU_ID_1);
26399848924SVitaly Kuznetsov 	vcpu_set_hv_cpuid(vcpu[1]);
26499848924SVitaly Kuznetsov 
26599848924SVitaly Kuznetsov 	vcpu[2] = vm_vcpu_add(vm, RECEIVER_VCPU_ID_2, receiver_code);
26699848924SVitaly Kuznetsov 	vcpu_init_descriptor_tables(vcpu[2]);
26799848924SVitaly Kuznetsov 	vcpu_args_set(vcpu[2], 2, hcall_page, addr_gva2gpa(vm, hcall_page));
26899848924SVitaly Kuznetsov 	vcpu_set_msr(vcpu[2], HV_X64_MSR_VP_INDEX, RECEIVER_VCPU_ID_2);
26999848924SVitaly Kuznetsov 	vcpu_set_hv_cpuid(vcpu[2]);
27099848924SVitaly Kuznetsov 
27199848924SVitaly Kuznetsov 	vm_install_exception_handler(vm, IPI_VECTOR, guest_ipi_handler);
27299848924SVitaly Kuznetsov 
27399848924SVitaly Kuznetsov 	vcpu_args_set(vcpu[0], 2, hcall_page, addr_gva2gpa(vm, hcall_page));
27499848924SVitaly Kuznetsov 	vcpu_set_hv_cpuid(vcpu[0]);
27599848924SVitaly Kuznetsov 
27699848924SVitaly Kuznetsov 	r = pthread_create(&threads[0], NULL, vcpu_thread, vcpu[1]);
27799848924SVitaly Kuznetsov 	TEST_ASSERT(!r, "pthread_create failed errno=%d", r);
27899848924SVitaly Kuznetsov 
27999848924SVitaly Kuznetsov 	r = pthread_create(&threads[1], NULL, vcpu_thread, vcpu[2]);
28099848924SVitaly Kuznetsov 	TEST_ASSERT(!r, "pthread_create failed errno=%d", errno);
28199848924SVitaly Kuznetsov 
28299848924SVitaly Kuznetsov 	while (true) {
28399848924SVitaly Kuznetsov 		vcpu_run(vcpu[0]);
28499848924SVitaly Kuznetsov 
285*c96f57b0SVipin Sharma 		TEST_ASSERT_KVM_EXIT_REASON(vcpu[0], KVM_EXIT_IO);
28699848924SVitaly Kuznetsov 
28799848924SVitaly Kuznetsov 		switch (get_ucall(vcpu[0], &uc)) {
28899848924SVitaly Kuznetsov 		case UCALL_SYNC:
28999848924SVitaly Kuznetsov 			TEST_ASSERT(uc.args[1] == stage,
29099848924SVitaly Kuznetsov 				    "Unexpected stage: %ld (%d expected)\n",
29199848924SVitaly Kuznetsov 				    uc.args[1], stage);
29299848924SVitaly Kuznetsov 			break;
29399848924SVitaly Kuznetsov 		case UCALL_DONE:
29499848924SVitaly Kuznetsov 			goto done;
29599848924SVitaly Kuznetsov 		case UCALL_ABORT:
29699848924SVitaly Kuznetsov 			REPORT_GUEST_ASSERT(uc);
29799848924SVitaly Kuznetsov 			/* NOT REACHED */
29899848924SVitaly Kuznetsov 		default:
29999848924SVitaly Kuznetsov 			TEST_FAIL("Unknown ucall %lu", uc.cmd);
30099848924SVitaly Kuznetsov 		}
30199848924SVitaly Kuznetsov 
30299848924SVitaly Kuznetsov 		stage++;
30399848924SVitaly Kuznetsov 	}
30499848924SVitaly Kuznetsov 
30599848924SVitaly Kuznetsov done:
30699848924SVitaly Kuznetsov 	cancel_join_vcpu_thread(threads[0], vcpu[1]);
30799848924SVitaly Kuznetsov 	cancel_join_vcpu_thread(threads[1], vcpu[2]);
30899848924SVitaly Kuznetsov 	kvm_vm_free(vm);
30999848924SVitaly Kuznetsov 
31099848924SVitaly Kuznetsov 	return r;
31199848924SVitaly Kuznetsov }
312