intercept.c 12 KB

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