18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (C) 2012,2013 - ARM Ltd
48c2ecf20Sopenharmony_ci * Author: Marc Zyngier <marc.zyngier@arm.com>
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci * Derived from arch/arm/kvm/coproc.c:
78c2ecf20Sopenharmony_ci * Copyright (C) 2012 - Virtual Open Systems and Columbia University
88c2ecf20Sopenharmony_ci * Authors: Rusty Russell <rusty@rustcorp.com.au>
98c2ecf20Sopenharmony_ci *          Christoffer Dall <c.dall@virtualopensystems.com>
108c2ecf20Sopenharmony_ci */
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci#include <linux/bsearch.h>
138c2ecf20Sopenharmony_ci#include <linux/kvm_host.h>
148c2ecf20Sopenharmony_ci#include <linux/mm.h>
158c2ecf20Sopenharmony_ci#include <linux/printk.h>
168c2ecf20Sopenharmony_ci#include <linux/uaccess.h>
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci#include <asm/cacheflush.h>
198c2ecf20Sopenharmony_ci#include <asm/cputype.h>
208c2ecf20Sopenharmony_ci#include <asm/debug-monitors.h>
218c2ecf20Sopenharmony_ci#include <asm/esr.h>
228c2ecf20Sopenharmony_ci#include <asm/kvm_arm.h>
238c2ecf20Sopenharmony_ci#include <asm/kvm_coproc.h>
248c2ecf20Sopenharmony_ci#include <asm/kvm_emulate.h>
258c2ecf20Sopenharmony_ci#include <asm/kvm_hyp.h>
268c2ecf20Sopenharmony_ci#include <asm/kvm_mmu.h>
278c2ecf20Sopenharmony_ci#include <asm/perf_event.h>
288c2ecf20Sopenharmony_ci#include <asm/sysreg.h>
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci#include <trace/events/kvm.h>
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci#include "sys_regs.h"
338c2ecf20Sopenharmony_ci#include "vgic/vgic.h"
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci#include "trace.h"
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci/*
388c2ecf20Sopenharmony_ci * All of this file is extremely similar to the ARM coproc.c, but the
398c2ecf20Sopenharmony_ci * types are different. My gut feeling is that it should be pretty
408c2ecf20Sopenharmony_ci * easy to merge, but that would be an ABI breakage -- again. VFP
418c2ecf20Sopenharmony_ci * would also need to be abstracted.
428c2ecf20Sopenharmony_ci *
438c2ecf20Sopenharmony_ci * For AArch32, we only take care of what is being trapped. Anything
448c2ecf20Sopenharmony_ci * that has to do with init and userspace access has to go via the
458c2ecf20Sopenharmony_ci * 64bit interface.
468c2ecf20Sopenharmony_ci */
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_cistatic bool read_from_write_only(struct kvm_vcpu *vcpu,
498c2ecf20Sopenharmony_ci				 struct sys_reg_params *params,
508c2ecf20Sopenharmony_ci				 const struct sys_reg_desc *r)
518c2ecf20Sopenharmony_ci{
528c2ecf20Sopenharmony_ci	WARN_ONCE(1, "Unexpected sys_reg read to write-only register\n");
538c2ecf20Sopenharmony_ci	print_sys_reg_instr(params);
548c2ecf20Sopenharmony_ci	kvm_inject_undefined(vcpu);
558c2ecf20Sopenharmony_ci	return false;
568c2ecf20Sopenharmony_ci}
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_cistatic bool write_to_read_only(struct kvm_vcpu *vcpu,
598c2ecf20Sopenharmony_ci			       struct sys_reg_params *params,
608c2ecf20Sopenharmony_ci			       const struct sys_reg_desc *r)
618c2ecf20Sopenharmony_ci{
628c2ecf20Sopenharmony_ci	WARN_ONCE(1, "Unexpected sys_reg write to read-only register\n");
638c2ecf20Sopenharmony_ci	print_sys_reg_instr(params);
648c2ecf20Sopenharmony_ci	kvm_inject_undefined(vcpu);
658c2ecf20Sopenharmony_ci	return false;
668c2ecf20Sopenharmony_ci}
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_cistatic bool __vcpu_read_sys_reg_from_cpu(int reg, u64 *val)
698c2ecf20Sopenharmony_ci{
708c2ecf20Sopenharmony_ci	/*
718c2ecf20Sopenharmony_ci	 * System registers listed in the switch are not saved on every
728c2ecf20Sopenharmony_ci	 * exit from the guest but are only saved on vcpu_put.
738c2ecf20Sopenharmony_ci	 *
748c2ecf20Sopenharmony_ci	 * Note that MPIDR_EL1 for the guest is set by KVM via VMPIDR_EL2 but
758c2ecf20Sopenharmony_ci	 * should never be listed below, because the guest cannot modify its
768c2ecf20Sopenharmony_ci	 * own MPIDR_EL1 and MPIDR_EL1 is accessed for VCPU A from VCPU B's
778c2ecf20Sopenharmony_ci	 * thread when emulating cross-VCPU communication.
788c2ecf20Sopenharmony_ci	 */
798c2ecf20Sopenharmony_ci	switch (reg) {
808c2ecf20Sopenharmony_ci	case CSSELR_EL1:	*val = read_sysreg_s(SYS_CSSELR_EL1);	break;
818c2ecf20Sopenharmony_ci	case SCTLR_EL1:		*val = read_sysreg_s(SYS_SCTLR_EL12);	break;
828c2ecf20Sopenharmony_ci	case CPACR_EL1:		*val = read_sysreg_s(SYS_CPACR_EL12);	break;
838c2ecf20Sopenharmony_ci	case TTBR0_EL1:		*val = read_sysreg_s(SYS_TTBR0_EL12);	break;
848c2ecf20Sopenharmony_ci	case TTBR1_EL1:		*val = read_sysreg_s(SYS_TTBR1_EL12);	break;
858c2ecf20Sopenharmony_ci	case TCR_EL1:		*val = read_sysreg_s(SYS_TCR_EL12);	break;
868c2ecf20Sopenharmony_ci	case ESR_EL1:		*val = read_sysreg_s(SYS_ESR_EL12);	break;
878c2ecf20Sopenharmony_ci	case AFSR0_EL1:		*val = read_sysreg_s(SYS_AFSR0_EL12);	break;
888c2ecf20Sopenharmony_ci	case AFSR1_EL1:		*val = read_sysreg_s(SYS_AFSR1_EL12);	break;
898c2ecf20Sopenharmony_ci	case FAR_EL1:		*val = read_sysreg_s(SYS_FAR_EL12);	break;
908c2ecf20Sopenharmony_ci	case MAIR_EL1:		*val = read_sysreg_s(SYS_MAIR_EL12);	break;
918c2ecf20Sopenharmony_ci	case VBAR_EL1:		*val = read_sysreg_s(SYS_VBAR_EL12);	break;
928c2ecf20Sopenharmony_ci	case CONTEXTIDR_EL1:	*val = read_sysreg_s(SYS_CONTEXTIDR_EL12);break;
938c2ecf20Sopenharmony_ci	case TPIDR_EL0:		*val = read_sysreg_s(SYS_TPIDR_EL0);	break;
948c2ecf20Sopenharmony_ci	case TPIDRRO_EL0:	*val = read_sysreg_s(SYS_TPIDRRO_EL0);	break;
958c2ecf20Sopenharmony_ci	case TPIDR_EL1:		*val = read_sysreg_s(SYS_TPIDR_EL1);	break;
968c2ecf20Sopenharmony_ci	case AMAIR_EL1:		*val = read_sysreg_s(SYS_AMAIR_EL12);	break;
978c2ecf20Sopenharmony_ci	case CNTKCTL_EL1:	*val = read_sysreg_s(SYS_CNTKCTL_EL12);	break;
988c2ecf20Sopenharmony_ci	case ELR_EL1:		*val = read_sysreg_s(SYS_ELR_EL12);	break;
998c2ecf20Sopenharmony_ci	case PAR_EL1:		*val = read_sysreg_par();		break;
1008c2ecf20Sopenharmony_ci	case DACR32_EL2:	*val = read_sysreg_s(SYS_DACR32_EL2);	break;
1018c2ecf20Sopenharmony_ci	case IFSR32_EL2:	*val = read_sysreg_s(SYS_IFSR32_EL2);	break;
1028c2ecf20Sopenharmony_ci	case DBGVCR32_EL2:	*val = read_sysreg_s(SYS_DBGVCR32_EL2);	break;
1038c2ecf20Sopenharmony_ci	default:		return false;
1048c2ecf20Sopenharmony_ci	}
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ci	return true;
1078c2ecf20Sopenharmony_ci}
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_cistatic bool __vcpu_write_sys_reg_to_cpu(u64 val, int reg)
1108c2ecf20Sopenharmony_ci{
1118c2ecf20Sopenharmony_ci	/*
1128c2ecf20Sopenharmony_ci	 * System registers listed in the switch are not restored on every
1138c2ecf20Sopenharmony_ci	 * entry to the guest but are only restored on vcpu_load.
1148c2ecf20Sopenharmony_ci	 *
1158c2ecf20Sopenharmony_ci	 * Note that MPIDR_EL1 for the guest is set by KVM via VMPIDR_EL2 but
1168c2ecf20Sopenharmony_ci	 * should never be listed below, because the MPIDR should only be set
1178c2ecf20Sopenharmony_ci	 * once, before running the VCPU, and never changed later.
1188c2ecf20Sopenharmony_ci	 */
1198c2ecf20Sopenharmony_ci	switch (reg) {
1208c2ecf20Sopenharmony_ci	case CSSELR_EL1:	write_sysreg_s(val, SYS_CSSELR_EL1);	break;
1218c2ecf20Sopenharmony_ci	case SCTLR_EL1:		write_sysreg_s(val, SYS_SCTLR_EL12);	break;
1228c2ecf20Sopenharmony_ci	case CPACR_EL1:		write_sysreg_s(val, SYS_CPACR_EL12);	break;
1238c2ecf20Sopenharmony_ci	case TTBR0_EL1:		write_sysreg_s(val, SYS_TTBR0_EL12);	break;
1248c2ecf20Sopenharmony_ci	case TTBR1_EL1:		write_sysreg_s(val, SYS_TTBR1_EL12);	break;
1258c2ecf20Sopenharmony_ci	case TCR_EL1:		write_sysreg_s(val, SYS_TCR_EL12);	break;
1268c2ecf20Sopenharmony_ci	case ESR_EL1:		write_sysreg_s(val, SYS_ESR_EL12);	break;
1278c2ecf20Sopenharmony_ci	case AFSR0_EL1:		write_sysreg_s(val, SYS_AFSR0_EL12);	break;
1288c2ecf20Sopenharmony_ci	case AFSR1_EL1:		write_sysreg_s(val, SYS_AFSR1_EL12);	break;
1298c2ecf20Sopenharmony_ci	case FAR_EL1:		write_sysreg_s(val, SYS_FAR_EL12);	break;
1308c2ecf20Sopenharmony_ci	case MAIR_EL1:		write_sysreg_s(val, SYS_MAIR_EL12);	break;
1318c2ecf20Sopenharmony_ci	case VBAR_EL1:		write_sysreg_s(val, SYS_VBAR_EL12);	break;
1328c2ecf20Sopenharmony_ci	case CONTEXTIDR_EL1:	write_sysreg_s(val, SYS_CONTEXTIDR_EL12);break;
1338c2ecf20Sopenharmony_ci	case TPIDR_EL0:		write_sysreg_s(val, SYS_TPIDR_EL0);	break;
1348c2ecf20Sopenharmony_ci	case TPIDRRO_EL0:	write_sysreg_s(val, SYS_TPIDRRO_EL0);	break;
1358c2ecf20Sopenharmony_ci	case TPIDR_EL1:		write_sysreg_s(val, SYS_TPIDR_EL1);	break;
1368c2ecf20Sopenharmony_ci	case AMAIR_EL1:		write_sysreg_s(val, SYS_AMAIR_EL12);	break;
1378c2ecf20Sopenharmony_ci	case CNTKCTL_EL1:	write_sysreg_s(val, SYS_CNTKCTL_EL12);	break;
1388c2ecf20Sopenharmony_ci	case ELR_EL1:		write_sysreg_s(val, SYS_ELR_EL12);	break;
1398c2ecf20Sopenharmony_ci	case PAR_EL1:		write_sysreg_s(val, SYS_PAR_EL1);	break;
1408c2ecf20Sopenharmony_ci	case DACR32_EL2:	write_sysreg_s(val, SYS_DACR32_EL2);	break;
1418c2ecf20Sopenharmony_ci	case IFSR32_EL2:	write_sysreg_s(val, SYS_IFSR32_EL2);	break;
1428c2ecf20Sopenharmony_ci	case DBGVCR32_EL2:	write_sysreg_s(val, SYS_DBGVCR32_EL2);	break;
1438c2ecf20Sopenharmony_ci	default:		return false;
1448c2ecf20Sopenharmony_ci	}
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_ci	return true;
1478c2ecf20Sopenharmony_ci}
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ciu64 vcpu_read_sys_reg(const struct kvm_vcpu *vcpu, int reg)
1508c2ecf20Sopenharmony_ci{
1518c2ecf20Sopenharmony_ci	u64 val = 0x8badf00d8badf00d;
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_ci	if (vcpu->arch.sysregs_loaded_on_cpu &&
1548c2ecf20Sopenharmony_ci	    __vcpu_read_sys_reg_from_cpu(reg, &val))
1558c2ecf20Sopenharmony_ci		return val;
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ci	return __vcpu_sys_reg(vcpu, reg);
1588c2ecf20Sopenharmony_ci}
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_civoid vcpu_write_sys_reg(struct kvm_vcpu *vcpu, u64 val, int reg)
1618c2ecf20Sopenharmony_ci{
1628c2ecf20Sopenharmony_ci	if (vcpu->arch.sysregs_loaded_on_cpu &&
1638c2ecf20Sopenharmony_ci	    __vcpu_write_sys_reg_to_cpu(val, reg))
1648c2ecf20Sopenharmony_ci		return;
1658c2ecf20Sopenharmony_ci
1668c2ecf20Sopenharmony_ci	 __vcpu_sys_reg(vcpu, reg) = val;
1678c2ecf20Sopenharmony_ci}
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci/* 3 bits per cache level, as per CLIDR, but non-existent caches always 0 */
1708c2ecf20Sopenharmony_cistatic u32 cache_levels;
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_ci/* CSSELR values; used to index KVM_REG_ARM_DEMUX_ID_CCSIDR */
1738c2ecf20Sopenharmony_ci#define CSSELR_MAX 12
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_ci/* Which cache CCSIDR represents depends on CSSELR value. */
1768c2ecf20Sopenharmony_cistatic u32 get_ccsidr(u32 csselr)
1778c2ecf20Sopenharmony_ci{
1788c2ecf20Sopenharmony_ci	u32 ccsidr;
1798c2ecf20Sopenharmony_ci
1808c2ecf20Sopenharmony_ci	/* Make sure noone else changes CSSELR during this! */
1818c2ecf20Sopenharmony_ci	local_irq_disable();
1828c2ecf20Sopenharmony_ci	write_sysreg(csselr, csselr_el1);
1838c2ecf20Sopenharmony_ci	isb();
1848c2ecf20Sopenharmony_ci	ccsidr = read_sysreg(ccsidr_el1);
1858c2ecf20Sopenharmony_ci	local_irq_enable();
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_ci	return ccsidr;
1888c2ecf20Sopenharmony_ci}
1898c2ecf20Sopenharmony_ci
1908c2ecf20Sopenharmony_ci/*
1918c2ecf20Sopenharmony_ci * See note at ARMv7 ARM B1.14.4 (TL;DR: S/W ops are not easily virtualized).
1928c2ecf20Sopenharmony_ci */
1938c2ecf20Sopenharmony_cistatic bool access_dcsw(struct kvm_vcpu *vcpu,
1948c2ecf20Sopenharmony_ci			struct sys_reg_params *p,
1958c2ecf20Sopenharmony_ci			const struct sys_reg_desc *r)
1968c2ecf20Sopenharmony_ci{
1978c2ecf20Sopenharmony_ci	if (!p->is_write)
1988c2ecf20Sopenharmony_ci		return read_from_write_only(vcpu, p, r);
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_ci	/*
2018c2ecf20Sopenharmony_ci	 * Only track S/W ops if we don't have FWB. It still indicates
2028c2ecf20Sopenharmony_ci	 * that the guest is a bit broken (S/W operations should only
2038c2ecf20Sopenharmony_ci	 * be done by firmware, knowing that there is only a single
2048c2ecf20Sopenharmony_ci	 * CPU left in the system, and certainly not from non-secure
2058c2ecf20Sopenharmony_ci	 * software).
2068c2ecf20Sopenharmony_ci	 */
2078c2ecf20Sopenharmony_ci	if (!cpus_have_const_cap(ARM64_HAS_STAGE2_FWB))
2088c2ecf20Sopenharmony_ci		kvm_set_way_flush(vcpu);
2098c2ecf20Sopenharmony_ci
2108c2ecf20Sopenharmony_ci	return true;
2118c2ecf20Sopenharmony_ci}
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_ci/*
2148c2ecf20Sopenharmony_ci * Generic accessor for VM registers. Only called as long as HCR_TVM
2158c2ecf20Sopenharmony_ci * is set. If the guest enables the MMU, we stop trapping the VM
2168c2ecf20Sopenharmony_ci * sys_regs and leave it in complete control of the caches.
2178c2ecf20Sopenharmony_ci */
2188c2ecf20Sopenharmony_cistatic bool access_vm_reg(struct kvm_vcpu *vcpu,
2198c2ecf20Sopenharmony_ci			  struct sys_reg_params *p,
2208c2ecf20Sopenharmony_ci			  const struct sys_reg_desc *r)
2218c2ecf20Sopenharmony_ci{
2228c2ecf20Sopenharmony_ci	bool was_enabled = vcpu_has_cache_enabled(vcpu);
2238c2ecf20Sopenharmony_ci	u64 val;
2248c2ecf20Sopenharmony_ci	int reg = r->reg;
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_ci	BUG_ON(!p->is_write);
2278c2ecf20Sopenharmony_ci
2288c2ecf20Sopenharmony_ci	/* See the 32bit mapping in kvm_host.h */
2298c2ecf20Sopenharmony_ci	if (p->is_aarch32)
2308c2ecf20Sopenharmony_ci		reg = r->reg / 2;
2318c2ecf20Sopenharmony_ci
2328c2ecf20Sopenharmony_ci	if (!p->is_aarch32 || !p->is_32bit) {
2338c2ecf20Sopenharmony_ci		val = p->regval;
2348c2ecf20Sopenharmony_ci	} else {
2358c2ecf20Sopenharmony_ci		val = vcpu_read_sys_reg(vcpu, reg);
2368c2ecf20Sopenharmony_ci		if (r->reg % 2)
2378c2ecf20Sopenharmony_ci			val = (p->regval << 32) | (u64)lower_32_bits(val);
2388c2ecf20Sopenharmony_ci		else
2398c2ecf20Sopenharmony_ci			val = ((u64)upper_32_bits(val) << 32) |
2408c2ecf20Sopenharmony_ci				lower_32_bits(p->regval);
2418c2ecf20Sopenharmony_ci	}
2428c2ecf20Sopenharmony_ci	vcpu_write_sys_reg(vcpu, val, reg);
2438c2ecf20Sopenharmony_ci
2448c2ecf20Sopenharmony_ci	kvm_toggle_cache(vcpu, was_enabled);
2458c2ecf20Sopenharmony_ci	return true;
2468c2ecf20Sopenharmony_ci}
2478c2ecf20Sopenharmony_ci
2488c2ecf20Sopenharmony_cistatic bool access_actlr(struct kvm_vcpu *vcpu,
2498c2ecf20Sopenharmony_ci			 struct sys_reg_params *p,
2508c2ecf20Sopenharmony_ci			 const struct sys_reg_desc *r)
2518c2ecf20Sopenharmony_ci{
2528c2ecf20Sopenharmony_ci	if (p->is_write)
2538c2ecf20Sopenharmony_ci		return ignore_write(vcpu, p);
2548c2ecf20Sopenharmony_ci
2558c2ecf20Sopenharmony_ci	p->regval = vcpu_read_sys_reg(vcpu, ACTLR_EL1);
2568c2ecf20Sopenharmony_ci
2578c2ecf20Sopenharmony_ci	if (p->is_aarch32) {
2588c2ecf20Sopenharmony_ci		if (r->Op2 & 2)
2598c2ecf20Sopenharmony_ci			p->regval = upper_32_bits(p->regval);
2608c2ecf20Sopenharmony_ci		else
2618c2ecf20Sopenharmony_ci			p->regval = lower_32_bits(p->regval);
2628c2ecf20Sopenharmony_ci	}
2638c2ecf20Sopenharmony_ci
2648c2ecf20Sopenharmony_ci	return true;
2658c2ecf20Sopenharmony_ci}
2668c2ecf20Sopenharmony_ci
2678c2ecf20Sopenharmony_ci/*
2688c2ecf20Sopenharmony_ci * Trap handler for the GICv3 SGI generation system register.
2698c2ecf20Sopenharmony_ci * Forward the request to the VGIC emulation.
2708c2ecf20Sopenharmony_ci * The cp15_64 code makes sure this automatically works
2718c2ecf20Sopenharmony_ci * for both AArch64 and AArch32 accesses.
2728c2ecf20Sopenharmony_ci */
2738c2ecf20Sopenharmony_cistatic bool access_gic_sgi(struct kvm_vcpu *vcpu,
2748c2ecf20Sopenharmony_ci			   struct sys_reg_params *p,
2758c2ecf20Sopenharmony_ci			   const struct sys_reg_desc *r)
2768c2ecf20Sopenharmony_ci{
2778c2ecf20Sopenharmony_ci	bool g1;
2788c2ecf20Sopenharmony_ci
2798c2ecf20Sopenharmony_ci	if (!kvm_has_gicv3(vcpu->kvm)) {
2808c2ecf20Sopenharmony_ci		kvm_inject_undefined(vcpu);
2818c2ecf20Sopenharmony_ci		return false;
2828c2ecf20Sopenharmony_ci	}
2838c2ecf20Sopenharmony_ci
2848c2ecf20Sopenharmony_ci	if (!p->is_write)
2858c2ecf20Sopenharmony_ci		return read_from_write_only(vcpu, p, r);
2868c2ecf20Sopenharmony_ci
2878c2ecf20Sopenharmony_ci	/*
2888c2ecf20Sopenharmony_ci	 * In a system where GICD_CTLR.DS=1, a ICC_SGI0R_EL1 access generates
2898c2ecf20Sopenharmony_ci	 * Group0 SGIs only, while ICC_SGI1R_EL1 can generate either group,
2908c2ecf20Sopenharmony_ci	 * depending on the SGI configuration. ICC_ASGI1R_EL1 is effectively
2918c2ecf20Sopenharmony_ci	 * equivalent to ICC_SGI0R_EL1, as there is no "alternative" secure
2928c2ecf20Sopenharmony_ci	 * group.
2938c2ecf20Sopenharmony_ci	 */
2948c2ecf20Sopenharmony_ci	if (p->is_aarch32) {
2958c2ecf20Sopenharmony_ci		switch (p->Op1) {
2968c2ecf20Sopenharmony_ci		default:		/* Keep GCC quiet */
2978c2ecf20Sopenharmony_ci		case 0:			/* ICC_SGI1R */
2988c2ecf20Sopenharmony_ci			g1 = true;
2998c2ecf20Sopenharmony_ci			break;
3008c2ecf20Sopenharmony_ci		case 1:			/* ICC_ASGI1R */
3018c2ecf20Sopenharmony_ci		case 2:			/* ICC_SGI0R */
3028c2ecf20Sopenharmony_ci			g1 = false;
3038c2ecf20Sopenharmony_ci			break;
3048c2ecf20Sopenharmony_ci		}
3058c2ecf20Sopenharmony_ci	} else {
3068c2ecf20Sopenharmony_ci		switch (p->Op2) {
3078c2ecf20Sopenharmony_ci		default:		/* Keep GCC quiet */
3088c2ecf20Sopenharmony_ci		case 5:			/* ICC_SGI1R_EL1 */
3098c2ecf20Sopenharmony_ci			g1 = true;
3108c2ecf20Sopenharmony_ci			break;
3118c2ecf20Sopenharmony_ci		case 6:			/* ICC_ASGI1R_EL1 */
3128c2ecf20Sopenharmony_ci		case 7:			/* ICC_SGI0R_EL1 */
3138c2ecf20Sopenharmony_ci			g1 = false;
3148c2ecf20Sopenharmony_ci			break;
3158c2ecf20Sopenharmony_ci		}
3168c2ecf20Sopenharmony_ci	}
3178c2ecf20Sopenharmony_ci
3188c2ecf20Sopenharmony_ci	vgic_v3_dispatch_sgi(vcpu, p->regval, g1);
3198c2ecf20Sopenharmony_ci
3208c2ecf20Sopenharmony_ci	return true;
3218c2ecf20Sopenharmony_ci}
3228c2ecf20Sopenharmony_ci
3238c2ecf20Sopenharmony_cistatic bool access_gic_sre(struct kvm_vcpu *vcpu,
3248c2ecf20Sopenharmony_ci			   struct sys_reg_params *p,
3258c2ecf20Sopenharmony_ci			   const struct sys_reg_desc *r)
3268c2ecf20Sopenharmony_ci{
3278c2ecf20Sopenharmony_ci	if (p->is_write)
3288c2ecf20Sopenharmony_ci		return ignore_write(vcpu, p);
3298c2ecf20Sopenharmony_ci
3308c2ecf20Sopenharmony_ci	p->regval = vcpu->arch.vgic_cpu.vgic_v3.vgic_sre;
3318c2ecf20Sopenharmony_ci	return true;
3328c2ecf20Sopenharmony_ci}
3338c2ecf20Sopenharmony_ci
3348c2ecf20Sopenharmony_cistatic bool trap_raz_wi(struct kvm_vcpu *vcpu,
3358c2ecf20Sopenharmony_ci			struct sys_reg_params *p,
3368c2ecf20Sopenharmony_ci			const struct sys_reg_desc *r)
3378c2ecf20Sopenharmony_ci{
3388c2ecf20Sopenharmony_ci	if (p->is_write)
3398c2ecf20Sopenharmony_ci		return ignore_write(vcpu, p);
3408c2ecf20Sopenharmony_ci	else
3418c2ecf20Sopenharmony_ci		return read_zero(vcpu, p);
3428c2ecf20Sopenharmony_ci}
3438c2ecf20Sopenharmony_ci
3448c2ecf20Sopenharmony_ci/*
3458c2ecf20Sopenharmony_ci * ARMv8.1 mandates at least a trivial LORegion implementation, where all the
3468c2ecf20Sopenharmony_ci * RW registers are RES0 (which we can implement as RAZ/WI). On an ARMv8.0
3478c2ecf20Sopenharmony_ci * system, these registers should UNDEF. LORID_EL1 being a RO register, we
3488c2ecf20Sopenharmony_ci * treat it separately.
3498c2ecf20Sopenharmony_ci */
3508c2ecf20Sopenharmony_cistatic bool trap_loregion(struct kvm_vcpu *vcpu,
3518c2ecf20Sopenharmony_ci			  struct sys_reg_params *p,
3528c2ecf20Sopenharmony_ci			  const struct sys_reg_desc *r)
3538c2ecf20Sopenharmony_ci{
3548c2ecf20Sopenharmony_ci	u64 val = read_sanitised_ftr_reg(SYS_ID_AA64MMFR1_EL1);
3558c2ecf20Sopenharmony_ci	u32 sr = sys_reg((u32)r->Op0, (u32)r->Op1,
3568c2ecf20Sopenharmony_ci			 (u32)r->CRn, (u32)r->CRm, (u32)r->Op2);
3578c2ecf20Sopenharmony_ci
3588c2ecf20Sopenharmony_ci	if (!(val & (0xfUL << ID_AA64MMFR1_LOR_SHIFT))) {
3598c2ecf20Sopenharmony_ci		kvm_inject_undefined(vcpu);
3608c2ecf20Sopenharmony_ci		return false;
3618c2ecf20Sopenharmony_ci	}
3628c2ecf20Sopenharmony_ci
3638c2ecf20Sopenharmony_ci	if (p->is_write && sr == SYS_LORID_EL1)
3648c2ecf20Sopenharmony_ci		return write_to_read_only(vcpu, p, r);
3658c2ecf20Sopenharmony_ci
3668c2ecf20Sopenharmony_ci	return trap_raz_wi(vcpu, p, r);
3678c2ecf20Sopenharmony_ci}
3688c2ecf20Sopenharmony_ci
3698c2ecf20Sopenharmony_cistatic bool trap_oslsr_el1(struct kvm_vcpu *vcpu,
3708c2ecf20Sopenharmony_ci			   struct sys_reg_params *p,
3718c2ecf20Sopenharmony_ci			   const struct sys_reg_desc *r)
3728c2ecf20Sopenharmony_ci{
3738c2ecf20Sopenharmony_ci	if (p->is_write) {
3748c2ecf20Sopenharmony_ci		return ignore_write(vcpu, p);
3758c2ecf20Sopenharmony_ci	} else {
3768c2ecf20Sopenharmony_ci		p->regval = (1 << 3);
3778c2ecf20Sopenharmony_ci		return true;
3788c2ecf20Sopenharmony_ci	}
3798c2ecf20Sopenharmony_ci}
3808c2ecf20Sopenharmony_ci
3818c2ecf20Sopenharmony_cistatic bool trap_dbgauthstatus_el1(struct kvm_vcpu *vcpu,
3828c2ecf20Sopenharmony_ci				   struct sys_reg_params *p,
3838c2ecf20Sopenharmony_ci				   const struct sys_reg_desc *r)
3848c2ecf20Sopenharmony_ci{
3858c2ecf20Sopenharmony_ci	if (p->is_write) {
3868c2ecf20Sopenharmony_ci		return ignore_write(vcpu, p);
3878c2ecf20Sopenharmony_ci	} else {
3888c2ecf20Sopenharmony_ci		p->regval = read_sysreg(dbgauthstatus_el1);
3898c2ecf20Sopenharmony_ci		return true;
3908c2ecf20Sopenharmony_ci	}
3918c2ecf20Sopenharmony_ci}
3928c2ecf20Sopenharmony_ci
3938c2ecf20Sopenharmony_ci/*
3948c2ecf20Sopenharmony_ci * We want to avoid world-switching all the DBG registers all the
3958c2ecf20Sopenharmony_ci * time:
3968c2ecf20Sopenharmony_ci *
3978c2ecf20Sopenharmony_ci * - If we've touched any debug register, it is likely that we're
3988c2ecf20Sopenharmony_ci *   going to touch more of them. It then makes sense to disable the
3998c2ecf20Sopenharmony_ci *   traps and start doing the save/restore dance
4008c2ecf20Sopenharmony_ci * - If debug is active (DBG_MDSCR_KDE or DBG_MDSCR_MDE set), it is
4018c2ecf20Sopenharmony_ci *   then mandatory to save/restore the registers, as the guest
4028c2ecf20Sopenharmony_ci *   depends on them.
4038c2ecf20Sopenharmony_ci *
4048c2ecf20Sopenharmony_ci * For this, we use a DIRTY bit, indicating the guest has modified the
4058c2ecf20Sopenharmony_ci * debug registers, used as follow:
4068c2ecf20Sopenharmony_ci *
4078c2ecf20Sopenharmony_ci * On guest entry:
4088c2ecf20Sopenharmony_ci * - If the dirty bit is set (because we're coming back from trapping),
4098c2ecf20Sopenharmony_ci *   disable the traps, save host registers, restore guest registers.
4108c2ecf20Sopenharmony_ci * - If debug is actively in use (DBG_MDSCR_KDE or DBG_MDSCR_MDE set),
4118c2ecf20Sopenharmony_ci *   set the dirty bit, disable the traps, save host registers,
4128c2ecf20Sopenharmony_ci *   restore guest registers.
4138c2ecf20Sopenharmony_ci * - Otherwise, enable the traps
4148c2ecf20Sopenharmony_ci *
4158c2ecf20Sopenharmony_ci * On guest exit:
4168c2ecf20Sopenharmony_ci * - If the dirty bit is set, save guest registers, restore host
4178c2ecf20Sopenharmony_ci *   registers and clear the dirty bit. This ensure that the host can
4188c2ecf20Sopenharmony_ci *   now use the debug registers.
4198c2ecf20Sopenharmony_ci */
4208c2ecf20Sopenharmony_cistatic bool trap_debug_regs(struct kvm_vcpu *vcpu,
4218c2ecf20Sopenharmony_ci			    struct sys_reg_params *p,
4228c2ecf20Sopenharmony_ci			    const struct sys_reg_desc *r)
4238c2ecf20Sopenharmony_ci{
4248c2ecf20Sopenharmony_ci	if (p->is_write) {
4258c2ecf20Sopenharmony_ci		vcpu_write_sys_reg(vcpu, p->regval, r->reg);
4268c2ecf20Sopenharmony_ci		vcpu->arch.flags |= KVM_ARM64_DEBUG_DIRTY;
4278c2ecf20Sopenharmony_ci	} else {
4288c2ecf20Sopenharmony_ci		p->regval = vcpu_read_sys_reg(vcpu, r->reg);
4298c2ecf20Sopenharmony_ci	}
4308c2ecf20Sopenharmony_ci
4318c2ecf20Sopenharmony_ci	trace_trap_reg(__func__, r->reg, p->is_write, p->regval);
4328c2ecf20Sopenharmony_ci
4338c2ecf20Sopenharmony_ci	return true;
4348c2ecf20Sopenharmony_ci}
4358c2ecf20Sopenharmony_ci
4368c2ecf20Sopenharmony_ci/*
4378c2ecf20Sopenharmony_ci * reg_to_dbg/dbg_to_reg
4388c2ecf20Sopenharmony_ci *
4398c2ecf20Sopenharmony_ci * A 32 bit write to a debug register leave top bits alone
4408c2ecf20Sopenharmony_ci * A 32 bit read from a debug register only returns the bottom bits
4418c2ecf20Sopenharmony_ci *
4428c2ecf20Sopenharmony_ci * All writes will set the KVM_ARM64_DEBUG_DIRTY flag to ensure the
4438c2ecf20Sopenharmony_ci * hyp.S code switches between host and guest values in future.
4448c2ecf20Sopenharmony_ci */
4458c2ecf20Sopenharmony_cistatic void reg_to_dbg(struct kvm_vcpu *vcpu,
4468c2ecf20Sopenharmony_ci		       struct sys_reg_params *p,
4478c2ecf20Sopenharmony_ci		       u64 *dbg_reg)
4488c2ecf20Sopenharmony_ci{
4498c2ecf20Sopenharmony_ci	u64 val = p->regval;
4508c2ecf20Sopenharmony_ci
4518c2ecf20Sopenharmony_ci	if (p->is_32bit) {
4528c2ecf20Sopenharmony_ci		val &= 0xffffffffUL;
4538c2ecf20Sopenharmony_ci		val |= ((*dbg_reg >> 32) << 32);
4548c2ecf20Sopenharmony_ci	}
4558c2ecf20Sopenharmony_ci
4568c2ecf20Sopenharmony_ci	*dbg_reg = val;
4578c2ecf20Sopenharmony_ci	vcpu->arch.flags |= KVM_ARM64_DEBUG_DIRTY;
4588c2ecf20Sopenharmony_ci}
4598c2ecf20Sopenharmony_ci
4608c2ecf20Sopenharmony_cistatic void dbg_to_reg(struct kvm_vcpu *vcpu,
4618c2ecf20Sopenharmony_ci		       struct sys_reg_params *p,
4628c2ecf20Sopenharmony_ci		       u64 *dbg_reg)
4638c2ecf20Sopenharmony_ci{
4648c2ecf20Sopenharmony_ci	p->regval = *dbg_reg;
4658c2ecf20Sopenharmony_ci	if (p->is_32bit)
4668c2ecf20Sopenharmony_ci		p->regval &= 0xffffffffUL;
4678c2ecf20Sopenharmony_ci}
4688c2ecf20Sopenharmony_ci
4698c2ecf20Sopenharmony_cistatic bool trap_bvr(struct kvm_vcpu *vcpu,
4708c2ecf20Sopenharmony_ci		     struct sys_reg_params *p,
4718c2ecf20Sopenharmony_ci		     const struct sys_reg_desc *rd)
4728c2ecf20Sopenharmony_ci{
4738c2ecf20Sopenharmony_ci	u64 *dbg_reg = &vcpu->arch.vcpu_debug_state.dbg_bvr[rd->CRm];
4748c2ecf20Sopenharmony_ci
4758c2ecf20Sopenharmony_ci	if (p->is_write)
4768c2ecf20Sopenharmony_ci		reg_to_dbg(vcpu, p, dbg_reg);
4778c2ecf20Sopenharmony_ci	else
4788c2ecf20Sopenharmony_ci		dbg_to_reg(vcpu, p, dbg_reg);
4798c2ecf20Sopenharmony_ci
4808c2ecf20Sopenharmony_ci	trace_trap_reg(__func__, rd->CRm, p->is_write, *dbg_reg);
4818c2ecf20Sopenharmony_ci
4828c2ecf20Sopenharmony_ci	return true;
4838c2ecf20Sopenharmony_ci}
4848c2ecf20Sopenharmony_ci
4858c2ecf20Sopenharmony_cistatic int set_bvr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd,
4868c2ecf20Sopenharmony_ci		const struct kvm_one_reg *reg, void __user *uaddr)
4878c2ecf20Sopenharmony_ci{
4888c2ecf20Sopenharmony_ci	__u64 *r = &vcpu->arch.vcpu_debug_state.dbg_bvr[rd->CRm];
4898c2ecf20Sopenharmony_ci
4908c2ecf20Sopenharmony_ci	if (copy_from_user(r, uaddr, KVM_REG_SIZE(reg->id)) != 0)
4918c2ecf20Sopenharmony_ci		return -EFAULT;
4928c2ecf20Sopenharmony_ci	return 0;
4938c2ecf20Sopenharmony_ci}
4948c2ecf20Sopenharmony_ci
4958c2ecf20Sopenharmony_cistatic int get_bvr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd,
4968c2ecf20Sopenharmony_ci	const struct kvm_one_reg *reg, void __user *uaddr)
4978c2ecf20Sopenharmony_ci{
4988c2ecf20Sopenharmony_ci	__u64 *r = &vcpu->arch.vcpu_debug_state.dbg_bvr[rd->CRm];
4998c2ecf20Sopenharmony_ci
5008c2ecf20Sopenharmony_ci	if (copy_to_user(uaddr, r, KVM_REG_SIZE(reg->id)) != 0)
5018c2ecf20Sopenharmony_ci		return -EFAULT;
5028c2ecf20Sopenharmony_ci	return 0;
5038c2ecf20Sopenharmony_ci}
5048c2ecf20Sopenharmony_ci
5058c2ecf20Sopenharmony_cistatic void reset_bvr(struct kvm_vcpu *vcpu,
5068c2ecf20Sopenharmony_ci		      const struct sys_reg_desc *rd)
5078c2ecf20Sopenharmony_ci{
5088c2ecf20Sopenharmony_ci	vcpu->arch.vcpu_debug_state.dbg_bvr[rd->CRm] = rd->val;
5098c2ecf20Sopenharmony_ci}
5108c2ecf20Sopenharmony_ci
5118c2ecf20Sopenharmony_cistatic bool trap_bcr(struct kvm_vcpu *vcpu,
5128c2ecf20Sopenharmony_ci		     struct sys_reg_params *p,
5138c2ecf20Sopenharmony_ci		     const struct sys_reg_desc *rd)
5148c2ecf20Sopenharmony_ci{
5158c2ecf20Sopenharmony_ci	u64 *dbg_reg = &vcpu->arch.vcpu_debug_state.dbg_bcr[rd->CRm];
5168c2ecf20Sopenharmony_ci
5178c2ecf20Sopenharmony_ci	if (p->is_write)
5188c2ecf20Sopenharmony_ci		reg_to_dbg(vcpu, p, dbg_reg);
5198c2ecf20Sopenharmony_ci	else
5208c2ecf20Sopenharmony_ci		dbg_to_reg(vcpu, p, dbg_reg);
5218c2ecf20Sopenharmony_ci
5228c2ecf20Sopenharmony_ci	trace_trap_reg(__func__, rd->CRm, p->is_write, *dbg_reg);
5238c2ecf20Sopenharmony_ci
5248c2ecf20Sopenharmony_ci	return true;
5258c2ecf20Sopenharmony_ci}
5268c2ecf20Sopenharmony_ci
5278c2ecf20Sopenharmony_cistatic int set_bcr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd,
5288c2ecf20Sopenharmony_ci		const struct kvm_one_reg *reg, void __user *uaddr)
5298c2ecf20Sopenharmony_ci{
5308c2ecf20Sopenharmony_ci	__u64 *r = &vcpu->arch.vcpu_debug_state.dbg_bcr[rd->CRm];
5318c2ecf20Sopenharmony_ci
5328c2ecf20Sopenharmony_ci	if (copy_from_user(r, uaddr, KVM_REG_SIZE(reg->id)) != 0)
5338c2ecf20Sopenharmony_ci		return -EFAULT;
5348c2ecf20Sopenharmony_ci
5358c2ecf20Sopenharmony_ci	return 0;
5368c2ecf20Sopenharmony_ci}
5378c2ecf20Sopenharmony_ci
5388c2ecf20Sopenharmony_cistatic int get_bcr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd,
5398c2ecf20Sopenharmony_ci	const struct kvm_one_reg *reg, void __user *uaddr)
5408c2ecf20Sopenharmony_ci{
5418c2ecf20Sopenharmony_ci	__u64 *r = &vcpu->arch.vcpu_debug_state.dbg_bcr[rd->CRm];
5428c2ecf20Sopenharmony_ci
5438c2ecf20Sopenharmony_ci	if (copy_to_user(uaddr, r, KVM_REG_SIZE(reg->id)) != 0)
5448c2ecf20Sopenharmony_ci		return -EFAULT;
5458c2ecf20Sopenharmony_ci	return 0;
5468c2ecf20Sopenharmony_ci}
5478c2ecf20Sopenharmony_ci
5488c2ecf20Sopenharmony_cistatic void reset_bcr(struct kvm_vcpu *vcpu,
5498c2ecf20Sopenharmony_ci		      const struct sys_reg_desc *rd)
5508c2ecf20Sopenharmony_ci{
5518c2ecf20Sopenharmony_ci	vcpu->arch.vcpu_debug_state.dbg_bcr[rd->CRm] = rd->val;
5528c2ecf20Sopenharmony_ci}
5538c2ecf20Sopenharmony_ci
5548c2ecf20Sopenharmony_cistatic bool trap_wvr(struct kvm_vcpu *vcpu,
5558c2ecf20Sopenharmony_ci		     struct sys_reg_params *p,
5568c2ecf20Sopenharmony_ci		     const struct sys_reg_desc *rd)
5578c2ecf20Sopenharmony_ci{
5588c2ecf20Sopenharmony_ci	u64 *dbg_reg = &vcpu->arch.vcpu_debug_state.dbg_wvr[rd->CRm];
5598c2ecf20Sopenharmony_ci
5608c2ecf20Sopenharmony_ci	if (p->is_write)
5618c2ecf20Sopenharmony_ci		reg_to_dbg(vcpu, p, dbg_reg);
5628c2ecf20Sopenharmony_ci	else
5638c2ecf20Sopenharmony_ci		dbg_to_reg(vcpu, p, dbg_reg);
5648c2ecf20Sopenharmony_ci
5658c2ecf20Sopenharmony_ci	trace_trap_reg(__func__, rd->CRm, p->is_write,
5668c2ecf20Sopenharmony_ci		vcpu->arch.vcpu_debug_state.dbg_wvr[rd->CRm]);
5678c2ecf20Sopenharmony_ci
5688c2ecf20Sopenharmony_ci	return true;
5698c2ecf20Sopenharmony_ci}
5708c2ecf20Sopenharmony_ci
5718c2ecf20Sopenharmony_cistatic int set_wvr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd,
5728c2ecf20Sopenharmony_ci		const struct kvm_one_reg *reg, void __user *uaddr)
5738c2ecf20Sopenharmony_ci{
5748c2ecf20Sopenharmony_ci	__u64 *r = &vcpu->arch.vcpu_debug_state.dbg_wvr[rd->CRm];
5758c2ecf20Sopenharmony_ci
5768c2ecf20Sopenharmony_ci	if (copy_from_user(r, uaddr, KVM_REG_SIZE(reg->id)) != 0)
5778c2ecf20Sopenharmony_ci		return -EFAULT;
5788c2ecf20Sopenharmony_ci	return 0;
5798c2ecf20Sopenharmony_ci}
5808c2ecf20Sopenharmony_ci
5818c2ecf20Sopenharmony_cistatic int get_wvr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd,
5828c2ecf20Sopenharmony_ci	const struct kvm_one_reg *reg, void __user *uaddr)
5838c2ecf20Sopenharmony_ci{
5848c2ecf20Sopenharmony_ci	__u64 *r = &vcpu->arch.vcpu_debug_state.dbg_wvr[rd->CRm];
5858c2ecf20Sopenharmony_ci
5868c2ecf20Sopenharmony_ci	if (copy_to_user(uaddr, r, KVM_REG_SIZE(reg->id)) != 0)
5878c2ecf20Sopenharmony_ci		return -EFAULT;
5888c2ecf20Sopenharmony_ci	return 0;
5898c2ecf20Sopenharmony_ci}
5908c2ecf20Sopenharmony_ci
5918c2ecf20Sopenharmony_cistatic void reset_wvr(struct kvm_vcpu *vcpu,
5928c2ecf20Sopenharmony_ci		      const struct sys_reg_desc *rd)
5938c2ecf20Sopenharmony_ci{
5948c2ecf20Sopenharmony_ci	vcpu->arch.vcpu_debug_state.dbg_wvr[rd->CRm] = rd->val;
5958c2ecf20Sopenharmony_ci}
5968c2ecf20Sopenharmony_ci
5978c2ecf20Sopenharmony_cistatic bool trap_wcr(struct kvm_vcpu *vcpu,
5988c2ecf20Sopenharmony_ci		     struct sys_reg_params *p,
5998c2ecf20Sopenharmony_ci		     const struct sys_reg_desc *rd)
6008c2ecf20Sopenharmony_ci{
6018c2ecf20Sopenharmony_ci	u64 *dbg_reg = &vcpu->arch.vcpu_debug_state.dbg_wcr[rd->CRm];
6028c2ecf20Sopenharmony_ci
6038c2ecf20Sopenharmony_ci	if (p->is_write)
6048c2ecf20Sopenharmony_ci		reg_to_dbg(vcpu, p, dbg_reg);
6058c2ecf20Sopenharmony_ci	else
6068c2ecf20Sopenharmony_ci		dbg_to_reg(vcpu, p, dbg_reg);
6078c2ecf20Sopenharmony_ci
6088c2ecf20Sopenharmony_ci	trace_trap_reg(__func__, rd->CRm, p->is_write, *dbg_reg);
6098c2ecf20Sopenharmony_ci
6108c2ecf20Sopenharmony_ci	return true;
6118c2ecf20Sopenharmony_ci}
6128c2ecf20Sopenharmony_ci
6138c2ecf20Sopenharmony_cistatic int set_wcr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd,
6148c2ecf20Sopenharmony_ci		const struct kvm_one_reg *reg, void __user *uaddr)
6158c2ecf20Sopenharmony_ci{
6168c2ecf20Sopenharmony_ci	__u64 *r = &vcpu->arch.vcpu_debug_state.dbg_wcr[rd->CRm];
6178c2ecf20Sopenharmony_ci
6188c2ecf20Sopenharmony_ci	if (copy_from_user(r, uaddr, KVM_REG_SIZE(reg->id)) != 0)
6198c2ecf20Sopenharmony_ci		return -EFAULT;
6208c2ecf20Sopenharmony_ci	return 0;
6218c2ecf20Sopenharmony_ci}
6228c2ecf20Sopenharmony_ci
6238c2ecf20Sopenharmony_cistatic int get_wcr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd,
6248c2ecf20Sopenharmony_ci	const struct kvm_one_reg *reg, void __user *uaddr)
6258c2ecf20Sopenharmony_ci{
6268c2ecf20Sopenharmony_ci	__u64 *r = &vcpu->arch.vcpu_debug_state.dbg_wcr[rd->CRm];
6278c2ecf20Sopenharmony_ci
6288c2ecf20Sopenharmony_ci	if (copy_to_user(uaddr, r, KVM_REG_SIZE(reg->id)) != 0)
6298c2ecf20Sopenharmony_ci		return -EFAULT;
6308c2ecf20Sopenharmony_ci	return 0;
6318c2ecf20Sopenharmony_ci}
6328c2ecf20Sopenharmony_ci
6338c2ecf20Sopenharmony_cistatic void reset_wcr(struct kvm_vcpu *vcpu,
6348c2ecf20Sopenharmony_ci		      const struct sys_reg_desc *rd)
6358c2ecf20Sopenharmony_ci{
6368c2ecf20Sopenharmony_ci	vcpu->arch.vcpu_debug_state.dbg_wcr[rd->CRm] = rd->val;
6378c2ecf20Sopenharmony_ci}
6388c2ecf20Sopenharmony_ci
6398c2ecf20Sopenharmony_cistatic void reset_amair_el1(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r)
6408c2ecf20Sopenharmony_ci{
6418c2ecf20Sopenharmony_ci	u64 amair = read_sysreg(amair_el1);
6428c2ecf20Sopenharmony_ci	vcpu_write_sys_reg(vcpu, amair, AMAIR_EL1);
6438c2ecf20Sopenharmony_ci}
6448c2ecf20Sopenharmony_ci
6458c2ecf20Sopenharmony_cistatic void reset_actlr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r)
6468c2ecf20Sopenharmony_ci{
6478c2ecf20Sopenharmony_ci	u64 actlr = read_sysreg(actlr_el1);
6488c2ecf20Sopenharmony_ci	vcpu_write_sys_reg(vcpu, actlr, ACTLR_EL1);
6498c2ecf20Sopenharmony_ci}
6508c2ecf20Sopenharmony_ci
6518c2ecf20Sopenharmony_cistatic void reset_mpidr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r)
6528c2ecf20Sopenharmony_ci{
6538c2ecf20Sopenharmony_ci	u64 mpidr;
6548c2ecf20Sopenharmony_ci
6558c2ecf20Sopenharmony_ci	/*
6568c2ecf20Sopenharmony_ci	 * Map the vcpu_id into the first three affinity level fields of
6578c2ecf20Sopenharmony_ci	 * the MPIDR. We limit the number of VCPUs in level 0 due to a
6588c2ecf20Sopenharmony_ci	 * limitation to 16 CPUs in that level in the ICC_SGIxR registers
6598c2ecf20Sopenharmony_ci	 * of the GICv3 to be able to address each CPU directly when
6608c2ecf20Sopenharmony_ci	 * sending IPIs.
6618c2ecf20Sopenharmony_ci	 */
6628c2ecf20Sopenharmony_ci	mpidr = (vcpu->vcpu_id & 0x0f) << MPIDR_LEVEL_SHIFT(0);
6638c2ecf20Sopenharmony_ci	mpidr |= ((vcpu->vcpu_id >> 4) & 0xff) << MPIDR_LEVEL_SHIFT(1);
6648c2ecf20Sopenharmony_ci	mpidr |= ((vcpu->vcpu_id >> 12) & 0xff) << MPIDR_LEVEL_SHIFT(2);
6658c2ecf20Sopenharmony_ci	vcpu_write_sys_reg(vcpu, (1ULL << 31) | mpidr, MPIDR_EL1);
6668c2ecf20Sopenharmony_ci}
6678c2ecf20Sopenharmony_ci
6688c2ecf20Sopenharmony_cistatic void reset_pmcr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r)
6698c2ecf20Sopenharmony_ci{
6708c2ecf20Sopenharmony_ci	u64 pmcr, val;
6718c2ecf20Sopenharmony_ci
6728c2ecf20Sopenharmony_ci	/* No PMU available, PMCR_EL0 may UNDEF... */
6738c2ecf20Sopenharmony_ci	if (!kvm_arm_support_pmu_v3())
6748c2ecf20Sopenharmony_ci		return;
6758c2ecf20Sopenharmony_ci
6768c2ecf20Sopenharmony_ci	pmcr = read_sysreg(pmcr_el0);
6778c2ecf20Sopenharmony_ci	/*
6788c2ecf20Sopenharmony_ci	 * Writable bits of PMCR_EL0 (ARMV8_PMU_PMCR_MASK) are reset to UNKNOWN
6798c2ecf20Sopenharmony_ci	 * except PMCR.E resetting to zero.
6808c2ecf20Sopenharmony_ci	 */
6818c2ecf20Sopenharmony_ci	val = ((pmcr & ~ARMV8_PMU_PMCR_MASK)
6828c2ecf20Sopenharmony_ci	       | (ARMV8_PMU_PMCR_MASK & 0xdecafbad)) & (~ARMV8_PMU_PMCR_E);
6838c2ecf20Sopenharmony_ci	if (!system_supports_32bit_el0())
6848c2ecf20Sopenharmony_ci		val |= ARMV8_PMU_PMCR_LC;
6858c2ecf20Sopenharmony_ci	__vcpu_sys_reg(vcpu, r->reg) = val;
6868c2ecf20Sopenharmony_ci}
6878c2ecf20Sopenharmony_ci
6888c2ecf20Sopenharmony_cistatic bool check_pmu_access_disabled(struct kvm_vcpu *vcpu, u64 flags)
6898c2ecf20Sopenharmony_ci{
6908c2ecf20Sopenharmony_ci	u64 reg = __vcpu_sys_reg(vcpu, PMUSERENR_EL0);
6918c2ecf20Sopenharmony_ci	bool enabled = (reg & flags) || vcpu_mode_priv(vcpu);
6928c2ecf20Sopenharmony_ci
6938c2ecf20Sopenharmony_ci	if (!enabled)
6948c2ecf20Sopenharmony_ci		kvm_inject_undefined(vcpu);
6958c2ecf20Sopenharmony_ci
6968c2ecf20Sopenharmony_ci	return !enabled;
6978c2ecf20Sopenharmony_ci}
6988c2ecf20Sopenharmony_ci
6998c2ecf20Sopenharmony_cistatic bool pmu_access_el0_disabled(struct kvm_vcpu *vcpu)
7008c2ecf20Sopenharmony_ci{
7018c2ecf20Sopenharmony_ci	return check_pmu_access_disabled(vcpu, ARMV8_PMU_USERENR_EN);
7028c2ecf20Sopenharmony_ci}
7038c2ecf20Sopenharmony_ci
7048c2ecf20Sopenharmony_cistatic bool pmu_write_swinc_el0_disabled(struct kvm_vcpu *vcpu)
7058c2ecf20Sopenharmony_ci{
7068c2ecf20Sopenharmony_ci	return check_pmu_access_disabled(vcpu, ARMV8_PMU_USERENR_SW | ARMV8_PMU_USERENR_EN);
7078c2ecf20Sopenharmony_ci}
7088c2ecf20Sopenharmony_ci
7098c2ecf20Sopenharmony_cistatic bool pmu_access_cycle_counter_el0_disabled(struct kvm_vcpu *vcpu)
7108c2ecf20Sopenharmony_ci{
7118c2ecf20Sopenharmony_ci	return check_pmu_access_disabled(vcpu, ARMV8_PMU_USERENR_CR | ARMV8_PMU_USERENR_EN);
7128c2ecf20Sopenharmony_ci}
7138c2ecf20Sopenharmony_ci
7148c2ecf20Sopenharmony_cistatic bool pmu_access_event_counter_el0_disabled(struct kvm_vcpu *vcpu)
7158c2ecf20Sopenharmony_ci{
7168c2ecf20Sopenharmony_ci	return check_pmu_access_disabled(vcpu, ARMV8_PMU_USERENR_ER | ARMV8_PMU_USERENR_EN);
7178c2ecf20Sopenharmony_ci}
7188c2ecf20Sopenharmony_ci
7198c2ecf20Sopenharmony_cistatic bool access_pmcr(struct kvm_vcpu *vcpu, struct sys_reg_params *p,
7208c2ecf20Sopenharmony_ci			const struct sys_reg_desc *r)
7218c2ecf20Sopenharmony_ci{
7228c2ecf20Sopenharmony_ci	u64 val;
7238c2ecf20Sopenharmony_ci
7248c2ecf20Sopenharmony_ci	if (!kvm_arm_pmu_v3_ready(vcpu))
7258c2ecf20Sopenharmony_ci		return trap_raz_wi(vcpu, p, r);
7268c2ecf20Sopenharmony_ci
7278c2ecf20Sopenharmony_ci	if (pmu_access_el0_disabled(vcpu))
7288c2ecf20Sopenharmony_ci		return false;
7298c2ecf20Sopenharmony_ci
7308c2ecf20Sopenharmony_ci	if (p->is_write) {
7318c2ecf20Sopenharmony_ci		/* Only update writeable bits of PMCR */
7328c2ecf20Sopenharmony_ci		val = __vcpu_sys_reg(vcpu, PMCR_EL0);
7338c2ecf20Sopenharmony_ci		val &= ~ARMV8_PMU_PMCR_MASK;
7348c2ecf20Sopenharmony_ci		val |= p->regval & ARMV8_PMU_PMCR_MASK;
7358c2ecf20Sopenharmony_ci		if (!system_supports_32bit_el0())
7368c2ecf20Sopenharmony_ci			val |= ARMV8_PMU_PMCR_LC;
7378c2ecf20Sopenharmony_ci		__vcpu_sys_reg(vcpu, PMCR_EL0) = val;
7388c2ecf20Sopenharmony_ci		kvm_pmu_handle_pmcr(vcpu, val);
7398c2ecf20Sopenharmony_ci		kvm_vcpu_pmu_restore_guest(vcpu);
7408c2ecf20Sopenharmony_ci	} else {
7418c2ecf20Sopenharmony_ci		/* PMCR.P & PMCR.C are RAZ */
7428c2ecf20Sopenharmony_ci		val = __vcpu_sys_reg(vcpu, PMCR_EL0)
7438c2ecf20Sopenharmony_ci		      & ~(ARMV8_PMU_PMCR_P | ARMV8_PMU_PMCR_C);
7448c2ecf20Sopenharmony_ci		p->regval = val;
7458c2ecf20Sopenharmony_ci	}
7468c2ecf20Sopenharmony_ci
7478c2ecf20Sopenharmony_ci	return true;
7488c2ecf20Sopenharmony_ci}
7498c2ecf20Sopenharmony_ci
7508c2ecf20Sopenharmony_cistatic bool access_pmselr(struct kvm_vcpu *vcpu, struct sys_reg_params *p,
7518c2ecf20Sopenharmony_ci			  const struct sys_reg_desc *r)
7528c2ecf20Sopenharmony_ci{
7538c2ecf20Sopenharmony_ci	if (!kvm_arm_pmu_v3_ready(vcpu))
7548c2ecf20Sopenharmony_ci		return trap_raz_wi(vcpu, p, r);
7558c2ecf20Sopenharmony_ci
7568c2ecf20Sopenharmony_ci	if (pmu_access_event_counter_el0_disabled(vcpu))
7578c2ecf20Sopenharmony_ci		return false;
7588c2ecf20Sopenharmony_ci
7598c2ecf20Sopenharmony_ci	if (p->is_write)
7608c2ecf20Sopenharmony_ci		__vcpu_sys_reg(vcpu, PMSELR_EL0) = p->regval;
7618c2ecf20Sopenharmony_ci	else
7628c2ecf20Sopenharmony_ci		/* return PMSELR.SEL field */
7638c2ecf20Sopenharmony_ci		p->regval = __vcpu_sys_reg(vcpu, PMSELR_EL0)
7648c2ecf20Sopenharmony_ci			    & ARMV8_PMU_COUNTER_MASK;
7658c2ecf20Sopenharmony_ci
7668c2ecf20Sopenharmony_ci	return true;
7678c2ecf20Sopenharmony_ci}
7688c2ecf20Sopenharmony_ci
7698c2ecf20Sopenharmony_cistatic bool access_pmceid(struct kvm_vcpu *vcpu, struct sys_reg_params *p,
7708c2ecf20Sopenharmony_ci			  const struct sys_reg_desc *r)
7718c2ecf20Sopenharmony_ci{
7728c2ecf20Sopenharmony_ci	u64 pmceid;
7738c2ecf20Sopenharmony_ci
7748c2ecf20Sopenharmony_ci	if (!kvm_arm_pmu_v3_ready(vcpu))
7758c2ecf20Sopenharmony_ci		return trap_raz_wi(vcpu, p, r);
7768c2ecf20Sopenharmony_ci
7778c2ecf20Sopenharmony_ci	BUG_ON(p->is_write);
7788c2ecf20Sopenharmony_ci
7798c2ecf20Sopenharmony_ci	if (pmu_access_el0_disabled(vcpu))
7808c2ecf20Sopenharmony_ci		return false;
7818c2ecf20Sopenharmony_ci
7828c2ecf20Sopenharmony_ci	pmceid = kvm_pmu_get_pmceid(vcpu, (p->Op2 & 1));
7838c2ecf20Sopenharmony_ci
7848c2ecf20Sopenharmony_ci	p->regval = pmceid;
7858c2ecf20Sopenharmony_ci
7868c2ecf20Sopenharmony_ci	return true;
7878c2ecf20Sopenharmony_ci}
7888c2ecf20Sopenharmony_ci
7898c2ecf20Sopenharmony_cistatic bool pmu_counter_idx_valid(struct kvm_vcpu *vcpu, u64 idx)
7908c2ecf20Sopenharmony_ci{
7918c2ecf20Sopenharmony_ci	u64 pmcr, val;
7928c2ecf20Sopenharmony_ci
7938c2ecf20Sopenharmony_ci	pmcr = __vcpu_sys_reg(vcpu, PMCR_EL0);
7948c2ecf20Sopenharmony_ci	val = (pmcr >> ARMV8_PMU_PMCR_N_SHIFT) & ARMV8_PMU_PMCR_N_MASK;
7958c2ecf20Sopenharmony_ci	if (idx >= val && idx != ARMV8_PMU_CYCLE_IDX) {
7968c2ecf20Sopenharmony_ci		kvm_inject_undefined(vcpu);
7978c2ecf20Sopenharmony_ci		return false;
7988c2ecf20Sopenharmony_ci	}
7998c2ecf20Sopenharmony_ci
8008c2ecf20Sopenharmony_ci	return true;
8018c2ecf20Sopenharmony_ci}
8028c2ecf20Sopenharmony_ci
8038c2ecf20Sopenharmony_cistatic bool access_pmu_evcntr(struct kvm_vcpu *vcpu,
8048c2ecf20Sopenharmony_ci			      struct sys_reg_params *p,
8058c2ecf20Sopenharmony_ci			      const struct sys_reg_desc *r)
8068c2ecf20Sopenharmony_ci{
8078c2ecf20Sopenharmony_ci	u64 idx;
8088c2ecf20Sopenharmony_ci
8098c2ecf20Sopenharmony_ci	if (!kvm_arm_pmu_v3_ready(vcpu))
8108c2ecf20Sopenharmony_ci		return trap_raz_wi(vcpu, p, r);
8118c2ecf20Sopenharmony_ci
8128c2ecf20Sopenharmony_ci	if (r->CRn == 9 && r->CRm == 13) {
8138c2ecf20Sopenharmony_ci		if (r->Op2 == 2) {
8148c2ecf20Sopenharmony_ci			/* PMXEVCNTR_EL0 */
8158c2ecf20Sopenharmony_ci			if (pmu_access_event_counter_el0_disabled(vcpu))
8168c2ecf20Sopenharmony_ci				return false;
8178c2ecf20Sopenharmony_ci
8188c2ecf20Sopenharmony_ci			idx = __vcpu_sys_reg(vcpu, PMSELR_EL0)
8198c2ecf20Sopenharmony_ci			      & ARMV8_PMU_COUNTER_MASK;
8208c2ecf20Sopenharmony_ci		} else if (r->Op2 == 0) {
8218c2ecf20Sopenharmony_ci			/* PMCCNTR_EL0 */
8228c2ecf20Sopenharmony_ci			if (pmu_access_cycle_counter_el0_disabled(vcpu))
8238c2ecf20Sopenharmony_ci				return false;
8248c2ecf20Sopenharmony_ci
8258c2ecf20Sopenharmony_ci			idx = ARMV8_PMU_CYCLE_IDX;
8268c2ecf20Sopenharmony_ci		} else {
8278c2ecf20Sopenharmony_ci			return false;
8288c2ecf20Sopenharmony_ci		}
8298c2ecf20Sopenharmony_ci	} else if (r->CRn == 0 && r->CRm == 9) {
8308c2ecf20Sopenharmony_ci		/* PMCCNTR */
8318c2ecf20Sopenharmony_ci		if (pmu_access_event_counter_el0_disabled(vcpu))
8328c2ecf20Sopenharmony_ci			return false;
8338c2ecf20Sopenharmony_ci
8348c2ecf20Sopenharmony_ci		idx = ARMV8_PMU_CYCLE_IDX;
8358c2ecf20Sopenharmony_ci	} else if (r->CRn == 14 && (r->CRm & 12) == 8) {
8368c2ecf20Sopenharmony_ci		/* PMEVCNTRn_EL0 */
8378c2ecf20Sopenharmony_ci		if (pmu_access_event_counter_el0_disabled(vcpu))
8388c2ecf20Sopenharmony_ci			return false;
8398c2ecf20Sopenharmony_ci
8408c2ecf20Sopenharmony_ci		idx = ((r->CRm & 3) << 3) | (r->Op2 & 7);
8418c2ecf20Sopenharmony_ci	} else {
8428c2ecf20Sopenharmony_ci		return false;
8438c2ecf20Sopenharmony_ci	}
8448c2ecf20Sopenharmony_ci
8458c2ecf20Sopenharmony_ci	if (!pmu_counter_idx_valid(vcpu, idx))
8468c2ecf20Sopenharmony_ci		return false;
8478c2ecf20Sopenharmony_ci
8488c2ecf20Sopenharmony_ci	if (p->is_write) {
8498c2ecf20Sopenharmony_ci		if (pmu_access_el0_disabled(vcpu))
8508c2ecf20Sopenharmony_ci			return false;
8518c2ecf20Sopenharmony_ci
8528c2ecf20Sopenharmony_ci		kvm_pmu_set_counter_value(vcpu, idx, p->regval);
8538c2ecf20Sopenharmony_ci	} else {
8548c2ecf20Sopenharmony_ci		p->regval = kvm_pmu_get_counter_value(vcpu, idx);
8558c2ecf20Sopenharmony_ci	}
8568c2ecf20Sopenharmony_ci
8578c2ecf20Sopenharmony_ci	return true;
8588c2ecf20Sopenharmony_ci}
8598c2ecf20Sopenharmony_ci
8608c2ecf20Sopenharmony_cistatic bool access_pmu_evtyper(struct kvm_vcpu *vcpu, struct sys_reg_params *p,
8618c2ecf20Sopenharmony_ci			       const struct sys_reg_desc *r)
8628c2ecf20Sopenharmony_ci{
8638c2ecf20Sopenharmony_ci	u64 idx, reg;
8648c2ecf20Sopenharmony_ci
8658c2ecf20Sopenharmony_ci	if (!kvm_arm_pmu_v3_ready(vcpu))
8668c2ecf20Sopenharmony_ci		return trap_raz_wi(vcpu, p, r);
8678c2ecf20Sopenharmony_ci
8688c2ecf20Sopenharmony_ci	if (pmu_access_el0_disabled(vcpu))
8698c2ecf20Sopenharmony_ci		return false;
8708c2ecf20Sopenharmony_ci
8718c2ecf20Sopenharmony_ci	if (r->CRn == 9 && r->CRm == 13 && r->Op2 == 1) {
8728c2ecf20Sopenharmony_ci		/* PMXEVTYPER_EL0 */
8738c2ecf20Sopenharmony_ci		idx = __vcpu_sys_reg(vcpu, PMSELR_EL0) & ARMV8_PMU_COUNTER_MASK;
8748c2ecf20Sopenharmony_ci		reg = PMEVTYPER0_EL0 + idx;
8758c2ecf20Sopenharmony_ci	} else if (r->CRn == 14 && (r->CRm & 12) == 12) {
8768c2ecf20Sopenharmony_ci		idx = ((r->CRm & 3) << 3) | (r->Op2 & 7);
8778c2ecf20Sopenharmony_ci		if (idx == ARMV8_PMU_CYCLE_IDX)
8788c2ecf20Sopenharmony_ci			reg = PMCCFILTR_EL0;
8798c2ecf20Sopenharmony_ci		else
8808c2ecf20Sopenharmony_ci			/* PMEVTYPERn_EL0 */
8818c2ecf20Sopenharmony_ci			reg = PMEVTYPER0_EL0 + idx;
8828c2ecf20Sopenharmony_ci	} else {
8838c2ecf20Sopenharmony_ci		BUG();
8848c2ecf20Sopenharmony_ci	}
8858c2ecf20Sopenharmony_ci
8868c2ecf20Sopenharmony_ci	if (!pmu_counter_idx_valid(vcpu, idx))
8878c2ecf20Sopenharmony_ci		return false;
8888c2ecf20Sopenharmony_ci
8898c2ecf20Sopenharmony_ci	if (p->is_write) {
8908c2ecf20Sopenharmony_ci		kvm_pmu_set_counter_event_type(vcpu, p->regval, idx);
8918c2ecf20Sopenharmony_ci		__vcpu_sys_reg(vcpu, reg) = p->regval & ARMV8_PMU_EVTYPE_MASK;
8928c2ecf20Sopenharmony_ci		kvm_vcpu_pmu_restore_guest(vcpu);
8938c2ecf20Sopenharmony_ci	} else {
8948c2ecf20Sopenharmony_ci		p->regval = __vcpu_sys_reg(vcpu, reg) & ARMV8_PMU_EVTYPE_MASK;
8958c2ecf20Sopenharmony_ci	}
8968c2ecf20Sopenharmony_ci
8978c2ecf20Sopenharmony_ci	return true;
8988c2ecf20Sopenharmony_ci}
8998c2ecf20Sopenharmony_ci
9008c2ecf20Sopenharmony_cistatic bool access_pmcnten(struct kvm_vcpu *vcpu, struct sys_reg_params *p,
9018c2ecf20Sopenharmony_ci			   const struct sys_reg_desc *r)
9028c2ecf20Sopenharmony_ci{
9038c2ecf20Sopenharmony_ci	u64 val, mask;
9048c2ecf20Sopenharmony_ci
9058c2ecf20Sopenharmony_ci	if (!kvm_arm_pmu_v3_ready(vcpu))
9068c2ecf20Sopenharmony_ci		return trap_raz_wi(vcpu, p, r);
9078c2ecf20Sopenharmony_ci
9088c2ecf20Sopenharmony_ci	if (pmu_access_el0_disabled(vcpu))
9098c2ecf20Sopenharmony_ci		return false;
9108c2ecf20Sopenharmony_ci
9118c2ecf20Sopenharmony_ci	mask = kvm_pmu_valid_counter_mask(vcpu);
9128c2ecf20Sopenharmony_ci	if (p->is_write) {
9138c2ecf20Sopenharmony_ci		val = p->regval & mask;
9148c2ecf20Sopenharmony_ci		if (r->Op2 & 0x1) {
9158c2ecf20Sopenharmony_ci			/* accessing PMCNTENSET_EL0 */
9168c2ecf20Sopenharmony_ci			__vcpu_sys_reg(vcpu, PMCNTENSET_EL0) |= val;
9178c2ecf20Sopenharmony_ci			kvm_pmu_enable_counter_mask(vcpu, val);
9188c2ecf20Sopenharmony_ci			kvm_vcpu_pmu_restore_guest(vcpu);
9198c2ecf20Sopenharmony_ci		} else {
9208c2ecf20Sopenharmony_ci			/* accessing PMCNTENCLR_EL0 */
9218c2ecf20Sopenharmony_ci			__vcpu_sys_reg(vcpu, PMCNTENSET_EL0) &= ~val;
9228c2ecf20Sopenharmony_ci			kvm_pmu_disable_counter_mask(vcpu, val);
9238c2ecf20Sopenharmony_ci		}
9248c2ecf20Sopenharmony_ci	} else {
9258c2ecf20Sopenharmony_ci		p->regval = __vcpu_sys_reg(vcpu, PMCNTENSET_EL0) & mask;
9268c2ecf20Sopenharmony_ci	}
9278c2ecf20Sopenharmony_ci
9288c2ecf20Sopenharmony_ci	return true;
9298c2ecf20Sopenharmony_ci}
9308c2ecf20Sopenharmony_ci
9318c2ecf20Sopenharmony_cistatic bool access_pminten(struct kvm_vcpu *vcpu, struct sys_reg_params *p,
9328c2ecf20Sopenharmony_ci			   const struct sys_reg_desc *r)
9338c2ecf20Sopenharmony_ci{
9348c2ecf20Sopenharmony_ci	u64 mask = kvm_pmu_valid_counter_mask(vcpu);
9358c2ecf20Sopenharmony_ci
9368c2ecf20Sopenharmony_ci	if (!kvm_arm_pmu_v3_ready(vcpu))
9378c2ecf20Sopenharmony_ci		return trap_raz_wi(vcpu, p, r);
9388c2ecf20Sopenharmony_ci
9398c2ecf20Sopenharmony_ci	if (!vcpu_mode_priv(vcpu)) {
9408c2ecf20Sopenharmony_ci		kvm_inject_undefined(vcpu);
9418c2ecf20Sopenharmony_ci		return false;
9428c2ecf20Sopenharmony_ci	}
9438c2ecf20Sopenharmony_ci
9448c2ecf20Sopenharmony_ci	if (p->is_write) {
9458c2ecf20Sopenharmony_ci		u64 val = p->regval & mask;
9468c2ecf20Sopenharmony_ci
9478c2ecf20Sopenharmony_ci		if (r->Op2 & 0x1)
9488c2ecf20Sopenharmony_ci			/* accessing PMINTENSET_EL1 */
9498c2ecf20Sopenharmony_ci			__vcpu_sys_reg(vcpu, PMINTENSET_EL1) |= val;
9508c2ecf20Sopenharmony_ci		else
9518c2ecf20Sopenharmony_ci			/* accessing PMINTENCLR_EL1 */
9528c2ecf20Sopenharmony_ci			__vcpu_sys_reg(vcpu, PMINTENSET_EL1) &= ~val;
9538c2ecf20Sopenharmony_ci	} else {
9548c2ecf20Sopenharmony_ci		p->regval = __vcpu_sys_reg(vcpu, PMINTENSET_EL1) & mask;
9558c2ecf20Sopenharmony_ci	}
9568c2ecf20Sopenharmony_ci
9578c2ecf20Sopenharmony_ci	return true;
9588c2ecf20Sopenharmony_ci}
9598c2ecf20Sopenharmony_ci
9608c2ecf20Sopenharmony_cistatic bool access_pmovs(struct kvm_vcpu *vcpu, struct sys_reg_params *p,
9618c2ecf20Sopenharmony_ci			 const struct sys_reg_desc *r)
9628c2ecf20Sopenharmony_ci{
9638c2ecf20Sopenharmony_ci	u64 mask = kvm_pmu_valid_counter_mask(vcpu);
9648c2ecf20Sopenharmony_ci
9658c2ecf20Sopenharmony_ci	if (!kvm_arm_pmu_v3_ready(vcpu))
9668c2ecf20Sopenharmony_ci		return trap_raz_wi(vcpu, p, r);
9678c2ecf20Sopenharmony_ci
9688c2ecf20Sopenharmony_ci	if (pmu_access_el0_disabled(vcpu))
9698c2ecf20Sopenharmony_ci		return false;
9708c2ecf20Sopenharmony_ci
9718c2ecf20Sopenharmony_ci	if (p->is_write) {
9728c2ecf20Sopenharmony_ci		if (r->CRm & 0x2)
9738c2ecf20Sopenharmony_ci			/* accessing PMOVSSET_EL0 */
9748c2ecf20Sopenharmony_ci			__vcpu_sys_reg(vcpu, PMOVSSET_EL0) |= (p->regval & mask);
9758c2ecf20Sopenharmony_ci		else
9768c2ecf20Sopenharmony_ci			/* accessing PMOVSCLR_EL0 */
9778c2ecf20Sopenharmony_ci			__vcpu_sys_reg(vcpu, PMOVSSET_EL0) &= ~(p->regval & mask);
9788c2ecf20Sopenharmony_ci	} else {
9798c2ecf20Sopenharmony_ci		p->regval = __vcpu_sys_reg(vcpu, PMOVSSET_EL0) & mask;
9808c2ecf20Sopenharmony_ci	}
9818c2ecf20Sopenharmony_ci
9828c2ecf20Sopenharmony_ci	return true;
9838c2ecf20Sopenharmony_ci}
9848c2ecf20Sopenharmony_ci
9858c2ecf20Sopenharmony_cistatic bool access_pmswinc(struct kvm_vcpu *vcpu, struct sys_reg_params *p,
9868c2ecf20Sopenharmony_ci			   const struct sys_reg_desc *r)
9878c2ecf20Sopenharmony_ci{
9888c2ecf20Sopenharmony_ci	u64 mask;
9898c2ecf20Sopenharmony_ci
9908c2ecf20Sopenharmony_ci	if (!kvm_arm_pmu_v3_ready(vcpu))
9918c2ecf20Sopenharmony_ci		return trap_raz_wi(vcpu, p, r);
9928c2ecf20Sopenharmony_ci
9938c2ecf20Sopenharmony_ci	if (!p->is_write)
9948c2ecf20Sopenharmony_ci		return read_from_write_only(vcpu, p, r);
9958c2ecf20Sopenharmony_ci
9968c2ecf20Sopenharmony_ci	if (pmu_write_swinc_el0_disabled(vcpu))
9978c2ecf20Sopenharmony_ci		return false;
9988c2ecf20Sopenharmony_ci
9998c2ecf20Sopenharmony_ci	mask = kvm_pmu_valid_counter_mask(vcpu);
10008c2ecf20Sopenharmony_ci	kvm_pmu_software_increment(vcpu, p->regval & mask);
10018c2ecf20Sopenharmony_ci	return true;
10028c2ecf20Sopenharmony_ci}
10038c2ecf20Sopenharmony_ci
10048c2ecf20Sopenharmony_cistatic bool access_pmuserenr(struct kvm_vcpu *vcpu, struct sys_reg_params *p,
10058c2ecf20Sopenharmony_ci			     const struct sys_reg_desc *r)
10068c2ecf20Sopenharmony_ci{
10078c2ecf20Sopenharmony_ci	if (!kvm_arm_pmu_v3_ready(vcpu))
10088c2ecf20Sopenharmony_ci		return trap_raz_wi(vcpu, p, r);
10098c2ecf20Sopenharmony_ci
10108c2ecf20Sopenharmony_ci	if (p->is_write) {
10118c2ecf20Sopenharmony_ci		if (!vcpu_mode_priv(vcpu)) {
10128c2ecf20Sopenharmony_ci			kvm_inject_undefined(vcpu);
10138c2ecf20Sopenharmony_ci			return false;
10148c2ecf20Sopenharmony_ci		}
10158c2ecf20Sopenharmony_ci
10168c2ecf20Sopenharmony_ci		__vcpu_sys_reg(vcpu, PMUSERENR_EL0) =
10178c2ecf20Sopenharmony_ci			       p->regval & ARMV8_PMU_USERENR_MASK;
10188c2ecf20Sopenharmony_ci	} else {
10198c2ecf20Sopenharmony_ci		p->regval = __vcpu_sys_reg(vcpu, PMUSERENR_EL0)
10208c2ecf20Sopenharmony_ci			    & ARMV8_PMU_USERENR_MASK;
10218c2ecf20Sopenharmony_ci	}
10228c2ecf20Sopenharmony_ci
10238c2ecf20Sopenharmony_ci	return true;
10248c2ecf20Sopenharmony_ci}
10258c2ecf20Sopenharmony_ci
10268c2ecf20Sopenharmony_ci#define reg_to_encoding(x)						\
10278c2ecf20Sopenharmony_ci	sys_reg((u32)(x)->Op0, (u32)(x)->Op1,				\
10288c2ecf20Sopenharmony_ci		(u32)(x)->CRn, (u32)(x)->CRm, (u32)(x)->Op2);
10298c2ecf20Sopenharmony_ci
10308c2ecf20Sopenharmony_ci/* Silly macro to expand the DBG{BCR,BVR,WVR,WCR}n_EL1 registers in one go */
10318c2ecf20Sopenharmony_ci#define DBG_BCR_BVR_WCR_WVR_EL1(n)					\
10328c2ecf20Sopenharmony_ci	{ SYS_DESC(SYS_DBGBVRn_EL1(n)),					\
10338c2ecf20Sopenharmony_ci	  trap_bvr, reset_bvr, 0, 0, get_bvr, set_bvr },		\
10348c2ecf20Sopenharmony_ci	{ SYS_DESC(SYS_DBGBCRn_EL1(n)),					\
10358c2ecf20Sopenharmony_ci	  trap_bcr, reset_bcr, 0, 0, get_bcr, set_bcr },		\
10368c2ecf20Sopenharmony_ci	{ SYS_DESC(SYS_DBGWVRn_EL1(n)),					\
10378c2ecf20Sopenharmony_ci	  trap_wvr, reset_wvr, 0, 0,  get_wvr, set_wvr },		\
10388c2ecf20Sopenharmony_ci	{ SYS_DESC(SYS_DBGWCRn_EL1(n)),					\
10398c2ecf20Sopenharmony_ci	  trap_wcr, reset_wcr, 0, 0,  get_wcr, set_wcr }
10408c2ecf20Sopenharmony_ci
10418c2ecf20Sopenharmony_ci/* Macro to expand the PMEVCNTRn_EL0 register */
10428c2ecf20Sopenharmony_ci#define PMU_PMEVCNTR_EL0(n)						\
10438c2ecf20Sopenharmony_ci	{ SYS_DESC(SYS_PMEVCNTRn_EL0(n)),					\
10448c2ecf20Sopenharmony_ci	  access_pmu_evcntr, reset_unknown, (PMEVCNTR0_EL0 + n), }
10458c2ecf20Sopenharmony_ci
10468c2ecf20Sopenharmony_ci/* Macro to expand the PMEVTYPERn_EL0 register */
10478c2ecf20Sopenharmony_ci#define PMU_PMEVTYPER_EL0(n)						\
10488c2ecf20Sopenharmony_ci	{ SYS_DESC(SYS_PMEVTYPERn_EL0(n)),					\
10498c2ecf20Sopenharmony_ci	  access_pmu_evtyper, reset_unknown, (PMEVTYPER0_EL0 + n), }
10508c2ecf20Sopenharmony_ci
10518c2ecf20Sopenharmony_cistatic bool undef_access(struct kvm_vcpu *vcpu, struct sys_reg_params *p,
10528c2ecf20Sopenharmony_ci			 const struct sys_reg_desc *r)
10538c2ecf20Sopenharmony_ci{
10548c2ecf20Sopenharmony_ci	kvm_inject_undefined(vcpu);
10558c2ecf20Sopenharmony_ci
10568c2ecf20Sopenharmony_ci	return false;
10578c2ecf20Sopenharmony_ci}
10588c2ecf20Sopenharmony_ci
10598c2ecf20Sopenharmony_ci/* Macro to expand the AMU counter and type registers*/
10608c2ecf20Sopenharmony_ci#define AMU_AMEVCNTR0_EL0(n) { SYS_DESC(SYS_AMEVCNTR0_EL0(n)), undef_access }
10618c2ecf20Sopenharmony_ci#define AMU_AMEVTYPER0_EL0(n) { SYS_DESC(SYS_AMEVTYPER0_EL0(n)), undef_access }
10628c2ecf20Sopenharmony_ci#define AMU_AMEVCNTR1_EL0(n) { SYS_DESC(SYS_AMEVCNTR1_EL0(n)), undef_access }
10638c2ecf20Sopenharmony_ci#define AMU_AMEVTYPER1_EL0(n) { SYS_DESC(SYS_AMEVTYPER1_EL0(n)), undef_access }
10648c2ecf20Sopenharmony_ci
10658c2ecf20Sopenharmony_cistatic unsigned int ptrauth_visibility(const struct kvm_vcpu *vcpu,
10668c2ecf20Sopenharmony_ci			const struct sys_reg_desc *rd)
10678c2ecf20Sopenharmony_ci{
10688c2ecf20Sopenharmony_ci	return vcpu_has_ptrauth(vcpu) ? 0 : REG_HIDDEN;
10698c2ecf20Sopenharmony_ci}
10708c2ecf20Sopenharmony_ci
10718c2ecf20Sopenharmony_ci/*
10728c2ecf20Sopenharmony_ci * If we land here on a PtrAuth access, that is because we didn't
10738c2ecf20Sopenharmony_ci * fixup the access on exit by allowing the PtrAuth sysregs. The only
10748c2ecf20Sopenharmony_ci * way this happens is when the guest does not have PtrAuth support
10758c2ecf20Sopenharmony_ci * enabled.
10768c2ecf20Sopenharmony_ci */
10778c2ecf20Sopenharmony_ci#define __PTRAUTH_KEY(k)						\
10788c2ecf20Sopenharmony_ci	{ SYS_DESC(SYS_## k), undef_access, reset_unknown, k,		\
10798c2ecf20Sopenharmony_ci	.visibility = ptrauth_visibility}
10808c2ecf20Sopenharmony_ci
10818c2ecf20Sopenharmony_ci#define PTRAUTH_KEY(k)							\
10828c2ecf20Sopenharmony_ci	__PTRAUTH_KEY(k ## KEYLO_EL1),					\
10838c2ecf20Sopenharmony_ci	__PTRAUTH_KEY(k ## KEYHI_EL1)
10848c2ecf20Sopenharmony_ci
10858c2ecf20Sopenharmony_cistatic bool access_arch_timer(struct kvm_vcpu *vcpu,
10868c2ecf20Sopenharmony_ci			      struct sys_reg_params *p,
10878c2ecf20Sopenharmony_ci			      const struct sys_reg_desc *r)
10888c2ecf20Sopenharmony_ci{
10898c2ecf20Sopenharmony_ci	enum kvm_arch_timers tmr;
10908c2ecf20Sopenharmony_ci	enum kvm_arch_timer_regs treg;
10918c2ecf20Sopenharmony_ci	u64 reg = reg_to_encoding(r);
10928c2ecf20Sopenharmony_ci
10938c2ecf20Sopenharmony_ci	switch (reg) {
10948c2ecf20Sopenharmony_ci	case SYS_CNTP_TVAL_EL0:
10958c2ecf20Sopenharmony_ci	case SYS_AARCH32_CNTP_TVAL:
10968c2ecf20Sopenharmony_ci		tmr = TIMER_PTIMER;
10978c2ecf20Sopenharmony_ci		treg = TIMER_REG_TVAL;
10988c2ecf20Sopenharmony_ci		break;
10998c2ecf20Sopenharmony_ci	case SYS_CNTP_CTL_EL0:
11008c2ecf20Sopenharmony_ci	case SYS_AARCH32_CNTP_CTL:
11018c2ecf20Sopenharmony_ci		tmr = TIMER_PTIMER;
11028c2ecf20Sopenharmony_ci		treg = TIMER_REG_CTL;
11038c2ecf20Sopenharmony_ci		break;
11048c2ecf20Sopenharmony_ci	case SYS_CNTP_CVAL_EL0:
11058c2ecf20Sopenharmony_ci	case SYS_AARCH32_CNTP_CVAL:
11068c2ecf20Sopenharmony_ci		tmr = TIMER_PTIMER;
11078c2ecf20Sopenharmony_ci		treg = TIMER_REG_CVAL;
11088c2ecf20Sopenharmony_ci		break;
11098c2ecf20Sopenharmony_ci	default:
11108c2ecf20Sopenharmony_ci		BUG();
11118c2ecf20Sopenharmony_ci	}
11128c2ecf20Sopenharmony_ci
11138c2ecf20Sopenharmony_ci	if (p->is_write)
11148c2ecf20Sopenharmony_ci		kvm_arm_timer_write_sysreg(vcpu, tmr, treg, p->regval);
11158c2ecf20Sopenharmony_ci	else
11168c2ecf20Sopenharmony_ci		p->regval = kvm_arm_timer_read_sysreg(vcpu, tmr, treg);
11178c2ecf20Sopenharmony_ci
11188c2ecf20Sopenharmony_ci	return true;
11198c2ecf20Sopenharmony_ci}
11208c2ecf20Sopenharmony_ci
11218c2ecf20Sopenharmony_ci/* Read a sanitised cpufeature ID register by sys_reg_desc */
11228c2ecf20Sopenharmony_cistatic u64 read_id_reg(const struct kvm_vcpu *vcpu,
11238c2ecf20Sopenharmony_ci		struct sys_reg_desc const *r, bool raz)
11248c2ecf20Sopenharmony_ci{
11258c2ecf20Sopenharmony_ci	u32 id = sys_reg((u32)r->Op0, (u32)r->Op1,
11268c2ecf20Sopenharmony_ci			 (u32)r->CRn, (u32)r->CRm, (u32)r->Op2);
11278c2ecf20Sopenharmony_ci	u64 val = raz ? 0 : read_sanitised_ftr_reg(id);
11288c2ecf20Sopenharmony_ci
11298c2ecf20Sopenharmony_ci	if (id == SYS_ID_AA64PFR0_EL1) {
11308c2ecf20Sopenharmony_ci		if (!vcpu_has_sve(vcpu))
11318c2ecf20Sopenharmony_ci			val &= ~(0xfUL << ID_AA64PFR0_SVE_SHIFT);
11328c2ecf20Sopenharmony_ci		val &= ~(0xfUL << ID_AA64PFR0_AMU_SHIFT);
11338c2ecf20Sopenharmony_ci		val &= ~(0xfUL << ID_AA64PFR0_CSV2_SHIFT);
11348c2ecf20Sopenharmony_ci		val |= ((u64)vcpu->kvm->arch.pfr0_csv2 << ID_AA64PFR0_CSV2_SHIFT);
11358c2ecf20Sopenharmony_ci	} else if (id == SYS_ID_AA64PFR1_EL1) {
11368c2ecf20Sopenharmony_ci		val &= ~(0xfUL << ID_AA64PFR1_MTE_SHIFT);
11378c2ecf20Sopenharmony_ci	} else if (id == SYS_ID_AA64ISAR1_EL1 && !vcpu_has_ptrauth(vcpu)) {
11388c2ecf20Sopenharmony_ci		val &= ~((0xfUL << ID_AA64ISAR1_APA_SHIFT) |
11398c2ecf20Sopenharmony_ci			 (0xfUL << ID_AA64ISAR1_API_SHIFT) |
11408c2ecf20Sopenharmony_ci			 (0xfUL << ID_AA64ISAR1_GPA_SHIFT) |
11418c2ecf20Sopenharmony_ci			 (0xfUL << ID_AA64ISAR1_GPI_SHIFT));
11428c2ecf20Sopenharmony_ci	} else if (id == SYS_ID_AA64DFR0_EL1) {
11438c2ecf20Sopenharmony_ci		/* Limit guests to PMUv3 for ARMv8.1 */
11448c2ecf20Sopenharmony_ci		val = cpuid_feature_cap_perfmon_field(val,
11458c2ecf20Sopenharmony_ci						ID_AA64DFR0_PMUVER_SHIFT,
11468c2ecf20Sopenharmony_ci						ID_AA64DFR0_PMUVER_8_1);
11478c2ecf20Sopenharmony_ci	} else if (id == SYS_ID_DFR0_EL1) {
11488c2ecf20Sopenharmony_ci		/* Limit guests to PMUv3 for ARMv8.1 */
11498c2ecf20Sopenharmony_ci		val = cpuid_feature_cap_perfmon_field(val,
11508c2ecf20Sopenharmony_ci						ID_DFR0_PERFMON_SHIFT,
11518c2ecf20Sopenharmony_ci						ID_DFR0_PERFMON_8_1);
11528c2ecf20Sopenharmony_ci	}
11538c2ecf20Sopenharmony_ci
11548c2ecf20Sopenharmony_ci	return val;
11558c2ecf20Sopenharmony_ci}
11568c2ecf20Sopenharmony_ci
11578c2ecf20Sopenharmony_cistatic unsigned int id_visibility(const struct kvm_vcpu *vcpu,
11588c2ecf20Sopenharmony_ci				  const struct sys_reg_desc *r)
11598c2ecf20Sopenharmony_ci{
11608c2ecf20Sopenharmony_ci	u32 id = sys_reg((u32)r->Op0, (u32)r->Op1,
11618c2ecf20Sopenharmony_ci			 (u32)r->CRn, (u32)r->CRm, (u32)r->Op2);
11628c2ecf20Sopenharmony_ci
11638c2ecf20Sopenharmony_ci	switch (id) {
11648c2ecf20Sopenharmony_ci	case SYS_ID_AA64ZFR0_EL1:
11658c2ecf20Sopenharmony_ci		if (!vcpu_has_sve(vcpu))
11668c2ecf20Sopenharmony_ci			return REG_RAZ;
11678c2ecf20Sopenharmony_ci		break;
11688c2ecf20Sopenharmony_ci	}
11698c2ecf20Sopenharmony_ci
11708c2ecf20Sopenharmony_ci	return 0;
11718c2ecf20Sopenharmony_ci}
11728c2ecf20Sopenharmony_ci
11738c2ecf20Sopenharmony_ci/* cpufeature ID register access trap handlers */
11748c2ecf20Sopenharmony_ci
11758c2ecf20Sopenharmony_cistatic bool __access_id_reg(struct kvm_vcpu *vcpu,
11768c2ecf20Sopenharmony_ci			    struct sys_reg_params *p,
11778c2ecf20Sopenharmony_ci			    const struct sys_reg_desc *r,
11788c2ecf20Sopenharmony_ci			    bool raz)
11798c2ecf20Sopenharmony_ci{
11808c2ecf20Sopenharmony_ci	if (p->is_write)
11818c2ecf20Sopenharmony_ci		return write_to_read_only(vcpu, p, r);
11828c2ecf20Sopenharmony_ci
11838c2ecf20Sopenharmony_ci	p->regval = read_id_reg(vcpu, r, raz);
11848c2ecf20Sopenharmony_ci	return true;
11858c2ecf20Sopenharmony_ci}
11868c2ecf20Sopenharmony_ci
11878c2ecf20Sopenharmony_cistatic bool access_id_reg(struct kvm_vcpu *vcpu,
11888c2ecf20Sopenharmony_ci			  struct sys_reg_params *p,
11898c2ecf20Sopenharmony_ci			  const struct sys_reg_desc *r)
11908c2ecf20Sopenharmony_ci{
11918c2ecf20Sopenharmony_ci	bool raz = sysreg_visible_as_raz(vcpu, r);
11928c2ecf20Sopenharmony_ci
11938c2ecf20Sopenharmony_ci	return __access_id_reg(vcpu, p, r, raz);
11948c2ecf20Sopenharmony_ci}
11958c2ecf20Sopenharmony_ci
11968c2ecf20Sopenharmony_cistatic bool access_raz_id_reg(struct kvm_vcpu *vcpu,
11978c2ecf20Sopenharmony_ci			      struct sys_reg_params *p,
11988c2ecf20Sopenharmony_ci			      const struct sys_reg_desc *r)
11998c2ecf20Sopenharmony_ci{
12008c2ecf20Sopenharmony_ci	return __access_id_reg(vcpu, p, r, true);
12018c2ecf20Sopenharmony_ci}
12028c2ecf20Sopenharmony_ci
12038c2ecf20Sopenharmony_cistatic int reg_from_user(u64 *val, const void __user *uaddr, u64 id);
12048c2ecf20Sopenharmony_cistatic int reg_to_user(void __user *uaddr, const u64 *val, u64 id);
12058c2ecf20Sopenharmony_cistatic u64 sys_reg_to_index(const struct sys_reg_desc *reg);
12068c2ecf20Sopenharmony_ci
12078c2ecf20Sopenharmony_ci/* Visibility overrides for SVE-specific control registers */
12088c2ecf20Sopenharmony_cistatic unsigned int sve_visibility(const struct kvm_vcpu *vcpu,
12098c2ecf20Sopenharmony_ci				   const struct sys_reg_desc *rd)
12108c2ecf20Sopenharmony_ci{
12118c2ecf20Sopenharmony_ci	if (vcpu_has_sve(vcpu))
12128c2ecf20Sopenharmony_ci		return 0;
12138c2ecf20Sopenharmony_ci
12148c2ecf20Sopenharmony_ci	return REG_HIDDEN;
12158c2ecf20Sopenharmony_ci}
12168c2ecf20Sopenharmony_ci
12178c2ecf20Sopenharmony_cistatic int set_id_aa64pfr0_el1(struct kvm_vcpu *vcpu,
12188c2ecf20Sopenharmony_ci			       const struct sys_reg_desc *rd,
12198c2ecf20Sopenharmony_ci			       const struct kvm_one_reg *reg, void __user *uaddr)
12208c2ecf20Sopenharmony_ci{
12218c2ecf20Sopenharmony_ci	const u64 id = sys_reg_to_index(rd);
12228c2ecf20Sopenharmony_ci	int err;
12238c2ecf20Sopenharmony_ci	u64 val;
12248c2ecf20Sopenharmony_ci	u8 csv2;
12258c2ecf20Sopenharmony_ci
12268c2ecf20Sopenharmony_ci	err = reg_from_user(&val, uaddr, id);
12278c2ecf20Sopenharmony_ci	if (err)
12288c2ecf20Sopenharmony_ci		return err;
12298c2ecf20Sopenharmony_ci
12308c2ecf20Sopenharmony_ci	/*
12318c2ecf20Sopenharmony_ci	 * Allow AA64PFR0_EL1.CSV2 to be set from userspace as long as
12328c2ecf20Sopenharmony_ci	 * it doesn't promise more than what is actually provided (the
12338c2ecf20Sopenharmony_ci	 * guest could otherwise be covered in ectoplasmic residue).
12348c2ecf20Sopenharmony_ci	 */
12358c2ecf20Sopenharmony_ci	csv2 = cpuid_feature_extract_unsigned_field(val, ID_AA64PFR0_CSV2_SHIFT);
12368c2ecf20Sopenharmony_ci	if (csv2 > 1 ||
12378c2ecf20Sopenharmony_ci	    (csv2 && arm64_get_spectre_v2_state() != SPECTRE_UNAFFECTED))
12388c2ecf20Sopenharmony_ci		return -EINVAL;
12398c2ecf20Sopenharmony_ci
12408c2ecf20Sopenharmony_ci	/* We can only differ with CSV2, and anything else is an error */
12418c2ecf20Sopenharmony_ci	val ^= read_id_reg(vcpu, rd, false);
12428c2ecf20Sopenharmony_ci	val &= ~(0xFUL << ID_AA64PFR0_CSV2_SHIFT);
12438c2ecf20Sopenharmony_ci	if (val)
12448c2ecf20Sopenharmony_ci		return -EINVAL;
12458c2ecf20Sopenharmony_ci
12468c2ecf20Sopenharmony_ci	vcpu->kvm->arch.pfr0_csv2 = csv2;
12478c2ecf20Sopenharmony_ci
12488c2ecf20Sopenharmony_ci	return 0;
12498c2ecf20Sopenharmony_ci}
12508c2ecf20Sopenharmony_ci
12518c2ecf20Sopenharmony_ci/*
12528c2ecf20Sopenharmony_ci * cpufeature ID register user accessors
12538c2ecf20Sopenharmony_ci *
12548c2ecf20Sopenharmony_ci * For now, these registers are immutable for userspace, so no values
12558c2ecf20Sopenharmony_ci * are stored, and for set_id_reg() we don't allow the effective value
12568c2ecf20Sopenharmony_ci * to be changed.
12578c2ecf20Sopenharmony_ci */
12588c2ecf20Sopenharmony_cistatic int __get_id_reg(const struct kvm_vcpu *vcpu,
12598c2ecf20Sopenharmony_ci			const struct sys_reg_desc *rd, void __user *uaddr,
12608c2ecf20Sopenharmony_ci			bool raz)
12618c2ecf20Sopenharmony_ci{
12628c2ecf20Sopenharmony_ci	const u64 id = sys_reg_to_index(rd);
12638c2ecf20Sopenharmony_ci	const u64 val = read_id_reg(vcpu, rd, raz);
12648c2ecf20Sopenharmony_ci
12658c2ecf20Sopenharmony_ci	return reg_to_user(uaddr, &val, id);
12668c2ecf20Sopenharmony_ci}
12678c2ecf20Sopenharmony_ci
12688c2ecf20Sopenharmony_cistatic int __set_id_reg(const struct kvm_vcpu *vcpu,
12698c2ecf20Sopenharmony_ci			const struct sys_reg_desc *rd, void __user *uaddr,
12708c2ecf20Sopenharmony_ci			bool raz)
12718c2ecf20Sopenharmony_ci{
12728c2ecf20Sopenharmony_ci	const u64 id = sys_reg_to_index(rd);
12738c2ecf20Sopenharmony_ci	int err;
12748c2ecf20Sopenharmony_ci	u64 val;
12758c2ecf20Sopenharmony_ci
12768c2ecf20Sopenharmony_ci	err = reg_from_user(&val, uaddr, id);
12778c2ecf20Sopenharmony_ci	if (err)
12788c2ecf20Sopenharmony_ci		return err;
12798c2ecf20Sopenharmony_ci
12808c2ecf20Sopenharmony_ci	/* This is what we mean by invariant: you can't change it. */
12818c2ecf20Sopenharmony_ci	if (val != read_id_reg(vcpu, rd, raz))
12828c2ecf20Sopenharmony_ci		return -EINVAL;
12838c2ecf20Sopenharmony_ci
12848c2ecf20Sopenharmony_ci	return 0;
12858c2ecf20Sopenharmony_ci}
12868c2ecf20Sopenharmony_ci
12878c2ecf20Sopenharmony_cistatic int get_id_reg(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd,
12888c2ecf20Sopenharmony_ci		      const struct kvm_one_reg *reg, void __user *uaddr)
12898c2ecf20Sopenharmony_ci{
12908c2ecf20Sopenharmony_ci	bool raz = sysreg_visible_as_raz(vcpu, rd);
12918c2ecf20Sopenharmony_ci
12928c2ecf20Sopenharmony_ci	return __get_id_reg(vcpu, rd, uaddr, raz);
12938c2ecf20Sopenharmony_ci}
12948c2ecf20Sopenharmony_ci
12958c2ecf20Sopenharmony_cistatic int set_id_reg(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd,
12968c2ecf20Sopenharmony_ci		      const struct kvm_one_reg *reg, void __user *uaddr)
12978c2ecf20Sopenharmony_ci{
12988c2ecf20Sopenharmony_ci	bool raz = sysreg_visible_as_raz(vcpu, rd);
12998c2ecf20Sopenharmony_ci
13008c2ecf20Sopenharmony_ci	return __set_id_reg(vcpu, rd, uaddr, raz);
13018c2ecf20Sopenharmony_ci}
13028c2ecf20Sopenharmony_ci
13038c2ecf20Sopenharmony_cistatic int get_raz_id_reg(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd,
13048c2ecf20Sopenharmony_ci			  const struct kvm_one_reg *reg, void __user *uaddr)
13058c2ecf20Sopenharmony_ci{
13068c2ecf20Sopenharmony_ci	return __get_id_reg(vcpu, rd, uaddr, true);
13078c2ecf20Sopenharmony_ci}
13088c2ecf20Sopenharmony_ci
13098c2ecf20Sopenharmony_cistatic int set_raz_id_reg(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd,
13108c2ecf20Sopenharmony_ci			  const struct kvm_one_reg *reg, void __user *uaddr)
13118c2ecf20Sopenharmony_ci{
13128c2ecf20Sopenharmony_ci	return __set_id_reg(vcpu, rd, uaddr, true);
13138c2ecf20Sopenharmony_ci}
13148c2ecf20Sopenharmony_ci
13158c2ecf20Sopenharmony_cistatic bool access_ctr(struct kvm_vcpu *vcpu, struct sys_reg_params *p,
13168c2ecf20Sopenharmony_ci		       const struct sys_reg_desc *r)
13178c2ecf20Sopenharmony_ci{
13188c2ecf20Sopenharmony_ci	if (p->is_write)
13198c2ecf20Sopenharmony_ci		return write_to_read_only(vcpu, p, r);
13208c2ecf20Sopenharmony_ci
13218c2ecf20Sopenharmony_ci	p->regval = read_sanitised_ftr_reg(SYS_CTR_EL0);
13228c2ecf20Sopenharmony_ci	return true;
13238c2ecf20Sopenharmony_ci}
13248c2ecf20Sopenharmony_ci
13258c2ecf20Sopenharmony_cistatic bool access_clidr(struct kvm_vcpu *vcpu, struct sys_reg_params *p,
13268c2ecf20Sopenharmony_ci			 const struct sys_reg_desc *r)
13278c2ecf20Sopenharmony_ci{
13288c2ecf20Sopenharmony_ci	if (p->is_write)
13298c2ecf20Sopenharmony_ci		return write_to_read_only(vcpu, p, r);
13308c2ecf20Sopenharmony_ci
13318c2ecf20Sopenharmony_ci	p->regval = read_sysreg(clidr_el1);
13328c2ecf20Sopenharmony_ci	return true;
13338c2ecf20Sopenharmony_ci}
13348c2ecf20Sopenharmony_ci
13358c2ecf20Sopenharmony_cistatic bool access_csselr(struct kvm_vcpu *vcpu, struct sys_reg_params *p,
13368c2ecf20Sopenharmony_ci			  const struct sys_reg_desc *r)
13378c2ecf20Sopenharmony_ci{
13388c2ecf20Sopenharmony_ci	int reg = r->reg;
13398c2ecf20Sopenharmony_ci
13408c2ecf20Sopenharmony_ci	/* See the 32bit mapping in kvm_host.h */
13418c2ecf20Sopenharmony_ci	if (p->is_aarch32)
13428c2ecf20Sopenharmony_ci		reg = r->reg / 2;
13438c2ecf20Sopenharmony_ci
13448c2ecf20Sopenharmony_ci	if (p->is_write)
13458c2ecf20Sopenharmony_ci		vcpu_write_sys_reg(vcpu, p->regval, reg);
13468c2ecf20Sopenharmony_ci	else
13478c2ecf20Sopenharmony_ci		p->regval = vcpu_read_sys_reg(vcpu, reg);
13488c2ecf20Sopenharmony_ci	return true;
13498c2ecf20Sopenharmony_ci}
13508c2ecf20Sopenharmony_ci
13518c2ecf20Sopenharmony_cistatic bool access_ccsidr(struct kvm_vcpu *vcpu, struct sys_reg_params *p,
13528c2ecf20Sopenharmony_ci			  const struct sys_reg_desc *r)
13538c2ecf20Sopenharmony_ci{
13548c2ecf20Sopenharmony_ci	u32 csselr;
13558c2ecf20Sopenharmony_ci
13568c2ecf20Sopenharmony_ci	if (p->is_write)
13578c2ecf20Sopenharmony_ci		return write_to_read_only(vcpu, p, r);
13588c2ecf20Sopenharmony_ci
13598c2ecf20Sopenharmony_ci	csselr = vcpu_read_sys_reg(vcpu, CSSELR_EL1);
13608c2ecf20Sopenharmony_ci	p->regval = get_ccsidr(csselr);
13618c2ecf20Sopenharmony_ci
13628c2ecf20Sopenharmony_ci	/*
13638c2ecf20Sopenharmony_ci	 * Guests should not be doing cache operations by set/way at all, and
13648c2ecf20Sopenharmony_ci	 * for this reason, we trap them and attempt to infer the intent, so
13658c2ecf20Sopenharmony_ci	 * that we can flush the entire guest's address space at the appropriate
13668c2ecf20Sopenharmony_ci	 * time.
13678c2ecf20Sopenharmony_ci	 * To prevent this trapping from causing performance problems, let's
13688c2ecf20Sopenharmony_ci	 * expose the geometry of all data and unified caches (which are
13698c2ecf20Sopenharmony_ci	 * guaranteed to be PIPT and thus non-aliasing) as 1 set and 1 way.
13708c2ecf20Sopenharmony_ci	 * [If guests should attempt to infer aliasing properties from the
13718c2ecf20Sopenharmony_ci	 * geometry (which is not permitted by the architecture), they would
13728c2ecf20Sopenharmony_ci	 * only do so for virtually indexed caches.]
13738c2ecf20Sopenharmony_ci	 */
13748c2ecf20Sopenharmony_ci	if (!(csselr & 1)) // data or unified cache
13758c2ecf20Sopenharmony_ci		p->regval &= ~GENMASK(27, 3);
13768c2ecf20Sopenharmony_ci	return true;
13778c2ecf20Sopenharmony_ci}
13788c2ecf20Sopenharmony_ci
13798c2ecf20Sopenharmony_ci/* sys_reg_desc initialiser for known cpufeature ID registers */
13808c2ecf20Sopenharmony_ci#define ID_SANITISED(name) {			\
13818c2ecf20Sopenharmony_ci	SYS_DESC(SYS_##name),			\
13828c2ecf20Sopenharmony_ci	.access	= access_id_reg,		\
13838c2ecf20Sopenharmony_ci	.get_user = get_id_reg,			\
13848c2ecf20Sopenharmony_ci	.set_user = set_id_reg,			\
13858c2ecf20Sopenharmony_ci	.visibility = id_visibility,		\
13868c2ecf20Sopenharmony_ci}
13878c2ecf20Sopenharmony_ci
13888c2ecf20Sopenharmony_ci/*
13898c2ecf20Sopenharmony_ci * sys_reg_desc initialiser for architecturally unallocated cpufeature ID
13908c2ecf20Sopenharmony_ci * register with encoding Op0=3, Op1=0, CRn=0, CRm=crm, Op2=op2
13918c2ecf20Sopenharmony_ci * (1 <= crm < 8, 0 <= Op2 < 8).
13928c2ecf20Sopenharmony_ci */
13938c2ecf20Sopenharmony_ci#define ID_UNALLOCATED(crm, op2) {			\
13948c2ecf20Sopenharmony_ci	Op0(3), Op1(0), CRn(0), CRm(crm), Op2(op2),	\
13958c2ecf20Sopenharmony_ci	.access = access_raz_id_reg,			\
13968c2ecf20Sopenharmony_ci	.get_user = get_raz_id_reg,			\
13978c2ecf20Sopenharmony_ci	.set_user = set_raz_id_reg,			\
13988c2ecf20Sopenharmony_ci}
13998c2ecf20Sopenharmony_ci
14008c2ecf20Sopenharmony_ci/*
14018c2ecf20Sopenharmony_ci * sys_reg_desc initialiser for known ID registers that we hide from guests.
14028c2ecf20Sopenharmony_ci * For now, these are exposed just like unallocated ID regs: they appear
14038c2ecf20Sopenharmony_ci * RAZ for the guest.
14048c2ecf20Sopenharmony_ci */
14058c2ecf20Sopenharmony_ci#define ID_HIDDEN(name) {			\
14068c2ecf20Sopenharmony_ci	SYS_DESC(SYS_##name),			\
14078c2ecf20Sopenharmony_ci	.access = access_raz_id_reg,		\
14088c2ecf20Sopenharmony_ci	.get_user = get_raz_id_reg,		\
14098c2ecf20Sopenharmony_ci	.set_user = set_raz_id_reg,		\
14108c2ecf20Sopenharmony_ci}
14118c2ecf20Sopenharmony_ci
14128c2ecf20Sopenharmony_ci/*
14138c2ecf20Sopenharmony_ci * Architected system registers.
14148c2ecf20Sopenharmony_ci * Important: Must be sorted ascending by Op0, Op1, CRn, CRm, Op2
14158c2ecf20Sopenharmony_ci *
14168c2ecf20Sopenharmony_ci * Debug handling: We do trap most, if not all debug related system
14178c2ecf20Sopenharmony_ci * registers. The implementation is good enough to ensure that a guest
14188c2ecf20Sopenharmony_ci * can use these with minimal performance degradation. The drawback is
14198c2ecf20Sopenharmony_ci * that we don't implement any of the external debug, none of the
14208c2ecf20Sopenharmony_ci * OSlock protocol. This should be revisited if we ever encounter a
14218c2ecf20Sopenharmony_ci * more demanding guest...
14228c2ecf20Sopenharmony_ci */
14238c2ecf20Sopenharmony_cistatic const struct sys_reg_desc sys_reg_descs[] = {
14248c2ecf20Sopenharmony_ci	{ SYS_DESC(SYS_DC_ISW), access_dcsw },
14258c2ecf20Sopenharmony_ci	{ SYS_DESC(SYS_DC_CSW), access_dcsw },
14268c2ecf20Sopenharmony_ci	{ SYS_DESC(SYS_DC_CISW), access_dcsw },
14278c2ecf20Sopenharmony_ci
14288c2ecf20Sopenharmony_ci	DBG_BCR_BVR_WCR_WVR_EL1(0),
14298c2ecf20Sopenharmony_ci	DBG_BCR_BVR_WCR_WVR_EL1(1),
14308c2ecf20Sopenharmony_ci	{ SYS_DESC(SYS_MDCCINT_EL1), trap_debug_regs, reset_val, MDCCINT_EL1, 0 },
14318c2ecf20Sopenharmony_ci	{ SYS_DESC(SYS_MDSCR_EL1), trap_debug_regs, reset_val, MDSCR_EL1, 0 },
14328c2ecf20Sopenharmony_ci	DBG_BCR_BVR_WCR_WVR_EL1(2),
14338c2ecf20Sopenharmony_ci	DBG_BCR_BVR_WCR_WVR_EL1(3),
14348c2ecf20Sopenharmony_ci	DBG_BCR_BVR_WCR_WVR_EL1(4),
14358c2ecf20Sopenharmony_ci	DBG_BCR_BVR_WCR_WVR_EL1(5),
14368c2ecf20Sopenharmony_ci	DBG_BCR_BVR_WCR_WVR_EL1(6),
14378c2ecf20Sopenharmony_ci	DBG_BCR_BVR_WCR_WVR_EL1(7),
14388c2ecf20Sopenharmony_ci	DBG_BCR_BVR_WCR_WVR_EL1(8),
14398c2ecf20Sopenharmony_ci	DBG_BCR_BVR_WCR_WVR_EL1(9),
14408c2ecf20Sopenharmony_ci	DBG_BCR_BVR_WCR_WVR_EL1(10),
14418c2ecf20Sopenharmony_ci	DBG_BCR_BVR_WCR_WVR_EL1(11),
14428c2ecf20Sopenharmony_ci	DBG_BCR_BVR_WCR_WVR_EL1(12),
14438c2ecf20Sopenharmony_ci	DBG_BCR_BVR_WCR_WVR_EL1(13),
14448c2ecf20Sopenharmony_ci	DBG_BCR_BVR_WCR_WVR_EL1(14),
14458c2ecf20Sopenharmony_ci	DBG_BCR_BVR_WCR_WVR_EL1(15),
14468c2ecf20Sopenharmony_ci
14478c2ecf20Sopenharmony_ci	{ SYS_DESC(SYS_MDRAR_EL1), trap_raz_wi },
14488c2ecf20Sopenharmony_ci	{ SYS_DESC(SYS_OSLAR_EL1), trap_raz_wi },
14498c2ecf20Sopenharmony_ci	{ SYS_DESC(SYS_OSLSR_EL1), trap_oslsr_el1 },
14508c2ecf20Sopenharmony_ci	{ SYS_DESC(SYS_OSDLR_EL1), trap_raz_wi },
14518c2ecf20Sopenharmony_ci	{ SYS_DESC(SYS_DBGPRCR_EL1), trap_raz_wi },
14528c2ecf20Sopenharmony_ci	{ SYS_DESC(SYS_DBGCLAIMSET_EL1), trap_raz_wi },
14538c2ecf20Sopenharmony_ci	{ SYS_DESC(SYS_DBGCLAIMCLR_EL1), trap_raz_wi },
14548c2ecf20Sopenharmony_ci	{ SYS_DESC(SYS_DBGAUTHSTATUS_EL1), trap_dbgauthstatus_el1 },
14558c2ecf20Sopenharmony_ci
14568c2ecf20Sopenharmony_ci	{ SYS_DESC(SYS_MDCCSR_EL0), trap_raz_wi },
14578c2ecf20Sopenharmony_ci	{ SYS_DESC(SYS_DBGDTR_EL0), trap_raz_wi },
14588c2ecf20Sopenharmony_ci	// DBGDTR[TR]X_EL0 share the same encoding
14598c2ecf20Sopenharmony_ci	{ SYS_DESC(SYS_DBGDTRTX_EL0), trap_raz_wi },
14608c2ecf20Sopenharmony_ci
14618c2ecf20Sopenharmony_ci	{ SYS_DESC(SYS_DBGVCR32_EL2), NULL, reset_val, DBGVCR32_EL2, 0 },
14628c2ecf20Sopenharmony_ci
14638c2ecf20Sopenharmony_ci	{ SYS_DESC(SYS_MPIDR_EL1), NULL, reset_mpidr, MPIDR_EL1 },
14648c2ecf20Sopenharmony_ci
14658c2ecf20Sopenharmony_ci	/*
14668c2ecf20Sopenharmony_ci	 * ID regs: all ID_SANITISED() entries here must have corresponding
14678c2ecf20Sopenharmony_ci	 * entries in arm64_ftr_regs[].
14688c2ecf20Sopenharmony_ci	 */
14698c2ecf20Sopenharmony_ci
14708c2ecf20Sopenharmony_ci	/* AArch64 mappings of the AArch32 ID registers */
14718c2ecf20Sopenharmony_ci	/* CRm=1 */
14728c2ecf20Sopenharmony_ci	ID_SANITISED(ID_PFR0_EL1),
14738c2ecf20Sopenharmony_ci	ID_SANITISED(ID_PFR1_EL1),
14748c2ecf20Sopenharmony_ci	ID_SANITISED(ID_DFR0_EL1),
14758c2ecf20Sopenharmony_ci	ID_HIDDEN(ID_AFR0_EL1),
14768c2ecf20Sopenharmony_ci	ID_SANITISED(ID_MMFR0_EL1),
14778c2ecf20Sopenharmony_ci	ID_SANITISED(ID_MMFR1_EL1),
14788c2ecf20Sopenharmony_ci	ID_SANITISED(ID_MMFR2_EL1),
14798c2ecf20Sopenharmony_ci	ID_SANITISED(ID_MMFR3_EL1),
14808c2ecf20Sopenharmony_ci
14818c2ecf20Sopenharmony_ci	/* CRm=2 */
14828c2ecf20Sopenharmony_ci	ID_SANITISED(ID_ISAR0_EL1),
14838c2ecf20Sopenharmony_ci	ID_SANITISED(ID_ISAR1_EL1),
14848c2ecf20Sopenharmony_ci	ID_SANITISED(ID_ISAR2_EL1),
14858c2ecf20Sopenharmony_ci	ID_SANITISED(ID_ISAR3_EL1),
14868c2ecf20Sopenharmony_ci	ID_SANITISED(ID_ISAR4_EL1),
14878c2ecf20Sopenharmony_ci	ID_SANITISED(ID_ISAR5_EL1),
14888c2ecf20Sopenharmony_ci	ID_SANITISED(ID_MMFR4_EL1),
14898c2ecf20Sopenharmony_ci	ID_SANITISED(ID_ISAR6_EL1),
14908c2ecf20Sopenharmony_ci
14918c2ecf20Sopenharmony_ci	/* CRm=3 */
14928c2ecf20Sopenharmony_ci	ID_SANITISED(MVFR0_EL1),
14938c2ecf20Sopenharmony_ci	ID_SANITISED(MVFR1_EL1),
14948c2ecf20Sopenharmony_ci	ID_SANITISED(MVFR2_EL1),
14958c2ecf20Sopenharmony_ci	ID_UNALLOCATED(3,3),
14968c2ecf20Sopenharmony_ci	ID_SANITISED(ID_PFR2_EL1),
14978c2ecf20Sopenharmony_ci	ID_HIDDEN(ID_DFR1_EL1),
14988c2ecf20Sopenharmony_ci	ID_SANITISED(ID_MMFR5_EL1),
14998c2ecf20Sopenharmony_ci	ID_UNALLOCATED(3,7),
15008c2ecf20Sopenharmony_ci
15018c2ecf20Sopenharmony_ci	/* AArch64 ID registers */
15028c2ecf20Sopenharmony_ci	/* CRm=4 */
15038c2ecf20Sopenharmony_ci	{ SYS_DESC(SYS_ID_AA64PFR0_EL1), .access = access_id_reg,
15048c2ecf20Sopenharmony_ci	  .get_user = get_id_reg, .set_user = set_id_aa64pfr0_el1, },
15058c2ecf20Sopenharmony_ci	ID_SANITISED(ID_AA64PFR1_EL1),
15068c2ecf20Sopenharmony_ci	ID_UNALLOCATED(4,2),
15078c2ecf20Sopenharmony_ci	ID_UNALLOCATED(4,3),
15088c2ecf20Sopenharmony_ci	ID_SANITISED(ID_AA64ZFR0_EL1),
15098c2ecf20Sopenharmony_ci	ID_UNALLOCATED(4,5),
15108c2ecf20Sopenharmony_ci	ID_UNALLOCATED(4,6),
15118c2ecf20Sopenharmony_ci	ID_UNALLOCATED(4,7),
15128c2ecf20Sopenharmony_ci
15138c2ecf20Sopenharmony_ci	/* CRm=5 */
15148c2ecf20Sopenharmony_ci	ID_SANITISED(ID_AA64DFR0_EL1),
15158c2ecf20Sopenharmony_ci	ID_SANITISED(ID_AA64DFR1_EL1),
15168c2ecf20Sopenharmony_ci	ID_UNALLOCATED(5,2),
15178c2ecf20Sopenharmony_ci	ID_UNALLOCATED(5,3),
15188c2ecf20Sopenharmony_ci	ID_HIDDEN(ID_AA64AFR0_EL1),
15198c2ecf20Sopenharmony_ci	ID_HIDDEN(ID_AA64AFR1_EL1),
15208c2ecf20Sopenharmony_ci	ID_UNALLOCATED(5,6),
15218c2ecf20Sopenharmony_ci	ID_UNALLOCATED(5,7),
15228c2ecf20Sopenharmony_ci
15238c2ecf20Sopenharmony_ci	/* CRm=6 */
15248c2ecf20Sopenharmony_ci	ID_SANITISED(ID_AA64ISAR0_EL1),
15258c2ecf20Sopenharmony_ci	ID_SANITISED(ID_AA64ISAR1_EL1),
15268c2ecf20Sopenharmony_ci	ID_SANITISED(ID_AA64ISAR2_EL1),
15278c2ecf20Sopenharmony_ci	ID_UNALLOCATED(6,3),
15288c2ecf20Sopenharmony_ci	ID_UNALLOCATED(6,4),
15298c2ecf20Sopenharmony_ci	ID_UNALLOCATED(6,5),
15308c2ecf20Sopenharmony_ci	ID_UNALLOCATED(6,6),
15318c2ecf20Sopenharmony_ci	ID_UNALLOCATED(6,7),
15328c2ecf20Sopenharmony_ci
15338c2ecf20Sopenharmony_ci	/* CRm=7 */
15348c2ecf20Sopenharmony_ci	ID_SANITISED(ID_AA64MMFR0_EL1),
15358c2ecf20Sopenharmony_ci	ID_SANITISED(ID_AA64MMFR1_EL1),
15368c2ecf20Sopenharmony_ci	ID_SANITISED(ID_AA64MMFR2_EL1),
15378c2ecf20Sopenharmony_ci	ID_UNALLOCATED(7,3),
15388c2ecf20Sopenharmony_ci	ID_UNALLOCATED(7,4),
15398c2ecf20Sopenharmony_ci	ID_UNALLOCATED(7,5),
15408c2ecf20Sopenharmony_ci	ID_UNALLOCATED(7,6),
15418c2ecf20Sopenharmony_ci	ID_UNALLOCATED(7,7),
15428c2ecf20Sopenharmony_ci
15438c2ecf20Sopenharmony_ci	{ SYS_DESC(SYS_SCTLR_EL1), access_vm_reg, reset_val, SCTLR_EL1, 0x00C50078 },
15448c2ecf20Sopenharmony_ci	{ SYS_DESC(SYS_ACTLR_EL1), access_actlr, reset_actlr, ACTLR_EL1 },
15458c2ecf20Sopenharmony_ci	{ SYS_DESC(SYS_CPACR_EL1), NULL, reset_val, CPACR_EL1, 0 },
15468c2ecf20Sopenharmony_ci
15478c2ecf20Sopenharmony_ci	{ SYS_DESC(SYS_RGSR_EL1), undef_access },
15488c2ecf20Sopenharmony_ci	{ SYS_DESC(SYS_GCR_EL1), undef_access },
15498c2ecf20Sopenharmony_ci
15508c2ecf20Sopenharmony_ci	{ SYS_DESC(SYS_ZCR_EL1), NULL, reset_val, ZCR_EL1, 0, .visibility = sve_visibility },
15518c2ecf20Sopenharmony_ci	{ SYS_DESC(SYS_TTBR0_EL1), access_vm_reg, reset_unknown, TTBR0_EL1 },
15528c2ecf20Sopenharmony_ci	{ SYS_DESC(SYS_TTBR1_EL1), access_vm_reg, reset_unknown, TTBR1_EL1 },
15538c2ecf20Sopenharmony_ci	{ SYS_DESC(SYS_TCR_EL1), access_vm_reg, reset_val, TCR_EL1, 0 },
15548c2ecf20Sopenharmony_ci
15558c2ecf20Sopenharmony_ci	PTRAUTH_KEY(APIA),
15568c2ecf20Sopenharmony_ci	PTRAUTH_KEY(APIB),
15578c2ecf20Sopenharmony_ci	PTRAUTH_KEY(APDA),
15588c2ecf20Sopenharmony_ci	PTRAUTH_KEY(APDB),
15598c2ecf20Sopenharmony_ci	PTRAUTH_KEY(APGA),
15608c2ecf20Sopenharmony_ci
15618c2ecf20Sopenharmony_ci	{ SYS_DESC(SYS_AFSR0_EL1), access_vm_reg, reset_unknown, AFSR0_EL1 },
15628c2ecf20Sopenharmony_ci	{ SYS_DESC(SYS_AFSR1_EL1), access_vm_reg, reset_unknown, AFSR1_EL1 },
15638c2ecf20Sopenharmony_ci	{ SYS_DESC(SYS_ESR_EL1), access_vm_reg, reset_unknown, ESR_EL1 },
15648c2ecf20Sopenharmony_ci
15658c2ecf20Sopenharmony_ci	{ SYS_DESC(SYS_ERRIDR_EL1), trap_raz_wi },
15668c2ecf20Sopenharmony_ci	{ SYS_DESC(SYS_ERRSELR_EL1), trap_raz_wi },
15678c2ecf20Sopenharmony_ci	{ SYS_DESC(SYS_ERXFR_EL1), trap_raz_wi },
15688c2ecf20Sopenharmony_ci	{ SYS_DESC(SYS_ERXCTLR_EL1), trap_raz_wi },
15698c2ecf20Sopenharmony_ci	{ SYS_DESC(SYS_ERXSTATUS_EL1), trap_raz_wi },
15708c2ecf20Sopenharmony_ci	{ SYS_DESC(SYS_ERXADDR_EL1), trap_raz_wi },
15718c2ecf20Sopenharmony_ci	{ SYS_DESC(SYS_ERXMISC0_EL1), trap_raz_wi },
15728c2ecf20Sopenharmony_ci	{ SYS_DESC(SYS_ERXMISC1_EL1), trap_raz_wi },
15738c2ecf20Sopenharmony_ci
15748c2ecf20Sopenharmony_ci	{ SYS_DESC(SYS_TFSR_EL1), undef_access },
15758c2ecf20Sopenharmony_ci	{ SYS_DESC(SYS_TFSRE0_EL1), undef_access },
15768c2ecf20Sopenharmony_ci
15778c2ecf20Sopenharmony_ci	{ SYS_DESC(SYS_FAR_EL1), access_vm_reg, reset_unknown, FAR_EL1 },
15788c2ecf20Sopenharmony_ci	{ SYS_DESC(SYS_PAR_EL1), NULL, reset_unknown, PAR_EL1 },
15798c2ecf20Sopenharmony_ci
15808c2ecf20Sopenharmony_ci	{ SYS_DESC(SYS_PMINTENSET_EL1), access_pminten, reset_unknown, PMINTENSET_EL1 },
15818c2ecf20Sopenharmony_ci	{ SYS_DESC(SYS_PMINTENCLR_EL1), access_pminten, reset_unknown, PMINTENSET_EL1 },
15828c2ecf20Sopenharmony_ci
15838c2ecf20Sopenharmony_ci	{ SYS_DESC(SYS_MAIR_EL1), access_vm_reg, reset_unknown, MAIR_EL1 },
15848c2ecf20Sopenharmony_ci	{ SYS_DESC(SYS_AMAIR_EL1), access_vm_reg, reset_amair_el1, AMAIR_EL1 },
15858c2ecf20Sopenharmony_ci
15868c2ecf20Sopenharmony_ci	{ SYS_DESC(SYS_LORSA_EL1), trap_loregion },
15878c2ecf20Sopenharmony_ci	{ SYS_DESC(SYS_LOREA_EL1), trap_loregion },
15888c2ecf20Sopenharmony_ci	{ SYS_DESC(SYS_LORN_EL1), trap_loregion },
15898c2ecf20Sopenharmony_ci	{ SYS_DESC(SYS_LORC_EL1), trap_loregion },
15908c2ecf20Sopenharmony_ci	{ SYS_DESC(SYS_LORID_EL1), trap_loregion },
15918c2ecf20Sopenharmony_ci
15928c2ecf20Sopenharmony_ci	{ SYS_DESC(SYS_VBAR_EL1), NULL, reset_val, VBAR_EL1, 0 },
15938c2ecf20Sopenharmony_ci	{ SYS_DESC(SYS_DISR_EL1), NULL, reset_val, DISR_EL1, 0 },
15948c2ecf20Sopenharmony_ci
15958c2ecf20Sopenharmony_ci	{ SYS_DESC(SYS_ICC_IAR0_EL1), write_to_read_only },
15968c2ecf20Sopenharmony_ci	{ SYS_DESC(SYS_ICC_EOIR0_EL1), read_from_write_only },
15978c2ecf20Sopenharmony_ci	{ SYS_DESC(SYS_ICC_HPPIR0_EL1), write_to_read_only },
15988c2ecf20Sopenharmony_ci	{ SYS_DESC(SYS_ICC_DIR_EL1), read_from_write_only },
15998c2ecf20Sopenharmony_ci	{ SYS_DESC(SYS_ICC_RPR_EL1), write_to_read_only },
16008c2ecf20Sopenharmony_ci	{ SYS_DESC(SYS_ICC_SGI1R_EL1), access_gic_sgi },
16018c2ecf20Sopenharmony_ci	{ SYS_DESC(SYS_ICC_ASGI1R_EL1), access_gic_sgi },
16028c2ecf20Sopenharmony_ci	{ SYS_DESC(SYS_ICC_SGI0R_EL1), access_gic_sgi },
16038c2ecf20Sopenharmony_ci	{ SYS_DESC(SYS_ICC_IAR1_EL1), write_to_read_only },
16048c2ecf20Sopenharmony_ci	{ SYS_DESC(SYS_ICC_EOIR1_EL1), read_from_write_only },
16058c2ecf20Sopenharmony_ci	{ SYS_DESC(SYS_ICC_HPPIR1_EL1), write_to_read_only },
16068c2ecf20Sopenharmony_ci	{ SYS_DESC(SYS_ICC_SRE_EL1), access_gic_sre },
16078c2ecf20Sopenharmony_ci
16088c2ecf20Sopenharmony_ci	{ SYS_DESC(SYS_CONTEXTIDR_EL1), access_vm_reg, reset_val, CONTEXTIDR_EL1, 0 },
16098c2ecf20Sopenharmony_ci	{ SYS_DESC(SYS_TPIDR_EL1), NULL, reset_unknown, TPIDR_EL1 },
16108c2ecf20Sopenharmony_ci
16118c2ecf20Sopenharmony_ci	{ SYS_DESC(SYS_SCXTNUM_EL1), undef_access },
16128c2ecf20Sopenharmony_ci
16138c2ecf20Sopenharmony_ci	{ SYS_DESC(SYS_CNTKCTL_EL1), NULL, reset_val, CNTKCTL_EL1, 0},
16148c2ecf20Sopenharmony_ci
16158c2ecf20Sopenharmony_ci	{ SYS_DESC(SYS_CCSIDR_EL1), access_ccsidr },
16168c2ecf20Sopenharmony_ci	{ SYS_DESC(SYS_CLIDR_EL1), access_clidr },
16178c2ecf20Sopenharmony_ci	{ SYS_DESC(SYS_CSSELR_EL1), access_csselr, reset_unknown, CSSELR_EL1 },
16188c2ecf20Sopenharmony_ci	{ SYS_DESC(SYS_CTR_EL0), access_ctr },
16198c2ecf20Sopenharmony_ci
16208c2ecf20Sopenharmony_ci	{ SYS_DESC(SYS_PMCR_EL0), access_pmcr, reset_pmcr, PMCR_EL0 },
16218c2ecf20Sopenharmony_ci	{ SYS_DESC(SYS_PMCNTENSET_EL0), access_pmcnten, reset_unknown, PMCNTENSET_EL0 },
16228c2ecf20Sopenharmony_ci	{ SYS_DESC(SYS_PMCNTENCLR_EL0), access_pmcnten, reset_unknown, PMCNTENSET_EL0 },
16238c2ecf20Sopenharmony_ci	{ SYS_DESC(SYS_PMOVSCLR_EL0), access_pmovs, reset_unknown, PMOVSSET_EL0 },
16248c2ecf20Sopenharmony_ci	{ SYS_DESC(SYS_PMSWINC_EL0), access_pmswinc, reset_unknown, PMSWINC_EL0 },
16258c2ecf20Sopenharmony_ci	{ SYS_DESC(SYS_PMSELR_EL0), access_pmselr, reset_unknown, PMSELR_EL0 },
16268c2ecf20Sopenharmony_ci	{ SYS_DESC(SYS_PMCEID0_EL0), access_pmceid },
16278c2ecf20Sopenharmony_ci	{ SYS_DESC(SYS_PMCEID1_EL0), access_pmceid },
16288c2ecf20Sopenharmony_ci	{ SYS_DESC(SYS_PMCCNTR_EL0), access_pmu_evcntr, reset_unknown, PMCCNTR_EL0 },
16298c2ecf20Sopenharmony_ci	{ SYS_DESC(SYS_PMXEVTYPER_EL0), access_pmu_evtyper },
16308c2ecf20Sopenharmony_ci	{ SYS_DESC(SYS_PMXEVCNTR_EL0), access_pmu_evcntr },
16318c2ecf20Sopenharmony_ci	/*
16328c2ecf20Sopenharmony_ci	 * PMUSERENR_EL0 resets as unknown in 64bit mode while it resets as zero
16338c2ecf20Sopenharmony_ci	 * in 32bit mode. Here we choose to reset it as zero for consistency.
16348c2ecf20Sopenharmony_ci	 */
16358c2ecf20Sopenharmony_ci	{ SYS_DESC(SYS_PMUSERENR_EL0), access_pmuserenr, reset_val, PMUSERENR_EL0, 0 },
16368c2ecf20Sopenharmony_ci	{ SYS_DESC(SYS_PMOVSSET_EL0), access_pmovs, reset_unknown, PMOVSSET_EL0 },
16378c2ecf20Sopenharmony_ci
16388c2ecf20Sopenharmony_ci	{ SYS_DESC(SYS_TPIDR_EL0), NULL, reset_unknown, TPIDR_EL0 },
16398c2ecf20Sopenharmony_ci	{ SYS_DESC(SYS_TPIDRRO_EL0), NULL, reset_unknown, TPIDRRO_EL0 },
16408c2ecf20Sopenharmony_ci
16418c2ecf20Sopenharmony_ci	{ SYS_DESC(SYS_SCXTNUM_EL0), undef_access },
16428c2ecf20Sopenharmony_ci
16438c2ecf20Sopenharmony_ci	{ SYS_DESC(SYS_AMCR_EL0), undef_access },
16448c2ecf20Sopenharmony_ci	{ SYS_DESC(SYS_AMCFGR_EL0), undef_access },
16458c2ecf20Sopenharmony_ci	{ SYS_DESC(SYS_AMCGCR_EL0), undef_access },
16468c2ecf20Sopenharmony_ci	{ SYS_DESC(SYS_AMUSERENR_EL0), undef_access },
16478c2ecf20Sopenharmony_ci	{ SYS_DESC(SYS_AMCNTENCLR0_EL0), undef_access },
16488c2ecf20Sopenharmony_ci	{ SYS_DESC(SYS_AMCNTENSET0_EL0), undef_access },
16498c2ecf20Sopenharmony_ci	{ SYS_DESC(SYS_AMCNTENCLR1_EL0), undef_access },
16508c2ecf20Sopenharmony_ci	{ SYS_DESC(SYS_AMCNTENSET1_EL0), undef_access },
16518c2ecf20Sopenharmony_ci	AMU_AMEVCNTR0_EL0(0),
16528c2ecf20Sopenharmony_ci	AMU_AMEVCNTR0_EL0(1),
16538c2ecf20Sopenharmony_ci	AMU_AMEVCNTR0_EL0(2),
16548c2ecf20Sopenharmony_ci	AMU_AMEVCNTR0_EL0(3),
16558c2ecf20Sopenharmony_ci	AMU_AMEVCNTR0_EL0(4),
16568c2ecf20Sopenharmony_ci	AMU_AMEVCNTR0_EL0(5),
16578c2ecf20Sopenharmony_ci	AMU_AMEVCNTR0_EL0(6),
16588c2ecf20Sopenharmony_ci	AMU_AMEVCNTR0_EL0(7),
16598c2ecf20Sopenharmony_ci	AMU_AMEVCNTR0_EL0(8),
16608c2ecf20Sopenharmony_ci	AMU_AMEVCNTR0_EL0(9),
16618c2ecf20Sopenharmony_ci	AMU_AMEVCNTR0_EL0(10),
16628c2ecf20Sopenharmony_ci	AMU_AMEVCNTR0_EL0(11),
16638c2ecf20Sopenharmony_ci	AMU_AMEVCNTR0_EL0(12),
16648c2ecf20Sopenharmony_ci	AMU_AMEVCNTR0_EL0(13),
16658c2ecf20Sopenharmony_ci	AMU_AMEVCNTR0_EL0(14),
16668c2ecf20Sopenharmony_ci	AMU_AMEVCNTR0_EL0(15),
16678c2ecf20Sopenharmony_ci	AMU_AMEVTYPER0_EL0(0),
16688c2ecf20Sopenharmony_ci	AMU_AMEVTYPER0_EL0(1),
16698c2ecf20Sopenharmony_ci	AMU_AMEVTYPER0_EL0(2),
16708c2ecf20Sopenharmony_ci	AMU_AMEVTYPER0_EL0(3),
16718c2ecf20Sopenharmony_ci	AMU_AMEVTYPER0_EL0(4),
16728c2ecf20Sopenharmony_ci	AMU_AMEVTYPER0_EL0(5),
16738c2ecf20Sopenharmony_ci	AMU_AMEVTYPER0_EL0(6),
16748c2ecf20Sopenharmony_ci	AMU_AMEVTYPER0_EL0(7),
16758c2ecf20Sopenharmony_ci	AMU_AMEVTYPER0_EL0(8),
16768c2ecf20Sopenharmony_ci	AMU_AMEVTYPER0_EL0(9),
16778c2ecf20Sopenharmony_ci	AMU_AMEVTYPER0_EL0(10),
16788c2ecf20Sopenharmony_ci	AMU_AMEVTYPER0_EL0(11),
16798c2ecf20Sopenharmony_ci	AMU_AMEVTYPER0_EL0(12),
16808c2ecf20Sopenharmony_ci	AMU_AMEVTYPER0_EL0(13),
16818c2ecf20Sopenharmony_ci	AMU_AMEVTYPER0_EL0(14),
16828c2ecf20Sopenharmony_ci	AMU_AMEVTYPER0_EL0(15),
16838c2ecf20Sopenharmony_ci	AMU_AMEVCNTR1_EL0(0),
16848c2ecf20Sopenharmony_ci	AMU_AMEVCNTR1_EL0(1),
16858c2ecf20Sopenharmony_ci	AMU_AMEVCNTR1_EL0(2),
16868c2ecf20Sopenharmony_ci	AMU_AMEVCNTR1_EL0(3),
16878c2ecf20Sopenharmony_ci	AMU_AMEVCNTR1_EL0(4),
16888c2ecf20Sopenharmony_ci	AMU_AMEVCNTR1_EL0(5),
16898c2ecf20Sopenharmony_ci	AMU_AMEVCNTR1_EL0(6),
16908c2ecf20Sopenharmony_ci	AMU_AMEVCNTR1_EL0(7),
16918c2ecf20Sopenharmony_ci	AMU_AMEVCNTR1_EL0(8),
16928c2ecf20Sopenharmony_ci	AMU_AMEVCNTR1_EL0(9),
16938c2ecf20Sopenharmony_ci	AMU_AMEVCNTR1_EL0(10),
16948c2ecf20Sopenharmony_ci	AMU_AMEVCNTR1_EL0(11),
16958c2ecf20Sopenharmony_ci	AMU_AMEVCNTR1_EL0(12),
16968c2ecf20Sopenharmony_ci	AMU_AMEVCNTR1_EL0(13),
16978c2ecf20Sopenharmony_ci	AMU_AMEVCNTR1_EL0(14),
16988c2ecf20Sopenharmony_ci	AMU_AMEVCNTR1_EL0(15),
16998c2ecf20Sopenharmony_ci	AMU_AMEVTYPER1_EL0(0),
17008c2ecf20Sopenharmony_ci	AMU_AMEVTYPER1_EL0(1),
17018c2ecf20Sopenharmony_ci	AMU_AMEVTYPER1_EL0(2),
17028c2ecf20Sopenharmony_ci	AMU_AMEVTYPER1_EL0(3),
17038c2ecf20Sopenharmony_ci	AMU_AMEVTYPER1_EL0(4),
17048c2ecf20Sopenharmony_ci	AMU_AMEVTYPER1_EL0(5),
17058c2ecf20Sopenharmony_ci	AMU_AMEVTYPER1_EL0(6),
17068c2ecf20Sopenharmony_ci	AMU_AMEVTYPER1_EL0(7),
17078c2ecf20Sopenharmony_ci	AMU_AMEVTYPER1_EL0(8),
17088c2ecf20Sopenharmony_ci	AMU_AMEVTYPER1_EL0(9),
17098c2ecf20Sopenharmony_ci	AMU_AMEVTYPER1_EL0(10),
17108c2ecf20Sopenharmony_ci	AMU_AMEVTYPER1_EL0(11),
17118c2ecf20Sopenharmony_ci	AMU_AMEVTYPER1_EL0(12),
17128c2ecf20Sopenharmony_ci	AMU_AMEVTYPER1_EL0(13),
17138c2ecf20Sopenharmony_ci	AMU_AMEVTYPER1_EL0(14),
17148c2ecf20Sopenharmony_ci	AMU_AMEVTYPER1_EL0(15),
17158c2ecf20Sopenharmony_ci
17168c2ecf20Sopenharmony_ci	{ SYS_DESC(SYS_CNTP_TVAL_EL0), access_arch_timer },
17178c2ecf20Sopenharmony_ci	{ SYS_DESC(SYS_CNTP_CTL_EL0), access_arch_timer },
17188c2ecf20Sopenharmony_ci	{ SYS_DESC(SYS_CNTP_CVAL_EL0), access_arch_timer },
17198c2ecf20Sopenharmony_ci
17208c2ecf20Sopenharmony_ci	/* PMEVCNTRn_EL0 */
17218c2ecf20Sopenharmony_ci	PMU_PMEVCNTR_EL0(0),
17228c2ecf20Sopenharmony_ci	PMU_PMEVCNTR_EL0(1),
17238c2ecf20Sopenharmony_ci	PMU_PMEVCNTR_EL0(2),
17248c2ecf20Sopenharmony_ci	PMU_PMEVCNTR_EL0(3),
17258c2ecf20Sopenharmony_ci	PMU_PMEVCNTR_EL0(4),
17268c2ecf20Sopenharmony_ci	PMU_PMEVCNTR_EL0(5),
17278c2ecf20Sopenharmony_ci	PMU_PMEVCNTR_EL0(6),
17288c2ecf20Sopenharmony_ci	PMU_PMEVCNTR_EL0(7),
17298c2ecf20Sopenharmony_ci	PMU_PMEVCNTR_EL0(8),
17308c2ecf20Sopenharmony_ci	PMU_PMEVCNTR_EL0(9),
17318c2ecf20Sopenharmony_ci	PMU_PMEVCNTR_EL0(10),
17328c2ecf20Sopenharmony_ci	PMU_PMEVCNTR_EL0(11),
17338c2ecf20Sopenharmony_ci	PMU_PMEVCNTR_EL0(12),
17348c2ecf20Sopenharmony_ci	PMU_PMEVCNTR_EL0(13),
17358c2ecf20Sopenharmony_ci	PMU_PMEVCNTR_EL0(14),
17368c2ecf20Sopenharmony_ci	PMU_PMEVCNTR_EL0(15),
17378c2ecf20Sopenharmony_ci	PMU_PMEVCNTR_EL0(16),
17388c2ecf20Sopenharmony_ci	PMU_PMEVCNTR_EL0(17),
17398c2ecf20Sopenharmony_ci	PMU_PMEVCNTR_EL0(18),
17408c2ecf20Sopenharmony_ci	PMU_PMEVCNTR_EL0(19),
17418c2ecf20Sopenharmony_ci	PMU_PMEVCNTR_EL0(20),
17428c2ecf20Sopenharmony_ci	PMU_PMEVCNTR_EL0(21),
17438c2ecf20Sopenharmony_ci	PMU_PMEVCNTR_EL0(22),
17448c2ecf20Sopenharmony_ci	PMU_PMEVCNTR_EL0(23),
17458c2ecf20Sopenharmony_ci	PMU_PMEVCNTR_EL0(24),
17468c2ecf20Sopenharmony_ci	PMU_PMEVCNTR_EL0(25),
17478c2ecf20Sopenharmony_ci	PMU_PMEVCNTR_EL0(26),
17488c2ecf20Sopenharmony_ci	PMU_PMEVCNTR_EL0(27),
17498c2ecf20Sopenharmony_ci	PMU_PMEVCNTR_EL0(28),
17508c2ecf20Sopenharmony_ci	PMU_PMEVCNTR_EL0(29),
17518c2ecf20Sopenharmony_ci	PMU_PMEVCNTR_EL0(30),
17528c2ecf20Sopenharmony_ci	/* PMEVTYPERn_EL0 */
17538c2ecf20Sopenharmony_ci	PMU_PMEVTYPER_EL0(0),
17548c2ecf20Sopenharmony_ci	PMU_PMEVTYPER_EL0(1),
17558c2ecf20Sopenharmony_ci	PMU_PMEVTYPER_EL0(2),
17568c2ecf20Sopenharmony_ci	PMU_PMEVTYPER_EL0(3),
17578c2ecf20Sopenharmony_ci	PMU_PMEVTYPER_EL0(4),
17588c2ecf20Sopenharmony_ci	PMU_PMEVTYPER_EL0(5),
17598c2ecf20Sopenharmony_ci	PMU_PMEVTYPER_EL0(6),
17608c2ecf20Sopenharmony_ci	PMU_PMEVTYPER_EL0(7),
17618c2ecf20Sopenharmony_ci	PMU_PMEVTYPER_EL0(8),
17628c2ecf20Sopenharmony_ci	PMU_PMEVTYPER_EL0(9),
17638c2ecf20Sopenharmony_ci	PMU_PMEVTYPER_EL0(10),
17648c2ecf20Sopenharmony_ci	PMU_PMEVTYPER_EL0(11),
17658c2ecf20Sopenharmony_ci	PMU_PMEVTYPER_EL0(12),
17668c2ecf20Sopenharmony_ci	PMU_PMEVTYPER_EL0(13),
17678c2ecf20Sopenharmony_ci	PMU_PMEVTYPER_EL0(14),
17688c2ecf20Sopenharmony_ci	PMU_PMEVTYPER_EL0(15),
17698c2ecf20Sopenharmony_ci	PMU_PMEVTYPER_EL0(16),
17708c2ecf20Sopenharmony_ci	PMU_PMEVTYPER_EL0(17),
17718c2ecf20Sopenharmony_ci	PMU_PMEVTYPER_EL0(18),
17728c2ecf20Sopenharmony_ci	PMU_PMEVTYPER_EL0(19),
17738c2ecf20Sopenharmony_ci	PMU_PMEVTYPER_EL0(20),
17748c2ecf20Sopenharmony_ci	PMU_PMEVTYPER_EL0(21),
17758c2ecf20Sopenharmony_ci	PMU_PMEVTYPER_EL0(22),
17768c2ecf20Sopenharmony_ci	PMU_PMEVTYPER_EL0(23),
17778c2ecf20Sopenharmony_ci	PMU_PMEVTYPER_EL0(24),
17788c2ecf20Sopenharmony_ci	PMU_PMEVTYPER_EL0(25),
17798c2ecf20Sopenharmony_ci	PMU_PMEVTYPER_EL0(26),
17808c2ecf20Sopenharmony_ci	PMU_PMEVTYPER_EL0(27),
17818c2ecf20Sopenharmony_ci	PMU_PMEVTYPER_EL0(28),
17828c2ecf20Sopenharmony_ci	PMU_PMEVTYPER_EL0(29),
17838c2ecf20Sopenharmony_ci	PMU_PMEVTYPER_EL0(30),
17848c2ecf20Sopenharmony_ci	/*
17858c2ecf20Sopenharmony_ci	 * PMCCFILTR_EL0 resets as unknown in 64bit mode while it resets as zero
17868c2ecf20Sopenharmony_ci	 * in 32bit mode. Here we choose to reset it as zero for consistency.
17878c2ecf20Sopenharmony_ci	 */
17888c2ecf20Sopenharmony_ci	{ SYS_DESC(SYS_PMCCFILTR_EL0), access_pmu_evtyper, reset_val, PMCCFILTR_EL0, 0 },
17898c2ecf20Sopenharmony_ci
17908c2ecf20Sopenharmony_ci	{ SYS_DESC(SYS_DACR32_EL2), NULL, reset_unknown, DACR32_EL2 },
17918c2ecf20Sopenharmony_ci	{ SYS_DESC(SYS_IFSR32_EL2), NULL, reset_unknown, IFSR32_EL2 },
17928c2ecf20Sopenharmony_ci	{ SYS_DESC(SYS_FPEXC32_EL2), NULL, reset_val, FPEXC32_EL2, 0x700 },
17938c2ecf20Sopenharmony_ci};
17948c2ecf20Sopenharmony_ci
17958c2ecf20Sopenharmony_cistatic bool trap_dbgidr(struct kvm_vcpu *vcpu,
17968c2ecf20Sopenharmony_ci			struct sys_reg_params *p,
17978c2ecf20Sopenharmony_ci			const struct sys_reg_desc *r)
17988c2ecf20Sopenharmony_ci{
17998c2ecf20Sopenharmony_ci	if (p->is_write) {
18008c2ecf20Sopenharmony_ci		return ignore_write(vcpu, p);
18018c2ecf20Sopenharmony_ci	} else {
18028c2ecf20Sopenharmony_ci		u64 dfr = read_sanitised_ftr_reg(SYS_ID_AA64DFR0_EL1);
18038c2ecf20Sopenharmony_ci		u64 pfr = read_sanitised_ftr_reg(SYS_ID_AA64PFR0_EL1);
18048c2ecf20Sopenharmony_ci		u32 el3 = !!cpuid_feature_extract_unsigned_field(pfr, ID_AA64PFR0_EL3_SHIFT);
18058c2ecf20Sopenharmony_ci
18068c2ecf20Sopenharmony_ci		p->regval = ((((dfr >> ID_AA64DFR0_WRPS_SHIFT) & 0xf) << 28) |
18078c2ecf20Sopenharmony_ci			     (((dfr >> ID_AA64DFR0_BRPS_SHIFT) & 0xf) << 24) |
18088c2ecf20Sopenharmony_ci			     (((dfr >> ID_AA64DFR0_CTX_CMPS_SHIFT) & 0xf) << 20)
18098c2ecf20Sopenharmony_ci			     | (6 << 16) | (el3 << 14) | (el3 << 12));
18108c2ecf20Sopenharmony_ci		return true;
18118c2ecf20Sopenharmony_ci	}
18128c2ecf20Sopenharmony_ci}
18138c2ecf20Sopenharmony_ci
18148c2ecf20Sopenharmony_cistatic bool trap_debug32(struct kvm_vcpu *vcpu,
18158c2ecf20Sopenharmony_ci			 struct sys_reg_params *p,
18168c2ecf20Sopenharmony_ci			 const struct sys_reg_desc *r)
18178c2ecf20Sopenharmony_ci{
18188c2ecf20Sopenharmony_ci	if (p->is_write) {
18198c2ecf20Sopenharmony_ci		vcpu_cp14(vcpu, r->reg) = p->regval;
18208c2ecf20Sopenharmony_ci		vcpu->arch.flags |= KVM_ARM64_DEBUG_DIRTY;
18218c2ecf20Sopenharmony_ci	} else {
18228c2ecf20Sopenharmony_ci		p->regval = vcpu_cp14(vcpu, r->reg);
18238c2ecf20Sopenharmony_ci	}
18248c2ecf20Sopenharmony_ci
18258c2ecf20Sopenharmony_ci	return true;
18268c2ecf20Sopenharmony_ci}
18278c2ecf20Sopenharmony_ci
18288c2ecf20Sopenharmony_ci/* AArch32 debug register mappings
18298c2ecf20Sopenharmony_ci *
18308c2ecf20Sopenharmony_ci * AArch32 DBGBVRn is mapped to DBGBVRn_EL1[31:0]
18318c2ecf20Sopenharmony_ci * AArch32 DBGBXVRn is mapped to DBGBVRn_EL1[63:32]
18328c2ecf20Sopenharmony_ci *
18338c2ecf20Sopenharmony_ci * All control registers and watchpoint value registers are mapped to
18348c2ecf20Sopenharmony_ci * the lower 32 bits of their AArch64 equivalents. We share the trap
18358c2ecf20Sopenharmony_ci * handlers with the above AArch64 code which checks what mode the
18368c2ecf20Sopenharmony_ci * system is in.
18378c2ecf20Sopenharmony_ci */
18388c2ecf20Sopenharmony_ci
18398c2ecf20Sopenharmony_cistatic bool trap_xvr(struct kvm_vcpu *vcpu,
18408c2ecf20Sopenharmony_ci		     struct sys_reg_params *p,
18418c2ecf20Sopenharmony_ci		     const struct sys_reg_desc *rd)
18428c2ecf20Sopenharmony_ci{
18438c2ecf20Sopenharmony_ci	u64 *dbg_reg = &vcpu->arch.vcpu_debug_state.dbg_bvr[rd->reg];
18448c2ecf20Sopenharmony_ci
18458c2ecf20Sopenharmony_ci	if (p->is_write) {
18468c2ecf20Sopenharmony_ci		u64 val = *dbg_reg;
18478c2ecf20Sopenharmony_ci
18488c2ecf20Sopenharmony_ci		val &= 0xffffffffUL;
18498c2ecf20Sopenharmony_ci		val |= p->regval << 32;
18508c2ecf20Sopenharmony_ci		*dbg_reg = val;
18518c2ecf20Sopenharmony_ci
18528c2ecf20Sopenharmony_ci		vcpu->arch.flags |= KVM_ARM64_DEBUG_DIRTY;
18538c2ecf20Sopenharmony_ci	} else {
18548c2ecf20Sopenharmony_ci		p->regval = *dbg_reg >> 32;
18558c2ecf20Sopenharmony_ci	}
18568c2ecf20Sopenharmony_ci
18578c2ecf20Sopenharmony_ci	trace_trap_reg(__func__, rd->reg, p->is_write, *dbg_reg);
18588c2ecf20Sopenharmony_ci
18598c2ecf20Sopenharmony_ci	return true;
18608c2ecf20Sopenharmony_ci}
18618c2ecf20Sopenharmony_ci
18628c2ecf20Sopenharmony_ci#define DBG_BCR_BVR_WCR_WVR(n)						\
18638c2ecf20Sopenharmony_ci	/* DBGBVRn */							\
18648c2ecf20Sopenharmony_ci	{ Op1( 0), CRn( 0), CRm((n)), Op2( 4), trap_bvr, NULL, n }, 	\
18658c2ecf20Sopenharmony_ci	/* DBGBCRn */							\
18668c2ecf20Sopenharmony_ci	{ Op1( 0), CRn( 0), CRm((n)), Op2( 5), trap_bcr, NULL, n },	\
18678c2ecf20Sopenharmony_ci	/* DBGWVRn */							\
18688c2ecf20Sopenharmony_ci	{ Op1( 0), CRn( 0), CRm((n)), Op2( 6), trap_wvr, NULL, n },	\
18698c2ecf20Sopenharmony_ci	/* DBGWCRn */							\
18708c2ecf20Sopenharmony_ci	{ Op1( 0), CRn( 0), CRm((n)), Op2( 7), trap_wcr, NULL, n }
18718c2ecf20Sopenharmony_ci
18728c2ecf20Sopenharmony_ci#define DBGBXVR(n)							\
18738c2ecf20Sopenharmony_ci	{ Op1( 0), CRn( 1), CRm((n)), Op2( 1), trap_xvr, NULL, n }
18748c2ecf20Sopenharmony_ci
18758c2ecf20Sopenharmony_ci/*
18768c2ecf20Sopenharmony_ci * Trapped cp14 registers. We generally ignore most of the external
18778c2ecf20Sopenharmony_ci * debug, on the principle that they don't really make sense to a
18788c2ecf20Sopenharmony_ci * guest. Revisit this one day, would this principle change.
18798c2ecf20Sopenharmony_ci */
18808c2ecf20Sopenharmony_cistatic const struct sys_reg_desc cp14_regs[] = {
18818c2ecf20Sopenharmony_ci	/* DBGIDR */
18828c2ecf20Sopenharmony_ci	{ Op1( 0), CRn( 0), CRm( 0), Op2( 0), trap_dbgidr },
18838c2ecf20Sopenharmony_ci	/* DBGDTRRXext */
18848c2ecf20Sopenharmony_ci	{ Op1( 0), CRn( 0), CRm( 0), Op2( 2), trap_raz_wi },
18858c2ecf20Sopenharmony_ci
18868c2ecf20Sopenharmony_ci	DBG_BCR_BVR_WCR_WVR(0),
18878c2ecf20Sopenharmony_ci	/* DBGDSCRint */
18888c2ecf20Sopenharmony_ci	{ Op1( 0), CRn( 0), CRm( 1), Op2( 0), trap_raz_wi },
18898c2ecf20Sopenharmony_ci	DBG_BCR_BVR_WCR_WVR(1),
18908c2ecf20Sopenharmony_ci	/* DBGDCCINT */
18918c2ecf20Sopenharmony_ci	{ Op1( 0), CRn( 0), CRm( 2), Op2( 0), trap_debug32, NULL, cp14_DBGDCCINT },
18928c2ecf20Sopenharmony_ci	/* DBGDSCRext */
18938c2ecf20Sopenharmony_ci	{ Op1( 0), CRn( 0), CRm( 2), Op2( 2), trap_debug32, NULL, cp14_DBGDSCRext },
18948c2ecf20Sopenharmony_ci	DBG_BCR_BVR_WCR_WVR(2),
18958c2ecf20Sopenharmony_ci	/* DBGDTR[RT]Xint */
18968c2ecf20Sopenharmony_ci	{ Op1( 0), CRn( 0), CRm( 3), Op2( 0), trap_raz_wi },
18978c2ecf20Sopenharmony_ci	/* DBGDTR[RT]Xext */
18988c2ecf20Sopenharmony_ci	{ Op1( 0), CRn( 0), CRm( 3), Op2( 2), trap_raz_wi },
18998c2ecf20Sopenharmony_ci	DBG_BCR_BVR_WCR_WVR(3),
19008c2ecf20Sopenharmony_ci	DBG_BCR_BVR_WCR_WVR(4),
19018c2ecf20Sopenharmony_ci	DBG_BCR_BVR_WCR_WVR(5),
19028c2ecf20Sopenharmony_ci	/* DBGWFAR */
19038c2ecf20Sopenharmony_ci	{ Op1( 0), CRn( 0), CRm( 6), Op2( 0), trap_raz_wi },
19048c2ecf20Sopenharmony_ci	/* DBGOSECCR */
19058c2ecf20Sopenharmony_ci	{ Op1( 0), CRn( 0), CRm( 6), Op2( 2), trap_raz_wi },
19068c2ecf20Sopenharmony_ci	DBG_BCR_BVR_WCR_WVR(6),
19078c2ecf20Sopenharmony_ci	/* DBGVCR */
19088c2ecf20Sopenharmony_ci	{ Op1( 0), CRn( 0), CRm( 7), Op2( 0), trap_debug32, NULL, cp14_DBGVCR },
19098c2ecf20Sopenharmony_ci	DBG_BCR_BVR_WCR_WVR(7),
19108c2ecf20Sopenharmony_ci	DBG_BCR_BVR_WCR_WVR(8),
19118c2ecf20Sopenharmony_ci	DBG_BCR_BVR_WCR_WVR(9),
19128c2ecf20Sopenharmony_ci	DBG_BCR_BVR_WCR_WVR(10),
19138c2ecf20Sopenharmony_ci	DBG_BCR_BVR_WCR_WVR(11),
19148c2ecf20Sopenharmony_ci	DBG_BCR_BVR_WCR_WVR(12),
19158c2ecf20Sopenharmony_ci	DBG_BCR_BVR_WCR_WVR(13),
19168c2ecf20Sopenharmony_ci	DBG_BCR_BVR_WCR_WVR(14),
19178c2ecf20Sopenharmony_ci	DBG_BCR_BVR_WCR_WVR(15),
19188c2ecf20Sopenharmony_ci
19198c2ecf20Sopenharmony_ci	/* DBGDRAR (32bit) */
19208c2ecf20Sopenharmony_ci	{ Op1( 0), CRn( 1), CRm( 0), Op2( 0), trap_raz_wi },
19218c2ecf20Sopenharmony_ci
19228c2ecf20Sopenharmony_ci	DBGBXVR(0),
19238c2ecf20Sopenharmony_ci	/* DBGOSLAR */
19248c2ecf20Sopenharmony_ci	{ Op1( 0), CRn( 1), CRm( 0), Op2( 4), trap_raz_wi },
19258c2ecf20Sopenharmony_ci	DBGBXVR(1),
19268c2ecf20Sopenharmony_ci	/* DBGOSLSR */
19278c2ecf20Sopenharmony_ci	{ Op1( 0), CRn( 1), CRm( 1), Op2( 4), trap_oslsr_el1 },
19288c2ecf20Sopenharmony_ci	DBGBXVR(2),
19298c2ecf20Sopenharmony_ci	DBGBXVR(3),
19308c2ecf20Sopenharmony_ci	/* DBGOSDLR */
19318c2ecf20Sopenharmony_ci	{ Op1( 0), CRn( 1), CRm( 3), Op2( 4), trap_raz_wi },
19328c2ecf20Sopenharmony_ci	DBGBXVR(4),
19338c2ecf20Sopenharmony_ci	/* DBGPRCR */
19348c2ecf20Sopenharmony_ci	{ Op1( 0), CRn( 1), CRm( 4), Op2( 4), trap_raz_wi },
19358c2ecf20Sopenharmony_ci	DBGBXVR(5),
19368c2ecf20Sopenharmony_ci	DBGBXVR(6),
19378c2ecf20Sopenharmony_ci	DBGBXVR(7),
19388c2ecf20Sopenharmony_ci	DBGBXVR(8),
19398c2ecf20Sopenharmony_ci	DBGBXVR(9),
19408c2ecf20Sopenharmony_ci	DBGBXVR(10),
19418c2ecf20Sopenharmony_ci	DBGBXVR(11),
19428c2ecf20Sopenharmony_ci	DBGBXVR(12),
19438c2ecf20Sopenharmony_ci	DBGBXVR(13),
19448c2ecf20Sopenharmony_ci	DBGBXVR(14),
19458c2ecf20Sopenharmony_ci	DBGBXVR(15),
19468c2ecf20Sopenharmony_ci
19478c2ecf20Sopenharmony_ci	/* DBGDSAR (32bit) */
19488c2ecf20Sopenharmony_ci	{ Op1( 0), CRn( 2), CRm( 0), Op2( 0), trap_raz_wi },
19498c2ecf20Sopenharmony_ci
19508c2ecf20Sopenharmony_ci	/* DBGDEVID2 */
19518c2ecf20Sopenharmony_ci	{ Op1( 0), CRn( 7), CRm( 0), Op2( 7), trap_raz_wi },
19528c2ecf20Sopenharmony_ci	/* DBGDEVID1 */
19538c2ecf20Sopenharmony_ci	{ Op1( 0), CRn( 7), CRm( 1), Op2( 7), trap_raz_wi },
19548c2ecf20Sopenharmony_ci	/* DBGDEVID */
19558c2ecf20Sopenharmony_ci	{ Op1( 0), CRn( 7), CRm( 2), Op2( 7), trap_raz_wi },
19568c2ecf20Sopenharmony_ci	/* DBGCLAIMSET */
19578c2ecf20Sopenharmony_ci	{ Op1( 0), CRn( 7), CRm( 8), Op2( 6), trap_raz_wi },
19588c2ecf20Sopenharmony_ci	/* DBGCLAIMCLR */
19598c2ecf20Sopenharmony_ci	{ Op1( 0), CRn( 7), CRm( 9), Op2( 6), trap_raz_wi },
19608c2ecf20Sopenharmony_ci	/* DBGAUTHSTATUS */
19618c2ecf20Sopenharmony_ci	{ Op1( 0), CRn( 7), CRm(14), Op2( 6), trap_dbgauthstatus_el1 },
19628c2ecf20Sopenharmony_ci};
19638c2ecf20Sopenharmony_ci
19648c2ecf20Sopenharmony_ci/* Trapped cp14 64bit registers */
19658c2ecf20Sopenharmony_cistatic const struct sys_reg_desc cp14_64_regs[] = {
19668c2ecf20Sopenharmony_ci	/* DBGDRAR (64bit) */
19678c2ecf20Sopenharmony_ci	{ Op1( 0), CRm( 1), .access = trap_raz_wi },
19688c2ecf20Sopenharmony_ci
19698c2ecf20Sopenharmony_ci	/* DBGDSAR (64bit) */
19708c2ecf20Sopenharmony_ci	{ Op1( 0), CRm( 2), .access = trap_raz_wi },
19718c2ecf20Sopenharmony_ci};
19728c2ecf20Sopenharmony_ci
19738c2ecf20Sopenharmony_ci/* Macro to expand the PMEVCNTRn register */
19748c2ecf20Sopenharmony_ci#define PMU_PMEVCNTR(n)							\
19758c2ecf20Sopenharmony_ci	/* PMEVCNTRn */							\
19768c2ecf20Sopenharmony_ci	{ Op1(0), CRn(0b1110),						\
19778c2ecf20Sopenharmony_ci	  CRm((0b1000 | (((n) >> 3) & 0x3))), Op2(((n) & 0x7)),		\
19788c2ecf20Sopenharmony_ci	  access_pmu_evcntr }
19798c2ecf20Sopenharmony_ci
19808c2ecf20Sopenharmony_ci/* Macro to expand the PMEVTYPERn register */
19818c2ecf20Sopenharmony_ci#define PMU_PMEVTYPER(n)						\
19828c2ecf20Sopenharmony_ci	/* PMEVTYPERn */						\
19838c2ecf20Sopenharmony_ci	{ Op1(0), CRn(0b1110),						\
19848c2ecf20Sopenharmony_ci	  CRm((0b1100 | (((n) >> 3) & 0x3))), Op2(((n) & 0x7)),		\
19858c2ecf20Sopenharmony_ci	  access_pmu_evtyper }
19868c2ecf20Sopenharmony_ci
19878c2ecf20Sopenharmony_ci/*
19888c2ecf20Sopenharmony_ci * Trapped cp15 registers. TTBR0/TTBR1 get a double encoding,
19898c2ecf20Sopenharmony_ci * depending on the way they are accessed (as a 32bit or a 64bit
19908c2ecf20Sopenharmony_ci * register).
19918c2ecf20Sopenharmony_ci */
19928c2ecf20Sopenharmony_cistatic const struct sys_reg_desc cp15_regs[] = {
19938c2ecf20Sopenharmony_ci	{ Op1( 0), CRn( 0), CRm( 0), Op2( 1), access_ctr },
19948c2ecf20Sopenharmony_ci	{ Op1( 0), CRn( 1), CRm( 0), Op2( 0), access_vm_reg, NULL, c1_SCTLR },
19958c2ecf20Sopenharmony_ci	{ Op1( 0), CRn( 1), CRm( 0), Op2( 1), access_actlr },
19968c2ecf20Sopenharmony_ci	{ Op1( 0), CRn( 1), CRm( 0), Op2( 3), access_actlr },
19978c2ecf20Sopenharmony_ci	{ Op1( 0), CRn( 2), CRm( 0), Op2( 0), access_vm_reg, NULL, c2_TTBR0 },
19988c2ecf20Sopenharmony_ci	{ Op1( 0), CRn( 2), CRm( 0), Op2( 1), access_vm_reg, NULL, c2_TTBR1 },
19998c2ecf20Sopenharmony_ci	{ Op1( 0), CRn( 2), CRm( 0), Op2( 2), access_vm_reg, NULL, c2_TTBCR },
20008c2ecf20Sopenharmony_ci	{ Op1( 0), CRn( 2), CRm( 0), Op2( 3), access_vm_reg, NULL, c2_TTBCR2 },
20018c2ecf20Sopenharmony_ci	{ Op1( 0), CRn( 3), CRm( 0), Op2( 0), access_vm_reg, NULL, c3_DACR },
20028c2ecf20Sopenharmony_ci	{ Op1( 0), CRn( 5), CRm( 0), Op2( 0), access_vm_reg, NULL, c5_DFSR },
20038c2ecf20Sopenharmony_ci	{ Op1( 0), CRn( 5), CRm( 0), Op2( 1), access_vm_reg, NULL, c5_IFSR },
20048c2ecf20Sopenharmony_ci	{ Op1( 0), CRn( 5), CRm( 1), Op2( 0), access_vm_reg, NULL, c5_ADFSR },
20058c2ecf20Sopenharmony_ci	{ Op1( 0), CRn( 5), CRm( 1), Op2( 1), access_vm_reg, NULL, c5_AIFSR },
20068c2ecf20Sopenharmony_ci	{ Op1( 0), CRn( 6), CRm( 0), Op2( 0), access_vm_reg, NULL, c6_DFAR },
20078c2ecf20Sopenharmony_ci	{ Op1( 0), CRn( 6), CRm( 0), Op2( 2), access_vm_reg, NULL, c6_IFAR },
20088c2ecf20Sopenharmony_ci
20098c2ecf20Sopenharmony_ci	/*
20108c2ecf20Sopenharmony_ci	 * DC{C,I,CI}SW operations:
20118c2ecf20Sopenharmony_ci	 */
20128c2ecf20Sopenharmony_ci	{ Op1( 0), CRn( 7), CRm( 6), Op2( 2), access_dcsw },
20138c2ecf20Sopenharmony_ci	{ Op1( 0), CRn( 7), CRm(10), Op2( 2), access_dcsw },
20148c2ecf20Sopenharmony_ci	{ Op1( 0), CRn( 7), CRm(14), Op2( 2), access_dcsw },
20158c2ecf20Sopenharmony_ci
20168c2ecf20Sopenharmony_ci	/* PMU */
20178c2ecf20Sopenharmony_ci	{ Op1( 0), CRn( 9), CRm(12), Op2( 0), access_pmcr },
20188c2ecf20Sopenharmony_ci	{ Op1( 0), CRn( 9), CRm(12), Op2( 1), access_pmcnten },
20198c2ecf20Sopenharmony_ci	{ Op1( 0), CRn( 9), CRm(12), Op2( 2), access_pmcnten },
20208c2ecf20Sopenharmony_ci	{ Op1( 0), CRn( 9), CRm(12), Op2( 3), access_pmovs },
20218c2ecf20Sopenharmony_ci	{ Op1( 0), CRn( 9), CRm(12), Op2( 4), access_pmswinc },
20228c2ecf20Sopenharmony_ci	{ Op1( 0), CRn( 9), CRm(12), Op2( 5), access_pmselr },
20238c2ecf20Sopenharmony_ci	{ Op1( 0), CRn( 9), CRm(12), Op2( 6), access_pmceid },
20248c2ecf20Sopenharmony_ci	{ Op1( 0), CRn( 9), CRm(12), Op2( 7), access_pmceid },
20258c2ecf20Sopenharmony_ci	{ Op1( 0), CRn( 9), CRm(13), Op2( 0), access_pmu_evcntr },
20268c2ecf20Sopenharmony_ci	{ Op1( 0), CRn( 9), CRm(13), Op2( 1), access_pmu_evtyper },
20278c2ecf20Sopenharmony_ci	{ Op1( 0), CRn( 9), CRm(13), Op2( 2), access_pmu_evcntr },
20288c2ecf20Sopenharmony_ci	{ Op1( 0), CRn( 9), CRm(14), Op2( 0), access_pmuserenr },
20298c2ecf20Sopenharmony_ci	{ Op1( 0), CRn( 9), CRm(14), Op2( 1), access_pminten },
20308c2ecf20Sopenharmony_ci	{ Op1( 0), CRn( 9), CRm(14), Op2( 2), access_pminten },
20318c2ecf20Sopenharmony_ci	{ Op1( 0), CRn( 9), CRm(14), Op2( 3), access_pmovs },
20328c2ecf20Sopenharmony_ci
20338c2ecf20Sopenharmony_ci	{ Op1( 0), CRn(10), CRm( 2), Op2( 0), access_vm_reg, NULL, c10_PRRR },
20348c2ecf20Sopenharmony_ci	{ Op1( 0), CRn(10), CRm( 2), Op2( 1), access_vm_reg, NULL, c10_NMRR },
20358c2ecf20Sopenharmony_ci	{ Op1( 0), CRn(10), CRm( 3), Op2( 0), access_vm_reg, NULL, c10_AMAIR0 },
20368c2ecf20Sopenharmony_ci	{ Op1( 0), CRn(10), CRm( 3), Op2( 1), access_vm_reg, NULL, c10_AMAIR1 },
20378c2ecf20Sopenharmony_ci
20388c2ecf20Sopenharmony_ci	/* ICC_SRE */
20398c2ecf20Sopenharmony_ci	{ Op1( 0), CRn(12), CRm(12), Op2( 5), access_gic_sre },
20408c2ecf20Sopenharmony_ci
20418c2ecf20Sopenharmony_ci	{ Op1( 0), CRn(13), CRm( 0), Op2( 1), access_vm_reg, NULL, c13_CID },
20428c2ecf20Sopenharmony_ci
20438c2ecf20Sopenharmony_ci	/* Arch Tmers */
20448c2ecf20Sopenharmony_ci	{ SYS_DESC(SYS_AARCH32_CNTP_TVAL), access_arch_timer },
20458c2ecf20Sopenharmony_ci	{ SYS_DESC(SYS_AARCH32_CNTP_CTL), access_arch_timer },
20468c2ecf20Sopenharmony_ci
20478c2ecf20Sopenharmony_ci	/* PMEVCNTRn */
20488c2ecf20Sopenharmony_ci	PMU_PMEVCNTR(0),
20498c2ecf20Sopenharmony_ci	PMU_PMEVCNTR(1),
20508c2ecf20Sopenharmony_ci	PMU_PMEVCNTR(2),
20518c2ecf20Sopenharmony_ci	PMU_PMEVCNTR(3),
20528c2ecf20Sopenharmony_ci	PMU_PMEVCNTR(4),
20538c2ecf20Sopenharmony_ci	PMU_PMEVCNTR(5),
20548c2ecf20Sopenharmony_ci	PMU_PMEVCNTR(6),
20558c2ecf20Sopenharmony_ci	PMU_PMEVCNTR(7),
20568c2ecf20Sopenharmony_ci	PMU_PMEVCNTR(8),
20578c2ecf20Sopenharmony_ci	PMU_PMEVCNTR(9),
20588c2ecf20Sopenharmony_ci	PMU_PMEVCNTR(10),
20598c2ecf20Sopenharmony_ci	PMU_PMEVCNTR(11),
20608c2ecf20Sopenharmony_ci	PMU_PMEVCNTR(12),
20618c2ecf20Sopenharmony_ci	PMU_PMEVCNTR(13),
20628c2ecf20Sopenharmony_ci	PMU_PMEVCNTR(14),
20638c2ecf20Sopenharmony_ci	PMU_PMEVCNTR(15),
20648c2ecf20Sopenharmony_ci	PMU_PMEVCNTR(16),
20658c2ecf20Sopenharmony_ci	PMU_PMEVCNTR(17),
20668c2ecf20Sopenharmony_ci	PMU_PMEVCNTR(18),
20678c2ecf20Sopenharmony_ci	PMU_PMEVCNTR(19),
20688c2ecf20Sopenharmony_ci	PMU_PMEVCNTR(20),
20698c2ecf20Sopenharmony_ci	PMU_PMEVCNTR(21),
20708c2ecf20Sopenharmony_ci	PMU_PMEVCNTR(22),
20718c2ecf20Sopenharmony_ci	PMU_PMEVCNTR(23),
20728c2ecf20Sopenharmony_ci	PMU_PMEVCNTR(24),
20738c2ecf20Sopenharmony_ci	PMU_PMEVCNTR(25),
20748c2ecf20Sopenharmony_ci	PMU_PMEVCNTR(26),
20758c2ecf20Sopenharmony_ci	PMU_PMEVCNTR(27),
20768c2ecf20Sopenharmony_ci	PMU_PMEVCNTR(28),
20778c2ecf20Sopenharmony_ci	PMU_PMEVCNTR(29),
20788c2ecf20Sopenharmony_ci	PMU_PMEVCNTR(30),
20798c2ecf20Sopenharmony_ci	/* PMEVTYPERn */
20808c2ecf20Sopenharmony_ci	PMU_PMEVTYPER(0),
20818c2ecf20Sopenharmony_ci	PMU_PMEVTYPER(1),
20828c2ecf20Sopenharmony_ci	PMU_PMEVTYPER(2),
20838c2ecf20Sopenharmony_ci	PMU_PMEVTYPER(3),
20848c2ecf20Sopenharmony_ci	PMU_PMEVTYPER(4),
20858c2ecf20Sopenharmony_ci	PMU_PMEVTYPER(5),
20868c2ecf20Sopenharmony_ci	PMU_PMEVTYPER(6),
20878c2ecf20Sopenharmony_ci	PMU_PMEVTYPER(7),
20888c2ecf20Sopenharmony_ci	PMU_PMEVTYPER(8),
20898c2ecf20Sopenharmony_ci	PMU_PMEVTYPER(9),
20908c2ecf20Sopenharmony_ci	PMU_PMEVTYPER(10),
20918c2ecf20Sopenharmony_ci	PMU_PMEVTYPER(11),
20928c2ecf20Sopenharmony_ci	PMU_PMEVTYPER(12),
20938c2ecf20Sopenharmony_ci	PMU_PMEVTYPER(13),
20948c2ecf20Sopenharmony_ci	PMU_PMEVTYPER(14),
20958c2ecf20Sopenharmony_ci	PMU_PMEVTYPER(15),
20968c2ecf20Sopenharmony_ci	PMU_PMEVTYPER(16),
20978c2ecf20Sopenharmony_ci	PMU_PMEVTYPER(17),
20988c2ecf20Sopenharmony_ci	PMU_PMEVTYPER(18),
20998c2ecf20Sopenharmony_ci	PMU_PMEVTYPER(19),
21008c2ecf20Sopenharmony_ci	PMU_PMEVTYPER(20),
21018c2ecf20Sopenharmony_ci	PMU_PMEVTYPER(21),
21028c2ecf20Sopenharmony_ci	PMU_PMEVTYPER(22),
21038c2ecf20Sopenharmony_ci	PMU_PMEVTYPER(23),
21048c2ecf20Sopenharmony_ci	PMU_PMEVTYPER(24),
21058c2ecf20Sopenharmony_ci	PMU_PMEVTYPER(25),
21068c2ecf20Sopenharmony_ci	PMU_PMEVTYPER(26),
21078c2ecf20Sopenharmony_ci	PMU_PMEVTYPER(27),
21088c2ecf20Sopenharmony_ci	PMU_PMEVTYPER(28),
21098c2ecf20Sopenharmony_ci	PMU_PMEVTYPER(29),
21108c2ecf20Sopenharmony_ci	PMU_PMEVTYPER(30),
21118c2ecf20Sopenharmony_ci	/* PMCCFILTR */
21128c2ecf20Sopenharmony_ci	{ Op1(0), CRn(14), CRm(15), Op2(7), access_pmu_evtyper },
21138c2ecf20Sopenharmony_ci
21148c2ecf20Sopenharmony_ci	{ Op1(1), CRn( 0), CRm( 0), Op2(0), access_ccsidr },
21158c2ecf20Sopenharmony_ci	{ Op1(1), CRn( 0), CRm( 0), Op2(1), access_clidr },
21168c2ecf20Sopenharmony_ci	{ Op1(2), CRn( 0), CRm( 0), Op2(0), access_csselr, NULL, c0_CSSELR },
21178c2ecf20Sopenharmony_ci};
21188c2ecf20Sopenharmony_ci
21198c2ecf20Sopenharmony_cistatic const struct sys_reg_desc cp15_64_regs[] = {
21208c2ecf20Sopenharmony_ci	{ Op1( 0), CRn( 0), CRm( 2), Op2( 0), access_vm_reg, NULL, c2_TTBR0 },
21218c2ecf20Sopenharmony_ci	{ Op1( 0), CRn( 0), CRm( 9), Op2( 0), access_pmu_evcntr },
21228c2ecf20Sopenharmony_ci	{ Op1( 0), CRn( 0), CRm(12), Op2( 0), access_gic_sgi }, /* ICC_SGI1R */
21238c2ecf20Sopenharmony_ci	{ Op1( 1), CRn( 0), CRm( 2), Op2( 0), access_vm_reg, NULL, c2_TTBR1 },
21248c2ecf20Sopenharmony_ci	{ Op1( 1), CRn( 0), CRm(12), Op2( 0), access_gic_sgi }, /* ICC_ASGI1R */
21258c2ecf20Sopenharmony_ci	{ Op1( 2), CRn( 0), CRm(12), Op2( 0), access_gic_sgi }, /* ICC_SGI0R */
21268c2ecf20Sopenharmony_ci	{ SYS_DESC(SYS_AARCH32_CNTP_CVAL),    access_arch_timer },
21278c2ecf20Sopenharmony_ci};
21288c2ecf20Sopenharmony_ci
21298c2ecf20Sopenharmony_cistatic int check_sysreg_table(const struct sys_reg_desc *table, unsigned int n,
21308c2ecf20Sopenharmony_ci			      bool is_32)
21318c2ecf20Sopenharmony_ci{
21328c2ecf20Sopenharmony_ci	unsigned int i;
21338c2ecf20Sopenharmony_ci
21348c2ecf20Sopenharmony_ci	for (i = 0; i < n; i++) {
21358c2ecf20Sopenharmony_ci		if (!is_32 && table[i].reg && !table[i].reset) {
21368c2ecf20Sopenharmony_ci			kvm_err("sys_reg table %p entry %d has lacks reset\n",
21378c2ecf20Sopenharmony_ci				table, i);
21388c2ecf20Sopenharmony_ci			return 1;
21398c2ecf20Sopenharmony_ci		}
21408c2ecf20Sopenharmony_ci
21418c2ecf20Sopenharmony_ci		if (i && cmp_sys_reg(&table[i-1], &table[i]) >= 0) {
21428c2ecf20Sopenharmony_ci			kvm_err("sys_reg table %p out of order (%d)\n", table, i - 1);
21438c2ecf20Sopenharmony_ci			return 1;
21448c2ecf20Sopenharmony_ci		}
21458c2ecf20Sopenharmony_ci	}
21468c2ecf20Sopenharmony_ci
21478c2ecf20Sopenharmony_ci	return 0;
21488c2ecf20Sopenharmony_ci}
21498c2ecf20Sopenharmony_ci
21508c2ecf20Sopenharmony_cistatic int match_sys_reg(const void *key, const void *elt)
21518c2ecf20Sopenharmony_ci{
21528c2ecf20Sopenharmony_ci	const unsigned long pval = (unsigned long)key;
21538c2ecf20Sopenharmony_ci	const struct sys_reg_desc *r = elt;
21548c2ecf20Sopenharmony_ci
21558c2ecf20Sopenharmony_ci	return pval - reg_to_encoding(r);
21568c2ecf20Sopenharmony_ci}
21578c2ecf20Sopenharmony_ci
21588c2ecf20Sopenharmony_cistatic const struct sys_reg_desc *find_reg(const struct sys_reg_params *params,
21598c2ecf20Sopenharmony_ci					 const struct sys_reg_desc table[],
21608c2ecf20Sopenharmony_ci					 unsigned int num)
21618c2ecf20Sopenharmony_ci{
21628c2ecf20Sopenharmony_ci	unsigned long pval = reg_to_encoding(params);
21638c2ecf20Sopenharmony_ci
21648c2ecf20Sopenharmony_ci	return bsearch((void *)pval, table, num, sizeof(table[0]), match_sys_reg);
21658c2ecf20Sopenharmony_ci}
21668c2ecf20Sopenharmony_ci
21678c2ecf20Sopenharmony_ciint kvm_handle_cp14_load_store(struct kvm_vcpu *vcpu)
21688c2ecf20Sopenharmony_ci{
21698c2ecf20Sopenharmony_ci	kvm_inject_undefined(vcpu);
21708c2ecf20Sopenharmony_ci	return 1;
21718c2ecf20Sopenharmony_ci}
21728c2ecf20Sopenharmony_ci
21738c2ecf20Sopenharmony_cistatic void perform_access(struct kvm_vcpu *vcpu,
21748c2ecf20Sopenharmony_ci			   struct sys_reg_params *params,
21758c2ecf20Sopenharmony_ci			   const struct sys_reg_desc *r)
21768c2ecf20Sopenharmony_ci{
21778c2ecf20Sopenharmony_ci	trace_kvm_sys_access(*vcpu_pc(vcpu), params, r);
21788c2ecf20Sopenharmony_ci
21798c2ecf20Sopenharmony_ci	/* Check for regs disabled by runtime config */
21808c2ecf20Sopenharmony_ci	if (sysreg_hidden(vcpu, r)) {
21818c2ecf20Sopenharmony_ci		kvm_inject_undefined(vcpu);
21828c2ecf20Sopenharmony_ci		return;
21838c2ecf20Sopenharmony_ci	}
21848c2ecf20Sopenharmony_ci
21858c2ecf20Sopenharmony_ci	/*
21868c2ecf20Sopenharmony_ci	 * Not having an accessor means that we have configured a trap
21878c2ecf20Sopenharmony_ci	 * that we don't know how to handle. This certainly qualifies
21888c2ecf20Sopenharmony_ci	 * as a gross bug that should be fixed right away.
21898c2ecf20Sopenharmony_ci	 */
21908c2ecf20Sopenharmony_ci	BUG_ON(!r->access);
21918c2ecf20Sopenharmony_ci
21928c2ecf20Sopenharmony_ci	/* Skip instruction if instructed so */
21938c2ecf20Sopenharmony_ci	if (likely(r->access(vcpu, params, r)))
21948c2ecf20Sopenharmony_ci		kvm_skip_instr(vcpu, kvm_vcpu_trap_il_is32bit(vcpu));
21958c2ecf20Sopenharmony_ci}
21968c2ecf20Sopenharmony_ci
21978c2ecf20Sopenharmony_ci/*
21988c2ecf20Sopenharmony_ci * emulate_cp --  tries to match a sys_reg access in a handling table, and
21998c2ecf20Sopenharmony_ci *                call the corresponding trap handler.
22008c2ecf20Sopenharmony_ci *
22018c2ecf20Sopenharmony_ci * @params: pointer to the descriptor of the access
22028c2ecf20Sopenharmony_ci * @table: array of trap descriptors
22038c2ecf20Sopenharmony_ci * @num: size of the trap descriptor array
22048c2ecf20Sopenharmony_ci *
22058c2ecf20Sopenharmony_ci * Return 0 if the access has been handled, and -1 if not.
22068c2ecf20Sopenharmony_ci */
22078c2ecf20Sopenharmony_cistatic int emulate_cp(struct kvm_vcpu *vcpu,
22088c2ecf20Sopenharmony_ci		      struct sys_reg_params *params,
22098c2ecf20Sopenharmony_ci		      const struct sys_reg_desc *table,
22108c2ecf20Sopenharmony_ci		      size_t num)
22118c2ecf20Sopenharmony_ci{
22128c2ecf20Sopenharmony_ci	const struct sys_reg_desc *r;
22138c2ecf20Sopenharmony_ci
22148c2ecf20Sopenharmony_ci	if (!table)
22158c2ecf20Sopenharmony_ci		return -1;	/* Not handled */
22168c2ecf20Sopenharmony_ci
22178c2ecf20Sopenharmony_ci	r = find_reg(params, table, num);
22188c2ecf20Sopenharmony_ci
22198c2ecf20Sopenharmony_ci	if (r) {
22208c2ecf20Sopenharmony_ci		perform_access(vcpu, params, r);
22218c2ecf20Sopenharmony_ci		return 0;
22228c2ecf20Sopenharmony_ci	}
22238c2ecf20Sopenharmony_ci
22248c2ecf20Sopenharmony_ci	/* Not handled */
22258c2ecf20Sopenharmony_ci	return -1;
22268c2ecf20Sopenharmony_ci}
22278c2ecf20Sopenharmony_ci
22288c2ecf20Sopenharmony_cistatic void unhandled_cp_access(struct kvm_vcpu *vcpu,
22298c2ecf20Sopenharmony_ci				struct sys_reg_params *params)
22308c2ecf20Sopenharmony_ci{
22318c2ecf20Sopenharmony_ci	u8 esr_ec = kvm_vcpu_trap_get_class(vcpu);
22328c2ecf20Sopenharmony_ci	int cp = -1;
22338c2ecf20Sopenharmony_ci
22348c2ecf20Sopenharmony_ci	switch (esr_ec) {
22358c2ecf20Sopenharmony_ci	case ESR_ELx_EC_CP15_32:
22368c2ecf20Sopenharmony_ci	case ESR_ELx_EC_CP15_64:
22378c2ecf20Sopenharmony_ci		cp = 15;
22388c2ecf20Sopenharmony_ci		break;
22398c2ecf20Sopenharmony_ci	case ESR_ELx_EC_CP14_MR:
22408c2ecf20Sopenharmony_ci	case ESR_ELx_EC_CP14_64:
22418c2ecf20Sopenharmony_ci		cp = 14;
22428c2ecf20Sopenharmony_ci		break;
22438c2ecf20Sopenharmony_ci	default:
22448c2ecf20Sopenharmony_ci		WARN_ON(1);
22458c2ecf20Sopenharmony_ci	}
22468c2ecf20Sopenharmony_ci
22478c2ecf20Sopenharmony_ci	print_sys_reg_msg(params,
22488c2ecf20Sopenharmony_ci			  "Unsupported guest CP%d access at: %08lx [%08lx]\n",
22498c2ecf20Sopenharmony_ci			  cp, *vcpu_pc(vcpu), *vcpu_cpsr(vcpu));
22508c2ecf20Sopenharmony_ci	kvm_inject_undefined(vcpu);
22518c2ecf20Sopenharmony_ci}
22528c2ecf20Sopenharmony_ci
22538c2ecf20Sopenharmony_ci/**
22548c2ecf20Sopenharmony_ci * kvm_handle_cp_64 -- handles a mrrc/mcrr trap on a guest CP14/CP15 access
22558c2ecf20Sopenharmony_ci * @vcpu: The VCPU pointer
22568c2ecf20Sopenharmony_ci * @run:  The kvm_run struct
22578c2ecf20Sopenharmony_ci */
22588c2ecf20Sopenharmony_cistatic int kvm_handle_cp_64(struct kvm_vcpu *vcpu,
22598c2ecf20Sopenharmony_ci			    const struct sys_reg_desc *global,
22608c2ecf20Sopenharmony_ci			    size_t nr_global)
22618c2ecf20Sopenharmony_ci{
22628c2ecf20Sopenharmony_ci	struct sys_reg_params params;
22638c2ecf20Sopenharmony_ci	u32 esr = kvm_vcpu_get_esr(vcpu);
22648c2ecf20Sopenharmony_ci	int Rt = kvm_vcpu_sys_get_rt(vcpu);
22658c2ecf20Sopenharmony_ci	int Rt2 = (esr >> 10) & 0x1f;
22668c2ecf20Sopenharmony_ci
22678c2ecf20Sopenharmony_ci	params.is_aarch32 = true;
22688c2ecf20Sopenharmony_ci	params.is_32bit = false;
22698c2ecf20Sopenharmony_ci	params.CRm = (esr >> 1) & 0xf;
22708c2ecf20Sopenharmony_ci	params.is_write = ((esr & 1) == 0);
22718c2ecf20Sopenharmony_ci
22728c2ecf20Sopenharmony_ci	params.Op0 = 0;
22738c2ecf20Sopenharmony_ci	params.Op1 = (esr >> 16) & 0xf;
22748c2ecf20Sopenharmony_ci	params.Op2 = 0;
22758c2ecf20Sopenharmony_ci	params.CRn = 0;
22768c2ecf20Sopenharmony_ci
22778c2ecf20Sopenharmony_ci	/*
22788c2ecf20Sopenharmony_ci	 * Make a 64-bit value out of Rt and Rt2. As we use the same trap
22798c2ecf20Sopenharmony_ci	 * backends between AArch32 and AArch64, we get away with it.
22808c2ecf20Sopenharmony_ci	 */
22818c2ecf20Sopenharmony_ci	if (params.is_write) {
22828c2ecf20Sopenharmony_ci		params.regval = vcpu_get_reg(vcpu, Rt) & 0xffffffff;
22838c2ecf20Sopenharmony_ci		params.regval |= vcpu_get_reg(vcpu, Rt2) << 32;
22848c2ecf20Sopenharmony_ci	}
22858c2ecf20Sopenharmony_ci
22868c2ecf20Sopenharmony_ci	/*
22878c2ecf20Sopenharmony_ci	 * If the table contains a handler, handle the
22888c2ecf20Sopenharmony_ci	 * potential register operation in the case of a read and return
22898c2ecf20Sopenharmony_ci	 * with success.
22908c2ecf20Sopenharmony_ci	 */
22918c2ecf20Sopenharmony_ci	if (!emulate_cp(vcpu, &params, global, nr_global)) {
22928c2ecf20Sopenharmony_ci		/* Split up the value between registers for the read side */
22938c2ecf20Sopenharmony_ci		if (!params.is_write) {
22948c2ecf20Sopenharmony_ci			vcpu_set_reg(vcpu, Rt, lower_32_bits(params.regval));
22958c2ecf20Sopenharmony_ci			vcpu_set_reg(vcpu, Rt2, upper_32_bits(params.regval));
22968c2ecf20Sopenharmony_ci		}
22978c2ecf20Sopenharmony_ci
22988c2ecf20Sopenharmony_ci		return 1;
22998c2ecf20Sopenharmony_ci	}
23008c2ecf20Sopenharmony_ci
23018c2ecf20Sopenharmony_ci	unhandled_cp_access(vcpu, &params);
23028c2ecf20Sopenharmony_ci	return 1;
23038c2ecf20Sopenharmony_ci}
23048c2ecf20Sopenharmony_ci
23058c2ecf20Sopenharmony_ci/**
23068c2ecf20Sopenharmony_ci * kvm_handle_cp_32 -- handles a mrc/mcr trap on a guest CP14/CP15 access
23078c2ecf20Sopenharmony_ci * @vcpu: The VCPU pointer
23088c2ecf20Sopenharmony_ci * @run:  The kvm_run struct
23098c2ecf20Sopenharmony_ci */
23108c2ecf20Sopenharmony_cistatic int kvm_handle_cp_32(struct kvm_vcpu *vcpu,
23118c2ecf20Sopenharmony_ci			    const struct sys_reg_desc *global,
23128c2ecf20Sopenharmony_ci			    size_t nr_global)
23138c2ecf20Sopenharmony_ci{
23148c2ecf20Sopenharmony_ci	struct sys_reg_params params;
23158c2ecf20Sopenharmony_ci	u32 esr = kvm_vcpu_get_esr(vcpu);
23168c2ecf20Sopenharmony_ci	int Rt  = kvm_vcpu_sys_get_rt(vcpu);
23178c2ecf20Sopenharmony_ci
23188c2ecf20Sopenharmony_ci	params.is_aarch32 = true;
23198c2ecf20Sopenharmony_ci	params.is_32bit = true;
23208c2ecf20Sopenharmony_ci	params.CRm = (esr >> 1) & 0xf;
23218c2ecf20Sopenharmony_ci	params.regval = vcpu_get_reg(vcpu, Rt);
23228c2ecf20Sopenharmony_ci	params.is_write = ((esr & 1) == 0);
23238c2ecf20Sopenharmony_ci	params.CRn = (esr >> 10) & 0xf;
23248c2ecf20Sopenharmony_ci	params.Op0 = 0;
23258c2ecf20Sopenharmony_ci	params.Op1 = (esr >> 14) & 0x7;
23268c2ecf20Sopenharmony_ci	params.Op2 = (esr >> 17) & 0x7;
23278c2ecf20Sopenharmony_ci
23288c2ecf20Sopenharmony_ci	if (!emulate_cp(vcpu, &params, global, nr_global)) {
23298c2ecf20Sopenharmony_ci		if (!params.is_write)
23308c2ecf20Sopenharmony_ci			vcpu_set_reg(vcpu, Rt, params.regval);
23318c2ecf20Sopenharmony_ci		return 1;
23328c2ecf20Sopenharmony_ci	}
23338c2ecf20Sopenharmony_ci
23348c2ecf20Sopenharmony_ci	unhandled_cp_access(vcpu, &params);
23358c2ecf20Sopenharmony_ci	return 1;
23368c2ecf20Sopenharmony_ci}
23378c2ecf20Sopenharmony_ci
23388c2ecf20Sopenharmony_ciint kvm_handle_cp15_64(struct kvm_vcpu *vcpu)
23398c2ecf20Sopenharmony_ci{
23408c2ecf20Sopenharmony_ci	return kvm_handle_cp_64(vcpu, cp15_64_regs, ARRAY_SIZE(cp15_64_regs));
23418c2ecf20Sopenharmony_ci}
23428c2ecf20Sopenharmony_ci
23438c2ecf20Sopenharmony_ciint kvm_handle_cp15_32(struct kvm_vcpu *vcpu)
23448c2ecf20Sopenharmony_ci{
23458c2ecf20Sopenharmony_ci	return kvm_handle_cp_32(vcpu, cp15_regs, ARRAY_SIZE(cp15_regs));
23468c2ecf20Sopenharmony_ci}
23478c2ecf20Sopenharmony_ci
23488c2ecf20Sopenharmony_ciint kvm_handle_cp14_64(struct kvm_vcpu *vcpu)
23498c2ecf20Sopenharmony_ci{
23508c2ecf20Sopenharmony_ci	return kvm_handle_cp_64(vcpu, cp14_64_regs, ARRAY_SIZE(cp14_64_regs));
23518c2ecf20Sopenharmony_ci}
23528c2ecf20Sopenharmony_ci
23538c2ecf20Sopenharmony_ciint kvm_handle_cp14_32(struct kvm_vcpu *vcpu)
23548c2ecf20Sopenharmony_ci{
23558c2ecf20Sopenharmony_ci	return kvm_handle_cp_32(vcpu, cp14_regs, ARRAY_SIZE(cp14_regs));
23568c2ecf20Sopenharmony_ci}
23578c2ecf20Sopenharmony_ci
23588c2ecf20Sopenharmony_cistatic bool is_imp_def_sys_reg(struct sys_reg_params *params)
23598c2ecf20Sopenharmony_ci{
23608c2ecf20Sopenharmony_ci	// See ARM DDI 0487E.a, section D12.3.2
23618c2ecf20Sopenharmony_ci	return params->Op0 == 3 && (params->CRn & 0b1011) == 0b1011;
23628c2ecf20Sopenharmony_ci}
23638c2ecf20Sopenharmony_ci
23648c2ecf20Sopenharmony_cistatic int emulate_sys_reg(struct kvm_vcpu *vcpu,
23658c2ecf20Sopenharmony_ci			   struct sys_reg_params *params)
23668c2ecf20Sopenharmony_ci{
23678c2ecf20Sopenharmony_ci	const struct sys_reg_desc *r;
23688c2ecf20Sopenharmony_ci
23698c2ecf20Sopenharmony_ci	r = find_reg(params, sys_reg_descs, ARRAY_SIZE(sys_reg_descs));
23708c2ecf20Sopenharmony_ci
23718c2ecf20Sopenharmony_ci	if (likely(r)) {
23728c2ecf20Sopenharmony_ci		perform_access(vcpu, params, r);
23738c2ecf20Sopenharmony_ci	} else if (is_imp_def_sys_reg(params)) {
23748c2ecf20Sopenharmony_ci		kvm_inject_undefined(vcpu);
23758c2ecf20Sopenharmony_ci	} else {
23768c2ecf20Sopenharmony_ci		print_sys_reg_msg(params,
23778c2ecf20Sopenharmony_ci				  "Unsupported guest sys_reg access at: %lx [%08lx]\n",
23788c2ecf20Sopenharmony_ci				  *vcpu_pc(vcpu), *vcpu_cpsr(vcpu));
23798c2ecf20Sopenharmony_ci		kvm_inject_undefined(vcpu);
23808c2ecf20Sopenharmony_ci	}
23818c2ecf20Sopenharmony_ci	return 1;
23828c2ecf20Sopenharmony_ci}
23838c2ecf20Sopenharmony_ci
23848c2ecf20Sopenharmony_ci/**
23858c2ecf20Sopenharmony_ci * kvm_reset_sys_regs - sets system registers to reset value
23868c2ecf20Sopenharmony_ci * @vcpu: The VCPU pointer
23878c2ecf20Sopenharmony_ci *
23888c2ecf20Sopenharmony_ci * This function finds the right table above and sets the registers on the
23898c2ecf20Sopenharmony_ci * virtual CPU struct to their architecturally defined reset values.
23908c2ecf20Sopenharmony_ci */
23918c2ecf20Sopenharmony_civoid kvm_reset_sys_regs(struct kvm_vcpu *vcpu)
23928c2ecf20Sopenharmony_ci{
23938c2ecf20Sopenharmony_ci	unsigned long i;
23948c2ecf20Sopenharmony_ci
23958c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(sys_reg_descs); i++)
23968c2ecf20Sopenharmony_ci		if (sys_reg_descs[i].reset)
23978c2ecf20Sopenharmony_ci			sys_reg_descs[i].reset(vcpu, &sys_reg_descs[i]);
23988c2ecf20Sopenharmony_ci}
23998c2ecf20Sopenharmony_ci
24008c2ecf20Sopenharmony_ci/**
24018c2ecf20Sopenharmony_ci * kvm_handle_sys_reg -- handles a mrs/msr trap on a guest sys_reg access
24028c2ecf20Sopenharmony_ci * @vcpu: The VCPU pointer
24038c2ecf20Sopenharmony_ci */
24048c2ecf20Sopenharmony_ciint kvm_handle_sys_reg(struct kvm_vcpu *vcpu)
24058c2ecf20Sopenharmony_ci{
24068c2ecf20Sopenharmony_ci	struct sys_reg_params params;
24078c2ecf20Sopenharmony_ci	unsigned long esr = kvm_vcpu_get_esr(vcpu);
24088c2ecf20Sopenharmony_ci	int Rt = kvm_vcpu_sys_get_rt(vcpu);
24098c2ecf20Sopenharmony_ci	int ret;
24108c2ecf20Sopenharmony_ci
24118c2ecf20Sopenharmony_ci	trace_kvm_handle_sys_reg(esr);
24128c2ecf20Sopenharmony_ci
24138c2ecf20Sopenharmony_ci	params.is_aarch32 = false;
24148c2ecf20Sopenharmony_ci	params.is_32bit = false;
24158c2ecf20Sopenharmony_ci	params.Op0 = (esr >> 20) & 3;
24168c2ecf20Sopenharmony_ci	params.Op1 = (esr >> 14) & 0x7;
24178c2ecf20Sopenharmony_ci	params.CRn = (esr >> 10) & 0xf;
24188c2ecf20Sopenharmony_ci	params.CRm = (esr >> 1) & 0xf;
24198c2ecf20Sopenharmony_ci	params.Op2 = (esr >> 17) & 0x7;
24208c2ecf20Sopenharmony_ci	params.regval = vcpu_get_reg(vcpu, Rt);
24218c2ecf20Sopenharmony_ci	params.is_write = !(esr & 1);
24228c2ecf20Sopenharmony_ci
24238c2ecf20Sopenharmony_ci	ret = emulate_sys_reg(vcpu, &params);
24248c2ecf20Sopenharmony_ci
24258c2ecf20Sopenharmony_ci	if (!params.is_write)
24268c2ecf20Sopenharmony_ci		vcpu_set_reg(vcpu, Rt, params.regval);
24278c2ecf20Sopenharmony_ci	return ret;
24288c2ecf20Sopenharmony_ci}
24298c2ecf20Sopenharmony_ci
24308c2ecf20Sopenharmony_ci/******************************************************************************
24318c2ecf20Sopenharmony_ci * Userspace API
24328c2ecf20Sopenharmony_ci *****************************************************************************/
24338c2ecf20Sopenharmony_ci
24348c2ecf20Sopenharmony_cistatic bool index_to_params(u64 id, struct sys_reg_params *params)
24358c2ecf20Sopenharmony_ci{
24368c2ecf20Sopenharmony_ci	switch (id & KVM_REG_SIZE_MASK) {
24378c2ecf20Sopenharmony_ci	case KVM_REG_SIZE_U64:
24388c2ecf20Sopenharmony_ci		/* Any unused index bits means it's not valid. */
24398c2ecf20Sopenharmony_ci		if (id & ~(KVM_REG_ARCH_MASK | KVM_REG_SIZE_MASK
24408c2ecf20Sopenharmony_ci			      | KVM_REG_ARM_COPROC_MASK
24418c2ecf20Sopenharmony_ci			      | KVM_REG_ARM64_SYSREG_OP0_MASK
24428c2ecf20Sopenharmony_ci			      | KVM_REG_ARM64_SYSREG_OP1_MASK
24438c2ecf20Sopenharmony_ci			      | KVM_REG_ARM64_SYSREG_CRN_MASK
24448c2ecf20Sopenharmony_ci			      | KVM_REG_ARM64_SYSREG_CRM_MASK
24458c2ecf20Sopenharmony_ci			      | KVM_REG_ARM64_SYSREG_OP2_MASK))
24468c2ecf20Sopenharmony_ci			return false;
24478c2ecf20Sopenharmony_ci		params->Op0 = ((id & KVM_REG_ARM64_SYSREG_OP0_MASK)
24488c2ecf20Sopenharmony_ci			       >> KVM_REG_ARM64_SYSREG_OP0_SHIFT);
24498c2ecf20Sopenharmony_ci		params->Op1 = ((id & KVM_REG_ARM64_SYSREG_OP1_MASK)
24508c2ecf20Sopenharmony_ci			       >> KVM_REG_ARM64_SYSREG_OP1_SHIFT);
24518c2ecf20Sopenharmony_ci		params->CRn = ((id & KVM_REG_ARM64_SYSREG_CRN_MASK)
24528c2ecf20Sopenharmony_ci			       >> KVM_REG_ARM64_SYSREG_CRN_SHIFT);
24538c2ecf20Sopenharmony_ci		params->CRm = ((id & KVM_REG_ARM64_SYSREG_CRM_MASK)
24548c2ecf20Sopenharmony_ci			       >> KVM_REG_ARM64_SYSREG_CRM_SHIFT);
24558c2ecf20Sopenharmony_ci		params->Op2 = ((id & KVM_REG_ARM64_SYSREG_OP2_MASK)
24568c2ecf20Sopenharmony_ci			       >> KVM_REG_ARM64_SYSREG_OP2_SHIFT);
24578c2ecf20Sopenharmony_ci		return true;
24588c2ecf20Sopenharmony_ci	default:
24598c2ecf20Sopenharmony_ci		return false;
24608c2ecf20Sopenharmony_ci	}
24618c2ecf20Sopenharmony_ci}
24628c2ecf20Sopenharmony_ci
24638c2ecf20Sopenharmony_ciconst struct sys_reg_desc *find_reg_by_id(u64 id,
24648c2ecf20Sopenharmony_ci					  struct sys_reg_params *params,
24658c2ecf20Sopenharmony_ci					  const struct sys_reg_desc table[],
24668c2ecf20Sopenharmony_ci					  unsigned int num)
24678c2ecf20Sopenharmony_ci{
24688c2ecf20Sopenharmony_ci	if (!index_to_params(id, params))
24698c2ecf20Sopenharmony_ci		return NULL;
24708c2ecf20Sopenharmony_ci
24718c2ecf20Sopenharmony_ci	return find_reg(params, table, num);
24728c2ecf20Sopenharmony_ci}
24738c2ecf20Sopenharmony_ci
24748c2ecf20Sopenharmony_ci/* Decode an index value, and find the sys_reg_desc entry. */
24758c2ecf20Sopenharmony_cistatic const struct sys_reg_desc *index_to_sys_reg_desc(struct kvm_vcpu *vcpu,
24768c2ecf20Sopenharmony_ci						    u64 id)
24778c2ecf20Sopenharmony_ci{
24788c2ecf20Sopenharmony_ci	const struct sys_reg_desc *r;
24798c2ecf20Sopenharmony_ci	struct sys_reg_params params;
24808c2ecf20Sopenharmony_ci
24818c2ecf20Sopenharmony_ci	/* We only do sys_reg for now. */
24828c2ecf20Sopenharmony_ci	if ((id & KVM_REG_ARM_COPROC_MASK) != KVM_REG_ARM64_SYSREG)
24838c2ecf20Sopenharmony_ci		return NULL;
24848c2ecf20Sopenharmony_ci
24858c2ecf20Sopenharmony_ci	if (!index_to_params(id, &params))
24868c2ecf20Sopenharmony_ci		return NULL;
24878c2ecf20Sopenharmony_ci
24888c2ecf20Sopenharmony_ci	r = find_reg(&params, sys_reg_descs, ARRAY_SIZE(sys_reg_descs));
24898c2ecf20Sopenharmony_ci
24908c2ecf20Sopenharmony_ci	/* Not saved in the sys_reg array and not otherwise accessible? */
24918c2ecf20Sopenharmony_ci	if (r && !(r->reg || r->get_user))
24928c2ecf20Sopenharmony_ci		r = NULL;
24938c2ecf20Sopenharmony_ci
24948c2ecf20Sopenharmony_ci	return r;
24958c2ecf20Sopenharmony_ci}
24968c2ecf20Sopenharmony_ci
24978c2ecf20Sopenharmony_ci/*
24988c2ecf20Sopenharmony_ci * These are the invariant sys_reg registers: we let the guest see the
24998c2ecf20Sopenharmony_ci * host versions of these, so they're part of the guest state.
25008c2ecf20Sopenharmony_ci *
25018c2ecf20Sopenharmony_ci * A future CPU may provide a mechanism to present different values to
25028c2ecf20Sopenharmony_ci * the guest, or a future kvm may trap them.
25038c2ecf20Sopenharmony_ci */
25048c2ecf20Sopenharmony_ci
25058c2ecf20Sopenharmony_ci#define FUNCTION_INVARIANT(reg)						\
25068c2ecf20Sopenharmony_ci	static void get_##reg(struct kvm_vcpu *v,			\
25078c2ecf20Sopenharmony_ci			      const struct sys_reg_desc *r)		\
25088c2ecf20Sopenharmony_ci	{								\
25098c2ecf20Sopenharmony_ci		((struct sys_reg_desc *)r)->val = read_sysreg(reg);	\
25108c2ecf20Sopenharmony_ci	}
25118c2ecf20Sopenharmony_ci
25128c2ecf20Sopenharmony_ciFUNCTION_INVARIANT(midr_el1)
25138c2ecf20Sopenharmony_ciFUNCTION_INVARIANT(revidr_el1)
25148c2ecf20Sopenharmony_ciFUNCTION_INVARIANT(clidr_el1)
25158c2ecf20Sopenharmony_ciFUNCTION_INVARIANT(aidr_el1)
25168c2ecf20Sopenharmony_ci
25178c2ecf20Sopenharmony_cistatic void get_ctr_el0(struct kvm_vcpu *v, const struct sys_reg_desc *r)
25188c2ecf20Sopenharmony_ci{
25198c2ecf20Sopenharmony_ci	((struct sys_reg_desc *)r)->val = read_sanitised_ftr_reg(SYS_CTR_EL0);
25208c2ecf20Sopenharmony_ci}
25218c2ecf20Sopenharmony_ci
25228c2ecf20Sopenharmony_ci/* ->val is filled in by kvm_sys_reg_table_init() */
25238c2ecf20Sopenharmony_cistatic struct sys_reg_desc invariant_sys_regs[] = {
25248c2ecf20Sopenharmony_ci	{ SYS_DESC(SYS_MIDR_EL1), NULL, get_midr_el1 },
25258c2ecf20Sopenharmony_ci	{ SYS_DESC(SYS_REVIDR_EL1), NULL, get_revidr_el1 },
25268c2ecf20Sopenharmony_ci	{ SYS_DESC(SYS_CLIDR_EL1), NULL, get_clidr_el1 },
25278c2ecf20Sopenharmony_ci	{ SYS_DESC(SYS_AIDR_EL1), NULL, get_aidr_el1 },
25288c2ecf20Sopenharmony_ci	{ SYS_DESC(SYS_CTR_EL0), NULL, get_ctr_el0 },
25298c2ecf20Sopenharmony_ci};
25308c2ecf20Sopenharmony_ci
25318c2ecf20Sopenharmony_cistatic int reg_from_user(u64 *val, const void __user *uaddr, u64 id)
25328c2ecf20Sopenharmony_ci{
25338c2ecf20Sopenharmony_ci	if (copy_from_user(val, uaddr, KVM_REG_SIZE(id)) != 0)
25348c2ecf20Sopenharmony_ci		return -EFAULT;
25358c2ecf20Sopenharmony_ci	return 0;
25368c2ecf20Sopenharmony_ci}
25378c2ecf20Sopenharmony_ci
25388c2ecf20Sopenharmony_cistatic int reg_to_user(void __user *uaddr, const u64 *val, u64 id)
25398c2ecf20Sopenharmony_ci{
25408c2ecf20Sopenharmony_ci	if (copy_to_user(uaddr, val, KVM_REG_SIZE(id)) != 0)
25418c2ecf20Sopenharmony_ci		return -EFAULT;
25428c2ecf20Sopenharmony_ci	return 0;
25438c2ecf20Sopenharmony_ci}
25448c2ecf20Sopenharmony_ci
25458c2ecf20Sopenharmony_cistatic int get_invariant_sys_reg(u64 id, void __user *uaddr)
25468c2ecf20Sopenharmony_ci{
25478c2ecf20Sopenharmony_ci	struct sys_reg_params params;
25488c2ecf20Sopenharmony_ci	const struct sys_reg_desc *r;
25498c2ecf20Sopenharmony_ci
25508c2ecf20Sopenharmony_ci	r = find_reg_by_id(id, &params, invariant_sys_regs,
25518c2ecf20Sopenharmony_ci			   ARRAY_SIZE(invariant_sys_regs));
25528c2ecf20Sopenharmony_ci	if (!r)
25538c2ecf20Sopenharmony_ci		return -ENOENT;
25548c2ecf20Sopenharmony_ci
25558c2ecf20Sopenharmony_ci	return reg_to_user(uaddr, &r->val, id);
25568c2ecf20Sopenharmony_ci}
25578c2ecf20Sopenharmony_ci
25588c2ecf20Sopenharmony_cistatic int set_invariant_sys_reg(u64 id, void __user *uaddr)
25598c2ecf20Sopenharmony_ci{
25608c2ecf20Sopenharmony_ci	struct sys_reg_params params;
25618c2ecf20Sopenharmony_ci	const struct sys_reg_desc *r;
25628c2ecf20Sopenharmony_ci	int err;
25638c2ecf20Sopenharmony_ci	u64 val = 0; /* Make sure high bits are 0 for 32-bit regs */
25648c2ecf20Sopenharmony_ci
25658c2ecf20Sopenharmony_ci	r = find_reg_by_id(id, &params, invariant_sys_regs,
25668c2ecf20Sopenharmony_ci			   ARRAY_SIZE(invariant_sys_regs));
25678c2ecf20Sopenharmony_ci	if (!r)
25688c2ecf20Sopenharmony_ci		return -ENOENT;
25698c2ecf20Sopenharmony_ci
25708c2ecf20Sopenharmony_ci	err = reg_from_user(&val, uaddr, id);
25718c2ecf20Sopenharmony_ci	if (err)
25728c2ecf20Sopenharmony_ci		return err;
25738c2ecf20Sopenharmony_ci
25748c2ecf20Sopenharmony_ci	/* This is what we mean by invariant: you can't change it. */
25758c2ecf20Sopenharmony_ci	if (r->val != val)
25768c2ecf20Sopenharmony_ci		return -EINVAL;
25778c2ecf20Sopenharmony_ci
25788c2ecf20Sopenharmony_ci	return 0;
25798c2ecf20Sopenharmony_ci}
25808c2ecf20Sopenharmony_ci
25818c2ecf20Sopenharmony_cistatic bool is_valid_cache(u32 val)
25828c2ecf20Sopenharmony_ci{
25838c2ecf20Sopenharmony_ci	u32 level, ctype;
25848c2ecf20Sopenharmony_ci
25858c2ecf20Sopenharmony_ci	if (val >= CSSELR_MAX)
25868c2ecf20Sopenharmony_ci		return false;
25878c2ecf20Sopenharmony_ci
25888c2ecf20Sopenharmony_ci	/* Bottom bit is Instruction or Data bit.  Next 3 bits are level. */
25898c2ecf20Sopenharmony_ci	level = (val >> 1);
25908c2ecf20Sopenharmony_ci	ctype = (cache_levels >> (level * 3)) & 7;
25918c2ecf20Sopenharmony_ci
25928c2ecf20Sopenharmony_ci	switch (ctype) {
25938c2ecf20Sopenharmony_ci	case 0: /* No cache */
25948c2ecf20Sopenharmony_ci		return false;
25958c2ecf20Sopenharmony_ci	case 1: /* Instruction cache only */
25968c2ecf20Sopenharmony_ci		return (val & 1);
25978c2ecf20Sopenharmony_ci	case 2: /* Data cache only */
25988c2ecf20Sopenharmony_ci	case 4: /* Unified cache */
25998c2ecf20Sopenharmony_ci		return !(val & 1);
26008c2ecf20Sopenharmony_ci	case 3: /* Separate instruction and data caches */
26018c2ecf20Sopenharmony_ci		return true;
26028c2ecf20Sopenharmony_ci	default: /* Reserved: we can't know instruction or data. */
26038c2ecf20Sopenharmony_ci		return false;
26048c2ecf20Sopenharmony_ci	}
26058c2ecf20Sopenharmony_ci}
26068c2ecf20Sopenharmony_ci
26078c2ecf20Sopenharmony_cistatic int demux_c15_get(u64 id, void __user *uaddr)
26088c2ecf20Sopenharmony_ci{
26098c2ecf20Sopenharmony_ci	u32 val;
26108c2ecf20Sopenharmony_ci	u32 __user *uval = uaddr;
26118c2ecf20Sopenharmony_ci
26128c2ecf20Sopenharmony_ci	/* Fail if we have unknown bits set. */
26138c2ecf20Sopenharmony_ci	if (id & ~(KVM_REG_ARCH_MASK|KVM_REG_SIZE_MASK|KVM_REG_ARM_COPROC_MASK
26148c2ecf20Sopenharmony_ci		   | ((1 << KVM_REG_ARM_COPROC_SHIFT)-1)))
26158c2ecf20Sopenharmony_ci		return -ENOENT;
26168c2ecf20Sopenharmony_ci
26178c2ecf20Sopenharmony_ci	switch (id & KVM_REG_ARM_DEMUX_ID_MASK) {
26188c2ecf20Sopenharmony_ci	case KVM_REG_ARM_DEMUX_ID_CCSIDR:
26198c2ecf20Sopenharmony_ci		if (KVM_REG_SIZE(id) != 4)
26208c2ecf20Sopenharmony_ci			return -ENOENT;
26218c2ecf20Sopenharmony_ci		val = (id & KVM_REG_ARM_DEMUX_VAL_MASK)
26228c2ecf20Sopenharmony_ci			>> KVM_REG_ARM_DEMUX_VAL_SHIFT;
26238c2ecf20Sopenharmony_ci		if (!is_valid_cache(val))
26248c2ecf20Sopenharmony_ci			return -ENOENT;
26258c2ecf20Sopenharmony_ci
26268c2ecf20Sopenharmony_ci		return put_user(get_ccsidr(val), uval);
26278c2ecf20Sopenharmony_ci	default:
26288c2ecf20Sopenharmony_ci		return -ENOENT;
26298c2ecf20Sopenharmony_ci	}
26308c2ecf20Sopenharmony_ci}
26318c2ecf20Sopenharmony_ci
26328c2ecf20Sopenharmony_cistatic int demux_c15_set(u64 id, void __user *uaddr)
26338c2ecf20Sopenharmony_ci{
26348c2ecf20Sopenharmony_ci	u32 val, newval;
26358c2ecf20Sopenharmony_ci	u32 __user *uval = uaddr;
26368c2ecf20Sopenharmony_ci
26378c2ecf20Sopenharmony_ci	/* Fail if we have unknown bits set. */
26388c2ecf20Sopenharmony_ci	if (id & ~(KVM_REG_ARCH_MASK|KVM_REG_SIZE_MASK|KVM_REG_ARM_COPROC_MASK
26398c2ecf20Sopenharmony_ci		   | ((1 << KVM_REG_ARM_COPROC_SHIFT)-1)))
26408c2ecf20Sopenharmony_ci		return -ENOENT;
26418c2ecf20Sopenharmony_ci
26428c2ecf20Sopenharmony_ci	switch (id & KVM_REG_ARM_DEMUX_ID_MASK) {
26438c2ecf20Sopenharmony_ci	case KVM_REG_ARM_DEMUX_ID_CCSIDR:
26448c2ecf20Sopenharmony_ci		if (KVM_REG_SIZE(id) != 4)
26458c2ecf20Sopenharmony_ci			return -ENOENT;
26468c2ecf20Sopenharmony_ci		val = (id & KVM_REG_ARM_DEMUX_VAL_MASK)
26478c2ecf20Sopenharmony_ci			>> KVM_REG_ARM_DEMUX_VAL_SHIFT;
26488c2ecf20Sopenharmony_ci		if (!is_valid_cache(val))
26498c2ecf20Sopenharmony_ci			return -ENOENT;
26508c2ecf20Sopenharmony_ci
26518c2ecf20Sopenharmony_ci		if (get_user(newval, uval))
26528c2ecf20Sopenharmony_ci			return -EFAULT;
26538c2ecf20Sopenharmony_ci
26548c2ecf20Sopenharmony_ci		/* This is also invariant: you can't change it. */
26558c2ecf20Sopenharmony_ci		if (newval != get_ccsidr(val))
26568c2ecf20Sopenharmony_ci			return -EINVAL;
26578c2ecf20Sopenharmony_ci		return 0;
26588c2ecf20Sopenharmony_ci	default:
26598c2ecf20Sopenharmony_ci		return -ENOENT;
26608c2ecf20Sopenharmony_ci	}
26618c2ecf20Sopenharmony_ci}
26628c2ecf20Sopenharmony_ci
26638c2ecf20Sopenharmony_ciint kvm_arm_sys_reg_get_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
26648c2ecf20Sopenharmony_ci{
26658c2ecf20Sopenharmony_ci	const struct sys_reg_desc *r;
26668c2ecf20Sopenharmony_ci	void __user *uaddr = (void __user *)(unsigned long)reg->addr;
26678c2ecf20Sopenharmony_ci
26688c2ecf20Sopenharmony_ci	if ((reg->id & KVM_REG_ARM_COPROC_MASK) == KVM_REG_ARM_DEMUX)
26698c2ecf20Sopenharmony_ci		return demux_c15_get(reg->id, uaddr);
26708c2ecf20Sopenharmony_ci
26718c2ecf20Sopenharmony_ci	if (KVM_REG_SIZE(reg->id) != sizeof(__u64))
26728c2ecf20Sopenharmony_ci		return -ENOENT;
26738c2ecf20Sopenharmony_ci
26748c2ecf20Sopenharmony_ci	r = index_to_sys_reg_desc(vcpu, reg->id);
26758c2ecf20Sopenharmony_ci	if (!r)
26768c2ecf20Sopenharmony_ci		return get_invariant_sys_reg(reg->id, uaddr);
26778c2ecf20Sopenharmony_ci
26788c2ecf20Sopenharmony_ci	/* Check for regs disabled by runtime config */
26798c2ecf20Sopenharmony_ci	if (sysreg_hidden(vcpu, r))
26808c2ecf20Sopenharmony_ci		return -ENOENT;
26818c2ecf20Sopenharmony_ci
26828c2ecf20Sopenharmony_ci	if (r->get_user)
26838c2ecf20Sopenharmony_ci		return (r->get_user)(vcpu, r, reg, uaddr);
26848c2ecf20Sopenharmony_ci
26858c2ecf20Sopenharmony_ci	return reg_to_user(uaddr, &__vcpu_sys_reg(vcpu, r->reg), reg->id);
26868c2ecf20Sopenharmony_ci}
26878c2ecf20Sopenharmony_ci
26888c2ecf20Sopenharmony_ciint kvm_arm_sys_reg_set_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
26898c2ecf20Sopenharmony_ci{
26908c2ecf20Sopenharmony_ci	const struct sys_reg_desc *r;
26918c2ecf20Sopenharmony_ci	void __user *uaddr = (void __user *)(unsigned long)reg->addr;
26928c2ecf20Sopenharmony_ci
26938c2ecf20Sopenharmony_ci	if ((reg->id & KVM_REG_ARM_COPROC_MASK) == KVM_REG_ARM_DEMUX)
26948c2ecf20Sopenharmony_ci		return demux_c15_set(reg->id, uaddr);
26958c2ecf20Sopenharmony_ci
26968c2ecf20Sopenharmony_ci	if (KVM_REG_SIZE(reg->id) != sizeof(__u64))
26978c2ecf20Sopenharmony_ci		return -ENOENT;
26988c2ecf20Sopenharmony_ci
26998c2ecf20Sopenharmony_ci	r = index_to_sys_reg_desc(vcpu, reg->id);
27008c2ecf20Sopenharmony_ci	if (!r)
27018c2ecf20Sopenharmony_ci		return set_invariant_sys_reg(reg->id, uaddr);
27028c2ecf20Sopenharmony_ci
27038c2ecf20Sopenharmony_ci	/* Check for regs disabled by runtime config */
27048c2ecf20Sopenharmony_ci	if (sysreg_hidden(vcpu, r))
27058c2ecf20Sopenharmony_ci		return -ENOENT;
27068c2ecf20Sopenharmony_ci
27078c2ecf20Sopenharmony_ci	if (r->set_user)
27088c2ecf20Sopenharmony_ci		return (r->set_user)(vcpu, r, reg, uaddr);
27098c2ecf20Sopenharmony_ci
27108c2ecf20Sopenharmony_ci	return reg_from_user(&__vcpu_sys_reg(vcpu, r->reg), uaddr, reg->id);
27118c2ecf20Sopenharmony_ci}
27128c2ecf20Sopenharmony_ci
27138c2ecf20Sopenharmony_cistatic unsigned int num_demux_regs(void)
27148c2ecf20Sopenharmony_ci{
27158c2ecf20Sopenharmony_ci	unsigned int i, count = 0;
27168c2ecf20Sopenharmony_ci
27178c2ecf20Sopenharmony_ci	for (i = 0; i < CSSELR_MAX; i++)
27188c2ecf20Sopenharmony_ci		if (is_valid_cache(i))
27198c2ecf20Sopenharmony_ci			count++;
27208c2ecf20Sopenharmony_ci
27218c2ecf20Sopenharmony_ci	return count;
27228c2ecf20Sopenharmony_ci}
27238c2ecf20Sopenharmony_ci
27248c2ecf20Sopenharmony_cistatic int write_demux_regids(u64 __user *uindices)
27258c2ecf20Sopenharmony_ci{
27268c2ecf20Sopenharmony_ci	u64 val = KVM_REG_ARM64 | KVM_REG_SIZE_U32 | KVM_REG_ARM_DEMUX;
27278c2ecf20Sopenharmony_ci	unsigned int i;
27288c2ecf20Sopenharmony_ci
27298c2ecf20Sopenharmony_ci	val |= KVM_REG_ARM_DEMUX_ID_CCSIDR;
27308c2ecf20Sopenharmony_ci	for (i = 0; i < CSSELR_MAX; i++) {
27318c2ecf20Sopenharmony_ci		if (!is_valid_cache(i))
27328c2ecf20Sopenharmony_ci			continue;
27338c2ecf20Sopenharmony_ci		if (put_user(val | i, uindices))
27348c2ecf20Sopenharmony_ci			return -EFAULT;
27358c2ecf20Sopenharmony_ci		uindices++;
27368c2ecf20Sopenharmony_ci	}
27378c2ecf20Sopenharmony_ci	return 0;
27388c2ecf20Sopenharmony_ci}
27398c2ecf20Sopenharmony_ci
27408c2ecf20Sopenharmony_cistatic u64 sys_reg_to_index(const struct sys_reg_desc *reg)
27418c2ecf20Sopenharmony_ci{
27428c2ecf20Sopenharmony_ci	return (KVM_REG_ARM64 | KVM_REG_SIZE_U64 |
27438c2ecf20Sopenharmony_ci		KVM_REG_ARM64_SYSREG |
27448c2ecf20Sopenharmony_ci		(reg->Op0 << KVM_REG_ARM64_SYSREG_OP0_SHIFT) |
27458c2ecf20Sopenharmony_ci		(reg->Op1 << KVM_REG_ARM64_SYSREG_OP1_SHIFT) |
27468c2ecf20Sopenharmony_ci		(reg->CRn << KVM_REG_ARM64_SYSREG_CRN_SHIFT) |
27478c2ecf20Sopenharmony_ci		(reg->CRm << KVM_REG_ARM64_SYSREG_CRM_SHIFT) |
27488c2ecf20Sopenharmony_ci		(reg->Op2 << KVM_REG_ARM64_SYSREG_OP2_SHIFT));
27498c2ecf20Sopenharmony_ci}
27508c2ecf20Sopenharmony_ci
27518c2ecf20Sopenharmony_cistatic bool copy_reg_to_user(const struct sys_reg_desc *reg, u64 __user **uind)
27528c2ecf20Sopenharmony_ci{
27538c2ecf20Sopenharmony_ci	if (!*uind)
27548c2ecf20Sopenharmony_ci		return true;
27558c2ecf20Sopenharmony_ci
27568c2ecf20Sopenharmony_ci	if (put_user(sys_reg_to_index(reg), *uind))
27578c2ecf20Sopenharmony_ci		return false;
27588c2ecf20Sopenharmony_ci
27598c2ecf20Sopenharmony_ci	(*uind)++;
27608c2ecf20Sopenharmony_ci	return true;
27618c2ecf20Sopenharmony_ci}
27628c2ecf20Sopenharmony_ci
27638c2ecf20Sopenharmony_cistatic int walk_one_sys_reg(const struct kvm_vcpu *vcpu,
27648c2ecf20Sopenharmony_ci			    const struct sys_reg_desc *rd,
27658c2ecf20Sopenharmony_ci			    u64 __user **uind,
27668c2ecf20Sopenharmony_ci			    unsigned int *total)
27678c2ecf20Sopenharmony_ci{
27688c2ecf20Sopenharmony_ci	/*
27698c2ecf20Sopenharmony_ci	 * Ignore registers we trap but don't save,
27708c2ecf20Sopenharmony_ci	 * and for which no custom user accessor is provided.
27718c2ecf20Sopenharmony_ci	 */
27728c2ecf20Sopenharmony_ci	if (!(rd->reg || rd->get_user))
27738c2ecf20Sopenharmony_ci		return 0;
27748c2ecf20Sopenharmony_ci
27758c2ecf20Sopenharmony_ci	if (sysreg_hidden(vcpu, rd))
27768c2ecf20Sopenharmony_ci		return 0;
27778c2ecf20Sopenharmony_ci
27788c2ecf20Sopenharmony_ci	if (!copy_reg_to_user(rd, uind))
27798c2ecf20Sopenharmony_ci		return -EFAULT;
27808c2ecf20Sopenharmony_ci
27818c2ecf20Sopenharmony_ci	(*total)++;
27828c2ecf20Sopenharmony_ci	return 0;
27838c2ecf20Sopenharmony_ci}
27848c2ecf20Sopenharmony_ci
27858c2ecf20Sopenharmony_ci/* Assumed ordered tables, see kvm_sys_reg_table_init. */
27868c2ecf20Sopenharmony_cistatic int walk_sys_regs(struct kvm_vcpu *vcpu, u64 __user *uind)
27878c2ecf20Sopenharmony_ci{
27888c2ecf20Sopenharmony_ci	const struct sys_reg_desc *i2, *end2;
27898c2ecf20Sopenharmony_ci	unsigned int total = 0;
27908c2ecf20Sopenharmony_ci	int err;
27918c2ecf20Sopenharmony_ci
27928c2ecf20Sopenharmony_ci	i2 = sys_reg_descs;
27938c2ecf20Sopenharmony_ci	end2 = sys_reg_descs + ARRAY_SIZE(sys_reg_descs);
27948c2ecf20Sopenharmony_ci
27958c2ecf20Sopenharmony_ci	while (i2 != end2) {
27968c2ecf20Sopenharmony_ci		err = walk_one_sys_reg(vcpu, i2++, &uind, &total);
27978c2ecf20Sopenharmony_ci		if (err)
27988c2ecf20Sopenharmony_ci			return err;
27998c2ecf20Sopenharmony_ci	}
28008c2ecf20Sopenharmony_ci	return total;
28018c2ecf20Sopenharmony_ci}
28028c2ecf20Sopenharmony_ci
28038c2ecf20Sopenharmony_ciunsigned long kvm_arm_num_sys_reg_descs(struct kvm_vcpu *vcpu)
28048c2ecf20Sopenharmony_ci{
28058c2ecf20Sopenharmony_ci	return ARRAY_SIZE(invariant_sys_regs)
28068c2ecf20Sopenharmony_ci		+ num_demux_regs()
28078c2ecf20Sopenharmony_ci		+ walk_sys_regs(vcpu, (u64 __user *)NULL);
28088c2ecf20Sopenharmony_ci}
28098c2ecf20Sopenharmony_ci
28108c2ecf20Sopenharmony_ciint kvm_arm_copy_sys_reg_indices(struct kvm_vcpu *vcpu, u64 __user *uindices)
28118c2ecf20Sopenharmony_ci{
28128c2ecf20Sopenharmony_ci	unsigned int i;
28138c2ecf20Sopenharmony_ci	int err;
28148c2ecf20Sopenharmony_ci
28158c2ecf20Sopenharmony_ci	/* Then give them all the invariant registers' indices. */
28168c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(invariant_sys_regs); i++) {
28178c2ecf20Sopenharmony_ci		if (put_user(sys_reg_to_index(&invariant_sys_regs[i]), uindices))
28188c2ecf20Sopenharmony_ci			return -EFAULT;
28198c2ecf20Sopenharmony_ci		uindices++;
28208c2ecf20Sopenharmony_ci	}
28218c2ecf20Sopenharmony_ci
28228c2ecf20Sopenharmony_ci	err = walk_sys_regs(vcpu, uindices);
28238c2ecf20Sopenharmony_ci	if (err < 0)
28248c2ecf20Sopenharmony_ci		return err;
28258c2ecf20Sopenharmony_ci	uindices += err;
28268c2ecf20Sopenharmony_ci
28278c2ecf20Sopenharmony_ci	return write_demux_regids(uindices);
28288c2ecf20Sopenharmony_ci}
28298c2ecf20Sopenharmony_ci
28308c2ecf20Sopenharmony_civoid kvm_sys_reg_table_init(void)
28318c2ecf20Sopenharmony_ci{
28328c2ecf20Sopenharmony_ci	unsigned int i;
28338c2ecf20Sopenharmony_ci	struct sys_reg_desc clidr;
28348c2ecf20Sopenharmony_ci
28358c2ecf20Sopenharmony_ci	/* Make sure tables are unique and in order. */
28368c2ecf20Sopenharmony_ci	BUG_ON(check_sysreg_table(sys_reg_descs, ARRAY_SIZE(sys_reg_descs), false));
28378c2ecf20Sopenharmony_ci	BUG_ON(check_sysreg_table(cp14_regs, ARRAY_SIZE(cp14_regs), true));
28388c2ecf20Sopenharmony_ci	BUG_ON(check_sysreg_table(cp14_64_regs, ARRAY_SIZE(cp14_64_regs), true));
28398c2ecf20Sopenharmony_ci	BUG_ON(check_sysreg_table(cp15_regs, ARRAY_SIZE(cp15_regs), true));
28408c2ecf20Sopenharmony_ci	BUG_ON(check_sysreg_table(cp15_64_regs, ARRAY_SIZE(cp15_64_regs), true));
28418c2ecf20Sopenharmony_ci	BUG_ON(check_sysreg_table(invariant_sys_regs, ARRAY_SIZE(invariant_sys_regs), false));
28428c2ecf20Sopenharmony_ci
28438c2ecf20Sopenharmony_ci	/* We abuse the reset function to overwrite the table itself. */
28448c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(invariant_sys_regs); i++)
28458c2ecf20Sopenharmony_ci		invariant_sys_regs[i].reset(NULL, &invariant_sys_regs[i]);
28468c2ecf20Sopenharmony_ci
28478c2ecf20Sopenharmony_ci	/*
28488c2ecf20Sopenharmony_ci	 * CLIDR format is awkward, so clean it up.  See ARM B4.1.20:
28498c2ecf20Sopenharmony_ci	 *
28508c2ecf20Sopenharmony_ci	 *   If software reads the Cache Type fields from Ctype1
28518c2ecf20Sopenharmony_ci	 *   upwards, once it has seen a value of 0b000, no caches
28528c2ecf20Sopenharmony_ci	 *   exist at further-out levels of the hierarchy. So, for
28538c2ecf20Sopenharmony_ci	 *   example, if Ctype3 is the first Cache Type field with a
28548c2ecf20Sopenharmony_ci	 *   value of 0b000, the values of Ctype4 to Ctype7 must be
28558c2ecf20Sopenharmony_ci	 *   ignored.
28568c2ecf20Sopenharmony_ci	 */
28578c2ecf20Sopenharmony_ci	get_clidr_el1(NULL, &clidr); /* Ugly... */
28588c2ecf20Sopenharmony_ci	cache_levels = clidr.val;
28598c2ecf20Sopenharmony_ci	for (i = 0; i < 7; i++)
28608c2ecf20Sopenharmony_ci		if (((cache_levels >> (i*3)) & 7) == 0)
28618c2ecf20Sopenharmony_ci			break;
28628c2ecf20Sopenharmony_ci	/* Clear all higher bits. */
28638c2ecf20Sopenharmony_ci	cache_levels &= (1 << (i*3))-1;
28648c2ecf20Sopenharmony_ci}
2865