blob: 950c13ecaf607d412d133d7577ec4f51188b663b [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 if ((vcpu->arch.sie_block->ipb & 0xff) != 0x2f)
Heiko Carstensb8e660b2010-02-26 22:37:41 +010034 return -EOPNOTSUPP;
Carsten Otteba5c1e92008-03-25 18:47:26 +010035
Cornelia Huckb1c571a2012-12-20 15:32:07 +010036 useraddr = kvm_s390_get_base_disp_rsy(vcpu);
Carsten Otteba5c1e92008-03-25 18:47:26 +010037
Christian Borntraeger5a00a5e2008-07-25 15:53:12 +020038 if (useraddr & 7)
39 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
40
Carsten Otteba5c1e92008-03-25 18:47:26 +010041 reg = reg1;
42
Cornelia Huckb1c571a2012-12-20 15:32:07 +010043 VCPU_EVENT(vcpu, 5, "lctlg r1:%x, r3:%x, addr:%llx", reg1, reg3,
44 useraddr);
Cornelia Huck5786fff2012-07-23 17:20:29 +020045 trace_kvm_s390_handle_lctl(vcpu, 1, reg1, reg3, useraddr);
Carsten Otteba5c1e92008-03-25 18:47:26 +010046
47 do {
48 rc = get_guest_u64(vcpu, useraddr,
49 &vcpu->arch.sie_block->gcr[reg]);
50 if (rc == -EFAULT) {
51 kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
52 break;
53 }
54 useraddr += 8;
55 if (reg == reg3)
56 break;
57 reg = (reg + 1) % 16;
58 } while (1);
59 return 0;
60}
61
62static int handle_lctl(struct kvm_vcpu *vcpu)
63{
64 int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
65 int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
Carsten Otteba5c1e92008-03-25 18:47:26 +010066 u64 useraddr;
67 u32 val = 0;
68 int reg, rc;
69
70 vcpu->stat.instruction_lctl++;
71
Cornelia Huckb1c571a2012-12-20 15:32:07 +010072 useraddr = kvm_s390_get_base_disp_rs(vcpu);
Carsten Otteba5c1e92008-03-25 18:47:26 +010073
Christian Borntraeger5a00a5e2008-07-25 15:53:12 +020074 if (useraddr & 3)
75 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
76
Cornelia Huckb1c571a2012-12-20 15:32:07 +010077 VCPU_EVENT(vcpu, 5, "lctl r1:%x, r3:%x, addr:%llx", reg1, reg3,
78 useraddr);
Cornelia Huck5786fff2012-07-23 17:20:29 +020079 trace_kvm_s390_handle_lctl(vcpu, 0, reg1, reg3, useraddr);
Carsten Otteba5c1e92008-03-25 18:47:26 +010080
81 reg = reg1;
82 do {
83 rc = get_guest_u32(vcpu, useraddr, &val);
84 if (rc == -EFAULT) {
85 kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
86 break;
87 }
88 vcpu->arch.sie_block->gcr[reg] &= 0xffffffff00000000ul;
89 vcpu->arch.sie_block->gcr[reg] |= val;
90 useraddr += 4;
91 if (reg == reg3)
92 break;
93 reg = (reg + 1) % 16;
94 } while (1);
95 return 0;
96}
97
Cornelia Huck77975352012-12-20 15:32:06 +010098static const intercept_handler_t instruction_handlers[256] = {
Cornelia Huck8c3f61e2012-04-24 09:24:44 +020099 [0x01] = kvm_s390_handle_01,
Cornelia Huck48a3e952012-12-20 15:32:09 +0100100 [0x82] = kvm_s390_handle_lpsw,
Christian Borntraegere28acfe2008-03-25 18:47:34 +0100101 [0x83] = kvm_s390_handle_diag,
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100102 [0xae] = kvm_s390_handle_sigp,
Christian Borntraeger70455a32009-01-22 10:28:29 +0100103 [0xb2] = kvm_s390_handle_b2,
Carsten Otteba5c1e92008-03-25 18:47:26 +0100104 [0xb7] = handle_lctl,
Cornelia Huck48a3e952012-12-20 15:32:09 +0100105 [0xb9] = kvm_s390_handle_b9,
Christian Borntraegerbb25b9b2011-07-24 10:48:17 +0200106 [0xe5] = kvm_s390_handle_e5,
Christian Borntraegerf5e10b02008-07-25 15:52:44 +0200107 [0xeb] = handle_lctlg,
Carsten Otteba5c1e92008-03-25 18:47:26 +0100108};
Christian Borntraeger8f2abe62008-03-25 18:47:23 +0100109
110static int handle_noop(struct kvm_vcpu *vcpu)
111{
112 switch (vcpu->arch.sie_block->icptcode) {
Christian Borntraeger0eaeafa2008-05-07 09:22:53 +0200113 case 0x0:
114 vcpu->stat.exit_null++;
115 break;
Christian Borntraeger8f2abe62008-03-25 18:47:23 +0100116 case 0x10:
117 vcpu->stat.exit_external_request++;
118 break;
119 case 0x14:
120 vcpu->stat.exit_external_interrupt++;
121 break;
122 default:
123 break; /* nothing */
124 }
125 return 0;
126}
127
128static int handle_stop(struct kvm_vcpu *vcpu)
129{
Christian Ehrhardt9ace9032009-05-20 15:34:55 +0200130 int rc = 0;
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100131
Christian Borntraeger8f2abe62008-03-25 18:47:23 +0100132 vcpu->stat.exit_stop_request++;
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100133 spin_lock_bh(&vcpu->arch.local_int.lock);
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100134
Cornelia Huckade38c32012-07-23 17:20:30 +0200135 trace_kvm_s390_stop_request(vcpu->arch.local_int.action_bits);
136
Christian Ehrhardt9ace9032009-05-20 15:34:55 +0200137 if (vcpu->arch.local_int.action_bits & ACTION_RELOADVCPU_ON_STOP) {
138 vcpu->arch.local_int.action_bits &= ~ACTION_RELOADVCPU_ON_STOP;
139 rc = SIE_INTERCEPT_RERUNVCPU;
140 vcpu->run->exit_reason = KVM_EXIT_INTR;
141 }
142
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100143 if (vcpu->arch.local_int.action_bits & ACTION_STOP_ON_STOP) {
Cornelia Huck9e6dabe2011-11-17 11:00:41 +0100144 atomic_set_mask(CPUSTAT_STOPPED,
145 &vcpu->arch.sie_block->cpuflags);
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100146 vcpu->arch.local_int.action_bits &= ~ACTION_STOP_ON_STOP;
147 VCPU_EVENT(vcpu, 3, "%s", "cpu stopped");
Heiko Carstensb8e660b2010-02-26 22:37:41 +0100148 rc = -EOPNOTSUPP;
Christian Ehrhardt9ace9032009-05-20 15:34:55 +0200149 }
150
Jens Freimann9e0d5472012-02-06 10:59:03 +0100151 if (vcpu->arch.local_int.action_bits & ACTION_STORE_ON_STOP) {
152 vcpu->arch.local_int.action_bits &= ~ACTION_STORE_ON_STOP;
153 /* store status must be called unlocked. Since local_int.lock
154 * only protects local_int.* and not guest memory we can give
155 * up the lock here */
156 spin_unlock_bh(&vcpu->arch.local_int.lock);
157 rc = kvm_s390_vcpu_store_status(vcpu,
158 KVM_S390_STORE_STATUS_NOADDR);
159 if (rc >= 0)
160 rc = -EOPNOTSUPP;
161 } else
162 spin_unlock_bh(&vcpu->arch.local_int.lock);
Christian Borntraeger5288fbf2008-03-25 18:47:31 +0100163 return rc;
Christian Borntraeger8f2abe62008-03-25 18:47:23 +0100164}
165
166static int handle_validity(struct kvm_vcpu *vcpu)
167{
Carsten Otte598841c2011-07-24 10:48:21 +0200168 unsigned long vmaddr;
Christian Borntraeger8f2abe62008-03-25 18:47:23 +0100169 int viwhy = vcpu->arch.sie_block->ipb >> 16;
Carsten Otte3edbcff2009-05-12 17:21:52 +0200170 int rc;
171
Christian Borntraeger8f2abe62008-03-25 18:47:23 +0100172 vcpu->stat.exit_validity++;
Cornelia Huck5786fff2012-07-23 17:20:29 +0200173 trace_kvm_s390_intercept_validity(vcpu, viwhy);
Carsten Otte092670c2011-07-24 10:48:22 +0200174 if (viwhy == 0x37) {
Carsten Otte598841c2011-07-24 10:48:21 +0200175 vmaddr = gmap_fault(vcpu->arch.sie_block->prefix,
176 vcpu->arch.gmap);
177 if (IS_ERR_VALUE(vmaddr)) {
178 rc = -EOPNOTSUPP;
179 goto out;
180 }
Carsten Otte092670c2011-07-24 10:48:22 +0200181 rc = fault_in_pages_writeable((char __user *) vmaddr,
182 PAGE_SIZE);
183 if (rc) {
184 /* user will receive sigsegv, exit to user */
185 rc = -EOPNOTSUPP;
186 goto out;
187 }
Carsten Otte598841c2011-07-24 10:48:21 +0200188 vmaddr = gmap_fault(vcpu->arch.sie_block->prefix + PAGE_SIZE,
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 Otte3edbcff2009-05-12 17:21:52 +0200201 } else
Heiko Carstensb8e660b2010-02-26 22:37:41 +0100202 rc = -EOPNOTSUPP;
Carsten Otte3edbcff2009-05-12 17:21:52 +0200203
Carsten Otte598841c2011-07-24 10:48:21 +0200204out:
Carsten Otte3edbcff2009-05-12 17:21:52 +0200205 if (rc)
206 VCPU_EVENT(vcpu, 2, "unhandled validity intercept code %d",
207 viwhy);
208 return rc;
Christian Borntraeger8f2abe62008-03-25 18:47:23 +0100209}
210
Carsten Otteba5c1e92008-03-25 18:47:26 +0100211static int handle_instruction(struct kvm_vcpu *vcpu)
212{
213 intercept_handler_t handler;
214
215 vcpu->stat.exit_instruction++;
Cornelia Huck5786fff2012-07-23 17:20:29 +0200216 trace_kvm_s390_intercept_instruction(vcpu,
217 vcpu->arch.sie_block->ipa,
218 vcpu->arch.sie_block->ipb);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100219 handler = instruction_handlers[vcpu->arch.sie_block->ipa >> 8];
220 if (handler)
221 return handler(vcpu);
Heiko Carstensb8e660b2010-02-26 22:37:41 +0100222 return -EOPNOTSUPP;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100223}
224
225static int handle_prog(struct kvm_vcpu *vcpu)
226{
227 vcpu->stat.exit_program_interruption++;
Cornelia Huck5786fff2012-07-23 17:20:29 +0200228 trace_kvm_s390_intercept_prog(vcpu, vcpu->arch.sie_block->iprcc);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100229 return kvm_s390_inject_program_int(vcpu, vcpu->arch.sie_block->iprcc);
230}
231
232static int handle_instruction_and_prog(struct kvm_vcpu *vcpu)
233{
234 int rc, rc2;
235
236 vcpu->stat.exit_instr_and_program++;
237 rc = handle_instruction(vcpu);
238 rc2 = handle_prog(vcpu);
239
Heiko Carstensb8e660b2010-02-26 22:37:41 +0100240 if (rc == -EOPNOTSUPP)
Carsten Otteba5c1e92008-03-25 18:47:26 +0100241 vcpu->arch.sie_block->icptcode = 0x04;
242 if (rc)
243 return rc;
244 return rc2;
245}
246
Christian Borntraeger062d5e92010-01-21 12:19:07 +0100247static const intercept_handler_t intercept_funcs[] = {
Christian Borntraeger8f2abe62008-03-25 18:47:23 +0100248 [0x00 >> 2] = handle_noop,
Carsten Otteba5c1e92008-03-25 18:47:26 +0100249 [0x04 >> 2] = handle_instruction,
250 [0x08 >> 2] = handle_prog,
251 [0x0C >> 2] = handle_instruction_and_prog,
Christian Borntraeger8f2abe62008-03-25 18:47:23 +0100252 [0x10 >> 2] = handle_noop,
253 [0x14 >> 2] = handle_noop,
Carsten Otteba5c1e92008-03-25 18:47:26 +0100254 [0x1C >> 2] = kvm_s390_handle_wait,
Christian Borntraeger8f2abe62008-03-25 18:47:23 +0100255 [0x20 >> 2] = handle_validity,
256 [0x28 >> 2] = handle_stop,
257};
258
259int kvm_handle_sie_intercept(struct kvm_vcpu *vcpu)
260{
261 intercept_handler_t func;
262 u8 code = vcpu->arch.sie_block->icptcode;
263
Christian Borntraeger062d5e92010-01-21 12:19:07 +0100264 if (code & 3 || (code >> 2) >= ARRAY_SIZE(intercept_funcs))
Heiko Carstensb8e660b2010-02-26 22:37:41 +0100265 return -EOPNOTSUPP;
Christian Borntraeger8f2abe62008-03-25 18:47:23 +0100266 func = intercept_funcs[code >> 2];
267 if (func)
268 return func(vcpu);
Heiko Carstensb8e660b2010-02-26 22:37:41 +0100269 return -EOPNOTSUPP;
Christian Borntraeger8f2abe62008-03-25 18:47:23 +0100270}