intercept.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. /*
  2. * in-kernel handling for sie intercepts
  3. *
  4. * Copyright IBM Corp. 2008, 2014
  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_host.h>
  14. #include <linux/errno.h>
  15. #include <linux/pagemap.h>
  16. #include <asm/kvm_host.h>
  17. #include <asm/asm-offsets.h>
  18. #include <asm/irq.h>
  19. #include "kvm-s390.h"
  20. #include "gaccess.h"
  21. #include "trace.h"
  22. #include "trace-s390.h"
  23. static const intercept_handler_t instruction_handlers[256] = {
  24. [0x01] = kvm_s390_handle_01,
  25. [0x82] = kvm_s390_handle_lpsw,
  26. [0x83] = kvm_s390_handle_diag,
  27. [0xaa] = kvm_s390_handle_aa,
  28. [0xae] = kvm_s390_handle_sigp,
  29. [0xb2] = kvm_s390_handle_b2,
  30. [0xb6] = kvm_s390_handle_stctl,
  31. [0xb7] = kvm_s390_handle_lctl,
  32. [0xb9] = kvm_s390_handle_b9,
  33. [0xe5] = kvm_s390_handle_e5,
  34. [0xeb] = kvm_s390_handle_eb,
  35. };
  36. u8 kvm_s390_get_ilen(struct kvm_vcpu *vcpu)
  37. {
  38. struct kvm_s390_sie_block *sie_block = vcpu->arch.sie_block;
  39. u8 ilen = 0;
  40. switch (vcpu->arch.sie_block->icptcode) {
  41. case ICPT_INST:
  42. case ICPT_INSTPROGI:
  43. case ICPT_OPEREXC:
  44. case ICPT_PARTEXEC:
  45. case ICPT_IOINST:
  46. /* instruction only stored for these icptcodes */
  47. ilen = insn_length(vcpu->arch.sie_block->ipa >> 8);
  48. /* Use the length of the EXECUTE instruction if necessary */
  49. if (sie_block->icptstatus & 1) {
  50. ilen = (sie_block->icptstatus >> 4) & 0x6;
  51. if (!ilen)
  52. ilen = 4;
  53. }
  54. break;
  55. case ICPT_PROGI:
  56. /* bit 1+2 of pgmilc are the ilc, so we directly get ilen */
  57. ilen = vcpu->arch.sie_block->pgmilc & 0x6;
  58. break;
  59. }
  60. return ilen;
  61. }
  62. static int handle_noop(struct kvm_vcpu *vcpu)
  63. {
  64. switch (vcpu->arch.sie_block->icptcode) {
  65. case 0x10:
  66. vcpu->stat.exit_external_request++;
  67. break;
  68. default:
  69. break; /* nothing */
  70. }
  71. return 0;
  72. }
  73. static int handle_stop(struct kvm_vcpu *vcpu)
  74. {
  75. struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
  76. int rc = 0;
  77. uint8_t flags, stop_pending;
  78. vcpu->stat.exit_stop_request++;
  79. /* delay the stop if any non-stop irq is pending */
  80. if (kvm_s390_vcpu_has_irq(vcpu, 1))
  81. return 0;
  82. /* avoid races with the injection/SIGP STOP code */
  83. spin_lock(&li->lock);
  84. flags = li->irq.stop.flags;
  85. stop_pending = kvm_s390_is_stop_irq_pending(vcpu);
  86. spin_unlock(&li->lock);
  87. trace_kvm_s390_stop_request(stop_pending, flags);
  88. if (!stop_pending)
  89. return 0;
  90. if (flags & KVM_S390_STOP_FLAG_STORE_STATUS) {
  91. rc = kvm_s390_vcpu_store_status(vcpu,
  92. KVM_S390_STORE_STATUS_NOADDR);
  93. if (rc)
  94. return rc;
  95. }
  96. if (!kvm_s390_user_cpu_state_ctrl(vcpu->kvm))
  97. kvm_s390_vcpu_stop(vcpu);
  98. return -EOPNOTSUPP;
  99. }
  100. static int handle_validity(struct kvm_vcpu *vcpu)
  101. {
  102. int viwhy = vcpu->arch.sie_block->ipb >> 16;
  103. vcpu->stat.exit_validity++;
  104. trace_kvm_s390_intercept_validity(vcpu, viwhy);
  105. KVM_EVENT(3, "validity intercept 0x%x for pid %u (kvm 0x%pK)", viwhy,
  106. current->pid, vcpu->kvm);
  107. /* do not warn on invalid runtime instrumentation mode */
  108. WARN_ONCE(viwhy != 0x44, "kvm: unhandled validity intercept 0x%x\n",
  109. viwhy);
  110. return -EINVAL;
  111. }
  112. static int handle_instruction(struct kvm_vcpu *vcpu)
  113. {
  114. intercept_handler_t handler;
  115. vcpu->stat.exit_instruction++;
  116. trace_kvm_s390_intercept_instruction(vcpu,
  117. vcpu->arch.sie_block->ipa,
  118. vcpu->arch.sie_block->ipb);
  119. handler = instruction_handlers[vcpu->arch.sie_block->ipa >> 8];
  120. if (handler)
  121. return handler(vcpu);
  122. return -EOPNOTSUPP;
  123. }
  124. static int inject_prog_on_prog_intercept(struct kvm_vcpu *vcpu)
  125. {
  126. struct kvm_s390_pgm_info pgm_info = {
  127. .code = vcpu->arch.sie_block->iprcc,
  128. /* the PSW has already been rewound */
  129. .flags = KVM_S390_PGM_FLAGS_NO_REWIND,
  130. };
  131. switch (vcpu->arch.sie_block->iprcc & ~PGM_PER) {
  132. case PGM_AFX_TRANSLATION:
  133. case PGM_ASX_TRANSLATION:
  134. case PGM_EX_TRANSLATION:
  135. case PGM_LFX_TRANSLATION:
  136. case PGM_LSTE_SEQUENCE:
  137. case PGM_LSX_TRANSLATION:
  138. case PGM_LX_TRANSLATION:
  139. case PGM_PRIMARY_AUTHORITY:
  140. case PGM_SECONDARY_AUTHORITY:
  141. case PGM_SPACE_SWITCH:
  142. pgm_info.trans_exc_code = vcpu->arch.sie_block->tecmc;
  143. break;
  144. case PGM_ALEN_TRANSLATION:
  145. case PGM_ALE_SEQUENCE:
  146. case PGM_ASTE_INSTANCE:
  147. case PGM_ASTE_SEQUENCE:
  148. case PGM_ASTE_VALIDITY:
  149. case PGM_EXTENDED_AUTHORITY:
  150. pgm_info.exc_access_id = vcpu->arch.sie_block->eai;
  151. break;
  152. case PGM_ASCE_TYPE:
  153. case PGM_PAGE_TRANSLATION:
  154. case PGM_REGION_FIRST_TRANS:
  155. case PGM_REGION_SECOND_TRANS:
  156. case PGM_REGION_THIRD_TRANS:
  157. case PGM_SEGMENT_TRANSLATION:
  158. pgm_info.trans_exc_code = vcpu->arch.sie_block->tecmc;
  159. pgm_info.exc_access_id = vcpu->arch.sie_block->eai;
  160. pgm_info.op_access_id = vcpu->arch.sie_block->oai;
  161. break;
  162. case PGM_MONITOR:
  163. pgm_info.mon_class_nr = vcpu->arch.sie_block->mcn;
  164. pgm_info.mon_code = vcpu->arch.sie_block->tecmc;
  165. break;
  166. case PGM_VECTOR_PROCESSING:
  167. case PGM_DATA:
  168. pgm_info.data_exc_code = vcpu->arch.sie_block->dxc;
  169. break;
  170. case PGM_PROTECTION:
  171. pgm_info.trans_exc_code = vcpu->arch.sie_block->tecmc;
  172. pgm_info.exc_access_id = vcpu->arch.sie_block->eai;
  173. break;
  174. default:
  175. break;
  176. }
  177. if (vcpu->arch.sie_block->iprcc & PGM_PER) {
  178. pgm_info.per_code = vcpu->arch.sie_block->perc;
  179. pgm_info.per_atmid = vcpu->arch.sie_block->peratmid;
  180. pgm_info.per_address = vcpu->arch.sie_block->peraddr;
  181. pgm_info.per_access_id = vcpu->arch.sie_block->peraid;
  182. }
  183. return kvm_s390_inject_prog_irq(vcpu, &pgm_info);
  184. }
  185. /*
  186. * restore ITDB to program-interruption TDB in guest lowcore
  187. * and set TX abort indication if required
  188. */
  189. static int handle_itdb(struct kvm_vcpu *vcpu)
  190. {
  191. struct kvm_s390_itdb *itdb;
  192. int rc;
  193. if (!IS_TE_ENABLED(vcpu) || !IS_ITDB_VALID(vcpu))
  194. return 0;
  195. if (current->thread.per_flags & PER_FLAG_NO_TE)
  196. return 0;
  197. itdb = (struct kvm_s390_itdb *)vcpu->arch.sie_block->itdba;
  198. rc = write_guest_lc(vcpu, __LC_PGM_TDB, itdb, sizeof(*itdb));
  199. if (rc)
  200. return rc;
  201. memset(itdb, 0, sizeof(*itdb));
  202. return 0;
  203. }
  204. #define per_event(vcpu) (vcpu->arch.sie_block->iprcc & PGM_PER)
  205. static int handle_prog(struct kvm_vcpu *vcpu)
  206. {
  207. psw_t psw;
  208. int rc;
  209. vcpu->stat.exit_program_interruption++;
  210. if (guestdbg_enabled(vcpu) && per_event(vcpu)) {
  211. kvm_s390_handle_per_event(vcpu);
  212. /* the interrupt might have been filtered out completely */
  213. if (vcpu->arch.sie_block->iprcc == 0)
  214. return 0;
  215. }
  216. trace_kvm_s390_intercept_prog(vcpu, vcpu->arch.sie_block->iprcc);
  217. if (vcpu->arch.sie_block->iprcc == PGM_SPECIFICATION) {
  218. rc = read_guest_lc(vcpu, __LC_PGM_NEW_PSW, &psw, sizeof(psw_t));
  219. if (rc)
  220. return rc;
  221. /* Avoid endless loops of specification exceptions */
  222. if (!is_valid_psw(&psw))
  223. return -EOPNOTSUPP;
  224. }
  225. rc = handle_itdb(vcpu);
  226. if (rc)
  227. return rc;
  228. return inject_prog_on_prog_intercept(vcpu);
  229. }
  230. /**
  231. * handle_external_interrupt - used for external interruption interceptions
  232. *
  233. * This interception only occurs if the CPUSTAT_EXT_INT bit was set, or if
  234. * the new PSW does not have external interrupts disabled. In the first case,
  235. * we've got to deliver the interrupt manually, and in the second case, we
  236. * drop to userspace to handle the situation there.
  237. */
  238. static int handle_external_interrupt(struct kvm_vcpu *vcpu)
  239. {
  240. u16 eic = vcpu->arch.sie_block->eic;
  241. struct kvm_s390_irq irq;
  242. psw_t newpsw;
  243. int rc;
  244. vcpu->stat.exit_external_interrupt++;
  245. rc = read_guest_lc(vcpu, __LC_EXT_NEW_PSW, &newpsw, sizeof(psw_t));
  246. if (rc)
  247. return rc;
  248. /* We can not handle clock comparator or timer interrupt with bad PSW */
  249. if ((eic == EXT_IRQ_CLK_COMP || eic == EXT_IRQ_CPU_TIMER) &&
  250. (newpsw.mask & PSW_MASK_EXT))
  251. return -EOPNOTSUPP;
  252. switch (eic) {
  253. case EXT_IRQ_CLK_COMP:
  254. irq.type = KVM_S390_INT_CLOCK_COMP;
  255. break;
  256. case EXT_IRQ_CPU_TIMER:
  257. irq.type = KVM_S390_INT_CPU_TIMER;
  258. break;
  259. case EXT_IRQ_EXTERNAL_CALL:
  260. irq.type = KVM_S390_INT_EXTERNAL_CALL;
  261. irq.u.extcall.code = vcpu->arch.sie_block->extcpuaddr;
  262. rc = kvm_s390_inject_vcpu(vcpu, &irq);
  263. /* ignore if another external call is already pending */
  264. if (rc == -EBUSY)
  265. return 0;
  266. return rc;
  267. default:
  268. return -EOPNOTSUPP;
  269. }
  270. return kvm_s390_inject_vcpu(vcpu, &irq);
  271. }
  272. /**
  273. * Handle MOVE PAGE partial execution interception.
  274. *
  275. * This interception can only happen for guests with DAT disabled and
  276. * addresses that are currently not mapped in the host. Thus we try to
  277. * set up the mappings for the corresponding user pages here (or throw
  278. * addressing exceptions in case of illegal guest addresses).
  279. */
  280. static int handle_mvpg_pei(struct kvm_vcpu *vcpu)
  281. {
  282. unsigned long srcaddr, dstaddr;
  283. int reg1, reg2, rc;
  284. kvm_s390_get_regs_rre(vcpu, &reg1, &reg2);
  285. /* Make sure that the source is paged-in */
  286. rc = guest_translate_address(vcpu, vcpu->run->s.regs.gprs[reg2],
  287. reg2, &srcaddr, GACC_FETCH);
  288. if (rc)
  289. return kvm_s390_inject_prog_cond(vcpu, rc);
  290. rc = kvm_arch_fault_in_page(vcpu, srcaddr, 0);
  291. if (rc != 0)
  292. return rc;
  293. /* Make sure that the destination is paged-in */
  294. rc = guest_translate_address(vcpu, vcpu->run->s.regs.gprs[reg1],
  295. reg1, &dstaddr, GACC_STORE);
  296. if (rc)
  297. return kvm_s390_inject_prog_cond(vcpu, rc);
  298. rc = kvm_arch_fault_in_page(vcpu, dstaddr, 1);
  299. if (rc != 0)
  300. return rc;
  301. kvm_s390_retry_instr(vcpu);
  302. return 0;
  303. }
  304. static int handle_partial_execution(struct kvm_vcpu *vcpu)
  305. {
  306. vcpu->stat.exit_pei++;
  307. if (vcpu->arch.sie_block->ipa == 0xb254) /* MVPG */
  308. return handle_mvpg_pei(vcpu);
  309. if (vcpu->arch.sie_block->ipa >> 8 == 0xae) /* SIGP */
  310. return kvm_s390_handle_sigp_pei(vcpu);
  311. return -EOPNOTSUPP;
  312. }
  313. static int handle_operexc(struct kvm_vcpu *vcpu)
  314. {
  315. vcpu->stat.exit_operation_exception++;
  316. trace_kvm_s390_handle_operexc(vcpu, vcpu->arch.sie_block->ipa,
  317. vcpu->arch.sie_block->ipb);
  318. if (vcpu->arch.sie_block->ipa == 0xb256 &&
  319. test_kvm_facility(vcpu->kvm, 74))
  320. return handle_sthyi(vcpu);
  321. if (vcpu->arch.sie_block->ipa == 0 && vcpu->kvm->arch.user_instr0)
  322. return -EOPNOTSUPP;
  323. return kvm_s390_inject_program_int(vcpu, PGM_OPERATION);
  324. }
  325. int kvm_handle_sie_intercept(struct kvm_vcpu *vcpu)
  326. {
  327. int rc, per_rc = 0;
  328. if (kvm_is_ucontrol(vcpu->kvm))
  329. return -EOPNOTSUPP;
  330. switch (vcpu->arch.sie_block->icptcode) {
  331. case 0x10:
  332. case 0x18:
  333. return handle_noop(vcpu);
  334. case 0x04:
  335. rc = handle_instruction(vcpu);
  336. break;
  337. case 0x08:
  338. return handle_prog(vcpu);
  339. case 0x14:
  340. return handle_external_interrupt(vcpu);
  341. case 0x1c:
  342. return kvm_s390_handle_wait(vcpu);
  343. case 0x20:
  344. return handle_validity(vcpu);
  345. case 0x28:
  346. return handle_stop(vcpu);
  347. case 0x2c:
  348. rc = handle_operexc(vcpu);
  349. break;
  350. case 0x38:
  351. rc = handle_partial_execution(vcpu);
  352. break;
  353. default:
  354. return -EOPNOTSUPP;
  355. }
  356. /* process PER, also if the instrution is processed in user space */
  357. if (vcpu->arch.sie_block->icptstatus & 0x02 &&
  358. (!rc || rc == -EOPNOTSUPP))
  359. per_rc = kvm_s390_handle_per_ifetch_icpt(vcpu);
  360. return per_rc ? per_rc : rc;
  361. }