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> |
| 25 | #include <asm/mmu-44x.h> |
| 26 | #include <asm/kvm_ppc.h> |
Hollis Blanchard | db93f57 | 2008-11-05 09:36:18 -0600 | [diff] [blame] | 27 | #include <asm/kvm_44x.h> |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 28 | |
| 29 | #include "44x_tlb.h" |
| 30 | |
Hollis Blanchard | df9b856 | 2008-11-10 14:57:35 -0600 | [diff] [blame^] | 31 | #define PPC44x_TLB_UATTR_MASK \ |
| 32 | (PPC44x_TLB_U0|PPC44x_TLB_U1|PPC44x_TLB_U2|PPC44x_TLB_U3) |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 33 | #define PPC44x_TLB_USER_PERM_MASK (PPC44x_TLB_UX|PPC44x_TLB_UR|PPC44x_TLB_UW) |
| 34 | #define PPC44x_TLB_SUPER_PERM_MASK (PPC44x_TLB_SX|PPC44x_TLB_SR|PPC44x_TLB_SW) |
| 35 | |
| 36 | static unsigned int kvmppc_tlb_44x_pos; |
| 37 | |
Hollis Blanchard | a0d7b9f | 2008-11-05 09:36:11 -0600 | [diff] [blame] | 38 | #ifdef DEBUG |
| 39 | void kvmppc_dump_tlbs(struct kvm_vcpu *vcpu) |
| 40 | { |
| 41 | struct kvmppc_44x_tlbe *tlbe; |
| 42 | int i; |
| 43 | |
| 44 | printk("vcpu %d TLB dump:\n", vcpu->vcpu_id); |
| 45 | printk("| %2s | %3s | %8s | %8s | %8s |\n", |
| 46 | "nr", "tid", "word0", "word1", "word2"); |
| 47 | |
| 48 | for (i = 0; i < PPC44x_TLB_SIZE; i++) { |
Hollis Blanchard | db93f57 | 2008-11-05 09:36:18 -0600 | [diff] [blame] | 49 | tlbe = &vcpu_44x->guest_tlb[i]; |
Hollis Blanchard | a0d7b9f | 2008-11-05 09:36:11 -0600 | [diff] [blame] | 50 | if (tlbe->word0 & PPC44x_TLB_VALID) |
| 51 | printk(" G%2d | %02X | %08X | %08X | %08X |\n", |
| 52 | i, tlbe->tid, tlbe->word0, tlbe->word1, |
| 53 | tlbe->word2); |
| 54 | } |
| 55 | |
| 56 | for (i = 0; i < PPC44x_TLB_SIZE; i++) { |
Hollis Blanchard | db93f57 | 2008-11-05 09:36:18 -0600 | [diff] [blame] | 57 | tlbe = &vcpu_44x->shadow_tlb[i]; |
Hollis Blanchard | a0d7b9f | 2008-11-05 09:36:11 -0600 | [diff] [blame] | 58 | if (tlbe->word0 & PPC44x_TLB_VALID) |
| 59 | printk(" S%2d | %02X | %08X | %08X | %08X |\n", |
| 60 | i, tlbe->tid, tlbe->word0, tlbe->word1, |
| 61 | tlbe->word2); |
| 62 | } |
| 63 | } |
| 64 | #endif |
| 65 | |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 66 | static u32 kvmppc_44x_tlb_shadow_attrib(u32 attrib, int usermode) |
| 67 | { |
Hollis Blanchard | df9b856 | 2008-11-10 14:57:35 -0600 | [diff] [blame^] | 68 | /* We only care about the guest's permission and user bits. */ |
| 69 | attrib &= PPC44x_TLB_PERM_MASK|PPC44x_TLB_UATTR_MASK; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 70 | |
| 71 | if (!usermode) { |
| 72 | /* Guest is in supervisor mode, so we need to translate guest |
| 73 | * supervisor permissions into user permissions. */ |
| 74 | attrib &= ~PPC44x_TLB_USER_PERM_MASK; |
| 75 | attrib |= (attrib & PPC44x_TLB_SUPER_PERM_MASK) << 3; |
| 76 | } |
| 77 | |
| 78 | /* Make sure host can always access this memory. */ |
| 79 | attrib |= PPC44x_TLB_SX|PPC44x_TLB_SR|PPC44x_TLB_SW; |
| 80 | |
Hollis Blanchard | df9b856 | 2008-11-10 14:57:35 -0600 | [diff] [blame^] | 81 | /* WIMGE = 0b00100 */ |
| 82 | attrib |= PPC44x_TLB_M; |
| 83 | |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 84 | return attrib; |
| 85 | } |
| 86 | |
| 87 | /* Search the guest TLB for a matching entry. */ |
| 88 | int kvmppc_44x_tlb_index(struct kvm_vcpu *vcpu, gva_t eaddr, unsigned int pid, |
| 89 | unsigned int as) |
| 90 | { |
Hollis Blanchard | db93f57 | 2008-11-05 09:36:18 -0600 | [diff] [blame] | 91 | struct kvmppc_vcpu_44x *vcpu_44x = to_44x(vcpu); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 92 | int i; |
| 93 | |
| 94 | /* XXX Replace loop with fancy data structures. */ |
| 95 | for (i = 0; i < PPC44x_TLB_SIZE; i++) { |
Hollis Blanchard | db93f57 | 2008-11-05 09:36:18 -0600 | [diff] [blame] | 96 | struct kvmppc_44x_tlbe *tlbe = &vcpu_44x->guest_tlb[i]; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 97 | unsigned int tid; |
| 98 | |
| 99 | if (eaddr < get_tlb_eaddr(tlbe)) |
| 100 | continue; |
| 101 | |
| 102 | if (eaddr > get_tlb_end(tlbe)) |
| 103 | continue; |
| 104 | |
| 105 | tid = get_tlb_tid(tlbe); |
| 106 | if (tid && (tid != pid)) |
| 107 | continue; |
| 108 | |
| 109 | if (!get_tlb_v(tlbe)) |
| 110 | continue; |
| 111 | |
| 112 | if (get_tlb_ts(tlbe) != as) |
| 113 | continue; |
| 114 | |
| 115 | return i; |
| 116 | } |
| 117 | |
| 118 | return -1; |
| 119 | } |
| 120 | |
Hollis Blanchard | 0f55dc4 | 2008-11-05 09:36:12 -0600 | [diff] [blame] | 121 | struct kvmppc_44x_tlbe *kvmppc_44x_itlb_search(struct kvm_vcpu *vcpu, |
| 122 | gva_t eaddr) |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 123 | { |
Hollis Blanchard | db93f57 | 2008-11-05 09:36:18 -0600 | [diff] [blame] | 124 | struct kvmppc_vcpu_44x *vcpu_44x = to_44x(vcpu); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 125 | unsigned int as = !!(vcpu->arch.msr & MSR_IS); |
| 126 | unsigned int index; |
| 127 | |
| 128 | index = kvmppc_44x_tlb_index(vcpu, eaddr, vcpu->arch.pid, as); |
| 129 | if (index == -1) |
| 130 | return NULL; |
Hollis Blanchard | db93f57 | 2008-11-05 09:36:18 -0600 | [diff] [blame] | 131 | return &vcpu_44x->guest_tlb[index]; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 132 | } |
| 133 | |
Hollis Blanchard | 0f55dc4 | 2008-11-05 09:36:12 -0600 | [diff] [blame] | 134 | struct kvmppc_44x_tlbe *kvmppc_44x_dtlb_search(struct kvm_vcpu *vcpu, |
| 135 | gva_t eaddr) |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 136 | { |
Hollis Blanchard | db93f57 | 2008-11-05 09:36:18 -0600 | [diff] [blame] | 137 | struct kvmppc_vcpu_44x *vcpu_44x = to_44x(vcpu); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 138 | unsigned int as = !!(vcpu->arch.msr & MSR_DS); |
| 139 | unsigned int index; |
| 140 | |
| 141 | index = kvmppc_44x_tlb_index(vcpu, eaddr, vcpu->arch.pid, as); |
| 142 | if (index == -1) |
| 143 | return NULL; |
Hollis Blanchard | db93f57 | 2008-11-05 09:36:18 -0600 | [diff] [blame] | 144 | return &vcpu_44x->guest_tlb[index]; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 145 | } |
| 146 | |
Hollis Blanchard | 0f55dc4 | 2008-11-05 09:36:12 -0600 | [diff] [blame] | 147 | static int kvmppc_44x_tlbe_is_writable(struct kvmppc_44x_tlbe *tlbe) |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 148 | { |
| 149 | return tlbe->word2 & (PPC44x_TLB_SW|PPC44x_TLB_UW); |
| 150 | } |
| 151 | |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 152 | static void kvmppc_44x_shadow_release(struct kvm_vcpu *vcpu, |
| 153 | unsigned int index) |
| 154 | { |
Hollis Blanchard | db93f57 | 2008-11-05 09:36:18 -0600 | [diff] [blame] | 155 | struct kvmppc_vcpu_44x *vcpu_44x = to_44x(vcpu); |
| 156 | struct kvmppc_44x_tlbe *stlbe = &vcpu_44x->shadow_tlb[index]; |
| 157 | struct page *page = vcpu_44x->shadow_pages[index]; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 158 | |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 159 | if (get_tlb_v(stlbe)) { |
| 160 | if (kvmppc_44x_tlbe_is_writable(stlbe)) |
| 161 | kvm_release_page_dirty(page); |
| 162 | else |
| 163 | kvm_release_page_clean(page); |
| 164 | } |
| 165 | } |
| 166 | |
Hollis Blanchard | c30f8a6 | 2008-11-24 11:37:38 -0600 | [diff] [blame] | 167 | void kvmppc_core_destroy_mmu(struct kvm_vcpu *vcpu) |
| 168 | { |
| 169 | int i; |
| 170 | |
| 171 | for (i = 0; i <= tlb_44x_hwater; i++) |
| 172 | kvmppc_44x_shadow_release(vcpu, i); |
| 173 | } |
| 174 | |
Hollis Blanchard | 83aae4a | 2008-07-25 13:54:52 -0500 | [diff] [blame] | 175 | void kvmppc_tlbe_set_modified(struct kvm_vcpu *vcpu, unsigned int i) |
| 176 | { |
Hollis Blanchard | db93f57 | 2008-11-05 09:36:18 -0600 | [diff] [blame] | 177 | struct kvmppc_vcpu_44x *vcpu_44x = to_44x(vcpu); |
| 178 | |
| 179 | vcpu_44x->shadow_tlb_mod[i] = 1; |
Hollis Blanchard | 83aae4a | 2008-07-25 13:54:52 -0500 | [diff] [blame] | 180 | } |
| 181 | |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 182 | /* Caller must ensure that the specified guest TLB entry is safe to insert into |
| 183 | * the shadow TLB. */ |
| 184 | void kvmppc_mmu_map(struct kvm_vcpu *vcpu, u64 gvaddr, gfn_t gfn, u64 asid, |
| 185 | u32 flags) |
| 186 | { |
Hollis Blanchard | db93f57 | 2008-11-05 09:36:18 -0600 | [diff] [blame] | 187 | struct kvmppc_vcpu_44x *vcpu_44x = to_44x(vcpu); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 188 | struct page *new_page; |
Hollis Blanchard | 0f55dc4 | 2008-11-05 09:36:12 -0600 | [diff] [blame] | 189 | struct kvmppc_44x_tlbe *stlbe; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 190 | hpa_t hpaddr; |
| 191 | unsigned int victim; |
| 192 | |
| 193 | /* Future optimization: don't overwrite the TLB entry containing the |
| 194 | * current PC (or stack?). */ |
| 195 | victim = kvmppc_tlb_44x_pos++; |
| 196 | if (kvmppc_tlb_44x_pos > tlb_44x_hwater) |
| 197 | kvmppc_tlb_44x_pos = 0; |
Hollis Blanchard | db93f57 | 2008-11-05 09:36:18 -0600 | [diff] [blame] | 198 | stlbe = &vcpu_44x->shadow_tlb[victim]; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 199 | |
| 200 | /* Get reference to new page. */ |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 201 | new_page = gfn_to_page(vcpu->kvm, gfn); |
| 202 | if (is_error_page(new_page)) { |
Hollis Blanchard | 9dcb40e | 2008-05-21 18:22:55 -0500 | [diff] [blame] | 203 | printk(KERN_ERR "Couldn't get guest page for gfn %lx!\n", gfn); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 204 | kvm_release_page_clean(new_page); |
| 205 | return; |
| 206 | } |
| 207 | hpaddr = page_to_phys(new_page); |
| 208 | |
| 209 | /* Drop reference to old page. */ |
| 210 | kvmppc_44x_shadow_release(vcpu, victim); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 211 | |
Hollis Blanchard | db93f57 | 2008-11-05 09:36:18 -0600 | [diff] [blame] | 212 | vcpu_44x->shadow_pages[victim] = new_page; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 213 | |
| 214 | /* XXX Make sure (va, size) doesn't overlap any other |
| 215 | * entries. 440x6 user manual says the result would be |
| 216 | * "undefined." */ |
| 217 | |
| 218 | /* XXX what about AS? */ |
| 219 | |
Hollis Blanchard | 49dd2c4 | 2008-07-25 13:54:53 -0500 | [diff] [blame] | 220 | stlbe->tid = !(asid & 0xff); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 221 | |
| 222 | /* Force TS=1 for all guest mappings. */ |
| 223 | /* For now we hardcode 4KB mappings, but it will be important to |
| 224 | * use host large pages in the future. */ |
| 225 | stlbe->word0 = (gvaddr & PAGE_MASK) | PPC44x_TLB_VALID | PPC44x_TLB_TS |
| 226 | | PPC44x_TLB_4K; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 227 | stlbe->word1 = (hpaddr & 0xfffffc00) | ((hpaddr >> 32) & 0xf); |
| 228 | stlbe->word2 = kvmppc_44x_tlb_shadow_attrib(flags, |
| 229 | vcpu->arch.msr & MSR_PR); |
Hollis Blanchard | 83aae4a | 2008-07-25 13:54:52 -0500 | [diff] [blame] | 230 | kvmppc_tlbe_set_modified(vcpu, victim); |
Jerone Young | 31711f2 | 2008-07-14 14:00:03 +0200 | [diff] [blame] | 231 | |
| 232 | KVMTRACE_5D(STLB_WRITE, vcpu, victim, |
| 233 | stlbe->tid, stlbe->word0, stlbe->word1, stlbe->word2, |
| 234 | handler); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 235 | } |
| 236 | |
Hollis Blanchard | a0d7b9f | 2008-11-05 09:36:11 -0600 | [diff] [blame] | 237 | static void kvmppc_mmu_invalidate(struct kvm_vcpu *vcpu, gva_t eaddr, |
| 238 | gva_t eend, u32 asid) |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 239 | { |
Hollis Blanchard | db93f57 | 2008-11-05 09:36:18 -0600 | [diff] [blame] | 240 | struct kvmppc_vcpu_44x *vcpu_44x = to_44x(vcpu); |
Hollis Blanchard | 49dd2c4 | 2008-07-25 13:54:53 -0500 | [diff] [blame] | 241 | unsigned int pid = !(asid & 0xff); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 242 | int i; |
| 243 | |
| 244 | /* XXX Replace loop with fancy data structures. */ |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 245 | for (i = 0; i <= tlb_44x_hwater; i++) { |
Hollis Blanchard | db93f57 | 2008-11-05 09:36:18 -0600 | [diff] [blame] | 246 | struct kvmppc_44x_tlbe *stlbe = &vcpu_44x->shadow_tlb[i]; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 247 | unsigned int tid; |
| 248 | |
| 249 | if (!get_tlb_v(stlbe)) |
| 250 | continue; |
| 251 | |
Hollis Blanchard | cc04454 | 2008-07-25 13:54:50 -0500 | [diff] [blame] | 252 | if (eend < get_tlb_eaddr(stlbe)) |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 253 | continue; |
| 254 | |
| 255 | if (eaddr > get_tlb_end(stlbe)) |
| 256 | continue; |
| 257 | |
| 258 | tid = get_tlb_tid(stlbe); |
| 259 | if (tid && (tid != pid)) |
| 260 | continue; |
| 261 | |
| 262 | kvmppc_44x_shadow_release(vcpu, i); |
| 263 | stlbe->word0 = 0; |
Hollis Blanchard | 83aae4a | 2008-07-25 13:54:52 -0500 | [diff] [blame] | 264 | kvmppc_tlbe_set_modified(vcpu, i); |
Jerone Young | 31711f2 | 2008-07-14 14:00:03 +0200 | [diff] [blame] | 265 | KVMTRACE_5D(STLB_INVAL, vcpu, i, |
| 266 | stlbe->tid, stlbe->word0, stlbe->word1, |
| 267 | stlbe->word2, handler); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 268 | } |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 269 | } |
| 270 | |
Hollis Blanchard | 49dd2c4 | 2008-07-25 13:54:53 -0500 | [diff] [blame] | 271 | /* Invalidate all mappings on the privilege switch after PID has been changed. |
| 272 | * The guest always runs with PID=1, so we must clear the entire TLB when |
| 273 | * switching address spaces. */ |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 274 | void kvmppc_mmu_priv_switch(struct kvm_vcpu *vcpu, int usermode) |
| 275 | { |
Hollis Blanchard | db93f57 | 2008-11-05 09:36:18 -0600 | [diff] [blame] | 276 | struct kvmppc_vcpu_44x *vcpu_44x = to_44x(vcpu); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 277 | int i; |
| 278 | |
Hollis Blanchard | 49dd2c4 | 2008-07-25 13:54:53 -0500 | [diff] [blame] | 279 | if (vcpu->arch.swap_pid) { |
| 280 | /* XXX Replace loop with fancy data structures. */ |
Hollis Blanchard | 49dd2c4 | 2008-07-25 13:54:53 -0500 | [diff] [blame] | 281 | for (i = 0; i <= tlb_44x_hwater; i++) { |
Hollis Blanchard | db93f57 | 2008-11-05 09:36:18 -0600 | [diff] [blame] | 282 | struct kvmppc_44x_tlbe *stlbe = &vcpu_44x->shadow_tlb[i]; |
Jerone Young | 31711f2 | 2008-07-14 14:00:03 +0200 | [diff] [blame] | 283 | |
Hollis Blanchard | 49dd2c4 | 2008-07-25 13:54:53 -0500 | [diff] [blame] | 284 | /* Future optimization: clear only userspace mappings. */ |
| 285 | kvmppc_44x_shadow_release(vcpu, i); |
| 286 | stlbe->word0 = 0; |
| 287 | kvmppc_tlbe_set_modified(vcpu, i); |
| 288 | KVMTRACE_5D(STLB_INVAL, vcpu, i, |
| 289 | stlbe->tid, stlbe->word0, stlbe->word1, |
| 290 | stlbe->word2, handler); |
| 291 | } |
Hollis Blanchard | 49dd2c4 | 2008-07-25 13:54:53 -0500 | [diff] [blame] | 292 | vcpu->arch.swap_pid = 0; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 293 | } |
Hollis Blanchard | 49dd2c4 | 2008-07-25 13:54:53 -0500 | [diff] [blame] | 294 | |
| 295 | vcpu->arch.shadow_pid = !usermode; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 296 | } |
Hollis Blanchard | a0d7b9f | 2008-11-05 09:36:11 -0600 | [diff] [blame] | 297 | |
| 298 | static int tlbe_is_host_safe(const struct kvm_vcpu *vcpu, |
Hollis Blanchard | 0f55dc4 | 2008-11-05 09:36:12 -0600 | [diff] [blame] | 299 | const struct kvmppc_44x_tlbe *tlbe) |
Hollis Blanchard | a0d7b9f | 2008-11-05 09:36:11 -0600 | [diff] [blame] | 300 | { |
| 301 | gpa_t gpa; |
| 302 | |
| 303 | if (!get_tlb_v(tlbe)) |
| 304 | return 0; |
| 305 | |
| 306 | /* Does it match current guest AS? */ |
| 307 | /* XXX what about IS != DS? */ |
| 308 | if (get_tlb_ts(tlbe) != !!(vcpu->arch.msr & MSR_IS)) |
| 309 | return 0; |
| 310 | |
| 311 | gpa = get_tlb_raddr(tlbe); |
| 312 | if (!gfn_to_memslot(vcpu->kvm, gpa >> PAGE_SHIFT)) |
| 313 | /* Mapping is not for RAM. */ |
| 314 | return 0; |
| 315 | |
| 316 | return 1; |
| 317 | } |
| 318 | |
Hollis Blanchard | 75f74f0 | 2008-11-05 09:36:16 -0600 | [diff] [blame] | 319 | 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] | 320 | { |
Hollis Blanchard | db93f57 | 2008-11-05 09:36:18 -0600 | [diff] [blame] | 321 | struct kvmppc_vcpu_44x *vcpu_44x = to_44x(vcpu); |
Hollis Blanchard | a0d7b9f | 2008-11-05 09:36:11 -0600 | [diff] [blame] | 322 | u64 eaddr; |
| 323 | u64 raddr; |
| 324 | u64 asid; |
| 325 | u32 flags; |
Hollis Blanchard | 0f55dc4 | 2008-11-05 09:36:12 -0600 | [diff] [blame] | 326 | struct kvmppc_44x_tlbe *tlbe; |
Hollis Blanchard | a0d7b9f | 2008-11-05 09:36:11 -0600 | [diff] [blame] | 327 | unsigned int index; |
| 328 | |
| 329 | index = vcpu->arch.gpr[ra]; |
| 330 | if (index > PPC44x_TLB_SIZE) { |
| 331 | printk("%s: index %d\n", __func__, index); |
| 332 | kvmppc_dump_vcpu(vcpu); |
| 333 | return EMULATE_FAIL; |
| 334 | } |
| 335 | |
Hollis Blanchard | db93f57 | 2008-11-05 09:36:18 -0600 | [diff] [blame] | 336 | tlbe = &vcpu_44x->guest_tlb[index]; |
Hollis Blanchard | a0d7b9f | 2008-11-05 09:36:11 -0600 | [diff] [blame] | 337 | |
| 338 | /* Invalidate shadow mappings for the about-to-be-clobbered TLBE. */ |
| 339 | if (tlbe->word0 & PPC44x_TLB_VALID) { |
| 340 | eaddr = get_tlb_eaddr(tlbe); |
| 341 | asid = (tlbe->word0 & PPC44x_TLB_TS) | tlbe->tid; |
| 342 | kvmppc_mmu_invalidate(vcpu, eaddr, get_tlb_end(tlbe), asid); |
| 343 | } |
| 344 | |
| 345 | switch (ws) { |
| 346 | case PPC44x_TLB_PAGEID: |
Hollis Blanchard | bf5d402 | 2008-11-10 14:57:34 -0600 | [diff] [blame] | 347 | tlbe->tid = get_mmucr_stid(vcpu); |
Hollis Blanchard | a0d7b9f | 2008-11-05 09:36:11 -0600 | [diff] [blame] | 348 | tlbe->word0 = vcpu->arch.gpr[rs]; |
| 349 | break; |
| 350 | |
| 351 | case PPC44x_TLB_XLAT: |
| 352 | tlbe->word1 = vcpu->arch.gpr[rs]; |
| 353 | break; |
| 354 | |
| 355 | case PPC44x_TLB_ATTRIB: |
| 356 | tlbe->word2 = vcpu->arch.gpr[rs]; |
| 357 | break; |
| 358 | |
| 359 | default: |
| 360 | return EMULATE_FAIL; |
| 361 | } |
| 362 | |
| 363 | if (tlbe_is_host_safe(vcpu, tlbe)) { |
| 364 | eaddr = get_tlb_eaddr(tlbe); |
| 365 | raddr = get_tlb_raddr(tlbe); |
| 366 | asid = (tlbe->word0 & PPC44x_TLB_TS) | tlbe->tid; |
| 367 | flags = tlbe->word2 & 0xffff; |
| 368 | |
| 369 | /* Create a 4KB mapping on the host. If the guest wanted a |
| 370 | * large page, only the first 4KB is mapped here and the rest |
| 371 | * are mapped on the fly. */ |
| 372 | kvmppc_mmu_map(vcpu, eaddr, raddr >> PAGE_SHIFT, asid, flags); |
| 373 | } |
| 374 | |
| 375 | KVMTRACE_5D(GTLB_WRITE, vcpu, index, |
| 376 | tlbe->tid, tlbe->word0, tlbe->word1, tlbe->word2, |
| 377 | handler); |
| 378 | |
| 379 | return EMULATE_DONE; |
| 380 | } |
| 381 | |
Hollis Blanchard | 75f74f0 | 2008-11-05 09:36:16 -0600 | [diff] [blame] | 382 | 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] | 383 | { |
| 384 | u32 ea; |
| 385 | int index; |
| 386 | unsigned int as = get_mmucr_sts(vcpu); |
| 387 | unsigned int pid = get_mmucr_stid(vcpu); |
| 388 | |
| 389 | ea = vcpu->arch.gpr[rb]; |
| 390 | if (ra) |
| 391 | ea += vcpu->arch.gpr[ra]; |
| 392 | |
| 393 | index = kvmppc_44x_tlb_index(vcpu, ea, pid, as); |
| 394 | if (rc) { |
| 395 | if (index < 0) |
| 396 | vcpu->arch.cr &= ~0x20000000; |
| 397 | else |
| 398 | vcpu->arch.cr |= 0x20000000; |
| 399 | } |
| 400 | vcpu->arch.gpr[rt] = index; |
| 401 | |
| 402 | return EMULATE_DONE; |
| 403 | } |