fault.c 21 KB

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