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. * 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. struct siginfo si;
  248. report_user_fault(regs, SIGSEGV, 1);
  249. si.si_signo = SIGSEGV;
  250. si.si_errno = 0;
  251. si.si_code = si_code;
  252. si.si_addr = (void __user *)(regs->int_parm_long & __FAIL_ADDR_MASK);
  253. force_sig_info(SIGSEGV, &si, 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. struct task_struct *tsk = current;
  292. struct siginfo si;
  293. /*
  294. * Send a sigbus, regardless of whether we were in kernel
  295. * or user mode.
  296. */
  297. si.si_signo = SIGBUS;
  298. si.si_errno = 0;
  299. si.si_code = BUS_ADRERR;
  300. si.si_addr = (void __user *)(regs->int_parm_long & __FAIL_ADDR_MASK);
  301. force_sig_info(SIGBUS, &si, tsk);
  302. }
  303. static noinline int signal_return(struct pt_regs *regs)
  304. {
  305. u16 instruction;
  306. int rc;
  307. rc = __get_user(instruction, (u16 __user *) regs->psw.addr);
  308. if (rc)
  309. return rc;
  310. if (instruction == 0x0a77) {
  311. set_pt_regs_flag(regs, PIF_SYSCALL);
  312. regs->int_code = 0x00040077;
  313. return 0;
  314. } else if (instruction == 0x0aad) {
  315. set_pt_regs_flag(regs, PIF_SYSCALL);
  316. regs->int_code = 0x000400ad;
  317. return 0;
  318. }
  319. return -EACCES;
  320. }
  321. static noinline void do_fault_error(struct pt_regs *regs, int access, int fault)
  322. {
  323. int si_code;
  324. switch (fault) {
  325. case VM_FAULT_BADACCESS:
  326. if (access == VM_EXEC && signal_return(regs) == 0)
  327. break;
  328. case VM_FAULT_BADMAP:
  329. /* Bad memory access. Check if it is kernel or user space. */
  330. if (user_mode(regs)) {
  331. /* User mode accesses just cause a SIGSEGV */
  332. si_code = (fault == VM_FAULT_BADMAP) ?
  333. SEGV_MAPERR : SEGV_ACCERR;
  334. do_sigsegv(regs, si_code);
  335. break;
  336. }
  337. case VM_FAULT_BADCONTEXT:
  338. case VM_FAULT_PFAULT:
  339. do_no_context(regs);
  340. break;
  341. case VM_FAULT_SIGNAL:
  342. if (!user_mode(regs))
  343. do_no_context(regs);
  344. break;
  345. default: /* fault & VM_FAULT_ERROR */
  346. if (fault & VM_FAULT_OOM) {
  347. if (!user_mode(regs))
  348. do_no_context(regs);
  349. else
  350. pagefault_out_of_memory();
  351. } else if (fault & VM_FAULT_SIGSEGV) {
  352. /* Kernel mode? Handle exceptions or die */
  353. if (!user_mode(regs))
  354. do_no_context(regs);
  355. else
  356. do_sigsegv(regs, SEGV_MAPERR);
  357. } else if (fault & VM_FAULT_SIGBUS) {
  358. /* Kernel mode? Handle exceptions or die */
  359. if (!user_mode(regs))
  360. do_no_context(regs);
  361. else
  362. do_sigbus(regs);
  363. } else
  364. BUG();
  365. break;
  366. }
  367. }
  368. /*
  369. * This routine handles page faults. It determines the address,
  370. * and the problem, and then passes it off to one of the appropriate
  371. * routines.
  372. *
  373. * interruption code (int_code):
  374. * 04 Protection -> Write-Protection (suprression)
  375. * 10 Segment translation -> Not present (nullification)
  376. * 11 Page translation -> Not present (nullification)
  377. * 3b Region third trans. -> Not present (nullification)
  378. */
  379. static inline int do_exception(struct pt_regs *regs, int access)
  380. {
  381. struct gmap *gmap;
  382. struct task_struct *tsk;
  383. struct mm_struct *mm;
  384. struct vm_area_struct *vma;
  385. enum fault_type type;
  386. unsigned long trans_exc_code;
  387. unsigned long address;
  388. unsigned int flags;
  389. int fault;
  390. tsk = current;
  391. /*
  392. * The instruction that caused the program check has
  393. * been nullified. Don't signal single step via SIGTRAP.
  394. */
  395. clear_pt_regs_flag(regs, PIF_PER_TRAP);
  396. if (notify_page_fault(regs))
  397. return 0;
  398. mm = tsk->mm;
  399. trans_exc_code = regs->int_parm_long;
  400. /*
  401. * Verify that the fault happened in user space, that
  402. * we are not in an interrupt and that there is a
  403. * user context.
  404. */
  405. fault = VM_FAULT_BADCONTEXT;
  406. type = get_fault_type(regs);
  407. switch (type) {
  408. case KERNEL_FAULT:
  409. goto out;
  410. case VDSO_FAULT:
  411. fault = VM_FAULT_BADMAP;
  412. goto out;
  413. case USER_FAULT:
  414. case GMAP_FAULT:
  415. if (faulthandler_disabled() || !mm)
  416. goto out;
  417. break;
  418. }
  419. address = trans_exc_code & __FAIL_ADDR_MASK;
  420. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
  421. flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE;
  422. if (user_mode(regs))
  423. flags |= FAULT_FLAG_USER;
  424. if (access == VM_WRITE || (trans_exc_code & store_indication) == 0x400)
  425. flags |= FAULT_FLAG_WRITE;
  426. down_read(&mm->mmap_sem);
  427. gmap = NULL;
  428. if (IS_ENABLED(CONFIG_PGSTE) && type == GMAP_FAULT) {
  429. gmap = (struct gmap *) S390_lowcore.gmap;
  430. current->thread.gmap_addr = address;
  431. current->thread.gmap_write_flag = !!(flags & FAULT_FLAG_WRITE);
  432. current->thread.gmap_int_code = regs->int_code & 0xffff;
  433. address = __gmap_translate(gmap, address);
  434. if (address == -EFAULT) {
  435. fault = VM_FAULT_BADMAP;
  436. goto out_up;
  437. }
  438. if (gmap->pfault_enabled)
  439. flags |= FAULT_FLAG_RETRY_NOWAIT;
  440. }
  441. retry:
  442. fault = VM_FAULT_BADMAP;
  443. vma = find_vma(mm, address);
  444. if (!vma)
  445. goto out_up;
  446. if (unlikely(vma->vm_start > address)) {
  447. if (!(vma->vm_flags & VM_GROWSDOWN))
  448. goto out_up;
  449. if (expand_stack(vma, address))
  450. goto out_up;
  451. }
  452. /*
  453. * Ok, we have a good vm_area for this memory access, so
  454. * we can handle it..
  455. */
  456. fault = VM_FAULT_BADACCESS;
  457. if (unlikely(!(vma->vm_flags & access)))
  458. goto out_up;
  459. if (is_vm_hugetlb_page(vma))
  460. address &= HPAGE_MASK;
  461. /*
  462. * If for any reason at all we couldn't handle the fault,
  463. * make sure we exit gracefully rather than endlessly redo
  464. * the fault.
  465. */
  466. fault = handle_mm_fault(vma, address, flags);
  467. /* No reason to continue if interrupted by SIGKILL. */
  468. if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(current)) {
  469. fault = VM_FAULT_SIGNAL;
  470. goto out;
  471. }
  472. if (unlikely(fault & VM_FAULT_ERROR))
  473. goto out_up;
  474. /*
  475. * Major/minor page fault accounting is only done on the
  476. * initial attempt. If we go through a retry, it is extremely
  477. * likely that the page will be found in page cache at that point.
  478. */
  479. if (flags & FAULT_FLAG_ALLOW_RETRY) {
  480. if (fault & VM_FAULT_MAJOR) {
  481. tsk->maj_flt++;
  482. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1,
  483. regs, address);
  484. } else {
  485. tsk->min_flt++;
  486. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1,
  487. regs, address);
  488. }
  489. if (fault & VM_FAULT_RETRY) {
  490. if (IS_ENABLED(CONFIG_PGSTE) && gmap &&
  491. (flags & FAULT_FLAG_RETRY_NOWAIT)) {
  492. /* FAULT_FLAG_RETRY_NOWAIT has been set,
  493. * mmap_sem has not been released */
  494. current->thread.gmap_pfault = 1;
  495. fault = VM_FAULT_PFAULT;
  496. goto out_up;
  497. }
  498. /* Clear FAULT_FLAG_ALLOW_RETRY to avoid any risk
  499. * of starvation. */
  500. flags &= ~(FAULT_FLAG_ALLOW_RETRY |
  501. FAULT_FLAG_RETRY_NOWAIT);
  502. flags |= FAULT_FLAG_TRIED;
  503. down_read(&mm->mmap_sem);
  504. goto retry;
  505. }
  506. }
  507. if (IS_ENABLED(CONFIG_PGSTE) && gmap) {
  508. address = __gmap_link(gmap, current->thread.gmap_addr,
  509. address);
  510. if (address == -EFAULT) {
  511. fault = VM_FAULT_BADMAP;
  512. goto out_up;
  513. }
  514. if (address == -ENOMEM) {
  515. fault = VM_FAULT_OOM;
  516. goto out_up;
  517. }
  518. }
  519. fault = 0;
  520. out_up:
  521. up_read(&mm->mmap_sem);
  522. out:
  523. return fault;
  524. }
  525. void do_protection_exception(struct pt_regs *regs)
  526. {
  527. unsigned long trans_exc_code;
  528. int access, 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, fault;
  562. access = VM_READ | VM_EXEC | VM_WRITE;
  563. fault = do_exception(regs, access);
  564. if (unlikely(fault))
  565. do_fault_error(regs, access, fault);
  566. }
  567. NOKPROBE_SYMBOL(do_dat_exception);
  568. #ifdef CONFIG_PFAULT
  569. /*
  570. * 'pfault' pseudo page faults routines.
  571. */
  572. static int pfault_disable;
  573. static int __init nopfault(char *str)
  574. {
  575. pfault_disable = 1;
  576. return 1;
  577. }
  578. __setup("nopfault", nopfault);
  579. struct pfault_refbk {
  580. u16 refdiagc;
  581. u16 reffcode;
  582. u16 refdwlen;
  583. u16 refversn;
  584. u64 refgaddr;
  585. u64 refselmk;
  586. u64 refcmpmk;
  587. u64 reserved;
  588. } __attribute__ ((packed, aligned(8)));
  589. int pfault_init(void)
  590. {
  591. struct pfault_refbk refbk = {
  592. .refdiagc = 0x258,
  593. .reffcode = 0,
  594. .refdwlen = 5,
  595. .refversn = 2,
  596. .refgaddr = __LC_LPP,
  597. .refselmk = 1ULL << 48,
  598. .refcmpmk = 1ULL << 48,
  599. .reserved = __PF_RES_FIELD };
  600. int rc;
  601. if (pfault_disable)
  602. return -1;
  603. diag_stat_inc(DIAG_STAT_X258);
  604. asm volatile(
  605. " diag %1,%0,0x258\n"
  606. "0: j 2f\n"
  607. "1: la %0,8\n"
  608. "2:\n"
  609. EX_TABLE(0b,1b)
  610. : "=d" (rc) : "a" (&refbk), "m" (refbk) : "cc");
  611. return rc;
  612. }
  613. void pfault_fini(void)
  614. {
  615. struct pfault_refbk refbk = {
  616. .refdiagc = 0x258,
  617. .reffcode = 1,
  618. .refdwlen = 5,
  619. .refversn = 2,
  620. };
  621. if (pfault_disable)
  622. return;
  623. diag_stat_inc(DIAG_STAT_X258);
  624. asm volatile(
  625. " diag %0,0,0x258\n"
  626. "0: nopr %%r7\n"
  627. EX_TABLE(0b,0b)
  628. : : "a" (&refbk), "m" (refbk) : "cc");
  629. }
  630. static DEFINE_SPINLOCK(pfault_lock);
  631. static LIST_HEAD(pfault_list);
  632. #define PF_COMPLETE 0x0080
  633. /*
  634. * The mechanism of our pfault code: if Linux is running as guest, runs a user
  635. * space process and the user space process accesses a page that the host has
  636. * paged out we get a pfault interrupt.
  637. *
  638. * This allows us, within the guest, to schedule a different process. Without
  639. * this mechanism the host would have to suspend the whole virtual cpu until
  640. * the page has been paged in.
  641. *
  642. * So when we get such an interrupt then we set the state of the current task
  643. * to uninterruptible and also set the need_resched flag. Both happens within
  644. * interrupt context(!). If we later on want to return to user space we
  645. * recognize the need_resched flag and then call schedule(). It's not very
  646. * obvious how this works...
  647. *
  648. * Of course we have a lot of additional fun with the completion interrupt (->
  649. * host signals that a page of a process has been paged in and the process can
  650. * continue to run). This interrupt can arrive on any cpu and, since we have
  651. * virtual cpus, actually appear before the interrupt that signals that a page
  652. * is missing.
  653. */
  654. static void pfault_interrupt(struct ext_code ext_code,
  655. unsigned int param32, unsigned long param64)
  656. {
  657. struct task_struct *tsk;
  658. __u16 subcode;
  659. pid_t pid;
  660. /*
  661. * Get the external interruption subcode & pfault initial/completion
  662. * signal bit. VM stores this in the 'cpu address' field associated
  663. * with the external interrupt.
  664. */
  665. subcode = ext_code.subcode;
  666. if ((subcode & 0xff00) != __SUBCODE_MASK)
  667. return;
  668. inc_irq_stat(IRQEXT_PFL);
  669. /* Get the token (= pid of the affected task). */
  670. pid = param64 & LPP_PFAULT_PID_MASK;
  671. rcu_read_lock();
  672. tsk = find_task_by_pid_ns(pid, &init_pid_ns);
  673. if (tsk)
  674. get_task_struct(tsk);
  675. rcu_read_unlock();
  676. if (!tsk)
  677. return;
  678. spin_lock(&pfault_lock);
  679. if (subcode & PF_COMPLETE) {
  680. /* signal bit is set -> a page has been swapped in by VM */
  681. if (tsk->thread.pfault_wait == 1) {
  682. /* Initial interrupt was faster than the completion
  683. * interrupt. pfault_wait is valid. Set pfault_wait
  684. * back to zero and wake up the process. This can
  685. * safely be done because the task is still sleeping
  686. * and can't produce new pfaults. */
  687. tsk->thread.pfault_wait = 0;
  688. list_del(&tsk->thread.list);
  689. wake_up_process(tsk);
  690. put_task_struct(tsk);
  691. } else {
  692. /* Completion interrupt was faster than initial
  693. * interrupt. Set pfault_wait to -1 so the initial
  694. * interrupt doesn't put the task to sleep.
  695. * If the task is not running, ignore the completion
  696. * interrupt since it must be a leftover of a PFAULT
  697. * CANCEL operation which didn't remove all pending
  698. * completion interrupts. */
  699. if (tsk->state == TASK_RUNNING)
  700. tsk->thread.pfault_wait = -1;
  701. }
  702. } else {
  703. /* signal bit not set -> a real page is missing. */
  704. if (WARN_ON_ONCE(tsk != current))
  705. goto out;
  706. if (tsk->thread.pfault_wait == 1) {
  707. /* Already on the list with a reference: put to sleep */
  708. goto block;
  709. } else if (tsk->thread.pfault_wait == -1) {
  710. /* Completion interrupt was faster than the initial
  711. * interrupt (pfault_wait == -1). Set pfault_wait
  712. * back to zero and exit. */
  713. tsk->thread.pfault_wait = 0;
  714. } else {
  715. /* Initial interrupt arrived before completion
  716. * interrupt. Let the task sleep.
  717. * An extra task reference is needed since a different
  718. * cpu may set the task state to TASK_RUNNING again
  719. * before the scheduler is reached. */
  720. get_task_struct(tsk);
  721. tsk->thread.pfault_wait = 1;
  722. list_add(&tsk->thread.list, &pfault_list);
  723. block:
  724. /* Since this must be a userspace fault, there
  725. * is no kernel task state to trample. Rely on the
  726. * return to userspace schedule() to block. */
  727. __set_current_state(TASK_UNINTERRUPTIBLE);
  728. set_tsk_need_resched(tsk);
  729. set_preempt_need_resched();
  730. }
  731. }
  732. out:
  733. spin_unlock(&pfault_lock);
  734. put_task_struct(tsk);
  735. }
  736. static int pfault_cpu_dead(unsigned int cpu)
  737. {
  738. struct thread_struct *thread, *next;
  739. struct task_struct *tsk;
  740. spin_lock_irq(&pfault_lock);
  741. list_for_each_entry_safe(thread, next, &pfault_list, list) {
  742. thread->pfault_wait = 0;
  743. list_del(&thread->list);
  744. tsk = container_of(thread, struct task_struct, thread);
  745. wake_up_process(tsk);
  746. put_task_struct(tsk);
  747. }
  748. spin_unlock_irq(&pfault_lock);
  749. return 0;
  750. }
  751. static int __init pfault_irq_init(void)
  752. {
  753. int rc;
  754. rc = register_external_irq(EXT_IRQ_CP_SERVICE, pfault_interrupt);
  755. if (rc)
  756. goto out_extint;
  757. rc = pfault_init() == 0 ? 0 : -EOPNOTSUPP;
  758. if (rc)
  759. goto out_pfault;
  760. irq_subclass_register(IRQ_SUBCLASS_SERVICE_SIGNAL);
  761. cpuhp_setup_state_nocalls(CPUHP_S390_PFAULT_DEAD, "s390/pfault:dead",
  762. NULL, pfault_cpu_dead);
  763. return 0;
  764. out_pfault:
  765. unregister_external_irq(EXT_IRQ_CP_SERVICE, pfault_interrupt);
  766. out_extint:
  767. pfault_disable = 1;
  768. return rc;
  769. }
  770. early_initcall(pfault_irq_init);
  771. #endif /* CONFIG_PFAULT */