xref: /linux/tools/testing/selftests/kvm/arm64/aarch32_id_regs.c (revision 1260ed77798502de9c98020040d2995008de10cc)
167730e6cSSean Christopherson // SPDX-License-Identifier: GPL-2.0-only
267730e6cSSean Christopherson /*
367730e6cSSean Christopherson  * aarch32_id_regs - Test for ID register behavior on AArch64-only systems
467730e6cSSean Christopherson  *
567730e6cSSean Christopherson  * Copyright (c) 2022 Google LLC.
667730e6cSSean Christopherson  *
767730e6cSSean Christopherson  * Test that KVM handles the AArch64 views of the AArch32 ID registers as RAZ
867730e6cSSean Christopherson  * and WI from userspace.
967730e6cSSean Christopherson  */
1067730e6cSSean Christopherson 
1167730e6cSSean Christopherson #include <stdint.h>
1267730e6cSSean Christopherson 
1367730e6cSSean Christopherson #include "kvm_util.h"
1467730e6cSSean Christopherson #include "processor.h"
1567730e6cSSean Christopherson #include "test_util.h"
1667730e6cSSean Christopherson #include <linux/bitfield.h>
1767730e6cSSean Christopherson 
1867730e6cSSean Christopherson #define BAD_ID_REG_VAL	0x1badc0deul
1967730e6cSSean Christopherson 
2067730e6cSSean Christopherson #define GUEST_ASSERT_REG_RAZ(reg)	GUEST_ASSERT_EQ(read_sysreg_s(reg), 0)
2167730e6cSSean Christopherson 
2267730e6cSSean Christopherson static void guest_main(void)
2367730e6cSSean Christopherson {
2467730e6cSSean Christopherson 	GUEST_ASSERT_REG_RAZ(SYS_ID_PFR0_EL1);
2567730e6cSSean Christopherson 	GUEST_ASSERT_REG_RAZ(SYS_ID_PFR1_EL1);
2667730e6cSSean Christopherson 	GUEST_ASSERT_REG_RAZ(SYS_ID_DFR0_EL1);
2767730e6cSSean Christopherson 	GUEST_ASSERT_REG_RAZ(SYS_ID_AFR0_EL1);
2867730e6cSSean Christopherson 	GUEST_ASSERT_REG_RAZ(SYS_ID_MMFR0_EL1);
2967730e6cSSean Christopherson 	GUEST_ASSERT_REG_RAZ(SYS_ID_MMFR1_EL1);
3067730e6cSSean Christopherson 	GUEST_ASSERT_REG_RAZ(SYS_ID_MMFR2_EL1);
3167730e6cSSean Christopherson 	GUEST_ASSERT_REG_RAZ(SYS_ID_MMFR3_EL1);
3267730e6cSSean Christopherson 	GUEST_ASSERT_REG_RAZ(SYS_ID_ISAR0_EL1);
3367730e6cSSean Christopherson 	GUEST_ASSERT_REG_RAZ(SYS_ID_ISAR1_EL1);
3467730e6cSSean Christopherson 	GUEST_ASSERT_REG_RAZ(SYS_ID_ISAR2_EL1);
3567730e6cSSean Christopherson 	GUEST_ASSERT_REG_RAZ(SYS_ID_ISAR3_EL1);
3667730e6cSSean Christopherson 	GUEST_ASSERT_REG_RAZ(SYS_ID_ISAR4_EL1);
3767730e6cSSean Christopherson 	GUEST_ASSERT_REG_RAZ(SYS_ID_ISAR5_EL1);
3867730e6cSSean Christopherson 	GUEST_ASSERT_REG_RAZ(SYS_ID_MMFR4_EL1);
3967730e6cSSean Christopherson 	GUEST_ASSERT_REG_RAZ(SYS_ID_ISAR6_EL1);
4067730e6cSSean Christopherson 	GUEST_ASSERT_REG_RAZ(SYS_MVFR0_EL1);
4167730e6cSSean Christopherson 	GUEST_ASSERT_REG_RAZ(SYS_MVFR1_EL1);
4267730e6cSSean Christopherson 	GUEST_ASSERT_REG_RAZ(SYS_MVFR2_EL1);
4367730e6cSSean Christopherson 	GUEST_ASSERT_REG_RAZ(sys_reg(3, 0, 0, 3, 3));
4467730e6cSSean Christopherson 	GUEST_ASSERT_REG_RAZ(SYS_ID_PFR2_EL1);
4567730e6cSSean Christopherson 	GUEST_ASSERT_REG_RAZ(SYS_ID_DFR1_EL1);
4667730e6cSSean Christopherson 	GUEST_ASSERT_REG_RAZ(SYS_ID_MMFR5_EL1);
4767730e6cSSean Christopherson 	GUEST_ASSERT_REG_RAZ(sys_reg(3, 0, 0, 3, 7));
4867730e6cSSean Christopherson 
4967730e6cSSean Christopherson 	GUEST_DONE();
5067730e6cSSean Christopherson }
5167730e6cSSean Christopherson 
5267730e6cSSean Christopherson static void test_guest_raz(struct kvm_vcpu *vcpu)
5367730e6cSSean Christopherson {
5467730e6cSSean Christopherson 	struct ucall uc;
5567730e6cSSean Christopherson 
5667730e6cSSean Christopherson 	vcpu_run(vcpu);
5767730e6cSSean Christopherson 
5867730e6cSSean Christopherson 	switch (get_ucall(vcpu, &uc)) {
5967730e6cSSean Christopherson 	case UCALL_ABORT:
6067730e6cSSean Christopherson 		REPORT_GUEST_ASSERT(uc);
6167730e6cSSean Christopherson 		break;
6267730e6cSSean Christopherson 	case UCALL_DONE:
6367730e6cSSean Christopherson 		break;
6467730e6cSSean Christopherson 	default:
6567730e6cSSean Christopherson 		TEST_FAIL("Unexpected ucall: %lu", uc.cmd);
6667730e6cSSean Christopherson 	}
6767730e6cSSean Christopherson }
6867730e6cSSean Christopherson 
6967730e6cSSean Christopherson static uint64_t raz_wi_reg_ids[] = {
7067730e6cSSean Christopherson 	KVM_ARM64_SYS_REG(SYS_ID_PFR0_EL1),
7167730e6cSSean Christopherson 	KVM_ARM64_SYS_REG(SYS_ID_PFR1_EL1),
7267730e6cSSean Christopherson 	KVM_ARM64_SYS_REG(SYS_ID_DFR0_EL1),
7367730e6cSSean Christopherson 	KVM_ARM64_SYS_REG(SYS_ID_MMFR0_EL1),
7467730e6cSSean Christopherson 	KVM_ARM64_SYS_REG(SYS_ID_MMFR1_EL1),
7567730e6cSSean Christopherson 	KVM_ARM64_SYS_REG(SYS_ID_MMFR2_EL1),
7667730e6cSSean Christopherson 	KVM_ARM64_SYS_REG(SYS_ID_MMFR3_EL1),
7767730e6cSSean Christopherson 	KVM_ARM64_SYS_REG(SYS_ID_ISAR0_EL1),
7867730e6cSSean Christopherson 	KVM_ARM64_SYS_REG(SYS_ID_ISAR1_EL1),
7967730e6cSSean Christopherson 	KVM_ARM64_SYS_REG(SYS_ID_ISAR2_EL1),
8067730e6cSSean Christopherson 	KVM_ARM64_SYS_REG(SYS_ID_ISAR3_EL1),
8167730e6cSSean Christopherson 	KVM_ARM64_SYS_REG(SYS_ID_ISAR4_EL1),
8267730e6cSSean Christopherson 	KVM_ARM64_SYS_REG(SYS_ID_ISAR5_EL1),
8367730e6cSSean Christopherson 	KVM_ARM64_SYS_REG(SYS_ID_MMFR4_EL1),
8467730e6cSSean Christopherson 	KVM_ARM64_SYS_REG(SYS_ID_ISAR6_EL1),
8567730e6cSSean Christopherson 	KVM_ARM64_SYS_REG(SYS_MVFR0_EL1),
8667730e6cSSean Christopherson 	KVM_ARM64_SYS_REG(SYS_MVFR1_EL1),
8767730e6cSSean Christopherson 	KVM_ARM64_SYS_REG(SYS_MVFR2_EL1),
8867730e6cSSean Christopherson 	KVM_ARM64_SYS_REG(SYS_ID_PFR2_EL1),
8967730e6cSSean Christopherson 	KVM_ARM64_SYS_REG(SYS_ID_MMFR5_EL1),
9067730e6cSSean Christopherson };
9167730e6cSSean Christopherson 
9267730e6cSSean Christopherson static void test_user_raz_wi(struct kvm_vcpu *vcpu)
9367730e6cSSean Christopherson {
9467730e6cSSean Christopherson 	int i;
9567730e6cSSean Christopherson 
9667730e6cSSean Christopherson 	for (i = 0; i < ARRAY_SIZE(raz_wi_reg_ids); i++) {
9767730e6cSSean Christopherson 		uint64_t reg_id = raz_wi_reg_ids[i];
9867730e6cSSean Christopherson 		uint64_t val;
9967730e6cSSean Christopherson 
10067730e6cSSean Christopherson 		val = vcpu_get_reg(vcpu, reg_id);
10167730e6cSSean Christopherson 		TEST_ASSERT_EQ(val, 0);
10267730e6cSSean Christopherson 
10367730e6cSSean Christopherson 		/*
10467730e6cSSean Christopherson 		 * Expect the ioctl to succeed with no effect on the register
10567730e6cSSean Christopherson 		 * value.
10667730e6cSSean Christopherson 		 */
10767730e6cSSean Christopherson 		vcpu_set_reg(vcpu, reg_id, BAD_ID_REG_VAL);
10867730e6cSSean Christopherson 
10967730e6cSSean Christopherson 		val = vcpu_get_reg(vcpu, reg_id);
11067730e6cSSean Christopherson 		TEST_ASSERT_EQ(val, 0);
11167730e6cSSean Christopherson 	}
11267730e6cSSean Christopherson }
11367730e6cSSean Christopherson 
11467730e6cSSean Christopherson static uint64_t raz_invariant_reg_ids[] = {
11567730e6cSSean Christopherson 	KVM_ARM64_SYS_REG(SYS_ID_AFR0_EL1),
11667730e6cSSean Christopherson 	KVM_ARM64_SYS_REG(sys_reg(3, 0, 0, 3, 3)),
11767730e6cSSean Christopherson 	KVM_ARM64_SYS_REG(SYS_ID_DFR1_EL1),
11867730e6cSSean Christopherson 	KVM_ARM64_SYS_REG(sys_reg(3, 0, 0, 3, 7)),
11967730e6cSSean Christopherson };
12067730e6cSSean Christopherson 
12167730e6cSSean Christopherson static void test_user_raz_invariant(struct kvm_vcpu *vcpu)
12267730e6cSSean Christopherson {
12367730e6cSSean Christopherson 	int i, r;
12467730e6cSSean Christopherson 
12567730e6cSSean Christopherson 	for (i = 0; i < ARRAY_SIZE(raz_invariant_reg_ids); i++) {
12667730e6cSSean Christopherson 		uint64_t reg_id = raz_invariant_reg_ids[i];
12767730e6cSSean Christopherson 		uint64_t val;
12867730e6cSSean Christopherson 
12967730e6cSSean Christopherson 		val = vcpu_get_reg(vcpu, reg_id);
13067730e6cSSean Christopherson 		TEST_ASSERT_EQ(val, 0);
13167730e6cSSean Christopherson 
13267730e6cSSean Christopherson 		r = __vcpu_set_reg(vcpu, reg_id, BAD_ID_REG_VAL);
13367730e6cSSean Christopherson 		TEST_ASSERT(r < 0 && errno == EINVAL,
13467730e6cSSean Christopherson 			    "unexpected KVM_SET_ONE_REG error: r=%d, errno=%d", r, errno);
13567730e6cSSean Christopherson 
13667730e6cSSean Christopherson 		val = vcpu_get_reg(vcpu, reg_id);
13767730e6cSSean Christopherson 		TEST_ASSERT_EQ(val, 0);
13867730e6cSSean Christopherson 	}
13967730e6cSSean Christopherson }
14067730e6cSSean Christopherson 
14167730e6cSSean Christopherson 
14267730e6cSSean Christopherson 
14367730e6cSSean Christopherson static bool vcpu_aarch64_only(struct kvm_vcpu *vcpu)
14467730e6cSSean Christopherson {
14567730e6cSSean Christopherson 	uint64_t val, el0;
14667730e6cSSean Christopherson 
14767730e6cSSean Christopherson 	val = vcpu_get_reg(vcpu, KVM_ARM64_SYS_REG(SYS_ID_AA64PFR0_EL1));
14867730e6cSSean Christopherson 
14967730e6cSSean Christopherson 	el0 = FIELD_GET(ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_EL0), val);
150*e2ee2e9bSLinus Torvalds 	return el0 == ID_AA64PFR0_EL1_EL0_IMP;
15167730e6cSSean Christopherson }
15267730e6cSSean Christopherson 
15367730e6cSSean Christopherson int main(void)
15467730e6cSSean Christopherson {
15567730e6cSSean Christopherson 	struct kvm_vcpu *vcpu;
15667730e6cSSean Christopherson 	struct kvm_vm *vm;
15767730e6cSSean Christopherson 
15867730e6cSSean Christopherson 	vm = vm_create_with_one_vcpu(&vcpu, guest_main);
15967730e6cSSean Christopherson 
16067730e6cSSean Christopherson 	TEST_REQUIRE(vcpu_aarch64_only(vcpu));
16167730e6cSSean Christopherson 
16267730e6cSSean Christopherson 	test_user_raz_wi(vcpu);
16367730e6cSSean Christopherson 	test_user_raz_invariant(vcpu);
16467730e6cSSean Christopherson 	test_guest_raz(vcpu);
16567730e6cSSean Christopherson 
16667730e6cSSean Christopherson 	kvm_vm_free(vm);
16767730e6cSSean Christopherson }
168