fault.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786
  1. /*
  2. * S390 version
  3. * Copyright IBM Corp. 1999
  4. * Author(s): Hartmut Penner (hp@de.ibm.com)
  5. * Ulrich Weigand (uweigand@de.ibm.com)
  6. *
  7. * Derived from "arch/i386/mm/fault.c"
  8. * Copyright (C) 1995 Linus Torvalds
  9. */
  10. #include <linux/kernel_stat.h>
  11. #include <linux/perf_event.h>
  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/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/asm-offsets.h>
  32. #include <asm/pgtable.h>
  33. #include <asm/irq.h>
  34. #include <asm/mmu_context.h>
  35. #include <asm/facility.h>
  36. #include "../kernel/entry.h"
  37. #ifndef CONFIG_64BIT
  38. #define __FAIL_ADDR_MASK 0x7ffff000
  39. #define __SUBCODE_MASK 0x0200
  40. #define __PF_RES_FIELD 0ULL
  41. #else /* CONFIG_64BIT */
  42. #define __FAIL_ADDR_MASK -4096L
  43. #define __SUBCODE_MASK 0x0600
  44. #define __PF_RES_FIELD 0x8000000000000000ULL
  45. #endif /* CONFIG_64BIT */
  46. #define VM_FAULT_BADCONTEXT 0x010000
  47. #define VM_FAULT_BADMAP 0x020000
  48. #define VM_FAULT_BADACCESS 0x040000
  49. #define VM_FAULT_SIGNAL 0x080000
  50. #define VM_FAULT_PFAULT 0x100000
  51. static unsigned long store_indication __read_mostly;
  52. #ifdef CONFIG_64BIT
  53. static int __init fault_init(void)
  54. {
  55. if (test_facility(75))
  56. store_indication = 0xc00;
  57. return 0;
  58. }
  59. early_initcall(fault_init);
  60. #endif
  61. static inline int notify_page_fault(struct pt_regs *regs)
  62. {
  63. int ret = 0;
  64. /* kprobe_running() needs smp_processor_id() */
  65. if (kprobes_built_in() && !user_mode(regs)) {
  66. preempt_disable();
  67. if (kprobe_running() && kprobe_fault_handler(regs, 14))
  68. ret = 1;
  69. preempt_enable();
  70. }
  71. return ret;
  72. }
  73. /*
  74. * Unlock any spinlocks which will prevent us from getting the
  75. * message out.
  76. */
  77. void bust_spinlocks(int yes)
  78. {
  79. if (yes) {
  80. oops_in_progress = 1;
  81. } else {
  82. int loglevel_save = console_loglevel;
  83. console_unblank();
  84. oops_in_progress = 0;
  85. /*
  86. * OK, the message is on the console. Now we call printk()
  87. * without oops_in_progress set so that printk will give klogd
  88. * a poke. Hold onto your hats...
  89. */
  90. console_loglevel = 15;
  91. printk(" ");
  92. console_loglevel = loglevel_save;
  93. }
  94. }
  95. /*
  96. * Returns the address space associated with the fault.
  97. * Returns 0 for kernel space and 1 for user space.
  98. */
  99. static inline int user_space_fault(struct pt_regs *regs)
  100. {
  101. unsigned long trans_exc_code;
  102. /*
  103. * The lowest two bits of the translation exception
  104. * identification indicate which paging table was used.
  105. */
  106. trans_exc_code = regs->int_parm_long & 3;
  107. if (trans_exc_code == 3) /* home space -> kernel */
  108. return 0;
  109. if (user_mode(regs))
  110. return 1;
  111. if (trans_exc_code == 2) /* secondary space -> set_fs */
  112. return current->thread.mm_segment.ar4;
  113. if (current->flags & PF_VCPU)
  114. return 1;
  115. return 0;
  116. }
  117. static int bad_address(void *p)
  118. {
  119. unsigned long dummy;
  120. return probe_kernel_address((unsigned long *)p, dummy);
  121. }
  122. #ifdef CONFIG_64BIT
  123. static void dump_pagetable(unsigned long asce, unsigned long address)
  124. {
  125. unsigned long *table = __va(asce & PAGE_MASK);
  126. pr_alert("AS:%016lx ", asce);
  127. switch (asce & _ASCE_TYPE_MASK) {
  128. case _ASCE_TYPE_REGION1:
  129. table = table + ((address >> 53) & 0x7ff);
  130. if (bad_address(table))
  131. goto bad;
  132. pr_cont("R1:%016lx ", *table);
  133. if (*table & _REGION_ENTRY_INVALID)
  134. goto out;
  135. table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
  136. /* fallthrough */
  137. case _ASCE_TYPE_REGION2:
  138. table = table + ((address >> 42) & 0x7ff);
  139. if (bad_address(table))
  140. goto bad;
  141. pr_cont("R2:%016lx ", *table);
  142. if (*table & _REGION_ENTRY_INVALID)
  143. goto out;
  144. table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
  145. /* fallthrough */
  146. case _ASCE_TYPE_REGION3:
  147. table = table + ((address >> 31) & 0x7ff);
  148. if (bad_address(table))
  149. goto bad;
  150. pr_cont("R3:%016lx ", *table);
  151. if (*table & (_REGION_ENTRY_INVALID | _REGION3_ENTRY_LARGE))
  152. goto out;
  153. table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
  154. /* fallthrough */
  155. case _ASCE_TYPE_SEGMENT:
  156. table = table + ((address >> 20) & 0x7ff);
  157. if (bad_address(table))
  158. goto bad;
  159. pr_cont(KERN_CONT "S:%016lx ", *table);
  160. if (*table & (_SEGMENT_ENTRY_INVALID | _SEGMENT_ENTRY_LARGE))
  161. goto out;
  162. table = (unsigned long *)(*table & _SEGMENT_ENTRY_ORIGIN);
  163. }
  164. table = table + ((address >> 12) & 0xff);
  165. if (bad_address(table))
  166. goto bad;
  167. pr_cont("P:%016lx ", *table);
  168. out:
  169. pr_cont("\n");
  170. return;
  171. bad:
  172. pr_cont("BAD\n");
  173. }
  174. #else /* CONFIG_64BIT */
  175. static void dump_pagetable(unsigned long asce, unsigned long address)
  176. {
  177. unsigned long *table = __va(asce & PAGE_MASK);
  178. pr_alert("AS:%08lx ", asce);
  179. table = table + ((address >> 20) & 0x7ff);
  180. if (bad_address(table))
  181. goto bad;
  182. pr_cont("S:%08lx ", *table);
  183. if (*table & _SEGMENT_ENTRY_INVALID)
  184. goto out;
  185. table = (unsigned long *)(*table & _SEGMENT_ENTRY_ORIGIN);
  186. table = table + ((address >> 12) & 0xff);
  187. if (bad_address(table))
  188. goto bad;
  189. pr_cont("P:%08lx ", *table);
  190. out:
  191. pr_cont("\n");
  192. return;
  193. bad:
  194. pr_cont("BAD\n");
  195. }
  196. #endif /* CONFIG_64BIT */
  197. static void dump_fault_info(struct pt_regs *regs)
  198. {
  199. unsigned long asce;
  200. pr_alert("Fault in ");
  201. switch (regs->int_parm_long & 3) {
  202. case 3:
  203. pr_cont("home space ");
  204. break;
  205. case 2:
  206. pr_cont("secondary space ");
  207. break;
  208. case 1:
  209. pr_cont("access register ");
  210. break;
  211. case 0:
  212. pr_cont("primary space ");
  213. break;
  214. }
  215. pr_cont("mode while using ");
  216. if (!user_space_fault(regs)) {
  217. asce = S390_lowcore.kernel_asce;
  218. pr_cont("kernel ");
  219. }
  220. #ifdef CONFIG_PGSTE
  221. else if ((current->flags & PF_VCPU) && S390_lowcore.gmap) {
  222. struct gmap *gmap = (struct gmap *)S390_lowcore.gmap;
  223. asce = gmap->asce;
  224. pr_cont("gmap ");
  225. }
  226. #endif
  227. else {
  228. asce = S390_lowcore.user_asce;
  229. pr_cont("user ");
  230. }
  231. pr_cont("ASCE.\n");
  232. dump_pagetable(asce, regs->int_parm_long & __FAIL_ADDR_MASK);
  233. }
  234. static inline void report_user_fault(struct pt_regs *regs, long signr)
  235. {
  236. if ((task_pid_nr(current) > 1) && !show_unhandled_signals)
  237. return;
  238. if (!unhandled_signal(current, signr))
  239. return;
  240. if (!printk_ratelimit())
  241. return;
  242. printk(KERN_ALERT "User process fault: interruption code 0x%X ",
  243. regs->int_code);
  244. print_vma_addr(KERN_CONT "in ", regs->psw.addr & PSW_ADDR_INSN);
  245. printk(KERN_CONT "\n");
  246. printk(KERN_ALERT "failing address: %016lx TEID: %016lx\n",
  247. regs->int_parm_long & __FAIL_ADDR_MASK, regs->int_parm_long);
  248. dump_fault_info(regs);
  249. show_regs(regs);
  250. }
  251. /*
  252. * Send SIGSEGV to task. This is an external routine
  253. * to keep the stack usage of do_page_fault small.
  254. */
  255. static noinline void do_sigsegv(struct pt_regs *regs, int si_code)
  256. {
  257. struct siginfo si;
  258. report_user_fault(regs, SIGSEGV);
  259. si.si_signo = SIGSEGV;
  260. si.si_code = si_code;
  261. si.si_addr = (void __user *)(regs->int_parm_long & __FAIL_ADDR_MASK);
  262. force_sig_info(SIGSEGV, &si, current);
  263. }
  264. static noinline void do_no_context(struct pt_regs *regs)
  265. {
  266. const struct exception_table_entry *fixup;
  267. unsigned long address;
  268. /* Are we prepared to handle this kernel fault? */
  269. fixup = search_exception_tables(regs->psw.addr & PSW_ADDR_INSN);
  270. if (fixup) {
  271. regs->psw.addr = extable_fixup(fixup) | PSW_ADDR_AMODE;
  272. return;
  273. }
  274. /*
  275. * Oops. The kernel tried to access some bad page. We'll have to
  276. * terminate things with extreme prejudice.
  277. */
  278. address = regs->int_parm_long & __FAIL_ADDR_MASK;
  279. if (!user_space_fault(regs))
  280. printk(KERN_ALERT "Unable to handle kernel pointer dereference"
  281. " in virtual kernel address space\n");
  282. else
  283. printk(KERN_ALERT "Unable to handle kernel paging request"
  284. " in virtual user address space\n");
  285. printk(KERN_ALERT "failing address: %016lx TEID: %016lx\n",
  286. regs->int_parm_long & __FAIL_ADDR_MASK, regs->int_parm_long);
  287. dump_fault_info(regs);
  288. die(regs, "Oops");
  289. do_exit(SIGKILL);
  290. }
  291. static noinline void do_low_address(struct pt_regs *regs)
  292. {
  293. /* Low-address protection hit in kernel mode means
  294. NULL pointer write access in kernel mode. */
  295. if (regs->psw.mask & PSW_MASK_PSTATE) {
  296. /* Low-address protection hit in user mode 'cannot happen'. */
  297. die (regs, "Low-address protection");
  298. do_exit(SIGKILL);
  299. }
  300. do_no_context(regs);
  301. }
  302. static noinline void do_sigbus(struct pt_regs *regs)
  303. {
  304. struct task_struct *tsk = current;
  305. struct siginfo si;
  306. /*
  307. * Send a sigbus, regardless of whether we were in kernel
  308. * or user mode.
  309. */
  310. si.si_signo = SIGBUS;
  311. si.si_errno = 0;
  312. si.si_code = BUS_ADRERR;
  313. si.si_addr = (void __user *)(regs->int_parm_long & __FAIL_ADDR_MASK);
  314. force_sig_info(SIGBUS, &si, tsk);
  315. }
  316. static noinline void do_fault_error(struct pt_regs *regs, int fault)
  317. {
  318. int si_code;
  319. switch (fault) {
  320. case VM_FAULT_BADACCESS:
  321. case VM_FAULT_BADMAP:
  322. /* Bad memory access. Check if it is kernel or user space. */
  323. if (user_mode(regs)) {
  324. /* User mode accesses just cause a SIGSEGV */
  325. si_code = (fault == VM_FAULT_BADMAP) ?
  326. SEGV_MAPERR : SEGV_ACCERR;
  327. do_sigsegv(regs, si_code);
  328. return;
  329. }
  330. case VM_FAULT_BADCONTEXT:
  331. case VM_FAULT_PFAULT:
  332. do_no_context(regs);
  333. break;
  334. case VM_FAULT_SIGNAL:
  335. if (!user_mode(regs))
  336. do_no_context(regs);
  337. break;
  338. default: /* fault & VM_FAULT_ERROR */
  339. if (fault & VM_FAULT_OOM) {
  340. if (!user_mode(regs))
  341. do_no_context(regs);
  342. else
  343. pagefault_out_of_memory();
  344. } else if (fault & VM_FAULT_SIGBUS) {
  345. /* Kernel mode? Handle exceptions or die */
  346. if (!user_mode(regs))
  347. do_no_context(regs);
  348. else
  349. do_sigbus(regs);
  350. } else
  351. BUG();
  352. break;
  353. }
  354. }
  355. /*
  356. * This routine handles page faults. It determines the address,
  357. * and the problem, and then passes it off to one of the appropriate
  358. * routines.
  359. *
  360. * interruption code (int_code):
  361. * 04 Protection -> Write-Protection (suprression)
  362. * 10 Segment translation -> Not present (nullification)
  363. * 11 Page translation -> Not present (nullification)
  364. * 3b Region third trans. -> Not present (nullification)
  365. */
  366. static inline int do_exception(struct pt_regs *regs, int access)
  367. {
  368. #ifdef CONFIG_PGSTE
  369. struct gmap *gmap;
  370. #endif
  371. struct task_struct *tsk;
  372. struct mm_struct *mm;
  373. struct vm_area_struct *vma;
  374. unsigned long trans_exc_code;
  375. unsigned long address;
  376. unsigned int flags;
  377. int fault;
  378. tsk = current;
  379. /*
  380. * The instruction that caused the program check has
  381. * been nullified. Don't signal single step via SIGTRAP.
  382. */
  383. clear_pt_regs_flag(regs, PIF_PER_TRAP);
  384. if (notify_page_fault(regs))
  385. return 0;
  386. mm = tsk->mm;
  387. trans_exc_code = regs->int_parm_long;
  388. /*
  389. * Verify that the fault happened in user space, that
  390. * we are not in an interrupt and that there is a
  391. * user context.
  392. */
  393. fault = VM_FAULT_BADCONTEXT;
  394. if (unlikely(!user_space_fault(regs) || in_atomic() || !mm))
  395. goto out;
  396. address = trans_exc_code & __FAIL_ADDR_MASK;
  397. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
  398. flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE;
  399. if (user_mode(regs))
  400. flags |= FAULT_FLAG_USER;
  401. if (access == VM_WRITE || (trans_exc_code & store_indication) == 0x400)
  402. flags |= FAULT_FLAG_WRITE;
  403. down_read(&mm->mmap_sem);
  404. #ifdef CONFIG_PGSTE
  405. gmap = (current->flags & PF_VCPU) ?
  406. (struct gmap *) S390_lowcore.gmap : NULL;
  407. if (gmap) {
  408. current->thread.gmap_addr = address;
  409. address = __gmap_translate(gmap, address);
  410. if (address == -EFAULT) {
  411. fault = VM_FAULT_BADMAP;
  412. goto out_up;
  413. }
  414. if (gmap->pfault_enabled)
  415. flags |= FAULT_FLAG_RETRY_NOWAIT;
  416. }
  417. #endif
  418. retry:
  419. fault = VM_FAULT_BADMAP;
  420. vma = find_vma(mm, address);
  421. if (!vma)
  422. goto out_up;
  423. if (unlikely(vma->vm_start > address)) {
  424. if (!(vma->vm_flags & VM_GROWSDOWN))
  425. goto out_up;
  426. if (expand_stack(vma, address))
  427. goto out_up;
  428. }
  429. /*
  430. * Ok, we have a good vm_area for this memory access, so
  431. * we can handle it..
  432. */
  433. fault = VM_FAULT_BADACCESS;
  434. if (unlikely(!(vma->vm_flags & access)))
  435. goto out_up;
  436. if (is_vm_hugetlb_page(vma))
  437. address &= HPAGE_MASK;
  438. /*
  439. * If for any reason at all we couldn't handle the fault,
  440. * make sure we exit gracefully rather than endlessly redo
  441. * the fault.
  442. */
  443. fault = handle_mm_fault(mm, vma, address, flags);
  444. /* No reason to continue if interrupted by SIGKILL. */
  445. if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(current)) {
  446. fault = VM_FAULT_SIGNAL;
  447. goto out;
  448. }
  449. if (unlikely(fault & VM_FAULT_ERROR))
  450. goto out_up;
  451. /*
  452. * Major/minor page fault accounting is only done on the
  453. * initial attempt. If we go through a retry, it is extremely
  454. * likely that the page will be found in page cache at that point.
  455. */
  456. if (flags & FAULT_FLAG_ALLOW_RETRY) {
  457. if (fault & VM_FAULT_MAJOR) {
  458. tsk->maj_flt++;
  459. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1,
  460. regs, address);
  461. } else {
  462. tsk->min_flt++;
  463. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1,
  464. regs, address);
  465. }
  466. if (fault & VM_FAULT_RETRY) {
  467. #ifdef CONFIG_PGSTE
  468. if (gmap && (flags & FAULT_FLAG_RETRY_NOWAIT)) {
  469. /* FAULT_FLAG_RETRY_NOWAIT has been set,
  470. * mmap_sem has not been released */
  471. current->thread.gmap_pfault = 1;
  472. fault = VM_FAULT_PFAULT;
  473. goto out_up;
  474. }
  475. #endif
  476. /* Clear FAULT_FLAG_ALLOW_RETRY to avoid any risk
  477. * of starvation. */
  478. flags &= ~(FAULT_FLAG_ALLOW_RETRY |
  479. FAULT_FLAG_RETRY_NOWAIT);
  480. flags |= FAULT_FLAG_TRIED;
  481. down_read(&mm->mmap_sem);
  482. goto retry;
  483. }
  484. }
  485. #ifdef CONFIG_PGSTE
  486. if (gmap) {
  487. address = __gmap_link(gmap, current->thread.gmap_addr,
  488. address);
  489. if (address == -EFAULT) {
  490. fault = VM_FAULT_BADMAP;
  491. goto out_up;
  492. }
  493. if (address == -ENOMEM) {
  494. fault = VM_FAULT_OOM;
  495. goto out_up;
  496. }
  497. }
  498. #endif
  499. fault = 0;
  500. out_up:
  501. up_read(&mm->mmap_sem);
  502. out:
  503. return fault;
  504. }
  505. void __kprobes do_protection_exception(struct pt_regs *regs)
  506. {
  507. unsigned long trans_exc_code;
  508. int fault;
  509. trans_exc_code = regs->int_parm_long;
  510. /*
  511. * Protection exceptions are suppressing, decrement psw address.
  512. * The exception to this rule are aborted transactions, for these
  513. * the PSW already points to the correct location.
  514. */
  515. if (!(regs->int_code & 0x200))
  516. regs->psw.addr = __rewind_psw(regs->psw, regs->int_code >> 16);
  517. /*
  518. * Check for low-address protection. This needs to be treated
  519. * as a special case because the translation exception code
  520. * field is not guaranteed to contain valid data in this case.
  521. */
  522. if (unlikely(!(trans_exc_code & 4))) {
  523. do_low_address(regs);
  524. return;
  525. }
  526. fault = do_exception(regs, VM_WRITE);
  527. if (unlikely(fault))
  528. do_fault_error(regs, fault);
  529. }
  530. void __kprobes do_dat_exception(struct pt_regs *regs)
  531. {
  532. int access, fault;
  533. access = VM_READ | VM_EXEC | VM_WRITE;
  534. fault = do_exception(regs, access);
  535. if (unlikely(fault))
  536. do_fault_error(regs, fault);
  537. }
  538. #ifdef CONFIG_PFAULT
  539. /*
  540. * 'pfault' pseudo page faults routines.
  541. */
  542. static int pfault_disable;
  543. static int __init nopfault(char *str)
  544. {
  545. pfault_disable = 1;
  546. return 1;
  547. }
  548. __setup("nopfault", nopfault);
  549. struct pfault_refbk {
  550. u16 refdiagc;
  551. u16 reffcode;
  552. u16 refdwlen;
  553. u16 refversn;
  554. u64 refgaddr;
  555. u64 refselmk;
  556. u64 refcmpmk;
  557. u64 reserved;
  558. } __attribute__ ((packed, aligned(8)));
  559. int pfault_init(void)
  560. {
  561. struct pfault_refbk refbk = {
  562. .refdiagc = 0x258,
  563. .reffcode = 0,
  564. .refdwlen = 5,
  565. .refversn = 2,
  566. .refgaddr = __LC_CURRENT_PID,
  567. .refselmk = 1ULL << 48,
  568. .refcmpmk = 1ULL << 48,
  569. .reserved = __PF_RES_FIELD };
  570. int rc;
  571. if (pfault_disable)
  572. return -1;
  573. asm volatile(
  574. " diag %1,%0,0x258\n"
  575. "0: j 2f\n"
  576. "1: la %0,8\n"
  577. "2:\n"
  578. EX_TABLE(0b,1b)
  579. : "=d" (rc) : "a" (&refbk), "m" (refbk) : "cc");
  580. return rc;
  581. }
  582. void pfault_fini(void)
  583. {
  584. struct pfault_refbk refbk = {
  585. .refdiagc = 0x258,
  586. .reffcode = 1,
  587. .refdwlen = 5,
  588. .refversn = 2,
  589. };
  590. if (pfault_disable)
  591. return;
  592. asm volatile(
  593. " diag %0,0,0x258\n"
  594. "0:\n"
  595. EX_TABLE(0b,0b)
  596. : : "a" (&refbk), "m" (refbk) : "cc");
  597. }
  598. static DEFINE_SPINLOCK(pfault_lock);
  599. static LIST_HEAD(pfault_list);
  600. static void pfault_interrupt(struct ext_code ext_code,
  601. unsigned int param32, unsigned long param64)
  602. {
  603. struct task_struct *tsk;
  604. __u16 subcode;
  605. pid_t pid;
  606. /*
  607. * Get the external interruption subcode & pfault
  608. * initial/completion signal bit. VM stores this
  609. * in the 'cpu address' field associated with the
  610. * external interrupt.
  611. */
  612. subcode = ext_code.subcode;
  613. if ((subcode & 0xff00) != __SUBCODE_MASK)
  614. return;
  615. inc_irq_stat(IRQEXT_PFL);
  616. /* Get the token (= pid of the affected task). */
  617. pid = sizeof(void *) == 4 ? param32 : param64;
  618. rcu_read_lock();
  619. tsk = find_task_by_pid_ns(pid, &init_pid_ns);
  620. if (tsk)
  621. get_task_struct(tsk);
  622. rcu_read_unlock();
  623. if (!tsk)
  624. return;
  625. spin_lock(&pfault_lock);
  626. if (subcode & 0x0080) {
  627. /* signal bit is set -> a page has been swapped in by VM */
  628. if (tsk->thread.pfault_wait == 1) {
  629. /* Initial interrupt was faster than the completion
  630. * interrupt. pfault_wait is valid. Set pfault_wait
  631. * back to zero and wake up the process. This can
  632. * safely be done because the task is still sleeping
  633. * and can't produce new pfaults. */
  634. tsk->thread.pfault_wait = 0;
  635. list_del(&tsk->thread.list);
  636. wake_up_process(tsk);
  637. put_task_struct(tsk);
  638. } else {
  639. /* Completion interrupt was faster than initial
  640. * interrupt. Set pfault_wait to -1 so the initial
  641. * interrupt doesn't put the task to sleep.
  642. * If the task is not running, ignore the completion
  643. * interrupt since it must be a leftover of a PFAULT
  644. * CANCEL operation which didn't remove all pending
  645. * completion interrupts. */
  646. if (tsk->state == TASK_RUNNING)
  647. tsk->thread.pfault_wait = -1;
  648. }
  649. } else {
  650. /* signal bit not set -> a real page is missing. */
  651. if (WARN_ON_ONCE(tsk != current))
  652. goto out;
  653. if (tsk->thread.pfault_wait == 1) {
  654. /* Already on the list with a reference: put to sleep */
  655. __set_task_state(tsk, TASK_UNINTERRUPTIBLE);
  656. set_tsk_need_resched(tsk);
  657. } else if (tsk->thread.pfault_wait == -1) {
  658. /* Completion interrupt was faster than the initial
  659. * interrupt (pfault_wait == -1). Set pfault_wait
  660. * back to zero and exit. */
  661. tsk->thread.pfault_wait = 0;
  662. } else {
  663. /* Initial interrupt arrived before completion
  664. * interrupt. Let the task sleep.
  665. * An extra task reference is needed since a different
  666. * cpu may set the task state to TASK_RUNNING again
  667. * before the scheduler is reached. */
  668. get_task_struct(tsk);
  669. tsk->thread.pfault_wait = 1;
  670. list_add(&tsk->thread.list, &pfault_list);
  671. __set_task_state(tsk, TASK_UNINTERRUPTIBLE);
  672. set_tsk_need_resched(tsk);
  673. }
  674. }
  675. out:
  676. spin_unlock(&pfault_lock);
  677. put_task_struct(tsk);
  678. }
  679. static int pfault_cpu_notify(struct notifier_block *self, unsigned long action,
  680. void *hcpu)
  681. {
  682. struct thread_struct *thread, *next;
  683. struct task_struct *tsk;
  684. switch (action & ~CPU_TASKS_FROZEN) {
  685. case CPU_DEAD:
  686. spin_lock_irq(&pfault_lock);
  687. list_for_each_entry_safe(thread, next, &pfault_list, list) {
  688. thread->pfault_wait = 0;
  689. list_del(&thread->list);
  690. tsk = container_of(thread, struct task_struct, thread);
  691. wake_up_process(tsk);
  692. put_task_struct(tsk);
  693. }
  694. spin_unlock_irq(&pfault_lock);
  695. break;
  696. default:
  697. break;
  698. }
  699. return NOTIFY_OK;
  700. }
  701. static int __init pfault_irq_init(void)
  702. {
  703. int rc;
  704. rc = register_external_irq(EXT_IRQ_CP_SERVICE, pfault_interrupt);
  705. if (rc)
  706. goto out_extint;
  707. rc = pfault_init() == 0 ? 0 : -EOPNOTSUPP;
  708. if (rc)
  709. goto out_pfault;
  710. irq_subclass_register(IRQ_SUBCLASS_SERVICE_SIGNAL);
  711. hotcpu_notifier(pfault_cpu_notify, 0);
  712. return 0;
  713. out_pfault:
  714. unregister_external_irq(EXT_IRQ_CP_SERVICE, pfault_interrupt);
  715. out_extint:
  716. pfault_disable = 1;
  717. return rc;
  718. }
  719. early_initcall(pfault_irq_init);
  720. #endif /* CONFIG_PFAULT */