fault.c 16 KB

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