2 * QEMU KVM support -- ARM specific functions.
4 * Copyright (c) 2012 Linaro Limited
6 * This work is licensed under the terms of the GNU GPL, version 2 or later.
7 * See the COPYING file in the top-level directory.
11 #ifndef QEMU_KVM_ARM_H
12 #define QEMU_KVM_ARM_H
14 #include "sysemu/kvm.h"
15 #include "exec/memory.h"
16 #include "qemu/error-report.h"
18 #define KVM_ARM_VGIC_V2 (1 << 0)
19 #define KVM_ARM_VGIC_V3 (1 << 1)
25 * Initialize (or reinitialize) the VCPU by invoking the
26 * KVM_ARM_VCPU_INIT ioctl with the CPU type and feature
27 * bitmask specified in the CPUState.
29 * Returns: 0 if success else < 0 error code
31 int kvm_arm_vcpu_init(CPUState
*cs
);
34 * kvm_arm_vcpu_finalize:
36 * @feature: feature to finalize
38 * Finalizes the configuration of the specified VCPU feature by
39 * invoking the KVM_ARM_VCPU_FINALIZE ioctl. Features requiring
40 * this are documented in the "KVM_ARM_VCPU_FINALIZE" section of
41 * KVM's API documentation.
43 * Returns: 0 if success else < 0 error code
45 int kvm_arm_vcpu_finalize(CPUState
*cs
, int feature
);
48 * kvm_arm_register_device:
49 * @mr: memory region for this device
50 * @devid: the KVM device ID
51 * @group: device control API group for setting addresses
52 * @attr: device control API address type
53 * @dev_fd: device control device file descriptor (or -1 if not supported)
54 * @addr_ormask: value to be OR'ed with resolved address
56 * Remember the memory region @mr, and when it is mapped by the
57 * machine model, tell the kernel that base address using the
58 * KVM_ARM_SET_DEVICE_ADDRESS ioctl or the newer device control API. @devid
59 * should be the ID of the device as defined by KVM_ARM_SET_DEVICE_ADDRESS or
60 * the arm-vgic device in the device control API.
61 * The machine model may map
62 * and unmap the device multiple times; the kernel will only be told the final
63 * address at the point where machine init is complete.
65 void kvm_arm_register_device(MemoryRegion
*mr
, uint64_t devid
, uint64_t group
,
66 uint64_t attr
, int dev_fd
, uint64_t addr_ormask
);
69 * kvm_arm_init_cpreg_list:
72 * Initialize the ARMCPU cpreg list according to the kernel's
73 * definition of what CPU registers it knows about (and throw away
74 * the previous TCG-created cpreg list).
76 * Returns: 0 if success, else < 0 error code
78 int kvm_arm_init_cpreg_list(ARMCPU
*cpu
);
81 * kvm_arm_reg_syncs_via_cpreg_list:
82 * @regidx: KVM register index
84 * Return true if this KVM register should be synchronized via the
85 * cpreg list of arbitrary system registers, false if it is synchronized
86 * by hand using code in kvm_arch_get/put_registers().
88 bool kvm_arm_reg_syncs_via_cpreg_list(uint64_t regidx
);
91 * kvm_arm_cpreg_level:
92 * @regidx: KVM register index
94 * Return the level of this coprocessor/system register. Return value is
95 * either KVM_PUT_RUNTIME_STATE, KVM_PUT_RESET_STATE, or KVM_PUT_FULL_STATE.
97 int kvm_arm_cpreg_level(uint64_t regidx
);
100 * write_list_to_kvmstate:
102 * @level: the state level to sync
104 * For each register listed in the ARMCPU cpreg_indexes list, write
105 * its value from the cpreg_values list into the kernel (via ioctl).
106 * This updates KVM's working data structures from TCG data or
107 * from incoming migration state.
109 * Returns: true if all register values were updated correctly,
110 * false if some register was unknown to the kernel or could not
111 * be written (eg constant register with the wrong value).
112 * Note that we do not stop early on failure -- we will attempt
113 * writing all registers in the list.
115 bool write_list_to_kvmstate(ARMCPU
*cpu
, int level
);
118 * write_kvmstate_to_list:
121 * For each register listed in the ARMCPU cpreg_indexes list, write
122 * its value from the kernel into the cpreg_values list. This is used to
123 * copy info from KVM's working data structures into TCG or
124 * for outbound migration.
126 * Returns: true if all register values were read correctly,
127 * false if some register was unknown or could not be read.
128 * Note that we do not stop early on failure -- we will attempt
129 * reading all registers in the list.
131 bool write_kvmstate_to_list(ARMCPU
*cpu
);
134 * kvm_arm_cpu_pre_save:
137 * Called after write_kvmstate_to_list() from cpu_pre_save() to update
138 * the cpreg list with KVM CPU state.
140 void kvm_arm_cpu_pre_save(ARMCPU
*cpu
);
143 * kvm_arm_cpu_post_load:
146 * Called from cpu_post_load() to update KVM CPU state from the cpreg list.
148 void kvm_arm_cpu_post_load(ARMCPU
*cpu
);
151 * kvm_arm_reset_vcpu:
154 * Called at reset time to kernel registers to their initial values.
156 void kvm_arm_reset_vcpu(ARMCPU
*cpu
);
159 * kvm_arm_init_serror_injection:
162 * Check whether KVM can set guest SError syndrome.
164 void kvm_arm_init_serror_injection(CPUState
*cs
);
167 * kvm_get_vcpu_events:
170 * Get VCPU related state from kvm.
172 * Returns: 0 if success else < 0 error code
174 int kvm_get_vcpu_events(ARMCPU
*cpu
);
177 * kvm_put_vcpu_events:
180 * Put VCPU related state to kvm.
182 * Returns: 0 if success else < 0 error code
184 int kvm_put_vcpu_events(ARMCPU
*cpu
);
188 * kvm_arm_create_scratch_host_vcpu:
189 * @cpus_to_try: array of QEMU_KVM_ARM_TARGET_* values (terminated with
190 * QEMU_KVM_ARM_TARGET_NONE) to try as fallback if the kernel does not
191 * know the PREFERRED_TARGET ioctl. Passing NULL is the same as passing
193 * @fdarray: filled in with kvmfd, vmfd, cpufd file descriptors in that order
194 * @init: filled in with the necessary values for creating a host
195 * vcpu. If NULL is provided, will not init the vCPU (though the cpufd
196 * will still be set up).
198 * Create a scratch vcpu in its own VM of the type preferred by the host
199 * kernel (as would be used for '-cpu host'), for purposes of probing it
202 * Returns: true on success (and fdarray and init are filled in),
203 * false on failure (and fdarray and init are not valid).
205 bool kvm_arm_create_scratch_host_vcpu(const uint32_t *cpus_to_try
,
207 struct kvm_vcpu_init
*init
);
210 * kvm_arm_destroy_scratch_host_vcpu:
211 * @fdarray: array of fds as set up by kvm_arm_create_scratch_host_vcpu
213 * Tear down the scratch vcpu created by kvm_arm_create_scratch_host_vcpu.
215 void kvm_arm_destroy_scratch_host_vcpu(int *fdarray
);
217 #define TYPE_ARM_HOST_CPU "host-" TYPE_ARM_CPU
220 * ARMHostCPUFeatures: information about the host CPU (identified
221 * by asking the host kernel)
223 typedef struct ARMHostCPUFeatures
{
224 ARMISARegisters isar
;
227 const char *dtb_compatible
;
228 } ARMHostCPUFeatures
;
231 * kvm_arm_get_host_cpu_features:
232 * @ahcf: ARMHostCPUClass to fill in
234 * Probe the capabilities of the host kernel's preferred CPU and fill
235 * in the ARMHostCPUClass struct accordingly.
237 * Returns true on success and false otherwise.
239 bool kvm_arm_get_host_cpu_features(ARMHostCPUFeatures
*ahcf
);
242 * kvm_arm_sve_get_vls:
244 * @map: bitmap to fill in
246 * Get all the SVE vector lengths supported by the KVM host, setting
247 * the bits corresponding to their length in quadwords minus one
248 * (vq - 1) in @map up to ARM_MAX_VQ.
250 void kvm_arm_sve_get_vls(CPUState
*cs
, unsigned long *map
);
253 * kvm_arm_set_cpu_features_from_host:
254 * @cpu: ARMCPU to set the features for
256 * Set up the ARMCPU struct fields up to match the information probed
259 void kvm_arm_set_cpu_features_from_host(ARMCPU
*cpu
);
262 * kvm_arm_add_vcpu_properties:
263 * @obj: The CPU object to add the properties to
265 * Add all KVM specific CPU properties to the CPU object. These
266 * are the CPU properties with "kvm-" prefixed names.
268 void kvm_arm_add_vcpu_properties(Object
*obj
);
271 * kvm_arm_steal_time_finalize:
272 * @cpu: ARMCPU for which to finalize kvm-steal-time
273 * @errp: Pointer to Error* for error propagation
275 * Validate the kvm-steal-time property selection and set its default
276 * based on KVM support and guest configuration.
278 void kvm_arm_steal_time_finalize(ARMCPU
*cpu
, Error
**errp
);
281 * kvm_arm_steal_time_supported:
283 * Returns: true if KVM can enable steal time reporting
284 * and false otherwise.
286 bool kvm_arm_steal_time_supported(void);
289 * kvm_arm_aarch32_supported:
291 * Returns: true if KVM can enable AArch32 mode
292 * and false otherwise.
294 bool kvm_arm_aarch32_supported(void);
297 * kvm_arm_pmu_supported:
299 * Returns: true if KVM can enable the PMU
300 * and false otherwise.
302 bool kvm_arm_pmu_supported(void);
305 * kvm_arm_sve_supported:
307 * Returns true if KVM can enable SVE and false otherwise.
309 bool kvm_arm_sve_supported(void);
312 * kvm_arm_get_max_vm_ipa_size:
313 * @ms: Machine state handle
314 * @fixed_ipa: True when the IPA limit is fixed at 40. This is the case
317 * Returns the number of bits in the IPA address space supported by KVM
319 int kvm_arm_get_max_vm_ipa_size(MachineState
*ms
, bool *fixed_ipa
);
322 * kvm_arm_sync_mpstate_to_kvm:
325 * If supported set the KVM MP_STATE based on QEMU's model.
327 * Returns 0 on success and -1 on failure.
329 int kvm_arm_sync_mpstate_to_kvm(ARMCPU
*cpu
);
332 * kvm_arm_sync_mpstate_to_qemu:
335 * If supported get the MP_STATE from KVM and store in QEMU's model.
337 * Returns 0 on success and aborts on failure.
339 int kvm_arm_sync_mpstate_to_qemu(ARMCPU
*cpu
);
342 * kvm_arm_get_virtual_time:
345 * Gets the VCPU's virtual counter and stores it in the KVM CPU state.
347 void kvm_arm_get_virtual_time(CPUState
*cs
);
350 * kvm_arm_put_virtual_time:
353 * Sets the VCPU's virtual counter to the value stored in the KVM CPU state.
355 void kvm_arm_put_virtual_time(CPUState
*cs
);
357 void kvm_arm_vm_state_change(void *opaque
, bool running
, RunState state
);
359 int kvm_arm_vgic_probe(void);
361 void kvm_arm_pmu_set_irq(CPUState
*cs
, int irq
);
362 void kvm_arm_pmu_init(CPUState
*cs
);
365 * kvm_arm_pvtime_init:
367 * @ipa: Per-vcpu guest physical base address of the pvtime structures
369 * Initializes PVTIME for the VCPU, setting the PVTIME IPA to @ipa.
371 void kvm_arm_pvtime_init(CPUState
*cs
, uint64_t ipa
);
373 int kvm_arm_set_irq(int cpu
, int irqtype
, int irq
, int level
);
378 * It's safe to call these functions without KVM support.
379 * They should either do nothing or return "not supported".
381 static inline bool kvm_arm_aarch32_supported(void)
386 static inline bool kvm_arm_pmu_supported(void)
391 static inline bool kvm_arm_sve_supported(void)
396 static inline bool kvm_arm_steal_time_supported(void)
402 * These functions should never actually be called without KVM support.
404 static inline void kvm_arm_set_cpu_features_from_host(ARMCPU
*cpu
)
406 g_assert_not_reached();
409 static inline void kvm_arm_add_vcpu_properties(Object
*obj
)
411 g_assert_not_reached();
414 static inline int kvm_arm_get_max_vm_ipa_size(MachineState
*ms
, bool *fixed_ipa
)
416 g_assert_not_reached();
419 static inline int kvm_arm_vgic_probe(void)
421 g_assert_not_reached();
424 static inline void kvm_arm_pmu_set_irq(CPUState
*cs
, int irq
)
426 g_assert_not_reached();
429 static inline void kvm_arm_pmu_init(CPUState
*cs
)
431 g_assert_not_reached();
434 static inline void kvm_arm_pvtime_init(CPUState
*cs
, uint64_t ipa
)
436 g_assert_not_reached();
439 static inline void kvm_arm_steal_time_finalize(ARMCPU
*cpu
, Error
**errp
)
441 g_assert_not_reached();
444 static inline void kvm_arm_sve_get_vls(CPUState
*cs
, unsigned long *map
)
446 g_assert_not_reached();
451 static inline const char *gic_class_name(void)
453 return kvm_irqchip_in_kernel() ? "kvm-arm-gic" : "arm_gic";
459 * Return name of GICv3 class to use depending on whether KVM acceleration is
460 * in use. May throw an error if the chosen implementation is not available.
462 * Returns: class name to use
464 static inline const char *gicv3_class_name(void)
466 if (kvm_irqchip_in_kernel()) {
467 return "kvm-arm-gicv3";
470 error_report("Userspace GICv3 is not supported with KVM");
478 * kvm_arm_handle_debug:
480 * @debug_exit: debug part of the KVM exit structure
482 * Returns: TRUE if the debug exception was handled.
484 bool kvm_arm_handle_debug(CPUState
*cs
, struct kvm_debug_exit_arch
*debug_exit
);
487 * kvm_arm_hw_debug_active:
490 * Return: TRUE if any hardware breakpoints in use.
492 bool kvm_arm_hw_debug_active(CPUState
*cs
);
495 * kvm_arm_copy_hw_debug_data:
496 * @ptr: kvm_guest_debug_arch structure
498 * Copy the architecture specific debug registers into the
499 * kvm_guest_debug ioctl structure.
501 struct kvm_guest_debug_arch
;
502 void kvm_arm_copy_hw_debug_data(struct kvm_guest_debug_arch
*ptr
);
505 * kvm_arm_verify_ext_dabt_pending:
508 * Verify the fault status code wrt the Ext DABT injection
510 * Returns: true if the fault status code is as expected, false otherwise
512 bool kvm_arm_verify_ext_dabt_pending(CPUState
*cs
);
517 * Return the ITS class name to use depending on whether KVM acceleration
518 * and KVM CAP_SIGNAL_MSI are supported
520 * Returns: class name to use or NULL
522 static inline const char *its_class_name(void)
524 if (kvm_irqchip_in_kernel()) {
525 /* KVM implementation requires this capability */
526 return kvm_direct_msi_enabled() ? "arm-its-kvm" : NULL
;
528 /* Software emulation is not implemented yet */