diag.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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/pgalloc.h>
  16. #include <asm/virtio-ccw.h>
  17. #include "kvm-s390.h"
  18. #include "trace.h"
  19. #include "trace-s390.h"
  20. #include "gaccess.h"
  21. static int diag_release_pages(struct kvm_vcpu *vcpu)
  22. {
  23. unsigned long start, end;
  24. unsigned long prefix = kvm_s390_get_prefix(vcpu);
  25. start = vcpu->run->s.regs.gprs[(vcpu->arch.sie_block->ipa & 0xf0) >> 4];
  26. end = vcpu->run->s.regs.gprs[vcpu->arch.sie_block->ipa & 0xf] + 4096;
  27. if (start & ~PAGE_MASK || end & ~PAGE_MASK || start > end
  28. || start < 2 * PAGE_SIZE)
  29. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  30. VCPU_EVENT(vcpu, 5, "diag release pages %lX %lX", start, end);
  31. vcpu->stat.diagnose_10++;
  32. /* we checked for start > end above */
  33. if (end < prefix || start >= prefix + 2 * PAGE_SIZE) {
  34. gmap_discard(start, end, vcpu->arch.gmap);
  35. } else {
  36. if (start < prefix)
  37. gmap_discard(start, prefix, vcpu->arch.gmap);
  38. if (end >= prefix)
  39. gmap_discard(prefix + 2 * PAGE_SIZE,
  40. end, vcpu->arch.gmap);
  41. }
  42. return 0;
  43. }
  44. static int __diag_page_ref_service(struct kvm_vcpu *vcpu)
  45. {
  46. struct prs_parm {
  47. u16 code;
  48. u16 subcode;
  49. u16 parm_len;
  50. u16 parm_version;
  51. u64 token_addr;
  52. u64 select_mask;
  53. u64 compare_mask;
  54. u64 zarch;
  55. };
  56. struct prs_parm parm;
  57. int rc;
  58. u16 rx = (vcpu->arch.sie_block->ipa & 0xf0) >> 4;
  59. u16 ry = (vcpu->arch.sie_block->ipa & 0x0f);
  60. if (vcpu->run->s.regs.gprs[rx] & 7)
  61. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  62. rc = read_guest(vcpu, vcpu->run->s.regs.gprs[rx], &parm, sizeof(parm));
  63. if (rc)
  64. return kvm_s390_inject_prog_cond(vcpu, rc);
  65. if (parm.parm_version != 2 || parm.parm_len < 5 || parm.code != 0x258)
  66. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  67. switch (parm.subcode) {
  68. case 0: /* TOKEN */
  69. if (vcpu->arch.pfault_token != KVM_S390_PFAULT_TOKEN_INVALID) {
  70. /*
  71. * If the pagefault handshake is already activated,
  72. * the token must not be changed. We have to return
  73. * decimal 8 instead, as mandated in SC24-6084.
  74. */
  75. vcpu->run->s.regs.gprs[ry] = 8;
  76. return 0;
  77. }
  78. if ((parm.compare_mask & parm.select_mask) != parm.compare_mask ||
  79. parm.token_addr & 7 || parm.zarch != 0x8000000000000000ULL)
  80. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  81. if (kvm_is_error_gpa(vcpu->kvm, parm.token_addr))
  82. return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
  83. vcpu->arch.pfault_token = parm.token_addr;
  84. vcpu->arch.pfault_select = parm.select_mask;
  85. vcpu->arch.pfault_compare = parm.compare_mask;
  86. vcpu->run->s.regs.gprs[ry] = 0;
  87. rc = 0;
  88. break;
  89. case 1: /*
  90. * CANCEL
  91. * Specification allows to let already pending tokens survive
  92. * the cancel, therefore to reduce code complexity, we assume
  93. * all outstanding tokens are already pending.
  94. */
  95. if (parm.token_addr || parm.select_mask ||
  96. parm.compare_mask || parm.zarch)
  97. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  98. vcpu->run->s.regs.gprs[ry] = 0;
  99. /*
  100. * If the pfault handling was not established or is already
  101. * canceled SC24-6084 requests to return decimal 4.
  102. */
  103. if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID)
  104. vcpu->run->s.regs.gprs[ry] = 4;
  105. else
  106. vcpu->arch.pfault_token = KVM_S390_PFAULT_TOKEN_INVALID;
  107. rc = 0;
  108. break;
  109. default:
  110. rc = -EOPNOTSUPP;
  111. break;
  112. }
  113. return rc;
  114. }
  115. static int __diag_time_slice_end(struct kvm_vcpu *vcpu)
  116. {
  117. VCPU_EVENT(vcpu, 5, "%s", "diag time slice end");
  118. vcpu->stat.diagnose_44++;
  119. kvm_vcpu_on_spin(vcpu);
  120. return 0;
  121. }
  122. static int __diag_time_slice_end_directed(struct kvm_vcpu *vcpu)
  123. {
  124. struct kvm *kvm = vcpu->kvm;
  125. struct kvm_vcpu *tcpu;
  126. int tid;
  127. int i;
  128. tid = vcpu->run->s.regs.gprs[(vcpu->arch.sie_block->ipa & 0xf0) >> 4];
  129. vcpu->stat.diagnose_9c++;
  130. VCPU_EVENT(vcpu, 5, "diag time slice end directed to %d", tid);
  131. if (tid == vcpu->vcpu_id)
  132. return 0;
  133. kvm_for_each_vcpu(i, tcpu, kvm)
  134. if (tcpu->vcpu_id == tid) {
  135. kvm_vcpu_yield_to(tcpu);
  136. break;
  137. }
  138. return 0;
  139. }
  140. static int __diag_ipl_functions(struct kvm_vcpu *vcpu)
  141. {
  142. unsigned int reg = vcpu->arch.sie_block->ipa & 0xf;
  143. unsigned long subcode = vcpu->run->s.regs.gprs[reg] & 0xffff;
  144. VCPU_EVENT(vcpu, 5, "diag ipl functions, subcode %lx", subcode);
  145. switch (subcode) {
  146. case 3:
  147. vcpu->run->s390_reset_flags = KVM_S390_RESET_CLEAR;
  148. break;
  149. case 4:
  150. vcpu->run->s390_reset_flags = 0;
  151. break;
  152. default:
  153. return -EOPNOTSUPP;
  154. }
  155. kvm_s390_vcpu_stop(vcpu);
  156. vcpu->run->s390_reset_flags |= KVM_S390_RESET_SUBSYSTEM;
  157. vcpu->run->s390_reset_flags |= KVM_S390_RESET_IPL;
  158. vcpu->run->s390_reset_flags |= KVM_S390_RESET_CPU_INIT;
  159. vcpu->run->exit_reason = KVM_EXIT_S390_RESET;
  160. VCPU_EVENT(vcpu, 3, "requesting userspace resets %llx",
  161. vcpu->run->s390_reset_flags);
  162. trace_kvm_s390_request_resets(vcpu->run->s390_reset_flags);
  163. return -EREMOTE;
  164. }
  165. static int __diag_virtio_hypercall(struct kvm_vcpu *vcpu)
  166. {
  167. int ret;
  168. /* No virtio-ccw notification? Get out quickly. */
  169. if (!vcpu->kvm->arch.css_support ||
  170. (vcpu->run->s.regs.gprs[1] != KVM_S390_VIRTIO_CCW_NOTIFY))
  171. return -EOPNOTSUPP;
  172. /*
  173. * The layout is as follows:
  174. * - gpr 2 contains the subchannel id (passed as addr)
  175. * - gpr 3 contains the virtqueue index (passed as datamatch)
  176. * - gpr 4 contains the index on the bus (optionally)
  177. */
  178. ret = kvm_io_bus_write_cookie(vcpu->kvm, KVM_VIRTIO_CCW_NOTIFY_BUS,
  179. vcpu->run->s.regs.gprs[2] & 0xffffffff,
  180. 8, &vcpu->run->s.regs.gprs[3],
  181. vcpu->run->s.regs.gprs[4]);
  182. /*
  183. * Return cookie in gpr 2, but don't overwrite the register if the
  184. * diagnose will be handled by userspace.
  185. */
  186. if (ret != -EOPNOTSUPP)
  187. vcpu->run->s.regs.gprs[2] = ret;
  188. /* kvm_io_bus_write_cookie returns -EOPNOTSUPP if it found no match. */
  189. return ret < 0 ? ret : 0;
  190. }
  191. int kvm_s390_handle_diag(struct kvm_vcpu *vcpu)
  192. {
  193. int code = kvm_s390_get_base_disp_rs(vcpu) & 0xffff;
  194. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
  195. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  196. trace_kvm_s390_handle_diag(vcpu, code);
  197. switch (code) {
  198. case 0x10:
  199. return diag_release_pages(vcpu);
  200. case 0x44:
  201. return __diag_time_slice_end(vcpu);
  202. case 0x9c:
  203. return __diag_time_slice_end_directed(vcpu);
  204. case 0x258:
  205. return __diag_page_ref_service(vcpu);
  206. case 0x308:
  207. return __diag_ipl_functions(vcpu);
  208. case 0x500:
  209. return __diag_virtio_hypercall(vcpu);
  210. default:
  211. return -EOPNOTSUPP;
  212. }
  213. }