priv.c 33 KB

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