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> |
de56a948 PM |
27 | |
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> | |
36 | ||
de56a948 PM |
37 | /* Pages in the VRMA are 16MB pages */ |
38 | #define VRMA_PAGE_ORDER 24 | |
39 | #define VRMA_VSID 0x1ffffffUL /* 1TB VSID reserved for VRMA */ | |
40 | ||
9e368f29 PM |
41 | /* POWER7 has 10-bit LPIDs, PPC970 has 6-bit LPIDs */ |
42 | #define MAX_LPID_970 63 | |
de56a948 PM |
43 | #define NR_LPIDS (LPID_RSVD + 1) |
44 | unsigned long lpid_inuse[BITS_TO_LONGS(NR_LPIDS)]; | |
45 | ||
46 | long kvmppc_alloc_hpt(struct kvm *kvm) | |
47 | { | |
48 | unsigned long hpt; | |
49 | unsigned long lpid; | |
8936dda4 | 50 | struct revmap_entry *rev; |
de56a948 | 51 | |
8936dda4 | 52 | /* Allocate guest's hashed page table */ |
de56a948 PM |
53 | hpt = __get_free_pages(GFP_KERNEL|__GFP_ZERO|__GFP_REPEAT|__GFP_NOWARN, |
54 | HPT_ORDER - PAGE_SHIFT); | |
55 | if (!hpt) { | |
56 | pr_err("kvm_alloc_hpt: Couldn't alloc HPT\n"); | |
57 | return -ENOMEM; | |
58 | } | |
59 | kvm->arch.hpt_virt = hpt; | |
60 | ||
8936dda4 PM |
61 | /* Allocate reverse map array */ |
62 | rev = vmalloc(sizeof(struct revmap_entry) * HPT_NPTE); | |
63 | if (!rev) { | |
64 | pr_err("kvmppc_alloc_hpt: Couldn't alloc reverse map array\n"); | |
65 | goto out_freehpt; | |
66 | } | |
67 | kvm->arch.revmap = rev; | |
68 | ||
69 | /* Allocate the guest's logical partition ID */ | |
de56a948 PM |
70 | do { |
71 | lpid = find_first_zero_bit(lpid_inuse, NR_LPIDS); | |
72 | if (lpid >= NR_LPIDS) { | |
73 | pr_err("kvm_alloc_hpt: No LPIDs free\n"); | |
8936dda4 | 74 | goto out_freeboth; |
de56a948 PM |
75 | } |
76 | } while (test_and_set_bit(lpid, lpid_inuse)); | |
77 | ||
78 | kvm->arch.sdr1 = __pa(hpt) | (HPT_ORDER - 18); | |
79 | kvm->arch.lpid = lpid; | |
de56a948 PM |
80 | |
81 | pr_info("KVM guest htab at %lx, LPID %lx\n", hpt, lpid); | |
82 | return 0; | |
8936dda4 PM |
83 | |
84 | out_freeboth: | |
85 | vfree(rev); | |
86 | out_freehpt: | |
87 | free_pages(hpt, HPT_ORDER - PAGE_SHIFT); | |
88 | return -ENOMEM; | |
de56a948 PM |
89 | } |
90 | ||
91 | void kvmppc_free_hpt(struct kvm *kvm) | |
92 | { | |
de56a948 | 93 | clear_bit(kvm->arch.lpid, lpid_inuse); |
8936dda4 | 94 | vfree(kvm->arch.revmap); |
de56a948 | 95 | free_pages(kvm->arch.hpt_virt, HPT_ORDER - PAGE_SHIFT); |
de56a948 PM |
96 | } |
97 | ||
c77162de | 98 | void kvmppc_map_vrma(struct kvm_vcpu *vcpu, struct kvm_memory_slot *memslot) |
de56a948 | 99 | { |
c77162de | 100 | struct kvm *kvm = vcpu->kvm; |
de56a948 | 101 | unsigned long i; |
b2b2f165 | 102 | unsigned long npages; |
c77162de PM |
103 | unsigned long hp_v, hp_r; |
104 | unsigned long addr, hash; | |
8936dda4 | 105 | unsigned long porder = kvm->arch.ram_porder; |
c77162de | 106 | long ret; |
de56a948 | 107 | |
c77162de | 108 | npages = kvm->arch.slot_npages[memslot->id]; |
de56a948 PM |
109 | |
110 | /* VRMA can't be > 1TB */ | |
8936dda4 PM |
111 | if (npages > 1ul << (40 - porder)) |
112 | npages = 1ul << (40 - porder); | |
de56a948 PM |
113 | /* Can't use more than 1 HPTE per HPTEG */ |
114 | if (npages > HPT_NPTEG) | |
115 | npages = HPT_NPTEG; | |
116 | ||
117 | for (i = 0; i < npages; ++i) { | |
c77162de | 118 | addr = i << porder; |
de56a948 PM |
119 | /* can't use hpt_hash since va > 64 bits */ |
120 | hash = (i ^ (VRMA_VSID ^ (VRMA_VSID << 25))) & HPT_HASH_MASK; | |
121 | /* | |
122 | * We assume that the hash table is empty and no | |
123 | * vcpus are using it at this stage. Since we create | |
124 | * at most one HPTE per HPTEG, we just assume entry 7 | |
125 | * is available and use it. | |
126 | */ | |
8936dda4 | 127 | hash = (hash << 3) + 7; |
c77162de | 128 | hp_v = HPTE_V_1TB_SEG | (VRMA_VSID << (40 - 16)) | |
de56a948 PM |
129 | (i << (VRMA_PAGE_ORDER - 16)) | HPTE_V_BOLTED | |
130 | HPTE_V_LARGE | HPTE_V_VALID; | |
c77162de PM |
131 | hp_r = addr | HPTE_R_R | HPTE_R_C | HPTE_R_M | PP_RWXX; |
132 | ret = kvmppc_virtmode_h_enter(vcpu, H_EXACT, hash, hp_v, hp_r); | |
133 | if (ret != H_SUCCESS) { | |
134 | pr_err("KVM: map_vrma at %lx failed, ret=%ld\n", | |
135 | addr, ret); | |
136 | break; | |
137 | } | |
de56a948 PM |
138 | } |
139 | } | |
140 | ||
141 | int kvmppc_mmu_hv_init(void) | |
142 | { | |
9e368f29 PM |
143 | unsigned long host_lpid, rsvd_lpid; |
144 | ||
145 | if (!cpu_has_feature(CPU_FTR_HVMODE)) | |
de56a948 | 146 | return -EINVAL; |
9e368f29 | 147 | |
de56a948 | 148 | memset(lpid_inuse, 0, sizeof(lpid_inuse)); |
9e368f29 PM |
149 | |
150 | if (cpu_has_feature(CPU_FTR_ARCH_206)) { | |
151 | host_lpid = mfspr(SPRN_LPID); /* POWER7 */ | |
152 | rsvd_lpid = LPID_RSVD; | |
153 | } else { | |
154 | host_lpid = 0; /* PPC970 */ | |
155 | rsvd_lpid = MAX_LPID_970; | |
156 | } | |
157 | ||
158 | set_bit(host_lpid, lpid_inuse); | |
159 | /* rsvd_lpid is reserved for use in partition switching */ | |
160 | set_bit(rsvd_lpid, lpid_inuse); | |
de56a948 PM |
161 | |
162 | return 0; | |
163 | } | |
164 | ||
165 | void kvmppc_mmu_destroy(struct kvm_vcpu *vcpu) | |
166 | { | |
167 | } | |
168 | ||
169 | static void kvmppc_mmu_book3s_64_hv_reset_msr(struct kvm_vcpu *vcpu) | |
170 | { | |
171 | kvmppc_set_msr(vcpu, MSR_SF | MSR_ME); | |
172 | } | |
173 | ||
c77162de PM |
174 | /* |
175 | * This is called to get a reference to a guest page if there isn't | |
176 | * one already in the kvm->arch.slot_phys[][] arrays. | |
177 | */ | |
178 | static long kvmppc_get_guest_page(struct kvm *kvm, unsigned long gfn, | |
179 | struct kvm_memory_slot *memslot) | |
180 | { | |
181 | unsigned long start; | |
182 | long np; | |
183 | struct page *page, *pages[1]; | |
184 | unsigned long *physp; | |
185 | unsigned long pfn, i; | |
186 | ||
187 | physp = kvm->arch.slot_phys[memslot->id]; | |
188 | if (!physp) | |
189 | return -EINVAL; | |
190 | i = (gfn - memslot->base_gfn) >> (kvm->arch.ram_porder - PAGE_SHIFT); | |
191 | if (physp[i]) | |
192 | return 0; | |
193 | ||
194 | page = NULL; | |
195 | start = gfn_to_hva_memslot(memslot, gfn); | |
196 | ||
197 | /* Instantiate and get the page we want access to */ | |
198 | np = get_user_pages_fast(start, 1, 1, pages); | |
199 | if (np != 1) | |
200 | return -EINVAL; | |
201 | page = pages[0]; | |
202 | ||
203 | /* Check it's a 16MB page */ | |
204 | if (!PageHead(page) || | |
205 | compound_order(page) != (kvm->arch.ram_porder - PAGE_SHIFT)) { | |
206 | pr_err("page at %lx isn't 16MB (o=%d)\n", | |
207 | start, compound_order(page)); | |
208 | put_page(page); | |
209 | return -EINVAL; | |
210 | } | |
211 | pfn = page_to_pfn(page); | |
212 | ||
213 | spin_lock(&kvm->arch.slot_phys_lock); | |
214 | if (!physp[i]) | |
215 | physp[i] = (pfn << PAGE_SHIFT) | KVMPPC_GOT_PAGE; | |
216 | else | |
217 | put_page(page); | |
218 | spin_unlock(&kvm->arch.slot_phys_lock); | |
219 | ||
220 | return 0; | |
221 | } | |
222 | ||
223 | /* | |
224 | * We come here on a H_ENTER call from the guest when | |
225 | * we don't have the requested page pinned already. | |
226 | */ | |
227 | long kvmppc_virtmode_h_enter(struct kvm_vcpu *vcpu, unsigned long flags, | |
228 | long pte_index, unsigned long pteh, unsigned long ptel) | |
229 | { | |
230 | struct kvm *kvm = vcpu->kvm; | |
231 | unsigned long psize, gpa, gfn; | |
232 | struct kvm_memory_slot *memslot; | |
233 | long ret; | |
234 | ||
235 | psize = hpte_page_size(pteh, ptel); | |
236 | if (!psize) | |
237 | return H_PARAMETER; | |
238 | ||
239 | /* Find the memslot (if any) for this address */ | |
240 | gpa = (ptel & HPTE_R_RPN) & ~(psize - 1); | |
241 | gfn = gpa >> PAGE_SHIFT; | |
242 | memslot = gfn_to_memslot(kvm, gfn); | |
243 | if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID)) | |
244 | return H_PARAMETER; | |
245 | if (kvmppc_get_guest_page(kvm, gfn, memslot) < 0) | |
246 | return H_PARAMETER; | |
247 | ||
248 | preempt_disable(); | |
249 | ret = kvmppc_h_enter(vcpu, flags, pte_index, pteh, ptel); | |
250 | preempt_enable(); | |
251 | if (ret == H_TOO_HARD) { | |
252 | /* this can't happen */ | |
253 | pr_err("KVM: Oops, kvmppc_h_enter returned too hard!\n"); | |
254 | ret = H_RESOURCE; /* or something */ | |
255 | } | |
256 | return ret; | |
257 | ||
258 | } | |
259 | ||
de56a948 PM |
260 | static int kvmppc_mmu_book3s_64_hv_xlate(struct kvm_vcpu *vcpu, gva_t eaddr, |
261 | struct kvmppc_pte *gpte, bool data) | |
262 | { | |
263 | return -ENOENT; | |
264 | } | |
265 | ||
93e60249 PM |
266 | void *kvmppc_pin_guest_page(struct kvm *kvm, unsigned long gpa, |
267 | unsigned long *nb_ret) | |
268 | { | |
269 | struct kvm_memory_slot *memslot; | |
270 | unsigned long gfn = gpa >> PAGE_SHIFT; | |
271 | struct page *page; | |
272 | unsigned long offset; | |
273 | unsigned long pfn, pa; | |
274 | unsigned long *physp; | |
275 | ||
276 | memslot = gfn_to_memslot(kvm, gfn); | |
277 | if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID)) | |
278 | return NULL; | |
279 | physp = kvm->arch.slot_phys[memslot->id]; | |
280 | if (!physp) | |
281 | return NULL; | |
282 | physp += (gfn - memslot->base_gfn) >> | |
283 | (kvm->arch.ram_porder - PAGE_SHIFT); | |
284 | pa = *physp; | |
c77162de PM |
285 | if (!pa) { |
286 | if (kvmppc_get_guest_page(kvm, gfn, memslot) < 0) | |
287 | return NULL; | |
288 | pa = *physp; | |
289 | } | |
93e60249 PM |
290 | pfn = pa >> PAGE_SHIFT; |
291 | page = pfn_to_page(pfn); | |
292 | get_page(page); | |
293 | offset = gpa & (kvm->arch.ram_psize - 1); | |
294 | if (nb_ret) | |
295 | *nb_ret = kvm->arch.ram_psize - offset; | |
296 | return page_address(page) + offset; | |
297 | } | |
298 | ||
299 | void kvmppc_unpin_guest_page(struct kvm *kvm, void *va) | |
300 | { | |
301 | struct page *page = virt_to_page(va); | |
302 | ||
303 | page = compound_head(page); | |
304 | put_page(page); | |
305 | } | |
306 | ||
de56a948 PM |
307 | void kvmppc_mmu_book3s_hv_init(struct kvm_vcpu *vcpu) |
308 | { | |
309 | struct kvmppc_mmu *mmu = &vcpu->arch.mmu; | |
310 | ||
9e368f29 PM |
311 | if (cpu_has_feature(CPU_FTR_ARCH_206)) |
312 | vcpu->arch.slb_nr = 32; /* POWER7 */ | |
313 | else | |
314 | vcpu->arch.slb_nr = 64; | |
de56a948 PM |
315 | |
316 | mmu->xlate = kvmppc_mmu_book3s_64_hv_xlate; | |
317 | mmu->reset_msr = kvmppc_mmu_book3s_64_hv_reset_msr; | |
318 | ||
319 | vcpu->arch.hflags |= BOOK3S_HFLAG_SLB; | |
320 | } |