17c0e8b0cSWill Deacon #include "kvm/kvm.h" 27c0e8b0cSWill Deacon #include "kvm/kvm-cpu.h" 37c0e8b0cSWill Deacon 47c0e8b0cSWill Deacon static int debug_fd; 57c0e8b0cSWill Deacon 67c0e8b0cSWill Deacon void kvm_cpu__set_debug_fd(int fd) 77c0e8b0cSWill Deacon { 87c0e8b0cSWill Deacon debug_fd = fd; 97c0e8b0cSWill Deacon } 107c0e8b0cSWill Deacon 117c0e8b0cSWill Deacon int kvm_cpu__get_debug_fd(void) 127c0e8b0cSWill Deacon { 137c0e8b0cSWill Deacon return debug_fd; 147c0e8b0cSWill Deacon } 157c0e8b0cSWill Deacon 1685bd726aSAnup Patel static struct kvm_arm_target *kvm_arm_generic_target; 177c0e8b0cSWill Deacon static struct kvm_arm_target *kvm_arm_targets[KVM_ARM_NUM_TARGETS]; 1885bd726aSAnup Patel 1985bd726aSAnup Patel void kvm_cpu__set_kvm_arm_generic_target(struct kvm_arm_target *target) 2085bd726aSAnup Patel { 2185bd726aSAnup Patel kvm_arm_generic_target = target; 2285bd726aSAnup Patel } 2385bd726aSAnup Patel 247c0e8b0cSWill Deacon int kvm_cpu__register_kvm_arm_target(struct kvm_arm_target *target) 257c0e8b0cSWill Deacon { 267c0e8b0cSWill Deacon unsigned int i = 0; 277c0e8b0cSWill Deacon 287c0e8b0cSWill Deacon for (i = 0; i < ARRAY_SIZE(kvm_arm_targets); ++i) { 297c0e8b0cSWill Deacon if (!kvm_arm_targets[i]) { 307c0e8b0cSWill Deacon kvm_arm_targets[i] = target; 317c0e8b0cSWill Deacon return 0; 327c0e8b0cSWill Deacon } 337c0e8b0cSWill Deacon } 347c0e8b0cSWill Deacon 357c0e8b0cSWill Deacon return -ENOSPC; 367c0e8b0cSWill Deacon } 377c0e8b0cSWill Deacon 387c0e8b0cSWill Deacon struct kvm_cpu *kvm_cpu__arch_init(struct kvm *kvm, unsigned long cpu_id) 397c0e8b0cSWill Deacon { 409b47146bSMarc Zyngier struct kvm_arm_target *target; 417c0e8b0cSWill Deacon struct kvm_cpu *vcpu; 427c0e8b0cSWill Deacon int coalesced_offset, mmap_size, err = -1; 437c0e8b0cSWill Deacon unsigned int i; 4485bd726aSAnup Patel struct kvm_vcpu_init preferred_init; 4561076240SWill Deacon struct kvm_vcpu_init vcpu_init = { 4661076240SWill Deacon .features = ARM_VCPU_FEATURE_FLAGS(kvm, cpu_id) 4761076240SWill Deacon }; 487c0e8b0cSWill Deacon 497c0e8b0cSWill Deacon vcpu = calloc(1, sizeof(struct kvm_cpu)); 507c0e8b0cSWill Deacon if (!vcpu) 517c0e8b0cSWill Deacon return NULL; 527c0e8b0cSWill Deacon 537c0e8b0cSWill Deacon vcpu->vcpu_fd = ioctl(kvm->vm_fd, KVM_CREATE_VCPU, cpu_id); 547c0e8b0cSWill Deacon if (vcpu->vcpu_fd < 0) 557c0e8b0cSWill Deacon die_perror("KVM_CREATE_VCPU ioctl"); 567c0e8b0cSWill Deacon 577c0e8b0cSWill Deacon mmap_size = ioctl(kvm->sys_fd, KVM_GET_VCPU_MMAP_SIZE, 0); 587c0e8b0cSWill Deacon if (mmap_size < 0) 597c0e8b0cSWill Deacon die_perror("KVM_GET_VCPU_MMAP_SIZE ioctl"); 607c0e8b0cSWill Deacon 617c0e8b0cSWill Deacon vcpu->kvm_run = mmap(NULL, mmap_size, PROT_RW, MAP_SHARED, 627c0e8b0cSWill Deacon vcpu->vcpu_fd, 0); 637c0e8b0cSWill Deacon if (vcpu->kvm_run == MAP_FAILED) 647c0e8b0cSWill Deacon die("unable to mmap vcpu fd"); 657c0e8b0cSWill Deacon 66c2dad402SAnup Patel /* Set KVM_ARM_VCPU_PSCI_0_2 if available */ 67c2dad402SAnup Patel if (kvm__supports_extension(kvm, KVM_CAP_ARM_PSCI_0_2)) { 68c2dad402SAnup Patel vcpu_init.features[0] |= (1UL << KVM_ARM_VCPU_PSCI_0_2); 69c2dad402SAnup Patel } 70c2dad402SAnup Patel 71*74c5e7b2SDave Martin kvm_cpu__select_features(kvm, &vcpu_init); 72254cb189SAmit Daniel Kachhap 7385bd726aSAnup Patel /* 7485bd726aSAnup Patel * If the preferred target ioctl is successful then 7585bd726aSAnup Patel * use preferred target else try each and every target type 7685bd726aSAnup Patel */ 7785bd726aSAnup Patel err = ioctl(kvm->vm_fd, KVM_ARM_PREFERRED_TARGET, &preferred_init); 7885bd726aSAnup Patel if (!err) { 7985bd726aSAnup Patel /* Match preferred target CPU type. */ 8085bd726aSAnup Patel target = NULL; 8185bd726aSAnup Patel for (i = 0; i < ARRAY_SIZE(kvm_arm_targets); ++i) { 8285bd726aSAnup Patel if (!kvm_arm_targets[i]) 8385bd726aSAnup Patel continue; 8485bd726aSAnup Patel if (kvm_arm_targets[i]->id == preferred_init.target) { 8585bd726aSAnup Patel target = kvm_arm_targets[i]; 8685bd726aSAnup Patel break; 8785bd726aSAnup Patel } 8885bd726aSAnup Patel } 8985bd726aSAnup Patel if (!target) { 9085bd726aSAnup Patel target = kvm_arm_generic_target; 9185bd726aSAnup Patel vcpu_init.target = preferred_init.target; 9285bd726aSAnup Patel } else { 9385bd726aSAnup Patel vcpu_init.target = target->id; 9485bd726aSAnup Patel } 9585bd726aSAnup Patel err = ioctl(vcpu->vcpu_fd, KVM_ARM_VCPU_INIT, &vcpu_init); 9685bd726aSAnup Patel } else { 977c0e8b0cSWill Deacon /* Find an appropriate target CPU type. */ 987c0e8b0cSWill Deacon for (i = 0; i < ARRAY_SIZE(kvm_arm_targets); ++i) { 998d0413d2SMarc Zyngier if (!kvm_arm_targets[i]) 1008d0413d2SMarc Zyngier continue; 1019b47146bSMarc Zyngier target = kvm_arm_targets[i]; 1029b47146bSMarc Zyngier vcpu_init.target = target->id; 1037c0e8b0cSWill Deacon err = ioctl(vcpu->vcpu_fd, KVM_ARM_VCPU_INIT, &vcpu_init); 1047c0e8b0cSWill Deacon if (!err) 1057c0e8b0cSWill Deacon break; 1067c0e8b0cSWill Deacon } 10785bd726aSAnup Patel if (err) 10885bd726aSAnup Patel die("Unable to find matching target"); 10985bd726aSAnup Patel } 1107c0e8b0cSWill Deacon 1119b47146bSMarc Zyngier if (err || target->init(vcpu)) 11285bd726aSAnup Patel die("Unable to initialise vcpu"); 1137c0e8b0cSWill Deacon 1147c0e8b0cSWill Deacon coalesced_offset = ioctl(kvm->sys_fd, KVM_CHECK_EXTENSION, 1157c0e8b0cSWill Deacon KVM_CAP_COALESCED_MMIO); 1167c0e8b0cSWill Deacon if (coalesced_offset) 1177c0e8b0cSWill Deacon vcpu->ring = (void *)vcpu->kvm_run + 1187c0e8b0cSWill Deacon (coalesced_offset * PAGE_SIZE); 1197c0e8b0cSWill Deacon 1207c0e8b0cSWill Deacon /* Populate the vcpu structure. */ 1217c0e8b0cSWill Deacon vcpu->kvm = kvm; 1227c0e8b0cSWill Deacon vcpu->cpu_id = cpu_id; 12385bd726aSAnup Patel vcpu->cpu_type = vcpu_init.target; 1249b47146bSMarc Zyngier vcpu->cpu_compatible = target->compatible; 1257c0e8b0cSWill Deacon vcpu->is_running = true; 12685bd726aSAnup Patel 1277c0e8b0cSWill Deacon return vcpu; 1287c0e8b0cSWill Deacon } 1297c0e8b0cSWill Deacon 1307c0e8b0cSWill Deacon void kvm_cpu__arch_nmi(struct kvm_cpu *cpu) 1317c0e8b0cSWill Deacon { 1327c0e8b0cSWill Deacon } 1337c0e8b0cSWill Deacon 1347c0e8b0cSWill Deacon void kvm_cpu__delete(struct kvm_cpu *vcpu) 1357c0e8b0cSWill Deacon { 1367c0e8b0cSWill Deacon free(vcpu); 1377c0e8b0cSWill Deacon } 1387c0e8b0cSWill Deacon 1397c0e8b0cSWill Deacon bool kvm_cpu__handle_exit(struct kvm_cpu *vcpu) 1407c0e8b0cSWill Deacon { 1417c0e8b0cSWill Deacon return false; 1427c0e8b0cSWill Deacon } 1437c0e8b0cSWill Deacon 1447c0e8b0cSWill Deacon void kvm_cpu__show_page_tables(struct kvm_cpu *vcpu) 1457c0e8b0cSWill Deacon { 1467c0e8b0cSWill Deacon } 147