KVM
Macros | Functions | Variables
reset.c File Reference
#include <linux/errno.h>
#include <linux/kernel.h>
#include <linux/kvm_host.h>
#include <linux/kvm.h>
#include <linux/hw_breakpoint.h>
#include <linux/slab.h>
#include <linux/string.h>
#include <linux/types.h>
#include <kvm/arm_arch_timer.h>
#include <asm/cpufeature.h>
#include <asm/cputype.h>
#include <asm/fpsimd.h>
#include <asm/ptrace.h>
#include <asm/kvm_arm.h>
#include <asm/kvm_asm.h>
#include <asm/kvm_emulate.h>
#include <asm/kvm_mmu.h>
#include <asm/kvm_nested.h>
#include <asm/virt.h>
Include dependency graph for reset.c:

Go to the source code of this file.

Macros

#define VCPU_RESET_PSTATE_EL1
 
#define VCPU_RESET_PSTATE_EL2
 
#define VCPU_RESET_PSTATE_SVC
 

Functions

int __init kvm_arm_init_sve (void)
 
static void kvm_vcpu_enable_sve (struct kvm_vcpu *vcpu)
 
static int kvm_vcpu_finalize_sve (struct kvm_vcpu *vcpu)
 
int kvm_arm_vcpu_finalize (struct kvm_vcpu *vcpu, int feature)
 
bool kvm_arm_vcpu_is_finalized (struct kvm_vcpu *vcpu)
 
void kvm_arm_vcpu_destroy (struct kvm_vcpu *vcpu)
 
static void kvm_vcpu_reset_sve (struct kvm_vcpu *vcpu)
 
static void kvm_vcpu_enable_ptrauth (struct kvm_vcpu *vcpu)
 
void kvm_reset_vcpu (struct kvm_vcpu *vcpu)
 
u32 get_kvm_ipa_limit (void)
 
int __init kvm_set_ipa_limit (void)
 

Variables

static u32 __ro_after_init kvm_ipa_limit
 
unsigned int __ro_after_init kvm_sve_max_vl
 

Macro Definition Documentation

◆ VCPU_RESET_PSTATE_EL1

#define VCPU_RESET_PSTATE_EL1
Value:
(PSR_MODE_EL1h | PSR_A_BIT | PSR_I_BIT | \
PSR_F_BIT | PSR_D_BIT)

Definition at line 39 of file reset.c.

◆ VCPU_RESET_PSTATE_EL2

#define VCPU_RESET_PSTATE_EL2
Value:
(PSR_MODE_EL2h | PSR_A_BIT | PSR_I_BIT | \
PSR_F_BIT | PSR_D_BIT)

Definition at line 42 of file reset.c.

◆ VCPU_RESET_PSTATE_SVC

#define VCPU_RESET_PSTATE_SVC
Value:
(PSR_AA32_MODE_SVC | PSR_AA32_A_BIT | \
PSR_AA32_I_BIT | PSR_AA32_F_BIT)

Definition at line 45 of file reset.c.

Function Documentation

◆ get_kvm_ipa_limit()

u32 get_kvm_ipa_limit ( void  )

Definition at line 269 of file reset.c.

270 {
271  return kvm_ipa_limit;
272 }
static u32 __ro_after_init kvm_ipa_limit
Definition: reset.c:34
Here is the caller graph for this function:

◆ kvm_arm_init_sve()

int __init kvm_arm_init_sve ( void  )

Definition at line 50 of file reset.c.

