sthyi.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. /*
  2. * store hypervisor information instruction emulation functions.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License (version 2 only)
  6. * as published by the Free Software Foundation.
  7. *
  8. * Copyright IBM Corp. 2016
  9. * Author(s): Janosch Frank <frankja@linux.vnet.ibm.com>
  10. */
  11. #include <linux/kvm_host.h>
  12. #include <linux/errno.h>
  13. #include <linux/pagemap.h>
  14. #include <linux/vmalloc.h>
  15. #include <linux/ratelimit.h>
  16. #include <asm/kvm_host.h>
  17. #include <asm/asm-offsets.h>
  18. #include <asm/sclp.h>
  19. #include <asm/diag.h>
  20. #include <asm/sysinfo.h>
  21. #include <asm/ebcdic.h>
  22. #include "kvm-s390.h"
  23. #include "gaccess.h"
  24. #include "trace.h"
  25. #define DED_WEIGHT 0xffff
  26. /*
  27. * CP and IFL as EBCDIC strings, SP/0x40 determines the end of string
  28. * as they are justified with spaces.
  29. */
  30. #define CP 0xc3d7404040404040UL
  31. #define IFL 0xc9c6d34040404040UL
  32. enum hdr_flags {
  33. HDR_NOT_LPAR = 0x10,
  34. HDR_STACK_INCM = 0x20,
  35. HDR_STSI_UNAV = 0x40,
  36. HDR_PERF_UNAV = 0x80,
  37. };
  38. enum mac_validity {
  39. MAC_NAME_VLD = 0x20,
  40. MAC_ID_VLD = 0x40,
  41. MAC_CNT_VLD = 0x80,
  42. };
  43. enum par_flag {
  44. PAR_MT_EN = 0x80,
  45. };
  46. enum par_validity {
  47. PAR_GRP_VLD = 0x08,
  48. PAR_ID_VLD = 0x10,
  49. PAR_ABS_VLD = 0x20,
  50. PAR_WGHT_VLD = 0x40,
  51. PAR_PCNT_VLD = 0x80,
  52. };
  53. struct hdr_sctn {
  54. u8 infhflg1;
  55. u8 infhflg2; /* reserved */
  56. u8 infhval1; /* reserved */
  57. u8 infhval2; /* reserved */
  58. u8 reserved[3];
  59. u8 infhygct;
  60. u16 infhtotl;
  61. u16 infhdln;
  62. u16 infmoff;
  63. u16 infmlen;
  64. u16 infpoff;
  65. u16 infplen;
  66. u16 infhoff1;
  67. u16 infhlen1;
  68. u16 infgoff1;
  69. u16 infglen1;
  70. u16 infhoff2;
  71. u16 infhlen2;
  72. u16 infgoff2;
  73. u16 infglen2;
  74. u16 infhoff3;
  75. u16 infhlen3;
  76. u16 infgoff3;
  77. u16 infglen3;
  78. u8 reserved2[4];
  79. } __packed;
  80. struct mac_sctn {
  81. u8 infmflg1; /* reserved */
  82. u8 infmflg2; /* reserved */
  83. u8 infmval1;
  84. u8 infmval2; /* reserved */
  85. u16 infmscps;
  86. u16 infmdcps;
  87. u16 infmsifl;
  88. u16 infmdifl;
  89. char infmname[8];
  90. char infmtype[4];
  91. char infmmanu[16];
  92. char infmseq[16];
  93. char infmpman[4];
  94. u8 reserved[4];
  95. } __packed;
  96. struct par_sctn {
  97. u8 infpflg1;
  98. u8 infpflg2; /* reserved */
  99. u8 infpval1;
  100. u8 infpval2; /* reserved */
  101. u16 infppnum;
  102. u16 infpscps;
  103. u16 infpdcps;
  104. u16 infpsifl;
  105. u16 infpdifl;
  106. u16 reserved;
  107. char infppnam[8];
  108. u32 infpwbcp;
  109. u32 infpabcp;
  110. u32 infpwbif;
  111. u32 infpabif;
  112. char infplgnm[8];
  113. u32 infplgcp;
  114. u32 infplgif;
  115. } __packed;
  116. struct sthyi_sctns {
  117. struct hdr_sctn hdr;
  118. struct mac_sctn mac;
  119. struct par_sctn par;
  120. } __packed;
  121. struct cpu_inf {
  122. u64 lpar_cap;
  123. u64 lpar_grp_cap;
  124. u64 lpar_weight;
  125. u64 all_weight;
  126. int cpu_num_ded;
  127. int cpu_num_shd;
  128. };
  129. struct lpar_cpu_inf {
  130. struct cpu_inf cp;
  131. struct cpu_inf ifl;
  132. };
  133. static inline u64 cpu_id(u8 ctidx, void *diag224_buf)
  134. {
  135. return *((u64 *)(diag224_buf + (ctidx + 1) * DIAG204_CPU_NAME_LEN));
  136. }
  137. /*
  138. * Scales the cpu capping from the lpar range to the one expected in
  139. * sthyi data.
  140. *
  141. * diag204 reports a cap in hundredths of processor units.
  142. * z/VM's range for one core is 0 - 0x10000.
  143. */
  144. static u32 scale_cap(u32 in)
  145. {
  146. return (0x10000 * in) / 100;
  147. }
  148. static void fill_hdr(struct sthyi_sctns *sctns)
  149. {
  150. sctns->hdr.infhdln = sizeof(sctns->hdr);
  151. sctns->hdr.infmoff = sizeof(sctns->hdr);
  152. sctns->hdr.infmlen = sizeof(sctns->mac);
  153. sctns->hdr.infplen = sizeof(sctns->par);
  154. sctns->hdr.infpoff = sctns->hdr.infhdln + sctns->hdr.infmlen;
  155. sctns->hdr.infhtotl = sctns->hdr.infpoff + sctns->hdr.infplen;
  156. }
  157. static void fill_stsi_mac(struct sthyi_sctns *sctns,
  158. struct sysinfo_1_1_1 *sysinfo)
  159. {
  160. if (stsi(sysinfo, 1, 1, 1))
  161. return;
  162. sclp_ocf_cpc_name_copy(sctns->mac.infmname);
  163. memcpy(sctns->mac.infmtype, sysinfo->type, sizeof(sctns->mac.infmtype));
  164. memcpy(sctns->mac.infmmanu, sysinfo->manufacturer, sizeof(sctns->mac.infmmanu));
  165. memcpy(sctns->mac.infmpman, sysinfo->plant, sizeof(sctns->mac.infmpman));
  166. memcpy(sctns->mac.infmseq, sysinfo->sequence, sizeof(sctns->mac.infmseq));
  167. sctns->mac.infmval1 |= MAC_ID_VLD | MAC_NAME_VLD;
  168. }
  169. static void fill_stsi_par(struct sthyi_sctns *sctns,
  170. struct sysinfo_2_2_2 *sysinfo)
  171. {
  172. if (stsi(sysinfo, 2, 2, 2))
  173. return;
  174. sctns->par.infppnum = sysinfo->lpar_number;
  175. memcpy(sctns->par.infppnam, sysinfo->name, sizeof(sctns->par.infppnam));
  176. sctns->par.infpval1 |= PAR_ID_VLD;
  177. }
  178. static void fill_stsi(struct sthyi_sctns *sctns)
  179. {
  180. void *sysinfo;
  181. /* Errors are handled through the validity bits in the response. */
  182. sysinfo = (void *)__get_free_page(GFP_KERNEL);
  183. if (!sysinfo)
  184. return;
  185. fill_stsi_mac(sctns, sysinfo);
  186. fill_stsi_par(sctns, sysinfo);
  187. free_pages((unsigned long)sysinfo, 0);
  188. }
  189. static void fill_diag_mac(struct sthyi_sctns *sctns,
  190. struct diag204_x_phys_block *block,
  191. void *diag224_buf)
  192. {
  193. int i;
  194. for (i = 0; i < block->hdr.cpus; i++) {
  195. switch (cpu_id(block->cpus[i].ctidx, diag224_buf)) {
  196. case CP:
  197. if (block->cpus[i].weight == DED_WEIGHT)
  198. sctns->mac.infmdcps++;
  199. else
  200. sctns->mac.infmscps++;
  201. break;
  202. case IFL:
  203. if (block->cpus[i].weight == DED_WEIGHT)
  204. sctns->mac.infmdifl++;
  205. else
  206. sctns->mac.infmsifl++;
  207. break;
  208. }
  209. }
  210. sctns->mac.infmval1 |= MAC_CNT_VLD;
  211. }
  212. /* Returns a pointer to the the next partition block. */
  213. static struct diag204_x_part_block *lpar_cpu_inf(struct lpar_cpu_inf *part_inf,
  214. bool this_lpar,
  215. void *diag224_buf,
  216. struct diag204_x_part_block *block)
  217. {
  218. int i, capped = 0, weight_cp = 0, weight_ifl = 0;
  219. struct cpu_inf *cpu_inf;
  220. for (i = 0; i < block->hdr.rcpus; i++) {
  221. if (!(block->cpus[i].cflag & DIAG204_CPU_ONLINE))
  222. continue;
  223. switch (cpu_id(block->cpus[i].ctidx, diag224_buf)) {
  224. case CP:
  225. cpu_inf = &part_inf->cp;
  226. if (block->cpus[i].cur_weight < DED_WEIGHT)
  227. weight_cp |= block->cpus[i].cur_weight;
  228. break;
  229. case IFL:
  230. cpu_inf = &part_inf->ifl;
  231. if (block->cpus[i].cur_weight < DED_WEIGHT)
  232. weight_ifl |= block->cpus[i].cur_weight;
  233. break;
  234. default:
  235. continue;
  236. }
  237. if (!this_lpar)
  238. continue;
  239. capped |= block->cpus[i].cflag & DIAG204_CPU_CAPPED;
  240. cpu_inf->lpar_cap |= block->cpus[i].cpu_type_cap;
  241. cpu_inf->lpar_grp_cap |= block->cpus[i].group_cpu_type_cap;
  242. if (block->cpus[i].weight == DED_WEIGHT)
  243. cpu_inf->cpu_num_ded += 1;
  244. else
  245. cpu_inf->cpu_num_shd += 1;
  246. }
  247. if (this_lpar && capped) {
  248. part_inf->cp.lpar_weight = weight_cp;
  249. part_inf->ifl.lpar_weight = weight_ifl;
  250. }
  251. part_inf->cp.all_weight += weight_cp;
  252. part_inf->ifl.all_weight += weight_ifl;
  253. return (struct diag204_x_part_block *)&block->cpus[i];
  254. }
  255. static void fill_diag(struct sthyi_sctns *sctns)
  256. {
  257. int i, r, pages;
  258. bool this_lpar;
  259. void *diag204_buf;
  260. void *diag224_buf = NULL;
  261. struct diag204_x_info_blk_hdr *ti_hdr;
  262. struct diag204_x_part_block *part_block;
  263. struct diag204_x_phys_block *phys_block;
  264. struct lpar_cpu_inf lpar_inf = {};
  265. /* Errors are handled through the validity bits in the response. */
  266. pages = diag204((unsigned long)DIAG204_SUBC_RSI |
  267. (unsigned long)DIAG204_INFO_EXT, 0, NULL);
  268. if (pages <= 0)
  269. return;
  270. diag204_buf = vmalloc(PAGE_SIZE * pages);
  271. if (!diag204_buf)
  272. return;
  273. r = diag204((unsigned long)DIAG204_SUBC_STIB7 |
  274. (unsigned long)DIAG204_INFO_EXT, pages, diag204_buf);
  275. if (r < 0)
  276. goto out;
  277. diag224_buf = (void *)__get_free_page(GFP_KERNEL | GFP_DMA);
  278. if (!diag224_buf || diag224(diag224_buf))
  279. goto out;
  280. ti_hdr = diag204_buf;
  281. part_block = diag204_buf + sizeof(*ti_hdr);
  282. for (i = 0; i < ti_hdr->npar; i++) {
  283. /*
  284. * For the calling lpar we also need to get the cpu
  285. * caps and weights. The time information block header
  286. * specifies the offset to the partition block of the
  287. * caller lpar, so we know when we process its data.
  288. */
  289. this_lpar = (void *)part_block - diag204_buf == ti_hdr->this_part;
  290. part_block = lpar_cpu_inf(&lpar_inf, this_lpar, diag224_buf,
  291. part_block);
  292. }
  293. phys_block = (struct diag204_x_phys_block *)part_block;
  294. part_block = diag204_buf + ti_hdr->this_part;
  295. if (part_block->hdr.mtid)
  296. sctns->par.infpflg1 = PAR_MT_EN;
  297. sctns->par.infpval1 |= PAR_GRP_VLD;
  298. sctns->par.infplgcp = scale_cap(lpar_inf.cp.lpar_grp_cap);
  299. sctns->par.infplgif = scale_cap(lpar_inf.ifl.lpar_grp_cap);
  300. memcpy(sctns->par.infplgnm, part_block->hdr.hardware_group_name,
  301. sizeof(sctns->par.infplgnm));
  302. sctns->par.infpscps = lpar_inf.cp.cpu_num_shd;
  303. sctns->par.infpdcps = lpar_inf.cp.cpu_num_ded;
  304. sctns->par.infpsifl = lpar_inf.ifl.cpu_num_shd;
  305. sctns->par.infpdifl = lpar_inf.ifl.cpu_num_ded;
  306. sctns->par.infpval1 |= PAR_PCNT_VLD;
  307. sctns->par.infpabcp = scale_cap(lpar_inf.cp.lpar_cap);
  308. sctns->par.infpabif = scale_cap(lpar_inf.ifl.lpar_cap);
  309. sctns->par.infpval1 |= PAR_ABS_VLD;
  310. /*
  311. * Everything below needs global performance data to be
  312. * meaningful.
  313. */
  314. if (!(ti_hdr->flags & DIAG204_LPAR_PHYS_FLG)) {
  315. sctns->hdr.infhflg1 |= HDR_PERF_UNAV;
  316. goto out;
  317. }
  318. fill_diag_mac(sctns, phys_block, diag224_buf);
  319. if (lpar_inf.cp.lpar_weight) {
  320. sctns->par.infpwbcp = sctns->mac.infmscps * 0x10000 *
  321. lpar_inf.cp.lpar_weight / lpar_inf.cp.all_weight;
  322. }
  323. if (lpar_inf.ifl.lpar_weight) {
  324. sctns->par.infpwbif = sctns->mac.infmsifl * 0x10000 *
  325. lpar_inf.ifl.lpar_weight / lpar_inf.ifl.all_weight;
  326. }
  327. sctns->par.infpval1 |= PAR_WGHT_VLD;
  328. out:
  329. free_page((unsigned long)diag224_buf);
  330. vfree(diag204_buf);
  331. }
  332. static int sthyi(u64 vaddr)
  333. {
  334. register u64 code asm("0") = 0;
  335. register u64 addr asm("2") = vaddr;
  336. int cc;
  337. asm volatile(
  338. ".insn rre,0xB2560000,%[code],%[addr]\n"
  339. "ipm %[cc]\n"
  340. "srl %[cc],28\n"
  341. : [cc] "=d" (cc)
  342. : [code] "d" (code), [addr] "a" (addr)
  343. : "3", "memory", "cc");
  344. return cc;
  345. }
  346. int handle_sthyi(struct kvm_vcpu *vcpu)
  347. {
  348. int reg1, reg2, r = 0;
  349. u64 code, addr, cc = 0;
  350. struct sthyi_sctns *sctns = NULL;
  351. /*
  352. * STHYI requires extensive locking in the higher hypervisors
  353. * and is very computational/memory expensive. Therefore we
  354. * ratelimit the executions per VM.
  355. */
  356. if (!__ratelimit(&vcpu->kvm->arch.sthyi_limit)) {
  357. kvm_s390_retry_instr(vcpu);
  358. return 0;
  359. }
  360. kvm_s390_get_regs_rre(vcpu, &reg1, &reg2);
  361. code = vcpu->run->s.regs.gprs[reg1];
  362. addr = vcpu->run->s.regs.gprs[reg2];
  363. vcpu->stat.instruction_sthyi++;
  364. VCPU_EVENT(vcpu, 3, "STHYI: fc: %llu addr: 0x%016llx", code, addr);
  365. trace_kvm_s390_handle_sthyi(vcpu, code, addr);
  366. if (reg1 == reg2 || reg1 & 1 || reg2 & 1)
  367. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  368. if (code & 0xffff) {
  369. cc = 3;
  370. goto out;
  371. }
  372. if (addr & ~PAGE_MASK)
  373. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  374. /*
  375. * If the page has not yet been faulted in, we want to do that
  376. * now and not after all the expensive calculations.
  377. */
  378. r = write_guest(vcpu, addr, reg2, &cc, 1);
  379. if (r)
  380. return kvm_s390_inject_prog_cond(vcpu, r);
  381. sctns = (void *)get_zeroed_page(GFP_KERNEL);
  382. if (!sctns)
  383. return -ENOMEM;
  384. /*
  385. * If we are a guest, we don't want to emulate an emulated
  386. * instruction. We ask the hypervisor to provide the data.
  387. */
  388. if (test_facility(74)) {
  389. cc = sthyi((u64)sctns);
  390. goto out;
  391. }
  392. fill_hdr(sctns);
  393. fill_stsi(sctns);
  394. fill_diag(sctns);
  395. out:
  396. if (!cc) {
  397. r = write_guest(vcpu, addr, reg2, sctns, PAGE_SIZE);
  398. if (r) {
  399. free_page((unsigned long)sctns);
  400. return kvm_s390_inject_prog_cond(vcpu, r);
  401. }
  402. }
  403. free_page((unsigned long)sctns);
  404. vcpu->run->s.regs.gprs[reg2 + 1] = cc ? 4 : 0;
  405. kvm_s390_set_psw_cc(vcpu, cc);
  406. return r;
  407. }