2019-06-03 13:44:50 +08:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0-only */
|
2012-12-11 00:15:34 +08:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2012,2013 - ARM Ltd
|
|
|
|
* Author: Marc Zyngier <marc.zyngier@arm.com>
|
|
|
|
*
|
|
|
|
* Derived from arch/arm/kvm/coproc.h
|
|
|
|
* Copyright (C) 2012 - Virtual Open Systems and Columbia University
|
|
|
|
* Authors: Christoffer Dall <c.dall@virtualopensystems.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __ARM64_KVM_SYS_REGS_LOCAL_H__
|
|
|
|
#define __ARM64_KVM_SYS_REGS_LOCAL_H__
|
|
|
|
|
2021-08-17 16:11:24 +08:00
|
|
|
#include <linux/bsearch.h>
|
|
|
|
|
|
|
|
#define reg_to_encoding(x) \
|
|
|
|
sys_reg((u32)(x)->Op0, (u32)(x)->Op1, \
|
|
|
|
(u32)(x)->CRn, (u32)(x)->CRm, (u32)(x)->Op2)
|
|
|
|
|
2012-12-11 00:15:34 +08:00
|
|
|
struct sys_reg_params {
|
|
|
|
u8 Op0;
|
|
|
|
u8 Op1;
|
|
|
|
u8 CRn;
|
|
|
|
u8 CRm;
|
|
|
|
u8 Op2;
|
2015-12-04 20:03:13 +08:00
|
|
|
u64 regval;
|
2012-12-11 00:15:34 +08:00
|
|
|
bool is_write;
|
|
|
|
};
|
|
|
|
|
2023-06-10 03:00:49 +08:00
|
|
|
#define encoding_to_params(reg) \
|
|
|
|
((struct sys_reg_params){ .Op0 = sys_reg_Op0(reg), \
|
|
|
|
.Op1 = sys_reg_Op1(reg), \
|
|
|
|
.CRn = sys_reg_CRn(reg), \
|
|
|
|
.CRm = sys_reg_CRm(reg), \
|
|
|
|
.Op2 = sys_reg_Op2(reg) })
|
|
|
|
|
2021-08-17 16:11:24 +08:00
|
|
|
#define esr_sys64_to_params(esr) \
|
|
|
|
((struct sys_reg_params){ .Op0 = ((esr) >> 20) & 3, \
|
|
|
|
.Op1 = ((esr) >> 14) & 0x7, \
|
|
|
|
.CRn = ((esr) >> 10) & 0xf, \
|
|
|
|
.CRm = ((esr) >> 1) & 0xf, \
|
|
|
|
.Op2 = ((esr) >> 17) & 0x7, \
|
|
|
|
.is_write = !((esr) & 1) })
|
|
|
|
|
2022-05-03 14:02:01 +08:00
|
|
|
#define esr_cp1x_32_to_params(esr) \
|
|
|
|
((struct sys_reg_params){ .Op1 = ((esr) >> 14) & 0x7, \
|
|
|
|
.CRn = ((esr) >> 10) & 0xf, \
|
|
|
|
.CRm = ((esr) >> 1) & 0xf, \
|
|
|
|
.Op2 = ((esr) >> 17) & 0x7, \
|
|
|
|
.is_write = !((esr) & 1) })
|
|
|
|
|
2012-12-11 00:15:34 +08:00
|
|
|
struct sys_reg_desc {
|
2018-12-04 18:44:22 +08:00
|
|
|
/* Sysreg string for debug */
|
|
|
|
const char *name;
|
|
|
|
|
2020-10-30 01:09:12 +08:00
|
|
|
enum {
|
2022-05-03 14:02:04 +08:00
|
|
|
AA32_DIRECT,
|
2020-10-30 01:09:12 +08:00
|
|
|
AA32_LO,
|
|
|
|
AA32_HI,
|
|
|
|
} aarch32_map;
|
|
|
|
|
2012-12-11 00:15:34 +08:00
|
|
|
/* MRS/MSR instruction which accesses it. */
|
|
|
|
u8 Op0;
|
|
|
|
u8 Op1;
|
|
|
|
u8 CRn;
|
|
|
|
u8 CRm;
|
|
|
|
u8 Op2;
|
|
|
|
|
|
|
|
/* Trapped access from guest, if non-NULL. */
|
|
|
|
bool (*access)(struct kvm_vcpu *,
|
2015-12-04 20:03:12 +08:00
|
|
|
struct sys_reg_params *,
|
2012-12-11 00:15:34 +08:00
|
|
|
const struct sys_reg_desc *);
|
|
|
|
|
2023-06-10 03:00:48 +08:00
|
|
|
/*
|
|
|
|
* Initialization for vcpu. Return initialized value, or KVM
|
|
|
|
* sanitized value for ID registers.
|
|
|
|
*/
|
|
|
|
u64 (*reset)(struct kvm_vcpu *, const struct sys_reg_desc *);
|
2012-12-11 00:15:34 +08:00
|
|
|
|
|
|
|
/* Index into sys_reg[], or 0 if we don't need to save it. */
|
|
|
|
int reg;
|
|
|
|
|
2023-06-10 03:00:48 +08:00
|
|
|
/* Value (usually reset value), or write mask for idregs */
|
2012-12-11 00:15:34 +08:00
|
|
|
u64 val;
|
2015-07-08 00:30:00 +08:00
|
|
|
|
|
|
|
/* Custom get/set_user functions, fallback to generic if NULL */
|
|
|
|
int (*get_user)(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd,
|
2022-07-05 00:27:00 +08:00
|
|
|
u64 *val);
|
2015-07-08 00:30:00 +08:00
|
|
|
int (*set_user)(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd,
|
2022-07-05 00:27:00 +08:00
|
|
|
u64 val);
|
2018-09-28 21:39:15 +08:00
|
|
|
|
|
|
|
/* Return mask of REG_* runtime visibility overrides */
|
|
|
|
unsigned int (*visibility)(const struct kvm_vcpu *vcpu,
|
|
|
|
const struct sys_reg_desc *rd);
|
2012-12-11 00:15:34 +08:00
|
|
|
};
|
|
|
|
|
2020-11-05 17:10:20 +08:00
|
|
|
#define REG_HIDDEN (1 << 0) /* hidden from userspace and guest */
|
2023-02-10 01:58:17 +08:00
|
|
|
#define REG_HIDDEN_USER (1 << 1) /* hidden from userspace only */
|
|
|
|
#define REG_RAZ (1 << 2) /* RAZ from userspace and guest */
|
|
|
|
#define REG_USER_WI (1 << 3) /* WI from userspace only */
|
2018-09-28 21:39:15 +08:00
|
|
|
|
2019-12-06 02:06:51 +08:00
|
|
|
static __printf(2, 3)
|
|
|
|
inline void print_sys_reg_msg(const struct sys_reg_params *p,
|
|
|
|
char *fmt, ...)
|
2012-12-11 00:15:34 +08:00
|
|
|
{
|
2019-12-06 02:06:51 +08:00
|
|
|
va_list va;
|
|
|
|
|
|
|
|
va_start(va, fmt);
|
2012-12-11 00:15:34 +08:00
|
|
|
/* Look, we even formatted it for you to paste into the table! */
|
2019-12-06 02:06:51 +08:00
|
|
|
kvm_pr_unimpl("%pV { Op0(%2u), Op1(%2u), CRn(%2u), CRm(%2u), Op2(%2u), func_%s },\n",
|
|
|
|
&(struct va_format){ fmt, &va },
|
2012-12-11 00:15:34 +08:00
|
|
|
p->Op0, p->Op1, p->CRn, p->CRm, p->Op2, p->is_write ? "write" : "read");
|
2019-12-06 02:06:51 +08:00
|
|
|
va_end(va);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void print_sys_reg_instr(const struct sys_reg_params *p)
|
|
|
|
{
|
|
|
|
/* GCC warns on an empty format string */
|
|
|
|
print_sys_reg_msg(p, "%s", "");
|
2012-12-11 00:15:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool ignore_write(struct kvm_vcpu *vcpu,
|
|
|
|
const struct sys_reg_params *p)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool read_zero(struct kvm_vcpu *vcpu,
|
2015-12-04 20:03:12 +08:00
|
|
|
struct sys_reg_params *p)
|
2012-12-11 00:15:34 +08:00
|
|
|
{
|
2015-12-04 20:03:13 +08:00
|
|
|
p->regval = 0;
|
2012-12-11 00:15:34 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Reset functions */
|
2023-06-10 03:00:48 +08:00
|
|
|
static inline u64 reset_unknown(struct kvm_vcpu *vcpu,
|
2012-12-11 00:15:34 +08:00
|
|
|
const struct sys_reg_desc *r)
|
|
|
|
{
|
|
|
|
BUG_ON(!r->reg);
|
|
|
|
BUG_ON(r->reg >= NR_SYS_REGS);
|
2016-03-16 22:38:53 +08:00
|
|
|
__vcpu_sys_reg(vcpu, r->reg) = 0x1de7ec7edbadc0deULL;
|
2023-06-10 03:00:48 +08:00
|
|
|
return __vcpu_sys_reg(vcpu, r->reg);
|
2012-12-11 00:15:34 +08:00
|
|
|
}
|
|
|
|
|
2023-06-10 03:00:48 +08:00
|
|
|
static inline u64 reset_val(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r)
|
2012-12-11 00:15:34 +08:00
|
|
|
{
|
|
|
|
BUG_ON(!r->reg);
|
|
|
|
BUG_ON(r->reg >= NR_SYS_REGS);
|
2016-03-16 22:38:53 +08:00
|
|
|
__vcpu_sys_reg(vcpu, r->reg) = r->val;
|
2023-06-10 03:00:48 +08:00
|
|
|
return __vcpu_sys_reg(vcpu, r->reg);
|
2012-12-11 00:15:34 +08:00
|
|
|
}
|
|
|
|
|
2022-09-13 17:44:37 +08:00
|
|
|
static inline unsigned int sysreg_visibility(const struct kvm_vcpu *vcpu,
|
|
|
|
const struct sys_reg_desc *r)
|
2018-09-28 21:39:15 +08:00
|
|
|
{
|
|
|
|
if (likely(!r->visibility))
|
2022-09-13 17:44:37 +08:00
|
|
|
return 0;
|
2018-09-28 21:39:15 +08:00
|
|
|
|
2022-09-13 17:44:37 +08:00
|
|
|
return r->visibility(vcpu, r);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool sysreg_hidden(const struct kvm_vcpu *vcpu,
|
|
|
|
const struct sys_reg_desc *r)
|
|
|
|
{
|
|
|
|
return sysreg_visibility(vcpu, r) & REG_HIDDEN;
|
2018-09-28 21:39:15 +08:00
|
|
|
}
|
|
|
|
|
2023-02-10 01:58:17 +08:00
|
|
|
static inline bool sysreg_hidden_user(const struct kvm_vcpu *vcpu,
|
|
|
|
const struct sys_reg_desc *r)
|
|
|
|
{
|
|
|
|
if (likely(!r->visibility))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return r->visibility(vcpu, r) & (REG_HIDDEN | REG_HIDDEN_USER);
|
|
|
|
}
|
|
|
|
|
2020-11-05 17:10:21 +08:00
|
|
|
static inline bool sysreg_visible_as_raz(const struct kvm_vcpu *vcpu,
|
|
|
|
const struct sys_reg_desc *r)
|
|
|
|
{
|
2022-09-13 17:44:37 +08:00
|
|
|
return sysreg_visibility(vcpu, r) & REG_RAZ;
|
2020-11-05 17:10:21 +08:00
|
|
|
}
|
|
|
|
|
2022-09-13 17:44:38 +08:00
|
|
|
static inline bool sysreg_user_write_ignore(const struct kvm_vcpu *vcpu,
|
|
|
|
const struct sys_reg_desc *r)
|
|
|
|
{
|
|
|
|
return sysreg_visibility(vcpu, r) & REG_USER_WI;
|
|
|
|
}
|
|
|
|
|
2012-12-11 00:15:34 +08:00
|
|
|
static inline int cmp_sys_reg(const struct sys_reg_desc *i1,
|
|
|
|
const struct sys_reg_desc *i2)
|
|
|
|
{
|
|
|
|
BUG_ON(i1 == i2);
|
|
|
|
if (!i1)
|
|
|
|
return 1;
|
|
|
|
else if (!i2)
|
|
|
|
return -1;
|
|
|
|
if (i1->Op0 != i2->Op0)
|
|
|
|
return i1->Op0 - i2->Op0;
|
|
|
|
if (i1->Op1 != i2->Op1)
|
|
|
|
return i1->Op1 - i2->Op1;
|
|
|
|
if (i1->CRn != i2->CRn)
|
|
|
|
return i1->CRn - i2->CRn;
|
|
|
|
if (i1->CRm != i2->CRm)
|
|
|
|
return i1->CRm - i2->CRm;
|
|
|
|
return i1->Op2 - i2->Op2;
|
|
|
|
}
|
|
|
|
|
2021-08-17 16:11:24 +08:00
|
|
|
static inline int match_sys_reg(const void *key, const void *elt)
|
|
|
|
{
|
|
|
|
const unsigned long pval = (unsigned long)key;
|
|
|
|
const struct sys_reg_desc *r = elt;
|
|
|
|
|
|
|
|
return pval - reg_to_encoding(r);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline const struct sys_reg_desc *
|
|
|
|
find_reg(const struct sys_reg_params *params, const struct sys_reg_desc table[],
|
|
|
|
unsigned int num)
|
|
|
|
{
|
|
|
|
unsigned long pval = reg_to_encoding(params);
|
|
|
|
|
|
|
|
return __inline_bsearch((void *)pval, table, num, sizeof(table[0]), match_sys_reg);
|
|
|
|
}
|
|
|
|
|
2022-07-03 21:08:46 +08:00
|
|
|
const struct sys_reg_desc *get_reg_by_id(u64 id,
|
|
|
|
const struct sys_reg_desc table[],
|
|
|
|
unsigned int num);
|
|
|
|
|
2022-07-05 01:25:41 +08:00
|
|
|
int kvm_arm_sys_reg_get_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *);
|
|
|
|
int kvm_arm_sys_reg_set_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *);
|
2022-07-03 23:06:51 +08:00
|
|
|
int kvm_sys_reg_get_user(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg,
|
|
|
|
const struct sys_reg_desc table[], unsigned int num);
|
|
|
|
int kvm_sys_reg_set_user(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg,
|
|
|
|
const struct sys_reg_desc table[], unsigned int num);
|
|
|
|
|
2020-10-30 01:09:12 +08:00
|
|
|
#define AA32(_x) .aarch32_map = AA32_##_x
|
2012-12-11 00:15:34 +08:00
|
|
|
#define Op0(_x) .Op0 = _x
|
|
|
|
#define Op1(_x) .Op1 = _x
|
|
|
|
#define CRn(_x) .CRn = _x
|
|
|
|
#define CRm(_x) .CRm = _x
|
|
|
|
#define Op2(_x) .Op2 = _x
|
|
|
|
|
2017-01-14 01:25:11 +08:00
|
|
|
#define SYS_DESC(reg) \
|
2018-12-04 18:44:22 +08:00
|
|
|
.name = #reg, \
|
2017-01-14 01:25:11 +08:00
|
|
|
Op0(sys_reg_Op0(reg)), Op1(sys_reg_Op1(reg)), \
|
|
|
|
CRn(sys_reg_CRn(reg)), CRm(sys_reg_CRm(reg)), \
|
|
|
|
Op2(sys_reg_Op2(reg))
|
|
|
|
|
2012-12-11 00:15:34 +08:00
|
|
|
#endif /* __ARM64_KVM_SYS_REGS_LOCAL_H__ */
|