fault.c 13 KB

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