fault.c 15 KB

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