fault.c 15 KB

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