xref: /openbmc/linux/tools/testing/selftests/kvm/s390x/debug_test.c (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
1*642dbc03SIlya Leoshkevich // SPDX-License-Identifier: GPL-2.0-only
2*642dbc03SIlya Leoshkevich /* Test KVM debugging features. */
3*642dbc03SIlya Leoshkevich #include "kvm_util.h"
4*642dbc03SIlya Leoshkevich #include "test_util.h"
5*642dbc03SIlya Leoshkevich 
6*642dbc03SIlya Leoshkevich #include <linux/kvm.h>
7*642dbc03SIlya Leoshkevich 
8*642dbc03SIlya Leoshkevich #define __LC_SVC_NEW_PSW 0x1c0
9*642dbc03SIlya Leoshkevich #define __LC_PGM_NEW_PSW 0x1d0
10*642dbc03SIlya Leoshkevich #define ICPT_INSTRUCTION 0x04
11*642dbc03SIlya Leoshkevich #define IPA0_DIAG 0x8300
12*642dbc03SIlya Leoshkevich #define PGM_SPECIFICATION 0x06
13*642dbc03SIlya Leoshkevich 
14*642dbc03SIlya Leoshkevich /* Common code for testing single-stepping interruptions. */
15*642dbc03SIlya Leoshkevich extern char int_handler[];
16*642dbc03SIlya Leoshkevich asm("int_handler:\n"
17*642dbc03SIlya Leoshkevich     "j .\n");
18*642dbc03SIlya Leoshkevich 
test_step_int_1(struct kvm_vcpu ** vcpu,void * guest_code,size_t new_psw_off,uint64_t * new_psw)19*642dbc03SIlya Leoshkevich static struct kvm_vm *test_step_int_1(struct kvm_vcpu **vcpu, void *guest_code,
20*642dbc03SIlya Leoshkevich 				      size_t new_psw_off, uint64_t *new_psw)
21*642dbc03SIlya Leoshkevich {
22*642dbc03SIlya Leoshkevich 	struct kvm_guest_debug debug = {};
23*642dbc03SIlya Leoshkevich 	struct kvm_regs regs;
24*642dbc03SIlya Leoshkevich 	struct kvm_vm *vm;
25*642dbc03SIlya Leoshkevich 	char *lowcore;
26*642dbc03SIlya Leoshkevich 
27*642dbc03SIlya Leoshkevich 	vm = vm_create_with_one_vcpu(vcpu, guest_code);
28*642dbc03SIlya Leoshkevich 	lowcore = addr_gpa2hva(vm, 0);
29*642dbc03SIlya Leoshkevich 	new_psw[0] = (*vcpu)->run->psw_mask;
30*642dbc03SIlya Leoshkevich 	new_psw[1] = (uint64_t)int_handler;
31*642dbc03SIlya Leoshkevich 	memcpy(lowcore + new_psw_off, new_psw, 16);
32*642dbc03SIlya Leoshkevich 	vcpu_regs_get(*vcpu, &regs);
33*642dbc03SIlya Leoshkevich 	regs.gprs[2] = -1;
34*642dbc03SIlya Leoshkevich 	vcpu_regs_set(*vcpu, &regs);
35*642dbc03SIlya Leoshkevich 	debug.control = KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_SINGLESTEP;
36*642dbc03SIlya Leoshkevich 	vcpu_guest_debug_set(*vcpu, &debug);
37*642dbc03SIlya Leoshkevich 	vcpu_run(*vcpu);
38*642dbc03SIlya Leoshkevich 
39*642dbc03SIlya Leoshkevich 	return vm;
40*642dbc03SIlya Leoshkevich }
41*642dbc03SIlya Leoshkevich 
test_step_int(void * guest_code,size_t new_psw_off)42*642dbc03SIlya Leoshkevich static void test_step_int(void *guest_code, size_t new_psw_off)
43*642dbc03SIlya Leoshkevich {
44*642dbc03SIlya Leoshkevich 	struct kvm_vcpu *vcpu;
45*642dbc03SIlya Leoshkevich 	uint64_t new_psw[2];
46*642dbc03SIlya Leoshkevich 	struct kvm_vm *vm;
47*642dbc03SIlya Leoshkevich 
48*642dbc03SIlya Leoshkevich 	vm = test_step_int_1(&vcpu, guest_code, new_psw_off, new_psw);
49*642dbc03SIlya Leoshkevich 	TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_DEBUG);
50*642dbc03SIlya Leoshkevich 	TEST_ASSERT_EQ(vcpu->run->psw_mask, new_psw[0]);
51*642dbc03SIlya Leoshkevich 	TEST_ASSERT_EQ(vcpu->run->psw_addr, new_psw[1]);
52*642dbc03SIlya Leoshkevich 	kvm_vm_free(vm);
53*642dbc03SIlya Leoshkevich }
54*642dbc03SIlya Leoshkevich 
55*642dbc03SIlya Leoshkevich /* Test single-stepping "boring" program interruptions. */
56*642dbc03SIlya Leoshkevich extern char test_step_pgm_guest_code[];
57*642dbc03SIlya Leoshkevich asm("test_step_pgm_guest_code:\n"
58*642dbc03SIlya Leoshkevich     ".insn rr,0x1d00,%r1,%r0 /* dr %r1,%r0 */\n"
59*642dbc03SIlya Leoshkevich     "j .\n");
60*642dbc03SIlya Leoshkevich 
test_step_pgm(void)61*642dbc03SIlya Leoshkevich static void test_step_pgm(void)
62*642dbc03SIlya Leoshkevich {
63*642dbc03SIlya Leoshkevich 	test_step_int(test_step_pgm_guest_code, __LC_PGM_NEW_PSW);
64*642dbc03SIlya Leoshkevich }
65*642dbc03SIlya Leoshkevich 
66*642dbc03SIlya Leoshkevich /*
67*642dbc03SIlya Leoshkevich  * Test single-stepping program interruptions caused by DIAG.
68*642dbc03SIlya Leoshkevich  * Userspace emulation must not interfere with single-stepping.
69*642dbc03SIlya Leoshkevich  */
70*642dbc03SIlya Leoshkevich extern char test_step_pgm_diag_guest_code[];
71*642dbc03SIlya Leoshkevich asm("test_step_pgm_diag_guest_code:\n"
72*642dbc03SIlya Leoshkevich     "diag %r0,%r0,0\n"
73*642dbc03SIlya Leoshkevich     "j .\n");
74*642dbc03SIlya Leoshkevich 
test_step_pgm_diag(void)75*642dbc03SIlya Leoshkevich static void test_step_pgm_diag(void)
76*642dbc03SIlya Leoshkevich {
77*642dbc03SIlya Leoshkevich 	struct kvm_s390_irq irq = {
78*642dbc03SIlya Leoshkevich 		.type = KVM_S390_PROGRAM_INT,
79*642dbc03SIlya Leoshkevich 		.u.pgm.code = PGM_SPECIFICATION,
80*642dbc03SIlya Leoshkevich 	};
81*642dbc03SIlya Leoshkevich 	struct kvm_vcpu *vcpu;
82*642dbc03SIlya Leoshkevich 	uint64_t new_psw[2];
83*642dbc03SIlya Leoshkevich 	struct kvm_vm *vm;
84*642dbc03SIlya Leoshkevich 
85*642dbc03SIlya Leoshkevich 	vm = test_step_int_1(&vcpu, test_step_pgm_diag_guest_code,
86*642dbc03SIlya Leoshkevich 			     __LC_PGM_NEW_PSW, new_psw);
87*642dbc03SIlya Leoshkevich 	TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_S390_SIEIC);
88*642dbc03SIlya Leoshkevich 	TEST_ASSERT_EQ(vcpu->run->s390_sieic.icptcode, ICPT_INSTRUCTION);
89*642dbc03SIlya Leoshkevich 	TEST_ASSERT_EQ(vcpu->run->s390_sieic.ipa & 0xff00, IPA0_DIAG);
90*642dbc03SIlya Leoshkevich 	vcpu_ioctl(vcpu, KVM_S390_IRQ, &irq);
91*642dbc03SIlya Leoshkevich 	vcpu_run(vcpu);
92*642dbc03SIlya Leoshkevich 	TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_DEBUG);
93*642dbc03SIlya Leoshkevich 	TEST_ASSERT_EQ(vcpu->run->psw_mask, new_psw[0]);
94*642dbc03SIlya Leoshkevich 	TEST_ASSERT_EQ(vcpu->run->psw_addr, new_psw[1]);
95*642dbc03SIlya Leoshkevich 	kvm_vm_free(vm);
96*642dbc03SIlya Leoshkevich }
97*642dbc03SIlya Leoshkevich 
98*642dbc03SIlya Leoshkevich /*
99*642dbc03SIlya Leoshkevich  * Test single-stepping program interruptions caused by ISKE.
100*642dbc03SIlya Leoshkevich  * CPUSTAT_KSS handling must not interfere with single-stepping.
101*642dbc03SIlya Leoshkevich  */
102*642dbc03SIlya Leoshkevich extern char test_step_pgm_iske_guest_code[];
103*642dbc03SIlya Leoshkevich asm("test_step_pgm_iske_guest_code:\n"
104*642dbc03SIlya Leoshkevich     "iske %r2,%r2\n"
105*642dbc03SIlya Leoshkevich     "j .\n");
106*642dbc03SIlya Leoshkevich 
test_step_pgm_iske(void)107*642dbc03SIlya Leoshkevich static void test_step_pgm_iske(void)
108*642dbc03SIlya Leoshkevich {
109*642dbc03SIlya Leoshkevich 	test_step_int(test_step_pgm_iske_guest_code, __LC_PGM_NEW_PSW);
110*642dbc03SIlya Leoshkevich }
111*642dbc03SIlya Leoshkevich 
112*642dbc03SIlya Leoshkevich /*
113*642dbc03SIlya Leoshkevich  * Test single-stepping program interruptions caused by LCTL.
114*642dbc03SIlya Leoshkevich  * KVM emulation must not interfere with single-stepping.
115*642dbc03SIlya Leoshkevich  */
116*642dbc03SIlya Leoshkevich extern char test_step_pgm_lctl_guest_code[];
117*642dbc03SIlya Leoshkevich asm("test_step_pgm_lctl_guest_code:\n"
118*642dbc03SIlya Leoshkevich     "lctl %c0,%c0,1\n"
119*642dbc03SIlya Leoshkevich     "j .\n");
120*642dbc03SIlya Leoshkevich 
test_step_pgm_lctl(void)121*642dbc03SIlya Leoshkevich static void test_step_pgm_lctl(void)
122*642dbc03SIlya Leoshkevich {
123*642dbc03SIlya Leoshkevich 	test_step_int(test_step_pgm_lctl_guest_code, __LC_PGM_NEW_PSW);
124*642dbc03SIlya Leoshkevich }
125*642dbc03SIlya Leoshkevich 
126*642dbc03SIlya Leoshkevich /* Test single-stepping supervisor-call interruptions. */
127*642dbc03SIlya Leoshkevich extern char test_step_svc_guest_code[];
128*642dbc03SIlya Leoshkevich asm("test_step_svc_guest_code:\n"
129*642dbc03SIlya Leoshkevich     "svc 0\n"
130*642dbc03SIlya Leoshkevich     "j .\n");
131*642dbc03SIlya Leoshkevich 
test_step_svc(void)132*642dbc03SIlya Leoshkevich static void test_step_svc(void)
133*642dbc03SIlya Leoshkevich {
134*642dbc03SIlya Leoshkevich 	test_step_int(test_step_svc_guest_code, __LC_SVC_NEW_PSW);
135*642dbc03SIlya Leoshkevich }
136*642dbc03SIlya Leoshkevich 
137*642dbc03SIlya Leoshkevich /* Run all tests above. */
138*642dbc03SIlya Leoshkevich static struct testdef {
139*642dbc03SIlya Leoshkevich 	const char *name;
140*642dbc03SIlya Leoshkevich 	void (*test)(void);
141*642dbc03SIlya Leoshkevich } testlist[] = {
142*642dbc03SIlya Leoshkevich 	{ "single-step pgm", test_step_pgm },
143*642dbc03SIlya Leoshkevich 	{ "single-step pgm caused by diag", test_step_pgm_diag },
144*642dbc03SIlya Leoshkevich 	{ "single-step pgm caused by iske", test_step_pgm_iske },
145*642dbc03SIlya Leoshkevich 	{ "single-step pgm caused by lctl", test_step_pgm_lctl },
146*642dbc03SIlya Leoshkevich 	{ "single-step svc", test_step_svc },
147*642dbc03SIlya Leoshkevich };
148*642dbc03SIlya Leoshkevich 
main(int argc,char * argv[])149*642dbc03SIlya Leoshkevich int main(int argc, char *argv[])
150*642dbc03SIlya Leoshkevich {
151*642dbc03SIlya Leoshkevich 	int idx;
152*642dbc03SIlya Leoshkevich 
153*642dbc03SIlya Leoshkevich 	ksft_print_header();
154*642dbc03SIlya Leoshkevich 	ksft_set_plan(ARRAY_SIZE(testlist));
155*642dbc03SIlya Leoshkevich 	for (idx = 0; idx < ARRAY_SIZE(testlist); idx++) {
156*642dbc03SIlya Leoshkevich 		testlist[idx].test();
157*642dbc03SIlya Leoshkevich 		ksft_test_result_pass("%s\n", testlist[idx].name);
158*642dbc03SIlya Leoshkevich 	}
159*642dbc03SIlya Leoshkevich 	ksft_finished();
160*642dbc03SIlya Leoshkevich }
161