1 /* SPDX-License-Identifier: GPL-2.0-only */ 2 /* 3 * Copyright (C) 2012,2013 - ARM Ltd 4 * Author: Marc Zyngier <marc.zyngier@arm.com> 5 * 6 * Derived from arch/arm/kvm/coproc.h 7 * Copyright (C) 2012 - Virtual Open Systems and Columbia University 8 * Authors: Christoffer Dall <c.dall@virtualopensystems.com> 9 */ 10 11 #ifndef __ARM64_KVM_SYS_REGS_LOCAL_H__ 12 #define __ARM64_KVM_SYS_REGS_LOCAL_H__ 13 14 struct sys_reg_params { 15 u8 Op0; 16 u8 Op1; 17 u8 CRn; 18 u8 CRm; 19 u8 Op2; 20 u64 regval; 21 bool is_write; 22 }; 23 24 struct sys_reg_desc { 25 /* Sysreg string for debug */ 26 const char *name; 27 28 enum { 29 AA32_ZEROHIGH, 30 AA32_LO, 31 AA32_HI, 32 } aarch32_map; 33 34 /* MRS/MSR instruction which accesses it. */ 35 u8 Op0; 36 u8 Op1; 37 u8 CRn; 38 u8 CRm; 39 u8 Op2; 40 41 /* Trapped access from guest, if non-NULL. */ 42 bool (*access)(struct kvm_vcpu *, 43 struct sys_reg_params *, 44 const struct sys_reg_desc *); 45 46 /* Initialization for vcpu. */ 47 void (*reset)(struct kvm_vcpu *, const struct sys_reg_desc *); 48 49 /* Index into sys_reg[], or 0 if we don't need to save it. */ 50 int reg; 51 52 /* Value (usually reset value) */ 53 u64 val; 54 55 /* Custom get/set_user functions, fallback to generic if NULL */ 56 int (*get_user)(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd, 57 const struct kvm_one_reg *reg, void __user *uaddr); 58 int (*set_user)(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd, 59 const struct kvm_one_reg *reg, void __user *uaddr); 60 61 /* Return mask of REG_* runtime visibility overrides */ 62 unsigned int (*visibility)(const struct kvm_vcpu *vcpu, 63 const struct sys_reg_desc *rd); 64 }; 65 66 #define REG_HIDDEN (1 << 0) /* hidden from userspace and guest */ 67 #define REG_RAZ (1 << 1) /* RAZ from userspace and guest */ 68 69 static __printf(2, 3) 70 inline void print_sys_reg_msg(const struct sys_reg_params *p, 71 char *fmt, ...) 72 { 73 va_list va; 74 75 va_start(va, fmt); 76 /* Look, we even formatted it for you to paste into the table! */ 77 kvm_pr_unimpl("%pV { Op0(%2u), Op1(%2u), CRn(%2u), CRm(%2u), Op2(%2u), func_%s },\n", 78 &(struct va_format){ fmt, &va }, 79 p->Op0, p->Op1, p->CRn, p->CRm, p->Op2, p->is_write ? "write" : "read"); 80 va_end(va); 81 } 82 83 static inline void print_sys_reg_instr(const struct sys_reg_params *p) 84 { 85 /* GCC warns on an empty format string */ 86 print_sys_reg_msg(p, "%s", ""); 87 } 88 89 static inline bool ignore_write(struct kvm_vcpu *vcpu, 90 const struct sys_reg_params *p) 91 { 92 return true; 93 } 94 95 static inline bool read_zero(struct kvm_vcpu *vcpu, 96 struct sys_reg_params *p) 97 { 98 p->regval = 0; 99 return true; 100 } 101 102 /* Reset functions */ 103 static inline void reset_unknown(struct kvm_vcpu *vcpu, 104 const struct sys_reg_desc *r) 105 { 106 BUG_ON(!r->reg); 107 BUG_ON(r->reg >= NR_SYS_REGS); 108 __vcpu_sys_reg(vcpu, r->reg) = 0x1de7ec7edbadc0deULL; 109 } 110 111 static inline void reset_val(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r) 112 { 113 BUG_ON(!r->reg); 114 BUG_ON(r->reg >= NR_SYS_REGS); 115 __vcpu_sys_reg(vcpu, r->reg) = r->val; 116 } 117 118 static inline bool sysreg_hidden(const struct kvm_vcpu *vcpu, 119 const struct sys_reg_desc *r) 120 { 121 if (likely(!r->visibility)) 122 return false; 123 124 return r->visibility(vcpu, r) & REG_HIDDEN; 125 } 126 127 static inline bool sysreg_visible_as_raz(const struct kvm_vcpu *vcpu, 128 const struct sys_reg_desc *r) 129 { 130 if (likely(!r->visibility)) 131 return false; 132 133 return r->visibility(vcpu, r) & REG_RAZ; 134 } 135 136 static inline int cmp_sys_reg(const struct sys_reg_desc *i1, 137 const struct sys_reg_desc *i2) 138 { 139 BUG_ON(i1 == i2); 140 if (!i1) 141 return 1; 142 else if (!i2) 143 return -1; 144 if (i1->Op0 != i2->Op0) 145 return i1->Op0 - i2->Op0; 146 if (i1->Op1 != i2->Op1) 147 return i1->Op1 - i2->Op1; 148 if (i1->CRn != i2->CRn) 149 return i1->CRn - i2->CRn; 150 if (i1->CRm != i2->CRm) 151 return i1->CRm - i2->CRm; 152 return i1->Op2 - i2->Op2; 153 } 154 155 const struct sys_reg_desc *find_reg_by_id(u64 id, 156 struct sys_reg_params *params, 157 const struct sys_reg_desc table[], 158 unsigned int num); 159 160 #define AA32(_x) .aarch32_map = AA32_##_x 161 #define Op0(_x) .Op0 = _x 162 #define Op1(_x) .Op1 = _x 163 #define CRn(_x) .CRn = _x 164 #define CRm(_x) .CRm = _x 165 #define Op2(_x) .Op2 = _x 166 167 #define SYS_DESC(reg) \ 168 .name = #reg, \ 169 Op0(sys_reg_Op0(reg)), Op1(sys_reg_Op1(reg)), \ 170 CRn(sys_reg_CRn(reg)), CRm(sys_reg_CRm(reg)), \ 171 Op2(sys_reg_Op2(reg)) 172 173 #endif /* __ARM64_KVM_SYS_REGS_LOCAL_H__ */ 174