vserver 1.9.5.x5
[linux-2.6.git] / arch / ppc64 / mm / hash_low.S
1 /*
2  * ppc64 MMU hashtable management routines
3  *
4  * (c) Copyright IBM Corp. 2003
5  *
6  * Maintained by: Benjamin Herrenschmidt
7  *                <benh@kernel.crashing.org>
8  *
9  * This file is covered by the GNU Public Licence v2 as
10  * described in the kernel's COPYING file.
11  */
12
13 #include <asm/processor.h>
14 #include <asm/pgtable.h>
15 #include <asm/mmu.h>
16 #include <asm/page.h>
17 #include <asm/types.h>
18 #include <asm/ppc_asm.h>
19 #include <asm/offsets.h>
20 #include <asm/cputable.h>
21
22         .text
23
24 /*
25  * Stackframe:
26  *              
27  *         +-> Back chain                       (SP + 256)
28  *         |   General register save area       (SP + 112)
29  *         |   Parameter save area              (SP + 48)
30  *         |   TOC save area                    (SP + 40)
31  *         |   link editor doubleword           (SP + 32)
32  *         |   compiler doubleword              (SP + 24)
33  *         |   LR save area                     (SP + 16)
34  *         |   CR save area                     (SP + 8)
35  * SP ---> +-- Back chain                       (SP + 0)
36  */
37 #define STACKFRAMESIZE  256
38
39 /* Save parameters offsets */
40 #define STK_PARM(i)     (STACKFRAMESIZE + 48 + ((i)-3)*8)
41
42 /* Save non-volatile offsets */
43 #define STK_REG(i)      (112 + ((i)-14)*8)
44
45 /*
46  * _hash_page(unsigned long ea, unsigned long access, unsigned long vsid,
47  *              pte_t *ptep, unsigned long trap, int local)
48  *
49  * Adds a page to the hash table. This is the non-LPAR version for now
50  */
51
52 _GLOBAL(__hash_page)
53         mflr    r0
54         std     r0,16(r1)
55         stdu    r1,-STACKFRAMESIZE(r1)
56         /* Save all params that we need after a function call */
57         std     r6,STK_PARM(r6)(r1)
58         std     r8,STK_PARM(r8)(r1)
59         
60         /* Add _PAGE_PRESENT to access */
61         ori     r4,r4,_PAGE_PRESENT
62
63         /* Save non-volatile registers.
64          * r31 will hold "old PTE"
65          * r30 is "new PTE"
66          * r29 is "va"
67          * r28 is a hash value
68          * r27 is hashtab mask (maybe dynamic patched instead ?)
69          */
70         std     r27,STK_REG(r27)(r1)
71         std     r28,STK_REG(r28)(r1)
72         std     r29,STK_REG(r29)(r1)
73         std     r30,STK_REG(r30)(r1)
74         std     r31,STK_REG(r31)(r1)
75         
76         /* Step 1:
77          *
78          * Check permissions, atomically mark the linux PTE busy
79          * and hashed.
80          */ 
81 1:
82         ldarx   r31,0,r6
83         /* Check access rights (access & ~(pte_val(*ptep))) */
84         andc.   r0,r4,r31
85         bne-    htab_wrong_access
86         /* Check if PTE is busy */
87         andi.   r0,r31,_PAGE_BUSY
88         bne-    1b
89         /* Prepare new PTE value (turn access RW into DIRTY, then
90          * add BUSY,HASHPTE and ACCESSED)
91          */
92         rlwinm  r30,r4,5,24,24  /* _PAGE_RW -> _PAGE_DIRTY */
93         or      r30,r30,r31
94         ori     r30,r30,_PAGE_BUSY | _PAGE_ACCESSED | _PAGE_HASHPTE
95         /* Write the linux PTE atomically (setting busy) */
96         stdcx.  r30,0,r6
97         bne-    1b
98         isync
99
100         /* Step 2:
101          *
102          * Insert/Update the HPTE in the hash table. At this point,
103          * r4 (access) is re-useable, we use it for the new HPTE flags
104          */
105
106         /* Calc va and put it in r29 */
107         rldicr  r29,r5,28,63-28
108         rldicl  r3,r3,0,36
109         or      r29,r3,r29
110
111         /* Calculate hash value for primary slot and store it in r28 */
112         rldicl  r5,r5,0,25              /* vsid & 0x0000007fffffffff */
113         rldicl  r0,r3,64-12,48          /* (ea >> 12) & 0xffff */
114         xor     r28,r5,r0
115         
116         /* Convert linux PTE bits into HW equivalents
117          */
118         andi.   r3,r30,0x1fa            /* Get basic set of flags */
119         rlwinm  r0,r30,32-2+1,30,30     /* _PAGE_RW -> _PAGE_USER (r0) */
120         rlwinm  r4,r30,32-7+1,30,30     /* _PAGE_DIRTY -> _PAGE_USER (r4) */
121         and     r0,r0,r4                /* _PAGE_RW & _PAGE_DIRTY -> r0 bit 30 */
122         andc    r0,r30,r0               /* r0 = pte & ~r0 */
123         rlwimi  r3,r0,32-1,31,31        /* Insert result into PP lsb */
124
125         /* We eventually do the icache sync here (maybe inline that
126          * code rather than call a C function...) 
127          */
128 BEGIN_FTR_SECTION
129 BEGIN_FTR_SECTION
130         mr      r4,r30
131         mr      r5,r7
132         bl      .hash_page_do_lazy_icache
133 END_FTR_SECTION_IFSET(CPU_FTR_NOEXECUTE)
134 END_FTR_SECTION_IFCLR(CPU_FTR_COHERENT_ICACHE)
135
136         /* At this point, r3 contains new PP bits, save them in
137          * place of "access" in the param area (sic)
138          */
139         std     r3,STK_PARM(r4)(r1)
140
141         /* Get htab_hash_mask */
142         ld      r4,htab_hash_mask@got(2)
143         ld      r27,0(r4)       /* htab_hash_mask -> r27 */
144
145         /* Check if we may already be in the hashtable, in this case, we
146          * go to out-of-line code to try to modify the HPTE
147          */
148         andi.   r0,r31,_PAGE_HASHPTE
149         bne     htab_modify_pte
150
151 htab_insert_pte:
152         /* Clear hpte bits in new pte (we also clear BUSY btw) and
153          * add _PAGE_HASHPTE
154          */
155         lis     r0,_PAGE_HPTEFLAGS@h
156         ori     r0,r0,_PAGE_HPTEFLAGS@l
157         andc    r30,r30,r0
158         ori     r30,r30,_PAGE_HASHPTE
159
160         /* page number in r5 */
161         rldicl  r5,r31,64-PTE_SHIFT,PTE_SHIFT
162
163         /* Calculate primary group hash */
164         and     r0,r28,r27
165         rldicr  r3,r0,3,63-3    /* r0 = (hash & mask) << 3 */
166
167         /* Call ppc_md.hpte_insert */
168         ld      r7,STK_PARM(r4)(r1)     /* Retreive new pp bits */
169         mr      r4,r29                  /* Retreive va */
170         li      r6,0                    /* primary slot */
171         li      r8,0                    /* not bolted and not large */
172         li      r9,0
173 _GLOBAL(htab_call_hpte_insert1)
174         bl      .                       /* Will be patched by htab_finish_init() */
175         cmpdi   0,r3,0
176         bge     htab_pte_insert_ok      /* Insertion successful */
177         cmpdi   0,r3,-2                 /* Critical failure */
178         beq-    htab_pte_insert_failure
179
180         /* Now try secondary slot */
181         
182         /* page number in r5 */
183         rldicl  r5,r31,64-PTE_SHIFT,PTE_SHIFT
184
185         /* Calculate secondary group hash */
186         andc    r0,r27,r28
187         rldicr  r3,r0,3,63-3    /* r0 = (~hash & mask) << 3 */
188         
189         /* Call ppc_md.hpte_insert */
190         ld      r7,STK_PARM(r4)(r1)     /* Retreive new pp bits */
191         mr      r4,r29                  /* Retreive va */
192         li      r6,1                    /* secondary slot */
193         li      r8,0                    /* not bolted and not large */
194         li      r9,0
195 _GLOBAL(htab_call_hpte_insert2)
196         bl      .                       /* Will be patched by htab_finish_init() */
197         cmpdi   0,r3,0
198         bge+    htab_pte_insert_ok      /* Insertion successful */
199         cmpdi   0,r3,-2                 /* Critical failure */
200         beq-    htab_pte_insert_failure
201
202         /* Both are full, we need to evict something */
203         mftb    r0
204         /* Pick a random group based on TB */
205         andi.   r0,r0,1
206         mr      r5,r28
207         bne     2f
208         not     r5,r5
209 2:      and     r0,r5,r27
210         rldicr  r3,r0,3,63-3    /* r0 = (hash & mask) << 3 */   
211         /* Call ppc_md.hpte_remove */
212 _GLOBAL(htab_call_hpte_remove)
213         bl      .                       /* Will be patched by htab_finish_init() */
214
215         /* Try all again */
216         b       htab_insert_pte 
217
218 htab_pte_insert_ok:
219         /* Insert slot number & secondary bit in PTE */
220         rldimi  r30,r3,12,63-15
221                 
222         /* Write out the PTE with a normal write
223          * (maybe add eieio may be good still ?)
224          */
225 htab_write_out_pte:
226         ld      r6,STK_PARM(r6)(r1)
227         std     r30,0(r6)
228         li      r3, 0
229 bail:
230         ld      r27,STK_REG(r27)(r1)
231         ld      r28,STK_REG(r28)(r1)
232         ld      r29,STK_REG(r29)(r1)
233         ld      r30,STK_REG(r30)(r1)
234         ld      r31,STK_REG(r31)(r1)
235         addi    r1,r1,STACKFRAMESIZE
236         ld      r0,16(r1)
237         mtlr    r0
238         blr
239
240 htab_modify_pte:
241         /* Keep PP bits in r4 and slot idx from the PTE around in r3 */
242         mr      r4,r3
243         rlwinm  r3,r31,32-12,29,31
244
245         /* Secondary group ? if yes, get a inverted hash value */
246         mr      r5,r28
247         andi.   r0,r31,_PAGE_SECONDARY
248         beq     1f
249         not     r5,r5
250 1:
251         /* Calculate proper slot value for ppc_md.hpte_updatepp */
252         and     r0,r5,r27
253         rldicr  r0,r0,3,63-3    /* r0 = (hash & mask) << 3 */
254         add     r3,r0,r3        /* add slot idx */
255
256         /* Call ppc_md.hpte_updatepp */
257         mr      r5,r29                  /* va */
258         li      r6,0                    /* large is 0 */
259         ld      r7,STK_PARM(r8)(r1)     /* get "local" param */
260 _GLOBAL(htab_call_hpte_updatepp)
261         bl      .                       /* Will be patched by htab_finish_init() */
262
263         /* if we failed because typically the HPTE wasn't really here
264          * we try an insertion. 
265          */
266         cmpdi   0,r3,-1
267         beq-    htab_insert_pte
268
269         /* Clear the BUSY bit and Write out the PTE */
270         li      r0,_PAGE_BUSY
271         andc    r30,r30,r0
272         b       htab_write_out_pte
273
274 htab_wrong_access:
275         /* Bail out clearing reservation */
276         stdcx.  r31,0,r6
277         li      r3,1
278         b       bail
279
280 htab_pte_insert_failure:
281         /* Bail out restoring old PTE */
282         ld      r6,STK_PARM(r6)(r1)
283         std     r31,0(r6)
284         li      r3,-1
285         b       bail
286
287