fault.c 12 KB

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