Skip to content

Commit a6014f1

Browse files
committed
Merge tag 'kvm-s390-next-4.15-1' of git://git.kernel.org/pub/scm/linux/kernel/git/kvms390/linux
KVM: s390: fixes and improvements for 4.15 - Some initial preparation patches for exitless interrupts and crypto - New capability for AIS migration - Fixes - merge of the sthyi tree from the base s390 team, which moves the sthyi out of KVM into a shared function also for non-KVM
2 parents 61d7507 + da9a144 commit a6014f1

20 files changed

Lines changed: 262 additions & 113 deletions

File tree

‎Documentation/virtual/kvm/api.txt‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4351,3 +4351,12 @@ This capability indicates that userspace can load HV_X64_MSR_VP_INDEX msr. Its
43514351
value is used to denote the target vcpu for a SynIC interrupt. For
43524352
compatibilty, KVM initializes this msr to KVM's internal vcpu index. When this
43534353
capability is absent, userspace can still query this msr's value.
4354+
4355+
8.13 KVM_CAP_S390_AIS_MIGRATION
4356+
4357+
Architectures: s390
4358+
Parameters: none
4359+
4360+
This capability indicates if the flic device will be able to get/set the
4361+
AIS states for migration via the KVM_DEV_FLIC_AISM_ALL attribute and allows
4362+
to discover this without having to create a flic device.

