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 bool is_aarch32; 23 bool is_32bit; /* Only valid if is_aarch32 is true */ 24 }; 25 26 struct sys_reg_desc { 27 /* Sysreg string for debug */ 28 const char *name; 29 30 /* MRS/MSR instruction which accesses it. */ 31 u8 Op0; 32 u8 Op1; 33 u8 CRn; 34 u8 CRm; 35 u8 Op2; 36 37 /* Trapped access from guest, if non-NULL. */ 38 bool (*access)(struct kvm_vcpu *, 39 struct sys_reg_params *, 40 const struct sys_reg_desc *); 41 42 /* Initialization for vcpu. */ 43 void (*reset)(struct kvm_vcpu *, const struct sys_reg_desc *); 44 45 /* Index into sys_reg[], or 0 if we don't need to save it. */ 46 int reg; 47 48 /* Value (usually reset value) */ 49 u64 val; 50 51 /* Custom get/set_user functions, fallback to generic if NULL */ 52 int (*get_user)(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd, 53 const struct kvm_one_reg *reg, void __user *uaddr); 54 int (*set_user)(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd, 55 const struct kvm_one_reg *reg, void __user *uaddr); 56 57 /* Return mask of REG_* runtime visibility overrides */ 58 unsigned int (*visibility)(const struct kvm_vcpu *vcpu, 59 const struct sys_reg_desc *rd); 60 }; 61 62 #define REG_HIDDEN (1 << 0) /* hidden from userspace and guest */ 63 64 static __printf(2, 3) 65 inline void print_sys_reg_msg(const struct sys_reg_params *p, 66 char *fmt, ...) 67 { 68 va_list va; 69 70 va_start(va, fmt); 71 /* Look, we even formatted it for you to paste into the table! */ 72 kvm_pr_unimpl("%pV { Op0(%2u), Op1(%2u), CRn(%2u), CRm(%2u), Op2(%2u), func_%s },\n", 73 &(struct va_format){ fmt, &va }, 74 p->Op0, p->Op1, p->CRn, p->CRm, p->Op2, p->is_write ? "write" : "read"); 75 va_end(va); 76 } 77 78 static inline void print_sys_reg_instr(const struct sys_reg_params *p) 79 { 80 /* GCC warns on an empty format string */ 81 print_sys_reg_msg(p, "%s", ""); 82 } 83 84 static inline bool ignore_write(struct kvm_vcpu *vcpu, 85 const struct sys_reg_params *p) 86 { 87 return true; 88 } 89 90 static inline bool read_zero(struct kvm_vcpu *vcpu, 91 struct sys_reg_params *p) 92 { 93 p->regval = 0; 94 return true; 95 } 96 97 /* Reset functions */ 98 static inline void reset_unknown(struct kvm_vcpu *vcpu, 99 const struct sys_reg_desc *r) 100 { 101 BUG_ON(!r->reg); 102 BUG_ON(r->reg >= NR_SYS_REGS); 103 __vcpu_sys_reg(vcpu, r->reg) = 0x1de7ec7edbadc0deULL; 104 } 105 106 static inline void reset_val(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r) 107 { 108 BUG_ON(!r->reg); 109 BUG_ON(r->reg >= NR_SYS_REGS); 110 __vcpu_sys_reg(vcpu, r->reg) = r->val; 111 } 112 113 static inline bool sysreg_hidden(const struct kvm_vcpu *vcpu, 114 const struct sys_reg_desc *r) 115 { 116 if (likely(!r->visibility)) 117 return false; 118 119 return r->visibility(vcpu, r) & REG_HIDDEN; 120 } 121 122 static inline int cmp_sys_reg(const struct sys_reg_desc *i1, 123 const struct sys_reg_desc *i2) 124 { 125 BUG_ON(i1 == i2); 126 if (!i1) 127 return 1; 128 else if (!i2) 129 return -1; 130 if (i1->Op0 != i2->Op0) 131 return i1->Op0 - i2->Op0; 132 if (i1->Op1 != i2->Op1) 133 return i1->Op1 - i2->Op1; 134 if (i1->CRn != i2->CRn) 135 return i1->CRn - i2->CRn; 136 if (i1->CRm != i2->CRm) 137 return i1->CRm - i2->CRm; 138 return i1->Op2 - i2->Op2; 139 } 140 141 const struct sys_reg_desc *find_reg_by_id(u64 id, 142 struct sys_reg_params *params, 143 const struct sys_reg_desc table[], 144 unsigned int num); 145 146 #define Op0(_x) .Op0 = _x 147 #define Op1(_x) .Op1 = _x 148 #define CRn(_x) .CRn = _x 149 #define CRm(_x) .CRm = _x 150 #define Op2(_x) .Op2 = _x 151 152 #define SYS_DESC(reg) \ 153 .name = #reg, \ 154 Op0(sys_reg_Op0(reg)), Op1(sys_reg_Op1(reg)), \ 155 CRn(sys_reg_CRn(reg)), CRm(sys_reg_CRm(reg)), \ 156 Op2(sys_reg_Op2(reg)) 157 158 #endif /* __ARM64_KVM_SYS_REGS_LOCAL_H__ */ 159