priv.c 37 KB

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