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