fault.c 16 KB

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