priv.c 22 KB

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