fault.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  1. /*
  2. * S390 version
  3. * Copyright IBM Corp. 1999
  4. * Author(s): Hartmut Penner (hp@de.ibm.com)
  5. * Ulrich Weigand (uweigand@de.ibm.com)
  6. *
  7. * Derived from "arch/i386/mm/fault.c"
  8. * Copyright (C) 1995 Linus Torvalds
  9. */
  10. #include <linux/kernel_stat.h>
  11. #include <linux/perf_event.h>
  12. #include <linux/signal.h>
  13. #include <linux/sched.h>
  14. #include <linux/kernel.h>
  15. #include <linux/errno.h>
  16. #include <linux/string.h>
  17. #include <linux/types.h>
  18. #include <linux/ptrace.h>
  19. #include <linux/mman.h>
  20. #include <linux/mm.h>
  21. #include <linux/compat.h>
  22. #include <linux/smp.h>
  23. #include <linux/kdebug.h>
  24. #include <linux/init.h>
  25. #include <linux/console.h>
  26. #include <linux/module.h>
  27. #include <linux/hardirq.h>
  28. #include <linux/kprobes.h>
  29. #include <linux/uaccess.h>
  30. #include <linux/hugetlb.h>
  31. #include <asm/asm-offsets.h>
  32. #include <asm/pgtable.h>
  33. #include <asm/irq.h>
  34. #include <asm/mmu_context.h>
  35. #include <asm/facility.h>
  36. #include "../kernel/entry.h"
  37. #ifndef CONFIG_64BIT
  38. #define __FAIL_ADDR_MASK 0x7ffff000
  39. #define __SUBCODE_MASK 0x0200
  40. #define __PF_RES_FIELD 0ULL
  41. #else /* CONFIG_64BIT */
  42. #define __FAIL_ADDR_MASK -4096L
  43. #define __SUBCODE_MASK 0x0600
  44. #define __PF_RES_FIELD 0x8000000000000000ULL
  45. #endif /* CONFIG_64BIT */
  46. #define VM_FAULT_BADCONTEXT 0x010000
  47. #define VM_FAULT_BADMAP 0x020000
  48. #define VM_FAULT_BADACCESS 0x040000
  49. #define VM_FAULT_SIGNAL 0x080000
  50. #define VM_FAULT_PFAULT 0x100000
  51. static unsigned long store_indication __read_mostly;
  52. #ifdef CONFIG_64BIT
  53. static int __init fault_init(void)
  54. {
  55. if (test_facility(75))
  56. store_indication = 0xc00;
  57. return 0;
  58. }
  59. early_initcall(fault_init);
  60. #endif
  61. static inline int notify_page_fault(struct pt_regs *regs)
  62. {
  63. int ret = 0;
  64. /* kprobe_running() needs smp_processor_id() */
  65. if (kprobes_built_in() && !user_mode(regs)) {
  66. preempt_disable();
  67. if (kprobe_running() && kprobe_fault_handler(regs, 14))
  68. ret = 1;
  69. preempt_enable();
  70. }
  71. return ret;
  72. }
  73. /*
  74. * Unlock any spinlocks which will prevent us from getting the
  75. * message out.
  76. */
  77. void bust_spinlocks(int yes)
  78. {
  79. if (yes) {
  80. oops_in_progress = 1;
  81. } else {
  82. int loglevel_save = console_loglevel;
  83. console_unblank();
  84. oops_in_progress = 0;
  85. /*
  86. * OK, the message is on the console. Now we call printk()
  87. * without oops_in_progress set so that printk will give klogd
  88. * a poke. Hold onto your hats...
  89. */
  90. console_loglevel = 15;
  91. printk(" ");
  92. console_loglevel = loglevel_save;
  93. }
  94. }
  95. /*
  96. * Returns the address space associated with the fault.
  97. * Returns 0 for kernel space and 1 for user space.
  98. */
  99. static inline int user_space_fault(struct pt_regs *regs)
  100. {
  101. unsigned long trans_exc_code;
  102. /*
  103. * The lowest two bits of the translation exception
  104. * identification indicate which paging table was used.
  105. */
  106. trans_exc_code = regs->int_parm_long & 3;
  107. if (trans_exc_code == 3) /* home space -> kernel */
  108. return 0;
  109. if (user_mode(regs))
  110. return 1;
  111. if (trans_exc_code == 2) /* secondary space -> set_fs */
  112. return current->thread.mm_segment.ar4;
  113. if (current->flags & PF_VCPU)
  114. return 1;
  115. return 0;
  116. }
  117. static inline void report_user_fault(struct pt_regs *regs, long signr)
  118. {
  119. if ((task_pid_nr(current) > 1) && !show_unhandled_signals)
  120. return;
  121. if (!unhandled_signal(current, signr))
  122. return;
  123. if (!printk_ratelimit())
  124. return;
  125. printk(KERN_ALERT "User process fault: interruption code 0x%X ",
  126. regs->int_code);
  127. print_vma_addr(KERN_CONT "in ", regs->psw.addr & PSW_ADDR_INSN);
  128. printk(KERN_CONT "\n");
  129. printk(KERN_ALERT "failing address: %lX\n",
  130. regs->int_parm_long & __FAIL_ADDR_MASK);
  131. show_regs(regs);
  132. }
  133. /*
  134. * Send SIGSEGV to task. This is an external routine
  135. * to keep the stack usage of do_page_fault small.
  136. */
  137. static noinline void do_sigsegv(struct pt_regs *regs, int si_code)
  138. {
  139. struct siginfo si;
  140. report_user_fault(regs, SIGSEGV);
  141. si.si_signo = SIGSEGV;
  142. si.si_code = si_code;
  143. si.si_addr = (void __user *)(regs->int_parm_long & __FAIL_ADDR_MASK);
  144. force_sig_info(SIGSEGV, &si, current);
  145. }
  146. static noinline void do_no_context(struct pt_regs *regs)
  147. {
  148. const struct exception_table_entry *fixup;
  149. unsigned long address;
  150. /* Are we prepared to handle this kernel fault? */
  151. fixup = search_exception_tables(regs->psw.addr & PSW_ADDR_INSN);
  152. if (fixup) {
  153. regs->psw.addr = extable_fixup(fixup) | PSW_ADDR_AMODE;
  154. return;
  155. }
  156. /*
  157. * Oops. The kernel tried to access some bad page. We'll have to
  158. * terminate things with extreme prejudice.
  159. */
  160. address = regs->int_parm_long & __FAIL_ADDR_MASK;
  161. if (!user_space_fault(regs))
  162. printk(KERN_ALERT "Unable to handle kernel pointer dereference"
  163. " at virtual kernel address %p\n", (void *)address);
  164. else
  165. printk(KERN_ALERT "Unable to handle kernel paging request"
  166. " at virtual user address %p\n", (void *)address);
  167. die(regs, "Oops");
  168. do_exit(SIGKILL);
  169. }
  170. static noinline void do_low_address(struct pt_regs *regs)
  171. {
  172. /* Low-address protection hit in kernel mode means
  173. NULL pointer write access in kernel mode. */
  174. if (regs->psw.mask & PSW_MASK_PSTATE) {
  175. /* Low-address protection hit in user mode 'cannot happen'. */
  176. die (regs, "Low-address protection");
  177. do_exit(SIGKILL);
  178. }
  179. do_no_context(regs);
  180. }
  181. static noinline void do_sigbus(struct pt_regs *regs)
  182. {
  183. struct task_struct *tsk = current;
  184. struct siginfo si;
  185. /*
  186. * Send a sigbus, regardless of whether we were in kernel
  187. * or user mode.
  188. */
  189. si.si_signo = SIGBUS;
  190. si.si_errno = 0;
  191. si.si_code = BUS_ADRERR;
  192. si.si_addr = (void __user *)(regs->int_parm_long & __FAIL_ADDR_MASK);
  193. force_sig_info(SIGBUS, &si, tsk);
  194. }
  195. static noinline void do_fault_error(struct pt_regs *regs, int fault)
  196. {
  197. int si_code;
  198. switch (fault) {
  199. case VM_FAULT_BADACCESS:
  200. case VM_FAULT_BADMAP:
  201. /* Bad memory access. Check if it is kernel or user space. */
  202. if (user_mode(regs)) {
  203. /* User mode accesses just cause a SIGSEGV */
  204. si_code = (fault == VM_FAULT_BADMAP) ?
  205. SEGV_MAPERR : SEGV_ACCERR;
  206. do_sigsegv(regs, si_code);
  207. return;
  208. }
  209. case VM_FAULT_BADCONTEXT:
  210. case VM_FAULT_PFAULT:
  211. do_no_context(regs);
  212. break;
  213. case VM_FAULT_SIGNAL:
  214. if (!user_mode(regs))
  215. do_no_context(regs);
  216. break;
  217. default: /* fault & VM_FAULT_ERROR */
  218. if (fault & VM_FAULT_OOM) {
  219. if (!user_mode(regs))
  220. do_no_context(regs);
  221. else
  222. pagefault_out_of_memory();
  223. } else if (fault & VM_FAULT_SIGBUS) {
  224. /* Kernel mode? Handle exceptions or die */
  225. if (!user_mode(regs))
  226. do_no_context(regs);
  227. else
  228. do_sigbus(regs);
  229. } else
  230. BUG();
  231. break;
  232. }
  233. }
  234. /*
  235. * This routine handles page faults. It determines the address,
  236. * and the problem, and then passes it off to one of the appropriate
  237. * routines.
  238. *
  239. * interruption code (int_code):
  240. * 04 Protection -> Write-Protection (suprression)
  241. * 10 Segment translation -> Not present (nullification)
  242. * 11 Page translation -> Not present (nullification)
  243. * 3b Region third trans. -> Not present (nullification)
  244. */
  245. static inline int do_exception(struct pt_regs *regs, int access)
  246. {
  247. #ifdef CONFIG_PGSTE
  248. struct gmap *gmap;
  249. #endif
  250. struct task_struct *tsk;
  251. struct mm_struct *mm;
  252. struct vm_area_struct *vma;
  253. unsigned long trans_exc_code;
  254. unsigned long address;
  255. unsigned int flags;
  256. int fault;
  257. tsk = current;
  258. /*
  259. * The instruction that caused the program check has
  260. * been nullified. Don't signal single step via SIGTRAP.
  261. */
  262. clear_tsk_thread_flag(tsk, TIF_PER_TRAP);
  263. if (notify_page_fault(regs))
  264. return 0;
  265. mm = tsk->mm;
  266. trans_exc_code = regs->int_parm_long;
  267. /*
  268. * Verify that the fault happened in user space, that
  269. * we are not in an interrupt and that there is a
  270. * user context.
  271. */
  272. fault = VM_FAULT_BADCONTEXT;
  273. if (unlikely(!user_space_fault(regs) || in_atomic() || !mm))
  274. goto out;
  275. address = trans_exc_code & __FAIL_ADDR_MASK;
  276. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
  277. flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE;
  278. if (user_mode(regs))
  279. flags |= FAULT_FLAG_USER;
  280. if (access == VM_WRITE || (trans_exc_code & store_indication) == 0x400)
  281. flags |= FAULT_FLAG_WRITE;
  282. down_read(&mm->mmap_sem);
  283. #ifdef CONFIG_PGSTE
  284. gmap = (struct gmap *)
  285. ((current->flags & PF_VCPU) ? S390_lowcore.gmap : 0);
  286. if (gmap) {
  287. address = __gmap_fault(address, gmap);
  288. if (address == -EFAULT) {
  289. fault = VM_FAULT_BADMAP;
  290. goto out_up;
  291. }
  292. if (address == -ENOMEM) {
  293. fault = VM_FAULT_OOM;
  294. goto out_up;
  295. }
  296. if (gmap->pfault_enabled)
  297. flags |= FAULT_FLAG_RETRY_NOWAIT;
  298. }
  299. #endif
  300. retry:
  301. fault = VM_FAULT_BADMAP;
  302. vma = find_vma(mm, address);
  303. if (!vma)
  304. goto out_up;
  305. if (unlikely(vma->vm_start > address)) {
  306. if (!(vma->vm_flags & VM_GROWSDOWN))
  307. goto out_up;
  308. if (expand_stack(vma, address))
  309. goto out_up;
  310. }
  311. /*
  312. * Ok, we have a good vm_area for this memory access, so
  313. * we can handle it..
  314. */
  315. fault = VM_FAULT_BADACCESS;
  316. if (unlikely(!(vma->vm_flags & access)))
  317. goto out_up;
  318. if (is_vm_hugetlb_page(vma))
  319. address &= HPAGE_MASK;
  320. /*
  321. * If for any reason at all we couldn't handle the fault,
  322. * make sure we exit gracefully rather than endlessly redo
  323. * the fault.
  324. */
  325. fault = handle_mm_fault(mm, vma, address, flags);
  326. /* No reason to continue if interrupted by SIGKILL. */
  327. if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(current)) {
  328. fault = VM_FAULT_SIGNAL;
  329. goto out;
  330. }
  331. if (unlikely(fault & VM_FAULT_ERROR))
  332. goto out_up;
  333. /*
  334. * Major/minor page fault accounting is only done on the
  335. * initial attempt. If we go through a retry, it is extremely
  336. * likely that the page will be found in page cache at that point.
  337. */
  338. if (flags & FAULT_FLAG_ALLOW_RETRY) {
  339. if (fault & VM_FAULT_MAJOR) {
  340. tsk->maj_flt++;
  341. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1,
  342. regs, address);
  343. } else {
  344. tsk->min_flt++;
  345. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1,
  346. regs, address);
  347. }
  348. if (fault & VM_FAULT_RETRY) {
  349. #ifdef CONFIG_PGSTE
  350. if (gmap && (flags & FAULT_FLAG_RETRY_NOWAIT)) {
  351. /* FAULT_FLAG_RETRY_NOWAIT has been set,
  352. * mmap_sem has not been released */
  353. current->thread.gmap_pfault = 1;
  354. fault = VM_FAULT_PFAULT;
  355. goto out_up;
  356. }
  357. #endif
  358. /* Clear FAULT_FLAG_ALLOW_RETRY to avoid any risk
  359. * of starvation. */
  360. flags &= ~(FAULT_FLAG_ALLOW_RETRY |
  361. FAULT_FLAG_RETRY_NOWAIT);
  362. flags |= FAULT_FLAG_TRIED;
  363. down_read(&mm->mmap_sem);
  364. goto retry;
  365. }
  366. }
  367. fault = 0;
  368. out_up:
  369. up_read(&mm->mmap_sem);
  370. out:
  371. return fault;
  372. }
  373. void __kprobes do_protection_exception(struct pt_regs *regs)
  374. {
  375. unsigned long trans_exc_code;
  376. int fault;
  377. trans_exc_code = regs->int_parm_long;
  378. /*
  379. * Protection exceptions are suppressing, decrement psw address.
  380. * The exception to this rule are aborted transactions, for these
  381. * the PSW already points to the correct location.
  382. */
  383. if (!(regs->int_code & 0x200))
  384. regs->psw.addr = __rewind_psw(regs->psw, regs->int_code >> 16);
  385. /*
  386. * Check for low-address protection. This needs to be treated
  387. * as a special case because the translation exception code
  388. * field is not guaranteed to contain valid data in this case.
  389. */
  390. if (unlikely(!(trans_exc_code & 4))) {
  391. do_low_address(regs);
  392. return;
  393. }
  394. fault = do_exception(regs, VM_WRITE);
  395. if (unlikely(fault))
  396. do_fault_error(regs, fault);
  397. }
  398. void __kprobes do_dat_exception(struct pt_regs *regs)
  399. {
  400. int access, fault;
  401. access = VM_READ | VM_EXEC | VM_WRITE;
  402. fault = do_exception(regs, access);
  403. if (unlikely(fault))
  404. do_fault_error(regs, fault);
  405. }
  406. #ifdef CONFIG_PFAULT
  407. /*
  408. * 'pfault' pseudo page faults routines.
  409. */
  410. static int pfault_disable;
  411. static int __init nopfault(char *str)
  412. {
  413. pfault_disable = 1;
  414. return 1;
  415. }
  416. __setup("nopfault", nopfault);
  417. struct pfault_refbk {
  418. u16 refdiagc;
  419. u16 reffcode;
  420. u16 refdwlen;
  421. u16 refversn;
  422. u64 refgaddr;
  423. u64 refselmk;
  424. u64 refcmpmk;
  425. u64 reserved;
  426. } __attribute__ ((packed, aligned(8)));
  427. int pfault_init(void)
  428. {
  429. struct pfault_refbk refbk = {
  430. .refdiagc = 0x258,
  431. .reffcode = 0,
  432. .refdwlen = 5,
  433. .refversn = 2,
  434. .refgaddr = __LC_CURRENT_PID,
  435. .refselmk = 1ULL << 48,
  436. .refcmpmk = 1ULL << 48,
  437. .reserved = __PF_RES_FIELD };
  438. int rc;
  439. if (pfault_disable)
  440. return -1;
  441. asm volatile(
  442. " diag %1,%0,0x258\n"
  443. "0: j 2f\n"
  444. "1: la %0,8\n"
  445. "2:\n"
  446. EX_TABLE(0b,1b)
  447. : "=d" (rc) : "a" (&refbk), "m" (refbk) : "cc");
  448. return rc;
  449. }
  450. void pfault_fini(void)
  451. {
  452. struct pfault_refbk refbk = {
  453. .refdiagc = 0x258,
  454. .reffcode = 1,
  455. .refdwlen = 5,
  456. .refversn = 2,
  457. };
  458. if (pfault_disable)
  459. return;
  460. asm volatile(
  461. " diag %0,0,0x258\n"
  462. "0:\n"
  463. EX_TABLE(0b,0b)
  464. : : "a" (&refbk), "m" (refbk) : "cc");
  465. }
  466. static DEFINE_SPINLOCK(pfault_lock);
  467. static LIST_HEAD(pfault_list);
  468. static void pfault_interrupt(struct ext_code ext_code,
  469. unsigned int param32, unsigned long param64)
  470. {
  471. struct task_struct *tsk;
  472. __u16 subcode;
  473. pid_t pid;
  474. /*
  475. * Get the external interruption subcode & pfault
  476. * initial/completion signal bit. VM stores this
  477. * in the 'cpu address' field associated with the
  478. * external interrupt.
  479. */
  480. subcode = ext_code.subcode;
  481. if ((subcode & 0xff00) != __SUBCODE_MASK)
  482. return;
  483. inc_irq_stat(IRQEXT_PFL);
  484. /* Get the token (= pid of the affected task). */
  485. pid = sizeof(void *) == 4 ? param32 : param64;
  486. rcu_read_lock();
  487. tsk = find_task_by_pid_ns(pid, &init_pid_ns);
  488. if (tsk)
  489. get_task_struct(tsk);
  490. rcu_read_unlock();
  491. if (!tsk)
  492. return;
  493. spin_lock(&pfault_lock);
  494. if (subcode & 0x0080) {
  495. /* signal bit is set -> a page has been swapped in by VM */
  496. if (tsk->thread.pfault_wait == 1) {
  497. /* Initial interrupt was faster than the completion
  498. * interrupt. pfault_wait is valid. Set pfault_wait
  499. * back to zero and wake up the process. This can
  500. * safely be done because the task is still sleeping
  501. * and can't produce new pfaults. */
  502. tsk->thread.pfault_wait = 0;
  503. list_del(&tsk->thread.list);
  504. wake_up_process(tsk);
  505. put_task_struct(tsk);
  506. } else {
  507. /* Completion interrupt was faster than initial
  508. * interrupt. Set pfault_wait to -1 so the initial
  509. * interrupt doesn't put the task to sleep.
  510. * If the task is not running, ignore the completion
  511. * interrupt since it must be a leftover of a PFAULT
  512. * CANCEL operation which didn't remove all pending
  513. * completion interrupts. */
  514. if (tsk->state == TASK_RUNNING)
  515. tsk->thread.pfault_wait = -1;
  516. }
  517. } else {
  518. /* signal bit not set -> a real page is missing. */
  519. if (WARN_ON_ONCE(tsk != current))
  520. goto out;
  521. if (tsk->thread.pfault_wait == 1) {
  522. /* Already on the list with a reference: put to sleep */
  523. __set_task_state(tsk, TASK_UNINTERRUPTIBLE);
  524. set_tsk_need_resched(tsk);
  525. } else if (tsk->thread.pfault_wait == -1) {
  526. /* Completion interrupt was faster than the initial
  527. * interrupt (pfault_wait == -1). Set pfault_wait
  528. * back to zero and exit. */
  529. tsk->thread.pfault_wait = 0;
  530. } else {
  531. /* Initial interrupt arrived before completion
  532. * interrupt. Let the task sleep.
  533. * An extra task reference is needed since a different
  534. * cpu may set the task state to TASK_RUNNING again
  535. * before the scheduler is reached. */
  536. get_task_struct(tsk);
  537. tsk->thread.pfault_wait = 1;
  538. list_add(&tsk->thread.list, &pfault_list);
  539. __set_task_state(tsk, TASK_UNINTERRUPTIBLE);
  540. set_tsk_need_resched(tsk);
  541. }
  542. }
  543. out:
  544. spin_unlock(&pfault_lock);
  545. put_task_struct(tsk);
  546. }
  547. static int pfault_cpu_notify(struct notifier_block *self, unsigned long action,
  548. void *hcpu)
  549. {
  550. struct thread_struct *thread, *next;
  551. struct task_struct *tsk;
  552. switch (action & ~CPU_TASKS_FROZEN) {
  553. case CPU_DEAD:
  554. spin_lock_irq(&pfault_lock);
  555. list_for_each_entry_safe(thread, next, &pfault_list, list) {
  556. thread->pfault_wait = 0;
  557. list_del(&thread->list);
  558. tsk = container_of(thread, struct task_struct, thread);
  559. wake_up_process(tsk);
  560. put_task_struct(tsk);
  561. }
  562. spin_unlock_irq(&pfault_lock);
  563. break;
  564. default:
  565. break;
  566. }
  567. return NOTIFY_OK;
  568. }
  569. static int __init pfault_irq_init(void)
  570. {
  571. int rc;
  572. rc = register_external_irq(EXT_IRQ_CP_SERVICE, pfault_interrupt);
  573. if (rc)
  574. goto out_extint;
  575. rc = pfault_init() == 0 ? 0 : -EOPNOTSUPP;
  576. if (rc)
  577. goto out_pfault;
  578. irq_subclass_register(IRQ_SUBCLASS_SERVICE_SIGNAL);
  579. hotcpu_notifier(pfault_cpu_notify, 0);
  580. return 0;
  581. out_pfault:
  582. unregister_external_irq(EXT_IRQ_CP_SERVICE, pfault_interrupt);
  583. out_extint:
  584. pfault_disable = 1;
  585. return rc;
  586. }
  587. early_initcall(pfault_irq_init);
  588. #endif /* CONFIG_PFAULT */