]>
Commit | Line | Data |
---|---|---|
8f2abe6a CB |
1 | /* |
2 | * intercept.c - in-kernel handling for sie intercepts | |
3 | * | |
628eb9b8 | 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 CO |
21 | #include "gaccess.h" |
22 | ||
f5e10b09 | 23 | static int handle_lctlg(struct kvm_vcpu *vcpu) |
ba5c1e9b CO |
24 | { |
25 | int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4; | |
26 | int reg3 = vcpu->arch.sie_block->ipa & 0x000f; | |
27 | int base2 = vcpu->arch.sie_block->ipb >> 28; | |
28 | int disp2 = ((vcpu->arch.sie_block->ipb & 0x0fff0000) >> 16) + | |
29 | ((vcpu->arch.sie_block->ipb & 0xff00) << 4); | |
30 | u64 useraddr; | |
31 | int reg, rc; | |
32 | ||
f5e10b09 | 33 | vcpu->stat.instruction_lctlg++; |
ba5c1e9b | 34 | if ((vcpu->arch.sie_block->ipb & 0xff) != 0x2f) |
b8e660b8 | 35 | return -EOPNOTSUPP; |
ba5c1e9b CO |
36 | |
37 | useraddr = disp2; | |
38 | if (base2) | |
5a32c1af | 39 | useraddr += vcpu->run->s.regs.gprs[base2]; |
ba5c1e9b | 40 | |
5a00a5e7 CB |
41 | if (useraddr & 7) |
42 | return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION); | |
43 | ||
ba5c1e9b CO |
44 | reg = reg1; |
45 | ||
f5e10b09 | 46 | VCPU_EVENT(vcpu, 5, "lctlg r1:%x, r3:%x,b2:%x,d2:%x", reg1, reg3, base2, |
ba5c1e9b CO |
47 | disp2); |
48 | ||
49 | do { | |
50 | rc = get_guest_u64(vcpu, useraddr, | |
51 | &vcpu->arch.sie_block->gcr[reg]); | |
52 | if (rc == -EFAULT) { | |
53 | kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING); | |
54 | break; | |
55 | } | |
56 | useraddr += 8; | |
57 | if (reg == reg3) | |
58 | break; | |
59 | reg = (reg + 1) % 16; | |
60 | } while (1); | |
61 | return 0; | |
62 | } | |
63 | ||
64 | static int handle_lctl(struct kvm_vcpu *vcpu) | |
65 | { | |
66 | int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4; | |
67 | int reg3 = vcpu->arch.sie_block->ipa & 0x000f; | |
68 | int base2 = vcpu->arch.sie_block->ipb >> 28; | |
69 | int disp2 = ((vcpu->arch.sie_block->ipb & 0x0fff0000) >> 16); | |
70 | u64 useraddr; | |
71 | u32 val = 0; | |
72 | int reg, rc; | |
73 | ||
74 | vcpu->stat.instruction_lctl++; | |
75 | ||
76 | useraddr = disp2; | |
77 | if (base2) | |
5a32c1af | 78 | useraddr += vcpu->run->s.regs.gprs[base2]; |
ba5c1e9b | 79 | |
5a00a5e7 CB |
80 | if (useraddr & 3) |
81 | return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION); | |
82 | ||
ba5c1e9b CO |
83 | VCPU_EVENT(vcpu, 5, "lctl r1:%x, r3:%x,b2:%x,d2:%x", reg1, reg3, base2, |
84 | disp2); | |
85 | ||
86 | reg = reg1; | |
87 | do { | |
88 | rc = get_guest_u32(vcpu, useraddr, &val); | |
89 | if (rc == -EFAULT) { | |
90 | kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING); | |
91 | break; | |
92 | } | |
93 | vcpu->arch.sie_block->gcr[reg] &= 0xffffffff00000000ul; | |
94 | vcpu->arch.sie_block->gcr[reg] |= val; | |
95 | useraddr += 4; | |
96 | if (reg == reg3) | |
97 | break; | |
98 | reg = (reg + 1) % 16; | |
99 | } while (1); | |
100 | return 0; | |
101 | } | |
102 | ||
103 | static intercept_handler_t instruction_handlers[256] = { | |
8c3f61e2 | 104 | [0x01] = kvm_s390_handle_01, |
e28acfea | 105 | [0x83] = kvm_s390_handle_diag, |
5288fbf0 | 106 | [0xae] = kvm_s390_handle_sigp, |
70455a36 | 107 | [0xb2] = kvm_s390_handle_b2, |
ba5c1e9b | 108 | [0xb7] = handle_lctl, |
bb25b9ba | 109 | [0xe5] = kvm_s390_handle_e5, |
f5e10b09 | 110 | [0xeb] = handle_lctlg, |
ba5c1e9b | 111 | }; |
8f2abe6a CB |
112 | |
113 | static int handle_noop(struct kvm_vcpu *vcpu) | |
114 | { | |
115 | switch (vcpu->arch.sie_block->icptcode) { | |
0eaeafa1 CB |
116 | case 0x0: |
117 | vcpu->stat.exit_null++; | |
118 | break; | |
8f2abe6a CB |
119 | case 0x10: |
120 | vcpu->stat.exit_external_request++; | |
121 | break; | |
122 | case 0x14: | |
123 | vcpu->stat.exit_external_interrupt++; | |
124 | break; | |
125 | default: | |
126 | break; /* nothing */ | |
127 | } | |
128 | return 0; | |
129 | } | |
130 | ||
131 | static int handle_stop(struct kvm_vcpu *vcpu) | |
132 | { | |
9ace903d | 133 | int rc = 0; |
5288fbf0 | 134 | |
8f2abe6a | 135 | vcpu->stat.exit_stop_request++; |
5288fbf0 | 136 | spin_lock_bh(&vcpu->arch.local_int.lock); |
5288fbf0 | 137 | |
9ace903d CE |
138 | if (vcpu->arch.local_int.action_bits & ACTION_RELOADVCPU_ON_STOP) { |
139 | vcpu->arch.local_int.action_bits &= ~ACTION_RELOADVCPU_ON_STOP; | |
140 | rc = SIE_INTERCEPT_RERUNVCPU; | |
141 | vcpu->run->exit_reason = KVM_EXIT_INTR; | |
142 | } | |
143 | ||
5288fbf0 | 144 | if (vcpu->arch.local_int.action_bits & ACTION_STOP_ON_STOP) { |
9e6dabef CH |
145 | atomic_set_mask(CPUSTAT_STOPPED, |
146 | &vcpu->arch.sie_block->cpuflags); | |
5288fbf0 CB |
147 | vcpu->arch.local_int.action_bits &= ~ACTION_STOP_ON_STOP; |
148 | VCPU_EVENT(vcpu, 3, "%s", "cpu stopped"); | |
b8e660b8 | 149 | rc = -EOPNOTSUPP; |
9ace903d CE |
150 | } |
151 | ||
9e0d5473 JF |
152 | if (vcpu->arch.local_int.action_bits & ACTION_STORE_ON_STOP) { |
153 | vcpu->arch.local_int.action_bits &= ~ACTION_STORE_ON_STOP; | |
154 | /* store status must be called unlocked. Since local_int.lock | |
155 | * only protects local_int.* and not guest memory we can give | |
156 | * up the lock here */ | |
157 | spin_unlock_bh(&vcpu->arch.local_int.lock); | |
158 | rc = kvm_s390_vcpu_store_status(vcpu, | |
159 | KVM_S390_STORE_STATUS_NOADDR); | |
160 | if (rc >= 0) | |
161 | rc = -EOPNOTSUPP; | |
162 | } else | |
163 | spin_unlock_bh(&vcpu->arch.local_int.lock); | |
5288fbf0 | 164 | return rc; |
8f2abe6a CB |
165 | } |
166 | ||
167 | static int handle_validity(struct kvm_vcpu *vcpu) | |
168 | { | |
598841ca | 169 | unsigned long vmaddr; |
8f2abe6a | 170 | int viwhy = vcpu->arch.sie_block->ipb >> 16; |
3edbcff9 CO |
171 | int rc; |
172 | ||
8f2abe6a | 173 | vcpu->stat.exit_validity++; |
092670cd CO |
174 | if (viwhy == 0x37) { |
175 | 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 | } | |
181 | rc = fault_in_pages_writeable((char __user *) vmaddr, | |
182 | PAGE_SIZE); | |
598841ca | 183 | if (rc) { |
3edbcff9 | 184 | /* user will receive sigsegv, exit to user */ |
b8e660b8 | 185 | rc = -EOPNOTSUPP; |
598841ca CO |
186 | goto out; |
187 | } | |
092670cd | 188 | vmaddr = gmap_fault(vcpu->arch.sie_block->prefix + PAGE_SIZE, |
598841ca CO |
189 | vcpu->arch.gmap); |
190 | if (IS_ERR_VALUE(vmaddr)) { | |
191 | rc = -EOPNOTSUPP; | |
192 | goto out; | |
193 | } | |
092670cd CO |
194 | rc = fault_in_pages_writeable((char __user *) vmaddr, |
195 | PAGE_SIZE); | |
196 | if (rc) { | |
197 | /* user will receive sigsegv, exit to user */ | |
598841ca CO |
198 | rc = -EOPNOTSUPP; |
199 | goto out; | |
200 | } | |
3edbcff9 | 201 | } else |
b8e660b8 | 202 | rc = -EOPNOTSUPP; |
3edbcff9 | 203 | |
598841ca | 204 | out: |
3edbcff9 CO |
205 | if (rc) |
206 | VCPU_EVENT(vcpu, 2, "unhandled validity intercept code %d", | |
207 | viwhy); | |
208 | return rc; | |
8f2abe6a CB |
209 | } |
210 | ||
ba5c1e9b CO |
211 | static int handle_instruction(struct kvm_vcpu *vcpu) |
212 | { | |
213 | intercept_handler_t handler; | |
214 | ||
215 | vcpu->stat.exit_instruction++; | |
216 | handler = instruction_handlers[vcpu->arch.sie_block->ipa >> 8]; | |
217 | if (handler) | |
218 | return handler(vcpu); | |
b8e660b8 | 219 | return -EOPNOTSUPP; |
ba5c1e9b CO |
220 | } |
221 | ||
222 | static int handle_prog(struct kvm_vcpu *vcpu) | |
223 | { | |
224 | vcpu->stat.exit_program_interruption++; | |
225 | return kvm_s390_inject_program_int(vcpu, vcpu->arch.sie_block->iprcc); | |
226 | } | |
227 | ||
228 | static int handle_instruction_and_prog(struct kvm_vcpu *vcpu) | |
229 | { | |
230 | int rc, rc2; | |
231 | ||
232 | vcpu->stat.exit_instr_and_program++; | |
233 | rc = handle_instruction(vcpu); | |
234 | rc2 = handle_prog(vcpu); | |
235 | ||
b8e660b8 | 236 | if (rc == -EOPNOTSUPP) |
ba5c1e9b CO |
237 | vcpu->arch.sie_block->icptcode = 0x04; |
238 | if (rc) | |
239 | return rc; | |
240 | return rc2; | |
241 | } | |
242 | ||
062d5e9b | 243 | static const intercept_handler_t intercept_funcs[] = { |
8f2abe6a | 244 | [0x00 >> 2] = handle_noop, |
ba5c1e9b CO |
245 | [0x04 >> 2] = handle_instruction, |
246 | [0x08 >> 2] = handle_prog, | |
247 | [0x0C >> 2] = handle_instruction_and_prog, | |
8f2abe6a CB |
248 | [0x10 >> 2] = handle_noop, |
249 | [0x14 >> 2] = handle_noop, | |
ba5c1e9b | 250 | [0x1C >> 2] = kvm_s390_handle_wait, |
8f2abe6a CB |
251 | [0x20 >> 2] = handle_validity, |
252 | [0x28 >> 2] = handle_stop, | |
253 | }; | |
254 | ||
255 | int kvm_handle_sie_intercept(struct kvm_vcpu *vcpu) | |
256 | { | |
257 | intercept_handler_t func; | |
258 | u8 code = vcpu->arch.sie_block->icptcode; | |
259 | ||
062d5e9b | 260 | if (code & 3 || (code >> 2) >= ARRAY_SIZE(intercept_funcs)) |
b8e660b8 | 261 | return -EOPNOTSUPP; |
8f2abe6a CB |
262 | func = intercept_funcs[code >> 2]; |
263 | if (func) | |
264 | return func(vcpu); | |
b8e660b8 | 265 | return -EOPNOTSUPP; |
8f2abe6a | 266 | } |