fault.c 21 KB

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