intercept.c 10 KB

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