51 {
52  if (system_supports_sve()) {
53  kvm_sve_max_vl = sve_max_virtualisable_vl();
54 
55  /*
56  * The get_sve_reg()/set_sve_reg() ioctl interface will need
57  * to be extended with multiple register slice support in
58  * order to support vector lengths greater than
59  * VL_ARCH_MAX:
60  */
61  if (WARN_ON(kvm_sve_max_vl > VL_ARCH_MAX))
62  kvm_sve_max_vl = VL_ARCH_MAX;
63 
64  /*
65  * Don't even try to make use of vector lengths that
66  * aren't available on all CPUs, for now:
67  */
68  if (kvm_sve_max_vl < sve_max_vl())
69  pr_warn("KVM: SVE vector length for guests limited to %u bytes\n",
71  }
72 
73  return 0;
74 }
unsigned int __ro_after_init kvm_sve_max_vl
Definition: reset.c:48
Here is the caller graph for this function:

◆ kvm_arm_vcpu_destroy()

void kvm_arm_vcpu_destroy ( struct kvm_vcpu *  vcpu)

Definition at line 150 of file reset.c.

151 {
152  void *sve_state = vcpu->arch.sve_state;
153 
155  kvm_unshare_hyp(vcpu, vcpu + 1);
156  if (sve_state)
157  kvm_unshare_hyp(sve_state, sve_state + vcpu_sve_state_size(vcpu));
158  kfree(sve_state);
159  kfree(vcpu->arch.ccsidr);
160 }
void kvm_vcpu_unshare_task_fp(struct kvm_vcpu *vcpu)
Definition: fpsimd.c:17
void kvm_unshare_hyp(void *from, void *to)
Definition: mmu.c:548
Here is the call graph for this function:
Here is the caller graph for this function:

◆ kvm_arm_vcpu_finalize()

int kvm_arm_vcpu_finalize ( struct kvm_vcpu *  vcpu,
int  feature 
)

Definition at line 126 of file reset.c.

127 {
128  switch (feature) {
129  case KVM_ARM_VCPU_SVE:
130  if (!vcpu_has_sve(vcpu))
131  return -EINVAL;
132 
133  if (kvm_arm_vcpu_sve_finalized(vcpu))
134  return -EPERM;
135 
136  return kvm_vcpu_finalize_sve(vcpu);
137  }
138 
139  return -EINVAL;
140 }
static int kvm_vcpu_finalize_sve(struct kvm_vcpu *vcpu)
Definition: reset.c:92
Here is the call graph for this function:
Here is the caller graph for this function:

◆ kvm_arm_vcpu_is_finalized()

bool kvm_arm_vcpu_is_finalized ( struct kvm_vcpu *  vcpu)

Definition at line 142 of file reset.c.

143 {
144  if (vcpu_has_sve(vcpu) && !kvm_arm_vcpu_sve_finalized(vcpu))
145  return false;
146 
147  return true;
148 }
Here is the caller graph for this function:

◆ kvm_reset_vcpu()

void kvm_reset_vcpu ( struct kvm_vcpu *  vcpu)

kvm_reset_vcpu - sets core registers and sys_regs to reset value @vcpu: The VCPU pointer

This function sets the registers on the virtual CPU struct to their architecturally defined reset values, except for registers whose reset is deferred until kvm_arm_vcpu_finalize().

Note: This function can be called from two paths: The KVM_ARM_VCPU_INIT ioctl or as part of handling a request issued by another VCPU in the PSCI handling code. In the first case, the VCPU will not be loaded, and in the second case the VCPU will be loaded. Because this function operates purely on the memory-backed values of system registers, we want to do a full put if we were loaded (handling a request) and load the values back at the end of the function. Otherwise we leave the state alone. In both cases, we disable preemption around the vcpu reset as we would otherwise race with preempt notifiers which also call put/load.

Definition at line 191 of file reset.c.

192 {
193  struct vcpu_reset_state reset_state;
194  bool loaded;
195  u32 pstate;
196 
197  spin_lock(&vcpu->arch.mp_state_lock);
198  reset_state = vcpu->arch.reset_state;
199  vcpu->arch.reset_state.reset = false;
200  spin_unlock(&vcpu->arch.mp_state_lock);
201 
202  /* Reset PMU outside of the non-preemptible section */
203  kvm_pmu_vcpu_reset(vcpu);
204 
205  preempt_disable();
206  loaded = (vcpu->cpu != -1);
207  if (loaded)
208  kvm_arch_vcpu_put(vcpu);
209 
210  if (!kvm_arm_vcpu_sve_finalized(vcpu)) {
211  if (vcpu_has_feature(vcpu, KVM_ARM_VCPU_SVE))
212  kvm_vcpu_enable_sve(vcpu);
213  } else {
214  kvm_vcpu_reset_sve(vcpu);
215  }
216 
217  if (vcpu_has_feature(vcpu, KVM_ARM_VCPU_PTRAUTH_ADDRESS) ||
218  vcpu_has_feature(vcpu, KVM_ARM_VCPU_PTRAUTH_GENERIC))
220 
221  if (vcpu_el1_is_32bit(vcpu))
222  pstate = VCPU_RESET_PSTATE_SVC;
223  else if (vcpu_has_nv(vcpu))
224  pstate = VCPU_RESET_PSTATE_EL2;
225  else
226  pstate = VCPU_RESET_PSTATE_EL1;
227 
228  /* Reset core registers */
229  memset(vcpu_gp_regs(vcpu), 0, sizeof(*vcpu_gp_regs(vcpu)));
230  memset(&vcpu->arch.ctxt.fp_regs, 0, sizeof(vcpu->arch.ctxt.fp_regs));
231  vcpu->arch.ctxt.spsr_abt = 0;
232  vcpu->arch.ctxt.spsr_und = 0;
233  vcpu->arch.ctxt.spsr_irq = 0;
234  vcpu->arch.ctxt.spsr_fiq = 0;
235  vcpu_gp_regs(vcpu)->pstate = pstate;
236 
237  /* Reset system registers */
238  kvm_reset_sys_regs(vcpu);
239 
240  /*
241  * Additional reset state handling that PSCI may have imposed on us.
242  * Must be done after all the sys_reg reset.
243  */
244  if (reset_state.reset) {
245  unsigned long target_pc = reset_state.pc;
246 
247  /* Gracefully handle Thumb2 entry point */
248  if (vcpu_mode_is_32bit(vcpu) && (target_pc & 1)) {
249  target_pc &= ~1UL;
250  vcpu_set_thumb(vcpu);
251  }
252 
253  /* Propagate caller endianness */
254  if (reset_state.be)
255  kvm_vcpu_set_be(vcpu);
256 
257  *vcpu_pc(vcpu) = target_pc;
258  vcpu_set_reg(vcpu, 0, reset_state.r0);
259  }
260 
261  /* Reset timer */
262  kvm_timer_vcpu_reset(vcpu);
263 
264  if (loaded)
265  kvm_arch_vcpu_load(vcpu, smp_processor_id());
266  preempt_enable();
267 }
void kvm_timer_vcpu_reset(struct kvm_vcpu *vcpu)
Definition: arch_timer.c:938
void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
Definition: arm.c:472
void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Definition: arm.c:426
void kvm_pmu_vcpu_reset(struct kvm_vcpu *vcpu)
Definition: pmu-emul.c:245
#define VCPU_RESET_PSTATE_EL1
Definition: reset.c:39
static void kvm_vcpu_enable_ptrauth(struct kvm_vcpu *vcpu)
Definition: reset.c:168
#define VCPU_RESET_PSTATE_EL2
Definition: reset.c:42
static void kvm_vcpu_reset_sve(struct kvm_vcpu *vcpu)
Definition: reset.c:162
static void kvm_vcpu_enable_sve(struct kvm_vcpu *vcpu)
Definition: reset.c:76
#define VCPU_RESET_PSTATE_SVC
Definition: reset.c:45
void kvm_reset_sys_regs(struct kvm_vcpu *vcpu)
Definition: sys_regs.c:3452
Here is the call graph for this function:
Here is the caller graph for this function:

◆ kvm_set_ipa_limit()

int __init kvm_set_ipa_limit ( void  )

Definition at line 274 of file reset.c.

275 {
276  unsigned int parange;
277  u64 mmfr0;
278 
279  mmfr0 = read_sanitised_ftr_reg(SYS_ID_AA64MMFR0_EL1);
280  parange = cpuid_feature_extract_unsigned_field(mmfr0,
281  ID_AA64MMFR0_EL1_PARANGE_SHIFT);
282  /*
283  * IPA size beyond 48 bits for 4K and 16K page size is only supported
284  * when LPA2 is available. So if we have LPA2, enable it, else cap to 48
285  * bits, in case it's reported as larger on the system.
286  */
287  if (!kvm_lpa2_is_enabled() && PAGE_SIZE != SZ_64K)
288  parange = min(parange, (unsigned int)ID_AA64MMFR0_EL1_PARANGE_48);
289 
290  /*
291  * Check with ARMv8.5-GTG that our PAGE_SIZE is supported at
292  * Stage-2. If not, things will stop very quickly.
293  */
294  switch (cpuid_feature_extract_unsigned_field(mmfr0, ID_AA64MMFR0_EL1_TGRAN_2_SHIFT)) {
295  case ID_AA64MMFR0_EL1_TGRAN_2_SUPPORTED_NONE:
296  kvm_err("PAGE_SIZE not supported at Stage-2, giving up\n");
297  return -EINVAL;
298  case ID_AA64MMFR0_EL1_TGRAN_2_SUPPORTED_DEFAULT:
299  kvm_debug("PAGE_SIZE supported at Stage-2 (default)\n");
300  break;
301  case ID_AA64MMFR0_EL1_TGRAN_2_SUPPORTED_MIN ... ID_AA64MMFR0_EL1_TGRAN_2_SUPPORTED_MAX:
302  kvm_debug("PAGE_SIZE supported at Stage-2 (advertised)\n");
303  break;
304  default:
305  kvm_err("Unsupported value for TGRAN_2, giving up\n");
306  return -EINVAL;
307  }
308 
309  kvm_ipa_limit = id_aa64mmfr0_parange_to_phys_shift(parange);
310  kvm_info("IPA Size Limit: %d bits%s\n", kvm_ipa_limit,
311  ((kvm_ipa_limit < KVM_PHYS_SHIFT) ?
312  " (Reduced IPA size, limited VM/VMM compatibility)" : ""));
313 
314  return 0;
315 }
Here is the caller graph for this function:

◆ kvm_vcpu_enable_ptrauth()

static void kvm_vcpu_enable_ptrauth ( struct kvm_vcpu *  vcpu)
static

Definition at line 168 of file reset.c.

169 {
170  vcpu_set_flag(vcpu, GUEST_HAS_PTRAUTH);
171 }
Here is the caller graph for this function:

◆ kvm_vcpu_enable_sve()

static void kvm_vcpu_enable_sve ( struct kvm_vcpu *  vcpu)
static

Definition at line 76 of file reset.c.

77 {
78  vcpu->arch.sve_max_vl = kvm_sve_max_vl;
79 
80  /*
81  * Userspace can still customize the vector lengths by writing
82  * KVM_REG_ARM64_SVE_VLS. Allocation is deferred until
83  * kvm_arm_vcpu_finalize(), which freezes the configuration.
84  */
85  vcpu_set_flag(vcpu, GUEST_HAS_SVE);
86 }
Here is the caller graph for this function:

◆ kvm_vcpu_finalize_sve()

static int kvm_vcpu_finalize_sve ( struct kvm_vcpu *  vcpu)
static

Definition at line 92 of file reset.c.

93 {
94  void *buf;
95  unsigned int vl;
96  size_t reg_sz;
97  int ret;
98 
99  vl = vcpu->arch.sve_max_vl;
100 
101  /*
102  * Responsibility for these properties is shared between
103  * kvm_arm_init_sve(), kvm_vcpu_enable_sve() and
104  * set_sve_vls(). Double-check here just to be sure:
105  */
106  if (WARN_ON(!sve_vl_valid(vl) || vl > sve_max_virtualisable_vl() ||
107  vl > VL_ARCH_MAX))
108  return -EIO;
109 
110  reg_sz = vcpu_sve_state_size(vcpu);
111  buf = kzalloc(reg_sz, GFP_KERNEL_ACCOUNT);
112  if (!buf)
113  return -ENOMEM;
114 
115  ret = kvm_share_hyp(buf, buf + reg_sz);
116  if (ret) {
117  kfree(buf);
118  return ret;
119  }
120 
121  vcpu->arch.sve_state = buf;
122  vcpu_set_flag(vcpu, VCPU_SVE_FINALIZED);
123  return 0;
124 }
int kvm_share_hyp(void *from, void *to)
Definition: mmu.c:516
Here is the call graph for this function:
Here is the caller graph for this function:

◆ kvm_vcpu_reset_sve()

static void kvm_vcpu_reset_sve ( struct kvm_vcpu *  vcpu)
static

Definition at line 162 of file reset.c.

163 {
164  if (vcpu_has_sve(vcpu))
165  memset(vcpu->arch.sve_state, 0, vcpu_sve_state_size(vcpu));
166 }
Here is the caller graph for this function:

Variable Documentation

◆ kvm_ipa_limit

u32 __ro_after_init kvm_ipa_limit
static

Definition at line 34 of file reset.c.

◆ kvm_sve_max_vl

unsigned int __ro_after_init kvm_sve_max_vl

Definition at line 48 of file reset.c.