blob: 2b356cec24896ddaf3b37ac0f3dbd2583f45244c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Paul Mundt811d50c2007-11-20 17:01:55 +09002 * The SH64 TLB miss.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
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 Mundt811d50c2007-11-20 17:01:55 +090011 * 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 Torvalds1da177e2005-04-16 15:20:36 -070020 *
Paul Mundt811d50c2007-11-20 17:01:55 +090021 * 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 Torvalds1da177e2005-04-16 15:20:36 -070024 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#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 Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <asm/system.h>
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>
Paul Mundtf15cbe62008-07-29 08:09:44 +090042#include <cpu/registers.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44/* Callable from fault.c, so not static */
45inline void __do_tlb_refill(unsigned long address,
46 unsigned long long is_text_not_data, pte_t *pte)
47{
48 unsigned long long ptel;
49 unsigned long long pteh=0;
50 struct tlb_info *tlbp;
51 unsigned long long next;
52
53 /* Get PTEL first */
54 ptel = pte_val(*pte);
55
56 /*
57 * Set PTEH register
58 */
Paul Mundtc7914832009-08-04 17:14:39 +090059 pteh = neff_sign_extend(address & MMU_VPN_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
61 /* Set the ASID. */
62 pteh |= get_asid() << PTEH_ASID_SHIFT;
63 pteh |= PTEH_VALID;
64
65 /* Set PTEL register, set_pte has performed the sign extension */
66 ptel &= _PAGE_FLAGS_HARDWARE_MASK; /* drop software flags */
67
68 tlbp = is_text_not_data ? &(cpu_data->itlb) : &(cpu_data->dtlb);
69 next = tlbp->next;
70 __flush_tlb_slot(next);
71 asm volatile ("putcfg %0,1,%2\n\n\t"
72 "putcfg %0,0,%1\n"
73 : : "r" (next), "r" (pteh), "r" (ptel) );
74
75 next += TLB_STEP;
76 if (next > tlbp->last) next = tlbp->first;
77 tlbp->next = next;
78
79}
80
Paul Mundt811d50c2007-11-20 17:01:55 +090081static int handle_vmalloc_fault(struct mm_struct *mm,
82 unsigned long protection_flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 unsigned long long textaccess,
84 unsigned long address)
85{
86 pgd_t *dir;
Paul Mundt811d50c2007-11-20 17:01:55 +090087 pud_t *pud;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 pmd_t *pmd;
89 static pte_t *pte;
90 pte_t entry;
91
92 dir = pgd_offset_k(address);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Paul Mundt811d50c2007-11-20 17:01:55 +090094 pud = pud_offset(dir, address);
95 if (pud_none_or_clear_bad(pud))
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Paul Mundt811d50c2007-11-20 17:01:55 +090098 pmd = pmd_offset(pud, address);
99 if (pmd_none_or_clear_bad(pmd))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
102 pte = pte_offset_kernel(pmd, address);
103 entry = *pte;
104
Paul Mundt811d50c2007-11-20 17:01:55 +0900105 if (pte_none(entry) || !pte_present(entry))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 return 0;
Paul Mundt811d50c2007-11-20 17:01:55 +0900107 if ((pte_val(entry) & protection_flags) != protection_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
110 __do_tlb_refill(address, textaccess, pte);
111
112 return 1;
113}
114
Paul Mundt811d50c2007-11-20 17:01:55 +0900115static int handle_tlbmiss(struct mm_struct *mm,
116 unsigned long long protection_flags,
117 unsigned long long textaccess,
118 unsigned long address)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119{
120 pgd_t *dir;
Paul Mundt811d50c2007-11-20 17:01:55 +0900121 pud_t *pud;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 pmd_t *pmd;
123 pte_t *pte;
124 pte_t entry;
125
126 /* NB. The PGD currently only contains a single entry - there is no
127 page table tree stored for the top half of the address space since
128 virtual pages in that region should never be mapped in user mode.
129 (In kernel mode, the only things in that region are the 512Mb super
130 page (locked in), and vmalloc (modules) + I/O device pages (handled
131 by handle_vmalloc_fault), so no PGD for the upper half is required
132 by kernel mode either).
133
134 See how mm->pgd is allocated and initialised in pgd_alloc to see why
135 the next test is necessary. - RPC */
Paul Mundt811d50c2007-11-20 17:01:55 +0900136 if (address >= (unsigned long) TASK_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 /* upper half - never has page table entries. */
138 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Paul Mundt811d50c2007-11-20 17:01:55 +0900140 dir = pgd_offset(mm, address);
141 if (pgd_none(*dir) || !pgd_present(*dir))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 return 0;
Paul Mundt811d50c2007-11-20 17:01:55 +0900143 if (!pgd_present(*dir))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 return 0;
Paul Mundt811d50c2007-11-20 17:01:55 +0900145
146 pud = pud_offset(dir, address);
147 if (pud_none(*pud) || !pud_present(*pud))
148 return 0;
149
150 pmd = pmd_offset(pud, address);
151 if (pmd_none(*pmd) || !pmd_present(*pmd))
152 return 0;
153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 pte = pte_offset_kernel(pmd, address);
155 entry = *pte;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
Paul Mundt811d50c2007-11-20 17:01:55 +0900157 if (pte_none(entry) || !pte_present(entry))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 return 0;
Paul Mundt811d50c2007-11-20 17:01:55 +0900159
160 /*
161 * If the page doesn't have sufficient protection bits set to
162 * service the kind of fault being handled, there's not much
163 * point doing the TLB refill. Punt the fault to the general
164 * handler.
165 */
166 if ((pte_val(entry) & protection_flags) != protection_flags)
167 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
169 __do_tlb_refill(address, textaccess, pte);
170
171 return 1;
172}
173
Paul Mundt811d50c2007-11-20 17:01:55 +0900174/*
175 * Put all this information into one structure so that everything is just
176 * arithmetic relative to a single base address. This reduces the number
177 * of movi/shori pairs needed just to load addresses of static data.
178 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179struct expevt_lookup {
180 unsigned short protection_flags[8];
181 unsigned char is_text_access[8];
182 unsigned char is_write_access[8];
183};
184
185#define PRU (1<<9)
186#define PRW (1<<8)
187#define PRX (1<<7)
188#define PRR (1<<6)
189
190#define DIRTY (_PAGE_DIRTY | _PAGE_ACCESSED)
191#define YOUNG (_PAGE_ACCESSED)
192
193/* Sized as 8 rather than 4 to allow checking the PTE's PRU bit against whether
194 the fault happened in user mode or privileged mode. */
195static struct expevt_lookup expevt_lookup_table = {
196 .protection_flags = {PRX, PRX, 0, 0, PRR, PRR, PRW, PRW},
197 .is_text_access = {1, 1, 0, 0, 0, 0, 0, 0}
198};
199
200/*
201 This routine handles page faults that can be serviced just by refilling a
202 TLB entry from an existing page table entry. (This case represents a very
203 large majority of page faults.) Return 1 if the fault was successfully
204 handled. Return 0 if the fault could not be handled. (This leads into the
205 general fault handling in fault.c which deals with mapping file-backed
206 pages, stack growth, segmentation faults, swapping etc etc)
207 */
Paul Mundt811d50c2007-11-20 17:01:55 +0900208asmlinkage int do_fast_page_fault(unsigned long long ssr_md,
209 unsigned long long expevt,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 unsigned long address)
211{
212 struct task_struct *tsk;
213 struct mm_struct *mm;
214 unsigned long long textaccess;
215 unsigned long long protection_flags;
216 unsigned long long index;
217 unsigned long long expevt4;
218
Paul Mundt811d50c2007-11-20 17:01:55 +0900219 /* The next few lines implement a way of hashing EXPEVT into a
220 * small array index which can be used to lookup parameters
221 * specific to the type of TLBMISS being handled.
222 *
223 * Note:
224 * ITLBMISS has EXPEVT==0xa40
225 * RTLBMISS has EXPEVT==0x040
226 * WTLBMISS has EXPEVT==0x060
227 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 expevt4 = (expevt >> 4);
Paul Mundt811d50c2007-11-20 17:01:55 +0900229 /* TODO : xor ssr_md into this expression too. Then we can check
230 * that PRU is set when it needs to be. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 index = expevt4 ^ (expevt4 >> 5);
232 index &= 7;
233 protection_flags = expevt_lookup_table.protection_flags[index];
234 textaccess = expevt_lookup_table.is_text_access[index];
235
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 /* SIM
237 * Note this is now called with interrupts still disabled
238 * This is to cope with being called for a missing IO port
Simon Arlott0a354772007-05-14 08:25:48 +0900239 * address with interrupts disabled. This should be fixed as
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 * soon as we have a better 'fast path' miss handler.
241 *
242 * Plus take care how you try and debug this stuff.
243 * For example, writing debug data to a port which you
244 * have just faulted on is not going to work.
245 */
246
247 tsk = current;
248 mm = tsk->mm;
249
250 if ((address >= VMALLOC_START && address < VMALLOC_END) ||
251 (address >= IOBASE_VADDR && address < IOBASE_END)) {
Paul Mundt811d50c2007-11-20 17:01:55 +0900252 if (ssr_md)
253 /*
254 * Process-contexts can never have this address
255 * range mapped
256 */
257 if (handle_vmalloc_fault(mm, protection_flags,
258 textaccess, address))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 } else if (!in_interrupt() && mm) {
Paul Mundt811d50c2007-11-20 17:01:55 +0900261 if (handle_tlbmiss(mm, protection_flags, textaccess, address))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 }
264
265 return 0;
266}