3 * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
4 * Rewritten by Cort Dougan (cort@cs.nmt.edu) for PReP
5 * Copyright (C) 1996 Cort Dougan <cort@cs.nmt.edu>
6 * Adapted for Power Macintosh by Paul Mackerras.
7 * Low-level exception handlers and MMU support
8 * rewritten by Paul Mackerras.
9 * Copyright (C) 1996 Paul Mackerras.
11 * This file contains low-level assembler routines for managing
12 * the PowerPC MMU hash table. (PPC 8xx processors don't use a
13 * hash table, so this file is not used on them.)
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version
18 * 2 of the License, or (at your option) any later version.
24 #include <asm/pgtable.h>
25 #include <asm/cputable.h>
26 #include <asm/ppc_asm.h>
27 #include <asm/thread_info.h>
28 #include <asm/asm-offsets.h>
36 #endif /* CONFIG_SMP */
39 * Load a PTE into the hash table, if possible.
40 * The address is in r4, and r3 contains an access flag:
41 * _PAGE_RW (0x400) if a write.
42 * r9 contains the SRR1 value, from which we use the MSR_PR bit.
43 * SPRG3 contains the physical address of the current task's thread.
45 * Returns to the caller if the access is illegal or there is no
46 * mapping for the address. Otherwise it places an appropriate PTE
47 * in the hash table and returns from the exception.
48 * Uses r0, r3 - r8, r10, ctr, lr.
52 tophys(r7,0) /* gets -KERNELBASE into r7 */
54 addis r8,r7,mmu_hash_lock@h
55 ori r8,r8,mmu_hash_lock@l
68 /* Get PTE (linux-style) and check access */
69 lis r0,KERNELBASE@h /* check if kernel address */
71 mfspr r8,SPRN_SPRG3 /* current task's THREAD (phys) */
72 ori r3,r3,_PAGE_USER|_PAGE_PRESENT /* test low addresses as user */
73 lwz r5,PGDIR(r8) /* virt page-table root */
74 blt+ 112f /* assume user more likely */
75 lis r5,swapper_pg_dir@ha /* if kernel address, use */
76 addi r5,r5,swapper_pg_dir@l /* kernel page table */
77 rlwimi r3,r9,32-12,29,29 /* MSR_PR -> _PAGE_USER */
78 112: add r5,r5,r7 /* convert to phys addr */
79 #ifndef CONFIG_PTE_64BIT
80 rlwimi r5,r4,12,20,29 /* insert top 10 bits of address */
81 lwz r8,0(r5) /* get pmd entry */
82 rlwinm. r8,r8,0,0,19 /* extract address of pte page */
84 rlwinm r8,r4,13,19,29 /* Compute pgdir/pmd offset */
85 lwzx r8,r8,r5 /* Get L1 entry */
86 rlwinm. r8,r8,0,0,20 /* extract pt base address */
89 beq- hash_page_out /* return if no mapping */
91 /* XXX it seems like the 601 will give a machine fault on the
92 rfi if its alignment is wrong (bottom 4 bits of address are
93 8 or 0xc) and we have had a not-taken conditional branch
94 to the address following the rfi. */
97 #ifndef CONFIG_PTE_64BIT
98 rlwimi r8,r4,22,20,29 /* insert next 10 bits of address */
100 rlwimi r8,r4,23,20,28 /* compute pte address */
102 rlwinm r0,r3,32-3,24,24 /* _PAGE_RW access -> _PAGE_DIRTY */
103 ori r0,r0,_PAGE_ACCESSED|_PAGE_HASHPTE
106 * Update the linux PTE atomically. We do the lwarx up-front
107 * because almost always, there won't be a permission violation
108 * and there won't already be an HPTE, and thus we will have
109 * to update the PTE to set _PAGE_HASHPTE. -- paulus.
111 * If PTE_64BIT is set, the low word is the flags word; use that
112 * word for locking since it contains all the interesting bits.
114 #if (PTE_FLAGS_OFFSET != 0)
115 addi r8,r8,PTE_FLAGS_OFFSET
118 lwarx r6,0,r8 /* get linux-style pte, flag word */
119 andc. r5,r3,r6 /* check access & ~permission */
121 bne- hash_page_out /* return if access not permitted */
125 or r5,r0,r6 /* set accessed/dirty bits */
126 #ifdef CONFIG_PTE_64BIT
128 subf r10,r6,r8 /* create false data dependency */
129 subi r10,r10,PTE_FLAGS_OFFSET
130 lwzx r10,r6,r10 /* Get upper PTE word */
132 lwz r10,-PTE_FLAGS_OFFSET(r8)
133 #endif /* CONFIG_SMP */
134 #endif /* CONFIG_PTE_64BIT */
135 stwcx. r5,0,r8 /* attempt to update PTE */
136 bne- retry /* retry if someone got there first */
138 mfsrin r3,r4 /* get segment reg for segment */
141 bl create_hpte /* add the hash table entry */
145 addis r8,r7,mmu_hash_lock@ha
147 stw r0,mmu_hash_lock@l(r8)
150 /* Return from the exception */
156 b fast_exception_return
161 addis r8,r7,mmu_hash_lock@ha
163 stw r0,mmu_hash_lock@l(r8)
165 #endif /* CONFIG_SMP */
168 * Add an entry for a particular page to the hash table.
170 * add_hash_page(unsigned context, unsigned long va, unsigned long pmdval)
172 * We assume any necessary modifications to the pte (e.g. setting
173 * the accessed bit) have already been done and that there is actually
174 * a hash table in use (i.e. we're not on a 603).
176 _GLOBAL(add_hash_page)
180 /* Convert context and va to VSID */
181 mulli r3,r3,897*16 /* multiply context by context skew */
182 rlwinm r0,r4,4,28,31 /* get ESID (top 4 bits of va) */
183 mulli r0,r0,0x111 /* multiply by ESID skew */
184 add r3,r3,r0 /* note create_hpte trims to 24 bits */
187 rlwinm r8,r1,0,0,(31-THREAD_SHIFT) /* use cpu number to make tag */
188 lwz r8,TI_CPU(r8) /* to go in mmu_hash_lock */
190 #endif /* CONFIG_SMP */
193 * We disable interrupts here, even on UP, because we don't
194 * want to race with hash_page, and because we want the
195 * _PAGE_HASHPTE bit to be a reliable indication of whether
196 * the HPTE exists (or at least whether one did once).
197 * We also turn off the MMU for data accesses so that we
198 * we can't take a hash table miss (assuming the code is
199 * covered by a BAT). -- paulus
203 rlwinm r0,r9,0,17,15 /* clear bit 16 (MSR_EE) */
204 rlwinm r0,r0,0,28,26 /* clear MSR_DR */
212 addis r6,r7,mmu_hash_lock@ha
213 addi r6,r6,mmu_hash_lock@l
214 10: lwarx r0,0,r6 /* take the mmu_hash_lock */
227 * Fetch the linux pte and test and set _PAGE_HASHPTE atomically.
228 * If _PAGE_HASHPTE was already set, we don't replace the existing
229 * HPTE, so we just unlock and return.
232 #ifndef CONFIG_PTE_64BIT
233 rlwimi r8,r4,22,20,29
235 rlwimi r8,r4,23,20,28
236 addi r8,r8,PTE_FLAGS_OFFSET
239 andi. r0,r6,_PAGE_HASHPTE
240 bne 9f /* if HASHPTE already set, done */
241 #ifdef CONFIG_PTE_64BIT
243 subf r10,r6,r8 /* create false data dependency */
244 subi r10,r10,PTE_FLAGS_OFFSET
245 lwzx r10,r6,r10 /* Get upper PTE word */
247 lwz r10,-PTE_FLAGS_OFFSET(r8)
248 #endif /* CONFIG_SMP */
249 #endif /* CONFIG_PTE_64BIT */
250 ori r5,r6,_PAGE_HASHPTE
258 addis r6,r7,mmu_hash_lock@ha
259 addi r6,r6,mmu_hash_lock@l
262 stw r0,0(r6) /* clear mmu_hash_lock */
265 /* reenable interrupts and DR */
275 * This routine adds a hardware PTE to the hash table.
276 * It is designed to be called with the MMU either on or off.
277 * r3 contains the VSID, r4 contains the virtual address,
278 * r5 contains the linux PTE, r6 contains the old value of the
279 * linux PTE (before setting _PAGE_HASHPTE) and r7 contains the
280 * offset to be added to addresses (0 if the MMU is on,
281 * -KERNELBASE if it is off). r10 contains the upper half of
282 * the PTE if CONFIG_PTE_64BIT.
283 * On SMP, the caller should have the mmu_hash_lock held.
284 * We assume that the caller has (or will) set the _PAGE_HASHPTE
285 * bit in the linux PTE in memory. The value passed in r6 should
286 * be the old linux PTE value; if it doesn't have _PAGE_HASHPTE set
287 * this routine will skip the search for an existing HPTE.
288 * This procedure modifies r0, r3 - r6, r8, cr0.
291 * For speed, 4 of the instructions get patched once the size and
292 * physical address of the hash table are known. These definitions
293 * of Hash_base and Hash_bits below are just an example.
295 Hash_base = 0xc0180000
296 Hash_bits = 12 /* e.g. 256kB hash table */
297 Hash_msk = (((1 << Hash_bits) - 1) * 64)
299 /* defines for the PTE format for 32-bit PPCs */
302 #define LG_PTEG_SIZE 6
308 #define PTE_V 0x80000000
309 #define TST_V(r) rlwinm. r,r,0,0,0
310 #define SET_V(r) oris r,r,PTE_V@h
311 #define CLR_V(r,t) rlwinm r,r,0,1,31
313 #define HASH_LEFT 31-(LG_PTEG_SIZE+Hash_bits-1)
314 #define HASH_RIGHT 31-LG_PTEG_SIZE
317 /* Convert linux-style PTE (r5) to low word of PPC-style PTE (r8) */
318 rlwinm r8,r5,32-10,31,31 /* _PAGE_RW -> PP lsb */
319 rlwinm r0,r5,32-7,31,31 /* _PAGE_DIRTY -> PP lsb */
320 and r8,r8,r0 /* writable if _RW & _DIRTY */
321 rlwimi r5,r5,32-1,30,30 /* _PAGE_USER -> PP msb */
322 rlwimi r5,r5,32-2,31,31 /* _PAGE_USER -> PP lsb */
323 ori r8,r8,0xe14 /* clear out reserved bits and M */
324 andc r8,r5,r8 /* PP = user? (rw&dirty? 2: 3): 0 */
326 rlwinm r8,r8,0,~_PAGE_COHERENT /* clear M (coherence not required) */
327 END_FTR_SECTION_IFCLR(CPU_FTR_NEED_COHERENT)
328 #ifdef CONFIG_PTE_64BIT
329 /* Put the XPN bits into the PTE */
330 rlwimi r8,r10,8,20,22
331 rlwimi r8,r10,2,29,29
334 /* Construct the high word of the PPC-style PTE (r5) */
335 rlwinm r5,r3,7,1,24 /* put VSID in 0x7fffff80 bits */
336 rlwimi r5,r4,10,26,31 /* put in API (abbrev page index) */
337 SET_V(r5) /* set V (valid) bit */
339 /* Get the address of the primary PTE group in the hash table (r3) */
340 _GLOBAL(hash_page_patch_A)
341 addis r0,r7,Hash_base@h /* base address of hash table */
342 rlwimi r0,r3,LG_PTEG_SIZE,HASH_LEFT,HASH_RIGHT /* VSID -> hash */
343 rlwinm r3,r4,20+LG_PTEG_SIZE,HASH_LEFT,HASH_RIGHT /* PI -> hash */
344 xor r3,r3,r0 /* make primary hash */
345 li r0,8 /* PTEs/group */
348 * Test the _PAGE_HASHPTE bit in the old linux PTE, and skip the search
349 * if it is clear, meaning that the HPTE isn't there already...
351 andi. r6,r6,_PAGE_HASHPTE
352 beq+ 10f /* no PTE: go look for an empty slot */
355 addis r4,r7,htab_hash_searches@ha
356 lwz r6,htab_hash_searches@l(r4)
357 addi r6,r6,1 /* count how many searches we do */
358 stw r6,htab_hash_searches@l(r4)
360 /* Search the primary PTEG for a PTE whose 1st (d)word matches r5 */
362 addi r4,r3,-HPTE_SIZE
363 1: LDPTEu r6,HPTE_SIZE(r4) /* get next PTE */
365 bdnzf 2,1b /* loop while ctr != 0 && !cr0.eq */
368 /* Search the secondary PTEG for a matching PTE */
369 ori r5,r5,PTE_H /* set H (secondary hash) bit */
370 _GLOBAL(hash_page_patch_B)
371 xoris r4,r3,Hash_msk>>16 /* compute secondary hash */
372 xori r4,r4,(-PTEG_SIZE & 0xffff)
373 addi r4,r4,-HPTE_SIZE
375 2: LDPTEu r6,HPTE_SIZE(r4)
379 xori r5,r5,PTE_H /* clear H bit again */
381 /* Search the primary PTEG for an empty slot */
383 addi r4,r3,-HPTE_SIZE /* search primary PTEG */
384 1: LDPTEu r6,HPTE_SIZE(r4) /* get next PTE */
385 TST_V(r6) /* test valid bit */
386 bdnzf 2,1b /* loop while ctr != 0 && !cr0.eq */
389 /* update counter of times that the primary PTEG is full */
390 addis r4,r7,primary_pteg_full@ha
391 lwz r6,primary_pteg_full@l(r4)
393 stw r6,primary_pteg_full@l(r4)
395 /* Search the secondary PTEG for an empty slot */
396 ori r5,r5,PTE_H /* set H (secondary hash) bit */
397 _GLOBAL(hash_page_patch_C)
398 xoris r4,r3,Hash_msk>>16 /* compute secondary hash */
399 xori r4,r4,(-PTEG_SIZE & 0xffff)
400 addi r4,r4,-HPTE_SIZE
402 2: LDPTEu r6,HPTE_SIZE(r4)
406 xori r5,r5,PTE_H /* clear H bit again */
409 * Choose an arbitrary slot in the primary PTEG to overwrite.
410 * Since both the primary and secondary PTEGs are full, and we
411 * have no information that the PTEs in the primary PTEG are
412 * more important or useful than those in the secondary PTEG,
413 * and we know there is a definite (although small) speed
414 * advantage to putting the PTE in the primary PTEG, we always
415 * put the PTE in the primary PTEG.
417 * In addition, we skip any slot that is mapping kernel text in
418 * order to avoid a deadlock when not using BAT mappings if
419 * trying to hash in the kernel hash code itself after it has
420 * already taken the hash table lock. This works in conjunction
421 * with pre-faulting of the kernel text.
423 * If the hash table bucket is full of kernel text entries, we'll
424 * lockup here but that shouldn't happen
427 1: addis r4,r7,next_slot@ha /* get next evict slot */
428 lwz r6,next_slot@l(r4)
429 addi r6,r6,HPTE_SIZE /* search for candidate */
430 andi. r6,r6,7*HPTE_SIZE
431 stw r6,next_slot@l(r4)
433 LDPTE r0,HPTE_SIZE/2(r4) /* get PTE second word */
436 ori r6,r6,etext@l /* get etext */
438 cmpl cr0,r0,r6 /* compare and try again */
442 /* Store PTE in PTEG */
446 STPTE r8,HPTE_SIZE/2(r4)
448 #else /* CONFIG_SMP */
450 * Between the tlbie above and updating the hash table entry below,
451 * another CPU could read the hash table entry and put it in its TLB.
453 * 1. using an empty slot
454 * 2. updating an earlier entry to change permissions (i.e. enable write)
455 * 3. taking over the PTE for an unrelated address
457 * In each case it doesn't really matter if the other CPUs have the old
458 * PTE in their TLB. So we don't need to bother with another tlbie here,
459 * which is convenient as we've overwritten the register that had the
460 * address. :-) The tlbie above is mainly to make sure that this CPU comes
461 * and gets the new PTE from the hash table.
463 * We do however have to make sure that the PTE is never in an invalid
464 * state with the V bit set.
468 CLR_V(r5,r0) /* clear V (valid) bit in PTE */
472 STPTE r8,HPTE_SIZE/2(r4) /* put in correct RPN, WIMG, PP bits */
475 STPTE r5,0(r4) /* finally set V bit in PTE */
476 #endif /* CONFIG_SMP */
478 sync /* make sure pte updates get to memory */
492 * Flush the entry for a particular page from the hash table.
494 * flush_hash_pages(unsigned context, unsigned long va, unsigned long pmdval,
497 * We assume that there is a hash table in use (Hash != 0).
499 _GLOBAL(flush_hash_pages)
503 * We disable interrupts here, even on UP, because we want
504 * the _PAGE_HASHPTE bit to be a reliable indication of
505 * whether the HPTE exists (or at least whether one did once).
506 * We also turn off the MMU for data accesses so that we
507 * we can't take a hash table miss (assuming the code is
508 * covered by a BAT). -- paulus
512 rlwinm r0,r10,0,17,15 /* clear bit 16 (MSR_EE) */
513 rlwinm r0,r0,0,28,26 /* clear MSR_DR */
518 /* First find a PTE in the range that has _PAGE_HASHPTE set */
519 #ifndef CONFIG_PTE_64BIT
520 rlwimi r5,r4,22,20,29
522 rlwimi r5,r4,23,20,28
524 1: lwz r0,PTE_FLAGS_OFFSET(r5)
526 andi. r0,r0,_PAGE_HASHPTE
534 /* Convert context and va to VSID */
535 2: mulli r3,r3,897*16 /* multiply context by context skew */
536 rlwinm r0,r4,4,28,31 /* get ESID (top 4 bits of va) */
537 mulli r0,r0,0x111 /* multiply by ESID skew */
538 add r3,r3,r0 /* note code below trims to 24 bits */
540 /* Construct the high word of the PPC-style PTE (r11) */
541 rlwinm r11,r3,7,1,24 /* put VSID in 0x7fffff80 bits */
542 rlwimi r11,r4,10,26,31 /* put in API (abbrev page index) */
543 SET_V(r11) /* set V (valid) bit */
546 addis r9,r7,mmu_hash_lock@ha
547 addi r9,r9,mmu_hash_lock@l
548 rlwinm r8,r1,0,0,(31-THREAD_SHIFT)
565 * Check the _PAGE_HASHPTE bit in the linux PTE. If it is
566 * already clear, we're done (for this pte). If not,
567 * clear it (atomically) and proceed. -- paulus.
569 #if (PTE_FLAGS_OFFSET != 0)
570 addi r5,r5,PTE_FLAGS_OFFSET
572 33: lwarx r8,0,r5 /* fetch the pte flags word */
573 andi. r0,r8,_PAGE_HASHPTE
574 beq 8f /* done if HASHPTE is already clear */
575 rlwinm r8,r8,0,31,29 /* clear HASHPTE bit */
576 stwcx. r8,0,r5 /* update the pte */
579 /* Get the address of the primary PTE group in the hash table (r3) */
580 _GLOBAL(flush_hash_patch_A)
581 addis r8,r7,Hash_base@h /* base address of hash table */
582 rlwimi r8,r3,LG_PTEG_SIZE,HASH_LEFT,HASH_RIGHT /* VSID -> hash */
583 rlwinm r0,r4,20+LG_PTEG_SIZE,HASH_LEFT,HASH_RIGHT /* PI -> hash */
584 xor r8,r0,r8 /* make primary hash */
586 /* Search the primary PTEG for a PTE whose 1st (d)word matches r5 */
587 li r0,8 /* PTEs/group */
589 addi r12,r8,-HPTE_SIZE
590 1: LDPTEu r0,HPTE_SIZE(r12) /* get next PTE */
592 bdnzf 2,1b /* loop while ctr != 0 && !cr0.eq */
595 /* Search the secondary PTEG for a matching PTE */
596 ori r11,r11,PTE_H /* set H (secondary hash) bit */
597 li r0,8 /* PTEs/group */
598 _GLOBAL(flush_hash_patch_B)
599 xoris r12,r8,Hash_msk>>16 /* compute secondary hash */
600 xori r12,r12,(-PTEG_SIZE & 0xffff)
601 addi r12,r12,-HPTE_SIZE
603 2: LDPTEu r0,HPTE_SIZE(r12)
606 xori r11,r11,PTE_H /* clear H again */
607 bne- 4f /* should rarely fail to find it */
610 STPTE r0,0(r12) /* invalidate entry */
612 tlbie r4 /* in hw tlb too */
615 8: ble cr1,9f /* if all ptes checked */
619 lwz r0,0(r5) /* check next pte */
621 andi. r0,r0,_PAGE_HASHPTE
629 stw r0,0(r9) /* clear mmu_hash_lock */
638 * Flush an entry from the TLB
642 rlwinm r8,r1,0,0,(31-THREAD_SHIFT)
647 rlwinm r0,r10,0,17,15 /* clear bit 16 (MSR_EE) */
648 rlwinm r0,r0,0,28,26 /* clear DR */
652 lis r9,mmu_hash_lock@h
653 ori r9,r9,mmu_hash_lock@l
665 stw r0,0(r9) /* clear mmu_hash_lock */
669 #else /* CONFIG_SMP */
672 #endif /* CONFIG_SMP */
676 * Flush the entire TLB. 603/603e only
679 #if defined(CONFIG_SMP)
680 rlwinm r8,r1,0,0,(31-THREAD_SHIFT)
685 rlwinm r0,r10,0,17,15 /* clear bit 16 (MSR_EE) */
686 rlwinm r0,r0,0,28,26 /* clear DR */
690 lis r9,mmu_hash_lock@h
691 ori r9,r9,mmu_hash_lock@l
703 stw r0,0(r9) /* clear mmu_hash_lock */
707 #else /* CONFIG_SMP */
711 #endif /* CONFIG_SMP */