diag.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * handling diagnose instructions
  3. *
  4. * Copyright IBM Corp. 2008, 2011
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License (version 2 only)
  8. * as published by the Free Software Foundation.
  9. *
  10. * Author(s): Carsten Otte <cotte@de.ibm.com>
  11. * Christian Borntraeger <borntraeger@de.ibm.com>
  12. */
  13. #include <linux/kvm.h>
  14. #include <linux/kvm_host.h>
  15. #include <asm/virtio-ccw.h>
  16. #include "kvm-s390.h"
  17. #include "trace.h"
  18. #include "trace-s390.h"
  19. static int diag_release_pages(struct kvm_vcpu *vcpu)
  20. {
  21. unsigned long start, end;
  22. unsigned long prefix = vcpu->arch.sie_block->prefix;
  23. start = vcpu->run->s.regs.gprs[(vcpu->arch.sie_block->ipa & 0xf0) >> 4];
  24. end = vcpu->run->s.regs.gprs[vcpu->arch.sie_block->ipa & 0xf] + 4096;
  25. if (start & ~PAGE_MASK || end & ~PAGE_MASK || start > end
  26. || start < 2 * PAGE_SIZE)
  27. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  28. VCPU_EVENT(vcpu, 5, "diag release pages %lX %lX", start, end);
  29. vcpu->stat.diagnose_10++;
  30. /* we checked for start > end above */
  31. if (end < prefix || start >= prefix + 2 * PAGE_SIZE) {
  32. gmap_discard(start, end, vcpu->arch.gmap);
  33. } else {
  34. if (start < prefix)
  35. gmap_discard(start, prefix, vcpu->arch.gmap);
  36. if (end >= prefix)
  37. gmap_discard(prefix + 2 * PAGE_SIZE,
  38. end, vcpu->arch.gmap);
  39. }
  40. return 0;
  41. }
  42. static int __diag_time_slice_end(struct kvm_vcpu *vcpu)
  43. {
  44. VCPU_EVENT(vcpu, 5, "%s", "diag time slice end");
  45. vcpu->stat.diagnose_44++;
  46. kvm_vcpu_on_spin(vcpu);
  47. return 0;
  48. }
  49. static int __diag_time_slice_end_directed(struct kvm_vcpu *vcpu)
  50. {
  51. struct kvm *kvm = vcpu->kvm;
  52. struct kvm_vcpu *tcpu;
  53. int tid;
  54. int i;
  55. tid = vcpu->run->s.regs.gprs[(vcpu->arch.sie_block->ipa & 0xf0) >> 4];
  56. vcpu->stat.diagnose_9c++;
  57. VCPU_EVENT(vcpu, 5, "diag time slice end directed to %d", tid);
  58. if (tid == vcpu->vcpu_id)
  59. return 0;
  60. kvm_for_each_vcpu(i, tcpu, kvm)
  61. if (tcpu->vcpu_id == tid) {
  62. kvm_vcpu_yield_to(tcpu);
  63. break;
  64. }
  65. return 0;
  66. }
  67. static int __diag_ipl_functions(struct kvm_vcpu *vcpu)
  68. {
  69. unsigned int reg = vcpu->arch.sie_block->ipa & 0xf;
  70. unsigned long subcode = vcpu->run->s.regs.gprs[reg] & 0xffff;
  71. VCPU_EVENT(vcpu, 5, "diag ipl functions, subcode %lx", subcode);
  72. switch (subcode) {
  73. case 3:
  74. vcpu->run->s390_reset_flags = KVM_S390_RESET_CLEAR;
  75. break;
  76. case 4:
  77. vcpu->run->s390_reset_flags = 0;
  78. break;
  79. default:
  80. return -EOPNOTSUPP;
  81. }
  82. atomic_set_mask(CPUSTAT_STOPPED, &vcpu->arch.sie_block->cpuflags);
  83. vcpu->run->s390_reset_flags |= KVM_S390_RESET_SUBSYSTEM;
  84. vcpu->run->s390_reset_flags |= KVM_S390_RESET_IPL;
  85. vcpu->run->s390_reset_flags |= KVM_S390_RESET_CPU_INIT;
  86. vcpu->run->exit_reason = KVM_EXIT_S390_RESET;
  87. VCPU_EVENT(vcpu, 3, "requesting userspace resets %llx",
  88. vcpu->run->s390_reset_flags);
  89. trace_kvm_s390_request_resets(vcpu->run->s390_reset_flags);
  90. return -EREMOTE;
  91. }
  92. static int __diag_virtio_hypercall(struct kvm_vcpu *vcpu)
  93. {
  94. int ret, idx;
  95. /* No virtio-ccw notification? Get out quickly. */
  96. if (!vcpu->kvm->arch.css_support ||
  97. (vcpu->run->s.regs.gprs[1] != KVM_S390_VIRTIO_CCW_NOTIFY))
  98. return -EOPNOTSUPP;
  99. idx = srcu_read_lock(&vcpu->kvm->srcu);
  100. /*
  101. * The layout is as follows:
  102. * - gpr 2 contains the subchannel id (passed as addr)
  103. * - gpr 3 contains the virtqueue index (passed as datamatch)
  104. */
  105. ret = kvm_io_bus_write(vcpu->kvm, KVM_VIRTIO_CCW_NOTIFY_BUS,
  106. vcpu->run->s.regs.gprs[2],
  107. 8, &vcpu->run->s.regs.gprs[3]);
  108. srcu_read_unlock(&vcpu->kvm->srcu, idx);
  109. /* kvm_io_bus_write returns -EOPNOTSUPP if it found no match. */
  110. return ret < 0 ? ret : 0;
  111. }
  112. int kvm_s390_handle_diag(struct kvm_vcpu *vcpu)
  113. {
  114. int code = (vcpu->arch.sie_block->ipb & 0xfff0000) >> 16;
  115. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
  116. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  117. trace_kvm_s390_handle_diag(vcpu, code);
  118. switch (code) {
  119. case 0x10:
  120. return diag_release_pages(vcpu);
  121. case 0x44:
  122. return __diag_time_slice_end(vcpu);
  123. case 0x9c:
  124. return __diag_time_slice_end_directed(vcpu);
  125. case 0x308:
  126. return __diag_ipl_functions(vcpu);
  127. case 0x500:
  128. return __diag_virtio_hypercall(vcpu);
  129. default:
  130. return -EOPNOTSUPP;
  131. }
  132. }