priv.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861
  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->arch.sie_block->gcr[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 issuing 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. int cpus = 0;
  332. int n;
  333. cpus = atomic_read(&vcpu->kvm->online_vcpus);
  334. /* deal with other level 3 hypervisors */
  335. if (stsi(mem, 3, 2, 2))
  336. mem->count = 0;
  337. if (mem->count < 8)
  338. mem->count++;
  339. for (n = mem->count - 1; n > 0 ; n--)
  340. memcpy(&mem->vm[n], &mem->vm[n - 1], sizeof(mem->vm[0]));
  341. mem->vm[0].cpus_total = cpus;
  342. mem->vm[0].cpus_configured = cpus;
  343. mem->vm[0].cpus_standby = 0;
  344. mem->vm[0].cpus_reserved = 0;
  345. mem->vm[0].caf = 1000;
  346. memcpy(mem->vm[0].name, "KVMguest", 8);
  347. ASCEBC(mem->vm[0].name, 8);
  348. memcpy(mem->vm[0].cpi, "KVM/Linux ", 16);
  349. ASCEBC(mem->vm[0].cpi, 16);
  350. }
  351. static int handle_stsi(struct kvm_vcpu *vcpu)
  352. {
  353. int fc = (vcpu->run->s.regs.gprs[0] & 0xf0000000) >> 28;
  354. int sel1 = vcpu->run->s.regs.gprs[0] & 0xff;
  355. int sel2 = vcpu->run->s.regs.gprs[1] & 0xffff;
  356. unsigned long mem = 0;
  357. u64 operand2;
  358. int rc = 0;
  359. vcpu->stat.instruction_stsi++;
  360. VCPU_EVENT(vcpu, 4, "stsi: fc: %x sel1: %x sel2: %x", fc, sel1, sel2);
  361. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
  362. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  363. if (fc > 3) {
  364. kvm_s390_set_psw_cc(vcpu, 3);
  365. return 0;
  366. }
  367. if (vcpu->run->s.regs.gprs[0] & 0x0fffff00
  368. || vcpu->run->s.regs.gprs[1] & 0xffff0000)
  369. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  370. if (fc == 0) {
  371. vcpu->run->s.regs.gprs[0] = 3 << 28;
  372. kvm_s390_set_psw_cc(vcpu, 0);
  373. return 0;
  374. }
  375. operand2 = kvm_s390_get_base_disp_s(vcpu);
  376. if (operand2 & 0xfff)
  377. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  378. switch (fc) {
  379. case 1: /* same handling for 1 and 2 */
  380. case 2:
  381. mem = get_zeroed_page(GFP_KERNEL);
  382. if (!mem)
  383. goto out_no_data;
  384. if (stsi((void *) mem, fc, sel1, sel2))
  385. goto out_no_data;
  386. break;
  387. case 3:
  388. if (sel1 != 2 || sel2 != 2)
  389. goto out_no_data;
  390. mem = get_zeroed_page(GFP_KERNEL);
  391. if (!mem)
  392. goto out_no_data;
  393. handle_stsi_3_2_2(vcpu, (void *) mem);
  394. break;
  395. }
  396. if (copy_to_guest_absolute(vcpu, operand2, (void *) mem, PAGE_SIZE)) {
  397. rc = kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
  398. goto out_exception;
  399. }
  400. trace_kvm_s390_handle_stsi(vcpu, fc, sel1, sel2, operand2);
  401. free_page(mem);
  402. kvm_s390_set_psw_cc(vcpu, 0);
  403. vcpu->run->s.regs.gprs[0] = 0;
  404. return 0;
  405. out_no_data:
  406. kvm_s390_set_psw_cc(vcpu, 3);
  407. out_exception:
  408. free_page(mem);
  409. return rc;
  410. }
  411. static const intercept_handler_t b2_handlers[256] = {
  412. [0x02] = handle_stidp,
  413. [0x04] = handle_set_clock,
  414. [0x10] = handle_set_prefix,
  415. [0x11] = handle_store_prefix,
  416. [0x12] = handle_store_cpu_address,
  417. [0x29] = handle_skey,
  418. [0x2a] = handle_skey,
  419. [0x2b] = handle_skey,
  420. [0x2c] = handle_test_block,
  421. [0x30] = handle_io_inst,
  422. [0x31] = handle_io_inst,
  423. [0x32] = handle_io_inst,
  424. [0x33] = handle_io_inst,
  425. [0x34] = handle_io_inst,
  426. [0x35] = handle_io_inst,
  427. [0x36] = handle_io_inst,
  428. [0x37] = handle_io_inst,
  429. [0x38] = handle_io_inst,
  430. [0x39] = handle_io_inst,
  431. [0x3a] = handle_io_inst,
  432. [0x3b] = handle_io_inst,
  433. [0x3c] = handle_io_inst,
  434. [0x5f] = handle_io_inst,
  435. [0x74] = handle_io_inst,
  436. [0x76] = handle_io_inst,
  437. [0x7d] = handle_stsi,
  438. [0xb1] = handle_stfl,
  439. [0xb2] = handle_lpswe,
  440. };
  441. int kvm_s390_handle_b2(struct kvm_vcpu *vcpu)
  442. {
  443. intercept_handler_t handler;
  444. /*
  445. * A lot of B2 instructions are priviledged. Here we check for
  446. * the privileged ones, that we can handle in the kernel.
  447. * Anything else goes to userspace.
  448. */
  449. handler = b2_handlers[vcpu->arch.sie_block->ipa & 0x00ff];
  450. if (handler)
  451. return handler(vcpu);
  452. return -EOPNOTSUPP;
  453. }
  454. static int handle_epsw(struct kvm_vcpu *vcpu)
  455. {
  456. int reg1, reg2;
  457. kvm_s390_get_regs_rre(vcpu, &reg1, &reg2);
  458. /* This basically extracts the mask half of the psw. */
  459. vcpu->run->s.regs.gprs[reg1] &= 0xffffffff00000000UL;
  460. vcpu->run->s.regs.gprs[reg1] |= vcpu->arch.sie_block->gpsw.mask >> 32;
  461. if (reg2) {
  462. vcpu->run->s.regs.gprs[reg2] &= 0xffffffff00000000UL;
  463. vcpu->run->s.regs.gprs[reg2] |=
  464. vcpu->arch.sie_block->gpsw.mask & 0x00000000ffffffffUL;
  465. }
  466. return 0;
  467. }
  468. #define PFMF_RESERVED 0xfffc0101UL
  469. #define PFMF_SK 0x00020000UL
  470. #define PFMF_CF 0x00010000UL
  471. #define PFMF_UI 0x00008000UL
  472. #define PFMF_FSC 0x00007000UL
  473. #define PFMF_NQ 0x00000800UL
  474. #define PFMF_MR 0x00000400UL
  475. #define PFMF_MC 0x00000200UL
  476. #define PFMF_KEY 0x000000feUL
  477. static int handle_pfmf(struct kvm_vcpu *vcpu)
  478. {
  479. int reg1, reg2;
  480. unsigned long start, end;
  481. vcpu->stat.instruction_pfmf++;
  482. kvm_s390_get_regs_rre(vcpu, &reg1, &reg2);
  483. if (!MACHINE_HAS_PFMF)
  484. return kvm_s390_inject_program_int(vcpu, PGM_OPERATION);
  485. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
  486. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  487. if (vcpu->run->s.regs.gprs[reg1] & PFMF_RESERVED)
  488. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  489. /* Only provide non-quiescing support if the host supports it */
  490. if (vcpu->run->s.regs.gprs[reg1] & PFMF_NQ && !test_facility(14))
  491. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  492. /* No support for conditional-SSKE */
  493. if (vcpu->run->s.regs.gprs[reg1] & (PFMF_MR | PFMF_MC))
  494. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  495. start = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK;
  496. switch (vcpu->run->s.regs.gprs[reg1] & PFMF_FSC) {
  497. case 0x00000000:
  498. end = (start + (1UL << 12)) & ~((1UL << 12) - 1);
  499. break;
  500. case 0x00001000:
  501. end = (start + (1UL << 20)) & ~((1UL << 20) - 1);
  502. break;
  503. /* We dont support EDAT2
  504. case 0x00002000:
  505. end = (start + (1UL << 31)) & ~((1UL << 31) - 1);
  506. break;*/
  507. default:
  508. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  509. }
  510. while (start < end) {
  511. unsigned long useraddr;
  512. useraddr = gmap_translate(start, vcpu->arch.gmap);
  513. if (IS_ERR((void *)useraddr))
  514. return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
  515. if (vcpu->run->s.regs.gprs[reg1] & PFMF_CF) {
  516. if (clear_user((void __user *)useraddr, PAGE_SIZE))
  517. return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
  518. }
  519. if (vcpu->run->s.regs.gprs[reg1] & PFMF_SK) {
  520. if (set_guest_storage_key(current->mm, useraddr,
  521. vcpu->run->s.regs.gprs[reg1] & PFMF_KEY,
  522. vcpu->run->s.regs.gprs[reg1] & PFMF_NQ))
  523. return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
  524. }
  525. start += PAGE_SIZE;
  526. }
  527. if (vcpu->run->s.regs.gprs[reg1] & PFMF_FSC)
  528. vcpu->run->s.regs.gprs[reg2] = end;
  529. return 0;
  530. }
  531. static int handle_essa(struct kvm_vcpu *vcpu)
  532. {
  533. /* entries expected to be 1FF */
  534. int entries = (vcpu->arch.sie_block->cbrlo & ~PAGE_MASK) >> 3;
  535. unsigned long *cbrlo, cbrle;
  536. struct gmap *gmap;
  537. int i;
  538. VCPU_EVENT(vcpu, 5, "cmma release %d pages", entries);
  539. gmap = vcpu->arch.gmap;
  540. vcpu->stat.instruction_essa++;
  541. if (!kvm_enabled_cmma() || !vcpu->arch.sie_block->cbrlo)
  542. return kvm_s390_inject_program_int(vcpu, PGM_OPERATION);
  543. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
  544. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  545. if (((vcpu->arch.sie_block->ipb & 0xf0000000) >> 28) > 6)
  546. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  547. /* Rewind PSW to repeat the ESSA instruction */
  548. vcpu->arch.sie_block->gpsw.addr =
  549. __rewind_psw(vcpu->arch.sie_block->gpsw, 4);
  550. vcpu->arch.sie_block->cbrlo &= PAGE_MASK; /* reset nceo */
  551. cbrlo = phys_to_virt(vcpu->arch.sie_block->cbrlo);
  552. down_read(&gmap->mm->mmap_sem);
  553. for (i = 0; i < entries; ++i) {
  554. cbrle = cbrlo[i];
  555. if (unlikely(cbrle & ~PAGE_MASK || cbrle < 2 * PAGE_SIZE))
  556. /* invalid entry */
  557. break;
  558. /* try to free backing */
  559. __gmap_zap(cbrle, gmap);
  560. }
  561. up_read(&gmap->mm->mmap_sem);
  562. if (i < entries)
  563. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  564. return 0;
  565. }
  566. static const intercept_handler_t b9_handlers[256] = {
  567. [0x8d] = handle_epsw,
  568. [0xab] = handle_essa,
  569. [0xaf] = handle_pfmf,
  570. };
  571. int kvm_s390_handle_b9(struct kvm_vcpu *vcpu)
  572. {
  573. intercept_handler_t handler;
  574. /* This is handled just as for the B2 instructions. */
  575. handler = b9_handlers[vcpu->arch.sie_block->ipa & 0x00ff];
  576. if (handler)
  577. return handler(vcpu);
  578. return -EOPNOTSUPP;
  579. }
  580. int kvm_s390_handle_lctl(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. u32 val = 0;
  586. int reg, rc;
  587. vcpu->stat.instruction_lctl++;
  588. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
  589. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  590. useraddr = kvm_s390_get_base_disp_rs(vcpu);
  591. if (useraddr & 3)
  592. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  593. VCPU_EVENT(vcpu, 5, "lctl r1:%x, r3:%x, addr:%llx", reg1, reg3,
  594. useraddr);
  595. trace_kvm_s390_handle_lctl(vcpu, 0, reg1, reg3, useraddr);
  596. reg = reg1;
  597. do {
  598. rc = get_guest(vcpu, val, (u32 __user *) useraddr);
  599. if (rc)
  600. return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
  601. vcpu->arch.sie_block->gcr[reg] &= 0xffffffff00000000ul;
  602. vcpu->arch.sie_block->gcr[reg] |= val;
  603. useraddr += 4;
  604. if (reg == reg3)
  605. break;
  606. reg = (reg + 1) % 16;
  607. } while (1);
  608. return 0;
  609. }
  610. static int handle_lctlg(struct kvm_vcpu *vcpu)
  611. {
  612. int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
  613. int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
  614. u64 useraddr;
  615. int reg, rc;
  616. vcpu->stat.instruction_lctlg++;
  617. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
  618. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  619. useraddr = kvm_s390_get_base_disp_rsy(vcpu);
  620. if (useraddr & 7)
  621. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  622. reg = reg1;
  623. VCPU_EVENT(vcpu, 5, "lctlg r1:%x, r3:%x, addr:%llx", reg1, reg3,
  624. useraddr);
  625. trace_kvm_s390_handle_lctl(vcpu, 1, reg1, reg3, useraddr);
  626. do {
  627. rc = get_guest(vcpu, vcpu->arch.sie_block->gcr[reg],
  628. (u64 __user *) useraddr);
  629. if (rc)
  630. return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
  631. useraddr += 8;
  632. if (reg == reg3)
  633. break;
  634. reg = (reg + 1) % 16;
  635. } while (1);
  636. return 0;
  637. }
  638. static const intercept_handler_t eb_handlers[256] = {
  639. [0x2f] = handle_lctlg,
  640. };
  641. int kvm_s390_handle_eb(struct kvm_vcpu *vcpu)
  642. {
  643. intercept_handler_t handler;
  644. handler = eb_handlers[vcpu->arch.sie_block->ipb & 0xff];
  645. if (handler)
  646. return handler(vcpu);
  647. return -EOPNOTSUPP;
  648. }
  649. static int handle_tprot(struct kvm_vcpu *vcpu)
  650. {
  651. u64 address1, address2;
  652. struct vm_area_struct *vma;
  653. unsigned long user_address;
  654. vcpu->stat.instruction_tprot++;
  655. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
  656. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  657. kvm_s390_get_base_disp_sse(vcpu, &address1, &address2);
  658. /* we only handle the Linux memory detection case:
  659. * access key == 0
  660. * guest DAT == off
  661. * everything else goes to userspace. */
  662. if (address2 & 0xf0)
  663. return -EOPNOTSUPP;
  664. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_DAT)
  665. return -EOPNOTSUPP;
  666. down_read(&current->mm->mmap_sem);
  667. user_address = __gmap_translate(address1, vcpu->arch.gmap);
  668. if (IS_ERR_VALUE(user_address))
  669. goto out_inject;
  670. vma = find_vma(current->mm, user_address);
  671. if (!vma)
  672. goto out_inject;
  673. vcpu->arch.sie_block->gpsw.mask &= ~(3ul << 44);
  674. if (!(vma->vm_flags & VM_WRITE) && (vma->vm_flags & VM_READ))
  675. vcpu->arch.sie_block->gpsw.mask |= (1ul << 44);
  676. if (!(vma->vm_flags & VM_WRITE) && !(vma->vm_flags & VM_READ))
  677. vcpu->arch.sie_block->gpsw.mask |= (2ul << 44);
  678. up_read(&current->mm->mmap_sem);
  679. return 0;
  680. out_inject:
  681. up_read(&current->mm->mmap_sem);
  682. return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
  683. }
  684. int kvm_s390_handle_e5(struct kvm_vcpu *vcpu)
  685. {
  686. /* For e5xx... instructions we only handle TPROT */
  687. if ((vcpu->arch.sie_block->ipa & 0x00ff) == 0x01)
  688. return handle_tprot(vcpu);
  689. return -EOPNOTSUPP;
  690. }
  691. static int handle_sckpf(struct kvm_vcpu *vcpu)
  692. {
  693. u32 value;
  694. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
  695. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  696. if (vcpu->run->s.regs.gprs[0] & 0x00000000ffff0000)
  697. return kvm_s390_inject_program_int(vcpu,
  698. PGM_SPECIFICATION);
  699. value = vcpu->run->s.regs.gprs[0] & 0x000000000000ffff;
  700. vcpu->arch.sie_block->todpr = value;
  701. return 0;
  702. }
  703. static const intercept_handler_t x01_handlers[256] = {
  704. [0x07] = handle_sckpf,
  705. };
  706. int kvm_s390_handle_01(struct kvm_vcpu *vcpu)
  707. {
  708. intercept_handler_t handler;
  709. handler = x01_handlers[vcpu->arch.sie_block->ipa & 0x00ff];
  710. if (handler)
  711. return handler(vcpu);
  712. return -EOPNOTSUPP;
  713. }