fault.c 14 KB

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