mmu.S 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. #ifdef CONFIG_SMP
  108. move $s7, $acr ; PGD
  109. #else
  110. move.d current_pgd, $acr ; PGD
  111. #endif
  112. ; Look up PMD in PGD
  113. lsrq 24, $r0 ; Get PMD index into PGD (bit 24-31)
  114. move.d [$acr], $acr ; PGD for the current process
  115. addi $r0.d, $acr, $acr
  116. move $s3, $r0 ; rw_mm_cause
  117. move.d [$acr], $acr ; Get PMD
  118. beq 8f
  119. ; Look up PTE in PMD
  120. lsrq PAGE_SHIFT, $r0
  121. and.w PAGE_MASK, $acr ; Remove PMD flags
  122. and.d 0x7ff, $r0 ; Get PTE index into PMD (bit 13-23)
  123. addi $r0.d, $acr, $acr
  124. move.d [$acr], $acr ; Get PTE
  125. beq 9f
  126. movem [$sp], $r2 ; Restore r0-r2 in delay slot
  127. addq 12, $sp
  128. ; Store in TLB
  129. move $acr, $s5
  130. 4: ; Return
  131. move.d [$sp+], $acr
  132. move [$sp], $srs
  133. addq 4, $sp
  134. rete
  135. rfe
  136. 5: ; last_refill_cause is invalid
  137. moveq 1, $r2
  138. addq 4, $r1 ; Point to last_refill_cause
  139. move.d $r2, [$acr] ; refill_count = 1
  140. move $s3, $r0 ; Get rw_mm_cause
  141. ba 3b ; Continue normal processing
  142. move.d $r0,[$r1] ; last_refill_cause = rw_mm_cause
  143. 6: ; rw_mm_cause == last_refill_cause
  144. move.d [$acr], $r2 ; Get refill_count
  145. cmpq 4, $r2 ; refill_count > 4 ?
  146. bhi 7f ; yes
  147. addq 1, $r2 ; refill_count++
  148. ba 3b ; Continue normal processing
  149. move.d $r2, [$acr]
  150. 7: ; refill_count > 4, error
  151. move.d $acr, $r0 ; Save pointer to refill_count
  152. clear.d [$r0] ; refill_count = 0
  153. ;; rewind the short stack
  154. movem [$sp], $r2 ; Restore r0-r2
  155. addq 12, $sp
  156. move.d [$sp+], $acr
  157. move [$sp], $srs
  158. addq 4, $sp
  159. ;; Keep it simple (slow), save all the regs.
  160. SAVE_ALL
  161. jsr __flush_tlb_all
  162. nop
  163. ba ret_from_intr ; Return
  164. nop
  165. 8: ; PMD missing, let the mm subsystem fix it up.
  166. movem [$sp], $r2 ; Restore r0-r2
  167. 9: ; PTE missing, let the mm subsystem fix it up.
  168. addq 12, $sp
  169. move.d [$sp+], $acr
  170. move [$sp], $srs
  171. addq 4, $sp
  172. SAVE_ALL
  173. move \mmu, $srs
  174. move.d $sp, $r11 ; regs
  175. clear.d $r12 ; Not a protection fault
  176. move.w PAGE_MASK, $acr
  177. move $s3, $r10 ; rw_mm_cause
  178. btstq 9, $r10 ; Check if write access
  179. smi $r13
  180. and.w PAGE_MASK, $r10 ; Get VPN (virtual address)
  181. jsr do_page_fault
  182. and.w $acr, $r10
  183. ; Return
  184. ba ret_from_intr
  185. nop
  186. .size \handler, . - \handler
  187. .endm
  188. ; This is the MMU bus fault handlers.
  189. MMU_REFILL_HANDLER i_mmu_refill, 1
  190. MMU_BUS_FAULT_HANDLER i_mmu_invalid, 1, 0, 0
  191. MMU_BUS_FAULT_HANDLER i_mmu_access, 1, 0, 0
  192. MMU_BUS_FAULT_HANDLER i_mmu_execute, 1, 0, 1
  193. MMU_REFILL_HANDLER d_mmu_refill, 2
  194. MMU_BUS_FAULT_HANDLER d_mmu_invalid, 2, 0, 0
  195. MMU_BUS_FAULT_HANDLER d_mmu_access, 2, 0, 0
  196. MMU_BUS_FAULT_HANDLER d_mmu_write, 2, 1, 0