xref: /linux/tools/testing/selftests/kvm/arm64/page_fault_test.c (revision 0410c6121529409b08e81a77ae3ee58c657e2243)
1*67730e6cSSean Christopherson // SPDX-License-Identifier: GPL-2.0
2*67730e6cSSean Christopherson /*
3*67730e6cSSean Christopherson  * page_fault_test.c - Test stage 2 faults.
4*67730e6cSSean Christopherson  *
5*67730e6cSSean Christopherson  * This test tries different combinations of guest accesses (e.g., write,
6*67730e6cSSean Christopherson  * S1PTW), backing source type (e.g., anon) and types of faults (e.g., read on
7*67730e6cSSean Christopherson  * hugetlbfs with a hole). It checks that the expected handling method is
8*67730e6cSSean Christopherson  * called (e.g., uffd faults with the right address and write/read flag).
9*67730e6cSSean Christopherson  */
10*67730e6cSSean Christopherson #include <linux/bitmap.h>
11*67730e6cSSean Christopherson #include <fcntl.h>
12*67730e6cSSean Christopherson #include <test_util.h>
13*67730e6cSSean Christopherson #include <kvm_util.h>
14*67730e6cSSean Christopherson #include <processor.h>
15*67730e6cSSean Christopherson #include <asm/sysreg.h>
16*67730e6cSSean Christopherson #include <linux/bitfield.h>
17*67730e6cSSean Christopherson #include "guest_modes.h"
18*67730e6cSSean Christopherson #include "userfaultfd_util.h"
19*67730e6cSSean Christopherson 
20*67730e6cSSean Christopherson /* Guest virtual addresses that point to the test page and its PTE. */
21*67730e6cSSean Christopherson #define TEST_GVA				0xc0000000
22*67730e6cSSean Christopherson #define TEST_EXEC_GVA				(TEST_GVA + 0x8)
23*67730e6cSSean Christopherson #define TEST_PTE_GVA				0xb0000000
24*67730e6cSSean Christopherson #define TEST_DATA				0x0123456789ABCDEF
25*67730e6cSSean Christopherson 
26*67730e6cSSean Christopherson static uint64_t *guest_test_memory = (uint64_t *)TEST_GVA;
27*67730e6cSSean Christopherson 
28*67730e6cSSean Christopherson #define CMD_NONE				(0)
29*67730e6cSSean Christopherson #define CMD_SKIP_TEST				(1ULL << 1)
30*67730e6cSSean Christopherson #define CMD_HOLE_PT				(1ULL << 2)
31*67730e6cSSean Christopherson #define CMD_HOLE_DATA				(1ULL << 3)
32*67730e6cSSean Christopherson #define CMD_CHECK_WRITE_IN_DIRTY_LOG		(1ULL << 4)
33*67730e6cSSean Christopherson #define CMD_CHECK_S1PTW_WR_IN_DIRTY_LOG		(1ULL << 5)
34*67730e6cSSean Christopherson #define CMD_CHECK_NO_WRITE_IN_DIRTY_LOG		(1ULL << 6)
35*67730e6cSSean Christopherson #define CMD_CHECK_NO_S1PTW_WR_IN_DIRTY_LOG	(1ULL << 7)
36*67730e6cSSean Christopherson #define CMD_SET_PTE_AF				(1ULL << 8)
37*67730e6cSSean Christopherson 
38*67730e6cSSean Christopherson #define PREPARE_FN_NR				10
39*67730e6cSSean Christopherson #define CHECK_FN_NR				10
40*67730e6cSSean Christopherson 
41*67730e6cSSean Christopherson static struct event_cnt {
42*67730e6cSSean Christopherson 	int mmio_exits;
43*67730e6cSSean Christopherson 	int fail_vcpu_runs;
44*67730e6cSSean Christopherson 	int uffd_faults;
45*67730e6cSSean Christopherson 	/* uffd_faults is incremented from multiple threads. */
46*67730e6cSSean Christopherson 	pthread_mutex_t uffd_faults_mutex;
47*67730e6cSSean Christopherson } events;
48*67730e6cSSean Christopherson 
49*67730e6cSSean Christopherson struct test_desc {
50*67730e6cSSean Christopherson 	const char *name;
51*67730e6cSSean Christopherson 	uint64_t mem_mark_cmd;
52*67730e6cSSean Christopherson 	/* Skip the test if any prepare function returns false */
53*67730e6cSSean Christopherson 	bool (*guest_prepare[PREPARE_FN_NR])(void);
54*67730e6cSSean Christopherson 	void (*guest_test)(void);
55*67730e6cSSean Christopherson 	void (*guest_test_check[CHECK_FN_NR])(void);
56*67730e6cSSean Christopherson 	uffd_handler_t uffd_pt_handler;
57*67730e6cSSean Christopherson 	uffd_handler_t uffd_data_handler;
58*67730e6cSSean Christopherson 	void (*dabt_handler)(struct ex_regs *regs);
59*67730e6cSSean Christopherson 	void (*iabt_handler)(struct ex_regs *regs);
60*67730e6cSSean Christopherson 	void (*mmio_handler)(struct kvm_vm *vm, struct kvm_run *run);
61*67730e6cSSean Christopherson 	void (*fail_vcpu_run_handler)(int ret);
62*67730e6cSSean Christopherson 	uint32_t pt_memslot_flags;
63*67730e6cSSean Christopherson 	uint32_t data_memslot_flags;
64*67730e6cSSean Christopherson 	bool skip;
65*67730e6cSSean Christopherson 	struct event_cnt expected_events;
66*67730e6cSSean Christopherson };
67*67730e6cSSean Christopherson 
68*67730e6cSSean Christopherson struct test_params {
69*67730e6cSSean Christopherson 	enum vm_mem_backing_src_type src_type;
70*67730e6cSSean Christopherson 	struct test_desc *test_desc;
71*67730e6cSSean Christopherson };
72*67730e6cSSean Christopherson 
73*67730e6cSSean Christopherson static inline void flush_tlb_page(uint64_t vaddr)
74*67730e6cSSean Christopherson {
75*67730e6cSSean Christopherson 	uint64_t page = vaddr >> 12;
76*67730e6cSSean Christopherson 
77*67730e6cSSean Christopherson 	dsb(ishst);
78*67730e6cSSean Christopherson 	asm volatile("tlbi vaae1is, %0" :: "r" (page));
79*67730e6cSSean Christopherson 	dsb(ish);
80*67730e6cSSean Christopherson 	isb();
81*67730e6cSSean Christopherson }
82*67730e6cSSean Christopherson 
83*67730e6cSSean Christopherson static void guest_write64(void)
84*67730e6cSSean Christopherson {
85*67730e6cSSean Christopherson 	uint64_t val;
86*67730e6cSSean Christopherson 
87*67730e6cSSean Christopherson 	WRITE_ONCE(*guest_test_memory, TEST_DATA);
88*67730e6cSSean Christopherson 	val = READ_ONCE(*guest_test_memory);
89*67730e6cSSean Christopherson 	GUEST_ASSERT_EQ(val, TEST_DATA);
90*67730e6cSSean Christopherson }
91*67730e6cSSean Christopherson 
92*67730e6cSSean Christopherson /* Check the system for atomic instructions. */
93*67730e6cSSean Christopherson static bool guest_check_lse(void)
94*67730e6cSSean Christopherson {
95*67730e6cSSean Christopherson 	uint64_t isar0 = read_sysreg(id_aa64isar0_el1);
96*67730e6cSSean Christopherson 	uint64_t atomic;
97*67730e6cSSean Christopherson 
98*67730e6cSSean Christopherson 	atomic = FIELD_GET(ARM64_FEATURE_MASK(ID_AA64ISAR0_EL1_ATOMIC), isar0);
99*67730e6cSSean Christopherson 	return atomic >= 2;
100*67730e6cSSean Christopherson }
101*67730e6cSSean Christopherson 
102*67730e6cSSean Christopherson static bool guest_check_dc_zva(void)
103*67730e6cSSean Christopherson {
104*67730e6cSSean Christopherson 	uint64_t dczid = read_sysreg(dczid_el0);
105*67730e6cSSean Christopherson 	uint64_t dzp = FIELD_GET(ARM64_FEATURE_MASK(DCZID_EL0_DZP), dczid);
106*67730e6cSSean Christopherson 
107*67730e6cSSean Christopherson 	return dzp == 0;
108*67730e6cSSean Christopherson }
109*67730e6cSSean Christopherson 
110*67730e6cSSean Christopherson /* Compare and swap instruction. */
111*67730e6cSSean Christopherson static void guest_cas(void)
112*67730e6cSSean Christopherson {
113*67730e6cSSean Christopherson 	uint64_t val;
114*67730e6cSSean Christopherson 
115*67730e6cSSean Christopherson 	GUEST_ASSERT(guest_check_lse());
116*67730e6cSSean Christopherson 	asm volatile(".arch_extension lse\n"
117*67730e6cSSean Christopherson 		     "casal %0, %1, [%2]\n"
118*67730e6cSSean Christopherson 		     :: "r" (0ul), "r" (TEST_DATA), "r" (guest_test_memory));
119*67730e6cSSean Christopherson 	val = READ_ONCE(*guest_test_memory);
120*67730e6cSSean Christopherson 	GUEST_ASSERT_EQ(val, TEST_DATA);
121*67730e6cSSean Christopherson }
122*67730e6cSSean Christopherson 
123*67730e6cSSean Christopherson static void guest_read64(void)
124*67730e6cSSean Christopherson {
125*67730e6cSSean Christopherson 	uint64_t val;
126*67730e6cSSean Christopherson 
127*67730e6cSSean Christopherson 	val = READ_ONCE(*guest_test_memory);
128*67730e6cSSean Christopherson 	GUEST_ASSERT_EQ(val, 0);
129*67730e6cSSean Christopherson }
130*67730e6cSSean Christopherson 
131*67730e6cSSean Christopherson /* Address translation instruction */
132*67730e6cSSean Christopherson static void guest_at(void)
133*67730e6cSSean Christopherson {
134*67730e6cSSean Christopherson 	uint64_t par;
135*67730e6cSSean Christopherson 
136*67730e6cSSean Christopherson 	asm volatile("at s1e1r, %0" :: "r" (guest_test_memory));
137*67730e6cSSean Christopherson 	isb();
138*67730e6cSSean Christopherson 	par = read_sysreg(par_el1);
139*67730e6cSSean Christopherson 
140*67730e6cSSean Christopherson 	/* Bit 1 indicates whether the AT was successful */
141*67730e6cSSean Christopherson 	GUEST_ASSERT_EQ(par & 1, 0);
142*67730e6cSSean Christopherson }
143*67730e6cSSean Christopherson 
144*67730e6cSSean Christopherson /*
145*67730e6cSSean Christopherson  * The size of the block written by "dc zva" is guaranteed to be between (2 <<
146*67730e6cSSean Christopherson  * 0) and (2 << 9), which is safe in our case as we need the write to happen
147*67730e6cSSean Christopherson  * for at least a word, and not more than a page.
148*67730e6cSSean Christopherson  */
149*67730e6cSSean Christopherson static void guest_dc_zva(void)
150*67730e6cSSean Christopherson {
151*67730e6cSSean Christopherson 	uint16_t val;
152*67730e6cSSean Christopherson 
153*67730e6cSSean Christopherson 	asm volatile("dc zva, %0" :: "r" (guest_test_memory));
154*67730e6cSSean Christopherson 	dsb(ish);
155*67730e6cSSean Christopherson 	val = READ_ONCE(*guest_test_memory);
156*67730e6cSSean Christopherson 	GUEST_ASSERT_EQ(val, 0);
157*67730e6cSSean Christopherson }
158*67730e6cSSean Christopherson 
159*67730e6cSSean Christopherson /*
160*67730e6cSSean Christopherson  * Pre-indexing loads and stores don't have a valid syndrome (ESR_EL2.ISV==0).
161*67730e6cSSean Christopherson  * And that's special because KVM must take special care with those: they
162*67730e6cSSean Christopherson  * should still count as accesses for dirty logging or user-faulting, but
163*67730e6cSSean Christopherson  * should be handled differently on mmio.
164*67730e6cSSean Christopherson  */
165*67730e6cSSean Christopherson static void guest_ld_preidx(void)
166*67730e6cSSean Christopherson {
167*67730e6cSSean Christopherson 	uint64_t val;
168*67730e6cSSean Christopherson 	uint64_t addr = TEST_GVA - 8;
169*67730e6cSSean Christopherson 
170*67730e6cSSean Christopherson 	/*
171*67730e6cSSean Christopherson 	 * This ends up accessing "TEST_GVA + 8 - 8", where "TEST_GVA - 8" is
172*67730e6cSSean Christopherson 	 * in a gap between memslots not backing by anything.
173*67730e6cSSean Christopherson 	 */
174*67730e6cSSean Christopherson 	asm volatile("ldr %0, [%1, #8]!"
175*67730e6cSSean Christopherson 		     : "=r" (val), "+r" (addr));
176*67730e6cSSean Christopherson 	GUEST_ASSERT_EQ(val, 0);
177*67730e6cSSean Christopherson 	GUEST_ASSERT_EQ(addr, TEST_GVA);
178*67730e6cSSean Christopherson }
179*67730e6cSSean Christopherson 
180*67730e6cSSean Christopherson static void guest_st_preidx(void)
181*67730e6cSSean Christopherson {
182*67730e6cSSean Christopherson 	uint64_t val = TEST_DATA;
183*67730e6cSSean Christopherson 	uint64_t addr = TEST_GVA - 8;
184*67730e6cSSean Christopherson 
185*67730e6cSSean Christopherson 	asm volatile("str %0, [%1, #8]!"
186*67730e6cSSean Christopherson 		     : "+r" (val), "+r" (addr));
187*67730e6cSSean Christopherson 
188*67730e6cSSean Christopherson 	GUEST_ASSERT_EQ(addr, TEST_GVA);
189*67730e6cSSean Christopherson 	val = READ_ONCE(*guest_test_memory);
190*67730e6cSSean Christopherson }
191*67730e6cSSean Christopherson 
192*67730e6cSSean Christopherson static bool guest_set_ha(void)
193*67730e6cSSean Christopherson {
194*67730e6cSSean Christopherson 	uint64_t mmfr1 = read_sysreg(id_aa64mmfr1_el1);
195*67730e6cSSean Christopherson 	uint64_t hadbs, tcr;
196*67730e6cSSean Christopherson 
197*67730e6cSSean Christopherson 	/* Skip if HA is not supported. */
198*67730e6cSSean Christopherson 	hadbs = FIELD_GET(ARM64_FEATURE_MASK(ID_AA64MMFR1_EL1_HAFDBS), mmfr1);
199*67730e6cSSean Christopherson 	if (hadbs == 0)
200*67730e6cSSean Christopherson 		return false;
201*67730e6cSSean Christopherson 
202*67730e6cSSean Christopherson 	tcr = read_sysreg(tcr_el1) | TCR_EL1_HA;
203*67730e6cSSean Christopherson 	write_sysreg(tcr, tcr_el1);
204*67730e6cSSean Christopherson 	isb();
205*67730e6cSSean Christopherson 
206*67730e6cSSean Christopherson 	return true;
207*67730e6cSSean Christopherson }
208*67730e6cSSean Christopherson 
209*67730e6cSSean Christopherson static bool guest_clear_pte_af(void)
210*67730e6cSSean Christopherson {
211*67730e6cSSean Christopherson 	*((uint64_t *)TEST_PTE_GVA) &= ~PTE_AF;
212*67730e6cSSean Christopherson 	flush_tlb_page(TEST_GVA);
213*67730e6cSSean Christopherson 
214*67730e6cSSean Christopherson 	return true;
215*67730e6cSSean Christopherson }
216*67730e6cSSean Christopherson 
217*67730e6cSSean Christopherson static void guest_check_pte_af(void)
218*67730e6cSSean Christopherson {
219*67730e6cSSean Christopherson 	dsb(ish);
220*67730e6cSSean Christopherson 	GUEST_ASSERT_EQ(*((uint64_t *)TEST_PTE_GVA) & PTE_AF, PTE_AF);
221*67730e6cSSean Christopherson }
222*67730e6cSSean Christopherson 
223*67730e6cSSean Christopherson static void guest_check_write_in_dirty_log(void)
224*67730e6cSSean Christopherson {
225*67730e6cSSean Christopherson 	GUEST_SYNC(CMD_CHECK_WRITE_IN_DIRTY_LOG);
226*67730e6cSSean Christopherson }
227*67730e6cSSean Christopherson 
228*67730e6cSSean Christopherson static void guest_check_no_write_in_dirty_log(void)
229*67730e6cSSean Christopherson {
230*67730e6cSSean Christopherson 	GUEST_SYNC(CMD_CHECK_NO_WRITE_IN_DIRTY_LOG);
231*67730e6cSSean Christopherson }
232*67730e6cSSean Christopherson 
233*67730e6cSSean Christopherson static void guest_check_s1ptw_wr_in_dirty_log(void)
234*67730e6cSSean Christopherson {
235*67730e6cSSean Christopherson 	GUEST_SYNC(CMD_CHECK_S1PTW_WR_IN_DIRTY_LOG);
236*67730e6cSSean Christopherson }
237*67730e6cSSean Christopherson 
238*67730e6cSSean Christopherson static void guest_check_no_s1ptw_wr_in_dirty_log(void)
239*67730e6cSSean Christopherson {
240*67730e6cSSean Christopherson 	GUEST_SYNC(CMD_CHECK_NO_S1PTW_WR_IN_DIRTY_LOG);
241*67730e6cSSean Christopherson }
242*67730e6cSSean Christopherson 
243*67730e6cSSean Christopherson static void guest_exec(void)
244*67730e6cSSean Christopherson {
245*67730e6cSSean Christopherson 	int (*code)(void) = (int (*)(void))TEST_EXEC_GVA;
246*67730e6cSSean Christopherson 	int ret;
247*67730e6cSSean Christopherson 
248*67730e6cSSean Christopherson 	ret = code();
249*67730e6cSSean Christopherson 	GUEST_ASSERT_EQ(ret, 0x77);
250*67730e6cSSean Christopherson }
251*67730e6cSSean Christopherson 
252*67730e6cSSean Christopherson static bool guest_prepare(struct test_desc *test)
253*67730e6cSSean Christopherson {
254*67730e6cSSean Christopherson 	bool (*prepare_fn)(void);
255*67730e6cSSean Christopherson 	int i;
256*67730e6cSSean Christopherson 
257*67730e6cSSean Christopherson 	for (i = 0; i < PREPARE_FN_NR; i++) {
258*67730e6cSSean Christopherson 		prepare_fn = test->guest_prepare[i];
259*67730e6cSSean Christopherson 		if (prepare_fn && !prepare_fn())
260*67730e6cSSean Christopherson 			return false;
261*67730e6cSSean Christopherson 	}
262*67730e6cSSean Christopherson 
263*67730e6cSSean Christopherson 	return true;
264*67730e6cSSean Christopherson }
265*67730e6cSSean Christopherson 
266*67730e6cSSean Christopherson static void guest_test_check(struct test_desc *test)
267*67730e6cSSean Christopherson {
268*67730e6cSSean Christopherson 	void (*check_fn)(void);
269*67730e6cSSean Christopherson 	int i;
270*67730e6cSSean Christopherson 
271*67730e6cSSean Christopherson 	for (i = 0; i < CHECK_FN_NR; i++) {
272*67730e6cSSean Christopherson 		check_fn = test->guest_test_check[i];
273*67730e6cSSean Christopherson 		if (check_fn)
274*67730e6cSSean Christopherson 			check_fn();
275*67730e6cSSean Christopherson 	}
276*67730e6cSSean Christopherson }
277*67730e6cSSean Christopherson 
278*67730e6cSSean Christopherson static void guest_code(struct test_desc *test)
279*67730e6cSSean Christopherson {
280*67730e6cSSean Christopherson 	if (!guest_prepare(test))
281*67730e6cSSean Christopherson 		GUEST_SYNC(CMD_SKIP_TEST);
282*67730e6cSSean Christopherson 
283*67730e6cSSean Christopherson 	GUEST_SYNC(test->mem_mark_cmd);
284*67730e6cSSean Christopherson 
285*67730e6cSSean Christopherson 	if (test->guest_test)
286*67730e6cSSean Christopherson 		test->guest_test();
287*67730e6cSSean Christopherson 
288*67730e6cSSean Christopherson 	guest_test_check(test);
289*67730e6cSSean Christopherson 	GUEST_DONE();
290*67730e6cSSean Christopherson }
291*67730e6cSSean Christopherson 
292*67730e6cSSean Christopherson static void no_dabt_handler(struct ex_regs *regs)
293*67730e6cSSean Christopherson {
294*67730e6cSSean Christopherson 	GUEST_FAIL("Unexpected dabt, far_el1 = 0x%lx", read_sysreg(far_el1));
295*67730e6cSSean Christopherson }
296*67730e6cSSean Christopherson 
297*67730e6cSSean Christopherson static void no_iabt_handler(struct ex_regs *regs)
298*67730e6cSSean Christopherson {
299*67730e6cSSean Christopherson 	GUEST_FAIL("Unexpected iabt, pc = 0x%lx", regs->pc);
300*67730e6cSSean Christopherson }
301*67730e6cSSean Christopherson 
302*67730e6cSSean Christopherson static struct uffd_args {
303*67730e6cSSean Christopherson 	char *copy;
304*67730e6cSSean Christopherson 	void *hva;
305*67730e6cSSean Christopherson 	uint64_t paging_size;
306*67730e6cSSean Christopherson } pt_args, data_args;
307*67730e6cSSean Christopherson 
308*67730e6cSSean Christopherson /* Returns true to continue the test, and false if it should be skipped. */
309*67730e6cSSean Christopherson static int uffd_generic_handler(int uffd_mode, int uffd, struct uffd_msg *msg,
310*67730e6cSSean Christopherson 				struct uffd_args *args)
311*67730e6cSSean Christopherson {
312*67730e6cSSean Christopherson 	uint64_t addr = msg->arg.pagefault.address;
313*67730e6cSSean Christopherson 	uint64_t flags = msg->arg.pagefault.flags;
314*67730e6cSSean Christopherson 	struct uffdio_copy copy;
315*67730e6cSSean Christopherson 	int ret;
316*67730e6cSSean Christopherson 
317*67730e6cSSean Christopherson 	TEST_ASSERT(uffd_mode == UFFDIO_REGISTER_MODE_MISSING,
318*67730e6cSSean Christopherson 		    "The only expected UFFD mode is MISSING");
319*67730e6cSSean Christopherson 	TEST_ASSERT_EQ(addr, (uint64_t)args->hva);
320*67730e6cSSean Christopherson 
321*67730e6cSSean Christopherson 	pr_debug("uffd fault: addr=%p write=%d\n",
322*67730e6cSSean Christopherson 		 (void *)addr, !!(flags & UFFD_PAGEFAULT_FLAG_WRITE));
323*67730e6cSSean Christopherson 
324*67730e6cSSean Christopherson 	copy.src = (uint64_t)args->copy;
325*67730e6cSSean Christopherson 	copy.dst = addr;
326*67730e6cSSean Christopherson 	copy.len = args->paging_size;
327*67730e6cSSean Christopherson 	copy.mode = 0;
328*67730e6cSSean Christopherson 
329*67730e6cSSean Christopherson 	ret = ioctl(uffd, UFFDIO_COPY, &copy);
330*67730e6cSSean Christopherson 	if (ret == -1) {
331*67730e6cSSean Christopherson 		pr_info("Failed UFFDIO_COPY in 0x%lx with errno: %d\n",
332*67730e6cSSean Christopherson 			addr, errno);
333*67730e6cSSean Christopherson 		return ret;
334*67730e6cSSean Christopherson 	}
335*67730e6cSSean Christopherson 
336*67730e6cSSean Christopherson 	pthread_mutex_lock(&events.uffd_faults_mutex);
337*67730e6cSSean Christopherson 	events.uffd_faults += 1;
338*67730e6cSSean Christopherson 	pthread_mutex_unlock(&events.uffd_faults_mutex);
339*67730e6cSSean Christopherson 	return 0;
340*67730e6cSSean Christopherson }
341*67730e6cSSean Christopherson 
342*67730e6cSSean Christopherson static int uffd_pt_handler(int mode, int uffd, struct uffd_msg *msg)
343*67730e6cSSean Christopherson {
344*67730e6cSSean Christopherson 	return uffd_generic_handler(mode, uffd, msg, &pt_args);
345*67730e6cSSean Christopherson }
346*67730e6cSSean Christopherson 
347*67730e6cSSean Christopherson static int uffd_data_handler(int mode, int uffd, struct uffd_msg *msg)
348*67730e6cSSean Christopherson {
349*67730e6cSSean Christopherson 	return uffd_generic_handler(mode, uffd, msg, &data_args);
350*67730e6cSSean Christopherson }
351*67730e6cSSean Christopherson 
352*67730e6cSSean Christopherson static void setup_uffd_args(struct userspace_mem_region *region,
353*67730e6cSSean Christopherson 			    struct uffd_args *args)
354*67730e6cSSean Christopherson {
355*67730e6cSSean Christopherson 	args->hva = (void *)region->region.userspace_addr;
356*67730e6cSSean Christopherson 	args->paging_size = region->region.memory_size;
357*67730e6cSSean Christopherson 
358*67730e6cSSean Christopherson 	args->copy = malloc(args->paging_size);
359*67730e6cSSean Christopherson 	TEST_ASSERT(args->copy, "Failed to allocate data copy.");
360*67730e6cSSean Christopherson 	memcpy(args->copy, args->hva, args->paging_size);
361*67730e6cSSean Christopherson }
362*67730e6cSSean Christopherson 
363*67730e6cSSean Christopherson static void setup_uffd(struct kvm_vm *vm, struct test_params *p,
364*67730e6cSSean Christopherson 		       struct uffd_desc **pt_uffd, struct uffd_desc **data_uffd)
365*67730e6cSSean Christopherson {
366*67730e6cSSean Christopherson 	struct test_desc *test = p->test_desc;
367*67730e6cSSean Christopherson 	int uffd_mode = UFFDIO_REGISTER_MODE_MISSING;
368*67730e6cSSean Christopherson 
369*67730e6cSSean Christopherson 	setup_uffd_args(vm_get_mem_region(vm, MEM_REGION_PT), &pt_args);
370*67730e6cSSean Christopherson 	setup_uffd_args(vm_get_mem_region(vm, MEM_REGION_TEST_DATA), &data_args);
371*67730e6cSSean Christopherson 
372*67730e6cSSean Christopherson 	*pt_uffd = NULL;
373*67730e6cSSean Christopherson 	if (test->uffd_pt_handler)
374*67730e6cSSean Christopherson 		*pt_uffd = uffd_setup_demand_paging(uffd_mode, 0,
375*67730e6cSSean Christopherson 						    pt_args.hva,
376*67730e6cSSean Christopherson 						    pt_args.paging_size,
377*67730e6cSSean Christopherson 						    1, test->uffd_pt_handler);
378*67730e6cSSean Christopherson 
379*67730e6cSSean Christopherson 	*data_uffd = NULL;
380*67730e6cSSean Christopherson 	if (test->uffd_data_handler)
381*67730e6cSSean Christopherson 		*data_uffd = uffd_setup_demand_paging(uffd_mode, 0,
382*67730e6cSSean Christopherson 						      data_args.hva,
383*67730e6cSSean Christopherson 						      data_args.paging_size,
384*67730e6cSSean Christopherson 						      1, test->uffd_data_handler);
385*67730e6cSSean Christopherson }
386*67730e6cSSean Christopherson 
387*67730e6cSSean Christopherson static void free_uffd(struct test_desc *test, struct uffd_desc *pt_uffd,
388*67730e6cSSean Christopherson 		      struct uffd_desc *data_uffd)
389*67730e6cSSean Christopherson {
390*67730e6cSSean Christopherson 	if (test->uffd_pt_handler)
391*67730e6cSSean Christopherson 		uffd_stop_demand_paging(pt_uffd);
392*67730e6cSSean Christopherson 	if (test->uffd_data_handler)
393*67730e6cSSean Christopherson 		uffd_stop_demand_paging(data_uffd);
394*67730e6cSSean Christopherson 
395*67730e6cSSean Christopherson 	free(pt_args.copy);
396*67730e6cSSean Christopherson 	free(data_args.copy);
397*67730e6cSSean Christopherson }
398*67730e6cSSean Christopherson 
399*67730e6cSSean Christopherson static int uffd_no_handler(int mode, int uffd, struct uffd_msg *msg)
400*67730e6cSSean Christopherson {
401*67730e6cSSean Christopherson 	TEST_FAIL("There was no UFFD fault expected.");
402*67730e6cSSean Christopherson 	return -1;
403*67730e6cSSean Christopherson }
404*67730e6cSSean Christopherson 
405*67730e6cSSean Christopherson /* Returns false if the test should be skipped. */
406*67730e6cSSean Christopherson static bool punch_hole_in_backing_store(struct kvm_vm *vm,
407*67730e6cSSean Christopherson 					struct userspace_mem_region *region)
408*67730e6cSSean Christopherson {
409*67730e6cSSean Christopherson 	void *hva = (void *)region->region.userspace_addr;
410*67730e6cSSean Christopherson 	uint64_t paging_size = region->region.memory_size;
411*67730e6cSSean Christopherson 	int ret, fd = region->fd;
412*67730e6cSSean Christopherson 
413*67730e6cSSean Christopherson 	if (fd != -1) {
414*67730e6cSSean Christopherson 		ret = fallocate(fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
415*67730e6cSSean Christopherson 				0, paging_size);
416*67730e6cSSean Christopherson 		TEST_ASSERT(ret == 0, "fallocate failed");
417*67730e6cSSean Christopherson 	} else {
418*67730e6cSSean Christopherson 		ret = madvise(hva, paging_size, MADV_DONTNEED);
419*67730e6cSSean Christopherson 		TEST_ASSERT(ret == 0, "madvise failed");
420*67730e6cSSean Christopherson 	}
421*67730e6cSSean Christopherson 
422*67730e6cSSean Christopherson 	return true;
423*67730e6cSSean Christopherson }
424*67730e6cSSean Christopherson 
425*67730e6cSSean Christopherson static void mmio_on_test_gpa_handler(struct kvm_vm *vm, struct kvm_run *run)
426*67730e6cSSean Christopherson {
427*67730e6cSSean Christopherson 	struct userspace_mem_region *region;
428*67730e6cSSean Christopherson 	void *hva;
429*67730e6cSSean Christopherson 
430*67730e6cSSean Christopherson 	region = vm_get_mem_region(vm, MEM_REGION_TEST_DATA);
431*67730e6cSSean Christopherson 	hva = (void *)region->region.userspace_addr;
432*67730e6cSSean Christopherson 
433*67730e6cSSean Christopherson 	TEST_ASSERT_EQ(run->mmio.phys_addr, region->region.guest_phys_addr);
434*67730e6cSSean Christopherson 
435*67730e6cSSean Christopherson 	memcpy(hva, run->mmio.data, run->mmio.len);
436*67730e6cSSean Christopherson 	events.mmio_exits += 1;
437*67730e6cSSean Christopherson }
438*67730e6cSSean Christopherson 
439*67730e6cSSean Christopherson static void mmio_no_handler(struct kvm_vm *vm, struct kvm_run *run)
440*67730e6cSSean Christopherson {
441*67730e6cSSean Christopherson 	uint64_t data;
442*67730e6cSSean Christopherson 
443*67730e6cSSean Christopherson 	memcpy(&data, run->mmio.data, sizeof(data));
444*67730e6cSSean Christopherson 	pr_debug("addr=%lld len=%d w=%d data=%lx\n",
445*67730e6cSSean Christopherson 		 run->mmio.phys_addr, run->mmio.len,
446*67730e6cSSean Christopherson 		 run->mmio.is_write, data);
447*67730e6cSSean Christopherson 	TEST_FAIL("There was no MMIO exit expected.");
448*67730e6cSSean Christopherson }
449*67730e6cSSean Christopherson 
450*67730e6cSSean Christopherson static bool check_write_in_dirty_log(struct kvm_vm *vm,
451*67730e6cSSean Christopherson 				     struct userspace_mem_region *region,
452*67730e6cSSean Christopherson 				     uint64_t host_pg_nr)
453*67730e6cSSean Christopherson {
454*67730e6cSSean Christopherson 	unsigned long *bmap;
455*67730e6cSSean Christopherson 	bool first_page_dirty;
456*67730e6cSSean Christopherson 	uint64_t size = region->region.memory_size;
457*67730e6cSSean Christopherson 
458*67730e6cSSean Christopherson 	/* getpage_size() is not always equal to vm->page_size */
459*67730e6cSSean Christopherson 	bmap = bitmap_zalloc(size / getpagesize());
460*67730e6cSSean Christopherson 	kvm_vm_get_dirty_log(vm, region->region.slot, bmap);
461*67730e6cSSean Christopherson 	first_page_dirty = test_bit(host_pg_nr, bmap);
462*67730e6cSSean Christopherson 	free(bmap);
463*67730e6cSSean Christopherson 	return first_page_dirty;
464*67730e6cSSean Christopherson }
465*67730e6cSSean Christopherson 
466*67730e6cSSean Christopherson /* Returns true to continue the test, and false if it should be skipped. */
467*67730e6cSSean Christopherson static bool handle_cmd(struct kvm_vm *vm, int cmd)
468*67730e6cSSean Christopherson {
469*67730e6cSSean Christopherson 	struct userspace_mem_region *data_region, *pt_region;
470*67730e6cSSean Christopherson 	bool continue_test = true;
471*67730e6cSSean Christopherson 	uint64_t pte_gpa, pte_pg;
472*67730e6cSSean Christopherson 
473*67730e6cSSean Christopherson 	data_region = vm_get_mem_region(vm, MEM_REGION_TEST_DATA);
474*67730e6cSSean Christopherson 	pt_region = vm_get_mem_region(vm, MEM_REGION_PT);
475*67730e6cSSean Christopherson 	pte_gpa = addr_hva2gpa(vm, virt_get_pte_hva(vm, TEST_GVA));
476*67730e6cSSean Christopherson 	pte_pg = (pte_gpa - pt_region->region.guest_phys_addr) / getpagesize();
477*67730e6cSSean Christopherson 
478*67730e6cSSean Christopherson 	if (cmd == CMD_SKIP_TEST)
479*67730e6cSSean Christopherson 		continue_test = false;
480*67730e6cSSean Christopherson 
481*67730e6cSSean Christopherson 	if (cmd & CMD_HOLE_PT)
482*67730e6cSSean Christopherson 		continue_test = punch_hole_in_backing_store(vm, pt_region);
483*67730e6cSSean Christopherson 	if (cmd & CMD_HOLE_DATA)
484*67730e6cSSean Christopherson 		continue_test = punch_hole_in_backing_store(vm, data_region);
485*67730e6cSSean Christopherson 	if (cmd & CMD_CHECK_WRITE_IN_DIRTY_LOG)
486*67730e6cSSean Christopherson 		TEST_ASSERT(check_write_in_dirty_log(vm, data_region, 0),
487*67730e6cSSean Christopherson 			    "Missing write in dirty log");
488*67730e6cSSean Christopherson 	if (cmd & CMD_CHECK_S1PTW_WR_IN_DIRTY_LOG)
489*67730e6cSSean Christopherson 		TEST_ASSERT(check_write_in_dirty_log(vm, pt_region, pte_pg),
490*67730e6cSSean Christopherson 			    "Missing s1ptw write in dirty log");
491*67730e6cSSean Christopherson 	if (cmd & CMD_CHECK_NO_WRITE_IN_DIRTY_LOG)
492*67730e6cSSean Christopherson 		TEST_ASSERT(!check_write_in_dirty_log(vm, data_region, 0),
493*67730e6cSSean Christopherson 			    "Unexpected write in dirty log");
494*67730e6cSSean Christopherson 	if (cmd & CMD_CHECK_NO_S1PTW_WR_IN_DIRTY_LOG)
495*67730e6cSSean Christopherson 		TEST_ASSERT(!check_write_in_dirty_log(vm, pt_region, pte_pg),
496*67730e6cSSean Christopherson 			    "Unexpected s1ptw write in dirty log");
497*67730e6cSSean Christopherson 
498*67730e6cSSean Christopherson 	return continue_test;
499*67730e6cSSean Christopherson }
500*67730e6cSSean Christopherson 
501*67730e6cSSean Christopherson void fail_vcpu_run_no_handler(int ret)
502*67730e6cSSean Christopherson {
503*67730e6cSSean Christopherson 	TEST_FAIL("Unexpected vcpu run failure");
504*67730e6cSSean Christopherson }
505*67730e6cSSean Christopherson 
506*67730e6cSSean Christopherson void fail_vcpu_run_mmio_no_syndrome_handler(int ret)
507*67730e6cSSean Christopherson {
508*67730e6cSSean Christopherson 	TEST_ASSERT(errno == ENOSYS,
509*67730e6cSSean Christopherson 		    "The mmio handler should have returned not implemented.");
510*67730e6cSSean Christopherson 	events.fail_vcpu_runs += 1;
511*67730e6cSSean Christopherson }
512*67730e6cSSean Christopherson 
513*67730e6cSSean Christopherson typedef uint32_t aarch64_insn_t;
514*67730e6cSSean Christopherson extern aarch64_insn_t __exec_test[2];
515*67730e6cSSean Christopherson 
516*67730e6cSSean Christopherson noinline void __return_0x77(void)
517*67730e6cSSean Christopherson {
518*67730e6cSSean Christopherson 	asm volatile("__exec_test: mov x0, #0x77\n"
519*67730e6cSSean Christopherson 		     "ret\n");
520*67730e6cSSean Christopherson }
521*67730e6cSSean Christopherson 
522*67730e6cSSean Christopherson /*
523*67730e6cSSean Christopherson  * Note that this function runs on the host before the test VM starts: there's
524*67730e6cSSean Christopherson  * no need to sync the D$ and I$ caches.
525*67730e6cSSean Christopherson  */
526*67730e6cSSean Christopherson static void load_exec_code_for_test(struct kvm_vm *vm)
527*67730e6cSSean Christopherson {
528*67730e6cSSean Christopherson 	uint64_t *code;
529*67730e6cSSean Christopherson 	struct userspace_mem_region *region;
530*67730e6cSSean Christopherson 	void *hva;
531*67730e6cSSean Christopherson 
532*67730e6cSSean Christopherson 	region = vm_get_mem_region(vm, MEM_REGION_TEST_DATA);
533*67730e6cSSean Christopherson 	hva = (void *)region->region.userspace_addr;
534*67730e6cSSean Christopherson 
535*67730e6cSSean Christopherson 	assert(TEST_EXEC_GVA > TEST_GVA);
536*67730e6cSSean Christopherson 	code = hva + TEST_EXEC_GVA - TEST_GVA;
537*67730e6cSSean Christopherson 	memcpy(code, __exec_test, sizeof(__exec_test));
538*67730e6cSSean Christopherson }
539*67730e6cSSean Christopherson 
540*67730e6cSSean Christopherson static void setup_abort_handlers(struct kvm_vm *vm, struct kvm_vcpu *vcpu,
541*67730e6cSSean Christopherson 				 struct test_desc *test)
542*67730e6cSSean Christopherson {
543*67730e6cSSean Christopherson 	vm_init_descriptor_tables(vm);
544*67730e6cSSean Christopherson 	vcpu_init_descriptor_tables(vcpu);
545*67730e6cSSean Christopherson 
546*67730e6cSSean Christopherson 	vm_install_sync_handler(vm, VECTOR_SYNC_CURRENT,
547*67730e6cSSean Christopherson 				ESR_ELx_EC_DABT_CUR, no_dabt_handler);
548*67730e6cSSean Christopherson 	vm_install_sync_handler(vm, VECTOR_SYNC_CURRENT,
549*67730e6cSSean Christopherson 				ESR_ELx_EC_IABT_CUR, no_iabt_handler);
550*67730e6cSSean Christopherson }
551*67730e6cSSean Christopherson 
552*67730e6cSSean Christopherson static void setup_gva_maps(struct kvm_vm *vm)
553*67730e6cSSean Christopherson {
554*67730e6cSSean Christopherson 	struct userspace_mem_region *region;
555*67730e6cSSean Christopherson 	uint64_t pte_gpa;
556*67730e6cSSean Christopherson 
557*67730e6cSSean Christopherson 	region = vm_get_mem_region(vm, MEM_REGION_TEST_DATA);
558*67730e6cSSean Christopherson 	/* Map TEST_GVA first. This will install a new PTE. */
559*67730e6cSSean Christopherson 	virt_pg_map(vm, TEST_GVA, region->region.guest_phys_addr);
560*67730e6cSSean Christopherson 	/* Then map TEST_PTE_GVA to the above PTE. */
561*67730e6cSSean Christopherson 	pte_gpa = addr_hva2gpa(vm, virt_get_pte_hva(vm, TEST_GVA));
562*67730e6cSSean Christopherson 	virt_pg_map(vm, TEST_PTE_GVA, pte_gpa);
563*67730e6cSSean Christopherson }
564*67730e6cSSean Christopherson 
565*67730e6cSSean Christopherson enum pf_test_memslots {
566*67730e6cSSean Christopherson 	CODE_AND_DATA_MEMSLOT,
567*67730e6cSSean Christopherson 	PAGE_TABLE_MEMSLOT,
568*67730e6cSSean Christopherson 	TEST_DATA_MEMSLOT,
569*67730e6cSSean Christopherson };
570*67730e6cSSean Christopherson 
571*67730e6cSSean Christopherson /*
572*67730e6cSSean Christopherson  * Create a memslot for code and data at pfn=0, and test-data and PT ones
573*67730e6cSSean Christopherson  * at max_gfn.
574*67730e6cSSean Christopherson  */
575*67730e6cSSean Christopherson static void setup_memslots(struct kvm_vm *vm, struct test_params *p)
576*67730e6cSSean Christopherson {
577*67730e6cSSean Christopherson 	uint64_t backing_src_pagesz = get_backing_src_pagesz(p->src_type);
578*67730e6cSSean Christopherson 	uint64_t guest_page_size = vm->page_size;
579*67730e6cSSean Christopherson 	uint64_t max_gfn = vm_compute_max_gfn(vm);
580*67730e6cSSean Christopherson 	/* Enough for 2M of code when using 4K guest pages. */
581*67730e6cSSean Christopherson 	uint64_t code_npages = 512;
582*67730e6cSSean Christopherson 	uint64_t pt_size, data_size, data_gpa;
583*67730e6cSSean Christopherson 
584*67730e6cSSean Christopherson 	/*
585*67730e6cSSean Christopherson 	 * This test requires 1 pgd, 2 pud, 4 pmd, and 6 pte pages when using
586*67730e6cSSean Christopherson 	 * VM_MODE_P48V48_4K. Note that the .text takes ~1.6MBs.  That's 13
587*67730e6cSSean Christopherson 	 * pages. VM_MODE_P48V48_4K is the mode with most PT pages; let's use
588*67730e6cSSean Christopherson 	 * twice that just in case.
589*67730e6cSSean Christopherson 	 */
590*67730e6cSSean Christopherson 	pt_size = 26 * guest_page_size;
591*67730e6cSSean Christopherson 
592*67730e6cSSean Christopherson 	/* memslot sizes and gpa's must be aligned to the backing page size */
593*67730e6cSSean Christopherson 	pt_size = align_up(pt_size, backing_src_pagesz);
594*67730e6cSSean Christopherson 	data_size = align_up(guest_page_size, backing_src_pagesz);
595*67730e6cSSean Christopherson 	data_gpa = (max_gfn * guest_page_size) - data_size;
596*67730e6cSSean Christopherson 	data_gpa = align_down(data_gpa, backing_src_pagesz);
597*67730e6cSSean Christopherson 
598*67730e6cSSean Christopherson 	vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS, 0,
599*67730e6cSSean Christopherson 				    CODE_AND_DATA_MEMSLOT, code_npages, 0);
600*67730e6cSSean Christopherson 	vm->memslots[MEM_REGION_CODE] = CODE_AND_DATA_MEMSLOT;
601*67730e6cSSean Christopherson 	vm->memslots[MEM_REGION_DATA] = CODE_AND_DATA_MEMSLOT;
602*67730e6cSSean Christopherson 
603*67730e6cSSean Christopherson 	vm_userspace_mem_region_add(vm, p->src_type, data_gpa - pt_size,
604*67730e6cSSean Christopherson 				    PAGE_TABLE_MEMSLOT, pt_size / guest_page_size,
605*67730e6cSSean Christopherson 				    p->test_desc->pt_memslot_flags);
606*67730e6cSSean Christopherson 	vm->memslots[MEM_REGION_PT] = PAGE_TABLE_MEMSLOT;
607*67730e6cSSean Christopherson 
608*67730e6cSSean Christopherson 	vm_userspace_mem_region_add(vm, p->src_type, data_gpa, TEST_DATA_MEMSLOT,
609*67730e6cSSean Christopherson 				    data_size / guest_page_size,
610*67730e6cSSean Christopherson 				    p->test_desc->data_memslot_flags);
611*67730e6cSSean Christopherson 	vm->memslots[MEM_REGION_TEST_DATA] = TEST_DATA_MEMSLOT;
612*67730e6cSSean Christopherson }
613*67730e6cSSean Christopherson 
614*67730e6cSSean Christopherson static void setup_ucall(struct kvm_vm *vm)
615*67730e6cSSean Christopherson {
616*67730e6cSSean Christopherson 	struct userspace_mem_region *region = vm_get_mem_region(vm, MEM_REGION_TEST_DATA);
617*67730e6cSSean Christopherson 
618*67730e6cSSean Christopherson 	ucall_init(vm, region->region.guest_phys_addr + region->region.memory_size);
619*67730e6cSSean Christopherson }
620*67730e6cSSean Christopherson 
621*67730e6cSSean Christopherson static void setup_default_handlers(struct test_desc *test)
622*67730e6cSSean Christopherson {
623*67730e6cSSean Christopherson 	if (!test->mmio_handler)
624*67730e6cSSean Christopherson 		test->mmio_handler = mmio_no_handler;
625*67730e6cSSean Christopherson 
626*67730e6cSSean Christopherson 	if (!test->fail_vcpu_run_handler)
627*67730e6cSSean Christopherson 		test->fail_vcpu_run_handler = fail_vcpu_run_no_handler;
628*67730e6cSSean Christopherson }
629*67730e6cSSean Christopherson 
630*67730e6cSSean Christopherson static void check_event_counts(struct test_desc *test)
631*67730e6cSSean Christopherson {
632*67730e6cSSean Christopherson 	TEST_ASSERT_EQ(test->expected_events.uffd_faults, events.uffd_faults);
633*67730e6cSSean Christopherson 	TEST_ASSERT_EQ(test->expected_events.mmio_exits, events.mmio_exits);
634*67730e6cSSean Christopherson 	TEST_ASSERT_EQ(test->expected_events.fail_vcpu_runs, events.fail_vcpu_runs);
635*67730e6cSSean Christopherson }
636*67730e6cSSean Christopherson 
637*67730e6cSSean Christopherson static void print_test_banner(enum vm_guest_mode mode, struct test_params *p)
638*67730e6cSSean Christopherson {
639*67730e6cSSean Christopherson 	struct test_desc *test = p->test_desc;
640*67730e6cSSean Christopherson 
641*67730e6cSSean Christopherson 	pr_debug("Test: %s\n", test->name);
642*67730e6cSSean Christopherson 	pr_debug("Testing guest mode: %s\n", vm_guest_mode_string(mode));
643*67730e6cSSean Christopherson 	pr_debug("Testing memory backing src type: %s\n",
644*67730e6cSSean Christopherson 		 vm_mem_backing_src_alias(p->src_type)->name);
645*67730e6cSSean Christopherson }
646*67730e6cSSean Christopherson 
647*67730e6cSSean Christopherson static void reset_event_counts(void)
648*67730e6cSSean Christopherson {
649*67730e6cSSean Christopherson 	memset(&events, 0, sizeof(events));
650*67730e6cSSean Christopherson }
651*67730e6cSSean Christopherson 
652*67730e6cSSean Christopherson /*
653*67730e6cSSean Christopherson  * This function either succeeds, skips the test (after setting test->skip), or
654*67730e6cSSean Christopherson  * fails with a TEST_FAIL that aborts all tests.
655*67730e6cSSean Christopherson  */
656*67730e6cSSean Christopherson static void vcpu_run_loop(struct kvm_vm *vm, struct kvm_vcpu *vcpu,
657*67730e6cSSean Christopherson 			  struct test_desc *test)
658*67730e6cSSean Christopherson {
659*67730e6cSSean Christopherson 	struct kvm_run *run;
660*67730e6cSSean Christopherson 	struct ucall uc;
661*67730e6cSSean Christopherson 	int ret;
662*67730e6cSSean Christopherson 
663*67730e6cSSean Christopherson 	run = vcpu->run;
664*67730e6cSSean Christopherson 
665*67730e6cSSean Christopherson 	for (;;) {
666*67730e6cSSean Christopherson 		ret = _vcpu_run(vcpu);
667*67730e6cSSean Christopherson 		if (ret) {
668*67730e6cSSean Christopherson 			test->fail_vcpu_run_handler(ret);
669*67730e6cSSean Christopherson 			goto done;
670*67730e6cSSean Christopherson 		}
671*67730e6cSSean Christopherson 
672*67730e6cSSean Christopherson 		switch (get_ucall(vcpu, &uc)) {
673*67730e6cSSean Christopherson 		case UCALL_SYNC:
674*67730e6cSSean Christopherson 			if (!handle_cmd(vm, uc.args[1])) {
675*67730e6cSSean Christopherson 				test->skip = true;
676*67730e6cSSean Christopherson 				goto done;
677*67730e6cSSean Christopherson 			}
678*67730e6cSSean Christopherson 			break;
679*67730e6cSSean Christopherson 		case UCALL_ABORT:
680*67730e6cSSean Christopherson 			REPORT_GUEST_ASSERT(uc);
681*67730e6cSSean Christopherson 			break;
682*67730e6cSSean Christopherson 		case UCALL_DONE:
683*67730e6cSSean Christopherson 			goto done;
684*67730e6cSSean Christopherson 		case UCALL_NONE:
685*67730e6cSSean Christopherson 			if (run->exit_reason == KVM_EXIT_MMIO)
686*67730e6cSSean Christopherson 				test->mmio_handler(vm, run);
687*67730e6cSSean Christopherson 			break;
688*67730e6cSSean Christopherson 		default:
689*67730e6cSSean Christopherson 			TEST_FAIL("Unknown ucall %lu", uc.cmd);
690*67730e6cSSean Christopherson 		}
691*67730e6cSSean Christopherson 	}
692*67730e6cSSean Christopherson 
693*67730e6cSSean Christopherson done:
694*67730e6cSSean Christopherson 	pr_debug(test->skip ? "Skipped.\n" : "Done.\n");
695*67730e6cSSean Christopherson }
696*67730e6cSSean Christopherson 
697*67730e6cSSean Christopherson static void run_test(enum vm_guest_mode mode, void *arg)
698*67730e6cSSean Christopherson {
699*67730e6cSSean Christopherson 	struct test_params *p = (struct test_params *)arg;
700*67730e6cSSean Christopherson 	struct test_desc *test = p->test_desc;
701*67730e6cSSean Christopherson 	struct kvm_vm *vm;
702*67730e6cSSean Christopherson 	struct kvm_vcpu *vcpu;
703*67730e6cSSean Christopherson 	struct uffd_desc *pt_uffd, *data_uffd;
704*67730e6cSSean Christopherson 
705*67730e6cSSean Christopherson 	print_test_banner(mode, p);
706*67730e6cSSean Christopherson 
707*67730e6cSSean Christopherson 	vm = ____vm_create(VM_SHAPE(mode));
708*67730e6cSSean Christopherson 	setup_memslots(vm, p);
709*67730e6cSSean Christopherson 	kvm_vm_elf_load(vm, program_invocation_name);
710*67730e6cSSean Christopherson 	setup_ucall(vm);
711*67730e6cSSean Christopherson 	vcpu = vm_vcpu_add(vm, 0, guest_code);
712*67730e6cSSean Christopherson 
713*67730e6cSSean Christopherson 	setup_gva_maps(vm);
714*67730e6cSSean Christopherson 
715*67730e6cSSean Christopherson 	reset_event_counts();
716*67730e6cSSean Christopherson 
717*67730e6cSSean Christopherson 	/*
718*67730e6cSSean Christopherson 	 * Set some code in the data memslot for the guest to execute (only
719*67730e6cSSean Christopherson 	 * applicable to the EXEC tests). This has to be done before
720*67730e6cSSean Christopherson 	 * setup_uffd() as that function copies the memslot data for the uffd
721*67730e6cSSean Christopherson 	 * handler.
722*67730e6cSSean Christopherson 	 */
723*67730e6cSSean Christopherson 	load_exec_code_for_test(vm);
724*67730e6cSSean Christopherson 	setup_uffd(vm, p, &pt_uffd, &data_uffd);
725*67730e6cSSean Christopherson 	setup_abort_handlers(vm, vcpu, test);
726*67730e6cSSean Christopherson 	setup_default_handlers(test);
727*67730e6cSSean Christopherson 	vcpu_args_set(vcpu, 1, test);
728*67730e6cSSean Christopherson 
729*67730e6cSSean Christopherson 	vcpu_run_loop(vm, vcpu, test);
730*67730e6cSSean Christopherson 
731*67730e6cSSean Christopherson 	kvm_vm_free(vm);
732*67730e6cSSean Christopherson 	free_uffd(test, pt_uffd, data_uffd);
733*67730e6cSSean Christopherson 
734*67730e6cSSean Christopherson 	/*
735*67730e6cSSean Christopherson 	 * Make sure we check the events after the uffd threads have exited,
736*67730e6cSSean Christopherson 	 * which means they updated their respective event counters.
737*67730e6cSSean Christopherson 	 */
738*67730e6cSSean Christopherson 	if (!test->skip)
739*67730e6cSSean Christopherson 		check_event_counts(test);
740*67730e6cSSean Christopherson }
741*67730e6cSSean Christopherson 
742*67730e6cSSean Christopherson static void help(char *name)
743*67730e6cSSean Christopherson {
744*67730e6cSSean Christopherson 	puts("");
745*67730e6cSSean Christopherson 	printf("usage: %s [-h] [-s mem-type]\n", name);
746*67730e6cSSean Christopherson 	puts("");
747*67730e6cSSean Christopherson 	guest_modes_help();
748*67730e6cSSean Christopherson 	backing_src_help("-s");
749*67730e6cSSean Christopherson 	puts("");
750*67730e6cSSean Christopherson }
751*67730e6cSSean Christopherson 
752*67730e6cSSean Christopherson #define SNAME(s)			#s
753*67730e6cSSean Christopherson #define SCAT2(a, b)			SNAME(a ## _ ## b)
754*67730e6cSSean Christopherson #define SCAT3(a, b, c)			SCAT2(a, SCAT2(b, c))
755*67730e6cSSean Christopherson #define SCAT4(a, b, c, d)		SCAT2(a, SCAT3(b, c, d))
756*67730e6cSSean Christopherson 
757*67730e6cSSean Christopherson #define _CHECK(_test)			_CHECK_##_test
758*67730e6cSSean Christopherson #define _PREPARE(_test)			_PREPARE_##_test
759*67730e6cSSean Christopherson #define _PREPARE_guest_read64		NULL
760*67730e6cSSean Christopherson #define _PREPARE_guest_ld_preidx	NULL
761*67730e6cSSean Christopherson #define _PREPARE_guest_write64		NULL
762*67730e6cSSean Christopherson #define _PREPARE_guest_st_preidx	NULL
763*67730e6cSSean Christopherson #define _PREPARE_guest_exec		NULL
764*67730e6cSSean Christopherson #define _PREPARE_guest_at		NULL
765*67730e6cSSean Christopherson #define _PREPARE_guest_dc_zva		guest_check_dc_zva
766*67730e6cSSean Christopherson #define _PREPARE_guest_cas		guest_check_lse
767*67730e6cSSean Christopherson 
768*67730e6cSSean Christopherson /* With or without access flag checks */
769*67730e6cSSean Christopherson #define _PREPARE_with_af		guest_set_ha, guest_clear_pte_af
770*67730e6cSSean Christopherson #define _PREPARE_no_af			NULL
771*67730e6cSSean Christopherson #define _CHECK_with_af			guest_check_pte_af
772*67730e6cSSean Christopherson #define _CHECK_no_af			NULL
773*67730e6cSSean Christopherson 
774*67730e6cSSean Christopherson /* Performs an access and checks that no faults were triggered. */
775*67730e6cSSean Christopherson #define TEST_ACCESS(_access, _with_af, _mark_cmd)				\
776*67730e6cSSean Christopherson {										\
777*67730e6cSSean Christopherson 	.name			= SCAT3(_access, _with_af, #_mark_cmd),		\
778*67730e6cSSean Christopherson 	.guest_prepare		= { _PREPARE(_with_af),				\
779*67730e6cSSean Christopherson 				    _PREPARE(_access) },			\
780*67730e6cSSean Christopherson 	.mem_mark_cmd		= _mark_cmd,					\
781*67730e6cSSean Christopherson 	.guest_test		= _access,					\
782*67730e6cSSean Christopherson 	.guest_test_check	= { _CHECK(_with_af) },				\
783*67730e6cSSean Christopherson 	.expected_events	= { 0 },					\
784*67730e6cSSean Christopherson }
785*67730e6cSSean Christopherson 
786*67730e6cSSean Christopherson #define TEST_UFFD(_access, _with_af, _mark_cmd,					\
787*67730e6cSSean Christopherson 		  _uffd_data_handler, _uffd_pt_handler, _uffd_faults)		\
788*67730e6cSSean Christopherson {										\
789*67730e6cSSean Christopherson 	.name			= SCAT4(uffd, _access, _with_af, #_mark_cmd),	\
790*67730e6cSSean Christopherson 	.guest_prepare		= { _PREPARE(_with_af),				\
791*67730e6cSSean Christopherson 				    _PREPARE(_access) },			\
792*67730e6cSSean Christopherson 	.guest_test		= _access,					\
793*67730e6cSSean Christopherson 	.mem_mark_cmd		= _mark_cmd,					\
794*67730e6cSSean Christopherson 	.guest_test_check	= { _CHECK(_with_af) },				\
795*67730e6cSSean Christopherson 	.uffd_data_handler	= _uffd_data_handler,				\
796*67730e6cSSean Christopherson 	.uffd_pt_handler	= _uffd_pt_handler,				\
797*67730e6cSSean Christopherson 	.expected_events	= { .uffd_faults = _uffd_faults, },		\
798*67730e6cSSean Christopherson }
799*67730e6cSSean Christopherson 
800*67730e6cSSean Christopherson #define TEST_DIRTY_LOG(_access, _with_af, _test_check, _pt_check)		\
801*67730e6cSSean Christopherson {										\
802*67730e6cSSean Christopherson 	.name			= SCAT3(dirty_log, _access, _with_af),		\
803*67730e6cSSean Christopherson 	.data_memslot_flags	= KVM_MEM_LOG_DIRTY_PAGES,			\
804*67730e6cSSean Christopherson 	.pt_memslot_flags	= KVM_MEM_LOG_DIRTY_PAGES,			\
805*67730e6cSSean Christopherson 	.guest_prepare		= { _PREPARE(_with_af),				\
806*67730e6cSSean Christopherson 				    _PREPARE(_access) },			\
807*67730e6cSSean Christopherson 	.guest_test		= _access,					\
808*67730e6cSSean Christopherson 	.guest_test_check	= { _CHECK(_with_af), _test_check, _pt_check },	\
809*67730e6cSSean Christopherson 	.expected_events	= { 0 },					\
810*67730e6cSSean Christopherson }
811*67730e6cSSean Christopherson 
812*67730e6cSSean Christopherson #define TEST_UFFD_AND_DIRTY_LOG(_access, _with_af, _uffd_data_handler,		\
813*67730e6cSSean Christopherson 				_uffd_faults, _test_check, _pt_check)		\
814*67730e6cSSean Christopherson {										\
815*67730e6cSSean Christopherson 	.name			= SCAT3(uffd_and_dirty_log, _access, _with_af),	\
816*67730e6cSSean Christopherson 	.data_memslot_flags	= KVM_MEM_LOG_DIRTY_PAGES,			\
817*67730e6cSSean Christopherson 	.pt_memslot_flags	= KVM_MEM_LOG_DIRTY_PAGES,			\
818*67730e6cSSean Christopherson 	.guest_prepare		= { _PREPARE(_with_af),				\
819*67730e6cSSean Christopherson 				    _PREPARE(_access) },			\
820*67730e6cSSean Christopherson 	.guest_test		= _access,					\
821*67730e6cSSean Christopherson 	.mem_mark_cmd		= CMD_HOLE_DATA | CMD_HOLE_PT,			\
822*67730e6cSSean Christopherson 	.guest_test_check	= { _CHECK(_with_af), _test_check, _pt_check },	\
823*67730e6cSSean Christopherson 	.uffd_data_handler	= _uffd_data_handler,				\
824*67730e6cSSean Christopherson 	.uffd_pt_handler	= uffd_pt_handler,				\
825*67730e6cSSean Christopherson 	.expected_events	= { .uffd_faults = _uffd_faults, },		\
826*67730e6cSSean Christopherson }
827*67730e6cSSean Christopherson 
828*67730e6cSSean Christopherson #define TEST_RO_MEMSLOT(_access, _mmio_handler, _mmio_exits)			\
829*67730e6cSSean Christopherson {										\
830*67730e6cSSean Christopherson 	.name			= SCAT2(ro_memslot, _access),			\
831*67730e6cSSean Christopherson 	.data_memslot_flags	= KVM_MEM_READONLY,				\
832*67730e6cSSean Christopherson 	.pt_memslot_flags	= KVM_MEM_READONLY,				\
833*67730e6cSSean Christopherson 	.guest_prepare		= { _PREPARE(_access) },			\
834*67730e6cSSean Christopherson 	.guest_test		= _access,					\
835*67730e6cSSean Christopherson 	.mmio_handler		= _mmio_handler,				\
836*67730e6cSSean Christopherson 	.expected_events	= { .mmio_exits = _mmio_exits },		\
837*67730e6cSSean Christopherson }
838*67730e6cSSean Christopherson 
839*67730e6cSSean Christopherson #define TEST_RO_MEMSLOT_NO_SYNDROME(_access)					\
840*67730e6cSSean Christopherson {										\
841*67730e6cSSean Christopherson 	.name			= SCAT2(ro_memslot_no_syndrome, _access),	\
842*67730e6cSSean Christopherson 	.data_memslot_flags	= KVM_MEM_READONLY,				\
843*67730e6cSSean Christopherson 	.pt_memslot_flags	= KVM_MEM_READONLY,				\
844*67730e6cSSean Christopherson 	.guest_prepare		= { _PREPARE(_access) },			\
845*67730e6cSSean Christopherson 	.guest_test		= _access,					\
846*67730e6cSSean Christopherson 	.fail_vcpu_run_handler	= fail_vcpu_run_mmio_no_syndrome_handler,	\
847*67730e6cSSean Christopherson 	.expected_events	= { .fail_vcpu_runs = 1 },			\
848*67730e6cSSean Christopherson }
849*67730e6cSSean Christopherson 
850*67730e6cSSean Christopherson #define TEST_RO_MEMSLOT_AND_DIRTY_LOG(_access, _mmio_handler, _mmio_exits,	\
851*67730e6cSSean Christopherson 				      _test_check)				\
852*67730e6cSSean Christopherson {										\
853*67730e6cSSean Christopherson 	.name			= SCAT2(ro_memslot, _access),			\
854*67730e6cSSean Christopherson 	.data_memslot_flags	= KVM_MEM_READONLY | KVM_MEM_LOG_DIRTY_PAGES,	\
855*67730e6cSSean Christopherson 	.pt_memslot_flags	= KVM_MEM_READONLY | KVM_MEM_LOG_DIRTY_PAGES,	\
856*67730e6cSSean Christopherson 	.guest_prepare		= { _PREPARE(_access) },			\
857*67730e6cSSean Christopherson 	.guest_test		= _access,					\
858*67730e6cSSean Christopherson 	.guest_test_check	= { _test_check },				\
859*67730e6cSSean Christopherson 	.mmio_handler		= _mmio_handler,				\
860*67730e6cSSean Christopherson 	.expected_events	= { .mmio_exits = _mmio_exits},			\
861*67730e6cSSean Christopherson }
862*67730e6cSSean Christopherson 
863*67730e6cSSean Christopherson #define TEST_RO_MEMSLOT_NO_SYNDROME_AND_DIRTY_LOG(_access, _test_check)		\
864*67730e6cSSean Christopherson {										\
865*67730e6cSSean Christopherson 	.name			= SCAT2(ro_memslot_no_syn_and_dlog, _access),	\
866*67730e6cSSean Christopherson 	.data_memslot_flags	= KVM_MEM_READONLY | KVM_MEM_LOG_DIRTY_PAGES,	\
867*67730e6cSSean Christopherson 	.pt_memslot_flags	= KVM_MEM_READONLY | KVM_MEM_LOG_DIRTY_PAGES,	\
868*67730e6cSSean Christopherson 	.guest_prepare		= { _PREPARE(_access) },			\
869*67730e6cSSean Christopherson 	.guest_test		= _access,					\
870*67730e6cSSean Christopherson 	.guest_test_check	= { _test_check },				\
871*67730e6cSSean Christopherson 	.fail_vcpu_run_handler	= fail_vcpu_run_mmio_no_syndrome_handler,	\
872*67730e6cSSean Christopherson 	.expected_events	= { .fail_vcpu_runs = 1 },			\
873*67730e6cSSean Christopherson }
874*67730e6cSSean Christopherson 
875*67730e6cSSean Christopherson #define TEST_RO_MEMSLOT_AND_UFFD(_access, _mmio_handler, _mmio_exits,		\
876*67730e6cSSean Christopherson 				 _uffd_data_handler, _uffd_faults)		\
877*67730e6cSSean Christopherson {										\
878*67730e6cSSean Christopherson 	.name			= SCAT2(ro_memslot_uffd, _access),		\
879*67730e6cSSean Christopherson 	.data_memslot_flags	= KVM_MEM_READONLY,				\
880*67730e6cSSean Christopherson 	.pt_memslot_flags	= KVM_MEM_READONLY,				\
881*67730e6cSSean Christopherson 	.mem_mark_cmd		= CMD_HOLE_DATA | CMD_HOLE_PT,			\
882*67730e6cSSean Christopherson 	.guest_prepare		= { _PREPARE(_access) },			\
883*67730e6cSSean Christopherson 	.guest_test		= _access,					\
884*67730e6cSSean Christopherson 	.uffd_data_handler	= _uffd_data_handler,				\
885*67730e6cSSean Christopherson 	.uffd_pt_handler	= uffd_pt_handler,				\
886*67730e6cSSean Christopherson 	.mmio_handler		= _mmio_handler,				\
887*67730e6cSSean Christopherson 	.expected_events	= { .mmio_exits = _mmio_exits,			\
888*67730e6cSSean Christopherson 				    .uffd_faults = _uffd_faults },		\
889*67730e6cSSean Christopherson }
890*67730e6cSSean Christopherson 
891*67730e6cSSean Christopherson #define TEST_RO_MEMSLOT_NO_SYNDROME_AND_UFFD(_access, _uffd_data_handler,	\
892*67730e6cSSean Christopherson 					     _uffd_faults)			\
893*67730e6cSSean Christopherson {										\
894*67730e6cSSean Christopherson 	.name			= SCAT2(ro_memslot_no_syndrome, _access),	\
895*67730e6cSSean Christopherson 	.data_memslot_flags	= KVM_MEM_READONLY,				\
896*67730e6cSSean Christopherson 	.pt_memslot_flags	= KVM_MEM_READONLY,				\
897*67730e6cSSean Christopherson 	.mem_mark_cmd		= CMD_HOLE_DATA | CMD_HOLE_PT,			\
898*67730e6cSSean Christopherson 	.guest_prepare		= { _PREPARE(_access) },			\
899*67730e6cSSean Christopherson 	.guest_test		= _access,					\
900*67730e6cSSean Christopherson 	.uffd_data_handler	= _uffd_data_handler,				\
901*67730e6cSSean Christopherson 	.uffd_pt_handler	= uffd_pt_handler,			\
902*67730e6cSSean Christopherson 	.fail_vcpu_run_handler	= fail_vcpu_run_mmio_no_syndrome_handler,	\
903*67730e6cSSean Christopherson 	.expected_events	= { .fail_vcpu_runs = 1,			\
904*67730e6cSSean Christopherson 				    .uffd_faults = _uffd_faults },		\
905*67730e6cSSean Christopherson }
906*67730e6cSSean Christopherson 
907*67730e6cSSean Christopherson static struct test_desc tests[] = {
908*67730e6cSSean Christopherson 
909*67730e6cSSean Christopherson 	/* Check that HW is setting the Access Flag (AF) (sanity checks). */
910*67730e6cSSean Christopherson 	TEST_ACCESS(guest_read64, with_af, CMD_NONE),
911*67730e6cSSean Christopherson 	TEST_ACCESS(guest_ld_preidx, with_af, CMD_NONE),
912*67730e6cSSean Christopherson 	TEST_ACCESS(guest_cas, with_af, CMD_NONE),
913*67730e6cSSean Christopherson 	TEST_ACCESS(guest_write64, with_af, CMD_NONE),
914*67730e6cSSean Christopherson 	TEST_ACCESS(guest_st_preidx, with_af, CMD_NONE),
915*67730e6cSSean Christopherson 	TEST_ACCESS(guest_dc_zva, with_af, CMD_NONE),
916*67730e6cSSean Christopherson 	TEST_ACCESS(guest_exec, with_af, CMD_NONE),
917*67730e6cSSean Christopherson 
918*67730e6cSSean Christopherson 	/*
919*67730e6cSSean Christopherson 	 * Punch a hole in the data backing store, and then try multiple
920*67730e6cSSean Christopherson 	 * accesses: reads should rturn zeroes, and writes should
921*67730e6cSSean Christopherson 	 * re-populate the page. Moreover, the test also check that no
922*67730e6cSSean Christopherson 	 * exception was generated in the guest.  Note that this
923*67730e6cSSean Christopherson 	 * reading/writing behavior is the same as reading/writing a
924*67730e6cSSean Christopherson 	 * punched page (with fallocate(FALLOC_FL_PUNCH_HOLE)) from
925*67730e6cSSean Christopherson 	 * userspace.
926*67730e6cSSean Christopherson 	 */
927*67730e6cSSean Christopherson 	TEST_ACCESS(guest_read64, no_af, CMD_HOLE_DATA),
928*67730e6cSSean Christopherson 	TEST_ACCESS(guest_cas, no_af, CMD_HOLE_DATA),
929*67730e6cSSean Christopherson 	TEST_ACCESS(guest_ld_preidx, no_af, CMD_HOLE_DATA),
930*67730e6cSSean Christopherson 	TEST_ACCESS(guest_write64, no_af, CMD_HOLE_DATA),
931*67730e6cSSean Christopherson 	TEST_ACCESS(guest_st_preidx, no_af, CMD_HOLE_DATA),
932*67730e6cSSean Christopherson 	TEST_ACCESS(guest_at, no_af, CMD_HOLE_DATA),
933*67730e6cSSean Christopherson 	TEST_ACCESS(guest_dc_zva, no_af, CMD_HOLE_DATA),
934*67730e6cSSean Christopherson 
935*67730e6cSSean Christopherson 	/*
936*67730e6cSSean Christopherson 	 * Punch holes in the data and PT backing stores and mark them for
937*67730e6cSSean Christopherson 	 * userfaultfd handling. This should result in 2 faults: the access
938*67730e6cSSean Christopherson 	 * on the data backing store, and its respective S1 page table walk
939*67730e6cSSean Christopherson 	 * (S1PTW).
940*67730e6cSSean Christopherson 	 */
941*67730e6cSSean Christopherson 	TEST_UFFD(guest_read64, with_af, CMD_HOLE_DATA | CMD_HOLE_PT,
942*67730e6cSSean Christopherson 		  uffd_data_handler, uffd_pt_handler, 2),
943*67730e6cSSean Christopherson 	TEST_UFFD(guest_read64, no_af, CMD_HOLE_DATA | CMD_HOLE_PT,
944*67730e6cSSean Christopherson 		  uffd_data_handler, uffd_pt_handler, 2),
945*67730e6cSSean Christopherson 	TEST_UFFD(guest_cas, with_af, CMD_HOLE_DATA | CMD_HOLE_PT,
946*67730e6cSSean Christopherson 		  uffd_data_handler, uffd_pt_handler, 2),
947*67730e6cSSean Christopherson 	/*
948*67730e6cSSean Christopherson 	 * Can't test guest_at with_af as it's IMPDEF whether the AF is set.
949*67730e6cSSean Christopherson 	 * The S1PTW fault should still be marked as a write.
950*67730e6cSSean Christopherson 	 */
951*67730e6cSSean Christopherson 	TEST_UFFD(guest_at, no_af, CMD_HOLE_DATA | CMD_HOLE_PT,
952*67730e6cSSean Christopherson 		  uffd_no_handler, uffd_pt_handler, 1),
953*67730e6cSSean Christopherson 	TEST_UFFD(guest_ld_preidx, with_af, CMD_HOLE_DATA | CMD_HOLE_PT,
954*67730e6cSSean Christopherson 		  uffd_data_handler, uffd_pt_handler, 2),
955*67730e6cSSean Christopherson 	TEST_UFFD(guest_write64, with_af, CMD_HOLE_DATA | CMD_HOLE_PT,
956*67730e6cSSean Christopherson 		  uffd_data_handler, uffd_pt_handler, 2),
957*67730e6cSSean Christopherson 	TEST_UFFD(guest_dc_zva, with_af, CMD_HOLE_DATA | CMD_HOLE_PT,
958*67730e6cSSean Christopherson 		  uffd_data_handler, uffd_pt_handler, 2),
959*67730e6cSSean Christopherson 	TEST_UFFD(guest_st_preidx, with_af, CMD_HOLE_DATA | CMD_HOLE_PT,
960*67730e6cSSean Christopherson 		  uffd_data_handler, uffd_pt_handler, 2),
961*67730e6cSSean Christopherson 	TEST_UFFD(guest_exec, with_af, CMD_HOLE_DATA | CMD_HOLE_PT,
962*67730e6cSSean Christopherson 		  uffd_data_handler, uffd_pt_handler, 2),
963*67730e6cSSean Christopherson 
964*67730e6cSSean Christopherson 	/*
965*67730e6cSSean Christopherson 	 * Try accesses when the data and PT memory regions are both
966*67730e6cSSean Christopherson 	 * tracked for dirty logging.
967*67730e6cSSean Christopherson 	 */
968*67730e6cSSean Christopherson 	TEST_DIRTY_LOG(guest_read64, with_af, guest_check_no_write_in_dirty_log,
969*67730e6cSSean Christopherson 		       guest_check_s1ptw_wr_in_dirty_log),
970*67730e6cSSean Christopherson 	TEST_DIRTY_LOG(guest_read64, no_af, guest_check_no_write_in_dirty_log,
971*67730e6cSSean Christopherson 		       guest_check_no_s1ptw_wr_in_dirty_log),
972*67730e6cSSean Christopherson 	TEST_DIRTY_LOG(guest_ld_preidx, with_af,
973*67730e6cSSean Christopherson 		       guest_check_no_write_in_dirty_log,
974*67730e6cSSean Christopherson 		       guest_check_s1ptw_wr_in_dirty_log),
975*67730e6cSSean Christopherson 	TEST_DIRTY_LOG(guest_at, no_af, guest_check_no_write_in_dirty_log,
976*67730e6cSSean Christopherson 		       guest_check_no_s1ptw_wr_in_dirty_log),
977*67730e6cSSean Christopherson 	TEST_DIRTY_LOG(guest_exec, with_af, guest_check_no_write_in_dirty_log,
978*67730e6cSSean Christopherson 		       guest_check_s1ptw_wr_in_dirty_log),
979*67730e6cSSean Christopherson 	TEST_DIRTY_LOG(guest_write64, with_af, guest_check_write_in_dirty_log,
980*67730e6cSSean Christopherson 		       guest_check_s1ptw_wr_in_dirty_log),
981*67730e6cSSean Christopherson 	TEST_DIRTY_LOG(guest_cas, with_af, guest_check_write_in_dirty_log,
982*67730e6cSSean Christopherson 		       guest_check_s1ptw_wr_in_dirty_log),
983*67730e6cSSean Christopherson 	TEST_DIRTY_LOG(guest_dc_zva, with_af, guest_check_write_in_dirty_log,
984*67730e6cSSean Christopherson 		       guest_check_s1ptw_wr_in_dirty_log),
985*67730e6cSSean Christopherson 	TEST_DIRTY_LOG(guest_st_preidx, with_af, guest_check_write_in_dirty_log,
986*67730e6cSSean Christopherson 		       guest_check_s1ptw_wr_in_dirty_log),
987*67730e6cSSean Christopherson 
988*67730e6cSSean Christopherson 	/*
989*67730e6cSSean Christopherson 	 * Access when the data and PT memory regions are both marked for
990*67730e6cSSean Christopherson 	 * dirty logging and UFFD at the same time. The expected result is
991*67730e6cSSean Christopherson 	 * that writes should mark the dirty log and trigger a userfaultfd
992*67730e6cSSean Christopherson 	 * write fault.  Reads/execs should result in a read userfaultfd
993*67730e6cSSean Christopherson 	 * fault, and nothing in the dirty log.  Any S1PTW should result in
994*67730e6cSSean Christopherson 	 * a write in the dirty log and a userfaultfd write.
995*67730e6cSSean Christopherson 	 */
996*67730e6cSSean Christopherson 	TEST_UFFD_AND_DIRTY_LOG(guest_read64, with_af,
997*67730e6cSSean Christopherson 				uffd_data_handler, 2,
998*67730e6cSSean Christopherson 				guest_check_no_write_in_dirty_log,
999*67730e6cSSean Christopherson 				guest_check_s1ptw_wr_in_dirty_log),
1000*67730e6cSSean Christopherson 	TEST_UFFD_AND_DIRTY_LOG(guest_read64, no_af,
1001*67730e6cSSean Christopherson 				uffd_data_handler, 2,
1002*67730e6cSSean Christopherson 				guest_check_no_write_in_dirty_log,
1003*67730e6cSSean Christopherson 				guest_check_no_s1ptw_wr_in_dirty_log),
1004*67730e6cSSean Christopherson 	TEST_UFFD_AND_DIRTY_LOG(guest_ld_preidx, with_af,
1005*67730e6cSSean Christopherson 				uffd_data_handler,
1006*67730e6cSSean Christopherson 				2, guest_check_no_write_in_dirty_log,
1007*67730e6cSSean Christopherson 				guest_check_s1ptw_wr_in_dirty_log),
1008*67730e6cSSean Christopherson 	TEST_UFFD_AND_DIRTY_LOG(guest_at, with_af, uffd_no_handler, 1,
1009*67730e6cSSean Christopherson 				guest_check_no_write_in_dirty_log,
1010*67730e6cSSean Christopherson 				guest_check_s1ptw_wr_in_dirty_log),
1011*67730e6cSSean Christopherson 	TEST_UFFD_AND_DIRTY_LOG(guest_exec, with_af,
1012*67730e6cSSean Christopherson 				uffd_data_handler, 2,
1013*67730e6cSSean Christopherson 				guest_check_no_write_in_dirty_log,
1014*67730e6cSSean Christopherson 				guest_check_s1ptw_wr_in_dirty_log),
1015*67730e6cSSean Christopherson 	TEST_UFFD_AND_DIRTY_LOG(guest_write64, with_af,
1016*67730e6cSSean Christopherson 				uffd_data_handler,
1017*67730e6cSSean Christopherson 				2, guest_check_write_in_dirty_log,
1018*67730e6cSSean Christopherson 				guest_check_s1ptw_wr_in_dirty_log),
1019*67730e6cSSean Christopherson 	TEST_UFFD_AND_DIRTY_LOG(guest_cas, with_af,
1020*67730e6cSSean Christopherson 				uffd_data_handler, 2,
1021*67730e6cSSean Christopherson 				guest_check_write_in_dirty_log,
1022*67730e6cSSean Christopherson 				guest_check_s1ptw_wr_in_dirty_log),
1023*67730e6cSSean Christopherson 	TEST_UFFD_AND_DIRTY_LOG(guest_dc_zva, with_af,
1024*67730e6cSSean Christopherson 				uffd_data_handler,
1025*67730e6cSSean Christopherson 				2, guest_check_write_in_dirty_log,
1026*67730e6cSSean Christopherson 				guest_check_s1ptw_wr_in_dirty_log),
1027*67730e6cSSean Christopherson 	TEST_UFFD_AND_DIRTY_LOG(guest_st_preidx, with_af,
1028*67730e6cSSean Christopherson 				uffd_data_handler, 2,
1029*67730e6cSSean Christopherson 				guest_check_write_in_dirty_log,
1030*67730e6cSSean Christopherson 				guest_check_s1ptw_wr_in_dirty_log),
1031*67730e6cSSean Christopherson 	/*
1032*67730e6cSSean Christopherson 	 * Access when both the PT and data regions are marked read-only
1033*67730e6cSSean Christopherson 	 * (with KVM_MEM_READONLY). Writes with a syndrome result in an
1034*67730e6cSSean Christopherson 	 * MMIO exit, writes with no syndrome (e.g., CAS) result in a
1035*67730e6cSSean Christopherson 	 * failed vcpu run, and reads/execs with and without syndroms do
1036*67730e6cSSean Christopherson 	 * not fault.
1037*67730e6cSSean Christopherson 	 */
1038*67730e6cSSean Christopherson 	TEST_RO_MEMSLOT(guest_read64, 0, 0),
1039*67730e6cSSean Christopherson 	TEST_RO_MEMSLOT(guest_ld_preidx, 0, 0),
1040*67730e6cSSean Christopherson 	TEST_RO_MEMSLOT(guest_at, 0, 0),
1041*67730e6cSSean Christopherson 	TEST_RO_MEMSLOT(guest_exec, 0, 0),
1042*67730e6cSSean Christopherson 	TEST_RO_MEMSLOT(guest_write64, mmio_on_test_gpa_handler, 1),
1043*67730e6cSSean Christopherson 	TEST_RO_MEMSLOT_NO_SYNDROME(guest_dc_zva),
1044*67730e6cSSean Christopherson 	TEST_RO_MEMSLOT_NO_SYNDROME(guest_cas),
1045*67730e6cSSean Christopherson 	TEST_RO_MEMSLOT_NO_SYNDROME(guest_st_preidx),
1046*67730e6cSSean Christopherson 
1047*67730e6cSSean Christopherson 	/*
1048*67730e6cSSean Christopherson 	 * The PT and data regions are both read-only and marked
1049*67730e6cSSean Christopherson 	 * for dirty logging at the same time. The expected result is that
1050*67730e6cSSean Christopherson 	 * for writes there should be no write in the dirty log. The
1051*67730e6cSSean Christopherson 	 * readonly handling is the same as if the memslot was not marked
1052*67730e6cSSean Christopherson 	 * for dirty logging: writes with a syndrome result in an MMIO
1053*67730e6cSSean Christopherson 	 * exit, and writes with no syndrome result in a failed vcpu run.
1054*67730e6cSSean Christopherson 	 */
1055*67730e6cSSean Christopherson 	TEST_RO_MEMSLOT_AND_DIRTY_LOG(guest_read64, 0, 0,
1056*67730e6cSSean Christopherson 				      guest_check_no_write_in_dirty_log),
1057*67730e6cSSean Christopherson 	TEST_RO_MEMSLOT_AND_DIRTY_LOG(guest_ld_preidx, 0, 0,
1058*67730e6cSSean Christopherson 				      guest_check_no_write_in_dirty_log),
1059*67730e6cSSean Christopherson 	TEST_RO_MEMSLOT_AND_DIRTY_LOG(guest_at, 0, 0,
1060*67730e6cSSean Christopherson 				      guest_check_no_write_in_dirty_log),
1061*67730e6cSSean Christopherson 	TEST_RO_MEMSLOT_AND_DIRTY_LOG(guest_exec, 0, 0,
1062*67730e6cSSean Christopherson 				      guest_check_no_write_in_dirty_log),
1063*67730e6cSSean Christopherson 	TEST_RO_MEMSLOT_AND_DIRTY_LOG(guest_write64, mmio_on_test_gpa_handler,
1064*67730e6cSSean Christopherson 				      1, guest_check_no_write_in_dirty_log),
1065*67730e6cSSean Christopherson 	TEST_RO_MEMSLOT_NO_SYNDROME_AND_DIRTY_LOG(guest_dc_zva,
1066*67730e6cSSean Christopherson 						  guest_check_no_write_in_dirty_log),
1067*67730e6cSSean Christopherson 	TEST_RO_MEMSLOT_NO_SYNDROME_AND_DIRTY_LOG(guest_cas,
1068*67730e6cSSean Christopherson 						  guest_check_no_write_in_dirty_log),
1069*67730e6cSSean Christopherson 	TEST_RO_MEMSLOT_NO_SYNDROME_AND_DIRTY_LOG(guest_st_preidx,
1070*67730e6cSSean Christopherson 						  guest_check_no_write_in_dirty_log),
1071*67730e6cSSean Christopherson 
1072*67730e6cSSean Christopherson 	/*
1073*67730e6cSSean Christopherson 	 * The PT and data regions are both read-only and punched with
1074*67730e6cSSean Christopherson 	 * holes tracked with userfaultfd.  The expected result is the
1075*67730e6cSSean Christopherson 	 * union of both userfaultfd and read-only behaviors. For example,
1076*67730e6cSSean Christopherson 	 * write accesses result in a userfaultfd write fault and an MMIO
1077*67730e6cSSean Christopherson 	 * exit.  Writes with no syndrome result in a failed vcpu run and
1078*67730e6cSSean Christopherson 	 * no userfaultfd write fault. Reads result in userfaultfd getting
1079*67730e6cSSean Christopherson 	 * triggered.
1080*67730e6cSSean Christopherson 	 */
1081*67730e6cSSean Christopherson 	TEST_RO_MEMSLOT_AND_UFFD(guest_read64, 0, 0, uffd_data_handler, 2),
1082*67730e6cSSean Christopherson 	TEST_RO_MEMSLOT_AND_UFFD(guest_ld_preidx, 0, 0, uffd_data_handler, 2),
1083*67730e6cSSean Christopherson 	TEST_RO_MEMSLOT_AND_UFFD(guest_at, 0, 0, uffd_no_handler, 1),
1084*67730e6cSSean Christopherson 	TEST_RO_MEMSLOT_AND_UFFD(guest_exec, 0, 0, uffd_data_handler, 2),
1085*67730e6cSSean Christopherson 	TEST_RO_MEMSLOT_AND_UFFD(guest_write64, mmio_on_test_gpa_handler, 1,
1086*67730e6cSSean Christopherson 				 uffd_data_handler, 2),
1087*67730e6cSSean Christopherson 	TEST_RO_MEMSLOT_NO_SYNDROME_AND_UFFD(guest_cas, uffd_data_handler, 2),
1088*67730e6cSSean Christopherson 	TEST_RO_MEMSLOT_NO_SYNDROME_AND_UFFD(guest_dc_zva, uffd_no_handler, 1),
1089*67730e6cSSean Christopherson 	TEST_RO_MEMSLOT_NO_SYNDROME_AND_UFFD(guest_st_preidx, uffd_no_handler, 1),
1090*67730e6cSSean Christopherson 
1091*67730e6cSSean Christopherson 	{ 0 }
1092*67730e6cSSean Christopherson };
1093*67730e6cSSean Christopherson 
1094*67730e6cSSean Christopherson static void for_each_test_and_guest_mode(enum vm_mem_backing_src_type src_type)
1095*67730e6cSSean Christopherson {
1096*67730e6cSSean Christopherson 	struct test_desc *t;
1097*67730e6cSSean Christopherson 
1098*67730e6cSSean Christopherson 	for (t = &tests[0]; t->name; t++) {
1099*67730e6cSSean Christopherson 		if (t->skip)
1100*67730e6cSSean Christopherson 			continue;
1101*67730e6cSSean Christopherson 
1102*67730e6cSSean Christopherson 		struct test_params p = {
1103*67730e6cSSean Christopherson 			.src_type = src_type,
1104*67730e6cSSean Christopherson 			.test_desc = t,
1105*67730e6cSSean Christopherson 		};
1106*67730e6cSSean Christopherson 
1107*67730e6cSSean Christopherson 		for_each_guest_mode(run_test, &p);
1108*67730e6cSSean Christopherson 	}
1109*67730e6cSSean Christopherson }
1110*67730e6cSSean Christopherson 
1111*67730e6cSSean Christopherson int main(int argc, char *argv[])
1112*67730e6cSSean Christopherson {
1113*67730e6cSSean Christopherson 	enum vm_mem_backing_src_type src_type;
1114*67730e6cSSean Christopherson 	int opt;
1115*67730e6cSSean Christopherson 
1116*67730e6cSSean Christopherson 	src_type = DEFAULT_VM_MEM_SRC;
1117*67730e6cSSean Christopherson 
1118*67730e6cSSean Christopherson 	while ((opt = getopt(argc, argv, "hm:s:")) != -1) {
1119*67730e6cSSean Christopherson 		switch (opt) {
1120*67730e6cSSean Christopherson 		case 'm':
1121*67730e6cSSean Christopherson 			guest_modes_cmdline(optarg);
1122*67730e6cSSean Christopherson 			break;
1123*67730e6cSSean Christopherson 		case 's':
1124*67730e6cSSean Christopherson 			src_type = parse_backing_src_type(optarg);
1125*67730e6cSSean Christopherson 			break;
1126*67730e6cSSean Christopherson 		case 'h':
1127*67730e6cSSean Christopherson 		default:
1128*67730e6cSSean Christopherson 			help(argv[0]);
1129*67730e6cSSean Christopherson 			exit(0);
1130*67730e6cSSean Christopherson 		}
1131*67730e6cSSean Christopherson 	}
1132*67730e6cSSean Christopherson 
1133*67730e6cSSean Christopherson 	for_each_test_and_guest_mode(src_type);
1134*67730e6cSSean Christopherson 	return 0;
1135*67730e6cSSean Christopherson }
1136