diag.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. vcpu->stat.diagnose_10++;
  28. if (start & ~PAGE_MASK || end & ~PAGE_MASK || start >= end
  29. || start < 2 * PAGE_SIZE)
  30. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  31. VCPU_EVENT(vcpu, 5, "diag release pages %lX %lX", start, end);
  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. VCPU_EVENT(vcpu, 3, "diag page reference parameter block at 0x%llx",
  71. vcpu->run->s.regs.gprs[rx]);
  72. vcpu->stat.diagnose_258++;
  73. if (vcpu->run->s.regs.gprs[rx] & 7)
  74. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  75. rc = read_guest(vcpu, vcpu->run->s.regs.gprs[rx], rx, &parm, sizeof(parm));
  76. if (rc)
  77. return kvm_s390_inject_prog_cond(vcpu, rc);
  78. if (parm.parm_version != 2 || parm.parm_len < 5 || parm.code != 0x258)
  79. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  80. switch (parm.subcode) {
  81. case 0: /* TOKEN */
  82. VCPU_EVENT(vcpu, 3, "pageref token addr 0x%llx "
  83. "select mask 0x%llx compare mask 0x%llx",
  84. parm.token_addr, parm.select_mask, parm.compare_mask);
  85. if (vcpu->arch.pfault_token != KVM_S390_PFAULT_TOKEN_INVALID) {
  86. /*
  87. * If the pagefault handshake is already activated,
  88. * the token must not be changed. We have to return
  89. * decimal 8 instead, as mandated in SC24-6084.
  90. */
  91. vcpu->run->s.regs.gprs[ry] = 8;
  92. return 0;
  93. }
  94. if ((parm.compare_mask & parm.select_mask) != parm.compare_mask ||
  95. parm.token_addr & 7 || parm.zarch != 0x8000000000000000ULL)
  96. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  97. if (kvm_is_error_gpa(vcpu->kvm, parm.token_addr))
  98. return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
  99. vcpu->arch.pfault_token = parm.token_addr;
  100. vcpu->arch.pfault_select = parm.select_mask;
  101. vcpu->arch.pfault_compare = parm.compare_mask;
  102. vcpu->run->s.regs.gprs[ry] = 0;
  103. rc = 0;
  104. break;
  105. case 1: /*
  106. * CANCEL
  107. * Specification allows to let already pending tokens survive
  108. * the cancel, therefore to reduce code complexity, we assume
  109. * all outstanding tokens are already pending.
  110. */
  111. VCPU_EVENT(vcpu, 3, "pageref cancel addr 0x%llx", parm.token_addr);
  112. if (parm.token_addr || parm.select_mask ||
  113. parm.compare_mask || parm.zarch)
  114. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  115. vcpu->run->s.regs.gprs[ry] = 0;
  116. /*
  117. * If the pfault handling was not established or is already
  118. * canceled SC24-6084 requests to return decimal 4.
  119. */
  120. if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID)
  121. vcpu->run->s.regs.gprs[ry] = 4;
  122. else
  123. vcpu->arch.pfault_token = KVM_S390_PFAULT_TOKEN_INVALID;
  124. rc = 0;
  125. break;
  126. default:
  127. rc = -EOPNOTSUPP;
  128. break;
  129. }
  130. return rc;
  131. }
  132. static int __diag_time_slice_end(struct kvm_vcpu *vcpu)
  133. {
  134. VCPU_EVENT(vcpu, 5, "%s", "diag time slice end");
  135. vcpu->stat.diagnose_44++;
  136. kvm_vcpu_on_spin(vcpu);
  137. return 0;
  138. }
  139. static int __diag_time_slice_end_directed(struct kvm_vcpu *vcpu)
  140. {
  141. struct kvm *kvm = vcpu->kvm;
  142. struct kvm_vcpu *tcpu;
  143. int tid;
  144. int i;
  145. tid = vcpu->run->s.regs.gprs[(vcpu->arch.sie_block->ipa & 0xf0) >> 4];
  146. vcpu->stat.diagnose_9c++;
  147. VCPU_EVENT(vcpu, 5, "diag time slice end directed to %d", tid);
  148. if (tid == vcpu->vcpu_id)
  149. return 0;
  150. kvm_for_each_vcpu(i, tcpu, kvm)
  151. if (tcpu->vcpu_id == tid) {
  152. kvm_vcpu_yield_to(tcpu);
  153. break;
  154. }
  155. return 0;
  156. }
  157. static int __diag_ipl_functions(struct kvm_vcpu *vcpu)
  158. {
  159. unsigned int reg = vcpu->arch.sie_block->ipa & 0xf;
  160. unsigned long subcode = vcpu->run->s.regs.gprs[reg] & 0xffff;
  161. VCPU_EVENT(vcpu, 3, "diag ipl functions, subcode %lx", subcode);
  162. vcpu->stat.diagnose_308++;
  163. switch (subcode) {
  164. case 3:
  165. vcpu->run->s390_reset_flags = KVM_S390_RESET_CLEAR;
  166. break;
  167. case 4:
  168. vcpu->run->s390_reset_flags = 0;
  169. break;
  170. default:
  171. return -EOPNOTSUPP;
  172. }
  173. if (!kvm_s390_user_cpu_state_ctrl(vcpu->kvm))
  174. kvm_s390_vcpu_stop(vcpu);
  175. vcpu->run->s390_reset_flags |= KVM_S390_RESET_SUBSYSTEM;
  176. vcpu->run->s390_reset_flags |= KVM_S390_RESET_IPL;
  177. vcpu->run->s390_reset_flags |= KVM_S390_RESET_CPU_INIT;
  178. vcpu->run->exit_reason = KVM_EXIT_S390_RESET;
  179. VCPU_EVENT(vcpu, 3, "requesting userspace resets %llx",
  180. vcpu->run->s390_reset_flags);
  181. trace_kvm_s390_request_resets(vcpu->run->s390_reset_flags);
  182. return -EREMOTE;
  183. }
  184. static int __diag_virtio_hypercall(struct kvm_vcpu *vcpu)
  185. {
  186. int ret;
  187. vcpu->stat.diagnose_500++;
  188. /* No virtio-ccw notification? Get out quickly. */
  189. if (!vcpu->kvm->arch.css_support ||
  190. (vcpu->run->s.regs.gprs[1] != KVM_S390_VIRTIO_CCW_NOTIFY))
  191. return -EOPNOTSUPP;
  192. /*
  193. * The layout is as follows:
  194. * - gpr 2 contains the subchannel id (passed as addr)
  195. * - gpr 3 contains the virtqueue index (passed as datamatch)
  196. * - gpr 4 contains the index on the bus (optionally)
  197. */
  198. ret = kvm_io_bus_write_cookie(vcpu, KVM_VIRTIO_CCW_NOTIFY_BUS,
  199. vcpu->run->s.regs.gprs[2] & 0xffffffff,
  200. 8, &vcpu->run->s.regs.gprs[3],
  201. vcpu->run->s.regs.gprs[4]);
  202. /*
  203. * Return cookie in gpr 2, but don't overwrite the register if the
  204. * diagnose will be handled by userspace.
  205. */
  206. if (ret != -EOPNOTSUPP)
  207. vcpu->run->s.regs.gprs[2] = ret;
  208. /* kvm_io_bus_write_cookie returns -EOPNOTSUPP if it found no match. */
  209. return ret < 0 ? ret : 0;
  210. }
  211. int kvm_s390_handle_diag(struct kvm_vcpu *vcpu)
  212. {
  213. int code = kvm_s390_get_base_disp_rs(vcpu, NULL) & 0xffff;
  214. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
  215. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  216. trace_kvm_s390_handle_diag(vcpu, code);
  217. switch (code) {
  218. case 0x10:
  219. return diag_release_pages(vcpu);
  220. case 0x44:
  221. return __diag_time_slice_end(vcpu);
  222. case 0x9c:
  223. return __diag_time_slice_end_directed(vcpu);
  224. case 0x258:
  225. return __diag_page_ref_service(vcpu);
  226. case 0x308:
  227. return __diag_ipl_functions(vcpu);
  228. case 0x500:
  229. return __diag_virtio_hypercall(vcpu);
  230. default:
  231. return -EOPNOTSUPP;
  232. }
  233. }