priv.c 26 KB

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