Avoid beyond bounds copy while caching ACL
[zen-stable.git] / arch / s390 / kvm / intercept.c
bloba5f6effd558f3b4b7c73037d4de196e84609aab6
1 /*
2 * intercept.c - in-kernel handling for sie intercepts
4 * Copyright IBM Corp. 2008,2009
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.
10 * Author(s): Carsten Otte <cotte@de.ibm.com>
11 * Christian Borntraeger <borntraeger@de.ibm.com>
14 #include <linux/kvm_host.h>
15 #include <linux/errno.h>
16 #include <linux/pagemap.h>
18 #include <asm/kvm_host.h>
20 #include "kvm-s390.h"
21 #include "gaccess.h"
23 static int handle_lctlg(struct kvm_vcpu *vcpu)
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;
33 vcpu->stat.instruction_lctlg++;
34 if ((vcpu->arch.sie_block->ipb & 0xff) != 0x2f)
35 return -EOPNOTSUPP;
37 useraddr = disp2;
38 if (base2)
39 useraddr += vcpu->arch.guest_gprs[base2];
41 if (useraddr & 7)
42 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
44 reg = reg1;
46 VCPU_EVENT(vcpu, 5, "lctlg r1:%x, r3:%x,b2:%x,d2:%x", reg1, reg3, base2,
47 disp2);
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;
56 useraddr += 8;
57 if (reg == reg3)
58 break;
59 reg = (reg + 1) % 16;
60 } while (1);
61 return 0;
64 static int handle_lctl(struct kvm_vcpu *vcpu)
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;
74 vcpu->stat.instruction_lctl++;
76 useraddr = disp2;
77 if (base2)
78 useraddr += vcpu->arch.guest_gprs[base2];
80 if (useraddr & 3)
81 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
83 VCPU_EVENT(vcpu, 5, "lctl r1:%x, r3:%x,b2:%x,d2:%x", reg1, reg3, base2,
84 disp2);
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;
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;
103 static intercept_handler_t instruction_handlers[256] = {
104 [0x83] = kvm_s390_handle_diag,
105 [0xae] = kvm_s390_handle_sigp,
106 [0xb2] = kvm_s390_handle_b2,
107 [0xb7] = handle_lctl,
108 [0xe5] = kvm_s390_handle_e5,
109 [0xeb] = handle_lctlg,
112 static int handle_noop(struct kvm_vcpu *vcpu)
114 switch (vcpu->arch.sie_block->icptcode) {
115 case 0x0:
116 vcpu->stat.exit_null++;
117 break;
118 case 0x10:
119 vcpu->stat.exit_external_request++;
120 break;
121 case 0x14:
122 vcpu->stat.exit_external_interrupt++;
123 break;
124 default:
125 break; /* nothing */
127 return 0;
130 static int handle_stop(struct kvm_vcpu *vcpu)
132 int rc = 0;
134 vcpu->stat.exit_stop_request++;
135 spin_lock_bh(&vcpu->arch.local_int.lock);
137 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;
143 if (vcpu->arch.local_int.action_bits & ACTION_STOP_ON_STOP) {
144 atomic_set_mask(CPUSTAT_STOPPED,
145 &vcpu->arch.sie_block->cpuflags);
146 vcpu->arch.local_int.action_bits &= ~ACTION_STOP_ON_STOP;
147 VCPU_EVENT(vcpu, 3, "%s", "cpu stopped");
148 rc = -EOPNOTSUPP;
151 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);
163 return rc;
166 static int handle_validity(struct kvm_vcpu *vcpu)
168 unsigned long vmaddr;
169 int viwhy = vcpu->arch.sie_block->ipb >> 16;
170 int rc;
172 vcpu->stat.exit_validity++;
173 if (viwhy == 0x37) {
174 vmaddr = gmap_fault(vcpu->arch.sie_block->prefix,
175 vcpu->arch.gmap);
176 if (IS_ERR_VALUE(vmaddr)) {
177 rc = -EOPNOTSUPP;
178 goto out;
180 rc = fault_in_pages_writeable((char __user *) vmaddr,
181 PAGE_SIZE);
182 if (rc) {
183 /* user will receive sigsegv, exit to user */
184 rc = -EOPNOTSUPP;
185 goto out;
187 vmaddr = gmap_fault(vcpu->arch.sie_block->prefix + PAGE_SIZE,
188 vcpu->arch.gmap);
189 if (IS_ERR_VALUE(vmaddr)) {
190 rc = -EOPNOTSUPP;
191 goto out;
193 rc = fault_in_pages_writeable((char __user *) vmaddr,
194 PAGE_SIZE);
195 if (rc) {
196 /* user will receive sigsegv, exit to user */
197 rc = -EOPNOTSUPP;
198 goto out;
200 } else
201 rc = -EOPNOTSUPP;
203 out:
204 if (rc)
205 VCPU_EVENT(vcpu, 2, "unhandled validity intercept code %d",
206 viwhy);
207 return rc;
210 static int handle_instruction(struct kvm_vcpu *vcpu)
212 intercept_handler_t handler;
214 vcpu->stat.exit_instruction++;
215 handler = instruction_handlers[vcpu->arch.sie_block->ipa >> 8];
216 if (handler)
217 return handler(vcpu);
218 return -EOPNOTSUPP;
221 static int handle_prog(struct kvm_vcpu *vcpu)
223 vcpu->stat.exit_program_interruption++;
224 return kvm_s390_inject_program_int(vcpu, vcpu->arch.sie_block->iprcc);
227 static int handle_instruction_and_prog(struct kvm_vcpu *vcpu)
229 int rc, rc2;
231 vcpu->stat.exit_instr_and_program++;
232 rc = handle_instruction(vcpu);
233 rc2 = handle_prog(vcpu);
235 if (rc == -EOPNOTSUPP)
236 vcpu->arch.sie_block->icptcode = 0x04;
237 if (rc)
238 return rc;
239 return rc2;
242 static const intercept_handler_t intercept_funcs[] = {
243 [0x00 >> 2] = handle_noop,
244 [0x04 >> 2] = handle_instruction,
245 [0x08 >> 2] = handle_prog,
246 [0x0C >> 2] = handle_instruction_and_prog,
247 [0x10 >> 2] = handle_noop,
248 [0x14 >> 2] = handle_noop,
249 [0x1C >> 2] = kvm_s390_handle_wait,
250 [0x20 >> 2] = handle_validity,
251 [0x28 >> 2] = handle_stop,
254 int kvm_handle_sie_intercept(struct kvm_vcpu *vcpu)
256 intercept_handler_t func;
257 u8 code = vcpu->arch.sie_block->icptcode;
259 if (code & 3 || (code >> 2) >= ARRAY_SIZE(intercept_funcs))
260 return -EOPNOTSUPP;
261 func = intercept_funcs[code >> 2];
262 if (func)
263 return func(vcpu);
264 return -EOPNOTSUPP;