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 
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 
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 	unsigned long pteg[2 * 8];
44 	unsigned long pteg_addr, i, *hpte;
45 	long int ret;
46 
47 	i = pte_index & 7;
48 	pte_index &= ~7UL;
49 	pteg_addr = get_pteg_addr(vcpu, pte_index);
50 
51 	mutex_lock(&vcpu->kvm->arch.hpt_mutex);
52 	copy_from_user(pteg, (void __user *)pteg_addr, sizeof(pteg));
53 	hpte = pteg;
54 
55 	ret = H_PTEG_FULL;
56 	if (likely((flags & H_EXACT) == 0)) {
57 		for (i = 0; ; ++i) {
58 			if (i == 8)
59 				goto done;
60 			if ((be64_to_cpu(*hpte) & HPTE_V_VALID) == 0)
61 				break;
62 			hpte += 2;
63 		}
64 	} else {
65 		hpte += i * 2;
66 		if (*hpte & HPTE_V_VALID)
67 			goto done;
68 	}
69 
70 	hpte[0] = cpu_to_be64(kvmppc_get_gpr(vcpu, 6));
71 	hpte[1] = cpu_to_be64(kvmppc_get_gpr(vcpu, 7));
72 	pteg_addr += i * HPTE_SIZE;
73 	copy_to_user((void __user *)pteg_addr, hpte, HPTE_SIZE);
74 	kvmppc_set_gpr(vcpu, 4, pte_index | i);
75 	ret = H_SUCCESS;
76 
77  done:
78 	mutex_unlock(&vcpu->kvm->arch.hpt_mutex);
79 	kvmppc_set_gpr(vcpu, 3, ret);
80 
81 	return EMULATE_DONE;
82 }
83 
84 static int kvmppc_h_pr_remove(struct kvm_vcpu *vcpu)
85 {
86 	unsigned long flags= kvmppc_get_gpr(vcpu, 4);
87 	unsigned long pte_index = kvmppc_get_gpr(vcpu, 5);
88 	unsigned long avpn = kvmppc_get_gpr(vcpu, 6);
89 	unsigned long v = 0, pteg, rb;
90 	unsigned long pte[2];
91 	long int ret;
92 
93 	pteg = get_pteg_addr(vcpu, pte_index);
94 	mutex_lock(&vcpu->kvm->arch.hpt_mutex);
95 	copy_from_user(pte, (void __user *)pteg, sizeof(pte));
96 	pte[0] = be64_to_cpu(pte[0]);
97 	pte[1] = be64_to_cpu(pte[1]);
98 
99 	ret = H_NOT_FOUND;
100 	if ((pte[0] & HPTE_V_VALID) == 0 ||
101 	    ((flags & H_AVPN) && (pte[0] & ~0x7fUL) != avpn) ||
102 	    ((flags & H_ANDCOND) && (pte[0] & avpn) != 0))
103 		goto done;
104 
105 	copy_to_user((void __user *)pteg, &v, sizeof(v));
106 
107 	rb = compute_tlbie_rb(pte[0], pte[1], pte_index);
108 	vcpu->arch.mmu.tlbie(vcpu, rb, rb & 1 ? true : false);
109 
110 	ret = H_SUCCESS;
111 	kvmppc_set_gpr(vcpu, 4, pte[0]);
112 	kvmppc_set_gpr(vcpu, 5, pte[1]);
113 
114  done:
115 	mutex_unlock(&vcpu->kvm->arch.hpt_mutex);
116 	kvmppc_set_gpr(vcpu, 3, ret);
117 
118 	return EMULATE_DONE;
119 }
120 
121 /* Request defs for kvmppc_h_pr_bulk_remove() */
122 #define H_BULK_REMOVE_TYPE             0xc000000000000000ULL
123 #define   H_BULK_REMOVE_REQUEST        0x4000000000000000ULL
124 #define   H_BULK_REMOVE_RESPONSE       0x8000000000000000ULL
125 #define   H_BULK_REMOVE_END            0xc000000000000000ULL
126 #define H_BULK_REMOVE_CODE             0x3000000000000000ULL
127 #define   H_BULK_REMOVE_SUCCESS        0x0000000000000000ULL
128 #define   H_BULK_REMOVE_NOT_FOUND      0x1000000000000000ULL
129 #define   H_BULK_REMOVE_PARM           0x2000000000000000ULL
130 #define   H_BULK_REMOVE_HW             0x3000000000000000ULL
131 #define H_BULK_REMOVE_RC               0x0c00000000000000ULL
132 #define H_BULK_REMOVE_FLAGS            0x0300000000000000ULL
133 #define   H_BULK_REMOVE_ABSOLUTE       0x0000000000000000ULL
134 #define   H_BULK_REMOVE_ANDCOND        0x0100000000000000ULL
135 #define   H_BULK_REMOVE_AVPN           0x0200000000000000ULL
136 #define H_BULK_REMOVE_PTEX             0x00ffffffffffffffULL
137 #define H_BULK_REMOVE_MAX_BATCH        4
138 
139 static int kvmppc_h_pr_bulk_remove(struct kvm_vcpu *vcpu)
140 {
141 	int i;
142 	int paramnr = 4;
143 	int ret = H_SUCCESS;
144 
145 	mutex_lock(&vcpu->kvm->arch.hpt_mutex);
146 	for (i = 0; i < H_BULK_REMOVE_MAX_BATCH; i++) {
147 		unsigned long tsh = kvmppc_get_gpr(vcpu, paramnr+(2*i));
148 		unsigned long tsl = kvmppc_get_gpr(vcpu, paramnr+(2*i)+1);
149 		unsigned long pteg, rb, flags;
150 		unsigned long pte[2];
151 		unsigned long v = 0;
152 
153 		if ((tsh & H_BULK_REMOVE_TYPE) == H_BULK_REMOVE_END) {
154 			break; /* Exit success */
155 		} else if ((tsh & H_BULK_REMOVE_TYPE) !=
156 			   H_BULK_REMOVE_REQUEST) {
157 			ret = H_PARAMETER;
158 			break; /* Exit fail */
159 		}
160 
161 		tsh &= H_BULK_REMOVE_PTEX | H_BULK_REMOVE_FLAGS;
162 		tsh |= H_BULK_REMOVE_RESPONSE;
163 
164 		if ((tsh & H_BULK_REMOVE_ANDCOND) &&
165 		    (tsh & H_BULK_REMOVE_AVPN)) {
166 			tsh |= H_BULK_REMOVE_PARM;
167 			kvmppc_set_gpr(vcpu, paramnr+(2*i), tsh);
168 			ret = H_PARAMETER;
169 			break; /* Exit fail */
170 		}
171 
172 		pteg = get_pteg_addr(vcpu, tsh & H_BULK_REMOVE_PTEX);
173 		copy_from_user(pte, (void __user *)pteg, sizeof(pte));
174 		pte[0] = be64_to_cpu(pte[0]);
175 		pte[1] = be64_to_cpu(pte[1]);
176 
177 		/* tsl = AVPN */
178 		flags = (tsh & H_BULK_REMOVE_FLAGS) >> 26;
179 
180 		if ((pte[0] & HPTE_V_VALID) == 0 ||
181 		    ((flags & H_AVPN) && (pte[0] & ~0x7fUL) != tsl) ||
182 		    ((flags & H_ANDCOND) && (pte[0] & tsl) != 0)) {
183 			tsh |= H_BULK_REMOVE_NOT_FOUND;
184 		} else {
185 			/* Splat the pteg in (userland) hpt */
186 			copy_to_user((void __user *)pteg, &v, sizeof(v));
187 
188 			rb = compute_tlbie_rb(pte[0], pte[1],
189 					      tsh & H_BULK_REMOVE_PTEX);
190 			vcpu->arch.mmu.tlbie(vcpu, rb, rb & 1 ? true : false);
191 			tsh |= H_BULK_REMOVE_SUCCESS;
192 			tsh |= (pte[1] & (HPTE_R_C | HPTE_R_R)) << 43;
193 		}
194 		kvmppc_set_gpr(vcpu, paramnr+(2*i), tsh);
195 	}
196 	mutex_unlock(&vcpu->kvm->arch.hpt_mutex);
197 	kvmppc_set_gpr(vcpu, 3, ret);
198 
199 	return EMULATE_DONE;
200 }
201 
202 static int kvmppc_h_pr_protect(struct kvm_vcpu *vcpu)
203 {
204 	unsigned long flags = kvmppc_get_gpr(vcpu, 4);
205 	unsigned long pte_index = kvmppc_get_gpr(vcpu, 5);
206 	unsigned long avpn = kvmppc_get_gpr(vcpu, 6);
207 	unsigned long rb, pteg, r, v;
208 	unsigned long pte[2];
209 	long int ret;
210 
211 	pteg = get_pteg_addr(vcpu, pte_index);
212 	mutex_lock(&vcpu->kvm->arch.hpt_mutex);
213 	copy_from_user(pte, (void __user *)pteg, sizeof(pte));
214 	pte[0] = be64_to_cpu(pte[0]);
215 	pte[1] = be64_to_cpu(pte[1]);
216 
217 	ret = H_NOT_FOUND;
218 	if ((pte[0] & HPTE_V_VALID) == 0 ||
219 	    ((flags & H_AVPN) && (pte[0] & ~0x7fUL) != avpn))
220 		goto done;
221 
222 	v = pte[0];
223 	r = pte[1];
224 	r &= ~(HPTE_R_PP0 | HPTE_R_PP | HPTE_R_N | HPTE_R_KEY_HI |
225 	       HPTE_R_KEY_LO);
226 	r |= (flags << 55) & HPTE_R_PP0;
227 	r |= (flags << 48) & HPTE_R_KEY_HI;
228 	r |= flags & (HPTE_R_PP | HPTE_R_N | HPTE_R_KEY_LO);
229 
230 	pte[1] = r;
231 
232 	rb = compute_tlbie_rb(v, r, pte_index);
233 	vcpu->arch.mmu.tlbie(vcpu, rb, rb & 1 ? true : false);
234 	pte[0] = cpu_to_be64(pte[0]);
235 	pte[1] = cpu_to_be64(pte[1]);
236 	copy_to_user((void __user *)pteg, pte, sizeof(pte));
237 	ret = H_SUCCESS;
238 
239  done:
240 	mutex_unlock(&vcpu->kvm->arch.hpt_mutex);
241 	kvmppc_set_gpr(vcpu, 3, ret);
242 
243 	return EMULATE_DONE;
244 }
245 
246 static int kvmppc_h_pr_put_tce(struct kvm_vcpu *vcpu)
247 {
248 	unsigned long liobn = kvmppc_get_gpr(vcpu, 4);
249 	unsigned long ioba = kvmppc_get_gpr(vcpu, 5);
250 	unsigned long tce = kvmppc_get_gpr(vcpu, 6);
251 	long rc;
252 
253 	rc = kvmppc_h_put_tce(vcpu, liobn, ioba, tce);
254 	if (rc == H_TOO_HARD)
255 		return EMULATE_FAIL;
256 	kvmppc_set_gpr(vcpu, 3, rc);
257 	return EMULATE_DONE;
258 }
259 
260 static int kvmppc_h_pr_xics_hcall(struct kvm_vcpu *vcpu, u32 cmd)
261 {
262 	long rc = kvmppc_xics_hcall(vcpu, cmd);
263 	kvmppc_set_gpr(vcpu, 3, rc);
264 	return EMULATE_DONE;
265 }
266 
267 int kvmppc_h_pr(struct kvm_vcpu *vcpu, unsigned long cmd)
268 {
269 	switch (cmd) {
270 	case H_ENTER:
271 		return kvmppc_h_pr_enter(vcpu);
272 	case H_REMOVE:
273 		return kvmppc_h_pr_remove(vcpu);
274 	case H_PROTECT:
275 		return kvmppc_h_pr_protect(vcpu);
276 	case H_BULK_REMOVE:
277 		return kvmppc_h_pr_bulk_remove(vcpu);
278 	case H_PUT_TCE:
279 		return kvmppc_h_pr_put_tce(vcpu);
280 	case H_CEDE:
281 		kvmppc_set_msr_fast(vcpu, kvmppc_get_msr(vcpu) | MSR_EE);
282 		kvm_vcpu_block(vcpu);
283 		clear_bit(KVM_REQ_UNHALT, &vcpu->requests);
284 		vcpu->stat.halt_wakeup++;
285 		return EMULATE_DONE;
286 	case H_XIRR:
287 	case H_CPPR:
288 	case H_EOI:
289 	case H_IPI:
290 	case H_IPOLL:
291 	case H_XIRR_X:
292 		if (kvmppc_xics_enabled(vcpu))
293 			return kvmppc_h_pr_xics_hcall(vcpu, cmd);
294 		break;
295 	case H_RTAS:
296 		if (list_empty(&vcpu->kvm->arch.rtas_tokens))
297 			return RESUME_HOST;
298 		if (kvmppc_rtas_hcall(vcpu))
299 			break;
300 		kvmppc_set_gpr(vcpu, 3, 0);
301 		return EMULATE_DONE;
302 	}
303 
304 	return EMULATE_FAIL;
305 }
306