fault.c 16 KB

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