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