fault.c 15 KB

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