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