Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 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 IBM Corp. 2007 |
| 16 | * |
| 17 | * Authors: Hollis Blanchard <hollisb@us.ibm.com> |
| 18 | */ |
| 19 | |
| 20 | #include <linux/types.h> |
| 21 | #include <linux/string.h> |
Jerone Young | 31711f2 | 2008-07-14 14:00:03 +0200 | [diff] [blame] | 22 | #include <linux/kvm.h> |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 23 | #include <linux/kvm_host.h> |
| 24 | #include <linux/highmem.h> |
Hollis Blanchard | 7924bd4 | 2008-12-02 15:51:55 -0600 | [diff] [blame] | 25 | |
| 26 | #include <asm/tlbflush.h> |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 27 | #include <asm/mmu-44x.h> |
| 28 | #include <asm/kvm_ppc.h> |
Hollis Blanchard | db93f57 | 2008-11-05 09:36:18 -0600 | [diff] [blame] | 29 | #include <asm/kvm_44x.h> |
Hollis Blanchard | 73e75b4 | 2008-12-02 15:51:57 -0600 | [diff] [blame] | 30 | #include "timing.h" |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 31 | |
| 32 | #include "44x_tlb.h" |
Marcelo Tosatti | 46f43c6 | 2009-06-18 11:47:27 -0300 | [diff] [blame] | 33 | #include "trace.h" |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 34 | |
Hollis Blanchard | 8916861 | 2008-12-02 15:51:53 -0600 | [diff] [blame] | 35 | #ifndef PPC44x_TLBE_SIZE |
| 36 | #define PPC44x_TLBE_SIZE PPC44x_TLB_4K |
| 37 | #endif |
| 38 | |
| 39 | #define PAGE_SIZE_4K (1<<12) |
| 40 | #define PAGE_MASK_4K (~(PAGE_SIZE_4K - 1)) |
| 41 | |
Hollis Blanchard | df9b856 | 2008-11-10 14:57:35 -0600 | [diff] [blame] | 42 | #define PPC44x_TLB_UATTR_MASK \ |
| 43 | (PPC44x_TLB_U0|PPC44x_TLB_U1|PPC44x_TLB_U2|PPC44x_TLB_U3) |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 44 | #define PPC44x_TLB_USER_PERM_MASK (PPC44x_TLB_UX|PPC44x_TLB_UR|PPC44x_TLB_UW) |
| 45 | #define PPC44x_TLB_SUPER_PERM_MASK (PPC44x_TLB_SX|PPC44x_TLB_SR|PPC44x_TLB_SW) |
| 46 | |
Hollis Blanchard | a0d7b9f | 2008-11-05 09:36:11 -0600 | [diff] [blame] | 47 | #ifdef DEBUG |
| 48 | void kvmppc_dump_tlbs(struct kvm_vcpu *vcpu) |
| 49 | { |
Hollis Blanchard | 0b3bafc | 2010-08-07 10:33:57 -0700 | [diff] [blame] | 50 | struct kvmppc_vcpu_44x *vcpu_44x = to_44x(vcpu); |
Hollis Blanchard | a0d7b9f | 2008-11-05 09:36:11 -0600 | [diff] [blame] | 51 | struct kvmppc_44x_tlbe *tlbe; |
| 52 | int i; |
| 53 | |
| 54 | printk("vcpu %d TLB dump:\n", vcpu->vcpu_id); |
| 55 | printk("| %2s | %3s | %8s | %8s | %8s |\n", |
| 56 | "nr", "tid", "word0", "word1", "word2"); |
| 57 | |
Hollis Blanchard | 7924bd4 | 2008-12-02 15:51:55 -0600 | [diff] [blame] | 58 | for (i = 0; i < ARRAY_SIZE(vcpu_44x->guest_tlb); i++) { |
Hollis Blanchard | db93f57 | 2008-11-05 09:36:18 -0600 | [diff] [blame] | 59 | tlbe = &vcpu_44x->guest_tlb[i]; |
Hollis Blanchard | a0d7b9f | 2008-11-05 09:36:11 -0600 | [diff] [blame] | 60 | if (tlbe->word0 & PPC44x_TLB_VALID) |
| 61 | printk(" G%2d | %02X | %08X | %08X | %08X |\n", |
| 62 | i, tlbe->tid, tlbe->word0, tlbe->word1, |
| 63 | tlbe->word2); |
| 64 | } |
Hollis Blanchard | a0d7b9f | 2008-11-05 09:36:11 -0600 | [diff] [blame] | 65 | } |
| 66 | #endif |
| 67 | |
Hollis Blanchard | 7924bd4 | 2008-12-02 15:51:55 -0600 | [diff] [blame] | 68 | static inline void kvmppc_44x_tlbie(unsigned int index) |
| 69 | { |
| 70 | /* 0 <= index < 64, so the V bit is clear and we can use the index as |
| 71 | * word0. */ |
| 72 | asm volatile( |
| 73 | "tlbwe %[index], %[index], 0\n" |
| 74 | : |
| 75 | : [index] "r"(index) |
| 76 | ); |
| 77 | } |
| 78 | |
Hollis Blanchard | c5fbdff | 2008-12-02 15:51:56 -0600 | [diff] [blame] | 79 | static inline void kvmppc_44x_tlbre(unsigned int index, |
| 80 | struct kvmppc_44x_tlbe *tlbe) |
| 81 | { |
| 82 | asm volatile( |
| 83 | "tlbre %[word0], %[index], 0\n" |
| 84 | "mfspr %[tid], %[sprn_mmucr]\n" |
| 85 | "andi. %[tid], %[tid], 0xff\n" |
| 86 | "tlbre %[word1], %[index], 1\n" |
| 87 | "tlbre %[word2], %[index], 2\n" |
| 88 | : [word0] "=r"(tlbe->word0), |
| 89 | [word1] "=r"(tlbe->word1), |
| 90 | [word2] "=r"(tlbe->word2), |
| 91 | [tid] "=r"(tlbe->tid) |
| 92 | : [index] "r"(index), |
| 93 | [sprn_mmucr] "i"(SPRN_MMUCR) |
| 94 | : "cc" |
| 95 | ); |
| 96 | } |
| 97 | |
Hollis Blanchard | 7924bd4 | 2008-12-02 15:51:55 -0600 | [diff] [blame] | 98 | static inline void kvmppc_44x_tlbwe(unsigned int index, |
| 99 | struct kvmppc_44x_tlbe *stlbe) |
| 100 | { |
| 101 | unsigned long tmp; |
| 102 | |
| 103 | asm volatile( |
| 104 | "mfspr %[tmp], %[sprn_mmucr]\n" |
| 105 | "rlwimi %[tmp], %[tid], 0, 0xff\n" |
| 106 | "mtspr %[sprn_mmucr], %[tmp]\n" |
| 107 | "tlbwe %[word0], %[index], 0\n" |
| 108 | "tlbwe %[word1], %[index], 1\n" |
| 109 | "tlbwe %[word2], %[index], 2\n" |
| 110 | : [tmp] "=&r"(tmp) |
| 111 | : [word0] "r"(stlbe->word0), |
| 112 | [word1] "r"(stlbe->word1), |
| 113 | [word2] "r"(stlbe->word2), |
| 114 | [tid] "r"(stlbe->tid), |
| 115 | [index] "r"(index), |
| 116 | [sprn_mmucr] "i"(SPRN_MMUCR) |
| 117 | ); |
| 118 | } |
| 119 | |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 120 | static u32 kvmppc_44x_tlb_shadow_attrib(u32 attrib, int usermode) |
| 121 | { |
Hollis Blanchard | df9b856 | 2008-11-10 14:57:35 -0600 | [diff] [blame] | 122 | /* We only care about the guest's permission and user bits. */ |
| 123 | attrib &= PPC44x_TLB_PERM_MASK|PPC44x_TLB_UATTR_MASK; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 124 | |
| 125 | if (!usermode) { |
| 126 | /* Guest is in supervisor mode, so we need to translate guest |
| 127 | * supervisor permissions into user permissions. */ |
| 128 | attrib &= ~PPC44x_TLB_USER_PERM_MASK; |
| 129 | attrib |= (attrib & PPC44x_TLB_SUPER_PERM_MASK) << 3; |
| 130 | } |
| 131 | |
| 132 | /* Make sure host can always access this memory. */ |
| 133 | attrib |= PPC44x_TLB_SX|PPC44x_TLB_SR|PPC44x_TLB_SW; |
| 134 | |
Hollis Blanchard | df9b856 | 2008-11-10 14:57:35 -0600 | [diff] [blame] | 135 | /* WIMGE = 0b00100 */ |
| 136 | attrib |= PPC44x_TLB_M; |
| 137 | |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 138 | return attrib; |
| 139 | } |
| 140 | |
Hollis Blanchard | c5fbdff | 2008-12-02 15:51:56 -0600 | [diff] [blame] | 141 | /* Load shadow TLB back into hardware. */ |
| 142 | void kvmppc_44x_tlb_load(struct kvm_vcpu *vcpu) |
| 143 | { |
| 144 | struct kvmppc_vcpu_44x *vcpu_44x = to_44x(vcpu); |
| 145 | int i; |
| 146 | |
| 147 | for (i = 0; i <= tlb_44x_hwater; i++) { |
| 148 | struct kvmppc_44x_tlbe *stlbe = &vcpu_44x->shadow_tlb[i]; |
| 149 | |
| 150 | if (get_tlb_v(stlbe) && get_tlb_ts(stlbe)) |
| 151 | kvmppc_44x_tlbwe(i, stlbe); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | static void kvmppc_44x_tlbe_set_modified(struct kvmppc_vcpu_44x *vcpu_44x, |
| 156 | unsigned int i) |
| 157 | { |
| 158 | vcpu_44x->shadow_tlb_mod[i] = 1; |
| 159 | } |
| 160 | |
| 161 | /* Save hardware TLB to the vcpu, and invalidate all guest mappings. */ |
| 162 | void kvmppc_44x_tlb_put(struct kvm_vcpu *vcpu) |
| 163 | { |
| 164 | struct kvmppc_vcpu_44x *vcpu_44x = to_44x(vcpu); |
| 165 | int i; |
| 166 | |
| 167 | for (i = 0; i <= tlb_44x_hwater; i++) { |
| 168 | struct kvmppc_44x_tlbe *stlbe = &vcpu_44x->shadow_tlb[i]; |
| 169 | |
| 170 | if (vcpu_44x->shadow_tlb_mod[i]) |
| 171 | kvmppc_44x_tlbre(i, stlbe); |
| 172 | |
| 173 | if (get_tlb_v(stlbe) && get_tlb_ts(stlbe)) |
| 174 | kvmppc_44x_tlbie(i); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 179 | /* Search the guest TLB for a matching entry. */ |
| 180 | int kvmppc_44x_tlb_index(struct kvm_vcpu *vcpu, gva_t eaddr, unsigned int pid, |
| 181 | unsigned int as) |
| 182 | { |
Hollis Blanchard | db93f57 | 2008-11-05 09:36:18 -0600 | [diff] [blame] | 183 | struct kvmppc_vcpu_44x *vcpu_44x = to_44x(vcpu); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 184 | int i; |
| 185 | |
| 186 | /* XXX Replace loop with fancy data structures. */ |
Hollis Blanchard | 7924bd4 | 2008-12-02 15:51:55 -0600 | [diff] [blame] | 187 | for (i = 0; i < ARRAY_SIZE(vcpu_44x->guest_tlb); i++) { |
Hollis Blanchard | db93f57 | 2008-11-05 09:36:18 -0600 | [diff] [blame] | 188 | struct kvmppc_44x_tlbe *tlbe = &vcpu_44x->guest_tlb[i]; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 189 | unsigned int tid; |
| 190 | |
| 191 | if (eaddr < get_tlb_eaddr(tlbe)) |
| 192 | continue; |
| 193 | |
| 194 | if (eaddr > get_tlb_end(tlbe)) |
| 195 | continue; |
| 196 | |
| 197 | tid = get_tlb_tid(tlbe); |
| 198 | if (tid && (tid != pid)) |
| 199 | continue; |
| 200 | |
| 201 | if (!get_tlb_v(tlbe)) |
| 202 | continue; |
| 203 | |
| 204 | if (get_tlb_ts(tlbe) != as) |
| 205 | continue; |
| 206 | |
| 207 | return i; |
| 208 | } |
| 209 | |
| 210 | return -1; |
| 211 | } |
| 212 | |
Hollis Blanchard | be8d1ca | 2009-01-03 16:23:02 -0600 | [diff] [blame] | 213 | gpa_t kvmppc_mmu_xlate(struct kvm_vcpu *vcpu, unsigned int gtlb_index, |
| 214 | gva_t eaddr) |
| 215 | { |
| 216 | struct kvmppc_vcpu_44x *vcpu_44x = to_44x(vcpu); |
| 217 | struct kvmppc_44x_tlbe *gtlbe = &vcpu_44x->guest_tlb[gtlb_index]; |
| 218 | unsigned int pgmask = get_tlb_bytes(gtlbe) - 1; |
| 219 | |
| 220 | return get_tlb_raddr(gtlbe) | (eaddr & pgmask); |
| 221 | } |
| 222 | |
Hollis Blanchard | fa86b8d | 2009-01-03 16:23:03 -0600 | [diff] [blame] | 223 | int kvmppc_mmu_itlb_index(struct kvm_vcpu *vcpu, gva_t eaddr) |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 224 | { |
Alexander Graf | 666e725 | 2010-07-29 14:47:43 +0200 | [diff] [blame] | 225 | unsigned int as = !!(vcpu->arch.shared->msr & MSR_IS); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 226 | |
Hollis Blanchard | 7924bd4 | 2008-12-02 15:51:55 -0600 | [diff] [blame] | 227 | return kvmppc_44x_tlb_index(vcpu, eaddr, vcpu->arch.pid, as); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 228 | } |
| 229 | |
Hollis Blanchard | fa86b8d | 2009-01-03 16:23:03 -0600 | [diff] [blame] | 230 | int kvmppc_mmu_dtlb_index(struct kvm_vcpu *vcpu, gva_t eaddr) |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 231 | { |
Alexander Graf | 666e725 | 2010-07-29 14:47:43 +0200 | [diff] [blame] | 232 | unsigned int as = !!(vcpu->arch.shared->msr & MSR_DS); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 233 | |
Hollis Blanchard | 7924bd4 | 2008-12-02 15:51:55 -0600 | [diff] [blame] | 234 | return kvmppc_44x_tlb_index(vcpu, eaddr, vcpu->arch.pid, as); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 235 | } |
| 236 | |
Hollis Blanchard | b52a638 | 2009-01-03 16:23:11 -0600 | [diff] [blame] | 237 | void kvmppc_mmu_itlb_miss(struct kvm_vcpu *vcpu) |
| 238 | { |
| 239 | } |
| 240 | |
| 241 | void kvmppc_mmu_dtlb_miss(struct kvm_vcpu *vcpu) |
| 242 | { |
| 243 | } |
| 244 | |
Hollis Blanchard | 7924bd4 | 2008-12-02 15:51:55 -0600 | [diff] [blame] | 245 | static void kvmppc_44x_shadow_release(struct kvmppc_vcpu_44x *vcpu_44x, |
| 246 | unsigned int stlb_index) |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 247 | { |
Hollis Blanchard | 7924bd4 | 2008-12-02 15:51:55 -0600 | [diff] [blame] | 248 | struct kvmppc_44x_shadow_ref *ref = &vcpu_44x->shadow_refs[stlb_index]; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 249 | |
Hollis Blanchard | 7924bd4 | 2008-12-02 15:51:55 -0600 | [diff] [blame] | 250 | if (!ref->page) |
| 251 | return; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 252 | |
Hollis Blanchard | 7924bd4 | 2008-12-02 15:51:55 -0600 | [diff] [blame] | 253 | /* Discard from the TLB. */ |
| 254 | /* Note: we could actually invalidate a host mapping, if the host overwrote |
| 255 | * this TLB entry since we inserted a guest mapping. */ |
| 256 | kvmppc_44x_tlbie(stlb_index); |
| 257 | |
| 258 | /* Now release the page. */ |
| 259 | if (ref->writeable) |
| 260 | kvm_release_page_dirty(ref->page); |
| 261 | else |
| 262 | kvm_release_page_clean(ref->page); |
| 263 | |
| 264 | ref->page = NULL; |
| 265 | |
| 266 | /* XXX set tlb_44x_index to stlb_index? */ |
| 267 | |
Marcelo Tosatti | 46f43c6 | 2009-06-18 11:47:27 -0300 | [diff] [blame] | 268 | trace_kvm_stlb_inval(stlb_index); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 269 | } |
| 270 | |
Hollis Blanchard | ecc0981 | 2009-01-03 16:22:59 -0600 | [diff] [blame] | 271 | void kvmppc_mmu_destroy(struct kvm_vcpu *vcpu) |
Hollis Blanchard | c30f8a6 | 2008-11-24 11:37:38 -0600 | [diff] [blame] | 272 | { |
Hollis Blanchard | 7924bd4 | 2008-12-02 15:51:55 -0600 | [diff] [blame] | 273 | struct kvmppc_vcpu_44x *vcpu_44x = to_44x(vcpu); |
Hollis Blanchard | c30f8a6 | 2008-11-24 11:37:38 -0600 | [diff] [blame] | 274 | int i; |
| 275 | |
| 276 | for (i = 0; i <= tlb_44x_hwater; i++) |
Hollis Blanchard | 7924bd4 | 2008-12-02 15:51:55 -0600 | [diff] [blame] | 277 | kvmppc_44x_shadow_release(vcpu_44x, i); |
Hollis Blanchard | 83aae4a | 2008-07-25 13:54:52 -0500 | [diff] [blame] | 278 | } |
| 279 | |
Hollis Blanchard | 8916861 | 2008-12-02 15:51:53 -0600 | [diff] [blame] | 280 | /** |
| 281 | * kvmppc_mmu_map -- create a host mapping for guest memory |
| 282 | * |
| 283 | * If the guest wanted a larger page than the host supports, only the first |
| 284 | * host page is mapped here and the rest are demand faulted. |
| 285 | * |
| 286 | * If the guest wanted a smaller page than the host page size, we map only the |
| 287 | * guest-size page (i.e. not a full host page mapping). |
| 288 | * |
| 289 | * Caller must ensure that the specified guest TLB entry is safe to insert into |
| 290 | * the shadow TLB. |
| 291 | */ |
Hollis Blanchard | 58a9621 | 2009-01-03 16:23:01 -0600 | [diff] [blame] | 292 | void kvmppc_mmu_map(struct kvm_vcpu *vcpu, u64 gvaddr, gpa_t gpaddr, |
| 293 | unsigned int gtlb_index) |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 294 | { |
Hollis Blanchard | 7924bd4 | 2008-12-02 15:51:55 -0600 | [diff] [blame] | 295 | struct kvmppc_44x_tlbe stlbe; |
Hollis Blanchard | db93f57 | 2008-11-05 09:36:18 -0600 | [diff] [blame] | 296 | struct kvmppc_vcpu_44x *vcpu_44x = to_44x(vcpu); |
Hollis Blanchard | 58a9621 | 2009-01-03 16:23:01 -0600 | [diff] [blame] | 297 | struct kvmppc_44x_tlbe *gtlbe = &vcpu_44x->guest_tlb[gtlb_index]; |
Hollis Blanchard | 7924bd4 | 2008-12-02 15:51:55 -0600 | [diff] [blame] | 298 | struct kvmppc_44x_shadow_ref *ref; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 299 | struct page *new_page; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 300 | hpa_t hpaddr; |
Hollis Blanchard | 8916861 | 2008-12-02 15:51:53 -0600 | [diff] [blame] | 301 | gfn_t gfn; |
Hollis Blanchard | 58a9621 | 2009-01-03 16:23:01 -0600 | [diff] [blame] | 302 | u32 asid = gtlbe->tid; |
| 303 | u32 flags = gtlbe->word2; |
| 304 | u32 max_bytes = get_tlb_bytes(gtlbe); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 305 | unsigned int victim; |
| 306 | |
Hollis Blanchard | 7924bd4 | 2008-12-02 15:51:55 -0600 | [diff] [blame] | 307 | /* Select TLB entry to clobber. Indirectly guard against races with the TLB |
| 308 | * miss handler by disabling interrupts. */ |
| 309 | local_irq_disable(); |
| 310 | victim = ++tlb_44x_index; |
| 311 | if (victim > tlb_44x_hwater) |
| 312 | victim = 0; |
| 313 | tlb_44x_index = victim; |
| 314 | local_irq_enable(); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 315 | |
| 316 | /* Get reference to new page. */ |
Hollis Blanchard | 8916861 | 2008-12-02 15:51:53 -0600 | [diff] [blame] | 317 | gfn = gpaddr >> PAGE_SHIFT; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 318 | new_page = gfn_to_page(vcpu->kvm, gfn); |
| 319 | if (is_error_page(new_page)) { |
Joerg Roedel | 5689cc5 | 2010-07-01 16:00:12 +0200 | [diff] [blame] | 320 | printk(KERN_ERR "Couldn't get guest page for gfn %llx!\n", |
| 321 | (unsigned long long)gfn); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 322 | kvm_release_page_clean(new_page); |
| 323 | return; |
| 324 | } |
| 325 | hpaddr = page_to_phys(new_page); |
| 326 | |
Hollis Blanchard | 7924bd4 | 2008-12-02 15:51:55 -0600 | [diff] [blame] | 327 | /* Invalidate any previous shadow mappings. */ |
| 328 | kvmppc_44x_shadow_release(vcpu_44x, victim); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 329 | |
| 330 | /* XXX Make sure (va, size) doesn't overlap any other |
| 331 | * entries. 440x6 user manual says the result would be |
| 332 | * "undefined." */ |
| 333 | |
| 334 | /* XXX what about AS? */ |
| 335 | |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 336 | /* Force TS=1 for all guest mappings. */ |
Hollis Blanchard | 7924bd4 | 2008-12-02 15:51:55 -0600 | [diff] [blame] | 337 | stlbe.word0 = PPC44x_TLB_VALID | PPC44x_TLB_TS; |
Hollis Blanchard | 8916861 | 2008-12-02 15:51:53 -0600 | [diff] [blame] | 338 | |
| 339 | if (max_bytes >= PAGE_SIZE) { |
| 340 | /* Guest mapping is larger than or equal to host page size. We can use |
| 341 | * a "native" host mapping. */ |
Hollis Blanchard | 7924bd4 | 2008-12-02 15:51:55 -0600 | [diff] [blame] | 342 | stlbe.word0 |= (gvaddr & PAGE_MASK) | PPC44x_TLBE_SIZE; |
Hollis Blanchard | 8916861 | 2008-12-02 15:51:53 -0600 | [diff] [blame] | 343 | } else { |
| 344 | /* Guest mapping is smaller than host page size. We must restrict the |
| 345 | * size of the mapping to be at most the smaller of the two, but for |
| 346 | * simplicity we fall back to a 4K mapping (this is probably what the |
| 347 | * guest is using anyways). */ |
Hollis Blanchard | 7924bd4 | 2008-12-02 15:51:55 -0600 | [diff] [blame] | 348 | stlbe.word0 |= (gvaddr & PAGE_MASK_4K) | PPC44x_TLB_4K; |
Hollis Blanchard | 8916861 | 2008-12-02 15:51:53 -0600 | [diff] [blame] | 349 | |
| 350 | /* 'hpaddr' is a host page, which is larger than the mapping we're |
| 351 | * inserting here. To compensate, we must add the in-page offset to the |
| 352 | * sub-page. */ |
| 353 | hpaddr |= gpaddr & (PAGE_MASK ^ PAGE_MASK_4K); |
| 354 | } |
| 355 | |
Hollis Blanchard | 7924bd4 | 2008-12-02 15:51:55 -0600 | [diff] [blame] | 356 | stlbe.word1 = (hpaddr & 0xfffffc00) | ((hpaddr >> 32) & 0xf); |
| 357 | stlbe.word2 = kvmppc_44x_tlb_shadow_attrib(flags, |
Alexander Graf | 666e725 | 2010-07-29 14:47:43 +0200 | [diff] [blame] | 358 | vcpu->arch.shared->msr & MSR_PR); |
Hollis Blanchard | 7924bd4 | 2008-12-02 15:51:55 -0600 | [diff] [blame] | 359 | stlbe.tid = !(asid & 0xff); |
Jerone Young | 31711f2 | 2008-07-14 14:00:03 +0200 | [diff] [blame] | 360 | |
Hollis Blanchard | 7924bd4 | 2008-12-02 15:51:55 -0600 | [diff] [blame] | 361 | /* Keep track of the reference so we can properly release it later. */ |
| 362 | ref = &vcpu_44x->shadow_refs[victim]; |
| 363 | ref->page = new_page; |
| 364 | ref->gtlb_index = gtlb_index; |
| 365 | ref->writeable = !!(stlbe.word2 & PPC44x_TLB_UW); |
| 366 | ref->tid = stlbe.tid; |
| 367 | |
| 368 | /* Insert shadow mapping into hardware TLB. */ |
Hollis Blanchard | c5fbdff | 2008-12-02 15:51:56 -0600 | [diff] [blame] | 369 | kvmppc_44x_tlbe_set_modified(vcpu_44x, victim); |
Hollis Blanchard | 7924bd4 | 2008-12-02 15:51:55 -0600 | [diff] [blame] | 370 | kvmppc_44x_tlbwe(victim, &stlbe); |
Marcelo Tosatti | 46f43c6 | 2009-06-18 11:47:27 -0300 | [diff] [blame] | 371 | trace_kvm_stlb_write(victim, stlbe.tid, stlbe.word0, stlbe.word1, |
| 372 | stlbe.word2); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 373 | } |
| 374 | |
Hollis Blanchard | 7924bd4 | 2008-12-02 15:51:55 -0600 | [diff] [blame] | 375 | /* For a particular guest TLB entry, invalidate the corresponding host TLB |
| 376 | * mappings and release the host pages. */ |
| 377 | static void kvmppc_44x_invalidate(struct kvm_vcpu *vcpu, |
| 378 | unsigned int gtlb_index) |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 379 | { |
Hollis Blanchard | db93f57 | 2008-11-05 09:36:18 -0600 | [diff] [blame] | 380 | struct kvmppc_vcpu_44x *vcpu_44x = to_44x(vcpu); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 381 | int i; |
| 382 | |
Hollis Blanchard | 7924bd4 | 2008-12-02 15:51:55 -0600 | [diff] [blame] | 383 | for (i = 0; i < ARRAY_SIZE(vcpu_44x->shadow_refs); i++) { |
| 384 | struct kvmppc_44x_shadow_ref *ref = &vcpu_44x->shadow_refs[i]; |
| 385 | if (ref->gtlb_index == gtlb_index) |
| 386 | kvmppc_44x_shadow_release(vcpu_44x, i); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 387 | } |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 388 | } |
| 389 | |
Liu Yu | dd9ebf1f | 2011-06-14 18:35:14 -0500 | [diff] [blame] | 390 | void kvmppc_mmu_msr_notify(struct kvm_vcpu *vcpu, u32 old_msr) |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 391 | { |
Liu Yu | dd9ebf1f | 2011-06-14 18:35:14 -0500 | [diff] [blame] | 392 | int usermode = vcpu->arch.shared->msr & MSR_PR; |
| 393 | |
Hollis Blanchard | fe4e771 | 2008-11-10 14:57:36 -0600 | [diff] [blame] | 394 | vcpu->arch.shadow_pid = !usermode; |
| 395 | } |
| 396 | |
| 397 | void kvmppc_set_pid(struct kvm_vcpu *vcpu, u32 new_pid) |
| 398 | { |
Hollis Blanchard | db93f57 | 2008-11-05 09:36:18 -0600 | [diff] [blame] | 399 | struct kvmppc_vcpu_44x *vcpu_44x = to_44x(vcpu); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 400 | int i; |
| 401 | |
Hollis Blanchard | fe4e771 | 2008-11-10 14:57:36 -0600 | [diff] [blame] | 402 | if (unlikely(vcpu->arch.pid == new_pid)) |
| 403 | return; |
Jerone Young | 31711f2 | 2008-07-14 14:00:03 +0200 | [diff] [blame] | 404 | |
Hollis Blanchard | fe4e771 | 2008-11-10 14:57:36 -0600 | [diff] [blame] | 405 | vcpu->arch.pid = new_pid; |
| 406 | |
| 407 | /* Guest userspace runs with TID=0 mappings and PID=0, to make sure it |
| 408 | * can't access guest kernel mappings (TID=1). When we switch to a new |
| 409 | * guest PID, which will also use host PID=0, we must discard the old guest |
| 410 | * userspace mappings. */ |
Hollis Blanchard | 7924bd4 | 2008-12-02 15:51:55 -0600 | [diff] [blame] | 411 | for (i = 0; i < ARRAY_SIZE(vcpu_44x->shadow_refs); i++) { |
| 412 | struct kvmppc_44x_shadow_ref *ref = &vcpu_44x->shadow_refs[i]; |
Hollis Blanchard | fe4e771 | 2008-11-10 14:57:36 -0600 | [diff] [blame] | 413 | |
Hollis Blanchard | 7924bd4 | 2008-12-02 15:51:55 -0600 | [diff] [blame] | 414 | if (ref->tid == 0) |
| 415 | kvmppc_44x_shadow_release(vcpu_44x, i); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 416 | } |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 417 | } |
Hollis Blanchard | a0d7b9f | 2008-11-05 09:36:11 -0600 | [diff] [blame] | 418 | |
| 419 | static int tlbe_is_host_safe(const struct kvm_vcpu *vcpu, |
Hollis Blanchard | 0f55dc4 | 2008-11-05 09:36:12 -0600 | [diff] [blame] | 420 | const struct kvmppc_44x_tlbe *tlbe) |
Hollis Blanchard | a0d7b9f | 2008-11-05 09:36:11 -0600 | [diff] [blame] | 421 | { |
| 422 | gpa_t gpa; |
| 423 | |
| 424 | if (!get_tlb_v(tlbe)) |
| 425 | return 0; |
| 426 | |
| 427 | /* Does it match current guest AS? */ |
| 428 | /* XXX what about IS != DS? */ |
Alexander Graf | 666e725 | 2010-07-29 14:47:43 +0200 | [diff] [blame] | 429 | if (get_tlb_ts(tlbe) != !!(vcpu->arch.shared->msr & MSR_IS)) |
Hollis Blanchard | a0d7b9f | 2008-11-05 09:36:11 -0600 | [diff] [blame] | 430 | return 0; |
| 431 | |
| 432 | gpa = get_tlb_raddr(tlbe); |
| 433 | if (!gfn_to_memslot(vcpu->kvm, gpa >> PAGE_SHIFT)) |
| 434 | /* Mapping is not for RAM. */ |
| 435 | return 0; |
| 436 | |
| 437 | return 1; |
| 438 | } |
| 439 | |
Hollis Blanchard | 75f74f0 | 2008-11-05 09:36:16 -0600 | [diff] [blame] | 440 | int kvmppc_44x_emul_tlbwe(struct kvm_vcpu *vcpu, u8 ra, u8 rs, u8 ws) |
Hollis Blanchard | a0d7b9f | 2008-11-05 09:36:11 -0600 | [diff] [blame] | 441 | { |
Hollis Blanchard | db93f57 | 2008-11-05 09:36:18 -0600 | [diff] [blame] | 442 | struct kvmppc_vcpu_44x *vcpu_44x = to_44x(vcpu); |
Hollis Blanchard | 0f55dc4 | 2008-11-05 09:36:12 -0600 | [diff] [blame] | 443 | struct kvmppc_44x_tlbe *tlbe; |
Hollis Blanchard | 7924bd4 | 2008-12-02 15:51:55 -0600 | [diff] [blame] | 444 | unsigned int gtlb_index; |
Hollis Blanchard | a0d7b9f | 2008-11-05 09:36:11 -0600 | [diff] [blame] | 445 | |
Alexander Graf | 8e5b26b | 2010-01-08 02:58:01 +0100 | [diff] [blame] | 446 | gtlb_index = kvmppc_get_gpr(vcpu, ra); |
Roel Kluin | 4f018c5 | 2010-05-09 17:26:47 +0200 | [diff] [blame] | 447 | if (gtlb_index >= KVM44x_GUEST_TLB_SIZE) { |
Hollis Blanchard | 7924bd4 | 2008-12-02 15:51:55 -0600 | [diff] [blame] | 448 | printk("%s: index %d\n", __func__, gtlb_index); |
Hollis Blanchard | a0d7b9f | 2008-11-05 09:36:11 -0600 | [diff] [blame] | 449 | kvmppc_dump_vcpu(vcpu); |
| 450 | return EMULATE_FAIL; |
| 451 | } |
| 452 | |
Hollis Blanchard | 7924bd4 | 2008-12-02 15:51:55 -0600 | [diff] [blame] | 453 | tlbe = &vcpu_44x->guest_tlb[gtlb_index]; |
Hollis Blanchard | a0d7b9f | 2008-11-05 09:36:11 -0600 | [diff] [blame] | 454 | |
Hollis Blanchard | 7924bd4 | 2008-12-02 15:51:55 -0600 | [diff] [blame] | 455 | /* Invalidate shadow mappings for the about-to-be-clobbered TLB entry. */ |
| 456 | if (tlbe->word0 & PPC44x_TLB_VALID) |
| 457 | kvmppc_44x_invalidate(vcpu, gtlb_index); |
Hollis Blanchard | a0d7b9f | 2008-11-05 09:36:11 -0600 | [diff] [blame] | 458 | |
| 459 | switch (ws) { |
| 460 | case PPC44x_TLB_PAGEID: |
Hollis Blanchard | bf5d402 | 2008-11-10 14:57:34 -0600 | [diff] [blame] | 461 | tlbe->tid = get_mmucr_stid(vcpu); |
Alexander Graf | 8e5b26b | 2010-01-08 02:58:01 +0100 | [diff] [blame] | 462 | tlbe->word0 = kvmppc_get_gpr(vcpu, rs); |
Hollis Blanchard | a0d7b9f | 2008-11-05 09:36:11 -0600 | [diff] [blame] | 463 | break; |
| 464 | |
| 465 | case PPC44x_TLB_XLAT: |
Alexander Graf | 8e5b26b | 2010-01-08 02:58:01 +0100 | [diff] [blame] | 466 | tlbe->word1 = kvmppc_get_gpr(vcpu, rs); |
Hollis Blanchard | a0d7b9f | 2008-11-05 09:36:11 -0600 | [diff] [blame] | 467 | break; |
| 468 | |
| 469 | case PPC44x_TLB_ATTRIB: |
Alexander Graf | 8e5b26b | 2010-01-08 02:58:01 +0100 | [diff] [blame] | 470 | tlbe->word2 = kvmppc_get_gpr(vcpu, rs); |
Hollis Blanchard | a0d7b9f | 2008-11-05 09:36:11 -0600 | [diff] [blame] | 471 | break; |
| 472 | |
| 473 | default: |
| 474 | return EMULATE_FAIL; |
| 475 | } |
| 476 | |
| 477 | if (tlbe_is_host_safe(vcpu, tlbe)) { |
Hollis Blanchard | 7924bd4 | 2008-12-02 15:51:55 -0600 | [diff] [blame] | 478 | gva_t eaddr; |
Hollis Blanchard | 8916861 | 2008-12-02 15:51:53 -0600 | [diff] [blame] | 479 | gpa_t gpaddr; |
Hollis Blanchard | 8916861 | 2008-12-02 15:51:53 -0600 | [diff] [blame] | 480 | u32 bytes; |
| 481 | |
Hollis Blanchard | a0d7b9f | 2008-11-05 09:36:11 -0600 | [diff] [blame] | 482 | eaddr = get_tlb_eaddr(tlbe); |
Hollis Blanchard | 8916861 | 2008-12-02 15:51:53 -0600 | [diff] [blame] | 483 | gpaddr = get_tlb_raddr(tlbe); |
| 484 | |
| 485 | /* Use the advertised page size to mask effective and real addrs. */ |
| 486 | bytes = get_tlb_bytes(tlbe); |
| 487 | eaddr &= ~(bytes - 1); |
| 488 | gpaddr &= ~(bytes - 1); |
| 489 | |
Hollis Blanchard | 58a9621 | 2009-01-03 16:23:01 -0600 | [diff] [blame] | 490 | kvmppc_mmu_map(vcpu, eaddr, gpaddr, gtlb_index); |
Hollis Blanchard | a0d7b9f | 2008-11-05 09:36:11 -0600 | [diff] [blame] | 491 | } |
| 492 | |
Marcelo Tosatti | 46f43c6 | 2009-06-18 11:47:27 -0300 | [diff] [blame] | 493 | trace_kvm_gtlb_write(gtlb_index, tlbe->tid, tlbe->word0, tlbe->word1, |
| 494 | tlbe->word2); |
Hollis Blanchard | a0d7b9f | 2008-11-05 09:36:11 -0600 | [diff] [blame] | 495 | |
Hollis Blanchard | 73e75b4 | 2008-12-02 15:51:57 -0600 | [diff] [blame] | 496 | kvmppc_set_exit_type(vcpu, EMULATED_TLBWE_EXITS); |
Hollis Blanchard | a0d7b9f | 2008-11-05 09:36:11 -0600 | [diff] [blame] | 497 | return EMULATE_DONE; |
| 498 | } |
| 499 | |
Hollis Blanchard | 75f74f0 | 2008-11-05 09:36:16 -0600 | [diff] [blame] | 500 | int kvmppc_44x_emul_tlbsx(struct kvm_vcpu *vcpu, u8 rt, u8 ra, u8 rb, u8 rc) |
Hollis Blanchard | a0d7b9f | 2008-11-05 09:36:11 -0600 | [diff] [blame] | 501 | { |
| 502 | u32 ea; |
Hollis Blanchard | 7924bd4 | 2008-12-02 15:51:55 -0600 | [diff] [blame] | 503 | int gtlb_index; |
Hollis Blanchard | a0d7b9f | 2008-11-05 09:36:11 -0600 | [diff] [blame] | 504 | unsigned int as = get_mmucr_sts(vcpu); |
| 505 | unsigned int pid = get_mmucr_stid(vcpu); |
| 506 | |
Alexander Graf | 8e5b26b | 2010-01-08 02:58:01 +0100 | [diff] [blame] | 507 | ea = kvmppc_get_gpr(vcpu, rb); |
Hollis Blanchard | a0d7b9f | 2008-11-05 09:36:11 -0600 | [diff] [blame] | 508 | if (ra) |
Alexander Graf | 8e5b26b | 2010-01-08 02:58:01 +0100 | [diff] [blame] | 509 | ea += kvmppc_get_gpr(vcpu, ra); |
Hollis Blanchard | a0d7b9f | 2008-11-05 09:36:11 -0600 | [diff] [blame] | 510 | |
Hollis Blanchard | 7924bd4 | 2008-12-02 15:51:55 -0600 | [diff] [blame] | 511 | gtlb_index = kvmppc_44x_tlb_index(vcpu, ea, pid, as); |
Hollis Blanchard | a0d7b9f | 2008-11-05 09:36:11 -0600 | [diff] [blame] | 512 | if (rc) { |
Alexander Graf | 992b5b2 | 2010-01-08 02:58:02 +0100 | [diff] [blame] | 513 | u32 cr = kvmppc_get_cr(vcpu); |
| 514 | |
Hollis Blanchard | 7924bd4 | 2008-12-02 15:51:55 -0600 | [diff] [blame] | 515 | if (gtlb_index < 0) |
Alexander Graf | 992b5b2 | 2010-01-08 02:58:02 +0100 | [diff] [blame] | 516 | kvmppc_set_cr(vcpu, cr & ~0x20000000); |
Hollis Blanchard | a0d7b9f | 2008-11-05 09:36:11 -0600 | [diff] [blame] | 517 | else |
Alexander Graf | 992b5b2 | 2010-01-08 02:58:02 +0100 | [diff] [blame] | 518 | kvmppc_set_cr(vcpu, cr | 0x20000000); |
Hollis Blanchard | a0d7b9f | 2008-11-05 09:36:11 -0600 | [diff] [blame] | 519 | } |
Alexander Graf | 8e5b26b | 2010-01-08 02:58:01 +0100 | [diff] [blame] | 520 | kvmppc_set_gpr(vcpu, rt, gtlb_index); |
Hollis Blanchard | a0d7b9f | 2008-11-05 09:36:11 -0600 | [diff] [blame] | 521 | |
Hollis Blanchard | 73e75b4 | 2008-12-02 15:51:57 -0600 | [diff] [blame] | 522 | kvmppc_set_exit_type(vcpu, EMULATED_TLBSX_EXITS); |
Hollis Blanchard | a0d7b9f | 2008-11-05 09:36:11 -0600 | [diff] [blame] | 523 | return EMULATE_DONE; |
| 524 | } |