KVM
Macros | Functions | Variables
switch.c File Reference
#include <hyp/switch.h>
#include <hyp/sysreg-sr.h>
#include <linux/arm-smccc.h>
#include <linux/kvm_host.h>
#include <linux/types.h>
#include <linux/jump_label.h>
#include <uapi/linux/psci.h>
#include <kvm/arm_psci.h>
#include <asm/barrier.h>
#include <asm/cpufeature.h>
#include <asm/kprobes.h>
#include <asm/kvm_asm.h>
#include <asm/kvm_emulate.h>
#include <asm/kvm_hyp.h>
#include <asm/kvm_mmu.h>
#include <asm/fpsimd.h>
#include <asm/debug-monitors.h>
#include <asm/processor.h>
#include <nvhe/fixed_config.h>
#include <nvhe/mem_protect.h>
Include dependency graph for switch.c:

Go to the source code of this file.

Macros

#define __pmu_switch_to_guest(v)   ({ false; })
 
#define __pmu_switch_to_host(v)   do {} while (0)
 

Functions

 DEFINE_PER_CPU (struct kvm_host_data, kvm_host_data)
 
 DEFINE_PER_CPU (struct kvm_cpu_context, kvm_hyp_ctxt)
 
 DEFINE_PER_CPU (unsigned long, kvm_hyp_vector)
 
void kvm_nvhe_prepare_backtrace (unsigned long fp, unsigned long pc)
 
static void __activate_traps (struct kvm_vcpu *vcpu)
 
static void __deactivate_traps (struct kvm_vcpu *vcpu)
 
static void __hyp_vgic_save_state (struct kvm_vcpu *vcpu)
 
static void __hyp_vgic_restore_state (struct kvm_vcpu *vcpu)
 
static bool kvm_handle_pvm_sys64 (struct kvm_vcpu *vcpu, u64 *exit_code)
 
static const exit_handler_fnkvm_get_exit_handler_array (struct kvm_vcpu *vcpu)
 
static void early_exit_filter (struct kvm_vcpu *vcpu, u64 *exit_code)
 
int __kvm_vcpu_run (struct kvm_vcpu *vcpu)
 
asmlinkage void __noreturn hyp_panic (void)
 
asmlinkage void __noreturn hyp_panic_bad_stack (void)
 
asmlinkage void kvm_unexpected_el2_exception (void)
 

Variables

static const exit_handler_fn hyp_exit_handlers []
 
static const exit_handler_fn pvm_exit_handlers []
 

Macro Definition Documentation

◆ __pmu_switch_to_guest

#define __pmu_switch_to_guest (   v)    ({ false; })

Definition at line 164 of file switch.c.

◆ __pmu_switch_to_host

#define __pmu_switch_to_host (   v)    do {} while (0)

Definition at line 165 of file switch.c.

Function Documentation

◆ __activate_traps()

static void __activate_traps ( struct kvm_vcpu *  vcpu)
static

Definition at line 39 of file switch.c.

