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/current.h>
  19. #include <asm/debug.h>
  20. #include <asm/ebcdic.h>
  21. #include <asm/sysinfo.h>
  22. #include <asm/pgtable.h>
  23. #include <asm/pgalloc.h>
  24. #include <asm/io.h>
  25. #include <asm/ptrace.h>
  26. #include <asm/compat.h>
  27. #include "gaccess.h"
  28. #include "kvm-s390.h"
  29. #include "trace.h"
  30. static int handle_set_prefix(struct kvm_vcpu *vcpu)
  31. {
  32. u64 operand2;
  33. u32 address = 0;
  34. u8 tmp;
  35. vcpu->stat.instruction_spx++;
  36. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
  37. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  38. operand2 = kvm_s390_get_base_disp_s(vcpu);
  39. /* must be word boundary */
  40. if (operand2 & 3)
  41. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  42. /* get the value */
  43. if (get_guest(vcpu, address, (u32 __user *) operand2))
  44. return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
  45. address = address & 0x7fffe000u;
  46. /* make sure that the new value is valid memory */
  47. if (copy_from_guest_absolute(vcpu, &tmp, address, 1) ||
  48. (copy_from_guest_absolute(vcpu, &tmp, address + PAGE_SIZE, 1)))
  49. return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
  50. kvm_s390_set_prefix(vcpu, address);
  51. VCPU_EVENT(vcpu, 5, "setting prefix to %x", address);
  52. trace_kvm_s390_handle_prefix(vcpu, 1, address);
  53. return 0;
  54. }
  55. static int handle_store_prefix(struct kvm_vcpu *vcpu)
  56. {
  57. u64 operand2;
  58. u32 address;
  59. vcpu->stat.instruction_stpx++;
  60. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
  61. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  62. operand2 = kvm_s390_get_base_disp_s(vcpu);
  63. /* must be word boundary */
  64. if (operand2 & 3)
  65. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  66. address = vcpu->arch.sie_block->prefix;
  67. address = address & 0x7fffe000u;
  68. /* get the value */
  69. if (put_guest(vcpu, address, (u32 __user *)operand2))
  70. return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
  71. VCPU_EVENT(vcpu, 5, "storing prefix to %x", address);
  72. trace_kvm_s390_handle_prefix(vcpu, 0, address);
  73. return 0;
  74. }
  75. static int handle_store_cpu_address(struct kvm_vcpu *vcpu)
  76. {
  77. u64 useraddr;
  78. vcpu->stat.instruction_stap++;
  79. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
  80. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  81. useraddr = kvm_s390_get_base_disp_s(vcpu);
  82. if (useraddr & 1)
  83. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  84. if (put_guest(vcpu, vcpu->vcpu_id, (u16 __user *)useraddr))
  85. return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
  86. VCPU_EVENT(vcpu, 5, "storing cpu address to %llx", useraddr);
  87. trace_kvm_s390_handle_stap(vcpu, useraddr);
  88. return 0;
  89. }
  90. static int handle_skey(struct kvm_vcpu *vcpu)
  91. {
  92. vcpu->stat.instruction_storage_key++;
  93. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
  94. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  95. vcpu->arch.sie_block->gpsw.addr =
  96. __rewind_psw(vcpu->arch.sie_block->gpsw, 4);
  97. VCPU_EVENT(vcpu, 4, "%s", "retrying storage key operation");
  98. return 0;
  99. }
  100. static int handle_tpi(struct kvm_vcpu *vcpu)
  101. {
  102. struct kvm_s390_interrupt_info *inti;
  103. u64 addr;
  104. int cc;
  105. addr = kvm_s390_get_base_disp_s(vcpu);
  106. if (addr & 3)
  107. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  108. cc = 0;
  109. inti = kvm_s390_get_io_int(vcpu->kvm, vcpu->run->s.regs.crs[6], 0);
  110. if (!inti)
  111. goto no_interrupt;
  112. cc = 1;
  113. if (addr) {
  114. /*
  115. * Store the two-word I/O interruption code into the
  116. * provided area.
  117. */
  118. if (put_guest(vcpu, inti->io.subchannel_id, (u16 __user *)addr)
  119. || put_guest(vcpu, inti->io.subchannel_nr, (u16 __user *)(addr + 2))
  120. || put_guest(vcpu, inti->io.io_int_parm, (u32 __user *)(addr + 4)))
  121. return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
  122. } else {
  123. /*
  124. * Store the three-word I/O interruption code into
  125. * the appropriate lowcore area.
  126. */
  127. put_guest(vcpu, inti->io.subchannel_id, (u16 __user *) __LC_SUBCHANNEL_ID);
  128. put_guest(vcpu, inti->io.subchannel_nr, (u16 __user *) __LC_SUBCHANNEL_NR);
  129. put_guest(vcpu, inti->io.io_int_parm, (u32 __user *) __LC_IO_INT_PARM);
  130. put_guest(vcpu, inti->io.io_int_word, (u32 __user *) __LC_IO_INT_WORD);
  131. }
  132. kfree(inti);
  133. no_interrupt:
  134. /* Set condition code and we're done. */
  135. kvm_s390_set_psw_cc(vcpu, cc);
  136. return 0;
  137. }
  138. static int handle_tsch(struct kvm_vcpu *vcpu)
  139. {
  140. struct kvm_s390_interrupt_info *inti;
  141. inti = kvm_s390_get_io_int(vcpu->kvm, 0,
  142. vcpu->run->s.regs.gprs[1]);
  143. /*
  144. * Prepare exit to userspace.
  145. * We indicate whether we dequeued a pending I/O interrupt
  146. * so that userspace can re-inject it if the instruction gets
  147. * a program check. While this may re-order the pending I/O
  148. * interrupts, this is no problem since the priority is kept
  149. * intact.
  150. */
  151. vcpu->run->exit_reason = KVM_EXIT_S390_TSCH;
  152. vcpu->run->s390_tsch.dequeued = !!inti;
  153. if (inti) {
  154. vcpu->run->s390_tsch.subchannel_id = inti->io.subchannel_id;
  155. vcpu->run->s390_tsch.subchannel_nr = inti->io.subchannel_nr;
  156. vcpu->run->s390_tsch.io_int_parm = inti->io.io_int_parm;
  157. vcpu->run->s390_tsch.io_int_word = inti->io.io_int_word;
  158. }
  159. vcpu->run->s390_tsch.ipb = vcpu->arch.sie_block->ipb;
  160. kfree(inti);
  161. return -EREMOTE;
  162. }
  163. static int handle_io_inst(struct kvm_vcpu *vcpu)
  164. {
  165. VCPU_EVENT(vcpu, 4, "%s", "I/O instruction");
  166. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
  167. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  168. if (vcpu->kvm->arch.css_support) {
  169. /*
  170. * Most I/O instructions will be handled by userspace.
  171. * Exceptions are tpi and the interrupt portion of tsch.
  172. */
  173. if (vcpu->arch.sie_block->ipa == 0xb236)
  174. return handle_tpi(vcpu);
  175. if (vcpu->arch.sie_block->ipa == 0xb235)
  176. return handle_tsch(vcpu);
  177. /* Handle in userspace. */
  178. return -EOPNOTSUPP;
  179. } else {
  180. /*
  181. * Set condition code 3 to stop the guest from issueing channel
  182. * I/O instructions.
  183. */
  184. kvm_s390_set_psw_cc(vcpu, 3);
  185. return 0;
  186. }
  187. }
  188. static int handle_stfl(struct kvm_vcpu *vcpu)
  189. {
  190. int rc;
  191. vcpu->stat.instruction_stfl++;
  192. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
  193. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  194. rc = copy_to_guest(vcpu, offsetof(struct _lowcore, stfl_fac_list),
  195. vfacilities, 4);
  196. if (rc)
  197. return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
  198. VCPU_EVENT(vcpu, 5, "store facility list value %x",
  199. *(unsigned int *) vfacilities);
  200. trace_kvm_s390_handle_stfl(vcpu, *(unsigned int *) vfacilities);
  201. return 0;
  202. }
  203. static void handle_new_psw(struct kvm_vcpu *vcpu)
  204. {
  205. /* Check whether the new psw is enabled for machine checks. */
  206. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_MCHECK)
  207. kvm_s390_deliver_pending_machine_checks(vcpu);
  208. }
  209. #define PSW_MASK_ADDR_MODE (PSW_MASK_EA | PSW_MASK_BA)
  210. #define PSW_MASK_UNASSIGNED 0xb80800fe7fffffffUL
  211. #define PSW_ADDR_24 0x0000000000ffffffUL
  212. #define PSW_ADDR_31 0x000000007fffffffUL
  213. static int is_valid_psw(psw_t *psw) {
  214. if (psw->mask & PSW_MASK_UNASSIGNED)
  215. return 0;
  216. if ((psw->mask & PSW_MASK_ADDR_MODE) == PSW_MASK_BA) {
  217. if (psw->addr & ~PSW_ADDR_31)
  218. return 0;
  219. }
  220. if (!(psw->mask & PSW_MASK_ADDR_MODE) && (psw->addr & ~PSW_ADDR_24))
  221. return 0;
  222. if ((psw->mask & PSW_MASK_ADDR_MODE) == PSW_MASK_EA)
  223. return 0;
  224. return 1;
  225. }
  226. int kvm_s390_handle_lpsw(struct kvm_vcpu *vcpu)
  227. {
  228. psw_t *gpsw = &vcpu->arch.sie_block->gpsw;
  229. psw_compat_t new_psw;
  230. u64 addr;
  231. if (gpsw->mask & PSW_MASK_PSTATE)
  232. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  233. addr = kvm_s390_get_base_disp_s(vcpu);
  234. if (addr & 7)
  235. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  236. if (copy_from_guest(vcpu, &new_psw, addr, sizeof(new_psw)))
  237. return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
  238. if (!(new_psw.mask & PSW32_MASK_BASE))
  239. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  240. gpsw->mask = (new_psw.mask & ~PSW32_MASK_BASE) << 32;
  241. gpsw->mask |= new_psw.addr & PSW32_ADDR_AMODE;
  242. gpsw->addr = new_psw.addr & ~PSW32_ADDR_AMODE;
  243. if (!is_valid_psw(gpsw))
  244. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  245. handle_new_psw(vcpu);
  246. return 0;
  247. }
  248. static int handle_lpswe(struct kvm_vcpu *vcpu)
  249. {
  250. psw_t new_psw;
  251. u64 addr;
  252. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
  253. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  254. addr = kvm_s390_get_base_disp_s(vcpu);
  255. if (addr & 7)
  256. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  257. if (copy_from_guest(vcpu, &new_psw, addr, sizeof(new_psw)))
  258. return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
  259. vcpu->arch.sie_block->gpsw = new_psw;
  260. if (!is_valid_psw(&vcpu->arch.sie_block->gpsw))
  261. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  262. handle_new_psw(vcpu);
  263. return 0;
  264. }
  265. static int handle_stidp(struct kvm_vcpu *vcpu)
  266. {
  267. u64 operand2;
  268. vcpu->stat.instruction_stidp++;
  269. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
  270. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  271. operand2 = kvm_s390_get_base_disp_s(vcpu);
  272. if (operand2 & 7)
  273. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  274. if (put_guest(vcpu, vcpu->arch.stidp_data, (u64 __user *)operand2))
  275. return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
  276. VCPU_EVENT(vcpu, 5, "%s", "store cpu id");
  277. return 0;
  278. }
  279. static void handle_stsi_3_2_2(struct kvm_vcpu *vcpu, struct sysinfo_3_2_2 *mem)
  280. {
  281. struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
  282. int cpus = 0;
  283. int n;
  284. spin_lock(&fi->lock);
  285. for (n = 0; n < KVM_MAX_VCPUS; n++)
  286. if (fi->local_int[n])
  287. cpus++;
  288. spin_unlock(&fi->lock);
  289. /* deal with other level 3 hypervisors */
  290. if (stsi(mem, 3, 2, 2))
  291. mem->count = 0;
  292. if (mem->count < 8)
  293. mem->count++;
  294. for (n = mem->count - 1; n > 0 ; n--)
  295. memcpy(&mem->vm[n], &mem->vm[n - 1], sizeof(mem->vm[0]));
  296. mem->vm[0].cpus_total = cpus;
  297. mem->vm[0].cpus_configured = cpus;
  298. mem->vm[0].cpus_standby = 0;
  299. mem->vm[0].cpus_reserved = 0;
  300. mem->vm[0].caf = 1000;
  301. memcpy(mem->vm[0].name, "KVMguest", 8);
  302. ASCEBC(mem->vm[0].name, 8);
  303. memcpy(mem->vm[0].cpi, "KVM/Linux ", 16);
  304. ASCEBC(mem->vm[0].cpi, 16);
  305. }
  306. static int handle_stsi(struct kvm_vcpu *vcpu)
  307. {
  308. int fc = (vcpu->run->s.regs.gprs[0] & 0xf0000000) >> 28;
  309. int sel1 = vcpu->run->s.regs.gprs[0] & 0xff;
  310. int sel2 = vcpu->run->s.regs.gprs[1] & 0xffff;
  311. unsigned long mem = 0;
  312. u64 operand2;
  313. int rc = 0;
  314. vcpu->stat.instruction_stsi++;
  315. VCPU_EVENT(vcpu, 4, "stsi: fc: %x sel1: %x sel2: %x", fc, sel1, sel2);
  316. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
  317. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  318. if (fc > 3) {
  319. kvm_s390_set_psw_cc(vcpu, 3);
  320. return 0;
  321. }
  322. if (vcpu->run->s.regs.gprs[0] & 0x0fffff00
  323. || vcpu->run->s.regs.gprs[1] & 0xffff0000)
  324. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  325. if (fc == 0) {
  326. vcpu->run->s.regs.gprs[0] = 3 << 28;
  327. kvm_s390_set_psw_cc(vcpu, 0);
  328. return 0;
  329. }
  330. operand2 = kvm_s390_get_base_disp_s(vcpu);
  331. if (operand2 & 0xfff)
  332. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  333. switch (fc) {
  334. case 1: /* same handling for 1 and 2 */
  335. case 2:
  336. mem = get_zeroed_page(GFP_KERNEL);
  337. if (!mem)
  338. goto out_no_data;
  339. if (stsi((void *) mem, fc, sel1, sel2))
  340. goto out_no_data;
  341. break;
  342. case 3:
  343. if (sel1 != 2 || sel2 != 2)
  344. goto out_no_data;
  345. mem = get_zeroed_page(GFP_KERNEL);
  346. if (!mem)
  347. goto out_no_data;
  348. handle_stsi_3_2_2(vcpu, (void *) mem);
  349. break;
  350. }
  351. if (copy_to_guest_absolute(vcpu, operand2, (void *) mem, PAGE_SIZE)) {
  352. rc = kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
  353. goto out_exception;
  354. }
  355. trace_kvm_s390_handle_stsi(vcpu, fc, sel1, sel2, operand2);
  356. free_page(mem);
  357. kvm_s390_set_psw_cc(vcpu, 0);
  358. vcpu->run->s.regs.gprs[0] = 0;
  359. return 0;
  360. out_no_data:
  361. kvm_s390_set_psw_cc(vcpu, 3);
  362. out_exception:
  363. free_page(mem);
  364. return rc;
  365. }
  366. static const intercept_handler_t b2_handlers[256] = {
  367. [0x02] = handle_stidp,
  368. [0x10] = handle_set_prefix,
  369. [0x11] = handle_store_prefix,
  370. [0x12] = handle_store_cpu_address,
  371. [0x29] = handle_skey,
  372. [0x2a] = handle_skey,
  373. [0x2b] = handle_skey,
  374. [0x30] = handle_io_inst,
  375. [0x31] = handle_io_inst,
  376. [0x32] = handle_io_inst,
  377. [0x33] = handle_io_inst,
  378. [0x34] = handle_io_inst,
  379. [0x35] = handle_io_inst,
  380. [0x36] = handle_io_inst,
  381. [0x37] = handle_io_inst,
  382. [0x38] = handle_io_inst,
  383. [0x39] = handle_io_inst,
  384. [0x3a] = handle_io_inst,
  385. [0x3b] = handle_io_inst,
  386. [0x3c] = handle_io_inst,
  387. [0x5f] = handle_io_inst,
  388. [0x74] = handle_io_inst,
  389. [0x76] = handle_io_inst,
  390. [0x7d] = handle_stsi,
  391. [0xb1] = handle_stfl,
  392. [0xb2] = handle_lpswe,
  393. };
  394. int kvm_s390_handle_b2(struct kvm_vcpu *vcpu)
  395. {
  396. intercept_handler_t handler;
  397. /*
  398. * A lot of B2 instructions are priviledged. Here we check for
  399. * the privileged ones, that we can handle in the kernel.
  400. * Anything else goes to userspace.
  401. */
  402. handler = b2_handlers[vcpu->arch.sie_block->ipa & 0x00ff];
  403. if (handler)
  404. return handler(vcpu);
  405. return -EOPNOTSUPP;
  406. }
  407. static int handle_epsw(struct kvm_vcpu *vcpu)
  408. {
  409. int reg1, reg2;
  410. kvm_s390_get_regs_rre(vcpu, &reg1, &reg2);
  411. /* This basically extracts the mask half of the psw. */
  412. vcpu->run->s.regs.gprs[reg1] &= 0xffffffff00000000UL;
  413. vcpu->run->s.regs.gprs[reg1] |= vcpu->arch.sie_block->gpsw.mask >> 32;
  414. if (reg2) {
  415. vcpu->run->s.regs.gprs[reg2] &= 0xffffffff00000000UL;
  416. vcpu->run->s.regs.gprs[reg2] |=
  417. vcpu->arch.sie_block->gpsw.mask & 0x00000000ffffffffUL;
  418. }
  419. return 0;
  420. }
  421. #define PFMF_RESERVED 0xfffc0101UL
  422. #define PFMF_SK 0x00020000UL
  423. #define PFMF_CF 0x00010000UL
  424. #define PFMF_UI 0x00008000UL
  425. #define PFMF_FSC 0x00007000UL
  426. #define PFMF_NQ 0x00000800UL
  427. #define PFMF_MR 0x00000400UL
  428. #define PFMF_MC 0x00000200UL
  429. #define PFMF_KEY 0x000000feUL
  430. static int handle_pfmf(struct kvm_vcpu *vcpu)
  431. {
  432. int reg1, reg2;
  433. unsigned long start, end;
  434. vcpu->stat.instruction_pfmf++;
  435. kvm_s390_get_regs_rre(vcpu, &reg1, &reg2);
  436. if (!MACHINE_HAS_PFMF)
  437. return kvm_s390_inject_program_int(vcpu, PGM_OPERATION);
  438. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
  439. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  440. if (vcpu->run->s.regs.gprs[reg1] & PFMF_RESERVED)
  441. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  442. /* Only provide non-quiescing support if the host supports it */
  443. if (vcpu->run->s.regs.gprs[reg1] & PFMF_NQ &&
  444. S390_lowcore.stfl_fac_list & 0x00020000)
  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. }