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.
4 * Copyright (C) 1996 David S. Miller (davem@caip.rutgers.edu)
5 * Copyright (C) 1997, 1999 Jakub Jelinek (jj@ultra.linux.cz)
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>
17 #include <linux/module.h>
18 #include <linux/smp_lock.h>
19 #include <linux/init.h>
20 #include <linux/interrupt.h>
23 #include <asm/pgtable.h>
24 #include <asm/openprom.h>
25 #include <asm/oplib.h>
26 #include <asm/uaccess.h>
29 #include <asm/sections.h>
30 #include <asm/kdebug.h>
32 #define ELEMENTS(arr) (sizeof (arr)/sizeof (arr[0]))
34 extern struct sparc_phys_banks sp_banks
[SPARC_PHYS_BANKS
];
37 * To debug kernel during syscall entry.
39 void syscall_trace_entry(struct pt_regs
*regs
)
41 printk("scall entry: %s[%d]/cpu%d: %d\n", current
->comm
, current
->pid
, smp_processor_id(), (int) regs
->u_regs
[UREG_G1
]);
45 * To debug kernel during syscall exit.
47 void syscall_trace_exit(struct pt_regs
*regs
)
49 printk("scall exit: %s[%d]/cpu%d: %d\n", current
->comm
, current
->pid
, smp_processor_id(), (int) regs
->u_regs
[UREG_G1
]);
53 * To debug kernel to catch accesses to certain virtual/physical addresses.
54 * Mode = 0 selects physical watchpoints, mode = 1 selects virtual watchpoints.
55 * flags = VM_READ watches memread accesses, flags = VM_WRITE watches memwrite accesses.
56 * Caller passes in a 64bit aligned addr, with mask set to the bytes that need to be
57 * watched. This is only useful on a single cpu machine for now. After the watchpoint
58 * is detected, the process causing it will be killed, thus preventing an infinite loop.
60 void set_brkpt(unsigned long addr
, unsigned char mask
, int flags
, int mode
)
62 unsigned long lsubits
;
64 __asm__
__volatile__("ldxa [%%g0] %1, %0"
66 : "i" (ASI_LSU_CONTROL
));
67 lsubits
&= ~(LSU_CONTROL_PM
| LSU_CONTROL_VM
|
68 LSU_CONTROL_PR
| LSU_CONTROL_VR
|
69 LSU_CONTROL_PW
| LSU_CONTROL_VW
);
71 __asm__
__volatile__("stxa %0, [%1] %2\n\t"
74 : "r" (addr
), "r" (mode
? VIRT_WATCHPOINT
: PHYS_WATCHPOINT
),
77 lsubits
|= ((unsigned long)mask
<< (mode
? 25 : 33));
79 lsubits
|= (mode
? LSU_CONTROL_VR
: LSU_CONTROL_PR
);
81 lsubits
|= (mode
? LSU_CONTROL_VW
: LSU_CONTROL_PW
);
82 __asm__
__volatile__("stxa %0, [%%g0] %1\n\t"
85 : "r" (lsubits
), "i" (ASI_LSU_CONTROL
)
89 /* Nice, simple, prom library does all the sweating for us. ;) */
90 unsigned long __init
prom_probe_memory (void)
92 register struct linux_mlist_p1275
*mlist
;
93 register unsigned long bytes
, base_paddr
, tally
;
97 mlist
= *prom_meminfo()->p1275_available
;
98 bytes
= tally
= mlist
->num_bytes
;
99 base_paddr
= mlist
->start_adr
;
101 sp_banks
[0].base_addr
= base_paddr
;
102 sp_banks
[0].num_bytes
= bytes
;
104 while (mlist
->theres_more
!= (void *) 0) {
106 mlist
= mlist
->theres_more
;
107 bytes
= mlist
->num_bytes
;
109 if (i
>= SPARC_PHYS_BANKS
-1) {
110 printk ("The machine has more banks than "
111 "this kernel can support\n"
112 "Increase the SPARC_PHYS_BANKS "
113 "setting (currently %d)\n",
115 i
= SPARC_PHYS_BANKS
-1;
119 sp_banks
[i
].base_addr
= mlist
->start_adr
;
120 sp_banks
[i
].num_bytes
= mlist
->num_bytes
;
124 sp_banks
[i
].base_addr
= 0xdeadbeefbeefdeadUL
;
125 sp_banks
[i
].num_bytes
= 0;
127 /* Now mask all bank sizes on a page boundary, it is all we can
130 for (i
= 0; sp_banks
[i
].num_bytes
!= 0; i
++)
131 sp_banks
[i
].num_bytes
&= PAGE_MASK
;
136 static void unhandled_fault(unsigned long address
, struct task_struct
*tsk
,
137 struct pt_regs
*regs
)
139 if ((unsigned long) address
< PAGE_SIZE
) {
140 printk(KERN_ALERT
"Unable to handle kernel NULL "
141 "pointer dereference\n");
143 printk(KERN_ALERT
"Unable to handle kernel paging request "
144 "at virtual address %016lx\n", (unsigned long)address
);
146 printk(KERN_ALERT
"tsk->{mm,active_mm}->context = %016lx\n",
148 CTX_HWBITS(tsk
->mm
->context
) :
149 CTX_HWBITS(tsk
->active_mm
->context
)));
150 printk(KERN_ALERT
"tsk->{mm,active_mm}->pgd = %016lx\n",
151 (tsk
->mm
? (unsigned long) tsk
->mm
->pgd
:
152 (unsigned long) tsk
->active_mm
->pgd
));
153 if (notify_die(DIE_GPF
, "general protection fault", regs
,
154 0, 0, SIGSEGV
) == NOTIFY_STOP
)
156 die_if_kernel("Oops", regs
);
159 static void bad_kernel_pc(struct pt_regs
*regs
)
163 printk(KERN_CRIT
"OOPS: Bogus kernel PC [%016lx] in fault handler\n",
165 __asm__("mov %%sp, %0" : "=r" (ksp
));
166 show_stack(current
, ksp
);
167 unhandled_fault(regs
->tpc
, current
, regs
);
171 * We now make sure that mmap_sem is held in all paths that call
172 * this. Additionally, to prevent kswapd from ripping ptes from
173 * under us, raise interrupts around the time that we look at the
174 * pte, kswapd will have to wait to get his smp ipi response from
175 * us. This saves us having to get page_table_lock.
177 static unsigned int get_user_insn(unsigned long tpc
)
179 pgd_t
*pgdp
= pgd_offset(current
->mm
, tpc
);
185 unsigned long pstate
;
189 pudp
= pud_offset(pgdp
, tpc
);
192 pmdp
= pmd_offset(pudp
, tpc
);
196 /* This disables preemption for us as well. */
197 __asm__
__volatile__("rdpr %%pstate, %0" : "=r" (pstate
));
198 __asm__
__volatile__("wrpr %0, %1, %%pstate"
199 : : "r" (pstate
), "i" (PSTATE_IE
));
200 ptep
= pte_offset_map(pmdp
, tpc
);
202 if (!pte_present(pte
))
205 pa
= (pte_val(pte
) & _PAGE_PADDR
);
206 pa
+= (tpc
& ~PAGE_MASK
);
208 /* Use phys bypass so we don't pollute dtlb/dcache. */
209 __asm__
__volatile__("lduwa [%1] %2, %0"
211 : "r" (pa
), "i" (ASI_PHYS_USE_EC
));
215 __asm__
__volatile__("wrpr %0, 0x0, %%pstate" : : "r" (pstate
));
220 extern unsigned long compute_effective_address(struct pt_regs
*, unsigned int, unsigned int);
222 static void do_fault_siginfo(int code
, int sig
, struct pt_regs
*regs
,
223 unsigned int insn
, int fault_code
)
230 if (fault_code
& FAULT_CODE_ITLB
)
231 info
.si_addr
= (void __user
*) regs
->tpc
;
233 info
.si_addr
= (void __user
*)
234 compute_effective_address(regs
, insn
, 0);
236 force_sig_info(sig
, &info
, current
);
239 extern int handle_ldf_stq(u32
, struct pt_regs
*);
240 extern int handle_ld_nf(u32
, struct pt_regs
*);
242 static unsigned int get_fault_insn(struct pt_regs
*regs
, unsigned int insn
)
245 if (!regs
->tpc
|| (regs
->tpc
& 0x3))
247 if (regs
->tstate
& TSTATE_PRIV
) {
248 insn
= *(unsigned int *) regs
->tpc
;
250 insn
= get_user_insn(regs
->tpc
);
256 static void do_kernel_fault(struct pt_regs
*regs
, int si_code
, int fault_code
,
257 unsigned int insn
, unsigned long address
)
260 unsigned char asi
= ASI_P
;
262 if ((!insn
) && (regs
->tstate
& TSTATE_PRIV
))
265 /* If user insn could be read (thus insn is zero), that
266 * is fine. We will just gun down the process with a signal
270 if (!(fault_code
& (FAULT_CODE_WRITE
|FAULT_CODE_ITLB
)) &&
271 (insn
& 0xc0800000) == 0xc0800000) {
273 asi
= (regs
->tstate
>> 24);
276 if ((asi
& 0xf2) == 0x82) {
277 if (insn
& 0x1000000) {
278 handle_ldf_stq(insn
, regs
);
280 /* This was a non-faulting load. Just clear the
281 * destination register(s) and continue with the next
284 handle_ld_nf(insn
, regs
);
290 g2
= regs
->u_regs
[UREG_G2
];
292 /* Is this in ex_table? */
293 if (regs
->tstate
& TSTATE_PRIV
) {
296 if (asi
== ASI_P
&& (insn
& 0xc0800000) == 0xc0800000) {
298 asi
= (regs
->tstate
>> 24);
303 /* Look in asi.h: All _S asis have LS bit set */
305 (fixup
= search_extables_range(regs
->tpc
, &g2
))) {
307 regs
->tnpc
= regs
->tpc
+ 4;
308 regs
->u_regs
[UREG_G2
] = g2
;
312 /* The si_code was set to make clear whether
313 * this was a SEGV_MAPERR or SEGV_ACCERR fault.
315 do_fault_siginfo(si_code
, SIGSEGV
, regs
, insn
, fault_code
);
320 unhandled_fault (address
, current
, regs
);
323 asmlinkage
void do_sparc64_fault(struct pt_regs
*regs
)
325 struct mm_struct
*mm
= current
->mm
;
326 struct vm_area_struct
*vma
;
327 unsigned int insn
= 0;
328 int si_code
, fault_code
;
329 unsigned long address
;
331 fault_code
= get_thread_fault_code();
333 if (notify_die(DIE_PAGE_FAULT
, "page_fault", regs
,
334 fault_code
, 0, SIGSEGV
) == NOTIFY_STOP
)
337 si_code
= SEGV_MAPERR
;
338 address
= current_thread_info()->fault_address
;
340 if ((fault_code
& FAULT_CODE_ITLB
) &&
341 (fault_code
& FAULT_CODE_DTLB
))
344 if (regs
->tstate
& TSTATE_PRIV
) {
345 unsigned long tpc
= regs
->tpc
;
347 /* Sanity check the PC. */
348 if ((tpc
>= KERNBASE
&& tpc
< (unsigned long) _etext
) ||
349 (tpc
>= MODULES_VADDR
&& tpc
< MODULES_END
)) {
350 /* Valid, no problems... */
358 * If we're in an interrupt or have no user
359 * context, we must not take the fault..
361 if (in_atomic() || !mm
)
364 if (test_thread_flag(TIF_32BIT
)) {
365 if (!(regs
->tstate
& TSTATE_PRIV
))
366 regs
->tpc
&= 0xffffffff;
367 address
&= 0xffffffff;
370 if (!down_read_trylock(&mm
->mmap_sem
)) {
371 if ((regs
->tstate
& TSTATE_PRIV
) &&
372 !search_exception_tables(regs
->tpc
)) {
373 insn
= get_fault_insn(regs
, insn
);
374 goto handle_kernel_fault
;
376 down_read(&mm
->mmap_sem
);
379 vma
= find_vma(mm
, address
);
383 /* Pure DTLB misses do not tell us whether the fault causing
384 * load/store/atomic was a write or not, it only says that there
385 * was no match. So in such a case we (carefully) read the
386 * instruction to try and figure this out. It's an optimization
387 * so it's ok if we can't do this.
389 * Special hack, window spill/fill knows the exact fault type.
392 (FAULT_CODE_DTLB
| FAULT_CODE_WRITE
| FAULT_CODE_WINFIXUP
)) == FAULT_CODE_DTLB
) &&
393 (vma
->vm_flags
& VM_WRITE
) != 0) {
394 insn
= get_fault_insn(regs
, 0);
397 if ((insn
& 0xc0200000) == 0xc0200000 &&
398 (insn
& 0x1780000) != 0x1680000) {
399 /* Don't bother updating thread struct value,
400 * because update_mmu_cache only cares which tlb
401 * the access came from.
403 fault_code
|= FAULT_CODE_WRITE
;
408 if (vma
->vm_start
<= address
)
410 if (!(vma
->vm_flags
& VM_GROWSDOWN
))
412 if (!(fault_code
& FAULT_CODE_WRITE
)) {
413 /* Non-faulting loads shouldn't expand stack. */
414 insn
= get_fault_insn(regs
, insn
);
415 if ((insn
& 0xc0800000) == 0xc0800000) {
419 asi
= (regs
->tstate
>> 24);
422 if ((asi
& 0xf2) == 0x82)
426 if (expand_stack(vma
, address
))
429 * Ok, we have a good vm_area for this memory access, so
433 si_code
= SEGV_ACCERR
;
435 /* If we took a ITLB miss on a non-executable page, catch
438 if ((fault_code
& FAULT_CODE_ITLB
) && !(vma
->vm_flags
& VM_EXEC
)) {
439 BUG_ON(address
!= regs
->tpc
);
440 BUG_ON(regs
->tstate
& TSTATE_PRIV
);
444 if (fault_code
& FAULT_CODE_WRITE
) {
445 if (!(vma
->vm_flags
& VM_WRITE
))
448 /* Spitfire has an icache which does not snoop
449 * processor stores. Later processors do...
451 if (tlb_type
== spitfire
&&
452 (vma
->vm_flags
& VM_EXEC
) != 0 &&
453 vma
->vm_file
!= NULL
)
454 set_thread_fault_code(fault_code
|
455 FAULT_CODE_BLKCOMMIT
);
457 /* Allow reads even for write-only mappings */
458 if (!(vma
->vm_flags
& (VM_READ
| VM_EXEC
)))
462 switch (handle_mm_fault(mm
, vma
, address
, (fault_code
& FAULT_CODE_WRITE
))) {
469 case VM_FAULT_SIGBUS
:
477 up_read(&mm
->mmap_sem
);
481 * Something tried to access memory that isn't in our memory map..
482 * Fix it, but check if it's kernel or user first..
485 insn
= get_fault_insn(regs
, insn
);
486 up_read(&mm
->mmap_sem
);
489 do_kernel_fault(regs
, si_code
, fault_code
, insn
, address
);
494 * We ran out of memory, or some other thing happened to us that made
495 * us unable to handle the page fault gracefully.
498 insn
= get_fault_insn(regs
, insn
);
499 up_read(&mm
->mmap_sem
);
500 printk("VM: killing process %s\n", current
->comm
);
501 if (!(regs
->tstate
& TSTATE_PRIV
))
503 goto handle_kernel_fault
;
506 insn
= get_fault_insn(regs
, 0);
507 goto handle_kernel_fault
;
510 insn
= get_fault_insn(regs
, insn
);
511 up_read(&mm
->mmap_sem
);
514 * Send a sigbus, regardless of whether we were in kernel
517 do_fault_siginfo(BUS_ADRERR
, SIGBUS
, regs
, insn
, fault_code
);
519 /* Kernel mode? Handle exceptions or die */
520 if (regs
->tstate
& TSTATE_PRIV
)
521 goto handle_kernel_fault
;
524 /* These values are no longer needed, clear them. */
525 set_thread_fault_code(0);
526 current_thread_info()->fault_address
= 0;