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 <asm/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
);
53 copy_from_user(pteg
, (void __user
*)pteg_addr
, sizeof(pteg
));
57 if (likely((flags
& H_EXACT
) == 0)) {
61 if ((be64_to_cpu(*hpte
) & HPTE_V_VALID
) == 0)
67 if (*hpte
& HPTE_V_VALID
)
71 hpte
[0] = cpu_to_be64(kvmppc_get_gpr(vcpu
, 6));
72 hpte
[1] = cpu_to_be64(kvmppc_get_gpr(vcpu
, 7));
73 pteg_addr
+= i
* HPTE_SIZE
;
74 copy_to_user((void __user
*)pteg_addr
, hpte
, HPTE_SIZE
);
75 kvmppc_set_gpr(vcpu
, 4, pte_index
| i
);
79 mutex_unlock(&vcpu
->kvm
->arch
.hpt_mutex
);
80 kvmppc_set_gpr(vcpu
, 3, ret
);
85 static int kvmppc_h_pr_remove(struct kvm_vcpu
*vcpu
)
87 unsigned long flags
= kvmppc_get_gpr(vcpu
, 4);
88 unsigned long pte_index
= kvmppc_get_gpr(vcpu
, 5);
89 unsigned long avpn
= kvmppc_get_gpr(vcpu
, 6);
90 unsigned long v
= 0, pteg
, rb
;
94 pteg
= get_pteg_addr(vcpu
, pte_index
);
95 mutex_lock(&vcpu
->kvm
->arch
.hpt_mutex
);
96 copy_from_user(pte
, (void __user
*)pteg
, sizeof(pte
));
97 pte
[0] = be64_to_cpu((__force __be64
)pte
[0]);
98 pte
[1] = be64_to_cpu((__force __be64
)pte
[1]);
101 if ((pte
[0] & HPTE_V_VALID
) == 0 ||
102 ((flags
& H_AVPN
) && (pte
[0] & ~0x7fUL
) != avpn
) ||
103 ((flags
& H_ANDCOND
) && (pte
[0] & avpn
) != 0))
106 copy_to_user((void __user
*)pteg
, &v
, sizeof(v
));
108 rb
= compute_tlbie_rb(pte
[0], pte
[1], pte_index
);
109 vcpu
->arch
.mmu
.tlbie(vcpu
, rb
, rb
& 1 ? true : false);
112 kvmppc_set_gpr(vcpu
, 4, pte
[0]);
113 kvmppc_set_gpr(vcpu
, 5, pte
[1]);
116 mutex_unlock(&vcpu
->kvm
->arch
.hpt_mutex
);
117 kvmppc_set_gpr(vcpu
, 3, ret
);
122 /* Request defs for kvmppc_h_pr_bulk_remove() */
123 #define H_BULK_REMOVE_TYPE 0xc000000000000000ULL
124 #define H_BULK_REMOVE_REQUEST 0x4000000000000000ULL
125 #define H_BULK_REMOVE_RESPONSE 0x8000000000000000ULL
126 #define H_BULK_REMOVE_END 0xc000000000000000ULL
127 #define H_BULK_REMOVE_CODE 0x3000000000000000ULL
128 #define H_BULK_REMOVE_SUCCESS 0x0000000000000000ULL
129 #define H_BULK_REMOVE_NOT_FOUND 0x1000000000000000ULL
130 #define H_BULK_REMOVE_PARM 0x2000000000000000ULL
131 #define H_BULK_REMOVE_HW 0x3000000000000000ULL
132 #define H_BULK_REMOVE_RC 0x0c00000000000000ULL
133 #define H_BULK_REMOVE_FLAGS 0x0300000000000000ULL
134 #define H_BULK_REMOVE_ABSOLUTE 0x0000000000000000ULL
135 #define H_BULK_REMOVE_ANDCOND 0x0100000000000000ULL
136 #define H_BULK_REMOVE_AVPN 0x0200000000000000ULL
137 #define H_BULK_REMOVE_PTEX 0x00ffffffffffffffULL
138 #define H_BULK_REMOVE_MAX_BATCH 4
140 static int kvmppc_h_pr_bulk_remove(struct kvm_vcpu
*vcpu
)
146 mutex_lock(&vcpu
->kvm
->arch
.hpt_mutex
);
147 for (i
= 0; i
< H_BULK_REMOVE_MAX_BATCH
; i
++) {
148 unsigned long tsh
= kvmppc_get_gpr(vcpu
, paramnr
+(2*i
));
149 unsigned long tsl
= kvmppc_get_gpr(vcpu
, paramnr
+(2*i
)+1);
150 unsigned long pteg
, rb
, flags
;
151 unsigned long pte
[2];
154 if ((tsh
& H_BULK_REMOVE_TYPE
) == H_BULK_REMOVE_END
) {
155 break; /* Exit success */
156 } else if ((tsh
& H_BULK_REMOVE_TYPE
) !=
157 H_BULK_REMOVE_REQUEST
) {
159 break; /* Exit fail */
162 tsh
&= H_BULK_REMOVE_PTEX
| H_BULK_REMOVE_FLAGS
;
163 tsh
|= H_BULK_REMOVE_RESPONSE
;
165 if ((tsh
& H_BULK_REMOVE_ANDCOND
) &&
166 (tsh
& H_BULK_REMOVE_AVPN
)) {
167 tsh
|= H_BULK_REMOVE_PARM
;
168 kvmppc_set_gpr(vcpu
, paramnr
+(2*i
), tsh
);
170 break; /* Exit fail */
173 pteg
= get_pteg_addr(vcpu
, tsh
& H_BULK_REMOVE_PTEX
);
174 copy_from_user(pte
, (void __user
*)pteg
, sizeof(pte
));
175 pte
[0] = be64_to_cpu((__force __be64
)pte
[0]);
176 pte
[1] = be64_to_cpu((__force __be64
)pte
[1]);
179 flags
= (tsh
& H_BULK_REMOVE_FLAGS
) >> 26;
181 if ((pte
[0] & HPTE_V_VALID
) == 0 ||
182 ((flags
& H_AVPN
) && (pte
[0] & ~0x7fUL
) != tsl
) ||
183 ((flags
& H_ANDCOND
) && (pte
[0] & tsl
) != 0)) {
184 tsh
|= H_BULK_REMOVE_NOT_FOUND
;
186 /* Splat the pteg in (userland) hpt */
187 copy_to_user((void __user
*)pteg
, &v
, sizeof(v
));
189 rb
= compute_tlbie_rb(pte
[0], pte
[1],
190 tsh
& H_BULK_REMOVE_PTEX
);
191 vcpu
->arch
.mmu
.tlbie(vcpu
, rb
, rb
& 1 ? true : false);
192 tsh
|= H_BULK_REMOVE_SUCCESS
;
193 tsh
|= (pte
[1] & (HPTE_R_C
| HPTE_R_R
)) << 43;
195 kvmppc_set_gpr(vcpu
, paramnr
+(2*i
), tsh
);
197 mutex_unlock(&vcpu
->kvm
->arch
.hpt_mutex
);
198 kvmppc_set_gpr(vcpu
, 3, ret
);
203 static int kvmppc_h_pr_protect(struct kvm_vcpu
*vcpu
)
205 unsigned long flags
= kvmppc_get_gpr(vcpu
, 4);
206 unsigned long pte_index
= kvmppc_get_gpr(vcpu
, 5);
207 unsigned long avpn
= kvmppc_get_gpr(vcpu
, 6);
208 unsigned long rb
, pteg
, r
, v
;
209 unsigned long pte
[2];
212 pteg
= get_pteg_addr(vcpu
, pte_index
);
213 mutex_lock(&vcpu
->kvm
->arch
.hpt_mutex
);
214 copy_from_user(pte
, (void __user
*)pteg
, sizeof(pte
));
215 pte
[0] = be64_to_cpu((__force __be64
)pte
[0]);
216 pte
[1] = be64_to_cpu((__force __be64
)pte
[1]);
219 if ((pte
[0] & HPTE_V_VALID
) == 0 ||
220 ((flags
& H_AVPN
) && (pte
[0] & ~0x7fUL
) != avpn
))
225 r
&= ~(HPTE_R_PP0
| HPTE_R_PP
| HPTE_R_N
| HPTE_R_KEY_HI
|
227 r
|= (flags
<< 55) & HPTE_R_PP0
;
228 r
|= (flags
<< 48) & HPTE_R_KEY_HI
;
229 r
|= flags
& (HPTE_R_PP
| HPTE_R_N
| HPTE_R_KEY_LO
);
233 rb
= compute_tlbie_rb(v
, r
, pte_index
);
234 vcpu
->arch
.mmu
.tlbie(vcpu
, rb
, rb
& 1 ? true : false);
235 pte
[0] = (__force u64
)cpu_to_be64(pte
[0]);
236 pte
[1] = (__force u64
)cpu_to_be64(pte
[1]);
237 copy_to_user((void __user
*)pteg
, pte
, sizeof(pte
));
241 mutex_unlock(&vcpu
->kvm
->arch
.hpt_mutex
);
242 kvmppc_set_gpr(vcpu
, 3, ret
);
247 static int kvmppc_h_pr_put_tce(struct kvm_vcpu
*vcpu
)
249 unsigned long liobn
= kvmppc_get_gpr(vcpu
, 4);
250 unsigned long ioba
= kvmppc_get_gpr(vcpu
, 5);
251 unsigned long tce
= kvmppc_get_gpr(vcpu
, 6);
254 rc
= kvmppc_h_put_tce(vcpu
, liobn
, ioba
, tce
);
255 if (rc
== H_TOO_HARD
)
257 kvmppc_set_gpr(vcpu
, 3, rc
);
261 static int kvmppc_h_pr_logical_ci_load(struct kvm_vcpu
*vcpu
)
265 rc
= kvmppc_h_logical_ci_load(vcpu
);
266 if (rc
== H_TOO_HARD
)
268 kvmppc_set_gpr(vcpu
, 3, rc
);
272 static int kvmppc_h_pr_logical_ci_store(struct kvm_vcpu
*vcpu
)
276 rc
= kvmppc_h_logical_ci_store(vcpu
);
277 if (rc
== H_TOO_HARD
)
279 kvmppc_set_gpr(vcpu
, 3, rc
);
283 static int kvmppc_h_pr_put_tce_indirect(struct kvm_vcpu
*vcpu
)
285 unsigned long liobn
= kvmppc_get_gpr(vcpu
, 4);
286 unsigned long ioba
= kvmppc_get_gpr(vcpu
, 5);
287 unsigned long tce
= kvmppc_get_gpr(vcpu
, 6);
288 unsigned long npages
= kvmppc_get_gpr(vcpu
, 7);
291 rc
= kvmppc_h_put_tce_indirect(vcpu
, liobn
, ioba
,
293 if (rc
== H_TOO_HARD
)
295 kvmppc_set_gpr(vcpu
, 3, rc
);
299 static int kvmppc_h_pr_stuff_tce(struct kvm_vcpu
*vcpu
)
301 unsigned long liobn
= kvmppc_get_gpr(vcpu
, 4);
302 unsigned long ioba
= kvmppc_get_gpr(vcpu
, 5);
303 unsigned long tce_value
= kvmppc_get_gpr(vcpu
, 6);
304 unsigned long npages
= kvmppc_get_gpr(vcpu
, 7);
307 rc
= kvmppc_h_stuff_tce(vcpu
, liobn
, ioba
, tce_value
, npages
);
308 if (rc
== H_TOO_HARD
)
310 kvmppc_set_gpr(vcpu
, 3, rc
);
314 static int kvmppc_h_pr_xics_hcall(struct kvm_vcpu
*vcpu
, u32 cmd
)
316 long rc
= kvmppc_xics_hcall(vcpu
, cmd
);
317 kvmppc_set_gpr(vcpu
, 3, rc
);
321 int kvmppc_h_pr(struct kvm_vcpu
*vcpu
, unsigned long cmd
)
325 if (cmd
<= MAX_HCALL_OPCODE
&&
326 !test_bit(cmd
/4, vcpu
->kvm
->arch
.enabled_hcalls
))
331 return kvmppc_h_pr_enter(vcpu
);
333 return kvmppc_h_pr_remove(vcpu
);
335 return kvmppc_h_pr_protect(vcpu
);
337 return kvmppc_h_pr_bulk_remove(vcpu
);
339 return kvmppc_h_pr_put_tce(vcpu
);
340 case H_PUT_TCE_INDIRECT
:
341 return kvmppc_h_pr_put_tce_indirect(vcpu
);
343 return kvmppc_h_pr_stuff_tce(vcpu
);
345 kvmppc_set_msr_fast(vcpu
, kvmppc_get_msr(vcpu
) | MSR_EE
);
346 kvm_vcpu_block(vcpu
);
347 clear_bit(KVM_REQ_UNHALT
, &vcpu
->requests
);
348 vcpu
->stat
.halt_wakeup
++;
350 case H_LOGICAL_CI_LOAD
:
351 return kvmppc_h_pr_logical_ci_load(vcpu
);
352 case H_LOGICAL_CI_STORE
:
353 return kvmppc_h_pr_logical_ci_store(vcpu
);
360 if (kvmppc_xics_enabled(vcpu
))
361 return kvmppc_h_pr_xics_hcall(vcpu
, cmd
);
364 if (list_empty(&vcpu
->kvm
->arch
.rtas_tokens
))
366 idx
= srcu_read_lock(&vcpu
->kvm
->srcu
);
367 rc
= kvmppc_rtas_hcall(vcpu
);
368 srcu_read_unlock(&vcpu
->kvm
->srcu
, idx
);
371 kvmppc_set_gpr(vcpu
, 3, 0);
378 int kvmppc_hcall_impl_pr(unsigned long cmd
)
387 case H_LOGICAL_CI_LOAD
:
388 case H_LOGICAL_CI_STORE
:
389 #ifdef CONFIG_KVM_XICS
403 * List of hcall numbers to enable by default.
404 * For compatibility with old userspace, we enable by default
405 * all hcalls that were implemented before the hcall-enabling
406 * facility was added. Note this list should not include H_RTAS.
408 static unsigned int default_hcall_list
[] = {
415 #ifdef CONFIG_KVM_XICS
426 void kvmppc_pr_init_default_hcalls(struct kvm
*kvm
)
431 for (i
= 0; default_hcall_list
[i
]; ++i
) {
432 hcall
= default_hcall_list
[i
];
433 WARN_ON(!kvmppc_hcall_impl_pr(hcall
));
434 __set_bit(hcall
/ 4, kvm
->arch
.enabled_hcalls
);