diag.c 7.1 KB

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