priv.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057
  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;
  248. inti = kvm_s390_get_io_int(vcpu->kvm, 0,
  249. vcpu->run->s.regs.gprs[1]);
  250. /*
  251. * Prepare exit to userspace.
  252. * We indicate whether we dequeued a pending I/O interrupt
  253. * so that userspace can re-inject it if the instruction gets
  254. * a program check. While this may re-order the pending I/O
  255. * interrupts, this is no problem since the priority is kept
  256. * intact.
  257. */
  258. vcpu->run->exit_reason = KVM_EXIT_S390_TSCH;
  259. vcpu->run->s390_tsch.dequeued = !!inti;
  260. if (inti) {
  261. vcpu->run->s390_tsch.subchannel_id = inti->io.subchannel_id;
  262. vcpu->run->s390_tsch.subchannel_nr = inti->io.subchannel_nr;
  263. vcpu->run->s390_tsch.io_int_parm = inti->io.io_int_parm;
  264. vcpu->run->s390_tsch.io_int_word = inti->io.io_int_word;
  265. }
  266. vcpu->run->s390_tsch.ipb = vcpu->arch.sie_block->ipb;
  267. kfree(inti);
  268. return -EREMOTE;
  269. }
  270. static int handle_io_inst(struct kvm_vcpu *vcpu)
  271. {
  272. VCPU_EVENT(vcpu, 4, "%s", "I/O instruction");
  273. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
  274. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  275. if (vcpu->kvm->arch.css_support) {
  276. /*
  277. * Most I/O instructions will be handled by userspace.
  278. * Exceptions are tpi and the interrupt portion of tsch.
  279. */
  280. if (vcpu->arch.sie_block->ipa == 0xb236)
  281. return handle_tpi(vcpu);
  282. if (vcpu->arch.sie_block->ipa == 0xb235)
  283. return handle_tsch(vcpu);
  284. /* Handle in userspace. */
  285. return -EOPNOTSUPP;
  286. } else {
  287. /*
  288. * Set condition code 3 to stop the guest from issuing channel
  289. * I/O instructions.
  290. */
  291. kvm_s390_set_psw_cc(vcpu, 3);
  292. return 0;
  293. }
  294. }
  295. static int handle_stfl(struct kvm_vcpu *vcpu)
  296. {
  297. int rc;
  298. unsigned int fac;
  299. vcpu->stat.instruction_stfl++;
  300. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
  301. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  302. /*
  303. * We need to shift the lower 32 facility bits (bit 0-31) from a u64
  304. * into a u32 memory representation. They will remain bits 0-31.
  305. */
  306. fac = *vcpu->kvm->arch.model.fac->list >> 32;
  307. rc = write_guest_lc(vcpu, offsetof(struct _lowcore, stfl_fac_list),
  308. &fac, sizeof(fac));
  309. if (rc)
  310. return rc;
  311. VCPU_EVENT(vcpu, 5, "store facility list value %x", fac);
  312. trace_kvm_s390_handle_stfl(vcpu, fac);
  313. return 0;
  314. }
  315. #define PSW_MASK_ADDR_MODE (PSW_MASK_EA | PSW_MASK_BA)
  316. #define PSW_MASK_UNASSIGNED 0xb80800fe7fffffffUL
  317. #define PSW_ADDR_24 0x0000000000ffffffUL
  318. #define PSW_ADDR_31 0x000000007fffffffUL
  319. int is_valid_psw(psw_t *psw)
  320. {
  321. if (psw->mask & PSW_MASK_UNASSIGNED)
  322. return 0;
  323. if ((psw->mask & PSW_MASK_ADDR_MODE) == PSW_MASK_BA) {
  324. if (psw->addr & ~PSW_ADDR_31)
  325. return 0;
  326. }
  327. if (!(psw->mask & PSW_MASK_ADDR_MODE) && (psw->addr & ~PSW_ADDR_24))
  328. return 0;
  329. if ((psw->mask & PSW_MASK_ADDR_MODE) == PSW_MASK_EA)
  330. return 0;
  331. if (psw->addr & 1)
  332. return 0;
  333. return 1;
  334. }
  335. int kvm_s390_handle_lpsw(struct kvm_vcpu *vcpu)
  336. {
  337. psw_t *gpsw = &vcpu->arch.sie_block->gpsw;
  338. psw_compat_t new_psw;
  339. u64 addr;
  340. int rc;
  341. ar_t ar;
  342. if (gpsw->mask & PSW_MASK_PSTATE)
  343. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  344. addr = kvm_s390_get_base_disp_s(vcpu, &ar);
  345. if (addr & 7)
  346. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  347. rc = read_guest(vcpu, addr, ar, &new_psw, sizeof(new_psw));
  348. if (rc)
  349. return kvm_s390_inject_prog_cond(vcpu, rc);
  350. if (!(new_psw.mask & PSW32_MASK_BASE))
  351. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  352. gpsw->mask = (new_psw.mask & ~PSW32_MASK_BASE) << 32;
  353. gpsw->mask |= new_psw.addr & PSW32_ADDR_AMODE;
  354. gpsw->addr = new_psw.addr & ~PSW32_ADDR_AMODE;
  355. if (!is_valid_psw(gpsw))
  356. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  357. return 0;
  358. }
  359. static int handle_lpswe(struct kvm_vcpu *vcpu)
  360. {
  361. psw_t new_psw;
  362. u64 addr;
  363. int rc;
  364. ar_t ar;
  365. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
  366. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  367. addr = kvm_s390_get_base_disp_s(vcpu, &ar);
  368. if (addr & 7)
  369. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  370. rc = read_guest(vcpu, addr, ar, &new_psw, sizeof(new_psw));
  371. if (rc)
  372. return kvm_s390_inject_prog_cond(vcpu, rc);
  373. vcpu->arch.sie_block->gpsw = new_psw;
  374. if (!is_valid_psw(&vcpu->arch.sie_block->gpsw))
  375. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  376. return 0;
  377. }
  378. static int handle_stidp(struct kvm_vcpu *vcpu)
  379. {
  380. u64 stidp_data = vcpu->arch.stidp_data;
  381. u64 operand2;
  382. int rc;
  383. ar_t ar;
  384. vcpu->stat.instruction_stidp++;
  385. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
  386. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  387. operand2 = kvm_s390_get_base_disp_s(vcpu, &ar);
  388. if (operand2 & 7)
  389. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  390. rc = write_guest(vcpu, operand2, ar, &stidp_data, sizeof(stidp_data));
  391. if (rc)
  392. return kvm_s390_inject_prog_cond(vcpu, rc);
  393. VCPU_EVENT(vcpu, 5, "%s", "store cpu id");
  394. return 0;
  395. }
  396. static void handle_stsi_3_2_2(struct kvm_vcpu *vcpu, struct sysinfo_3_2_2 *mem)
  397. {
  398. int cpus = 0;
  399. int n;
  400. cpus = atomic_read(&vcpu->kvm->online_vcpus);
  401. /* deal with other level 3 hypervisors */
  402. if (stsi(mem, 3, 2, 2))
  403. mem->count = 0;
  404. if (mem->count < 8)
  405. mem->count++;
  406. for (n = mem->count - 1; n > 0 ; n--)
  407. memcpy(&mem->vm[n], &mem->vm[n - 1], sizeof(mem->vm[0]));
  408. memset(&mem->vm[0], 0, sizeof(mem->vm[0]));
  409. mem->vm[0].cpus_total = cpus;
  410. mem->vm[0].cpus_configured = cpus;
  411. mem->vm[0].cpus_standby = 0;
  412. mem->vm[0].cpus_reserved = 0;
  413. mem->vm[0].caf = 1000;
  414. memcpy(mem->vm[0].name, "KVMguest", 8);
  415. ASCEBC(mem->vm[0].name, 8);
  416. memcpy(mem->vm[0].cpi, "KVM/Linux ", 16);
  417. ASCEBC(mem->vm[0].cpi, 16);
  418. }
  419. static void insert_stsi_usr_data(struct kvm_vcpu *vcpu, u64 addr, ar_t ar,
  420. u8 fc, u8 sel1, u16 sel2)
  421. {
  422. vcpu->run->exit_reason = KVM_EXIT_S390_STSI;
  423. vcpu->run->s390_stsi.addr = addr;
  424. vcpu->run->s390_stsi.ar = ar;
  425. vcpu->run->s390_stsi.fc = fc;
  426. vcpu->run->s390_stsi.sel1 = sel1;
  427. vcpu->run->s390_stsi.sel2 = sel2;
  428. }
  429. static int handle_stsi(struct kvm_vcpu *vcpu)
  430. {
  431. int fc = (vcpu->run->s.regs.gprs[0] & 0xf0000000) >> 28;
  432. int sel1 = vcpu->run->s.regs.gprs[0] & 0xff;
  433. int sel2 = vcpu->run->s.regs.gprs[1] & 0xffff;
  434. unsigned long mem = 0;
  435. u64 operand2;
  436. int rc = 0;
  437. ar_t ar;
  438. vcpu->stat.instruction_stsi++;
  439. VCPU_EVENT(vcpu, 4, "stsi: fc: %x sel1: %x sel2: %x", fc, sel1, sel2);
  440. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
  441. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  442. if (fc > 3) {
  443. kvm_s390_set_psw_cc(vcpu, 3);
  444. return 0;
  445. }
  446. if (vcpu->run->s.regs.gprs[0] & 0x0fffff00
  447. || vcpu->run->s.regs.gprs[1] & 0xffff0000)
  448. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  449. if (fc == 0) {
  450. vcpu->run->s.regs.gprs[0] = 3 << 28;
  451. kvm_s390_set_psw_cc(vcpu, 0);
  452. return 0;
  453. }
  454. operand2 = kvm_s390_get_base_disp_s(vcpu, &ar);
  455. if (operand2 & 0xfff)
  456. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  457. switch (fc) {
  458. case 1: /* same handling for 1 and 2 */
  459. case 2:
  460. mem = get_zeroed_page(GFP_KERNEL);
  461. if (!mem)
  462. goto out_no_data;
  463. if (stsi((void *) mem, fc, sel1, sel2))
  464. goto out_no_data;
  465. break;
  466. case 3:
  467. if (sel1 != 2 || sel2 != 2)
  468. goto out_no_data;
  469. mem = get_zeroed_page(GFP_KERNEL);
  470. if (!mem)
  471. goto out_no_data;
  472. handle_stsi_3_2_2(vcpu, (void *) mem);
  473. break;
  474. }
  475. rc = write_guest(vcpu, operand2, ar, (void *)mem, PAGE_SIZE);
  476. if (rc) {
  477. rc = kvm_s390_inject_prog_cond(vcpu, rc);
  478. goto out;
  479. }
  480. if (vcpu->kvm->arch.user_stsi) {
  481. insert_stsi_usr_data(vcpu, operand2, ar, fc, sel1, sel2);
  482. rc = -EREMOTE;
  483. }
  484. trace_kvm_s390_handle_stsi(vcpu, fc, sel1, sel2, operand2);
  485. free_page(mem);
  486. kvm_s390_set_psw_cc(vcpu, 0);
  487. vcpu->run->s.regs.gprs[0] = 0;
  488. return rc;
  489. out_no_data:
  490. kvm_s390_set_psw_cc(vcpu, 3);
  491. out:
  492. free_page(mem);
  493. return rc;
  494. }
  495. static const intercept_handler_t b2_handlers[256] = {
  496. [0x02] = handle_stidp,
  497. [0x04] = handle_set_clock,
  498. [0x10] = handle_set_prefix,
  499. [0x11] = handle_store_prefix,
  500. [0x12] = handle_store_cpu_address,
  501. [0x21] = handle_ipte_interlock,
  502. [0x29] = handle_skey,
  503. [0x2a] = handle_skey,
  504. [0x2b] = handle_skey,
  505. [0x2c] = handle_test_block,
  506. [0x30] = handle_io_inst,
  507. [0x31] = handle_io_inst,
  508. [0x32] = handle_io_inst,
  509. [0x33] = handle_io_inst,
  510. [0x34] = handle_io_inst,
  511. [0x35] = handle_io_inst,
  512. [0x36] = handle_io_inst,
  513. [0x37] = handle_io_inst,
  514. [0x38] = handle_io_inst,
  515. [0x39] = handle_io_inst,
  516. [0x3a] = handle_io_inst,
  517. [0x3b] = handle_io_inst,
  518. [0x3c] = handle_io_inst,
  519. [0x50] = handle_ipte_interlock,
  520. [0x5f] = handle_io_inst,
  521. [0x74] = handle_io_inst,
  522. [0x76] = handle_io_inst,
  523. [0x7d] = handle_stsi,
  524. [0xb1] = handle_stfl,
  525. [0xb2] = handle_lpswe,
  526. };
  527. int kvm_s390_handle_b2(struct kvm_vcpu *vcpu)
  528. {
  529. intercept_handler_t handler;
  530. /*
  531. * A lot of B2 instructions are priviledged. Here we check for
  532. * the privileged ones, that we can handle in the kernel.
  533. * Anything else goes to userspace.
  534. */
  535. handler = b2_handlers[vcpu->arch.sie_block->ipa & 0x00ff];
  536. if (handler)
  537. return handler(vcpu);
  538. return -EOPNOTSUPP;
  539. }
  540. static int handle_epsw(struct kvm_vcpu *vcpu)
  541. {
  542. int reg1, reg2;
  543. kvm_s390_get_regs_rre(vcpu, &reg1, &reg2);
  544. /* This basically extracts the mask half of the psw. */
  545. vcpu->run->s.regs.gprs[reg1] &= 0xffffffff00000000UL;
  546. vcpu->run->s.regs.gprs[reg1] |= vcpu->arch.sie_block->gpsw.mask >> 32;
  547. if (reg2) {
  548. vcpu->run->s.regs.gprs[reg2] &= 0xffffffff00000000UL;
  549. vcpu->run->s.regs.gprs[reg2] |=
  550. vcpu->arch.sie_block->gpsw.mask & 0x00000000ffffffffUL;
  551. }
  552. return 0;
  553. }
  554. #define PFMF_RESERVED 0xfffc0101UL
  555. #define PFMF_SK 0x00020000UL
  556. #define PFMF_CF 0x00010000UL
  557. #define PFMF_UI 0x00008000UL
  558. #define PFMF_FSC 0x00007000UL
  559. #define PFMF_NQ 0x00000800UL
  560. #define PFMF_MR 0x00000400UL
  561. #define PFMF_MC 0x00000200UL
  562. #define PFMF_KEY 0x000000feUL
  563. static int handle_pfmf(struct kvm_vcpu *vcpu)
  564. {
  565. int reg1, reg2;
  566. unsigned long start, end;
  567. vcpu->stat.instruction_pfmf++;
  568. kvm_s390_get_regs_rre(vcpu, &reg1, &reg2);
  569. if (!MACHINE_HAS_PFMF)
  570. return kvm_s390_inject_program_int(vcpu, PGM_OPERATION);
  571. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
  572. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  573. if (vcpu->run->s.regs.gprs[reg1] & PFMF_RESERVED)
  574. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  575. /* Only provide non-quiescing support if the host supports it */
  576. if (vcpu->run->s.regs.gprs[reg1] & PFMF_NQ && !test_facility(14))
  577. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  578. /* No support for conditional-SSKE */
  579. if (vcpu->run->s.regs.gprs[reg1] & (PFMF_MR | PFMF_MC))
  580. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  581. start = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK;
  582. start = kvm_s390_logical_to_effective(vcpu, start);
  583. switch (vcpu->run->s.regs.gprs[reg1] & PFMF_FSC) {
  584. case 0x00000000:
  585. end = (start + (1UL << 12)) & ~((1UL << 12) - 1);
  586. break;
  587. case 0x00001000:
  588. end = (start + (1UL << 20)) & ~((1UL << 20) - 1);
  589. break;
  590. /* We dont support EDAT2
  591. case 0x00002000:
  592. end = (start + (1UL << 31)) & ~((1UL << 31) - 1);
  593. break;*/
  594. default:
  595. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  596. }
  597. if (vcpu->run->s.regs.gprs[reg1] & PFMF_CF) {
  598. if (kvm_s390_check_low_addr_prot_real(vcpu, start))
  599. return kvm_s390_inject_prog_irq(vcpu, &vcpu->arch.pgm);
  600. }
  601. while (start < end) {
  602. unsigned long useraddr, abs_addr;
  603. /* Translate guest address to host address */
  604. if ((vcpu->run->s.regs.gprs[reg1] & PFMF_FSC) == 0)
  605. abs_addr = kvm_s390_real_to_abs(vcpu, start);
  606. else
  607. abs_addr = start;
  608. useraddr = gfn_to_hva(vcpu->kvm, gpa_to_gfn(abs_addr));
  609. if (kvm_is_error_hva(useraddr))
  610. return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
  611. if (vcpu->run->s.regs.gprs[reg1] & PFMF_CF) {
  612. if (clear_user((void __user *)useraddr, PAGE_SIZE))
  613. return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
  614. }
  615. if (vcpu->run->s.regs.gprs[reg1] & PFMF_SK) {
  616. int rc = __skey_check_enable(vcpu);
  617. if (rc)
  618. return rc;
  619. if (set_guest_storage_key(current->mm, useraddr,
  620. vcpu->run->s.regs.gprs[reg1] & PFMF_KEY,
  621. vcpu->run->s.regs.gprs[reg1] & PFMF_NQ))
  622. return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
  623. }
  624. start += PAGE_SIZE;
  625. }
  626. if (vcpu->run->s.regs.gprs[reg1] & PFMF_FSC)
  627. vcpu->run->s.regs.gprs[reg2] = end;
  628. return 0;
  629. }
  630. static int handle_essa(struct kvm_vcpu *vcpu)
  631. {
  632. /* entries expected to be 1FF */
  633. int entries = (vcpu->arch.sie_block->cbrlo & ~PAGE_MASK) >> 3;
  634. unsigned long *cbrlo, cbrle;
  635. struct gmap *gmap;
  636. int i;
  637. VCPU_EVENT(vcpu, 5, "cmma release %d pages", entries);
  638. gmap = vcpu->arch.gmap;
  639. vcpu->stat.instruction_essa++;
  640. if (!kvm_s390_cmma_enabled(vcpu->kvm))
  641. return kvm_s390_inject_program_int(vcpu, PGM_OPERATION);
  642. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
  643. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  644. if (((vcpu->arch.sie_block->ipb & 0xf0000000) >> 28) > 6)
  645. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  646. /* Rewind PSW to repeat the ESSA instruction */
  647. kvm_s390_rewind_psw(vcpu, 4);
  648. vcpu->arch.sie_block->cbrlo &= PAGE_MASK; /* reset nceo */
  649. cbrlo = phys_to_virt(vcpu->arch.sie_block->cbrlo);
  650. down_read(&gmap->mm->mmap_sem);
  651. for (i = 0; i < entries; ++i) {
  652. cbrle = cbrlo[i];
  653. if (unlikely(cbrle & ~PAGE_MASK || cbrle < 2 * PAGE_SIZE))
  654. /* invalid entry */
  655. break;
  656. /* try to free backing */
  657. __gmap_zap(gmap, cbrle);
  658. }
  659. up_read(&gmap->mm->mmap_sem);
  660. if (i < entries)
  661. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  662. return 0;
  663. }
  664. static const intercept_handler_t b9_handlers[256] = {
  665. [0x8a] = handle_ipte_interlock,
  666. [0x8d] = handle_epsw,
  667. [0x8e] = handle_ipte_interlock,
  668. [0x8f] = handle_ipte_interlock,
  669. [0xab] = handle_essa,
  670. [0xaf] = handle_pfmf,
  671. };
  672. int kvm_s390_handle_b9(struct kvm_vcpu *vcpu)
  673. {
  674. intercept_handler_t handler;
  675. /* This is handled just as for the B2 instructions. */
  676. handler = b9_handlers[vcpu->arch.sie_block->ipa & 0x00ff];
  677. if (handler)
  678. return handler(vcpu);
  679. return -EOPNOTSUPP;
  680. }
  681. int kvm_s390_handle_lctl(struct kvm_vcpu *vcpu)
  682. {
  683. int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
  684. int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
  685. int reg, rc, nr_regs;
  686. u32 ctl_array[16];
  687. u64 ga;
  688. ar_t ar;
  689. vcpu->stat.instruction_lctl++;
  690. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
  691. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  692. ga = kvm_s390_get_base_disp_rs(vcpu, &ar);
  693. if (ga & 3)
  694. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  695. VCPU_EVENT(vcpu, 5, "lctl r1:%x, r3:%x, addr:%llx", reg1, reg3, ga);
  696. trace_kvm_s390_handle_lctl(vcpu, 0, reg1, reg3, ga);
  697. nr_regs = ((reg3 - reg1) & 0xf) + 1;
  698. rc = read_guest(vcpu, ga, ar, ctl_array, nr_regs * sizeof(u32));
  699. if (rc)
  700. return kvm_s390_inject_prog_cond(vcpu, rc);
  701. reg = reg1;
  702. nr_regs = 0;
  703. do {
  704. vcpu->arch.sie_block->gcr[reg] &= 0xffffffff00000000ul;
  705. vcpu->arch.sie_block->gcr[reg] |= ctl_array[nr_regs++];
  706. if (reg == reg3)
  707. break;
  708. reg = (reg + 1) % 16;
  709. } while (1);
  710. kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
  711. return 0;
  712. }
  713. int kvm_s390_handle_stctl(struct kvm_vcpu *vcpu)
  714. {
  715. int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
  716. int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
  717. int reg, rc, nr_regs;
  718. u32 ctl_array[16];
  719. u64 ga;
  720. ar_t ar;
  721. vcpu->stat.instruction_stctl++;
  722. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
  723. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  724. ga = kvm_s390_get_base_disp_rs(vcpu, &ar);
  725. if (ga & 3)
  726. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  727. VCPU_EVENT(vcpu, 5, "stctl r1:%x, r3:%x, addr:%llx", reg1, reg3, ga);
  728. trace_kvm_s390_handle_stctl(vcpu, 0, reg1, reg3, ga);
  729. reg = reg1;
  730. nr_regs = 0;
  731. do {
  732. ctl_array[nr_regs++] = vcpu->arch.sie_block->gcr[reg];
  733. if (reg == reg3)
  734. break;
  735. reg = (reg + 1) % 16;
  736. } while (1);
  737. rc = write_guest(vcpu, ga, ar, ctl_array, nr_regs * sizeof(u32));
  738. return rc ? kvm_s390_inject_prog_cond(vcpu, rc) : 0;
  739. }
  740. static int handle_lctlg(struct kvm_vcpu *vcpu)
  741. {
  742. int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
  743. int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
  744. int reg, rc, nr_regs;
  745. u64 ctl_array[16];
  746. u64 ga;
  747. ar_t ar;
  748. vcpu->stat.instruction_lctlg++;
  749. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
  750. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  751. ga = kvm_s390_get_base_disp_rsy(vcpu, &ar);
  752. if (ga & 7)
  753. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  754. VCPU_EVENT(vcpu, 5, "lctlg r1:%x, r3:%x, addr:%llx", reg1, reg3, ga);
  755. trace_kvm_s390_handle_lctl(vcpu, 1, reg1, reg3, ga);
  756. nr_regs = ((reg3 - reg1) & 0xf) + 1;
  757. rc = read_guest(vcpu, ga, ar, ctl_array, nr_regs * sizeof(u64));
  758. if (rc)
  759. return kvm_s390_inject_prog_cond(vcpu, rc);
  760. reg = reg1;
  761. nr_regs = 0;
  762. do {
  763. vcpu->arch.sie_block->gcr[reg] = ctl_array[nr_regs++];
  764. if (reg == reg3)
  765. break;
  766. reg = (reg + 1) % 16;
  767. } while (1);
  768. kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
  769. return 0;
  770. }
  771. static int handle_stctg(struct kvm_vcpu *vcpu)
  772. {
  773. int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
  774. int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
  775. int reg, rc, nr_regs;
  776. u64 ctl_array[16];
  777. u64 ga;
  778. ar_t ar;
  779. vcpu->stat.instruction_stctg++;
  780. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
  781. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  782. ga = kvm_s390_get_base_disp_rsy(vcpu, &ar);
  783. if (ga & 7)
  784. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  785. VCPU_EVENT(vcpu, 5, "stctg r1:%x, r3:%x, addr:%llx", reg1, reg3, ga);
  786. trace_kvm_s390_handle_stctl(vcpu, 1, reg1, reg3, ga);
  787. reg = reg1;
  788. nr_regs = 0;
  789. do {
  790. ctl_array[nr_regs++] = vcpu->arch.sie_block->gcr[reg];
  791. if (reg == reg3)
  792. break;
  793. reg = (reg + 1) % 16;
  794. } while (1);
  795. rc = write_guest(vcpu, ga, ar, ctl_array, nr_regs * sizeof(u64));
  796. return rc ? kvm_s390_inject_prog_cond(vcpu, rc) : 0;
  797. }
  798. static const intercept_handler_t eb_handlers[256] = {
  799. [0x2f] = handle_lctlg,
  800. [0x25] = handle_stctg,
  801. };
  802. int kvm_s390_handle_eb(struct kvm_vcpu *vcpu)
  803. {
  804. intercept_handler_t handler;
  805. handler = eb_handlers[vcpu->arch.sie_block->ipb & 0xff];
  806. if (handler)
  807. return handler(vcpu);
  808. return -EOPNOTSUPP;
  809. }
  810. static int handle_tprot(struct kvm_vcpu *vcpu)
  811. {
  812. u64 address1, address2;
  813. unsigned long hva, gpa;
  814. int ret = 0, cc = 0;
  815. bool writable;
  816. ar_t ar;
  817. vcpu->stat.instruction_tprot++;
  818. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
  819. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  820. kvm_s390_get_base_disp_sse(vcpu, &address1, &address2, &ar, NULL);
  821. /* we only handle the Linux memory detection case:
  822. * access key == 0
  823. * everything else goes to userspace. */
  824. if (address2 & 0xf0)
  825. return -EOPNOTSUPP;
  826. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_DAT)
  827. ipte_lock(vcpu);
  828. ret = guest_translate_address(vcpu, address1, ar, &gpa, 1);
  829. if (ret == PGM_PROTECTION) {
  830. /* Write protected? Try again with read-only... */
  831. cc = 1;
  832. ret = guest_translate_address(vcpu, address1, ar, &gpa, 0);
  833. }
  834. if (ret) {
  835. if (ret == PGM_ADDRESSING || ret == PGM_TRANSLATION_SPEC) {
  836. ret = kvm_s390_inject_program_int(vcpu, ret);
  837. } else if (ret > 0) {
  838. /* Translation not available */
  839. kvm_s390_set_psw_cc(vcpu, 3);
  840. ret = 0;
  841. }
  842. goto out_unlock;
  843. }
  844. hva = gfn_to_hva_prot(vcpu->kvm, gpa_to_gfn(gpa), &writable);
  845. if (kvm_is_error_hva(hva)) {
  846. ret = kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
  847. } else {
  848. if (!writable)
  849. cc = 1; /* Write not permitted ==> read-only */
  850. kvm_s390_set_psw_cc(vcpu, cc);
  851. /* Note: CC2 only occurs for storage keys (not supported yet) */
  852. }
  853. out_unlock:
  854. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_DAT)
  855. ipte_unlock(vcpu);
  856. return ret;
  857. }
  858. int kvm_s390_handle_e5(struct kvm_vcpu *vcpu)
  859. {
  860. /* For e5xx... instructions we only handle TPROT */
  861. if ((vcpu->arch.sie_block->ipa & 0x00ff) == 0x01)
  862. return handle_tprot(vcpu);
  863. return -EOPNOTSUPP;
  864. }
  865. static int handle_sckpf(struct kvm_vcpu *vcpu)
  866. {
  867. u32 value;
  868. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
  869. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  870. if (vcpu->run->s.regs.gprs[0] & 0x00000000ffff0000)
  871. return kvm_s390_inject_program_int(vcpu,
  872. PGM_SPECIFICATION);
  873. value = vcpu->run->s.regs.gprs[0] & 0x000000000000ffff;
  874. vcpu->arch.sie_block->todpr = value;
  875. return 0;
  876. }
  877. static const intercept_handler_t x01_handlers[256] = {
  878. [0x07] = handle_sckpf,
  879. };
  880. int kvm_s390_handle_01(struct kvm_vcpu *vcpu)
  881. {
  882. intercept_handler_t handler;
  883. handler = x01_handlers[vcpu->arch.sie_block->ipa & 0x00ff];
  884. if (handler)
  885. return handler(vcpu);
  886. return -EOPNOTSUPP;
  887. }