forked from ~ljy/RK356X_SDK_RELEASE

hc
2023-12-11 6778948f9de86c3cfaf36725a7c87dcff9ba247f
kernel/arch/s390/kvm/interrupt.c
....@@ -2,15 +2,19 @@
22 /*
33 * handling kvm guest interrupts
44 *
5
- * Copyright IBM Corp. 2008, 2015
5
+ * Copyright IBM Corp. 2008, 2020
66 *
77 * Author(s): Carsten Otte <cotte@de.ibm.com>
88 */
9
+
10
+#define KMSG_COMPONENT "kvm-s390"
11
+#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
912
1013 #include <linux/interrupt.h>
1114 #include <linux/kvm_host.h>
1215 #include <linux/hrtimer.h>
1316 #include <linux/mmu_context.h>
17
+#include <linux/nospec.h>
1418 #include <linux/signal.h>
1519 #include <linux/slab.h>
1620 #include <linux/bitmap.h>
....@@ -23,6 +27,7 @@
2327 #include <asm/gmap.h>
2428 #include <asm/switch_to.h>
2529 #include <asm/nmi.h>
30
+#include <asm/airq.h>
2631 #include "kvm-s390.h"
2732 #include "gaccess.h"
2833 #include "trace-s390.h"
....@@ -30,6 +35,8 @@
3035 #define PFAULT_INIT 0x0600
3136 #define PFAULT_DONE 0x0680
3237 #define VIRTIO_PARAM 0x0d00
38
+
39
+static struct kvm_s390_gib *gib;
3340
3441 /* handle external calls via sigp interpretation facility */
3542 static int sca_ext_call_pending(struct kvm_vcpu *vcpu, int *src_id)
....@@ -217,36 +224,122 @@
217224 */
218225 #define IPM_BIT_OFFSET (offsetof(struct kvm_s390_gisa, ipm) * BITS_PER_BYTE)
219226
220
-static inline void kvm_s390_gisa_set_ipm_gisc(struct kvm_s390_gisa *gisa, u32 gisc)
227
+/**
228
+ * gisa_set_iam - change the GISA interruption alert mask
229
+ *
230
+ * @gisa: gisa to operate on
231
+ * @iam: new IAM value to use
232
+ *
233
+ * Change the IAM atomically with the next alert address and the IPM
234
+ * of the GISA if the GISA is not part of the GIB alert list. All three
235
+ * fields are located in the first long word of the GISA.
236
+ *
237
+ * Returns: 0 on success
238
+ * -EBUSY in case the gisa is part of the alert list
239
+ */
240
+static inline int gisa_set_iam(struct kvm_s390_gisa *gisa, u8 iam)
241
+{
242
+ u64 word, _word;
243
+
244
+ do {
245
+ word = READ_ONCE(gisa->u64.word[0]);
246
+ if ((u64)gisa != word >> 32)
247
+ return -EBUSY;
248
+ _word = (word & ~0xffUL) | iam;
249
+ } while (cmpxchg(&gisa->u64.word[0], word, _word) != word);
250
+
251
+ return 0;
252
+}
253
+
254
+/**
255
+ * gisa_clear_ipm - clear the GISA interruption pending mask
256
+ *
257
+ * @gisa: gisa to operate on
258
+ *
259
+ * Clear the IPM atomically with the next alert address and the IAM
260
+ * of the GISA unconditionally. All three fields are located in the
261
+ * first long word of the GISA.
262
+ */
263
+static inline void gisa_clear_ipm(struct kvm_s390_gisa *gisa)
264
+{
265
+ u64 word, _word;
266
+
267
+ do {
268
+ word = READ_ONCE(gisa->u64.word[0]);
269
+ _word = word & ~(0xffUL << 24);
270
+ } while (cmpxchg(&gisa->u64.word[0], word, _word) != word);
271
+}
272
+
273
+/**
274
+ * gisa_get_ipm_or_restore_iam - return IPM or restore GISA IAM
275
+ *
276
+ * @gi: gisa interrupt struct to work on
277
+ *
278
+ * Atomically restores the interruption alert mask if none of the
279
+ * relevant ISCs are pending and return the IPM.
280
+ *
281
+ * Returns: the relevant pending ISCs
282
+ */
283
+static inline u8 gisa_get_ipm_or_restore_iam(struct kvm_s390_gisa_interrupt *gi)
284
+{
285
+ u8 pending_mask, alert_mask;
286
+ u64 word, _word;
287
+
288
+ do {
289
+ word = READ_ONCE(gi->origin->u64.word[0]);
290
+ alert_mask = READ_ONCE(gi->alert.mask);
291
+ pending_mask = (u8)(word >> 24) & alert_mask;
292
+ if (pending_mask)
293
+ return pending_mask;
294
+ _word = (word & ~0xffUL) | alert_mask;
295
+ } while (cmpxchg(&gi->origin->u64.word[0], word, _word) != word);
296
+
297
+ return 0;
298
+}
299
+
300
+static inline int gisa_in_alert_list(struct kvm_s390_gisa *gisa)
301
+{
302
+ return READ_ONCE(gisa->next_alert) != (u32)(u64)gisa;
303
+}
304
+
305
+static inline void gisa_set_ipm_gisc(struct kvm_s390_gisa *gisa, u32 gisc)
221306 {
222307 set_bit_inv(IPM_BIT_OFFSET + gisc, (unsigned long *) gisa);
223308 }
224309
225
-static inline u8 kvm_s390_gisa_get_ipm(struct kvm_s390_gisa *gisa)
310
+static inline u8 gisa_get_ipm(struct kvm_s390_gisa *gisa)
226311 {
227312 return READ_ONCE(gisa->ipm);
228313 }
229314
230
-static inline void kvm_s390_gisa_clear_ipm_gisc(struct kvm_s390_gisa *gisa, u32 gisc)
315
+static inline void gisa_clear_ipm_gisc(struct kvm_s390_gisa *gisa, u32 gisc)
231316 {
232317 clear_bit_inv(IPM_BIT_OFFSET + gisc, (unsigned long *) gisa);
233318 }
234319
235
-static inline int kvm_s390_gisa_tac_ipm_gisc(struct kvm_s390_gisa *gisa, u32 gisc)
320
+static inline int gisa_tac_ipm_gisc(struct kvm_s390_gisa *gisa, u32 gisc)
236321 {
237322 return test_and_clear_bit_inv(IPM_BIT_OFFSET + gisc, (unsigned long *) gisa);
238323 }
239324
240325 static inline unsigned long pending_irqs_no_gisa(struct kvm_vcpu *vcpu)
241326 {
242
- return vcpu->kvm->arch.float_int.pending_irqs |
243
- vcpu->arch.local_int.pending_irqs;
327
+ unsigned long pending = vcpu->kvm->arch.float_int.pending_irqs |
328
+ vcpu->arch.local_int.pending_irqs;
329
+
330
+ pending &= ~vcpu->kvm->arch.float_int.masked_irqs;
331
+ return pending;
244332 }
245333
246334 static inline unsigned long pending_irqs(struct kvm_vcpu *vcpu)
247335 {
248
- return pending_irqs_no_gisa(vcpu) |
249
- kvm_s390_gisa_get_ipm(vcpu->kvm->arch.gisa) << IRQ_PEND_IO_ISC_7;
336
+ struct kvm_s390_gisa_interrupt *gi = &vcpu->kvm->arch.gisa_int;
337
+ unsigned long pending_mask;
338
+
339
+ pending_mask = pending_irqs_no_gisa(vcpu);
340
+ if (gi->origin)
341
+ pending_mask |= gisa_get_ipm(gi->origin) << IRQ_PEND_IO_ISC_7;
342
+ return pending_mask;
250343 }
251344
252345 static inline int isc_to_irq_type(unsigned long isc)
....@@ -293,10 +386,18 @@
293386 __clear_bit(IRQ_PEND_EXT_CLOCK_COMP, &active_mask);
294387 if (!(vcpu->arch.sie_block->gcr[0] & CR0_CPU_TIMER_SUBMASK))
295388 __clear_bit(IRQ_PEND_EXT_CPU_TIMER, &active_mask);
296
- if (!(vcpu->arch.sie_block->gcr[0] & CR0_SERVICE_SIGNAL_SUBMASK))
389
+ if (!(vcpu->arch.sie_block->gcr[0] & CR0_SERVICE_SIGNAL_SUBMASK)) {
297390 __clear_bit(IRQ_PEND_EXT_SERVICE, &active_mask);
391
+ __clear_bit(IRQ_PEND_EXT_SERVICE_EV, &active_mask);
392
+ }
298393 if (psw_mchk_disabled(vcpu))
299394 active_mask &= ~IRQ_PEND_MCHK_MASK;
395
+ /* PV guest cpus can have a single interruption injected at a time. */
396
+ if (kvm_s390_pv_cpu_get_handle(vcpu) &&
397
+ vcpu->arch.sie_block->iictl != IICTL_CODE_NONE)
398
+ active_mask &= ~(IRQ_PEND_EXT_II_MASK |
399
+ IRQ_PEND_IO_MASK |
400
+ IRQ_PEND_MCHK_MASK);
300401 /*
301402 * Check both floating and local interrupt's cr14 because
302403 * bit IRQ_PEND_MCHK_REP could be set in both cases.
....@@ -318,13 +419,13 @@
318419 static void __set_cpu_idle(struct kvm_vcpu *vcpu)
319420 {
320421 kvm_s390_set_cpuflags(vcpu, CPUSTAT_WAIT);
321
- set_bit(kvm_vcpu_get_idx(vcpu), vcpu->kvm->arch.float_int.idle_mask);
422
+ set_bit(kvm_vcpu_get_idx(vcpu), vcpu->kvm->arch.idle_mask);
322423 }
323424
324425 static void __unset_cpu_idle(struct kvm_vcpu *vcpu)
325426 {
326427 kvm_s390_clear_cpuflags(vcpu, CPUSTAT_WAIT);
327
- clear_bit(kvm_vcpu_get_idx(vcpu), vcpu->kvm->arch.float_int.idle_mask);
428
+ clear_bit(kvm_vcpu_get_idx(vcpu), vcpu->kvm->arch.idle_mask);
328429 }
329430
330431 static void __reset_intercept_indicators(struct kvm_vcpu *vcpu)
....@@ -345,7 +446,7 @@
345446 {
346447 if (!(pending_irqs_no_gisa(vcpu) & IRQ_PEND_IO_MASK))
347448 return;
348
- else if (psw_ioint_disabled(vcpu))
449
+ if (psw_ioint_disabled(vcpu))
349450 kvm_s390_set_cpuflags(vcpu, CPUSTAT_IO_INT);
350451 else
351452 vcpu->arch.sie_block->lctl |= LCTL_CR6;
....@@ -353,7 +454,7 @@
353454
354455 static void set_intercept_indicators_ext(struct kvm_vcpu *vcpu)
355456 {
356
- if (!(pending_irqs(vcpu) & IRQ_PEND_EXT_MASK))
457
+ if (!(pending_irqs_no_gisa(vcpu) & IRQ_PEND_EXT_MASK))
357458 return;
358459 if (psw_extint_disabled(vcpu))
359460 kvm_s390_set_cpuflags(vcpu, CPUSTAT_EXT_INT);
....@@ -363,7 +464,7 @@
363464
364465 static void set_intercept_indicators_mchk(struct kvm_vcpu *vcpu)
365466 {
366
- if (!(pending_irqs(vcpu) & IRQ_PEND_MCHK_MASK))
467
+ if (!(pending_irqs_no_gisa(vcpu) & IRQ_PEND_MCHK_MASK))
367468 return;
368469 if (psw_mchk_disabled(vcpu))
369470 vcpu->arch.sie_block->ictl |= ICTL_LPSW;
....@@ -389,19 +490,23 @@
389490 static int __must_check __deliver_cpu_timer(struct kvm_vcpu *vcpu)
390491 {
391492 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
392
- int rc;
493
+ int rc = 0;
393494
394495 vcpu->stat.deliver_cputm++;
395496 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_INT_CPU_TIMER,
396497 0, 0);
397
-
398
- rc = put_guest_lc(vcpu, EXT_IRQ_CPU_TIMER,
399
- (u16 *)__LC_EXT_INT_CODE);
400
- rc |= put_guest_lc(vcpu, 0, (u16 *)__LC_EXT_CPU_ADDR);
401
- rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
402
- &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
403
- rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
404
- &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
498
+ if (kvm_s390_pv_cpu_is_protected(vcpu)) {
499
+ vcpu->arch.sie_block->iictl = IICTL_CODE_EXT;
500
+ vcpu->arch.sie_block->eic = EXT_IRQ_CPU_TIMER;
501
+ } else {
502
+ rc = put_guest_lc(vcpu, EXT_IRQ_CPU_TIMER,
503
+ (u16 *)__LC_EXT_INT_CODE);
504
+ rc |= put_guest_lc(vcpu, 0, (u16 *)__LC_EXT_CPU_ADDR);
505
+ rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
506
+ &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
507
+ rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
508
+ &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
509
+ }
405510 clear_bit(IRQ_PEND_EXT_CPU_TIMER, &li->pending_irqs);
406511 return rc ? -EFAULT : 0;
407512 }
....@@ -409,19 +514,23 @@
409514 static int __must_check __deliver_ckc(struct kvm_vcpu *vcpu)
410515 {
411516 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
412
- int rc;
517
+ int rc = 0;
413518
414519 vcpu->stat.deliver_ckc++;
415520 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_INT_CLOCK_COMP,
416521 0, 0);
417
-
418
- rc = put_guest_lc(vcpu, EXT_IRQ_CLK_COMP,
419
- (u16 __user *)__LC_EXT_INT_CODE);
420
- rc |= put_guest_lc(vcpu, 0, (u16 *)__LC_EXT_CPU_ADDR);
421
- rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
422
- &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
423
- rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
424
- &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
522
+ if (kvm_s390_pv_cpu_is_protected(vcpu)) {
523
+ vcpu->arch.sie_block->iictl = IICTL_CODE_EXT;
524
+ vcpu->arch.sie_block->eic = EXT_IRQ_CLK_COMP;
525
+ } else {
526
+ rc = put_guest_lc(vcpu, EXT_IRQ_CLK_COMP,
527
+ (u16 __user *)__LC_EXT_INT_CODE);
528
+ rc |= put_guest_lc(vcpu, 0, (u16 *)__LC_EXT_CPU_ADDR);
529
+ rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
530
+ &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
531
+ rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
532
+ &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
533
+ }
425534 clear_bit(IRQ_PEND_EXT_CLOCK_COMP, &li->pending_irqs);
426535 return rc ? -EFAULT : 0;
427536 }
....@@ -462,6 +571,20 @@
462571 freg_t fprs[NUM_FPRS];
463572 union mci mci;
464573 int rc;
574
+
575
+ /*
576
+ * All other possible payload for a machine check (e.g. the register
577
+ * contents in the save area) will be handled by the ultravisor, as
578
+ * the hypervisor does not not have the needed information for
579
+ * protected guests.
580
+ */
581
+ if (kvm_s390_pv_cpu_is_protected(vcpu)) {
582
+ vcpu->arch.sie_block->iictl = IICTL_CODE_MCHK;
583
+ vcpu->arch.sie_block->mcic = mchk->mcic;
584
+ vcpu->arch.sie_block->faddr = mchk->failing_storage_address;
585
+ vcpu->arch.sie_block->edc = mchk->ext_damage_code;
586
+ return 0;
587
+ }
465588
466589 mci.val = mchk->mcic;
467590 /* take care of lazy register loading */
....@@ -606,17 +729,21 @@
606729 static int __must_check __deliver_restart(struct kvm_vcpu *vcpu)
607730 {
608731 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
609
- int rc;
732
+ int rc = 0;
610733
611734 VCPU_EVENT(vcpu, 3, "%s", "deliver: cpu restart");
612735 vcpu->stat.deliver_restart_signal++;
613736 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_RESTART, 0, 0);
614737
615
- rc = write_guest_lc(vcpu,
616
- offsetof(struct lowcore, restart_old_psw),
617
- &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
618
- rc |= read_guest_lc(vcpu, offsetof(struct lowcore, restart_psw),
619
- &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
738
+ if (kvm_s390_pv_cpu_is_protected(vcpu)) {
739
+ vcpu->arch.sie_block->iictl = IICTL_CODE_RESTART;
740
+ } else {
741
+ rc = write_guest_lc(vcpu,
742
+ offsetof(struct lowcore, restart_old_psw),
743
+ &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
744
+ rc |= read_guest_lc(vcpu, offsetof(struct lowcore, restart_psw),
745
+ &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
746
+ }
620747 clear_bit(IRQ_PEND_RESTART, &li->pending_irqs);
621748 return rc ? -EFAULT : 0;
622749 }
....@@ -658,6 +785,12 @@
658785 vcpu->stat.deliver_emergency_signal++;
659786 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_INT_EMERGENCY,
660787 cpu_addr, 0);
788
+ if (kvm_s390_pv_cpu_is_protected(vcpu)) {
789
+ vcpu->arch.sie_block->iictl = IICTL_CODE_EXT;
790
+ vcpu->arch.sie_block->eic = EXT_IRQ_EMERGENCY_SIG;
791
+ vcpu->arch.sie_block->extcpuaddr = cpu_addr;
792
+ return 0;
793
+ }
661794
662795 rc = put_guest_lc(vcpu, EXT_IRQ_EMERGENCY_SIG,
663796 (u16 *)__LC_EXT_INT_CODE);
....@@ -686,6 +819,12 @@
686819 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id,
687820 KVM_S390_INT_EXTERNAL_CALL,
688821 extcall.code, 0);
822
+ if (kvm_s390_pv_cpu_is_protected(vcpu)) {
823
+ vcpu->arch.sie_block->iictl = IICTL_CODE_EXT;
824
+ vcpu->arch.sie_block->eic = EXT_IRQ_EXTERNAL_CALL;
825
+ vcpu->arch.sie_block->extcpuaddr = extcall.code;
826
+ return 0;
827
+ }
689828
690829 rc = put_guest_lc(vcpu, EXT_IRQ_EXTERNAL_CALL,
691830 (u16 *)__LC_EXT_INT_CODE);
....@@ -695,6 +834,21 @@
695834 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW, &vcpu->arch.sie_block->gpsw,
696835 sizeof(psw_t));
697836 return rc ? -EFAULT : 0;
837
+}
838
+
839
+static int __deliver_prog_pv(struct kvm_vcpu *vcpu, u16 code)
840
+{
841
+ switch (code) {
842
+ case PGM_SPECIFICATION:
843
+ vcpu->arch.sie_block->iictl = IICTL_CODE_SPECIFICATION;
844
+ break;
845
+ case PGM_OPERAND:
846
+ vcpu->arch.sie_block->iictl = IICTL_CODE_OPERAND;
847
+ break;
848
+ default:
849
+ return -EINVAL;
850
+ }
851
+ return 0;
698852 }
699853
700854 static int __must_check __deliver_prog(struct kvm_vcpu *vcpu)
....@@ -717,6 +871,10 @@
717871 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_PROGRAM_INT,
718872 pgm_info.code, 0);
719873
874
+ /* PER is handled by the ultravisor */
875
+ if (kvm_s390_pv_cpu_is_protected(vcpu))
876
+ return __deliver_prog_pv(vcpu, pgm_info.code & ~PGM_PER);
877
+
720878 switch (pgm_info.code & ~PGM_PER) {
721879 case PGM_AFX_TRANSLATION:
722880 case PGM_ASX_TRANSLATION:
....@@ -728,7 +886,7 @@
728886 case PGM_PRIMARY_AUTHORITY:
729887 case PGM_SECONDARY_AUTHORITY:
730888 nullifying = true;
731
- /* fall through */
889
+ fallthrough;
732890 case PGM_SPACE_SWITCH:
733891 rc = put_guest_lc(vcpu, pgm_info.trans_exc_code,
734892 (u64 *)__LC_TRANS_EXC_CODE);
....@@ -812,20 +970,49 @@
812970 return rc ? -EFAULT : 0;
813971 }
814972
973
+#define SCCB_MASK 0xFFFFFFF8
974
+#define SCCB_EVENT_PENDING 0x3
975
+
976
+static int write_sclp(struct kvm_vcpu *vcpu, u32 parm)
977
+{
978
+ int rc;
979
+
980
+ if (kvm_s390_pv_cpu_get_handle(vcpu)) {
981
+ vcpu->arch.sie_block->iictl = IICTL_CODE_EXT;
982
+ vcpu->arch.sie_block->eic = EXT_IRQ_SERVICE_SIG;
983
+ vcpu->arch.sie_block->eiparams = parm;
984
+ return 0;
985
+ }
986
+
987
+ rc = put_guest_lc(vcpu, EXT_IRQ_SERVICE_SIG, (u16 *)__LC_EXT_INT_CODE);
988
+ rc |= put_guest_lc(vcpu, 0, (u16 *)__LC_EXT_CPU_ADDR);
989
+ rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
990
+ &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
991
+ rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
992
+ &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
993
+ rc |= put_guest_lc(vcpu, parm,
994
+ (u32 *)__LC_EXT_PARAMS);
995
+
996
+ return rc ? -EFAULT : 0;
997
+}
998
+
815999 static int __must_check __deliver_service(struct kvm_vcpu *vcpu)
8161000 {
8171001 struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
8181002 struct kvm_s390_ext_info ext;
819
- int rc = 0;
8201003
8211004 spin_lock(&fi->lock);
822
- if (!(test_bit(IRQ_PEND_EXT_SERVICE, &fi->pending_irqs))) {
1005
+ if (test_bit(IRQ_PEND_EXT_SERVICE, &fi->masked_irqs) ||
1006
+ !(test_bit(IRQ_PEND_EXT_SERVICE, &fi->pending_irqs))) {
8231007 spin_unlock(&fi->lock);
8241008 return 0;
8251009 }
8261010 ext = fi->srv_signal;
8271011 memset(&fi->srv_signal, 0, sizeof(ext));
8281012 clear_bit(IRQ_PEND_EXT_SERVICE, &fi->pending_irqs);
1013
+ clear_bit(IRQ_PEND_EXT_SERVICE_EV, &fi->pending_irqs);
1014
+ if (kvm_s390_pv_cpu_is_protected(vcpu))
1015
+ set_bit(IRQ_PEND_EXT_SERVICE, &fi->masked_irqs);
8291016 spin_unlock(&fi->lock);
8301017
8311018 VCPU_EVENT(vcpu, 4, "deliver: sclp parameter 0x%x",
....@@ -834,16 +1021,31 @@
8341021 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_INT_SERVICE,
8351022 ext.ext_params, 0);
8361023
837
- rc = put_guest_lc(vcpu, EXT_IRQ_SERVICE_SIG, (u16 *)__LC_EXT_INT_CODE);
838
- rc |= put_guest_lc(vcpu, 0, (u16 *)__LC_EXT_CPU_ADDR);
839
- rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
840
- &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
841
- rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
842
- &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
843
- rc |= put_guest_lc(vcpu, ext.ext_params,
844
- (u32 *)__LC_EXT_PARAMS);
1024
+ return write_sclp(vcpu, ext.ext_params);
1025
+}
8451026
846
- return rc ? -EFAULT : 0;
1027
+static int __must_check __deliver_service_ev(struct kvm_vcpu *vcpu)
1028
+{
1029
+ struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
1030
+ struct kvm_s390_ext_info ext;
1031
+
1032
+ spin_lock(&fi->lock);
1033
+ if (!(test_bit(IRQ_PEND_EXT_SERVICE_EV, &fi->pending_irqs))) {
1034
+ spin_unlock(&fi->lock);
1035
+ return 0;
1036
+ }
1037
+ ext = fi->srv_signal;
1038
+ /* only clear the event bit */
1039
+ fi->srv_signal.ext_params &= ~SCCB_EVENT_PENDING;
1040
+ clear_bit(IRQ_PEND_EXT_SERVICE_EV, &fi->pending_irqs);
1041
+ spin_unlock(&fi->lock);
1042
+
1043
+ VCPU_EVENT(vcpu, 4, "%s", "deliver: sclp parameter event");
1044
+ vcpu->stat.deliver_service_signal++;
1045
+ trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_INT_SERVICE,
1046
+ ext.ext_params, 0);
1047
+
1048
+ return write_sclp(vcpu, SCCB_EVENT_PENDING);
8471049 }
8481050
8491051 static int __must_check __deliver_pfault_done(struct kvm_vcpu *vcpu)
....@@ -938,6 +1140,15 @@
9381140 {
9391141 int rc;
9401142
1143
+ if (kvm_s390_pv_cpu_is_protected(vcpu)) {
1144
+ vcpu->arch.sie_block->iictl = IICTL_CODE_IO;
1145
+ vcpu->arch.sie_block->subchannel_id = io->subchannel_id;
1146
+ vcpu->arch.sie_block->subchannel_nr = io->subchannel_nr;
1147
+ vcpu->arch.sie_block->io_int_parm = io->io_int_parm;
1148
+ vcpu->arch.sie_block->io_int_word = io->io_int_word;
1149
+ return 0;
1150
+ }
1151
+
9411152 rc = put_guest_lc(vcpu, io->subchannel_id, (u16 *)__LC_SUBCHANNEL_ID);
9421153 rc |= put_guest_lc(vcpu, io->subchannel_nr, (u16 *)__LC_SUBCHANNEL_NR);
9431154 rc |= put_guest_lc(vcpu, io->io_int_parm, (u32 *)__LC_IO_INT_PARM);
....@@ -956,6 +1167,7 @@
9561167 {
9571168 struct list_head *isc_list;
9581169 struct kvm_s390_float_interrupt *fi;
1170
+ struct kvm_s390_gisa_interrupt *gi = &vcpu->kvm->arch.gisa_int;
9591171 struct kvm_s390_interrupt_info *inti = NULL;
9601172 struct kvm_s390_io_info io;
9611173 u32 isc;
....@@ -998,8 +1210,7 @@
9981210 goto out;
9991211 }
10001212
1001
- if (vcpu->kvm->arch.gisa &&
1002
- kvm_s390_gisa_tac_ipm_gisc(vcpu->kvm->arch.gisa, isc)) {
1213
+ if (gi->origin && gisa_tac_ipm_gisc(gi->origin, isc)) {
10031214 /*
10041215 * in case an adapter interrupt was not delivered
10051216 * in SIE context KVM will handle the delivery
....@@ -1089,6 +1300,7 @@
10891300
10901301 int kvm_s390_handle_wait(struct kvm_vcpu *vcpu)
10911302 {
1303
+ struct kvm_s390_gisa_interrupt *gi = &vcpu->kvm->arch.gisa_int;
10921304 u64 sltime;
10931305
10941306 vcpu->stat.exit_wait_state++;
....@@ -1101,6 +1313,11 @@
11011313 VCPU_EVENT(vcpu, 3, "%s", "disabled wait");
11021314 return -EOPNOTSUPP; /* disabled wait */
11031315 }
1316
+
1317
+ if (gi->origin &&
1318
+ (gisa_get_ipm_or_restore_iam(gi) &
1319
+ vcpu->arch.sie_block->gcr[6] >> 24))
1320
+ return 0;
11041321
11051322 if (!ckc_interrupts_enabled(vcpu) &&
11061323 !cpu_timer_interrupts_enabled(vcpu)) {
....@@ -1128,28 +1345,11 @@
11281345
11291346 void kvm_s390_vcpu_wakeup(struct kvm_vcpu *vcpu)
11301347 {
1131
- /*
1132
- * We cannot move this into the if, as the CPU might be already
1133
- * in kvm_vcpu_block without having the waitqueue set (polling)
1134
- */
11351348 vcpu->valid_wakeup = true;
1349
+ kvm_vcpu_wake_up(vcpu);
1350
+
11361351 /*
1137
- * This is mostly to document, that the read in swait_active could
1138
- * be moved before other stores, leading to subtle races.
1139
- * All current users do not store or use an atomic like update
1140
- */
1141
- smp_mb__after_atomic();
1142
- if (swait_active(&vcpu->wq)) {
1143
- /*
1144
- * The vcpu gave up the cpu voluntarily, mark it as a good
1145
- * yield-candidate.
1146
- */
1147
- vcpu->preempted = true;
1148
- swake_up_one(&vcpu->wq);
1149
- vcpu->stat.halt_wakeup++;
1150
- }
1151
- /*
1152
- * The VCPU might not be sleeping but is executing the VSIE. Let's
1352
+ * The VCPU might not be sleeping but rather executing VSIE. Let's
11531353 * kick it, so it leaves the SIE to process the request.
11541354 */
11551355 kvm_s390_vsie_kick(vcpu);
....@@ -1250,6 +1450,9 @@
12501450 case IRQ_PEND_EXT_SERVICE:
12511451 rc = __deliver_service(vcpu);
12521452 break;
1453
+ case IRQ_PEND_EXT_SERVICE_EV:
1454
+ rc = __deliver_service_ev(vcpu);
1455
+ break;
12531456 case IRQ_PEND_PFAULT_DONE:
12541457 rc = __deliver_pfault_done(vcpu);
12551458 break;
....@@ -1342,7 +1545,7 @@
13421545 if (kvm_get_vcpu_by_id(vcpu->kvm, src_id) == NULL)
13431546 return -EINVAL;
13441547
1345
- if (sclp.has_sigpif)
1548
+ if (sclp.has_sigpif && !kvm_s390_pv_cpu_get_handle(vcpu))
13461549 return sca_inject_ext_call(vcpu, src_id);
13471550
13481551 if (test_and_set_bit(IRQ_PEND_EXT_EXTERNAL, &li->pending_irqs))
....@@ -1398,8 +1601,7 @@
13981601 return 0;
13991602 }
14001603
1401
-static int __inject_sigp_restart(struct kvm_vcpu *vcpu,
1402
- struct kvm_s390_irq *irq)
1604
+static int __inject_sigp_restart(struct kvm_vcpu *vcpu)
14031605 {
14041606 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
14051607
....@@ -1533,18 +1735,19 @@
15331735
15341736 static int get_top_gisa_isc(struct kvm *kvm, u64 isc_mask, u32 schid)
15351737 {
1738
+ struct kvm_s390_gisa_interrupt *gi = &kvm->arch.gisa_int;
15361739 unsigned long active_mask;
15371740 int isc;
15381741
15391742 if (schid)
15401743 goto out;
1541
- if (!kvm->arch.gisa)
1744
+ if (!gi->origin)
15421745 goto out;
15431746
1544
- active_mask = (isc_mask & kvm_s390_gisa_get_ipm(kvm->arch.gisa) << 24) << 32;
1747
+ active_mask = (isc_mask & gisa_get_ipm(gi->origin) << 24) << 32;
15451748 while (active_mask) {
15461749 isc = __fls(active_mask) ^ (BITS_PER_LONG - 1);
1547
- if (kvm_s390_gisa_tac_ipm_gisc(kvm->arch.gisa, isc))
1750
+ if (gisa_tac_ipm_gisc(gi->origin, isc))
15481751 return isc;
15491752 clear_bit_inv(isc, &active_mask);
15501753 }
....@@ -1567,6 +1770,7 @@
15671770 struct kvm_s390_interrupt_info *kvm_s390_get_io_int(struct kvm *kvm,
15681771 u64 isc_mask, u32 schid)
15691772 {
1773
+ struct kvm_s390_gisa_interrupt *gi = &kvm->arch.gisa_int;
15701774 struct kvm_s390_interrupt_info *inti, *tmp_inti;
15711775 int isc;
15721776
....@@ -1584,7 +1788,7 @@
15841788 /* both types of interrupts present */
15851789 if (int_word_to_isc(inti->io.io_int_word) <= isc) {
15861790 /* classical IO int with higher priority */
1587
- kvm_s390_gisa_set_ipm_gisc(kvm->arch.gisa, isc);
1791
+ gisa_set_ipm_gisc(gi->origin, isc);
15881792 goto out;
15891793 }
15901794 gisa_out:
....@@ -1596,13 +1800,10 @@
15961800 kvm_s390_reinject_io_int(kvm, inti);
15971801 inti = tmp_inti;
15981802 } else
1599
- kvm_s390_gisa_set_ipm_gisc(kvm->arch.gisa, isc);
1803
+ gisa_set_ipm_gisc(gi->origin, isc);
16001804 out:
16011805 return inti;
16021806 }
1603
-
1604
-#define SCCB_MASK 0xFFFFFFF8
1605
-#define SCCB_EVENT_PENDING 0x3
16061807
16071808 static int __inject_service(struct kvm *kvm,
16081809 struct kvm_s390_interrupt_info *inti)
....@@ -1612,6 +1813,11 @@
16121813 kvm->stat.inject_service_signal++;
16131814 spin_lock(&fi->lock);
16141815 fi->srv_signal.ext_params |= inti->ext.ext_params & SCCB_EVENT_PENDING;
1816
+
1817
+ /* We always allow events, track them separately from the sccb ints */
1818
+ if (fi->srv_signal.ext_params & SCCB_EVENT_PENDING)
1819
+ set_bit(IRQ_PEND_EXT_SERVICE_EV, &fi->pending_irqs);
1820
+
16151821 /*
16161822 * Early versions of the QEMU s390 bios will inject several
16171823 * service interrupts after another without handling a
....@@ -1685,6 +1891,7 @@
16851891
16861892 static int __inject_io(struct kvm *kvm, struct kvm_s390_interrupt_info *inti)
16871893 {
1894
+ struct kvm_s390_gisa_interrupt *gi = &kvm->arch.gisa_int;
16881895 struct kvm_s390_float_interrupt *fi;
16891896 struct list_head *list;
16901897 int isc;
....@@ -1692,9 +1899,16 @@
16921899 kvm->stat.inject_io++;
16931900 isc = int_word_to_isc(inti->io.io_int_word);
16941901
1695
- if (kvm->arch.gisa && inti->type & KVM_S390_INT_IO_AI_MASK) {
1902
+ /*
1903
+ * Do not make use of gisa in protected mode. We do not use the lock
1904
+ * checking variant as this is just a performance optimization and we
1905
+ * do not hold the lock here. This is ok as the code will pick
1906
+ * interrupts from both "lists" for delivery.
1907
+ */
1908
+ if (!kvm_s390_pv_get_handle(kvm) &&
1909
+ gi->origin && inti->type & KVM_S390_INT_IO_AI_MASK) {
16961910 VM_EVENT(kvm, 4, "%s isc %1u", "inject: I/O (AI/gisa)", isc);
1697
- kvm_s390_gisa_set_ipm_gisc(kvm->arch.gisa, isc);
1911
+ gisa_set_ipm_gisc(gi->origin, isc);
16981912 kfree(inti);
16991913 return 0;
17001914 }
....@@ -1726,7 +1940,6 @@
17261940 */
17271941 static void __floating_irq_kick(struct kvm *kvm, u64 type)
17281942 {
1729
- struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
17301943 struct kvm_vcpu *dst_vcpu;
17311944 int sigcpu, online_vcpus, nr_tries = 0;
17321945
....@@ -1735,11 +1948,11 @@
17351948 return;
17361949
17371950 /* find idle VCPUs first, then round robin */
1738
- sigcpu = find_first_bit(fi->idle_mask, online_vcpus);
1951
+ sigcpu = find_first_bit(kvm->arch.idle_mask, online_vcpus);
17391952 if (sigcpu == online_vcpus) {
17401953 do {
1741
- sigcpu = fi->next_rr_cpu;
1742
- fi->next_rr_cpu = (fi->next_rr_cpu + 1) % online_vcpus;
1954
+ sigcpu = kvm->arch.float_int.next_rr_cpu++;
1955
+ kvm->arch.float_int.next_rr_cpu %= online_vcpus;
17431956 /* avoid endless loops if all vcpus are stopped */
17441957 if (nr_tries++ >= online_vcpus)
17451958 return;
....@@ -1753,7 +1966,9 @@
17531966 kvm_s390_set_cpuflags(dst_vcpu, CPUSTAT_STOP_INT);
17541967 break;
17551968 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
1756
- if (!(type & KVM_S390_INT_IO_AI_MASK && kvm->arch.gisa))
1969
+ if (!(type & KVM_S390_INT_IO_AI_MASK &&
1970
+ kvm->arch.gisa_int.origin) ||
1971
+ kvm_s390_pv_cpu_get_handle(dst_vcpu))
17571972 kvm_s390_set_cpuflags(dst_vcpu, CPUSTAT_IO_INT);
17581973 break;
17591974 default:
....@@ -1932,7 +2147,7 @@
19322147 rc = __inject_sigp_stop(vcpu, irq);
19332148 break;
19342149 case KVM_S390_RESTART:
1935
- rc = __inject_sigp_restart(vcpu, irq);
2150
+ rc = __inject_sigp_restart(vcpu);
19362151 break;
19372152 case KVM_S390_INT_CLOCK_COMP:
19382153 rc = __inject_ckc(vcpu);
....@@ -2006,6 +2221,10 @@
20062221 struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
20072222 int i;
20082223
2224
+ mutex_lock(&kvm->lock);
2225
+ if (!kvm_s390_pv_is_protected(kvm))
2226
+ fi->masked_irqs = 0;
2227
+ mutex_unlock(&kvm->lock);
20092228 spin_lock(&fi->lock);
20102229 fi->pending_irqs = 0;
20112230 memset(&fi->srv_signal, 0, sizeof(fi->srv_signal));
....@@ -2020,6 +2239,7 @@
20202239
20212240 static int get_all_floating_irqs(struct kvm *kvm, u8 __user *usrbuf, u64 len)
20222241 {
2242
+ struct kvm_s390_gisa_interrupt *gi = &kvm->arch.gisa_int;
20232243 struct kvm_s390_interrupt_info *inti;
20242244 struct kvm_s390_float_interrupt *fi;
20252245 struct kvm_s390_irq *buf;
....@@ -2043,15 +2263,14 @@
20432263
20442264 max_irqs = len / sizeof(struct kvm_s390_irq);
20452265
2046
- if (kvm->arch.gisa &&
2047
- kvm_s390_gisa_get_ipm(kvm->arch.gisa)) {
2266
+ if (gi->origin && gisa_get_ipm(gi->origin)) {
20482267 for (i = 0; i <= MAX_ISC; i++) {
20492268 if (n == max_irqs) {
20502269 /* signal userspace to try again */
20512270 ret = -ENOMEM;
20522271 goto out_nolock;
20532272 }
2054
- if (kvm_s390_gisa_tac_ipm_gisc(kvm->arch.gisa, i)) {
2273
+ if (gisa_tac_ipm_gisc(gi->origin, i)) {
20552274 irq = (struct kvm_s390_irq *) &buf[n];
20562275 irq->type = KVM_S390_INT_IO(1, 0, 0, 0);
20572276 irq->u.io.io_int_word = isc_to_int_word(i);
....@@ -2072,7 +2291,8 @@
20722291 n++;
20732292 }
20742293 }
2075
- if (test_bit(IRQ_PEND_EXT_SERVICE, &fi->pending_irqs)) {
2294
+ if (test_bit(IRQ_PEND_EXT_SERVICE, &fi->pending_irqs) ||
2295
+ test_bit(IRQ_PEND_EXT_SERVICE_EV, &fi->pending_irqs)) {
20762296 if (n == max_irqs) {
20772297 /* signal userspace to try again */
20782298 ret = -ENOMEM;
....@@ -2226,6 +2446,7 @@
22262446 {
22272447 if (id >= MAX_S390_IO_ADAPTERS)
22282448 return NULL;
2449
+ id = array_index_nospec(id, MAX_S390_IO_ADAPTERS);
22292450 return kvm->arch.adapters[id];
22302451 }
22312452
....@@ -2239,17 +2460,19 @@
22392460 (void __user *)attr->addr, sizeof(adapter_info)))
22402461 return -EFAULT;
22412462
2242
- if ((adapter_info.id >= MAX_S390_IO_ADAPTERS) ||
2243
- (dev->kvm->arch.adapters[adapter_info.id] != NULL))
2463
+ if (adapter_info.id >= MAX_S390_IO_ADAPTERS)
2464
+ return -EINVAL;
2465
+
2466
+ adapter_info.id = array_index_nospec(adapter_info.id,
2467
+ MAX_S390_IO_ADAPTERS);
2468
+
2469
+ if (dev->kvm->arch.adapters[adapter_info.id] != NULL)
22442470 return -EINVAL;
22452471
22462472 adapter = kzalloc(sizeof(*adapter), GFP_KERNEL);
22472473 if (!adapter)
22482474 return -ENOMEM;
22492475
2250
- INIT_LIST_HEAD(&adapter->maps);
2251
- init_rwsem(&adapter->maps_lock);
2252
- atomic_set(&adapter->nr_maps, 0);
22532476 adapter->id = adapter_info.id;
22542477 adapter->isc = adapter_info.isc;
22552478 adapter->maskable = adapter_info.maskable;
....@@ -2274,87 +2497,12 @@
22742497 return ret;
22752498 }
22762499
2277
-static int kvm_s390_adapter_map(struct kvm *kvm, unsigned int id, __u64 addr)
2278
-{
2279
- struct s390_io_adapter *adapter = get_io_adapter(kvm, id);
2280
- struct s390_map_info *map;
2281
- int ret;
2282
-
2283
- if (!adapter || !addr)
2284
- return -EINVAL;
2285
-
2286
- map = kzalloc(sizeof(*map), GFP_KERNEL);
2287
- if (!map) {
2288
- ret = -ENOMEM;
2289
- goto out;
2290
- }
2291
- INIT_LIST_HEAD(&map->list);
2292
- map->guest_addr = addr;
2293
- map->addr = gmap_translate(kvm->arch.gmap, addr);
2294
- if (map->addr == -EFAULT) {
2295
- ret = -EFAULT;
2296
- goto out;
2297
- }
2298
- ret = get_user_pages_fast(map->addr, 1, 1, &map->page);
2299
- if (ret < 0)
2300
- goto out;
2301
- BUG_ON(ret != 1);
2302
- down_write(&adapter->maps_lock);
2303
- if (atomic_inc_return(&adapter->nr_maps) < MAX_S390_ADAPTER_MAPS) {
2304
- list_add_tail(&map->list, &adapter->maps);
2305
- ret = 0;
2306
- } else {
2307
- put_page(map->page);
2308
- ret = -EINVAL;
2309
- }
2310
- up_write(&adapter->maps_lock);
2311
-out:
2312
- if (ret)
2313
- kfree(map);
2314
- return ret;
2315
-}
2316
-
2317
-static int kvm_s390_adapter_unmap(struct kvm *kvm, unsigned int id, __u64 addr)
2318
-{
2319
- struct s390_io_adapter *adapter = get_io_adapter(kvm, id);
2320
- struct s390_map_info *map, *tmp;
2321
- int found = 0;
2322
-
2323
- if (!adapter || !addr)
2324
- return -EINVAL;
2325
-
2326
- down_write(&adapter->maps_lock);
2327
- list_for_each_entry_safe(map, tmp, &adapter->maps, list) {
2328
- if (map->guest_addr == addr) {
2329
- found = 1;
2330
- atomic_dec(&adapter->nr_maps);
2331
- list_del(&map->list);
2332
- put_page(map->page);
2333
- kfree(map);
2334
- break;
2335
- }
2336
- }
2337
- up_write(&adapter->maps_lock);
2338
-
2339
- return found ? 0 : -EINVAL;
2340
-}
2341
-
23422500 void kvm_s390_destroy_adapters(struct kvm *kvm)
23432501 {
23442502 int i;
2345
- struct s390_map_info *map, *tmp;
23462503
2347
- for (i = 0; i < MAX_S390_IO_ADAPTERS; i++) {
2348
- if (!kvm->arch.adapters[i])
2349
- continue;
2350
- list_for_each_entry_safe(map, tmp,
2351
- &kvm->arch.adapters[i]->maps, list) {
2352
- list_del(&map->list);
2353
- put_page(map->page);
2354
- kfree(map);
2355
- }
2504
+ for (i = 0; i < MAX_S390_IO_ADAPTERS; i++)
23562505 kfree(kvm->arch.adapters[i]);
2357
- }
23582506 }
23592507
23602508 static int modify_io_adapter(struct kvm_device *dev,
....@@ -2376,11 +2524,14 @@
23762524 if (ret > 0)
23772525 ret = 0;
23782526 break;
2527
+ /*
2528
+ * The following operations are no longer needed and therefore no-ops.
2529
+ * The gpa to hva translation is done when an IRQ route is set up. The
2530
+ * set_irq code uses get_user_pages_remote() to do the actual write.
2531
+ */
23792532 case KVM_S390_IO_ADAPTER_MAP:
2380
- ret = kvm_s390_adapter_map(dev->kvm, req.id, req.addr);
2381
- break;
23822533 case KVM_S390_IO_ADAPTER_UNMAP:
2383
- ret = kvm_s390_adapter_unmap(dev->kvm, req.id, req.addr);
2534
+ ret = 0;
23842535 break;
23852536 default:
23862537 ret = -EINVAL;
....@@ -2619,19 +2770,15 @@
26192770 return swap ? (bit ^ (BITS_PER_LONG - 1)) : bit;
26202771 }
26212772
2622
-static struct s390_map_info *get_map_info(struct s390_io_adapter *adapter,
2623
- u64 addr)
2773
+static struct page *get_map_page(struct kvm *kvm, u64 uaddr)
26242774 {
2625
- struct s390_map_info *map;
2775
+ struct page *page = NULL;
26262776
2627
- if (!adapter)
2628
- return NULL;
2629
-
2630
- list_for_each_entry(map, &adapter->maps, list) {
2631
- if (map->guest_addr == addr)
2632
- return map;
2633
- }
2634
- return NULL;
2777
+ mmap_read_lock(kvm->mm);
2778
+ get_user_pages_remote(kvm->mm, uaddr, 1, FOLL_WRITE,
2779
+ &page, NULL, NULL);
2780
+ mmap_read_unlock(kvm->mm);
2781
+ return page;
26352782 }
26362783
26372784 static int adapter_indicators_set(struct kvm *kvm,
....@@ -2640,30 +2787,35 @@
26402787 {
26412788 unsigned long bit;
26422789 int summary_set, idx;
2643
- struct s390_map_info *info;
2790
+ struct page *ind_page, *summary_page;
26442791 void *map;
26452792
2646
- info = get_map_info(adapter, adapter_int->ind_addr);
2647
- if (!info)
2793
+ ind_page = get_map_page(kvm, adapter_int->ind_addr);
2794
+ if (!ind_page)
26482795 return -1;
2649
- map = page_address(info->page);
2650
- bit = get_ind_bit(info->addr, adapter_int->ind_offset, adapter->swap);
2651
- set_bit(bit, map);
2652
- idx = srcu_read_lock(&kvm->srcu);
2653
- mark_page_dirty(kvm, info->guest_addr >> PAGE_SHIFT);
2654
- set_page_dirty_lock(info->page);
2655
- info = get_map_info(adapter, adapter_int->summary_addr);
2656
- if (!info) {
2657
- srcu_read_unlock(&kvm->srcu, idx);
2796
+ summary_page = get_map_page(kvm, adapter_int->summary_addr);
2797
+ if (!summary_page) {
2798
+ put_page(ind_page);
26582799 return -1;
26592800 }
2660
- map = page_address(info->page);
2661
- bit = get_ind_bit(info->addr, adapter_int->summary_offset,
2662
- adapter->swap);
2801
+
2802
+ idx = srcu_read_lock(&kvm->srcu);
2803
+ map = page_address(ind_page);
2804
+ bit = get_ind_bit(adapter_int->ind_addr,
2805
+ adapter_int->ind_offset, adapter->swap);
2806
+ set_bit(bit, map);
2807
+ mark_page_dirty(kvm, adapter_int->ind_addr >> PAGE_SHIFT);
2808
+ set_page_dirty_lock(ind_page);
2809
+ map = page_address(summary_page);
2810
+ bit = get_ind_bit(adapter_int->summary_addr,
2811
+ adapter_int->summary_offset, adapter->swap);
26632812 summary_set = test_and_set_bit(bit, map);
2664
- mark_page_dirty(kvm, info->guest_addr >> PAGE_SHIFT);
2665
- set_page_dirty_lock(info->page);
2813
+ mark_page_dirty(kvm, adapter_int->summary_addr >> PAGE_SHIFT);
2814
+ set_page_dirty_lock(summary_page);
26662815 srcu_read_unlock(&kvm->srcu, idx);
2816
+
2817
+ put_page(ind_page);
2818
+ put_page(summary_page);
26672819 return summary_set ? 0 : 1;
26682820 }
26692821
....@@ -2685,9 +2837,7 @@
26852837 adapter = get_io_adapter(kvm, e->adapter.adapter_id);
26862838 if (!adapter)
26872839 return -1;
2688
- down_read(&adapter->maps_lock);
26892840 ret = adapter_indicators_set(kvm, adapter, &e->adapter);
2690
- up_read(&adapter->maps_lock);
26912841 if ((ret > 0) && !adapter->masked) {
26922842 ret = kvm_s390_inject_airq(kvm, adapter);
26932843 if (ret == 0)
....@@ -2738,23 +2888,27 @@
27382888 struct kvm_kernel_irq_routing_entry *e,
27392889 const struct kvm_irq_routing_entry *ue)
27402890 {
2741
- int ret;
2891
+ u64 uaddr;
27422892
27432893 switch (ue->type) {
2894
+ /* we store the userspace addresses instead of the guest addresses */
27442895 case KVM_IRQ_ROUTING_S390_ADAPTER:
27452896 e->set = set_adapter_int;
2746
- e->adapter.summary_addr = ue->u.adapter.summary_addr;
2747
- e->adapter.ind_addr = ue->u.adapter.ind_addr;
2897
+ uaddr = gmap_translate(kvm->arch.gmap, ue->u.adapter.summary_addr);
2898
+ if (uaddr == -EFAULT)
2899
+ return -EFAULT;
2900
+ e->adapter.summary_addr = uaddr;
2901
+ uaddr = gmap_translate(kvm->arch.gmap, ue->u.adapter.ind_addr);
2902
+ if (uaddr == -EFAULT)
2903
+ return -EFAULT;
2904
+ e->adapter.ind_addr = uaddr;
27482905 e->adapter.summary_offset = ue->u.adapter.summary_offset;
27492906 e->adapter.ind_offset = ue->u.adapter.ind_offset;
27502907 e->adapter.adapter_id = ue->u.adapter.adapter_id;
2751
- ret = 0;
2752
- break;
2908
+ return 0;
27532909 default:
2754
- ret = -EINVAL;
2910
+ return -EINVAL;
27552911 }
2756
-
2757
- return ret;
27582912 }
27592913
27602914 int kvm_set_msi(struct kvm_kernel_irq_routing_entry *e, struct kvm *kvm,
....@@ -2848,7 +3002,7 @@
28483002 int kvm_s390_get_irq_state(struct kvm_vcpu *vcpu, __u8 __user *buf, int len)
28493003 {
28503004 int scn;
2851
- unsigned long sigp_emerg_pending[BITS_TO_LONGS(KVM_MAX_VCPUS)];
3005
+ DECLARE_BITMAP(sigp_emerg_pending, KVM_MAX_VCPUS);
28523006 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
28533007 unsigned long pending_irqs;
28543008 struct kvm_s390_irq irq;
....@@ -2901,27 +3055,279 @@
29013055 return n;
29023056 }
29033057
3058
+static void __airqs_kick_single_vcpu(struct kvm *kvm, u8 deliverable_mask)
3059
+{
3060
+ int vcpu_idx, online_vcpus = atomic_read(&kvm->online_vcpus);
3061
+ struct kvm_s390_gisa_interrupt *gi = &kvm->arch.gisa_int;
3062
+ struct kvm_vcpu *vcpu;
3063
+ u8 vcpu_isc_mask;
3064
+
3065
+ for_each_set_bit(vcpu_idx, kvm->arch.idle_mask, online_vcpus) {
3066
+ vcpu = kvm_get_vcpu(kvm, vcpu_idx);
3067
+ if (psw_ioint_disabled(vcpu))
3068
+ continue;
3069
+ vcpu_isc_mask = (u8)(vcpu->arch.sie_block->gcr[6] >> 24);
3070
+ if (deliverable_mask & vcpu_isc_mask) {
3071
+ /* lately kicked but not yet running */
3072
+ if (test_and_set_bit(vcpu_idx, gi->kicked_mask))
3073
+ return;
3074
+ kvm_s390_vcpu_wakeup(vcpu);
3075
+ return;
3076
+ }
3077
+ }
3078
+}
3079
+
3080
+static enum hrtimer_restart gisa_vcpu_kicker(struct hrtimer *timer)
3081
+{
3082
+ struct kvm_s390_gisa_interrupt *gi =
3083
+ container_of(timer, struct kvm_s390_gisa_interrupt, timer);
3084
+ struct kvm *kvm =
3085
+ container_of(gi->origin, struct sie_page2, gisa)->kvm;
3086
+ u8 pending_mask;
3087
+
3088
+ pending_mask = gisa_get_ipm_or_restore_iam(gi);
3089
+ if (pending_mask) {
3090
+ __airqs_kick_single_vcpu(kvm, pending_mask);
3091
+ hrtimer_forward_now(timer, ns_to_ktime(gi->expires));
3092
+ return HRTIMER_RESTART;
3093
+ }
3094
+
3095
+ return HRTIMER_NORESTART;
3096
+}
3097
+
3098
+#define NULL_GISA_ADDR 0x00000000UL
3099
+#define NONE_GISA_ADDR 0x00000001UL
3100
+#define GISA_ADDR_MASK 0xfffff000UL
3101
+
3102
+static void process_gib_alert_list(void)
3103
+{
3104
+ struct kvm_s390_gisa_interrupt *gi;
3105
+ struct kvm_s390_gisa *gisa;
3106
+ struct kvm *kvm;
3107
+ u32 final, origin = 0UL;
3108
+
3109
+ do {
3110
+ /*
3111
+ * If the NONE_GISA_ADDR is still stored in the alert list
3112
+ * origin, we will leave the outer loop. No further GISA has
3113
+ * been added to the alert list by millicode while processing
3114
+ * the current alert list.
3115
+ */
3116
+ final = (origin & NONE_GISA_ADDR);
3117
+ /*
3118
+ * Cut off the alert list and store the NONE_GISA_ADDR in the
3119
+ * alert list origin to avoid further GAL interruptions.
3120
+ * A new alert list can be build up by millicode in parallel
3121
+ * for guests not in the yet cut-off alert list. When in the
3122
+ * final loop, store the NULL_GISA_ADDR instead. This will re-
3123
+ * enable GAL interruptions on the host again.
3124
+ */
3125
+ origin = xchg(&gib->alert_list_origin,
3126
+ (!final) ? NONE_GISA_ADDR : NULL_GISA_ADDR);
3127
+ /*
3128
+ * Loop through the just cut-off alert list and start the
3129
+ * gisa timers to kick idle vcpus to consume the pending
3130
+ * interruptions asap.
3131
+ */
3132
+ while (origin & GISA_ADDR_MASK) {
3133
+ gisa = (struct kvm_s390_gisa *)(u64)origin;
3134
+ origin = gisa->next_alert;
3135
+ gisa->next_alert = (u32)(u64)gisa;
3136
+ kvm = container_of(gisa, struct sie_page2, gisa)->kvm;
3137
+ gi = &kvm->arch.gisa_int;
3138
+ if (hrtimer_active(&gi->timer))
3139
+ hrtimer_cancel(&gi->timer);
3140
+ hrtimer_start(&gi->timer, 0, HRTIMER_MODE_REL);
3141
+ }
3142
+ } while (!final);
3143
+
3144
+}
3145
+
29043146 void kvm_s390_gisa_clear(struct kvm *kvm)
29053147 {
2906
- if (kvm->arch.gisa) {
2907
- memset(kvm->arch.gisa, 0, sizeof(struct kvm_s390_gisa));
2908
- kvm->arch.gisa->next_alert = (u32)(u64)kvm->arch.gisa;
2909
- VM_EVENT(kvm, 3, "gisa 0x%pK cleared", kvm->arch.gisa);
2910
- }
3148
+ struct kvm_s390_gisa_interrupt *gi = &kvm->arch.gisa_int;
3149
+
3150
+ if (!gi->origin)
3151
+ return;
3152
+ gisa_clear_ipm(gi->origin);
3153
+ VM_EVENT(kvm, 3, "gisa 0x%pK cleared", gi->origin);
29113154 }
29123155
29133156 void kvm_s390_gisa_init(struct kvm *kvm)
29143157 {
2915
- if (css_general_characteristics.aiv) {
2916
- kvm->arch.gisa = &kvm->arch.sie_page2->gisa;
2917
- VM_EVENT(kvm, 3, "gisa 0x%pK initialized", kvm->arch.gisa);
2918
- kvm_s390_gisa_clear(kvm);
2919
- }
3158
+ struct kvm_s390_gisa_interrupt *gi = &kvm->arch.gisa_int;
3159
+
3160
+ if (!css_general_characteristics.aiv)
3161
+ return;
3162
+ gi->origin = &kvm->arch.sie_page2->gisa;
3163
+ gi->alert.mask = 0;
3164
+ spin_lock_init(&gi->alert.ref_lock);
3165
+ gi->expires = 50 * 1000; /* 50 usec */
3166
+ hrtimer_init(&gi->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
3167
+ gi->timer.function = gisa_vcpu_kicker;
3168
+ memset(gi->origin, 0, sizeof(struct kvm_s390_gisa));
3169
+ gi->origin->next_alert = (u32)(u64)gi->origin;
3170
+ VM_EVENT(kvm, 3, "gisa 0x%pK initialized", gi->origin);
29203171 }
29213172
29223173 void kvm_s390_gisa_destroy(struct kvm *kvm)
29233174 {
2924
- if (!kvm->arch.gisa)
3175
+ struct kvm_s390_gisa_interrupt *gi = &kvm->arch.gisa_int;
3176
+
3177
+ if (!gi->origin)
29253178 return;
2926
- kvm->arch.gisa = NULL;
3179
+ if (gi->alert.mask)
3180
+ KVM_EVENT(3, "vm 0x%pK has unexpected iam 0x%02x",
3181
+ kvm, gi->alert.mask);
3182
+ while (gisa_in_alert_list(gi->origin))
3183
+ cpu_relax();
3184
+ hrtimer_cancel(&gi->timer);
3185
+ gi->origin = NULL;
3186
+}
3187
+
3188
+/**
3189
+ * kvm_s390_gisc_register - register a guest ISC
3190
+ *
3191
+ * @kvm: the kernel vm to work with
3192
+ * @gisc: the guest interruption sub class to register
3193
+ *
3194
+ * The function extends the vm specific alert mask to use.
3195
+ * The effective IAM mask in the GISA is updated as well
3196
+ * in case the GISA is not part of the GIB alert list.
3197
+ * It will be updated latest when the IAM gets restored
3198
+ * by gisa_get_ipm_or_restore_iam().
3199
+ *
3200
+ * Returns: the nonspecific ISC (NISC) the gib alert mechanism
3201
+ * has registered with the channel subsystem.
3202
+ * -ENODEV in case the vm uses no GISA
3203
+ * -ERANGE in case the guest ISC is invalid
3204
+ */
3205
+int kvm_s390_gisc_register(struct kvm *kvm, u32 gisc)
3206
+{
3207
+ struct kvm_s390_gisa_interrupt *gi = &kvm->arch.gisa_int;
3208
+
3209
+ if (!gi->origin)
3210
+ return -ENODEV;
3211
+ if (gisc > MAX_ISC)
3212
+ return -ERANGE;
3213
+
3214
+ spin_lock(&gi->alert.ref_lock);
3215
+ gi->alert.ref_count[gisc]++;
3216
+ if (gi->alert.ref_count[gisc] == 1) {
3217
+ gi->alert.mask |= 0x80 >> gisc;
3218
+ gisa_set_iam(gi->origin, gi->alert.mask);
3219
+ }
3220
+ spin_unlock(&gi->alert.ref_lock);
3221
+
3222
+ return gib->nisc;
3223
+}
3224
+EXPORT_SYMBOL_GPL(kvm_s390_gisc_register);
3225
+
3226
+/**
3227
+ * kvm_s390_gisc_unregister - unregister a guest ISC
3228
+ *
3229
+ * @kvm: the kernel vm to work with
3230
+ * @gisc: the guest interruption sub class to register
3231
+ *
3232
+ * The function reduces the vm specific alert mask to use.
3233
+ * The effective IAM mask in the GISA is updated as well
3234
+ * in case the GISA is not part of the GIB alert list.
3235
+ * It will be updated latest when the IAM gets restored
3236
+ * by gisa_get_ipm_or_restore_iam().
3237
+ *
3238
+ * Returns: the nonspecific ISC (NISC) the gib alert mechanism
3239
+ * has registered with the channel subsystem.
3240
+ * -ENODEV in case the vm uses no GISA
3241
+ * -ERANGE in case the guest ISC is invalid
3242
+ * -EINVAL in case the guest ISC is not registered
3243
+ */
3244
+int kvm_s390_gisc_unregister(struct kvm *kvm, u32 gisc)
3245
+{
3246
+ struct kvm_s390_gisa_interrupt *gi = &kvm->arch.gisa_int;
3247
+ int rc = 0;
3248
+
3249
+ if (!gi->origin)
3250
+ return -ENODEV;
3251
+ if (gisc > MAX_ISC)
3252
+ return -ERANGE;
3253
+
3254
+ spin_lock(&gi->alert.ref_lock);
3255
+ if (gi->alert.ref_count[gisc] == 0) {
3256
+ rc = -EINVAL;
3257
+ goto out;
3258
+ }
3259
+ gi->alert.ref_count[gisc]--;
3260
+ if (gi->alert.ref_count[gisc] == 0) {
3261
+ gi->alert.mask &= ~(0x80 >> gisc);
3262
+ gisa_set_iam(gi->origin, gi->alert.mask);
3263
+ }
3264
+out:
3265
+ spin_unlock(&gi->alert.ref_lock);
3266
+
3267
+ return rc;
3268
+}
3269
+EXPORT_SYMBOL_GPL(kvm_s390_gisc_unregister);
3270
+
3271
+static void gib_alert_irq_handler(struct airq_struct *airq, bool floating)
3272
+{
3273
+ inc_irq_stat(IRQIO_GAL);
3274
+ process_gib_alert_list();
3275
+}
3276
+
3277
+static struct airq_struct gib_alert_irq = {
3278
+ .handler = gib_alert_irq_handler,
3279
+ .lsi_ptr = &gib_alert_irq.lsi_mask,
3280
+};
3281
+
3282
+void kvm_s390_gib_destroy(void)
3283
+{
3284
+ if (!gib)
3285
+ return;
3286
+ chsc_sgib(0);
3287
+ unregister_adapter_interrupt(&gib_alert_irq);
3288
+ free_page((unsigned long)gib);
3289
+ gib = NULL;
3290
+}
3291
+
3292
+int kvm_s390_gib_init(u8 nisc)
3293
+{
3294
+ int rc = 0;
3295
+
3296
+ if (!css_general_characteristics.aiv) {
3297
+ KVM_EVENT(3, "%s", "gib not initialized, no AIV facility");
3298
+ goto out;
3299
+ }
3300
+
3301
+ gib = (struct kvm_s390_gib *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
3302
+ if (!gib) {
3303
+ rc = -ENOMEM;
3304
+ goto out;
3305
+ }
3306
+
3307
+ gib_alert_irq.isc = nisc;
3308
+ if (register_adapter_interrupt(&gib_alert_irq)) {
3309
+ pr_err("Registering the GIB alert interruption handler failed\n");
3310
+ rc = -EIO;
3311
+ goto out_free_gib;
3312
+ }
3313
+
3314
+ gib->nisc = nisc;
3315
+ if (chsc_sgib((u32)(u64)gib)) {
3316
+ pr_err("Associating the GIB with the AIV facility failed\n");
3317
+ free_page((unsigned long)gib);
3318
+ gib = NULL;
3319
+ rc = -EIO;
3320
+ goto out_unreg_gal;
3321
+ }
3322
+
3323
+ KVM_EVENT(3, "gib 0x%pK (nisc=%d) initialized", gib, gib->nisc);
3324
+ goto out;
3325
+
3326
+out_unreg_gal:
3327
+ unregister_adapter_interrupt(&gib_alert_irq);
3328
+out_free_gib:
3329
+ free_page((unsigned long)gib);
3330
+ gib = NULL;
3331
+out:
3332
+ return rc;
29273333 }