Merge branch 'upstream-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jgarzi...
[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         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)
78                 useraddr += vcpu->arch.guest_gprs[base2];
79
80         if (useraddr & 3)
81                 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
82
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] = {
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,
110 };
111
112 static int handle_noop(struct kvm_vcpu *vcpu)
113 {
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 */
126         }
127         return 0;
128 }
129
130 static int handle_stop(struct kvm_vcpu *vcpu)
131 {
132         int rc = 0;
133
134         vcpu->stat.exit_stop_request++;
135         atomic_clear_mask(CPUSTAT_RUNNING, &vcpu->arch.sie_block->cpuflags);
136         spin_lock_bh(&vcpu->arch.local_int.lock);
137         if (vcpu->arch.local_int.action_bits & ACTION_STORE_ON_STOP) {
138                 vcpu->arch.local_int.action_bits &= ~ACTION_STORE_ON_STOP;
139                 rc = kvm_s390_vcpu_store_status(vcpu,
140                                                   KVM_S390_STORE_STATUS_NOADDR);
141                 if (rc >= 0)
142                         rc = -EOPNOTSUPP;
143         }
144
145         if (vcpu->arch.local_int.action_bits & ACTION_RELOADVCPU_ON_STOP) {
146                 vcpu->arch.local_int.action_bits &= ~ACTION_RELOADVCPU_ON_STOP;
147                 rc = SIE_INTERCEPT_RERUNVCPU;
148                 vcpu->run->exit_reason = KVM_EXIT_INTR;
149         }
150
151         if (vcpu->arch.local_int.action_bits & ACTION_STOP_ON_STOP) {
152                 vcpu->arch.local_int.action_bits &= ~ACTION_STOP_ON_STOP;
153                 VCPU_EVENT(vcpu, 3, "%s", "cpu stopped");
154                 rc = -EOPNOTSUPP;
155         }
156
157         spin_unlock_bh(&vcpu->arch.local_int.lock);
158         return rc;
159 }
160
161 static int handle_validity(struct kvm_vcpu *vcpu)
162 {
163         unsigned long vmaddr;
164         int viwhy = vcpu->arch.sie_block->ipb >> 16;
165         int rc;
166
167         vcpu->stat.exit_validity++;
168         if (viwhy == 0x37) {
169                 vmaddr = gmap_fault(vcpu->arch.sie_block->prefix,
170                                     vcpu->arch.gmap);
171                 if (IS_ERR_VALUE(vmaddr)) {
172                         rc = -EOPNOTSUPP;
173                         goto out;
174                 }
175                 rc = fault_in_pages_writeable((char __user *) vmaddr,
176                          PAGE_SIZE);
177                 if (rc) {
178                         /* user will receive sigsegv, exit to user */
179                         rc = -EOPNOTSUPP;
180                         goto out;
181                 }
182                 vmaddr = gmap_fault(vcpu->arch.sie_block->prefix + PAGE_SIZE,
183                                     vcpu->arch.gmap);
184                 if (IS_ERR_VALUE(vmaddr)) {
185                         rc = -EOPNOTSUPP;
186                         goto out;
187                 }
188                 rc = fault_in_pages_writeable((char __user *) vmaddr,
189                          PAGE_SIZE);
190                 if (rc) {
191                         /* user will receive sigsegv, exit to user */
192                         rc = -EOPNOTSUPP;
193                         goto out;
194                 }
195         } else
196                 rc = -EOPNOTSUPP;
197
198 out:
199         if (rc)
200                 VCPU_EVENT(vcpu, 2, "unhandled validity intercept code %d",
201                            viwhy);
202         return rc;
203 }
204
205 static int handle_instruction(struct kvm_vcpu *vcpu)
206 {
207         intercept_handler_t handler;
208
209         vcpu->stat.exit_instruction++;
210         handler = instruction_handlers[vcpu->arch.sie_block->ipa >> 8];
211         if (handler)
212                 return handler(vcpu);
213         return -EOPNOTSUPP;
214 }
215
216 static int handle_prog(struct kvm_vcpu *vcpu)
217 {
218         vcpu->stat.exit_program_interruption++;
219         return kvm_s390_inject_program_int(vcpu, vcpu->arch.sie_block->iprcc);
220 }
221
222 static int handle_instruction_and_prog(struct kvm_vcpu *vcpu)
223 {
224         int rc, rc2;
225
226         vcpu->stat.exit_instr_and_program++;
227         rc = handle_instruction(vcpu);
228         rc2 = handle_prog(vcpu);
229
230         if (rc == -EOPNOTSUPP)
231                 vcpu->arch.sie_block->icptcode = 0x04;
232         if (rc)
233                 return rc;
234         return rc2;
235 }
236
237 static const intercept_handler_t intercept_funcs[] = {
238         [0x00 >> 2] = handle_noop,
239         [0x04 >> 2] = handle_instruction,
240         [0x08 >> 2] = handle_prog,
241         [0x0C >> 2] = handle_instruction_and_prog,
242         [0x10 >> 2] = handle_noop,
243         [0x14 >> 2] = handle_noop,
244         [0x1C >> 2] = kvm_s390_handle_wait,
245         [0x20 >> 2] = handle_validity,
246         [0x28 >> 2] = handle_stop,
247 };
248
249 int kvm_handle_sie_intercept(struct kvm_vcpu *vcpu)
250 {
251         intercept_handler_t func;
252         u8 code = vcpu->arch.sie_block->icptcode;
253
254         if (code & 3 || (code >> 2) >= ARRAY_SIZE(intercept_funcs))
255                 return -EOPNOTSUPP;
256         func = intercept_funcs[code >> 2];
257         if (func)
258                 return func(vcpu);
259         return -EOPNOTSUPP;
260 }