Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Paul Mundt | 811d50c | 2007-11-20 17:01:55 +0900 | [diff] [blame] | 2 | * The SH64 TLB miss. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * |
| 4 | * Original code from fault.c |
| 5 | * Copyright (C) 2000, 2001 Paolo Alberelli |
| 6 | * |
| 7 | * Fast PTE->TLB refill path |
| 8 | * Copyright (C) 2003 Richard.Curnow@superh.com |
| 9 | * |
| 10 | * IMPORTANT NOTES : |
Paul Mundt | 811d50c | 2007-11-20 17:01:55 +0900 | [diff] [blame] | 11 | * The do_fast_page_fault function is called from a context in entry.S |
| 12 | * where very few registers have been saved. In particular, the code in |
| 13 | * this file must be compiled not to use ANY caller-save registers that |
| 14 | * are not part of the restricted save set. Also, it means that code in |
| 15 | * this file must not make calls to functions elsewhere in the kernel, or |
| 16 | * else the excepting context will see corruption in its caller-save |
| 17 | * registers. Plus, the entry.S save area is non-reentrant, so this code |
| 18 | * has to run with SR.BL==1, i.e. no interrupts taken inside it and panic |
| 19 | * on any exception. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | * |
Paul Mundt | 811d50c | 2007-11-20 17:01:55 +0900 | [diff] [blame] | 21 | * This file is subject to the terms and conditions of the GNU General Public |
| 22 | * License. See the file "COPYING" in the main directory of this archive |
| 23 | * for more details. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include <linux/signal.h> |
| 26 | #include <linux/sched.h> |
| 27 | #include <linux/kernel.h> |
| 28 | #include <linux/errno.h> |
| 29 | #include <linux/string.h> |
| 30 | #include <linux/types.h> |
| 31 | #include <linux/ptrace.h> |
| 32 | #include <linux/mman.h> |
| 33 | #include <linux/mm.h> |
| 34 | #include <linux/smp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | #include <linux/interrupt.h> |
Paul Mundt | 392c382 | 2012-05-14 17:24:21 +0900 | [diff] [blame^] | 36 | #include <linux/kprobes.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | #include <asm/tlb.h> |
| 38 | #include <asm/io.h> |
| 39 | #include <asm/uaccess.h> |
| 40 | #include <asm/pgalloc.h> |
| 41 | #include <asm/mmu_context.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | |
Paul Mundt | 392c382 | 2012-05-14 17:24:21 +0900 | [diff] [blame^] | 43 | static int handle_tlbmiss(unsigned long long protection_flags, |
Paul Mundt | 811d50c | 2007-11-20 17:01:55 +0900 | [diff] [blame] | 44 | unsigned long address) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | { |
Paul Mundt | 392c382 | 2012-05-14 17:24:21 +0900 | [diff] [blame^] | 46 | pgd_t *pgd; |
Paul Mundt | 811d50c | 2007-11-20 17:01:55 +0900 | [diff] [blame] | 47 | pud_t *pud; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | pmd_t *pmd; |
| 49 | pte_t *pte; |
| 50 | pte_t entry; |
| 51 | |
Paul Mundt | 392c382 | 2012-05-14 17:24:21 +0900 | [diff] [blame^] | 52 | if (is_vmalloc_addr((void *)address)) { |
| 53 | pgd = pgd_offset_k(address); |
| 54 | } else { |
| 55 | if (unlikely(address >= TASK_SIZE || !current->mm)) |
| 56 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | |
Paul Mundt | 392c382 | 2012-05-14 17:24:21 +0900 | [diff] [blame^] | 58 | pgd = pgd_offset(current->mm, address); |
| 59 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | |
Paul Mundt | 392c382 | 2012-05-14 17:24:21 +0900 | [diff] [blame^] | 61 | pud = pud_offset(pgd, address); |
Paul Mundt | 811d50c | 2007-11-20 17:01:55 +0900 | [diff] [blame] | 62 | if (pud_none(*pud) || !pud_present(*pud)) |
Paul Mundt | 4de5185 | 2012-05-14 16:44:45 +0900 | [diff] [blame] | 63 | return 1; |
Paul Mundt | 811d50c | 2007-11-20 17:01:55 +0900 | [diff] [blame] | 64 | |
| 65 | pmd = pmd_offset(pud, address); |
| 66 | if (pmd_none(*pmd) || !pmd_present(*pmd)) |
Paul Mundt | 4de5185 | 2012-05-14 16:44:45 +0900 | [diff] [blame] | 67 | return 1; |
Paul Mundt | 811d50c | 2007-11-20 17:01:55 +0900 | [diff] [blame] | 68 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | pte = pte_offset_kernel(pmd, address); |
| 70 | entry = *pte; |
Paul Mundt | 811d50c | 2007-11-20 17:01:55 +0900 | [diff] [blame] | 71 | if (pte_none(entry) || !pte_present(entry)) |
Paul Mundt | 4de5185 | 2012-05-14 16:44:45 +0900 | [diff] [blame] | 72 | return 1; |
Paul Mundt | 811d50c | 2007-11-20 17:01:55 +0900 | [diff] [blame] | 73 | |
| 74 | /* |
| 75 | * If the page doesn't have sufficient protection bits set to |
| 76 | * service the kind of fault being handled, there's not much |
| 77 | * point doing the TLB refill. Punt the fault to the general |
| 78 | * handler. |
| 79 | */ |
| 80 | if ((pte_val(entry) & protection_flags) != protection_flags) |
Paul Mundt | 4de5185 | 2012-05-14 16:44:45 +0900 | [diff] [blame] | 81 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | |
Paul Mundt | c06fd28 | 2012-05-14 15:52:28 +0900 | [diff] [blame] | 83 | update_mmu_cache(NULL, address, pte); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | |
Paul Mundt | 4de5185 | 2012-05-14 16:44:45 +0900 | [diff] [blame] | 85 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | } |
| 87 | |
Paul Mundt | 811d50c | 2007-11-20 17:01:55 +0900 | [diff] [blame] | 88 | /* |
| 89 | * Put all this information into one structure so that everything is just |
| 90 | * arithmetic relative to a single base address. This reduces the number |
| 91 | * of movi/shori pairs needed just to load addresses of static data. |
| 92 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | struct expevt_lookup { |
| 94 | unsigned short protection_flags[8]; |
| 95 | unsigned char is_text_access[8]; |
| 96 | unsigned char is_write_access[8]; |
| 97 | }; |
| 98 | |
| 99 | #define PRU (1<<9) |
| 100 | #define PRW (1<<8) |
| 101 | #define PRX (1<<7) |
| 102 | #define PRR (1<<6) |
| 103 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | /* Sized as 8 rather than 4 to allow checking the PTE's PRU bit against whether |
| 105 | the fault happened in user mode or privileged mode. */ |
| 106 | static struct expevt_lookup expevt_lookup_table = { |
| 107 | .protection_flags = {PRX, PRX, 0, 0, PRR, PRR, PRW, PRW}, |
| 108 | .is_text_access = {1, 1, 0, 0, 0, 0, 0, 0} |
| 109 | }; |
| 110 | |
| 111 | /* |
| 112 | This routine handles page faults that can be serviced just by refilling a |
| 113 | TLB entry from an existing page table entry. (This case represents a very |
| 114 | large majority of page faults.) Return 1 if the fault was successfully |
| 115 | handled. Return 0 if the fault could not be handled. (This leads into the |
| 116 | general fault handling in fault.c which deals with mapping file-backed |
| 117 | pages, stack growth, segmentation faults, swapping etc etc) |
| 118 | */ |
Paul Mundt | 392c382 | 2012-05-14 17:24:21 +0900 | [diff] [blame^] | 119 | asmlinkage int __kprobes |
| 120 | do_fast_page_fault(unsigned long long ssr_md, unsigned long long expevt, |
| 121 | unsigned long address) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | unsigned long long protection_flags; |
| 124 | unsigned long long index; |
| 125 | unsigned long long expevt4; |
| 126 | |
Paul Mundt | 811d50c | 2007-11-20 17:01:55 +0900 | [diff] [blame] | 127 | /* The next few lines implement a way of hashing EXPEVT into a |
| 128 | * small array index which can be used to lookup parameters |
| 129 | * specific to the type of TLBMISS being handled. |
| 130 | * |
| 131 | * Note: |
| 132 | * ITLBMISS has EXPEVT==0xa40 |
| 133 | * RTLBMISS has EXPEVT==0x040 |
| 134 | * WTLBMISS has EXPEVT==0x060 |
| 135 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | expevt4 = (expevt >> 4); |
Paul Mundt | 811d50c | 2007-11-20 17:01:55 +0900 | [diff] [blame] | 137 | /* TODO : xor ssr_md into this expression too. Then we can check |
| 138 | * that PRU is set when it needs to be. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | index = expevt4 ^ (expevt4 >> 5); |
| 140 | index &= 7; |
Paul Mundt | c06fd28 | 2012-05-14 15:52:28 +0900 | [diff] [blame] | 141 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | protection_flags = expevt_lookup_table.protection_flags[index]; |
Paul Mundt | c06fd28 | 2012-05-14 15:52:28 +0900 | [diff] [blame] | 143 | |
| 144 | if (expevt_lookup_table.is_text_access[index]) |
| 145 | set_thread_fault_code(FAULT_CODE_ITLB); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | |
Paul Mundt | 392c382 | 2012-05-14 17:24:21 +0900 | [diff] [blame^] | 147 | return handle_tlbmiss(protection_flags, address); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | } |