1 ; WARNING : The refill handler has been modified, see below !!!
4 * Copyright (C) 2003 Axis Communications AB
6 * Authors: Mikael Starvik (starvik@axis.com)
8 * Code for the fault low-level handling routines.
13 #include <asm/pgtable.h>
15 ; Save all register. Must save in same order as struct pt_regs.
37 ; Bus fault handler. Extracts relevant information and calls mm subsystem
38 ; to handle the fault.
39 .macro MMU_BUS_FAULT_HANDLER handler, mmu, we, ex
41 .type \handler,"function"
44 move \mmu, $srs ; Select MMU support register bank
45 move.d $sp, $r11 ; regs
46 moveq 1, $r12 ; protection fault
47 moveq \we, $r13 ; write exception?
48 orq \ex << 1, $r13 ; execute?
49 move $s3, $r10 ; rw_mm_cause
50 and.d ~8191, $r10 ; Get faulting page start address
56 .size \handler, . - \handler
59 ; Refill handler. Three cases may occur:
60 ; 1. PMD and PTE exists in mm subsystem but not in TLB
61 ; 2. PMD exists but not PTE
62 ; 3. PMD doesn't exist
63 ; The code below handles case 1 and calls the mm subsystem for case 2 and 3.
64 ; Do not touch this code without very good reasons and extensive testing.
65 ; Note that the code is optimized to minimize stalls (makes the code harder
69 ; Modified by Mikael Asker 060725: added a workaround for strange TLB
70 ; behavior. If the same PTE is present in more than one set, the TLB
71 ; doesn't recognize it and we get stuck in a loop of refill exceptions.
72 ; The workaround detects such loops and exits them by flushing
73 ; the TLB contents. The problem and workaround were verified
74 ; in VCS by Mikael Starvik.
76 ; Each page is 8 KB. Each PMD holds 8192/4 PTEs (each PTE is 4 bytes) so each
77 ; PMD holds 16 MB of virtual memory.
78 ; Bits 0-12 : Offset within a page
79 ; Bits 13-23 : PTE offset within a PMD
80 ; Bits 24-31 : PMD offset within the PGD
82 .macro MMU_REFILL_HANDLER handler, mmu
84 1: .dword 0 ; refill_count
85 ; == 0 <=> last_refill_cause is invalid
86 2: .dword 0 ; last_refill_cause
89 .type \handler, "function"
92 ; (The pipeline stalls for one cycle; $sp used as address in the next cycle.)
95 move \mmu, $srs ; Select MMU support register bank
98 move.d 1b, $acr ; Point to refill_count
101 test.d [$acr] ; refill_count == 0 ?
102 beq 5f ; yes, last_refill_cause is invalid
105 ; last_refill_cause is valid, investigate cause
106 addq 4, $r1 ; Point to last_refill_cause
107 move $s3, $r0 ; Get rw_mm_cause
108 move.d [$r1], $r2 ; Get last_refill_cause
109 cmp.d $r0, $r2 ; rw_mm_cause == last_refill_cause ?
110 beq 6f ; yes, increment count
113 ; rw_mm_cause != last_refill_cause
114 move.d $r2, [$acr] ; refill_count = 1
115 move.d $r0, [$r1] ; last_refill_cause = rw_mm_cause
117 3: ; Probably not in a loop, continue normal processing
121 move.d current_pgd, $acr ; PGD
124 lsrq 24, $r0 ; Get PMD index into PGD (bit 24-31)
125 move.d [$acr], $acr ; PGD for the current process
126 addi $r0.d, $acr, $acr
127 move $s3, $r0 ; rw_mm_cause
128 move.d [$acr], $acr ; Get PMD
132 and.w PAGE_MASK, $acr ; Remove PMD flags
133 and.d 0x7ff, $r0 ; Get PTE index into PMD (bit 13-23)
134 addi $r0.d, $acr, $acr
135 move.d [$acr], $acr ; Get PTE
137 movem [$sp], $r2 ; Restore r0-r2 in delay slot
148 5: ; last_refill_cause is invalid
150 addq 4, $r1 ; Point to last_refill_cause
151 move.d $r2, [$acr] ; refill_count = 1
152 move $s3, $r0 ; Get rw_mm_cause
153 ba 3b ; Continue normal processing
154 move.d $r0,[$r1] ; last_refill_cause = rw_mm_cause
156 6: ; rw_mm_cause == last_refill_cause
157 move.d [$acr], $r2 ; Get refill_count
158 cmpq 4, $r2 ; refill_count > 4 ?
160 addq 1, $r2 ; refill_count++
161 ba 3b ; Continue normal processing
164 7: ; refill_count > 4, error
165 move.d $acr, $r0 ; Save pointer to refill_count
166 clear.d [$r0] ; refill_count = 0
168 ;; rewind the short stack
169 movem [$sp], $r2 ; Restore r0-r2
174 ;; Keep it simple (slow), save all the regs.
178 ba ret_from_intr ; Return
181 8: ; PMD missing, let the mm subsystem fix it up.
182 movem [$sp], $r2 ; Restore r0-r2
183 9: ; PTE missing, let the mm subsystem fix it up.
190 move.d $sp, $r11 ; regs
191 clear.d $r12 ; Not a protection fault
192 move.w PAGE_MASK, $acr
193 move $s3, $r10 ; rw_mm_cause
194 btstq 9, $r10 ; Check if write access
196 and.w PAGE_MASK, $r10 ; Get VPN (virtual address)
202 .size \handler, . - \handler
205 ; This is the MMU bus fault handlers.
207 MMU_REFILL_HANDLER i_mmu_refill, 1
208 MMU_BUS_FAULT_HANDLER i_mmu_invalid, 1, 0, 0
209 MMU_BUS_FAULT_HANDLER i_mmu_access, 1, 0, 0
210 MMU_BUS_FAULT_HANDLER i_mmu_execute, 1, 0, 1
211 MMU_REFILL_HANDLER d_mmu_refill, 2
212 MMU_BUS_FAULT_HANDLER d_mmu_invalid, 2, 0, 0
213 MMU_BUS_FAULT_HANDLER d_mmu_access, 2, 0, 0
214 MMU_BUS_FAULT_HANDLER d_mmu_write, 2, 1, 0