xref: /linux/arch/s390/kvm/guestdbg.c (revision 8934827db5403eae57d4537114a9ff88b0a8460f)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * kvm guest debug support
4  *
5  * Copyright IBM Corp. 2014
6  *
7  *    Author(s): David Hildenbrand <dahi@linux.vnet.ibm.com>
8  */
9 #include <linux/kvm_host.h>
10 #include <linux/errno.h>
11 #include "kvm-s390.h"
12 #include "gaccess.h"
13 
14 /*
15  * Extends the address range given by *start and *stop to include the address
16  * range starting with estart and the length len. Takes care of overflowing
17  * intervals and tries to minimize the overall interval size.
18  */
extend_address_range(u64 * start,u64 * stop,u64 estart,int len)19 static void extend_address_range(u64 *start, u64 *stop, u64 estart, int len)
20 {
21 	u64 estop;
22 
23 	if (len > 0)
24 		len--;
25 	else
26 		len = 0;
27 
28 	estop = estart + len;
29 
30 	/* 0-0 range represents "not set" */
31 	if ((*start == 0) && (*stop == 0)) {
32 		*start = estart;
33 		*stop = estop;
34 	} else if (*start <= *stop) {
35 		/* increase the existing range */
36 		if (estart < *start)
37 			*start = estart;
38 		if (estop > *stop)
39 			*stop = estop;
40 	} else {
41 		/* "overflowing" interval, whereby *stop > *start */
42 		if (estart <= *stop) {
43 			if (estop > *stop)
44 				*stop = estop;
45 		} else if (estop > *start) {
46 			if (estart < *start)
47 				*start = estart;
48 		}
49 		/* minimize the range */
50 		else if ((estop - *stop) < (*start - estart))
51 			*stop = estop;
52 		else
53 			*start = estart;
54 	}
55 }
56 
57 #define MAX_INST_SIZE 6
58 
enable_all_hw_bp(struct kvm_vcpu * vcpu)59 static void enable_all_hw_bp(struct kvm_vcpu *vcpu)
60 {
61 	unsigned long start, len;
62 	u64 *cr9 = &vcpu->arch.sie_block->gcr[9];
63 	u64 *cr10 = &vcpu->arch.sie_block->gcr[10];
64 	u64 *cr11 = &vcpu->arch.sie_block->gcr[11];
65 	int i;
66 
67 	if (vcpu->arch.guestdbg.nr_hw_bp <= 0 ||
68 	    vcpu->arch.guestdbg.hw_bp_info == NULL)
69 		return;
70 
71 	/*
72 	 * If the guest is not interested in branching events, we can safely
73 	 * limit them to the PER address range.
74 	 */
75 	if (!(*cr9 & PER_EVENT_BRANCH))
76 		*cr9 |= PER_CONTROL_BRANCH_ADDRESS;
77 	*cr9 |= PER_EVENT_IFETCH | PER_EVENT_BRANCH;
78 
79 	for (i = 0; i < vcpu->arch.guestdbg.nr_hw_bp; i++) {
80 		start = vcpu->arch.guestdbg.hw_bp_info[i].addr;
81 		len = vcpu->arch.guestdbg.hw_bp_info[i].len;
82 
83 		/*
84 		 * The instruction in front of the desired bp has to
85 		 * report instruction-fetching events
86 		 */
87 		if (start < MAX_INST_SIZE) {
88 			len += start;
89 			start = 0;
90 		} else {
91 			start -= MAX_INST_SIZE;
92 			len += MAX_INST_SIZE;
93 		}
94 
95 		extend_address_range(cr10, cr11, start, len);
96 	}
97 }
98 
enable_all_hw_wp(struct kvm_vcpu * vcpu)99 static void enable_all_hw_wp(struct kvm_vcpu *vcpu)
100 {
101 	unsigned long start, len;
102 	u64 *cr9 = &vcpu->arch.sie_block->gcr[9];
103 	u64 *cr10 = &vcpu->arch.sie_block->gcr[10];
104 	u64 *cr11 = &vcpu->arch.sie_block->gcr[11];
105 	int i;
106 
107 	if (vcpu->arch.guestdbg.nr_hw_wp <= 0 ||
108 	    vcpu->arch.guestdbg.hw_wp_info == NULL)
109 		return;
110 
111 	/* if host uses storage alternation for special address
112 	 * spaces, enable all events and give all to the guest */
113 	if (*cr9 & PER_EVENT_STORE && *cr9 & PER_CONTROL_ALTERATION) {
114 		*cr9 &= ~PER_CONTROL_ALTERATION;
115 		*cr10 = 0;
116 		*cr11 = -1UL;
117 	} else {
118 		*cr9 &= ~PER_CONTROL_ALTERATION;
119 		*cr9 |= PER_EVENT_STORE;
120 
121 		for (i = 0; i < vcpu->arch.guestdbg.nr_hw_wp; i++) {
122 			start = vcpu->arch.guestdbg.hw_wp_info[i].addr;
123 			len = vcpu->arch.guestdbg.hw_wp_info[i].len;
124 
125 			extend_address_range(cr10, cr11, start, len);
126 		}
127 	}
128 }
129 
kvm_s390_backup_guest_per_regs(struct kvm_vcpu * vcpu)130 void kvm_s390_backup_guest_per_regs(struct kvm_vcpu *vcpu)
131 {
132 	vcpu->arch.guestdbg.cr0 = vcpu->arch.sie_block->gcr[0];
133 	vcpu->arch.guestdbg.cr9 = vcpu->arch.sie_block->gcr[9];
134 	vcpu->arch.guestdbg.cr10 = vcpu->arch.sie_block->gcr[10];
135 	vcpu->arch.guestdbg.cr11 = vcpu->arch.sie_block->gcr[11];
136 }
137 
kvm_s390_restore_guest_per_regs(struct kvm_vcpu * vcpu)138 void kvm_s390_restore_guest_per_regs(struct kvm_vcpu *vcpu)
139 {
140 	vcpu->arch.sie_block->gcr[0] = vcpu->arch.guestdbg.cr0;
141 	vcpu->arch.sie_block->gcr[9] = vcpu->arch.guestdbg.cr9;
142 	vcpu->arch.sie_block->gcr[10] = vcpu->arch.guestdbg.cr10;
143 	vcpu->arch.sie_block->gcr[11] = vcpu->arch.guestdbg.cr11;
144 }
145 
kvm_s390_patch_guest_per_regs(struct kvm_vcpu * vcpu)146 void kvm_s390_patch_guest_per_regs(struct kvm_vcpu *vcpu)
147 {
148 	/*
149 	 * TODO: if guest psw has per enabled, otherwise 0s!
150 	 * This reduces the amount of reported events.
151 	 * Need to intercept all psw changes!
152 	 */
153 
154 	if (guestdbg_sstep_enabled(vcpu)) {
155 		/* disable timer (clock-comparator) interrupts */
156 		vcpu->arch.sie_block->gcr[0] &= ~CR0_CLOCK_COMPARATOR_SUBMASK;
157 		vcpu->arch.sie_block->gcr[9] |= PER_EVENT_IFETCH;
158 		vcpu->arch.sie_block->gcr[10] = 0;
159 		vcpu->arch.sie_block->gcr[11] = -1UL;
160 	}
161 
162 	if (guestdbg_hw_bp_enabled(vcpu)) {
163 		enable_all_hw_bp(vcpu);
164 		enable_all_hw_wp(vcpu);
165 	}
166 
167 	/* TODO: Instruction-fetching-nullification not allowed for now */
168 	if (vcpu->arch.sie_block->gcr[9] & PER_EVENT_NULLIFICATION)
169 		vcpu->arch.sie_block->gcr[9] &= ~PER_EVENT_NULLIFICATION;
170 }
171 
172 #define MAX_WP_SIZE 100
173 
__import_wp_info(struct kvm_vcpu * vcpu,struct kvm_hw_breakpoint * bp_data,struct kvm_hw_wp_info_arch * wp_info)174 static int __import_wp_info(struct kvm_vcpu *vcpu,
175 			    struct kvm_hw_breakpoint *bp_data,
176 			    struct kvm_hw_wp_info_arch *wp_info)
177 {
178 	int ret = 0;
179 	wp_info->len = bp_data->len;
180 	wp_info->addr = bp_data->addr;
181 	wp_info->phys_addr = bp_data->phys_addr;
182 	wp_info->old_data = NULL;
183 
184 	if (wp_info->len < 0 || wp_info->len > MAX_WP_SIZE)
185 		return -EINVAL;
186 
187 	wp_info->old_data = kmalloc(bp_data->len, GFP_KERNEL_ACCOUNT);
188 	if (!wp_info->old_data)
189 		return -ENOMEM;
190 	/* try to backup the original value */
191 	ret = read_guest_abs(vcpu, wp_info->phys_addr, wp_info->old_data,
192 			     wp_info->len);
193 	if (ret) {
194 		kfree(wp_info->old_data);
195 		wp_info->old_data = NULL;
196 	}
197 
198 	return ret;
199 }
200 
201 #define MAX_BP_COUNT 50
202 
kvm_s390_import_bp_data(struct kvm_vcpu * vcpu,struct kvm_guest_debug * dbg)203 int kvm_s390_import_bp_data(struct kvm_vcpu *vcpu,
204 			    struct kvm_guest_debug *dbg)
205 {
206 	int ret = 0, nr_wp = 0, nr_bp = 0, i;
207 	struct kvm_hw_breakpoint *bp_data = NULL;
208 	struct kvm_hw_wp_info_arch *wp_info = NULL;
209 	struct kvm_hw_bp_info_arch *bp_info = NULL;
210 
211 	if (dbg->arch.nr_hw_bp <= 0 || !dbg->arch.hw_bp)
212 		return 0;
213 	else if (dbg->arch.nr_hw_bp > MAX_BP_COUNT)
214 		return -EINVAL;
215 
216 	bp_data = memdup_array_user(dbg->arch.hw_bp, dbg->arch.nr_hw_bp,
217 				    sizeof(*bp_data));
218 	if (IS_ERR(bp_data))
219 		return PTR_ERR(bp_data);
220 
221 	for (i = 0; i < dbg->arch.nr_hw_bp; i++) {
222 		switch (bp_data[i].type) {
223 		case KVM_HW_WP_WRITE:
224 			nr_wp++;
225 			break;
226 		case KVM_HW_BP:
227 			nr_bp++;
228 			break;
229 		default:
230 			break;
231 		}
232 	}
233 
234 	if (nr_wp > 0) {
235 		wp_info = kmalloc_objs(*wp_info, nr_wp, GFP_KERNEL_ACCOUNT);
236 		if (!wp_info) {
237 			ret = -ENOMEM;
238 			goto error;
239 		}
240 	}
241 	if (nr_bp > 0) {
242 		bp_info = kmalloc_objs(*bp_info, nr_bp, GFP_KERNEL_ACCOUNT);
243 		if (!bp_info) {
244 			ret = -ENOMEM;
245 			goto error;
246 		}
247 	}
248 
249 	for (nr_wp = 0, nr_bp = 0, i = 0; i < dbg->arch.nr_hw_bp; i++) {
250 		switch (bp_data[i].type) {
251 		case KVM_HW_WP_WRITE:
252 			ret = __import_wp_info(vcpu, &bp_data[i],
253 					       &wp_info[nr_wp]);
254 			if (ret)
255 				goto error;
256 			nr_wp++;
257 			break;
258 		case KVM_HW_BP:
259 			bp_info[nr_bp].len = bp_data[i].len;
260 			bp_info[nr_bp].addr = bp_data[i].addr;
261 			nr_bp++;
262 			break;
263 		}
264 	}
265 
266 	vcpu->arch.guestdbg.nr_hw_bp = nr_bp;
267 	vcpu->arch.guestdbg.hw_bp_info = bp_info;
268 	vcpu->arch.guestdbg.nr_hw_wp = nr_wp;
269 	vcpu->arch.guestdbg.hw_wp_info = wp_info;
270 	return 0;
271 error:
272 	kfree(bp_data);
273 	kfree(wp_info);
274 	kfree(bp_info);
275 	return ret;
276 }
277 
kvm_s390_clear_bp_data(struct kvm_vcpu * vcpu)278 void kvm_s390_clear_bp_data(struct kvm_vcpu *vcpu)
279 {
280 	int i;
281 	struct kvm_hw_wp_info_arch *hw_wp_info = NULL;
282 
283 	for (i = 0; i < vcpu->arch.guestdbg.nr_hw_wp; i++) {
284 		hw_wp_info = &vcpu->arch.guestdbg.hw_wp_info[i];
285 		kfree(hw_wp_info->old_data);
286 		hw_wp_info->old_data = NULL;
287 	}
288 	kfree(vcpu->arch.guestdbg.hw_wp_info);
289 	vcpu->arch.guestdbg.hw_wp_info = NULL;
290 
291 	kfree(vcpu->arch.guestdbg.hw_bp_info);
292 	vcpu->arch.guestdbg.hw_bp_info = NULL;
293 
294 	vcpu->arch.guestdbg.nr_hw_wp = 0;
295 	vcpu->arch.guestdbg.nr_hw_bp = 0;
296 }
297 
in_addr_range(u64 addr,u64 a,u64 b)298 static inline int in_addr_range(u64 addr, u64 a, u64 b)
299 {
300 	if (a <= b)
301 		return (addr >= a) && (addr <= b);
302 	else
303 		/* "overflowing" interval */
304 		return (addr >= a) || (addr <= b);
305 }
306 
307 #define end_of_range(bp_info) (bp_info->addr + bp_info->len - 1)
308 
find_hw_bp(struct kvm_vcpu * vcpu,unsigned long addr)309 static struct kvm_hw_bp_info_arch *find_hw_bp(struct kvm_vcpu *vcpu,
310 					      unsigned long addr)
311 {
312 	struct kvm_hw_bp_info_arch *bp_info = vcpu->arch.guestdbg.hw_bp_info;
313 	int i;
314 
315 	if (vcpu->arch.guestdbg.nr_hw_bp == 0)
316 		return NULL;
317 
318 	for (i = 0; i < vcpu->arch.guestdbg.nr_hw_bp; i++) {
319 		/* addr is directly the start or in the range of a bp */
320 		if (addr == bp_info->addr)
321 			goto found;
322 		if (bp_info->len > 0 &&
323 		    in_addr_range(addr, bp_info->addr, end_of_range(bp_info)))
324 			goto found;
325 
326 		bp_info++;
327 	}
328 
329 	return NULL;
330 found:
331 	return bp_info;
332 }
333 
any_wp_changed(struct kvm_vcpu * vcpu)334 static struct kvm_hw_wp_info_arch *any_wp_changed(struct kvm_vcpu *vcpu)
335 {
336 	int i;
337 	struct kvm_hw_wp_info_arch *wp_info = NULL;
338 	void *temp = NULL;
339 
340 	if (vcpu->arch.guestdbg.nr_hw_wp == 0)
341 		return NULL;
342 
343 	for (i = 0; i < vcpu->arch.guestdbg.nr_hw_wp; i++) {
344 		wp_info = &vcpu->arch.guestdbg.hw_wp_info[i];
345 		if (!wp_info || !wp_info->old_data || wp_info->len <= 0)
346 			continue;
347 
348 		temp = kmalloc(wp_info->len, GFP_KERNEL_ACCOUNT);
349 		if (!temp)
350 			continue;
351 
352 		/* refetch the wp data and compare it to the old value */
353 		if (!read_guest_abs(vcpu, wp_info->phys_addr, temp,
354 				    wp_info->len)) {
355 			if (memcmp(temp, wp_info->old_data, wp_info->len)) {
356 				kfree(temp);
357 				return wp_info;
358 			}
359 		}
360 		kfree(temp);
361 		temp = NULL;
362 	}
363 
364 	return NULL;
365 }
366 
kvm_s390_prepare_debug_exit(struct kvm_vcpu * vcpu)367 void kvm_s390_prepare_debug_exit(struct kvm_vcpu *vcpu)
368 {
369 	vcpu->run->exit_reason = KVM_EXIT_DEBUG;
370 	vcpu->guest_debug &= ~KVM_GUESTDBG_EXIT_PENDING;
371 }
372 
373 #define PER_CODE_MASK		(PER_EVENT_MASK >> 24)
374 #define PER_CODE_BRANCH		(PER_EVENT_BRANCH >> 24)
375 #define PER_CODE_IFETCH		(PER_EVENT_IFETCH >> 24)
376 #define PER_CODE_STORE		(PER_EVENT_STORE >> 24)
377 #define PER_CODE_STORE_REAL	(PER_EVENT_STORE_REAL >> 24)
378 
379 #define per_bp_event(code) \
380 			(code & (PER_CODE_IFETCH | PER_CODE_BRANCH))
381 #define per_write_wp_event(code) \
382 			(code & (PER_CODE_STORE | PER_CODE_STORE_REAL))
383 
debug_exit_required(struct kvm_vcpu * vcpu,u8 perc,unsigned long peraddr)384 static int debug_exit_required(struct kvm_vcpu *vcpu, u8 perc,
385 			       unsigned long peraddr)
386 {
387 	struct kvm_debug_exit_arch *debug_exit = &vcpu->run->debug.arch;
388 	struct kvm_hw_wp_info_arch *wp_info = NULL;
389 	struct kvm_hw_bp_info_arch *bp_info = NULL;
390 	unsigned long addr = vcpu->arch.sie_block->gpsw.addr;
391 
392 	if (guestdbg_hw_bp_enabled(vcpu)) {
393 		if (per_write_wp_event(perc) &&
394 		    vcpu->arch.guestdbg.nr_hw_wp > 0) {
395 			wp_info = any_wp_changed(vcpu);
396 			if (wp_info) {
397 				debug_exit->addr = wp_info->addr;
398 				debug_exit->type = KVM_HW_WP_WRITE;
399 				goto exit_required;
400 			}
401 		}
402 		if (per_bp_event(perc) &&
403 			 vcpu->arch.guestdbg.nr_hw_bp > 0) {
404 			bp_info = find_hw_bp(vcpu, addr);
405 			/* remove duplicate events if PC==PER address */
406 			if (bp_info && (addr != peraddr)) {
407 				debug_exit->addr = addr;
408 				debug_exit->type = KVM_HW_BP;
409 				vcpu->arch.guestdbg.last_bp = addr;
410 				goto exit_required;
411 			}
412 			/* breakpoint missed */
413 			bp_info = find_hw_bp(vcpu, peraddr);
414 			if (bp_info && vcpu->arch.guestdbg.last_bp != peraddr) {
415 				debug_exit->addr = peraddr;
416 				debug_exit->type = KVM_HW_BP;
417 				goto exit_required;
418 			}
419 		}
420 	}
421 	if (guestdbg_sstep_enabled(vcpu) && per_bp_event(perc)) {
422 		debug_exit->addr = addr;
423 		debug_exit->type = KVM_SINGLESTEP;
424 		goto exit_required;
425 	}
426 
427 	return 0;
428 exit_required:
429 	return 1;
430 }
431 
per_fetched_addr(struct kvm_vcpu * vcpu,unsigned long * addr)432 static int per_fetched_addr(struct kvm_vcpu *vcpu, unsigned long *addr)
433 {
434 	u8 exec_ilen = 0;
435 	u16 opcode[3];
436 	int rc;
437 
438 	if (vcpu->arch.sie_block->icptcode == ICPT_PROGI) {
439 		/* PER address references the fetched or the execute instr */
440 		*addr = vcpu->arch.sie_block->peraddr;
441 		/*
442 		 * Manually detect if we have an EXECUTE instruction. As
443 		 * instructions are always 2 byte aligned we can read the
444 		 * first two bytes unconditionally
445 		 */
446 		rc = read_guest_instr(vcpu, *addr, &opcode, 2);
447 		if (rc)
448 			return rc;
449 		if (opcode[0] >> 8 == 0x44)
450 			exec_ilen = 4;
451 		if ((opcode[0] & 0xff0f) == 0xc600)
452 			exec_ilen = 6;
453 	} else {
454 		/* instr was suppressed, calculate the responsible instr */
455 		*addr = __rewind_psw(vcpu->arch.sie_block->gpsw,
456 				     kvm_s390_get_ilen(vcpu));
457 		if (vcpu->arch.sie_block->icptstatus & 0x01) {
458 			exec_ilen = (vcpu->arch.sie_block->icptstatus & 0x60) >> 4;
459 			if (!exec_ilen)
460 				exec_ilen = 4;
461 		}
462 	}
463 
464 	if (exec_ilen) {
465 		/* read the complete EXECUTE instr to detect the fetched addr */
466 		rc = read_guest_instr(vcpu, *addr, &opcode, exec_ilen);
467 		if (rc)
468 			return rc;
469 		if (exec_ilen == 6) {
470 			/* EXECUTE RELATIVE LONG - RIL-b format */
471 			s32 rl = *((s32 *) (opcode + 1));
472 
473 			/* rl is a _signed_ 32 bit value specifying halfwords */
474 			*addr += (u64)(s64) rl * 2;
475 		} else {
476 			/* EXECUTE - RX-a format */
477 			u32 base = (opcode[1] & 0xf000) >> 12;
478 			u32 disp = opcode[1] & 0x0fff;
479 			u32 index = opcode[0] & 0x000f;
480 
481 			*addr = base ? vcpu->run->s.regs.gprs[base] : 0;
482 			*addr += index ? vcpu->run->s.regs.gprs[index] : 0;
483 			*addr += disp;
484 		}
485 		*addr = kvm_s390_logical_to_effective(vcpu, *addr);
486 	}
487 	return 0;
488 }
489 
490 #define guest_per_enabled(vcpu) \
491 			     (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PER)
492 
kvm_s390_handle_per_ifetch_icpt(struct kvm_vcpu * vcpu)493 int kvm_s390_handle_per_ifetch_icpt(struct kvm_vcpu *vcpu)
494 {
495 	const u64 cr10 = vcpu->arch.sie_block->gcr[10];
496 	const u64 cr11 = vcpu->arch.sie_block->gcr[11];
497 	const u8 ilen = kvm_s390_get_ilen(vcpu);
498 	struct kvm_s390_pgm_info pgm_info = {
499 		.code = PGM_PER,
500 		.per_code = PER_CODE_IFETCH,
501 		.per_address = __rewind_psw(vcpu->arch.sie_block->gpsw, ilen),
502 	};
503 	unsigned long fetched_addr;
504 	int rc;
505 
506 	/*
507 	 * The PSW points to the next instruction, therefore the intercepted
508 	 * instruction generated a PER i-fetch event. PER address therefore
509 	 * points at the previous PSW address (could be an EXECUTE function).
510 	 */
511 	if (!guestdbg_enabled(vcpu))
512 		return kvm_s390_inject_prog_irq(vcpu, &pgm_info);
513 
514 	if (debug_exit_required(vcpu, pgm_info.per_code, pgm_info.per_address))
515 		vcpu->guest_debug |= KVM_GUESTDBG_EXIT_PENDING;
516 
517 	if (!guest_per_enabled(vcpu) ||
518 	    !(vcpu->arch.sie_block->gcr[9] & PER_EVENT_IFETCH))
519 		return 0;
520 
521 	rc = per_fetched_addr(vcpu, &fetched_addr);
522 	if (rc < 0)
523 		return rc;
524 	if (rc)
525 		/* instruction-fetching exceptions */
526 		return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
527 
528 	if (in_addr_range(fetched_addr, cr10, cr11))
529 		return kvm_s390_inject_prog_irq(vcpu, &pgm_info);
530 	return 0;
531 }
532 
filter_guest_per_event(struct kvm_vcpu * vcpu)533 static int filter_guest_per_event(struct kvm_vcpu *vcpu)
534 {
535 	const u8 perc = vcpu->arch.sie_block->perc;
536 	u64 addr = vcpu->arch.sie_block->gpsw.addr;
537 	u64 cr9 = vcpu->arch.sie_block->gcr[9];
538 	u64 cr10 = vcpu->arch.sie_block->gcr[10];
539 	u64 cr11 = vcpu->arch.sie_block->gcr[11];
540 	/* filter all events, demanded by the guest */
541 	u8 guest_perc = perc & (cr9 >> 24) & PER_CODE_MASK;
542 	unsigned long fetched_addr;
543 	int rc;
544 
545 	if (!guest_per_enabled(vcpu))
546 		guest_perc = 0;
547 
548 	/* filter "successful-branching" events */
549 	if (guest_perc & PER_CODE_BRANCH &&
550 	    cr9 & PER_CONTROL_BRANCH_ADDRESS &&
551 	    !in_addr_range(addr, cr10, cr11))
552 		guest_perc &= ~PER_CODE_BRANCH;
553 
554 	/* filter "instruction-fetching" events */
555 	if (guest_perc & PER_CODE_IFETCH) {
556 		rc = per_fetched_addr(vcpu, &fetched_addr);
557 		if (rc < 0)
558 			return rc;
559 		/*
560 		 * Don't inject an irq on exceptions. This would make handling
561 		 * on icpt code 8 very complex (as PSW was already rewound).
562 		 */
563 		if (rc || !in_addr_range(fetched_addr, cr10, cr11))
564 			guest_perc &= ~PER_CODE_IFETCH;
565 	}
566 
567 	/* All other PER events will be given to the guest */
568 	/* TODO: Check altered address/address space */
569 
570 	vcpu->arch.sie_block->perc = guest_perc;
571 
572 	if (!guest_perc)
573 		vcpu->arch.sie_block->iprcc &= ~PGM_PER;
574 	return 0;
575 }
576 
577 #define pssec(vcpu) (vcpu->arch.sie_block->gcr[1] & _ASCE_SPACE_SWITCH)
578 #define hssec(vcpu) (vcpu->arch.sie_block->gcr[13] & _ASCE_SPACE_SWITCH)
579 #define old_ssec(vcpu) ((vcpu->arch.sie_block->tecmc >> 31) & 0x1)
580 #define old_as_is_home(vcpu) !(vcpu->arch.sie_block->tecmc & 0xffff)
581 
kvm_s390_handle_per_event(struct kvm_vcpu * vcpu)582 int kvm_s390_handle_per_event(struct kvm_vcpu *vcpu)
583 {
584 	int rc, new_as;
585 
586 	if (debug_exit_required(vcpu, vcpu->arch.sie_block->perc,
587 				vcpu->arch.sie_block->peraddr))
588 		vcpu->guest_debug |= KVM_GUESTDBG_EXIT_PENDING;
589 
590 	rc = filter_guest_per_event(vcpu);
591 	if (rc)
592 		return rc;
593 
594 	/*
595 	 * Only RP, SAC, SACF, PT, PTI, PR, PC instructions can trigger
596 	 * a space-switch event. PER events enforce space-switch events
597 	 * for these instructions. So if no PER event for the guest is left,
598 	 * we might have to filter the space-switch element out, too.
599 	 */
600 	if (vcpu->arch.sie_block->iprcc == PGM_SPACE_SWITCH) {
601 		vcpu->arch.sie_block->iprcc = 0;
602 		new_as = psw_bits(vcpu->arch.sie_block->gpsw).as;
603 
604 		/*
605 		 * If the AS changed from / to home, we had RP, SAC or SACF
606 		 * instruction. Check primary and home space-switch-event
607 		 * controls. (theoretically home -> home produced no event)
608 		 */
609 		if (((new_as == PSW_BITS_AS_HOME) ^ old_as_is_home(vcpu)) &&
610 		    (pssec(vcpu) || hssec(vcpu)))
611 			vcpu->arch.sie_block->iprcc = PGM_SPACE_SWITCH;
612 
613 		/*
614 		 * PT, PTI, PR, PC instruction operate on primary AS only. Check
615 		 * if the primary-space-switch-event control was or got set.
616 		 */
617 		if (new_as == PSW_BITS_AS_PRIMARY && !old_as_is_home(vcpu) &&
618 		    (pssec(vcpu) || old_ssec(vcpu)))
619 			vcpu->arch.sie_block->iprcc = PGM_SPACE_SWITCH;
620 	}
621 	return 0;
622 }
623