priv.c 38 KB

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