[SPARC64]: Fix _PAGE_EXEC handling.
[powerpc.git] / arch / sparc64 / kernel / tsb.S
1 /* tsb.S: Sparc64 TSB table handling.
2  *
3  * Copyright (C) 2006 David S. Miller <davem@davemloft.net>
4  */
5
6 #include <asm/tsb.h>
7 #include <asm/hypervisor.h>
8
9         .text
10         .align  32
11
12         /* Invoked from TLB miss handler, we are in the
13          * MMU global registers and they are setup like
14          * this:
15          *
16          * %g1: TSB entry pointer
17          * %g2: available temporary
18          * %g3: FAULT_CODE_{D,I}TLB
19          * %g4: available temporary
20          * %g5: available temporary
21          * %g6: TAG TARGET
22          * %g7: available temporary, will be loaded by us with
23          *      the physical address base of the linux page
24          *      tables for the current address space
25          */
26 tsb_miss_dtlb:
27         mov             TLB_TAG_ACCESS, %g4
28         ba,pt           %xcc, tsb_miss_page_table_walk
29          ldxa           [%g4] ASI_DMMU, %g4
30
31 tsb_miss_itlb:
32         mov             TLB_TAG_ACCESS, %g4
33         ba,pt           %xcc, tsb_miss_page_table_walk
34          ldxa           [%g4] ASI_IMMU, %g4
35
36         /* At this point we have:
37          * %g4 --       missing virtual address
38          * %g1 --       TSB entry address
39          * %g6 --       TAG TARGET (vaddr >> 22)
40          */
41 tsb_miss_page_table_walk:
42         TRAP_LOAD_PGD_PHYS(%g7, %g5)
43
44         /* And now we have the PGD base physical address in %g7.  */
45 tsb_miss_page_table_walk_sun4v_fastpath:
46         USER_PGTABLE_WALK_TL1(%g4, %g7, %g5, %g2, tsb_do_fault)
47
48 tsb_reload:
49         TSB_LOCK_TAG(%g1, %g2, %g7)
50
51         /* Load and check PTE.  */
52         ldxa            [%g5] ASI_PHYS_USE_EC, %g5
53         mov             1, %g7
54         sllx            %g7, TSB_TAG_INVALID_BIT, %g7
55         brgez,a,pn      %g5, tsb_do_fault
56          TSB_STORE(%g1, %g7)
57
58         /* If it is larger than the base page size, don't
59          * bother putting it into the TSB.
60          */
61         sethi           %hi(_PAGE_ALL_SZ_BITS), %g7
62         ldx             [%g7 + %lo(_PAGE_ALL_SZ_BITS)], %g7
63         and             %g5, %g7, %g2
64         sethi           %hi(_PAGE_SZBITS), %g7
65         ldx             [%g7 + %lo(_PAGE_SZBITS)], %g7
66         cmp             %g2, %g7
67         mov             1, %g7
68         sllx            %g7, TSB_TAG_INVALID_BIT, %g7
69         bne,a,pn        %xcc, tsb_tlb_reload
70          TSB_STORE(%g1, %g7)
71
72         TSB_WRITE(%g1, %g5, %g6)
73
74         /* Finally, load TLB and return from trap.  */
75 tsb_tlb_reload:
76         cmp             %g3, FAULT_CODE_DTLB
77         bne,pn          %xcc, tsb_itlb_load
78          nop
79
80 tsb_dtlb_load:
81
82 661:    stxa            %g5, [%g0] ASI_DTLB_DATA_IN
83         retry
84         .section        .sun4v_2insn_patch, "ax"
85         .word           661b
86         nop
87         nop
88         .previous
89
90         /* For sun4v the ASI_DTLB_DATA_IN store and the retry
91          * instruction get nop'd out and we get here to branch
92          * to the sun4v tlb load code.  The registers are setup
93          * as follows:
94          *
95          * %g4: vaddr
96          * %g5: PTE
97          * %g6: TAG
98          *
99          * The sun4v TLB load wants the PTE in %g3 so we fix that
100          * up here.
101          */
102         ba,pt           %xcc, sun4v_dtlb_load
103          mov            %g5, %g3
104
105 tsb_itlb_load:
106         /* Executable bit must be set.  */
107 661:    andcc           %g5, _PAGE_EXEC_4U, %g0
108         .section        .sun4v_1insn_patch, "ax"
109         .word           661b
110         andcc           %g5, _PAGE_EXEC_4V, %g0
111         .previous
112
113         be,pn           %xcc, tsb_do_fault
114          nop
115
116 661:    stxa            %g5, [%g0] ASI_ITLB_DATA_IN
117         retry
118         .section        .sun4v_2insn_patch, "ax"
119         .word           661b
120         nop
121         nop
122         .previous
123
124         /* For sun4v the ASI_ITLB_DATA_IN store and the retry
125          * instruction get nop'd out and we get here to branch
126          * to the sun4v tlb load code.  The registers are setup
127          * as follows:
128          *
129          * %g4: vaddr
130          * %g5: PTE
131          * %g6: TAG
132          *
133          * The sun4v TLB load wants the PTE in %g3 so we fix that
134          * up here.
135          */
136         ba,pt           %xcc, sun4v_itlb_load
137          mov            %g5, %g3
138
139         /* No valid entry in the page tables, do full fault
140          * processing.
141          */
142
143         .globl          tsb_do_fault
144 tsb_do_fault:
145         cmp             %g3, FAULT_CODE_DTLB
146
147 661:    rdpr            %pstate, %g5
148         wrpr            %g5, PSTATE_AG | PSTATE_MG, %pstate
149         .section        .sun4v_2insn_patch, "ax"
150         .word           661b
151         SET_GL(1)
152         ldxa            [%g0] ASI_SCRATCHPAD, %g4
153         .previous
154
155         bne,pn          %xcc, tsb_do_itlb_fault
156          nop
157
158 tsb_do_dtlb_fault:
159         rdpr    %tl, %g3
160         cmp     %g3, 1
161
162 661:    mov     TLB_TAG_ACCESS, %g4
163         ldxa    [%g4] ASI_DMMU, %g5
164         .section .sun4v_2insn_patch, "ax"
165         .word   661b
166         ldx     [%g4 + HV_FAULT_D_ADDR_OFFSET], %g5
167         nop
168         .previous
169
170         be,pt   %xcc, sparc64_realfault_common
171          mov    FAULT_CODE_DTLB, %g4
172         ba,pt   %xcc, winfix_trampoline
173          nop
174
175 tsb_do_itlb_fault:
176         rdpr    %tpc, %g5
177         ba,pt   %xcc, sparc64_realfault_common
178          mov    FAULT_CODE_ITLB, %g4
179
180         .globl  sparc64_realfault_common
181 sparc64_realfault_common:
182         /* fault code in %g4, fault address in %g5, etrap will
183          * preserve these two values in %l4 and %l5 respectively
184          */
185         ba,pt   %xcc, etrap                     ! Save trap state
186 1:       rd     %pc, %g7                        ! ...
187         stb     %l4, [%g6 + TI_FAULT_CODE]      ! Save fault code
188         stx     %l5, [%g6 + TI_FAULT_ADDR]      ! Save fault address
189         call    do_sparc64_fault                ! Call fault handler
190          add    %sp, PTREGS_OFF, %o0            ! Compute pt_regs arg
191         ba,pt   %xcc, rtrap_clr_l6              ! Restore cpu state
192          nop                                    ! Delay slot (fill me)
193
194 winfix_trampoline:
195         rdpr    %tpc, %g3                       ! Prepare winfixup TNPC
196         or      %g3, 0x7c, %g3                  ! Compute branch offset
197         wrpr    %g3, %tnpc                      ! Write it into TNPC
198         done                                    ! Trap return
199
200         /* Insert an entry into the TSB.
201          *
202          * %o0: TSB entry pointer (virt or phys address)
203          * %o1: tag
204          * %o2: pte
205          */
206         .align  32
207         .globl  __tsb_insert
208 __tsb_insert:
209         rdpr    %pstate, %o5
210         wrpr    %o5, PSTATE_IE, %pstate
211         TSB_LOCK_TAG(%o0, %g2, %g3)
212         TSB_WRITE(%o0, %o2, %o1)
213         wrpr    %o5, %pstate
214         retl
215          nop
216
217         /* Flush the given TSB entry if it has the matching
218          * tag.
219          *
220          * %o0: TSB entry pointer (virt or phys address)
221          * %o1: tag
222          */
223         .align  32
224         .globl  tsb_flush
225 tsb_flush:
226         sethi   %hi(TSB_TAG_LOCK_HIGH), %g2
227 1:      TSB_LOAD_TAG(%o0, %g1)
228         srlx    %g1, 32, %o3
229         andcc   %o3, %g2, %g0
230         bne,pn  %icc, 1b
231          membar #LoadLoad
232         cmp     %g1, %o1
233         mov     1, %o3
234         bne,pt  %xcc, 2f
235          sllx   %o3, TSB_TAG_INVALID_BIT, %o3
236         TSB_CAS_TAG(%o0, %g1, %o3)
237         cmp     %g1, %o3
238         bne,pn  %xcc, 1b
239          nop
240 2:      retl
241          TSB_MEMBAR
242
243         /* Reload MMU related context switch state at
244          * schedule() time.
245          *
246          * %o0: page table physical address
247          * %o1: TSB register value
248          * %o2: TSB virtual address
249          * %o3: TSB mapping locked PTE
250          * %o4: Hypervisor TSB descriptor physical address
251          *
252          * We have to run this whole thing with interrupts
253          * disabled so that the current cpu doesn't change
254          * due to preemption.
255          */
256         .align  32
257         .globl  __tsb_context_switch
258 __tsb_context_switch:
259         rdpr    %pstate, %o5
260         wrpr    %o5, PSTATE_IE, %pstate
261
262         ldub    [%g6 + TI_CPU], %g1
263         sethi   %hi(trap_block), %g2
264         sllx    %g1, TRAP_BLOCK_SZ_SHIFT, %g1
265         or      %g2, %lo(trap_block), %g2
266         add     %g2, %g1, %g2
267         stx     %o0, [%g2 + TRAP_PER_CPU_PGD_PADDR]
268
269         sethi   %hi(tlb_type), %g1
270         lduw    [%g1 + %lo(tlb_type)], %g1
271         cmp     %g1, 3
272         bne,pt  %icc, 1f
273          nop
274
275         /* Hypervisor TSB switch. */
276         mov     SCRATCHPAD_UTSBREG1, %g1
277         stxa    %o1, [%g1] ASI_SCRATCHPAD
278         mov     -1, %g2
279         mov     SCRATCHPAD_UTSBREG2, %g1
280         stxa    %g2, [%g1] ASI_SCRATCHPAD
281
282         /* Save away %o5's %pstate, we have to use %o5 for
283          * the hypervisor call.
284          */
285         mov     %o5, %g1
286
287         mov     HV_FAST_MMU_TSB_CTXNON0, %o5
288         mov     1, %o0
289         mov     %o4, %o1
290         ta      HV_FAST_TRAP
291
292         /* Finish up and restore %o5.  */
293         ba,pt   %xcc, 9f
294          mov    %g1, %o5
295
296         /* SUN4U TSB switch.  */
297 1:      mov     TSB_REG, %g1
298         stxa    %o1, [%g1] ASI_DMMU
299         membar  #Sync
300         stxa    %o1, [%g1] ASI_IMMU
301         membar  #Sync
302
303 2:      brz     %o2, 9f
304          nop
305
306         sethi   %hi(sparc64_highest_unlocked_tlb_ent), %g2
307         mov     TLB_TAG_ACCESS, %g1
308         lduw    [%g2 + %lo(sparc64_highest_unlocked_tlb_ent)], %g2
309         stxa    %o2, [%g1] ASI_DMMU
310         membar  #Sync
311         sllx    %g2, 3, %g2
312         stxa    %o3, [%g2] ASI_DTLB_DATA_ACCESS
313         membar  #Sync
314 9:
315         wrpr    %o5, %pstate
316
317         retl
318          nop