blob: 22798ec33fd16bd58e5a9726a6f5eec41d798d6a [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;
29 int base2 = vcpu->arch.sie_block->ipb >> 28;
30 int disp2 = ((vcpu->arch.sie_block->ipb & 0x0fff0000) >> 16) +
31 ((vcpu->arch.sie_block->ipb & 0xff00) << 4);
32 u64 useraddr;
33 int reg, rc;
34
Christian Borntraegerf5e10b02008-07-25 15:52:44 +020035 vcpu->stat.instruction_lctlg++;
Carsten Otteba5c1e92008-03-25 18:47:26 +010036 if ((vcpu->arch.sie_block->ipb & 0xff) != 0x2f)
Heiko Carstensb8e660b2010-02-26 22:37:41 +010037 return -EOPNOTSUPP;
Carsten Otteba5c1e92008-03-25 18:47:26 +010038
39 useraddr = disp2;
40 if (base2)
Christian Borntraeger5a32c1a2012-01-11 11:20:32 +010041 useraddr += vcpu->run->s.regs.gprs[base2];
Carsten Otteba5c1e92008-03-25 18:47:26 +010042
Christian Borntraeger5a00a5e2008-07-25 15:53:12 +020043 if (useraddr & 7)
44 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
45
Carsten Otteba5c1e92008-03-25 18:47:26 +010046 reg = reg1;
47
Christian Borntraegerf5e10b02008-07-25 15:52:44 +020048 VCPU_EVENT(vcpu, 5, "lctlg r1:%x, r3:%x,b2:%x,d2:%x", reg1, reg3, base2,
Carsten Otteba5c1e92008-03-25 18:47:26 +010049 disp2);
Cornelia Huck5786fff2012-07-23 17:20:29 +020050 trace_kvm_s390_handle_lctl(vcpu, 1, reg1, reg3, useraddr);
Carsten Otteba5c1e92008-03-25 18:47:26 +010051
52 do {
53 rc = get_guest_u64(vcpu, useraddr,
54 &vcpu->arch.sie_block->gcr[reg]);
55 if (rc == -EFAULT) {
56 kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
57 break;
58 }
59 useraddr += 8;
60 if (reg == reg3)
61 break;
62 reg = (reg + 1) % 16;
63 } while (1);
64 return 0;
65}
66
67static int handle_lctl(struct kvm_vcpu *vcpu)
68{
69 int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
70 int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
71 int base2 = vcpu->arch.sie_block->ipb >> 28;
72 int disp2 = ((vcpu->arch.sie_block->ipb & 0x0fff0000) >> 16);
73 u64 useraddr;
74 u32 val = 0;
75 int reg, rc;
76
77 vcpu->stat.instruction_lctl++;
78
79 useraddr = disp2;
80 if (base2)
Christian Borntraeger5a32c1a2012-01-11 11:20:32 +010081 useraddr += vcpu->run->s.regs.gprs[base2];
Carsten Otteba5c1e92008-03-25 18:47:26 +010082
Christian Borntraeger5a00a5e2008-07-25 15:53:12 +020083 if (useraddr & 3)
84 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
85
Carsten Otteba5c1e92008-03-25 18:47:26 +010086 VCPU_EVENT(vcpu, 5, "lctl r1:%x, r3:%x,b2:%x,d2:%x", reg1, reg3, base2,
87 disp2);
Cornelia Huck5786fff2012-07-23 17:20:29 +020088 trace_kvm_s390_handle_lctl(vcpu, 0, reg1, reg3, useraddr);
Carsten Otteba5c1e92008-03-25 18:47:26 +010089
90 reg = reg1;
91 do {
92 rc = get_guest_u32(vcpu, useraddr, &val);
93 if (rc == -EFAULT) {
94 kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
95 break;
96 }
97 vcpu->arch.sie_block->gcr[reg] &= 0xffffffff00000000ul;
98 vcpu->arch.sie_block->gcr[reg] |= val;
99 useraddr += 4;
100 if (reg == reg3)
101 break;
102 reg = (reg + 1) % 16;
103 } while (1);
104 return 0;
105}
106
107static intercept_handler_t instruction_handlers[256] = {
Cornelia Huck8c3f61e2012-04-24 09:24:44 +0200108 [0x01] = kvm_s390_handle_01,
Christian Borntraegere28acfe2008-03-25 18:47:34 +0100109 [0x83] = kvm_s390_handle_diag,
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100110 [0xae] = kvm_s390_handle_sigp,
Christian Borntraeger70455a32009-01-22 10:28:29 +0100111 [0xb2] = kvm_s390_handle_b2,
Carsten Otteba5c1e92008-03-25 18:47:26 +0100112 [0xb7] = handle_lctl,
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +0200113 [0xe5] = kvm_s390_handle_e5,
Christian Borntraegerf5e10b02008-07-25 15:52:44 +0200114 [0xeb] = handle_lctlg,
Carsten Otteba5c1e92008-03-25 18:47:26 +0100115};
Christian Borntraeger8f2abe62008-03-25 18:47:23 +0100116
117static int handle_noop(struct kvm_vcpu *vcpu)
118{
119 switch (vcpu->arch.sie_block->icptcode) {
Christian Borntraeger0eaeafa2008-05-07 09:22:53 +0200120 case 0x0:
121 vcpu->stat.exit_null++;
122 break;
Christian Borntraeger8f2abe62008-03-25 18:47:23 +0100123 case 0x10:
124 vcpu->stat.exit_external_request++;
125 break;
126 case 0x14:
127 vcpu->stat.exit_external_interrupt++;
128 break;
129 default:
130 break; /* nothing */
131 }
132 return 0;
133}
134
135static int handle_stop(struct kvm_vcpu *vcpu)
136{
Christian Ehrhardt9ace9032009-05-20 15:34:55 +0200137 int rc = 0;
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100138
Christian Borntraeger8f2abe62008-03-25 18:47:23 +0100139 vcpu->stat.exit_stop_request++;
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100140 spin_lock_bh(&vcpu->arch.local_int.lock);
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100141
Cornelia Huckade38c32012-07-23 17:20:30 +0200142 trace_kvm_s390_stop_request(vcpu->arch.local_int.action_bits);
143
Christian Ehrhardt9ace9032009-05-20 15:34:55 +0200144 if (vcpu->arch.local_int.action_bits & ACTION_RELOADVCPU_ON_STOP) {
145 vcpu->arch.local_int.action_bits &= ~ACTION_RELOADVCPU_ON_STOP;
146 rc = SIE_INTERCEPT_RERUNVCPU;
147 vcpu->run->exit_reason = KVM_EXIT_INTR;
148 }
149
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100150 if (vcpu->arch.local_int.action_bits & ACTION_STOP_ON_STOP) {
Cornelia Huck9e6dabe2011-11-17 11:00:41 +0100151 atomic_set_mask(CPUSTAT_STOPPED,
152 &vcpu->arch.sie_block->cpuflags);
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100153 vcpu->arch.local_int.action_bits &= ~ACTION_STOP_ON_STOP;
154 VCPU_EVENT(vcpu, 3, "%s", "cpu stopped");
Heiko Carstensb8e660b2010-02-26 22:37:41 +0100155 rc = -EOPNOTSUPP;
Christian Ehrhardt9ace9032009-05-20 15:34:55 +0200156 }
157
Jens Freimann9e0d5472012-02-06 10:59:03 +0100158 if (vcpu->arch.local_int.action_bits & ACTION_STORE_ON_STOP) {
159 vcpu->arch.local_int.action_bits &= ~ACTION_STORE_ON_STOP;
160 /* store status must be called unlocked. Since local_int.lock
161 * only protects local_int.* and not guest memory we can give
162 * up the lock here */
163 spin_unlock_bh(&vcpu->arch.local_int.lock);
164 rc = kvm_s390_vcpu_store_status(vcpu,
165 KVM_S390_STORE_STATUS_NOADDR);
166 if (rc >= 0)
167 rc = -EOPNOTSUPP;
168 } else
169 spin_unlock_bh(&vcpu->arch.local_int.lock);
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100170 return rc;
Christian Borntraeger8f2abe62008-03-25 18:47:23 +0100171}
172
173static int handle_validity(struct kvm_vcpu *vcpu)
174{
Carsten Otte598841c2011-07-24 10:48:21 +0200175 unsigned long vmaddr;
Christian Borntraeger8f2abe62008-03-25 18:47:23 +0100176 int viwhy = vcpu->arch.sie_block->ipb >> 16;
Carsten Otte3edbcff2009-05-12 17:21:52 +0200177 int rc;
178
Christian Borntraeger8f2abe62008-03-25 18:47:23 +0100179 vcpu->stat.exit_validity++;
Cornelia Huck5786fff2012-07-23 17:20:29 +0200180 trace_kvm_s390_intercept_validity(vcpu, viwhy);
Carsten Otte092670c2011-07-24 10:48:22 +0200181 if (viwhy == 0x37) {
Carsten Otte598841c2011-07-24 10:48:21 +0200182 vmaddr = gmap_fault(vcpu->arch.sie_block->prefix,
183 vcpu->arch.gmap);
184 if (IS_ERR_VALUE(vmaddr)) {
185 rc = -EOPNOTSUPP;
186 goto out;
187 }
Carsten Otte092670c2011-07-24 10:48:22 +0200188 rc = fault_in_pages_writeable((char __user *) vmaddr,
189 PAGE_SIZE);
190 if (rc) {
191 /* user will receive sigsegv, exit to user */
192 rc = -EOPNOTSUPP;
193 goto out;
194 }
Carsten Otte598841c2011-07-24 10:48:21 +0200195 vmaddr = gmap_fault(vcpu->arch.sie_block->prefix + PAGE_SIZE,
196 vcpu->arch.gmap);
197 if (IS_ERR_VALUE(vmaddr)) {
198 rc = -EOPNOTSUPP;
199 goto out;
200 }
Carsten Otte092670c2011-07-24 10:48:22 +0200201 rc = fault_in_pages_writeable((char __user *) vmaddr,
202 PAGE_SIZE);
203 if (rc) {
204 /* user will receive sigsegv, exit to user */
205 rc = -EOPNOTSUPP;
206 goto out;
207 }
Carsten Otte3edbcff2009-05-12 17:21:52 +0200208 } else
Heiko Carstensb8e660b2010-02-26 22:37:41 +0100209 rc = -EOPNOTSUPP;
Carsten Otte3edbcff2009-05-12 17:21:52 +0200210
Carsten Otte598841c2011-07-24 10:48:21 +0200211out:
Carsten Otte3edbcff2009-05-12 17:21:52 +0200212 if (rc)
213 VCPU_EVENT(vcpu, 2, "unhandled validity intercept code %d",
214 viwhy);
215 return rc;
Christian Borntraeger8f2abe62008-03-25 18:47:23 +0100216}
217
Carsten Otteba5c1e92008-03-25 18:47:26 +0100218static int handle_instruction(struct kvm_vcpu *vcpu)
219{
220 intercept_handler_t handler;
221
222 vcpu->stat.exit_instruction++;
Cornelia Huck5786fff2012-07-23 17:20:29 +0200223 trace_kvm_s390_intercept_instruction(vcpu,
224 vcpu->arch.sie_block->ipa,
225 vcpu->arch.sie_block->ipb);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100226 handler = instruction_handlers[vcpu->arch.sie_block->ipa >> 8];
227 if (handler)
228 return handler(vcpu);
Heiko Carstensb8e660b2010-02-26 22:37:41 +0100229 return -EOPNOTSUPP;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100230}
231
232static int handle_prog(struct kvm_vcpu *vcpu)
233{
234 vcpu->stat.exit_program_interruption++;
Cornelia Huck5786fff2012-07-23 17:20:29 +0200235 trace_kvm_s390_intercept_prog(vcpu, vcpu->arch.sie_block->iprcc);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100236 return kvm_s390_inject_program_int(vcpu, vcpu->arch.sie_block->iprcc);
237}
238
239static int handle_instruction_and_prog(struct kvm_vcpu *vcpu)
240{
241 int rc, rc2;
242
243 vcpu->stat.exit_instr_and_program++;
244 rc = handle_instruction(vcpu);
245 rc2 = handle_prog(vcpu);
246
Heiko Carstensb8e660b2010-02-26 22:37:41 +0100247 if (rc == -EOPNOTSUPP)
Carsten Otteba5c1e92008-03-25 18:47:26 +0100248 vcpu->arch.sie_block->icptcode = 0x04;
249 if (rc)
250 return rc;
251 return rc2;
252}
253
Christian Borntraeger062d5e92010-01-21 12:19:07 +0100254static const intercept_handler_t intercept_funcs[] = {
Christian Borntraeger8f2abe62008-03-25 18:47:23 +0100255 [0x00 >> 2] = handle_noop,
Carsten Otteba5c1e92008-03-25 18:47:26 +0100256 [0x04 >> 2] = handle_instruction,
257 [0x08 >> 2] = handle_prog,
258 [0x0C >> 2] = handle_instruction_and_prog,
Christian Borntraeger8f2abe62008-03-25 18:47:23 +0100259 [0x10 >> 2] = handle_noop,
260 [0x14 >> 2] = handle_noop,
Carsten Otteba5c1e92008-03-25 18:47:26 +0100261 [0x1C >> 2] = kvm_s390_handle_wait,
Christian Borntraeger8f2abe62008-03-25 18:47:23 +0100262 [0x20 >> 2] = handle_validity,
263 [0x28 >> 2] = handle_stop,
264};
265
266int kvm_handle_sie_intercept(struct kvm_vcpu *vcpu)
267{
268 intercept_handler_t func;
269 u8 code = vcpu->arch.sie_block->icptcode;
270
Christian Borntraeger062d5e92010-01-21 12:19:07 +0100271 if (code & 3 || (code >> 2) >= ARRAY_SIZE(intercept_funcs))
Heiko Carstensb8e660b2010-02-26 22:37:41 +0100272 return -EOPNOTSUPP;
Christian Borntraeger8f2abe62008-03-25 18:47:23 +0100273 func = intercept_funcs[code >> 2];
274 if (func)
275 return func(vcpu);
Heiko Carstensb8e660b2010-02-26 22:37:41 +0100276 return -EOPNOTSUPP;
Christian Borntraeger8f2abe62008-03-25 18:47:23 +0100277}