~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/arch/s390/kvm/interrupt.c

Version: ~ [ linux-5.13-rc7 ] ~ [ linux-5.12.12 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.45 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.127 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.195 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.237 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.273 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.273 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.18.140 ] ~ [ linux-3.16.85 ] ~ [ linux-3.14.79 ] ~ [ linux-3.12.74 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /*
  2  * handling kvm guest interrupts
  3  *
  4  * Copyright IBM Corp. 2008
  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  */
 12 
 13 #include <linux/interrupt.h>
 14 #include <linux/kvm_host.h>
 15 #include <linux/hrtimer.h>
 16 #include <linux/signal.h>
 17 #include <linux/slab.h>
 18 #include <asm/asm-offsets.h>
 19 #include <asm/uaccess.h>
 20 #include "kvm-s390.h"
 21 #include "gaccess.h"
 22 #include "trace-s390.h"
 23 
 24 #define IOINT_SCHID_MASK 0x0000ffff
 25 #define IOINT_SSID_MASK 0x00030000
 26 #define IOINT_CSSID_MASK 0x03fc0000
 27 #define IOINT_AI_MASK 0x04000000
 28 
 29 static int is_ioint(u64 type)
 30 {
 31         return ((type & 0xfffe0000u) != 0xfffe0000u);
 32 }
 33 
 34 static int psw_extint_disabled(struct kvm_vcpu *vcpu)
 35 {
 36         return !(vcpu->arch.sie_block->gpsw.mask & PSW_MASK_EXT);
 37 }
 38 
 39 static int psw_ioint_disabled(struct kvm_vcpu *vcpu)
 40 {
 41         return !(vcpu->arch.sie_block->gpsw.mask & PSW_MASK_IO);
 42 }
 43 
 44 static int psw_mchk_disabled(struct kvm_vcpu *vcpu)
 45 {
 46         return !(vcpu->arch.sie_block->gpsw.mask & PSW_MASK_MCHECK);
 47 }
 48 
 49 static int psw_interrupts_disabled(struct kvm_vcpu *vcpu)
 50 {
 51         if ((vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PER) ||
 52             (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_IO) ||
 53             (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_EXT))
 54                 return 0;
 55         return 1;
 56 }
 57 
 58 static u64 int_word_to_isc_bits(u32 int_word)
 59 {
 60         u8 isc = (int_word & 0x38000000) >> 27;
 61 
 62         return (0x80 >> isc) << 24;
 63 }
 64 
 65 static int __interrupt_is_deliverable(struct kvm_vcpu *vcpu,
 66                                       struct kvm_s390_interrupt_info *inti)
 67 {
 68         switch (inti->type) {
 69         case KVM_S390_INT_EXTERNAL_CALL:
 70                 if (psw_extint_disabled(vcpu))
 71                         return 0;
 72                 if (vcpu->arch.sie_block->gcr[0] & 0x2000ul)
 73                         return 1;
 74                 return 0;
 75         case KVM_S390_INT_EMERGENCY:
 76                 if (psw_extint_disabled(vcpu))
 77                         return 0;
 78                 if (vcpu->arch.sie_block->gcr[0] & 0x4000ul)
 79                         return 1;
 80                 return 0;
 81         case KVM_S390_INT_SERVICE:
 82                 if (psw_extint_disabled(vcpu))
 83                         return 0;
 84                 if (vcpu->arch.sie_block->gcr[0] & 0x200ul)
 85                         return 1;
 86                 return 0;
 87         case KVM_S390_INT_VIRTIO:
 88                 if (psw_extint_disabled(vcpu))
 89                         return 0;
 90                 if (vcpu->arch.sie_block->gcr[0] & 0x200ul)
 91                         return 1;
 92                 return 0;
 93         case KVM_S390_PROGRAM_INT:
 94         case KVM_S390_SIGP_STOP:
 95         case KVM_S390_SIGP_SET_PREFIX:
 96         case KVM_S390_RESTART:
 97                 return 1;
 98         case KVM_S390_MCHK:
 99                 if (psw_mchk_disabled(vcpu))
100                         return 0;
101                 if (vcpu->arch.sie_block->gcr[14] & inti->mchk.cr14)
102                         return 1;
103                 return 0;
104         case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
105                 if (psw_ioint_disabled(vcpu))
106                         return 0;
107                 if (vcpu->arch.sie_block->gcr[6] &
108                     int_word_to_isc_bits(inti->io.io_int_word))
109                         return 1;
110                 return 0;
111         default:
112                 printk(KERN_WARNING "illegal interrupt type %llx\n",
113                        inti->type);
114                 BUG();
115         }
116         return 0;
117 }
118 
119 static void __set_cpu_idle(struct kvm_vcpu *vcpu)
120 {
121         BUG_ON(vcpu->vcpu_id > KVM_MAX_VCPUS - 1);
122         atomic_set_mask(CPUSTAT_WAIT, &vcpu->arch.sie_block->cpuflags);
123         set_bit(vcpu->vcpu_id, vcpu->arch.local_int.float_int->idle_mask);
124 }
125 
126 static void __unset_cpu_idle(struct kvm_vcpu *vcpu)
127 {
128         BUG_ON(vcpu->vcpu_id > KVM_MAX_VCPUS - 1);
129         atomic_clear_mask(CPUSTAT_WAIT, &vcpu->arch.sie_block->cpuflags);
130         clear_bit(vcpu->vcpu_id, vcpu->arch.local_int.float_int->idle_mask);
131 }
132 
133 static void __reset_intercept_indicators(struct kvm_vcpu *vcpu)
134 {
135         atomic_clear_mask(CPUSTAT_ECALL_PEND |
136                 CPUSTAT_IO_INT | CPUSTAT_EXT_INT | CPUSTAT_STOP_INT,
137                 &vcpu->arch.sie_block->cpuflags);
138         vcpu->arch.sie_block->lctl = 0x0000;
139         vcpu->arch.sie_block->ictl &= ~ICTL_LPSW;
140 }
141 
142 static void __set_cpuflag(struct kvm_vcpu *vcpu, u32 flag)
143 {
144         atomic_set_mask(flag, &vcpu->arch.sie_block->cpuflags);
145 }
146 
147 static void __set_intercept_indicator(struct kvm_vcpu *vcpu,
148                                       struct kvm_s390_interrupt_info *inti)
149 {
150         switch (inti->type) {
151         case KVM_S390_INT_EXTERNAL_CALL:
152         case KVM_S390_INT_EMERGENCY:
153         case KVM_S390_INT_SERVICE:
154         case KVM_S390_INT_VIRTIO:
155                 if (psw_extint_disabled(vcpu))
156                         __set_cpuflag(vcpu, CPUSTAT_EXT_INT);
157                 else
158                         vcpu->arch.sie_block->lctl |= LCTL_CR0;
159                 break;
160         case KVM_S390_SIGP_STOP:
161                 __set_cpuflag(vcpu, CPUSTAT_STOP_INT);
162                 break;
163         case KVM_S390_MCHK:
164                 if (psw_mchk_disabled(vcpu))
165                         vcpu->arch.sie_block->ictl |= ICTL_LPSW;
166                 else
167                         vcpu->arch.sie_block->lctl |= LCTL_CR14;
168                 break;
169         case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
170                 if (psw_ioint_disabled(vcpu))
171                         __set_cpuflag(vcpu, CPUSTAT_IO_INT);
172                 else
173                         vcpu->arch.sie_block->lctl |= LCTL_CR6;
174                 break;
175         default:
176                 BUG();
177         }
178 }
179 
180 static void __do_deliver_interrupt(struct kvm_vcpu *vcpu,
181                                    struct kvm_s390_interrupt_info *inti)
182 {
183         const unsigned short table[] = { 2, 4, 4, 6 };
184         int rc = 0;
185 
186         switch (inti->type) {
187         case KVM_S390_INT_EMERGENCY:
188                 VCPU_EVENT(vcpu, 4, "%s", "interrupt: sigp emerg");
189                 vcpu->stat.deliver_emergency_signal++;
190                 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
191                                                  inti->emerg.code, 0);
192                 rc  = put_guest(vcpu, 0x1201, (u16 __user *)__LC_EXT_INT_CODE);
193                 rc |= put_guest(vcpu, inti->emerg.code,
194                                 (u16 __user *)__LC_EXT_CPU_ADDR);
195                 rc |= copy_to_guest(vcpu, __LC_EXT_OLD_PSW,
196                                     &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
197                 rc |= copy_from_guest(vcpu, &vcpu->arch.sie_block->gpsw,
198                                       __LC_EXT_NEW_PSW, sizeof(psw_t));
199                 break;
200         case KVM_S390_INT_EXTERNAL_CALL:
201                 VCPU_EVENT(vcpu, 4, "%s", "interrupt: sigp ext call");
202                 vcpu->stat.deliver_external_call++;
203                 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
204                                                  inti->extcall.code, 0);
205                 rc  = put_guest(vcpu, 0x1202, (u16 __user *)__LC_EXT_INT_CODE);
206                 rc |= put_guest(vcpu, inti->extcall.code,
207                                 (u16 __user *)__LC_EXT_CPU_ADDR);
208                 rc |= copy_to_guest(vcpu, __LC_EXT_OLD_PSW,
209                                     &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
210                 rc |= copy_from_guest(vcpu, &vcpu->arch.sie_block->gpsw,
211                                       __LC_EXT_NEW_PSW, sizeof(psw_t));
212                 break;
213         case KVM_S390_INT_SERVICE:
214                 VCPU_EVENT(vcpu, 4, "interrupt: sclp parm:%x",
215                            inti->ext.ext_params);
216                 vcpu->stat.deliver_service_signal++;
217                 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
218                                                  inti->ext.ext_params, 0);
219                 rc  = put_guest(vcpu, 0x2401, (u16 __user *)__LC_EXT_INT_CODE);
220                 rc |= copy_to_guest(vcpu, __LC_EXT_OLD_PSW,
221                                     &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
222                 rc |= copy_from_guest(vcpu, &vcpu->arch.sie_block->gpsw,
223                                       __LC_EXT_NEW_PSW, sizeof(psw_t));
224                 rc |= put_guest(vcpu, inti->ext.ext_params,
225                                 (u32 __user *)__LC_EXT_PARAMS);
226                 break;
227         case KVM_S390_INT_VIRTIO:
228                 VCPU_EVENT(vcpu, 4, "interrupt: virtio parm:%x,parm64:%llx",
229                            inti->ext.ext_params, inti->ext.ext_params2);
230                 vcpu->stat.deliver_virtio_interrupt++;
231                 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
232                                                  inti->ext.ext_params,
233                                                  inti->ext.ext_params2);
234                 rc  = put_guest(vcpu, 0x2603, (u16 __user *)__LC_EXT_INT_CODE);
235                 rc |= put_guest(vcpu, 0x0d00, (u16 __user *)__LC_EXT_CPU_ADDR);
236                 rc |= copy_to_guest(vcpu, __LC_EXT_OLD_PSW,
237                                     &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
238                 rc |= copy_from_guest(vcpu, &vcpu->arch.sie_block->gpsw,
239                                       __LC_EXT_NEW_PSW, sizeof(psw_t));
240                 rc |= put_guest(vcpu, inti->ext.ext_params,
241                                 (u32 __user *)__LC_EXT_PARAMS);
242                 rc |= put_guest(vcpu, inti->ext.ext_params2,
243                                 (u64 __user *)__LC_EXT_PARAMS2);
244                 break;
245         case KVM_S390_SIGP_STOP:
246                 VCPU_EVENT(vcpu, 4, "%s", "interrupt: cpu stop");
247                 vcpu->stat.deliver_stop_signal++;
248                 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
249                                                  0, 0);
250                 __set_intercept_indicator(vcpu, inti);
251                 break;
252 
253         case KVM_S390_SIGP_SET_PREFIX:
254                 VCPU_EVENT(vcpu, 4, "interrupt: set prefix to %x",
255                            inti->prefix.address);
256                 vcpu->stat.deliver_prefix_signal++;
257                 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
258                                                  inti->prefix.address, 0);
259                 kvm_s390_set_prefix(vcpu, inti->prefix.address);
260                 break;
261 
262         case KVM_S390_RESTART:
263                 VCPU_EVENT(vcpu, 4, "%s", "interrupt: cpu restart");
264                 vcpu->stat.deliver_restart_signal++;
265                 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
266                                                  0, 0);
267                 rc  = copy_to_guest(vcpu,
268                                     offsetof(struct _lowcore, restart_old_psw),
269                                     &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
270                 rc |= copy_from_guest(vcpu, &vcpu->arch.sie_block->gpsw,
271                                       offsetof(struct _lowcore, restart_psw),
272                                       sizeof(psw_t));
273                 atomic_clear_mask(CPUSTAT_STOPPED, &vcpu->arch.sie_block->cpuflags);
274                 break;
275         case KVM_S390_PROGRAM_INT:
276                 VCPU_EVENT(vcpu, 4, "interrupt: pgm check code:%x, ilc:%x",
277                            inti->pgm.code,
278                            table[vcpu->arch.sie_block->ipa >> 14]);
279                 vcpu->stat.deliver_program_int++;
280                 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
281                                                  inti->pgm.code, 0);
282                 rc  = put_guest(vcpu, inti->pgm.code, (u16 __user *)__LC_PGM_INT_CODE);
283                 rc |= put_guest(vcpu, table[vcpu->arch.sie_block->ipa >> 14],
284                                 (u16 __user *)__LC_PGM_ILC);
285                 rc |= copy_to_guest(vcpu, __LC_PGM_OLD_PSW,
286                                     &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
287                 rc |= copy_from_guest(vcpu, &vcpu->arch.sie_block->gpsw,
288                                       __LC_PGM_NEW_PSW, sizeof(psw_t));
289                 break;
290 
291         case KVM_S390_MCHK:
292                 VCPU_EVENT(vcpu, 4, "interrupt: machine check mcic=%llx",
293                            inti->mchk.mcic);
294                 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
295                                                  inti->mchk.cr14,
296                                                  inti->mchk.mcic);
297                 rc  = kvm_s390_vcpu_store_status(vcpu,
298                                                  KVM_S390_STORE_STATUS_PREFIXED);
299                 rc |= put_guest(vcpu, inti->mchk.mcic, (u64 __user *) __LC_MCCK_CODE);
300                 rc |= copy_to_guest(vcpu, __LC_MCK_OLD_PSW,
301                                     &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
302                 rc |= copy_from_guest(vcpu, &vcpu->arch.sie_block->gpsw,
303                                       __LC_MCK_NEW_PSW, sizeof(psw_t));
304                 break;
305 
306         case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
307         {
308                 __u32 param0 = ((__u32)inti->io.subchannel_id << 16) |
309                         inti->io.subchannel_nr;
310                 __u64 param1 = ((__u64)inti->io.io_int_parm << 32) |
311                         inti->io.io_int_word;
312                 VCPU_EVENT(vcpu, 4, "interrupt: I/O %llx", inti->type);
313                 vcpu->stat.deliver_io_int++;
314                 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
315                                                  param0, param1);
316                 rc  = put_guest(vcpu, inti->io.subchannel_id,
317                                 (u16 __user *) __LC_SUBCHANNEL_ID);
318                 rc |= put_guest(vcpu, inti->io.subchannel_nr,
319                                 (u16 __user *) __LC_SUBCHANNEL_NR);
320                 rc |= put_guest(vcpu, inti->io.io_int_parm,
321                                 (u32 __user *) __LC_IO_INT_PARM);
322                 rc |= put_guest(vcpu, inti->io.io_int_word,
323                                 (u32 __user *) __LC_IO_INT_WORD);
324                 rc |= copy_to_guest(vcpu, __LC_IO_OLD_PSW,
325                                     &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
326                 rc |= copy_from_guest(vcpu, &vcpu->arch.sie_block->gpsw,
327                                       __LC_IO_NEW_PSW, sizeof(psw_t));
328                 break;
329         }
330         default:
331                 BUG();
332         }
333         if (rc) {
334                 printk("kvm: The guest lowcore is not mapped during interrupt "
335                        "delivery, killing userspace\n");
336                 do_exit(SIGKILL);
337         }
338 }
339 
340 static int __try_deliver_ckc_interrupt(struct kvm_vcpu *vcpu)
341 {
342         int rc;
343 
344         if (psw_extint_disabled(vcpu))
345                 return 0;
346         if (!(vcpu->arch.sie_block->gcr[0] & 0x800ul))
347                 return 0;
348         rc  = put_guest(vcpu, 0x1004, (u16 __user *)__LC_EXT_INT_CODE);
349         rc |= copy_to_guest(vcpu, __LC_EXT_OLD_PSW,
350                             &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
351         rc |= copy_from_guest(vcpu, &vcpu->arch.sie_block->gpsw,
352                               __LC_EXT_NEW_PSW, sizeof(psw_t));
353         if (rc) {
354                 printk("kvm: The guest lowcore is not mapped during interrupt "
355                         "delivery, killing userspace\n");
356                 do_exit(SIGKILL);
357         }
358         return 1;
359 }
360 
361 static int kvm_cpu_has_interrupt(struct kvm_vcpu *vcpu)
362 {
363         struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
364         struct kvm_s390_float_interrupt *fi = vcpu->arch.local_int.float_int;
365         struct kvm_s390_interrupt_info  *inti;
366         int rc = 0;
367 
368         if (atomic_read(&li->active)) {
369                 spin_lock_bh(&li->lock);
370                 list_for_each_entry(inti, &li->list, list)
371                         if (__interrupt_is_deliverable(vcpu, inti)) {
372                                 rc = 1;
373                                 break;
374                         }
375                 spin_unlock_bh(&li->lock);
376         }
377 
378         if ((!rc) && atomic_read(&fi->active)) {
379                 spin_lock(&fi->lock);
380                 list_for_each_entry(inti, &fi->list, list)
381                         if (__interrupt_is_deliverable(vcpu, inti)) {
382                                 rc = 1;
383                                 break;
384                         }
385                 spin_unlock(&fi->lock);
386         }
387 
388         if ((!rc) && (vcpu->arch.sie_block->ckc <
389                 get_tod_clock_fast() + vcpu->arch.sie_block->epoch)) {
390                 if ((!psw_extint_disabled(vcpu)) &&
391                         (vcpu->arch.sie_block->gcr[0] & 0x800ul))
392                         rc = 1;
393         }
394 
395         return rc;
396 }
397 
398 int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
399 {
400         return 0;
401 }
402 
403 int kvm_s390_handle_wait(struct kvm_vcpu *vcpu)
404 {
405         u64 now, sltime;
406         DECLARE_WAITQUEUE(wait, current);
407 
408         vcpu->stat.exit_wait_state++;
409         if (kvm_cpu_has_interrupt(vcpu))
410                 return 0;
411 
412         __set_cpu_idle(vcpu);
413         spin_lock_bh(&vcpu->arch.local_int.lock);
414         vcpu->arch.local_int.timer_due = 0;
415         spin_unlock_bh(&vcpu->arch.local_int.lock);
416 
417         if (psw_interrupts_disabled(vcpu)) {
418                 VCPU_EVENT(vcpu, 3, "%s", "disabled wait");
419                 __unset_cpu_idle(vcpu);
420                 return -EOPNOTSUPP; /* disabled wait */
421         }
422 
423         if (psw_extint_disabled(vcpu) ||
424             (!(vcpu->arch.sie_block->gcr[0] & 0x800ul))) {
425                 VCPU_EVENT(vcpu, 3, "%s", "enabled wait w/o timer");
426                 goto no_timer;
427         }
428 
429         now = get_tod_clock_fast() + vcpu->arch.sie_block->epoch;
430         if (vcpu->arch.sie_block->ckc < now) {
431                 __unset_cpu_idle(vcpu);
432                 return 0;
433         }
434 
435         sltime = tod_to_ns(vcpu->arch.sie_block->ckc - now);
436 
437         hrtimer_start(&vcpu->arch.ckc_timer, ktime_set (0, sltime) , HRTIMER_MODE_REL);
438         VCPU_EVENT(vcpu, 5, "enabled wait via clock comparator: %llx ns", sltime);
439 no_timer:
440         srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
441         spin_lock(&vcpu->arch.local_int.float_int->lock);
442         spin_lock_bh(&vcpu->arch.local_int.lock);
443         add_wait_queue(&vcpu->wq, &wait);
444         while (list_empty(&vcpu->arch.local_int.list) &&
445                 list_empty(&vcpu->arch.local_int.float_int->list) &&
446                 (!vcpu->arch.local_int.timer_due) &&
447                 !signal_pending(current)) {
448                 set_current_state(TASK_INTERRUPTIBLE);
449                 spin_unlock_bh(&vcpu->arch.local_int.lock);
450                 spin_unlock(&vcpu->arch.local_int.float_int->lock);
451                 schedule();
452                 spin_lock(&vcpu->arch.local_int.float_int->lock);
453                 spin_lock_bh(&vcpu->arch.local_int.lock);
454         }
455         __unset_cpu_idle(vcpu);
456         __set_current_state(TASK_RUNNING);
457         remove_wait_queue(&vcpu->wq, &wait);
458         spin_unlock_bh(&vcpu->arch.local_int.lock);
459         spin_unlock(&vcpu->arch.local_int.float_int->lock);
460         vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
461 
462         hrtimer_try_to_cancel(&vcpu->arch.ckc_timer);
463         return 0;
464 }
465 
466 void kvm_s390_tasklet(unsigned long parm)
467 {
468         struct kvm_vcpu *vcpu = (struct kvm_vcpu *) parm;
469 
470         spin_lock(&vcpu->arch.local_int.lock);
471         vcpu->arch.local_int.timer_due = 1;
472         if (waitqueue_active(&vcpu->wq))
473                 wake_up_interruptible(&vcpu->wq);
474         spin_unlock(&vcpu->arch.local_int.lock);
475 }
476 
477 /*
478  * low level hrtimer wake routine. Because this runs in hardirq context
479  * we schedule a tasklet to do the real work.
480  */
481 enum hrtimer_restart kvm_s390_idle_wakeup(struct hrtimer *timer)
482 {
483         struct kvm_vcpu *vcpu;
484 
485         vcpu = container_of(timer, struct kvm_vcpu, arch.ckc_timer);
486         tasklet_schedule(&vcpu->arch.tasklet);
487 
488         return HRTIMER_NORESTART;
489 }
490 
491 void kvm_s390_deliver_pending_interrupts(struct kvm_vcpu *vcpu)
492 {
493         struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
494         struct kvm_s390_float_interrupt *fi = vcpu->arch.local_int.float_int;
495         struct kvm_s390_interrupt_info  *n, *inti = NULL;
496         int deliver;
497 
498         __reset_intercept_indicators(vcpu);
499         if (atomic_read(&li->active)) {
500                 do {
501                         deliver = 0;
502                         spin_lock_bh(&li->lock);
503                         list_for_each_entry_safe(inti, n, &li->list, list) {
504                                 if (__interrupt_is_deliverable(vcpu, inti)) {
505                                         list_del(&inti->list);
506                                         deliver = 1;
507                                         break;
508                                 }
509                                 __set_intercept_indicator(vcpu, inti);
510                         }
511                         if (list_empty(&li->list))
512                                 atomic_set(&li->active, 0);
513                         spin_unlock_bh(&li->lock);
514                         if (deliver) {
515                                 __do_deliver_interrupt(vcpu, inti);
516                                 kfree(inti);
517                         }
518                 } while (deliver);
519         }
520 
521         if ((vcpu->arch.sie_block->ckc <
522                 get_tod_clock_fast() + vcpu->arch.sie_block->epoch))
523                 __try_deliver_ckc_interrupt(vcpu);
524 
525         if (atomic_read(&fi->active)) {
526                 do {
527                         deliver = 0;
528                         spin_lock(&fi->lock);
529                         list_for_each_entry_safe(inti, n, &fi->list, list) {
530                                 if (__interrupt_is_deliverable(vcpu, inti)) {
531                                         list_del(&inti->list);
532                                         deliver = 1;
533                                         break;
534                                 }
535                                 __set_intercept_indicator(vcpu, inti);
536                         }
537                         if (list_empty(&fi->list))
538                                 atomic_set(&fi->active, 0);
539                         spin_unlock(&fi->lock);
540                         if (deliver) {
541                                 __do_deliver_interrupt(vcpu, inti);
542                                 kfree(inti);
543                         }
544                 } while (deliver);
545         }
546 }
547 
548 void kvm_s390_deliver_pending_machine_checks(struct kvm_vcpu *vcpu)
549 {
550         struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
551         struct kvm_s390_float_interrupt *fi = vcpu->arch.local_int.float_int;
552         struct kvm_s390_interrupt_info  *n, *inti = NULL;
553         int deliver;
554 
555         __reset_intercept_indicators(vcpu);
556         if (atomic_read(&li->active)) {
557                 do {
558                         deliver = 0;
559                         spin_lock_bh(&li->lock);
560                         list_for_each_entry_safe(inti, n, &li->list, list) {
561                                 if ((inti->type == KVM_S390_MCHK) &&
562                                     __interrupt_is_deliverable(vcpu, inti)) {
563                                         list_del(&inti->list);
564                                         deliver = 1;
565                                         break;
566                                 }
567                                 __set_intercept_indicator(vcpu, inti);
568                         }
569                         if (list_empty(&li->list))
570                                 atomic_set(&li->active, 0);
571                         spin_unlock_bh(&li->lock);
572                         if (deliver) {
573                                 __do_deliver_interrupt(vcpu, inti);
574                                 kfree(inti);
575                         }
576                 } while (deliver);
577         }
578 
579         if (atomic_read(&fi->active)) {
580                 do {
581                         deliver = 0;
582                         spin_lock(&fi->lock);
583                         list_for_each_entry_safe(inti, n, &fi->list, list) {
584                                 if ((inti->type == KVM_S390_MCHK) &&
585                                     __interrupt_is_deliverable(vcpu, inti)) {
586                                         list_del(&inti->list);
587                                         deliver = 1;
588                                         break;
589                                 }
590                                 __set_intercept_indicator(vcpu, inti);
591                         }
592                         if (list_empty(&fi->list))
593                                 atomic_set(&fi->active, 0);
594                         spin_unlock(&fi->lock);
595                         if (deliver) {
596                                 __do_deliver_interrupt(vcpu, inti);
597                                 kfree(inti);
598                         }
599                 } while (deliver);
600         }
601 }
602 
603 int kvm_s390_inject_program_int(struct kvm_vcpu *vcpu, u16 code)
604 {
605         struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
606         struct kvm_s390_interrupt_info *inti;
607 
608         inti = kzalloc(sizeof(*inti), GFP_KERNEL);
609         if (!inti)
610                 return -ENOMEM;
611 
612         inti->type = KVM_S390_PROGRAM_INT;
613         inti->pgm.code = code;
614 
615         VCPU_EVENT(vcpu, 3, "inject: program check %d (from kernel)", code);
616         trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, inti->type, code, 0, 1);
617         spin_lock_bh(&li->lock);
618         list_add(&inti->list, &li->list);
619         atomic_set(&li->active, 1);
620         BUG_ON(waitqueue_active(li->wq));
621         spin_unlock_bh(&li->lock);
622         return 0;
623 }
624 
625 struct kvm_s390_interrupt_info *kvm_s390_get_io_int(struct kvm *kvm,
626                                                     u64 cr6, u64 schid)
627 {
628         struct kvm_s390_float_interrupt *fi;
629         struct kvm_s390_interrupt_info *inti, *iter;
630 
631         if ((!schid && !cr6) || (schid && cr6))
632                 return NULL;
633         mutex_lock(&kvm->lock);
634         fi = &kvm->arch.float_int;
635         spin_lock(&fi->lock);
636         inti = NULL;
637         list_for_each_entry(iter, &fi->list, list) {
638                 if (!is_ioint(iter->type))
639                         continue;
640                 if (cr6 &&
641                     ((cr6 & int_word_to_isc_bits(iter->io.io_int_word)) == 0))
642                         continue;
643                 if (schid) {
644                         if (((schid & 0x00000000ffff0000) >> 16) !=
645                             iter->io.subchannel_id)
646                                 continue;
647                         if ((schid & 0x000000000000ffff) !=
648                             iter->io.subchannel_nr)
649                                 continue;
650                 }
651                 inti = iter;
652                 break;
653         }
654         if (inti)
655                 list_del_init(&inti->list);
656         if (list_empty(&fi->list))
657                 atomic_set(&fi->active, 0);
658         spin_unlock(&fi->lock);
659         mutex_unlock(&kvm->lock);
660         return inti;
661 }
662 
663 int kvm_s390_inject_vm(struct kvm *kvm,
664                        struct kvm_s390_interrupt *s390int)
665 {
666         struct kvm_s390_local_interrupt *li;
667         struct kvm_s390_float_interrupt *fi;
668         struct kvm_s390_interrupt_info *inti, *iter;
669         int sigcpu;
670 
671         inti = kzalloc(sizeof(*inti), GFP_KERNEL);
672         if (!inti)
673                 return -ENOMEM;
674 
675         switch (s390int->type) {
676         case KVM_S390_INT_VIRTIO:
677                 VM_EVENT(kvm, 5, "inject: virtio parm:%x,parm64:%llx",
678                          s390int->parm, s390int->parm64);
679                 inti->type = s390int->type;
680                 inti->ext.ext_params = s390int->parm;
681                 inti->ext.ext_params2 = s390int->parm64;
682                 break;
683         case KVM_S390_INT_SERVICE:
684                 VM_EVENT(kvm, 5, "inject: sclp parm:%x", s390int->parm);
685                 inti->type = s390int->type;
686                 inti->ext.ext_params = s390int->parm;
687                 break;
688         case KVM_S390_PROGRAM_INT:
689         case KVM_S390_SIGP_STOP:
690         case KVM_S390_INT_EXTERNAL_CALL:
691         case KVM_S390_INT_EMERGENCY:
692                 kfree(inti);
693                 return -EINVAL;
694         case KVM_S390_MCHK:
695                 VM_EVENT(kvm, 5, "inject: machine check parm64:%llx",
696                          s390int->parm64);
697                 inti->type = s390int->type;
698                 inti->mchk.cr14 = s390int->parm; /* upper bits are not used */
699                 inti->mchk.mcic = s390int->parm64;
700                 break;
701         case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
702                 if (s390int->type & IOINT_AI_MASK)
703                         VM_EVENT(kvm, 5, "%s", "inject: I/O (AI)");
704                 else
705                         VM_EVENT(kvm, 5, "inject: I/O css %x ss %x schid %04x",
706                                  s390int->type & IOINT_CSSID_MASK,
707                                  s390int->type & IOINT_SSID_MASK,
708                                  s390int->type & IOINT_SCHID_MASK);
709                 inti->type = s390int->type;
710                 inti->io.subchannel_id = s390int->parm >> 16;
711                 inti->io.subchannel_nr = s390int->parm & 0x0000ffffu;
712                 inti->io.io_int_parm = s390int->parm64 >> 32;
713                 inti->io.io_int_word = s390int->parm64 & 0x00000000ffffffffull;
714                 break;
715         default:
716                 kfree(inti);
717                 return -EINVAL;
718         }
719         trace_kvm_s390_inject_vm(s390int->type, s390int->parm, s390int->parm64,
720                                  2);
721 
722         mutex_lock(&kvm->lock);
723         fi = &kvm->arch.float_int;
724         spin_lock(&fi->lock);
725         if (!is_ioint(inti->type))
726                 list_add_tail(&inti->list, &fi->list);
727         else {
728                 u64 isc_bits = int_word_to_isc_bits(inti->io.io_int_word);
729 
730                 /* Keep I/O interrupts sorted in isc order. */
731                 list_for_each_entry(iter, &fi->list, list) {
732                         if (!is_ioint(iter->type))
733                                 continue;
734                         if (int_word_to_isc_bits(iter->io.io_int_word)
735                             <= isc_bits)
736                                 continue;
737                         break;
738                 }
739                 list_add_tail(&inti->list, &iter->list);
740         }
741         atomic_set(&fi->active, 1);
742         sigcpu = find_first_bit(fi->idle_mask, KVM_MAX_VCPUS);
743         if (sigcpu == KVM_MAX_VCPUS) {
744                 do {
745                         sigcpu = fi->next_rr_cpu++;
746                         if (sigcpu == KVM_MAX_VCPUS)
747                                 sigcpu = fi->next_rr_cpu = 0;
748                 } while (fi->local_int[sigcpu] == NULL);
749         }
750         li = fi->local_int[sigcpu];
751         spin_lock_bh(&li->lock);
752         atomic_set_mask(CPUSTAT_EXT_INT, li->cpuflags);
753         if (waitqueue_active(li->wq))
754                 wake_up_interruptible(li->wq);
755         spin_unlock_bh(&li->lock);
756         spin_unlock(&fi->lock);
757         mutex_unlock(&kvm->lock);
758         return 0;
759 }
760 
761 int kvm_s390_inject_vcpu(struct kvm_vcpu *vcpu,
762                          struct kvm_s390_interrupt *s390int)
763 {
764         struct kvm_s390_local_interrupt *li;
765         struct kvm_s390_interrupt_info *inti;
766 
767         inti = kzalloc(sizeof(*inti), GFP_KERNEL);
768         if (!inti)
769                 return -ENOMEM;
770 
771         switch (s390int->type) {
772         case KVM_S390_PROGRAM_INT:
773                 if (s390int->parm & 0xffff0000) {
774                         kfree(inti);
775                         return -EINVAL;
776                 }
777                 inti->type = s390int->type;
778                 inti->pgm.code = s390int->parm;
779                 VCPU_EVENT(vcpu, 3, "inject: program check %d (from user)",
780                            s390int->parm);
781                 break;
782         case KVM_S390_SIGP_SET_PREFIX:
783                 inti->prefix.address = s390int->parm;
784                 inti->type = s390int->type;
785                 VCPU_EVENT(vcpu, 3, "inject: set prefix to %x (from user)",
786                            s390int->parm);
787                 break;
788         case KVM_S390_SIGP_STOP:
789         case KVM_S390_RESTART:
790                 VCPU_EVENT(vcpu, 3, "inject: type %x", s390int->type);
791                 inti->type = s390int->type;
792                 break;
793         case KVM_S390_INT_EXTERNAL_CALL:
794                 if (s390int->parm & 0xffff0000) {
795                         kfree(inti);
796                         return -EINVAL;
797                 }
798                 VCPU_EVENT(vcpu, 3, "inject: external call source-cpu:%u",
799                            s390int->parm);
800                 inti->type = s390int->type;
801                 inti->extcall.code = s390int->parm;
802                 break;
803         case KVM_S390_INT_EMERGENCY:
804                 if (s390int->parm & 0xffff0000) {
805                         kfree(inti);
806                         return -EINVAL;
807                 }
808                 VCPU_EVENT(vcpu, 3, "inject: emergency %u\n", s390int->parm);
809                 inti->type = s390int->type;
810                 inti->emerg.code = s390int->parm;
811                 break;
812         case KVM_S390_MCHK:
813                 VCPU_EVENT(vcpu, 5, "inject: machine check parm64:%llx",
814                            s390int->parm64);
815                 inti->type = s390int->type;
816                 inti->mchk.mcic = s390int->parm64;
817                 break;
818         case KVM_S390_INT_VIRTIO:
819         case KVM_S390_INT_SERVICE:
820         case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
821         default:
822                 kfree(inti);
823                 return -EINVAL;
824         }
825         trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, s390int->type, s390int->parm,
826                                    s390int->parm64, 2);
827 
828         mutex_lock(&vcpu->kvm->lock);
829         li = &vcpu->arch.local_int;
830         spin_lock_bh(&li->lock);
831         if (inti->type == KVM_S390_PROGRAM_INT)
832                 list_add(&inti->list, &li->list);
833         else
834                 list_add_tail(&inti->list, &li->list);
835         atomic_set(&li->active, 1);
836         if (inti->type == KVM_S390_SIGP_STOP)
837                 li->action_bits |= ACTION_STOP_ON_STOP;
838         atomic_set_mask(CPUSTAT_EXT_INT, li->cpuflags);
839         if (waitqueue_active(&vcpu->wq))
840                 wake_up_interruptible(&vcpu->wq);
841         spin_unlock_bh(&li->lock);
842         mutex_unlock(&vcpu->kvm->lock);
843         return 0;
844 }
845 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp