KVM: s390: add kvm stat counter for all diagnoses
[firefly-linux-kernel-4.4.55.git] / arch / s390 / kvm / diag.c
1 /*
2  * handling diagnose instructions
3  *
4  * Copyright IBM Corp. 2008, 2011
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License (version 2 only)
8  * as published by the Free Software Foundation.
9  *
10  *    Author(s): Carsten Otte <cotte@de.ibm.com>
11  *               Christian Borntraeger <borntraeger@de.ibm.com>
12  */
13
14 #include <linux/kvm.h>
15 #include <linux/kvm_host.h>
16 #include <asm/pgalloc.h>
17 #include <asm/virtio-ccw.h>
18 #include "kvm-s390.h"
19 #include "trace.h"
20 #include "trace-s390.h"
21 #include "gaccess.h"
22
23 static int diag_release_pages(struct kvm_vcpu *vcpu)
24 {
25         unsigned long start, end;
26         unsigned long prefix  = kvm_s390_get_prefix(vcpu);
27
28         start = vcpu->run->s.regs.gprs[(vcpu->arch.sie_block->ipa & 0xf0) >> 4];
29         end = vcpu->run->s.regs.gprs[vcpu->arch.sie_block->ipa & 0xf] + 4096;
30         vcpu->stat.diagnose_10++;
31
32         if (start & ~PAGE_MASK || end & ~PAGE_MASK || start >= end
33             || start < 2 * PAGE_SIZE)
34                 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
35
36         VCPU_EVENT(vcpu, 5, "diag release pages %lX %lX", start, end);
37
38         /*
39          * We checked for start >= end above, so lets check for the
40          * fast path (no prefix swap page involved)
41          */
42         if (end <= prefix || start >= prefix + 2 * PAGE_SIZE) {
43                 gmap_discard(vcpu->arch.gmap, start, end);
44         } else {
45                 /*
46                  * This is slow path.  gmap_discard will check for start
47                  * so lets split this into before prefix, prefix, after
48                  * prefix and let gmap_discard make some of these calls
49                  * NOPs.
50                  */
51                 gmap_discard(vcpu->arch.gmap, start, prefix);
52                 if (start <= prefix)
53                         gmap_discard(vcpu->arch.gmap, 0, 4096);
54                 if (end > prefix + 4096)
55                         gmap_discard(vcpu->arch.gmap, 4096, 8192);
56                 gmap_discard(vcpu->arch.gmap, prefix + 2 * PAGE_SIZE, end);
57         }
58         return 0;
59 }
60
61 static int __diag_page_ref_service(struct kvm_vcpu *vcpu)
62 {
63         struct prs_parm {
64                 u16 code;
65                 u16 subcode;
66                 u16 parm_len;
67                 u16 parm_version;
68                 u64 token_addr;
69                 u64 select_mask;
70                 u64 compare_mask;
71                 u64 zarch;
72         };
73         struct prs_parm parm;
74         int rc;
75         u16 rx = (vcpu->arch.sie_block->ipa & 0xf0) >> 4;
76         u16 ry = (vcpu->arch.sie_block->ipa & 0x0f);
77
78         vcpu->stat.diagnose_258++;
79         if (vcpu->run->s.regs.gprs[rx] & 7)
80                 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
81         rc = read_guest(vcpu, vcpu->run->s.regs.gprs[rx], rx, &parm, sizeof(parm));
82         if (rc)
83                 return kvm_s390_inject_prog_cond(vcpu, rc);
84         if (parm.parm_version != 2 || parm.parm_len < 5 || parm.code != 0x258)
85                 return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
86
87         switch (parm.subcode) {
88         case 0: /* TOKEN */
89                 if (vcpu->arch.pfault_token != KVM_S390_PFAULT_TOKEN_INVALID) {
90                         /*
91                          * If the pagefault handshake is already activated,
92                          * the token must not be changed.  We have to return
93                          * decimal 8 instead, as mandated in SC24-6084.
94                          */
95                         vcpu->run->s.regs.gprs[ry] = 8;
96                         return 0;
97                 }
98
99                 if ((parm.compare_mask & parm.select_mask) != parm.compare_mask ||
100                     parm.token_addr & 7 || parm.zarch != 0x8000000000000000ULL)
101                         return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
102
103                 if (kvm_is_error_gpa(vcpu->kvm, parm.token_addr))
104                         return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
105
106                 vcpu->arch.pfault_token = parm.token_addr;
107                 vcpu->arch.pfault_select = parm.select_mask;
108                 vcpu->arch.pfault_compare = parm.compare_mask;
109                 vcpu->run->s.regs.gprs[ry] = 0;
110                 rc = 0;
111                 break;
112         case 1: /*
113                  * CANCEL
114                  * Specification allows to let already pending tokens survive
115                  * the cancel, therefore to reduce code complexity, we assume
116                  * all outstanding tokens are already pending.
117                  */
118                 if (parm.token_addr || parm.select_mask ||
119                     parm.compare_mask || parm.zarch)
120                         return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
121
122                 vcpu->run->s.regs.gprs[ry] = 0;
123                 /*
124                  * If the pfault handling was not established or is already
125                  * canceled SC24-6084 requests to return decimal 4.
126                  */
127                 if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID)
128                         vcpu->run->s.regs.gprs[ry] = 4;
129                 else
130                         vcpu->arch.pfault_token = KVM_S390_PFAULT_TOKEN_INVALID;
131
132                 rc = 0;
133                 break;
134         default:
135                 rc = -EOPNOTSUPP;
136                 break;
137         }
138
139         return rc;
140 }
141
142 static int __diag_time_slice_end(struct kvm_vcpu *vcpu)
143 {
144         VCPU_EVENT(vcpu, 5, "%s", "diag time slice end");
145         vcpu->stat.diagnose_44++;
146         kvm_vcpu_on_spin(vcpu);
147         return 0;
148 }
149
150 static int __diag_time_slice_end_directed(struct kvm_vcpu *vcpu)
151 {
152         struct kvm *kvm = vcpu->kvm;
153         struct kvm_vcpu *tcpu;
154         int tid;
155         int i;
156
157         tid = vcpu->run->s.regs.gprs[(vcpu->arch.sie_block->ipa & 0xf0) >> 4];
158         vcpu->stat.diagnose_9c++;
159         VCPU_EVENT(vcpu, 5, "diag time slice end directed to %d", tid);
160
161         if (tid == vcpu->vcpu_id)
162                 return 0;
163
164         kvm_for_each_vcpu(i, tcpu, kvm)
165                 if (tcpu->vcpu_id == tid) {
166                         kvm_vcpu_yield_to(tcpu);
167                         break;
168                 }
169
170         return 0;
171 }
172
173 static int __diag_ipl_functions(struct kvm_vcpu *vcpu)
174 {
175         unsigned int reg = vcpu->arch.sie_block->ipa & 0xf;
176         unsigned long subcode = vcpu->run->s.regs.gprs[reg] & 0xffff;
177
178         VCPU_EVENT(vcpu, 5, "diag ipl functions, subcode %lx", subcode);
179         vcpu->stat.diagnose_308++;
180         switch (subcode) {
181         case 3:
182                 vcpu->run->s390_reset_flags = KVM_S390_RESET_CLEAR;
183                 break;
184         case 4:
185                 vcpu->run->s390_reset_flags = 0;
186                 break;
187         default:
188                 return -EOPNOTSUPP;
189         }
190
191         if (!kvm_s390_user_cpu_state_ctrl(vcpu->kvm))
192                 kvm_s390_vcpu_stop(vcpu);
193         vcpu->run->s390_reset_flags |= KVM_S390_RESET_SUBSYSTEM;
194         vcpu->run->s390_reset_flags |= KVM_S390_RESET_IPL;
195         vcpu->run->s390_reset_flags |= KVM_S390_RESET_CPU_INIT;
196         vcpu->run->exit_reason = KVM_EXIT_S390_RESET;
197         VCPU_EVENT(vcpu, 3, "requesting userspace resets %llx",
198           vcpu->run->s390_reset_flags);
199         trace_kvm_s390_request_resets(vcpu->run->s390_reset_flags);
200         return -EREMOTE;
201 }
202
203 static int __diag_virtio_hypercall(struct kvm_vcpu *vcpu)
204 {
205         int ret;
206
207         vcpu->stat.diagnose_500++;
208         /* No virtio-ccw notification? Get out quickly. */
209         if (!vcpu->kvm->arch.css_support ||
210             (vcpu->run->s.regs.gprs[1] != KVM_S390_VIRTIO_CCW_NOTIFY))
211                 return -EOPNOTSUPP;
212
213         /*
214          * The layout is as follows:
215          * - gpr 2 contains the subchannel id (passed as addr)
216          * - gpr 3 contains the virtqueue index (passed as datamatch)
217          * - gpr 4 contains the index on the bus (optionally)
218          */
219         ret = kvm_io_bus_write_cookie(vcpu, KVM_VIRTIO_CCW_NOTIFY_BUS,
220                                       vcpu->run->s.regs.gprs[2] & 0xffffffff,
221                                       8, &vcpu->run->s.regs.gprs[3],
222                                       vcpu->run->s.regs.gprs[4]);
223
224         /*
225          * Return cookie in gpr 2, but don't overwrite the register if the
226          * diagnose will be handled by userspace.
227          */
228         if (ret != -EOPNOTSUPP)
229                 vcpu->run->s.regs.gprs[2] = ret;
230         /* kvm_io_bus_write_cookie returns -EOPNOTSUPP if it found no match. */
231         return ret < 0 ? ret : 0;
232 }
233
234 int kvm_s390_handle_diag(struct kvm_vcpu *vcpu)
235 {
236         int code = kvm_s390_get_base_disp_rs(vcpu, NULL) & 0xffff;
237
238         if (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE)
239                 return kvm_s390_inject_program_int(vcpu, PGM_PRIVILEGED_OP);
240
241         trace_kvm_s390_handle_diag(vcpu, code);
242         switch (code) {
243         case 0x10:
244                 return diag_release_pages(vcpu);
245         case 0x44:
246                 return __diag_time_slice_end(vcpu);
247         case 0x9c:
248                 return __diag_time_slice_end_directed(vcpu);
249         case 0x258:
250                 return __diag_page_ref_service(vcpu);
251         case 0x308:
252                 return __diag_ipl_functions(vcpu);
253         case 0x500:
254                 return __diag_virtio_hypercall(vcpu);
255         default:
256                 return -EOPNOTSUPP;
257         }
258 }