1 #include "kvm/kvm.h" 2 #include "kvm/kvm-cpu.h" 3 4 static int debug_fd; 5 6 void kvm_cpu__set_debug_fd(int fd) 7 { 8 debug_fd = fd; 9 } 10 11 int kvm_cpu__get_debug_fd(void) 12 { 13 return debug_fd; 14 } 15 16 static struct kvm_arm_target *kvm_arm_generic_target; 17 static struct kvm_arm_target *kvm_arm_targets[KVM_ARM_NUM_TARGETS]; 18 19 void kvm_cpu__set_kvm_arm_generic_target(struct kvm_arm_target *target) 20 { 21 kvm_arm_generic_target = target; 22 } 23 24 int kvm_cpu__register_kvm_arm_target(struct kvm_arm_target *target) 25 { 26 unsigned int i = 0; 27 28 for (i = 0; i < ARRAY_SIZE(kvm_arm_targets); ++i) { 29 if (!kvm_arm_targets[i]) { 30 kvm_arm_targets[i] = target; 31 return 0; 32 } 33 } 34 35 return -ENOSPC; 36 } 37 38 struct kvm_cpu *kvm_cpu__arch_init(struct kvm *kvm, unsigned long cpu_id) 39 { 40 struct kvm_arm_target *target; 41 struct kvm_cpu *vcpu; 42 int coalesced_offset, mmap_size, err = -1; 43 unsigned int i; 44 struct kvm_vcpu_init preferred_init; 45 struct kvm_vcpu_init vcpu_init = { 46 .features = {}, 47 }; 48 49 vcpu = calloc(1, sizeof(struct kvm_cpu)); 50 if (!vcpu) 51 return NULL; 52 53 vcpu->vcpu_fd = ioctl(kvm->vm_fd, KVM_CREATE_VCPU, cpu_id); 54 if (vcpu->vcpu_fd < 0) 55 die_perror("KVM_CREATE_VCPU ioctl"); 56 57 mmap_size = ioctl(kvm->sys_fd, KVM_GET_VCPU_MMAP_SIZE, 0); 58 if (mmap_size < 0) 59 die_perror("KVM_GET_VCPU_MMAP_SIZE ioctl"); 60 61 vcpu->kvm_run = mmap(NULL, mmap_size, PROT_RW, MAP_SHARED, 62 vcpu->vcpu_fd, 0); 63 if (vcpu->kvm_run == MAP_FAILED) 64 die("unable to mmap vcpu fd"); 65 66 /* VCPU 0 is the boot CPU, the others start in a poweroff state. */ 67 if (cpu_id > 0) 68 vcpu_init.features[0] |= (1UL << KVM_ARM_VCPU_POWER_OFF); 69 70 /* Set KVM_ARM_VCPU_PSCI_0_2 if available */ 71 if (kvm__supports_extension(kvm, KVM_CAP_ARM_PSCI_0_2)) { 72 vcpu_init.features[0] |= (1UL << KVM_ARM_VCPU_PSCI_0_2); 73 } 74 75 kvm_cpu__select_features(kvm, &vcpu_init); 76 77 /* 78 * If the preferred target ioctl is successful then 79 * use preferred target else try each and every target type 80 */ 81 err = ioctl(kvm->vm_fd, KVM_ARM_PREFERRED_TARGET, &preferred_init); 82 if (!err) { 83 /* Match preferred target CPU type. */ 84 target = NULL; 85 for (i = 0; i < ARRAY_SIZE(kvm_arm_targets); ++i) { 86 if (!kvm_arm_targets[i]) 87 continue; 88 if (kvm_arm_targets[i]->id == preferred_init.target) { 89 target = kvm_arm_targets[i]; 90 break; 91 } 92 } 93 if (!target) { 94 target = kvm_arm_generic_target; 95 vcpu_init.target = preferred_init.target; 96 } else { 97 vcpu_init.target = target->id; 98 } 99 err = ioctl(vcpu->vcpu_fd, KVM_ARM_VCPU_INIT, &vcpu_init); 100 } else { 101 /* Find an appropriate target CPU type. */ 102 for (i = 0; i < ARRAY_SIZE(kvm_arm_targets); ++i) { 103 if (!kvm_arm_targets[i]) 104 continue; 105 target = kvm_arm_targets[i]; 106 vcpu_init.target = target->id; 107 err = ioctl(vcpu->vcpu_fd, KVM_ARM_VCPU_INIT, &vcpu_init); 108 if (!err) 109 break; 110 } 111 if (err) 112 die("Unable to find matching target"); 113 } 114 115 /* Populate the vcpu structure. */ 116 vcpu->kvm = kvm; 117 vcpu->cpu_id = cpu_id; 118 vcpu->cpu_type = vcpu_init.target; 119 vcpu->cpu_compatible = target->compatible; 120 vcpu->is_running = true; 121 122 if (err || target->init(vcpu)) 123 die("Unable to initialise vcpu"); 124 125 coalesced_offset = ioctl(kvm->sys_fd, KVM_CHECK_EXTENSION, 126 KVM_CAP_COALESCED_MMIO); 127 if (coalesced_offset) 128 vcpu->ring = (void *)vcpu->kvm_run + 129 (coalesced_offset * PAGE_SIZE); 130 131 if (kvm_cpu__configure_features(vcpu)) 132 die("Unable to configure requested vcpu features"); 133 134 return vcpu; 135 } 136 137 void kvm_cpu__arch_nmi(struct kvm_cpu *cpu) 138 { 139 } 140 141 void kvm_cpu__delete(struct kvm_cpu *vcpu) 142 { 143 kvm_cpu__teardown_pvtime(vcpu->kvm); 144 free(vcpu); 145 } 146 147 bool kvm_cpu__handle_exit(struct kvm_cpu *vcpu) 148 { 149 return false; 150 } 151 152 void kvm_cpu__show_page_tables(struct kvm_cpu *vcpu) 153 { 154 } 155