• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2011. Freescale Inc. All rights reserved.
3  *
4  * Authors:
5  *    Alexander Graf <agraf@suse.de>
6  *    Paul Mackerras <paulus@samba.org>
7  *
8  * Description:
9  *
10  * Hypercall handling for running PAPR guests in PR KVM on Book 3S
11  * processors.
12  *
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License, version 2, as
15  * published by the Free Software Foundation.
16  */
17 
18 #include <linux/anon_inodes.h>
19 
20 #include <asm/uaccess.h>
21 #include <asm/kvm_ppc.h>
22 #include <asm/kvm_book3s.h>
23 
24 #define HPTE_SIZE	16		/* bytes per HPT entry */
25 
get_pteg_addr(struct kvm_vcpu * vcpu,long pte_index)26 static unsigned long get_pteg_addr(struct kvm_vcpu *vcpu, long pte_index)
27 {
28 	struct kvmppc_vcpu_book3s *vcpu_book3s = to_book3s(vcpu);
29 	unsigned long pteg_addr;
30 
31 	pte_index <<= 4;
32 	pte_index &= ((1 << ((vcpu_book3s->sdr1 & 0x1f) + 11)) - 1) << 7 | 0x70;
33 	pteg_addr = vcpu_book3s->sdr1 & 0xfffffffffffc0000ULL;
34 	pteg_addr |= pte_index;
35 
36 	return pteg_addr;
37 }
38 
kvmppc_h_pr_enter(struct kvm_vcpu * vcpu)39 static int kvmppc_h_pr_enter(struct kvm_vcpu *vcpu)
40 {
41 	long flags = kvmppc_get_gpr(vcpu, 4);
42 	long pte_index = kvmppc_get_gpr(vcpu, 5);
43 	__be64 pteg[2 * 8];
44 	__be64 *hpte;
45 	unsigned long pteg_addr, i;
46 	long int ret;
47 
48 	i = pte_index & 7;
49 	pte_index &= ~7UL;
50 	pteg_addr = get_pteg_addr(vcpu, pte_index);
51 
52 	mutex_lock(&vcpu->kvm->arch.hpt_mutex);
53 	ret = H_FUNCTION;
54 	if (copy_from_user(pteg, (void __user *)pteg_addr, sizeof(pteg)))
55 		goto done;
56 	hpte = pteg;
57 
58 	ret = H_PTEG_FULL;
59 	if (likely((flags & H_EXACT) == 0)) {
60 		for (i = 0; ; ++i) {
61 			if (i == 8)
62 				goto done;
63 			if ((be64_to_cpu(*hpte) & HPTE_V_VALID) == 0)
64 				break;
65 			hpte += 2;
66 		}
67 	} else {
68 		hpte += i * 2;
69 		if (*hpte & HPTE_V_VALID)
70 			goto done;
71 	}
72 
73 	hpte[0] = cpu_to_be64(kvmppc_get_gpr(vcpu, 6));
74 	hpte[1] = cpu_to_be64(kvmppc_get_gpr(vcpu, 7));
75 	pteg_addr += i * HPTE_SIZE;
76 	ret = H_FUNCTION;
77 	if (copy_to_user((void __user *)pteg_addr, hpte, HPTE_SIZE))
78 		goto done;
79 	kvmppc_set_gpr(vcpu, 4, pte_index | i);
80 	ret = H_SUCCESS;
81 
82  done:
83 	mutex_unlock(&vcpu->kvm->arch.hpt_mutex);
84 	kvmppc_set_gpr(vcpu, 3, ret);
85 
86 	return EMULATE_DONE;
87 }
88 
kvmppc_h_pr_remove(struct kvm_vcpu * vcpu)89 static int kvmppc_h_pr_remove(struct kvm_vcpu *vcpu)
90 {
91 	unsigned long flags= kvmppc_get_gpr(vcpu, 4);
92 	unsigned long pte_index = kvmppc_get_gpr(vcpu, 5);
93 	unsigned long avpn = kvmppc_get_gpr(vcpu, 6);
94 	unsigned long v = 0, pteg, rb;
95 	unsigned long pte[2];
96 	long int ret;
97 
98 	pteg = get_pteg_addr(vcpu, pte_index);
99 	mutex_lock(&vcpu->kvm->arch.hpt_mutex);
100 	ret = H_FUNCTION;
101 	if (copy_from_user(pte, (void __user *)pteg, sizeof(pte)))
102 		goto done;
103 	pte[0] = be64_to_cpu((__force __be64)pte[0]);
104 	pte[1] = be64_to_cpu((__force __be64)pte[1]);
105 
106 	ret = H_NOT_FOUND;
107 	if ((pte[0] & HPTE_V_VALID) == 0 ||
108 	    ((flags & H_AVPN) && (pte[0] & ~0x7fUL) != avpn) ||
109 	    ((flags & H_ANDCOND) && (pte[0] & avpn) != 0))
110 		goto done;
111 
112 	ret = H_FUNCTION;
113 	if (copy_to_user((void __user *)pteg, &v, sizeof(v)))
114 		goto done;
115 
116 	rb = compute_tlbie_rb(pte[0], pte[1], pte_index);
117 	vcpu->arch.mmu.tlbie(vcpu, rb, rb & 1 ? true : false);
118 
119 	ret = H_SUCCESS;
120 	kvmppc_set_gpr(vcpu, 4, pte[0]);
121 	kvmppc_set_gpr(vcpu, 5, pte[1]);
122 
123  done:
124 	mutex_unlock(&vcpu->kvm->arch.hpt_mutex);
125 	kvmppc_set_gpr(vcpu, 3, ret);
126 
127 	return EMULATE_DONE;
128 }
129 
130 /* Request defs for kvmppc_h_pr_bulk_remove() */
131 #define H_BULK_REMOVE_TYPE             0xc000000000000000ULL
132 #define   H_BULK_REMOVE_REQUEST        0x4000000000000000ULL
133 #define   H_BULK_REMOVE_RESPONSE       0x8000000000000000ULL
134 #define   H_BULK_REMOVE_END            0xc000000000000000ULL
135 #define H_BULK_REMOVE_CODE             0x3000000000000000ULL
136 #define   H_BULK_REMOVE_SUCCESS        0x0000000000000000ULL
137 #define   H_BULK_REMOVE_NOT_FOUND      0x1000000000000000ULL
138 #define   H_BULK_REMOVE_PARM           0x2000000000000000ULL
139 #define   H_BULK_REMOVE_HW             0x3000000000000000ULL
140 #define H_BULK_REMOVE_RC               0x0c00000000000000ULL
141 #define H_BULK_REMOVE_FLAGS            0x0300000000000000ULL
142 #define   H_BULK_REMOVE_ABSOLUTE       0x0000000000000000ULL
143 #define   H_BULK_REMOVE_ANDCOND        0x0100000000000000ULL
144 #define   H_BULK_REMOVE_AVPN           0x0200000000000000ULL
145 #define H_BULK_REMOVE_PTEX             0x00ffffffffffffffULL
146 #define H_BULK_REMOVE_MAX_BATCH        4
147 
kvmppc_h_pr_bulk_remove(struct kvm_vcpu * vcpu)148 static int kvmppc_h_pr_bulk_remove(struct kvm_vcpu *vcpu)
149 {
150 	int i;
151 	int paramnr = 4;
152 	int ret = H_SUCCESS;
153 
154 	mutex_lock(&vcpu->kvm->arch.hpt_mutex);
155 	for (i = 0; i < H_BULK_REMOVE_MAX_BATCH; i++) {
156 		unsigned long tsh = kvmppc_get_gpr(vcpu, paramnr+(2*i));
157 		unsigned long tsl = kvmppc_get_gpr(vcpu, paramnr+(2*i)+1);
158 		unsigned long pteg, rb, flags;
159 		unsigned long pte[2];
160 		unsigned long v = 0;
161 
162 		if ((tsh & H_BULK_REMOVE_TYPE) == H_BULK_REMOVE_END) {
163 			break; /* Exit success */
164 		} else if ((tsh & H_BULK_REMOVE_TYPE) !=
165 			   H_BULK_REMOVE_REQUEST) {
166 			ret = H_PARAMETER;
167 			break; /* Exit fail */
168 		}
169 
170 		tsh &= H_BULK_REMOVE_PTEX | H_BULK_REMOVE_FLAGS;
171 		tsh |= H_BULK_REMOVE_RESPONSE;
172 
173 		if ((tsh & H_BULK_REMOVE_ANDCOND) &&
174 		    (tsh & H_BULK_REMOVE_AVPN)) {
175 			tsh |= H_BULK_REMOVE_PARM;
176 			kvmppc_set_gpr(vcpu, paramnr+(2*i), tsh);
177 			ret = H_PARAMETER;
178 			break; /* Exit fail */
179 		}
180 
181 		pteg = get_pteg_addr(vcpu, tsh & H_BULK_REMOVE_PTEX);
182 		if (copy_from_user(pte, (void __user *)pteg, sizeof(pte))) {
183 			ret = H_FUNCTION;
184 			break;
185 		}
186 		pte[0] = be64_to_cpu((__force __be64)pte[0]);
187 		pte[1] = be64_to_cpu((__force __be64)pte[1]);
188 
189 		/* tsl = AVPN */
190 		flags = (tsh & H_BULK_REMOVE_FLAGS) >> 26;
191 
192 		if ((pte[0] & HPTE_V_VALID) == 0 ||
193 		    ((flags & H_AVPN) && (pte[0] & ~0x7fUL) != tsl) ||
194 		    ((flags & H_ANDCOND) && (pte[0] & tsl) != 0)) {
195 			tsh |= H_BULK_REMOVE_NOT_FOUND;
196 		} else {
197 			/* Splat the pteg in (userland) hpt */
198 			if (copy_to_user((void __user *)pteg, &v, sizeof(v))) {
199 				ret = H_FUNCTION;
200 				break;
201 			}
202 
203 			rb = compute_tlbie_rb(pte[0], pte[1],
204 					      tsh & H_BULK_REMOVE_PTEX);
205 			vcpu->arch.mmu.tlbie(vcpu, rb, rb & 1 ? true : false);
206 			tsh |= H_BULK_REMOVE_SUCCESS;
207 			tsh |= (pte[1] & (HPTE_R_C | HPTE_R_R)) << 43;
208 		}
209 		kvmppc_set_gpr(vcpu, paramnr+(2*i), tsh);
210 	}
211 	mutex_unlock(&vcpu->kvm->arch.hpt_mutex);
212 	kvmppc_set_gpr(vcpu, 3, ret);
213 
214 	return EMULATE_DONE;
215 }
216 
kvmppc_h_pr_protect(struct kvm_vcpu * vcpu)217 static int kvmppc_h_pr_protect(struct kvm_vcpu *vcpu)
218 {
219 	unsigned long flags = kvmppc_get_gpr(vcpu, 4);
220 	unsigned long pte_index = kvmppc_get_gpr(vcpu, 5);
221 	unsigned long avpn = kvmppc_get_gpr(vcpu, 6);
222 	unsigned long rb, pteg, r, v;
223 	unsigned long pte[2];
224 	long int ret;
225 
226 	pteg = get_pteg_addr(vcpu, pte_index);
227 	mutex_lock(&vcpu->kvm->arch.hpt_mutex);
228 	ret = H_FUNCTION;
229 	if (copy_from_user(pte, (void __user *)pteg, sizeof(pte)))
230 		goto done;
231 	pte[0] = be64_to_cpu((__force __be64)pte[0]);
232 	pte[1] = be64_to_cpu((__force __be64)pte[1]);
233 
234 	ret = H_NOT_FOUND;
235 	if ((pte[0] & HPTE_V_VALID) == 0 ||
236 	    ((flags & H_AVPN) && (pte[0] & ~0x7fUL) != avpn))
237 		goto done;
238 
239 	v = pte[0];
240 	r = pte[1];
241 	r &= ~(HPTE_R_PP0 | HPTE_R_PP | HPTE_R_N | HPTE_R_KEY_HI |
242 	       HPTE_R_KEY_LO);
243 	r |= (flags << 55) & HPTE_R_PP0;
244 	r |= (flags << 48) & HPTE_R_KEY_HI;
245 	r |= flags & (HPTE_R_PP | HPTE_R_N | HPTE_R_KEY_LO);
246 
247 	pte[1] = r;
248 
249 	rb = compute_tlbie_rb(v, r, pte_index);
250 	vcpu->arch.mmu.tlbie(vcpu, rb, rb & 1 ? true : false);
251 	pte[0] = (__force u64)cpu_to_be64(pte[0]);
252 	pte[1] = (__force u64)cpu_to_be64(pte[1]);
253 	ret = H_FUNCTION;
254 	if (copy_to_user((void __user *)pteg, pte, sizeof(pte)))
255 		goto done;
256 	ret = H_SUCCESS;
257 
258  done:
259 	mutex_unlock(&vcpu->kvm->arch.hpt_mutex);
260 	kvmppc_set_gpr(vcpu, 3, ret);
261 
262 	return EMULATE_DONE;
263 }
264 
kvmppc_h_pr_put_tce(struct kvm_vcpu * vcpu)265 static int kvmppc_h_pr_put_tce(struct kvm_vcpu *vcpu)
266 {
267 	unsigned long liobn = kvmppc_get_gpr(vcpu, 4);
268 	unsigned long ioba = kvmppc_get_gpr(vcpu, 5);
269 	unsigned long tce = kvmppc_get_gpr(vcpu, 6);
270 	long rc;
271 
272 	rc = kvmppc_h_put_tce(vcpu, liobn, ioba, tce);
273 	if (rc == H_TOO_HARD)
274 		return EMULATE_FAIL;
275 	kvmppc_set_gpr(vcpu, 3, rc);
276 	return EMULATE_DONE;
277 }
278 
kvmppc_h_pr_logical_ci_load(struct kvm_vcpu * vcpu)279 static int kvmppc_h_pr_logical_ci_load(struct kvm_vcpu *vcpu)
280 {
281 	long rc;
282 
283 	rc = kvmppc_h_logical_ci_load(vcpu);
284 	if (rc == H_TOO_HARD)
285 		return EMULATE_FAIL;
286 	kvmppc_set_gpr(vcpu, 3, rc);
287 	return EMULATE_DONE;
288 }
289 
kvmppc_h_pr_logical_ci_store(struct kvm_vcpu * vcpu)290 static int kvmppc_h_pr_logical_ci_store(struct kvm_vcpu *vcpu)
291 {
292 	long rc;
293 
294 	rc = kvmppc_h_logical_ci_store(vcpu);
295 	if (rc == H_TOO_HARD)
296 		return EMULATE_FAIL;
297 	kvmppc_set_gpr(vcpu, 3, rc);
298 	return EMULATE_DONE;
299 }
300 
kvmppc_h_pr_xics_hcall(struct kvm_vcpu * vcpu,u32 cmd)301 static int kvmppc_h_pr_xics_hcall(struct kvm_vcpu *vcpu, u32 cmd)
302 {
303 	long rc = kvmppc_xics_hcall(vcpu, cmd);
304 	kvmppc_set_gpr(vcpu, 3, rc);
305 	return EMULATE_DONE;
306 }
307 
kvmppc_h_pr(struct kvm_vcpu * vcpu,unsigned long cmd)308 int kvmppc_h_pr(struct kvm_vcpu *vcpu, unsigned long cmd)
309 {
310 	int rc, idx;
311 
312 	if (cmd <= MAX_HCALL_OPCODE &&
313 	    !test_bit(cmd/4, vcpu->kvm->arch.enabled_hcalls))
314 		return EMULATE_FAIL;
315 
316 	switch (cmd) {
317 	case H_ENTER:
318 		return kvmppc_h_pr_enter(vcpu);
319 	case H_REMOVE:
320 		return kvmppc_h_pr_remove(vcpu);
321 	case H_PROTECT:
322 		return kvmppc_h_pr_protect(vcpu);
323 	case H_BULK_REMOVE:
324 		return kvmppc_h_pr_bulk_remove(vcpu);
325 	case H_PUT_TCE:
326 		return kvmppc_h_pr_put_tce(vcpu);
327 	case H_CEDE:
328 		kvmppc_set_msr_fast(vcpu, kvmppc_get_msr(vcpu) | MSR_EE);
329 		kvm_vcpu_block(vcpu);
330 		clear_bit(KVM_REQ_UNHALT, &vcpu->requests);
331 		vcpu->stat.halt_wakeup++;
332 		return EMULATE_DONE;
333 	case H_LOGICAL_CI_LOAD:
334 		return kvmppc_h_pr_logical_ci_load(vcpu);
335 	case H_LOGICAL_CI_STORE:
336 		return kvmppc_h_pr_logical_ci_store(vcpu);
337 	case H_XIRR:
338 	case H_CPPR:
339 	case H_EOI:
340 	case H_IPI:
341 	case H_IPOLL:
342 	case H_XIRR_X:
343 		if (kvmppc_xics_enabled(vcpu))
344 			return kvmppc_h_pr_xics_hcall(vcpu, cmd);
345 		break;
346 	case H_RTAS:
347 		if (list_empty(&vcpu->kvm->arch.rtas_tokens))
348 			break;
349 		idx = srcu_read_lock(&vcpu->kvm->srcu);
350 		rc = kvmppc_rtas_hcall(vcpu);
351 		srcu_read_unlock(&vcpu->kvm->srcu, idx);
352 		if (rc)
353 			break;
354 		kvmppc_set_gpr(vcpu, 3, 0);
355 		return EMULATE_DONE;
356 	}
357 
358 	return EMULATE_FAIL;
359 }
360 
kvmppc_hcall_impl_pr(unsigned long cmd)361 int kvmppc_hcall_impl_pr(unsigned long cmd)
362 {
363 	switch (cmd) {
364 	case H_ENTER:
365 	case H_REMOVE:
366 	case H_PROTECT:
367 	case H_BULK_REMOVE:
368 	case H_PUT_TCE:
369 	case H_CEDE:
370 	case H_LOGICAL_CI_LOAD:
371 	case H_LOGICAL_CI_STORE:
372 #ifdef CONFIG_KVM_XICS
373 	case H_XIRR:
374 	case H_CPPR:
375 	case H_EOI:
376 	case H_IPI:
377 	case H_IPOLL:
378 	case H_XIRR_X:
379 #endif
380 		return 1;
381 	}
382 	return 0;
383 }
384 
385 /*
386  * List of hcall numbers to enable by default.
387  * For compatibility with old userspace, we enable by default
388  * all hcalls that were implemented before the hcall-enabling
389  * facility was added.  Note this list should not include H_RTAS.
390  */
391 static unsigned int default_hcall_list[] = {
392 	H_ENTER,
393 	H_REMOVE,
394 	H_PROTECT,
395 	H_BULK_REMOVE,
396 	H_PUT_TCE,
397 	H_CEDE,
398 #ifdef CONFIG_KVM_XICS
399 	H_XIRR,
400 	H_CPPR,
401 	H_EOI,
402 	H_IPI,
403 	H_IPOLL,
404 	H_XIRR_X,
405 #endif
406 	0
407 };
408 
kvmppc_pr_init_default_hcalls(struct kvm * kvm)409 void kvmppc_pr_init_default_hcalls(struct kvm *kvm)
410 {
411 	int i;
412 	unsigned int hcall;
413 
414 	for (i = 0; default_hcall_list[i]; ++i) {
415 		hcall = default_hcall_list[i];
416 		WARN_ON(!kvmppc_hcall_impl_pr(hcall));
417 		__set_bit(hcall / 4, kvm->arch.enabled_hcalls);
418 	}
419 }
420