e500_tlb.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. * Copyright (C) 2008-2011 Freescale Semiconductor, Inc. All rights reserved.
  3. *
  4. * Author: Yu Liu, yu.liu@freescale.com
  5. *
  6. * Description:
  7. * This file is based on arch/powerpc/kvm/44x_tlb.h,
  8. * by Hollis Blanchard <hollisb@us.ibm.com>.
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License, version 2, as
  12. * published by the Free Software Foundation.
  13. */
  14. #ifndef __KVM_E500_TLB_H__
  15. #define __KVM_E500_TLB_H__
  16. #include <linux/kvm_host.h>
  17. #include <asm/mmu-book3e.h>
  18. #include <asm/tlb.h>
  19. #include <asm/kvm_e500.h>
  20. /* This geometry is the legacy default -- can be overridden by userspace */
  21. #define KVM_E500_TLB0_WAY_SIZE 128
  22. #define KVM_E500_TLB0_WAY_NUM 2
  23. #define KVM_E500_TLB0_SIZE (KVM_E500_TLB0_WAY_SIZE * KVM_E500_TLB0_WAY_NUM)
  24. #define KVM_E500_TLB1_SIZE 16
  25. #define index_of(tlbsel, esel) (((tlbsel) << 16) | ((esel) & 0xFFFF))
  26. #define tlbsel_of(index) ((index) >> 16)
  27. #define esel_of(index) ((index) & 0xFFFF)
  28. #define E500_TLB_USER_PERM_MASK (MAS3_UX|MAS3_UR|MAS3_UW)
  29. #define E500_TLB_SUPER_PERM_MASK (MAS3_SX|MAS3_SR|MAS3_SW)
  30. #define MAS2_ATTRIB_MASK \
  31. (MAS2_X0 | MAS2_X1)
  32. #define MAS3_ATTRIB_MASK \
  33. (MAS3_U0 | MAS3_U1 | MAS3_U2 | MAS3_U3 \
  34. | E500_TLB_USER_PERM_MASK | E500_TLB_SUPER_PERM_MASK)
  35. extern void kvmppc_dump_tlbs(struct kvm_vcpu *);
  36. extern int kvmppc_e500_emul_mt_mmucsr0(struct kvmppc_vcpu_e500 *, ulong);
  37. extern int kvmppc_e500_emul_tlbwe(struct kvm_vcpu *);
  38. extern int kvmppc_e500_emul_tlbre(struct kvm_vcpu *);
  39. extern int kvmppc_e500_emul_tlbivax(struct kvm_vcpu *, int, int);
  40. extern int kvmppc_e500_emul_tlbsx(struct kvm_vcpu *, int);
  41. extern int kvmppc_e500_tlb_search(struct kvm_vcpu *, gva_t, unsigned int, int);
  42. extern void kvmppc_e500_tlb_put(struct kvm_vcpu *);
  43. extern void kvmppc_e500_tlb_load(struct kvm_vcpu *, int);
  44. extern int kvmppc_e500_tlb_init(struct kvmppc_vcpu_e500 *);
  45. extern void kvmppc_e500_tlb_uninit(struct kvmppc_vcpu_e500 *);
  46. extern void kvmppc_e500_tlb_setup(struct kvmppc_vcpu_e500 *);
  47. extern void kvmppc_e500_recalc_shadow_pid(struct kvmppc_vcpu_e500 *);
  48. /* TLB helper functions */
  49. static inline unsigned int
  50. get_tlb_size(const struct kvm_book3e_206_tlb_entry *tlbe)
  51. {
  52. return (tlbe->mas1 >> 7) & 0x1f;
  53. }
  54. static inline gva_t get_tlb_eaddr(const struct kvm_book3e_206_tlb_entry *tlbe)
  55. {
  56. return tlbe->mas2 & 0xfffff000;
  57. }
  58. static inline u64 get_tlb_bytes(const struct kvm_book3e_206_tlb_entry *tlbe)
  59. {
  60. unsigned int pgsize = get_tlb_size(tlbe);
  61. return 1ULL << 10 << pgsize;
  62. }
  63. static inline gva_t get_tlb_end(const struct kvm_book3e_206_tlb_entry *tlbe)
  64. {
  65. u64 bytes = get_tlb_bytes(tlbe);
  66. return get_tlb_eaddr(tlbe) + bytes - 1;
  67. }
  68. static inline u64 get_tlb_raddr(const struct kvm_book3e_206_tlb_entry *tlbe)
  69. {
  70. return tlbe->mas7_3 & ~0xfffULL;
  71. }
  72. static inline unsigned int
  73. get_tlb_tid(const struct kvm_book3e_206_tlb_entry *tlbe)
  74. {
  75. return (tlbe->mas1 >> 16) & 0xff;
  76. }
  77. static inline unsigned int
  78. get_tlb_ts(const struct kvm_book3e_206_tlb_entry *tlbe)
  79. {
  80. return (tlbe->mas1 >> 12) & 0x1;
  81. }
  82. static inline unsigned int
  83. get_tlb_v(const struct kvm_book3e_206_tlb_entry *tlbe)
  84. {
  85. return (tlbe->mas1 >> 31) & 0x1;
  86. }
  87. static inline unsigned int
  88. get_tlb_iprot(const struct kvm_book3e_206_tlb_entry *tlbe)
  89. {
  90. return (tlbe->mas1 >> 30) & 0x1;
  91. }
  92. static inline unsigned int get_cur_pid(struct kvm_vcpu *vcpu)
  93. {
  94. return vcpu->arch.pid & 0xff;
  95. }
  96. static inline unsigned int get_cur_as(struct kvm_vcpu *vcpu)
  97. {
  98. return !!(vcpu->arch.shared->msr & (MSR_IS | MSR_DS));
  99. }
  100. static inline unsigned int get_cur_pr(struct kvm_vcpu *vcpu)
  101. {
  102. return !!(vcpu->arch.shared->msr & MSR_PR);
  103. }
  104. static inline unsigned int get_cur_spid(const struct kvm_vcpu *vcpu)
  105. {
  106. return (vcpu->arch.shared->mas6 >> 16) & 0xff;
  107. }
  108. static inline unsigned int get_cur_sas(const struct kvm_vcpu *vcpu)
  109. {
  110. return vcpu->arch.shared->mas6 & 0x1;
  111. }
  112. static inline unsigned int get_tlb_tlbsel(const struct kvm_vcpu *vcpu)
  113. {
  114. /*
  115. * Manual says that tlbsel has 2 bits wide.
  116. * Since we only have two TLBs, only lower bit is used.
  117. */
  118. return (vcpu->arch.shared->mas0 >> 28) & 0x1;
  119. }
  120. static inline unsigned int get_tlb_nv_bit(const struct kvm_vcpu *vcpu)
  121. {
  122. return vcpu->arch.shared->mas0 & 0xfff;
  123. }
  124. static inline unsigned int get_tlb_esel_bit(const struct kvm_vcpu *vcpu)
  125. {
  126. return (vcpu->arch.shared->mas0 >> 16) & 0xfff;
  127. }
  128. static inline int tlbe_is_host_safe(const struct kvm_vcpu *vcpu,
  129. const struct kvm_book3e_206_tlb_entry *tlbe)
  130. {
  131. gpa_t gpa;
  132. if (!get_tlb_v(tlbe))
  133. return 0;
  134. /* Does it match current guest AS? */
  135. /* XXX what about IS != DS? */
  136. if (get_tlb_ts(tlbe) != !!(vcpu->arch.shared->msr & MSR_IS))
  137. return 0;
  138. gpa = get_tlb_raddr(tlbe);
  139. if (!gfn_to_memslot(vcpu->kvm, gpa >> PAGE_SHIFT))
  140. /* Mapping is not for RAM. */
  141. return 0;
  142. return 1;
  143. }
  144. #endif /* __KVM_E500_TLB_H__ */