fault.c 15 KB

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