‎Documentation/virtual/kvm/devices/s390_flic.txt‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,13 @@ struct kvm_s390_ais_all {
151151
to an ISC (MSB0 bit 0 to ISC 0 and so on). The combination of simm bit and
152152
nimm bit presents AIS mode for a ISC.
153153

154+
KVM_DEV_FLIC_AISM_ALL is indicated by KVM_CAP_S390_AIS_MIGRATION.
155+
154156
Note: The KVM_SET_DEVICE_ATTR/KVM_GET_DEVICE_ATTR device ioctls executed on
155157
FLIC with an unknown group or attribute gives the error code EINVAL (instead of
156158
ENXIO, as specified in the API documentation). It is not possible to conclude
157159
that a FLIC operation is unavailable based on the error code resulting from a
158160
usage attempt.
161+
162+
Note: The KVM_DEV_FLIC_CLEAR_IO_IRQ ioctl will return EINVAL in case a zero
163+
schid is specified.

‎arch/s390/include/asm/kvm_host.h‎

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -685,11 +685,28 @@ struct kvm_s390_crypto {
685685
__u8 dea_kw;
686686
};
687687

688+
#define APCB0_MASK_SIZE 1
689+
struct kvm_s390_apcb0 {
690+
__u64 apm[APCB0_MASK_SIZE]; /* 0x0000 */
691+
__u64 aqm[APCB0_MASK_SIZE]; /* 0x0008 */
692+
__u64 adm[APCB0_MASK_SIZE]; /* 0x0010 */
693+
__u64 reserved18; /* 0x0018 */
694+
};
695+
696+
#define APCB1_MASK_SIZE 4
697+
struct kvm_s390_apcb1 {
698+
__u64 apm[APCB1_MASK_SIZE]; /* 0x0000 */
699+
__u64 aqm[APCB1_MASK_SIZE]; /* 0x0020 */
700+
__u64 adm[APCB1_MASK_SIZE]; /* 0x0040 */
701+
__u64 reserved60[4]; /* 0x0060 */
702+
};
703+
688704
struct kvm_s390_crypto_cb {
689-
__u8 reserved00[72]; /* 0x0000 */
690-
__u8 dea_wrapping_key_mask[24]; /* 0x0048 */
691-
__u8 aes_wrapping_key_mask[32]; /* 0x0060 */
692-
__u8 reserved80[128]; /* 0x0080 */
705+
struct kvm_s390_apcb0 apcb0; /* 0x0000 */
706+
__u8 reserved20[0x0048 - 0x0020]; /* 0x0020 */
707+
__u8 dea_wrapping_key_mask[24]; /* 0x0048 */
708+
__u8 aes_wrapping_key_mask[32]; /* 0x0060 */
709+
struct kvm_s390_apcb1 apcb1; /* 0x0080 */
693710
};
694711

695712
/*
@@ -736,7 +753,6 @@ struct kvm_arch{
736753
wait_queue_head_t ipte_wq;
737754
int ipte_lock_count;
738755
struct mutex ipte_mutex;
739-
struct ratelimit_state sthyi_limit;
740756
spinlock_t start_stop_lock;
741757
struct sie_page2 *sie_page2;
742758
struct kvm_s390_cpu_model model;

‎arch/s390/include/asm/sysinfo.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,4 +198,5 @@ struct service_level {
198198
int register_service_level(struct service_level *);
199199
int unregister_service_level(struct service_level *);
200200

201+
int sthyi_fill(void *dst, u64 *rc);
201202
#endif /* __ASM_S390_SYSINFO_H */

‎arch/s390/include/uapi/asm/sthyi.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#ifndef _UAPI_ASM_STHYI_H
2+
#define _UAPI_ASM_STHYI_H
3+
4+
#define STHYI_FC_CP_IFL_CAP 0
5+
6+
#endif /* _UAPI_ASM_STHYI_H */

‎arch/s390/include/uapi/asm/unistd.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,8 @@
315315
#define __NR_pwritev2 377
316316
#define __NR_s390_guarded_storage 378
317317
#define __NR_statx 379
318-
#define NR_syscalls 380
318+
#define __NR_s390_sthyi 380
319+
#define NR_syscalls 381
319320

320321
/*
321322
* There are some system calls that are not present on 64 bit, some

‎arch/s390/kernel/Makefile‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ obj-y := traps.o time.o process.o base.o early.o setup.o idle.o vtime.o
5555
obj-y += processor.o sys_s390.o ptrace.o signal.o cpcmd.o ebcdic.o nmi.o
5656
obj-y += debug.o irq.o ipl.o dis.o diag.o vdso.o als.o
5757
obj-y += sysinfo.o jump_label.o lgr.o os_info.o machine_kexec.o pgm_check.o
58-
obj-y += runtime_instr.o cache.o fpu.o dumpstack.o guarded_storage.o
58+
obj-y += runtime_instr.o cache.o fpu.o dumpstack.o guarded_storage.o sthyi.o
5959
obj-y += entry.o reipl.o relocate_kernel.o kdebugfs.o
6060

6161
extra-y += head.o head64.o vmlinux.lds

‎arch/s390/kernel/compat_wrapper.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,3 +180,4 @@ COMPAT_SYSCALL_WRAP3(mlock2, unsigned long, start, size_t, len, int, flags);
180180
COMPAT_SYSCALL_WRAP6(copy_file_range, int, fd_in, loff_t __user *, off_in, int, fd_out, loff_t __user *, off_out, size_t, len, unsigned int, flags);
181181
COMPAT_SYSCALL_WRAP2(s390_guarded_storage, int, command, struct gs_cb *, gs_cb);
182182
COMPAT_SYSCALL_WRAP5(statx, int, dfd, const char __user *, path, unsigned, flags, unsigned, mask, struct statx __user *, buffer);
183+
COMPAT_SYSCALL_WRAP4(s390_sthyi, unsigned long, code, void __user *, info, u64 __user *, rc, unsigned long, flags);

‎arch/s390/kernel/entry.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ long sys_s390_runtime_instr(int command, int signum);
7777
long sys_s390_guarded_storage(int command, struct gs_cb __user *);
7878
long sys_s390_pci_mmio_write(unsigned long, const void __user *, size_t);
7979
long sys_s390_pci_mmio_read(unsigned long, void __user *, size_t);
80+
long sys_s390_sthyi(unsigned long function_code, void __user *buffer, u64 __user *return_code, unsigned long flags);
8081

8182
DECLARE_PER_CPU(u64, mt_cycles[8]);
8283

Lines changed: 110 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,19 @@
88
* Copyright IBM Corp. 2016
99
* Author(s): Janosch Frank <frankja@linux.vnet.ibm.com>
1010
*/
11-
#include <linux/kvm_host.h>
1211
#include <linux/errno.h>
1312
#include <linux/pagemap.h>
1413
#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>
1816
#include <asm/asm-offsets.h>
1917
#include <asm/sclp.h>
2018
#include <asm/diag.h>
2119
#include <asm/sysinfo.h>
2220
#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"
2724

2825
#define DED_WEIGHT 0xffff
2926
/*
@@ -144,6 +141,21 @@ struct lpar_cpu_inf {
144141
struct cpu_inf ifl;
145142
};
146143

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+
147159
static inline u64 cpu_id(u8 ctidx, void *diag224_buf)
148160
{
149161
return *((u64 *)(diag224_buf + (ctidx + 1) * DIAG204_CPU_NAME_LEN));
@@ -382,88 +394,124 @@ static void fill_diag(struct sthyi_sctns *sctns)
382394
vfree(diag204_buf);
383395
}
384396

385-
static int sthyi(u64 vaddr)
397+
static int sthyi(u64 vaddr, u64 *rc)
386398
{
387399
register u64 code asm("0") = 0;
388400
register u64 addr asm("2") = vaddr;
401+
register u64 rcode asm("3");
389402
int cc;
390403

391404
asm volatile(
392405
".insn rre,0xB2560000,%[code],%[addr]\n"
393406
"ipm %[cc]\n"
394407
"srl %[cc],28\n"
395-
: [cc] "=d" (cc)
408+
: [cc] "=d" (cc), "=d" (rcode)
396409
: [code] "d" (code), [addr] "a" (addr)
397-
: "3", "memory", "cc");
410+
: "memory", "cc");
411+
*rc = rcode;
398412
return cc;
399413
}
400414

401-
int handle_sthyi(struct kvm_vcpu *vcpu)
415+
static int fill_dst(void *dst, u64 *rc)
402416
{
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;
409418

410419
/*
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.
414422
*/
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)
417436
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+
}
419443

420-
kvm_s390_get_regs_rre(vcpu, &reg1, &reg2);
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;
423447

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+
}
427455

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;
430471

431-
if (code & 0xffff) {
432-
cc = 3;
472+
mutex_lock(&sthyi_mutex);
473+
r = sthyi_init_cache();
474+
if (r)
433475
goto out;
434-
}
435476

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);
438490

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)
441504
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;
449510
goto out;
450511
}
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;
456514
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);
468516
return r;
469517
}

0 commit comments

Comments
 (0)