fault.c 15 KB

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