tlb-mips1.S 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. * Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
  3. * The President and Fellows of Harvard College.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the University nor the names of its contributors
  14. * may be used to endorse or promote products derived from this software
  15. * without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. */
  29. #include <kern/mips/regdefs.h>
  30. #include <mips/specialreg.h>
  31. /*
  32. * TLB handling for mips-1 (r2000/r3000)
  33. */
  34. .text
  35. .set noreorder
  36. /*
  37. * tlb_random: use the "tlbwr" instruction to write a TLB entry
  38. * into a (very pseudo-) random slot in the TLB.
  39. *
  40. * Pipeline hazard: must wait between setting entryhi/lo and
  41. * doing the tlbwr. Use two cycles; some processors may vary.
  42. */
  43. .globl tlb_random
  44. .type tlb_random,@function
  45. .ent tlb_random
  46. tlb_random:
  47. mtc0 a0, c0_entryhi /* store the passed entry into the */
  48. mtc0 a1, c0_entrylo /* tlb entry registers */
  49. nop /* wait for pipeline hazard */
  50. nop
  51. tlbwr /* do it */
  52. j ra
  53. nop
  54. .end tlb_random
  55. /*
  56. * tlb_write: use the "tlbwi" instruction to write a TLB entry
  57. * into a selected slot in the TLB.
  58. *
  59. * Pipeline hazard: must wait between setting entryhi/lo and
  60. * doing the tlbwi. Use two cycles; some processors may vary.
  61. */
  62. .text
  63. .globl tlb_write
  64. .type tlb_write,@function
  65. .ent tlb_write
  66. tlb_write:
  67. mtc0 a0, c0_entryhi /* store the passed entry into the */
  68. mtc0 a1, c0_entrylo /* tlb entry registers */
  69. sll t0, a2, CIN_INDEXSHIFT /* shift the passed index into place */
  70. mtc0 t0, c0_index /* store the shifted index into the index register */
  71. nop /* wait for pipeline hazard */
  72. nop
  73. tlbwi /* do it */
  74. j ra
  75. nop
  76. .end tlb_write
  77. /*
  78. * tlb_read: use the "tlbr" instruction to read a TLB entry
  79. * from a selected slot in the TLB.
  80. *
  81. * Pipeline hazard: must wait between setting c0_index and
  82. * doing the tlbr. Use two cycles; some processors may vary.
  83. * Similarly, three more cycles before reading c0_entryhi/lo.
  84. */
  85. .text
  86. .globl tlb_read
  87. .type tlb_read,@function
  88. .ent tlb_read
  89. tlb_read:
  90. sll t0, a2, CIN_INDEXSHIFT /* shift the passed index into place */
  91. mtc0 t0, c0_index /* store the shifted index into the index register */
  92. nop /* wait for pipeline hazard */
  93. nop
  94. tlbr /* do it */
  95. nop /* wait for pipeline hazard */
  96. nop
  97. nop
  98. mfc0 t0, c0_entryhi /* get the tlb entry out of the */
  99. mfc0 t1, c0_entrylo /* tlb entry registers */
  100. sw t0, 0(a0) /* store through the passed pointer */
  101. j ra
  102. sw t1, 0(a1) /* store (in delay slot) */
  103. .end tlb_read
  104. /*
  105. * tlb_probe: use the "tlbp" instruction to find the index in the
  106. * TLB of a TLB entry matching the relevant parts of the one supplied.
  107. *
  108. * Pipeline hazard: must wait between setting c0_entryhi/lo and
  109. * doing the tlbp. Use two cycles; some processors may vary.
  110. * Similarly, two more cycles before reading c0_index.
  111. */
  112. .text
  113. .globl tlb_probe
  114. .type tlb_probe,@function
  115. .ent tlb_probe
  116. tlb_probe:
  117. mtc0 a0, c0_entryhi /* store the passed entry into the */
  118. mtc0 a1, c0_entrylo /* tlb entry registers */
  119. nop /* wait for pipeline hazard */
  120. nop
  121. tlbp /* do it */
  122. nop /* wait for pipeline hazard */
  123. nop
  124. mfc0 t0, c0_index /* fetch the index back in t0 */
  125. /*
  126. * If the high bit (CIN_P) of c0_index is set, the probe failed.
  127. * The high bit is not set <--> c0_index (now in t0) >= 0.
  128. */
  129. bgez t0, 1f /* did probe succeed? if so, skip forward */
  130. nop /* delay slot */
  131. addi v0, z0, -1 /* set return value to -1 to indicate failure */
  132. j ra /* done */
  133. nop /* delay slot */
  134. 1:
  135. /* succeeded - get the index field from the index register value */
  136. andi t1, t0, CIN_INDEX /* mask off the field */
  137. j ra /* done */
  138. sra v0, t1, CIN_INDEXSHIFT /* shift it (in delay slot) */
  139. .end tlb_probe
  140. /*
  141. * tlb_reset
  142. *
  143. * Initialize the TLB. At processor startup, the TLB state is completely
  144. * undefined. So be sure to avoid creating any duplicates. Also make sure
  145. * that the initialization entries don't duplicate the INVALID entries
  146. * defined in tlb.h. (This way you can write the invalid entries in
  147. * without having to use tlbp to find out if they're going to cause dups.)
  148. *
  149. * This function is not defined in tlb.h because it's only called from
  150. * start.S.
  151. *
  152. * Pipeline hazards are as above.
  153. */
  154. .text
  155. .globl tlb_reset
  156. .type tlb_reset,@function
  157. .ent tlb_reset
  158. tlb_reset:
  159. li t0, 0 /* t0 <- tlb index number (shifted) */
  160. li t1, 0x81000000 /* t1 <- tlb reset vaddr */
  161. 1:
  162. mtc0 $0, c0_entrylo /* set up proposed tlb entry for reset */
  163. mtc0 t1, c0_entryhi
  164. nop /* wait for pipeline hazard */
  165. nop
  166. tlbp /* check if it already exists */
  167. nop /* wait for pipeline hazard */
  168. nop
  169. mfc0 t2, c0_index
  170. bgez t2, 1b /* if it does, loop back */
  171. addiu t1, t1, 0x1000 /* next vaddr (in delay slot) */
  172. mtc0 t0, c0_index /* doesn't exist, set index to write to */
  173. /* nop */ /* don't wait for pipeline hazard */
  174. /* nop */ /* (have enough other instructions) */
  175. addiu t0, t0, 0x100 /* next tlb index (shifted) */
  176. bne t0, 0x4000, 1b /* if it's not the last tlb index, loop */
  177. tlbwi /* write tlb entry (in delay slot) */
  178. j ra /* done */
  179. nop /* delay slot */
  180. .end tlb_reset