priv.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766
  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. static int handle_set_prefix(struct kvm_vcpu *vcpu)
  32. {
  33. u64 operand2;
  34. u32 address = 0;
  35. u8 tmp;
  36. vcpu->stat.instruction_spx++;
  37. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
  38. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  39. operand2 = kvm_s390_get_base_disp_s(vcpu);
  40. /* must be word boundary */
  41. if (operand2 & 3)
  42. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  43. /* get the value */
  44. if (get_guest(vcpu, address, (u32 __user *) operand2))
  45. return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
  46. address = address & 0x7fffe000u;
  47. /* make sure that the new value is valid memory */
  48. if (copy_from_guest_absolute(vcpu, &tmp, address, 1) ||
  49. (copy_from_guest_absolute(vcpu, &tmp, address + PAGE_SIZE, 1)))
  50. return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
  51. kvm_s390_set_prefix(vcpu, address);
  52. VCPU_EVENT(vcpu, 5, "setting prefix to %x", address);
  53. trace_kvm_s390_handle_prefix(vcpu, 1, address);
  54. return 0;
  55. }
  56. static int handle_store_prefix(struct kvm_vcpu *vcpu)
  57. {
  58. u64 operand2;
  59. u32 address;
  60. vcpu->stat.instruction_stpx++;
  61. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
  62. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  63. operand2 = kvm_s390_get_base_disp_s(vcpu);
  64. /* must be word boundary */
  65. if (operand2 & 3)
  66. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  67. address = vcpu->arch.sie_block->prefix;
  68. address = address & 0x7fffe000u;
  69. /* get the value */
  70. if (put_guest(vcpu, address, (u32 __user *)operand2))
  71. return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
  72. VCPU_EVENT(vcpu, 5, "storing prefix to %x", address);
  73. trace_kvm_s390_handle_prefix(vcpu, 0, address);
  74. return 0;
  75. }
  76. static int handle_store_cpu_address(struct kvm_vcpu *vcpu)
  77. {
  78. u64 useraddr;
  79. vcpu->stat.instruction_stap++;
  80. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
  81. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  82. useraddr = kvm_s390_get_base_disp_s(vcpu);
  83. if (useraddr & 1)
  84. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  85. if (put_guest(vcpu, vcpu->vcpu_id, (u16 __user *)useraddr))
  86. return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
  87. VCPU_EVENT(vcpu, 5, "storing cpu address to %llx", useraddr);
  88. trace_kvm_s390_handle_stap(vcpu, useraddr);
  89. return 0;
  90. }
  91. static int handle_skey(struct kvm_vcpu *vcpu)
  92. {
  93. vcpu->stat.instruction_storage_key++;
  94. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
  95. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  96. vcpu->arch.sie_block->gpsw.addr =
  97. __rewind_psw(vcpu->arch.sie_block->gpsw, 4);
  98. VCPU_EVENT(vcpu, 4, "%s", "retrying storage key operation");
  99. return 0;
  100. }
  101. static int handle_tpi(struct kvm_vcpu *vcpu)
  102. {
  103. struct kvm_s390_interrupt_info *inti;
  104. u64 addr;
  105. int cc;
  106. addr = kvm_s390_get_base_disp_s(vcpu);
  107. if (addr & 3)
  108. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  109. cc = 0;
  110. inti = kvm_s390_get_io_int(vcpu->kvm, vcpu->run->s.regs.crs[6], 0);
  111. if (!inti)
  112. goto no_interrupt;
  113. cc = 1;
  114. if (addr) {
  115. /*
  116. * Store the two-word I/O interruption code into the
  117. * provided area.
  118. */
  119. if (put_guest(vcpu, inti->io.subchannel_id, (u16 __user *)addr)
  120. || put_guest(vcpu, inti->io.subchannel_nr, (u16 __user *)(addr + 2))
  121. || put_guest(vcpu, inti->io.io_int_parm, (u32 __user *)(addr + 4)))
  122. return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
  123. } else {
  124. /*
  125. * Store the three-word I/O interruption code into
  126. * the appropriate lowcore area.
  127. */
  128. put_guest(vcpu, inti->io.subchannel_id, (u16 __user *) __LC_SUBCHANNEL_ID);
  129. put_guest(vcpu, inti->io.subchannel_nr, (u16 __user *) __LC_SUBCHANNEL_NR);
  130. put_guest(vcpu, inti->io.io_int_parm, (u32 __user *) __LC_IO_INT_PARM);
  131. put_guest(vcpu, inti->io.io_int_word, (u32 __user *) __LC_IO_INT_WORD);
  132. }
  133. kfree(inti);
  134. no_interrupt:
  135. /* Set condition code and we're done. */
  136. kvm_s390_set_psw_cc(vcpu, cc);
  137. return 0;
  138. }
  139. static int handle_tsch(struct kvm_vcpu *vcpu)
  140. {
  141. struct kvm_s390_interrupt_info *inti;
  142. inti = kvm_s390_get_io_int(vcpu->kvm, 0,
  143. vcpu->run->s.regs.gprs[1]);
  144. /*
  145. * Prepare exit to userspace.
  146. * We indicate whether we dequeued a pending I/O interrupt
  147. * so that userspace can re-inject it if the instruction gets
  148. * a program check. While this may re-order the pending I/O
  149. * interrupts, this is no problem since the priority is kept
  150. * intact.
  151. */
  152. vcpu->run->exit_reason = KVM_EXIT_S390_TSCH;
  153. vcpu->run->s390_tsch.dequeued = !!inti;
  154. if (inti) {
  155. vcpu->run->s390_tsch.subchannel_id = inti->io.subchannel_id;
  156. vcpu->run->s390_tsch.subchannel_nr = inti->io.subchannel_nr;
  157. vcpu->run->s390_tsch.io_int_parm = inti->io.io_int_parm;
  158. vcpu->run->s390_tsch.io_int_word = inti->io.io_int_word;
  159. }
  160. vcpu->run->s390_tsch.ipb = vcpu->arch.sie_block->ipb;
  161. kfree(inti);
  162. return -EREMOTE;
  163. }
  164. static int handle_io_inst(struct kvm_vcpu *vcpu)
  165. {
  166. VCPU_EVENT(vcpu, 4, "%s", "I/O instruction");
  167. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
  168. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  169. if (vcpu->kvm->arch.css_support) {
  170. /*
  171. * Most I/O instructions will be handled by userspace.
  172. * Exceptions are tpi and the interrupt portion of tsch.
  173. */
  174. if (vcpu->arch.sie_block->ipa == 0xb236)
  175. return handle_tpi(vcpu);
  176. if (vcpu->arch.sie_block->ipa == 0xb235)
  177. return handle_tsch(vcpu);
  178. /* Handle in userspace. */
  179. return -EOPNOTSUPP;
  180. } else {
  181. /*
  182. * Set condition code 3 to stop the guest from issueing channel
  183. * I/O instructions.
  184. */
  185. kvm_s390_set_psw_cc(vcpu, 3);
  186. return 0;
  187. }
  188. }
  189. static int handle_stfl(struct kvm_vcpu *vcpu)
  190. {
  191. int rc;
  192. vcpu->stat.instruction_stfl++;
  193. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
  194. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  195. rc = copy_to_guest(vcpu, offsetof(struct _lowcore, stfl_fac_list),
  196. vfacilities, 4);
  197. if (rc)
  198. return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
  199. VCPU_EVENT(vcpu, 5, "store facility list value %x",
  200. *(unsigned int *) vfacilities);
  201. trace_kvm_s390_handle_stfl(vcpu, *(unsigned int *) vfacilities);
  202. return 0;
  203. }
  204. static void handle_new_psw(struct kvm_vcpu *vcpu)
  205. {
  206. /* Check whether the new psw is enabled for machine checks. */
  207. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_MCHECK)
  208. kvm_s390_deliver_pending_machine_checks(vcpu);
  209. }
  210. #define PSW_MASK_ADDR_MODE (PSW_MASK_EA | PSW_MASK_BA)
  211. #define PSW_MASK_UNASSIGNED 0xb80800fe7fffffffUL
  212. #define PSW_ADDR_24 0x0000000000ffffffUL
  213. #define PSW_ADDR_31 0x000000007fffffffUL
  214. static int is_valid_psw(psw_t *psw) {
  215. if (psw->mask & PSW_MASK_UNASSIGNED)
  216. return 0;
  217. if ((psw->mask & PSW_MASK_ADDR_MODE) == PSW_MASK_BA) {
  218. if (psw->addr & ~PSW_ADDR_31)
  219. return 0;
  220. }
  221. if (!(psw->mask & PSW_MASK_ADDR_MODE) && (psw->addr & ~PSW_ADDR_24))
  222. return 0;
  223. if ((psw->mask & PSW_MASK_ADDR_MODE) == PSW_MASK_EA)
  224. return 0;
  225. return 1;
  226. }
  227. int kvm_s390_handle_lpsw(struct kvm_vcpu *vcpu)
  228. {
  229. psw_t *gpsw = &vcpu->arch.sie_block->gpsw;
  230. psw_compat_t new_psw;
  231. u64 addr;
  232. if (gpsw->mask & PSW_MASK_PSTATE)
  233. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  234. addr = kvm_s390_get_base_disp_s(vcpu);
  235. if (addr & 7)
  236. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  237. if (copy_from_guest(vcpu, &new_psw, addr, sizeof(new_psw)))
  238. return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
  239. if (!(new_psw.mask & PSW32_MASK_BASE))
  240. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  241. gpsw->mask = (new_psw.mask & ~PSW32_MASK_BASE) << 32;
  242. gpsw->mask |= new_psw.addr & PSW32_ADDR_AMODE;
  243. gpsw->addr = new_psw.addr & ~PSW32_ADDR_AMODE;
  244. if (!is_valid_psw(gpsw))
  245. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  246. handle_new_psw(vcpu);
  247. return 0;
  248. }
  249. static int handle_lpswe(struct kvm_vcpu *vcpu)
  250. {
  251. psw_t new_psw;
  252. u64 addr;
  253. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
  254. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  255. addr = kvm_s390_get_base_disp_s(vcpu);
  256. if (addr & 7)
  257. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  258. if (copy_from_guest(vcpu, &new_psw, addr, sizeof(new_psw)))
  259. return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
  260. vcpu->arch.sie_block->gpsw = new_psw;
  261. if (!is_valid_psw(&vcpu->arch.sie_block->gpsw))
  262. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  263. handle_new_psw(vcpu);
  264. return 0;
  265. }
  266. static int handle_stidp(struct kvm_vcpu *vcpu)
  267. {
  268. u64 operand2;
  269. vcpu->stat.instruction_stidp++;
  270. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
  271. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  272. operand2 = kvm_s390_get_base_disp_s(vcpu);
  273. if (operand2 & 7)
  274. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  275. if (put_guest(vcpu, vcpu->arch.stidp_data, (u64 __user *)operand2))
  276. return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
  277. VCPU_EVENT(vcpu, 5, "%s", "store cpu id");
  278. return 0;
  279. }
  280. static void handle_stsi_3_2_2(struct kvm_vcpu *vcpu, struct sysinfo_3_2_2 *mem)
  281. {
  282. struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
  283. int cpus = 0;
  284. int n;
  285. spin_lock(&fi->lock);
  286. for (n = 0; n < KVM_MAX_VCPUS; n++)
  287. if (fi->local_int[n])
  288. cpus++;
  289. spin_unlock(&fi->lock);
  290. /* deal with other level 3 hypervisors */
  291. if (stsi(mem, 3, 2, 2))
  292. mem->count = 0;
  293. if (mem->count < 8)
  294. mem->count++;
  295. for (n = mem->count - 1; n > 0 ; n--)
  296. memcpy(&mem->vm[n], &mem->vm[n - 1], sizeof(mem->vm[0]));
  297. mem->vm[0].cpus_total = cpus;
  298. mem->vm[0].cpus_configured = cpus;
  299. mem->vm[0].cpus_standby = 0;
  300. mem->vm[0].cpus_reserved = 0;
  301. mem->vm[0].caf = 1000;
  302. memcpy(mem->vm[0].name, "KVMguest", 8);
  303. ASCEBC(mem->vm[0].name, 8);
  304. memcpy(mem->vm[0].cpi, "KVM/Linux ", 16);
  305. ASCEBC(mem->vm[0].cpi, 16);
  306. }
  307. static int handle_stsi(struct kvm_vcpu *vcpu)
  308. {
  309. int fc = (vcpu->run->s.regs.gprs[0] & 0xf0000000) >> 28;
  310. int sel1 = vcpu->run->s.regs.gprs[0] & 0xff;
  311. int sel2 = vcpu->run->s.regs.gprs[1] & 0xffff;
  312. unsigned long mem = 0;
  313. u64 operand2;
  314. int rc = 0;
  315. vcpu->stat.instruction_stsi++;
  316. VCPU_EVENT(vcpu, 4, "stsi: fc: %x sel1: %x sel2: %x", fc, sel1, sel2);
  317. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
  318. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  319. if (fc > 3) {
  320. kvm_s390_set_psw_cc(vcpu, 3);
  321. return 0;
  322. }
  323. if (vcpu->run->s.regs.gprs[0] & 0x0fffff00
  324. || vcpu->run->s.regs.gprs[1] & 0xffff0000)
  325. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  326. if (fc == 0) {
  327. vcpu->run->s.regs.gprs[0] = 3 << 28;
  328. kvm_s390_set_psw_cc(vcpu, 0);
  329. return 0;
  330. }
  331. operand2 = kvm_s390_get_base_disp_s(vcpu);
  332. if (operand2 & 0xfff)
  333. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  334. switch (fc) {
  335. case 1: /* same handling for 1 and 2 */
  336. case 2:
  337. mem = get_zeroed_page(GFP_KERNEL);
  338. if (!mem)
  339. goto out_no_data;
  340. if (stsi((void *) mem, fc, sel1, sel2))
  341. goto out_no_data;
  342. break;
  343. case 3:
  344. if (sel1 != 2 || sel2 != 2)
  345. goto out_no_data;
  346. mem = get_zeroed_page(GFP_KERNEL);
  347. if (!mem)
  348. goto out_no_data;
  349. handle_stsi_3_2_2(vcpu, (void *) mem);
  350. break;
  351. }
  352. if (copy_to_guest_absolute(vcpu, operand2, (void *) mem, PAGE_SIZE)) {
  353. rc = kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
  354. goto out_exception;
  355. }
  356. trace_kvm_s390_handle_stsi(vcpu, fc, sel1, sel2, operand2);
  357. free_page(mem);
  358. kvm_s390_set_psw_cc(vcpu, 0);
  359. vcpu->run->s.regs.gprs[0] = 0;
  360. return 0;
  361. out_no_data:
  362. kvm_s390_set_psw_cc(vcpu, 3);
  363. out_exception:
  364. free_page(mem);
  365. return rc;
  366. }
  367. static const intercept_handler_t b2_handlers[256] = {
  368. [0x02] = handle_stidp,
  369. [0x10] = handle_set_prefix,
  370. [0x11] = handle_store_prefix,
  371. [0x12] = handle_store_cpu_address,
  372. [0x29] = handle_skey,
  373. [0x2a] = handle_skey,
  374. [0x2b] = handle_skey,
  375. [0x30] = handle_io_inst,
  376. [0x31] = handle_io_inst,
  377. [0x32] = handle_io_inst,
  378. [0x33] = handle_io_inst,
  379. [0x34] = handle_io_inst,
  380. [0x35] = handle_io_inst,
  381. [0x36] = handle_io_inst,
  382. [0x37] = handle_io_inst,
  383. [0x38] = handle_io_inst,
  384. [0x39] = handle_io_inst,
  385. [0x3a] = handle_io_inst,
  386. [0x3b] = handle_io_inst,
  387. [0x3c] = handle_io_inst,
  388. [0x5f] = handle_io_inst,
  389. [0x74] = handle_io_inst,
  390. [0x76] = handle_io_inst,
  391. [0x7d] = handle_stsi,
  392. [0xb1] = handle_stfl,
  393. [0xb2] = handle_lpswe,
  394. };
  395. int kvm_s390_handle_b2(struct kvm_vcpu *vcpu)
  396. {
  397. intercept_handler_t handler;
  398. /*
  399. * A lot of B2 instructions are priviledged. Here we check for
  400. * the privileged ones, that we can handle in the kernel.
  401. * Anything else goes to userspace.
  402. */
  403. handler = b2_handlers[vcpu->arch.sie_block->ipa & 0x00ff];
  404. if (handler)
  405. return handler(vcpu);
  406. return -EOPNOTSUPP;
  407. }
  408. static int handle_epsw(struct kvm_vcpu *vcpu)
  409. {
  410. int reg1, reg2;
  411. kvm_s390_get_regs_rre(vcpu, &reg1, &reg2);
  412. /* This basically extracts the mask half of the psw. */
  413. vcpu->run->s.regs.gprs[reg1] &= 0xffffffff00000000UL;
  414. vcpu->run->s.regs.gprs[reg1] |= vcpu->arch.sie_block->gpsw.mask >> 32;
  415. if (reg2) {
  416. vcpu->run->s.regs.gprs[reg2] &= 0xffffffff00000000UL;
  417. vcpu->run->s.regs.gprs[reg2] |=
  418. vcpu->arch.sie_block->gpsw.mask & 0x00000000ffffffffUL;
  419. }
  420. return 0;
  421. }
  422. #define PFMF_RESERVED 0xfffc0101UL
  423. #define PFMF_SK 0x00020000UL
  424. #define PFMF_CF 0x00010000UL
  425. #define PFMF_UI 0x00008000UL
  426. #define PFMF_FSC 0x00007000UL
  427. #define PFMF_NQ 0x00000800UL
  428. #define PFMF_MR 0x00000400UL
  429. #define PFMF_MC 0x00000200UL
  430. #define PFMF_KEY 0x000000feUL
  431. static int handle_pfmf(struct kvm_vcpu *vcpu)
  432. {
  433. int reg1, reg2;
  434. unsigned long start, end;
  435. vcpu->stat.instruction_pfmf++;
  436. kvm_s390_get_regs_rre(vcpu, &reg1, &reg2);
  437. if (!MACHINE_HAS_PFMF)
  438. return kvm_s390_inject_program_int(vcpu, PGM_OPERATION);
  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->run->s.regs.gprs[reg1] & PFMF_RESERVED)
  442. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  443. /* Only provide non-quiescing support if the host supports it */
  444. if (vcpu->run->s.regs.gprs[reg1] & PFMF_NQ && !test_facility(14))
  445. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  446. /* No support for conditional-SSKE */
  447. if (vcpu->run->s.regs.gprs[reg1] & (PFMF_MR | PFMF_MC))
  448. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  449. start = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK;
  450. switch (vcpu->run->s.regs.gprs[reg1] & PFMF_FSC) {
  451. case 0x00000000:
  452. end = (start + (1UL << 12)) & ~((1UL << 12) - 1);
  453. break;
  454. case 0x00001000:
  455. end = (start + (1UL << 20)) & ~((1UL << 20) - 1);
  456. break;
  457. /* We dont support EDAT2
  458. case 0x00002000:
  459. end = (start + (1UL << 31)) & ~((1UL << 31) - 1);
  460. break;*/
  461. default:
  462. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  463. }
  464. while (start < end) {
  465. unsigned long useraddr;
  466. useraddr = gmap_translate(start, vcpu->arch.gmap);
  467. if (IS_ERR((void *)useraddr))
  468. return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
  469. if (vcpu->run->s.regs.gprs[reg1] & PFMF_CF) {
  470. if (clear_user((void __user *)useraddr, PAGE_SIZE))
  471. return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
  472. }
  473. if (vcpu->run->s.regs.gprs[reg1] & PFMF_SK) {
  474. if (set_guest_storage_key(current->mm, useraddr,
  475. vcpu->run->s.regs.gprs[reg1] & PFMF_KEY,
  476. vcpu->run->s.regs.gprs[reg1] & PFMF_NQ))
  477. return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
  478. }
  479. start += PAGE_SIZE;
  480. }
  481. if (vcpu->run->s.regs.gprs[reg1] & PFMF_FSC)
  482. vcpu->run->s.regs.gprs[reg2] = end;
  483. return 0;
  484. }
  485. static const intercept_handler_t b9_handlers[256] = {
  486. [0x8d] = handle_epsw,
  487. [0x9c] = handle_io_inst,
  488. [0xaf] = handle_pfmf,
  489. };
  490. int kvm_s390_handle_b9(struct kvm_vcpu *vcpu)
  491. {
  492. intercept_handler_t handler;
  493. /* This is handled just as for the B2 instructions. */
  494. handler = b9_handlers[vcpu->arch.sie_block->ipa & 0x00ff];
  495. if (handler)
  496. return handler(vcpu);
  497. return -EOPNOTSUPP;
  498. }
  499. int kvm_s390_handle_lctl(struct kvm_vcpu *vcpu)
  500. {
  501. int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
  502. int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
  503. u64 useraddr;
  504. u32 val = 0;
  505. int reg, rc;
  506. vcpu->stat.instruction_lctl++;
  507. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
  508. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  509. useraddr = kvm_s390_get_base_disp_rs(vcpu);
  510. if (useraddr & 3)
  511. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  512. VCPU_EVENT(vcpu, 5, "lctl r1:%x, r3:%x, addr:%llx", reg1, reg3,
  513. useraddr);
  514. trace_kvm_s390_handle_lctl(vcpu, 0, reg1, reg3, useraddr);
  515. reg = reg1;
  516. do {
  517. rc = get_guest(vcpu, val, (u32 __user *) useraddr);
  518. if (rc)
  519. return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
  520. vcpu->arch.sie_block->gcr[reg] &= 0xffffffff00000000ul;
  521. vcpu->arch.sie_block->gcr[reg] |= val;
  522. useraddr += 4;
  523. if (reg == reg3)
  524. break;
  525. reg = (reg + 1) % 16;
  526. } while (1);
  527. return 0;
  528. }
  529. static int handle_lctlg(struct kvm_vcpu *vcpu)
  530. {
  531. int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
  532. int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
  533. u64 useraddr;
  534. int reg, rc;
  535. vcpu->stat.instruction_lctlg++;
  536. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
  537. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  538. useraddr = kvm_s390_get_base_disp_rsy(vcpu);
  539. if (useraddr & 7)
  540. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  541. reg = reg1;
  542. VCPU_EVENT(vcpu, 5, "lctlg r1:%x, r3:%x, addr:%llx", reg1, reg3,
  543. useraddr);
  544. trace_kvm_s390_handle_lctl(vcpu, 1, reg1, reg3, useraddr);
  545. do {
  546. rc = get_guest(vcpu, vcpu->arch.sie_block->gcr[reg],
  547. (u64 __user *) useraddr);
  548. if (rc)
  549. return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
  550. useraddr += 8;
  551. if (reg == reg3)
  552. break;
  553. reg = (reg + 1) % 16;
  554. } while (1);
  555. return 0;
  556. }
  557. static const intercept_handler_t eb_handlers[256] = {
  558. [0x2f] = handle_lctlg,
  559. [0x8a] = handle_io_inst,
  560. };
  561. int kvm_s390_handle_eb(struct kvm_vcpu *vcpu)
  562. {
  563. intercept_handler_t handler;
  564. handler = eb_handlers[vcpu->arch.sie_block->ipb & 0xff];
  565. if (handler)
  566. return handler(vcpu);
  567. return -EOPNOTSUPP;
  568. }
  569. static int handle_tprot(struct kvm_vcpu *vcpu)
  570. {
  571. u64 address1, address2;
  572. struct vm_area_struct *vma;
  573. unsigned long user_address;
  574. vcpu->stat.instruction_tprot++;
  575. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
  576. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  577. kvm_s390_get_base_disp_sse(vcpu, &address1, &address2);
  578. /* we only handle the Linux memory detection case:
  579. * access key == 0
  580. * guest DAT == off
  581. * everything else goes to userspace. */
  582. if (address2 & 0xf0)
  583. return -EOPNOTSUPP;
  584. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_DAT)
  585. return -EOPNOTSUPP;
  586. down_read(&current->mm->mmap_sem);
  587. user_address = __gmap_translate(address1, vcpu->arch.gmap);
  588. if (IS_ERR_VALUE(user_address))
  589. goto out_inject;
  590. vma = find_vma(current->mm, user_address);
  591. if (!vma)
  592. goto out_inject;
  593. vcpu->arch.sie_block->gpsw.mask &= ~(3ul << 44);
  594. if (!(vma->vm_flags & VM_WRITE) && (vma->vm_flags & VM_READ))
  595. vcpu->arch.sie_block->gpsw.mask |= (1ul << 44);
  596. if (!(vma->vm_flags & VM_WRITE) && !(vma->vm_flags & VM_READ))
  597. vcpu->arch.sie_block->gpsw.mask |= (2ul << 44);
  598. up_read(&current->mm->mmap_sem);
  599. return 0;
  600. out_inject:
  601. up_read(&current->mm->mmap_sem);
  602. return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
  603. }
  604. int kvm_s390_handle_e5(struct kvm_vcpu *vcpu)
  605. {
  606. /* For e5xx... instructions we only handle TPROT */
  607. if ((vcpu->arch.sie_block->ipa & 0x00ff) == 0x01)
  608. return handle_tprot(vcpu);
  609. return -EOPNOTSUPP;
  610. }
  611. static int handle_sckpf(struct kvm_vcpu *vcpu)
  612. {
  613. u32 value;
  614. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
  615. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  616. if (vcpu->run->s.regs.gprs[0] & 0x00000000ffff0000)
  617. return kvm_s390_inject_program_int(vcpu,
  618. PGM_SPECIFICATION);
  619. value = vcpu->run->s.regs.gprs[0] & 0x000000000000ffff;
  620. vcpu->arch.sie_block->todpr = value;
  621. return 0;
  622. }
  623. static const intercept_handler_t x01_handlers[256] = {
  624. [0x07] = handle_sckpf,
  625. };
  626. int kvm_s390_handle_01(struct kvm_vcpu *vcpu)
  627. {
  628. intercept_handler_t handler;
  629. handler = x01_handlers[vcpu->arch.sie_block->ipa & 0x00ff];
  630. if (handler)
  631. return handler(vcpu);
  632. return -EOPNOTSUPP;
  633. }