KVM: s390: migration / injection of prog irq ilc
[deliverable/linux.git] / arch / s390 / kvm / intercept.c
CommitLineData
8f2abe6a 1/*
a53c8fab 2 * in-kernel handling for sie intercepts
8f2abe6a 3 *
9a558ee3 4 * Copyright IBM Corp. 2008, 2014
8f2abe6a
CB
5 *
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.
9 *
10 * Author(s): Carsten Otte <cotte@de.ibm.com>
11 * Christian Borntraeger <borntraeger@de.ibm.com>
12 */
13
14#include <linux/kvm_host.h>
15#include <linux/errno.h>
16#include <linux/pagemap.h>
17
18#include <asm/kvm_host.h>
a86dcc24 19#include <asm/asm-offsets.h>
f14d82e0 20#include <asm/irq.h>
8f2abe6a
CB
21
22#include "kvm-s390.h"
ba5c1e9b 23#include "gaccess.h"
5786fffa 24#include "trace.h"
ade38c31 25#include "trace-s390.h"
ba5c1e9b 26
f379aae5 27
77975357 28static const intercept_handler_t instruction_handlers[256] = {
8c3f61e2 29 [0x01] = kvm_s390_handle_01,
48a3e950 30 [0x82] = kvm_s390_handle_lpsw,
e28acfea 31 [0x83] = kvm_s390_handle_diag,
5288fbf0 32 [0xae] = kvm_s390_handle_sigp,
70455a36 33 [0xb2] = kvm_s390_handle_b2,
aba07508 34 [0xb6] = kvm_s390_handle_stctl,
953ed88d 35 [0xb7] = kvm_s390_handle_lctl,
48a3e950 36 [0xb9] = kvm_s390_handle_b9,
bb25b9ba 37 [0xe5] = kvm_s390_handle_e5,
953ed88d 38 [0xeb] = kvm_s390_handle_eb,
ba5c1e9b 39};
8f2abe6a 40
0e8bc06a 41u8 kvm_s390_get_ilen(struct kvm_vcpu *vcpu)
04b41acd
TH
42{
43 struct kvm_s390_sie_block *sie_block = vcpu->arch.sie_block;
0e8bc06a 44 u8 ilen = 0;
04b41acd 45
0e8bc06a
DH
46 switch (vcpu->arch.sie_block->icptcode) {
47 case ICPT_INST:
48 case ICPT_INSTPROGI:
49 case ICPT_OPEREXC:
50 case ICPT_PARTEXEC:
51 case ICPT_IOINST:
52 /* instruction only stored for these icptcodes */
53 ilen = insn_length(vcpu->arch.sie_block->ipa >> 8);
54 /* Use the length of the EXECUTE instruction if necessary */
55 if (sie_block->icptstatus & 1) {
56 ilen = (sie_block->icptstatus >> 4) & 0x6;
57 if (!ilen)
58 ilen = 4;
59 }
60 break;
61 case ICPT_PROGI:
62 /* bit 1+2 of pgmilc are the ilc, so we directly get ilen */
63 ilen = vcpu->arch.sie_block->pgmilc & 0x6;
64 break;
04b41acd 65 }
0e8bc06a 66 return ilen;
04b41acd
TH
67}
68
8f2abe6a
CB
69static int handle_noop(struct kvm_vcpu *vcpu)
70{
71 switch (vcpu->arch.sie_block->icptcode) {
72 case 0x10:
73 vcpu->stat.exit_external_request++;
74 break;
8f2abe6a
CB
75 default:
76 break; /* nothing */
77 }
78 return 0;
79}
80
81static int handle_stop(struct kvm_vcpu *vcpu)
82{
6cddd432 83 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
9ace903d 84 int rc = 0;
6cddd432 85 uint8_t flags, stop_pending;
5288fbf0 86
8f2abe6a 87 vcpu->stat.exit_stop_request++;
ade38c31 88
9a022067
DH
89 /* delay the stop if any non-stop irq is pending */
90 if (kvm_s390_vcpu_has_irq(vcpu, 1))
91 return 0;
92
6cddd432
DH
93 /* avoid races with the injection/SIGP STOP code */
94 spin_lock(&li->lock);
95 flags = li->irq.stop.flags;
96 stop_pending = kvm_s390_is_stop_irq_pending(vcpu);
97 spin_unlock(&li->lock);
32f5ff63 98
6cddd432
DH
99 trace_kvm_s390_stop_request(stop_pending, flags);
100 if (!stop_pending)
32f5ff63 101 return 0;
9ace903d 102
6cddd432 103 if (flags & KVM_S390_STOP_FLAG_STORE_STATUS) {
9e0d5473
JF
104 rc = kvm_s390_vcpu_store_status(vcpu,
105 KVM_S390_STORE_STATUS_NOADDR);
32f5ff63
DH
106 if (rc)
107 return rc;
108 }
109
6352e4d2
DH
110 if (!kvm_s390_user_cpu_state_ctrl(vcpu->kvm))
111 kvm_s390_vcpu_stop(vcpu);
32f5ff63 112 return -EOPNOTSUPP;
8f2abe6a
CB
113}
114
115static int handle_validity(struct kvm_vcpu *vcpu)
116{
117 int viwhy = vcpu->arch.sie_block->ipb >> 16;
3edbcff9 118
8f2abe6a 119 vcpu->stat.exit_validity++;
5786fffa 120 trace_kvm_s390_intercept_validity(vcpu, viwhy);
2c70fe44
CB
121 WARN_ONCE(true, "kvm: unhandled validity intercept 0x%x\n", viwhy);
122 return -EOPNOTSUPP;
8f2abe6a
CB
123}
124
ba5c1e9b
CO
125static int handle_instruction(struct kvm_vcpu *vcpu)
126{
127 intercept_handler_t handler;
128
129 vcpu->stat.exit_instruction++;
5786fffa
CH
130 trace_kvm_s390_intercept_instruction(vcpu,
131 vcpu->arch.sie_block->ipa,
132 vcpu->arch.sie_block->ipb);
ba5c1e9b
CO
133 handler = instruction_handlers[vcpu->arch.sie_block->ipa >> 8];
134 if (handler)
135 return handler(vcpu);
b8e660b8 136 return -EOPNOTSUPP;
ba5c1e9b
CO
137}
138
439716a5
DH
139static void __extract_prog_irq(struct kvm_vcpu *vcpu,
140 struct kvm_s390_pgm_info *pgm_info)
141{
142 memset(pgm_info, 0, sizeof(struct kvm_s390_pgm_info));
143 pgm_info->code = vcpu->arch.sie_block->iprcc;
144
145 switch (vcpu->arch.sie_block->iprcc & ~PGM_PER) {
146 case PGM_AFX_TRANSLATION:
147 case PGM_ASX_TRANSLATION:
148 case PGM_EX_TRANSLATION:
149 case PGM_LFX_TRANSLATION:
150 case PGM_LSTE_SEQUENCE:
151 case PGM_LSX_TRANSLATION:
152 case PGM_LX_TRANSLATION:
153 case PGM_PRIMARY_AUTHORITY:
154 case PGM_SECONDARY_AUTHORITY:
155 case PGM_SPACE_SWITCH:
156 pgm_info->trans_exc_code = vcpu->arch.sie_block->tecmc;
157 break;
158 case PGM_ALEN_TRANSLATION:
159 case PGM_ALE_SEQUENCE:
160 case PGM_ASTE_INSTANCE:
161 case PGM_ASTE_SEQUENCE:
162 case PGM_ASTE_VALIDITY:
163 case PGM_EXTENDED_AUTHORITY:
164 pgm_info->exc_access_id = vcpu->arch.sie_block->eai;
165 break;
166 case PGM_ASCE_TYPE:
167 case PGM_PAGE_TRANSLATION:
168 case PGM_REGION_FIRST_TRANS:
169 case PGM_REGION_SECOND_TRANS:
170 case PGM_REGION_THIRD_TRANS:
171 case PGM_SEGMENT_TRANSLATION:
172 pgm_info->trans_exc_code = vcpu->arch.sie_block->tecmc;
173 pgm_info->exc_access_id = vcpu->arch.sie_block->eai;
174 pgm_info->op_access_id = vcpu->arch.sie_block->oai;
175 break;
176 case PGM_MONITOR:
177 pgm_info->mon_class_nr = vcpu->arch.sie_block->mcn;
178 pgm_info->mon_code = vcpu->arch.sie_block->tecmc;
179 break;
403c8648 180 case PGM_VECTOR_PROCESSING:
439716a5
DH
181 case PGM_DATA:
182 pgm_info->data_exc_code = vcpu->arch.sie_block->dxc;
183 break;
184 case PGM_PROTECTION:
185 pgm_info->trans_exc_code = vcpu->arch.sie_block->tecmc;
186 pgm_info->exc_access_id = vcpu->arch.sie_block->eai;
187 break;
188 default:
189 break;
190 }
191
192 if (vcpu->arch.sie_block->iprcc & PGM_PER) {
193 pgm_info->per_code = vcpu->arch.sie_block->perc;
194 pgm_info->per_atmid = vcpu->arch.sie_block->peratmid;
195 pgm_info->per_address = vcpu->arch.sie_block->peraddr;
196 pgm_info->per_access_id = vcpu->arch.sie_block->peraid;
197 }
198}
199
e325fe69
MM
200/*
201 * restore ITDB to program-interruption TDB in guest lowcore
202 * and set TX abort indication if required
203*/
204static int handle_itdb(struct kvm_vcpu *vcpu)
205{
206 struct kvm_s390_itdb *itdb;
207 int rc;
208
209 if (!IS_TE_ENABLED(vcpu) || !IS_ITDB_VALID(vcpu))
210 return 0;
211 if (current->thread.per_flags & PER_FLAG_NO_TE)
212 return 0;
213 itdb = (struct kvm_s390_itdb *)vcpu->arch.sie_block->itdba;
214 rc = write_guest_lc(vcpu, __LC_PGM_TDB, itdb, sizeof(*itdb));
215 if (rc)
216 return rc;
217 memset(itdb, 0, sizeof(*itdb));
218
219 return 0;
220}
221
27291e21
DH
222#define per_event(vcpu) (vcpu->arch.sie_block->iprcc & PGM_PER)
223
ba5c1e9b
CO
224static int handle_prog(struct kvm_vcpu *vcpu)
225{
439716a5 226 struct kvm_s390_pgm_info pgm_info;
684135e0 227 psw_t psw;
0040e7d2
HC
228 int rc;
229
ba5c1e9b 230 vcpu->stat.exit_program_interruption++;
7feb6bb8 231
27291e21
DH
232 if (guestdbg_enabled(vcpu) && per_event(vcpu)) {
233 kvm_s390_handle_per_event(vcpu);
234 /* the interrupt might have been filtered out completely */
235 if (vcpu->arch.sie_block->iprcc == 0)
236 return 0;
237 }
238
e325fe69 239 trace_kvm_s390_intercept_prog(vcpu, vcpu->arch.sie_block->iprcc);
684135e0
TH
240 if (vcpu->arch.sie_block->iprcc == PGM_SPECIFICATION) {
241 rc = read_guest_lc(vcpu, __LC_PGM_NEW_PSW, &psw, sizeof(psw_t));
242 if (rc)
243 return rc;
244 /* Avoid endless loops of specification exceptions */
245 if (!is_valid_psw(&psw))
246 return -EOPNOTSUPP;
247 }
e325fe69 248 rc = handle_itdb(vcpu);
0040e7d2
HC
249 if (rc)
250 return rc;
439716a5 251
e325fe69 252 __extract_prog_irq(vcpu, &pgm_info);
439716a5 253 return kvm_s390_inject_prog_irq(vcpu, &pgm_info);
ba5c1e9b
CO
254}
255
f14d82e0
TH
256/**
257 * handle_external_interrupt - used for external interruption interceptions
258 *
259 * This interception only occurs if the CPUSTAT_EXT_INT bit was set, or if
260 * the new PSW does not have external interrupts disabled. In the first case,
261 * we've got to deliver the interrupt manually, and in the second case, we
262 * drop to userspace to handle the situation there.
263 */
264static int handle_external_interrupt(struct kvm_vcpu *vcpu)
265{
266 u16 eic = vcpu->arch.sie_block->eic;
383d0b05 267 struct kvm_s390_irq irq;
f14d82e0
TH
268 psw_t newpsw;
269 int rc;
270
271 vcpu->stat.exit_external_interrupt++;
272
273 rc = read_guest_lc(vcpu, __LC_EXT_NEW_PSW, &newpsw, sizeof(psw_t));
274 if (rc)
275 return rc;
276 /* We can not handle clock comparator or timer interrupt with bad PSW */
277 if ((eic == EXT_IRQ_CLK_COMP || eic == EXT_IRQ_CPU_TIMER) &&
278 (newpsw.mask & PSW_MASK_EXT))
279 return -EOPNOTSUPP;
280
281 switch (eic) {
282 case EXT_IRQ_CLK_COMP:
283 irq.type = KVM_S390_INT_CLOCK_COMP;
284 break;
285 case EXT_IRQ_CPU_TIMER:
286 irq.type = KVM_S390_INT_CPU_TIMER;
287 break;
288 case EXT_IRQ_EXTERNAL_CALL:
289 irq.type = KVM_S390_INT_EXTERNAL_CALL;
383d0b05 290 irq.u.extcall.code = vcpu->arch.sie_block->extcpuaddr;
ea5f4969
DH
291 rc = kvm_s390_inject_vcpu(vcpu, &irq);
292 /* ignore if another external call is already pending */
293 if (rc == -EBUSY)
294 return 0;
295 return rc;
f14d82e0
TH
296 default:
297 return -EOPNOTSUPP;
298 }
299
300 return kvm_s390_inject_vcpu(vcpu, &irq);
301}
302
9a558ee3
TH
303/**
304 * Handle MOVE PAGE partial execution interception.
305 *
306 * This interception can only happen for guests with DAT disabled and
307 * addresses that are currently not mapped in the host. Thus we try to
308 * set up the mappings for the corresponding user pages here (or throw
309 * addressing exceptions in case of illegal guest addresses).
310 */
311static int handle_mvpg_pei(struct kvm_vcpu *vcpu)
312{
f22166dc 313 unsigned long srcaddr, dstaddr;
9a558ee3
TH
314 int reg1, reg2, rc;
315
316 kvm_s390_get_regs_rre(vcpu, &reg1, &reg2);
9a558ee3
TH
317
318 /* Make sure that the source is paged-in */
3cfad023 319 rc = guest_translate_address(vcpu, vcpu->run->s.regs.gprs[reg2],
8ae04b8f 320 reg2, &srcaddr, 0);
3cfad023
TH
321 if (rc)
322 return kvm_s390_inject_prog_cond(vcpu, rc);
f22166dc
TH
323 rc = kvm_arch_fault_in_page(vcpu, srcaddr, 0);
324 if (rc != 0)
9a558ee3
TH
325 return rc;
326
327 /* Make sure that the destination is paged-in */
3cfad023 328 rc = guest_translate_address(vcpu, vcpu->run->s.regs.gprs[reg1],
8ae04b8f 329 reg1, &dstaddr, 1);
3cfad023
TH
330 if (rc)
331 return kvm_s390_inject_prog_cond(vcpu, rc);
f22166dc
TH
332 rc = kvm_arch_fault_in_page(vcpu, dstaddr, 1);
333 if (rc != 0)
9a558ee3
TH
334 return rc;
335
0e8bc06a 336 kvm_s390_retry_instr(vcpu);
9a558ee3
TH
337
338 return 0;
339}
340
341static int handle_partial_execution(struct kvm_vcpu *vcpu)
342{
343 if (vcpu->arch.sie_block->ipa == 0xb254) /* MVPG */
344 return handle_mvpg_pei(vcpu);
4953919f
DH
345 if (vcpu->arch.sie_block->ipa >> 8 == 0xae) /* SIGP */
346 return kvm_s390_handle_sigp_pei(vcpu);
9a558ee3
TH
347
348 return -EOPNOTSUPP;
349}
350
8f2abe6a
CB
351int kvm_handle_sie_intercept(struct kvm_vcpu *vcpu)
352{
71f116bf
DH
353 if (kvm_is_ucontrol(vcpu->kvm))
354 return -EOPNOTSUPP;
355
46b708ea 356 switch (vcpu->arch.sie_block->icptcode) {
46b708ea
CB
357 case 0x10:
358 case 0x18:
359 return handle_noop(vcpu);
360 case 0x04:
361 return handle_instruction(vcpu);
362 case 0x08:
363 return handle_prog(vcpu);
364 case 0x14:
365 return handle_external_interrupt(vcpu);
366 case 0x1c:
367 return kvm_s390_handle_wait(vcpu);
368 case 0x20:
369 return handle_validity(vcpu);
370 case 0x28:
371 return handle_stop(vcpu);
372 case 0x38:
373 return handle_partial_execution(vcpu);
374 default:
b8e660b8 375 return -EOPNOTSUPP;
46b708ea 376 }
8f2abe6a 377}
This page took 0.581202 seconds and 5 git commands to generate.