fault.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  1. /*
  2. * arch/s390/mm/fault.c
  3. *
  4. * S390 version
  5. * Copyright (C) 1999 IBM Deutschland Entwicklung GmbH, IBM Corporation
  6. * Author(s): Hartmut Penner (hp@de.ibm.com)
  7. * Ulrich Weigand (uweigand@de.ibm.com)
  8. *
  9. * Derived from "arch/i386/mm/fault.c"
  10. * Copyright (C) 1995 Linus Torvalds
  11. */
  12. #include <linux/signal.h>
  13. #include <linux/sched.h>
  14. #include <linux/kernel.h>
  15. #include <linux/errno.h>
  16. #include <linux/string.h>
  17. #include <linux/types.h>
  18. #include <linux/ptrace.h>
  19. #include <linux/mman.h>
  20. #include <linux/mm.h>
  21. #include <linux/compat.h>
  22. #include <linux/smp.h>
  23. #include <linux/kdebug.h>
  24. #include <linux/init.h>
  25. #include <linux/console.h>
  26. #include <linux/module.h>
  27. #include <linux/hardirq.h>
  28. #include <linux/kprobes.h>
  29. #include <linux/uaccess.h>
  30. #include <linux/hugetlb.h>
  31. #include <asm/system.h>
  32. #include <asm/pgtable.h>
  33. #include <asm/s390_ext.h>
  34. #include <asm/mmu_context.h>
  35. #include "../kernel/entry.h"
  36. #ifndef CONFIG_64BIT
  37. #define __FAIL_ADDR_MASK 0x7ffff000
  38. #define __FIXUP_MASK 0x7fffffff
  39. #define __SUBCODE_MASK 0x0200
  40. #define __PF_RES_FIELD 0ULL
  41. #else /* CONFIG_64BIT */
  42. #define __FAIL_ADDR_MASK -4096L
  43. #define __FIXUP_MASK ~0L
  44. #define __SUBCODE_MASK 0x0600
  45. #define __PF_RES_FIELD 0x8000000000000000ULL
  46. #endif /* CONFIG_64BIT */
  47. #ifdef CONFIG_SYSCTL
  48. extern int sysctl_userprocess_debug;
  49. #endif
  50. #ifdef CONFIG_KPROBES
  51. static inline int notify_page_fault(struct pt_regs *regs, long err)
  52. {
  53. int ret = 0;
  54. /* kprobe_running() needs smp_processor_id() */
  55. if (!user_mode(regs)) {
  56. preempt_disable();
  57. if (kprobe_running() && kprobe_fault_handler(regs, 14))
  58. ret = 1;
  59. preempt_enable();
  60. }
  61. return ret;
  62. }
  63. #else
  64. static inline int notify_page_fault(struct pt_regs *regs, long err)
  65. {
  66. return 0;
  67. }
  68. #endif
  69. /*
  70. * Unlock any spinlocks which will prevent us from getting the
  71. * message out.
  72. */
  73. void bust_spinlocks(int yes)
  74. {
  75. if (yes) {
  76. oops_in_progress = 1;
  77. } else {
  78. int loglevel_save = console_loglevel;
  79. console_unblank();
  80. oops_in_progress = 0;
  81. /*
  82. * OK, the message is on the console. Now we call printk()
  83. * without oops_in_progress set so that printk will give klogd
  84. * a poke. Hold onto your hats...
  85. */
  86. console_loglevel = 15;
  87. printk(" ");
  88. console_loglevel = loglevel_save;
  89. }
  90. }
  91. /*
  92. * Returns the address space associated with the fault.
  93. * Returns 0 for kernel space, 1 for user space and
  94. * 2 for code execution in user space with noexec=on.
  95. */
  96. static inline int check_space(struct task_struct *tsk)
  97. {
  98. /*
  99. * The lowest two bits of S390_lowcore.trans_exc_code
  100. * indicate which paging table was used.
  101. */
  102. int desc = S390_lowcore.trans_exc_code & 3;
  103. if (desc == 3) /* Home Segment Table Descriptor */
  104. return switch_amode == 0;
  105. if (desc == 2) /* Secondary Segment Table Descriptor */
  106. return tsk->thread.mm_segment.ar4;
  107. #ifdef CONFIG_S390_SWITCH_AMODE
  108. if (unlikely(desc == 1)) { /* STD determined via access register */
  109. /* %a0 always indicates primary space. */
  110. if (S390_lowcore.exc_access_id != 0) {
  111. save_access_regs(tsk->thread.acrs);
  112. /*
  113. * An alet of 0 indicates primary space.
  114. * An alet of 1 indicates secondary space.
  115. * Any other alet values generate an
  116. * alen-translation exception.
  117. */
  118. if (tsk->thread.acrs[S390_lowcore.exc_access_id])
  119. return tsk->thread.mm_segment.ar4;
  120. }
  121. }
  122. #endif
  123. /* Primary Segment Table Descriptor */
  124. return switch_amode << s390_noexec;
  125. }
  126. /*
  127. * Send SIGSEGV to task. This is an external routine
  128. * to keep the stack usage of do_page_fault small.
  129. */
  130. static void do_sigsegv(struct pt_regs *regs, unsigned long error_code,
  131. int si_code, unsigned long address)
  132. {
  133. struct siginfo si;
  134. #if defined(CONFIG_SYSCTL) || defined(CONFIG_PROCESS_DEBUG)
  135. #if defined(CONFIG_SYSCTL)
  136. if (sysctl_userprocess_debug)
  137. #endif
  138. {
  139. printk("User process fault: interruption code 0x%lX\n",
  140. error_code);
  141. printk("failing address: %lX\n", address);
  142. show_regs(regs);
  143. }
  144. #endif
  145. si.si_signo = SIGSEGV;
  146. si.si_code = si_code;
  147. si.si_addr = (void __user *) address;
  148. force_sig_info(SIGSEGV, &si, current);
  149. }
  150. static void do_no_context(struct pt_regs *regs, unsigned long error_code,
  151. unsigned long address)
  152. {
  153. const struct exception_table_entry *fixup;
  154. /* Are we prepared to handle this kernel fault? */
  155. fixup = search_exception_tables(regs->psw.addr & __FIXUP_MASK);
  156. if (fixup) {
  157. regs->psw.addr = fixup->fixup | PSW_ADDR_AMODE;
  158. return;
  159. }
  160. /*
  161. * Oops. The kernel tried to access some bad page. We'll have to
  162. * terminate things with extreme prejudice.
  163. */
  164. if (check_space(current) == 0)
  165. printk(KERN_ALERT "Unable to handle kernel pointer dereference"
  166. " at virtual kernel address %p\n", (void *)address);
  167. else
  168. printk(KERN_ALERT "Unable to handle kernel paging request"
  169. " at virtual user address %p\n", (void *)address);
  170. die("Oops", regs, error_code);
  171. do_exit(SIGKILL);
  172. }
  173. static void do_low_address(struct pt_regs *regs, unsigned long error_code)
  174. {
  175. /* Low-address protection hit in kernel mode means
  176. NULL pointer write access in kernel mode. */
  177. if (regs->psw.mask & PSW_MASK_PSTATE) {
  178. /* Low-address protection hit in user mode 'cannot happen'. */
  179. die ("Low-address protection", regs, error_code);
  180. do_exit(SIGKILL);
  181. }
  182. do_no_context(regs, error_code, 0);
  183. }
  184. static void do_sigbus(struct pt_regs *regs, unsigned long error_code,
  185. unsigned long address)
  186. {
  187. struct task_struct *tsk = current;
  188. struct mm_struct *mm = tsk->mm;
  189. up_read(&mm->mmap_sem);
  190. /*
  191. * Send a sigbus, regardless of whether we were in kernel
  192. * or user mode.
  193. */
  194. tsk->thread.prot_addr = address;
  195. tsk->thread.trap_no = error_code;
  196. force_sig(SIGBUS, tsk);
  197. /* Kernel mode? Handle exceptions or die */
  198. if (!(regs->psw.mask & PSW_MASK_PSTATE))
  199. do_no_context(regs, error_code, address);
  200. }
  201. #ifdef CONFIG_S390_EXEC_PROTECT
  202. static int signal_return(struct mm_struct *mm, struct pt_regs *regs,
  203. unsigned long address, unsigned long error_code)
  204. {
  205. u16 instruction;
  206. int rc;
  207. #ifdef CONFIG_COMPAT
  208. int compat;
  209. #endif
  210. pagefault_disable();
  211. rc = __get_user(instruction, (u16 __user *) regs->psw.addr);
  212. pagefault_enable();
  213. if (rc)
  214. return -EFAULT;
  215. up_read(&mm->mmap_sem);
  216. clear_tsk_thread_flag(current, TIF_SINGLE_STEP);
  217. #ifdef CONFIG_COMPAT
  218. compat = is_compat_task();
  219. if (compat && instruction == 0x0a77)
  220. sys32_sigreturn();
  221. else if (compat && instruction == 0x0aad)
  222. sys32_rt_sigreturn();
  223. else
  224. #endif
  225. if (instruction == 0x0a77)
  226. sys_sigreturn();
  227. else if (instruction == 0x0aad)
  228. sys_rt_sigreturn();
  229. else {
  230. current->thread.prot_addr = address;
  231. current->thread.trap_no = error_code;
  232. do_sigsegv(regs, error_code, SEGV_MAPERR, address);
  233. }
  234. return 0;
  235. }
  236. #endif /* CONFIG_S390_EXEC_PROTECT */
  237. /*
  238. * This routine handles page faults. It determines the address,
  239. * and the problem, and then passes it off to one of the appropriate
  240. * routines.
  241. *
  242. * error_code:
  243. * 04 Protection -> Write-Protection (suprression)
  244. * 10 Segment translation -> Not present (nullification)
  245. * 11 Page translation -> Not present (nullification)
  246. * 3b Region third trans. -> Not present (nullification)
  247. */
  248. static inline void
  249. do_exception(struct pt_regs *regs, unsigned long error_code, int write)
  250. {
  251. struct task_struct *tsk;
  252. struct mm_struct *mm;
  253. struct vm_area_struct *vma;
  254. unsigned long address;
  255. int space;
  256. int si_code;
  257. int fault;
  258. if (notify_page_fault(regs, error_code))
  259. return;
  260. tsk = current;
  261. mm = tsk->mm;
  262. /* get the failing address and the affected space */
  263. address = S390_lowcore.trans_exc_code & __FAIL_ADDR_MASK;
  264. space = check_space(tsk);
  265. /*
  266. * Verify that the fault happened in user space, that
  267. * we are not in an interrupt and that there is a
  268. * user context.
  269. */
  270. if (unlikely(space == 0 || in_atomic() || !mm))
  271. goto no_context;
  272. /*
  273. * When we get here, the fault happened in the current
  274. * task's user address space, so we can switch on the
  275. * interrupts again and then search the VMAs
  276. */
  277. local_irq_enable();
  278. down_read(&mm->mmap_sem);
  279. si_code = SEGV_MAPERR;
  280. vma = find_vma(mm, address);
  281. if (!vma)
  282. goto bad_area;
  283. #ifdef CONFIG_S390_EXEC_PROTECT
  284. if (unlikely((space == 2) && !(vma->vm_flags & VM_EXEC)))
  285. if (!signal_return(mm, regs, address, error_code))
  286. /*
  287. * signal_return() has done an up_read(&mm->mmap_sem)
  288. * if it returns 0.
  289. */
  290. return;
  291. #endif
  292. if (vma->vm_start <= address)
  293. goto good_area;
  294. if (!(vma->vm_flags & VM_GROWSDOWN))
  295. goto bad_area;
  296. if (expand_stack(vma, address))
  297. goto bad_area;
  298. /*
  299. * Ok, we have a good vm_area for this memory access, so
  300. * we can handle it..
  301. */
  302. good_area:
  303. si_code = SEGV_ACCERR;
  304. if (!write) {
  305. /* page not present, check vm flags */
  306. if (!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE)))
  307. goto bad_area;
  308. } else {
  309. if (!(vma->vm_flags & VM_WRITE))
  310. goto bad_area;
  311. }
  312. if (is_vm_hugetlb_page(vma))
  313. address &= HPAGE_MASK;
  314. /*
  315. * If for any reason at all we couldn't handle the fault,
  316. * make sure we exit gracefully rather than endlessly redo
  317. * the fault.
  318. */
  319. fault = handle_mm_fault(mm, vma, address, write ? FAULT_FLAG_WRITE : 0);
  320. if (unlikely(fault & VM_FAULT_ERROR)) {
  321. if (fault & VM_FAULT_OOM) {
  322. up_read(&mm->mmap_sem);
  323. pagefault_out_of_memory();
  324. return;
  325. } else if (fault & VM_FAULT_SIGBUS) {
  326. do_sigbus(regs, error_code, address);
  327. return;
  328. }
  329. BUG();
  330. }
  331. if (fault & VM_FAULT_MAJOR)
  332. tsk->maj_flt++;
  333. else
  334. tsk->min_flt++;
  335. up_read(&mm->mmap_sem);
  336. /*
  337. * The instruction that caused the program check will
  338. * be repeated. Don't signal single step via SIGTRAP.
  339. */
  340. clear_tsk_thread_flag(tsk, TIF_SINGLE_STEP);
  341. return;
  342. /*
  343. * Something tried to access memory that isn't in our memory map..
  344. * Fix it, but check if it's kernel or user first..
  345. */
  346. bad_area:
  347. up_read(&mm->mmap_sem);
  348. /* User mode accesses just cause a SIGSEGV */
  349. if (regs->psw.mask & PSW_MASK_PSTATE) {
  350. tsk->thread.prot_addr = address;
  351. tsk->thread.trap_no = error_code;
  352. do_sigsegv(regs, error_code, si_code, address);
  353. return;
  354. }
  355. no_context:
  356. do_no_context(regs, error_code, address);
  357. }
  358. void __kprobes do_protection_exception(struct pt_regs *regs,
  359. long error_code)
  360. {
  361. /* Protection exception is supressing, decrement psw address. */
  362. regs->psw.addr -= (error_code >> 16);
  363. /*
  364. * Check for low-address protection. This needs to be treated
  365. * as a special case because the translation exception code
  366. * field is not guaranteed to contain valid data in this case.
  367. */
  368. if (unlikely(!(S390_lowcore.trans_exc_code & 4))) {
  369. do_low_address(regs, error_code);
  370. return;
  371. }
  372. do_exception(regs, 4, 1);
  373. }
  374. void __kprobes do_dat_exception(struct pt_regs *regs, long error_code)
  375. {
  376. do_exception(regs, error_code & 0xff, 0);
  377. }
  378. #ifdef CONFIG_64BIT
  379. void __kprobes do_asce_exception(struct pt_regs *regs, unsigned long error_code)
  380. {
  381. struct mm_struct *mm;
  382. struct vm_area_struct *vma;
  383. unsigned long address;
  384. int space;
  385. mm = current->mm;
  386. address = S390_lowcore.trans_exc_code & __FAIL_ADDR_MASK;
  387. space = check_space(current);
  388. if (unlikely(space == 0 || in_atomic() || !mm))
  389. goto no_context;
  390. local_irq_enable();
  391. down_read(&mm->mmap_sem);
  392. vma = find_vma(mm, address);
  393. up_read(&mm->mmap_sem);
  394. if (vma) {
  395. update_mm(mm, current);
  396. return;
  397. }
  398. /* User mode accesses just cause a SIGSEGV */
  399. if (regs->psw.mask & PSW_MASK_PSTATE) {
  400. current->thread.prot_addr = address;
  401. current->thread.trap_no = error_code;
  402. do_sigsegv(regs, error_code, SEGV_MAPERR, address);
  403. return;
  404. }
  405. no_context:
  406. do_no_context(regs, error_code, address);
  407. }
  408. #endif
  409. #ifdef CONFIG_PFAULT
  410. /*
  411. * 'pfault' pseudo page faults routines.
  412. */
  413. static ext_int_info_t ext_int_pfault;
  414. static int pfault_disable = 0;
  415. static int __init nopfault(char *str)
  416. {
  417. pfault_disable = 1;
  418. return 1;
  419. }
  420. __setup("nopfault", nopfault);
  421. typedef struct {
  422. __u16 refdiagc;
  423. __u16 reffcode;
  424. __u16 refdwlen;
  425. __u16 refversn;
  426. __u64 refgaddr;
  427. __u64 refselmk;
  428. __u64 refcmpmk;
  429. __u64 reserved;
  430. } __attribute__ ((packed, aligned(8))) pfault_refbk_t;
  431. int pfault_init(void)
  432. {
  433. pfault_refbk_t refbk =
  434. { 0x258, 0, 5, 2, __LC_CURRENT, 1ULL << 48, 1ULL << 48,
  435. __PF_RES_FIELD };
  436. int rc;
  437. if (!MACHINE_IS_VM || pfault_disable)
  438. return -1;
  439. asm volatile(
  440. " diag %1,%0,0x258\n"
  441. "0: j 2f\n"
  442. "1: la %0,8\n"
  443. "2:\n"
  444. EX_TABLE(0b,1b)
  445. : "=d" (rc) : "a" (&refbk), "m" (refbk) : "cc");
  446. __ctl_set_bit(0, 9);
  447. return rc;
  448. }
  449. void pfault_fini(void)
  450. {
  451. pfault_refbk_t refbk =
  452. { 0x258, 1, 5, 2, 0ULL, 0ULL, 0ULL, 0ULL };
  453. if (!MACHINE_IS_VM || pfault_disable)
  454. return;
  455. __ctl_clear_bit(0,9);
  456. asm volatile(
  457. " diag %0,0,0x258\n"
  458. "0:\n"
  459. EX_TABLE(0b,0b)
  460. : : "a" (&refbk), "m" (refbk) : "cc");
  461. }
  462. static void pfault_interrupt(__u16 error_code)
  463. {
  464. struct task_struct *tsk;
  465. __u16 subcode;
  466. /*
  467. * Get the external interruption subcode & pfault
  468. * initial/completion signal bit. VM stores this
  469. * in the 'cpu address' field associated with the
  470. * external interrupt.
  471. */
  472. subcode = S390_lowcore.cpu_addr;
  473. if ((subcode & 0xff00) != __SUBCODE_MASK)
  474. return;
  475. /*
  476. * Get the token (= address of the task structure of the affected task).
  477. */
  478. tsk = *(struct task_struct **) __LC_PFAULT_INTPARM;
  479. if (subcode & 0x0080) {
  480. /* signal bit is set -> a page has been swapped in by VM */
  481. if (xchg(&tsk->thread.pfault_wait, -1) != 0) {
  482. /* Initial interrupt was faster than the completion
  483. * interrupt. pfault_wait is valid. Set pfault_wait
  484. * back to zero and wake up the process. This can
  485. * safely be done because the task is still sleeping
  486. * and can't produce new pfaults. */
  487. tsk->thread.pfault_wait = 0;
  488. wake_up_process(tsk);
  489. put_task_struct(tsk);
  490. }
  491. } else {
  492. /* signal bit not set -> a real page is missing. */
  493. get_task_struct(tsk);
  494. set_task_state(tsk, TASK_UNINTERRUPTIBLE);
  495. if (xchg(&tsk->thread.pfault_wait, 1) != 0) {
  496. /* Completion interrupt was faster than the initial
  497. * interrupt (swapped in a -1 for pfault_wait). Set
  498. * pfault_wait back to zero and exit. This can be
  499. * done safely because tsk is running in kernel
  500. * mode and can't produce new pfaults. */
  501. tsk->thread.pfault_wait = 0;
  502. set_task_state(tsk, TASK_RUNNING);
  503. put_task_struct(tsk);
  504. } else
  505. set_tsk_need_resched(tsk);
  506. }
  507. }
  508. void __init pfault_irq_init(void)
  509. {
  510. if (!MACHINE_IS_VM)
  511. return;
  512. /*
  513. * Try to get pfault pseudo page faults going.
  514. */
  515. if (register_early_external_interrupt(0x2603, pfault_interrupt,
  516. &ext_int_pfault) != 0)
  517. panic("Couldn't request external interrupt 0x2603");
  518. if (pfault_init() == 0)
  519. return;
  520. /* Tough luck, no pfault. */
  521. pfault_disable = 1;
  522. unregister_early_external_interrupt(0x2603, pfault_interrupt,
  523. &ext_int_pfault);
  524. }
  525. #endif