fault.c 16 KB

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