2 * store hypervisor information instruction emulation functions.
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.
8 * Copyright IBM Corp. 2016
9 * Author(s): Janosch Frank <frankja@linux.vnet.ibm.com>
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>
17 #include <asm/kvm_host.h>
18 #include <asm/asm-offsets.h>
21 #include <asm/sysinfo.h>
22 #include <asm/ebcdic.h>
28 #define DED_WEIGHT 0xffff
30 * CP and IFL as EBCDIC strings, SP/0x40 determines the end of string
31 * as they are justified with spaces.
33 #define CP 0xc3d7404040404040UL
34 #define IFL 0xc9c6d34040404040UL
38 HDR_STACK_INCM = 0x20,
63 u8 infhflg2; /* reserved */
64 u8 infhval1; /* reserved */
65 u8 infhval2; /* reserved */
90 u8 infmflg1; /* reserved */
91 u8 infmflg2; /* reserved */
93 u8 infmval2; /* reserved */
108 u8 infpflg2; /* reserved */
110 u8 infpval2; /* reserved */
142 struct lpar_cpu_inf {
147 static inline u64 cpu_id(u8 ctidx, void *diag224_buf)
149 return *((u64 *)(diag224_buf + (ctidx + 1) * DIAG204_CPU_NAME_LEN));
153 * Scales the cpu capping from the lpar range to the one expected in
156 * diag204 reports a cap in hundredths of processor units.
157 * z/VM's range for one core is 0 - 0x10000.
159 static u32 scale_cap(u32 in)
161 return (0x10000 * in) / 100;
164 static void fill_hdr(struct sthyi_sctns *sctns)
166 sctns->hdr.infhdln = sizeof(sctns->hdr);
167 sctns->hdr.infmoff = sizeof(sctns->hdr);
168 sctns->hdr.infmlen = sizeof(sctns->mac);
169 sctns->hdr.infplen = sizeof(sctns->par);
170 sctns->hdr.infpoff = sctns->hdr.infhdln + sctns->hdr.infmlen;
171 sctns->hdr.infhtotl = sctns->hdr.infpoff + sctns->hdr.infplen;
174 static void fill_stsi_mac(struct sthyi_sctns *sctns,
175 struct sysinfo_1_1_1 *sysinfo)
177 sclp_ocf_cpc_name_copy(sctns->mac.infmname);
178 if (*(u64 *)sctns->mac.infmname != 0)
179 sctns->mac.infmval1 |= MAC_NAME_VLD;
181 if (stsi(sysinfo, 1, 1, 1))
184 memcpy(sctns->mac.infmtype, sysinfo->type, sizeof(sctns->mac.infmtype));
185 memcpy(sctns->mac.infmmanu, sysinfo->manufacturer, sizeof(sctns->mac.infmmanu));
186 memcpy(sctns->mac.infmpman, sysinfo->plant, sizeof(sctns->mac.infmpman));
187 memcpy(sctns->mac.infmseq, sysinfo->sequence, sizeof(sctns->mac.infmseq));
189 sctns->mac.infmval1 |= MAC_ID_VLD;
192 static void fill_stsi_par(struct sthyi_sctns *sctns,
193 struct sysinfo_2_2_2 *sysinfo)
195 if (stsi(sysinfo, 2, 2, 2))
198 sctns->par.infppnum = sysinfo->lpar_number;
199 memcpy(sctns->par.infppnam, sysinfo->name, sizeof(sctns->par.infppnam));
201 sctns->par.infpval1 |= PAR_ID_VLD;
204 static void fill_stsi(struct sthyi_sctns *sctns)
208 /* Errors are handled through the validity bits in the response. */
209 sysinfo = (void *)__get_free_page(GFP_KERNEL);
213 fill_stsi_mac(sctns, sysinfo);
214 fill_stsi_par(sctns, sysinfo);
216 free_pages((unsigned long)sysinfo, 0);
219 static void fill_diag_mac(struct sthyi_sctns *sctns,
220 struct diag204_x_phys_block *block,
225 for (i = 0; i < block->hdr.cpus; i++) {
226 switch (cpu_id(block->cpus[i].ctidx, diag224_buf)) {
228 if (block->cpus[i].weight == DED_WEIGHT)
229 sctns->mac.infmdcps++;
231 sctns->mac.infmscps++;
234 if (block->cpus[i].weight == DED_WEIGHT)
235 sctns->mac.infmdifl++;
237 sctns->mac.infmsifl++;
241 sctns->mac.infmval1 |= MAC_CNT_VLD;
244 /* Returns a pointer to the the next partition block. */
245 static struct diag204_x_part_block *lpar_cpu_inf(struct lpar_cpu_inf *part_inf,
248 struct diag204_x_part_block *block)
250 int i, capped = 0, weight_cp = 0, weight_ifl = 0;
251 struct cpu_inf *cpu_inf;
253 for (i = 0; i < block->hdr.rcpus; i++) {
254 if (!(block->cpus[i].cflag & DIAG204_CPU_ONLINE))
257 switch (cpu_id(block->cpus[i].ctidx, diag224_buf)) {
259 cpu_inf = &part_inf->cp;
260 if (block->cpus[i].cur_weight < DED_WEIGHT)
261 weight_cp |= block->cpus[i].cur_weight;
264 cpu_inf = &part_inf->ifl;
265 if (block->cpus[i].cur_weight < DED_WEIGHT)
266 weight_ifl |= block->cpus[i].cur_weight;
275 capped |= block->cpus[i].cflag & DIAG204_CPU_CAPPED;
276 cpu_inf->lpar_cap |= block->cpus[i].cpu_type_cap;
277 cpu_inf->lpar_grp_cap |= block->cpus[i].group_cpu_type_cap;
279 if (block->cpus[i].weight == DED_WEIGHT)
280 cpu_inf->cpu_num_ded += 1;
282 cpu_inf->cpu_num_shd += 1;
285 if (this_lpar && capped) {
286 part_inf->cp.lpar_weight = weight_cp;
287 part_inf->ifl.lpar_weight = weight_ifl;
289 part_inf->cp.all_weight += weight_cp;
290 part_inf->ifl.all_weight += weight_ifl;
291 return (struct diag204_x_part_block *)&block->cpus[i];
294 static void fill_diag(struct sthyi_sctns *sctns)
299 void *diag224_buf = NULL;
300 struct diag204_x_info_blk_hdr *ti_hdr;
301 struct diag204_x_part_block *part_block;
302 struct diag204_x_phys_block *phys_block;
303 struct lpar_cpu_inf lpar_inf = {};
305 /* Errors are handled through the validity bits in the response. */
306 pages = diag204((unsigned long)DIAG204_SUBC_RSI |
307 (unsigned long)DIAG204_INFO_EXT, 0, NULL);
311 diag204_buf = vmalloc(PAGE_SIZE * pages);
315 r = diag204((unsigned long)DIAG204_SUBC_STIB7 |
316 (unsigned long)DIAG204_INFO_EXT, pages, diag204_buf);
320 diag224_buf = (void *)__get_free_page(GFP_KERNEL | GFP_DMA);
321 if (!diag224_buf || diag224(diag224_buf))
324 ti_hdr = diag204_buf;
325 part_block = diag204_buf + sizeof(*ti_hdr);
327 for (i = 0; i < ti_hdr->npar; i++) {
329 * For the calling lpar we also need to get the cpu
330 * caps and weights. The time information block header
331 * specifies the offset to the partition block of the
332 * caller lpar, so we know when we process its data.
334 this_lpar = (void *)part_block - diag204_buf == ti_hdr->this_part;
335 part_block = lpar_cpu_inf(&lpar_inf, this_lpar, diag224_buf,
339 phys_block = (struct diag204_x_phys_block *)part_block;
340 part_block = diag204_buf + ti_hdr->this_part;
341 if (part_block->hdr.mtid)
342 sctns->par.infpflg1 = PAR_MT_EN;
344 sctns->par.infpval1 |= PAR_GRP_VLD;
345 sctns->par.infplgcp = scale_cap(lpar_inf.cp.lpar_grp_cap);
346 sctns->par.infplgif = scale_cap(lpar_inf.ifl.lpar_grp_cap);
347 memcpy(sctns->par.infplgnm, part_block->hdr.hardware_group_name,
348 sizeof(sctns->par.infplgnm));
350 sctns->par.infpscps = lpar_inf.cp.cpu_num_shd;
351 sctns->par.infpdcps = lpar_inf.cp.cpu_num_ded;
352 sctns->par.infpsifl = lpar_inf.ifl.cpu_num_shd;
353 sctns->par.infpdifl = lpar_inf.ifl.cpu_num_ded;
354 sctns->par.infpval1 |= PAR_PCNT_VLD;
356 sctns->par.infpabcp = scale_cap(lpar_inf.cp.lpar_cap);
357 sctns->par.infpabif = scale_cap(lpar_inf.ifl.lpar_cap);
358 sctns->par.infpval1 |= PAR_ABS_VLD;
361 * Everything below needs global performance data to be
364 if (!(ti_hdr->flags & DIAG204_LPAR_PHYS_FLG)) {
365 sctns->hdr.infhflg1 |= HDR_PERF_UNAV;
369 fill_diag_mac(sctns, phys_block, diag224_buf);
371 if (lpar_inf.cp.lpar_weight) {
372 sctns->par.infpwbcp = sctns->mac.infmscps * 0x10000 *
373 lpar_inf.cp.lpar_weight / lpar_inf.cp.all_weight;
376 if (lpar_inf.ifl.lpar_weight) {
377 sctns->par.infpwbif = sctns->mac.infmsifl * 0x10000 *
378 lpar_inf.ifl.lpar_weight / lpar_inf.ifl.all_weight;
380 sctns->par.infpval1 |= PAR_WGHT_VLD;
383 free_page((unsigned long)diag224_buf);
387 static int sthyi(u64 vaddr)
389 register u64 code asm("0") = 0;
390 register u64 addr asm("2") = vaddr;
394 ".insn rre,0xB2560000,%[code],%[addr]\n"
398 : [code] "d" (code), [addr] "a" (addr)
399 : "3", "memory", "cc");
403 int handle_sthyi(struct kvm_vcpu *vcpu)
405 int reg1, reg2, r = 0;
406 u64 code, addr, cc = 0;
407 struct sthyi_sctns *sctns = NULL;
409 if (!test_kvm_facility(vcpu->kvm, 74))
410 return kvm_s390_inject_program_int(vcpu, PGM_OPERATION);
413 * STHYI requires extensive locking in the higher hypervisors
414 * and is very computational/memory expensive. Therefore we
415 * ratelimit the executions per VM.
417 if (!__ratelimit(&vcpu->kvm->arch.sthyi_limit)) {
418 kvm_s390_retry_instr(vcpu);
422 kvm_s390_get_regs_rre(vcpu, ®1, ®2);
423 code = vcpu->run->s.regs.gprs[reg1];
424 addr = vcpu->run->s.regs.gprs[reg2];
426 vcpu->stat.instruction_sthyi++;
427 VCPU_EVENT(vcpu, 3, "STHYI: fc: %llu addr: 0x%016llx", code, addr);
428 trace_kvm_s390_handle_sthyi(vcpu, code, addr);
430 if (reg1 == reg2 || reg1 & 1 || reg2 & 1)
431 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
438 if (addr & ~PAGE_MASK)
439 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
441 sctns = (void *)get_zeroed_page(GFP_KERNEL);
446 * If we are a guest, we don't want to emulate an emulated
447 * instruction. We ask the hypervisor to provide the data.
449 if (test_facility(74)) {
450 cc = sthyi((u64)sctns);
460 r = write_guest(vcpu, addr, reg2, sctns, PAGE_SIZE);
462 free_page((unsigned long)sctns);
463 return kvm_s390_inject_prog_cond(vcpu, r);
467 free_page((unsigned long)sctns);
468 vcpu->run->s.regs.gprs[reg2 + 1] = cc ? 4 : 0;
469 kvm_s390_set_psw_cc(vcpu, cc);