priv.c 18 KB

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