sys_regs.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * Copyright (C) 2012,2013 - ARM Ltd
  3. * Author: Marc Zyngier <marc.zyngier@arm.com>
  4. *
  5. * Derived from arch/arm/kvm/coproc.h
  6. * Copyright (C) 2012 - Virtual Open Systems and Columbia University
  7. * Authors: Christoffer Dall <c.dall@virtualopensystems.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License, version 2, as
  11. * published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #ifndef __ARM64_KVM_SYS_REGS_LOCAL_H__
  22. #define __ARM64_KVM_SYS_REGS_LOCAL_H__
  23. struct sys_reg_params {
  24. u8 Op0;
  25. u8 Op1;
  26. u8 CRn;
  27. u8 CRm;
  28. u8 Op2;
  29. u8 Rt;
  30. bool is_write;
  31. };
  32. struct sys_reg_desc {
  33. /* MRS/MSR instruction which accesses it. */
  34. u8 Op0;
  35. u8 Op1;
  36. u8 CRn;
  37. u8 CRm;
  38. u8 Op2;
  39. /* Trapped access from guest, if non-NULL. */
  40. bool (*access)(struct kvm_vcpu *,
  41. const struct sys_reg_params *,
  42. const struct sys_reg_desc *);
  43. /* Initialization for vcpu. */
  44. void (*reset)(struct kvm_vcpu *, const struct sys_reg_desc *);
  45. /* Index into sys_reg[], or 0 if we don't need to save it. */
  46. int reg;
  47. /* Value (usually reset value) */
  48. u64 val;
  49. };
  50. static inline void print_sys_reg_instr(const struct sys_reg_params *p)
  51. {
  52. /* Look, we even formatted it for you to paste into the table! */
  53. kvm_pr_unimpl(" { Op0(%2u), Op1(%2u), CRn(%2u), CRm(%2u), Op2(%2u), func_%s },\n",
  54. p->Op0, p->Op1, p->CRn, p->CRm, p->Op2, p->is_write ? "write" : "read");
  55. }
  56. static inline bool ignore_write(struct kvm_vcpu *vcpu,
  57. const struct sys_reg_params *p)
  58. {
  59. return true;
  60. }
  61. static inline bool read_zero(struct kvm_vcpu *vcpu,
  62. const struct sys_reg_params *p)
  63. {
  64. *vcpu_reg(vcpu, p->Rt) = 0;
  65. return true;
  66. }
  67. static inline bool write_to_read_only(struct kvm_vcpu *vcpu,
  68. const struct sys_reg_params *params)
  69. {
  70. kvm_debug("sys_reg write to read-only register at: %lx\n",
  71. *vcpu_pc(vcpu));
  72. print_sys_reg_instr(params);
  73. return false;
  74. }
  75. static inline bool read_from_write_only(struct kvm_vcpu *vcpu,
  76. const struct sys_reg_params *params)
  77. {
  78. kvm_debug("sys_reg read to write-only register at: %lx\n",
  79. *vcpu_pc(vcpu));
  80. print_sys_reg_instr(params);
  81. return false;
  82. }
  83. /* Reset functions */
  84. static inline void reset_unknown(struct kvm_vcpu *vcpu,
  85. const struct sys_reg_desc *r)
  86. {
  87. BUG_ON(!r->reg);
  88. BUG_ON(r->reg >= NR_SYS_REGS);
  89. vcpu_sys_reg(vcpu, r->reg) = 0x1de7ec7edbadc0deULL;
  90. }
  91. static inline void reset_val(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r)
  92. {
  93. BUG_ON(!r->reg);
  94. BUG_ON(r->reg >= NR_SYS_REGS);
  95. vcpu_sys_reg(vcpu, r->reg) = r->val;
  96. }
  97. static inline int cmp_sys_reg(const struct sys_reg_desc *i1,
  98. const struct sys_reg_desc *i2)
  99. {
  100. BUG_ON(i1 == i2);
  101. if (!i1)
  102. return 1;
  103. else if (!i2)
  104. return -1;
  105. if (i1->Op0 != i2->Op0)
  106. return i1->Op0 - i2->Op0;
  107. if (i1->Op1 != i2->Op1)
  108. return i1->Op1 - i2->Op1;
  109. if (i1->CRn != i2->CRn)
  110. return i1->CRn - i2->CRn;
  111. if (i1->CRm != i2->CRm)
  112. return i1->CRm - i2->CRm;
  113. return i1->Op2 - i2->Op2;
  114. }
  115. #define Op0(_x) .Op0 = _x
  116. #define Op1(_x) .Op1 = _x
  117. #define CRn(_x) .CRn = _x
  118. #define CRm(_x) .CRm = _x
  119. #define Op2(_x) .Op2 = _x
  120. #endif /* __ARM64_KVM_SYS_REGS_LOCAL_H__ */