40 {
41  u64 val;
42 
43  ___activate_traps(vcpu);
45 
46  val = vcpu->arch.cptr_el2;
47  val |= CPTR_EL2_TAM; /* Same bit irrespective of E2H */
48  val |= has_hvhe() ? CPACR_EL1_TTA : CPTR_EL2_TTA;
49  if (cpus_have_final_cap(ARM64_SME)) {
50  if (has_hvhe())
51  val &= ~(CPACR_EL1_SMEN_EL1EN | CPACR_EL1_SMEN_EL0EN);
52  else
53  val |= CPTR_EL2_TSM;
54  }
55 
56  if (!guest_owns_fp_regs(vcpu)) {
57  if (has_hvhe())
58  val &= ~(CPACR_EL1_FPEN_EL0EN | CPACR_EL1_FPEN_EL1EN |
59  CPACR_EL1_ZEN_EL0EN | CPACR_EL1_ZEN_EL1EN);
60  else
61  val |= CPTR_EL2_TFP | CPTR_EL2_TZ;
62 
64  }
65 
66  kvm_write_cptr_el2(val);
67  write_sysreg(__this_cpu_read(kvm_hyp_vector), vbar_el2);
68 
69  if (cpus_have_final_cap(ARM64_WORKAROUND_SPECULATIVE_AT)) {
70  struct kvm_cpu_context *ctxt = &vcpu->arch.ctxt;
71 
72  isb();
73  /*
74  * At this stage, and thanks to the above isb(), S2 is
75  * configured and enabled. We can now restore the guest's S1
76  * configuration: SCTLR, and only then TCR.
77  */
78  write_sysreg_el1(ctxt_sys_reg(ctxt, SCTLR_EL1), SYS_SCTLR);
79  isb();
80  write_sysreg_el1(ctxt_sys_reg(ctxt, TCR_EL1), SYS_TCR);
81  }
82 }
static void __activate_traps_common(struct kvm_vcpu *vcpu)
Definition: switch.h:207
static void __activate_traps_fpsimd32(struct kvm_vcpu *vcpu)
Definition: switch.h:57
static void ___activate_traps(struct kvm_vcpu *vcpu)
Definition: switch.h:268
static bool guest_owns_fp_regs(struct kvm_vcpu *vcpu)
Definition: switch.h:43
Here is the call graph for this function:
Here is the caller graph for this function:

◆ __deactivate_traps()

static void __deactivate_traps ( struct kvm_vcpu *  vcpu)
static

Definition at line 84 of file switch.c.

85 {
86  extern char __kvm_hyp_host_vector[];
87 
88  ___deactivate_traps(vcpu);
89 
90  if (cpus_have_final_cap(ARM64_WORKAROUND_SPECULATIVE_AT)) {
91  u64 val;
92 
93  /*
94  * Set the TCR and SCTLR registers in the exact opposite
95  * sequence as __activate_traps (first prevent walks,
96  * then force the MMU on). A generous sprinkling of isb()
97  * ensure that things happen in this exact order.
98  */
99  val = read_sysreg_el1(SYS_TCR);
100  write_sysreg_el1(val | TCR_EPD1_MASK | TCR_EPD0_MASK, SYS_TCR);
101  isb();
102  val = read_sysreg_el1(SYS_SCTLR);
103  write_sysreg_el1(val | SCTLR_ELx_M, SYS_SCTLR);
104  isb();
105  }
106 
108 
109  write_sysreg(this_cpu_ptr(&kvm_init_params)->hcr_el2, hcr_el2);
110 
111  kvm_reset_cptr_el2(vcpu);
112  write_sysreg(__kvm_hyp_host_vector, vbar_el2);
113 }
static void __deactivate_traps_common(struct kvm_vcpu *vcpu)
Definition: switch.h:249
static void ___deactivate_traps(struct kvm_vcpu *vcpu)
Definition: switch.h:281
Here is the call graph for this function:
Here is the caller graph for this function:

◆ __hyp_vgic_restore_state()

static void __hyp_vgic_restore_state ( struct kvm_vcpu *  vcpu)
static

Definition at line 125 of file switch.c.

126 {
127  if (static_branch_unlikely(&kvm_vgic_global_state.gicv3_cpuif)) {
128  __vgic_v3_activate_traps(&vcpu->arch.vgic_cpu.vgic_v3);
129  __vgic_v3_restore_state(&vcpu->arch.vgic_cpu.vgic_v3);
130  }
131 }
struct vgic_global kvm_vgic_global_state
struct static_key_false gicv3_cpuif
Definition: arm_vgic.h:81
void __vgic_v3_restore_state(struct vgic_v3_cpu_if *cpu_if)
Definition: vgic-v3-sr.c:234
void __vgic_v3_activate_traps(struct vgic_v3_cpu_if *cpu_if)
Definition: vgic-v3-sr.c:260
Here is the call graph for this function:
Here is the caller graph for this function:

◆ __hyp_vgic_save_state()

static void __hyp_vgic_save_state ( struct kvm_vcpu *  vcpu)
static

