priv.c 37 KB

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