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