11e0c135aSWill Deacon #include "kvm/kvm-cpu.h" 21e0c135aSWill Deacon #include "kvm/kvm.h" 31e0c135aSWill Deacon 41e0c135aSWill Deacon #include <asm/ptrace.h> 51e0c135aSWill Deacon 61e0c135aSWill Deacon #define COMPAT_PSR_F_BIT 0x00000040 71e0c135aSWill Deacon #define COMPAT_PSR_I_BIT 0x00000080 81e0c135aSWill Deacon #define COMPAT_PSR_MODE_SVC 0x00000013 91e0c135aSWill Deacon 101e0c135aSWill Deacon #define ARM64_CORE_REG(x) (KVM_REG_ARM64 | KVM_REG_SIZE_U64 | \ 111e0c135aSWill Deacon KVM_REG_ARM_CORE | KVM_REG_ARM_CORE_REG(x)) 121e0c135aSWill Deacon 131e0c135aSWill Deacon static void reset_vcpu_aarch32(struct kvm_cpu *vcpu) 141e0c135aSWill Deacon { 151e0c135aSWill Deacon struct kvm *kvm = vcpu->kvm; 161e0c135aSWill Deacon struct kvm_one_reg reg; 171e0c135aSWill Deacon u64 data; 181e0c135aSWill Deacon 191e0c135aSWill Deacon reg.addr = (u64)&data; 201e0c135aSWill Deacon 211e0c135aSWill Deacon /* pstate = all interrupts masked */ 221e0c135aSWill Deacon data = COMPAT_PSR_I_BIT | COMPAT_PSR_F_BIT | COMPAT_PSR_MODE_SVC; 231e0c135aSWill Deacon reg.id = ARM64_CORE_REG(regs.pstate); 241e0c135aSWill Deacon if (ioctl(vcpu->vcpu_fd, KVM_SET_ONE_REG, ®) < 0) 251e0c135aSWill Deacon die_perror("KVM_SET_ONE_REG failed (spsr[EL1])"); 261e0c135aSWill Deacon 271e0c135aSWill Deacon /* Secondary cores are stopped awaiting PSCI wakeup */ 281e0c135aSWill Deacon if (vcpu->cpu_id != 0) 291e0c135aSWill Deacon return; 301e0c135aSWill Deacon 311e0c135aSWill Deacon /* r0 = 0 */ 321e0c135aSWill Deacon data = 0; 331e0c135aSWill Deacon reg.id = ARM64_CORE_REG(regs.regs[0]); 341e0c135aSWill Deacon if (ioctl(vcpu->vcpu_fd, KVM_SET_ONE_REG, ®) < 0) 351e0c135aSWill Deacon die_perror("KVM_SET_ONE_REG failed (r0)"); 361e0c135aSWill Deacon 371e0c135aSWill Deacon /* r1 = machine type (-1) */ 381e0c135aSWill Deacon data = -1; 391e0c135aSWill Deacon reg.id = ARM64_CORE_REG(regs.regs[1]); 401e0c135aSWill Deacon if (ioctl(vcpu->vcpu_fd, KVM_SET_ONE_REG, ®) < 0) 411e0c135aSWill Deacon die_perror("KVM_SET_ONE_REG failed (r1)"); 421e0c135aSWill Deacon 431e0c135aSWill Deacon /* r2 = physical address of the device tree blob */ 441e0c135aSWill Deacon data = kvm->arch.dtb_guest_start; 451e0c135aSWill Deacon reg.id = ARM64_CORE_REG(regs.regs[2]); 461e0c135aSWill Deacon if (ioctl(vcpu->vcpu_fd, KVM_SET_ONE_REG, ®) < 0) 471e0c135aSWill Deacon die_perror("KVM_SET_ONE_REG failed (r2)"); 481e0c135aSWill Deacon 491e0c135aSWill Deacon /* pc = start of kernel image */ 501e0c135aSWill Deacon data = kvm->arch.kern_guest_start; 511e0c135aSWill Deacon reg.id = ARM64_CORE_REG(regs.pc); 521e0c135aSWill Deacon if (ioctl(vcpu->vcpu_fd, KVM_SET_ONE_REG, ®) < 0) 531e0c135aSWill Deacon die_perror("KVM_SET_ONE_REG failed (pc)"); 541e0c135aSWill Deacon } 551e0c135aSWill Deacon 561e0c135aSWill Deacon static void reset_vcpu_aarch64(struct kvm_cpu *vcpu) 571e0c135aSWill Deacon { 581e0c135aSWill Deacon struct kvm *kvm = vcpu->kvm; 591e0c135aSWill Deacon struct kvm_one_reg reg; 601e0c135aSWill Deacon u64 data; 611e0c135aSWill Deacon 621e0c135aSWill Deacon reg.addr = (u64)&data; 631e0c135aSWill Deacon 641e0c135aSWill Deacon /* pstate = all interrupts masked */ 651e0c135aSWill Deacon data = PSR_D_BIT | PSR_A_BIT | PSR_I_BIT | PSR_F_BIT | PSR_MODE_EL1h; 661e0c135aSWill Deacon reg.id = ARM64_CORE_REG(regs.pstate); 671e0c135aSWill Deacon if (ioctl(vcpu->vcpu_fd, KVM_SET_ONE_REG, ®) < 0) 681e0c135aSWill Deacon die_perror("KVM_SET_ONE_REG failed (spsr[EL1])"); 691e0c135aSWill Deacon 701e0c135aSWill Deacon /* x1...x3 = 0 */ 711e0c135aSWill Deacon data = 0; 721e0c135aSWill Deacon reg.id = ARM64_CORE_REG(regs.regs[1]); 731e0c135aSWill Deacon if (ioctl(vcpu->vcpu_fd, KVM_SET_ONE_REG, ®) < 0) 741e0c135aSWill Deacon die_perror("KVM_SET_ONE_REG failed (x1)"); 751e0c135aSWill Deacon 761e0c135aSWill Deacon reg.id = ARM64_CORE_REG(regs.regs[2]); 771e0c135aSWill Deacon if (ioctl(vcpu->vcpu_fd, KVM_SET_ONE_REG, ®) < 0) 781e0c135aSWill Deacon die_perror("KVM_SET_ONE_REG failed (x2)"); 791e0c135aSWill Deacon 801e0c135aSWill Deacon reg.id = ARM64_CORE_REG(regs.regs[3]); 811e0c135aSWill Deacon if (ioctl(vcpu->vcpu_fd, KVM_SET_ONE_REG, ®) < 0) 821e0c135aSWill Deacon die_perror("KVM_SET_ONE_REG failed (x3)"); 831e0c135aSWill Deacon 841e0c135aSWill Deacon /* Secondary cores are stopped awaiting PSCI wakeup */ 851e0c135aSWill Deacon if (vcpu->cpu_id == 0) { 861e0c135aSWill Deacon /* x0 = physical address of the device tree blob */ 871e0c135aSWill Deacon data = kvm->arch.dtb_guest_start; 881e0c135aSWill Deacon reg.id = ARM64_CORE_REG(regs.regs[0]); 891e0c135aSWill Deacon if (ioctl(vcpu->vcpu_fd, KVM_SET_ONE_REG, ®) < 0) 901e0c135aSWill Deacon die_perror("KVM_SET_ONE_REG failed (x0)"); 911e0c135aSWill Deacon 921e0c135aSWill Deacon /* pc = start of kernel image */ 931e0c135aSWill Deacon data = kvm->arch.kern_guest_start; 941e0c135aSWill Deacon reg.id = ARM64_CORE_REG(regs.pc); 951e0c135aSWill Deacon if (ioctl(vcpu->vcpu_fd, KVM_SET_ONE_REG, ®) < 0) 961e0c135aSWill Deacon die_perror("KVM_SET_ONE_REG failed (pc)"); 971e0c135aSWill Deacon } 981e0c135aSWill Deacon } 991e0c135aSWill Deacon 1001e0c135aSWill Deacon void kvm_cpu__reset_vcpu(struct kvm_cpu *vcpu) 1011e0c135aSWill Deacon { 1021e0c135aSWill Deacon if (vcpu->kvm->cfg.arch.aarch32_guest) 1031e0c135aSWill Deacon return reset_vcpu_aarch32(vcpu); 1041e0c135aSWill Deacon else 1051e0c135aSWill Deacon return reset_vcpu_aarch64(vcpu); 1061e0c135aSWill Deacon } 1071e0c135aSWill Deacon 1081e0c135aSWill Deacon void kvm_cpu__show_code(struct kvm_cpu *vcpu) 1091e0c135aSWill Deacon { 1101e0c135aSWill Deacon struct kvm_one_reg reg; 1111e0c135aSWill Deacon unsigned long data; 112*30c31b66SWill Deacon int debug_fd = kvm_cpu__get_debug_fd(); 1131e0c135aSWill Deacon 1141e0c135aSWill Deacon reg.addr = (u64)&data; 1151e0c135aSWill Deacon 116*30c31b66SWill Deacon dprintf(debug_fd, "\n*pc:\n"); 1171e0c135aSWill Deacon reg.id = ARM64_CORE_REG(regs.pc); 1181e0c135aSWill Deacon if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, ®) < 0) 1191e0c135aSWill Deacon die("KVM_GET_ONE_REG failed (show_code @ PC)"); 1201e0c135aSWill Deacon 121*30c31b66SWill Deacon kvm__dump_mem(vcpu->kvm, data, 32, debug_fd); 1221e0c135aSWill Deacon 123*30c31b66SWill Deacon dprintf(debug_fd, "\n*lr:\n"); 1241e0c135aSWill Deacon reg.id = ARM64_CORE_REG(regs.regs[30]); 1251e0c135aSWill Deacon if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, ®) < 0) 1261e0c135aSWill Deacon die("KVM_GET_ONE_REG failed (show_code @ LR)"); 1271e0c135aSWill Deacon 128*30c31b66SWill Deacon kvm__dump_mem(vcpu->kvm, data, 32, debug_fd); 1291e0c135aSWill Deacon } 1301e0c135aSWill Deacon 1311e0c135aSWill Deacon void kvm_cpu__show_registers(struct kvm_cpu *vcpu) 1321e0c135aSWill Deacon { 1331e0c135aSWill Deacon struct kvm_one_reg reg; 1341e0c135aSWill Deacon unsigned long data; 1351e0c135aSWill Deacon int debug_fd = kvm_cpu__get_debug_fd(); 1361e0c135aSWill Deacon 1371e0c135aSWill Deacon reg.addr = (u64)&data; 1381e0c135aSWill Deacon dprintf(debug_fd, "\n Registers:\n"); 1391e0c135aSWill Deacon 1401e0c135aSWill Deacon reg.id = ARM64_CORE_REG(regs.pc); 1411e0c135aSWill Deacon if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, ®) < 0) 1421e0c135aSWill Deacon die("KVM_GET_ONE_REG failed (pc)"); 1431e0c135aSWill Deacon dprintf(debug_fd, " PC: 0x%lx\n", data); 1441e0c135aSWill Deacon 1451e0c135aSWill Deacon reg.id = ARM64_CORE_REG(regs.pstate); 1461e0c135aSWill Deacon if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, ®) < 0) 1471e0c135aSWill Deacon die("KVM_GET_ONE_REG failed (pstate)"); 1481e0c135aSWill Deacon dprintf(debug_fd, " PSTATE: 0x%lx\n", data); 1491e0c135aSWill Deacon 1501e0c135aSWill Deacon reg.id = ARM64_CORE_REG(sp_el1); 1511e0c135aSWill Deacon if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, ®) < 0) 1521e0c135aSWill Deacon die("KVM_GET_ONE_REG failed (sp_el1)"); 1531e0c135aSWill Deacon dprintf(debug_fd, " SP_EL1: 0x%lx\n", data); 1541e0c135aSWill Deacon 1551e0c135aSWill Deacon reg.id = ARM64_CORE_REG(regs.regs[30]); 1561e0c135aSWill Deacon if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, ®) < 0) 1571e0c135aSWill Deacon die("KVM_GET_ONE_REG failed (lr)"); 1581e0c135aSWill Deacon dprintf(debug_fd, " LR: 0x%lx\n", data); 1591e0c135aSWill Deacon } 160