fault.c 16 KB

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