fault.c 14 KB

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