2 * arch/ppc/kernel/hashtable.S
4 * $Id: hashtable.S,v 1.6 1999/10/08 01:56:15 paulus Exp $
7 * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
8 * Rewritten by Cort Dougan (cort@cs.nmt.edu) for PReP
9 * Copyright (C) 1996 Cort Dougan <cort@cs.nmt.edu>
10 * Adapted for Power Macintosh by Paul Mackerras.
11 * Low-level exception handlers and MMU support
12 * rewritten by Paul Mackerras.
13 * Copyright (C) 1996 Paul Mackerras.
15 * This file contains low-level assembler routines for managing
16 * the PowerPC MMU hash table. (PPC 8xx processors don't use a
17 * hash table, so this file is not used on them.)
19 * This program is free software; you can redistribute it and/or
20 * modify it under the terms of the GNU General Public License
21 * as published by the Free Software Foundation; either version
22 * 2 of the License, or (at your option) any later version.
27 #include <asm/processor.h>
29 #include <linux/config.h>
32 * Load a PTE into the hash table, if possible.
33 * The address is in r3, and r4 contains access flags:
34 * _PAGE_USER (4) if a user-mode access, ored with
35 * _PAGE_RW (2) if a write. r20 contains DSISR or SRR1,
36 * so bit 1 (0x40000000) is set if the exception was due
37 * to no matching PTE being found in the hash table.
38 * SPRG3 contains the physical address of the current task's thread.
40 * Returns to the caller if the access is illegal or there is no
41 * mapping for the address. Otherwise it places an appropriate PTE
42 * in the hash table and returns from the exception.
43 * Uses r0, r2 - r6, ctr, lr.
45 * For speed, 4 of the instructions get patched once the size and
46 * physical address of the hash table are known. These definitions
47 * of Hash_base and Hash_bits below are just an example.
50 Hash_bits = 12 /* e.g. 256kB hash table */
51 Hash_msk = (((1 << Hash_bits) - 1) * 64)
58 lis r2,hash_table_lock@h
59 ori r2,r2,hash_table_lock@l
64 lwz r0,PROCESSOR-THREAD(r5)
84 /* Get PTE (linux-style) and check access */
85 mfspr r2,SPRG3 /* current task's THREAD (phys) */
86 lwz r5,PGDIR(r2) /* virt page-table root */
87 tophys(r5,r5) /* convert to phys addr */
88 rlwimi r5,r3,12,20,29 /* insert top 10 bits of address */
89 lwz r5,0(r5) /* get pmd entry */
90 rlwinm. r5,r5,0,0,19 /* extract address of pte page */
92 beq- hash_page_out /* return if no mapping */
94 /* XXX it seems like the 601 will give a machine fault on the
95 rfi if its alignment is wrong (bottom 4 bits of address are
96 8 or 0xc) and we have had a not-taken conditional branch
97 to the address following the rfi. */
101 rlwimi r2,r3,22,20,29 /* insert next 10 bits of address */
102 lwz r6,0(r2) /* get linux-style pte */
103 ori r4,r4,1 /* set _PAGE_PRESENT bit in access */
104 andc. r0,r4,r6 /* check access & ~permission */
106 bne- hash_page_out /* return if access not permitted */
111 ori r6,r6,0x100 /* set _PAGE_ACCESSED in pte */
112 rlwinm r5,r4,5,24,24 /* _PAGE_RW access -> _PAGE_DIRTY */
113 rlwimi r5,r4,7,22,22 /* _PAGE_RW -> _PAGE_HWWRITE */
115 stw r6,0(r2) /* update PTE (accessed/dirty bits) */
117 /* Convert linux-style PTE to low word of PPC-style PTE */
118 rlwinm r4,r6,32-9,31,31 /* _PAGE_HWWRITE -> PP lsb */
119 rlwimi r6,r6,32-1,31,31 /* _PAGE_USER -> PP (both bits now) */
120 ori r4,r4,0xe04 /* clear out reserved bits */
121 andc r6,r6,r4 /* PP=2 or 0, when _PAGE_HWWRITE */
123 /* Construct the high word of the PPC-style PTE */
124 mfsrin r5,r3 /* get segment reg for segment */
127 #else /* CONFIG_PPC64 */
128 rlwinm r5,r5,7,1,24 /* put VSID in 0x7fffff80 bits */
129 #endif /* CONFIG_PPC64 */
131 #ifndef __SMP__ /* do this later for SMP */
133 ori r5,r5,1 /* set V (valid) bit */
134 #else /* CONFIG_PPC64 */
135 oris r5,r5,0x8000 /* set V (valid) bit */
136 #endif /* CONFIG_PPC64 */
140 /* XXX: does this insert the api correctly? -- Cort */
141 rlwimi r5,r3,17,21,25 /* put in API (abbrev page index) */
142 #else /* CONFIG_PPC64 */
143 rlwimi r5,r3,10,26,31 /* put in API (abbrev page index) */
144 #endif /* CONFIG_PPC64 */
145 /* Get the address of the primary PTE group in the hash table */
146 .globl hash_page_patch_A
148 lis r4,Hash_base@h /* base address of hash table */
149 rlwimi r4,r5,32-1,26-Hash_bits,25 /* (VSID & hash_mask) << 6 */
150 rlwinm r0,r3,32-6,26-Hash_bits,25 /* (PI & hash_mask) << 6 */
151 xor r4,r4,r0 /* make primary hash */
153 /* See whether it was a PTE not found exception or a
154 protection violation. */
156 li r2,8 /* PTEs/group */
157 bne 10f /* no PTE: go look for an empty slot */
158 tlbie r3 /* invalidate TLB entry */
160 /* Search the primary PTEG for a PTE whose 1st word matches r5 */
165 lwzu r0,16(r3) /* get next PTE */
167 lwzu r0,8(r3) /* get next PTE */
170 bdnzf 2,1b /* loop while ctr != 0 && !cr0.eq */
173 /* Search the secondary PTEG for a matching PTE */
175 ori r5,r5,0x2 /* set H (secondary hash) bit */
177 ori r5,r5,0x40 /* set H (secondary hash) bit */
179 .globl hash_page_patch_B
181 xoris r3,r4,Hash_msk>>16 /* compute secondary hash */
199 xori r5,r5,0x2 /* clear H bit again */
201 xori r5,r5,0x40 /* clear H bit again */
204 /* Search the primary PTEG for an empty slot */
207 addi r3,r4,-16 /* search primary PTEG */
209 addi r3,r4,-8 /* search primary PTEG */
213 lwzu r0,16(r3) /* get next PTE */
216 lwzu r0,8(r3) /* get next PTE */
217 rlwinm. r0,r0,0,0,0 /* only want to check valid bit */
219 bdnzf 2,1b /* loop while ctr != 0 && !cr0.eq */
222 /* Search the secondary PTEG for an empty slot */
224 ori r5,r5,0x2 /* set H (secondary hash) bit */
226 ori r5,r5,0x40 /* set H (secondary hash) bit */
228 .globl hash_page_patch_C
230 xoris r3,r4,Hash_msk>>16 /* compute secondary hash */
244 rlwinm. r0,r0,0,0,0 /* only want to check valid bit */
250 * Choose an arbitrary slot in the primary PTEG to overwrite.
251 * Since both the primary and secondary PTEGs are full, and we
252 * have no information that the PTEs in the primary PTEG are
253 * more important or useful than those in the secondary PTEG,
254 * and we know there is a definite (although small) speed
255 * advantage to putting the PTE in the primary PTEG, we always
256 * put the PTE in the primary PTEG.
259 xori r5,r5,0x2 /* clear H bit again */
261 xori r5,r5,0x40 /* clear H bit again */
265 lwz r2,next_slot@l(r3)
273 stw r2,next_slot@l(r3)
276 /* update counter of evicted pages */
277 lis r2,htab_evicts@ha
279 lwz r4,htab_evicts@l(r2)
281 stw r4,htab_evicts@l(r2)
284 /* Store PTE in PTEG */
301 * Between the tlbie above and updating the hash table entry below,
302 * another CPU could read the hash table entry and put it in its TLB.
304 * 1. using an empty slot
305 * 2. updating an earlier entry to change permissions (i.e. enable write)
306 * 3. taking over the PTE for an unrelated address
308 * In each case it doesn't really matter if the other CPUs have the old
309 * PTE in their TLB. So we don't need to bother with another tlbie here,
310 * which is convenient as we've overwritten the register that had the
311 * address. :-) The tlbie above is mainly to make sure that this CPU comes
312 * and gets the new PTE from the hash table.
314 * We do however have to make sure that the PTE is never in an invalid
315 * state with the V bit set.
319 stw r5,0(r3) /* clear V (valid) bit in PTE */
323 stw r6,4(r3) /* put in correct RPN, WIMG, PP bits */
326 stw r5,0(r3) /* finally set V bit in PTE */
330 * Update the hash table miss count. We only want misses here
331 * that _are_ valid addresses and have a pte otherwise we don't
332 * count it as a reload. do_page_fault() takes care of bad addrs
333 * and entries that need linux-style pte's created.
335 * safe to use r2 here since we're not using it as current yet
336 * update the htab misses count
339 lis r2,htab_reloads@ha
341 lwz r3,htab_reloads@l(r2)
343 stw r3,htab_reloads@l(r2)
346 lis r2,hash_table_lock@ha
349 stw r0,hash_table_lock@l(r2)
353 /* Return from the exception */
367 /* we haven't used xer */
378 lis r2,hash_table_lock@ha
381 stw r0,hash_table_lock@l(r2)
386 .globl hash_table_lock
397 * Flush entries from the hash table with VSIDs in the range
400 _GLOBAL(flush_hash_segments)
402 lwz r5,Hash@l(r5) /* base of hash table */
414 /* Note - we had better not do anything which could generate
415 a hash table miss while we have the hash table locked,
416 or we'll get a deadlock. -paulus */
419 rlwinm r0,r10,0,17,15 /* clear bit 16 (MSR_EE) */
422 lis r9,hash_table_lock@h
423 ori r9,r9,hash_table_lock@l
433 rlwinm r3,r3,7,1,24 /* put VSID lower limit in position */
434 oris r3,r3,0x8000 /* set V bit */
435 rlwinm r4,r4,7,1,24 /* put VSID upper limit in position */
439 lwz r6,Hash_size@l(r6) /* size in bytes */
440 srwi r6,r6,3 /* # PTEs */
444 1: lwzu r6,8(r5) /* get next tag word */
447 cror 0,0,5 /* set cr0.lt if out of range */
448 blt 2f /* branch if out of range */
449 stw r0,0(r5) /* invalidate entry */
450 2: bdnz 1b /* continue with loop */
457 lis r3,hash_table_lock@ha
458 stw r0,hash_table_lock@l(r3)
465 * Flush the entry for a particular page from the hash table.
467 * flush_hash_page(unsigned context, unsigned long va)
469 _GLOBAL(flush_hash_page)
471 lwz r6,Hash@l(r6) /* hash table base */
472 cmpwi 0,r6,0 /* hash table in use? */
474 tlbie r4 /* in hw tlb too */
483 /* Note - we had better not do anything which could generate
484 a hash table miss while we have the hash table locked,
485 or we'll get a deadlock. -paulus */
488 rlwinm r0,r10,0,17,15 /* clear bit 16 (MSR_EE) */
491 lis r9,hash_table_lock@h
492 ori r9,r9,hash_table_lock@l
502 rlwinm r3,r3,11,1,20 /* put context into vsid */
503 rlwimi r3,r4,11,21,24 /* put top 4 bits of va into vsid */
504 oris r3,r3,0x8000 /* set V (valid) bit */
505 rlwimi r3,r4,10,26,31 /* put in API (abbrev page index) */
506 rlwinm r7,r4,32-6,10,25 /* get page index << 6 */
507 rlwinm r5,r3,32-1,7,25 /* vsid << 6 */
508 xor r7,r7,r5 /* primary hash << 6 */
510 lwz r5,Hash_mask@l(r5) /* hash mask */
511 slwi r5,r5,6 /* << 6 */
513 add r6,r6,r7 /* address of primary PTEG */
517 1: lwzu r0,8(r7) /* get next PTE */
518 cmpw 0,r0,r3 /* see if tag matches */
519 bdnzf 2,1b /* while --ctr != 0 && !cr0.eq */
520 beq 3f /* if we found it */
521 ori r3,r3,0x40 /* set H (alt. hash) bit */
522 xor r6,r6,r5 /* address of secondary PTEG */
525 2: lwzu r0,8(r7) /* get next PTE */
526 cmpw 0,r0,r3 /* see if tag matches */
527 bdnzf 2,2b /* while --ctr != 0 && !cr0.eq */
528 bne 4f /* if we didn't find it */
530 stw r0,0(r7) /* invalidate entry */
532 tlbie r4 /* in hw tlb too */
538 stw r0,0(r9) /* clear hash_table_lock */