Commit | Line | Data |
---|---|---|
de56a948 PM |
1 | /* |
2 | * This program is free software; you can redistribute it and/or modify | |
3 | * it under the terms of the GNU General Public License, version 2, as | |
4 | * published by the Free Software Foundation. | |
5 | * | |
6 | * This program is distributed in the hope that it will be useful, | |
7 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
8 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
9 | * GNU General Public License for more details. | |
10 | * | |
11 | * You should have received a copy of the GNU General Public License | |
12 | * along with this program; if not, write to the Free Software | |
13 | * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
14 | * | |
15 | * Copyright 2010 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com> | |
16 | */ | |
17 | ||
18 | #include <linux/types.h> | |
19 | #include <linux/string.h> | |
20 | #include <linux/kvm.h> | |
21 | #include <linux/kvm_host.h> | |
22 | #include <linux/highmem.h> | |
23 | #include <linux/gfp.h> | |
24 | #include <linux/slab.h> | |
25 | #include <linux/hugetlb.h> | |
8936dda4 | 26 | #include <linux/vmalloc.h> |
2c9097e4 | 27 | #include <linux/srcu.h> |
a2932923 PM |
28 | #include <linux/anon_inodes.h> |
29 | #include <linux/file.h> | |
e23a808b | 30 | #include <linux/debugfs.h> |
de56a948 | 31 | |
de56a948 PM |
32 | #include <asm/kvm_ppc.h> |
33 | #include <asm/kvm_book3s.h> | |
f64e8084 | 34 | #include <asm/book3s/64/mmu-hash.h> |
de56a948 PM |
35 | #include <asm/hvcall.h> |
36 | #include <asm/synch.h> | |
37 | #include <asm/ppc-opcode.h> | |
38 | #include <asm/cputable.h> | |
94171b19 | 39 | #include <asm/pte-walk.h> |
de56a948 | 40 | |
3c78f78a SW |
41 | #include "trace_hv.h" |
42 | ||
5e985969 DG |
43 | //#define DEBUG_RESIZE_HPT 1 |
44 | ||
45 | #ifdef DEBUG_RESIZE_HPT | |
46 | #define resize_hpt_debug(resize, ...) \ | |
47 | do { \ | |
48 | printk(KERN_DEBUG "RESIZE HPT %p: ", resize); \ | |
49 | printk(__VA_ARGS__); \ | |
50 | } while (0) | |
51 | #else | |
52 | #define resize_hpt_debug(resize, ...) \ | |
53 | do { } while (0) | |
54 | #endif | |
55 | ||
7ed661bf PM |
56 | static long kvmppc_virtmode_do_h_enter(struct kvm *kvm, unsigned long flags, |
57 | long pte_index, unsigned long pteh, | |
58 | unsigned long ptel, unsigned long *pte_idx_ret); | |
5e985969 DG |
59 | |
60 | struct kvm_resize_hpt { | |
61 | /* These fields read-only after init */ | |
62 | struct kvm *kvm; | |
63 | struct work_struct work; | |
64 | u32 order; | |
65 | ||
66 | /* These fields protected by kvm->lock */ | |
3073774e SP |
67 | |
68 | /* Possible values and their usage: | |
69 | * <0 an error occurred during allocation, | |
70 | * -EBUSY allocation is in the progress, | |
71 | * 0 allocation made successfuly. | |
72 | */ | |
5e985969 | 73 | int error; |
b5baa687 | 74 | |
3073774e SP |
75 | /* Private to the work thread, until error != -EBUSY, |
76 | * then protected by kvm->lock. | |
77 | */ | |
b5baa687 | 78 | struct kvm_hpt_info hpt; |
5e985969 DG |
79 | }; |
80 | ||
aae0777f | 81 | int kvmppc_allocate_hpt(struct kvm_hpt_info *info, u32 order) |
de56a948 | 82 | { |
792fc497 | 83 | unsigned long hpt = 0; |
aae0777f | 84 | int cma = 0; |
fa61a4e3 | 85 | struct page *page = NULL; |
aae0777f DG |
86 | struct revmap_entry *rev; |
87 | unsigned long npte; | |
de56a948 | 88 | |
aae0777f DG |
89 | if ((order < PPC_MIN_HPT_ORDER) || (order > PPC_MAX_HPT_ORDER)) |
90 | return -EINVAL; | |
32fad281 | 91 | |
db9a290d | 92 | page = kvm_alloc_hpt_cma(1ul << (order - PAGE_SHIFT)); |
792fc497 AK |
93 | if (page) { |
94 | hpt = (unsigned long)pfn_to_kaddr(page_to_pfn(page)); | |
02a68d05 | 95 | memset((void *)hpt, 0, (1ul << order)); |
aae0777f | 96 | cma = 1; |
de56a948 | 97 | } |
32fad281 | 98 | |
aae0777f | 99 | if (!hpt) |
dcda9b04 | 100 | hpt = __get_free_pages(GFP_KERNEL|__GFP_ZERO|__GFP_RETRY_MAYFAIL |
aae0777f | 101 | |__GFP_NOWARN, order - PAGE_SHIFT); |
32fad281 PM |
102 | |
103 | if (!hpt) | |
104 | return -ENOMEM; | |
105 | ||
aae0777f DG |
106 | /* HPTEs are 2**4 bytes long */ |
107 | npte = 1ul << (order - 4); | |
a56ee9f8 | 108 | |
8936dda4 | 109 | /* Allocate reverse map array */ |
42bc47b3 | 110 | rev = vmalloc(array_size(npte, sizeof(struct revmap_entry))); |
8936dda4 | 111 | if (!rev) { |
aae0777f DG |
112 | if (cma) |
113 | kvm_free_hpt_cma(page, 1 << (order - PAGE_SHIFT)); | |
114 | else | |
115 | free_pages(hpt, order - PAGE_SHIFT); | |
116 | return -ENOMEM; | |
8936dda4 | 117 | } |
8936dda4 | 118 | |
aae0777f DG |
119 | info->order = order; |
120 | info->virt = hpt; | |
121 | info->cma = cma; | |
122 | info->rev = rev; | |
de56a948 | 123 | |
de56a948 | 124 | return 0; |
aae0777f | 125 | } |
8936dda4 | 126 | |
aae0777f DG |
127 | void kvmppc_set_hpt(struct kvm *kvm, struct kvm_hpt_info *info) |
128 | { | |
129 | atomic64_set(&kvm->arch.mmio_update, 0); | |
130 | kvm->arch.hpt = *info; | |
131 | kvm->arch.sdr1 = __pa(info->virt) | (info->order - 18); | |
132 | ||
3a4f1760 TH |
133 | pr_debug("KVM guest htab at %lx (order %ld), LPID %x\n", |
134 | info->virt, (long)info->order, kvm->arch.lpid); | |
de56a948 PM |
135 | } |
136 | ||
f98a8bf9 | 137 | long kvmppc_alloc_reset_hpt(struct kvm *kvm, int order) |
32fad281 PM |
138 | { |
139 | long err = -EBUSY; | |
f98a8bf9 | 140 | struct kvm_hpt_info info; |
32fad281 PM |
141 | |
142 | mutex_lock(&kvm->lock); | |
1b151ce4 PM |
143 | if (kvm->arch.mmu_ready) { |
144 | kvm->arch.mmu_ready = 0; | |
145 | /* order mmu_ready vs. vcpus_running */ | |
32fad281 PM |
146 | smp_mb(); |
147 | if (atomic_read(&kvm->arch.vcpus_running)) { | |
1b151ce4 | 148 | kvm->arch.mmu_ready = 1; |
32fad281 PM |
149 | goto out; |
150 | } | |
151 | } | |
18c3640c PM |
152 | if (kvm_is_radix(kvm)) { |
153 | err = kvmppc_switch_mmu_to_hpt(kvm); | |
154 | if (err) | |
155 | goto out; | |
156 | } | |
157 | ||
f98a8bf9 DG |
158 | if (kvm->arch.hpt.order == order) { |
159 | /* We already have a suitable HPT */ | |
160 | ||
32fad281 | 161 | /* Set the entire HPT to 0, i.e. invalid HPTEs */ |
3f9d4f5a | 162 | memset((void *)kvm->arch.hpt.virt, 0, 1ul << order); |
a64fd707 PM |
163 | /* |
164 | * Reset all the reverse-mapping chains for all memslots | |
165 | */ | |
166 | kvmppc_rmap_reset(kvm); | |
32fad281 | 167 | err = 0; |
f98a8bf9 | 168 | goto out; |
32fad281 | 169 | } |
f98a8bf9 | 170 | |
ef427198 | 171 | if (kvm->arch.hpt.virt) { |
f98a8bf9 | 172 | kvmppc_free_hpt(&kvm->arch.hpt); |
ef427198 PM |
173 | kvmppc_rmap_reset(kvm); |
174 | } | |
f98a8bf9 DG |
175 | |
176 | err = kvmppc_allocate_hpt(&info, order); | |
177 | if (err < 0) | |
178 | goto out; | |
179 | kvmppc_set_hpt(kvm, &info); | |
180 | ||
181 | out: | |
ecba8297 DG |
182 | if (err == 0) |
183 | /* Ensure that each vcpu will flush its TLB on next entry. */ | |
184 | cpumask_setall(&kvm->arch.need_tlb_flush); | |
185 | ||
32fad281 PM |
186 | mutex_unlock(&kvm->lock); |
187 | return err; | |
188 | } | |
189 | ||
aae0777f | 190 | void kvmppc_free_hpt(struct kvm_hpt_info *info) |
de56a948 | 191 | { |
aae0777f | 192 | vfree(info->rev); |
18c3640c | 193 | info->rev = NULL; |
aae0777f DG |
194 | if (info->cma) |
195 | kvm_free_hpt_cma(virt_to_page(info->virt), | |
196 | 1 << (info->order - PAGE_SHIFT)); | |
197 | else if (info->virt) | |
198 | free_pages(info->virt, info->order - PAGE_SHIFT); | |
199 | info->virt = 0; | |
200 | info->order = 0; | |
de56a948 PM |
201 | } |
202 | ||
da9d1d7f PM |
203 | /* Bits in first HPTE dword for pagesize 4k, 64k or 16M */ |
204 | static inline unsigned long hpte0_pgsize_encoding(unsigned long pgsize) | |
205 | { | |
206 | return (pgsize > 0x1000) ? HPTE_V_LARGE : 0; | |
207 | } | |
208 | ||
209 | /* Bits in second HPTE dword for pagesize 4k, 64k or 16M */ | |
210 | static inline unsigned long hpte1_pgsize_encoding(unsigned long pgsize) | |
211 | { | |
212 | return (pgsize == 0x10000) ? 0x1000 : 0; | |
213 | } | |
214 | ||
215 | void kvmppc_map_vrma(struct kvm_vcpu *vcpu, struct kvm_memory_slot *memslot, | |
216 | unsigned long porder) | |
de56a948 PM |
217 | { |
218 | unsigned long i; | |
b2b2f165 | 219 | unsigned long npages; |
c77162de PM |
220 | unsigned long hp_v, hp_r; |
221 | unsigned long addr, hash; | |
da9d1d7f PM |
222 | unsigned long psize; |
223 | unsigned long hp0, hp1; | |
7ed661bf | 224 | unsigned long idx_ret; |
c77162de | 225 | long ret; |
32fad281 | 226 | struct kvm *kvm = vcpu->kvm; |
de56a948 | 227 | |
da9d1d7f PM |
228 | psize = 1ul << porder; |
229 | npages = memslot->npages >> (porder - PAGE_SHIFT); | |
de56a948 PM |
230 | |
231 | /* VRMA can't be > 1TB */ | |
8936dda4 PM |
232 | if (npages > 1ul << (40 - porder)) |
233 | npages = 1ul << (40 - porder); | |
de56a948 | 234 | /* Can't use more than 1 HPTE per HPTEG */ |
3d089f84 DG |
235 | if (npages > kvmppc_hpt_mask(&kvm->arch.hpt) + 1) |
236 | npages = kvmppc_hpt_mask(&kvm->arch.hpt) + 1; | |
de56a948 | 237 | |
da9d1d7f PM |
238 | hp0 = HPTE_V_1TB_SEG | (VRMA_VSID << (40 - 16)) | |
239 | HPTE_V_BOLTED | hpte0_pgsize_encoding(psize); | |
240 | hp1 = hpte1_pgsize_encoding(psize) | | |
241 | HPTE_R_R | HPTE_R_C | HPTE_R_M | PP_RWXX; | |
242 | ||
de56a948 | 243 | for (i = 0; i < npages; ++i) { |
c77162de | 244 | addr = i << porder; |
de56a948 | 245 | /* can't use hpt_hash since va > 64 bits */ |
3d089f84 DG |
246 | hash = (i ^ (VRMA_VSID ^ (VRMA_VSID << 25))) |
247 | & kvmppc_hpt_mask(&kvm->arch.hpt); | |
de56a948 PM |
248 | /* |
249 | * We assume that the hash table is empty and no | |
250 | * vcpus are using it at this stage. Since we create | |
251 | * at most one HPTE per HPTEG, we just assume entry 7 | |
252 | * is available and use it. | |
253 | */ | |
8936dda4 | 254 | hash = (hash << 3) + 7; |
da9d1d7f PM |
255 | hp_v = hp0 | ((addr >> 16) & ~0x7fUL); |
256 | hp_r = hp1 | addr; | |
7ed661bf PM |
257 | ret = kvmppc_virtmode_do_h_enter(kvm, H_EXACT, hash, hp_v, hp_r, |
258 | &idx_ret); | |
c77162de PM |
259 | if (ret != H_SUCCESS) { |
260 | pr_err("KVM: map_vrma at %lx failed, ret=%ld\n", | |
261 | addr, ret); | |
262 | break; | |
263 | } | |
de56a948 PM |
264 | } |
265 | } | |
266 | ||
267 | int kvmppc_mmu_hv_init(void) | |
268 | { | |
9e368f29 PM |
269 | unsigned long host_lpid, rsvd_lpid; |
270 | ||
b7557451 NP |
271 | if (!mmu_has_feature(MMU_FTR_LOCKLESS_TLBIE)) |
272 | return -EINVAL; | |
273 | ||
c17b98cf | 274 | /* POWER7 has 10-bit LPIDs (12-bit in POWER8) */ |
f3c99f97 PM |
275 | host_lpid = 0; |
276 | if (cpu_has_feature(CPU_FTR_HVMODE)) | |
277 | host_lpid = mfspr(SPRN_LPID); | |
c17b98cf | 278 | rsvd_lpid = LPID_RSVD; |
9e368f29 | 279 | |
043cc4d7 SW |
280 | kvmppc_init_lpid(rsvd_lpid + 1); |
281 | ||
282 | kvmppc_claim_lpid(host_lpid); | |
9e368f29 | 283 | /* rsvd_lpid is reserved for use in partition switching */ |
043cc4d7 | 284 | kvmppc_claim_lpid(rsvd_lpid); |
de56a948 PM |
285 | |
286 | return 0; | |
287 | } | |
288 | ||
de56a948 PM |
289 | static void kvmppc_mmu_book3s_64_hv_reset_msr(struct kvm_vcpu *vcpu) |
290 | { | |
e4e38121 MN |
291 | unsigned long msr = vcpu->arch.intr_msr; |
292 | ||
293 | /* If transactional, change to suspend mode on IRQ delivery */ | |
294 | if (MSR_TM_TRANSACTIONAL(vcpu->arch.shregs.msr)) | |
295 | msr |= MSR_TS_S; | |
296 | else | |
297 | msr |= vcpu->arch.shregs.msr & MSR_TS_MASK; | |
298 | kvmppc_set_msr(vcpu, msr); | |
de56a948 PM |
299 | } |
300 | ||
025c9511 | 301 | static long kvmppc_virtmode_do_h_enter(struct kvm *kvm, unsigned long flags, |
7ed661bf PM |
302 | long pte_index, unsigned long pteh, |
303 | unsigned long ptel, unsigned long *pte_idx_ret) | |
c77162de | 304 | { |
c77162de PM |
305 | long ret; |
306 | ||
342d3db7 PM |
307 | /* Protect linux PTE lookup from page table destruction */ |
308 | rcu_read_lock_sched(); /* this disables preemption too */ | |
7ed661bf PM |
309 | ret = kvmppc_do_h_enter(kvm, flags, pte_index, pteh, ptel, |
310 | current->mm->pgd, false, pte_idx_ret); | |
342d3db7 | 311 | rcu_read_unlock_sched(); |
c77162de PM |
312 | if (ret == H_TOO_HARD) { |
313 | /* this can't happen */ | |
314 | pr_err("KVM: Oops, kvmppc_h_enter returned too hard!\n"); | |
315 | ret = H_RESOURCE; /* or something */ | |
316 | } | |
317 | return ret; | |
318 | ||
319 | } | |
320 | ||
697d3899 PM |
321 | static struct kvmppc_slb *kvmppc_mmu_book3s_hv_find_slbe(struct kvm_vcpu *vcpu, |
322 | gva_t eaddr) | |
323 | { | |
324 | u64 mask; | |
325 | int i; | |
326 | ||
327 | for (i = 0; i < vcpu->arch.slb_nr; i++) { | |
328 | if (!(vcpu->arch.slb[i].orige & SLB_ESID_V)) | |
329 | continue; | |
330 | ||
331 | if (vcpu->arch.slb[i].origv & SLB_VSID_B_1T) | |
332 | mask = ESID_MASK_1T; | |
333 | else | |
334 | mask = ESID_MASK; | |
335 | ||
336 | if (((vcpu->arch.slb[i].orige ^ eaddr) & mask) == 0) | |
337 | return &vcpu->arch.slb[i]; | |
338 | } | |
339 | return NULL; | |
340 | } | |
341 | ||
342 | static unsigned long kvmppc_mmu_get_real_addr(unsigned long v, unsigned long r, | |
343 | unsigned long ea) | |
344 | { | |
345 | unsigned long ra_mask; | |
346 | ||
8dc6cca5 | 347 | ra_mask = kvmppc_actual_pgsz(v, r) - 1; |
697d3899 PM |
348 | return (r & HPTE_R_RPN & ~ra_mask) | (ea & ra_mask); |
349 | } | |
350 | ||
de56a948 | 351 | static int kvmppc_mmu_book3s_64_hv_xlate(struct kvm_vcpu *vcpu, gva_t eaddr, |
93b159b4 | 352 | struct kvmppc_pte *gpte, bool data, bool iswrite) |
de56a948 | 353 | { |
697d3899 PM |
354 | struct kvm *kvm = vcpu->kvm; |
355 | struct kvmppc_slb *slbe; | |
356 | unsigned long slb_v; | |
357 | unsigned long pp, key; | |
abb7c7dd | 358 | unsigned long v, orig_v, gr; |
6f22bd32 | 359 | __be64 *hptep; |
46dec40f | 360 | long int index; |
697d3899 PM |
361 | int virtmode = vcpu->arch.shregs.msr & (data ? MSR_DR : MSR_IR); |
362 | ||
18c3640c PM |
363 | if (kvm_is_radix(vcpu->kvm)) |
364 | return kvmppc_mmu_radix_xlate(vcpu, eaddr, gpte, data, iswrite); | |
365 | ||
697d3899 PM |
366 | /* Get SLB entry */ |
367 | if (virtmode) { | |
368 | slbe = kvmppc_mmu_book3s_hv_find_slbe(vcpu, eaddr); | |
369 | if (!slbe) | |
370 | return -EINVAL; | |
371 | slb_v = slbe->origv; | |
372 | } else { | |
373 | /* real mode access */ | |
374 | slb_v = vcpu->kvm->arch.vrma_slb_v; | |
375 | } | |
376 | ||
91648ec0 | 377 | preempt_disable(); |
697d3899 PM |
378 | /* Find the HPTE in the hash table */ |
379 | index = kvmppc_hv_find_lock_hpte(kvm, eaddr, slb_v, | |
380 | HPTE_V_VALID | HPTE_V_ABSENT); | |
91648ec0 | 381 | if (index < 0) { |
382 | preempt_enable(); | |
697d3899 | 383 | return -ENOENT; |
91648ec0 | 384 | } |
3f9d4f5a | 385 | hptep = (__be64 *)(kvm->arch.hpt.virt + (index << 4)); |
abb7c7dd PM |
386 | v = orig_v = be64_to_cpu(hptep[0]) & ~HPTE_V_HVLOCK; |
387 | if (cpu_has_feature(CPU_FTR_ARCH_300)) | |
388 | v = hpte_new_to_old_v(v, be64_to_cpu(hptep[1])); | |
3f9d4f5a | 389 | gr = kvm->arch.hpt.rev[index].guest_rpte; |
697d3899 | 390 | |
abb7c7dd | 391 | unlock_hpte(hptep, orig_v); |
91648ec0 | 392 | preempt_enable(); |
697d3899 PM |
393 | |
394 | gpte->eaddr = eaddr; | |
395 | gpte->vpage = ((v & HPTE_V_AVPN) << 4) | ((eaddr >> 12) & 0xfff); | |
396 | ||
397 | /* Get PP bits and key for permission check */ | |
398 | pp = gr & (HPTE_R_PP0 | HPTE_R_PP); | |
399 | key = (vcpu->arch.shregs.msr & MSR_PR) ? SLB_VSID_KP : SLB_VSID_KS; | |
400 | key &= slb_v; | |
401 | ||
402 | /* Calculate permissions */ | |
403 | gpte->may_read = hpte_read_permission(pp, key); | |
404 | gpte->may_write = hpte_write_permission(pp, key); | |
405 | gpte->may_execute = gpte->may_read && !(gr & (HPTE_R_N | HPTE_R_G)); | |
406 | ||
407 | /* Storage key permission check for POWER7 */ | |
c17b98cf | 408 | if (data && virtmode) { |
697d3899 PM |
409 | int amrfield = hpte_get_skey_perm(gr, vcpu->arch.amr); |
410 | if (amrfield & 1) | |
411 | gpte->may_read = 0; | |
412 | if (amrfield & 2) | |
413 | gpte->may_write = 0; | |
414 | } | |
415 | ||
416 | /* Get the guest physical address */ | |
417 | gpte->raddr = kvmppc_mmu_get_real_addr(v, gr, eaddr); | |
418 | return 0; | |
419 | } | |
420 | ||
421 | /* | |
422 | * Quick test for whether an instruction is a load or a store. | |
423 | * If the instruction is a load or a store, then this will indicate | |
424 | * which it is, at least on server processors. (Embedded processors | |
425 | * have some external PID instructions that don't follow the rule | |
426 | * embodied here.) If the instruction isn't a load or store, then | |
427 | * this doesn't return anything useful. | |
428 | */ | |
429 | static int instruction_is_store(unsigned int instr) | |
430 | { | |
431 | unsigned int mask; | |
432 | ||
433 | mask = 0x10000000; | |
434 | if ((instr & 0xfc000000) == 0x7c000000) | |
435 | mask = 0x100; /* major opcode 31 */ | |
436 | return (instr & mask) != 0; | |
437 | } | |
438 | ||
5a319350 PM |
439 | int kvmppc_hv_emulate_mmio(struct kvm_run *run, struct kvm_vcpu *vcpu, |
440 | unsigned long gpa, gva_t ea, int is_store) | |
697d3899 | 441 | { |
697d3899 | 442 | u32 last_inst; |
697d3899 | 443 | |
51f04726 | 444 | /* |
697d3899 PM |
445 | * If we fail, we just return to the guest and try executing it again. |
446 | */ | |
51f04726 MC |
447 | if (kvmppc_get_last_inst(vcpu, INST_GENERIC, &last_inst) != |
448 | EMULATE_DONE) | |
449 | return RESUME_GUEST; | |
697d3899 PM |
450 | |
451 | /* | |
452 | * WARNING: We do not know for sure whether the instruction we just | |
453 | * read from memory is the same that caused the fault in the first | |
454 | * place. If the instruction we read is neither an load or a store, | |
455 | * then it can't access memory, so we don't need to worry about | |
456 | * enforcing access permissions. So, assuming it is a load or | |
457 | * store, we just check that its direction (load or store) is | |
458 | * consistent with the original fault, since that's what we | |
459 | * checked the access permissions against. If there is a mismatch | |
460 | * we just return and retry the instruction. | |
461 | */ | |
462 | ||
51f04726 | 463 | if (instruction_is_store(last_inst) != !!is_store) |
697d3899 PM |
464 | return RESUME_GUEST; |
465 | ||
466 | /* | |
467 | * Emulated accesses are emulated by looking at the hash for | |
468 | * translation once, then performing the access later. The | |
469 | * translation could be invalidated in the meantime in which | |
470 | * point performing the subsequent memory access on the old | |
471 | * physical address could possibly be a security hole for the | |
472 | * guest (but not the host). | |
473 | * | |
474 | * This is less of an issue for MMIO stores since they aren't | |
475 | * globally visible. It could be an issue for MMIO loads to | |
476 | * a certain extent but we'll ignore it for now. | |
477 | */ | |
478 | ||
479 | vcpu->arch.paddr_accessed = gpa; | |
6020c0f6 | 480 | vcpu->arch.vaddr_accessed = ea; |
697d3899 PM |
481 | return kvmppc_emulate_mmio(run, vcpu); |
482 | } | |
483 | ||
484 | int kvmppc_book3s_hv_page_fault(struct kvm_run *run, struct kvm_vcpu *vcpu, | |
485 | unsigned long ea, unsigned long dsisr) | |
486 | { | |
487 | struct kvm *kvm = vcpu->kvm; | |
6f22bd32 | 488 | unsigned long hpte[3], r; |
abb7c7dd | 489 | unsigned long hnow_v, hnow_r; |
6f22bd32 | 490 | __be64 *hptep; |
342d3db7 | 491 | unsigned long mmu_seq, psize, pte_size; |
1066f772 | 492 | unsigned long gpa_base, gfn_base; |
70bddfef | 493 | unsigned long gpa, gfn, hva, pfn; |
697d3899 | 494 | struct kvm_memory_slot *memslot; |
342d3db7 | 495 | unsigned long *rmap; |
697d3899 | 496 | struct revmap_entry *rev; |
342d3db7 PM |
497 | struct page *page, *pages[1]; |
498 | long index, ret, npages; | |
30bda41a | 499 | bool is_ci; |
4cf302bc | 500 | unsigned int writing, write_ok; |
342d3db7 | 501 | struct vm_area_struct *vma; |
bad3b507 | 502 | unsigned long rcbits; |
a56ee9f8 | 503 | long mmio_update; |
697d3899 | 504 | |
5a319350 PM |
505 | if (kvm_is_radix(kvm)) |
506 | return kvmppc_book3s_radix_page_fault(run, vcpu, ea, dsisr); | |
507 | ||
697d3899 PM |
508 | /* |
509 | * Real-mode code has already searched the HPT and found the | |
510 | * entry we're interested in. Lock the entry and check that | |
511 | * it hasn't changed. If it has, just return and re-execute the | |
512 | * instruction. | |
513 | */ | |
514 | if (ea != vcpu->arch.pgfault_addr) | |
515 | return RESUME_GUEST; | |
a56ee9f8 YX |
516 | |
517 | if (vcpu->arch.pgfault_cache) { | |
518 | mmio_update = atomic64_read(&kvm->arch.mmio_update); | |
519 | if (mmio_update == vcpu->arch.pgfault_cache->mmio_update) { | |
520 | r = vcpu->arch.pgfault_cache->rpte; | |
8dc6cca5 PM |
521 | psize = kvmppc_actual_pgsz(vcpu->arch.pgfault_hpte[0], |
522 | r); | |
a56ee9f8 YX |
523 | gpa_base = r & HPTE_R_RPN & ~(psize - 1); |
524 | gfn_base = gpa_base >> PAGE_SHIFT; | |
525 | gpa = gpa_base | (ea & (psize - 1)); | |
526 | return kvmppc_hv_emulate_mmio(run, vcpu, gpa, ea, | |
527 | dsisr & DSISR_ISSTORE); | |
528 | } | |
529 | } | |
697d3899 | 530 | index = vcpu->arch.pgfault_index; |
3f9d4f5a DG |
531 | hptep = (__be64 *)(kvm->arch.hpt.virt + (index << 4)); |
532 | rev = &kvm->arch.hpt.rev[index]; | |
697d3899 PM |
533 | preempt_disable(); |
534 | while (!try_lock_hpte(hptep, HPTE_V_HVLOCK)) | |
535 | cpu_relax(); | |
6f22bd32 AG |
536 | hpte[0] = be64_to_cpu(hptep[0]) & ~HPTE_V_HVLOCK; |
537 | hpte[1] = be64_to_cpu(hptep[1]); | |
342d3db7 | 538 | hpte[2] = r = rev->guest_rpte; |
a4bd6eb0 | 539 | unlock_hpte(hptep, hpte[0]); |
697d3899 PM |
540 | preempt_enable(); |
541 | ||
abb7c7dd PM |
542 | if (cpu_has_feature(CPU_FTR_ARCH_300)) { |
543 | hpte[0] = hpte_new_to_old_v(hpte[0], hpte[1]); | |
544 | hpte[1] = hpte_new_to_old_r(hpte[1]); | |
545 | } | |
697d3899 PM |
546 | if (hpte[0] != vcpu->arch.pgfault_hpte[0] || |
547 | hpte[1] != vcpu->arch.pgfault_hpte[1]) | |
548 | return RESUME_GUEST; | |
549 | ||
550 | /* Translate the logical address and get the page */ | |
8dc6cca5 | 551 | psize = kvmppc_actual_pgsz(hpte[0], r); |
1066f772 PM |
552 | gpa_base = r & HPTE_R_RPN & ~(psize - 1); |
553 | gfn_base = gpa_base >> PAGE_SHIFT; | |
554 | gpa = gpa_base | (ea & (psize - 1)); | |
70bddfef | 555 | gfn = gpa >> PAGE_SHIFT; |
697d3899 PM |
556 | memslot = gfn_to_memslot(kvm, gfn); |
557 | ||
3c78f78a SW |
558 | trace_kvm_page_fault_enter(vcpu, hpte, memslot, ea, dsisr); |
559 | ||
697d3899 | 560 | /* No memslot means it's an emulated MMIO region */ |
70bddfef | 561 | if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID)) |
6020c0f6 | 562 | return kvmppc_hv_emulate_mmio(run, vcpu, gpa, ea, |
697d3899 | 563 | dsisr & DSISR_ISSTORE); |
697d3899 | 564 | |
1066f772 PM |
565 | /* |
566 | * This should never happen, because of the slot_is_aligned() | |
567 | * check in kvmppc_do_h_enter(). | |
568 | */ | |
569 | if (gfn_base < memslot->base_gfn) | |
570 | return -EFAULT; | |
571 | ||
342d3db7 PM |
572 | /* used to check for invalidations in progress */ |
573 | mmu_seq = kvm->mmu_notifier_seq; | |
574 | smp_rmb(); | |
575 | ||
3c78f78a | 576 | ret = -EFAULT; |
30bda41a | 577 | is_ci = false; |
342d3db7 PM |
578 | pfn = 0; |
579 | page = NULL; | |
580 | pte_size = PAGE_SIZE; | |
4cf302bc PM |
581 | writing = (dsisr & DSISR_ISSTORE) != 0; |
582 | /* If writing != 0, then the HPTE must allow writing, if we get here */ | |
583 | write_ok = writing; | |
342d3db7 | 584 | hva = gfn_to_hva_memslot(memslot, gfn); |
4cf302bc | 585 | npages = get_user_pages_fast(hva, 1, writing, pages); |
342d3db7 PM |
586 | if (npages < 1) { |
587 | /* Check if it's an I/O mapping */ | |
588 | down_read(¤t->mm->mmap_sem); | |
589 | vma = find_vma(current->mm, hva); | |
590 | if (vma && vma->vm_start <= hva && hva + psize <= vma->vm_end && | |
591 | (vma->vm_flags & VM_PFNMAP)) { | |
592 | pfn = vma->vm_pgoff + | |
593 | ((hva - vma->vm_start) >> PAGE_SHIFT); | |
594 | pte_size = psize; | |
30bda41a | 595 | is_ci = pte_ci(__pte((pgprot_val(vma->vm_page_prot)))); |
4cf302bc | 596 | write_ok = vma->vm_flags & VM_WRITE; |
342d3db7 PM |
597 | } |
598 | up_read(¤t->mm->mmap_sem); | |
599 | if (!pfn) | |
3c78f78a | 600 | goto out_put; |
342d3db7 PM |
601 | } else { |
602 | page = pages[0]; | |
caaa4c80 | 603 | pfn = page_to_pfn(page); |
342d3db7 PM |
604 | if (PageHuge(page)) { |
605 | page = compound_head(page); | |
606 | pte_size <<= compound_order(page); | |
607 | } | |
4cf302bc PM |
608 | /* if the guest wants write access, see if that is OK */ |
609 | if (!writing && hpte_is_writable(r)) { | |
610 | pte_t *ptep, pte; | |
691e95fd | 611 | unsigned long flags; |
4cf302bc PM |
612 | /* |
613 | * We need to protect against page table destruction | |
7d6e7f7f | 614 | * hugepage split and collapse. |
4cf302bc | 615 | */ |
691e95fd | 616 | local_irq_save(flags); |
94171b19 AK |
617 | ptep = find_current_mm_pte(current->mm->pgd, |
618 | hva, NULL, NULL); | |
db7cb5b9 | 619 | if (ptep) { |
7d6e7f7f | 620 | pte = kvmppc_read_update_linux_pte(ptep, 1); |
d19469e8 | 621 | if (__pte_write(pte)) |
4cf302bc PM |
622 | write_ok = 1; |
623 | } | |
691e95fd | 624 | local_irq_restore(flags); |
4cf302bc | 625 | } |
342d3db7 PM |
626 | } |
627 | ||
342d3db7 PM |
628 | if (psize > pte_size) |
629 | goto out_put; | |
630 | ||
631 | /* Check WIMG vs. the actual page we're accessing */ | |
30bda41a AK |
632 | if (!hpte_cache_flags_ok(r, is_ci)) { |
633 | if (is_ci) | |
3c78f78a | 634 | goto out_put; |
342d3db7 PM |
635 | /* |
636 | * Allow guest to map emulated device memory as | |
637 | * uncacheable, but actually make it cacheable. | |
638 | */ | |
639 | r = (r & ~(HPTE_R_W|HPTE_R_I|HPTE_R_G)) | HPTE_R_M; | |
640 | } | |
641 | ||
caaa4c80 PM |
642 | /* |
643 | * Set the HPTE to point to pfn. | |
644 | * Since the pfn is at PAGE_SIZE granularity, make sure we | |
645 | * don't mask out lower-order bits if psize < PAGE_SIZE. | |
646 | */ | |
647 | if (psize < PAGE_SIZE) | |
648 | psize = PAGE_SIZE; | |
f0585982 YX |
649 | r = (r & HPTE_R_KEY_HI) | (r & ~(HPTE_R_PP0 - psize)) | |
650 | ((pfn << PAGE_SHIFT) & ~(psize - 1)); | |
4cf302bc PM |
651 | if (hpte_is_writable(r) && !write_ok) |
652 | r = hpte_make_readonly(r); | |
342d3db7 PM |
653 | ret = RESUME_GUEST; |
654 | preempt_disable(); | |
655 | while (!try_lock_hpte(hptep, HPTE_V_HVLOCK)) | |
656 | cpu_relax(); | |
abb7c7dd PM |
657 | hnow_v = be64_to_cpu(hptep[0]); |
658 | hnow_r = be64_to_cpu(hptep[1]); | |
659 | if (cpu_has_feature(CPU_FTR_ARCH_300)) { | |
660 | hnow_v = hpte_new_to_old_v(hnow_v, hnow_r); | |
661 | hnow_r = hpte_new_to_old_r(hnow_r); | |
662 | } | |
38c53af8 PM |
663 | |
664 | /* | |
665 | * If the HPT is being resized, don't update the HPTE, | |
666 | * instead let the guest retry after the resize operation is complete. | |
072df813 | 667 | * The synchronization for mmu_ready test vs. set is provided |
38c53af8 PM |
668 | * by the HPTE lock. |
669 | */ | |
072df813 | 670 | if (!kvm->arch.mmu_ready) |
38c53af8 PM |
671 | goto out_unlock; |
672 | ||
abb7c7dd PM |
673 | if ((hnow_v & ~HPTE_V_HVLOCK) != hpte[0] || hnow_r != hpte[1] || |
674 | rev->guest_rpte != hpte[2]) | |
342d3db7 PM |
675 | /* HPTE has been changed under us; let the guest retry */ |
676 | goto out_unlock; | |
677 | hpte[0] = (hpte[0] & ~HPTE_V_ABSENT) | HPTE_V_VALID; | |
678 | ||
1066f772 PM |
679 | /* Always put the HPTE in the rmap chain for the page base address */ |
680 | rmap = &memslot->arch.rmap[gfn_base - memslot->base_gfn]; | |
342d3db7 PM |
681 | lock_rmap(rmap); |
682 | ||
683 | /* Check if we might have been invalidated; let the guest retry if so */ | |
684 | ret = RESUME_GUEST; | |
8ca40a70 | 685 | if (mmu_notifier_retry(vcpu->kvm, mmu_seq)) { |
342d3db7 PM |
686 | unlock_rmap(rmap); |
687 | goto out_unlock; | |
688 | } | |
4cf302bc | 689 | |
bad3b507 PM |
690 | /* Only set R/C in real HPTE if set in both *rmap and guest_rpte */ |
691 | rcbits = *rmap >> KVMPPC_RMAP_RC_SHIFT; | |
692 | r &= rcbits | ~(HPTE_R_R | HPTE_R_C); | |
693 | ||
6f22bd32 | 694 | if (be64_to_cpu(hptep[0]) & HPTE_V_VALID) { |
4cf302bc PM |
695 | /* HPTE was previously valid, so we need to invalidate it */ |
696 | unlock_rmap(rmap); | |
6f22bd32 | 697 | hptep[0] |= cpu_to_be64(HPTE_V_ABSENT); |
4cf302bc | 698 | kvmppc_invalidate_hpte(kvm, hptep, index); |
bad3b507 | 699 | /* don't lose previous R and C bits */ |
6f22bd32 | 700 | r |= be64_to_cpu(hptep[1]) & (HPTE_R_R | HPTE_R_C); |
4cf302bc PM |
701 | } else { |
702 | kvmppc_add_revmap_chain(kvm, rev, rmap, index, 0); | |
703 | } | |
342d3db7 | 704 | |
abb7c7dd PM |
705 | if (cpu_has_feature(CPU_FTR_ARCH_300)) { |
706 | r = hpte_old_to_new_r(hpte[0], r); | |
707 | hpte[0] = hpte_old_to_new_v(hpte[0]); | |
708 | } | |
6f22bd32 | 709 | hptep[1] = cpu_to_be64(r); |
342d3db7 | 710 | eieio(); |
a4bd6eb0 | 711 | __unlock_hpte(hptep, hpte[0]); |
342d3db7 PM |
712 | asm volatile("ptesync" : : : "memory"); |
713 | preempt_enable(); | |
4cf302bc | 714 | if (page && hpte_is_writable(r)) |
342d3db7 PM |
715 | SetPageDirty(page); |
716 | ||
717 | out_put: | |
3c78f78a SW |
718 | trace_kvm_page_fault_exit(vcpu, hpte, ret); |
719 | ||
de6c0b02 DG |
720 | if (page) { |
721 | /* | |
722 | * We drop pages[0] here, not page because page might | |
723 | * have been set to the head page of a compound, but | |
724 | * we have to drop the reference on the correct tail | |
725 | * page to match the get inside gup() | |
726 | */ | |
727 | put_page(pages[0]); | |
728 | } | |
342d3db7 PM |
729 | return ret; |
730 | ||
731 | out_unlock: | |
a4bd6eb0 | 732 | __unlock_hpte(hptep, be64_to_cpu(hptep[0])); |
342d3db7 PM |
733 | preempt_enable(); |
734 | goto out_put; | |
735 | } | |
736 | ||
18c3640c | 737 | void kvmppc_rmap_reset(struct kvm *kvm) |
a64fd707 PM |
738 | { |
739 | struct kvm_memslots *slots; | |
740 | struct kvm_memory_slot *memslot; | |
741 | int srcu_idx; | |
742 | ||
743 | srcu_idx = srcu_read_lock(&kvm->srcu); | |
9f6b8029 | 744 | slots = kvm_memslots(kvm); |
a64fd707 | 745 | kvm_for_each_memslot(memslot, slots) { |
234ff0b7 PM |
746 | /* Mutual exclusion with kvm_unmap_hva_range etc. */ |
747 | spin_lock(&kvm->mmu_lock); | |
a64fd707 PM |
748 | /* |
749 | * This assumes it is acceptable to lose reference and | |
750 | * change bits across a reset. | |
751 | */ | |
752 | memset(memslot->arch.rmap, 0, | |
753 | memslot->npages * sizeof(*memslot->arch.rmap)); | |
234ff0b7 | 754 | spin_unlock(&kvm->mmu_lock); |
a64fd707 PM |
755 | } |
756 | srcu_read_unlock(&kvm->srcu, srcu_idx); | |
757 | } | |
758 | ||
01756099 PM |
759 | typedef int (*hva_handler_fn)(struct kvm *kvm, struct kvm_memory_slot *memslot, |
760 | unsigned long gfn); | |
761 | ||
84504ef3 TY |
762 | static int kvm_handle_hva_range(struct kvm *kvm, |
763 | unsigned long start, | |
764 | unsigned long end, | |
01756099 | 765 | hva_handler_fn handler) |
342d3db7 PM |
766 | { |
767 | int ret; | |
768 | int retval = 0; | |
769 | struct kvm_memslots *slots; | |
770 | struct kvm_memory_slot *memslot; | |
771 | ||
772 | slots = kvm_memslots(kvm); | |
773 | kvm_for_each_memslot(memslot, slots) { | |
84504ef3 TY |
774 | unsigned long hva_start, hva_end; |
775 | gfn_t gfn, gfn_end; | |
776 | ||
777 | hva_start = max(start, memslot->userspace_addr); | |
778 | hva_end = min(end, memslot->userspace_addr + | |
779 | (memslot->npages << PAGE_SHIFT)); | |
780 | if (hva_start >= hva_end) | |
781 | continue; | |
782 | /* | |
783 | * {gfn(page) | page intersects with [hva_start, hva_end)} = | |
784 | * {gfn, gfn+1, ..., gfn_end-1}. | |
785 | */ | |
786 | gfn = hva_to_gfn_memslot(hva_start, memslot); | |
787 | gfn_end = hva_to_gfn_memslot(hva_end + PAGE_SIZE - 1, memslot); | |
342d3db7 | 788 | |
84504ef3 | 789 | for (; gfn < gfn_end; ++gfn) { |
01756099 | 790 | ret = handler(kvm, memslot, gfn); |
342d3db7 PM |
791 | retval |= ret; |
792 | } | |
793 | } | |
794 | ||
795 | return retval; | |
796 | } | |
797 | ||
84504ef3 | 798 | static int kvm_handle_hva(struct kvm *kvm, unsigned long hva, |
01756099 | 799 | hva_handler_fn handler) |
84504ef3 TY |
800 | { |
801 | return kvm_handle_hva_range(kvm, hva, hva + 1, handler); | |
802 | } | |
803 | ||
639e4597 DG |
804 | /* Must be called with both HPTE and rmap locked */ |
805 | static void kvmppc_unmap_hpte(struct kvm *kvm, unsigned long i, | |
e641a317 | 806 | struct kvm_memory_slot *memslot, |
639e4597 DG |
807 | unsigned long *rmapp, unsigned long gfn) |
808 | { | |
809 | __be64 *hptep = (__be64 *) (kvm->arch.hpt.virt + (i << 4)); | |
810 | struct revmap_entry *rev = kvm->arch.hpt.rev; | |
811 | unsigned long j, h; | |
812 | unsigned long ptel, psize, rcbits; | |
813 | ||
814 | j = rev[i].forw; | |
815 | if (j == i) { | |
816 | /* chain is now empty */ | |
817 | *rmapp &= ~(KVMPPC_RMAP_PRESENT | KVMPPC_RMAP_INDEX); | |
818 | } else { | |
819 | /* remove i from chain */ | |
820 | h = rev[i].back; | |
821 | rev[h].forw = j; | |
822 | rev[j].back = h; | |
823 | rev[i].forw = rev[i].back = i; | |
824 | *rmapp = (*rmapp & ~KVMPPC_RMAP_INDEX) | j; | |
825 | } | |
826 | ||
827 | /* Now check and modify the HPTE */ | |
828 | ptel = rev[i].guest_rpte; | |
8dc6cca5 | 829 | psize = kvmppc_actual_pgsz(be64_to_cpu(hptep[0]), ptel); |
639e4597 DG |
830 | if ((be64_to_cpu(hptep[0]) & HPTE_V_VALID) && |
831 | hpte_rpn(ptel, psize) == gfn) { | |
832 | hptep[0] |= cpu_to_be64(HPTE_V_ABSENT); | |
833 | kvmppc_invalidate_hpte(kvm, hptep, i); | |
834 | hptep[1] &= ~cpu_to_be64(HPTE_R_KEY_HI | HPTE_R_KEY_LO); | |
835 | /* Harvest R and C */ | |
836 | rcbits = be64_to_cpu(hptep[1]) & (HPTE_R_R | HPTE_R_C); | |
837 | *rmapp |= rcbits << KVMPPC_RMAP_RC_SHIFT; | |
e641a317 PM |
838 | if ((rcbits & HPTE_R_C) && memslot->dirty_bitmap) |
839 | kvmppc_update_dirty_map(memslot, gfn, psize); | |
639e4597 DG |
840 | if (rcbits & ~rev[i].guest_rpte) { |
841 | rev[i].guest_rpte = ptel | rcbits; | |
842 | note_hpte_modification(kvm, &rev[i]); | |
843 | } | |
844 | } | |
845 | } | |
846 | ||
01756099 | 847 | static int kvm_unmap_rmapp(struct kvm *kvm, struct kvm_memory_slot *memslot, |
342d3db7 PM |
848 | unsigned long gfn) |
849 | { | |
639e4597 | 850 | unsigned long i; |
6f22bd32 | 851 | __be64 *hptep; |
01756099 | 852 | unsigned long *rmapp; |
342d3db7 | 853 | |
01756099 | 854 | rmapp = &memslot->arch.rmap[gfn - memslot->base_gfn]; |
342d3db7 | 855 | for (;;) { |
bad3b507 | 856 | lock_rmap(rmapp); |
342d3db7 | 857 | if (!(*rmapp & KVMPPC_RMAP_PRESENT)) { |
bad3b507 | 858 | unlock_rmap(rmapp); |
342d3db7 PM |
859 | break; |
860 | } | |
861 | ||
862 | /* | |
863 | * To avoid an ABBA deadlock with the HPTE lock bit, | |
bad3b507 PM |
864 | * we can't spin on the HPTE lock while holding the |
865 | * rmap chain lock. | |
342d3db7 PM |
866 | */ |
867 | i = *rmapp & KVMPPC_RMAP_INDEX; | |
3f9d4f5a | 868 | hptep = (__be64 *) (kvm->arch.hpt.virt + (i << 4)); |
bad3b507 PM |
869 | if (!try_lock_hpte(hptep, HPTE_V_HVLOCK)) { |
870 | /* unlock rmap before spinning on the HPTE lock */ | |
871 | unlock_rmap(rmapp); | |
6f22bd32 | 872 | while (be64_to_cpu(hptep[0]) & HPTE_V_HVLOCK) |
bad3b507 PM |
873 | cpu_relax(); |
874 | continue; | |
875 | } | |
342d3db7 | 876 | |
e641a317 | 877 | kvmppc_unmap_hpte(kvm, i, memslot, rmapp, gfn); |
bad3b507 | 878 | unlock_rmap(rmapp); |
a4bd6eb0 | 879 | __unlock_hpte(hptep, be64_to_cpu(hptep[0])); |
342d3db7 PM |
880 | } |
881 | return 0; | |
882 | } | |
883 | ||
3a167bea | 884 | int kvm_unmap_hva_range_hv(struct kvm *kvm, unsigned long start, unsigned long end) |
b3ae2096 | 885 | { |
01756099 PM |
886 | hva_handler_fn handler; |
887 | ||
888 | handler = kvm_is_radix(kvm) ? kvm_unmap_radix : kvm_unmap_rmapp; | |
889 | kvm_handle_hva_range(kvm, start, end, handler); | |
b3ae2096 TY |
890 | return 0; |
891 | } | |
892 | ||
3a167bea AK |
893 | void kvmppc_core_flush_memslot_hv(struct kvm *kvm, |
894 | struct kvm_memory_slot *memslot) | |
dfe49dbd | 895 | { |
dfe49dbd PM |
896 | unsigned long gfn; |
897 | unsigned long n; | |
01756099 | 898 | unsigned long *rmapp; |
dfe49dbd | 899 | |
dfe49dbd | 900 | gfn = memslot->base_gfn; |
01756099 | 901 | rmapp = memslot->arch.rmap; |
5af3e9d0 PM |
902 | if (kvm_is_radix(kvm)) { |
903 | kvmppc_radix_flush_memslot(kvm, memslot); | |
904 | return; | |
905 | } | |
906 | ||
01756099 | 907 | for (n = memslot->npages; n; --n, ++gfn) { |
dfe49dbd PM |
908 | /* |
909 | * Testing the present bit without locking is OK because | |
910 | * the memslot has been marked invalid already, and hence | |
911 | * no new HPTEs referencing this page can be created, | |
912 | * thus the present bit can't go from 0 to 1. | |
913 | */ | |
914 | if (*rmapp & KVMPPC_RMAP_PRESENT) | |
01756099 | 915 | kvm_unmap_rmapp(kvm, memslot, gfn); |
dfe49dbd | 916 | ++rmapp; |
dfe49dbd PM |
917 | } |
918 | } | |
919 | ||
01756099 | 920 | static int kvm_age_rmapp(struct kvm *kvm, struct kvm_memory_slot *memslot, |
342d3db7 PM |
921 | unsigned long gfn) |
922 | { | |
3f9d4f5a | 923 | struct revmap_entry *rev = kvm->arch.hpt.rev; |
55514893 | 924 | unsigned long head, i, j; |
6f22bd32 | 925 | __be64 *hptep; |
55514893 | 926 | int ret = 0; |
01756099 | 927 | unsigned long *rmapp; |
55514893 | 928 | |
01756099 | 929 | rmapp = &memslot->arch.rmap[gfn - memslot->base_gfn]; |
55514893 PM |
930 | retry: |
931 | lock_rmap(rmapp); | |
932 | if (*rmapp & KVMPPC_RMAP_REFERENCED) { | |
933 | *rmapp &= ~KVMPPC_RMAP_REFERENCED; | |
934 | ret = 1; | |
935 | } | |
936 | if (!(*rmapp & KVMPPC_RMAP_PRESENT)) { | |
937 | unlock_rmap(rmapp); | |
938 | return ret; | |
939 | } | |
940 | ||
941 | i = head = *rmapp & KVMPPC_RMAP_INDEX; | |
942 | do { | |
3f9d4f5a | 943 | hptep = (__be64 *) (kvm->arch.hpt.virt + (i << 4)); |
55514893 PM |
944 | j = rev[i].forw; |
945 | ||
946 | /* If this HPTE isn't referenced, ignore it */ | |
6f22bd32 | 947 | if (!(be64_to_cpu(hptep[1]) & HPTE_R_R)) |
55514893 PM |
948 | continue; |
949 | ||
950 | if (!try_lock_hpte(hptep, HPTE_V_HVLOCK)) { | |
951 | /* unlock rmap before spinning on the HPTE lock */ | |
952 | unlock_rmap(rmapp); | |
6f22bd32 | 953 | while (be64_to_cpu(hptep[0]) & HPTE_V_HVLOCK) |
55514893 PM |
954 | cpu_relax(); |
955 | goto retry; | |
956 | } | |
957 | ||
958 | /* Now check and modify the HPTE */ | |
6f22bd32 AG |
959 | if ((be64_to_cpu(hptep[0]) & HPTE_V_VALID) && |
960 | (be64_to_cpu(hptep[1]) & HPTE_R_R)) { | |
55514893 | 961 | kvmppc_clear_ref_hpte(kvm, hptep, i); |
a1b4a0f6 PM |
962 | if (!(rev[i].guest_rpte & HPTE_R_R)) { |
963 | rev[i].guest_rpte |= HPTE_R_R; | |
964 | note_hpte_modification(kvm, &rev[i]); | |
965 | } | |
55514893 PM |
966 | ret = 1; |
967 | } | |
a4bd6eb0 | 968 | __unlock_hpte(hptep, be64_to_cpu(hptep[0])); |
55514893 PM |
969 | } while ((i = j) != head); |
970 | ||
971 | unlock_rmap(rmapp); | |
972 | return ret; | |
342d3db7 PM |
973 | } |
974 | ||
57128468 | 975 | int kvm_age_hva_hv(struct kvm *kvm, unsigned long start, unsigned long end) |
342d3db7 | 976 | { |
01756099 PM |
977 | hva_handler_fn handler; |
978 | ||
979 | handler = kvm_is_radix(kvm) ? kvm_age_radix : kvm_age_rmapp; | |
980 | return kvm_handle_hva_range(kvm, start, end, handler); | |
342d3db7 PM |
981 | } |
982 | ||
01756099 | 983 | static int kvm_test_age_rmapp(struct kvm *kvm, struct kvm_memory_slot *memslot, |
342d3db7 PM |
984 | unsigned long gfn) |
985 | { | |
3f9d4f5a | 986 | struct revmap_entry *rev = kvm->arch.hpt.rev; |
55514893 PM |
987 | unsigned long head, i, j; |
988 | unsigned long *hp; | |
989 | int ret = 1; | |
01756099 | 990 | unsigned long *rmapp; |
55514893 | 991 | |
01756099 | 992 | rmapp = &memslot->arch.rmap[gfn - memslot->base_gfn]; |
55514893 PM |
993 | if (*rmapp & KVMPPC_RMAP_REFERENCED) |
994 | return 1; | |
995 | ||
996 | lock_rmap(rmapp); | |
997 | if (*rmapp & KVMPPC_RMAP_REFERENCED) | |
998 | goto out; | |
999 | ||
1000 | if (*rmapp & KVMPPC_RMAP_PRESENT) { | |
1001 | i = head = *rmapp & KVMPPC_RMAP_INDEX; | |
1002 | do { | |
3f9d4f5a | 1003 | hp = (unsigned long *)(kvm->arch.hpt.virt + (i << 4)); |
55514893 | 1004 | j = rev[i].forw; |
6f22bd32 | 1005 | if (be64_to_cpu(hp[1]) & HPTE_R_R) |
55514893 PM |
1006 | goto out; |
1007 | } while ((i = j) != head); | |
1008 | } | |
1009 | ret = 0; | |
1010 | ||
1011 | out: | |
1012 | unlock_rmap(rmapp); | |
1013 | return ret; | |
342d3db7 PM |
1014 | } |
1015 | ||
3a167bea | 1016 | int kvm_test_age_hva_hv(struct kvm *kvm, unsigned long hva) |
342d3db7 | 1017 | { |
01756099 PM |
1018 | hva_handler_fn handler; |
1019 | ||
1020 | handler = kvm_is_radix(kvm) ? kvm_test_age_radix : kvm_test_age_rmapp; | |
1021 | return kvm_handle_hva(kvm, hva, handler); | |
342d3db7 PM |
1022 | } |
1023 | ||
3a167bea | 1024 | void kvm_set_spte_hva_hv(struct kvm *kvm, unsigned long hva, pte_t pte) |
342d3db7 | 1025 | { |
01756099 PM |
1026 | hva_handler_fn handler; |
1027 | ||
1028 | handler = kvm_is_radix(kvm) ? kvm_unmap_radix : kvm_unmap_rmapp; | |
1029 | kvm_handle_hva(kvm, hva, handler); | |
de56a948 PM |
1030 | } |
1031 | ||
6c576e74 PM |
1032 | static int vcpus_running(struct kvm *kvm) |
1033 | { | |
1034 | return atomic_read(&kvm->arch.vcpus_running) != 0; | |
1035 | } | |
1036 | ||
687414be AK |
1037 | /* |
1038 | * Returns the number of system pages that are dirty. | |
1039 | * This can be more than 1 if we find a huge-page HPTE. | |
1040 | */ | |
1041 | static int kvm_test_clear_dirty_npages(struct kvm *kvm, unsigned long *rmapp) | |
82ed3616 | 1042 | { |
3f9d4f5a | 1043 | struct revmap_entry *rev = kvm->arch.hpt.rev; |
82ed3616 | 1044 | unsigned long head, i, j; |
687414be | 1045 | unsigned long n; |
6c576e74 | 1046 | unsigned long v, r; |
6f22bd32 | 1047 | __be64 *hptep; |
687414be | 1048 | int npages_dirty = 0; |
82ed3616 PM |
1049 | |
1050 | retry: | |
1051 | lock_rmap(rmapp); | |
82ed3616 PM |
1052 | if (!(*rmapp & KVMPPC_RMAP_PRESENT)) { |
1053 | unlock_rmap(rmapp); | |
687414be | 1054 | return npages_dirty; |
82ed3616 PM |
1055 | } |
1056 | ||
1057 | i = head = *rmapp & KVMPPC_RMAP_INDEX; | |
1058 | do { | |
6f22bd32 | 1059 | unsigned long hptep1; |
3f9d4f5a | 1060 | hptep = (__be64 *) (kvm->arch.hpt.virt + (i << 4)); |
82ed3616 PM |
1061 | j = rev[i].forw; |
1062 | ||
6c576e74 PM |
1063 | /* |
1064 | * Checking the C (changed) bit here is racy since there | |
1065 | * is no guarantee about when the hardware writes it back. | |
1066 | * If the HPTE is not writable then it is stable since the | |
1067 | * page can't be written to, and we would have done a tlbie | |
1068 | * (which forces the hardware to complete any writeback) | |
1069 | * when making the HPTE read-only. | |
1070 | * If vcpus are running then this call is racy anyway | |
1071 | * since the page could get dirtied subsequently, so we | |
1072 | * expect there to be a further call which would pick up | |
1073 | * any delayed C bit writeback. | |
1074 | * Otherwise we need to do the tlbie even if C==0 in | |
1075 | * order to pick up any delayed writeback of C. | |
1076 | */ | |
6f22bd32 AG |
1077 | hptep1 = be64_to_cpu(hptep[1]); |
1078 | if (!(hptep1 & HPTE_R_C) && | |
1079 | (!hpte_is_writable(hptep1) || vcpus_running(kvm))) | |
82ed3616 PM |
1080 | continue; |
1081 | ||
1082 | if (!try_lock_hpte(hptep, HPTE_V_HVLOCK)) { | |
1083 | /* unlock rmap before spinning on the HPTE lock */ | |
1084 | unlock_rmap(rmapp); | |
6f22bd32 | 1085 | while (hptep[0] & cpu_to_be64(HPTE_V_HVLOCK)) |
82ed3616 PM |
1086 | cpu_relax(); |
1087 | goto retry; | |
1088 | } | |
1089 | ||
1090 | /* Now check and modify the HPTE */ | |
f6fb9e84 | 1091 | if (!(hptep[0] & cpu_to_be64(HPTE_V_VALID))) { |
a4bd6eb0 | 1092 | __unlock_hpte(hptep, be64_to_cpu(hptep[0])); |
6c576e74 | 1093 | continue; |
f6fb9e84 | 1094 | } |
6c576e74 PM |
1095 | |
1096 | /* need to make it temporarily absent so C is stable */ | |
6f22bd32 | 1097 | hptep[0] |= cpu_to_be64(HPTE_V_ABSENT); |
6c576e74 | 1098 | kvmppc_invalidate_hpte(kvm, hptep, i); |
6f22bd32 AG |
1099 | v = be64_to_cpu(hptep[0]); |
1100 | r = be64_to_cpu(hptep[1]); | |
6c576e74 | 1101 | if (r & HPTE_R_C) { |
6f22bd32 | 1102 | hptep[1] = cpu_to_be64(r & ~HPTE_R_C); |
a1b4a0f6 PM |
1103 | if (!(rev[i].guest_rpte & HPTE_R_C)) { |
1104 | rev[i].guest_rpte |= HPTE_R_C; | |
1105 | note_hpte_modification(kvm, &rev[i]); | |
1106 | } | |
8dc6cca5 | 1107 | n = kvmppc_actual_pgsz(v, r); |
687414be AK |
1108 | n = (n + PAGE_SIZE - 1) >> PAGE_SHIFT; |
1109 | if (n > npages_dirty) | |
1110 | npages_dirty = n; | |
6c576e74 | 1111 | eieio(); |
82ed3616 | 1112 | } |
a4bd6eb0 | 1113 | v &= ~HPTE_V_ABSENT; |
6c576e74 | 1114 | v |= HPTE_V_VALID; |
a4bd6eb0 | 1115 | __unlock_hpte(hptep, v); |
82ed3616 PM |
1116 | } while ((i = j) != head); |
1117 | ||
1118 | unlock_rmap(rmapp); | |
687414be | 1119 | return npages_dirty; |
82ed3616 PM |
1120 | } |
1121 | ||
8f7b79b8 | 1122 | void kvmppc_harvest_vpa_dirty(struct kvmppc_vpa *vpa, |
c35635ef PM |
1123 | struct kvm_memory_slot *memslot, |
1124 | unsigned long *map) | |
1125 | { | |
1126 | unsigned long gfn; | |
1127 | ||
1128 | if (!vpa->dirty || !vpa->pinned_addr) | |
1129 | return; | |
1130 | gfn = vpa->gpa >> PAGE_SHIFT; | |
1131 | if (gfn < memslot->base_gfn || | |
1132 | gfn >= memslot->base_gfn + memslot->npages) | |
1133 | return; | |
1134 | ||
1135 | vpa->dirty = false; | |
1136 | if (map) | |
1137 | __set_bit_le(gfn - memslot->base_gfn, map); | |
1138 | } | |
1139 | ||
8f7b79b8 PM |
1140 | long kvmppc_hv_get_dirty_log_hpt(struct kvm *kvm, |
1141 | struct kvm_memory_slot *memslot, unsigned long *map) | |
82ed3616 | 1142 | { |
e641a317 | 1143 | unsigned long i; |
dfe49dbd | 1144 | unsigned long *rmapp; |
82ed3616 PM |
1145 | |
1146 | preempt_disable(); | |
d89cc617 | 1147 | rmapp = memslot->arch.rmap; |
82ed3616 | 1148 | for (i = 0; i < memslot->npages; ++i) { |
687414be AK |
1149 | int npages = kvm_test_clear_dirty_npages(kvm, rmapp); |
1150 | /* | |
1151 | * Note that if npages > 0 then i must be a multiple of npages, | |
1152 | * since we always put huge-page HPTEs in the rmap chain | |
1153 | * corresponding to their page base address. | |
1154 | */ | |
e641a317 PM |
1155 | if (npages) |
1156 | set_dirty_bits(map, i, npages); | |
82ed3616 PM |
1157 | ++rmapp; |
1158 | } | |
1159 | preempt_enable(); | |
1160 | return 0; | |
1161 | } | |
1162 | ||
93e60249 PM |
1163 | void *kvmppc_pin_guest_page(struct kvm *kvm, unsigned long gpa, |
1164 | unsigned long *nb_ret) | |
1165 | { | |
1166 | struct kvm_memory_slot *memslot; | |
1167 | unsigned long gfn = gpa >> PAGE_SHIFT; | |
342d3db7 PM |
1168 | struct page *page, *pages[1]; |
1169 | int npages; | |
c35635ef | 1170 | unsigned long hva, offset; |
2c9097e4 | 1171 | int srcu_idx; |
93e60249 | 1172 | |
2c9097e4 | 1173 | srcu_idx = srcu_read_lock(&kvm->srcu); |
93e60249 PM |
1174 | memslot = gfn_to_memslot(kvm, gfn); |
1175 | if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID)) | |
2c9097e4 | 1176 | goto err; |
c17b98cf PM |
1177 | hva = gfn_to_hva_memslot(memslot, gfn); |
1178 | npages = get_user_pages_fast(hva, 1, 1, pages); | |
1179 | if (npages < 1) | |
1180 | goto err; | |
1181 | page = pages[0]; | |
2c9097e4 PM |
1182 | srcu_read_unlock(&kvm->srcu, srcu_idx); |
1183 | ||
c35635ef | 1184 | offset = gpa & (PAGE_SIZE - 1); |
93e60249 | 1185 | if (nb_ret) |
c35635ef | 1186 | *nb_ret = PAGE_SIZE - offset; |
93e60249 | 1187 | return page_address(page) + offset; |
2c9097e4 PM |
1188 | |
1189 | err: | |
1190 | srcu_read_unlock(&kvm->srcu, srcu_idx); | |
1191 | return NULL; | |
93e60249 PM |
1192 | } |
1193 | ||
c35635ef PM |
1194 | void kvmppc_unpin_guest_page(struct kvm *kvm, void *va, unsigned long gpa, |
1195 | bool dirty) | |
93e60249 PM |
1196 | { |
1197 | struct page *page = virt_to_page(va); | |
c35635ef PM |
1198 | struct kvm_memory_slot *memslot; |
1199 | unsigned long gfn; | |
c35635ef | 1200 | int srcu_idx; |
93e60249 | 1201 | |
93e60249 | 1202 | put_page(page); |
c35635ef | 1203 | |
c17b98cf | 1204 | if (!dirty) |
c35635ef PM |
1205 | return; |
1206 | ||
e641a317 | 1207 | /* We need to mark this page dirty in the memslot dirty_bitmap, if any */ |
c35635ef PM |
1208 | gfn = gpa >> PAGE_SHIFT; |
1209 | srcu_idx = srcu_read_lock(&kvm->srcu); | |
1210 | memslot = gfn_to_memslot(kvm, gfn); | |
e641a317 PM |
1211 | if (memslot && memslot->dirty_bitmap) |
1212 | set_bit_le(gfn - memslot->base_gfn, memslot->dirty_bitmap); | |
c35635ef | 1213 | srcu_read_unlock(&kvm->srcu, srcu_idx); |
93e60249 PM |
1214 | } |
1215 | ||
5e985969 DG |
1216 | /* |
1217 | * HPT resizing | |
1218 | */ | |
1219 | static int resize_hpt_allocate(struct kvm_resize_hpt *resize) | |
1220 | { | |
b5baa687 DG |
1221 | int rc; |
1222 | ||
1223 | rc = kvmppc_allocate_hpt(&resize->hpt, resize->order); | |
1224 | if (rc < 0) | |
1225 | return rc; | |
1226 | ||
1227 | resize_hpt_debug(resize, "resize_hpt_allocate(): HPT @ 0x%lx\n", | |
1228 | resize->hpt.virt); | |
1229 | ||
5e985969 DG |
1230 | return 0; |
1231 | } | |
1232 | ||
b5baa687 DG |
1233 | static unsigned long resize_hpt_rehash_hpte(struct kvm_resize_hpt *resize, |
1234 | unsigned long idx) | |
1235 | { | |
1236 | struct kvm *kvm = resize->kvm; | |
1237 | struct kvm_hpt_info *old = &kvm->arch.hpt; | |
1238 | struct kvm_hpt_info *new = &resize->hpt; | |
1239 | unsigned long old_hash_mask = (1ULL << (old->order - 7)) - 1; | |
1240 | unsigned long new_hash_mask = (1ULL << (new->order - 7)) - 1; | |
1241 | __be64 *hptep, *new_hptep; | |
1242 | unsigned long vpte, rpte, guest_rpte; | |
1243 | int ret; | |
1244 | struct revmap_entry *rev; | |
ded13fc1 | 1245 | unsigned long apsize, avpn, pteg, hash; |
b5baa687 | 1246 | unsigned long new_idx, new_pteg, replace_vpte; |
ded13fc1 | 1247 | int pshift; |
b5baa687 DG |
1248 | |
1249 | hptep = (__be64 *)(old->virt + (idx << 4)); | |
1250 | ||
1251 | /* Guest is stopped, so new HPTEs can't be added or faulted | |
1252 | * in, only unmapped or altered by host actions. So, it's | |
1253 | * safe to check this before we take the HPTE lock */ | |
1254 | vpte = be64_to_cpu(hptep[0]); | |
1255 | if (!(vpte & HPTE_V_VALID) && !(vpte & HPTE_V_ABSENT)) | |
1256 | return 0; /* nothing to do */ | |
1257 | ||
1258 | while (!try_lock_hpte(hptep, HPTE_V_HVLOCK)) | |
1259 | cpu_relax(); | |
1260 | ||
1261 | vpte = be64_to_cpu(hptep[0]); | |
1262 | ||
1263 | ret = 0; | |
1264 | if (!(vpte & HPTE_V_VALID) && !(vpte & HPTE_V_ABSENT)) | |
1265 | /* Nothing to do */ | |
1266 | goto out; | |
1267 | ||
790a9df5 DG |
1268 | if (cpu_has_feature(CPU_FTR_ARCH_300)) { |
1269 | rpte = be64_to_cpu(hptep[1]); | |
1270 | vpte = hpte_new_to_old_v(vpte, rpte); | |
1271 | } | |
1272 | ||
b5baa687 DG |
1273 | /* Unmap */ |
1274 | rev = &old->rev[idx]; | |
1275 | guest_rpte = rev->guest_rpte; | |
1276 | ||
1277 | ret = -EIO; | |
8dc6cca5 | 1278 | apsize = kvmppc_actual_pgsz(vpte, guest_rpte); |
b5baa687 DG |
1279 | if (!apsize) |
1280 | goto out; | |
1281 | ||
1282 | if (vpte & HPTE_V_VALID) { | |
1283 | unsigned long gfn = hpte_rpn(guest_rpte, apsize); | |
1284 | int srcu_idx = srcu_read_lock(&kvm->srcu); | |
1285 | struct kvm_memory_slot *memslot = | |
1286 | __gfn_to_memslot(kvm_memslots(kvm), gfn); | |
1287 | ||
1288 | if (memslot) { | |
1289 | unsigned long *rmapp; | |
1290 | rmapp = &memslot->arch.rmap[gfn - memslot->base_gfn]; | |
1291 | ||
1292 | lock_rmap(rmapp); | |
e641a317 | 1293 | kvmppc_unmap_hpte(kvm, idx, memslot, rmapp, gfn); |
b5baa687 DG |
1294 | unlock_rmap(rmapp); |
1295 | } | |
1296 | ||
1297 | srcu_read_unlock(&kvm->srcu, srcu_idx); | |
1298 | } | |
1299 | ||
1300 | /* Reload PTE after unmap */ | |
1301 | vpte = be64_to_cpu(hptep[0]); | |
b5baa687 DG |
1302 | BUG_ON(vpte & HPTE_V_VALID); |
1303 | BUG_ON(!(vpte & HPTE_V_ABSENT)); | |
1304 | ||
1305 | ret = 0; | |
1306 | if (!(vpte & HPTE_V_BOLTED)) | |
1307 | goto out; | |
1308 | ||
1309 | rpte = be64_to_cpu(hptep[1]); | |
790a9df5 DG |
1310 | |
1311 | if (cpu_has_feature(CPU_FTR_ARCH_300)) { | |
1312 | vpte = hpte_new_to_old_v(vpte, rpte); | |
1313 | rpte = hpte_new_to_old_r(rpte); | |
1314 | } | |
1315 | ||
ded13fc1 PM |
1316 | pshift = kvmppc_hpte_base_page_shift(vpte, rpte); |
1317 | avpn = HPTE_V_AVPN_VAL(vpte) & ~(((1ul << pshift) - 1) >> 23); | |
b5baa687 DG |
1318 | pteg = idx / HPTES_PER_GROUP; |
1319 | if (vpte & HPTE_V_SECONDARY) | |
1320 | pteg = ~pteg; | |
1321 | ||
1322 | if (!(vpte & HPTE_V_1TB_SEG)) { | |
1323 | unsigned long offset, vsid; | |
1324 | ||
1325 | /* We only have 28 - 23 bits of offset in avpn */ | |
1326 | offset = (avpn & 0x1f) << 23; | |
1327 | vsid = avpn >> 5; | |
1328 | /* We can find more bits from the pteg value */ | |
ded13fc1 PM |
1329 | if (pshift < 23) |
1330 | offset |= ((vsid ^ pteg) & old_hash_mask) << pshift; | |
b5baa687 | 1331 | |
ded13fc1 | 1332 | hash = vsid ^ (offset >> pshift); |
b5baa687 DG |
1333 | } else { |
1334 | unsigned long offset, vsid; | |
1335 | ||
1336 | /* We only have 40 - 23 bits of seg_off in avpn */ | |
1337 | offset = (avpn & 0x1ffff) << 23; | |
1338 | vsid = avpn >> 17; | |
ded13fc1 PM |
1339 | if (pshift < 23) |
1340 | offset |= ((vsid ^ (vsid << 25) ^ pteg) & old_hash_mask) << pshift; | |
b5baa687 | 1341 | |
ded13fc1 | 1342 | hash = vsid ^ (vsid << 25) ^ (offset >> pshift); |
b5baa687 DG |
1343 | } |
1344 | ||
1345 | new_pteg = hash & new_hash_mask; | |
05f2bb03 PM |
1346 | if (vpte & HPTE_V_SECONDARY) |
1347 | new_pteg = ~hash & new_hash_mask; | |
b5baa687 DG |
1348 | |
1349 | new_idx = new_pteg * HPTES_PER_GROUP + (idx % HPTES_PER_GROUP); | |
1350 | new_hptep = (__be64 *)(new->virt + (new_idx << 4)); | |
1351 | ||
1352 | replace_vpte = be64_to_cpu(new_hptep[0]); | |
790a9df5 DG |
1353 | if (cpu_has_feature(CPU_FTR_ARCH_300)) { |
1354 | unsigned long replace_rpte = be64_to_cpu(new_hptep[1]); | |
1355 | replace_vpte = hpte_new_to_old_v(replace_vpte, replace_rpte); | |
1356 | } | |
b5baa687 DG |
1357 | |
1358 | if (replace_vpte & (HPTE_V_VALID | HPTE_V_ABSENT)) { | |
1359 | BUG_ON(new->order >= old->order); | |
1360 | ||
1361 | if (replace_vpte & HPTE_V_BOLTED) { | |
1362 | if (vpte & HPTE_V_BOLTED) | |
1363 | /* Bolted collision, nothing we can do */ | |
1364 | ret = -ENOSPC; | |
1365 | /* Discard the new HPTE */ | |
1366 | goto out; | |
1367 | } | |
1368 | ||
1369 | /* Discard the previous HPTE */ | |
1370 | } | |
1371 | ||
790a9df5 DG |
1372 | if (cpu_has_feature(CPU_FTR_ARCH_300)) { |
1373 | rpte = hpte_old_to_new_r(vpte, rpte); | |
1374 | vpte = hpte_old_to_new_v(vpte); | |
1375 | } | |
1376 | ||
b5baa687 DG |
1377 | new_hptep[1] = cpu_to_be64(rpte); |
1378 | new->rev[new_idx].guest_rpte = guest_rpte; | |
1379 | /* No need for a barrier, since new HPT isn't active */ | |
1380 | new_hptep[0] = cpu_to_be64(vpte); | |
1381 | unlock_hpte(new_hptep, vpte); | |
1382 | ||
1383 | out: | |
1384 | unlock_hpte(hptep, vpte); | |
1385 | return ret; | |
1386 | } | |
1387 | ||
5e985969 DG |
1388 | static int resize_hpt_rehash(struct kvm_resize_hpt *resize) |
1389 | { | |
b5baa687 DG |
1390 | struct kvm *kvm = resize->kvm; |
1391 | unsigned long i; | |
1392 | int rc; | |
1393 | ||
1394 | for (i = 0; i < kvmppc_hpt_npte(&kvm->arch.hpt); i++) { | |
1395 | rc = resize_hpt_rehash_hpte(resize, i); | |
1396 | if (rc != 0) | |
1397 | return rc; | |
1398 | } | |
1399 | ||
1400 | return 0; | |
5e985969 DG |
1401 | } |
1402 | ||
1403 | static void resize_hpt_pivot(struct kvm_resize_hpt *resize) | |
1404 | { | |
b5baa687 DG |
1405 | struct kvm *kvm = resize->kvm; |
1406 | struct kvm_hpt_info hpt_tmp; | |
1407 | ||
1408 | /* Exchange the pending tables in the resize structure with | |
1409 | * the active tables */ | |
1410 | ||
1411 | resize_hpt_debug(resize, "resize_hpt_pivot()\n"); | |
1412 | ||
1413 | spin_lock(&kvm->mmu_lock); | |
1414 | asm volatile("ptesync" : : : "memory"); | |
1415 | ||
1416 | hpt_tmp = kvm->arch.hpt; | |
1417 | kvmppc_set_hpt(kvm, &resize->hpt); | |
1418 | resize->hpt = hpt_tmp; | |
1419 | ||
1420 | spin_unlock(&kvm->mmu_lock); | |
1421 | ||
1422 | synchronize_srcu_expedited(&kvm->srcu); | |
1423 | ||
790a9df5 DG |
1424 | if (cpu_has_feature(CPU_FTR_ARCH_300)) |
1425 | kvmppc_setup_partition_table(kvm); | |
1426 | ||
b5baa687 | 1427 | resize_hpt_debug(resize, "resize_hpt_pivot() done\n"); |
5e985969 DG |
1428 | } |
1429 | ||
1430 | static void resize_hpt_release(struct kvm *kvm, struct kvm_resize_hpt *resize) | |
1431 | { | |
4ed11aee SP |
1432 | if (WARN_ON(!mutex_is_locked(&kvm->lock))) |
1433 | return; | |
b5baa687 | 1434 | |
5b73d634 DG |
1435 | if (!resize) |
1436 | return; | |
1437 | ||
4ed11aee SP |
1438 | if (resize->error != -EBUSY) { |
1439 | if (resize->hpt.virt) | |
1440 | kvmppc_free_hpt(&resize->hpt); | |
1441 | kfree(resize); | |
1442 | } | |
b5baa687 | 1443 | |
4ed11aee SP |
1444 | if (kvm->arch.resize_hpt == resize) |
1445 | kvm->arch.resize_hpt = NULL; | |
5e985969 DG |
1446 | } |
1447 | ||
1448 | static void resize_hpt_prepare_work(struct work_struct *work) | |
1449 | { | |
1450 | struct kvm_resize_hpt *resize = container_of(work, | |
1451 | struct kvm_resize_hpt, | |
1452 | work); | |
1453 | struct kvm *kvm = resize->kvm; | |
4ed11aee | 1454 | int err = 0; |
5e985969 | 1455 | |
3073774e SP |
1456 | if (WARN_ON(resize->error != -EBUSY)) |
1457 | return; | |
1458 | ||
4ed11aee | 1459 | mutex_lock(&kvm->lock); |
5e985969 | 1460 | |
4ed11aee SP |
1461 | /* Request is still current? */ |
1462 | if (kvm->arch.resize_hpt == resize) { | |
1463 | /* We may request large allocations here: | |
1464 | * do not sleep with kvm->lock held for a while. | |
1465 | */ | |
1466 | mutex_unlock(&kvm->lock); | |
5e985969 | 1467 | |
4ed11aee SP |
1468 | resize_hpt_debug(resize, "resize_hpt_prepare_work(): order = %d\n", |
1469 | resize->order); | |
3073774e | 1470 | |
4ed11aee SP |
1471 | err = resize_hpt_allocate(resize); |
1472 | ||
1473 | /* We have strict assumption about -EBUSY | |
1474 | * when preparing for HPT resize. | |
1475 | */ | |
1476 | if (WARN_ON(err == -EBUSY)) | |
1477 | err = -EINPROGRESS; | |
1478 | ||
1479 | mutex_lock(&kvm->lock); | |
1480 | /* It is possible that kvm->arch.resize_hpt != resize | |
1481 | * after we grab kvm->lock again. | |
1482 | */ | |
1483 | } | |
5e985969 DG |
1484 | |
1485 | resize->error = err; | |
5e985969 | 1486 | |
4ed11aee SP |
1487 | if (kvm->arch.resize_hpt != resize) |
1488 | resize_hpt_release(kvm, resize); | |
1489 | ||
5e985969 DG |
1490 | mutex_unlock(&kvm->lock); |
1491 | } | |
1492 | ||
1493 | long kvm_vm_ioctl_resize_hpt_prepare(struct kvm *kvm, | |
1494 | struct kvm_ppc_resize_hpt *rhpt) | |
1495 | { | |
1496 | unsigned long flags = rhpt->flags; | |
1497 | unsigned long shift = rhpt->shift; | |
1498 | struct kvm_resize_hpt *resize; | |
1499 | int ret; | |
1500 | ||
891f1ebf | 1501 | if (flags != 0 || kvm_is_radix(kvm)) |
5e985969 DG |
1502 | return -EINVAL; |
1503 | ||
1504 | if (shift && ((shift < 18) || (shift > 46))) | |
1505 | return -EINVAL; | |
1506 | ||
1507 | mutex_lock(&kvm->lock); | |
1508 | ||
1509 | resize = kvm->arch.resize_hpt; | |
1510 | ||
1511 | if (resize) { | |
1512 | if (resize->order == shift) { | |
3073774e SP |
1513 | /* Suitable resize in progress? */ |
1514 | ret = resize->error; | |
1515 | if (ret == -EBUSY) | |
5e985969 | 1516 | ret = 100; /* estimated time in ms */ |
3073774e SP |
1517 | else if (ret) |
1518 | resize_hpt_release(kvm, resize); | |
5e985969 DG |
1519 | |
1520 | goto out; | |
1521 | } | |
1522 | ||
1523 | /* not suitable, cancel it */ | |
1524 | resize_hpt_release(kvm, resize); | |
1525 | } | |
1526 | ||
1527 | ret = 0; | |
1528 | if (!shift) | |
1529 | goto out; /* nothing to do */ | |
1530 | ||
1531 | /* start new resize */ | |
1532 | ||
1533 | resize = kzalloc(sizeof(*resize), GFP_KERNEL); | |
abd80dcb DC |
1534 | if (!resize) { |
1535 | ret = -ENOMEM; | |
1536 | goto out; | |
1537 | } | |
3073774e SP |
1538 | |
1539 | resize->error = -EBUSY; | |
5e985969 DG |
1540 | resize->order = shift; |
1541 | resize->kvm = kvm; | |
1542 | INIT_WORK(&resize->work, resize_hpt_prepare_work); | |
1543 | kvm->arch.resize_hpt = resize; | |
1544 | ||
1545 | schedule_work(&resize->work); | |
1546 | ||
1547 | ret = 100; /* estimated time in ms */ | |
1548 | ||
1549 | out: | |
1550 | mutex_unlock(&kvm->lock); | |
1551 | return ret; | |
1552 | } | |
1553 | ||
1554 | static void resize_hpt_boot_vcpu(void *opaque) | |
1555 | { | |
1556 | /* Nothing to do, just force a KVM exit */ | |
1557 | } | |
1558 | ||
1559 | long kvm_vm_ioctl_resize_hpt_commit(struct kvm *kvm, | |
1560 | struct kvm_ppc_resize_hpt *rhpt) | |
1561 | { | |
1562 | unsigned long flags = rhpt->flags; | |
1563 | unsigned long shift = rhpt->shift; | |
1564 | struct kvm_resize_hpt *resize; | |
1565 | long ret; | |
1566 | ||
891f1ebf | 1567 | if (flags != 0 || kvm_is_radix(kvm)) |
5e985969 DG |
1568 | return -EINVAL; |
1569 | ||
1570 | if (shift && ((shift < 18) || (shift > 46))) | |
1571 | return -EINVAL; | |
1572 | ||
1573 | mutex_lock(&kvm->lock); | |
1574 | ||
1575 | resize = kvm->arch.resize_hpt; | |
1576 | ||
1577 | /* This shouldn't be possible */ | |
1578 | ret = -EIO; | |
1b151ce4 | 1579 | if (WARN_ON(!kvm->arch.mmu_ready)) |
5e985969 DG |
1580 | goto out_no_hpt; |
1581 | ||
1582 | /* Stop VCPUs from running while we mess with the HPT */ | |
1b151ce4 | 1583 | kvm->arch.mmu_ready = 0; |
5e985969 DG |
1584 | smp_mb(); |
1585 | ||
1586 | /* Boot all CPUs out of the guest so they re-read | |
1b151ce4 | 1587 | * mmu_ready */ |
5e985969 DG |
1588 | on_each_cpu(resize_hpt_boot_vcpu, NULL, 1); |
1589 | ||
1590 | ret = -ENXIO; | |
1591 | if (!resize || (resize->order != shift)) | |
1592 | goto out; | |
1593 | ||
5e985969 | 1594 | ret = resize->error; |
3073774e | 1595 | if (ret) |
5e985969 DG |
1596 | goto out; |
1597 | ||
1598 | ret = resize_hpt_rehash(resize); | |
3073774e | 1599 | if (ret) |
5e985969 DG |
1600 | goto out; |
1601 | ||
1602 | resize_hpt_pivot(resize); | |
1603 | ||
1604 | out: | |
1605 | /* Let VCPUs run again */ | |
1b151ce4 | 1606 | kvm->arch.mmu_ready = 1; |
5e985969 DG |
1607 | smp_mb(); |
1608 | out_no_hpt: | |
1609 | resize_hpt_release(kvm, resize); | |
1610 | mutex_unlock(&kvm->lock); | |
1611 | return ret; | |
1612 | } | |
1613 | ||
a2932923 PM |
1614 | /* |
1615 | * Functions for reading and writing the hash table via reads and | |
1616 | * writes on a file descriptor. | |
1617 | * | |
1618 | * Reads return the guest view of the hash table, which has to be | |
1619 | * pieced together from the real hash table and the guest_rpte | |
1620 | * values in the revmap array. | |
1621 | * | |
1622 | * On writes, each HPTE written is considered in turn, and if it | |
1623 | * is valid, it is written to the HPT as if an H_ENTER with the | |
1624 | * exact flag set was done. When the invalid count is non-zero | |
1625 | * in the header written to the stream, the kernel will make | |
1626 | * sure that that many HPTEs are invalid, and invalidate them | |
1627 | * if not. | |
1628 | */ | |
1629 | ||
1630 | struct kvm_htab_ctx { | |
1631 | unsigned long index; | |
1632 | unsigned long flags; | |
1633 | struct kvm *kvm; | |
1634 | int first_pass; | |
1635 | }; | |
1636 | ||
1637 | #define HPTE_SIZE (2 * sizeof(unsigned long)) | |
1638 | ||
a1b4a0f6 PM |
1639 | /* |
1640 | * Returns 1 if this HPT entry has been modified or has pending | |
1641 | * R/C bit changes. | |
1642 | */ | |
6f22bd32 | 1643 | static int hpte_dirty(struct revmap_entry *revp, __be64 *hptp) |
a1b4a0f6 PM |
1644 | { |
1645 | unsigned long rcbits_unset; | |
1646 | ||
1647 | if (revp->guest_rpte & HPTE_GR_MODIFIED) | |
1648 | return 1; | |
1649 | ||
1650 | /* Also need to consider changes in reference and changed bits */ | |
1651 | rcbits_unset = ~revp->guest_rpte & (HPTE_R_R | HPTE_R_C); | |
6f22bd32 AG |
1652 | if ((be64_to_cpu(hptp[0]) & HPTE_V_VALID) && |
1653 | (be64_to_cpu(hptp[1]) & rcbits_unset)) | |
a1b4a0f6 PM |
1654 | return 1; |
1655 | ||
1656 | return 0; | |
1657 | } | |
1658 | ||
6f22bd32 | 1659 | static long record_hpte(unsigned long flags, __be64 *hptp, |
a2932923 PM |
1660 | unsigned long *hpte, struct revmap_entry *revp, |
1661 | int want_valid, int first_pass) | |
1662 | { | |
abb7c7dd | 1663 | unsigned long v, r, hr; |
a1b4a0f6 | 1664 | unsigned long rcbits_unset; |
a2932923 PM |
1665 | int ok = 1; |
1666 | int valid, dirty; | |
1667 | ||
1668 | /* Unmodified entries are uninteresting except on the first pass */ | |
a1b4a0f6 | 1669 | dirty = hpte_dirty(revp, hptp); |
a2932923 PM |
1670 | if (!first_pass && !dirty) |
1671 | return 0; | |
1672 | ||
1673 | valid = 0; | |
6f22bd32 | 1674 | if (be64_to_cpu(hptp[0]) & (HPTE_V_VALID | HPTE_V_ABSENT)) { |
a2932923 PM |
1675 | valid = 1; |
1676 | if ((flags & KVM_GET_HTAB_BOLTED_ONLY) && | |
6f22bd32 | 1677 | !(be64_to_cpu(hptp[0]) & HPTE_V_BOLTED)) |
a2932923 PM |
1678 | valid = 0; |
1679 | } | |
1680 | if (valid != want_valid) | |
1681 | return 0; | |
1682 | ||
1683 | v = r = 0; | |
1684 | if (valid || dirty) { | |
1685 | /* lock the HPTE so it's stable and read it */ | |
1686 | preempt_disable(); | |
1687 | while (!try_lock_hpte(hptp, HPTE_V_HVLOCK)) | |
1688 | cpu_relax(); | |
6f22bd32 | 1689 | v = be64_to_cpu(hptp[0]); |
abb7c7dd PM |
1690 | hr = be64_to_cpu(hptp[1]); |
1691 | if (cpu_has_feature(CPU_FTR_ARCH_300)) { | |
1692 | v = hpte_new_to_old_v(v, hr); | |
1693 | hr = hpte_new_to_old_r(hr); | |
1694 | } | |
a1b4a0f6 PM |
1695 | |
1696 | /* re-evaluate valid and dirty from synchronized HPTE value */ | |
1697 | valid = !!(v & HPTE_V_VALID); | |
1698 | dirty = !!(revp->guest_rpte & HPTE_GR_MODIFIED); | |
1699 | ||
1700 | /* Harvest R and C into guest view if necessary */ | |
1701 | rcbits_unset = ~revp->guest_rpte & (HPTE_R_R | HPTE_R_C); | |
abb7c7dd PM |
1702 | if (valid && (rcbits_unset & hr)) { |
1703 | revp->guest_rpte |= (hr & | |
6f22bd32 | 1704 | (HPTE_R_R | HPTE_R_C)) | HPTE_GR_MODIFIED; |
a1b4a0f6 PM |
1705 | dirty = 1; |
1706 | } | |
1707 | ||
a2932923 PM |
1708 | if (v & HPTE_V_ABSENT) { |
1709 | v &= ~HPTE_V_ABSENT; | |
1710 | v |= HPTE_V_VALID; | |
a1b4a0f6 | 1711 | valid = 1; |
a2932923 | 1712 | } |
a2932923 PM |
1713 | if ((flags & KVM_GET_HTAB_BOLTED_ONLY) && !(v & HPTE_V_BOLTED)) |
1714 | valid = 0; | |
a1b4a0f6 PM |
1715 | |
1716 | r = revp->guest_rpte; | |
a2932923 PM |
1717 | /* only clear modified if this is the right sort of entry */ |
1718 | if (valid == want_valid && dirty) { | |
1719 | r &= ~HPTE_GR_MODIFIED; | |
1720 | revp->guest_rpte = r; | |
1721 | } | |
a4bd6eb0 | 1722 | unlock_hpte(hptp, be64_to_cpu(hptp[0])); |
a2932923 PM |
1723 | preempt_enable(); |
1724 | if (!(valid == want_valid && (first_pass || dirty))) | |
1725 | ok = 0; | |
1726 | } | |
6f22bd32 AG |
1727 | hpte[0] = cpu_to_be64(v); |
1728 | hpte[1] = cpu_to_be64(r); | |
a2932923 PM |
1729 | return ok; |
1730 | } | |
1731 | ||
1732 | static ssize_t kvm_htab_read(struct file *file, char __user *buf, | |
1733 | size_t count, loff_t *ppos) | |
1734 | { | |
1735 | struct kvm_htab_ctx *ctx = file->private_data; | |
1736 | struct kvm *kvm = ctx->kvm; | |
1737 | struct kvm_get_htab_header hdr; | |
6f22bd32 | 1738 | __be64 *hptp; |
a2932923 PM |
1739 | struct revmap_entry *revp; |
1740 | unsigned long i, nb, nw; | |
1741 | unsigned long __user *lbuf; | |
1742 | struct kvm_get_htab_header __user *hptr; | |
1743 | unsigned long flags; | |
1744 | int first_pass; | |
1745 | unsigned long hpte[2]; | |
1746 | ||
96d4f267 | 1747 | if (!access_ok(buf, count)) |
a2932923 | 1748 | return -EFAULT; |
891f1ebf PM |
1749 | if (kvm_is_radix(kvm)) |
1750 | return 0; | |
a2932923 PM |
1751 | |
1752 | first_pass = ctx->first_pass; | |
1753 | flags = ctx->flags; | |
1754 | ||
1755 | i = ctx->index; | |
3f9d4f5a DG |
1756 | hptp = (__be64 *)(kvm->arch.hpt.virt + (i * HPTE_SIZE)); |
1757 | revp = kvm->arch.hpt.rev + i; | |
a2932923 PM |
1758 | lbuf = (unsigned long __user *)buf; |
1759 | ||
1760 | nb = 0; | |
1761 | while (nb + sizeof(hdr) + HPTE_SIZE < count) { | |
1762 | /* Initialize header */ | |
1763 | hptr = (struct kvm_get_htab_header __user *)buf; | |
a2932923 PM |
1764 | hdr.n_valid = 0; |
1765 | hdr.n_invalid = 0; | |
1766 | nw = nb; | |
1767 | nb += sizeof(hdr); | |
1768 | lbuf = (unsigned long __user *)(buf + sizeof(hdr)); | |
1769 | ||
1770 | /* Skip uninteresting entries, i.e. clean on not-first pass */ | |
1771 | if (!first_pass) { | |
3d089f84 | 1772 | while (i < kvmppc_hpt_npte(&kvm->arch.hpt) && |
a1b4a0f6 | 1773 | !hpte_dirty(revp, hptp)) { |
a2932923 PM |
1774 | ++i; |
1775 | hptp += 2; | |
1776 | ++revp; | |
1777 | } | |
1778 | } | |
05dd85f7 | 1779 | hdr.index = i; |
a2932923 PM |
1780 | |
1781 | /* Grab a series of valid entries */ | |
3d089f84 | 1782 | while (i < kvmppc_hpt_npte(&kvm->arch.hpt) && |
a2932923 PM |
1783 | hdr.n_valid < 0xffff && |
1784 | nb + HPTE_SIZE < count && | |
1785 | record_hpte(flags, hptp, hpte, revp, 1, first_pass)) { | |
1786 | /* valid entry, write it out */ | |
1787 | ++hdr.n_valid; | |
1788 | if (__put_user(hpte[0], lbuf) || | |
1789 | __put_user(hpte[1], lbuf + 1)) | |
1790 | return -EFAULT; | |
1791 | nb += HPTE_SIZE; | |
1792 | lbuf += 2; | |
1793 | ++i; | |
1794 | hptp += 2; | |
1795 | ++revp; | |
1796 | } | |
1797 | /* Now skip invalid entries while we can */ | |
3d089f84 | 1798 | while (i < kvmppc_hpt_npte(&kvm->arch.hpt) && |
a2932923 PM |
1799 | hdr.n_invalid < 0xffff && |
1800 | record_hpte(flags, hptp, hpte, revp, 0, first_pass)) { | |
1801 | /* found an invalid entry */ | |
1802 | ++hdr.n_invalid; | |
1803 | ++i; | |
1804 | hptp += 2; | |
1805 | ++revp; | |
1806 | } | |
1807 | ||
1808 | if (hdr.n_valid || hdr.n_invalid) { | |
1809 | /* write back the header */ | |
1810 | if (__copy_to_user(hptr, &hdr, sizeof(hdr))) | |
1811 | return -EFAULT; | |
1812 | nw = nb; | |
1813 | buf = (char __user *)lbuf; | |
1814 | } else { | |
1815 | nb = nw; | |
1816 | } | |
1817 | ||
1818 | /* Check if we've wrapped around the hash table */ | |
3d089f84 | 1819 | if (i >= kvmppc_hpt_npte(&kvm->arch.hpt)) { |
a2932923 PM |
1820 | i = 0; |
1821 | ctx->first_pass = 0; | |
1822 | break; | |
1823 | } | |
1824 | } | |
1825 | ||
1826 | ctx->index = i; | |
1827 | ||
1828 | return nb; | |
1829 | } | |
1830 | ||
1831 | static ssize_t kvm_htab_write(struct file *file, const char __user *buf, | |
1832 | size_t count, loff_t *ppos) | |
1833 | { | |
1834 | struct kvm_htab_ctx *ctx = file->private_data; | |
1835 | struct kvm *kvm = ctx->kvm; | |
1836 | struct kvm_get_htab_header hdr; | |
1837 | unsigned long i, j; | |
1838 | unsigned long v, r; | |
1839 | unsigned long __user *lbuf; | |
6f22bd32 | 1840 | __be64 *hptp; |
a2932923 PM |
1841 | unsigned long tmp[2]; |
1842 | ssize_t nb; | |
1843 | long int err, ret; | |
1b151ce4 | 1844 | int mmu_ready; |
ded13fc1 | 1845 | int pshift; |
a2932923 | 1846 | |
96d4f267 | 1847 | if (!access_ok(buf, count)) |
a2932923 | 1848 | return -EFAULT; |
891f1ebf PM |
1849 | if (kvm_is_radix(kvm)) |
1850 | return -EINVAL; | |
a2932923 PM |
1851 | |
1852 | /* lock out vcpus from running while we're doing this */ | |
1853 | mutex_lock(&kvm->lock); | |
1b151ce4 PM |
1854 | mmu_ready = kvm->arch.mmu_ready; |
1855 | if (mmu_ready) { | |
1856 | kvm->arch.mmu_ready = 0; /* temporarily */ | |
1857 | /* order mmu_ready vs. vcpus_running */ | |
a2932923 PM |
1858 | smp_mb(); |
1859 | if (atomic_read(&kvm->arch.vcpus_running)) { | |
1b151ce4 | 1860 | kvm->arch.mmu_ready = 1; |
a2932923 PM |
1861 | mutex_unlock(&kvm->lock); |
1862 | return -EBUSY; | |
1863 | } | |
1864 | } | |
1865 | ||
1866 | err = 0; | |
1867 | for (nb = 0; nb + sizeof(hdr) <= count; ) { | |
1868 | err = -EFAULT; | |
1869 | if (__copy_from_user(&hdr, buf, sizeof(hdr))) | |
1870 | break; | |
1871 | ||
1872 | err = 0; | |
1873 | if (nb + hdr.n_valid * HPTE_SIZE > count) | |
1874 | break; | |
1875 | ||
1876 | nb += sizeof(hdr); | |
1877 | buf += sizeof(hdr); | |
1878 | ||
1879 | err = -EINVAL; | |
1880 | i = hdr.index; | |
3d089f84 DG |
1881 | if (i >= kvmppc_hpt_npte(&kvm->arch.hpt) || |
1882 | i + hdr.n_valid + hdr.n_invalid > kvmppc_hpt_npte(&kvm->arch.hpt)) | |
a2932923 PM |
1883 | break; |
1884 | ||
3f9d4f5a | 1885 | hptp = (__be64 *)(kvm->arch.hpt.virt + (i * HPTE_SIZE)); |
a2932923 PM |
1886 | lbuf = (unsigned long __user *)buf; |
1887 | for (j = 0; j < hdr.n_valid; ++j) { | |
ffada016 CLG |
1888 | __be64 hpte_v; |
1889 | __be64 hpte_r; | |
1890 | ||
a2932923 | 1891 | err = -EFAULT; |
ffada016 CLG |
1892 | if (__get_user(hpte_v, lbuf) || |
1893 | __get_user(hpte_r, lbuf + 1)) | |
a2932923 | 1894 | goto out; |
ffada016 CLG |
1895 | v = be64_to_cpu(hpte_v); |
1896 | r = be64_to_cpu(hpte_r); | |
a2932923 PM |
1897 | err = -EINVAL; |
1898 | if (!(v & HPTE_V_VALID)) | |
1899 | goto out; | |
ded13fc1 PM |
1900 | pshift = kvmppc_hpte_base_page_shift(v, r); |
1901 | if (pshift <= 0) | |
1902 | goto out; | |
a2932923 PM |
1903 | lbuf += 2; |
1904 | nb += HPTE_SIZE; | |
1905 | ||
6f22bd32 | 1906 | if (be64_to_cpu(hptp[0]) & (HPTE_V_VALID | HPTE_V_ABSENT)) |
a2932923 PM |
1907 | kvmppc_do_h_remove(kvm, 0, i, 0, tmp); |
1908 | err = -EIO; | |
1909 | ret = kvmppc_virtmode_do_h_enter(kvm, H_EXACT, i, v, r, | |
1910 | tmp); | |
1911 | if (ret != H_SUCCESS) { | |
1912 | pr_err("kvm_htab_write ret %ld i=%ld v=%lx " | |
1913 | "r=%lx\n", ret, i, v, r); | |
1914 | goto out; | |
1915 | } | |
1b151ce4 | 1916 | if (!mmu_ready && is_vrma_hpte(v)) { |
ded13fc1 | 1917 | unsigned long senc, lpcr; |
a2932923 | 1918 | |
ded13fc1 | 1919 | senc = slb_pgsize_encoding(1ul << pshift); |
a2932923 PM |
1920 | kvm->arch.vrma_slb_v = senc | SLB_VSID_B_1T | |
1921 | (VRMA_VSID << SLB_VSID_SHIFT_1T); | |
ded13fc1 PM |
1922 | if (!cpu_has_feature(CPU_FTR_ARCH_300)) { |
1923 | lpcr = senc << (LPCR_VRMASD_SH - 4); | |
1924 | kvmppc_update_lpcr(kvm, lpcr, | |
1925 | LPCR_VRMASD); | |
1926 | } else { | |
1927 | kvmppc_setup_partition_table(kvm); | |
1928 | } | |
1b151ce4 | 1929 | mmu_ready = 1; |
a2932923 PM |
1930 | } |
1931 | ++i; | |
1932 | hptp += 2; | |
1933 | } | |
1934 | ||
1935 | for (j = 0; j < hdr.n_invalid; ++j) { | |
6f22bd32 | 1936 | if (be64_to_cpu(hptp[0]) & (HPTE_V_VALID | HPTE_V_ABSENT)) |
a2932923 PM |
1937 | kvmppc_do_h_remove(kvm, 0, i, 0, tmp); |
1938 | ++i; | |
1939 | hptp += 2; | |
1940 | } | |
1941 | err = 0; | |
1942 | } | |
1943 | ||
1944 | out: | |
1b151ce4 | 1945 | /* Order HPTE updates vs. mmu_ready */ |
a2932923 | 1946 | smp_wmb(); |
1b151ce4 | 1947 | kvm->arch.mmu_ready = mmu_ready; |
a2932923 PM |
1948 | mutex_unlock(&kvm->lock); |
1949 | ||
1950 | if (err) | |
1951 | return err; | |
1952 | return nb; | |
1953 | } | |
1954 | ||
1955 | static int kvm_htab_release(struct inode *inode, struct file *filp) | |
1956 | { | |
1957 | struct kvm_htab_ctx *ctx = filp->private_data; | |
1958 | ||
1959 | filp->private_data = NULL; | |
1960 | if (!(ctx->flags & KVM_GET_HTAB_WRITE)) | |
1961 | atomic_dec(&ctx->kvm->arch.hpte_mod_interest); | |
1962 | kvm_put_kvm(ctx->kvm); | |
1963 | kfree(ctx); | |
1964 | return 0; | |
1965 | } | |
1966 | ||
75ef9de1 | 1967 | static const struct file_operations kvm_htab_fops = { |
a2932923 PM |
1968 | .read = kvm_htab_read, |
1969 | .write = kvm_htab_write, | |
1970 | .llseek = default_llseek, | |
1971 | .release = kvm_htab_release, | |
1972 | }; | |
1973 | ||
1974 | int kvm_vm_ioctl_get_htab_fd(struct kvm *kvm, struct kvm_get_htab_fd *ghf) | |
1975 | { | |
1976 | int ret; | |
1977 | struct kvm_htab_ctx *ctx; | |
1978 | int rwflag; | |
1979 | ||
1980 | /* reject flags we don't recognize */ | |
1981 | if (ghf->flags & ~(KVM_GET_HTAB_BOLTED_ONLY | KVM_GET_HTAB_WRITE)) | |
1982 | return -EINVAL; | |
1983 | ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); | |
1984 | if (!ctx) | |
1985 | return -ENOMEM; | |
1986 | kvm_get_kvm(kvm); | |
1987 | ctx->kvm = kvm; | |
1988 | ctx->index = ghf->start_index; | |
1989 | ctx->flags = ghf->flags; | |
1990 | ctx->first_pass = 1; | |
1991 | ||
1992 | rwflag = (ghf->flags & KVM_GET_HTAB_WRITE) ? O_WRONLY : O_RDONLY; | |
2f84d5ea | 1993 | ret = anon_inode_getfd("kvm-htab", &kvm_htab_fops, ctx, rwflag | O_CLOEXEC); |
a2932923 | 1994 | if (ret < 0) { |
43f6b0cf | 1995 | kfree(ctx); |
a2932923 PM |
1996 | kvm_put_kvm(kvm); |
1997 | return ret; | |
1998 | } | |
1999 | ||
2000 | if (rwflag == O_RDONLY) { | |
2001 | mutex_lock(&kvm->slots_lock); | |
2002 | atomic_inc(&kvm->arch.hpte_mod_interest); | |
2003 | /* make sure kvmppc_do_h_enter etc. see the increment */ | |
2004 | synchronize_srcu_expedited(&kvm->srcu); | |
2005 | mutex_unlock(&kvm->slots_lock); | |
2006 | } | |
2007 | ||
2008 | return ret; | |
2009 | } | |
2010 | ||
e23a808b PM |
2011 | struct debugfs_htab_state { |
2012 | struct kvm *kvm; | |
2013 | struct mutex mutex; | |
2014 | unsigned long hpt_index; | |
2015 | int chars_left; | |
2016 | int buf_index; | |
2017 | char buf[64]; | |
2018 | }; | |
2019 | ||
2020 | static int debugfs_htab_open(struct inode *inode, struct file *file) | |
2021 | { | |
2022 | struct kvm *kvm = inode->i_private; | |
2023 | struct debugfs_htab_state *p; | |
2024 | ||
2025 | p = kzalloc(sizeof(*p), GFP_KERNEL); | |
2026 | if (!p) | |
2027 | return -ENOMEM; | |
2028 | ||
2029 | kvm_get_kvm(kvm); | |
2030 | p->kvm = kvm; | |
2031 | mutex_init(&p->mutex); | |
2032 | file->private_data = p; | |
2033 | ||
2034 | return nonseekable_open(inode, file); | |
2035 | } | |
2036 | ||
2037 | static int debugfs_htab_release(struct inode *inode, struct file *file) | |
2038 | { | |
2039 | struct debugfs_htab_state *p = file->private_data; | |
2040 | ||
2041 | kvm_put_kvm(p->kvm); | |
2042 | kfree(p); | |
2043 | return 0; | |
2044 | } | |
2045 | ||
2046 | static ssize_t debugfs_htab_read(struct file *file, char __user *buf, | |
2047 | size_t len, loff_t *ppos) | |
2048 | { | |
2049 | struct debugfs_htab_state *p = file->private_data; | |
2050 | ssize_t ret, r; | |
2051 | unsigned long i, n; | |
2052 | unsigned long v, hr, gr; | |
2053 | struct kvm *kvm; | |
2054 | __be64 *hptp; | |
2055 | ||
891f1ebf PM |
2056 | kvm = p->kvm; |
2057 | if (kvm_is_radix(kvm)) | |
2058 | return 0; | |
2059 | ||
e23a808b PM |
2060 | ret = mutex_lock_interruptible(&p->mutex); |
2061 | if (ret) | |
2062 | return ret; | |
2063 | ||
2064 | if (p->chars_left) { | |
2065 | n = p->chars_left; | |
2066 | if (n > len) | |
2067 | n = len; | |
2068 | r = copy_to_user(buf, p->buf + p->buf_index, n); | |
2069 | n -= r; | |
2070 | p->chars_left -= n; | |
2071 | p->buf_index += n; | |
2072 | buf += n; | |
2073 | len -= n; | |
2074 | ret = n; | |
2075 | if (r) { | |
2076 | if (!n) | |
2077 | ret = -EFAULT; | |
2078 | goto out; | |
2079 | } | |
2080 | } | |
2081 | ||
e23a808b | 2082 | i = p->hpt_index; |
3f9d4f5a | 2083 | hptp = (__be64 *)(kvm->arch.hpt.virt + (i * HPTE_SIZE)); |
3d089f84 DG |
2084 | for (; len != 0 && i < kvmppc_hpt_npte(&kvm->arch.hpt); |
2085 | ++i, hptp += 2) { | |
e23a808b PM |
2086 | if (!(be64_to_cpu(hptp[0]) & (HPTE_V_VALID | HPTE_V_ABSENT))) |
2087 | continue; | |
2088 | ||
2089 | /* lock the HPTE so it's stable and read it */ | |
2090 | preempt_disable(); | |
2091 | while (!try_lock_hpte(hptp, HPTE_V_HVLOCK)) | |
2092 | cpu_relax(); | |
2093 | v = be64_to_cpu(hptp[0]) & ~HPTE_V_HVLOCK; | |
2094 | hr = be64_to_cpu(hptp[1]); | |
3f9d4f5a | 2095 | gr = kvm->arch.hpt.rev[i].guest_rpte; |
e23a808b PM |
2096 | unlock_hpte(hptp, v); |
2097 | preempt_enable(); | |
2098 | ||
2099 | if (!(v & (HPTE_V_VALID | HPTE_V_ABSENT))) | |
2100 | continue; | |
2101 | ||
2102 | n = scnprintf(p->buf, sizeof(p->buf), | |
2103 | "%6lx %.16lx %.16lx %.16lx\n", | |
2104 | i, v, hr, gr); | |
2105 | p->chars_left = n; | |
2106 | if (n > len) | |
2107 | n = len; | |
2108 | r = copy_to_user(buf, p->buf, n); | |
2109 | n -= r; | |
2110 | p->chars_left -= n; | |
2111 | p->buf_index = n; | |
2112 | buf += n; | |
2113 | len -= n; | |
2114 | ret += n; | |
2115 | if (r) { | |
2116 | if (!ret) | |
2117 | ret = -EFAULT; | |
2118 | goto out; | |
2119 | } | |
2120 | } | |
2121 | p->hpt_index = i; | |
2122 | ||
2123 | out: | |
2124 | mutex_unlock(&p->mutex); | |
2125 | return ret; | |
2126 | } | |
2127 | ||
025c9511 | 2128 | static ssize_t debugfs_htab_write(struct file *file, const char __user *buf, |
e23a808b PM |
2129 | size_t len, loff_t *ppos) |
2130 | { | |
2131 | return -EACCES; | |
2132 | } | |
2133 | ||
2134 | static const struct file_operations debugfs_htab_fops = { | |
2135 | .owner = THIS_MODULE, | |
2136 | .open = debugfs_htab_open, | |
2137 | .release = debugfs_htab_release, | |
2138 | .read = debugfs_htab_read, | |
2139 | .write = debugfs_htab_write, | |
2140 | .llseek = generic_file_llseek, | |
2141 | }; | |
2142 | ||
2143 | void kvmppc_mmu_debugfs_init(struct kvm *kvm) | |
2144 | { | |
2145 | kvm->arch.htab_dentry = debugfs_create_file("htab", 0400, | |
2146 | kvm->arch.debugfs_dir, kvm, | |
2147 | &debugfs_htab_fops); | |
2148 | } | |
2149 | ||
de56a948 PM |
2150 | void kvmppc_mmu_book3s_hv_init(struct kvm_vcpu *vcpu) |
2151 | { | |
2152 | struct kvmppc_mmu *mmu = &vcpu->arch.mmu; | |
2153 | ||
c17b98cf | 2154 | vcpu->arch.slb_nr = 32; /* POWER7/POWER8 */ |
de56a948 | 2155 | |
18c3640c | 2156 | mmu->xlate = kvmppc_mmu_book3s_64_hv_xlate; |
de56a948 PM |
2157 | mmu->reset_msr = kvmppc_mmu_book3s_64_hv_reset_msr; |
2158 | ||
2159 | vcpu->arch.hflags |= BOOK3S_HFLAG_SLB; | |
2160 | } |