2 * in-kernel handling for sie intercepts
4 * Copyright IBM Corp. 2008, 2014
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License (version 2 only)
8 * as published by the Free Software Foundation.
14 #include <linux/kvm_host.h>
15 #include <linux/errno.h>
16 #include <linux/pagemap.h>
18 #include <asm/kvm_host.h>
19 #include <asm/asm-offsets.h>
21 #include <asm/sysinfo.h>
26 #include "trace-s390.h"
29 static const intercept_handler_t instruction_handlers[256] = {
30 [0x01] = kvm_s390_handle_01,
31 [0x82] = kvm_s390_handle_lpsw,
32 [0x83] = kvm_s390_handle_diag,
33 [0xaa] = kvm_s390_handle_aa,
34 [0xae] = kvm_s390_handle_sigp,
35 [0xb2] = kvm_s390_handle_b2,
36 [0xb6] = kvm_s390_handle_stctl,
37 [0xb7] = kvm_s390_handle_lctl,
38 [0xb9] = kvm_s390_handle_b9,
39 [0xe3] = kvm_s390_handle_e3,
40 [0xe5] = kvm_s390_handle_e5,
41 [0xeb] = kvm_s390_handle_eb,
44 u8 kvm_s390_get_ilen(struct kvm_vcpu *vcpu)
46 struct kvm_s390_sie_block *sie_block = vcpu->arch.sie_block;
49 switch (vcpu->arch.sie_block->icptcode) {
55 /* instruction only stored for these icptcodes */
56 ilen = insn_length(vcpu->arch.sie_block->ipa >> 8);
57 /* Use the length of the EXECUTE instruction if necessary */
58 if (sie_block->icptstatus & 1) {
59 ilen = (sie_block->icptstatus >> 4) & 0x6;
65 /* bit 1+2 of pgmilc are the ilc, so we directly get ilen */
66 ilen = vcpu->arch.sie_block->pgmilc & 0x6;
72 static int handle_noop(struct kvm_vcpu *vcpu)
74 switch (vcpu->arch.sie_block->icptcode) {
76 vcpu->stat.exit_external_request++;
84 static int handle_stop(struct kvm_vcpu *vcpu)
86 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
88 uint8_t flags, stop_pending;
90 vcpu->stat.exit_stop_request++;
92 /* delay the stop if any non-stop irq is pending */
93 if (kvm_s390_vcpu_has_irq(vcpu, 1))
96 /* avoid races with the injection/SIGP STOP code */
98 flags = li->irq.stop.flags;
99 stop_pending = kvm_s390_is_stop_irq_pending(vcpu);
100 spin_unlock(&li->lock);
102 trace_kvm_s390_stop_request(stop_pending, flags);
106 if (flags & KVM_S390_STOP_FLAG_STORE_STATUS) {
107 rc = kvm_s390_vcpu_store_status(vcpu,
108 KVM_S390_STORE_STATUS_NOADDR);
113 if (!kvm_s390_user_cpu_state_ctrl(vcpu->kvm))
114 kvm_s390_vcpu_stop(vcpu);
118 static int handle_validity(struct kvm_vcpu *vcpu)
120 int viwhy = vcpu->arch.sie_block->ipb >> 16;
122 vcpu->stat.exit_validity++;
123 trace_kvm_s390_intercept_validity(vcpu, viwhy);
124 KVM_EVENT(3, "validity intercept 0x%x for pid %u (kvm 0x%pK)", viwhy,
125 current->pid, vcpu->kvm);
127 /* do not warn on invalid runtime instrumentation mode */
128 WARN_ONCE(viwhy != 0x44, "kvm: unhandled validity intercept 0x%x\n",
133 static int handle_instruction(struct kvm_vcpu *vcpu)
135 intercept_handler_t handler;
137 vcpu->stat.exit_instruction++;
138 trace_kvm_s390_intercept_instruction(vcpu,
139 vcpu->arch.sie_block->ipa,
140 vcpu->arch.sie_block->ipb);
141 handler = instruction_handlers[vcpu->arch.sie_block->ipa >> 8];
143 return handler(vcpu);
147 static int inject_prog_on_prog_intercept(struct kvm_vcpu *vcpu)
149 struct kvm_s390_pgm_info pgm_info = {
150 .code = vcpu->arch.sie_block->iprcc,
151 /* the PSW has already been rewound */
152 .flags = KVM_S390_PGM_FLAGS_NO_REWIND,
155 switch (vcpu->arch.sie_block->iprcc & ~PGM_PER) {
156 case PGM_AFX_TRANSLATION:
157 case PGM_ASX_TRANSLATION:
158 case PGM_EX_TRANSLATION:
159 case PGM_LFX_TRANSLATION:
160 case PGM_LSTE_SEQUENCE:
161 case PGM_LSX_TRANSLATION:
162 case PGM_LX_TRANSLATION:
163 case PGM_PRIMARY_AUTHORITY:
164 case PGM_SECONDARY_AUTHORITY:
165 case PGM_SPACE_SWITCH:
166 pgm_info.trans_exc_code = vcpu->arch.sie_block->tecmc;
168 case PGM_ALEN_TRANSLATION:
169 case PGM_ALE_SEQUENCE:
170 case PGM_ASTE_INSTANCE:
171 case PGM_ASTE_SEQUENCE:
172 case PGM_ASTE_VALIDITY:
173 case PGM_EXTENDED_AUTHORITY:
174 pgm_info.exc_access_id = vcpu->arch.sie_block->eai;
177 case PGM_PAGE_TRANSLATION:
178 case PGM_REGION_FIRST_TRANS:
179 case PGM_REGION_SECOND_TRANS:
180 case PGM_REGION_THIRD_TRANS:
181 case PGM_SEGMENT_TRANSLATION:
182 pgm_info.trans_exc_code = vcpu->arch.sie_block->tecmc;
183 pgm_info.exc_access_id = vcpu->arch.sie_block->eai;
184 pgm_info.op_access_id = vcpu->arch.sie_block->oai;
187 pgm_info.mon_class_nr = vcpu->arch.sie_block->mcn;
188 pgm_info.mon_code = vcpu->arch.sie_block->tecmc;
190 case PGM_VECTOR_PROCESSING:
192 pgm_info.data_exc_code = vcpu->arch.sie_block->dxc;
195 pgm_info.trans_exc_code = vcpu->arch.sie_block->tecmc;
196 pgm_info.exc_access_id = vcpu->arch.sie_block->eai;
202 if (vcpu->arch.sie_block->iprcc & PGM_PER) {
203 pgm_info.per_code = vcpu->arch.sie_block->perc;
204 pgm_info.per_atmid = vcpu->arch.sie_block->peratmid;
205 pgm_info.per_address = vcpu->arch.sie_block->peraddr;
206 pgm_info.per_access_id = vcpu->arch.sie_block->peraid;
208 return kvm_s390_inject_prog_irq(vcpu, &pgm_info);
212 * restore ITDB to program-interruption TDB in guest lowcore
213 * and set TX abort indication if required
215 static int handle_itdb(struct kvm_vcpu *vcpu)
217 struct kvm_s390_itdb *itdb;
220 if (!IS_TE_ENABLED(vcpu) || !IS_ITDB_VALID(vcpu))
222 if (current->thread.per_flags & PER_FLAG_NO_TE)
224 itdb = (struct kvm_s390_itdb *)vcpu->arch.sie_block->itdba;
225 rc = write_guest_lc(vcpu, __LC_PGM_TDB, itdb, sizeof(*itdb));
228 memset(itdb, 0, sizeof(*itdb));
233 #define per_event(vcpu) (vcpu->arch.sie_block->iprcc & PGM_PER)
235 static int handle_prog(struct kvm_vcpu *vcpu)
240 vcpu->stat.exit_program_interruption++;
242 if (guestdbg_enabled(vcpu) && per_event(vcpu)) {
243 rc = kvm_s390_handle_per_event(vcpu);
246 /* the interrupt might have been filtered out completely */
247 if (vcpu->arch.sie_block->iprcc == 0)
251 trace_kvm_s390_intercept_prog(vcpu, vcpu->arch.sie_block->iprcc);
252 if (vcpu->arch.sie_block->iprcc == PGM_SPECIFICATION) {
253 rc = read_guest_lc(vcpu, __LC_PGM_NEW_PSW, &psw, sizeof(psw_t));
256 /* Avoid endless loops of specification exceptions */
257 if (!is_valid_psw(&psw))
260 rc = handle_itdb(vcpu);
264 return inject_prog_on_prog_intercept(vcpu);
268 * handle_external_interrupt - used for external interruption interceptions
270 * This interception only occurs if the CPUSTAT_EXT_INT bit was set, or if
271 * the new PSW does not have external interrupts disabled. In the first case,
272 * we've got to deliver the interrupt manually, and in the second case, we
273 * drop to userspace to handle the situation there.
275 static int handle_external_interrupt(struct kvm_vcpu *vcpu)
277 u16 eic = vcpu->arch.sie_block->eic;
278 struct kvm_s390_irq irq;
282 vcpu->stat.exit_external_interrupt++;
284 rc = read_guest_lc(vcpu, __LC_EXT_NEW_PSW, &newpsw, sizeof(psw_t));
287 /* We can not handle clock comparator or timer interrupt with bad PSW */
288 if ((eic == EXT_IRQ_CLK_COMP || eic == EXT_IRQ_CPU_TIMER) &&
289 (newpsw.mask & PSW_MASK_EXT))
293 case EXT_IRQ_CLK_COMP:
294 irq.type = KVM_S390_INT_CLOCK_COMP;
296 case EXT_IRQ_CPU_TIMER:
297 irq.type = KVM_S390_INT_CPU_TIMER;
299 case EXT_IRQ_EXTERNAL_CALL:
300 irq.type = KVM_S390_INT_EXTERNAL_CALL;
301 irq.u.extcall.code = vcpu->arch.sie_block->extcpuaddr;
302 rc = kvm_s390_inject_vcpu(vcpu, &irq);
303 /* ignore if another external call is already pending */
311 return kvm_s390_inject_vcpu(vcpu, &irq);
315 * Handle MOVE PAGE partial execution interception.
317 * This interception can only happen for guests with DAT disabled and
318 * addresses that are currently not mapped in the host. Thus we try to
319 * set up the mappings for the corresponding user pages here (or throw
320 * addressing exceptions in case of illegal guest addresses).
322 static int handle_mvpg_pei(struct kvm_vcpu *vcpu)
324 unsigned long srcaddr, dstaddr;
327 kvm_s390_get_regs_rre(vcpu, ®1, ®2);
329 /* Make sure that the source is paged-in */
330 rc = guest_translate_address(vcpu, vcpu->run->s.regs.gprs[reg2],
331 reg2, &srcaddr, GACC_FETCH);
333 return kvm_s390_inject_prog_cond(vcpu, rc);
334 rc = kvm_arch_fault_in_page(vcpu, srcaddr, 0);
338 /* Make sure that the destination is paged-in */
339 rc = guest_translate_address(vcpu, vcpu->run->s.regs.gprs[reg1],
340 reg1, &dstaddr, GACC_STORE);
342 return kvm_s390_inject_prog_cond(vcpu, rc);
343 rc = kvm_arch_fault_in_page(vcpu, dstaddr, 1);
347 kvm_s390_retry_instr(vcpu);
352 static int handle_partial_execution(struct kvm_vcpu *vcpu)
354 vcpu->stat.exit_pei++;
356 if (vcpu->arch.sie_block->ipa == 0xb254) /* MVPG */
357 return handle_mvpg_pei(vcpu);
358 if (vcpu->arch.sie_block->ipa >> 8 == 0xae) /* SIGP */
359 return kvm_s390_handle_sigp_pei(vcpu);
365 * Handle the sthyi instruction that provides the guest with system
366 * information, like current CPU resources available at each level of
369 int handle_sthyi(struct kvm_vcpu *vcpu)
371 int reg1, reg2, r = 0;
372 u64 code, addr, cc = 0, rc = 0;
373 struct sthyi_sctns *sctns = NULL;
375 if (!test_kvm_facility(vcpu->kvm, 74))
376 return kvm_s390_inject_program_int(vcpu, PGM_OPERATION);
378 kvm_s390_get_regs_rre(vcpu, ®1, ®2);
379 code = vcpu->run->s.regs.gprs[reg1];
380 addr = vcpu->run->s.regs.gprs[reg2];
382 vcpu->stat.instruction_sthyi++;
383 VCPU_EVENT(vcpu, 3, "STHYI: fc: %llu addr: 0x%016llx", code, addr);
384 trace_kvm_s390_handle_sthyi(vcpu, code, addr);
386 if (reg1 == reg2 || reg1 & 1 || reg2 & 1)
387 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
395 if (addr & ~PAGE_MASK)
396 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
398 sctns = (void *)get_zeroed_page(GFP_KERNEL);
402 cc = sthyi_fill(sctns, &rc);
406 r = write_guest(vcpu, addr, reg2, sctns, PAGE_SIZE);
408 free_page((unsigned long)sctns);
409 return kvm_s390_inject_prog_cond(vcpu, r);
413 free_page((unsigned long)sctns);
414 vcpu->run->s.regs.gprs[reg2 + 1] = rc;
415 kvm_s390_set_psw_cc(vcpu, cc);
419 static int handle_operexc(struct kvm_vcpu *vcpu)
421 psw_t oldpsw, newpsw;
424 vcpu->stat.exit_operation_exception++;
425 trace_kvm_s390_handle_operexc(vcpu, vcpu->arch.sie_block->ipa,
426 vcpu->arch.sie_block->ipb);
428 if (vcpu->arch.sie_block->ipa == 0xb256)
429 return handle_sthyi(vcpu);
431 if (vcpu->arch.sie_block->ipa == 0 && vcpu->kvm->arch.user_instr0)
433 rc = read_guest_lc(vcpu, __LC_PGM_NEW_PSW, &newpsw, sizeof(psw_t));
437 * Avoid endless loops of operation exceptions, if the pgm new
438 * PSW will cause a new operation exception.
439 * The heuristic checks if the pgm new psw is within 6 bytes before
440 * the faulting psw address (with same DAT, AS settings) and the
441 * new psw is not a wait psw and the fault was not triggered by
444 oldpsw = vcpu->arch.sie_block->gpsw;
445 if (oldpsw.addr - newpsw.addr <= 6 &&
446 !(newpsw.mask & PSW_MASK_WAIT) &&
447 !(oldpsw.mask & PSW_MASK_PSTATE) &&
448 (newpsw.mask & PSW_MASK_ASC) == (oldpsw.mask & PSW_MASK_ASC) &&
449 (newpsw.mask & PSW_MASK_DAT) == (oldpsw.mask & PSW_MASK_DAT))
452 return kvm_s390_inject_program_int(vcpu, PGM_OPERATION);
455 int kvm_handle_sie_intercept(struct kvm_vcpu *vcpu)
459 if (kvm_is_ucontrol(vcpu->kvm))
462 switch (vcpu->arch.sie_block->icptcode) {
465 return handle_noop(vcpu);
467 rc = handle_instruction(vcpu);
470 return handle_prog(vcpu);
472 return handle_external_interrupt(vcpu);
474 return kvm_s390_handle_wait(vcpu);
476 return handle_validity(vcpu);
478 return handle_stop(vcpu);
480 rc = handle_operexc(vcpu);
483 rc = handle_partial_execution(vcpu);
486 rc = kvm_s390_skey_check_enable(vcpu);
492 /* process PER, also if the instrution is processed in user space */
493 if (vcpu->arch.sie_block->icptstatus & 0x02 &&
494 (!rc || rc == -EOPNOTSUPP))
495 per_rc = kvm_s390_handle_per_ifetch_icpt(vcpu);
496 return per_rc ? per_rc : rc;