Definition at line 116 of file switch.c.

117 {
118  if (static_branch_unlikely(&kvm_vgic_global_state.gicv3_cpuif)) {
119  __vgic_v3_save_state(&vcpu->arch.vgic_cpu.vgic_v3);
120  __vgic_v3_deactivate_traps(&vcpu->arch.vgic_cpu.vgic_v3);
121  }
122 }
void __vgic_v3_save_state(struct vgic_v3_cpu_if *cpu_if)
Definition: vgic-v3-sr.c:199
void __vgic_v3_deactivate_traps(struct vgic_v3_cpu_if *cpu_if)
Definition: vgic-v3-sr.c:307
Here is the call graph for this function:
Here is the caller graph for this function:

◆ __kvm_vcpu_run()

int __kvm_vcpu_run ( struct kvm_vcpu *  vcpu)

Definition at line 248 of file switch.c.

249 {
250  struct kvm_cpu_context *host_ctxt;
251  struct kvm_cpu_context *guest_ctxt;
252  struct kvm_s2_mmu *mmu;
253  bool pmu_switch_needed;
254  u64 exit_code;
255 
256  /*
257  * Having IRQs masked via PMR when entering the guest means the GIC
258  * will not signal the CPU of interrupts of lower priority, and the
259  * only way to get out will be via guest exceptions.
260  * Naturally, we want to avoid this.
261  */
262  if (system_uses_irq_prio_masking()) {
263  gic_write_pmr(GIC_PRIO_IRQON | GIC_PRIO_PSR_I_SET);
264  pmr_sync();
265  }
266 
267  host_ctxt = &this_cpu_ptr(&kvm_host_data)->host_ctxt;
268  host_ctxt->__hyp_running_vcpu = vcpu;
269  guest_ctxt = &vcpu->arch.ctxt;
270 
271  pmu_switch_needed = __pmu_switch_to_guest(vcpu);
272 
273  __sysreg_save_state_nvhe(host_ctxt);
274  /*
275  * We must flush and disable the SPE buffer for nVHE, as
276  * the translation regime(EL1&0) is going to be loaded with
277  * that of the guest. And we must do this before we change the
278  * translation regime to EL2 (via MDCR_EL2_E2PB == 0) and
279  * before we load guest Stage1.
280  */
282 
283  /*
284  * We're about to restore some new MMU state. Make sure
285  * ongoing page-table walks that have started before we
286  * trapped to EL2 have completed. This also synchronises the
287  * above disabling of SPE and TRBE.
288  *
289  * See DDI0487I.a D8.1.5 "Out-of-context translation regimes",
290  * rule R_LFHQG and subsequent information statements.
291  */
292  dsb(nsh);
293 
294  __kvm_adjust_pc(vcpu);
295 
296  /*
297  * We must restore the 32-bit state before the sysregs, thanks
298  * to erratum #852523 (Cortex-A57) or #853709 (Cortex-A72).
299  *
300  * Also, and in order to be able to deal with erratum #1319537 (A57)
301  * and #1319367 (A72), we must ensure that all VM-related sysreg are
302  * restored before we enable S2 translation.
303  */
305  __sysreg_restore_state_nvhe(guest_ctxt);
306 
307  mmu = kern_hyp_va(vcpu->arch.hw_mmu);
308  __load_stage2(mmu, kern_hyp_va(mmu->arch));
309  __activate_traps(vcpu);
310 
312  __timer_enable_traps(vcpu);
313 
315 
316  do {
317  /* Jump in the fire! */
318  exit_code = __guest_enter(vcpu);
319 
320  /* And we're baaack! */
321  } while (fixup_guest_exit(vcpu, &exit_code));
322 
323  __sysreg_save_state_nvhe(guest_ctxt);
324  __sysreg32_save_state(vcpu);
325  __timer_disable_traps(vcpu);
326  __hyp_vgic_save_state(vcpu);
327 
328  /*
329  * Same thing as before the guest run: we're about to switch
330  * the MMU context, so let's make sure we don't have any
331  * ongoing EL1&0 translations.
332  */
333  dsb(nsh);
334 
335  __deactivate_traps(vcpu);
337 
338  __sysreg_restore_state_nvhe(host_ctxt);
339 
340  if (vcpu->arch.fp_state == FP_STATE_GUEST_OWNED)
341  __fpsimd_save_fpexc32(vcpu);
342 
344  /*
345  * This must come after restoring the host sysregs, since a non-VHE
346  * system may enable SPE here and make use of the TTBRs.
347  */
349 
350  if (pmu_switch_needed)
351  __pmu_switch_to_host(vcpu);
352 
353  /* Returning to host will clear PSR.I, remask PMR if needed */
354  if (system_uses_irq_prio_masking())
355  gic_write_pmr(GIC_PRIO_IRQOFF);
356 
357  host_ctxt->__hyp_running_vcpu = NULL;
358 
359  return exit_code;
360 }
void __kvm_adjust_pc(struct kvm_vcpu *vcpu)
Definition: exception.c:365
static __always_inline void __load_host_stage2(void)
Definition: mem_protect.h:86
void __debug_switch_to_guest(struct kvm_vcpu *vcpu)
Definition: debug-sr.c:92
void __debug_restore_host_buffers_nvhe(struct kvm_vcpu *vcpu)
Definition: debug-sr.c:97
void __debug_switch_to_host(struct kvm_vcpu *vcpu)
Definition: debug-sr.c:105
void __debug_save_host_buffers_nvhe(struct kvm_vcpu *vcpu)
Definition: debug-sr.c:82
static void __activate_traps(struct kvm_vcpu *vcpu)
Definition: switch.c:39
static void __hyp_vgic_restore_state(struct kvm_vcpu *vcpu)
Definition: switch.c:125
static void __hyp_vgic_save_state(struct kvm_vcpu *vcpu)
Definition: switch.c:116
#define __pmu_switch_to_host(v)
Definition: switch.c:165
#define __pmu_switch_to_guest(v)
Definition: switch.c:164
static void __deactivate_traps(struct kvm_vcpu *vcpu)
Definition: switch.c:84
void __sysreg_restore_state_nvhe(struct kvm_cpu_context *ctxt)
Definition: sysreg-sr.c:29
void __sysreg_save_state_nvhe(struct kvm_cpu_context *ctxt)
Definition: sysreg-sr.c:21
void __timer_disable_traps(struct kvm_vcpu *vcpu)
Definition: timer-sr.c:23
void __timer_enable_traps(struct kvm_vcpu *vcpu)
Definition: timer-sr.c:40
static bool fixup_guest_exit(struct kvm_vcpu *vcpu, u64 *exit_code)
Definition: switch.h:693
static void __fpsimd_save_fpexc32(struct kvm_vcpu *vcpu)
Definition: switch.h:49
static void __sysreg32_restore_state(struct kvm_vcpu *vcpu)
Definition: sysreg-sr.h:229
static void __sysreg32_save_state(struct kvm_vcpu *vcpu)
Definition: sysreg-sr.h:212
Here is the call graph for this function:
Here is the caller graph for this function:

