fault.c 16 KB

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