mmu.S 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. ; WARNING : The refill handler has been modified, see below !!!
  2. /*
  3. * Copyright (C) 2003 Axis Communications AB
  4. *
  5. * Authors: Mikael Starvik (starvik@axis.com)
  6. *
  7. * Code for the fault low-level handling routines.
  8. *
  9. */
  10. #include <asm/page.h>
  11. #include <asm/pgtable.h>
  12. ; Save all register. Must save in same order as struct pt_regs.
  13. .macro SAVE_ALL
  14. subq 12, $sp
  15. move $erp, [$sp]
  16. subq 4, $sp
  17. move $srp, [$sp]
  18. subq 4, $sp
  19. move $ccs, [$sp]
  20. subq 4, $sp
  21. move $spc, [$sp]
  22. subq 4, $sp
  23. move $mof, [$sp]
  24. subq 4, $sp
  25. move $srs, [$sp]
  26. subq 4, $sp
  27. move.d $acr, [$sp]
  28. subq 14*4, $sp
  29. movem $r13, [$sp]
  30. subq 4, $sp
  31. move.d $r10, [$sp]
  32. .endm
  33. ; Bus fault handler. Extracts relevant information and calls mm subsystem
  34. ; to handle the fault.
  35. .macro MMU_BUS_FAULT_HANDLER handler, mmu, we, ex
  36. .globl \handler
  37. .type \handler,"function"
  38. \handler:
  39. SAVE_ALL
  40. move \mmu, $srs ; Select MMU support register bank
  41. move.d $sp, $r11 ; regs
  42. moveq 1, $r12 ; protection fault
  43. moveq \we, $r13 ; write exception?
  44. orq \ex << 1, $r13 ; execute?
  45. move $s3, $r10 ; rw_mm_cause
  46. and.d ~8191, $r10 ; Get faulting page start address
  47. jsr do_page_fault
  48. nop
  49. ba ret_from_intr
  50. nop
  51. .size \handler, . - \handler
  52. .endm
  53. ; Refill handler. Three cases may occur:
  54. ; 1. PMD and PTE exists in mm subsystem but not in TLB
  55. ; 2. PMD exists but not PTE
  56. ; 3. PMD doesn't exist
  57. ; The code below handles case 1 and calls the mm subsystem for case 2 and 3.
  58. ; Do not touch this code without very good reasons and extensive testing.
  59. ; Note that the code is optimized to minimize stalls (makes the code harder
  60. ; to read).
  61. ;
  62. ; WARNING !!!
  63. ; Modified by Mikael Asker 060725: added a workaround for strange TLB
  64. ; behavior. If the same PTE is present in more than one set, the TLB
  65. ; doesn't recognize it and we get stuck in a loop of refill exceptions.
  66. ; The workaround detects such loops and exits them by flushing
  67. ; the TLB contents. The problem and workaround were verified
  68. ; in VCS by Mikael Starvik.
  69. ;
  70. ; Each page is 8 KB. Each PMD holds 8192/4 PTEs (each PTE is 4 bytes) so each
  71. ; PMD holds 16 MB of virtual memory.
  72. ; Bits 0-12 : Offset within a page
  73. ; Bits 13-23 : PTE offset within a PMD
  74. ; Bits 24-31 : PMD offset within the PGD
  75. .macro MMU_REFILL_HANDLER handler, mmu
  76. .data
  77. 1: .dword 0 ; refill_count
  78. ; == 0 <=> last_refill_cause is invalid
  79. 2: .dword 0 ; last_refill_cause
  80. .text
  81. .globl \handler
  82. .type \handler, "function"
  83. \handler:
  84. subq 4, $sp
  85. ; (The pipeline stalls for one cycle; $sp used as address in the next cycle.)
  86. move $srs, [$sp]
  87. subq 4, $sp
  88. move \mmu, $srs ; Select MMU support register bank
  89. move.d $acr, [$sp]
  90. subq 12, $sp
  91. move.d 1b, $acr ; Point to refill_count
  92. movem $r2, [$sp]
  93. test.d [$acr] ; refill_count == 0 ?
  94. beq 5f ; yes, last_refill_cause is invalid
  95. move.d $acr, $r1
  96. ; last_refill_cause is valid, investigate cause
  97. addq 4, $r1 ; Point to last_refill_cause
  98. move $s3, $r0 ; Get rw_mm_cause
  99. move.d [$r1], $r2 ; Get last_refill_cause
  100. cmp.d $r0, $r2 ; rw_mm_cause == last_refill_cause ?
  101. beq 6f ; yes, increment count
  102. moveq 1, $r2
  103. ; rw_mm_cause != last_refill_cause
  104. move.d $r2, [$acr] ; refill_count = 1
  105. move.d $r0, [$r1] ; last_refill_cause = rw_mm_cause
  106. 3: ; Probably not in a loop, continue normal processing
  107. move.d current_pgd, $acr ; PGD
  108. ; Look up PMD in PGD
  109. lsrq 24, $r0 ; Get PMD index into PGD (bit 24-31)
  110. move.d [$acr], $acr ; PGD for the current process
  111. addi $r0.d, $acr, $acr
  112. move $s3, $r0 ; rw_mm_cause
  113. move.d [$acr], $acr ; Get PMD
  114. beq 8f
  115. ; Look up PTE in PMD
  116. lsrq PAGE_SHIFT, $r0
  117. and.w PAGE_MASK, $acr ; Remove PMD flags
  118. and.d 0x7ff, $r0 ; Get PTE index into PMD (bit 13-23)
  119. addi $r0.d, $acr, $acr
  120. move.d [$acr], $acr ; Get PTE
  121. beq 9f
  122. movem [$sp], $r2 ; Restore r0-r2 in delay slot
  123. addq 12, $sp
  124. ; Store in TLB
  125. move $acr, $s5
  126. 4: ; Return
  127. move.d [$sp+], $acr
  128. move [$sp], $srs
  129. addq 4, $sp
  130. rete
  131. rfe
  132. 5: ; last_refill_cause is invalid
  133. moveq 1, $r2
  134. addq 4, $r1 ; Point to last_refill_cause
  135. move.d $r2, [$acr] ; refill_count = 1
  136. move $s3, $r0 ; Get rw_mm_cause
  137. ba 3b ; Continue normal processing
  138. move.d $r0,[$r1] ; last_refill_cause = rw_mm_cause
  139. 6: ; rw_mm_cause == last_refill_cause
  140. move.d [$acr], $r2 ; Get refill_count
  141. cmpq 4, $r2 ; refill_count > 4 ?
  142. bhi 7f ; yes
  143. addq 1, $r2 ; refill_count++
  144. ba 3b ; Continue normal processing
  145. move.d $r2, [$acr]
  146. 7: ; refill_count > 4, error
  147. move.d $acr, $r0 ; Save pointer to refill_count
  148. clear.d [$r0] ; refill_count = 0
  149. ;; rewind the short stack
  150. movem [$sp], $r2 ; Restore r0-r2
  151. addq 12, $sp
  152. move.d [$sp+], $acr
  153. move [$sp], $srs
  154. addq 4, $sp
  155. ;; Keep it simple (slow), save all the regs.
  156. SAVE_ALL
  157. jsr __flush_tlb_all
  158. nop
  159. ba ret_from_intr ; Return
  160. nop
  161. 8: ; PMD missing, let the mm subsystem fix it up.
  162. movem [$sp], $r2 ; Restore r0-r2
  163. 9: ; PTE missing, let the mm subsystem fix it up.
  164. addq 12, $sp
  165. move.d [$sp+], $acr
  166. move [$sp], $srs
  167. addq 4, $sp
  168. SAVE_ALL
  169. move \mmu, $srs
  170. move.d $sp, $r11 ; regs
  171. clear.d $r12 ; Not a protection fault
  172. move.w PAGE_MASK, $acr
  173. move $s3, $r10 ; rw_mm_cause
  174. btstq 9, $r10 ; Check if write access
  175. smi $r13
  176. and.w PAGE_MASK, $r10 ; Get VPN (virtual address)
  177. jsr do_page_fault
  178. and.w $acr, $r10
  179. ; Return
  180. ba ret_from_intr
  181. nop
  182. .size \handler, . - \handler
  183. .endm
  184. ; This is the MMU bus fault handlers.
  185. MMU_REFILL_HANDLER i_mmu_refill, 1
  186. MMU_BUS_FAULT_HANDLER i_mmu_invalid, 1, 0, 0
  187. MMU_BUS_FAULT_HANDLER i_mmu_access, 1, 0, 0
  188. MMU_BUS_FAULT_HANDLER i_mmu_execute, 1, 0, 1
  189. MMU_REFILL_HANDLER d_mmu_refill, 2
  190. MMU_BUS_FAULT_HANDLER d_mmu_invalid, 2, 0, 0
  191. MMU_BUS_FAULT_HANDLER d_mmu_access, 2, 0, 0
  192. MMU_BUS_FAULT_HANDLER d_mmu_write, 2, 1, 0