priv.c 28 KB

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