Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 1 | /* |
Scott Wood | 49ea069 | 2011-03-28 15:01:24 -0500 | [diff] [blame] | 2 | * Copyright (C) 2008-2011 Freescale Semiconductor, Inc. All rights reserved. |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 3 | * |
| 4 | * Author: Yu Liu, yu.liu@freescale.com |
| 5 | * |
| 6 | * Description: |
| 7 | * This file is based on arch/powerpc/kvm/44x_tlb.c, |
| 8 | * by Hollis Blanchard <hollisb@us.ibm.com>. |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License, version 2, as |
| 12 | * published by the Free Software Foundation. |
| 13 | */ |
| 14 | |
| 15 | #include <linux/types.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 16 | #include <linux/slab.h> |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 17 | #include <linux/string.h> |
| 18 | #include <linux/kvm.h> |
| 19 | #include <linux/kvm_host.h> |
| 20 | #include <linux/highmem.h> |
| 21 | #include <asm/kvm_ppc.h> |
| 22 | #include <asm/kvm_e500.h> |
| 23 | |
Liu Yu | 9aa4dd5 | 2009-01-14 10:47:38 -0600 | [diff] [blame] | 24 | #include "../mm/mmu_decl.h" |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 25 | #include "e500_tlb.h" |
Marcelo Tosatti | 46f43c6 | 2009-06-18 11:47:27 -0300 | [diff] [blame] | 26 | #include "trace.h" |
Scott Wood | 49ea069 | 2011-03-28 15:01:24 -0500 | [diff] [blame] | 27 | #include "timing.h" |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 28 | |
| 29 | #define to_htlb1_esel(esel) (tlb1_entry_num - (esel) - 1) |
| 30 | |
| 31 | static unsigned int tlb1_entry_num; |
| 32 | |
| 33 | void kvmppc_dump_tlbs(struct kvm_vcpu *vcpu) |
| 34 | { |
| 35 | struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu); |
| 36 | struct tlbe *tlbe; |
| 37 | int i, tlbsel; |
| 38 | |
| 39 | printk("| %8s | %8s | %8s | %8s | %8s |\n", |
| 40 | "nr", "mas1", "mas2", "mas3", "mas7"); |
| 41 | |
| 42 | for (tlbsel = 0; tlbsel < 2; tlbsel++) { |
| 43 | printk("Guest TLB%d:\n", tlbsel); |
| 44 | for (i = 0; i < vcpu_e500->guest_tlb_size[tlbsel]; i++) { |
| 45 | tlbe = &vcpu_e500->guest_tlb[tlbsel][i]; |
| 46 | if (tlbe->mas1 & MAS1_VALID) |
| 47 | printk(" G[%d][%3d] | %08X | %08X | %08X | %08X |\n", |
| 48 | tlbsel, i, tlbe->mas1, tlbe->mas2, |
| 49 | tlbe->mas3, tlbe->mas7); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | for (tlbsel = 0; tlbsel < 2; tlbsel++) { |
| 54 | printk("Shadow TLB%d:\n", tlbsel); |
| 55 | for (i = 0; i < vcpu_e500->shadow_tlb_size[tlbsel]; i++) { |
| 56 | tlbe = &vcpu_e500->shadow_tlb[tlbsel][i]; |
| 57 | if (tlbe->mas1 & MAS1_VALID) |
| 58 | printk(" S[%d][%3d] | %08X | %08X | %08X | %08X |\n", |
| 59 | tlbsel, i, tlbe->mas1, tlbe->mas2, |
| 60 | tlbe->mas3, tlbe->mas7); |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | static inline unsigned int tlb0_get_next_victim( |
| 66 | struct kvmppc_vcpu_e500 *vcpu_e500) |
| 67 | { |
| 68 | unsigned int victim; |
| 69 | |
| 70 | victim = vcpu_e500->guest_tlb_nv[0]++; |
| 71 | if (unlikely(vcpu_e500->guest_tlb_nv[0] >= KVM_E500_TLB0_WAY_NUM)) |
| 72 | vcpu_e500->guest_tlb_nv[0] = 0; |
| 73 | |
| 74 | return victim; |
| 75 | } |
| 76 | |
| 77 | static inline unsigned int tlb1_max_shadow_size(void) |
| 78 | { |
Scott Wood | a4cd8b2 | 2011-06-14 18:34:41 -0500 | [diff] [blame^] | 79 | /* reserve one entry for magic page */ |
| 80 | return tlb1_entry_num - tlbcam_index - 1; |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | static inline int tlbe_is_writable(struct tlbe *tlbe) |
| 84 | { |
| 85 | return tlbe->mas3 & (MAS3_SW|MAS3_UW); |
| 86 | } |
| 87 | |
| 88 | static inline u32 e500_shadow_mas3_attrib(u32 mas3, int usermode) |
| 89 | { |
| 90 | /* Mask off reserved bits. */ |
| 91 | mas3 &= MAS3_ATTRIB_MASK; |
| 92 | |
| 93 | if (!usermode) { |
| 94 | /* Guest is in supervisor mode, |
| 95 | * so we need to translate guest |
| 96 | * supervisor permissions into user permissions. */ |
| 97 | mas3 &= ~E500_TLB_USER_PERM_MASK; |
| 98 | mas3 |= (mas3 & E500_TLB_SUPER_PERM_MASK) << 1; |
| 99 | } |
| 100 | |
| 101 | return mas3 | E500_TLB_SUPER_PERM_MASK; |
| 102 | } |
| 103 | |
| 104 | static inline u32 e500_shadow_mas2_attrib(u32 mas2, int usermode) |
| 105 | { |
Liu Yu | 046a48b | 2009-03-17 16:57:46 +0800 | [diff] [blame] | 106 | #ifdef CONFIG_SMP |
| 107 | return (mas2 & MAS2_ATTRIB_MASK) | MAS2_M; |
| 108 | #else |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 109 | return mas2 & MAS2_ATTRIB_MASK; |
Liu Yu | 046a48b | 2009-03-17 16:57:46 +0800 | [diff] [blame] | 110 | #endif |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | /* |
| 114 | * writing shadow tlb entry to host TLB |
| 115 | */ |
Scott Wood | 0ef3099 | 2011-06-14 18:34:35 -0500 | [diff] [blame] | 116 | static inline void __write_host_tlbe(struct tlbe *stlbe, uint32_t mas0) |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 117 | { |
Scott Wood | 0ef3099 | 2011-06-14 18:34:35 -0500 | [diff] [blame] | 118 | unsigned long flags; |
| 119 | |
| 120 | local_irq_save(flags); |
| 121 | mtspr(SPRN_MAS0, mas0); |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 122 | mtspr(SPRN_MAS1, stlbe->mas1); |
| 123 | mtspr(SPRN_MAS2, stlbe->mas2); |
| 124 | mtspr(SPRN_MAS3, stlbe->mas3); |
| 125 | mtspr(SPRN_MAS7, stlbe->mas7); |
Scott Wood | 0ef3099 | 2011-06-14 18:34:35 -0500 | [diff] [blame] | 126 | asm volatile("isync; tlbwe" : : : "memory"); |
| 127 | local_irq_restore(flags); |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | static inline void write_host_tlbe(struct kvmppc_vcpu_e500 *vcpu_e500, |
| 131 | int tlbsel, int esel) |
| 132 | { |
| 133 | struct tlbe *stlbe = &vcpu_e500->shadow_tlb[tlbsel][esel]; |
| 134 | |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 135 | if (tlbsel == 0) { |
Scott Wood | 0ef3099 | 2011-06-14 18:34:35 -0500 | [diff] [blame] | 136 | __write_host_tlbe(stlbe, |
| 137 | MAS0_TLBSEL(0) | |
| 138 | MAS0_ESEL(esel & (KVM_E500_TLB0_WAY_NUM - 1))); |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 139 | } else { |
Scott Wood | 0ef3099 | 2011-06-14 18:34:35 -0500 | [diff] [blame] | 140 | __write_host_tlbe(stlbe, |
| 141 | MAS0_TLBSEL(1) | |
| 142 | MAS0_ESEL(to_htlb1_esel(esel))); |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 143 | } |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 144 | } |
| 145 | |
Scott Wood | a4cd8b2 | 2011-06-14 18:34:41 -0500 | [diff] [blame^] | 146 | void kvmppc_map_magic(struct kvm_vcpu *vcpu) |
| 147 | { |
| 148 | struct tlbe magic; |
| 149 | ulong shared_page = ((ulong)vcpu->arch.shared) & PAGE_MASK; |
| 150 | pfn_t pfn; |
| 151 | |
| 152 | pfn = (pfn_t)virt_to_phys((void *)shared_page) >> PAGE_SHIFT; |
| 153 | get_page(pfn_to_page(pfn)); |
| 154 | |
| 155 | magic.mas1 = MAS1_VALID | MAS1_TS | |
| 156 | MAS1_TSIZE(BOOK3E_PAGESZ_4K); |
| 157 | magic.mas2 = vcpu->arch.magic_page_ea | MAS2_M; |
| 158 | magic.mas3 = (pfn << PAGE_SHIFT) | |
| 159 | MAS3_SW | MAS3_SR | MAS3_UW | MAS3_UR; |
| 160 | magic.mas7 = pfn >> (32 - PAGE_SHIFT); |
| 161 | |
| 162 | __write_host_tlbe(&magic, MAS0_TLBSEL(1) | MAS0_ESEL(tlbcam_index)); |
| 163 | } |
| 164 | |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 165 | void kvmppc_e500_tlb_load(struct kvm_vcpu *vcpu, int cpu) |
| 166 | { |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | void kvmppc_e500_tlb_put(struct kvm_vcpu *vcpu) |
| 170 | { |
Liu Yu | 9aa4dd5 | 2009-01-14 10:47:38 -0600 | [diff] [blame] | 171 | _tlbil_all(); |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | /* Search the guest TLB for a matching entry. */ |
| 175 | static int kvmppc_e500_tlb_index(struct kvmppc_vcpu_e500 *vcpu_e500, |
| 176 | gva_t eaddr, int tlbsel, unsigned int pid, int as) |
| 177 | { |
| 178 | int i; |
| 179 | |
| 180 | /* XXX Replace loop with fancy data structures. */ |
| 181 | for (i = 0; i < vcpu_e500->guest_tlb_size[tlbsel]; i++) { |
| 182 | struct tlbe *tlbe = &vcpu_e500->guest_tlb[tlbsel][i]; |
| 183 | unsigned int tid; |
| 184 | |
| 185 | if (eaddr < get_tlb_eaddr(tlbe)) |
| 186 | continue; |
| 187 | |
| 188 | if (eaddr > get_tlb_end(tlbe)) |
| 189 | continue; |
| 190 | |
| 191 | tid = get_tlb_tid(tlbe); |
| 192 | if (tid && (tid != pid)) |
| 193 | continue; |
| 194 | |
| 195 | if (!get_tlb_v(tlbe)) |
| 196 | continue; |
| 197 | |
| 198 | if (get_tlb_ts(tlbe) != as && as != -1) |
| 199 | continue; |
| 200 | |
| 201 | return i; |
| 202 | } |
| 203 | |
| 204 | return -1; |
| 205 | } |
| 206 | |
| 207 | static void kvmppc_e500_shadow_release(struct kvmppc_vcpu_e500 *vcpu_e500, |
| 208 | int tlbsel, int esel) |
| 209 | { |
| 210 | struct tlbe *stlbe = &vcpu_e500->shadow_tlb[tlbsel][esel]; |
Scott Wood | 59c1f4e | 2011-06-14 18:34:37 -0500 | [diff] [blame] | 211 | unsigned long pfn; |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 212 | |
Scott Wood | 59c1f4e | 2011-06-14 18:34:37 -0500 | [diff] [blame] | 213 | pfn = stlbe->mas3 >> PAGE_SHIFT; |
| 214 | pfn |= stlbe->mas7 << (32 - PAGE_SHIFT); |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 215 | |
Scott Wood | 59c1f4e | 2011-06-14 18:34:37 -0500 | [diff] [blame] | 216 | if (get_tlb_v(stlbe)) { |
| 217 | if (tlbe_is_writable(stlbe)) |
| 218 | kvm_release_pfn_dirty(pfn); |
| 219 | else |
| 220 | kvm_release_pfn_clean(pfn); |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 221 | } |
| 222 | } |
| 223 | |
| 224 | static void kvmppc_e500_stlbe_invalidate(struct kvmppc_vcpu_e500 *vcpu_e500, |
| 225 | int tlbsel, int esel) |
| 226 | { |
| 227 | struct tlbe *stlbe = &vcpu_e500->shadow_tlb[tlbsel][esel]; |
| 228 | |
| 229 | kvmppc_e500_shadow_release(vcpu_e500, tlbsel, esel); |
| 230 | stlbe->mas1 = 0; |
Kyle Moffett | 21e537b | 2010-08-30 11:38:39 -0400 | [diff] [blame] | 231 | trace_kvm_stlb_inval(index_of(tlbsel, esel)); |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | static void kvmppc_e500_tlb1_invalidate(struct kvmppc_vcpu_e500 *vcpu_e500, |
| 235 | gva_t eaddr, gva_t eend, u32 tid) |
| 236 | { |
| 237 | unsigned int pid = tid & 0xff; |
| 238 | unsigned int i; |
| 239 | |
| 240 | /* XXX Replace loop with fancy data structures. */ |
| 241 | for (i = 0; i < vcpu_e500->guest_tlb_size[1]; i++) { |
| 242 | struct tlbe *stlbe = &vcpu_e500->shadow_tlb[1][i]; |
| 243 | unsigned int tid; |
| 244 | |
| 245 | if (!get_tlb_v(stlbe)) |
| 246 | continue; |
| 247 | |
| 248 | if (eend < get_tlb_eaddr(stlbe)) |
| 249 | continue; |
| 250 | |
| 251 | if (eaddr > get_tlb_end(stlbe)) |
| 252 | continue; |
| 253 | |
| 254 | tid = get_tlb_tid(stlbe); |
| 255 | if (tid && (tid != pid)) |
| 256 | continue; |
| 257 | |
| 258 | kvmppc_e500_stlbe_invalidate(vcpu_e500, 1, i); |
| 259 | write_host_tlbe(vcpu_e500, 1, i); |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | static inline void kvmppc_e500_deliver_tlb_miss(struct kvm_vcpu *vcpu, |
| 264 | unsigned int eaddr, int as) |
| 265 | { |
| 266 | struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu); |
| 267 | unsigned int victim, pidsel, tsized; |
| 268 | int tlbsel; |
| 269 | |
Liu Yu | fb2838d | 2009-01-14 10:47:37 -0600 | [diff] [blame] | 270 | /* since we only have two TLBs, only lower bit is used. */ |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 271 | tlbsel = (vcpu_e500->mas4 >> 28) & 0x1; |
| 272 | victim = (tlbsel == 0) ? tlb0_get_next_victim(vcpu_e500) : 0; |
| 273 | pidsel = (vcpu_e500->mas4 >> 16) & 0xf; |
Liu Yu | 0cfb50e | 2009-06-05 14:54:29 +0800 | [diff] [blame] | 274 | tsized = (vcpu_e500->mas4 >> 7) & 0x1f; |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 275 | |
| 276 | vcpu_e500->mas0 = MAS0_TLBSEL(tlbsel) | MAS0_ESEL(victim) |
| 277 | | MAS0_NV(vcpu_e500->guest_tlb_nv[tlbsel]); |
| 278 | vcpu_e500->mas1 = MAS1_VALID | (as ? MAS1_TS : 0) |
| 279 | | MAS1_TID(vcpu_e500->pid[pidsel]) |
| 280 | | MAS1_TSIZE(tsized); |
| 281 | vcpu_e500->mas2 = (eaddr & MAS2_EPN) |
| 282 | | (vcpu_e500->mas4 & MAS2_ATTRIB_MASK); |
| 283 | vcpu_e500->mas3 &= MAS3_U0 | MAS3_U1 | MAS3_U2 | MAS3_U3; |
| 284 | vcpu_e500->mas6 = (vcpu_e500->mas6 & MAS6_SPID1) |
| 285 | | (get_cur_pid(vcpu) << 16) |
| 286 | | (as ? MAS6_SAS : 0); |
| 287 | vcpu_e500->mas7 = 0; |
| 288 | } |
| 289 | |
| 290 | static inline void kvmppc_e500_shadow_map(struct kvmppc_vcpu_e500 *vcpu_e500, |
| 291 | u64 gvaddr, gfn_t gfn, struct tlbe *gtlbe, int tlbsel, int esel) |
| 292 | { |
Scott Wood | 9973d54 | 2011-06-14 18:34:39 -0500 | [diff] [blame] | 293 | struct kvm_memory_slot *slot; |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 294 | struct tlbe *stlbe; |
Scott Wood | 9973d54 | 2011-06-14 18:34:39 -0500 | [diff] [blame] | 295 | unsigned long pfn, hva; |
| 296 | int pfnmap = 0; |
| 297 | int tsize = BOOK3E_PAGESZ_4K; |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 298 | |
| 299 | stlbe = &vcpu_e500->shadow_tlb[tlbsel][esel]; |
| 300 | |
Scott Wood | 59c1f4e | 2011-06-14 18:34:37 -0500 | [diff] [blame] | 301 | /* |
| 302 | * Translate guest physical to true physical, acquiring |
| 303 | * a page reference if it is normal, non-reserved memory. |
Scott Wood | 9973d54 | 2011-06-14 18:34:39 -0500 | [diff] [blame] | 304 | * |
| 305 | * gfn_to_memslot() must succeed because otherwise we wouldn't |
| 306 | * have gotten this far. Eventually we should just pass the slot |
| 307 | * pointer through from the first lookup. |
Scott Wood | 59c1f4e | 2011-06-14 18:34:37 -0500 | [diff] [blame] | 308 | */ |
Scott Wood | 9973d54 | 2011-06-14 18:34:39 -0500 | [diff] [blame] | 309 | slot = gfn_to_memslot(vcpu_e500->vcpu.kvm, gfn); |
| 310 | hva = gfn_to_hva_memslot(slot, gfn); |
| 311 | |
| 312 | if (tlbsel == 1) { |
| 313 | struct vm_area_struct *vma; |
| 314 | down_read(¤t->mm->mmap_sem); |
| 315 | |
| 316 | vma = find_vma(current->mm, hva); |
| 317 | if (vma && hva >= vma->vm_start && |
| 318 | (vma->vm_flags & VM_PFNMAP)) { |
| 319 | /* |
| 320 | * This VMA is a physically contiguous region (e.g. |
| 321 | * /dev/mem) that bypasses normal Linux page |
| 322 | * management. Find the overlap between the |
| 323 | * vma and the memslot. |
| 324 | */ |
| 325 | |
| 326 | unsigned long start, end; |
| 327 | unsigned long slot_start, slot_end; |
| 328 | |
| 329 | pfnmap = 1; |
| 330 | |
| 331 | start = vma->vm_pgoff; |
| 332 | end = start + |
| 333 | ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT); |
| 334 | |
| 335 | pfn = start + ((hva - vma->vm_start) >> PAGE_SHIFT); |
| 336 | |
| 337 | slot_start = pfn - (gfn - slot->base_gfn); |
| 338 | slot_end = slot_start + slot->npages; |
| 339 | |
| 340 | if (start < slot_start) |
| 341 | start = slot_start; |
| 342 | if (end > slot_end) |
| 343 | end = slot_end; |
| 344 | |
| 345 | tsize = (gtlbe->mas1 & MAS1_TSIZE_MASK) >> |
| 346 | MAS1_TSIZE_SHIFT; |
| 347 | |
| 348 | /* |
| 349 | * e500 doesn't implement the lowest tsize bit, |
| 350 | * or 1K pages. |
| 351 | */ |
| 352 | tsize = max(BOOK3E_PAGESZ_4K, tsize & ~1); |
| 353 | |
| 354 | /* |
| 355 | * Now find the largest tsize (up to what the guest |
| 356 | * requested) that will cover gfn, stay within the |
| 357 | * range, and for which gfn and pfn are mutually |
| 358 | * aligned. |
| 359 | */ |
| 360 | |
| 361 | for (; tsize > BOOK3E_PAGESZ_4K; tsize -= 2) { |
| 362 | unsigned long gfn_start, gfn_end, tsize_pages; |
| 363 | tsize_pages = 1 << (tsize - 2); |
| 364 | |
| 365 | gfn_start = gfn & ~(tsize_pages - 1); |
| 366 | gfn_end = gfn_start + tsize_pages; |
| 367 | |
| 368 | if (gfn_start + pfn - gfn < start) |
| 369 | continue; |
| 370 | if (gfn_end + pfn - gfn > end) |
| 371 | continue; |
| 372 | if ((gfn & (tsize_pages - 1)) != |
| 373 | (pfn & (tsize_pages - 1))) |
| 374 | continue; |
| 375 | |
| 376 | gvaddr &= ~((tsize_pages << PAGE_SHIFT) - 1); |
| 377 | pfn &= ~(tsize_pages - 1); |
| 378 | break; |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | up_read(¤t->mm->mmap_sem); |
| 383 | } |
| 384 | |
| 385 | if (likely(!pfnmap)) { |
| 386 | pfn = gfn_to_pfn_memslot(vcpu_e500->vcpu.kvm, slot, gfn); |
| 387 | if (is_error_pfn(pfn)) { |
| 388 | printk(KERN_ERR "Couldn't get real page for gfn %lx!\n", |
| 389 | (long)gfn); |
| 390 | kvm_release_pfn_clean(pfn); |
| 391 | return; |
| 392 | } |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 393 | } |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 394 | |
| 395 | /* Drop reference to old page. */ |
| 396 | kvmppc_e500_shadow_release(vcpu_e500, tlbsel, esel); |
| 397 | |
Scott Wood | 9973d54 | 2011-06-14 18:34:39 -0500 | [diff] [blame] | 398 | /* Force TS=1 IPROT=0 for all guest mappings. */ |
| 399 | stlbe->mas1 = MAS1_TSIZE(tsize) |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 400 | | MAS1_TID(get_tlb_tid(gtlbe)) | MAS1_TS | MAS1_VALID; |
| 401 | stlbe->mas2 = (gvaddr & MAS2_EPN) |
| 402 | | e500_shadow_mas2_attrib(gtlbe->mas2, |
Alexander Graf | 666e725 | 2010-07-29 14:47:43 +0200 | [diff] [blame] | 403 | vcpu_e500->vcpu.arch.shared->msr & MSR_PR); |
Scott Wood | 59c1f4e | 2011-06-14 18:34:37 -0500 | [diff] [blame] | 404 | stlbe->mas3 = ((pfn << PAGE_SHIFT) & MAS3_RPN) |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 405 | | e500_shadow_mas3_attrib(gtlbe->mas3, |
Alexander Graf | 666e725 | 2010-07-29 14:47:43 +0200 | [diff] [blame] | 406 | vcpu_e500->vcpu.arch.shared->msr & MSR_PR); |
Scott Wood | 59c1f4e | 2011-06-14 18:34:37 -0500 | [diff] [blame] | 407 | stlbe->mas7 = (pfn >> (32 - PAGE_SHIFT)) & MAS7_RPN; |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 408 | |
Marcelo Tosatti | 46f43c6 | 2009-06-18 11:47:27 -0300 | [diff] [blame] | 409 | trace_kvm_stlb_write(index_of(tlbsel, esel), stlbe->mas1, stlbe->mas2, |
| 410 | stlbe->mas3, stlbe->mas7); |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | /* XXX only map the one-one case, for now use TLB0 */ |
| 414 | static int kvmppc_e500_stlbe_map(struct kvmppc_vcpu_e500 *vcpu_e500, |
| 415 | int tlbsel, int esel) |
| 416 | { |
| 417 | struct tlbe *gtlbe; |
| 418 | |
| 419 | gtlbe = &vcpu_e500->guest_tlb[tlbsel][esel]; |
| 420 | |
| 421 | kvmppc_e500_shadow_map(vcpu_e500, get_tlb_eaddr(gtlbe), |
| 422 | get_tlb_raddr(gtlbe) >> PAGE_SHIFT, |
| 423 | gtlbe, tlbsel, esel); |
| 424 | |
| 425 | return esel; |
| 426 | } |
| 427 | |
| 428 | /* Caller must ensure that the specified guest TLB entry is safe to insert into |
| 429 | * the shadow TLB. */ |
| 430 | /* XXX for both one-one and one-to-many , for now use TLB1 */ |
| 431 | static int kvmppc_e500_tlb1_map(struct kvmppc_vcpu_e500 *vcpu_e500, |
| 432 | u64 gvaddr, gfn_t gfn, struct tlbe *gtlbe) |
| 433 | { |
| 434 | unsigned int victim; |
| 435 | |
| 436 | victim = vcpu_e500->guest_tlb_nv[1]++; |
| 437 | |
| 438 | if (unlikely(vcpu_e500->guest_tlb_nv[1] >= tlb1_max_shadow_size())) |
| 439 | vcpu_e500->guest_tlb_nv[1] = 0; |
| 440 | |
| 441 | kvmppc_e500_shadow_map(vcpu_e500, gvaddr, gfn, gtlbe, 1, victim); |
| 442 | |
| 443 | return victim; |
| 444 | } |
| 445 | |
| 446 | /* Invalidate all guest kernel mappings when enter usermode, |
| 447 | * so that when they fault back in they will get the |
| 448 | * proper permission bits. */ |
| 449 | void kvmppc_mmu_priv_switch(struct kvm_vcpu *vcpu, int usermode) |
| 450 | { |
| 451 | if (usermode) { |
| 452 | struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu); |
| 453 | int i; |
| 454 | |
| 455 | /* XXX Replace loop with fancy data structures. */ |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 456 | for (i = 0; i < tlb1_max_shadow_size(); i++) |
| 457 | kvmppc_e500_stlbe_invalidate(vcpu_e500, 1, i); |
| 458 | |
Liu Yu | 9aa4dd5 | 2009-01-14 10:47:38 -0600 | [diff] [blame] | 459 | _tlbil_all(); |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 460 | } |
| 461 | } |
| 462 | |
| 463 | static int kvmppc_e500_gtlbe_invalidate(struct kvmppc_vcpu_e500 *vcpu_e500, |
| 464 | int tlbsel, int esel) |
| 465 | { |
| 466 | struct tlbe *gtlbe = &vcpu_e500->guest_tlb[tlbsel][esel]; |
| 467 | |
| 468 | if (unlikely(get_tlb_iprot(gtlbe))) |
| 469 | return -1; |
| 470 | |
| 471 | if (tlbsel == 1) { |
| 472 | kvmppc_e500_tlb1_invalidate(vcpu_e500, get_tlb_eaddr(gtlbe), |
| 473 | get_tlb_end(gtlbe), |
| 474 | get_tlb_tid(gtlbe)); |
| 475 | } else { |
| 476 | kvmppc_e500_stlbe_invalidate(vcpu_e500, tlbsel, esel); |
| 477 | } |
| 478 | |
| 479 | gtlbe->mas1 = 0; |
| 480 | |
| 481 | return 0; |
| 482 | } |
| 483 | |
Liu Yu | b0a1835 | 2009-02-17 16:52:08 +0800 | [diff] [blame] | 484 | int kvmppc_e500_emul_mt_mmucsr0(struct kvmppc_vcpu_e500 *vcpu_e500, ulong value) |
| 485 | { |
| 486 | int esel; |
| 487 | |
| 488 | if (value & MMUCSR0_TLB0FI) |
| 489 | for (esel = 0; esel < vcpu_e500->guest_tlb_size[0]; esel++) |
| 490 | kvmppc_e500_gtlbe_invalidate(vcpu_e500, 0, esel); |
| 491 | if (value & MMUCSR0_TLB1FI) |
| 492 | for (esel = 0; esel < vcpu_e500->guest_tlb_size[1]; esel++) |
| 493 | kvmppc_e500_gtlbe_invalidate(vcpu_e500, 1, esel); |
| 494 | |
| 495 | _tlbil_all(); |
| 496 | |
| 497 | return EMULATE_DONE; |
| 498 | } |
| 499 | |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 500 | int kvmppc_e500_emul_tlbivax(struct kvm_vcpu *vcpu, int ra, int rb) |
| 501 | { |
| 502 | struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu); |
| 503 | unsigned int ia; |
| 504 | int esel, tlbsel; |
| 505 | gva_t ea; |
| 506 | |
Alexander Graf | 8e5b26b | 2010-01-08 02:58:01 +0100 | [diff] [blame] | 507 | ea = ((ra) ? kvmppc_get_gpr(vcpu, ra) : 0) + kvmppc_get_gpr(vcpu, rb); |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 508 | |
| 509 | ia = (ea >> 2) & 0x1; |
| 510 | |
Liu Yu | fb2838d | 2009-01-14 10:47:37 -0600 | [diff] [blame] | 511 | /* since we only have two TLBs, only lower bit is used. */ |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 512 | tlbsel = (ea >> 3) & 0x1; |
| 513 | |
| 514 | if (ia) { |
| 515 | /* invalidate all entries */ |
| 516 | for (esel = 0; esel < vcpu_e500->guest_tlb_size[tlbsel]; esel++) |
| 517 | kvmppc_e500_gtlbe_invalidate(vcpu_e500, tlbsel, esel); |
| 518 | } else { |
| 519 | ea &= 0xfffff000; |
| 520 | esel = kvmppc_e500_tlb_index(vcpu_e500, ea, tlbsel, |
| 521 | get_cur_pid(vcpu), -1); |
| 522 | if (esel >= 0) |
| 523 | kvmppc_e500_gtlbe_invalidate(vcpu_e500, tlbsel, esel); |
| 524 | } |
| 525 | |
Liu Yu | 9aa4dd5 | 2009-01-14 10:47:38 -0600 | [diff] [blame] | 526 | _tlbil_all(); |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 527 | |
| 528 | return EMULATE_DONE; |
| 529 | } |
| 530 | |
| 531 | int kvmppc_e500_emul_tlbre(struct kvm_vcpu *vcpu) |
| 532 | { |
| 533 | struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu); |
| 534 | int tlbsel, esel; |
| 535 | struct tlbe *gtlbe; |
| 536 | |
| 537 | tlbsel = get_tlb_tlbsel(vcpu_e500); |
| 538 | esel = get_tlb_esel(vcpu_e500, tlbsel); |
| 539 | |
| 540 | gtlbe = &vcpu_e500->guest_tlb[tlbsel][esel]; |
Liu Yu | bc35cbc | 2009-03-17 16:57:45 +0800 | [diff] [blame] | 541 | vcpu_e500->mas0 &= ~MAS0_NV(~0); |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 542 | vcpu_e500->mas0 |= MAS0_NV(vcpu_e500->guest_tlb_nv[tlbsel]); |
| 543 | vcpu_e500->mas1 = gtlbe->mas1; |
| 544 | vcpu_e500->mas2 = gtlbe->mas2; |
| 545 | vcpu_e500->mas3 = gtlbe->mas3; |
| 546 | vcpu_e500->mas7 = gtlbe->mas7; |
| 547 | |
| 548 | return EMULATE_DONE; |
| 549 | } |
| 550 | |
| 551 | int kvmppc_e500_emul_tlbsx(struct kvm_vcpu *vcpu, int rb) |
| 552 | { |
| 553 | struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu); |
| 554 | int as = !!get_cur_sas(vcpu_e500); |
| 555 | unsigned int pid = get_cur_spid(vcpu_e500); |
| 556 | int esel, tlbsel; |
| 557 | struct tlbe *gtlbe = NULL; |
| 558 | gva_t ea; |
| 559 | |
Alexander Graf | 8e5b26b | 2010-01-08 02:58:01 +0100 | [diff] [blame] | 560 | ea = kvmppc_get_gpr(vcpu, rb); |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 561 | |
| 562 | for (tlbsel = 0; tlbsel < 2; tlbsel++) { |
| 563 | esel = kvmppc_e500_tlb_index(vcpu_e500, ea, tlbsel, pid, as); |
| 564 | if (esel >= 0) { |
| 565 | gtlbe = &vcpu_e500->guest_tlb[tlbsel][esel]; |
| 566 | break; |
| 567 | } |
| 568 | } |
| 569 | |
| 570 | if (gtlbe) { |
| 571 | vcpu_e500->mas0 = MAS0_TLBSEL(tlbsel) | MAS0_ESEL(esel) |
| 572 | | MAS0_NV(vcpu_e500->guest_tlb_nv[tlbsel]); |
| 573 | vcpu_e500->mas1 = gtlbe->mas1; |
| 574 | vcpu_e500->mas2 = gtlbe->mas2; |
| 575 | vcpu_e500->mas3 = gtlbe->mas3; |
| 576 | vcpu_e500->mas7 = gtlbe->mas7; |
| 577 | } else { |
| 578 | int victim; |
| 579 | |
Liu Yu | fb2838d | 2009-01-14 10:47:37 -0600 | [diff] [blame] | 580 | /* since we only have two TLBs, only lower bit is used. */ |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 581 | tlbsel = vcpu_e500->mas4 >> 28 & 0x1; |
| 582 | victim = (tlbsel == 0) ? tlb0_get_next_victim(vcpu_e500) : 0; |
| 583 | |
| 584 | vcpu_e500->mas0 = MAS0_TLBSEL(tlbsel) | MAS0_ESEL(victim) |
| 585 | | MAS0_NV(vcpu_e500->guest_tlb_nv[tlbsel]); |
| 586 | vcpu_e500->mas1 = (vcpu_e500->mas6 & MAS6_SPID0) |
| 587 | | (vcpu_e500->mas6 & (MAS6_SAS ? MAS1_TS : 0)) |
| 588 | | (vcpu_e500->mas4 & MAS4_TSIZED(~0)); |
| 589 | vcpu_e500->mas2 &= MAS2_EPN; |
| 590 | vcpu_e500->mas2 |= vcpu_e500->mas4 & MAS2_ATTRIB_MASK; |
| 591 | vcpu_e500->mas3 &= MAS3_U0 | MAS3_U1 | MAS3_U2 | MAS3_U3; |
| 592 | vcpu_e500->mas7 = 0; |
| 593 | } |
| 594 | |
Scott Wood | 49ea069 | 2011-03-28 15:01:24 -0500 | [diff] [blame] | 595 | kvmppc_set_exit_type(vcpu, EMULATED_TLBSX_EXITS); |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 596 | return EMULATE_DONE; |
| 597 | } |
| 598 | |
| 599 | int kvmppc_e500_emul_tlbwe(struct kvm_vcpu *vcpu) |
| 600 | { |
| 601 | struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu); |
| 602 | u64 eaddr; |
| 603 | u64 raddr; |
| 604 | u32 tid; |
| 605 | struct tlbe *gtlbe; |
| 606 | int tlbsel, esel, stlbsel, sesel; |
| 607 | |
| 608 | tlbsel = get_tlb_tlbsel(vcpu_e500); |
| 609 | esel = get_tlb_esel(vcpu_e500, tlbsel); |
| 610 | |
| 611 | gtlbe = &vcpu_e500->guest_tlb[tlbsel][esel]; |
| 612 | |
| 613 | if (get_tlb_v(gtlbe) && tlbsel == 1) { |
| 614 | eaddr = get_tlb_eaddr(gtlbe); |
| 615 | tid = get_tlb_tid(gtlbe); |
| 616 | kvmppc_e500_tlb1_invalidate(vcpu_e500, eaddr, |
| 617 | get_tlb_end(gtlbe), tid); |
| 618 | } |
| 619 | |
| 620 | gtlbe->mas1 = vcpu_e500->mas1; |
| 621 | gtlbe->mas2 = vcpu_e500->mas2; |
| 622 | gtlbe->mas3 = vcpu_e500->mas3; |
| 623 | gtlbe->mas7 = vcpu_e500->mas7; |
| 624 | |
Marcelo Tosatti | 46f43c6 | 2009-06-18 11:47:27 -0300 | [diff] [blame] | 625 | trace_kvm_gtlb_write(vcpu_e500->mas0, gtlbe->mas1, gtlbe->mas2, |
| 626 | gtlbe->mas3, gtlbe->mas7); |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 627 | |
| 628 | /* Invalidate shadow mappings for the about-to-be-clobbered TLBE. */ |
| 629 | if (tlbe_is_host_safe(vcpu, gtlbe)) { |
| 630 | switch (tlbsel) { |
| 631 | case 0: |
| 632 | /* TLB0 */ |
| 633 | gtlbe->mas1 &= ~MAS1_TSIZE(~0); |
Liu Yu | 0cfb50e | 2009-06-05 14:54:29 +0800 | [diff] [blame] | 634 | gtlbe->mas1 |= MAS1_TSIZE(BOOK3E_PAGESZ_4K); |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 635 | |
| 636 | stlbsel = 0; |
| 637 | sesel = kvmppc_e500_stlbe_map(vcpu_e500, 0, esel); |
| 638 | |
| 639 | break; |
| 640 | |
| 641 | case 1: |
| 642 | /* TLB1 */ |
| 643 | eaddr = get_tlb_eaddr(gtlbe); |
| 644 | raddr = get_tlb_raddr(gtlbe); |
| 645 | |
| 646 | /* Create a 4KB mapping on the host. |
| 647 | * If the guest wanted a large page, |
| 648 | * only the first 4KB is mapped here and the rest |
| 649 | * are mapped on the fly. */ |
| 650 | stlbsel = 1; |
| 651 | sesel = kvmppc_e500_tlb1_map(vcpu_e500, eaddr, |
| 652 | raddr >> PAGE_SHIFT, gtlbe); |
| 653 | break; |
| 654 | |
| 655 | default: |
| 656 | BUG(); |
| 657 | } |
| 658 | write_host_tlbe(vcpu_e500, stlbsel, sesel); |
| 659 | } |
| 660 | |
Scott Wood | 49ea069 | 2011-03-28 15:01:24 -0500 | [diff] [blame] | 661 | kvmppc_set_exit_type(vcpu, EMULATED_TLBWE_EXITS); |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 662 | return EMULATE_DONE; |
| 663 | } |
| 664 | |
| 665 | int kvmppc_mmu_itlb_index(struct kvm_vcpu *vcpu, gva_t eaddr) |
| 666 | { |
Alexander Graf | 666e725 | 2010-07-29 14:47:43 +0200 | [diff] [blame] | 667 | unsigned int as = !!(vcpu->arch.shared->msr & MSR_IS); |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 668 | |
| 669 | return kvmppc_e500_tlb_search(vcpu, eaddr, get_cur_pid(vcpu), as); |
| 670 | } |
| 671 | |
| 672 | int kvmppc_mmu_dtlb_index(struct kvm_vcpu *vcpu, gva_t eaddr) |
| 673 | { |
Alexander Graf | 666e725 | 2010-07-29 14:47:43 +0200 | [diff] [blame] | 674 | unsigned int as = !!(vcpu->arch.shared->msr & MSR_DS); |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 675 | |
| 676 | return kvmppc_e500_tlb_search(vcpu, eaddr, get_cur_pid(vcpu), as); |
| 677 | } |
| 678 | |
| 679 | void kvmppc_mmu_itlb_miss(struct kvm_vcpu *vcpu) |
| 680 | { |
Alexander Graf | 666e725 | 2010-07-29 14:47:43 +0200 | [diff] [blame] | 681 | unsigned int as = !!(vcpu->arch.shared->msr & MSR_IS); |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 682 | |
| 683 | kvmppc_e500_deliver_tlb_miss(vcpu, vcpu->arch.pc, as); |
| 684 | } |
| 685 | |
| 686 | void kvmppc_mmu_dtlb_miss(struct kvm_vcpu *vcpu) |
| 687 | { |
Alexander Graf | 666e725 | 2010-07-29 14:47:43 +0200 | [diff] [blame] | 688 | unsigned int as = !!(vcpu->arch.shared->msr & MSR_DS); |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 689 | |
| 690 | kvmppc_e500_deliver_tlb_miss(vcpu, vcpu->arch.fault_dear, as); |
| 691 | } |
| 692 | |
| 693 | gpa_t kvmppc_mmu_xlate(struct kvm_vcpu *vcpu, unsigned int index, |
| 694 | gva_t eaddr) |
| 695 | { |
| 696 | struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu); |
| 697 | struct tlbe *gtlbe = |
| 698 | &vcpu_e500->guest_tlb[tlbsel_of(index)][esel_of(index)]; |
| 699 | u64 pgmask = get_tlb_bytes(gtlbe) - 1; |
| 700 | |
| 701 | return get_tlb_raddr(gtlbe) | (eaddr & pgmask); |
| 702 | } |
| 703 | |
| 704 | void kvmppc_mmu_destroy(struct kvm_vcpu *vcpu) |
| 705 | { |
| 706 | struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu); |
| 707 | int tlbsel, i; |
| 708 | |
| 709 | for (tlbsel = 0; tlbsel < 2; tlbsel++) |
| 710 | for (i = 0; i < vcpu_e500->guest_tlb_size[tlbsel]; i++) |
| 711 | kvmppc_e500_shadow_release(vcpu_e500, tlbsel, i); |
| 712 | |
| 713 | /* discard all guest mapping */ |
Liu Yu | 9aa4dd5 | 2009-01-14 10:47:38 -0600 | [diff] [blame] | 714 | _tlbil_all(); |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 715 | } |
| 716 | |
| 717 | void kvmppc_mmu_map(struct kvm_vcpu *vcpu, u64 eaddr, gpa_t gpaddr, |
| 718 | unsigned int index) |
| 719 | { |
| 720 | struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu); |
| 721 | int tlbsel = tlbsel_of(index); |
| 722 | int esel = esel_of(index); |
| 723 | int stlbsel, sesel; |
| 724 | |
| 725 | switch (tlbsel) { |
| 726 | case 0: |
| 727 | stlbsel = 0; |
| 728 | sesel = esel; |
| 729 | break; |
| 730 | |
| 731 | case 1: { |
| 732 | gfn_t gfn = gpaddr >> PAGE_SHIFT; |
| 733 | struct tlbe *gtlbe |
| 734 | = &vcpu_e500->guest_tlb[tlbsel][esel]; |
| 735 | |
| 736 | stlbsel = 1; |
| 737 | sesel = kvmppc_e500_tlb1_map(vcpu_e500, eaddr, gfn, gtlbe); |
| 738 | break; |
| 739 | } |
| 740 | |
| 741 | default: |
| 742 | BUG(); |
| 743 | break; |
| 744 | } |
| 745 | write_host_tlbe(vcpu_e500, stlbsel, sesel); |
| 746 | } |
| 747 | |
| 748 | int kvmppc_e500_tlb_search(struct kvm_vcpu *vcpu, |
| 749 | gva_t eaddr, unsigned int pid, int as) |
| 750 | { |
| 751 | struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu); |
| 752 | int esel, tlbsel; |
| 753 | |
| 754 | for (tlbsel = 0; tlbsel < 2; tlbsel++) { |
| 755 | esel = kvmppc_e500_tlb_index(vcpu_e500, eaddr, tlbsel, pid, as); |
| 756 | if (esel >= 0) |
| 757 | return index_of(tlbsel, esel); |
| 758 | } |
| 759 | |
| 760 | return -1; |
| 761 | } |
| 762 | |
Scott Wood | 5ce941e | 2011-04-27 17:24:21 -0500 | [diff] [blame] | 763 | void kvmppc_set_pid(struct kvm_vcpu *vcpu, u32 pid) |
| 764 | { |
| 765 | struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu); |
| 766 | |
| 767 | vcpu_e500->pid[0] = vcpu->arch.shadow_pid = |
| 768 | vcpu->arch.pid = pid; |
| 769 | } |
| 770 | |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 771 | void kvmppc_e500_tlb_setup(struct kvmppc_vcpu_e500 *vcpu_e500) |
| 772 | { |
| 773 | struct tlbe *tlbe; |
| 774 | |
| 775 | /* Insert large initial mapping for guest. */ |
| 776 | tlbe = &vcpu_e500->guest_tlb[1][0]; |
Liu Yu | 0cfb50e | 2009-06-05 14:54:29 +0800 | [diff] [blame] | 777 | tlbe->mas1 = MAS1_VALID | MAS1_TSIZE(BOOK3E_PAGESZ_256M); |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 778 | tlbe->mas2 = 0; |
| 779 | tlbe->mas3 = E500_TLB_SUPER_PERM_MASK; |
| 780 | tlbe->mas7 = 0; |
| 781 | |
| 782 | /* 4K map for serial output. Used by kernel wrapper. */ |
| 783 | tlbe = &vcpu_e500->guest_tlb[1][1]; |
Liu Yu | 0cfb50e | 2009-06-05 14:54:29 +0800 | [diff] [blame] | 784 | tlbe->mas1 = MAS1_VALID | MAS1_TSIZE(BOOK3E_PAGESZ_4K); |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 785 | tlbe->mas2 = (0xe0004500 & 0xFFFFF000) | MAS2_I | MAS2_G; |
| 786 | tlbe->mas3 = (0xe0004500 & 0xFFFFF000) | E500_TLB_SUPER_PERM_MASK; |
| 787 | tlbe->mas7 = 0; |
| 788 | } |
| 789 | |
| 790 | int kvmppc_e500_tlb_init(struct kvmppc_vcpu_e500 *vcpu_e500) |
| 791 | { |
| 792 | tlb1_entry_num = mfspr(SPRN_TLB1CFG) & 0xFFF; |
| 793 | |
| 794 | vcpu_e500->guest_tlb_size[0] = KVM_E500_TLB0_SIZE; |
| 795 | vcpu_e500->guest_tlb[0] = |
| 796 | kzalloc(sizeof(struct tlbe) * KVM_E500_TLB0_SIZE, GFP_KERNEL); |
| 797 | if (vcpu_e500->guest_tlb[0] == NULL) |
| 798 | goto err_out; |
| 799 | |
| 800 | vcpu_e500->shadow_tlb_size[0] = KVM_E500_TLB0_SIZE; |
| 801 | vcpu_e500->shadow_tlb[0] = |
| 802 | kzalloc(sizeof(struct tlbe) * KVM_E500_TLB0_SIZE, GFP_KERNEL); |
| 803 | if (vcpu_e500->shadow_tlb[0] == NULL) |
| 804 | goto err_out_guest0; |
| 805 | |
| 806 | vcpu_e500->guest_tlb_size[1] = KVM_E500_TLB1_SIZE; |
| 807 | vcpu_e500->guest_tlb[1] = |
| 808 | kzalloc(sizeof(struct tlbe) * KVM_E500_TLB1_SIZE, GFP_KERNEL); |
| 809 | if (vcpu_e500->guest_tlb[1] == NULL) |
| 810 | goto err_out_shadow0; |
| 811 | |
| 812 | vcpu_e500->shadow_tlb_size[1] = tlb1_entry_num; |
| 813 | vcpu_e500->shadow_tlb[1] = |
| 814 | kzalloc(sizeof(struct tlbe) * tlb1_entry_num, GFP_KERNEL); |
| 815 | if (vcpu_e500->shadow_tlb[1] == NULL) |
| 816 | goto err_out_guest1; |
| 817 | |
Liu Yu | da15bf4 | 2010-01-22 19:36:53 +0800 | [diff] [blame] | 818 | /* Init TLB configuration register */ |
| 819 | vcpu_e500->tlb0cfg = mfspr(SPRN_TLB0CFG) & ~0xfffUL; |
| 820 | vcpu_e500->tlb0cfg |= vcpu_e500->guest_tlb_size[0]; |
| 821 | vcpu_e500->tlb1cfg = mfspr(SPRN_TLB1CFG) & ~0xfffUL; |
| 822 | vcpu_e500->tlb1cfg |= vcpu_e500->guest_tlb_size[1]; |
| 823 | |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 824 | return 0; |
| 825 | |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 826 | err_out_guest1: |
| 827 | kfree(vcpu_e500->guest_tlb[1]); |
| 828 | err_out_shadow0: |
| 829 | kfree(vcpu_e500->shadow_tlb[0]); |
| 830 | err_out_guest0: |
| 831 | kfree(vcpu_e500->guest_tlb[0]); |
| 832 | err_out: |
| 833 | return -1; |
| 834 | } |
| 835 | |
| 836 | void kvmppc_e500_tlb_uninit(struct kvmppc_vcpu_e500 *vcpu_e500) |
| 837 | { |
Hollis Blanchard | bc8080c | 2009-01-03 16:23:10 -0600 | [diff] [blame] | 838 | kfree(vcpu_e500->shadow_tlb[1]); |
| 839 | kfree(vcpu_e500->guest_tlb[1]); |
| 840 | kfree(vcpu_e500->shadow_tlb[0]); |
| 841 | kfree(vcpu_e500->guest_tlb[0]); |
| 842 | } |