priv.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687
  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. vcpu->arch.sie_block->gpsw.mask &= ~(3ul << 44);
  136. vcpu->arch.sie_block->gpsw.mask |= (cc & 3ul) << 44;
  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. vcpu->arch.sie_block->gpsw.mask &= ~(3ul << 44);
  186. vcpu->arch.sie_block->gpsw.mask |= (3 & 3ul) << 44;
  187. return 0;
  188. }
  189. }
  190. static int handle_stfl(struct kvm_vcpu *vcpu)
  191. {
  192. unsigned int facility_list;
  193. int rc;
  194. vcpu->stat.instruction_stfl++;
  195. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
  196. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  197. /* only pass the facility bits, which we can handle */
  198. facility_list = S390_lowcore.stfl_fac_list & 0xff82fff3;
  199. rc = copy_to_guest(vcpu, offsetof(struct _lowcore, stfl_fac_list),
  200. &facility_list, sizeof(facility_list));
  201. if (rc)
  202. return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
  203. VCPU_EVENT(vcpu, 5, "store facility list value %x", facility_list);
  204. trace_kvm_s390_handle_stfl(vcpu, facility_list);
  205. return 0;
  206. }
  207. static void handle_new_psw(struct kvm_vcpu *vcpu)
  208. {
  209. /* Check whether the new psw is enabled for machine checks. */
  210. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_MCHECK)
  211. kvm_s390_deliver_pending_machine_checks(vcpu);
  212. }
  213. #define PSW_MASK_ADDR_MODE (PSW_MASK_EA | PSW_MASK_BA)
  214. #define PSW_MASK_UNASSIGNED 0xb80800fe7fffffffUL
  215. #define PSW_ADDR_24 0x0000000000ffffffUL
  216. #define PSW_ADDR_31 0x000000007fffffffUL
  217. static int is_valid_psw(psw_t *psw) {
  218. if (psw->mask & PSW_MASK_UNASSIGNED)
  219. return 0;
  220. if ((psw->mask & PSW_MASK_ADDR_MODE) == PSW_MASK_BA) {
  221. if (psw->addr & ~PSW_ADDR_31)
  222. return 0;
  223. }
  224. if (!(psw->mask & PSW_MASK_ADDR_MODE) && (psw->addr & ~PSW_ADDR_24))
  225. return 0;
  226. if ((psw->mask & PSW_MASK_ADDR_MODE) == PSW_MASK_EA)
  227. return 0;
  228. return 1;
  229. }
  230. int kvm_s390_handle_lpsw(struct kvm_vcpu *vcpu)
  231. {
  232. psw_t *gpsw = &vcpu->arch.sie_block->gpsw;
  233. psw_compat_t new_psw;
  234. u64 addr;
  235. if (gpsw->mask & PSW_MASK_PSTATE)
  236. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  237. addr = kvm_s390_get_base_disp_s(vcpu);
  238. if (addr & 7)
  239. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  240. if (copy_from_guest(vcpu, &new_psw, addr, sizeof(new_psw)))
  241. return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
  242. if (!(new_psw.mask & PSW32_MASK_BASE))
  243. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  244. gpsw->mask = (new_psw.mask & ~PSW32_MASK_BASE) << 32;
  245. gpsw->mask |= new_psw.addr & PSW32_ADDR_AMODE;
  246. gpsw->addr = new_psw.addr & ~PSW32_ADDR_AMODE;
  247. if (!is_valid_psw(gpsw))
  248. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  249. handle_new_psw(vcpu);
  250. return 0;
  251. }
  252. static int handle_lpswe(struct kvm_vcpu *vcpu)
  253. {
  254. psw_t new_psw;
  255. u64 addr;
  256. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
  257. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  258. addr = kvm_s390_get_base_disp_s(vcpu);
  259. if (addr & 7)
  260. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  261. if (copy_from_guest(vcpu, &new_psw, addr, sizeof(new_psw)))
  262. return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
  263. vcpu->arch.sie_block->gpsw = new_psw;
  264. if (!is_valid_psw(&vcpu->arch.sie_block->gpsw))
  265. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  266. handle_new_psw(vcpu);
  267. return 0;
  268. }
  269. static int handle_stidp(struct kvm_vcpu *vcpu)
  270. {
  271. u64 operand2;
  272. vcpu->stat.instruction_stidp++;
  273. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
  274. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  275. operand2 = kvm_s390_get_base_disp_s(vcpu);
  276. if (operand2 & 7)
  277. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  278. if (put_guest(vcpu, vcpu->arch.stidp_data, (u64 __user *)operand2))
  279. return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
  280. VCPU_EVENT(vcpu, 5, "%s", "store cpu id");
  281. return 0;
  282. }
  283. static void handle_stsi_3_2_2(struct kvm_vcpu *vcpu, struct sysinfo_3_2_2 *mem)
  284. {
  285. struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
  286. int cpus = 0;
  287. int n;
  288. spin_lock(&fi->lock);
  289. for (n = 0; n < KVM_MAX_VCPUS; n++)
  290. if (fi->local_int[n])
  291. cpus++;
  292. spin_unlock(&fi->lock);
  293. /* deal with other level 3 hypervisors */
  294. if (stsi(mem, 3, 2, 2))
  295. mem->count = 0;
  296. if (mem->count < 8)
  297. mem->count++;
  298. for (n = mem->count - 1; n > 0 ; n--)
  299. memcpy(&mem->vm[n], &mem->vm[n - 1], sizeof(mem->vm[0]));
  300. mem->vm[0].cpus_total = cpus;
  301. mem->vm[0].cpus_configured = cpus;
  302. mem->vm[0].cpus_standby = 0;
  303. mem->vm[0].cpus_reserved = 0;
  304. mem->vm[0].caf = 1000;
  305. memcpy(mem->vm[0].name, "KVMguest", 8);
  306. ASCEBC(mem->vm[0].name, 8);
  307. memcpy(mem->vm[0].cpi, "KVM/Linux ", 16);
  308. ASCEBC(mem->vm[0].cpi, 16);
  309. }
  310. static int handle_stsi(struct kvm_vcpu *vcpu)
  311. {
  312. int fc = (vcpu->run->s.regs.gprs[0] & 0xf0000000) >> 28;
  313. int sel1 = vcpu->run->s.regs.gprs[0] & 0xff;
  314. int sel2 = vcpu->run->s.regs.gprs[1] & 0xffff;
  315. unsigned long mem = 0;
  316. u64 operand2;
  317. int rc = 0;
  318. vcpu->stat.instruction_stsi++;
  319. VCPU_EVENT(vcpu, 4, "stsi: fc: %x sel1: %x sel2: %x", fc, sel1, sel2);
  320. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
  321. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  322. operand2 = kvm_s390_get_base_disp_s(vcpu);
  323. if (operand2 & 0xfff && fc > 0)
  324. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  325. switch (fc) {
  326. case 0:
  327. vcpu->run->s.regs.gprs[0] = 3 << 28;
  328. vcpu->arch.sie_block->gpsw.mask &= ~(3ul << 44);
  329. return 0;
  330. case 1: /* same handling for 1 and 2 */
  331. case 2:
  332. mem = get_zeroed_page(GFP_KERNEL);
  333. if (!mem)
  334. goto out_no_data;
  335. if (stsi((void *) mem, fc, sel1, sel2))
  336. goto out_no_data;
  337. break;
  338. case 3:
  339. if (sel1 != 2 || sel2 != 2)
  340. goto out_no_data;
  341. mem = get_zeroed_page(GFP_KERNEL);
  342. if (!mem)
  343. goto out_no_data;
  344. handle_stsi_3_2_2(vcpu, (void *) mem);
  345. break;
  346. default:
  347. goto out_no_data;
  348. }
  349. if (copy_to_guest_absolute(vcpu, operand2, (void *) mem, PAGE_SIZE)) {
  350. rc = kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
  351. goto out_exception;
  352. }
  353. trace_kvm_s390_handle_stsi(vcpu, fc, sel1, sel2, operand2);
  354. free_page(mem);
  355. vcpu->arch.sie_block->gpsw.mask &= ~(3ul << 44);
  356. vcpu->run->s.regs.gprs[0] = 0;
  357. return 0;
  358. out_no_data:
  359. /* condition code 3 */
  360. vcpu->arch.sie_block->gpsw.mask |= 3ul << 44;
  361. out_exception:
  362. free_page(mem);
  363. return rc;
  364. }
  365. static const intercept_handler_t b2_handlers[256] = {
  366. [0x02] = handle_stidp,
  367. [0x10] = handle_set_prefix,
  368. [0x11] = handle_store_prefix,
  369. [0x12] = handle_store_cpu_address,
  370. [0x29] = handle_skey,
  371. [0x2a] = handle_skey,
  372. [0x2b] = handle_skey,
  373. [0x30] = handle_io_inst,
  374. [0x31] = handle_io_inst,
  375. [0x32] = handle_io_inst,
  376. [0x33] = handle_io_inst,
  377. [0x34] = handle_io_inst,
  378. [0x35] = handle_io_inst,
  379. [0x36] = handle_io_inst,
  380. [0x37] = handle_io_inst,
  381. [0x38] = handle_io_inst,
  382. [0x39] = handle_io_inst,
  383. [0x3a] = handle_io_inst,
  384. [0x3b] = handle_io_inst,
  385. [0x3c] = handle_io_inst,
  386. [0x5f] = handle_io_inst,
  387. [0x74] = handle_io_inst,
  388. [0x76] = handle_io_inst,
  389. [0x7d] = handle_stsi,
  390. [0xb1] = handle_stfl,
  391. [0xb2] = handle_lpswe,
  392. };
  393. int kvm_s390_handle_b2(struct kvm_vcpu *vcpu)
  394. {
  395. intercept_handler_t handler;
  396. /*
  397. * A lot of B2 instructions are priviledged. Here we check for
  398. * the privileged ones, that we can handle in the kernel.
  399. * Anything else goes to userspace.
  400. */
  401. handler = b2_handlers[vcpu->arch.sie_block->ipa & 0x00ff];
  402. if (handler)
  403. return handler(vcpu);
  404. return -EOPNOTSUPP;
  405. }
  406. static int handle_epsw(struct kvm_vcpu *vcpu)
  407. {
  408. int reg1, reg2;
  409. kvm_s390_get_regs_rre(vcpu, &reg1, &reg2);
  410. /* This basically extracts the mask half of the psw. */
  411. vcpu->run->s.regs.gprs[reg1] &= 0xffffffff00000000;
  412. vcpu->run->s.regs.gprs[reg1] |= vcpu->arch.sie_block->gpsw.mask >> 32;
  413. if (reg2) {
  414. vcpu->run->s.regs.gprs[reg2] &= 0xffffffff00000000;
  415. vcpu->run->s.regs.gprs[reg2] |=
  416. vcpu->arch.sie_block->gpsw.mask & 0x00000000ffffffff;
  417. }
  418. return 0;
  419. }
  420. #define PFMF_RESERVED 0xfffc0101UL
  421. #define PFMF_SK 0x00020000UL
  422. #define PFMF_CF 0x00010000UL
  423. #define PFMF_UI 0x00008000UL
  424. #define PFMF_FSC 0x00007000UL
  425. #define PFMF_NQ 0x00000800UL
  426. #define PFMF_MR 0x00000400UL
  427. #define PFMF_MC 0x00000200UL
  428. #define PFMF_KEY 0x000000feUL
  429. static int handle_pfmf(struct kvm_vcpu *vcpu)
  430. {
  431. int reg1, reg2;
  432. unsigned long start, end;
  433. vcpu->stat.instruction_pfmf++;
  434. kvm_s390_get_regs_rre(vcpu, &reg1, &reg2);
  435. if (!MACHINE_HAS_PFMF)
  436. return kvm_s390_inject_program_int(vcpu, PGM_OPERATION);
  437. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
  438. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  439. if (vcpu->run->s.regs.gprs[reg1] & PFMF_RESERVED)
  440. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  441. /* Only provide non-quiescing support if the host supports it */
  442. if (vcpu->run->s.regs.gprs[reg1] & PFMF_NQ &&
  443. S390_lowcore.stfl_fac_list & 0x00020000)
  444. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  445. /* No support for conditional-SSKE */
  446. if (vcpu->run->s.regs.gprs[reg1] & (PFMF_MR | PFMF_MC))
  447. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  448. start = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK;
  449. switch (vcpu->run->s.regs.gprs[reg1] & PFMF_FSC) {
  450. case 0x00000000:
  451. end = (start + (1UL << 12)) & ~((1UL << 12) - 1);
  452. break;
  453. case 0x00001000:
  454. end = (start + (1UL << 20)) & ~((1UL << 20) - 1);
  455. break;
  456. /* We dont support EDAT2
  457. case 0x00002000:
  458. end = (start + (1UL << 31)) & ~((1UL << 31) - 1);
  459. break;*/
  460. default:
  461. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  462. }
  463. while (start < end) {
  464. unsigned long useraddr;
  465. useraddr = gmap_translate(start, vcpu->arch.gmap);
  466. if (IS_ERR((void *)useraddr))
  467. return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
  468. if (vcpu->run->s.regs.gprs[reg1] & PFMF_CF) {
  469. if (clear_user((void __user *)useraddr, PAGE_SIZE))
  470. return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
  471. }
  472. if (vcpu->run->s.regs.gprs[reg1] & PFMF_SK) {
  473. if (set_guest_storage_key(current->mm, useraddr,
  474. vcpu->run->s.regs.gprs[reg1] & PFMF_KEY,
  475. vcpu->run->s.regs.gprs[reg1] & PFMF_NQ))
  476. return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
  477. }
  478. start += PAGE_SIZE;
  479. }
  480. if (vcpu->run->s.regs.gprs[reg1] & PFMF_FSC)
  481. vcpu->run->s.regs.gprs[reg2] = end;
  482. return 0;
  483. }
  484. static const intercept_handler_t b9_handlers[256] = {
  485. [0x8d] = handle_epsw,
  486. [0x9c] = handle_io_inst,
  487. [0xaf] = handle_pfmf,
  488. };
  489. int kvm_s390_handle_b9(struct kvm_vcpu *vcpu)
  490. {
  491. intercept_handler_t handler;
  492. /* This is handled just as for the B2 instructions. */
  493. handler = b9_handlers[vcpu->arch.sie_block->ipa & 0x00ff];
  494. if (handler)
  495. return handler(vcpu);
  496. return -EOPNOTSUPP;
  497. }
  498. static const intercept_handler_t eb_handlers[256] = {
  499. [0x8a] = handle_io_inst,
  500. };
  501. int kvm_s390_handle_priv_eb(struct kvm_vcpu *vcpu)
  502. {
  503. intercept_handler_t handler;
  504. handler = eb_handlers[vcpu->arch.sie_block->ipb & 0xff];
  505. if (handler)
  506. return handler(vcpu);
  507. return -EOPNOTSUPP;
  508. }
  509. static int handle_tprot(struct kvm_vcpu *vcpu)
  510. {
  511. u64 address1, address2;
  512. struct vm_area_struct *vma;
  513. unsigned long user_address;
  514. vcpu->stat.instruction_tprot++;
  515. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
  516. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  517. kvm_s390_get_base_disp_sse(vcpu, &address1, &address2);
  518. /* we only handle the Linux memory detection case:
  519. * access key == 0
  520. * guest DAT == off
  521. * everything else goes to userspace. */
  522. if (address2 & 0xf0)
  523. return -EOPNOTSUPP;
  524. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_DAT)
  525. return -EOPNOTSUPP;
  526. down_read(&current->mm->mmap_sem);
  527. user_address = __gmap_translate(address1, vcpu->arch.gmap);
  528. if (IS_ERR_VALUE(user_address))
  529. goto out_inject;
  530. vma = find_vma(current->mm, user_address);
  531. if (!vma)
  532. goto out_inject;
  533. vcpu->arch.sie_block->gpsw.mask &= ~(3ul << 44);
  534. if (!(vma->vm_flags & VM_WRITE) && (vma->vm_flags & VM_READ))
  535. vcpu->arch.sie_block->gpsw.mask |= (1ul << 44);
  536. if (!(vma->vm_flags & VM_WRITE) && !(vma->vm_flags & VM_READ))
  537. vcpu->arch.sie_block->gpsw.mask |= (2ul << 44);
  538. up_read(&current->mm->mmap_sem);
  539. return 0;
  540. out_inject:
  541. up_read(&current->mm->mmap_sem);
  542. return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
  543. }
  544. int kvm_s390_handle_e5(struct kvm_vcpu *vcpu)
  545. {
  546. /* For e5xx... instructions we only handle TPROT */
  547. if ((vcpu->arch.sie_block->ipa & 0x00ff) == 0x01)
  548. return handle_tprot(vcpu);
  549. return -EOPNOTSUPP;
  550. }
  551. static int handle_sckpf(struct kvm_vcpu *vcpu)
  552. {
  553. u32 value;
  554. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
  555. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  556. if (vcpu->run->s.regs.gprs[0] & 0x00000000ffff0000)
  557. return kvm_s390_inject_program_int(vcpu,
  558. PGM_SPECIFICATION);
  559. value = vcpu->run->s.regs.gprs[0] & 0x000000000000ffff;
  560. vcpu->arch.sie_block->todpr = value;
  561. return 0;
  562. }
  563. static const intercept_handler_t x01_handlers[256] = {
  564. [0x07] = handle_sckpf,
  565. };
  566. int kvm_s390_handle_01(struct kvm_vcpu *vcpu)
  567. {
  568. intercept_handler_t handler;
  569. handler = x01_handlers[vcpu->arch.sie_block->ipa & 0x00ff];
  570. if (handler)
  571. return handler(vcpu);
  572. return -EOPNOTSUPP;
  573. }