fault.c 15 KB

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