Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Copyright (C) 2012-2015 - ARM Ltd
0004  * Author: Marc Zyngier <marc.zyngier@arm.com>
0005  */
0006 
0007 #include <hyp/sysreg-sr.h>
0008 
0009 #include <linux/compiler.h>
0010 #include <linux/kvm_host.h>
0011 
0012 #include <asm/kprobes.h>
0013 #include <asm/kvm_asm.h>
0014 #include <asm/kvm_emulate.h>
0015 #include <asm/kvm_hyp.h>
0016 
0017 /*
0018  * VHE: Host and guest must save mdscr_el1 and sp_el0 (and the PC and
0019  * pstate, which are handled as part of the el2 return state) on every
0020  * switch (sp_el0 is being dealt with in the assembly code).
0021  * tpidr_el0 and tpidrro_el0 only need to be switched when going
0022  * to host userspace or a different VCPU.  EL1 registers only need to be
0023  * switched when potentially going to run a different VCPU.  The latter two
0024  * classes are handled as part of kvm_arch_vcpu_load and kvm_arch_vcpu_put.
0025  */
0026 
0027 void sysreg_save_host_state_vhe(struct kvm_cpu_context *ctxt)
0028 {
0029     __sysreg_save_common_state(ctxt);
0030 }
0031 NOKPROBE_SYMBOL(sysreg_save_host_state_vhe);
0032 
0033 void sysreg_save_guest_state_vhe(struct kvm_cpu_context *ctxt)
0034 {
0035     __sysreg_save_common_state(ctxt);
0036     __sysreg_save_el2_return_state(ctxt);
0037 }
0038 NOKPROBE_SYMBOL(sysreg_save_guest_state_vhe);
0039 
0040 void sysreg_restore_host_state_vhe(struct kvm_cpu_context *ctxt)
0041 {
0042     __sysreg_restore_common_state(ctxt);
0043 }
0044 NOKPROBE_SYMBOL(sysreg_restore_host_state_vhe);
0045 
0046 void sysreg_restore_guest_state_vhe(struct kvm_cpu_context *ctxt)
0047 {
0048     __sysreg_restore_common_state(ctxt);
0049     __sysreg_restore_el2_return_state(ctxt);
0050 }
0051 NOKPROBE_SYMBOL(sysreg_restore_guest_state_vhe);
0052 
0053 /**
0054  * kvm_vcpu_load_sysregs_vhe - Load guest system registers to the physical CPU
0055  *
0056  * @vcpu: The VCPU pointer
0057  *
0058  * Load system registers that do not affect the host's execution, for
0059  * example EL1 system registers on a VHE system where the host kernel
0060  * runs at EL2.  This function is called from KVM's vcpu_load() function
0061  * and loading system register state early avoids having to load them on
0062  * every entry to the VM.
0063  */
0064 void kvm_vcpu_load_sysregs_vhe(struct kvm_vcpu *vcpu)
0065 {
0066     struct kvm_cpu_context *guest_ctxt = &vcpu->arch.ctxt;
0067     struct kvm_cpu_context *host_ctxt;
0068 
0069     host_ctxt = &this_cpu_ptr(&kvm_host_data)->host_ctxt;
0070     __sysreg_save_user_state(host_ctxt);
0071 
0072     /*
0073      * Load guest EL1 and user state
0074      *
0075      * We must restore the 32-bit state before the sysregs, thanks
0076      * to erratum #852523 (Cortex-A57) or #853709 (Cortex-A72).
0077      */
0078     __sysreg32_restore_state(vcpu);
0079     __sysreg_restore_user_state(guest_ctxt);
0080     __sysreg_restore_el1_state(guest_ctxt);
0081 
0082     vcpu_set_flag(vcpu, SYSREGS_ON_CPU);
0083 
0084     activate_traps_vhe_load(vcpu);
0085 }
0086 
0087 /**
0088  * kvm_vcpu_put_sysregs_vhe - Restore host system registers to the physical CPU
0089  *
0090  * @vcpu: The VCPU pointer
0091  *
0092  * Save guest system registers that do not affect the host's execution, for
0093  * example EL1 system registers on a VHE system where the host kernel
0094  * runs at EL2.  This function is called from KVM's vcpu_put() function
0095  * and deferring saving system register state until we're no longer running the
0096  * VCPU avoids having to save them on every exit from the VM.
0097  */
0098 void kvm_vcpu_put_sysregs_vhe(struct kvm_vcpu *vcpu)
0099 {
0100     struct kvm_cpu_context *guest_ctxt = &vcpu->arch.ctxt;
0101     struct kvm_cpu_context *host_ctxt;
0102 
0103     host_ctxt = &this_cpu_ptr(&kvm_host_data)->host_ctxt;
0104     deactivate_traps_vhe_put(vcpu);
0105 
0106     __sysreg_save_el1_state(guest_ctxt);
0107     __sysreg_save_user_state(guest_ctxt);
0108     __sysreg32_save_state(vcpu);
0109 
0110     /* Restore host user state */
0111     __sysreg_restore_user_state(host_ctxt);
0112 
0113     vcpu_clear_flag(vcpu, SYSREGS_ON_CPU);
0114 }