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