fault.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * S390 version
  4. * Copyright IBM Corp. 1999
  5. * Author(s): Hartmut Penner ([email protected])
  6. * Ulrich Weigand ([email protected])
  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 <linux/kfence.h>
  34. #include <asm/asm-extable.h>
  35. #include <asm/asm-offsets.h>
  36. #include <asm/diag.h>
  37. #include <asm/gmap.h>
  38. #include <asm/irq.h>
  39. #include <asm/mmu_context.h>
  40. #include <asm/facility.h>
  41. #include <asm/uv.h>
  42. #include "../kernel/entry.h"
  43. #define __FAIL_ADDR_MASK -4096L
  44. #define __SUBCODE_MASK 0x0600
  45. #define __PF_RES_FIELD 0x8000000000000000ULL
  46. #define VM_FAULT_BADCONTEXT ((__force vm_fault_t) 0x010000)
  47. #define VM_FAULT_BADMAP ((__force vm_fault_t) 0x020000)
  48. #define VM_FAULT_BADACCESS ((__force vm_fault_t) 0x040000)
  49. #define VM_FAULT_SIGNAL ((__force vm_fault_t) 0x080000)
  50. #define VM_FAULT_PFAULT ((__force vm_fault_t) 0x100000)
  51. enum fault_type {
  52. KERNEL_FAULT,
  53. USER_FAULT,
  54. GMAP_FAULT,
  55. };
  56. static unsigned long store_indication __read_mostly;
  57. static int __init fault_init(void)
  58. {
  59. if (test_facility(75))
  60. store_indication = 0xc00;
  61. return 0;
  62. }
  63. early_initcall(fault_init);
  64. /*
  65. * Find out which address space caused the exception.
  66. */
  67. static enum fault_type get_fault_type(struct pt_regs *regs)
  68. {
  69. unsigned long trans_exc_code;
  70. trans_exc_code = regs->int_parm_long & 3;
  71. if (likely(trans_exc_code == 0)) {
  72. /* primary space exception */
  73. if (user_mode(regs))
  74. return USER_FAULT;
  75. if (!IS_ENABLED(CONFIG_PGSTE))
  76. return KERNEL_FAULT;
  77. if (test_pt_regs_flag(regs, PIF_GUEST_FAULT))
  78. return GMAP_FAULT;
  79. return KERNEL_FAULT;
  80. }
  81. if (trans_exc_code == 2)
  82. return USER_FAULT;
  83. if (trans_exc_code == 1) {
  84. /* access register mode, not used in the kernel */
  85. return USER_FAULT;
  86. }
  87. /* home space exception -> access via kernel ASCE */
  88. return KERNEL_FAULT;
  89. }
  90. static unsigned long get_fault_address(struct pt_regs *regs)
  91. {
  92. unsigned long trans_exc_code = regs->int_parm_long;
  93. return trans_exc_code & __FAIL_ADDR_MASK;
  94. }
  95. static bool fault_is_write(struct pt_regs *regs)
  96. {
  97. unsigned long trans_exc_code = regs->int_parm_long;
  98. return (trans_exc_code & store_indication) == 0x400;
  99. }
  100. static int bad_address(void *p)
  101. {
  102. unsigned long dummy;
  103. return get_kernel_nofault(dummy, (unsigned long *)p);
  104. }
  105. static void dump_pagetable(unsigned long asce, unsigned long address)
  106. {
  107. unsigned long *table = __va(asce & _ASCE_ORIGIN);
  108. pr_alert("AS:%016lx ", asce);
  109. switch (asce & _ASCE_TYPE_MASK) {
  110. case _ASCE_TYPE_REGION1:
  111. table += (address & _REGION1_INDEX) >> _REGION1_SHIFT;
  112. if (bad_address(table))
  113. goto bad;
  114. pr_cont("R1:%016lx ", *table);
  115. if (*table & _REGION_ENTRY_INVALID)
  116. goto out;
  117. table = __va(*table & _REGION_ENTRY_ORIGIN);
  118. fallthrough;
  119. case _ASCE_TYPE_REGION2:
  120. table += (address & _REGION2_INDEX) >> _REGION2_SHIFT;
  121. if (bad_address(table))
  122. goto bad;
  123. pr_cont("R2:%016lx ", *table);
  124. if (*table & _REGION_ENTRY_INVALID)
  125. goto out;
  126. table = __va(*table & _REGION_ENTRY_ORIGIN);
  127. fallthrough;
  128. case _ASCE_TYPE_REGION3:
  129. table += (address & _REGION3_INDEX) >> _REGION3_SHIFT;
  130. if (bad_address(table))
  131. goto bad;
  132. pr_cont("R3:%016lx ", *table);
  133. if (*table & (_REGION_ENTRY_INVALID | _REGION3_ENTRY_LARGE))
  134. goto out;
  135. table = __va(*table & _REGION_ENTRY_ORIGIN);
  136. fallthrough;
  137. case _ASCE_TYPE_SEGMENT:
  138. table += (address & _SEGMENT_INDEX) >> _SEGMENT_SHIFT;
  139. if (bad_address(table))
  140. goto bad;
  141. pr_cont("S:%016lx ", *table);
  142. if (*table & (_SEGMENT_ENTRY_INVALID | _SEGMENT_ENTRY_LARGE))
  143. goto out;
  144. table = __va(*table & _SEGMENT_ENTRY_ORIGIN);
  145. }
  146. table += (address & _PAGE_INDEX) >> _PAGE_SHIFT;
  147. if (bad_address(table))
  148. goto bad;
  149. pr_cont("P:%016lx ", *table);
  150. out:
  151. pr_cont("\n");
  152. return;
  153. bad:
  154. pr_cont("BAD\n");
  155. }
  156. static void dump_fault_info(struct pt_regs *regs)
  157. {
  158. unsigned long asce;
  159. pr_alert("Failing address: %016lx TEID: %016lx\n",
  160. regs->int_parm_long & __FAIL_ADDR_MASK, regs->int_parm_long);
  161. pr_alert("Fault in ");
  162. switch (regs->int_parm_long & 3) {
  163. case 3:
  164. pr_cont("home space ");
  165. break;
  166. case 2:
  167. pr_cont("secondary space ");
  168. break;
  169. case 1:
  170. pr_cont("access register ");
  171. break;
  172. case 0:
  173. pr_cont("primary space ");
  174. break;
  175. }
  176. pr_cont("mode while using ");
  177. switch (get_fault_type(regs)) {
  178. case USER_FAULT:
  179. asce = S390_lowcore.user_asce;
  180. pr_cont("user ");
  181. break;
  182. case GMAP_FAULT:
  183. asce = ((struct gmap *) S390_lowcore.gmap)->asce;
  184. pr_cont("gmap ");
  185. break;
  186. case KERNEL_FAULT:
  187. asce = S390_lowcore.kernel_asce;
  188. pr_cont("kernel ");
  189. break;
  190. default:
  191. unreachable();
  192. }
  193. pr_cont("ASCE.\n");
  194. dump_pagetable(asce, regs->int_parm_long & __FAIL_ADDR_MASK);
  195. }
  196. int show_unhandled_signals = 1;
  197. void report_user_fault(struct pt_regs *regs, long signr, int is_mm_fault)
  198. {
  199. if ((task_pid_nr(current) > 1) && !show_unhandled_signals)
  200. return;
  201. if (!unhandled_signal(current, signr))
  202. return;
  203. if (!printk_ratelimit())
  204. return;
  205. printk(KERN_ALERT "User process fault: interruption code %04x ilc:%d ",
  206. regs->int_code & 0xffff, regs->int_code >> 17);
  207. print_vma_addr(KERN_CONT "in ", regs->psw.addr);
  208. printk(KERN_CONT "\n");
  209. if (is_mm_fault)
  210. dump_fault_info(regs);
  211. show_regs(regs);
  212. }
  213. /*
  214. * Send SIGSEGV to task. This is an external routine
  215. * to keep the stack usage of do_page_fault small.
  216. */
  217. static noinline void do_sigsegv(struct pt_regs *regs, int si_code)
  218. {
  219. report_user_fault(regs, SIGSEGV, 1);
  220. force_sig_fault(SIGSEGV, si_code,
  221. (void __user *)(regs->int_parm_long & __FAIL_ADDR_MASK));
  222. }
  223. static noinline void do_no_context(struct pt_regs *regs, vm_fault_t fault)
  224. {
  225. enum fault_type fault_type;
  226. unsigned long address;
  227. bool is_write;
  228. if (fixup_exception(regs))
  229. return;
  230. fault_type = get_fault_type(regs);
  231. if ((fault_type == KERNEL_FAULT) && (fault == VM_FAULT_BADCONTEXT)) {
  232. address = get_fault_address(regs);
  233. is_write = fault_is_write(regs);
  234. if (kfence_handle_page_fault(address, is_write, regs))
  235. return;
  236. }
  237. /*
  238. * Oops. The kernel tried to access some bad page. We'll have to
  239. * terminate things with extreme prejudice.
  240. */
  241. if (fault_type == KERNEL_FAULT)
  242. printk(KERN_ALERT "Unable to handle kernel pointer dereference"
  243. " in virtual kernel address space\n");
  244. else
  245. printk(KERN_ALERT "Unable to handle kernel paging request"
  246. " in virtual user address space\n");
  247. dump_fault_info(regs);
  248. die(regs, "Oops");
  249. }
  250. static noinline void do_low_address(struct pt_regs *regs)
  251. {
  252. /* Low-address protection hit in kernel mode means
  253. NULL pointer write access in kernel mode. */
  254. if (regs->psw.mask & PSW_MASK_PSTATE) {
  255. /* Low-address protection hit in user mode 'cannot happen'. */
  256. die (regs, "Low-address protection");
  257. }
  258. do_no_context(regs, VM_FAULT_BADACCESS);
  259. }
  260. static noinline void do_sigbus(struct pt_regs *regs)
  261. {
  262. /*
  263. * Send a sigbus, regardless of whether we were in kernel
  264. * or user mode.
  265. */
  266. force_sig_fault(SIGBUS, BUS_ADRERR,
  267. (void __user *)(regs->int_parm_long & __FAIL_ADDR_MASK));
  268. }
  269. static noinline void do_fault_error(struct pt_regs *regs, vm_fault_t fault)
  270. {
  271. int si_code;
  272. switch (fault) {
  273. case VM_FAULT_BADACCESS:
  274. case VM_FAULT_BADMAP:
  275. /* Bad memory access. Check if it is kernel or user space. */
  276. if (user_mode(regs)) {
  277. /* User mode accesses just cause a SIGSEGV */
  278. si_code = (fault == VM_FAULT_BADMAP) ?
  279. SEGV_MAPERR : SEGV_ACCERR;
  280. do_sigsegv(regs, si_code);
  281. break;
  282. }
  283. fallthrough;
  284. case VM_FAULT_BADCONTEXT:
  285. case VM_FAULT_PFAULT:
  286. do_no_context(regs, fault);
  287. break;
  288. case VM_FAULT_SIGNAL:
  289. if (!user_mode(regs))
  290. do_no_context(regs, fault);
  291. break;
  292. default: /* fault & VM_FAULT_ERROR */
  293. if (fault & VM_FAULT_OOM) {
  294. if (!user_mode(regs))
  295. do_no_context(regs, fault);
  296. else
  297. pagefault_out_of_memory();
  298. } else if (fault & VM_FAULT_SIGSEGV) {
  299. /* Kernel mode? Handle exceptions or die */
  300. if (!user_mode(regs))
  301. do_no_context(regs, fault);
  302. else
  303. do_sigsegv(regs, SEGV_MAPERR);
  304. } else if (fault & VM_FAULT_SIGBUS) {
  305. /* Kernel mode? Handle exceptions or die */
  306. if (!user_mode(regs))
  307. do_no_context(regs, fault);
  308. else
  309. do_sigbus(regs);
  310. } else
  311. BUG();
  312. break;
  313. }
  314. }
  315. /*
  316. * This routine handles page faults. It determines the address,
  317. * and the problem, and then passes it off to one of the appropriate
  318. * routines.
  319. *
  320. * interruption code (int_code):
  321. * 04 Protection -> Write-Protection (suppression)
  322. * 10 Segment translation -> Not present (nullification)
  323. * 11 Page translation -> Not present (nullification)
  324. * 3b Region third trans. -> Not present (nullification)
  325. */
  326. static inline vm_fault_t do_exception(struct pt_regs *regs, int access)
  327. {
  328. struct gmap *gmap;
  329. struct task_struct *tsk;
  330. struct mm_struct *mm;
  331. struct vm_area_struct *vma;
  332. enum fault_type type;
  333. unsigned long address;
  334. unsigned int flags;
  335. vm_fault_t fault;
  336. bool is_write;
  337. tsk = current;
  338. /*
  339. * The instruction that caused the program check has
  340. * been nullified. Don't signal single step via SIGTRAP.
  341. */
  342. clear_thread_flag(TIF_PER_TRAP);
  343. if (kprobe_page_fault(regs, 14))
  344. return 0;
  345. mm = tsk->mm;
  346. address = get_fault_address(regs);
  347. is_write = fault_is_write(regs);
  348. /*
  349. * Verify that the fault happened in user space, that
  350. * we are not in an interrupt and that there is a
  351. * user context.
  352. */
  353. fault = VM_FAULT_BADCONTEXT;
  354. type = get_fault_type(regs);
  355. switch (type) {
  356. case KERNEL_FAULT:
  357. goto out;
  358. case USER_FAULT:
  359. case GMAP_FAULT:
  360. if (faulthandler_disabled() || !mm)
  361. goto out;
  362. break;
  363. }
  364. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
  365. flags = FAULT_FLAG_DEFAULT;
  366. if (user_mode(regs))
  367. flags |= FAULT_FLAG_USER;
  368. if (is_write)
  369. access = VM_WRITE;
  370. if (access == VM_WRITE)
  371. flags |= FAULT_FLAG_WRITE;
  372. if (!(flags & FAULT_FLAG_USER))
  373. goto lock_mmap;
  374. vma = lock_vma_under_rcu(mm, address);
  375. if (!vma)
  376. goto lock_mmap;
  377. if (!(vma->vm_flags & access)) {
  378. vma_end_read(vma);
  379. goto lock_mmap;
  380. }
  381. fault = handle_mm_fault(vma, address, flags | FAULT_FLAG_VMA_LOCK, regs);
  382. if (!(fault & (VM_FAULT_RETRY | VM_FAULT_COMPLETED)))
  383. vma_end_read(vma);
  384. if (!(fault & VM_FAULT_RETRY)) {
  385. count_vm_vma_lock_event(VMA_LOCK_SUCCESS);
  386. goto out;
  387. }
  388. count_vm_vma_lock_event(VMA_LOCK_RETRY);
  389. if (fault & VM_FAULT_MAJOR)
  390. flags |= FAULT_FLAG_TRIED;
  391. /* Quick path to respond to signals */
  392. if (fault_signal_pending(fault, regs)) {
  393. fault = VM_FAULT_SIGNAL;
  394. goto out;
  395. }
  396. lock_mmap:
  397. mmap_read_lock(mm);
  398. gmap = NULL;
  399. if (IS_ENABLED(CONFIG_PGSTE) && type == GMAP_FAULT) {
  400. gmap = (struct gmap *) S390_lowcore.gmap;
  401. current->thread.gmap_addr = address;
  402. current->thread.gmap_write_flag = !!(flags & FAULT_FLAG_WRITE);
  403. current->thread.gmap_int_code = regs->int_code & 0xffff;
  404. address = __gmap_translate(gmap, address);
  405. if (address == -EFAULT) {
  406. fault = VM_FAULT_BADMAP;
  407. goto out_up;
  408. }
  409. if (gmap->pfault_enabled)
  410. flags |= FAULT_FLAG_RETRY_NOWAIT;
  411. }
  412. retry:
  413. fault = VM_FAULT_BADMAP;
  414. vma = find_vma(mm, address);
  415. if (!vma)
  416. goto out_up;
  417. if (unlikely(vma->vm_start > address)) {
  418. if (!(vma->vm_flags & VM_GROWSDOWN))
  419. goto out_up;
  420. vma = expand_stack(mm, address);
  421. if (!vma)
  422. goto out;
  423. }
  424. /*
  425. * Ok, we have a good vm_area for this memory access, so
  426. * we can handle it..
  427. */
  428. fault = VM_FAULT_BADACCESS;
  429. if (unlikely(!(vma->vm_flags & access)))
  430. goto out_up;
  431. /*
  432. * If for any reason at all we couldn't handle the fault,
  433. * make sure we exit gracefully rather than endlessly redo
  434. * the fault.
  435. */
  436. fault = handle_mm_fault(vma, address, flags, regs);
  437. if (fault_signal_pending(fault, regs)) {
  438. fault = VM_FAULT_SIGNAL;
  439. if (flags & FAULT_FLAG_RETRY_NOWAIT)
  440. goto out_up;
  441. goto out;
  442. }
  443. /* The fault is fully completed (including releasing mmap lock) */
  444. if (fault & VM_FAULT_COMPLETED) {
  445. if (gmap) {
  446. mmap_read_lock(mm);
  447. goto out_gmap;
  448. }
  449. fault = 0;
  450. goto out;
  451. }
  452. if (unlikely(fault & VM_FAULT_ERROR))
  453. goto out_up;
  454. if (fault & VM_FAULT_RETRY) {
  455. if (IS_ENABLED(CONFIG_PGSTE) && gmap &&
  456. (flags & FAULT_FLAG_RETRY_NOWAIT)) {
  457. /*
  458. * FAULT_FLAG_RETRY_NOWAIT has been set, mmap_lock has
  459. * not been released
  460. */
  461. current->thread.gmap_pfault = 1;
  462. fault = VM_FAULT_PFAULT;
  463. goto out_up;
  464. }
  465. flags &= ~FAULT_FLAG_RETRY_NOWAIT;
  466. flags |= FAULT_FLAG_TRIED;
  467. mmap_read_lock(mm);
  468. goto retry;
  469. }
  470. out_gmap:
  471. if (IS_ENABLED(CONFIG_PGSTE) && gmap) {
  472. address = __gmap_link(gmap, current->thread.gmap_addr,
  473. address);
  474. if (address == -EFAULT) {
  475. fault = VM_FAULT_BADMAP;
  476. goto out_up;
  477. }
  478. if (address == -ENOMEM) {
  479. fault = VM_FAULT_OOM;
  480. goto out_up;
  481. }
  482. }
  483. fault = 0;
  484. out_up:
  485. mmap_read_unlock(mm);
  486. out:
  487. return fault;
  488. }
  489. void do_protection_exception(struct pt_regs *regs)
  490. {
  491. unsigned long trans_exc_code;
  492. int access;
  493. vm_fault_t fault;
  494. trans_exc_code = regs->int_parm_long;
  495. /*
  496. * Protection exceptions are suppressing, decrement psw address.
  497. * The exception to this rule are aborted transactions, for these
  498. * the PSW already points to the correct location.
  499. */
  500. if (!(regs->int_code & 0x200))
  501. regs->psw.addr = __rewind_psw(regs->psw, regs->int_code >> 16);
  502. /*
  503. * Check for low-address protection. This needs to be treated
  504. * as a special case because the translation exception code
  505. * field is not guaranteed to contain valid data in this case.
  506. */
  507. if (unlikely(!(trans_exc_code & 4))) {
  508. do_low_address(regs);
  509. return;
  510. }
  511. if (unlikely(MACHINE_HAS_NX && (trans_exc_code & 0x80))) {
  512. regs->int_parm_long = (trans_exc_code & ~PAGE_MASK) |
  513. (regs->psw.addr & PAGE_MASK);
  514. access = VM_EXEC;
  515. fault = VM_FAULT_BADACCESS;
  516. } else {
  517. access = VM_WRITE;
  518. fault = do_exception(regs, access);
  519. }
  520. if (unlikely(fault))
  521. do_fault_error(regs, fault);
  522. }
  523. NOKPROBE_SYMBOL(do_protection_exception);
  524. void do_dat_exception(struct pt_regs *regs)
  525. {
  526. int access;
  527. vm_fault_t fault;
  528. access = VM_ACCESS_FLAGS;
  529. fault = do_exception(regs, access);
  530. if (unlikely(fault))
  531. do_fault_error(regs, fault);
  532. }
  533. NOKPROBE_SYMBOL(do_dat_exception);
  534. #ifdef CONFIG_PFAULT
  535. /*
  536. * 'pfault' pseudo page faults routines.
  537. */
  538. static int pfault_disable;
  539. static int __init nopfault(char *str)
  540. {
  541. pfault_disable = 1;
  542. return 1;
  543. }
  544. __setup("nopfault", nopfault);
  545. struct pfault_refbk {
  546. u16 refdiagc;
  547. u16 reffcode;
  548. u16 refdwlen;
  549. u16 refversn;
  550. u64 refgaddr;
  551. u64 refselmk;
  552. u64 refcmpmk;
  553. u64 reserved;
  554. } __attribute__ ((packed, aligned(8)));
  555. static struct pfault_refbk pfault_init_refbk = {
  556. .refdiagc = 0x258,
  557. .reffcode = 0,
  558. .refdwlen = 5,
  559. .refversn = 2,
  560. .refgaddr = __LC_LPP,
  561. .refselmk = 1ULL << 48,
  562. .refcmpmk = 1ULL << 48,
  563. .reserved = __PF_RES_FIELD
  564. };
  565. int pfault_init(void)
  566. {
  567. int rc;
  568. if (pfault_disable)
  569. return -1;
  570. diag_stat_inc(DIAG_STAT_X258);
  571. asm volatile(
  572. " diag %1,%0,0x258\n"
  573. "0: j 2f\n"
  574. "1: la %0,8\n"
  575. "2:\n"
  576. EX_TABLE(0b,1b)
  577. : "=d" (rc)
  578. : "a" (&pfault_init_refbk), "m" (pfault_init_refbk) : "cc");
  579. return rc;
  580. }
  581. static struct pfault_refbk pfault_fini_refbk = {
  582. .refdiagc = 0x258,
  583. .reffcode = 1,
  584. .refdwlen = 5,
  585. .refversn = 2,
  586. };
  587. void pfault_fini(void)
  588. {
  589. if (pfault_disable)
  590. return;
  591. diag_stat_inc(DIAG_STAT_X258);
  592. asm volatile(
  593. " diag %0,0,0x258\n"
  594. "0: nopr %%r7\n"
  595. EX_TABLE(0b,0b)
  596. : : "a" (&pfault_fini_refbk), "m" (pfault_fini_refbk) : "cc");
  597. }
  598. static DEFINE_SPINLOCK(pfault_lock);
  599. static LIST_HEAD(pfault_list);
  600. #define PF_COMPLETE 0x0080
  601. /*
  602. * The mechanism of our pfault code: if Linux is running as guest, runs a user
  603. * space process and the user space process accesses a page that the host has
  604. * paged out we get a pfault interrupt.
  605. *
  606. * This allows us, within the guest, to schedule a different process. Without
  607. * this mechanism the host would have to suspend the whole virtual cpu until
  608. * the page has been paged in.
  609. *
  610. * So when we get such an interrupt then we set the state of the current task
  611. * to uninterruptible and also set the need_resched flag. Both happens within
  612. * interrupt context(!). If we later on want to return to user space we
  613. * recognize the need_resched flag and then call schedule(). It's not very
  614. * obvious how this works...
  615. *
  616. * Of course we have a lot of additional fun with the completion interrupt (->
  617. * host signals that a page of a process has been paged in and the process can
  618. * continue to run). This interrupt can arrive on any cpu and, since we have
  619. * virtual cpus, actually appear before the interrupt that signals that a page
  620. * is missing.
  621. */
  622. static void pfault_interrupt(struct ext_code ext_code,
  623. unsigned int param32, unsigned long param64)
  624. {
  625. struct task_struct *tsk;
  626. __u16 subcode;
  627. pid_t pid;
  628. /*
  629. * Get the external interruption subcode & pfault initial/completion
  630. * signal bit. VM stores this in the 'cpu address' field associated
  631. * with the external interrupt.
  632. */
  633. subcode = ext_code.subcode;
  634. if ((subcode & 0xff00) != __SUBCODE_MASK)
  635. return;
  636. inc_irq_stat(IRQEXT_PFL);
  637. /* Get the token (= pid of the affected task). */
  638. pid = param64 & LPP_PID_MASK;
  639. rcu_read_lock();
  640. tsk = find_task_by_pid_ns(pid, &init_pid_ns);
  641. if (tsk)
  642. get_task_struct(tsk);
  643. rcu_read_unlock();
  644. if (!tsk)
  645. return;
  646. spin_lock(&pfault_lock);
  647. if (subcode & PF_COMPLETE) {
  648. /* signal bit is set -> a page has been swapped in by VM */
  649. if (tsk->thread.pfault_wait == 1) {
  650. /* Initial interrupt was faster than the completion
  651. * interrupt. pfault_wait is valid. Set pfault_wait
  652. * back to zero and wake up the process. This can
  653. * safely be done because the task is still sleeping
  654. * and can't produce new pfaults. */
  655. tsk->thread.pfault_wait = 0;
  656. list_del(&tsk->thread.list);
  657. wake_up_process(tsk);
  658. put_task_struct(tsk);
  659. } else {
  660. /* Completion interrupt was faster than initial
  661. * interrupt. Set pfault_wait to -1 so the initial
  662. * interrupt doesn't put the task to sleep.
  663. * If the task is not running, ignore the completion
  664. * interrupt since it must be a leftover of a PFAULT
  665. * CANCEL operation which didn't remove all pending
  666. * completion interrupts. */
  667. if (task_is_running(tsk))
  668. tsk->thread.pfault_wait = -1;
  669. }
  670. } else {
  671. /* signal bit not set -> a real page is missing. */
  672. if (WARN_ON_ONCE(tsk != current))
  673. goto out;
  674. if (tsk->thread.pfault_wait == 1) {
  675. /* Already on the list with a reference: put to sleep */
  676. goto block;
  677. } else if (tsk->thread.pfault_wait == -1) {
  678. /* Completion interrupt was faster than the initial
  679. * interrupt (pfault_wait == -1). Set pfault_wait
  680. * back to zero and exit. */
  681. tsk->thread.pfault_wait = 0;
  682. } else {
  683. /* Initial interrupt arrived before completion
  684. * interrupt. Let the task sleep.
  685. * An extra task reference is needed since a different
  686. * cpu may set the task state to TASK_RUNNING again
  687. * before the scheduler is reached. */
  688. get_task_struct(tsk);
  689. tsk->thread.pfault_wait = 1;
  690. list_add(&tsk->thread.list, &pfault_list);
  691. block:
  692. /* Since this must be a userspace fault, there
  693. * is no kernel task state to trample. Rely on the
  694. * return to userspace schedule() to block. */
  695. __set_current_state(TASK_UNINTERRUPTIBLE);
  696. set_tsk_need_resched(tsk);
  697. set_preempt_need_resched();
  698. }
  699. }
  700. out:
  701. spin_unlock(&pfault_lock);
  702. put_task_struct(tsk);
  703. }
  704. static int pfault_cpu_dead(unsigned int cpu)
  705. {
  706. struct thread_struct *thread, *next;
  707. struct task_struct *tsk;
  708. spin_lock_irq(&pfault_lock);
  709. list_for_each_entry_safe(thread, next, &pfault_list, list) {
  710. thread->pfault_wait = 0;
  711. list_del(&thread->list);
  712. tsk = container_of(thread, struct task_struct, thread);
  713. wake_up_process(tsk);
  714. put_task_struct(tsk);
  715. }
  716. spin_unlock_irq(&pfault_lock);
  717. return 0;
  718. }
  719. static int __init pfault_irq_init(void)
  720. {
  721. int rc;
  722. rc = register_external_irq(EXT_IRQ_CP_SERVICE, pfault_interrupt);
  723. if (rc)
  724. goto out_extint;
  725. rc = pfault_init() == 0 ? 0 : -EOPNOTSUPP;
  726. if (rc)
  727. goto out_pfault;
  728. irq_subclass_register(IRQ_SUBCLASS_SERVICE_SIGNAL);
  729. cpuhp_setup_state_nocalls(CPUHP_S390_PFAULT_DEAD, "s390/pfault:dead",
  730. NULL, pfault_cpu_dead);
  731. return 0;
  732. out_pfault:
  733. unregister_external_irq(EXT_IRQ_CP_SERVICE, pfault_interrupt);
  734. out_extint:
  735. pfault_disable = 1;
  736. return rc;
  737. }
  738. early_initcall(pfault_irq_init);
  739. #endif /* CONFIG_PFAULT */
  740. #if IS_ENABLED(CONFIG_PGSTE)
  741. void do_secure_storage_access(struct pt_regs *regs)
  742. {
  743. unsigned long addr = regs->int_parm_long & __FAIL_ADDR_MASK;
  744. struct vm_area_struct *vma;
  745. struct mm_struct *mm;
  746. struct page *page;
  747. struct gmap *gmap;
  748. int rc;
  749. /*
  750. * bit 61 tells us if the address is valid, if it's not we
  751. * have a major problem and should stop the kernel or send a
  752. * SIGSEGV to the process. Unfortunately bit 61 is not
  753. * reliable without the misc UV feature so we need to check
  754. * for that as well.
  755. */
  756. if (test_bit_inv(BIT_UV_FEAT_MISC, &uv_info.uv_feature_indications) &&
  757. !test_bit_inv(61, &regs->int_parm_long)) {
  758. /*
  759. * When this happens, userspace did something that it
  760. * was not supposed to do, e.g. branching into secure
  761. * memory. Trigger a segmentation fault.
  762. */
  763. if (user_mode(regs)) {
  764. send_sig(SIGSEGV, current, 0);
  765. return;
  766. }
  767. /*
  768. * The kernel should never run into this case and we
  769. * have no way out of this situation.
  770. */
  771. panic("Unexpected PGM 0x3d with TEID bit 61=0");
  772. }
  773. switch (get_fault_type(regs)) {
  774. case GMAP_FAULT:
  775. mm = current->mm;
  776. gmap = (struct gmap *)S390_lowcore.gmap;
  777. mmap_read_lock(mm);
  778. addr = __gmap_translate(gmap, addr);
  779. mmap_read_unlock(mm);
  780. if (IS_ERR_VALUE(addr)) {
  781. do_fault_error(regs, VM_FAULT_BADMAP);
  782. break;
  783. }
  784. fallthrough;
  785. case USER_FAULT:
  786. mm = current->mm;
  787. mmap_read_lock(mm);
  788. vma = find_vma(mm, addr);
  789. if (!vma) {
  790. mmap_read_unlock(mm);
  791. do_fault_error(regs, VM_FAULT_BADMAP);
  792. break;
  793. }
  794. page = follow_page(vma, addr, FOLL_WRITE | FOLL_GET);
  795. if (IS_ERR_OR_NULL(page)) {
  796. mmap_read_unlock(mm);
  797. break;
  798. }
  799. if (arch_make_page_accessible(page))
  800. send_sig(SIGSEGV, current, 0);
  801. put_page(page);
  802. mmap_read_unlock(mm);
  803. break;
  804. case KERNEL_FAULT:
  805. page = phys_to_page(addr);
  806. if (unlikely(!try_get_page(page)))
  807. break;
  808. rc = arch_make_page_accessible(page);
  809. put_page(page);
  810. if (rc)
  811. BUG();
  812. break;
  813. default:
  814. do_fault_error(regs, VM_FAULT_BADMAP);
  815. WARN_ON_ONCE(1);
  816. }
  817. }
  818. NOKPROBE_SYMBOL(do_secure_storage_access);
  819. void do_non_secure_storage_access(struct pt_regs *regs)
  820. {
  821. unsigned long gaddr = regs->int_parm_long & __FAIL_ADDR_MASK;
  822. struct gmap *gmap = (struct gmap *)S390_lowcore.gmap;
  823. if (get_fault_type(regs) != GMAP_FAULT) {
  824. do_fault_error(regs, VM_FAULT_BADMAP);
  825. WARN_ON_ONCE(1);
  826. return;
  827. }
  828. if (gmap_convert_to_secure(gmap, gaddr) == -EINVAL)
  829. send_sig(SIGSEGV, current, 0);
  830. }
  831. NOKPROBE_SYMBOL(do_non_secure_storage_access);
  832. void do_secure_storage_violation(struct pt_regs *regs)
  833. {
  834. unsigned long gaddr = regs->int_parm_long & __FAIL_ADDR_MASK;
  835. struct gmap *gmap = (struct gmap *)S390_lowcore.gmap;
  836. /*
  837. * If the VM has been rebooted, its address space might still contain
  838. * secure pages from the previous boot.
  839. * Clear the page so it can be reused.
  840. */
  841. if (!gmap_destroy_page(gmap, gaddr))
  842. return;
  843. /*
  844. * Either KVM messed up the secure guest mapping or the same
  845. * page is mapped into multiple secure guests.
  846. *
  847. * This exception is only triggered when a guest 2 is running
  848. * and can therefore never occur in kernel context.
  849. */
  850. printk_ratelimited(KERN_WARNING
  851. "Secure storage violation in task: %s, pid %d\n",
  852. current->comm, current->pid);
  853. send_sig(SIGSEGV, current, 0);
  854. }
  855. #endif /* CONFIG_PGSTE */