fault.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  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. #include "../kernel/entry.h"
  35. #ifndef CONFIG_64BIT
  36. #define __FAIL_ADDR_MASK 0x7ffff000
  37. #define __FIXUP_MASK 0x7fffffff
  38. #define __SUBCODE_MASK 0x0200
  39. #define __PF_RES_FIELD 0ULL
  40. #else /* CONFIG_64BIT */
  41. #define __FAIL_ADDR_MASK -4096L
  42. #define __FIXUP_MASK ~0L
  43. #define __SUBCODE_MASK 0x0600
  44. #define __PF_RES_FIELD 0x8000000000000000ULL
  45. #endif /* CONFIG_64BIT */
  46. #ifdef CONFIG_SYSCTL
  47. extern int sysctl_userprocess_debug;
  48. #endif
  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. static int signal_return(struct mm_struct *mm, struct pt_regs *regs,
  223. unsigned long address, unsigned long error_code)
  224. {
  225. u16 instruction;
  226. int rc;
  227. #ifdef CONFIG_COMPAT
  228. int compat;
  229. #endif
  230. pagefault_disable();
  231. rc = __get_user(instruction, (u16 __user *) regs->psw.addr);
  232. pagefault_enable();
  233. if (rc)
  234. return -EFAULT;
  235. up_read(&mm->mmap_sem);
  236. clear_tsk_thread_flag(current, TIF_SINGLE_STEP);
  237. #ifdef CONFIG_COMPAT
  238. compat = test_tsk_thread_flag(current, TIF_31BIT);
  239. if (compat && instruction == 0x0a77)
  240. sys32_sigreturn();
  241. else if (compat && instruction == 0x0aad)
  242. sys32_rt_sigreturn();
  243. else
  244. #endif
  245. if (instruction == 0x0a77)
  246. sys_sigreturn();
  247. else if (instruction == 0x0aad)
  248. sys_rt_sigreturn();
  249. else {
  250. current->thread.prot_addr = address;
  251. current->thread.trap_no = error_code;
  252. do_sigsegv(regs, error_code, SEGV_MAPERR, address);
  253. }
  254. return 0;
  255. }
  256. #endif /* CONFIG_S390_EXEC_PROTECT */
  257. /*
  258. * This routine handles page faults. It determines the address,
  259. * and the problem, and then passes it off to one of the appropriate
  260. * routines.
  261. *
  262. * error_code:
  263. * 04 Protection -> Write-Protection (suprression)
  264. * 10 Segment translation -> Not present (nullification)
  265. * 11 Page translation -> Not present (nullification)
  266. * 3b Region third trans. -> Not present (nullification)
  267. */
  268. static inline void
  269. do_exception(struct pt_regs *regs, unsigned long error_code, int write)
  270. {
  271. struct task_struct *tsk;
  272. struct mm_struct *mm;
  273. struct vm_area_struct *vma;
  274. unsigned long address;
  275. int space;
  276. int si_code;
  277. int fault;
  278. if (notify_page_fault(regs, error_code))
  279. return;
  280. tsk = current;
  281. mm = tsk->mm;
  282. /* get the failing address and the affected space */
  283. address = S390_lowcore.trans_exc_code & __FAIL_ADDR_MASK;
  284. space = check_space(tsk);
  285. /*
  286. * Verify that the fault happened in user space, that
  287. * we are not in an interrupt and that there is a
  288. * user context.
  289. */
  290. if (unlikely(space == 0 || in_atomic() || !mm))
  291. goto no_context;
  292. /*
  293. * When we get here, the fault happened in the current
  294. * task's user address space, so we can switch on the
  295. * interrupts again and then search the VMAs
  296. */
  297. local_irq_enable();
  298. down_read(&mm->mmap_sem);
  299. si_code = SEGV_MAPERR;
  300. vma = find_vma(mm, address);
  301. if (!vma)
  302. goto bad_area;
  303. #ifdef CONFIG_S390_EXEC_PROTECT
  304. if (unlikely((space == 2) && !(vma->vm_flags & VM_EXEC)))
  305. if (!signal_return(mm, regs, address, error_code))
  306. /*
  307. * signal_return() has done an up_read(&mm->mmap_sem)
  308. * if it returns 0.
  309. */
  310. return;
  311. #endif
  312. if (vma->vm_start <= address)
  313. goto good_area;
  314. if (!(vma->vm_flags & VM_GROWSDOWN))
  315. goto bad_area;
  316. if (expand_stack(vma, address))
  317. goto bad_area;
  318. /*
  319. * Ok, we have a good vm_area for this memory access, so
  320. * we can handle it..
  321. */
  322. good_area:
  323. si_code = SEGV_ACCERR;
  324. if (!write) {
  325. /* page not present, check vm flags */
  326. if (!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE)))
  327. goto bad_area;
  328. } else {
  329. if (!(vma->vm_flags & VM_WRITE))
  330. goto bad_area;
  331. }
  332. survive:
  333. /*
  334. * If for any reason at all we couldn't handle the fault,
  335. * make sure we exit gracefully rather than endlessly redo
  336. * the fault.
  337. */
  338. fault = handle_mm_fault(mm, vma, address, write);
  339. if (unlikely(fault & VM_FAULT_ERROR)) {
  340. if (fault & VM_FAULT_OOM) {
  341. if (do_out_of_memory(regs, error_code, address))
  342. goto survive;
  343. return;
  344. } else if (fault & VM_FAULT_SIGBUS) {
  345. do_sigbus(regs, error_code, address);
  346. return;
  347. }
  348. BUG();
  349. }
  350. if (fault & VM_FAULT_MAJOR)
  351. tsk->maj_flt++;
  352. else
  353. tsk->min_flt++;
  354. up_read(&mm->mmap_sem);
  355. /*
  356. * The instruction that caused the program check will
  357. * be repeated. Don't signal single step via SIGTRAP.
  358. */
  359. clear_tsk_thread_flag(tsk, TIF_SINGLE_STEP);
  360. return;
  361. /*
  362. * Something tried to access memory that isn't in our memory map..
  363. * Fix it, but check if it's kernel or user first..
  364. */
  365. bad_area:
  366. up_read(&mm->mmap_sem);
  367. /* User mode accesses just cause a SIGSEGV */
  368. if (regs->psw.mask & PSW_MASK_PSTATE) {
  369. tsk->thread.prot_addr = address;
  370. tsk->thread.trap_no = error_code;
  371. do_sigsegv(regs, error_code, si_code, address);
  372. return;
  373. }
  374. no_context:
  375. do_no_context(regs, error_code, address);
  376. }
  377. void __kprobes do_protection_exception(struct pt_regs *regs,
  378. long error_code)
  379. {
  380. /* Protection exception is supressing, decrement psw address. */
  381. regs->psw.addr -= (error_code >> 16);
  382. /*
  383. * Check for low-address protection. This needs to be treated
  384. * as a special case because the translation exception code
  385. * field is not guaranteed to contain valid data in this case.
  386. */
  387. if (unlikely(!(S390_lowcore.trans_exc_code & 4))) {
  388. do_low_address(regs, error_code);
  389. return;
  390. }
  391. do_exception(regs, 4, 1);
  392. }
  393. void __kprobes do_dat_exception(struct pt_regs *regs, long error_code)
  394. {
  395. do_exception(regs, error_code & 0xff, 0);
  396. }
  397. #ifdef CONFIG_64BIT
  398. void __kprobes do_asce_exception(struct pt_regs *regs, unsigned long error_code)
  399. {
  400. struct mm_struct *mm;
  401. struct vm_area_struct *vma;
  402. unsigned long address;
  403. int space;
  404. mm = current->mm;
  405. address = S390_lowcore.trans_exc_code & __FAIL_ADDR_MASK;
  406. space = check_space(current);
  407. if (unlikely(space == 0 || in_atomic() || !mm))
  408. goto no_context;
  409. local_irq_enable();
  410. down_read(&mm->mmap_sem);
  411. vma = find_vma(mm, address);
  412. up_read(&mm->mmap_sem);
  413. if (vma) {
  414. update_mm(mm, current);
  415. return;
  416. }
  417. /* User mode accesses just cause a SIGSEGV */
  418. if (regs->psw.mask & PSW_MASK_PSTATE) {
  419. current->thread.prot_addr = address;
  420. current->thread.trap_no = error_code;
  421. do_sigsegv(regs, error_code, SEGV_MAPERR, address);
  422. return;
  423. }
  424. no_context:
  425. do_no_context(regs, error_code, address);
  426. }
  427. #endif
  428. #ifdef CONFIG_PFAULT
  429. /*
  430. * 'pfault' pseudo page faults routines.
  431. */
  432. static ext_int_info_t ext_int_pfault;
  433. static int pfault_disable = 0;
  434. static int __init nopfault(char *str)
  435. {
  436. pfault_disable = 1;
  437. return 1;
  438. }
  439. __setup("nopfault", nopfault);
  440. typedef struct {
  441. __u16 refdiagc;
  442. __u16 reffcode;
  443. __u16 refdwlen;
  444. __u16 refversn;
  445. __u64 refgaddr;
  446. __u64 refselmk;
  447. __u64 refcmpmk;
  448. __u64 reserved;
  449. } __attribute__ ((packed, aligned(8))) pfault_refbk_t;
  450. int pfault_init(void)
  451. {
  452. pfault_refbk_t refbk =
  453. { 0x258, 0, 5, 2, __LC_CURRENT, 1ULL << 48, 1ULL << 48,
  454. __PF_RES_FIELD };
  455. int rc;
  456. if (!MACHINE_IS_VM || pfault_disable)
  457. return -1;
  458. asm volatile(
  459. " diag %1,%0,0x258\n"
  460. "0: j 2f\n"
  461. "1: la %0,8\n"
  462. "2:\n"
  463. EX_TABLE(0b,1b)
  464. : "=d" (rc) : "a" (&refbk), "m" (refbk) : "cc");
  465. __ctl_set_bit(0, 9);
  466. return rc;
  467. }
  468. void pfault_fini(void)
  469. {
  470. pfault_refbk_t refbk =
  471. { 0x258, 1, 5, 2, 0ULL, 0ULL, 0ULL, 0ULL };
  472. if (!MACHINE_IS_VM || pfault_disable)
  473. return;
  474. __ctl_clear_bit(0,9);
  475. asm volatile(
  476. " diag %0,0,0x258\n"
  477. "0:\n"
  478. EX_TABLE(0b,0b)
  479. : : "a" (&refbk), "m" (refbk) : "cc");
  480. }
  481. static void pfault_interrupt(__u16 error_code)
  482. {
  483. struct task_struct *tsk;
  484. __u16 subcode;
  485. /*
  486. * Get the external interruption subcode & pfault
  487. * initial/completion signal bit. VM stores this
  488. * in the 'cpu address' field associated with the
  489. * external interrupt.
  490. */
  491. subcode = S390_lowcore.cpu_addr;
  492. if ((subcode & 0xff00) != __SUBCODE_MASK)
  493. return;
  494. /*
  495. * Get the token (= address of the task structure of the affected task).
  496. */
  497. tsk = *(struct task_struct **) __LC_PFAULT_INTPARM;
  498. if (subcode & 0x0080) {
  499. /* signal bit is set -> a page has been swapped in by VM */
  500. if (xchg(&tsk->thread.pfault_wait, -1) != 0) {
  501. /* Initial interrupt was faster than the completion
  502. * interrupt. pfault_wait is valid. Set pfault_wait
  503. * back to zero and wake up the process. This can
  504. * safely be done because the task is still sleeping
  505. * and can't produce new pfaults. */
  506. tsk->thread.pfault_wait = 0;
  507. wake_up_process(tsk);
  508. put_task_struct(tsk);
  509. }
  510. } else {
  511. /* signal bit not set -> a real page is missing. */
  512. get_task_struct(tsk);
  513. set_task_state(tsk, TASK_UNINTERRUPTIBLE);
  514. if (xchg(&tsk->thread.pfault_wait, 1) != 0) {
  515. /* Completion interrupt was faster than the initial
  516. * interrupt (swapped in a -1 for pfault_wait). Set
  517. * pfault_wait back to zero and exit. This can be
  518. * done safely because tsk is running in kernel
  519. * mode and can't produce new pfaults. */
  520. tsk->thread.pfault_wait = 0;
  521. set_task_state(tsk, TASK_RUNNING);
  522. put_task_struct(tsk);
  523. } else
  524. set_tsk_need_resched(tsk);
  525. }
  526. }
  527. void __init pfault_irq_init(void)
  528. {
  529. if (!MACHINE_IS_VM)
  530. return;
  531. /*
  532. * Try to get pfault pseudo page faults going.
  533. */
  534. if (register_early_external_interrupt(0x2603, pfault_interrupt,
  535. &ext_int_pfault) != 0)
  536. panic("Couldn't request external interrupt 0x2603");
  537. if (pfault_init() == 0)
  538. return;
  539. /* Tough luck, no pfault. */
  540. pfault_disable = 1;
  541. unregister_early_external_interrupt(0x2603, pfault_interrupt,
  542. &ext_int_pfault);
  543. }
  544. #endif