fault.c 15 KB

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