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