xref: /kvmtool/arm/aarch64/kvm-cpu.c (revision 74c5e7b2bee69372c63e1a48e7e05ceb81a6ea2c)
11e0c135aSWill Deacon #include "kvm/kvm-cpu.h"
21e0c135aSWill Deacon #include "kvm/kvm.h"
3fc9d8ec3SMarc Zyngier #include "kvm/virtio.h"
41e0c135aSWill Deacon 
51e0c135aSWill Deacon #include <asm/ptrace.h>
61e0c135aSWill Deacon 
71e0c135aSWill Deacon #define COMPAT_PSR_F_BIT	0x00000040
81e0c135aSWill Deacon #define COMPAT_PSR_I_BIT	0x00000080
9fc9d8ec3SMarc Zyngier #define COMPAT_PSR_E_BIT	0x00000200
101e0c135aSWill Deacon #define COMPAT_PSR_MODE_SVC	0x00000013
111e0c135aSWill Deacon 
12fc9d8ec3SMarc Zyngier #define SCTLR_EL1_E0E_MASK	(1 << 24)
13fc9d8ec3SMarc Zyngier #define SCTLR_EL1_EE_MASK	(1 << 25)
14fc9d8ec3SMarc Zyngier 
155ae841d1SDave Martin static __u64 __core_reg_id(__u64 offset)
165ae841d1SDave Martin {
175ae841d1SDave Martin 	__u64 id = KVM_REG_ARM64 | KVM_REG_ARM_CORE | offset;
185ae841d1SDave Martin 
195ae841d1SDave Martin 	if (offset < KVM_REG_ARM_CORE_REG(fp_regs))
205ae841d1SDave Martin 		id |= KVM_REG_SIZE_U64;
215ae841d1SDave Martin 	else if (offset < KVM_REG_ARM_CORE_REG(fp_regs.fpsr))
225ae841d1SDave Martin 		id |= KVM_REG_SIZE_U128;
235ae841d1SDave Martin 	else
245ae841d1SDave Martin 		id |= KVM_REG_SIZE_U32;
255ae841d1SDave Martin 
265ae841d1SDave Martin 	return id;
275ae841d1SDave Martin }
285ae841d1SDave Martin 
295ae841d1SDave Martin #define ARM64_CORE_REG(x) __core_reg_id(KVM_REG_ARM_CORE_REG(x))
301e0c135aSWill Deacon 
31d06bc640SMarc Zyngier unsigned long kvm_cpu__get_vcpu_mpidr(struct kvm_cpu *vcpu)
32d06bc640SMarc Zyngier {
33d06bc640SMarc Zyngier 	struct kvm_one_reg reg;
34d06bc640SMarc Zyngier 	u64 mpidr;
35d06bc640SMarc Zyngier 
36d06bc640SMarc Zyngier 	reg.id = ARM64_SYS_REG(ARM_CPU_ID, ARM_CPU_ID_MPIDR);
37d06bc640SMarc Zyngier 	reg.addr = (u64)&mpidr;
38d06bc640SMarc Zyngier 	if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, &reg) < 0)
39d06bc640SMarc Zyngier 		die("KVM_GET_ONE_REG failed (get_mpidr vcpu%ld", vcpu->cpu_id);
40d06bc640SMarc Zyngier 
41d06bc640SMarc Zyngier 	return mpidr;
42d06bc640SMarc Zyngier }
43d06bc640SMarc Zyngier 
441e0c135aSWill Deacon static void reset_vcpu_aarch32(struct kvm_cpu *vcpu)
451e0c135aSWill Deacon {
461e0c135aSWill Deacon 	struct kvm *kvm = vcpu->kvm;
471e0c135aSWill Deacon 	struct kvm_one_reg reg;
481e0c135aSWill Deacon 	u64 data;
491e0c135aSWill Deacon 
501e0c135aSWill Deacon 	reg.addr = (u64)&data;
511e0c135aSWill Deacon 
521e0c135aSWill Deacon 	/* pstate = all interrupts masked */
531e0c135aSWill Deacon 	data	= COMPAT_PSR_I_BIT | COMPAT_PSR_F_BIT | COMPAT_PSR_MODE_SVC;
541e0c135aSWill Deacon 	reg.id	= ARM64_CORE_REG(regs.pstate);
551e0c135aSWill Deacon 	if (ioctl(vcpu->vcpu_fd, KVM_SET_ONE_REG, &reg) < 0)
561e0c135aSWill Deacon 		die_perror("KVM_SET_ONE_REG failed (spsr[EL1])");
571e0c135aSWill Deacon 
581e0c135aSWill Deacon 	/* Secondary cores are stopped awaiting PSCI wakeup */
591e0c135aSWill Deacon 	if (vcpu->cpu_id != 0)
601e0c135aSWill Deacon 		return;
611e0c135aSWill Deacon 
621e0c135aSWill Deacon 	/* r0 = 0 */
631e0c135aSWill Deacon 	data	= 0;
641e0c135aSWill Deacon 	reg.id	= ARM64_CORE_REG(regs.regs[0]);
651e0c135aSWill Deacon 	if (ioctl(vcpu->vcpu_fd, KVM_SET_ONE_REG, &reg) < 0)
661e0c135aSWill Deacon 		die_perror("KVM_SET_ONE_REG failed (r0)");
671e0c135aSWill Deacon 
681e0c135aSWill Deacon 	/* r1 = machine type (-1) */
691e0c135aSWill Deacon 	data	= -1;
701e0c135aSWill Deacon 	reg.id	= ARM64_CORE_REG(regs.regs[1]);
711e0c135aSWill Deacon 	if (ioctl(vcpu->vcpu_fd, KVM_SET_ONE_REG, &reg) < 0)
721e0c135aSWill Deacon 		die_perror("KVM_SET_ONE_REG failed (r1)");
731e0c135aSWill Deacon 
741e0c135aSWill Deacon 	/* r2 = physical address of the device tree blob */
751e0c135aSWill Deacon 	data	= kvm->arch.dtb_guest_start;
761e0c135aSWill Deacon 	reg.id	= ARM64_CORE_REG(regs.regs[2]);
771e0c135aSWill Deacon 	if (ioctl(vcpu->vcpu_fd, KVM_SET_ONE_REG, &reg) < 0)
781e0c135aSWill Deacon 		die_perror("KVM_SET_ONE_REG failed (r2)");
791e0c135aSWill Deacon 
801e0c135aSWill Deacon 	/* pc = start of kernel image */
811e0c135aSWill Deacon 	data	= kvm->arch.kern_guest_start;
821e0c135aSWill Deacon 	reg.id	= ARM64_CORE_REG(regs.pc);
831e0c135aSWill Deacon 	if (ioctl(vcpu->vcpu_fd, KVM_SET_ONE_REG, &reg) < 0)
841e0c135aSWill Deacon 		die_perror("KVM_SET_ONE_REG failed (pc)");
851e0c135aSWill Deacon }
861e0c135aSWill Deacon 
871e0c135aSWill Deacon static void reset_vcpu_aarch64(struct kvm_cpu *vcpu)
881e0c135aSWill Deacon {
891e0c135aSWill Deacon 	struct kvm *kvm = vcpu->kvm;
901e0c135aSWill Deacon 	struct kvm_one_reg reg;
911e0c135aSWill Deacon 	u64 data;
921e0c135aSWill Deacon 
931e0c135aSWill Deacon 	reg.addr = (u64)&data;
941e0c135aSWill Deacon 
951e0c135aSWill Deacon 	/* pstate = all interrupts masked */
961e0c135aSWill Deacon 	data	= PSR_D_BIT | PSR_A_BIT | PSR_I_BIT | PSR_F_BIT | PSR_MODE_EL1h;
971e0c135aSWill Deacon 	reg.id	= ARM64_CORE_REG(regs.pstate);
981e0c135aSWill Deacon 	if (ioctl(vcpu->vcpu_fd, KVM_SET_ONE_REG, &reg) < 0)
991e0c135aSWill Deacon 		die_perror("KVM_SET_ONE_REG failed (spsr[EL1])");
1001e0c135aSWill Deacon 
1011e0c135aSWill Deacon 	/* x1...x3 = 0 */
1021e0c135aSWill Deacon 	data	= 0;
1031e0c135aSWill Deacon 	reg.id	= ARM64_CORE_REG(regs.regs[1]);
1041e0c135aSWill Deacon 	if (ioctl(vcpu->vcpu_fd, KVM_SET_ONE_REG, &reg) < 0)
1051e0c135aSWill Deacon 		die_perror("KVM_SET_ONE_REG failed (x1)");
1061e0c135aSWill Deacon 
1071e0c135aSWill Deacon 	reg.id	= ARM64_CORE_REG(regs.regs[2]);
1081e0c135aSWill Deacon 	if (ioctl(vcpu->vcpu_fd, KVM_SET_ONE_REG, &reg) < 0)
1091e0c135aSWill Deacon 		die_perror("KVM_SET_ONE_REG failed (x2)");
1101e0c135aSWill Deacon 
1111e0c135aSWill Deacon 	reg.id	= ARM64_CORE_REG(regs.regs[3]);
1121e0c135aSWill Deacon 	if (ioctl(vcpu->vcpu_fd, KVM_SET_ONE_REG, &reg) < 0)
1131e0c135aSWill Deacon 		die_perror("KVM_SET_ONE_REG failed (x3)");
1141e0c135aSWill Deacon 
1151e0c135aSWill Deacon 	/* Secondary cores are stopped awaiting PSCI wakeup */
1161e0c135aSWill Deacon 	if (vcpu->cpu_id == 0) {
1171e0c135aSWill Deacon 		/* x0 = physical address of the device tree blob */
1181e0c135aSWill Deacon 		data	= kvm->arch.dtb_guest_start;
1191e0c135aSWill Deacon 		reg.id	= ARM64_CORE_REG(regs.regs[0]);
1201e0c135aSWill Deacon 		if (ioctl(vcpu->vcpu_fd, KVM_SET_ONE_REG, &reg) < 0)
1211e0c135aSWill Deacon 			die_perror("KVM_SET_ONE_REG failed (x0)");
1221e0c135aSWill Deacon 
1231e0c135aSWill Deacon 		/* pc = start of kernel image */
1241e0c135aSWill Deacon 		data	= kvm->arch.kern_guest_start;
1251e0c135aSWill Deacon 		reg.id	= ARM64_CORE_REG(regs.pc);
1261e0c135aSWill Deacon 		if (ioctl(vcpu->vcpu_fd, KVM_SET_ONE_REG, &reg) < 0)
1271e0c135aSWill Deacon 			die_perror("KVM_SET_ONE_REG failed (pc)");
1281e0c135aSWill Deacon 	}
1291e0c135aSWill Deacon }
1301e0c135aSWill Deacon 
131*74c5e7b2SDave Martin void kvm_cpu__select_features(struct kvm *kvm, struct kvm_vcpu_init *init)
132*74c5e7b2SDave Martin {
133*74c5e7b2SDave Martin 	/* Enable pointer authentication if available */
134*74c5e7b2SDave Martin 	if (kvm__supports_extension(kvm, KVM_CAP_ARM_PTRAUTH_ADDRESS) &&
135*74c5e7b2SDave Martin 	    kvm__supports_extension(kvm, KVM_CAP_ARM_PTRAUTH_GENERIC)) {
136*74c5e7b2SDave Martin 		init->features[0] |= 1UL << KVM_ARM_VCPU_PTRAUTH_ADDRESS;
137*74c5e7b2SDave Martin 		init->features[0] |= 1UL << KVM_ARM_VCPU_PTRAUTH_GENERIC;
138*74c5e7b2SDave Martin 	}
139*74c5e7b2SDave Martin }
140*74c5e7b2SDave Martin 
1411e0c135aSWill Deacon void kvm_cpu__reset_vcpu(struct kvm_cpu *vcpu)
1421e0c135aSWill Deacon {
1431e0c135aSWill Deacon 	if (vcpu->kvm->cfg.arch.aarch32_guest)
1441e0c135aSWill Deacon 		return reset_vcpu_aarch32(vcpu);
1451e0c135aSWill Deacon 	else
1461e0c135aSWill Deacon 		return reset_vcpu_aarch64(vcpu);
1471e0c135aSWill Deacon }
1481e0c135aSWill Deacon 
149fc9d8ec3SMarc Zyngier int kvm_cpu__get_endianness(struct kvm_cpu *vcpu)
150fc9d8ec3SMarc Zyngier {
151fc9d8ec3SMarc Zyngier 	struct kvm_one_reg reg;
152fc9d8ec3SMarc Zyngier 	u64 psr;
153fc9d8ec3SMarc Zyngier 	u64 sctlr;
154fc9d8ec3SMarc Zyngier 
155fc9d8ec3SMarc Zyngier 	/*
156fc9d8ec3SMarc Zyngier 	 * Quoting the definition given by Peter Maydell:
157fc9d8ec3SMarc Zyngier 	 *
158fc9d8ec3SMarc Zyngier 	 * "Endianness of the CPU which does the virtio reset at the
159fc9d8ec3SMarc Zyngier 	 * point when it does that reset"
160fc9d8ec3SMarc Zyngier 	 *
161fc9d8ec3SMarc Zyngier 	 * We first check for an AArch32 guest: its endianness can
162fc9d8ec3SMarc Zyngier 	 * change when using SETEND, which affects the CPSR.E bit.
163fc9d8ec3SMarc Zyngier 	 *
164fc9d8ec3SMarc Zyngier 	 * If we're AArch64, use SCTLR_EL1.E0E if access comes from
165fc9d8ec3SMarc Zyngier 	 * EL0, and SCTLR_EL1.EE if access comes from EL1.
166fc9d8ec3SMarc Zyngier 	 */
167fc9d8ec3SMarc Zyngier 	reg.id = ARM64_CORE_REG(regs.pstate);
168fc9d8ec3SMarc Zyngier 	reg.addr = (u64)&psr;
169fc9d8ec3SMarc Zyngier 	if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, &reg) < 0)
170fc9d8ec3SMarc Zyngier 		die("KVM_GET_ONE_REG failed (spsr[EL1])");
171fc9d8ec3SMarc Zyngier 
172fc9d8ec3SMarc Zyngier 	if (psr & PSR_MODE32_BIT)
173fc9d8ec3SMarc Zyngier 		return (psr & COMPAT_PSR_E_BIT) ? VIRTIO_ENDIAN_BE : VIRTIO_ENDIAN_LE;
174fc9d8ec3SMarc Zyngier 
175fc9d8ec3SMarc Zyngier 	reg.id = ARM64_SYS_REG(ARM_CPU_CTRL, ARM_CPU_CTRL_SCTLR_EL1);
176fc9d8ec3SMarc Zyngier 	reg.addr = (u64)&sctlr;
177fc9d8ec3SMarc Zyngier 	if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, &reg) < 0)
178fc9d8ec3SMarc Zyngier 		die("KVM_GET_ONE_REG failed (SCTLR_EL1)");
179fc9d8ec3SMarc Zyngier 
180fc9d8ec3SMarc Zyngier 	if ((psr & PSR_MODE_MASK) == PSR_MODE_EL0t)
181fc9d8ec3SMarc Zyngier 		sctlr &= SCTLR_EL1_E0E_MASK;
182fc9d8ec3SMarc Zyngier 	else
183fc9d8ec3SMarc Zyngier 		sctlr &= SCTLR_EL1_EE_MASK;
184fc9d8ec3SMarc Zyngier 	return sctlr ? VIRTIO_ENDIAN_BE : VIRTIO_ENDIAN_LE;
185fc9d8ec3SMarc Zyngier }
186fc9d8ec3SMarc Zyngier 
1871e0c135aSWill Deacon void kvm_cpu__show_code(struct kvm_cpu *vcpu)
1881e0c135aSWill Deacon {
1891e0c135aSWill Deacon 	struct kvm_one_reg reg;
1901e0c135aSWill Deacon 	unsigned long data;
19130c31b66SWill Deacon 	int debug_fd = kvm_cpu__get_debug_fd();
1921e0c135aSWill Deacon 
1931e0c135aSWill Deacon 	reg.addr = (u64)&data;
1941e0c135aSWill Deacon 
19530c31b66SWill Deacon 	dprintf(debug_fd, "\n*pc:\n");
1961e0c135aSWill Deacon 	reg.id = ARM64_CORE_REG(regs.pc);
1971e0c135aSWill Deacon 	if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, &reg) < 0)
1981e0c135aSWill Deacon 		die("KVM_GET_ONE_REG failed (show_code @ PC)");
1991e0c135aSWill Deacon 
20030c31b66SWill Deacon 	kvm__dump_mem(vcpu->kvm, data, 32, debug_fd);
2011e0c135aSWill Deacon 
20230c31b66SWill Deacon 	dprintf(debug_fd, "\n*lr:\n");
2031e0c135aSWill Deacon 	reg.id = ARM64_CORE_REG(regs.regs[30]);
2041e0c135aSWill Deacon 	if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, &reg) < 0)
2051e0c135aSWill Deacon 		die("KVM_GET_ONE_REG failed (show_code @ LR)");
2061e0c135aSWill Deacon 
20730c31b66SWill Deacon 	kvm__dump_mem(vcpu->kvm, data, 32, debug_fd);
2081e0c135aSWill Deacon }
2091e0c135aSWill Deacon 
2101e0c135aSWill Deacon void kvm_cpu__show_registers(struct kvm_cpu *vcpu)
2111e0c135aSWill Deacon {
2121e0c135aSWill Deacon 	struct kvm_one_reg reg;
2131e0c135aSWill Deacon 	unsigned long data;
2141e0c135aSWill Deacon 	int debug_fd = kvm_cpu__get_debug_fd();
2151e0c135aSWill Deacon 
2161e0c135aSWill Deacon 	reg.addr = (u64)&data;
2171e0c135aSWill Deacon 	dprintf(debug_fd, "\n Registers:\n");
2181e0c135aSWill Deacon 
2191e0c135aSWill Deacon 	reg.id		= ARM64_CORE_REG(regs.pc);
2201e0c135aSWill Deacon 	if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, &reg) < 0)
2211e0c135aSWill Deacon 		die("KVM_GET_ONE_REG failed (pc)");
2221e0c135aSWill Deacon 	dprintf(debug_fd, " PC:    0x%lx\n", data);
2231e0c135aSWill Deacon 
2241e0c135aSWill Deacon 	reg.id		= ARM64_CORE_REG(regs.pstate);
2251e0c135aSWill Deacon 	if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, &reg) < 0)
2261e0c135aSWill Deacon 		die("KVM_GET_ONE_REG failed (pstate)");
2271e0c135aSWill Deacon 	dprintf(debug_fd, " PSTATE:    0x%lx\n", data);
2281e0c135aSWill Deacon 
2291e0c135aSWill Deacon 	reg.id		= ARM64_CORE_REG(sp_el1);
2301e0c135aSWill Deacon 	if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, &reg) < 0)
2311e0c135aSWill Deacon 		die("KVM_GET_ONE_REG failed (sp_el1)");
2321e0c135aSWill Deacon 	dprintf(debug_fd, " SP_EL1:    0x%lx\n", data);
2331e0c135aSWill Deacon 
2341e0c135aSWill Deacon 	reg.id		= ARM64_CORE_REG(regs.regs[30]);
2351e0c135aSWill Deacon 	if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, &reg) < 0)
2361e0c135aSWill Deacon 		die("KVM_GET_ONE_REG failed (lr)");
2371e0c135aSWill Deacon 	dprintf(debug_fd, " LR:    0x%lx\n", data);
2381e0c135aSWill Deacon }
239