2 * Copyright (C) 2011. Freescale Inc. All rights reserved.
5 * Alexander Graf <agraf@suse.de>
6 * Paul Mackerras <paulus@samba.org>
10 * Hypercall handling for running PAPR guests in PR KVM on Book 3S
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.
18 #include <linux/anon_inodes.h>
20 #include <linux/uaccess.h>
21 #include <asm/kvm_ppc.h>
22 #include <asm/kvm_book3s.h>
24 #define HPTE_SIZE 16 /* bytes per HPT entry */
26 static unsigned long get_pteg_addr(struct kvm_vcpu
*vcpu
, long pte_index
)
28 struct kvmppc_vcpu_book3s
*vcpu_book3s
= to_book3s(vcpu
);
29 unsigned long pteg_addr
;
32 pte_index
&= ((1 << ((vcpu_book3s
->sdr1
& 0x1f) + 11)) - 1) << 7 | 0x70;
33 pteg_addr
= vcpu_book3s
->sdr1
& 0xfffffffffffc0000ULL
;
34 pteg_addr
|= pte_index
;
39 static int kvmppc_h_pr_enter(struct kvm_vcpu
*vcpu
)
41 long flags
= kvmppc_get_gpr(vcpu
, 4);
42 long pte_index
= kvmppc_get_gpr(vcpu
, 5);
45 unsigned long pteg_addr
, i
;
50 pteg_addr
= get_pteg_addr(vcpu
, pte_index
);
52 mutex_lock(&vcpu
->kvm
->arch
.hpt_mutex
);
54 if (copy_from_user(pteg
, (void __user
*)pteg_addr
, sizeof(pteg
)))
59 if (likely((flags
& H_EXACT
) == 0)) {
63 if ((be64_to_cpu(*hpte
) & HPTE_V_VALID
) == 0)
69 if (*hpte
& HPTE_V_VALID
)
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
;
77 if (copy_to_user((void __user
*)pteg_addr
, hpte
, HPTE_SIZE
))
79 kvmppc_set_gpr(vcpu
, 4, pte_index
| i
);
83 mutex_unlock(&vcpu
->kvm
->arch
.hpt_mutex
);
84 kvmppc_set_gpr(vcpu
, 3, ret
);
89 static int kvmppc_h_pr_remove(struct kvm_vcpu
*vcpu
)
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
;
98 pteg
= get_pteg_addr(vcpu
, pte_index
);
99 mutex_lock(&vcpu
->kvm
->arch
.hpt_mutex
);
101 if (copy_from_user(pte
, (void __user
*)pteg
, sizeof(pte
)))
103 pte
[0] = be64_to_cpu((__force __be64
)pte
[0]);
104 pte
[1] = be64_to_cpu((__force __be64
)pte
[1]);
107 if ((pte
[0] & HPTE_V_VALID
) == 0 ||
108 ((flags
& H_AVPN
) && (pte
[0] & ~0x7fUL
) != avpn
) ||
109 ((flags
& H_ANDCOND
) && (pte
[0] & avpn
) != 0))
113 if (copy_to_user((void __user
*)pteg
, &v
, sizeof(v
)))
116 rb
= compute_tlbie_rb(pte
[0], pte
[1], pte_index
);
117 vcpu
->arch
.mmu
.tlbie(vcpu
, rb
, rb
& 1 ? true : false);
120 kvmppc_set_gpr(vcpu
, 4, pte
[0]);
121 kvmppc_set_gpr(vcpu
, 5, pte
[1]);
124 mutex_unlock(&vcpu
->kvm
->arch
.hpt_mutex
);
125 kvmppc_set_gpr(vcpu
, 3, ret
);
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
148 static int kvmppc_h_pr_bulk_remove(struct kvm_vcpu
*vcpu
)
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];
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
) {
167 break; /* Exit fail */
170 tsh
&= H_BULK_REMOVE_PTEX
| H_BULK_REMOVE_FLAGS
;
171 tsh
|= H_BULK_REMOVE_RESPONSE
;
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
);
178 break; /* Exit fail */
181 pteg
= get_pteg_addr(vcpu
, tsh
& H_BULK_REMOVE_PTEX
);
182 if (copy_from_user(pte
, (void __user
*)pteg
, sizeof(pte
))) {
186 pte
[0] = be64_to_cpu((__force __be64
)pte
[0]);
187 pte
[1] = be64_to_cpu((__force __be64
)pte
[1]);
190 flags
= (tsh
& H_BULK_REMOVE_FLAGS
) >> 26;
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
;
197 /* Splat the pteg in (userland) hpt */
198 if (copy_to_user((void __user
*)pteg
, &v
, sizeof(v
))) {
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;
209 kvmppc_set_gpr(vcpu
, paramnr
+(2*i
), tsh
);
211 mutex_unlock(&vcpu
->kvm
->arch
.hpt_mutex
);
212 kvmppc_set_gpr(vcpu
, 3, ret
);
217 static int kvmppc_h_pr_protect(struct kvm_vcpu
*vcpu
)
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];
226 pteg
= get_pteg_addr(vcpu
, pte_index
);
227 mutex_lock(&vcpu
->kvm
->arch
.hpt_mutex
);
229 if (copy_from_user(pte
, (void __user
*)pteg
, sizeof(pte
)))
231 pte
[0] = be64_to_cpu((__force __be64
)pte
[0]);
232 pte
[1] = be64_to_cpu((__force __be64
)pte
[1]);
235 if ((pte
[0] & HPTE_V_VALID
) == 0 ||
236 ((flags
& H_AVPN
) && (pte
[0] & ~0x7fUL
) != avpn
))
241 r
&= ~(HPTE_R_PP0
| HPTE_R_PP
| HPTE_R_N
| HPTE_R_KEY_HI
|
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
);
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]);
254 if (copy_to_user((void __user
*)pteg
, pte
, sizeof(pte
)))
259 mutex_unlock(&vcpu
->kvm
->arch
.hpt_mutex
);
260 kvmppc_set_gpr(vcpu
, 3, ret
);
265 static int kvmppc_h_pr_logical_ci_load(struct kvm_vcpu
*vcpu
)
269 rc
= kvmppc_h_logical_ci_load(vcpu
);
270 if (rc
== H_TOO_HARD
)
272 kvmppc_set_gpr(vcpu
, 3, rc
);
276 static int kvmppc_h_pr_logical_ci_store(struct kvm_vcpu
*vcpu
)
280 rc
= kvmppc_h_logical_ci_store(vcpu
);
281 if (rc
== H_TOO_HARD
)
283 kvmppc_set_gpr(vcpu
, 3, rc
);
287 #ifdef CONFIG_SPAPR_TCE_IOMMU
288 static int kvmppc_h_pr_put_tce(struct kvm_vcpu
*vcpu
)
290 unsigned long liobn
= kvmppc_get_gpr(vcpu
, 4);
291 unsigned long ioba
= kvmppc_get_gpr(vcpu
, 5);
292 unsigned long tce
= kvmppc_get_gpr(vcpu
, 6);
295 rc
= kvmppc_h_put_tce(vcpu
, liobn
, ioba
, tce
);
296 if (rc
== H_TOO_HARD
)
298 kvmppc_set_gpr(vcpu
, 3, rc
);
302 static int kvmppc_h_pr_put_tce_indirect(struct kvm_vcpu
*vcpu
)
304 unsigned long liobn
= kvmppc_get_gpr(vcpu
, 4);
305 unsigned long ioba
= kvmppc_get_gpr(vcpu
, 5);
306 unsigned long tce
= kvmppc_get_gpr(vcpu
, 6);
307 unsigned long npages
= kvmppc_get_gpr(vcpu
, 7);
310 rc
= kvmppc_h_put_tce_indirect(vcpu
, liobn
, ioba
,
312 if (rc
== H_TOO_HARD
)
314 kvmppc_set_gpr(vcpu
, 3, rc
);
318 static int kvmppc_h_pr_stuff_tce(struct kvm_vcpu
*vcpu
)
320 unsigned long liobn
= kvmppc_get_gpr(vcpu
, 4);
321 unsigned long ioba
= kvmppc_get_gpr(vcpu
, 5);
322 unsigned long tce_value
= kvmppc_get_gpr(vcpu
, 6);
323 unsigned long npages
= kvmppc_get_gpr(vcpu
, 7);
326 rc
= kvmppc_h_stuff_tce(vcpu
, liobn
, ioba
, tce_value
, npages
);
327 if (rc
== H_TOO_HARD
)
329 kvmppc_set_gpr(vcpu
, 3, rc
);
333 #else /* CONFIG_SPAPR_TCE_IOMMU */
334 static int kvmppc_h_pr_put_tce(struct kvm_vcpu
*vcpu
)
339 static int kvmppc_h_pr_put_tce_indirect(struct kvm_vcpu
*vcpu
)
344 static int kvmppc_h_pr_stuff_tce(struct kvm_vcpu
*vcpu
)
348 #endif /* CONFIG_SPAPR_TCE_IOMMU */
350 static int kvmppc_h_pr_xics_hcall(struct kvm_vcpu
*vcpu
, u32 cmd
)
352 long rc
= kvmppc_xics_hcall(vcpu
, cmd
);
353 kvmppc_set_gpr(vcpu
, 3, rc
);
357 int kvmppc_h_pr(struct kvm_vcpu
*vcpu
, unsigned long cmd
)
361 if (cmd
<= MAX_HCALL_OPCODE
&&
362 !test_bit(cmd
/4, vcpu
->kvm
->arch
.enabled_hcalls
))
367 return kvmppc_h_pr_enter(vcpu
);
369 return kvmppc_h_pr_remove(vcpu
);
371 return kvmppc_h_pr_protect(vcpu
);
373 return kvmppc_h_pr_bulk_remove(vcpu
);
375 return kvmppc_h_pr_put_tce(vcpu
);
376 case H_PUT_TCE_INDIRECT
:
377 return kvmppc_h_pr_put_tce_indirect(vcpu
);
379 return kvmppc_h_pr_stuff_tce(vcpu
);
381 kvmppc_set_msr_fast(vcpu
, kvmppc_get_msr(vcpu
) | MSR_EE
);
382 kvm_vcpu_block(vcpu
);
383 kvm_clear_request(KVM_REQ_UNHALT
, vcpu
);
384 vcpu
->stat
.halt_wakeup
++;
386 case H_LOGICAL_CI_LOAD
:
387 return kvmppc_h_pr_logical_ci_load(vcpu
);
388 case H_LOGICAL_CI_STORE
:
389 return kvmppc_h_pr_logical_ci_store(vcpu
);
396 if (kvmppc_xics_enabled(vcpu
))
397 return kvmppc_h_pr_xics_hcall(vcpu
, cmd
);
400 if (list_empty(&vcpu
->kvm
->arch
.rtas_tokens
))
402 idx
= srcu_read_lock(&vcpu
->kvm
->srcu
);
403 rc
= kvmppc_rtas_hcall(vcpu
);
404 srcu_read_unlock(&vcpu
->kvm
->srcu
, idx
);
407 kvmppc_set_gpr(vcpu
, 3, 0);
414 int kvmppc_hcall_impl_pr(unsigned long cmd
)
422 case H_PUT_TCE_INDIRECT
:
425 case H_LOGICAL_CI_LOAD
:
426 case H_LOGICAL_CI_STORE
:
427 #ifdef CONFIG_KVM_XICS
441 * List of hcall numbers to enable by default.
442 * For compatibility with old userspace, we enable by default
443 * all hcalls that were implemented before the hcall-enabling
444 * facility was added. Note this list should not include H_RTAS.
446 static unsigned int default_hcall_list
[] = {
453 #ifdef CONFIG_KVM_XICS
464 void kvmppc_pr_init_default_hcalls(struct kvm
*kvm
)
469 for (i
= 0; default_hcall_list
[i
]; ++i
) {
470 hcall
= default_hcall_list
[i
];
471 WARN_ON(!kvmppc_hcall_impl_pr(hcall
));
472 __set_bit(hcall
/ 4, kvm
->arch
.enabled_hcalls
);