◆ DEFINE_PER_CPU() [1/3]

DEFINE_PER_CPU ( struct kvm_cpu_context  ,
kvm_hyp_ctxt   
)

◆ DEFINE_PER_CPU() [2/3]

DEFINE_PER_CPU ( struct kvm_host_data  ,
kvm_host_data   
)

◆ DEFINE_PER_CPU() [3/3]

DEFINE_PER_CPU ( unsigned long  ,
kvm_hyp_vector   
)

◆ early_exit_filter()

static void early_exit_filter ( struct kvm_vcpu *  vcpu,
u64 *  exit_code 
)
static

Definition at line 229 of file switch.c.

230 {
231  struct kvm *kvm = kern_hyp_va(vcpu->kvm);
232 
233  if (kvm_vm_is_protected(kvm) && vcpu_mode_is_32bit(vcpu)) {
234  /*
235  * As we have caught the guest red-handed, decide that it isn't
236  * fit for purpose anymore by making the vcpu invalid. The VMM
237  * can try and fix it by re-initializing the vcpu with
238  * KVM_ARM_VCPU_INIT, however, this is likely not possible for
239  * protected VMs.
240  */
241  vcpu_clear_flag(vcpu, VCPU_INITIALIZED);
242  *exit_code &= BIT(ARM_EXIT_WITH_SERROR_BIT);
243  *exit_code |= ARM_EXCEPTION_IL;
244  }
245 }

