fault.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  1. /* $Id: fault.c,v 1.122 2001/11/17 07:19:26 davem Exp $
  2. * fault.c: Page fault handlers for the Sparc.
  3. *
  4. * Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu)
  5. * Copyright (C) 1996 Eddie C. Dost (ecd@skynet.be)
  6. * Copyright (C) 1997 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
  7. */
  8. #include <asm/head.h>
  9. #include <linux/string.h>
  10. #include <linux/types.h>
  11. #include <linux/sched.h>
  12. #include <linux/ptrace.h>
  13. #include <linux/mman.h>
  14. #include <linux/threads.h>
  15. #include <linux/kernel.h>
  16. #include <linux/signal.h>
  17. #include <linux/mm.h>
  18. #include <linux/smp.h>
  19. #include <linux/smp_lock.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/module.h>
  22. #include <asm/system.h>
  23. #include <asm/segment.h>
  24. #include <asm/page.h>
  25. #include <asm/pgtable.h>
  26. #include <asm/memreg.h>
  27. #include <asm/openprom.h>
  28. #include <asm/oplib.h>
  29. #include <asm/smp.h>
  30. #include <asm/traps.h>
  31. #include <asm/kdebug.h>
  32. #include <asm/uaccess.h>
  33. #define ELEMENTS(arr) (sizeof (arr)/sizeof (arr[0]))
  34. extern int prom_node_root;
  35. /* At boot time we determine these two values necessary for setting
  36. * up the segment maps and page table entries (pte's).
  37. */
  38. int num_segmaps, num_contexts;
  39. int invalid_segment;
  40. /* various Virtual Address Cache parameters we find at boot time... */
  41. int vac_size, vac_linesize, vac_do_hw_vac_flushes;
  42. int vac_entries_per_context, vac_entries_per_segment;
  43. int vac_entries_per_page;
  44. /* Nice, simple, prom library does all the sweating for us. ;) */
  45. int prom_probe_memory (void)
  46. {
  47. register struct linux_mlist_v0 *mlist;
  48. register unsigned long bytes, base_paddr, tally;
  49. register int i;
  50. i = 0;
  51. mlist= *prom_meminfo()->v0_available;
  52. bytes = tally = mlist->num_bytes;
  53. base_paddr = (unsigned long) mlist->start_adr;
  54. sp_banks[0].base_addr = base_paddr;
  55. sp_banks[0].num_bytes = bytes;
  56. while (mlist->theres_more != (void *) 0){
  57. i++;
  58. mlist = mlist->theres_more;
  59. bytes = mlist->num_bytes;
  60. tally += bytes;
  61. if (i > SPARC_PHYS_BANKS-1) {
  62. printk ("The machine has more banks than "
  63. "this kernel can support\n"
  64. "Increase the SPARC_PHYS_BANKS "
  65. "setting (currently %d)\n",
  66. SPARC_PHYS_BANKS);
  67. i = SPARC_PHYS_BANKS-1;
  68. break;
  69. }
  70. sp_banks[i].base_addr = (unsigned long) mlist->start_adr;
  71. sp_banks[i].num_bytes = mlist->num_bytes;
  72. }
  73. i++;
  74. sp_banks[i].base_addr = 0xdeadbeef;
  75. sp_banks[i].num_bytes = 0;
  76. /* Now mask all bank sizes on a page boundary, it is all we can
  77. * use anyways.
  78. */
  79. for(i=0; sp_banks[i].num_bytes != 0; i++)
  80. sp_banks[i].num_bytes &= PAGE_MASK;
  81. return tally;
  82. }
  83. /* Traverse the memory lists in the prom to see how much physical we
  84. * have.
  85. */
  86. unsigned long
  87. probe_memory(void)
  88. {
  89. int total;
  90. total = prom_probe_memory();
  91. /* Oh man, much nicer, keep the dirt in promlib. */
  92. return total;
  93. }
  94. extern void sun4c_complete_all_stores(void);
  95. /* Whee, a level 15 NMI interrupt memory error. Let's have fun... */
  96. asmlinkage void sparc_lvl15_nmi(struct pt_regs *regs, unsigned long serr,
  97. unsigned long svaddr, unsigned long aerr,
  98. unsigned long avaddr)
  99. {
  100. sun4c_complete_all_stores();
  101. printk("FAULT: NMI received\n");
  102. printk("SREGS: Synchronous Error %08lx\n", serr);
  103. printk(" Synchronous Vaddr %08lx\n", svaddr);
  104. printk(" Asynchronous Error %08lx\n", aerr);
  105. printk(" Asynchronous Vaddr %08lx\n", avaddr);
  106. if (sun4c_memerr_reg)
  107. printk(" Memory Parity Error %08lx\n", *sun4c_memerr_reg);
  108. printk("REGISTER DUMP:\n");
  109. show_regs(regs);
  110. prom_halt();
  111. }
  112. static void unhandled_fault(unsigned long, struct task_struct *,
  113. struct pt_regs *) __attribute__ ((noreturn));
  114. static void unhandled_fault(unsigned long address, struct task_struct *tsk,
  115. struct pt_regs *regs)
  116. {
  117. if((unsigned long) address < PAGE_SIZE) {
  118. printk(KERN_ALERT
  119. "Unable to handle kernel NULL pointer dereference\n");
  120. } else {
  121. printk(KERN_ALERT "Unable to handle kernel paging request "
  122. "at virtual address %08lx\n", address);
  123. }
  124. printk(KERN_ALERT "tsk->{mm,active_mm}->context = %08lx\n",
  125. (tsk->mm ? tsk->mm->context : tsk->active_mm->context));
  126. printk(KERN_ALERT "tsk->{mm,active_mm}->pgd = %08lx\n",
  127. (tsk->mm ? (unsigned long) tsk->mm->pgd :
  128. (unsigned long) tsk->active_mm->pgd));
  129. die_if_kernel("Oops", regs);
  130. }
  131. asmlinkage int lookup_fault(unsigned long pc, unsigned long ret_pc,
  132. unsigned long address)
  133. {
  134. struct pt_regs regs;
  135. unsigned long g2;
  136. unsigned int insn;
  137. int i;
  138. i = search_extables_range(ret_pc, &g2);
  139. switch (i) {
  140. case 3:
  141. /* load & store will be handled by fixup */
  142. return 3;
  143. case 1:
  144. /* store will be handled by fixup, load will bump out */
  145. /* for _to_ macros */
  146. insn = *((unsigned int *) pc);
  147. if ((insn >> 21) & 1)
  148. return 1;
  149. break;
  150. case 2:
  151. /* load will be handled by fixup, store will bump out */
  152. /* for _from_ macros */
  153. insn = *((unsigned int *) pc);
  154. if (!((insn >> 21) & 1) || ((insn>>19)&0x3f) == 15)
  155. return 2;
  156. break;
  157. default:
  158. break;
  159. };
  160. memset(&regs, 0, sizeof (regs));
  161. regs.pc = pc;
  162. regs.npc = pc + 4;
  163. __asm__ __volatile__(
  164. "rd %%psr, %0\n\t"
  165. "nop\n\t"
  166. "nop\n\t"
  167. "nop\n" : "=r" (regs.psr));
  168. unhandled_fault(address, current, &regs);
  169. /* Not reached */
  170. return 0;
  171. }
  172. extern unsigned long safe_compute_effective_address(struct pt_regs *,
  173. unsigned int);
  174. static unsigned long compute_si_addr(struct pt_regs *regs, int text_fault)
  175. {
  176. unsigned int insn;
  177. if (text_fault)
  178. return regs->pc;
  179. if (regs->psr & PSR_PS) {
  180. insn = *(unsigned int *) regs->pc;
  181. } else {
  182. __get_user(insn, (unsigned int *) regs->pc);
  183. }
  184. return safe_compute_effective_address(regs, insn);
  185. }
  186. asmlinkage void do_sparc_fault(struct pt_regs *regs, int text_fault, int write,
  187. unsigned long address)
  188. {
  189. struct vm_area_struct *vma;
  190. struct task_struct *tsk = current;
  191. struct mm_struct *mm = tsk->mm;
  192. unsigned int fixup;
  193. unsigned long g2;
  194. siginfo_t info;
  195. int from_user = !(regs->psr & PSR_PS);
  196. if(text_fault)
  197. address = regs->pc;
  198. /*
  199. * We fault-in kernel-space virtual memory on-demand. The
  200. * 'reference' page table is init_mm.pgd.
  201. *
  202. * NOTE! We MUST NOT take any locks for this case. We may
  203. * be in an interrupt or a critical region, and should
  204. * only copy the information from the master page table,
  205. * nothing more.
  206. */
  207. if (!ARCH_SUN4C_SUN4 && address >= TASK_SIZE)
  208. goto vmalloc_fault;
  209. info.si_code = SEGV_MAPERR;
  210. /*
  211. * If we're in an interrupt or have no user
  212. * context, we must not take the fault..
  213. */
  214. if (in_atomic() || !mm)
  215. goto no_context;
  216. down_read(&mm->mmap_sem);
  217. /*
  218. * The kernel referencing a bad kernel pointer can lock up
  219. * a sun4c machine completely, so we must attempt recovery.
  220. */
  221. if(!from_user && address >= PAGE_OFFSET)
  222. goto bad_area;
  223. vma = find_vma(mm, address);
  224. if(!vma)
  225. goto bad_area;
  226. if(vma->vm_start <= address)
  227. goto good_area;
  228. if(!(vma->vm_flags & VM_GROWSDOWN))
  229. goto bad_area;
  230. if(expand_stack(vma, address))
  231. goto bad_area;
  232. /*
  233. * Ok, we have a good vm_area for this memory access, so
  234. * we can handle it..
  235. */
  236. good_area:
  237. info.si_code = SEGV_ACCERR;
  238. if(write) {
  239. if(!(vma->vm_flags & VM_WRITE))
  240. goto bad_area;
  241. } else {
  242. /* Allow reads even for write-only mappings */
  243. if(!(vma->vm_flags & (VM_READ | VM_EXEC)))
  244. goto bad_area;
  245. }
  246. /*
  247. * If for any reason at all we couldn't handle the fault,
  248. * make sure we exit gracefully rather than endlessly redo
  249. * the fault.
  250. */
  251. switch (handle_mm_fault(mm, vma, address, write)) {
  252. case VM_FAULT_SIGBUS:
  253. goto do_sigbus;
  254. case VM_FAULT_OOM:
  255. goto out_of_memory;
  256. case VM_FAULT_MAJOR:
  257. current->maj_flt++;
  258. break;
  259. case VM_FAULT_MINOR:
  260. default:
  261. current->min_flt++;
  262. break;
  263. }
  264. up_read(&mm->mmap_sem);
  265. return;
  266. /*
  267. * Something tried to access memory that isn't in our memory map..
  268. * Fix it, but check if it's kernel or user first..
  269. */
  270. bad_area:
  271. up_read(&mm->mmap_sem);
  272. bad_area_nosemaphore:
  273. /* User mode accesses just cause a SIGSEGV */
  274. if(from_user) {
  275. #if 0
  276. printk("Fault whee %s [%d]: segfaults at %08lx pc=%08lx\n",
  277. tsk->comm, tsk->pid, address, regs->pc);
  278. #endif
  279. info.si_signo = SIGSEGV;
  280. info.si_errno = 0;
  281. /* info.si_code set above to make clear whether
  282. this was a SEGV_MAPERR or SEGV_ACCERR fault. */
  283. info.si_addr = (void __user *)compute_si_addr(regs, text_fault);
  284. info.si_trapno = 0;
  285. force_sig_info (SIGSEGV, &info, tsk);
  286. return;
  287. }
  288. /* Is this in ex_table? */
  289. no_context:
  290. g2 = regs->u_regs[UREG_G2];
  291. if (!from_user && (fixup = search_extables_range(regs->pc, &g2))) {
  292. if (fixup > 10) { /* Values below are reserved for other things */
  293. extern const unsigned __memset_start[];
  294. extern const unsigned __memset_end[];
  295. extern const unsigned __csum_partial_copy_start[];
  296. extern const unsigned __csum_partial_copy_end[];
  297. #ifdef DEBUG_EXCEPTIONS
  298. printk("Exception: PC<%08lx> faddr<%08lx>\n", regs->pc, address);
  299. printk("EX_TABLE: insn<%08lx> fixup<%08x> g2<%08lx>\n",
  300. regs->pc, fixup, g2);
  301. #endif
  302. if ((regs->pc >= (unsigned long)__memset_start &&
  303. regs->pc < (unsigned long)__memset_end) ||
  304. (regs->pc >= (unsigned long)__csum_partial_copy_start &&
  305. regs->pc < (unsigned long)__csum_partial_copy_end)) {
  306. regs->u_regs[UREG_I4] = address;
  307. regs->u_regs[UREG_I5] = regs->pc;
  308. }
  309. regs->u_regs[UREG_G2] = g2;
  310. regs->pc = fixup;
  311. regs->npc = regs->pc + 4;
  312. return;
  313. }
  314. }
  315. unhandled_fault (address, tsk, regs);
  316. do_exit(SIGKILL);
  317. /*
  318. * We ran out of memory, or some other thing happened to us that made
  319. * us unable to handle the page fault gracefully.
  320. */
  321. out_of_memory:
  322. up_read(&mm->mmap_sem);
  323. printk("VM: killing process %s\n", tsk->comm);
  324. if (from_user)
  325. do_exit(SIGKILL);
  326. goto no_context;
  327. do_sigbus:
  328. up_read(&mm->mmap_sem);
  329. info.si_signo = SIGBUS;
  330. info.si_errno = 0;
  331. info.si_code = BUS_ADRERR;
  332. info.si_addr = (void __user *) compute_si_addr(regs, text_fault);
  333. info.si_trapno = 0;
  334. force_sig_info (SIGBUS, &info, tsk);
  335. if (!from_user)
  336. goto no_context;
  337. vmalloc_fault:
  338. {
  339. /*
  340. * Synchronize this task's top level page-table
  341. * with the 'reference' page table.
  342. */
  343. int offset = pgd_index(address);
  344. pgd_t *pgd, *pgd_k;
  345. pmd_t *pmd, *pmd_k;
  346. pgd = tsk->active_mm->pgd + offset;
  347. pgd_k = init_mm.pgd + offset;
  348. if (!pgd_present(*pgd)) {
  349. if (!pgd_present(*pgd_k))
  350. goto bad_area_nosemaphore;
  351. pgd_val(*pgd) = pgd_val(*pgd_k);
  352. return;
  353. }
  354. pmd = pmd_offset(pgd, address);
  355. pmd_k = pmd_offset(pgd_k, address);
  356. if (pmd_present(*pmd) || !pmd_present(*pmd_k))
  357. goto bad_area_nosemaphore;
  358. *pmd = *pmd_k;
  359. return;
  360. }
  361. }
  362. asmlinkage void do_sun4c_fault(struct pt_regs *regs, int text_fault, int write,
  363. unsigned long address)
  364. {
  365. extern void sun4c_update_mmu_cache(struct vm_area_struct *,
  366. unsigned long,pte_t);
  367. extern pte_t *sun4c_pte_offset_kernel(pmd_t *,unsigned long);
  368. struct task_struct *tsk = current;
  369. struct mm_struct *mm = tsk->mm;
  370. pgd_t *pgdp;
  371. pte_t *ptep;
  372. if (text_fault) {
  373. address = regs->pc;
  374. } else if (!write &&
  375. !(regs->psr & PSR_PS)) {
  376. unsigned int insn, __user *ip;
  377. ip = (unsigned int __user *)regs->pc;
  378. if (!get_user(insn, ip)) {
  379. if ((insn & 0xc1680000) == 0xc0680000)
  380. write = 1;
  381. }
  382. }
  383. if (!mm) {
  384. /* We are oopsing. */
  385. do_sparc_fault(regs, text_fault, write, address);
  386. BUG(); /* P3 Oops already, you bitch */
  387. }
  388. pgdp = pgd_offset(mm, address);
  389. ptep = sun4c_pte_offset_kernel((pmd_t *) pgdp, address);
  390. if (pgd_val(*pgdp)) {
  391. if (write) {
  392. if ((pte_val(*ptep) & (_SUN4C_PAGE_WRITE|_SUN4C_PAGE_PRESENT))
  393. == (_SUN4C_PAGE_WRITE|_SUN4C_PAGE_PRESENT)) {
  394. unsigned long flags;
  395. *ptep = __pte(pte_val(*ptep) | _SUN4C_PAGE_ACCESSED |
  396. _SUN4C_PAGE_MODIFIED |
  397. _SUN4C_PAGE_VALID |
  398. _SUN4C_PAGE_DIRTY);
  399. local_irq_save(flags);
  400. if (sun4c_get_segmap(address) != invalid_segment) {
  401. sun4c_put_pte(address, pte_val(*ptep));
  402. local_irq_restore(flags);
  403. return;
  404. }
  405. local_irq_restore(flags);
  406. }
  407. } else {
  408. if ((pte_val(*ptep) & (_SUN4C_PAGE_READ|_SUN4C_PAGE_PRESENT))
  409. == (_SUN4C_PAGE_READ|_SUN4C_PAGE_PRESENT)) {
  410. unsigned long flags;
  411. *ptep = __pte(pte_val(*ptep) | _SUN4C_PAGE_ACCESSED |
  412. _SUN4C_PAGE_VALID);
  413. local_irq_save(flags);
  414. if (sun4c_get_segmap(address) != invalid_segment) {
  415. sun4c_put_pte(address, pte_val(*ptep));
  416. local_irq_restore(flags);
  417. return;
  418. }
  419. local_irq_restore(flags);
  420. }
  421. }
  422. }
  423. /* This conditional is 'interesting'. */
  424. if (pgd_val(*pgdp) && !(write && !(pte_val(*ptep) & _SUN4C_PAGE_WRITE))
  425. && (pte_val(*ptep) & _SUN4C_PAGE_VALID))
  426. /* Note: It is safe to not grab the MMAP semaphore here because
  427. * we know that update_mmu_cache() will not sleep for
  428. * any reason (at least not in the current implementation)
  429. * and therefore there is no danger of another thread getting
  430. * on the CPU and doing a shrink_mmap() on this vma.
  431. */
  432. sun4c_update_mmu_cache (find_vma(current->mm, address), address,
  433. *ptep);
  434. else
  435. do_sparc_fault(regs, text_fault, write, address);
  436. }
  437. /* This always deals with user addresses. */
  438. inline void force_user_fault(unsigned long address, int write)
  439. {
  440. struct vm_area_struct *vma;
  441. struct task_struct *tsk = current;
  442. struct mm_struct *mm = tsk->mm;
  443. siginfo_t info;
  444. info.si_code = SEGV_MAPERR;
  445. #if 0
  446. printk("wf<pid=%d,wr=%d,addr=%08lx>\n",
  447. tsk->pid, write, address);
  448. #endif
  449. down_read(&mm->mmap_sem);
  450. vma = find_vma(mm, address);
  451. if(!vma)
  452. goto bad_area;
  453. if(vma->vm_start <= address)
  454. goto good_area;
  455. if(!(vma->vm_flags & VM_GROWSDOWN))
  456. goto bad_area;
  457. if(expand_stack(vma, address))
  458. goto bad_area;
  459. good_area:
  460. info.si_code = SEGV_ACCERR;
  461. if(write) {
  462. if(!(vma->vm_flags & VM_WRITE))
  463. goto bad_area;
  464. } else {
  465. if(!(vma->vm_flags & (VM_READ | VM_EXEC)))
  466. goto bad_area;
  467. }
  468. switch (handle_mm_fault(mm, vma, address, write)) {
  469. case VM_FAULT_SIGBUS:
  470. case VM_FAULT_OOM:
  471. goto do_sigbus;
  472. }
  473. up_read(&mm->mmap_sem);
  474. return;
  475. bad_area:
  476. up_read(&mm->mmap_sem);
  477. #if 0
  478. printk("Window whee %s [%d]: segfaults at %08lx\n",
  479. tsk->comm, tsk->pid, address);
  480. #endif
  481. info.si_signo = SIGSEGV;
  482. info.si_errno = 0;
  483. /* info.si_code set above to make clear whether
  484. this was a SEGV_MAPERR or SEGV_ACCERR fault. */
  485. info.si_addr = (void __user *) address;
  486. info.si_trapno = 0;
  487. force_sig_info (SIGSEGV, &info, tsk);
  488. return;
  489. do_sigbus:
  490. up_read(&mm->mmap_sem);
  491. info.si_signo = SIGBUS;
  492. info.si_errno = 0;
  493. info.si_code = BUS_ADRERR;
  494. info.si_addr = (void __user *) address;
  495. info.si_trapno = 0;
  496. force_sig_info (SIGBUS, &info, tsk);
  497. }
  498. void window_overflow_fault(void)
  499. {
  500. unsigned long sp;
  501. sp = current_thread_info()->rwbuf_stkptrs[0];
  502. if(((sp + 0x38) & PAGE_MASK) != (sp & PAGE_MASK))
  503. force_user_fault(sp + 0x38, 1);
  504. force_user_fault(sp, 1);
  505. }
  506. void window_underflow_fault(unsigned long sp)
  507. {
  508. if(((sp + 0x38) & PAGE_MASK) != (sp & PAGE_MASK))
  509. force_user_fault(sp + 0x38, 0);
  510. force_user_fault(sp, 0);
  511. }
  512. void window_ret_fault(struct pt_regs *regs)
  513. {
  514. unsigned long sp;
  515. sp = regs->u_regs[UREG_FP];
  516. if(((sp + 0x38) & PAGE_MASK) != (sp & PAGE_MASK))
  517. force_user_fault(sp + 0x38, 0);
  518. force_user_fault(sp, 0);
  519. }