[SPARC64]: Implement rest of generic interrupt hypervisor calls.
[deliverable/linux.git] / arch / sparc64 / mm / fault.c
CommitLineData
1da177e4
LT
1/* $Id: fault.c,v 1.59 2002/02/09 19:49:31 davem Exp $
2 * arch/sparc64/mm/fault.c: Page fault handlers for the 64-bit Sparc.
3 *
4 * Copyright (C) 1996 David S. Miller (davem@caip.rutgers.edu)
5 * Copyright (C) 1997, 1999 Jakub Jelinek (jj@ultra.linux.cz)
6 */
7
8#include <asm/head.h>
9
10#include <linux/string.h>
11#include <linux/types.h>
12#include <linux/sched.h>
13#include <linux/ptrace.h>
14#include <linux/mman.h>
15#include <linux/signal.h>
16#include <linux/mm.h>
17#include <linux/module.h>
18#include <linux/smp_lock.h>
19#include <linux/init.h>
20#include <linux/interrupt.h>
05e14cb3 21#include <linux/kprobes.h>
1da177e4
LT
22
23#include <asm/page.h>
24#include <asm/pgtable.h>
25#include <asm/openprom.h>
26#include <asm/oplib.h>
27#include <asm/uaccess.h>
28#include <asm/asi.h>
29#include <asm/lsu.h>
30#include <asm/sections.h>
31#include <asm/kdebug.h>
32
1da177e4
LT
33/*
34 * To debug kernel to catch accesses to certain virtual/physical addresses.
35 * Mode = 0 selects physical watchpoints, mode = 1 selects virtual watchpoints.
36 * flags = VM_READ watches memread accesses, flags = VM_WRITE watches memwrite accesses.
37 * Caller passes in a 64bit aligned addr, with mask set to the bytes that need to be
38 * watched. This is only useful on a single cpu machine for now. After the watchpoint
39 * is detected, the process causing it will be killed, thus preventing an infinite loop.
40 */
41void set_brkpt(unsigned long addr, unsigned char mask, int flags, int mode)
42{
43 unsigned long lsubits;
44
45 __asm__ __volatile__("ldxa [%%g0] %1, %0"
46 : "=r" (lsubits)
47 : "i" (ASI_LSU_CONTROL));
48 lsubits &= ~(LSU_CONTROL_PM | LSU_CONTROL_VM |
49 LSU_CONTROL_PR | LSU_CONTROL_VR |
50 LSU_CONTROL_PW | LSU_CONTROL_VW);
51
52 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
53 "membar #Sync"
54 : /* no outputs */
55 : "r" (addr), "r" (mode ? VIRT_WATCHPOINT : PHYS_WATCHPOINT),
56 "i" (ASI_DMMU));
57
58 lsubits |= ((unsigned long)mask << (mode ? 25 : 33));
59 if (flags & VM_READ)
60 lsubits |= (mode ? LSU_CONTROL_VR : LSU_CONTROL_PR);
61 if (flags & VM_WRITE)
62 lsubits |= (mode ? LSU_CONTROL_VW : LSU_CONTROL_PW);
63 __asm__ __volatile__("stxa %0, [%%g0] %1\n\t"
64 "membar #Sync"
65 : /* no outputs */
66 : "r" (lsubits), "i" (ASI_LSU_CONTROL)
67 : "memory");
68}
69
05e14cb3
PP
70static void __kprobes unhandled_fault(unsigned long address,
71 struct task_struct *tsk,
72 struct pt_regs *regs)
1da177e4
LT
73{
74 if ((unsigned long) address < PAGE_SIZE) {
75 printk(KERN_ALERT "Unable to handle kernel NULL "
76 "pointer dereference\n");
77 } else {
78 printk(KERN_ALERT "Unable to handle kernel paging request "
79 "at virtual address %016lx\n", (unsigned long)address);
80 }
81 printk(KERN_ALERT "tsk->{mm,active_mm}->context = %016lx\n",
82 (tsk->mm ?
83 CTX_HWBITS(tsk->mm->context) :
84 CTX_HWBITS(tsk->active_mm->context)));
85 printk(KERN_ALERT "tsk->{mm,active_mm}->pgd = %016lx\n",
86 (tsk->mm ? (unsigned long) tsk->mm->pgd :
87 (unsigned long) tsk->active_mm->pgd));
88 if (notify_die(DIE_GPF, "general protection fault", regs,
89 0, 0, SIGSEGV) == NOTIFY_STOP)
90 return;
91 die_if_kernel("Oops", regs);
92}
93
94static void bad_kernel_pc(struct pt_regs *regs)
95{
96 unsigned long *ksp;
97
98 printk(KERN_CRIT "OOPS: Bogus kernel PC [%016lx] in fault handler\n",
99 regs->tpc);
100 __asm__("mov %%sp, %0" : "=r" (ksp));
101 show_stack(current, ksp);
102 unhandled_fault(regs->tpc, current, regs);
103}
104
105/*
106 * We now make sure that mmap_sem is held in all paths that call
107 * this. Additionally, to prevent kswapd from ripping ptes from
108 * under us, raise interrupts around the time that we look at the
109 * pte, kswapd will have to wait to get his smp ipi response from
da160546 110 * us. vmtruncate likewise. This saves us having to get pte lock.
1da177e4
LT
111 */
112static unsigned int get_user_insn(unsigned long tpc)
113{
114 pgd_t *pgdp = pgd_offset(current->mm, tpc);
115 pud_t *pudp;
116 pmd_t *pmdp;
117 pte_t *ptep, pte;
118 unsigned long pa;
119 u32 insn = 0;
120 unsigned long pstate;
121
122 if (pgd_none(*pgdp))
123 goto outret;
124 pudp = pud_offset(pgdp, tpc);
125 if (pud_none(*pudp))
126 goto outret;
127 pmdp = pmd_offset(pudp, tpc);
128 if (pmd_none(*pmdp))
129 goto outret;
130
131 /* This disables preemption for us as well. */
132 __asm__ __volatile__("rdpr %%pstate, %0" : "=r" (pstate));
133 __asm__ __volatile__("wrpr %0, %1, %%pstate"
134 : : "r" (pstate), "i" (PSTATE_IE));
135 ptep = pte_offset_map(pmdp, tpc);
136 pte = *ptep;
137 if (!pte_present(pte))
138 goto out;
139
c4bce90e 140 pa = (pte_pfn(pte) << PAGE_SHIFT);
1da177e4
LT
141 pa += (tpc & ~PAGE_MASK);
142
143 /* Use phys bypass so we don't pollute dtlb/dcache. */
144 __asm__ __volatile__("lduwa [%1] %2, %0"
145 : "=r" (insn)
146 : "r" (pa), "i" (ASI_PHYS_USE_EC));
147
148out:
149 pte_unmap(ptep);
150 __asm__ __volatile__("wrpr %0, 0x0, %%pstate" : : "r" (pstate));
151outret:
152 return insn;
153}
154
155extern unsigned long compute_effective_address(struct pt_regs *, unsigned int, unsigned int);
156
157static void do_fault_siginfo(int code, int sig, struct pt_regs *regs,
158 unsigned int insn, int fault_code)
159{
160 siginfo_t info;
161
162 info.si_code = code;
163 info.si_signo = sig;
164 info.si_errno = 0;
165 if (fault_code & FAULT_CODE_ITLB)
166 info.si_addr = (void __user *) regs->tpc;
167 else
168 info.si_addr = (void __user *)
169 compute_effective_address(regs, insn, 0);
170 info.si_trapno = 0;
171 force_sig_info(sig, &info, current);
172}
173
174extern int handle_ldf_stq(u32, struct pt_regs *);
175extern int handle_ld_nf(u32, struct pt_regs *);
176
177static unsigned int get_fault_insn(struct pt_regs *regs, unsigned int insn)
178{
179 if (!insn) {
180 if (!regs->tpc || (regs->tpc & 0x3))
181 return 0;
182 if (regs->tstate & TSTATE_PRIV) {
183 insn = *(unsigned int *) regs->tpc;
184 } else {
185 insn = get_user_insn(regs->tpc);
186 }
187 }
188 return insn;
189}
190
191static void do_kernel_fault(struct pt_regs *regs, int si_code, int fault_code,
192 unsigned int insn, unsigned long address)
193{
1da177e4
LT
194 unsigned char asi = ASI_P;
195
196 if ((!insn) && (regs->tstate & TSTATE_PRIV))
197 goto cannot_handle;
198
199 /* If user insn could be read (thus insn is zero), that
200 * is fine. We will just gun down the process with a signal
201 * in that case.
202 */
203
204 if (!(fault_code & (FAULT_CODE_WRITE|FAULT_CODE_ITLB)) &&
205 (insn & 0xc0800000) == 0xc0800000) {
206 if (insn & 0x2000)
207 asi = (regs->tstate >> 24);
208 else
209 asi = (insn >> 5);
210 if ((asi & 0xf2) == 0x82) {
211 if (insn & 0x1000000) {
212 handle_ldf_stq(insn, regs);
213 } else {
214 /* This was a non-faulting load. Just clear the
215 * destination register(s) and continue with the next
216 * instruction. -jj
217 */
218 handle_ld_nf(insn, regs);
219 }
220 return;
221 }
222 }
223
1da177e4
LT
224 /* Is this in ex_table? */
225 if (regs->tstate & TSTATE_PRIV) {
8cf14af0 226 const struct exception_table_entry *entry;
1da177e4
LT
227
228 if (asi == ASI_P && (insn & 0xc0800000) == 0xc0800000) {
229 if (insn & 0x2000)
230 asi = (regs->tstate >> 24);
231 else
232 asi = (insn >> 5);
233 }
234
235 /* Look in asi.h: All _S asis have LS bit set */
236 if ((asi & 0x1) &&
8cf14af0
DM
237 (entry = search_exception_tables(regs->tpc))) {
238 regs->tpc = entry->fixup;
1da177e4 239 regs->tnpc = regs->tpc + 4;
1da177e4
LT
240 return;
241 }
242 } else {
243 /* The si_code was set to make clear whether
244 * this was a SEGV_MAPERR or SEGV_ACCERR fault.
245 */
246 do_fault_siginfo(si_code, SIGSEGV, regs, insn, fault_code);
247 return;
248 }
249
250cannot_handle:
251 unhandled_fault (address, current, regs);
252}
253
05e14cb3 254asmlinkage void __kprobes do_sparc64_fault(struct pt_regs *regs)
1da177e4
LT
255{
256 struct mm_struct *mm = current->mm;
257 struct vm_area_struct *vma;
258 unsigned int insn = 0;
259 int si_code, fault_code;
260 unsigned long address;
261
262 fault_code = get_thread_fault_code();
263
264 if (notify_die(DIE_PAGE_FAULT, "page_fault", regs,
265 fault_code, 0, SIGSEGV) == NOTIFY_STOP)
266 return;
267
268 si_code = SEGV_MAPERR;
269 address = current_thread_info()->fault_address;
270
271 if ((fault_code & FAULT_CODE_ITLB) &&
272 (fault_code & FAULT_CODE_DTLB))
273 BUG();
274
275 if (regs->tstate & TSTATE_PRIV) {
276 unsigned long tpc = regs->tpc;
277
278 /* Sanity check the PC. */
279 if ((tpc >= KERNBASE && tpc < (unsigned long) _etext) ||
280 (tpc >= MODULES_VADDR && tpc < MODULES_END)) {
281 /* Valid, no problems... */
282 } else {
283 bad_kernel_pc(regs);
284 return;
285 }
286 }
287
288 /*
289 * If we're in an interrupt or have no user
290 * context, we must not take the fault..
291 */
292 if (in_atomic() || !mm)
293 goto intr_or_no_mm;
294
295 if (test_thread_flag(TIF_32BIT)) {
296 if (!(regs->tstate & TSTATE_PRIV))
297 regs->tpc &= 0xffffffff;
298 address &= 0xffffffff;
299 }
300
301 if (!down_read_trylock(&mm->mmap_sem)) {
302 if ((regs->tstate & TSTATE_PRIV) &&
303 !search_exception_tables(regs->tpc)) {
304 insn = get_fault_insn(regs, insn);
305 goto handle_kernel_fault;
306 }
307 down_read(&mm->mmap_sem);
308 }
309
310 vma = find_vma(mm, address);
311 if (!vma)
312 goto bad_area;
313
314 /* Pure DTLB misses do not tell us whether the fault causing
315 * load/store/atomic was a write or not, it only says that there
316 * was no match. So in such a case we (carefully) read the
317 * instruction to try and figure this out. It's an optimization
318 * so it's ok if we can't do this.
319 *
320 * Special hack, window spill/fill knows the exact fault type.
321 */
322 if (((fault_code &
323 (FAULT_CODE_DTLB | FAULT_CODE_WRITE | FAULT_CODE_WINFIXUP)) == FAULT_CODE_DTLB) &&
324 (vma->vm_flags & VM_WRITE) != 0) {
325 insn = get_fault_insn(regs, 0);
326 if (!insn)
327 goto continue_fault;
328 if ((insn & 0xc0200000) == 0xc0200000 &&
329 (insn & 0x1780000) != 0x1680000) {
330 /* Don't bother updating thread struct value,
331 * because update_mmu_cache only cares which tlb
332 * the access came from.
333 */
334 fault_code |= FAULT_CODE_WRITE;
335 }
336 }
337continue_fault:
338
339 if (vma->vm_start <= address)
340 goto good_area;
341 if (!(vma->vm_flags & VM_GROWSDOWN))
342 goto bad_area;
343 if (!(fault_code & FAULT_CODE_WRITE)) {
344 /* Non-faulting loads shouldn't expand stack. */
345 insn = get_fault_insn(regs, insn);
346 if ((insn & 0xc0800000) == 0xc0800000) {
347 unsigned char asi;
348
349 if (insn & 0x2000)
350 asi = (regs->tstate >> 24);
351 else
352 asi = (insn >> 5);
353 if ((asi & 0xf2) == 0x82)
354 goto bad_area;
355 }
356 }
357 if (expand_stack(vma, address))
358 goto bad_area;
359 /*
360 * Ok, we have a good vm_area for this memory access, so
361 * we can handle it..
362 */
363good_area:
364 si_code = SEGV_ACCERR;
365
366 /* If we took a ITLB miss on a non-executable page, catch
367 * that here.
368 */
369 if ((fault_code & FAULT_CODE_ITLB) && !(vma->vm_flags & VM_EXEC)) {
370 BUG_ON(address != regs->tpc);
371 BUG_ON(regs->tstate & TSTATE_PRIV);
372 goto bad_area;
373 }
374
375 if (fault_code & FAULT_CODE_WRITE) {
376 if (!(vma->vm_flags & VM_WRITE))
377 goto bad_area;
378
379 /* Spitfire has an icache which does not snoop
380 * processor stores. Later processors do...
381 */
382 if (tlb_type == spitfire &&
383 (vma->vm_flags & VM_EXEC) != 0 &&
384 vma->vm_file != NULL)
385 set_thread_fault_code(fault_code |
386 FAULT_CODE_BLKCOMMIT);
387 } else {
388 /* Allow reads even for write-only mappings */
389 if (!(vma->vm_flags & (VM_READ | VM_EXEC)))
390 goto bad_area;
391 }
392
393 switch (handle_mm_fault(mm, vma, address, (fault_code & FAULT_CODE_WRITE))) {
394 case VM_FAULT_MINOR:
395 current->min_flt++;
396 break;
397 case VM_FAULT_MAJOR:
398 current->maj_flt++;
399 break;
400 case VM_FAULT_SIGBUS:
401 goto do_sigbus;
402 case VM_FAULT_OOM:
403 goto out_of_memory;
404 default:
405 BUG();
406 }
407
408 up_read(&mm->mmap_sem);
efdc1e20 409 return;
1da177e4
LT
410
411 /*
412 * Something tried to access memory that isn't in our memory map..
413 * Fix it, but check if it's kernel or user first..
414 */
415bad_area:
416 insn = get_fault_insn(regs, insn);
417 up_read(&mm->mmap_sem);
418
419handle_kernel_fault:
420 do_kernel_fault(regs, si_code, fault_code, insn, address);
efdc1e20 421 return;
1da177e4
LT
422
423/*
424 * We ran out of memory, or some other thing happened to us that made
425 * us unable to handle the page fault gracefully.
426 */
427out_of_memory:
428 insn = get_fault_insn(regs, insn);
429 up_read(&mm->mmap_sem);
430 printk("VM: killing process %s\n", current->comm);
431 if (!(regs->tstate & TSTATE_PRIV))
432 do_exit(SIGKILL);
433 goto handle_kernel_fault;
434
435intr_or_no_mm:
436 insn = get_fault_insn(regs, 0);
437 goto handle_kernel_fault;
438
439do_sigbus:
440 insn = get_fault_insn(regs, insn);
441 up_read(&mm->mmap_sem);
442
443 /*
444 * Send a sigbus, regardless of whether we were in kernel
445 * or user mode.
446 */
447 do_fault_siginfo(BUS_ADRERR, SIGBUS, regs, insn, fault_code);
448
449 /* Kernel mode? Handle exceptions or die */
450 if (regs->tstate & TSTATE_PRIV)
451 goto handle_kernel_fault;
1da177e4 452}
This page took 0.10941 seconds and 5 git commands to generate.