Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009. SUSE Linux Products GmbH. All rights reserved. |
| 3 | * |
| 4 | * Authors: |
| 5 | * Alexander Graf <agraf@suse.de> |
| 6 | * Kevin Wolf <mail@kevin-wolf.de> |
| 7 | * Paul Mackerras <paulus@samba.org> |
| 8 | * |
| 9 | * Description: |
| 10 | * Functions relating to running KVM on Book 3S processors where |
| 11 | * we don't have access to hypervisor mode, and we run the guest |
| 12 | * in problem state (user mode). |
| 13 | * |
| 14 | * This file is derived from arch/powerpc/kvm/44x.c, |
| 15 | * by Hollis Blanchard <hollisb@us.ibm.com>. |
| 16 | * |
| 17 | * This program is free software; you can redistribute it and/or modify |
| 18 | * it under the terms of the GNU General Public License, version 2, as |
| 19 | * published by the Free Software Foundation. |
| 20 | */ |
| 21 | |
| 22 | #include <linux/kvm_host.h> |
Paul Gortmaker | 9308794 | 2011-07-29 16:19:31 +1000 | [diff] [blame] | 23 | #include <linux/export.h> |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 24 | #include <linux/err.h> |
| 25 | #include <linux/slab.h> |
| 26 | |
| 27 | #include <asm/reg.h> |
| 28 | #include <asm/cputable.h> |
| 29 | #include <asm/cacheflush.h> |
| 30 | #include <asm/tlbflush.h> |
| 31 | #include <asm/uaccess.h> |
| 32 | #include <asm/io.h> |
| 33 | #include <asm/kvm_ppc.h> |
| 34 | #include <asm/kvm_book3s.h> |
| 35 | #include <asm/mmu_context.h> |
Benjamin Herrenschmidt | 95327d0 | 2012-04-01 17:35:53 +0000 | [diff] [blame] | 36 | #include <asm/switch_to.h> |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 37 | #include <linux/gfp.h> |
| 38 | #include <linux/sched.h> |
| 39 | #include <linux/vmalloc.h> |
| 40 | #include <linux/highmem.h> |
| 41 | |
| 42 | #include "trace.h" |
| 43 | |
| 44 | /* #define EXIT_DEBUG */ |
| 45 | /* #define DEBUG_EXT */ |
| 46 | |
| 47 | static int kvmppc_handle_ext(struct kvm_vcpu *vcpu, unsigned int exit_nr, |
| 48 | ulong msr); |
| 49 | |
| 50 | /* Some compatibility defines */ |
| 51 | #ifdef CONFIG_PPC_BOOK3S_32 |
| 52 | #define MSR_USER32 MSR_USER |
| 53 | #define MSR_USER64 MSR_USER |
| 54 | #define HW_PAGE_SIZE PAGE_SIZE |
| 55 | #endif |
| 56 | |
| 57 | void kvmppc_core_vcpu_load(struct kvm_vcpu *vcpu, int cpu) |
| 58 | { |
| 59 | #ifdef CONFIG_PPC_BOOK3S_64 |
Alexander Graf | 468a12c | 2011-12-09 14:44:13 +0100 | [diff] [blame] | 60 | struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu); |
| 61 | memcpy(svcpu->slb, to_book3s(vcpu)->slb_shadow, sizeof(svcpu->slb)); |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 62 | memcpy(&get_paca()->shadow_vcpu, to_book3s(vcpu)->shadow_vcpu, |
| 63 | sizeof(get_paca()->shadow_vcpu)); |
Alexander Graf | 468a12c | 2011-12-09 14:44:13 +0100 | [diff] [blame] | 64 | svcpu->slb_max = to_book3s(vcpu)->slb_shadow_max; |
| 65 | svcpu_put(svcpu); |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 66 | #endif |
| 67 | |
| 68 | #ifdef CONFIG_PPC_BOOK3S_32 |
| 69 | current->thread.kvm_shadow_vcpu = to_book3s(vcpu)->shadow_vcpu; |
| 70 | #endif |
| 71 | } |
| 72 | |
| 73 | void kvmppc_core_vcpu_put(struct kvm_vcpu *vcpu) |
| 74 | { |
| 75 | #ifdef CONFIG_PPC_BOOK3S_64 |
Alexander Graf | 468a12c | 2011-12-09 14:44:13 +0100 | [diff] [blame] | 76 | struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu); |
| 77 | memcpy(to_book3s(vcpu)->slb_shadow, svcpu->slb, sizeof(svcpu->slb)); |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 78 | memcpy(to_book3s(vcpu)->shadow_vcpu, &get_paca()->shadow_vcpu, |
| 79 | sizeof(get_paca()->shadow_vcpu)); |
Alexander Graf | 468a12c | 2011-12-09 14:44:13 +0100 | [diff] [blame] | 80 | to_book3s(vcpu)->slb_shadow_max = svcpu->slb_max; |
| 81 | svcpu_put(svcpu); |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 82 | #endif |
| 83 | |
| 84 | kvmppc_giveup_ext(vcpu, MSR_FP); |
| 85 | kvmppc_giveup_ext(vcpu, MSR_VEC); |
| 86 | kvmppc_giveup_ext(vcpu, MSR_VSX); |
| 87 | } |
| 88 | |
Alexander Graf | 03d25c5 | 2012-08-10 12:28:50 +0200 | [diff] [blame] | 89 | void kvmppc_core_check_requests(struct kvm_vcpu *vcpu) |
| 90 | { |
Alexander Graf | 9b0cb3c | 2012-08-10 13:23:55 +0200 | [diff] [blame] | 91 | /* We misuse TLB_FLUSH to indicate that we want to clear |
| 92 | all shadow cache entries */ |
| 93 | if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu)) |
| 94 | kvmppc_mmu_pte_flush(vcpu, 0, 0); |
Alexander Graf | 03d25c5 | 2012-08-10 12:28:50 +0200 | [diff] [blame] | 95 | } |
| 96 | |
Alexander Graf | 9b0cb3c | 2012-08-10 13:23:55 +0200 | [diff] [blame] | 97 | /************* MMU Notifiers *************/ |
| 98 | |
| 99 | int kvm_unmap_hva(struct kvm *kvm, unsigned long hva) |
| 100 | { |
| 101 | trace_kvm_unmap_hva(hva); |
| 102 | |
| 103 | /* |
| 104 | * Flush all shadow tlb entries everywhere. This is slow, but |
| 105 | * we are 100% sure that we catch the to be unmapped page |
| 106 | */ |
| 107 | kvm_flush_remote_tlbs(kvm); |
| 108 | |
| 109 | return 0; |
| 110 | } |
| 111 | |
| 112 | int kvm_unmap_hva_range(struct kvm *kvm, unsigned long start, unsigned long end) |
| 113 | { |
| 114 | /* kvm_unmap_hva flushes everything anyways */ |
| 115 | kvm_unmap_hva(kvm, start); |
| 116 | |
| 117 | return 0; |
| 118 | } |
| 119 | |
| 120 | int kvm_age_hva(struct kvm *kvm, unsigned long hva) |
| 121 | { |
| 122 | /* XXX could be more clever ;) */ |
| 123 | return 0; |
| 124 | } |
| 125 | |
| 126 | int kvm_test_age_hva(struct kvm *kvm, unsigned long hva) |
| 127 | { |
| 128 | /* XXX could be more clever ;) */ |
| 129 | return 0; |
| 130 | } |
| 131 | |
| 132 | void kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte) |
| 133 | { |
| 134 | /* The page will get remapped properly on its next fault */ |
| 135 | kvm_unmap_hva(kvm, hva); |
| 136 | } |
| 137 | |
| 138 | /*****************************************/ |
| 139 | |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 140 | static void kvmppc_recalc_shadow_msr(struct kvm_vcpu *vcpu) |
| 141 | { |
| 142 | ulong smsr = vcpu->arch.shared->msr; |
| 143 | |
| 144 | /* Guest MSR values */ |
| 145 | smsr &= MSR_FE0 | MSR_FE1 | MSR_SF | MSR_SE | MSR_BE | MSR_DE; |
| 146 | /* Process MSR values */ |
| 147 | smsr |= MSR_ME | MSR_RI | MSR_IR | MSR_DR | MSR_PR | MSR_EE; |
| 148 | /* External providers the guest reserved */ |
| 149 | smsr |= (vcpu->arch.shared->msr & vcpu->arch.guest_owned_ext); |
| 150 | /* 64-bit Process MSR values */ |
| 151 | #ifdef CONFIG_PPC_BOOK3S_64 |
| 152 | smsr |= MSR_ISF | MSR_HV; |
| 153 | #endif |
| 154 | vcpu->arch.shadow_msr = smsr; |
| 155 | } |
| 156 | |
| 157 | void kvmppc_set_msr(struct kvm_vcpu *vcpu, u64 msr) |
| 158 | { |
| 159 | ulong old_msr = vcpu->arch.shared->msr; |
| 160 | |
| 161 | #ifdef EXIT_DEBUG |
| 162 | printk(KERN_INFO "KVM: Set MSR to 0x%llx\n", msr); |
| 163 | #endif |
| 164 | |
| 165 | msr &= to_book3s(vcpu)->msr_mask; |
| 166 | vcpu->arch.shared->msr = msr; |
| 167 | kvmppc_recalc_shadow_msr(vcpu); |
| 168 | |
| 169 | if (msr & MSR_POW) { |
| 170 | if (!vcpu->arch.pending_exceptions) { |
| 171 | kvm_vcpu_block(vcpu); |
Alexander Graf | 966cd0f | 2012-03-14 16:55:08 +0100 | [diff] [blame] | 172 | clear_bit(KVM_REQ_UNHALT, &vcpu->requests); |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 173 | vcpu->stat.halt_wakeup++; |
| 174 | |
| 175 | /* Unset POW bit after we woke up */ |
| 176 | msr &= ~MSR_POW; |
| 177 | vcpu->arch.shared->msr = msr; |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | if ((vcpu->arch.shared->msr & (MSR_PR|MSR_IR|MSR_DR)) != |
| 182 | (old_msr & (MSR_PR|MSR_IR|MSR_DR))) { |
| 183 | kvmppc_mmu_flush_segments(vcpu); |
| 184 | kvmppc_mmu_map_segment(vcpu, kvmppc_get_pc(vcpu)); |
| 185 | |
| 186 | /* Preload magic page segment when in kernel mode */ |
| 187 | if (!(msr & MSR_PR) && vcpu->arch.magic_page_pa) { |
| 188 | struct kvm_vcpu_arch *a = &vcpu->arch; |
| 189 | |
| 190 | if (msr & MSR_DR) |
| 191 | kvmppc_mmu_map_segment(vcpu, a->magic_page_ea); |
| 192 | else |
| 193 | kvmppc_mmu_map_segment(vcpu, a->magic_page_pa); |
| 194 | } |
| 195 | } |
| 196 | |
Benjamin Herrenschmidt | bbcc9c0 | 2012-03-13 21:52:44 +0000 | [diff] [blame] | 197 | /* |
| 198 | * When switching from 32 to 64-bit, we may have a stale 32-bit |
| 199 | * magic page around, we need to flush it. Typically 32-bit magic |
| 200 | * page will be instanciated when calling into RTAS. Note: We |
| 201 | * assume that such transition only happens while in kernel mode, |
| 202 | * ie, we never transition from user 32-bit to kernel 64-bit with |
| 203 | * a 32-bit magic page around. |
| 204 | */ |
| 205 | if (vcpu->arch.magic_page_pa && |
| 206 | !(old_msr & MSR_PR) && !(old_msr & MSR_SF) && (msr & MSR_SF)) { |
| 207 | /* going from RTAS to normal kernel code */ |
| 208 | kvmppc_mmu_pte_flush(vcpu, (uint32_t)vcpu->arch.magic_page_pa, |
| 209 | ~0xFFFUL); |
| 210 | } |
| 211 | |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 212 | /* Preload FPU if it's enabled */ |
| 213 | if (vcpu->arch.shared->msr & MSR_FP) |
| 214 | kvmppc_handle_ext(vcpu, BOOK3S_INTERRUPT_FP_UNAVAIL, MSR_FP); |
| 215 | } |
| 216 | |
| 217 | void kvmppc_set_pvr(struct kvm_vcpu *vcpu, u32 pvr) |
| 218 | { |
| 219 | u32 host_pvr; |
| 220 | |
| 221 | vcpu->arch.hflags &= ~BOOK3S_HFLAG_SLB; |
| 222 | vcpu->arch.pvr = pvr; |
| 223 | #ifdef CONFIG_PPC_BOOK3S_64 |
| 224 | if ((pvr >= 0x330000) && (pvr < 0x70330000)) { |
| 225 | kvmppc_mmu_book3s_64_init(vcpu); |
Alexander Graf | 1022fc3 | 2011-09-14 21:45:23 +0200 | [diff] [blame] | 226 | if (!to_book3s(vcpu)->hior_explicit) |
| 227 | to_book3s(vcpu)->hior = 0xfff00000; |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 228 | to_book3s(vcpu)->msr_mask = 0xffffffffffffffffULL; |
Alexander Graf | af8f38b | 2011-08-10 13:57:08 +0200 | [diff] [blame] | 229 | vcpu->arch.cpu_type = KVM_CPU_3S_64; |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 230 | } else |
| 231 | #endif |
| 232 | { |
| 233 | kvmppc_mmu_book3s_32_init(vcpu); |
Alexander Graf | 1022fc3 | 2011-09-14 21:45:23 +0200 | [diff] [blame] | 234 | if (!to_book3s(vcpu)->hior_explicit) |
| 235 | to_book3s(vcpu)->hior = 0; |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 236 | to_book3s(vcpu)->msr_mask = 0xffffffffULL; |
Alexander Graf | af8f38b | 2011-08-10 13:57:08 +0200 | [diff] [blame] | 237 | vcpu->arch.cpu_type = KVM_CPU_3S_32; |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 238 | } |
| 239 | |
Alexander Graf | af8f38b | 2011-08-10 13:57:08 +0200 | [diff] [blame] | 240 | kvmppc_sanity_check(vcpu); |
| 241 | |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 242 | /* If we are in hypervisor level on 970, we can tell the CPU to |
| 243 | * treat DCBZ as 32 bytes store */ |
| 244 | vcpu->arch.hflags &= ~BOOK3S_HFLAG_DCBZ32; |
| 245 | if (vcpu->arch.mmu.is_dcbz32(vcpu) && (mfmsr() & MSR_HV) && |
| 246 | !strcmp(cur_cpu_spec->platform, "ppc970")) |
| 247 | vcpu->arch.hflags |= BOOK3S_HFLAG_DCBZ32; |
| 248 | |
| 249 | /* Cell performs badly if MSR_FEx are set. So let's hope nobody |
| 250 | really needs them in a VM on Cell and force disable them. */ |
| 251 | if (!strcmp(cur_cpu_spec->platform, "ppc-cell-be")) |
| 252 | to_book3s(vcpu)->msr_mask &= ~(MSR_FE0 | MSR_FE1); |
| 253 | |
| 254 | #ifdef CONFIG_PPC_BOOK3S_32 |
| 255 | /* 32 bit Book3S always has 32 byte dcbz */ |
| 256 | vcpu->arch.hflags |= BOOK3S_HFLAG_DCBZ32; |
| 257 | #endif |
| 258 | |
| 259 | /* On some CPUs we can execute paired single operations natively */ |
| 260 | asm ( "mfpvr %0" : "=r"(host_pvr)); |
| 261 | switch (host_pvr) { |
| 262 | case 0x00080200: /* lonestar 2.0 */ |
| 263 | case 0x00088202: /* lonestar 2.2 */ |
| 264 | case 0x70000100: /* gekko 1.0 */ |
| 265 | case 0x00080100: /* gekko 2.0 */ |
| 266 | case 0x00083203: /* gekko 2.3a */ |
| 267 | case 0x00083213: /* gekko 2.3b */ |
| 268 | case 0x00083204: /* gekko 2.4 */ |
| 269 | case 0x00083214: /* gekko 2.4e (8SE) - retail HW2 */ |
| 270 | case 0x00087200: /* broadway */ |
| 271 | vcpu->arch.hflags |= BOOK3S_HFLAG_NATIVE_PS; |
| 272 | /* Enable HID2.PSE - in case we need it later */ |
| 273 | mtspr(SPRN_HID2_GEKKO, mfspr(SPRN_HID2_GEKKO) | (1 << 29)); |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | /* Book3s_32 CPUs always have 32 bytes cache line size, which Linux assumes. To |
| 278 | * make Book3s_32 Linux work on Book3s_64, we have to make sure we trap dcbz to |
| 279 | * emulate 32 bytes dcbz length. |
| 280 | * |
| 281 | * The Book3s_64 inventors also realized this case and implemented a special bit |
| 282 | * in the HID5 register, which is a hypervisor ressource. Thus we can't use it. |
| 283 | * |
| 284 | * My approach here is to patch the dcbz instruction on executing pages. |
| 285 | */ |
| 286 | static void kvmppc_patch_dcbz(struct kvm_vcpu *vcpu, struct kvmppc_pte *pte) |
| 287 | { |
| 288 | struct page *hpage; |
| 289 | u64 hpage_offset; |
| 290 | u32 *page; |
| 291 | int i; |
| 292 | |
| 293 | hpage = gfn_to_page(vcpu->kvm, pte->raddr >> PAGE_SHIFT); |
Xiao Guangrong | 32cad84 | 2012-08-03 15:42:52 +0800 | [diff] [blame] | 294 | if (is_error_page(hpage)) |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 295 | return; |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 296 | |
| 297 | hpage_offset = pte->raddr & ~PAGE_MASK; |
| 298 | hpage_offset &= ~0xFFFULL; |
| 299 | hpage_offset /= 4; |
| 300 | |
| 301 | get_page(hpage); |
Cong Wang | 2480b20 | 2011-11-25 23:14:16 +0800 | [diff] [blame] | 302 | page = kmap_atomic(hpage); |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 303 | |
| 304 | /* patch dcbz into reserved instruction, so we trap */ |
| 305 | for (i=hpage_offset; i < hpage_offset + (HW_PAGE_SIZE / 4); i++) |
| 306 | if ((page[i] & 0xff0007ff) == INS_DCBZ) |
| 307 | page[i] &= 0xfffffff7; |
| 308 | |
Cong Wang | 2480b20 | 2011-11-25 23:14:16 +0800 | [diff] [blame] | 309 | kunmap_atomic(page); |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 310 | put_page(hpage); |
| 311 | } |
| 312 | |
| 313 | static int kvmppc_visible_gfn(struct kvm_vcpu *vcpu, gfn_t gfn) |
| 314 | { |
| 315 | ulong mp_pa = vcpu->arch.magic_page_pa; |
| 316 | |
Benjamin Herrenschmidt | bbcc9c0 | 2012-03-13 21:52:44 +0000 | [diff] [blame] | 317 | if (!(vcpu->arch.shared->msr & MSR_SF)) |
| 318 | mp_pa = (uint32_t)mp_pa; |
| 319 | |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 320 | if (unlikely(mp_pa) && |
| 321 | unlikely((mp_pa & KVM_PAM) >> PAGE_SHIFT == gfn)) { |
| 322 | return 1; |
| 323 | } |
| 324 | |
| 325 | return kvm_is_visible_gfn(vcpu->kvm, gfn); |
| 326 | } |
| 327 | |
| 328 | int kvmppc_handle_pagefault(struct kvm_run *run, struct kvm_vcpu *vcpu, |
| 329 | ulong eaddr, int vec) |
| 330 | { |
| 331 | bool data = (vec == BOOK3S_INTERRUPT_DATA_STORAGE); |
| 332 | int r = RESUME_GUEST; |
| 333 | int relocated; |
| 334 | int page_found = 0; |
| 335 | struct kvmppc_pte pte; |
| 336 | bool is_mmio = false; |
| 337 | bool dr = (vcpu->arch.shared->msr & MSR_DR) ? true : false; |
| 338 | bool ir = (vcpu->arch.shared->msr & MSR_IR) ? true : false; |
| 339 | u64 vsid; |
| 340 | |
| 341 | relocated = data ? dr : ir; |
| 342 | |
| 343 | /* Resolve real address if translation turned on */ |
| 344 | if (relocated) { |
| 345 | page_found = vcpu->arch.mmu.xlate(vcpu, eaddr, &pte, data); |
| 346 | } else { |
| 347 | pte.may_execute = true; |
| 348 | pte.may_read = true; |
| 349 | pte.may_write = true; |
| 350 | pte.raddr = eaddr & KVM_PAM; |
| 351 | pte.eaddr = eaddr; |
| 352 | pte.vpage = eaddr >> 12; |
| 353 | } |
| 354 | |
| 355 | switch (vcpu->arch.shared->msr & (MSR_DR|MSR_IR)) { |
| 356 | case 0: |
| 357 | pte.vpage |= ((u64)VSID_REAL << (SID_SHIFT - 12)); |
| 358 | break; |
| 359 | case MSR_DR: |
| 360 | case MSR_IR: |
| 361 | vcpu->arch.mmu.esid_to_vsid(vcpu, eaddr >> SID_SHIFT, &vsid); |
| 362 | |
| 363 | if ((vcpu->arch.shared->msr & (MSR_DR|MSR_IR)) == MSR_DR) |
| 364 | pte.vpage |= ((u64)VSID_REAL_DR << (SID_SHIFT - 12)); |
| 365 | else |
| 366 | pte.vpage |= ((u64)VSID_REAL_IR << (SID_SHIFT - 12)); |
| 367 | pte.vpage |= vsid; |
| 368 | |
| 369 | if (vsid == -1) |
| 370 | page_found = -EINVAL; |
| 371 | break; |
| 372 | } |
| 373 | |
| 374 | if (vcpu->arch.mmu.is_dcbz32(vcpu) && |
| 375 | (!(vcpu->arch.hflags & BOOK3S_HFLAG_DCBZ32))) { |
| 376 | /* |
| 377 | * If we do the dcbz hack, we have to NX on every execution, |
| 378 | * so we can patch the executing code. This renders our guest |
| 379 | * NX-less. |
| 380 | */ |
| 381 | pte.may_execute = !data; |
| 382 | } |
| 383 | |
| 384 | if (page_found == -ENOENT) { |
| 385 | /* Page not found in guest PTE entries */ |
Alexander Graf | 468a12c | 2011-12-09 14:44:13 +0100 | [diff] [blame] | 386 | struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu); |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 387 | vcpu->arch.shared->dar = kvmppc_get_fault_dar(vcpu); |
Alexander Graf | 468a12c | 2011-12-09 14:44:13 +0100 | [diff] [blame] | 388 | vcpu->arch.shared->dsisr = svcpu->fault_dsisr; |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 389 | vcpu->arch.shared->msr |= |
Alexander Graf | 468a12c | 2011-12-09 14:44:13 +0100 | [diff] [blame] | 390 | (svcpu->shadow_srr1 & 0x00000000f8000000ULL); |
| 391 | svcpu_put(svcpu); |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 392 | kvmppc_book3s_queue_irqprio(vcpu, vec); |
| 393 | } else if (page_found == -EPERM) { |
| 394 | /* Storage protection */ |
Alexander Graf | 468a12c | 2011-12-09 14:44:13 +0100 | [diff] [blame] | 395 | struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu); |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 396 | vcpu->arch.shared->dar = kvmppc_get_fault_dar(vcpu); |
Alexander Graf | 468a12c | 2011-12-09 14:44:13 +0100 | [diff] [blame] | 397 | vcpu->arch.shared->dsisr = svcpu->fault_dsisr & ~DSISR_NOHPTE; |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 398 | vcpu->arch.shared->dsisr |= DSISR_PROTFAULT; |
| 399 | vcpu->arch.shared->msr |= |
Alexander Graf | 468a12c | 2011-12-09 14:44:13 +0100 | [diff] [blame] | 400 | svcpu->shadow_srr1 & 0x00000000f8000000ULL; |
| 401 | svcpu_put(svcpu); |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 402 | kvmppc_book3s_queue_irqprio(vcpu, vec); |
| 403 | } else if (page_found == -EINVAL) { |
| 404 | /* Page not found in guest SLB */ |
| 405 | vcpu->arch.shared->dar = kvmppc_get_fault_dar(vcpu); |
| 406 | kvmppc_book3s_queue_irqprio(vcpu, vec + 0x80); |
| 407 | } else if (!is_mmio && |
| 408 | kvmppc_visible_gfn(vcpu, pte.raddr >> PAGE_SHIFT)) { |
| 409 | /* The guest's PTE is not mapped yet. Map on the host */ |
| 410 | kvmppc_mmu_map_page(vcpu, &pte); |
| 411 | if (data) |
| 412 | vcpu->stat.sp_storage++; |
| 413 | else if (vcpu->arch.mmu.is_dcbz32(vcpu) && |
| 414 | (!(vcpu->arch.hflags & BOOK3S_HFLAG_DCBZ32))) |
| 415 | kvmppc_patch_dcbz(vcpu, &pte); |
| 416 | } else { |
| 417 | /* MMIO */ |
| 418 | vcpu->stat.mmio_exits++; |
| 419 | vcpu->arch.paddr_accessed = pte.raddr; |
Alexander Graf | 6020c0f | 2012-03-12 02:26:30 +0100 | [diff] [blame] | 420 | vcpu->arch.vaddr_accessed = pte.eaddr; |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 421 | r = kvmppc_emulate_mmio(run, vcpu); |
| 422 | if ( r == RESUME_HOST_NV ) |
| 423 | r = RESUME_HOST; |
| 424 | } |
| 425 | |
| 426 | return r; |
| 427 | } |
| 428 | |
| 429 | static inline int get_fpr_index(int i) |
| 430 | { |
| 431 | #ifdef CONFIG_VSX |
| 432 | i *= 2; |
| 433 | #endif |
| 434 | return i; |
| 435 | } |
| 436 | |
| 437 | /* Give up external provider (FPU, Altivec, VSX) */ |
| 438 | void kvmppc_giveup_ext(struct kvm_vcpu *vcpu, ulong msr) |
| 439 | { |
| 440 | struct thread_struct *t = ¤t->thread; |
| 441 | u64 *vcpu_fpr = vcpu->arch.fpr; |
| 442 | #ifdef CONFIG_VSX |
| 443 | u64 *vcpu_vsx = vcpu->arch.vsr; |
| 444 | #endif |
| 445 | u64 *thread_fpr = (u64*)t->fpr; |
| 446 | int i; |
| 447 | |
| 448 | if (!(vcpu->arch.guest_owned_ext & msr)) |
| 449 | return; |
| 450 | |
| 451 | #ifdef DEBUG_EXT |
| 452 | printk(KERN_INFO "Giving up ext 0x%lx\n", msr); |
| 453 | #endif |
| 454 | |
| 455 | switch (msr) { |
| 456 | case MSR_FP: |
| 457 | giveup_fpu(current); |
| 458 | for (i = 0; i < ARRAY_SIZE(vcpu->arch.fpr); i++) |
| 459 | vcpu_fpr[i] = thread_fpr[get_fpr_index(i)]; |
| 460 | |
| 461 | vcpu->arch.fpscr = t->fpscr.val; |
| 462 | break; |
| 463 | case MSR_VEC: |
| 464 | #ifdef CONFIG_ALTIVEC |
| 465 | giveup_altivec(current); |
| 466 | memcpy(vcpu->arch.vr, t->vr, sizeof(vcpu->arch.vr)); |
| 467 | vcpu->arch.vscr = t->vscr; |
| 468 | #endif |
| 469 | break; |
| 470 | case MSR_VSX: |
| 471 | #ifdef CONFIG_VSX |
| 472 | __giveup_vsx(current); |
| 473 | for (i = 0; i < ARRAY_SIZE(vcpu->arch.vsr); i++) |
| 474 | vcpu_vsx[i] = thread_fpr[get_fpr_index(i) + 1]; |
| 475 | #endif |
| 476 | break; |
| 477 | default: |
| 478 | BUG(); |
| 479 | } |
| 480 | |
| 481 | vcpu->arch.guest_owned_ext &= ~msr; |
| 482 | current->thread.regs->msr &= ~msr; |
| 483 | kvmppc_recalc_shadow_msr(vcpu); |
| 484 | } |
| 485 | |
| 486 | static int kvmppc_read_inst(struct kvm_vcpu *vcpu) |
| 487 | { |
| 488 | ulong srr0 = kvmppc_get_pc(vcpu); |
| 489 | u32 last_inst = kvmppc_get_last_inst(vcpu); |
| 490 | int ret; |
| 491 | |
| 492 | ret = kvmppc_ld(vcpu, &srr0, sizeof(u32), &last_inst, false); |
| 493 | if (ret == -ENOENT) { |
| 494 | ulong msr = vcpu->arch.shared->msr; |
| 495 | |
| 496 | msr = kvmppc_set_field(msr, 33, 33, 1); |
| 497 | msr = kvmppc_set_field(msr, 34, 36, 0); |
| 498 | vcpu->arch.shared->msr = kvmppc_set_field(msr, 42, 47, 0); |
| 499 | kvmppc_book3s_queue_irqprio(vcpu, BOOK3S_INTERRUPT_INST_STORAGE); |
| 500 | return EMULATE_AGAIN; |
| 501 | } |
| 502 | |
| 503 | return EMULATE_DONE; |
| 504 | } |
| 505 | |
| 506 | static int kvmppc_check_ext(struct kvm_vcpu *vcpu, unsigned int exit_nr) |
| 507 | { |
| 508 | |
| 509 | /* Need to do paired single emulation? */ |
| 510 | if (!(vcpu->arch.hflags & BOOK3S_HFLAG_PAIRED_SINGLE)) |
| 511 | return EMULATE_DONE; |
| 512 | |
| 513 | /* Read out the instruction */ |
| 514 | if (kvmppc_read_inst(vcpu) == EMULATE_DONE) |
| 515 | /* Need to emulate */ |
| 516 | return EMULATE_FAIL; |
| 517 | |
| 518 | return EMULATE_AGAIN; |
| 519 | } |
| 520 | |
| 521 | /* Handle external providers (FPU, Altivec, VSX) */ |
| 522 | static int kvmppc_handle_ext(struct kvm_vcpu *vcpu, unsigned int exit_nr, |
| 523 | ulong msr) |
| 524 | { |
| 525 | struct thread_struct *t = ¤t->thread; |
| 526 | u64 *vcpu_fpr = vcpu->arch.fpr; |
| 527 | #ifdef CONFIG_VSX |
| 528 | u64 *vcpu_vsx = vcpu->arch.vsr; |
| 529 | #endif |
| 530 | u64 *thread_fpr = (u64*)t->fpr; |
| 531 | int i; |
| 532 | |
| 533 | /* When we have paired singles, we emulate in software */ |
| 534 | if (vcpu->arch.hflags & BOOK3S_HFLAG_PAIRED_SINGLE) |
| 535 | return RESUME_GUEST; |
| 536 | |
| 537 | if (!(vcpu->arch.shared->msr & msr)) { |
| 538 | kvmppc_book3s_queue_irqprio(vcpu, exit_nr); |
| 539 | return RESUME_GUEST; |
| 540 | } |
| 541 | |
| 542 | /* We already own the ext */ |
| 543 | if (vcpu->arch.guest_owned_ext & msr) { |
| 544 | return RESUME_GUEST; |
| 545 | } |
| 546 | |
| 547 | #ifdef DEBUG_EXT |
| 548 | printk(KERN_INFO "Loading up ext 0x%lx\n", msr); |
| 549 | #endif |
| 550 | |
| 551 | current->thread.regs->msr |= msr; |
| 552 | |
| 553 | switch (msr) { |
| 554 | case MSR_FP: |
| 555 | for (i = 0; i < ARRAY_SIZE(vcpu->arch.fpr); i++) |
| 556 | thread_fpr[get_fpr_index(i)] = vcpu_fpr[i]; |
| 557 | |
| 558 | t->fpscr.val = vcpu->arch.fpscr; |
| 559 | t->fpexc_mode = 0; |
| 560 | kvmppc_load_up_fpu(); |
| 561 | break; |
| 562 | case MSR_VEC: |
| 563 | #ifdef CONFIG_ALTIVEC |
| 564 | memcpy(t->vr, vcpu->arch.vr, sizeof(vcpu->arch.vr)); |
| 565 | t->vscr = vcpu->arch.vscr; |
| 566 | t->vrsave = -1; |
| 567 | kvmppc_load_up_altivec(); |
| 568 | #endif |
| 569 | break; |
| 570 | case MSR_VSX: |
| 571 | #ifdef CONFIG_VSX |
| 572 | for (i = 0; i < ARRAY_SIZE(vcpu->arch.vsr); i++) |
| 573 | thread_fpr[get_fpr_index(i) + 1] = vcpu_vsx[i]; |
| 574 | kvmppc_load_up_vsx(); |
| 575 | #endif |
| 576 | break; |
| 577 | default: |
| 578 | BUG(); |
| 579 | } |
| 580 | |
| 581 | vcpu->arch.guest_owned_ext |= msr; |
| 582 | |
| 583 | kvmppc_recalc_shadow_msr(vcpu); |
| 584 | |
| 585 | return RESUME_GUEST; |
| 586 | } |
| 587 | |
| 588 | int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu, |
| 589 | unsigned int exit_nr) |
| 590 | { |
| 591 | int r = RESUME_HOST; |
| 592 | |
| 593 | vcpu->stat.sum_exits++; |
| 594 | |
| 595 | run->exit_reason = KVM_EXIT_UNKNOWN; |
| 596 | run->ready_for_interrupt_injection = 1; |
| 597 | |
Alexander Graf | bd2be68 | 2012-08-13 01:04:19 +0200 | [diff] [blame^] | 598 | /* We get here with MSR.EE=1 */ |
Alexander Graf | 3b1d9d7 | 2012-04-30 10:56:12 +0200 | [diff] [blame] | 599 | |
Alexander Graf | 97c9505 | 2012-08-02 15:10:00 +0200 | [diff] [blame] | 600 | trace_kvm_exit(exit_nr, vcpu); |
Alexander Graf | 706fb73 | 2012-08-12 11:29:09 +0200 | [diff] [blame] | 601 | kvm_guest_exit(); |
Alexander Graf | c63ddcb | 2012-08-12 11:27:49 +0200 | [diff] [blame] | 602 | |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 603 | switch (exit_nr) { |
| 604 | case BOOK3S_INTERRUPT_INST_STORAGE: |
Alexander Graf | 468a12c | 2011-12-09 14:44:13 +0100 | [diff] [blame] | 605 | { |
| 606 | struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu); |
| 607 | ulong shadow_srr1 = svcpu->shadow_srr1; |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 608 | vcpu->stat.pf_instruc++; |
| 609 | |
| 610 | #ifdef CONFIG_PPC_BOOK3S_32 |
| 611 | /* We set segments as unused segments when invalidating them. So |
| 612 | * treat the respective fault as segment fault. */ |
Alexander Graf | 468a12c | 2011-12-09 14:44:13 +0100 | [diff] [blame] | 613 | if (svcpu->sr[kvmppc_get_pc(vcpu) >> SID_SHIFT] == SR_INVALID) { |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 614 | kvmppc_mmu_map_segment(vcpu, kvmppc_get_pc(vcpu)); |
| 615 | r = RESUME_GUEST; |
Alexander Graf | 468a12c | 2011-12-09 14:44:13 +0100 | [diff] [blame] | 616 | svcpu_put(svcpu); |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 617 | break; |
| 618 | } |
| 619 | #endif |
Alexander Graf | 468a12c | 2011-12-09 14:44:13 +0100 | [diff] [blame] | 620 | svcpu_put(svcpu); |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 621 | |
| 622 | /* only care about PTEG not found errors, but leave NX alone */ |
Alexander Graf | 468a12c | 2011-12-09 14:44:13 +0100 | [diff] [blame] | 623 | if (shadow_srr1 & 0x40000000) { |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 624 | r = kvmppc_handle_pagefault(run, vcpu, kvmppc_get_pc(vcpu), exit_nr); |
| 625 | vcpu->stat.sp_instruc++; |
| 626 | } else if (vcpu->arch.mmu.is_dcbz32(vcpu) && |
| 627 | (!(vcpu->arch.hflags & BOOK3S_HFLAG_DCBZ32))) { |
| 628 | /* |
| 629 | * XXX If we do the dcbz hack we use the NX bit to flush&patch the page, |
| 630 | * so we can't use the NX bit inside the guest. Let's cross our fingers, |
| 631 | * that no guest that needs the dcbz hack does NX. |
| 632 | */ |
| 633 | kvmppc_mmu_pte_flush(vcpu, kvmppc_get_pc(vcpu), ~0xFFFUL); |
| 634 | r = RESUME_GUEST; |
| 635 | } else { |
Alexander Graf | 468a12c | 2011-12-09 14:44:13 +0100 | [diff] [blame] | 636 | vcpu->arch.shared->msr |= shadow_srr1 & 0x58000000; |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 637 | kvmppc_book3s_queue_irqprio(vcpu, exit_nr); |
| 638 | r = RESUME_GUEST; |
| 639 | } |
| 640 | break; |
Alexander Graf | 468a12c | 2011-12-09 14:44:13 +0100 | [diff] [blame] | 641 | } |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 642 | case BOOK3S_INTERRUPT_DATA_STORAGE: |
| 643 | { |
| 644 | ulong dar = kvmppc_get_fault_dar(vcpu); |
Alexander Graf | 468a12c | 2011-12-09 14:44:13 +0100 | [diff] [blame] | 645 | struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu); |
| 646 | u32 fault_dsisr = svcpu->fault_dsisr; |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 647 | vcpu->stat.pf_storage++; |
| 648 | |
| 649 | #ifdef CONFIG_PPC_BOOK3S_32 |
| 650 | /* We set segments as unused segments when invalidating them. So |
| 651 | * treat the respective fault as segment fault. */ |
Alexander Graf | 468a12c | 2011-12-09 14:44:13 +0100 | [diff] [blame] | 652 | if ((svcpu->sr[dar >> SID_SHIFT]) == SR_INVALID) { |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 653 | kvmppc_mmu_map_segment(vcpu, dar); |
| 654 | r = RESUME_GUEST; |
Alexander Graf | 468a12c | 2011-12-09 14:44:13 +0100 | [diff] [blame] | 655 | svcpu_put(svcpu); |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 656 | break; |
| 657 | } |
| 658 | #endif |
Alexander Graf | 468a12c | 2011-12-09 14:44:13 +0100 | [diff] [blame] | 659 | svcpu_put(svcpu); |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 660 | |
| 661 | /* The only case we need to handle is missing shadow PTEs */ |
Alexander Graf | 468a12c | 2011-12-09 14:44:13 +0100 | [diff] [blame] | 662 | if (fault_dsisr & DSISR_NOHPTE) { |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 663 | r = kvmppc_handle_pagefault(run, vcpu, dar, exit_nr); |
| 664 | } else { |
| 665 | vcpu->arch.shared->dar = dar; |
Alexander Graf | 468a12c | 2011-12-09 14:44:13 +0100 | [diff] [blame] | 666 | vcpu->arch.shared->dsisr = fault_dsisr; |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 667 | kvmppc_book3s_queue_irqprio(vcpu, exit_nr); |
| 668 | r = RESUME_GUEST; |
| 669 | } |
| 670 | break; |
| 671 | } |
| 672 | case BOOK3S_INTERRUPT_DATA_SEGMENT: |
| 673 | if (kvmppc_mmu_map_segment(vcpu, kvmppc_get_fault_dar(vcpu)) < 0) { |
| 674 | vcpu->arch.shared->dar = kvmppc_get_fault_dar(vcpu); |
| 675 | kvmppc_book3s_queue_irqprio(vcpu, |
| 676 | BOOK3S_INTERRUPT_DATA_SEGMENT); |
| 677 | } |
| 678 | r = RESUME_GUEST; |
| 679 | break; |
| 680 | case BOOK3S_INTERRUPT_INST_SEGMENT: |
| 681 | if (kvmppc_mmu_map_segment(vcpu, kvmppc_get_pc(vcpu)) < 0) { |
| 682 | kvmppc_book3s_queue_irqprio(vcpu, |
| 683 | BOOK3S_INTERRUPT_INST_SEGMENT); |
| 684 | } |
| 685 | r = RESUME_GUEST; |
| 686 | break; |
| 687 | /* We're good on these - the host merely wanted to get our attention */ |
| 688 | case BOOK3S_INTERRUPT_DECREMENTER: |
Alexander Graf | 4f225ae | 2012-03-13 23:05:16 +0100 | [diff] [blame] | 689 | case BOOK3S_INTERRUPT_HV_DECREMENTER: |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 690 | vcpu->stat.dec_exits++; |
| 691 | r = RESUME_GUEST; |
| 692 | break; |
| 693 | case BOOK3S_INTERRUPT_EXTERNAL: |
Alexander Graf | 4f225ae | 2012-03-13 23:05:16 +0100 | [diff] [blame] | 694 | case BOOK3S_INTERRUPT_EXTERNAL_LEVEL: |
| 695 | case BOOK3S_INTERRUPT_EXTERNAL_HV: |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 696 | vcpu->stat.ext_intr_exits++; |
| 697 | r = RESUME_GUEST; |
| 698 | break; |
| 699 | case BOOK3S_INTERRUPT_PERFMON: |
| 700 | r = RESUME_GUEST; |
| 701 | break; |
| 702 | case BOOK3S_INTERRUPT_PROGRAM: |
Alexander Graf | 4f225ae | 2012-03-13 23:05:16 +0100 | [diff] [blame] | 703 | case BOOK3S_INTERRUPT_H_EMUL_ASSIST: |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 704 | { |
| 705 | enum emulation_result er; |
Alexander Graf | 468a12c | 2011-12-09 14:44:13 +0100 | [diff] [blame] | 706 | struct kvmppc_book3s_shadow_vcpu *svcpu; |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 707 | ulong flags; |
| 708 | |
| 709 | program_interrupt: |
Alexander Graf | 468a12c | 2011-12-09 14:44:13 +0100 | [diff] [blame] | 710 | svcpu = svcpu_get(vcpu); |
| 711 | flags = svcpu->shadow_srr1 & 0x1f0000ull; |
| 712 | svcpu_put(svcpu); |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 713 | |
| 714 | if (vcpu->arch.shared->msr & MSR_PR) { |
| 715 | #ifdef EXIT_DEBUG |
| 716 | printk(KERN_INFO "Userspace triggered 0x700 exception at 0x%lx (0x%x)\n", kvmppc_get_pc(vcpu), kvmppc_get_last_inst(vcpu)); |
| 717 | #endif |
| 718 | if ((kvmppc_get_last_inst(vcpu) & 0xff0007ff) != |
| 719 | (INS_DCBZ & 0xfffffff7)) { |
| 720 | kvmppc_core_queue_program(vcpu, flags); |
| 721 | r = RESUME_GUEST; |
| 722 | break; |
| 723 | } |
| 724 | } |
| 725 | |
| 726 | vcpu->stat.emulated_inst_exits++; |
| 727 | er = kvmppc_emulate_instruction(run, vcpu); |
| 728 | switch (er) { |
| 729 | case EMULATE_DONE: |
| 730 | r = RESUME_GUEST_NV; |
| 731 | break; |
| 732 | case EMULATE_AGAIN: |
| 733 | r = RESUME_GUEST; |
| 734 | break; |
| 735 | case EMULATE_FAIL: |
| 736 | printk(KERN_CRIT "%s: emulation at %lx failed (%08x)\n", |
| 737 | __func__, kvmppc_get_pc(vcpu), kvmppc_get_last_inst(vcpu)); |
| 738 | kvmppc_core_queue_program(vcpu, flags); |
| 739 | r = RESUME_GUEST; |
| 740 | break; |
| 741 | case EMULATE_DO_MMIO: |
| 742 | run->exit_reason = KVM_EXIT_MMIO; |
| 743 | r = RESUME_HOST_NV; |
| 744 | break; |
| 745 | default: |
| 746 | BUG(); |
| 747 | } |
| 748 | break; |
| 749 | } |
| 750 | case BOOK3S_INTERRUPT_SYSCALL: |
Alexander Graf | a668f2b | 2011-08-08 17:26:24 +0200 | [diff] [blame] | 751 | if (vcpu->arch.papr_enabled && |
| 752 | (kvmppc_get_last_inst(vcpu) == 0x44000022) && |
| 753 | !(vcpu->arch.shared->msr & MSR_PR)) { |
| 754 | /* SC 1 papr hypercalls */ |
| 755 | ulong cmd = kvmppc_get_gpr(vcpu, 3); |
| 756 | int i; |
| 757 | |
Andreas Schwab | 96f38d7 | 2011-11-08 07:17:39 +0000 | [diff] [blame] | 758 | #ifdef CONFIG_KVM_BOOK3S_64_PR |
Alexander Graf | a668f2b | 2011-08-08 17:26:24 +0200 | [diff] [blame] | 759 | if (kvmppc_h_pr(vcpu, cmd) == EMULATE_DONE) { |
| 760 | r = RESUME_GUEST; |
| 761 | break; |
| 762 | } |
Andreas Schwab | 96f38d7 | 2011-11-08 07:17:39 +0000 | [diff] [blame] | 763 | #endif |
Alexander Graf | a668f2b | 2011-08-08 17:26:24 +0200 | [diff] [blame] | 764 | |
| 765 | run->papr_hcall.nr = cmd; |
| 766 | for (i = 0; i < 9; ++i) { |
| 767 | ulong gpr = kvmppc_get_gpr(vcpu, 4 + i); |
| 768 | run->papr_hcall.args[i] = gpr; |
| 769 | } |
| 770 | run->exit_reason = KVM_EXIT_PAPR_HCALL; |
| 771 | vcpu->arch.hcall_needed = 1; |
| 772 | r = RESUME_HOST; |
| 773 | } else if (vcpu->arch.osi_enabled && |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 774 | (((u32)kvmppc_get_gpr(vcpu, 3)) == OSI_SC_MAGIC_R3) && |
| 775 | (((u32)kvmppc_get_gpr(vcpu, 4)) == OSI_SC_MAGIC_R4)) { |
| 776 | /* MOL hypercalls */ |
| 777 | u64 *gprs = run->osi.gprs; |
| 778 | int i; |
| 779 | |
| 780 | run->exit_reason = KVM_EXIT_OSI; |
| 781 | for (i = 0; i < 32; i++) |
| 782 | gprs[i] = kvmppc_get_gpr(vcpu, i); |
| 783 | vcpu->arch.osi_needed = 1; |
| 784 | r = RESUME_HOST_NV; |
| 785 | } else if (!(vcpu->arch.shared->msr & MSR_PR) && |
| 786 | (((u32)kvmppc_get_gpr(vcpu, 0)) == KVM_SC_MAGIC_R0)) { |
| 787 | /* KVM PV hypercalls */ |
| 788 | kvmppc_set_gpr(vcpu, 3, kvmppc_kvm_pv(vcpu)); |
| 789 | r = RESUME_GUEST; |
| 790 | } else { |
| 791 | /* Guest syscalls */ |
| 792 | vcpu->stat.syscall_exits++; |
| 793 | kvmppc_book3s_queue_irqprio(vcpu, exit_nr); |
| 794 | r = RESUME_GUEST; |
| 795 | } |
| 796 | break; |
| 797 | case BOOK3S_INTERRUPT_FP_UNAVAIL: |
| 798 | case BOOK3S_INTERRUPT_ALTIVEC: |
| 799 | case BOOK3S_INTERRUPT_VSX: |
| 800 | { |
| 801 | int ext_msr = 0; |
| 802 | |
| 803 | switch (exit_nr) { |
| 804 | case BOOK3S_INTERRUPT_FP_UNAVAIL: ext_msr = MSR_FP; break; |
| 805 | case BOOK3S_INTERRUPT_ALTIVEC: ext_msr = MSR_VEC; break; |
| 806 | case BOOK3S_INTERRUPT_VSX: ext_msr = MSR_VSX; break; |
| 807 | } |
| 808 | |
| 809 | switch (kvmppc_check_ext(vcpu, exit_nr)) { |
| 810 | case EMULATE_DONE: |
| 811 | /* everything ok - let's enable the ext */ |
| 812 | r = kvmppc_handle_ext(vcpu, exit_nr, ext_msr); |
| 813 | break; |
| 814 | case EMULATE_FAIL: |
| 815 | /* we need to emulate this instruction */ |
| 816 | goto program_interrupt; |
| 817 | break; |
| 818 | default: |
| 819 | /* nothing to worry about - go again */ |
| 820 | break; |
| 821 | } |
| 822 | break; |
| 823 | } |
| 824 | case BOOK3S_INTERRUPT_ALIGNMENT: |
| 825 | if (kvmppc_read_inst(vcpu) == EMULATE_DONE) { |
| 826 | vcpu->arch.shared->dsisr = kvmppc_alignment_dsisr(vcpu, |
| 827 | kvmppc_get_last_inst(vcpu)); |
| 828 | vcpu->arch.shared->dar = kvmppc_alignment_dar(vcpu, |
| 829 | kvmppc_get_last_inst(vcpu)); |
| 830 | kvmppc_book3s_queue_irqprio(vcpu, exit_nr); |
| 831 | } |
| 832 | r = RESUME_GUEST; |
| 833 | break; |
| 834 | case BOOK3S_INTERRUPT_MACHINE_CHECK: |
| 835 | case BOOK3S_INTERRUPT_TRACE: |
| 836 | kvmppc_book3s_queue_irqprio(vcpu, exit_nr); |
| 837 | r = RESUME_GUEST; |
| 838 | break; |
| 839 | default: |
Alexander Graf | 468a12c | 2011-12-09 14:44:13 +0100 | [diff] [blame] | 840 | { |
| 841 | struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu); |
| 842 | ulong shadow_srr1 = svcpu->shadow_srr1; |
| 843 | svcpu_put(svcpu); |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 844 | /* Ugh - bork here! What did we get? */ |
| 845 | printk(KERN_EMERG "exit_nr=0x%x | pc=0x%lx | msr=0x%lx\n", |
Alexander Graf | 468a12c | 2011-12-09 14:44:13 +0100 | [diff] [blame] | 846 | exit_nr, kvmppc_get_pc(vcpu), shadow_srr1); |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 847 | r = RESUME_HOST; |
| 848 | BUG(); |
| 849 | break; |
| 850 | } |
Alexander Graf | 468a12c | 2011-12-09 14:44:13 +0100 | [diff] [blame] | 851 | } |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 852 | |
| 853 | if (!(r & RESUME_HOST)) { |
| 854 | /* To avoid clobbering exit_reason, only check for signals if |
| 855 | * we aren't already exiting to userspace for some other |
| 856 | * reason. */ |
Alexander Graf | e371f71 | 2011-12-19 13:36:55 +0100 | [diff] [blame] | 857 | |
| 858 | /* |
| 859 | * Interrupts could be timers for the guest which we have to |
| 860 | * inject again, so let's postpone them until we're in the guest |
| 861 | * and if we really did time things so badly, then we just exit |
| 862 | * again due to a host external interrupt. |
| 863 | */ |
Alexander Graf | bd2be68 | 2012-08-13 01:04:19 +0200 | [diff] [blame^] | 864 | local_irq_disable(); |
Alexander Graf | 03d25c5 | 2012-08-10 12:28:50 +0200 | [diff] [blame] | 865 | if (kvmppc_prepare_to_enter(vcpu)) { |
Alexander Graf | bd2be68 | 2012-08-13 01:04:19 +0200 | [diff] [blame^] | 866 | local_irq_enable(); |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 867 | run->exit_reason = KVM_EXIT_INTR; |
| 868 | r = -EINTR; |
Alexander Graf | 24afa37 | 2012-08-12 12:42:30 +0200 | [diff] [blame] | 869 | } else { |
| 870 | /* Going back to guest */ |
| 871 | kvm_guest_enter(); |
Alexander Graf | bd2be68 | 2012-08-13 01:04:19 +0200 | [diff] [blame^] | 872 | kvmppc_lazy_ee_enable(); |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 873 | } |
| 874 | } |
| 875 | |
| 876 | trace_kvm_book3s_reenter(r, vcpu); |
| 877 | |
| 878 | return r; |
| 879 | } |
| 880 | |
| 881 | int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu, |
| 882 | struct kvm_sregs *sregs) |
| 883 | { |
| 884 | struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu); |
| 885 | int i; |
| 886 | |
| 887 | sregs->pvr = vcpu->arch.pvr; |
| 888 | |
| 889 | sregs->u.s.sdr1 = to_book3s(vcpu)->sdr1; |
| 890 | if (vcpu->arch.hflags & BOOK3S_HFLAG_SLB) { |
| 891 | for (i = 0; i < 64; i++) { |
| 892 | sregs->u.s.ppc64.slb[i].slbe = vcpu->arch.slb[i].orige | i; |
| 893 | sregs->u.s.ppc64.slb[i].slbv = vcpu->arch.slb[i].origv; |
| 894 | } |
| 895 | } else { |
| 896 | for (i = 0; i < 16; i++) |
| 897 | sregs->u.s.ppc32.sr[i] = vcpu->arch.shared->sr[i]; |
| 898 | |
| 899 | for (i = 0; i < 8; i++) { |
| 900 | sregs->u.s.ppc32.ibat[i] = vcpu3s->ibat[i].raw; |
| 901 | sregs->u.s.ppc32.dbat[i] = vcpu3s->dbat[i].raw; |
| 902 | } |
| 903 | } |
| 904 | |
| 905 | return 0; |
| 906 | } |
| 907 | |
| 908 | int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu, |
| 909 | struct kvm_sregs *sregs) |
| 910 | { |
| 911 | struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu); |
| 912 | int i; |
| 913 | |
| 914 | kvmppc_set_pvr(vcpu, sregs->pvr); |
| 915 | |
| 916 | vcpu3s->sdr1 = sregs->u.s.sdr1; |
| 917 | if (vcpu->arch.hflags & BOOK3S_HFLAG_SLB) { |
| 918 | for (i = 0; i < 64; i++) { |
| 919 | vcpu->arch.mmu.slbmte(vcpu, sregs->u.s.ppc64.slb[i].slbv, |
| 920 | sregs->u.s.ppc64.slb[i].slbe); |
| 921 | } |
| 922 | } else { |
| 923 | for (i = 0; i < 16; i++) { |
| 924 | vcpu->arch.mmu.mtsrin(vcpu, i, sregs->u.s.ppc32.sr[i]); |
| 925 | } |
| 926 | for (i = 0; i < 8; i++) { |
| 927 | kvmppc_set_bat(vcpu, &(vcpu3s->ibat[i]), false, |
| 928 | (u32)sregs->u.s.ppc32.ibat[i]); |
| 929 | kvmppc_set_bat(vcpu, &(vcpu3s->ibat[i]), true, |
| 930 | (u32)(sregs->u.s.ppc32.ibat[i] >> 32)); |
| 931 | kvmppc_set_bat(vcpu, &(vcpu3s->dbat[i]), false, |
| 932 | (u32)sregs->u.s.ppc32.dbat[i]); |
| 933 | kvmppc_set_bat(vcpu, &(vcpu3s->dbat[i]), true, |
| 934 | (u32)(sregs->u.s.ppc32.dbat[i] >> 32)); |
| 935 | } |
| 936 | } |
| 937 | |
| 938 | /* Flush the MMU after messing with the segments */ |
| 939 | kvmppc_mmu_pte_flush(vcpu, 0, 0); |
| 940 | |
| 941 | return 0; |
| 942 | } |
| 943 | |
Paul Mackerras | 31f3438 | 2011-12-12 12:26:50 +0000 | [diff] [blame] | 944 | int kvm_vcpu_ioctl_get_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg) |
| 945 | { |
| 946 | int r = -EINVAL; |
| 947 | |
| 948 | switch (reg->id) { |
| 949 | case KVM_REG_PPC_HIOR: |
Alexander Graf | b8e6f8a | 2012-03-13 19:59:39 +0100 | [diff] [blame] | 950 | r = copy_to_user((u64 __user *)(long)reg->addr, |
| 951 | &to_book3s(vcpu)->hior, sizeof(u64)); |
Paul Mackerras | 31f3438 | 2011-12-12 12:26:50 +0000 | [diff] [blame] | 952 | break; |
| 953 | default: |
| 954 | break; |
| 955 | } |
| 956 | |
| 957 | return r; |
| 958 | } |
| 959 | |
| 960 | int kvm_vcpu_ioctl_set_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg) |
| 961 | { |
| 962 | int r = -EINVAL; |
| 963 | |
| 964 | switch (reg->id) { |
| 965 | case KVM_REG_PPC_HIOR: |
Alexander Graf | b8e6f8a | 2012-03-13 19:59:39 +0100 | [diff] [blame] | 966 | r = copy_from_user(&to_book3s(vcpu)->hior, |
| 967 | (u64 __user *)(long)reg->addr, sizeof(u64)); |
Paul Mackerras | 31f3438 | 2011-12-12 12:26:50 +0000 | [diff] [blame] | 968 | if (!r) |
| 969 | to_book3s(vcpu)->hior_explicit = true; |
| 970 | break; |
| 971 | default: |
| 972 | break; |
| 973 | } |
| 974 | |
| 975 | return r; |
| 976 | } |
| 977 | |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 978 | int kvmppc_core_check_processor_compat(void) |
| 979 | { |
| 980 | return 0; |
| 981 | } |
| 982 | |
| 983 | struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm, unsigned int id) |
| 984 | { |
| 985 | struct kvmppc_vcpu_book3s *vcpu_book3s; |
| 986 | struct kvm_vcpu *vcpu; |
| 987 | int err = -ENOMEM; |
| 988 | unsigned long p; |
| 989 | |
| 990 | vcpu_book3s = vzalloc(sizeof(struct kvmppc_vcpu_book3s)); |
| 991 | if (!vcpu_book3s) |
| 992 | goto out; |
| 993 | |
| 994 | vcpu_book3s->shadow_vcpu = (struct kvmppc_book3s_shadow_vcpu *) |
| 995 | kzalloc(sizeof(*vcpu_book3s->shadow_vcpu), GFP_KERNEL); |
| 996 | if (!vcpu_book3s->shadow_vcpu) |
| 997 | goto free_vcpu; |
| 998 | |
| 999 | vcpu = &vcpu_book3s->vcpu; |
| 1000 | err = kvm_vcpu_init(vcpu, kvm, id); |
| 1001 | if (err) |
| 1002 | goto free_shadow_vcpu; |
| 1003 | |
| 1004 | p = __get_free_page(GFP_KERNEL|__GFP_ZERO); |
| 1005 | /* the real shared page fills the last 4k of our page */ |
| 1006 | vcpu->arch.shared = (void*)(p + PAGE_SIZE - 4096); |
| 1007 | if (!p) |
| 1008 | goto uninit_vcpu; |
| 1009 | |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 1010 | #ifdef CONFIG_PPC_BOOK3S_64 |
| 1011 | /* default to book3s_64 (970fx) */ |
| 1012 | vcpu->arch.pvr = 0x3C0301; |
| 1013 | #else |
| 1014 | /* default to book3s_32 (750) */ |
| 1015 | vcpu->arch.pvr = 0x84202; |
| 1016 | #endif |
| 1017 | kvmppc_set_pvr(vcpu, vcpu->arch.pvr); |
| 1018 | vcpu->arch.slb_nr = 64; |
| 1019 | |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 1020 | vcpu->arch.shadow_msr = MSR_USER64; |
| 1021 | |
| 1022 | err = kvmppc_mmu_init(vcpu); |
| 1023 | if (err < 0) |
| 1024 | goto uninit_vcpu; |
| 1025 | |
| 1026 | return vcpu; |
| 1027 | |
| 1028 | uninit_vcpu: |
| 1029 | kvm_vcpu_uninit(vcpu); |
| 1030 | free_shadow_vcpu: |
| 1031 | kfree(vcpu_book3s->shadow_vcpu); |
| 1032 | free_vcpu: |
| 1033 | vfree(vcpu_book3s); |
| 1034 | out: |
| 1035 | return ERR_PTR(err); |
| 1036 | } |
| 1037 | |
| 1038 | void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu) |
| 1039 | { |
| 1040 | struct kvmppc_vcpu_book3s *vcpu_book3s = to_book3s(vcpu); |
| 1041 | |
| 1042 | free_page((unsigned long)vcpu->arch.shared & PAGE_MASK); |
| 1043 | kvm_vcpu_uninit(vcpu); |
| 1044 | kfree(vcpu_book3s->shadow_vcpu); |
| 1045 | vfree(vcpu_book3s); |
| 1046 | } |
| 1047 | |
Paul Mackerras | df6909e5 | 2011-06-29 00:19:50 +0000 | [diff] [blame] | 1048 | int kvmppc_vcpu_run(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu) |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 1049 | { |
| 1050 | int ret; |
| 1051 | double fpr[32][TS_FPRWIDTH]; |
| 1052 | unsigned int fpscr; |
| 1053 | int fpexc_mode; |
| 1054 | #ifdef CONFIG_ALTIVEC |
| 1055 | vector128 vr[32]; |
| 1056 | vector128 vscr; |
| 1057 | unsigned long uninitialized_var(vrsave); |
| 1058 | int used_vr; |
| 1059 | #endif |
| 1060 | #ifdef CONFIG_VSX |
| 1061 | int used_vsr; |
| 1062 | #endif |
| 1063 | ulong ext_msr; |
| 1064 | |
Alexander Graf | af8f38b | 2011-08-10 13:57:08 +0200 | [diff] [blame] | 1065 | /* Check if we can run the vcpu at all */ |
| 1066 | if (!vcpu->arch.sane) { |
| 1067 | kvm_run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
Alexander Graf | 7d82714 | 2011-12-09 15:46:21 +0100 | [diff] [blame] | 1068 | ret = -EINVAL; |
| 1069 | goto out; |
Alexander Graf | af8f38b | 2011-08-10 13:57:08 +0200 | [diff] [blame] | 1070 | } |
| 1071 | |
Alexander Graf | e371f71 | 2011-12-19 13:36:55 +0100 | [diff] [blame] | 1072 | /* |
| 1073 | * Interrupts could be timers for the guest which we have to inject |
| 1074 | * again, so let's postpone them until we're in the guest and if we |
| 1075 | * really did time things so badly, then we just exit again due to |
| 1076 | * a host external interrupt. |
| 1077 | */ |
Alexander Graf | bd2be68 | 2012-08-13 01:04:19 +0200 | [diff] [blame^] | 1078 | local_irq_disable(); |
Alexander Graf | 03d25c5 | 2012-08-10 12:28:50 +0200 | [diff] [blame] | 1079 | if (kvmppc_prepare_to_enter(vcpu)) { |
Alexander Graf | bd2be68 | 2012-08-13 01:04:19 +0200 | [diff] [blame^] | 1080 | local_irq_enable(); |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 1081 | kvm_run->exit_reason = KVM_EXIT_INTR; |
Alexander Graf | 7d82714 | 2011-12-09 15:46:21 +0100 | [diff] [blame] | 1082 | ret = -EINTR; |
| 1083 | goto out; |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 1084 | } |
| 1085 | |
| 1086 | /* Save FPU state in stack */ |
| 1087 | if (current->thread.regs->msr & MSR_FP) |
| 1088 | giveup_fpu(current); |
| 1089 | memcpy(fpr, current->thread.fpr, sizeof(current->thread.fpr)); |
| 1090 | fpscr = current->thread.fpscr.val; |
| 1091 | fpexc_mode = current->thread.fpexc_mode; |
| 1092 | |
| 1093 | #ifdef CONFIG_ALTIVEC |
| 1094 | /* Save Altivec state in stack */ |
| 1095 | used_vr = current->thread.used_vr; |
| 1096 | if (used_vr) { |
| 1097 | if (current->thread.regs->msr & MSR_VEC) |
| 1098 | giveup_altivec(current); |
| 1099 | memcpy(vr, current->thread.vr, sizeof(current->thread.vr)); |
| 1100 | vscr = current->thread.vscr; |
| 1101 | vrsave = current->thread.vrsave; |
| 1102 | } |
| 1103 | #endif |
| 1104 | |
| 1105 | #ifdef CONFIG_VSX |
| 1106 | /* Save VSX state in stack */ |
| 1107 | used_vsr = current->thread.used_vsr; |
| 1108 | if (used_vsr && (current->thread.regs->msr & MSR_VSX)) |
| 1109 | __giveup_vsx(current); |
| 1110 | #endif |
| 1111 | |
| 1112 | /* Remember the MSR with disabled extensions */ |
| 1113 | ext_msr = current->thread.regs->msr; |
| 1114 | |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 1115 | /* Preload FPU if it's enabled */ |
| 1116 | if (vcpu->arch.shared->msr & MSR_FP) |
| 1117 | kvmppc_handle_ext(vcpu, BOOK3S_INTERRUPT_FP_UNAVAIL, MSR_FP); |
| 1118 | |
Alexander Graf | bd2be68 | 2012-08-13 01:04:19 +0200 | [diff] [blame^] | 1119 | kvmppc_lazy_ee_enable(); |
Paul Mackerras | df6909e5 | 2011-06-29 00:19:50 +0000 | [diff] [blame] | 1120 | |
| 1121 | ret = __kvmppc_vcpu_run(kvm_run, vcpu); |
| 1122 | |
Alexander Graf | 24afa37 | 2012-08-12 12:42:30 +0200 | [diff] [blame] | 1123 | /* No need for kvm_guest_exit. It's done in handle_exit. |
| 1124 | We also get here with interrupts enabled. */ |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 1125 | |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 1126 | current->thread.regs->msr = ext_msr; |
| 1127 | |
| 1128 | /* Make sure we save the guest FPU/Altivec/VSX state */ |
| 1129 | kvmppc_giveup_ext(vcpu, MSR_FP); |
| 1130 | kvmppc_giveup_ext(vcpu, MSR_VEC); |
| 1131 | kvmppc_giveup_ext(vcpu, MSR_VSX); |
| 1132 | |
| 1133 | /* Restore FPU state from stack */ |
| 1134 | memcpy(current->thread.fpr, fpr, sizeof(current->thread.fpr)); |
| 1135 | current->thread.fpscr.val = fpscr; |
| 1136 | current->thread.fpexc_mode = fpexc_mode; |
| 1137 | |
| 1138 | #ifdef CONFIG_ALTIVEC |
| 1139 | /* Restore Altivec state from stack */ |
| 1140 | if (used_vr && current->thread.used_vr) { |
| 1141 | memcpy(current->thread.vr, vr, sizeof(current->thread.vr)); |
| 1142 | current->thread.vscr = vscr; |
| 1143 | current->thread.vrsave = vrsave; |
| 1144 | } |
| 1145 | current->thread.used_vr = used_vr; |
| 1146 | #endif |
| 1147 | |
| 1148 | #ifdef CONFIG_VSX |
| 1149 | current->thread.used_vsr = used_vsr; |
| 1150 | #endif |
| 1151 | |
Alexander Graf | 7d82714 | 2011-12-09 15:46:21 +0100 | [diff] [blame] | 1152 | out: |
Alexander Graf | 0652eaa | 2012-08-12 11:34:21 +0200 | [diff] [blame] | 1153 | vcpu->mode = OUTSIDE_GUEST_MODE; |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 1154 | return ret; |
| 1155 | } |
| 1156 | |
Paul Mackerras | 82ed361 | 2011-12-15 02:03:22 +0000 | [diff] [blame] | 1157 | /* |
| 1158 | * Get (and clear) the dirty memory log for a memory slot. |
| 1159 | */ |
| 1160 | int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, |
| 1161 | struct kvm_dirty_log *log) |
| 1162 | { |
| 1163 | struct kvm_memory_slot *memslot; |
| 1164 | struct kvm_vcpu *vcpu; |
| 1165 | ulong ga, ga_end; |
| 1166 | int is_dirty = 0; |
| 1167 | int r; |
| 1168 | unsigned long n; |
| 1169 | |
| 1170 | mutex_lock(&kvm->slots_lock); |
| 1171 | |
| 1172 | r = kvm_get_dirty_log(kvm, log, &is_dirty); |
| 1173 | if (r) |
| 1174 | goto out; |
| 1175 | |
| 1176 | /* If nothing is dirty, don't bother messing with page tables. */ |
| 1177 | if (is_dirty) { |
| 1178 | memslot = id_to_memslot(kvm->memslots, log->slot); |
| 1179 | |
| 1180 | ga = memslot->base_gfn << PAGE_SHIFT; |
| 1181 | ga_end = ga + (memslot->npages << PAGE_SHIFT); |
| 1182 | |
| 1183 | kvm_for_each_vcpu(n, vcpu, kvm) |
| 1184 | kvmppc_mmu_pte_pflush(vcpu, ga, ga_end); |
| 1185 | |
| 1186 | n = kvm_dirty_bitmap_bytes(memslot); |
| 1187 | memset(memslot->dirty_bitmap, 0, n); |
| 1188 | } |
| 1189 | |
| 1190 | r = 0; |
| 1191 | out: |
| 1192 | mutex_unlock(&kvm->slots_lock); |
| 1193 | return r; |
| 1194 | } |
| 1195 | |
Benjamin Herrenschmidt | 5b74716 | 2012-04-26 19:43:42 +0000 | [diff] [blame] | 1196 | #ifdef CONFIG_PPC64 |
| 1197 | int kvm_vm_ioctl_get_smmu_info(struct kvm *kvm, struct kvm_ppc_smmu_info *info) |
| 1198 | { |
| 1199 | /* No flags */ |
| 1200 | info->flags = 0; |
| 1201 | |
| 1202 | /* SLB is always 64 entries */ |
| 1203 | info->slb_size = 64; |
| 1204 | |
| 1205 | /* Standard 4k base page size segment */ |
| 1206 | info->sps[0].page_shift = 12; |
| 1207 | info->sps[0].slb_enc = 0; |
| 1208 | info->sps[0].enc[0].page_shift = 12; |
| 1209 | info->sps[0].enc[0].pte_enc = 0; |
| 1210 | |
| 1211 | /* Standard 16M large page size segment */ |
| 1212 | info->sps[1].page_shift = 24; |
| 1213 | info->sps[1].slb_enc = SLB_VSID_L; |
| 1214 | info->sps[1].enc[0].page_shift = 24; |
| 1215 | info->sps[1].enc[0].pte_enc = 0; |
| 1216 | |
| 1217 | return 0; |
| 1218 | } |
| 1219 | #endif /* CONFIG_PPC64 */ |
| 1220 | |
Paul Mackerras | f9e0554 | 2011-06-29 00:19:22 +0000 | [diff] [blame] | 1221 | int kvmppc_core_prepare_memory_region(struct kvm *kvm, |
| 1222 | struct kvm_userspace_memory_region *mem) |
| 1223 | { |
| 1224 | return 0; |
| 1225 | } |
| 1226 | |
| 1227 | void kvmppc_core_commit_memory_region(struct kvm *kvm, |
| 1228 | struct kvm_userspace_memory_region *mem) |
| 1229 | { |
| 1230 | } |
| 1231 | |
| 1232 | int kvmppc_core_init_vm(struct kvm *kvm) |
| 1233 | { |
Benjamin Herrenschmidt | f31e65e | 2012-03-15 21:58:34 +0000 | [diff] [blame] | 1234 | #ifdef CONFIG_PPC64 |
| 1235 | INIT_LIST_HEAD(&kvm->arch.spapr_tce_tables); |
| 1236 | #endif |
| 1237 | |
Paul Mackerras | f9e0554 | 2011-06-29 00:19:22 +0000 | [diff] [blame] | 1238 | return 0; |
| 1239 | } |
| 1240 | |
| 1241 | void kvmppc_core_destroy_vm(struct kvm *kvm) |
| 1242 | { |
Benjamin Herrenschmidt | f31e65e | 2012-03-15 21:58:34 +0000 | [diff] [blame] | 1243 | #ifdef CONFIG_PPC64 |
| 1244 | WARN_ON(!list_empty(&kvm->arch.spapr_tce_tables)); |
| 1245 | #endif |
Paul Mackerras | f9e0554 | 2011-06-29 00:19:22 +0000 | [diff] [blame] | 1246 | } |
| 1247 | |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 1248 | static int kvmppc_book3s_init(void) |
| 1249 | { |
| 1250 | int r; |
| 1251 | |
| 1252 | r = kvm_init(NULL, sizeof(struct kvmppc_vcpu_book3s), 0, |
| 1253 | THIS_MODULE); |
| 1254 | |
| 1255 | if (r) |
| 1256 | return r; |
| 1257 | |
| 1258 | r = kvmppc_mmu_hpte_sysinit(); |
| 1259 | |
| 1260 | return r; |
| 1261 | } |
| 1262 | |
| 1263 | static void kvmppc_book3s_exit(void) |
| 1264 | { |
| 1265 | kvmppc_mmu_hpte_sysexit(); |
| 1266 | kvm_exit(); |
| 1267 | } |
| 1268 | |
| 1269 | module_init(kvmppc_book3s_init); |
| 1270 | module_exit(kvmppc_book3s_exit); |