fault.c 13 KB

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