xref: /linux/tools/testing/selftests/kvm/s390x/ucontrol_test.c (revision a634dda26186cf9a51567020fcce52bcba5e1e59)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Test code for the s390x kvm ucontrol interface
4  *
5  * Copyright IBM Corp. 2024
6  *
7  * Authors:
8  *  Christoph Schlameuss <schlameuss@linux.ibm.com>
9  */
10 #include "debug_print.h"
11 #include "kselftest_harness.h"
12 #include "kvm_util.h"
13 #include "processor.h"
14 #include "sie.h"
15 
16 #include <linux/capability.h>
17 #include <linux/sizes.h>
18 
19 #define PGM_SEGMENT_TRANSLATION 0x10
20 
21 #define VM_MEM_SIZE (4 * SZ_1M)
22 #define VM_MEM_EXT_SIZE (2 * SZ_1M)
23 #define VM_MEM_MAX_M ((VM_MEM_SIZE + VM_MEM_EXT_SIZE) / SZ_1M)
24 
25 /* so directly declare capget to check caps without libcap */
26 int capget(cap_user_header_t header, cap_user_data_t data);
27 
28 /**
29  * In order to create user controlled virtual machines on S390,
30  * check KVM_CAP_S390_UCONTROL and use the flag KVM_VM_S390_UCONTROL
31  * as privileged user (SYS_ADMIN).
32  */
33 void require_ucontrol_admin(void)
34 {
35 	struct __user_cap_data_struct data[_LINUX_CAPABILITY_U32S_3];
36 	struct __user_cap_header_struct hdr = {
37 		.version = _LINUX_CAPABILITY_VERSION_3,
38 	};
39 	int rc;
40 
41 	rc = capget(&hdr, data);
42 	TEST_ASSERT_EQ(0, rc);
43 	TEST_REQUIRE((data->effective & CAP_TO_MASK(CAP_SYS_ADMIN)) > 0);
44 
45 	TEST_REQUIRE(kvm_has_cap(KVM_CAP_S390_UCONTROL));
46 }
47 
48 /* Test program setting some registers and looping */
49 extern char test_gprs_asm[];
50 asm("test_gprs_asm:\n"
51 	"xgr	%r0, %r0\n"
52 	"lgfi	%r1,1\n"
53 	"lgfi	%r2,2\n"
54 	"lgfi	%r3,3\n"
55 	"lgfi	%r4,4\n"
56 	"lgfi	%r5,5\n"
57 	"lgfi	%r6,6\n"
58 	"lgfi	%r7,7\n"
59 	"0:\n"
60 	"	diag	0,0,0x44\n"
61 	"	ahi	%r0,1\n"
62 	"	j	0b\n"
63 );
64 
65 /* Test program manipulating memory */
66 extern char test_mem_asm[];
67 asm("test_mem_asm:\n"
68 	"xgr	%r0, %r0\n"
69 
70 	"0:\n"
71 	"	ahi	%r0,1\n"
72 	"	st	%r1,0(%r5,%r6)\n"
73 
74 	"	xgr	%r1,%r1\n"
75 	"	l	%r1,0(%r5,%r6)\n"
76 	"	ahi	%r0,1\n"
77 	"	diag	0,0,0x44\n"
78 
79 	"	j	0b\n"
80 );
81 
82 /* Test program manipulating storage keys */
83 extern char test_skey_asm[];
84 asm("test_skey_asm:\n"
85 	"xgr	%r0, %r0\n"
86 
87 	"0:\n"
88 	"	ahi	%r0,1\n"
89 	"	st	%r1,0(%r5,%r6)\n"
90 
91 	"	iske	%r1,%r6\n"
92 	"	ahi	%r0,1\n"
93 	"	diag	0,0,0x44\n"
94 
95 	"	sske	%r1,%r6\n"
96 	"	xgr	%r1,%r1\n"
97 	"	iske	%r1,%r6\n"
98 	"	ahi	%r0,1\n"
99 	"	diag	0,0,0x44\n"
100 
101 	"	rrbe	%r1,%r6\n"
102 	"	iske	%r1,%r6\n"
103 	"	ahi	%r0,1\n"
104 	"	diag	0,0,0x44\n"
105 
106 	"	j	0b\n"
107 );
108 
109 FIXTURE(uc_kvm)
110 {
111 	struct kvm_s390_sie_block *sie_block;
112 	struct kvm_run *run;
113 	uintptr_t base_gpa;
114 	uintptr_t code_gpa;
115 	uintptr_t base_hva;
116 	uintptr_t code_hva;
117 	int kvm_run_size;
118 	vm_paddr_t pgd;
119 	void *vm_mem;
120 	int vcpu_fd;
121 	int kvm_fd;
122 	int vm_fd;
123 };
124 
125 /**
126  * create VM with single vcpu, map kvm_run and SIE control block for easy access
127  */
128 FIXTURE_SETUP(uc_kvm)
129 {
130 	struct kvm_s390_vm_cpu_processor info;
131 	int rc;
132 
133 	require_ucontrol_admin();
134 
135 	self->kvm_fd = open_kvm_dev_path_or_exit();
136 	self->vm_fd = ioctl(self->kvm_fd, KVM_CREATE_VM, KVM_VM_S390_UCONTROL);
137 	ASSERT_GE(self->vm_fd, 0);
138 
139 	kvm_device_attr_get(self->vm_fd, KVM_S390_VM_CPU_MODEL,
140 			    KVM_S390_VM_CPU_PROCESSOR, &info);
141 	TH_LOG("create VM 0x%llx", info.cpuid);
142 
143 	self->vcpu_fd = ioctl(self->vm_fd, KVM_CREATE_VCPU, 0);
144 	ASSERT_GE(self->vcpu_fd, 0);
145 
146 	self->kvm_run_size = ioctl(self->kvm_fd, KVM_GET_VCPU_MMAP_SIZE, NULL);
147 	ASSERT_GE(self->kvm_run_size, sizeof(struct kvm_run))
148 		  TH_LOG(KVM_IOCTL_ERROR(KVM_GET_VCPU_MMAP_SIZE, self->kvm_run_size));
149 	self->run = (struct kvm_run *)mmap(NULL, self->kvm_run_size,
150 		    PROT_READ | PROT_WRITE, MAP_SHARED, self->vcpu_fd, 0);
151 	ASSERT_NE(self->run, MAP_FAILED);
152 	/**
153 	 * For virtual cpus that have been created with S390 user controlled
154 	 * virtual machines, the resulting vcpu fd can be memory mapped at page
155 	 * offset KVM_S390_SIE_PAGE_OFFSET in order to obtain a memory map of
156 	 * the virtual cpu's hardware control block.
157 	 */
158 	self->sie_block = (struct kvm_s390_sie_block *)mmap(NULL, PAGE_SIZE,
159 			  PROT_READ | PROT_WRITE, MAP_SHARED,
160 			  self->vcpu_fd, KVM_S390_SIE_PAGE_OFFSET << PAGE_SHIFT);
161 	ASSERT_NE(self->sie_block, MAP_FAILED);
162 
163 	TH_LOG("VM created %p %p", self->run, self->sie_block);
164 
165 	self->base_gpa = 0;
166 	self->code_gpa = self->base_gpa + (3 * SZ_1M);
167 
168 	self->vm_mem = aligned_alloc(SZ_1M, VM_MEM_MAX_M * SZ_1M);
169 	ASSERT_NE(NULL, self->vm_mem) TH_LOG("malloc failed %u", errno);
170 	self->base_hva = (uintptr_t)self->vm_mem;
171 	self->code_hva = self->base_hva - self->base_gpa + self->code_gpa;
172 	struct kvm_s390_ucas_mapping map = {
173 		.user_addr = self->base_hva,
174 		.vcpu_addr = self->base_gpa,
175 		.length = VM_MEM_SIZE,
176 	};
177 	TH_LOG("ucas map %p %p 0x%llx",
178 	       (void *)map.user_addr, (void *)map.vcpu_addr, map.length);
179 	rc = ioctl(self->vcpu_fd, KVM_S390_UCAS_MAP, &map);
180 	ASSERT_EQ(0, rc) TH_LOG("ucas map result %d not expected, %s",
181 				rc, strerror(errno));
182 
183 	TH_LOG("page in %p", (void *)self->base_gpa);
184 	rc = ioctl(self->vcpu_fd, KVM_S390_VCPU_FAULT, self->base_gpa);
185 	ASSERT_EQ(0, rc) TH_LOG("vcpu fault (%p) result %d not expected, %s",
186 				(void *)self->base_hva, rc, strerror(errno));
187 
188 	self->sie_block->cpuflags &= ~CPUSTAT_STOPPED;
189 }
190 
191 FIXTURE_TEARDOWN(uc_kvm)
192 {
193 	munmap(self->sie_block, PAGE_SIZE);
194 	munmap(self->run, self->kvm_run_size);
195 	close(self->vcpu_fd);
196 	close(self->vm_fd);
197 	close(self->kvm_fd);
198 	free(self->vm_mem);
199 }
200 
201 TEST_F(uc_kvm, uc_sie_assertions)
202 {
203 	/* assert interception of Code 08 (Program Interruption) is set */
204 	EXPECT_EQ(0, self->sie_block->ecb & ECB_SPECI);
205 }
206 
207 TEST_F(uc_kvm, uc_attr_mem_limit)
208 {
209 	u64 limit;
210 	struct kvm_device_attr attr = {
211 		.group = KVM_S390_VM_MEM_CTRL,
212 		.attr = KVM_S390_VM_MEM_LIMIT_SIZE,
213 		.addr = (u64)&limit,
214 	};
215 	int rc;
216 
217 	rc = ioctl(self->vm_fd, KVM_HAS_DEVICE_ATTR, &attr);
218 	EXPECT_EQ(0, rc);
219 
220 	rc = ioctl(self->vm_fd, KVM_GET_DEVICE_ATTR, &attr);
221 	EXPECT_EQ(0, rc);
222 	EXPECT_EQ(~0UL, limit);
223 
224 	/* assert set not supported */
225 	rc = ioctl(self->vm_fd, KVM_SET_DEVICE_ATTR, &attr);
226 	EXPECT_EQ(-1, rc);
227 	EXPECT_EQ(EINVAL, errno);
228 }
229 
230 TEST_F(uc_kvm, uc_no_dirty_log)
231 {
232 	struct kvm_dirty_log dlog;
233 	int rc;
234 
235 	rc = ioctl(self->vm_fd, KVM_GET_DIRTY_LOG, &dlog);
236 	EXPECT_EQ(-1, rc);
237 	EXPECT_EQ(EINVAL, errno);
238 }
239 
240 /**
241  * Assert HPAGE CAP cannot be enabled on UCONTROL VM
242  */
243 TEST(uc_cap_hpage)
244 {
245 	int rc, kvm_fd, vm_fd, vcpu_fd;
246 	struct kvm_enable_cap cap = {
247 		.cap = KVM_CAP_S390_HPAGE_1M,
248 	};
249 
250 	require_ucontrol_admin();
251 
252 	kvm_fd = open_kvm_dev_path_or_exit();
253 	vm_fd = ioctl(kvm_fd, KVM_CREATE_VM, KVM_VM_S390_UCONTROL);
254 	ASSERT_GE(vm_fd, 0);
255 
256 	/* assert hpages are not supported on ucontrol vm */
257 	rc = ioctl(vm_fd, KVM_CHECK_EXTENSION, KVM_CAP_S390_HPAGE_1M);
258 	EXPECT_EQ(0, rc);
259 
260 	/* Test that KVM_CAP_S390_HPAGE_1M can't be enabled for a ucontrol vm */
261 	rc = ioctl(vm_fd, KVM_ENABLE_CAP, cap);
262 	EXPECT_EQ(-1, rc);
263 	EXPECT_EQ(EINVAL, errno);
264 
265 	/* assert HPAGE CAP is rejected after vCPU creation */
266 	vcpu_fd = ioctl(vm_fd, KVM_CREATE_VCPU, 0);
267 	ASSERT_GE(vcpu_fd, 0);
268 	rc = ioctl(vm_fd, KVM_ENABLE_CAP, cap);
269 	EXPECT_EQ(-1, rc);
270 	EXPECT_EQ(EBUSY, errno);
271 
272 	close(vcpu_fd);
273 	close(vm_fd);
274 	close(kvm_fd);
275 }
276 
277 /* calculate host virtual addr from guest physical addr */
278 static void *gpa2hva(FIXTURE_DATA(uc_kvm) *self, u64 gpa)
279 {
280 	return (void *)(self->base_hva - self->base_gpa + gpa);
281 }
282 
283 /* map / make additional memory available */
284 static int uc_map_ext(FIXTURE_DATA(uc_kvm) *self, u64 vcpu_addr, u64 length)
285 {
286 	struct kvm_s390_ucas_mapping map = {
287 		.user_addr = (u64)gpa2hva(self, vcpu_addr),
288 		.vcpu_addr = vcpu_addr,
289 		.length = length,
290 	};
291 	pr_info("ucas map %p %p 0x%llx",
292 		(void *)map.user_addr, (void *)map.vcpu_addr, map.length);
293 	return ioctl(self->vcpu_fd, KVM_S390_UCAS_MAP, &map);
294 }
295 
296 /* unmap previously mapped memory */
297 static int uc_unmap_ext(FIXTURE_DATA(uc_kvm) *self, u64 vcpu_addr, u64 length)
298 {
299 	struct kvm_s390_ucas_mapping map = {
300 		.user_addr = (u64)gpa2hva(self, vcpu_addr),
301 		.vcpu_addr = vcpu_addr,
302 		.length = length,
303 	};
304 	pr_info("ucas unmap %p %p 0x%llx",
305 		(void *)map.user_addr, (void *)map.vcpu_addr, map.length);
306 	return ioctl(self->vcpu_fd, KVM_S390_UCAS_UNMAP, &map);
307 }
308 
309 /* handle ucontrol exit by mapping the accessed segment */
310 static void uc_handle_exit_ucontrol(FIXTURE_DATA(uc_kvm) *self)
311 {
312 	struct kvm_run *run = self->run;
313 	u64 seg_addr;
314 	int rc;
315 
316 	TEST_ASSERT_EQ(KVM_EXIT_S390_UCONTROL, run->exit_reason);
317 	switch (run->s390_ucontrol.pgm_code) {
318 	case PGM_SEGMENT_TRANSLATION:
319 		seg_addr = run->s390_ucontrol.trans_exc_code & ~(SZ_1M - 1);
320 		pr_info("ucontrol pic segment translation 0x%llx, mapping segment 0x%lx\n",
321 			run->s390_ucontrol.trans_exc_code, seg_addr);
322 		/* map / make additional memory available */
323 		rc = uc_map_ext(self, seg_addr, SZ_1M);
324 		TEST_ASSERT_EQ(0, rc);
325 		break;
326 	default:
327 		TEST_FAIL("UNEXPECTED PGM CODE %d", run->s390_ucontrol.pgm_code);
328 	}
329 }
330 
331 /*
332  * Handle the SIEIC exit
333  * * fail on codes not expected in the test cases
334  * Returns if interception is handled / execution can be continued
335  */
336 static void uc_skey_enable(FIXTURE_DATA(uc_kvm) *self)
337 {
338 	struct kvm_s390_sie_block *sie_block = self->sie_block;
339 
340 	/* disable KSS */
341 	sie_block->cpuflags &= ~CPUSTAT_KSS;
342 	/* disable skey inst interception */
343 	sie_block->ictl &= ~(ICTL_ISKE | ICTL_SSKE | ICTL_RRBE);
344 }
345 
346 /*
347  * Handle the instruction intercept
348  * Returns if interception is handled / execution can be continued
349  */
350 static bool uc_handle_insn_ic(FIXTURE_DATA(uc_kvm) *self)
351 {
352 	struct kvm_s390_sie_block *sie_block = self->sie_block;
353 	int ilen = insn_length(sie_block->ipa >> 8);
354 	struct kvm_run *run = self->run;
355 
356 	switch (run->s390_sieic.ipa) {
357 	case 0xB229: /* ISKE */
358 	case 0xB22b: /* SSKE */
359 	case 0xB22a: /* RRBE */
360 		uc_skey_enable(self);
361 
362 		/* rewind to reexecute intercepted instruction */
363 		run->psw_addr = run->psw_addr - ilen;
364 		pr_info("rewind guest addr to 0x%.16llx\n", run->psw_addr);
365 		return true;
366 	default:
367 		return false;
368 	}
369 }
370 
371 /*
372  * Handle the SIEIC exit
373  * * fail on codes not expected in the test cases
374  * Returns if interception is handled / execution can be continued
375  */
376 static bool uc_handle_sieic(FIXTURE_DATA(uc_kvm) *self)
377 {
378 	struct kvm_s390_sie_block *sie_block = self->sie_block;
379 	struct kvm_run *run = self->run;
380 
381 	/* check SIE interception code */
382 	pr_info("sieic: 0x%.2x 0x%.4x 0x%.8x\n",
383 		run->s390_sieic.icptcode,
384 		run->s390_sieic.ipa,
385 		run->s390_sieic.ipb);
386 	switch (run->s390_sieic.icptcode) {
387 	case ICPT_INST:
388 		/* end execution in caller on intercepted instruction */
389 		pr_info("sie instruction interception\n");
390 		return uc_handle_insn_ic(self);
391 	case ICPT_KSS:
392 		uc_skey_enable(self);
393 		return true;
394 	case ICPT_OPEREXC:
395 		/* operation exception */
396 		TEST_FAIL("sie exception on %.4x%.8x", sie_block->ipa, sie_block->ipb);
397 	default:
398 		TEST_FAIL("UNEXPECTED SIEIC CODE %d", run->s390_sieic.icptcode);
399 	}
400 	return true;
401 }
402 
403 /* verify VM state on exit */
404 static bool uc_handle_exit(FIXTURE_DATA(uc_kvm) *self)
405 {
406 	struct kvm_run *run = self->run;
407 
408 	switch (run->exit_reason) {
409 	case KVM_EXIT_S390_UCONTROL:
410 		/** check program interruption code
411 		 * handle page fault --> ucas map
412 		 */
413 		uc_handle_exit_ucontrol(self);
414 		break;
415 	case KVM_EXIT_S390_SIEIC:
416 		return uc_handle_sieic(self);
417 	default:
418 		pr_info("exit_reason %2d not handled\n", run->exit_reason);
419 	}
420 	return true;
421 }
422 
423 /* run the VM until interrupted */
424 static int uc_run_once(FIXTURE_DATA(uc_kvm) *self)
425 {
426 	int rc;
427 
428 	rc = ioctl(self->vcpu_fd, KVM_RUN, NULL);
429 	print_run(self->run, self->sie_block);
430 	print_regs(self->run);
431 	pr_debug("run %d / %d %s\n", rc, errno, strerror(errno));
432 	return rc;
433 }
434 
435 static void uc_assert_diag44(FIXTURE_DATA(uc_kvm) *self)
436 {
437 	struct kvm_s390_sie_block *sie_block = self->sie_block;
438 
439 	/* assert vm was interrupted by diag 0x0044 */
440 	TEST_ASSERT_EQ(KVM_EXIT_S390_SIEIC, self->run->exit_reason);
441 	TEST_ASSERT_EQ(ICPT_INST, sie_block->icptcode);
442 	TEST_ASSERT_EQ(0x8300, sie_block->ipa);
443 	TEST_ASSERT_EQ(0x440000, sie_block->ipb);
444 }
445 
446 TEST_F(uc_kvm, uc_no_user_region)
447 {
448 	struct kvm_userspace_memory_region region = {
449 		.slot = 1,
450 		.guest_phys_addr = self->code_gpa,
451 		.memory_size = VM_MEM_EXT_SIZE,
452 		.userspace_addr = (uintptr_t)self->code_hva,
453 	};
454 	struct kvm_userspace_memory_region2 region2 = {
455 		.slot = 1,
456 		.guest_phys_addr = self->code_gpa,
457 		.memory_size = VM_MEM_EXT_SIZE,
458 		.userspace_addr = (uintptr_t)self->code_hva,
459 	};
460 
461 	ASSERT_EQ(-1, ioctl(self->vm_fd, KVM_SET_USER_MEMORY_REGION, &region));
462 	ASSERT_EQ(EINVAL, errno);
463 
464 	ASSERT_EQ(-1, ioctl(self->vm_fd, KVM_SET_USER_MEMORY_REGION2, &region2));
465 	ASSERT_EQ(EINVAL, errno);
466 }
467 
468 TEST_F(uc_kvm, uc_map_unmap)
469 {
470 	struct kvm_sync_regs *sync_regs = &self->run->s.regs;
471 	struct kvm_run *run = self->run;
472 	const u64 disp = 1;
473 	int rc;
474 
475 	/* copy test_mem_asm to code_hva / code_gpa */
476 	TH_LOG("copy code %p to vm mapped memory %p / %p",
477 	       &test_mem_asm, (void *)self->code_hva, (void *)self->code_gpa);
478 	memcpy((void *)self->code_hva, &test_mem_asm, PAGE_SIZE);
479 
480 	/* DAT disabled + 64 bit mode */
481 	run->psw_mask = 0x0000000180000000ULL;
482 	run->psw_addr = self->code_gpa;
483 
484 	/* set register content for test_mem_asm to access not mapped memory*/
485 	sync_regs->gprs[1] = 0x55;
486 	sync_regs->gprs[5] = self->base_gpa;
487 	sync_regs->gprs[6] = VM_MEM_SIZE + disp;
488 	run->kvm_dirty_regs |= KVM_SYNC_GPRS;
489 
490 	/* run and expect to fail with ucontrol pic segment translation */
491 	ASSERT_EQ(0, uc_run_once(self));
492 	ASSERT_EQ(1, sync_regs->gprs[0]);
493 	ASSERT_EQ(KVM_EXIT_S390_UCONTROL, run->exit_reason);
494 
495 	ASSERT_EQ(PGM_SEGMENT_TRANSLATION, run->s390_ucontrol.pgm_code);
496 	ASSERT_EQ(self->base_gpa + VM_MEM_SIZE, run->s390_ucontrol.trans_exc_code);
497 
498 	/* fail to map memory with not segment aligned address */
499 	rc = uc_map_ext(self, self->base_gpa + VM_MEM_SIZE + disp, VM_MEM_EXT_SIZE);
500 	ASSERT_GT(0, rc)
501 		TH_LOG("ucas map for non segment address should fail but didn't; "
502 		       "result %d not expected, %s", rc, strerror(errno));
503 
504 	/* map / make additional memory available */
505 	rc = uc_map_ext(self, self->base_gpa + VM_MEM_SIZE, VM_MEM_EXT_SIZE);
506 	ASSERT_EQ(0, rc)
507 		TH_LOG("ucas map result %d not expected, %s", rc, strerror(errno));
508 	ASSERT_EQ(0, uc_run_once(self));
509 	ASSERT_EQ(false, uc_handle_exit(self));
510 	uc_assert_diag44(self);
511 
512 	/* assert registers and memory are in expected state */
513 	ASSERT_EQ(2, sync_regs->gprs[0]);
514 	ASSERT_EQ(0x55, sync_regs->gprs[1]);
515 	ASSERT_EQ(0x55, *(u32 *)gpa2hva(self, self->base_gpa + VM_MEM_SIZE + disp));
516 
517 	/* unmap and run loop again */
518 	rc = uc_unmap_ext(self, self->base_gpa + VM_MEM_SIZE, VM_MEM_EXT_SIZE);
519 	ASSERT_EQ(0, rc)
520 		TH_LOG("ucas unmap result %d not expected, %s", rc, strerror(errno));
521 	ASSERT_EQ(0, uc_run_once(self));
522 	ASSERT_EQ(3, sync_regs->gprs[0]);
523 	ASSERT_EQ(KVM_EXIT_S390_UCONTROL, run->exit_reason);
524 	ASSERT_EQ(PGM_SEGMENT_TRANSLATION, run->s390_ucontrol.pgm_code);
525 	/* handle ucontrol exit and remap memory after previous map and unmap */
526 	ASSERT_EQ(true, uc_handle_exit(self));
527 }
528 
529 TEST_F(uc_kvm, uc_gprs)
530 {
531 	struct kvm_sync_regs *sync_regs = &self->run->s.regs;
532 	struct kvm_run *run = self->run;
533 	struct kvm_regs regs = {};
534 
535 	/* Set registers to values that are different from the ones that we expect below */
536 	for (int i = 0; i < 8; i++)
537 		sync_regs->gprs[i] = 8;
538 	run->kvm_dirty_regs |= KVM_SYNC_GPRS;
539 
540 	/* copy test_gprs_asm to code_hva / code_gpa */
541 	TH_LOG("copy code %p to vm mapped memory %p / %p",
542 	       &test_gprs_asm, (void *)self->code_hva, (void *)self->code_gpa);
543 	memcpy((void *)self->code_hva, &test_gprs_asm, PAGE_SIZE);
544 
545 	/* DAT disabled + 64 bit mode */
546 	run->psw_mask = 0x0000000180000000ULL;
547 	run->psw_addr = self->code_gpa;
548 
549 	/* run and expect interception of diag 44 */
550 	ASSERT_EQ(0, uc_run_once(self));
551 	ASSERT_EQ(false, uc_handle_exit(self));
552 	uc_assert_diag44(self);
553 
554 	/* Retrieve and check guest register values */
555 	ASSERT_EQ(0, ioctl(self->vcpu_fd, KVM_GET_REGS, &regs));
556 	for (int i = 0; i < 8; i++) {
557 		ASSERT_EQ(i, regs.gprs[i]);
558 		ASSERT_EQ(i, sync_regs->gprs[i]);
559 	}
560 
561 	/* run and expect interception of diag 44 again */
562 	ASSERT_EQ(0, uc_run_once(self));
563 	ASSERT_EQ(false, uc_handle_exit(self));
564 	uc_assert_diag44(self);
565 
566 	/* check continued increment of register 0 value */
567 	ASSERT_EQ(0, ioctl(self->vcpu_fd, KVM_GET_REGS, &regs));
568 	ASSERT_EQ(1, regs.gprs[0]);
569 	ASSERT_EQ(1, sync_regs->gprs[0]);
570 }
571 
572 TEST_F(uc_kvm, uc_skey)
573 {
574 	struct kvm_s390_sie_block *sie_block = self->sie_block;
575 	struct kvm_sync_regs *sync_regs = &self->run->s.regs;
576 	u64 test_vaddr = VM_MEM_SIZE - (SZ_1M / 2);
577 	struct kvm_run *run = self->run;
578 	const u8 skeyvalue = 0x34;
579 
580 	/* copy test_skey_asm to code_hva / code_gpa */
581 	TH_LOG("copy code %p to vm mapped memory %p / %p",
582 	       &test_skey_asm, (void *)self->code_hva, (void *)self->code_gpa);
583 	memcpy((void *)self->code_hva, &test_skey_asm, PAGE_SIZE);
584 
585 	/* set register content for test_skey_asm to access not mapped memory */
586 	sync_regs->gprs[1] = skeyvalue;
587 	sync_regs->gprs[5] = self->base_gpa;
588 	sync_regs->gprs[6] = test_vaddr;
589 	run->kvm_dirty_regs |= KVM_SYNC_GPRS;
590 
591 	/* DAT disabled + 64 bit mode */
592 	run->psw_mask = 0x0000000180000000ULL;
593 	run->psw_addr = self->code_gpa;
594 
595 	ASSERT_EQ(0, uc_run_once(self));
596 	ASSERT_EQ(true, uc_handle_exit(self));
597 	ASSERT_EQ(1, sync_regs->gprs[0]);
598 
599 	/* ISKE */
600 	ASSERT_EQ(0, uc_run_once(self));
601 
602 	/*
603 	 * Bail out and skip the test after uc_skey_enable was executed but iske
604 	 * is still intercepted. Instructions are not handled by the kernel.
605 	 * Thus there is no need to test this here.
606 	 */
607 	TEST_ASSERT_EQ(0, sie_block->cpuflags & CPUSTAT_KSS);
608 	TEST_ASSERT_EQ(0, sie_block->ictl & (ICTL_ISKE | ICTL_SSKE | ICTL_RRBE));
609 	TEST_ASSERT_EQ(KVM_EXIT_S390_SIEIC, self->run->exit_reason);
610 	TEST_ASSERT_EQ(ICPT_INST, sie_block->icptcode);
611 	TEST_REQUIRE(sie_block->ipa != 0xb229);
612 
613 	/* ISKE contd. */
614 	ASSERT_EQ(false, uc_handle_exit(self));
615 	ASSERT_EQ(2, sync_regs->gprs[0]);
616 	/* assert initial skey (ACC = 0, R & C = 1) */
617 	ASSERT_EQ(0x06, sync_regs->gprs[1]);
618 	uc_assert_diag44(self);
619 
620 	/* SSKE + ISKE */
621 	sync_regs->gprs[1] = skeyvalue;
622 	run->kvm_dirty_regs |= KVM_SYNC_GPRS;
623 	ASSERT_EQ(0, uc_run_once(self));
624 	ASSERT_EQ(false, uc_handle_exit(self));
625 	ASSERT_EQ(3, sync_regs->gprs[0]);
626 	ASSERT_EQ(skeyvalue, sync_regs->gprs[1]);
627 	uc_assert_diag44(self);
628 
629 	/* RRBE + ISKE */
630 	sync_regs->gprs[1] = skeyvalue;
631 	run->kvm_dirty_regs |= KVM_SYNC_GPRS;
632 	ASSERT_EQ(0, uc_run_once(self));
633 	ASSERT_EQ(false, uc_handle_exit(self));
634 	ASSERT_EQ(4, sync_regs->gprs[0]);
635 	/* assert R reset but rest of skey unchanged */
636 	ASSERT_EQ(skeyvalue & 0xfa, sync_regs->gprs[1]);
637 	ASSERT_EQ(0, sync_regs->gprs[1] & 0x04);
638 	uc_assert_diag44(self);
639 }
640 
641 static char uc_flic_b[PAGE_SIZE];
642 static struct kvm_s390_io_adapter uc_flic_ioa = { .id = 0 };
643 static struct kvm_s390_io_adapter_req uc_flic_ioam = { .id = 0 };
644 static struct kvm_s390_ais_req uc_flic_asim = { .isc = 0 };
645 static struct kvm_s390_ais_all uc_flic_asima = { .simm = 0 };
646 static struct uc_flic_attr_test {
647 	char *name;
648 	struct kvm_device_attr a;
649 	int hasrc;
650 	int geterrno;
651 	int seterrno;
652 } uc_flic_attr_tests[] = {
653 	{
654 		.name = "KVM_DEV_FLIC_GET_ALL_IRQS",
655 		.seterrno = EINVAL,
656 		.a = {
657 			.group = KVM_DEV_FLIC_GET_ALL_IRQS,
658 			.addr = (u64)&uc_flic_b,
659 			.attr = PAGE_SIZE,
660 		},
661 	},
662 	{
663 		.name = "KVM_DEV_FLIC_ENQUEUE",
664 		.geterrno = EINVAL,
665 		.a = { .group = KVM_DEV_FLIC_ENQUEUE, },
666 	},
667 	{
668 		.name = "KVM_DEV_FLIC_CLEAR_IRQS",
669 		.geterrno = EINVAL,
670 		.a = { .group = KVM_DEV_FLIC_CLEAR_IRQS, },
671 	},
672 	{
673 		.name = "KVM_DEV_FLIC_ADAPTER_REGISTER",
674 		.geterrno = EINVAL,
675 		.a = {
676 			.group = KVM_DEV_FLIC_ADAPTER_REGISTER,
677 			.addr = (u64)&uc_flic_ioa,
678 		},
679 	},
680 	{
681 		.name = "KVM_DEV_FLIC_ADAPTER_MODIFY",
682 		.geterrno = EINVAL,
683 		.seterrno = EINVAL,
684 		.a = {
685 			.group = KVM_DEV_FLIC_ADAPTER_MODIFY,
686 			.addr = (u64)&uc_flic_ioam,
687 			.attr = sizeof(uc_flic_ioam),
688 		},
689 	},
690 	{
691 		.name = "KVM_DEV_FLIC_CLEAR_IO_IRQ",
692 		.geterrno = EINVAL,
693 		.seterrno = EINVAL,
694 		.a = {
695 			.group = KVM_DEV_FLIC_CLEAR_IO_IRQ,
696 			.attr = 32,
697 		},
698 	},
699 	{
700 		.name = "KVM_DEV_FLIC_AISM",
701 		.geterrno = EINVAL,
702 		.seterrno = ENOTSUP,
703 		.a = {
704 			.group = KVM_DEV_FLIC_AISM,
705 			.addr = (u64)&uc_flic_asim,
706 		},
707 	},
708 	{
709 		.name = "KVM_DEV_FLIC_AIRQ_INJECT",
710 		.geterrno = EINVAL,
711 		.a = { .group = KVM_DEV_FLIC_AIRQ_INJECT, },
712 	},
713 	{
714 		.name = "KVM_DEV_FLIC_AISM_ALL",
715 		.geterrno = ENOTSUP,
716 		.seterrno = ENOTSUP,
717 		.a = {
718 			.group = KVM_DEV_FLIC_AISM_ALL,
719 			.addr = (u64)&uc_flic_asima,
720 			.attr = sizeof(uc_flic_asima),
721 		},
722 	},
723 	{
724 		.name = "KVM_DEV_FLIC_APF_ENABLE",
725 		.geterrno = EINVAL,
726 		.seterrno = EINVAL,
727 		.a = { .group = KVM_DEV_FLIC_APF_ENABLE, },
728 	},
729 	{
730 		.name = "KVM_DEV_FLIC_APF_DISABLE_WAIT",
731 		.geterrno = EINVAL,
732 		.seterrno = EINVAL,
733 		.a = { .group = KVM_DEV_FLIC_APF_DISABLE_WAIT, },
734 	},
735 };
736 
737 TEST_F(uc_kvm, uc_flic_attrs)
738 {
739 	struct kvm_create_device cd = { .type = KVM_DEV_TYPE_FLIC };
740 	struct kvm_device_attr attr;
741 	u64 value;
742 	int rc, i;
743 
744 	rc = ioctl(self->vm_fd, KVM_CREATE_DEVICE, &cd);
745 	ASSERT_EQ(0, rc) TH_LOG("create device failed with err %s (%i)",
746 				strerror(errno), errno);
747 
748 	for (i = 0; i < ARRAY_SIZE(uc_flic_attr_tests); i++) {
749 		TH_LOG("test %s", uc_flic_attr_tests[i].name);
750 		attr = (struct kvm_device_attr) {
751 			.group = uc_flic_attr_tests[i].a.group,
752 			.attr = uc_flic_attr_tests[i].a.attr,
753 			.addr = uc_flic_attr_tests[i].a.addr,
754 		};
755 		if (attr.addr == 0)
756 			attr.addr = (u64)&value;
757 
758 		rc = ioctl(cd.fd, KVM_HAS_DEVICE_ATTR, &attr);
759 		EXPECT_EQ(uc_flic_attr_tests[i].hasrc, !!rc)
760 			TH_LOG("expected dev attr missing %s",
761 			       uc_flic_attr_tests[i].name);
762 
763 		rc = ioctl(cd.fd, KVM_GET_DEVICE_ATTR, &attr);
764 		EXPECT_EQ(!!uc_flic_attr_tests[i].geterrno, !!rc)
765 			TH_LOG("get dev attr rc not expected on %s %s (%i)",
766 			       uc_flic_attr_tests[i].name,
767 			       strerror(errno), errno);
768 		if (uc_flic_attr_tests[i].geterrno)
769 			EXPECT_EQ(uc_flic_attr_tests[i].geterrno, errno)
770 				TH_LOG("get dev attr errno not expected on %s %s (%i)",
771 				       uc_flic_attr_tests[i].name,
772 				       strerror(errno), errno);
773 
774 		rc = ioctl(cd.fd, KVM_SET_DEVICE_ATTR, &attr);
775 		EXPECT_EQ(!!uc_flic_attr_tests[i].seterrno, !!rc)
776 			TH_LOG("set sev attr rc not expected on %s %s (%i)",
777 			       uc_flic_attr_tests[i].name,
778 			       strerror(errno), errno);
779 		if (uc_flic_attr_tests[i].seterrno)
780 			EXPECT_EQ(uc_flic_attr_tests[i].seterrno, errno)
781 				TH_LOG("set dev attr errno not expected on %s %s (%i)",
782 				       uc_flic_attr_tests[i].name,
783 				       strerror(errno), errno);
784 	}
785 
786 	close(cd.fd);
787 }
788 
789 TEST_F(uc_kvm, uc_set_gsi_routing)
790 {
791 	struct kvm_irq_routing *routing = kvm_gsi_routing_create();
792 	struct kvm_irq_routing_entry ue = {
793 		.type = KVM_IRQ_ROUTING_S390_ADAPTER,
794 		.gsi = 1,
795 		.u.adapter = (struct kvm_irq_routing_s390_adapter) {
796 			.ind_addr = 0,
797 		},
798 	};
799 	int rc;
800 
801 	routing->entries[0] = ue;
802 	routing->nr = 1;
803 	rc = ioctl(self->vm_fd, KVM_SET_GSI_ROUTING, routing);
804 	ASSERT_EQ(-1, rc) TH_LOG("err %s (%i)", strerror(errno), errno);
805 	ASSERT_EQ(EINVAL, errno) TH_LOG("err %s (%i)", strerror(errno), errno);
806 }
807 
808 TEST_HARNESS_MAIN
809