1 /* 2 * QEMU KVM support -- ARM specific functions. 3 * 4 * Copyright (c) 2012 Linaro Limited 5 * 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. 8 * 9 */ 10 11 #ifndef QEMU_KVM_ARM_H 12 #define QEMU_KVM_ARM_H 13 14 #include "sysemu/kvm.h" 15 #include "exec/memory.h" 16 #include "qemu/error-report.h" 17 18 #define KVM_ARM_VGIC_V2 (1 << 0) 19 #define KVM_ARM_VGIC_V3 (1 << 1) 20 21 /** 22 * kvm_arm_init_debug() - initialize guest debug capabilities 23 * @s: KVMState 24 * 25 * Should be called only once before using guest debug capabilities. 26 */ 27 void kvm_arm_init_debug(KVMState *s); 28 29 /** 30 * kvm_arm_vcpu_init: 31 * @cs: CPUState 32 * 33 * Initialize (or reinitialize) the VCPU by invoking the 34 * KVM_ARM_VCPU_INIT ioctl with the CPU type and feature 35 * bitmask specified in the CPUState. 36 * 37 * Returns: 0 if success else < 0 error code 38 */ 39 int kvm_arm_vcpu_init(CPUState *cs); 40 41 /** 42 * kvm_arm_vcpu_finalize: 43 * @cs: CPUState 44 * @feature: feature to finalize 45 * 46 * Finalizes the configuration of the specified VCPU feature by 47 * invoking the KVM_ARM_VCPU_FINALIZE ioctl. Features requiring 48 * this are documented in the "KVM_ARM_VCPU_FINALIZE" section of 49 * KVM's API documentation. 50 * 51 * Returns: 0 if success else < 0 error code 52 */ 53 int kvm_arm_vcpu_finalize(CPUState *cs, int feature); 54 55 /** 56 * kvm_arm_register_device: 57 * @mr: memory region for this device 58 * @devid: the KVM device ID 59 * @group: device control API group for setting addresses 60 * @attr: device control API address type 61 * @dev_fd: device control device file descriptor (or -1 if not supported) 62 * @addr_ormask: value to be OR'ed with resolved address 63 * 64 * Remember the memory region @mr, and when it is mapped by the 65 * machine model, tell the kernel that base address using the 66 * KVM_ARM_SET_DEVICE_ADDRESS ioctl or the newer device control API. @devid 67 * should be the ID of the device as defined by KVM_ARM_SET_DEVICE_ADDRESS or 68 * the arm-vgic device in the device control API. 69 * The machine model may map 70 * and unmap the device multiple times; the kernel will only be told the final 71 * address at the point where machine init is complete. 72 */ 73 void kvm_arm_register_device(MemoryRegion *mr, uint64_t devid, uint64_t group, 74 uint64_t attr, int dev_fd, uint64_t addr_ormask); 75 76 /** 77 * kvm_arm_init_cpreg_list: 78 * @cpu: ARMCPU 79 * 80 * Initialize the ARMCPU cpreg list according to the kernel's 81 * definition of what CPU registers it knows about (and throw away 82 * the previous TCG-created cpreg list). 83 * 84 * Returns: 0 if success, else < 0 error code 85 */ 86 int kvm_arm_init_cpreg_list(ARMCPU *cpu); 87 88 /** 89 * kvm_arm_reg_syncs_via_cpreg_list: 90 * @regidx: KVM register index 91 * 92 * Return true if this KVM register should be synchronized via the 93 * cpreg list of arbitrary system registers, false if it is synchronized 94 * by hand using code in kvm_arch_get/put_registers(). 95 */ 96 bool kvm_arm_reg_syncs_via_cpreg_list(uint64_t regidx); 97 98 /** 99 * kvm_arm_cpreg_level: 100 * @regidx: KVM register index 101 * 102 * Return the level of this coprocessor/system register. Return value is 103 * either KVM_PUT_RUNTIME_STATE, KVM_PUT_RESET_STATE, or KVM_PUT_FULL_STATE. 104 */ 105 int kvm_arm_cpreg_level(uint64_t regidx); 106 107 /** 108 * write_list_to_kvmstate: 109 * @cpu: ARMCPU 110 * @level: the state level to sync 111 * 112 * For each register listed in the ARMCPU cpreg_indexes list, write 113 * its value from the cpreg_values list into the kernel (via ioctl). 114 * This updates KVM's working data structures from TCG data or 115 * from incoming migration state. 116 * 117 * Returns: true if all register values were updated correctly, 118 * false if some register was unknown to the kernel or could not 119 * be written (eg constant register with the wrong value). 120 * Note that we do not stop early on failure -- we will attempt 121 * writing all registers in the list. 122 */ 123 bool write_list_to_kvmstate(ARMCPU *cpu, int level); 124 125 /** 126 * write_kvmstate_to_list: 127 * @cpu: ARMCPU 128 * 129 * For each register listed in the ARMCPU cpreg_indexes list, write 130 * its value from the kernel into the cpreg_values list. This is used to 131 * copy info from KVM's working data structures into TCG or 132 * for outbound migration. 133 * 134 * Returns: true if all register values were read correctly, 135 * false if some register was unknown or could not be read. 136 * Note that we do not stop early on failure -- we will attempt 137 * reading all registers in the list. 138 */ 139 bool write_kvmstate_to_list(ARMCPU *cpu); 140 141 /** 142 * kvm_arm_cpu_pre_save: 143 * @cpu: ARMCPU 144 * 145 * Called after write_kvmstate_to_list() from cpu_pre_save() to update 146 * the cpreg list with KVM CPU state. 147 */ 148 void kvm_arm_cpu_pre_save(ARMCPU *cpu); 149 150 /** 151 * kvm_arm_cpu_post_load: 152 * @cpu: ARMCPU 153 * 154 * Called from cpu_post_load() to update KVM CPU state from the cpreg list. 155 */ 156 void kvm_arm_cpu_post_load(ARMCPU *cpu); 157 158 /** 159 * kvm_arm_reset_vcpu: 160 * @cpu: ARMCPU 161 * 162 * Called at reset time to kernel registers to their initial values. 163 */ 164 void kvm_arm_reset_vcpu(ARMCPU *cpu); 165 166 /** 167 * kvm_arm_init_serror_injection: 168 * @cs: CPUState 169 * 170 * Check whether KVM can set guest SError syndrome. 171 */ 172 void kvm_arm_init_serror_injection(CPUState *cs); 173 174 /** 175 * kvm_get_vcpu_events: 176 * @cpu: ARMCPU 177 * 178 * Get VCPU related state from kvm. 179 * 180 * Returns: 0 if success else < 0 error code 181 */ 182 int kvm_get_vcpu_events(ARMCPU *cpu); 183 184 /** 185 * kvm_put_vcpu_events: 186 * @cpu: ARMCPU 187 * 188 * Put VCPU related state to kvm. 189 * 190 * Returns: 0 if success else < 0 error code 191 */ 192 int kvm_put_vcpu_events(ARMCPU *cpu); 193 194 #ifdef CONFIG_KVM 195 /** 196 * kvm_arm_create_scratch_host_vcpu: 197 * @cpus_to_try: array of QEMU_KVM_ARM_TARGET_* values (terminated with 198 * QEMU_KVM_ARM_TARGET_NONE) to try as fallback if the kernel does not 199 * know the PREFERRED_TARGET ioctl. Passing NULL is the same as passing 200 * an empty array. 201 * @fdarray: filled in with kvmfd, vmfd, cpufd file descriptors in that order 202 * @init: filled in with the necessary values for creating a host 203 * vcpu. If NULL is provided, will not init the vCPU (though the cpufd 204 * will still be set up). 205 * 206 * Create a scratch vcpu in its own VM of the type preferred by the host 207 * kernel (as would be used for '-cpu host'), for purposes of probing it 208 * for capabilities. 209 * 210 * Returns: true on success (and fdarray and init are filled in), 211 * false on failure (and fdarray and init are not valid). 212 */ 213 bool kvm_arm_create_scratch_host_vcpu(const uint32_t *cpus_to_try, 214 int *fdarray, 215 struct kvm_vcpu_init *init); 216 217 /** 218 * kvm_arm_destroy_scratch_host_vcpu: 219 * @fdarray: array of fds as set up by kvm_arm_create_scratch_host_vcpu 220 * 221 * Tear down the scratch vcpu created by kvm_arm_create_scratch_host_vcpu. 222 */ 223 void kvm_arm_destroy_scratch_host_vcpu(int *fdarray); 224 225 /** 226 * ARMHostCPUFeatures: information about the host CPU (identified 227 * by asking the host kernel) 228 */ 229 typedef struct ARMHostCPUFeatures { 230 ARMISARegisters isar; 231 uint64_t features; 232 uint32_t target; 233 const char *dtb_compatible; 234 } ARMHostCPUFeatures; 235 236 /** 237 * kvm_arm_get_host_cpu_features: 238 * @ahcf: ARMHostCPUClass to fill in 239 * 240 * Probe the capabilities of the host kernel's preferred CPU and fill 241 * in the ARMHostCPUClass struct accordingly. 242 * 243 * Returns true on success and false otherwise. 244 */ 245 bool kvm_arm_get_host_cpu_features(ARMHostCPUFeatures *ahcf); 246 247 /** 248 * kvm_arm_sve_get_vls: 249 * @cs: CPUState 250 * 251 * Get all the SVE vector lengths supported by the KVM host, setting 252 * the bits corresponding to their length in quadwords minus one 253 * (vq - 1) up to ARM_MAX_VQ. Return the resulting map. 254 */ 255 uint32_t kvm_arm_sve_get_vls(CPUState *cs); 256 257 /** 258 * kvm_arm_set_cpu_features_from_host: 259 * @cpu: ARMCPU to set the features for 260 * 261 * Set up the ARMCPU struct fields up to match the information probed 262 * from the host CPU. 263 */ 264 void kvm_arm_set_cpu_features_from_host(ARMCPU *cpu); 265 266 /** 267 * kvm_arm_add_vcpu_properties: 268 * @obj: The CPU object to add the properties to 269 * 270 * Add all KVM specific CPU properties to the CPU object. These 271 * are the CPU properties with "kvm-" prefixed names. 272 */ 273 void kvm_arm_add_vcpu_properties(Object *obj); 274 275 /** 276 * kvm_arm_steal_time_finalize: 277 * @cpu: ARMCPU for which to finalize kvm-steal-time 278 * @errp: Pointer to Error* for error propagation 279 * 280 * Validate the kvm-steal-time property selection and set its default 281 * based on KVM support and guest configuration. 282 */ 283 void kvm_arm_steal_time_finalize(ARMCPU *cpu, Error **errp); 284 285 /** 286 * kvm_arm_steal_time_supported: 287 * 288 * Returns: true if KVM can enable steal time reporting 289 * and false otherwise. 290 */ 291 bool kvm_arm_steal_time_supported(void); 292 293 /** 294 * kvm_arm_aarch32_supported: 295 * 296 * Returns: true if KVM can enable AArch32 mode 297 * and false otherwise. 298 */ 299 bool kvm_arm_aarch32_supported(void); 300 301 /** 302 * kvm_arm_pmu_supported: 303 * 304 * Returns: true if KVM can enable the PMU 305 * and false otherwise. 306 */ 307 bool kvm_arm_pmu_supported(void); 308 309 /** 310 * kvm_arm_sve_supported: 311 * 312 * Returns true if KVM can enable SVE and false otherwise. 313 */ 314 bool kvm_arm_sve_supported(void); 315 316 /** 317 * kvm_arm_get_max_vm_ipa_size: 318 * @ms: Machine state handle 319 * @fixed_ipa: True when the IPA limit is fixed at 40. This is the case 320 * for legacy KVM. 321 * 322 * Returns the number of bits in the IPA address space supported by KVM 323 */ 324 int kvm_arm_get_max_vm_ipa_size(MachineState *ms, bool *fixed_ipa); 325 326 /** 327 * kvm_arm_sync_mpstate_to_kvm: 328 * @cpu: ARMCPU 329 * 330 * If supported set the KVM MP_STATE based on QEMU's model. 331 * 332 * Returns 0 on success and -1 on failure. 333 */ 334 int kvm_arm_sync_mpstate_to_kvm(ARMCPU *cpu); 335 336 /** 337 * kvm_arm_sync_mpstate_to_qemu: 338 * @cpu: ARMCPU 339 * 340 * If supported get the MP_STATE from KVM and store in QEMU's model. 341 * 342 * Returns 0 on success and aborts on failure. 343 */ 344 int kvm_arm_sync_mpstate_to_qemu(ARMCPU *cpu); 345 346 /** 347 * kvm_arm_get_virtual_time: 348 * @cs: CPUState 349 * 350 * Gets the VCPU's virtual counter and stores it in the KVM CPU state. 351 */ 352 void kvm_arm_get_virtual_time(CPUState *cs); 353 354 /** 355 * kvm_arm_put_virtual_time: 356 * @cs: CPUState 357 * 358 * Sets the VCPU's virtual counter to the value stored in the KVM CPU state. 359 */ 360 void kvm_arm_put_virtual_time(CPUState *cs); 361 362 void kvm_arm_vm_state_change(void *opaque, bool running, RunState state); 363 364 int kvm_arm_vgic_probe(void); 365 366 void kvm_arm_pmu_set_irq(CPUState *cs, int irq); 367 void kvm_arm_pmu_init(CPUState *cs); 368 369 /** 370 * kvm_arm_pvtime_init: 371 * @cs: CPUState 372 * @ipa: Per-vcpu guest physical base address of the pvtime structures 373 * 374 * Initializes PVTIME for the VCPU, setting the PVTIME IPA to @ipa. 375 */ 376 void kvm_arm_pvtime_init(CPUState *cs, uint64_t ipa); 377 378 int kvm_arm_set_irq(int cpu, int irqtype, int irq, int level); 379 380 #else 381 382 /* 383 * It's safe to call these functions without KVM support. 384 * They should either do nothing or return "not supported". 385 */ 386 static inline bool kvm_arm_aarch32_supported(void) 387 { 388 return false; 389 } 390 391 static inline bool kvm_arm_pmu_supported(void) 392 { 393 return false; 394 } 395 396 static inline bool kvm_arm_sve_supported(void) 397 { 398 return false; 399 } 400 401 static inline bool kvm_arm_steal_time_supported(void) 402 { 403 return false; 404 } 405 406 /* 407 * These functions should never actually be called without KVM support. 408 */ 409 static inline void kvm_arm_set_cpu_features_from_host(ARMCPU *cpu) 410 { 411 g_assert_not_reached(); 412 } 413 414 static inline void kvm_arm_add_vcpu_properties(Object *obj) 415 { 416 g_assert_not_reached(); 417 } 418 419 static inline int kvm_arm_get_max_vm_ipa_size(MachineState *ms, bool *fixed_ipa) 420 { 421 g_assert_not_reached(); 422 } 423 424 static inline int kvm_arm_vgic_probe(void) 425 { 426 g_assert_not_reached(); 427 } 428 429 static inline void kvm_arm_pmu_set_irq(CPUState *cs, int irq) 430 { 431 g_assert_not_reached(); 432 } 433 434 static inline void kvm_arm_pmu_init(CPUState *cs) 435 { 436 g_assert_not_reached(); 437 } 438 439 static inline void kvm_arm_pvtime_init(CPUState *cs, uint64_t ipa) 440 { 441 g_assert_not_reached(); 442 } 443 444 static inline void kvm_arm_steal_time_finalize(ARMCPU *cpu, Error **errp) 445 { 446 g_assert_not_reached(); 447 } 448 449 static inline uint32_t kvm_arm_sve_get_vls(CPUState *cs) 450 { 451 g_assert_not_reached(); 452 } 453 454 #endif 455 456 /** 457 * kvm_arm_handle_debug: 458 * @cs: CPUState 459 * @debug_exit: debug part of the KVM exit structure 460 * 461 * Returns: TRUE if the debug exception was handled. 462 */ 463 bool kvm_arm_handle_debug(CPUState *cs, struct kvm_debug_exit_arch *debug_exit); 464 465 /** 466 * kvm_arm_hw_debug_active: 467 * @cs: CPU State 468 * 469 * Return: TRUE if any hardware breakpoints in use. 470 */ 471 bool kvm_arm_hw_debug_active(CPUState *cs); 472 473 /** 474 * kvm_arm_copy_hw_debug_data: 475 * @ptr: kvm_guest_debug_arch structure 476 * 477 * Copy the architecture specific debug registers into the 478 * kvm_guest_debug ioctl structure. 479 */ 480 struct kvm_guest_debug_arch; 481 void kvm_arm_copy_hw_debug_data(struct kvm_guest_debug_arch *ptr); 482 483 /** 484 * kvm_arm_verify_ext_dabt_pending: 485 * @cs: CPUState 486 * 487 * Verify the fault status code wrt the Ext DABT injection 488 * 489 * Returns: true if the fault status code is as expected, false otherwise 490 */ 491 bool kvm_arm_verify_ext_dabt_pending(CPUState *cs); 492 493 #endif 494