priv.c 33 KB

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