blob: 71af87dbb42c045227e0d2feeecdb27af40af1a8 [file] [log] [blame]
Christian Borntraeger8f2abe62008-03-25 18:47:23 +01001/*
Heiko Carstensa53c8fa2012-07-20 11:15:04 +02002 * in-kernel handling for sie intercepts
Christian Borntraeger8f2abe62008-03-25 18:47:23 +01003 *
Heiko Carstensa53c8fa2012-07-20 11:15:04 +02004 * Copyright IBM Corp. 2008, 2009
Christian Borntraeger8f2abe62008-03-25 18:47:23 +01005 *
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>
19
20#include "kvm-s390.h"
Carsten Otteba5c1e92008-03-25 18:47:26 +010021#include "gaccess.h"
Cornelia Huck5786fff2012-07-23 17:20:29 +020022#include "trace.h"
Cornelia Huckade38c32012-07-23 17:20:30 +020023#include "trace-s390.h"
Carsten Otteba5c1e92008-03-25 18:47:26 +010024
Christian Borntraegerf5e10b02008-07-25 15:52:44 +020025static int handle_lctlg(struct kvm_vcpu *vcpu)
Carsten Otteba5c1e92008-03-25 18:47:26 +010026{
27 int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
28 int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
Carsten Otteba5c1e92008-03-25 18:47:26 +010029 u64 useraddr;
30 int reg, rc;
31
Christian Borntraegerf5e10b02008-07-25 15:52:44 +020032 vcpu->stat.instruction_lctlg++;
Carsten Otteba5c1e92008-03-25 18:47:26 +010033
Cornelia Huckb1c571a2012-12-20 15:32:07 +010034 useraddr = kvm_s390_get_base_disp_rsy(vcpu);
Carsten Otteba5c1e92008-03-25 18:47:26 +010035
Christian Borntraeger5a00a5e2008-07-25 15:53:12 +020036 if (useraddr & 7)
37 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
38
Carsten Otteba5c1e92008-03-25 18:47:26 +010039 reg = reg1;
40
Cornelia Huckb1c571a2012-12-20 15:32:07 +010041 VCPU_EVENT(vcpu, 5, "lctlg r1:%x, r3:%x, addr:%llx", reg1, reg3,
42 useraddr);
Cornelia Huck5786fff2012-07-23 17:20:29 +020043 trace_kvm_s390_handle_lctl(vcpu, 1, reg1, reg3, useraddr);
Carsten Otteba5c1e92008-03-25 18:47:26 +010044
45 do {
46 rc = get_guest_u64(vcpu, useraddr,
47 &vcpu->arch.sie_block->gcr[reg]);
48 if (rc == -EFAULT) {
49 kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
50 break;
51 }
52 useraddr += 8;
53 if (reg == reg3)
54 break;
55 reg = (reg + 1) % 16;
56 } while (1);
57 return 0;
58}
59
60static int handle_lctl(struct kvm_vcpu *vcpu)
61{
62 int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
63 int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
Carsten Otteba5c1e92008-03-25 18:47:26 +010064 u64 useraddr;
65 u32 val = 0;
66 int reg, rc;
67
68 vcpu->stat.instruction_lctl++;
69
Cornelia Huckb1c571a2012-12-20 15:32:07 +010070 useraddr = kvm_s390_get_base_disp_rs(vcpu);
Carsten Otteba5c1e92008-03-25 18:47:26 +010071
Christian Borntraeger5a00a5e2008-07-25 15:53:12 +020072 if (useraddr & 3)
73 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
74
Cornelia Huckb1c571a2012-12-20 15:32:07 +010075 VCPU_EVENT(vcpu, 5, "lctl r1:%x, r3:%x, addr:%llx", reg1, reg3,
76 useraddr);
Cornelia Huck5786fff2012-07-23 17:20:29 +020077 trace_kvm_s390_handle_lctl(vcpu, 0, reg1, reg3, useraddr);
Carsten Otteba5c1e92008-03-25 18:47:26 +010078
79 reg = reg1;
80 do {
81 rc = get_guest_u32(vcpu, useraddr, &val);
82 if (rc == -EFAULT) {
83 kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
84 break;
85 }
86 vcpu->arch.sie_block->gcr[reg] &= 0xffffffff00000000ul;
87 vcpu->arch.sie_block->gcr[reg] |= val;
88 useraddr += 4;
89 if (reg == reg3)
90 break;
91 reg = (reg + 1) % 16;
92 } while (1);
93 return 0;
94}
95
Cornelia Huckf379aae2012-12-20 15:32:10 +010096static const intercept_handler_t eb_handlers[256] = {
97 [0x2f] = handle_lctlg,
98 [0x8a] = kvm_s390_handle_priv_eb,
99};
100
101static int handle_eb(struct kvm_vcpu *vcpu)
102{
103 intercept_handler_t handler;
104
105 handler = eb_handlers[vcpu->arch.sie_block->ipb & 0xff];
106 if (handler)
107 return handler(vcpu);
108 return -EOPNOTSUPP;
109}
110
Cornelia Huck77975352012-12-20 15:32:06 +0100111static const intercept_handler_t instruction_handlers[256] = {
Cornelia Huck8c3f61e2012-04-24 09:24:44 +0200112 [0x01] = kvm_s390_handle_01,
Cornelia Huck48a3e952012-12-20 15:32:09 +0100113 [0x82] = kvm_s390_handle_lpsw,
Christian Borntraegere28acfe2008-03-25 18:47:34 +0100114 [0x83] = kvm_s390_handle_diag,
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100115 [0xae] = kvm_s390_handle_sigp,
Christian Borntraeger70455a32009-01-22 10:28:29 +0100116 [0xb2] = kvm_s390_handle_b2,
Carsten Otteba5c1e92008-03-25 18:47:26 +0100117 [0xb7] = handle_lctl,
Cornelia Huck48a3e952012-12-20 15:32:09 +0100118 [0xb9] = kvm_s390_handle_b9,
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +0200119 [0xe5] = kvm_s390_handle_e5,
Cornelia Huckf379aae2012-12-20 15:32:10 +0100120 [0xeb] = handle_eb,
Carsten Otteba5c1e92008-03-25 18:47:26 +0100121};
Christian Borntraeger8f2abe62008-03-25 18:47:23 +0100122
123static int handle_noop(struct kvm_vcpu *vcpu)
124{
125 switch (vcpu->arch.sie_block->icptcode) {
Christian Borntraeger0eaeafa2008-05-07 09:22:53 +0200126 case 0x0:
127 vcpu->stat.exit_null++;
128 break;
Christian Borntraeger8f2abe62008-03-25 18:47:23 +0100129 case 0x10:
130 vcpu->stat.exit_external_request++;
131 break;
132 case 0x14:
133 vcpu->stat.exit_external_interrupt++;
134 break;
135 default:
136 break; /* nothing */
137 }
138 return 0;
139}
140
141static int handle_stop(struct kvm_vcpu *vcpu)
142{
Christian Ehrhardt9ace9032009-05-20 15:34:55 +0200143 int rc = 0;
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100144
Christian Borntraeger8f2abe62008-03-25 18:47:23 +0100145 vcpu->stat.exit_stop_request++;
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100146 spin_lock_bh(&vcpu->arch.local_int.lock);
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100147
Cornelia Huckade38c32012-07-23 17:20:30 +0200148 trace_kvm_s390_stop_request(vcpu->arch.local_int.action_bits);
149
Christian Ehrhardt9ace9032009-05-20 15:34:55 +0200150 if (vcpu->arch.local_int.action_bits & ACTION_RELOADVCPU_ON_STOP) {
151 vcpu->arch.local_int.action_bits &= ~ACTION_RELOADVCPU_ON_STOP;
152 rc = SIE_INTERCEPT_RERUNVCPU;
153 vcpu->run->exit_reason = KVM_EXIT_INTR;
154 }
155
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100156 if (vcpu->arch.local_int.action_bits & ACTION_STOP_ON_STOP) {
Cornelia Huck9e6dabe2011-11-17 11:00:41 +0100157 atomic_set_mask(CPUSTAT_STOPPED,
158 &vcpu->arch.sie_block->cpuflags);
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100159 vcpu->arch.local_int.action_bits &= ~ACTION_STOP_ON_STOP;
160 VCPU_EVENT(vcpu, 3, "%s", "cpu stopped");
Heiko Carstensb8e660b2010-02-26 22:37:41 +0100161 rc = -EOPNOTSUPP;
Christian Ehrhardt9ace9032009-05-20 15:34:55 +0200162 }
163
Jens Freimann9e0d5472012-02-06 10:59:03 +0100164 if (vcpu->arch.local_int.action_bits & ACTION_STORE_ON_STOP) {
165 vcpu->arch.local_int.action_bits &= ~ACTION_STORE_ON_STOP;
166 /* store status must be called unlocked. Since local_int.lock
167 * only protects local_int.* and not guest memory we can give
168 * up the lock here */
169 spin_unlock_bh(&vcpu->arch.local_int.lock);
170 rc = kvm_s390_vcpu_store_status(vcpu,
171 KVM_S390_STORE_STATUS_NOADDR);
172 if (rc >= 0)
173 rc = -EOPNOTSUPP;
174 } else
175 spin_unlock_bh(&vcpu->arch.local_int.lock);
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100176 return rc;
Christian Borntraeger8f2abe62008-03-25 18:47:23 +0100177}
178
179static int handle_validity(struct kvm_vcpu *vcpu)
180{
Carsten Otte598841c2011-07-24 10:48:21 +0200181 unsigned long vmaddr;
Christian Borntraeger8f2abe62008-03-25 18:47:23 +0100182 int viwhy = vcpu->arch.sie_block->ipb >> 16;
Carsten Otte3edbcff2009-05-12 17:21:52 +0200183 int rc;
184
Christian Borntraeger8f2abe62008-03-25 18:47:23 +0100185 vcpu->stat.exit_validity++;
Cornelia Huck5786fff2012-07-23 17:20:29 +0200186 trace_kvm_s390_intercept_validity(vcpu, viwhy);
Carsten Otte092670c2011-07-24 10:48:22 +0200187 if (viwhy == 0x37) {
Carsten Otte598841c2011-07-24 10:48:21 +0200188 vmaddr = gmap_fault(vcpu->arch.sie_block->prefix,
189 vcpu->arch.gmap);
190 if (IS_ERR_VALUE(vmaddr)) {
191 rc = -EOPNOTSUPP;
192 goto out;
193 }
Carsten Otte092670c2011-07-24 10:48:22 +0200194 rc = fault_in_pages_writeable((char __user *) vmaddr,
195 PAGE_SIZE);
196 if (rc) {
197 /* user will receive sigsegv, exit to user */
198 rc = -EOPNOTSUPP;
199 goto out;
200 }
Carsten Otte598841c2011-07-24 10:48:21 +0200201 vmaddr = gmap_fault(vcpu->arch.sie_block->prefix + PAGE_SIZE,
202 vcpu->arch.gmap);
203 if (IS_ERR_VALUE(vmaddr)) {
204 rc = -EOPNOTSUPP;
205 goto out;
206 }
Carsten Otte092670c2011-07-24 10:48:22 +0200207 rc = fault_in_pages_writeable((char __user *) vmaddr,
208 PAGE_SIZE);
209 if (rc) {
210 /* user will receive sigsegv, exit to user */
211 rc = -EOPNOTSUPP;
212 goto out;
213 }
Carsten Otte3edbcff2009-05-12 17:21:52 +0200214 } else
Heiko Carstensb8e660b2010-02-26 22:37:41 +0100215 rc = -EOPNOTSUPP;
Carsten Otte3edbcff2009-05-12 17:21:52 +0200216
Carsten Otte598841c2011-07-24 10:48:21 +0200217out:
Carsten Otte3edbcff2009-05-12 17:21:52 +0200218 if (rc)
219 VCPU_EVENT(vcpu, 2, "unhandled validity intercept code %d",
220 viwhy);
221 return rc;
Christian Borntraeger8f2abe62008-03-25 18:47:23 +0100222}
223
Carsten Otteba5c1e92008-03-25 18:47:26 +0100224static int handle_instruction(struct kvm_vcpu *vcpu)
225{
226 intercept_handler_t handler;
227
228 vcpu->stat.exit_instruction++;
Cornelia Huck5786fff2012-07-23 17:20:29 +0200229 trace_kvm_s390_intercept_instruction(vcpu,
230 vcpu->arch.sie_block->ipa,
231 vcpu->arch.sie_block->ipb);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100232 handler = instruction_handlers[vcpu->arch.sie_block->ipa >> 8];
233 if (handler)
234 return handler(vcpu);
Heiko Carstensb8e660b2010-02-26 22:37:41 +0100235 return -EOPNOTSUPP;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100236}
237
238static int handle_prog(struct kvm_vcpu *vcpu)
239{
240 vcpu->stat.exit_program_interruption++;
Cornelia Huck5786fff2012-07-23 17:20:29 +0200241 trace_kvm_s390_intercept_prog(vcpu, vcpu->arch.sie_block->iprcc);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100242 return kvm_s390_inject_program_int(vcpu, vcpu->arch.sie_block->iprcc);
243}
244
245static int handle_instruction_and_prog(struct kvm_vcpu *vcpu)
246{
247 int rc, rc2;
248
249 vcpu->stat.exit_instr_and_program++;
250 rc = handle_instruction(vcpu);
251 rc2 = handle_prog(vcpu);
252
Heiko Carstensb8e660b2010-02-26 22:37:41 +0100253 if (rc == -EOPNOTSUPP)
Carsten Otteba5c1e92008-03-25 18:47:26 +0100254 vcpu->arch.sie_block->icptcode = 0x04;
255 if (rc)
256 return rc;
257 return rc2;
258}
259
Christian Borntraeger062d5e92010-01-21 12:19:07 +0100260static const intercept_handler_t intercept_funcs[] = {
Christian Borntraeger8f2abe62008-03-25 18:47:23 +0100261 [0x00 >> 2] = handle_noop,
Carsten Otteba5c1e92008-03-25 18:47:26 +0100262 [0x04 >> 2] = handle_instruction,
263 [0x08 >> 2] = handle_prog,
264 [0x0C >> 2] = handle_instruction_and_prog,
Christian Borntraeger8f2abe62008-03-25 18:47:23 +0100265 [0x10 >> 2] = handle_noop,
266 [0x14 >> 2] = handle_noop,
Carsten Otteba5c1e92008-03-25 18:47:26 +0100267 [0x1C >> 2] = kvm_s390_handle_wait,
Christian Borntraeger8f2abe62008-03-25 18:47:23 +0100268 [0x20 >> 2] = handle_validity,
269 [0x28 >> 2] = handle_stop,
270};
271
272int kvm_handle_sie_intercept(struct kvm_vcpu *vcpu)
273{
274 intercept_handler_t func;
275 u8 code = vcpu->arch.sie_block->icptcode;
276
Christian Borntraeger062d5e92010-01-21 12:19:07 +0100277 if (code & 3 || (code >> 2) >= ARRAY_SIZE(intercept_funcs))
Heiko Carstensb8e660b2010-02-26 22:37:41 +0100278 return -EOPNOTSUPP;
Christian Borntraeger8f2abe62008-03-25 18:47:23 +0100279 func = intercept_funcs[code >> 2];
280 if (func)
281 return func(vcpu);
Heiko Carstensb8e660b2010-02-26 22:37:41 +0100282 return -EOPNOTSUPP;
Christian Borntraeger8f2abe62008-03-25 18:47:23 +0100283}