diag.c 7.9 KB

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