intercept.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * in-kernel handling for sie intercepts
  4. *
  5. * Copyright IBM Corp. 2008, 2020
  6. *
  7. * Author(s): Carsten Otte <cotte@de.ibm.com>
  8. * Christian Borntraeger <borntraeger@de.ibm.com>
  9. */
  10. #include <linux/kvm_host.h>
  11. #include <linux/errno.h>
  12. #include <linux/pagemap.h>
  13. #include <asm/asm-offsets.h>
  14. #include <asm/irq.h>
  15. #include <asm/sysinfo.h>
  16. #include <asm/uv.h>
  17. #include "kvm-s390.h"
  18. #include "gaccess.h"
  19. #include "trace.h"
  20. #include "trace-s390.h"
  21. #include "faultin.h"
  22. u8 kvm_s390_get_ilen(struct kvm_vcpu *vcpu)
  23. {
  24. struct kvm_s390_sie_block *sie_block = vcpu->arch.sie_block;
  25. u8 ilen = 0;
  26. switch (vcpu->arch.sie_block->icptcode) {
  27. case ICPT_INST:
  28. case ICPT_INSTPROGI:
  29. case ICPT_OPEREXC:
  30. case ICPT_PARTEXEC:
  31. case ICPT_IOINST:
  32. /* instruction only stored for these icptcodes */
  33. ilen = insn_length(vcpu->arch.sie_block->ipa >> 8);
  34. /* Use the length of the EXECUTE instruction if necessary */
  35. if (sie_block->icptstatus & 1) {
  36. ilen = (sie_block->icptstatus >> 4) & 0x6;
  37. if (!ilen)
  38. ilen = 4;
  39. }
  40. break;
  41. case ICPT_PROGI:
  42. /* bit 1+2 of pgmilc are the ilc, so we directly get ilen */
  43. ilen = vcpu->arch.sie_block->pgmilc & 0x6;
  44. break;
  45. }
  46. return ilen;
  47. }
  48. static int handle_stop(struct kvm_vcpu *vcpu)
  49. {
  50. struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
  51. int rc = 0;
  52. uint8_t flags, stop_pending;
  53. vcpu->stat.exit_stop_request++;
  54. /* delay the stop if any non-stop irq is pending */
  55. if (kvm_s390_vcpu_has_irq(vcpu, 1))
  56. return 0;
  57. /* avoid races with the injection/SIGP STOP code */
  58. spin_lock(&li->lock);
  59. flags = li->irq.stop.flags;
  60. stop_pending = kvm_s390_is_stop_irq_pending(vcpu);
  61. spin_unlock(&li->lock);
  62. trace_kvm_s390_stop_request(stop_pending, flags);
  63. if (!stop_pending)
  64. return 0;
  65. if (flags & KVM_S390_STOP_FLAG_STORE_STATUS) {
  66. rc = kvm_s390_vcpu_store_status(vcpu,
  67. KVM_S390_STORE_STATUS_NOADDR);
  68. if (rc)
  69. return rc;
  70. }
  71. /*
  72. * no need to check the return value of vcpu_stop as it can only have
  73. * an error for protvirt, but protvirt means user cpu state
  74. */
  75. if (!kvm_s390_user_cpu_state_ctrl(vcpu->kvm))
  76. kvm_s390_vcpu_stop(vcpu);
  77. return -EOPNOTSUPP;
  78. }
  79. static int handle_validity(struct kvm_vcpu *vcpu)
  80. {
  81. int viwhy = vcpu->arch.sie_block->ipb >> 16;
  82. vcpu->stat.exit_validity++;
  83. trace_kvm_s390_intercept_validity(vcpu, viwhy);
  84. KVM_EVENT(3, "validity intercept 0x%x for pid %u (kvm 0x%p)", viwhy,
  85. current->pid, vcpu->kvm);
  86. /* do not warn on invalid runtime instrumentation mode */
  87. WARN_ONCE(viwhy != 0x44, "kvm: unhandled validity intercept 0x%x\n",
  88. viwhy);
  89. return -EINVAL;
  90. }
  91. static int handle_instruction(struct kvm_vcpu *vcpu)
  92. {
  93. vcpu->stat.exit_instruction++;
  94. trace_kvm_s390_intercept_instruction(vcpu,
  95. vcpu->arch.sie_block->ipa,
  96. vcpu->arch.sie_block->ipb);
  97. switch (vcpu->arch.sie_block->ipa >> 8) {
  98. case 0x01:
  99. return kvm_s390_handle_01(vcpu);
  100. case 0x82:
  101. return kvm_s390_handle_lpsw(vcpu);
  102. case 0x83:
  103. return kvm_s390_handle_diag(vcpu);
  104. case 0xaa:
  105. return kvm_s390_handle_aa(vcpu);
  106. case 0xae:
  107. return kvm_s390_handle_sigp(vcpu);
  108. case 0xb2:
  109. return kvm_s390_handle_b2(vcpu);
  110. case 0xb6:
  111. return kvm_s390_handle_stctl(vcpu);
  112. case 0xb7:
  113. return kvm_s390_handle_lctl(vcpu);
  114. case 0xb9:
  115. return kvm_s390_handle_b9(vcpu);
  116. case 0xe3:
  117. return kvm_s390_handle_e3(vcpu);
  118. case 0xe5:
  119. return kvm_s390_handle_e5(vcpu);
  120. case 0xeb:
  121. return kvm_s390_handle_eb(vcpu);
  122. default:
  123. return -EOPNOTSUPP;
  124. }
  125. }
  126. static int inject_prog_on_prog_intercept(struct kvm_vcpu *vcpu)
  127. {
  128. struct kvm_s390_pgm_info pgm_info = {
  129. .code = vcpu->arch.sie_block->iprcc,
  130. /* the PSW has already been rewound */
  131. .flags = KVM_S390_PGM_FLAGS_NO_REWIND,
  132. };
  133. switch (vcpu->arch.sie_block->iprcc & ~PGM_PER) {
  134. case PGM_AFX_TRANSLATION:
  135. case PGM_ASX_TRANSLATION:
  136. case PGM_EX_TRANSLATION:
  137. case PGM_LFX_TRANSLATION:
  138. case PGM_LSTE_SEQUENCE:
  139. case PGM_LSX_TRANSLATION:
  140. case PGM_LX_TRANSLATION:
  141. case PGM_PRIMARY_AUTHORITY:
  142. case PGM_SECONDARY_AUTHORITY:
  143. case PGM_SPACE_SWITCH:
  144. pgm_info.trans_exc_code = vcpu->arch.sie_block->tecmc;
  145. break;
  146. case PGM_ALEN_TRANSLATION:
  147. case PGM_ALE_SEQUENCE:
  148. case PGM_ASTE_INSTANCE:
  149. case PGM_ASTE_SEQUENCE:
  150. case PGM_ASTE_VALIDITY:
  151. case PGM_EXTENDED_AUTHORITY:
  152. pgm_info.exc_access_id = vcpu->arch.sie_block->eai;
  153. break;
  154. case PGM_ASCE_TYPE:
  155. case PGM_PAGE_TRANSLATION:
  156. case PGM_REGION_FIRST_TRANS:
  157. case PGM_REGION_SECOND_TRANS:
  158. case PGM_REGION_THIRD_TRANS:
  159. case PGM_SEGMENT_TRANSLATION:
  160. pgm_info.trans_exc_code = vcpu->arch.sie_block->tecmc;
  161. pgm_info.exc_access_id = vcpu->arch.sie_block->eai;
  162. pgm_info.op_access_id = vcpu->arch.sie_block->oai;
  163. break;
  164. case PGM_MONITOR:
  165. pgm_info.mon_class_nr = vcpu->arch.sie_block->mcn;
  166. pgm_info.mon_code = vcpu->arch.sie_block->tecmc;
  167. break;
  168. case PGM_VECTOR_PROCESSING:
  169. case PGM_DATA:
  170. pgm_info.data_exc_code = vcpu->arch.sie_block->dxc;
  171. break;
  172. case PGM_PROTECTION:
  173. pgm_info.trans_exc_code = vcpu->arch.sie_block->tecmc;
  174. pgm_info.exc_access_id = vcpu->arch.sie_block->eai;
  175. break;
  176. default:
  177. break;
  178. }
  179. if (vcpu->arch.sie_block->iprcc & PGM_PER) {
  180. pgm_info.per_code = vcpu->arch.sie_block->perc;
  181. pgm_info.per_atmid = vcpu->arch.sie_block->peratmid;
  182. pgm_info.per_address = vcpu->arch.sie_block->peraddr;
  183. pgm_info.per_access_id = vcpu->arch.sie_block->peraid;
  184. }
  185. return kvm_s390_inject_prog_irq(vcpu, &pgm_info);
  186. }
  187. /*
  188. * restore ITDB to program-interruption TDB in guest lowcore
  189. * and set TX abort indication if required
  190. */
  191. static int handle_itdb(struct kvm_vcpu *vcpu)
  192. {
  193. struct kvm_s390_itdb *itdb;
  194. int rc;
  195. if (!IS_TE_ENABLED(vcpu) || !IS_ITDB_VALID(vcpu))
  196. return 0;
  197. if (current->thread.per_flags & PER_FLAG_NO_TE)
  198. return 0;
  199. itdb = phys_to_virt(vcpu->arch.sie_block->itdba);
  200. rc = write_guest_lc(vcpu, __LC_PGM_TDB, itdb, sizeof(*itdb));
  201. if (rc)
  202. return rc;
  203. memset(itdb, 0, sizeof(*itdb));
  204. return 0;
  205. }
  206. #define per_event(vcpu) (vcpu->arch.sie_block->iprcc & PGM_PER)
  207. static bool should_handle_per_event(const struct kvm_vcpu *vcpu)
  208. {
  209. if (!guestdbg_enabled(vcpu) || !per_event(vcpu))
  210. return false;
  211. if (guestdbg_sstep_enabled(vcpu) &&
  212. vcpu->arch.sie_block->iprcc != PGM_PER) {
  213. /*
  214. * __vcpu_run() will exit after delivering the concurrently
  215. * indicated condition.
  216. */
  217. return false;
  218. }
  219. return true;
  220. }
  221. static int handle_prog(struct kvm_vcpu *vcpu)
  222. {
  223. psw_t psw;
  224. int rc;
  225. vcpu->stat.exit_program_interruption++;
  226. /*
  227. * Intercept 8 indicates a loop of specification exceptions
  228. * for protected guests.
  229. */
  230. if (kvm_s390_pv_cpu_is_protected(vcpu))
  231. return -EOPNOTSUPP;
  232. if (should_handle_per_event(vcpu)) {
  233. rc = kvm_s390_handle_per_event(vcpu);
  234. if (rc)
  235. return rc;
  236. /* the interrupt might have been filtered out completely */
  237. if (vcpu->arch.sie_block->iprcc == 0)
  238. return 0;
  239. }
  240. trace_kvm_s390_intercept_prog(vcpu, vcpu->arch.sie_block->iprcc);
  241. if (vcpu->arch.sie_block->iprcc == PGM_SPECIFICATION) {
  242. rc = read_guest_lc(vcpu, __LC_PGM_NEW_PSW, &psw, sizeof(psw_t));
  243. if (rc)
  244. return rc;
  245. /* Avoid endless loops of specification exceptions */
  246. if (!is_valid_psw(&psw))
  247. return -EOPNOTSUPP;
  248. }
  249. rc = handle_itdb(vcpu);
  250. if (rc)
  251. return rc;
  252. return inject_prog_on_prog_intercept(vcpu);
  253. }
  254. /**
  255. * handle_external_interrupt - used for external interruption interceptions
  256. * @vcpu: virtual cpu
  257. *
  258. * This interception occurs if:
  259. * - the CPUSTAT_EXT_INT bit was already set when the external interrupt
  260. * occurred. In this case, the interrupt needs to be injected manually to
  261. * preserve interrupt priority.
  262. * - the external new PSW has external interrupts enabled, which will cause an
  263. * interruption loop. We drop to userspace in this case.
  264. *
  265. * The latter case can be detected by inspecting the external mask bit in the
  266. * external new psw.
  267. *
  268. * Under PV, only the latter case can occur, since interrupt priorities are
  269. * handled in the ultravisor.
  270. */
  271. static int handle_external_interrupt(struct kvm_vcpu *vcpu)
  272. {
  273. u16 eic = vcpu->arch.sie_block->eic;
  274. struct kvm_s390_irq irq;
  275. psw_t newpsw;
  276. int rc;
  277. vcpu->stat.exit_external_interrupt++;
  278. if (kvm_s390_pv_cpu_is_protected(vcpu)) {
  279. newpsw = vcpu->arch.sie_block->gpsw;
  280. } else {
  281. rc = read_guest_lc(vcpu, __LC_EXT_NEW_PSW, &newpsw, sizeof(psw_t));
  282. if (rc)
  283. return rc;
  284. }
  285. /*
  286. * Clock comparator or timer interrupt with external interrupt enabled
  287. * will cause interrupt loop. Drop to userspace.
  288. */
  289. if ((eic == EXT_IRQ_CLK_COMP || eic == EXT_IRQ_CPU_TIMER) &&
  290. (newpsw.mask & PSW_MASK_EXT))
  291. return -EOPNOTSUPP;
  292. switch (eic) {
  293. case EXT_IRQ_CLK_COMP:
  294. irq.type = KVM_S390_INT_CLOCK_COMP;
  295. break;
  296. case EXT_IRQ_CPU_TIMER:
  297. irq.type = KVM_S390_INT_CPU_TIMER;
  298. break;
  299. case EXT_IRQ_EXTERNAL_CALL:
  300. irq.type = KVM_S390_INT_EXTERNAL_CALL;
  301. irq.u.extcall.code = vcpu->arch.sie_block->extcpuaddr;
  302. rc = kvm_s390_inject_vcpu(vcpu, &irq);
  303. /* ignore if another external call is already pending */
  304. if (rc == -EBUSY)
  305. return 0;
  306. return rc;
  307. default:
  308. return -EOPNOTSUPP;
  309. }
  310. return kvm_s390_inject_vcpu(vcpu, &irq);
  311. }
  312. /**
  313. * handle_mvpg_pei - Handle MOVE PAGE partial execution interception.
  314. * @vcpu: virtual cpu
  315. *
  316. * This interception can only happen for guests with DAT disabled and
  317. * addresses that are currently not mapped in the host. Thus we try to
  318. * set up the mappings for the corresponding user pages here (or throw
  319. * addressing exceptions in case of illegal guest addresses).
  320. */
  321. static int handle_mvpg_pei(struct kvm_vcpu *vcpu)
  322. {
  323. unsigned long srcaddr, dstaddr;
  324. int reg1, reg2, rc;
  325. kvm_s390_get_regs_rre(vcpu, &reg1, &reg2);
  326. /* Ensure that the source is paged-in, no actual access -> no key checking */
  327. rc = guest_translate_address_with_key(vcpu, vcpu->run->s.regs.gprs[reg2],
  328. reg2, &srcaddr, GACC_FETCH, 0);
  329. if (rc)
  330. return kvm_s390_inject_prog_cond(vcpu, rc);
  331. do {
  332. rc = kvm_s390_faultin_gfn_simple(vcpu, NULL, gpa_to_gfn(srcaddr), false);
  333. } while (rc == -EAGAIN);
  334. if (rc)
  335. return rc;
  336. /* Ensure that the source is paged-in, no actual access -> no key checking */
  337. rc = guest_translate_address_with_key(vcpu, vcpu->run->s.regs.gprs[reg1],
  338. reg1, &dstaddr, GACC_STORE, 0);
  339. if (rc)
  340. return kvm_s390_inject_prog_cond(vcpu, rc);
  341. do {
  342. rc = kvm_s390_faultin_gfn_simple(vcpu, NULL, gpa_to_gfn(dstaddr), true);
  343. } while (rc == -EAGAIN);
  344. if (rc)
  345. return rc;
  346. kvm_s390_retry_instr(vcpu);
  347. return 0;
  348. }
  349. static int handle_partial_execution(struct kvm_vcpu *vcpu)
  350. {
  351. vcpu->stat.exit_pei++;
  352. if (vcpu->arch.sie_block->ipa == 0xb254) /* MVPG */
  353. return handle_mvpg_pei(vcpu);
  354. if (vcpu->arch.sie_block->ipa >> 8 == 0xae) /* SIGP */
  355. return kvm_s390_handle_sigp_pei(vcpu);
  356. return -EOPNOTSUPP;
  357. }
  358. /*
  359. * Handle the sthyi instruction that provides the guest with system
  360. * information, like current CPU resources available at each level of
  361. * the machine.
  362. */
  363. int handle_sthyi(struct kvm_vcpu *vcpu)
  364. {
  365. int reg1, reg2, cc = 0, r = 0;
  366. u64 code, addr, rc = 0;
  367. struct sthyi_sctns *sctns = NULL;
  368. if (!test_kvm_facility(vcpu->kvm, 74))
  369. return kvm_s390_inject_program_int(vcpu, PGM_OPERATION);
  370. kvm_s390_get_regs_rre(vcpu, &reg1, &reg2);
  371. code = vcpu->run->s.regs.gprs[reg1];
  372. addr = vcpu->run->s.regs.gprs[reg2];
  373. vcpu->stat.instruction_sthyi++;
  374. VCPU_EVENT(vcpu, 3, "STHYI: fc: %llu addr: 0x%016llx", code, addr);
  375. trace_kvm_s390_handle_sthyi(vcpu, code, addr);
  376. if (reg1 == reg2 || reg1 & 1 || reg2 & 1)
  377. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  378. if (code & 0xffff) {
  379. cc = 3;
  380. rc = 4;
  381. goto out;
  382. }
  383. if (!kvm_s390_pv_cpu_is_protected(vcpu) && (addr & ~PAGE_MASK))
  384. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  385. sctns = (void *)get_zeroed_page(GFP_KERNEL_ACCOUNT);
  386. if (!sctns)
  387. return -ENOMEM;
  388. cc = sthyi_fill(sctns, &rc);
  389. if (cc < 0) {
  390. free_page((unsigned long)sctns);
  391. return cc;
  392. }
  393. out:
  394. if (!cc) {
  395. if (kvm_s390_pv_cpu_is_protected(vcpu)) {
  396. memcpy(sida_addr(vcpu->arch.sie_block), sctns, PAGE_SIZE);
  397. } else {
  398. r = write_guest(vcpu, addr, reg2, sctns, PAGE_SIZE);
  399. if (r) {
  400. free_page((unsigned long)sctns);
  401. return kvm_s390_inject_prog_cond(vcpu, r);
  402. }
  403. }
  404. }
  405. free_page((unsigned long)sctns);
  406. vcpu->run->s.regs.gprs[reg2 + 1] = rc;
  407. kvm_s390_set_psw_cc(vcpu, cc);
  408. return r;
  409. }
  410. static int handle_operexc(struct kvm_vcpu *vcpu)
  411. {
  412. psw_t oldpsw, newpsw;
  413. int rc;
  414. vcpu->stat.exit_operation_exception++;
  415. trace_kvm_s390_handle_operexc(vcpu, vcpu->arch.sie_block->ipa,
  416. vcpu->arch.sie_block->ipb);
  417. if (vcpu->arch.sie_block->ipa == 0xb256)
  418. return handle_sthyi(vcpu);
  419. if (vcpu->kvm->arch.user_operexec)
  420. return -EOPNOTSUPP;
  421. if (vcpu->arch.sie_block->ipa == 0 && vcpu->kvm->arch.user_instr0)
  422. return -EOPNOTSUPP;
  423. rc = read_guest_lc(vcpu, __LC_PGM_NEW_PSW, &newpsw, sizeof(psw_t));
  424. if (rc)
  425. return rc;
  426. /*
  427. * Avoid endless loops of operation exceptions, if the pgm new
  428. * PSW will cause a new operation exception.
  429. * The heuristic checks if the pgm new psw is within 6 bytes before
  430. * the faulting psw address (with same DAT, AS settings) and the
  431. * new psw is not a wait psw and the fault was not triggered by
  432. * problem state.
  433. */
  434. oldpsw = vcpu->arch.sie_block->gpsw;
  435. if (oldpsw.addr - newpsw.addr <= 6 &&
  436. !(newpsw.mask & PSW_MASK_WAIT) &&
  437. !(oldpsw.mask & PSW_MASK_PSTATE) &&
  438. (newpsw.mask & PSW_MASK_ASC) == (oldpsw.mask & PSW_MASK_ASC) &&
  439. (newpsw.mask & PSW_MASK_DAT) == (oldpsw.mask & PSW_MASK_DAT))
  440. return -EOPNOTSUPP;
  441. return kvm_s390_inject_program_int(vcpu, PGM_OPERATION);
  442. }
  443. static int handle_pv_spx(struct kvm_vcpu *vcpu)
  444. {
  445. u32 pref = *(u32 *)sida_addr(vcpu->arch.sie_block);
  446. kvm_s390_set_prefix(vcpu, pref);
  447. trace_kvm_s390_handle_prefix(vcpu, 1, pref);
  448. return 0;
  449. }
  450. static int handle_pv_sclp(struct kvm_vcpu *vcpu)
  451. {
  452. struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
  453. spin_lock(&fi->lock);
  454. /*
  455. * 2 cases:
  456. * a: an sccb answering interrupt was already pending or in flight.
  457. * As the sccb value is not known we can simply set some value to
  458. * trigger delivery of a saved SCCB. UV will then use its saved
  459. * copy of the SCCB value.
  460. * b: an error SCCB interrupt needs to be injected so we also inject
  461. * a fake SCCB address. Firmware will use the proper one.
  462. * This makes sure, that both errors and real sccb returns will only
  463. * be delivered after a notification intercept (instruction has
  464. * finished) but not after others.
  465. */
  466. fi->srv_signal.ext_params |= 0x43000;
  467. set_bit(IRQ_PEND_EXT_SERVICE, &fi->pending_irqs);
  468. clear_bit(IRQ_PEND_EXT_SERVICE, &fi->masked_irqs);
  469. spin_unlock(&fi->lock);
  470. return 0;
  471. }
  472. static int handle_pv_uvc(struct kvm_vcpu *vcpu)
  473. {
  474. struct uv_cb_share *guest_uvcb = sida_addr(vcpu->arch.sie_block);
  475. struct uv_cb_cts uvcb = {
  476. .header.cmd = UVC_CMD_UNPIN_PAGE_SHARED,
  477. .header.len = sizeof(uvcb),
  478. .guest_handle = kvm_s390_pv_get_handle(vcpu->kvm),
  479. .gaddr = guest_uvcb->paddr,
  480. };
  481. int rc;
  482. if (guest_uvcb->header.cmd != UVC_CMD_REMOVE_SHARED_ACCESS) {
  483. WARN_ONCE(1, "Unexpected notification intercept for UVC 0x%x\n",
  484. guest_uvcb->header.cmd);
  485. return 0;
  486. }
  487. rc = kvm_s390_pv_make_secure(vcpu->kvm, uvcb.gaddr, &uvcb);
  488. /*
  489. * If the unpin did not succeed, the guest will exit again for the UVC
  490. * and we will retry the unpin.
  491. */
  492. if (rc == -EINVAL || rc == -ENXIO)
  493. return 0;
  494. /*
  495. * If we got -EAGAIN here, we simply return it. It will eventually
  496. * get propagated all the way to userspace, which should then try
  497. * again.
  498. */
  499. return rc;
  500. }
  501. static int handle_pv_notification(struct kvm_vcpu *vcpu)
  502. {
  503. int ret;
  504. if (vcpu->arch.sie_block->ipa == 0xb210)
  505. return handle_pv_spx(vcpu);
  506. if (vcpu->arch.sie_block->ipa == 0xb220)
  507. return handle_pv_sclp(vcpu);
  508. if (vcpu->arch.sie_block->ipa == 0xb9a4)
  509. return handle_pv_uvc(vcpu);
  510. if (vcpu->arch.sie_block->ipa >> 8 == 0xae) {
  511. /*
  512. * Besides external call, other SIGP orders also cause a
  513. * 108 (pv notify) intercept. In contrast to external call,
  514. * these orders need to be emulated and hence the appropriate
  515. * place to handle them is in handle_instruction().
  516. * So first try kvm_s390_handle_sigp_pei() and if that isn't
  517. * successful, go on with handle_instruction().
  518. */
  519. ret = kvm_s390_handle_sigp_pei(vcpu);
  520. if (!ret)
  521. return ret;
  522. }
  523. return handle_instruction(vcpu);
  524. }
  525. static bool should_handle_per_ifetch(const struct kvm_vcpu *vcpu, int rc)
  526. {
  527. /* Process PER, also if the instruction is processed in user space. */
  528. if (!(vcpu->arch.sie_block->icptstatus & 0x02))
  529. return false;
  530. if (rc != 0 && rc != -EOPNOTSUPP)
  531. return false;
  532. if (guestdbg_sstep_enabled(vcpu) && vcpu->arch.local_int.pending_irqs)
  533. /* __vcpu_run() will exit after delivering the interrupt. */
  534. return false;
  535. return true;
  536. }
  537. int kvm_handle_sie_intercept(struct kvm_vcpu *vcpu)
  538. {
  539. int rc, per_rc = 0;
  540. if (kvm_is_ucontrol(vcpu->kvm))
  541. return -EOPNOTSUPP;
  542. switch (vcpu->arch.sie_block->icptcode) {
  543. case ICPT_EXTREQ:
  544. vcpu->stat.exit_external_request++;
  545. return 0;
  546. case ICPT_IOREQ:
  547. vcpu->stat.exit_io_request++;
  548. return 0;
  549. case ICPT_INST:
  550. rc = handle_instruction(vcpu);
  551. break;
  552. case ICPT_PROGI:
  553. return handle_prog(vcpu);
  554. case ICPT_EXTINT:
  555. return handle_external_interrupt(vcpu);
  556. case ICPT_WAIT:
  557. return kvm_s390_handle_wait(vcpu);
  558. case ICPT_VALIDITY:
  559. return handle_validity(vcpu);
  560. case ICPT_STOP:
  561. return handle_stop(vcpu);
  562. case ICPT_OPEREXC:
  563. rc = handle_operexc(vcpu);
  564. break;
  565. case ICPT_PARTEXEC:
  566. rc = handle_partial_execution(vcpu);
  567. break;
  568. case ICPT_KSS:
  569. /* Instruction will be redriven, skip the PER check. */
  570. return kvm_s390_skey_check_enable(vcpu);
  571. case ICPT_MCHKREQ:
  572. case ICPT_INT_ENABLE:
  573. /*
  574. * PSW bit 13 or a CR (0, 6, 14) changed and we might
  575. * now be able to deliver interrupts. The pre-run code
  576. * will take care of this.
  577. */
  578. rc = 0;
  579. break;
  580. case ICPT_PV_INSTR:
  581. rc = handle_instruction(vcpu);
  582. break;
  583. case ICPT_PV_NOTIFY:
  584. rc = handle_pv_notification(vcpu);
  585. break;
  586. case ICPT_PV_PREF:
  587. rc = 0;
  588. kvm_s390_pv_convert_to_secure(vcpu->kvm, kvm_s390_get_prefix(vcpu));
  589. kvm_s390_pv_convert_to_secure(vcpu->kvm, kvm_s390_get_prefix(vcpu) + PAGE_SIZE);
  590. break;
  591. default:
  592. return -EOPNOTSUPP;
  593. }
  594. if (should_handle_per_ifetch(vcpu, rc))
  595. per_rc = kvm_s390_handle_per_ifetch_icpt(vcpu);
  596. return per_rc ? per_rc : rc;
  597. }