Path: blob/master/arch/powerpc/kvm/book3s_hv_p9_entry.c
26439 views
// SPDX-License-Identifier: GPL-2.0-only1#include <linux/kernel.h>2#include <linux/kvm_host.h>3#include <asm/asm-prototypes.h>4#include <asm/dbell.h>5#include <asm/ppc-opcode.h>67#include "book3s_hv.h"89static void load_spr_state(struct kvm_vcpu *vcpu,10struct p9_host_os_sprs *host_os_sprs)11{12/* TAR is very fast */13mtspr(SPRN_TAR, vcpu->arch.tar);1415#ifdef CONFIG_ALTIVEC16if (cpu_has_feature(CPU_FTR_ALTIVEC) &&17current->thread.vrsave != vcpu->arch.vrsave)18mtspr(SPRN_VRSAVE, vcpu->arch.vrsave);19#endif2021if (vcpu->arch.hfscr & HFSCR_EBB) {22if (current->thread.ebbhr != vcpu->arch.ebbhr)23mtspr(SPRN_EBBHR, vcpu->arch.ebbhr);24if (current->thread.ebbrr != vcpu->arch.ebbrr)25mtspr(SPRN_EBBRR, vcpu->arch.ebbrr);26if (current->thread.bescr != vcpu->arch.bescr)27mtspr(SPRN_BESCR, vcpu->arch.bescr);28}2930if (cpu_has_feature(CPU_FTR_P9_TIDR) &&31current->thread.tidr != vcpu->arch.tid)32mtspr(SPRN_TIDR, vcpu->arch.tid);33if (host_os_sprs->iamr != vcpu->arch.iamr)34mtspr(SPRN_IAMR, vcpu->arch.iamr);35if (host_os_sprs->amr != vcpu->arch.amr)36mtspr(SPRN_AMR, vcpu->arch.amr);37if (vcpu->arch.uamor != 0)38mtspr(SPRN_UAMOR, vcpu->arch.uamor);39if (current->thread.fscr != vcpu->arch.fscr)40mtspr(SPRN_FSCR, vcpu->arch.fscr);41if (current->thread.dscr != vcpu->arch.dscr)42mtspr(SPRN_DSCR, vcpu->arch.dscr);43if (vcpu->arch.pspb != 0)44mtspr(SPRN_PSPB, vcpu->arch.pspb);4546/*47* DAR, DSISR, and for nested HV, SPRGs must be set with MSR[RI]48* clear (or hstate set appropriately to catch those registers49* being clobbered if we take a MCE or SRESET), so those are done50* later.51*/5253if (!(vcpu->arch.ctrl & 1))54mtspr(SPRN_CTRLT, 0);55}5657static void store_spr_state(struct kvm_vcpu *vcpu)58{59vcpu->arch.tar = mfspr(SPRN_TAR);6061#ifdef CONFIG_ALTIVEC62if (cpu_has_feature(CPU_FTR_ALTIVEC))63vcpu->arch.vrsave = mfspr(SPRN_VRSAVE);64#endif6566if (vcpu->arch.hfscr & HFSCR_EBB) {67vcpu->arch.ebbhr = mfspr(SPRN_EBBHR);68vcpu->arch.ebbrr = mfspr(SPRN_EBBRR);69vcpu->arch.bescr = mfspr(SPRN_BESCR);70}7172if (cpu_has_feature(CPU_FTR_P9_TIDR))73vcpu->arch.tid = mfspr(SPRN_TIDR);74vcpu->arch.iamr = mfspr(SPRN_IAMR);75vcpu->arch.amr = mfspr(SPRN_AMR);76vcpu->arch.uamor = mfspr(SPRN_UAMOR);77vcpu->arch.fscr = mfspr(SPRN_FSCR);78vcpu->arch.dscr = mfspr(SPRN_DSCR);79vcpu->arch.pspb = mfspr(SPRN_PSPB);8081vcpu->arch.ctrl = mfspr(SPRN_CTRLF);82}8384/* Returns true if current MSR and/or guest MSR may have changed */85bool load_vcpu_state(struct kvm_vcpu *vcpu,86struct p9_host_os_sprs *host_os_sprs)87{88bool ret = false;8990#ifdef CONFIG_PPC_TRANSACTIONAL_MEM91if (cpu_has_feature(CPU_FTR_TM) ||92cpu_has_feature(CPU_FTR_P9_TM_HV_ASSIST)) {93unsigned long guest_msr = vcpu->arch.shregs.msr;94if (MSR_TM_ACTIVE(guest_msr)) {95kvmppc_restore_tm_hv(vcpu, guest_msr, true);96ret = true;97} else if (vcpu->arch.hfscr & HFSCR_TM) {98mtspr(SPRN_TEXASR, vcpu->arch.texasr);99mtspr(SPRN_TFHAR, vcpu->arch.tfhar);100mtspr(SPRN_TFIAR, vcpu->arch.tfiar);101}102}103#endif104105load_spr_state(vcpu, host_os_sprs);106107load_fp_state(&vcpu->arch.fp);108#ifdef CONFIG_ALTIVEC109load_vr_state(&vcpu->arch.vr);110#endif111112return ret;113}114EXPORT_SYMBOL_GPL(load_vcpu_state);115116void store_vcpu_state(struct kvm_vcpu *vcpu)117{118store_spr_state(vcpu);119120store_fp_state(&vcpu->arch.fp);121#ifdef CONFIG_ALTIVEC122store_vr_state(&vcpu->arch.vr);123#endif124125#ifdef CONFIG_PPC_TRANSACTIONAL_MEM126if (cpu_has_feature(CPU_FTR_TM) ||127cpu_has_feature(CPU_FTR_P9_TM_HV_ASSIST)) {128unsigned long guest_msr = vcpu->arch.shregs.msr;129if (MSR_TM_ACTIVE(guest_msr)) {130kvmppc_save_tm_hv(vcpu, guest_msr, true);131} else if (vcpu->arch.hfscr & HFSCR_TM) {132vcpu->arch.texasr = mfspr(SPRN_TEXASR);133vcpu->arch.tfhar = mfspr(SPRN_TFHAR);134vcpu->arch.tfiar = mfspr(SPRN_TFIAR);135136if (!vcpu->arch.nested) {137vcpu->arch.load_tm++; /* see load_ebb comment */138if (!vcpu->arch.load_tm)139vcpu->arch.hfscr &= ~HFSCR_TM;140}141}142}143#endif144}145EXPORT_SYMBOL_GPL(store_vcpu_state);146147void save_p9_host_os_sprs(struct p9_host_os_sprs *host_os_sprs)148{149host_os_sprs->iamr = mfspr(SPRN_IAMR);150host_os_sprs->amr = mfspr(SPRN_AMR);151}152EXPORT_SYMBOL_GPL(save_p9_host_os_sprs);153154/* vcpu guest regs must already be saved */155void restore_p9_host_os_sprs(struct kvm_vcpu *vcpu,156struct p9_host_os_sprs *host_os_sprs)157{158/*159* current->thread.xxx registers must all be restored to host160* values before a potential context switch, otherwise the context161* switch itself will overwrite current->thread.xxx with the values162* from the guest SPRs.163*/164165mtspr(SPRN_SPRG_VDSO_WRITE, local_paca->sprg_vdso);166167if (cpu_has_feature(CPU_FTR_P9_TIDR) &&168current->thread.tidr != vcpu->arch.tid)169mtspr(SPRN_TIDR, current->thread.tidr);170if (host_os_sprs->iamr != vcpu->arch.iamr)171mtspr(SPRN_IAMR, host_os_sprs->iamr);172if (vcpu->arch.uamor != 0)173mtspr(SPRN_UAMOR, 0);174if (host_os_sprs->amr != vcpu->arch.amr)175mtspr(SPRN_AMR, host_os_sprs->amr);176if (current->thread.fscr != vcpu->arch.fscr)177mtspr(SPRN_FSCR, current->thread.fscr);178if (current->thread.dscr != vcpu->arch.dscr)179mtspr(SPRN_DSCR, current->thread.dscr);180if (vcpu->arch.pspb != 0)181mtspr(SPRN_PSPB, 0);182183/* Save guest CTRL register, set runlatch to 1 */184if (!(vcpu->arch.ctrl & 1))185mtspr(SPRN_CTRLT, 1);186187#ifdef CONFIG_ALTIVEC188if (cpu_has_feature(CPU_FTR_ALTIVEC) &&189vcpu->arch.vrsave != current->thread.vrsave)190mtspr(SPRN_VRSAVE, current->thread.vrsave);191#endif192if (vcpu->arch.hfscr & HFSCR_EBB) {193if (vcpu->arch.bescr != current->thread.bescr)194mtspr(SPRN_BESCR, current->thread.bescr);195if (vcpu->arch.ebbhr != current->thread.ebbhr)196mtspr(SPRN_EBBHR, current->thread.ebbhr);197if (vcpu->arch.ebbrr != current->thread.ebbrr)198mtspr(SPRN_EBBRR, current->thread.ebbrr);199200if (!vcpu->arch.nested) {201/*202* This is like load_fp in context switching, turn off203* the facility after it wraps the u8 to try avoiding204* saving and restoring the registers each partition205* switch.206*/207vcpu->arch.load_ebb++;208if (!vcpu->arch.load_ebb)209vcpu->arch.hfscr &= ~HFSCR_EBB;210}211}212213if (vcpu->arch.tar != current->thread.tar)214mtspr(SPRN_TAR, current->thread.tar);215}216EXPORT_SYMBOL_GPL(restore_p9_host_os_sprs);217218#ifdef CONFIG_KVM_BOOK3S_HV_P9_TIMING219void accumulate_time(struct kvm_vcpu *vcpu, struct kvmhv_tb_accumulator *next)220{221struct kvmppc_vcore *vc = vcpu->arch.vcore;222struct kvmhv_tb_accumulator *curr;223u64 tb = mftb() - vc->tb_offset_applied;224u64 prev_tb;225u64 delta;226u64 seq;227228curr = vcpu->arch.cur_activity;229vcpu->arch.cur_activity = next;230prev_tb = vcpu->arch.cur_tb_start;231vcpu->arch.cur_tb_start = tb;232233if (!curr)234return;235236delta = tb - prev_tb;237238seq = curr->seqcount;239curr->seqcount = seq + 1;240smp_wmb();241curr->tb_total += delta;242if (seq == 0 || delta < curr->tb_min)243curr->tb_min = delta;244if (delta > curr->tb_max)245curr->tb_max = delta;246smp_wmb();247curr->seqcount = seq + 2;248}249EXPORT_SYMBOL_GPL(accumulate_time);250#endif251252static inline u64 mfslbv(unsigned int idx)253{254u64 slbev;255256asm volatile("slbmfev %0,%1" : "=r" (slbev) : "r" (idx));257258return slbev;259}260261static inline u64 mfslbe(unsigned int idx)262{263u64 slbee;264265asm volatile("slbmfee %0,%1" : "=r" (slbee) : "r" (idx));266267return slbee;268}269270static inline void mtslb(u64 slbee, u64 slbev)271{272asm volatile("slbmte %0,%1" :: "r" (slbev), "r" (slbee));273}274275static inline void clear_slb_entry(unsigned int idx)276{277mtslb(idx, 0);278}279280static inline void slb_clear_invalidate_partition(void)281{282clear_slb_entry(0);283asm volatile(PPC_SLBIA(6));284}285286/*287* Malicious or buggy radix guests may have inserted SLB entries288* (only 0..3 because radix always runs with UPRT=1), so these must289* be cleared here to avoid side-channels. slbmte is used rather290* than slbia, as it won't clear cached translations.291*/292static void radix_clear_slb(void)293{294int i;295296for (i = 0; i < 4; i++)297clear_slb_entry(i);298}299300static void switch_mmu_to_guest_radix(struct kvm *kvm, struct kvm_vcpu *vcpu, u64 lpcr)301{302struct kvm_nested_guest *nested = vcpu->arch.nested;303u32 lpid;304u32 pid;305306lpid = nested ? nested->shadow_lpid : kvm->arch.lpid;307pid = kvmppc_get_pid(vcpu);308309/*310* Prior memory accesses to host PID Q3 must be completed before we311* start switching, and stores must be drained to avoid not-my-LPAR312* logic (see switch_mmu_to_host).313*/314asm volatile("hwsync" ::: "memory");315isync();316mtspr(SPRN_LPID, lpid);317mtspr(SPRN_LPCR, lpcr);318mtspr(SPRN_PID, pid);319/*320* isync not required here because we are HRFID'ing to guest before321* any guest context access, which is context synchronising.322*/323}324325static void switch_mmu_to_guest_hpt(struct kvm *kvm, struct kvm_vcpu *vcpu, u64 lpcr)326{327u32 lpid;328u32 pid;329int i;330331lpid = kvm->arch.lpid;332pid = kvmppc_get_pid(vcpu);333334/*335* See switch_mmu_to_guest_radix. ptesync should not be required here336* even if the host is in HPT mode because speculative accesses would337* not cause RC updates (we are in real mode).338*/339asm volatile("hwsync" ::: "memory");340isync();341mtspr(SPRN_LPID, lpid);342mtspr(SPRN_LPCR, lpcr);343mtspr(SPRN_PID, pid);344345for (i = 0; i < vcpu->arch.slb_max; i++)346mtslb(vcpu->arch.slb[i].orige, vcpu->arch.slb[i].origv);347/*348* isync not required here, see switch_mmu_to_guest_radix.349*/350}351352static void switch_mmu_to_host(struct kvm *kvm, u32 pid)353{354u32 lpid = kvm->arch.host_lpid;355u64 lpcr = kvm->arch.host_lpcr;356357/*358* The guest has exited, so guest MMU context is no longer being359* non-speculatively accessed, but a hwsync is needed before the360* mtLPIDR / mtPIDR switch, in order to ensure all stores are drained,361* so the not-my-LPAR tlbie logic does not overlook them.362*/363asm volatile("hwsync" ::: "memory");364isync();365mtspr(SPRN_PID, pid);366mtspr(SPRN_LPID, lpid);367mtspr(SPRN_LPCR, lpcr);368/*369* isync is not required after the switch, because mtmsrd with L=0370* is performed after this switch, which is context synchronising.371*/372373if (!radix_enabled())374slb_restore_bolted_realmode();375}376377static void save_clear_host_mmu(struct kvm *kvm)378{379if (!radix_enabled()) {380/*381* Hash host could save and restore host SLB entries to382* reduce SLB fault overheads of VM exits, but for now the383* existing code clears all entries and restores just the384* bolted ones when switching back to host.385*/386slb_clear_invalidate_partition();387}388}389390static void save_clear_guest_mmu(struct kvm *kvm, struct kvm_vcpu *vcpu)391{392if (kvm_is_radix(kvm)) {393radix_clear_slb();394} else {395int i;396int nr = 0;397398/*399* This must run before switching to host (radix host can't400* access all SLBs).401*/402for (i = 0; i < vcpu->arch.slb_nr; i++) {403u64 slbee, slbev;404405slbee = mfslbe(i);406if (slbee & SLB_ESID_V) {407slbev = mfslbv(i);408vcpu->arch.slb[nr].orige = slbee | i;409vcpu->arch.slb[nr].origv = slbev;410nr++;411}412}413vcpu->arch.slb_max = nr;414slb_clear_invalidate_partition();415}416}417418static void flush_guest_tlb(struct kvm *kvm)419{420unsigned long rb, set;421422rb = PPC_BIT(52); /* IS = 2 */423if (kvm_is_radix(kvm)) {424/* R=1 PRS=1 RIC=2 */425asm volatile(PPC_TLBIEL(%0, %4, %3, %2, %1)426: : "r" (rb), "i" (1), "i" (1), "i" (2),427"r" (0) : "memory");428for (set = 1; set < kvm->arch.tlb_sets; ++set) {429rb += PPC_BIT(51); /* increment set number */430/* R=1 PRS=1 RIC=0 */431asm volatile(PPC_TLBIEL(%0, %4, %3, %2, %1)432: : "r" (rb), "i" (1), "i" (1), "i" (0),433"r" (0) : "memory");434}435asm volatile("ptesync": : :"memory");436// POWER9 congruence-class TLBIEL leaves ERAT. Flush it now.437asm volatile(PPC_RADIX_INVALIDATE_ERAT_GUEST : : :"memory");438} else {439for (set = 0; set < kvm->arch.tlb_sets; ++set) {440/* R=0 PRS=0 RIC=0 */441asm volatile(PPC_TLBIEL(%0, %4, %3, %2, %1)442: : "r" (rb), "i" (0), "i" (0), "i" (0),443"r" (0) : "memory");444rb += PPC_BIT(51); /* increment set number */445}446asm volatile("ptesync": : :"memory");447// POWER9 congruence-class TLBIEL leaves ERAT. Flush it now.448asm volatile(PPC_ISA_3_0_INVALIDATE_ERAT : : :"memory");449}450}451452static void check_need_tlb_flush(struct kvm *kvm, int pcpu,453struct kvm_nested_guest *nested)454{455cpumask_t *need_tlb_flush;456bool all_set = true;457int i;458459if (nested)460need_tlb_flush = &nested->need_tlb_flush;461else462need_tlb_flush = &kvm->arch.need_tlb_flush;463464if (likely(!cpumask_test_cpu(pcpu, need_tlb_flush)))465return;466467/*468* Individual threads can come in here, but the TLB is shared between469* the 4 threads in a core, hence invalidating on one thread470* invalidates for all, so only invalidate the first time (if all bits471* were set. The others must still execute a ptesync.472*473* If a race occurs and two threads do the TLB flush, that is not a474* problem, just sub-optimal.475*/476for (i = cpu_first_tlb_thread_sibling(pcpu);477i <= cpu_last_tlb_thread_sibling(pcpu);478i += cpu_tlb_thread_sibling_step()) {479if (!cpumask_test_cpu(i, need_tlb_flush)) {480all_set = false;481break;482}483}484if (all_set)485flush_guest_tlb(kvm);486else487asm volatile("ptesync" ::: "memory");488489/* Clear the bit after the TLB flush */490cpumask_clear_cpu(pcpu, need_tlb_flush);491}492493unsigned long kvmppc_msr_hard_disable_set_facilities(struct kvm_vcpu *vcpu, unsigned long msr)494{495unsigned long msr_needed = 0;496497msr &= ~MSR_EE;498499/* MSR bits may have been cleared by context switch so must recheck */500if (IS_ENABLED(CONFIG_PPC_FPU))501msr_needed |= MSR_FP;502if (cpu_has_feature(CPU_FTR_ALTIVEC))503msr_needed |= MSR_VEC;504if (cpu_has_feature(CPU_FTR_VSX))505msr_needed |= MSR_VSX;506if ((cpu_has_feature(CPU_FTR_TM) ||507cpu_has_feature(CPU_FTR_P9_TM_HV_ASSIST)) &&508(vcpu->arch.hfscr & HFSCR_TM))509msr_needed |= MSR_TM;510511/*512* This could be combined with MSR[RI] clearing, but that expands513* the unrecoverable window. It would be better to cover unrecoverable514* with KVM bad interrupt handling rather than use MSR[RI] at all.515*516* Much more difficult and less worthwhile to combine with IR/DR517* disable.518*/519if ((msr & msr_needed) != msr_needed) {520msr |= msr_needed;521__mtmsrd(msr, 0);522} else {523__hard_irq_disable();524}525local_paca->irq_happened |= PACA_IRQ_HARD_DIS;526527return msr;528}529EXPORT_SYMBOL_GPL(kvmppc_msr_hard_disable_set_facilities);530531int kvmhv_vcpu_entry_p9(struct kvm_vcpu *vcpu, u64 time_limit, unsigned long lpcr, u64 *tb)532{533struct p9_host_os_sprs host_os_sprs;534struct kvm *kvm = vcpu->kvm;535struct kvm_nested_guest *nested = vcpu->arch.nested;536struct kvmppc_vcore *vc = vcpu->arch.vcore;537s64 hdec, dec;538u64 purr, spurr;539u64 *exsave;540int trap;541unsigned long msr;542unsigned long host_hfscr;543unsigned long host_ciabr;544unsigned long host_dawr0;545unsigned long host_dawrx0;546unsigned long host_psscr;547unsigned long host_hpsscr;548unsigned long host_pidr;549unsigned long host_dawr1;550unsigned long host_dawrx1;551unsigned long dpdes;552553hdec = time_limit - *tb;554if (hdec < 0)555return BOOK3S_INTERRUPT_HV_DECREMENTER;556557WARN_ON_ONCE(vcpu->arch.shregs.msr & MSR_HV);558WARN_ON_ONCE(!(vcpu->arch.shregs.msr & MSR_ME));559560vcpu->arch.ceded = 0;561562/* Save MSR for restore, with EE clear. */563msr = mfmsr() & ~MSR_EE;564565host_hfscr = mfspr(SPRN_HFSCR);566host_ciabr = mfspr(SPRN_CIABR);567host_psscr = mfspr(SPRN_PSSCR_PR);568if (cpu_has_feature(CPU_FTR_P9_TM_HV_ASSIST))569host_hpsscr = mfspr(SPRN_PSSCR);570host_pidr = mfspr(SPRN_PID);571572if (dawr_enabled()) {573host_dawr0 = mfspr(SPRN_DAWR0);574host_dawrx0 = mfspr(SPRN_DAWRX0);575if (cpu_has_feature(CPU_FTR_DAWR1)) {576host_dawr1 = mfspr(SPRN_DAWR1);577host_dawrx1 = mfspr(SPRN_DAWRX1);578}579}580581local_paca->kvm_hstate.host_purr = mfspr(SPRN_PURR);582local_paca->kvm_hstate.host_spurr = mfspr(SPRN_SPURR);583584save_p9_host_os_sprs(&host_os_sprs);585586msr = kvmppc_msr_hard_disable_set_facilities(vcpu, msr);587if (lazy_irq_pending()) {588trap = 0;589goto out;590}591592if (unlikely(load_vcpu_state(vcpu, &host_os_sprs)))593msr = mfmsr(); /* MSR may have been updated */594595if (vc->tb_offset) {596u64 new_tb = *tb + vc->tb_offset;597mtspr(SPRN_TBU40, new_tb);598if ((mftb() & 0xffffff) < (new_tb & 0xffffff)) {599new_tb += 0x1000000;600mtspr(SPRN_TBU40, new_tb);601}602*tb = new_tb;603vc->tb_offset_applied = vc->tb_offset;604}605606mtspr(SPRN_VTB, vc->vtb);607mtspr(SPRN_PURR, vcpu->arch.purr);608mtspr(SPRN_SPURR, vcpu->arch.spurr);609610if (vc->pcr)611mtspr(SPRN_PCR, vc->pcr | PCR_MASK);612if (vcpu->arch.doorbell_request) {613vcpu->arch.doorbell_request = 0;614mtspr(SPRN_DPDES, 1);615}616617if (dawr_enabled()) {618if (vcpu->arch.dawr0 != host_dawr0)619mtspr(SPRN_DAWR0, vcpu->arch.dawr0);620if (vcpu->arch.dawrx0 != host_dawrx0)621mtspr(SPRN_DAWRX0, vcpu->arch.dawrx0);622if (cpu_has_feature(CPU_FTR_DAWR1)) {623if (vcpu->arch.dawr1 != host_dawr1)624mtspr(SPRN_DAWR1, vcpu->arch.dawr1);625if (vcpu->arch.dawrx1 != host_dawrx1)626mtspr(SPRN_DAWRX1, vcpu->arch.dawrx1);627}628}629if (vcpu->arch.ciabr != host_ciabr)630mtspr(SPRN_CIABR, vcpu->arch.ciabr);631632633if (cpu_has_feature(CPU_FTR_P9_TM_HV_ASSIST)) {634mtspr(SPRN_PSSCR, vcpu->arch.psscr | PSSCR_EC |635(local_paca->kvm_hstate.fake_suspend << PSSCR_FAKE_SUSPEND_LG));636} else {637if (vcpu->arch.psscr != host_psscr)638mtspr(SPRN_PSSCR_PR, vcpu->arch.psscr);639}640641mtspr(SPRN_HFSCR, vcpu->arch.hfscr);642643mtspr(SPRN_HSRR0, vcpu->arch.regs.nip);644mtspr(SPRN_HSRR1, (vcpu->arch.shregs.msr & ~MSR_HV) | MSR_ME);645646/*647* On POWER9 DD2.1 and below, sometimes on a Hypervisor Data Storage648* Interrupt (HDSI) the HDSISR is not be updated at all.649*650* To work around this we put a canary value into the HDSISR before651* returning to a guest and then check for this canary when we take a652* HDSI. If we find the canary on a HDSI, we know the hardware didn't653* update the HDSISR. In this case we return to the guest to retake the654* HDSI which should correctly update the HDSISR the second time HDSI655* entry.656*657* The "radix prefetch bug" test can be used to test for this bug, as658* it also exists fo DD2.1 and below.659*/660if (cpu_has_feature(CPU_FTR_P9_RADIX_PREFETCH_BUG))661mtspr(SPRN_HDSISR, HDSISR_CANARY);662663mtspr(SPRN_SPRG0, vcpu->arch.shregs.sprg0);664mtspr(SPRN_SPRG1, vcpu->arch.shregs.sprg1);665mtspr(SPRN_SPRG2, vcpu->arch.shregs.sprg2);666mtspr(SPRN_SPRG3, vcpu->arch.shregs.sprg3);667668/*669* It might be preferable to load_vcpu_state here, in order to get the670* GPR/FP register loads executing in parallel with the previous mtSPR671* instructions, but for now that can't be done because the TM handling672* in load_vcpu_state can change some SPRs and vcpu state (nip, msr).673* But TM could be split out if this would be a significant benefit.674*/675676/*677* MSR[RI] does not need to be cleared (and is not, for radix guests678* with no prefetch bug), because in_guest is set. If we take a SRESET679* or MCE with in_guest set but still in HV mode, then680* kvmppc_p9_bad_interrupt handles the interrupt, which effectively681* clears MSR[RI] and doesn't return.682*/683WRITE_ONCE(local_paca->kvm_hstate.in_guest, KVM_GUEST_MODE_HV_P9);684barrier(); /* Open in_guest critical section */685686/*687* Hash host, hash guest, or radix guest with prefetch bug, all have688* to disable the MMU before switching to guest MMU state.689*/690if (!radix_enabled() || !kvm_is_radix(kvm) ||691cpu_has_feature(CPU_FTR_P9_RADIX_PREFETCH_BUG))692__mtmsrd(msr & ~(MSR_IR|MSR_DR|MSR_RI), 0);693694save_clear_host_mmu(kvm);695696if (kvm_is_radix(kvm))697switch_mmu_to_guest_radix(kvm, vcpu, lpcr);698else699switch_mmu_to_guest_hpt(kvm, vcpu, lpcr);700701/* TLBIEL uses LPID=LPIDR, so run this after setting guest LPID */702check_need_tlb_flush(kvm, vc->pcpu, nested);703704/*705* P9 suppresses the HDEC exception when LPCR[HDICE] = 0,706* so set guest LPCR (with HDICE) before writing HDEC.707*/708mtspr(SPRN_HDEC, hdec);709710mtspr(SPRN_DEC, vcpu->arch.dec_expires - *tb);711712#ifdef CONFIG_PPC_TRANSACTIONAL_MEM713tm_return_to_guest:714#endif715mtspr(SPRN_DAR, vcpu->arch.shregs.dar);716mtspr(SPRN_DSISR, vcpu->arch.shregs.dsisr);717mtspr(SPRN_SRR0, vcpu->arch.shregs.srr0);718mtspr(SPRN_SRR1, vcpu->arch.shregs.srr1);719720switch_pmu_to_guest(vcpu, &host_os_sprs);721accumulate_time(vcpu, &vcpu->arch.in_guest);722723kvmppc_p9_enter_guest(vcpu);724725accumulate_time(vcpu, &vcpu->arch.guest_exit);726switch_pmu_to_host(vcpu, &host_os_sprs);727728/* XXX: Could get these from r11/12 and paca exsave instead */729vcpu->arch.shregs.srr0 = mfspr(SPRN_SRR0);730vcpu->arch.shregs.srr1 = mfspr(SPRN_SRR1);731vcpu->arch.shregs.dar = mfspr(SPRN_DAR);732vcpu->arch.shregs.dsisr = mfspr(SPRN_DSISR);733734/* 0x2 bit for HSRR is only used by PR and P7/8 HV paths, clear it */735trap = local_paca->kvm_hstate.scratch0 & ~0x2;736737if (likely(trap > BOOK3S_INTERRUPT_MACHINE_CHECK))738exsave = local_paca->exgen;739else if (trap == BOOK3S_INTERRUPT_SYSTEM_RESET)740exsave = local_paca->exnmi;741else /* trap == 0x200 */742exsave = local_paca->exmc;743744vcpu->arch.regs.gpr[1] = local_paca->kvm_hstate.scratch1;745vcpu->arch.regs.gpr[3] = local_paca->kvm_hstate.scratch2;746747/*748* After reading machine check regs (DAR, DSISR, SRR0/1) and hstate749* scratch (which we need to move into exsave to make re-entrant vs750* SRESET/MCE), register state is protected from reentrancy. However751* timebase, MMU, among other state is still set to guest, so don't752* enable MSR[RI] here. It gets enabled at the end, after in_guest753* is cleared.754*755* It is possible an NMI could come in here, which is why it is756* important to save the above state early so it can be debugged.757*/758759vcpu->arch.regs.gpr[9] = exsave[EX_R9/sizeof(u64)];760vcpu->arch.regs.gpr[10] = exsave[EX_R10/sizeof(u64)];761vcpu->arch.regs.gpr[11] = exsave[EX_R11/sizeof(u64)];762vcpu->arch.regs.gpr[12] = exsave[EX_R12/sizeof(u64)];763vcpu->arch.regs.gpr[13] = exsave[EX_R13/sizeof(u64)];764vcpu->arch.ppr = exsave[EX_PPR/sizeof(u64)];765vcpu->arch.cfar = exsave[EX_CFAR/sizeof(u64)];766vcpu->arch.regs.ctr = exsave[EX_CTR/sizeof(u64)];767768vcpu->arch.last_inst = KVM_INST_FETCH_FAILED;769770if (unlikely(trap == BOOK3S_INTERRUPT_MACHINE_CHECK)) {771vcpu->arch.fault_dar = exsave[EX_DAR/sizeof(u64)];772vcpu->arch.fault_dsisr = exsave[EX_DSISR/sizeof(u64)];773kvmppc_realmode_machine_check(vcpu);774775} else if (unlikely(trap == BOOK3S_INTERRUPT_HMI)) {776kvmppc_p9_realmode_hmi_handler(vcpu);777778} else if (trap == BOOK3S_INTERRUPT_H_EMUL_ASSIST) {779vcpu->arch.emul_inst = mfspr(SPRN_HEIR);780781} else if (trap == BOOK3S_INTERRUPT_H_DATA_STORAGE) {782vcpu->arch.fault_dar = exsave[EX_DAR/sizeof(u64)];783vcpu->arch.fault_dsisr = exsave[EX_DSISR/sizeof(u64)];784vcpu->arch.fault_gpa = mfspr(SPRN_ASDR);785786} else if (trap == BOOK3S_INTERRUPT_H_INST_STORAGE) {787vcpu->arch.fault_gpa = mfspr(SPRN_ASDR);788789} else if (trap == BOOK3S_INTERRUPT_H_FAC_UNAVAIL) {790vcpu->arch.hfscr = mfspr(SPRN_HFSCR);791792#ifdef CONFIG_PPC_TRANSACTIONAL_MEM793/*794* Softpatch interrupt for transactional memory emulation cases795* on POWER9 DD2.2. This is early in the guest exit path - we796* haven't saved registers or done a treclaim yet.797*/798} else if (trap == BOOK3S_INTERRUPT_HV_SOFTPATCH) {799vcpu->arch.emul_inst = mfspr(SPRN_HEIR);800801/*802* The cases we want to handle here are those where the guest803* is in real suspend mode and is trying to transition to804* transactional mode.805*/806if (!local_paca->kvm_hstate.fake_suspend &&807(vcpu->arch.shregs.msr & MSR_TS_S)) {808if (kvmhv_p9_tm_emulation_early(vcpu)) {809/*810* Go straight back into the guest with the811* new NIP/MSR as set by TM emulation.812*/813mtspr(SPRN_HSRR0, vcpu->arch.regs.nip);814mtspr(SPRN_HSRR1, vcpu->arch.shregs.msr);815goto tm_return_to_guest;816}817}818#endif819}820821/* Advance host PURR/SPURR by the amount used by guest */822purr = mfspr(SPRN_PURR);823spurr = mfspr(SPRN_SPURR);824local_paca->kvm_hstate.host_purr += purr - vcpu->arch.purr;825local_paca->kvm_hstate.host_spurr += spurr - vcpu->arch.spurr;826vcpu->arch.purr = purr;827vcpu->arch.spurr = spurr;828829vcpu->arch.ic = mfspr(SPRN_IC);830vcpu->arch.pid = mfspr(SPRN_PID);831vcpu->arch.psscr = mfspr(SPRN_PSSCR_PR);832833vcpu->arch.shregs.sprg0 = mfspr(SPRN_SPRG0);834vcpu->arch.shregs.sprg1 = mfspr(SPRN_SPRG1);835vcpu->arch.shregs.sprg2 = mfspr(SPRN_SPRG2);836vcpu->arch.shregs.sprg3 = mfspr(SPRN_SPRG3);837838dpdes = mfspr(SPRN_DPDES);839if (dpdes)840vcpu->arch.doorbell_request = 1;841842vc->vtb = mfspr(SPRN_VTB);843844dec = mfspr(SPRN_DEC);845if (!(lpcr & LPCR_LD)) /* Sign extend if not using large decrementer */846dec = (s32) dec;847*tb = mftb();848vcpu->arch.dec_expires = dec + *tb;849850if (vc->tb_offset_applied) {851u64 new_tb = *tb - vc->tb_offset_applied;852mtspr(SPRN_TBU40, new_tb);853if ((mftb() & 0xffffff) < (new_tb & 0xffffff)) {854new_tb += 0x1000000;855mtspr(SPRN_TBU40, new_tb);856}857*tb = new_tb;858vc->tb_offset_applied = 0;859}860861save_clear_guest_mmu(kvm, vcpu);862switch_mmu_to_host(kvm, host_pidr);863864/*865* Enable MSR here in order to have facilities enabled to save866* guest registers. This enables MMU (if we were in realmode), so867* only switch MMU on after the MMU is switched to host, to avoid868* the P9_RADIX_PREFETCH_BUG or hash guest context.869*/870if (IS_ENABLED(CONFIG_PPC_TRANSACTIONAL_MEM) &&871vcpu->arch.shregs.msr & MSR_TS_MASK)872msr |= MSR_TS_S;873__mtmsrd(msr, 0);874875store_vcpu_state(vcpu);876877mtspr(SPRN_PURR, local_paca->kvm_hstate.host_purr);878mtspr(SPRN_SPURR, local_paca->kvm_hstate.host_spurr);879880if (cpu_has_feature(CPU_FTR_P9_TM_HV_ASSIST)) {881/* Preserve PSSCR[FAKE_SUSPEND] until we've called kvmppc_save_tm_hv */882mtspr(SPRN_PSSCR, host_hpsscr |883(local_paca->kvm_hstate.fake_suspend << PSSCR_FAKE_SUSPEND_LG));884}885886mtspr(SPRN_HFSCR, host_hfscr);887if (vcpu->arch.ciabr != host_ciabr)888mtspr(SPRN_CIABR, host_ciabr);889890if (dawr_enabled()) {891if (vcpu->arch.dawr0 != host_dawr0)892mtspr(SPRN_DAWR0, host_dawr0);893if (vcpu->arch.dawrx0 != host_dawrx0)894mtspr(SPRN_DAWRX0, host_dawrx0);895if (cpu_has_feature(CPU_FTR_DAWR1)) {896if (vcpu->arch.dawr1 != host_dawr1)897mtspr(SPRN_DAWR1, host_dawr1);898if (vcpu->arch.dawrx1 != host_dawrx1)899mtspr(SPRN_DAWRX1, host_dawrx1);900}901}902903if (dpdes)904mtspr(SPRN_DPDES, 0);905if (vc->pcr)906mtspr(SPRN_PCR, PCR_MASK);907908/* HDEC must be at least as large as DEC, so decrementer_max fits */909mtspr(SPRN_HDEC, decrementer_max);910911timer_rearm_host_dec(*tb);912913restore_p9_host_os_sprs(vcpu, &host_os_sprs);914915barrier(); /* Close in_guest critical section */916WRITE_ONCE(local_paca->kvm_hstate.in_guest, KVM_GUEST_MODE_NONE);917/* Interrupts are recoverable at this point */918919/*920* cp_abort is required if the processor supports local copy-paste921* to clear the copy buffer that was under control of the guest.922*/923if (cpu_has_feature(CPU_FTR_ARCH_31))924asm volatile(PPC_CP_ABORT);925926out:927return trap;928}929EXPORT_SYMBOL_GPL(kvmhv_vcpu_entry_p9);930931932