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.
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.
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.
15 * Copyright 2010 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
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>
26 #include <linux/vmalloc.h>
28 #include <asm/tlbflush.h>
29 #include <asm/kvm_ppc.h>
30 #include <asm/kvm_book3s.h>
31 #include <asm/mmu-hash64.h>
32 #include <asm/hvcall.h>
33 #include <asm/synch.h>
34 #include <asm/ppc-opcode.h>
35 #include <asm/cputable.h>
37 #define VRMA_VSID 0x1ffffffUL /* 1TB VSID reserved for VRMA */
39 /* POWER7 has 10-bit LPIDs, PPC970 has 6-bit LPIDs */
40 #define MAX_LPID_970 63
41 #define NR_LPIDS (LPID_RSVD + 1)
42 unsigned long lpid_inuse[BITS_TO_LONGS(NR_LPIDS)];
44 long kvmppc_alloc_hpt(struct kvm *kvm)
48 struct revmap_entry *rev;
50 /* Allocate guest's hashed page table */
51 hpt = __get_free_pages(GFP_KERNEL|__GFP_ZERO|__GFP_REPEAT|__GFP_NOWARN,
52 HPT_ORDER - PAGE_SHIFT);
54 pr_err("kvm_alloc_hpt: Couldn't alloc HPT\n");
57 kvm->arch.hpt_virt = hpt;
59 /* Allocate reverse map array */
60 rev = vmalloc(sizeof(struct revmap_entry) * HPT_NPTE);
62 pr_err("kvmppc_alloc_hpt: Couldn't alloc reverse map array\n");
65 kvm->arch.revmap = rev;
67 /* Allocate the guest's logical partition ID */
69 lpid = find_first_zero_bit(lpid_inuse, NR_LPIDS);
70 if (lpid >= NR_LPIDS) {
71 pr_err("kvm_alloc_hpt: No LPIDs free\n");
74 } while (test_and_set_bit(lpid, lpid_inuse));
76 kvm->arch.sdr1 = __pa(hpt) | (HPT_ORDER - 18);
77 kvm->arch.lpid = lpid;
79 pr_info("KVM guest htab at %lx, LPID %lx\n", hpt, lpid);
85 free_pages(hpt, HPT_ORDER - PAGE_SHIFT);
89 void kvmppc_free_hpt(struct kvm *kvm)
91 clear_bit(kvm->arch.lpid, lpid_inuse);
92 vfree(kvm->arch.revmap);
93 free_pages(kvm->arch.hpt_virt, HPT_ORDER - PAGE_SHIFT);
96 /* Bits in first HPTE dword for pagesize 4k, 64k or 16M */
97 static inline unsigned long hpte0_pgsize_encoding(unsigned long pgsize)
99 return (pgsize > 0x1000) ? HPTE_V_LARGE : 0;
102 /* Bits in second HPTE dword for pagesize 4k, 64k or 16M */
103 static inline unsigned long hpte1_pgsize_encoding(unsigned long pgsize)
105 return (pgsize == 0x10000) ? 0x1000 : 0;
108 void kvmppc_map_vrma(struct kvm_vcpu *vcpu, struct kvm_memory_slot *memslot,
109 unsigned long porder)
112 unsigned long npages;
113 unsigned long hp_v, hp_r;
114 unsigned long addr, hash;
116 unsigned long hp0, hp1;
119 psize = 1ul << porder;
120 npages = memslot->npages >> (porder - PAGE_SHIFT);
122 /* VRMA can't be > 1TB */
123 if (npages > 1ul << (40 - porder))
124 npages = 1ul << (40 - porder);
125 /* Can't use more than 1 HPTE per HPTEG */
126 if (npages > HPT_NPTEG)
129 hp0 = HPTE_V_1TB_SEG | (VRMA_VSID << (40 - 16)) |
130 HPTE_V_BOLTED | hpte0_pgsize_encoding(psize);
131 hp1 = hpte1_pgsize_encoding(psize) |
132 HPTE_R_R | HPTE_R_C | HPTE_R_M | PP_RWXX;
134 for (i = 0; i < npages; ++i) {
136 /* can't use hpt_hash since va > 64 bits */
137 hash = (i ^ (VRMA_VSID ^ (VRMA_VSID << 25))) & HPT_HASH_MASK;
139 * We assume that the hash table is empty and no
140 * vcpus are using it at this stage. Since we create
141 * at most one HPTE per HPTEG, we just assume entry 7
142 * is available and use it.
144 hash = (hash << 3) + 7;
145 hp_v = hp0 | ((addr >> 16) & ~0x7fUL);
147 ret = kvmppc_virtmode_h_enter(vcpu, H_EXACT, hash, hp_v, hp_r);
148 if (ret != H_SUCCESS) {
149 pr_err("KVM: map_vrma at %lx failed, ret=%ld\n",
156 int kvmppc_mmu_hv_init(void)
158 unsigned long host_lpid, rsvd_lpid;
160 if (!cpu_has_feature(CPU_FTR_HVMODE))
163 memset(lpid_inuse, 0, sizeof(lpid_inuse));
165 if (cpu_has_feature(CPU_FTR_ARCH_206)) {
166 host_lpid = mfspr(SPRN_LPID); /* POWER7 */
167 rsvd_lpid = LPID_RSVD;
169 host_lpid = 0; /* PPC970 */
170 rsvd_lpid = MAX_LPID_970;
173 set_bit(host_lpid, lpid_inuse);
174 /* rsvd_lpid is reserved for use in partition switching */
175 set_bit(rsvd_lpid, lpid_inuse);
180 void kvmppc_mmu_destroy(struct kvm_vcpu *vcpu)
184 static void kvmppc_mmu_book3s_64_hv_reset_msr(struct kvm_vcpu *vcpu)
186 kvmppc_set_msr(vcpu, MSR_SF | MSR_ME);
190 * This is called to get a reference to a guest page if there isn't
191 * one already in the kvm->arch.slot_phys[][] arrays.
193 static long kvmppc_get_guest_page(struct kvm *kvm, unsigned long gfn,
194 struct kvm_memory_slot *memslot,
199 struct page *page, *hpage, *pages[1];
200 unsigned long s, pgsize;
201 unsigned long *physp;
202 unsigned int got, pgorder;
203 unsigned long pfn, i, npages;
205 physp = kvm->arch.slot_phys[memslot->id];
208 if (physp[gfn - memslot->base_gfn])
213 start = gfn_to_hva_memslot(memslot, gfn);
215 /* Instantiate and get the page we want access to */
216 np = get_user_pages_fast(start, 1, 1, pages);
220 got = KVMPPC_GOT_PAGE;
222 /* See if this is a large page */
224 if (PageHuge(page)) {
225 hpage = compound_head(page);
226 s <<= compound_order(hpage);
227 /* Get the whole large page if slot alignment is ok */
228 if (s > psize && slot_is_aligned(memslot, s) &&
229 !(memslot->userspace_addr & (s - 1))) {
238 pfn = page_to_pfn(page);
240 npages = pgsize >> PAGE_SHIFT;
241 pgorder = __ilog2(npages);
242 physp += (gfn - memslot->base_gfn) & ~(npages - 1);
243 spin_lock(&kvm->arch.slot_phys_lock);
244 for (i = 0; i < npages; ++i) {
246 physp[i] = ((pfn + i) << PAGE_SHIFT) + got + pgorder;
250 spin_unlock(&kvm->arch.slot_phys_lock);
256 page = compound_head(page);
263 * We come here on a H_ENTER call from the guest when
264 * we don't have the requested page pinned already.
266 long kvmppc_virtmode_h_enter(struct kvm_vcpu *vcpu, unsigned long flags,
267 long pte_index, unsigned long pteh, unsigned long ptel)
269 struct kvm *kvm = vcpu->kvm;
270 unsigned long psize, gpa, gfn;
271 struct kvm_memory_slot *memslot;
274 psize = hpte_page_size(pteh, ptel);
278 /* Find the memslot (if any) for this address */
279 gpa = (ptel & HPTE_R_RPN) & ~(psize - 1);
280 gfn = gpa >> PAGE_SHIFT;
281 memslot = gfn_to_memslot(kvm, gfn);
282 if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID))
284 if (!slot_is_aligned(memslot, psize))
286 if (kvmppc_get_guest_page(kvm, gfn, memslot, psize) < 0)
290 ret = kvmppc_h_enter(vcpu, flags, pte_index, pteh, ptel);
292 if (ret == H_TOO_HARD) {
293 /* this can't happen */
294 pr_err("KVM: Oops, kvmppc_h_enter returned too hard!\n");
295 ret = H_RESOURCE; /* or something */
301 static int kvmppc_mmu_book3s_64_hv_xlate(struct kvm_vcpu *vcpu, gva_t eaddr,
302 struct kvmppc_pte *gpte, bool data)
307 void *kvmppc_pin_guest_page(struct kvm *kvm, unsigned long gpa,
308 unsigned long *nb_ret)
310 struct kvm_memory_slot *memslot;
311 unsigned long gfn = gpa >> PAGE_SHIFT;
313 unsigned long psize, offset;
315 unsigned long *physp;
317 memslot = gfn_to_memslot(kvm, gfn);
318 if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID))
320 physp = kvm->arch.slot_phys[memslot->id];
323 physp += gfn - memslot->base_gfn;
326 if (kvmppc_get_guest_page(kvm, gfn, memslot, PAGE_SIZE) < 0)
330 page = pfn_to_page(pa >> PAGE_SHIFT);
332 if (PageHuge(page)) {
333 page = compound_head(page);
334 psize <<= compound_order(page);
337 offset = gpa & (psize - 1);
339 *nb_ret = psize - offset;
340 return page_address(page) + offset;
343 void kvmppc_unpin_guest_page(struct kvm *kvm, void *va)
345 struct page *page = virt_to_page(va);
347 page = compound_head(page);
351 void kvmppc_mmu_book3s_hv_init(struct kvm_vcpu *vcpu)
353 struct kvmppc_mmu *mmu = &vcpu->arch.mmu;
355 if (cpu_has_feature(CPU_FTR_ARCH_206))
356 vcpu->arch.slb_nr = 32; /* POWER7 */
358 vcpu->arch.slb_nr = 64;
360 mmu->xlate = kvmppc_mmu_book3s_64_hv_xlate;
361 mmu->reset_msr = kvmppc_mmu_book3s_64_hv_reset_msr;
363 vcpu->arch.hflags |= BOOK3S_HFLAG_SLB;