intercept.c 9.2 KB

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