fault.c 15 KB

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