1bbf45ba5SHollis Blanchard /* 2bbf45ba5SHollis Blanchard * This program is free software; you can redistribute it and/or modify 3bbf45ba5SHollis Blanchard * it under the terms of the GNU General Public License, version 2, as 4bbf45ba5SHollis Blanchard * published by the Free Software Foundation. 5bbf45ba5SHollis Blanchard * 6bbf45ba5SHollis Blanchard * This program is distributed in the hope that it will be useful, 7bbf45ba5SHollis Blanchard * but WITHOUT ANY WARRANTY; without even the implied warranty of 8bbf45ba5SHollis Blanchard * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 9bbf45ba5SHollis Blanchard * GNU General Public License for more details. 10bbf45ba5SHollis Blanchard * 11bbf45ba5SHollis Blanchard * You should have received a copy of the GNU General Public License 12bbf45ba5SHollis Blanchard * along with this program; if not, write to the Free Software 13bbf45ba5SHollis Blanchard * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 14bbf45ba5SHollis Blanchard * 15bbf45ba5SHollis Blanchard * Copyright IBM Corp. 2007 16bbf45ba5SHollis Blanchard * 17bbf45ba5SHollis Blanchard * Authors: Hollis Blanchard <hollisb@us.ibm.com> 18bbf45ba5SHollis Blanchard * Christian Ehrhardt <ehrhardt@linux.vnet.ibm.com> 19bbf45ba5SHollis Blanchard */ 20bbf45ba5SHollis Blanchard 21bbf45ba5SHollis Blanchard #include <linux/errno.h> 22bbf45ba5SHollis Blanchard #include <linux/err.h> 23bbf45ba5SHollis Blanchard #include <linux/kvm_host.h> 24bbf45ba5SHollis Blanchard #include <linux/module.h> 25bbf45ba5SHollis Blanchard #include <linux/vmalloc.h> 26544c6761SAlexander Graf #include <linux/hrtimer.h> 27bbf45ba5SHollis Blanchard #include <linux/fs.h> 285a0e3ad6STejun Heo #include <linux/slab.h> 29bbf45ba5SHollis Blanchard #include <asm/cputable.h> 30bbf45ba5SHollis Blanchard #include <asm/uaccess.h> 31bbf45ba5SHollis Blanchard #include <asm/kvm_ppc.h> 3283aae4a8SHollis Blanchard #include <asm/tlbflush.h> 3373e75b41SHollis Blanchard #include "timing.h" 34fad7b9b5SPaul Mackerras #include "../mm/mmu_decl.h" 35bbf45ba5SHollis Blanchard 3646f43c6eSMarcelo Tosatti #define CREATE_TRACE_POINTS 3746f43c6eSMarcelo Tosatti #include "trace.h" 3846f43c6eSMarcelo Tosatti 39bbf45ba5SHollis Blanchard gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn) 40bbf45ba5SHollis Blanchard { 41bbf45ba5SHollis Blanchard return gfn; 42bbf45ba5SHollis Blanchard } 43bbf45ba5SHollis Blanchard 44bbf45ba5SHollis Blanchard int kvm_arch_vcpu_runnable(struct kvm_vcpu *v) 45bbf45ba5SHollis Blanchard { 46a1b37100SGleb Natapov return !(v->arch.msr & MSR_WE) || !!(v->arch.pending_exceptions); 47bbf45ba5SHollis Blanchard } 48bbf45ba5SHollis Blanchard 49bbf45ba5SHollis Blanchard 50bbf45ba5SHollis Blanchard int kvmppc_emulate_mmio(struct kvm_run *run, struct kvm_vcpu *vcpu) 51bbf45ba5SHollis Blanchard { 52bbf45ba5SHollis Blanchard enum emulation_result er; 53bbf45ba5SHollis Blanchard int r; 54bbf45ba5SHollis Blanchard 55bbf45ba5SHollis Blanchard er = kvmppc_emulate_instruction(run, vcpu); 56bbf45ba5SHollis Blanchard switch (er) { 57bbf45ba5SHollis Blanchard case EMULATE_DONE: 58bbf45ba5SHollis Blanchard /* Future optimization: only reload non-volatiles if they were 59bbf45ba5SHollis Blanchard * actually modified. */ 60bbf45ba5SHollis Blanchard r = RESUME_GUEST_NV; 61bbf45ba5SHollis Blanchard break; 62bbf45ba5SHollis Blanchard case EMULATE_DO_MMIO: 63bbf45ba5SHollis Blanchard run->exit_reason = KVM_EXIT_MMIO; 64bbf45ba5SHollis Blanchard /* We must reload nonvolatiles because "update" load/store 65bbf45ba5SHollis Blanchard * instructions modify register state. */ 66bbf45ba5SHollis Blanchard /* Future optimization: only reload non-volatiles if they were 67bbf45ba5SHollis Blanchard * actually modified. */ 68bbf45ba5SHollis Blanchard r = RESUME_HOST_NV; 69bbf45ba5SHollis Blanchard break; 70bbf45ba5SHollis Blanchard case EMULATE_FAIL: 71bbf45ba5SHollis Blanchard /* XXX Deliver Program interrupt to guest. */ 72bbf45ba5SHollis Blanchard printk(KERN_EMERG "%s: emulation failed (%08x)\n", __func__, 73c7f38f46SAlexander Graf kvmppc_get_last_inst(vcpu)); 74bbf45ba5SHollis Blanchard r = RESUME_HOST; 75bbf45ba5SHollis Blanchard break; 76bbf45ba5SHollis Blanchard default: 77bbf45ba5SHollis Blanchard BUG(); 78bbf45ba5SHollis Blanchard } 79bbf45ba5SHollis Blanchard 80bbf45ba5SHollis Blanchard return r; 81bbf45ba5SHollis Blanchard } 82bbf45ba5SHollis Blanchard 8310474ae8SAlexander Graf int kvm_arch_hardware_enable(void *garbage) 84bbf45ba5SHollis Blanchard { 8510474ae8SAlexander Graf return 0; 86bbf45ba5SHollis Blanchard } 87bbf45ba5SHollis Blanchard 88bbf45ba5SHollis Blanchard void kvm_arch_hardware_disable(void *garbage) 89bbf45ba5SHollis Blanchard { 90bbf45ba5SHollis Blanchard } 91bbf45ba5SHollis Blanchard 92bbf45ba5SHollis Blanchard int kvm_arch_hardware_setup(void) 93bbf45ba5SHollis Blanchard { 94bbf45ba5SHollis Blanchard return 0; 95bbf45ba5SHollis Blanchard } 96bbf45ba5SHollis Blanchard 97bbf45ba5SHollis Blanchard void kvm_arch_hardware_unsetup(void) 98bbf45ba5SHollis Blanchard { 99bbf45ba5SHollis Blanchard } 100bbf45ba5SHollis Blanchard 101bbf45ba5SHollis Blanchard void kvm_arch_check_processor_compat(void *rtn) 102bbf45ba5SHollis Blanchard { 1039dd921cfSHollis Blanchard *(int *)rtn = kvmppc_core_check_processor_compat(); 104bbf45ba5SHollis Blanchard } 105bbf45ba5SHollis Blanchard 106bbf45ba5SHollis Blanchard struct kvm *kvm_arch_create_vm(void) 107bbf45ba5SHollis Blanchard { 108bbf45ba5SHollis Blanchard struct kvm *kvm; 109bbf45ba5SHollis Blanchard 110bbf45ba5SHollis Blanchard kvm = kzalloc(sizeof(struct kvm), GFP_KERNEL); 111bbf45ba5SHollis Blanchard if (!kvm) 112bbf45ba5SHollis Blanchard return ERR_PTR(-ENOMEM); 113bbf45ba5SHollis Blanchard 114bbf45ba5SHollis Blanchard return kvm; 115bbf45ba5SHollis Blanchard } 116bbf45ba5SHollis Blanchard 117bbf45ba5SHollis Blanchard static void kvmppc_free_vcpus(struct kvm *kvm) 118bbf45ba5SHollis Blanchard { 119bbf45ba5SHollis Blanchard unsigned int i; 120988a2caeSGleb Natapov struct kvm_vcpu *vcpu; 121bbf45ba5SHollis Blanchard 122988a2caeSGleb Natapov kvm_for_each_vcpu(i, vcpu, kvm) 123988a2caeSGleb Natapov kvm_arch_vcpu_free(vcpu); 124988a2caeSGleb Natapov 125988a2caeSGleb Natapov mutex_lock(&kvm->lock); 126988a2caeSGleb Natapov for (i = 0; i < atomic_read(&kvm->online_vcpus); i++) 127bbf45ba5SHollis Blanchard kvm->vcpus[i] = NULL; 128988a2caeSGleb Natapov 129988a2caeSGleb Natapov atomic_set(&kvm->online_vcpus, 0); 130988a2caeSGleb Natapov mutex_unlock(&kvm->lock); 131bbf45ba5SHollis Blanchard } 132bbf45ba5SHollis Blanchard 133ad8ba2cdSSheng Yang void kvm_arch_sync_events(struct kvm *kvm) 134ad8ba2cdSSheng Yang { 135ad8ba2cdSSheng Yang } 136ad8ba2cdSSheng Yang 137bbf45ba5SHollis Blanchard void kvm_arch_destroy_vm(struct kvm *kvm) 138bbf45ba5SHollis Blanchard { 139bbf45ba5SHollis Blanchard kvmppc_free_vcpus(kvm); 140bbf45ba5SHollis Blanchard kvm_free_physmem(kvm); 14164749204SMarcelo Tosatti cleanup_srcu_struct(&kvm->srcu); 142bbf45ba5SHollis Blanchard kfree(kvm); 143bbf45ba5SHollis Blanchard } 144bbf45ba5SHollis Blanchard 145bbf45ba5SHollis Blanchard int kvm_dev_ioctl_check_extension(long ext) 146bbf45ba5SHollis Blanchard { 147bbf45ba5SHollis Blanchard int r; 148bbf45ba5SHollis Blanchard 149bbf45ba5SHollis Blanchard switch (ext) { 150e15a1137SAlexander Graf case KVM_CAP_PPC_SEGSTATE: 151c10207feSAlexander Graf case KVM_CAP_PPC_PAIRED_SINGLES: 15218978768SAlexander Graf case KVM_CAP_PPC_UNSET_IRQ: 15371fbfd5fSAlexander Graf case KVM_CAP_ENABLE_CAP: 154ad0a048bSAlexander Graf case KVM_CAP_PPC_OSI: 155e15a1137SAlexander Graf r = 1; 156e15a1137SAlexander Graf break; 157588968b6SLaurent Vivier case KVM_CAP_COALESCED_MMIO: 158588968b6SLaurent Vivier r = KVM_COALESCED_MMIO_PAGE_OFFSET; 159588968b6SLaurent Vivier break; 160bbf45ba5SHollis Blanchard default: 161bbf45ba5SHollis Blanchard r = 0; 162bbf45ba5SHollis Blanchard break; 163bbf45ba5SHollis Blanchard } 164bbf45ba5SHollis Blanchard return r; 165bbf45ba5SHollis Blanchard 166bbf45ba5SHollis Blanchard } 167bbf45ba5SHollis Blanchard 168bbf45ba5SHollis Blanchard long kvm_arch_dev_ioctl(struct file *filp, 169bbf45ba5SHollis Blanchard unsigned int ioctl, unsigned long arg) 170bbf45ba5SHollis Blanchard { 171bbf45ba5SHollis Blanchard return -EINVAL; 172bbf45ba5SHollis Blanchard } 173bbf45ba5SHollis Blanchard 174f7784b8eSMarcelo Tosatti int kvm_arch_prepare_memory_region(struct kvm *kvm, 175f7784b8eSMarcelo Tosatti struct kvm_memory_slot *memslot, 176bbf45ba5SHollis Blanchard struct kvm_memory_slot old, 177f7784b8eSMarcelo Tosatti struct kvm_userspace_memory_region *mem, 178bbf45ba5SHollis Blanchard int user_alloc) 179bbf45ba5SHollis Blanchard { 180bbf45ba5SHollis Blanchard return 0; 181bbf45ba5SHollis Blanchard } 182bbf45ba5SHollis Blanchard 183f7784b8eSMarcelo Tosatti void kvm_arch_commit_memory_region(struct kvm *kvm, 184f7784b8eSMarcelo Tosatti struct kvm_userspace_memory_region *mem, 185f7784b8eSMarcelo Tosatti struct kvm_memory_slot old, 186f7784b8eSMarcelo Tosatti int user_alloc) 187f7784b8eSMarcelo Tosatti { 188f7784b8eSMarcelo Tosatti return; 189f7784b8eSMarcelo Tosatti } 190f7784b8eSMarcelo Tosatti 191f7784b8eSMarcelo Tosatti 19234d4cb8fSMarcelo Tosatti void kvm_arch_flush_shadow(struct kvm *kvm) 19334d4cb8fSMarcelo Tosatti { 19434d4cb8fSMarcelo Tosatti } 19534d4cb8fSMarcelo Tosatti 196bbf45ba5SHollis Blanchard struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id) 197bbf45ba5SHollis Blanchard { 19873e75b41SHollis Blanchard struct kvm_vcpu *vcpu; 19973e75b41SHollis Blanchard vcpu = kvmppc_core_vcpu_create(kvm, id); 20006056bfbSWei Yongjun if (!IS_ERR(vcpu)) 20173e75b41SHollis Blanchard kvmppc_create_vcpu_debugfs(vcpu, id); 20273e75b41SHollis Blanchard return vcpu; 203bbf45ba5SHollis Blanchard } 204bbf45ba5SHollis Blanchard 205bbf45ba5SHollis Blanchard void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu) 206bbf45ba5SHollis Blanchard { 207a595405dSAlexander Graf /* Make sure we're not using the vcpu anymore */ 208a595405dSAlexander Graf hrtimer_cancel(&vcpu->arch.dec_timer); 209a595405dSAlexander Graf tasklet_kill(&vcpu->arch.tasklet); 210a595405dSAlexander Graf 21173e75b41SHollis Blanchard kvmppc_remove_vcpu_debugfs(vcpu); 212db93f574SHollis Blanchard kvmppc_core_vcpu_free(vcpu); 213bbf45ba5SHollis Blanchard } 214bbf45ba5SHollis Blanchard 215bbf45ba5SHollis Blanchard void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu) 216bbf45ba5SHollis Blanchard { 217bbf45ba5SHollis Blanchard kvm_arch_vcpu_free(vcpu); 218bbf45ba5SHollis Blanchard } 219bbf45ba5SHollis Blanchard 220bbf45ba5SHollis Blanchard int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu) 221bbf45ba5SHollis Blanchard { 2229dd921cfSHollis Blanchard return kvmppc_core_pending_dec(vcpu); 223bbf45ba5SHollis Blanchard } 224bbf45ba5SHollis Blanchard 225bbf45ba5SHollis Blanchard static void kvmppc_decrementer_func(unsigned long data) 226bbf45ba5SHollis Blanchard { 227bbf45ba5SHollis Blanchard struct kvm_vcpu *vcpu = (struct kvm_vcpu *)data; 228bbf45ba5SHollis Blanchard 2299dd921cfSHollis Blanchard kvmppc_core_queue_dec(vcpu); 23045c5eb67SHollis Blanchard 23145c5eb67SHollis Blanchard if (waitqueue_active(&vcpu->wq)) { 23245c5eb67SHollis Blanchard wake_up_interruptible(&vcpu->wq); 23345c5eb67SHollis Blanchard vcpu->stat.halt_wakeup++; 23445c5eb67SHollis Blanchard } 235bbf45ba5SHollis Blanchard } 236bbf45ba5SHollis Blanchard 237544c6761SAlexander Graf /* 238544c6761SAlexander Graf * low level hrtimer wake routine. Because this runs in hardirq context 239544c6761SAlexander Graf * we schedule a tasklet to do the real work. 240544c6761SAlexander Graf */ 241544c6761SAlexander Graf enum hrtimer_restart kvmppc_decrementer_wakeup(struct hrtimer *timer) 242544c6761SAlexander Graf { 243544c6761SAlexander Graf struct kvm_vcpu *vcpu; 244544c6761SAlexander Graf 245544c6761SAlexander Graf vcpu = container_of(timer, struct kvm_vcpu, arch.dec_timer); 246544c6761SAlexander Graf tasklet_schedule(&vcpu->arch.tasklet); 247544c6761SAlexander Graf 248544c6761SAlexander Graf return HRTIMER_NORESTART; 249544c6761SAlexander Graf } 250544c6761SAlexander Graf 251bbf45ba5SHollis Blanchard int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu) 252bbf45ba5SHollis Blanchard { 253544c6761SAlexander Graf hrtimer_init(&vcpu->arch.dec_timer, CLOCK_REALTIME, HRTIMER_MODE_ABS); 254544c6761SAlexander Graf tasklet_init(&vcpu->arch.tasklet, kvmppc_decrementer_func, (ulong)vcpu); 255544c6761SAlexander Graf vcpu->arch.dec_timer.function = kvmppc_decrementer_wakeup; 256bbf45ba5SHollis Blanchard 257bbf45ba5SHollis Blanchard return 0; 258bbf45ba5SHollis Blanchard } 259bbf45ba5SHollis Blanchard 260bbf45ba5SHollis Blanchard void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu) 261bbf45ba5SHollis Blanchard { 262ecc0981fSHollis Blanchard kvmppc_mmu_destroy(vcpu); 263bbf45ba5SHollis Blanchard } 264bbf45ba5SHollis Blanchard 265bbf45ba5SHollis Blanchard void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu) 266bbf45ba5SHollis Blanchard { 2679dd921cfSHollis Blanchard kvmppc_core_vcpu_load(vcpu, cpu); 268bbf45ba5SHollis Blanchard } 269bbf45ba5SHollis Blanchard 270bbf45ba5SHollis Blanchard void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu) 271bbf45ba5SHollis Blanchard { 2729dd921cfSHollis Blanchard kvmppc_core_vcpu_put(vcpu); 273bbf45ba5SHollis Blanchard } 274bbf45ba5SHollis Blanchard 275d0bfb940SJan Kiszka int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu, 276d0bfb940SJan Kiszka struct kvm_guest_debug *dbg) 277bbf45ba5SHollis Blanchard { 278f5d0906bSHollis Blanchard return -EINVAL; 279bbf45ba5SHollis Blanchard } 280bbf45ba5SHollis Blanchard 281bbf45ba5SHollis Blanchard static void kvmppc_complete_dcr_load(struct kvm_vcpu *vcpu, 282bbf45ba5SHollis Blanchard struct kvm_run *run) 283bbf45ba5SHollis Blanchard { 2848e5b26b5SAlexander Graf kvmppc_set_gpr(vcpu, vcpu->arch.io_gpr, run->dcr.data); 285bbf45ba5SHollis Blanchard } 286bbf45ba5SHollis Blanchard 287bbf45ba5SHollis Blanchard static void kvmppc_complete_mmio_load(struct kvm_vcpu *vcpu, 288bbf45ba5SHollis Blanchard struct kvm_run *run) 289bbf45ba5SHollis Blanchard { 290b104d066SAlexander Graf u64 gpr; 291bbf45ba5SHollis Blanchard 2928e5b26b5SAlexander Graf if (run->mmio.len > sizeof(gpr)) { 293bbf45ba5SHollis Blanchard printk(KERN_ERR "bad MMIO length: %d\n", run->mmio.len); 294bbf45ba5SHollis Blanchard return; 295bbf45ba5SHollis Blanchard } 296bbf45ba5SHollis Blanchard 297bbf45ba5SHollis Blanchard if (vcpu->arch.mmio_is_bigendian) { 298bbf45ba5SHollis Blanchard switch (run->mmio.len) { 299b104d066SAlexander Graf case 8: gpr = *(u64 *)run->mmio.data; break; 3008e5b26b5SAlexander Graf case 4: gpr = *(u32 *)run->mmio.data; break; 3018e5b26b5SAlexander Graf case 2: gpr = *(u16 *)run->mmio.data; break; 3028e5b26b5SAlexander Graf case 1: gpr = *(u8 *)run->mmio.data; break; 303bbf45ba5SHollis Blanchard } 304bbf45ba5SHollis Blanchard } else { 305bbf45ba5SHollis Blanchard /* Convert BE data from userland back to LE. */ 306bbf45ba5SHollis Blanchard switch (run->mmio.len) { 3078e5b26b5SAlexander Graf case 4: gpr = ld_le32((u32 *)run->mmio.data); break; 3088e5b26b5SAlexander Graf case 2: gpr = ld_le16((u16 *)run->mmio.data); break; 3098e5b26b5SAlexander Graf case 1: gpr = *(u8 *)run->mmio.data; break; 310bbf45ba5SHollis Blanchard } 311bbf45ba5SHollis Blanchard } 3128e5b26b5SAlexander Graf 3133587d534SAlexander Graf if (vcpu->arch.mmio_sign_extend) { 3143587d534SAlexander Graf switch (run->mmio.len) { 3153587d534SAlexander Graf #ifdef CONFIG_PPC64 3163587d534SAlexander Graf case 4: 3173587d534SAlexander Graf gpr = (s64)(s32)gpr; 3183587d534SAlexander Graf break; 3193587d534SAlexander Graf #endif 3203587d534SAlexander Graf case 2: 3213587d534SAlexander Graf gpr = (s64)(s16)gpr; 3223587d534SAlexander Graf break; 3233587d534SAlexander Graf case 1: 3243587d534SAlexander Graf gpr = (s64)(s8)gpr; 3253587d534SAlexander Graf break; 3263587d534SAlexander Graf } 3273587d534SAlexander Graf } 3283587d534SAlexander Graf 3298e5b26b5SAlexander Graf kvmppc_set_gpr(vcpu, vcpu->arch.io_gpr, gpr); 330b104d066SAlexander Graf 331b104d066SAlexander Graf switch (vcpu->arch.io_gpr & KVM_REG_EXT_MASK) { 332b104d066SAlexander Graf case KVM_REG_GPR: 333b104d066SAlexander Graf kvmppc_set_gpr(vcpu, vcpu->arch.io_gpr, gpr); 334b104d066SAlexander Graf break; 335b104d066SAlexander Graf case KVM_REG_FPR: 336b104d066SAlexander Graf vcpu->arch.fpr[vcpu->arch.io_gpr & KVM_REG_MASK] = gpr; 337b104d066SAlexander Graf break; 338287d5611SAlexander Graf #ifdef CONFIG_PPC_BOOK3S 339b104d066SAlexander Graf case KVM_REG_QPR: 340b104d066SAlexander Graf vcpu->arch.qpr[vcpu->arch.io_gpr & KVM_REG_MASK] = gpr; 341b104d066SAlexander Graf break; 342b104d066SAlexander Graf case KVM_REG_FQPR: 343b104d066SAlexander Graf vcpu->arch.fpr[vcpu->arch.io_gpr & KVM_REG_MASK] = gpr; 344b104d066SAlexander Graf vcpu->arch.qpr[vcpu->arch.io_gpr & KVM_REG_MASK] = gpr; 345b104d066SAlexander Graf break; 346287d5611SAlexander Graf #endif 347b104d066SAlexander Graf default: 348b104d066SAlexander Graf BUG(); 349b104d066SAlexander Graf } 350bbf45ba5SHollis Blanchard } 351bbf45ba5SHollis Blanchard 352bbf45ba5SHollis Blanchard int kvmppc_handle_load(struct kvm_run *run, struct kvm_vcpu *vcpu, 353bbf45ba5SHollis Blanchard unsigned int rt, unsigned int bytes, int is_bigendian) 354bbf45ba5SHollis Blanchard { 355bbf45ba5SHollis Blanchard if (bytes > sizeof(run->mmio.data)) { 356bbf45ba5SHollis Blanchard printk(KERN_ERR "%s: bad MMIO length: %d\n", __func__, 357bbf45ba5SHollis Blanchard run->mmio.len); 358bbf45ba5SHollis Blanchard } 359bbf45ba5SHollis Blanchard 360bbf45ba5SHollis Blanchard run->mmio.phys_addr = vcpu->arch.paddr_accessed; 361bbf45ba5SHollis Blanchard run->mmio.len = bytes; 362bbf45ba5SHollis Blanchard run->mmio.is_write = 0; 363bbf45ba5SHollis Blanchard 364bbf45ba5SHollis Blanchard vcpu->arch.io_gpr = rt; 365bbf45ba5SHollis Blanchard vcpu->arch.mmio_is_bigendian = is_bigendian; 366bbf45ba5SHollis Blanchard vcpu->mmio_needed = 1; 367bbf45ba5SHollis Blanchard vcpu->mmio_is_write = 0; 3683587d534SAlexander Graf vcpu->arch.mmio_sign_extend = 0; 369bbf45ba5SHollis Blanchard 370bbf45ba5SHollis Blanchard return EMULATE_DO_MMIO; 371bbf45ba5SHollis Blanchard } 372bbf45ba5SHollis Blanchard 3733587d534SAlexander Graf /* Same as above, but sign extends */ 3743587d534SAlexander Graf int kvmppc_handle_loads(struct kvm_run *run, struct kvm_vcpu *vcpu, 3753587d534SAlexander Graf unsigned int rt, unsigned int bytes, int is_bigendian) 3763587d534SAlexander Graf { 3773587d534SAlexander Graf int r; 3783587d534SAlexander Graf 3793587d534SAlexander Graf r = kvmppc_handle_load(run, vcpu, rt, bytes, is_bigendian); 3803587d534SAlexander Graf vcpu->arch.mmio_sign_extend = 1; 3813587d534SAlexander Graf 3823587d534SAlexander Graf return r; 3833587d534SAlexander Graf } 3843587d534SAlexander Graf 385bbf45ba5SHollis Blanchard int kvmppc_handle_store(struct kvm_run *run, struct kvm_vcpu *vcpu, 386b104d066SAlexander Graf u64 val, unsigned int bytes, int is_bigendian) 387bbf45ba5SHollis Blanchard { 388bbf45ba5SHollis Blanchard void *data = run->mmio.data; 389bbf45ba5SHollis Blanchard 390bbf45ba5SHollis Blanchard if (bytes > sizeof(run->mmio.data)) { 391bbf45ba5SHollis Blanchard printk(KERN_ERR "%s: bad MMIO length: %d\n", __func__, 392bbf45ba5SHollis Blanchard run->mmio.len); 393bbf45ba5SHollis Blanchard } 394bbf45ba5SHollis Blanchard 395bbf45ba5SHollis Blanchard run->mmio.phys_addr = vcpu->arch.paddr_accessed; 396bbf45ba5SHollis Blanchard run->mmio.len = bytes; 397bbf45ba5SHollis Blanchard run->mmio.is_write = 1; 398bbf45ba5SHollis Blanchard vcpu->mmio_needed = 1; 399bbf45ba5SHollis Blanchard vcpu->mmio_is_write = 1; 400bbf45ba5SHollis Blanchard 401bbf45ba5SHollis Blanchard /* Store the value at the lowest bytes in 'data'. */ 402bbf45ba5SHollis Blanchard if (is_bigendian) { 403bbf45ba5SHollis Blanchard switch (bytes) { 404b104d066SAlexander Graf case 8: *(u64 *)data = val; break; 405bbf45ba5SHollis Blanchard case 4: *(u32 *)data = val; break; 406bbf45ba5SHollis Blanchard case 2: *(u16 *)data = val; break; 407bbf45ba5SHollis Blanchard case 1: *(u8 *)data = val; break; 408bbf45ba5SHollis Blanchard } 409bbf45ba5SHollis Blanchard } else { 410bbf45ba5SHollis Blanchard /* Store LE value into 'data'. */ 411bbf45ba5SHollis Blanchard switch (bytes) { 412bbf45ba5SHollis Blanchard case 4: st_le32(data, val); break; 413bbf45ba5SHollis Blanchard case 2: st_le16(data, val); break; 414bbf45ba5SHollis Blanchard case 1: *(u8 *)data = val; break; 415bbf45ba5SHollis Blanchard } 416bbf45ba5SHollis Blanchard } 417bbf45ba5SHollis Blanchard 418bbf45ba5SHollis Blanchard return EMULATE_DO_MMIO; 419bbf45ba5SHollis Blanchard } 420bbf45ba5SHollis Blanchard 421bbf45ba5SHollis Blanchard int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run) 422bbf45ba5SHollis Blanchard { 423bbf45ba5SHollis Blanchard int r; 424bbf45ba5SHollis Blanchard sigset_t sigsaved; 425bbf45ba5SHollis Blanchard 426bbf45ba5SHollis Blanchard if (vcpu->sigset_active) 427bbf45ba5SHollis Blanchard sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved); 428bbf45ba5SHollis Blanchard 429bbf45ba5SHollis Blanchard if (vcpu->mmio_needed) { 430bbf45ba5SHollis Blanchard if (!vcpu->mmio_is_write) 431bbf45ba5SHollis Blanchard kvmppc_complete_mmio_load(vcpu, run); 432bbf45ba5SHollis Blanchard vcpu->mmio_needed = 0; 433bbf45ba5SHollis Blanchard } else if (vcpu->arch.dcr_needed) { 434bbf45ba5SHollis Blanchard if (!vcpu->arch.dcr_is_write) 435bbf45ba5SHollis Blanchard kvmppc_complete_dcr_load(vcpu, run); 436bbf45ba5SHollis Blanchard vcpu->arch.dcr_needed = 0; 437ad0a048bSAlexander Graf } else if (vcpu->arch.osi_needed) { 438ad0a048bSAlexander Graf u64 *gprs = run->osi.gprs; 439ad0a048bSAlexander Graf int i; 440ad0a048bSAlexander Graf 441ad0a048bSAlexander Graf for (i = 0; i < 32; i++) 442ad0a048bSAlexander Graf kvmppc_set_gpr(vcpu, i, gprs[i]); 443ad0a048bSAlexander Graf vcpu->arch.osi_needed = 0; 444bbf45ba5SHollis Blanchard } 445bbf45ba5SHollis Blanchard 4469dd921cfSHollis Blanchard kvmppc_core_deliver_interrupts(vcpu); 447bbf45ba5SHollis Blanchard 448bbf45ba5SHollis Blanchard local_irq_disable(); 449bbf45ba5SHollis Blanchard kvm_guest_enter(); 450bbf45ba5SHollis Blanchard r = __kvmppc_vcpu_run(run, vcpu); 451bbf45ba5SHollis Blanchard kvm_guest_exit(); 452bbf45ba5SHollis Blanchard local_irq_enable(); 453bbf45ba5SHollis Blanchard 454bbf45ba5SHollis Blanchard if (vcpu->sigset_active) 455bbf45ba5SHollis Blanchard sigprocmask(SIG_SETMASK, &sigsaved, NULL); 456bbf45ba5SHollis Blanchard 457bbf45ba5SHollis Blanchard return r; 458bbf45ba5SHollis Blanchard } 459bbf45ba5SHollis Blanchard 460bbf45ba5SHollis Blanchard int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, struct kvm_interrupt *irq) 461bbf45ba5SHollis Blanchard { 46218978768SAlexander Graf if (irq->irq == KVM_INTERRUPT_UNSET) 46318978768SAlexander Graf kvmppc_core_dequeue_external(vcpu, irq); 46418978768SAlexander Graf else 4659dd921cfSHollis Blanchard kvmppc_core_queue_external(vcpu, irq); 46645c5eb67SHollis Blanchard 46745c5eb67SHollis Blanchard if (waitqueue_active(&vcpu->wq)) { 46845c5eb67SHollis Blanchard wake_up_interruptible(&vcpu->wq); 46945c5eb67SHollis Blanchard vcpu->stat.halt_wakeup++; 47045c5eb67SHollis Blanchard } 47145c5eb67SHollis Blanchard 472bbf45ba5SHollis Blanchard return 0; 473bbf45ba5SHollis Blanchard } 474bbf45ba5SHollis Blanchard 47571fbfd5fSAlexander Graf static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu, 47671fbfd5fSAlexander Graf struct kvm_enable_cap *cap) 47771fbfd5fSAlexander Graf { 47871fbfd5fSAlexander Graf int r; 47971fbfd5fSAlexander Graf 48071fbfd5fSAlexander Graf if (cap->flags) 48171fbfd5fSAlexander Graf return -EINVAL; 48271fbfd5fSAlexander Graf 48371fbfd5fSAlexander Graf switch (cap->cap) { 484ad0a048bSAlexander Graf case KVM_CAP_PPC_OSI: 485ad0a048bSAlexander Graf r = 0; 486ad0a048bSAlexander Graf vcpu->arch.osi_enabled = true; 487ad0a048bSAlexander Graf break; 48871fbfd5fSAlexander Graf default: 48971fbfd5fSAlexander Graf r = -EINVAL; 49071fbfd5fSAlexander Graf break; 49171fbfd5fSAlexander Graf } 49271fbfd5fSAlexander Graf 49371fbfd5fSAlexander Graf return r; 49471fbfd5fSAlexander Graf } 49571fbfd5fSAlexander Graf 496bbf45ba5SHollis Blanchard int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu, 497bbf45ba5SHollis Blanchard struct kvm_mp_state *mp_state) 498bbf45ba5SHollis Blanchard { 499bbf45ba5SHollis Blanchard return -EINVAL; 500bbf45ba5SHollis Blanchard } 501bbf45ba5SHollis Blanchard 502bbf45ba5SHollis Blanchard int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu, 503bbf45ba5SHollis Blanchard struct kvm_mp_state *mp_state) 504bbf45ba5SHollis Blanchard { 505bbf45ba5SHollis Blanchard return -EINVAL; 506bbf45ba5SHollis Blanchard } 507bbf45ba5SHollis Blanchard 508bbf45ba5SHollis Blanchard long kvm_arch_vcpu_ioctl(struct file *filp, 509bbf45ba5SHollis Blanchard unsigned int ioctl, unsigned long arg) 510bbf45ba5SHollis Blanchard { 511bbf45ba5SHollis Blanchard struct kvm_vcpu *vcpu = filp->private_data; 512bbf45ba5SHollis Blanchard void __user *argp = (void __user *)arg; 513bbf45ba5SHollis Blanchard long r; 514bbf45ba5SHollis Blanchard 515*93736624SAvi Kivity switch (ioctl) { 516*93736624SAvi Kivity case KVM_INTERRUPT: { 517bbf45ba5SHollis Blanchard struct kvm_interrupt irq; 518bbf45ba5SHollis Blanchard r = -EFAULT; 519bbf45ba5SHollis Blanchard if (copy_from_user(&irq, argp, sizeof(irq))) 520*93736624SAvi Kivity goto out; 521bbf45ba5SHollis Blanchard r = kvm_vcpu_ioctl_interrupt(vcpu, &irq); 522*93736624SAvi Kivity goto out; 523bbf45ba5SHollis Blanchard } 52419483d14SAvi Kivity 52571fbfd5fSAlexander Graf case KVM_ENABLE_CAP: 52671fbfd5fSAlexander Graf { 52771fbfd5fSAlexander Graf struct kvm_enable_cap cap; 52871fbfd5fSAlexander Graf r = -EFAULT; 52971fbfd5fSAlexander Graf if (copy_from_user(&cap, argp, sizeof(cap))) 53071fbfd5fSAlexander Graf goto out; 53171fbfd5fSAlexander Graf r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap); 53271fbfd5fSAlexander Graf break; 53371fbfd5fSAlexander Graf } 534bbf45ba5SHollis Blanchard default: 535bbf45ba5SHollis Blanchard r = -EINVAL; 536bbf45ba5SHollis Blanchard } 537bbf45ba5SHollis Blanchard 538bbf45ba5SHollis Blanchard out: 539bbf45ba5SHollis Blanchard return r; 540bbf45ba5SHollis Blanchard } 541bbf45ba5SHollis Blanchard 542bbf45ba5SHollis Blanchard long kvm_arch_vm_ioctl(struct file *filp, 543bbf45ba5SHollis Blanchard unsigned int ioctl, unsigned long arg) 544bbf45ba5SHollis Blanchard { 545bbf45ba5SHollis Blanchard long r; 546bbf45ba5SHollis Blanchard 547bbf45ba5SHollis Blanchard switch (ioctl) { 548bbf45ba5SHollis Blanchard default: 549367e1319SAvi Kivity r = -ENOTTY; 550bbf45ba5SHollis Blanchard } 551bbf45ba5SHollis Blanchard 552bbf45ba5SHollis Blanchard return r; 553bbf45ba5SHollis Blanchard } 554bbf45ba5SHollis Blanchard 555bbf45ba5SHollis Blanchard int kvm_arch_init(void *opaque) 556bbf45ba5SHollis Blanchard { 557bbf45ba5SHollis Blanchard return 0; 558bbf45ba5SHollis Blanchard } 559bbf45ba5SHollis Blanchard 560bbf45ba5SHollis Blanchard void kvm_arch_exit(void) 561bbf45ba5SHollis Blanchard { 562bbf45ba5SHollis Blanchard } 563