intercept.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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. static int handle_instruction_and_prog(struct kvm_vcpu *vcpu)
  213. {
  214. int rc, rc2;
  215. vcpu->stat.exit_instr_and_program++;
  216. rc = handle_instruction(vcpu);
  217. rc2 = handle_prog(vcpu);
  218. if (rc == -EOPNOTSUPP)
  219. vcpu->arch.sie_block->icptcode = 0x04;
  220. if (rc)
  221. return rc;
  222. return rc2;
  223. }
  224. /**
  225. * handle_external_interrupt - used for external interruption interceptions
  226. *
  227. * This interception only occurs if the CPUSTAT_EXT_INT bit was set, or if
  228. * the new PSW does not have external interrupts disabled. In the first case,
  229. * we've got to deliver the interrupt manually, and in the second case, we
  230. * drop to userspace to handle the situation there.
  231. */
  232. static int handle_external_interrupt(struct kvm_vcpu *vcpu)
  233. {
  234. u16 eic = vcpu->arch.sie_block->eic;
  235. struct kvm_s390_irq irq;
  236. psw_t newpsw;
  237. int rc;
  238. vcpu->stat.exit_external_interrupt++;
  239. rc = read_guest_lc(vcpu, __LC_EXT_NEW_PSW, &newpsw, sizeof(psw_t));
  240. if (rc)
  241. return rc;
  242. /* We can not handle clock comparator or timer interrupt with bad PSW */
  243. if ((eic == EXT_IRQ_CLK_COMP || eic == EXT_IRQ_CPU_TIMER) &&
  244. (newpsw.mask & PSW_MASK_EXT))
  245. return -EOPNOTSUPP;
  246. switch (eic) {
  247. case EXT_IRQ_CLK_COMP:
  248. irq.type = KVM_S390_INT_CLOCK_COMP;
  249. break;
  250. case EXT_IRQ_CPU_TIMER:
  251. irq.type = KVM_S390_INT_CPU_TIMER;
  252. break;
  253. case EXT_IRQ_EXTERNAL_CALL:
  254. irq.type = KVM_S390_INT_EXTERNAL_CALL;
  255. irq.u.extcall.code = vcpu->arch.sie_block->extcpuaddr;
  256. rc = kvm_s390_inject_vcpu(vcpu, &irq);
  257. /* ignore if another external call is already pending */
  258. if (rc == -EBUSY)
  259. return 0;
  260. return rc;
  261. default:
  262. return -EOPNOTSUPP;
  263. }
  264. return kvm_s390_inject_vcpu(vcpu, &irq);
  265. }
  266. /**
  267. * Handle MOVE PAGE partial execution interception.
  268. *
  269. * This interception can only happen for guests with DAT disabled and
  270. * addresses that are currently not mapped in the host. Thus we try to
  271. * set up the mappings for the corresponding user pages here (or throw
  272. * addressing exceptions in case of illegal guest addresses).
  273. */
  274. static int handle_mvpg_pei(struct kvm_vcpu *vcpu)
  275. {
  276. unsigned long srcaddr, dstaddr;
  277. int reg1, reg2, rc;
  278. kvm_s390_get_regs_rre(vcpu, &reg1, &reg2);
  279. /* Make sure that the source is paged-in */
  280. rc = guest_translate_address(vcpu, vcpu->run->s.regs.gprs[reg2],
  281. reg2, &srcaddr, 0);
  282. if (rc)
  283. return kvm_s390_inject_prog_cond(vcpu, rc);
  284. rc = kvm_arch_fault_in_page(vcpu, srcaddr, 0);
  285. if (rc != 0)
  286. return rc;
  287. /* Make sure that the destination is paged-in */
  288. rc = guest_translate_address(vcpu, vcpu->run->s.regs.gprs[reg1],
  289. reg1, &dstaddr, 1);
  290. if (rc)
  291. return kvm_s390_inject_prog_cond(vcpu, rc);
  292. rc = kvm_arch_fault_in_page(vcpu, dstaddr, 1);
  293. if (rc != 0)
  294. return rc;
  295. kvm_s390_rewind_psw(vcpu, 4);
  296. return 0;
  297. }
  298. static int handle_partial_execution(struct kvm_vcpu *vcpu)
  299. {
  300. if (vcpu->arch.sie_block->ipa == 0xb254) /* MVPG */
  301. return handle_mvpg_pei(vcpu);
  302. if (vcpu->arch.sie_block->ipa >> 8 == 0xae) /* SIGP */
  303. return kvm_s390_handle_sigp_pei(vcpu);
  304. return -EOPNOTSUPP;
  305. }
  306. static const intercept_handler_t intercept_funcs[] = {
  307. [0x00 >> 2] = handle_noop,
  308. [0x04 >> 2] = handle_instruction,
  309. [0x08 >> 2] = handle_prog,
  310. [0x0C >> 2] = handle_instruction_and_prog,
  311. [0x10 >> 2] = handle_noop,
  312. [0x14 >> 2] = handle_external_interrupt,
  313. [0x18 >> 2] = handle_noop,
  314. [0x1C >> 2] = kvm_s390_handle_wait,
  315. [0x20 >> 2] = handle_validity,
  316. [0x28 >> 2] = handle_stop,
  317. [0x38 >> 2] = handle_partial_execution,
  318. };
  319. int kvm_handle_sie_intercept(struct kvm_vcpu *vcpu)
  320. {
  321. intercept_handler_t func;
  322. u8 code = vcpu->arch.sie_block->icptcode;
  323. if (code & 3 || (code >> 2) >= ARRAY_SIZE(intercept_funcs))
  324. return -EOPNOTSUPP;
  325. func = intercept_funcs[code >> 2];
  326. if (func)
  327. return func(vcpu);
  328. return -EOPNOTSUPP;
  329. }