^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Copyright IBM Corp. 2007
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright 2011 Freescale Semiconductor, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Authors: Hollis Blanchard <hollisb@us.ibm.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/jiffies.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/hrtimer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/kvm_host.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/clockchips.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <asm/reg.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <asm/time.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <asm/byteorder.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <asm/kvm_ppc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <asm/disassemble.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <asm/ppc-opcode.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include "timing.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include "trace.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) void kvmppc_emulate_dec(struct kvm_vcpu *vcpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) unsigned long dec_nsec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) unsigned long long dec_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) pr_debug("mtDEC: %lx\n", vcpu->arch.dec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) #ifdef CONFIG_PPC_BOOK3S
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) /* mtdec lowers the interrupt line when positive. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) kvmppc_core_dequeue_dec(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) #ifdef CONFIG_BOOKE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) /* On BOOKE, DEC = 0 is as good as decrementer not enabled */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) if (vcpu->arch.dec == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) * The decrementer ticks at the same rate as the timebase, so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) * that's how we convert the guest DEC value to the number of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) * host ticks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) dec_time = vcpu->arch.dec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) * Guest timebase ticks at the same frequency as host timebase.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) * So use the host timebase calculations for decrementer emulation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) dec_time = tb_to_ns(dec_time);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) dec_nsec = do_div(dec_time, NSEC_PER_SEC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) hrtimer_start(&vcpu->arch.dec_timer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) ktime_set(dec_time, dec_nsec), HRTIMER_MODE_REL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) vcpu->arch.dec_jiffies = get_tb();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) u32 kvmppc_get_dec(struct kvm_vcpu *vcpu, u64 tb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) u64 jd = tb - vcpu->arch.dec_jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) #ifdef CONFIG_BOOKE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) if (vcpu->arch.dec < jd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) return vcpu->arch.dec - jd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) static int kvmppc_emulate_mtspr(struct kvm_vcpu *vcpu, int sprn, int rs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) enum emulation_result emulated = EMULATE_DONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) ulong spr_val = kvmppc_get_gpr(vcpu, rs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) switch (sprn) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) case SPRN_SRR0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) kvmppc_set_srr0(vcpu, spr_val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) case SPRN_SRR1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) kvmppc_set_srr1(vcpu, spr_val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) /* XXX We need to context-switch the timebase for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) * watchdog and FIT. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) case SPRN_TBWL: break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) case SPRN_TBWU: break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) case SPRN_DEC:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) vcpu->arch.dec = (u32) spr_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) kvmppc_emulate_dec(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) case SPRN_SPRG0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) kvmppc_set_sprg0(vcpu, spr_val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) case SPRN_SPRG1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) kvmppc_set_sprg1(vcpu, spr_val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) case SPRN_SPRG2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) kvmppc_set_sprg2(vcpu, spr_val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) case SPRN_SPRG3:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) kvmppc_set_sprg3(vcpu, spr_val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) /* PIR can legally be written, but we ignore it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) case SPRN_PIR: break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) emulated = vcpu->kvm->arch.kvm_ops->emulate_mtspr(vcpu, sprn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) spr_val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) if (emulated == EMULATE_FAIL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) printk(KERN_INFO "mtspr: unknown spr "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) "0x%x\n", sprn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) kvmppc_set_exit_type(vcpu, EMULATED_MTSPR_EXITS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) return emulated;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) static int kvmppc_emulate_mfspr(struct kvm_vcpu *vcpu, int sprn, int rt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) enum emulation_result emulated = EMULATE_DONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) ulong spr_val = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) switch (sprn) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) case SPRN_SRR0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) spr_val = kvmppc_get_srr0(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) case SPRN_SRR1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) spr_val = kvmppc_get_srr1(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) case SPRN_PVR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) spr_val = vcpu->arch.pvr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) case SPRN_PIR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) spr_val = vcpu->vcpu_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) /* Note: mftb and TBRL/TBWL are user-accessible, so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) * the guest can always access the real TB anyways.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) * In fact, we probably will never see these traps. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) case SPRN_TBWL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) spr_val = get_tb() >> 32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) case SPRN_TBWU:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) spr_val = get_tb();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) case SPRN_SPRG0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) spr_val = kvmppc_get_sprg0(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) case SPRN_SPRG1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) spr_val = kvmppc_get_sprg1(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) case SPRN_SPRG2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) spr_val = kvmppc_get_sprg2(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) case SPRN_SPRG3:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) spr_val = kvmppc_get_sprg3(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) /* Note: SPRG4-7 are user-readable, so we don't get
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) * a trap. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) case SPRN_DEC:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) spr_val = kvmppc_get_dec(vcpu, get_tb());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) emulated = vcpu->kvm->arch.kvm_ops->emulate_mfspr(vcpu, sprn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) &spr_val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) if (unlikely(emulated == EMULATE_FAIL)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) printk(KERN_INFO "mfspr: unknown spr "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) "0x%x\n", sprn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) if (emulated == EMULATE_DONE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) kvmppc_set_gpr(vcpu, rt, spr_val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) kvmppc_set_exit_type(vcpu, EMULATED_MFSPR_EXITS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) return emulated;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) /* XXX Should probably auto-generate instruction decoding for a particular core
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) * from opcode tables in the future. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) int kvmppc_emulate_instruction(struct kvm_vcpu *vcpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) u32 inst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) int rs, rt, sprn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) enum emulation_result emulated;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) int advance = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) /* this default type might be overwritten by subcategories */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) kvmppc_set_exit_type(vcpu, EMULATED_INST_EXITS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) emulated = kvmppc_get_last_inst(vcpu, INST_GENERIC, &inst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) if (emulated != EMULATE_DONE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) return emulated;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) pr_debug("Emulating opcode %d / %d\n", get_op(inst), get_xop(inst));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) rs = get_rs(inst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) rt = get_rt(inst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) sprn = get_sprn(inst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) switch (get_op(inst)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) case OP_TRAP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) #ifdef CONFIG_PPC_BOOK3S
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) case OP_TRAP_64:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) kvmppc_core_queue_program(vcpu, SRR1_PROGTRAP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) kvmppc_core_queue_program(vcpu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) vcpu->arch.shared->esr | ESR_PTR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) advance = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) case 31:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) switch (get_xop(inst)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) case OP_31_XOP_TRAP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) #ifdef CONFIG_64BIT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) case OP_31_XOP_TRAP_64:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) #ifdef CONFIG_PPC_BOOK3S
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) kvmppc_core_queue_program(vcpu, SRR1_PROGTRAP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) kvmppc_core_queue_program(vcpu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) vcpu->arch.shared->esr | ESR_PTR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) advance = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) case OP_31_XOP_MFSPR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) emulated = kvmppc_emulate_mfspr(vcpu, sprn, rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) if (emulated == EMULATE_AGAIN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) emulated = EMULATE_DONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) advance = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) case OP_31_XOP_MTSPR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) emulated = kvmppc_emulate_mtspr(vcpu, sprn, rs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) if (emulated == EMULATE_AGAIN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) emulated = EMULATE_DONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) advance = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) case OP_31_XOP_TLBSYNC:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) /* Attempt core-specific emulation below. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) emulated = EMULATE_FAIL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) case 0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) * Instruction with primary opcode 0. Based on PowerISA
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) * these are illegal instructions.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) if (inst == KVMPPC_INST_SW_BREAKPOINT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) vcpu->run->exit_reason = KVM_EXIT_DEBUG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) vcpu->run->debug.arch.status = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) vcpu->run->debug.arch.address = kvmppc_get_pc(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) emulated = EMULATE_EXIT_USER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) advance = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) emulated = EMULATE_FAIL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) emulated = EMULATE_FAIL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) if (emulated == EMULATE_FAIL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) emulated = vcpu->kvm->arch.kvm_ops->emulate_op(vcpu, inst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) &advance);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) if (emulated == EMULATE_AGAIN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) advance = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) } else if (emulated == EMULATE_FAIL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) advance = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) printk(KERN_ERR "Couldn't emulate instruction 0x%08x "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) "(op %d xop %d)\n", inst, get_op(inst), get_xop(inst));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) trace_kvm_ppc_instr(inst, kvmppc_get_pc(vcpu), emulated);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) /* Advance past emulated instruction. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) if (advance)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) kvmppc_set_pc(vcpu, kvmppc_get_pc(vcpu) + 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) return emulated;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) EXPORT_SYMBOL_GPL(kvmppc_emulate_instruction);