diag.c 7.5 KB

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