fault_32.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. /*
  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/perf_event.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/kdebug.h>
  22. #include <asm/page.h>
  23. #include <asm/pgtable.h>
  24. #include <asm/openprom.h>
  25. #include <asm/oplib.h>
  26. #include <asm/smp.h>
  27. #include <asm/traps.h>
  28. #include <asm/uaccess.h>
  29. extern int prom_node_root;
  30. int show_unhandled_signals = 1;
  31. /* At boot time we determine these two values necessary for setting
  32. * up the segment maps and page table entries (pte's).
  33. */
  34. int num_segmaps, num_contexts;
  35. int invalid_segment;
  36. /* various Virtual Address Cache parameters we find at boot time... */
  37. int vac_size, vac_linesize, vac_do_hw_vac_flushes;
  38. int vac_entries_per_context, vac_entries_per_segment;
  39. int vac_entries_per_page;
  40. /* Return how much physical memory we have. */
  41. unsigned long probe_memory(void)
  42. {
  43. unsigned long total = 0;
  44. int i;
  45. for (i = 0; sp_banks[i].num_bytes; i++)
  46. total += sp_banks[i].num_bytes;
  47. return total;
  48. }
  49. /* Whee, a level 15 NMI interrupt memory error. Let's have fun... */
  50. asmlinkage void sparc_lvl15_nmi(struct pt_regs *regs, unsigned long serr,
  51. unsigned long svaddr, unsigned long aerr,
  52. unsigned long avaddr)
  53. {
  54. printk("FAULT: NMI received\n");
  55. printk("SREGS: Synchronous Error %08lx\n", serr);
  56. printk(" Synchronous Vaddr %08lx\n", svaddr);
  57. printk(" Asynchronous Error %08lx\n", aerr);
  58. printk(" Asynchronous Vaddr %08lx\n", avaddr);
  59. printk("REGISTER DUMP:\n");
  60. show_regs(regs);
  61. prom_halt();
  62. }
  63. static void unhandled_fault(unsigned long, struct task_struct *,
  64. struct pt_regs *) __attribute__ ((noreturn));
  65. static void unhandled_fault(unsigned long address, struct task_struct *tsk,
  66. struct pt_regs *regs)
  67. {
  68. if((unsigned long) address < PAGE_SIZE) {
  69. printk(KERN_ALERT
  70. "Unable to handle kernel NULL pointer dereference\n");
  71. } else {
  72. printk(KERN_ALERT "Unable to handle kernel paging request "
  73. "at virtual address %08lx\n", address);
  74. }
  75. printk(KERN_ALERT "tsk->{mm,active_mm}->context = %08lx\n",
  76. (tsk->mm ? tsk->mm->context : tsk->active_mm->context));
  77. printk(KERN_ALERT "tsk->{mm,active_mm}->pgd = %08lx\n",
  78. (tsk->mm ? (unsigned long) tsk->mm->pgd :
  79. (unsigned long) tsk->active_mm->pgd));
  80. die_if_kernel("Oops", regs);
  81. }
  82. asmlinkage int lookup_fault(unsigned long pc, unsigned long ret_pc,
  83. unsigned long address)
  84. {
  85. struct pt_regs regs;
  86. unsigned long g2;
  87. unsigned int insn;
  88. int i;
  89. i = search_extables_range(ret_pc, &g2);
  90. switch (i) {
  91. case 3:
  92. /* load & store will be handled by fixup */
  93. return 3;
  94. case 1:
  95. /* store will be handled by fixup, load will bump out */
  96. /* for _to_ macros */
  97. insn = *((unsigned int *) pc);
  98. if ((insn >> 21) & 1)
  99. return 1;
  100. break;
  101. case 2:
  102. /* load will be handled by fixup, store will bump out */
  103. /* for _from_ macros */
  104. insn = *((unsigned int *) pc);
  105. if (!((insn >> 21) & 1) || ((insn>>19)&0x3f) == 15)
  106. return 2;
  107. break;
  108. default:
  109. break;
  110. }
  111. memset(&regs, 0, sizeof (regs));
  112. regs.pc = pc;
  113. regs.npc = pc + 4;
  114. __asm__ __volatile__(
  115. "rd %%psr, %0\n\t"
  116. "nop\n\t"
  117. "nop\n\t"
  118. "nop\n" : "=r" (regs.psr));
  119. unhandled_fault(address, current, &regs);
  120. /* Not reached */
  121. return 0;
  122. }
  123. static inline void
  124. show_signal_msg(struct pt_regs *regs, int sig, int code,
  125. unsigned long address, struct task_struct *tsk)
  126. {
  127. if (!unhandled_signal(tsk, sig))
  128. return;
  129. if (!printk_ratelimit())
  130. return;
  131. printk("%s%s[%d]: segfault at %lx ip %p (rpc %p) sp %p error %x",
  132. task_pid_nr(tsk) > 1 ? KERN_INFO : KERN_EMERG,
  133. tsk->comm, task_pid_nr(tsk), address,
  134. (void *)regs->pc, (void *)regs->u_regs[UREG_I7],
  135. (void *)regs->u_regs[UREG_FP], code);
  136. print_vma_addr(KERN_CONT " in ", regs->pc);
  137. printk(KERN_CONT "\n");
  138. }
  139. static void __do_fault_siginfo(int code, int sig, struct pt_regs *regs,
  140. unsigned long addr)
  141. {
  142. siginfo_t info;
  143. info.si_signo = sig;
  144. info.si_code = code;
  145. info.si_errno = 0;
  146. info.si_addr = (void __user *) addr;
  147. info.si_trapno = 0;
  148. if (unlikely(show_unhandled_signals))
  149. show_signal_msg(regs, sig, info.si_code,
  150. addr, current);
  151. force_sig_info (sig, &info, current);
  152. }
  153. extern unsigned long safe_compute_effective_address(struct pt_regs *,
  154. unsigned int);
  155. static unsigned long compute_si_addr(struct pt_regs *regs, int text_fault)
  156. {
  157. unsigned int insn;
  158. if (text_fault)
  159. return regs->pc;
  160. if (regs->psr & PSR_PS) {
  161. insn = *(unsigned int *) regs->pc;
  162. } else {
  163. __get_user(insn, (unsigned int *) regs->pc);
  164. }
  165. return safe_compute_effective_address(regs, insn);
  166. }
  167. static noinline void do_fault_siginfo(int code, int sig, struct pt_regs *regs,
  168. int text_fault)
  169. {
  170. unsigned long addr = compute_si_addr(regs, text_fault);
  171. __do_fault_siginfo(code, sig, regs, addr);
  172. }
  173. asmlinkage void do_sparc_fault(struct pt_regs *regs, int text_fault, int write,
  174. unsigned long address)
  175. {
  176. struct vm_area_struct *vma;
  177. struct task_struct *tsk = current;
  178. struct mm_struct *mm = tsk->mm;
  179. unsigned int fixup;
  180. unsigned long g2;
  181. int from_user = !(regs->psr & PSR_PS);
  182. int fault, code;
  183. unsigned int flags = (FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE |
  184. (write ? FAULT_FLAG_WRITE : 0));
  185. if(text_fault)
  186. address = regs->pc;
  187. /*
  188. * We fault-in kernel-space virtual memory on-demand. The
  189. * 'reference' page table is init_mm.pgd.
  190. *
  191. * NOTE! We MUST NOT take any locks for this case. We may
  192. * be in an interrupt or a critical region, and should
  193. * only copy the information from the master page table,
  194. * nothing more.
  195. */
  196. code = SEGV_MAPERR;
  197. if (!ARCH_SUN4C && address >= TASK_SIZE)
  198. goto vmalloc_fault;
  199. /*
  200. * If we're in an interrupt or have no user
  201. * context, we must not take the fault..
  202. */
  203. if (in_atomic() || !mm)
  204. goto no_context;
  205. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
  206. retry:
  207. down_read(&mm->mmap_sem);
  208. /*
  209. * The kernel referencing a bad kernel pointer can lock up
  210. * a sun4c machine completely, so we must attempt recovery.
  211. */
  212. if(!from_user && address >= PAGE_OFFSET)
  213. goto bad_area;
  214. vma = find_vma(mm, address);
  215. if(!vma)
  216. goto bad_area;
  217. if(vma->vm_start <= address)
  218. goto good_area;
  219. if(!(vma->vm_flags & VM_GROWSDOWN))
  220. goto bad_area;
  221. if(expand_stack(vma, address))
  222. goto bad_area;
  223. /*
  224. * Ok, we have a good vm_area for this memory access, so
  225. * we can handle it..
  226. */
  227. good_area:
  228. code = SEGV_ACCERR;
  229. if(write) {
  230. if(!(vma->vm_flags & VM_WRITE))
  231. goto bad_area;
  232. } else {
  233. /* Allow reads even for write-only mappings */
  234. if(!(vma->vm_flags & (VM_READ | VM_EXEC)))
  235. goto bad_area;
  236. }
  237. /*
  238. * If for any reason at all we couldn't handle the fault,
  239. * make sure we exit gracefully rather than endlessly redo
  240. * the fault.
  241. */
  242. fault = handle_mm_fault(mm, vma, address, flags);
  243. if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(current))
  244. return;
  245. if (unlikely(fault & VM_FAULT_ERROR)) {
  246. if (fault & VM_FAULT_OOM)
  247. goto out_of_memory;
  248. else if (fault & VM_FAULT_SIGBUS)
  249. goto do_sigbus;
  250. BUG();
  251. }
  252. if (flags & FAULT_FLAG_ALLOW_RETRY) {
  253. if (fault & VM_FAULT_MAJOR) {
  254. current->maj_flt++;
  255. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ,
  256. 1, regs, address);
  257. } else {
  258. current->min_flt++;
  259. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN,
  260. 1, regs, address);
  261. }
  262. if (fault & VM_FAULT_RETRY) {
  263. flags &= ~FAULT_FLAG_ALLOW_RETRY;
  264. /* No need to up_read(&mm->mmap_sem) as we would
  265. * have already released it in __lock_page_or_retry
  266. * in mm/filemap.c.
  267. */
  268. goto retry;
  269. }
  270. }
  271. up_read(&mm->mmap_sem);
  272. return;
  273. /*
  274. * Something tried to access memory that isn't in our memory map..
  275. * Fix it, but check if it's kernel or user first..
  276. */
  277. bad_area:
  278. up_read(&mm->mmap_sem);
  279. bad_area_nosemaphore:
  280. /* User mode accesses just cause a SIGSEGV */
  281. if (from_user) {
  282. do_fault_siginfo(code, SIGSEGV, regs, text_fault);
  283. return;
  284. }
  285. /* Is this in ex_table? */
  286. no_context:
  287. g2 = regs->u_regs[UREG_G2];
  288. if (!from_user) {
  289. fixup = search_extables_range(regs->pc, &g2);
  290. if (fixup > 10) { /* Values below are reserved for other things */
  291. extern const unsigned __memset_start[];
  292. extern const unsigned __memset_end[];
  293. extern const unsigned __csum_partial_copy_start[];
  294. extern const unsigned __csum_partial_copy_end[];
  295. #ifdef DEBUG_EXCEPTIONS
  296. printk("Exception: PC<%08lx> faddr<%08lx>\n", regs->pc, address);
  297. printk("EX_TABLE: insn<%08lx> fixup<%08x> g2<%08lx>\n",
  298. regs->pc, fixup, g2);
  299. #endif
  300. if ((regs->pc >= (unsigned long)__memset_start &&
  301. regs->pc < (unsigned long)__memset_end) ||
  302. (regs->pc >= (unsigned long)__csum_partial_copy_start &&
  303. regs->pc < (unsigned long)__csum_partial_copy_end)) {
  304. regs->u_regs[UREG_I4] = address;
  305. regs->u_regs[UREG_I5] = regs->pc;
  306. }
  307. regs->u_regs[UREG_G2] = g2;
  308. regs->pc = fixup;
  309. regs->npc = regs->pc + 4;
  310. return;
  311. }
  312. }
  313. unhandled_fault (address, tsk, regs);
  314. do_exit(SIGKILL);
  315. /*
  316. * We ran out of memory, or some other thing happened to us that made
  317. * us unable to handle the page fault gracefully.
  318. */
  319. out_of_memory:
  320. up_read(&mm->mmap_sem);
  321. if (from_user) {
  322. pagefault_out_of_memory();
  323. return;
  324. }
  325. goto no_context;
  326. do_sigbus:
  327. up_read(&mm->mmap_sem);
  328. do_fault_siginfo(BUS_ADRERR, SIGBUS, regs, text_fault);
  329. if (!from_user)
  330. goto no_context;
  331. vmalloc_fault:
  332. {
  333. /*
  334. * Synchronize this task's top level page-table
  335. * with the 'reference' page table.
  336. */
  337. int offset = pgd_index(address);
  338. pgd_t *pgd, *pgd_k;
  339. pmd_t *pmd, *pmd_k;
  340. pgd = tsk->active_mm->pgd + offset;
  341. pgd_k = init_mm.pgd + offset;
  342. if (!pgd_present(*pgd)) {
  343. if (!pgd_present(*pgd_k))
  344. goto bad_area_nosemaphore;
  345. pgd_val(*pgd) = pgd_val(*pgd_k);
  346. return;
  347. }
  348. pmd = pmd_offset(pgd, address);
  349. pmd_k = pmd_offset(pgd_k, address);
  350. if (pmd_present(*pmd) || !pmd_present(*pmd_k))
  351. goto bad_area_nosemaphore;
  352. *pmd = *pmd_k;
  353. return;
  354. }
  355. }
  356. /* This always deals with user addresses. */
  357. static void force_user_fault(unsigned long address, int write)
  358. {
  359. struct vm_area_struct *vma;
  360. struct task_struct *tsk = current;
  361. struct mm_struct *mm = tsk->mm;
  362. int code;
  363. code = SEGV_MAPERR;
  364. down_read(&mm->mmap_sem);
  365. vma = find_vma(mm, address);
  366. if(!vma)
  367. goto bad_area;
  368. if(vma->vm_start <= address)
  369. goto good_area;
  370. if(!(vma->vm_flags & VM_GROWSDOWN))
  371. goto bad_area;
  372. if(expand_stack(vma, address))
  373. goto bad_area;
  374. good_area:
  375. code = SEGV_ACCERR;
  376. if(write) {
  377. if(!(vma->vm_flags & VM_WRITE))
  378. goto bad_area;
  379. } else {
  380. if(!(vma->vm_flags & (VM_READ | VM_EXEC)))
  381. goto bad_area;
  382. }
  383. switch (handle_mm_fault(mm, vma, address, write ? FAULT_FLAG_WRITE : 0)) {
  384. case VM_FAULT_SIGBUS:
  385. case VM_FAULT_OOM:
  386. goto do_sigbus;
  387. }
  388. up_read(&mm->mmap_sem);
  389. return;
  390. bad_area:
  391. up_read(&mm->mmap_sem);
  392. __do_fault_siginfo(code, SIGSEGV, tsk->thread.kregs, address);
  393. return;
  394. do_sigbus:
  395. up_read(&mm->mmap_sem);
  396. __do_fault_siginfo(BUS_ADRERR, SIGBUS, tsk->thread.kregs, address);
  397. }
  398. static void check_stack_aligned(unsigned long sp)
  399. {
  400. if (sp & 0x7UL)
  401. force_sig(SIGILL, current);
  402. }
  403. void window_overflow_fault(void)
  404. {
  405. unsigned long sp;
  406. sp = current_thread_info()->rwbuf_stkptrs[0];
  407. if(((sp + 0x38) & PAGE_MASK) != (sp & PAGE_MASK))
  408. force_user_fault(sp + 0x38, 1);
  409. force_user_fault(sp, 1);
  410. check_stack_aligned(sp);
  411. }
  412. void window_underflow_fault(unsigned long sp)
  413. {
  414. if(((sp + 0x38) & PAGE_MASK) != (sp & PAGE_MASK))
  415. force_user_fault(sp + 0x38, 0);
  416. force_user_fault(sp, 0);
  417. check_stack_aligned(sp);
  418. }
  419. void window_ret_fault(struct pt_regs *regs)
  420. {
  421. unsigned long sp;
  422. sp = regs->u_regs[UREG_FP];
  423. if(((sp + 0x38) & PAGE_MASK) != (sp & PAGE_MASK))
  424. force_user_fault(sp + 0x38, 0);
  425. force_user_fault(sp, 0);
  426. check_stack_aligned(sp);
  427. }