coproc.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * Copyright (C) 2012 - Virtual Open Systems and Columbia University
  3. * Authors: Christoffer Dall <c.dall@virtualopensystems.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License, version 2, as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. */
  18. #ifndef __ARM_KVM_COPROC_LOCAL_H__
  19. #define __ARM_KVM_COPROC_LOCAL_H__
  20. struct coproc_params {
  21. unsigned long CRn;
  22. unsigned long CRm;
  23. unsigned long Op1;
  24. unsigned long Op2;
  25. unsigned long Rt1;
  26. unsigned long Rt2;
  27. bool is_64bit;
  28. bool is_write;
  29. };
  30. struct coproc_reg {
  31. /* MRC/MCR/MRRC/MCRR instruction which accesses it. */
  32. unsigned long CRn;
  33. unsigned long CRm;
  34. unsigned long Op1;
  35. unsigned long Op2;
  36. bool is_64bit;
  37. /* Trapped access from guest, if non-NULL. */
  38. bool (*access)(struct kvm_vcpu *,
  39. const struct coproc_params *,
  40. const struct coproc_reg *);
  41. /* Initialization for vcpu. */
  42. void (*reset)(struct kvm_vcpu *, const struct coproc_reg *);
  43. /* Index into vcpu_cp15(vcpu, ...), or 0 if we don't need to save it. */
  44. unsigned long reg;
  45. /* Value (usually reset value) */
  46. u64 val;
  47. };
  48. static inline void print_cp_instr(const struct coproc_params *p)
  49. {
  50. /* Look, we even formatted it for you to paste into the table! */
  51. if (p->is_64bit) {
  52. kvm_pr_unimpl(" { CRm64(%2lu), Op1(%2lu), is64, func_%s },\n",
  53. p->CRn, p->Op1, p->is_write ? "write" : "read");
  54. } else {
  55. kvm_pr_unimpl(" { CRn(%2lu), CRm(%2lu), Op1(%2lu), Op2(%2lu), is32,"
  56. " func_%s },\n",
  57. p->CRn, p->CRm, p->Op1, p->Op2,
  58. p->is_write ? "write" : "read");
  59. }
  60. }
  61. static inline bool ignore_write(struct kvm_vcpu *vcpu,
  62. const struct coproc_params *p)
  63. {
  64. return true;
  65. }
  66. static inline bool read_zero(struct kvm_vcpu *vcpu,
  67. const struct coproc_params *p)
  68. {
  69. *vcpu_reg(vcpu, p->Rt1) = 0;
  70. return true;
  71. }
  72. static inline bool write_to_read_only(struct kvm_vcpu *vcpu,
  73. const struct coproc_params *params)
  74. {
  75. kvm_debug("CP15 write to read-only register at: %08lx\n",
  76. *vcpu_pc(vcpu));
  77. print_cp_instr(params);
  78. return false;
  79. }
  80. static inline bool read_from_write_only(struct kvm_vcpu *vcpu,
  81. const struct coproc_params *params)
  82. {
  83. kvm_debug("CP15 read to write-only register at: %08lx\n",
  84. *vcpu_pc(vcpu));
  85. print_cp_instr(params);
  86. return false;
  87. }
  88. /* Reset functions */
  89. static inline void reset_unknown(struct kvm_vcpu *vcpu,
  90. const struct coproc_reg *r)
  91. {
  92. BUG_ON(!r->reg);
  93. BUG_ON(r->reg >= ARRAY_SIZE(vcpu->arch.ctxt.cp15));
  94. vcpu_cp15(vcpu, r->reg) = 0xdecafbad;
  95. }
  96. static inline void reset_val(struct kvm_vcpu *vcpu, const struct coproc_reg *r)
  97. {
  98. BUG_ON(!r->reg);
  99. BUG_ON(r->reg >= ARRAY_SIZE(vcpu->arch.ctxt.cp15));
  100. vcpu_cp15(vcpu, r->reg) = r->val;
  101. }
  102. static inline void reset_unknown64(struct kvm_vcpu *vcpu,
  103. const struct coproc_reg *r)
  104. {
  105. BUG_ON(!r->reg);
  106. BUG_ON(r->reg + 1 >= ARRAY_SIZE(vcpu->arch.ctxt.cp15));
  107. vcpu_cp15(vcpu, r->reg) = 0xdecafbad;
  108. vcpu_cp15(vcpu, r->reg+1) = 0xd0c0ffee;
  109. }
  110. static inline int cmp_reg(const struct coproc_reg *i1,
  111. const struct coproc_reg *i2)
  112. {
  113. BUG_ON(i1 == i2);
  114. if (!i1)
  115. return 1;
  116. else if (!i2)
  117. return -1;
  118. if (i1->CRn != i2->CRn)
  119. return i1->CRn - i2->CRn;
  120. if (i1->CRm != i2->CRm)
  121. return i1->CRm - i2->CRm;
  122. if (i1->Op1 != i2->Op1)
  123. return i1->Op1 - i2->Op1;
  124. if (i1->Op2 != i2->Op2)
  125. return i1->Op2 - i2->Op2;
  126. return i2->is_64bit - i1->is_64bit;
  127. }
  128. #define CRn(_x) .CRn = _x
  129. #define CRm(_x) .CRm = _x
  130. #define CRm64(_x) .CRn = _x, .CRm = 0
  131. #define Op1(_x) .Op1 = _x
  132. #define Op2(_x) .Op2 = _x
  133. #define is64 .is_64bit = true
  134. #define is32 .is_64bit = false
  135. bool access_vm_reg(struct kvm_vcpu *vcpu,
  136. const struct coproc_params *p,
  137. const struct coproc_reg *r);
  138. #endif /* __ARM_KVM_COPROC_LOCAL_H__ */