KVM: s390: Check for access exceptions during TPI
[deliverable/linux.git] / arch / s390 / kvm / intercept.c
CommitLineData
8f2abe6a 1/*
a53c8fab 2 * in-kernel handling for sie intercepts
8f2abe6a 3 *
a53c8fab 4 * Copyright IBM Corp. 2008, 2009
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>
19
20#include "kvm-s390.h"
ba5c1e9b 21#include "gaccess.h"
5786fffa 22#include "trace.h"
ade38c31 23#include "trace-s390.h"
ba5c1e9b 24
f5e10b09 25static int handle_lctlg(struct kvm_vcpu *vcpu)
ba5c1e9b
CO
26{
27 int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
28 int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
ba5c1e9b
CO
29 u64 useraddr;
30 int reg, rc;
31
f5e10b09 32 vcpu->stat.instruction_lctlg++;
ba5c1e9b 33
b1c571a5 34 useraddr = kvm_s390_get_base_disp_rsy(vcpu);
ba5c1e9b 35
5a00a5e7
CB
36 if (useraddr & 7)
37 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
38
ba5c1e9b
CO
39 reg = reg1;
40
b1c571a5
CH
41 VCPU_EVENT(vcpu, 5, "lctlg r1:%x, r3:%x, addr:%llx", reg1, reg3,
42 useraddr);
5786fffa 43 trace_kvm_s390_handle_lctl(vcpu, 1, reg1, reg3, useraddr);
ba5c1e9b
CO
44
45 do {
396083a9 46 rc = get_guest(vcpu, vcpu->arch.sie_block->gcr[reg],
0a75ca27 47 (u64 __user *) useraddr);
db4a29cb
HC
48 if (rc)
49 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
ba5c1e9b
CO
50 useraddr += 8;
51 if (reg == reg3)
52 break;
53 reg = (reg + 1) % 16;
54 } while (1);
55 return 0;
56}
57
58static int handle_lctl(struct kvm_vcpu *vcpu)
59{
60 int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
61 int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
ba5c1e9b
CO
62 u64 useraddr;
63 u32 val = 0;
64 int reg, rc;
65
66 vcpu->stat.instruction_lctl++;
67
b1c571a5 68 useraddr = kvm_s390_get_base_disp_rs(vcpu);
ba5c1e9b 69
5a00a5e7
CB
70 if (useraddr & 3)
71 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
72
b1c571a5
CH
73 VCPU_EVENT(vcpu, 5, "lctl r1:%x, r3:%x, addr:%llx", reg1, reg3,
74 useraddr);
5786fffa 75 trace_kvm_s390_handle_lctl(vcpu, 0, reg1, reg3, useraddr);
ba5c1e9b
CO
76
77 reg = reg1;
78 do {
0a75ca27 79 rc = get_guest(vcpu, val, (u32 __user *) useraddr);
db4a29cb
HC
80 if (rc)
81 return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
ba5c1e9b
CO
82 vcpu->arch.sie_block->gcr[reg] &= 0xffffffff00000000ul;
83 vcpu->arch.sie_block->gcr[reg] |= val;
84 useraddr += 4;
85 if (reg == reg3)
86 break;
87 reg = (reg + 1) % 16;
88 } while (1);
89 return 0;
90}
91
f379aae5
CH
92static const intercept_handler_t eb_handlers[256] = {
93 [0x2f] = handle_lctlg,
94 [0x8a] = kvm_s390_handle_priv_eb,
95};
96
97static int handle_eb(struct kvm_vcpu *vcpu)
98{
99 intercept_handler_t handler;
100
101 handler = eb_handlers[vcpu->arch.sie_block->ipb & 0xff];
102 if (handler)
103 return handler(vcpu);
104 return -EOPNOTSUPP;
105}
106
77975357 107static const intercept_handler_t instruction_handlers[256] = {
8c3f61e2 108 [0x01] = kvm_s390_handle_01,
48a3e950 109 [0x82] = kvm_s390_handle_lpsw,
e28acfea 110 [0x83] = kvm_s390_handle_diag,
5288fbf0 111 [0xae] = kvm_s390_handle_sigp,
70455a36 112 [0xb2] = kvm_s390_handle_b2,
ba5c1e9b 113 [0xb7] = handle_lctl,
48a3e950 114 [0xb9] = kvm_s390_handle_b9,
bb25b9ba 115 [0xe5] = kvm_s390_handle_e5,
f379aae5 116 [0xeb] = handle_eb,
ba5c1e9b 117};
8f2abe6a
CB
118
119static int handle_noop(struct kvm_vcpu *vcpu)
120{
121 switch (vcpu->arch.sie_block->icptcode) {
0eaeafa1
CB
122 case 0x0:
123 vcpu->stat.exit_null++;
124 break;
8f2abe6a
CB
125 case 0x10:
126 vcpu->stat.exit_external_request++;
127 break;
128 case 0x14:
129 vcpu->stat.exit_external_interrupt++;
130 break;
131 default:
132 break; /* nothing */
133 }
134 return 0;
135}
136
137static int handle_stop(struct kvm_vcpu *vcpu)
138{
9ace903d 139 int rc = 0;
5288fbf0 140
8f2abe6a 141 vcpu->stat.exit_stop_request++;
5288fbf0 142 spin_lock_bh(&vcpu->arch.local_int.lock);
5288fbf0 143
ade38c31
CH
144 trace_kvm_s390_stop_request(vcpu->arch.local_int.action_bits);
145
9ace903d
CE
146 if (vcpu->arch.local_int.action_bits & ACTION_RELOADVCPU_ON_STOP) {
147 vcpu->arch.local_int.action_bits &= ~ACTION_RELOADVCPU_ON_STOP;
148 rc = SIE_INTERCEPT_RERUNVCPU;
149 vcpu->run->exit_reason = KVM_EXIT_INTR;
150 }
151
5288fbf0 152 if (vcpu->arch.local_int.action_bits & ACTION_STOP_ON_STOP) {
9e6dabef
CH
153 atomic_set_mask(CPUSTAT_STOPPED,
154 &vcpu->arch.sie_block->cpuflags);
5288fbf0
CB
155 vcpu->arch.local_int.action_bits &= ~ACTION_STOP_ON_STOP;
156 VCPU_EVENT(vcpu, 3, "%s", "cpu stopped");
b8e660b8 157 rc = -EOPNOTSUPP;
9ace903d
CE
158 }
159
9e0d5473
JF
160 if (vcpu->arch.local_int.action_bits & ACTION_STORE_ON_STOP) {
161 vcpu->arch.local_int.action_bits &= ~ACTION_STORE_ON_STOP;
162 /* store status must be called unlocked. Since local_int.lock
163 * only protects local_int.* and not guest memory we can give
164 * up the lock here */
165 spin_unlock_bh(&vcpu->arch.local_int.lock);
166 rc = kvm_s390_vcpu_store_status(vcpu,
167 KVM_S390_STORE_STATUS_NOADDR);
168 if (rc >= 0)
169 rc = -EOPNOTSUPP;
170 } else
171 spin_unlock_bh(&vcpu->arch.local_int.lock);
5288fbf0 172 return rc;
8f2abe6a
CB
173}
174
175static int handle_validity(struct kvm_vcpu *vcpu)
176{
177 int viwhy = vcpu->arch.sie_block->ipb >> 16;
3edbcff9 178
8f2abe6a 179 vcpu->stat.exit_validity++;
5786fffa 180 trace_kvm_s390_intercept_validity(vcpu, viwhy);
2c70fe44
CB
181 WARN_ONCE(true, "kvm: unhandled validity intercept 0x%x\n", viwhy);
182 return -EOPNOTSUPP;
8f2abe6a
CB
183}
184
ba5c1e9b
CO
185static int handle_instruction(struct kvm_vcpu *vcpu)
186{
187 intercept_handler_t handler;
188
189 vcpu->stat.exit_instruction++;
5786fffa
CH
190 trace_kvm_s390_intercept_instruction(vcpu,
191 vcpu->arch.sie_block->ipa,
192 vcpu->arch.sie_block->ipb);
ba5c1e9b
CO
193 handler = instruction_handlers[vcpu->arch.sie_block->ipa >> 8];
194 if (handler)
195 return handler(vcpu);
b8e660b8 196 return -EOPNOTSUPP;
ba5c1e9b
CO
197}
198
199static int handle_prog(struct kvm_vcpu *vcpu)
200{
201 vcpu->stat.exit_program_interruption++;
5786fffa 202 trace_kvm_s390_intercept_prog(vcpu, vcpu->arch.sie_block->iprcc);
ba5c1e9b
CO
203 return kvm_s390_inject_program_int(vcpu, vcpu->arch.sie_block->iprcc);
204}
205
206static int handle_instruction_and_prog(struct kvm_vcpu *vcpu)
207{
208 int rc, rc2;
209
210 vcpu->stat.exit_instr_and_program++;
211 rc = handle_instruction(vcpu);
212 rc2 = handle_prog(vcpu);
213
b8e660b8 214 if (rc == -EOPNOTSUPP)
ba5c1e9b
CO
215 vcpu->arch.sie_block->icptcode = 0x04;
216 if (rc)
217 return rc;
218 return rc2;
219}
220
062d5e9b 221static const intercept_handler_t intercept_funcs[] = {
8f2abe6a 222 [0x00 >> 2] = handle_noop,
ba5c1e9b
CO
223 [0x04 >> 2] = handle_instruction,
224 [0x08 >> 2] = handle_prog,
225 [0x0C >> 2] = handle_instruction_and_prog,
8f2abe6a
CB
226 [0x10 >> 2] = handle_noop,
227 [0x14 >> 2] = handle_noop,
fa6b7fe9 228 [0x18 >> 2] = handle_noop,
ba5c1e9b 229 [0x1C >> 2] = kvm_s390_handle_wait,
8f2abe6a
CB
230 [0x20 >> 2] = handle_validity,
231 [0x28 >> 2] = handle_stop,
232};
233
234int kvm_handle_sie_intercept(struct kvm_vcpu *vcpu)
235{
236 intercept_handler_t func;
237 u8 code = vcpu->arch.sie_block->icptcode;
238
062d5e9b 239 if (code & 3 || (code >> 2) >= ARRAY_SIZE(intercept_funcs))
b8e660b8 240 return -EOPNOTSUPP;
8f2abe6a
CB
241 func = intercept_funcs[code >> 2];
242 if (func)
243 return func(vcpu);
b8e660b8 244 return -EOPNOTSUPP;
8f2abe6a 245}
This page took 0.497861 seconds and 5 git commands to generate.