fault.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  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/s390_ext.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. pagefault_out_of_memory();
  227. else if (fault & VM_FAULT_SIGBUS) {
  228. /* Kernel mode? Handle exceptions or die */
  229. if (!(regs->psw.mask & PSW_MASK_PSTATE))
  230. do_no_context(regs, int_code, trans_exc_code);
  231. else
  232. do_sigbus(regs, int_code, trans_exc_code);
  233. } else
  234. BUG();
  235. break;
  236. }
  237. }
  238. /*
  239. * This routine handles page faults. It determines the address,
  240. * and the problem, and then passes it off to one of the appropriate
  241. * routines.
  242. *
  243. * interruption code (int_code):
  244. * 04 Protection -> Write-Protection (suprression)
  245. * 10 Segment translation -> Not present (nullification)
  246. * 11 Page translation -> Not present (nullification)
  247. * 3b Region third trans. -> Not present (nullification)
  248. */
  249. static inline int do_exception(struct pt_regs *regs, int access,
  250. unsigned long trans_exc_code)
  251. {
  252. struct task_struct *tsk;
  253. struct mm_struct *mm;
  254. struct vm_area_struct *vma;
  255. unsigned long address;
  256. int fault, write;
  257. if (notify_page_fault(regs))
  258. return 0;
  259. tsk = current;
  260. mm = tsk->mm;
  261. /*
  262. * Verify that the fault happened in user space, that
  263. * we are not in an interrupt and that there is a
  264. * user context.
  265. */
  266. fault = VM_FAULT_BADCONTEXT;
  267. if (unlikely(!user_space_fault(trans_exc_code) || in_atomic() || !mm))
  268. goto out;
  269. address = trans_exc_code & __FAIL_ADDR_MASK;
  270. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, 0, regs, address);
  271. down_read(&mm->mmap_sem);
  272. fault = VM_FAULT_BADMAP;
  273. vma = find_vma(mm, address);
  274. if (!vma)
  275. goto out_up;
  276. if (unlikely(vma->vm_start > address)) {
  277. if (!(vma->vm_flags & VM_GROWSDOWN))
  278. goto out_up;
  279. if (expand_stack(vma, address))
  280. goto out_up;
  281. }
  282. /*
  283. * Ok, we have a good vm_area for this memory access, so
  284. * we can handle it..
  285. */
  286. fault = VM_FAULT_BADACCESS;
  287. if (unlikely(!(vma->vm_flags & access)))
  288. goto out_up;
  289. if (is_vm_hugetlb_page(vma))
  290. address &= HPAGE_MASK;
  291. /*
  292. * If for any reason at all we couldn't handle the fault,
  293. * make sure we exit gracefully rather than endlessly redo
  294. * the fault.
  295. */
  296. write = (access == VM_WRITE ||
  297. (trans_exc_code & store_indication) == 0x400) ?
  298. FAULT_FLAG_WRITE : 0;
  299. fault = handle_mm_fault(mm, vma, address, write);
  300. if (unlikely(fault & VM_FAULT_ERROR))
  301. goto out_up;
  302. if (fault & VM_FAULT_MAJOR) {
  303. tsk->maj_flt++;
  304. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1, 0,
  305. regs, address);
  306. } else {
  307. tsk->min_flt++;
  308. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1, 0,
  309. regs, address);
  310. }
  311. /*
  312. * The instruction that caused the program check will
  313. * be repeated. Don't signal single step via SIGTRAP.
  314. */
  315. clear_tsk_thread_flag(tsk, TIF_PER_TRAP);
  316. fault = 0;
  317. out_up:
  318. up_read(&mm->mmap_sem);
  319. out:
  320. return fault;
  321. }
  322. void __kprobes do_protection_exception(struct pt_regs *regs, long pgm_int_code,
  323. unsigned long trans_exc_code)
  324. {
  325. int fault;
  326. /* Protection exception is suppressing, decrement psw address. */
  327. regs->psw.addr -= (pgm_int_code >> 16);
  328. /*
  329. * Check for low-address protection. This needs to be treated
  330. * as a special case because the translation exception code
  331. * field is not guaranteed to contain valid data in this case.
  332. */
  333. if (unlikely(!(trans_exc_code & 4))) {
  334. do_low_address(regs, pgm_int_code, trans_exc_code);
  335. return;
  336. }
  337. fault = do_exception(regs, VM_WRITE, trans_exc_code);
  338. if (unlikely(fault))
  339. do_fault_error(regs, 4, trans_exc_code, fault);
  340. }
  341. void __kprobes do_dat_exception(struct pt_regs *regs, long pgm_int_code,
  342. unsigned long trans_exc_code)
  343. {
  344. int access, fault;
  345. access = VM_READ | VM_EXEC | VM_WRITE;
  346. fault = do_exception(regs, access, trans_exc_code);
  347. if (unlikely(fault))
  348. do_fault_error(regs, pgm_int_code & 255, trans_exc_code, fault);
  349. }
  350. #ifdef CONFIG_64BIT
  351. void __kprobes do_asce_exception(struct pt_regs *regs, long pgm_int_code,
  352. unsigned long trans_exc_code)
  353. {
  354. struct mm_struct *mm = current->mm;
  355. struct vm_area_struct *vma;
  356. if (unlikely(!user_space_fault(trans_exc_code) || in_atomic() || !mm))
  357. goto no_context;
  358. down_read(&mm->mmap_sem);
  359. vma = find_vma(mm, trans_exc_code & __FAIL_ADDR_MASK);
  360. up_read(&mm->mmap_sem);
  361. if (vma) {
  362. update_mm(mm, current);
  363. return;
  364. }
  365. /* User mode accesses just cause a SIGSEGV */
  366. if (regs->psw.mask & PSW_MASK_PSTATE) {
  367. do_sigsegv(regs, pgm_int_code, SEGV_MAPERR, trans_exc_code);
  368. return;
  369. }
  370. no_context:
  371. do_no_context(regs, pgm_int_code, trans_exc_code);
  372. }
  373. #endif
  374. int __handle_fault(unsigned long uaddr, unsigned long pgm_int_code, int write)
  375. {
  376. struct pt_regs regs;
  377. int access, fault;
  378. regs.psw.mask = psw_kernel_bits;
  379. if (!irqs_disabled())
  380. regs.psw.mask |= PSW_MASK_IO | PSW_MASK_EXT;
  381. regs.psw.addr = (unsigned long) __builtin_return_address(0);
  382. regs.psw.addr |= PSW_ADDR_AMODE;
  383. uaddr &= PAGE_MASK;
  384. access = write ? VM_WRITE : VM_READ;
  385. fault = do_exception(&regs, access, uaddr | 2);
  386. if (unlikely(fault)) {
  387. if (fault & VM_FAULT_OOM) {
  388. pagefault_out_of_memory();
  389. fault = 0;
  390. } else if (fault & VM_FAULT_SIGBUS)
  391. do_sigbus(&regs, pgm_int_code, uaddr);
  392. }
  393. return fault ? -EFAULT : 0;
  394. }
  395. #ifdef CONFIG_PFAULT
  396. /*
  397. * 'pfault' pseudo page faults routines.
  398. */
  399. static int pfault_disable;
  400. static int __init nopfault(char *str)
  401. {
  402. pfault_disable = 1;
  403. return 1;
  404. }
  405. __setup("nopfault", nopfault);
  406. struct pfault_refbk {
  407. u16 refdiagc;
  408. u16 reffcode;
  409. u16 refdwlen;
  410. u16 refversn;
  411. u64 refgaddr;
  412. u64 refselmk;
  413. u64 refcmpmk;
  414. u64 reserved;
  415. } __attribute__ ((packed, aligned(8)));
  416. int pfault_init(void)
  417. {
  418. struct pfault_refbk refbk = {
  419. .refdiagc = 0x258,
  420. .reffcode = 0,
  421. .refdwlen = 5,
  422. .refversn = 2,
  423. .refgaddr = __LC_CURRENT_PID,
  424. .refselmk = 1ULL << 48,
  425. .refcmpmk = 1ULL << 48,
  426. .reserved = __PF_RES_FIELD };
  427. int rc;
  428. if (!MACHINE_IS_VM || pfault_disable)
  429. return -1;
  430. asm volatile(
  431. " diag %1,%0,0x258\n"
  432. "0: j 2f\n"
  433. "1: la %0,8\n"
  434. "2:\n"
  435. EX_TABLE(0b,1b)
  436. : "=d" (rc) : "a" (&refbk), "m" (refbk) : "cc");
  437. return rc;
  438. }
  439. void pfault_fini(void)
  440. {
  441. struct pfault_refbk refbk = {
  442. .refdiagc = 0x258,
  443. .reffcode = 1,
  444. .refdwlen = 5,
  445. .refversn = 2,
  446. };
  447. if (!MACHINE_IS_VM || pfault_disable)
  448. return;
  449. asm volatile(
  450. " diag %0,0,0x258\n"
  451. "0:\n"
  452. EX_TABLE(0b,0b)
  453. : : "a" (&refbk), "m" (refbk) : "cc");
  454. }
  455. static DEFINE_SPINLOCK(pfault_lock);
  456. static LIST_HEAD(pfault_list);
  457. static void pfault_interrupt(unsigned int ext_int_code,
  458. unsigned int param32, unsigned long param64)
  459. {
  460. struct task_struct *tsk;
  461. __u16 subcode;
  462. pid_t pid;
  463. /*
  464. * Get the external interruption subcode & pfault
  465. * initial/completion signal bit. VM stores this
  466. * in the 'cpu address' field associated with the
  467. * external interrupt.
  468. */
  469. subcode = ext_int_code >> 16;
  470. if ((subcode & 0xff00) != __SUBCODE_MASK)
  471. return;
  472. kstat_cpu(smp_processor_id()).irqs[EXTINT_PFL]++;
  473. if (subcode & 0x0080) {
  474. /* Get the token (= pid of the affected task). */
  475. pid = sizeof(void *) == 4 ? param32 : param64;
  476. rcu_read_lock();
  477. tsk = find_task_by_pid_ns(pid, &init_pid_ns);
  478. if (tsk)
  479. get_task_struct(tsk);
  480. rcu_read_unlock();
  481. if (!tsk)
  482. return;
  483. } else {
  484. tsk = current;
  485. }
  486. spin_lock(&pfault_lock);
  487. if (subcode & 0x0080) {
  488. /* signal bit is set -> a page has been swapped in by VM */
  489. if (tsk->thread.pfault_wait == 1) {
  490. /* Initial interrupt was faster than the completion
  491. * interrupt. pfault_wait is valid. Set pfault_wait
  492. * back to zero and wake up the process. This can
  493. * safely be done because the task is still sleeping
  494. * and can't produce new pfaults. */
  495. tsk->thread.pfault_wait = 0;
  496. list_del(&tsk->thread.list);
  497. wake_up_process(tsk);
  498. } else {
  499. /* Completion interrupt was faster than initial
  500. * interrupt. Set pfault_wait to -1 so the initial
  501. * interrupt doesn't put the task to sleep. */
  502. tsk->thread.pfault_wait = -1;
  503. }
  504. put_task_struct(tsk);
  505. } else {
  506. /* signal bit not set -> a real page is missing. */
  507. if (tsk->thread.pfault_wait == -1) {
  508. /* Completion interrupt was faster than the initial
  509. * interrupt (pfault_wait == -1). Set pfault_wait
  510. * back to zero and exit. */
  511. tsk->thread.pfault_wait = 0;
  512. } else {
  513. /* Initial interrupt arrived before completion
  514. * interrupt. Let the task sleep. */
  515. tsk->thread.pfault_wait = 1;
  516. list_add(&tsk->thread.list, &pfault_list);
  517. set_task_state(tsk, TASK_UNINTERRUPTIBLE);
  518. set_tsk_need_resched(tsk);
  519. }
  520. }
  521. spin_unlock(&pfault_lock);
  522. }
  523. static int __cpuinit pfault_cpu_notify(struct notifier_block *self,
  524. unsigned long action, void *hcpu)
  525. {
  526. struct thread_struct *thread, *next;
  527. struct task_struct *tsk;
  528. switch (action) {
  529. case CPU_DEAD:
  530. case CPU_DEAD_FROZEN:
  531. spin_lock_irq(&pfault_lock);
  532. list_for_each_entry_safe(thread, next, &pfault_list, list) {
  533. thread->pfault_wait = 0;
  534. list_del(&thread->list);
  535. tsk = container_of(thread, struct task_struct, thread);
  536. wake_up_process(tsk);
  537. }
  538. spin_unlock_irq(&pfault_lock);
  539. break;
  540. default:
  541. break;
  542. }
  543. return NOTIFY_OK;
  544. }
  545. static int __init pfault_irq_init(void)
  546. {
  547. int rc;
  548. if (!MACHINE_IS_VM)
  549. return 0;
  550. rc = register_external_interrupt(0x2603, pfault_interrupt);
  551. if (rc)
  552. goto out_extint;
  553. rc = pfault_init() == 0 ? 0 : -EOPNOTSUPP;
  554. if (rc)
  555. goto out_pfault;
  556. service_subclass_irq_register();
  557. hotcpu_notifier(pfault_cpu_notify, 0);
  558. return 0;
  559. out_pfault:
  560. unregister_external_interrupt(0x2603, pfault_interrupt);
  561. out_extint:
  562. pfault_disable = 1;
  563. return rc;
  564. }
  565. early_initcall(pfault_irq_init);
  566. #endif /* CONFIG_PFAULT */