intercept.c 9.5 KB

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