intercept.c 9.3 KB

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