priv.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060
  1. /*
  2. * handling privileged instructions
  3. *
  4. * Copyright IBM Corp. 2008, 2013
  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.h>
  14. #include <linux/gfp.h>
  15. #include <linux/errno.h>
  16. #include <linux/compat.h>
  17. #include <asm/asm-offsets.h>
  18. #include <asm/facility.h>
  19. #include <asm/current.h>
  20. #include <asm/debug.h>
  21. #include <asm/ebcdic.h>
  22. #include <asm/sysinfo.h>
  23. #include <asm/pgtable.h>
  24. #include <asm/pgalloc.h>
  25. #include <asm/io.h>
  26. #include <asm/ptrace.h>
  27. #include <asm/compat.h>
  28. #include "gaccess.h"
  29. #include "kvm-s390.h"
  30. #include "trace.h"
  31. /* Handle SCK (SET CLOCK) interception */
  32. static int handle_set_clock(struct kvm_vcpu *vcpu)
  33. {
  34. struct kvm_vcpu *cpup;
  35. s64 hostclk, val;
  36. int i, rc;
  37. ar_t ar;
  38. u64 op2;
  39. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
  40. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  41. op2 = kvm_s390_get_base_disp_s(vcpu, &ar);
  42. if (op2 & 7) /* Operand must be on a doubleword boundary */
  43. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  44. rc = read_guest(vcpu, op2, ar, &val, sizeof(val));
  45. if (rc)
  46. return kvm_s390_inject_prog_cond(vcpu, rc);
  47. if (store_tod_clock(&hostclk)) {
  48. kvm_s390_set_psw_cc(vcpu, 3);
  49. return 0;
  50. }
  51. val = (val - hostclk) & ~0x3fUL;
  52. mutex_lock(&vcpu->kvm->lock);
  53. kvm_for_each_vcpu(i, cpup, vcpu->kvm)
  54. cpup->arch.sie_block->epoch = val;
  55. mutex_unlock(&vcpu->kvm->lock);
  56. kvm_s390_set_psw_cc(vcpu, 0);
  57. return 0;
  58. }
  59. static int handle_set_prefix(struct kvm_vcpu *vcpu)
  60. {
  61. u64 operand2;
  62. u32 address;
  63. int rc;
  64. ar_t ar;
  65. vcpu->stat.instruction_spx++;
  66. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
  67. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  68. operand2 = kvm_s390_get_base_disp_s(vcpu, &ar);
  69. /* must be word boundary */
  70. if (operand2 & 3)
  71. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  72. /* get the value */
  73. rc = read_guest(vcpu, operand2, ar, &address, sizeof(address));
  74. if (rc)
  75. return kvm_s390_inject_prog_cond(vcpu, rc);
  76. address &= 0x7fffe000u;
  77. /*
  78. * Make sure the new value is valid memory. We only need to check the
  79. * first page, since address is 8k aligned and memory pieces are always
  80. * at least 1MB aligned and have at least a size of 1MB.
  81. */
  82. if (kvm_is_error_gpa(vcpu->kvm, address))
  83. return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
  84. kvm_s390_set_prefix(vcpu, address);
  85. VCPU_EVENT(vcpu, 5, "setting prefix to %x", address);
  86. trace_kvm_s390_handle_prefix(vcpu, 1, address);
  87. return 0;
  88. }
  89. static int handle_store_prefix(struct kvm_vcpu *vcpu)
  90. {
  91. u64 operand2;
  92. u32 address;
  93. int rc;
  94. ar_t ar;
  95. vcpu->stat.instruction_stpx++;
  96. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
  97. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  98. operand2 = kvm_s390_get_base_disp_s(vcpu, &ar);
  99. /* must be word boundary */
  100. if (operand2 & 3)
  101. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  102. address = kvm_s390_get_prefix(vcpu);
  103. /* get the value */
  104. rc = write_guest(vcpu, operand2, ar, &address, sizeof(address));
  105. if (rc)
  106. return kvm_s390_inject_prog_cond(vcpu, rc);
  107. VCPU_EVENT(vcpu, 5, "storing prefix to %x", address);
  108. trace_kvm_s390_handle_prefix(vcpu, 0, address);
  109. return 0;
  110. }
  111. static int handle_store_cpu_address(struct kvm_vcpu *vcpu)
  112. {
  113. u16 vcpu_id = vcpu->vcpu_id;
  114. u64 ga;
  115. int rc;
  116. ar_t ar;
  117. vcpu->stat.instruction_stap++;
  118. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
  119. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  120. ga = kvm_s390_get_base_disp_s(vcpu, &ar);
  121. if (ga & 1)
  122. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  123. rc = write_guest(vcpu, ga, ar, &vcpu_id, sizeof(vcpu_id));
  124. if (rc)
  125. return kvm_s390_inject_prog_cond(vcpu, rc);
  126. VCPU_EVENT(vcpu, 5, "storing cpu address to %llx", ga);
  127. trace_kvm_s390_handle_stap(vcpu, ga);
  128. return 0;
  129. }
  130. static int __skey_check_enable(struct kvm_vcpu *vcpu)
  131. {
  132. int rc = 0;
  133. if (!(vcpu->arch.sie_block->ictl & (ICTL_ISKE | ICTL_SSKE | ICTL_RRBE)))
  134. return rc;
  135. rc = s390_enable_skey();
  136. trace_kvm_s390_skey_related_inst(vcpu);
  137. vcpu->arch.sie_block->ictl &= ~(ICTL_ISKE | ICTL_SSKE | ICTL_RRBE);
  138. return rc;
  139. }
  140. static int handle_skey(struct kvm_vcpu *vcpu)
  141. {
  142. int rc = __skey_check_enable(vcpu);
  143. if (rc)
  144. return rc;
  145. vcpu->stat.instruction_storage_key++;
  146. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
  147. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  148. kvm_s390_rewind_psw(vcpu, 4);
  149. VCPU_EVENT(vcpu, 4, "%s", "retrying storage key operation");
  150. return 0;
  151. }
  152. static int handle_ipte_interlock(struct kvm_vcpu *vcpu)
  153. {
  154. vcpu->stat.instruction_ipte_interlock++;
  155. if (psw_bits(vcpu->arch.sie_block->gpsw).p)
  156. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  157. wait_event(vcpu->kvm->arch.ipte_wq, !ipte_lock_held(vcpu));
  158. kvm_s390_rewind_psw(vcpu, 4);
  159. VCPU_EVENT(vcpu, 4, "%s", "retrying ipte interlock operation");
  160. return 0;
  161. }
  162. static int handle_test_block(struct kvm_vcpu *vcpu)
  163. {
  164. gpa_t addr;
  165. int reg2;
  166. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
  167. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  168. kvm_s390_get_regs_rre(vcpu, NULL, &reg2);
  169. addr = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK;
  170. addr = kvm_s390_logical_to_effective(vcpu, addr);
  171. if (kvm_s390_check_low_addr_prot_real(vcpu, addr))
  172. return kvm_s390_inject_prog_irq(vcpu, &vcpu->arch.pgm);
  173. addr = kvm_s390_real_to_abs(vcpu, addr);
  174. if (kvm_is_error_gpa(vcpu->kvm, addr))
  175. return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
  176. /*
  177. * We don't expect errors on modern systems, and do not care
  178. * about storage keys (yet), so let's just clear the page.
  179. */
  180. if (kvm_clear_guest(vcpu->kvm, addr, PAGE_SIZE))
  181. return -EFAULT;
  182. kvm_s390_set_psw_cc(vcpu, 0);
  183. vcpu->run->s.regs.gprs[0] = 0;
  184. return 0;
  185. }
  186. static int handle_tpi(struct kvm_vcpu *vcpu)
  187. {
  188. struct kvm_s390_interrupt_info *inti;
  189. unsigned long len;
  190. u32 tpi_data[3];
  191. int rc;
  192. u64 addr;
  193. ar_t ar;
  194. addr = kvm_s390_get_base_disp_s(vcpu, &ar);
  195. if (addr & 3)
  196. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  197. inti = kvm_s390_get_io_int(vcpu->kvm, vcpu->arch.sie_block->gcr[6], 0);
  198. if (!inti) {
  199. kvm_s390_set_psw_cc(vcpu, 0);
  200. return 0;
  201. }
  202. tpi_data[0] = inti->io.subchannel_id << 16 | inti->io.subchannel_nr;
  203. tpi_data[1] = inti->io.io_int_parm;
  204. tpi_data[2] = inti->io.io_int_word;
  205. if (addr) {
  206. /*
  207. * Store the two-word I/O interruption code into the
  208. * provided area.
  209. */
  210. len = sizeof(tpi_data) - 4;
  211. rc = write_guest(vcpu, addr, ar, &tpi_data, len);
  212. if (rc) {
  213. rc = kvm_s390_inject_prog_cond(vcpu, rc);
  214. goto reinject_interrupt;
  215. }
  216. } else {
  217. /*
  218. * Store the three-word I/O interruption code into
  219. * the appropriate lowcore area.
  220. */
  221. len = sizeof(tpi_data);
  222. if (write_guest_lc(vcpu, __LC_SUBCHANNEL_ID, &tpi_data, len)) {
  223. /* failed writes to the low core are not recoverable */
  224. rc = -EFAULT;
  225. goto reinject_interrupt;
  226. }
  227. }
  228. /* irq was successfully handed to the guest */
  229. kfree(inti);
  230. kvm_s390_set_psw_cc(vcpu, 1);
  231. return 0;
  232. reinject_interrupt:
  233. /*
  234. * If we encounter a problem storing the interruption code, the
  235. * instruction is suppressed from the guest's view: reinject the
  236. * interrupt.
  237. */
  238. if (kvm_s390_reinject_io_int(vcpu->kvm, inti)) {
  239. kfree(inti);
  240. rc = -EFAULT;
  241. }
  242. /* don't set the cc, a pgm irq was injected or we drop to user space */
  243. return rc ? -EFAULT : 0;
  244. }
  245. static int handle_tsch(struct kvm_vcpu *vcpu)
  246. {
  247. struct kvm_s390_interrupt_info *inti = NULL;
  248. const u64 isc_mask = 0xffUL << 24; /* all iscs set */
  249. /* a valid schid has at least one bit set */
  250. if (vcpu->run->s.regs.gprs[1])
  251. inti = kvm_s390_get_io_int(vcpu->kvm, isc_mask,
  252. vcpu->run->s.regs.gprs[1]);
  253. /*
  254. * Prepare exit to userspace.
  255. * We indicate whether we dequeued a pending I/O interrupt
  256. * so that userspace can re-inject it if the instruction gets
  257. * a program check. While this may re-order the pending I/O
  258. * interrupts, this is no problem since the priority is kept
  259. * intact.
  260. */
  261. vcpu->run->exit_reason = KVM_EXIT_S390_TSCH;
  262. vcpu->run->s390_tsch.dequeued = !!inti;
  263. if (inti) {
  264. vcpu->run->s390_tsch.subchannel_id = inti->io.subchannel_id;
  265. vcpu->run->s390_tsch.subchannel_nr = inti->io.subchannel_nr;
  266. vcpu->run->s390_tsch.io_int_parm = inti->io.io_int_parm;
  267. vcpu->run->s390_tsch.io_int_word = inti->io.io_int_word;
  268. }
  269. vcpu->run->s390_tsch.ipb = vcpu->arch.sie_block->ipb;
  270. kfree(inti);
  271. return -EREMOTE;
  272. }
  273. static int handle_io_inst(struct kvm_vcpu *vcpu)
  274. {
  275. VCPU_EVENT(vcpu, 4, "%s", "I/O instruction");
  276. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
  277. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  278. if (vcpu->kvm->arch.css_support) {
  279. /*
  280. * Most I/O instructions will be handled by userspace.
  281. * Exceptions are tpi and the interrupt portion of tsch.
  282. */
  283. if (vcpu->arch.sie_block->ipa == 0xb236)
  284. return handle_tpi(vcpu);
  285. if (vcpu->arch.sie_block->ipa == 0xb235)
  286. return handle_tsch(vcpu);
  287. /* Handle in userspace. */
  288. return -EOPNOTSUPP;
  289. } else {
  290. /*
  291. * Set condition code 3 to stop the guest from issuing channel
  292. * I/O instructions.
  293. */
  294. kvm_s390_set_psw_cc(vcpu, 3);
  295. return 0;
  296. }
  297. }
  298. static int handle_stfl(struct kvm_vcpu *vcpu)
  299. {
  300. int rc;
  301. unsigned int fac;
  302. vcpu->stat.instruction_stfl++;
  303. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
  304. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  305. /*
  306. * We need to shift the lower 32 facility bits (bit 0-31) from a u64
  307. * into a u32 memory representation. They will remain bits 0-31.
  308. */
  309. fac = *vcpu->kvm->arch.model.fac->list >> 32;
  310. rc = write_guest_lc(vcpu, offsetof(struct _lowcore, stfl_fac_list),
  311. &fac, sizeof(fac));
  312. if (rc)
  313. return rc;
  314. VCPU_EVENT(vcpu, 5, "store facility list value %x", fac);
  315. trace_kvm_s390_handle_stfl(vcpu, fac);
  316. return 0;
  317. }
  318. #define PSW_MASK_ADDR_MODE (PSW_MASK_EA | PSW_MASK_BA)
  319. #define PSW_MASK_UNASSIGNED 0xb80800fe7fffffffUL
  320. #define PSW_ADDR_24 0x0000000000ffffffUL
  321. #define PSW_ADDR_31 0x000000007fffffffUL
  322. int is_valid_psw(psw_t *psw)
  323. {
  324. if (psw->mask & PSW_MASK_UNASSIGNED)
  325. return 0;
  326. if ((psw->mask & PSW_MASK_ADDR_MODE) == PSW_MASK_BA) {
  327. if (psw->addr & ~PSW_ADDR_31)
  328. return 0;
  329. }
  330. if (!(psw->mask & PSW_MASK_ADDR_MODE) && (psw->addr & ~PSW_ADDR_24))
  331. return 0;
  332. if ((psw->mask & PSW_MASK_ADDR_MODE) == PSW_MASK_EA)
  333. return 0;
  334. if (psw->addr & 1)
  335. return 0;
  336. return 1;
  337. }
  338. int kvm_s390_handle_lpsw(struct kvm_vcpu *vcpu)
  339. {
  340. psw_t *gpsw = &vcpu->arch.sie_block->gpsw;
  341. psw_compat_t new_psw;
  342. u64 addr;
  343. int rc;
  344. ar_t ar;
  345. if (gpsw->mask & PSW_MASK_PSTATE)
  346. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  347. addr = kvm_s390_get_base_disp_s(vcpu, &ar);
  348. if (addr & 7)
  349. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  350. rc = read_guest(vcpu, addr, ar, &new_psw, sizeof(new_psw));
  351. if (rc)
  352. return kvm_s390_inject_prog_cond(vcpu, rc);
  353. if (!(new_psw.mask & PSW32_MASK_BASE))
  354. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  355. gpsw->mask = (new_psw.mask & ~PSW32_MASK_BASE) << 32;
  356. gpsw->mask |= new_psw.addr & PSW32_ADDR_AMODE;
  357. gpsw->addr = new_psw.addr & ~PSW32_ADDR_AMODE;
  358. if (!is_valid_psw(gpsw))
  359. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  360. return 0;
  361. }
  362. static int handle_lpswe(struct kvm_vcpu *vcpu)
  363. {
  364. psw_t new_psw;
  365. u64 addr;
  366. int rc;
  367. ar_t ar;
  368. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
  369. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  370. addr = kvm_s390_get_base_disp_s(vcpu, &ar);
  371. if (addr & 7)
  372. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  373. rc = read_guest(vcpu, addr, ar, &new_psw, sizeof(new_psw));
  374. if (rc)
  375. return kvm_s390_inject_prog_cond(vcpu, rc);
  376. vcpu->arch.sie_block->gpsw = new_psw;
  377. if (!is_valid_psw(&vcpu->arch.sie_block->gpsw))
  378. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  379. return 0;
  380. }
  381. static int handle_stidp(struct kvm_vcpu *vcpu)
  382. {
  383. u64 stidp_data = vcpu->arch.stidp_data;
  384. u64 operand2;
  385. int rc;
  386. ar_t ar;
  387. vcpu->stat.instruction_stidp++;
  388. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
  389. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  390. operand2 = kvm_s390_get_base_disp_s(vcpu, &ar);
  391. if (operand2 & 7)
  392. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  393. rc = write_guest(vcpu, operand2, ar, &stidp_data, sizeof(stidp_data));
  394. if (rc)
  395. return kvm_s390_inject_prog_cond(vcpu, rc);
  396. VCPU_EVENT(vcpu, 5, "%s", "store cpu id");
  397. return 0;
  398. }
  399. static void handle_stsi_3_2_2(struct kvm_vcpu *vcpu, struct sysinfo_3_2_2 *mem)
  400. {
  401. int cpus = 0;
  402. int n;
  403. cpus = atomic_read(&vcpu->kvm->online_vcpus);
  404. /* deal with other level 3 hypervisors */
  405. if (stsi(mem, 3, 2, 2))
  406. mem->count = 0;
  407. if (mem->count < 8)
  408. mem->count++;
  409. for (n = mem->count - 1; n > 0 ; n--)
  410. memcpy(&mem->vm[n], &mem->vm[n - 1], sizeof(mem->vm[0]));
  411. memset(&mem->vm[0], 0, sizeof(mem->vm[0]));
  412. mem->vm[0].cpus_total = cpus;
  413. mem->vm[0].cpus_configured = cpus;
  414. mem->vm[0].cpus_standby = 0;
  415. mem->vm[0].cpus_reserved = 0;
  416. mem->vm[0].caf = 1000;
  417. memcpy(mem->vm[0].name, "KVMguest", 8);
  418. ASCEBC(mem->vm[0].name, 8);
  419. memcpy(mem->vm[0].cpi, "KVM/Linux ", 16);
  420. ASCEBC(mem->vm[0].cpi, 16);
  421. }
  422. static void insert_stsi_usr_data(struct kvm_vcpu *vcpu, u64 addr, ar_t ar,
  423. u8 fc, u8 sel1, u16 sel2)
  424. {
  425. vcpu->run->exit_reason = KVM_EXIT_S390_STSI;
  426. vcpu->run->s390_stsi.addr = addr;
  427. vcpu->run->s390_stsi.ar = ar;
  428. vcpu->run->s390_stsi.fc = fc;
  429. vcpu->run->s390_stsi.sel1 = sel1;
  430. vcpu->run->s390_stsi.sel2 = sel2;
  431. }
  432. static int handle_stsi(struct kvm_vcpu *vcpu)
  433. {
  434. int fc = (vcpu->run->s.regs.gprs[0] & 0xf0000000) >> 28;
  435. int sel1 = vcpu->run->s.regs.gprs[0] & 0xff;
  436. int sel2 = vcpu->run->s.regs.gprs[1] & 0xffff;
  437. unsigned long mem = 0;
  438. u64 operand2;
  439. int rc = 0;
  440. ar_t ar;
  441. vcpu->stat.instruction_stsi++;
  442. VCPU_EVENT(vcpu, 4, "stsi: fc: %x sel1: %x sel2: %x", fc, sel1, sel2);
  443. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
  444. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  445. if (fc > 3) {
  446. kvm_s390_set_psw_cc(vcpu, 3);
  447. return 0;
  448. }
  449. if (vcpu->run->s.regs.gprs[0] & 0x0fffff00
  450. || vcpu->run->s.regs.gprs[1] & 0xffff0000)
  451. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  452. if (fc == 0) {
  453. vcpu->run->s.regs.gprs[0] = 3 << 28;
  454. kvm_s390_set_psw_cc(vcpu, 0);
  455. return 0;
  456. }
  457. operand2 = kvm_s390_get_base_disp_s(vcpu, &ar);
  458. if (operand2 & 0xfff)
  459. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  460. switch (fc) {
  461. case 1: /* same handling for 1 and 2 */
  462. case 2:
  463. mem = get_zeroed_page(GFP_KERNEL);
  464. if (!mem)
  465. goto out_no_data;
  466. if (stsi((void *) mem, fc, sel1, sel2))
  467. goto out_no_data;
  468. break;
  469. case 3:
  470. if (sel1 != 2 || sel2 != 2)
  471. goto out_no_data;
  472. mem = get_zeroed_page(GFP_KERNEL);
  473. if (!mem)
  474. goto out_no_data;
  475. handle_stsi_3_2_2(vcpu, (void *) mem);
  476. break;
  477. }
  478. rc = write_guest(vcpu, operand2, ar, (void *)mem, PAGE_SIZE);
  479. if (rc) {
  480. rc = kvm_s390_inject_prog_cond(vcpu, rc);
  481. goto out;
  482. }
  483. if (vcpu->kvm->arch.user_stsi) {
  484. insert_stsi_usr_data(vcpu, operand2, ar, fc, sel1, sel2);
  485. rc = -EREMOTE;
  486. }
  487. trace_kvm_s390_handle_stsi(vcpu, fc, sel1, sel2, operand2);
  488. free_page(mem);
  489. kvm_s390_set_psw_cc(vcpu, 0);
  490. vcpu->run->s.regs.gprs[0] = 0;
  491. return rc;
  492. out_no_data:
  493. kvm_s390_set_psw_cc(vcpu, 3);
  494. out:
  495. free_page(mem);
  496. return rc;
  497. }
  498. static const intercept_handler_t b2_handlers[256] = {
  499. [0x02] = handle_stidp,
  500. [0x04] = handle_set_clock,
  501. [0x10] = handle_set_prefix,
  502. [0x11] = handle_store_prefix,
  503. [0x12] = handle_store_cpu_address,
  504. [0x21] = handle_ipte_interlock,
  505. [0x29] = handle_skey,
  506. [0x2a] = handle_skey,
  507. [0x2b] = handle_skey,
  508. [0x2c] = handle_test_block,
  509. [0x30] = handle_io_inst,
  510. [0x31] = handle_io_inst,
  511. [0x32] = handle_io_inst,
  512. [0x33] = handle_io_inst,
  513. [0x34] = handle_io_inst,
  514. [0x35] = handle_io_inst,
  515. [0x36] = handle_io_inst,
  516. [0x37] = handle_io_inst,
  517. [0x38] = handle_io_inst,
  518. [0x39] = handle_io_inst,
  519. [0x3a] = handle_io_inst,
  520. [0x3b] = handle_io_inst,
  521. [0x3c] = handle_io_inst,
  522. [0x50] = handle_ipte_interlock,
  523. [0x5f] = handle_io_inst,
  524. [0x74] = handle_io_inst,
  525. [0x76] = handle_io_inst,
  526. [0x7d] = handle_stsi,
  527. [0xb1] = handle_stfl,
  528. [0xb2] = handle_lpswe,
  529. };
  530. int kvm_s390_handle_b2(struct kvm_vcpu *vcpu)
  531. {
  532. intercept_handler_t handler;
  533. /*
  534. * A lot of B2 instructions are priviledged. Here we check for
  535. * the privileged ones, that we can handle in the kernel.
  536. * Anything else goes to userspace.
  537. */
  538. handler = b2_handlers[vcpu->arch.sie_block->ipa & 0x00ff];
  539. if (handler)
  540. return handler(vcpu);
  541. return -EOPNOTSUPP;
  542. }
  543. static int handle_epsw(struct kvm_vcpu *vcpu)
  544. {
  545. int reg1, reg2;
  546. kvm_s390_get_regs_rre(vcpu, &reg1, &reg2);
  547. /* This basically extracts the mask half of the psw. */
  548. vcpu->run->s.regs.gprs[reg1] &= 0xffffffff00000000UL;
  549. vcpu->run->s.regs.gprs[reg1] |= vcpu->arch.sie_block->gpsw.mask >> 32;
  550. if (reg2) {
  551. vcpu->run->s.regs.gprs[reg2] &= 0xffffffff00000000UL;
  552. vcpu->run->s.regs.gprs[reg2] |=
  553. vcpu->arch.sie_block->gpsw.mask & 0x00000000ffffffffUL;
  554. }
  555. return 0;
  556. }
  557. #define PFMF_RESERVED 0xfffc0101UL
  558. #define PFMF_SK 0x00020000UL
  559. #define PFMF_CF 0x00010000UL
  560. #define PFMF_UI 0x00008000UL
  561. #define PFMF_FSC 0x00007000UL
  562. #define PFMF_NQ 0x00000800UL
  563. #define PFMF_MR 0x00000400UL
  564. #define PFMF_MC 0x00000200UL
  565. #define PFMF_KEY 0x000000feUL
  566. static int handle_pfmf(struct kvm_vcpu *vcpu)
  567. {
  568. int reg1, reg2;
  569. unsigned long start, end;
  570. vcpu->stat.instruction_pfmf++;
  571. kvm_s390_get_regs_rre(vcpu, &reg1, &reg2);
  572. if (!MACHINE_HAS_PFMF)
  573. return kvm_s390_inject_program_int(vcpu, PGM_OPERATION);
  574. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
  575. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  576. if (vcpu->run->s.regs.gprs[reg1] & PFMF_RESERVED)
  577. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  578. /* Only provide non-quiescing support if the host supports it */
  579. if (vcpu->run->s.regs.gprs[reg1] & PFMF_NQ && !test_facility(14))
  580. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  581. /* No support for conditional-SSKE */
  582. if (vcpu->run->s.regs.gprs[reg1] & (PFMF_MR | PFMF_MC))
  583. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  584. start = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK;
  585. start = kvm_s390_logical_to_effective(vcpu, start);
  586. switch (vcpu->run->s.regs.gprs[reg1] & PFMF_FSC) {
  587. case 0x00000000:
  588. end = (start + (1UL << 12)) & ~((1UL << 12) - 1);
  589. break;
  590. case 0x00001000:
  591. end = (start + (1UL << 20)) & ~((1UL << 20) - 1);
  592. break;
  593. /* We dont support EDAT2
  594. case 0x00002000:
  595. end = (start + (1UL << 31)) & ~((1UL << 31) - 1);
  596. break;*/
  597. default:
  598. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  599. }
  600. if (vcpu->run->s.regs.gprs[reg1] & PFMF_CF) {
  601. if (kvm_s390_check_low_addr_prot_real(vcpu, start))
  602. return kvm_s390_inject_prog_irq(vcpu, &vcpu->arch.pgm);
  603. }
  604. while (start < end) {
  605. unsigned long useraddr, abs_addr;
  606. /* Translate guest address to host address */
  607. if ((vcpu->run->s.regs.gprs[reg1] & PFMF_FSC) == 0)
  608. abs_addr = kvm_s390_real_to_abs(vcpu, start);
  609. else
  610. abs_addr = start;
  611. useraddr = gfn_to_hva(vcpu->kvm, gpa_to_gfn(abs_addr));
  612. if (kvm_is_error_hva(useraddr))
  613. return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
  614. if (vcpu->run->s.regs.gprs[reg1] & PFMF_CF) {
  615. if (clear_user((void __user *)useraddr, PAGE_SIZE))
  616. return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
  617. }
  618. if (vcpu->run->s.regs.gprs[reg1] & PFMF_SK) {
  619. int rc = __skey_check_enable(vcpu);
  620. if (rc)
  621. return rc;
  622. if (set_guest_storage_key(current->mm, useraddr,
  623. vcpu->run->s.regs.gprs[reg1] & PFMF_KEY,
  624. vcpu->run->s.regs.gprs[reg1] & PFMF_NQ))
  625. return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
  626. }
  627. start += PAGE_SIZE;
  628. }
  629. if (vcpu->run->s.regs.gprs[reg1] & PFMF_FSC)
  630. vcpu->run->s.regs.gprs[reg2] = end;
  631. return 0;
  632. }
  633. static int handle_essa(struct kvm_vcpu *vcpu)
  634. {
  635. /* entries expected to be 1FF */
  636. int entries = (vcpu->arch.sie_block->cbrlo & ~PAGE_MASK) >> 3;
  637. unsigned long *cbrlo, cbrle;
  638. struct gmap *gmap;
  639. int i;
  640. VCPU_EVENT(vcpu, 5, "cmma release %d pages", entries);
  641. gmap = vcpu->arch.gmap;
  642. vcpu->stat.instruction_essa++;
  643. if (!kvm_s390_cmma_enabled(vcpu->kvm))
  644. return kvm_s390_inject_program_int(vcpu, PGM_OPERATION);
  645. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
  646. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  647. if (((vcpu->arch.sie_block->ipb & 0xf0000000) >> 28) > 6)
  648. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  649. /* Rewind PSW to repeat the ESSA instruction */
  650. kvm_s390_rewind_psw(vcpu, 4);
  651. vcpu->arch.sie_block->cbrlo &= PAGE_MASK; /* reset nceo */
  652. cbrlo = phys_to_virt(vcpu->arch.sie_block->cbrlo);
  653. down_read(&gmap->mm->mmap_sem);
  654. for (i = 0; i < entries; ++i) {
  655. cbrle = cbrlo[i];
  656. if (unlikely(cbrle & ~PAGE_MASK || cbrle < 2 * PAGE_SIZE))
  657. /* invalid entry */
  658. break;
  659. /* try to free backing */
  660. __gmap_zap(gmap, cbrle);
  661. }
  662. up_read(&gmap->mm->mmap_sem);
  663. if (i < entries)
  664. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  665. return 0;
  666. }
  667. static const intercept_handler_t b9_handlers[256] = {
  668. [0x8a] = handle_ipte_interlock,
  669. [0x8d] = handle_epsw,
  670. [0x8e] = handle_ipte_interlock,
  671. [0x8f] = handle_ipte_interlock,
  672. [0xab] = handle_essa,
  673. [0xaf] = handle_pfmf,
  674. };
  675. int kvm_s390_handle_b9(struct kvm_vcpu *vcpu)
  676. {
  677. intercept_handler_t handler;
  678. /* This is handled just as for the B2 instructions. */
  679. handler = b9_handlers[vcpu->arch.sie_block->ipa & 0x00ff];
  680. if (handler)
  681. return handler(vcpu);
  682. return -EOPNOTSUPP;
  683. }
  684. int kvm_s390_handle_lctl(struct kvm_vcpu *vcpu)
  685. {
  686. int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
  687. int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
  688. int reg, rc, nr_regs;
  689. u32 ctl_array[16];
  690. u64 ga;
  691. ar_t ar;
  692. vcpu->stat.instruction_lctl++;
  693. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
  694. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  695. ga = kvm_s390_get_base_disp_rs(vcpu, &ar);
  696. if (ga & 3)
  697. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  698. VCPU_EVENT(vcpu, 5, "lctl r1:%x, r3:%x, addr:%llx", reg1, reg3, ga);
  699. trace_kvm_s390_handle_lctl(vcpu, 0, reg1, reg3, ga);
  700. nr_regs = ((reg3 - reg1) & 0xf) + 1;
  701. rc = read_guest(vcpu, ga, ar, ctl_array, nr_regs * sizeof(u32));
  702. if (rc)
  703. return kvm_s390_inject_prog_cond(vcpu, rc);
  704. reg = reg1;
  705. nr_regs = 0;
  706. do {
  707. vcpu->arch.sie_block->gcr[reg] &= 0xffffffff00000000ul;
  708. vcpu->arch.sie_block->gcr[reg] |= ctl_array[nr_regs++];
  709. if (reg == reg3)
  710. break;
  711. reg = (reg + 1) % 16;
  712. } while (1);
  713. kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
  714. return 0;
  715. }
  716. int kvm_s390_handle_stctl(struct kvm_vcpu *vcpu)
  717. {
  718. int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
  719. int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
  720. int reg, rc, nr_regs;
  721. u32 ctl_array[16];
  722. u64 ga;
  723. ar_t ar;
  724. vcpu->stat.instruction_stctl++;
  725. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
  726. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  727. ga = kvm_s390_get_base_disp_rs(vcpu, &ar);
  728. if (ga & 3)
  729. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  730. VCPU_EVENT(vcpu, 5, "stctl r1:%x, r3:%x, addr:%llx", reg1, reg3, ga);
  731. trace_kvm_s390_handle_stctl(vcpu, 0, reg1, reg3, ga);
  732. reg = reg1;
  733. nr_regs = 0;
  734. do {
  735. ctl_array[nr_regs++] = vcpu->arch.sie_block->gcr[reg];
  736. if (reg == reg3)
  737. break;
  738. reg = (reg + 1) % 16;
  739. } while (1);
  740. rc = write_guest(vcpu, ga, ar, ctl_array, nr_regs * sizeof(u32));
  741. return rc ? kvm_s390_inject_prog_cond(vcpu, rc) : 0;
  742. }
  743. static int handle_lctlg(struct kvm_vcpu *vcpu)
  744. {
  745. int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
  746. int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
  747. int reg, rc, nr_regs;
  748. u64 ctl_array[16];
  749. u64 ga;
  750. ar_t ar;
  751. vcpu->stat.instruction_lctlg++;
  752. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
  753. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  754. ga = kvm_s390_get_base_disp_rsy(vcpu, &ar);
  755. if (ga & 7)
  756. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  757. VCPU_EVENT(vcpu, 5, "lctlg r1:%x, r3:%x, addr:%llx", reg1, reg3, ga);
  758. trace_kvm_s390_handle_lctl(vcpu, 1, reg1, reg3, ga);
  759. nr_regs = ((reg3 - reg1) & 0xf) + 1;
  760. rc = read_guest(vcpu, ga, ar, ctl_array, nr_regs * sizeof(u64));
  761. if (rc)
  762. return kvm_s390_inject_prog_cond(vcpu, rc);
  763. reg = reg1;
  764. nr_regs = 0;
  765. do {
  766. vcpu->arch.sie_block->gcr[reg] = ctl_array[nr_regs++];
  767. if (reg == reg3)
  768. break;
  769. reg = (reg + 1) % 16;
  770. } while (1);
  771. kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
  772. return 0;
  773. }
  774. static int handle_stctg(struct kvm_vcpu *vcpu)
  775. {
  776. int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
  777. int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
  778. int reg, rc, nr_regs;
  779. u64 ctl_array[16];
  780. u64 ga;
  781. ar_t ar;
  782. vcpu->stat.instruction_stctg++;
  783. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
  784. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  785. ga = kvm_s390_get_base_disp_rsy(vcpu, &ar);
  786. if (ga & 7)
  787. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  788. VCPU_EVENT(vcpu, 5, "stctg r1:%x, r3:%x, addr:%llx", reg1, reg3, ga);
  789. trace_kvm_s390_handle_stctl(vcpu, 1, reg1, reg3, ga);
  790. reg = reg1;
  791. nr_regs = 0;
  792. do {
  793. ctl_array[nr_regs++] = vcpu->arch.sie_block->gcr[reg];
  794. if (reg == reg3)
  795. break;
  796. reg = (reg + 1) % 16;
  797. } while (1);
  798. rc = write_guest(vcpu, ga, ar, ctl_array, nr_regs * sizeof(u64));
  799. return rc ? kvm_s390_inject_prog_cond(vcpu, rc) : 0;
  800. }
  801. static const intercept_handler_t eb_handlers[256] = {
  802. [0x2f] = handle_lctlg,
  803. [0x25] = handle_stctg,
  804. };
  805. int kvm_s390_handle_eb(struct kvm_vcpu *vcpu)
  806. {
  807. intercept_handler_t handler;
  808. handler = eb_handlers[vcpu->arch.sie_block->ipb & 0xff];
  809. if (handler)
  810. return handler(vcpu);
  811. return -EOPNOTSUPP;
  812. }
  813. static int handle_tprot(struct kvm_vcpu *vcpu)
  814. {
  815. u64 address1, address2;
  816. unsigned long hva, gpa;
  817. int ret = 0, cc = 0;
  818. bool writable;
  819. ar_t ar;
  820. vcpu->stat.instruction_tprot++;
  821. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
  822. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  823. kvm_s390_get_base_disp_sse(vcpu, &address1, &address2, &ar, NULL);
  824. /* we only handle the Linux memory detection case:
  825. * access key == 0
  826. * everything else goes to userspace. */
  827. if (address2 & 0xf0)
  828. return -EOPNOTSUPP;
  829. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_DAT)
  830. ipte_lock(vcpu);
  831. ret = guest_translate_address(vcpu, address1, ar, &gpa, 1);
  832. if (ret == PGM_PROTECTION) {
  833. /* Write protected? Try again with read-only... */
  834. cc = 1;
  835. ret = guest_translate_address(vcpu, address1, ar, &gpa, 0);
  836. }
  837. if (ret) {
  838. if (ret == PGM_ADDRESSING || ret == PGM_TRANSLATION_SPEC) {
  839. ret = kvm_s390_inject_program_int(vcpu, ret);
  840. } else if (ret > 0) {
  841. /* Translation not available */
  842. kvm_s390_set_psw_cc(vcpu, 3);
  843. ret = 0;
  844. }
  845. goto out_unlock;
  846. }
  847. hva = gfn_to_hva_prot(vcpu->kvm, gpa_to_gfn(gpa), &writable);
  848. if (kvm_is_error_hva(hva)) {
  849. ret = kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
  850. } else {
  851. if (!writable)
  852. cc = 1; /* Write not permitted ==> read-only */
  853. kvm_s390_set_psw_cc(vcpu, cc);
  854. /* Note: CC2 only occurs for storage keys (not supported yet) */
  855. }
  856. out_unlock:
  857. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_DAT)
  858. ipte_unlock(vcpu);
  859. return ret;
  860. }
  861. int kvm_s390_handle_e5(struct kvm_vcpu *vcpu)
  862. {
  863. /* For e5xx... instructions we only handle TPROT */
  864. if ((vcpu->arch.sie_block->ipa & 0x00ff) == 0x01)
  865. return handle_tprot(vcpu);
  866. return -EOPNOTSUPP;
  867. }
  868. static int handle_sckpf(struct kvm_vcpu *vcpu)
  869. {
  870. u32 value;
  871. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
  872. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  873. if (vcpu->run->s.regs.gprs[0] & 0x00000000ffff0000)
  874. return kvm_s390_inject_program_int(vcpu,
  875. PGM_SPECIFICATION);
  876. value = vcpu->run->s.regs.gprs[0] & 0x000000000000ffff;
  877. vcpu->arch.sie_block->todpr = value;
  878. return 0;
  879. }
  880. static const intercept_handler_t x01_handlers[256] = {
  881. [0x07] = handle_sckpf,
  882. };
  883. int kvm_s390_handle_01(struct kvm_vcpu *vcpu)
  884. {
  885. intercept_handler_t handler;
  886. handler = x01_handlers[vcpu->arch.sie_block->ipa & 0x00ff];
  887. if (handler)
  888. return handler(vcpu);
  889. return -EOPNOTSUPP;
  890. }