fault.c 21 KB

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