1; WARNING : The refill handler has been modified, see below !!!
2
3/*
4 *  Copyright (C) 2003 Axis Communications AB
5 *
6 *  Authors:	Mikael Starvik (starvik@axis.com)
7 *
8 * Code for the fault low-level handling routines.
9 *
10 */
11
12#include <asm/page.h>
13#include <asm/pgtable.h>
14
15; Save all register. Must save in same order as struct pt_regs.
16.macro SAVE_ALL
17	subq	12, $sp
18	move	$erp, [$sp]
19	subq	4, $sp
20	move	$srp, [$sp]
21	subq	4, $sp
22	move	$ccs, [$sp]
23	subq	4, $sp
24	move	$spc, [$sp]
25	subq	4, $sp
26	move	$mof, [$sp]
27	subq	4, $sp
28	move	$srs, [$sp]
29	subq	4, $sp
30	move.d	$acr, [$sp]
31	subq	14*4, $sp
32	movem	$r13, [$sp]
33	subq	4, $sp
34	move.d	$r10, [$sp]
35.endm
36
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
40	.globl	\handler
41	.type   \handler,"function"
42\handler:
43	SAVE_ALL
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
51
52	jsr	do_page_fault
53	nop
54	ba	ret_from_intr
55	nop
56	.size   \handler, . - \handler
57.endm
58
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
66; to read).
67;
68; WARNING !!!
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.
75;
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
81
82.macro MMU_REFILL_HANDLER handler, mmu
83	.data
841:	.dword	0		; refill_count
85                                ;   == 0 <=> last_refill_cause is invalid
862:	.dword	0		; last_refill_cause
87	.text
88	.globl \handler
89	.type   \handler, "function"
90\handler:
91	subq	4, $sp
92; (The pipeline stalls for one cycle; $sp used as address in the next cycle.)
93	move	$srs, [$sp]
94	subq	4, $sp
95	move	\mmu, $srs	; Select MMU support register bank
96	move.d	$acr, [$sp]
97	subq	12, $sp
98	move.d	1b, $acr        ; Point to refill_count
99	movem	$r2, [$sp]
100
101	test.d	[$acr]	        ; refill_count == 0 ?
102	beq	5f		;   yes, last_refill_cause is invalid
103        move.d	$acr, $r1
104
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
111	moveq	1, $r2
112
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
116
1173:	; Probably not in a loop, continue normal processing
118	move.d  current_pgd, $acr ; PGD
119	; Look up PMD in PGD
120	lsrq	24, $r0	; Get PMD index into PGD (bit 24-31)
121	move.d  [$acr], $acr	; PGD for the current process
122	addi	$r0.d, $acr, $acr
123	move	$s3, $r0	; rw_mm_cause
124	move.d  [$acr], $acr	; Get PMD
125	beq	8f
126	; Look up PTE in PMD
127	lsrq	PAGE_SHIFT, $r0
128	and.w	PAGE_MASK, $acr	; Remove PMD flags
129	and.d	0x7ff, $r0	; Get PTE index into PMD (bit 13-23)
130	addi    $r0.d, $acr, $acr
131	move.d  [$acr], $acr	; Get PTE
132	beq	9f
133	movem	[$sp], $r2	; Restore r0-r2 in delay slot
134	addq	12, $sp
135	; Store in TLB
136	move	$acr, $s5
1374:	; Return
138	move.d	[$sp+], $acr
139	move	[$sp], $srs
140	addq	4, $sp
141	rete
142	rfe
143
1445:      ; last_refill_cause is invalid
145	moveq	1, $r2
146        addq    4, $r1          ; Point to last_refill_cause
147	move.d	$r2, [$acr]	; refill_count = 1
148	move	$s3, $r0	; Get rw_mm_cause
149        ba      3b		; Continue normal processing
150	move.d	$r0,[$r1]	; last_refill_cause = rw_mm_cause
151
1526:      ; rw_mm_cause == last_refill_cause
153        move.d  [$acr], $r2     ; Get refill_count
154	cmpq	4, $r2		; refill_count > 4 ?
155	bhi	7f		;   yes
156	addq	1, $r2	        ; refill_count++
157	ba	3b		; Continue normal processing
158	move.d	$r2, [$acr]
159
1607:	; refill_count > 4, error
161	move.d	$acr, $r0       ; Save pointer to refill_count
162	clear.d	[$r0]		; refill_count = 0
163
164	;; rewind the short stack
165	movem	[$sp], $r2	; Restore r0-r2
166	addq	12, $sp
167	move.d	[$sp+], $acr
168	move	[$sp], $srs
169	addq	4, $sp
170	;; Keep it simple (slow), save all the regs.
171	SAVE_ALL
172	jsr	__flush_tlb_all
173	nop
174	ba	ret_from_intr	; Return
175	nop
176
1778:	; PMD missing, let the mm subsystem fix it up.
178	movem	[$sp], $r2	; Restore r0-r2
1799:      ; PTE missing, let the mm subsystem fix it up.
180	addq	12, $sp
181	move.d	[$sp+], $acr
182	move	[$sp], $srs
183	addq	4, $sp
184	SAVE_ALL
185	move    \mmu, $srs
186	move.d	$sp, $r11	; regs
187	clear.d	$r12		; Not a protection fault
188	move.w  PAGE_MASK, $acr
189	move    $s3, $r10	; rw_mm_cause
190	btstq   9, $r10		; Check if write access
191	smi     $r13
192	and.w	PAGE_MASK, $r10	; Get VPN (virtual address)
193	jsr	do_page_fault
194	and.w   $acr, $r10
195	; Return
196	ba	ret_from_intr
197	nop
198	.size   \handler, . - \handler
199.endm
200
201	; This is the MMU bus fault handlers.
202
203MMU_REFILL_HANDLER i_mmu_refill, 1
204MMU_BUS_FAULT_HANDLER i_mmu_invalid, 1, 0, 0
205MMU_BUS_FAULT_HANDLER i_mmu_access,  1, 0, 0
206MMU_BUS_FAULT_HANDLER i_mmu_execute, 1, 0, 1
207MMU_REFILL_HANDLER d_mmu_refill,  2
208MMU_BUS_FAULT_HANDLER d_mmu_invalid, 2, 0, 0
209MMU_BUS_FAULT_HANDLER d_mmu_access,  2, 0, 0
210MMU_BUS_FAULT_HANDLER d_mmu_write,   2, 1, 0
211