fault.c 15 KB

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