priv.c 32 KB

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