priv.c 28 KB

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