fault.c 21 KB

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