fault.c 15 KB

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