fda1d64f15eec14d81ac4d26e5e0ff4f229480c0
[pandora-kernel.git] / arch / s390 / kvm / sigp.c
1 /*
2  * sigp.c - handlinge interprocessor communication
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  *               Christian Ehrhardt <ehrhardt@de.ibm.com>
13  */
14
15 #include <linux/kvm.h>
16 #include <linux/kvm_host.h>
17 #include <linux/slab.h>
18 #include <asm/sigp.h>
19 #include "gaccess.h"
20 #include "kvm-s390.h"
21
22 static int __sigp_sense(struct kvm_vcpu *vcpu, u16 cpu_addr,
23                         u64 *reg)
24 {
25         struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
26         int rc;
27
28         if (cpu_addr >= KVM_MAX_VCPUS)
29                 return 3; /* not operational */
30
31         spin_lock(&fi->lock);
32         if (fi->local_int[cpu_addr] == NULL)
33                 rc = 3; /* not operational */
34         else if (!(atomic_read(fi->local_int[cpu_addr]->cpuflags)
35                   & CPUSTAT_STOPPED)) {
36                 *reg &= 0xffffffff00000000UL;
37                 rc = 1; /* status stored */
38         } else {
39                 *reg &= 0xffffffff00000000UL;
40                 *reg |= SIGP_STATUS_STOPPED;
41                 rc = 1; /* status stored */
42         }
43         spin_unlock(&fi->lock);
44
45         VCPU_EVENT(vcpu, 4, "sensed status of cpu %x rc %x", cpu_addr, rc);
46         return rc;
47 }
48
49 static int __sigp_emergency(struct kvm_vcpu *vcpu, u16 cpu_addr)
50 {
51         struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
52         struct kvm_s390_local_interrupt *li;
53         struct kvm_s390_interrupt_info *inti;
54         int rc;
55
56         if (cpu_addr >= KVM_MAX_VCPUS)
57                 return 3; /* not operational */
58
59         inti = kzalloc(sizeof(*inti), GFP_KERNEL);
60         if (!inti)
61                 return -ENOMEM;
62
63         inti->type = KVM_S390_INT_EMERGENCY;
64         inti->emerg.code = vcpu->vcpu_id;
65
66         spin_lock(&fi->lock);
67         li = fi->local_int[cpu_addr];
68         if (li == NULL) {
69                 rc = 3; /* not operational */
70                 kfree(inti);
71                 goto unlock;
72         }
73         spin_lock_bh(&li->lock);
74         list_add_tail(&inti->list, &li->list);
75         atomic_set(&li->active, 1);
76         atomic_set_mask(CPUSTAT_EXT_INT, li->cpuflags);
77         if (waitqueue_active(&li->wq))
78                 wake_up_interruptible(&li->wq);
79         spin_unlock_bh(&li->lock);
80         rc = 0; /* order accepted */
81         VCPU_EVENT(vcpu, 4, "sent sigp emerg to cpu %x", cpu_addr);
82 unlock:
83         spin_unlock(&fi->lock);
84         return rc;
85 }
86
87 static int __sigp_external_call(struct kvm_vcpu *vcpu, u16 cpu_addr)
88 {
89         struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
90         struct kvm_s390_local_interrupt *li;
91         struct kvm_s390_interrupt_info *inti;
92         int rc;
93
94         if (cpu_addr >= KVM_MAX_VCPUS)
95                 return 3; /* not operational */
96
97         inti = kzalloc(sizeof(*inti), GFP_KERNEL);
98         if (!inti)
99                 return -ENOMEM;
100
101         inti->type = KVM_S390_INT_EXTERNAL_CALL;
102         inti->extcall.code = vcpu->vcpu_id;
103
104         spin_lock(&fi->lock);
105         li = fi->local_int[cpu_addr];
106         if (li == NULL) {
107                 rc = 3; /* not operational */
108                 kfree(inti);
109                 goto unlock;
110         }
111         spin_lock_bh(&li->lock);
112         list_add_tail(&inti->list, &li->list);
113         atomic_set(&li->active, 1);
114         atomic_set_mask(CPUSTAT_EXT_INT, li->cpuflags);
115         if (waitqueue_active(&li->wq))
116                 wake_up_interruptible(&li->wq);
117         spin_unlock_bh(&li->lock);
118         rc = 0; /* order accepted */
119         VCPU_EVENT(vcpu, 4, "sent sigp ext call to cpu %x", cpu_addr);
120 unlock:
121         spin_unlock(&fi->lock);
122         return rc;
123 }
124
125 static int __inject_sigp_stop(struct kvm_s390_local_interrupt *li, int action)
126 {
127         struct kvm_s390_interrupt_info *inti;
128
129         inti = kzalloc(sizeof(*inti), GFP_ATOMIC);
130         if (!inti)
131                 return -ENOMEM;
132         inti->type = KVM_S390_SIGP_STOP;
133
134         spin_lock_bh(&li->lock);
135         if ((atomic_read(li->cpuflags) & CPUSTAT_STOPPED))
136                 goto out;
137         list_add_tail(&inti->list, &li->list);
138         atomic_set(&li->active, 1);
139         atomic_set_mask(CPUSTAT_STOP_INT, li->cpuflags);
140         li->action_bits |= action;
141         if (waitqueue_active(&li->wq))
142                 wake_up_interruptible(&li->wq);
143 out:
144         spin_unlock_bh(&li->lock);
145
146         return 0; /* order accepted */
147 }
148
149 static int __sigp_stop(struct kvm_vcpu *vcpu, u16 cpu_addr, int action)
150 {
151         struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
152         struct kvm_s390_local_interrupt *li;
153         int rc;
154
155         if (cpu_addr >= KVM_MAX_VCPUS)
156                 return 3; /* not operational */
157
158         spin_lock(&fi->lock);
159         li = fi->local_int[cpu_addr];
160         if (li == NULL) {
161                 rc = 3; /* not operational */
162                 goto unlock;
163         }
164
165         rc = __inject_sigp_stop(li, action);
166
167 unlock:
168         spin_unlock(&fi->lock);
169         VCPU_EVENT(vcpu, 4, "sent sigp stop to cpu %x", cpu_addr);
170         return rc;
171 }
172
173 int kvm_s390_inject_sigp_stop(struct kvm_vcpu *vcpu, int action)
174 {
175         struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
176         return __inject_sigp_stop(li, action);
177 }
178
179 static int __sigp_set_arch(struct kvm_vcpu *vcpu, u32 parameter)
180 {
181         int rc;
182
183         switch (parameter & 0xff) {
184         case 0:
185                 rc = 3; /* not operational */
186                 break;
187         case 1:
188         case 2:
189                 rc = 0; /* order accepted */
190                 break;
191         default:
192                 rc = -EOPNOTSUPP;
193         }
194         return rc;
195 }
196
197 static int __sigp_set_prefix(struct kvm_vcpu *vcpu, u16 cpu_addr, u32 address,
198                              u64 *reg)
199 {
200         struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
201         struct kvm_s390_local_interrupt *li = NULL;
202         struct kvm_s390_interrupt_info *inti;
203         int rc;
204         u8 tmp;
205
206         /* make sure that the new value is valid memory */
207         address = address & 0x7fffe000u;
208         if (copy_from_guest_absolute(vcpu, &tmp, address, 1) ||
209            copy_from_guest_absolute(vcpu, &tmp, address + PAGE_SIZE, 1)) {
210                 *reg |= SIGP_STATUS_INVALID_PARAMETER;
211                 return 1; /* invalid parameter */
212         }
213
214         inti = kzalloc(sizeof(*inti), GFP_KERNEL);
215         if (!inti)
216                 return 2; /* busy */
217
218         spin_lock(&fi->lock);
219         if (cpu_addr < KVM_MAX_VCPUS)
220                 li = fi->local_int[cpu_addr];
221
222         if (li == NULL) {
223                 rc = 1; /* incorrect state */
224                 *reg &= SIGP_STATUS_INCORRECT_STATE;
225                 kfree(inti);
226                 goto out_fi;
227         }
228
229         spin_lock_bh(&li->lock);
230         /* cpu must be in stopped state */
231         if (!(atomic_read(li->cpuflags) & CPUSTAT_STOPPED)) {
232                 rc = 1; /* incorrect state */
233                 *reg &= SIGP_STATUS_INCORRECT_STATE;
234                 kfree(inti);
235                 goto out_li;
236         }
237
238         inti->type = KVM_S390_SIGP_SET_PREFIX;
239         inti->prefix.address = address;
240
241         list_add_tail(&inti->list, &li->list);
242         atomic_set(&li->active, 1);
243         if (waitqueue_active(&li->wq))
244                 wake_up_interruptible(&li->wq);
245         rc = 0; /* order accepted */
246
247         VCPU_EVENT(vcpu, 4, "set prefix of cpu %02x to %x", cpu_addr, address);
248 out_li:
249         spin_unlock_bh(&li->lock);
250 out_fi:
251         spin_unlock(&fi->lock);
252         return rc;
253 }
254
255 static int __sigp_sense_running(struct kvm_vcpu *vcpu, u16 cpu_addr,
256                                 u64 *reg)
257 {
258         int rc;
259         struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
260
261         if (cpu_addr >= KVM_MAX_VCPUS)
262                 return 3; /* not operational */
263
264         spin_lock(&fi->lock);
265         if (fi->local_int[cpu_addr] == NULL)
266                 rc = 3; /* not operational */
267         else {
268                 if (atomic_read(fi->local_int[cpu_addr]->cpuflags)
269                     & CPUSTAT_RUNNING) {
270                         /* running */
271                         rc = 1;
272                 } else {
273                         /* not running */
274                         *reg &= 0xffffffff00000000UL;
275                         *reg |= SIGP_STATUS_NOT_RUNNING;
276                         rc = 0;
277                 }
278         }
279         spin_unlock(&fi->lock);
280
281         VCPU_EVENT(vcpu, 4, "sensed running status of cpu %x rc %x", cpu_addr,
282                    rc);
283
284         return rc;
285 }
286
287 static int __sigp_restart(struct kvm_vcpu *vcpu, u16 cpu_addr)
288 {
289         int rc = 0;
290         struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
291         struct kvm_s390_local_interrupt *li;
292
293         if (cpu_addr >= KVM_MAX_VCPUS)
294                 return 3; /* not operational */
295
296         spin_lock(&fi->lock);
297         li = fi->local_int[cpu_addr];
298         if (li == NULL) {
299                 rc = 3; /* not operational */
300                 goto out;
301         }
302
303         spin_lock_bh(&li->lock);
304         if (li->action_bits & ACTION_STOP_ON_STOP)
305                 rc = 2; /* busy */
306         else
307                 VCPU_EVENT(vcpu, 4, "sigp restart %x to handle userspace",
308                         cpu_addr);
309         spin_unlock_bh(&li->lock);
310 out:
311         spin_unlock(&fi->lock);
312         return rc;
313 }
314
315 int kvm_s390_handle_sigp(struct kvm_vcpu *vcpu)
316 {
317         int r1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
318         int r3 = vcpu->arch.sie_block->ipa & 0x000f;
319         int base2 = vcpu->arch.sie_block->ipb >> 28;
320         int disp2 = ((vcpu->arch.sie_block->ipb & 0x0fff0000) >> 16);
321         u32 parameter;
322         u16 cpu_addr = vcpu->run->s.regs.gprs[r3];
323         u8 order_code;
324         int rc;
325
326         /* sigp in userspace can exit */
327         if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
328                 return kvm_s390_inject_program_int(vcpu,
329                                                    PGM_PRIVILEGED_OPERATION);
330
331         order_code = disp2;
332         if (base2)
333                 order_code += vcpu->run->s.regs.gprs[base2];
334
335         if (r1 % 2)
336                 parameter = vcpu->run->s.regs.gprs[r1];
337         else
338                 parameter = vcpu->run->s.regs.gprs[r1 + 1];
339
340         switch (order_code) {
341         case SIGP_SENSE:
342                 vcpu->stat.instruction_sigp_sense++;
343                 rc = __sigp_sense(vcpu, cpu_addr,
344                                   &vcpu->run->s.regs.gprs[r1]);
345                 break;
346         case SIGP_EXTERNAL_CALL:
347                 vcpu->stat.instruction_sigp_external_call++;
348                 rc = __sigp_external_call(vcpu, cpu_addr);
349                 break;
350         case SIGP_EMERGENCY_SIGNAL:
351                 vcpu->stat.instruction_sigp_emergency++;
352                 rc = __sigp_emergency(vcpu, cpu_addr);
353                 break;
354         case SIGP_STOP:
355                 vcpu->stat.instruction_sigp_stop++;
356                 rc = __sigp_stop(vcpu, cpu_addr, ACTION_STOP_ON_STOP);
357                 break;
358         case SIGP_STOP_AND_STORE_STATUS:
359                 vcpu->stat.instruction_sigp_stop++;
360                 rc = __sigp_stop(vcpu, cpu_addr, ACTION_STORE_ON_STOP |
361                                                  ACTION_STOP_ON_STOP);
362                 break;
363         case SIGP_SET_ARCHITECTURE:
364                 vcpu->stat.instruction_sigp_arch++;
365                 rc = __sigp_set_arch(vcpu, parameter);
366                 break;
367         case SIGP_SET_PREFIX:
368                 vcpu->stat.instruction_sigp_prefix++;
369                 rc = __sigp_set_prefix(vcpu, cpu_addr, parameter,
370                                        &vcpu->run->s.regs.gprs[r1]);
371                 break;
372         case SIGP_SENSE_RUNNING:
373                 vcpu->stat.instruction_sigp_sense_running++;
374                 rc = __sigp_sense_running(vcpu, cpu_addr,
375                                           &vcpu->run->s.regs.gprs[r1]);
376                 break;
377         case SIGP_RESTART:
378                 vcpu->stat.instruction_sigp_restart++;
379                 rc = __sigp_restart(vcpu, cpu_addr);
380                 if (rc == 2) /* busy */
381                         break;
382                 /* user space must know about restart */
383         default:
384                 return -EOPNOTSUPP;
385         }
386
387         if (rc < 0)
388                 return rc;
389
390         vcpu->arch.sie_block->gpsw.mask &= ~(3ul << 44);
391         vcpu->arch.sie_block->gpsw.mask |= (rc & 3ul) << 44;
392         return 0;
393 }