sthyi.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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. sclp_ocf_cpc_name_copy(sctns->mac.infmname);
  161. if (*(u64 *)sctns->mac.infmname != 0)
  162. sctns->mac.infmval1 |= MAC_NAME_VLD;
  163. if (stsi(sysinfo, 1, 1, 1))
  164. return;
  165. memcpy(sctns->mac.infmtype, sysinfo->type, sizeof(sctns->mac.infmtype));
  166. memcpy(sctns->mac.infmmanu, sysinfo->manufacturer, sizeof(sctns->mac.infmmanu));
  167. memcpy(sctns->mac.infmpman, sysinfo->plant, sizeof(sctns->mac.infmpman));
  168. memcpy(sctns->mac.infmseq, sysinfo->sequence, sizeof(sctns->mac.infmseq));
  169. sctns->mac.infmval1 |= MAC_ID_VLD;
  170. }
  171. static void fill_stsi_par(struct sthyi_sctns *sctns,
  172. struct sysinfo_2_2_2 *sysinfo)
  173. {
  174. if (stsi(sysinfo, 2, 2, 2))
  175. return;
  176. sctns->par.infppnum = sysinfo->lpar_number;
  177. memcpy(sctns->par.infppnam, sysinfo->name, sizeof(sctns->par.infppnam));
  178. sctns->par.infpval1 |= PAR_ID_VLD;
  179. }
  180. static void fill_stsi(struct sthyi_sctns *sctns)
  181. {
  182. void *sysinfo;
  183. /* Errors are handled through the validity bits in the response. */
  184. sysinfo = (void *)__get_free_page(GFP_KERNEL);
  185. if (!sysinfo)
  186. return;
  187. fill_stsi_mac(sctns, sysinfo);
  188. fill_stsi_par(sctns, sysinfo);
  189. free_pages((unsigned long)sysinfo, 0);
  190. }
  191. static void fill_diag_mac(struct sthyi_sctns *sctns,
  192. struct diag204_x_phys_block *block,
  193. void *diag224_buf)
  194. {
  195. int i;
  196. for (i = 0; i < block->hdr.cpus; i++) {
  197. switch (cpu_id(block->cpus[i].ctidx, diag224_buf)) {
  198. case CP:
  199. if (block->cpus[i].weight == DED_WEIGHT)
  200. sctns->mac.infmdcps++;
  201. else
  202. sctns->mac.infmscps++;
  203. break;
  204. case IFL:
  205. if (block->cpus[i].weight == DED_WEIGHT)
  206. sctns->mac.infmdifl++;
  207. else
  208. sctns->mac.infmsifl++;
  209. break;
  210. }
  211. }
  212. sctns->mac.infmval1 |= MAC_CNT_VLD;
  213. }
  214. /* Returns a pointer to the the next partition block. */
  215. static struct diag204_x_part_block *lpar_cpu_inf(struct lpar_cpu_inf *part_inf,
  216. bool this_lpar,
  217. void *diag224_buf,
  218. struct diag204_x_part_block *block)
  219. {
  220. int i, capped = 0, weight_cp = 0, weight_ifl = 0;
  221. struct cpu_inf *cpu_inf;
  222. for (i = 0; i < block->hdr.rcpus; i++) {
  223. if (!(block->cpus[i].cflag & DIAG204_CPU_ONLINE))
  224. continue;
  225. switch (cpu_id(block->cpus[i].ctidx, diag224_buf)) {
  226. case CP:
  227. cpu_inf = &part_inf->cp;
  228. if (block->cpus[i].cur_weight < DED_WEIGHT)
  229. weight_cp |= block->cpus[i].cur_weight;
  230. break;
  231. case IFL:
  232. cpu_inf = &part_inf->ifl;
  233. if (block->cpus[i].cur_weight < DED_WEIGHT)
  234. weight_ifl |= block->cpus[i].cur_weight;
  235. break;
  236. default:
  237. continue;
  238. }
  239. if (!this_lpar)
  240. continue;
  241. capped |= block->cpus[i].cflag & DIAG204_CPU_CAPPED;
  242. cpu_inf->lpar_cap |= block->cpus[i].cpu_type_cap;
  243. cpu_inf->lpar_grp_cap |= block->cpus[i].group_cpu_type_cap;
  244. if (block->cpus[i].weight == DED_WEIGHT)
  245. cpu_inf->cpu_num_ded += 1;
  246. else
  247. cpu_inf->cpu_num_shd += 1;
  248. }
  249. if (this_lpar && capped) {
  250. part_inf->cp.lpar_weight = weight_cp;
  251. part_inf->ifl.lpar_weight = weight_ifl;
  252. }
  253. part_inf->cp.all_weight += weight_cp;
  254. part_inf->ifl.all_weight += weight_ifl;
  255. return (struct diag204_x_part_block *)&block->cpus[i];
  256. }
  257. static void fill_diag(struct sthyi_sctns *sctns)
  258. {
  259. int i, r, pages;
  260. bool this_lpar;
  261. void *diag204_buf;
  262. void *diag224_buf = NULL;
  263. struct diag204_x_info_blk_hdr *ti_hdr;
  264. struct diag204_x_part_block *part_block;
  265. struct diag204_x_phys_block *phys_block;
  266. struct lpar_cpu_inf lpar_inf = {};
  267. /* Errors are handled through the validity bits in the response. */
  268. pages = diag204((unsigned long)DIAG204_SUBC_RSI |
  269. (unsigned long)DIAG204_INFO_EXT, 0, NULL);
  270. if (pages <= 0)
  271. return;
  272. diag204_buf = vmalloc(PAGE_SIZE * pages);
  273. if (!diag204_buf)
  274. return;
  275. r = diag204((unsigned long)DIAG204_SUBC_STIB7 |
  276. (unsigned long)DIAG204_INFO_EXT, pages, diag204_buf);
  277. if (r < 0)
  278. goto out;
  279. diag224_buf = (void *)__get_free_page(GFP_KERNEL | GFP_DMA);
  280. if (!diag224_buf || diag224(diag224_buf))
  281. goto out;
  282. ti_hdr = diag204_buf;
  283. part_block = diag204_buf + sizeof(*ti_hdr);
  284. for (i = 0; i < ti_hdr->npar; i++) {
  285. /*
  286. * For the calling lpar we also need to get the cpu
  287. * caps and weights. The time information block header
  288. * specifies the offset to the partition block of the
  289. * caller lpar, so we know when we process its data.
  290. */
  291. this_lpar = (void *)part_block - diag204_buf == ti_hdr->this_part;
  292. part_block = lpar_cpu_inf(&lpar_inf, this_lpar, diag224_buf,
  293. part_block);
  294. }
  295. phys_block = (struct diag204_x_phys_block *)part_block;
  296. part_block = diag204_buf + ti_hdr->this_part;
  297. if (part_block->hdr.mtid)
  298. sctns->par.infpflg1 = PAR_MT_EN;
  299. sctns->par.infpval1 |= PAR_GRP_VLD;
  300. sctns->par.infplgcp = scale_cap(lpar_inf.cp.lpar_grp_cap);
  301. sctns->par.infplgif = scale_cap(lpar_inf.ifl.lpar_grp_cap);
  302. memcpy(sctns->par.infplgnm, part_block->hdr.hardware_group_name,
  303. sizeof(sctns->par.infplgnm));
  304. sctns->par.infpscps = lpar_inf.cp.cpu_num_shd;
  305. sctns->par.infpdcps = lpar_inf.cp.cpu_num_ded;
  306. sctns->par.infpsifl = lpar_inf.ifl.cpu_num_shd;
  307. sctns->par.infpdifl = lpar_inf.ifl.cpu_num_ded;
  308. sctns->par.infpval1 |= PAR_PCNT_VLD;
  309. sctns->par.infpabcp = scale_cap(lpar_inf.cp.lpar_cap);
  310. sctns->par.infpabif = scale_cap(lpar_inf.ifl.lpar_cap);
  311. sctns->par.infpval1 |= PAR_ABS_VLD;
  312. /*
  313. * Everything below needs global performance data to be
  314. * meaningful.
  315. */
  316. if (!(ti_hdr->flags & DIAG204_LPAR_PHYS_FLG)) {
  317. sctns->hdr.infhflg1 |= HDR_PERF_UNAV;
  318. goto out;
  319. }
  320. fill_diag_mac(sctns, phys_block, diag224_buf);
  321. if (lpar_inf.cp.lpar_weight) {
  322. sctns->par.infpwbcp = sctns->mac.infmscps * 0x10000 *
  323. lpar_inf.cp.lpar_weight / lpar_inf.cp.all_weight;
  324. }
  325. if (lpar_inf.ifl.lpar_weight) {
  326. sctns->par.infpwbif = sctns->mac.infmsifl * 0x10000 *
  327. lpar_inf.ifl.lpar_weight / lpar_inf.ifl.all_weight;
  328. }
  329. sctns->par.infpval1 |= PAR_WGHT_VLD;
  330. out:
  331. free_page((unsigned long)diag224_buf);
  332. vfree(diag204_buf);
  333. }
  334. static int sthyi(u64 vaddr)
  335. {
  336. register u64 code asm("0") = 0;
  337. register u64 addr asm("2") = vaddr;
  338. int cc;
  339. asm volatile(
  340. ".insn rre,0xB2560000,%[code],%[addr]\n"
  341. "ipm %[cc]\n"
  342. "srl %[cc],28\n"
  343. : [cc] "=d" (cc)
  344. : [code] "d" (code), [addr] "a" (addr)
  345. : "3", "memory", "cc");
  346. return cc;
  347. }
  348. int handle_sthyi(struct kvm_vcpu *vcpu)
  349. {
  350. int reg1, reg2, r = 0;
  351. u64 code, addr, cc = 0;
  352. struct sthyi_sctns *sctns = NULL;
  353. if (!test_kvm_facility(vcpu->kvm, 74))
  354. return kvm_s390_inject_program_int(vcpu, PGM_OPERATION);
  355. /*
  356. * STHYI requires extensive locking in the higher hypervisors
  357. * and is very computational/memory expensive. Therefore we
  358. * ratelimit the executions per VM.
  359. */
  360. if (!__ratelimit(&vcpu->kvm->arch.sthyi_limit)) {
  361. kvm_s390_retry_instr(vcpu);
  362. return 0;
  363. }
  364. kvm_s390_get_regs_rre(vcpu, &reg1, &reg2);
  365. code = vcpu->run->s.regs.gprs[reg1];
  366. addr = vcpu->run->s.regs.gprs[reg2];
  367. vcpu->stat.instruction_sthyi++;
  368. VCPU_EVENT(vcpu, 3, "STHYI: fc: %llu addr: 0x%016llx", code, addr);
  369. trace_kvm_s390_handle_sthyi(vcpu, code, addr);
  370. if (reg1 == reg2 || reg1 & 1 || reg2 & 1)
  371. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  372. if (code & 0xffff) {
  373. cc = 3;
  374. goto out;
  375. }
  376. if (addr & ~PAGE_MASK)
  377. return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
  378. sctns = (void *)get_zeroed_page(GFP_KERNEL);
  379. if (!sctns)
  380. return -ENOMEM;
  381. /*
  382. * If we are a guest, we don't want to emulate an emulated
  383. * instruction. We ask the hypervisor to provide the data.
  384. */
  385. if (test_facility(74)) {
  386. cc = sthyi((u64)sctns);
  387. goto out;
  388. }
  389. fill_hdr(sctns);
  390. fill_stsi(sctns);
  391. fill_diag(sctns);
  392. out:
  393. if (!cc) {
  394. r = write_guest(vcpu, addr, reg2, sctns, PAGE_SIZE);
  395. if (r) {
  396. free_page((unsigned long)sctns);
  397. return kvm_s390_inject_prog_cond(vcpu, r);
  398. }
  399. }
  400. free_page((unsigned long)sctns);
  401. vcpu->run->s.regs.gprs[reg2 + 1] = cc ? 4 : 0;
  402. kvm_s390_set_psw_cc(vcpu, cc);
  403. return r;
  404. }