◆ hyp_panic()

asmlinkage void __noreturn hyp_panic ( void  )

Definition at line 362 of file switch.c.

363 {
364  u64 spsr = read_sysreg_el2(SYS_SPSR);
365  u64 elr = read_sysreg_el2(SYS_ELR);
366  u64 par = read_sysreg_par();
367  struct kvm_cpu_context *host_ctxt;
368  struct kvm_vcpu *vcpu;
369 
370  host_ctxt = &this_cpu_ptr(&kvm_host_data)->host_ctxt;
371  vcpu = host_ctxt->__hyp_running_vcpu;
372 
373  if (vcpu) {
374  __timer_disable_traps(vcpu);
375  __deactivate_traps(vcpu);
377  __sysreg_restore_state_nvhe(host_ctxt);
378  }
379 
380  /* Prepare to dump kvm nvhe hyp stacktrace */
381  kvm_nvhe_prepare_backtrace((unsigned long)__builtin_frame_address(0),
382  _THIS_IP_);
383 
384  __hyp_do_panic(host_ctxt, spsr, elr, par);
385  unreachable();
386 }
void kvm_nvhe_prepare_backtrace(unsigned long fp, unsigned long pc)
Definition: stacktrace.c:152
Here is the call graph for this function:
Here is the caller graph for this function:

◆ hyp_panic_bad_stack()

asmlinkage void __noreturn hyp_panic_bad_stack ( void  )

Definition at line 388 of file switch.c.

389 {
390  hyp_panic();
391 }
asmlinkage void __noreturn hyp_panic(void)
Definition: switch.c:362
Here is the call graph for this function:

◆ kvm_get_exit_handler_array()

static const exit_handler_fn* kvm_get_exit_handler_array ( struct kvm_vcpu *  vcpu)
static

Definition at line 210 of file switch.c.

211 {
212  if (unlikely(kvm_vm_is_protected(kern_hyp_va(vcpu->kvm))))
213  return pvm_exit_handlers;
214 
215  return hyp_exit_handlers;
216 }
static const exit_handler_fn pvm_exit_handlers[]
Definition: switch.c:198
static const exit_handler_fn hyp_exit_handlers[]
Definition: switch.c:185

◆ kvm_handle_pvm_sys64()

static bool kvm_handle_pvm_sys64 ( struct kvm_vcpu *  vcpu,
u64 *  exit_code 
)
static

Definition at line 174 of file switch.c.

175 {
176  /*
177  * Make sure we handle the exit for workarounds and ptrauth
178  * before the pKVM handling, as the latter could decide to
179  * UNDEF.
180  */
181  return (kvm_hyp_handle_sysreg(vcpu, exit_code) ||
182  kvm_handle_pvm_sysreg(vcpu, exit_code));
183 }
bool kvm_handle_pvm_sysreg(struct kvm_vcpu *vcpu, u64 *exit_code)
Definition: sys_regs.c:474
static bool kvm_hyp_handle_sysreg(struct kvm_vcpu *vcpu, u64 *exit_code)
Definition: switch.h:573
Here is the call graph for this function:

◆ kvm_nvhe_prepare_backtrace()

void kvm_nvhe_prepare_backtrace ( unsigned long  fp,
unsigned long  pc 
)

Definition at line 152 of file stacktrace.c.

