KVM: prepare for KVM_(S|G)ET_MP_STATE on other architectures
[linux-block.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
CB
40
41static int handle_noop(struct kvm_vcpu *vcpu)
42{
43 switch (vcpu->arch.sie_block->icptcode) {
0eaeafa1
CB
44 case 0x0:
45 vcpu->stat.exit_null++;
46 break;
8f2abe6a
CB
47 case 0x10:
48 vcpu->stat.exit_external_request++;
49 break;
8f2abe6a
CB
50 default:
51 break; /* nothing */
52 }
53 return 0;
54}
55
56static int handle_stop(struct kvm_vcpu *vcpu)
57{
9ace903d 58 int rc = 0;
32f5ff63 59 unsigned int action_bits;
5288fbf0 60
8f2abe6a 61 vcpu->stat.exit_stop_request++;
ade38c31
CH
62 trace_kvm_s390_stop_request(vcpu->arch.local_int.action_bits);
63
32f5ff63
DH
64 action_bits = vcpu->arch.local_int.action_bits;
65
66 if (!(action_bits & ACTION_STOP_ON_STOP))
67 return 0;
9ace903d 68
32f5ff63 69 if (action_bits & ACTION_STORE_ON_STOP) {
9e0d5473
JF
70 rc = kvm_s390_vcpu_store_status(vcpu,
71 KVM_S390_STORE_STATUS_NOADDR);
32f5ff63
DH
72 if (rc)
73 return rc;
74 }
75
76 kvm_s390_vcpu_stop(vcpu);
77 return -EOPNOTSUPP;
8f2abe6a
CB
78}
79
80static int handle_validity(struct kvm_vcpu *vcpu)
81{
82 int viwhy = vcpu->arch.sie_block->ipb >> 16;
3edbcff9 83
8f2abe6a 84 vcpu->stat.exit_validity++;
5786fffa 85 trace_kvm_s390_intercept_validity(vcpu, viwhy);
2c70fe44
CB
86 WARN_ONCE(true, "kvm: unhandled validity intercept 0x%x\n", viwhy);
87 return -EOPNOTSUPP;
8f2abe6a
CB
88}
89
ba5c1e9b
CO
90static int handle_instruction(struct kvm_vcpu *vcpu)
91{
92 intercept_handler_t handler;
93
94 vcpu->stat.exit_instruction++;
5786fffa
CH
95 trace_kvm_s390_intercept_instruction(vcpu,
96 vcpu->arch.sie_block->ipa,
97 vcpu->arch.sie_block->ipb);
ba5c1e9b
CO
98 handler = instruction_handlers[vcpu->arch.sie_block->ipa >> 8];
99 if (handler)
100 return handler(vcpu);
b8e660b8 101 return -EOPNOTSUPP;
ba5c1e9b
CO
102}
103
439716a5
DH
104static void __extract_prog_irq(struct kvm_vcpu *vcpu,
105 struct kvm_s390_pgm_info *pgm_info)
106{
107 memset(pgm_info, 0, sizeof(struct kvm_s390_pgm_info));
108 pgm_info->code = vcpu->arch.sie_block->iprcc;
109
110 switch (vcpu->arch.sie_block->iprcc & ~PGM_PER) {
111 case PGM_AFX_TRANSLATION:
112 case PGM_ASX_TRANSLATION:
113 case PGM_EX_TRANSLATION:
114 case PGM_LFX_TRANSLATION:
115 case PGM_LSTE_SEQUENCE:
116 case PGM_LSX_TRANSLATION:
117 case PGM_LX_TRANSLATION:
118 case PGM_PRIMARY_AUTHORITY:
119 case PGM_SECONDARY_AUTHORITY:
120 case PGM_SPACE_SWITCH:
121 pgm_info->trans_exc_code = vcpu->arch.sie_block->tecmc;
122 break;
123 case PGM_ALEN_TRANSLATION:
124 case PGM_ALE_SEQUENCE:
125 case PGM_ASTE_INSTANCE:
126 case PGM_ASTE_SEQUENCE:
127 case PGM_ASTE_VALIDITY:
128 case PGM_EXTENDED_AUTHORITY:
129 pgm_info->exc_access_id = vcpu->arch.sie_block->eai;
130 break;
131 case PGM_ASCE_TYPE:
132 case PGM_PAGE_TRANSLATION:
133 case PGM_REGION_FIRST_TRANS:
134 case PGM_REGION_SECOND_TRANS:
135 case PGM_REGION_THIRD_TRANS:
136 case PGM_SEGMENT_TRANSLATION:
137 pgm_info->trans_exc_code = vcpu->arch.sie_block->tecmc;
138 pgm_info->exc_access_id = vcpu->arch.sie_block->eai;
139 pgm_info->op_access_id = vcpu->arch.sie_block->oai;
140 break;
141 case PGM_MONITOR:
142 pgm_info->mon_class_nr = vcpu->arch.sie_block->mcn;
143 pgm_info->mon_code = vcpu->arch.sie_block->tecmc;
144 break;
145 case PGM_DATA:
146 pgm_info->data_exc_code = vcpu->arch.sie_block->dxc;
147 break;
148 case PGM_PROTECTION:
149 pgm_info->trans_exc_code = vcpu->arch.sie_block->tecmc;
150 pgm_info->exc_access_id = vcpu->arch.sie_block->eai;
151 break;
152 default:
153 break;
154 }
155
156 if (vcpu->arch.sie_block->iprcc & PGM_PER) {
157 pgm_info->per_code = vcpu->arch.sie_block->perc;
158 pgm_info->per_atmid = vcpu->arch.sie_block->peratmid;
159 pgm_info->per_address = vcpu->arch.sie_block->peraddr;
160 pgm_info->per_access_id = vcpu->arch.sie_block->peraid;
161 }
162}
163
e325fe69
MM
164/*
165 * restore ITDB to program-interruption TDB in guest lowcore
166 * and set TX abort indication if required
167*/
168static int handle_itdb(struct kvm_vcpu *vcpu)
169{
170 struct kvm_s390_itdb *itdb;
171 int rc;
172
173 if (!IS_TE_ENABLED(vcpu) || !IS_ITDB_VALID(vcpu))
174 return 0;
175 if (current->thread.per_flags & PER_FLAG_NO_TE)
176 return 0;
177 itdb = (struct kvm_s390_itdb *)vcpu->arch.sie_block->itdba;
178 rc = write_guest_lc(vcpu, __LC_PGM_TDB, itdb, sizeof(*itdb));
179 if (rc)
180 return rc;
181 memset(itdb, 0, sizeof(*itdb));
182
183 return 0;
184}
185
27291e21
DH
186#define per_event(vcpu) (vcpu->arch.sie_block->iprcc & PGM_PER)
187
ba5c1e9b
CO
188static int handle_prog(struct kvm_vcpu *vcpu)
189{
439716a5 190 struct kvm_s390_pgm_info pgm_info;
684135e0 191 psw_t psw;
0040e7d2
HC
192 int rc;
193
ba5c1e9b 194 vcpu->stat.exit_program_interruption++;
7feb6bb8 195
27291e21
DH
196 if (guestdbg_enabled(vcpu) && per_event(vcpu)) {
197 kvm_s390_handle_per_event(vcpu);
198 /* the interrupt might have been filtered out completely */
199 if (vcpu->arch.sie_block->iprcc == 0)
200 return 0;
201 }
202
e325fe69 203 trace_kvm_s390_intercept_prog(vcpu, vcpu->arch.sie_block->iprcc);
684135e0
TH
204 if (vcpu->arch.sie_block->iprcc == PGM_SPECIFICATION) {
205 rc = read_guest_lc(vcpu, __LC_PGM_NEW_PSW, &psw, sizeof(psw_t));
206 if (rc)
207 return rc;
208 /* Avoid endless loops of specification exceptions */
209 if (!is_valid_psw(&psw))
210 return -EOPNOTSUPP;
211 }
e325fe69 212 rc = handle_itdb(vcpu);
0040e7d2
HC
213 if (rc)
214 return rc;
439716a5 215
e325fe69 216 __extract_prog_irq(vcpu, &pgm_info);
439716a5 217 return kvm_s390_inject_prog_irq(vcpu, &pgm_info);
ba5c1e9b
CO
218}
219
220static int handle_instruction_and_prog(struct kvm_vcpu *vcpu)
221{
222 int rc, rc2;
223
224 vcpu->stat.exit_instr_and_program++;
225 rc = handle_instruction(vcpu);
226 rc2 = handle_prog(vcpu);
227
b8e660b8 228 if (rc == -EOPNOTSUPP)
ba5c1e9b
CO
229 vcpu->arch.sie_block->icptcode = 0x04;
230 if (rc)
231 return rc;
232 return rc2;
233}
234
f14d82e0
TH
235/**
236 * handle_external_interrupt - used for external interruption interceptions
237 *
238 * This interception only occurs if the CPUSTAT_EXT_INT bit was set, or if
239 * the new PSW does not have external interrupts disabled. In the first case,
240 * we've got to deliver the interrupt manually, and in the second case, we
241 * drop to userspace to handle the situation there.
242 */
243static int handle_external_interrupt(struct kvm_vcpu *vcpu)
244{
245 u16 eic = vcpu->arch.sie_block->eic;
246 struct kvm_s390_interrupt irq;
247 psw_t newpsw;
248 int rc;
249
250 vcpu->stat.exit_external_interrupt++;
251
252 rc = read_guest_lc(vcpu, __LC_EXT_NEW_PSW, &newpsw, sizeof(psw_t));
253 if (rc)
254 return rc;
255 /* We can not handle clock comparator or timer interrupt with bad PSW */
256 if ((eic == EXT_IRQ_CLK_COMP || eic == EXT_IRQ_CPU_TIMER) &&
257 (newpsw.mask & PSW_MASK_EXT))
258 return -EOPNOTSUPP;
259
260 switch (eic) {
261 case EXT_IRQ_CLK_COMP:
262 irq.type = KVM_S390_INT_CLOCK_COMP;
263 break;
264 case EXT_IRQ_CPU_TIMER:
265 irq.type = KVM_S390_INT_CPU_TIMER;
266 break;
267 case EXT_IRQ_EXTERNAL_CALL:
4953919f
DH
268 if (kvm_s390_si_ext_call_pending(vcpu))
269 return 0;
f14d82e0
TH
270 irq.type = KVM_S390_INT_EXTERNAL_CALL;
271 irq.parm = vcpu->arch.sie_block->extcpuaddr;
272 break;
273 default:
274 return -EOPNOTSUPP;
275 }
276
277 return kvm_s390_inject_vcpu(vcpu, &irq);
278}
279
9a558ee3
TH
280/**
281 * Handle MOVE PAGE partial execution interception.
282 *
283 * This interception can only happen for guests with DAT disabled and
284 * addresses that are currently not mapped in the host. Thus we try to
285 * set up the mappings for the corresponding user pages here (or throw
286 * addressing exceptions in case of illegal guest addresses).
287 */
288static int handle_mvpg_pei(struct kvm_vcpu *vcpu)
289{
9a558ee3 290 psw_t *psw = &vcpu->arch.sie_block->gpsw;
f22166dc 291 unsigned long srcaddr, dstaddr;
9a558ee3
TH
292 int reg1, reg2, rc;
293
294 kvm_s390_get_regs_rre(vcpu, &reg1, &reg2);
9a558ee3
TH
295
296 /* Make sure that the source is paged-in */
f22166dc
TH
297 srcaddr = kvm_s390_real_to_abs(vcpu, vcpu->run->s.regs.gprs[reg2]);
298 if (kvm_is_error_gpa(vcpu->kvm, srcaddr))
9a558ee3 299 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
f22166dc
TH
300 rc = kvm_arch_fault_in_page(vcpu, srcaddr, 0);
301 if (rc != 0)
9a558ee3
TH
302 return rc;
303
304 /* Make sure that the destination is paged-in */
f22166dc
TH
305 dstaddr = kvm_s390_real_to_abs(vcpu, vcpu->run->s.regs.gprs[reg1]);
306 if (kvm_is_error_gpa(vcpu->kvm, dstaddr))
9a558ee3 307 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
f22166dc
TH
308 rc = kvm_arch_fault_in_page(vcpu, dstaddr, 1);
309 if (rc != 0)
9a558ee3
TH
310 return rc;
311
312 psw->addr = __rewind_psw(*psw, 4);
313
314 return 0;
315}
316
317static int handle_partial_execution(struct kvm_vcpu *vcpu)
318{
319 if (vcpu->arch.sie_block->ipa == 0xb254) /* MVPG */
320 return handle_mvpg_pei(vcpu);
4953919f
DH
321 if (vcpu->arch.sie_block->ipa >> 8 == 0xae) /* SIGP */
322 return kvm_s390_handle_sigp_pei(vcpu);
9a558ee3
TH
323
324 return -EOPNOTSUPP;
325}
326
062d5e9b 327static const intercept_handler_t intercept_funcs[] = {
8f2abe6a 328 [0x00 >> 2] = handle_noop,
ba5c1e9b
CO
329 [0x04 >> 2] = handle_instruction,
330 [0x08 >> 2] = handle_prog,
331 [0x0C >> 2] = handle_instruction_and_prog,
8f2abe6a 332 [0x10 >> 2] = handle_noop,
f14d82e0 333 [0x14 >> 2] = handle_external_interrupt,
fa6b7fe9 334 [0x18 >> 2] = handle_noop,
ba5c1e9b 335 [0x1C >> 2] = kvm_s390_handle_wait,
8f2abe6a
CB
336 [0x20 >> 2] = handle_validity,
337 [0x28 >> 2] = handle_stop,
9a558ee3 338 [0x38 >> 2] = handle_partial_execution,
8f2abe6a
CB
339};
340
341int kvm_handle_sie_intercept(struct kvm_vcpu *vcpu)
342{
343 intercept_handler_t func;
344 u8 code = vcpu->arch.sie_block->icptcode;
345
062d5e9b 346 if (code & 3 || (code >> 2) >= ARRAY_SIZE(intercept_funcs))
b8e660b8 347 return -EOPNOTSUPP;
8f2abe6a
CB
348 func = intercept_funcs[code >> 2];
349 if (func)
350 return func(vcpu);
b8e660b8 351 return -EOPNOTSUPP;
8f2abe6a 352}