|
8 | 8 | * Copyright IBM Corp. 2016 |
9 | 9 | * Author(s): Janosch Frank <frankja@linux.vnet.ibm.com> |
10 | 10 | */ |
11 | | -#include <linux/kvm_host.h> |
12 | 11 | #include <linux/errno.h> |
13 | 12 | #include <linux/pagemap.h> |
14 | 13 | #include <linux/vmalloc.h> |
15 | | -#include <linux/ratelimit.h> |
16 | | - |
17 | | -#include <asm/kvm_host.h> |
| 14 | +#include <linux/syscalls.h> |
| 15 | +#include <linux/mutex.h> |
18 | 16 | #include <asm/asm-offsets.h> |
19 | 17 | #include <asm/sclp.h> |
20 | 18 | #include <asm/diag.h> |
21 | 19 | #include <asm/sysinfo.h> |
22 | 20 | #include <asm/ebcdic.h> |
23 | | - |
24 | | -#include "kvm-s390.h" |
25 | | -#include "gaccess.h" |
26 | | -#include "trace.h" |
| 21 | +#include <asm/facility.h> |
| 22 | +#include <asm/sthyi.h> |
| 23 | +#include "entry.h" |
27 | 24 |
|
28 | 25 | #define DED_WEIGHT 0xffff |
29 | 26 | /* |
@@ -144,6 +141,21 @@ struct lpar_cpu_inf { |
144 | 141 | struct cpu_inf ifl; |
145 | 142 | }; |
146 | 143 |
|
| 144 | +/* |
| 145 | + * STHYI requires extensive locking in the higher hypervisors |
| 146 | + * and is very computational/memory expensive. Therefore we |
| 147 | + * cache the retrieved data whose valid period is 1s. |
| 148 | + */ |
| 149 | +#define CACHE_VALID_JIFFIES HZ |
| 150 | + |
| 151 | +struct sthyi_info { |
| 152 | + void *info; |
| 153 | + unsigned long end; |
| 154 | +}; |
| 155 | + |
| 156 | +static DEFINE_MUTEX(sthyi_mutex); |
| 157 | +static struct sthyi_info sthyi_cache; |
| 158 | + |
147 | 159 | static inline u64 cpu_id(u8 ctidx, void *diag224_buf) |
148 | 160 | { |
149 | 161 | return *((u64 *)(diag224_buf + (ctidx + 1) * DIAG204_CPU_NAME_LEN)); |
@@ -382,88 +394,124 @@ static void fill_diag(struct sthyi_sctns *sctns) |
382 | 394 | vfree(diag204_buf); |
383 | 395 | } |
384 | 396 |
|
385 | | -static int sthyi(u64 vaddr) |
| 397 | +static int sthyi(u64 vaddr, u64 *rc) |
386 | 398 | { |
387 | 399 | register u64 code asm("0") = 0; |
388 | 400 | register u64 addr asm("2") = vaddr; |
| 401 | + register u64 rcode asm("3"); |
389 | 402 | int cc; |
390 | 403 |
|
391 | 404 | asm volatile( |
392 | 405 | ".insn rre,0xB2560000,%[code],%[addr]\n" |
393 | 406 | "ipm %[cc]\n" |
394 | 407 | "srl %[cc],28\n" |
395 | | - : [cc] "=d" (cc) |
| 408 | + : [cc] "=d" (cc), "=d" (rcode) |
396 | 409 | : [code] "d" (code), [addr] "a" (addr) |
397 | | - : "3", "memory", "cc"); |
| 410 | + : "memory", "cc"); |
| 411 | + *rc = rcode; |
398 | 412 | return cc; |
399 | 413 | } |
400 | 414 |
|
401 | | -int handle_sthyi(struct kvm_vcpu *vcpu) |
| 415 | +static int fill_dst(void *dst, u64 *rc) |
402 | 416 | { |
403 | | - int reg1, reg2, r = 0; |
404 | | - u64 code, addr, cc = 0; |
405 | | - struct sthyi_sctns *sctns = NULL; |
406 | | - |
407 | | - if (!test_kvm_facility(vcpu->kvm, 74)) |
408 | | - return kvm_s390_inject_program_int(vcpu, PGM_OPERATION); |
| 417 | + struct sthyi_sctns *sctns = (struct sthyi_sctns *)dst; |
409 | 418 |
|
410 | 419 | /* |
411 | | - * STHYI requires extensive locking in the higher hypervisors |
412 | | - * and is very computational/memory expensive. Therefore we |
413 | | - * ratelimit the executions per VM. |
| 420 | + * If the facility is on, we don't want to emulate the instruction. |
| 421 | + * We ask the hypervisor to provide the data. |
414 | 422 | */ |
415 | | - if (!__ratelimit(&vcpu->kvm->arch.sthyi_limit)) { |
416 | | - kvm_s390_retry_instr(vcpu); |
| 423 | + if (test_facility(74)) |
| 424 | + return sthyi((u64)dst, rc); |
| 425 | + |
| 426 | + fill_hdr(sctns); |
| 427 | + fill_stsi(sctns); |
| 428 | + fill_diag(sctns); |
| 429 | + *rc = 0; |
| 430 | + return 0; |
| 431 | +} |
| 432 | + |
| 433 | +static int sthyi_init_cache(void) |
| 434 | +{ |
| 435 | + if (sthyi_cache.info) |
417 | 436 | return 0; |
418 | | - } |
| 437 | + sthyi_cache.info = (void *)get_zeroed_page(GFP_KERNEL); |
| 438 | + if (!sthyi_cache.info) |
| 439 | + return -ENOMEM; |
| 440 | + sthyi_cache.end = jiffies - 1; /* expired */ |
| 441 | + return 0; |
| 442 | +} |
419 | 443 |
|
420 | | - kvm_s390_get_regs_rre(vcpu, ®1, ®2); |
421 | | - code = vcpu->run->s.regs.gprs[reg1]; |
422 | | - addr = vcpu->run->s.regs.gprs[reg2]; |
| 444 | +static int sthyi_update_cache(u64 *rc) |
| 445 | +{ |
| 446 | + int r; |
423 | 447 |
|
424 | | - vcpu->stat.instruction_sthyi++; |
425 | | - VCPU_EVENT(vcpu, 3, "STHYI: fc: %llu addr: 0x%016llx", code, addr); |
426 | | - trace_kvm_s390_handle_sthyi(vcpu, code, addr); |
| 448 | + memset(sthyi_cache.info, 0, PAGE_SIZE); |
| 449 | + r = fill_dst(sthyi_cache.info, rc); |
| 450 | + if (r) |
| 451 | + return r; |
| 452 | + sthyi_cache.end = jiffies + CACHE_VALID_JIFFIES; |
| 453 | + return r; |
| 454 | +} |
427 | 455 |
|
428 | | - if (reg1 == reg2 || reg1 & 1 || reg2 & 1) |
429 | | - return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION); |
| 456 | +/* |
| 457 | + * sthyi_fill - Fill page with data returned by the STHYI instruction |
| 458 | + * |
| 459 | + * @dst: Pointer to zeroed page |
| 460 | + * @rc: Pointer for storing the return code of the instruction |
| 461 | + * |
| 462 | + * Fills the destination with system information returned by the STHYI |
| 463 | + * instruction. The data is generated by emulation or execution of STHYI, |
| 464 | + * if available. The return value is the condition code that would be |
| 465 | + * returned, the rc parameter is the return code which is passed in |
| 466 | + * register R2 + 1. |
| 467 | + */ |
| 468 | +int sthyi_fill(void *dst, u64 *rc) |
| 469 | +{ |
| 470 | + int r; |
430 | 471 |
|
431 | | - if (code & 0xffff) { |
432 | | - cc = 3; |
| 472 | + mutex_lock(&sthyi_mutex); |
| 473 | + r = sthyi_init_cache(); |
| 474 | + if (r) |
433 | 475 | goto out; |
434 | | - } |
435 | 476 |
|
436 | | - if (addr & ~PAGE_MASK) |
437 | | - return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION); |
| 477 | + if (time_is_before_jiffies(sthyi_cache.end)) { |
| 478 | + /* cache expired */ |
| 479 | + r = sthyi_update_cache(rc); |
| 480 | + if (r) |
| 481 | + goto out; |
| 482 | + } |
| 483 | + *rc = 0; |
| 484 | + memcpy(dst, sthyi_cache.info, PAGE_SIZE); |
| 485 | +out: |
| 486 | + mutex_unlock(&sthyi_mutex); |
| 487 | + return r; |
| 488 | +} |
| 489 | +EXPORT_SYMBOL_GPL(sthyi_fill); |
438 | 490 |
|
439 | | - sctns = (void *)get_zeroed_page(GFP_KERNEL); |
440 | | - if (!sctns) |
| 491 | +SYSCALL_DEFINE4(s390_sthyi, unsigned long, function_code, void __user *, buffer, |
| 492 | + u64 __user *, return_code, unsigned long, flags) |
| 493 | +{ |
| 494 | + u64 sthyi_rc; |
| 495 | + void *info; |
| 496 | + int r; |
| 497 | + |
| 498 | + if (flags) |
| 499 | + return -EINVAL; |
| 500 | + if (function_code != STHYI_FC_CP_IFL_CAP) |
| 501 | + return -EOPNOTSUPP; |
| 502 | + info = (void *)get_zeroed_page(GFP_KERNEL); |
| 503 | + if (!info) |
441 | 504 | return -ENOMEM; |
442 | | - |
443 | | - /* |
444 | | - * If we are a guest, we don't want to emulate an emulated |
445 | | - * instruction. We ask the hypervisor to provide the data. |
446 | | - */ |
447 | | - if (test_facility(74)) { |
448 | | - cc = sthyi((u64)sctns); |
| 505 | + r = sthyi_fill(info, &sthyi_rc); |
| 506 | + if (r < 0) |
| 507 | + goto out; |
| 508 | + if (return_code && put_user(sthyi_rc, return_code)) { |
| 509 | + r = -EFAULT; |
449 | 510 | goto out; |
450 | 511 | } |
451 | | - |
452 | | - fill_hdr(sctns); |
453 | | - fill_stsi(sctns); |
454 | | - fill_diag(sctns); |
455 | | - |
| 512 | + if (copy_to_user(buffer, info, PAGE_SIZE)) |
| 513 | + r = -EFAULT; |
456 | 514 | out: |
457 | | - if (!cc) { |
458 | | - r = write_guest(vcpu, addr, reg2, sctns, PAGE_SIZE); |
459 | | - if (r) { |
460 | | - free_page((unsigned long)sctns); |
461 | | - return kvm_s390_inject_prog_cond(vcpu, r); |
462 | | - } |
463 | | - } |
464 | | - |
465 | | - free_page((unsigned long)sctns); |
466 | | - vcpu->run->s.regs.gprs[reg2 + 1] = cc ? 4 : 0; |
467 | | - kvm_s390_set_psw_cc(vcpu, cc); |
| 515 | + free_page((unsigned long)info); |
468 | 516 | return r; |
469 | 517 | } |
0 commit comments