fault.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794
  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("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 %04x ilc:%d ",
  243. regs->int_code & 0xffff, regs->int_code >> 17);
  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_SIGSEGV) {
  345. /* Kernel mode? Handle exceptions or die */
  346. if (!user_mode(regs))
  347. do_no_context(regs);
  348. else
  349. do_sigsegv(regs, SEGV_MAPERR);
  350. } else if (fault & VM_FAULT_SIGBUS) {
  351. /* Kernel mode? Handle exceptions or die */
  352. if (!user_mode(regs))
  353. do_no_context(regs);
  354. else
  355. do_sigbus(regs);
  356. } else
  357. BUG();
  358. break;
  359. }
  360. }
  361. /*
  362. * This routine handles page faults. It determines the address,
  363. * and the problem, and then passes it off to one of the appropriate
  364. * routines.
  365. *
  366. * interruption code (int_code):
  367. * 04 Protection -> Write-Protection (suprression)
  368. * 10 Segment translation -> Not present (nullification)
  369. * 11 Page translation -> Not present (nullification)
  370. * 3b Region third trans. -> Not present (nullification)
  371. */
  372. static inline int do_exception(struct pt_regs *regs, int access)
  373. {
  374. #ifdef CONFIG_PGSTE
  375. struct gmap *gmap;
  376. #endif
  377. struct task_struct *tsk;
  378. struct mm_struct *mm;
  379. struct vm_area_struct *vma;
  380. unsigned long trans_exc_code;
  381. unsigned long address;
  382. unsigned int flags;
  383. int fault;
  384. tsk = current;
  385. /*
  386. * The instruction that caused the program check has
  387. * been nullified. Don't signal single step via SIGTRAP.
  388. */
  389. clear_pt_regs_flag(regs, PIF_PER_TRAP);
  390. if (notify_page_fault(regs))
  391. return 0;
  392. mm = tsk->mm;
  393. trans_exc_code = regs->int_parm_long;
  394. /*
  395. * Verify that the fault happened in user space, that
  396. * we are not in an interrupt and that there is a
  397. * user context.
  398. */
  399. fault = VM_FAULT_BADCONTEXT;
  400. if (unlikely(!user_space_fault(regs) || in_atomic() || !mm))
  401. goto out;
  402. address = trans_exc_code & __FAIL_ADDR_MASK;
  403. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
  404. flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE;
  405. if (user_mode(regs))
  406. flags |= FAULT_FLAG_USER;
  407. if (access == VM_WRITE || (trans_exc_code & store_indication) == 0x400)
  408. flags |= FAULT_FLAG_WRITE;
  409. down_read(&mm->mmap_sem);
  410. #ifdef CONFIG_PGSTE
  411. gmap = (current->flags & PF_VCPU) ?
  412. (struct gmap *) S390_lowcore.gmap : NULL;
  413. if (gmap) {
  414. current->thread.gmap_addr = address;
  415. address = __gmap_translate(gmap, address);
  416. if (address == -EFAULT) {
  417. fault = VM_FAULT_BADMAP;
  418. goto out_up;
  419. }
  420. if (gmap->pfault_enabled)
  421. flags |= FAULT_FLAG_RETRY_NOWAIT;
  422. }
  423. #endif
  424. retry:
  425. fault = VM_FAULT_BADMAP;
  426. vma = find_vma(mm, address);
  427. if (!vma)
  428. goto out_up;
  429. if (unlikely(vma->vm_start > address)) {
  430. if (!(vma->vm_flags & VM_GROWSDOWN))
  431. goto out_up;
  432. if (expand_stack(vma, address))
  433. goto out_up;
  434. }
  435. /*
  436. * Ok, we have a good vm_area for this memory access, so
  437. * we can handle it..
  438. */
  439. fault = VM_FAULT_BADACCESS;
  440. if (unlikely(!(vma->vm_flags & access)))
  441. goto out_up;
  442. if (is_vm_hugetlb_page(vma))
  443. address &= HPAGE_MASK;
  444. /*
  445. * If for any reason at all we couldn't handle the fault,
  446. * make sure we exit gracefully rather than endlessly redo
  447. * the fault.
  448. */
  449. fault = handle_mm_fault(mm, vma, address, flags);
  450. /* No reason to continue if interrupted by SIGKILL. */
  451. if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(current)) {
  452. fault = VM_FAULT_SIGNAL;
  453. goto out;
  454. }
  455. if (unlikely(fault & VM_FAULT_ERROR))
  456. goto out_up;
  457. /*
  458. * Major/minor page fault accounting is only done on the
  459. * initial attempt. If we go through a retry, it is extremely
  460. * likely that the page will be found in page cache at that point.
  461. */
  462. if (flags & FAULT_FLAG_ALLOW_RETRY) {
  463. if (fault & VM_FAULT_MAJOR) {
  464. tsk->maj_flt++;
  465. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1,
  466. regs, address);
  467. } else {
  468. tsk->min_flt++;
  469. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1,
  470. regs, address);
  471. }
  472. if (fault & VM_FAULT_RETRY) {
  473. #ifdef CONFIG_PGSTE
  474. if (gmap && (flags & FAULT_FLAG_RETRY_NOWAIT)) {
  475. /* FAULT_FLAG_RETRY_NOWAIT has been set,
  476. * mmap_sem has not been released */
  477. current->thread.gmap_pfault = 1;
  478. fault = VM_FAULT_PFAULT;
  479. goto out_up;
  480. }
  481. #endif
  482. /* Clear FAULT_FLAG_ALLOW_RETRY to avoid any risk
  483. * of starvation. */
  484. flags &= ~(FAULT_FLAG_ALLOW_RETRY |
  485. FAULT_FLAG_RETRY_NOWAIT);
  486. flags |= FAULT_FLAG_TRIED;
  487. down_read(&mm->mmap_sem);
  488. goto retry;
  489. }
  490. }
  491. #ifdef CONFIG_PGSTE
  492. if (gmap) {
  493. address = __gmap_link(gmap, current->thread.gmap_addr,
  494. address);
  495. if (address == -EFAULT) {
  496. fault = VM_FAULT_BADMAP;
  497. goto out_up;
  498. }
  499. if (address == -ENOMEM) {
  500. fault = VM_FAULT_OOM;
  501. goto out_up;
  502. }
  503. }
  504. #endif
  505. fault = 0;
  506. out_up:
  507. up_read(&mm->mmap_sem);
  508. out:
  509. return fault;
  510. }
  511. void do_protection_exception(struct pt_regs *regs)
  512. {
  513. unsigned long trans_exc_code;
  514. int fault;
  515. trans_exc_code = regs->int_parm_long;
  516. /*
  517. * Protection exceptions are suppressing, decrement psw address.
  518. * The exception to this rule are aborted transactions, for these
  519. * the PSW already points to the correct location.
  520. */
  521. if (!(regs->int_code & 0x200))
  522. regs->psw.addr = __rewind_psw(regs->psw, regs->int_code >> 16);
  523. /*
  524. * Check for low-address protection. This needs to be treated
  525. * as a special case because the translation exception code
  526. * field is not guaranteed to contain valid data in this case.
  527. */
  528. if (unlikely(!(trans_exc_code & 4))) {
  529. do_low_address(regs);
  530. return;
  531. }
  532. fault = do_exception(regs, VM_WRITE);
  533. if (unlikely(fault))
  534. do_fault_error(regs, fault);
  535. }
  536. NOKPROBE_SYMBOL(do_protection_exception);
  537. void do_dat_exception(struct pt_regs *regs)
  538. {
  539. int access, fault;
  540. access = VM_READ | VM_EXEC | VM_WRITE;
  541. fault = do_exception(regs, access);
  542. if (unlikely(fault))
  543. do_fault_error(regs, fault);
  544. }
  545. NOKPROBE_SYMBOL(do_dat_exception);
  546. #ifdef CONFIG_PFAULT
  547. /*
  548. * 'pfault' pseudo page faults routines.
  549. */
  550. static int pfault_disable;
  551. static int __init nopfault(char *str)
  552. {
  553. pfault_disable = 1;
  554. return 1;
  555. }
  556. __setup("nopfault", nopfault);
  557. struct pfault_refbk {
  558. u16 refdiagc;
  559. u16 reffcode;
  560. u16 refdwlen;
  561. u16 refversn;
  562. u64 refgaddr;
  563. u64 refselmk;
  564. u64 refcmpmk;
  565. u64 reserved;
  566. } __attribute__ ((packed, aligned(8)));
  567. int pfault_init(void)
  568. {
  569. struct pfault_refbk refbk = {
  570. .refdiagc = 0x258,
  571. .reffcode = 0,
  572. .refdwlen = 5,
  573. .refversn = 2,
  574. .refgaddr = __LC_CURRENT_PID,
  575. .refselmk = 1ULL << 48,
  576. .refcmpmk = 1ULL << 48,
  577. .reserved = __PF_RES_FIELD };
  578. int rc;
  579. if (pfault_disable)
  580. return -1;
  581. asm volatile(
  582. " diag %1,%0,0x258\n"
  583. "0: j 2f\n"
  584. "1: la %0,8\n"
  585. "2:\n"
  586. EX_TABLE(0b,1b)
  587. : "=d" (rc) : "a" (&refbk), "m" (refbk) : "cc");
  588. return rc;
  589. }
  590. void pfault_fini(void)
  591. {
  592. struct pfault_refbk refbk = {
  593. .refdiagc = 0x258,
  594. .reffcode = 1,
  595. .refdwlen = 5,
  596. .refversn = 2,
  597. };
  598. if (pfault_disable)
  599. return;
  600. asm volatile(
  601. " diag %0,0,0x258\n"
  602. "0:\n"
  603. EX_TABLE(0b,0b)
  604. : : "a" (&refbk), "m" (refbk) : "cc");
  605. }
  606. static DEFINE_SPINLOCK(pfault_lock);
  607. static LIST_HEAD(pfault_list);
  608. static void pfault_interrupt(struct ext_code ext_code,
  609. unsigned int param32, unsigned long param64)
  610. {
  611. struct task_struct *tsk;
  612. __u16 subcode;
  613. pid_t pid;
  614. /*
  615. * Get the external interruption subcode & pfault
  616. * initial/completion signal bit. VM stores this
  617. * in the 'cpu address' field associated with the
  618. * external interrupt.
  619. */
  620. subcode = ext_code.subcode;
  621. if ((subcode & 0xff00) != __SUBCODE_MASK)
  622. return;
  623. inc_irq_stat(IRQEXT_PFL);
  624. /* Get the token (= pid of the affected task). */
  625. pid = sizeof(void *) == 4 ? param32 : param64;
  626. rcu_read_lock();
  627. tsk = find_task_by_pid_ns(pid, &init_pid_ns);
  628. if (tsk)
  629. get_task_struct(tsk);
  630. rcu_read_unlock();
  631. if (!tsk)
  632. return;
  633. spin_lock(&pfault_lock);
  634. if (subcode & 0x0080) {
  635. /* signal bit is set -> a page has been swapped in by VM */
  636. if (tsk->thread.pfault_wait == 1) {
  637. /* Initial interrupt was faster than the completion
  638. * interrupt. pfault_wait is valid. Set pfault_wait
  639. * back to zero and wake up the process. This can
  640. * safely be done because the task is still sleeping
  641. * and can't produce new pfaults. */
  642. tsk->thread.pfault_wait = 0;
  643. list_del(&tsk->thread.list);
  644. wake_up_process(tsk);
  645. put_task_struct(tsk);
  646. } else {
  647. /* Completion interrupt was faster than initial
  648. * interrupt. Set pfault_wait to -1 so the initial
  649. * interrupt doesn't put the task to sleep.
  650. * If the task is not running, ignore the completion
  651. * interrupt since it must be a leftover of a PFAULT
  652. * CANCEL operation which didn't remove all pending
  653. * completion interrupts. */
  654. if (tsk->state == TASK_RUNNING)
  655. tsk->thread.pfault_wait = -1;
  656. }
  657. } else {
  658. /* signal bit not set -> a real page is missing. */
  659. if (WARN_ON_ONCE(tsk != current))
  660. goto out;
  661. if (tsk->thread.pfault_wait == 1) {
  662. /* Already on the list with a reference: put to sleep */
  663. __set_task_state(tsk, TASK_UNINTERRUPTIBLE);
  664. set_tsk_need_resched(tsk);
  665. } else if (tsk->thread.pfault_wait == -1) {
  666. /* Completion interrupt was faster than the initial
  667. * interrupt (pfault_wait == -1). Set pfault_wait
  668. * back to zero and exit. */
  669. tsk->thread.pfault_wait = 0;
  670. } else {
  671. /* Initial interrupt arrived before completion
  672. * interrupt. Let the task sleep.
  673. * An extra task reference is needed since a different
  674. * cpu may set the task state to TASK_RUNNING again
  675. * before the scheduler is reached. */
  676. get_task_struct(tsk);
  677. tsk->thread.pfault_wait = 1;
  678. list_add(&tsk->thread.list, &pfault_list);
  679. __set_task_state(tsk, TASK_UNINTERRUPTIBLE);
  680. set_tsk_need_resched(tsk);
  681. }
  682. }
  683. out:
  684. spin_unlock(&pfault_lock);
  685. put_task_struct(tsk);
  686. }
  687. static int pfault_cpu_notify(struct notifier_block *self, unsigned long action,
  688. void *hcpu)
  689. {
  690. struct thread_struct *thread, *next;
  691. struct task_struct *tsk;
  692. switch (action & ~CPU_TASKS_FROZEN) {
  693. case CPU_DEAD:
  694. spin_lock_irq(&pfault_lock);
  695. list_for_each_entry_safe(thread, next, &pfault_list, list) {
  696. thread->pfault_wait = 0;
  697. list_del(&thread->list);
  698. tsk = container_of(thread, struct task_struct, thread);
  699. wake_up_process(tsk);
  700. put_task_struct(tsk);
  701. }
  702. spin_unlock_irq(&pfault_lock);
  703. break;
  704. default:
  705. break;
  706. }
  707. return NOTIFY_OK;
  708. }
  709. static int __init pfault_irq_init(void)
  710. {
  711. int rc;
  712. rc = register_external_irq(EXT_IRQ_CP_SERVICE, pfault_interrupt);
  713. if (rc)
  714. goto out_extint;
  715. rc = pfault_init() == 0 ? 0 : -EOPNOTSUPP;
  716. if (rc)
  717. goto out_pfault;
  718. irq_subclass_register(IRQ_SUBCLASS_SERVICE_SIGNAL);
  719. hotcpu_notifier(pfault_cpu_notify, 0);
  720. return 0;
  721. out_pfault:
  722. unregister_external_irq(EXT_IRQ_CP_SERVICE, pfault_interrupt);
  723. out_extint:
  724. pfault_disable = 1;
  725. return rc;
  726. }
  727. early_initcall(pfault_irq_init);
  728. #endif /* CONFIG_PFAULT */