priv.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020
  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. int i, rc;
  37. u64 op2;
  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. rc = read_guest(vcpu, op2, &val, sizeof(val));
  44. if (rc)
  45. return kvm_s390_inject_prog_cond(vcpu, rc);
  46. if (store_tod_clock(&hostclk)) {
  47. kvm_s390_set_psw_cc(vcpu, 3);
  48. return 0;
  49. }
  50. val = (val - hostclk) & ~0x3fUL;
  51. mutex_lock(&vcpu->kvm->lock);
  52. kvm_for_each_vcpu(i, cpup, vcpu->kvm)
  53. cpup->arch.sie_block->epoch = val;
  54. mutex_unlock(&vcpu->kvm->lock);
  55. kvm_s390_set_psw_cc(vcpu, 0);
  56. return 0;
  57. }
  58. static int handle_set_prefix(struct kvm_vcpu *vcpu)
  59. {
  60. u64 operand2;
  61. u32 address;
  62. int rc;
  63. vcpu->stat.instruction_spx++;
  64. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
  65. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  66. operand2 = kvm_s390_get_base_disp_s(vcpu);
  67. /* must be word boundary */
  68. if (operand2 & 3)
  69. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  70. /* get the value */
  71. rc = read_guest(vcpu, operand2, &address, sizeof(address));
  72. if (rc)
  73. return kvm_s390_inject_prog_cond(vcpu, rc);
  74. address &= 0x7fffe000u;
  75. /*
  76. * Make sure the new value is valid memory. We only need to check the
  77. * first page, since address is 8k aligned and memory pieces are always
  78. * at least 1MB aligned and have at least a size of 1MB.
  79. */
  80. if (kvm_is_error_gpa(vcpu->kvm, address))
  81. return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
  82. kvm_s390_set_prefix(vcpu, address);
  83. VCPU_EVENT(vcpu, 5, "setting prefix to %x", address);
  84. trace_kvm_s390_handle_prefix(vcpu, 1, address);
  85. return 0;
  86. }
  87. static int handle_store_prefix(struct kvm_vcpu *vcpu)
  88. {
  89. u64 operand2;
  90. u32 address;
  91. int rc;
  92. vcpu->stat.instruction_stpx++;
  93. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
  94. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  95. operand2 = kvm_s390_get_base_disp_s(vcpu);
  96. /* must be word boundary */
  97. if (operand2 & 3)
  98. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  99. address = kvm_s390_get_prefix(vcpu);
  100. /* get the value */
  101. rc = write_guest(vcpu, operand2, &address, sizeof(address));
  102. if (rc)
  103. return kvm_s390_inject_prog_cond(vcpu, rc);
  104. VCPU_EVENT(vcpu, 5, "storing prefix to %x", address);
  105. trace_kvm_s390_handle_prefix(vcpu, 0, address);
  106. return 0;
  107. }
  108. static int handle_store_cpu_address(struct kvm_vcpu *vcpu)
  109. {
  110. u16 vcpu_id = vcpu->vcpu_id;
  111. u64 ga;
  112. int rc;
  113. vcpu->stat.instruction_stap++;
  114. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
  115. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  116. ga = kvm_s390_get_base_disp_s(vcpu);
  117. if (ga & 1)
  118. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  119. rc = write_guest(vcpu, ga, &vcpu_id, sizeof(vcpu_id));
  120. if (rc)
  121. return kvm_s390_inject_prog_cond(vcpu, rc);
  122. VCPU_EVENT(vcpu, 5, "storing cpu address to %llx", ga);
  123. trace_kvm_s390_handle_stap(vcpu, ga);
  124. return 0;
  125. }
  126. static void __skey_check_enable(struct kvm_vcpu *vcpu)
  127. {
  128. if (!(vcpu->arch.sie_block->ictl & (ICTL_ISKE | ICTL_SSKE | ICTL_RRBE)))
  129. return;
  130. s390_enable_skey();
  131. trace_kvm_s390_skey_related_inst(vcpu);
  132. vcpu->arch.sie_block->ictl &= ~(ICTL_ISKE | ICTL_SSKE | ICTL_RRBE);
  133. }
  134. static int handle_skey(struct kvm_vcpu *vcpu)
  135. {
  136. __skey_check_enable(vcpu);
  137. vcpu->stat.instruction_storage_key++;
  138. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
  139. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  140. vcpu->arch.sie_block->gpsw.addr =
  141. __rewind_psw(vcpu->arch.sie_block->gpsw, 4);
  142. VCPU_EVENT(vcpu, 4, "%s", "retrying storage key operation");
  143. return 0;
  144. }
  145. static int handle_ipte_interlock(struct kvm_vcpu *vcpu)
  146. {
  147. psw_t *psw = &vcpu->arch.sie_block->gpsw;
  148. vcpu->stat.instruction_ipte_interlock++;
  149. if (psw_bits(*psw).p)
  150. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  151. wait_event(vcpu->kvm->arch.ipte_wq, !ipte_lock_held(vcpu));
  152. psw->addr = __rewind_psw(*psw, 4);
  153. VCPU_EVENT(vcpu, 4, "%s", "retrying ipte interlock operation");
  154. return 0;
  155. }
  156. static int handle_test_block(struct kvm_vcpu *vcpu)
  157. {
  158. gpa_t addr;
  159. int reg2;
  160. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
  161. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  162. kvm_s390_get_regs_rre(vcpu, NULL, &reg2);
  163. addr = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK;
  164. addr = kvm_s390_logical_to_effective(vcpu, addr);
  165. if (kvm_s390_check_low_addr_protection(vcpu, addr))
  166. return kvm_s390_inject_prog_irq(vcpu, &vcpu->arch.pgm);
  167. addr = kvm_s390_real_to_abs(vcpu, addr);
  168. if (kvm_is_error_gpa(vcpu->kvm, addr))
  169. return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
  170. /*
  171. * We don't expect errors on modern systems, and do not care
  172. * about storage keys (yet), so let's just clear the page.
  173. */
  174. if (kvm_clear_guest(vcpu->kvm, addr, PAGE_SIZE))
  175. return -EFAULT;
  176. kvm_s390_set_psw_cc(vcpu, 0);
  177. vcpu->run->s.regs.gprs[0] = 0;
  178. return 0;
  179. }
  180. static int handle_tpi(struct kvm_vcpu *vcpu)
  181. {
  182. struct kvm_s390_interrupt_info *inti;
  183. unsigned long len;
  184. u32 tpi_data[3];
  185. int cc, rc;
  186. u64 addr;
  187. rc = 0;
  188. addr = kvm_s390_get_base_disp_s(vcpu);
  189. if (addr & 3)
  190. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  191. cc = 0;
  192. inti = kvm_s390_get_io_int(vcpu->kvm, vcpu->arch.sie_block->gcr[6], 0);
  193. if (!inti)
  194. goto no_interrupt;
  195. cc = 1;
  196. tpi_data[0] = inti->io.subchannel_id << 16 | inti->io.subchannel_nr;
  197. tpi_data[1] = inti->io.io_int_parm;
  198. tpi_data[2] = inti->io.io_int_word;
  199. if (addr) {
  200. /*
  201. * Store the two-word I/O interruption code into the
  202. * provided area.
  203. */
  204. len = sizeof(tpi_data) - 4;
  205. rc = write_guest(vcpu, addr, &tpi_data, len);
  206. if (rc)
  207. return kvm_s390_inject_prog_cond(vcpu, rc);
  208. } else {
  209. /*
  210. * Store the three-word I/O interruption code into
  211. * the appropriate lowcore area.
  212. */
  213. len = sizeof(tpi_data);
  214. if (write_guest_lc(vcpu, __LC_SUBCHANNEL_ID, &tpi_data, len))
  215. rc = -EFAULT;
  216. }
  217. /*
  218. * If we encounter a problem storing the interruption code, the
  219. * instruction is suppressed from the guest's view: reinject the
  220. * interrupt.
  221. */
  222. if (!rc)
  223. kfree(inti);
  224. else
  225. kvm_s390_reinject_io_int(vcpu->kvm, inti);
  226. no_interrupt:
  227. /* Set condition code and we're done. */
  228. if (!rc)
  229. kvm_s390_set_psw_cc(vcpu, cc);
  230. return rc ? -EFAULT : 0;
  231. }
  232. static int handle_tsch(struct kvm_vcpu *vcpu)
  233. {
  234. struct kvm_s390_interrupt_info *inti;
  235. inti = kvm_s390_get_io_int(vcpu->kvm, 0,
  236. vcpu->run->s.regs.gprs[1]);
  237. /*
  238. * Prepare exit to userspace.
  239. * We indicate whether we dequeued a pending I/O interrupt
  240. * so that userspace can re-inject it if the instruction gets
  241. * a program check. While this may re-order the pending I/O
  242. * interrupts, this is no problem since the priority is kept
  243. * intact.
  244. */
  245. vcpu->run->exit_reason = KVM_EXIT_S390_TSCH;
  246. vcpu->run->s390_tsch.dequeued = !!inti;
  247. if (inti) {
  248. vcpu->run->s390_tsch.subchannel_id = inti->io.subchannel_id;
  249. vcpu->run->s390_tsch.subchannel_nr = inti->io.subchannel_nr;
  250. vcpu->run->s390_tsch.io_int_parm = inti->io.io_int_parm;
  251. vcpu->run->s390_tsch.io_int_word = inti->io.io_int_word;
  252. }
  253. vcpu->run->s390_tsch.ipb = vcpu->arch.sie_block->ipb;
  254. kfree(inti);
  255. return -EREMOTE;
  256. }
  257. static int handle_io_inst(struct kvm_vcpu *vcpu)
  258. {
  259. VCPU_EVENT(vcpu, 4, "%s", "I/O instruction");
  260. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
  261. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  262. if (vcpu->kvm->arch.css_support) {
  263. /*
  264. * Most I/O instructions will be handled by userspace.
  265. * Exceptions are tpi and the interrupt portion of tsch.
  266. */
  267. if (vcpu->arch.sie_block->ipa == 0xb236)
  268. return handle_tpi(vcpu);
  269. if (vcpu->arch.sie_block->ipa == 0xb235)
  270. return handle_tsch(vcpu);
  271. /* Handle in userspace. */
  272. return -EOPNOTSUPP;
  273. } else {
  274. /*
  275. * Set condition code 3 to stop the guest from issuing channel
  276. * I/O instructions.
  277. */
  278. kvm_s390_set_psw_cc(vcpu, 3);
  279. return 0;
  280. }
  281. }
  282. static int handle_stfl(struct kvm_vcpu *vcpu)
  283. {
  284. int rc;
  285. vcpu->stat.instruction_stfl++;
  286. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
  287. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  288. rc = write_guest_lc(vcpu, offsetof(struct _lowcore, stfl_fac_list),
  289. vfacilities, 4);
  290. if (rc)
  291. return rc;
  292. VCPU_EVENT(vcpu, 5, "store facility list value %x",
  293. *(unsigned int *) vfacilities);
  294. trace_kvm_s390_handle_stfl(vcpu, *(unsigned int *) vfacilities);
  295. return 0;
  296. }
  297. static void handle_new_psw(struct kvm_vcpu *vcpu)
  298. {
  299. /* Check whether the new psw is enabled for machine checks. */
  300. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_MCHECK)
  301. kvm_s390_deliver_pending_machine_checks(vcpu);
  302. }
  303. #define PSW_MASK_ADDR_MODE (PSW_MASK_EA | PSW_MASK_BA)
  304. #define PSW_MASK_UNASSIGNED 0xb80800fe7fffffffUL
  305. #define PSW_ADDR_24 0x0000000000ffffffUL
  306. #define PSW_ADDR_31 0x000000007fffffffUL
  307. int is_valid_psw(psw_t *psw)
  308. {
  309. if (psw->mask & PSW_MASK_UNASSIGNED)
  310. return 0;
  311. if ((psw->mask & PSW_MASK_ADDR_MODE) == PSW_MASK_BA) {
  312. if (psw->addr & ~PSW_ADDR_31)
  313. return 0;
  314. }
  315. if (!(psw->mask & PSW_MASK_ADDR_MODE) && (psw->addr & ~PSW_ADDR_24))
  316. return 0;
  317. if ((psw->mask & PSW_MASK_ADDR_MODE) == PSW_MASK_EA)
  318. return 0;
  319. if (psw->addr & 1)
  320. return 0;
  321. return 1;
  322. }
  323. int kvm_s390_handle_lpsw(struct kvm_vcpu *vcpu)
  324. {
  325. psw_t *gpsw = &vcpu->arch.sie_block->gpsw;
  326. psw_compat_t new_psw;
  327. u64 addr;
  328. int rc;
  329. if (gpsw->mask & PSW_MASK_PSTATE)
  330. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  331. addr = kvm_s390_get_base_disp_s(vcpu);
  332. if (addr & 7)
  333. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  334. rc = read_guest(vcpu, addr, &new_psw, sizeof(new_psw));
  335. if (rc)
  336. return kvm_s390_inject_prog_cond(vcpu, rc);
  337. if (!(new_psw.mask & PSW32_MASK_BASE))
  338. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  339. gpsw->mask = (new_psw.mask & ~PSW32_MASK_BASE) << 32;
  340. gpsw->mask |= new_psw.addr & PSW32_ADDR_AMODE;
  341. gpsw->addr = new_psw.addr & ~PSW32_ADDR_AMODE;
  342. if (!is_valid_psw(gpsw))
  343. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  344. handle_new_psw(vcpu);
  345. return 0;
  346. }
  347. static int handle_lpswe(struct kvm_vcpu *vcpu)
  348. {
  349. psw_t new_psw;
  350. u64 addr;
  351. int rc;
  352. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
  353. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  354. addr = kvm_s390_get_base_disp_s(vcpu);
  355. if (addr & 7)
  356. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  357. rc = read_guest(vcpu, addr, &new_psw, sizeof(new_psw));
  358. if (rc)
  359. return kvm_s390_inject_prog_cond(vcpu, rc);
  360. vcpu->arch.sie_block->gpsw = new_psw;
  361. if (!is_valid_psw(&vcpu->arch.sie_block->gpsw))
  362. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  363. handle_new_psw(vcpu);
  364. return 0;
  365. }
  366. static int handle_stidp(struct kvm_vcpu *vcpu)
  367. {
  368. u64 stidp_data = vcpu->arch.stidp_data;
  369. u64 operand2;
  370. int rc;
  371. vcpu->stat.instruction_stidp++;
  372. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
  373. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  374. operand2 = kvm_s390_get_base_disp_s(vcpu);
  375. if (operand2 & 7)
  376. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  377. rc = write_guest(vcpu, operand2, &stidp_data, sizeof(stidp_data));
  378. if (rc)
  379. return kvm_s390_inject_prog_cond(vcpu, rc);
  380. VCPU_EVENT(vcpu, 5, "%s", "store cpu id");
  381. return 0;
  382. }
  383. static void handle_stsi_3_2_2(struct kvm_vcpu *vcpu, struct sysinfo_3_2_2 *mem)
  384. {
  385. int cpus = 0;
  386. int n;
  387. cpus = atomic_read(&vcpu->kvm->online_vcpus);
  388. /* deal with other level 3 hypervisors */
  389. if (stsi(mem, 3, 2, 2))
  390. mem->count = 0;
  391. if (mem->count < 8)
  392. mem->count++;
  393. for (n = mem->count - 1; n > 0 ; n--)
  394. memcpy(&mem->vm[n], &mem->vm[n - 1], sizeof(mem->vm[0]));
  395. mem->vm[0].cpus_total = cpus;
  396. mem->vm[0].cpus_configured = cpus;
  397. mem->vm[0].cpus_standby = 0;
  398. mem->vm[0].cpus_reserved = 0;
  399. mem->vm[0].caf = 1000;
  400. memcpy(mem->vm[0].name, "KVMguest", 8);
  401. ASCEBC(mem->vm[0].name, 8);
  402. memcpy(mem->vm[0].cpi, "KVM/Linux ", 16);
  403. ASCEBC(mem->vm[0].cpi, 16);
  404. }
  405. static int handle_stsi(struct kvm_vcpu *vcpu)
  406. {
  407. int fc = (vcpu->run->s.regs.gprs[0] & 0xf0000000) >> 28;
  408. int sel1 = vcpu->run->s.regs.gprs[0] & 0xff;
  409. int sel2 = vcpu->run->s.regs.gprs[1] & 0xffff;
  410. unsigned long mem = 0;
  411. u64 operand2;
  412. int rc = 0;
  413. vcpu->stat.instruction_stsi++;
  414. VCPU_EVENT(vcpu, 4, "stsi: fc: %x sel1: %x sel2: %x", fc, sel1, sel2);
  415. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
  416. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  417. if (fc > 3) {
  418. kvm_s390_set_psw_cc(vcpu, 3);
  419. return 0;
  420. }
  421. if (vcpu->run->s.regs.gprs[0] & 0x0fffff00
  422. || vcpu->run->s.regs.gprs[1] & 0xffff0000)
  423. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  424. if (fc == 0) {
  425. vcpu->run->s.regs.gprs[0] = 3 << 28;
  426. kvm_s390_set_psw_cc(vcpu, 0);
  427. return 0;
  428. }
  429. operand2 = kvm_s390_get_base_disp_s(vcpu);
  430. if (operand2 & 0xfff)
  431. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  432. switch (fc) {
  433. case 1: /* same handling for 1 and 2 */
  434. case 2:
  435. mem = get_zeroed_page(GFP_KERNEL);
  436. if (!mem)
  437. goto out_no_data;
  438. if (stsi((void *) mem, fc, sel1, sel2))
  439. goto out_no_data;
  440. break;
  441. case 3:
  442. if (sel1 != 2 || sel2 != 2)
  443. goto out_no_data;
  444. mem = get_zeroed_page(GFP_KERNEL);
  445. if (!mem)
  446. goto out_no_data;
  447. handle_stsi_3_2_2(vcpu, (void *) mem);
  448. break;
  449. }
  450. rc = write_guest(vcpu, operand2, (void *)mem, PAGE_SIZE);
  451. if (rc) {
  452. rc = kvm_s390_inject_prog_cond(vcpu, rc);
  453. goto out;
  454. }
  455. trace_kvm_s390_handle_stsi(vcpu, fc, sel1, sel2, operand2);
  456. free_page(mem);
  457. kvm_s390_set_psw_cc(vcpu, 0);
  458. vcpu->run->s.regs.gprs[0] = 0;
  459. return 0;
  460. out_no_data:
  461. kvm_s390_set_psw_cc(vcpu, 3);
  462. out:
  463. free_page(mem);
  464. return rc;
  465. }
  466. static const intercept_handler_t b2_handlers[256] = {
  467. [0x02] = handle_stidp,
  468. [0x04] = handle_set_clock,
  469. [0x10] = handle_set_prefix,
  470. [0x11] = handle_store_prefix,
  471. [0x12] = handle_store_cpu_address,
  472. [0x21] = handle_ipte_interlock,
  473. [0x29] = handle_skey,
  474. [0x2a] = handle_skey,
  475. [0x2b] = handle_skey,
  476. [0x2c] = handle_test_block,
  477. [0x30] = handle_io_inst,
  478. [0x31] = handle_io_inst,
  479. [0x32] = handle_io_inst,
  480. [0x33] = handle_io_inst,
  481. [0x34] = handle_io_inst,
  482. [0x35] = handle_io_inst,
  483. [0x36] = handle_io_inst,
  484. [0x37] = handle_io_inst,
  485. [0x38] = handle_io_inst,
  486. [0x39] = handle_io_inst,
  487. [0x3a] = handle_io_inst,
  488. [0x3b] = handle_io_inst,
  489. [0x3c] = handle_io_inst,
  490. [0x50] = handle_ipte_interlock,
  491. [0x5f] = handle_io_inst,
  492. [0x74] = handle_io_inst,
  493. [0x76] = handle_io_inst,
  494. [0x7d] = handle_stsi,
  495. [0xb1] = handle_stfl,
  496. [0xb2] = handle_lpswe,
  497. };
  498. int kvm_s390_handle_b2(struct kvm_vcpu *vcpu)
  499. {
  500. intercept_handler_t handler;
  501. /*
  502. * A lot of B2 instructions are priviledged. Here we check for
  503. * the privileged ones, that we can handle in the kernel.
  504. * Anything else goes to userspace.
  505. */
  506. handler = b2_handlers[vcpu->arch.sie_block->ipa & 0x00ff];
  507. if (handler)
  508. return handler(vcpu);
  509. return -EOPNOTSUPP;
  510. }
  511. static int handle_epsw(struct kvm_vcpu *vcpu)
  512. {
  513. int reg1, reg2;
  514. kvm_s390_get_regs_rre(vcpu, &reg1, &reg2);
  515. /* This basically extracts the mask half of the psw. */
  516. vcpu->run->s.regs.gprs[reg1] &= 0xffffffff00000000UL;
  517. vcpu->run->s.regs.gprs[reg1] |= vcpu->arch.sie_block->gpsw.mask >> 32;
  518. if (reg2) {
  519. vcpu->run->s.regs.gprs[reg2] &= 0xffffffff00000000UL;
  520. vcpu->run->s.regs.gprs[reg2] |=
  521. vcpu->arch.sie_block->gpsw.mask & 0x00000000ffffffffUL;
  522. }
  523. return 0;
  524. }
  525. #define PFMF_RESERVED 0xfffc0101UL
  526. #define PFMF_SK 0x00020000UL
  527. #define PFMF_CF 0x00010000UL
  528. #define PFMF_UI 0x00008000UL
  529. #define PFMF_FSC 0x00007000UL
  530. #define PFMF_NQ 0x00000800UL
  531. #define PFMF_MR 0x00000400UL
  532. #define PFMF_MC 0x00000200UL
  533. #define PFMF_KEY 0x000000feUL
  534. static int handle_pfmf(struct kvm_vcpu *vcpu)
  535. {
  536. int reg1, reg2;
  537. unsigned long start, end;
  538. vcpu->stat.instruction_pfmf++;
  539. kvm_s390_get_regs_rre(vcpu, &reg1, &reg2);
  540. if (!MACHINE_HAS_PFMF)
  541. return kvm_s390_inject_program_int(vcpu, PGM_OPERATION);
  542. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
  543. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  544. if (vcpu->run->s.regs.gprs[reg1] & PFMF_RESERVED)
  545. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  546. /* Only provide non-quiescing support if the host supports it */
  547. if (vcpu->run->s.regs.gprs[reg1] & PFMF_NQ && !test_facility(14))
  548. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  549. /* No support for conditional-SSKE */
  550. if (vcpu->run->s.regs.gprs[reg1] & (PFMF_MR | PFMF_MC))
  551. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  552. start = vcpu->run->s.regs.gprs[reg2] & PAGE_MASK;
  553. if (vcpu->run->s.regs.gprs[reg1] & PFMF_CF) {
  554. if (kvm_s390_check_low_addr_protection(vcpu, start))
  555. return kvm_s390_inject_prog_irq(vcpu, &vcpu->arch.pgm);
  556. }
  557. switch (vcpu->run->s.regs.gprs[reg1] & PFMF_FSC) {
  558. case 0x00000000:
  559. end = (start + (1UL << 12)) & ~((1UL << 12) - 1);
  560. break;
  561. case 0x00001000:
  562. end = (start + (1UL << 20)) & ~((1UL << 20) - 1);
  563. break;
  564. /* We dont support EDAT2
  565. case 0x00002000:
  566. end = (start + (1UL << 31)) & ~((1UL << 31) - 1);
  567. break;*/
  568. default:
  569. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  570. }
  571. while (start < end) {
  572. unsigned long useraddr, abs_addr;
  573. /* Translate guest address to host address */
  574. if ((vcpu->run->s.regs.gprs[reg1] & PFMF_FSC) == 0)
  575. abs_addr = kvm_s390_real_to_abs(vcpu, start);
  576. else
  577. abs_addr = start;
  578. useraddr = gfn_to_hva(vcpu->kvm, gpa_to_gfn(abs_addr));
  579. if (kvm_is_error_hva(useraddr))
  580. return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
  581. if (vcpu->run->s.regs.gprs[reg1] & PFMF_CF) {
  582. if (clear_user((void __user *)useraddr, PAGE_SIZE))
  583. return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
  584. }
  585. if (vcpu->run->s.regs.gprs[reg1] & PFMF_SK) {
  586. __skey_check_enable(vcpu);
  587. if (set_guest_storage_key(current->mm, useraddr,
  588. vcpu->run->s.regs.gprs[reg1] & PFMF_KEY,
  589. vcpu->run->s.regs.gprs[reg1] & PFMF_NQ))
  590. return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
  591. }
  592. start += PAGE_SIZE;
  593. }
  594. if (vcpu->run->s.regs.gprs[reg1] & PFMF_FSC)
  595. vcpu->run->s.regs.gprs[reg2] = end;
  596. return 0;
  597. }
  598. static int handle_essa(struct kvm_vcpu *vcpu)
  599. {
  600. /* entries expected to be 1FF */
  601. int entries = (vcpu->arch.sie_block->cbrlo & ~PAGE_MASK) >> 3;
  602. unsigned long *cbrlo, cbrle;
  603. struct gmap *gmap;
  604. int i;
  605. VCPU_EVENT(vcpu, 5, "cmma release %d pages", entries);
  606. gmap = vcpu->arch.gmap;
  607. vcpu->stat.instruction_essa++;
  608. if (!kvm_s390_cmma_enabled(vcpu->kvm))
  609. return kvm_s390_inject_program_int(vcpu, PGM_OPERATION);
  610. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
  611. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  612. if (((vcpu->arch.sie_block->ipb & 0xf0000000) >> 28) > 6)
  613. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  614. /* Rewind PSW to repeat the ESSA instruction */
  615. vcpu->arch.sie_block->gpsw.addr =
  616. __rewind_psw(vcpu->arch.sie_block->gpsw, 4);
  617. vcpu->arch.sie_block->cbrlo &= PAGE_MASK; /* reset nceo */
  618. cbrlo = phys_to_virt(vcpu->arch.sie_block->cbrlo);
  619. down_read(&gmap->mm->mmap_sem);
  620. for (i = 0; i < entries; ++i) {
  621. cbrle = cbrlo[i];
  622. if (unlikely(cbrle & ~PAGE_MASK || cbrle < 2 * PAGE_SIZE))
  623. /* invalid entry */
  624. break;
  625. /* try to free backing */
  626. __gmap_zap(cbrle, gmap);
  627. }
  628. up_read(&gmap->mm->mmap_sem);
  629. if (i < entries)
  630. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  631. return 0;
  632. }
  633. static const intercept_handler_t b9_handlers[256] = {
  634. [0x8a] = handle_ipte_interlock,
  635. [0x8d] = handle_epsw,
  636. [0x8e] = handle_ipte_interlock,
  637. [0x8f] = handle_ipte_interlock,
  638. [0xab] = handle_essa,
  639. [0xaf] = handle_pfmf,
  640. };
  641. int kvm_s390_handle_b9(struct kvm_vcpu *vcpu)
  642. {
  643. intercept_handler_t handler;
  644. /* This is handled just as for the B2 instructions. */
  645. handler = b9_handlers[vcpu->arch.sie_block->ipa & 0x00ff];
  646. if (handler)
  647. return handler(vcpu);
  648. return -EOPNOTSUPP;
  649. }
  650. int kvm_s390_handle_lctl(struct kvm_vcpu *vcpu)
  651. {
  652. int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
  653. int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
  654. u32 val = 0;
  655. int reg, rc;
  656. u64 ga;
  657. vcpu->stat.instruction_lctl++;
  658. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
  659. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  660. ga = kvm_s390_get_base_disp_rs(vcpu);
  661. if (ga & 3)
  662. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  663. VCPU_EVENT(vcpu, 5, "lctl r1:%x, r3:%x, addr:%llx", reg1, reg3, ga);
  664. trace_kvm_s390_handle_lctl(vcpu, 0, reg1, reg3, ga);
  665. reg = reg1;
  666. do {
  667. rc = read_guest(vcpu, ga, &val, sizeof(val));
  668. if (rc)
  669. return kvm_s390_inject_prog_cond(vcpu, rc);
  670. vcpu->arch.sie_block->gcr[reg] &= 0xffffffff00000000ul;
  671. vcpu->arch.sie_block->gcr[reg] |= val;
  672. ga += 4;
  673. if (reg == reg3)
  674. break;
  675. reg = (reg + 1) % 16;
  676. } while (1);
  677. return 0;
  678. }
  679. int kvm_s390_handle_stctl(struct kvm_vcpu *vcpu)
  680. {
  681. int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
  682. int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
  683. u64 ga;
  684. u32 val;
  685. int reg, rc;
  686. vcpu->stat.instruction_stctl++;
  687. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
  688. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  689. ga = kvm_s390_get_base_disp_rs(vcpu);
  690. if (ga & 3)
  691. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  692. VCPU_EVENT(vcpu, 5, "stctl r1:%x, r3:%x, addr:%llx", reg1, reg3, ga);
  693. trace_kvm_s390_handle_stctl(vcpu, 0, reg1, reg3, ga);
  694. reg = reg1;
  695. do {
  696. val = vcpu->arch.sie_block->gcr[reg] & 0x00000000fffffffful;
  697. rc = write_guest(vcpu, ga, &val, sizeof(val));
  698. if (rc)
  699. return kvm_s390_inject_prog_cond(vcpu, rc);
  700. ga += 4;
  701. if (reg == reg3)
  702. break;
  703. reg = (reg + 1) % 16;
  704. } while (1);
  705. return 0;
  706. }
  707. static int handle_lctlg(struct kvm_vcpu *vcpu)
  708. {
  709. int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
  710. int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
  711. u64 ga, val;
  712. int reg, rc;
  713. vcpu->stat.instruction_lctlg++;
  714. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
  715. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  716. ga = kvm_s390_get_base_disp_rsy(vcpu);
  717. if (ga & 7)
  718. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  719. reg = reg1;
  720. VCPU_EVENT(vcpu, 5, "lctlg r1:%x, r3:%x, addr:%llx", reg1, reg3, ga);
  721. trace_kvm_s390_handle_lctl(vcpu, 1, reg1, reg3, ga);
  722. do {
  723. rc = read_guest(vcpu, ga, &val, sizeof(val));
  724. if (rc)
  725. return kvm_s390_inject_prog_cond(vcpu, rc);
  726. vcpu->arch.sie_block->gcr[reg] = val;
  727. ga += 8;
  728. if (reg == reg3)
  729. break;
  730. reg = (reg + 1) % 16;
  731. } while (1);
  732. return 0;
  733. }
  734. static int handle_stctg(struct kvm_vcpu *vcpu)
  735. {
  736. int reg1 = (vcpu->arch.sie_block->ipa & 0x00f0) >> 4;
  737. int reg3 = vcpu->arch.sie_block->ipa & 0x000f;
  738. u64 ga, val;
  739. int reg, rc;
  740. vcpu->stat.instruction_stctg++;
  741. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
  742. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  743. ga = kvm_s390_get_base_disp_rsy(vcpu);
  744. if (ga & 7)
  745. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  746. reg = reg1;
  747. VCPU_EVENT(vcpu, 5, "stctg r1:%x, r3:%x, addr:%llx", reg1, reg3, ga);
  748. trace_kvm_s390_handle_stctl(vcpu, 1, reg1, reg3, ga);
  749. do {
  750. val = vcpu->arch.sie_block->gcr[reg];
  751. rc = write_guest(vcpu, ga, &val, sizeof(val));
  752. if (rc)
  753. return kvm_s390_inject_prog_cond(vcpu, rc);
  754. ga += 8;
  755. if (reg == reg3)
  756. break;
  757. reg = (reg + 1) % 16;
  758. } while (1);
  759. return 0;
  760. }
  761. static const intercept_handler_t eb_handlers[256] = {
  762. [0x2f] = handle_lctlg,
  763. [0x25] = handle_stctg,
  764. };
  765. int kvm_s390_handle_eb(struct kvm_vcpu *vcpu)
  766. {
  767. intercept_handler_t handler;
  768. handler = eb_handlers[vcpu->arch.sie_block->ipb & 0xff];
  769. if (handler)
  770. return handler(vcpu);
  771. return -EOPNOTSUPP;
  772. }
  773. static int handle_tprot(struct kvm_vcpu *vcpu)
  774. {
  775. u64 address1, address2;
  776. unsigned long hva, gpa;
  777. int ret = 0, cc = 0;
  778. bool writable;
  779. vcpu->stat.instruction_tprot++;
  780. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
  781. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  782. kvm_s390_get_base_disp_sse(vcpu, &address1, &address2);
  783. /* we only handle the Linux memory detection case:
  784. * access key == 0
  785. * everything else goes to userspace. */
  786. if (address2 & 0xf0)
  787. return -EOPNOTSUPP;
  788. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_DAT)
  789. ipte_lock(vcpu);
  790. ret = guest_translate_address(vcpu, address1, &gpa, 1);
  791. if (ret == PGM_PROTECTION) {
  792. /* Write protected? Try again with read-only... */
  793. cc = 1;
  794. ret = guest_translate_address(vcpu, address1, &gpa, 0);
  795. }
  796. if (ret) {
  797. if (ret == PGM_ADDRESSING || ret == PGM_TRANSLATION_SPEC) {
  798. ret = kvm_s390_inject_program_int(vcpu, ret);
  799. } else if (ret > 0) {
  800. /* Translation not available */
  801. kvm_s390_set_psw_cc(vcpu, 3);
  802. ret = 0;
  803. }
  804. goto out_unlock;
  805. }
  806. hva = gfn_to_hva_prot(vcpu->kvm, gpa_to_gfn(gpa), &writable);
  807. if (kvm_is_error_hva(hva)) {
  808. ret = kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
  809. } else {
  810. if (!writable)
  811. cc = 1; /* Write not permitted ==> read-only */
  812. kvm_s390_set_psw_cc(vcpu, cc);
  813. /* Note: CC2 only occurs for storage keys (not supported yet) */
  814. }
  815. out_unlock:
  816. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_DAT)
  817. ipte_unlock(vcpu);
  818. return ret;
  819. }
  820. int kvm_s390_handle_e5(struct kvm_vcpu *vcpu)
  821. {
  822. /* For e5xx... instructions we only handle TPROT */
  823. if ((vcpu->arch.sie_block->ipa & 0x00ff) == 0x01)
  824. return handle_tprot(vcpu);
  825. return -EOPNOTSUPP;
  826. }
  827. static int handle_sckpf(struct kvm_vcpu *vcpu)
  828. {
  829. u32 value;
  830. if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
  831. return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
  832. if (vcpu->run->s.regs.gprs[0] & 0x00000000ffff0000)
  833. return kvm_s390_inject_program_int(vcpu,
  834. PGM_SPECIFICATION);
  835. value = vcpu->run->s.regs.gprs[0] & 0x000000000000ffff;
  836. vcpu->arch.sie_block->todpr = value;
  837. return 0;
  838. }
  839. static const intercept_handler_t x01_handlers[256] = {
  840. [0x07] = handle_sckpf,
  841. };
  842. int kvm_s390_handle_01(struct kvm_vcpu *vcpu)
  843. {
  844. intercept_handler_t handler;
  845. handler = x01_handlers[vcpu->arch.sie_block->ipa & 0x00ff];
  846. if (handler)
  847. return handler(vcpu);
  848. return -EOPNOTSUPP;
  849. }