priv.c 18 KB

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