File: /usr/src/linux/arch/sparc64/mm/fault.c
1 /* $Id: fault.c,v 1.58 2001/09/01 00:11:16 kanoj 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/ptrace.h>
13 #include <linux/mman.h>
14 #include <linux/signal.h>
15 #include <linux/mm.h>
16 #include <linux/smp_lock.h>
17 #include <linux/init.h>
18 #include <linux/interrupt.h>
19
20 #include <asm/page.h>
21 #include <asm/pgtable.h>
22 #include <asm/openprom.h>
23 #include <asm/oplib.h>
24 #include <asm/uaccess.h>
25 #include <asm/asi.h>
26 #include <asm/lsu.h>
27
28 #define ELEMENTS(arr) (sizeof (arr)/sizeof (arr[0]))
29
30 extern struct sparc_phys_banks sp_banks[SPARC_PHYS_BANKS];
31
32 /*
33 * To debug kernel during syscall entry.
34 */
35 void syscall_trace_entry(struct pt_regs *regs)
36 {
37 printk("scall entry: %s[%d]/cpu%d: %d\n", current->comm, current->pid, smp_processor_id(), (int) regs->u_regs[UREG_G1]);
38 }
39
40 /*
41 * To debug kernel during syscall exit.
42 */
43 void syscall_trace_exit(struct pt_regs *regs)
44 {
45 printk("scall exit: %s[%d]/cpu%d: %d\n", current->comm, current->pid, smp_processor_id(), (int) regs->u_regs[UREG_G1]);
46 }
47
48 /*
49 * To debug kernel to catch accesses to certain virtual/physical addresses.
50 * Mode = 0 selects physical watchpoints, mode = 1 selects virtual watchpoints.
51 * flags = VM_READ watches memread accesses, flags = VM_WRITE watches memwrite accesses.
52 * Caller passes in a 64bit aligned addr, with mask set to the bytes that need to be
53 * watched. This is only useful on a single cpu machine for now. After the watchpoint
54 * is detected, the process causing it will be killed, thus preventing an infinite loop.
55 */
56 void set_brkpt(unsigned long addr, unsigned char mask, int flags, int mode)
57 {
58 unsigned long lsubits = LSU_CONTROL_IC|LSU_CONTROL_DC|LSU_CONTROL_IM|LSU_CONTROL_DM;
59
60 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
61 "membar #Sync"
62 : /* no outputs */
63 : "r" (addr), "r" (mode ? VIRT_WATCHPOINT : PHYS_WATCHPOINT),
64 "i" (ASI_DMMU));
65 lsubits |= ((unsigned long)mask << (mode ? 25 : 33));
66 if (flags & VM_READ)
67 lsubits |= (mode ? LSU_CONTROL_VR : LSU_CONTROL_PR);
68 if (flags & VM_WRITE)
69 lsubits |= (mode ? LSU_CONTROL_VW : LSU_CONTROL_PW);
70 __asm__ __volatile__("stxa %0, [%%g0] %1\n\t"
71 "membar #Sync"
72 : /* no outputs */
73 : "r" (lsubits), "i" (ASI_LSU_CONTROL)
74 : "memory");
75 }
76
77 /* Nice, simple, prom library does all the sweating for us. ;) */
78 unsigned long __init prom_probe_memory (void)
79 {
80 register struct linux_mlist_p1275 *mlist;
81 register unsigned long bytes, base_paddr, tally;
82 register int i;
83
84 i = 0;
85 mlist = *prom_meminfo()->p1275_available;
86 bytes = tally = mlist->num_bytes;
87 base_paddr = mlist->start_adr;
88
89 sp_banks[0].base_addr = base_paddr;
90 sp_banks[0].num_bytes = bytes;
91
92 while (mlist->theres_more != (void *) 0) {
93 i++;
94 mlist = mlist->theres_more;
95 bytes = mlist->num_bytes;
96 tally += bytes;
97 if (i >= SPARC_PHYS_BANKS-1) {
98 printk ("The machine has more banks than "
99 "this kernel can support\n"
100 "Increase the SPARC_PHYS_BANKS "
101 "setting (currently %d)\n",
102 SPARC_PHYS_BANKS);
103 i = SPARC_PHYS_BANKS-1;
104 break;
105 }
106
107 sp_banks[i].base_addr = mlist->start_adr;
108 sp_banks[i].num_bytes = mlist->num_bytes;
109 }
110
111 i++;
112 sp_banks[i].base_addr = 0xdeadbeefbeefdeadUL;
113 sp_banks[i].num_bytes = 0;
114
115 /* Now mask all bank sizes on a page boundary, it is all we can
116 * use anyways.
117 */
118 for (i = 0; sp_banks[i].num_bytes != 0; i++)
119 sp_banks[i].num_bytes &= PAGE_MASK;
120
121 return tally;
122 }
123
124 void unhandled_fault(unsigned long address, struct task_struct *tsk,
125 struct pt_regs *regs)
126 {
127 if ((unsigned long) address < PAGE_SIZE) {
128 printk(KERN_ALERT "Unable to handle kernel NULL "
129 "pointer dereference\n");
130 } else {
131 printk(KERN_ALERT "Unable to handle kernel paging request "
132 "at virtual address %016lx\n", (unsigned long)address);
133 }
134 printk(KERN_ALERT "tsk->{mm,active_mm}->context = %016lx\n",
135 (tsk->mm ? tsk->mm->context : tsk->active_mm->context));
136 printk(KERN_ALERT "tsk->{mm,active_mm}->pgd = %016lx\n",
137 (tsk->mm ? (unsigned long) tsk->mm->pgd :
138 (unsigned long) tsk->active_mm->pgd));
139 die_if_kernel("Oops", regs);
140 }
141
142 /*
143 * We now make sure that mmap_sem is held in all paths that call
144 * this. Additionally, to prevent kswapd from ripping ptes from
145 * under us, raise interrupts around the time that we look at the
146 * pte, kswapd will have to wait to get his smp ipi response from
147 * us. This saves us having to get page_table_lock.
148 */
149 static unsigned int get_user_insn(unsigned long tpc)
150 {
151 pgd_t *pgdp = pgd_offset(current->mm, tpc);
152 pmd_t *pmdp;
153 pte_t *ptep, pte;
154 unsigned long pa;
155 u32 insn = 0;
156 unsigned long pstate;
157
158 if (pgd_none(*pgdp))
159 goto outret;
160 pmdp = pmd_offset(pgdp, tpc);
161 if (pmd_none(*pmdp))
162 goto outret;
163 ptep = pte_offset(pmdp, tpc);
164 __asm__ __volatile__("rdpr %%pstate, %0" : "=r" (pstate));
165 __asm__ __volatile__("wrpr %0, %1, %%pstate"
166 : : "r" (pstate), "i" (PSTATE_IE));
167 pte = *ptep;
168 if (!pte_present(pte))
169 goto out;
170
171 pa = (pte_val(pte) & _PAGE_PADDR);
172 pa += (tpc & ~PAGE_MASK);
173
174 /* Use phys bypass so we don't pollute dtlb/dcache. */
175 __asm__ __volatile__("lduwa [%1] %2, %0"
176 : "=r" (insn)
177 : "r" (pa), "i" (ASI_PHYS_USE_EC));
178
179 out:
180 __asm__ __volatile__("wrpr %0, 0x0, %%pstate" : : "r" (pstate));
181 outret:
182 return insn;
183 }
184
185 static void do_fault_siginfo(int code, int sig, unsigned long address)
186 {
187 siginfo_t info;
188
189 info.si_code = code;
190 info.si_signo = sig;
191 info.si_errno = 0;
192 info.si_addr = (void *) address;
193 info.si_trapno = 0;
194 force_sig_info(sig, &info, current);
195 }
196
197 extern int handle_ldf_stq(u32, struct pt_regs *);
198 extern int handle_ld_nf(u32, struct pt_regs *);
199
200 static inline unsigned int get_fault_insn(struct pt_regs *regs, unsigned int insn)
201 {
202 if (!insn) {
203 if (!regs->tpc || (regs->tpc & 0x3))
204 return 0;
205 if (regs->tstate & TSTATE_PRIV) {
206 insn = *(unsigned int *)regs->tpc;
207 } else {
208 insn = get_user_insn(regs->tpc);
209 }
210 }
211 return insn;
212 }
213
214 static void do_kernel_fault(struct pt_regs *regs, int si_code, int fault_code,
215 unsigned int insn, unsigned long address)
216 {
217 unsigned long g2;
218 unsigned char asi = ASI_P;
219
220 if ((!insn) && (regs->tstate & TSTATE_PRIV))
221 goto cannot_handle;
222
223 /* If user insn could be read (thus insn is zero), that
224 * is fine. We will just gun down the process with a signal
225 * in that case.
226 */
227
228 if (!(fault_code & FAULT_CODE_WRITE) &&
229 (insn & 0xc0800000) == 0xc0800000) {
230 if (insn & 0x2000)
231 asi = (regs->tstate >> 24);
232 else
233 asi = (insn >> 5);
234 if ((asi & 0xf2) == 0x82) {
235 if (insn & 0x1000000) {
236 handle_ldf_stq(insn, regs);
237 } else {
238 /* This was a non-faulting load. Just clear the
239 * destination register(s) and continue with the next
240 * instruction. -jj
241 */
242 handle_ld_nf(insn, regs);
243 }
244 return;
245 }
246 }
247
248 g2 = regs->u_regs[UREG_G2];
249
250 /* Is this in ex_table? */
251 if (regs->tstate & TSTATE_PRIV) {
252 unsigned long fixup;
253
254 if (asi == ASI_P && (insn & 0xc0800000) == 0xc0800000) {
255 if (insn & 0x2000)
256 asi = (regs->tstate >> 24);
257 else
258 asi = (insn >> 5);
259 }
260
261 /* Look in asi.h: All _S asis have LS bit set */
262 if ((asi & 0x1) &&
263 (fixup = search_exception_table (regs->tpc, &g2))) {
264 regs->tpc = fixup;
265 regs->tnpc = regs->tpc + 4;
266 regs->u_regs[UREG_G2] = g2;
267 return;
268 }
269 } else {
270 /* The si_code was set to make clear whether
271 * this was a SEGV_MAPERR or SEGV_ACCERR fault.
272 */
273 do_fault_siginfo(si_code, SIGSEGV, address);
274 return;
275 }
276
277 cannot_handle:
278 unhandled_fault (address, current, regs);
279 }
280
281 asmlinkage void do_sparc64_fault(struct pt_regs *regs)
282 {
283 struct mm_struct *mm = current->mm;
284 struct vm_area_struct *vma;
285 unsigned int insn = 0;
286 int si_code, fault_code;
287 unsigned long address;
288
289 si_code = SEGV_MAPERR;
290 fault_code = current->thread.fault_code;
291 address = current->thread.fault_address;
292
293 if ((fault_code & FAULT_CODE_ITLB) &&
294 (fault_code & FAULT_CODE_DTLB))
295 BUG();
296
297 /*
298 * If we're in an interrupt or have no user
299 * context, we must not take the fault..
300 */
301 if (in_interrupt() || !mm)
302 goto intr_or_no_mm;
303
304 if ((current->thread.flags & SPARC_FLAG_32BIT) != 0) {
305 regs->tpc &= 0xffffffff;
306 address &= 0xffffffff;
307 }
308
309 down_read(&mm->mmap_sem);
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 }
337 continue_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 (expand_stack(vma, address))
344 goto bad_area;
345 /*
346 * Ok, we have a good vm_area for this memory access, so
347 * we can handle it..
348 */
349 good_area:
350 si_code = SEGV_ACCERR;
351 if (fault_code & FAULT_CODE_WRITE) {
352 if (!(vma->vm_flags & VM_WRITE))
353 goto bad_area;
354
355 /* Spitfire has an icache which does not snoop
356 * processor stores. Later processors do...
357 */
358 if (tlb_type == spitfire &&
359 (vma->vm_flags & VM_EXEC) != 0 &&
360 vma->vm_file != NULL)
361 current->thread.use_blkcommit = 1;
362 } else {
363 /* Allow reads even for write-only mappings */
364 if (!(vma->vm_flags & (VM_READ | VM_EXEC)))
365 goto bad_area;
366 }
367
368 switch (handle_mm_fault(mm, vma, address, (fault_code & FAULT_CODE_WRITE))) {
369 case 1:
370 current->min_flt++;
371 break;
372 case 2:
373 current->maj_flt++;
374 break;
375 case 0:
376 goto do_sigbus;
377 default:
378 goto out_of_memory;
379 }
380
381 up_read(&mm->mmap_sem);
382 goto fault_done;
383
384 /*
385 * Something tried to access memory that isn't in our memory map..
386 * Fix it, but check if it's kernel or user first..
387 */
388 bad_area:
389 insn = get_fault_insn(regs, insn);
390 up_read(&mm->mmap_sem);
391
392 handle_kernel_fault:
393 do_kernel_fault(regs, si_code, fault_code, insn, address);
394
395 goto fault_done;
396
397 /*
398 * We ran out of memory, or some other thing happened to us that made
399 * us unable to handle the page fault gracefully.
400 */
401 out_of_memory:
402 insn = get_fault_insn(regs, insn);
403 up_read(&mm->mmap_sem);
404 printk("VM: killing process %s\n", current->comm);
405 if (!(regs->tstate & TSTATE_PRIV))
406 do_exit(SIGKILL);
407 goto handle_kernel_fault;
408
409 intr_or_no_mm:
410 insn = get_fault_insn(regs, 0);
411 goto handle_kernel_fault;
412
413 do_sigbus:
414 insn = get_fault_insn(regs, insn);
415 up_read(&mm->mmap_sem);
416
417 /*
418 * Send a sigbus, regardless of whether we were in kernel
419 * or user mode.
420 */
421 do_fault_siginfo(BUS_ADRERR, SIGBUS, address);
422
423 /* Kernel mode? Handle exceptions or die */
424 if (regs->tstate & TSTATE_PRIV)
425 goto handle_kernel_fault;
426
427 fault_done:
428 /* These values are no longer needed, clear them. */
429 current->thread.fault_code = 0;
430 current->thread.use_blkcommit = 0;
431 current->thread.fault_address = 0;
432 }
433