fault.c 15 KB

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