153 {
154  if (is_protected_kvm_enabled())
155  pkvm_save_backtrace(fp, pc);
156  else
157  hyp_prepare_backtrace(fp, pc);
158 }
static void pkvm_save_backtrace(unsigned long fp, unsigned long pc)
Definition: stacktrace.c:138
static void hyp_prepare_backtrace(unsigned long fp, unsigned long pc)
Definition: stacktrace.c:26
Here is the call graph for this function:
Here is the caller graph for this function:

◆ kvm_unexpected_el2_exception()

asmlinkage void kvm_unexpected_el2_exception ( void  )

Definition at line 393 of file switch.c.

394 {
396 }
static void __kvm_unexpected_el2_exception(void)
Definition: switch.h:748
Here is the call graph for this function:

Variable Documentation

◆ hyp_exit_handlers

const exit_handler_fn hyp_exit_handlers[]
static
Initial value:
= {
[0 ... ESR_ELx_EC_MAX] = NULL,
[ESR_ELx_EC_CP15_32] = kvm_hyp_handle_cp15_32,
[ESR_ELx_EC_SYS64] = kvm_hyp_handle_sysreg,
[ESR_ELx_EC_SVE] = kvm_hyp_handle_fpsimd,
[ESR_ELx_EC_FP_ASIMD] = kvm_hyp_handle_fpsimd,
[ESR_ELx_EC_IABT_LOW] = kvm_hyp_handle_iabt_low,
[ESR_ELx_EC_DABT_LOW] = kvm_hyp_handle_dabt_low,
[ESR_ELx_EC_WATCHPT_LOW] = kvm_hyp_handle_watchpt_low,
[ESR_ELx_EC_PAC] = kvm_hyp_handle_ptrauth,
[ESR_ELx_EC_MOPS] = kvm_hyp_handle_mops,
}
static bool kvm_hyp_handle_iabt_low(struct kvm_vcpu *vcpu, u64 *exit_code) __alias(kvm_hyp_handle_memory_fault)
static bool kvm_hyp_handle_fpsimd(struct kvm_vcpu *vcpu, u64 *exit_code)
Definition: switch.h:330
static bool kvm_hyp_handle_mops(struct kvm_vcpu *vcpu, u64 *exit_code)
Definition: switch.h:300
static bool kvm_hyp_handle_ptrauth(struct kvm_vcpu *vcpu, u64 *exit_code)
Definition: switch.h:476
static bool kvm_hyp_handle_dabt_low(struct kvm_vcpu *vcpu, u64 *exit_code)
Definition: switch.h:617
static bool kvm_hyp_handle_cp15_32(struct kvm_vcpu *vcpu, u64 *exit_code)
Definition: switch.h:596
static bool kvm_hyp_handle_watchpt_low(struct kvm_vcpu *vcpu, u64 *exit_code) __alias(kvm_hyp_handle_memory_fault)

Definition at line 185 of file switch.c.

◆ pvm_exit_handlers

const exit_handler_fn pvm_exit_handlers[]
static
Initial value:
= {
[0 ... ESR_ELx_EC_MAX] = NULL,
[ESR_ELx_EC_SYS64] = kvm_handle_pvm_sys64,
[ESR_ELx_EC_SVE] = kvm_handle_pvm_restricted,
[ESR_ELx_EC_FP_ASIMD] = kvm_hyp_handle_fpsimd,
[ESR_ELx_EC_IABT_LOW] = kvm_hyp_handle_iabt_low,
[ESR_ELx_EC_DABT_LOW] = kvm_hyp_handle_dabt_low,
[ESR_ELx_EC_WATCHPT_LOW] = kvm_hyp_handle_watchpt_low,
[ESR_ELx_EC_PAC] = kvm_hyp_handle_ptrauth,
[ESR_ELx_EC_MOPS] = kvm_hyp_handle_mops,
}
bool kvm_handle_pvm_restricted(struct kvm_vcpu *vcpu, u64 *exit_code)
Definition: sys_regs.c:512
static bool kvm_handle_pvm_sys64(struct kvm_vcpu *vcpu, u64 *exit_code)
Definition: switch.c:174

Definition at line 198 of file switch.c.