blob: 029fd5e707b445cf05466748006b30d3db948174 [file] [log] [blame]
Janosch Frank95ca2cb2016-05-23 15:11:58 +02001/*
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>
Janosch Frank7d0a5e62016-05-10 15:03:42 +020015#include <linux/ratelimit.h>
Janosch Frank95ca2cb2016-05-23 15:11:58 +020016
17#include <asm/kvm_host.h>
18#include <asm/asm-offsets.h>
19#include <asm/sclp.h>
20#include <asm/diag.h>
21#include <asm/sysinfo.h>
22#include <asm/ebcdic.h>
23
24#include "kvm-s390.h"
25#include "gaccess.h"
26#include "trace.h"
27
28#define DED_WEIGHT 0xffff
29/*
30 * CP and IFL as EBCDIC strings, SP/0x40 determines the end of string
31 * as they are justified with spaces.
32 */
33#define CP 0xc3d7404040404040UL
34#define IFL 0xc9c6d34040404040UL
35
36enum hdr_flags {
37 HDR_NOT_LPAR = 0x10,
38 HDR_STACK_INCM = 0x20,
39 HDR_STSI_UNAV = 0x40,
40 HDR_PERF_UNAV = 0x80,
41};
42
43enum mac_validity {
44 MAC_NAME_VLD = 0x20,
45 MAC_ID_VLD = 0x40,
46 MAC_CNT_VLD = 0x80,
47};
48
49enum par_flag {
50 PAR_MT_EN = 0x80,
51};
52
53enum par_validity {
54 PAR_GRP_VLD = 0x08,
55 PAR_ID_VLD = 0x10,
56 PAR_ABS_VLD = 0x20,
57 PAR_WGHT_VLD = 0x40,
58 PAR_PCNT_VLD = 0x80,
59};
60
61struct hdr_sctn {
62 u8 infhflg1;
63 u8 infhflg2; /* reserved */
64 u8 infhval1; /* reserved */
65 u8 infhval2; /* reserved */
66 u8 reserved[3];
67 u8 infhygct;
68 u16 infhtotl;
69 u16 infhdln;
70 u16 infmoff;
71 u16 infmlen;
72 u16 infpoff;
73 u16 infplen;
74 u16 infhoff1;
75 u16 infhlen1;
76 u16 infgoff1;
77 u16 infglen1;
78 u16 infhoff2;
79 u16 infhlen2;
80 u16 infgoff2;
81 u16 infglen2;
82 u16 infhoff3;
83 u16 infhlen3;
84 u16 infgoff3;
85 u16 infglen3;
86 u8 reserved2[4];
87} __packed;
88
89struct mac_sctn {
90 u8 infmflg1; /* reserved */
91 u8 infmflg2; /* reserved */
92 u8 infmval1;
93 u8 infmval2; /* reserved */
94 u16 infmscps;
95 u16 infmdcps;
96 u16 infmsifl;
97 u16 infmdifl;
98 char infmname[8];
99 char infmtype[4];
100 char infmmanu[16];
101 char infmseq[16];
102 char infmpman[4];
103 u8 reserved[4];
104} __packed;
105
106struct par_sctn {
107 u8 infpflg1;
108 u8 infpflg2; /* reserved */
109 u8 infpval1;
110 u8 infpval2; /* reserved */
111 u16 infppnum;
112 u16 infpscps;
113 u16 infpdcps;
114 u16 infpsifl;
115 u16 infpdifl;
116 u16 reserved;
117 char infppnam[8];
118 u32 infpwbcp;
119 u32 infpabcp;
120 u32 infpwbif;
121 u32 infpabif;
122 char infplgnm[8];
123 u32 infplgcp;
124 u32 infplgif;
125} __packed;
126
127struct sthyi_sctns {
128 struct hdr_sctn hdr;
129 struct mac_sctn mac;
130 struct par_sctn par;
131} __packed;
132
133struct cpu_inf {
134 u64 lpar_cap;
135 u64 lpar_grp_cap;
136 u64 lpar_weight;
137 u64 all_weight;
138 int cpu_num_ded;
139 int cpu_num_shd;
140};
141
142struct lpar_cpu_inf {
143 struct cpu_inf cp;
144 struct cpu_inf ifl;
145};
146
147static inline u64 cpu_id(u8 ctidx, void *diag224_buf)
148{
149 return *((u64 *)(diag224_buf + (ctidx + 1) * DIAG204_CPU_NAME_LEN));
150}
151
152/*
153 * Scales the cpu capping from the lpar range to the one expected in
154 * sthyi data.
155 *
156 * diag204 reports a cap in hundredths of processor units.
157 * z/VM's range for one core is 0 - 0x10000.
158 */
159static u32 scale_cap(u32 in)
160{
161 return (0x10000 * in) / 100;
162}
163
164static void fill_hdr(struct sthyi_sctns *sctns)
165{
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;
172}
173
174static void fill_stsi_mac(struct sthyi_sctns *sctns,
175 struct sysinfo_1_1_1 *sysinfo)
176{
Janosch Frankfccb6992018-10-02 10:57:52 +0200177 sclp_ocf_cpc_name_copy(sctns->mac.infmname);
178 if (*(u64 *)sctns->mac.infmname != 0)
179 sctns->mac.infmval1 |= MAC_NAME_VLD;
180
Janosch Frank95ca2cb2016-05-23 15:11:58 +0200181 if (stsi(sysinfo, 1, 1, 1))
182 return;
183
Janosch Frank95ca2cb2016-05-23 15:11:58 +0200184 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));
188
Janosch Frankfccb6992018-10-02 10:57:52 +0200189 sctns->mac.infmval1 |= MAC_ID_VLD;
Janosch Frank95ca2cb2016-05-23 15:11:58 +0200190}
191
192static void fill_stsi_par(struct sthyi_sctns *sctns,
193 struct sysinfo_2_2_2 *sysinfo)
194{
195 if (stsi(sysinfo, 2, 2, 2))
196 return;
197
198 sctns->par.infppnum = sysinfo->lpar_number;
199 memcpy(sctns->par.infppnam, sysinfo->name, sizeof(sctns->par.infppnam));
200
201 sctns->par.infpval1 |= PAR_ID_VLD;
202}
203
204static void fill_stsi(struct sthyi_sctns *sctns)
205{
206 void *sysinfo;
207
208 /* Errors are handled through the validity bits in the response. */
209 sysinfo = (void *)__get_free_page(GFP_KERNEL);
210 if (!sysinfo)
211 return;
212
213 fill_stsi_mac(sctns, sysinfo);
214 fill_stsi_par(sctns, sysinfo);
215
216 free_pages((unsigned long)sysinfo, 0);
217}
218
219static void fill_diag_mac(struct sthyi_sctns *sctns,
220 struct diag204_x_phys_block *block,
221 void *diag224_buf)
222{
223 int i;
224
225 for (i = 0; i < block->hdr.cpus; i++) {
226 switch (cpu_id(block->cpus[i].ctidx, diag224_buf)) {
227 case CP:
228 if (block->cpus[i].weight == DED_WEIGHT)
229 sctns->mac.infmdcps++;
230 else
231 sctns->mac.infmscps++;
232 break;
233 case IFL:
234 if (block->cpus[i].weight == DED_WEIGHT)
235 sctns->mac.infmdifl++;
236 else
237 sctns->mac.infmsifl++;
238 break;
239 }
240 }
241 sctns->mac.infmval1 |= MAC_CNT_VLD;
242}
243
244/* Returns a pointer to the the next partition block. */
245static struct diag204_x_part_block *lpar_cpu_inf(struct lpar_cpu_inf *part_inf,
246 bool this_lpar,
247 void *diag224_buf,
248 struct diag204_x_part_block *block)
249{
250 int i, capped = 0, weight_cp = 0, weight_ifl = 0;
251 struct cpu_inf *cpu_inf;
252
253 for (i = 0; i < block->hdr.rcpus; i++) {
254 if (!(block->cpus[i].cflag & DIAG204_CPU_ONLINE))
255 continue;
256
257 switch (cpu_id(block->cpus[i].ctidx, diag224_buf)) {
258 case CP:
259 cpu_inf = &part_inf->cp;
260 if (block->cpus[i].cur_weight < DED_WEIGHT)
261 weight_cp |= block->cpus[i].cur_weight;
262 break;
263 case IFL:
264 cpu_inf = &part_inf->ifl;
265 if (block->cpus[i].cur_weight < DED_WEIGHT)
266 weight_ifl |= block->cpus[i].cur_weight;
267 break;
268 default:
269 continue;
270 }
271
272 if (!this_lpar)
273 continue;
274
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;
278
279 if (block->cpus[i].weight == DED_WEIGHT)
280 cpu_inf->cpu_num_ded += 1;
281 else
282 cpu_inf->cpu_num_shd += 1;
283 }
284
285 if (this_lpar && capped) {
286 part_inf->cp.lpar_weight = weight_cp;
287 part_inf->ifl.lpar_weight = weight_ifl;
288 }
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];
292}
293
294static void fill_diag(struct sthyi_sctns *sctns)
295{
296 int i, r, pages;
297 bool this_lpar;
298 void *diag204_buf;
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 = {};
304
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);
308 if (pages <= 0)
309 return;
310
311 diag204_buf = vmalloc(PAGE_SIZE * pages);
312 if (!diag204_buf)
313 return;
314
315 r = diag204((unsigned long)DIAG204_SUBC_STIB7 |
316 (unsigned long)DIAG204_INFO_EXT, pages, diag204_buf);
317 if (r < 0)
318 goto out;
319
Janosch Frank45c7ee42016-10-26 08:48:11 +0200320 diag224_buf = (void *)__get_free_page(GFP_KERNEL | GFP_DMA);
Janosch Frank95ca2cb2016-05-23 15:11:58 +0200321 if (!diag224_buf || diag224(diag224_buf))
322 goto out;
323
324 ti_hdr = diag204_buf;
325 part_block = diag204_buf + sizeof(*ti_hdr);
326
327 for (i = 0; i < ti_hdr->npar; i++) {
328 /*
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.
333 */
334 this_lpar = (void *)part_block - diag204_buf == ti_hdr->this_part;
335 part_block = lpar_cpu_inf(&lpar_inf, this_lpar, diag224_buf,
336 part_block);
337 }
338
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;
343
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));
349
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;
355
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;
359
360 /*
361 * Everything below needs global performance data to be
362 * meaningful.
363 */
364 if (!(ti_hdr->flags & DIAG204_LPAR_PHYS_FLG)) {
365 sctns->hdr.infhflg1 |= HDR_PERF_UNAV;
366 goto out;
367 }
368
369 fill_diag_mac(sctns, phys_block, diag224_buf);
370
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;
374 }
375
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;
379 }
380 sctns->par.infpval1 |= PAR_WGHT_VLD;
381
382out:
Janosch Frank45c7ee42016-10-26 08:48:11 +0200383 free_page((unsigned long)diag224_buf);
Janosch Frank95ca2cb2016-05-23 15:11:58 +0200384 vfree(diag204_buf);
385}
386
387static int sthyi(u64 vaddr)
388{
389 register u64 code asm("0") = 0;
390 register u64 addr asm("2") = vaddr;
391 int cc;
392
393 asm volatile(
394 ".insn rre,0xB2560000,%[code],%[addr]\n"
395 "ipm %[cc]\n"
396 "srl %[cc],28\n"
397 : [cc] "=d" (cc)
398 : [code] "d" (code), [addr] "a" (addr)
Heiko Carstensa7453332017-08-03 13:05:11 +0200399 : "3", "memory", "cc");
Janosch Frank95ca2cb2016-05-23 15:11:58 +0200400 return cc;
401}
402
403int handle_sthyi(struct kvm_vcpu *vcpu)
404{
405 int reg1, reg2, r = 0;
406 u64 code, addr, cc = 0;
407 struct sthyi_sctns *sctns = NULL;
408
Janosch Frank7d0a5e62016-05-10 15:03:42 +0200409 /*
410 * STHYI requires extensive locking in the higher hypervisors
411 * and is very computational/memory expensive. Therefore we
412 * ratelimit the executions per VM.
413 */
414 if (!__ratelimit(&vcpu->kvm->arch.sthyi_limit)) {
415 kvm_s390_retry_instr(vcpu);
416 return 0;
417 }
418
Janosch Frank95ca2cb2016-05-23 15:11:58 +0200419 kvm_s390_get_regs_rre(vcpu, &reg1, &reg2);
420 code = vcpu->run->s.regs.gprs[reg1];
421 addr = vcpu->run->s.regs.gprs[reg2];
422
423 vcpu->stat.instruction_sthyi++;
424 VCPU_EVENT(vcpu, 3, "STHYI: fc: %llu addr: 0x%016llx", code, addr);
425 trace_kvm_s390_handle_sthyi(vcpu, code, addr);
426
Heiko Carstenscfb917a2017-08-03 14:27:30 +0200427 if (reg1 == reg2 || reg1 & 1 || reg2 & 1)
Janosch Frank95ca2cb2016-05-23 15:11:58 +0200428 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
429
430 if (code & 0xffff) {
431 cc = 3;
432 goto out;
433 }
434
Heiko Carstenscfb917a2017-08-03 14:27:30 +0200435 if (addr & ~PAGE_MASK)
436 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
437
Janosch Frank95ca2cb2016-05-23 15:11:58 +0200438 /*
439 * If the page has not yet been faulted in, we want to do that
440 * now and not after all the expensive calculations.
441 */
442 r = write_guest(vcpu, addr, reg2, &cc, 1);
443 if (r)
444 return kvm_s390_inject_prog_cond(vcpu, r);
445
446 sctns = (void *)get_zeroed_page(GFP_KERNEL);
447 if (!sctns)
448 return -ENOMEM;
449
450 /*
451 * If we are a guest, we don't want to emulate an emulated
452 * instruction. We ask the hypervisor to provide the data.
453 */
454 if (test_facility(74)) {
455 cc = sthyi((u64)sctns);
456 goto out;
457 }
458
459 fill_hdr(sctns);
460 fill_stsi(sctns);
461 fill_diag(sctns);
462
463out:
464 if (!cc) {
465 r = write_guest(vcpu, addr, reg2, sctns, PAGE_SIZE);
466 if (r) {
467 free_page((unsigned long)sctns);
468 return kvm_s390_inject_prog_cond(vcpu, r);
469 }
470 }
471
472 free_page((unsigned long)sctns);
473 vcpu->run->s.regs.gprs[reg2 + 1] = cc ? 4 : 0;
474 kvm_s390_set_psw_cc(vcpu, cc);
475 return r;
476}