fault.c 16 KB

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